e66a75b3822cbf24f09c0824c87b7f0c81089749
[pandora-kernel.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/err.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/gfp.h>
20 #include <asm/uaccess.h>
21
22 #include "ctrlchar.h"
23 #include "sclp.h"
24 #include "sclp_rw.h"
25 #include "sclp_tty.h"
26
27 /*
28  * size of a buffer that collects single characters coming in
29  * via sclp_tty_put_char()
30  */
31 #define SCLP_TTY_BUF_SIZE 512
32
33 /*
34  * There is exactly one SCLP terminal, so we can keep things simple
35  * and allocate all variables statically.
36  */
37
38 /* Lock to guard over changes to global variables. */
39 static spinlock_t sclp_tty_lock;
40 /* List of free pages that can be used for console output buffering. */
41 static struct list_head sclp_tty_pages;
42 /* List of full struct sclp_buffer structures ready for output. */
43 static struct list_head sclp_tty_outqueue;
44 /* Counter how many buffers are emitted. */
45 static int sclp_tty_buffer_count;
46 /* Pointer to current console buffer. */
47 static struct sclp_buffer *sclp_ttybuf;
48 /* Timer for delayed output of console messages. */
49 static struct timer_list sclp_tty_timer;
50
51 static struct tty_port sclp_port;
52 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
53 static unsigned short int sclp_tty_chars_count;
54
55 struct tty_driver *sclp_tty_driver;
56
57 static int sclp_tty_tolower;
58 static int sclp_tty_columns = 80;
59
60 #define SPACES_PER_TAB 8
61 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
62
63 /* This routine is called whenever we try to open a SCLP terminal. */
64 static int
65 sclp_tty_open(struct tty_struct *tty, struct file *filp)
66 {
67         tty_port_tty_set(&sclp_port, tty);
68         tty->driver_data = NULL;
69         tty->low_latency = 0;
70         return 0;
71 }
72
73 /* This routine is called when the SCLP terminal is closed. */
74 static void
75 sclp_tty_close(struct tty_struct *tty, struct file *filp)
76 {
77         if (tty->count > 1)
78                 return;
79         tty_port_tty_set(&sclp_port, NULL);
80 }
81
82 /*
83  * This routine returns the numbers of characters the tty driver
84  * will accept for queuing to be written.  This number is subject
85  * to change as output buffers get emptied, or if the output flow
86  * control is acted. This is not an exact number because not every
87  * character needs the same space in the sccb. The worst case is
88  * a string of newlines. Every newlines creates a new mto which
89  * needs 8 bytes.
90  */
91 static int
92 sclp_tty_write_room (struct tty_struct *tty)
93 {
94         unsigned long flags;
95         struct list_head *l;
96         int count;
97
98         spin_lock_irqsave(&sclp_tty_lock, flags);
99         count = 0;
100         if (sclp_ttybuf != NULL)
101                 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
102         list_for_each(l, &sclp_tty_pages)
103                 count += NR_EMPTY_MTO_PER_SCCB;
104         spin_unlock_irqrestore(&sclp_tty_lock, flags);
105         return count;
106 }
107
108 static void
109 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
110 {
111         struct tty_struct *tty;
112         unsigned long flags;
113         void *page;
114
115         do {
116                 page = sclp_unmake_buffer(buffer);
117                 spin_lock_irqsave(&sclp_tty_lock, flags);
118                 /* Remove buffer from outqueue */
119                 list_del(&buffer->list);
120                 sclp_tty_buffer_count--;
121                 list_add_tail((struct list_head *) page, &sclp_tty_pages);
122                 /* Check if there is a pending buffer on the out queue. */
123                 buffer = NULL;
124                 if (!list_empty(&sclp_tty_outqueue))
125                         buffer = list_entry(sclp_tty_outqueue.next,
126                                             struct sclp_buffer, list);
127                 spin_unlock_irqrestore(&sclp_tty_lock, flags);
128         } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
129         /* check if the tty needs a wake up call */
130         tty = tty_port_tty_get(&sclp_port);
131         if (tty != NULL) {
132                 tty_wakeup(tty);
133                 tty_kref_put(tty);
134         }
135 }
136
137 static inline void
138 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
139 {
140         unsigned long flags;
141         int count;
142         int rc;
143
144         spin_lock_irqsave(&sclp_tty_lock, flags);
145         list_add_tail(&buffer->list, &sclp_tty_outqueue);
146         count = sclp_tty_buffer_count++;
147         spin_unlock_irqrestore(&sclp_tty_lock, flags);
148         if (count)
149                 return;
150         rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
151         if (rc)
152                 sclp_ttybuf_callback(buffer, rc);
153 }
154
155 /*
156  * When this routine is called from the timer then we flush the
157  * temporary write buffer.
158  */
159 static void
160 sclp_tty_timeout(unsigned long data)
161 {
162         unsigned long flags;
163         struct sclp_buffer *buf;
164
165         spin_lock_irqsave(&sclp_tty_lock, flags);
166         buf = sclp_ttybuf;
167         sclp_ttybuf = NULL;
168         spin_unlock_irqrestore(&sclp_tty_lock, flags);
169
170         if (buf != NULL) {
171                 __sclp_ttybuf_emit(buf);
172         }
173 }
174
175 /*
176  * Write a string to the sclp tty.
177  */
178 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
179 {
180         unsigned long flags;
181         void *page;
182         int written;
183         int overall_written;
184         struct sclp_buffer *buf;
185
186         if (count <= 0)
187                 return 0;
188         overall_written = 0;
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 (may_fail)
196                                         goto out;
197                                 else
198                                         sclp_sync_wait();
199                                 spin_lock_irqsave(&sclp_tty_lock, flags);
200                         }
201                         page = sclp_tty_pages.next;
202                         list_del((struct list_head *) page);
203                         sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
204                                                        SPACES_PER_TAB);
205                 }
206                 /* try to write the string to the current output buffer */
207                 written = sclp_write(sclp_ttybuf, str, count);
208                 overall_written += written;
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 out:
235         return overall_written;
236 }
237
238 /*
239  * This routine is called by the kernel to write a series of characters to the
240  * tty device. The characters may come from user space or kernel space. This
241  * routine will return the number of characters actually accepted for writing.
242  */
243 static int
244 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
245 {
246         if (sclp_tty_chars_count > 0) {
247                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
248                 sclp_tty_chars_count = 0;
249         }
250         return sclp_tty_write_string(buf, count, 1);
251 }
252
253 /*
254  * This routine is called by the kernel to write a single character to the tty
255  * device. If the kernel uses this routine, it must call the flush_chars()
256  * routine (if defined) when it is done stuffing characters into the driver.
257  *
258  * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
259  * If the given character is a '\n' the contents of the SCLP write buffer
260  * - including previous characters from sclp_tty_put_char() and strings from
261  * sclp_write() without final '\n' - will be written.
262  */
263 static int
264 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
265 {
266         sclp_tty_chars[sclp_tty_chars_count++] = ch;
267         if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
268                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
269                 sclp_tty_chars_count = 0;
270         }
271         return 1;
272 }
273
274 /*
275  * This routine is called by the kernel after it has written a series of
276  * characters to the tty device using put_char().
277  */
278 static void
279 sclp_tty_flush_chars(struct tty_struct *tty)
280 {
281         if (sclp_tty_chars_count > 0) {
282                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
283                 sclp_tty_chars_count = 0;
284         }
285 }
286
287 /*
288  * This routine returns the number of characters in the write buffer of the
289  * SCLP driver. The provided number includes all characters that are stored
290  * in the SCCB (will be written next time the SCLP is not busy) as well as
291  * characters in the write buffer (will not be written as long as there is a
292  * final line feed missing).
293  */
294 static int
295 sclp_tty_chars_in_buffer(struct tty_struct *tty)
296 {
297         unsigned long flags;
298         struct list_head *l;
299         struct sclp_buffer *t;
300         int count;
301
302         spin_lock_irqsave(&sclp_tty_lock, flags);
303         count = 0;
304         if (sclp_ttybuf != NULL)
305                 count = sclp_chars_in_buffer(sclp_ttybuf);
306         list_for_each(l, &sclp_tty_outqueue) {
307                 t = list_entry(l, struct sclp_buffer, list);
308                 count += sclp_chars_in_buffer(t);
309         }
310         spin_unlock_irqrestore(&sclp_tty_lock, flags);
311         return count;
312 }
313
314 /*
315  * removes all content from buffers of low level driver
316  */
317 static void
318 sclp_tty_flush_buffer(struct tty_struct *tty)
319 {
320         if (sclp_tty_chars_count > 0) {
321                 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
322                 sclp_tty_chars_count = 0;
323         }
324 }
325
326 /*
327  * push input to tty
328  */
329 static void
330 sclp_tty_input(unsigned char* buf, unsigned int count)
331 {
332         struct tty_struct *tty = tty_port_tty_get(&sclp_port);
333         unsigned int cchar;
334
335         /*
336          * If this tty driver is currently closed
337          * then throw the received input away.
338          */
339         if (tty == NULL)
340                 return;
341         cchar = ctrlchar_handle(buf, count, tty);
342         switch (cchar & CTRLCHAR_MASK) {
343         case CTRLCHAR_SYSRQ:
344                 break;
345         case CTRLCHAR_CTRL:
346                 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
347                 tty_flip_buffer_push(tty);
348                 break;
349         case CTRLCHAR_NONE:
350                 /* send (normal) input to line discipline */
351                 if (count < 2 ||
352                     (strncmp((const char *) buf + count - 2, "^n", 2) &&
353                      strncmp((const char *) buf + count - 2, "\252n", 2))) {
354                         /* add the auto \n */
355                         tty_insert_flip_string(tty, buf, count);
356                         tty_insert_flip_char(tty, '\n', TTY_NORMAL);
357                 } else
358                         tty_insert_flip_string(tty, buf, count - 2);
359                 tty_flip_buffer_push(tty);
360                 break;
361         }
362         tty_kref_put(tty);
363 }
364
365 /*
366  * get a EBCDIC string in upper/lower case,
367  * find out characters in lower/upper case separated by a special character,
368  * modifiy original string,
369  * returns length of resulting string
370  */
371 static int sclp_switch_cases(unsigned char *buf, int count)
372 {
373         unsigned char *ip, *op;
374         int toggle;
375
376         /* initially changing case is off */
377         toggle = 0;
378         ip = op = buf;
379         while (count-- > 0) {
380                 /* compare with special character */
381                 if (*ip == CASE_DELIMITER) {
382                         /* followed by another special character? */
383                         if (count && ip[1] == CASE_DELIMITER) {
384                                 /*
385                                  * ... then put a single copy of the special
386                                  * character to the output string
387                                  */
388                                 *op++ = *ip++;
389                                 count--;
390                         } else
391                                 /*
392                                  * ... special character follower by a normal
393                                  * character toggles the case change behaviour
394                                  */
395                                 toggle = ~toggle;
396                         /* skip special character */
397                         ip++;
398                 } else
399                         /* not the special character */
400                         if (toggle)
401                                 /* but case switching is on */
402                                 if (sclp_tty_tolower)
403                                         /* switch to uppercase */
404                                         *op++ = _ebc_toupper[(int) *ip++];
405                                 else
406                                         /* switch to lowercase */
407                                         *op++ = _ebc_tolower[(int) *ip++];
408                         else
409                                 /* no case switching, copy the character */
410                                 *op++ = *ip++;
411         }
412         /* return length of reformatted string. */
413         return op - buf;
414 }
415
416 static void sclp_get_input(struct gds_subvector *sv)
417 {
418         unsigned char *str;
419         int count;
420
421         str = (unsigned char *) (sv + 1);
422         count = sv->length - sizeof(*sv);
423         if (sclp_tty_tolower)
424                 EBC_TOLOWER(str, count);
425         count = sclp_switch_cases(str, count);
426         /* convert EBCDIC to ASCII (modify original input in SCCB) */
427         sclp_ebcasc_str(str, count);
428
429         /* transfer input to high level driver */
430         sclp_tty_input(str, count);
431 }
432
433 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
434 {
435         void *end;
436
437         end = (void *) sv + sv->length;
438         for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
439                 if (sv->key == 0x30)
440                         sclp_get_input(sv);
441 }
442
443 static inline void sclp_eval_textcmd(struct gds_vector *v)
444 {
445         struct gds_subvector *sv;
446         void *end;
447
448         end = (void *) v + v->length;
449         for (sv = (struct gds_subvector *) (v + 1);
450              (void *) sv < end; sv = (void *) sv + sv->length)
451                 if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
452                         sclp_eval_selfdeftextmsg(sv);
453
454 }
455
456 static inline void sclp_eval_cpmsu(struct gds_vector *v)
457 {
458         void *end;
459
460         end = (void *) v + v->length;
461         for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
462                 if (v->gds_id == GDS_ID_TEXTCMD)
463                         sclp_eval_textcmd(v);
464 }
465
466
467 static inline void sclp_eval_mdsmu(struct gds_vector *v)
468 {
469         v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
470         if (v)
471                 sclp_eval_cpmsu(v);
472 }
473
474 static void sclp_tty_receiver(struct evbuf_header *evbuf)
475 {
476         struct gds_vector *v;
477
478         v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
479                                  GDS_ID_MDSMU);
480         if (v)
481                 sclp_eval_mdsmu(v);
482 }
483
484 static void
485 sclp_tty_state_change(struct sclp_register *reg)
486 {
487 }
488
489 static struct sclp_register sclp_input_event =
490 {
491         .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
492         .state_change_fn = sclp_tty_state_change,
493         .receiver_fn = sclp_tty_receiver
494 };
495
496 static const struct tty_operations sclp_ops = {
497         .open = sclp_tty_open,
498         .close = sclp_tty_close,
499         .write = sclp_tty_write,
500         .put_char = sclp_tty_put_char,
501         .flush_chars = sclp_tty_flush_chars,
502         .write_room = sclp_tty_write_room,
503         .chars_in_buffer = sclp_tty_chars_in_buffer,
504         .flush_buffer = sclp_tty_flush_buffer,
505 };
506
507 static int __init
508 sclp_tty_init(void)
509 {
510         struct tty_driver *driver;
511         void *page;
512         int i;
513         int rc;
514
515         if (!CONSOLE_IS_SCLP)
516                 return 0;
517         driver = alloc_tty_driver(1);
518         if (!driver)
519                 return -ENOMEM;
520
521         rc = sclp_rw_init();
522         if (rc) {
523                 put_tty_driver(driver);
524                 return rc;
525         }
526         /* Allocate pages for output buffering */
527         INIT_LIST_HEAD(&sclp_tty_pages);
528         for (i = 0; i < MAX_KMEM_PAGES; i++) {
529                 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
530                 if (page == NULL) {
531                         put_tty_driver(driver);
532                         return -ENOMEM;
533                 }
534                 list_add_tail((struct list_head *) page, &sclp_tty_pages);
535         }
536         INIT_LIST_HEAD(&sclp_tty_outqueue);
537         spin_lock_init(&sclp_tty_lock);
538         init_timer(&sclp_tty_timer);
539         sclp_ttybuf = NULL;
540         sclp_tty_buffer_count = 0;
541         if (MACHINE_IS_VM) {
542                 /*
543                  * save 4 characters for the CPU number
544                  * written at start of each line by VM/CP
545                  */
546                 sclp_tty_columns = 76;
547                 /* case input lines to lowercase */
548                 sclp_tty_tolower = 1;
549         }
550         sclp_tty_chars_count = 0;
551         tty_port_init(&sclp_port);
552
553         rc = sclp_register(&sclp_input_event);
554         if (rc) {
555                 put_tty_driver(driver);
556                 return rc;
557         }
558
559         driver->driver_name = "sclp_line";
560         driver->name = "sclp_line";
561         driver->major = TTY_MAJOR;
562         driver->minor_start = 64;
563         driver->type = TTY_DRIVER_TYPE_SYSTEM;
564         driver->subtype = SYSTEM_TYPE_TTY;
565         driver->init_termios = tty_std_termios;
566         driver->init_termios.c_iflag = IGNBRK | IGNPAR;
567         driver->init_termios.c_oflag = ONLCR | XTABS;
568         driver->init_termios.c_lflag = ISIG | ECHO;
569         driver->flags = TTY_DRIVER_REAL_RAW;
570         tty_set_operations(driver, &sclp_ops);
571         rc = tty_register_driver(driver);
572         if (rc) {
573                 put_tty_driver(driver);
574                 return rc;
575         }
576         sclp_tty_driver = driver;
577         return 0;
578 }
579 module_init(sclp_tty_init);