2 * drivers/s390/char/sclp_vt220.c
3 * SCLP VT220 terminal driver.
6 * Copyright IBM Corp. 2003,2008
7 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/list.h>
13 #include <linux/wait.h>
14 #include <linux/timer.h>
15 #include <linux/kernel.h>
16 #include <linux/tty.h>
17 #include <linux/tty_driver.h>
18 #include <linux/tty_flip.h>
19 #include <linux/errno.h>
21 #include <linux/major.h>
22 #include <linux/console.h>
23 #include <linux/kdev_t.h>
24 #include <linux/bootmem.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.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 /* Number of requests in outqueue */
71 static int sclp_vt220_outqueue_count
;
73 /* Timer used for delaying write requests to merge subsequent messages into
75 static struct timer_list sclp_vt220_timer
;
77 /* Pointer to current request buffer which has been partially filled but not
79 static struct sclp_vt220_request
*sclp_vt220_current_request
;
81 /* Number of characters in current request buffer */
82 static int sclp_vt220_buffered_chars
;
84 /* Counter controlling core driver initialization. */
85 static int __initdata sclp_vt220_init_count
;
87 /* Flag indicating that sclp_vt220_current_request should really
88 * have been already queued but wasn't because the SCLP was processing
90 static int sclp_vt220_flush_later
;
92 static void sclp_vt220_receiver_fn(struct evbuf_header
*evbuf
);
93 static int __sclp_vt220_emit(struct sclp_vt220_request
*request
);
94 static void sclp_vt220_emit_current(void);
96 /* Registration structure for our interest in SCLP event buffers */
97 static struct sclp_register sclp_vt220_register
= {
98 .send_mask
= EVTYP_VT220MSG_MASK
,
99 .receive_mask
= EVTYP_VT220MSG_MASK
,
100 .state_change_fn
= NULL
,
101 .receiver_fn
= sclp_vt220_receiver_fn
106 * Put provided request buffer back into queue and check emit pending
107 * buffers if necessary.
110 sclp_vt220_process_queue(struct sclp_vt220_request
*request
)
116 /* Put buffer back to list of empty buffers */
117 page
= request
->sclp_req
.sccb
;
118 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
119 /* Move request from outqueue to empty queue */
120 list_del(&request
->list
);
121 sclp_vt220_outqueue_count
--;
122 list_add_tail((struct list_head
*) page
, &sclp_vt220_empty
);
123 /* Check if there is a pending buffer on the out queue. */
125 if (!list_empty(&sclp_vt220_outqueue
))
126 request
= list_entry(sclp_vt220_outqueue
.next
,
127 struct sclp_vt220_request
, list
);
128 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
129 } while (request
&& __sclp_vt220_emit(request
));
130 if (request
== NULL
&& sclp_vt220_flush_later
)
131 sclp_vt220_emit_current();
132 /* Check if the tty needs a wake up call */
133 if (sclp_vt220_tty
!= NULL
) {
134 tty_wakeup(sclp_vt220_tty
);
138 #define SCLP_BUFFER_MAX_RETRY 1
141 * Callback through which the result of a write request is reported by the
145 sclp_vt220_callback(struct sclp_req
*request
, void *data
)
147 struct sclp_vt220_request
*vt220_request
;
148 struct sclp_vt220_sccb
*sccb
;
150 vt220_request
= (struct sclp_vt220_request
*) data
;
151 if (request
->status
== SCLP_REQ_FAILED
) {
152 sclp_vt220_process_queue(vt220_request
);
155 sccb
= (struct sclp_vt220_sccb
*) vt220_request
->sclp_req
.sccb
;
157 /* Check SCLP response code and choose suitable action */
158 switch (sccb
->header
.response_code
) {
162 case 0x05f0: /* Target resource in improper state */
165 case 0x0340: /* Contained SCLP equipment check */
166 if (++vt220_request
->retry_count
> SCLP_BUFFER_MAX_RETRY
)
168 /* Remove processed buffers and requeue rest */
169 if (sclp_remove_processed((struct sccb_header
*) sccb
) > 0) {
170 /* Not all buffers were processed */
171 sccb
->header
.response_code
= 0x0000;
172 vt220_request
->sclp_req
.status
= SCLP_REQ_FILLED
;
173 if (sclp_add_request(request
) == 0)
178 case 0x0040: /* SCLP equipment check */
179 if (++vt220_request
->retry_count
> SCLP_BUFFER_MAX_RETRY
)
181 sccb
->header
.response_code
= 0x0000;
182 vt220_request
->sclp_req
.status
= SCLP_REQ_FILLED
;
183 if (sclp_add_request(request
) == 0)
190 sclp_vt220_process_queue(vt220_request
);
194 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
198 __sclp_vt220_emit(struct sclp_vt220_request
*request
)
200 if (!(sclp_vt220_register
.sclp_receive_mask
& EVTYP_VT220MSG_MASK
)) {
201 request
->sclp_req
.status
= SCLP_REQ_FAILED
;
204 request
->sclp_req
.command
= SCLP_CMDW_WRITE_EVENT_DATA
;
205 request
->sclp_req
.status
= SCLP_REQ_FILLED
;
206 request
->sclp_req
.callback
= sclp_vt220_callback
;
207 request
->sclp_req
.callback_data
= (void *) request
;
209 return sclp_add_request(&request
->sclp_req
);
213 * Queue and emit given request.
216 sclp_vt220_emit(struct sclp_vt220_request
*request
)
221 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
222 list_add_tail(&request
->list
, &sclp_vt220_outqueue
);
223 count
= sclp_vt220_outqueue_count
++;
224 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
225 /* Emit only the first buffer immediately - callback takes care of
227 if (count
== 0 && __sclp_vt220_emit(request
))
228 sclp_vt220_process_queue(request
);
232 * Queue and emit current request. Return zero on success, non-zero otherwise.
235 sclp_vt220_emit_current(void)
238 struct sclp_vt220_request
*request
;
239 struct sclp_vt220_sccb
*sccb
;
241 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
243 if (sclp_vt220_current_request
!= NULL
) {
244 sccb
= (struct sclp_vt220_sccb
*)
245 sclp_vt220_current_request
->sclp_req
.sccb
;
246 /* Only emit buffers with content */
247 if (sccb
->header
.length
!= sizeof(struct sclp_vt220_sccb
)) {
248 request
= sclp_vt220_current_request
;
249 sclp_vt220_current_request
= NULL
;
250 if (timer_pending(&sclp_vt220_timer
))
251 del_timer(&sclp_vt220_timer
);
253 sclp_vt220_flush_later
= 0;
255 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
257 sclp_vt220_emit(request
);
260 #define SCLP_NORMAL_WRITE 0x00
263 * Helper function to initialize a page with the sclp request structure.
265 static struct sclp_vt220_request
*
266 sclp_vt220_initialize_page(void *page
)
268 struct sclp_vt220_request
*request
;
269 struct sclp_vt220_sccb
*sccb
;
271 /* Place request structure at end of page */
272 request
= ((struct sclp_vt220_request
*)
273 ((addr_t
) page
+ PAGE_SIZE
)) - 1;
274 request
->retry_count
= 0;
275 request
->sclp_req
.sccb
= page
;
276 /* SCCB goes at start of page */
277 sccb
= (struct sclp_vt220_sccb
*) page
;
278 memset((void *) sccb
, 0, sizeof(struct sclp_vt220_sccb
));
279 sccb
->header
.length
= sizeof(struct sclp_vt220_sccb
);
280 sccb
->header
.function_code
= SCLP_NORMAL_WRITE
;
281 sccb
->header
.response_code
= 0x0000;
282 sccb
->evbuf
.type
= EVTYP_VT220MSG
;
283 sccb
->evbuf
.length
= sizeof(struct evbuf_header
);
288 static inline unsigned int
289 sclp_vt220_space_left(struct sclp_vt220_request
*request
)
291 struct sclp_vt220_sccb
*sccb
;
292 sccb
= (struct sclp_vt220_sccb
*) request
->sclp_req
.sccb
;
293 return PAGE_SIZE
- sizeof(struct sclp_vt220_request
) -
297 static inline unsigned int
298 sclp_vt220_chars_stored(struct sclp_vt220_request
*request
)
300 struct sclp_vt220_sccb
*sccb
;
301 sccb
= (struct sclp_vt220_sccb
*) request
->sclp_req
.sccb
;
302 return sccb
->evbuf
.length
- sizeof(struct evbuf_header
);
306 * Add msg to buffer associated with request. Return the number of characters
310 sclp_vt220_add_msg(struct sclp_vt220_request
*request
,
311 const unsigned char *msg
, int count
, int convertlf
)
313 struct sclp_vt220_sccb
*sccb
;
319 if (count
> sclp_vt220_space_left(request
))
320 count
= sclp_vt220_space_left(request
);
324 sccb
= (struct sclp_vt220_sccb
*) request
->sclp_req
.sccb
;
325 buffer
= (void *) ((addr_t
) sccb
+ sccb
->header
.length
);
328 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
330 (from
< count
) && (to
< sclp_vt220_space_left(request
));
332 /* Retrieve character */
334 /* Perform conversion */
336 if (to
+ 1 < sclp_vt220_space_left(request
)) {
337 ((unsigned char *) buffer
)[to
++] = c
;
338 ((unsigned char *) buffer
)[to
++] = 0x0d;
343 ((unsigned char *) buffer
)[to
++] = c
;
345 sccb
->header
.length
+= to
;
346 sccb
->evbuf
.length
+= to
;
349 memcpy(buffer
, (const void *) msg
, count
);
350 sccb
->header
.length
+= count
;
351 sccb
->evbuf
.length
+= count
;
357 * Emit buffer after having waited long enough for more data to arrive.
360 sclp_vt220_timeout(unsigned long data
)
362 sclp_vt220_emit_current();
365 #define BUFFER_MAX_DELAY HZ/20
368 * Internal implementation of the write function. Write COUNT bytes of data
370 * to the SCLP interface. In case that the data does not fit into the current
371 * write buffer, emit the current one and allocate a new one. If there are no
372 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
373 * is non-zero, the buffer will be scheduled for emitting after a timeout -
374 * otherwise the user has to explicitly call the flush function.
375 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
376 * buffer should be converted to 0x0a 0x0d. After completion, return the number
380 __sclp_vt220_write(const unsigned char *buf
, int count
, int do_schedule
,
381 int convertlf
, int may_fail
)
391 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
393 /* Create an sclp output buffer if none exists yet */
394 if (sclp_vt220_current_request
== NULL
) {
395 while (list_empty(&sclp_vt220_empty
)) {
396 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
401 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
403 page
= (void *) sclp_vt220_empty
.next
;
404 list_del((struct list_head
*) page
);
405 sclp_vt220_current_request
=
406 sclp_vt220_initialize_page(page
);
408 /* Try to write the string to the current request buffer */
409 written
= sclp_vt220_add_msg(sclp_vt220_current_request
,
410 buf
, count
, convertlf
);
411 overall_written
+= written
;
412 if (written
== count
)
415 * Not all characters could be written to the current
416 * output buffer. Emit the buffer, create a new buffer
417 * and then output the rest of the string.
419 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
420 sclp_vt220_emit_current();
421 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
425 /* Setup timer to output current console buffer after some time */
426 if (sclp_vt220_current_request
!= NULL
&&
427 !timer_pending(&sclp_vt220_timer
) && do_schedule
) {
428 sclp_vt220_timer
.function
= sclp_vt220_timeout
;
429 sclp_vt220_timer
.data
= 0UL;
430 sclp_vt220_timer
.expires
= jiffies
+ BUFFER_MAX_DELAY
;
431 add_timer(&sclp_vt220_timer
);
433 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
435 return overall_written
;
439 * This routine is called by the kernel to write a series of
440 * characters to the tty device. The characters may come from
441 * user space or kernel space. This routine will return the
442 * number of characters actually accepted for writing.
445 sclp_vt220_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
447 return __sclp_vt220_write(buf
, count
, 1, 0, 1);
450 #define SCLP_VT220_SESSION_ENDED 0x01
451 #define SCLP_VT220_SESSION_STARTED 0x80
452 #define SCLP_VT220_SESSION_DATA 0x00
455 * Called by the SCLP to report incoming event buffers.
458 sclp_vt220_receiver_fn(struct evbuf_header
*evbuf
)
463 /* Ignore input if device is not open */
464 if (sclp_vt220_tty
== NULL
)
467 buffer
= (char *) ((addr_t
) evbuf
+ sizeof(struct evbuf_header
));
468 count
= evbuf
->length
- sizeof(struct evbuf_header
);
471 case SCLP_VT220_SESSION_ENDED
:
472 case SCLP_VT220_SESSION_STARTED
:
474 case SCLP_VT220_SESSION_DATA
:
475 /* Send input to line discipline */
478 tty_insert_flip_string(sclp_vt220_tty
, buffer
, count
);
479 tty_flip_buffer_push(sclp_vt220_tty
);
485 * This routine is called when a particular tty device is opened.
488 sclp_vt220_open(struct tty_struct
*tty
, struct file
*filp
)
490 if (tty
->count
== 1) {
491 sclp_vt220_tty
= tty
;
492 tty
->driver_data
= kmalloc(SCLP_VT220_BUF_SIZE
, GFP_KERNEL
);
493 if (tty
->driver_data
== NULL
)
495 tty
->low_latency
= 0;
501 * This routine is called when a particular tty device is closed.
504 sclp_vt220_close(struct tty_struct
*tty
, struct file
*filp
)
506 if (tty
->count
== 1) {
507 sclp_vt220_tty
= NULL
;
508 kfree(tty
->driver_data
);
509 tty
->driver_data
= NULL
;
514 * This routine is called by the kernel to write a single
515 * character to the tty device. If the kernel uses this routine,
516 * it must call the flush_chars() routine (if defined) when it is
517 * done stuffing characters into the driver.
520 sclp_vt220_put_char(struct tty_struct
*tty
, unsigned char ch
)
522 return __sclp_vt220_write(&ch
, 1, 0, 0, 1);
526 * This routine is called by the kernel after it has written a
527 * series of characters to the tty device using put_char().
530 sclp_vt220_flush_chars(struct tty_struct
*tty
)
532 if (sclp_vt220_outqueue_count
== 0)
533 sclp_vt220_emit_current();
535 sclp_vt220_flush_later
= 1;
539 * This routine returns the numbers of characters the tty driver
540 * will accept for queuing to be written. This number is subject
541 * to change as output buffers get emptied, or if the output flow
545 sclp_vt220_write_room(struct tty_struct
*tty
)
551 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
553 if (sclp_vt220_current_request
!= NULL
)
554 count
= sclp_vt220_space_left(sclp_vt220_current_request
);
555 list_for_each(l
, &sclp_vt220_empty
)
556 count
+= SCLP_VT220_MAX_CHARS_PER_BUFFER
;
557 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
562 * Return number of buffered chars.
565 sclp_vt220_chars_in_buffer(struct tty_struct
*tty
)
569 struct sclp_vt220_request
*r
;
572 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
574 if (sclp_vt220_current_request
!= NULL
)
575 count
= sclp_vt220_chars_stored(sclp_vt220_current_request
);
576 list_for_each(l
, &sclp_vt220_outqueue
) {
577 r
= list_entry(l
, struct sclp_vt220_request
, list
);
578 count
+= sclp_vt220_chars_stored(r
);
580 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
585 __sclp_vt220_flush_buffer(void)
589 sclp_vt220_emit_current();
590 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
591 if (timer_pending(&sclp_vt220_timer
))
592 del_timer(&sclp_vt220_timer
);
593 while (sclp_vt220_outqueue_count
> 0) {
594 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
596 spin_lock_irqsave(&sclp_vt220_lock
, flags
);
598 spin_unlock_irqrestore(&sclp_vt220_lock
, flags
);
602 * Pass on all buffers to the hardware. Return only when there are no more
606 sclp_vt220_flush_buffer(struct tty_struct
*tty
)
608 sclp_vt220_emit_current();
611 /* Release allocated pages. */
612 static void __init
__sclp_vt220_free_pages(void)
614 struct list_head
*page
, *p
;
616 list_for_each_safe(page
, p
, &sclp_vt220_empty
) {
618 if (slab_is_available())
619 free_page((unsigned long) page
);
621 free_bootmem((unsigned long) page
, PAGE_SIZE
);
625 /* Release memory and unregister from sclp core. Controlled by init counting -
626 * only the last invoker will actually perform these actions. */
627 static void __init
__sclp_vt220_cleanup(void)
629 sclp_vt220_init_count
--;
630 if (sclp_vt220_init_count
!= 0)
632 sclp_unregister(&sclp_vt220_register
);
633 __sclp_vt220_free_pages();
636 /* Allocate buffer pages and register with sclp core. Controlled by init
637 * counting - only the first invoker will actually perform these actions. */
638 static int __init
__sclp_vt220_init(int num_pages
)
644 sclp_vt220_init_count
++;
645 if (sclp_vt220_init_count
!= 1)
647 spin_lock_init(&sclp_vt220_lock
);
648 INIT_LIST_HEAD(&sclp_vt220_empty
);
649 INIT_LIST_HEAD(&sclp_vt220_outqueue
);
650 init_timer(&sclp_vt220_timer
);
651 sclp_vt220_current_request
= NULL
;
652 sclp_vt220_buffered_chars
= 0;
653 sclp_vt220_outqueue_count
= 0;
654 sclp_vt220_tty
= NULL
;
655 sclp_vt220_flush_later
= 0;
657 /* Allocate pages for output buffering */
658 for (i
= 0; i
< num_pages
; i
++) {
659 if (slab_is_available())
660 page
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
662 page
= alloc_bootmem_low_pages(PAGE_SIZE
);
667 list_add_tail((struct list_head
*) page
, &sclp_vt220_empty
);
669 rc
= sclp_register(&sclp_vt220_register
);
672 __sclp_vt220_free_pages();
673 sclp_vt220_init_count
--;
678 static const struct tty_operations sclp_vt220_ops
= {
679 .open
= sclp_vt220_open
,
680 .close
= sclp_vt220_close
,
681 .write
= sclp_vt220_write
,
682 .put_char
= sclp_vt220_put_char
,
683 .flush_chars
= sclp_vt220_flush_chars
,
684 .write_room
= sclp_vt220_write_room
,
685 .chars_in_buffer
= sclp_vt220_chars_in_buffer
,
686 .flush_buffer
= sclp_vt220_flush_buffer
,
690 * Register driver with SCLP and Linux and initialize internal tty structures.
692 static int __init
sclp_vt220_tty_init(void)
694 struct tty_driver
*driver
;
697 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
698 * symmetry between VM and LPAR systems regarding ttyS1. */
699 driver
= alloc_tty_driver(1);
702 rc
= __sclp_vt220_init(MAX_KMEM_PAGES
);
706 driver
->owner
= THIS_MODULE
;
707 driver
->driver_name
= SCLP_VT220_DRIVER_NAME
;
708 driver
->name
= SCLP_VT220_DEVICE_NAME
;
709 driver
->major
= SCLP_VT220_MAJOR
;
710 driver
->minor_start
= SCLP_VT220_MINOR
;
711 driver
->type
= TTY_DRIVER_TYPE_SYSTEM
;
712 driver
->subtype
= SYSTEM_TYPE_TTY
;
713 driver
->init_termios
= tty_std_termios
;
714 driver
->flags
= TTY_DRIVER_REAL_RAW
;
715 tty_set_operations(driver
, &sclp_vt220_ops
);
717 rc
= tty_register_driver(driver
);
720 sclp_vt220_driver
= driver
;
724 __sclp_vt220_cleanup();
726 put_tty_driver(driver
);
729 __initcall(sclp_vt220_tty_init
);
731 #ifdef CONFIG_SCLP_VT220_CONSOLE
734 sclp_vt220_con_write(struct console
*con
, const char *buf
, unsigned int count
)
736 __sclp_vt220_write((const unsigned char *) buf
, count
, 1, 1, 0);
739 static struct tty_driver
*
740 sclp_vt220_con_device(struct console
*c
, int *index
)
743 return sclp_vt220_driver
;
747 * This routine is called from panic when the kernel is going to give up.
748 * We have to make sure that all buffers will be flushed to the SCLP.
749 * Note that this function may be called from within an interrupt context.
752 sclp_vt220_con_unblank(void)
754 __sclp_vt220_flush_buffer();
757 /* Structure needed to register with printk */
758 static struct console sclp_vt220_console
=
760 .name
= SCLP_VT220_CONSOLE_NAME
,
761 .write
= sclp_vt220_con_write
,
762 .device
= sclp_vt220_con_device
,
763 .unblank
= sclp_vt220_con_unblank
,
764 .flags
= CON_PRINTBUFFER
,
765 .index
= SCLP_VT220_CONSOLE_INDEX
769 sclp_vt220_con_init(void)
773 if (!CONSOLE_IS_SCLP
)
775 rc
= __sclp_vt220_init(MAX_CONSOLE_PAGES
);
778 /* Attach linux console */
779 register_console(&sclp_vt220_console
);
783 console_initcall(sclp_vt220_con_init
);
784 #endif /* CONFIG_SCLP_VT220_CONSOLE */