usb: xhci-plat: properly handle probe deferral for devm_clk_get()
[linux/fpc-iii.git] / drivers / s390 / char / con3270.c
blob4d7a9badfedee7acebe7289d0d02a3d9d8997ca5
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/module.h>
11 #include <linux/console.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/reboot.h>
20 #include <asm/ccwdev.h>
21 #include <asm/cio.h>
22 #include <asm/cpcmd.h>
23 #include <asm/ebcdic.h>
25 #include "raw3270.h"
26 #include "tty3270.h"
27 #include "ctrlchar.h"
29 #define CON3270_OUTPUT_BUFFER_SIZE 1024
30 #define CON3270_STRING_PAGES 4
32 static struct raw3270_fn con3270_fn;
34 static bool auto_update = 1;
35 module_param(auto_update, bool, 0);
38 * Main 3270 console view data structure.
40 struct con3270 {
41 struct raw3270_view view;
42 struct list_head freemem; /* list of free memory for strings. */
44 /* Output stuff. */
45 struct list_head lines; /* list of lines. */
46 struct list_head update; /* list of lines to update. */
47 int line_nr; /* line number for next update. */
48 int nr_lines; /* # lines in list. */
49 int nr_up; /* # lines up in history. */
50 unsigned long update_flags; /* Update indication bits. */
51 struct string *cline; /* current output line. */
52 struct string *status; /* last line of display. */
53 struct raw3270_request *write; /* single write request. */
54 struct timer_list timer;
56 /* Input stuff. */
57 struct string *input; /* input string for read request. */
58 struct raw3270_request *read; /* single read request. */
59 struct raw3270_request *kreset; /* single keyboard reset request. */
60 struct tasklet_struct readlet; /* tasklet to issue read request. */
63 static struct con3270 *condev;
65 /* con3270->update_flags. See con3270_update for details. */
66 #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
67 #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */
68 #define CON_UPDATE_STATUS 4 /* Update status line. */
69 #define CON_UPDATE_ALL 8 /* Recreate screen. */
71 static void con3270_update(struct con3270 *);
74 * Setup timeout for a device. On timeout trigger an update.
76 static void con3270_set_timer(struct con3270 *cp, int expires)
78 if (expires == 0)
79 del_timer(&cp->timer);
80 else
81 mod_timer(&cp->timer, jiffies + expires);
85 * The status line is the last line of the screen. It shows the string
86 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
87 * in the lower right corner of the screen.
89 static void
90 con3270_update_status(struct con3270 *cp)
92 char *str;
94 str = (cp->nr_up != 0) ? "History" : "Running";
95 memcpy(cp->status->string + 24, str, 7);
96 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
97 cp->update_flags |= CON_UPDATE_STATUS;
100 static void
101 con3270_create_status(struct con3270 *cp)
103 static const unsigned char blueprint[] =
104 { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
105 'c','o','n','s','o','l','e',' ','v','i','e','w',
106 TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
108 cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
109 /* Copy blueprint to status line */
110 memcpy(cp->status->string, blueprint, sizeof(blueprint));
111 /* Set TO_RA addresses. */
112 raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
113 cp->view.cols * (cp->view.rows - 1));
114 raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
115 cp->view.cols * cp->view.rows - 8);
116 /* Convert strings to ebcdic. */
117 codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
118 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
122 * Set output offsets to 3270 datastream fragment of a console string.
124 static void
125 con3270_update_string(struct con3270 *cp, struct string *s, int nr)
127 if (s->len >= cp->view.cols - 5)
128 return;
129 raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
130 cp->view.cols * (nr + 1));
134 * Rebuild update list to print all lines.
136 static void
137 con3270_rebuild_update(struct con3270 *cp)
139 struct string *s, *n;
140 int nr;
143 * Throw away update list and create a new one,
144 * containing all lines that will fit on the screen.
146 list_for_each_entry_safe(s, n, &cp->update, update)
147 list_del_init(&s->update);
148 nr = cp->view.rows - 2 + cp->nr_up;
149 list_for_each_entry_reverse(s, &cp->lines, list) {
150 if (nr < cp->view.rows - 1)
151 list_add(&s->update, &cp->update);
152 if (--nr < 0)
153 break;
155 cp->line_nr = 0;
156 cp->update_flags |= CON_UPDATE_LIST;
160 * Alloc string for size bytes. Free strings from history if necessary.
162 static struct string *
163 con3270_alloc_string(struct con3270 *cp, size_t size)
165 struct string *s, *n;
167 s = alloc_string(&cp->freemem, size);
168 if (s)
169 return s;
170 list_for_each_entry_safe(s, n, &cp->lines, list) {
171 list_del(&s->list);
172 if (!list_empty(&s->update))
173 list_del(&s->update);
174 cp->nr_lines--;
175 if (free_string(&cp->freemem, s) >= size)
176 break;
178 s = alloc_string(&cp->freemem, size);
179 BUG_ON(!s);
180 if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
181 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
182 con3270_rebuild_update(cp);
183 con3270_update_status(cp);
185 return s;
189 * Write completion callback.
191 static void
192 con3270_write_callback(struct raw3270_request *rq, void *data)
194 raw3270_request_reset(rq);
195 xchg(&((struct con3270 *) rq->view)->write, rq);
199 * Update console display.
201 static void
202 con3270_update(struct con3270 *cp)
204 struct raw3270_request *wrq;
205 char wcc, prolog[6];
206 unsigned long flags;
207 unsigned long updated;
208 struct string *s, *n;
209 int rc;
211 if (!auto_update && !raw3270_view_active(&cp->view))
212 return;
213 if (cp->view.dev)
214 raw3270_activate_view(&cp->view);
216 wrq = xchg(&cp->write, 0);
217 if (!wrq) {
218 con3270_set_timer(cp, 1);
219 return;
222 spin_lock_irqsave(&cp->view.lock, flags);
223 updated = 0;
224 if (cp->update_flags & CON_UPDATE_ALL) {
225 con3270_rebuild_update(cp);
226 con3270_update_status(cp);
227 cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
228 CON_UPDATE_STATUS;
230 if (cp->update_flags & CON_UPDATE_ERASE) {
231 /* Use erase write alternate to initialize display. */
232 raw3270_request_set_cmd(wrq, TC_EWRITEA);
233 updated |= CON_UPDATE_ERASE;
234 } else
235 raw3270_request_set_cmd(wrq, TC_WRITE);
237 wcc = TW_NONE;
238 raw3270_request_add_data(wrq, &wcc, 1);
241 * Update status line.
243 if (cp->update_flags & CON_UPDATE_STATUS)
244 if (raw3270_request_add_data(wrq, cp->status->string,
245 cp->status->len) == 0)
246 updated |= CON_UPDATE_STATUS;
248 if (cp->update_flags & CON_UPDATE_LIST) {
249 prolog[0] = TO_SBA;
250 prolog[3] = TO_SA;
251 prolog[4] = TAT_COLOR;
252 prolog[5] = TAC_TURQ;
253 raw3270_buffer_address(cp->view.dev, prolog + 1,
254 cp->view.cols * cp->line_nr);
255 raw3270_request_add_data(wrq, prolog, 6);
256 /* Write strings in the update list to the screen. */
257 list_for_each_entry_safe(s, n, &cp->update, update) {
258 if (s != cp->cline)
259 con3270_update_string(cp, s, cp->line_nr);
260 if (raw3270_request_add_data(wrq, s->string,
261 s->len) != 0)
262 break;
263 list_del_init(&s->update);
264 if (s != cp->cline)
265 cp->line_nr++;
267 if (list_empty(&cp->update))
268 updated |= CON_UPDATE_LIST;
270 wrq->callback = con3270_write_callback;
271 rc = raw3270_start(&cp->view, wrq);
272 if (rc == 0) {
273 cp->update_flags &= ~updated;
274 if (cp->update_flags)
275 con3270_set_timer(cp, 1);
276 } else {
277 raw3270_request_reset(wrq);
278 xchg(&cp->write, wrq);
280 spin_unlock_irqrestore(&cp->view.lock, flags);
284 * Read tasklet.
286 static void
287 con3270_read_tasklet(struct raw3270_request *rrq)
289 static char kreset_data = TW_KR;
290 struct con3270 *cp;
291 unsigned long flags;
292 int nr_up, deactivate;
294 cp = (struct con3270 *) rrq->view;
295 spin_lock_irqsave(&cp->view.lock, flags);
296 nr_up = cp->nr_up;
297 deactivate = 0;
298 /* Check aid byte. */
299 switch (cp->input->string[0]) {
300 case 0x7d: /* enter: jump to bottom. */
301 nr_up = 0;
302 break;
303 case 0xf3: /* PF3: deactivate the console view. */
304 deactivate = 1;
305 break;
306 case 0x6d: /* clear: start from scratch. */
307 cp->update_flags = CON_UPDATE_ALL;
308 con3270_set_timer(cp, 1);
309 break;
310 case 0xf7: /* PF7: do a page up in the console log. */
311 nr_up += cp->view.rows - 2;
312 if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
313 nr_up = cp->nr_lines - cp->view.rows + 1;
314 if (nr_up < 0)
315 nr_up = 0;
317 break;
318 case 0xf8: /* PF8: do a page down in the console log. */
319 nr_up -= cp->view.rows - 2;
320 if (nr_up < 0)
321 nr_up = 0;
322 break;
324 if (nr_up != cp->nr_up) {
325 cp->nr_up = nr_up;
326 con3270_rebuild_update(cp);
327 con3270_update_status(cp);
328 con3270_set_timer(cp, 1);
330 spin_unlock_irqrestore(&cp->view.lock, flags);
332 /* Start keyboard reset command. */
333 raw3270_request_reset(cp->kreset);
334 raw3270_request_set_cmd(cp->kreset, TC_WRITE);
335 raw3270_request_add_data(cp->kreset, &kreset_data, 1);
336 raw3270_start(&cp->view, cp->kreset);
338 if (deactivate)
339 raw3270_deactivate_view(&cp->view);
341 raw3270_request_reset(rrq);
342 xchg(&cp->read, rrq);
343 raw3270_put_view(&cp->view);
347 * Read request completion callback.
349 static void
350 con3270_read_callback(struct raw3270_request *rq, void *data)
352 raw3270_get_view(rq->view);
353 /* Schedule tasklet to pass input to tty. */
354 tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
358 * Issue a read request. Called only from interrupt function.
360 static void
361 con3270_issue_read(struct con3270 *cp)
363 struct raw3270_request *rrq;
364 int rc;
366 rrq = xchg(&cp->read, 0);
367 if (!rrq)
368 /* Read already scheduled. */
369 return;
370 rrq->callback = con3270_read_callback;
371 rrq->callback_data = cp;
372 raw3270_request_set_cmd(rrq, TC_READMOD);
373 raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
374 /* Issue the read modified request. */
375 rc = raw3270_start_irq(&cp->view, rrq);
376 if (rc)
377 raw3270_request_reset(rrq);
381 * Switch to the console view.
383 static int
384 con3270_activate(struct raw3270_view *view)
386 struct con3270 *cp;
388 cp = (struct con3270 *) view;
389 cp->update_flags = CON_UPDATE_ALL;
390 con3270_set_timer(cp, 1);
391 return 0;
394 static void
395 con3270_deactivate(struct raw3270_view *view)
397 struct con3270 *cp;
399 cp = (struct con3270 *) view;
400 del_timer(&cp->timer);
403 static int
404 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
406 /* Handle ATTN. Schedule tasklet to read aid. */
407 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
408 con3270_issue_read(cp);
410 if (rq) {
411 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
412 rq->rc = -EIO;
413 else
414 /* Normal end. Copy residual count. */
415 rq->rescnt = irb->scsw.cmd.count;
416 } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
417 /* Interrupt without an outstanding request -> update all */
418 cp->update_flags = CON_UPDATE_ALL;
419 con3270_set_timer(cp, 1);
421 return RAW3270_IO_DONE;
424 /* Console view to a 3270 device. */
425 static struct raw3270_fn con3270_fn = {
426 .activate = con3270_activate,
427 .deactivate = con3270_deactivate,
428 .intv = (void *) con3270_irq
431 static inline void
432 con3270_cline_add(struct con3270 *cp)
434 if (!list_empty(&cp->cline->list))
435 /* Already added. */
436 return;
437 list_add_tail(&cp->cline->list, &cp->lines);
438 cp->nr_lines++;
439 con3270_rebuild_update(cp);
442 static inline void
443 con3270_cline_insert(struct con3270 *cp, unsigned char c)
445 cp->cline->string[cp->cline->len++] =
446 cp->view.ascebc[(c < ' ') ? ' ' : c];
447 if (list_empty(&cp->cline->update)) {
448 list_add_tail(&cp->cline->update, &cp->update);
449 cp->update_flags |= CON_UPDATE_LIST;
453 static inline void
454 con3270_cline_end(struct con3270 *cp)
456 struct string *s;
457 unsigned int size;
459 /* Copy cline. */
460 size = (cp->cline->len < cp->view.cols - 5) ?
461 cp->cline->len + 4 : cp->view.cols;
462 s = con3270_alloc_string(cp, size);
463 memcpy(s->string, cp->cline->string, cp->cline->len);
464 if (s->len < cp->view.cols - 5) {
465 s->string[s->len - 4] = TO_RA;
466 s->string[s->len - 1] = 0;
467 } else {
468 while (--size > cp->cline->len)
469 s->string[size] = cp->view.ascebc[' '];
471 /* Replace cline with allocated line s and reset cline. */
472 list_add(&s->list, &cp->cline->list);
473 list_del_init(&cp->cline->list);
474 if (!list_empty(&cp->cline->update)) {
475 list_add(&s->update, &cp->cline->update);
476 list_del_init(&cp->cline->update);
478 cp->cline->len = 0;
482 * Write a string to the 3270 console
484 static void
485 con3270_write(struct console *co, const char *str, unsigned int count)
487 struct con3270 *cp;
488 unsigned long flags;
489 unsigned char c;
491 cp = condev;
492 spin_lock_irqsave(&cp->view.lock, flags);
493 while (count-- > 0) {
494 c = *str++;
495 if (cp->cline->len == 0)
496 con3270_cline_add(cp);
497 if (c != '\n')
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 cp->nr_up = 0;
504 if (cp->view.dev && !timer_pending(&cp->timer))
505 con3270_set_timer(cp, HZ/10);
506 spin_unlock_irqrestore(&cp->view.lock,flags);
509 static struct tty_driver *
510 con3270_device(struct console *c, int *index)
512 *index = c->index;
513 return tty3270_driver;
517 * Wait for end of write request.
519 static void
520 con3270_wait_write(struct con3270 *cp)
522 while (!cp->write) {
523 raw3270_wait_cons_dev(cp->view.dev);
524 barrier();
529 * panic() calls con3270_flush through a panic_notifier
530 * before the system enters a disabled, endless loop.
532 static void
533 con3270_flush(void)
535 struct con3270 *cp;
536 unsigned long flags;
538 cp = condev;
539 if (!cp->view.dev)
540 return;
541 raw3270_pm_unfreeze(&cp->view);
542 raw3270_activate_view(&cp->view);
543 spin_lock_irqsave(&cp->view.lock, flags);
544 con3270_wait_write(cp);
545 cp->nr_up = 0;
546 con3270_rebuild_update(cp);
547 con3270_update_status(cp);
548 while (cp->update_flags != 0) {
549 spin_unlock_irqrestore(&cp->view.lock, flags);
550 con3270_update(cp);
551 spin_lock_irqsave(&cp->view.lock, flags);
552 con3270_wait_write(cp);
554 spin_unlock_irqrestore(&cp->view.lock, flags);
557 static int con3270_notify(struct notifier_block *self,
558 unsigned long event, void *data)
560 con3270_flush();
561 return NOTIFY_OK;
564 static struct notifier_block on_panic_nb = {
565 .notifier_call = con3270_notify,
566 .priority = 0,
569 static struct notifier_block on_reboot_nb = {
570 .notifier_call = con3270_notify,
571 .priority = 0,
575 * The console structure for the 3270 console
577 static struct console con3270 = {
578 .name = "tty3270",
579 .write = con3270_write,
580 .device = con3270_device,
581 .flags = CON_PRINTBUFFER,
585 * 3270 console initialization code called from console_init().
587 static int __init
588 con3270_init(void)
590 struct raw3270 *rp;
591 void *cbuf;
592 int i;
594 /* Check if 3270 is to be the console */
595 if (!CONSOLE_IS_3270)
596 return -ENODEV;
598 /* Set the console mode for VM */
599 if (MACHINE_IS_VM) {
600 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
601 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
604 rp = raw3270_setup_console();
605 if (IS_ERR(rp))
606 return PTR_ERR(rp);
608 condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
609 if (!condev)
610 return -ENOMEM;
611 condev->view.dev = rp;
613 condev->read = raw3270_request_alloc(0);
614 condev->read->callback = con3270_read_callback;
615 condev->read->callback_data = condev;
616 condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
617 condev->kreset = raw3270_request_alloc(1);
619 INIT_LIST_HEAD(&condev->lines);
620 INIT_LIST_HEAD(&condev->update);
621 setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
622 (unsigned long) condev);
623 tasklet_init(&condev->readlet,
624 (void (*)(unsigned long)) con3270_read_tasklet,
625 (unsigned long) condev->read);
627 raw3270_add_view(&condev->view, &con3270_fn, 1);
629 INIT_LIST_HEAD(&condev->freemem);
630 for (i = 0; i < CON3270_STRING_PAGES; i++) {
631 cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
632 add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
634 condev->cline = alloc_string(&condev->freemem, condev->view.cols);
635 condev->cline->len = 0;
636 con3270_create_status(condev);
637 condev->input = alloc_string(&condev->freemem, 80);
638 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
639 register_reboot_notifier(&on_reboot_nb);
640 register_console(&con3270);
641 return 0;
644 console_initcall(con3270_init);