2 * IBM/3270 Driver - core functions.
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/kernel_stat.h>
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 static struct class *class3270
;
34 /* The main 3270 data structure. */
36 struct list_head list
;
37 struct ccw_device
*cdev
;
40 short model
, rows
, cols
;
43 struct list_head req_queue
; /* Request queue. */
44 struct list_head view_list
; /* List of available views. */
45 struct raw3270_view
*view
; /* Active view. */
47 struct timer_list timer
; /* Device timer. */
49 unsigned char *ascebc
; /* ascii -> ebcdic table */
50 struct device
*clttydev
; /* 3270-class tty device ptr */
51 struct device
*cltubdev
; /* 3270-class tub device ptr */
53 struct raw3270_request init_request
;
54 unsigned char init_data
[256];
58 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
59 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
60 #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
61 #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
62 #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
63 #define RAW3270_FLAGS_FROZEN 16 /* set if 3270 is frozen for suspend */
65 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
66 static DEFINE_MUTEX(raw3270_mutex
);
68 /* List of 3270 devices. */
69 static LIST_HEAD(raw3270_devices
);
72 * Flag to indicate if the driver has been registered. Some operations
73 * like waiting for the end of i/o need to be done differently as long
74 * as the kernel is still starting up (console support).
76 static int raw3270_registered
;
78 /* Module parameters */
79 static int tubxcorrect
= 0;
80 module_param(tubxcorrect
, bool, 0);
83 * Wait queue for device init/delete, view delete.
85 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue
);
88 * Encode array for 12 bit 3270 addresses.
90 static unsigned char raw3270_ebcgraf
[64] = {
91 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
92 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
93 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
94 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
95 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
96 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
97 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
98 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
102 raw3270_buffer_address(struct raw3270
*rp
, char *cp
, unsigned short addr
)
104 if (test_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
)) {
105 cp
[0] = (addr
>> 8) & 0x3f;
108 cp
[0] = raw3270_ebcgraf
[(addr
>> 6) & 0x3f];
109 cp
[1] = raw3270_ebcgraf
[addr
& 0x3f];
114 * Allocate a new 3270 ccw request
116 struct raw3270_request
*
117 raw3270_request_alloc(size_t size
)
119 struct raw3270_request
*rq
;
121 /* Allocate request structure */
122 rq
= kzalloc(sizeof(struct raw3270_request
), GFP_KERNEL
| GFP_DMA
);
124 return ERR_PTR(-ENOMEM
);
126 /* alloc output buffer. */
128 rq
->buffer
= kmalloc(size
, GFP_KERNEL
| GFP_DMA
);
131 return ERR_PTR(-ENOMEM
);
135 INIT_LIST_HEAD(&rq
->list
);
140 rq
->ccw
.cda
= __pa(rq
->buffer
);
141 rq
->ccw
.flags
= CCW_FLAG_SLI
;
147 * Free 3270 ccw request
150 raw3270_request_free (struct raw3270_request
*rq
)
157 * Reset request to initial state.
160 raw3270_request_reset(struct raw3270_request
*rq
)
162 BUG_ON(!list_empty(&rq
->list
));
163 rq
->ccw
.cmd_code
= 0;
165 rq
->ccw
.cda
= __pa(rq
->buffer
);
166 rq
->ccw
.flags
= CCW_FLAG_SLI
;
172 * Set command code to ccw of a request.
175 raw3270_request_set_cmd(struct raw3270_request
*rq
, u8 cmd
)
177 rq
->ccw
.cmd_code
= cmd
;
181 * Add data fragment to output buffer.
184 raw3270_request_add_data(struct raw3270_request
*rq
, void *data
, size_t size
)
186 if (size
+ rq
->ccw
.count
> rq
->size
)
188 memcpy(rq
->buffer
+ rq
->ccw
.count
, data
, size
);
189 rq
->ccw
.count
+= size
;
194 * Set address/length pair to ccw of a request.
197 raw3270_request_set_data(struct raw3270_request
*rq
, void *data
, size_t size
)
199 rq
->ccw
.cda
= __pa(data
);
200 rq
->ccw
.count
= size
;
204 * Set idal buffer to ccw of a request.
207 raw3270_request_set_idal(struct raw3270_request
*rq
, struct idal_buffer
*ib
)
209 rq
->ccw
.cda
= __pa(ib
->data
);
210 rq
->ccw
.count
= ib
->size
;
211 rq
->ccw
.flags
|= CCW_FLAG_IDA
;
218 raw3270_halt_io_nolock(struct raw3270
*rp
, struct raw3270_request
*rq
)
223 if (raw3270_request_final(rq
))
225 /* Check if interrupt has already been processed */
226 for (retries
= 0; retries
< 5; retries
++) {
228 rc
= ccw_device_halt(rp
->cdev
, (long) rq
);
230 rc
= ccw_device_clear(rp
->cdev
, (long) rq
);
232 break; /* termination successful */
238 raw3270_halt_io(struct raw3270
*rp
, struct raw3270_request
*rq
)
243 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
244 rc
= raw3270_halt_io_nolock(rp
, rq
);
245 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
250 * Add the request to the request queue, try to start it if the
251 * 3270 device is idle. Return without waiting for end of i/o.
254 __raw3270_start(struct raw3270
*rp
, struct raw3270_view
*view
,
255 struct raw3270_request
*rq
)
258 raw3270_get_view(view
);
259 if (list_empty(&rp
->req_queue
) &&
260 !test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
)) {
261 /* No other requests are on the queue. Start this one. */
262 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
263 (unsigned long) rq
, 0, 0);
265 raw3270_put_view(view
);
269 list_add_tail(&rq
->list
, &rp
->req_queue
);
274 raw3270_start(struct raw3270_view
*view
, struct raw3270_request
*rq
)
280 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
282 if (!rp
|| rp
->view
!= view
||
283 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
285 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
288 rc
= __raw3270_start(rp
, view
, rq
);
289 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
294 raw3270_start_locked(struct raw3270_view
*view
, struct raw3270_request
*rq
)
300 if (!rp
|| rp
->view
!= view
||
301 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
303 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
306 rc
= __raw3270_start(rp
, view
, rq
);
311 raw3270_start_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
)
317 raw3270_get_view(view
);
318 list_add_tail(&rq
->list
, &rp
->req_queue
);
323 * 3270 interrupt routine, called from the ccw_device layer
326 raw3270_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
329 struct raw3270_view
*view
;
330 struct raw3270_request
*rq
;
333 kstat_cpu(smp_processor_id()).irqs
[IOINT_C70
]++;
334 rp
= dev_get_drvdata(&cdev
->dev
);
337 rq
= (struct raw3270_request
*) intparm
;
338 view
= rq
? rq
->view
: rp
->view
;
341 rc
= RAW3270_IO_RETRY
;
342 else if (irb
->scsw
.cmd
.fctl
& SCSW_FCTL_HALT_FUNC
) {
344 rc
= RAW3270_IO_DONE
;
345 } else if (irb
->scsw
.cmd
.dstat
== (DEV_STAT_CHN_END
| DEV_STAT_DEV_END
|
346 DEV_STAT_UNIT_EXCEP
)) {
347 /* Handle CE-DE-UE and subsequent UDE */
348 set_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
349 rc
= RAW3270_IO_BUSY
;
350 } else if (test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
)) {
351 /* Wait for UDE if busy flag is set. */
352 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_DEV_END
) {
353 clear_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
354 /* Got it, now retry. */
355 rc
= RAW3270_IO_RETRY
;
357 rc
= RAW3270_IO_BUSY
;
359 rc
= view
->fn
->intv(view
, rq
, irb
);
361 rc
= RAW3270_IO_DONE
;
364 case RAW3270_IO_DONE
:
366 case RAW3270_IO_BUSY
:
368 * Intervention required by the operator. We have to wait
369 * for unsolicited device end.
372 case RAW3270_IO_RETRY
:
375 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
376 (unsigned long) rq
, 0, 0);
378 return; /* Successfully restarted. */
380 case RAW3270_IO_STOP
:
383 raw3270_halt_io_nolock(rp
, rq
);
390 BUG_ON(list_empty(&rq
->list
));
391 /* The request completed, remove from queue and do callback. */
392 list_del_init(&rq
->list
);
394 rq
->callback(rq
, rq
->callback_data
);
395 /* Do put_device for get_device in raw3270_start. */
396 raw3270_put_view(view
);
399 * Try to start each request on request queue until one is
400 * started successful.
402 while (!list_empty(&rp
->req_queue
)) {
403 rq
= list_entry(rp
->req_queue
.next
,struct raw3270_request
,list
);
404 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
405 (unsigned long) rq
, 0, 0);
408 /* Start failed. Remove request and do callback. */
409 list_del_init(&rq
->list
);
411 rq
->callback(rq
, rq
->callback_data
);
412 /* Do put_device for get_device in raw3270_start. */
413 raw3270_put_view(view
);
421 struct raw3270_ua
{ /* Query Reply structure for Usable Area */
422 struct { /* Usable Area Query Reply Base */
423 short l
; /* Length of this structured field */
424 char sfid
; /* 0x81 if Query Reply */
425 char qcode
; /* 0x81 if Usable Area */
428 short w
; /* Width of usable area */
429 short h
; /* Heigth of usavle area */
430 char units
; /* 0x00:in; 0x01:mm */
435 short buffsz
; /* Character buffer size, bytes */
440 } __attribute__ ((packed
)) uab
;
441 struct { /* Alternate Usable Area Self-Defining Parameter */
442 char l
; /* Length of this Self-Defining Parm */
443 char sdpid
; /* 0x02 if Alternate Usable Area */
445 char auaid
; /* 0x01 is Id for the A U A */
446 short wauai
; /* Width of AUAi */
447 short hauai
; /* Height of AUAi */
448 char auaunits
; /* 0x00:in, 0x01:mm */
453 } __attribute__ ((packed
)) aua
;
454 } __attribute__ ((packed
));
456 static struct diag210 raw3270_init_diag210
;
457 static DEFINE_MUTEX(raw3270_init_mutex
);
460 raw3270_init_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
,
464 * Unit-Check Processing:
465 * Expect Command Reject or Intervention Required.
467 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
) {
468 /* Request finished abnormally. */
469 if (irb
->ecw
[0] & SNS0_INTERVENTION_REQ
) {
470 set_bit(RAW3270_FLAGS_BUSY
, &view
->dev
->flags
);
471 return RAW3270_IO_BUSY
;
475 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
) {
476 if (irb
->ecw
[0] & SNS0_CMD_REJECT
)
477 rq
->rc
= -EOPNOTSUPP
;
481 /* Request finished normally. Copy residual count. */
482 rq
->rescnt
= irb
->scsw
.cmd
.count
;
484 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_ATTENTION
) {
485 set_bit(RAW3270_FLAGS_ATTN
, &view
->dev
->flags
);
486 wake_up(&raw3270_wait_queue
);
488 return RAW3270_IO_DONE
;
491 static struct raw3270_fn raw3270_init_fn
= {
492 .intv
= raw3270_init_irq
495 static struct raw3270_view raw3270_init_view
= {
496 .fn
= &raw3270_init_fn
500 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
501 * Wait for end of request. The request must have been started
502 * with raw3270_start, rc = 0. The device lock may NOT have been
503 * released between calling raw3270_start and raw3270_wait.
506 raw3270_wake_init(struct raw3270_request
*rq
, void *data
)
508 wake_up((wait_queue_head_t
*) data
);
512 * Special wait function that can cope with console initialization.
515 raw3270_start_init(struct raw3270
*rp
, struct raw3270_view
*view
,
516 struct raw3270_request
*rq
)
521 #ifdef CONFIG_TN3270_CONSOLE
522 if (raw3270_registered
== 0) {
523 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
525 rc
= __raw3270_start(rp
, view
, rq
);
527 while (!raw3270_request_final(rq
)) {
531 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
535 rq
->callback
= raw3270_wake_init
;
536 rq
->callback_data
= &raw3270_wait_queue
;
537 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
538 rc
= __raw3270_start(rp
, view
, rq
);
539 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
542 /* Now wait for the completion. */
543 rc
= wait_event_interruptible(raw3270_wait_queue
,
544 raw3270_request_final(rq
));
545 if (rc
== -ERESTARTSYS
) { /* Interrupted by a signal. */
546 raw3270_halt_io(view
->dev
, rq
);
547 /* No wait for the halt to complete. */
548 wait_event(raw3270_wait_queue
, raw3270_request_final(rq
));
555 __raw3270_size_device_vm(struct raw3270
*rp
)
558 struct ccw_dev_id dev_id
;
560 ccw_device_get_id(rp
->cdev
, &dev_id
);
561 raw3270_init_diag210
.vrdcdvno
= dev_id
.devno
;
562 raw3270_init_diag210
.vrdclen
= sizeof(struct diag210
);
563 rc
= diag210(&raw3270_init_diag210
);
566 model
= raw3270_init_diag210
.vrdccrmd
;
596 __raw3270_size_device(struct raw3270
*rp
)
598 static const unsigned char wbuf
[] =
599 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
600 struct raw3270_ua
*uap
;
601 unsigned short count
;
605 * To determine the size of the 3270 device we need to do:
606 * 1) send a 'read partition' data stream to the device
607 * 2) wait for the attn interrupt that precedes the query reply
608 * 3) do a read modified to get the query reply
609 * To make things worse we have to cope with intervention
610 * required (3270 device switched to 'stand-by') and command
611 * rejects (old devices that can't do 'read partition').
613 memset(&rp
->init_request
, 0, sizeof(rp
->init_request
));
614 memset(&rp
->init_data
, 0, 256);
615 /* Store 'read partition' data stream to init_data */
616 memcpy(&rp
->init_data
, wbuf
, sizeof(wbuf
));
617 INIT_LIST_HEAD(&rp
->init_request
.list
);
618 rp
->init_request
.ccw
.cmd_code
= TC_WRITESF
;
619 rp
->init_request
.ccw
.flags
= CCW_FLAG_SLI
;
620 rp
->init_request
.ccw
.count
= sizeof(wbuf
);
621 rp
->init_request
.ccw
.cda
= (__u32
) __pa(&rp
->init_data
);
623 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &rp
->init_request
);
625 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
628 /* Wait for attention interrupt. */
629 #ifdef CONFIG_TN3270_CONSOLE
630 if (raw3270_registered
== 0) {
633 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
634 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN
, &rp
->flags
))
636 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
639 rc
= wait_event_interruptible(raw3270_wait_queue
,
640 test_and_clear_bit(RAW3270_FLAGS_ATTN
, &rp
->flags
));
645 * The device accepted the 'read partition' command. Now
646 * set up a read ccw and issue it.
648 rp
->init_request
.ccw
.cmd_code
= TC_READMOD
;
649 rp
->init_request
.ccw
.flags
= CCW_FLAG_SLI
;
650 rp
->init_request
.ccw
.count
= sizeof(rp
->init_data
);
651 rp
->init_request
.ccw
.cda
= (__u32
) __pa(rp
->init_data
);
652 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &rp
->init_request
);
655 /* Got a Query Reply */
656 count
= sizeof(rp
->init_data
) - rp
->init_request
.rescnt
;
657 uap
= (struct raw3270_ua
*) (rp
->init_data
+ 1);
658 /* Paranoia check. */
659 if (rp
->init_data
[0] != 0x88 || uap
->uab
.qcode
!= 0x81)
661 /* Copy rows/columns of default Usable Area */
662 rp
->rows
= uap
->uab
.h
;
663 rp
->cols
= uap
->uab
.w
;
664 /* Check for 14 bit addressing */
665 if ((uap
->uab
.flags0
& 0x0d) == 0x01)
666 set_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
);
667 /* Check for Alternate Usable Area */
668 if (uap
->uab
.l
== sizeof(struct raw3270_ua
) &&
669 uap
->aua
.sdpid
== 0x02) {
670 rp
->rows
= uap
->aua
.hauai
;
671 rp
->cols
= uap
->aua
.wauai
;
677 raw3270_size_device(struct raw3270
*rp
)
681 mutex_lock(&raw3270_init_mutex
);
682 rp
->view
= &raw3270_init_view
;
683 raw3270_init_view
.dev
= rp
;
685 rc
= __raw3270_size_device_vm(rp
);
687 rc
= __raw3270_size_device(rp
);
688 raw3270_init_view
.dev
= NULL
;
690 mutex_unlock(&raw3270_init_mutex
);
691 if (rc
== 0) { /* Found something. */
692 /* Try to find a model. */
694 if (rp
->rows
== 24 && rp
->cols
== 80)
696 if (rp
->rows
== 32 && rp
->cols
== 80)
698 if (rp
->rows
== 43 && rp
->cols
== 80)
700 if (rp
->rows
== 27 && rp
->cols
== 132)
703 /* Couldn't detect size. Use default model 2. */
713 raw3270_reset_device(struct raw3270
*rp
)
717 mutex_lock(&raw3270_init_mutex
);
718 memset(&rp
->init_request
, 0, sizeof(rp
->init_request
));
719 memset(&rp
->init_data
, 0, sizeof(rp
->init_data
));
720 /* Store reset data stream to init_data/init_request */
721 rp
->init_data
[0] = TW_KR
;
722 INIT_LIST_HEAD(&rp
->init_request
.list
);
723 rp
->init_request
.ccw
.cmd_code
= TC_EWRITEA
;
724 rp
->init_request
.ccw
.flags
= CCW_FLAG_SLI
;
725 rp
->init_request
.ccw
.count
= 1;
726 rp
->init_request
.ccw
.cda
= (__u32
) __pa(rp
->init_data
);
727 rp
->view
= &raw3270_init_view
;
728 raw3270_init_view
.dev
= rp
;
729 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &rp
->init_request
);
730 raw3270_init_view
.dev
= NULL
;
732 mutex_unlock(&raw3270_init_mutex
);
737 raw3270_reset(struct raw3270_view
*view
)
743 if (!rp
|| rp
->view
!= view
||
744 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
746 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
749 rc
= raw3270_reset_device(view
->dev
);
754 * Setup new 3270 device.
757 raw3270_setup_device(struct ccw_device
*cdev
, struct raw3270
*rp
, char *ascebc
)
763 memset(rp
, 0, sizeof(struct raw3270
));
764 /* Copy ebcdic -> ascii translation table. */
765 memcpy(ascebc
, _ascebc
, 256);
767 /* correct brackets and circumflex */
778 INIT_LIST_HEAD(&rp
->req_queue
);
779 INIT_LIST_HEAD(&rp
->view_list
);
782 * Add device to list and find the smallest unused minor
783 * number for it. Note: there is no device with minor 0,
784 * see special case for fs3270.c:fs3270_open().
786 mutex_lock(&raw3270_mutex
);
787 /* Keep the list sorted. */
788 minor
= RAW3270_FIRSTMINOR
;
790 list_for_each(l
, &raw3270_devices
) {
791 tmp
= list_entry(l
, struct raw3270
, list
);
792 if (tmp
->minor
> minor
) {
794 __list_add(&rp
->list
, l
->prev
, l
);
799 if (rp
->minor
== -1 && minor
< RAW3270_MAXDEVS
+ RAW3270_FIRSTMINOR
) {
801 list_add_tail(&rp
->list
, &raw3270_devices
);
803 mutex_unlock(&raw3270_mutex
);
804 /* No free minor number? Then give up. */
808 dev_set_drvdata(&cdev
->dev
, rp
);
809 cdev
->handler
= raw3270_irq
;
813 #ifdef CONFIG_TN3270_CONSOLE
815 * Setup 3270 device configured as console.
817 struct raw3270 __init
*raw3270_setup_console(struct ccw_device
*cdev
)
823 rp
= kzalloc(sizeof(struct raw3270
), GFP_KERNEL
| GFP_DMA
);
824 ascebc
= kzalloc(256, GFP_KERNEL
);
825 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
828 set_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
);
829 rc
= raw3270_reset_device(rp
);
832 rc
= raw3270_size_device(rp
);
835 rc
= raw3270_reset_device(rp
);
838 set_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
843 raw3270_wait_cons_dev(struct raw3270
*rp
)
847 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
849 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
855 * Create a 3270 device structure.
857 static struct raw3270
*
858 raw3270_create_device(struct ccw_device
*cdev
)
864 rp
= kmalloc(sizeof(struct raw3270
), GFP_KERNEL
| GFP_DMA
);
866 return ERR_PTR(-ENOMEM
);
867 ascebc
= kmalloc(256, GFP_KERNEL
);
870 return ERR_PTR(-ENOMEM
);
872 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
878 /* Get reference to ccw_device structure. */
879 get_device(&cdev
->dev
);
887 raw3270_activate_view(struct raw3270_view
*view
)
890 struct raw3270_view
*oldview
, *nv
;
897 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
898 if (rp
->view
== view
)
900 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
902 else if (test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
908 oldview
->fn
->deactivate(oldview
);
911 rc
= view
->fn
->activate(view
);
913 /* Didn't work. Try to reactivate the old view. */
915 if (!oldview
|| oldview
->fn
->activate(oldview
) != 0) {
916 /* Didn't work as well. Try any other view. */
917 list_for_each_entry(nv
, &rp
->view_list
, list
)
918 if (nv
!= view
&& nv
!= oldview
) {
920 if (nv
->fn
->activate(nv
) == 0)
927 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
932 * Deactivate current view.
935 raw3270_deactivate_view(struct raw3270_view
*view
)
943 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
944 if (rp
->view
== view
) {
945 view
->fn
->deactivate(view
);
947 /* Move deactivated view to end of list. */
948 list_del_init(&view
->list
);
949 list_add_tail(&view
->list
, &rp
->view_list
);
950 /* Try to activate another view. */
951 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
) &&
952 !test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
)) {
953 list_for_each_entry(view
, &rp
->view_list
, list
) {
955 if (view
->fn
->activate(view
) == 0)
961 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
965 * Add view to device with minor "minor".
968 raw3270_add_view(struct raw3270_view
*view
, struct raw3270_fn
*fn
, int minor
)
976 mutex_lock(&raw3270_mutex
);
978 list_for_each_entry(rp
, &raw3270_devices
, list
) {
979 if (rp
->minor
!= minor
)
981 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
982 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
983 atomic_set(&view
->ref_count
, 2);
986 view
->model
= rp
->model
;
987 view
->rows
= rp
->rows
;
988 view
->cols
= rp
->cols
;
989 view
->ascebc
= rp
->ascebc
;
990 spin_lock_init(&view
->lock
);
991 list_add(&view
->list
, &rp
->view_list
);
994 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
997 mutex_unlock(&raw3270_mutex
);
1002 * Find specific view of device with minor "minor".
1004 struct raw3270_view
*
1005 raw3270_find_view(struct raw3270_fn
*fn
, int minor
)
1008 struct raw3270_view
*view
, *tmp
;
1009 unsigned long flags
;
1011 mutex_lock(&raw3270_mutex
);
1012 view
= ERR_PTR(-ENODEV
);
1013 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1014 if (rp
->minor
!= minor
)
1016 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1017 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
1018 view
= ERR_PTR(-ENOENT
);
1019 list_for_each_entry(tmp
, &rp
->view_list
, list
) {
1020 if (tmp
->fn
== fn
) {
1021 raw3270_get_view(tmp
);
1027 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1030 mutex_unlock(&raw3270_mutex
);
1035 * Remove view from device and free view structure via call to view->fn->free.
1038 raw3270_del_view(struct raw3270_view
*view
)
1040 unsigned long flags
;
1042 struct raw3270_view
*nv
;
1045 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1046 if (rp
->view
== view
) {
1047 view
->fn
->deactivate(view
);
1050 list_del_init(&view
->list
);
1051 if (!rp
->view
&& test_bit(RAW3270_FLAGS_READY
, &rp
->flags
) &&
1052 !test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
)) {
1053 /* Try to activate another view. */
1054 list_for_each_entry(nv
, &rp
->view_list
, list
) {
1055 if (nv
->fn
->activate(nv
) == 0) {
1061 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1062 /* Wait for reference counter to drop to zero. */
1063 atomic_dec(&view
->ref_count
);
1064 wait_event(raw3270_wait_queue
, atomic_read(&view
->ref_count
) == 0);
1066 view
->fn
->free(view
);
1070 * Remove a 3270 device structure.
1073 raw3270_delete_device(struct raw3270
*rp
)
1075 struct ccw_device
*cdev
;
1077 /* Remove from device chain. */
1078 mutex_lock(&raw3270_mutex
);
1079 if (rp
->clttydev
&& !IS_ERR(rp
->clttydev
))
1080 device_destroy(class3270
, MKDEV(IBM_TTY3270_MAJOR
, rp
->minor
));
1081 if (rp
->cltubdev
&& !IS_ERR(rp
->cltubdev
))
1082 device_destroy(class3270
, MKDEV(IBM_FS3270_MAJOR
, rp
->minor
));
1083 list_del_init(&rp
->list
);
1084 mutex_unlock(&raw3270_mutex
);
1086 /* Disconnect from ccw_device. */
1089 dev_set_drvdata(&cdev
->dev
, NULL
);
1090 cdev
->handler
= NULL
;
1092 /* Put ccw_device structure. */
1093 put_device(&cdev
->dev
);
1095 /* Now free raw3270 structure. */
1101 raw3270_probe (struct ccw_device
*cdev
)
1107 * Additional attributes for a 3270 device
1110 raw3270_model_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1112 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1113 ((struct raw3270
*) dev_get_drvdata(dev
))->model
);
1115 static DEVICE_ATTR(model
, 0444, raw3270_model_show
, NULL
);
1118 raw3270_rows_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1120 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1121 ((struct raw3270
*) dev_get_drvdata(dev
))->rows
);
1123 static DEVICE_ATTR(rows
, 0444, raw3270_rows_show
, NULL
);
1126 raw3270_columns_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1128 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1129 ((struct raw3270
*) dev_get_drvdata(dev
))->cols
);
1131 static DEVICE_ATTR(columns
, 0444, raw3270_columns_show
, NULL
);
1133 static struct attribute
* raw3270_attrs
[] = {
1134 &dev_attr_model
.attr
,
1135 &dev_attr_rows
.attr
,
1136 &dev_attr_columns
.attr
,
1140 static struct attribute_group raw3270_attr_group
= {
1141 .attrs
= raw3270_attrs
,
1144 static int raw3270_create_attributes(struct raw3270
*rp
)
1148 rc
= sysfs_create_group(&rp
->cdev
->dev
.kobj
, &raw3270_attr_group
);
1152 rp
->clttydev
= device_create(class3270
, &rp
->cdev
->dev
,
1153 MKDEV(IBM_TTY3270_MAJOR
, rp
->minor
), NULL
,
1154 "tty%s", dev_name(&rp
->cdev
->dev
));
1155 if (IS_ERR(rp
->clttydev
)) {
1156 rc
= PTR_ERR(rp
->clttydev
);
1160 rp
->cltubdev
= device_create(class3270
, &rp
->cdev
->dev
,
1161 MKDEV(IBM_FS3270_MAJOR
, rp
->minor
), NULL
,
1162 "tub%s", dev_name(&rp
->cdev
->dev
));
1163 if (!IS_ERR(rp
->cltubdev
))
1166 rc
= PTR_ERR(rp
->cltubdev
);
1167 device_destroy(class3270
, MKDEV(IBM_TTY3270_MAJOR
, rp
->minor
));
1170 sysfs_remove_group(&rp
->cdev
->dev
.kobj
, &raw3270_attr_group
);
1176 * Notifier for device addition/removal
1178 struct raw3270_notifier
{
1179 struct list_head list
;
1180 void (*notifier
)(int, int);
1183 static LIST_HEAD(raw3270_notifier
);
1185 int raw3270_register_notifier(void (*notifier
)(int, int))
1187 struct raw3270_notifier
*np
;
1190 np
= kmalloc(sizeof(struct raw3270_notifier
), GFP_KERNEL
);
1193 np
->notifier
= notifier
;
1194 mutex_lock(&raw3270_mutex
);
1195 list_add_tail(&np
->list
, &raw3270_notifier
);
1196 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1197 get_device(&rp
->cdev
->dev
);
1198 notifier(rp
->minor
, 1);
1200 mutex_unlock(&raw3270_mutex
);
1204 void raw3270_unregister_notifier(void (*notifier
)(int, int))
1206 struct raw3270_notifier
*np
;
1208 mutex_lock(&raw3270_mutex
);
1209 list_for_each_entry(np
, &raw3270_notifier
, list
)
1210 if (np
->notifier
== notifier
) {
1211 list_del(&np
->list
);
1215 mutex_unlock(&raw3270_mutex
);
1219 * Set 3270 device online.
1222 raw3270_set_online (struct ccw_device
*cdev
)
1225 struct raw3270_notifier
*np
;
1228 rp
= raw3270_create_device(cdev
);
1231 rc
= raw3270_reset_device(rp
);
1234 rc
= raw3270_size_device(rp
);
1237 rc
= raw3270_reset_device(rp
);
1240 rc
= raw3270_create_attributes(rp
);
1243 set_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
1244 mutex_lock(&raw3270_mutex
);
1245 list_for_each_entry(np
, &raw3270_notifier
, list
)
1246 np
->notifier(rp
->minor
, 1);
1247 mutex_unlock(&raw3270_mutex
);
1251 raw3270_delete_device(rp
);
1256 * Remove 3270 device structure.
1259 raw3270_remove (struct ccw_device
*cdev
)
1261 unsigned long flags
;
1263 struct raw3270_view
*v
;
1264 struct raw3270_notifier
*np
;
1266 rp
= dev_get_drvdata(&cdev
->dev
);
1268 * _remove is the opposite of _probe; it's probe that
1269 * should set up rp. raw3270_remove gets entered for
1270 * devices even if they haven't been varied online.
1271 * Thus, rp may validly be NULL here.
1275 clear_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
1277 sysfs_remove_group(&cdev
->dev
.kobj
, &raw3270_attr_group
);
1279 /* Deactivate current view and remove all views. */
1280 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1282 rp
->view
->fn
->deactivate(rp
->view
);
1285 while (!list_empty(&rp
->view_list
)) {
1286 v
= list_entry(rp
->view_list
.next
, struct raw3270_view
, list
);
1289 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1290 raw3270_del_view(v
);
1291 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1293 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1295 mutex_lock(&raw3270_mutex
);
1296 list_for_each_entry(np
, &raw3270_notifier
, list
)
1297 np
->notifier(rp
->minor
, 0);
1298 mutex_unlock(&raw3270_mutex
);
1300 /* Reset 3270 device. */
1301 raw3270_reset_device(rp
);
1302 /* And finally remove it. */
1303 raw3270_delete_device(rp
);
1307 * Set 3270 device offline.
1310 raw3270_set_offline (struct ccw_device
*cdev
)
1314 rp
= dev_get_drvdata(&cdev
->dev
);
1315 if (test_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
))
1317 raw3270_remove(cdev
);
1321 static int raw3270_pm_stop(struct ccw_device
*cdev
)
1324 struct raw3270_view
*view
;
1325 unsigned long flags
;
1327 rp
= dev_get_drvdata(&cdev
->dev
);
1330 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1332 rp
->view
->fn
->deactivate(rp
->view
);
1333 if (!test_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
)) {
1335 * Release tty and fullscreen for all non-console
1338 list_for_each_entry(view
, &rp
->view_list
, list
) {
1339 if (view
->fn
->release
)
1340 view
->fn
->release(view
);
1343 set_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
);
1344 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1348 static int raw3270_pm_start(struct ccw_device
*cdev
)
1351 unsigned long flags
;
1353 rp
= dev_get_drvdata(&cdev
->dev
);
1356 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1357 clear_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
);
1359 rp
->view
->fn
->activate(rp
->view
);
1360 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1364 void raw3270_pm_unfreeze(struct raw3270_view
*view
)
1366 #ifdef CONFIG_TN3270_CONSOLE
1370 if (rp
&& test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
1371 ccw_device_force_console();
1375 static struct ccw_device_id raw3270_id
[] = {
1376 { CCW_DEVICE(0x3270, 0) },
1377 { CCW_DEVICE(0x3271, 0) },
1378 { CCW_DEVICE(0x3272, 0) },
1379 { CCW_DEVICE(0x3273, 0) },
1380 { CCW_DEVICE(0x3274, 0) },
1381 { CCW_DEVICE(0x3275, 0) },
1382 { CCW_DEVICE(0x3276, 0) },
1383 { CCW_DEVICE(0x3277, 0) },
1384 { CCW_DEVICE(0x3278, 0) },
1385 { CCW_DEVICE(0x3279, 0) },
1386 { CCW_DEVICE(0x3174, 0) },
1387 { /* end of list */ },
1390 static struct ccw_driver raw3270_ccw_driver
= {
1393 .owner
= THIS_MODULE
,
1396 .probe
= &raw3270_probe
,
1397 .remove
= &raw3270_remove
,
1398 .set_online
= &raw3270_set_online
,
1399 .set_offline
= &raw3270_set_offline
,
1400 .freeze
= &raw3270_pm_stop
,
1401 .thaw
= &raw3270_pm_start
,
1402 .restore
= &raw3270_pm_start
,
1411 if (raw3270_registered
)
1413 raw3270_registered
= 1;
1414 rc
= ccw_driver_register(&raw3270_ccw_driver
);
1416 /* Create attributes for early (= console) device. */
1417 mutex_lock(&raw3270_mutex
);
1418 class3270
= class_create(THIS_MODULE
, "3270");
1419 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1420 get_device(&rp
->cdev
->dev
);
1421 raw3270_create_attributes(rp
);
1423 mutex_unlock(&raw3270_mutex
);
1431 ccw_driver_unregister(&raw3270_ccw_driver
);
1432 class_destroy(class3270
);
1435 MODULE_LICENSE("GPL");
1437 module_init(raw3270_init
);
1438 module_exit(raw3270_exit
);
1440 EXPORT_SYMBOL(raw3270_request_alloc
);
1441 EXPORT_SYMBOL(raw3270_request_free
);
1442 EXPORT_SYMBOL(raw3270_request_reset
);
1443 EXPORT_SYMBOL(raw3270_request_set_cmd
);
1444 EXPORT_SYMBOL(raw3270_request_add_data
);
1445 EXPORT_SYMBOL(raw3270_request_set_data
);
1446 EXPORT_SYMBOL(raw3270_request_set_idal
);
1447 EXPORT_SYMBOL(raw3270_buffer_address
);
1448 EXPORT_SYMBOL(raw3270_add_view
);
1449 EXPORT_SYMBOL(raw3270_del_view
);
1450 EXPORT_SYMBOL(raw3270_find_view
);
1451 EXPORT_SYMBOL(raw3270_activate_view
);
1452 EXPORT_SYMBOL(raw3270_deactivate_view
);
1453 EXPORT_SYMBOL(raw3270_start
);
1454 EXPORT_SYMBOL(raw3270_start_locked
);
1455 EXPORT_SYMBOL(raw3270_start_irq
);
1456 EXPORT_SYMBOL(raw3270_reset
);
1457 EXPORT_SYMBOL(raw3270_register_notifier
);
1458 EXPORT_SYMBOL(raw3270_unregister_notifier
);
1459 EXPORT_SYMBOL(raw3270_wait_queue
);