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