GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / s390 / char / con3270.c
blobbb07577e8fd4409331aeef22562edec3c838a468
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 spinlock_t lock;
39 struct list_head freemem; /* list of free memory for strings. */
41 /* Output stuff. */
42 struct list_head lines; /* list of lines. */
43 struct list_head update; /* list of lines to update. */
44 int line_nr; /* line number for next update. */
45 int nr_lines; /* # lines in list. */
46 int nr_up; /* # lines up in history. */
47 unsigned long update_flags; /* Update indication bits. */
48 struct string *cline; /* current output line. */
49 struct string *status; /* last line of display. */
50 struct raw3270_request *write; /* single write request. */
51 struct timer_list timer;
53 /* Input stuff. */
54 struct string *input; /* input string for read request. */
55 struct raw3270_request *read; /* single read request. */
56 struct raw3270_request *kreset; /* single keyboard reset request. */
57 struct tasklet_struct readlet; /* tasklet to issue read request. */
60 static struct con3270 *condev;
62 /* con3270->update_flags. See con3270_update for details. */
63 #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
64 #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */
65 #define CON_UPDATE_STATUS 4 /* Update status line. */
66 #define CON_UPDATE_ALL 8 /* Recreate screen. */
68 static void con3270_update(struct con3270 *);
71 * Setup timeout for a device. On timeout trigger an update.
73 static void con3270_set_timer(struct con3270 *cp, int expires)
75 if (expires == 0)
76 del_timer(&cp->timer);
77 else
78 mod_timer(&cp->timer, jiffies + expires);
82 * The status line is the last line of the screen. It shows the string
83 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
84 * in the lower right corner of the screen.
86 static void
87 con3270_update_status(struct con3270 *cp)
89 char *str;
91 str = (cp->nr_up != 0) ? "History" : "Running";
92 memcpy(cp->status->string + 24, str, 7);
93 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
94 cp->update_flags |= CON_UPDATE_STATUS;
97 static void
98 con3270_create_status(struct con3270 *cp)
100 static const unsigned char blueprint[] =
101 { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
102 'c','o','n','s','o','l','e',' ','v','i','e','w',
103 TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
105 cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
106 /* Copy blueprint to status line */
107 memcpy(cp->status->string, blueprint, sizeof(blueprint));
108 /* Set TO_RA addresses. */
109 raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
110 cp->view.cols * (cp->view.rows - 1));
111 raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
112 cp->view.cols * cp->view.rows - 8);
113 /* Convert strings to ebcdic. */
114 codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
115 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
119 * Set output offsets to 3270 datastream fragment of a console string.
121 static void
122 con3270_update_string(struct con3270 *cp, struct string *s, int nr)
124 if (s->len >= cp->view.cols - 5)
125 return;
126 raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
127 cp->view.cols * (nr + 1));
131 * Rebuild update list to print all lines.
133 static void
134 con3270_rebuild_update(struct con3270 *cp)
136 struct string *s, *n;
137 int nr;
140 * Throw away update list and create a new one,
141 * containing all lines that will fit on the screen.
143 list_for_each_entry_safe(s, n, &cp->update, update)
144 list_del_init(&s->update);
145 nr = cp->view.rows - 2 + cp->nr_up;
146 list_for_each_entry_reverse(s, &cp->lines, list) {
147 if (nr < cp->view.rows - 1)
148 list_add(&s->update, &cp->update);
149 if (--nr < 0)
150 break;
152 cp->line_nr = 0;
153 cp->update_flags |= CON_UPDATE_LIST;
157 * Alloc string for size bytes. Free strings from history if necessary.
159 static struct string *
160 con3270_alloc_string(struct con3270 *cp, size_t size)
162 struct string *s, *n;
164 s = alloc_string(&cp->freemem, size);
165 if (s)
166 return s;
167 list_for_each_entry_safe(s, n, &cp->lines, list) {
168 list_del(&s->list);
169 if (!list_empty(&s->update))
170 list_del(&s->update);
171 cp->nr_lines--;
172 if (free_string(&cp->freemem, s) >= size)
173 break;
175 s = alloc_string(&cp->freemem, size);
176 BUG_ON(!s);
177 if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
178 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
179 con3270_rebuild_update(cp);
180 con3270_update_status(cp);
182 return s;
186 * Write completion callback.
188 static void
189 con3270_write_callback(struct raw3270_request *rq, void *data)
191 raw3270_request_reset(rq);
192 xchg(&((struct con3270 *) rq->view)->write, rq);
196 * Update console display.
198 static void
199 con3270_update(struct con3270 *cp)
201 struct raw3270_request *wrq;
202 char wcc, prolog[6];
203 unsigned long flags;
204 unsigned long updated;
205 struct string *s, *n;
206 int rc;
208 if (cp->view.dev)
209 raw3270_activate_view(&cp->view);
211 wrq = xchg(&cp->write, 0);
212 if (!wrq) {
213 con3270_set_timer(cp, 1);
214 return;
217 spin_lock_irqsave(&cp->view.lock, flags);
218 updated = 0;
219 if (cp->update_flags & CON_UPDATE_ALL) {
220 con3270_rebuild_update(cp);
221 con3270_update_status(cp);
222 cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
223 CON_UPDATE_STATUS;
225 if (cp->update_flags & CON_UPDATE_ERASE) {
226 /* Use erase write alternate to initialize display. */
227 raw3270_request_set_cmd(wrq, TC_EWRITEA);
228 updated |= CON_UPDATE_ERASE;
229 } else
230 raw3270_request_set_cmd(wrq, TC_WRITE);
232 wcc = TW_NONE;
233 raw3270_request_add_data(wrq, &wcc, 1);
236 * Update status line.
238 if (cp->update_flags & CON_UPDATE_STATUS)
239 if (raw3270_request_add_data(wrq, cp->status->string,
240 cp->status->len) == 0)
241 updated |= CON_UPDATE_STATUS;
243 if (cp->update_flags & CON_UPDATE_LIST) {
244 prolog[0] = TO_SBA;
245 prolog[3] = TO_SA;
246 prolog[4] = TAT_COLOR;
247 prolog[5] = TAC_TURQ;
248 raw3270_buffer_address(cp->view.dev, prolog + 1,
249 cp->view.cols * cp->line_nr);
250 raw3270_request_add_data(wrq, prolog, 6);
251 /* Write strings in the update list to the screen. */
252 list_for_each_entry_safe(s, n, &cp->update, update) {
253 if (s != cp->cline)
254 con3270_update_string(cp, s, cp->line_nr);
255 if (raw3270_request_add_data(wrq, s->string,
256 s->len) != 0)
257 break;
258 list_del_init(&s->update);
259 if (s != cp->cline)
260 cp->line_nr++;
262 if (list_empty(&cp->update))
263 updated |= CON_UPDATE_LIST;
265 wrq->callback = con3270_write_callback;
266 rc = raw3270_start(&cp->view, wrq);
267 if (rc == 0) {
268 cp->update_flags &= ~updated;
269 if (cp->update_flags)
270 con3270_set_timer(cp, 1);
271 } else {
272 raw3270_request_reset(wrq);
273 xchg(&cp->write, wrq);
275 spin_unlock_irqrestore(&cp->view.lock, flags);
279 * Read tasklet.
281 static void
282 con3270_read_tasklet(struct raw3270_request *rrq)
284 static char kreset_data = TW_KR;
285 struct con3270 *cp;
286 unsigned long flags;
287 int nr_up, deactivate;
289 cp = (struct con3270 *) rrq->view;
290 spin_lock_irqsave(&cp->view.lock, flags);
291 nr_up = cp->nr_up;
292 deactivate = 0;
293 /* Check aid byte. */
294 switch (cp->input->string[0]) {
295 case 0x7d: /* enter: jump to bottom. */
296 nr_up = 0;
297 break;
298 case 0xf3: /* PF3: deactivate the console view. */
299 deactivate = 1;
300 break;
301 case 0x6d: /* clear: start from scratch. */
302 cp->update_flags = CON_UPDATE_ALL;
303 con3270_set_timer(cp, 1);
304 break;
305 case 0xf7: /* PF7: do a page up in the console log. */
306 nr_up += cp->view.rows - 2;
307 if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
308 nr_up = cp->nr_lines - cp->view.rows + 1;
309 if (nr_up < 0)
310 nr_up = 0;
312 break;
313 case 0xf8: /* PF8: do a page down in the console log. */
314 nr_up -= cp->view.rows - 2;
315 if (nr_up < 0)
316 nr_up = 0;
317 break;
319 if (nr_up != cp->nr_up) {
320 cp->nr_up = nr_up;
321 con3270_rebuild_update(cp);
322 con3270_update_status(cp);
323 con3270_set_timer(cp, 1);
325 spin_unlock_irqrestore(&cp->view.lock, flags);
327 /* Start keyboard reset command. */
328 raw3270_request_reset(cp->kreset);
329 raw3270_request_set_cmd(cp->kreset, TC_WRITE);
330 raw3270_request_add_data(cp->kreset, &kreset_data, 1);
331 raw3270_start(&cp->view, cp->kreset);
333 if (deactivate)
334 raw3270_deactivate_view(&cp->view);
336 raw3270_request_reset(rrq);
337 xchg(&cp->read, rrq);
338 raw3270_put_view(&cp->view);
342 * Read request completion callback.
344 static void
345 con3270_read_callback(struct raw3270_request *rq, void *data)
347 raw3270_get_view(rq->view);
348 /* Schedule tasklet to pass input to tty. */
349 tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
353 * Issue a read request. Called only from interrupt function.
355 static void
356 con3270_issue_read(struct con3270 *cp)
358 struct raw3270_request *rrq;
359 int rc;
361 rrq = xchg(&cp->read, 0);
362 if (!rrq)
363 /* Read already scheduled. */
364 return;
365 rrq->callback = con3270_read_callback;
366 rrq->callback_data = cp;
367 raw3270_request_set_cmd(rrq, TC_READMOD);
368 raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
369 /* Issue the read modified request. */
370 rc = raw3270_start_irq(&cp->view, rrq);
371 if (rc)
372 raw3270_request_reset(rrq);
376 * Switch to the console view.
378 static int
379 con3270_activate(struct raw3270_view *view)
381 struct con3270 *cp;
383 cp = (struct con3270 *) view;
384 cp->update_flags = CON_UPDATE_ALL;
385 con3270_set_timer(cp, 1);
386 return 0;
389 static void
390 con3270_deactivate(struct raw3270_view *view)
392 struct con3270 *cp;
394 cp = (struct con3270 *) view;
395 del_timer(&cp->timer);
398 static int
399 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
401 /* Handle ATTN. Schedule tasklet to read aid. */
402 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
403 con3270_issue_read(cp);
405 if (rq) {
406 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
407 rq->rc = -EIO;
408 else
409 /* Normal end. Copy residual count. */
410 rq->rescnt = irb->scsw.cmd.count;
412 return RAW3270_IO_DONE;
415 /* Console view to a 3270 device. */
416 static struct raw3270_fn con3270_fn = {
417 .activate = con3270_activate,
418 .deactivate = con3270_deactivate,
419 .intv = (void *) con3270_irq
422 static inline void
423 con3270_cline_add(struct con3270 *cp)
425 if (!list_empty(&cp->cline->list))
426 /* Already added. */
427 return;
428 list_add_tail(&cp->cline->list, &cp->lines);
429 cp->nr_lines++;
430 con3270_rebuild_update(cp);
433 static inline void
434 con3270_cline_insert(struct con3270 *cp, unsigned char c)
436 cp->cline->string[cp->cline->len++] =
437 cp->view.ascebc[(c < ' ') ? ' ' : c];
438 if (list_empty(&cp->cline->update)) {
439 list_add_tail(&cp->cline->update, &cp->update);
440 cp->update_flags |= CON_UPDATE_LIST;
444 static inline void
445 con3270_cline_end(struct con3270 *cp)
447 struct string *s;
448 unsigned int size;
450 /* Copy cline. */
451 size = (cp->cline->len < cp->view.cols - 5) ?
452 cp->cline->len + 4 : cp->view.cols;
453 s = con3270_alloc_string(cp, size);
454 memcpy(s->string, cp->cline->string, cp->cline->len);
455 if (s->len < cp->view.cols - 5) {
456 s->string[s->len - 4] = TO_RA;
457 s->string[s->len - 1] = 0;
458 } else {
459 while (--size > cp->cline->len)
460 s->string[size] = cp->view.ascebc[' '];
462 /* Replace cline with allocated line s and reset cline. */
463 list_add(&s->list, &cp->cline->list);
464 list_del_init(&cp->cline->list);
465 if (!list_empty(&cp->cline->update)) {
466 list_add(&s->update, &cp->cline->update);
467 list_del_init(&cp->cline->update);
469 cp->cline->len = 0;
473 * Write a string to the 3270 console
475 static void
476 con3270_write(struct console *co, const char *str, unsigned int count)
478 struct con3270 *cp;
479 unsigned long flags;
480 unsigned char c;
482 cp = condev;
483 spin_lock_irqsave(&cp->view.lock, flags);
484 while (count-- > 0) {
485 c = *str++;
486 if (cp->cline->len == 0)
487 con3270_cline_add(cp);
488 if (c != '\n')
489 con3270_cline_insert(cp, c);
490 if (c == '\n' || cp->cline->len >= cp->view.cols)
491 con3270_cline_end(cp);
493 /* Setup timer to output current console buffer after 1/10 second */
494 cp->nr_up = 0;
495 if (cp->view.dev && !timer_pending(&cp->timer))
496 con3270_set_timer(cp, HZ/10);
497 spin_unlock_irqrestore(&cp->view.lock,flags);
500 static struct tty_driver *
501 con3270_device(struct console *c, int *index)
503 *index = c->index;
504 return tty3270_driver;
508 * Wait for end of write request.
510 static void
511 con3270_wait_write(struct con3270 *cp)
513 while (!cp->write) {
514 raw3270_wait_cons_dev(cp->view.dev);
515 barrier();
520 * panic() calls con3270_flush through a panic_notifier
521 * before the system enters a disabled, endless loop.
523 static void
524 con3270_flush(void)
526 struct con3270 *cp;
527 unsigned long flags;
529 cp = condev;
530 if (!cp->view.dev)
531 return;
532 raw3270_pm_unfreeze(&cp->view);
533 spin_lock_irqsave(&cp->view.lock, flags);
534 con3270_wait_write(cp);
535 cp->nr_up = 0;
536 con3270_rebuild_update(cp);
537 con3270_update_status(cp);
538 while (cp->update_flags != 0) {
539 spin_unlock_irqrestore(&cp->view.lock, flags);
540 con3270_update(cp);
541 spin_lock_irqsave(&cp->view.lock, flags);
542 con3270_wait_write(cp);
544 spin_unlock_irqrestore(&cp->view.lock, flags);
547 static int con3270_notify(struct notifier_block *self,
548 unsigned long event, void *data)
550 con3270_flush();
551 return NOTIFY_OK;
554 static struct notifier_block on_panic_nb = {
555 .notifier_call = con3270_notify,
556 .priority = 0,
559 static struct notifier_block on_reboot_nb = {
560 .notifier_call = con3270_notify,
561 .priority = 0,
565 * The console structure for the 3270 console
567 static struct console con3270 = {
568 .name = "tty3270",
569 .write = con3270_write,
570 .device = con3270_device,
571 .flags = CON_PRINTBUFFER,
575 * 3270 console initialization code called from console_init().
577 static int __init
578 con3270_init(void)
580 struct ccw_device *cdev;
581 struct raw3270 *rp;
582 void *cbuf;
583 int i;
585 /* Check if 3270 is to be the console */
586 if (!CONSOLE_IS_3270)
587 return -ENODEV;
589 /* Set the console mode for VM */
590 if (MACHINE_IS_VM) {
591 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
592 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
595 cdev = ccw_device_probe_console();
596 if (IS_ERR(cdev))
597 return -ENODEV;
598 rp = raw3270_setup_console(cdev);
599 if (IS_ERR(rp))
600 return PTR_ERR(rp);
602 condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
603 condev->view.dev = rp;
605 condev->read = raw3270_request_alloc(0);
606 condev->read->callback = con3270_read_callback;
607 condev->read->callback_data = condev;
608 condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
609 condev->kreset = raw3270_request_alloc(1);
611 INIT_LIST_HEAD(&condev->lines);
612 INIT_LIST_HEAD(&condev->update);
613 setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
614 (unsigned long) condev);
615 tasklet_init(&condev->readlet,
616 (void (*)(unsigned long)) con3270_read_tasklet,
617 (unsigned long) condev->read);
619 raw3270_add_view(&condev->view, &con3270_fn, 1);
621 INIT_LIST_HEAD(&condev->freemem);
622 for (i = 0; i < CON3270_STRING_PAGES; i++) {
623 cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
624 add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
626 condev->cline = alloc_string(&condev->freemem, condev->view.cols);
627 condev->cline->len = 0;
628 con3270_create_status(condev);
629 condev->input = alloc_string(&condev->freemem, 80);
630 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
631 register_reboot_notifier(&on_reboot_nb);
632 register_console(&con3270);
633 return 0;
636 console_initcall(con3270_init);