Merge branch 'timers/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / s390 / char / con3215.c
1 /*
2  *  drivers/s390/char/con3215.c
3  *    3215 line mode terminal driver.
4  *
5  *  S390 version
6  *    Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
8  *
9  *  Updated:
10  *   Aug-2000: Added tab support
11  *             Dan Morrison, IBM Corporation (dmorriso@cse.buffalo.edu)
12  */
13
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kdev_t.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/vt_kern.h>
20 #include <linux/init.h>
21 #include <linux/console.h>
22 #include <linux/interrupt.h>
23 #include <linux/err.h>
24
25 #include <linux/slab.h>
26 #include <linux/bootmem.h>
27
28 #include <asm/ccwdev.h>
29 #include <asm/cio.h>
30 #include <asm/io.h>
31 #include <asm/ebcdic.h>
32 #include <asm/uaccess.h>
33 #include <asm/delay.h>
34 #include <asm/cpcmd.h>
35 #include <asm/setup.h>
36
37 #include "ctrlchar.h"
38
39 #define NR_3215             1
40 #define NR_3215_REQ         (4*NR_3215)
41 #define RAW3215_BUFFER_SIZE 65536     /* output buffer size */
42 #define RAW3215_INBUF_SIZE  256       /* input buffer size */
43 #define RAW3215_MIN_SPACE   128       /* minimum free space for wakeup */
44 #define RAW3215_MIN_WRITE   1024      /* min. length for immediate output */
45 #define RAW3215_MAX_BYTES   3968      /* max. bytes to write with one ssch */
46 #define RAW3215_MAX_NEWLINE 50        /* max. lines to write with one ssch */
47 #define RAW3215_NR_CCWS     3
48 #define RAW3215_TIMEOUT     HZ/10     /* time for delayed output */
49
50 #define RAW3215_FIXED       1         /* 3215 console device is not be freed */
51 #define RAW3215_ACTIVE      2         /* set if the device is in use */
52 #define RAW3215_WORKING     4         /* set if a request is being worked on */
53 #define RAW3215_THROTTLED   8         /* set if reading is disabled */
54 #define RAW3215_STOPPED     16        /* set if writing is disabled */
55 #define RAW3215_CLOSING     32        /* set while in close process */
56 #define RAW3215_TIMER_RUNS  64        /* set if the output delay timer is on */
57 #define RAW3215_FLUSHING    128       /* set to flush buffer (no delay) */
58
59 #define TAB_STOP_SIZE       8         /* tab stop size */
60
61 /*
62  * Request types for a 3215 device
63  */
64 enum raw3215_type {
65         RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
66 };
67
68 /*
69  * Request structure for a 3215 device
70  */
71 struct raw3215_req {
72         enum raw3215_type type;       /* type of the request */
73         int start, len;               /* start index & len in output buffer */
74         int delayable;                /* indication to wait for more data */
75         int residual;                 /* residual count for read request */
76         struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
77         struct raw3215_info *info;    /* pointer to main structure */
78         struct raw3215_req *next;     /* pointer to next request */
79 } __attribute__ ((aligned(8)));
80
81 struct raw3215_info {
82         struct ccw_device *cdev;      /* device for tty driver */
83         spinlock_t *lock;             /* pointer to irq lock */
84         int flags;                    /* state flags */
85         char *buffer;                 /* pointer to output buffer */
86         char *inbuf;                  /* pointer to input buffer */
87         int head;                     /* first free byte in output buffer */
88         int count;                    /* number of bytes in output buffer */
89         int written;                  /* number of bytes in write requests */
90         struct tty_struct *tty;       /* pointer to tty structure if present */
91         struct tasklet_struct tasklet;
92         struct raw3215_req *queued_read; /* pointer to queued read requests */
93         struct raw3215_req *queued_write;/* pointer to queued write requests */
94         wait_queue_head_t empty_wait; /* wait queue for flushing */
95         struct timer_list timer;      /* timer for delayed output */
96         int line_pos;                 /* position on the line (for tabs) */
97         char ubuffer[80];             /* copy_from_user buffer */
98 };
99
100 /* array of 3215 devices structures */
101 static struct raw3215_info *raw3215[NR_3215];
102 /* spinlock to protect the raw3215 array */
103 static DEFINE_SPINLOCK(raw3215_device_lock);
104 /* list of free request structures */
105 static struct raw3215_req *raw3215_freelist;
106 /* spinlock to protect free list */
107 static spinlock_t raw3215_freelist_lock;
108
109 static struct tty_driver *tty3215_driver;
110
111 /*
112  * Get a request structure from the free list
113  */
114 static inline struct raw3215_req *
115 raw3215_alloc_req(void) {
116         struct raw3215_req *req;
117         unsigned long flags;
118
119         spin_lock_irqsave(&raw3215_freelist_lock, flags);
120         req = raw3215_freelist;
121         raw3215_freelist = req->next;
122         spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
123         return req;
124 }
125
126 /*
127  * Put a request structure back to the free list
128  */
129 static inline void
130 raw3215_free_req(struct raw3215_req *req) {
131         unsigned long flags;
132
133         if (req->type == RAW3215_FREE)
134                 return;         /* don't free a free request */
135         req->type = RAW3215_FREE;
136         spin_lock_irqsave(&raw3215_freelist_lock, flags);
137         req->next = raw3215_freelist;
138         raw3215_freelist = req;
139         spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
140 }
141
142 /*
143  * Set up a read request that reads up to 160 byte from the 3215 device.
144  * If there is a queued read request it is used, but that shouldn't happen
145  * because a 3215 terminal won't accept a new read before the old one is
146  * completed.
147  */
148 static void
149 raw3215_mk_read_req(struct raw3215_info *raw)
150 {
151         struct raw3215_req *req;
152         struct ccw1 *ccw;
153
154         /* there can only be ONE read request at a time */
155         req = raw->queued_read;
156         if (req == NULL) {
157                 /* no queued read request, use new req structure */
158                 req = raw3215_alloc_req();
159                 req->type = RAW3215_READ;
160                 req->info = raw;
161                 raw->queued_read = req;
162         }
163
164         ccw = req->ccws;
165         ccw->cmd_code = 0x0A; /* read inquiry */
166         ccw->flags = 0x20;    /* ignore incorrect length */
167         ccw->count = 160;
168         ccw->cda = (__u32) __pa(raw->inbuf);
169 }
170
171 /*
172  * Set up a write request with the information from the main structure.
173  * A ccw chain is created that writes as much as possible from the output
174  * buffer to the 3215 device. If a queued write exists it is replaced by
175  * the new, probably lengthened request.
176  */
177 static void
178 raw3215_mk_write_req(struct raw3215_info *raw)
179 {
180         struct raw3215_req *req;
181         struct ccw1 *ccw;
182         int len, count, ix, lines;
183
184         if (raw->count <= raw->written)
185                 return;
186         /* check if there is a queued write request */
187         req = raw->queued_write;
188         if (req == NULL) {
189                 /* no queued write request, use new req structure */
190                 req = raw3215_alloc_req();
191                 req->type = RAW3215_WRITE;
192                 req->info = raw;
193                 raw->queued_write = req;
194         } else {
195                 raw->written -= req->len;
196         }
197
198         ccw = req->ccws;
199         req->start = (raw->head - raw->count + raw->written) &
200                      (RAW3215_BUFFER_SIZE - 1);
201         /*
202          * now we have to count newlines. We can at max accept
203          * RAW3215_MAX_NEWLINE newlines in a single ssch due to
204          * a restriction in VM
205          */
206         lines = 0;
207         ix = req->start;
208         while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
209                 if (raw->buffer[ix] == 0x15)
210                         lines++;
211                 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
212         }
213         len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
214         if (len > RAW3215_MAX_BYTES)
215                 len = RAW3215_MAX_BYTES;
216         req->len = len;
217         raw->written += len;
218
219         /* set the indication if we should try to enlarge this request */
220         req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
221
222         ix = req->start;
223         while (len > 0) {
224                 if (ccw > req->ccws)
225                         ccw[-1].flags |= 0x40; /* use command chaining */
226                 ccw->cmd_code = 0x01; /* write, auto carrier return */
227                 ccw->flags = 0x20;    /* ignore incorrect length ind.  */
228                 ccw->cda =
229                         (__u32) __pa(raw->buffer + ix);
230                 count = len;
231                 if (ix + count > RAW3215_BUFFER_SIZE)
232                         count = RAW3215_BUFFER_SIZE - ix;
233                 ccw->count = count;
234                 len -= count;
235                 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
236                 ccw++;
237         }
238         /*
239          * Add a NOP to the channel program. 3215 devices are purely
240          * emulated and its much better to avoid the channel end
241          * interrupt in this case.
242          */
243         if (ccw > req->ccws)
244                 ccw[-1].flags |= 0x40; /* use command chaining */
245         ccw->cmd_code = 0x03; /* NOP */
246         ccw->flags = 0;
247         ccw->cda = 0;
248         ccw->count = 1;
249 }
250
251 /*
252  * Start a read or a write request
253  */
254 static void
255 raw3215_start_io(struct raw3215_info *raw)
256 {
257         struct raw3215_req *req;
258         int res;
259
260         req = raw->queued_read;
261         if (req != NULL &&
262             !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
263                 /* dequeue request */
264                 raw->queued_read = NULL;
265                 res = ccw_device_start(raw->cdev, req->ccws,
266                                        (unsigned long) req, 0, 0);
267                 if (res != 0) {
268                         /* do_IO failed, put request back to queue */
269                         raw->queued_read = req;
270                 } else {
271                         raw->flags |= RAW3215_WORKING;
272                 }
273         }
274         req = raw->queued_write;
275         if (req != NULL &&
276             !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
277                 /* dequeue request */
278                 raw->queued_write = NULL;
279                 res = ccw_device_start(raw->cdev, req->ccws,
280                                        (unsigned long) req, 0, 0);
281                 if (res != 0) {
282                         /* do_IO failed, put request back to queue */
283                         raw->queued_write = req;
284                 } else {
285                         raw->flags |= RAW3215_WORKING;
286                 }
287         }
288 }
289
290 /*
291  * Function to start a delayed output after RAW3215_TIMEOUT seconds
292  */
293 static void
294 raw3215_timeout(unsigned long __data)
295 {
296         struct raw3215_info *raw = (struct raw3215_info *) __data;
297         unsigned long flags;
298
299         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
300         if (raw->flags & RAW3215_TIMER_RUNS) {
301                 del_timer(&raw->timer);
302                 raw->flags &= ~RAW3215_TIMER_RUNS;
303                 raw3215_mk_write_req(raw);
304                 raw3215_start_io(raw);
305         }
306         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
307 }
308
309 /*
310  * Function to conditionally start an IO. A read is started immediately,
311  * a write is only started immediately if the flush flag is on or the
312  * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
313  * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
314  */
315 static inline void
316 raw3215_try_io(struct raw3215_info *raw)
317 {
318         if (!(raw->flags & RAW3215_ACTIVE))
319                 return;
320         if (raw->queued_read != NULL)
321                 raw3215_start_io(raw);
322         else if (raw->queued_write != NULL) {
323                 if ((raw->queued_write->delayable == 0) ||
324                     (raw->flags & RAW3215_FLUSHING)) {
325                         /* execute write requests bigger than minimum size */
326                         raw3215_start_io(raw);
327                         if (raw->flags & RAW3215_TIMER_RUNS) {
328                                 del_timer(&raw->timer);
329                                 raw->flags &= ~RAW3215_TIMER_RUNS;
330                         }
331                 } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
332                         /* delay small writes */
333                         init_timer(&raw->timer);
334                         raw->timer.expires = RAW3215_TIMEOUT + jiffies;
335                         raw->timer.data = (unsigned long) raw;
336                         raw->timer.function = raw3215_timeout;
337                         add_timer(&raw->timer);
338                         raw->flags |= RAW3215_TIMER_RUNS;
339                 }
340         }
341 }
342
343 /*
344  * The bottom half handler routine for 3215 devices. It tries to start
345  * the next IO and wakes up processes waiting on the tty.
346  */
347 static void
348 raw3215_tasklet(void *data)
349 {
350         struct raw3215_info *raw;
351         struct tty_struct *tty;
352         unsigned long flags;
353
354         raw = (struct raw3215_info *) data;
355         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
356         raw3215_mk_write_req(raw);
357         raw3215_try_io(raw);
358         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
359         tty = raw->tty;
360         if (tty != NULL &&
361             RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
362                 tty_wakeup(tty);
363         }
364 }
365
366 /*
367  * Interrupt routine, called from common io layer
368  */
369 static void
370 raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
371 {
372         struct raw3215_info *raw;
373         struct raw3215_req *req;
374         struct tty_struct *tty;
375         int cstat, dstat;
376         int count;
377
378         raw = cdev->dev.driver_data;
379         req = (struct raw3215_req *) intparm;
380         cstat = irb->scsw.cmd.cstat;
381         dstat = irb->scsw.cmd.dstat;
382         if (cstat != 0)
383                 tasklet_schedule(&raw->tasklet);
384         if (dstat & 0x01) { /* we got a unit exception */
385                 dstat &= ~0x01;  /* we can ignore it */
386         }
387         switch (dstat) {
388         case 0x80:
389                 if (cstat != 0)
390                         break;
391                 /* Attention interrupt, someone hit the enter key */
392                 raw3215_mk_read_req(raw);
393                 tasklet_schedule(&raw->tasklet);
394                 break;
395         case 0x08:
396         case 0x0C:
397                 /* Channel end interrupt. */
398                 if ((raw = req->info) == NULL)
399                         return;              /* That shouldn't happen ... */
400                 if (req->type == RAW3215_READ) {
401                         /* store residual count, then wait for device end */
402                         req->residual = irb->scsw.cmd.count;
403                 }
404                 if (dstat == 0x08)
405                         break;
406         case 0x04:
407                 /* Device end interrupt. */
408                 if ((raw = req->info) == NULL)
409                         return;              /* That shouldn't happen ... */
410                 if (req->type == RAW3215_READ && raw->tty != NULL) {
411                         unsigned int cchar;
412
413                         tty = raw->tty;
414                         count = 160 - req->residual;
415                         EBCASC(raw->inbuf, count);
416                         cchar = ctrlchar_handle(raw->inbuf, count, tty);
417                         switch (cchar & CTRLCHAR_MASK) {
418                         case CTRLCHAR_SYSRQ:
419                                 break;
420
421                         case CTRLCHAR_CTRL:
422                                 tty_insert_flip_char(tty, cchar, TTY_NORMAL);
423                                 tty_flip_buffer_push(raw->tty);
424                                 break;
425
426                         case CTRLCHAR_NONE:
427                                 if (count < 2 ||
428                                     (strncmp(raw->inbuf+count-2, "\252n", 2) &&
429                                      strncmp(raw->inbuf+count-2, "^n", 2)) ) {
430                                         /* add the auto \n */
431                                         raw->inbuf[count] = '\n';
432                                         count++;
433                                 } else
434                                         count -= 2;
435                                 tty_insert_flip_string(tty, raw->inbuf, count);
436                                 tty_flip_buffer_push(raw->tty);
437                                 break;
438                         }
439                 } else if (req->type == RAW3215_WRITE) {
440                         raw->count -= req->len;
441                         raw->written -= req->len;
442                 }
443                 raw->flags &= ~RAW3215_WORKING;
444                 raw3215_free_req(req);
445                 /* check for empty wait */
446                 if (waitqueue_active(&raw->empty_wait) &&
447                     raw->queued_write == NULL &&
448                     raw->queued_read == NULL) {
449                         wake_up_interruptible(&raw->empty_wait);
450                 }
451                 tasklet_schedule(&raw->tasklet);
452                 break;
453         default:
454                 /* Strange interrupt, I'll do my best to clean up */
455                 if (req != NULL && req->type != RAW3215_FREE) {
456                         if (req->type == RAW3215_WRITE) {
457                                 raw->count -= req->len;
458                                 raw->written -= req->len;
459                         }
460                         raw->flags &= ~RAW3215_WORKING;
461                         raw3215_free_req(req);
462                 }
463                 tasklet_schedule(&raw->tasklet);
464         }
465         return;
466 }
467
468 /*
469  * Wait until length bytes are available int the output buffer.
470  * Has to be called with the s390irq lock held. Can be called
471  * disabled.
472  */
473 static void
474 raw3215_make_room(struct raw3215_info *raw, unsigned int length)
475 {
476         while (RAW3215_BUFFER_SIZE - raw->count < length) {
477                 /* there might be a request pending */
478                 raw->flags |= RAW3215_FLUSHING;
479                 raw3215_mk_write_req(raw);
480                 raw3215_try_io(raw);
481                 raw->flags &= ~RAW3215_FLUSHING;
482 #ifdef CONFIG_TN3215_CONSOLE
483                 wait_cons_dev();
484 #endif
485                 /* Enough room freed up ? */
486                 if (RAW3215_BUFFER_SIZE - raw->count >= length)
487                         break;
488                 /* there might be another cpu waiting for the lock */
489                 spin_unlock(get_ccwdev_lock(raw->cdev));
490                 udelay(100);
491                 spin_lock(get_ccwdev_lock(raw->cdev));
492         }
493 }
494
495 /*
496  * String write routine for 3215 devices
497  */
498 static void
499 raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
500 {
501         unsigned long flags;
502         int c, count;
503
504         while (length > 0) {
505                 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
506                 count = (length > RAW3215_BUFFER_SIZE) ?
507                                              RAW3215_BUFFER_SIZE : length;
508                 length -= count;
509
510                 raw3215_make_room(raw, count);
511
512                 /* copy string to output buffer and convert it to EBCDIC */
513                 while (1) {
514                         c = min_t(int, count,
515                                   min(RAW3215_BUFFER_SIZE - raw->count,
516                                       RAW3215_BUFFER_SIZE - raw->head));
517                         if (c <= 0)
518                                 break;
519                         memcpy(raw->buffer + raw->head, str, c);
520                         ASCEBC(raw->buffer + raw->head, c);
521                         raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
522                         raw->count += c;
523                         raw->line_pos += c;
524                         str += c;
525                         count -= c;
526                 }
527                 if (!(raw->flags & RAW3215_WORKING)) {
528                         raw3215_mk_write_req(raw);
529                         /* start or queue request */
530                         raw3215_try_io(raw);
531                 }
532                 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
533         }
534 }
535
536 /*
537  * Put character routine for 3215 devices
538  */
539 static void
540 raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
541 {
542         unsigned long flags;
543         unsigned int length, i;
544
545         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
546         if (ch == '\t') {
547                 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
548                 raw->line_pos += length;
549                 ch = ' ';
550         } else if (ch == '\n') {
551                 length = 1;
552                 raw->line_pos = 0;
553         } else {
554                 length = 1;
555                 raw->line_pos++;
556         }
557         raw3215_make_room(raw, length);
558
559         for (i = 0; i < length; i++) {
560                 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
561                 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
562                 raw->count++;
563         }
564         if (!(raw->flags & RAW3215_WORKING)) {
565                 raw3215_mk_write_req(raw);
566                 /* start or queue request */
567                 raw3215_try_io(raw);
568         }
569         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
570 }
571
572 /*
573  * Flush routine, it simply sets the flush flag and tries to start
574  * pending IO.
575  */
576 static void
577 raw3215_flush_buffer(struct raw3215_info *raw)
578 {
579         unsigned long flags;
580
581         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
582         if (raw->count > 0) {
583                 raw->flags |= RAW3215_FLUSHING;
584                 raw3215_try_io(raw);
585                 raw->flags &= ~RAW3215_FLUSHING;
586         }
587         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
588 }
589
590 /*
591  * Fire up a 3215 device.
592  */
593 static int
594 raw3215_startup(struct raw3215_info *raw)
595 {
596         unsigned long flags;
597
598         if (raw->flags & RAW3215_ACTIVE)
599                 return 0;
600         raw->line_pos = 0;
601         raw->flags |= RAW3215_ACTIVE;
602         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
603         raw3215_try_io(raw);
604         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
605
606         return 0;
607 }
608
609 /*
610  * Shutdown a 3215 device.
611  */
612 static void
613 raw3215_shutdown(struct raw3215_info *raw)
614 {
615         DECLARE_WAITQUEUE(wait, current);
616         unsigned long flags;
617
618         if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
619                 return;
620         /* Wait for outstanding requests, then free irq */
621         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
622         if ((raw->flags & RAW3215_WORKING) ||
623             raw->queued_write != NULL ||
624             raw->queued_read != NULL) {
625                 raw->flags |= RAW3215_CLOSING;
626                 add_wait_queue(&raw->empty_wait, &wait);
627                 set_current_state(TASK_INTERRUPTIBLE);
628                 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
629                 schedule();
630                 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
631                 remove_wait_queue(&raw->empty_wait, &wait);
632                 set_current_state(TASK_RUNNING);
633                 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
634         }
635         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
636 }
637
638 static int
639 raw3215_probe (struct ccw_device *cdev)
640 {
641         struct raw3215_info *raw;
642         int line;
643
644         /* Console is special. */
645         if (raw3215[0] && (cdev->dev.driver_data == raw3215[0]))
646                 return 0;
647         raw = kmalloc(sizeof(struct raw3215_info) +
648                       RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
649         if (raw == NULL)
650                 return -ENOMEM;
651
652         spin_lock(&raw3215_device_lock);
653         for (line = 0; line < NR_3215; line++) {
654                 if (!raw3215[line]) {
655                         raw3215[line] = raw;
656                         break;
657                 }
658         }
659         spin_unlock(&raw3215_device_lock);
660         if (line == NR_3215) {
661                 kfree(raw);
662                 return -ENODEV;
663         }
664
665         raw->cdev = cdev;
666         raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
667         memset(raw, 0, sizeof(struct raw3215_info));
668         raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
669                                        GFP_KERNEL|GFP_DMA);
670         if (raw->buffer == NULL) {
671                 spin_lock(&raw3215_device_lock);
672                 raw3215[line] = NULL;
673                 spin_unlock(&raw3215_device_lock);
674                 kfree(raw);
675                 return -ENOMEM;
676         }
677         tasklet_init(&raw->tasklet,
678                      (void (*)(unsigned long)) raw3215_tasklet,
679                      (unsigned long) raw);
680         init_waitqueue_head(&raw->empty_wait);
681
682         cdev->dev.driver_data = raw;
683         cdev->handler = raw3215_irq;
684
685         return 0;
686 }
687
688 static void
689 raw3215_remove (struct ccw_device *cdev)
690 {
691         struct raw3215_info *raw;
692
693         ccw_device_set_offline(cdev);
694         raw = cdev->dev.driver_data;
695         if (raw) {
696                 cdev->dev.driver_data = NULL;
697                 kfree(raw->buffer);
698                 kfree(raw);
699         }
700 }
701
702 static int
703 raw3215_set_online (struct ccw_device *cdev)
704 {
705         struct raw3215_info *raw;
706
707         raw = cdev->dev.driver_data;
708         if (!raw)
709                 return -ENODEV;
710
711         return raw3215_startup(raw);
712 }
713
714 static int
715 raw3215_set_offline (struct ccw_device *cdev)
716 {
717         struct raw3215_info *raw;
718
719         raw = cdev->dev.driver_data;
720         if (!raw)
721                 return -ENODEV;
722
723         raw3215_shutdown(raw);
724
725         return 0;
726 }
727
728 static struct ccw_device_id raw3215_id[] = {
729         { CCW_DEVICE(0x3215, 0) },
730         { /* end of list */ },
731 };
732
733 static struct ccw_driver raw3215_ccw_driver = {
734         .name           = "3215",
735         .owner          = THIS_MODULE,
736         .ids            = raw3215_id,
737         .probe          = &raw3215_probe,
738         .remove         = &raw3215_remove,
739         .set_online     = &raw3215_set_online,
740         .set_offline    = &raw3215_set_offline,
741 };
742
743 #ifdef CONFIG_TN3215_CONSOLE
744 /*
745  * Write a string to the 3215 console
746  */
747 static void
748 con3215_write(struct console *co, const char *str, unsigned int count)
749 {
750         struct raw3215_info *raw;
751         int i;
752
753         if (count <= 0)
754                 return;
755         raw = raw3215[0];       /* console 3215 is the first one */
756         while (count > 0) {
757                 for (i = 0; i < count; i++)
758                         if (str[i] == '\t' || str[i] == '\n')
759                                 break;
760                 raw3215_write(raw, str, i);
761                 count -= i;
762                 str += i;
763                 if (count > 0) {
764                         raw3215_putchar(raw, *str);
765                         count--;
766                         str++;
767                 }
768         }
769 }
770
771 static struct tty_driver *con3215_device(struct console *c, int *index)
772 {
773         *index = c->index;
774         return tty3215_driver;
775 }
776
777 /*
778  * panic() calls console_unblank before the system enters a
779  * disabled, endless loop.
780  */
781 static void
782 con3215_unblank(void)
783 {
784         struct raw3215_info *raw;
785         unsigned long flags;
786
787         raw = raw3215[0];  /* console 3215 is the first one */
788         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
789         raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
790         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
791 }
792
793 /*
794  *  The console structure for the 3215 console
795  */
796 static struct console con3215 = {
797         .name    = "ttyS",
798         .write   = con3215_write,
799         .device  = con3215_device,
800         .unblank = con3215_unblank,
801         .flags   = CON_PRINTBUFFER,
802 };
803
804 /*
805  * 3215 console initialization code called from console_init().
806  * NOTE: This is called before kmalloc is available.
807  */
808 static int __init
809 con3215_init(void)
810 {
811         struct ccw_device *cdev;
812         struct raw3215_info *raw;
813         struct raw3215_req *req;
814         int i;
815
816         /* Check if 3215 is to be the console */
817         if (!CONSOLE_IS_3215)
818                 return -ENODEV;
819
820         /* Set the console mode for VM */
821         if (MACHINE_IS_VM) {
822                 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
823                 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
824         }
825
826         /* allocate 3215 request structures */
827         raw3215_freelist = NULL;
828         spin_lock_init(&raw3215_freelist_lock);
829         for (i = 0; i < NR_3215_REQ; i++) {
830                 req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
831                 req->next = raw3215_freelist;
832                 raw3215_freelist = req;
833         }
834
835         cdev = ccw_device_probe_console();
836         if (IS_ERR(cdev))
837                 return -ENODEV;
838
839         raw3215[0] = raw = (struct raw3215_info *)
840                 alloc_bootmem_low(sizeof(struct raw3215_info));
841         memset(raw, 0, sizeof(struct raw3215_info));
842         raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
843         raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
844         raw->cdev = cdev;
845         cdev->dev.driver_data = raw;
846         cdev->handler = raw3215_irq;
847
848         raw->flags |= RAW3215_FIXED;
849         tasklet_init(&raw->tasklet,
850                      (void (*)(unsigned long)) raw3215_tasklet,
851                      (unsigned long) raw);
852         init_waitqueue_head(&raw->empty_wait);
853
854         /* Request the console irq */
855         if (raw3215_startup(raw) != 0) {
856                 free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
857                 free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
858                 free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
859                 raw3215[0] = NULL;
860                 return -ENODEV;
861         }
862         register_console(&con3215);
863         return 0;
864 }
865 console_initcall(con3215_init);
866 #endif
867
868 /*
869  * tty3215_open
870  *
871  * This routine is called whenever a 3215 tty is opened.
872  */
873 static int
874 tty3215_open(struct tty_struct *tty, struct file * filp)
875 {
876         struct raw3215_info *raw;
877         int retval, line;
878
879         line = tty->index;
880         if ((line < 0) || (line >= NR_3215))
881                 return -ENODEV;
882
883         raw = raw3215[line];
884         if (raw == NULL)
885                 return -ENODEV;
886
887         tty->driver_data = raw;
888         raw->tty = tty;
889
890         tty->low_latency = 0;  /* don't use bottom half for pushing chars */
891         /*
892          * Start up 3215 device
893          */
894         retval = raw3215_startup(raw);
895         if (retval)
896                 return retval;
897
898         return 0;
899 }
900
901 /*
902  * tty3215_close()
903  *
904  * This routine is called when the 3215 tty is closed. We wait
905  * for the remaining request to be completed. Then we clean up.
906  */
907 static void
908 tty3215_close(struct tty_struct *tty, struct file * filp)
909 {
910         struct raw3215_info *raw;
911
912         raw = (struct raw3215_info *) tty->driver_data;
913         if (raw == NULL || tty->count > 1)
914                 return;
915         tty->closing = 1;
916         /* Shutdown the terminal */
917         raw3215_shutdown(raw);
918         tty->closing = 0;
919         raw->tty = NULL;
920 }
921
922 /*
923  * Returns the amount of free space in the output buffer.
924  */
925 static int
926 tty3215_write_room(struct tty_struct *tty)
927 {
928         struct raw3215_info *raw;
929
930         raw = (struct raw3215_info *) tty->driver_data;
931
932         /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
933         if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
934                 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
935         else
936                 return 0;
937 }
938
939 /*
940  * String write routine for 3215 ttys
941  */
942 static int
943 tty3215_write(struct tty_struct * tty,
944               const unsigned char *buf, int count)
945 {
946         struct raw3215_info *raw;
947
948         if (!tty)
949                 return 0;
950         raw = (struct raw3215_info *) tty->driver_data;
951         raw3215_write(raw, buf, count);
952         return count;
953 }
954
955 /*
956  * Put character routine for 3215 ttys
957  */
958 static int
959 tty3215_put_char(struct tty_struct *tty, unsigned char ch)
960 {
961         struct raw3215_info *raw;
962
963         if (!tty)
964                 return 0;
965         raw = (struct raw3215_info *) tty->driver_data;
966         raw3215_putchar(raw, ch);
967         return 1;
968 }
969
970 static void
971 tty3215_flush_chars(struct tty_struct *tty)
972 {
973 }
974
975 /*
976  * Returns the number of characters in the output buffer
977  */
978 static int
979 tty3215_chars_in_buffer(struct tty_struct *tty)
980 {
981         struct raw3215_info *raw;
982
983         raw = (struct raw3215_info *) tty->driver_data;
984         return raw->count;
985 }
986
987 static void
988 tty3215_flush_buffer(struct tty_struct *tty)
989 {
990         struct raw3215_info *raw;
991
992         raw = (struct raw3215_info *) tty->driver_data;
993         raw3215_flush_buffer(raw);
994         tty_wakeup(tty);
995 }
996
997 /*
998  * Currently we don't have any io controls for 3215 ttys
999  */
1000 static int
1001 tty3215_ioctl(struct tty_struct *tty, struct file * file,
1002               unsigned int cmd, unsigned long arg)
1003 {
1004         if (tty->flags & (1 << TTY_IO_ERROR))
1005                 return -EIO;
1006
1007         switch (cmd) {
1008         default:
1009                 return -ENOIOCTLCMD;
1010         }
1011         return 0;
1012 }
1013
1014 /*
1015  * Disable reading from a 3215 tty
1016  */
1017 static void
1018 tty3215_throttle(struct tty_struct * tty)
1019 {
1020         struct raw3215_info *raw;
1021
1022         raw = (struct raw3215_info *) tty->driver_data;
1023         raw->flags |= RAW3215_THROTTLED;
1024 }
1025
1026 /*
1027  * Enable reading from a 3215 tty
1028  */
1029 static void
1030 tty3215_unthrottle(struct tty_struct * tty)
1031 {
1032         struct raw3215_info *raw;
1033         unsigned long flags;
1034
1035         raw = (struct raw3215_info *) tty->driver_data;
1036         if (raw->flags & RAW3215_THROTTLED) {
1037                 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1038                 raw->flags &= ~RAW3215_THROTTLED;
1039                 raw3215_try_io(raw);
1040                 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1041         }
1042 }
1043
1044 /*
1045  * Disable writing to a 3215 tty
1046  */
1047 static void
1048 tty3215_stop(struct tty_struct *tty)
1049 {
1050         struct raw3215_info *raw;
1051
1052         raw = (struct raw3215_info *) tty->driver_data;
1053         raw->flags |= RAW3215_STOPPED;
1054 }
1055
1056 /*
1057  * Enable writing to a 3215 tty
1058  */
1059 static void
1060 tty3215_start(struct tty_struct *tty)
1061 {
1062         struct raw3215_info *raw;
1063         unsigned long flags;
1064
1065         raw = (struct raw3215_info *) tty->driver_data;
1066         if (raw->flags & RAW3215_STOPPED) {
1067                 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1068                 raw->flags &= ~RAW3215_STOPPED;
1069                 raw3215_try_io(raw);
1070                 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1071         }
1072 }
1073
1074 static const struct tty_operations tty3215_ops = {
1075         .open = tty3215_open,
1076         .close = tty3215_close,
1077         .write = tty3215_write,
1078         .put_char = tty3215_put_char,
1079         .flush_chars = tty3215_flush_chars,
1080         .write_room = tty3215_write_room,
1081         .chars_in_buffer = tty3215_chars_in_buffer,
1082         .flush_buffer = tty3215_flush_buffer,
1083         .ioctl = tty3215_ioctl,
1084         .throttle = tty3215_throttle,
1085         .unthrottle = tty3215_unthrottle,
1086         .stop = tty3215_stop,
1087         .start = tty3215_start,
1088 };
1089
1090 /*
1091  * 3215 tty registration code called from tty_init().
1092  * Most kernel services (incl. kmalloc) are available at this poimt.
1093  */
1094 static int __init
1095 tty3215_init(void)
1096 {
1097         struct tty_driver *driver;
1098         int ret;
1099
1100         if (!CONSOLE_IS_3215)
1101                 return 0;
1102
1103         driver = alloc_tty_driver(NR_3215);
1104         if (!driver)
1105                 return -ENOMEM;
1106
1107         ret = ccw_driver_register(&raw3215_ccw_driver);
1108         if (ret) {
1109                 put_tty_driver(driver);
1110                 return ret;
1111         }
1112         /*
1113          * Initialize the tty_driver structure
1114          * Entries in tty3215_driver that are NOT initialized:
1115          * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1116          */
1117
1118         driver->owner = THIS_MODULE;
1119         driver->driver_name = "tty3215";
1120         driver->name = "ttyS";
1121         driver->major = TTY_MAJOR;
1122         driver->minor_start = 64;
1123         driver->type = TTY_DRIVER_TYPE_SYSTEM;
1124         driver->subtype = SYSTEM_TYPE_TTY;
1125         driver->init_termios = tty_std_termios;
1126         driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1127         driver->init_termios.c_oflag = ONLCR | XTABS;
1128         driver->init_termios.c_lflag = ISIG;
1129         driver->flags = TTY_DRIVER_REAL_RAW;
1130         tty_set_operations(driver, &tty3215_ops);
1131         ret = tty_register_driver(driver);
1132         if (ret) {
1133                 put_tty_driver(driver);
1134                 return ret;
1135         }
1136         tty3215_driver = driver;
1137         return 0;
1138 }
1139
1140 static void __exit
1141 tty3215_exit(void)
1142 {
1143         tty_unregister_driver(tty3215_driver);
1144         put_tty_driver(tty3215_driver);
1145         ccw_driver_unregister(&raw3215_ccw_driver);
1146 }
1147
1148 module_init(tty3215_init);
1149 module_exit(tty3215_exit);