2 * drivers/s390/char/raw3270.c
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 (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
11 #include <linux/config.h>
12 #include <linux/bootmem.h>
13 #include <linux/module.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/wait.h>
22 #include <asm/ccwdev.h>
24 #include <asm/ebcdic.h>
28 /* The main 3270 data structure. */
30 struct list_head list
;
31 struct ccw_device
*cdev
;
34 short model
, rows
, cols
;
37 struct list_head req_queue
; /* Request queue. */
38 struct list_head view_list
; /* List of available views. */
39 struct raw3270_view
*view
; /* Active view. */
41 struct timer_list timer
; /* Device timer. */
43 unsigned char *ascebc
; /* ascii -> ebcdic table */
47 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
48 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
49 #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
50 #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
51 #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
53 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
54 static DECLARE_MUTEX(raw3270_sem
);
56 /* List of 3270 devices. */
57 static struct list_head raw3270_devices
= LIST_HEAD_INIT(raw3270_devices
);
60 * Flag to indicate if the driver has been registered. Some operations
61 * like waiting for the end of i/o need to be done differently as long
62 * as the kernel is still starting up (console support).
64 static int raw3270_registered
;
66 /* Module parameters */
67 static int tubxcorrect
= 0;
68 module_param(tubxcorrect
, bool, 0);
71 * Wait queue for device init/delete, view delete.
73 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue
);
76 * Encode array for 12 bit 3270 addresses.
78 unsigned char raw3270_ebcgraf
[64] = {
79 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
80 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
81 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
82 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
83 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
84 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
85 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
86 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
90 raw3270_buffer_address(struct raw3270
*rp
, char *cp
, unsigned short addr
)
92 if (test_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
)) {
93 cp
[0] = (addr
>> 8) & 0x3f;
96 cp
[0] = raw3270_ebcgraf
[(addr
>> 6) & 0x3f];
97 cp
[1] = raw3270_ebcgraf
[addr
& 0x3f];
102 * Allocate a new 3270 ccw request
104 struct raw3270_request
*
105 raw3270_request_alloc(size_t size
)
107 struct raw3270_request
*rq
;
109 /* Allocate request structure */
110 rq
= kmalloc(sizeof(struct raw3270_request
), GFP_KERNEL
| GFP_DMA
);
112 return ERR_PTR(-ENOMEM
);
113 memset(rq
, 0, sizeof(struct raw3270_request
));
115 /* alloc output buffer. */
117 rq
->buffer
= kmalloc(size
, GFP_KERNEL
| GFP_DMA
);
120 return ERR_PTR(-ENOMEM
);
124 INIT_LIST_HEAD(&rq
->list
);
129 rq
->ccw
.cda
= __pa(rq
->buffer
);
130 rq
->ccw
.flags
= CCW_FLAG_SLI
;
135 #ifdef CONFIG_TN3270_CONSOLE
137 * Allocate a new 3270 ccw request from bootmem. Only works very
138 * early in the boot process. Only con3270.c should be using this.
140 struct raw3270_request
*
141 raw3270_request_alloc_bootmem(size_t size
)
143 struct raw3270_request
*rq
;
145 rq
= alloc_bootmem_low(sizeof(struct raw3270
));
147 return ERR_PTR(-ENOMEM
);
148 memset(rq
, 0, sizeof(struct raw3270_request
));
150 /* alloc output buffer. */
152 rq
->buffer
= alloc_bootmem_low(size
);
154 free_bootmem((unsigned long) rq
,
155 sizeof(struct raw3270
));
156 return ERR_PTR(-ENOMEM
);
160 INIT_LIST_HEAD(&rq
->list
);
165 rq
->ccw
.cda
= __pa(rq
->buffer
);
166 rq
->ccw
.flags
= CCW_FLAG_SLI
;
173 * Free 3270 ccw request
176 raw3270_request_free (struct raw3270_request
*rq
)
184 * Reset request to initial state.
187 raw3270_request_reset(struct raw3270_request
*rq
)
189 BUG_ON(!list_empty(&rq
->list
));
190 rq
->ccw
.cmd_code
= 0;
192 rq
->ccw
.cda
= __pa(rq
->buffer
);
193 rq
->ccw
.flags
= CCW_FLAG_SLI
;
199 * Set command code to ccw of a request.
202 raw3270_request_set_cmd(struct raw3270_request
*rq
, u8 cmd
)
204 rq
->ccw
.cmd_code
= cmd
;
208 * Add data fragment to output buffer.
211 raw3270_request_add_data(struct raw3270_request
*rq
, void *data
, size_t size
)
213 if (size
+ rq
->ccw
.count
> rq
->size
)
215 memcpy(rq
->buffer
+ rq
->ccw
.count
, data
, size
);
216 rq
->ccw
.count
+= size
;
221 * Set address/length pair to ccw of a request.
224 raw3270_request_set_data(struct raw3270_request
*rq
, void *data
, size_t size
)
226 rq
->ccw
.cda
= __pa(data
);
227 rq
->ccw
.count
= size
;
231 * Set idal buffer to ccw of a request.
234 raw3270_request_set_idal(struct raw3270_request
*rq
, struct idal_buffer
*ib
)
236 rq
->ccw
.cda
= __pa(ib
->data
);
237 rq
->ccw
.count
= ib
->size
;
238 rq
->ccw
.flags
|= CCW_FLAG_IDA
;
245 raw3270_halt_io_nolock(struct raw3270
*rp
, struct raw3270_request
*rq
)
250 if (raw3270_request_final(rq
))
252 /* Check if interrupt has already been processed */
253 for (retries
= 0; retries
< 5; retries
++) {
255 rc
= ccw_device_halt(rp
->cdev
, (long) rq
);
257 rc
= ccw_device_clear(rp
->cdev
, (long) rq
);
259 break; /* termination successful */
265 raw3270_halt_io(struct raw3270
*rp
, struct raw3270_request
*rq
)
270 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
271 rc
= raw3270_halt_io_nolock(rp
, rq
);
272 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
277 * Add the request to the request queue, try to start it if the
278 * 3270 device is idle. Return without waiting for end of i/o.
281 __raw3270_start(struct raw3270
*rp
, struct raw3270_view
*view
,
282 struct raw3270_request
*rq
)
285 raw3270_get_view(view
);
286 if (list_empty(&rp
->req_queue
) &&
287 !test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
)) {
288 /* No other requests are on the queue. Start this one. */
289 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
290 (unsigned long) rq
, 0, 0);
292 raw3270_put_view(view
);
296 list_add_tail(&rq
->list
, &rp
->req_queue
);
301 raw3270_start(struct raw3270_view
*view
, struct raw3270_request
*rq
)
307 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
309 if (!rp
|| rp
->view
!= view
)
311 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
314 rc
= __raw3270_start(rp
, view
, rq
);
315 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
320 raw3270_start_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
)
326 raw3270_get_view(view
);
327 list_add_tail(&rq
->list
, &rp
->req_queue
);
332 * 3270 interrupt routine, called from the ccw_device layer
335 raw3270_irq (struct ccw_device
*cdev
, unsigned long intparm
, struct irb
*irb
)
338 struct raw3270_view
*view
;
339 struct raw3270_request
*rq
;
342 rp
= (struct raw3270
*) cdev
->dev
.driver_data
;
345 rq
= (struct raw3270_request
*) intparm
;
346 view
= rq
? rq
->view
: rp
->view
;
349 rc
= RAW3270_IO_RETRY
;
350 else if (irb
->scsw
.fctl
& SCSW_FCTL_HALT_FUNC
) {
352 rc
= RAW3270_IO_DONE
;
353 } else if (irb
->scsw
.dstat
== (DEV_STAT_CHN_END
| DEV_STAT_DEV_END
|
354 DEV_STAT_UNIT_EXCEP
)) {
355 /* Handle CE-DE-UE and subsequent UDE */
356 set_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
357 rc
= RAW3270_IO_BUSY
;
358 } else if (test_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
)) {
359 /* Wait for UDE if busy flag is set. */
360 if (irb
->scsw
.dstat
& DEV_STAT_DEV_END
) {
361 clear_bit(RAW3270_FLAGS_BUSY
, &rp
->flags
);
362 /* Got it, now retry. */
363 rc
= RAW3270_IO_RETRY
;
365 rc
= RAW3270_IO_BUSY
;
367 rc
= view
->fn
->intv(view
, rq
, irb
);
369 rc
= RAW3270_IO_DONE
;
372 case RAW3270_IO_DONE
:
374 case RAW3270_IO_BUSY
:
376 * Intervention required by the operator. We have to wait
377 * for unsolicited device end.
380 case RAW3270_IO_RETRY
:
383 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
384 (unsigned long) rq
, 0, 0);
386 return; /* Sucessfully restarted. */
388 case RAW3270_IO_STOP
:
391 raw3270_halt_io_nolock(rp
, rq
);
398 BUG_ON(list_empty(&rq
->list
));
399 /* The request completed, remove from queue and do callback. */
400 list_del_init(&rq
->list
);
402 rq
->callback(rq
, rq
->callback_data
);
403 /* Do put_device for get_device in raw3270_start. */
404 raw3270_put_view(view
);
407 * Try to start each request on request queue until one is
408 * started successful.
410 while (!list_empty(&rp
->req_queue
)) {
411 rq
= list_entry(rp
->req_queue
.next
,struct raw3270_request
,list
);
412 rq
->rc
= ccw_device_start(rp
->cdev
, &rq
->ccw
,
413 (unsigned long) rq
, 0, 0);
416 /* Start failed. Remove request and do callback. */
417 list_del_init(&rq
->list
);
419 rq
->callback(rq
, rq
->callback_data
);
420 /* Do put_device for get_device in raw3270_start. */
421 raw3270_put_view(view
);
429 struct raw3270_ua
{ /* Query Reply structure for Usable Area */
430 struct { /* Usable Area Query Reply Base */
431 short l
; /* Length of this structured field */
432 char sfid
; /* 0x81 if Query Reply */
433 char qcode
; /* 0x81 if Usable Area */
436 short w
; /* Width of usable area */
437 short h
; /* Heigth of usavle area */
438 char units
; /* 0x00:in; 0x01:mm */
443 short buffsz
; /* Character buffer size, bytes */
448 } __attribute__ ((packed
)) uab
;
449 struct { /* Alternate Usable Area Self-Defining Parameter */
450 char l
; /* Length of this Self-Defining Parm */
451 char sdpid
; /* 0x02 if Alternate Usable Area */
453 char auaid
; /* 0x01 is Id for the A U A */
454 short wauai
; /* Width of AUAi */
455 short hauai
; /* Height of AUAi */
456 char auaunits
; /* 0x00:in, 0x01:mm */
461 } __attribute__ ((packed
)) aua
;
462 } __attribute__ ((packed
));
464 static unsigned char raw3270_init_data
[256];
465 static struct raw3270_request raw3270_init_request
;
466 static struct diag210 raw3270_init_diag210
;
467 static DECLARE_MUTEX(raw3270_init_sem
);
470 raw3270_init_irq(struct raw3270_view
*view
, struct raw3270_request
*rq
,
474 * Unit-Check Processing:
475 * Expect Command Reject or Intervention Required.
477 if (irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
) {
478 /* Request finished abnormally. */
479 if (irb
->ecw
[0] & SNS0_INTERVENTION_REQ
) {
480 set_bit(RAW3270_FLAGS_BUSY
, &view
->dev
->flags
);
481 return RAW3270_IO_BUSY
;
485 if (irb
->scsw
.dstat
& DEV_STAT_UNIT_CHECK
) {
486 if (irb
->ecw
[0] & SNS0_CMD_REJECT
)
487 rq
->rc
= -EOPNOTSUPP
;
491 /* Request finished normally. Copy residual count. */
492 rq
->rescnt
= irb
->scsw
.count
;
494 if (irb
->scsw
.dstat
& DEV_STAT_ATTENTION
) {
495 set_bit(RAW3270_FLAGS_ATTN
, &view
->dev
->flags
);
496 wake_up(&raw3270_wait_queue
);
498 return RAW3270_IO_DONE
;
501 static struct raw3270_fn raw3270_init_fn
= {
502 .intv
= raw3270_init_irq
505 static struct raw3270_view raw3270_init_view
= {
506 .fn
= &raw3270_init_fn
510 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
511 * Wait for end of request. The request must have been started
512 * with raw3270_start, rc = 0. The device lock may NOT have been
513 * released between calling raw3270_start and raw3270_wait.
516 raw3270_wake_init(struct raw3270_request
*rq
, void *data
)
518 wake_up((wait_queue_head_t
*) data
);
522 * Special wait function that can cope with console initialization.
525 raw3270_start_init(struct raw3270
*rp
, struct raw3270_view
*view
,
526 struct raw3270_request
*rq
)
529 wait_queue_head_t wq
;
532 #ifdef CONFIG_TN3270_CONSOLE
533 if (raw3270_registered
== 0) {
534 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
536 rc
= __raw3270_start(rp
, view
, rq
);
538 while (!raw3270_request_final(rq
)) {
542 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
546 init_waitqueue_head(&wq
);
547 rq
->callback
= raw3270_wake_init
;
548 rq
->callback_data
= &wq
;
549 spin_lock_irqsave(get_ccwdev_lock(view
->dev
->cdev
), flags
);
550 rc
= __raw3270_start(rp
, view
, rq
);
551 spin_unlock_irqrestore(get_ccwdev_lock(view
->dev
->cdev
), flags
);
554 /* Now wait for the completion. */
555 rc
= wait_event_interruptible(wq
, raw3270_request_final(rq
));
556 if (rc
== -ERESTARTSYS
) { /* Interrupted by a signal. */
557 raw3270_halt_io(view
->dev
, rq
);
558 /* No wait for the halt to complete. */
559 wait_event(wq
, raw3270_request_final(rq
));
566 __raw3270_size_device_vm(struct raw3270
*rp
)
570 raw3270_init_diag210
.vrdcdvno
=
571 _ccw_device_get_device_number(rp
->cdev
);
572 raw3270_init_diag210
.vrdclen
= sizeof(struct diag210
);
573 rc
= diag210(&raw3270_init_diag210
);
576 model
= raw3270_init_diag210
.vrdccrmd
;
599 printk(KERN_WARNING
"vrdccrmd is 0x%.8x\n", model
);
607 __raw3270_size_device(struct raw3270
*rp
)
609 static const unsigned char wbuf
[] =
610 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
611 struct raw3270_ua
*uap
;
612 unsigned short count
;
616 * To determine the size of the 3270 device we need to do:
617 * 1) send a 'read partition' data stream to the device
618 * 2) wait for the attn interrupt that preceeds the query reply
619 * 3) do a read modified to get the query reply
620 * To make things worse we have to cope with intervention
621 * required (3270 device switched to 'stand-by') and command
622 * rejects (old devices that can't do 'read partition').
624 memset(&raw3270_init_request
, 0, sizeof(raw3270_init_request
));
625 memset(raw3270_init_data
, 0, sizeof(raw3270_init_data
));
626 /* Store 'read partition' data stream to raw3270_init_data */
627 memcpy(raw3270_init_data
, wbuf
, sizeof(wbuf
));
628 INIT_LIST_HEAD(&raw3270_init_request
.list
);
629 raw3270_init_request
.ccw
.cmd_code
= TC_WRITESF
;
630 raw3270_init_request
.ccw
.flags
= CCW_FLAG_SLI
;
631 raw3270_init_request
.ccw
.count
= sizeof(wbuf
);
632 raw3270_init_request
.ccw
.cda
= (__u32
) __pa(raw3270_init_data
);
634 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &raw3270_init_request
);
636 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
639 /* Wait for attention interrupt. */
640 #ifdef CONFIG_TN3270_CONSOLE
641 if (raw3270_registered
== 0) {
644 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
645 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN
, &rp
->flags
))
647 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
650 rc
= wait_event_interruptible(raw3270_wait_queue
,
651 test_and_clear_bit(RAW3270_FLAGS_ATTN
, &rp
->flags
));
656 * The device accepted the 'read partition' command. Now
657 * set up a read ccw and issue it.
659 raw3270_init_request
.ccw
.cmd_code
= TC_READMOD
;
660 raw3270_init_request
.ccw
.flags
= CCW_FLAG_SLI
;
661 raw3270_init_request
.ccw
.count
= sizeof(raw3270_init_data
);
662 raw3270_init_request
.ccw
.cda
= (__u32
) __pa(raw3270_init_data
);
663 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &raw3270_init_request
);
666 /* Got a Query Reply */
667 count
= sizeof(raw3270_init_data
) - raw3270_init_request
.rescnt
;
668 uap
= (struct raw3270_ua
*) (raw3270_init_data
+ 1);
669 /* Paranoia check. */
670 if (raw3270_init_data
[0] != 0x88 || uap
->uab
.qcode
!= 0x81)
672 /* Copy rows/columns of default Usable Area */
673 rp
->rows
= uap
->uab
.h
;
674 rp
->cols
= uap
->uab
.w
;
675 /* Check for 14 bit addressing */
676 if ((uap
->uab
.flags0
& 0x0d) == 0x01)
677 set_bit(RAW3270_FLAGS_14BITADDR
, &rp
->flags
);
678 /* Check for Alternate Usable Area */
679 if (uap
->uab
.l
== sizeof(struct raw3270_ua
) &&
680 uap
->aua
.sdpid
== 0x02) {
681 rp
->rows
= uap
->aua
.hauai
;
682 rp
->cols
= uap
->aua
.wauai
;
688 raw3270_size_device(struct raw3270
*rp
)
692 down(&raw3270_init_sem
);
693 rp
->view
= &raw3270_init_view
;
694 raw3270_init_view
.dev
= rp
;
696 rc
= __raw3270_size_device_vm(rp
);
698 rc
= __raw3270_size_device(rp
);
699 raw3270_init_view
.dev
= 0;
701 up(&raw3270_init_sem
);
702 if (rc
== 0) { /* Found something. */
703 /* Try to find a model. */
705 if (rp
->rows
== 24 && rp
->cols
== 80)
707 if (rp
->rows
== 32 && rp
->cols
== 80)
709 if (rp
->rows
== 43 && rp
->cols
== 80)
711 if (rp
->rows
== 27 && rp
->cols
== 132)
714 /* Couldn't detect size. Use default model 2. */
724 raw3270_reset_device(struct raw3270
*rp
)
728 down(&raw3270_init_sem
);
729 memset(&raw3270_init_request
, 0, sizeof(raw3270_init_request
));
730 memset(raw3270_init_data
, 0, sizeof(raw3270_init_data
));
731 /* Store reset data stream to raw3270_init_data/raw3270_init_request */
732 raw3270_init_data
[0] = TW_KR
;
733 INIT_LIST_HEAD(&raw3270_init_request
.list
);
734 raw3270_init_request
.ccw
.cmd_code
= TC_EWRITEA
;
735 raw3270_init_request
.ccw
.flags
= CCW_FLAG_SLI
;
736 raw3270_init_request
.ccw
.count
= 1;
737 raw3270_init_request
.ccw
.cda
= (__u32
) __pa(raw3270_init_data
);
738 rp
->view
= &raw3270_init_view
;
739 raw3270_init_view
.dev
= rp
;
740 rc
= raw3270_start_init(rp
, &raw3270_init_view
, &raw3270_init_request
);
741 raw3270_init_view
.dev
= 0;
743 up(&raw3270_init_sem
);
748 * Setup new 3270 device.
751 raw3270_setup_device(struct ccw_device
*cdev
, struct raw3270
*rp
, char *ascebc
)
757 memset(rp
, 0, sizeof(struct raw3270
));
758 /* Copy ebcdic -> ascii translation table. */
759 memcpy(ascebc
, _ascebc
, 256);
761 /* correct brackets and circumflex */
772 INIT_LIST_HEAD(&rp
->req_queue
);
773 INIT_LIST_HEAD(&rp
->view_list
);
776 * Add device to list and find the smallest unused minor
780 /* Keep the list sorted. */
783 list_for_each(l
, &raw3270_devices
) {
784 tmp
= list_entry(l
, struct raw3270
, list
);
785 if (tmp
->minor
> minor
) {
787 __list_add(&rp
->list
, l
->prev
, l
);
792 if (rp
->minor
== -1 && minor
< RAW3270_MAXDEVS
) {
794 list_add_tail(&rp
->list
, &raw3270_devices
);
797 /* No free minor number? Then give up. */
801 cdev
->dev
.driver_data
= rp
;
802 cdev
->handler
= raw3270_irq
;
806 #ifdef CONFIG_TN3270_CONSOLE
808 * Setup 3270 device configured as console.
811 raw3270_setup_console(struct ccw_device
*cdev
)
817 rp
= (struct raw3270
*) alloc_bootmem(sizeof(struct raw3270
));
818 ascebc
= (char *) alloc_bootmem(256);
819 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
822 set_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
);
823 rc
= raw3270_reset_device(rp
);
826 rc
= raw3270_size_device(rp
);
829 rc
= raw3270_reset_device(rp
);
832 set_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
837 raw3270_wait_cons_dev(struct raw3270
*rp
)
841 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
843 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
849 * Create a 3270 device structure.
851 static struct raw3270
*
852 raw3270_create_device(struct ccw_device
*cdev
)
858 rp
= kmalloc(sizeof(struct raw3270
), GFP_KERNEL
);
860 return ERR_PTR(-ENOMEM
);
861 ascebc
= kmalloc(256, GFP_KERNEL
);
864 return ERR_PTR(-ENOMEM
);
866 rc
= raw3270_setup_device(cdev
, rp
, ascebc
);
872 /* Get reference to ccw_device structure. */
873 get_device(&cdev
->dev
);
881 raw3270_activate_view(struct raw3270_view
*view
)
884 struct raw3270_view
*oldview
, *nv
;
891 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
892 if (rp
->view
== view
)
894 else if (!test_bit(RAW3270_FLAGS_READY
, &rp
->flags
))
900 oldview
->fn
->deactivate(oldview
);
903 rc
= view
->fn
->activate(view
);
905 /* Didn't work. Try to reactivate the old view. */
907 if (!oldview
|| oldview
->fn
->activate(oldview
) != 0) {
908 /* Didn't work as well. Try any other view. */
909 list_for_each_entry(nv
, &rp
->view_list
, list
)
910 if (nv
!= view
&& nv
!= oldview
) {
912 if (nv
->fn
->activate(nv
) == 0)
919 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
924 * Deactivate current view.
927 raw3270_deactivate_view(struct raw3270_view
*view
)
935 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
936 if (rp
->view
== view
) {
937 view
->fn
->deactivate(view
);
939 /* Move deactivated view to end of list. */
940 list_del_init(&view
->list
);
941 list_add_tail(&view
->list
, &rp
->view_list
);
942 /* Try to activate another view. */
943 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
944 list_for_each_entry(view
, &rp
->view_list
, list
)
945 if (view
->fn
->activate(view
) == 0) {
951 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
955 * Add view to device with minor "minor".
958 raw3270_add_view(struct raw3270_view
*view
, struct raw3270_fn
*fn
, int minor
)
966 list_for_each_entry(rp
, &raw3270_devices
, list
) {
967 if (rp
->minor
!= minor
)
969 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
970 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
971 atomic_set(&view
->ref_count
, 2);
974 view
->model
= rp
->model
;
975 view
->rows
= rp
->rows
;
976 view
->cols
= rp
->cols
;
977 view
->ascebc
= rp
->ascebc
;
978 spin_lock_init(&view
->lock
);
979 list_add_tail(&view
->list
, &rp
->view_list
);
982 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
990 * Find specific view of device with minor "minor".
992 struct raw3270_view
*
993 raw3270_find_view(struct raw3270_fn
*fn
, int minor
)
996 struct raw3270_view
*view
, *tmp
;
1000 view
= ERR_PTR(-ENODEV
);
1001 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1002 if (rp
->minor
!= minor
)
1004 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1005 if (test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
1006 view
= ERR_PTR(-ENOENT
);
1007 list_for_each_entry(tmp
, &rp
->view_list
, list
) {
1008 if (tmp
->fn
== fn
) {
1009 raw3270_get_view(tmp
);
1015 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1023 * Remove view from device and free view structure via call to view->fn->free.
1026 raw3270_del_view(struct raw3270_view
*view
)
1028 unsigned long flags
;
1030 struct raw3270_view
*nv
;
1033 spin_lock_irqsave(get_ccwdev_lock(rp
->cdev
), flags
);
1034 if (rp
->view
== view
) {
1035 view
->fn
->deactivate(view
);
1038 list_del_init(&view
->list
);
1039 if (!rp
->view
&& test_bit(RAW3270_FLAGS_READY
, &rp
->flags
)) {
1040 /* Try to activate another view. */
1041 list_for_each_entry(nv
, &rp
->view_list
, list
) {
1042 if (nv
->fn
->activate(view
) == 0) {
1048 spin_unlock_irqrestore(get_ccwdev_lock(rp
->cdev
), flags
);
1049 /* Wait for reference counter to drop to zero. */
1050 atomic_dec(&view
->ref_count
);
1051 wait_event(raw3270_wait_queue
, atomic_read(&view
->ref_count
) == 0);
1053 view
->fn
->free(view
);
1057 * Remove a 3270 device structure.
1060 raw3270_delete_device(struct raw3270
*rp
)
1062 struct ccw_device
*cdev
;
1064 /* Remove from device chain. */
1066 list_del_init(&rp
->list
);
1069 /* Disconnect from ccw_device. */
1072 cdev
->dev
.driver_data
= 0;
1075 /* Put ccw_device structure. */
1076 put_device(&cdev
->dev
);
1078 /* Now free raw3270 structure. */
1084 raw3270_probe (struct ccw_device
*cdev
)
1090 * Additional attributes for a 3270 device
1093 raw3270_model_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1095 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1096 ((struct raw3270
*) dev
->driver_data
)->model
);
1098 static DEVICE_ATTR(model
, 0444, raw3270_model_show
, 0);
1101 raw3270_rows_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1103 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1104 ((struct raw3270
*) dev
->driver_data
)->rows
);
1106 static DEVICE_ATTR(rows
, 0444, raw3270_rows_show
, 0);
1109 raw3270_columns_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1111 return snprintf(buf
, PAGE_SIZE
, "%i\n",
1112 ((struct raw3270
*) dev
->driver_data
)->cols
);
1114 static DEVICE_ATTR(columns
, 0444, raw3270_columns_show
, 0);
1116 static struct attribute
* raw3270_attrs
[] = {
1117 &dev_attr_model
.attr
,
1118 &dev_attr_rows
.attr
,
1119 &dev_attr_columns
.attr
,
1123 static struct attribute_group raw3270_attr_group
= {
1124 .attrs
= raw3270_attrs
,
1128 raw3270_create_attributes(struct raw3270
*rp
)
1130 //FIXME: check return code
1131 sysfs_create_group(&rp
->cdev
->dev
.kobj
, &raw3270_attr_group
);
1135 * Notifier for device addition/removal
1137 struct raw3270_notifier
{
1138 struct list_head list
;
1139 void (*notifier
)(int, int);
1142 static struct list_head raw3270_notifier
= LIST_HEAD_INIT(raw3270_notifier
);
1144 int raw3270_register_notifier(void (*notifier
)(int, int))
1146 struct raw3270_notifier
*np
;
1149 np
= kmalloc(sizeof(struct raw3270_notifier
), GFP_KERNEL
);
1152 np
->notifier
= notifier
;
1154 list_add_tail(&np
->list
, &raw3270_notifier
);
1155 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1156 get_device(&rp
->cdev
->dev
);
1157 notifier(rp
->minor
, 1);
1163 void raw3270_unregister_notifier(void (*notifier
)(int, int))
1165 struct raw3270_notifier
*np
;
1168 list_for_each_entry(np
, &raw3270_notifier
, list
)
1169 if (np
->notifier
== notifier
) {
1170 list_del(&np
->list
);
1178 * Set 3270 device online.
1181 raw3270_set_online (struct ccw_device
*cdev
)
1184 struct raw3270_notifier
*np
;
1187 rp
= raw3270_create_device(cdev
);
1190 rc
= raw3270_reset_device(rp
);
1193 rc
= raw3270_size_device(rp
);
1196 rc
= raw3270_reset_device(rp
);
1199 raw3270_create_attributes(rp
);
1200 set_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
1202 list_for_each_entry(np
, &raw3270_notifier
, list
)
1203 np
->notifier(rp
->minor
, 1);
1209 * Remove 3270 device structure.
1212 raw3270_remove (struct ccw_device
*cdev
)
1214 unsigned long flags
;
1216 struct raw3270_view
*v
;
1217 struct raw3270_notifier
*np
;
1219 rp
= cdev
->dev
.driver_data
;
1220 clear_bit(RAW3270_FLAGS_READY
, &rp
->flags
);
1222 sysfs_remove_group(&cdev
->dev
.kobj
, &raw3270_attr_group
);
1224 /* Deactivate current view and remove all views. */
1225 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1227 rp
->view
->fn
->deactivate(rp
->view
);
1230 while (!list_empty(&rp
->view_list
)) {
1231 v
= list_entry(rp
->view_list
.next
, struct raw3270_view
, list
);
1234 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1235 raw3270_del_view(v
);
1236 spin_lock_irqsave(get_ccwdev_lock(cdev
), flags
);
1238 spin_unlock_irqrestore(get_ccwdev_lock(cdev
), flags
);
1241 list_for_each_entry(np
, &raw3270_notifier
, list
)
1242 np
->notifier(rp
->minor
, 0);
1245 /* Reset 3270 device. */
1246 raw3270_reset_device(rp
);
1247 /* And finally remove it. */
1248 raw3270_delete_device(rp
);
1252 * Set 3270 device offline.
1255 raw3270_set_offline (struct ccw_device
*cdev
)
1259 rp
= cdev
->dev
.driver_data
;
1260 if (test_bit(RAW3270_FLAGS_CONSOLE
, &rp
->flags
))
1262 raw3270_remove(cdev
);
1266 static struct ccw_device_id raw3270_id
[] = {
1267 { CCW_DEVICE(0x3270, 0) },
1268 { CCW_DEVICE(0x3271, 0) },
1269 { CCW_DEVICE(0x3272, 0) },
1270 { CCW_DEVICE(0x3273, 0) },
1271 { CCW_DEVICE(0x3274, 0) },
1272 { CCW_DEVICE(0x3275, 0) },
1273 { CCW_DEVICE(0x3276, 0) },
1274 { CCW_DEVICE(0x3277, 0) },
1275 { CCW_DEVICE(0x3278, 0) },
1276 { CCW_DEVICE(0x3279, 0) },
1277 { CCW_DEVICE(0x3174, 0) },
1278 { /* end of list */ },
1281 static struct ccw_driver raw3270_ccw_driver
= {
1283 .owner
= THIS_MODULE
,
1285 .probe
= &raw3270_probe
,
1286 .remove
= &raw3270_remove
,
1287 .set_online
= &raw3270_set_online
,
1288 .set_offline
= &raw3270_set_offline
,
1297 if (raw3270_registered
)
1299 raw3270_registered
= 1;
1300 rc
= ccw_driver_register(&raw3270_ccw_driver
);
1302 /* Create attributes for early (= console) device. */
1304 list_for_each_entry(rp
, &raw3270_devices
, list
) {
1305 get_device(&rp
->cdev
->dev
);
1306 raw3270_create_attributes(rp
);
1316 ccw_driver_unregister(&raw3270_ccw_driver
);
1319 MODULE_LICENSE("GPL");
1321 module_init(raw3270_init
);
1322 module_exit(raw3270_exit
);
1324 EXPORT_SYMBOL(raw3270_request_alloc
);
1325 EXPORT_SYMBOL(raw3270_request_free
);
1326 EXPORT_SYMBOL(raw3270_request_reset
);
1327 EXPORT_SYMBOL(raw3270_request_set_cmd
);
1328 EXPORT_SYMBOL(raw3270_request_add_data
);
1329 EXPORT_SYMBOL(raw3270_request_set_data
);
1330 EXPORT_SYMBOL(raw3270_request_set_idal
);
1331 EXPORT_SYMBOL(raw3270_buffer_address
);
1332 EXPORT_SYMBOL(raw3270_add_view
);
1333 EXPORT_SYMBOL(raw3270_del_view
);
1334 EXPORT_SYMBOL(raw3270_find_view
);
1335 EXPORT_SYMBOL(raw3270_activate_view
);
1336 EXPORT_SYMBOL(raw3270_deactivate_view
);
1337 EXPORT_SYMBOL(raw3270_start
);
1338 EXPORT_SYMBOL(raw3270_start_irq
);
1339 EXPORT_SYMBOL(raw3270_register_notifier
);
1340 EXPORT_SYMBOL(raw3270_unregister_notifier
);
1341 EXPORT_SYMBOL(raw3270_wait_queue
);