2 * SCLP VT220 terminal driver.
4 * Copyright IBM Corp. 2003, 2009
6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
9 #include <linux/module.h>
10 #include <linux/spinlock.h>
11 #include <linux/list.h>
12 #include <linux/wait.h>
13 #include <linux/timer.h>
14 #include <linux/kernel.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/errno.h>
20 #include <linux/major.h>
21 #include <linux/console.h>
22 #include <linux/kdev_t.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/reboot.h>
26 #include <linux/slab.h>
28 #include <asm/uaccess.h>
31 #define SCLP_VT220_MAJOR TTY_MAJOR
32 #define SCLP_VT220_MINOR 65
33 #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
34 #define SCLP_VT220_DEVICE_NAME "ttysclp"
35 #define SCLP_VT220_CONSOLE_NAME "ttyS"
36 #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
38 /* Representation of a single write request */
39 struct sclp_vt220_request {
40 struct list_head list;
41 struct sclp_req sclp_req;
46 struct sclp_vt220_sccb {
47 struct sccb_header header;
48 struct evbuf_header evbuf;
51 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
52 sizeof(struct sclp_vt220_request) - \
53 sizeof(struct sclp_vt220_sccb))
55 /* Structures and data needed to register tty driver */
56 static struct tty_driver *sclp_vt220_driver;
58 static struct tty_port sclp_vt220_port;
60 /* Lock to protect internal data from concurrent access */
61 static spinlock_t sclp_vt220_lock;
63 /* List of empty pages to be used as write request buffers */
64 static struct list_head sclp_vt220_empty;
66 /* List of pending requests */
67 static struct list_head sclp_vt220_outqueue;
69 /* Suspend mode flag */
70 static int sclp_vt220_suspended;
72 /* Flag that output queue is currently running */
73 static int sclp_vt220_queue_running;
75 /* Timer used for delaying write requests to merge subsequent messages into
77 static struct timer_list sclp_vt220_timer;
79 /* Pointer to current request buffer which has been partially filled but not
81 static struct sclp_vt220_request *sclp_vt220_current_request;
83 /* Number of characters in current request buffer */
84 static int sclp_vt220_buffered_chars;
86 /* Counter controlling core driver initialization. */
87 static int __initdata sclp_vt220_init_count;
89 /* Flag indicating that sclp_vt220_current_request should really
90 * have been already queued but wasn't because the SCLP was processing
92 static int sclp_vt220_flush_later;
94 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
95 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
96 enum sclp_pm_event sclp_pm_event);
97 static int __sclp_vt220_emit(struct sclp_vt220_request *request);
98 static void sclp_vt220_emit_current(void);
100 /* Registration structure for our interest in SCLP event buffers */
101 static struct sclp_register sclp_vt220_register = {
102 .send_mask = EVTYP_VT220MSG_MASK,
103 .receive_mask = EVTYP_VT220MSG_MASK,
104 .state_change_fn = NULL,
105 .receiver_fn = sclp_vt220_receiver_fn,
106 .pm_event_fn = sclp_vt220_pm_event_fn,
111 * Put provided request buffer back into queue and check emit pending
112 * buffers if necessary.
115 sclp_vt220_process_queue(struct sclp_vt220_request *request)
121 /* Put buffer back to list of empty buffers */
122 page = request->sclp_req.sccb;
123 spin_lock_irqsave(&sclp_vt220_lock, flags);
124 /* Move request from outqueue to empty queue */
125 list_del(&request->list);
126 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
127 /* Check if there is a pending buffer on the out queue. */
129 if (!list_empty(&sclp_vt220_outqueue))
130 request = list_entry(sclp_vt220_outqueue.next,
131 struct sclp_vt220_request, list);
132 if (!request || sclp_vt220_suspended) {
133 sclp_vt220_queue_running = 0;
134 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
137 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
138 } while (__sclp_vt220_emit(request));
139 if (request == NULL && sclp_vt220_flush_later)
140 sclp_vt220_emit_current();
141 tty_port_tty_wakeup(&sclp_vt220_port);
144 #define SCLP_BUFFER_MAX_RETRY 1
147 * Callback through which the result of a write request is reported by the
151 sclp_vt220_callback(struct sclp_req *request, void *data)
153 struct sclp_vt220_request *vt220_request;
154 struct sclp_vt220_sccb *sccb;
156 vt220_request = (struct sclp_vt220_request *) data;
157 if (request->status == SCLP_REQ_FAILED) {
158 sclp_vt220_process_queue(vt220_request);
161 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
163 /* Check SCLP response code and choose suitable action */
164 switch (sccb->header.response_code) {
168 case 0x05f0: /* Target resource in improper state */
171 case 0x0340: /* Contained SCLP equipment check */
172 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
174 /* Remove processed buffers and requeue rest */
175 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
176 /* Not all buffers were processed */
177 sccb->header.response_code = 0x0000;
178 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
179 if (sclp_add_request(request) == 0)
184 case 0x0040: /* SCLP equipment check */
185 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
187 sccb->header.response_code = 0x0000;
188 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
189 if (sclp_add_request(request) == 0)
196 sclp_vt220_process_queue(vt220_request);
200 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
204 __sclp_vt220_emit(struct sclp_vt220_request *request)
206 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
207 request->sclp_req.status = SCLP_REQ_FAILED;
210 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
211 request->sclp_req.status = SCLP_REQ_FILLED;
212 request->sclp_req.callback = sclp_vt220_callback;
213 request->sclp_req.callback_data = (void *) request;
215 return sclp_add_request(&request->sclp_req);
219 * Queue and emit current request.
222 sclp_vt220_emit_current(void)
225 struct sclp_vt220_request *request;
226 struct sclp_vt220_sccb *sccb;
228 spin_lock_irqsave(&sclp_vt220_lock, flags);
229 if (sclp_vt220_current_request) {
230 sccb = (struct sclp_vt220_sccb *)
231 sclp_vt220_current_request->sclp_req.sccb;
232 /* Only emit buffers with content */
233 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
234 list_add_tail(&sclp_vt220_current_request->list,
235 &sclp_vt220_outqueue);
236 sclp_vt220_current_request = NULL;
237 if (timer_pending(&sclp_vt220_timer))
238 del_timer(&sclp_vt220_timer);
240 sclp_vt220_flush_later = 0;
242 if (sclp_vt220_queue_running || sclp_vt220_suspended)
244 if (list_empty(&sclp_vt220_outqueue))
246 request = list_first_entry(&sclp_vt220_outqueue,
247 struct sclp_vt220_request, list);
248 sclp_vt220_queue_running = 1;
249 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
251 if (__sclp_vt220_emit(request))
252 sclp_vt220_process_queue(request);
255 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
258 #define SCLP_NORMAL_WRITE 0x00
261 * Helper function to initialize a page with the sclp request structure.
263 static struct sclp_vt220_request *
264 sclp_vt220_initialize_page(void *page)
266 struct sclp_vt220_request *request;
267 struct sclp_vt220_sccb *sccb;
269 /* Place request structure at end of page */
270 request = ((struct sclp_vt220_request *)
271 ((addr_t) page + PAGE_SIZE)) - 1;
272 request->retry_count = 0;
273 request->sclp_req.sccb = page;
274 /* SCCB goes at start of page */
275 sccb = (struct sclp_vt220_sccb *) page;
276 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
277 sccb->header.length = sizeof(struct sclp_vt220_sccb);
278 sccb->header.function_code = SCLP_NORMAL_WRITE;
279 sccb->header.response_code = 0x0000;
280 sccb->evbuf.type = EVTYP_VT220MSG;
281 sccb->evbuf.length = sizeof(struct evbuf_header);
286 static inline unsigned int
287 sclp_vt220_space_left(struct sclp_vt220_request *request)
289 struct sclp_vt220_sccb *sccb;
290 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
291 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
295 static inline unsigned int
296 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
298 struct sclp_vt220_sccb *sccb;
299 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
300 return sccb->evbuf.length - sizeof(struct evbuf_header);
304 * Add msg to buffer associated with request. Return the number of characters
308 sclp_vt220_add_msg(struct sclp_vt220_request *request,
309 const unsigned char *msg, int count, int convertlf)
311 struct sclp_vt220_sccb *sccb;
317 if (count > sclp_vt220_space_left(request))
318 count = sclp_vt220_space_left(request);
322 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
323 buffer = (void *) ((addr_t) sccb + sccb->header.length);
326 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
328 (from < count) && (to < sclp_vt220_space_left(request));
330 /* Retrieve character */
332 /* Perform conversion */
334 if (to + 1 < sclp_vt220_space_left(request)) {
335 ((unsigned char *) buffer)[to++] = c;
336 ((unsigned char *) buffer)[to++] = 0x0d;
341 ((unsigned char *) buffer)[to++] = c;
343 sccb->header.length += to;
344 sccb->evbuf.length += to;
347 memcpy(buffer, (const void *) msg, count);
348 sccb->header.length += count;
349 sccb->evbuf.length += count;
355 * Emit buffer after having waited long enough for more data to arrive.
358 sclp_vt220_timeout(unsigned long data)
360 sclp_vt220_emit_current();
363 #define BUFFER_MAX_DELAY HZ/20
366 * Drop oldest console buffer if sclp_con_drop is set
369 sclp_vt220_drop_buffer(void)
371 struct list_head *list;
372 struct sclp_vt220_request *request;
375 if (!sclp_console_drop)
377 list = sclp_vt220_outqueue.next;
378 if (sclp_vt220_queue_running)
379 /* The first element is in I/O */
381 if (list == &sclp_vt220_outqueue)
384 request = list_entry(list, struct sclp_vt220_request, list);
385 page = request->sclp_req.sccb;
386 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
391 * Internal implementation of the write function. Write COUNT bytes of data
393 * to the SCLP interface. In case that the data does not fit into the current
394 * write buffer, emit the current one and allocate a new one. If there are no
395 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
396 * is non-zero, the buffer will be scheduled for emitting after a timeout -
397 * otherwise the user has to explicitly call the flush function.
398 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
399 * buffer should be converted to 0x0a 0x0d. After completion, return the number
403 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
404 int convertlf, int may_fail)
414 spin_lock_irqsave(&sclp_vt220_lock, flags);
416 /* Create an sclp output buffer if none exists yet */
417 if (sclp_vt220_current_request == NULL) {
418 if (list_empty(&sclp_vt220_empty))
420 while (list_empty(&sclp_vt220_empty)) {
421 if (may_fail || sclp_vt220_suspended)
423 if (sclp_vt220_drop_buffer())
425 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
428 spin_lock_irqsave(&sclp_vt220_lock, flags);
430 page = (void *) sclp_vt220_empty.next;
431 list_del((struct list_head *) page);
432 sclp_vt220_current_request =
433 sclp_vt220_initialize_page(page);
435 /* Try to write the string to the current request buffer */
436 written = sclp_vt220_add_msg(sclp_vt220_current_request,
437 buf, count, convertlf);
438 overall_written += written;
439 if (written == count)
442 * Not all characters could be written to the current
443 * output buffer. Emit the buffer, create a new buffer
444 * and then output the rest of the string.
446 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
447 sclp_vt220_emit_current();
448 spin_lock_irqsave(&sclp_vt220_lock, flags);
452 /* Setup timer to output current console buffer after some time */
453 if (sclp_vt220_current_request != NULL &&
454 !timer_pending(&sclp_vt220_timer) && do_schedule) {
455 sclp_vt220_timer.function = sclp_vt220_timeout;
456 sclp_vt220_timer.data = 0UL;
457 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
458 add_timer(&sclp_vt220_timer);
461 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
462 return overall_written;
466 * This routine is called by the kernel to write a series of
467 * characters to the tty device. The characters may come from
468 * user space or kernel space. This routine will return the
469 * number of characters actually accepted for writing.
472 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
474 return __sclp_vt220_write(buf, count, 1, 0, 1);
477 #define SCLP_VT220_SESSION_ENDED 0x01
478 #define SCLP_VT220_SESSION_STARTED 0x80
479 #define SCLP_VT220_SESSION_DATA 0x00
482 * Called by the SCLP to report incoming event buffers.
485 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
490 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
491 count = evbuf->length - sizeof(struct evbuf_header);
494 case SCLP_VT220_SESSION_ENDED:
495 case SCLP_VT220_SESSION_STARTED:
497 case SCLP_VT220_SESSION_DATA:
498 /* Send input to line discipline */
501 tty_insert_flip_string(&sclp_vt220_port, buffer, count);
502 tty_flip_buffer_push(&sclp_vt220_port);
508 * This routine is called when a particular tty device is opened.
511 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
513 if (tty->count == 1) {
514 tty_port_tty_set(&sclp_vt220_port, tty);
515 sclp_vt220_port.low_latency = 0;
516 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
517 tty->winsize.ws_row = 24;
518 tty->winsize.ws_col = 80;
525 * This routine is called when a particular tty device is closed.
528 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
531 tty_port_tty_set(&sclp_vt220_port, NULL);
535 * This routine is called by the kernel to write a single
536 * character to the tty device. If the kernel uses this routine,
537 * it must call the flush_chars() routine (if defined) when it is
538 * done stuffing characters into the driver.
541 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
543 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
547 * This routine is called by the kernel after it has written a
548 * series of characters to the tty device using put_char().
551 sclp_vt220_flush_chars(struct tty_struct *tty)
553 if (!sclp_vt220_queue_running)
554 sclp_vt220_emit_current();
556 sclp_vt220_flush_later = 1;
560 * This routine returns the numbers of characters the tty driver
561 * will accept for queuing to be written. This number is subject
562 * to change as output buffers get emptied, or if the output flow
566 sclp_vt220_write_room(struct tty_struct *tty)
572 spin_lock_irqsave(&sclp_vt220_lock, flags);
574 if (sclp_vt220_current_request != NULL)
575 count = sclp_vt220_space_left(sclp_vt220_current_request);
576 list_for_each(l, &sclp_vt220_empty)
577 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
578 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
583 * Return number of buffered chars.
586 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
590 struct sclp_vt220_request *r;
593 spin_lock_irqsave(&sclp_vt220_lock, flags);
595 if (sclp_vt220_current_request != NULL)
596 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
597 list_for_each(l, &sclp_vt220_outqueue) {
598 r = list_entry(l, struct sclp_vt220_request, list);
599 count += sclp_vt220_chars_stored(r);
601 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
606 * Pass on all buffers to the hardware. Return only when there are no more
610 sclp_vt220_flush_buffer(struct tty_struct *tty)
612 sclp_vt220_emit_current();
615 /* Release allocated pages. */
616 static void __init __sclp_vt220_free_pages(void)
618 struct list_head *page, *p;
620 list_for_each_safe(page, p, &sclp_vt220_empty) {
622 free_page((unsigned long) page);
626 /* Release memory and unregister from sclp core. Controlled by init counting -
627 * only the last invoker will actually perform these actions. */
628 static void __init __sclp_vt220_cleanup(void)
630 sclp_vt220_init_count--;
631 if (sclp_vt220_init_count != 0)
633 sclp_unregister(&sclp_vt220_register);
634 __sclp_vt220_free_pages();
635 tty_port_destroy(&sclp_vt220_port);
638 /* Allocate buffer pages and register with sclp core. Controlled by init
639 * counting - only the first invoker will actually perform these actions. */
640 static int __init __sclp_vt220_init(int num_pages)
646 sclp_vt220_init_count++;
647 if (sclp_vt220_init_count != 1)
649 spin_lock_init(&sclp_vt220_lock);
650 INIT_LIST_HEAD(&sclp_vt220_empty);
651 INIT_LIST_HEAD(&sclp_vt220_outqueue);
652 init_timer(&sclp_vt220_timer);
653 tty_port_init(&sclp_vt220_port);
654 sclp_vt220_current_request = NULL;
655 sclp_vt220_buffered_chars = 0;
656 sclp_vt220_flush_later = 0;
658 /* Allocate pages for output buffering */
660 for (i = 0; i < num_pages; i++) {
661 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
664 list_add_tail(page, &sclp_vt220_empty);
666 rc = sclp_register(&sclp_vt220_register);
669 __sclp_vt220_free_pages();
670 sclp_vt220_init_count--;
671 tty_port_destroy(&sclp_vt220_port);
676 static const struct tty_operations sclp_vt220_ops = {
677 .open = sclp_vt220_open,
678 .close = sclp_vt220_close,
679 .write = sclp_vt220_write,
680 .put_char = sclp_vt220_put_char,
681 .flush_chars = sclp_vt220_flush_chars,
682 .write_room = sclp_vt220_write_room,
683 .chars_in_buffer = sclp_vt220_chars_in_buffer,
684 .flush_buffer = sclp_vt220_flush_buffer,
688 * Register driver with SCLP and Linux and initialize internal tty structures.
690 static int __init sclp_vt220_tty_init(void)
692 struct tty_driver *driver;
695 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
696 * symmetry between VM and LPAR systems regarding ttyS1. */
697 driver = alloc_tty_driver(1);
700 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
704 driver->driver_name = SCLP_VT220_DRIVER_NAME;
705 driver->name = SCLP_VT220_DEVICE_NAME;
706 driver->major = SCLP_VT220_MAJOR;
707 driver->minor_start = SCLP_VT220_MINOR;
708 driver->type = TTY_DRIVER_TYPE_SYSTEM;
709 driver->subtype = SYSTEM_TYPE_TTY;
710 driver->init_termios = tty_std_termios;
711 driver->flags = TTY_DRIVER_REAL_RAW;
712 tty_set_operations(driver, &sclp_vt220_ops);
713 tty_port_link_device(&sclp_vt220_port, driver, 0);
715 rc = tty_register_driver(driver);
718 sclp_vt220_driver = driver;
722 __sclp_vt220_cleanup();
724 put_tty_driver(driver);
727 __initcall(sclp_vt220_tty_init);
729 static void __sclp_vt220_flush_buffer(void)
733 sclp_vt220_emit_current();
734 spin_lock_irqsave(&sclp_vt220_lock, flags);
735 if (timer_pending(&sclp_vt220_timer))
736 del_timer(&sclp_vt220_timer);
737 while (sclp_vt220_queue_running) {
738 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
740 spin_lock_irqsave(&sclp_vt220_lock, flags);
742 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
746 * Resume console: If there are cached messages, emit them.
748 static void sclp_vt220_resume(void)
752 spin_lock_irqsave(&sclp_vt220_lock, flags);
753 sclp_vt220_suspended = 0;
754 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
755 sclp_vt220_emit_current();
759 * Suspend console: Set suspend flag and flush console
761 static void sclp_vt220_suspend(void)
765 spin_lock_irqsave(&sclp_vt220_lock, flags);
766 sclp_vt220_suspended = 1;
767 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
768 __sclp_vt220_flush_buffer();
771 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
772 enum sclp_pm_event sclp_pm_event)
774 switch (sclp_pm_event) {
775 case SCLP_PM_EVENT_FREEZE:
776 sclp_vt220_suspend();
778 case SCLP_PM_EVENT_RESTORE:
779 case SCLP_PM_EVENT_THAW:
785 #ifdef CONFIG_SCLP_VT220_CONSOLE
788 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
790 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
793 static struct tty_driver *
794 sclp_vt220_con_device(struct console *c, int *index)
797 return sclp_vt220_driver;
801 sclp_vt220_notify(struct notifier_block *self,
802 unsigned long event, void *data)
804 __sclp_vt220_flush_buffer();
808 static struct notifier_block on_panic_nb = {
809 .notifier_call = sclp_vt220_notify,
813 static struct notifier_block on_reboot_nb = {
814 .notifier_call = sclp_vt220_notify,
818 /* Structure needed to register with printk */
819 static struct console sclp_vt220_console =
821 .name = SCLP_VT220_CONSOLE_NAME,
822 .write = sclp_vt220_con_write,
823 .device = sclp_vt220_con_device,
824 .flags = CON_PRINTBUFFER,
825 .index = SCLP_VT220_CONSOLE_INDEX
829 sclp_vt220_con_init(void)
833 if (!CONSOLE_IS_SCLP)
835 rc = __sclp_vt220_init(sclp_console_pages);
838 /* Attach linux console */
839 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
840 register_reboot_notifier(&on_reboot_nb);
841 register_console(&sclp_vt220_console);
845 console_initcall(sclp_vt220_con_init);
846 #endif /* CONFIG_SCLP_VT220_CONSOLE */