^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * IBM/3270 Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Author(s):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright IBM Corp. 2003, 2009
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/idals.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /* ioctls for fullscreen 3270 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define TUBICMD _IO('3', 3) /* set ccw command for fs reads. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define TUBOCMD _IO('3', 4) /* set ccw command for fs writes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define TUBGETI _IO('3', 7) /* get ccw command for fs reads. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define TUBGETO _IO('3', 8) /* get ccw command for fs writes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define TUBSETMOD _IO('3',12) /* FIXME: what does it do ?*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define TUBGETMOD _IO('3',13) /* FIXME: what does it do ?*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* Local Channel Commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define TC_WRITE 0x01 /* Write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define TC_RDBUF 0x02 /* Read Buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define TC_EWRITE 0x05 /* Erase write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define TC_READMOD 0x06 /* Read modified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define TC_EWRITEA 0x0d /* Erase write alternate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define TC_WRITESF 0x11 /* Write structured field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Buffer Control Orders */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define TO_SF 0x1d /* Start field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define TO_SBA 0x11 /* Set buffer address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define TO_IC 0x13 /* Insert cursor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define TO_PT 0x05 /* Program tab */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define TO_RA 0x3c /* Repeat to address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define TO_SFE 0x29 /* Start field extended */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define TO_EUA 0x12 /* Erase unprotected to address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define TO_MF 0x2c /* Modify field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define TO_SA 0x28 /* Set attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Field Attribute Bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define TF_INPUT 0x40 /* Visible input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define TF_INPUTN 0x4c /* Invisible input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define TF_INMDT 0xc1 /* Visible, Set-MDT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define TF_LOG 0x60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Character Attribute Bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define TAT_RESET 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define TAT_FIELD 0xc0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define TAT_EXTHI 0x41
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define TAT_COLOR 0x42
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define TAT_CHARS 0x43
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define TAT_TRANS 0x46
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /* Extended-Highlighting Bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define TAX_RESET 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define TAX_BLINK 0xf1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define TAX_REVER 0xf2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define TAX_UNDER 0xf4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* Reset value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define TAR_RESET 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* Color values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define TAC_RESET 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define TAC_BLUE 0xf1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define TAC_RED 0xf2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define TAC_PINK 0xf3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #define TAC_GREEN 0xf4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define TAC_TURQ 0xf5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define TAC_YELLOW 0xf6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define TAC_WHITE 0xf7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define TAC_DEFAULT 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Write Control Characters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define TW_NONE 0x40 /* No particular action */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define TW_KR 0xc2 /* Keyboard restore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define TW_PLUSALARM 0x04 /* Add this bit for alarm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define RAW3270_FIRSTMINOR 1 /* First minor number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define RAW3270_MAXDEVS 255 /* Max number of 3270 devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* For TUBGETMOD and TUBSETMOD. Should include. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct raw3270_iocb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) short model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) short line_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) short col_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) short pf_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) short re_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) short map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct raw3270;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct raw3270_view;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) extern struct class *class3270;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* 3270 CCW request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct raw3270_request {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct list_head list; /* list head for request queueing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct raw3270_view *view; /* view of this request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct ccw1 ccw; /* single ccw. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void *buffer; /* output buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) size_t size; /* size of output buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int rescnt; /* residual count from devstat. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) int rc; /* return code for this request. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Callback for delivering final status. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void (*callback)(struct raw3270_request *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void *callback_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct raw3270_request *raw3270_request_alloc(size_t size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) void raw3270_request_free(struct raw3270_request *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) void raw3270_request_reset(struct raw3270_request *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) void raw3270_request_set_cmd(struct raw3270_request *, u8 cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int raw3270_request_add_data(struct raw3270_request *, void *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) void raw3270_request_set_data(struct raw3270_request *, void *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) void raw3270_request_set_idal(struct raw3270_request *, struct idal_buffer *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) raw3270_request_final(struct raw3270_request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return list_empty(&rq->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) void raw3270_buffer_address(struct raw3270 *, char *, unsigned short);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * Functions of a 3270 view.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct raw3270_fn {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int (*activate)(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void (*deactivate)(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) void (*intv)(struct raw3270_view *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct raw3270_request *, struct irb *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void (*release)(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void (*free)(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void (*resize)(struct raw3270_view *, int, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * View structure chaining. The raw3270_view structure is meant to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * be embedded at the start of the real view data structure, e.g.:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * struct example {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * struct raw3270_view view;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct raw3270_view {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define RAW3270_VIEW_LOCK_IRQ 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define RAW3270_VIEW_LOCK_BH 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) atomic_t ref_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct raw3270 *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct raw3270_fn *fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned int model;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) unsigned int rows, cols; /* # of rows & colums of the view */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) unsigned char *ascebc; /* ascii -> ebcdic table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int raw3270_add_view(struct raw3270_view *, struct raw3270_fn *, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) int raw3270_activate_view(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void raw3270_del_view(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void raw3270_deactivate_view(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct raw3270_view *raw3270_find_view(struct raw3270_fn *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int raw3270_start(struct raw3270_view *, struct raw3270_request *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int raw3270_start_locked(struct raw3270_view *, struct raw3270_request *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int raw3270_start_irq(struct raw3270_view *, struct raw3270_request *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int raw3270_reset(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct raw3270_view *raw3270_view(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int raw3270_view_active(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* Reference count inliner for view structures. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) raw3270_get_view(struct raw3270_view *view)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) atomic_inc(&view->ref_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) extern wait_queue_head_t raw3270_wait_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) raw3270_put_view(struct raw3270_view *view)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (atomic_dec_return(&view->ref_count) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) wake_up(&raw3270_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct raw3270 *raw3270_setup_console(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) void raw3270_wait_cons_dev(struct raw3270 *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* Notifier for device addition/removal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct raw3270_notifier {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void (*create)(int minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) void (*destroy)(int minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int raw3270_register_notifier(struct raw3270_notifier *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) void raw3270_unregister_notifier(struct raw3270_notifier *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) void raw3270_pm_unfreeze(struct raw3270_view *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * Little memory allocator for string objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct list_head update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) unsigned long len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) char string[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) } __attribute__ ((aligned(8)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static inline struct string *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) alloc_string(struct list_head *free_list, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct string *cs, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) size = (len + 7L) & -8L;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) list_for_each_entry(cs, free_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (cs->size < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (cs->size > size + sizeof(struct string)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) char *endaddr = (char *) (cs + 1) + cs->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) tmp = (struct string *) (endaddr - size) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) tmp->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) cs->size -= size + sizeof(struct string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) cs = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) list_del(&cs->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) cs->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) INIT_LIST_HEAD(&cs->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) INIT_LIST_HEAD(&cs->update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static inline unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) free_string(struct list_head *free_list, struct string *cs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct string *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct list_head *p, *left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /* Find out the left neighbour in free memory list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) left = free_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) list_for_each(p, free_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (list_entry(p, struct string, list) > cs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) left = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* Try to merge with right neighbour = next element from left. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (left->next != free_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) tmp = list_entry(left->next, struct string, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if ((char *) (cs + 1) + cs->size == (char *) tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) list_del(&tmp->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) cs->size += tmp->size + sizeof(struct string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* Try to merge with left neighbour. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (left != free_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) tmp = list_entry(left, struct string, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if ((char *) (tmp + 1) + tmp->size == (char *) cs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) tmp->size += cs->size + sizeof(struct string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return tmp->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) __list_add(&cs->list, left, left->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return cs->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) add_string_memory(struct list_head *free_list, void *mem, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct string *cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) cs = (struct string *) mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) cs->size = size - sizeof(struct string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) free_string(free_list, cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)