Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/s390/char/sclp_tty.c | |
3 | * SCLP line mode terminal driver. | |
4 | * | |
5 | * S390 version | |
6 | * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation | |
7 | * Author(s): Martin Peschke <mpeschke@de.ibm.com> | |
8 | * Martin Schwidefsky <schwidefsky@de.ibm.com> | |
9 | */ | |
10 | ||
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/kmod.h> | |
13 | #include <linux/tty.h> | |
14 | #include <linux/tty_driver.h> | |
33f0f88f | 15 | #include <linux/tty_flip.h> |
1da177e4 LT |
16 | #include <linux/wait.h> |
17 | #include <linux/slab.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/interrupt.h> | |
21 | #include <asm/uaccess.h> | |
22 | ||
23 | #include "ctrlchar.h" | |
24 | #include "sclp.h" | |
25 | #include "sclp_rw.h" | |
26 | #include "sclp_tty.h" | |
27 | ||
1da177e4 LT |
28 | /* |
29 | * size of a buffer that collects single characters coming in | |
30 | * via sclp_tty_put_char() | |
31 | */ | |
32 | #define SCLP_TTY_BUF_SIZE 512 | |
33 | ||
34 | /* | |
35 | * There is exactly one SCLP terminal, so we can keep things simple | |
36 | * and allocate all variables statically. | |
37 | */ | |
38 | ||
39 | /* Lock to guard over changes to global variables. */ | |
40 | static spinlock_t sclp_tty_lock; | |
41 | /* List of free pages that can be used for console output buffering. */ | |
42 | static struct list_head sclp_tty_pages; | |
43 | /* List of full struct sclp_buffer structures ready for output. */ | |
44 | static struct list_head sclp_tty_outqueue; | |
45 | /* Counter how many buffers are emitted. */ | |
46 | static int sclp_tty_buffer_count; | |
47 | /* Pointer to current console buffer. */ | |
48 | static struct sclp_buffer *sclp_ttybuf; | |
49 | /* Timer for delayed output of console messages. */ | |
50 | static struct timer_list sclp_tty_timer; | |
51 | /* Waitqueue to wait for buffers to get empty. */ | |
52 | static wait_queue_head_t sclp_tty_waitq; | |
53 | ||
54 | static struct tty_struct *sclp_tty; | |
55 | static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE]; | |
56 | static unsigned short int sclp_tty_chars_count; | |
57 | ||
58 | struct tty_driver *sclp_tty_driver; | |
59 | ||
1da177e4 LT |
60 | static struct sclp_ioctls sclp_ioctls; |
61 | static struct sclp_ioctls sclp_ioctls_init = | |
62 | { | |
63 | 8, /* 1 hor. tab. = 8 spaces */ | |
64 | 0, /* no echo of input by this driver */ | |
65 | 80, /* 80 characters/line */ | |
66 | 1, /* write after 1/10 s without final new line */ | |
67 | MAX_KMEM_PAGES, /* quick fix: avoid __alloc_pages */ | |
68 | MAX_KMEM_PAGES, /* take 32/64 pages from kernel memory, */ | |
69 | 0, /* do not convert to lower case */ | |
70 | 0x6c /* to seprate upper and lower case */ | |
71 | /* ('%' in EBCDIC) */ | |
72 | }; | |
73 | ||
74 | /* This routine is called whenever we try to open a SCLP terminal. */ | |
75 | static int | |
76 | sclp_tty_open(struct tty_struct *tty, struct file *filp) | |
77 | { | |
78 | sclp_tty = tty; | |
79 | tty->driver_data = NULL; | |
80 | tty->low_latency = 0; | |
81 | return 0; | |
82 | } | |
83 | ||
84 | /* This routine is called when the SCLP terminal is closed. */ | |
85 | static void | |
86 | sclp_tty_close(struct tty_struct *tty, struct file *filp) | |
87 | { | |
88 | if (tty->count > 1) | |
89 | return; | |
90 | sclp_tty = NULL; | |
91 | } | |
92 | ||
93 | /* execute commands to control the i/o behaviour of the SCLP tty at runtime */ | |
94 | static int | |
95 | sclp_tty_ioctl(struct tty_struct *tty, struct file * file, | |
96 | unsigned int cmd, unsigned long arg) | |
97 | { | |
98 | unsigned long flags; | |
99 | unsigned int obuf; | |
100 | int check; | |
101 | int rc; | |
102 | ||
103 | if (tty->flags & (1 << TTY_IO_ERROR)) | |
104 | return -EIO; | |
105 | rc = 0; | |
106 | check = 0; | |
107 | switch (cmd) { | |
108 | case TIOCSCLPSHTAB: | |
109 | /* set width of horizontal tab */ | |
110 | if (get_user(sclp_ioctls.htab, (unsigned short __user *) arg)) | |
111 | rc = -EFAULT; | |
112 | else | |
113 | check = 1; | |
114 | break; | |
115 | case TIOCSCLPGHTAB: | |
116 | /* get width of horizontal tab */ | |
117 | if (put_user(sclp_ioctls.htab, (unsigned short __user *) arg)) | |
118 | rc = -EFAULT; | |
119 | break; | |
120 | case TIOCSCLPSECHO: | |
121 | /* enable/disable echo of input */ | |
122 | if (get_user(sclp_ioctls.echo, (unsigned char __user *) arg)) | |
123 | rc = -EFAULT; | |
124 | break; | |
125 | case TIOCSCLPGECHO: | |
126 | /* Is echo of input enabled ? */ | |
127 | if (put_user(sclp_ioctls.echo, (unsigned char __user *) arg)) | |
128 | rc = -EFAULT; | |
129 | break; | |
130 | case TIOCSCLPSCOLS: | |
131 | /* set number of columns for output */ | |
132 | if (get_user(sclp_ioctls.columns, (unsigned short __user *) arg)) | |
133 | rc = -EFAULT; | |
134 | else | |
135 | check = 1; | |
136 | break; | |
137 | case TIOCSCLPGCOLS: | |
138 | /* get number of columns for output */ | |
139 | if (put_user(sclp_ioctls.columns, (unsigned short __user *) arg)) | |
140 | rc = -EFAULT; | |
141 | break; | |
142 | case TIOCSCLPSNL: | |
143 | /* enable/disable writing without final new line character */ | |
144 | if (get_user(sclp_ioctls.final_nl, (signed char __user *) arg)) | |
145 | rc = -EFAULT; | |
146 | break; | |
147 | case TIOCSCLPGNL: | |
148 | /* Is writing without final new line character enabled ? */ | |
149 | if (put_user(sclp_ioctls.final_nl, (signed char __user *) arg)) | |
150 | rc = -EFAULT; | |
151 | break; | |
152 | case TIOCSCLPSOBUF: | |
153 | /* | |
154 | * set the maximum buffers size for output, will be rounded | |
155 | * up to next 4kB boundary and stored as number of SCCBs | |
156 | * (4kB Buffers) limitation: 256 x 4kB | |
157 | */ | |
158 | if (get_user(obuf, (unsigned int __user *) arg) == 0) { | |
159 | if (obuf & 0xFFF) | |
160 | sclp_ioctls.max_sccb = (obuf >> 12) + 1; | |
161 | else | |
162 | sclp_ioctls.max_sccb = (obuf >> 12); | |
163 | } else | |
164 | rc = -EFAULT; | |
165 | break; | |
166 | case TIOCSCLPGOBUF: | |
167 | /* get the maximum buffers size for output */ | |
168 | obuf = sclp_ioctls.max_sccb << 12; | |
169 | if (put_user(obuf, (unsigned int __user *) arg)) | |
170 | rc = -EFAULT; | |
171 | break; | |
172 | case TIOCSCLPGKBUF: | |
173 | /* get the number of buffers got from kernel at startup */ | |
174 | if (put_user(sclp_ioctls.kmem_sccb, (unsigned short __user *) arg)) | |
175 | rc = -EFAULT; | |
176 | break; | |
177 | case TIOCSCLPSCASE: | |
178 | /* enable/disable conversion from upper to lower case */ | |
179 | if (get_user(sclp_ioctls.tolower, (unsigned char __user *) arg)) | |
180 | rc = -EFAULT; | |
181 | break; | |
182 | case TIOCSCLPGCASE: | |
183 | /* Is conversion from upper to lower case of input enabled? */ | |
184 | if (put_user(sclp_ioctls.tolower, (unsigned char __user *) arg)) | |
185 | rc = -EFAULT; | |
186 | break; | |
187 | case TIOCSCLPSDELIM: | |
188 | /* | |
189 | * set special character used for separating upper and | |
190 | * lower case, 0x00 disables this feature | |
191 | */ | |
192 | if (get_user(sclp_ioctls.delim, (unsigned char __user *) arg)) | |
193 | rc = -EFAULT; | |
194 | break; | |
195 | case TIOCSCLPGDELIM: | |
196 | /* | |
197 | * get special character used for separating upper and | |
198 | * lower case, 0x00 disables this feature | |
199 | */ | |
200 | if (put_user(sclp_ioctls.delim, (unsigned char __user *) arg)) | |
201 | rc = -EFAULT; | |
202 | break; | |
203 | case TIOCSCLPSINIT: | |
204 | /* set initial (default) sclp ioctls */ | |
205 | sclp_ioctls = sclp_ioctls_init; | |
206 | check = 1; | |
207 | break; | |
208 | default: | |
209 | rc = -ENOIOCTLCMD; | |
210 | break; | |
211 | } | |
212 | if (check) { | |
213 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
214 | if (sclp_ttybuf != NULL) { | |
215 | sclp_set_htab(sclp_ttybuf, sclp_ioctls.htab); | |
216 | sclp_set_columns(sclp_ttybuf, sclp_ioctls.columns); | |
217 | } | |
218 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
219 | } | |
220 | return rc; | |
221 | } | |
222 | ||
223 | /* | |
224 | * This routine returns the numbers of characters the tty driver | |
225 | * will accept for queuing to be written. This number is subject | |
226 | * to change as output buffers get emptied, or if the output flow | |
227 | * control is acted. This is not an exact number because not every | |
228 | * character needs the same space in the sccb. The worst case is | |
229 | * a string of newlines. Every newlines creates a new mto which | |
230 | * needs 8 bytes. | |
231 | */ | |
232 | static int | |
233 | sclp_tty_write_room (struct tty_struct *tty) | |
234 | { | |
235 | unsigned long flags; | |
236 | struct list_head *l; | |
237 | int count; | |
238 | ||
239 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
240 | count = 0; | |
241 | if (sclp_ttybuf != NULL) | |
242 | count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto); | |
243 | list_for_each(l, &sclp_tty_pages) | |
244 | count += NR_EMPTY_MTO_PER_SCCB; | |
245 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
246 | return count; | |
247 | } | |
248 | ||
249 | static void | |
250 | sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc) | |
251 | { | |
252 | unsigned long flags; | |
253 | void *page; | |
254 | ||
255 | do { | |
256 | page = sclp_unmake_buffer(buffer); | |
257 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
258 | /* Remove buffer from outqueue */ | |
259 | list_del(&buffer->list); | |
260 | sclp_tty_buffer_count--; | |
261 | list_add_tail((struct list_head *) page, &sclp_tty_pages); | |
262 | /* Check if there is a pending buffer on the out queue. */ | |
263 | buffer = NULL; | |
264 | if (!list_empty(&sclp_tty_outqueue)) | |
265 | buffer = list_entry(sclp_tty_outqueue.next, | |
266 | struct sclp_buffer, list); | |
267 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
268 | } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback)); | |
269 | wake_up(&sclp_tty_waitq); | |
270 | /* check if the tty needs a wake up call */ | |
271 | if (sclp_tty != NULL) { | |
272 | tty_wakeup(sclp_tty); | |
273 | } | |
274 | } | |
275 | ||
276 | static inline void | |
277 | __sclp_ttybuf_emit(struct sclp_buffer *buffer) | |
278 | { | |
279 | unsigned long flags; | |
280 | int count; | |
281 | int rc; | |
282 | ||
283 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
284 | list_add_tail(&buffer->list, &sclp_tty_outqueue); | |
285 | count = sclp_tty_buffer_count++; | |
286 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
287 | if (count) | |
288 | return; | |
289 | rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback); | |
290 | if (rc) | |
291 | sclp_ttybuf_callback(buffer, rc); | |
292 | } | |
293 | ||
294 | /* | |
295 | * When this routine is called from the timer then we flush the | |
296 | * temporary write buffer. | |
297 | */ | |
298 | static void | |
299 | sclp_tty_timeout(unsigned long data) | |
300 | { | |
301 | unsigned long flags; | |
302 | struct sclp_buffer *buf; | |
303 | ||
304 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
305 | buf = sclp_ttybuf; | |
306 | sclp_ttybuf = NULL; | |
307 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
308 | ||
309 | if (buf != NULL) { | |
310 | __sclp_ttybuf_emit(buf); | |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * Write a string to the sclp tty. | |
316 | */ | |
317 | static void | |
318 | sclp_tty_write_string(const unsigned char *str, int count) | |
319 | { | |
320 | unsigned long flags; | |
321 | void *page; | |
322 | int written; | |
323 | struct sclp_buffer *buf; | |
324 | ||
325 | if (count <= 0) | |
326 | return; | |
327 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
328 | do { | |
329 | /* Create a sclp output buffer if none exists yet */ | |
330 | if (sclp_ttybuf == NULL) { | |
331 | while (list_empty(&sclp_tty_pages)) { | |
332 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
d1e23375 | 333 | if (in_interrupt()) |
1da177e4 LT |
334 | sclp_sync_wait(); |
335 | else | |
336 | wait_event(sclp_tty_waitq, | |
337 | !list_empty(&sclp_tty_pages)); | |
338 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
339 | } | |
340 | page = sclp_tty_pages.next; | |
341 | list_del((struct list_head *) page); | |
342 | sclp_ttybuf = sclp_make_buffer(page, | |
343 | sclp_ioctls.columns, | |
344 | sclp_ioctls.htab); | |
345 | } | |
346 | /* try to write the string to the current output buffer */ | |
347 | written = sclp_write(sclp_ttybuf, str, count); | |
348 | if (written == count) | |
349 | break; | |
350 | /* | |
351 | * Not all characters could be written to the current | |
352 | * output buffer. Emit the buffer, create a new buffer | |
353 | * and then output the rest of the string. | |
354 | */ | |
355 | buf = sclp_ttybuf; | |
356 | sclp_ttybuf = NULL; | |
357 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
358 | __sclp_ttybuf_emit(buf); | |
359 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
360 | str += written; | |
361 | count -= written; | |
362 | } while (count > 0); | |
363 | /* Setup timer to output current console buffer after 1/10 second */ | |
364 | if (sclp_ioctls.final_nl) { | |
365 | if (sclp_ttybuf != NULL && | |
366 | sclp_chars_in_buffer(sclp_ttybuf) != 0 && | |
367 | !timer_pending(&sclp_tty_timer)) { | |
368 | init_timer(&sclp_tty_timer); | |
369 | sclp_tty_timer.function = sclp_tty_timeout; | |
370 | sclp_tty_timer.data = 0UL; | |
371 | sclp_tty_timer.expires = jiffies + HZ/10; | |
372 | add_timer(&sclp_tty_timer); | |
373 | } | |
374 | } else { | |
375 | if (sclp_ttybuf != NULL && | |
376 | sclp_chars_in_buffer(sclp_ttybuf) != 0) { | |
377 | buf = sclp_ttybuf; | |
378 | sclp_ttybuf = NULL; | |
379 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
380 | __sclp_ttybuf_emit(buf); | |
381 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
382 | } | |
383 | } | |
384 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
385 | } | |
386 | ||
387 | /* | |
388 | * This routine is called by the kernel to write a series of characters to the | |
389 | * tty device. The characters may come from user space or kernel space. This | |
390 | * routine will return the number of characters actually accepted for writing. | |
391 | */ | |
392 | static int | |
393 | sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) | |
394 | { | |
395 | if (sclp_tty_chars_count > 0) { | |
396 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); | |
397 | sclp_tty_chars_count = 0; | |
398 | } | |
399 | sclp_tty_write_string(buf, count); | |
400 | return count; | |
401 | } | |
402 | ||
403 | /* | |
404 | * This routine is called by the kernel to write a single character to the tty | |
405 | * device. If the kernel uses this routine, it must call the flush_chars() | |
406 | * routine (if defined) when it is done stuffing characters into the driver. | |
407 | * | |
408 | * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver. | |
409 | * If the given character is a '\n' the contents of the SCLP write buffer | |
410 | * - including previous characters from sclp_tty_put_char() and strings from | |
411 | * sclp_write() without final '\n' - will be written. | |
412 | */ | |
9e7c9a19 | 413 | static int |
1da177e4 LT |
414 | sclp_tty_put_char(struct tty_struct *tty, unsigned char ch) |
415 | { | |
416 | sclp_tty_chars[sclp_tty_chars_count++] = ch; | |
417 | if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) { | |
418 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); | |
419 | sclp_tty_chars_count = 0; | |
9e7c9a19 | 420 | } return 1; |
1da177e4 LT |
421 | } |
422 | ||
423 | /* | |
424 | * This routine is called by the kernel after it has written a series of | |
425 | * characters to the tty device using put_char(). | |
426 | */ | |
427 | static void | |
428 | sclp_tty_flush_chars(struct tty_struct *tty) | |
429 | { | |
430 | if (sclp_tty_chars_count > 0) { | |
431 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); | |
432 | sclp_tty_chars_count = 0; | |
433 | } | |
434 | } | |
435 | ||
436 | /* | |
437 | * This routine returns the number of characters in the write buffer of the | |
438 | * SCLP driver. The provided number includes all characters that are stored | |
439 | * in the SCCB (will be written next time the SCLP is not busy) as well as | |
440 | * characters in the write buffer (will not be written as long as there is a | |
441 | * final line feed missing). | |
442 | */ | |
443 | static int | |
444 | sclp_tty_chars_in_buffer(struct tty_struct *tty) | |
445 | { | |
446 | unsigned long flags; | |
447 | struct list_head *l; | |
448 | struct sclp_buffer *t; | |
449 | int count; | |
450 | ||
451 | spin_lock_irqsave(&sclp_tty_lock, flags); | |
452 | count = 0; | |
453 | if (sclp_ttybuf != NULL) | |
454 | count = sclp_chars_in_buffer(sclp_ttybuf); | |
455 | list_for_each(l, &sclp_tty_outqueue) { | |
456 | t = list_entry(l, struct sclp_buffer, list); | |
457 | count += sclp_chars_in_buffer(t); | |
458 | } | |
459 | spin_unlock_irqrestore(&sclp_tty_lock, flags); | |
460 | return count; | |
461 | } | |
462 | ||
463 | /* | |
464 | * removes all content from buffers of low level driver | |
465 | */ | |
466 | static void | |
467 | sclp_tty_flush_buffer(struct tty_struct *tty) | |
468 | { | |
469 | if (sclp_tty_chars_count > 0) { | |
470 | sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count); | |
471 | sclp_tty_chars_count = 0; | |
472 | } | |
473 | } | |
474 | ||
475 | /* | |
476 | * push input to tty | |
477 | */ | |
478 | static void | |
479 | sclp_tty_input(unsigned char* buf, unsigned int count) | |
480 | { | |
481 | unsigned int cchar; | |
482 | ||
483 | /* | |
484 | * If this tty driver is currently closed | |
485 | * then throw the received input away. | |
486 | */ | |
487 | if (sclp_tty == NULL) | |
488 | return; | |
489 | cchar = ctrlchar_handle(buf, count, sclp_tty); | |
490 | switch (cchar & CTRLCHAR_MASK) { | |
491 | case CTRLCHAR_SYSRQ: | |
492 | break; | |
493 | case CTRLCHAR_CTRL: | |
33f0f88f | 494 | tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL); |
1da177e4 LT |
495 | tty_flip_buffer_push(sclp_tty); |
496 | break; | |
497 | case CTRLCHAR_NONE: | |
498 | /* send (normal) input to line discipline */ | |
1da177e4 | 499 | if (count < 2 || |
33f0f88f AC |
500 | (strncmp((const char *) buf + count - 2, "^n", 2) && |
501 | strncmp((const char *) buf + count - 2, "\252n", 2))) { | |
502 | /* add the auto \n */ | |
503 | tty_insert_flip_string(sclp_tty, buf, count); | |
504 | tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL); | |
1da177e4 | 505 | } else |
33f0f88f | 506 | tty_insert_flip_string(sclp_tty, buf, count - 2); |
1da177e4 LT |
507 | tty_flip_buffer_push(sclp_tty); |
508 | break; | |
509 | } | |
510 | } | |
511 | ||
512 | /* | |
513 | * get a EBCDIC string in upper/lower case, | |
514 | * find out characters in lower/upper case separated by a special character, | |
515 | * modifiy original string, | |
516 | * returns length of resulting string | |
517 | */ | |
518 | static int | |
519 | sclp_switch_cases(unsigned char *buf, int count, | |
520 | unsigned char delim, int tolower) | |
521 | { | |
522 | unsigned char *ip, *op; | |
523 | int toggle; | |
524 | ||
525 | /* initially changing case is off */ | |
526 | toggle = 0; | |
527 | ip = op = buf; | |
528 | while (count-- > 0) { | |
529 | /* compare with special character */ | |
530 | if (*ip == delim) { | |
531 | /* followed by another special character? */ | |
532 | if (count && ip[1] == delim) { | |
533 | /* | |
534 | * ... then put a single copy of the special | |
535 | * character to the output string | |
536 | */ | |
537 | *op++ = *ip++; | |
538 | count--; | |
539 | } else | |
540 | /* | |
541 | * ... special character follower by a normal | |
542 | * character toggles the case change behaviour | |
543 | */ | |
544 | toggle = ~toggle; | |
545 | /* skip special character */ | |
546 | ip++; | |
547 | } else | |
548 | /* not the special character */ | |
549 | if (toggle) | |
550 | /* but case switching is on */ | |
551 | if (tolower) | |
552 | /* switch to uppercase */ | |
553 | *op++ = _ebc_toupper[(int) *ip++]; | |
554 | else | |
555 | /* switch to lowercase */ | |
556 | *op++ = _ebc_tolower[(int) *ip++]; | |
557 | else | |
558 | /* no case switching, copy the character */ | |
559 | *op++ = *ip++; | |
560 | } | |
561 | /* return length of reformatted string. */ | |
562 | return op - buf; | |
563 | } | |
564 | ||
565 | static void | |
566 | sclp_get_input(unsigned char *start, unsigned char *end) | |
567 | { | |
568 | int count; | |
569 | ||
570 | count = end - start; | |
571 | /* | |
572 | * if set in ioctl convert EBCDIC to lower case | |
573 | * (modify original input in SCCB) | |
574 | */ | |
575 | if (sclp_ioctls.tolower) | |
576 | EBC_TOLOWER(start, count); | |
577 | ||
578 | /* | |
579 | * if set in ioctl find out characters in lower or upper case | |
580 | * (depends on current case) separated by a special character, | |
581 | * works on EBCDIC | |
582 | */ | |
583 | if (sclp_ioctls.delim) | |
584 | count = sclp_switch_cases(start, count, | |
585 | sclp_ioctls.delim, | |
586 | sclp_ioctls.tolower); | |
587 | ||
588 | /* convert EBCDIC to ASCII (modify original input in SCCB) */ | |
589 | sclp_ebcasc_str(start, count); | |
590 | ||
591 | /* if set in ioctl write operators input to console */ | |
592 | if (sclp_ioctls.echo) | |
593 | sclp_tty_write(sclp_tty, start, count); | |
594 | ||
595 | /* transfer input to high level driver */ | |
596 | sclp_tty_input(start, count); | |
597 | } | |
598 | ||
599 | static inline struct gds_vector * | |
600 | find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id) | |
601 | { | |
602 | struct gds_vector *vec; | |
603 | ||
604 | for (vec = start; vec < end; vec = (void *) vec + vec->length) | |
605 | if (vec->gds_id == id) | |
606 | return vec; | |
607 | return NULL; | |
608 | } | |
609 | ||
610 | static inline struct gds_subvector * | |
611 | find_gds_subvector(struct gds_subvector *start, | |
612 | struct gds_subvector *end, u8 key) | |
613 | { | |
614 | struct gds_subvector *subvec; | |
615 | ||
616 | for (subvec = start; subvec < end; | |
617 | subvec = (void *) subvec + subvec->length) | |
618 | if (subvec->key == key) | |
619 | return subvec; | |
620 | return NULL; | |
621 | } | |
622 | ||
623 | static inline void | |
624 | sclp_eval_selfdeftextmsg(struct gds_subvector *start, | |
625 | struct gds_subvector *end) | |
626 | { | |
627 | struct gds_subvector *subvec; | |
628 | ||
629 | subvec = start; | |
630 | while (subvec < end) { | |
631 | subvec = find_gds_subvector(subvec, end, 0x30); | |
632 | if (!subvec) | |
633 | break; | |
634 | sclp_get_input((unsigned char *)(subvec + 1), | |
635 | (unsigned char *) subvec + subvec->length); | |
636 | subvec = (void *) subvec + subvec->length; | |
637 | } | |
638 | } | |
639 | ||
640 | static inline void | |
641 | sclp_eval_textcmd(struct gds_subvector *start, | |
642 | struct gds_subvector *end) | |
643 | { | |
644 | struct gds_subvector *subvec; | |
645 | ||
646 | subvec = start; | |
647 | while (subvec < end) { | |
648 | subvec = find_gds_subvector(subvec, end, | |
6d4740c8 | 649 | GDS_KEY_SELFDEFTEXTMSG); |
1da177e4 LT |
650 | if (!subvec) |
651 | break; | |
652 | sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1), | |
653 | (void *)subvec + subvec->length); | |
654 | subvec = (void *) subvec + subvec->length; | |
655 | } | |
656 | } | |
657 | ||
658 | static inline void | |
659 | sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end) | |
660 | { | |
661 | struct gds_vector *vec; | |
662 | ||
663 | vec = start; | |
664 | while (vec < end) { | |
6d4740c8 | 665 | vec = find_gds_vector(vec, end, GDS_ID_TEXTCMD); |
1da177e4 LT |
666 | if (!vec) |
667 | break; | |
668 | sclp_eval_textcmd((struct gds_subvector *)(vec + 1), | |
669 | (void *) vec + vec->length); | |
670 | vec = (void *) vec + vec->length; | |
671 | } | |
672 | } | |
673 | ||
674 | ||
675 | static inline void | |
676 | sclp_eval_mdsmu(struct gds_vector *start, void *end) | |
677 | { | |
678 | struct gds_vector *vec; | |
679 | ||
680 | vec = find_gds_vector(start, end, GDS_ID_CPMSU); | |
681 | if (vec) | |
682 | sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length); | |
683 | } | |
684 | ||
685 | static void | |
686 | sclp_tty_receiver(struct evbuf_header *evbuf) | |
687 | { | |
688 | struct gds_vector *start, *end, *vec; | |
689 | ||
690 | start = (struct gds_vector *)(evbuf + 1); | |
691 | end = (void *) evbuf + evbuf->length; | |
692 | vec = find_gds_vector(start, end, GDS_ID_MDSMU); | |
693 | if (vec) | |
694 | sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length); | |
695 | } | |
696 | ||
697 | static void | |
698 | sclp_tty_state_change(struct sclp_register *reg) | |
699 | { | |
700 | } | |
701 | ||
702 | static struct sclp_register sclp_input_event = | |
703 | { | |
6d4740c8 | 704 | .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, |
1da177e4 LT |
705 | .state_change_fn = sclp_tty_state_change, |
706 | .receiver_fn = sclp_tty_receiver | |
707 | }; | |
708 | ||
b68e31d0 | 709 | static const struct tty_operations sclp_ops = { |
1da177e4 LT |
710 | .open = sclp_tty_open, |
711 | .close = sclp_tty_close, | |
712 | .write = sclp_tty_write, | |
713 | .put_char = sclp_tty_put_char, | |
714 | .flush_chars = sclp_tty_flush_chars, | |
715 | .write_room = sclp_tty_write_room, | |
716 | .chars_in_buffer = sclp_tty_chars_in_buffer, | |
717 | .flush_buffer = sclp_tty_flush_buffer, | |
718 | .ioctl = sclp_tty_ioctl, | |
719 | }; | |
720 | ||
2b67fc46 | 721 | static int __init |
1da177e4 LT |
722 | sclp_tty_init(void) |
723 | { | |
724 | struct tty_driver *driver; | |
725 | void *page; | |
726 | int i; | |
727 | int rc; | |
728 | ||
729 | if (!CONSOLE_IS_SCLP) | |
730 | return 0; | |
731 | driver = alloc_tty_driver(1); | |
732 | if (!driver) | |
733 | return -ENOMEM; | |
734 | ||
735 | rc = sclp_rw_init(); | |
736 | if (rc) { | |
1da177e4 LT |
737 | put_tty_driver(driver); |
738 | return rc; | |
739 | } | |
740 | /* Allocate pages for output buffering */ | |
741 | INIT_LIST_HEAD(&sclp_tty_pages); | |
742 | for (i = 0; i < MAX_KMEM_PAGES; i++) { | |
743 | page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); | |
744 | if (page == NULL) { | |
745 | put_tty_driver(driver); | |
746 | return -ENOMEM; | |
747 | } | |
748 | list_add_tail((struct list_head *) page, &sclp_tty_pages); | |
749 | } | |
750 | INIT_LIST_HEAD(&sclp_tty_outqueue); | |
751 | spin_lock_init(&sclp_tty_lock); | |
752 | init_waitqueue_head(&sclp_tty_waitq); | |
753 | init_timer(&sclp_tty_timer); | |
754 | sclp_ttybuf = NULL; | |
755 | sclp_tty_buffer_count = 0; | |
756 | if (MACHINE_IS_VM) { | |
757 | /* | |
758 | * save 4 characters for the CPU number | |
759 | * written at start of each line by VM/CP | |
760 | */ | |
761 | sclp_ioctls_init.columns = 76; | |
762 | /* case input lines to lowercase */ | |
763 | sclp_ioctls_init.tolower = 1; | |
764 | } | |
765 | sclp_ioctls = sclp_ioctls_init; | |
766 | sclp_tty_chars_count = 0; | |
767 | sclp_tty = NULL; | |
768 | ||
769 | rc = sclp_register(&sclp_input_event); | |
770 | if (rc) { | |
771 | put_tty_driver(driver); | |
772 | return rc; | |
773 | } | |
774 | ||
775 | driver->owner = THIS_MODULE; | |
776 | driver->driver_name = "sclp_line"; | |
777 | driver->name = "sclp_line"; | |
778 | driver->major = TTY_MAJOR; | |
779 | driver->minor_start = 64; | |
780 | driver->type = TTY_DRIVER_TYPE_SYSTEM; | |
781 | driver->subtype = SYSTEM_TYPE_TTY; | |
782 | driver->init_termios = tty_std_termios; | |
783 | driver->init_termios.c_iflag = IGNBRK | IGNPAR; | |
784 | driver->init_termios.c_oflag = ONLCR | XTABS; | |
785 | driver->init_termios.c_lflag = ISIG | ECHO; | |
786 | driver->flags = TTY_DRIVER_REAL_RAW; | |
787 | tty_set_operations(driver, &sclp_ops); | |
788 | rc = tty_register_driver(driver); | |
789 | if (rc) { | |
1da177e4 LT |
790 | put_tty_driver(driver); |
791 | return rc; | |
792 | } | |
793 | sclp_tty_driver = driver; | |
794 | return 0; | |
795 | } | |
796 | module_init(sclp_tty_init); |