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         /* alloc output buffer. */
 
 159                 rq->buffer = alloc_bootmem_low(size);
 
 161         INIT_LIST_HEAD(&rq->list);
 
 166         rq->ccw.cda = __pa(rq->buffer);
 
 167         rq->ccw.flags = CCW_FLAG_SLI;
 
 174  * Free 3270 ccw request
 
 177 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_locked(struct raw3270_view *view, struct raw3270_request *rq)
 
 326         if (!rp || rp->view != view)
 
 328         else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
 
 331                 rc =  __raw3270_start(rp, view, rq);
 
 336 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
 
 342         raw3270_get_view(view);
 
 343         list_add_tail(&rq->list, &rp->req_queue);
 
 348  * 3270 interrupt routine, called from the ccw_device layer
 
 351 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
 
 354         struct raw3270_view *view;
 
 355         struct raw3270_request *rq;
 
 358         rp = (struct raw3270 *) cdev->dev.driver_data;
 
 361         rq = (struct raw3270_request *) intparm;
 
 362         view = rq ? rq->view : rp->view;
 
 365                 rc = RAW3270_IO_RETRY;
 
 366         else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
 
 368                 rc = RAW3270_IO_DONE;
 
 369         } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
 
 370                                            DEV_STAT_UNIT_EXCEP)) {
 
 371                 /* Handle CE-DE-UE and subsequent UDE */
 
 372                 set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
 
 373                 rc = RAW3270_IO_BUSY;
 
 374         } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
 
 375                 /* Wait for UDE if busy flag is set. */
 
 376                 if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
 
 377                         clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
 
 378                         /* Got it, now retry. */
 
 379                         rc = RAW3270_IO_RETRY;
 
 381                         rc = RAW3270_IO_BUSY;
 
 383                 rc = view->fn->intv(view, rq, irb);
 
 385                 rc = RAW3270_IO_DONE;
 
 388         case RAW3270_IO_DONE:
 
 390         case RAW3270_IO_BUSY:
 
 392                  * Intervention required by the operator. We have to wait
 
 393                  * for unsolicited device end.
 
 396         case RAW3270_IO_RETRY:
 
 399                 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
 
 400                                           (unsigned long) rq, 0, 0);
 
 402                         return; /* Sucessfully restarted. */
 
 404         case RAW3270_IO_STOP:
 
 407                 raw3270_halt_io_nolock(rp, rq);
 
 414                 BUG_ON(list_empty(&rq->list));
 
 415                 /* The request completed, remove from queue and do callback. */
 
 416                 list_del_init(&rq->list);
 
 418                         rq->callback(rq, rq->callback_data);
 
 419                 /* Do put_device for get_device in raw3270_start. */
 
 420                 raw3270_put_view(view);
 
 423          * Try to start each request on request queue until one is
 
 424          * started successful.
 
 426         while (!list_empty(&rp->req_queue)) {
 
 427                 rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
 
 428                 rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
 
 429                                           (unsigned long) rq, 0, 0);
 
 432                 /* Start failed. Remove request and do callback. */
 
 433                 list_del_init(&rq->list);
 
 435                         rq->callback(rq, rq->callback_data);
 
 436                 /* Do put_device for get_device in raw3270_start. */
 
 437                 raw3270_put_view(view);
 
 445 struct raw3270_ua {     /* Query Reply structure for Usable Area */
 
 446         struct {        /* Usable Area Query Reply Base */
 
 447                 short l;        /* Length of this structured field */
 
 448                 char  sfid;     /* 0x81 if Query Reply */
 
 449                 char  qcode;    /* 0x81 if Usable Area */
 
 452                 short w;        /* Width of usable area */
 
 453                 short h;        /* Heigth of usavle area */
 
 454                 char  units;    /* 0x00:in; 0x01:mm */
 
 459                 short buffsz;   /* Character buffer size, bytes */
 
 464         } __attribute__ ((packed)) uab;
 
 465         struct {        /* Alternate Usable Area Self-Defining Parameter */
 
 466                 char  l;        /* Length of this Self-Defining Parm */
 
 467                 char  sdpid;    /* 0x02 if Alternate Usable Area */
 
 469                 char  auaid;    /* 0x01 is Id for the A U A */
 
 470                 short wauai;    /* Width of AUAi */
 
 471                 short hauai;    /* Height of AUAi */
 
 472                 char  auaunits; /* 0x00:in, 0x01:mm */
 
 477         } __attribute__ ((packed)) aua;
 
 478 } __attribute__ ((packed));
 
 480 static struct diag210 raw3270_init_diag210;
 
 481 static DEFINE_MUTEX(raw3270_init_mutex);
 
 484 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
 
 488          * Unit-Check Processing:
 
 489          * Expect Command Reject or Intervention Required.
 
 491         if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
 
 492                 /* Request finished abnormally. */
 
 493                 if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
 
 494                         set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
 
 495                         return RAW3270_IO_BUSY;
 
 499                 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
 
 500                         if (irb->ecw[0] & SNS0_CMD_REJECT)
 
 501                                 rq->rc = -EOPNOTSUPP;
 
 505                         /* Request finished normally. Copy residual count. */
 
 506                         rq->rescnt = irb->scsw.cmd.count;
 
 508         if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
 
 509                 set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
 
 510                 wake_up(&raw3270_wait_queue);
 
 512         return RAW3270_IO_DONE;
 
 515 static struct raw3270_fn raw3270_init_fn = {
 
 516         .intv = raw3270_init_irq
 
 519 static struct raw3270_view raw3270_init_view = {
 
 520         .fn = &raw3270_init_fn
 
 524  * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
 
 525  * Wait for end of request. The request must have been started
 
 526  * with raw3270_start, rc = 0. The device lock may NOT have been
 
 527  * released between calling raw3270_start and raw3270_wait.
 
 530 raw3270_wake_init(struct raw3270_request *rq, void *data)
 
 532         wake_up((wait_queue_head_t *) data);
 
 536  * Special wait function that can cope with console initialization.
 
 539 raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
 
 540                    struct raw3270_request *rq)
 
 545 #ifdef CONFIG_TN3270_CONSOLE
 
 546         if (raw3270_registered == 0) {
 
 547                 spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
 
 549                 rc = __raw3270_start(rp, view, rq);
 
 551                         while (!raw3270_request_final(rq)) {
 
 555                 spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
 
 559         rq->callback = raw3270_wake_init;
 
 560         rq->callback_data = &raw3270_wait_queue;
 
 561         spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
 
 562         rc = __raw3270_start(rp, view, rq);
 
 563         spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
 
 566         /* Now wait for the completion. */
 
 567         rc = wait_event_interruptible(raw3270_wait_queue,
 
 568                                       raw3270_request_final(rq));
 
 569         if (rc == -ERESTARTSYS) {       /* Interrupted by a signal. */
 
 570                 raw3270_halt_io(view->dev, rq);
 
 571                 /* No wait for the halt to complete. */
 
 572                 wait_event(raw3270_wait_queue, raw3270_request_final(rq));
 
 579 __raw3270_size_device_vm(struct raw3270 *rp)
 
 582         struct ccw_dev_id dev_id;
 
 584         ccw_device_get_id(rp->cdev, &dev_id);
 
 585         raw3270_init_diag210.vrdcdvno = dev_id.devno;
 
 586         raw3270_init_diag210.vrdclen = sizeof(struct diag210);
 
 587         rc = diag210(&raw3270_init_diag210);
 
 590         model = raw3270_init_diag210.vrdccrmd;
 
 620 __raw3270_size_device(struct raw3270 *rp)
 
 622         static const unsigned char wbuf[] =
 
 623                 { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
 
 624         struct raw3270_ua *uap;
 
 625         unsigned short count;
 
 629          * To determine the size of the 3270 device we need to do:
 
 630          * 1) send a 'read partition' data stream to the device
 
 631          * 2) wait for the attn interrupt that preceeds the query reply
 
 632          * 3) do a read modified to get the query reply
 
 633          * To make things worse we have to cope with intervention
 
 634          * required (3270 device switched to 'stand-by') and command
 
 635          * rejects (old devices that can't do 'read partition').
 
 637         memset(&rp->init_request, 0, sizeof(rp->init_request));
 
 638         memset(&rp->init_data, 0, 256);
 
 639         /* Store 'read partition' data stream to init_data */
 
 640         memcpy(&rp->init_data, wbuf, sizeof(wbuf));
 
 641         INIT_LIST_HEAD(&rp->init_request.list);
 
 642         rp->init_request.ccw.cmd_code = TC_WRITESF;
 
 643         rp->init_request.ccw.flags = CCW_FLAG_SLI;
 
 644         rp->init_request.ccw.count = sizeof(wbuf);
 
 645         rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
 
 647         rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
 
 649                 /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
 
 652         /* Wait for attention interrupt. */
 
 653 #ifdef CONFIG_TN3270_CONSOLE
 
 654         if (raw3270_registered == 0) {
 
 657                 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
 
 658                 while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
 
 660                 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
 
 663                 rc = wait_event_interruptible(raw3270_wait_queue,
 
 664                         test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
 
 669          * The device accepted the 'read partition' command. Now
 
 670          * set up a read ccw and issue it.
 
 672         rp->init_request.ccw.cmd_code = TC_READMOD;
 
 673         rp->init_request.ccw.flags = CCW_FLAG_SLI;
 
 674         rp->init_request.ccw.count = sizeof(rp->init_data);
 
 675         rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
 
 676         rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
 
 679         /* Got a Query Reply */
 
 680         count = sizeof(rp->init_data) - rp->init_request.rescnt;
 
 681         uap = (struct raw3270_ua *) (rp->init_data + 1);
 
 682         /* Paranoia check. */
 
 683         if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
 
 685         /* Copy rows/columns of default Usable Area */
 
 686         rp->rows = uap->uab.h;
 
 687         rp->cols = uap->uab.w;
 
 688         /* Check for 14 bit addressing */
 
 689         if ((uap->uab.flags0 & 0x0d) == 0x01)
 
 690                 set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
 
 691         /* Check for Alternate Usable Area */
 
 692         if (uap->uab.l == sizeof(struct raw3270_ua) &&
 
 693             uap->aua.sdpid == 0x02) {
 
 694                 rp->rows = uap->aua.hauai;
 
 695                 rp->cols = uap->aua.wauai;
 
 701 raw3270_size_device(struct raw3270 *rp)
 
 705         mutex_lock(&raw3270_init_mutex);
 
 706         rp->view = &raw3270_init_view;
 
 707         raw3270_init_view.dev = rp;
 
 709                 rc = __raw3270_size_device_vm(rp);
 
 711                 rc = __raw3270_size_device(rp);
 
 712         raw3270_init_view.dev = NULL;
 
 714         mutex_unlock(&raw3270_init_mutex);
 
 715         if (rc == 0) {  /* Found something. */
 
 716                 /* Try to find a model. */
 
 718                 if (rp->rows == 24 && rp->cols == 80)
 
 720                 if (rp->rows == 32 && rp->cols == 80)
 
 722                 if (rp->rows == 43 && rp->cols == 80)
 
 724                 if (rp->rows == 27 && rp->cols == 132)
 
 727                 /* Couldn't detect size. Use default model 2. */
 
 737 raw3270_reset_device(struct raw3270 *rp)
 
 741         mutex_lock(&raw3270_init_mutex);
 
 742         memset(&rp->init_request, 0, sizeof(rp->init_request));
 
 743         memset(&rp->init_data, 0, sizeof(rp->init_data));
 
 744         /* Store reset data stream to init_data/init_request */
 
 745         rp->init_data[0] = TW_KR;
 
 746         INIT_LIST_HEAD(&rp->init_request.list);
 
 747         rp->init_request.ccw.cmd_code = TC_EWRITEA;
 
 748         rp->init_request.ccw.flags = CCW_FLAG_SLI;
 
 749         rp->init_request.ccw.count = 1;
 
 750         rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
 
 751         rp->view = &raw3270_init_view;
 
 752         raw3270_init_view.dev = rp;
 
 753         rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
 
 754         raw3270_init_view.dev = NULL;
 
 756         mutex_unlock(&raw3270_init_mutex);
 
 761 raw3270_reset(struct raw3270_view *view)
 
 767         if (!rp || rp->view != view)
 
 769         else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
 
 772                 rc = raw3270_reset_device(view->dev);
 
 777  * Setup new 3270 device.
 
 780 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
 
 786         memset(rp, 0, sizeof(struct raw3270));
 
 787         /* Copy ebcdic -> ascii translation table. */
 
 788         memcpy(ascebc, _ascebc, 256);
 
 790                 /* correct brackets and circumflex */
 
 801         INIT_LIST_HEAD(&rp->req_queue);
 
 802         INIT_LIST_HEAD(&rp->view_list);
 
 805          * Add device to list and find the smallest unused minor
 
 806          * number for it. Note: there is no device with minor 0,
 
 807          * see special case for fs3270.c:fs3270_open().
 
 809         mutex_lock(&raw3270_mutex);
 
 810         /* Keep the list sorted. */
 
 811         minor = RAW3270_FIRSTMINOR;
 
 813         list_for_each(l, &raw3270_devices) {
 
 814                 tmp = list_entry(l, struct raw3270, list);
 
 815                 if (tmp->minor > minor) {
 
 817                         __list_add(&rp->list, l->prev, l);
 
 822         if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
 
 824                 list_add_tail(&rp->list, &raw3270_devices);
 
 826         mutex_unlock(&raw3270_mutex);
 
 827         /* No free minor number? Then give up. */
 
 831         cdev->dev.driver_data = rp;
 
 832         cdev->handler = raw3270_irq;
 
 836 #ifdef CONFIG_TN3270_CONSOLE
 
 838  * Setup 3270 device configured as console.
 
 840 struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
 
 846         rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
 
 847         ascebc = (char *) alloc_bootmem(256);
 
 848         rc = raw3270_setup_device(cdev, rp, ascebc);
 
 851         set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
 
 852         rc = raw3270_reset_device(rp);
 
 855         rc = raw3270_size_device(rp);
 
 858         rc = raw3270_reset_device(rp);
 
 861         set_bit(RAW3270_FLAGS_READY, &rp->flags);
 
 866 raw3270_wait_cons_dev(struct raw3270 *rp)
 
 870         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
 
 872         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
 
 878  * Create a 3270 device structure.
 
 880 static struct raw3270 *
 
 881 raw3270_create_device(struct ccw_device *cdev)
 
 887         rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
 
 889                 return ERR_PTR(-ENOMEM);
 
 890         ascebc = kmalloc(256, GFP_KERNEL);
 
 893                 return ERR_PTR(-ENOMEM);
 
 895         rc = raw3270_setup_device(cdev, rp, ascebc);
 
 901         /* Get reference to ccw_device structure. */
 
 902         get_device(&cdev->dev);
 
 910 raw3270_activate_view(struct raw3270_view *view)
 
 913         struct raw3270_view *oldview, *nv;
 
 920         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
 
 921         if (rp->view == view)
 
 923         else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
 
 929                         oldview->fn->deactivate(oldview);
 
 932                 rc = view->fn->activate(view);
 
 934                         /* Didn't work. Try to reactivate the old view. */
 
 936                         if (!oldview || oldview->fn->activate(oldview) != 0) {
 
 937                                 /* Didn't work as well. Try any other view. */
 
 938                                 list_for_each_entry(nv, &rp->view_list, list)
 
 939                                         if (nv != view && nv != oldview) {
 
 941                                                 if (nv->fn->activate(nv) == 0)
 
 948         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
 
 953  * Deactivate current view.
 
 956 raw3270_deactivate_view(struct raw3270_view *view)
 
 964         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
 
 965         if (rp->view == view) {
 
 966                 view->fn->deactivate(view);
 
 968                 /* Move deactivated view to end of list. */
 
 969                 list_del_init(&view->list);
 
 970                 list_add_tail(&view->list, &rp->view_list);
 
 971                 /* Try to activate another view. */
 
 972                 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
 
 973                         list_for_each_entry(view, &rp->view_list, list) {
 
 975                                 if (view->fn->activate(view) == 0)
 
 981         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
 
 985  * Add view to device with minor "minor".
 
 988 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
 
 996         mutex_lock(&raw3270_mutex);
 
 998         list_for_each_entry(rp, &raw3270_devices, list) {
 
 999                 if (rp->minor != minor)
 
1001                 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
 
1002                 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
 
1003                         atomic_set(&view->ref_count, 2);
 
1006                         view->model = rp->model;
 
1007                         view->rows = rp->rows;
 
1008                         view->cols = rp->cols;
 
1009                         view->ascebc = rp->ascebc;
 
1010                         spin_lock_init(&view->lock);
 
1011                         list_add(&view->list, &rp->view_list);
 
1014                 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
 
1017         mutex_unlock(&raw3270_mutex);
 
1022  * Find specific view of device with minor "minor".
 
1024 struct raw3270_view *
 
1025 raw3270_find_view(struct raw3270_fn *fn, int minor)
 
1028         struct raw3270_view *view, *tmp;
 
1029         unsigned long flags;
 
1031         mutex_lock(&raw3270_mutex);
 
1032         view = ERR_PTR(-ENODEV);
 
1033         list_for_each_entry(rp, &raw3270_devices, list) {
 
1034                 if (rp->minor != minor)
 
1036                 spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
 
1037                 if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
 
1038                         view = ERR_PTR(-ENOENT);
 
1039                         list_for_each_entry(tmp, &rp->view_list, list) {
 
1040                                 if (tmp->fn == fn) {
 
1041                                         raw3270_get_view(tmp);
 
1047                 spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
 
1050         mutex_unlock(&raw3270_mutex);
 
1055  * Remove view from device and free view structure via call to view->fn->free.
 
1058 raw3270_del_view(struct raw3270_view *view)
 
1060         unsigned long flags;
 
1062         struct raw3270_view *nv;
 
1065         spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
 
1066         if (rp->view == view) {
 
1067                 view->fn->deactivate(view);
 
1070         list_del_init(&view->list);
 
1071         if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
 
1072                 /* Try to activate another view. */
 
1073                 list_for_each_entry(nv, &rp->view_list, list) {
 
1074                         if (nv->fn->activate(nv) == 0) {
 
1080         spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
 
1081         /* Wait for reference counter to drop to zero. */
 
1082         atomic_dec(&view->ref_count);
 
1083         wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
 
1085                 view->fn->free(view);
 
1089  * Remove a 3270 device structure.
 
1092 raw3270_delete_device(struct raw3270 *rp)
 
1094         struct ccw_device *cdev;
 
1096         /* Remove from device chain. */
 
1097         mutex_lock(&raw3270_mutex);
 
1098         if (rp->clttydev && !IS_ERR(rp->clttydev))
 
1099                 device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
 
1100         if (rp->cltubdev && !IS_ERR(rp->cltubdev))
 
1101                 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
 
1102         list_del_init(&rp->list);
 
1103         mutex_unlock(&raw3270_mutex);
 
1105         /* Disconnect from ccw_device. */
 
1108         cdev->dev.driver_data = NULL;
 
1109         cdev->handler = NULL;
 
1111         /* Put ccw_device structure. */
 
1112         put_device(&cdev->dev);
 
1114         /* Now free raw3270 structure. */
 
1120 raw3270_probe (struct ccw_device *cdev)
 
1126  * Additional attributes for a 3270 device
 
1129 raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
 
1131         return snprintf(buf, PAGE_SIZE, "%i\n",
 
1132                         ((struct raw3270 *) dev->driver_data)->model);
 
1134 static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
 
1137 raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
 
1139         return snprintf(buf, PAGE_SIZE, "%i\n",
 
1140                         ((struct raw3270 *) dev->driver_data)->rows);
 
1142 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
 
1145 raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
 
1147         return snprintf(buf, PAGE_SIZE, "%i\n",
 
1148                         ((struct raw3270 *) dev->driver_data)->cols);
 
1150 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
 
1152 static struct attribute * raw3270_attrs[] = {
 
1153         &dev_attr_model.attr,
 
1154         &dev_attr_rows.attr,
 
1155         &dev_attr_columns.attr,
 
1159 static struct attribute_group raw3270_attr_group = {
 
1160         .attrs = raw3270_attrs,
 
1163 static int raw3270_create_attributes(struct raw3270 *rp)
 
1167         rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
 
1171         rp->clttydev = device_create(class3270, &rp->cdev->dev,
 
1172                                      MKDEV(IBM_TTY3270_MAJOR, rp->minor), NULL,
 
1173                                      "tty%s", dev_name(&rp->cdev->dev));
 
1174         if (IS_ERR(rp->clttydev)) {
 
1175                 rc = PTR_ERR(rp->clttydev);
 
1179         rp->cltubdev = device_create(class3270, &rp->cdev->dev,
 
1180                                      MKDEV(IBM_FS3270_MAJOR, rp->minor), NULL,
 
1181                                      "tub%s", dev_name(&rp->cdev->dev));
 
1182         if (!IS_ERR(rp->cltubdev))
 
1185         rc = PTR_ERR(rp->cltubdev);
 
1186         device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
 
1189         sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
 
1195  * Notifier for device addition/removal
 
1197 struct raw3270_notifier {
 
1198         struct list_head list;
 
1199         void (*notifier)(int, int);
 
1202 static LIST_HEAD(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;
 
1213         mutex_lock(&raw3270_mutex);
 
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);
 
1219         mutex_unlock(&raw3270_mutex);
 
1223 void raw3270_unregister_notifier(void (*notifier)(int, int))
 
1225         struct raw3270_notifier *np;
 
1227         mutex_lock(&raw3270_mutex);
 
1228         list_for_each_entry(np, &raw3270_notifier, list)
 
1229                 if (np->notifier == notifier) {
 
1230                         list_del(&np->list);
 
1234         mutex_unlock(&raw3270_mutex);
 
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         rc = raw3270_create_attributes(rp);
 
1262         set_bit(RAW3270_FLAGS_READY, &rp->flags);
 
1263         mutex_lock(&raw3270_mutex);
 
1264         list_for_each_entry(np, &raw3270_notifier, list)
 
1265                 np->notifier(rp->minor, 1);
 
1266         mutex_unlock(&raw3270_mutex);
 
1270         raw3270_delete_device(rp);
 
1275  * Remove 3270 device structure.
 
1278 raw3270_remove (struct ccw_device *cdev)
 
1280         unsigned long flags;
 
1282         struct raw3270_view *v;
 
1283         struct raw3270_notifier *np;
 
1285         rp = cdev->dev.driver_data;
 
1287          * _remove is the opposite of _probe; it's probe that
 
1288          * should set up rp.  raw3270_remove gets entered for
 
1289          * devices even if they haven't been varied online.
 
1290          * Thus, rp may validly be NULL here.
 
1294         clear_bit(RAW3270_FLAGS_READY, &rp->flags);
 
1296         sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
 
1298         /* Deactivate current view and remove all views. */
 
1299         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
 
1301                 rp->view->fn->deactivate(rp->view);
 
1304         while (!list_empty(&rp->view_list)) {
 
1305                 v = list_entry(rp->view_list.next, struct raw3270_view, list);
 
1308                 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
 
1309                 raw3270_del_view(v);
 
1310                 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
 
1312         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
 
1314         mutex_lock(&raw3270_mutex);
 
1315         list_for_each_entry(np, &raw3270_notifier, list)
 
1316                 np->notifier(rp->minor, 0);
 
1317         mutex_unlock(&raw3270_mutex);
 
1319         /* Reset 3270 device. */
 
1320         raw3270_reset_device(rp);
 
1321         /* And finally remove it. */
 
1322         raw3270_delete_device(rp);
 
1326  * Set 3270 device offline.
 
1329 raw3270_set_offline (struct ccw_device *cdev)
 
1333         rp = cdev->dev.driver_data;
 
1334         if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
 
1336         raw3270_remove(cdev);
 
1340 static struct ccw_device_id raw3270_id[] = {
 
1341         { CCW_DEVICE(0x3270, 0) },
 
1342         { CCW_DEVICE(0x3271, 0) },
 
1343         { CCW_DEVICE(0x3272, 0) },
 
1344         { CCW_DEVICE(0x3273, 0) },
 
1345         { CCW_DEVICE(0x3274, 0) },
 
1346         { CCW_DEVICE(0x3275, 0) },
 
1347         { CCW_DEVICE(0x3276, 0) },
 
1348         { CCW_DEVICE(0x3277, 0) },
 
1349         { CCW_DEVICE(0x3278, 0) },
 
1350         { CCW_DEVICE(0x3279, 0) },
 
1351         { CCW_DEVICE(0x3174, 0) },
 
1352         { /* end of list */ },
 
1355 static struct ccw_driver raw3270_ccw_driver = {
 
1357         .owner          = THIS_MODULE,
 
1359         .probe          = &raw3270_probe,
 
1360         .remove         = &raw3270_remove,
 
1361         .set_online     = &raw3270_set_online,
 
1362         .set_offline    = &raw3270_set_offline,
 
1371         if (raw3270_registered)
 
1373         raw3270_registered = 1;
 
1374         rc = ccw_driver_register(&raw3270_ccw_driver);
 
1376                 /* Create attributes for early (= console) device. */
 
1377                 mutex_lock(&raw3270_mutex);
 
1378                 class3270 = class_create(THIS_MODULE, "3270");
 
1379                 list_for_each_entry(rp, &raw3270_devices, list) {
 
1380                         get_device(&rp->cdev->dev);
 
1381                         raw3270_create_attributes(rp);
 
1383                 mutex_unlock(&raw3270_mutex);
 
1391         ccw_driver_unregister(&raw3270_ccw_driver);
 
1392         class_destroy(class3270);
 
1395 MODULE_LICENSE("GPL");
 
1397 module_init(raw3270_init);
 
1398 module_exit(raw3270_exit);
 
1400 EXPORT_SYMBOL(raw3270_request_alloc);
 
1401 EXPORT_SYMBOL(raw3270_request_free);
 
1402 EXPORT_SYMBOL(raw3270_request_reset);
 
1403 EXPORT_SYMBOL(raw3270_request_set_cmd);
 
1404 EXPORT_SYMBOL(raw3270_request_add_data);
 
1405 EXPORT_SYMBOL(raw3270_request_set_data);
 
1406 EXPORT_SYMBOL(raw3270_request_set_idal);
 
1407 EXPORT_SYMBOL(raw3270_buffer_address);
 
1408 EXPORT_SYMBOL(raw3270_add_view);
 
1409 EXPORT_SYMBOL(raw3270_del_view);
 
1410 EXPORT_SYMBOL(raw3270_find_view);
 
1411 EXPORT_SYMBOL(raw3270_activate_view);
 
1412 EXPORT_SYMBOL(raw3270_deactivate_view);
 
1413 EXPORT_SYMBOL(raw3270_start);
 
1414 EXPORT_SYMBOL(raw3270_start_locked);
 
1415 EXPORT_SYMBOL(raw3270_start_irq);
 
1416 EXPORT_SYMBOL(raw3270_reset);
 
1417 EXPORT_SYMBOL(raw3270_register_notifier);
 
1418 EXPORT_SYMBOL(raw3270_unregister_notifier);
 
1419 EXPORT_SYMBOL(raw3270_wait_queue);