Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * SCLP VT220 terminal driver.
0004  *
0005  * Copyright IBM Corp. 2003, 2009
0006  *
0007  * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/panic_notifier.h>
0013 #include <linux/list.h>
0014 #include <linux/wait.h>
0015 #include <linux/timer.h>
0016 #include <linux/kernel.h>
0017 #include <linux/sysrq.h>
0018 #include <linux/tty.h>
0019 #include <linux/tty_driver.h>
0020 #include <linux/tty_flip.h>
0021 #include <linux/errno.h>
0022 #include <linux/mm.h>
0023 #include <linux/major.h>
0024 #include <linux/console.h>
0025 #include <linux/kdev_t.h>
0026 #include <linux/interrupt.h>
0027 #include <linux/init.h>
0028 #include <linux/reboot.h>
0029 #include <linux/slab.h>
0030 
0031 #include <linux/uaccess.h>
0032 #include "sclp.h"
0033 #include "ctrlchar.h"
0034 
0035 #define SCLP_VT220_MAJOR        TTY_MAJOR
0036 #define SCLP_VT220_MINOR        65
0037 #define SCLP_VT220_DRIVER_NAME      "sclp_vt220"
0038 #define SCLP_VT220_DEVICE_NAME      "ttysclp"
0039 #define SCLP_VT220_CONSOLE_NAME     "ttysclp"
0040 #define SCLP_VT220_CONSOLE_INDEX    0   /* console=ttysclp0 */
0041 
0042 /* Representation of a single write request */
0043 struct sclp_vt220_request {
0044     struct list_head list;
0045     struct sclp_req sclp_req;
0046     int retry_count;
0047 };
0048 
0049 /* VT220 SCCB */
0050 struct sclp_vt220_sccb {
0051     struct sccb_header header;
0052     struct evbuf_header evbuf;
0053 };
0054 
0055 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
0056                      sizeof(struct sclp_vt220_request) - \
0057                      sizeof(struct sclp_vt220_sccb))
0058 
0059 /* Structures and data needed to register tty driver */
0060 static struct tty_driver *sclp_vt220_driver;
0061 
0062 static struct tty_port sclp_vt220_port;
0063 
0064 /* Lock to protect internal data from concurrent access */
0065 static DEFINE_SPINLOCK(sclp_vt220_lock);
0066 
0067 /* List of empty pages to be used as write request buffers */
0068 static LIST_HEAD(sclp_vt220_empty);
0069 
0070 /* List of pending requests */
0071 static LIST_HEAD(sclp_vt220_outqueue);
0072 
0073 /* Flag that output queue is currently running */
0074 static int sclp_vt220_queue_running;
0075 
0076 /* Timer used for delaying write requests to merge subsequent messages into
0077  * a single buffer */
0078 static struct timer_list sclp_vt220_timer;
0079 
0080 /* Pointer to current request buffer which has been partially filled but not
0081  * yet sent */
0082 static struct sclp_vt220_request *sclp_vt220_current_request;
0083 
0084 /* Number of characters in current request buffer */
0085 static int sclp_vt220_buffered_chars;
0086 
0087 /* Counter controlling core driver initialization. */
0088 static int __initdata sclp_vt220_init_count;
0089 
0090 /* Flag indicating that sclp_vt220_current_request should really
0091  * have been already queued but wasn't because the SCLP was processing
0092  * another buffer */
0093 static int sclp_vt220_flush_later;
0094 
0095 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
0096 static int __sclp_vt220_emit(struct sclp_vt220_request *request);
0097 static void sclp_vt220_emit_current(void);
0098 
0099 /* Registration structure for SCLP output event buffers */
0100 static struct sclp_register sclp_vt220_register = {
0101     .send_mask      = EVTYP_VT220MSG_MASK,
0102 };
0103 
0104 /* Registration structure for SCLP input event buffers */
0105 static struct sclp_register sclp_vt220_register_input = {
0106     .receive_mask       = EVTYP_VT220MSG_MASK,
0107     .receiver_fn        = sclp_vt220_receiver_fn,
0108 };
0109 
0110 
0111 /*
0112  * Put provided request buffer back into queue and check emit pending
0113  * buffers if necessary.
0114  */
0115 static void
0116 sclp_vt220_process_queue(struct sclp_vt220_request *request)
0117 {
0118     unsigned long flags;
0119     void *page;
0120 
0121     do {
0122         /* Put buffer back to list of empty buffers */
0123         page = request->sclp_req.sccb;
0124         spin_lock_irqsave(&sclp_vt220_lock, flags);
0125         /* Move request from outqueue to empty queue */
0126         list_del(&request->list);
0127         list_add_tail((struct list_head *) page, &sclp_vt220_empty);
0128         /* Check if there is a pending buffer on the out queue. */
0129         request = NULL;
0130         if (!list_empty(&sclp_vt220_outqueue))
0131             request = list_entry(sclp_vt220_outqueue.next,
0132                          struct sclp_vt220_request, list);
0133         if (!request) {
0134             sclp_vt220_queue_running = 0;
0135             spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0136             break;
0137         }
0138         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0139     } while (__sclp_vt220_emit(request));
0140     if (request == NULL && sclp_vt220_flush_later)
0141         sclp_vt220_emit_current();
0142     tty_port_tty_wakeup(&sclp_vt220_port);
0143 }
0144 
0145 #define SCLP_BUFFER_MAX_RETRY       1
0146 
0147 /*
0148  * Callback through which the result of a write request is reported by the
0149  * SCLP.
0150  */
0151 static void
0152 sclp_vt220_callback(struct sclp_req *request, void *data)
0153 {
0154     struct sclp_vt220_request *vt220_request;
0155     struct sclp_vt220_sccb *sccb;
0156 
0157     vt220_request = (struct sclp_vt220_request *) data;
0158     if (request->status == SCLP_REQ_FAILED) {
0159         sclp_vt220_process_queue(vt220_request);
0160         return;
0161     }
0162     sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
0163 
0164     /* Check SCLP response code and choose suitable action  */
0165     switch (sccb->header.response_code) {
0166     case 0x0020 :
0167         break;
0168 
0169     case 0x05f0: /* Target resource in improper state */
0170         break;
0171 
0172     case 0x0340: /* Contained SCLP equipment check */
0173         if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
0174             break;
0175         /* Remove processed buffers and requeue rest */
0176         if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
0177             /* Not all buffers were processed */
0178             sccb->header.response_code = 0x0000;
0179             vt220_request->sclp_req.status = SCLP_REQ_FILLED;
0180             if (sclp_add_request(request) == 0)
0181                 return;
0182         }
0183         break;
0184 
0185     case 0x0040: /* SCLP equipment check */
0186         if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
0187             break;
0188         sccb->header.response_code = 0x0000;
0189         vt220_request->sclp_req.status = SCLP_REQ_FILLED;
0190         if (sclp_add_request(request) == 0)
0191             return;
0192         break;
0193 
0194     default:
0195         break;
0196     }
0197     sclp_vt220_process_queue(vt220_request);
0198 }
0199 
0200 /*
0201  * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
0202  * otherwise.
0203  */
0204 static int
0205 __sclp_vt220_emit(struct sclp_vt220_request *request)
0206 {
0207     request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
0208     request->sclp_req.status = SCLP_REQ_FILLED;
0209     request->sclp_req.callback = sclp_vt220_callback;
0210     request->sclp_req.callback_data = (void *) request;
0211 
0212     return sclp_add_request(&request->sclp_req);
0213 }
0214 
0215 /*
0216  * Queue and emit current request.
0217  */
0218 static void
0219 sclp_vt220_emit_current(void)
0220 {
0221     unsigned long flags;
0222     struct sclp_vt220_request *request;
0223     struct sclp_vt220_sccb *sccb;
0224 
0225     spin_lock_irqsave(&sclp_vt220_lock, flags);
0226     if (sclp_vt220_current_request) {
0227         sccb = (struct sclp_vt220_sccb *) 
0228                 sclp_vt220_current_request->sclp_req.sccb;
0229         /* Only emit buffers with content */
0230         if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
0231             list_add_tail(&sclp_vt220_current_request->list,
0232                       &sclp_vt220_outqueue);
0233             sclp_vt220_current_request = NULL;
0234             del_timer(&sclp_vt220_timer);
0235         }
0236         sclp_vt220_flush_later = 0;
0237     }
0238     if (sclp_vt220_queue_running)
0239         goto out_unlock;
0240     if (list_empty(&sclp_vt220_outqueue))
0241         goto out_unlock;
0242     request = list_first_entry(&sclp_vt220_outqueue,
0243                    struct sclp_vt220_request, list);
0244     sclp_vt220_queue_running = 1;
0245     spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0246 
0247     if (__sclp_vt220_emit(request))
0248         sclp_vt220_process_queue(request);
0249     return;
0250 out_unlock:
0251     spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0252 }
0253 
0254 #define SCLP_NORMAL_WRITE   0x00
0255 
0256 /*
0257  * Helper function to initialize a page with the sclp request structure.
0258  */
0259 static struct sclp_vt220_request *
0260 sclp_vt220_initialize_page(void *page)
0261 {
0262     struct sclp_vt220_request *request;
0263     struct sclp_vt220_sccb *sccb;
0264 
0265     /* Place request structure at end of page */
0266     request = ((struct sclp_vt220_request *)
0267             ((addr_t) page + PAGE_SIZE)) - 1;
0268     request->retry_count = 0;
0269     request->sclp_req.sccb = page;
0270     /* SCCB goes at start of page */
0271     sccb = (struct sclp_vt220_sccb *) page;
0272     memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
0273     sccb->header.length = sizeof(struct sclp_vt220_sccb);
0274     sccb->header.function_code = SCLP_NORMAL_WRITE;
0275     sccb->header.response_code = 0x0000;
0276     sccb->evbuf.type = EVTYP_VT220MSG;
0277     sccb->evbuf.length = sizeof(struct evbuf_header);
0278 
0279     return request;
0280 }
0281 
0282 static inline unsigned int
0283 sclp_vt220_space_left(struct sclp_vt220_request *request)
0284 {
0285     struct sclp_vt220_sccb *sccb;
0286     sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
0287     return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
0288            sccb->header.length;
0289 }
0290 
0291 static inline unsigned int
0292 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
0293 {
0294     struct sclp_vt220_sccb *sccb;
0295     sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
0296     return sccb->evbuf.length - sizeof(struct evbuf_header);
0297 }
0298 
0299 /*
0300  * Add msg to buffer associated with request. Return the number of characters
0301  * added.
0302  */
0303 static int
0304 sclp_vt220_add_msg(struct sclp_vt220_request *request,
0305            const unsigned char *msg, int count, int convertlf)
0306 {
0307     struct sclp_vt220_sccb *sccb;
0308     void *buffer;
0309     unsigned char c;
0310     int from;
0311     int to;
0312 
0313     if (count > sclp_vt220_space_left(request))
0314         count = sclp_vt220_space_left(request);
0315     if (count <= 0)
0316         return 0;
0317 
0318     sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
0319     buffer = (void *) ((addr_t) sccb + sccb->header.length);
0320 
0321     if (convertlf) {
0322         /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
0323         for (from=0, to=0;
0324              (from < count) && (to < sclp_vt220_space_left(request));
0325              from++) {
0326             /* Retrieve character */
0327             c = msg[from];
0328             /* Perform conversion */
0329             if (c == 0x0a) {
0330                 if (to + 1 < sclp_vt220_space_left(request)) {
0331                     ((unsigned char *) buffer)[to++] = c;
0332                     ((unsigned char *) buffer)[to++] = 0x0d;
0333                 } else
0334                     break;
0335 
0336             } else
0337                 ((unsigned char *) buffer)[to++] = c;
0338         }
0339         sccb->header.length += to;
0340         sccb->evbuf.length += to;
0341         return from;
0342     } else {
0343         memcpy(buffer, (const void *) msg, count);
0344         sccb->header.length += count;
0345         sccb->evbuf.length += count;
0346         return count;
0347     }
0348 }
0349 
0350 /*
0351  * Emit buffer after having waited long enough for more data to arrive.
0352  */
0353 static void
0354 sclp_vt220_timeout(struct timer_list *unused)
0355 {
0356     sclp_vt220_emit_current();
0357 }
0358 
0359 #define BUFFER_MAX_DELAY    HZ/20
0360 
0361 /*
0362  * Drop oldest console buffer if sclp_con_drop is set
0363  */
0364 static int
0365 sclp_vt220_drop_buffer(void)
0366 {
0367     struct list_head *list;
0368     struct sclp_vt220_request *request;
0369     void *page;
0370 
0371     if (!sclp_console_drop)
0372         return 0;
0373     list = sclp_vt220_outqueue.next;
0374     if (sclp_vt220_queue_running)
0375         /* The first element is in I/O */
0376         list = list->next;
0377     if (list == &sclp_vt220_outqueue)
0378         return 0;
0379     list_del(list);
0380     request = list_entry(list, struct sclp_vt220_request, list);
0381     page = request->sclp_req.sccb;
0382     list_add_tail((struct list_head *) page, &sclp_vt220_empty);
0383     return 1;
0384 }
0385 
0386 /* 
0387  * Internal implementation of the write function. Write COUNT bytes of data
0388  * from memory at BUF
0389  * to the SCLP interface. In case that the data does not fit into the current
0390  * write buffer, emit the current one and allocate a new one. If there are no
0391  * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
0392  * is non-zero, the buffer will be scheduled for emitting after a timeout -
0393  * otherwise the user has to explicitly call the flush function.
0394  * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
0395  * buffer should be converted to 0x0a 0x0d. After completion, return the number
0396  * of bytes written.
0397  */
0398 static int
0399 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
0400            int convertlf, int may_fail)
0401 {
0402     unsigned long flags;
0403     void *page;
0404     int written;
0405     int overall_written;
0406 
0407     if (count <= 0)
0408         return 0;
0409     overall_written = 0;
0410     spin_lock_irqsave(&sclp_vt220_lock, flags);
0411     do {
0412         /* Create an sclp output buffer if none exists yet */
0413         if (sclp_vt220_current_request == NULL) {
0414             if (list_empty(&sclp_vt220_empty))
0415                 sclp_console_full++;
0416             while (list_empty(&sclp_vt220_empty)) {
0417                 if (may_fail)
0418                     goto out;
0419                 if (sclp_vt220_drop_buffer())
0420                     break;
0421                 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0422 
0423                 sclp_sync_wait();
0424                 spin_lock_irqsave(&sclp_vt220_lock, flags);
0425             }
0426             page = (void *) sclp_vt220_empty.next;
0427             list_del((struct list_head *) page);
0428             sclp_vt220_current_request =
0429                 sclp_vt220_initialize_page(page);
0430         }
0431         /* Try to write the string to the current request buffer */
0432         written = sclp_vt220_add_msg(sclp_vt220_current_request,
0433                          buf, count, convertlf);
0434         overall_written += written;
0435         if (written == count)
0436             break;
0437         /*
0438          * Not all characters could be written to the current
0439          * output buffer. Emit the buffer, create a new buffer
0440          * and then output the rest of the string.
0441          */
0442         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0443         sclp_vt220_emit_current();
0444         spin_lock_irqsave(&sclp_vt220_lock, flags);
0445         buf += written;
0446         count -= written;
0447     } while (count > 0);
0448     /* Setup timer to output current console buffer after some time */
0449     if (sclp_vt220_current_request != NULL &&
0450         !timer_pending(&sclp_vt220_timer) && do_schedule) {
0451         sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
0452         add_timer(&sclp_vt220_timer);
0453     }
0454 out:
0455     spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0456     return overall_written;
0457 }
0458 
0459 /*
0460  * This routine is called by the kernel to write a series of
0461  * characters to the tty device.  The characters may come from
0462  * user space or kernel space.  This routine will return the
0463  * number of characters actually accepted for writing.
0464  */
0465 static int
0466 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
0467 {
0468     return __sclp_vt220_write(buf, count, 1, 0, 1);
0469 }
0470 
0471 #define SCLP_VT220_SESSION_ENDED    0x01
0472 #define SCLP_VT220_SESSION_STARTED  0x80
0473 #define SCLP_VT220_SESSION_DATA     0x00
0474 
0475 #ifdef CONFIG_MAGIC_SYSRQ
0476 
0477 static int sysrq_pressed;
0478 static struct sysrq_work sysrq;
0479 
0480 static void sclp_vt220_reset_session(void)
0481 {
0482     sysrq_pressed = 0;
0483 }
0484 
0485 static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
0486 {
0487     int i;
0488 
0489     for (i = 0; i < count; i++) {
0490         /* Handle magic sys request */
0491         if (buffer[i] == ('O' ^ 0100)) { /* CTRL-O */
0492             /*
0493              * If pressed again, reset sysrq_pressed
0494              * and flip CTRL-O character
0495              */
0496             sysrq_pressed = !sysrq_pressed;
0497             if (sysrq_pressed)
0498                 continue;
0499         } else if (sysrq_pressed) {
0500             sysrq.key = buffer[i];
0501             schedule_sysrq_work(&sysrq);
0502             sysrq_pressed = 0;
0503             continue;
0504         }
0505         tty_insert_flip_char(&sclp_vt220_port, buffer[i], 0);
0506     }
0507 }
0508 
0509 #else
0510 
0511 static void sclp_vt220_reset_session(void)
0512 {
0513 }
0514 
0515 static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
0516 {
0517     tty_insert_flip_string(&sclp_vt220_port, buffer, count);
0518 }
0519 
0520 #endif
0521 
0522 /*
0523  * Called by the SCLP to report incoming event buffers.
0524  */
0525 static void
0526 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
0527 {
0528     char *buffer;
0529     unsigned int count;
0530 
0531     buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
0532     count = evbuf->length - sizeof(struct evbuf_header);
0533 
0534     switch (*buffer) {
0535     case SCLP_VT220_SESSION_ENDED:
0536     case SCLP_VT220_SESSION_STARTED:
0537         sclp_vt220_reset_session();
0538         break;
0539     case SCLP_VT220_SESSION_DATA:
0540         /* Send input to line discipline */
0541         buffer++;
0542         count--;
0543         sclp_vt220_handle_input(buffer, count);
0544         tty_flip_buffer_push(&sclp_vt220_port);
0545         break;
0546     }
0547 }
0548 
0549 /*
0550  * This routine is called when a particular tty device is opened.
0551  */
0552 static int
0553 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
0554 {
0555     if (tty->count == 1) {
0556         tty_port_tty_set(&sclp_vt220_port, tty);
0557         if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
0558             tty->winsize.ws_row = 24;
0559             tty->winsize.ws_col = 80;
0560         }
0561     }
0562     return 0;
0563 }
0564 
0565 /*
0566  * This routine is called when a particular tty device is closed.
0567  */
0568 static void
0569 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
0570 {
0571     if (tty->count == 1)
0572         tty_port_tty_set(&sclp_vt220_port, NULL);
0573 }
0574 
0575 /*
0576  * This routine is called by the kernel to write a single
0577  * character to the tty device.  If the kernel uses this routine,
0578  * it must call the flush_chars() routine (if defined) when it is
0579  * done stuffing characters into the driver.
0580  */
0581 static int
0582 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
0583 {
0584     return __sclp_vt220_write(&ch, 1, 0, 0, 1);
0585 }
0586 
0587 /*
0588  * This routine is called by the kernel after it has written a
0589  * series of characters to the tty device using put_char().  
0590  */
0591 static void
0592 sclp_vt220_flush_chars(struct tty_struct *tty)
0593 {
0594     if (!sclp_vt220_queue_running)
0595         sclp_vt220_emit_current();
0596     else
0597         sclp_vt220_flush_later = 1;
0598 }
0599 
0600 /*
0601  * This routine returns the numbers of characters the tty driver
0602  * will accept for queuing to be written.  This number is subject
0603  * to change as output buffers get emptied, or if the output flow
0604  * control is acted.
0605  */
0606 static unsigned int
0607 sclp_vt220_write_room(struct tty_struct *tty)
0608 {
0609     unsigned long flags;
0610     struct list_head *l;
0611     unsigned int count;
0612 
0613     spin_lock_irqsave(&sclp_vt220_lock, flags);
0614     count = 0;
0615     if (sclp_vt220_current_request != NULL)
0616         count = sclp_vt220_space_left(sclp_vt220_current_request);
0617     list_for_each(l, &sclp_vt220_empty)
0618         count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
0619     spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0620     return count;
0621 }
0622 
0623 /*
0624  * Return number of buffered chars.
0625  */
0626 static unsigned int
0627 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
0628 {
0629     unsigned long flags;
0630     struct list_head *l;
0631     struct sclp_vt220_request *r;
0632     unsigned int count = 0;
0633 
0634     spin_lock_irqsave(&sclp_vt220_lock, flags);
0635     if (sclp_vt220_current_request != NULL)
0636         count = sclp_vt220_chars_stored(sclp_vt220_current_request);
0637     list_for_each(l, &sclp_vt220_outqueue) {
0638         r = list_entry(l, struct sclp_vt220_request, list);
0639         count += sclp_vt220_chars_stored(r);
0640     }
0641     spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0642     return count;
0643 }
0644 
0645 /*
0646  * Pass on all buffers to the hardware. Return only when there are no more
0647  * buffers pending.
0648  */
0649 static void
0650 sclp_vt220_flush_buffer(struct tty_struct *tty)
0651 {
0652     sclp_vt220_emit_current();
0653 }
0654 
0655 /* Release allocated pages. */
0656 static void __init __sclp_vt220_free_pages(void)
0657 {
0658     struct list_head *page, *p;
0659 
0660     list_for_each_safe(page, p, &sclp_vt220_empty) {
0661         list_del(page);
0662         free_page((unsigned long) page);
0663     }
0664 }
0665 
0666 /* Release memory and unregister from sclp core. Controlled by init counting -
0667  * only the last invoker will actually perform these actions. */
0668 static void __init __sclp_vt220_cleanup(void)
0669 {
0670     sclp_vt220_init_count--;
0671     if (sclp_vt220_init_count != 0)
0672         return;
0673     sclp_unregister(&sclp_vt220_register);
0674     __sclp_vt220_free_pages();
0675     tty_port_destroy(&sclp_vt220_port);
0676 }
0677 
0678 /* Allocate buffer pages and register with sclp core. Controlled by init
0679  * counting - only the first invoker will actually perform these actions. */
0680 static int __init __sclp_vt220_init(int num_pages)
0681 {
0682     void *page;
0683     int i;
0684     int rc;
0685 
0686     sclp_vt220_init_count++;
0687     if (sclp_vt220_init_count != 1)
0688         return 0;
0689     timer_setup(&sclp_vt220_timer, sclp_vt220_timeout, 0);
0690     tty_port_init(&sclp_vt220_port);
0691     sclp_vt220_current_request = NULL;
0692     sclp_vt220_buffered_chars = 0;
0693     sclp_vt220_flush_later = 0;
0694 
0695     /* Allocate pages for output buffering */
0696     rc = -ENOMEM;
0697     for (i = 0; i < num_pages; i++) {
0698         page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
0699         if (!page)
0700             goto out;
0701         list_add_tail(page, &sclp_vt220_empty);
0702     }
0703     rc = sclp_register(&sclp_vt220_register);
0704 out:
0705     if (rc) {
0706         __sclp_vt220_free_pages();
0707         sclp_vt220_init_count--;
0708         tty_port_destroy(&sclp_vt220_port);
0709     }
0710     return rc;
0711 }
0712 
0713 static const struct tty_operations sclp_vt220_ops = {
0714     .open = sclp_vt220_open,
0715     .close = sclp_vt220_close,
0716     .write = sclp_vt220_write,
0717     .put_char = sclp_vt220_put_char,
0718     .flush_chars = sclp_vt220_flush_chars,
0719     .write_room = sclp_vt220_write_room,
0720     .chars_in_buffer = sclp_vt220_chars_in_buffer,
0721     .flush_buffer = sclp_vt220_flush_buffer,
0722 };
0723 
0724 /*
0725  * Register driver with SCLP and Linux and initialize internal tty structures.
0726  */
0727 static int __init sclp_vt220_tty_init(void)
0728 {
0729     struct tty_driver *driver;
0730     int rc;
0731 
0732     /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
0733      * symmetry between VM and LPAR systems regarding ttyS1. */
0734     driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
0735     if (IS_ERR(driver))
0736         return PTR_ERR(driver);
0737     rc = __sclp_vt220_init(MAX_KMEM_PAGES);
0738     if (rc)
0739         goto out_driver;
0740 
0741     driver->driver_name = SCLP_VT220_DRIVER_NAME;
0742     driver->name = SCLP_VT220_DEVICE_NAME;
0743     driver->major = SCLP_VT220_MAJOR;
0744     driver->minor_start = SCLP_VT220_MINOR;
0745     driver->type = TTY_DRIVER_TYPE_SYSTEM;
0746     driver->subtype = SYSTEM_TYPE_TTY;
0747     driver->init_termios = tty_std_termios;
0748     tty_set_operations(driver, &sclp_vt220_ops);
0749     tty_port_link_device(&sclp_vt220_port, driver, 0);
0750 
0751     rc = tty_register_driver(driver);
0752     if (rc)
0753         goto out_init;
0754     rc = sclp_register(&sclp_vt220_register_input);
0755     if (rc)
0756         goto out_reg;
0757     sclp_vt220_driver = driver;
0758     return 0;
0759 
0760 out_reg:
0761     tty_unregister_driver(driver);
0762 out_init:
0763     __sclp_vt220_cleanup();
0764 out_driver:
0765     tty_driver_kref_put(driver);
0766     return rc;
0767 }
0768 __initcall(sclp_vt220_tty_init);
0769 
0770 #ifdef CONFIG_SCLP_VT220_CONSOLE
0771 
0772 static void
0773 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
0774 {
0775     __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
0776 }
0777 
0778 static struct tty_driver *
0779 sclp_vt220_con_device(struct console *c, int *index)
0780 {
0781     *index = 0;
0782     return sclp_vt220_driver;
0783 }
0784 
0785 /*
0786  * This panic/reboot notifier runs in atomic context, so
0787  * locking restrictions apply to prevent potential lockups.
0788  */
0789 static int
0790 sclp_vt220_notify(struct notifier_block *self,
0791               unsigned long event, void *data)
0792 {
0793     unsigned long flags;
0794 
0795     if (spin_is_locked(&sclp_vt220_lock))
0796         return NOTIFY_DONE;
0797 
0798     sclp_vt220_emit_current();
0799 
0800     spin_lock_irqsave(&sclp_vt220_lock, flags);
0801     del_timer(&sclp_vt220_timer);
0802     while (sclp_vt220_queue_running) {
0803         spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0804         sclp_sync_wait();
0805         spin_lock_irqsave(&sclp_vt220_lock, flags);
0806     }
0807     spin_unlock_irqrestore(&sclp_vt220_lock, flags);
0808 
0809     return NOTIFY_DONE;
0810 }
0811 
0812 static struct notifier_block on_panic_nb = {
0813     .notifier_call = sclp_vt220_notify,
0814     .priority = INT_MIN + 1, /* run the callback late */
0815 };
0816 
0817 static struct notifier_block on_reboot_nb = {
0818     .notifier_call = sclp_vt220_notify,
0819     .priority = INT_MIN + 1, /* run the callback late */
0820 };
0821 
0822 /* Structure needed to register with printk */
0823 static struct console sclp_vt220_console =
0824 {
0825     .name = SCLP_VT220_CONSOLE_NAME,
0826     .write = sclp_vt220_con_write,
0827     .device = sclp_vt220_con_device,
0828     .flags = CON_PRINTBUFFER,
0829     .index = SCLP_VT220_CONSOLE_INDEX
0830 };
0831 
0832 static int __init
0833 sclp_vt220_con_init(void)
0834 {
0835     int rc;
0836 
0837     rc = __sclp_vt220_init(sclp_console_pages);
0838     if (rc)
0839         return rc;
0840     /* Attach linux console */
0841     atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
0842     register_reboot_notifier(&on_reboot_nb);
0843     register_console(&sclp_vt220_console);
0844     return 0;
0845 }
0846 
0847 console_initcall(sclp_vt220_con_init);
0848 #endif /* CONFIG_SCLP_VT220_CONSOLE */
0849