2 * drivers/s390/char/con3270.c
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 (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
11 #include <linux/config.h>
12 #include <linux/bootmem.h>
13 #include <linux/console.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/list.h>
17 #include <linux/types.h>
19 #include <asm/ccwdev.h>
21 #include <asm/cpcmd.h>
22 #include <asm/ebcdic.h>
27 #define CON3270_OUTPUT_BUFFER_SIZE 1024
28 #define CON3270_STRING_PAGES 4
30 static struct raw3270_fn con3270_fn;
33 * Main 3270 console view data structure.
36 struct raw3270_view view;
38 struct list_head freemem; /* list of free memory for strings. */
41 struct list_head lines; /* list of lines. */
42 struct list_head update; /* list of lines to update. */
43 int line_nr; /* line number for next update. */
44 int nr_lines; /* # lines in list. */
45 int nr_up; /* # lines up in history. */
46 unsigned long update_flags; /* Update indication bits. */
47 struct string *cline; /* current output line. */
48 struct string *status; /* last line of display. */
49 struct raw3270_request *write; /* single write request. */
50 struct timer_list timer;
53 struct string *input; /* input string for read request. */
54 struct raw3270_request *read; /* single read request. */
55 struct raw3270_request *kreset; /* single keyboard reset request. */
56 struct tasklet_struct readlet; /* tasklet to issue read request. */
59 static struct con3270 *condev;
61 /* con3270->update_flags. See con3270_update for details. */
62 #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
63 #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */
64 #define CON_UPDATE_STATUS 4 /* Update status line. */
65 #define CON_UPDATE_ALL 7
67 static void con3270_update(struct con3270 *);
70 * Setup timeout for a device. On timeout trigger an update.
73 con3270_set_timer(struct con3270 *cp, int expires)
76 del_timer(&cp->timer);
79 if (mod_timer(&cp->timer, jiffies + expires))
81 cp->timer.function = (void (*)(unsigned long)) con3270_update;
82 cp->timer.data = (unsigned long) cp;
83 cp->timer.expires = jiffies + expires;
84 add_timer(&cp->timer);
88 * The status line is the last line of the screen. It shows the string
89 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
90 * in the lower right corner of the screen.
93 con3270_update_status(struct con3270 *cp)
97 str = (cp->nr_up != 0) ? "History" : "Running";
98 memcpy(cp->status->string + 24, str, 7);
99 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
100 cp->update_flags |= CON_UPDATE_STATUS;
104 con3270_create_status(struct con3270 *cp)
106 static const unsigned char blueprint[] =
107 { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
108 'c','o','n','s','o','l','e',' ','v','i','e','w',
109 TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
111 cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
112 /* Copy blueprint to status line */
113 memcpy(cp->status->string, blueprint, sizeof(blueprint));
114 /* Set TO_RA addresses. */
115 raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
116 cp->view.cols * (cp->view.rows - 1));
117 raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
118 cp->view.cols * cp->view.rows - 8);
119 /* Convert strings to ebcdic. */
120 codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
121 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
125 * Set output offsets to 3270 datastream fragment of a console string.
128 con3270_update_string(struct con3270 *cp, struct string *s, int nr)
130 if (s->len >= cp->view.cols - 5)
132 raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
133 cp->view.cols * (nr + 1));
137 * Rebuild update list to print all lines.
140 con3270_rebuild_update(struct con3270 *cp)
142 struct string *s, *n;
146 * Throw away update list and create a new one,
147 * containing all lines that will fit on the screen.
149 list_for_each_entry_safe(s, n, &cp->update, update)
150 list_del_init(&s->update);
151 nr = cp->view.rows - 2 + cp->nr_up;
152 list_for_each_entry_reverse(s, &cp->lines, list) {
153 if (nr < cp->view.rows - 1)
154 list_add(&s->update, &cp->update);
159 cp->update_flags |= CON_UPDATE_LIST;
163 * Alloc string for size bytes. Free strings from history if necessary.
165 static struct string *
166 con3270_alloc_string(struct con3270 *cp, size_t size)
168 struct string *s, *n;
170 s = alloc_string(&cp->freemem, size);
173 list_for_each_entry_safe(s, n, &cp->lines, list) {
175 if (!list_empty(&s->update))
176 list_del(&s->update);
178 if (free_string(&cp->freemem, s) >= size)
181 s = alloc_string(&cp->freemem, size);
183 if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
184 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
185 con3270_rebuild_update(cp);
186 con3270_update_status(cp);
192 * Write completion callback.
195 con3270_write_callback(struct raw3270_request *rq, void *data)
197 raw3270_request_reset(rq);
198 xchg(&((struct con3270 *) rq->view)->write, rq);
202 * Update console display.
205 con3270_update(struct con3270 *cp)
207 struct raw3270_request *wrq;
210 unsigned long updated;
211 struct string *s, *n;
214 wrq = xchg(&cp->write, 0);
216 con3270_set_timer(cp, 1);
220 spin_lock_irqsave(&cp->view.lock, flags);
222 if (cp->update_flags & CON_UPDATE_ERASE) {
223 /* Use erase write alternate to initialize display. */
224 raw3270_request_set_cmd(wrq, TC_EWRITEA);
225 updated |= CON_UPDATE_ERASE;
227 raw3270_request_set_cmd(wrq, TC_WRITE);
230 raw3270_request_add_data(wrq, &wcc, 1);
233 * Update status line.
235 if (cp->update_flags & CON_UPDATE_STATUS)
236 if (raw3270_request_add_data(wrq, cp->status->string,
237 cp->status->len) == 0)
238 updated |= CON_UPDATE_STATUS;
240 if (cp->update_flags & CON_UPDATE_LIST) {
243 prolog[4] = TAT_COLOR;
244 prolog[5] = TAC_TURQ;
245 raw3270_buffer_address(cp->view.dev, prolog + 1,
246 cp->view.cols * cp->line_nr);
247 raw3270_request_add_data(wrq, prolog, 6);
248 /* Write strings in the update list to the screen. */
249 list_for_each_entry_safe(s, n, &cp->update, update) {
251 con3270_update_string(cp, s, cp->line_nr);
252 if (raw3270_request_add_data(wrq, s->string,
255 list_del_init(&s->update);
259 if (list_empty(&cp->update))
260 updated |= CON_UPDATE_LIST;
262 wrq->callback = con3270_write_callback;
263 rc = raw3270_start(&cp->view, wrq);
265 cp->update_flags &= ~updated;
266 if (cp->update_flags)
267 con3270_set_timer(cp, 1);
269 raw3270_request_reset(wrq);
270 xchg(&cp->write, wrq);
272 spin_unlock_irqrestore(&cp->view.lock, flags);
279 con3270_read_tasklet(struct raw3270_request *rrq)
281 static char kreset_data = TW_KR;
284 int nr_up, deactivate;
286 cp = (struct con3270 *) rrq->view;
287 spin_lock_irqsave(&cp->view.lock, flags);
290 /* Check aid byte. */
291 switch (cp->input->string[0]) {
292 case 0x7d: /* enter: jump to bottom. */
295 case 0xf3: /* PF3: deactivate the console view. */
298 case 0x6d: /* clear: start from scratch. */
299 con3270_rebuild_update(cp);
300 cp->update_flags = CON_UPDATE_ALL;
301 con3270_set_timer(cp, 1);
303 case 0xf7: /* PF7: do a page up in the console log. */
304 nr_up += cp->view.rows - 2;
305 if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
306 nr_up = cp->nr_lines - cp->view.rows + 1;
311 case 0xf8: /* PF8: do a page down in the console log. */
312 nr_up -= cp->view.rows - 2;
317 if (nr_up != cp->nr_up) {
319 con3270_rebuild_update(cp);
320 con3270_update_status(cp);
321 con3270_set_timer(cp, 1);
323 spin_unlock_irqrestore(&cp->view.lock, flags);
325 /* Start keyboard reset command. */
326 raw3270_request_reset(cp->kreset);
327 raw3270_request_set_cmd(cp->kreset, TC_WRITE);
328 raw3270_request_add_data(cp->kreset, &kreset_data, 1);
329 raw3270_start(&cp->view, cp->kreset);
332 raw3270_deactivate_view(&cp->view);
334 raw3270_request_reset(rrq);
335 xchg(&cp->read, rrq);
336 raw3270_put_view(&cp->view);
340 * Read request completion callback.
343 con3270_read_callback(struct raw3270_request *rq, void *data)
345 raw3270_get_view(rq->view);
346 /* Schedule tasklet to pass input to tty. */
347 tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
351 * Issue a read request. Called only from interrupt function.
354 con3270_issue_read(struct con3270 *cp)
356 struct raw3270_request *rrq;
359 rrq = xchg(&cp->read, 0);
361 /* Read already scheduled. */
363 rrq->callback = con3270_read_callback;
364 rrq->callback_data = cp;
365 raw3270_request_set_cmd(rrq, TC_READMOD);
366 raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
367 /* Issue the read modified request. */
368 rc = raw3270_start_irq(&cp->view, rrq);
370 raw3270_request_reset(rrq);
374 * Switch to the console view.
377 con3270_activate(struct raw3270_view *view)
382 cp = (struct con3270 *) view;
383 spin_lock_irqsave(&cp->view.lock, flags);
385 con3270_rebuild_update(cp);
386 con3270_update_status(cp);
387 cp->update_flags = CON_UPDATE_ALL;
388 con3270_set_timer(cp, 1);
389 spin_unlock_irqrestore(&cp->view.lock, flags);
394 con3270_deactivate(struct raw3270_view *view)
399 cp = (struct con3270 *) view;
400 spin_lock_irqsave(&cp->view.lock, flags);
401 del_timer(&cp->timer);
402 spin_unlock_irqrestore(&cp->view.lock, flags);
406 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
408 /* Handle ATTN. Schedule tasklet to read aid. */
409 if (irb->scsw.dstat & DEV_STAT_ATTENTION)
410 con3270_issue_read(cp);
413 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
416 /* Normal end. Copy residual count. */
417 rq->rescnt = irb->scsw.count;
419 return RAW3270_IO_DONE;
422 /* Console view to a 3270 device. */
423 static struct raw3270_fn con3270_fn = {
424 .activate = con3270_activate,
425 .deactivate = con3270_deactivate,
426 .intv = (void *) con3270_irq
430 con3270_cline_add(struct con3270 *cp)
432 if (!list_empty(&cp->cline->list))
435 list_add_tail(&cp->cline->list, &cp->lines);
437 con3270_rebuild_update(cp);
441 con3270_cline_insert(struct con3270 *cp, unsigned char c)
443 cp->cline->string[cp->cline->len++] =
444 cp->view.ascebc[(c < ' ') ? ' ' : c];
445 if (list_empty(&cp->cline->update)) {
446 list_add_tail(&cp->cline->update, &cp->update);
447 cp->update_flags |= CON_UPDATE_LIST;
452 con3270_cline_end(struct con3270 *cp)
458 size = (cp->cline->len < cp->view.cols - 5) ?
459 cp->cline->len + 4 : cp->view.cols;
460 s = con3270_alloc_string(cp, size);
461 memcpy(s->string, cp->cline->string, cp->cline->len);
462 if (s->len < cp->view.cols - 5) {
463 s->string[s->len - 4] = TO_RA;
464 s->string[s->len - 1] = 0;
466 while (--size > cp->cline->len)
467 s->string[size] = cp->view.ascebc[' '];
469 /* Replace cline with allocated line s and reset cline. */
470 list_add(&s->list, &cp->cline->list);
471 list_del_init(&cp->cline->list);
472 if (!list_empty(&cp->cline->update)) {
473 list_add(&s->update, &cp->cline->update);
474 list_del_init(&cp->cline->update);
480 * Write a string to the 3270 console
483 con3270_write(struct console *co, const char *str, unsigned int count)
491 raw3270_activate_view(&cp->view);
492 spin_lock_irqsave(&cp->view.lock, flags);
493 while (count-- > 0) {
495 if (cp->cline->len == 0)
496 con3270_cline_add(cp);
498 con3270_cline_insert(cp, c);
499 if (c == '\n' || cp->cline->len >= cp->view.cols)
500 con3270_cline_end(cp);
502 /* Setup timer to output current console buffer after 1/10 second */
503 if (cp->view.dev && !timer_pending(&cp->timer))
504 con3270_set_timer(cp, HZ/10);
505 spin_unlock_irqrestore(&cp->view.lock,flags);
508 extern struct tty_driver *tty3270_driver;
510 static struct tty_driver *
511 con3270_device(struct console *c, int *index)
514 return tty3270_driver;
518 * Wait for end of write request.
521 con3270_wait_write(struct con3270 *cp)
524 raw3270_wait_cons_dev(cp->view.dev);
530 * panic() calls console_unblank before the system enters a
531 * disabled, endless loop.
534 con3270_unblank(void)
542 spin_lock_irqsave(&cp->view.lock, flags);
543 con3270_wait_write(cp);
545 con3270_rebuild_update(cp);
546 con3270_update_status(cp);
547 while (cp->update_flags != 0) {
548 spin_unlock_irqrestore(&cp->view.lock, flags);
550 spin_lock_irqsave(&cp->view.lock, flags);
551 con3270_wait_write(cp);
553 spin_unlock_irqrestore(&cp->view.lock, flags);
557 con3270_consetup(struct console *co, char *options)
563 * The console structure for the 3270 console
565 static struct console con3270 = {
567 .write = con3270_write,
568 .device = con3270_device,
569 .unblank = con3270_unblank,
570 .setup = con3270_consetup,
571 .flags = CON_PRINTBUFFER,
575 * 3270 console initialization code called from console_init().
576 * NOTE: This is called before kmalloc is available.
581 struct ccw_device *cdev;
586 /* Check if 3270 is to be the console */
587 if (!CONSOLE_IS_3270)
590 /* Set the console mode for VM */
592 cpcmd("TERM CONMODE 3270", 0, 0);
593 cpcmd("TERM AUTOCR OFF", 0, 0);
596 cdev = ccw_device_probe_console();
599 rp = raw3270_setup_console(cdev);
603 condev = (struct con3270 *) alloc_bootmem_low(sizeof(struct con3270));
604 memset(condev, 0, sizeof(struct con3270));
605 condev->view.dev = rp;
607 condev->read = raw3270_request_alloc_bootmem(0);
608 condev->read->callback = con3270_read_callback;
609 condev->read->callback_data = condev;
611 raw3270_request_alloc_bootmem(CON3270_OUTPUT_BUFFER_SIZE);
612 condev->kreset = raw3270_request_alloc_bootmem(1);
614 INIT_LIST_HEAD(&condev->lines);
615 INIT_LIST_HEAD(&condev->update);
616 init_timer(&condev->timer);
617 tasklet_init(&condev->readlet,
618 (void (*)(unsigned long)) con3270_read_tasklet,
619 (unsigned long) condev->read);
621 raw3270_add_view(&condev->view, &con3270_fn, 0);
623 INIT_LIST_HEAD(&condev->freemem);
624 for (i = 0; i < CON3270_STRING_PAGES; i++) {
625 cbuf = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
626 add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
628 condev->cline = alloc_string(&condev->freemem, condev->view.cols);
629 condev->cline->len = 0;
630 con3270_create_status(condev);
631 condev->input = alloc_string(&condev->freemem, 80);
632 register_console(&con3270);
636 console_initcall(con3270_init);