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>
27 #include <asm/uaccess.h>
30 #define SCLP_VT220_MAJOR TTY_MAJOR
31 #define SCLP_VT220_MINOR 65
32 #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
33 #define SCLP_VT220_DEVICE_NAME "ttysclp"
34 #define SCLP_VT220_CONSOLE_NAME "ttyS"
35 #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
36 #define SCLP_VT220_BUF_SIZE 80
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 /* The tty_struct that the kernel associated with us */
59 static struct tty_struct *sclp_vt220_tty;
61 /* Lock to protect internal data from concurrent access */
62 static spinlock_t sclp_vt220_lock;
64 /* List of empty pages to be used as write request buffers */
65 static struct list_head sclp_vt220_empty;
67 /* List of pending requests */
68 static struct list_head sclp_vt220_outqueue;
70 /* Suspend mode flag */
71 static int sclp_vt220_suspended;
73 /* Flag that output queue is currently running */
74 static int sclp_vt220_queue_running;
76 /* Timer used for delaying write requests to merge subsequent messages into
78 static struct timer_list sclp_vt220_timer;
80 /* Pointer to current request buffer which has been partially filled but not
82 static struct sclp_vt220_request *sclp_vt220_current_request;
84 /* Number of characters in current request buffer */
85 static int sclp_vt220_buffered_chars;
87 /* Counter controlling core driver initialization. */
88 static int __initdata sclp_vt220_init_count;
90 /* Flag indicating that sclp_vt220_current_request should really
91 * have been already queued but wasn't because the SCLP was processing
93 static int sclp_vt220_flush_later;
95 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
96 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
97 enum sclp_pm_event sclp_pm_event);
98 static int __sclp_vt220_emit(struct sclp_vt220_request *request);
99 static void sclp_vt220_emit_current(void);
101 /* Registration structure for our interest in SCLP event buffers */
102 static struct sclp_register sclp_vt220_register = {
103 .send_mask = EVTYP_VT220MSG_MASK,
104 .receive_mask = EVTYP_VT220MSG_MASK,
105 .state_change_fn = NULL,
106 .receiver_fn = sclp_vt220_receiver_fn,
107 .pm_event_fn = sclp_vt220_pm_event_fn,
112 * Put provided request buffer back into queue and check emit pending
113 * buffers if necessary.
116 sclp_vt220_process_queue(struct sclp_vt220_request *request)
122 /* Put buffer back to list of empty buffers */
123 page = request->sclp_req.sccb;
124 spin_lock_irqsave(&sclp_vt220_lock, flags);
125 /* Move request from outqueue to empty queue */
126 list_del(&request->list);
127 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
128 /* Check if there is a pending buffer on the out queue. */
130 if (!list_empty(&sclp_vt220_outqueue))
131 request = list_entry(sclp_vt220_outqueue.next,
132 struct sclp_vt220_request, list);
133 if (!request || sclp_vt220_suspended) {
134 sclp_vt220_queue_running = 0;
135 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
138 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
139 } while (__sclp_vt220_emit(request));
140 if (request == NULL && sclp_vt220_flush_later)
141 sclp_vt220_emit_current();
142 /* Check if the tty needs a wake up call */
143 if (sclp_vt220_tty != NULL) {
144 tty_wakeup(sclp_vt220_tty);
148 #define SCLP_BUFFER_MAX_RETRY 1
151 * Callback through which the result of a write request is reported by the
155 sclp_vt220_callback(struct sclp_req *request, void *data)
157 struct sclp_vt220_request *vt220_request;
158 struct sclp_vt220_sccb *sccb;
160 vt220_request = (struct sclp_vt220_request *) data;
161 if (request->status == SCLP_REQ_FAILED) {
162 sclp_vt220_process_queue(vt220_request);
165 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
167 /* Check SCLP response code and choose suitable action */
168 switch (sccb->header.response_code) {
172 case 0x05f0: /* Target resource in improper state */
175 case 0x0340: /* Contained SCLP equipment check */
176 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
178 /* Remove processed buffers and requeue rest */
179 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
180 /* Not all buffers were processed */
181 sccb->header.response_code = 0x0000;
182 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
183 if (sclp_add_request(request) == 0)
188 case 0x0040: /* SCLP equipment check */
189 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
191 sccb->header.response_code = 0x0000;
192 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
193 if (sclp_add_request(request) == 0)
200 sclp_vt220_process_queue(vt220_request);
204 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
208 __sclp_vt220_emit(struct sclp_vt220_request *request)
210 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
211 request->sclp_req.status = SCLP_REQ_FAILED;
214 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
215 request->sclp_req.status = SCLP_REQ_FILLED;
216 request->sclp_req.callback = sclp_vt220_callback;
217 request->sclp_req.callback_data = (void *) request;
219 return sclp_add_request(&request->sclp_req);
223 * Queue and emit current request.
226 sclp_vt220_emit_current(void)
229 struct sclp_vt220_request *request;
230 struct sclp_vt220_sccb *sccb;
232 spin_lock_irqsave(&sclp_vt220_lock, flags);
233 if (sclp_vt220_current_request) {
234 sccb = (struct sclp_vt220_sccb *)
235 sclp_vt220_current_request->sclp_req.sccb;
236 /* Only emit buffers with content */
237 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
238 list_add_tail(&sclp_vt220_current_request->list,
239 &sclp_vt220_outqueue);
240 sclp_vt220_current_request = NULL;
241 if (timer_pending(&sclp_vt220_timer))
242 del_timer(&sclp_vt220_timer);
244 sclp_vt220_flush_later = 0;
246 if (sclp_vt220_queue_running || sclp_vt220_suspended)
248 if (list_empty(&sclp_vt220_outqueue))
250 request = list_first_entry(&sclp_vt220_outqueue,
251 struct sclp_vt220_request, list);
252 sclp_vt220_queue_running = 1;
253 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
255 if (__sclp_vt220_emit(request))
256 sclp_vt220_process_queue(request);
259 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
262 #define SCLP_NORMAL_WRITE 0x00
265 * Helper function to initialize a page with the sclp request structure.
267 static struct sclp_vt220_request *
268 sclp_vt220_initialize_page(void *page)
270 struct sclp_vt220_request *request;
271 struct sclp_vt220_sccb *sccb;
273 /* Place request structure at end of page */
274 request = ((struct sclp_vt220_request *)
275 ((addr_t) page + PAGE_SIZE)) - 1;
276 request->retry_count = 0;
277 request->sclp_req.sccb = page;
278 /* SCCB goes at start of page */
279 sccb = (struct sclp_vt220_sccb *) page;
280 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
281 sccb->header.length = sizeof(struct sclp_vt220_sccb);
282 sccb->header.function_code = SCLP_NORMAL_WRITE;
283 sccb->header.response_code = 0x0000;
284 sccb->evbuf.type = EVTYP_VT220MSG;
285 sccb->evbuf.length = sizeof(struct evbuf_header);
290 static inline unsigned int
291 sclp_vt220_space_left(struct sclp_vt220_request *request)
293 struct sclp_vt220_sccb *sccb;
294 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
295 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
299 static inline unsigned int
300 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
302 struct sclp_vt220_sccb *sccb;
303 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
304 return sccb->evbuf.length - sizeof(struct evbuf_header);
308 * Add msg to buffer associated with request. Return the number of characters
312 sclp_vt220_add_msg(struct sclp_vt220_request *request,
313 const unsigned char *msg, int count, int convertlf)
315 struct sclp_vt220_sccb *sccb;
321 if (count > sclp_vt220_space_left(request))
322 count = sclp_vt220_space_left(request);
326 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
327 buffer = (void *) ((addr_t) sccb + sccb->header.length);
330 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
332 (from < count) && (to < sclp_vt220_space_left(request));
334 /* Retrieve character */
336 /* Perform conversion */
338 if (to + 1 < sclp_vt220_space_left(request)) {
339 ((unsigned char *) buffer)[to++] = c;
340 ((unsigned char *) buffer)[to++] = 0x0d;
345 ((unsigned char *) buffer)[to++] = c;
347 sccb->header.length += to;
348 sccb->evbuf.length += to;
351 memcpy(buffer, (const void *) msg, count);
352 sccb->header.length += count;
353 sccb->evbuf.length += count;
359 * Emit buffer after having waited long enough for more data to arrive.
362 sclp_vt220_timeout(unsigned long data)
364 sclp_vt220_emit_current();
367 #define BUFFER_MAX_DELAY HZ/20
370 * Internal implementation of the write function. Write COUNT bytes of data
372 * to the SCLP interface. In case that the data does not fit into the current
373 * write buffer, emit the current one and allocate a new one. If there are no
374 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
375 * is non-zero, the buffer will be scheduled for emitting after a timeout -
376 * otherwise the user has to explicitly call the flush function.
377 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
378 * buffer should be converted to 0x0a 0x0d. After completion, return the number
382 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
383 int convertlf, int may_fail)
393 spin_lock_irqsave(&sclp_vt220_lock, flags);
395 /* Create an sclp output buffer if none exists yet */
396 if (sclp_vt220_current_request == NULL) {
397 while (list_empty(&sclp_vt220_empty)) {
398 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
399 if (may_fail || sclp_vt220_suspended)
403 spin_lock_irqsave(&sclp_vt220_lock, flags);
405 page = (void *) sclp_vt220_empty.next;
406 list_del((struct list_head *) page);
407 sclp_vt220_current_request =
408 sclp_vt220_initialize_page(page);
410 /* Try to write the string to the current request buffer */
411 written = sclp_vt220_add_msg(sclp_vt220_current_request,
412 buf, count, convertlf);
413 overall_written += written;
414 if (written == count)
417 * Not all characters could be written to the current
418 * output buffer. Emit the buffer, create a new buffer
419 * and then output the rest of the string.
421 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
422 sclp_vt220_emit_current();
423 spin_lock_irqsave(&sclp_vt220_lock, flags);
427 /* Setup timer to output current console buffer after some time */
428 if (sclp_vt220_current_request != NULL &&
429 !timer_pending(&sclp_vt220_timer) && do_schedule) {
430 sclp_vt220_timer.function = sclp_vt220_timeout;
431 sclp_vt220_timer.data = 0UL;
432 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
433 add_timer(&sclp_vt220_timer);
435 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
437 return overall_written;
441 * This routine is called by the kernel to write a series of
442 * characters to the tty device. The characters may come from
443 * user space or kernel space. This routine will return the
444 * number of characters actually accepted for writing.
447 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
449 return __sclp_vt220_write(buf, count, 1, 0, 1);
452 #define SCLP_VT220_SESSION_ENDED 0x01
453 #define SCLP_VT220_SESSION_STARTED 0x80
454 #define SCLP_VT220_SESSION_DATA 0x00
457 * Called by the SCLP to report incoming event buffers.
460 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
465 /* Ignore input if device is not open */
466 if (sclp_vt220_tty == NULL)
469 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
470 count = evbuf->length - sizeof(struct evbuf_header);
473 case SCLP_VT220_SESSION_ENDED:
474 case SCLP_VT220_SESSION_STARTED:
476 case SCLP_VT220_SESSION_DATA:
477 /* Send input to line discipline */
480 tty_insert_flip_string(sclp_vt220_tty, buffer, count);
481 tty_flip_buffer_push(sclp_vt220_tty);
487 * This routine is called when a particular tty device is opened.
490 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
492 if (tty->count == 1) {
493 sclp_vt220_tty = tty;
494 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
495 if (tty->driver_data == NULL)
497 tty->low_latency = 0;
503 * This routine is called when a particular tty device is closed.
506 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
508 if (tty->count == 1) {
509 sclp_vt220_tty = NULL;
510 kfree(tty->driver_data);
511 tty->driver_data = NULL;
516 * This routine is called by the kernel to write a single
517 * character to the tty device. If the kernel uses this routine,
518 * it must call the flush_chars() routine (if defined) when it is
519 * done stuffing characters into the driver.
522 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
524 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
528 * This routine is called by the kernel after it has written a
529 * series of characters to the tty device using put_char().
532 sclp_vt220_flush_chars(struct tty_struct *tty)
534 if (!sclp_vt220_queue_running)
535 sclp_vt220_emit_current();
537 sclp_vt220_flush_later = 1;
541 * This routine returns the numbers of characters the tty driver
542 * will accept for queuing to be written. This number is subject
543 * to change as output buffers get emptied, or if the output flow
547 sclp_vt220_write_room(struct tty_struct *tty)
553 spin_lock_irqsave(&sclp_vt220_lock, flags);
555 if (sclp_vt220_current_request != NULL)
556 count = sclp_vt220_space_left(sclp_vt220_current_request);
557 list_for_each(l, &sclp_vt220_empty)
558 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
559 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
564 * Return number of buffered chars.
567 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
571 struct sclp_vt220_request *r;
574 spin_lock_irqsave(&sclp_vt220_lock, flags);
576 if (sclp_vt220_current_request != NULL)
577 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
578 list_for_each(l, &sclp_vt220_outqueue) {
579 r = list_entry(l, struct sclp_vt220_request, list);
580 count += sclp_vt220_chars_stored(r);
582 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
587 * Pass on all buffers to the hardware. Return only when there are no more
591 sclp_vt220_flush_buffer(struct tty_struct *tty)
593 sclp_vt220_emit_current();
596 /* Release allocated pages. */
597 static void __init __sclp_vt220_free_pages(void)
599 struct list_head *page, *p;
601 list_for_each_safe(page, p, &sclp_vt220_empty) {
603 free_page((unsigned long) page);
607 /* Release memory and unregister from sclp core. Controlled by init counting -
608 * only the last invoker will actually perform these actions. */
609 static void __init __sclp_vt220_cleanup(void)
611 sclp_vt220_init_count--;
612 if (sclp_vt220_init_count != 0)
614 sclp_unregister(&sclp_vt220_register);
615 __sclp_vt220_free_pages();
618 /* Allocate buffer pages and register with sclp core. Controlled by init
619 * counting - only the first invoker will actually perform these actions. */
620 static int __init __sclp_vt220_init(int num_pages)
626 sclp_vt220_init_count++;
627 if (sclp_vt220_init_count != 1)
629 spin_lock_init(&sclp_vt220_lock);
630 INIT_LIST_HEAD(&sclp_vt220_empty);
631 INIT_LIST_HEAD(&sclp_vt220_outqueue);
632 init_timer(&sclp_vt220_timer);
633 sclp_vt220_current_request = NULL;
634 sclp_vt220_buffered_chars = 0;
635 sclp_vt220_tty = NULL;
636 sclp_vt220_flush_later = 0;
638 /* Allocate pages for output buffering */
640 for (i = 0; i < num_pages; i++) {
641 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
644 list_add_tail(page, &sclp_vt220_empty);
646 rc = sclp_register(&sclp_vt220_register);
649 __sclp_vt220_free_pages();
650 sclp_vt220_init_count--;
655 static const struct tty_operations sclp_vt220_ops = {
656 .open = sclp_vt220_open,
657 .close = sclp_vt220_close,
658 .write = sclp_vt220_write,
659 .put_char = sclp_vt220_put_char,
660 .flush_chars = sclp_vt220_flush_chars,
661 .write_room = sclp_vt220_write_room,
662 .chars_in_buffer = sclp_vt220_chars_in_buffer,
663 .flush_buffer = sclp_vt220_flush_buffer,
667 * Register driver with SCLP and Linux and initialize internal tty structures.
669 static int __init sclp_vt220_tty_init(void)
671 struct tty_driver *driver;
674 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
675 * symmetry between VM and LPAR systems regarding ttyS1. */
676 driver = alloc_tty_driver(1);
679 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
683 driver->owner = THIS_MODULE;
684 driver->driver_name = SCLP_VT220_DRIVER_NAME;
685 driver->name = SCLP_VT220_DEVICE_NAME;
686 driver->major = SCLP_VT220_MAJOR;
687 driver->minor_start = SCLP_VT220_MINOR;
688 driver->type = TTY_DRIVER_TYPE_SYSTEM;
689 driver->subtype = SYSTEM_TYPE_TTY;
690 driver->init_termios = tty_std_termios;
691 driver->flags = TTY_DRIVER_REAL_RAW;
692 tty_set_operations(driver, &sclp_vt220_ops);
694 rc = tty_register_driver(driver);
697 sclp_vt220_driver = driver;
701 __sclp_vt220_cleanup();
703 put_tty_driver(driver);
706 __initcall(sclp_vt220_tty_init);
708 #ifdef CONFIG_SCLP_VT220_CONSOLE
711 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
713 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
716 static struct tty_driver *
717 sclp_vt220_con_device(struct console *c, int *index)
720 return sclp_vt220_driver;
723 static void __sclp_vt220_flush_buffer(void)
727 sclp_vt220_emit_current();
728 spin_lock_irqsave(&sclp_vt220_lock, flags);
729 if (timer_pending(&sclp_vt220_timer))
730 del_timer(&sclp_vt220_timer);
731 while (sclp_vt220_queue_running) {
732 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
734 spin_lock_irqsave(&sclp_vt220_lock, flags);
736 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
740 * Resume console: If there are cached messages, emit them.
742 static void sclp_vt220_resume(void)
746 spin_lock_irqsave(&sclp_vt220_lock, flags);
747 sclp_vt220_suspended = 0;
748 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
749 sclp_vt220_emit_current();
753 * Suspend console: Set suspend flag and flush console
755 static void sclp_vt220_suspend(void)
759 spin_lock_irqsave(&sclp_vt220_lock, flags);
760 sclp_vt220_suspended = 1;
761 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
762 __sclp_vt220_flush_buffer();
765 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
766 enum sclp_pm_event sclp_pm_event)
768 switch (sclp_pm_event) {
769 case SCLP_PM_EVENT_FREEZE:
770 sclp_vt220_suspend();
772 case SCLP_PM_EVENT_RESTORE:
773 case SCLP_PM_EVENT_THAW:
780 sclp_vt220_notify(struct notifier_block *self,
781 unsigned long event, void *data)
783 __sclp_vt220_flush_buffer();
787 static struct notifier_block on_panic_nb = {
788 .notifier_call = sclp_vt220_notify,
792 static struct notifier_block on_reboot_nb = {
793 .notifier_call = sclp_vt220_notify,
797 /* Structure needed to register with printk */
798 static struct console sclp_vt220_console =
800 .name = SCLP_VT220_CONSOLE_NAME,
801 .write = sclp_vt220_con_write,
802 .device = sclp_vt220_con_device,
803 .flags = CON_PRINTBUFFER,
804 .index = SCLP_VT220_CONSOLE_INDEX
808 sclp_vt220_con_init(void)
812 if (!CONSOLE_IS_SCLP)
814 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
817 /* Attach linux console */
818 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
819 register_reboot_notifier(&on_reboot_nb);
820 register_console(&sclp_vt220_console);
824 console_initcall(sclp_vt220_con_init);
825 #endif /* CONFIG_SCLP_VT220_CONSOLE */