2 * IBM/3270 Driver - console view.
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
6 * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * Copyright IBM Corp. 2003, 2009
10 #include <linux/module.h>
11 #include <linux/console.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/reboot.h>
20 #include <asm/ccwdev.h>
22 #include <asm/cpcmd.h>
23 #include <asm/ebcdic.h>
29 #define CON3270_OUTPUT_BUFFER_SIZE 1024
30 #define CON3270_STRING_PAGES 4
32 static struct raw3270_fn con3270_fn
;
34 static bool auto_update
= 1;
35 module_param(auto_update
, bool, 0);
38 * Main 3270 console view data structure.
41 struct raw3270_view view
;
42 struct list_head freemem
; /* list of free memory for strings. */
45 struct list_head lines
; /* list of lines. */
46 struct list_head update
; /* list of lines to update. */
47 int line_nr
; /* line number for next update. */
48 int nr_lines
; /* # lines in list. */
49 int nr_up
; /* # lines up in history. */
50 unsigned long update_flags
; /* Update indication bits. */
51 struct string
*cline
; /* current output line. */
52 struct string
*status
; /* last line of display. */
53 struct raw3270_request
*write
; /* single write request. */
54 struct timer_list timer
;
57 struct string
*input
; /* input string for read request. */
58 struct raw3270_request
*read
; /* single read request. */
59 struct raw3270_request
*kreset
; /* single keyboard reset request. */
60 struct tasklet_struct readlet
; /* tasklet to issue read request. */
63 static struct con3270
*condev
;
65 /* con3270->update_flags. See con3270_update for details. */
66 #define CON_UPDATE_ERASE 1 /* Use EWRITEA instead of WRITE. */
67 #define CON_UPDATE_LIST 2 /* Update lines in tty3270->update. */
68 #define CON_UPDATE_STATUS 4 /* Update status line. */
69 #define CON_UPDATE_ALL 8 /* Recreate screen. */
71 static void con3270_update(struct con3270
*);
74 * Setup timeout for a device. On timeout trigger an update.
76 static void con3270_set_timer(struct con3270
*cp
, int expires
)
79 del_timer(&cp
->timer
);
81 mod_timer(&cp
->timer
, jiffies
+ expires
);
85 * The status line is the last line of the screen. It shows the string
86 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
87 * in the lower right corner of the screen.
90 con3270_update_status(struct con3270
*cp
)
94 str
= (cp
->nr_up
!= 0) ? "History" : "Running";
95 memcpy(cp
->status
->string
+ 24, str
, 7);
96 codepage_convert(cp
->view
.ascebc
, cp
->status
->string
+ 24, 7);
97 cp
->update_flags
|= CON_UPDATE_STATUS
;
101 con3270_create_status(struct con3270
*cp
)
103 static const unsigned char blueprint
[] =
104 { TO_SBA
, 0, 0, TO_SF
,TF_LOG
,TO_SA
,TAT_COLOR
, TAC_GREEN
,
105 'c','o','n','s','o','l','e',' ','v','i','e','w',
106 TO_RA
,0,0,0,'R','u','n','n','i','n','g',TO_SF
,TF_LOG
};
108 cp
->status
= alloc_string(&cp
->freemem
, sizeof(blueprint
));
109 /* Copy blueprint to status line */
110 memcpy(cp
->status
->string
, blueprint
, sizeof(blueprint
));
111 /* Set TO_RA addresses. */
112 raw3270_buffer_address(cp
->view
.dev
, cp
->status
->string
+ 1,
113 cp
->view
.cols
* (cp
->view
.rows
- 1));
114 raw3270_buffer_address(cp
->view
.dev
, cp
->status
->string
+ 21,
115 cp
->view
.cols
* cp
->view
.rows
- 8);
116 /* Convert strings to ebcdic. */
117 codepage_convert(cp
->view
.ascebc
, cp
->status
->string
+ 8, 12);
118 codepage_convert(cp
->view
.ascebc
, cp
->status
->string
+ 24, 7);
122 * Set output offsets to 3270 datastream fragment of a console string.
125 con3270_update_string(struct con3270
*cp
, struct string
*s
, int nr
)
128 /* This indicates a bug, but printing a warning would
129 * cause a deadlock. */
132 if (s
->string
[s
->len
- 4] != TO_RA
)
134 raw3270_buffer_address(cp
->view
.dev
, s
->string
+ s
->len
- 3,
135 cp
->view
.cols
* (nr
+ 1));
139 * Rebuild update list to print all lines.
142 con3270_rebuild_update(struct con3270
*cp
)
144 struct string
*s
, *n
;
148 * Throw away update list and create a new one,
149 * containing all lines that will fit on the screen.
151 list_for_each_entry_safe(s
, n
, &cp
->update
, update
)
152 list_del_init(&s
->update
);
153 nr
= cp
->view
.rows
- 2 + cp
->nr_up
;
154 list_for_each_entry_reverse(s
, &cp
->lines
, list
) {
155 if (nr
< cp
->view
.rows
- 1)
156 list_add(&s
->update
, &cp
->update
);
161 cp
->update_flags
|= CON_UPDATE_LIST
;
165 * Alloc string for size bytes. Free strings from history if necessary.
167 static struct string
*
168 con3270_alloc_string(struct con3270
*cp
, size_t size
)
170 struct string
*s
, *n
;
172 s
= alloc_string(&cp
->freemem
, size
);
175 list_for_each_entry_safe(s
, n
, &cp
->lines
, list
) {
177 if (!list_empty(&s
->update
))
178 list_del(&s
->update
);
180 if (free_string(&cp
->freemem
, s
) >= size
)
183 s
= alloc_string(&cp
->freemem
, size
);
185 if (cp
->nr_up
!= 0 && cp
->nr_up
+ cp
->view
.rows
> cp
->nr_lines
) {
186 cp
->nr_up
= cp
->nr_lines
- cp
->view
.rows
+ 1;
187 con3270_rebuild_update(cp
);
188 con3270_update_status(cp
);
194 * Write completion callback.
197 con3270_write_callback(struct raw3270_request
*rq
, void *data
)
199 raw3270_request_reset(rq
);
200 xchg(&((struct con3270
*) rq
->view
)->write
, rq
);
204 * Update console display.
207 con3270_update(struct con3270
*cp
)
209 struct raw3270_request
*wrq
;
212 unsigned long updated
;
213 struct string
*s
, *n
;
216 if (!auto_update
&& !raw3270_view_active(&cp
->view
))
219 raw3270_activate_view(&cp
->view
);
221 wrq
= xchg(&cp
->write
, 0);
223 con3270_set_timer(cp
, 1);
227 spin_lock_irqsave(&cp
->view
.lock
, flags
);
229 if (cp
->update_flags
& CON_UPDATE_ALL
) {
230 con3270_rebuild_update(cp
);
231 con3270_update_status(cp
);
232 cp
->update_flags
= CON_UPDATE_ERASE
| CON_UPDATE_LIST
|
235 if (cp
->update_flags
& CON_UPDATE_ERASE
) {
236 /* Use erase write alternate to initialize display. */
237 raw3270_request_set_cmd(wrq
, TC_EWRITEA
);
238 updated
|= CON_UPDATE_ERASE
;
240 raw3270_request_set_cmd(wrq
, TC_WRITE
);
243 raw3270_request_add_data(wrq
, &wcc
, 1);
246 * Update status line.
248 if (cp
->update_flags
& CON_UPDATE_STATUS
)
249 if (raw3270_request_add_data(wrq
, cp
->status
->string
,
250 cp
->status
->len
) == 0)
251 updated
|= CON_UPDATE_STATUS
;
253 if (cp
->update_flags
& CON_UPDATE_LIST
) {
256 prolog
[4] = TAT_COLOR
;
257 prolog
[5] = TAC_TURQ
;
258 raw3270_buffer_address(cp
->view
.dev
, prolog
+ 1,
259 cp
->view
.cols
* cp
->line_nr
);
260 raw3270_request_add_data(wrq
, prolog
, 6);
261 /* Write strings in the update list to the screen. */
262 list_for_each_entry_safe(s
, n
, &cp
->update
, update
) {
264 con3270_update_string(cp
, s
, cp
->line_nr
);
265 if (raw3270_request_add_data(wrq
, s
->string
,
268 list_del_init(&s
->update
);
272 if (list_empty(&cp
->update
))
273 updated
|= CON_UPDATE_LIST
;
275 wrq
->callback
= con3270_write_callback
;
276 rc
= raw3270_start(&cp
->view
, wrq
);
278 cp
->update_flags
&= ~updated
;
279 if (cp
->update_flags
)
280 con3270_set_timer(cp
, 1);
282 raw3270_request_reset(wrq
);
283 xchg(&cp
->write
, wrq
);
285 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
292 con3270_read_tasklet(struct raw3270_request
*rrq
)
294 static char kreset_data
= TW_KR
;
297 int nr_up
, deactivate
;
299 cp
= (struct con3270
*) rrq
->view
;
300 spin_lock_irqsave(&cp
->view
.lock
, flags
);
303 /* Check aid byte. */
304 switch (cp
->input
->string
[0]) {
305 case 0x7d: /* enter: jump to bottom. */
308 case 0xf3: /* PF3: deactivate the console view. */
311 case 0x6d: /* clear: start from scratch. */
312 cp
->update_flags
= CON_UPDATE_ALL
;
313 con3270_set_timer(cp
, 1);
315 case 0xf7: /* PF7: do a page up in the console log. */
316 nr_up
+= cp
->view
.rows
- 2;
317 if (nr_up
+ cp
->view
.rows
- 1 > cp
->nr_lines
) {
318 nr_up
= cp
->nr_lines
- cp
->view
.rows
+ 1;
323 case 0xf8: /* PF8: do a page down in the console log. */
324 nr_up
-= cp
->view
.rows
- 2;
329 if (nr_up
!= cp
->nr_up
) {
331 con3270_rebuild_update(cp
);
332 con3270_update_status(cp
);
333 con3270_set_timer(cp
, 1);
335 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
337 /* Start keyboard reset command. */
338 raw3270_request_reset(cp
->kreset
);
339 raw3270_request_set_cmd(cp
->kreset
, TC_WRITE
);
340 raw3270_request_add_data(cp
->kreset
, &kreset_data
, 1);
341 raw3270_start(&cp
->view
, cp
->kreset
);
344 raw3270_deactivate_view(&cp
->view
);
346 raw3270_request_reset(rrq
);
347 xchg(&cp
->read
, rrq
);
348 raw3270_put_view(&cp
->view
);
352 * Read request completion callback.
355 con3270_read_callback(struct raw3270_request
*rq
, void *data
)
357 raw3270_get_view(rq
->view
);
358 /* Schedule tasklet to pass input to tty. */
359 tasklet_schedule(&((struct con3270
*) rq
->view
)->readlet
);
363 * Issue a read request. Called only from interrupt function.
366 con3270_issue_read(struct con3270
*cp
)
368 struct raw3270_request
*rrq
;
371 rrq
= xchg(&cp
->read
, 0);
373 /* Read already scheduled. */
375 rrq
->callback
= con3270_read_callback
;
376 rrq
->callback_data
= cp
;
377 raw3270_request_set_cmd(rrq
, TC_READMOD
);
378 raw3270_request_set_data(rrq
, cp
->input
->string
, cp
->input
->len
);
379 /* Issue the read modified request. */
380 rc
= raw3270_start_irq(&cp
->view
, rrq
);
382 raw3270_request_reset(rrq
);
386 * Switch to the console view.
389 con3270_activate(struct raw3270_view
*view
)
393 cp
= (struct con3270
*) view
;
394 cp
->update_flags
= CON_UPDATE_ALL
;
395 con3270_set_timer(cp
, 1);
400 con3270_deactivate(struct raw3270_view
*view
)
404 cp
= (struct con3270
*) view
;
405 del_timer(&cp
->timer
);
409 con3270_irq(struct con3270
*cp
, struct raw3270_request
*rq
, struct irb
*irb
)
411 /* Handle ATTN. Schedule tasklet to read aid. */
412 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_ATTENTION
)
413 con3270_issue_read(cp
);
416 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
)
419 /* Normal end. Copy residual count. */
420 rq
->rescnt
= irb
->scsw
.cmd
.count
;
421 } else if (irb
->scsw
.cmd
.dstat
& DEV_STAT_DEV_END
) {
422 /* Interrupt without an outstanding request -> update all */
423 cp
->update_flags
= CON_UPDATE_ALL
;
424 con3270_set_timer(cp
, 1);
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
436 con3270_cline_add(struct con3270
*cp
)
438 if (!list_empty(&cp
->cline
->list
))
441 list_add_tail(&cp
->cline
->list
, &cp
->lines
);
443 con3270_rebuild_update(cp
);
447 con3270_cline_insert(struct con3270
*cp
, unsigned char c
)
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
;
458 con3270_cline_end(struct con3270
*cp
)
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 (cp
->cline
->len
< cp
->view
.cols
- 5) {
469 s
->string
[s
->len
- 4] = TO_RA
;
470 s
->string
[s
->len
- 1] = 0;
472 while (--size
>= cp
->cline
->len
)
473 s
->string
[size
] = cp
->view
.ascebc
[' '];
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
);
486 * Write a string to the 3270 console
489 con3270_write(struct console
*co
, const char *str
, unsigned int count
)
496 spin_lock_irqsave(&cp
->view
.lock
, flags
);
497 while (count
-- > 0) {
499 if (cp
->cline
->len
== 0)
500 con3270_cline_add(cp
);
502 con3270_cline_insert(cp
, c
);
503 if (c
== '\n' || cp
->cline
->len
>= cp
->view
.cols
)
504 con3270_cline_end(cp
);
506 /* Setup timer to output current console buffer after 1/10 second */
508 if (cp
->view
.dev
&& !timer_pending(&cp
->timer
))
509 con3270_set_timer(cp
, HZ
/10);
510 spin_unlock_irqrestore(&cp
->view
.lock
,flags
);
513 static struct tty_driver
*
514 con3270_device(struct console
*c
, int *index
)
517 return tty3270_driver
;
521 * Wait for end of write request.
524 con3270_wait_write(struct con3270
*cp
)
527 raw3270_wait_cons_dev(cp
->view
.dev
);
533 * panic() calls con3270_flush through a panic_notifier
534 * before the system enters a disabled, endless loop.
545 raw3270_pm_unfreeze(&cp
->view
);
546 raw3270_activate_view(&cp
->view
);
547 spin_lock_irqsave(&cp
->view
.lock
, flags
);
548 con3270_wait_write(cp
);
550 con3270_rebuild_update(cp
);
551 con3270_update_status(cp
);
552 while (cp
->update_flags
!= 0) {
553 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
555 spin_lock_irqsave(&cp
->view
.lock
, flags
);
556 con3270_wait_write(cp
);
558 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
561 static int con3270_notify(struct notifier_block
*self
,
562 unsigned long event
, void *data
)
568 static struct notifier_block on_panic_nb
= {
569 .notifier_call
= con3270_notify
,
573 static struct notifier_block on_reboot_nb
= {
574 .notifier_call
= con3270_notify
,
579 * The console structure for the 3270 console
581 static struct console con3270
= {
583 .write
= con3270_write
,
584 .device
= con3270_device
,
585 .flags
= CON_PRINTBUFFER
,
589 * 3270 console initialization code called from console_init().
598 /* Check if 3270 is to be the console */
599 if (!CONSOLE_IS_3270
)
602 /* Set the console mode for VM */
604 cpcmd("TERM CONMODE 3270", NULL
, 0, NULL
);
605 cpcmd("TERM AUTOCR OFF", NULL
, 0, NULL
);
608 rp
= raw3270_setup_console();
612 condev
= kzalloc(sizeof(struct con3270
), GFP_KERNEL
| GFP_DMA
);
615 condev
->view
.dev
= rp
;
617 condev
->read
= raw3270_request_alloc(0);
618 condev
->read
->callback
= con3270_read_callback
;
619 condev
->read
->callback_data
= condev
;
620 condev
->write
= raw3270_request_alloc(CON3270_OUTPUT_BUFFER_SIZE
);
621 condev
->kreset
= raw3270_request_alloc(1);
623 INIT_LIST_HEAD(&condev
->lines
);
624 INIT_LIST_HEAD(&condev
->update
);
625 setup_timer(&condev
->timer
, (void (*)(unsigned long)) con3270_update
,
626 (unsigned long) condev
);
627 tasklet_init(&condev
->readlet
,
628 (void (*)(unsigned long)) con3270_read_tasklet
,
629 (unsigned long) condev
->read
);
631 raw3270_add_view(&condev
->view
, &con3270_fn
, 1);
633 INIT_LIST_HEAD(&condev
->freemem
);
634 for (i
= 0; i
< CON3270_STRING_PAGES
; i
++) {
635 cbuf
= (void *) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
636 add_string_memory(&condev
->freemem
, cbuf
, PAGE_SIZE
);
638 condev
->cline
= alloc_string(&condev
->freemem
, condev
->view
.cols
);
639 condev
->cline
->len
= 0;
640 con3270_create_status(condev
);
641 condev
->input
= alloc_string(&condev
->freemem
, 80);
642 atomic_notifier_chain_register(&panic_notifier_list
, &on_panic_nb
);
643 register_reboot_notifier(&on_reboot_nb
);
644 register_console(&con3270
);
648 console_initcall(con3270_init
);