fix mismerge in ll_rw_blk.c
[linux-2.6] / 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 DEFINE_SPINLOCK(raw3215_device_lock);
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                 tty_wakeup(tty);
370         }
371 }
372
373 /*
374  * Interrupt routine, called from common io layer
375  */
376 static void
377 raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
378 {
379         struct raw3215_info *raw;
380         struct raw3215_req *req;
381         struct tty_struct *tty;
382         int cstat, dstat;
383         int count, slen;
384
385         raw = cdev->dev.driver_data;
386         req = (struct raw3215_req *) intparm;
387         cstat = irb->scsw.cstat;
388         dstat = irb->scsw.dstat;
389         if (cstat != 0) {
390                 raw->message = KERN_WARNING
391                         "Got nonzero channel status in raw3215_irq "
392                         "(dev sts 0x%2x, sch sts 0x%2x)";
393                 raw->msg_dstat = dstat;
394                 raw->msg_cstat = cstat;
395                 tasklet_schedule(&raw->tasklet);
396         }
397         if (dstat & 0x01) { /* we got a unit exception */
398                 dstat &= ~0x01;  /* we can ignore it */
399         }
400         switch (dstat) {
401         case 0x80:
402                 if (cstat != 0)
403                         break;
404                 /* Attention interrupt, someone hit the enter key */
405                 raw3215_mk_read_req(raw);
406                 if (MACHINE_IS_P390)
407                         memset(raw->inbuf, 0, RAW3215_INBUF_SIZE);
408                 tasklet_schedule(&raw->tasklet);
409                 break;
410         case 0x08:
411         case 0x0C:
412                 /* Channel end interrupt. */
413                 if ((raw = req->info) == NULL)
414                         return;              /* That shouldn't happen ... */
415                 if (req->type == RAW3215_READ) {
416                         /* store residual count, then wait for device end */
417                         req->residual = irb->scsw.count;
418                 }
419                 if (dstat == 0x08)
420                         break;
421         case 0x04:
422                 /* Device end interrupt. */
423                 if ((raw = req->info) == NULL)
424                         return;              /* That shouldn't happen ... */
425                 if (req->type == RAW3215_READ && raw->tty != NULL) {
426                         unsigned int cchar;
427
428                         tty = raw->tty;
429                         count = 160 - req->residual;
430                         if (MACHINE_IS_P390) {
431                                 slen = strnlen(raw->inbuf, RAW3215_INBUF_SIZE);
432                                 if (count > slen)
433                                         count = slen;
434                         } else
435                         if (count >= TTY_FLIPBUF_SIZE - tty->flip.count)
436                                 count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;
437                         EBCASC(raw->inbuf, count);
438                         cchar = ctrlchar_handle(raw->inbuf, count, tty);
439                         switch (cchar & CTRLCHAR_MASK) {
440                         case CTRLCHAR_SYSRQ:
441                                 break;
442
443                         case CTRLCHAR_CTRL:
444                                 tty->flip.count++;
445                                 *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
446                                 *tty->flip.char_buf_ptr++ = cchar;
447                                 tty_flip_buffer_push(raw->tty);
448                                 break;
449
450                         case CTRLCHAR_NONE:
451                                 memcpy(tty->flip.char_buf_ptr,
452                                        raw->inbuf, count);
453                                 if (count < 2 ||
454                                     (strncmp(raw->inbuf+count-2, "^n", 2) &&
455                                     strncmp(raw->inbuf+count-2, "\252n", 2)) ) {
456                                         /* don't add the auto \n */
457                                         tty->flip.char_buf_ptr[count] = '\n';
458                                         memset(tty->flip.flag_buf_ptr,
459                                                TTY_NORMAL, count + 1);
460                                         count++;
461                                 } else
462                                         count-=2;
463                                 tty->flip.char_buf_ptr += count;
464                                 tty->flip.flag_buf_ptr += count;
465                                 tty->flip.count += count;
466                                 tty_flip_buffer_push(raw->tty);
467                                 break;
468                         }
469                 } else if (req->type == RAW3215_WRITE) {
470                         raw->count -= req->len;
471                         raw->written -= req->len;
472                 }
473                 raw->flags &= ~RAW3215_WORKING;
474                 raw3215_free_req(req);
475                 /* check for empty wait */
476                 if (waitqueue_active(&raw->empty_wait) &&
477                     raw->queued_write == NULL &&
478                     raw->queued_read == NULL) {
479                         wake_up_interruptible(&raw->empty_wait);
480                 }
481                 tasklet_schedule(&raw->tasklet);
482                 break;
483         default:
484                 /* Strange interrupt, I'll do my best to clean up */
485                 if (req != NULL && req->type != RAW3215_FREE) {
486                         if (req->type == RAW3215_WRITE) {
487                                 raw->count -= req->len;
488                                 raw->written -= req->len;
489                         }
490                         raw->flags &= ~RAW3215_WORKING;
491                         raw3215_free_req(req);
492                 }
493                 raw->message = KERN_WARNING
494                         "Spurious interrupt in in raw3215_irq "
495                         "(dev sts 0x%2x, sch sts 0x%2x)";
496                 raw->msg_dstat = dstat;
497                 raw->msg_cstat = cstat;
498                 tasklet_schedule(&raw->tasklet);
499         }
500         return;
501 }
502
503 /*
504  * Wait until length bytes are available int the output buffer.
505  * Has to be called with the s390irq lock held. Can be called
506  * disabled.
507  */
508 static void
509 raw3215_make_room(struct raw3215_info *raw, unsigned int length)
510 {
511         while (RAW3215_BUFFER_SIZE - raw->count < length) {
512                 /* there might be a request pending */
513                 raw->flags |= RAW3215_FLUSHING;
514                 raw3215_mk_write_req(raw);
515                 raw3215_try_io(raw);
516                 raw->flags &= ~RAW3215_FLUSHING;
517 #ifdef CONFIG_TN3215_CONSOLE
518                 wait_cons_dev();
519 #endif
520                 /* Enough room freed up ? */
521                 if (RAW3215_BUFFER_SIZE - raw->count >= length)
522                         break;
523                 /* there might be another cpu waiting for the lock */
524                 spin_unlock(raw->lock);
525                 udelay(100);
526                 spin_lock(raw->lock);
527         }
528 }
529
530 /*
531  * String write routine for 3215 devices
532  */
533 static void
534 raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
535 {
536         unsigned long flags;
537         int c, count;
538
539         while (length > 0) {
540                 spin_lock_irqsave(raw->lock, flags);
541                 count = (length > RAW3215_BUFFER_SIZE) ?
542                                              RAW3215_BUFFER_SIZE : length;
543                 length -= count;
544
545                 raw3215_make_room(raw, count);
546
547                 /* copy string to output buffer and convert it to EBCDIC */
548                 while (1) {
549                         c = min_t(int, count,
550                                   min(RAW3215_BUFFER_SIZE - raw->count,
551                                       RAW3215_BUFFER_SIZE - raw->head));
552                         if (c <= 0)
553                                 break;
554                         memcpy(raw->buffer + raw->head, str, c);
555                         ASCEBC(raw->buffer + raw->head, c);
556                         raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
557                         raw->count += c;
558                         raw->line_pos += c;
559                         str += c;
560                         count -= c;
561                 }
562                 if (!(raw->flags & RAW3215_WORKING)) {
563                         raw3215_mk_write_req(raw);
564                         /* start or queue request */
565                         raw3215_try_io(raw);
566                 }
567                 spin_unlock_irqrestore(raw->lock, flags);
568         }
569 }
570
571 /*
572  * Put character routine for 3215 devices
573  */
574 static void
575 raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
576 {
577         unsigned long flags;
578         unsigned int length, i;
579
580         spin_lock_irqsave(raw->lock, flags);
581         if (ch == '\t') {
582                 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
583                 raw->line_pos += length;
584                 ch = ' ';
585         } else if (ch == '\n') {
586                 length = 1;
587                 raw->line_pos = 0;
588         } else {
589                 length = 1;
590                 raw->line_pos++;
591         }
592         raw3215_make_room(raw, length);
593
594         for (i = 0; i < length; i++) {
595                 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
596                 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
597                 raw->count++;
598         }
599         if (!(raw->flags & RAW3215_WORKING)) {
600                 raw3215_mk_write_req(raw);
601                 /* start or queue request */
602                 raw3215_try_io(raw);
603         }
604         spin_unlock_irqrestore(raw->lock, flags);
605 }
606
607 /*
608  * Flush routine, it simply sets the flush flag and tries to start
609  * pending IO.
610  */
611 static void
612 raw3215_flush_buffer(struct raw3215_info *raw)
613 {
614         unsigned long flags;
615
616         spin_lock_irqsave(raw->lock, flags);
617         if (raw->count > 0) {
618                 raw->flags |= RAW3215_FLUSHING;
619                 raw3215_try_io(raw);
620                 raw->flags &= ~RAW3215_FLUSHING;
621         }
622         spin_unlock_irqrestore(raw->lock, flags);
623 }
624
625 /*
626  * Fire up a 3215 device.
627  */
628 static int
629 raw3215_startup(struct raw3215_info *raw)
630 {
631         unsigned long flags;
632
633         if (raw->flags & RAW3215_ACTIVE)
634                 return 0;
635         raw->line_pos = 0;
636         raw->flags |= RAW3215_ACTIVE;
637         spin_lock_irqsave(raw->lock, flags);
638         raw3215_try_io(raw);
639         spin_unlock_irqrestore(raw->lock, flags);
640
641         return 0;
642 }
643
644 /*
645  * Shutdown a 3215 device.
646  */
647 static void
648 raw3215_shutdown(struct raw3215_info *raw)
649 {
650         DECLARE_WAITQUEUE(wait, current);
651         unsigned long flags;
652
653         if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
654                 return;
655         /* Wait for outstanding requests, then free irq */
656         spin_lock_irqsave(raw->lock, flags);
657         if ((raw->flags & RAW3215_WORKING) ||
658             raw->queued_write != NULL ||
659             raw->queued_read != NULL) {
660                 raw->flags |= RAW3215_CLOSING;
661                 add_wait_queue(&raw->empty_wait, &wait);
662                 set_current_state(TASK_INTERRUPTIBLE);
663                 spin_unlock_irqrestore(raw->lock, flags);
664                 schedule();
665                 spin_lock_irqsave(raw->lock, flags);
666                 remove_wait_queue(&raw->empty_wait, &wait);
667                 set_current_state(TASK_RUNNING);
668                 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
669         }
670         spin_unlock_irqrestore(raw->lock, flags);
671 }
672
673 static int
674 raw3215_probe (struct ccw_device *cdev)
675 {
676         struct raw3215_info *raw;
677         int line;
678
679         raw = kmalloc(sizeof(struct raw3215_info) +
680                       RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
681         if (raw == NULL)
682                 return -ENOMEM;
683
684         spin_lock(&raw3215_device_lock);
685         for (line = 0; line < NR_3215; line++) {
686                 if (!raw3215[line]) {
687                         raw3215[line] = raw;
688                         break;
689                 }
690         }
691         spin_unlock(&raw3215_device_lock);
692         if (line == NR_3215) {
693                 kfree(raw);
694                 return -ENODEV;
695         }
696
697         raw->cdev = cdev;
698         raw->lock = get_ccwdev_lock(cdev);
699         raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
700         memset(raw, 0, sizeof(struct raw3215_info));
701         raw->buffer = (char *) kmalloc(RAW3215_BUFFER_SIZE,
702                                        GFP_KERNEL|GFP_DMA);
703         if (raw->buffer == NULL) {
704                 spin_lock(&raw3215_device_lock);
705                 raw3215[line] = 0;
706                 spin_unlock(&raw3215_device_lock);
707                 kfree(raw);
708                 return -ENOMEM;
709         }
710         tasklet_init(&raw->tasklet,
711                      (void (*)(unsigned long)) raw3215_tasklet,
712                      (unsigned long) raw);
713         init_waitqueue_head(&raw->empty_wait);
714
715         cdev->dev.driver_data = raw;
716         cdev->handler = raw3215_irq;
717
718         return 0;
719 }
720
721 static void
722 raw3215_remove (struct ccw_device *cdev)
723 {
724         struct raw3215_info *raw;
725
726         ccw_device_set_offline(cdev);
727         raw = cdev->dev.driver_data;
728         if (raw) {
729                 cdev->dev.driver_data = NULL;
730                 if (raw->buffer)
731                         kfree(raw->buffer);
732                 kfree(raw);
733         }
734 }
735
736 static int
737 raw3215_set_online (struct ccw_device *cdev)
738 {
739         struct raw3215_info *raw;
740
741         raw = cdev->dev.driver_data;
742         if (!raw)
743                 return -ENODEV;
744
745         return raw3215_startup(raw);
746 }
747
748 static int
749 raw3215_set_offline (struct ccw_device *cdev)
750 {
751         struct raw3215_info *raw;
752
753         raw = cdev->dev.driver_data;
754         if (!raw)
755                 return -ENODEV;
756
757         raw3215_shutdown(raw);
758
759         return 0;
760 }
761
762 static struct ccw_device_id raw3215_id[] = {
763         { CCW_DEVICE(0x3215, 0) },
764         { /* end of list */ },
765 };
766
767 static struct ccw_driver raw3215_ccw_driver = {
768         .name           = "3215",
769         .owner          = THIS_MODULE,
770         .ids            = raw3215_id,
771         .probe          = &raw3215_probe,
772         .remove         = &raw3215_remove,
773         .set_online     = &raw3215_set_online,
774         .set_offline    = &raw3215_set_offline,
775 };
776
777 #ifdef CONFIG_TN3215_CONSOLE
778 /*
779  * Write a string to the 3215 console
780  */
781 static void
782 con3215_write(struct console *co, const char *str, unsigned int count)
783 {
784         struct raw3215_info *raw;
785         int i;
786
787         if (count <= 0)
788                 return;
789         raw = raw3215[0];       /* console 3215 is the first one */
790         while (count > 0) {
791                 for (i = 0; i < count; i++)
792                         if (str[i] == '\t' || str[i] == '\n')
793                                 break;
794                 raw3215_write(raw, str, i);
795                 count -= i;
796                 str += i;
797                 if (count > 0) {
798                         raw3215_putchar(raw, *str);
799                         count--;
800                         str++;
801                 }
802         }
803 }
804
805 static struct tty_driver *con3215_device(struct console *c, int *index)
806 {
807         *index = c->index;
808         return tty3215_driver;
809 }
810
811 /*
812  * panic() calls console_unblank before the system enters a
813  * disabled, endless loop.
814  */
815 static void
816 con3215_unblank(void)
817 {
818         struct raw3215_info *raw;
819         unsigned long flags;
820
821         raw = raw3215[0];  /* console 3215 is the first one */
822         spin_lock_irqsave(raw->lock, flags);
823         raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
824         spin_unlock_irqrestore(raw->lock, flags);
825 }
826
827 static int __init 
828 con3215_consetup(struct console *co, char *options)
829 {
830         return 0;
831 }
832
833 /*
834  *  The console structure for the 3215 console
835  */
836 static struct console con3215 = {
837         .name    = "ttyS",
838         .write   = con3215_write,
839         .device  = con3215_device,
840         .unblank = con3215_unblank,
841         .setup   = con3215_consetup,
842         .flags   = CON_PRINTBUFFER,
843 };
844
845 /*
846  * 3215 console initialization code called from console_init().
847  * NOTE: This is called before kmalloc is available.
848  */
849 static int __init
850 con3215_init(void)
851 {
852         struct ccw_device *cdev;
853         struct raw3215_info *raw;
854         struct raw3215_req *req;
855         int i;
856
857         /* Check if 3215 is to be the console */
858         if (!CONSOLE_IS_3215)
859                 return -ENODEV;
860
861         /* Set the console mode for VM */
862         if (MACHINE_IS_VM) {
863                 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
864                 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
865         }
866
867         /* allocate 3215 request structures */
868         raw3215_freelist = NULL;
869         spin_lock_init(&raw3215_freelist_lock);
870         for (i = 0; i < NR_3215_REQ; i++) {
871                 req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
872                 req->next = raw3215_freelist;
873                 raw3215_freelist = req;
874         }
875
876         cdev = ccw_device_probe_console();
877         if (!cdev)
878                 return -ENODEV;
879
880         raw3215[0] = raw = (struct raw3215_info *)
881                 alloc_bootmem_low(sizeof(struct raw3215_info));
882         memset(raw, 0, sizeof(struct raw3215_info));
883         raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
884         raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
885         raw->cdev = cdev;
886         raw->lock = get_ccwdev_lock(cdev);
887         cdev->dev.driver_data = raw;
888         cdev->handler = raw3215_irq;
889
890         raw->flags |= RAW3215_FIXED;
891         tasklet_init(&raw->tasklet,
892                      (void (*)(unsigned long)) raw3215_tasklet,
893                      (unsigned long) raw);
894         init_waitqueue_head(&raw->empty_wait);
895
896         /* Request the console irq */
897         if (raw3215_startup(raw) != 0) {
898                 free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
899                 free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
900                 free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
901                 raw3215[0] = NULL;
902                 printk("Couldn't find a 3215 console device\n");
903                 return -ENODEV;
904         }
905         register_console(&con3215);
906         return 0;
907 }
908 console_initcall(con3215_init);
909 #endif
910
911 /*
912  * tty3215_open
913  *
914  * This routine is called whenever a 3215 tty is opened.
915  */
916 static int
917 tty3215_open(struct tty_struct *tty, struct file * filp)
918 {
919         struct raw3215_info *raw;
920         int retval, line;
921
922         line = tty->index;
923         if ((line < 0) || (line >= NR_3215))
924                 return -ENODEV;
925
926         raw = raw3215[line];
927         if (raw == NULL)
928                 return -ENODEV;
929
930         tty->driver_data = raw;
931         raw->tty = tty;
932
933         tty->low_latency = 0;  /* don't use bottom half for pushing chars */
934         /*
935          * Start up 3215 device
936          */
937         retval = raw3215_startup(raw);
938         if (retval)
939                 return retval;
940
941         return 0;
942 }
943
944 /*
945  * tty3215_close()
946  *
947  * This routine is called when the 3215 tty is closed. We wait
948  * for the remaining request to be completed. Then we clean up.
949  */
950 static void
951 tty3215_close(struct tty_struct *tty, struct file * filp)
952 {
953         struct raw3215_info *raw;
954
955         raw = (struct raw3215_info *) tty->driver_data;
956         if (raw == NULL || tty->count > 1)
957                 return;
958         tty->closing = 1;
959         /* Shutdown the terminal */
960         raw3215_shutdown(raw);
961         tty->closing = 0;
962         raw->tty = NULL;
963 }
964
965 /*
966  * Returns the amount of free space in the output buffer.
967  */
968 static int
969 tty3215_write_room(struct tty_struct *tty)
970 {
971         struct raw3215_info *raw;
972
973         raw = (struct raw3215_info *) tty->driver_data;
974
975         /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
976         if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
977                 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
978         else
979                 return 0;
980 }
981
982 /*
983  * String write routine for 3215 ttys
984  */
985 static int
986 tty3215_write(struct tty_struct * tty,
987               const unsigned char *buf, int count)
988 {
989         struct raw3215_info *raw;
990
991         if (!tty)
992                 return 0;
993         raw = (struct raw3215_info *) tty->driver_data;
994         raw3215_write(raw, buf, count);
995         return count;
996 }
997
998 /*
999  * Put character routine for 3215 ttys
1000  */
1001 static void
1002 tty3215_put_char(struct tty_struct *tty, unsigned char ch)
1003 {
1004         struct raw3215_info *raw;
1005
1006         if (!tty)
1007                 return;
1008         raw = (struct raw3215_info *) tty->driver_data;
1009         raw3215_putchar(raw, ch);
1010 }
1011
1012 static void
1013 tty3215_flush_chars(struct tty_struct *tty)
1014 {
1015 }
1016
1017 /*
1018  * Returns the number of characters in the output buffer
1019  */
1020 static int
1021 tty3215_chars_in_buffer(struct tty_struct *tty)
1022 {
1023         struct raw3215_info *raw;
1024
1025         raw = (struct raw3215_info *) tty->driver_data;
1026         return raw->count;
1027 }
1028
1029 static void
1030 tty3215_flush_buffer(struct tty_struct *tty)
1031 {
1032         struct raw3215_info *raw;
1033
1034         raw = (struct raw3215_info *) tty->driver_data;
1035         raw3215_flush_buffer(raw);
1036         tty_wakeup(tty);
1037 }
1038
1039 /*
1040  * Currently we don't have any io controls for 3215 ttys
1041  */
1042 static int
1043 tty3215_ioctl(struct tty_struct *tty, struct file * file,
1044               unsigned int cmd, unsigned long arg)
1045 {
1046         if (tty->flags & (1 << TTY_IO_ERROR))
1047                 return -EIO;
1048
1049         switch (cmd) {
1050         default:
1051                 return -ENOIOCTLCMD;
1052         }
1053         return 0;
1054 }
1055
1056 /*
1057  * Disable reading from a 3215 tty
1058  */
1059 static void
1060 tty3215_throttle(struct tty_struct * tty)
1061 {
1062         struct raw3215_info *raw;
1063
1064         raw = (struct raw3215_info *) tty->driver_data;
1065         raw->flags |= RAW3215_THROTTLED;
1066 }
1067
1068 /*
1069  * Enable reading from a 3215 tty
1070  */
1071 static void
1072 tty3215_unthrottle(struct tty_struct * tty)
1073 {
1074         struct raw3215_info *raw;
1075         unsigned long flags;
1076
1077         raw = (struct raw3215_info *) tty->driver_data;
1078         if (raw->flags & RAW3215_THROTTLED) {
1079                 spin_lock_irqsave(raw->lock, flags);
1080                 raw->flags &= ~RAW3215_THROTTLED;
1081                 raw3215_try_io(raw);
1082                 spin_unlock_irqrestore(raw->lock, flags);
1083         }
1084 }
1085
1086 /*
1087  * Disable writing to a 3215 tty
1088  */
1089 static void
1090 tty3215_stop(struct tty_struct *tty)
1091 {
1092         struct raw3215_info *raw;
1093
1094         raw = (struct raw3215_info *) tty->driver_data;
1095         raw->flags |= RAW3215_STOPPED;
1096 }
1097
1098 /*
1099  * Enable writing to a 3215 tty
1100  */
1101 static void
1102 tty3215_start(struct tty_struct *tty)
1103 {
1104         struct raw3215_info *raw;
1105         unsigned long flags;
1106
1107         raw = (struct raw3215_info *) tty->driver_data;
1108         if (raw->flags & RAW3215_STOPPED) {
1109                 spin_lock_irqsave(raw->lock, flags);
1110                 raw->flags &= ~RAW3215_STOPPED;
1111                 raw3215_try_io(raw);
1112                 spin_unlock_irqrestore(raw->lock, flags);
1113         }
1114 }
1115
1116 static struct tty_operations tty3215_ops = {
1117         .open = tty3215_open,
1118         .close = tty3215_close,
1119         .write = tty3215_write,
1120         .put_char = tty3215_put_char,
1121         .flush_chars = tty3215_flush_chars,
1122         .write_room = tty3215_write_room,
1123         .chars_in_buffer = tty3215_chars_in_buffer,
1124         .flush_buffer = tty3215_flush_buffer,
1125         .ioctl = tty3215_ioctl,
1126         .throttle = tty3215_throttle,
1127         .unthrottle = tty3215_unthrottle,
1128         .stop = tty3215_stop,
1129         .start = tty3215_start,
1130 };
1131
1132 /*
1133  * 3215 tty registration code called from tty_init().
1134  * Most kernel services (incl. kmalloc) are available at this poimt.
1135  */
1136 int __init
1137 tty3215_init(void)
1138 {
1139         struct tty_driver *driver;
1140         int ret;
1141
1142         if (!CONSOLE_IS_3215)
1143                 return 0;
1144
1145         driver = alloc_tty_driver(NR_3215);
1146         if (!driver)
1147                 return -ENOMEM;
1148
1149         ret = ccw_driver_register(&raw3215_ccw_driver);
1150         if (ret) {
1151                 put_tty_driver(driver);
1152                 return ret;
1153         }
1154         /*
1155          * Initialize the tty_driver structure
1156          * Entries in tty3215_driver that are NOT initialized:
1157          * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1158          */
1159
1160         driver->owner = THIS_MODULE;
1161         driver->driver_name = "tty3215";
1162         driver->name = "ttyS";
1163         driver->major = TTY_MAJOR;
1164         driver->minor_start = 64;
1165         driver->type = TTY_DRIVER_TYPE_SYSTEM;
1166         driver->subtype = SYSTEM_TYPE_TTY;
1167         driver->init_termios = tty_std_termios;
1168         driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1169         driver->init_termios.c_oflag = ONLCR | XTABS;
1170         driver->init_termios.c_lflag = ISIG;
1171         driver->flags = TTY_DRIVER_REAL_RAW;
1172         tty_set_operations(driver, &tty3215_ops);
1173         ret = tty_register_driver(driver);
1174         if (ret) {
1175                 printk("Couldn't register tty3215 driver\n");
1176                 put_tty_driver(driver);
1177                 return ret;
1178         }
1179         tty3215_driver = driver;
1180         return 0;
1181 }
1182
1183 static void __exit
1184 tty3215_exit(void)
1185 {
1186         tty_unregister_driver(tty3215_driver);
1187         put_tty_driver(tty3215_driver);
1188         ccw_driver_unregister(&raw3215_ccw_driver);
1189 }
1190
1191 module_init(tty3215_init);
1192 module_exit(tty3215_exit);