1
2
3
4
5
6
7
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
31static struct raw3270_fn con3270_fn;
32
33
34
35
36struct con3270 {
37 struct raw3270_view view;
38 struct list_head freemem;
39
40
41 struct list_head lines;
42 struct list_head update;
43 int line_nr;
44 int nr_lines;
45 int nr_up;
46 unsigned long update_flags;
47 struct string *cline;
48 struct string *status;
49 struct raw3270_request *write;
50 struct timer_list timer;
51
52
53 struct string *input;
54 struct raw3270_request *read;
55 struct raw3270_request *kreset;
56 struct tasklet_struct readlet;
57};
58
59static struct con3270 *condev;
60
61
62#define CON_UPDATE_ERASE 1
63#define CON_UPDATE_LIST 2
64#define CON_UPDATE_STATUS 4
65#define CON_UPDATE_ALL 8
66
67static void con3270_update(struct con3270 *);
68
69
70
71
72static void con3270_set_timer(struct con3270 *cp, int expires)
73{
74 if (expires == 0)
75 del_timer(&cp->timer);
76 else
77 mod_timer(&cp->timer, jiffies + expires);
78}
79
80
81
82
83
84
85static void
86con3270_update_status(struct con3270 *cp)
87{
88 char *str;
89
90 str = (cp->nr_up != 0) ? "History" : "Running";
91 memcpy(cp->status->string + 24, str, 7);
92 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
93 cp->update_flags |= CON_UPDATE_STATUS;
94}
95
96static void
97con3270_create_status(struct con3270 *cp)
98{
99 static const unsigned char blueprint[] =
100 { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
101 'c','o','n','s','o','l','e',' ','v','i','e','w',
102 TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
103
104 cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
105
106 memcpy(cp->status->string, blueprint, sizeof(blueprint));
107
108 raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
109 cp->view.cols * (cp->view.rows - 1));
110 raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
111 cp->view.cols * cp->view.rows - 8);
112
113 codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
114 codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
115}
116
117
118
119
120static void
121con3270_update_string(struct con3270 *cp, struct string *s, int nr)
122{
123 if (s->len >= cp->view.cols - 5)
124 return;
125 raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
126 cp->view.cols * (nr + 1));
127}
128
129
130
131
132static void
133con3270_rebuild_update(struct con3270 *cp)
134{
135 struct string *s, *n;
136 int nr;
137
138
139
140
141
142 list_for_each_entry_safe(s, n, &cp->update, update)
143 list_del_init(&s->update);
144 nr = cp->view.rows - 2 + cp->nr_up;
145 list_for_each_entry_reverse(s, &cp->lines, list) {
146 if (nr < cp->view.rows - 1)
147 list_add(&s->update, &cp->update);
148 if (--nr < 0)
149 break;
150 }
151 cp->line_nr = 0;
152 cp->update_flags |= CON_UPDATE_LIST;
153}
154
155
156
157
158static struct string *
159con3270_alloc_string(struct con3270 *cp, size_t size)
160{
161 struct string *s, *n;
162
163 s = alloc_string(&cp->freemem, size);
164 if (s)
165 return s;
166 list_for_each_entry_safe(s, n, &cp->lines, list) {
167 list_del(&s->list);
168 if (!list_empty(&s->update))
169 list_del(&s->update);
170 cp->nr_lines--;
171 if (free_string(&cp->freemem, s) >= size)
172 break;
173 }
174 s = alloc_string(&cp->freemem, size);
175 BUG_ON(!s);
176 if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
177 cp->nr_up = cp->nr_lines - cp->view.rows + 1;
178 con3270_rebuild_update(cp);
179 con3270_update_status(cp);
180 }
181 return s;
182}
183
184
185
186
187static void
188con3270_write_callback(struct raw3270_request *rq, void *data)
189{
190 raw3270_request_reset(rq);
191 xchg(&((struct con3270 *) rq->view)->write, rq);
192}
193
194
195
196
197static void
198con3270_update(struct con3270 *cp)
199{
200 struct raw3270_request *wrq;
201 char wcc, prolog[6];
202 unsigned long flags;
203 unsigned long updated;
204 struct string *s, *n;
205 int rc;
206
207 if (cp->view.dev)
208 raw3270_activate_view(&cp->view);
209
210 wrq = xchg(&cp->write, 0);
211 if (!wrq) {
212 con3270_set_timer(cp, 1);
213 return;
214 }
215
216 spin_lock_irqsave(&cp->view.lock, flags);
217 updated = 0;
218 if (cp->update_flags & CON_UPDATE_ALL) {
219 con3270_rebuild_update(cp);
220 con3270_update_status(cp);
221 cp->update_flags = CON_UPDATE_ERASE | CON_UPDATE_LIST |
222 CON_UPDATE_STATUS;
223 }
224 if (cp->update_flags & CON_UPDATE_ERASE) {
225
226 raw3270_request_set_cmd(wrq, TC_EWRITEA);
227 updated |= CON_UPDATE_ERASE;
228 } else
229 raw3270_request_set_cmd(wrq, TC_WRITE);
230
231 wcc = TW_NONE;
232 raw3270_request_add_data(wrq, &wcc, 1);
233
234
235
236
237 if (cp->update_flags & CON_UPDATE_STATUS)
238 if (raw3270_request_add_data(wrq, cp->status->string,
239 cp->status->len) == 0)
240 updated |= CON_UPDATE_STATUS;
241
242 if (cp->update_flags & CON_UPDATE_LIST) {
243 prolog[0] = TO_SBA;
244 prolog[3] = TO_SA;
245 prolog[4] = TAT_COLOR;
246 prolog[5] = TAC_TURQ;
247 raw3270_buffer_address(cp->view.dev, prolog + 1,
248 cp->view.cols * cp->line_nr);
249 raw3270_request_add_data(wrq, prolog, 6);
250
251 list_for_each_entry_safe(s, n, &cp->update, update) {
252 if (s != cp->cline)
253 con3270_update_string(cp, s, cp->line_nr);
254 if (raw3270_request_add_data(wrq, s->string,
255 s->len) != 0)
256 break;
257 list_del_init(&s->update);
258 if (s != cp->cline)
259 cp->line_nr++;
260 }
261 if (list_empty(&cp->update))
262 updated |= CON_UPDATE_LIST;
263 }
264 wrq->callback = con3270_write_callback;
265 rc = raw3270_start(&cp->view, wrq);
266 if (rc == 0) {
267 cp->update_flags &= ~updated;
268 if (cp->update_flags)
269 con3270_set_timer(cp, 1);
270 } else {
271 raw3270_request_reset(wrq);
272 xchg(&cp->write, wrq);
273 }
274 spin_unlock_irqrestore(&cp->view.lock, flags);
275}
276
277
278
279
280static void
281con3270_read_tasklet(struct raw3270_request *rrq)
282{
283 static char kreset_data = TW_KR;
284 struct con3270 *cp;
285 unsigned long flags;
286 int nr_up, deactivate;
287
288 cp = (struct con3270 *) rrq->view;
289 spin_lock_irqsave(&cp->view.lock, flags);
290 nr_up = cp->nr_up;
291 deactivate = 0;
292
293 switch (cp->input->string[0]) {
294 case 0x7d:
295 nr_up = 0;
296 break;
297 case 0xf3:
298 deactivate = 1;
299 break;
300 case 0x6d:
301 cp->update_flags = CON_UPDATE_ALL;
302 con3270_set_timer(cp, 1);
303 break;
304 case 0xf7:
305 nr_up += cp->view.rows - 2;
306 if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
307 nr_up = cp->nr_lines - cp->view.rows + 1;
308 if (nr_up < 0)
309 nr_up = 0;
310 }
311 break;
312 case 0xf8:
313 nr_up -= cp->view.rows - 2;
314 if (nr_up < 0)
315 nr_up = 0;
316 break;
317 }
318 if (nr_up != cp->nr_up) {
319 cp->nr_up = nr_up;
320 con3270_rebuild_update(cp);
321 con3270_update_status(cp);
322 con3270_set_timer(cp, 1);
323 }
324 spin_unlock_irqrestore(&cp->view.lock, flags);
325
326
327 raw3270_request_reset(cp->kreset);
328 raw3270_request_set_cmd(cp->kreset, TC_WRITE);
329 raw3270_request_add_data(cp->kreset, &kreset_data, 1);
330 raw3270_start(&cp->view, cp->kreset);
331
332 if (deactivate)
333 raw3270_deactivate_view(&cp->view);
334
335 raw3270_request_reset(rrq);
336 xchg(&cp->read, rrq);
337 raw3270_put_view(&cp->view);
338}
339
340
341
342
343static void
344con3270_read_callback(struct raw3270_request *rq, void *data)
345{
346 raw3270_get_view(rq->view);
347
348 tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
349}
350
351
352
353
354static void
355con3270_issue_read(struct con3270 *cp)
356{
357 struct raw3270_request *rrq;
358 int rc;
359
360 rrq = xchg(&cp->read, 0);
361 if (!rrq)
362
363 return;
364 rrq->callback = con3270_read_callback;
365 rrq->callback_data = cp;
366 raw3270_request_set_cmd(rrq, TC_READMOD);
367 raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
368
369 rc = raw3270_start_irq(&cp->view, rrq);
370 if (rc)
371 raw3270_request_reset(rrq);
372}
373
374
375
376
377static int
378con3270_activate(struct raw3270_view *view)
379{
380 struct con3270 *cp;
381
382 cp = (struct con3270 *) view;
383 cp->update_flags = CON_UPDATE_ALL;
384 con3270_set_timer(cp, 1);
385 return 0;
386}
387
388static void
389con3270_deactivate(struct raw3270_view *view)
390{
391 struct con3270 *cp;
392
393 cp = (struct con3270 *) view;
394 del_timer(&cp->timer);
395}
396
397static int
398con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
399{
400
401 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
402 con3270_issue_read(cp);
403
404 if (rq) {
405 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
406 rq->rc = -EIO;
407 else
408
409 rq->rescnt = irb->scsw.cmd.count;
410 } else if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
411
412 cp->update_flags = CON_UPDATE_ALL;
413 con3270_set_timer(cp, 1);
414 }
415 return RAW3270_IO_DONE;
416}
417
418
419static struct raw3270_fn con3270_fn = {
420 .activate = con3270_activate,
421 .deactivate = con3270_deactivate,
422 .intv = (void *) con3270_irq
423};
424
425static inline void
426con3270_cline_add(struct con3270 *cp)
427{
428 if (!list_empty(&cp->cline->list))
429
430 return;
431 list_add_tail(&cp->cline->list, &cp->lines);
432 cp->nr_lines++;
433 con3270_rebuild_update(cp);
434}
435
436static inline void
437con3270_cline_insert(struct con3270 *cp, unsigned char c)
438{
439 cp->cline->string[cp->cline->len++] =
440 cp->view.ascebc[(c < ' ') ? ' ' : c];
441 if (list_empty(&cp->cline->update)) {
442 list_add_tail(&cp->cline->update, &cp->update);
443 cp->update_flags |= CON_UPDATE_LIST;
444 }
445}
446
447static inline void
448con3270_cline_end(struct con3270 *cp)
449{
450 struct string *s;
451 unsigned int size;
452
453
454 size = (cp->cline->len < cp->view.cols - 5) ?
455 cp->cline->len + 4 : cp->view.cols;
456 s = con3270_alloc_string(cp, size);
457 memcpy(s->string, cp->cline->string, cp->cline->len);
458 if (s->len < cp->view.cols - 5) {
459 s->string[s->len - 4] = TO_RA;
460 s->string[s->len - 1] = 0;
461 } else {
462 while (--size > cp->cline->len)
463 s->string[size] = cp->view.ascebc[' '];
464 }
465
466 list_add(&s->list, &cp->cline->list);
467 list_del_init(&cp->cline->list);
468 if (!list_empty(&cp->cline->update)) {
469 list_add(&s->update, &cp->cline->update);
470 list_del_init(&cp->cline->update);
471 }
472 cp->cline->len = 0;
473}
474
475
476
477
478static void
479con3270_write(struct console *co, const char *str, unsigned int count)
480{
481 struct con3270 *cp;
482 unsigned long flags;
483 unsigned char c;
484
485 cp = condev;
486 spin_lock_irqsave(&cp->view.lock, flags);
487 while (count-- > 0) {
488 c = *str++;
489 if (cp->cline->len == 0)
490 con3270_cline_add(cp);
491 if (c != '\n')
492 con3270_cline_insert(cp, c);
493 if (c == '\n' || cp->cline->len >= cp->view.cols)
494 con3270_cline_end(cp);
495 }
496
497 cp->nr_up = 0;
498 if (cp->view.dev && !timer_pending(&cp->timer))
499 con3270_set_timer(cp, HZ/10);
500 spin_unlock_irqrestore(&cp->view.lock,flags);
501}
502
503static struct tty_driver *
504con3270_device(struct console *c, int *index)
505{
506 *index = c->index;
507 return tty3270_driver;
508}
509
510
511
512
513static void
514con3270_wait_write(struct con3270 *cp)
515{
516 while (!cp->write) {
517 raw3270_wait_cons_dev(cp->view.dev);
518 barrier();
519 }
520}
521
522
523
524
525
526static void
527con3270_flush(void)
528{
529 struct con3270 *cp;
530 unsigned long flags;
531
532 cp = condev;
533 if (!cp->view.dev)
534 return;
535 raw3270_pm_unfreeze(&cp->view);
536 spin_lock_irqsave(&cp->view.lock, flags);
537 con3270_wait_write(cp);
538 cp->nr_up = 0;
539 con3270_rebuild_update(cp);
540 con3270_update_status(cp);
541 while (cp->update_flags != 0) {
542 spin_unlock_irqrestore(&cp->view.lock, flags);
543 con3270_update(cp);
544 spin_lock_irqsave(&cp->view.lock, flags);
545 con3270_wait_write(cp);
546 }
547 spin_unlock_irqrestore(&cp->view.lock, flags);
548}
549
550static int con3270_notify(struct notifier_block *self,
551 unsigned long event, void *data)
552{
553 con3270_flush();
554 return NOTIFY_OK;
555}
556
557static struct notifier_block on_panic_nb = {
558 .notifier_call = con3270_notify,
559 .priority = 0,
560};
561
562static struct notifier_block on_reboot_nb = {
563 .notifier_call = con3270_notify,
564 .priority = 0,
565};
566
567
568
569
570static struct console con3270 = {
571 .name = "tty3270",
572 .write = con3270_write,
573 .device = con3270_device,
574 .flags = CON_PRINTBUFFER,
575};
576
577
578
579
580static int __init
581con3270_init(void)
582{
583 struct ccw_device *cdev;
584 struct raw3270 *rp;
585 void *cbuf;
586 int i;
587
588
589 if (!CONSOLE_IS_3270)
590 return -ENODEV;
591
592
593 if (MACHINE_IS_VM) {
594 cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
595 cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
596 }
597
598 cdev = ccw_device_probe_console();
599 if (IS_ERR(cdev))
600 return -ENODEV;
601 rp = raw3270_setup_console(cdev);
602 if (IS_ERR(rp))
603 return PTR_ERR(rp);
604
605 condev = kzalloc(sizeof(struct con3270), GFP_KERNEL | GFP_DMA);
606 condev->view.dev = rp;
607
608 condev->read = raw3270_request_alloc(0);
609 condev->read->callback = con3270_read_callback;
610 condev->read->callback_data = condev;
611 condev->write = raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE);
612 condev->kreset = raw3270_request_alloc(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 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
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
639console_initcall(con3270_init);
640