]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/s390/char/sclp_tty.c
[S390] sclp_tty: remove ioctl interface.
[karo-tx-linux.git] / drivers / s390 / char / sclp_tty.c
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
11 #include <linux/module.h>
12 #include <linux/kmod.h>
13 #include <linux/tty.h>
14 #include <linux/tty_driver.h>
15 #include <linux/tty_flip.h>
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
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
60 static int sclp_tty_tolower;
61 static int sclp_tty_columns = 80;
62
63 #define SPACES_PER_TAB 8
64 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
65
66 /* This routine is called whenever we try to open a SCLP terminal. */
67 static int
68 sclp_tty_open(struct tty_struct *tty, struct file *filp)
69 {
70         sclp_tty = tty;
71         tty->driver_data = NULL;
72         tty->low_latency = 0;
73         return 0;
74 }
75
76 /* This routine is called when the SCLP terminal is closed. */
77 static void
78 sclp_tty_close(struct tty_struct *tty, struct file *filp)
79 {
80         if (tty->count > 1)
81                 return;
82         sclp_tty = NULL;
83 }
84
85 /*
86  * This routine returns the numbers of characters the tty driver
87  * will accept for queuing to be written.  This number is subject
88  * to change as output buffers get emptied, or if the output flow
89  * control is acted. This is not an exact number because not every
90  * character needs the same space in the sccb. The worst case is
91  * a string of newlines. Every newlines creates a new mto which
92  * needs 8 bytes.
93  */
94 static int
95 sclp_tty_write_room (struct tty_struct *tty)
96 {
97         unsigned long flags;
98         struct list_head *l;
99         int count;
100
101         spin_lock_irqsave(&sclp_tty_lock, flags);
102         count = 0;
103         if (sclp_ttybuf != NULL)
104                 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
105         list_for_each(l, &sclp_tty_pages)
106                 count += NR_EMPTY_MTO_PER_SCCB;
107         spin_unlock_irqrestore(&sclp_tty_lock, flags);
108         return count;
109 }
110
111 static void
112 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
113 {
114         unsigned long flags;
115         void *page;
116
117         do {
118                 page = sclp_unmake_buffer(buffer);
119                 spin_lock_irqsave(&sclp_tty_lock, flags);
120                 /* Remove buffer from outqueue */
121                 list_del(&buffer->list);
122                 sclp_tty_buffer_count--;
123                 list_add_tail((struct list_head *) page, &sclp_tty_pages);
124                 /* Check if there is a pending buffer on the out queue. */
125                 buffer = NULL;
126                 if (!list_empty(&sclp_tty_outqueue))
127                         buffer = list_entry(sclp_tty_outqueue.next,
128                                             struct sclp_buffer, list);
129                 spin_unlock_irqrestore(&sclp_tty_lock, flags);
130         } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
131         wake_up(&sclp_tty_waitq);
132         /* check if the tty needs a wake up call */
133         if (sclp_tty != NULL) {
134                 tty_wakeup(sclp_tty);
135         }
136 }
137
138 static inline void
139 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
140 {
141         unsigned long flags;
142         int count;
143         int rc;
144
145         spin_lock_irqsave(&sclp_tty_lock, flags);
146         list_add_tail(&buffer->list, &sclp_tty_outqueue);
147         count = sclp_tty_buffer_count++;
148         spin_unlock_irqrestore(&sclp_tty_lock, flags);
149         if (count)
150                 return;
151         rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
152         if (rc)
153                 sclp_ttybuf_callback(buffer, rc);
154 }
155
156 /*
157  * When this routine is called from the timer then we flush the
158  * temporary write buffer.
159  */
160 static void
161 sclp_tty_timeout(unsigned long data)
162 {
163         unsigned long flags;
164         struct sclp_buffer *buf;
165
166         spin_lock_irqsave(&sclp_tty_lock, flags);
167         buf = sclp_ttybuf;
168         sclp_ttybuf = NULL;
169         spin_unlock_irqrestore(&sclp_tty_lock, flags);
170
171         if (buf != NULL) {
172                 __sclp_ttybuf_emit(buf);
173         }
174 }
175
176 /*
177  * Write a string to the sclp tty.
178  */
179 static void
180 sclp_tty_write_string(const unsigned char *str, int count)
181 {
182         unsigned long flags;
183         void *page;
184         int written;
185         struct sclp_buffer *buf;
186
187         if (count <= 0)
188                 return;
189         spin_lock_irqsave(&sclp_tty_lock, flags);
190         do {
191                 /* Create a sclp output buffer if none exists yet */
192                 if (sclp_ttybuf == NULL) {
193                         while (list_empty(&sclp_tty_pages)) {
194                                 spin_unlock_irqrestore(&sclp_tty_lock, flags);
195                                 if (in_interrupt())
196                                         sclp_sync_wait();
197                                 else
198                                         wait_event(sclp_tty_waitq,
199                                                 !list_empty(&sclp_tty_pages));
200                                 spin_lock_irqsave(&sclp_tty_lock, flags);
201                         }
202                         page = sclp_tty_pages.next;
203                         list_del((struct list_head *) page);
204                         sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
205                                                        SPACES_PER_TAB);
206                 }
207                 /* try to write the string to the current output buffer */
208                 written = sclp_write(sclp_ttybuf, str, count);
209                 if (written == count)
210                         break;
211                 /*
212                  * Not all characters could be written to the current
213                  * output buffer. Emit the buffer, create a new buffer
214                  * and then output the rest of the string.
215                  */
216                 buf = sclp_ttybuf;
217                 sclp_ttybuf = NULL;
218                 spin_unlock_irqrestore(&sclp_tty_lock, flags);
219                 __sclp_ttybuf_emit(buf);
220                 spin_lock_irqsave(&sclp_tty_lock, flags);
221                 str += written;
222                 count -= written;
223         } while (count > 0);
224         /* Setup timer to output current console buffer after 1/10 second */
225         if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
226             !timer_pending(&sclp_tty_timer)) {
227                 init_timer(&sclp_tty_timer);
228                 sclp_tty_timer.function = sclp_tty_timeout;
229                 sclp_tty_timer.data = 0UL;
230                 sclp_tty_timer.expires = jiffies + HZ/10;
231                 add_timer(&sclp_tty_timer);
232         }
233         spin_unlock_irqrestore(&sclp_tty_lock, flags);
234 }
235
236 /*
237  * This routine is called by the kernel to write a series of characters to the
238  * tty device. The characters may come from user space or kernel space. This
239  * routine will return the number of characters actually accepted for writing.
240  */
241 static int
242 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
243 {
244         if (sclp_tty_chars_count > 0) {
245                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
246                 sclp_tty_chars_count = 0;
247         }
248         sclp_tty_write_string(buf, count);
249         return count;
250 }
251
252 /*
253  * This routine is called by the kernel to write a single character to the tty
254  * device. If the kernel uses this routine, it must call the flush_chars()
255  * routine (if defined) when it is done stuffing characters into the driver.
256  *
257  * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
258  * If the given character is a '\n' the contents of the SCLP write buffer
259  * - including previous characters from sclp_tty_put_char() and strings from
260  * sclp_write() without final '\n' - will be written.
261  */
262 static int
263 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
264 {
265         sclp_tty_chars[sclp_tty_chars_count++] = ch;
266         if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
267                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
268                 sclp_tty_chars_count = 0;
269         } return 1;
270 }
271
272 /*
273  * This routine is called by the kernel after it has written a series of
274  * characters to the tty device using put_char().
275  */
276 static void
277 sclp_tty_flush_chars(struct tty_struct *tty)
278 {
279         if (sclp_tty_chars_count > 0) {
280                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
281                 sclp_tty_chars_count = 0;
282         }
283 }
284
285 /*
286  * This routine returns the number of characters in the write buffer of the
287  * SCLP driver. The provided number includes all characters that are stored
288  * in the SCCB (will be written next time the SCLP is not busy) as well as
289  * characters in the write buffer (will not be written as long as there is a
290  * final line feed missing).
291  */
292 static int
293 sclp_tty_chars_in_buffer(struct tty_struct *tty)
294 {
295         unsigned long flags;
296         struct list_head *l;
297         struct sclp_buffer *t;
298         int count;
299
300         spin_lock_irqsave(&sclp_tty_lock, flags);
301         count = 0;
302         if (sclp_ttybuf != NULL)
303                 count = sclp_chars_in_buffer(sclp_ttybuf);
304         list_for_each(l, &sclp_tty_outqueue) {
305                 t = list_entry(l, struct sclp_buffer, list);
306                 count += sclp_chars_in_buffer(t);
307         }
308         spin_unlock_irqrestore(&sclp_tty_lock, flags);
309         return count;
310 }
311
312 /*
313  * removes all content from buffers of low level driver
314  */
315 static void
316 sclp_tty_flush_buffer(struct tty_struct *tty)
317 {
318         if (sclp_tty_chars_count > 0) {
319                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count);
320                 sclp_tty_chars_count = 0;
321         }
322 }
323
324 /*
325  * push input to tty
326  */
327 static void
328 sclp_tty_input(unsigned char* buf, unsigned int count)
329 {
330         unsigned int cchar;
331
332         /*
333          * If this tty driver is currently closed
334          * then throw the received input away.
335          */
336         if (sclp_tty == NULL)
337                 return;
338         cchar = ctrlchar_handle(buf, count, sclp_tty);
339         switch (cchar & CTRLCHAR_MASK) {
340         case CTRLCHAR_SYSRQ:
341                 break;
342         case CTRLCHAR_CTRL:
343                 tty_insert_flip_char(sclp_tty, cchar, TTY_NORMAL);
344                 tty_flip_buffer_push(sclp_tty);
345                 break;
346         case CTRLCHAR_NONE:
347                 /* send (normal) input to line discipline */
348                 if (count < 2 ||
349                     (strncmp((const char *) buf + count - 2, "^n", 2) &&
350                      strncmp((const char *) buf + count - 2, "\252n", 2))) {
351                         /* add the auto \n */
352                         tty_insert_flip_string(sclp_tty, buf, count);
353                         tty_insert_flip_char(sclp_tty, '\n', TTY_NORMAL);
354                 } else
355                         tty_insert_flip_string(sclp_tty, buf, count - 2);
356                 tty_flip_buffer_push(sclp_tty);
357                 break;
358         }
359 }
360
361 /*
362  * get a EBCDIC string in upper/lower case,
363  * find out characters in lower/upper case separated by a special character,
364  * modifiy original string,
365  * returns length of resulting string
366  */
367 static int sclp_switch_cases(unsigned char *buf, int count)
368 {
369         unsigned char *ip, *op;
370         int toggle;
371
372         /* initially changing case is off */
373         toggle = 0;
374         ip = op = buf;
375         while (count-- > 0) {
376                 /* compare with special character */
377                 if (*ip == CASE_DELIMITER) {
378                         /* followed by another special character? */
379                         if (count && ip[1] == CASE_DELIMITER) {
380                                 /*
381                                  * ... then put a single copy of the special
382                                  * character to the output string
383                                  */
384                                 *op++ = *ip++;
385                                 count--;
386                         } else
387                                 /*
388                                  * ... special character follower by a normal
389                                  * character toggles the case change behaviour
390                                  */
391                                 toggle = ~toggle;
392                         /* skip special character */
393                         ip++;
394                 } else
395                         /* not the special character */
396                         if (toggle)
397                                 /* but case switching is on */
398                                 if (sclp_tty_tolower)
399                                         /* switch to uppercase */
400                                         *op++ = _ebc_toupper[(int) *ip++];
401                                 else
402                                         /* switch to lowercase */
403                                         *op++ = _ebc_tolower[(int) *ip++];
404                         else
405                                 /* no case switching, copy the character */
406                                 *op++ = *ip++;
407         }
408         /* return length of reformatted string. */
409         return op - buf;
410 }
411
412 static void
413 sclp_get_input(unsigned char *start, unsigned char *end)
414 {
415         int count;
416
417         count = end - start;
418         if (sclp_tty_tolower)
419                 EBC_TOLOWER(start, count);
420         count = sclp_switch_cases(start, count);
421         /* convert EBCDIC to ASCII (modify original input in SCCB) */
422         sclp_ebcasc_str(start, count);
423
424         /* transfer input to high level driver */
425         sclp_tty_input(start, count);
426 }
427
428 static inline struct gds_vector *
429 find_gds_vector(struct gds_vector *start, struct gds_vector *end, u16 id)
430 {
431         struct gds_vector *vec;
432
433         for (vec = start; vec < end; vec = (void *) vec + vec->length)
434                 if (vec->gds_id == id)
435                         return vec;
436         return NULL;
437 }
438
439 static inline struct gds_subvector *
440 find_gds_subvector(struct gds_subvector *start,
441                    struct gds_subvector *end, u8 key)
442 {
443         struct gds_subvector *subvec;
444
445         for (subvec = start; subvec < end;
446              subvec = (void *) subvec + subvec->length)
447                 if (subvec->key == key)
448                         return subvec;
449         return NULL;
450 }
451
452 static inline void
453 sclp_eval_selfdeftextmsg(struct gds_subvector *start,
454                          struct gds_subvector *end)
455 {
456         struct gds_subvector *subvec;
457
458         subvec = start;
459         while (subvec < end) {
460                 subvec = find_gds_subvector(subvec, end, 0x30);
461                 if (!subvec)
462                         break;
463                 sclp_get_input((unsigned char *)(subvec + 1),
464                                (unsigned char *) subvec + subvec->length);
465                 subvec = (void *) subvec + subvec->length;
466         }
467 }
468
469 static inline void
470 sclp_eval_textcmd(struct gds_subvector *start,
471                   struct gds_subvector *end)
472 {
473         struct gds_subvector *subvec;
474
475         subvec = start;
476         while (subvec < end) {
477                 subvec = find_gds_subvector(subvec, end,
478                                             GDS_KEY_SELFDEFTEXTMSG);
479                 if (!subvec)
480                         break;
481                 sclp_eval_selfdeftextmsg((struct gds_subvector *)(subvec + 1),
482                                          (void *)subvec + subvec->length);
483                 subvec = (void *) subvec + subvec->length;
484         }
485 }
486
487 static inline void
488 sclp_eval_cpmsu(struct gds_vector *start, struct gds_vector *end)
489 {
490         struct gds_vector *vec;
491
492         vec = start;
493         while (vec < end) {
494                 vec = find_gds_vector(vec, end, GDS_ID_TEXTCMD);
495                 if (!vec)
496                         break;
497                 sclp_eval_textcmd((struct gds_subvector *)(vec + 1),
498                                   (void *) vec + vec->length);
499                 vec = (void *) vec + vec->length;
500         }
501 }
502
503
504 static inline void
505 sclp_eval_mdsmu(struct gds_vector *start, void *end)
506 {
507         struct gds_vector *vec;
508
509         vec = find_gds_vector(start, end, GDS_ID_CPMSU);
510         if (vec)
511                 sclp_eval_cpmsu(vec + 1, (void *) vec + vec->length);
512 }
513
514 static void
515 sclp_tty_receiver(struct evbuf_header *evbuf)
516 {
517         struct gds_vector *start, *end, *vec;
518
519         start = (struct gds_vector *)(evbuf + 1);
520         end = (void *) evbuf + evbuf->length;
521         vec = find_gds_vector(start, end, GDS_ID_MDSMU);
522         if (vec)
523                 sclp_eval_mdsmu(vec + 1, (void *) vec + vec->length);
524 }
525
526 static void
527 sclp_tty_state_change(struct sclp_register *reg)
528 {
529 }
530
531 static struct sclp_register sclp_input_event =
532 {
533         .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
534         .state_change_fn = sclp_tty_state_change,
535         .receiver_fn = sclp_tty_receiver
536 };
537
538 static const struct tty_operations sclp_ops = {
539         .open = sclp_tty_open,
540         .close = sclp_tty_close,
541         .write = sclp_tty_write,
542         .put_char = sclp_tty_put_char,
543         .flush_chars = sclp_tty_flush_chars,
544         .write_room = sclp_tty_write_room,
545         .chars_in_buffer = sclp_tty_chars_in_buffer,
546         .flush_buffer = sclp_tty_flush_buffer,
547 };
548
549 static int __init
550 sclp_tty_init(void)
551 {
552         struct tty_driver *driver;
553         void *page;
554         int i;
555         int rc;
556
557         if (!CONSOLE_IS_SCLP)
558                 return 0;
559         driver = alloc_tty_driver(1);
560         if (!driver)
561                 return -ENOMEM;
562
563         rc = sclp_rw_init();
564         if (rc) {
565                 put_tty_driver(driver);
566                 return rc;
567         }
568         /* Allocate pages for output buffering */
569         INIT_LIST_HEAD(&sclp_tty_pages);
570         for (i = 0; i < MAX_KMEM_PAGES; i++) {
571                 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
572                 if (page == NULL) {
573                         put_tty_driver(driver);
574                         return -ENOMEM;
575                 }
576                 list_add_tail((struct list_head *) page, &sclp_tty_pages);
577         }
578         INIT_LIST_HEAD(&sclp_tty_outqueue);
579         spin_lock_init(&sclp_tty_lock);
580         init_waitqueue_head(&sclp_tty_waitq);
581         init_timer(&sclp_tty_timer);
582         sclp_ttybuf = NULL;
583         sclp_tty_buffer_count = 0;
584         if (MACHINE_IS_VM) {
585                 /*
586                  * save 4 characters for the CPU number
587                  * written at start of each line by VM/CP
588                  */
589                 sclp_tty_columns = 76;
590                 /* case input lines to lowercase */
591                 sclp_tty_tolower = 1;
592         }
593         sclp_tty_chars_count = 0;
594         sclp_tty = NULL;
595
596         rc = sclp_register(&sclp_input_event);
597         if (rc) {
598                 put_tty_driver(driver);
599                 return rc;
600         }
601
602         driver->owner = THIS_MODULE;
603         driver->driver_name = "sclp_line";
604         driver->name = "sclp_line";
605         driver->major = TTY_MAJOR;
606         driver->minor_start = 64;
607         driver->type = TTY_DRIVER_TYPE_SYSTEM;
608         driver->subtype = SYSTEM_TYPE_TTY;
609         driver->init_termios = tty_std_termios;
610         driver->init_termios.c_iflag = IGNBRK | IGNPAR;
611         driver->init_termios.c_oflag = ONLCR | XTABS;
612         driver->init_termios.c_lflag = ISIG | ECHO;
613         driver->flags = TTY_DRIVER_REAL_RAW;
614         tty_set_operations(driver, &sclp_ops);
615         rc = tty_register_driver(driver);
616         if (rc) {
617                 put_tty_driver(driver);
618                 return rc;
619         }
620         sclp_tty_driver = driver;
621         return 0;
622 }
623 module_init(sclp_tty_init);