Merge branch 'for-linus' of git://git390.marist.edu/pub/scm/linux-2.6
[linux-2.6] / drivers / s390 / char / con3270.c
1 /*
2  *  drivers/s390/char/con3270.c
3  *    IBM/3270 Driver - console view.
4  *
5  *  Author(s):
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
9  */
10
11 #include <linux/bootmem.h>
12 #include <linux/console.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/types.h>
17 #include <linux/err.h>
18 #include <linux/reboot.h>
19
20 #include <asm/ccwdev.h>
21 #include <asm/cio.h>
22 #include <asm/cpcmd.h>
23 #include <asm/ebcdic.h>
24
25 #include "raw3270.h"
26 #include "tty3270.h"
27 #include "ctrlchar.h"
28
29 #define CON3270_OUTPUT_BUFFER_SIZE 1024
30 #define CON3270_STRING_PAGES 4
31
32 static struct raw3270_fn con3270_fn;
33
34 /*
35  * Main 3270 console view data structure.
36  */
37 struct con3270 {
38         struct raw3270_view view;
39         spinlock_t lock;
40         struct list_head freemem;       /* list of free memory for strings. */
41
42         /* Output stuff. */
43         struct list_head lines;         /* list of lines. */
44         struct list_head update;        /* list of lines to update. */
45         int line_nr;                    /* line number for next update. */
46         int nr_lines;                   /* # lines in list. */
47         int nr_up;                      /* # lines up in history. */
48         unsigned long update_flags;     /* Update indication bits. */
49         struct string *cline;           /* current output line. */
50         struct string *status;          /* last line of display. */
51         struct raw3270_request *write;  /* single write request. */
52         struct timer_list timer;
53
54         /* Input stuff. */
55         struct string *input;           /* input string for read request. */
56         struct raw3270_request *read;   /* single read request. */
57         struct raw3270_request *kreset; /* single keyboard reset request. */
58         struct tasklet_struct readlet;  /* tasklet to issue read request. */
59 };
60
61 static struct con3270 *condev;
62
63 /* con3270->update_flags. See con3270_update for details. */
64 #define CON_UPDATE_ERASE        1       /* Use EWRITEA instead of WRITE. */
65 #define CON_UPDATE_LIST         2       /* Update lines in tty3270->update. */
66 #define CON_UPDATE_STATUS       4       /* Update status line. */
67 #define CON_UPDATE_ALL          8       /* Recreate screen. */
68
69 static void con3270_update(struct con3270 *);
70
71 /*
72  * Setup timeout for a device. On timeout trigger an update.
73  */
74 static void con3270_set_timer(struct con3270 *cp, int expires)
75 {
76         if (expires == 0)
77                 del_timer(&cp->timer);
78         else
79                 mod_timer(&cp->timer, jiffies + expires);
80 }
81
82 /*
83  * The status line is the last line of the screen. It shows the string
84  * "console view" in the lower left corner and "Running"/"More..."/"Holding"
85  * in the lower right corner of the screen.
86  */
87 static void
88 con3270_update_status(struct con3270 *cp)
89 {
90         char *str;
91
92         str = (cp->nr_up != 0) ? "History" : "Running";
93         memcpy(cp->status->string + 24, str, 7);
94         codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
95         cp->update_flags |= CON_UPDATE_STATUS;
96 }
97
98 static void
99 con3270_create_status(struct con3270 *cp)
100 {
101         static const unsigned char blueprint[] =
102                 { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
103                   'c','o','n','s','o','l','e',' ','v','i','e','w',
104                   TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
105
106         cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
107         /* Copy blueprint to status line */
108         memcpy(cp->status->string, blueprint, sizeof(blueprint));
109         /* Set TO_RA addresses. */
110         raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
111                                cp->view.cols * (cp->view.rows - 1));
112         raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
113                                cp->view.cols * cp->view.rows - 8);
114         /* Convert strings to ebcdic. */
115         codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
116         codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
117 }
118
119 /*
120  * Set output offsets to 3270 datastream fragment of a console string.
121  */
122 static void
123 con3270_update_string(struct con3270 *cp, struct string *s, int nr)
124 {
125         if (s->len >= cp->view.cols - 5)
126                 return;
127         raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
128                                cp->view.cols * (nr + 1));
129 }
130
131 /*
132  * Rebuild update list to print all lines.
133  */
134 static void
135 con3270_rebuild_update(struct con3270 *cp)
136 {
137         struct string *s, *n;
138         int nr;
139
140         /* 
141          * Throw away update list and create a new one,
142          * containing all lines that will fit on the screen.
143          */
144         list_for_each_entry_safe(s, n, &cp->update, update)
145                 list_del_init(&s->update);
146         nr = cp->view.rows - 2 + cp->nr_up;
147         list_for_each_entry_reverse(s, &cp->lines, list) {
148                 if (nr < cp->view.rows - 1)
149                         list_add(&s->update, &cp->update);
150                 if (--nr < 0)
151                         break;
152         }
153         cp->line_nr = 0;
154         cp->update_flags |= CON_UPDATE_LIST;
155 }
156
157 /*
158  * Alloc string for size bytes. Free strings from history if necessary.
159  */
160 static struct string *
161 con3270_alloc_string(struct con3270 *cp, size_t size)
162 {
163         struct string *s, *n;
164
165         s = alloc_string(&cp->freemem, size);
166         if (s)
167                 return s;
168         list_for_each_entry_safe(s, n, &cp->lines, list) {
169                 list_del(&s->list);
170                 if (!list_empty(&s->update))
171                         list_del(&s->update);
172                 cp->nr_lines--;
173                 if (free_string(&cp->freemem, s) >= size)
174                         break;
175         }
176         s = alloc_string(&cp->freemem, size);
177         BUG_ON(!s);
178         if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
179                 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
180                 con3270_rebuild_update(cp);
181                 con3270_update_status(cp);
182         }
183         return s;
184 }
185
186 /*
187  * Write completion callback.
188  */
189 static void
190 con3270_write_callback(struct raw3270_request *rq, void *data)
191 {
192         raw3270_request_reset(rq);
193         xchg(&((struct con3270 *) rq->view)->write, rq);
194 }
195
196 /*
197  * Update console display.
198  */
199 static void
200 con3270_update(struct con3270 *cp)
201 {
202         struct raw3270_request *wrq;
203         char wcc, prolog[6];
204         unsigned long flags;
205         unsigned long updated;
206         struct string *s, *n;
207         int rc;
208
209         if (cp->view.dev)
210                 raw3270_activate_view(&cp->view);
211
212         wrq = xchg(&cp->write, 0);
213         if (!wrq) {
214                 con3270_set_timer(cp, 1);
215                 return;
216         }
217
218         spin_lock_irqsave(&cp->view.lock, flags);
219         updated = 0;
220         if (cp->update_flags & CON_UPDATE_ALL) {
221                 con3270_rebuild_update(cp);
222                 con3270_update_status(cp);
223                 cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
224                         CON_UPDATE_STATUS;
225         }
226         if (cp->update_flags & CON_UPDATE_ERASE) {
227                 /* Use erase write alternate to initialize display. */
228                 raw3270_request_set_cmd(wrq, TC_EWRITEA);
229                 updated |= CON_UPDATE_ERASE;
230         } else
231                 raw3270_request_set_cmd(wrq, TC_WRITE);
232
233         wcc = TW_NONE;
234         raw3270_request_add_data(wrq, &wcc, 1);
235
236         /*
237          * Update status line.
238          */
239         if (cp->update_flags & CON_UPDATE_STATUS)
240                 if (raw3270_request_add_data(wrq, cp->status->string,
241                                              cp->status->len) == 0)
242                         updated |= CON_UPDATE_STATUS;
243
244         if (cp->update_flags & CON_UPDATE_LIST) {
245                 prolog[0] = TO_SBA;
246                 prolog[3] = TO_SA;
247                 prolog[4] = TAT_COLOR;
248                 prolog[5] = TAC_TURQ;
249                 raw3270_buffer_address(cp->view.dev, prolog + 1,
250                                        cp->view.cols * cp->line_nr);
251                 raw3270_request_add_data(wrq, prolog, 6);
252                 /* Write strings in the update list to the screen. */
253                 list_for_each_entry_safe(s, n, &cp->update, update) {
254                         if (s != cp->cline)
255                                 con3270_update_string(cp, s, cp->line_nr);
256                         if (raw3270_request_add_data(wrq, s->string,
257                                                      s->len) != 0)
258                                 break;
259                         list_del_init(&s->update);
260                         if (s != cp->cline)
261                                 cp->line_nr++;
262                 }
263                 if (list_empty(&cp->update))
264                         updated |= CON_UPDATE_LIST;
265         }
266         wrq->callback = con3270_write_callback;
267         rc = raw3270_start(&cp->view, wrq);
268         if (rc == 0) {
269                 cp->update_flags &= ~updated;
270                 if (cp->update_flags)
271                         con3270_set_timer(cp, 1);
272         } else {
273                 raw3270_request_reset(wrq);
274                 xchg(&cp->write, wrq);
275         }
276         spin_unlock_irqrestore(&cp->view.lock, flags);
277 }
278
279 /*
280  * Read tasklet.
281  */
282 static void
283 con3270_read_tasklet(struct raw3270_request *rrq)
284 {
285         static char kreset_data = TW_KR;
286         struct con3270 *cp;
287         unsigned long flags;
288         int nr_up, deactivate;
289
290         cp = (struct con3270 *) rrq->view;
291         spin_lock_irqsave(&cp->view.lock, flags);
292         nr_up = cp->nr_up;
293         deactivate = 0;
294         /* Check aid byte. */
295         switch (cp->input->string[0]) {
296         case 0x7d:      /* enter: jump to bottom. */
297                 nr_up = 0;
298                 break;
299         case 0xf3:      /* PF3: deactivate the console view. */
300                 deactivate = 1;
301                 break;
302         case 0x6d:      /* clear: start from scratch. */
303                 cp->update_flags = CON_UPDATE_ALL;
304                 con3270_set_timer(cp, 1);
305                 break;
306         case 0xf7:      /* PF7: do a page up in the console log. */
307                 nr_up += cp->view.rows - 2;
308                 if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
309                         nr_up = cp->nr_lines - cp->view.rows + 1;
310                         if (nr_up < 0)
311                                 nr_up = 0;
312                 }
313                 break;
314         case 0xf8:      /* PF8: do a page down in the console log. */
315                 nr_up -= cp->view.rows - 2;
316                 if (nr_up < 0)
317                         nr_up = 0;
318                 break;
319         }
320         if (nr_up != cp->nr_up) {
321                 cp->nr_up = nr_up;
322                 con3270_rebuild_update(cp);
323                 con3270_update_status(cp);
324                 con3270_set_timer(cp, 1);
325         }
326         spin_unlock_irqrestore(&cp->view.lock, flags);
327
328         /* Start keyboard reset command. */
329         raw3270_request_reset(cp->kreset);
330         raw3270_request_set_cmd(cp->kreset, TC_WRITE);
331         raw3270_request_add_data(cp->kreset, &kreset_data, 1);
332         raw3270_start(&cp->view, cp->kreset);
333
334         if (deactivate)
335                 raw3270_deactivate_view(&cp->view);
336
337         raw3270_request_reset(rrq);
338         xchg(&cp->read, rrq);
339         raw3270_put_view(&cp->view);
340 }
341
342 /*
343  * Read request completion callback.
344  */
345 static void
346 con3270_read_callback(struct raw3270_request *rq, void *data)
347 {
348         raw3270_get_view(rq->view);
349         /* Schedule tasklet to pass input to tty. */
350         tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
351 }
352
353 /*
354  * Issue a read request. Called only from interrupt function.
355  */
356 static void
357 con3270_issue_read(struct con3270 *cp)
358 {
359         struct raw3270_request *rrq;
360         int rc;
361
362         rrq = xchg(&cp->read, 0);
363         if (!rrq)
364                 /* Read already scheduled. */
365                 return;
366         rrq->callback = con3270_read_callback;
367         rrq->callback_data = cp;
368         raw3270_request_set_cmd(rrq, TC_READMOD);
369         raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
370         /* Issue the read modified request. */
371         rc = raw3270_start_irq(&cp->view, rrq);
372         if (rc)
373                 raw3270_request_reset(rrq);
374 }
375
376 /*
377  * Switch to the console view.
378  */
379 static int
380 con3270_activate(struct raw3270_view *view)
381 {
382         struct con3270 *cp;
383
384         cp = (struct con3270 *) view;
385         cp->update_flags = CON_UPDATE_ALL;
386         con3270_set_timer(cp, 1);
387         return 0;
388 }
389
390 static void
391 con3270_deactivate(struct raw3270_view *view)
392 {
393         struct con3270 *cp;
394
395         cp = (struct con3270 *) view;
396         del_timer(&cp->timer);
397 }
398
399 static int
400 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
401 {
402         /* Handle ATTN. Schedule tasklet to read aid. */
403         if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
404                 con3270_issue_read(cp);
405
406         if (rq) {
407                 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
408                         rq->rc = -EIO;
409                 else
410                         /* Normal end. Copy residual count. */
411                         rq->rescnt = irb->scsw.cmd.count;
412         }
413         return RAW3270_IO_DONE;
414 }
415
416 /* Console view to a 3270 device. */
417 static struct raw3270_fn con3270_fn = {
418         .activate = con3270_activate,
419         .deactivate = con3270_deactivate,
420         .intv = (void *) con3270_irq
421 };
422
423 static inline void
424 con3270_cline_add(struct con3270 *cp)
425 {
426         if (!list_empty(&cp->cline->list))
427                 /* Already added. */
428                 return;
429         list_add_tail(&cp->cline->list, &cp->lines);
430         cp->nr_lines++;
431         con3270_rebuild_update(cp);
432 }
433
434 static inline void
435 con3270_cline_insert(struct con3270 *cp, unsigned char c)
436 {
437         cp->cline->string[cp->cline->len++] = 
438                 cp->view.ascebc[(c < ' ') ? ' ' : c];
439         if (list_empty(&cp->cline->update)) {
440                 list_add_tail(&cp->cline->update, &cp->update);
441                 cp->update_flags |= CON_UPDATE_LIST;
442         }
443 }
444
445 static inline void
446 con3270_cline_end(struct con3270 *cp)
447 {
448         struct string *s;
449         unsigned int size;
450
451         /* Copy cline. */
452         size = (cp->cline->len < cp->view.cols - 5) ?
453                 cp->cline->len + 4 : cp->view.cols;
454         s = con3270_alloc_string(cp, size);
455         memcpy(s->string, cp->cline->string, cp->cline->len);
456         if (s->len < cp->view.cols - 5) {
457                 s->string[s->len - 4] = TO_RA;
458                 s->string[s->len - 1] = 0;
459         } else {
460                 while (--size > cp->cline->len)
461                         s->string[size] = cp->view.ascebc[' '];
462         }
463         /* Replace cline with allocated line s and reset cline. */
464         list_add(&s->list, &cp->cline->list);
465         list_del_init(&cp->cline->list);
466         if (!list_empty(&cp->cline->update)) {
467                 list_add(&s->update, &cp->cline->update);
468                 list_del_init(&cp->cline->update);
469         }
470         cp->cline->len = 0;
471 }
472
473 /*
474  * Write a string to the 3270 console
475  */
476 static void
477 con3270_write(struct console *co, const char *str, unsigned int count)
478 {
479         struct con3270 *cp;
480         unsigned long flags;
481         unsigned char c;
482
483         cp = condev;
484         spin_lock_irqsave(&cp->view.lock, flags);
485         while (count-- > 0) {
486                 c = *str++;
487                 if (cp->cline->len == 0)
488                         con3270_cline_add(cp);
489                 if (c != '\n')
490                         con3270_cline_insert(cp, c);
491                 if (c == '\n' || cp->cline->len >= cp->view.cols)
492                         con3270_cline_end(cp);
493         }
494         /* Setup timer to output current console buffer after 1/10 second */
495         cp->nr_up = 0;
496         if (cp->view.dev && !timer_pending(&cp->timer))
497                 con3270_set_timer(cp, HZ/10);
498         spin_unlock_irqrestore(&cp->view.lock,flags);
499 }
500
501 static struct tty_driver *
502 con3270_device(struct console *c, int *index)
503 {
504         *index = c->index;
505         return tty3270_driver;
506 }
507
508 /*
509  * Wait for end of write request.
510  */
511 static void
512 con3270_wait_write(struct con3270 *cp)
513 {
514         while (!cp->write) {
515                 raw3270_wait_cons_dev(cp->view.dev);
516                 barrier();
517         }
518 }
519
520 /*
521  * panic() calls con3270_flush through a panic_notifier
522  * before the system enters a disabled, endless loop.
523  */
524 static void
525 con3270_flush(void)
526 {
527         struct con3270 *cp;
528         unsigned long flags;
529
530         cp = condev;
531         if (!cp->view.dev)
532                 return;
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);
543         }
544         spin_unlock_irqrestore(&cp->view.lock, flags);
545 }
546
547 static int con3270_notify(struct notifier_block *self,
548                           unsigned long event, void *data)
549 {
550         con3270_flush();
551         return NOTIFY_OK;
552 }
553
554 static struct notifier_block on_panic_nb = {
555         .notifier_call = con3270_notify,
556         .priority = 0,
557 };
558
559 static struct notifier_block on_reboot_nb = {
560         .notifier_call = con3270_notify,
561         .priority = 0,
562 };
563
564 /*
565  *  The console structure for the 3270 console
566  */
567 static struct console con3270 = {
568         .name    = "tty3270",
569         .write   = con3270_write,
570         .device  = con3270_device,
571         .flags   = CON_PRINTBUFFER,
572 };
573
574 /*
575  * 3270 console initialization code called from console_init().
576  * NOTE: This is called before kmalloc is available.
577  */
578 static int __init
579 con3270_init(void)
580 {
581         struct ccw_device *cdev;
582         struct raw3270 *rp;
583         void *cbuf;
584         int i;
585
586         /* Check if 3270 is to be the console */
587         if (!CONSOLE_IS_3270)
588                 return -ENODEV;
589
590         /* Set the console mode for VM */
591         if (MACHINE_IS_VM) {
592                 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
593                 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
594         }
595
596         cdev = ccw_device_probe_console();
597         if (IS_ERR(cdev))
598                 return -ENODEV;
599         rp = raw3270_setup_console(cdev);
600         if (IS_ERR(rp))
601                 return PTR_ERR(rp);
602
603         condev = (struct con3270 *) alloc_bootmem_low(sizeof(struct con3270));
604         memset(condev, 0, sizeof(struct con3270));
605         condev->view.dev = rp;
606
607         condev->read = raw3270_request_alloc_bootmem(0);
608         condev->read->callback = con3270_read_callback;
609         condev->read->callback_data = condev;
610         condev->write = 
611                 raw3270_request_alloc_bootmem(CON3270_OUTPUT_BUFFER_SIZE);
612         condev->kreset = raw3270_request_alloc_bootmem(1);
613
614         INIT_LIST_HEAD(&condev->lines);
615         INIT_LIST_HEAD(&condev->update);
616         setup_timer(&condev->timer, (void (*)(unsigned long)) con3270_update,
617                     (unsigned long) condev);
618         tasklet_init(&condev->readlet, 
619                      (void (*)(unsigned long)) con3270_read_tasklet,
620                      (unsigned long) condev->read);
621
622         raw3270_add_view(&condev->view, &con3270_fn, 1);
623
624         INIT_LIST_HEAD(&condev->freemem);
625         for (i = 0; i < CON3270_STRING_PAGES; i++) {
626                 cbuf = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
627                 add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
628         }
629         condev->cline = alloc_string(&condev->freemem, condev->view.cols);
630         condev->cline->len = 0;
631         con3270_create_status(condev);
632         condev->input = alloc_string(&condev->freemem, 80);
633         atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
634         register_reboot_notifier(&on_reboot_nb);
635         register_console(&con3270);
636         return 0;
637 }
638
639 console_initcall(con3270_init);