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