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/bootmem.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/reboot.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;
504 * This routine is called when a particular tty device is closed.
507 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
509 if (tty->count == 1) {
510 sclp_vt220_tty = NULL;
511 kfree(tty->driver_data);
512 tty->driver_data = NULL;
517 * This routine is called by the kernel to write a single
518 * character to the tty device. If the kernel uses this routine,
519 * it must call the flush_chars() routine (if defined) when it is
520 * done stuffing characters into the driver.
523 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
525 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
529 * This routine is called by the kernel after it has written a
530 * series of characters to the tty device using put_char().
533 sclp_vt220_flush_chars(struct tty_struct *tty)
535 if (!sclp_vt220_queue_running)
536 sclp_vt220_emit_current();
538 sclp_vt220_flush_later = 1;
542 * This routine returns the numbers of characters the tty driver
543 * will accept for queuing to be written. This number is subject
544 * to change as output buffers get emptied, or if the output flow
548 sclp_vt220_write_room(struct tty_struct *tty)
554 spin_lock_irqsave(&sclp_vt220_lock, flags);
556 if (sclp_vt220_current_request != NULL)
557 count = sclp_vt220_space_left(sclp_vt220_current_request);
558 list_for_each(l, &sclp_vt220_empty)
559 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
560 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
565 * Return number of buffered chars.
568 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
572 struct sclp_vt220_request *r;
575 spin_lock_irqsave(&sclp_vt220_lock, flags);
577 if (sclp_vt220_current_request != NULL)
578 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
579 list_for_each(l, &sclp_vt220_outqueue) {
580 r = list_entry(l, struct sclp_vt220_request, list);
581 count += sclp_vt220_chars_stored(r);
583 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
588 * Pass on all buffers to the hardware. Return only when there are no more
592 sclp_vt220_flush_buffer(struct tty_struct *tty)
594 sclp_vt220_emit_current();
597 /* Release allocated pages. */
598 static void __init __sclp_vt220_free_pages(void)
600 struct list_head *page, *p;
602 list_for_each_safe(page, p, &sclp_vt220_empty) {
604 if (slab_is_available())
605 free_page((unsigned long) page);
607 free_bootmem((unsigned long) page, PAGE_SIZE);
611 /* Release memory and unregister from sclp core. Controlled by init counting -
612 * only the last invoker will actually perform these actions. */
613 static void __init __sclp_vt220_cleanup(void)
615 sclp_vt220_init_count--;
616 if (sclp_vt220_init_count != 0)
618 sclp_unregister(&sclp_vt220_register);
619 __sclp_vt220_free_pages();
622 /* Allocate buffer pages and register with sclp core. Controlled by init
623 * counting - only the first invoker will actually perform these actions. */
624 static int __init __sclp_vt220_init(int num_pages)
630 sclp_vt220_init_count++;
631 if (sclp_vt220_init_count != 1)
633 spin_lock_init(&sclp_vt220_lock);
634 INIT_LIST_HEAD(&sclp_vt220_empty);
635 INIT_LIST_HEAD(&sclp_vt220_outqueue);
636 init_timer(&sclp_vt220_timer);
637 sclp_vt220_current_request = NULL;
638 sclp_vt220_buffered_chars = 0;
639 sclp_vt220_tty = NULL;
640 sclp_vt220_flush_later = 0;
642 /* Allocate pages for output buffering */
643 for (i = 0; i < num_pages; i++) {
644 if (slab_is_available())
645 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
647 page = alloc_bootmem_low_pages(PAGE_SIZE);
652 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
654 rc = sclp_register(&sclp_vt220_register);
657 __sclp_vt220_free_pages();
658 sclp_vt220_init_count--;
663 static const struct tty_operations sclp_vt220_ops = {
664 .open = sclp_vt220_open,
665 .close = sclp_vt220_close,
666 .write = sclp_vt220_write,
667 .put_char = sclp_vt220_put_char,
668 .flush_chars = sclp_vt220_flush_chars,
669 .write_room = sclp_vt220_write_room,
670 .chars_in_buffer = sclp_vt220_chars_in_buffer,
671 .flush_buffer = sclp_vt220_flush_buffer,
675 * Register driver with SCLP and Linux and initialize internal tty structures.
677 static int __init sclp_vt220_tty_init(void)
679 struct tty_driver *driver;
682 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
683 * symmetry between VM and LPAR systems regarding ttyS1. */
684 driver = alloc_tty_driver(1);
687 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
691 driver->owner = THIS_MODULE;
692 driver->driver_name = SCLP_VT220_DRIVER_NAME;
693 driver->name = SCLP_VT220_DEVICE_NAME;
694 driver->major = SCLP_VT220_MAJOR;
695 driver->minor_start = SCLP_VT220_MINOR;
696 driver->type = TTY_DRIVER_TYPE_SYSTEM;
697 driver->subtype = SYSTEM_TYPE_TTY;
698 driver->init_termios = tty_std_termios;
699 driver->flags = TTY_DRIVER_REAL_RAW;
700 tty_set_operations(driver, &sclp_vt220_ops);
702 rc = tty_register_driver(driver);
705 sclp_vt220_driver = driver;
709 __sclp_vt220_cleanup();
711 put_tty_driver(driver);
714 __initcall(sclp_vt220_tty_init);
716 #ifdef CONFIG_SCLP_VT220_CONSOLE
719 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
721 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
724 static struct tty_driver *
725 sclp_vt220_con_device(struct console *c, int *index)
728 return sclp_vt220_driver;
731 static void __sclp_vt220_flush_buffer(void)
735 sclp_vt220_emit_current();
736 spin_lock_irqsave(&sclp_vt220_lock, flags);
737 if (timer_pending(&sclp_vt220_timer))
738 del_timer(&sclp_vt220_timer);
739 while (sclp_vt220_queue_running) {
740 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
742 spin_lock_irqsave(&sclp_vt220_lock, flags);
744 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
748 * Resume console: If there are cached messages, emit them.
750 static void sclp_vt220_resume(void)
754 spin_lock_irqsave(&sclp_vt220_lock, flags);
755 sclp_vt220_suspended = 0;
756 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
757 sclp_vt220_emit_current();
761 * Suspend console: Set suspend flag and flush console
763 static void sclp_vt220_suspend(void)
767 spin_lock_irqsave(&sclp_vt220_lock, flags);
768 sclp_vt220_suspended = 1;
769 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
770 __sclp_vt220_flush_buffer();
773 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
774 enum sclp_pm_event sclp_pm_event)
776 switch (sclp_pm_event) {
777 case SCLP_PM_EVENT_FREEZE:
778 sclp_vt220_suspend();
780 case SCLP_PM_EVENT_RESTORE:
781 case SCLP_PM_EVENT_THAW:
788 sclp_vt220_notify(struct notifier_block *self,
789 unsigned long event, void *data)
791 __sclp_vt220_flush_buffer();
795 static struct notifier_block on_panic_nb = {
796 .notifier_call = sclp_vt220_notify,
800 static struct notifier_block on_reboot_nb = {
801 .notifier_call = sclp_vt220_notify,
805 /* Structure needed to register with printk */
806 static struct console sclp_vt220_console =
808 .name = SCLP_VT220_CONSOLE_NAME,
809 .write = sclp_vt220_con_write,
810 .device = sclp_vt220_con_device,
811 .flags = CON_PRINTBUFFER,
812 .index = SCLP_VT220_CONSOLE_INDEX
816 sclp_vt220_con_init(void)
820 if (!CONSOLE_IS_SCLP)
822 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
825 /* Attach linux console */
826 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
827 register_reboot_notifier(&on_reboot_nb);
828 register_console(&sclp_vt220_console);
832 console_initcall(sclp_vt220_con_init);
833 #endif /* CONFIG_SCLP_VT220_CONSOLE */