File: /usr/src/linux/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 extern 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 extern 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 extern 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 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
367 tty->ldisc.write_wakeup)
368 (tty->ldisc.write_wakeup)(tty);
369 wake_up_interruptible(&tty->write_wait);
370 }
371 }
372
373 /*
374 * Function to safely add raw3215_softint to tq_immediate.
375 * The s390irq spinlock must be held.
376 */
377 static inline void raw3215_sched_bh(raw3215_info *raw)
378 {
379 if (raw->flags & RAW3215_BH_PENDING)
380 return; /* already pending */
381 raw->flags |= RAW3215_BH_PENDING;
382 INIT_LIST_HEAD(&raw->tqueue.list);
383 raw->tqueue.sync = 0;
384 raw->tqueue.routine = raw3215_softint;
385 raw->tqueue.data = raw;
386 queue_task(&raw->tqueue, &tq_immediate);
387 mark_bh(IMMEDIATE_BH);
388 }
389
390 /*
391 * Find the raw3215_info structure associated with irq
392 */
393 static inline raw3215_info *raw3215_find_info(int irq) {
394 raw3215_info *raw;
395 int i;
396
397 for (i = 0; i < NR_3215; i++) {
398 raw = raw3215[i];
399 if (raw != NULL && raw->irq == irq &&
400 (raw->flags & RAW3215_ACTIVE))
401 break;
402 }
403 return (i >= NR_3215) ? NULL : raw;
404 }
405
406 /*
407 * Interrupt routine, called from Ingo's I/O layer
408 */
409 static void raw3215_irq(int irq, void *int_parm, struct pt_regs *regs)
410 {
411 raw3215_info *raw;
412 raw3215_req *req;
413 struct tty_struct *tty;
414 devstat_t *stat;
415 int cstat, dstat;
416 int count, slen;
417
418 stat = (devstat_t *) int_parm;
419 req = (raw3215_req *) stat->intparm;
420 cstat = stat->cstat;
421 dstat = stat->dstat;
422 if (cstat != 0) {
423 raw = raw3215_find_info(irq);
424 if (raw != NULL) {
425 raw->message = KERN_WARNING
426 "Got nonzero channel status in raw3215_irq "
427 "(dev %i, dev sts 0x%2x, sch sts 0x%2x)";
428 raw->msg_dstat = dstat;
429 raw->msg_cstat = cstat;
430 raw3215_sched_bh(raw);
431 }
432 }
433 if (dstat & 0x01) { /* we got a unit exception */
434 dstat &= ~0x01; /* we can ignore it */
435 }
436 switch (dstat) {
437 case 0x80:
438 if (cstat != 0)
439 break;
440 /* Attention interrupt, someone hit the enter key */
441 if ((raw = raw3215_find_info(irq)) == NULL)
442 return; /* That shouldn't happen ... */
443 /* Setup a read request */
444 raw3215_mk_read_req(raw);
445 if (MACHINE_IS_P390)
446 memset(raw->inbuf, 0, RAW3215_INBUF_SIZE);
447 raw3215_sched_bh(raw);
448 break;
449 case 0x08:
450 case 0x0C:
451 /* Channel end interrupt. */
452 if ((raw = req->info) == NULL)
453 return; /* That shouldn't happen ... */
454 if (req->type == RAW3215_READ) {
455 /* store residual count, then wait for device end */
456 req->residual = stat->rescnt;
457 }
458 if (dstat == 0x08)
459 break;
460 case 0x04:
461 /* Device end interrupt. */
462 if ((raw = req->info) == NULL)
463 return; /* That shouldn't happen ... */
464 if (req->type == RAW3215_READ && raw->tty != NULL) {
465 char *cchar;
466
467 tty = raw->tty;
468 count = 160 - req->residual;
469 if (MACHINE_IS_P390) {
470 slen = strnlen(raw->inbuf, RAW3215_INBUF_SIZE);
471 if (count > slen)
472 count = slen;
473 } else
474 if (count >= TTY_FLIPBUF_SIZE - tty->flip.count)
475 count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;
476 EBCASC(raw->inbuf, count);
477 if ((cchar = ctrlchar_handle(raw->inbuf, count, tty))) {
478 if (cchar == (char *)-1)
479 goto in_out;
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 } else {
485 memcpy(tty->flip.char_buf_ptr,
486 raw->inbuf, count);
487 if (count < 2 ||
488 (strncmp(raw->inbuf+count-2, "^n", 2) ||
489 strncmp(raw->inbuf+count-2, "\252n", 2)) ) {
490 /* don't add the auto \n */
491 tty->flip.char_buf_ptr[count] = '\n';
492 memset(tty->flip.flag_buf_ptr,
493 TTY_NORMAL, count + 1);
494 count++;
495 } else
496 count-=2;
497 tty->flip.char_buf_ptr += count;
498 tty->flip.flag_buf_ptr += count;
499 tty->flip.count += count;
500 tty_flip_buffer_push(raw->tty);
501 }
502 } else if (req->type == RAW3215_WRITE) {
503 raw->count -= req->len;
504 raw->written -= req->len;
505 }
506 in_out:
507 raw->flags &= ~RAW3215_WORKING;
508 raw3215_free_req(req);
509 /* check for empty wait */
510 if (waitqueue_active(&raw->empty_wait) &&
511 raw->queued_write == NULL &&
512 raw->queued_read == NULL) {
513 wake_up_interruptible(&raw->empty_wait);
514 }
515 raw3215_sched_bh(raw);
516 break;
517 default:
518 /* Strange interrupt, I'll do my best to clean up */
519 if ((raw = raw3215_find_info(irq)) == NULL)
520 return; /* That shouldn't happen ... */
521 if (raw == NULL) break;
522 if (req != NULL && req->type != RAW3215_FREE) {
523 if (req->type == RAW3215_WRITE) {
524 raw->count -= req->len;
525 raw->written -= req->len;
526 }
527 raw->flags &= ~RAW3215_WORKING;
528 raw3215_free_req(req);
529 }
530 raw->message = KERN_WARNING
531 "Spurious interrupt in in raw3215_irq "
532 "(dev %i, dev sts 0x%2x, sch sts 0x%2x)";
533 raw->msg_dstat = dstat;
534 raw->msg_cstat = cstat;
535 raw3215_sched_bh(raw);
536 }
537 return;
538 }
539
540 /*
541 * Wait until length bytes are available int the output buffer.
542 * Has to be called with the s390irq lock held. Can be called
543 * disabled.
544 */
545 void raw3215_make_room(raw3215_info *raw, unsigned int length)
546 {
547 while (RAW3215_BUFFER_SIZE - raw->count < length) {
548 /* there might be a request pending */
549 raw->flags |= RAW3215_FLUSHING;
550 raw3215_mk_write_req(raw);
551 raw3215_try_io(raw);
552 raw->flags &= ~RAW3215_FLUSHING;
553 if (wait_cons_dev(raw->irq) != 0) {
554 /* that shouldn't happen */
555 raw->count = 0;
556 raw->written = 0;
557 }
558 /* Enough room freed up ? */
559 if (RAW3215_BUFFER_SIZE - raw->count >= length)
560 break;
561 /* there might be another cpu waiting for the lock */
562 s390irq_spin_unlock(raw->irq);
563 udelay(100);
564 s390irq_spin_lock(raw->irq);
565 }
566 }
567
568 /*
569 * String write routine for 3215 devices
570 */
571 static int
572 raw3215_write(raw3215_info *raw, const char *str,
573 int from_user, unsigned int length)
574 {
575 unsigned long flags;
576 int ret, c;
577 int count;
578
579 ret = 0;
580 while (length > 0) {
581 s390irq_spin_lock_irqsave(raw->irq, flags);
582 count = (length > RAW3215_BUFFER_SIZE) ?
583 RAW3215_BUFFER_SIZE : length;
584 length -= count;
585
586 raw3215_make_room(raw, count);
587
588 /* copy string to output buffer and convert it to EBCDIC */
589 if (from_user) {
590 while (1) {
591 c = MIN(count,
592 MIN(RAW3215_BUFFER_SIZE - raw->count,
593 RAW3215_BUFFER_SIZE - raw->head));
594 if (c <= 0)
595 break;
596 c -= copy_from_user(raw->buffer + raw->head,
597 str, c);
598 if (c == 0) {
599 if (!ret)
600 ret = -EFAULT;
601 break;
602 }
603 ASCEBC(raw->buffer + raw->head, c);
604 raw->head = (raw->head + c) &
605 (RAW3215_BUFFER_SIZE - 1);
606 raw->count += c;
607 raw->line_pos += c;
608 str += c;
609 count -= c;
610 ret += c;
611 }
612 } else {
613 while (1) {
614 c = MIN(count,
615 MIN(RAW3215_BUFFER_SIZE - raw->count,
616 RAW3215_BUFFER_SIZE - raw->head));
617 if (c <= 0)
618 break;
619 memcpy(raw->buffer + raw->head, str, c);
620 ASCEBC(raw->buffer + raw->head, c);
621 raw->head = (raw->head + c) &
622 (RAW3215_BUFFER_SIZE - 1);
623 raw->count += c;
624 raw->line_pos += c;
625 str += c;
626 count -= c;
627 ret += c;
628 }
629 }
630 if (!(raw->flags & RAW3215_WORKING)) {
631 raw3215_mk_write_req(raw);
632 /* start or queue request */
633 raw3215_try_io(raw);
634 }
635 s390irq_spin_unlock_irqrestore(raw->irq, flags);
636 }
637
638 return ret;
639 }
640
641 /*
642 * Put character routine for 3215 devices
643 */
644
645 static void raw3215_putchar(raw3215_info *raw, unsigned char ch)
646 {
647 unsigned long flags;
648 unsigned int length, i;
649
650 s390irq_spin_lock_irqsave(raw->irq, flags);
651 if (ch == '\t') {
652 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
653 raw->line_pos += length;
654 ch = ' ';
655 } else if (ch == '\n') {
656 length = 1;
657 raw->line_pos = 0;
658 } else {
659 length = 1;
660 raw->line_pos++;
661 }
662 raw3215_make_room(raw, length);
663
664 for (i = 0; i < length; i++) {
665 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
666 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
667 raw->count++;
668 }
669 if (!(raw->flags & RAW3215_WORKING)) {
670 raw3215_mk_write_req(raw);
671 /* start or queue request */
672 raw3215_try_io(raw);
673 }
674 s390irq_spin_unlock_irqrestore(raw->irq, flags);
675 }
676
677 /*
678 * Flush routine, it simply sets the flush flag and tries to start
679 * pending IO.
680 */
681 static void raw3215_flush_buffer(raw3215_info *raw)
682 {
683 unsigned long flags;
684
685 s390irq_spin_lock_irqsave(raw->irq, flags);
686 if (raw->count > 0) {
687 raw->flags |= RAW3215_FLUSHING;
688 raw3215_try_io(raw);
689 raw->flags &= ~RAW3215_FLUSHING;
690 }
691 s390irq_spin_unlock_irqrestore(raw->irq, flags);
692 }
693
694 /*
695 * Fire up a 3215 device.
696 */
697 static int raw3215_startup(raw3215_info *raw)
698 {
699 unsigned long flags;
700
701 if (raw->flags & RAW3215_ACTIVE)
702 return 0;
703 if (request_irq(raw->irq, raw3215_irq, SA_INTERRUPT,
704 "3215 terminal driver", &raw->devstat) != 0)
705 return -1;
706 raw->line_pos = 0;
707 raw->flags |= RAW3215_ACTIVE;
708 s390irq_spin_lock_irqsave(raw->irq, flags);
709 set_cons_dev(raw->irq);
710 raw3215_try_io(raw);
711 s390irq_spin_unlock_irqrestore(raw->irq, flags);
712
713 return 0;
714 }
715
716 /*
717 * Shutdown a 3215 device.
718 */
719 static void raw3215_shutdown(raw3215_info *raw)
720 {
721 DECLARE_WAITQUEUE(wait, current);
722 unsigned long flags;
723
724 if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
725 return;
726 /* Wait for outstanding requests, then free irq */
727 s390irq_spin_lock_irqsave(raw->irq, flags);
728 if ((raw->flags & RAW3215_WORKING) ||
729 raw->queued_write != NULL ||
730 raw->queued_read != NULL) {
731 raw->flags |= RAW3215_CLOSING;
732 add_wait_queue(&raw->empty_wait, &wait);
733 current->state = TASK_INTERRUPTIBLE;
734 s390irq_spin_unlock_irqrestore(raw->irq, flags);
735 schedule();
736 s390irq_spin_lock_irqsave(raw->irq, flags);
737 remove_wait_queue(&raw->empty_wait, &wait);
738 current->state = TASK_RUNNING;
739 raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
740 }
741 free_irq(raw->irq, NULL);
742 s390irq_spin_unlock_irqrestore(raw->irq, flags);
743 }
744
745 static int
746 raw3215_find_dev(int number)
747 {
748 s390_dev_info_t dinfo;
749 int irq;
750 int count;
751
752 irq = get_irq_first();
753 count = 0;
754 while (count <= number && irq != -ENODEV) {
755 if (get_dev_info(irq, &dinfo) == -ENODEV)
756 break;
757 if (dinfo.devno == console_device ||
758 dinfo.sid_data.cu_type == 0x3215) {
759 count++;
760 if (count > number)
761 return irq;
762 }
763 irq = get_irq_next(irq);
764 }
765 return -1; /* console not found */
766 }
767
768 #ifdef CONFIG_TN3215_CONSOLE
769
770 /*
771 * Write a string to the 3215 console
772 */
773 static void
774 con3215_write(struct console *co, const char *str, unsigned int count)
775 {
776 raw3215_info *raw;
777 int i;
778
779 if (count <= 0)
780 return;
781 raw = raw3215[0]; /* console 3215 is the first one */
782 while (count > 0) {
783 for (i = 0; i < count; i++)
784 if (str[i] == '\t' || str[i] == '\n')
785 break;
786 raw3215_write(raw, str, 0, i);
787 count -= i;
788 str += i;
789 if (count > 0) {
790 raw3215_putchar(raw, *str);
791 count--;
792 str++;
793 }
794 }
795 }
796
797 kdev_t con3215_device(struct console *c)
798 {
799 return MKDEV(TTY_MAJOR, c->index + 64 );
800 }
801
802 /*
803 * panic() calls console_unblank before the system enters a
804 * disabled, endless loop.
805 */
806 void con3215_unblank(void)
807 {
808 raw3215_info *raw;
809 unsigned long flags;
810
811 raw = raw3215[0]; /* console 3215 is the first one */
812 s390irq_spin_lock_irqsave(raw->irq, flags);
813 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
814 s390irq_spin_unlock_irqrestore(raw->irq, flags);
815 }
816
817 static int __init con3215_consetup(struct console *co, char *options)
818 {
819 return 0;
820 }
821
822 /*
823 * The console structure for the 3215 console
824 */
825 static struct console con3215 = {
826 name: "tty3215",
827 write: con3215_write,
828 device: con3215_device,
829 unblank: con3215_unblank,
830 setup: con3215_consetup,
831 flags: CON_PRINTBUFFER,
832 };
833
834 #endif
835
836 /*
837 * tty3215_open
838 *
839 * This routine is called whenever a 3215 tty is opened.
840 */
841 static int tty3215_open(struct tty_struct *tty, struct file * filp)
842 {
843 raw3215_info *raw;
844 int retval, line;
845
846 line = MINOR(tty->device) - tty->driver.minor_start;
847 if ((line < 0) || (line >= NR_3215))
848 return -ENODEV;
849
850 raw = raw3215[line];
851 if (raw == NULL) {
852 raw = kmalloc(sizeof(raw3215_info) +
853 RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
854 if (raw == NULL)
855 return -ENOMEM;
856 raw->irq = raw3215_find_dev(line);
857 if (raw->irq == -1) {
858 kfree(raw);
859 return -ENODEV;
860 }
861 raw->inbuf = (char *) raw + sizeof(raw3215_info);
862 memset(raw, 0, sizeof(raw3215_info));
863 raw->buffer = (char *) kmalloc(RAW3215_BUFFER_SIZE,
864 GFP_KERNEL|GFP_DMA);
865 if (raw->buffer == NULL) {
866 kfree(raw);
867 return -ENOMEM;
868 }
869 raw->tqueue.routine = raw3215_softint;
870 raw->tqueue.data = raw;
871 init_waitqueue_head(&raw->empty_wait);
872 raw3215[line] = raw;
873 }
874
875 tty->driver_data = raw;
876 raw->tty = tty;
877
878 tty->low_latency = 0; /* don't use bottom half for pushing chars */
879 /*
880 * Start up 3215 device
881 */
882 retval = raw3215_startup(raw);
883 if (retval)
884 return retval;
885
886 return 0;
887 }
888
889 /*
890 * tty3215_close()
891 *
892 * This routine is called when the 3215 tty is closed. We wait
893 * for the remaining request to be completed. Then we clean up.
894 */
895 static void tty3215_close(struct tty_struct *tty, struct file * filp)
896 {
897 raw3215_info *raw;
898
899 raw = (raw3215_info *) tty->driver_data;
900 if (raw == NULL || tty->count > 1)
901 return;
902 tty->closing = 1;
903 /* Shutdown the terminal */
904 raw3215_shutdown(raw);
905 tty->closing = 0;
906 raw->tty = NULL;
907 }
908
909 /*
910 * Returns the amount of free space in the output buffer.
911 */
912 static int tty3215_write_room(struct tty_struct *tty)
913 {
914 raw3215_info *raw;
915
916 raw = (raw3215_info *) tty->driver_data;
917
918 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
919 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
920 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
921 else
922 return 0;
923 }
924
925 /*
926 * String write routine for 3215 ttys
927 */
928 static int tty3215_write(struct tty_struct * tty, int from_user,
929 const unsigned char *buf, int count)
930 {
931 raw3215_info *raw;
932 int ret = 0;
933
934 if (!tty)
935 return 0;
936 raw = (raw3215_info *) tty->driver_data;
937 ret = raw3215_write(raw, buf, from_user, count);
938 return ret;
939 }
940
941 /*
942 * Put character routine for 3215 ttys
943 */
944 static void tty3215_put_char(struct tty_struct *tty, unsigned char ch)
945 {
946 raw3215_info *raw;
947
948 if (!tty)
949 return;
950 raw = (raw3215_info *) tty->driver_data;
951 raw3215_putchar(raw, ch);
952 }
953
954 static void tty3215_flush_chars(struct tty_struct *tty)
955 {
956 }
957
958 /*
959 * Returns the number of characters in the output buffer
960 */
961 static int tty3215_chars_in_buffer(struct tty_struct *tty)
962 {
963 raw3215_info *raw;
964
965 raw = (raw3215_info *) tty->driver_data;
966 return raw->count;
967 }
968
969 static void tty3215_flush_buffer(struct tty_struct *tty)
970 {
971 raw3215_info *raw;
972
973 raw = (raw3215_info *) tty->driver_data;
974 raw3215_flush_buffer(raw);
975 wake_up_interruptible(&tty->write_wait);
976 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
977 tty->ldisc.write_wakeup)
978 (tty->ldisc.write_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 /*
1124 * Initialize the tty_driver structure
1125 * Entries in tty3215_driver that are NOT initialized:
1126 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1127 */
1128
1129 memset(&tty3215_driver, 0, sizeof(struct tty_driver));
1130 tty3215_driver.magic = TTY_DRIVER_MAGIC;
1131 tty3215_driver.driver_name = "tty3215";
1132 tty3215_driver.name = "ttyS";
1133 tty3215_driver.name_base = 0;
1134 tty3215_driver.major = TTY_MAJOR;
1135 tty3215_driver.minor_start = 64;
1136 tty3215_driver.num = NR_3215;
1137 tty3215_driver.type = TTY_DRIVER_TYPE_SYSTEM;
1138 tty3215_driver.subtype = SYSTEM_TYPE_TTY;
1139 tty3215_driver.init_termios = tty_std_termios;
1140 tty3215_driver.init_termios.c_iflag = IGNBRK | IGNPAR;
1141 tty3215_driver.init_termios.c_oflag = ONLCR | XTABS;
1142 tty3215_driver.init_termios.c_lflag = ISIG;
1143 tty3215_driver.flags = TTY_DRIVER_REAL_RAW;
1144 tty3215_driver.refcount = &tty3215_refcount;
1145 tty3215_driver.table = tty3215_table;
1146 tty3215_driver.termios = tty3215_termios;
1147 tty3215_driver.termios_locked = tty3215_termios_locked;
1148
1149 tty3215_driver.open = tty3215_open;
1150 tty3215_driver.close = tty3215_close;
1151 tty3215_driver.write = tty3215_write;
1152 tty3215_driver.put_char = tty3215_put_char;
1153 tty3215_driver.flush_chars = tty3215_flush_chars;
1154 tty3215_driver.write_room = tty3215_write_room;
1155 tty3215_driver.chars_in_buffer = tty3215_chars_in_buffer;
1156 tty3215_driver.flush_buffer = tty3215_flush_buffer;
1157 tty3215_driver.ioctl = tty3215_ioctl;
1158 tty3215_driver.throttle = tty3215_throttle;
1159 tty3215_driver.unthrottle = tty3215_unthrottle;
1160 tty3215_driver.send_xchar = NULL;
1161 tty3215_driver.set_termios = NULL;
1162 tty3215_driver.stop = tty3215_stop;
1163 tty3215_driver.start = tty3215_start;
1164 tty3215_driver.hangup = NULL;
1165 tty3215_driver.break_ctl = NULL;
1166 tty3215_driver.wait_until_sent = NULL;
1167 tty3215_driver.read_proc = NULL;
1168
1169 if (tty_register_driver(&tty3215_driver))
1170 panic("Couldn't register tty3215 driver\n");
1171 }
1172