1 // SPDX-License-Identifier: GPL-2.0
3 * IBM/3270 Driver - core functions.
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 IBM Corp. 2003, 2009
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/wait.h>
20 #include <asm/ccwdev.h>
22 #include <asm/ebcdic.h>
27 #include <linux/major.h>
28 #include <linux/kdev_t.h>
29 #include <linux/device.h>
30 #include <linux/mutex.h>
32 struct class *class3270
;
34 /* The main 3270 data structure. */
36 struct list_head list
;
37 struct ccw_device
*cdev
;
40 short model
, rows
, cols
;
44 struct list_head req_queue
; /* Request queue. */
45 struct list_head view_list
; /* List of available views. */
46 struct raw3270_view
*view
; /* Active view. */
48 struct timer_list timer
; /* Device timer. */
50 unsigned char *ascebc
; /* ascii -> ebcdic table */
52 struct raw3270_view init_view
;
53 struct raw3270_request init_reset
;
54 struct raw3270_request init_readpart
;
55 struct raw3270_request init_readmod
;
56 unsigned char init_data
[256];
60 #define RAW3270_STATE_INIT 0 /* Initial state */
61 #define RAW3270_STATE_RESET 1 /* Reset command is pending */
62 #define RAW3270_STATE_W4ATTN 2 /* Wait for attention interrupt */
63 #define RAW3270_STATE_READMOD 3 /* Read partition is pending */
64 #define RAW3270_STATE_READY 4 /* Device is usable by views */
67 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
68 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
69 #define RAW3270_FLAGS_CONSOLE 2 /* Device is the console. */
70 #define RAW3270_FLAGS_FROZEN 3 /* set if 3270 is frozen for suspend */
72 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
73 static DEFINE_MUTEX(raw3270_mutex
);
75 /* List of 3270 devices. */
76 static LIST_HEAD(raw3270_devices
);
79 * Flag to indicate if the driver has been registered. Some operations
80 * like waiting for the end of i/o need to be done differently as long
81 * as the kernel is still starting up (console support).
83 static int raw3270_registered
;
85 /* Module parameters */
86 static bool tubxcorrect
;
87 module_param(tubxcorrect
, bool, 0);
90 * Wait queue for device init/delete, view delete.
92 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue
);
94 static void __raw3270_disconnect(struct raw3270
*rp
);
97 * Encode array for 12 bit 3270 addresses.
99 static unsigned char raw3270_ebcgraf
[64] = {
100 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
101 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
102 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
103 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
104 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
105 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
106 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
107 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
110 static inline int raw3270_state_ready(struct raw3270
*rp
)
112 return rp
->state
== RAW3270_STATE_READY
;
115 static inline int raw3270_state_final(struct raw3270
*rp
)
117 return rp
->state
== RAW3270_STATE_INIT
||
118 rp
->state
== RAW3270_STATE_READY
;
122 raw3270_buffer_address(struct raw3270
*rp
, char *cp
, unsigned short addr
)
124 if (test_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
)) {
125 cp
[0] = (addr
>> 8) & 0x3f;
128 cp
[0] = raw3270_ebcgraf
[(addr
>> 6) & 0x3f];
129 cp
[1] = raw3270_ebcgraf
[addr
& 0x3f];
134 * Allocate a new 3270 ccw request
136 struct raw3270_request
*
137 raw3270_request_alloc(size_t size
)
139 struct raw3270_request
*rq
;
141 /* Allocate request structure */
142 rq
= kzalloc(sizeof(struct raw3270_request
), GFP_KERNEL
| GFP_DMA
);
144 return ERR_PTR(-ENOMEM
);
146 /* alloc output buffer. */
148 rq
->buffer
= kmalloc(size
, GFP_KERNEL
| GFP_DMA
);
151 return ERR_PTR(-ENOMEM
);
155 INIT_LIST_HEAD(&rq
->list
);
160 rq
->ccw
.cda
= __pa(rq
->buffer
);
161 rq
->ccw
.flags
= CCW_FLAG_SLI
;
167 * Free 3270 ccw request
170 raw3270_request_free (struct raw3270_request
*rq
)
177 * Reset request to initial state.
180 raw3270_request_reset(struct raw3270_request
*rq
)
182 BUG_ON(!list_empty(&rq
->list
));
183 rq
->ccw
.cmd_code
= 0;
185 rq
->ccw
.cda
= __pa(rq
->buffer
);
186 rq
->ccw
.flags
= CCW_FLAG_SLI
;
192 * Set command code to ccw of a request.
195 raw3270_request_set_cmd(struct raw3270_request
*rq
, u8 cmd
)
197 rq
->ccw
.cmd_code
= cmd
;
201 * Add data fragment to output buffer.
204 raw3270_request_add_data(struct raw3270_request
*rq
, void *data
, size_t size
)
206 if (size
+ rq
->ccw
.count
> rq
->size
)
208 memcpy(rq
->buffer
+ rq
->ccw
.count
, data
, size
);
209 rq
->ccw
.count
+= size
;
214 * Set address/length pair to ccw of a request.
217 raw3270_request_set_data(struct raw3270_request
*rq
, void *data
, size_t size
)
219 rq
->ccw
.cda
= __pa(data
);
220 rq
->ccw
.count
= size
;
224 * Set idal buffer to ccw of a request.
227 raw3270_request_set_idal(struct raw3270_request
*rq
, struct idal_buffer
*ib
)
229 rq
->ccw
.cda
= __pa(ib
->data
);
230 rq
->ccw
.count
= ib
->size
;
231 rq
->ccw
.flags
|= CCW_FLAG_IDA
;
235 * Add the request to the request queue, try to start it if the
236 * 3270 device is idle. Return without waiting for end of i/o.
239 __raw3270_start(struct raw3270
*rp
, struct raw3270_view
*view
,
240 struct raw3270_request
*rq
)
243 raw3270_get_view(view
);
244 if (list_empty(&rp
->req_queue
) &&
245 !test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
)) {
246 /* No other requests are on the queue. Start this one. */
247 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
248 (unsigned long) rq
, 0, 0);
250 raw3270_put_view(view
);
254 list_add_tail(&rq
->list
, &rp
->req_queue
);
259 raw3270_view_active(struct raw3270_view
*view
)
261 struct raw3270
*rp
= view
->dev
;
263 return rp
&& rp
->view
== view
&&
264 !test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
);
268 raw3270_start(struct raw3270_view
*view
, struct raw3270_request
*rq
)
274 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
276 if (!rp
|| rp
->view
!= view
||
277 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
279 else if (!raw3270_state_ready(rp
))
282 rc
= __raw3270_start(rp
, view
, rq
);
283 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
288 raw3270_start_locked(struct raw3270_view
*view
, struct raw3270_request
*rq
)
294 if (!rp
|| rp
->view
!= view
||
295 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
297 else if (!raw3270_state_ready(rp
))
300 rc
= __raw3270_start(rp
, view
, rq
);
305 raw3270_start_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
)
311 raw3270_get_view(view
);
312 list_add_tail(&rq
->list
, &rp
->req_queue
);
317 * 3270 interrupt routine, called from the ccw_device layer
320 raw3270_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
323 struct raw3270_view
*view
;
324 struct raw3270_request
*rq
;
326 rp
= dev_get_drvdata(&cdev
->dev
);
329 rq
= (struct raw3270_request
*) intparm
;
330 view
= rq
? rq
->view
: rp
->view
;
333 /* Handle CE-DE-UE and subsequent UDE */
334 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_DEV_END
)
335 clear_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
336 if (irb
->scsw
.cmd
.dstat
== (DEV_STAT_CHN_END
|
338 DEV_STAT_UNIT_EXCEP
))
339 set_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
340 /* Handle disconnected devices */
341 if ((irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
) &&
342 (irb
->ecw
[0] & SNS0_INTERVENTION_REQ
)) {
343 set_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
344 if (rp
->state
> RAW3270_STATE_RESET
)
345 __raw3270_disconnect(rp
);
347 /* Call interrupt handler of the view */
349 view
->fn
->intv(view
, rq
, irb
);
352 if (test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
))
353 /* Device busy, do not start I/O */
356 if (rq
&& !list_empty(&rq
->list
)) {
357 /* The request completed, remove from queue and do callback. */
358 list_del_init(&rq
->list
);
360 rq
->callback(rq
, rq
->callback_data
);
361 /* Do put_device for get_device in raw3270_start. */
362 raw3270_put_view(view
);
366 * Try to start each request on request queue until one is
367 * started successful.
369 while (!list_empty(&rp
->req_queue
)) {
370 rq
= list_entry(rp
->req_queue
.next
,struct raw3270_request
,list
);
371 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
372 (unsigned long) rq
, 0, 0);
375 /* Start failed. Remove request and do callback. */
376 list_del_init(&rq
->list
);
378 rq
->callback(rq
, rq
->callback_data
);
379 /* Do put_device for get_device in raw3270_start. */
380 raw3270_put_view(view
);
385 * To determine the size of the 3270 device we need to do:
386 * 1) send a 'read partition' data stream to the device
387 * 2) wait for the attn interrupt that precedes the query reply
388 * 3) do a read modified to get the query reply
389 * To make things worse we have to cope with intervention
390 * required (3270 device switched to 'stand-by') and command
391 * rejects (old devices that can't do 'read partition').
393 struct raw3270_ua
{ /* Query Reply structure for Usable Area */
394 struct { /* Usable Area Query Reply Base */
395 short l
; /* Length of this structured field */
396 char sfid
; /* 0x81 if Query Reply */
397 char qcode
; /* 0x81 if Usable Area */
400 short w
; /* Width of usable area */
401 short h
; /* Heigth of usavle area */
402 char units
; /* 0x00:in; 0x01:mm */
407 short buffsz
; /* Character buffer size, bytes */
412 } __attribute__ ((packed
)) uab
;
413 struct { /* Alternate Usable Area Self-Defining Parameter */
414 char l
; /* Length of this Self-Defining Parm */
415 char sdpid
; /* 0x02 if Alternate Usable Area */
417 char auaid
; /* 0x01 is Id for the A U A */
418 short wauai
; /* Width of AUAi */
419 short hauai
; /* Height of AUAi */
420 char auaunits
; /* 0x00:in, 0x01:mm */
425 } __attribute__ ((packed
)) aua
;
426 } __attribute__ ((packed
));
429 raw3270_size_device_vm(struct raw3270
*rp
)
432 struct ccw_dev_id dev_id
;
433 struct diag210 diag_data
;
435 ccw_device_get_id(rp
->cdev
, &dev_id
);
436 diag_data
.vrdcdvno
= dev_id
.devno
;
437 diag_data
.vrdclen
= sizeof(struct diag210
);
438 rc
= diag210(&diag_data
);
439 model
= diag_data
.vrdccrmd
;
440 /* Use default model 2 if the size could not be detected */
441 if (rc
|| model
< 2 || model
> 5)
468 raw3270_size_device(struct raw3270
*rp
)
470 struct raw3270_ua
*uap
;
472 /* Got a Query Reply */
473 uap
= (struct raw3270_ua
*) (rp
->init_data
+ 1);
474 /* Paranoia check. */
475 if (rp
->init_readmod
.rc
|| rp
->init_data
[0] != 0x88 ||
476 uap
->uab
.qcode
!= 0x81) {
477 /* Couldn't detect size. Use default model 2. */
483 /* Copy rows/columns of default Usable Area */
484 rp
->rows
= uap
->uab
.h
;
485 rp
->cols
= uap
->uab
.w
;
486 /* Check for 14 bit addressing */
487 if ((uap
->uab
.flags0
& 0x0d) == 0x01)
488 set_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
);
489 /* Check for Alternate Usable Area */
490 if (uap
->uab
.l
== sizeof(struct raw3270_ua
) &&
491 uap
->aua
.sdpid
== 0x02) {
492 rp
->rows
= uap
->aua
.hauai
;
493 rp
->cols
= uap
->aua
.wauai
;
495 /* Try to find a model. */
497 if (rp
->rows
== 24 && rp
->cols
== 80)
499 if (rp
->rows
== 32 && rp
->cols
== 80)
501 if (rp
->rows
== 43 && rp
->cols
== 80)
503 if (rp
->rows
== 27 && rp
->cols
== 132)
508 raw3270_size_device_done(struct raw3270
*rp
)
510 struct raw3270_view
*view
;
513 rp
->state
= RAW3270_STATE_READY
;
514 /* Notify views about new size */
515 list_for_each_entry(view
, &rp
->view_list
, list
)
516 if (view
->fn
->resize
)
517 view
->fn
->resize(view
, rp
->model
, rp
->rows
, rp
->cols
);
518 /* Setup processing done, now activate a view */
519 list_for_each_entry(view
, &rp
->view_list
, list
) {
521 if (view
->fn
->activate(view
) == 0)
528 raw3270_read_modified_cb(struct raw3270_request
*rq
, void *data
)
530 struct raw3270
*rp
= rq
->view
->dev
;
532 raw3270_size_device(rp
);
533 raw3270_size_device_done(rp
);
537 raw3270_read_modified(struct raw3270
*rp
)
539 if (rp
->state
!= RAW3270_STATE_W4ATTN
)
541 /* Use 'read modified' to get the result of a read partition. */
542 memset(&rp
->init_readmod
, 0, sizeof(rp
->init_readmod
));
543 memset(&rp
->init_data
, 0, sizeof(rp
->init_data
));
544 rp
->init_readmod
.ccw
.cmd_code
= TC_READMOD
;
545 rp
->init_readmod
.ccw
.flags
= CCW_FLAG_SLI
;
546 rp
->init_readmod
.ccw
.count
= sizeof(rp
->init_data
);
547 rp
->init_readmod
.ccw
.cda
= (__u32
) __pa(rp
->init_data
);
548 rp
->init_readmod
.callback
= raw3270_read_modified_cb
;
549 rp
->state
= RAW3270_STATE_READMOD
;
550 raw3270_start_irq(&rp
->init_view
, &rp
->init_readmod
);
554 raw3270_writesf_readpart(struct raw3270
*rp
)
556 static const unsigned char wbuf
[] =
557 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
559 /* Store 'read partition' data stream to init_data */
560 memset(&rp
->init_readpart
, 0, sizeof(rp
->init_readpart
));
561 memset(&rp
->init_data
, 0, sizeof(rp
->init_data
));
562 memcpy(&rp
->init_data
, wbuf
, sizeof(wbuf
));
563 rp
->init_readpart
.ccw
.cmd_code
= TC_WRITESF
;
564 rp
->init_readpart
.ccw
.flags
= CCW_FLAG_SLI
;
565 rp
->init_readpart
.ccw
.count
= sizeof(wbuf
);
566 rp
->init_readpart
.ccw
.cda
= (__u32
) __pa(&rp
->init_data
);
567 rp
->state
= RAW3270_STATE_W4ATTN
;
568 raw3270_start_irq(&rp
->init_view
, &rp
->init_readpart
);
575 raw3270_reset_device_cb(struct raw3270_request
*rq
, void *data
)
577 struct raw3270
*rp
= rq
->view
->dev
;
579 if (rp
->state
!= RAW3270_STATE_RESET
)
582 /* Reset command failed. */
583 rp
->state
= RAW3270_STATE_INIT
;
584 } else if (MACHINE_IS_VM
) {
585 raw3270_size_device_vm(rp
);
586 raw3270_size_device_done(rp
);
588 raw3270_writesf_readpart(rp
);
589 memset(&rp
->init_reset
, 0, sizeof(rp
->init_reset
));
593 __raw3270_reset_device(struct raw3270
*rp
)
597 /* Check if reset is already pending */
598 if (rp
->init_reset
.view
)
600 /* Store reset data stream to init_data/init_reset */
601 rp
->init_data
[0] = TW_KR
;
602 rp
->init_reset
.ccw
.cmd_code
= TC_EWRITEA
;
603 rp
->init_reset
.ccw
.flags
= CCW_FLAG_SLI
;
604 rp
->init_reset
.ccw
.count
= 1;
605 rp
->init_reset
.ccw
.cda
= (__u32
) __pa(rp
->init_data
);
606 rp
->init_reset
.callback
= raw3270_reset_device_cb
;
607 rc
= __raw3270_start(rp
, &rp
->init_view
, &rp
->init_reset
);
608 if (rc
== 0 && rp
->state
== RAW3270_STATE_INIT
)
609 rp
->state
= RAW3270_STATE_RESET
;
614 raw3270_reset_device(struct raw3270
*rp
)
619 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
620 rc
= __raw3270_reset_device(rp
);
621 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
626 raw3270_reset(struct raw3270_view
*view
)
632 if (!rp
|| rp
->view
!= view
||
633 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
635 else if (!raw3270_state_ready(rp
))
638 rc
= raw3270_reset_device(view
->dev
);
643 __raw3270_disconnect(struct raw3270
*rp
)
645 struct raw3270_request
*rq
;
646 struct raw3270_view
*view
;
648 rp
->state
= RAW3270_STATE_INIT
;
649 rp
->view
= &rp
->init_view
;
650 /* Cancel all queued requests */
651 while (!list_empty(&rp
->req_queue
)) {
652 rq
= list_entry(rp
->req_queue
.next
,struct raw3270_request
,list
);
655 list_del_init(&rq
->list
);
657 rq
->callback(rq
, rq
->callback_data
);
658 raw3270_put_view(view
);
660 /* Start from scratch */
661 __raw3270_reset_device(rp
);
665 raw3270_init_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
,
671 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
) {
672 if (irb
->ecw
[0] & SNS0_CMD_REJECT
)
673 rq
->rc
= -EOPNOTSUPP
;
678 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_ATTENTION
) {
679 /* Queue read modified after attention interrupt */
681 raw3270_read_modified(rp
);
685 static struct raw3270_fn raw3270_init_fn
= {
686 .intv
= raw3270_init_irq
690 * Setup new 3270 device.
693 raw3270_setup_device(struct ccw_device
*cdev
, struct raw3270
*rp
, char *ascebc
)
699 memset(rp
, 0, sizeof(struct raw3270
));
700 /* Copy ebcdic -> ascii translation table. */
701 memcpy(ascebc
, _ascebc
, 256);
703 /* correct brackets and circumflex */
714 INIT_LIST_HEAD(&rp
->req_queue
);
715 INIT_LIST_HEAD(&rp
->view_list
);
717 rp
->init_view
.dev
= rp
;
718 rp
->init_view
.fn
= &raw3270_init_fn
;
719 rp
->view
= &rp
->init_view
;
722 * Add device to list and find the smallest unused minor
723 * number for it. Note: there is no device with minor 0,
724 * see special case for fs3270.c:fs3270_open().
726 mutex_lock(&raw3270_mutex
);
727 /* Keep the list sorted. */
728 minor
= RAW3270_FIRSTMINOR
;
730 list_for_each(l
, &raw3270_devices
) {
731 tmp
= list_entry(l
, struct raw3270
, list
);
732 if (tmp
->minor
> minor
) {
734 __list_add(&rp
->list
, l
->prev
, l
);
739 if (rp
->minor
== -1 && minor
< RAW3270_MAXDEVS
+ RAW3270_FIRSTMINOR
) {
741 list_add_tail(&rp
->list
, &raw3270_devices
);
743 mutex_unlock(&raw3270_mutex
);
744 /* No free minor number? Then give up. */
748 dev_set_drvdata(&cdev
->dev
, rp
);
749 cdev
->handler
= raw3270_irq
;
753 #ifdef CONFIG_TN3270_CONSOLE
754 /* Tentative definition - see below for actual definition. */
755 static struct ccw_driver raw3270_ccw_driver
;
758 * Setup 3270 device configured as console.
760 struct raw3270 __init
*raw3270_setup_console(void)
762 struct ccw_device
*cdev
;
768 cdev
= ccw_device_create_console(&raw3270_ccw_driver
);
770 return ERR_CAST(cdev
);
772 rp
= kzalloc(sizeof(struct raw3270
), GFP_KERNEL
| GFP_DMA
);
773 ascebc
= kzalloc(256, GFP_KERNEL
);
774 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
777 set_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
);
779 rc
= ccw_device_enable_console(cdev
);
781 ccw_device_destroy_console(cdev
);
785 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
787 __raw3270_reset_device(rp
);
788 while (!raw3270_state_final(rp
)) {
789 ccw_device_wait_idle(rp
->cdev
);
792 } while (rp
->state
!= RAW3270_STATE_READY
);
793 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
798 raw3270_wait_cons_dev(struct raw3270
*rp
)
802 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
803 ccw_device_wait_idle(rp
->cdev
);
804 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
810 * Create a 3270 device structure.
812 static struct raw3270
*
813 raw3270_create_device(struct ccw_device
*cdev
)
819 rp
= kzalloc(sizeof(struct raw3270
), GFP_KERNEL
| GFP_DMA
);
821 return ERR_PTR(-ENOMEM
);
822 ascebc
= kmalloc(256, GFP_KERNEL
);
825 return ERR_PTR(-ENOMEM
);
827 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
833 /* Get reference to ccw_device structure. */
834 get_device(&cdev
->dev
);
842 raw3270_activate_view(struct raw3270_view
*view
)
845 struct raw3270_view
*oldview
, *nv
;
852 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
853 if (rp
->view
== view
)
855 else if (!raw3270_state_ready(rp
))
857 else if (test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
861 if (rp
->view
&& rp
->view
->fn
->deactivate
) {
863 oldview
->fn
->deactivate(oldview
);
866 rc
= view
->fn
->activate(view
);
868 /* Didn't work. Try to reactivate the old view. */
870 if (!oldview
|| oldview
->fn
->activate(oldview
) != 0) {
871 /* Didn't work as well. Try any other view. */
872 list_for_each_entry(nv
, &rp
->view_list
, list
)
873 if (nv
!= view
&& nv
!= oldview
) {
875 if (nv
->fn
->activate(nv
) == 0)
882 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
887 * Deactivate current view.
890 raw3270_deactivate_view(struct raw3270_view
*view
)
898 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
899 if (rp
->view
== view
) {
900 view
->fn
->deactivate(view
);
902 /* Move deactivated view to end of list. */
903 list_del_init(&view
->list
);
904 list_add_tail(&view
->list
, &rp
->view_list
);
905 /* Try to activate another view. */
906 if (raw3270_state_ready(rp
) &&
907 !test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
)) {
908 list_for_each_entry(view
, &rp
->view_list
, list
) {
910 if (view
->fn
->activate(view
) == 0)
916 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
920 * Add view to device with minor "minor".
923 raw3270_add_view(struct raw3270_view
*view
, struct raw3270_fn
*fn
, int minor
, int subclass
)
931 mutex_lock(&raw3270_mutex
);
933 list_for_each_entry(rp
, &raw3270_devices
, list
) {
934 if (rp
->minor
!= minor
)
936 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
937 atomic_set(&view
->ref_count
, 2);
940 view
->model
= rp
->model
;
941 view
->rows
= rp
->rows
;
942 view
->cols
= rp
->cols
;
943 view
->ascebc
= rp
->ascebc
;
944 spin_lock_init(&view
->lock
);
945 lockdep_set_subclass(&view
->lock
, subclass
);
946 list_add(&view
->list
, &rp
->view_list
);
948 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
951 mutex_unlock(&raw3270_mutex
);
956 * Find specific view of device with minor "minor".
958 struct raw3270_view
*
959 raw3270_find_view(struct raw3270_fn
*fn
, int minor
)
962 struct raw3270_view
*view
, *tmp
;
965 mutex_lock(&raw3270_mutex
);
966 view
= ERR_PTR(-ENODEV
);
967 list_for_each_entry(rp
, &raw3270_devices
, list
) {
968 if (rp
->minor
!= minor
)
970 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
971 list_for_each_entry(tmp
, &rp
->view_list
, list
) {
973 raw3270_get_view(tmp
);
978 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
981 mutex_unlock(&raw3270_mutex
);
986 * Remove view from device and free view structure via call to view->fn->free.
989 raw3270_del_view(struct raw3270_view
*view
)
993 struct raw3270_view
*nv
;
996 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
997 if (rp
->view
== view
) {
998 view
->fn
->deactivate(view
);
1001 list_del_init(&view
->list
);
1002 if (!rp
->view
&& raw3270_state_ready(rp
) &&
1003 !test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
)) {
1004 /* Try to activate another view. */
1005 list_for_each_entry(nv
, &rp
->view_list
, list
) {
1006 if (nv
->fn
->activate(nv
) == 0) {
1012 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1013 /* Wait for reference counter to drop to zero. */
1014 atomic_dec(&view
->ref_count
);
1015 wait_event(raw3270_wait_queue
, atomic_read(&view
->ref_count
) == 0);
1017 view
->fn
->free(view
);
1021 * Remove a 3270 device structure.
1024 raw3270_delete_device(struct raw3270
*rp
)
1026 struct ccw_device
*cdev
;
1028 /* Remove from device chain. */
1029 mutex_lock(&raw3270_mutex
);
1030 list_del_init(&rp
->list
);
1031 mutex_unlock(&raw3270_mutex
);
1033 /* Disconnect from ccw_device. */
1036 dev_set_drvdata(&cdev
->dev
, NULL
);
1037 cdev
->handler
= NULL
;
1039 /* Put ccw_device structure. */
1040 put_device(&cdev
->dev
);
1042 /* Now free raw3270 structure. */
1048 raw3270_probe (struct ccw_device
*cdev
)
1054 * Additional attributes for a 3270 device
1057 raw3270_model_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1059 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1060 ((struct raw3270
*) dev_get_drvdata(dev
))->model
);
1062 static DEVICE_ATTR(model
, 0444, raw3270_model_show
, NULL
);
1065 raw3270_rows_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1067 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1068 ((struct raw3270
*) dev_get_drvdata(dev
))->rows
);
1070 static DEVICE_ATTR(rows
, 0444, raw3270_rows_show
, NULL
);
1073 raw3270_columns_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1075 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1076 ((struct raw3270
*) dev_get_drvdata(dev
))->cols
);
1078 static DEVICE_ATTR(columns
, 0444, raw3270_columns_show
, NULL
);
1080 static struct attribute
* raw3270_attrs
[] = {
1081 &dev_attr_model
.attr
,
1082 &dev_attr_rows
.attr
,
1083 &dev_attr_columns
.attr
,
1087 static const struct attribute_group raw3270_attr_group
= {
1088 .attrs
= raw3270_attrs
,
1091 static int raw3270_create_attributes(struct raw3270
*rp
)
1093 return sysfs_create_group(&rp
->cdev
->dev
.kobj
, &raw3270_attr_group
);
1097 * Notifier for device addition/removal
1099 static LIST_HEAD(raw3270_notifier
);
1101 int raw3270_register_notifier(struct raw3270_notifier
*notifier
)
1105 mutex_lock(&raw3270_mutex
);
1106 list_add_tail(¬ifier
->list
, &raw3270_notifier
);
1107 list_for_each_entry(rp
, &raw3270_devices
, list
)
1108 notifier
->create(rp
->minor
);
1109 mutex_unlock(&raw3270_mutex
);
1113 void raw3270_unregister_notifier(struct raw3270_notifier
*notifier
)
1117 mutex_lock(&raw3270_mutex
);
1118 list_for_each_entry(rp
, &raw3270_devices
, list
)
1119 notifier
->destroy(rp
->minor
);
1120 list_del(¬ifier
->list
);
1121 mutex_unlock(&raw3270_mutex
);
1125 * Set 3270 device online.
1128 raw3270_set_online (struct ccw_device
*cdev
)
1130 struct raw3270_notifier
*np
;
1134 rp
= raw3270_create_device(cdev
);
1137 rc
= raw3270_create_attributes(rp
);
1140 raw3270_reset_device(rp
);
1141 mutex_lock(&raw3270_mutex
);
1142 list_for_each_entry(np
, &raw3270_notifier
, list
)
1143 np
->create(rp
->minor
);
1144 mutex_unlock(&raw3270_mutex
);
1148 raw3270_delete_device(rp
);
1153 * Remove 3270 device structure.
1156 raw3270_remove (struct ccw_device
*cdev
)
1158 unsigned long flags
;
1160 struct raw3270_view
*v
;
1161 struct raw3270_notifier
*np
;
1163 rp
= dev_get_drvdata(&cdev
->dev
);
1165 * _remove is the opposite of _probe; it's probe that
1166 * should set up rp. raw3270_remove gets entered for
1167 * devices even if they haven't been varied online.
1168 * Thus, rp may validly be NULL here.
1173 sysfs_remove_group(&cdev
->dev
.kobj
, &raw3270_attr_group
);
1175 /* Deactivate current view and remove all views. */
1176 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1178 if (rp
->view
->fn
->deactivate
)
1179 rp
->view
->fn
->deactivate(rp
->view
);
1182 while (!list_empty(&rp
->view_list
)) {
1183 v
= list_entry(rp
->view_list
.next
, struct raw3270_view
, list
);
1186 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1187 raw3270_del_view(v
);
1188 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1190 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1192 mutex_lock(&raw3270_mutex
);
1193 list_for_each_entry(np
, &raw3270_notifier
, list
)
1194 np
->destroy(rp
->minor
);
1195 mutex_unlock(&raw3270_mutex
);
1197 /* Reset 3270 device. */
1198 raw3270_reset_device(rp
);
1199 /* And finally remove it. */
1200 raw3270_delete_device(rp
);
1204 * Set 3270 device offline.
1207 raw3270_set_offline (struct ccw_device
*cdev
)
1211 rp
= dev_get_drvdata(&cdev
->dev
);
1212 if (test_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
))
1214 raw3270_remove(cdev
);
1218 static int raw3270_pm_stop(struct ccw_device
*cdev
)
1221 struct raw3270_view
*view
;
1222 unsigned long flags
;
1224 rp
= dev_get_drvdata(&cdev
->dev
);
1227 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1228 if (rp
->view
&& rp
->view
->fn
->deactivate
)
1229 rp
->view
->fn
->deactivate(rp
->view
);
1230 if (!test_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
)) {
1232 * Release tty and fullscreen for all non-console
1235 list_for_each_entry(view
, &rp
->view_list
, list
) {
1236 if (view
->fn
->release
)
1237 view
->fn
->release(view
);
1240 set_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
);
1241 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1245 static int raw3270_pm_start(struct ccw_device
*cdev
)
1248 unsigned long flags
;
1250 rp
= dev_get_drvdata(&cdev
->dev
);
1253 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1254 clear_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
);
1255 if (rp
->view
&& rp
->view
->fn
->activate
)
1256 rp
->view
->fn
->activate(rp
->view
);
1257 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1261 void raw3270_pm_unfreeze(struct raw3270_view
*view
)
1263 #ifdef CONFIG_TN3270_CONSOLE
1267 if (rp
&& test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
1268 ccw_device_force_console(rp
->cdev
);
1272 static struct ccw_device_id raw3270_id
[] = {
1273 { CCW_DEVICE(0x3270, 0) },
1274 { CCW_DEVICE(0x3271, 0) },
1275 { CCW_DEVICE(0x3272, 0) },
1276 { CCW_DEVICE(0x3273, 0) },
1277 { CCW_DEVICE(0x3274, 0) },
1278 { CCW_DEVICE(0x3275, 0) },
1279 { CCW_DEVICE(0x3276, 0) },
1280 { CCW_DEVICE(0x3277, 0) },
1281 { CCW_DEVICE(0x3278, 0) },
1282 { CCW_DEVICE(0x3279, 0) },
1283 { CCW_DEVICE(0x3174, 0) },
1284 { /* end of list */ },
1287 static struct ccw_driver raw3270_ccw_driver
= {
1290 .owner
= THIS_MODULE
,
1293 .probe
= &raw3270_probe
,
1294 .remove
= &raw3270_remove
,
1295 .set_online
= &raw3270_set_online
,
1296 .set_offline
= &raw3270_set_offline
,
1297 .freeze
= &raw3270_pm_stop
,
1298 .thaw
= &raw3270_pm_start
,
1299 .restore
= &raw3270_pm_start
,
1300 .int_class
= IRQIO_C70
,
1309 if (raw3270_registered
)
1311 raw3270_registered
= 1;
1312 rc
= ccw_driver_register(&raw3270_ccw_driver
);
1314 /* Create attributes for early (= console) device. */
1315 mutex_lock(&raw3270_mutex
);
1316 class3270
= class_create(THIS_MODULE
, "3270");
1317 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1318 get_device(&rp
->cdev
->dev
);
1319 raw3270_create_attributes(rp
);
1321 mutex_unlock(&raw3270_mutex
);
1329 ccw_driver_unregister(&raw3270_ccw_driver
);
1330 class_destroy(class3270
);
1333 MODULE_LICENSE("GPL");
1335 module_init(raw3270_init
);
1336 module_exit(raw3270_exit
);
1338 EXPORT_SYMBOL(class3270
);
1339 EXPORT_SYMBOL(raw3270_request_alloc
);
1340 EXPORT_SYMBOL(raw3270_request_free
);
1341 EXPORT_SYMBOL(raw3270_request_reset
);
1342 EXPORT_SYMBOL(raw3270_request_set_cmd
);
1343 EXPORT_SYMBOL(raw3270_request_add_data
);
1344 EXPORT_SYMBOL(raw3270_request_set_data
);
1345 EXPORT_SYMBOL(raw3270_request_set_idal
);
1346 EXPORT_SYMBOL(raw3270_buffer_address
);
1347 EXPORT_SYMBOL(raw3270_add_view
);
1348 EXPORT_SYMBOL(raw3270_del_view
);
1349 EXPORT_SYMBOL(raw3270_find_view
);
1350 EXPORT_SYMBOL(raw3270_activate_view
);
1351 EXPORT_SYMBOL(raw3270_deactivate_view
);
1352 EXPORT_SYMBOL(raw3270_start
);
1353 EXPORT_SYMBOL(raw3270_start_locked
);
1354 EXPORT_SYMBOL(raw3270_start_irq
);
1355 EXPORT_SYMBOL(raw3270_reset
);
1356 EXPORT_SYMBOL(raw3270_register_notifier
);
1357 EXPORT_SYMBOL(raw3270_unregister_notifier
);
1358 EXPORT_SYMBOL(raw3270_wait_queue
);