ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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                 if (timer_pending(&cp->timer))
77                         del_timer(&cp->timer);
78                 return;
79         }
80         if (timer_pending(&cp->timer)) {
81                 if (mod_timer(&cp->timer, jiffies + expires))
82                         return;
83         }
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         wrq = xchg(&cp->write, 0);
218         if (!wrq) {
219                 con3270_set_timer(cp, 1);
220                 return;
221         }
222
223         spin_lock_irqsave(&cp->view.lock, flags);
224         updated = 0;
225         if (cp->update_flags & CON_UPDATE_ERASE) {
226                 /* Use erase write alternate to initialize display. */
227                 raw3270_request_set_cmd(wrq, TC_EWRITEA);
228                 updated |= CON_UPDATE_ERASE;
229         } else
230                 raw3270_request_set_cmd(wrq, TC_WRITE);
231
232         wcc = TW_NONE;
233         raw3270_request_add_data(wrq, &wcc, 1);
234
235         /*
236          * Update status line.
237          */
238         if (cp->update_flags & CON_UPDATE_STATUS)
239                 if (raw3270_request_add_data(wrq, cp->status->string,
240                                              cp->status->len) == 0)
241                         updated |= CON_UPDATE_STATUS;
242
243         if (cp->update_flags & CON_UPDATE_LIST) {
244                 prolog[0] = TO_SBA;
245                 prolog[3] = TO_SA;
246                 prolog[4] = TAT_COLOR;
247                 prolog[5] = TAC_TURQ;
248                 raw3270_buffer_address(cp->view.dev, prolog + 1,
249                                        cp->view.cols * cp->line_nr);
250                 raw3270_request_add_data(wrq, prolog, 6);
251                 /* Write strings in the update list to the screen. */
252                 list_for_each_entry_safe(s, n, &cp->update, update) {
253                         if (s != cp->cline)
254                                 con3270_update_string(cp, s, cp->line_nr);
255                         if (raw3270_request_add_data(wrq, s->string,
256                                                      s->len) != 0)
257                                 break;
258                         list_del_init(&s->update);
259                         if (s != cp->cline)
260                                 cp->line_nr++;
261                 }
262                 if (list_empty(&cp->update))
263                         updated |= CON_UPDATE_LIST;
264         }
265         wrq->callback = con3270_write_callback;
266         rc = raw3270_start(&cp->view, wrq);
267         if (rc == 0) {
268                 cp->update_flags &= ~updated;
269                 if (cp->update_flags)
270                         con3270_set_timer(cp, 1);
271         } else {
272                 raw3270_request_reset(wrq);
273                 xchg(&cp->write, wrq);
274         }
275         spin_unlock_irqrestore(&cp->view.lock, flags);
276 }
277
278 /*
279  * Read tasklet.
280  */
281 static void
282 con3270_read_tasklet(struct raw3270_request *rrq)
283 {
284         static char kreset_data = TW_KR;
285         struct con3270 *cp;
286         unsigned long flags;
287         int nr_up, deactivate;
288
289         cp = (struct con3270 *) rrq->view;
290         spin_lock_irqsave(&cp->view.lock, flags);
291         nr_up = cp->nr_up;
292         deactivate = 0;
293         /* Check aid byte. */
294         switch (cp->input->string[0]) {
295         case 0x7d:      /* enter: jump to bottom. */
296                 nr_up = 0;
297                 break;
298         case 0xf3:      /* PF3: deactivate the console view. */
299                 deactivate = 1;
300                 break;
301         case 0x6d:      /* clear: start from scratch. */
302                 con3270_rebuild_update(cp);
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         unsigned long flags;
383         struct con3270 *cp;
384
385         cp = (struct con3270 *) view;
386         spin_lock_irqsave(&cp->view.lock, flags);
387         cp->nr_up = 0;
388         con3270_rebuild_update(cp);
389         con3270_update_status(cp);
390         cp->update_flags = CON_UPDATE_ALL;
391         con3270_set_timer(cp, 1);
392         spin_unlock_irqrestore(&cp->view.lock, flags);
393         return 0;
394 }
395
396 static void
397 con3270_deactivate(struct raw3270_view *view)
398 {
399         unsigned long flags;
400         struct con3270 *cp;
401
402         cp = (struct con3270 *) view;
403         spin_lock_irqsave(&cp->view.lock, flags);
404         del_timer(&cp->timer);
405         spin_unlock_irqrestore(&cp->view.lock, flags);
406 }
407
408 static int
409 con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
410 {
411         /* Handle ATTN. Schedule tasklet to read aid. */
412         if (irb->scsw.dstat & DEV_STAT_ATTENTION)
413                 con3270_issue_read(cp);
414
415         if (rq) {
416                 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
417                         rq->rc = -EIO;
418                 else
419                         /* Normal end. Copy residual count. */
420                         rq->rescnt = irb->scsw.count;
421         }
422         return RAW3270_IO_DONE;
423 }
424
425 /* Console view to a 3270 device. */
426 static struct raw3270_fn con3270_fn = {
427         .activate = con3270_activate,
428         .deactivate = con3270_deactivate,
429         .intv = (void *) con3270_irq
430 };
431
432 static inline void
433 con3270_cline_add(struct con3270 *cp)
434 {
435         if (!list_empty(&cp->cline->list))
436                 /* Already added. */
437                 return;
438         list_add_tail(&cp->cline->list, &cp->lines);
439         cp->nr_lines++;
440         con3270_rebuild_update(cp);
441 }
442
443 static inline void
444 con3270_cline_insert(struct con3270 *cp, unsigned char c)
445 {
446         cp->cline->string[cp->cline->len++] = 
447                 cp->view.ascebc[(c < ' ') ? ' ' : c];
448         if (list_empty(&cp->cline->update)) {
449                 list_add_tail(&cp->cline->update, &cp->update);
450                 cp->update_flags |= CON_UPDATE_LIST;
451         }
452 }
453
454 static inline void
455 con3270_cline_end(struct con3270 *cp)
456 {
457         struct string *s;
458         unsigned int size;
459
460         /* Copy cline. */
461         size = (cp->cline->len < cp->view.cols - 5) ?
462                 cp->cline->len + 4 : cp->view.cols;
463         s = con3270_alloc_string(cp, size);
464         memcpy(s->string, cp->cline->string, cp->cline->len);
465         if (s->len < cp->view.cols - 5) {
466                 s->string[s->len - 4] = TO_RA;
467                 s->string[s->len - 1] = 0;
468         } else {
469                 while (--size > cp->cline->len)
470                         s->string[size] = cp->view.ascebc[' '];
471         }
472         /* Replace cline with allocated line s and reset cline. */
473         list_add(&s->list, &cp->cline->list);
474         list_del_init(&cp->cline->list);
475         if (!list_empty(&cp->cline->update)) {
476                 list_add(&s->update, &cp->cline->update);
477                 list_del_init(&cp->cline->update);
478         }
479         cp->cline->len = 0;
480 }
481
482 /*
483  * Write a string to the 3270 console
484  */
485 static void
486 con3270_write(struct console *co, const char *str, unsigned int count)
487 {
488         struct con3270 *cp;
489         unsigned long flags;
490         unsigned char c;
491
492         cp = condev;
493         if (cp->view.dev)
494                 raw3270_activate_view(&cp->view);
495         spin_lock_irqsave(&cp->view.lock, flags);
496         while (count-- > 0) {
497                 c = *str++;
498                 if (cp->cline->len == 0)
499                         con3270_cline_add(cp);
500                 if (c != '\n')
501                         con3270_cline_insert(cp, c);
502                 if (c == '\n' || cp->cline->len >= cp->view.cols)
503                         con3270_cline_end(cp);
504         }
505         /* Setup timer to output current console buffer after 1/10 second */
506         if (cp->view.dev && !timer_pending(&cp->timer))
507                 con3270_set_timer(cp, HZ/10);
508         spin_unlock_irqrestore(&cp->view.lock,flags);
509 }
510
511 extern struct tty_driver *tty3270_driver;
512
513 static struct tty_driver *
514 con3270_device(struct console *c, int *index)
515 {
516         *index = c->index;
517         return tty3270_driver;
518 }
519
520 /*
521  * Wait for end of write request.
522  */
523 static void
524 con3270_wait_write(struct con3270 *cp)
525 {
526         while (!cp->write) {
527                 raw3270_wait_cons_dev(cp->view.dev);
528                 barrier();
529         }
530 }
531
532 /*
533  * panic() calls console_unblank before the system enters a
534  * disabled, endless loop.
535  */
536 static void
537 con3270_unblank(void)
538 {
539         struct con3270 *cp;
540         unsigned long flags;
541
542         cp = condev;
543         if (!cp->view.dev)
544                 return;
545         spin_lock_irqsave(&cp->view.lock, flags);
546         con3270_wait_write(cp);
547         cp->nr_up = 0;
548         con3270_rebuild_update(cp);
549         con3270_update_status(cp);
550         while (cp->update_flags != 0) {
551                 spin_unlock_irqrestore(&cp->view.lock, flags);
552                 con3270_update(cp);
553                 spin_lock_irqsave(&cp->view.lock, flags);
554                 con3270_wait_write(cp);
555         }
556         spin_unlock_irqrestore(&cp->view.lock, flags);
557 }
558
559 static int __init 
560 con3270_consetup(struct console *co, char *options)
561 {
562         return 0;
563 }
564
565 /*
566  *  The console structure for the 3270 console
567  */
568 static struct console con3270 = {
569         .name    = "tty3270",
570         .write   = con3270_write,
571         .device  = con3270_device,
572         .unblank = con3270_unblank,
573         .setup   = con3270_consetup,
574         .flags   = CON_PRINTBUFFER,
575 };
576
577 /*
578  * 3270 console initialization code called from console_init().
579  * NOTE: This is called before kmalloc is available.
580  */
581 static int __init
582 con3270_init(void)
583 {
584         struct ccw_device *cdev;
585         struct raw3270 *rp;
586         void *cbuf;
587         int i;
588
589         /* Check if 3270 is to be the console */
590         if (!CONSOLE_IS_3270)
591                 return -ENODEV;
592
593         /* Set the console mode for VM */
594         if (MACHINE_IS_VM) {
595                 cpcmd("TERM CONMODE 3270", 0, 0);
596                 cpcmd("TERM AUTOCR OFF", 0, 0);
597         }
598
599         cdev = ccw_device_probe_console();
600         if (!cdev)
601                 return -ENODEV;
602         rp = raw3270_setup_console(cdev);
603         if (IS_ERR(rp))
604                 return PTR_ERR(rp);
605
606         condev = (struct con3270 *) alloc_bootmem_low(sizeof(struct con3270));
607         memset(condev, 0, sizeof(struct con3270));
608         condev->view.dev = rp;
609
610         condev->read = raw3270_request_alloc_bootmem(0);
611         condev->read->callback = con3270_read_callback;
612         condev->read->callback_data = condev;
613         condev->write = 
614                 raw3270_request_alloc_bootmem(CON3270_OUTPUT_BUFFER_SIZE);
615         condev->kreset = raw3270_request_alloc_bootmem(1);
616
617         INIT_LIST_HEAD(&condev->lines);
618         INIT_LIST_HEAD(&condev->update);
619         init_timer(&condev->timer);
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, 0);
625
626         INIT_LIST_HEAD(&condev->freemem);
627         for (i = 0; i < CON3270_STRING_PAGES; i++) {
628                 cbuf = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
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         register_console(&con3270);
636         return 0;
637 }
638
639 console_initcall(con3270_init);