x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / s390 / char / con3270.c
blobbb6b0df50b33618ae337f623038d042f96a2430a
1 /*
2 * IBM/3270 Driver - console view.
4 * Author(s):
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
6 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Copyright IBM Corp. 2003, 2009
8 */
10 #include <linux/console.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/list.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/reboot.h>
19 #include <asm/ccwdev.h>
20 #include <asm/cio.h>
21 #include <asm/cpcmd.h>
22 #include <asm/ebcdic.h>
24 #include "raw3270.h"
25 #include "tty3270.h"
26 #include "ctrlchar.h"
28 #define CON3270_OUTPUT_BUFFER_SIZE 1024
29 #define CON3270_STRING_PAGES 4
31 static struct raw3270_fn con3270_fn;
34 * Main 3270 console view data structure.
36 struct con3270 {
37 struct raw3270_view view;
38 struct list_head freemem; /* list of free memory for strings. */
40 /* Output stuff. */
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;
52 /* Input stuff. */
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 8 /* Recreate screen. */
67 static void con3270_update(struct con3270 *);
70 * Setup timeout for a device. On timeout trigger an update.
72 static void con3270_set_timer(struct con3270 *cp, int expires)
74 if (expires == 0)
75 del_timer(&cp->timer);
76 else
77 mod_timer(&cp->timer, jiffies + expires);
81 * The status line is the last line of the screen. It shows the string
82 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
83 * in the lower right corner of the screen.
85 static void
86 con3270_update_status(struct con3270 *cp)
88 char *str;
90 str = (cp->nr_up != 0) ? "History" : "Running";
91 memcpy(cp->status->string + 24, str, 7);
92 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
93 cp->update_flags |= CON_UPDATE_STATUS;
96 static void
97 con3270_create_status(struct con3270 *cp)
99 static const unsigned char blueprint[] =
100 { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
101 'c','o','n','s','o','l','e',' ','v','i','e','w',
102 TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
104 cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
105 /* Copy blueprint to status line */
106 memcpy(cp->status->string, blueprint, sizeof(blueprint));
107 /* Set TO_RA addresses. */
108 raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
109 cp->view.cols * (cp->view.rows - 1));
110 raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
111 cp->view.cols * cp->view.rows - 8);
112 /* Convert strings to ebcdic. */
113 codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
114 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
118 * Set output offsets to 3270 datastream fragment of a console string.
120 static void
121 con3270_update_string(struct con3270 *cp, struct string *s, int nr)
123 if (s->len >= cp->view.cols - 5)
124 return;
125 raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
126 cp->view.cols * (nr + 1));
130 * Rebuild update list to print all lines.
132 static void
133 con3270_rebuild_update(struct con3270 *cp)
135 struct string *s, *n;
136 int nr;
139 * Throw away update list and create a new one,
140 * containing all lines that will fit on the screen.
142 list_for_each_entry_safe(s, n, &cp->update, update)
143 list_del_init(&s->update);
144 nr = cp->view.rows - 2 + cp->nr_up;
145 list_for_each_entry_reverse(s, &cp->lines, list) {
146 if (nr < cp->view.rows - 1)
147 list_add(&s->update, &cp->update);
148 if (--nr < 0)
149 break;
151 cp->line_nr = 0;
152 cp->update_flags |= CON_UPDATE_LIST;
156 * Alloc string for size bytes. Free strings from history if necessary.
158 static struct string *
159 con3270_alloc_string(struct con3270 *cp, size_t size)
161 struct string *s, *n;
163 s = alloc_string(&cp->freemem, size);
164 if (s)
165 return s;
166 list_for_each_entry_safe(s, n, &cp->lines, list) {
167 list_del(&s->list);
168 if (!list_empty(&s->update))
169 list_del(&s->update);
170 cp->nr_lines--;
171 if (free_string(&cp->freemem, s) >= size)
172 break;
174 s = alloc_string(&cp->freemem, size);
175 BUG_ON(!s);
176 if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
177 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
178 con3270_rebuild_update(cp);
179 con3270_update_status(cp);
181 return s;
185 * Write completion callback.
187 static void
188 con3270_write_callback(struct raw3270_request *rq, void *data)
190 raw3270_request_reset(rq);
191 xchg(&((struct con3270 *) rq->view)->write, rq);
195 * Update console display.
197 static void
198 con3270_update(struct con3270 *cp)
200 struct raw3270_request *wrq;
201 char wcc, prolog[6];
202 unsigned long flags;
203 unsigned long updated;
204 struct string *s, *n;
205 int rc;
207 if (cp->view.dev)
208 raw3270_activate_view(&cp->view);
210 wrq = xchg(&cp->write, 0);
211 if (!wrq) {
212 con3270_set_timer(cp, 1);
213 return;
216 spin_lock_irqsave(&cp->view.lock, flags);
217 updated = 0;
218 if (cp->update_flags & CON_UPDATE_ALL) {
219 con3270_rebuild_update(cp);
220 con3270_update_status(cp);
221 cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
222 CON_UPDATE_STATUS;
224 if (cp->update_flags & CON_UPDATE_ERASE) {
225 /* Use erase write alternate to initialize display. */
226 raw3270_request_set_cmd(wrq, TC_EWRITEA);
227 updated |= CON_UPDATE_ERASE;
228 } else
229 raw3270_request_set_cmd(wrq, TC_WRITE);
231 wcc = TW_NONE;
232 raw3270_request_add_data(wrq, &wcc, 1);
235 * Update status line.
237 if (cp->update_flags & CON_UPDATE_STATUS)
238 if (raw3270_request_add_data(wrq, cp->status->string,
239 cp->status->len) == 0)
240 updated |= CON_UPDATE_STATUS;
242 if (cp->update_flags & CON_UPDATE_LIST) {
243 prolog[0] = TO_SBA;
244 prolog[3] = TO_SA;
245 prolog[4] = TAT_COLOR;
246 prolog[5] = TAC_TURQ;
247 raw3270_buffer_address(cp->view.dev, prolog + 1,
248 cp->view.cols * cp->line_nr);
249 raw3270_request_add_data(wrq, prolog, 6);
250 /* Write strings in the update list to the screen. */
251 list_for_each_entry_safe(s, n, &cp->update, update) {
252 if (s != cp->cline)
253 con3270_update_string(cp, s, cp->line_nr);
254 if (raw3270_request_add_data(wrq, s->string,
255 s->len) != 0)
256 break;
257 list_del_init(&s->update);
258 if (s != cp->cline)
259 cp->line_nr++;
261 if (list_empty(&cp->update))
262 updated |= CON_UPDATE_LIST;
264 wrq->callback = con3270_write_callback;
265 rc = raw3270_start(&cp->view, wrq);
266 if (rc == 0) {
267 cp->update_flags &= ~updated;
268 if (cp->update_flags)
269 con3270_set_timer(cp, 1);
270 } else {
271 raw3270_request_reset(wrq);
272 xchg(&cp->write, wrq);
274 spin_unlock_irqrestore(&cp->view.lock, flags);
278 * Read tasklet.
280 static void
281 con3270_read_tasklet(struct raw3270_request *rrq)
283 static char kreset_data = TW_KR;
284 struct con3270 *cp;
285 unsigned long flags;
286 int nr_up, deactivate;
288 cp = (struct con3270 *) rrq->view;
289 spin_lock_irqsave(&cp->view.lock, flags);
290 nr_up = cp->nr_up;
291 deactivate = 0;
292 /* Check aid byte. */
293 switch (cp->input->string[0]) {
294 case 0x7d: /* enter: jump to bottom. */
295 nr_up = 0;
296 break;
297 case 0xf3: /* PF3: deactivate the console view. */
298 deactivate = 1;
299 break;
300 case 0x6d: /* clear: start from scratch. */
301 cp->update_flags = CON_UPDATE_ALL;
302 con3270_set_timer(cp, 1);
303 break;
304 case 0xf7: /* PF7: do a page up in the console log. */
305 nr_up += cp->view.rows - 2;
306 if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
307 nr_up = cp->nr_lines - cp->view.rows + 1;
308 if (nr_up < 0)
309 nr_up = 0;
311 break;
312 case 0xf8: /* PF8: do a page down in the console log. */
313 nr_up -= cp->view.rows - 2;
314 if (nr_up < 0)
315 nr_up = 0;
316 break;
318 if (nr_up != cp->nr_up) {
319 cp->nr_up = nr_up;
320 con3270_rebuild_update(cp);
321 con3270_update_status(cp);
322 con3270_set_timer(cp, 1);
324 spin_unlock_irqrestore(&cp->view.lock, flags);
326 /* Start keyboard reset command. */
327 raw3270_request_reset(cp->kreset);
328 raw3270_request_set_cmd(cp->kreset, TC_WRITE);
329 raw3270_request_add_data(cp->kreset, &kreset_data, 1);
330 raw3270_start(&cp->view, cp->kreset);
332 if (deactivate)
333 raw3270_deactivate_view(&cp->view);
335 raw3270_request_reset(rrq);
336 xchg(&cp->read, rrq);
337 raw3270_put_view(&cp->view);
341 * Read request completion callback.
343 static void
344 con3270_read_callback(struct raw3270_request *rq, void *data)
346 raw3270_get_view(rq->view);
347 /* Schedule tasklet to pass input to tty. */
348 tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
352 * Issue a read request. Called only from interrupt function.
354 static void
355 con3270_issue_read(struct con3270 *cp)
357 struct raw3270_request *rrq;
358 int rc;
360 rrq = xchg(&cp->read, 0);
361 if (!rrq)
362 /* Read already scheduled. */
363 return;
364 rrq->callback = con3270_read_callback;
365 rrq->callback_data = cp;
366 raw3270_request_set_cmd(rrq, TC_READMOD);
367 raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
368 /* Issue the read modified request. */
369 rc = raw3270_start_irq(&cp->view, rrq);
370 if (rc)
371 raw3270_request_reset(rrq);
375 * Switch to the console view.
377 static int
378 con3270_activate(struct raw3270_view *view)
380 struct con3270 *cp;
382 cp = (struct con3270 *) view;
383 cp->update_flags = CON_UPDATE_ALL;
384 con3270_set_timer(cp, 1);
385 return 0;
388 static void
389 con3270_deactivate(struct raw3270_view *view)
391 struct con3270 *cp;
393 cp = (struct con3270 *) view;
394 del_timer(&cp->timer);
397 static int
398 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
400 /* Handle ATTN. Schedule tasklet to read aid. */
401 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
402 con3270_issue_read(cp);
404 if (rq) {
405 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
406 rq->rc = -EIO;
407 else
408 /* Normal end. Copy residual count. */
409 rq->rescnt = irb->scsw.cmd.count;
411 return RAW3270_IO_DONE;
414 /* Console view to a 3270 device. */
415 static struct raw3270_fn con3270_fn = {
416 .activate = con3270_activate,
417 .deactivate = con3270_deactivate,
418 .intv = (void *) con3270_irq
421 static inline void
422 con3270_cline_add(struct con3270 *cp)
424 if (!list_empty(&cp->cline->list))
425 /* Already added. */
426 return;
427 list_add_tail(&cp->cline->list, &cp->lines);
428 cp->nr_lines++;
429 con3270_rebuild_update(cp);
432 static inline void
433 con3270_cline_insert(struct con3270 *cp, unsigned char c)
435 cp->cline->string[cp->cline->len++] =
436 cp->view.ascebc[(c < ' ') ? ' ' : c];
437 if (list_empty(&cp->cline->update)) {
438 list_add_tail(&cp->cline->update, &cp->update);
439 cp->update_flags |= CON_UPDATE_LIST;
443 static inline void
444 con3270_cline_end(struct con3270 *cp)
446 struct string *s;
447 unsigned int size;
449 /* Copy cline. */
450 size = (cp->cline->len < cp->view.cols - 5) ?
451 cp->cline->len + 4 : cp->view.cols;
452 s = con3270_alloc_string(cp, size);
453 memcpy(s->string, cp->cline->string, cp->cline->len);
454 if (s->len < cp->view.cols - 5) {
455 s->string[s->len - 4] = TO_RA;
456 s->string[s->len - 1] = 0;
457 } else {
458 while (--size > cp->cline->len)
459 s->string[size] = cp->view.ascebc[' '];
461 /* Replace cline with allocated line s and reset cline. */
462 list_add(&s->list, &cp->cline->list);
463 list_del_init(&cp->cline->list);
464 if (!list_empty(&cp->cline->update)) {
465 list_add(&s->update, &cp->cline->update);
466 list_del_init(&cp->cline->update);
468 cp->cline->len = 0;
472 * Write a string to the 3270 console
474 static void
475 con3270_write(struct console *co, const char *str, unsigned int count)
477 struct con3270 *cp;
478 unsigned long flags;
479 unsigned char c;
481 cp = condev;
482 spin_lock_irqsave(&cp->view.lock, flags);
483 while (count-- > 0) {
484 c = *str++;
485 if (cp->cline->len == 0)
486 con3270_cline_add(cp);
487 if (c != '\n')
488 con3270_cline_insert(cp, c);
489 if (c == '\n' || cp->cline->len >= cp->view.cols)
490 con3270_cline_end(cp);
492 /* Setup timer to output current console buffer after 1/10 second */
493 cp->nr_up = 0;
494 if (cp->view.dev && !timer_pending(&cp->timer))
495 con3270_set_timer(cp, HZ/10);
496 spin_unlock_irqrestore(&cp->view.lock,flags);
499 static struct tty_driver *
500 con3270_device(struct console *c, int *index)
502 *index = c->index;
503 return tty3270_driver;
507 * Wait for end of write request.
509 static void
510 con3270_wait_write(struct con3270 *cp)
512 while (!cp->write) {
513 raw3270_wait_cons_dev(cp->view.dev);
514 barrier();
519 * panic() calls con3270_flush through a panic_notifier
520 * before the system enters a disabled, endless loop.
522 static void
523 con3270_flush(void)
525 struct con3270 *cp;
526 unsigned long flags;
528 cp = condev;
529 if (!cp->view.dev)
530 return;
531 raw3270_pm_unfreeze(&cp->view);
532 spin_lock_irqsave(&cp->view.lock, flags);
533 con3270_wait_write(cp);
534 cp->nr_up = 0;
535 con3270_rebuild_update(cp);
536 con3270_update_status(cp);
537 while (cp->update_flags != 0) {
538 spin_unlock_irqrestore(&cp->view.lock, flags);
539 con3270_update(cp);
540 spin_lock_irqsave(&cp->view.lock, flags);
541 con3270_wait_write(cp);
543 spin_unlock_irqrestore(&cp->view.lock, flags);
546 static int con3270_notify(struct notifier_block *self,
547 unsigned long event, void *data)
549 con3270_flush();
550 return NOTIFY_OK;
553 static struct notifier_block on_panic_nb = {
554 .notifier_call = con3270_notify,
555 .priority = 0,
558 static struct notifier_block on_reboot_nb = {
559 .notifier_call = con3270_notify,
560 .priority = 0,
564 * The console structure for the 3270 console
566 static struct console con3270 = {
567 .name = "tty3270",
568 .write = con3270_write,
569 .device = con3270_device,
570 .flags = CON_PRINTBUFFER,
574 * 3270 console initialization code called from console_init().
576 static int __init
577 con3270_init(void)
579 struct raw3270 *rp;
580 void *cbuf;
581 int i;
583 /* Check if 3270 is to be the console */
584 if (!CONSOLE_IS_3270)
585 return -ENODEV;
587 /* Set the console mode for VM */
588 if (MACHINE_IS_VM) {
589 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
590 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
593 rp = raw3270_setup_console();
594 if (IS_ERR(rp))
595 return PTR_ERR(rp);
597 condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
598 condev->view.dev = rp;
600 condev->read = raw3270_request_alloc(0);
601 condev->read->callback = con3270_read_callback;
602 condev->read->callback_data = condev;
603 condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
604 condev->kreset = raw3270_request_alloc(1);
606 INIT_LIST_HEAD(&condev->lines);
607 INIT_LIST_HEAD(&condev->update);
608 setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
609 (unsigned long) condev);
610 tasklet_init(&condev->readlet,
611 (void (*)(unsigned long)) con3270_read_tasklet,
612 (unsigned long) condev->read);
614 raw3270_add_view(&condev->view, &con3270_fn, 1);
616 INIT_LIST_HEAD(&condev->freemem);
617 for (i = 0; i < CON3270_STRING_PAGES; i++) {
618 cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
619 add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
621 condev->cline = alloc_string(&condev->freemem, condev->view.cols);
622 condev->cline->len = 0;
623 con3270_create_status(condev);
624 condev->input = alloc_string(&condev->freemem, 80);
625 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
626 register_reboot_notifier(&on_reboot_nb);
627 register_console(&con3270);
628 return 0;
631 console_initcall(con3270_init);