2 * SCLP VT220 terminal driver.
4 * Copyright IBM Corp. 2003, 2009
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 */
37 #define SCLP_VT220_BUF_SIZE 80
39 /* Representation of a single write request */
40 struct sclp_vt220_request {
41 struct list_head list;
42 struct sclp_req sclp_req;
47 struct sclp_vt220_sccb {
48 struct sccb_header header;
49 struct evbuf_header evbuf;
52 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
53 sizeof(struct sclp_vt220_request) - \
54 sizeof(struct sclp_vt220_sccb))
56 /* Structures and data needed to register tty driver */
57 static struct tty_driver *sclp_vt220_driver;
59 /* The tty_struct that the kernel associated with us */
60 static struct tty_struct *sclp_vt220_tty;
62 /* Lock to protect internal data from concurrent access */
63 static spinlock_t sclp_vt220_lock;
65 /* List of empty pages to be used as write request buffers */
66 static struct list_head sclp_vt220_empty;
68 /* List of pending requests */
69 static struct list_head sclp_vt220_outqueue;
71 /* Suspend mode flag */
72 static int sclp_vt220_suspended;
74 /* Flag that output queue is currently running */
75 static int sclp_vt220_queue_running;
77 /* Timer used for delaying write requests to merge subsequent messages into
79 static struct timer_list sclp_vt220_timer;
81 /* Pointer to current request buffer which has been partially filled but not
83 static struct sclp_vt220_request *sclp_vt220_current_request;
85 /* Number of characters in current request buffer */
86 static int sclp_vt220_buffered_chars;
88 /* Counter controlling core driver initialization. */
89 static int __initdata sclp_vt220_init_count;
91 /* Flag indicating that sclp_vt220_current_request should really
92 * have been already queued but wasn't because the SCLP was processing
94 static int sclp_vt220_flush_later;
96 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
97 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
98 enum sclp_pm_event sclp_pm_event);
99 static int __sclp_vt220_emit(struct sclp_vt220_request *request);
100 static void sclp_vt220_emit_current(void);
102 /* Registration structure for our interest in SCLP event buffers */
103 static struct sclp_register sclp_vt220_register = {
104 .send_mask = EVTYP_VT220MSG_MASK,
105 .receive_mask = EVTYP_VT220MSG_MASK,
106 .state_change_fn = NULL,
107 .receiver_fn = sclp_vt220_receiver_fn,
108 .pm_event_fn = sclp_vt220_pm_event_fn,
113 * Put provided request buffer back into queue and check emit pending
114 * buffers if necessary.
117 sclp_vt220_process_queue(struct sclp_vt220_request *request)
123 /* Put buffer back to list of empty buffers */
124 page = request->sclp_req.sccb;
125 spin_lock_irqsave(&sclp_vt220_lock, flags);
126 /* Move request from outqueue to empty queue */
127 list_del(&request->list);
128 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
129 /* Check if there is a pending buffer on the out queue. */
131 if (!list_empty(&sclp_vt220_outqueue))
132 request = list_entry(sclp_vt220_outqueue.next,
133 struct sclp_vt220_request, list);
134 if (!request || sclp_vt220_suspended) {
135 sclp_vt220_queue_running = 0;
136 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
139 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
140 } while (__sclp_vt220_emit(request));
141 if (request == NULL && sclp_vt220_flush_later)
142 sclp_vt220_emit_current();
143 /* Check if the tty needs a wake up call */
144 if (sclp_vt220_tty != NULL) {
145 tty_wakeup(sclp_vt220_tty);
149 #define SCLP_BUFFER_MAX_RETRY 1
152 * Callback through which the result of a write request is reported by the
156 sclp_vt220_callback(struct sclp_req *request, void *data)
158 struct sclp_vt220_request *vt220_request;
159 struct sclp_vt220_sccb *sccb;
161 vt220_request = (struct sclp_vt220_request *) data;
162 if (request->status == SCLP_REQ_FAILED) {
163 sclp_vt220_process_queue(vt220_request);
166 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
168 /* Check SCLP response code and choose suitable action */
169 switch (sccb->header.response_code) {
173 case 0x05f0: /* Target resource in improper state */
176 case 0x0340: /* Contained SCLP equipment check */
177 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
179 /* Remove processed buffers and requeue rest */
180 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
181 /* Not all buffers were processed */
182 sccb->header.response_code = 0x0000;
183 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
184 if (sclp_add_request(request) == 0)
189 case 0x0040: /* SCLP equipment check */
190 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
192 sccb->header.response_code = 0x0000;
193 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
194 if (sclp_add_request(request) == 0)
201 sclp_vt220_process_queue(vt220_request);
205 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
209 __sclp_vt220_emit(struct sclp_vt220_request *request)
211 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
212 request->sclp_req.status = SCLP_REQ_FAILED;
215 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
216 request->sclp_req.status = SCLP_REQ_FILLED;
217 request->sclp_req.callback = sclp_vt220_callback;
218 request->sclp_req.callback_data = (void *) request;
220 return sclp_add_request(&request->sclp_req);
224 * Queue and emit current request.
227 sclp_vt220_emit_current(void)
230 struct sclp_vt220_request *request;
231 struct sclp_vt220_sccb *sccb;
233 spin_lock_irqsave(&sclp_vt220_lock, flags);
234 if (sclp_vt220_current_request) {
235 sccb = (struct sclp_vt220_sccb *)
236 sclp_vt220_current_request->sclp_req.sccb;
237 /* Only emit buffers with content */
238 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
239 list_add_tail(&sclp_vt220_current_request->list,
240 &sclp_vt220_outqueue);
241 sclp_vt220_current_request = NULL;
242 if (timer_pending(&sclp_vt220_timer))
243 del_timer(&sclp_vt220_timer);
245 sclp_vt220_flush_later = 0;
247 if (sclp_vt220_queue_running || sclp_vt220_suspended)
249 if (list_empty(&sclp_vt220_outqueue))
251 request = list_first_entry(&sclp_vt220_outqueue,
252 struct sclp_vt220_request, list);
253 sclp_vt220_queue_running = 1;
254 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
256 if (__sclp_vt220_emit(request))
257 sclp_vt220_process_queue(request);
260 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
263 #define SCLP_NORMAL_WRITE 0x00
266 * Helper function to initialize a page with the sclp request structure.
268 static struct sclp_vt220_request *
269 sclp_vt220_initialize_page(void *page)
271 struct sclp_vt220_request *request;
272 struct sclp_vt220_sccb *sccb;
274 /* Place request structure at end of page */
275 request = ((struct sclp_vt220_request *)
276 ((addr_t) page + PAGE_SIZE)) - 1;
277 request->retry_count = 0;
278 request->sclp_req.sccb = page;
279 /* SCCB goes at start of page */
280 sccb = (struct sclp_vt220_sccb *) page;
281 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
282 sccb->header.length = sizeof(struct sclp_vt220_sccb);
283 sccb->header.function_code = SCLP_NORMAL_WRITE;
284 sccb->header.response_code = 0x0000;
285 sccb->evbuf.type = EVTYP_VT220MSG;
286 sccb->evbuf.length = sizeof(struct evbuf_header);
291 static inline unsigned int
292 sclp_vt220_space_left(struct sclp_vt220_request *request)
294 struct sclp_vt220_sccb *sccb;
295 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
296 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
300 static inline unsigned int
301 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
303 struct sclp_vt220_sccb *sccb;
304 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
305 return sccb->evbuf.length - sizeof(struct evbuf_header);
309 * Add msg to buffer associated with request. Return the number of characters
313 sclp_vt220_add_msg(struct sclp_vt220_request *request,
314 const unsigned char *msg, int count, int convertlf)
316 struct sclp_vt220_sccb *sccb;
322 if (count > sclp_vt220_space_left(request))
323 count = sclp_vt220_space_left(request);
327 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
328 buffer = (void *) ((addr_t) sccb + sccb->header.length);
331 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
333 (from < count) && (to < sclp_vt220_space_left(request));
335 /* Retrieve character */
337 /* Perform conversion */
339 if (to + 1 < sclp_vt220_space_left(request)) {
340 ((unsigned char *) buffer)[to++] = c;
341 ((unsigned char *) buffer)[to++] = 0x0d;
346 ((unsigned char *) buffer)[to++] = c;
348 sccb->header.length += to;
349 sccb->evbuf.length += to;
352 memcpy(buffer, (const void *) msg, count);
353 sccb->header.length += count;
354 sccb->evbuf.length += count;
360 * Emit buffer after having waited long enough for more data to arrive.
363 sclp_vt220_timeout(unsigned long data)
365 sclp_vt220_emit_current();
368 #define BUFFER_MAX_DELAY HZ/20
371 * Internal implementation of the write function. Write COUNT bytes of data
373 * to the SCLP interface. In case that the data does not fit into the current
374 * write buffer, emit the current one and allocate a new one. If there are no
375 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
376 * is non-zero, the buffer will be scheduled for emitting after a timeout -
377 * otherwise the user has to explicitly call the flush function.
378 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
379 * buffer should be converted to 0x0a 0x0d. After completion, return the number
383 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
384 int convertlf, int may_fail)
394 spin_lock_irqsave(&sclp_vt220_lock, flags);
396 /* Create an sclp output buffer if none exists yet */
397 if (sclp_vt220_current_request == NULL) {
398 while (list_empty(&sclp_vt220_empty)) {
399 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
400 if (may_fail || sclp_vt220_suspended)
404 spin_lock_irqsave(&sclp_vt220_lock, flags);
406 page = (void *) sclp_vt220_empty.next;
407 list_del((struct list_head *) page);
408 sclp_vt220_current_request =
409 sclp_vt220_initialize_page(page);
411 /* Try to write the string to the current request buffer */
412 written = sclp_vt220_add_msg(sclp_vt220_current_request,
413 buf, count, convertlf);
414 overall_written += written;
415 if (written == count)
418 * Not all characters could be written to the current
419 * output buffer. Emit the buffer, create a new buffer
420 * and then output the rest of the string.
422 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
423 sclp_vt220_emit_current();
424 spin_lock_irqsave(&sclp_vt220_lock, flags);
428 /* Setup timer to output current console buffer after some time */
429 if (sclp_vt220_current_request != NULL &&
430 !timer_pending(&sclp_vt220_timer) && do_schedule) {
431 sclp_vt220_timer.function = sclp_vt220_timeout;
432 sclp_vt220_timer.data = 0UL;
433 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
434 add_timer(&sclp_vt220_timer);
436 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
438 return overall_written;
442 * This routine is called by the kernel to write a series of
443 * characters to the tty device. The characters may come from
444 * user space or kernel space. This routine will return the
445 * number of characters actually accepted for writing.
448 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
450 return __sclp_vt220_write(buf, count, 1, 0, 1);
453 #define SCLP_VT220_SESSION_ENDED 0x01
454 #define SCLP_VT220_SESSION_STARTED 0x80
455 #define SCLP_VT220_SESSION_DATA 0x00
458 * Called by the SCLP to report incoming event buffers.
461 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
466 /* Ignore input if device is not open */
467 if (sclp_vt220_tty == NULL)
470 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
471 count = evbuf->length - sizeof(struct evbuf_header);
474 case SCLP_VT220_SESSION_ENDED:
475 case SCLP_VT220_SESSION_STARTED:
477 case SCLP_VT220_SESSION_DATA:
478 /* Send input to line discipline */
481 tty_insert_flip_string(sclp_vt220_tty, buffer, count);
482 tty_flip_buffer_push(sclp_vt220_tty);
488 * This routine is called when a particular tty device is opened.
491 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
493 if (tty->count == 1) {
494 sclp_vt220_tty = tty;
495 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
496 if (tty->driver_data == NULL)
498 tty->low_latency = 0;
499 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
500 tty->winsize.ws_row = 24;
501 tty->winsize.ws_col = 80;
508 * This routine is called when a particular tty device is closed.
511 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
513 if (tty->count == 1) {
514 sclp_vt220_tty = NULL;
515 kfree(tty->driver_data);
516 tty->driver_data = NULL;
521 * This routine is called by the kernel to write a single
522 * character to the tty device. If the kernel uses this routine,
523 * it must call the flush_chars() routine (if defined) when it is
524 * done stuffing characters into the driver.
527 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
529 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
533 * This routine is called by the kernel after it has written a
534 * series of characters to the tty device using put_char().
537 sclp_vt220_flush_chars(struct tty_struct *tty)
539 if (!sclp_vt220_queue_running)
540 sclp_vt220_emit_current();
542 sclp_vt220_flush_later = 1;
546 * This routine returns the numbers of characters the tty driver
547 * will accept for queuing to be written. This number is subject
548 * to change as output buffers get emptied, or if the output flow
552 sclp_vt220_write_room(struct tty_struct *tty)
558 spin_lock_irqsave(&sclp_vt220_lock, flags);
560 if (sclp_vt220_current_request != NULL)
561 count = sclp_vt220_space_left(sclp_vt220_current_request);
562 list_for_each(l, &sclp_vt220_empty)
563 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
564 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
569 * Return number of buffered chars.
572 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
576 struct sclp_vt220_request *r;
579 spin_lock_irqsave(&sclp_vt220_lock, flags);
581 if (sclp_vt220_current_request != NULL)
582 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
583 list_for_each(l, &sclp_vt220_outqueue) {
584 r = list_entry(l, struct sclp_vt220_request, list);
585 count += sclp_vt220_chars_stored(r);
587 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
592 * Pass on all buffers to the hardware. Return only when there are no more
596 sclp_vt220_flush_buffer(struct tty_struct *tty)
598 sclp_vt220_emit_current();
601 /* Release allocated pages. */
602 static void __init __sclp_vt220_free_pages(void)
604 struct list_head *page, *p;
606 list_for_each_safe(page, p, &sclp_vt220_empty) {
608 free_page((unsigned long) page);
612 /* Release memory and unregister from sclp core. Controlled by init counting -
613 * only the last invoker will actually perform these actions. */
614 static void __init __sclp_vt220_cleanup(void)
616 sclp_vt220_init_count--;
617 if (sclp_vt220_init_count != 0)
619 sclp_unregister(&sclp_vt220_register);
620 __sclp_vt220_free_pages();
623 /* Allocate buffer pages and register with sclp core. Controlled by init
624 * counting - only the first invoker will actually perform these actions. */
625 static int __init __sclp_vt220_init(int num_pages)
631 sclp_vt220_init_count++;
632 if (sclp_vt220_init_count != 1)
634 spin_lock_init(&sclp_vt220_lock);
635 INIT_LIST_HEAD(&sclp_vt220_empty);
636 INIT_LIST_HEAD(&sclp_vt220_outqueue);
637 init_timer(&sclp_vt220_timer);
638 sclp_vt220_current_request = NULL;
639 sclp_vt220_buffered_chars = 0;
640 sclp_vt220_tty = NULL;
641 sclp_vt220_flush_later = 0;
643 /* Allocate pages for output buffering */
645 for (i = 0; i < num_pages; i++) {
646 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
649 list_add_tail(page, &sclp_vt220_empty);
651 rc = sclp_register(&sclp_vt220_register);
654 __sclp_vt220_free_pages();
655 sclp_vt220_init_count--;
660 static const struct tty_operations sclp_vt220_ops = {
661 .open = sclp_vt220_open,
662 .close = sclp_vt220_close,
663 .write = sclp_vt220_write,
664 .put_char = sclp_vt220_put_char,
665 .flush_chars = sclp_vt220_flush_chars,
666 .write_room = sclp_vt220_write_room,
667 .chars_in_buffer = sclp_vt220_chars_in_buffer,
668 .flush_buffer = sclp_vt220_flush_buffer,
672 * Register driver with SCLP and Linux and initialize internal tty structures.
674 static int __init sclp_vt220_tty_init(void)
676 struct tty_driver *driver;
679 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
680 * symmetry between VM and LPAR systems regarding ttyS1. */
681 driver = alloc_tty_driver(1);
684 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
688 driver->owner = THIS_MODULE;
689 driver->driver_name = SCLP_VT220_DRIVER_NAME;
690 driver->name = SCLP_VT220_DEVICE_NAME;
691 driver->major = SCLP_VT220_MAJOR;
692 driver->minor_start = SCLP_VT220_MINOR;
693 driver->type = TTY_DRIVER_TYPE_SYSTEM;
694 driver->subtype = SYSTEM_TYPE_TTY;
695 driver->init_termios = tty_std_termios;
696 driver->flags = TTY_DRIVER_REAL_RAW;
697 tty_set_operations(driver, &sclp_vt220_ops);
699 rc = tty_register_driver(driver);
702 sclp_vt220_driver = driver;
706 __sclp_vt220_cleanup();
708 put_tty_driver(driver);
711 __initcall(sclp_vt220_tty_init);
713 static void __sclp_vt220_flush_buffer(void)
717 sclp_vt220_emit_current();
718 spin_lock_irqsave(&sclp_vt220_lock, flags);
719 if (timer_pending(&sclp_vt220_timer))
720 del_timer(&sclp_vt220_timer);
721 while (sclp_vt220_queue_running) {
722 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
724 spin_lock_irqsave(&sclp_vt220_lock, flags);
726 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
730 * Resume console: If there are cached messages, emit them.
732 static void sclp_vt220_resume(void)
736 spin_lock_irqsave(&sclp_vt220_lock, flags);
737 sclp_vt220_suspended = 0;
738 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
739 sclp_vt220_emit_current();
743 * Suspend console: Set suspend flag and flush console
745 static void sclp_vt220_suspend(void)
749 spin_lock_irqsave(&sclp_vt220_lock, flags);
750 sclp_vt220_suspended = 1;
751 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
752 __sclp_vt220_flush_buffer();
755 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
756 enum sclp_pm_event sclp_pm_event)
758 switch (sclp_pm_event) {
759 case SCLP_PM_EVENT_FREEZE:
760 sclp_vt220_suspend();
762 case SCLP_PM_EVENT_RESTORE:
763 case SCLP_PM_EVENT_THAW:
769 #ifdef CONFIG_SCLP_VT220_CONSOLE
772 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
774 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
777 static struct tty_driver *
778 sclp_vt220_con_device(struct console *c, int *index)
781 return sclp_vt220_driver;
785 sclp_vt220_notify(struct notifier_block *self,
786 unsigned long event, void *data)
788 __sclp_vt220_flush_buffer();
792 static struct notifier_block on_panic_nb = {
793 .notifier_call = sclp_vt220_notify,
797 static struct notifier_block on_reboot_nb = {
798 .notifier_call = sclp_vt220_notify,
802 /* Structure needed to register with printk */
803 static struct console sclp_vt220_console =
805 .name = SCLP_VT220_CONSOLE_NAME,
806 .write = sclp_vt220_con_write,
807 .device = sclp_vt220_con_device,
808 .flags = CON_PRINTBUFFER,
809 .index = SCLP_VT220_CONSOLE_INDEX
813 sclp_vt220_con_init(void)
817 if (!CONSOLE_IS_SCLP)
819 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
822 /* Attach linux console */
823 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
824 register_reboot_notifier(&on_reboot_nb);
825 register_console(&sclp_vt220_console);
829 console_initcall(sclp_vt220_con_init);
830 #endif /* CONFIG_SCLP_VT220_CONSOLE */