1 // SPDX-License-Identifier: GPL-2.0
3 * IBM/3270 Driver - console view.
6 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
8 * Copyright IBM Corp. 2003, 2009
11 #include <linux/module.h>
12 #include <linux/console.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/panic_notifier.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/reboot.h>
22 #include <asm/ccwdev.h>
24 #include <asm/cpcmd.h>
25 #include <asm/ebcdic.h>
31 #define CON3270_OUTPUT_BUFFER_SIZE 1024
32 #define CON3270_STRING_PAGES 4
34 static struct raw3270_fn con3270_fn;
36 static bool auto_update = true;
37 module_param(auto_update, bool, 0);
40 * Main 3270 console view data structure.
43 struct raw3270_view view;
44 struct list_head freemem; /* list of free memory for strings. */
47 struct list_head lines; /* list of lines. */
48 struct list_head update; /* list of lines to update. */
49 int line_nr; /* line number for next update. */
50 int nr_lines; /* # lines in list. */
51 int nr_up; /* # lines up in history. */
52 unsigned long update_flags; /* Update indication bits. */
53 struct string *cline; /* current output line. */
54 struct string *status; /* last line of display. */
55 struct raw3270_request *write; /* single write request. */
56 struct timer_list timer;
59 struct string *input; /* input string for read request. */
60 struct raw3270_request *read; /* single read request. */
61 struct raw3270_request *kreset; /* single keyboard reset request. */
62 struct tasklet_struct readlet; /* tasklet to issue read request. */
65 static struct con3270 *condev;
67 /* con3270->update_flags. See con3270_update for details. */
68 #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
69 #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */
70 #define CON_UPDATE_STATUS 4 /* Update status line. */
71 #define CON_UPDATE_ALL 8 /* Recreate screen. */
73 static void con3270_update(struct timer_list *);
76 * Setup timeout for a device. On timeout trigger an update.
78 static void con3270_set_timer(struct con3270 *cp, int expires)
81 del_timer(&cp->timer);
83 mod_timer(&cp->timer, jiffies + expires);
87 * The status line is the last line of the screen. It shows the string
88 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
89 * in the lower right corner of the screen.
92 con3270_update_status(struct con3270 *cp)
96 str = (cp->nr_up != 0) ? "History" : "Running";
97 memcpy(cp->status->string + 24, str, 7);
98 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
99 cp->update_flags |= CON_UPDATE_STATUS;
103 con3270_create_status(struct con3270 *cp)
105 static const unsigned char blueprint[] =
106 { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
107 'c','o','n','s','o','l','e',' ','v','i','e','w',
108 TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
110 cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
111 /* Copy blueprint to status line */
112 memcpy(cp->status->string, blueprint, sizeof(blueprint));
113 /* Set TO_RA addresses. */
114 raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
115 cp->view.cols * (cp->view.rows - 1));
116 raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
117 cp->view.cols * cp->view.rows - 8);
118 /* Convert strings to ebcdic. */
119 codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
120 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
124 * Set output offsets to 3270 datastream fragment of a console string.
127 con3270_update_string(struct con3270 *cp, struct string *s, int nr)
130 /* This indicates a bug, but printing a warning would
131 * cause a deadlock. */
134 if (s->string[s->len - 4] != TO_RA)
136 raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
137 cp->view.cols * (nr + 1));
141 * Rebuild update list to print all lines.
144 con3270_rebuild_update(struct con3270 *cp)
146 struct string *s, *n;
150 * Throw away update list and create a new one,
151 * containing all lines that will fit on the screen.
153 list_for_each_entry_safe(s, n, &cp->update, update)
154 list_del_init(&s->update);
155 nr = cp->view.rows - 2 + cp->nr_up;
156 list_for_each_entry_reverse(s, &cp->lines, list) {
157 if (nr < cp->view.rows - 1)
158 list_add(&s->update, &cp->update);
163 cp->update_flags |= CON_UPDATE_LIST;
167 * Alloc string for size bytes. Free strings from history if necessary.
169 static struct string *
170 con3270_alloc_string(struct con3270 *cp, size_t size)
172 struct string *s, *n;
174 s = alloc_string(&cp->freemem, size);
177 list_for_each_entry_safe(s, n, &cp->lines, list) {
179 if (!list_empty(&s->update))
180 list_del(&s->update);
182 if (free_string(&cp->freemem, s) >= size)
185 s = alloc_string(&cp->freemem, size);
187 if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
188 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
189 con3270_rebuild_update(cp);
190 con3270_update_status(cp);
196 * Write completion callback.
199 con3270_write_callback(struct raw3270_request *rq, void *data)
201 raw3270_request_reset(rq);
202 xchg(&((struct con3270 *) rq->view)->write, rq);
206 * Update console display.
209 con3270_update(struct timer_list *t)
211 struct con3270 *cp = from_timer(cp, t, timer);
212 struct raw3270_request *wrq;
215 unsigned long updated;
216 struct string *s, *n;
219 if (!auto_update && !raw3270_view_active(&cp->view))
222 raw3270_activate_view(&cp->view);
224 wrq = xchg(&cp->write, 0);
226 con3270_set_timer(cp, 1);
230 spin_lock_irqsave(&cp->view.lock, flags);
232 if (cp->update_flags & CON_UPDATE_ALL) {
233 con3270_rebuild_update(cp);
234 con3270_update_status(cp);
235 cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
238 if (cp->update_flags & CON_UPDATE_ERASE) {
239 /* Use erase write alternate to initialize display. */
240 raw3270_request_set_cmd(wrq, TC_EWRITEA);
241 updated |= CON_UPDATE_ERASE;
243 raw3270_request_set_cmd(wrq, TC_WRITE);
246 raw3270_request_add_data(wrq, &wcc, 1);
249 * Update status line.
251 if (cp->update_flags & CON_UPDATE_STATUS)
252 if (raw3270_request_add_data(wrq, cp->status->string,
253 cp->status->len) == 0)
254 updated |= CON_UPDATE_STATUS;
256 if (cp->update_flags & CON_UPDATE_LIST) {
259 prolog[4] = TAT_COLOR;
260 prolog[5] = TAC_TURQ;
261 raw3270_buffer_address(cp->view.dev, prolog + 1,
262 cp->view.cols * cp->line_nr);
263 raw3270_request_add_data(wrq, prolog, 6);
264 /* Write strings in the update list to the screen. */
265 list_for_each_entry_safe(s, n, &cp->update, update) {
267 con3270_update_string(cp, s, cp->line_nr);
268 if (raw3270_request_add_data(wrq, s->string,
271 list_del_init(&s->update);
275 if (list_empty(&cp->update))
276 updated |= CON_UPDATE_LIST;
278 wrq->callback = con3270_write_callback;
279 rc = raw3270_start(&cp->view, wrq);
281 cp->update_flags &= ~updated;
282 if (cp->update_flags)
283 con3270_set_timer(cp, 1);
285 raw3270_request_reset(wrq);
286 xchg(&cp->write, wrq);
288 spin_unlock_irqrestore(&cp->view.lock, flags);
295 con3270_read_tasklet(unsigned long data)
297 static char kreset_data = TW_KR;
298 struct raw3270_request *rrq;
301 int nr_up, deactivate;
303 rrq = (struct raw3270_request *)data;
304 cp = (struct con3270 *) rrq->view;
305 spin_lock_irqsave(&cp->view.lock, flags);
308 /* Check aid byte. */
309 switch (cp->input->string[0]) {
310 case 0x7d: /* enter: jump to bottom. */
313 case 0xf3: /* PF3: deactivate the console view. */
316 case 0x6d: /* clear: start from scratch. */
317 cp->update_flags = CON_UPDATE_ALL;
318 con3270_set_timer(cp, 1);
320 case 0xf7: /* PF7: do a page up in the console log. */
321 nr_up += cp->view.rows - 2;
322 if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
323 nr_up = cp->nr_lines - cp->view.rows + 1;
328 case 0xf8: /* PF8: do a page down in the console log. */
329 nr_up -= cp->view.rows - 2;
334 if (nr_up != cp->nr_up) {
336 con3270_rebuild_update(cp);
337 con3270_update_status(cp);
338 con3270_set_timer(cp, 1);
340 spin_unlock_irqrestore(&cp->view.lock, flags);
342 /* Start keyboard reset command. */
343 raw3270_request_reset(cp->kreset);
344 raw3270_request_set_cmd(cp->kreset, TC_WRITE);
345 raw3270_request_add_data(cp->kreset, &kreset_data, 1);
346 raw3270_start(&cp->view, cp->kreset);
349 raw3270_deactivate_view(&cp->view);
351 raw3270_request_reset(rrq);
352 xchg(&cp->read, rrq);
353 raw3270_put_view(&cp->view);
357 * Read request completion callback.
360 con3270_read_callback(struct raw3270_request *rq, void *data)
362 raw3270_get_view(rq->view);
363 /* Schedule tasklet to pass input to tty. */
364 tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
368 * Issue a read request. Called only from interrupt function.
371 con3270_issue_read(struct con3270 *cp)
373 struct raw3270_request *rrq;
376 rrq = xchg(&cp->read, 0);
378 /* Read already scheduled. */
380 rrq->callback = con3270_read_callback;
381 rrq->callback_data = cp;
382 raw3270_request_set_cmd(rrq, TC_READMOD);
383 raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
384 /* Issue the read modified request. */
385 rc = raw3270_start_irq(&cp->view, rrq);
387 raw3270_request_reset(rrq);
391 * Switch to the console view.
394 con3270_activate(struct raw3270_view *view)
398 cp = (struct con3270 *) view;
399 cp->update_flags = CON_UPDATE_ALL;
400 con3270_set_timer(cp, 1);
405 con3270_deactivate(struct raw3270_view *view)
409 cp = (struct con3270 *) view;
410 del_timer(&cp->timer);
414 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
416 /* Handle ATTN. Schedule tasklet to read aid. */
417 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
418 con3270_issue_read(cp);
421 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
424 /* Normal end. Copy residual count. */
425 rq->rescnt = irb->scsw.cmd.count;
426 } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
427 /* Interrupt without an outstanding request -> update all */
428 cp->update_flags = CON_UPDATE_ALL;
429 con3270_set_timer(cp, 1);
433 /* Console view to a 3270 device. */
434 static struct raw3270_fn con3270_fn = {
435 .activate = con3270_activate,
436 .deactivate = con3270_deactivate,
437 .intv = (void *) con3270_irq
441 con3270_cline_add(struct con3270 *cp)
443 if (!list_empty(&cp->cline->list))
446 list_add_tail(&cp->cline->list, &cp->lines);
448 con3270_rebuild_update(cp);
452 con3270_cline_insert(struct con3270 *cp, unsigned char c)
454 cp->cline->string[cp->cline->len++] =
455 cp->view.ascebc[(c < ' ') ? ' ' : c];
456 if (list_empty(&cp->cline->update)) {
457 list_add_tail(&cp->cline->update, &cp->update);
458 cp->update_flags |= CON_UPDATE_LIST;
463 con3270_cline_end(struct con3270 *cp)
469 size = (cp->cline->len < cp->view.cols - 5) ?
470 cp->cline->len + 4 : cp->view.cols;
471 s = con3270_alloc_string(cp, size);
472 memcpy(s->string, cp->cline->string, cp->cline->len);
473 if (cp->cline->len < cp->view.cols - 5) {
474 s->string[s->len - 4] = TO_RA;
475 s->string[s->len - 1] = 0;
477 while (--size >= cp->cline->len)
478 s->string[size] = cp->view.ascebc[' '];
480 /* Replace cline with allocated line s and reset cline. */
481 list_add(&s->list, &cp->cline->list);
482 list_del_init(&cp->cline->list);
483 if (!list_empty(&cp->cline->update)) {
484 list_add(&s->update, &cp->cline->update);
485 list_del_init(&cp->cline->update);
491 * Write a string to the 3270 console
494 con3270_write(struct console *co, const char *str, unsigned int count)
501 spin_lock_irqsave(&cp->view.lock, flags);
502 while (count-- > 0) {
504 if (cp->cline->len == 0)
505 con3270_cline_add(cp);
507 con3270_cline_insert(cp, c);
508 if (c == '\n' || cp->cline->len >= cp->view.cols)
509 con3270_cline_end(cp);
511 /* Setup timer to output current console buffer after 1/10 second */
513 if (cp->view.dev && !timer_pending(&cp->timer))
514 con3270_set_timer(cp, HZ/10);
515 spin_unlock_irqrestore(&cp->view.lock,flags);
518 static struct tty_driver *
519 con3270_device(struct console *c, int *index)
522 return tty3270_driver;
526 * Wait for end of write request.
529 con3270_wait_write(struct con3270 *cp)
532 raw3270_wait_cons_dev(cp->view.dev);
538 * panic() calls con3270_flush through a panic_notifier
539 * before the system enters a disabled, endless loop.
550 raw3270_activate_view(&cp->view);
551 spin_lock_irqsave(&cp->view.lock, flags);
552 con3270_wait_write(cp);
554 con3270_rebuild_update(cp);
555 con3270_update_status(cp);
556 while (cp->update_flags != 0) {
557 spin_unlock_irqrestore(&cp->view.lock, flags);
558 con3270_update(&cp->timer);
559 spin_lock_irqsave(&cp->view.lock, flags);
560 con3270_wait_write(cp);
562 spin_unlock_irqrestore(&cp->view.lock, flags);
565 static int con3270_notify(struct notifier_block *self,
566 unsigned long event, void *data)
572 static struct notifier_block on_panic_nb = {
573 .notifier_call = con3270_notify,
577 static struct notifier_block on_reboot_nb = {
578 .notifier_call = con3270_notify,
583 * The console structure for the 3270 console
585 static struct console con3270 = {
587 .write = con3270_write,
588 .device = con3270_device,
589 .flags = CON_PRINTBUFFER,
593 * 3270 console initialization code called from console_init().
602 /* Check if 3270 is to be the console */
603 if (!CONSOLE_IS_3270)
606 /* Set the console mode for VM */
608 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
609 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
612 rp = raw3270_setup_console();
616 condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
619 condev->view.dev = rp;
621 condev->read = raw3270_request_alloc(0);
622 condev->read->callback = con3270_read_callback;
623 condev->read->callback_data = condev;
624 condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
625 condev->kreset = raw3270_request_alloc(1);
627 INIT_LIST_HEAD(&condev->lines);
628 INIT_LIST_HEAD(&condev->update);
629 timer_setup(&condev->timer, con3270_update, 0);
630 tasklet_init(&condev->readlet, con3270_read_tasklet,
631 (unsigned long) condev->read);
633 raw3270_add_view(&condev->view, &con3270_fn, 1, RAW3270_VIEW_LOCK_IRQ);
635 INIT_LIST_HEAD(&condev->freemem);
636 for (i = 0; i < CON3270_STRING_PAGES; i++) {
637 cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
638 add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
640 condev->cline = alloc_string(&condev->freemem, condev->view.cols);
641 condev->cline->len = 0;
642 con3270_create_status(condev);
643 condev->input = alloc_string(&condev->freemem, 80);
644 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
645 register_reboot_notifier(&on_reboot_nb);
646 register_console(&con3270);
650 console_initcall(con3270_init);