Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 | // SPDX-License-Identifier: GPL-2.0 /* * IBM/3270 Driver - console view. * * Author(s): * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global) * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com> * Copyright IBM Corp. 2003, 2009 */ #include <linux/module.h> #include <linux/console.h> #include <linux/init.h> #include <linux/interrupt.h> #include <linux/list.h> #include <linux/panic_notifier.h> #include <linux/types.h> #include <linux/slab.h> #include <linux/err.h> #include <linux/reboot.h> #include <asm/ccwdev.h> #include <asm/cio.h> #include <asm/cpcmd.h> #include <asm/ebcdic.h> #include "raw3270.h" #include "tty3270.h" #include "ctrlchar.h" #define CON3270_OUTPUT_BUFFER_SIZE 1024 #define CON3270_STRING_PAGES 4 static struct raw3270_fn con3270_fn; static bool auto_update = true; module_param(auto_update, bool, 0); /* * Main 3270 console view data structure. */ struct con3270 { struct raw3270_view view; struct list_head freemem; /* list of free memory for strings. */ /* Output stuff. */ struct list_head lines; /* list of lines. */ struct list_head update; /* list of lines to update. */ int line_nr; /* line number for next update. */ int nr_lines; /* # lines in list. */ int nr_up; /* # lines up in history. */ unsigned long update_flags; /* Update indication bits. */ struct string *cline; /* current output line. */ struct string *status; /* last line of display. */ struct raw3270_request *write; /* single write request. */ struct timer_list timer; /* Input stuff. */ struct string *input; /* input string for read request. */ struct raw3270_request *read; /* single read request. */ struct raw3270_request *kreset; /* single keyboard reset request. */ struct tasklet_struct readlet; /* tasklet to issue read request. */ }; static struct con3270 *condev; /* con3270->update_flags. See con3270_update for details. */ #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */ #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */ #define CON_UPDATE_STATUS 4 /* Update status line. */ #define CON_UPDATE_ALL 8 /* Recreate screen. */ static void con3270_update(struct timer_list *); /* * Setup timeout for a device. On timeout trigger an update. */ static void con3270_set_timer(struct con3270 *cp, int expires) { if (expires == 0) del_timer(&cp->timer); else mod_timer(&cp->timer, jiffies + expires); } /* * The status line is the last line of the screen. It shows the string * "console view" in the lower left corner and "Running"/"More..."/"Holding" * in the lower right corner of the screen. */ static void con3270_update_status(struct con3270 *cp) { char *str; str = (cp->nr_up != 0) ? "History" : "Running"; memcpy(cp->status->string + 24, str, 7); codepage_convert(cp->view.ascebc, cp->status->string + 24, 7); cp->update_flags |= CON_UPDATE_STATUS; } static void con3270_create_status(struct con3270 *cp) { static const unsigned char blueprint[] = { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN, 'c','o','n','s','o','l','e',' ','v','i','e','w', TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG }; cp->status = alloc_string(&cp->freemem, sizeof(blueprint)); /* Copy blueprint to status line */ memcpy(cp->status->string, blueprint, sizeof(blueprint)); /* Set TO_RA addresses. */ raw3270_buffer_address(cp->view.dev, cp->status->string + 1, cp->view.cols * (cp->view.rows - 1)); raw3270_buffer_address(cp->view.dev, cp->status->string + 21, cp->view.cols * cp->view.rows - 8); /* Convert strings to ebcdic. */ codepage_convert(cp->view.ascebc, cp->status->string + 8, 12); codepage_convert(cp->view.ascebc, cp->status->string + 24, 7); } /* * Set output offsets to 3270 datastream fragment of a console string. */ static void con3270_update_string(struct con3270 *cp, struct string *s, int nr) { if (s->len < 4) { /* This indicates a bug, but printing a warning would * cause a deadlock. */ return; } if (s->string[s->len - 4] != TO_RA) return; raw3270_buffer_address(cp->view.dev, s->string + s->len - 3, cp->view.cols * (nr + 1)); } /* * Rebuild update list to print all lines. */ static void con3270_rebuild_update(struct con3270 *cp) { struct string *s, *n; int nr; /* * Throw away update list and create a new one, * containing all lines that will fit on the screen. */ list_for_each_entry_safe(s, n, &cp->update, update) list_del_init(&s->update); nr = cp->view.rows - 2 + cp->nr_up; list_for_each_entry_reverse(s, &cp->lines, list) { if (nr < cp->view.rows - 1) list_add(&s->update, &cp->update); if (--nr < 0) break; } cp->line_nr = 0; cp->update_flags |= CON_UPDATE_LIST; } /* * Alloc string for size bytes. Free strings from history if necessary. */ static struct string * con3270_alloc_string(struct con3270 *cp, size_t size) { struct string *s, *n; s = alloc_string(&cp->freemem, size); if (s) return s; list_for_each_entry_safe(s, n, &cp->lines, list) { list_del(&s->list); if (!list_empty(&s->update)) list_del(&s->update); cp->nr_lines--; if (free_string(&cp->freemem, s) >= size) break; } s = alloc_string(&cp->freemem, size); BUG_ON(!s); if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) { cp->nr_up = cp->nr_lines - cp->view.rows + 1; con3270_rebuild_update(cp); con3270_update_status(cp); } return s; } /* * Write completion callback. */ static void con3270_write_callback(struct raw3270_request *rq, void *data) { raw3270_request_reset(rq); xchg(&((struct con3270 *) rq->view)->write, rq); } /* * Update console display. */ static void con3270_update(struct timer_list *t) { struct con3270 *cp = from_timer(cp, t, timer); struct raw3270_request *wrq; char wcc, prolog[6]; unsigned long flags; unsigned long updated; struct string *s, *n; int rc; if (!auto_update && !raw3270_view_active(&cp->view)) return; if (cp->view.dev) raw3270_activate_view(&cp->view); wrq = xchg(&cp->write, 0); if (!wrq) { con3270_set_timer(cp, 1); return; } spin_lock_irqsave(&cp->view.lock, flags); updated = 0; if (cp->update_flags & CON_UPDATE_ALL) { con3270_rebuild_update(cp); con3270_update_status(cp); cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST | CON_UPDATE_STATUS; } if (cp->update_flags & CON_UPDATE_ERASE) { /* Use erase write alternate to initialize display. */ raw3270_request_set_cmd(wrq, TC_EWRITEA); updated |= CON_UPDATE_ERASE; } else raw3270_request_set_cmd(wrq, TC_WRITE); wcc = TW_NONE; raw3270_request_add_data(wrq, &wcc, 1); /* * Update status line. */ if (cp->update_flags & CON_UPDATE_STATUS) if (raw3270_request_add_data(wrq, cp->status->string, cp->status->len) == 0) updated |= CON_UPDATE_STATUS; if (cp->update_flags & CON_UPDATE_LIST) { prolog[0] = TO_SBA; prolog[3] = TO_SA; prolog[4] = TAT_COLOR; prolog[5] = TAC_TURQ; raw3270_buffer_address(cp->view.dev, prolog + 1, cp->view.cols * cp->line_nr); raw3270_request_add_data(wrq, prolog, 6); /* Write strings in the update list to the screen. */ list_for_each_entry_safe(s, n, &cp->update, update) { if (s != cp->cline) con3270_update_string(cp, s, cp->line_nr); if (raw3270_request_add_data(wrq, s->string, s->len) != 0) break; list_del_init(&s->update); if (s != cp->cline) cp->line_nr++; } if (list_empty(&cp->update)) updated |= CON_UPDATE_LIST; } wrq->callback = con3270_write_callback; rc = raw3270_start(&cp->view, wrq); if (rc == 0) { cp->update_flags &= ~updated; if (cp->update_flags) con3270_set_timer(cp, 1); } else { raw3270_request_reset(wrq); xchg(&cp->write, wrq); } spin_unlock_irqrestore(&cp->view.lock, flags); } /* * Read tasklet. */ static void con3270_read_tasklet(unsigned long data) { static char kreset_data = TW_KR; struct raw3270_request *rrq; struct con3270 *cp; unsigned long flags; int nr_up, deactivate; rrq = (struct raw3270_request *)data; cp = (struct con3270 *) rrq->view; spin_lock_irqsave(&cp->view.lock, flags); nr_up = cp->nr_up; deactivate = 0; /* Check aid byte. */ switch (cp->input->string[0]) { case 0x7d: /* enter: jump to bottom. */ nr_up = 0; break; case 0xf3: /* PF3: deactivate the console view. */ deactivate = 1; break; case 0x6d: /* clear: start from scratch. */ cp->update_flags = CON_UPDATE_ALL; con3270_set_timer(cp, 1); break; case 0xf7: /* PF7: do a page up in the console log. */ nr_up += cp->view.rows - 2; if (nr_up + cp->view.rows - 1 > cp->nr_lines) { nr_up = cp->nr_lines - cp->view.rows + 1; if (nr_up < 0) nr_up = 0; } break; case 0xf8: /* PF8: do a page down in the console log. */ nr_up -= cp->view.rows - 2; if (nr_up < 0) nr_up = 0; break; } if (nr_up != cp->nr_up) { cp->nr_up = nr_up; con3270_rebuild_update(cp); con3270_update_status(cp); con3270_set_timer(cp, 1); } spin_unlock_irqrestore(&cp->view.lock, flags); /* Start keyboard reset command. */ raw3270_request_reset(cp->kreset); raw3270_request_set_cmd(cp->kreset, TC_WRITE); raw3270_request_add_data(cp->kreset, &kreset_data, 1); raw3270_start(&cp->view, cp->kreset); if (deactivate) raw3270_deactivate_view(&cp->view); raw3270_request_reset(rrq); xchg(&cp->read, rrq); raw3270_put_view(&cp->view); } /* * Read request completion callback. */ static void con3270_read_callback(struct raw3270_request *rq, void *data) { raw3270_get_view(rq->view); /* Schedule tasklet to pass input to tty. */ tasklet_schedule(&((struct con3270 *) rq->view)->readlet); } /* * Issue a read request. Called only from interrupt function. */ static void con3270_issue_read(struct con3270 *cp) { struct raw3270_request *rrq; int rc; rrq = xchg(&cp->read, 0); if (!rrq) /* Read already scheduled. */ return; rrq->callback = con3270_read_callback; rrq->callback_data = cp; raw3270_request_set_cmd(rrq, TC_READMOD); raw3270_request_set_data(rrq, cp->input->string, cp->input->len); /* Issue the read modified request. */ rc = raw3270_start_irq(&cp->view, rrq); if (rc) raw3270_request_reset(rrq); } /* * Switch to the console view. */ static int con3270_activate(struct raw3270_view *view) { struct con3270 *cp; cp = (struct con3270 *) view; cp->update_flags = CON_UPDATE_ALL; con3270_set_timer(cp, 1); return 0; } static void con3270_deactivate(struct raw3270_view *view) { struct con3270 *cp; cp = (struct con3270 *) view; del_timer(&cp->timer); } static void con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb) { /* Handle ATTN. Schedule tasklet to read aid. */ if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) con3270_issue_read(cp); if (rq) { if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) rq->rc = -EIO; else /* Normal end. Copy residual count. */ rq->rescnt = irb->scsw.cmd.count; } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) { /* Interrupt without an outstanding request -> update all */ cp->update_flags = CON_UPDATE_ALL; con3270_set_timer(cp, 1); } } /* Console view to a 3270 device. */ static struct raw3270_fn con3270_fn = { .activate = con3270_activate, .deactivate = con3270_deactivate, .intv = (void *) con3270_irq }; static inline void con3270_cline_add(struct con3270 *cp) { if (!list_empty(&cp->cline->list)) /* Already added. */ return; list_add_tail(&cp->cline->list, &cp->lines); cp->nr_lines++; con3270_rebuild_update(cp); } static inline void con3270_cline_insert(struct con3270 *cp, unsigned char c) { cp->cline->string[cp->cline->len++] = cp->view.ascebc[(c < ' ') ? ' ' : c]; if (list_empty(&cp->cline->update)) { list_add_tail(&cp->cline->update, &cp->update); cp->update_flags |= CON_UPDATE_LIST; } } static inline void con3270_cline_end(struct con3270 *cp) { struct string *s; unsigned int size; /* Copy cline. */ size = (cp->cline->len < cp->view.cols - 5) ? cp->cline->len + 4 : cp->view.cols; s = con3270_alloc_string(cp, size); memcpy(s->string, cp->cline->string, cp->cline->len); if (cp->cline->len < cp->view.cols - 5) { s->string[s->len - 4] = TO_RA; s->string[s->len - 1] = 0; } else { while (--size >= cp->cline->len) s->string[size] = cp->view.ascebc[' ']; } /* Replace cline with allocated line s and reset cline. */ list_add(&s->list, &cp->cline->list); list_del_init(&cp->cline->list); if (!list_empty(&cp->cline->update)) { list_add(&s->update, &cp->cline->update); list_del_init(&cp->cline->update); } cp->cline->len = 0; } /* * Write a string to the 3270 console */ static void con3270_write(struct console *co, const char *str, unsigned int count) { struct con3270 *cp; unsigned long flags; unsigned char c; cp = condev; spin_lock_irqsave(&cp->view.lock, flags); while (count-- > 0) { c = *str++; if (cp->cline->len == 0) con3270_cline_add(cp); if (c != '\n') con3270_cline_insert(cp, c); if (c == '\n' || cp->cline->len >= cp->view.cols) con3270_cline_end(cp); } /* Setup timer to output current console buffer after 1/10 second */ cp->nr_up = 0; if (cp->view.dev && !timer_pending(&cp->timer)) con3270_set_timer(cp, HZ/10); spin_unlock_irqrestore(&cp->view.lock,flags); } static struct tty_driver * con3270_device(struct console *c, int *index) { *index = c->index; return tty3270_driver; } /* * Wait for end of write request. */ static void con3270_wait_write(struct con3270 *cp) { while (!cp->write) { raw3270_wait_cons_dev(cp->view.dev); barrier(); } } /* * The below function is called as a panic/reboot notifier before the * system enters a disabled, endless loop. * * Notice we must use the spin_trylock() alternative, to prevent lockups * in atomic context (panic routine runs with secondary CPUs, local IRQs * and preemption disabled). */ static int con3270_notify(struct notifier_block *self, unsigned long event, void *data) { struct con3270 *cp; unsigned long flags; cp = condev; if (!cp->view.dev) return NOTIFY_DONE; if (!raw3270_view_lock_unavailable(&cp->view)) raw3270_activate_view(&cp->view); if (!spin_trylock_irqsave(&cp->view.lock, flags)) return NOTIFY_DONE; con3270_wait_write(cp); cp->nr_up = 0; con3270_rebuild_update(cp); con3270_update_status(cp); while (cp->update_flags != 0) { spin_unlock_irqrestore(&cp->view.lock, flags); con3270_update(&cp->timer); spin_lock_irqsave(&cp->view.lock, flags); con3270_wait_write(cp); } spin_unlock_irqrestore(&cp->view.lock, flags); return NOTIFY_DONE; } static struct notifier_block on_panic_nb = { .notifier_call = con3270_notify, .priority = INT_MIN + 1, /* run the callback late */ }; static struct notifier_block on_reboot_nb = { .notifier_call = con3270_notify, .priority = INT_MIN + 1, /* run the callback late */ }; /* * The console structure for the 3270 console */ static struct console con3270 = { .name = "tty3270", .write = con3270_write, .device = con3270_device, .flags = CON_PRINTBUFFER, }; /* * 3270 console initialization code called from console_init(). */ static int __init con3270_init(void) { struct raw3270 *rp; void *cbuf; int i; /* Check if 3270 is to be the console */ if (!CONSOLE_IS_3270) return -ENODEV; /* Set the console mode for VM */ if (MACHINE_IS_VM) { cpcmd("TERM CONMODE 3270", NULL, 0, NULL); cpcmd("TERM AUTOCR OFF", NULL, 0, NULL); } rp = raw3270_setup_console(); if (IS_ERR(rp)) return PTR_ERR(rp); condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA); if (!condev) return -ENOMEM; condev->view.dev = rp; condev->read = raw3270_request_alloc(0); condev->read->callback = con3270_read_callback; condev->read->callback_data = condev; condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE); condev->kreset = raw3270_request_alloc(1); INIT_LIST_HEAD(&condev->lines); INIT_LIST_HEAD(&condev->update); timer_setup(&condev->timer, con3270_update, 0); tasklet_init(&condev->readlet, con3270_read_tasklet, (unsigned long) condev->read); raw3270_add_view(&condev->view, &con3270_fn, 1, RAW3270_VIEW_LOCK_IRQ); INIT_LIST_HEAD(&condev->freemem); for (i = 0; i < CON3270_STRING_PAGES; i++) { cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); add_string_memory(&condev->freemem, cbuf, PAGE_SIZE); } condev->cline = alloc_string(&condev->freemem, condev->view.cols); condev->cline->len = 0; con3270_create_status(condev); condev->input = alloc_string(&condev->freemem, 80); atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb); register_reboot_notifier(&on_reboot_nb); register_console(&con3270); return 0; } console_initcall(con3270_init); |