1
2
3
4
5
6
7
8
9
10
11#include <linux/kmod.h>
12#include <linux/tty.h>
13#include <linux/tty_driver.h>
14#include <linux/tty_flip.h>
15#include <linux/err.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/gfp.h>
19#include <linux/uaccess.h>
20
21#include "ctrlchar.h"
22#include "sclp.h"
23#include "sclp_rw.h"
24#include "sclp_tty.h"
25
26
27
28
29
30#define SCLP_TTY_BUF_SIZE 512
31
32
33
34
35
36
37
38static spinlock_t sclp_tty_lock;
39
40static struct list_head sclp_tty_pages;
41
42static struct list_head sclp_tty_outqueue;
43
44static int sclp_tty_buffer_count;
45
46static struct sclp_buffer *sclp_ttybuf;
47
48static struct timer_list sclp_tty_timer;
49
50static struct tty_port sclp_port;
51static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
52static unsigned short int sclp_tty_chars_count;
53
54struct tty_driver *sclp_tty_driver;
55
56static int sclp_tty_tolower;
57static int sclp_tty_columns = 80;
58
59#define SPACES_PER_TAB 8
60#define CASE_DELIMITER 0x6c
61
62
63static int
64sclp_tty_open(struct tty_struct *tty, struct file *filp)
65{
66 tty_port_tty_set(&sclp_port, tty);
67 tty->driver_data = NULL;
68 sclp_port.low_latency = 0;
69 return 0;
70}
71
72
73static void
74sclp_tty_close(struct tty_struct *tty, struct file *filp)
75{
76 if (tty->count > 1)
77 return;
78 tty_port_tty_set(&sclp_port, NULL);
79}
80
81
82
83
84
85
86
87
88
89
90static int
91sclp_tty_write_room (struct tty_struct *tty)
92{
93 unsigned long flags;
94 struct list_head *l;
95 int count;
96
97 spin_lock_irqsave(&sclp_tty_lock, flags);
98 count = 0;
99 if (sclp_ttybuf != NULL)
100 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct msg_buf);
101 list_for_each(l, &sclp_tty_pages)
102 count += NR_EMPTY_MSG_PER_SCCB;
103 spin_unlock_irqrestore(&sclp_tty_lock, flags);
104 return count;
105}
106
107static void
108sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
109{
110 unsigned long flags;
111 void *page;
112
113 do {
114 page = sclp_unmake_buffer(buffer);
115 spin_lock_irqsave(&sclp_tty_lock, flags);
116
117 list_del(&buffer->list);
118 sclp_tty_buffer_count--;
119 list_add_tail((struct list_head *) page, &sclp_tty_pages);
120
121 buffer = NULL;
122 if (!list_empty(&sclp_tty_outqueue))
123 buffer = list_entry(sclp_tty_outqueue.next,
124 struct sclp_buffer, list);
125 spin_unlock_irqrestore(&sclp_tty_lock, flags);
126 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
127
128 tty_port_tty_wakeup(&sclp_port);
129}
130
131static inline void
132__sclp_ttybuf_emit(struct sclp_buffer *buffer)
133{
134 unsigned long flags;
135 int count;
136 int rc;
137
138 spin_lock_irqsave(&sclp_tty_lock, flags);
139 list_add_tail(&buffer->list, &sclp_tty_outqueue);
140 count = sclp_tty_buffer_count++;
141 spin_unlock_irqrestore(&sclp_tty_lock, flags);
142 if (count)
143 return;
144 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
145 if (rc)
146 sclp_ttybuf_callback(buffer, rc);
147}
148
149
150
151
152
153static void
154sclp_tty_timeout(struct timer_list *unused)
155{
156 unsigned long flags;
157 struct sclp_buffer *buf;
158
159 spin_lock_irqsave(&sclp_tty_lock, flags);
160 buf = sclp_ttybuf;
161 sclp_ttybuf = NULL;
162 spin_unlock_irqrestore(&sclp_tty_lock, flags);
163
164 if (buf != NULL) {
165 __sclp_ttybuf_emit(buf);
166 }
167}
168
169
170
171
172static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
173{
174 unsigned long flags;
175 void *page;
176 int written;
177 int overall_written;
178 struct sclp_buffer *buf;
179
180 if (count <= 0)
181 return 0;
182 overall_written = 0;
183 spin_lock_irqsave(&sclp_tty_lock, flags);
184 do {
185
186 if (sclp_ttybuf == NULL) {
187 while (list_empty(&sclp_tty_pages)) {
188 spin_unlock_irqrestore(&sclp_tty_lock, flags);
189 if (may_fail)
190 goto out;
191 else
192 sclp_sync_wait();
193 spin_lock_irqsave(&sclp_tty_lock, flags);
194 }
195 page = sclp_tty_pages.next;
196 list_del((struct list_head *) page);
197 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
198 SPACES_PER_TAB);
199 }
200
201 written = sclp_write(sclp_ttybuf, str, count);
202 overall_written += written;
203 if (written == count)
204 break;
205
206
207
208
209
210 buf = sclp_ttybuf;
211 sclp_ttybuf = NULL;
212 spin_unlock_irqrestore(&sclp_tty_lock, flags);
213 __sclp_ttybuf_emit(buf);
214 spin_lock_irqsave(&sclp_tty_lock, flags);
215 str += written;
216 count -= written;
217 } while (count > 0);
218
219 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
220 !timer_pending(&sclp_tty_timer)) {
221 mod_timer(&sclp_tty_timer, jiffies + HZ / 10);
222 }
223 spin_unlock_irqrestore(&sclp_tty_lock, flags);
224out:
225 return overall_written;
226}
227
228
229
230
231
232
233static int
234sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
235{
236 if (sclp_tty_chars_count > 0) {
237 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
238 sclp_tty_chars_count = 0;
239 }
240 return sclp_tty_write_string(buf, count, 1);
241}
242
243
244
245
246
247
248
249
250
251
252
253static int
254sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
255{
256 sclp_tty_chars[sclp_tty_chars_count++] = ch;
257 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
258 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
259 sclp_tty_chars_count = 0;
260 }
261 return 1;
262}
263
264
265
266
267
268static void
269sclp_tty_flush_chars(struct tty_struct *tty)
270{
271 if (sclp_tty_chars_count > 0) {
272 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
273 sclp_tty_chars_count = 0;
274 }
275}
276
277
278
279
280
281
282
283
284static int
285sclp_tty_chars_in_buffer(struct tty_struct *tty)
286{
287 unsigned long flags;
288 struct list_head *l;
289 struct sclp_buffer *t;
290 int count;
291
292 spin_lock_irqsave(&sclp_tty_lock, flags);
293 count = 0;
294 if (sclp_ttybuf != NULL)
295 count = sclp_chars_in_buffer(sclp_ttybuf);
296 list_for_each(l, &sclp_tty_outqueue) {
297 t = list_entry(l, struct sclp_buffer, list);
298 count += sclp_chars_in_buffer(t);
299 }
300 spin_unlock_irqrestore(&sclp_tty_lock, flags);
301 return count;
302}
303
304
305
306
307static void
308sclp_tty_flush_buffer(struct tty_struct *tty)
309{
310 if (sclp_tty_chars_count > 0) {
311 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
312 sclp_tty_chars_count = 0;
313 }
314}
315
316
317
318
319static void
320sclp_tty_input(unsigned char* buf, unsigned int count)
321{
322 struct tty_struct *tty = tty_port_tty_get(&sclp_port);
323 unsigned int cchar;
324
325
326
327
328
329 if (tty == NULL)
330 return;
331 cchar = ctrlchar_handle(buf, count, tty);
332 switch (cchar & CTRLCHAR_MASK) {
333 case CTRLCHAR_SYSRQ:
334 break;
335 case CTRLCHAR_CTRL:
336 tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL);
337 tty_flip_buffer_push(&sclp_port);
338 break;
339 case CTRLCHAR_NONE:
340
341 if (count < 2 ||
342 (strncmp((const char *) buf + count - 2, "^n", 2) &&
343 strncmp((const char *) buf + count - 2, "\252n", 2))) {
344
345 tty_insert_flip_string(&sclp_port, buf, count);
346 tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL);
347 } else
348 tty_insert_flip_string(&sclp_port, buf, count - 2);
349 tty_flip_buffer_push(&sclp_port);
350 break;
351 }
352 tty_kref_put(tty);
353}
354
355
356
357
358
359
360
361static int sclp_switch_cases(unsigned char *buf, int count)
362{
363 unsigned char *ip, *op;
364 int toggle;
365
366
367 toggle = 0;
368 ip = op = buf;
369 while (count-- > 0) {
370
371 if (*ip == CASE_DELIMITER) {
372
373 if (count && ip[1] == CASE_DELIMITER) {
374
375
376
377
378 *op++ = *ip++;
379 count--;
380 } else
381
382
383
384
385 toggle = ~toggle;
386
387 ip++;
388 } else
389
390 if (toggle)
391
392 if (sclp_tty_tolower)
393
394 *op++ = _ebc_toupper[(int) *ip++];
395 else
396
397 *op++ = _ebc_tolower[(int) *ip++];
398 else
399
400 *op++ = *ip++;
401 }
402
403 return op - buf;
404}
405
406static void sclp_get_input(struct gds_subvector *sv)
407{
408 unsigned char *str;
409 int count;
410
411 str = (unsigned char *) (sv + 1);
412 count = sv->length - sizeof(*sv);
413 if (sclp_tty_tolower)
414 EBC_TOLOWER(str, count);
415 count = sclp_switch_cases(str, count);
416
417 sclp_ebcasc_str(str, count);
418
419
420 sclp_tty_input(str, count);
421}
422
423static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
424{
425 void *end;
426
427 end = (void *) sv + sv->length;
428 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
429 if (sv->key == 0x30)
430 sclp_get_input(sv);
431}
432
433static inline void sclp_eval_textcmd(struct gds_vector *v)
434{
435 struct gds_subvector *sv;
436 void *end;
437
438 end = (void *) v + v->length;
439 for (sv = (struct gds_subvector *) (v + 1);
440 (void *) sv < end; sv = (void *) sv + sv->length)
441 if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
442 sclp_eval_selfdeftextmsg(sv);
443
444}
445
446static inline void sclp_eval_cpmsu(struct gds_vector *v)
447{
448 void *end;
449
450 end = (void *) v + v->length;
451 for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
452 if (v->gds_id == GDS_ID_TEXTCMD)
453 sclp_eval_textcmd(v);
454}
455
456
457static inline void sclp_eval_mdsmu(struct gds_vector *v)
458{
459 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
460 if (v)
461 sclp_eval_cpmsu(v);
462}
463
464static void sclp_tty_receiver(struct evbuf_header *evbuf)
465{
466 struct gds_vector *v;
467
468 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
469 GDS_ID_MDSMU);
470 if (v)
471 sclp_eval_mdsmu(v);
472}
473
474static void
475sclp_tty_state_change(struct sclp_register *reg)
476{
477}
478
479static struct sclp_register sclp_input_event =
480{
481 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
482 .state_change_fn = sclp_tty_state_change,
483 .receiver_fn = sclp_tty_receiver
484};
485
486static const struct tty_operations sclp_ops = {
487 .open = sclp_tty_open,
488 .close = sclp_tty_close,
489 .write = sclp_tty_write,
490 .put_char = sclp_tty_put_char,
491 .flush_chars = sclp_tty_flush_chars,
492 .write_room = sclp_tty_write_room,
493 .chars_in_buffer = sclp_tty_chars_in_buffer,
494 .flush_buffer = sclp_tty_flush_buffer,
495};
496
497static int __init
498sclp_tty_init(void)
499{
500 struct tty_driver *driver;
501 void *page;
502 int i;
503 int rc;
504
505
506 if (MACHINE_IS_VM && !CONSOLE_IS_SCLP)
507 return 0;
508 if (!sclp.has_linemode)
509 return 0;
510 driver = alloc_tty_driver(1);
511 if (!driver)
512 return -ENOMEM;
513
514 rc = sclp_rw_init();
515 if (rc) {
516 put_tty_driver(driver);
517 return rc;
518 }
519
520 INIT_LIST_HEAD(&sclp_tty_pages);
521 for (i = 0; i < MAX_KMEM_PAGES; i++) {
522 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
523 if (page == NULL) {
524 put_tty_driver(driver);
525 return -ENOMEM;
526 }
527 list_add_tail((struct list_head *) page, &sclp_tty_pages);
528 }
529 INIT_LIST_HEAD(&sclp_tty_outqueue);
530 spin_lock_init(&sclp_tty_lock);
531 timer_setup(&sclp_tty_timer, sclp_tty_timeout, 0);
532 sclp_ttybuf = NULL;
533 sclp_tty_buffer_count = 0;
534 if (MACHINE_IS_VM) {
535
536
537
538
539 sclp_tty_columns = 76;
540
541 sclp_tty_tolower = 1;
542 }
543 sclp_tty_chars_count = 0;
544
545 rc = sclp_register(&sclp_input_event);
546 if (rc) {
547 put_tty_driver(driver);
548 return rc;
549 }
550
551 tty_port_init(&sclp_port);
552
553 driver->driver_name = "sclp_line";
554 driver->name = "sclp_line";
555 driver->major = TTY_MAJOR;
556 driver->minor_start = 64;
557 driver->type = TTY_DRIVER_TYPE_SYSTEM;
558 driver->subtype = SYSTEM_TYPE_TTY;
559 driver->init_termios = tty_std_termios;
560 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
561 driver->init_termios.c_oflag = ONLCR;
562 driver->init_termios.c_lflag = ISIG | ECHO;
563 driver->flags = TTY_DRIVER_REAL_RAW;
564 tty_set_operations(driver, &sclp_ops);
565 tty_port_link_device(&sclp_port, driver, 0);
566 rc = tty_register_driver(driver);
567 if (rc) {
568 put_tty_driver(driver);
569 tty_port_destroy(&sclp_port);
570 return rc;
571 }
572 sclp_tty_driver = driver;
573 return 0;
574}
575device_initcall(sclp_tty_init);
576