1 // SPDX-License-Identifier: GPL-2.0
3 * 3215 line mode terminal driver.
5 * Copyright IBM Corp. 1999, 2009
9 * Aug-2000: Added tab support
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/panic_notifier.h>
23 #include <linux/reboot.h>
24 #include <linux/serial.h> /* ASYNC_* flags */
25 #include <linux/slab.h>
26 #include <asm/ccwdev.h>
29 #include <asm/ebcdic.h>
30 #include <linux/uaccess.h>
31 #include <asm/delay.h>
32 #include <asm/cpcmd.h>
33 #include <asm/setup.h>
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 */
48 #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
49 #define RAW3215_WORKING 4 /* set if a request is being worked on */
50 #define RAW3215_THROTTLED 8 /* set if reading is disabled */
51 #define RAW3215_STOPPED 16 /* set if writing is disabled */
52 #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
53 #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
55 #define TAB_STOP_SIZE 8 /* tab stop size */
58 * Request types for a 3215 device
61 RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
65 * Request structure for a 3215 device
68 enum raw3215_type type; /* type of the request */
69 int start, len; /* start index & len in output buffer */
70 int delayable; /* indication to wait for more data */
71 int residual; /* residual count for read request */
72 struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
73 struct raw3215_info *info; /* pointer to main structure */
74 struct raw3215_req *next; /* pointer to next request */
75 } __attribute__ ((aligned(8)));
79 struct ccw_device *cdev; /* device for tty driver */
80 spinlock_t *lock; /* pointer to irq lock */
81 int flags; /* state flags */
82 char *buffer; /* pointer to output buffer */
83 char *inbuf; /* pointer to input buffer */
84 int head; /* first free byte in output buffer */
85 int count; /* number of bytes in output buffer */
86 int written; /* number of bytes in write requests */
87 struct raw3215_req *queued_read; /* pointer to queued read requests */
88 struct raw3215_req *queued_write;/* pointer to queued write requests */
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 DEFINE_SPINLOCK(raw3215_freelist_lock);
104 static struct tty_driver *tty3215_driver;
105 static bool con3215_drop = true;
108 * Get a request structure from the free list
110 static inline struct raw3215_req *raw3215_alloc_req(void)
112 struct raw3215_req *req;
115 spin_lock_irqsave(&raw3215_freelist_lock, flags);
116 req = raw3215_freelist;
117 raw3215_freelist = req->next;
118 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
123 * Put a request structure back to the free list
125 static inline void raw3215_free_req(struct raw3215_req *req)
129 if (req->type == RAW3215_FREE)
130 return; /* don't free a free request */
131 req->type = RAW3215_FREE;
132 spin_lock_irqsave(&raw3215_freelist_lock, flags);
133 req->next = raw3215_freelist;
134 raw3215_freelist = req;
135 spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
139 * Set up a read request that reads up to 160 byte from the 3215 device.
140 * If there is a queued read request it is used, but that shouldn't happen
141 * because a 3215 terminal won't accept a new read before the old one is
144 static void raw3215_mk_read_req(struct raw3215_info *raw)
146 struct raw3215_req *req;
149 /* there can only be ONE read request at a time */
150 req = raw->queued_read;
152 /* no queued read request, use new req structure */
153 req = raw3215_alloc_req();
154 req->type = RAW3215_READ;
156 raw->queued_read = req;
160 ccw->cmd_code = 0x0A; /* read inquiry */
161 ccw->flags = 0x20; /* ignore incorrect length */
163 ccw->cda = (__u32)__pa(raw->inbuf);
167 * Set up a write request with the information from the main structure.
168 * A ccw chain is created that writes as much as possible from the output
169 * buffer to the 3215 device. If a queued write exists it is replaced by
170 * the new, probably lengthened request.
172 static void raw3215_mk_write_req(struct raw3215_info *raw)
174 struct raw3215_req *req;
176 int len, count, ix, lines;
178 if (raw->count <= raw->written)
180 /* check if there is a queued write request */
181 req = raw->queued_write;
183 /* no queued write request, use new req structure */
184 req = raw3215_alloc_req();
185 req->type = RAW3215_WRITE;
187 raw->queued_write = req;
189 raw->written -= req->len;
193 req->start = (raw->head - raw->count + raw->written) &
194 (RAW3215_BUFFER_SIZE - 1);
196 * now we have to count newlines. We can at max accept
197 * RAW3215_MAX_NEWLINE newlines in a single ssch due to
198 * a restriction in VM
202 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
203 if (raw->buffer[ix] == 0x15)
205 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
207 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
208 if (len > RAW3215_MAX_BYTES)
209 len = RAW3215_MAX_BYTES;
213 /* set the indication if we should try to enlarge this request */
214 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
219 ccw[-1].flags |= 0x40; /* use command chaining */
220 ccw->cmd_code = 0x01; /* write, auto carrier return */
221 ccw->flags = 0x20; /* ignore incorrect length ind. */
222 ccw->cda = (__u32)__pa(raw->buffer + ix);
224 if (ix + count > RAW3215_BUFFER_SIZE)
225 count = RAW3215_BUFFER_SIZE - ix;
228 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
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.
237 ccw[-1].flags |= 0x40; /* use command chaining */
238 ccw->cmd_code = 0x03; /* NOP */
245 * Start a read or a write request
247 static void raw3215_start_io(struct raw3215_info *raw)
249 struct raw3215_req *req;
252 req = raw->queued_read;
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);
260 /* do_IO failed, put request back to queue */
261 raw->queued_read = req;
263 raw->flags |= RAW3215_WORKING;
266 req = raw->queued_write;
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);
274 /* do_IO failed, put request back to queue */
275 raw->queued_write = req;
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);
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))
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 * Try to start the next IO and wake up processes waiting on the tty.
335 static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
337 raw3215_mk_write_req(raw);
339 if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
344 * Interrupt routine, called from common io layer
346 static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
349 struct raw3215_info *raw;
350 struct raw3215_req *req;
351 struct tty_struct *tty;
355 raw = dev_get_drvdata(&cdev->dev);
356 req = (struct raw3215_req *) intparm;
357 tty = tty_port_tty_get(&raw->port);
358 cstat = irb->scsw.cmd.cstat;
359 dstat = irb->scsw.cmd.dstat;
361 raw3215_next_io(raw, tty);
362 if (dstat & 0x01) { /* we got a unit exception */
363 dstat &= ~0x01; /* we can ignore it */
369 /* Attention interrupt, someone hit the enter key */
370 raw3215_mk_read_req(raw);
371 raw3215_next_io(raw, tty);
375 /* Channel end interrupt. */
376 if ((raw = req->info) == NULL)
377 goto put_tty; /* That shouldn't happen ... */
378 if (req->type == RAW3215_READ) {
379 /* store residual count, then wait for device end */
380 req->residual = irb->scsw.cmd.count;
386 /* Device end interrupt. */
387 if ((raw = req->info) == NULL)
388 goto put_tty; /* That shouldn't happen ... */
389 if (req->type == RAW3215_READ && tty != NULL) {
392 count = 160 - req->residual;
393 EBCASC(raw->inbuf, count);
394 cchar = ctrlchar_handle(raw->inbuf, count, tty);
395 switch (cchar & CTRLCHAR_MASK) {
400 tty_insert_flip_char(&raw->port, cchar,
402 tty_flip_buffer_push(&raw->port);
407 (strncmp(raw->inbuf+count-2, "\252n", 2) &&
408 strncmp(raw->inbuf+count-2, "^n", 2)) ) {
409 /* add the auto \n */
410 raw->inbuf[count] = '\n';
414 tty_insert_flip_string(&raw->port, raw->inbuf,
416 tty_flip_buffer_push(&raw->port);
419 } else if (req->type == RAW3215_WRITE) {
420 raw->count -= req->len;
421 raw->written -= req->len;
423 raw->flags &= ~RAW3215_WORKING;
424 raw3215_free_req(req);
425 /* check for empty wait */
426 if (waitqueue_active(&raw->empty_wait) &&
427 raw->queued_write == NULL &&
428 raw->queued_read == NULL) {
429 wake_up_interruptible(&raw->empty_wait);
431 raw3215_next_io(raw, tty);
434 /* Strange interrupt, I'll do my best to clean up */
435 if (req != NULL && req->type != RAW3215_FREE) {
436 if (req->type == RAW3215_WRITE) {
437 raw->count -= req->len;
438 raw->written -= req->len;
440 raw->flags &= ~RAW3215_WORKING;
441 raw3215_free_req(req);
443 raw3215_next_io(raw, tty);
450 * Need to drop data to avoid blocking. Drop as much data as possible.
451 * This is unqueued part in the buffer and the queued part in the request.
452 * Also adjust the head position to append new data and set count
455 * Return number of bytes available in buffer.
457 static unsigned int raw3215_drop(struct raw3215_info *raw)
459 struct raw3215_req *req;
461 req = raw->queued_write;
463 /* Drop queued data and delete request */
464 raw->written -= req->len;
465 raw3215_free_req(req);
466 raw->queued_write = NULL;
468 raw->head = (raw->head - raw->count + raw->written) &
469 (RAW3215_BUFFER_SIZE - 1);
470 raw->count = raw->written;
472 return RAW3215_BUFFER_SIZE - raw->count;
476 * Wait until length bytes are available int the output buffer.
477 * If drop mode is active and wait condition holds true, start dropping
479 * Has to be called with the s390irq lock held. Can be called
482 static unsigned int raw3215_make_room(struct raw3215_info *raw,
483 unsigned int length, bool drop)
485 while (RAW3215_BUFFER_SIZE - raw->count < length) {
487 return raw3215_drop(raw);
489 /* there might be a request pending */
490 raw->flags |= RAW3215_FLUSHING;
491 raw3215_mk_write_req(raw);
493 raw->flags &= ~RAW3215_FLUSHING;
494 #ifdef CONFIG_TN3215_CONSOLE
495 ccw_device_wait_idle(raw->cdev);
497 /* Enough room freed up ? */
498 if (RAW3215_BUFFER_SIZE - raw->count >= length)
500 /* there might be another cpu waiting for the lock */
501 spin_unlock(get_ccwdev_lock(raw->cdev));
503 spin_lock(get_ccwdev_lock(raw->cdev));
508 #define RAW3215_COUNT 1
509 #define RAW3215_STORE 2
512 * Add text to console buffer. Find tabs in input and calculate size
513 * including tab replacement.
514 * This function operates in 2 different modes, depending on parameter
516 * RAW3215_COUNT: Get the size needed for the input string with
517 * proper tab replacement calculation.
518 * Return value is the number of bytes required to store the
519 * input. However no data is actually stored.
520 * The parameter todrop is not used.
521 * RAW3215_STORE: Add data to the console buffer. The parameter todrop is
522 * valid and contains the number of bytes to be dropped from head of
523 * string without blocking.
524 * Return value is the number of bytes copied.
526 static unsigned int raw3215_addtext(const char *str, unsigned int length,
527 struct raw3215_info *raw, int opmode,
530 unsigned int c, ch, i, blanks, expanded_size = 0;
531 unsigned int column = raw->line_pos;
533 if (opmode == RAW3215_COUNT)
536 for (c = 0; c < length; ++c) {
546 blanks = TAB_STOP_SIZE - (column % TAB_STOP_SIZE);
548 expanded_size += blanks;
557 if (opmode == RAW3215_COUNT)
559 if (todrop && expanded_size < todrop) /* Drop head data */
561 for (i = 0; i < blanks; i++) {
562 raw->buffer[raw->head] = (char)_ascebc[(int)ch];
563 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
566 raw->line_pos = column;
568 return expanded_size - todrop;
572 * String write routine for 3215 devices
574 static void raw3215_write(struct raw3215_info *raw, const char *str,
577 unsigned int count, avail;
580 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
582 count = raw3215_addtext(str, length, raw, RAW3215_COUNT, 0);
584 avail = raw3215_make_room(raw, count, con3215_drop);
586 raw3215_addtext(str, length, raw, RAW3215_STORE,
589 if (!(raw->flags & RAW3215_WORKING)) {
590 raw3215_mk_write_req(raw);
591 /* start or queue request */
594 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
598 * Put character routine for 3215 devices
600 static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
602 raw3215_write(raw, &ch, 1);
606 * Flush routine, it simply sets the flush flag and tries to start
609 static void raw3215_flush_buffer(struct raw3215_info *raw)
613 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
614 if (raw->count > 0) {
615 raw->flags |= RAW3215_FLUSHING;
617 raw->flags &= ~RAW3215_FLUSHING;
619 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
623 * Fire up a 3215 device.
625 static int raw3215_startup(struct raw3215_info *raw)
629 if (tty_port_initialized(&raw->port))
632 tty_port_set_initialized(&raw->port, true);
633 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
635 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
641 * Shutdown a 3215 device.
643 static void raw3215_shutdown(struct raw3215_info *raw)
645 DECLARE_WAITQUEUE(wait, current);
648 if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED))
650 /* Wait for outstanding requests, then free irq */
651 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
652 if ((raw->flags & RAW3215_WORKING) ||
653 raw->queued_write != NULL ||
654 raw->queued_read != NULL) {
655 add_wait_queue(&raw->empty_wait, &wait);
656 set_current_state(TASK_INTERRUPTIBLE);
657 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
659 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
660 remove_wait_queue(&raw->empty_wait, &wait);
661 set_current_state(TASK_RUNNING);
662 tty_port_set_initialized(&raw->port, true);
664 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
667 static struct raw3215_info *raw3215_alloc_info(void)
669 struct raw3215_info *info;
671 info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
675 info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
676 info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
677 if (!info->buffer || !info->inbuf) {
684 timer_setup(&info->timer, raw3215_timeout, 0);
685 init_waitqueue_head(&info->empty_wait);
686 tty_port_init(&info->port);
691 static void raw3215_free_info(struct raw3215_info *raw)
695 tty_port_destroy(&raw->port);
699 static int raw3215_probe(struct ccw_device *cdev)
701 struct raw3215_info *raw;
704 /* Console is special. */
705 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
708 raw = raw3215_alloc_info();
713 dev_set_drvdata(&cdev->dev, raw);
714 cdev->handler = raw3215_irq;
716 spin_lock(&raw3215_device_lock);
717 for (line = 0; line < NR_3215; line++) {
718 if (!raw3215[line]) {
723 spin_unlock(&raw3215_device_lock);
724 if (line == NR_3215) {
725 raw3215_free_info(raw);
732 static void raw3215_remove(struct ccw_device *cdev)
734 struct raw3215_info *raw;
737 ccw_device_set_offline(cdev);
738 raw = dev_get_drvdata(&cdev->dev);
740 spin_lock(&raw3215_device_lock);
741 for (line = 0; line < NR_3215; line++)
742 if (raw3215[line] == raw)
744 raw3215[line] = NULL;
745 spin_unlock(&raw3215_device_lock);
746 dev_set_drvdata(&cdev->dev, NULL);
747 raw3215_free_info(raw);
751 static int raw3215_set_online(struct ccw_device *cdev)
753 struct raw3215_info *raw;
755 raw = dev_get_drvdata(&cdev->dev);
759 return raw3215_startup(raw);
762 static int raw3215_set_offline(struct ccw_device *cdev)
764 struct raw3215_info *raw;
766 raw = dev_get_drvdata(&cdev->dev);
770 raw3215_shutdown(raw);
775 static struct ccw_device_id raw3215_id[] = {
776 { CCW_DEVICE(0x3215, 0) },
777 { /* end of list */ },
780 static ssize_t con_drop_store(struct device_driver *dev, const char *buf, size_t count)
785 rc = kstrtobool(buf, &drop);
791 static ssize_t con_drop_show(struct device_driver *dev, char *buf)
793 return sysfs_emit(buf, "%d\n", con3215_drop ? 1 : 0);
796 static DRIVER_ATTR_RW(con_drop);
798 static struct attribute *con3215_drv_attrs[] = {
799 &driver_attr_con_drop.attr,
803 static struct attribute_group con3215_drv_attr_group = {
804 .attrs = con3215_drv_attrs,
808 static const struct attribute_group *con3215_drv_attr_groups[] = {
809 &con3215_drv_attr_group,
813 static struct ccw_driver raw3215_ccw_driver = {
816 .groups = con3215_drv_attr_groups,
817 .owner = THIS_MODULE,
820 .probe = &raw3215_probe,
821 .remove = &raw3215_remove,
822 .set_online = &raw3215_set_online,
823 .set_offline = &raw3215_set_offline,
824 .int_class = IRQIO_C15,
827 static void handle_write(struct raw3215_info *raw, const char *str, int count)
832 i = min_t(int, count, RAW3215_BUFFER_SIZE - 1);
833 raw3215_write(raw, str, i);
839 #ifdef CONFIG_TN3215_CONSOLE
841 * Write a string to the 3215 console
843 static void con3215_write(struct console *co, const char *str, unsigned int count)
845 handle_write(raw3215[0], str, count);
848 static struct tty_driver *con3215_device(struct console *c, int *index)
851 return tty3215_driver;
855 * The below function is called as a panic/reboot notifier before the
856 * system enters a disabled, endless loop.
858 * Notice we must use the spin_trylock() alternative, to prevent lockups
859 * in atomic context (panic routine runs with secondary CPUs, local IRQs
860 * and preemption disabled).
862 static int con3215_notify(struct notifier_block *self,
863 unsigned long event, void *data)
865 struct raw3215_info *raw;
868 raw = raw3215[0]; /* console 3215 is the first one */
869 if (!spin_trylock_irqsave(get_ccwdev_lock(raw->cdev), flags))
871 raw3215_make_room(raw, RAW3215_BUFFER_SIZE, false);
872 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
877 static struct notifier_block on_panic_nb = {
878 .notifier_call = con3215_notify,
879 .priority = INT_MIN + 1, /* run the callback late */
882 static struct notifier_block on_reboot_nb = {
883 .notifier_call = con3215_notify,
884 .priority = INT_MIN + 1, /* run the callback late */
888 * The console structure for the 3215 console
890 static struct console con3215 = {
892 .write = con3215_write,
893 .device = con3215_device,
894 .flags = CON_PRINTBUFFER,
898 * 3215 console initialization code called from console_init().
900 static int __init con3215_init(void)
902 struct ccw_device *cdev;
903 struct raw3215_info *raw;
904 struct raw3215_req *req;
907 /* Check if 3215 is to be the console */
908 if (!CONSOLE_IS_3215)
911 /* Set the console mode for VM */
913 cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
914 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
917 /* allocate 3215 request structures */
918 raw3215_freelist = NULL;
919 for (i = 0; i < NR_3215_REQ; i++) {
920 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
923 req->next = raw3215_freelist;
924 raw3215_freelist = req;
927 cdev = ccw_device_create_console(&raw3215_ccw_driver);
931 raw3215[0] = raw = raw3215_alloc_info();
933 dev_set_drvdata(&cdev->dev, raw);
934 cdev->handler = raw3215_irq;
936 raw->flags |= RAW3215_FIXED;
937 if (ccw_device_enable_console(cdev)) {
938 ccw_device_destroy_console(cdev);
939 raw3215_free_info(raw);
944 /* Request the console irq */
945 if (raw3215_startup(raw) != 0) {
946 raw3215_free_info(raw);
950 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
951 register_reboot_notifier(&on_reboot_nb);
952 register_console(&con3215);
955 console_initcall(con3215_init);
958 static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
960 struct raw3215_info *raw;
962 raw = raw3215[tty->index];
966 tty->driver_data = raw;
968 return tty_port_install(&raw->port, driver, tty);
974 * This routine is called whenever a 3215 tty is opened.
976 static int tty3215_open(struct tty_struct *tty, struct file * filp)
978 struct raw3215_info *raw = tty->driver_data;
980 tty_port_tty_set(&raw->port, tty);
983 * Start up 3215 device
985 return raw3215_startup(raw);
991 * This routine is called when the 3215 tty is closed. We wait
992 * for the remaining request to be completed. Then we clean up.
994 static void tty3215_close(struct tty_struct *tty, struct file * filp)
996 struct raw3215_info *raw = tty->driver_data;
998 if (raw == NULL || tty->count > 1)
1001 /* Shutdown the terminal */
1002 raw3215_shutdown(raw);
1004 tty_port_tty_set(&raw->port, NULL);
1008 * Returns the amount of free space in the output buffer.
1010 static unsigned int tty3215_write_room(struct tty_struct *tty)
1012 struct raw3215_info *raw = tty->driver_data;
1014 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
1015 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
1016 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
1022 * String write routine for 3215 ttys
1024 static ssize_t tty3215_write(struct tty_struct *tty, const u8 *buf,
1027 handle_write(tty->driver_data, buf, count);
1032 * Put character routine for 3215 ttys
1034 static int tty3215_put_char(struct tty_struct *tty, u8 ch)
1036 struct raw3215_info *raw = tty->driver_data;
1038 raw3215_putchar(raw, ch);
1043 static void tty3215_flush_chars(struct tty_struct *tty)
1048 * Returns the number of characters in the output buffer
1050 static unsigned int tty3215_chars_in_buffer(struct tty_struct *tty)
1052 struct raw3215_info *raw = tty->driver_data;
1057 static void tty3215_flush_buffer(struct tty_struct *tty)
1059 struct raw3215_info *raw = tty->driver_data;
1061 raw3215_flush_buffer(raw);
1066 * Disable reading from a 3215 tty
1068 static void tty3215_throttle(struct tty_struct *tty)
1070 struct raw3215_info *raw = tty->driver_data;
1072 raw->flags |= RAW3215_THROTTLED;
1076 * Enable reading from a 3215 tty
1078 static void tty3215_unthrottle(struct tty_struct *tty)
1080 struct raw3215_info *raw = tty->driver_data;
1081 unsigned long flags;
1083 if (raw->flags & RAW3215_THROTTLED) {
1084 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1085 raw->flags &= ~RAW3215_THROTTLED;
1086 raw3215_try_io(raw);
1087 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1092 * Disable writing to a 3215 tty
1094 static void tty3215_stop(struct tty_struct *tty)
1096 struct raw3215_info *raw = tty->driver_data;
1098 raw->flags |= RAW3215_STOPPED;
1102 * Enable writing to a 3215 tty
1104 static void tty3215_start(struct tty_struct *tty)
1106 struct raw3215_info *raw = tty->driver_data;
1107 unsigned long flags;
1109 if (raw->flags & RAW3215_STOPPED) {
1110 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1111 raw->flags &= ~RAW3215_STOPPED;
1112 raw3215_try_io(raw);
1113 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1117 static const struct tty_operations tty3215_ops = {
1118 .install = tty3215_install,
1119 .open = tty3215_open,
1120 .close = tty3215_close,
1121 .write = tty3215_write,
1122 .put_char = tty3215_put_char,
1123 .flush_chars = tty3215_flush_chars,
1124 .write_room = tty3215_write_room,
1125 .chars_in_buffer = tty3215_chars_in_buffer,
1126 .flush_buffer = tty3215_flush_buffer,
1127 .throttle = tty3215_throttle,
1128 .unthrottle = tty3215_unthrottle,
1129 .stop = tty3215_stop,
1130 .start = tty3215_start,
1133 static int __init con3215_setup_drop(char *str)
1138 rc = kstrtobool(str, &drop);
1140 con3215_drop = drop;
1143 early_param("con3215_drop", con3215_setup_drop);
1146 * 3215 tty registration code called from tty_init().
1147 * Most kernel services (incl. kmalloc) are available at this poimt.
1149 static int __init tty3215_init(void)
1151 struct tty_driver *driver;
1154 if (!CONSOLE_IS_3215)
1157 driver = tty_alloc_driver(NR_3215, TTY_DRIVER_REAL_RAW);
1159 return PTR_ERR(driver);
1161 ret = ccw_driver_register(&raw3215_ccw_driver);
1163 tty_driver_kref_put(driver);
1167 * Initialize the tty_driver structure
1168 * Entries in tty3215_driver that are NOT initialized:
1169 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1172 driver->driver_name = "tty3215";
1173 driver->name = "ttyS";
1174 driver->major = TTY_MAJOR;
1175 driver->minor_start = 64;
1176 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1177 driver->subtype = SYSTEM_TYPE_TTY;
1178 driver->init_termios = tty_std_termios;
1179 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1180 driver->init_termios.c_oflag = ONLCR;
1181 driver->init_termios.c_lflag = ISIG;
1182 tty_set_operations(driver, &tty3215_ops);
1183 ret = tty_register_driver(driver);
1185 tty_driver_kref_put(driver);
1188 tty3215_driver = driver;
1191 device_initcall(tty3215_init);