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