Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * 3215 line mode terminal driver.
0004  *
0005  * Copyright IBM Corp. 1999, 2009
0006  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
0007  *
0008  * Updated:
0009  *  Aug-2000: Added tab support
0010  *        Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
0011  */
0012 
0013 #include <linux/types.h>
0014 #include <linux/kdev_t.h>
0015 #include <linux/tty.h>
0016 #include <linux/tty_flip.h>
0017 #include <linux/vt_kern.h>
0018 #include <linux/init.h>
0019 #include <linux/console.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/err.h>
0022 #include <linux/panic_notifier.h>
0023 #include <linux/reboot.h>
0024 #include <linux/serial.h> /* ASYNC_* flags */
0025 #include <linux/slab.h>
0026 #include <asm/ccwdev.h>
0027 #include <asm/cio.h>
0028 #include <asm/io.h>
0029 #include <asm/ebcdic.h>
0030 #include <linux/uaccess.h>
0031 #include <asm/delay.h>
0032 #include <asm/cpcmd.h>
0033 #include <asm/setup.h>
0034 
0035 #include "ctrlchar.h"
0036 
0037 #define NR_3215         1
0038 #define NR_3215_REQ     (4*NR_3215)
0039 #define RAW3215_BUFFER_SIZE 65536     /* output buffer size */
0040 #define RAW3215_INBUF_SIZE  256       /* input buffer size */
0041 #define RAW3215_MIN_SPACE   128       /* minimum free space for wakeup */
0042 #define RAW3215_MIN_WRITE   1024      /* min. length for immediate output */
0043 #define RAW3215_MAX_BYTES   3968      /* max. bytes to write with one ssch */
0044 #define RAW3215_MAX_NEWLINE 50        /* max. lines to write with one ssch */
0045 #define RAW3215_NR_CCWS     3
0046 #define RAW3215_TIMEOUT     HZ/10     /* time for delayed output */
0047 
0048 #define RAW3215_FIXED       1         /* 3215 console device is not be freed */
0049 #define RAW3215_WORKING     4         /* set if a request is being worked on */
0050 #define RAW3215_THROTTLED   8         /* set if reading is disabled */
0051 #define RAW3215_STOPPED     16        /* set if writing is disabled */
0052 #define RAW3215_TIMER_RUNS  64        /* set if the output delay timer is on */
0053 #define RAW3215_FLUSHING    128       /* set to flush buffer (no delay) */
0054 
0055 #define TAB_STOP_SIZE       8         /* tab stop size */
0056 
0057 /*
0058  * Request types for a 3215 device
0059  */
0060 enum raw3215_type {
0061     RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
0062 };
0063 
0064 /*
0065  * Request structure for a 3215 device
0066  */
0067 struct raw3215_req {
0068     enum raw3215_type type;       /* type of the request */
0069     int start, len;           /* start index & len in output buffer */
0070     int delayable;            /* indication to wait for more data */
0071     int residual;             /* residual count for read request */
0072     struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
0073     struct raw3215_info *info;    /* pointer to main structure */
0074     struct raw3215_req *next;     /* pointer to next request */
0075 } __attribute__ ((aligned(8)));
0076 
0077 struct raw3215_info {
0078     struct tty_port port;
0079     struct ccw_device *cdev;      /* device for tty driver */
0080     spinlock_t *lock;         /* pointer to irq lock */
0081     int flags;            /* state flags */
0082     char *buffer;             /* pointer to output buffer */
0083     char *inbuf;              /* pointer to input buffer */
0084     int head;             /* first free byte in output buffer */
0085     int count;            /* number of bytes in output buffer */
0086     int written;              /* number of bytes in write requests */
0087     struct raw3215_req *queued_read; /* pointer to queued read requests */
0088     struct raw3215_req *queued_write;/* pointer to queued write requests */
0089     wait_queue_head_t empty_wait; /* wait queue for flushing */
0090     struct timer_list timer;      /* timer for delayed output */
0091     int line_pos;             /* position on the line (for tabs) */
0092     char ubuffer[80];         /* copy_from_user buffer */
0093 };
0094 
0095 /* array of 3215 devices structures */
0096 static struct raw3215_info *raw3215[NR_3215];
0097 /* spinlock to protect the raw3215 array */
0098 static DEFINE_SPINLOCK(raw3215_device_lock);
0099 /* list of free request structures */
0100 static struct raw3215_req *raw3215_freelist;
0101 /* spinlock to protect free list */
0102 static DEFINE_SPINLOCK(raw3215_freelist_lock);
0103 
0104 static struct tty_driver *tty3215_driver;
0105 
0106 /*
0107  * Get a request structure from the free list
0108  */
0109 static inline struct raw3215_req *raw3215_alloc_req(void)
0110 {
0111     struct raw3215_req *req;
0112     unsigned long flags;
0113 
0114     spin_lock_irqsave(&raw3215_freelist_lock, flags);
0115     req = raw3215_freelist;
0116     raw3215_freelist = req->next;
0117     spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
0118     return req;
0119 }
0120 
0121 /*
0122  * Put a request structure back to the free list
0123  */
0124 static inline void raw3215_free_req(struct raw3215_req *req)
0125 {
0126     unsigned long flags;
0127 
0128     if (req->type == RAW3215_FREE)
0129         return;     /* don't free a free request */
0130     req->type = RAW3215_FREE;
0131     spin_lock_irqsave(&raw3215_freelist_lock, flags);
0132     req->next = raw3215_freelist;
0133     raw3215_freelist = req;
0134     spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
0135 }
0136 
0137 /*
0138  * Set up a read request that reads up to 160 byte from the 3215 device.
0139  * If there is a queued read request it is used, but that shouldn't happen
0140  * because a 3215 terminal won't accept a new read before the old one is
0141  * completed.
0142  */
0143 static void raw3215_mk_read_req(struct raw3215_info *raw)
0144 {
0145     struct raw3215_req *req;
0146     struct ccw1 *ccw;
0147 
0148     /* there can only be ONE read request at a time */
0149     req = raw->queued_read;
0150     if (req == NULL) {
0151         /* no queued read request, use new req structure */
0152         req = raw3215_alloc_req();
0153         req->type = RAW3215_READ;
0154         req->info = raw;
0155         raw->queued_read = req;
0156     }
0157 
0158     ccw = req->ccws;
0159     ccw->cmd_code = 0x0A; /* read inquiry */
0160     ccw->flags = 0x20;    /* ignore incorrect length */
0161     ccw->count = 160;
0162     ccw->cda = (__u32) __pa(raw->inbuf);
0163 }
0164 
0165 /*
0166  * Set up a write request with the information from the main structure.
0167  * A ccw chain is created that writes as much as possible from the output
0168  * buffer to the 3215 device. If a queued write exists it is replaced by
0169  * the new, probably lengthened request.
0170  */
0171 static void raw3215_mk_write_req(struct raw3215_info *raw)
0172 {
0173     struct raw3215_req *req;
0174     struct ccw1 *ccw;
0175     int len, count, ix, lines;
0176 
0177     if (raw->count <= raw->written)
0178         return;
0179     /* check if there is a queued write request */
0180     req = raw->queued_write;
0181     if (req == NULL) {
0182         /* no queued write request, use new req structure */
0183         req = raw3215_alloc_req();
0184         req->type = RAW3215_WRITE;
0185         req->info = raw;
0186         raw->queued_write = req;
0187     } else {
0188         raw->written -= req->len;
0189     }
0190 
0191     ccw = req->ccws;
0192     req->start = (raw->head - raw->count + raw->written) &
0193              (RAW3215_BUFFER_SIZE - 1);
0194     /*
0195      * now we have to count newlines. We can at max accept
0196      * RAW3215_MAX_NEWLINE newlines in a single ssch due to
0197      * a restriction in VM
0198      */
0199     lines = 0;
0200     ix = req->start;
0201     while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
0202         if (raw->buffer[ix] == 0x15)
0203             lines++;
0204         ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
0205     }
0206     len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
0207     if (len > RAW3215_MAX_BYTES)
0208         len = RAW3215_MAX_BYTES;
0209     req->len = len;
0210     raw->written += len;
0211 
0212     /* set the indication if we should try to enlarge this request */
0213     req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
0214 
0215     ix = req->start;
0216     while (len > 0) {
0217         if (ccw > req->ccws)
0218             ccw[-1].flags |= 0x40; /* use command chaining */
0219         ccw->cmd_code = 0x01; /* write, auto carrier return */
0220         ccw->flags = 0x20;    /* ignore incorrect length ind.  */
0221         ccw->cda =
0222             (__u32) __pa(raw->buffer + ix);
0223         count = len;
0224         if (ix + count > RAW3215_BUFFER_SIZE)
0225             count = RAW3215_BUFFER_SIZE - ix;
0226         ccw->count = count;
0227         len -= count;
0228         ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
0229         ccw++;
0230     }
0231     /*
0232      * Add a NOP to the channel program. 3215 devices are purely
0233      * emulated and its much better to avoid the channel end
0234      * interrupt in this case.
0235      */
0236     if (ccw > req->ccws)
0237         ccw[-1].flags |= 0x40; /* use command chaining */
0238     ccw->cmd_code = 0x03; /* NOP */
0239     ccw->flags = 0;
0240     ccw->cda = 0;
0241     ccw->count = 1;
0242 }
0243 
0244 /*
0245  * Start a read or a write request
0246  */
0247 static void raw3215_start_io(struct raw3215_info *raw)
0248 {
0249     struct raw3215_req *req;
0250     int res;
0251 
0252     req = raw->queued_read;
0253     if (req != NULL &&
0254         !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
0255         /* dequeue request */
0256         raw->queued_read = NULL;
0257         res = ccw_device_start(raw->cdev, req->ccws,
0258                        (unsigned long) req, 0, 0);
0259         if (res != 0) {
0260             /* do_IO failed, put request back to queue */
0261             raw->queued_read = req;
0262         } else {
0263             raw->flags |= RAW3215_WORKING;
0264         }
0265     }
0266     req = raw->queued_write;
0267     if (req != NULL &&
0268         !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
0269         /* dequeue request */
0270         raw->queued_write = NULL;
0271         res = ccw_device_start(raw->cdev, req->ccws,
0272                        (unsigned long) req, 0, 0);
0273         if (res != 0) {
0274             /* do_IO failed, put request back to queue */
0275             raw->queued_write = req;
0276         } else {
0277             raw->flags |= RAW3215_WORKING;
0278         }
0279     }
0280 }
0281 
0282 /*
0283  * Function to start a delayed output after RAW3215_TIMEOUT seconds
0284  */
0285 static void raw3215_timeout(struct timer_list *t)
0286 {
0287     struct raw3215_info *raw = from_timer(raw, t, timer);
0288     unsigned long flags;
0289 
0290     spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
0291     raw->flags &= ~RAW3215_TIMER_RUNS;
0292     raw3215_mk_write_req(raw);
0293     raw3215_start_io(raw);
0294     if ((raw->queued_read || raw->queued_write) &&
0295         !(raw->flags & RAW3215_WORKING) &&
0296         !(raw->flags & RAW3215_TIMER_RUNS)) {
0297         raw->timer.expires = RAW3215_TIMEOUT + jiffies;
0298         add_timer(&raw->timer);
0299         raw->flags |= RAW3215_TIMER_RUNS;
0300     }
0301     spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0302 }
0303 
0304 /*
0305  * Function to conditionally start an IO. A read is started immediately,
0306  * a write is only started immediately if the flush flag is on or the
0307  * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
0308  * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
0309  */
0310 static inline void raw3215_try_io(struct raw3215_info *raw)
0311 {
0312     if (!tty_port_initialized(&raw->port))
0313         return;
0314     if (raw->queued_read != NULL)
0315         raw3215_start_io(raw);
0316     else if (raw->queued_write != NULL) {
0317         if ((raw->queued_write->delayable == 0) ||
0318             (raw->flags & RAW3215_FLUSHING)) {
0319             /* execute write requests bigger than minimum size */
0320             raw3215_start_io(raw);
0321         }
0322     }
0323     if ((raw->queued_read || raw->queued_write) &&
0324         !(raw->flags & RAW3215_WORKING) &&
0325         !(raw->flags & RAW3215_TIMER_RUNS)) {
0326         raw->timer.expires = RAW3215_TIMEOUT + jiffies;
0327         add_timer(&raw->timer);
0328         raw->flags |= RAW3215_TIMER_RUNS;
0329     }
0330 }
0331 
0332 /*
0333  * Try to start the next IO and wake up processes waiting on the tty.
0334  */
0335 static void raw3215_next_io(struct raw3215_info *raw, struct tty_struct *tty)
0336 {
0337     raw3215_mk_write_req(raw);
0338     raw3215_try_io(raw);
0339     if (tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
0340         tty_wakeup(tty);
0341 }
0342 
0343 /*
0344  * Interrupt routine, called from common io layer
0345  */
0346 static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
0347             struct irb *irb)
0348 {
0349     struct raw3215_info *raw;
0350     struct raw3215_req *req;
0351     struct tty_struct *tty;
0352     int cstat, dstat;
0353     int count;
0354 
0355     raw = dev_get_drvdata(&cdev->dev);
0356     req = (struct raw3215_req *) intparm;
0357     tty = tty_port_tty_get(&raw->port);
0358     cstat = irb->scsw.cmd.cstat;
0359     dstat = irb->scsw.cmd.dstat;
0360     if (cstat != 0)
0361         raw3215_next_io(raw, tty);
0362     if (dstat & 0x01) { /* we got a unit exception */
0363         dstat &= ~0x01;  /* we can ignore it */
0364     }
0365     switch (dstat) {
0366     case 0x80:
0367         if (cstat != 0)
0368             break;
0369         /* Attention interrupt, someone hit the enter key */
0370         raw3215_mk_read_req(raw);
0371         raw3215_next_io(raw, tty);
0372         break;
0373     case 0x08:
0374     case 0x0C:
0375         /* Channel end interrupt. */
0376         if ((raw = req->info) == NULL)
0377             goto put_tty;        /* That shouldn't happen ... */
0378         if (req->type == RAW3215_READ) {
0379             /* store residual count, then wait for device end */
0380             req->residual = irb->scsw.cmd.count;
0381         }
0382         if (dstat == 0x08)
0383             break;
0384         fallthrough;
0385     case 0x04:
0386         /* Device end interrupt. */
0387         if ((raw = req->info) == NULL)
0388             goto put_tty;        /* That shouldn't happen ... */
0389         if (req->type == RAW3215_READ && tty != NULL) {
0390             unsigned int cchar;
0391 
0392             count = 160 - req->residual;
0393             EBCASC(raw->inbuf, count);
0394             cchar = ctrlchar_handle(raw->inbuf, count, tty);
0395             switch (cchar & CTRLCHAR_MASK) {
0396             case CTRLCHAR_SYSRQ:
0397                 break;
0398 
0399             case CTRLCHAR_CTRL:
0400                 tty_insert_flip_char(&raw->port, cchar,
0401                         TTY_NORMAL);
0402                 tty_flip_buffer_push(&raw->port);
0403                 break;
0404 
0405             case CTRLCHAR_NONE:
0406                 if (count < 2 ||
0407                     (strncmp(raw->inbuf+count-2, "\252n", 2) &&
0408                      strncmp(raw->inbuf+count-2, "^n", 2)) ) {
0409                     /* add the auto \n */
0410                     raw->inbuf[count] = '\n';
0411                     count++;
0412                 } else
0413                     count -= 2;
0414                 tty_insert_flip_string(&raw->port, raw->inbuf,
0415                         count);
0416                 tty_flip_buffer_push(&raw->port);
0417                 break;
0418             }
0419         } else if (req->type == RAW3215_WRITE) {
0420             raw->count -= req->len;
0421             raw->written -= req->len;
0422         }
0423         raw->flags &= ~RAW3215_WORKING;
0424         raw3215_free_req(req);
0425         /* check for empty wait */
0426         if (waitqueue_active(&raw->empty_wait) &&
0427             raw->queued_write == NULL &&
0428             raw->queued_read == NULL) {
0429             wake_up_interruptible(&raw->empty_wait);
0430         }
0431         raw3215_next_io(raw, tty);
0432         break;
0433     default:
0434         /* Strange interrupt, I'll do my best to clean up */
0435         if (req != NULL && req->type != RAW3215_FREE) {
0436             if (req->type == RAW3215_WRITE) {
0437                 raw->count -= req->len;
0438                 raw->written -= req->len;
0439             }
0440             raw->flags &= ~RAW3215_WORKING;
0441             raw3215_free_req(req);
0442         }
0443         raw3215_next_io(raw, tty);
0444     }
0445 put_tty:
0446     tty_kref_put(tty);
0447 }
0448 
0449 /*
0450  * Wait until length bytes are available int the output buffer.
0451  * Has to be called with the s390irq lock held. Can be called
0452  * disabled.
0453  */
0454 static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
0455 {
0456     while (RAW3215_BUFFER_SIZE - raw->count < length) {
0457         /* there might be a request pending */
0458         raw->flags |= RAW3215_FLUSHING;
0459         raw3215_mk_write_req(raw);
0460         raw3215_try_io(raw);
0461         raw->flags &= ~RAW3215_FLUSHING;
0462 #ifdef CONFIG_TN3215_CONSOLE
0463         ccw_device_wait_idle(raw->cdev);
0464 #endif
0465         /* Enough room freed up ? */
0466         if (RAW3215_BUFFER_SIZE - raw->count >= length)
0467             break;
0468         /* there might be another cpu waiting for the lock */
0469         spin_unlock(get_ccwdev_lock(raw->cdev));
0470         udelay(100);
0471         spin_lock(get_ccwdev_lock(raw->cdev));
0472     }
0473 }
0474 
0475 /*
0476  * String write routine for 3215 devices
0477  */
0478 static void raw3215_write(struct raw3215_info *raw, const char *str,
0479               unsigned int length)
0480 {
0481     unsigned long flags;
0482     int c, count;
0483 
0484     while (length > 0) {
0485         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
0486         count = (length > RAW3215_BUFFER_SIZE) ?
0487                          RAW3215_BUFFER_SIZE : length;
0488         length -= count;
0489 
0490         raw3215_make_room(raw, count);
0491 
0492         /* copy string to output buffer and convert it to EBCDIC */
0493         while (1) {
0494             c = min_t(int, count,
0495                   min(RAW3215_BUFFER_SIZE - raw->count,
0496                       RAW3215_BUFFER_SIZE - raw->head));
0497             if (c <= 0)
0498                 break;
0499             memcpy(raw->buffer + raw->head, str, c);
0500             ASCEBC(raw->buffer + raw->head, c);
0501             raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
0502             raw->count += c;
0503             raw->line_pos += c;
0504             str += c;
0505             count -= c;
0506         }
0507         if (!(raw->flags & RAW3215_WORKING)) {
0508             raw3215_mk_write_req(raw);
0509             /* start or queue request */
0510             raw3215_try_io(raw);
0511         }
0512         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0513     }
0514 }
0515 
0516 /*
0517  * Put character routine for 3215 devices
0518  */
0519 static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
0520 {
0521     unsigned long flags;
0522     unsigned int length, i;
0523 
0524     spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
0525     if (ch == '\t') {
0526         length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
0527         raw->line_pos += length;
0528         ch = ' ';
0529     } else if (ch == '\n') {
0530         length = 1;
0531         raw->line_pos = 0;
0532     } else {
0533         length = 1;
0534         raw->line_pos++;
0535     }
0536     raw3215_make_room(raw, length);
0537 
0538     for (i = 0; i < length; i++) {
0539         raw->buffer[raw->head] = (char) _ascebc[(int) ch];
0540         raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
0541         raw->count++;
0542     }
0543     if (!(raw->flags & RAW3215_WORKING)) {
0544         raw3215_mk_write_req(raw);
0545         /* start or queue request */
0546         raw3215_try_io(raw);
0547     }
0548     spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0549 }
0550 
0551 /*
0552  * Flush routine, it simply sets the flush flag and tries to start
0553  * pending IO.
0554  */
0555 static void raw3215_flush_buffer(struct raw3215_info *raw)
0556 {
0557     unsigned long flags;
0558 
0559     spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
0560     if (raw->count > 0) {
0561         raw->flags |= RAW3215_FLUSHING;
0562         raw3215_try_io(raw);
0563         raw->flags &= ~RAW3215_FLUSHING;
0564     }
0565     spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0566 }
0567 
0568 /*
0569  * Fire up a 3215 device.
0570  */
0571 static int raw3215_startup(struct raw3215_info *raw)
0572 {
0573     unsigned long flags;
0574 
0575     if (tty_port_initialized(&raw->port))
0576         return 0;
0577     raw->line_pos = 0;
0578     tty_port_set_initialized(&raw->port, 1);
0579     spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
0580     raw3215_try_io(raw);
0581     spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0582 
0583     return 0;
0584 }
0585 
0586 /*
0587  * Shutdown a 3215 device.
0588  */
0589 static void raw3215_shutdown(struct raw3215_info *raw)
0590 {
0591     DECLARE_WAITQUEUE(wait, current);
0592     unsigned long flags;
0593 
0594     if (!tty_port_initialized(&raw->port) || (raw->flags & RAW3215_FIXED))
0595         return;
0596     /* Wait for outstanding requests, then free irq */
0597     spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
0598     if ((raw->flags & RAW3215_WORKING) ||
0599         raw->queued_write != NULL ||
0600         raw->queued_read != NULL) {
0601         add_wait_queue(&raw->empty_wait, &wait);
0602         set_current_state(TASK_INTERRUPTIBLE);
0603         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0604         schedule();
0605         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
0606         remove_wait_queue(&raw->empty_wait, &wait);
0607         set_current_state(TASK_RUNNING);
0608         tty_port_set_initialized(&raw->port, 1);
0609     }
0610     spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0611 }
0612 
0613 static struct raw3215_info *raw3215_alloc_info(void)
0614 {
0615     struct raw3215_info *info;
0616 
0617     info = kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
0618     if (!info)
0619         return NULL;
0620 
0621     info->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
0622     info->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
0623     if (!info->buffer || !info->inbuf) {
0624         kfree(info->inbuf);
0625         kfree(info->buffer);
0626         kfree(info);
0627         return NULL;
0628     }
0629 
0630     timer_setup(&info->timer, raw3215_timeout, 0);
0631     init_waitqueue_head(&info->empty_wait);
0632     tty_port_init(&info->port);
0633 
0634     return info;
0635 }
0636 
0637 static void raw3215_free_info(struct raw3215_info *raw)
0638 {
0639     kfree(raw->inbuf);
0640     kfree(raw->buffer);
0641     tty_port_destroy(&raw->port);
0642     kfree(raw);
0643 }
0644 
0645 static int raw3215_probe (struct ccw_device *cdev)
0646 {
0647     struct raw3215_info *raw;
0648     int line;
0649 
0650     /* Console is special. */
0651     if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
0652         return 0;
0653 
0654     raw = raw3215_alloc_info();
0655     if (raw == NULL)
0656         return -ENOMEM;
0657 
0658     raw->cdev = cdev;
0659     dev_set_drvdata(&cdev->dev, raw);
0660     cdev->handler = raw3215_irq;
0661 
0662     spin_lock(&raw3215_device_lock);
0663     for (line = 0; line < NR_3215; line++) {
0664         if (!raw3215[line]) {
0665             raw3215[line] = raw;
0666             break;
0667         }
0668     }
0669     spin_unlock(&raw3215_device_lock);
0670     if (line == NR_3215) {
0671         raw3215_free_info(raw);
0672         return -ENODEV;
0673     }
0674 
0675     return 0;
0676 }
0677 
0678 static void raw3215_remove (struct ccw_device *cdev)
0679 {
0680     struct raw3215_info *raw;
0681     unsigned int line;
0682 
0683     ccw_device_set_offline(cdev);
0684     raw = dev_get_drvdata(&cdev->dev);
0685     if (raw) {
0686         spin_lock(&raw3215_device_lock);
0687         for (line = 0; line < NR_3215; line++)
0688             if (raw3215[line] == raw)
0689                 break;
0690         raw3215[line] = NULL;
0691         spin_unlock(&raw3215_device_lock);
0692         dev_set_drvdata(&cdev->dev, NULL);
0693         raw3215_free_info(raw);
0694     }
0695 }
0696 
0697 static int raw3215_set_online (struct ccw_device *cdev)
0698 {
0699     struct raw3215_info *raw;
0700 
0701     raw = dev_get_drvdata(&cdev->dev);
0702     if (!raw)
0703         return -ENODEV;
0704 
0705     return raw3215_startup(raw);
0706 }
0707 
0708 static int raw3215_set_offline (struct ccw_device *cdev)
0709 {
0710     struct raw3215_info *raw;
0711 
0712     raw = dev_get_drvdata(&cdev->dev);
0713     if (!raw)
0714         return -ENODEV;
0715 
0716     raw3215_shutdown(raw);
0717 
0718     return 0;
0719 }
0720 
0721 static struct ccw_device_id raw3215_id[] = {
0722     { CCW_DEVICE(0x3215, 0) },
0723     { /* end of list */ },
0724 };
0725 
0726 static struct ccw_driver raw3215_ccw_driver = {
0727     .driver = {
0728         .name   = "3215",
0729         .owner  = THIS_MODULE,
0730     },
0731     .ids        = raw3215_id,
0732     .probe      = &raw3215_probe,
0733     .remove     = &raw3215_remove,
0734     .set_online = &raw3215_set_online,
0735     .set_offline    = &raw3215_set_offline,
0736     .int_class  = IRQIO_C15,
0737 };
0738 
0739 #ifdef CONFIG_TN3215_CONSOLE
0740 /*
0741  * Write a string to the 3215 console
0742  */
0743 static void con3215_write(struct console *co, const char *str,
0744               unsigned int count)
0745 {
0746     struct raw3215_info *raw;
0747     int i;
0748 
0749     if (count <= 0)
0750         return;
0751     raw = raw3215[0];   /* console 3215 is the first one */
0752     while (count > 0) {
0753         for (i = 0; i < count; i++)
0754             if (str[i] == '\t' || str[i] == '\n')
0755                 break;
0756         raw3215_write(raw, str, i);
0757         count -= i;
0758         str += i;
0759         if (count > 0) {
0760             raw3215_putchar(raw, *str);
0761             count--;
0762             str++;
0763         }
0764     }
0765 }
0766 
0767 static struct tty_driver *con3215_device(struct console *c, int *index)
0768 {
0769     *index = c->index;
0770     return tty3215_driver;
0771 }
0772 
0773 /*
0774  * The below function is called as a panic/reboot notifier before the
0775  * system enters a disabled, endless loop.
0776  *
0777  * Notice we must use the spin_trylock() alternative, to prevent lockups
0778  * in atomic context (panic routine runs with secondary CPUs, local IRQs
0779  * and preemption disabled).
0780  */
0781 static int con3215_notify(struct notifier_block *self,
0782               unsigned long event, void *data)
0783 {
0784     struct raw3215_info *raw;
0785     unsigned long flags;
0786 
0787     raw = raw3215[0];  /* console 3215 is the first one */
0788     if (!spin_trylock_irqsave(get_ccwdev_lock(raw->cdev), flags))
0789         return NOTIFY_DONE;
0790     raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
0791     spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
0792 
0793     return NOTIFY_DONE;
0794 }
0795 
0796 static struct notifier_block on_panic_nb = {
0797     .notifier_call = con3215_notify,
0798     .priority = INT_MIN + 1, /* run the callback late */
0799 };
0800 
0801 static struct notifier_block on_reboot_nb = {
0802     .notifier_call = con3215_notify,
0803     .priority = INT_MIN + 1, /* run the callback late */
0804 };
0805 
0806 /*
0807  *  The console structure for the 3215 console
0808  */
0809 static struct console con3215 = {
0810     .name    = "ttyS",
0811     .write   = con3215_write,
0812     .device  = con3215_device,
0813     .flags   = CON_PRINTBUFFER,
0814 };
0815 
0816 /*
0817  * 3215 console initialization code called from console_init().
0818  */
0819 static int __init con3215_init(void)
0820 {
0821     struct ccw_device *cdev;
0822     struct raw3215_info *raw;
0823     struct raw3215_req *req;
0824     int i;
0825 
0826     /* Check if 3215 is to be the console */
0827     if (!CONSOLE_IS_3215)
0828         return -ENODEV;
0829 
0830     /* Set the console mode for VM */
0831     if (MACHINE_IS_VM) {
0832         cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
0833         cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
0834     }
0835 
0836     /* allocate 3215 request structures */
0837     raw3215_freelist = NULL;
0838     for (i = 0; i < NR_3215_REQ; i++) {
0839         req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
0840         if (!req)
0841             return -ENOMEM;
0842         req->next = raw3215_freelist;
0843         raw3215_freelist = req;
0844     }
0845 
0846     cdev = ccw_device_create_console(&raw3215_ccw_driver);
0847     if (IS_ERR(cdev))
0848         return -ENODEV;
0849 
0850     raw3215[0] = raw = raw3215_alloc_info();
0851     raw->cdev = cdev;
0852     dev_set_drvdata(&cdev->dev, raw);
0853     cdev->handler = raw3215_irq;
0854 
0855     raw->flags |= RAW3215_FIXED;
0856     if (ccw_device_enable_console(cdev)) {
0857         ccw_device_destroy_console(cdev);
0858         raw3215_free_info(raw);
0859         raw3215[0] = NULL;
0860         return -ENODEV;
0861     }
0862 
0863     /* Request the console irq */
0864     if (raw3215_startup(raw) != 0) {
0865         raw3215_free_info(raw);
0866         raw3215[0] = NULL;
0867         return -ENODEV;
0868     }
0869     atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
0870     register_reboot_notifier(&on_reboot_nb);
0871     register_console(&con3215);
0872     return 0;
0873 }
0874 console_initcall(con3215_init);
0875 #endif
0876 
0877 static int tty3215_install(struct tty_driver *driver, struct tty_struct *tty)
0878 {
0879     struct raw3215_info *raw;
0880 
0881     raw = raw3215[tty->index];
0882     if (raw == NULL)
0883         return -ENODEV;
0884 
0885     tty->driver_data = raw;
0886 
0887     return tty_port_install(&raw->port, driver, tty);
0888 }
0889 
0890 /*
0891  * tty3215_open
0892  *
0893  * This routine is called whenever a 3215 tty is opened.
0894  */
0895 static int tty3215_open(struct tty_struct *tty, struct file * filp)
0896 {
0897     struct raw3215_info *raw = tty->driver_data;
0898 
0899     tty_port_tty_set(&raw->port, tty);
0900 
0901     /*
0902      * Start up 3215 device
0903      */
0904     return raw3215_startup(raw);
0905 }
0906 
0907 /*
0908  * tty3215_close()
0909  *
0910  * This routine is called when the 3215 tty is closed. We wait
0911  * for the remaining request to be completed. Then we clean up.
0912  */
0913 static void tty3215_close(struct tty_struct *tty, struct file * filp)
0914 {
0915     struct raw3215_info *raw = tty->driver_data;
0916 
0917     if (raw == NULL || tty->count > 1)
0918         return;
0919     tty->closing = 1;
0920     /* Shutdown the terminal */
0921     raw3215_shutdown(raw);
0922     tty->closing = 0;
0923     tty_port_tty_set(&raw->port, NULL);
0924 }
0925 
0926 /*
0927  * Returns the amount of free space in the output buffer.
0928  */
0929 static unsigned int tty3215_write_room(struct tty_struct *tty)
0930 {
0931     struct raw3215_info *raw = tty->driver_data;
0932 
0933     /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
0934     if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
0935         return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
0936     else
0937         return 0;
0938 }
0939 
0940 /*
0941  * String write routine for 3215 ttys
0942  */
0943 static int tty3215_write(struct tty_struct * tty,
0944              const unsigned char *buf, int count)
0945 {
0946     struct raw3215_info *raw = tty->driver_data;
0947     int i, written;
0948 
0949     written = count;
0950     while (count > 0) {
0951         for (i = 0; i < count; i++)
0952             if (buf[i] == '\t' || buf[i] == '\n')
0953                 break;
0954         raw3215_write(raw, buf, i);
0955         count -= i;
0956         buf += i;
0957         if (count > 0) {
0958             raw3215_putchar(raw, *buf);
0959             count--;
0960             buf++;
0961         }
0962     }
0963     return written;
0964 }
0965 
0966 /*
0967  * Put character routine for 3215 ttys
0968  */
0969 static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
0970 {
0971     struct raw3215_info *raw = tty->driver_data;
0972 
0973     raw3215_putchar(raw, ch);
0974 
0975     return 1;
0976 }
0977 
0978 static void tty3215_flush_chars(struct tty_struct *tty)
0979 {
0980 }
0981 
0982 /*
0983  * Returns the number of characters in the output buffer
0984  */
0985 static unsigned int tty3215_chars_in_buffer(struct tty_struct *tty)
0986 {
0987     struct raw3215_info *raw = tty->driver_data;
0988 
0989     return raw->count;
0990 }
0991 
0992 static void tty3215_flush_buffer(struct tty_struct *tty)
0993 {
0994     struct raw3215_info *raw = tty->driver_data;
0995 
0996     raw3215_flush_buffer(raw);
0997     tty_wakeup(tty);
0998 }
0999 
1000 /*
1001  * Disable reading from a 3215 tty
1002  */
1003 static void tty3215_throttle(struct tty_struct * tty)
1004 {
1005     struct raw3215_info *raw = tty->driver_data;
1006 
1007     raw->flags |= RAW3215_THROTTLED;
1008 }
1009 
1010 /*
1011  * Enable reading from a 3215 tty
1012  */
1013 static void tty3215_unthrottle(struct tty_struct * tty)
1014 {
1015     struct raw3215_info *raw = tty->driver_data;
1016     unsigned long flags;
1017 
1018     if (raw->flags & RAW3215_THROTTLED) {
1019         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1020         raw->flags &= ~RAW3215_THROTTLED;
1021         raw3215_try_io(raw);
1022         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1023     }
1024 }
1025 
1026 /*
1027  * Disable writing to a 3215 tty
1028  */
1029 static void tty3215_stop(struct tty_struct *tty)
1030 {
1031     struct raw3215_info *raw = tty->driver_data;
1032 
1033     raw->flags |= RAW3215_STOPPED;
1034 }
1035 
1036 /*
1037  * Enable writing to a 3215 tty
1038  */
1039 static void tty3215_start(struct tty_struct *tty)
1040 {
1041     struct raw3215_info *raw = tty->driver_data;
1042     unsigned long flags;
1043 
1044     if (raw->flags & RAW3215_STOPPED) {
1045         spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
1046         raw->flags &= ~RAW3215_STOPPED;
1047         raw3215_try_io(raw);
1048         spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
1049     }
1050 }
1051 
1052 static const struct tty_operations tty3215_ops = {
1053     .install = tty3215_install,
1054     .open = tty3215_open,
1055     .close = tty3215_close,
1056     .write = tty3215_write,
1057     .put_char = tty3215_put_char,
1058     .flush_chars = tty3215_flush_chars,
1059     .write_room = tty3215_write_room,
1060     .chars_in_buffer = tty3215_chars_in_buffer,
1061     .flush_buffer = tty3215_flush_buffer,
1062     .throttle = tty3215_throttle,
1063     .unthrottle = tty3215_unthrottle,
1064     .stop = tty3215_stop,
1065     .start = tty3215_start,
1066 };
1067 
1068 /*
1069  * 3215 tty registration code called from tty_init().
1070  * Most kernel services (incl. kmalloc) are available at this poimt.
1071  */
1072 static int __init tty3215_init(void)
1073 {
1074     struct tty_driver *driver;
1075     int ret;
1076 
1077     if (!CONSOLE_IS_3215)
1078         return 0;
1079 
1080     driver = tty_alloc_driver(NR_3215, TTY_DRIVER_REAL_RAW);
1081     if (IS_ERR(driver))
1082         return PTR_ERR(driver);
1083 
1084     ret = ccw_driver_register(&raw3215_ccw_driver);
1085     if (ret) {
1086         tty_driver_kref_put(driver);
1087         return ret;
1088     }
1089     /*
1090      * Initialize the tty_driver structure
1091      * Entries in tty3215_driver that are NOT initialized:
1092      * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
1093      */
1094 
1095     driver->driver_name = "tty3215";
1096     driver->name = "ttyS";
1097     driver->major = TTY_MAJOR;
1098     driver->minor_start = 64;
1099     driver->type = TTY_DRIVER_TYPE_SYSTEM;
1100     driver->subtype = SYSTEM_TYPE_TTY;
1101     driver->init_termios = tty_std_termios;
1102     driver->init_termios.c_iflag = IGNBRK | IGNPAR;
1103     driver->init_termios.c_oflag = ONLCR;
1104     driver->init_termios.c_lflag = ISIG;
1105     tty_set_operations(driver, &tty3215_ops);
1106     ret = tty_register_driver(driver);
1107     if (ret) {
1108         tty_driver_kref_put(driver);
1109         return ret;
1110     }
1111     tty3215_driver = driver;
1112     return 0;
1113 }
1114 device_initcall(tty3215_init);