Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / s390 / char / con3215.c
blob1354c42d95aa8c64f5af51cf1c613a6b6453b3d7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * 3215 line mode terminal driver.
5 * Copyright IBM Corp. 1999, 2009
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
8 * Updated:
9 * Aug-2000: Added tab support
10 * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
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 #include <linux/serial.h> /* ASYNC_* flags */
24 #include <linux/slab.h>
25 #include <asm/ccwdev.h>
26 #include <asm/cio.h>
27 #include <asm/io.h>
28 #include <asm/ebcdic.h>
29 #include <linux/uaccess.h>
30 #include <asm/delay.h>
31 #include <asm/cpcmd.h>
32 #include <asm/setup.h>
34 #include "ctrlchar.h"
36 #define NR_3215 1
37 #define NR_3215_REQ (4*NR_3215)
38 #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
39 #define RAW3215_INBUF_SIZE 256 /* input buffer size */
40 #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
41 #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
42 #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
43 #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
44 #define RAW3215_NR_CCWS 3
45 #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
47 #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
48 #define RAW3215_WORKING 4 /* set if a request is being worked on */
49 #define RAW3215_THROTTLED 8 /* set if reading is disabled */
50 #define RAW3215_STOPPED 16 /* set if writing is disabled */
51 #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
52 #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
54 #define TAB_STOP_SIZE 8 /* tab stop size */
57 * Request types for a 3215 device
59 enum raw3215_type {
60 RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
64 * Request structure for a 3215 device
66 struct raw3215_req {
67 enum raw3215_type type; /* type of the request */
68 int start, len; /* start index & len in output buffer */
69 int delayable; /* indication to wait for more data */
70 int residual; /* residual count for read request */
71 struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
72 struct raw3215_info *info; /* pointer to main structure */
73 struct raw3215_req *next; /* pointer to next request */
74 } __attribute__ ((aligned(8)));
76 struct raw3215_info {
77 struct tty_port port;
78 struct ccw_device *cdev; /* device for tty driver */
79 spinlock_t *lock; /* pointer to irq lock */
80 int flags; /* state flags */
81 char *buffer; /* pointer to output buffer */
82 char *inbuf; /* pointer to input buffer */
83 int head; /* first free byte in output buffer */
84 int count; /* number of bytes in output buffer */
85 int written; /* number of bytes in write requests */
86 struct raw3215_req *queued_read; /* pointer to queued read requests */
87 struct raw3215_req *queued_write;/* pointer to queued write requests */
88 struct tasklet_struct tlet; /* tasklet to invoke tty_wakeup */
89 wait_queue_head_t empty_wait; /* wait queue for flushing */
90 struct timer_list timer; /* timer for delayed output */
91 int line_pos; /* position on the line (for tabs) */
92 char ubuffer[80]; /* copy_from_user buffer */
95 /* array of 3215 devices structures */
96 static struct raw3215_info *raw3215[NR_3215];
97 /* spinlock to protect the raw3215 array */
98 static DEFINE_SPINLOCK(raw3215_device_lock);
99 /* list of free request structures */
100 static struct raw3215_req *raw3215_freelist;
101 /* spinlock to protect free list */
102 static spinlock_t raw3215_freelist_lock;
104 static struct tty_driver *tty3215_driver;
107 * Get a request structure from the free list
109 static inline struct raw3215_req *raw3215_alloc_req(void)
111 struct raw3215_req *req;
112 unsigned long flags;
114 spin_lock_irqsave(&raw3215_freelist_lock, flags);
115 req = raw3215_freelist;
116 raw3215_freelist = req->next;
117 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
118 return req;
122 * Put a request structure back to the free list
124 static inline void raw3215_free_req(struct raw3215_req *req)
126 unsigned long flags;
128 if (req->type == RAW3215_FREE)
129 return; /* don't free a free request */
130 req->type = RAW3215_FREE;
131 spin_lock_irqsave(&raw3215_freelist_lock, flags);
132 req->next = raw3215_freelist;
133 raw3215_freelist = req;
134 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
138 * Set up a read request that reads up to 160 byte from the 3215 device.
139 * If there is a queued read request it is used, but that shouldn't happen
140 * because a 3215 terminal won't accept a new read before the old one is
141 * completed.
143 static void raw3215_mk_read_req(struct raw3215_info *raw)
145 struct raw3215_req *req;
146 struct ccw1 *ccw;
148 /* there can only be ONE read request at a time */
149 req = raw->queued_read;
150 if (req == NULL) {
151 /* no queued read request, use new req structure */
152 req = raw3215_alloc_req();
153 req->type = RAW3215_READ;
154 req->info = raw;
155 raw->queued_read = req;
158 ccw = req->ccws;
159 ccw->cmd_code = 0x0A; /* read inquiry */
160 ccw->flags = 0x20; /* ignore incorrect length */
161 ccw->count = 160;
162 ccw->cda = (__u32) __pa(raw->inbuf);
166 * Set up a write request with the information from the main structure.
167 * A ccw chain is created that writes as much as possible from the output
168 * buffer to the 3215 device. If a queued write exists it is replaced by
169 * the new, probably lengthened request.
171 static void raw3215_mk_write_req(struct raw3215_info *raw)
173 struct raw3215_req *req;
174 struct ccw1 *ccw;
175 int len, count, ix, lines;
177 if (raw->count <= raw->written)
178 return;
179 /* check if there is a queued write request */
180 req = raw->queued_write;
181 if (req == NULL) {
182 /* no queued write request, use new req structure */
183 req = raw3215_alloc_req();
184 req->type = RAW3215_WRITE;
185 req->info = raw;
186 raw->queued_write = req;
187 } else {
188 raw->written -= req->len;
191 ccw = req->ccws;
192 req->start = (raw->head - raw->count + raw->written) &
193 (RAW3215_BUFFER_SIZE - 1);
195 * now we have to count newlines. We can at max accept
196 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
197 * a restriction in VM
199 lines = 0;
200 ix = req->start;
201 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
202 if (raw->buffer[ix] == 0x15)
203 lines++;
204 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
206 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
207 if (len > RAW3215_MAX_BYTES)
208 len = RAW3215_MAX_BYTES;
209 req->len = len;
210 raw->written += len;
212 /* set the indication if we should try to enlarge this request */
213 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
215 ix = req->start;
216 while (len > 0) {
217 if (ccw > req->ccws)
218 ccw[-1].flags |= 0x40; /* use command chaining */
219 ccw->cmd_code = 0x01; /* write, auto carrier return */
220 ccw->flags = 0x20; /* ignore incorrect length ind. */
221 ccw->cda =
222 (__u32) __pa(raw->buffer + ix);
223 count = len;
224 if (ix + count > RAW3215_BUFFER_SIZE)
225 count = RAW3215_BUFFER_SIZE - ix;
226 ccw->count = count;
227 len -= count;
228 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
229 ccw++;
232 * Add a NOP to the channel program. 3215 devices are purely
233 * emulated and its much better to avoid the channel end
234 * interrupt in this case.
236 if (ccw > req->ccws)
237 ccw[-1].flags |= 0x40; /* use command chaining */
238 ccw->cmd_code = 0x03; /* NOP */
239 ccw->flags = 0;
240 ccw->cda = 0;
241 ccw->count = 1;
245 * Start a read or a write request
247 static void raw3215_start_io(struct raw3215_info *raw)
249 struct raw3215_req *req;
250 int res;
252 req = raw->queued_read;
253 if (req != NULL &&
254 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
255 /* dequeue request */
256 raw->queued_read = NULL;
257 res = ccw_device_start(raw->cdev, req->ccws,
258 (unsigned long) req, 0, 0);
259 if (res != 0) {
260 /* do_IO failed, put request back to queue */
261 raw->queued_read = req;
262 } else {
263 raw->flags |= RAW3215_WORKING;
266 req = raw->queued_write;
267 if (req != NULL &&
268 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
269 /* dequeue request */
270 raw->queued_write = NULL;
271 res = ccw_device_start(raw->cdev, req->ccws,
272 (unsigned long) req, 0, 0);
273 if (res != 0) {
274 /* do_IO failed, put request back to queue */
275 raw->queued_write = req;
276 } else {
277 raw->flags |= RAW3215_WORKING;
283 * Function to start a delayed output after RAW3215_TIMEOUT seconds
285 static void raw3215_timeout(struct timer_list *t)
287 struct raw3215_info *raw = from_timer(raw, t, timer);
288 unsigned long flags;
290 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
291 raw->flags &= ~RAW3215_TIMER_RUNS;
292 raw3215_mk_write_req(raw);
293 raw3215_start_io(raw);
294 if ((raw->queued_read || raw->queued_write) &&
295 !(raw->flags & RAW3215_WORKING) &&
296 !(raw->flags & RAW3215_TIMER_RUNS)) {
297 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
298 add_timer(&raw->timer);
299 raw->flags |= RAW3215_TIMER_RUNS;
301 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
305 * Function to conditionally start an IO. A read is started immediately,
306 * a write is only started immediately if the flush flag is on or the
307 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
308 * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
310 static inline void raw3215_try_io(struct raw3215_info *raw)
312 if (!tty_port_initialized(&raw->port))
313 return;
314 if (raw->queued_read != NULL)
315 raw3215_start_io(raw);
316 else if (raw->queued_write != NULL) {
317 if ((raw->queued_write->delayable == 0) ||
318 (raw->flags & RAW3215_FLUSHING)) {
319 /* execute write requests bigger than minimum size */
320 raw3215_start_io(raw);
323 if ((raw->queued_read || raw->queued_write) &&
324 !(raw->flags & RAW3215_WORKING) &&
325 !(raw->flags & RAW3215_TIMER_RUNS)) {
326 raw->timer.expires = RAW3215_TIMEOUT + jiffies;
327 add_timer(&raw->timer);
328 raw->flags |= RAW3215_TIMER_RUNS;
333 * Call tty_wakeup from tasklet context
335 static void raw3215_wakeup(unsigned long data)
337 struct raw3215_info *raw = (struct raw3215_info *) data;
338 struct tty_struct *tty;
340 tty = tty_port_tty_get(&raw->port);
341 if (tty) {
342 tty_wakeup(tty);
343 tty_kref_put(tty);
348 * Try to start the next IO and wake up processes waiting on the tty.
350 static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
352 raw3215_mk_write_req(raw);
353 raw3215_try_io(raw);
354 if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
355 tasklet_schedule(&raw->tlet);
359 * Interrupt routine, called from common io layer
361 static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
362 struct irb *irb)
364 struct raw3215_info *raw;
365 struct raw3215_req *req;
366 struct tty_struct *tty;
367 int cstat, dstat;
368 int count;
370 raw = dev_get_drvdata(&cdev->dev);
371 req = (struct raw3215_req *) intparm;
372 tty = tty_port_tty_get(&raw->port);
373 cstat = irb->scsw.cmd.cstat;
374 dstat = irb->scsw.cmd.dstat;
375 if (cstat != 0)
376 raw3215_next_io(raw, tty);
377 if (dstat & 0x01) { /* we got a unit exception */
378 dstat &= ~0x01; /* we can ignore it */
380 switch (dstat) {
381 case 0x80:
382 if (cstat != 0)
383 break;
384 /* Attention interrupt, someone hit the enter key */
385 raw3215_mk_read_req(raw);
386 raw3215_next_io(raw, tty);
387 break;
388 case 0x08:
389 case 0x0C:
390 /* Channel end interrupt. */
391 if ((raw = req->info) == NULL)
392 goto put_tty; /* That shouldn't happen ... */
393 if (req->type == RAW3215_READ) {
394 /* store residual count, then wait for device end */
395 req->residual = irb->scsw.cmd.count;
397 if (dstat == 0x08)
398 break;
399 fallthrough;
400 case 0x04:
401 /* Device end interrupt. */
402 if ((raw = req->info) == NULL)
403 goto put_tty; /* That shouldn't happen ... */
404 if (req->type == RAW3215_READ && tty != NULL) {
405 unsigned int cchar;
407 count = 160 - req->residual;
408 EBCASC(raw->inbuf, count);
409 cchar = ctrlchar_handle(raw->inbuf, count, tty);
410 switch (cchar & CTRLCHAR_MASK) {
411 case CTRLCHAR_SYSRQ:
412 break;
414 case CTRLCHAR_CTRL:
415 tty_insert_flip_char(&raw->port, cchar,
416 TTY_NORMAL);
417 tty_flip_buffer_push(&raw->port);
418 break;
420 case CTRLCHAR_NONE:
421 if (count < 2 ||
422 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
423 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
424 /* add the auto \n */
425 raw->inbuf[count] = '\n';
426 count++;
427 } else
428 count -= 2;
429 tty_insert_flip_string(&raw->port, raw->inbuf,
430 count);
431 tty_flip_buffer_push(&raw->port);
432 break;
434 } else if (req->type == RAW3215_WRITE) {
435 raw->count -= req->len;
436 raw->written -= req->len;
438 raw->flags &= ~RAW3215_WORKING;
439 raw3215_free_req(req);
440 /* check for empty wait */
441 if (waitqueue_active(&raw->empty_wait) &&
442 raw->queued_write == NULL &&
443 raw->queued_read == NULL) {
444 wake_up_interruptible(&raw->empty_wait);
446 raw3215_next_io(raw, tty);
447 break;
448 default:
449 /* Strange interrupt, I'll do my best to clean up */
450 if (req != NULL && req->type != RAW3215_FREE) {
451 if (req->type == RAW3215_WRITE) {
452 raw->count -= req->len;
453 raw->written -= req->len;
455 raw->flags &= ~RAW3215_WORKING;
456 raw3215_free_req(req);
458 raw3215_next_io(raw, tty);
460 put_tty:
461 tty_kref_put(tty);
465 * Wait until length bytes are available int the output buffer.
466 * Has to be called with the s390irq lock held. Can be called
467 * disabled.
469 static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
471 while (RAW3215_BUFFER_SIZE - raw->count < length) {
472 /* there might be a request pending */
473 raw->flags |= RAW3215_FLUSHING;
474 raw3215_mk_write_req(raw);
475 raw3215_try_io(raw);
476 raw->flags &= ~RAW3215_FLUSHING;
477 #ifdef CONFIG_TN3215_CONSOLE
478 ccw_device_wait_idle(raw->cdev);
479 #endif
480 /* Enough room freed up ? */
481 if (RAW3215_BUFFER_SIZE - raw->count >= length)
482 break;
483 /* there might be another cpu waiting for the lock */
484 spin_unlock(get_ccwdev_lock(raw->cdev));
485 udelay(100);
486 spin_lock(get_ccwdev_lock(raw->cdev));
491 * String write routine for 3215 devices
493 static void raw3215_write(struct raw3215_info *raw, const char *str,
494 unsigned int length)
496 unsigned long flags;
497 int c, count;
499 while (length > 0) {
500 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
501 count = (length > RAW3215_BUFFER_SIZE) ?
502 RAW3215_BUFFER_SIZE : length;
503 length -= count;
505 raw3215_make_room(raw, count);
507 /* copy string to output buffer and convert it to EBCDIC */
508 while (1) {
509 c = min_t(int, count,
510 min(RAW3215_BUFFER_SIZE - raw->count,
511 RAW3215_BUFFER_SIZE - raw->head));
512 if (c <= 0)
513 break;
514 memcpy(raw->buffer + raw->head, str, c);
515 ASCEBC(raw->buffer + raw->head, c);
516 raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
517 raw->count += c;
518 raw->line_pos += c;
519 str += c;
520 count -= c;
522 if (!(raw->flags & RAW3215_WORKING)) {
523 raw3215_mk_write_req(raw);
524 /* start or queue request */
525 raw3215_try_io(raw);
527 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
532 * Put character routine for 3215 devices
534 static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
536 unsigned long flags;
537 unsigned int length, i;
539 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
540 if (ch == '\t') {
541 length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
542 raw->line_pos += length;
543 ch = ' ';
544 } else if (ch == '\n') {
545 length = 1;
546 raw->line_pos = 0;
547 } else {
548 length = 1;
549 raw->line_pos++;
551 raw3215_make_room(raw, length);
553 for (i = 0; i < length; i++) {
554 raw->buffer[raw->head] = (char) _ascebc[(int) ch];
555 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
556 raw->count++;
558 if (!(raw->flags & RAW3215_WORKING)) {
559 raw3215_mk_write_req(raw);
560 /* start or queue request */
561 raw3215_try_io(raw);
563 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
567 * Flush routine, it simply sets the flush flag and tries to start
568 * pending IO.
570 static void raw3215_flush_buffer(struct raw3215_info *raw)
572 unsigned long flags;
574 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
575 if (raw->count > 0) {
576 raw->flags |= RAW3215_FLUSHING;
577 raw3215_try_io(raw);
578 raw->flags &= ~RAW3215_FLUSHING;
580 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
584 * Fire up a 3215 device.
586 static int raw3215_startup(struct raw3215_info *raw)
588 unsigned long flags;
590 if (tty_port_initialized(&raw->port))
591 return 0;
592 raw->line_pos = 0;
593 tty_port_set_initialized(&raw->port, 1);
594 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
595 raw3215_try_io(raw);
596 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
598 return 0;
602 * Shutdown a 3215 device.
604 static void raw3215_shutdown(struct raw3215_info *raw)
606 DECLARE_WAITQUEUE(wait, current);
607 unsigned long flags;
609 if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED))
610 return;
611 /* Wait for outstanding requests, then free irq */
612 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
613 if ((raw->flags & RAW3215_WORKING) ||
614 raw->queued_write != NULL ||
615 raw->queued_read != NULL) {
616 add_wait_queue(&raw->empty_wait, &wait);
617 set_current_state(TASK_INTERRUPTIBLE);
618 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
619 schedule();
620 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
621 remove_wait_queue(&raw->empty_wait, &wait);
622 set_current_state(TASK_RUNNING);
623 tty_port_set_initialized(&raw->port, 1);
625 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
628 static struct raw3215_info *raw3215_alloc_info(void)
630 struct raw3215_info *info;
632 info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
633 if (!info)
634 return NULL;
636 info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
637 info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
638 if (!info->buffer || !info->inbuf) {
639 kfree(info->inbuf);
640 kfree(info->buffer);
641 kfree(info);
642 return NULL;
645 timer_setup(&info->timer, raw3215_timeout, 0);
646 init_waitqueue_head(&info->empty_wait);
647 tasklet_init(&info->tlet, raw3215_wakeup, (unsigned long)info);
648 tty_port_init(&info->port);
650 return info;
653 static void raw3215_free_info(struct raw3215_info *raw)
655 kfree(raw->inbuf);
656 kfree(raw->buffer);
657 tty_port_destroy(&raw->port);
658 kfree(raw);
661 static int raw3215_probe (struct ccw_device *cdev)
663 struct raw3215_info *raw;
664 int line;
666 /* Console is special. */
667 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
668 return 0;
670 raw = raw3215_alloc_info();
671 if (raw == NULL)
672 return -ENOMEM;
674 raw->cdev = cdev;
675 dev_set_drvdata(&cdev->dev, raw);
676 cdev->handler = raw3215_irq;
678 spin_lock(&raw3215_device_lock);
679 for (line = 0; line < NR_3215; line++) {
680 if (!raw3215[line]) {
681 raw3215[line] = raw;
682 break;
685 spin_unlock(&raw3215_device_lock);
686 if (line == NR_3215) {
687 raw3215_free_info(raw);
688 return -ENODEV;
691 return 0;
694 static void raw3215_remove (struct ccw_device *cdev)
696 struct raw3215_info *raw;
697 unsigned int line;
699 ccw_device_set_offline(cdev);
700 raw = dev_get_drvdata(&cdev->dev);
701 if (raw) {
702 spin_lock(&raw3215_device_lock);
703 for (line = 0; line < NR_3215; line++)
704 if (raw3215[line] == raw)
705 break;
706 raw3215[line] = NULL;
707 spin_unlock(&raw3215_device_lock);
708 dev_set_drvdata(&cdev->dev, NULL);
709 raw3215_free_info(raw);
713 static int raw3215_set_online (struct ccw_device *cdev)
715 struct raw3215_info *raw;
717 raw = dev_get_drvdata(&cdev->dev);
718 if (!raw)
719 return -ENODEV;
721 return raw3215_startup(raw);
724 static int raw3215_set_offline (struct ccw_device *cdev)
726 struct raw3215_info *raw;
728 raw = dev_get_drvdata(&cdev->dev);
729 if (!raw)
730 return -ENODEV;
732 raw3215_shutdown(raw);
734 return 0;
737 static struct ccw_device_id raw3215_id[] = {
738 { CCW_DEVICE(0x3215, 0) },
739 { /* end of list */ },
742 static struct ccw_driver raw3215_ccw_driver = {
743 .driver = {
744 .name = "3215",
745 .owner = THIS_MODULE,
747 .ids = raw3215_id,
748 .probe = &raw3215_probe,
749 .remove = &raw3215_remove,
750 .set_online = &raw3215_set_online,
751 .set_offline = &raw3215_set_offline,
752 .int_class = IRQIO_C15,
755 #ifdef CONFIG_TN3215_CONSOLE
757 * Write a string to the 3215 console
759 static void con3215_write(struct console *co, const char *str,
760 unsigned int count)
762 struct raw3215_info *raw;
763 int i;
765 if (count <= 0)
766 return;
767 raw = raw3215[0]; /* console 3215 is the first one */
768 while (count > 0) {
769 for (i = 0; i < count; i++)
770 if (str[i] == '\t' || str[i] == '\n')
771 break;
772 raw3215_write(raw, str, i);
773 count -= i;
774 str += i;
775 if (count > 0) {
776 raw3215_putchar(raw, *str);
777 count--;
778 str++;
783 static struct tty_driver *con3215_device(struct console *c, int *index)
785 *index = c->index;
786 return tty3215_driver;
790 * panic() calls con3215_flush through a panic_notifier
791 * before the system enters a disabled, endless loop.
793 static void con3215_flush(void)
795 struct raw3215_info *raw;
796 unsigned long flags;
798 raw = raw3215[0]; /* console 3215 is the first one */
799 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
800 raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
801 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
804 static int con3215_notify(struct notifier_block *self,
805 unsigned long event, void *data)
807 con3215_flush();
808 return NOTIFY_OK;
811 static struct notifier_block on_panic_nb = {
812 .notifier_call = con3215_notify,
813 .priority = 0,
816 static struct notifier_block on_reboot_nb = {
817 .notifier_call = con3215_notify,
818 .priority = 0,
822 * The console structure for the 3215 console
824 static struct console con3215 = {
825 .name = "ttyS",
826 .write = con3215_write,
827 .device = con3215_device,
828 .flags = CON_PRINTBUFFER,
832 * 3215 console initialization code called from console_init().
834 static int __init con3215_init(void)
836 struct ccw_device *cdev;
837 struct raw3215_info *raw;
838 struct raw3215_req *req;
839 int i;
841 /* Check if 3215 is to be the console */
842 if (!CONSOLE_IS_3215)
843 return -ENODEV;
845 /* Set the console mode for VM */
846 if (MACHINE_IS_VM) {
847 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
848 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
851 /* allocate 3215 request structures */
852 raw3215_freelist = NULL;
853 spin_lock_init(&raw3215_freelist_lock);
854 for (i = 0; i < NR_3215_REQ; i++) {
855 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
856 if (!req)
857 return -ENOMEM;
858 req->next = raw3215_freelist;
859 raw3215_freelist = req;
862 cdev = ccw_device_create_console(&raw3215_ccw_driver);
863 if (IS_ERR(cdev))
864 return -ENODEV;
866 raw3215[0] = raw = raw3215_alloc_info();
867 raw->cdev = cdev;
868 dev_set_drvdata(&cdev->dev, raw);
869 cdev->handler = raw3215_irq;
871 raw->flags |= RAW3215_FIXED;
872 if (ccw_device_enable_console(cdev)) {
873 ccw_device_destroy_console(cdev);
874 raw3215_free_info(raw);
875 raw3215[0] = NULL;
876 return -ENODEV;
879 /* Request the console irq */
880 if (raw3215_startup(raw) != 0) {
881 raw3215_free_info(raw);
882 raw3215[0] = NULL;
883 return -ENODEV;
885 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
886 register_reboot_notifier(&on_reboot_nb);
887 register_console(&con3215);
888 return 0;
890 console_initcall(con3215_init);
891 #endif
893 static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
895 struct raw3215_info *raw;
897 raw = raw3215[tty->index];
898 if (raw == NULL)
899 return -ENODEV;
901 tty->driver_data = raw;
903 return tty_port_install(&raw->port, driver, tty);
907 * tty3215_open
909 * This routine is called whenever a 3215 tty is opened.
911 static int tty3215_open(struct tty_struct *tty, struct file * filp)
913 struct raw3215_info *raw = tty->driver_data;
915 tty_port_tty_set(&raw->port, tty);
917 raw->port.low_latency = 0; /* don't use bottom half for pushing chars */
919 * Start up 3215 device
921 return raw3215_startup(raw);
925 * tty3215_close()
927 * This routine is called when the 3215 tty is closed. We wait
928 * for the remaining request to be completed. Then we clean up.
930 static void tty3215_close(struct tty_struct *tty, struct file * filp)
932 struct raw3215_info *raw;
934 raw = (struct raw3215_info *) tty->driver_data;
935 if (raw == NULL || tty->count > 1)
936 return;
937 tty->closing = 1;
938 /* Shutdown the terminal */
939 raw3215_shutdown(raw);
940 tasklet_kill(&raw->tlet);
941 tty->closing = 0;
942 tty_port_tty_set(&raw->port, NULL);
946 * Returns the amount of free space in the output buffer.
948 static int tty3215_write_room(struct tty_struct *tty)
950 struct raw3215_info *raw;
952 raw = (struct raw3215_info *) tty->driver_data;
954 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
955 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
956 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
957 else
958 return 0;
962 * String write routine for 3215 ttys
964 static int tty3215_write(struct tty_struct * tty,
965 const unsigned char *buf, int count)
967 struct raw3215_info *raw;
968 int i, written;
970 if (!tty)
971 return 0;
972 raw = (struct raw3215_info *) tty->driver_data;
973 written = count;
974 while (count > 0) {
975 for (i = 0; i < count; i++)
976 if (buf[i] == '\t' || buf[i] == '\n')
977 break;
978 raw3215_write(raw, buf, i);
979 count -= i;
980 buf += i;
981 if (count > 0) {
982 raw3215_putchar(raw, *buf);
983 count--;
984 buf++;
987 return written;
991 * Put character routine for 3215 ttys
993 static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
995 struct raw3215_info *raw;
997 if (!tty)
998 return 0;
999 raw = (struct raw3215_info *) tty->driver_data;
1000 raw3215_putchar(raw, ch);
1001 return 1;
1004 static void tty3215_flush_chars(struct tty_struct *tty)
1009 * Returns the number of characters in the output buffer
1011 static int tty3215_chars_in_buffer(struct tty_struct *tty)
1013 struct raw3215_info *raw;
1015 raw = (struct raw3215_info *) tty->driver_data;
1016 return raw->count;
1019 static void tty3215_flush_buffer(struct tty_struct *tty)
1021 struct raw3215_info *raw;
1023 raw = (struct raw3215_info *) tty->driver_data;
1024 raw3215_flush_buffer(raw);
1025 tty_wakeup(tty);
1029 * Disable reading from a 3215 tty
1031 static void tty3215_throttle(struct tty_struct * tty)
1033 struct raw3215_info *raw;
1035 raw = (struct raw3215_info *) tty->driver_data;
1036 raw->flags |= RAW3215_THROTTLED;
1040 * Enable reading from a 3215 tty
1042 static void tty3215_unthrottle(struct tty_struct * tty)
1044 struct raw3215_info *raw;
1045 unsigned long flags;
1047 raw = (struct raw3215_info *) tty->driver_data;
1048 if (raw->flags & RAW3215_THROTTLED) {
1049 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1050 raw->flags &= ~RAW3215_THROTTLED;
1051 raw3215_try_io(raw);
1052 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1057 * Disable writing to a 3215 tty
1059 static void tty3215_stop(struct tty_struct *tty)
1061 struct raw3215_info *raw;
1063 raw = (struct raw3215_info *) tty->driver_data;
1064 raw->flags |= RAW3215_STOPPED;
1068 * Enable writing to a 3215 tty
1070 static void tty3215_start(struct tty_struct *tty)
1072 struct raw3215_info *raw;
1073 unsigned long flags;
1075 raw = (struct raw3215_info *) tty->driver_data;
1076 if (raw->flags & RAW3215_STOPPED) {
1077 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1078 raw->flags &= ~RAW3215_STOPPED;
1079 raw3215_try_io(raw);
1080 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1084 static const struct tty_operations tty3215_ops = {
1085 .install = tty3215_install,
1086 .open = tty3215_open,
1087 .close = tty3215_close,
1088 .write = tty3215_write,
1089 .put_char = tty3215_put_char,
1090 .flush_chars = tty3215_flush_chars,
1091 .write_room = tty3215_write_room,
1092 .chars_in_buffer = tty3215_chars_in_buffer,
1093 .flush_buffer = tty3215_flush_buffer,
1094 .throttle = tty3215_throttle,
1095 .unthrottle = tty3215_unthrottle,
1096 .stop = tty3215_stop,
1097 .start = tty3215_start,
1101 * 3215 tty registration code called from tty_init().
1102 * Most kernel services (incl. kmalloc) are available at this poimt.
1104 static int __init tty3215_init(void)
1106 struct tty_driver *driver;
1107 int ret;
1109 if (!CONSOLE_IS_3215)
1110 return 0;
1112 driver = alloc_tty_driver(NR_3215);
1113 if (!driver)
1114 return -ENOMEM;
1116 ret = ccw_driver_register(&raw3215_ccw_driver);
1117 if (ret) {
1118 put_tty_driver(driver);
1119 return ret;
1122 * Initialize the tty_driver structure
1123 * Entries in tty3215_driver that are NOT initialized:
1124 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1127 driver->driver_name = "tty3215";
1128 driver->name = "ttyS";
1129 driver->major = TTY_MAJOR;
1130 driver->minor_start = 64;
1131 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1132 driver->subtype = SYSTEM_TYPE_TTY;
1133 driver->init_termios = tty_std_termios;
1134 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1135 driver->init_termios.c_oflag = ONLCR;
1136 driver->init_termios.c_lflag = ISIG;
1137 driver->flags = TTY_DRIVER_REAL_RAW;
1138 tty_set_operations(driver, &tty3215_ops);
1139 ret = tty_register_driver(driver);
1140 if (ret) {
1141 put_tty_driver(driver);
1142 return ret;
1144 tty3215_driver = driver;
1145 return 0;
1147 device_initcall(tty3215_init);