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