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