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 #include <linux/major.h>
29 #include <linux/kdev_t.h>
30 #include <linux/device.h>
32 struct class *class3270;
34 /* The main 3270 data structure. */
36 struct list_head list;
37 struct ccw_device *cdev;
40 short model, rows, cols;
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 class_device *clttydev; /* 3270-class tty device ptr */
51 struct class_device *cltubdev; /* 3270-class tub device ptr */
55 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
56 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
57 #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
58 #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
59 #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
61 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
62 static DECLARE_MUTEX(raw3270_sem);
64 /* List of 3270 devices. */
65 static struct list_head raw3270_devices = LIST_HEAD_INIT(raw3270_devices);
68 * Flag to indicate if the driver has been registered. Some operations
69 * like waiting for the end of i/o need to be done differently as long
70 * as the kernel is still starting up (console support).
72 static int raw3270_registered;
74 /* Module parameters */
75 static int tubxcorrect = 0;
76 module_param(tubxcorrect, bool, 0);
79 * Wait queue for device init/delete, view delete.
81 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
84 * Encode array for 12 bit 3270 addresses.
86 unsigned char raw3270_ebcgraf[64] = {
87 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
88 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
89 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
90 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
91 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
92 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
93 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
94 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
98 raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
100 if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
101 cp[0] = (addr >> 8) & 0x3f;
104 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
105 cp[1] = raw3270_ebcgraf[addr & 0x3f];
110 * Allocate a new 3270 ccw request
112 struct raw3270_request *
113 raw3270_request_alloc(size_t size)
115 struct raw3270_request *rq;
117 /* Allocate request structure */
118 rq = kmalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
120 return ERR_PTR(-ENOMEM);
121 memset(rq, 0, sizeof(struct raw3270_request));
123 /* alloc output buffer. */
125 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
128 return ERR_PTR(-ENOMEM);
132 INIT_LIST_HEAD(&rq->list);
137 rq->ccw.cda = __pa(rq->buffer);
138 rq->ccw.flags = CCW_FLAG_SLI;
143 #ifdef CONFIG_TN3270_CONSOLE
145 * Allocate a new 3270 ccw request from bootmem. Only works very
146 * early in the boot process. Only con3270.c should be using this.
148 struct raw3270_request *
149 raw3270_request_alloc_bootmem(size_t size)
151 struct raw3270_request *rq;
153 rq = alloc_bootmem_low(sizeof(struct raw3270));
155 return ERR_PTR(-ENOMEM);
156 memset(rq, 0, sizeof(struct raw3270_request));
158 /* alloc output buffer. */
160 rq->buffer = alloc_bootmem_low(size);
162 free_bootmem((unsigned long) rq,
163 sizeof(struct raw3270));
164 return ERR_PTR(-ENOMEM);
168 INIT_LIST_HEAD(&rq->list);
173 rq->ccw.cda = __pa(rq->buffer);
174 rq->ccw.flags = CCW_FLAG_SLI;
181 * Free 3270 ccw request
184 raw3270_request_free (struct raw3270_request *rq)
192 * Reset request to initial state.
195 raw3270_request_reset(struct raw3270_request *rq)
197 BUG_ON(!list_empty(&rq->list));
198 rq->ccw.cmd_code = 0;
200 rq->ccw.cda = __pa(rq->buffer);
201 rq->ccw.flags = CCW_FLAG_SLI;
207 * Set command code to ccw of a request.
210 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
212 rq->ccw.cmd_code = cmd;
216 * Add data fragment to output buffer.
219 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
221 if (size + rq->ccw.count > rq->size)
223 memcpy(rq->buffer + rq->ccw.count, data, size);
224 rq->ccw.count += size;
229 * Set address/length pair to ccw of a request.
232 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
234 rq->ccw.cda = __pa(data);
235 rq->ccw.count = size;
239 * Set idal buffer to ccw of a request.
242 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
244 rq->ccw.cda = __pa(ib->data);
245 rq->ccw.count = ib->size;
246 rq->ccw.flags |= CCW_FLAG_IDA;
253 raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
258 if (raw3270_request_final(rq))
260 /* Check if interrupt has already been processed */
261 for (retries = 0; retries < 5; retries++) {
263 rc = ccw_device_halt(rp->cdev, (long) rq);
265 rc = ccw_device_clear(rp->cdev, (long) rq);
267 break; /* termination successful */
273 raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
278 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
279 rc = raw3270_halt_io_nolock(rp, rq);
280 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
285 * Add the request to the request queue, try to start it if the
286 * 3270 device is idle. Return without waiting for end of i/o.
289 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
290 struct raw3270_request *rq)
293 raw3270_get_view(view);
294 if (list_empty(&rp->req_queue) &&
295 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
296 /* No other requests are on the queue. Start this one. */
297 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
298 (unsigned long) rq, 0, 0);
300 raw3270_put_view(view);
304 list_add_tail(&rq->list, &rp->req_queue);
309 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
315 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
317 if (!rp || rp->view != view)
319 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
322 rc = __raw3270_start(rp, view, rq);
323 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
328 raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
334 if (!rp || rp->view != view)
336 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
339 rc = __raw3270_start(rp, view, rq);
344 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
350 raw3270_get_view(view);
351 list_add_tail(&rq->list, &rp->req_queue);
356 * 3270 interrupt routine, called from the ccw_device layer
359 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
362 struct raw3270_view *view;
363 struct raw3270_request *rq;
366 rp = (struct raw3270 *) cdev->dev.driver_data;
369 rq = (struct raw3270_request *) intparm;
370 view = rq ? rq->view : rp->view;
373 rc = RAW3270_IO_RETRY;
374 else if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
376 rc = RAW3270_IO_DONE;
377 } else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
378 DEV_STAT_UNIT_EXCEP)) {
379 /* Handle CE-DE-UE and subsequent UDE */
380 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
381 rc = RAW3270_IO_BUSY;
382 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
383 /* Wait for UDE if busy flag is set. */
384 if (irb->scsw.dstat & DEV_STAT_DEV_END) {
385 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
386 /* Got it, now retry. */
387 rc = RAW3270_IO_RETRY;
389 rc = RAW3270_IO_BUSY;
391 rc = view->fn->intv(view, rq, irb);
393 rc = RAW3270_IO_DONE;
396 case RAW3270_IO_DONE:
398 case RAW3270_IO_BUSY:
400 * Intervention required by the operator. We have to wait
401 * for unsolicited device end.
404 case RAW3270_IO_RETRY:
407 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
408 (unsigned long) rq, 0, 0);
410 return; /* Sucessfully restarted. */
412 case RAW3270_IO_STOP:
415 raw3270_halt_io_nolock(rp, rq);
422 BUG_ON(list_empty(&rq->list));
423 /* The request completed, remove from queue and do callback. */
424 list_del_init(&rq->list);
426 rq->callback(rq, rq->callback_data);
427 /* Do put_device for get_device in raw3270_start. */
428 raw3270_put_view(view);
431 * Try to start each request on request queue until one is
432 * started successful.
434 while (!list_empty(&rp->req_queue)) {
435 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
436 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
437 (unsigned long) rq, 0, 0);
440 /* Start failed. Remove request and do callback. */
441 list_del_init(&rq->list);
443 rq->callback(rq, rq->callback_data);
444 /* Do put_device for get_device in raw3270_start. */
445 raw3270_put_view(view);
453 struct raw3270_ua { /* Query Reply structure for Usable Area */
454 struct { /* Usable Area Query Reply Base */
455 short l; /* Length of this structured field */
456 char sfid; /* 0x81 if Query Reply */
457 char qcode; /* 0x81 if Usable Area */
460 short w; /* Width of usable area */
461 short h; /* Heigth of usavle area */
462 char units; /* 0x00:in; 0x01:mm */
467 short buffsz; /* Character buffer size, bytes */
472 } __attribute__ ((packed)) uab;
473 struct { /* Alternate Usable Area Self-Defining Parameter */
474 char l; /* Length of this Self-Defining Parm */
475 char sdpid; /* 0x02 if Alternate Usable Area */
477 char auaid; /* 0x01 is Id for the A U A */
478 short wauai; /* Width of AUAi */
479 short hauai; /* Height of AUAi */
480 char auaunits; /* 0x00:in, 0x01:mm */
485 } __attribute__ ((packed)) aua;
486 } __attribute__ ((packed));
488 static unsigned char raw3270_init_data[256];
489 static struct raw3270_request raw3270_init_request;
490 static struct diag210 raw3270_init_diag210;
491 static DECLARE_MUTEX(raw3270_init_sem);
494 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
498 * Unit-Check Processing:
499 * Expect Command Reject or Intervention Required.
501 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
502 /* Request finished abnormally. */
503 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
504 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
505 return RAW3270_IO_BUSY;
509 if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
510 if (irb->ecw[0] & SNS0_CMD_REJECT)
511 rq->rc = -EOPNOTSUPP;
515 /* Request finished normally. Copy residual count. */
516 rq->rescnt = irb->scsw.count;
518 if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
519 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
520 wake_up(&raw3270_wait_queue);
522 return RAW3270_IO_DONE;
525 static struct raw3270_fn raw3270_init_fn = {
526 .intv = raw3270_init_irq
529 static struct raw3270_view raw3270_init_view = {
530 .fn = &raw3270_init_fn
534 * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
535 * Wait for end of request. The request must have been started
536 * with raw3270_start, rc = 0. The device lock may NOT have been
537 * released between calling raw3270_start and raw3270_wait.
540 raw3270_wake_init(struct raw3270_request *rq, void *data)
542 wake_up((wait_queue_head_t *) data);
546 * Special wait function that can cope with console initialization.
549 raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
550 struct raw3270_request *rq)
553 wait_queue_head_t wq;
556 #ifdef CONFIG_TN3270_CONSOLE
557 if (raw3270_registered == 0) {
558 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
560 rc = __raw3270_start(rp, view, rq);
562 while (!raw3270_request_final(rq)) {
566 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
570 init_waitqueue_head(&wq);
571 rq->callback = raw3270_wake_init;
572 rq->callback_data = &wq;
573 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
574 rc = __raw3270_start(rp, view, rq);
575 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
578 /* Now wait for the completion. */
579 rc = wait_event_interruptible(wq, raw3270_request_final(rq));
580 if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
581 raw3270_halt_io(view->dev, rq);
582 /* No wait for the halt to complete. */
583 wait_event(wq, raw3270_request_final(rq));
590 __raw3270_size_device_vm(struct raw3270 *rp)
594 raw3270_init_diag210.vrdcdvno =
595 _ccw_device_get_device_number(rp->cdev);
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(&raw3270_init_request, 0, sizeof(raw3270_init_request));
649 memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
650 /* Store 'read partition' data stream to raw3270_init_data */
651 memcpy(raw3270_init_data, wbuf, sizeof(wbuf));
652 INIT_LIST_HEAD(&raw3270_init_request.list);
653 raw3270_init_request.ccw.cmd_code = TC_WRITESF;
654 raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
655 raw3270_init_request.ccw.count = sizeof(wbuf);
656 raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
658 rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_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 raw3270_init_request.ccw.cmd_code = TC_READMOD;
684 raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
685 raw3270_init_request.ccw.count = sizeof(raw3270_init_data);
686 raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
687 rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
690 /* Got a Query Reply */
691 count = sizeof(raw3270_init_data) - raw3270_init_request.rescnt;
692 uap = (struct raw3270_ua *) (raw3270_init_data + 1);
693 /* Paranoia check. */
694 if (raw3270_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 down(&raw3270_init_sem);
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 = 0;
725 up(&raw3270_init_sem);
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 down(&raw3270_init_sem);
753 memset(&raw3270_init_request, 0, sizeof(raw3270_init_request));
754 memset(raw3270_init_data, 0, sizeof(raw3270_init_data));
755 /* Store reset data stream to raw3270_init_data/raw3270_init_request */
756 raw3270_init_data[0] = TW_KR;
757 INIT_LIST_HEAD(&raw3270_init_request.list);
758 raw3270_init_request.ccw.cmd_code = TC_EWRITEA;
759 raw3270_init_request.ccw.flags = CCW_FLAG_SLI;
760 raw3270_init_request.ccw.count = 1;
761 raw3270_init_request.ccw.cda = (__u32) __pa(raw3270_init_data);
762 rp->view = &raw3270_init_view;
763 raw3270_init_view.dev = rp;
764 rc = raw3270_start_init(rp, &raw3270_init_view, &raw3270_init_request);
765 raw3270_init_view.dev = 0;
767 up(&raw3270_init_sem);
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().
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);
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.
852 raw3270_setup_console(struct ccw_device *cdev)
858 rp = (struct raw3270 *) alloc_bootmem(sizeof(struct raw3270));
859 ascebc = (char *) alloc_bootmem(256);
860 rc = raw3270_setup_device(cdev, rp, ascebc);
863 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
864 rc = raw3270_reset_device(rp);
867 rc = raw3270_size_device(rp);
870 rc = raw3270_reset_device(rp);
873 set_bit(RAW3270_FLAGS_READY, &rp->flags);
878 raw3270_wait_cons_dev(struct raw3270 *rp)
882 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
884 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
890 * Create a 3270 device structure.
892 static struct raw3270 *
893 raw3270_create_device(struct ccw_device *cdev)
899 rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL);
901 return ERR_PTR(-ENOMEM);
902 ascebc = kmalloc(256, GFP_KERNEL);
905 return ERR_PTR(-ENOMEM);
907 rc = raw3270_setup_device(cdev, rp, ascebc);
913 /* Get reference to ccw_device structure. */
914 get_device(&cdev->dev);
922 raw3270_activate_view(struct raw3270_view *view)
925 struct raw3270_view *oldview, *nv;
932 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
933 if (rp->view == view)
935 else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
941 oldview->fn->deactivate(oldview);
944 rc = view->fn->activate(view);
946 /* Didn't work. Try to reactivate the old view. */
948 if (!oldview || oldview->fn->activate(oldview) != 0) {
949 /* Didn't work as well. Try any other view. */
950 list_for_each_entry(nv, &rp->view_list, list)
951 if (nv != view && nv != oldview) {
953 if (nv->fn->activate(nv) == 0)
960 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
965 * Deactivate current view.
968 raw3270_deactivate_view(struct raw3270_view *view)
976 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
977 if (rp->view == view) {
978 view->fn->deactivate(view);
980 /* Move deactivated view to end of list. */
981 list_del_init(&view->list);
982 list_add_tail(&view->list, &rp->view_list);
983 /* Try to activate another view. */
984 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
985 list_for_each_entry(view, &rp->view_list, list) {
987 if (view->fn->activate(view) == 0)
993 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
997 * Add view to device with minor "minor".
1000 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
1002 unsigned long flags;
1010 list_for_each_entry(rp, &raw3270_devices, list) {
1011 if (rp->minor != minor)
1013 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1014 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1015 atomic_set(&view->ref_count, 2);
1018 view->model = rp->model;
1019 view->rows = rp->rows;
1020 view->cols = rp->cols;
1021 view->ascebc = rp->ascebc;
1022 spin_lock_init(&view->lock);
1023 list_add(&view->list, &rp->view_list);
1026 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1034 * Find specific view of device with minor "minor".
1036 struct raw3270_view *
1037 raw3270_find_view(struct raw3270_fn *fn, int minor)
1040 struct raw3270_view *view, *tmp;
1041 unsigned long flags;
1044 view = ERR_PTR(-ENODEV);
1045 list_for_each_entry(rp, &raw3270_devices, list) {
1046 if (rp->minor != minor)
1048 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1049 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1050 view = ERR_PTR(-ENOENT);
1051 list_for_each_entry(tmp, &rp->view_list, list) {
1052 if (tmp->fn == fn) {
1053 raw3270_get_view(tmp);
1059 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1067 * Remove view from device and free view structure via call to view->fn->free.
1070 raw3270_del_view(struct raw3270_view *view)
1072 unsigned long flags;
1074 struct raw3270_view *nv;
1077 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1078 if (rp->view == view) {
1079 view->fn->deactivate(view);
1082 list_del_init(&view->list);
1083 if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
1084 /* Try to activate another view. */
1085 list_for_each_entry(nv, &rp->view_list, list) {
1086 if (nv->fn->activate(nv) == 0) {
1092 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1093 /* Wait for reference counter to drop to zero. */
1094 atomic_dec(&view->ref_count);
1095 wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1097 view->fn->free(view);
1101 * Remove a 3270 device structure.
1104 raw3270_delete_device(struct raw3270 *rp)
1106 struct ccw_device *cdev;
1108 /* Remove from device chain. */
1111 class_device_destroy(class3270,
1112 MKDEV(IBM_TTY3270_MAJOR, rp->minor));
1114 class_device_destroy(class3270,
1115 MKDEV(IBM_FS3270_MAJOR, rp->minor));
1116 list_del_init(&rp->list);
1119 /* Disconnect from ccw_device. */
1122 cdev->dev.driver_data = 0;
1125 /* Put ccw_device structure. */
1126 put_device(&cdev->dev);
1128 /* Now free raw3270 structure. */
1134 raw3270_probe (struct ccw_device *cdev)
1140 * Additional attributes for a 3270 device
1143 raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
1145 return snprintf(buf, PAGE_SIZE, "%i\n",
1146 ((struct raw3270 *) dev->driver_data)->model);
1148 static DEVICE_ATTR(model, 0444, raw3270_model_show, 0);
1151 raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
1153 return snprintf(buf, PAGE_SIZE, "%i\n",
1154 ((struct raw3270 *) dev->driver_data)->rows);
1156 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, 0);
1159 raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
1161 return snprintf(buf, PAGE_SIZE, "%i\n",
1162 ((struct raw3270 *) dev->driver_data)->cols);
1164 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, 0);
1166 static struct attribute * raw3270_attrs[] = {
1167 &dev_attr_model.attr,
1168 &dev_attr_rows.attr,
1169 &dev_attr_columns.attr,
1173 static struct attribute_group raw3270_attr_group = {
1174 .attrs = raw3270_attrs,
1178 raw3270_create_attributes(struct raw3270 *rp)
1180 //FIXME: check return code
1181 sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1183 class_device_create(class3270,
1184 MKDEV(IBM_TTY3270_MAJOR, rp->minor),
1185 &rp->cdev->dev, "tty%s",
1186 rp->cdev->dev.bus_id);
1188 class_device_create(class3270,
1189 MKDEV(IBM_FS3270_MAJOR, rp->minor),
1190 &rp->cdev->dev, "tub%s",
1191 rp->cdev->dev.bus_id);
1195 * Notifier for device addition/removal
1197 struct raw3270_notifier {
1198 struct list_head list;
1199 void (*notifier)(int, int);
1202 static struct list_head raw3270_notifier = LIST_HEAD_INIT(raw3270_notifier);
1204 int raw3270_register_notifier(void (*notifier)(int, int))
1206 struct raw3270_notifier *np;
1209 np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
1212 np->notifier = notifier;
1214 list_add_tail(&np->list, &raw3270_notifier);
1215 list_for_each_entry(rp, &raw3270_devices, list) {
1216 get_device(&rp->cdev->dev);
1217 notifier(rp->minor, 1);
1223 void raw3270_unregister_notifier(void (*notifier)(int, int))
1225 struct raw3270_notifier *np;
1228 list_for_each_entry(np, &raw3270_notifier, list)
1229 if (np->notifier == notifier) {
1230 list_del(&np->list);
1238 * Set 3270 device online.
1241 raw3270_set_online (struct ccw_device *cdev)
1244 struct raw3270_notifier *np;
1247 rp = raw3270_create_device(cdev);
1250 rc = raw3270_reset_device(rp);
1253 rc = raw3270_size_device(rp);
1256 rc = raw3270_reset_device(rp);
1259 raw3270_create_attributes(rp);
1260 set_bit(RAW3270_FLAGS_READY, &rp->flags);
1262 list_for_each_entry(np, &raw3270_notifier, list)
1263 np->notifier(rp->minor, 1);
1268 raw3270_delete_device(rp);
1273 * Remove 3270 device structure.
1276 raw3270_remove (struct ccw_device *cdev)
1278 unsigned long flags;
1280 struct raw3270_view *v;
1281 struct raw3270_notifier *np;
1283 rp = cdev->dev.driver_data;
1285 * _remove is the opposite of _probe; it's probe that
1286 * should set up rp. raw3270_remove gets entered for
1287 * devices even if they haven't been varied online.
1288 * Thus, rp may validly be NULL here.
1292 clear_bit(RAW3270_FLAGS_READY, &rp->flags);
1294 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1296 /* Deactivate current view and remove all views. */
1297 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1299 rp->view->fn->deactivate(rp->view);
1302 while (!list_empty(&rp->view_list)) {
1303 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1306 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1307 raw3270_del_view(v);
1308 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1310 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1313 list_for_each_entry(np, &raw3270_notifier, list)
1314 np->notifier(rp->minor, 0);
1317 /* Reset 3270 device. */
1318 raw3270_reset_device(rp);
1319 /* And finally remove it. */
1320 raw3270_delete_device(rp);
1324 * Set 3270 device offline.
1327 raw3270_set_offline (struct ccw_device *cdev)
1331 rp = cdev->dev.driver_data;
1332 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1334 raw3270_remove(cdev);
1338 static struct ccw_device_id raw3270_id[] = {
1339 { CCW_DEVICE(0x3270, 0) },
1340 { CCW_DEVICE(0x3271, 0) },
1341 { CCW_DEVICE(0x3272, 0) },
1342 { CCW_DEVICE(0x3273, 0) },
1343 { CCW_DEVICE(0x3274, 0) },
1344 { CCW_DEVICE(0x3275, 0) },
1345 { CCW_DEVICE(0x3276, 0) },
1346 { CCW_DEVICE(0x3277, 0) },
1347 { CCW_DEVICE(0x3278, 0) },
1348 { CCW_DEVICE(0x3279, 0) },
1349 { CCW_DEVICE(0x3174, 0) },
1350 { /* end of list */ },
1353 static struct ccw_driver raw3270_ccw_driver = {
1355 .owner = THIS_MODULE,
1357 .probe = &raw3270_probe,
1358 .remove = &raw3270_remove,
1359 .set_online = &raw3270_set_online,
1360 .set_offline = &raw3270_set_offline,
1369 if (raw3270_registered)
1371 raw3270_registered = 1;
1372 rc = ccw_driver_register(&raw3270_ccw_driver);
1374 /* Create attributes for early (= console) device. */
1376 class3270 = class_create(THIS_MODULE, "3270");
1377 list_for_each_entry(rp, &raw3270_devices, list) {
1378 get_device(&rp->cdev->dev);
1379 raw3270_create_attributes(rp);
1389 ccw_driver_unregister(&raw3270_ccw_driver);
1390 class_destroy(class3270);
1393 MODULE_LICENSE("GPL");
1395 module_init(raw3270_init);
1396 module_exit(raw3270_exit);
1398 EXPORT_SYMBOL(raw3270_request_alloc);
1399 EXPORT_SYMBOL(raw3270_request_free);
1400 EXPORT_SYMBOL(raw3270_request_reset);
1401 EXPORT_SYMBOL(raw3270_request_set_cmd);
1402 EXPORT_SYMBOL(raw3270_request_add_data);
1403 EXPORT_SYMBOL(raw3270_request_set_data);
1404 EXPORT_SYMBOL(raw3270_request_set_idal);
1405 EXPORT_SYMBOL(raw3270_buffer_address);
1406 EXPORT_SYMBOL(raw3270_add_view);
1407 EXPORT_SYMBOL(raw3270_del_view);
1408 EXPORT_SYMBOL(raw3270_find_view);
1409 EXPORT_SYMBOL(raw3270_activate_view);
1410 EXPORT_SYMBOL(raw3270_deactivate_view);
1411 EXPORT_SYMBOL(raw3270_start);
1412 EXPORT_SYMBOL(raw3270_start_locked);
1413 EXPORT_SYMBOL(raw3270_start_irq);
1414 EXPORT_SYMBOL(raw3270_reset);
1415 EXPORT_SYMBOL(raw3270_register_notifier);
1416 EXPORT_SYMBOL(raw3270_unregister_notifier);
1417 EXPORT_SYMBOL(raw3270_wait_queue);