2 * drivers/s390/char/con3270.c
3 * IBM/3270 Driver - console view.
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
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>
19 #include <asm/ccwdev.h>
21 #include <asm/cpcmd.h>
22 #include <asm/ebcdic.h>
27 #define CON3270_OUTPUT_BUFFER_SIZE 1024
28 #define CON3270_STRING_PAGES 4
30 static struct raw3270_fn con3270_fn
;
33 * Main 3270 console view data structure.
36 struct raw3270_view view
;
38 struct list_head freemem
; /* list of free memory for strings. */
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
;
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. */
59 static struct con3270
*condev
;
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
67 static void con3270_update(struct con3270
*);
70 * Setup timeout for a device. On timeout trigger an update.
73 con3270_set_timer(struct con3270
*cp
, int expires
)
76 if (timer_pending(&cp
->timer
))
77 del_timer(&cp
->timer
);
80 if (timer_pending(&cp
->timer
)) {
81 if (mod_timer(&cp
->timer
, jiffies
+ expires
))
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
);
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.
96 con3270_update_status(struct con3270
*cp
)
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
;
107 con3270_create_status(struct con3270
*cp
)
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
};
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);
128 * Set output offsets to 3270 datastream fragment of a console string.
131 con3270_update_string(struct con3270
*cp
, struct string
*s
, int nr
)
133 if (s
->len
>= cp
->view
.cols
- 5)
135 raw3270_buffer_address(cp
->view
.dev
, s
->string
+ s
->len
- 3,
136 cp
->view
.cols
* (nr
+ 1));
140 * Rebuild update list to print all lines.
143 con3270_rebuild_update(struct con3270
*cp
)
145 struct string
*s
, *n
;
149 * Throw away update list and create a new one,
150 * containing all lines that will fit on the screen.
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
);
162 cp
->update_flags
|= CON_UPDATE_LIST
;
166 * Alloc string for size bytes. Free strings from history if necessary.
168 static struct string
*
169 con3270_alloc_string(struct con3270
*cp
, size_t size
)
171 struct string
*s
, *n
;
173 s
= alloc_string(&cp
->freemem
, size
);
176 list_for_each_entry_safe(s
, n
, &cp
->lines
, list
) {
178 if (!list_empty(&s
->update
))
179 list_del(&s
->update
);
181 if (free_string(&cp
->freemem
, s
) >= size
)
184 s
= alloc_string(&cp
->freemem
, size
);
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
);
195 * Write completion callback.
198 con3270_write_callback(struct raw3270_request
*rq
, void *data
)
200 raw3270_request_reset(rq
);
201 xchg(&((struct con3270
*) rq
->view
)->write
, rq
);
205 * Update console display.
208 con3270_update(struct con3270
*cp
)
210 struct raw3270_request
*wrq
;
213 unsigned long updated
;
214 struct string
*s
, *n
;
217 wrq
= xchg(&cp
->write
, 0);
219 con3270_set_timer(cp
, 1);
223 spin_lock_irqsave(&cp
->view
.lock
, flags
);
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
;
230 raw3270_request_set_cmd(wrq
, TC_WRITE
);
233 raw3270_request_add_data(wrq
, &wcc
, 1);
236 * Update status line.
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
;
243 if (cp
->update_flags
& CON_UPDATE_LIST
) {
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
) {
254 con3270_update_string(cp
, s
, cp
->line_nr
);
255 if (raw3270_request_add_data(wrq
, s
->string
,
258 list_del_init(&s
->update
);
262 if (list_empty(&cp
->update
))
263 updated
|= CON_UPDATE_LIST
;
265 wrq
->callback
= con3270_write_callback
;
266 rc
= raw3270_start(&cp
->view
, wrq
);
268 cp
->update_flags
&= ~updated
;
269 if (cp
->update_flags
)
270 con3270_set_timer(cp
, 1);
272 raw3270_request_reset(wrq
);
273 xchg(&cp
->write
, wrq
);
275 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
282 con3270_read_tasklet(struct raw3270_request
*rrq
)
284 static char kreset_data
= TW_KR
;
287 int nr_up
, deactivate
;
289 cp
= (struct con3270
*) rrq
->view
;
290 spin_lock_irqsave(&cp
->view
.lock
, flags
);
293 /* Check aid byte. */
294 switch (cp
->input
->string
[0]) {
295 case 0x7d: /* enter: jump to bottom. */
298 case 0xf3: /* PF3: deactivate the console view. */
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);
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;
314 case 0xf8: /* PF8: do a page down in the console log. */
315 nr_up
-= cp
->view
.rows
- 2;
320 if (nr_up
!= cp
->nr_up
) {
322 con3270_rebuild_update(cp
);
323 con3270_update_status(cp
);
324 con3270_set_timer(cp
, 1);
326 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
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
);
335 raw3270_deactivate_view(&cp
->view
);
337 raw3270_request_reset(rrq
);
338 xchg(&cp
->read
, rrq
);
339 raw3270_put_view(&cp
->view
);
343 * Read request completion callback.
346 con3270_read_callback(struct raw3270_request
*rq
, void *data
)
348 raw3270_get_view(rq
->view
);
349 /* Schedule tasklet to pass input to tty. */
350 tasklet_schedule(&((struct con3270
*) rq
->view
)->readlet
);
354 * Issue a read request. Called only from interrupt function.
357 con3270_issue_read(struct con3270
*cp
)
359 struct raw3270_request
*rrq
;
362 rrq
= xchg(&cp
->read
, 0);
364 /* Read already scheduled. */
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
);
373 raw3270_request_reset(rrq
);
377 * Switch to the console view.
380 con3270_activate(struct raw3270_view
*view
)
385 cp
= (struct con3270
*) view
;
386 spin_lock_irqsave(&cp
->view
.lock
, flags
);
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
);
397 con3270_deactivate(struct raw3270_view
*view
)
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
);
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
.dstat
& DEV_STAT_ATTENTION
)
413 con3270_issue_read(cp
);
416 if (irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
)
419 /* Normal end. Copy residual count. */
420 rq
->rescnt
= irb
->scsw
.count
;
422 return RAW3270_IO_DONE
;
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
433 con3270_cline_add(struct con3270
*cp
)
435 if (!list_empty(&cp
->cline
->list
))
438 list_add_tail(&cp
->cline
->list
, &cp
->lines
);
440 con3270_rebuild_update(cp
);
444 con3270_cline_insert(struct con3270
*cp
, unsigned char c
)
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
;
455 con3270_cline_end(struct con3270
*cp
)
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;
469 while (--size
> cp
->cline
->len
)
470 s
->string
[size
] = cp
->view
.ascebc
[' '];
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
);
483 * Write a string to the 3270 console
486 con3270_write(struct console
*co
, const char *str
, unsigned int count
)
494 raw3270_activate_view(&cp
->view
);
495 spin_lock_irqsave(&cp
->view
.lock
, flags
);
496 while (count
-- > 0) {
498 if (cp
->cline
->len
== 0)
499 con3270_cline_add(cp
);
501 con3270_cline_insert(cp
, c
);
502 if (c
== '\n' || cp
->cline
->len
>= cp
->view
.cols
)
503 con3270_cline_end(cp
);
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
);
511 extern struct tty_driver
*tty3270_driver
;
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 console_unblank before the system enters a
534 * disabled, endless loop.
537 con3270_unblank(void)
545 spin_lock_irqsave(&cp
->view
.lock
, flags
);
546 con3270_wait_write(cp
);
548 con3270_rebuild_update(cp
);
549 con3270_update_status(cp
);
550 while (cp
->update_flags
!= 0) {
551 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
553 spin_lock_irqsave(&cp
->view
.lock
, flags
);
554 con3270_wait_write(cp
);
556 spin_unlock_irqrestore(&cp
->view
.lock
, flags
);
560 con3270_consetup(struct console
*co
, char *options
)
566 * The console structure for the 3270 console
568 static struct console con3270
= {
570 .write
= con3270_write
,
571 .device
= con3270_device
,
572 .unblank
= con3270_unblank
,
573 .setup
= con3270_consetup
,
574 .flags
= CON_PRINTBUFFER
,
578 * 3270 console initialization code called from console_init().
579 * NOTE: This is called before kmalloc is available.
584 struct ccw_device
*cdev
;
589 /* Check if 3270 is to be the console */
590 if (!CONSOLE_IS_3270
)
593 /* Set the console mode for VM */
595 cpcmd("TERM CONMODE 3270", 0, 0);
596 cpcmd("TERM AUTOCR OFF", 0, 0);
599 cdev
= ccw_device_probe_console();
602 rp
= raw3270_setup_console(cdev
);
606 condev
= (struct con3270
*) alloc_bootmem_low(sizeof(struct con3270
));
607 memset(condev
, 0, sizeof(struct con3270
));
608 condev
->view
.dev
= rp
;
610 condev
->read
= raw3270_request_alloc_bootmem(0);
611 condev
->read
->callback
= con3270_read_callback
;
612 condev
->read
->callback_data
= condev
;
614 raw3270_request_alloc_bootmem(CON3270_OUTPUT_BUFFER_SIZE
);
615 condev
->kreset
= raw3270_request_alloc_bootmem(1);
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
);
624 raw3270_add_view(&condev
->view
, &con3270_fn
, 0);
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
);
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
);
639 console_initcall(con3270_init
);