1
2
3
4
5
6
7
8
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>
19#include <linux/mm.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
27#include <asm/uaccess.h>
28#include "sclp.h"
29
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
36#define SCLP_VT220_BUF_SIZE 80
37
38
39struct sclp_vt220_request {
40 struct list_head list;
41 struct sclp_req sclp_req;
42 int retry_count;
43};
44
45
46struct sclp_vt220_sccb {
47 struct sccb_header header;
48 struct evbuf_header evbuf;
49};
50
51#define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
52 sizeof(struct sclp_vt220_request) - \
53 sizeof(struct sclp_vt220_sccb))
54
55
56static struct tty_driver *sclp_vt220_driver;
57
58
59static struct tty_struct *sclp_vt220_tty;
60
61
62static spinlock_t sclp_vt220_lock;
63
64
65static struct list_head sclp_vt220_empty;
66
67
68static struct list_head sclp_vt220_outqueue;
69
70
71static int sclp_vt220_suspended;
72
73
74static int sclp_vt220_queue_running;
75
76
77
78static struct timer_list sclp_vt220_timer;
79
80
81
82static struct sclp_vt220_request *sclp_vt220_current_request;
83
84
85static int sclp_vt220_buffered_chars;
86
87
88static int __initdata sclp_vt220_init_count;
89
90
91
92
93static int sclp_vt220_flush_later;
94
95static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
96static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
97 enum sclp_pm_event sclp_pm_event);
98static int __sclp_vt220_emit(struct sclp_vt220_request *request);
99static void sclp_vt220_emit_current(void);
100
101
102static struct sclp_register sclp_vt220_register = {
103 .send_mask = EVTYP_VT220MSG_MASK,
104 .receive_mask = EVTYP_VT220MSG_MASK,
105 .state_change_fn = NULL,
106 .receiver_fn = sclp_vt220_receiver_fn,
107 .pm_event_fn = sclp_vt220_pm_event_fn,
108};
109
110
111
112
113
114
115static void
116sclp_vt220_process_queue(struct sclp_vt220_request *request)
117{
118 unsigned long flags;
119 void *page;
120
121 do {
122
123 page = request->sclp_req.sccb;
124 spin_lock_irqsave(&sclp_vt220_lock, flags);
125
126 list_del(&request->list);
127 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
128
129 request = NULL;
130 if (!list_empty(&sclp_vt220_outqueue))
131 request = list_entry(sclp_vt220_outqueue.next,
132 struct sclp_vt220_request, list);
133 if (!request || sclp_vt220_suspended) {
134 sclp_vt220_queue_running = 0;
135 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
136 break;
137 }
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();
142
143 if (sclp_vt220_tty != NULL) {
144 tty_wakeup(sclp_vt220_tty);
145 }
146}
147
148#define SCLP_BUFFER_MAX_RETRY 1
149
150
151
152
153
154static void
155sclp_vt220_callback(struct sclp_req *request, void *data)
156{
157 struct sclp_vt220_request *vt220_request;
158 struct sclp_vt220_sccb *sccb;
159
160 vt220_request = (struct sclp_vt220_request *) data;
161 if (request->status == SCLP_REQ_FAILED) {
162 sclp_vt220_process_queue(vt220_request);
163 return;
164 }
165 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
166
167
168 switch (sccb->header.response_code) {
169 case 0x0020 :
170 break;
171
172 case 0x05f0:
173 break;
174
175 case 0x0340:
176 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
177 break;
178
179 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
180
181 sccb->header.response_code = 0x0000;
182 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
183 if (sclp_add_request(request) == 0)
184 return;
185 }
186 break;
187
188 case 0x0040:
189 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
190 break;
191 sccb->header.response_code = 0x0000;
192 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
193 if (sclp_add_request(request) == 0)
194 return;
195 break;
196
197 default:
198 break;
199 }
200 sclp_vt220_process_queue(vt220_request);
201}
202
203
204
205
206
207static int
208__sclp_vt220_emit(struct sclp_vt220_request *request)
209{
210 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
211 request->sclp_req.status = SCLP_REQ_FAILED;
212 return -EIO;
213 }
214 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
215 request->sclp_req.status = SCLP_REQ_FILLED;
216 request->sclp_req.callback = sclp_vt220_callback;
217 request->sclp_req.callback_data = (void *) request;
218
219 return sclp_add_request(&request->sclp_req);
220}
221
222
223
224
225static void
226sclp_vt220_emit_current(void)
227{
228 unsigned long flags;
229 struct sclp_vt220_request *request;
230 struct sclp_vt220_sccb *sccb;
231
232 spin_lock_irqsave(&sclp_vt220_lock, flags);
233 if (sclp_vt220_current_request) {
234 sccb = (struct sclp_vt220_sccb *)
235 sclp_vt220_current_request->sclp_req.sccb;
236
237 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
238 list_add_tail(&sclp_vt220_current_request->list,
239 &sclp_vt220_outqueue);
240 sclp_vt220_current_request = NULL;
241 if (timer_pending(&sclp_vt220_timer))
242 del_timer(&sclp_vt220_timer);
243 }
244 sclp_vt220_flush_later = 0;
245 }
246 if (sclp_vt220_queue_running || sclp_vt220_suspended)
247 goto out_unlock;
248 if (list_empty(&sclp_vt220_outqueue))
249 goto out_unlock;
250 request = list_first_entry(&sclp_vt220_outqueue,
251 struct sclp_vt220_request, list);
252 sclp_vt220_queue_running = 1;
253 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
254
255 if (__sclp_vt220_emit(request))
256 sclp_vt220_process_queue(request);
257 return;
258out_unlock:
259 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
260}
261
262#define SCLP_NORMAL_WRITE 0x00
263
264
265
266
267static struct sclp_vt220_request *
268sclp_vt220_initialize_page(void *page)
269{
270 struct sclp_vt220_request *request;
271 struct sclp_vt220_sccb *sccb;
272
273
274 request = ((struct sclp_vt220_request *)
275 ((addr_t) page + PAGE_SIZE)) - 1;
276 request->retry_count = 0;
277 request->sclp_req.sccb = page;
278
279 sccb = (struct sclp_vt220_sccb *) page;
280 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
281 sccb->header.length = sizeof(struct sclp_vt220_sccb);
282 sccb->header.function_code = SCLP_NORMAL_WRITE;
283 sccb->header.response_code = 0x0000;
284 sccb->evbuf.type = EVTYP_VT220MSG;
285 sccb->evbuf.length = sizeof(struct evbuf_header);
286
287 return request;
288}
289
290static inline unsigned int
291sclp_vt220_space_left(struct sclp_vt220_request *request)
292{
293 struct sclp_vt220_sccb *sccb;
294 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
295 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
296 sccb->header.length;
297}
298
299static inline unsigned int
300sclp_vt220_chars_stored(struct sclp_vt220_request *request)
301{
302 struct sclp_vt220_sccb *sccb;
303 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
304 return sccb->evbuf.length - sizeof(struct evbuf_header);
305}
306
307
308
309
310
311static int
312sclp_vt220_add_msg(struct sclp_vt220_request *request,
313 const unsigned char *msg, int count, int convertlf)
314{
315 struct sclp_vt220_sccb *sccb;
316 void *buffer;
317 unsigned char c;
318 int from;
319 int to;
320
321 if (count > sclp_vt220_space_left(request))
322 count = sclp_vt220_space_left(request);
323 if (count <= 0)
324 return 0;
325
326 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
327 buffer = (void *) ((addr_t) sccb + sccb->header.length);
328
329 if (convertlf) {
330
331 for (from=0, to=0;
332 (from < count) && (to < sclp_vt220_space_left(request));
333 from++) {
334
335 c = msg[from];
336
337 if (c == 0x0a) {
338 if (to + 1 < sclp_vt220_space_left(request)) {
339 ((unsigned char *) buffer)[to++] = c;
340 ((unsigned char *) buffer)[to++] = 0x0d;
341 } else
342 break;
343
344 } else
345 ((unsigned char *) buffer)[to++] = c;
346 }
347 sccb->header.length += to;
348 sccb->evbuf.length += to;
349 return from;
350 } else {
351 memcpy(buffer, (const void *) msg, count);
352 sccb->header.length += count;
353 sccb->evbuf.length += count;
354 return count;
355 }
356}
357
358
359
360
361static void
362sclp_vt220_timeout(unsigned long data)
363{
364 sclp_vt220_emit_current();
365}
366
367#define BUFFER_MAX_DELAY HZ/20
368
369
370
371
372
373
374
375
376
377
378
379
380
381static int
382__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
383 int convertlf, int may_fail)
384{
385 unsigned long flags;
386 void *page;
387 int written;
388 int overall_written;
389
390 if (count <= 0)
391 return 0;
392 overall_written = 0;
393 spin_lock_irqsave(&sclp_vt220_lock, flags);
394 do {
395
396 if (sclp_vt220_current_request == NULL) {
397 while (list_empty(&sclp_vt220_empty)) {
398 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
399 if (may_fail || sclp_vt220_suspended)
400 goto out;
401 else
402 sclp_sync_wait();
403 spin_lock_irqsave(&sclp_vt220_lock, flags);
404 }
405 page = (void *) sclp_vt220_empty.next;
406 list_del((struct list_head *) page);
407 sclp_vt220_current_request =
408 sclp_vt220_initialize_page(page);
409 }
410
411 written = sclp_vt220_add_msg(sclp_vt220_current_request,
412 buf, count, convertlf);
413 overall_written += written;
414 if (written == count)
415 break;
416
417
418
419
420
421 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
422 sclp_vt220_emit_current();
423 spin_lock_irqsave(&sclp_vt220_lock, flags);
424 buf += written;
425 count -= written;
426 } while (count > 0);
427
428 if (sclp_vt220_current_request != NULL &&
429 !timer_pending(&sclp_vt220_timer) && do_schedule) {
430 sclp_vt220_timer.function = sclp_vt220_timeout;
431 sclp_vt220_timer.data = 0UL;
432 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
433 add_timer(&sclp_vt220_timer);
434 }
435 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
436out:
437 return overall_written;
438}
439
440
441
442
443
444
445
446static int
447sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
448{
449 return __sclp_vt220_write(buf, count, 1, 0, 1);
450}
451
452#define SCLP_VT220_SESSION_ENDED 0x01
453#define SCLP_VT220_SESSION_STARTED 0x80
454#define SCLP_VT220_SESSION_DATA 0x00
455
456
457
458
459static void
460sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
461{
462 char *buffer;
463 unsigned int count;
464
465
466 if (sclp_vt220_tty == NULL)
467 return;
468
469 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
470 count = evbuf->length - sizeof(struct evbuf_header);
471
472 switch (*buffer) {
473 case SCLP_VT220_SESSION_ENDED:
474 case SCLP_VT220_SESSION_STARTED:
475 break;
476 case SCLP_VT220_SESSION_DATA:
477
478 buffer++;
479 count--;
480 tty_insert_flip_string(sclp_vt220_tty, buffer, count);
481 tty_flip_buffer_push(sclp_vt220_tty);
482 break;
483 }
484}
485
486
487
488
489static int
490sclp_vt220_open(struct tty_struct *tty, struct file *filp)
491{
492 if (tty->count == 1) {
493 sclp_vt220_tty = tty;
494 tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
495 if (tty->driver_data == NULL)
496 return -ENOMEM;
497 tty->low_latency = 0;
498 }
499 return 0;
500}
501
502
503
504
505static void
506sclp_vt220_close(struct tty_struct *tty, struct file *filp)
507{
508 if (tty->count == 1) {
509 sclp_vt220_tty = NULL;
510 kfree(tty->driver_data);
511 tty->driver_data = NULL;
512 }
513}
514
515
516
517
518
519
520
521static int
522sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
523{
524 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
525}
526
527
528
529
530
531static void
532sclp_vt220_flush_chars(struct tty_struct *tty)
533{
534 if (!sclp_vt220_queue_running)
535 sclp_vt220_emit_current();
536 else
537 sclp_vt220_flush_later = 1;
538}
539
540
541
542
543
544
545
546static int
547sclp_vt220_write_room(struct tty_struct *tty)
548{
549 unsigned long flags;
550 struct list_head *l;
551 int count;
552
553 spin_lock_irqsave(&sclp_vt220_lock, flags);
554 count = 0;
555 if (sclp_vt220_current_request != NULL)
556 count = sclp_vt220_space_left(sclp_vt220_current_request);
557 list_for_each(l, &sclp_vt220_empty)
558 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
559 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
560 return count;
561}
562
563
564
565
566static int
567sclp_vt220_chars_in_buffer(struct tty_struct *tty)
568{
569 unsigned long flags;
570 struct list_head *l;
571 struct sclp_vt220_request *r;
572 int count;
573
574 spin_lock_irqsave(&sclp_vt220_lock, flags);
575 count = 0;
576 if (sclp_vt220_current_request != NULL)
577 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
578 list_for_each(l, &sclp_vt220_outqueue) {
579 r = list_entry(l, struct sclp_vt220_request, list);
580 count += sclp_vt220_chars_stored(r);
581 }
582 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
583 return count;
584}
585
586
587
588
589
590static void
591sclp_vt220_flush_buffer(struct tty_struct *tty)
592{
593 sclp_vt220_emit_current();
594}
595
596
597static void __init __sclp_vt220_free_pages(void)
598{
599 struct list_head *page, *p;
600
601 list_for_each_safe(page, p, &sclp_vt220_empty) {
602 list_del(page);
603 free_page((unsigned long) page);
604 }
605}
606
607
608
609static void __init __sclp_vt220_cleanup(void)
610{
611 sclp_vt220_init_count--;
612 if (sclp_vt220_init_count != 0)
613 return;
614 sclp_unregister(&sclp_vt220_register);
615 __sclp_vt220_free_pages();
616}
617
618
619
620static int __init __sclp_vt220_init(int num_pages)
621{
622 void *page;
623 int i;
624 int rc;
625
626 sclp_vt220_init_count++;
627 if (sclp_vt220_init_count != 1)
628 return 0;
629 spin_lock_init(&sclp_vt220_lock);
630 INIT_LIST_HEAD(&sclp_vt220_empty);
631 INIT_LIST_HEAD(&sclp_vt220_outqueue);
632 init_timer(&sclp_vt220_timer);
633 sclp_vt220_current_request = NULL;
634 sclp_vt220_buffered_chars = 0;
635 sclp_vt220_tty = NULL;
636 sclp_vt220_flush_later = 0;
637
638
639 rc = -ENOMEM;
640 for (i = 0; i < num_pages; i++) {
641 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
642 if (!page)
643 goto out;
644 list_add_tail(page, &sclp_vt220_empty);
645 }
646 rc = sclp_register(&sclp_vt220_register);
647out:
648 if (rc) {
649 __sclp_vt220_free_pages();
650 sclp_vt220_init_count--;
651 }
652 return rc;
653}
654
655static const struct tty_operations sclp_vt220_ops = {
656 .open = sclp_vt220_open,
657 .close = sclp_vt220_close,
658 .write = sclp_vt220_write,
659 .put_char = sclp_vt220_put_char,
660 .flush_chars = sclp_vt220_flush_chars,
661 .write_room = sclp_vt220_write_room,
662 .chars_in_buffer = sclp_vt220_chars_in_buffer,
663 .flush_buffer = sclp_vt220_flush_buffer,
664};
665
666
667
668
669static int __init sclp_vt220_tty_init(void)
670{
671 struct tty_driver *driver;
672 int rc;
673
674
675
676 driver = alloc_tty_driver(1);
677 if (!driver)
678 return -ENOMEM;
679 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
680 if (rc)
681 goto out_driver;
682
683 driver->owner = THIS_MODULE;
684 driver->driver_name = SCLP_VT220_DRIVER_NAME;
685 driver->name = SCLP_VT220_DEVICE_NAME;
686 driver->major = SCLP_VT220_MAJOR;
687 driver->minor_start = SCLP_VT220_MINOR;
688 driver->type = TTY_DRIVER_TYPE_SYSTEM;
689 driver->subtype = SYSTEM_TYPE_TTY;
690 driver->init_termios = tty_std_termios;
691 driver->flags = TTY_DRIVER_REAL_RAW;
692 tty_set_operations(driver, &sclp_vt220_ops);
693
694 rc = tty_register_driver(driver);
695 if (rc)
696 goto out_init;
697 sclp_vt220_driver = driver;
698 return 0;
699
700out_init:
701 __sclp_vt220_cleanup();
702out_driver:
703 put_tty_driver(driver);
704 return rc;
705}
706__initcall(sclp_vt220_tty_init);
707
708static void __sclp_vt220_flush_buffer(void)
709{
710 unsigned long flags;
711
712 sclp_vt220_emit_current();
713 spin_lock_irqsave(&sclp_vt220_lock, flags);
714 if (timer_pending(&sclp_vt220_timer))
715 del_timer(&sclp_vt220_timer);
716 while (sclp_vt220_queue_running) {
717 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
718 sclp_sync_wait();
719 spin_lock_irqsave(&sclp_vt220_lock, flags);
720 }
721 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
722}
723
724
725
726
727static void sclp_vt220_resume(void)
728{
729 unsigned long flags;
730
731 spin_lock_irqsave(&sclp_vt220_lock, flags);
732 sclp_vt220_suspended = 0;
733 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
734 sclp_vt220_emit_current();
735}
736
737
738
739
740static void sclp_vt220_suspend(void)
741{
742 unsigned long flags;
743
744 spin_lock_irqsave(&sclp_vt220_lock, flags);
745 sclp_vt220_suspended = 1;
746 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
747 __sclp_vt220_flush_buffer();
748}
749
750static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
751 enum sclp_pm_event sclp_pm_event)
752{
753 switch (sclp_pm_event) {
754 case SCLP_PM_EVENT_FREEZE:
755 sclp_vt220_suspend();
756 break;
757 case SCLP_PM_EVENT_RESTORE:
758 case SCLP_PM_EVENT_THAW:
759 sclp_vt220_resume();
760 break;
761 }
762}
763
764#ifdef CONFIG_SCLP_VT220_CONSOLE
765
766static void
767sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
768{
769 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
770}
771
772static struct tty_driver *
773sclp_vt220_con_device(struct console *c, int *index)
774{
775 *index = 0;
776 return sclp_vt220_driver;
777}
778
779static int
780sclp_vt220_notify(struct notifier_block *self,
781 unsigned long event, void *data)
782{
783 __sclp_vt220_flush_buffer();
784 return NOTIFY_OK;
785}
786
787static struct notifier_block on_panic_nb = {
788 .notifier_call = sclp_vt220_notify,
789 .priority = 1,
790};
791
792static struct notifier_block on_reboot_nb = {
793 .notifier_call = sclp_vt220_notify,
794 .priority = 1,
795};
796
797
798static struct console sclp_vt220_console =
799{
800 .name = SCLP_VT220_CONSOLE_NAME,
801 .write = sclp_vt220_con_write,
802 .device = sclp_vt220_con_device,
803 .flags = CON_PRINTBUFFER,
804 .index = SCLP_VT220_CONSOLE_INDEX
805};
806
807static int __init
808sclp_vt220_con_init(void)
809{
810 int rc;
811
812 if (!CONSOLE_IS_SCLP)
813 return 0;
814 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
815 if (rc)
816 return rc;
817
818 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
819 register_reboot_notifier(&on_reboot_nb);
820 register_console(&sclp_vt220_console);
821 return 0;
822}
823
824console_initcall(sclp_vt220_con_init);
825#endif
826
827