1 /* 2 * SCLP line mode terminal driver. 3 * 4 * S390 version 5 * Copyright IBM Corp. 1999 6 * Author(s): Martin Peschke <mpeschke@de.ibm.com> 7 * Martin Schwidefsky <schwidefsky@de.ibm.com> 8 */ 9 10 #include <linux/kmod.h> 11 #include <linux/tty.h> 12 #include <linux/tty_driver.h> 13 #include <linux/tty_flip.h> 14 #include <linux/err.h> 15 #include <linux/init.h> 16 #include <linux/interrupt.h> 17 #include <linux/gfp.h> 18 #include <linux/uaccess.h> 19 20 #include "ctrlchar.h" 21 #include "sclp.h" 22 #include "sclp_rw.h" 23 #include "sclp_tty.h" 24 25 /* 26 * size of a buffer that collects single characters coming in 27 * via sclp_tty_put_char() 28 */ 29 #define SCLP_TTY_BUF_SIZE 512 30 31 /* 32 * There is exactly one SCLP terminal, so we can keep things simple 33 * and allocate all variables statically. 34 */ 35 36 /* Lock to guard over changes to global variables. */ 37 static spinlock_t sclp_tty_lock; 38 /* List of free pages that can be used for console output buffering. */ 39 static struct list_head sclp_tty_pages; 40 /* List of full struct sclp_buffer structures ready for output. */ 41 static struct list_head sclp_tty_outqueue; 42 /* Counter how many buffers are emitted. */ 43 static int sclp_tty_buffer_count; 44 /* Pointer to current console buffer. */ 45 static struct sclp_buffer *sclp_ttybuf; 46 /* Timer for delayed output of console messages. */ 47 static struct timer_list sclp_tty_timer; 48 49 static struct tty_port sclp_port; 50 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE]; 51 static unsigned short int sclp_tty_chars_count; 52 53 struct tty_driver *sclp_tty_driver; 54 55 static int sclp_tty_tolower; 56 static int sclp_tty_columns = 80; 57 58 #define SPACES_PER_TAB 8 59 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */ 60 61 /* This routine is called whenever we try to open a SCLP terminal. */ 62 static int 63 sclp_tty_open(struct tty_struct *tty, struct file *filp) 64 { 65 tty_port_tty_set(&sclp_port, tty); 66 tty->driver_data = NULL; 67 sclp_port.low_latency = 0; 68 return 0; 69 } 70 71 /* This routine is called when the SCLP terminal is closed. */ 72 static void 73 sclp_tty_close(struct tty_struct *tty, struct file *filp) 74 { 75 if (tty->count > 1) 76 return; 77 tty_port_tty_set(&sclp_port, NULL); 78 } 79 80 /* 81 * This routine returns the numbers of characters the tty driver 82 * will accept for queuing to be written. This number is subject 83 * to change as output buffers get emptied, or if the output flow 84 * control is acted. This is not an exact number because not every 85 * character needs the same space in the sccb. The worst case is 86 * a string of newlines. Every newline creates a new message which 87 * needs 82 bytes. 88 */ 89 static int 90 sclp_tty_write_room (struct tty_struct *tty) 91 { 92 unsigned long flags; 93 struct list_head *l; 94 int count; 95 96 spin_lock_irqsave(&sclp_tty_lock, flags); 97 count = 0; 98 if (sclp_ttybuf != NULL) 99 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct msg_buf); 100 list_for_each(l, &sclp_tty_pages) 101 count += NR_EMPTY_MSG_PER_SCCB; 102 spin_unlock_irqrestore(&sclp_tty_lock, flags); 103 return count; 104 } 105 106 static void 107 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc) 108 { 109 unsigned long flags; 110 void *page; 111 112 do { 113 page = sclp_unmake_buffer(buffer); 114 spin_lock_irqsave(&sclp_tty_lock, flags); 115 /* Remove buffer from outqueue */ 116 list_del(&buffer->list); 117 sclp_tty_buffer_count--; 118 list_add_tail((struct list_head *) page, &sclp_tty_pages); 119 /* Check if there is a pending buffer on the out queue. */ 120 buffer = NULL; 121 if (!list_empty(&sclp_tty_outqueue)) 122 buffer = list_entry(sclp_tty_outqueue.next, 123 struct sclp_buffer, list); 124 spin_unlock_irqrestore(&sclp_tty_lock, flags); 125 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback)); 126 127 tty_port_tty_wakeup(&sclp_port); 128 } 129 130 static inline void 131 __sclp_ttybuf_emit(struct sclp_buffer *buffer) 132 { 133 unsigned long flags; 134 int count; 135 int rc; 136 137 spin_lock_irqsave(&sclp_tty_lock, flags); 138 list_add_tail(&buffer->list, &sclp_tty_outqueue); 139 count = sclp_tty_buffer_count++; 140 spin_unlock_irqrestore(&sclp_tty_lock, flags); 141 if (count) 142 return; 143 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback); 144 if (rc) 145 sclp_ttybuf_callback(buffer, rc); 146 } 147 148 /* 149 * When this routine is called from the timer then we flush the 150 * temporary write buffer. 151 */ 152 static void 153 sclp_tty_timeout(unsigned long data) 154 { 155 unsigned long flags; 156 struct sclp_buffer *buf; 157 158 spin_lock_irqsave(&sclp_tty_lock, flags); 159 buf = sclp_ttybuf; 160 sclp_ttybuf = NULL; 161 spin_unlock_irqrestore(&sclp_tty_lock, flags); 162 163 if (buf != NULL) { 164 __sclp_ttybuf_emit(buf); 165 } 166 } 167 168 /* 169 * Write a string to the sclp tty. 170 */ 171 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail) 172 { 173 unsigned long flags; 174 void *page; 175 int written; 176 int overall_written; 177 struct sclp_buffer *buf; 178 179 if (count <= 0) 180 return 0; 181 overall_written = 0; 182 spin_lock_irqsave(&sclp_tty_lock, flags); 183 do { 184 /* Create a sclp output buffer if none exists yet */ 185 if (sclp_ttybuf == NULL) { 186 while (list_empty(&sclp_tty_pages)) { 187 spin_unlock_irqrestore(&sclp_tty_lock, flags); 188 if (may_fail) 189 goto out; 190 else 191 sclp_sync_wait(); 192 spin_lock_irqsave(&sclp_tty_lock, flags); 193 } 194 page = sclp_tty_pages.next; 195 list_del((struct list_head *) page); 196 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns, 197 SPACES_PER_TAB); 198 } 199 /* try to write the string to the current output buffer */ 200 written = sclp_write(sclp_ttybuf, str, count); 201 overall_written += written; 202 if (written == count) 203 break; 204 /* 205 * Not all characters could be written to the current 206 * output buffer. Emit the buffer, create a new buffer 207 * and then output the rest of the string. 208 */ 209 buf = sclp_ttybuf; 210 sclp_ttybuf = NULL; 211 spin_unlock_irqrestore(&sclp_tty_lock, flags); 212 __sclp_ttybuf_emit(buf); 213 spin_lock_irqsave(&sclp_tty_lock, flags); 214 str += written; 215 count -= written; 216 } while (count > 0); 217 /* Setup timer to output current console buffer after 1/10 second */ 218 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) && 219 !timer_pending(&sclp_tty_timer)) { 220 init_timer(&sclp_tty_timer); 221 sclp_tty_timer.function = sclp_tty_timeout; 222 sclp_tty_timer.data = 0UL; 223 sclp_tty_timer.expires = jiffies + HZ/10; 224 add_timer(&sclp_tty_timer); 225 } 226 spin_unlock_irqrestore(&sclp_tty_lock, flags); 227 out: 228 return overall_written; 229 } 230 231 /* 232 * This routine is called by the kernel to write a series of characters to the 233 * tty device. The characters may come from user space or kernel space. This 234 * routine will return the number of characters actually accepted for writing. 235 */ 236 static int 237 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) 238 { 239 if (sclp_tty_chars_count > 0) { 240 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 241 sclp_tty_chars_count = 0; 242 } 243 return sclp_tty_write_string(buf, count, 1); 244 } 245 246 /* 247 * This routine is called by the kernel to write a single character to the tty 248 * device. If the kernel uses this routine, it must call the flush_chars() 249 * routine (if defined) when it is done stuffing characters into the driver. 250 * 251 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver. 252 * If the given character is a '\n' the contents of the SCLP write buffer 253 * - including previous characters from sclp_tty_put_char() and strings from 254 * sclp_write() without final '\n' - will be written. 255 */ 256 static int 257 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch) 258 { 259 sclp_tty_chars[sclp_tty_chars_count++] = ch; 260 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) { 261 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 262 sclp_tty_chars_count = 0; 263 } 264 return 1; 265 } 266 267 /* 268 * This routine is called by the kernel after it has written a series of 269 * characters to the tty device using put_char(). 270 */ 271 static void 272 sclp_tty_flush_chars(struct tty_struct *tty) 273 { 274 if (sclp_tty_chars_count > 0) { 275 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 276 sclp_tty_chars_count = 0; 277 } 278 } 279 280 /* 281 * This routine returns the number of characters in the write buffer of the 282 * SCLP driver. The provided number includes all characters that are stored 283 * in the SCCB (will be written next time the SCLP is not busy) as well as 284 * characters in the write buffer (will not be written as long as there is a 285 * final line feed missing). 286 */ 287 static int 288 sclp_tty_chars_in_buffer(struct tty_struct *tty) 289 { 290 unsigned long flags; 291 struct list_head *l; 292 struct sclp_buffer *t; 293 int count; 294 295 spin_lock_irqsave(&sclp_tty_lock, flags); 296 count = 0; 297 if (sclp_ttybuf != NULL) 298 count = sclp_chars_in_buffer(sclp_ttybuf); 299 list_for_each(l, &sclp_tty_outqueue) { 300 t = list_entry(l, struct sclp_buffer, list); 301 count += sclp_chars_in_buffer(t); 302 } 303 spin_unlock_irqrestore(&sclp_tty_lock, flags); 304 return count; 305 } 306 307 /* 308 * removes all content from buffers of low level driver 309 */ 310 static void 311 sclp_tty_flush_buffer(struct tty_struct *tty) 312 { 313 if (sclp_tty_chars_count > 0) { 314 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 315 sclp_tty_chars_count = 0; 316 } 317 } 318 319 /* 320 * push input to tty 321 */ 322 static void 323 sclp_tty_input(unsigned char* buf, unsigned int count) 324 { 325 struct tty_struct *tty = tty_port_tty_get(&sclp_port); 326 unsigned int cchar; 327 328 /* 329 * If this tty driver is currently closed 330 * then throw the received input away. 331 */ 332 if (tty == NULL) 333 return; 334 cchar = ctrlchar_handle(buf, count, tty); 335 switch (cchar & CTRLCHAR_MASK) { 336 case CTRLCHAR_SYSRQ: 337 break; 338 case CTRLCHAR_CTRL: 339 tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL); 340 tty_flip_buffer_push(&sclp_port); 341 break; 342 case CTRLCHAR_NONE: 343 /* send (normal) input to line discipline */ 344 if (count < 2 || 345 (strncmp((const char *) buf + count - 2, "^n", 2) && 346 strncmp((const char *) buf + count - 2, "\252n", 2))) { 347 /* add the auto \n */ 348 tty_insert_flip_string(&sclp_port, buf, count); 349 tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL); 350 } else 351 tty_insert_flip_string(&sclp_port, buf, count - 2); 352 tty_flip_buffer_push(&sclp_port); 353 break; 354 } 355 tty_kref_put(tty); 356 } 357 358 /* 359 * get a EBCDIC string in upper/lower case, 360 * find out characters in lower/upper case separated by a special character, 361 * modifiy original string, 362 * returns length of resulting string 363 */ 364 static int sclp_switch_cases(unsigned char *buf, int count) 365 { 366 unsigned char *ip, *op; 367 int toggle; 368 369 /* initially changing case is off */ 370 toggle = 0; 371 ip = op = buf; 372 while (count-- > 0) { 373 /* compare with special character */ 374 if (*ip == CASE_DELIMITER) { 375 /* followed by another special character? */ 376 if (count && ip[1] == CASE_DELIMITER) { 377 /* 378 * ... then put a single copy of the special 379 * character to the output string 380 */ 381 *op++ = *ip++; 382 count--; 383 } else 384 /* 385 * ... special character follower by a normal 386 * character toggles the case change behaviour 387 */ 388 toggle = ~toggle; 389 /* skip special character */ 390 ip++; 391 } else 392 /* not the special character */ 393 if (toggle) 394 /* but case switching is on */ 395 if (sclp_tty_tolower) 396 /* switch to uppercase */ 397 *op++ = _ebc_toupper[(int) *ip++]; 398 else 399 /* switch to lowercase */ 400 *op++ = _ebc_tolower[(int) *ip++]; 401 else 402 /* no case switching, copy the character */ 403 *op++ = *ip++; 404 } 405 /* return length of reformatted string. */ 406 return op - buf; 407 } 408 409 static void sclp_get_input(struct gds_subvector *sv) 410 { 411 unsigned char *str; 412 int count; 413 414 str = (unsigned char *) (sv + 1); 415 count = sv->length - sizeof(*sv); 416 if (sclp_tty_tolower) 417 EBC_TOLOWER(str, count); 418 count = sclp_switch_cases(str, count); 419 /* convert EBCDIC to ASCII (modify original input in SCCB) */ 420 sclp_ebcasc_str(str, count); 421 422 /* transfer input to high level driver */ 423 sclp_tty_input(str, count); 424 } 425 426 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv) 427 { 428 void *end; 429 430 end = (void *) sv + sv->length; 431 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length) 432 if (sv->key == 0x30) 433 sclp_get_input(sv); 434 } 435 436 static inline void sclp_eval_textcmd(struct gds_vector *v) 437 { 438 struct gds_subvector *sv; 439 void *end; 440 441 end = (void *) v + v->length; 442 for (sv = (struct gds_subvector *) (v + 1); 443 (void *) sv < end; sv = (void *) sv + sv->length) 444 if (sv->key == GDS_KEY_SELFDEFTEXTMSG) 445 sclp_eval_selfdeftextmsg(sv); 446 447 } 448 449 static inline void sclp_eval_cpmsu(struct gds_vector *v) 450 { 451 void *end; 452 453 end = (void *) v + v->length; 454 for (v = v + 1; (void *) v < end; v = (void *) v + v->length) 455 if (v->gds_id == GDS_ID_TEXTCMD) 456 sclp_eval_textcmd(v); 457 } 458 459 460 static inline void sclp_eval_mdsmu(struct gds_vector *v) 461 { 462 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU); 463 if (v) 464 sclp_eval_cpmsu(v); 465 } 466 467 static void sclp_tty_receiver(struct evbuf_header *evbuf) 468 { 469 struct gds_vector *v; 470 471 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length, 472 GDS_ID_MDSMU); 473 if (v) 474 sclp_eval_mdsmu(v); 475 } 476 477 static void 478 sclp_tty_state_change(struct sclp_register *reg) 479 { 480 } 481 482 static struct sclp_register sclp_input_event = 483 { 484 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, 485 .state_change_fn = sclp_tty_state_change, 486 .receiver_fn = sclp_tty_receiver 487 }; 488 489 static const struct tty_operations sclp_ops = { 490 .open = sclp_tty_open, 491 .close = sclp_tty_close, 492 .write = sclp_tty_write, 493 .put_char = sclp_tty_put_char, 494 .flush_chars = sclp_tty_flush_chars, 495 .write_room = sclp_tty_write_room, 496 .chars_in_buffer = sclp_tty_chars_in_buffer, 497 .flush_buffer = sclp_tty_flush_buffer, 498 }; 499 500 static int __init 501 sclp_tty_init(void) 502 { 503 struct tty_driver *driver; 504 void *page; 505 int i; 506 int rc; 507 508 if (!CONSOLE_IS_SCLP) 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 /* Allocate pages for output buffering */ 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 init_timer(&sclp_tty_timer); 532 sclp_ttybuf = NULL; 533 sclp_tty_buffer_count = 0; 534 if (MACHINE_IS_VM) { 535 /* 536 * save 4 characters for the CPU number 537 * written at start of each line by VM/CP 538 */ 539 sclp_tty_columns = 76; 540 /* case input lines to lowercase */ 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 } 575 device_initcall(sclp_tty_init); 576