9 #include <linux/module.h>
11 #include <linux/list.h>
12 #include <linux/wait.h>
14 #include <linux/kernel.h>
15 #include <linux/tty.h>
18 #include <linux/errno.h>
22 #include <linux/kdev_t.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
51 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
52 sizeof(struct sclp_vt220_request) - \
53 sizeof(struct sclp_vt220_sccb))
58 static struct tty_port sclp_vt220_port;
67 static struct list_head sclp_vt220_outqueue;
70 static int sclp_vt220_suspended;
73 static int sclp_vt220_queue_running;
84 static int sclp_vt220_buffered_chars;
92 static int sclp_vt220_flush_later;
98 static void sclp_vt220_emit_current(
void);
104 .state_change_fn =
NULL,
105 .receiver_fn = sclp_vt220_receiver_fn,
106 .pm_event_fn = sclp_vt220_pm_event_fn,
130 if (!list_empty(&sclp_vt220_outqueue))
131 request =
list_entry(sclp_vt220_outqueue.next,
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();
150 #define SCLP_BUFFER_MAX_RETRY 1
157 sclp_vt220_callback(
struct sclp_req *request,
void *
data)
164 sclp_vt220_process_queue(vt220_request);
170 switch (sccb->
header.response_code) {
183 sccb->
header.response_code = 0x0000;
193 sccb->
header.response_code = 0x0000;
202 sclp_vt220_process_queue(vt220_request);
218 request->
sclp_req.callback = sclp_vt220_callback;
219 request->
sclp_req.callback_data = (
void *) request;
228 sclp_vt220_emit_current(
void)
235 if (sclp_vt220_current_request) {
237 sclp_vt220_current_request->
sclp_req.sccb;
241 &sclp_vt220_outqueue);
242 sclp_vt220_current_request =
NULL;
243 if (timer_pending(&sclp_vt220_timer))
246 sclp_vt220_flush_later = 0;
248 if (sclp_vt220_queue_running || sclp_vt220_suspended)
250 if (list_empty(&sclp_vt220_outqueue))
254 sclp_vt220_queue_running = 1;
255 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
257 if (__sclp_vt220_emit(request))
258 sclp_vt220_process_queue(request);
261 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
264 #define SCLP_NORMAL_WRITE 0x00
270 sclp_vt220_initialize_page(
void *page)
285 sccb->
header.response_code = 0x0000;
292 static inline unsigned int
301 static inline unsigned int
315 const unsigned char *
msg,
int count,
int convertlf)
323 if (count > sclp_vt220_space_left(request))
324 count = sclp_vt220_space_left(request);
329 buffer = (
void *) ((
addr_t) sccb + sccb->
header.length);
334 (from <
count) && (to < sclp_vt220_space_left(request));
340 if (to + 1 < sclp_vt220_space_left(request)) {
341 ((
unsigned char *) buffer)[to++] =
c;
342 ((
unsigned char *) buffer)[to++] = 0x0d;
347 ((
unsigned char *)
buffer)[to++] = c;
349 sccb->
header.length += to;
350 sccb->
evbuf.length += to;
353 memcpy(buffer, (
const void *) msg, count);
364 sclp_vt220_timeout(
unsigned long data)
366 sclp_vt220_emit_current();
369 #define BUFFER_MAX_DELAY HZ/20
384 __sclp_vt220_write(
const unsigned char *
buf,
int count,
int do_schedule,
385 int convertlf,
int may_fail)
398 if (sclp_vt220_current_request ==
NULL) {
399 while (list_empty(&sclp_vt220_empty)) {
400 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
401 if (may_fail || sclp_vt220_suspended)
407 page = (
void *) sclp_vt220_empty.next;
409 sclp_vt220_current_request =
410 sclp_vt220_initialize_page(page);
413 written = sclp_vt220_add_msg(sclp_vt220_current_request,
414 buf, count, convertlf);
415 overall_written += written;
416 if (written == count)
423 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
424 sclp_vt220_emit_current();
430 if (sclp_vt220_current_request !=
NULL &&
431 !timer_pending(&sclp_vt220_timer) && do_schedule) {
432 sclp_vt220_timer.function = sclp_vt220_timeout;
433 sclp_vt220_timer.data = 0
UL;
437 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
439 return overall_written;
449 sclp_vt220_write(
struct tty_struct *tty,
const unsigned char *
buf,
int count)
451 return __sclp_vt220_write(buf, count, 1, 0, 1);
454 #define SCLP_VT220_SESSION_ENDED 0x01
455 #define SCLP_VT220_SESSION_STARTED 0x80
456 #define SCLP_VT220_SESSION_DATA 0x00
483 tty_insert_flip_string(tty, buffer, count);
496 if (tty->
count == 1) {
524 sclp_vt220_put_char(
struct tty_struct *tty,
unsigned char ch)
526 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
534 sclp_vt220_flush_chars(
struct tty_struct *tty)
536 if (!sclp_vt220_queue_running)
537 sclp_vt220_emit_current();
539 sclp_vt220_flush_later = 1;
557 if (sclp_vt220_current_request !=
NULL)
558 count = sclp_vt220_space_left(sclp_vt220_current_request);
561 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
578 if (sclp_vt220_current_request !=
NULL)
579 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
582 count += sclp_vt220_chars_stored(r);
584 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
593 sclp_vt220_flush_buffer(
struct tty_struct *tty)
595 sclp_vt220_emit_current();
599 static void __init __sclp_vt220_free_pages(
void)
611 static void __init __sclp_vt220_cleanup(
void)
613 sclp_vt220_init_count--;
614 if (sclp_vt220_init_count != 0)
617 __sclp_vt220_free_pages();
628 sclp_vt220_init_count++;
629 if (sclp_vt220_init_count != 1)
632 INIT_LIST_HEAD(&sclp_vt220_empty);
633 INIT_LIST_HEAD(&sclp_vt220_outqueue);
636 sclp_vt220_current_request =
NULL;
637 sclp_vt220_buffered_chars = 0;
638 sclp_vt220_flush_later = 0;
651 __sclp_vt220_free_pages();
652 sclp_vt220_init_count--;
658 .open = sclp_vt220_open,
659 .close = sclp_vt220_close,
660 .write = sclp_vt220_write,
661 .put_char = sclp_vt220_put_char,
662 .flush_chars = sclp_vt220_flush_chars,
663 .write_room = sclp_vt220_write_room,
664 .chars_in_buffer = sclp_vt220_chars_in_buffer,
665 .flush_buffer = sclp_vt220_flush_buffer,
671 static int __init sclp_vt220_tty_init(
void)
678 driver = alloc_tty_driver(1);
699 sclp_vt220_driver =
driver;
703 __sclp_vt220_cleanup();
710 static void __sclp_vt220_flush_buffer(
void)
714 sclp_vt220_emit_current();
716 if (timer_pending(&sclp_vt220_timer))
718 while (sclp_vt220_queue_running) {
719 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
723 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
729 static void sclp_vt220_resume(
void)
734 sclp_vt220_suspended = 0;
735 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
736 sclp_vt220_emit_current();
742 static void sclp_vt220_suspend(
void)
747 sclp_vt220_suspended = 1;
748 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
749 __sclp_vt220_flush_buffer();
755 switch (sclp_pm_event) {
757 sclp_vt220_suspend();
766 #ifdef CONFIG_SCLP_VT220_CONSOLE
769 sclp_vt220_con_write(
struct console *
con,
const char *
buf,
unsigned int count)
771 __sclp_vt220_write((
const unsigned char *) buf, count, 1, 1, 0);
778 return sclp_vt220_driver;
785 __sclp_vt220_flush_buffer();
800 static struct console sclp_vt220_console =
803 .write = sclp_vt220_con_write,
804 .device = sclp_vt220_con_device,
810 sclp_vt220_con_init(
void)