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 struct class *class3270;
33 /* The main 3270 data structure. */
35 struct list_head list;
36 struct ccw_device *cdev;
39 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 */
51 struct raw3270_view init_view;
52 struct raw3270_request init_reset;
53 struct raw3270_request init_readpart;
54 struct raw3270_request init_readmod;
55 unsigned char init_data[256];
59 #define RAW3270_STATE_INIT 0 /* Initial state */
60 #define RAW3270_STATE_RESET 1 /* Reset command is pending */
61 #define RAW3270_STATE_W4ATTN 2 /* Wait for attention interrupt */
62 #define RAW3270_STATE_READMOD 3 /* Read partition is pending */
63 #define RAW3270_STATE_READY 4 /* Device is usable by views */
66 #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
67 #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
68 #define RAW3270_FLAGS_CONSOLE 2 /* Device is the console. */
69 #define RAW3270_FLAGS_FROZEN 3 /* set if 3270 is frozen for suspend */
71 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
72 static DEFINE_MUTEX(raw3270_mutex);
74 /* List of 3270 devices. */
75 static LIST_HEAD(raw3270_devices);
78 * Flag to indicate if the driver has been registered. Some operations
79 * like waiting for the end of i/o need to be done differently as long
80 * as the kernel is still starting up (console support).
82 static int raw3270_registered;
84 /* Module parameters */
85 static bool tubxcorrect = 0;
86 module_param(tubxcorrect, bool, 0);
89 * Wait queue for device init/delete, view delete.
91 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
94 * Encode array for 12 bit 3270 addresses.
96 static unsigned char raw3270_ebcgraf[64] = {
97 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
98 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
99 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
100 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
101 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
102 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
103 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
104 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
107 static inline int raw3270_state_ready(struct raw3270 *rp)
109 return rp->state == RAW3270_STATE_READY;
112 static inline int raw3270_state_final(struct raw3270 *rp)
114 return rp->state == RAW3270_STATE_INIT ||
115 rp->state == RAW3270_STATE_READY;
119 raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
121 if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
122 cp[0] = (addr >> 8) & 0x3f;
125 cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
126 cp[1] = raw3270_ebcgraf[addr & 0x3f];
131 * Allocate a new 3270 ccw request
133 struct raw3270_request *
134 raw3270_request_alloc(size_t size)
136 struct raw3270_request *rq;
138 /* Allocate request structure */
139 rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
141 return ERR_PTR(-ENOMEM);
143 /* alloc output buffer. */
145 rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
148 return ERR_PTR(-ENOMEM);
152 INIT_LIST_HEAD(&rq->list);
157 rq->ccw.cda = __pa(rq->buffer);
158 rq->ccw.flags = CCW_FLAG_SLI;
164 * Free 3270 ccw request
167 raw3270_request_free (struct raw3270_request *rq)
174 * Reset request to initial state.
177 raw3270_request_reset(struct raw3270_request *rq)
179 BUG_ON(!list_empty(&rq->list));
180 rq->ccw.cmd_code = 0;
182 rq->ccw.cda = __pa(rq->buffer);
183 rq->ccw.flags = CCW_FLAG_SLI;
189 * Set command code to ccw of a request.
192 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
194 rq->ccw.cmd_code = cmd;
198 * Add data fragment to output buffer.
201 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
203 if (size + rq->ccw.count > rq->size)
205 memcpy(rq->buffer + rq->ccw.count, data, size);
206 rq->ccw.count += size;
211 * Set address/length pair to ccw of a request.
214 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
216 rq->ccw.cda = __pa(data);
217 rq->ccw.count = size;
221 * Set idal buffer to ccw of a request.
224 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
226 rq->ccw.cda = __pa(ib->data);
227 rq->ccw.count = ib->size;
228 rq->ccw.flags |= CCW_FLAG_IDA;
235 __raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
240 if (raw3270_request_final(rq))
242 /* Check if interrupt has already been processed */
243 for (retries = 0; retries < 5; retries++) {
245 rc = ccw_device_halt(rp->cdev, (long) rq);
247 rc = ccw_device_clear(rp->cdev, (long) rq);
249 break; /* termination successful */
255 * Add the request to the request queue, try to start it if the
256 * 3270 device is idle. Return without waiting for end of i/o.
259 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
260 struct raw3270_request *rq)
263 raw3270_get_view(view);
264 if (list_empty(&rp->req_queue) &&
265 !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
266 /* No other requests are on the queue. Start this one. */
267 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
268 (unsigned long) rq, 0, 0);
270 raw3270_put_view(view);
274 list_add_tail(&rq->list, &rp->req_queue);
279 raw3270_view_active(struct raw3270_view *view)
281 struct raw3270 *rp = view->dev;
283 return rp && rp->view == view &&
284 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
288 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
294 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
296 if (!rp || rp->view != view ||
297 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
299 else if (!raw3270_state_ready(rp))
302 rc = __raw3270_start(rp, view, rq);
303 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
308 raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
314 if (!rp || rp->view != view ||
315 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
317 else if (!raw3270_state_ready(rp))
320 rc = __raw3270_start(rp, view, rq);
325 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
331 raw3270_get_view(view);
332 list_add_tail(&rq->list, &rp->req_queue);
337 * 3270 interrupt routine, called from the ccw_device layer
340 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
343 struct raw3270_view *view;
344 struct raw3270_request *rq;
347 rp = dev_get_drvdata(&cdev->dev);
350 rq = (struct raw3270_request *) intparm;
351 view = rq ? rq->view : rp->view;
354 rc = RAW3270_IO_RETRY;
355 else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
357 rc = RAW3270_IO_DONE;
358 } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
359 DEV_STAT_UNIT_EXCEP)) {
360 /* Handle CE-DE-UE and subsequent UDE */
361 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
362 rc = RAW3270_IO_BUSY;
363 } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
364 /* Wait for UDE if busy flag is set. */
365 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
366 clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
367 /* Got it, now retry. */
368 rc = RAW3270_IO_RETRY;
370 rc = RAW3270_IO_BUSY;
372 rc = view->fn->intv(view, rq, irb);
374 rc = RAW3270_IO_DONE;
377 case RAW3270_IO_DONE:
379 case RAW3270_IO_BUSY:
381 * Intervention required by the operator. We have to wait
382 * for unsolicited device end.
385 case RAW3270_IO_RETRY:
388 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
389 (unsigned long) rq, 0, 0);
391 return; /* Successfully restarted. */
393 case RAW3270_IO_STOP:
396 __raw3270_halt_io(rp, rq);
403 BUG_ON(list_empty(&rq->list));
404 /* The request completed, remove from queue and do callback. */
405 list_del_init(&rq->list);
407 rq->callback(rq, rq->callback_data);
408 /* Do put_device for get_device in raw3270_start. */
409 raw3270_put_view(view);
412 * Try to start each request on request queue until one is
413 * started successful.
415 while (!list_empty(&rp->req_queue)) {
416 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
417 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
418 (unsigned long) rq, 0, 0);
421 /* Start failed. Remove request and do callback. */
422 list_del_init(&rq->list);
424 rq->callback(rq, rq->callback_data);
425 /* Do put_device for get_device in raw3270_start. */
426 raw3270_put_view(view);
431 * To determine the size of the 3270 device we need to do:
432 * 1) send a 'read partition' data stream to the device
433 * 2) wait for the attn interrupt that precedes the query reply
434 * 3) do a read modified to get the query reply
435 * To make things worse we have to cope with intervention
436 * required (3270 device switched to 'stand-by') and command
437 * rejects (old devices that can't do 'read partition').
439 struct raw3270_ua { /* Query Reply structure for Usable Area */
440 struct { /* Usable Area Query Reply Base */
441 short l; /* Length of this structured field */
442 char sfid; /* 0x81 if Query Reply */
443 char qcode; /* 0x81 if Usable Area */
446 short w; /* Width of usable area */
447 short h; /* Heigth of usavle area */
448 char units; /* 0x00:in; 0x01:mm */
453 short buffsz; /* Character buffer size, bytes */
458 } __attribute__ ((packed)) uab;
459 struct { /* Alternate Usable Area Self-Defining Parameter */
460 char l; /* Length of this Self-Defining Parm */
461 char sdpid; /* 0x02 if Alternate Usable Area */
463 char auaid; /* 0x01 is Id for the A U A */
464 short wauai; /* Width of AUAi */
465 short hauai; /* Height of AUAi */
466 char auaunits; /* 0x00:in, 0x01:mm */
471 } __attribute__ ((packed)) aua;
472 } __attribute__ ((packed));
475 raw3270_size_device_vm(struct raw3270 *rp)
478 struct ccw_dev_id dev_id;
479 struct diag210 diag_data;
481 ccw_device_get_id(rp->cdev, &dev_id);
482 diag_data.vrdcdvno = dev_id.devno;
483 diag_data.vrdclen = sizeof(struct diag210);
484 rc = diag210(&diag_data);
485 model = diag_data.vrdccrmd;
486 /* Use default model 2 if the size could not be detected */
487 if (rc || model < 2 || model > 5)
514 raw3270_size_device(struct raw3270 *rp)
516 struct raw3270_ua *uap;
518 /* Got a Query Reply */
519 uap = (struct raw3270_ua *) (rp->init_data + 1);
520 /* Paranoia check. */
521 if (rp->init_readmod.rc || rp->init_data[0] != 0x88 ||
522 uap->uab.qcode != 0x81) {
523 /* Couldn't detect size. Use default model 2. */
529 /* Copy rows/columns of default Usable Area */
530 rp->rows = uap->uab.h;
531 rp->cols = uap->uab.w;
532 /* Check for 14 bit addressing */
533 if ((uap->uab.flags0 & 0x0d) == 0x01)
534 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
535 /* Check for Alternate Usable Area */
536 if (uap->uab.l == sizeof(struct raw3270_ua) &&
537 uap->aua.sdpid == 0x02) {
538 rp->rows = uap->aua.hauai;
539 rp->cols = uap->aua.wauai;
541 /* Try to find a model. */
543 if (rp->rows == 24 && rp->cols == 80)
545 if (rp->rows == 32 && rp->cols == 80)
547 if (rp->rows == 43 && rp->cols == 80)
549 if (rp->rows == 27 && rp->cols == 132)
554 raw3270_size_device_done(struct raw3270 *rp)
556 struct raw3270_view *view;
559 rp->state = RAW3270_STATE_READY;
560 /* Notify views about new size */
561 list_for_each_entry(view, &rp->view_list, list)
562 if (view->fn->resize)
563 view->fn->resize(view, rp->model, rp->rows, rp->cols);
564 /* Setup processing done, now activate a view */
565 list_for_each_entry(view, &rp->view_list, list) {
567 if (view->fn->activate(view) == 0)
574 raw3270_read_modified_cb(struct raw3270_request *rq, void *data)
576 struct raw3270 *rp = rq->view->dev;
578 raw3270_size_device(rp);
579 raw3270_size_device_done(rp);
583 raw3270_read_modified(struct raw3270 *rp)
585 if (rp->state != RAW3270_STATE_W4ATTN)
587 /* Use 'read modified' to get the result of a read partition. */
588 memset(&rp->init_readmod, 0, sizeof(rp->init_readmod));
589 memset(&rp->init_data, 0, sizeof(rp->init_data));
590 rp->init_readmod.ccw.cmd_code = TC_READMOD;
591 rp->init_readmod.ccw.flags = CCW_FLAG_SLI;
592 rp->init_readmod.ccw.count = sizeof(rp->init_data);
593 rp->init_readmod.ccw.cda = (__u32) __pa(rp->init_data);
594 rp->init_readmod.callback = raw3270_read_modified_cb;
595 rp->state = RAW3270_STATE_READMOD;
596 raw3270_start_irq(&rp->init_view, &rp->init_readmod);
600 raw3270_writesf_readpart(struct raw3270 *rp)
602 static const unsigned char wbuf[] =
603 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
605 /* Store 'read partition' data stream to init_data */
606 memset(&rp->init_readpart, 0, sizeof(rp->init_readpart));
607 memset(&rp->init_data, 0, sizeof(rp->init_data));
608 memcpy(&rp->init_data, wbuf, sizeof(wbuf));
609 rp->init_readpart.ccw.cmd_code = TC_WRITESF;
610 rp->init_readpart.ccw.flags = CCW_FLAG_SLI;
611 rp->init_readpart.ccw.count = sizeof(wbuf);
612 rp->init_readpart.ccw.cda = (__u32) __pa(&rp->init_data);
613 rp->state = RAW3270_STATE_W4ATTN;
614 raw3270_start_irq(&rp->init_view, &rp->init_readpart);
621 raw3270_reset_device_cb(struct raw3270_request *rq, void *data)
623 struct raw3270 *rp = rq->view->dev;
625 if (rp->state != RAW3270_STATE_RESET)
628 /* Reset command failed. */
629 rp->state = RAW3270_STATE_INIT;
630 } else if (MACHINE_IS_VM) {
631 raw3270_size_device_vm(rp);
632 raw3270_size_device_done(rp);
634 raw3270_writesf_readpart(rp);
635 memset(&rp->init_reset, 0, sizeof(rp->init_reset));
636 memset(&rp->init_data, 0, sizeof(rp->init_data));
640 __raw3270_reset_device(struct raw3270 *rp)
644 /* Check if reset is already pending */
645 if (rp->init_reset.view)
647 /* Store reset data stream to init_data/init_reset */
648 rp->init_data[0] = TW_KR;
649 rp->init_reset.ccw.cmd_code = TC_EWRITEA;
650 rp->init_reset.ccw.flags = CCW_FLAG_SLI;
651 rp->init_reset.ccw.count = 1;
652 rp->init_reset.ccw.cda = (__u32) __pa(rp->init_data);
653 rp->init_reset.callback = raw3270_reset_device_cb;
654 rc = __raw3270_start(rp, &rp->init_view, &rp->init_reset);
655 if (rc == 0 && rp->state == RAW3270_STATE_INIT)
656 rp->state = RAW3270_STATE_RESET;
661 raw3270_reset_device(struct raw3270 *rp)
666 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
667 rc = __raw3270_reset_device(rp);
668 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
673 raw3270_reset(struct raw3270_view *view)
679 if (!rp || rp->view != view ||
680 test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
682 else if (!raw3270_state_ready(rp))
685 rc = raw3270_reset_device(view->dev);
690 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
696 * Unit-Check Processing:
697 * Expect Command Reject or Intervention Required.
699 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
700 /* Request finished abnormally. */
701 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
702 set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
703 return RAW3270_IO_BUSY;
707 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
708 if (irb->ecw[0] & SNS0_CMD_REJECT)
709 rq->rc = -EOPNOTSUPP;
714 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
715 /* Queue read modified after attention interrupt */
717 raw3270_read_modified(rp);
719 return RAW3270_IO_DONE;
722 static struct raw3270_fn raw3270_init_fn = {
723 .intv = raw3270_init_irq
727 * Setup new 3270 device.
730 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
736 memset(rp, 0, sizeof(struct raw3270));
737 /* Copy ebcdic -> ascii translation table. */
738 memcpy(ascebc, _ascebc, 256);
740 /* correct brackets and circumflex */
751 INIT_LIST_HEAD(&rp->req_queue);
752 INIT_LIST_HEAD(&rp->view_list);
754 rp->init_view.dev = rp;
755 rp->init_view.fn = &raw3270_init_fn;
756 rp->view = &rp->init_view;
759 * Add device to list and find the smallest unused minor
760 * number for it. Note: there is no device with minor 0,
761 * see special case for fs3270.c:fs3270_open().
763 mutex_lock(&raw3270_mutex);
764 /* Keep the list sorted. */
765 minor = RAW3270_FIRSTMINOR;
767 list_for_each(l, &raw3270_devices) {
768 tmp = list_entry(l, struct raw3270, list);
769 if (tmp->minor > minor) {
771 __list_add(&rp->list, l->prev, l);
776 if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
778 list_add_tail(&rp->list, &raw3270_devices);
780 mutex_unlock(&raw3270_mutex);
781 /* No free minor number? Then give up. */
785 dev_set_drvdata(&cdev->dev, rp);
786 cdev->handler = raw3270_irq;
790 #ifdef CONFIG_TN3270_CONSOLE
791 /* Tentative definition - see below for actual definition. */
792 static struct ccw_driver raw3270_ccw_driver;
795 * Setup 3270 device configured as console.
797 struct raw3270 __init *raw3270_setup_console(void)
799 struct ccw_device *cdev;
805 cdev = ccw_device_create_console(&raw3270_ccw_driver);
807 return ERR_CAST(cdev);
809 rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
810 ascebc = kzalloc(256, GFP_KERNEL);
811 rc = raw3270_setup_device(cdev, rp, ascebc);
814 set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
816 rc = ccw_device_enable_console(cdev);
818 ccw_device_destroy_console(cdev);
822 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
824 __raw3270_reset_device(rp);
825 while (!raw3270_state_final(rp)) {
826 ccw_device_wait_idle(rp->cdev);
829 } while (rp->state != RAW3270_STATE_READY);
830 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
835 raw3270_wait_cons_dev(struct raw3270 *rp)
839 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
840 ccw_device_wait_idle(rp->cdev);
841 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
847 * Create a 3270 device structure.
849 static struct raw3270 *
850 raw3270_create_device(struct ccw_device *cdev)
856 rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
858 return ERR_PTR(-ENOMEM);
859 ascebc = kmalloc(256, GFP_KERNEL);
862 return ERR_PTR(-ENOMEM);
864 rc = raw3270_setup_device(cdev, rp, ascebc);
870 /* Get reference to ccw_device structure. */
871 get_device(&cdev->dev);
879 raw3270_activate_view(struct raw3270_view *view)
882 struct raw3270_view *oldview, *nv;
889 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
890 if (rp->view == view)
892 else if (!raw3270_state_ready(rp))
894 else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
898 if (rp->view && rp->view->fn->deactivate) {
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 (raw3270_state_ready(rp) &&
944 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
945 list_for_each_entry(view, &rp->view_list, list) {
947 if (view->fn->activate(view) == 0)
953 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
957 * Add view to device with minor "minor".
960 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
968 mutex_lock(&raw3270_mutex);
970 list_for_each_entry(rp, &raw3270_devices, list) {
971 if (rp->minor != minor)
973 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
974 atomic_set(&view->ref_count, 2);
977 view->model = rp->model;
978 view->rows = rp->rows;
979 view->cols = rp->cols;
980 view->ascebc = rp->ascebc;
981 spin_lock_init(&view->lock);
982 list_add(&view->list, &rp->view_list);
984 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
987 mutex_unlock(&raw3270_mutex);
992 * Find specific view of device with minor "minor".
994 struct raw3270_view *
995 raw3270_find_view(struct raw3270_fn *fn, int minor)
998 struct raw3270_view *view, *tmp;
1001 mutex_lock(&raw3270_mutex);
1002 view = ERR_PTR(-ENODEV);
1003 list_for_each_entry(rp, &raw3270_devices, list) {
1004 if (rp->minor != minor)
1006 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1007 list_for_each_entry(tmp, &rp->view_list, list) {
1008 if (tmp->fn == fn) {
1009 raw3270_get_view(tmp);
1014 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1017 mutex_unlock(&raw3270_mutex);
1022 * Remove view from device and free view structure via call to view->fn->free.
1025 raw3270_del_view(struct raw3270_view *view)
1027 unsigned long flags;
1029 struct raw3270_view *nv;
1032 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1033 if (rp->view == view) {
1034 view->fn->deactivate(view);
1037 list_del_init(&view->list);
1038 if (!rp->view && raw3270_state_ready(rp) &&
1039 !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
1040 /* Try to activate another view. */
1041 list_for_each_entry(nv, &rp->view_list, list) {
1042 if (nv->fn->activate(nv) == 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. */
1065 mutex_lock(&raw3270_mutex);
1066 list_del_init(&rp->list);
1067 mutex_unlock(&raw3270_mutex);
1069 /* Disconnect from ccw_device. */
1072 dev_set_drvdata(&cdev->dev, NULL);
1073 cdev->handler = NULL;
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_get_drvdata(dev))->model);
1098 static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
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_get_drvdata(dev))->rows);
1106 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
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_get_drvdata(dev))->cols);
1114 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
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,
1127 static int raw3270_create_attributes(struct raw3270 *rp)
1129 return sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1133 * Notifier for device addition/removal
1135 static LIST_HEAD(raw3270_notifier);
1137 int raw3270_register_notifier(struct raw3270_notifier *notifier)
1141 mutex_lock(&raw3270_mutex);
1142 list_add_tail(¬ifier->list, &raw3270_notifier);
1143 list_for_each_entry(rp, &raw3270_devices, list)
1144 notifier->create(rp->minor);
1145 mutex_unlock(&raw3270_mutex);
1149 void raw3270_unregister_notifier(struct raw3270_notifier *notifier)
1153 mutex_lock(&raw3270_mutex);
1154 list_for_each_entry(rp, &raw3270_devices, list)
1155 notifier->destroy(rp->minor);
1156 list_del(¬ifier->list);
1157 mutex_unlock(&raw3270_mutex);
1161 * Set 3270 device online.
1164 raw3270_set_online (struct ccw_device *cdev)
1166 struct raw3270_notifier *np;
1170 rp = raw3270_create_device(cdev);
1173 rc = raw3270_create_attributes(rp);
1176 raw3270_reset_device(rp);
1177 mutex_lock(&raw3270_mutex);
1178 list_for_each_entry(np, &raw3270_notifier, list)
1179 np->create(rp->minor);
1180 mutex_unlock(&raw3270_mutex);
1184 raw3270_delete_device(rp);
1189 * Remove 3270 device structure.
1192 raw3270_remove (struct ccw_device *cdev)
1194 unsigned long flags;
1196 struct raw3270_view *v;
1197 struct raw3270_notifier *np;
1199 rp = dev_get_drvdata(&cdev->dev);
1201 * _remove is the opposite of _probe; it's probe that
1202 * should set up rp. raw3270_remove gets entered for
1203 * devices even if they haven't been varied online.
1204 * Thus, rp may validly be NULL here.
1209 sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1211 /* Deactivate current view and remove all views. */
1212 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1214 if (rp->view->fn->deactivate)
1215 rp->view->fn->deactivate(rp->view);
1218 while (!list_empty(&rp->view_list)) {
1219 v = list_entry(rp->view_list.next, struct raw3270_view, list);
1222 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1223 raw3270_del_view(v);
1224 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1226 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1228 mutex_lock(&raw3270_mutex);
1229 list_for_each_entry(np, &raw3270_notifier, list)
1230 np->destroy(rp->minor);
1231 mutex_unlock(&raw3270_mutex);
1233 /* Reset 3270 device. */
1234 raw3270_reset_device(rp);
1235 /* And finally remove it. */
1236 raw3270_delete_device(rp);
1240 * Set 3270 device offline.
1243 raw3270_set_offline (struct ccw_device *cdev)
1247 rp = dev_get_drvdata(&cdev->dev);
1248 if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1250 raw3270_remove(cdev);
1254 static int raw3270_pm_stop(struct ccw_device *cdev)
1257 struct raw3270_view *view;
1258 unsigned long flags;
1260 rp = dev_get_drvdata(&cdev->dev);
1263 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1264 if (rp->view && rp->view->fn->deactivate)
1265 rp->view->fn->deactivate(rp->view);
1266 if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
1268 * Release tty and fullscreen for all non-console
1271 list_for_each_entry(view, &rp->view_list, list) {
1272 if (view->fn->release)
1273 view->fn->release(view);
1276 set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1277 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1281 static int raw3270_pm_start(struct ccw_device *cdev)
1284 unsigned long flags;
1286 rp = dev_get_drvdata(&cdev->dev);
1289 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
1290 clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
1291 if (rp->view && rp->view->fn->activate)
1292 rp->view->fn->activate(rp->view);
1293 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1297 void raw3270_pm_unfreeze(struct raw3270_view *view)
1299 #ifdef CONFIG_TN3270_CONSOLE
1303 if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
1304 ccw_device_force_console(rp->cdev);
1308 static struct ccw_device_id raw3270_id[] = {
1309 { CCW_DEVICE(0x3270, 0) },
1310 { CCW_DEVICE(0x3271, 0) },
1311 { CCW_DEVICE(0x3272, 0) },
1312 { CCW_DEVICE(0x3273, 0) },
1313 { CCW_DEVICE(0x3274, 0) },
1314 { CCW_DEVICE(0x3275, 0) },
1315 { CCW_DEVICE(0x3276, 0) },
1316 { CCW_DEVICE(0x3277, 0) },
1317 { CCW_DEVICE(0x3278, 0) },
1318 { CCW_DEVICE(0x3279, 0) },
1319 { CCW_DEVICE(0x3174, 0) },
1320 { /* end of list */ },
1323 static struct ccw_driver raw3270_ccw_driver = {
1326 .owner = THIS_MODULE,
1329 .probe = &raw3270_probe,
1330 .remove = &raw3270_remove,
1331 .set_online = &raw3270_set_online,
1332 .set_offline = &raw3270_set_offline,
1333 .freeze = &raw3270_pm_stop,
1334 .thaw = &raw3270_pm_start,
1335 .restore = &raw3270_pm_start,
1336 .int_class = IRQIO_C70,
1345 if (raw3270_registered)
1347 raw3270_registered = 1;
1348 rc = ccw_driver_register(&raw3270_ccw_driver);
1350 /* Create attributes for early (= console) device. */
1351 mutex_lock(&raw3270_mutex);
1352 class3270 = class_create(THIS_MODULE, "3270");
1353 list_for_each_entry(rp, &raw3270_devices, list) {
1354 get_device(&rp->cdev->dev);
1355 raw3270_create_attributes(rp);
1357 mutex_unlock(&raw3270_mutex);
1365 ccw_driver_unregister(&raw3270_ccw_driver);
1366 class_destroy(class3270);
1369 MODULE_LICENSE("GPL");
1371 module_init(raw3270_init);
1372 module_exit(raw3270_exit);
1374 EXPORT_SYMBOL(class3270);
1375 EXPORT_SYMBOL(raw3270_request_alloc);
1376 EXPORT_SYMBOL(raw3270_request_free);
1377 EXPORT_SYMBOL(raw3270_request_reset);
1378 EXPORT_SYMBOL(raw3270_request_set_cmd);
1379 EXPORT_SYMBOL(raw3270_request_add_data);
1380 EXPORT_SYMBOL(raw3270_request_set_data);
1381 EXPORT_SYMBOL(raw3270_request_set_idal);
1382 EXPORT_SYMBOL(raw3270_buffer_address);
1383 EXPORT_SYMBOL(raw3270_add_view);
1384 EXPORT_SYMBOL(raw3270_del_view);
1385 EXPORT_SYMBOL(raw3270_find_view);
1386 EXPORT_SYMBOL(raw3270_activate_view);
1387 EXPORT_SYMBOL(raw3270_deactivate_view);
1388 EXPORT_SYMBOL(raw3270_start);
1389 EXPORT_SYMBOL(raw3270_start_locked);
1390 EXPORT_SYMBOL(raw3270_start_irq);
1391 EXPORT_SYMBOL(raw3270_reset);
1392 EXPORT_SYMBOL(raw3270_register_notifier);
1393 EXPORT_SYMBOL(raw3270_unregister_notifier);
1394 EXPORT_SYMBOL(raw3270_wait_queue);