0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kmod.h>
0012 #include <linux/tty.h>
0013 #include <linux/tty_driver.h>
0014 #include <linux/tty_flip.h>
0015 #include <linux/err.h>
0016 #include <linux/init.h>
0017 #include <linux/interrupt.h>
0018 #include <linux/gfp.h>
0019 #include <linux/uaccess.h>
0020
0021 #include "ctrlchar.h"
0022 #include "sclp.h"
0023 #include "sclp_rw.h"
0024 #include "sclp_tty.h"
0025
0026
0027
0028
0029
0030 #define SCLP_TTY_BUF_SIZE 512
0031
0032
0033
0034
0035
0036
0037
0038 static DEFINE_SPINLOCK(sclp_tty_lock);
0039
0040 static LIST_HEAD(sclp_tty_pages);
0041
0042 static LIST_HEAD(sclp_tty_outqueue);
0043
0044 static int sclp_tty_buffer_count;
0045
0046 static struct sclp_buffer *sclp_ttybuf;
0047
0048 static struct timer_list sclp_tty_timer;
0049
0050 static struct tty_port sclp_port;
0051 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
0052 static unsigned short int sclp_tty_chars_count;
0053
0054 struct tty_driver *sclp_tty_driver;
0055
0056 static int sclp_tty_tolower;
0057
0058 #define SCLP_TTY_COLUMNS 320
0059 #define SPACES_PER_TAB 8
0060 #define CASE_DELIMITER 0x6c
0061
0062
0063 static int
0064 sclp_tty_open(struct tty_struct *tty, struct file *filp)
0065 {
0066 tty_port_tty_set(&sclp_port, tty);
0067 tty->driver_data = NULL;
0068 return 0;
0069 }
0070
0071
0072 static void
0073 sclp_tty_close(struct tty_struct *tty, struct file *filp)
0074 {
0075 if (tty->count > 1)
0076 return;
0077 tty_port_tty_set(&sclp_port, NULL);
0078 }
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 static unsigned int
0090 sclp_tty_write_room (struct tty_struct *tty)
0091 {
0092 unsigned long flags;
0093 struct list_head *l;
0094 unsigned int count;
0095
0096 spin_lock_irqsave(&sclp_tty_lock, flags);
0097 count = 0;
0098 if (sclp_ttybuf != NULL)
0099 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct msg_buf);
0100 list_for_each(l, &sclp_tty_pages)
0101 count += NR_EMPTY_MSG_PER_SCCB;
0102 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0103 return count;
0104 }
0105
0106 static void
0107 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
0108 {
0109 unsigned long flags;
0110 void *page;
0111
0112 do {
0113 page = sclp_unmake_buffer(buffer);
0114 spin_lock_irqsave(&sclp_tty_lock, flags);
0115
0116 list_del(&buffer->list);
0117 sclp_tty_buffer_count--;
0118 list_add_tail((struct list_head *) page, &sclp_tty_pages);
0119
0120 buffer = NULL;
0121 if (!list_empty(&sclp_tty_outqueue))
0122 buffer = list_entry(sclp_tty_outqueue.next,
0123 struct sclp_buffer, list);
0124 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0125 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
0126
0127 tty_port_tty_wakeup(&sclp_port);
0128 }
0129
0130 static inline void
0131 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
0132 {
0133 unsigned long flags;
0134 int count;
0135 int rc;
0136
0137 spin_lock_irqsave(&sclp_tty_lock, flags);
0138 list_add_tail(&buffer->list, &sclp_tty_outqueue);
0139 count = sclp_tty_buffer_count++;
0140 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0141 if (count)
0142 return;
0143 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
0144 if (rc)
0145 sclp_ttybuf_callback(buffer, rc);
0146 }
0147
0148
0149
0150
0151
0152 static void
0153 sclp_tty_timeout(struct timer_list *unused)
0154 {
0155 unsigned long flags;
0156 struct sclp_buffer *buf;
0157
0158 spin_lock_irqsave(&sclp_tty_lock, flags);
0159 buf = sclp_ttybuf;
0160 sclp_ttybuf = NULL;
0161 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0162
0163 if (buf != NULL) {
0164 __sclp_ttybuf_emit(buf);
0165 }
0166 }
0167
0168
0169
0170
0171 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
0172 {
0173 unsigned long flags;
0174 void *page;
0175 int written;
0176 int overall_written;
0177 struct sclp_buffer *buf;
0178
0179 if (count <= 0)
0180 return 0;
0181 overall_written = 0;
0182 spin_lock_irqsave(&sclp_tty_lock, flags);
0183 do {
0184
0185 if (sclp_ttybuf == NULL) {
0186 while (list_empty(&sclp_tty_pages)) {
0187 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0188 if (may_fail)
0189 goto out;
0190 else
0191 sclp_sync_wait();
0192 spin_lock_irqsave(&sclp_tty_lock, flags);
0193 }
0194 page = sclp_tty_pages.next;
0195 list_del((struct list_head *) page);
0196 sclp_ttybuf = sclp_make_buffer(page, SCLP_TTY_COLUMNS,
0197 SPACES_PER_TAB);
0198 }
0199
0200 written = sclp_write(sclp_ttybuf, str, count);
0201 overall_written += written;
0202 if (written == count)
0203 break;
0204
0205
0206
0207
0208
0209 buf = sclp_ttybuf;
0210 sclp_ttybuf = NULL;
0211 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0212 __sclp_ttybuf_emit(buf);
0213 spin_lock_irqsave(&sclp_tty_lock, flags);
0214 str += written;
0215 count -= written;
0216 } while (count > 0);
0217
0218 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
0219 !timer_pending(&sclp_tty_timer)) {
0220 mod_timer(&sclp_tty_timer, jiffies + HZ / 10);
0221 }
0222 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0223 out:
0224 return overall_written;
0225 }
0226
0227
0228
0229
0230
0231
0232 static int
0233 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
0234 {
0235 if (sclp_tty_chars_count > 0) {
0236 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
0237 sclp_tty_chars_count = 0;
0238 }
0239 return sclp_tty_write_string(buf, count, 1);
0240 }
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 static int
0253 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
0254 {
0255 sclp_tty_chars[sclp_tty_chars_count++] = ch;
0256 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
0257 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
0258 sclp_tty_chars_count = 0;
0259 }
0260 return 1;
0261 }
0262
0263
0264
0265
0266
0267 static void
0268 sclp_tty_flush_chars(struct tty_struct *tty)
0269 {
0270 if (sclp_tty_chars_count > 0) {
0271 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
0272 sclp_tty_chars_count = 0;
0273 }
0274 }
0275
0276
0277
0278
0279
0280
0281
0282
0283 static unsigned int
0284 sclp_tty_chars_in_buffer(struct tty_struct *tty)
0285 {
0286 unsigned long flags;
0287 struct sclp_buffer *t;
0288 unsigned int count = 0;
0289
0290 spin_lock_irqsave(&sclp_tty_lock, flags);
0291 if (sclp_ttybuf != NULL)
0292 count = sclp_chars_in_buffer(sclp_ttybuf);
0293 list_for_each_entry(t, &sclp_tty_outqueue, list) {
0294 count += sclp_chars_in_buffer(t);
0295 }
0296 spin_unlock_irqrestore(&sclp_tty_lock, flags);
0297 return count;
0298 }
0299
0300
0301
0302
0303 static void
0304 sclp_tty_flush_buffer(struct tty_struct *tty)
0305 {
0306 if (sclp_tty_chars_count > 0) {
0307 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
0308 sclp_tty_chars_count = 0;
0309 }
0310 }
0311
0312
0313
0314
0315 static void
0316 sclp_tty_input(unsigned char* buf, unsigned int count)
0317 {
0318 struct tty_struct *tty = tty_port_tty_get(&sclp_port);
0319 unsigned int cchar;
0320
0321
0322
0323
0324
0325 if (tty == NULL)
0326 return;
0327 cchar = ctrlchar_handle(buf, count, tty);
0328 switch (cchar & CTRLCHAR_MASK) {
0329 case CTRLCHAR_SYSRQ:
0330 break;
0331 case CTRLCHAR_CTRL:
0332 tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL);
0333 tty_flip_buffer_push(&sclp_port);
0334 break;
0335 case CTRLCHAR_NONE:
0336
0337 if (count < 2 ||
0338 (strncmp((const char *) buf + count - 2, "^n", 2) &&
0339 strncmp((const char *) buf + count - 2, "\252n", 2))) {
0340
0341 tty_insert_flip_string(&sclp_port, buf, count);
0342 tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL);
0343 } else
0344 tty_insert_flip_string(&sclp_port, buf, count - 2);
0345 tty_flip_buffer_push(&sclp_port);
0346 break;
0347 }
0348 tty_kref_put(tty);
0349 }
0350
0351
0352
0353
0354
0355
0356
0357 static int sclp_switch_cases(unsigned char *buf, int count)
0358 {
0359 unsigned char *ip, *op;
0360 int toggle;
0361
0362
0363 toggle = 0;
0364 ip = op = buf;
0365 while (count-- > 0) {
0366
0367 if (*ip == CASE_DELIMITER) {
0368
0369 if (count && ip[1] == CASE_DELIMITER) {
0370
0371
0372
0373
0374 *op++ = *ip++;
0375 count--;
0376 } else
0377
0378
0379
0380
0381 toggle = ~toggle;
0382
0383 ip++;
0384 } else
0385
0386 if (toggle)
0387
0388 if (sclp_tty_tolower)
0389
0390 *op++ = _ebc_toupper[(int) *ip++];
0391 else
0392
0393 *op++ = _ebc_tolower[(int) *ip++];
0394 else
0395
0396 *op++ = *ip++;
0397 }
0398
0399 return op - buf;
0400 }
0401
0402 static void sclp_get_input(struct gds_subvector *sv)
0403 {
0404 unsigned char *str;
0405 int count;
0406
0407 str = (unsigned char *) (sv + 1);
0408 count = sv->length - sizeof(*sv);
0409 if (sclp_tty_tolower)
0410 EBC_TOLOWER(str, count);
0411 count = sclp_switch_cases(str, count);
0412
0413 sclp_ebcasc_str(str, count);
0414
0415
0416 sclp_tty_input(str, count);
0417 }
0418
0419 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
0420 {
0421 void *end;
0422
0423 end = (void *) sv + sv->length;
0424 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
0425 if (sv->key == 0x30)
0426 sclp_get_input(sv);
0427 }
0428
0429 static inline void sclp_eval_textcmd(struct gds_vector *v)
0430 {
0431 struct gds_subvector *sv;
0432 void *end;
0433
0434 end = (void *) v + v->length;
0435 for (sv = (struct gds_subvector *) (v + 1);
0436 (void *) sv < end; sv = (void *) sv + sv->length)
0437 if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
0438 sclp_eval_selfdeftextmsg(sv);
0439
0440 }
0441
0442 static inline void sclp_eval_cpmsu(struct gds_vector *v)
0443 {
0444 void *end;
0445
0446 end = (void *) v + v->length;
0447 for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
0448 if (v->gds_id == GDS_ID_TEXTCMD)
0449 sclp_eval_textcmd(v);
0450 }
0451
0452
0453 static inline void sclp_eval_mdsmu(struct gds_vector *v)
0454 {
0455 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
0456 if (v)
0457 sclp_eval_cpmsu(v);
0458 }
0459
0460 static void sclp_tty_receiver(struct evbuf_header *evbuf)
0461 {
0462 struct gds_vector *v;
0463
0464 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
0465 GDS_ID_MDSMU);
0466 if (v)
0467 sclp_eval_mdsmu(v);
0468 }
0469
0470 static void
0471 sclp_tty_state_change(struct sclp_register *reg)
0472 {
0473 }
0474
0475 static struct sclp_register sclp_input_event =
0476 {
0477 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
0478 .state_change_fn = sclp_tty_state_change,
0479 .receiver_fn = sclp_tty_receiver
0480 };
0481
0482 static const struct tty_operations sclp_ops = {
0483 .open = sclp_tty_open,
0484 .close = sclp_tty_close,
0485 .write = sclp_tty_write,
0486 .put_char = sclp_tty_put_char,
0487 .flush_chars = sclp_tty_flush_chars,
0488 .write_room = sclp_tty_write_room,
0489 .chars_in_buffer = sclp_tty_chars_in_buffer,
0490 .flush_buffer = sclp_tty_flush_buffer,
0491 };
0492
0493 static int __init
0494 sclp_tty_init(void)
0495 {
0496 struct tty_driver *driver;
0497 void *page;
0498 int i;
0499 int rc;
0500
0501
0502 if (MACHINE_IS_VM && !CONSOLE_IS_SCLP)
0503 return 0;
0504 if (!sclp.has_linemode)
0505 return 0;
0506 driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
0507 if (IS_ERR(driver))
0508 return PTR_ERR(driver);
0509
0510 rc = sclp_rw_init();
0511 if (rc) {
0512 tty_driver_kref_put(driver);
0513 return rc;
0514 }
0515
0516 for (i = 0; i < MAX_KMEM_PAGES; i++) {
0517 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
0518 if (page == NULL) {
0519 tty_driver_kref_put(driver);
0520 return -ENOMEM;
0521 }
0522 list_add_tail((struct list_head *) page, &sclp_tty_pages);
0523 }
0524 timer_setup(&sclp_tty_timer, sclp_tty_timeout, 0);
0525 sclp_ttybuf = NULL;
0526 sclp_tty_buffer_count = 0;
0527 if (MACHINE_IS_VM) {
0528
0529 sclp_tty_tolower = 1;
0530 }
0531 sclp_tty_chars_count = 0;
0532
0533 rc = sclp_register(&sclp_input_event);
0534 if (rc) {
0535 tty_driver_kref_put(driver);
0536 return rc;
0537 }
0538
0539 tty_port_init(&sclp_port);
0540
0541 driver->driver_name = "sclp_line";
0542 driver->name = "sclp_line";
0543 driver->major = TTY_MAJOR;
0544 driver->minor_start = 64;
0545 driver->type = TTY_DRIVER_TYPE_SYSTEM;
0546 driver->subtype = SYSTEM_TYPE_TTY;
0547 driver->init_termios = tty_std_termios;
0548 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
0549 driver->init_termios.c_oflag = ONLCR;
0550 driver->init_termios.c_lflag = ISIG | ECHO;
0551 tty_set_operations(driver, &sclp_ops);
0552 tty_port_link_device(&sclp_port, driver, 0);
0553 rc = tty_register_driver(driver);
0554 if (rc) {
0555 tty_driver_kref_put(driver);
0556 tty_port_destroy(&sclp_port);
0557 return rc;
0558 }
0559 sclp_tty_driver = driver;
0560 return 0;
0561 }
0562 device_initcall(sclp_tty_init);