1 /* 2 * SCLP VT220 terminal driver. 3 * 4 * Copyright IBM Corp. 2003, 2009 5 * 6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/spinlock.h> 11 #include <linux/list.h> 12 #include <linux/wait.h> 13 #include <linux/timer.h> 14 #include <linux/kernel.h> 15 #include <linux/tty.h> 16 #include <linux/tty_driver.h> 17 #include <linux/tty_flip.h> 18 #include <linux/errno.h> 19 #include <linux/mm.h> 20 #include <linux/major.h> 21 #include <linux/console.h> 22 #include <linux/kdev_t.h> 23 #include <linux/interrupt.h> 24 #include <linux/init.h> 25 #include <linux/reboot.h> 26 #include <linux/slab.h> 27 28 #include <asm/uaccess.h> 29 #include "sclp.h" 30 31 #define SCLP_VT220_MAJOR TTY_MAJOR 32 #define SCLP_VT220_MINOR 65 33 #define SCLP_VT220_DRIVER_NAME "sclp_vt220" 34 #define SCLP_VT220_DEVICE_NAME "ttysclp" 35 #define SCLP_VT220_CONSOLE_NAME "ttyS" 36 #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */ 37 38 /* Representation of a single write request */ 39 struct sclp_vt220_request { 40 struct list_head list; 41 struct sclp_req sclp_req; 42 int retry_count; 43 }; 44 45 /* VT220 SCCB */ 46 struct sclp_vt220_sccb { 47 struct sccb_header header; 48 struct evbuf_header evbuf; 49 }; 50 51 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \ 52 sizeof(struct sclp_vt220_request) - \ 53 sizeof(struct sclp_vt220_sccb)) 54 55 /* Structures and data needed to register tty driver */ 56 static struct tty_driver *sclp_vt220_driver; 57 58 static struct tty_port sclp_vt220_port; 59 60 /* Lock to protect internal data from concurrent access */ 61 static spinlock_t sclp_vt220_lock; 62 63 /* List of empty pages to be used as write request buffers */ 64 static struct list_head sclp_vt220_empty; 65 66 /* List of pending requests */ 67 static struct list_head sclp_vt220_outqueue; 68 69 /* Suspend mode flag */ 70 static int sclp_vt220_suspended; 71 72 /* Flag that output queue is currently running */ 73 static int sclp_vt220_queue_running; 74 75 /* Timer used for delaying write requests to merge subsequent messages into 76 * a single buffer */ 77 static struct timer_list sclp_vt220_timer; 78 79 /* Pointer to current request buffer which has been partially filled but not 80 * yet sent */ 81 static struct sclp_vt220_request *sclp_vt220_current_request; 82 83 /* Number of characters in current request buffer */ 84 static int sclp_vt220_buffered_chars; 85 86 /* Counter controlling core driver initialization. */ 87 static int __initdata sclp_vt220_init_count; 88 89 /* Flag indicating that sclp_vt220_current_request should really 90 * have been already queued but wasn't because the SCLP was processing 91 * another buffer */ 92 static int sclp_vt220_flush_later; 93 94 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf); 95 static void sclp_vt220_pm_event_fn(struct sclp_register *reg, 96 enum sclp_pm_event sclp_pm_event); 97 static int __sclp_vt220_emit(struct sclp_vt220_request *request); 98 static void sclp_vt220_emit_current(void); 99 100 /* Registration structure for our interest in SCLP event buffers */ 101 static struct sclp_register sclp_vt220_register = { 102 .send_mask = EVTYP_VT220MSG_MASK, 103 .receive_mask = EVTYP_VT220MSG_MASK, 104 .state_change_fn = NULL, 105 .receiver_fn = sclp_vt220_receiver_fn, 106 .pm_event_fn = sclp_vt220_pm_event_fn, 107 }; 108 109 110 /* 111 * Put provided request buffer back into queue and check emit pending 112 * buffers if necessary. 113 */ 114 static void 115 sclp_vt220_process_queue(struct sclp_vt220_request *request) 116 { 117 struct tty_struct *tty; 118 unsigned long flags; 119 void *page; 120 121 do { 122 /* Put buffer back to list of empty buffers */ 123 page = request->sclp_req.sccb; 124 spin_lock_irqsave(&sclp_vt220_lock, flags); 125 /* Move request from outqueue to empty queue */ 126 list_del(&request->list); 127 list_add_tail((struct list_head *) page, &sclp_vt220_empty); 128 /* Check if there is a pending buffer on the out queue. */ 129 request = NULL; 130 if (!list_empty(&sclp_vt220_outqueue)) 131 request = list_entry(sclp_vt220_outqueue.next, 132 struct sclp_vt220_request, list); 133 if (!request || sclp_vt220_suspended) { 134 sclp_vt220_queue_running = 0; 135 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 136 break; 137 } 138 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 139 } while (__sclp_vt220_emit(request)); 140 if (request == NULL && sclp_vt220_flush_later) 141 sclp_vt220_emit_current(); 142 /* Check if the tty needs a wake up call */ 143 tty = tty_port_tty_get(&sclp_vt220_port); 144 if (tty) { 145 tty_wakeup(tty); 146 tty_kref_put(tty); 147 } 148 } 149 150 #define SCLP_BUFFER_MAX_RETRY 1 151 152 /* 153 * Callback through which the result of a write request is reported by the 154 * SCLP. 155 */ 156 static void 157 sclp_vt220_callback(struct sclp_req *request, void *data) 158 { 159 struct sclp_vt220_request *vt220_request; 160 struct sclp_vt220_sccb *sccb; 161 162 vt220_request = (struct sclp_vt220_request *) data; 163 if (request->status == SCLP_REQ_FAILED) { 164 sclp_vt220_process_queue(vt220_request); 165 return; 166 } 167 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb; 168 169 /* Check SCLP response code and choose suitable action */ 170 switch (sccb->header.response_code) { 171 case 0x0020 : 172 break; 173 174 case 0x05f0: /* Target resource in improper state */ 175 break; 176 177 case 0x0340: /* Contained SCLP equipment check */ 178 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY) 179 break; 180 /* Remove processed buffers and requeue rest */ 181 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) { 182 /* Not all buffers were processed */ 183 sccb->header.response_code = 0x0000; 184 vt220_request->sclp_req.status = SCLP_REQ_FILLED; 185 if (sclp_add_request(request) == 0) 186 return; 187 } 188 break; 189 190 case 0x0040: /* SCLP equipment check */ 191 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY) 192 break; 193 sccb->header.response_code = 0x0000; 194 vt220_request->sclp_req.status = SCLP_REQ_FILLED; 195 if (sclp_add_request(request) == 0) 196 return; 197 break; 198 199 default: 200 break; 201 } 202 sclp_vt220_process_queue(vt220_request); 203 } 204 205 /* 206 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero 207 * otherwise. 208 */ 209 static int 210 __sclp_vt220_emit(struct sclp_vt220_request *request) 211 { 212 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) { 213 request->sclp_req.status = SCLP_REQ_FAILED; 214 return -EIO; 215 } 216 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA; 217 request->sclp_req.status = SCLP_REQ_FILLED; 218 request->sclp_req.callback = sclp_vt220_callback; 219 request->sclp_req.callback_data = (void *) request; 220 221 return sclp_add_request(&request->sclp_req); 222 } 223 224 /* 225 * Queue and emit current request. 226 */ 227 static void 228 sclp_vt220_emit_current(void) 229 { 230 unsigned long flags; 231 struct sclp_vt220_request *request; 232 struct sclp_vt220_sccb *sccb; 233 234 spin_lock_irqsave(&sclp_vt220_lock, flags); 235 if (sclp_vt220_current_request) { 236 sccb = (struct sclp_vt220_sccb *) 237 sclp_vt220_current_request->sclp_req.sccb; 238 /* Only emit buffers with content */ 239 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) { 240 list_add_tail(&sclp_vt220_current_request->list, 241 &sclp_vt220_outqueue); 242 sclp_vt220_current_request = NULL; 243 if (timer_pending(&sclp_vt220_timer)) 244 del_timer(&sclp_vt220_timer); 245 } 246 sclp_vt220_flush_later = 0; 247 } 248 if (sclp_vt220_queue_running || sclp_vt220_suspended) 249 goto out_unlock; 250 if (list_empty(&sclp_vt220_outqueue)) 251 goto out_unlock; 252 request = list_first_entry(&sclp_vt220_outqueue, 253 struct sclp_vt220_request, list); 254 sclp_vt220_queue_running = 1; 255 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 256 257 if (__sclp_vt220_emit(request)) 258 sclp_vt220_process_queue(request); 259 return; 260 out_unlock: 261 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 262 } 263 264 #define SCLP_NORMAL_WRITE 0x00 265 266 /* 267 * Helper function to initialize a page with the sclp request structure. 268 */ 269 static struct sclp_vt220_request * 270 sclp_vt220_initialize_page(void *page) 271 { 272 struct sclp_vt220_request *request; 273 struct sclp_vt220_sccb *sccb; 274 275 /* Place request structure at end of page */ 276 request = ((struct sclp_vt220_request *) 277 ((addr_t) page + PAGE_SIZE)) - 1; 278 request->retry_count = 0; 279 request->sclp_req.sccb = page; 280 /* SCCB goes at start of page */ 281 sccb = (struct sclp_vt220_sccb *) page; 282 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb)); 283 sccb->header.length = sizeof(struct sclp_vt220_sccb); 284 sccb->header.function_code = SCLP_NORMAL_WRITE; 285 sccb->header.response_code = 0x0000; 286 sccb->evbuf.type = EVTYP_VT220MSG; 287 sccb->evbuf.length = sizeof(struct evbuf_header); 288 289 return request; 290 } 291 292 static inline unsigned int 293 sclp_vt220_space_left(struct sclp_vt220_request *request) 294 { 295 struct sclp_vt220_sccb *sccb; 296 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb; 297 return PAGE_SIZE - sizeof(struct sclp_vt220_request) - 298 sccb->header.length; 299 } 300 301 static inline unsigned int 302 sclp_vt220_chars_stored(struct sclp_vt220_request *request) 303 { 304 struct sclp_vt220_sccb *sccb; 305 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb; 306 return sccb->evbuf.length - sizeof(struct evbuf_header); 307 } 308 309 /* 310 * Add msg to buffer associated with request. Return the number of characters 311 * added. 312 */ 313 static int 314 sclp_vt220_add_msg(struct sclp_vt220_request *request, 315 const unsigned char *msg, int count, int convertlf) 316 { 317 struct sclp_vt220_sccb *sccb; 318 void *buffer; 319 unsigned char c; 320 int from; 321 int to; 322 323 if (count > sclp_vt220_space_left(request)) 324 count = sclp_vt220_space_left(request); 325 if (count <= 0) 326 return 0; 327 328 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb; 329 buffer = (void *) ((addr_t) sccb + sccb->header.length); 330 331 if (convertlf) { 332 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/ 333 for (from=0, to=0; 334 (from < count) && (to < sclp_vt220_space_left(request)); 335 from++) { 336 /* Retrieve character */ 337 c = msg[from]; 338 /* Perform conversion */ 339 if (c == 0x0a) { 340 if (to + 1 < sclp_vt220_space_left(request)) { 341 ((unsigned char *) buffer)[to++] = c; 342 ((unsigned char *) buffer)[to++] = 0x0d; 343 } else 344 break; 345 346 } else 347 ((unsigned char *) buffer)[to++] = c; 348 } 349 sccb->header.length += to; 350 sccb->evbuf.length += to; 351 return from; 352 } else { 353 memcpy(buffer, (const void *) msg, count); 354 sccb->header.length += count; 355 sccb->evbuf.length += count; 356 return count; 357 } 358 } 359 360 /* 361 * Emit buffer after having waited long enough for more data to arrive. 362 */ 363 static void 364 sclp_vt220_timeout(unsigned long data) 365 { 366 sclp_vt220_emit_current(); 367 } 368 369 #define BUFFER_MAX_DELAY HZ/20 370 371 /* 372 * Internal implementation of the write function. Write COUNT bytes of data 373 * from memory at BUF 374 * to the SCLP interface. In case that the data does not fit into the current 375 * write buffer, emit the current one and allocate a new one. If there are no 376 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE 377 * is non-zero, the buffer will be scheduled for emitting after a timeout - 378 * otherwise the user has to explicitly call the flush function. 379 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message 380 * buffer should be converted to 0x0a 0x0d. After completion, return the number 381 * of bytes written. 382 */ 383 static int 384 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule, 385 int convertlf, int may_fail) 386 { 387 unsigned long flags; 388 void *page; 389 int written; 390 int overall_written; 391 392 if (count <= 0) 393 return 0; 394 overall_written = 0; 395 spin_lock_irqsave(&sclp_vt220_lock, flags); 396 do { 397 /* Create an sclp output buffer if none exists yet */ 398 if (sclp_vt220_current_request == NULL) { 399 while (list_empty(&sclp_vt220_empty)) { 400 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 401 if (may_fail || sclp_vt220_suspended) 402 goto out; 403 else 404 sclp_sync_wait(); 405 spin_lock_irqsave(&sclp_vt220_lock, flags); 406 } 407 page = (void *) sclp_vt220_empty.next; 408 list_del((struct list_head *) page); 409 sclp_vt220_current_request = 410 sclp_vt220_initialize_page(page); 411 } 412 /* Try to write the string to the current request buffer */ 413 written = sclp_vt220_add_msg(sclp_vt220_current_request, 414 buf, count, convertlf); 415 overall_written += written; 416 if (written == count) 417 break; 418 /* 419 * Not all characters could be written to the current 420 * output buffer. Emit the buffer, create a new buffer 421 * and then output the rest of the string. 422 */ 423 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 424 sclp_vt220_emit_current(); 425 spin_lock_irqsave(&sclp_vt220_lock, flags); 426 buf += written; 427 count -= written; 428 } while (count > 0); 429 /* Setup timer to output current console buffer after some time */ 430 if (sclp_vt220_current_request != NULL && 431 !timer_pending(&sclp_vt220_timer) && do_schedule) { 432 sclp_vt220_timer.function = sclp_vt220_timeout; 433 sclp_vt220_timer.data = 0UL; 434 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY; 435 add_timer(&sclp_vt220_timer); 436 } 437 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 438 out: 439 return overall_written; 440 } 441 442 /* 443 * This routine is called by the kernel to write a series of 444 * characters to the tty device. The characters may come from 445 * user space or kernel space. This routine will return the 446 * number of characters actually accepted for writing. 447 */ 448 static int 449 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count) 450 { 451 return __sclp_vt220_write(buf, count, 1, 0, 1); 452 } 453 454 #define SCLP_VT220_SESSION_ENDED 0x01 455 #define SCLP_VT220_SESSION_STARTED 0x80 456 #define SCLP_VT220_SESSION_DATA 0x00 457 458 /* 459 * Called by the SCLP to report incoming event buffers. 460 */ 461 static void 462 sclp_vt220_receiver_fn(struct evbuf_header *evbuf) 463 { 464 char *buffer; 465 unsigned int count; 466 467 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header)); 468 count = evbuf->length - sizeof(struct evbuf_header); 469 470 switch (*buffer) { 471 case SCLP_VT220_SESSION_ENDED: 472 case SCLP_VT220_SESSION_STARTED: 473 break; 474 case SCLP_VT220_SESSION_DATA: 475 /* Send input to line discipline */ 476 buffer++; 477 count--; 478 tty_insert_flip_string(&sclp_vt220_port, buffer, count); 479 tty_flip_buffer_push(&sclp_vt220_port); 480 break; 481 } 482 } 483 484 /* 485 * This routine is called when a particular tty device is opened. 486 */ 487 static int 488 sclp_vt220_open(struct tty_struct *tty, struct file *filp) 489 { 490 if (tty->count == 1) { 491 tty_port_tty_set(&sclp_vt220_port, tty); 492 sclp_vt220_port.low_latency = 0; 493 if (!tty->winsize.ws_row && !tty->winsize.ws_col) { 494 tty->winsize.ws_row = 24; 495 tty->winsize.ws_col = 80; 496 } 497 } 498 return 0; 499 } 500 501 /* 502 * This routine is called when a particular tty device is closed. 503 */ 504 static void 505 sclp_vt220_close(struct tty_struct *tty, struct file *filp) 506 { 507 if (tty->count == 1) 508 tty_port_tty_set(&sclp_vt220_port, NULL); 509 } 510 511 /* 512 * This routine is called by the kernel to write a single 513 * character to the tty device. If the kernel uses this routine, 514 * it must call the flush_chars() routine (if defined) when it is 515 * done stuffing characters into the driver. 516 */ 517 static int 518 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch) 519 { 520 return __sclp_vt220_write(&ch, 1, 0, 0, 1); 521 } 522 523 /* 524 * This routine is called by the kernel after it has written a 525 * series of characters to the tty device using put_char(). 526 */ 527 static void 528 sclp_vt220_flush_chars(struct tty_struct *tty) 529 { 530 if (!sclp_vt220_queue_running) 531 sclp_vt220_emit_current(); 532 else 533 sclp_vt220_flush_later = 1; 534 } 535 536 /* 537 * This routine returns the numbers of characters the tty driver 538 * will accept for queuing to be written. This number is subject 539 * to change as output buffers get emptied, or if the output flow 540 * control is acted. 541 */ 542 static int 543 sclp_vt220_write_room(struct tty_struct *tty) 544 { 545 unsigned long flags; 546 struct list_head *l; 547 int count; 548 549 spin_lock_irqsave(&sclp_vt220_lock, flags); 550 count = 0; 551 if (sclp_vt220_current_request != NULL) 552 count = sclp_vt220_space_left(sclp_vt220_current_request); 553 list_for_each(l, &sclp_vt220_empty) 554 count += SCLP_VT220_MAX_CHARS_PER_BUFFER; 555 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 556 return count; 557 } 558 559 /* 560 * Return number of buffered chars. 561 */ 562 static int 563 sclp_vt220_chars_in_buffer(struct tty_struct *tty) 564 { 565 unsigned long flags; 566 struct list_head *l; 567 struct sclp_vt220_request *r; 568 int count; 569 570 spin_lock_irqsave(&sclp_vt220_lock, flags); 571 count = 0; 572 if (sclp_vt220_current_request != NULL) 573 count = sclp_vt220_chars_stored(sclp_vt220_current_request); 574 list_for_each(l, &sclp_vt220_outqueue) { 575 r = list_entry(l, struct sclp_vt220_request, list); 576 count += sclp_vt220_chars_stored(r); 577 } 578 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 579 return count; 580 } 581 582 /* 583 * Pass on all buffers to the hardware. Return only when there are no more 584 * buffers pending. 585 */ 586 static void 587 sclp_vt220_flush_buffer(struct tty_struct *tty) 588 { 589 sclp_vt220_emit_current(); 590 } 591 592 /* Release allocated pages. */ 593 static void __init __sclp_vt220_free_pages(void) 594 { 595 struct list_head *page, *p; 596 597 list_for_each_safe(page, p, &sclp_vt220_empty) { 598 list_del(page); 599 free_page((unsigned long) page); 600 } 601 } 602 603 /* Release memory and unregister from sclp core. Controlled by init counting - 604 * only the last invoker will actually perform these actions. */ 605 static void __init __sclp_vt220_cleanup(void) 606 { 607 sclp_vt220_init_count--; 608 if (sclp_vt220_init_count != 0) 609 return; 610 sclp_unregister(&sclp_vt220_register); 611 __sclp_vt220_free_pages(); 612 tty_port_destroy(&sclp_vt220_port); 613 } 614 615 /* Allocate buffer pages and register with sclp core. Controlled by init 616 * counting - only the first invoker will actually perform these actions. */ 617 static int __init __sclp_vt220_init(int num_pages) 618 { 619 void *page; 620 int i; 621 int rc; 622 623 sclp_vt220_init_count++; 624 if (sclp_vt220_init_count != 1) 625 return 0; 626 spin_lock_init(&sclp_vt220_lock); 627 INIT_LIST_HEAD(&sclp_vt220_empty); 628 INIT_LIST_HEAD(&sclp_vt220_outqueue); 629 init_timer(&sclp_vt220_timer); 630 tty_port_init(&sclp_vt220_port); 631 sclp_vt220_current_request = NULL; 632 sclp_vt220_buffered_chars = 0; 633 sclp_vt220_flush_later = 0; 634 635 /* Allocate pages for output buffering */ 636 rc = -ENOMEM; 637 for (i = 0; i < num_pages; i++) { 638 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 639 if (!page) 640 goto out; 641 list_add_tail(page, &sclp_vt220_empty); 642 } 643 rc = sclp_register(&sclp_vt220_register); 644 out: 645 if (rc) { 646 __sclp_vt220_free_pages(); 647 sclp_vt220_init_count--; 648 tty_port_destroy(&sclp_vt220_port); 649 } 650 return rc; 651 } 652 653 static const struct tty_operations sclp_vt220_ops = { 654 .open = sclp_vt220_open, 655 .close = sclp_vt220_close, 656 .write = sclp_vt220_write, 657 .put_char = sclp_vt220_put_char, 658 .flush_chars = sclp_vt220_flush_chars, 659 .write_room = sclp_vt220_write_room, 660 .chars_in_buffer = sclp_vt220_chars_in_buffer, 661 .flush_buffer = sclp_vt220_flush_buffer, 662 }; 663 664 /* 665 * Register driver with SCLP and Linux and initialize internal tty structures. 666 */ 667 static int __init sclp_vt220_tty_init(void) 668 { 669 struct tty_driver *driver; 670 int rc; 671 672 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve 673 * symmetry between VM and LPAR systems regarding ttyS1. */ 674 driver = alloc_tty_driver(1); 675 if (!driver) 676 return -ENOMEM; 677 rc = __sclp_vt220_init(MAX_KMEM_PAGES); 678 if (rc) 679 goto out_driver; 680 681 driver->driver_name = SCLP_VT220_DRIVER_NAME; 682 driver->name = SCLP_VT220_DEVICE_NAME; 683 driver->major = SCLP_VT220_MAJOR; 684 driver->minor_start = SCLP_VT220_MINOR; 685 driver->type = TTY_DRIVER_TYPE_SYSTEM; 686 driver->subtype = SYSTEM_TYPE_TTY; 687 driver->init_termios = tty_std_termios; 688 driver->flags = TTY_DRIVER_REAL_RAW; 689 tty_set_operations(driver, &sclp_vt220_ops); 690 tty_port_link_device(&sclp_vt220_port, driver, 0); 691 692 rc = tty_register_driver(driver); 693 if (rc) 694 goto out_init; 695 sclp_vt220_driver = driver; 696 return 0; 697 698 out_init: 699 __sclp_vt220_cleanup(); 700 out_driver: 701 put_tty_driver(driver); 702 return rc; 703 } 704 __initcall(sclp_vt220_tty_init); 705 706 static void __sclp_vt220_flush_buffer(void) 707 { 708 unsigned long flags; 709 710 sclp_vt220_emit_current(); 711 spin_lock_irqsave(&sclp_vt220_lock, flags); 712 if (timer_pending(&sclp_vt220_timer)) 713 del_timer(&sclp_vt220_timer); 714 while (sclp_vt220_queue_running) { 715 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 716 sclp_sync_wait(); 717 spin_lock_irqsave(&sclp_vt220_lock, flags); 718 } 719 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 720 } 721 722 /* 723 * Resume console: If there are cached messages, emit them. 724 */ 725 static void sclp_vt220_resume(void) 726 { 727 unsigned long flags; 728 729 spin_lock_irqsave(&sclp_vt220_lock, flags); 730 sclp_vt220_suspended = 0; 731 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 732 sclp_vt220_emit_current(); 733 } 734 735 /* 736 * Suspend console: Set suspend flag and flush console 737 */ 738 static void sclp_vt220_suspend(void) 739 { 740 unsigned long flags; 741 742 spin_lock_irqsave(&sclp_vt220_lock, flags); 743 sclp_vt220_suspended = 1; 744 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 745 __sclp_vt220_flush_buffer(); 746 } 747 748 static void sclp_vt220_pm_event_fn(struct sclp_register *reg, 749 enum sclp_pm_event sclp_pm_event) 750 { 751 switch (sclp_pm_event) { 752 case SCLP_PM_EVENT_FREEZE: 753 sclp_vt220_suspend(); 754 break; 755 case SCLP_PM_EVENT_RESTORE: 756 case SCLP_PM_EVENT_THAW: 757 sclp_vt220_resume(); 758 break; 759 } 760 } 761 762 #ifdef CONFIG_SCLP_VT220_CONSOLE 763 764 static void 765 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count) 766 { 767 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0); 768 } 769 770 static struct tty_driver * 771 sclp_vt220_con_device(struct console *c, int *index) 772 { 773 *index = 0; 774 return sclp_vt220_driver; 775 } 776 777 static int 778 sclp_vt220_notify(struct notifier_block *self, 779 unsigned long event, void *data) 780 { 781 __sclp_vt220_flush_buffer(); 782 return NOTIFY_OK; 783 } 784 785 static struct notifier_block on_panic_nb = { 786 .notifier_call = sclp_vt220_notify, 787 .priority = 1, 788 }; 789 790 static struct notifier_block on_reboot_nb = { 791 .notifier_call = sclp_vt220_notify, 792 .priority = 1, 793 }; 794 795 /* Structure needed to register with printk */ 796 static struct console sclp_vt220_console = 797 { 798 .name = SCLP_VT220_CONSOLE_NAME, 799 .write = sclp_vt220_con_write, 800 .device = sclp_vt220_con_device, 801 .flags = CON_PRINTBUFFER, 802 .index = SCLP_VT220_CONSOLE_INDEX 803 }; 804 805 static int __init 806 sclp_vt220_con_init(void) 807 { 808 int rc; 809 810 if (!CONSOLE_IS_SCLP) 811 return 0; 812 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES); 813 if (rc) 814 return rc; 815 /* Attach linux console */ 816 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb); 817 register_reboot_notifier(&on_reboot_nb); 818 register_console(&sclp_vt220_console); 819 return 0; 820 } 821 822 console_initcall(sclp_vt220_con_init); 823 #endif /* CONFIG_SCLP_VT220_CONSOLE */ 824 825