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