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