2 * drivers/s390/char/sclp_vt220.c
3 * SCLP VT220 terminal driver.
6 * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/spinlock.h>
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/timer.h>
16 #include <linux/kernel.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/sched.h>
20 #include <linux/errno.h>
22 #include <linux/major.h>
23 #include <linux/console.h>
24 #include <linux/kdev_t.h>
25 #include <linux/bootmem.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <asm/uaccess.h>
31 #define SCLP_VT220_PRINT_HEADER "sclp vt220 tty driver: "
32 #define SCLP_VT220_MAJOR TTY_MAJOR
33 #define SCLP_VT220_MINOR 65
34 #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
35 #define SCLP_VT220_DEVICE_NAME "ttysclp"
36 #define SCLP_VT220_CONSOLE_NAME "ttyS"
37 #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
38 #define SCLP_VT220_BUF_SIZE 80
40 /* Representation of a single write request */
41 struct sclp_vt220_request {
42 struct list_head list;
43 struct sclp_req sclp_req;
45 struct timer_list retry_timer;
49 struct sclp_vt220_sccb {
50 struct sccb_header header;
51 struct evbuf_header evbuf;
54 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
55 sizeof(struct sclp_vt220_request) - \
56 sizeof(struct sclp_vt220_sccb))
58 /* Structures and data needed to register tty driver */
59 static struct tty_driver *sclp_vt220_driver;
61 /* The tty_struct that the kernel associated with us */
62 static struct tty_struct *sclp_vt220_tty;
64 /* Lock to protect internal data from concurrent access */
65 static spinlock_t sclp_vt220_lock;
67 /* List of empty pages to be used as write request buffers */
68 static struct list_head sclp_vt220_empty;
70 /* List of pending requests */
71 static struct list_head sclp_vt220_outqueue;
73 /* Number of requests in outqueue */
74 static int sclp_vt220_outqueue_count;
76 /* Wait queue used to delay write requests while we've run out of buffers */
77 static wait_queue_head_t sclp_vt220_waitq;
79 /* Timer used for delaying write requests to merge subsequent messages into
81 static struct timer_list sclp_vt220_timer;
83 /* Pointer to current request buffer which has been partially filled but not
85 static struct sclp_vt220_request *sclp_vt220_current_request;
87 /* Number of characters in current request buffer */
88 static int sclp_vt220_buffered_chars;
90 /* Flag indicating whether this driver has already been initialized */
91 static int sclp_vt220_initialized = 0;
93 /* Flag indicating that sclp_vt220_current_request should really
94 * have been already queued but wasn't because the SCLP was processing
96 static int sclp_vt220_flush_later;
98 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
99 static void __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
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)
119 struct sclp_vt220_request *next;
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 sclp_vt220_outqueue_count--;
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 next = list_entry(sclp_vt220_outqueue.next,
133 struct sclp_vt220_request, list);
134 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
136 __sclp_vt220_emit(next);
137 else if (sclp_vt220_flush_later)
138 sclp_vt220_emit_current();
139 wake_up(&sclp_vt220_waitq);
140 /* Check if the tty needs a wake up call */
141 if (sclp_vt220_tty != NULL) {
142 tty_wakeup(sclp_vt220_tty);
147 * Retry sclp write request after waiting some time for an sclp equipment
151 sclp_vt220_retry(unsigned long data)
153 struct sclp_vt220_request *request;
154 struct sclp_vt220_sccb *sccb;
156 request = (struct sclp_vt220_request *) data;
157 request->sclp_req.status = SCLP_REQ_FILLED;
158 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
159 sccb->header.response_code = 0x0000;
160 sclp_add_request(&request->sclp_req);
163 #define SCLP_BUFFER_MAX_RETRY 5
164 #define SCLP_BUFFER_RETRY_INTERVAL 2
167 * Callback through which the result of a write request is reported by the
171 sclp_vt220_callback(struct sclp_req *request, void *data)
173 struct sclp_vt220_request *vt220_request;
174 struct sclp_vt220_sccb *sccb;
176 vt220_request = (struct sclp_vt220_request *) data;
177 if (request->status == SCLP_REQ_FAILED) {
178 sclp_vt220_process_queue(vt220_request);
181 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
183 /* Check SCLP response code and choose suitable action */
184 switch (sccb->header.response_code) {
188 case 0x05f0: /* Target resource in improper state */
191 case 0x0340: /* Contained SCLP equipment check */
192 if (vt220_request->retry_count++ > SCLP_BUFFER_MAX_RETRY)
194 /* Remove processed buffers and requeue rest */
195 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
196 /* Not all buffers were processed */
197 sccb->header.response_code = 0x0000;
198 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
199 sclp_add_request(request);
204 case 0x0040: /* SCLP equipment check */
205 if (vt220_request->retry_count++ > SCLP_BUFFER_MAX_RETRY)
207 /* Wait some time, then retry request */
208 vt220_request->retry_timer.function = sclp_vt220_retry;
209 vt220_request->retry_timer.data =
210 (unsigned long) vt220_request;
211 vt220_request->retry_timer.expires =
212 jiffies + SCLP_BUFFER_RETRY_INTERVAL*HZ;
213 add_timer(&vt220_request->retry_timer);
219 sclp_vt220_process_queue(vt220_request);
223 * Emit vt220 request buffer to SCLP.
226 __sclp_vt220_emit(struct sclp_vt220_request *request)
228 if (!(sclp_vt220_register.sclp_send_mask & EvTyp_VT220Msg_Mask)) {
229 request->sclp_req.status = SCLP_REQ_FAILED;
230 sclp_vt220_callback(&request->sclp_req, (void *) request);
233 request->sclp_req.command = SCLP_CMDW_WRITEDATA;
234 request->sclp_req.status = SCLP_REQ_FILLED;
235 request->sclp_req.callback = sclp_vt220_callback;
236 request->sclp_req.callback_data = (void *) request;
238 sclp_add_request(&request->sclp_req);
242 * Queue and emit given request.
245 sclp_vt220_emit(struct sclp_vt220_request *request)
250 spin_lock_irqsave(&sclp_vt220_lock, flags);
251 list_add_tail(&request->list, &sclp_vt220_outqueue);
252 count = sclp_vt220_outqueue_count++;
253 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
254 /* Emit only the first buffer immediately - callback takes care of
257 __sclp_vt220_emit(request);
261 * Queue and emit current request.
264 sclp_vt220_emit_current(void)
267 struct sclp_vt220_request *request;
268 struct sclp_vt220_sccb *sccb;
270 spin_lock_irqsave(&sclp_vt220_lock, flags);
272 if (sclp_vt220_current_request != NULL) {
273 sccb = (struct sclp_vt220_sccb *)
274 sclp_vt220_current_request->sclp_req.sccb;
275 /* Only emit buffers with content */
276 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
277 request = sclp_vt220_current_request;
278 sclp_vt220_current_request = NULL;
279 if (timer_pending(&sclp_vt220_timer))
280 del_timer(&sclp_vt220_timer);
282 sclp_vt220_flush_later = 0;
284 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
286 sclp_vt220_emit(request);
289 #define SCLP_NORMAL_WRITE 0x00
292 * Helper function to initialize a page with the sclp request structure.
294 static struct sclp_vt220_request *
295 sclp_vt220_initialize_page(void *page)
297 struct sclp_vt220_request *request;
298 struct sclp_vt220_sccb *sccb;
300 /* Place request structure at end of page */
301 request = ((struct sclp_vt220_request *)
302 ((addr_t) page + PAGE_SIZE)) - 1;
303 init_timer(&request->retry_timer);
304 request->retry_count = 0;
305 request->sclp_req.sccb = page;
306 /* SCCB goes at start of page */
307 sccb = (struct sclp_vt220_sccb *) page;
308 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
309 sccb->header.length = sizeof(struct sclp_vt220_sccb);
310 sccb->header.function_code = SCLP_NORMAL_WRITE;
311 sccb->header.response_code = 0x0000;
312 sccb->evbuf.type = EvTyp_VT220Msg;
313 sccb->evbuf.length = sizeof(struct evbuf_header);
318 static inline unsigned int
319 sclp_vt220_space_left(struct sclp_vt220_request *request)
321 struct sclp_vt220_sccb *sccb;
322 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
323 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
327 static inline unsigned int
328 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
330 struct sclp_vt220_sccb *sccb;
331 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
332 return sccb->evbuf.length - sizeof(struct evbuf_header);
336 * Add msg to buffer associated with request. Return the number of characters
340 sclp_vt220_add_msg(struct sclp_vt220_request *request,
341 const unsigned char *msg, int count, int convertlf)
343 struct sclp_vt220_sccb *sccb;
349 if (count > sclp_vt220_space_left(request))
350 count = sclp_vt220_space_left(request);
354 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
355 buffer = (void *) ((addr_t) sccb + sccb->header.length);
358 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
360 (from < count) && (to < sclp_vt220_space_left(request));
362 /* Retrieve character */
364 /* Perform conversion */
366 if (to + 1 < sclp_vt220_space_left(request)) {
367 ((unsigned char *) buffer)[to++] = c;
368 ((unsigned char *) buffer)[to++] = 0x0d;
373 ((unsigned char *) buffer)[to++] = c;
375 sccb->header.length += to;
376 sccb->evbuf.length += to;
379 memcpy(buffer, (const void *) msg, count);
380 sccb->header.length += count;
381 sccb->evbuf.length += count;
387 * Emit buffer after having waited long enough for more data to arrive.
390 sclp_vt220_timeout(unsigned long data)
392 sclp_vt220_emit_current();
395 #define BUFFER_MAX_DELAY HZ/2
398 * Internal implementation of the write function. Write COUNT bytes of data
400 * to the SCLP interface. In case that the data does not fit into the current
401 * write buffer, emit the current one and allocate a new one. If there are no
402 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
403 * is non-zero, the buffer will be scheduled for emitting after a timeout -
404 * otherwise the user has to explicitly call the flush function.
405 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
406 * buffer should be converted to 0x0a 0x0d. After completion, return the number
410 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
421 spin_lock_irqsave(&sclp_vt220_lock, flags);
423 /* Create a sclp output buffer if none exists yet */
424 if (sclp_vt220_current_request == NULL) {
425 while (list_empty(&sclp_vt220_empty)) {
426 spin_unlock_irqrestore(&sclp_vt220_lock,
431 wait_event(sclp_vt220_waitq,
432 !list_empty(&sclp_vt220_empty));
433 spin_lock_irqsave(&sclp_vt220_lock, flags);
435 page = (void *) sclp_vt220_empty.next;
436 list_del((struct list_head *) page);
437 sclp_vt220_current_request =
438 sclp_vt220_initialize_page(page);
440 /* Try to write the string to the current request buffer */
441 written = sclp_vt220_add_msg(sclp_vt220_current_request,
442 buf, count, convertlf);
443 overall_written += written;
444 if (written == count)
447 * Not all characters could be written to the current
448 * output buffer. Emit the buffer, create a new buffer
449 * and then output the rest of the string.
451 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
452 sclp_vt220_emit_current();
453 spin_lock_irqsave(&sclp_vt220_lock, flags);
457 /* Setup timer to output current console buffer after some time */
458 if (sclp_vt220_current_request != NULL &&
459 !timer_pending(&sclp_vt220_timer) && do_schedule) {
460 sclp_vt220_timer.function = sclp_vt220_timeout;
461 sclp_vt220_timer.data = 0UL;
462 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
463 add_timer(&sclp_vt220_timer);
465 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
466 return overall_written;
470 * This routine is called by the kernel to write a series of
471 * characters to the tty device. The characters may come from
472 * user space or kernel space. This routine will return the
473 * number of characters actually accepted for writing.
476 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
478 return __sclp_vt220_write(buf, count, 1, 0);
481 #define SCLP_VT220_SESSION_ENDED 0x01
482 #define SCLP_VT220_SESSION_STARTED 0x80
483 #define SCLP_VT220_SESSION_DATA 0x00
486 * Called by the SCLP to report incoming event buffers.
489 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
494 /* Ignore input if device is not open */
495 if (sclp_vt220_tty == NULL)
498 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
499 count = evbuf->length - sizeof(struct evbuf_header);
502 case SCLP_VT220_SESSION_ENDED:
503 case SCLP_VT220_SESSION_STARTED:
505 case SCLP_VT220_SESSION_DATA:
506 /* Send input to line discipline */
509 /* Prevent buffer overrun by discarding input. Note that
510 * because buffer_push works asynchronously, we cannot wait
511 * for the buffer to be emptied. */
512 if (count + sclp_vt220_tty->flip.count > TTY_FLIPBUF_SIZE)
513 count = TTY_FLIPBUF_SIZE - sclp_vt220_tty->flip.count;
514 memcpy(sclp_vt220_tty->flip.char_buf_ptr, buffer, count);
515 memset(sclp_vt220_tty->flip.flag_buf_ptr, TTY_NORMAL, count);
516 sclp_vt220_tty->flip.char_buf_ptr += count;
517 sclp_vt220_tty->flip.flag_buf_ptr += count;
518 sclp_vt220_tty->flip.count += count;
519 tty_flip_buffer_push(sclp_vt220_tty);
525 * This routine is called when a particular tty device is opened.
528 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
530 if (tty->count == 1) {
531 sclp_vt220_tty = tty;
532 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
533 if (tty->driver_data == NULL)
535 tty->low_latency = 0;
541 * This routine is called when a particular tty device is closed.
544 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
546 if (tty->count == 1) {
547 sclp_vt220_tty = NULL;
548 kfree(tty->driver_data);
549 tty->driver_data = NULL;
554 * This routine is called by the kernel to write a single
555 * character to the tty device. If the kernel uses this routine,
556 * it must call the flush_chars() routine (if defined) when it is
557 * done stuffing characters into the driver.
559 * NOTE: include/linux/tty_driver.h specifies that a character should be
560 * ignored if there is no room in the queue. This driver implements a different
561 * semantic in that it will block when there is no more room left.
564 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
566 __sclp_vt220_write(&ch, 1, 0, 0);
570 * This routine is called by the kernel after it has written a
571 * series of characters to the tty device using put_char().
574 sclp_vt220_flush_chars(struct tty_struct *tty)
576 if (sclp_vt220_outqueue_count == 0)
577 sclp_vt220_emit_current();
579 sclp_vt220_flush_later = 1;
583 * This routine returns the numbers of characters the tty driver
584 * will accept for queuing to be written. This number is subject
585 * to change as output buffers get emptied, or if the output flow
589 sclp_vt220_write_room(struct tty_struct *tty)
595 spin_lock_irqsave(&sclp_vt220_lock, flags);
597 if (sclp_vt220_current_request != NULL)
598 count = sclp_vt220_space_left(sclp_vt220_current_request);
599 list_for_each(l, &sclp_vt220_empty)
600 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
601 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
606 * Return number of buffered chars.
609 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
613 struct sclp_vt220_request *r;
616 spin_lock_irqsave(&sclp_vt220_lock, flags);
618 if (sclp_vt220_current_request != NULL)
619 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
620 list_for_each(l, &sclp_vt220_outqueue) {
621 r = list_entry(l, struct sclp_vt220_request, list);
622 count += sclp_vt220_chars_stored(r);
624 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
629 __sclp_vt220_flush_buffer(void)
633 sclp_vt220_emit_current();
634 spin_lock_irqsave(&sclp_vt220_lock, flags);
635 if (timer_pending(&sclp_vt220_timer))
636 del_timer(&sclp_vt220_timer);
637 while (sclp_vt220_outqueue_count > 0) {
638 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
640 spin_lock_irqsave(&sclp_vt220_lock, flags);
642 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
646 * Pass on all buffers to the hardware. Return only when there are no more
650 sclp_vt220_flush_buffer(struct tty_struct *tty)
652 sclp_vt220_emit_current();
656 * Initialize all relevant components and register driver with system.
659 __sclp_vt220_init(int early)
664 if (sclp_vt220_initialized)
666 sclp_vt220_initialized = 1;
667 spin_lock_init(&sclp_vt220_lock);
668 INIT_LIST_HEAD(&sclp_vt220_empty);
669 INIT_LIST_HEAD(&sclp_vt220_outqueue);
670 init_waitqueue_head(&sclp_vt220_waitq);
671 init_timer(&sclp_vt220_timer);
672 sclp_vt220_current_request = NULL;
673 sclp_vt220_buffered_chars = 0;
674 sclp_vt220_outqueue_count = 0;
675 sclp_vt220_tty = NULL;
676 sclp_vt220_flush_later = 0;
678 /* Allocate pages for output buffering */
679 for (i = 0; i < (early ? MAX_CONSOLE_PAGES : MAX_KMEM_PAGES); i++) {
681 page = alloc_bootmem_low_pages(PAGE_SIZE);
683 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
686 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
691 static struct tty_operations sclp_vt220_ops = {
692 .open = sclp_vt220_open,
693 .close = sclp_vt220_close,
694 .write = sclp_vt220_write,
695 .put_char = sclp_vt220_put_char,
696 .flush_chars = sclp_vt220_flush_chars,
697 .write_room = sclp_vt220_write_room,
698 .chars_in_buffer = sclp_vt220_chars_in_buffer,
699 .flush_buffer = sclp_vt220_flush_buffer
703 * Register driver with SCLP and Linux and initialize internal tty structures.
706 sclp_vt220_tty_init(void)
708 struct tty_driver *driver;
711 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
712 * symmetry between VM and LPAR systems regarding ttyS1. */
713 driver = alloc_tty_driver(1);
716 rc = __sclp_vt220_init(0);
718 put_tty_driver(driver);
721 rc = sclp_register(&sclp_vt220_register);
723 printk(KERN_ERR SCLP_VT220_PRINT_HEADER
724 "could not register tty - "
725 "sclp_register returned %d\n", rc);
726 put_tty_driver(driver);
730 driver->owner = THIS_MODULE;
731 driver->driver_name = SCLP_VT220_DRIVER_NAME;
732 driver->name = SCLP_VT220_DEVICE_NAME;
733 driver->major = SCLP_VT220_MAJOR;
734 driver->minor_start = SCLP_VT220_MINOR;
735 driver->type = TTY_DRIVER_TYPE_SYSTEM;
736 driver->subtype = SYSTEM_TYPE_TTY;
737 driver->init_termios = tty_std_termios;
738 driver->flags = TTY_DRIVER_REAL_RAW;
739 tty_set_operations(driver, &sclp_vt220_ops);
741 rc = tty_register_driver(driver);
743 printk(KERN_ERR SCLP_VT220_PRINT_HEADER
744 "could not register tty - "
745 "tty_register_driver returned %d\n", rc);
746 put_tty_driver(driver);
749 sclp_vt220_driver = driver;
753 module_init(sclp_vt220_tty_init);
755 #ifdef CONFIG_SCLP_VT220_CONSOLE
758 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
760 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1);
763 static struct tty_driver *
764 sclp_vt220_con_device(struct console *c, int *index)
767 return sclp_vt220_driver;
771 * This routine is called from panic when the kernel is going to give up.
772 * We have to make sure that all buffers will be flushed to the SCLP.
773 * Note that this function may be called from within an interrupt context.
776 sclp_vt220_con_unblank(void)
778 __sclp_vt220_flush_buffer();
781 /* Structure needed to register with printk */
782 static struct console sclp_vt220_console =
784 .name = SCLP_VT220_CONSOLE_NAME,
785 .write = sclp_vt220_con_write,
786 .device = sclp_vt220_con_device,
787 .unblank = sclp_vt220_con_unblank,
788 .flags = CON_PRINTBUFFER,
789 .index = SCLP_VT220_CONSOLE_INDEX
793 sclp_vt220_con_init(void)
797 if (!CONSOLE_IS_SCLP)
799 rc = __sclp_vt220_init(1);
802 /* Attach linux console */
803 register_console(&sclp_vt220_console);
807 console_initcall(sclp_vt220_con_init);
808 #endif /* CONFIG_SCLP_VT220_CONSOLE */