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/bootmem.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/wait.h>
21 #include <asm/ccwdev.h>
23 #include <asm/ebcdic.h>
28 #include <linux/major.h>
29 #include <linux/kdev_t.h>
30 #include <linux/device.h>
31 #include <linux/mutex.h>
33 static struct class *class3270;
35 /* The main 3270 data structure. */
37 struct list_head list;
38 struct ccw_device *cdev;
41 short model, rows, cols;
44 struct list_head req_queue; /* Request queue. */
45 struct list_head view_list; /* List of available views. */
46 struct raw3270_view *view; /* Active view. */
48 struct timer_list timer; /* Device timer. */
50 unsigned char *ascebc; /* ascii -> ebcdic table */
51 struct device *clttydev; /* 3270-class tty device ptr */
52 struct device *cltubdev; /* 3270-class tub device ptr */
54 struct raw3270_request init_request;
55 unsigned char init_data[256];
59 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
60 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
61 #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
62 #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
63 #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
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;
146 #ifdef CONFIG_TN3270_CONSOLE
148 * Allocate a new 3270 ccw request from bootmem. Only works very
149 * early in the boot process. Only con3270.c should be using this.
151 struct raw3270_request __init *raw3270_request_alloc_bootmem(size_t size)
153 struct raw3270_request *rq;
155 rq = alloc_bootmem_low(sizeof(struct raw3270));
157 return ERR_PTR(-ENOMEM);
158 memset(rq, 0, sizeof(struct raw3270_request));
160 /* alloc output buffer. */
162 rq->buffer = alloc_bootmem_low(size);
164 free_bootmem((unsigned long) rq,
165 sizeof(struct raw3270));
166 return ERR_PTR(-ENOMEM);
170 INIT_LIST_HEAD(&rq->list);
175 rq->ccw.cda = __pa(rq->buffer);
176 rq->ccw.flags = CCW_FLAG_SLI;
183 * Free 3270 ccw request
186 raw3270_request_free (struct raw3270_request *rq)
193 * Reset request to initial state.
196 raw3270_request_reset(struct raw3270_request *rq)
198 BUG_ON(!list_empty(&rq->list));
199 rq->ccw.cmd_code = 0;
201 rq->ccw.cda = __pa(rq->buffer);
202 rq->ccw.flags = CCW_FLAG_SLI;
208 * Set command code to ccw of a request.
211 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
213 rq->ccw.cmd_code = cmd;
217 * Add data fragment to output buffer.
220 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
222 if (size + rq->ccw.count > rq->size)
224 memcpy(rq->buffer + rq->ccw.count, data, size);
225 rq->ccw.count += size;
230 * Set address/length pair to ccw of a request.
233 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
235 rq->ccw.cda = __pa(data);
236 rq->ccw.count = size;
240 * Set idal buffer to ccw of a request.
243 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
245 rq->ccw.cda = __pa(ib->data);
246 rq->ccw.count = ib->size;
247 rq->ccw.flags |= CCW_FLAG_IDA;
254 raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
259 if (raw3270_request_final(rq))
261 /* Check if interrupt has already been processed */
262 for (retries = 0; retries < 5; retries++) {
264 rc = ccw_device_halt(rp->cdev, (long) rq);
266 rc = ccw_device_clear(rp->cdev, (long) rq);
268 break; /* termination successful */
274 raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
279 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
280 rc = raw3270_halt_io_nolock(rp, rq);
281 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
286 * Add the request to the request queue, try to start it if the
287 * 3270 device is idle. Return without waiting for end of i/o.
290 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
291 struct raw3270_request *rq)
294 raw3270_get_view(view);
295 if (list_empty(&rp->req_queue) &&
296 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
297 /* No other requests are on the queue. Start this one. */
298 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
299 (unsigned long) rq, 0, 0);
301 raw3270_put_view(view);
305 list_add_tail(&rq->list, &rp->req_queue);
310 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
316 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
318 if (!rp || rp->view != view)
320 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
323 rc = __raw3270_start(rp, view, rq);
324 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
329 raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
335 if (!rp || rp->view != view)
337 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
340 rc = __raw3270_start(rp, view, rq);
345 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
351 raw3270_get_view(view);
352 list_add_tail(&rq->list, &rp->req_queue);
357 * 3270 interrupt routine, called from the ccw_device layer
360 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
363 struct raw3270_view *view;
364 struct raw3270_request *rq;
367 rp = (struct raw3270 *) cdev->dev.driver_data;
370 rq = (struct raw3270_request *) intparm;
371 view = rq ? rq->view : rp->view;
374 rc = RAW3270_IO_RETRY;
375 else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
377 rc = RAW3270_IO_DONE;
378 } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
379 DEV_STAT_UNIT_EXCEP)) {
380 /* Handle CE-DE-UE and subsequent UDE */
381 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
382 rc = RAW3270_IO_BUSY;
383 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
384 /* Wait for UDE if busy flag is set. */
385 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
386 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
387 /* Got it, now retry. */
388 rc = RAW3270_IO_RETRY;
390 rc = RAW3270_IO_BUSY;
392 rc = view->fn->intv(view, rq, irb);
394 rc = RAW3270_IO_DONE;
397 case RAW3270_IO_DONE:
399 case RAW3270_IO_BUSY:
401 * Intervention required by the operator. We have to wait
402 * for unsolicited device end.
405 case RAW3270_IO_RETRY:
408 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
409 (unsigned long) rq, 0, 0);
411 return; /* Sucessfully restarted. */
413 case RAW3270_IO_STOP:
416 raw3270_halt_io_nolock(rp, rq);
423 BUG_ON(list_empty(&rq->list));
424 /* The request completed, remove from queue and do callback. */
425 list_del_init(&rq->list);
427 rq->callback(rq, rq->callback_data);
428 /* Do put_device for get_device in raw3270_start. */
429 raw3270_put_view(view);
432 * Try to start each request on request queue until one is
433 * started successful.
435 while (!list_empty(&rp->req_queue)) {
436 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
437 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
438 (unsigned long) rq, 0, 0);
441 /* Start failed. Remove request and do callback. */
442 list_del_init(&rq->list);
444 rq->callback(rq, rq->callback_data);
445 /* Do put_device for get_device in raw3270_start. */
446 raw3270_put_view(view);
454 struct raw3270_ua { /* Query Reply structure for Usable Area */
455 struct { /* Usable Area Query Reply Base */
456 short l; /* Length of this structured field */
457 char sfid; /* 0x81 if Query Reply */
458 char qcode; /* 0x81 if Usable Area */
461 short w; /* Width of usable area */
462 short h; /* Heigth of usavle area */
463 char units; /* 0x00:in; 0x01:mm */
468 short buffsz; /* Character buffer size, bytes */
473 } __attribute__ ((packed)) uab;
474 struct { /* Alternate Usable Area Self-Defining Parameter */
475 char l; /* Length of this Self-Defining Parm */
476 char sdpid; /* 0x02 if Alternate Usable Area */
478 char auaid; /* 0x01 is Id for the A U A */
479 short wauai; /* Width of AUAi */
480 short hauai; /* Height of AUAi */
481 char auaunits; /* 0x00:in, 0x01:mm */
486 } __attribute__ ((packed)) aua;
487 } __attribute__ ((packed));
489 static struct diag210 raw3270_init_diag210;
490 static DEFINE_MUTEX(raw3270_init_mutex);
493 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
497 * Unit-Check Processing:
498 * Expect Command Reject or Intervention Required.
500 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
501 /* Request finished abnormally. */
502 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
503 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
504 return RAW3270_IO_BUSY;
508 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
509 if (irb->ecw[0] & SNS0_CMD_REJECT)
510 rq->rc = -EOPNOTSUPP;
514 /* Request finished normally. Copy residual count. */
515 rq->rescnt = irb->scsw.count;
517 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
518 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
519 wake_up(&raw3270_wait_queue);
521 return RAW3270_IO_DONE;
524 static struct raw3270_fn raw3270_init_fn = {
525 .intv = raw3270_init_irq
528 static struct raw3270_view raw3270_init_view = {
529 .fn = &raw3270_init_fn
533 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
534 * Wait for end of request. The request must have been started
535 * with raw3270_start, rc = 0. The device lock may NOT have been
536 * released between calling raw3270_start and raw3270_wait.
539 raw3270_wake_init(struct raw3270_request *rq, void *data)
541 wake_up((wait_queue_head_t *) data);
545 * Special wait function that can cope with console initialization.
548 raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
549 struct raw3270_request *rq)
552 wait_queue_head_t wq;
555 #ifdef CONFIG_TN3270_CONSOLE
556 if (raw3270_registered == 0) {
557 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
559 rc = __raw3270_start(rp, view, rq);
561 while (!raw3270_request_final(rq)) {
565 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
569 init_waitqueue_head(&wq);
570 rq->callback = raw3270_wake_init;
571 rq->callback_data = &wq;
572 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
573 rc = __raw3270_start(rp, view, rq);
574 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
577 /* Now wait for the completion. */
578 rc = wait_event_interruptible(wq, raw3270_request_final(rq));
579 if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
580 raw3270_halt_io(view->dev, rq);
581 /* No wait for the halt to complete. */
582 wait_event(wq, raw3270_request_final(rq));
589 __raw3270_size_device_vm(struct raw3270 *rp)
592 struct ccw_dev_id dev_id;
594 ccw_device_get_id(rp->cdev, &dev_id);
595 raw3270_init_diag210.vrdcdvno = dev_id.devno;
596 raw3270_init_diag210.vrdclen = sizeof(struct diag210);
597 rc = diag210(&raw3270_init_diag210);
600 model = raw3270_init_diag210.vrdccrmd;
623 printk(KERN_WARNING "vrdccrmd is 0x%.8x\n", model);
631 __raw3270_size_device(struct raw3270 *rp)
633 static const unsigned char wbuf[] =
634 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
635 struct raw3270_ua *uap;
636 unsigned short count;
640 * To determine the size of the 3270 device we need to do:
641 * 1) send a 'read partition' data stream to the device
642 * 2) wait for the attn interrupt that preceeds the query reply
643 * 3) do a read modified to get the query reply
644 * To make things worse we have to cope with intervention
645 * required (3270 device switched to 'stand-by') and command
646 * rejects (old devices that can't do 'read partition').
648 memset(&rp->init_request, 0, sizeof(rp->init_request));
649 memset(&rp->init_data, 0, 256);
650 /* Store 'read partition' data stream to init_data */
651 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
652 INIT_LIST_HEAD(&rp->init_request.list);
653 rp->init_request.ccw.cmd_code = TC_WRITESF;
654 rp->init_request.ccw.flags = CCW_FLAG_SLI;
655 rp->init_request.ccw.count = sizeof(wbuf);
656 rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
658 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
660 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
663 /* Wait for attention interrupt. */
664 #ifdef CONFIG_TN3270_CONSOLE
665 if (raw3270_registered == 0) {
668 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
669 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
671 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
674 rc = wait_event_interruptible(raw3270_wait_queue,
675 test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
680 * The device accepted the 'read partition' command. Now
681 * set up a read ccw and issue it.
683 rp->init_request.ccw.cmd_code = TC_READMOD;
684 rp->init_request.ccw.flags = CCW_FLAG_SLI;
685 rp->init_request.ccw.count = sizeof(rp->init_data);
686 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
687 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
690 /* Got a Query Reply */
691 count = sizeof(rp->init_data) - rp->init_request.rescnt;
692 uap = (struct raw3270_ua *) (rp->init_data + 1);
693 /* Paranoia check. */
694 if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
696 /* Copy rows/columns of default Usable Area */
697 rp->rows = uap->uab.h;
698 rp->cols = uap->uab.w;
699 /* Check for 14 bit addressing */
700 if ((uap->uab.flags0 & 0x0d) == 0x01)
701 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
702 /* Check for Alternate Usable Area */
703 if (uap->uab.l == sizeof(struct raw3270_ua) &&
704 uap->aua.sdpid == 0x02) {
705 rp->rows = uap->aua.hauai;
706 rp->cols = uap->aua.wauai;
712 raw3270_size_device(struct raw3270 *rp)
716 mutex_lock(&raw3270_init_mutex);
717 rp->view = &raw3270_init_view;
718 raw3270_init_view.dev = rp;
720 rc = __raw3270_size_device_vm(rp);
722 rc = __raw3270_size_device(rp);
723 raw3270_init_view.dev = NULL;
725 mutex_unlock(&raw3270_init_mutex);
726 if (rc == 0) { /* Found something. */
727 /* Try to find a model. */
729 if (rp->rows == 24 && rp->cols == 80)
731 if (rp->rows == 32 && rp->cols == 80)
733 if (rp->rows == 43 && rp->cols == 80)
735 if (rp->rows == 27 && rp->cols == 132)
738 /* Couldn't detect size. Use default model 2. */
748 raw3270_reset_device(struct raw3270 *rp)
752 mutex_lock(&raw3270_init_mutex);
753 memset(&rp->init_request, 0, sizeof(rp->init_request));
754 memset(&rp->init_data, 0, sizeof(rp->init_data));
755 /* Store reset data stream to init_data/init_request */
756 rp->init_data[0] = TW_KR;
757 INIT_LIST_HEAD(&rp->init_request.list);
758 rp->init_request.ccw.cmd_code = TC_EWRITEA;
759 rp->init_request.ccw.flags = CCW_FLAG_SLI;
760 rp->init_request.ccw.count = 1;
761 rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
762 rp->view = &raw3270_init_view;
763 raw3270_init_view.dev = rp;
764 rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
765 raw3270_init_view.dev = NULL;
767 mutex_unlock(&raw3270_init_mutex);
772 raw3270_reset(struct raw3270_view *view)
778 if (!rp || rp->view != view)
780 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
783 rc = raw3270_reset_device(view->dev);
788 * Setup new 3270 device.
791 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
797 memset(rp, 0, sizeof(struct raw3270));
798 /* Copy ebcdic -> ascii translation table. */
799 memcpy(ascebc, _ascebc, 256);
801 /* correct brackets and circumflex */
812 INIT_LIST_HEAD(&rp->req_queue);
813 INIT_LIST_HEAD(&rp->view_list);
816 * Add device to list and find the smallest unused minor
817 * number for it. Note: there is no device with minor 0,
818 * see special case for fs3270.c:fs3270_open().
820 mutex_lock(&raw3270_mutex);
821 /* Keep the list sorted. */
822 minor = RAW3270_FIRSTMINOR;
824 list_for_each(l, &raw3270_devices) {
825 tmp = list_entry(l, struct raw3270, list);
826 if (tmp->minor > minor) {
828 __list_add(&rp->list, l->prev, l);
833 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
835 list_add_tail(&rp->list, &raw3270_devices);
837 mutex_unlock(&raw3270_mutex);
838 /* No free minor number? Then give up. */
842 cdev->dev.driver_data = rp;
843 cdev->handler = raw3270_irq;
847 #ifdef CONFIG_TN3270_CONSOLE
849 * Setup 3270 device configured as console.
851 struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
857 rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
858 ascebc = (char *) alloc_bootmem(256);
859 rc = raw3270_setup_device(cdev, rp, ascebc);
862 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
863 rc = raw3270_reset_device(rp);
866 rc = raw3270_size_device(rp);
869 rc = raw3270_reset_device(rp);
872 set_bit(RAW3270_FLAGS_READY, &rp->flags);
877 raw3270_wait_cons_dev(struct raw3270 *rp)
881 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
883 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
889 * Create a 3270 device structure.
891 static struct raw3270 *
892 raw3270_create_device(struct ccw_device *cdev)
898 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
900 return ERR_PTR(-ENOMEM);
901 ascebc = kmalloc(256, GFP_KERNEL);
904 return ERR_PTR(-ENOMEM);
906 rc = raw3270_setup_device(cdev, rp, ascebc);
912 /* Get reference to ccw_device structure. */
913 get_device(&cdev->dev);
921 raw3270_activate_view(struct raw3270_view *view)
924 struct raw3270_view *oldview, *nv;
931 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
932 if (rp->view == view)
934 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
940 oldview->fn->deactivate(oldview);
943 rc = view->fn->activate(view);
945 /* Didn't work. Try to reactivate the old view. */
947 if (!oldview || oldview->fn->activate(oldview) != 0) {
948 /* Didn't work as well. Try any other view. */
949 list_for_each_entry(nv, &rp->view_list, list)
950 if (nv != view && nv != oldview) {
952 if (nv->fn->activate(nv) == 0)
959 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
964 * Deactivate current view.
967 raw3270_deactivate_view(struct raw3270_view *view)
975 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
976 if (rp->view == view) {
977 view->fn->deactivate(view);
979 /* Move deactivated view to end of list. */
980 list_del_init(&view->list);
981 list_add_tail(&view->list, &rp->view_list);
982 /* Try to activate another view. */
983 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
984 list_for_each_entry(view, &rp->view_list, list) {
986 if (view->fn->activate(view) == 0)
992 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
996 * Add view to device with minor "minor".
999 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
1001 unsigned long flags;
1007 mutex_lock(&raw3270_mutex);
1009 list_for_each_entry(rp, &raw3270_devices, list) {
1010 if (rp->minor != minor)
1012 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1013 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1014 atomic_set(&view->ref_count, 2);
1017 view->model = rp->model;
1018 view->rows = rp->rows;
1019 view->cols = rp->cols;
1020 view->ascebc = rp->ascebc;
1021 spin_lock_init(&view->lock);
1022 list_add(&view->list, &rp->view_list);
1025 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1028 mutex_unlock(&raw3270_mutex);
1033 * Find specific view of device with minor "minor".
1035 struct raw3270_view *
1036 raw3270_find_view(struct raw3270_fn *fn, int minor)
1039 struct raw3270_view *view, *tmp;
1040 unsigned long flags;
1042 mutex_lock(&raw3270_mutex);
1043 view = ERR_PTR(-ENODEV);
1044 list_for_each_entry(rp, &raw3270_devices, list) {
1045 if (rp->minor != minor)
1047 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1048 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1049 view = ERR_PTR(-ENOENT);
1050 list_for_each_entry(tmp, &rp->view_list, list) {
1051 if (tmp->fn == fn) {
1052 raw3270_get_view(tmp);
1058 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1061 mutex_unlock(&raw3270_mutex);
1066 * Remove view from device and free view structure via call to view->fn->free.
1069 raw3270_del_view(struct raw3270_view *view)
1071 unsigned long flags;
1073 struct raw3270_view *nv;
1076 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1077 if (rp->view == view) {
1078 view->fn->deactivate(view);
1081 list_del_init(&view->list);
1082 if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1083 /* Try to activate another view. */
1084 list_for_each_entry(nv, &rp->view_list, list) {
1085 if (nv->fn->activate(nv) == 0) {
1091 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1092 /* Wait for reference counter to drop to zero. */
1093 atomic_dec(&view->ref_count);
1094 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1096 view->fn->free(view);
1100 * Remove a 3270 device structure.
1103 raw3270_delete_device(struct raw3270 *rp)
1105 struct ccw_device *cdev;
1107 /* Remove from device chain. */
1108 mutex_lock(&raw3270_mutex);
1109 if (rp->clttydev && !IS_ERR(rp->clttydev))
1110 device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
1111 if (rp->cltubdev && !IS_ERR(rp->cltubdev))
1112 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
1113 list_del_init(&rp->list);
1114 mutex_unlock(&raw3270_mutex);
1116 /* Disconnect from ccw_device. */
1119 cdev->dev.driver_data = NULL;
1120 cdev->handler = NULL;
1122 /* Put ccw_device structure. */
1123 put_device(&cdev->dev);
1125 /* Now free raw3270 structure. */
1131 raw3270_probe (struct ccw_device *cdev)
1137 * Additional attributes for a 3270 device
1140 raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
1142 return snprintf(buf, PAGE_SIZE, "%i\n",
1143 ((struct raw3270 *) dev->driver_data)->model);
1145 static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
1148 raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
1150 return snprintf(buf, PAGE_SIZE, "%i\n",
1151 ((struct raw3270 *) dev->driver_data)->rows);
1153 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
1156 raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
1158 return snprintf(buf, PAGE_SIZE, "%i\n",
1159 ((struct raw3270 *) dev->driver_data)->cols);
1161 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
1163 static struct attribute * raw3270_attrs[] = {
1164 &dev_attr_model.attr,
1165 &dev_attr_rows.attr,
1166 &dev_attr_columns.attr,
1170 static struct attribute_group raw3270_attr_group = {
1171 .attrs = raw3270_attrs,
1174 static int raw3270_create_attributes(struct raw3270 *rp)
1178 rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1182 rp->clttydev = device_create(class3270, &rp->cdev->dev,
1183 MKDEV(IBM_TTY3270_MAJOR, rp->minor),
1184 "tty%s", rp->cdev->dev.bus_id);
1185 if (IS_ERR(rp->clttydev)) {
1186 rc = PTR_ERR(rp->clttydev);
1190 rp->cltubdev = device_create(class3270, &rp->cdev->dev,
1191 MKDEV(IBM_FS3270_MAJOR, rp->minor),
1192 "tub%s", rp->cdev->dev.bus_id);
1193 if (!IS_ERR(rp->cltubdev))
1196 rc = PTR_ERR(rp->cltubdev);
1197 device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
1200 sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1206 * Notifier for device addition/removal
1208 struct raw3270_notifier {
1209 struct list_head list;
1210 void (*notifier)(int, int);
1213 static LIST_HEAD(raw3270_notifier);
1215 int raw3270_register_notifier(void (*notifier)(int, int))
1217 struct raw3270_notifier *np;
1220 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1223 np->notifier = notifier;
1224 mutex_lock(&raw3270_mutex);
1225 list_add_tail(&np->list, &raw3270_notifier);
1226 list_for_each_entry(rp, &raw3270_devices, list) {
1227 get_device(&rp->cdev->dev);
1228 notifier(rp->minor, 1);
1230 mutex_unlock(&raw3270_mutex);
1234 void raw3270_unregister_notifier(void (*notifier)(int, int))
1236 struct raw3270_notifier *np;
1238 mutex_lock(&raw3270_mutex);
1239 list_for_each_entry(np, &raw3270_notifier, list)
1240 if (np->notifier == notifier) {
1241 list_del(&np->list);
1245 mutex_unlock(&raw3270_mutex);
1249 * Set 3270 device online.
1252 raw3270_set_online (struct ccw_device *cdev)
1255 struct raw3270_notifier *np;
1258 rp = raw3270_create_device(cdev);
1261 rc = raw3270_reset_device(rp);
1264 rc = raw3270_size_device(rp);
1267 rc = raw3270_reset_device(rp);
1270 rc = raw3270_create_attributes(rp);
1273 set_bit(RAW3270_FLAGS_READY, &rp->flags);
1274 mutex_lock(&raw3270_mutex);
1275 list_for_each_entry(np, &raw3270_notifier, list)
1276 np->notifier(rp->minor, 1);
1277 mutex_unlock(&raw3270_mutex);
1281 raw3270_delete_device(rp);
1286 * Remove 3270 device structure.
1289 raw3270_remove (struct ccw_device *cdev)
1291 unsigned long flags;
1293 struct raw3270_view *v;
1294 struct raw3270_notifier *np;
1296 rp = cdev->dev.driver_data;
1298 * _remove is the opposite of _probe; it's probe that
1299 * should set up rp. raw3270_remove gets entered for
1300 * devices even if they haven't been varied online.
1301 * Thus, rp may validly be NULL here.
1305 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1307 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1309 /* Deactivate current view and remove all views. */
1310 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1312 rp->view->fn->deactivate(rp->view);
1315 while (!list_empty(&rp->view_list)) {
1316 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1319 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1320 raw3270_del_view(v);
1321 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1323 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1325 mutex_lock(&raw3270_mutex);
1326 list_for_each_entry(np, &raw3270_notifier, list)
1327 np->notifier(rp->minor, 0);
1328 mutex_unlock(&raw3270_mutex);
1330 /* Reset 3270 device. */
1331 raw3270_reset_device(rp);
1332 /* And finally remove it. */
1333 raw3270_delete_device(rp);
1337 * Set 3270 device offline.
1340 raw3270_set_offline (struct ccw_device *cdev)
1344 rp = cdev->dev.driver_data;
1345 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1347 raw3270_remove(cdev);
1351 static struct ccw_device_id raw3270_id[] = {
1352 { CCW_DEVICE(0x3270, 0) },
1353 { CCW_DEVICE(0x3271, 0) },
1354 { CCW_DEVICE(0x3272, 0) },
1355 { CCW_DEVICE(0x3273, 0) },
1356 { CCW_DEVICE(0x3274, 0) },
1357 { CCW_DEVICE(0x3275, 0) },
1358 { CCW_DEVICE(0x3276, 0) },
1359 { CCW_DEVICE(0x3277, 0) },
1360 { CCW_DEVICE(0x3278, 0) },
1361 { CCW_DEVICE(0x3279, 0) },
1362 { CCW_DEVICE(0x3174, 0) },
1363 { /* end of list */ },
1366 static struct ccw_driver raw3270_ccw_driver = {
1368 .owner = THIS_MODULE,
1370 .probe = &raw3270_probe,
1371 .remove = &raw3270_remove,
1372 .set_online = &raw3270_set_online,
1373 .set_offline = &raw3270_set_offline,
1382 if (raw3270_registered)
1384 raw3270_registered = 1;
1385 rc = ccw_driver_register(&raw3270_ccw_driver);
1387 /* Create attributes for early (= console) device. */
1388 mutex_lock(&raw3270_mutex);
1389 class3270 = class_create(THIS_MODULE, "3270");
1390 list_for_each_entry(rp, &raw3270_devices, list) {
1391 get_device(&rp->cdev->dev);
1392 raw3270_create_attributes(rp);
1394 mutex_unlock(&raw3270_mutex);
1402 ccw_driver_unregister(&raw3270_ccw_driver);
1403 class_destroy(class3270);
1406 MODULE_LICENSE("GPL");
1408 module_init(raw3270_init);
1409 module_exit(raw3270_exit);
1411 EXPORT_SYMBOL(raw3270_request_alloc);
1412 EXPORT_SYMBOL(raw3270_request_free);
1413 EXPORT_SYMBOL(raw3270_request_reset);
1414 EXPORT_SYMBOL(raw3270_request_set_cmd);
1415 EXPORT_SYMBOL(raw3270_request_add_data);
1416 EXPORT_SYMBOL(raw3270_request_set_data);
1417 EXPORT_SYMBOL(raw3270_request_set_idal);
1418 EXPORT_SYMBOL(raw3270_buffer_address);
1419 EXPORT_SYMBOL(raw3270_add_view);
1420 EXPORT_SYMBOL(raw3270_del_view);
1421 EXPORT_SYMBOL(raw3270_find_view);
1422 EXPORT_SYMBOL(raw3270_activate_view);
1423 EXPORT_SYMBOL(raw3270_deactivate_view);
1424 EXPORT_SYMBOL(raw3270_start);
1425 EXPORT_SYMBOL(raw3270_start_locked);
1426 EXPORT_SYMBOL(raw3270_start_irq);
1427 EXPORT_SYMBOL(raw3270_reset);
1428 EXPORT_SYMBOL(raw3270_register_notifier);
1429 EXPORT_SYMBOL(raw3270_unregister_notifier);
1430 EXPORT_SYMBOL(raw3270_wait_queue);