pandora: defconfig: update
[pandora-kernel.git] / drivers / s390 / char / con3270.c
1 /*
2  * IBM/3270 Driver - console view.
3  *
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  */
9
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>
18
19 #include <asm/ccwdev.h>
20 #include <asm/cio.h>
21 #include <asm/cpcmd.h>
22 #include <asm/ebcdic.h>
23
24 #include "raw3270.h"
25 #include "tty3270.h"
26 #include "ctrlchar.h"
27
28 #define CON3270_OUTPUT_BUFFER_SIZE 1024
29 #define CON3270_STRING_PAGES 4
30
31 static struct raw3270_fn con3270_fn;
32
33 /*
34  * Main 3270 console view data structure.
35  */
36 struct con3270 {
37         struct raw3270_view view;
38         spinlock_t lock;
39         struct list_head freemem;       /* list of free memory for strings. */
40
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;
52
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. */
58 };
59
60 static struct con3270 *condev;
61
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. */
67
68 static void con3270_update(struct con3270 *);
69
70 /*
71  * Setup timeout for a device. On timeout trigger an update.
72  */
73 static void con3270_set_timer(struct con3270 *cp, int expires)
74 {
75         if (expires == 0)
76                 del_timer(&cp->timer);
77         else
78                 mod_timer(&cp->timer, jiffies + expires);
79 }
80
81 /*
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.
85  */
86 static void
87 con3270_update_status(struct con3270 *cp)
88 {
89         char *str;
90
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;
95 }
96
97 static void
98 con3270_create_status(struct con3270 *cp)
99 {
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 };
104
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);
116 }
117
118 /*
119  * Set output offsets to 3270 datastream fragment of a console string.
120  */
121 static void
122 con3270_update_string(struct con3270 *cp, struct string *s, int nr)
123 {
124         if (s->len < 4) {
125                 /* This indicates a bug, but printing a warning would
126                  * cause a deadlock. */
127                 return;
128         }
129         if (s->string[s->len - 4] != TO_RA)
130                 return;
131         raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
132                                cp->view.cols * (nr + 1));
133 }
134
135 /*
136  * Rebuild update list to print all lines.
137  */
138 static void
139 con3270_rebuild_update(struct con3270 *cp)
140 {
141         struct string *s, *n;
142         int nr;
143
144         /* 
145          * Throw away update list and create a new one,
146          * containing all lines that will fit on the screen.
147          */
148         list_for_each_entry_safe(s, n, &cp->update, update)
149                 list_del_init(&s->update);
150         nr = cp->view.rows - 2 + cp->nr_up;
151         list_for_each_entry_reverse(s, &cp->lines, list) {
152                 if (nr < cp->view.rows - 1)
153                         list_add(&s->update, &cp->update);
154                 if (--nr < 0)
155                         break;
156         }
157         cp->line_nr = 0;
158         cp->update_flags |= CON_UPDATE_LIST;
159 }
160
161 /*
162  * Alloc string for size bytes. Free strings from history if necessary.
163  */
164 static struct string *
165 con3270_alloc_string(struct con3270 *cp, size_t size)
166 {
167         struct string *s, *n;
168
169         s = alloc_string(&cp->freemem, size);
170         if (s)
171                 return s;
172         list_for_each_entry_safe(s, n, &cp->lines, list) {
173                 list_del(&s->list);
174                 if (!list_empty(&s->update))
175                         list_del(&s->update);
176                 cp->nr_lines--;
177                 if (free_string(&cp->freemem, s) >= size)
178                         break;
179         }
180         s = alloc_string(&cp->freemem, size);
181         BUG_ON(!s);
182         if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
183                 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
184                 con3270_rebuild_update(cp);
185                 con3270_update_status(cp);
186         }
187         return s;
188 }
189
190 /*
191  * Write completion callback.
192  */
193 static void
194 con3270_write_callback(struct raw3270_request *rq, void *data)
195 {
196         raw3270_request_reset(rq);
197         xchg(&((struct con3270 *) rq->view)->write, rq);
198 }
199
200 /*
201  * Update console display.
202  */
203 static void
204 con3270_update(struct con3270 *cp)
205 {
206         struct raw3270_request *wrq;
207         char wcc, prolog[6];
208         unsigned long flags;
209         unsigned long updated;
210         struct string *s, *n;
211         int rc;
212
213         if (cp->view.dev)
214                 raw3270_activate_view(&cp->view);
215
216         wrq = xchg(&cp->write, 0);
217         if (!wrq) {
218                 con3270_set_timer(cp, 1);
219                 return;
220         }
221
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;
229         }
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);
236
237         wcc = TW_NONE;
238         raw3270_request_add_data(wrq, &wcc, 1);
239
240         /*
241          * Update status line.
242          */
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;
247
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++;
266                 }
267                 if (list_empty(&cp->update))
268                         updated |= CON_UPDATE_LIST;
269         }
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);
279         }
280         spin_unlock_irqrestore(&cp->view.lock, flags);
281 }
282
283 /*
284  * Read tasklet.
285  */
286 static void
287 con3270_read_tasklet(struct raw3270_request *rrq)
288 {
289         static char kreset_data = TW_KR;
290         struct con3270 *cp;
291         unsigned long flags;
292         int nr_up, deactivate;
293
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;
316                 }
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;
323         }
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);
329         }
330         spin_unlock_irqrestore(&cp->view.lock, flags);
331
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);
337
338         if (deactivate)
339                 raw3270_deactivate_view(&cp->view);
340
341         raw3270_request_reset(rrq);
342         xchg(&cp->read, rrq);
343         raw3270_put_view(&cp->view);
344 }
345
346 /*
347  * Read request completion callback.
348  */
349 static void
350 con3270_read_callback(struct raw3270_request *rq, void *data)
351 {
352         raw3270_get_view(rq->view);
353         /* Schedule tasklet to pass input to tty. */
354         tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
355 }
356
357 /*
358  * Issue a read request. Called only from interrupt function.
359  */
360 static void
361 con3270_issue_read(struct con3270 *cp)
362 {
363         struct raw3270_request *rrq;
364         int rc;
365
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);
378 }
379
380 /*
381  * Switch to the console view.
382  */
383 static int
384 con3270_activate(struct raw3270_view *view)
385 {
386         struct con3270 *cp;
387
388         cp = (struct con3270 *) view;
389         cp->update_flags = CON_UPDATE_ALL;
390         con3270_set_timer(cp, 1);
391         return 0;
392 }
393
394 static void
395 con3270_deactivate(struct raw3270_view *view)
396 {
397         struct con3270 *cp;
398
399         cp = (struct con3270 *) view;
400         del_timer(&cp->timer);
401 }
402
403 static int
404 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
405 {
406         /* Handle ATTN. Schedule tasklet to read aid. */
407         if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
408                 con3270_issue_read(cp);
409
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         }
417         return RAW3270_IO_DONE;
418 }
419
420 /* Console view to a 3270 device. */
421 static struct raw3270_fn con3270_fn = {
422         .activate = con3270_activate,
423         .deactivate = con3270_deactivate,
424         .intv = (void *) con3270_irq
425 };
426
427 static inline void
428 con3270_cline_add(struct con3270 *cp)
429 {
430         if (!list_empty(&cp->cline->list))
431                 /* Already added. */
432                 return;
433         list_add_tail(&cp->cline->list, &cp->lines);
434         cp->nr_lines++;
435         con3270_rebuild_update(cp);
436 }
437
438 static inline void
439 con3270_cline_insert(struct con3270 *cp, unsigned char c)
440 {
441         cp->cline->string[cp->cline->len++] = 
442                 cp->view.ascebc[(c < ' ') ? ' ' : c];
443         if (list_empty(&cp->cline->update)) {
444                 list_add_tail(&cp->cline->update, &cp->update);
445                 cp->update_flags |= CON_UPDATE_LIST;
446         }
447 }
448
449 static inline void
450 con3270_cline_end(struct con3270 *cp)
451 {
452         struct string *s;
453         unsigned int size;
454
455         /* Copy cline. */
456         size = (cp->cline->len < cp->view.cols - 5) ?
457                 cp->cline->len + 4 : cp->view.cols;
458         s = con3270_alloc_string(cp, size);
459         memcpy(s->string, cp->cline->string, cp->cline->len);
460         if (cp->cline->len < cp->view.cols - 5) {
461                 s->string[s->len - 4] = TO_RA;
462                 s->string[s->len - 1] = 0;
463         } else {
464                 while (--size >= cp->cline->len)
465                         s->string[size] = cp->view.ascebc[' '];
466         }
467         /* Replace cline with allocated line s and reset cline. */
468         list_add(&s->list, &cp->cline->list);
469         list_del_init(&cp->cline->list);
470         if (!list_empty(&cp->cline->update)) {
471                 list_add(&s->update, &cp->cline->update);
472                 list_del_init(&cp->cline->update);
473         }
474         cp->cline->len = 0;
475 }
476
477 /*
478  * Write a string to the 3270 console
479  */
480 static void
481 con3270_write(struct console *co, const char *str, unsigned int count)
482 {
483         struct con3270 *cp;
484         unsigned long flags;
485         unsigned char c;
486
487         cp = condev;
488         spin_lock_irqsave(&cp->view.lock, flags);
489         while (count-- > 0) {
490                 c = *str++;
491                 if (cp->cline->len == 0)
492                         con3270_cline_add(cp);
493                 if (c != '\n')
494                         con3270_cline_insert(cp, c);
495                 if (c == '\n' || cp->cline->len >= cp->view.cols)
496                         con3270_cline_end(cp);
497         }
498         /* Setup timer to output current console buffer after 1/10 second */
499         cp->nr_up = 0;
500         if (cp->view.dev && !timer_pending(&cp->timer))
501                 con3270_set_timer(cp, HZ/10);
502         spin_unlock_irqrestore(&cp->view.lock,flags);
503 }
504
505 static struct tty_driver *
506 con3270_device(struct console *c, int *index)
507 {
508         *index = c->index;
509         return tty3270_driver;
510 }
511
512 /*
513  * Wait for end of write request.
514  */
515 static void
516 con3270_wait_write(struct con3270 *cp)
517 {
518         while (!cp->write) {
519                 raw3270_wait_cons_dev(cp->view.dev);
520                 barrier();
521         }
522 }
523
524 /*
525  * panic() calls con3270_flush through a panic_notifier
526  * before the system enters a disabled, endless loop.
527  */
528 static void
529 con3270_flush(void)
530 {
531         struct con3270 *cp;
532         unsigned long flags;
533
534         cp = condev;
535         if (!cp->view.dev)
536                 return;
537         raw3270_pm_unfreeze(&cp->view);
538         spin_lock_irqsave(&cp->view.lock, flags);
539         con3270_wait_write(cp);
540         cp->nr_up = 0;
541         con3270_rebuild_update(cp);
542         con3270_update_status(cp);
543         while (cp->update_flags != 0) {
544                 spin_unlock_irqrestore(&cp->view.lock, flags);
545                 con3270_update(cp);
546                 spin_lock_irqsave(&cp->view.lock, flags);
547                 con3270_wait_write(cp);
548         }
549         spin_unlock_irqrestore(&cp->view.lock, flags);
550 }
551
552 static int con3270_notify(struct notifier_block *self,
553                           unsigned long event, void *data)
554 {
555         con3270_flush();
556         return NOTIFY_OK;
557 }
558
559 static struct notifier_block on_panic_nb = {
560         .notifier_call = con3270_notify,
561         .priority = 0,
562 };
563
564 static struct notifier_block on_reboot_nb = {
565         .notifier_call = con3270_notify,
566         .priority = 0,
567 };
568
569 /*
570  *  The console structure for the 3270 console
571  */
572 static struct console con3270 = {
573         .name    = "tty3270",
574         .write   = con3270_write,
575         .device  = con3270_device,
576         .flags   = CON_PRINTBUFFER,
577 };
578
579 /*
580  * 3270 console initialization code called from console_init().
581  */
582 static int __init
583 con3270_init(void)
584 {
585         struct ccw_device *cdev;
586         struct raw3270 *rp;
587         void *cbuf;
588         int i;
589
590         /* Check if 3270 is to be the console */
591         if (!CONSOLE_IS_3270)
592                 return -ENODEV;
593
594         /* Set the console mode for VM */
595         if (MACHINE_IS_VM) {
596                 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
597                 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
598         }
599
600         cdev = ccw_device_probe_console();
601         if (IS_ERR(cdev))
602                 return -ENODEV;
603         rp = raw3270_setup_console(cdev);
604         if (IS_ERR(rp))
605                 return PTR_ERR(rp);
606
607         condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
608         condev->view.dev = rp;
609
610         condev->read = raw3270_request_alloc(0);
611         condev->read->callback = con3270_read_callback;
612         condev->read->callback_data = condev;
613         condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
614         condev->kreset = raw3270_request_alloc(1);
615
616         INIT_LIST_HEAD(&condev->lines);
617         INIT_LIST_HEAD(&condev->update);
618         setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
619                     (unsigned long) condev);
620         tasklet_init(&condev->readlet, 
621                      (void (*)(unsigned long)) con3270_read_tasklet,
622                      (unsigned long) condev->read);
623
624         raw3270_add_view(&condev->view, &con3270_fn, 1);
625
626         INIT_LIST_HEAD(&condev->freemem);
627         for (i = 0; i < CON3270_STRING_PAGES; i++) {
628                 cbuf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
629                 add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
630         }
631         condev->cline = alloc_string(&condev->freemem, condev->view.cols);
632         condev->cline->len = 0;
633         con3270_create_status(condev);
634         condev->input = alloc_string(&condev->freemem, 80);
635         atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
636         register_reboot_notifier(&on_reboot_nb);
637         register_console(&con3270);
638         return 0;
639 }
640
641 console_initcall(con3270_init);