1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 3215 line mode terminal driver. 4 * 5 * Copyright IBM Corp. 1999, 2009 6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com> 7 * 8 * Updated: 9 * Aug-2000: Added tab support 10 * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu> 11 */ 12 13 #include <linux/types.h> 14 #include <linux/kdev_t.h> 15 #include <linux/tty.h> 16 #include <linux/tty_flip.h> 17 #include <linux/vt_kern.h> 18 #include <linux/init.h> 19 #include <linux/console.h> 20 #include <linux/interrupt.h> 21 #include <linux/err.h> 22 #include <linux/panic_notifier.h> 23 #include <linux/reboot.h> 24 #include <linux/serial.h> /* ASYNC_* flags */ 25 #include <linux/slab.h> 26 #include <asm/machine.h> 27 #include <asm/ccwdev.h> 28 #include <asm/cio.h> 29 #include <linux/io.h> 30 #include <asm/ebcdic.h> 31 #include <linux/uaccess.h> 32 #include <asm/delay.h> 33 #include <asm/cpcmd.h> 34 #include <asm/setup.h> 35 36 #include "ctrlchar.h" 37 38 #define NR_3215 1 39 #define NR_3215_REQ (4*NR_3215) 40 #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */ 41 #define RAW3215_INBUF_SIZE 256 /* input buffer size */ 42 #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */ 43 #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */ 44 #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */ 45 #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */ 46 #define RAW3215_NR_CCWS 3 47 #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */ 48 49 #define RAW3215_FIXED 1 /* 3215 console device is not be freed */ 50 #define RAW3215_WORKING 4 /* set if a request is being worked on */ 51 #define RAW3215_THROTTLED 8 /* set if reading is disabled */ 52 #define RAW3215_STOPPED 16 /* set if writing is disabled */ 53 #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */ 54 #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */ 55 56 #define TAB_STOP_SIZE 8 /* tab stop size */ 57 58 /* 59 * Request types for a 3215 device 60 */ 61 enum raw3215_type { 62 RAW3215_FREE, RAW3215_READ, RAW3215_WRITE 63 }; 64 65 /* 66 * Request structure for a 3215 device 67 */ 68 struct raw3215_req { 69 enum raw3215_type type; /* type of the request */ 70 int start, len; /* start index & len in output buffer */ 71 int delayable; /* indication to wait for more data */ 72 int residual; /* residual count for read request */ 73 struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */ 74 struct raw3215_info *info; /* pointer to main structure */ 75 struct raw3215_req *next; /* pointer to next request */ 76 } __attribute__ ((aligned(8))); 77 78 struct raw3215_info { 79 struct tty_port port; 80 struct ccw_device *cdev; /* device for tty driver */ 81 spinlock_t *lock; /* pointer to irq lock */ 82 int flags; /* state flags */ 83 u8 *buffer; /* pointer to output buffer */ 84 u8 *inbuf; /* pointer to input buffer */ 85 int head; /* first free byte in output buffer */ 86 int count; /* number of bytes in output buffer */ 87 int written; /* number of bytes in write requests */ 88 struct raw3215_req *queued_read; /* pointer to queued read requests */ 89 struct raw3215_req *queued_write;/* pointer to queued write requests */ 90 wait_queue_head_t empty_wait; /* wait queue for flushing */ 91 struct timer_list timer; /* timer for delayed output */ 92 int line_pos; /* position on the line (for tabs) */ 93 }; 94 95 /* array of 3215 devices structures */ 96 static struct raw3215_info *raw3215[NR_3215]; 97 /* spinlock to protect the raw3215 array */ 98 static DEFINE_SPINLOCK(raw3215_device_lock); 99 /* list of free request structures */ 100 static struct raw3215_req *raw3215_freelist; 101 /* spinlock to protect free list */ 102 static DEFINE_SPINLOCK(raw3215_freelist_lock); 103 104 static struct tty_driver *tty3215_driver; 105 static bool con3215_drop = true; 106 107 /* 108 * Get a request structure from the free list 109 */ 110 static inline struct raw3215_req *raw3215_alloc_req(void) 111 { 112 struct raw3215_req *req; 113 unsigned long flags; 114 115 spin_lock_irqsave(&raw3215_freelist_lock, flags); 116 req = raw3215_freelist; 117 raw3215_freelist = req->next; 118 spin_unlock_irqrestore(&raw3215_freelist_lock, flags); 119 return req; 120 } 121 122 /* 123 * Put a request structure back to the free list 124 */ 125 static inline void raw3215_free_req(struct raw3215_req *req) 126 { 127 unsigned long flags; 128 129 if (req->type == RAW3215_FREE) 130 return; /* don't free a free request */ 131 req->type = RAW3215_FREE; 132 spin_lock_irqsave(&raw3215_freelist_lock, flags); 133 req->next = raw3215_freelist; 134 raw3215_freelist = req; 135 spin_unlock_irqrestore(&raw3215_freelist_lock, flags); 136 } 137 138 /* 139 * Set up a read request that reads up to 160 byte from the 3215 device. 140 * If there is a queued read request it is used, but that shouldn't happen 141 * because a 3215 terminal won't accept a new read before the old one is 142 * completed. 143 */ 144 static void raw3215_mk_read_req(struct raw3215_info *raw) 145 { 146 struct raw3215_req *req; 147 struct ccw1 *ccw; 148 149 /* there can only be ONE read request at a time */ 150 req = raw->queued_read; 151 if (req == NULL) { 152 /* no queued read request, use new req structure */ 153 req = raw3215_alloc_req(); 154 req->type = RAW3215_READ; 155 req->info = raw; 156 raw->queued_read = req; 157 } 158 159 ccw = req->ccws; 160 ccw->cmd_code = 0x0A; /* read inquiry */ 161 ccw->flags = 0x20; /* ignore incorrect length */ 162 ccw->count = 160; 163 ccw->cda = virt_to_dma32(raw->inbuf); 164 } 165 166 /* 167 * Set up a write request with the information from the main structure. 168 * A ccw chain is created that writes as much as possible from the output 169 * buffer to the 3215 device. If a queued write exists it is replaced by 170 * the new, probably lengthened request. 171 */ 172 static void raw3215_mk_write_req(struct raw3215_info *raw) 173 { 174 struct raw3215_req *req; 175 struct ccw1 *ccw; 176 int len, count, ix, lines; 177 178 if (raw->count <= raw->written) 179 return; 180 /* check if there is a queued write request */ 181 req = raw->queued_write; 182 if (req == NULL) { 183 /* no queued write request, use new req structure */ 184 req = raw3215_alloc_req(); 185 req->type = RAW3215_WRITE; 186 req->info = raw; 187 raw->queued_write = req; 188 } else { 189 raw->written -= req->len; 190 } 191 192 ccw = req->ccws; 193 req->start = (raw->head - raw->count + raw->written) & 194 (RAW3215_BUFFER_SIZE - 1); 195 /* 196 * now we have to count newlines. We can at max accept 197 * RAW3215_MAX_NEWLINE newlines in a single ssch due to 198 * a restriction in VM 199 */ 200 lines = 0; 201 ix = req->start; 202 while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) { 203 if (raw->buffer[ix] == 0x15) 204 lines++; 205 ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1); 206 } 207 len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1; 208 if (len > RAW3215_MAX_BYTES) 209 len = RAW3215_MAX_BYTES; 210 req->len = len; 211 raw->written += len; 212 213 /* set the indication if we should try to enlarge this request */ 214 req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE); 215 216 ix = req->start; 217 while (len > 0) { 218 if (ccw > req->ccws) 219 ccw[-1].flags |= 0x40; /* use command chaining */ 220 ccw->cmd_code = 0x01; /* write, auto carrier return */ 221 ccw->flags = 0x20; /* ignore incorrect length ind. */ 222 ccw->cda = virt_to_dma32(raw->buffer + ix); 223 count = len; 224 if (ix + count > RAW3215_BUFFER_SIZE) 225 count = RAW3215_BUFFER_SIZE - ix; 226 ccw->count = count; 227 len -= count; 228 ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1); 229 ccw++; 230 } 231 /* 232 * Add a NOP to the channel program. 3215 devices are purely 233 * emulated and its much better to avoid the channel end 234 * interrupt in this case. 235 */ 236 if (ccw > req->ccws) 237 ccw[-1].flags |= 0x40; /* use command chaining */ 238 ccw->cmd_code = 0x03; /* NOP */ 239 ccw->flags = 0; 240 ccw->cda = 0; 241 ccw->count = 1; 242 } 243 244 /* 245 * Start a read or a write request 246 */ 247 static void raw3215_start_io(struct raw3215_info *raw) 248 { 249 struct raw3215_req *req; 250 int res; 251 252 req = raw->queued_read; 253 if (req != NULL && 254 !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) { 255 /* dequeue request */ 256 raw->queued_read = NULL; 257 res = ccw_device_start(raw->cdev, req->ccws, 258 (unsigned long) req, 0, 0); 259 if (res != 0) { 260 /* do_IO failed, put request back to queue */ 261 raw->queued_read = req; 262 } else { 263 raw->flags |= RAW3215_WORKING; 264 } 265 } 266 req = raw->queued_write; 267 if (req != NULL && 268 !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) { 269 /* dequeue request */ 270 raw->queued_write = NULL; 271 res = ccw_device_start(raw->cdev, req->ccws, 272 (unsigned long) req, 0, 0); 273 if (res != 0) { 274 /* do_IO failed, put request back to queue */ 275 raw->queued_write = req; 276 } else { 277 raw->flags |= RAW3215_WORKING; 278 } 279 } 280 } 281 282 /* 283 * Function to start a delayed output after RAW3215_TIMEOUT seconds 284 */ 285 static void raw3215_timeout(struct timer_list *t) 286 { 287 struct raw3215_info *raw = timer_container_of(raw, t, timer); 288 unsigned long flags; 289 290 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 291 raw->flags &= ~RAW3215_TIMER_RUNS; 292 raw3215_mk_write_req(raw); 293 raw3215_start_io(raw); 294 if ((raw->queued_read || raw->queued_write) && 295 !(raw->flags & RAW3215_WORKING) && 296 !(raw->flags & RAW3215_TIMER_RUNS)) { 297 raw->timer.expires = RAW3215_TIMEOUT + jiffies; 298 add_timer(&raw->timer); 299 raw->flags |= RAW3215_TIMER_RUNS; 300 } 301 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 302 } 303 304 /* 305 * Function to conditionally start an IO. A read is started immediately, 306 * a write is only started immediately if the flush flag is on or the 307 * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not 308 * done immediately a timer is started with a delay of RAW3215_TIMEOUT. 309 */ 310 static inline void raw3215_try_io(struct raw3215_info *raw) 311 { 312 if (!tty_port_initialized(&raw->port)) 313 return; 314 if (raw->queued_read != NULL) 315 raw3215_start_io(raw); 316 else if (raw->queued_write != NULL) { 317 if ((raw->queued_write->delayable == 0) || 318 (raw->flags & RAW3215_FLUSHING)) { 319 /* execute write requests bigger than minimum size */ 320 raw3215_start_io(raw); 321 } 322 } 323 if ((raw->queued_read || raw->queued_write) && 324 !(raw->flags & RAW3215_WORKING) && 325 !(raw->flags & RAW3215_TIMER_RUNS)) { 326 raw->timer.expires = RAW3215_TIMEOUT + jiffies; 327 add_timer(&raw->timer); 328 raw->flags |= RAW3215_TIMER_RUNS; 329 } 330 } 331 332 /* 333 * Try to start the next IO and wake up processes waiting on the tty. 334 */ 335 static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty) 336 { 337 raw3215_mk_write_req(raw); 338 raw3215_try_io(raw); 339 if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) 340 tty_wakeup(tty); 341 } 342 343 /* 344 * Interrupt routine, called from common io layer 345 */ 346 static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm, 347 struct irb *irb) 348 { 349 struct raw3215_info *raw; 350 struct raw3215_req *req; 351 struct tty_struct *tty; 352 int cstat, dstat; 353 int count; 354 355 raw = dev_get_drvdata(&cdev->dev); 356 req = (struct raw3215_req *) intparm; 357 tty = tty_port_tty_get(&raw->port); 358 cstat = irb->scsw.cmd.cstat; 359 dstat = irb->scsw.cmd.dstat; 360 if (cstat != 0) 361 raw3215_next_io(raw, tty); 362 if (dstat & 0x01) { /* we got a unit exception */ 363 dstat &= ~0x01; /* we can ignore it */ 364 } 365 switch (dstat) { 366 case 0x80: 367 if (cstat != 0) 368 break; 369 /* Attention interrupt, someone hit the enter key */ 370 raw3215_mk_read_req(raw); 371 raw3215_next_io(raw, tty); 372 break; 373 case 0x08: 374 case 0x0C: 375 /* Channel end interrupt. */ 376 if ((raw = req->info) == NULL) 377 goto put_tty; /* That shouldn't happen ... */ 378 if (req->type == RAW3215_READ) { 379 /* store residual count, then wait for device end */ 380 req->residual = irb->scsw.cmd.count; 381 } 382 if (dstat == 0x08) 383 break; 384 fallthrough; 385 case 0x04: 386 /* Device end interrupt. */ 387 if ((raw = req->info) == NULL) 388 goto put_tty; /* That shouldn't happen ... */ 389 if (req->type == RAW3215_READ && tty != NULL) { 390 unsigned int cchar; 391 392 count = 160 - req->residual; 393 EBCASC(raw->inbuf, count); 394 cchar = ctrlchar_handle(raw->inbuf, count, tty); 395 switch (cchar & CTRLCHAR_MASK) { 396 case CTRLCHAR_SYSRQ: 397 break; 398 399 case CTRLCHAR_CTRL: 400 tty_insert_flip_char(&raw->port, cchar, 401 TTY_NORMAL); 402 tty_flip_buffer_push(&raw->port); 403 break; 404 405 case CTRLCHAR_NONE: 406 if (count < 2 || 407 (strncmp(raw->inbuf+count-2, "\252n", 2) && 408 strncmp(raw->inbuf+count-2, "^n", 2)) ) { 409 /* add the auto \n */ 410 raw->inbuf[count] = '\n'; 411 count++; 412 } else 413 count -= 2; 414 tty_insert_flip_string(&raw->port, raw->inbuf, 415 count); 416 tty_flip_buffer_push(&raw->port); 417 break; 418 } 419 } else if (req->type == RAW3215_WRITE) { 420 raw->count -= req->len; 421 raw->written -= req->len; 422 } 423 raw->flags &= ~RAW3215_WORKING; 424 raw3215_free_req(req); 425 /* check for empty wait */ 426 if (waitqueue_active(&raw->empty_wait) && 427 raw->queued_write == NULL && 428 raw->queued_read == NULL) { 429 wake_up_interruptible(&raw->empty_wait); 430 } 431 raw3215_next_io(raw, tty); 432 break; 433 default: 434 /* Strange interrupt, I'll do my best to clean up */ 435 if (req != NULL && req->type != RAW3215_FREE) { 436 if (req->type == RAW3215_WRITE) { 437 raw->count -= req->len; 438 raw->written -= req->len; 439 } 440 raw->flags &= ~RAW3215_WORKING; 441 raw3215_free_req(req); 442 } 443 raw3215_next_io(raw, tty); 444 } 445 put_tty: 446 tty_kref_put(tty); 447 } 448 449 /* 450 * Need to drop data to avoid blocking. Drop as much data as possible. 451 * This is unqueued part in the buffer and the queued part in the request. 452 * Also adjust the head position to append new data and set count 453 * accordingly. 454 * 455 * Return number of bytes available in buffer. 456 */ 457 static unsigned int raw3215_drop(struct raw3215_info *raw) 458 { 459 struct raw3215_req *req; 460 461 req = raw->queued_write; 462 if (req) { 463 /* Drop queued data and delete request */ 464 raw->written -= req->len; 465 raw3215_free_req(req); 466 raw->queued_write = NULL; 467 } 468 raw->head = (raw->head - raw->count + raw->written) & 469 (RAW3215_BUFFER_SIZE - 1); 470 raw->count = raw->written; 471 472 return RAW3215_BUFFER_SIZE - raw->count; 473 } 474 475 /* 476 * Wait until length bytes are available int the output buffer. 477 * If drop mode is active and wait condition holds true, start dropping 478 * data. 479 * Has to be called with the s390irq lock held. Can be called 480 * disabled. 481 */ 482 static unsigned int raw3215_make_room(struct raw3215_info *raw, 483 unsigned int length, bool drop) 484 { 485 while (RAW3215_BUFFER_SIZE - raw->count < length) { 486 if (drop) 487 return raw3215_drop(raw); 488 489 /* there might be a request pending */ 490 raw->flags |= RAW3215_FLUSHING; 491 raw3215_mk_write_req(raw); 492 raw3215_try_io(raw); 493 raw->flags &= ~RAW3215_FLUSHING; 494 #ifdef CONFIG_TN3215_CONSOLE 495 ccw_device_wait_idle(raw->cdev); 496 #endif 497 /* Enough room freed up ? */ 498 if (RAW3215_BUFFER_SIZE - raw->count >= length) 499 break; 500 /* there might be another cpu waiting for the lock */ 501 spin_unlock(get_ccwdev_lock(raw->cdev)); 502 udelay(100); 503 spin_lock(get_ccwdev_lock(raw->cdev)); 504 } 505 return length; 506 } 507 508 #define RAW3215_COUNT 1 509 #define RAW3215_STORE 2 510 511 /* 512 * Add text to console buffer. Find tabs in input and calculate size 513 * including tab replacement. 514 * This function operates in 2 different modes, depending on parameter 515 * opmode: 516 * RAW3215_COUNT: Get the size needed for the input string with 517 * proper tab replacement calculation. 518 * Return value is the number of bytes required to store the 519 * input. However no data is actually stored. 520 * The parameter todrop is not used. 521 * RAW3215_STORE: Add data to the console buffer. The parameter todrop is 522 * valid and contains the number of bytes to be dropped from head of 523 * string without blocking. 524 * Return value is the number of bytes copied. 525 */ 526 static unsigned int raw3215_addtext(const u8 *str, size_t length, 527 struct raw3215_info *raw, int opmode, 528 unsigned int todrop) 529 { 530 unsigned int i, blanks, expanded_size = 0; 531 unsigned int column = raw->line_pos; 532 size_t c; 533 u8 ch; 534 535 if (opmode == RAW3215_COUNT) 536 todrop = 0; 537 538 for (c = 0; c < length; ++c) { 539 blanks = 1; 540 ch = str[c]; 541 542 switch (ch) { 543 case '\n': 544 expanded_size++; 545 column = 0; 546 break; 547 case '\t': 548 blanks = TAB_STOP_SIZE - (column % TAB_STOP_SIZE); 549 column += blanks; 550 expanded_size += blanks; 551 ch = ' '; 552 break; 553 default: 554 expanded_size++; 555 column++; 556 break; 557 } 558 559 if (opmode == RAW3215_COUNT) 560 continue; 561 if (todrop && expanded_size < todrop) /* Drop head data */ 562 continue; 563 for (i = 0; i < blanks; i++) { 564 raw->buffer[raw->head] = _ascebc[ch]; 565 raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1); 566 raw->count++; 567 } 568 raw->line_pos = column; 569 } 570 return expanded_size - todrop; 571 } 572 573 /* 574 * String write routine for 3215 devices 575 */ 576 static void raw3215_write(struct raw3215_info *raw, const u8 *str, 577 size_t length) 578 { 579 unsigned int count, avail; 580 unsigned long flags; 581 582 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 583 584 count = raw3215_addtext(str, length, raw, RAW3215_COUNT, 0); 585 586 avail = raw3215_make_room(raw, count, con3215_drop); 587 if (avail) { 588 raw3215_addtext(str, length, raw, RAW3215_STORE, 589 count - avail); 590 } 591 if (!(raw->flags & RAW3215_WORKING)) { 592 raw3215_mk_write_req(raw); 593 /* start or queue request */ 594 raw3215_try_io(raw); 595 } 596 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 597 } 598 599 /* 600 * Put character routine for 3215 devices 601 */ 602 static void raw3215_putchar(struct raw3215_info *raw, u8 ch) 603 { 604 raw3215_write(raw, &ch, 1); 605 } 606 607 /* 608 * Flush routine, it simply sets the flush flag and tries to start 609 * pending IO. 610 */ 611 static void raw3215_flush_buffer(struct raw3215_info *raw) 612 { 613 unsigned long flags; 614 615 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 616 if (raw->count > 0) { 617 raw->flags |= RAW3215_FLUSHING; 618 raw3215_try_io(raw); 619 raw->flags &= ~RAW3215_FLUSHING; 620 } 621 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 622 } 623 624 /* 625 * Fire up a 3215 device. 626 */ 627 static int raw3215_startup(struct raw3215_info *raw) 628 { 629 unsigned long flags; 630 631 if (tty_port_initialized(&raw->port)) 632 return 0; 633 raw->line_pos = 0; 634 tty_port_set_initialized(&raw->port, true); 635 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 636 raw3215_try_io(raw); 637 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 638 639 return 0; 640 } 641 642 /* 643 * Shutdown a 3215 device. 644 */ 645 static void raw3215_shutdown(struct raw3215_info *raw) 646 { 647 DECLARE_WAITQUEUE(wait, current); 648 unsigned long flags; 649 650 if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED)) 651 return; 652 /* Wait for outstanding requests, then free irq */ 653 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 654 if ((raw->flags & RAW3215_WORKING) || 655 raw->queued_write != NULL || 656 raw->queued_read != NULL) { 657 add_wait_queue(&raw->empty_wait, &wait); 658 set_current_state(TASK_INTERRUPTIBLE); 659 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 660 schedule(); 661 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 662 remove_wait_queue(&raw->empty_wait, &wait); 663 set_current_state(TASK_RUNNING); 664 tty_port_set_initialized(&raw->port, true); 665 } 666 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 667 } 668 669 static struct raw3215_info *raw3215_alloc_info(void) 670 { 671 struct raw3215_info *info; 672 673 info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA); 674 if (!info) 675 return NULL; 676 677 info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA); 678 info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA); 679 if (!info->buffer || !info->inbuf) { 680 kfree(info->inbuf); 681 kfree(info->buffer); 682 kfree(info); 683 return NULL; 684 } 685 686 timer_setup(&info->timer, raw3215_timeout, 0); 687 init_waitqueue_head(&info->empty_wait); 688 tty_port_init(&info->port); 689 690 return info; 691 } 692 693 static void raw3215_free_info(struct raw3215_info *raw) 694 { 695 kfree(raw->inbuf); 696 kfree(raw->buffer); 697 tty_port_destroy(&raw->port); 698 kfree(raw); 699 } 700 701 static int raw3215_probe(struct ccw_device *cdev) 702 { 703 struct raw3215_info *raw; 704 int line; 705 706 /* Console is special. */ 707 if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev))) 708 return 0; 709 710 raw = raw3215_alloc_info(); 711 if (raw == NULL) 712 return -ENOMEM; 713 714 raw->cdev = cdev; 715 dev_set_drvdata(&cdev->dev, raw); 716 cdev->handler = raw3215_irq; 717 718 spin_lock(&raw3215_device_lock); 719 for (line = 0; line < NR_3215; line++) { 720 if (!raw3215[line]) { 721 raw3215[line] = raw; 722 break; 723 } 724 } 725 spin_unlock(&raw3215_device_lock); 726 if (line == NR_3215) { 727 raw3215_free_info(raw); 728 return -ENODEV; 729 } 730 731 return 0; 732 } 733 734 static void raw3215_remove(struct ccw_device *cdev) 735 { 736 struct raw3215_info *raw; 737 unsigned int line; 738 739 ccw_device_set_offline(cdev); 740 raw = dev_get_drvdata(&cdev->dev); 741 if (raw) { 742 spin_lock(&raw3215_device_lock); 743 for (line = 0; line < NR_3215; line++) 744 if (raw3215[line] == raw) 745 break; 746 raw3215[line] = NULL; 747 spin_unlock(&raw3215_device_lock); 748 dev_set_drvdata(&cdev->dev, NULL); 749 raw3215_free_info(raw); 750 } 751 } 752 753 static int raw3215_set_online(struct ccw_device *cdev) 754 { 755 struct raw3215_info *raw; 756 757 raw = dev_get_drvdata(&cdev->dev); 758 if (!raw) 759 return -ENODEV; 760 761 return raw3215_startup(raw); 762 } 763 764 static int raw3215_set_offline(struct ccw_device *cdev) 765 { 766 struct raw3215_info *raw; 767 768 raw = dev_get_drvdata(&cdev->dev); 769 if (!raw) 770 return -ENODEV; 771 772 raw3215_shutdown(raw); 773 774 return 0; 775 } 776 777 static struct ccw_device_id raw3215_id[] = { 778 { CCW_DEVICE(0x3215, 0) }, 779 { /* end of list */ }, 780 }; 781 782 static ssize_t con_drop_store(struct device_driver *dev, const char *buf, size_t count) 783 { 784 bool drop; 785 int rc; 786 787 rc = kstrtobool(buf, &drop); 788 if (!rc) 789 con3215_drop = drop; 790 return rc ?: count; 791 } 792 793 static ssize_t con_drop_show(struct device_driver *dev, char *buf) 794 { 795 return sysfs_emit(buf, "%d\n", con3215_drop ? 1 : 0); 796 } 797 798 static DRIVER_ATTR_RW(con_drop); 799 800 static struct attribute *con3215_drv_attrs[] = { 801 &driver_attr_con_drop.attr, 802 NULL, 803 }; 804 805 static struct attribute_group con3215_drv_attr_group = { 806 .attrs = con3215_drv_attrs, 807 }; 808 809 static const struct attribute_group *con3215_drv_attr_groups[] = { 810 &con3215_drv_attr_group, 811 NULL, 812 }; 813 814 static struct ccw_driver raw3215_ccw_driver = { 815 .driver = { 816 .name = "3215", 817 .groups = con3215_drv_attr_groups, 818 .owner = THIS_MODULE, 819 }, 820 .ids = raw3215_id, 821 .probe = &raw3215_probe, 822 .remove = &raw3215_remove, 823 .set_online = &raw3215_set_online, 824 .set_offline = &raw3215_set_offline, 825 .int_class = IRQIO_C15, 826 }; 827 828 static void handle_write(struct raw3215_info *raw, const u8 *str, size_t count) 829 { 830 while (count > 0) { 831 size_t i = min_t(size_t, count, RAW3215_BUFFER_SIZE - 1); 832 raw3215_write(raw, str, i); 833 count -= i; 834 str += i; 835 } 836 } 837 838 #ifdef CONFIG_TN3215_CONSOLE 839 /* 840 * Write a string to the 3215 console 841 */ 842 static void con3215_write(struct console *co, const char *str, unsigned int count) 843 { 844 handle_write(raw3215[0], str, count); 845 } 846 847 static struct tty_driver *con3215_device(struct console *c, int *index) 848 { 849 *index = c->index; 850 return tty3215_driver; 851 } 852 853 /* 854 * The below function is called as a panic/reboot notifier before the 855 * system enters a disabled, endless loop. 856 * 857 * Notice we must use the spin_trylock() alternative, to prevent lockups 858 * in atomic context (panic routine runs with secondary CPUs, local IRQs 859 * and preemption disabled). 860 */ 861 static int con3215_notify(struct notifier_block *self, 862 unsigned long event, void *data) 863 { 864 struct raw3215_info *raw; 865 unsigned long flags; 866 867 raw = raw3215[0]; /* console 3215 is the first one */ 868 if (!spin_trylock_irqsave(get_ccwdev_lock(raw->cdev), flags)) 869 return NOTIFY_DONE; 870 raw3215_make_room(raw, RAW3215_BUFFER_SIZE, false); 871 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 872 873 return NOTIFY_DONE; 874 } 875 876 static struct notifier_block on_panic_nb = { 877 .notifier_call = con3215_notify, 878 .priority = INT_MIN + 1, /* run the callback late */ 879 }; 880 881 static struct notifier_block on_reboot_nb = { 882 .notifier_call = con3215_notify, 883 .priority = INT_MIN + 1, /* run the callback late */ 884 }; 885 886 /* 887 * The console structure for the 3215 console 888 */ 889 static struct console con3215 = { 890 .name = "ttyS", 891 .write = con3215_write, 892 .device = con3215_device, 893 .flags = CON_PRINTBUFFER, 894 }; 895 896 /* 897 * 3215 console initialization code called from console_init(). 898 */ 899 static int __init con3215_init(void) 900 { 901 struct ccw_device *cdev; 902 struct raw3215_info *raw; 903 struct raw3215_req *req; 904 int i; 905 906 /* Check if 3215 is to be the console */ 907 if (!CONSOLE_IS_3215) 908 return -ENODEV; 909 910 /* Set the console mode for VM */ 911 if (machine_is_vm()) { 912 cpcmd("TERM CONMODE 3215", NULL, 0, NULL); 913 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL); 914 } 915 916 /* allocate 3215 request structures */ 917 raw3215_freelist = NULL; 918 for (i = 0; i < NR_3215_REQ; i++) { 919 req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA); 920 if (!req) 921 return -ENOMEM; 922 req->next = raw3215_freelist; 923 raw3215_freelist = req; 924 } 925 926 cdev = ccw_device_create_console(&raw3215_ccw_driver); 927 if (IS_ERR(cdev)) 928 return -ENODEV; 929 930 raw3215[0] = raw = raw3215_alloc_info(); 931 raw->cdev = cdev; 932 dev_set_drvdata(&cdev->dev, raw); 933 cdev->handler = raw3215_irq; 934 935 raw->flags |= RAW3215_FIXED; 936 if (ccw_device_enable_console(cdev)) { 937 ccw_device_destroy_console(cdev); 938 raw3215_free_info(raw); 939 raw3215[0] = NULL; 940 return -ENODEV; 941 } 942 943 /* Request the console irq */ 944 if (raw3215_startup(raw) != 0) { 945 raw3215_free_info(raw); 946 raw3215[0] = NULL; 947 return -ENODEV; 948 } 949 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb); 950 register_reboot_notifier(&on_reboot_nb); 951 register_console(&con3215); 952 return 0; 953 } 954 console_initcall(con3215_init); 955 #endif 956 957 static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty) 958 { 959 struct raw3215_info *raw; 960 961 raw = raw3215[tty->index]; 962 if (raw == NULL) 963 return -ENODEV; 964 965 tty->driver_data = raw; 966 967 return tty_port_install(&raw->port, driver, tty); 968 } 969 970 /* 971 * tty3215_open 972 * 973 * This routine is called whenever a 3215 tty is opened. 974 */ 975 static int tty3215_open(struct tty_struct *tty, struct file * filp) 976 { 977 struct raw3215_info *raw = tty->driver_data; 978 979 tty_port_tty_set(&raw->port, tty); 980 981 /* 982 * Start up 3215 device 983 */ 984 return raw3215_startup(raw); 985 } 986 987 /* 988 * tty3215_close() 989 * 990 * This routine is called when the 3215 tty is closed. We wait 991 * for the remaining request to be completed. Then we clean up. 992 */ 993 static void tty3215_close(struct tty_struct *tty, struct file * filp) 994 { 995 struct raw3215_info *raw = tty->driver_data; 996 997 if (raw == NULL || tty->count > 1) 998 return; 999 tty->closing = 1; 1000 /* Shutdown the terminal */ 1001 raw3215_shutdown(raw); 1002 tty->closing = 0; 1003 tty_port_tty_set(&raw->port, NULL); 1004 } 1005 1006 /* 1007 * Returns the amount of free space in the output buffer. 1008 */ 1009 static unsigned int tty3215_write_room(struct tty_struct *tty) 1010 { 1011 struct raw3215_info *raw = tty->driver_data; 1012 1013 /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */ 1014 if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0) 1015 return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE; 1016 else 1017 return 0; 1018 } 1019 1020 /* 1021 * String write routine for 3215 ttys 1022 */ 1023 static ssize_t tty3215_write(struct tty_struct *tty, const u8 *buf, 1024 size_t count) 1025 { 1026 handle_write(tty->driver_data, buf, count); 1027 return count; 1028 } 1029 1030 /* 1031 * Put character routine for 3215 ttys 1032 */ 1033 static int tty3215_put_char(struct tty_struct *tty, u8 ch) 1034 { 1035 struct raw3215_info *raw = tty->driver_data; 1036 1037 raw3215_putchar(raw, ch); 1038 1039 return 1; 1040 } 1041 1042 static void tty3215_flush_chars(struct tty_struct *tty) 1043 { 1044 } 1045 1046 /* 1047 * Returns the number of characters in the output buffer 1048 */ 1049 static unsigned int tty3215_chars_in_buffer(struct tty_struct *tty) 1050 { 1051 struct raw3215_info *raw = tty->driver_data; 1052 1053 return raw->count; 1054 } 1055 1056 static void tty3215_flush_buffer(struct tty_struct *tty) 1057 { 1058 struct raw3215_info *raw = tty->driver_data; 1059 1060 raw3215_flush_buffer(raw); 1061 tty_wakeup(tty); 1062 } 1063 1064 /* 1065 * Disable reading from a 3215 tty 1066 */ 1067 static void tty3215_throttle(struct tty_struct *tty) 1068 { 1069 struct raw3215_info *raw = tty->driver_data; 1070 1071 raw->flags |= RAW3215_THROTTLED; 1072 } 1073 1074 /* 1075 * Enable reading from a 3215 tty 1076 */ 1077 static void tty3215_unthrottle(struct tty_struct *tty) 1078 { 1079 struct raw3215_info *raw = tty->driver_data; 1080 unsigned long flags; 1081 1082 if (raw->flags & RAW3215_THROTTLED) { 1083 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 1084 raw->flags &= ~RAW3215_THROTTLED; 1085 raw3215_try_io(raw); 1086 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 1087 } 1088 } 1089 1090 /* 1091 * Disable writing to a 3215 tty 1092 */ 1093 static void tty3215_stop(struct tty_struct *tty) 1094 { 1095 struct raw3215_info *raw = tty->driver_data; 1096 1097 raw->flags |= RAW3215_STOPPED; 1098 } 1099 1100 /* 1101 * Enable writing to a 3215 tty 1102 */ 1103 static void tty3215_start(struct tty_struct *tty) 1104 { 1105 struct raw3215_info *raw = tty->driver_data; 1106 unsigned long flags; 1107 1108 if (raw->flags & RAW3215_STOPPED) { 1109 spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags); 1110 raw->flags &= ~RAW3215_STOPPED; 1111 raw3215_try_io(raw); 1112 spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags); 1113 } 1114 } 1115 1116 static const struct tty_operations tty3215_ops = { 1117 .install = tty3215_install, 1118 .open = tty3215_open, 1119 .close = tty3215_close, 1120 .write = tty3215_write, 1121 .put_char = tty3215_put_char, 1122 .flush_chars = tty3215_flush_chars, 1123 .write_room = tty3215_write_room, 1124 .chars_in_buffer = tty3215_chars_in_buffer, 1125 .flush_buffer = tty3215_flush_buffer, 1126 .throttle = tty3215_throttle, 1127 .unthrottle = tty3215_unthrottle, 1128 .stop = tty3215_stop, 1129 .start = tty3215_start, 1130 }; 1131 1132 static int __init con3215_setup_drop(char *str) 1133 { 1134 bool drop; 1135 int rc; 1136 1137 rc = kstrtobool(str, &drop); 1138 if (!rc) 1139 con3215_drop = drop; 1140 return rc; 1141 } 1142 early_param("con3215_drop", con3215_setup_drop); 1143 1144 /* 1145 * 3215 tty registration code called from tty_init(). 1146 * Most kernel services (incl. kmalloc) are available at this poimt. 1147 */ 1148 static int __init tty3215_init(void) 1149 { 1150 struct tty_driver *driver; 1151 int ret; 1152 1153 if (!CONSOLE_IS_3215) 1154 return 0; 1155 1156 driver = tty_alloc_driver(NR_3215, TTY_DRIVER_REAL_RAW); 1157 if (IS_ERR(driver)) 1158 return PTR_ERR(driver); 1159 1160 ret = ccw_driver_register(&raw3215_ccw_driver); 1161 if (ret) { 1162 tty_driver_kref_put(driver); 1163 return ret; 1164 } 1165 /* 1166 * Initialize the tty_driver structure 1167 * Entries in tty3215_driver that are NOT initialized: 1168 * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc 1169 */ 1170 1171 driver->driver_name = "tty3215"; 1172 driver->name = "ttyS"; 1173 driver->major = TTY_MAJOR; 1174 driver->minor_start = 64; 1175 driver->type = TTY_DRIVER_TYPE_SYSTEM; 1176 driver->subtype = SYSTEM_TYPE_TTY; 1177 driver->init_termios = tty_std_termios; 1178 driver->init_termios.c_iflag = IGNBRK | IGNPAR; 1179 driver->init_termios.c_oflag = ONLCR; 1180 driver->init_termios.c_lflag = ISIG; 1181 tty_set_operations(driver, &tty3215_ops); 1182 ret = tty_register_driver(driver); 1183 if (ret) { 1184 tty_driver_kref_put(driver); 1185 return ret; 1186 } 1187 tty3215_driver = driver; 1188 return 0; 1189 } 1190 device_initcall(tty3215_init); 1191