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/module.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/wait.h>
19 #include <asm/ccwdev.h>
21 #include <asm/ebcdic.h>
26 #include <linux/major.h>
27 #include <linux/kdev_t.h>
28 #include <linux/device.h>
29 #include <linux/mutex.h>
31 static struct class *class3270
;
33 /* The main 3270 data structure. */
35 struct list_head list
;
36 struct ccw_device
*cdev
;
39 short model
, rows
, cols
;
42 struct list_head req_queue
; /* Request queue. */
43 struct list_head view_list
; /* List of available views. */
44 struct raw3270_view
*view
; /* Active view. */
46 struct timer_list timer
; /* Device timer. */
48 unsigned char *ascebc
; /* ascii -> ebcdic table */
49 struct device
*clttydev
; /* 3270-class tty device ptr */
50 struct device
*cltubdev
; /* 3270-class tub device ptr */
52 struct raw3270_request init_request
;
53 unsigned char init_data
[256];
57 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
58 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
59 #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
60 #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
61 #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
62 #define RAW3270_FLAGS_FROZEN 16 /* set if 3270 is frozen for suspend */
64 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
65 static DEFINE_MUTEX(raw3270_mutex
);
67 /* List of 3270 devices. */
68 static LIST_HEAD(raw3270_devices
);
71 * Flag to indicate if the driver has been registered. Some operations
72 * like waiting for the end of i/o need to be done differently as long
73 * as the kernel is still starting up (console support).
75 static int raw3270_registered
;
77 /* Module parameters */
78 static int tubxcorrect
= 0;
79 module_param(tubxcorrect
, bool, 0);
82 * Wait queue for device init/delete, view delete.
84 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue
);
87 * Encode array for 12 bit 3270 addresses.
89 static unsigned char raw3270_ebcgraf
[64] = {
90 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
91 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
92 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
93 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
94 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
95 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
96 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
97 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
101 raw3270_buffer_address(struct raw3270
*rp
, char *cp
, unsigned short addr
)
103 if (test_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
)) {
104 cp
[0] = (addr
>> 8) & 0x3f;
107 cp
[0] = raw3270_ebcgraf
[(addr
>> 6) & 0x3f];
108 cp
[1] = raw3270_ebcgraf
[addr
& 0x3f];
113 * Allocate a new 3270 ccw request
115 struct raw3270_request
*
116 raw3270_request_alloc(size_t size
)
118 struct raw3270_request
*rq
;
120 /* Allocate request structure */
121 rq
= kzalloc(sizeof(struct raw3270_request
), GFP_KERNEL
| GFP_DMA
);
123 return ERR_PTR(-ENOMEM
);
125 /* alloc output buffer. */
127 rq
->buffer
= kmalloc(size
, GFP_KERNEL
| GFP_DMA
);
130 return ERR_PTR(-ENOMEM
);
134 INIT_LIST_HEAD(&rq
->list
);
139 rq
->ccw
.cda
= __pa(rq
->buffer
);
140 rq
->ccw
.flags
= CCW_FLAG_SLI
;
146 * Free 3270 ccw request
149 raw3270_request_free (struct raw3270_request
*rq
)
156 * Reset request to initial state.
159 raw3270_request_reset(struct raw3270_request
*rq
)
161 BUG_ON(!list_empty(&rq
->list
));
162 rq
->ccw
.cmd_code
= 0;
164 rq
->ccw
.cda
= __pa(rq
->buffer
);
165 rq
->ccw
.flags
= CCW_FLAG_SLI
;
171 * Set command code to ccw of a request.
174 raw3270_request_set_cmd(struct raw3270_request
*rq
, u8 cmd
)
176 rq
->ccw
.cmd_code
= cmd
;
180 * Add data fragment to output buffer.
183 raw3270_request_add_data(struct raw3270_request
*rq
, void *data
, size_t size
)
185 if (size
+ rq
->ccw
.count
> rq
->size
)
187 memcpy(rq
->buffer
+ rq
->ccw
.count
, data
, size
);
188 rq
->ccw
.count
+= size
;
193 * Set address/length pair to ccw of a request.
196 raw3270_request_set_data(struct raw3270_request
*rq
, void *data
, size_t size
)
198 rq
->ccw
.cda
= __pa(data
);
199 rq
->ccw
.count
= size
;
203 * Set idal buffer to ccw of a request.
206 raw3270_request_set_idal(struct raw3270_request
*rq
, struct idal_buffer
*ib
)
208 rq
->ccw
.cda
= __pa(ib
->data
);
209 rq
->ccw
.count
= ib
->size
;
210 rq
->ccw
.flags
|= CCW_FLAG_IDA
;
217 raw3270_halt_io_nolock(struct raw3270
*rp
, struct raw3270_request
*rq
)
222 if (raw3270_request_final(rq
))
224 /* Check if interrupt has already been processed */
225 for (retries
= 0; retries
< 5; retries
++) {
227 rc
= ccw_device_halt(rp
->cdev
, (long) rq
);
229 rc
= ccw_device_clear(rp
->cdev
, (long) rq
);
231 break; /* termination successful */
237 raw3270_halt_io(struct raw3270
*rp
, struct raw3270_request
*rq
)
242 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
243 rc
= raw3270_halt_io_nolock(rp
, rq
);
244 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
249 * Add the request to the request queue, try to start it if the
250 * 3270 device is idle. Return without waiting for end of i/o.
253 __raw3270_start(struct raw3270
*rp
, struct raw3270_view
*view
,
254 struct raw3270_request
*rq
)
257 raw3270_get_view(view
);
258 if (list_empty(&rp
->req_queue
) &&
259 !test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
)) {
260 /* No other requests are on the queue. Start this one. */
261 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
262 (unsigned long) rq
, 0, 0);
264 raw3270_put_view(view
);
268 list_add_tail(&rq
->list
, &rp
->req_queue
);
273 raw3270_start(struct raw3270_view
*view
, struct raw3270_request
*rq
)
279 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
281 if (!rp
|| rp
->view
!= view
||
282 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
284 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
287 rc
= __raw3270_start(rp
, view
, rq
);
288 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
293 raw3270_start_locked(struct raw3270_view
*view
, struct raw3270_request
*rq
)
299 if (!rp
|| rp
->view
!= view
||
300 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
302 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
305 rc
= __raw3270_start(rp
, view
, rq
);
310 raw3270_start_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
)
316 raw3270_get_view(view
);
317 list_add_tail(&rq
->list
, &rp
->req_queue
);
322 * 3270 interrupt routine, called from the ccw_device layer
325 raw3270_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
328 struct raw3270_view
*view
;
329 struct raw3270_request
*rq
;
332 rp
= dev_get_drvdata(&cdev
->dev
);
335 rq
= (struct raw3270_request
*) intparm
;
336 view
= rq
? rq
->view
: rp
->view
;
339 rc
= RAW3270_IO_RETRY
;
340 else if (irb
->scsw
.cmd
.fctl
& SCSW_FCTL_HALT_FUNC
) {
342 rc
= RAW3270_IO_DONE
;
343 } else if (irb
->scsw
.cmd
.dstat
== (DEV_STAT_CHN_END
| DEV_STAT_DEV_END
|
344 DEV_STAT_UNIT_EXCEP
)) {
345 /* Handle CE-DE-UE and subsequent UDE */
346 set_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
347 rc
= RAW3270_IO_BUSY
;
348 } else if (test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
)) {
349 /* Wait for UDE if busy flag is set. */
350 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_DEV_END
) {
351 clear_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
352 /* Got it, now retry. */
353 rc
= RAW3270_IO_RETRY
;
355 rc
= RAW3270_IO_BUSY
;
357 rc
= view
->fn
->intv(view
, rq
, irb
);
359 rc
= RAW3270_IO_DONE
;
362 case RAW3270_IO_DONE
:
364 case RAW3270_IO_BUSY
:
366 * Intervention required by the operator. We have to wait
367 * for unsolicited device end.
370 case RAW3270_IO_RETRY
:
373 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
374 (unsigned long) rq
, 0, 0);
376 return; /* Sucessfully restarted. */
378 case RAW3270_IO_STOP
:
381 raw3270_halt_io_nolock(rp
, rq
);
388 BUG_ON(list_empty(&rq
->list
));
389 /* The request completed, remove from queue and do callback. */
390 list_del_init(&rq
->list
);
392 rq
->callback(rq
, rq
->callback_data
);
393 /* Do put_device for get_device in raw3270_start. */
394 raw3270_put_view(view
);
397 * Try to start each request on request queue until one is
398 * started successful.
400 while (!list_empty(&rp
->req_queue
)) {
401 rq
= list_entry(rp
->req_queue
.next
,struct raw3270_request
,list
);
402 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
403 (unsigned long) rq
, 0, 0);
406 /* Start failed. Remove request and do callback. */
407 list_del_init(&rq
->list
);
409 rq
->callback(rq
, rq
->callback_data
);
410 /* Do put_device for get_device in raw3270_start. */
411 raw3270_put_view(view
);
419 struct raw3270_ua
{ /* Query Reply structure for Usable Area */
420 struct { /* Usable Area Query Reply Base */
421 short l
; /* Length of this structured field */
422 char sfid
; /* 0x81 if Query Reply */
423 char qcode
; /* 0x81 if Usable Area */
426 short w
; /* Width of usable area */
427 short h
; /* Heigth of usavle area */
428 char units
; /* 0x00:in; 0x01:mm */
433 short buffsz
; /* Character buffer size, bytes */
438 } __attribute__ ((packed
)) uab
;
439 struct { /* Alternate Usable Area Self-Defining Parameter */
440 char l
; /* Length of this Self-Defining Parm */
441 char sdpid
; /* 0x02 if Alternate Usable Area */
443 char auaid
; /* 0x01 is Id for the A U A */
444 short wauai
; /* Width of AUAi */
445 short hauai
; /* Height of AUAi */
446 char auaunits
; /* 0x00:in, 0x01:mm */
451 } __attribute__ ((packed
)) aua
;
452 } __attribute__ ((packed
));
454 static struct diag210 raw3270_init_diag210
;
455 static DEFINE_MUTEX(raw3270_init_mutex
);
458 raw3270_init_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
,
462 * Unit-Check Processing:
463 * Expect Command Reject or Intervention Required.
465 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
) {
466 /* Request finished abnormally. */
467 if (irb
->ecw
[0] & SNS0_INTERVENTION_REQ
) {
468 set_bit(RAW3270_FLAGS_BUSY
, &view
->dev
->flags
);
469 return RAW3270_IO_BUSY
;
473 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
) {
474 if (irb
->ecw
[0] & SNS0_CMD_REJECT
)
475 rq
->rc
= -EOPNOTSUPP
;
479 /* Request finished normally. Copy residual count. */
480 rq
->rescnt
= irb
->scsw
.cmd
.count
;
482 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_ATTENTION
) {
483 set_bit(RAW3270_FLAGS_ATTN
, &view
->dev
->flags
);
484 wake_up(&raw3270_wait_queue
);
486 return RAW3270_IO_DONE
;
489 static struct raw3270_fn raw3270_init_fn
= {
490 .intv
= raw3270_init_irq
493 static struct raw3270_view raw3270_init_view
= {
494 .fn
= &raw3270_init_fn
498 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
499 * Wait for end of request. The request must have been started
500 * with raw3270_start, rc = 0. The device lock may NOT have been
501 * released between calling raw3270_start and raw3270_wait.
504 raw3270_wake_init(struct raw3270_request
*rq
, void *data
)
506 wake_up((wait_queue_head_t
*) data
);
510 * Special wait function that can cope with console initialization.
513 raw3270_start_init(struct raw3270
*rp
, struct raw3270_view
*view
,
514 struct raw3270_request
*rq
)
519 #ifdef CONFIG_TN3270_CONSOLE
520 if (raw3270_registered
== 0) {
521 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
523 rc
= __raw3270_start(rp
, view
, rq
);
525 while (!raw3270_request_final(rq
)) {
529 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
533 rq
->callback
= raw3270_wake_init
;
534 rq
->callback_data
= &raw3270_wait_queue
;
535 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
536 rc
= __raw3270_start(rp
, view
, rq
);
537 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
540 /* Now wait for the completion. */
541 rc
= wait_event_interruptible(raw3270_wait_queue
,
542 raw3270_request_final(rq
));
543 if (rc
== -ERESTARTSYS
) { /* Interrupted by a signal. */
544 raw3270_halt_io(view
->dev
, rq
);
545 /* No wait for the halt to complete. */
546 wait_event(raw3270_wait_queue
, raw3270_request_final(rq
));
553 __raw3270_size_device_vm(struct raw3270
*rp
)
556 struct ccw_dev_id dev_id
;
558 ccw_device_get_id(rp
->cdev
, &dev_id
);
559 raw3270_init_diag210
.vrdcdvno
= dev_id
.devno
;
560 raw3270_init_diag210
.vrdclen
= sizeof(struct diag210
);
561 rc
= diag210(&raw3270_init_diag210
);
564 model
= raw3270_init_diag210
.vrdccrmd
;
594 __raw3270_size_device(struct raw3270
*rp
)
596 static const unsigned char wbuf
[] =
597 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
598 struct raw3270_ua
*uap
;
599 unsigned short count
;
603 * To determine the size of the 3270 device we need to do:
604 * 1) send a 'read partition' data stream to the device
605 * 2) wait for the attn interrupt that preceeds the query reply
606 * 3) do a read modified to get the query reply
607 * To make things worse we have to cope with intervention
608 * required (3270 device switched to 'stand-by') and command
609 * rejects (old devices that can't do 'read partition').
611 memset(&rp
->init_request
, 0, sizeof(rp
->init_request
));
612 memset(&rp
->init_data
, 0, 256);
613 /* Store 'read partition' data stream to init_data */
614 memcpy(&rp
->init_data
, wbuf
, sizeof(wbuf
));
615 INIT_LIST_HEAD(&rp
->init_request
.list
);
616 rp
->init_request
.ccw
.cmd_code
= TC_WRITESF
;
617 rp
->init_request
.ccw
.flags
= CCW_FLAG_SLI
;
618 rp
->init_request
.ccw
.count
= sizeof(wbuf
);
619 rp
->init_request
.ccw
.cda
= (__u32
) __pa(&rp
->init_data
);
621 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &rp
->init_request
);
623 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
626 /* Wait for attention interrupt. */
627 #ifdef CONFIG_TN3270_CONSOLE
628 if (raw3270_registered
== 0) {
631 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
632 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN
, &rp
->flags
))
634 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
637 rc
= wait_event_interruptible(raw3270_wait_queue
,
638 test_and_clear_bit(RAW3270_FLAGS_ATTN
, &rp
->flags
));
643 * The device accepted the 'read partition' command. Now
644 * set up a read ccw and issue it.
646 rp
->init_request
.ccw
.cmd_code
= TC_READMOD
;
647 rp
->init_request
.ccw
.flags
= CCW_FLAG_SLI
;
648 rp
->init_request
.ccw
.count
= sizeof(rp
->init_data
);
649 rp
->init_request
.ccw
.cda
= (__u32
) __pa(rp
->init_data
);
650 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &rp
->init_request
);
653 /* Got a Query Reply */
654 count
= sizeof(rp
->init_data
) - rp
->init_request
.rescnt
;
655 uap
= (struct raw3270_ua
*) (rp
->init_data
+ 1);
656 /* Paranoia check. */
657 if (rp
->init_data
[0] != 0x88 || uap
->uab
.qcode
!= 0x81)
659 /* Copy rows/columns of default Usable Area */
660 rp
->rows
= uap
->uab
.h
;
661 rp
->cols
= uap
->uab
.w
;
662 /* Check for 14 bit addressing */
663 if ((uap
->uab
.flags0
& 0x0d) == 0x01)
664 set_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
);
665 /* Check for Alternate Usable Area */
666 if (uap
->uab
.l
== sizeof(struct raw3270_ua
) &&
667 uap
->aua
.sdpid
== 0x02) {
668 rp
->rows
= uap
->aua
.hauai
;
669 rp
->cols
= uap
->aua
.wauai
;
675 raw3270_size_device(struct raw3270
*rp
)
679 mutex_lock(&raw3270_init_mutex
);
680 rp
->view
= &raw3270_init_view
;
681 raw3270_init_view
.dev
= rp
;
683 rc
= __raw3270_size_device_vm(rp
);
685 rc
= __raw3270_size_device(rp
);
686 raw3270_init_view
.dev
= NULL
;
688 mutex_unlock(&raw3270_init_mutex
);
689 if (rc
== 0) { /* Found something. */
690 /* Try to find a model. */
692 if (rp
->rows
== 24 && rp
->cols
== 80)
694 if (rp
->rows
== 32 && rp
->cols
== 80)
696 if (rp
->rows
== 43 && rp
->cols
== 80)
698 if (rp
->rows
== 27 && rp
->cols
== 132)
701 /* Couldn't detect size. Use default model 2. */
711 raw3270_reset_device(struct raw3270
*rp
)
715 mutex_lock(&raw3270_init_mutex
);
716 memset(&rp
->init_request
, 0, sizeof(rp
->init_request
));
717 memset(&rp
->init_data
, 0, sizeof(rp
->init_data
));
718 /* Store reset data stream to init_data/init_request */
719 rp
->init_data
[0] = TW_KR
;
720 INIT_LIST_HEAD(&rp
->init_request
.list
);
721 rp
->init_request
.ccw
.cmd_code
= TC_EWRITEA
;
722 rp
->init_request
.ccw
.flags
= CCW_FLAG_SLI
;
723 rp
->init_request
.ccw
.count
= 1;
724 rp
->init_request
.ccw
.cda
= (__u32
) __pa(rp
->init_data
);
725 rp
->view
= &raw3270_init_view
;
726 raw3270_init_view
.dev
= rp
;
727 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &rp
->init_request
);
728 raw3270_init_view
.dev
= NULL
;
730 mutex_unlock(&raw3270_init_mutex
);
735 raw3270_reset(struct raw3270_view
*view
)
741 if (!rp
|| rp
->view
!= view
||
742 test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
744 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
747 rc
= raw3270_reset_device(view
->dev
);
752 * Setup new 3270 device.
755 raw3270_setup_device(struct ccw_device
*cdev
, struct raw3270
*rp
, char *ascebc
)
761 memset(rp
, 0, sizeof(struct raw3270
));
762 /* Copy ebcdic -> ascii translation table. */
763 memcpy(ascebc
, _ascebc
, 256);
765 /* correct brackets and circumflex */
776 INIT_LIST_HEAD(&rp
->req_queue
);
777 INIT_LIST_HEAD(&rp
->view_list
);
780 * Add device to list and find the smallest unused minor
781 * number for it. Note: there is no device with minor 0,
782 * see special case for fs3270.c:fs3270_open().
784 mutex_lock(&raw3270_mutex
);
785 /* Keep the list sorted. */
786 minor
= RAW3270_FIRSTMINOR
;
788 list_for_each(l
, &raw3270_devices
) {
789 tmp
= list_entry(l
, struct raw3270
, list
);
790 if (tmp
->minor
> minor
) {
792 __list_add(&rp
->list
, l
->prev
, l
);
797 if (rp
->minor
== -1 && minor
< RAW3270_MAXDEVS
+ RAW3270_FIRSTMINOR
) {
799 list_add_tail(&rp
->list
, &raw3270_devices
);
801 mutex_unlock(&raw3270_mutex
);
802 /* No free minor number? Then give up. */
806 dev_set_drvdata(&cdev
->dev
, rp
);
807 cdev
->handler
= raw3270_irq
;
811 #ifdef CONFIG_TN3270_CONSOLE
813 * Setup 3270 device configured as console.
815 struct raw3270 __init
*raw3270_setup_console(struct ccw_device
*cdev
)
821 rp
= kzalloc(sizeof(struct raw3270
), GFP_KERNEL
| GFP_DMA
);
822 ascebc
= kzalloc(256, GFP_KERNEL
);
823 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
826 set_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
);
827 rc
= raw3270_reset_device(rp
);
830 rc
= raw3270_size_device(rp
);
833 rc
= raw3270_reset_device(rp
);
836 set_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
841 raw3270_wait_cons_dev(struct raw3270
*rp
)
845 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
847 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
853 * Create a 3270 device structure.
855 static struct raw3270
*
856 raw3270_create_device(struct ccw_device
*cdev
)
862 rp
= kmalloc(sizeof(struct raw3270
), GFP_KERNEL
| GFP_DMA
);
864 return ERR_PTR(-ENOMEM
);
865 ascebc
= kmalloc(256, GFP_KERNEL
);
868 return ERR_PTR(-ENOMEM
);
870 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
876 /* Get reference to ccw_device structure. */
877 get_device(&cdev
->dev
);
885 raw3270_activate_view(struct raw3270_view
*view
)
888 struct raw3270_view
*oldview
, *nv
;
895 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
896 if (rp
->view
== view
)
898 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
900 else if (test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
906 oldview
->fn
->deactivate(oldview
);
909 rc
= view
->fn
->activate(view
);
911 /* Didn't work. Try to reactivate the old view. */
913 if (!oldview
|| oldview
->fn
->activate(oldview
) != 0) {
914 /* Didn't work as well. Try any other view. */
915 list_for_each_entry(nv
, &rp
->view_list
, list
)
916 if (nv
!= view
&& nv
!= oldview
) {
918 if (nv
->fn
->activate(nv
) == 0)
925 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
930 * Deactivate current view.
933 raw3270_deactivate_view(struct raw3270_view
*view
)
941 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
942 if (rp
->view
== view
) {
943 view
->fn
->deactivate(view
);
945 /* Move deactivated view to end of list. */
946 list_del_init(&view
->list
);
947 list_add_tail(&view
->list
, &rp
->view_list
);
948 /* Try to activate another view. */
949 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
) &&
950 !test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
)) {
951 list_for_each_entry(view
, &rp
->view_list
, list
) {
953 if (view
->fn
->activate(view
) == 0)
959 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
963 * Add view to device with minor "minor".
966 raw3270_add_view(struct raw3270_view
*view
, struct raw3270_fn
*fn
, int minor
)
974 mutex_lock(&raw3270_mutex
);
976 list_for_each_entry(rp
, &raw3270_devices
, list
) {
977 if (rp
->minor
!= minor
)
979 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
980 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
981 atomic_set(&view
->ref_count
, 2);
984 view
->model
= rp
->model
;
985 view
->rows
= rp
->rows
;
986 view
->cols
= rp
->cols
;
987 view
->ascebc
= rp
->ascebc
;
988 spin_lock_init(&view
->lock
);
989 list_add(&view
->list
, &rp
->view_list
);
992 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
995 mutex_unlock(&raw3270_mutex
);
1000 * Find specific view of device with minor "minor".
1002 struct raw3270_view
*
1003 raw3270_find_view(struct raw3270_fn
*fn
, int minor
)
1006 struct raw3270_view
*view
, *tmp
;
1007 unsigned long flags
;
1009 mutex_lock(&raw3270_mutex
);
1010 view
= ERR_PTR(-ENODEV
);
1011 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1012 if (rp
->minor
!= minor
)
1014 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1015 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
1016 view
= ERR_PTR(-ENOENT
);
1017 list_for_each_entry(tmp
, &rp
->view_list
, list
) {
1018 if (tmp
->fn
== fn
) {
1019 raw3270_get_view(tmp
);
1025 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1028 mutex_unlock(&raw3270_mutex
);
1033 * Remove view from device and free view structure via call to view->fn->free.
1036 raw3270_del_view(struct raw3270_view
*view
)
1038 unsigned long flags
;
1040 struct raw3270_view
*nv
;
1043 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1044 if (rp
->view
== view
) {
1045 view
->fn
->deactivate(view
);
1048 list_del_init(&view
->list
);
1049 if (!rp
->view
&& test_bit(RAW3270_FLAGS_READY
, &rp
->flags
) &&
1050 !test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
)) {
1051 /* Try to activate another view. */
1052 list_for_each_entry(nv
, &rp
->view_list
, list
) {
1053 if (nv
->fn
->activate(nv
) == 0) {
1059 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1060 /* Wait for reference counter to drop to zero. */
1061 atomic_dec(&view
->ref_count
);
1062 wait_event(raw3270_wait_queue
, atomic_read(&view
->ref_count
) == 0);
1064 view
->fn
->free(view
);
1068 * Remove a 3270 device structure.
1071 raw3270_delete_device(struct raw3270
*rp
)
1073 struct ccw_device
*cdev
;
1075 /* Remove from device chain. */
1076 mutex_lock(&raw3270_mutex
);
1077 if (rp
->clttydev
&& !IS_ERR(rp
->clttydev
))
1078 device_destroy(class3270
, MKDEV(IBM_TTY3270_MAJOR
, rp
->minor
));
1079 if (rp
->cltubdev
&& !IS_ERR(rp
->cltubdev
))
1080 device_destroy(class3270
, MKDEV(IBM_FS3270_MAJOR
, rp
->minor
));
1081 list_del_init(&rp
->list
);
1082 mutex_unlock(&raw3270_mutex
);
1084 /* Disconnect from ccw_device. */
1087 dev_set_drvdata(&cdev
->dev
, NULL
);
1088 cdev
->handler
= NULL
;
1090 /* Put ccw_device structure. */
1091 put_device(&cdev
->dev
);
1093 /* Now free raw3270 structure. */
1099 raw3270_probe (struct ccw_device
*cdev
)
1105 * Additional attributes for a 3270 device
1108 raw3270_model_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1110 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1111 ((struct raw3270
*) dev_get_drvdata(dev
))->model
);
1113 static DEVICE_ATTR(model
, 0444, raw3270_model_show
, NULL
);
1116 raw3270_rows_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1118 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1119 ((struct raw3270
*) dev_get_drvdata(dev
))->rows
);
1121 static DEVICE_ATTR(rows
, 0444, raw3270_rows_show
, NULL
);
1124 raw3270_columns_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1126 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1127 ((struct raw3270
*) dev_get_drvdata(dev
))->cols
);
1129 static DEVICE_ATTR(columns
, 0444, raw3270_columns_show
, NULL
);
1131 static struct attribute
* raw3270_attrs
[] = {
1132 &dev_attr_model
.attr
,
1133 &dev_attr_rows
.attr
,
1134 &dev_attr_columns
.attr
,
1138 static struct attribute_group raw3270_attr_group
= {
1139 .attrs
= raw3270_attrs
,
1142 static int raw3270_create_attributes(struct raw3270
*rp
)
1146 rc
= sysfs_create_group(&rp
->cdev
->dev
.kobj
, &raw3270_attr_group
);
1150 rp
->clttydev
= device_create(class3270
, &rp
->cdev
->dev
,
1151 MKDEV(IBM_TTY3270_MAJOR
, rp
->minor
), NULL
,
1152 "tty%s", dev_name(&rp
->cdev
->dev
));
1153 if (IS_ERR(rp
->clttydev
)) {
1154 rc
= PTR_ERR(rp
->clttydev
);
1158 rp
->cltubdev
= device_create(class3270
, &rp
->cdev
->dev
,
1159 MKDEV(IBM_FS3270_MAJOR
, rp
->minor
), NULL
,
1160 "tub%s", dev_name(&rp
->cdev
->dev
));
1161 if (!IS_ERR(rp
->cltubdev
))
1164 rc
= PTR_ERR(rp
->cltubdev
);
1165 device_destroy(class3270
, MKDEV(IBM_TTY3270_MAJOR
, rp
->minor
));
1168 sysfs_remove_group(&rp
->cdev
->dev
.kobj
, &raw3270_attr_group
);
1174 * Notifier for device addition/removal
1176 struct raw3270_notifier
{
1177 struct list_head list
;
1178 void (*notifier
)(int, int);
1181 static LIST_HEAD(raw3270_notifier
);
1183 int raw3270_register_notifier(void (*notifier
)(int, int))
1185 struct raw3270_notifier
*np
;
1188 np
= kmalloc(sizeof(struct raw3270_notifier
), GFP_KERNEL
);
1191 np
->notifier
= notifier
;
1192 mutex_lock(&raw3270_mutex
);
1193 list_add_tail(&np
->list
, &raw3270_notifier
);
1194 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1195 get_device(&rp
->cdev
->dev
);
1196 notifier(rp
->minor
, 1);
1198 mutex_unlock(&raw3270_mutex
);
1202 void raw3270_unregister_notifier(void (*notifier
)(int, int))
1204 struct raw3270_notifier
*np
;
1206 mutex_lock(&raw3270_mutex
);
1207 list_for_each_entry(np
, &raw3270_notifier
, list
)
1208 if (np
->notifier
== notifier
) {
1209 list_del(&np
->list
);
1213 mutex_unlock(&raw3270_mutex
);
1217 * Set 3270 device online.
1220 raw3270_set_online (struct ccw_device
*cdev
)
1223 struct raw3270_notifier
*np
;
1226 rp
= raw3270_create_device(cdev
);
1229 rc
= raw3270_reset_device(rp
);
1232 rc
= raw3270_size_device(rp
);
1235 rc
= raw3270_reset_device(rp
);
1238 rc
= raw3270_create_attributes(rp
);
1241 set_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
1242 mutex_lock(&raw3270_mutex
);
1243 list_for_each_entry(np
, &raw3270_notifier
, list
)
1244 np
->notifier(rp
->minor
, 1);
1245 mutex_unlock(&raw3270_mutex
);
1249 raw3270_delete_device(rp
);
1254 * Remove 3270 device structure.
1257 raw3270_remove (struct ccw_device
*cdev
)
1259 unsigned long flags
;
1261 struct raw3270_view
*v
;
1262 struct raw3270_notifier
*np
;
1264 rp
= dev_get_drvdata(&cdev
->dev
);
1266 * _remove is the opposite of _probe; it's probe that
1267 * should set up rp. raw3270_remove gets entered for
1268 * devices even if they haven't been varied online.
1269 * Thus, rp may validly be NULL here.
1273 clear_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
1275 sysfs_remove_group(&cdev
->dev
.kobj
, &raw3270_attr_group
);
1277 /* Deactivate current view and remove all views. */
1278 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1280 rp
->view
->fn
->deactivate(rp
->view
);
1283 while (!list_empty(&rp
->view_list
)) {
1284 v
= list_entry(rp
->view_list
.next
, struct raw3270_view
, list
);
1287 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1288 raw3270_del_view(v
);
1289 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1291 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1293 mutex_lock(&raw3270_mutex
);
1294 list_for_each_entry(np
, &raw3270_notifier
, list
)
1295 np
->notifier(rp
->minor
, 0);
1296 mutex_unlock(&raw3270_mutex
);
1298 /* Reset 3270 device. */
1299 raw3270_reset_device(rp
);
1300 /* And finally remove it. */
1301 raw3270_delete_device(rp
);
1305 * Set 3270 device offline.
1308 raw3270_set_offline (struct ccw_device
*cdev
)
1312 rp
= dev_get_drvdata(&cdev
->dev
);
1313 if (test_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
))
1315 raw3270_remove(cdev
);
1319 static int raw3270_pm_stop(struct ccw_device
*cdev
)
1322 struct raw3270_view
*view
;
1323 unsigned long flags
;
1325 rp
= dev_get_drvdata(&cdev
->dev
);
1328 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1330 rp
->view
->fn
->deactivate(rp
->view
);
1331 if (!test_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
)) {
1333 * Release tty and fullscreen for all non-console
1336 list_for_each_entry(view
, &rp
->view_list
, list
) {
1337 if (view
->fn
->release
)
1338 view
->fn
->release(view
);
1341 set_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
);
1342 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1346 static int raw3270_pm_start(struct ccw_device
*cdev
)
1349 unsigned long flags
;
1351 rp
= dev_get_drvdata(&cdev
->dev
);
1354 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1355 clear_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
);
1357 rp
->view
->fn
->activate(rp
->view
);
1358 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1362 void raw3270_pm_unfreeze(struct raw3270_view
*view
)
1364 #ifdef CONFIG_TN3270_CONSOLE
1368 if (rp
&& test_bit(RAW3270_FLAGS_FROZEN
, &rp
->flags
))
1369 ccw_device_force_console();
1373 static struct ccw_device_id raw3270_id
[] = {
1374 { CCW_DEVICE(0x3270, 0) },
1375 { CCW_DEVICE(0x3271, 0) },
1376 { CCW_DEVICE(0x3272, 0) },
1377 { CCW_DEVICE(0x3273, 0) },
1378 { CCW_DEVICE(0x3274, 0) },
1379 { CCW_DEVICE(0x3275, 0) },
1380 { CCW_DEVICE(0x3276, 0) },
1381 { CCW_DEVICE(0x3277, 0) },
1382 { CCW_DEVICE(0x3278, 0) },
1383 { CCW_DEVICE(0x3279, 0) },
1384 { CCW_DEVICE(0x3174, 0) },
1385 { /* end of list */ },
1388 static struct ccw_driver raw3270_ccw_driver
= {
1390 .owner
= THIS_MODULE
,
1392 .probe
= &raw3270_probe
,
1393 .remove
= &raw3270_remove
,
1394 .set_online
= &raw3270_set_online
,
1395 .set_offline
= &raw3270_set_offline
,
1396 .freeze
= &raw3270_pm_stop
,
1397 .thaw
= &raw3270_pm_start
,
1398 .restore
= &raw3270_pm_start
,
1407 if (raw3270_registered
)
1409 raw3270_registered
= 1;
1410 rc
= ccw_driver_register(&raw3270_ccw_driver
);
1412 /* Create attributes for early (= console) device. */
1413 mutex_lock(&raw3270_mutex
);
1414 class3270
= class_create(THIS_MODULE
, "3270");
1415 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1416 get_device(&rp
->cdev
->dev
);
1417 raw3270_create_attributes(rp
);
1419 mutex_unlock(&raw3270_mutex
);
1427 ccw_driver_unregister(&raw3270_ccw_driver
);
1428 class_destroy(class3270
);
1431 MODULE_LICENSE("GPL");
1433 module_init(raw3270_init
);
1434 module_exit(raw3270_exit
);
1436 EXPORT_SYMBOL(raw3270_request_alloc
);
1437 EXPORT_SYMBOL(raw3270_request_free
);
1438 EXPORT_SYMBOL(raw3270_request_reset
);
1439 EXPORT_SYMBOL(raw3270_request_set_cmd
);
1440 EXPORT_SYMBOL(raw3270_request_add_data
);
1441 EXPORT_SYMBOL(raw3270_request_set_data
);
1442 EXPORT_SYMBOL(raw3270_request_set_idal
);
1443 EXPORT_SYMBOL(raw3270_buffer_address
);
1444 EXPORT_SYMBOL(raw3270_add_view
);
1445 EXPORT_SYMBOL(raw3270_del_view
);
1446 EXPORT_SYMBOL(raw3270_find_view
);
1447 EXPORT_SYMBOL(raw3270_activate_view
);
1448 EXPORT_SYMBOL(raw3270_deactivate_view
);
1449 EXPORT_SYMBOL(raw3270_start
);
1450 EXPORT_SYMBOL(raw3270_start_locked
);
1451 EXPORT_SYMBOL(raw3270_start_irq
);
1452 EXPORT_SYMBOL(raw3270_reset
);
1453 EXPORT_SYMBOL(raw3270_register_notifier
);
1454 EXPORT_SYMBOL(raw3270_unregister_notifier
);
1455 EXPORT_SYMBOL(raw3270_wait_queue
);