linux/drivers/s390/char/sclp_vt220.c
<<
>>
Prefs
   1/*
   2 * SCLP VT220 terminal driver.
   3 *
   4 * Copyright IBM Corp. 2003, 2009
   5 *
   6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/spinlock.h>
  11#include <linux/list.h>
  12#include <linux/wait.h>
  13#include <linux/timer.h>
  14#include <linux/kernel.h>
  15#include <linux/tty.h>
  16#include <linux/tty_driver.h>
  17#include <linux/tty_flip.h>
  18#include <linux/errno.h>
  19#include <linux/mm.h>
  20#include <linux/major.h>
  21#include <linux/console.h>
  22#include <linux/kdev_t.h>
  23#include <linux/interrupt.h>
  24#include <linux/init.h>
  25#include <linux/reboot.h>
  26#include <linux/slab.h>
  27
  28#include <asm/uaccess.h>
  29#include "sclp.h"
  30
  31#define SCLP_VT220_MAJOR                TTY_MAJOR
  32#define SCLP_VT220_MINOR                65
  33#define SCLP_VT220_DRIVER_NAME          "sclp_vt220"
  34#define SCLP_VT220_DEVICE_NAME          "ttysclp"
  35#define SCLP_VT220_CONSOLE_NAME         "ttyS"
  36#define SCLP_VT220_CONSOLE_INDEX        1       /* console=ttyS1 */
  37#define SCLP_VT220_BUF_SIZE             80
  38
  39/* Representation of a single write request */
  40struct sclp_vt220_request {
  41        struct list_head list;
  42        struct sclp_req sclp_req;
  43        int retry_count;
  44};
  45
  46/* VT220 SCCB */
  47struct sclp_vt220_sccb {
  48        struct sccb_header header;
  49        struct evbuf_header evbuf;
  50};
  51
  52#define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
  53                                         sizeof(struct sclp_vt220_request) - \
  54                                         sizeof(struct sclp_vt220_sccb))
  55
  56/* Structures and data needed to register tty driver */
  57static struct tty_driver *sclp_vt220_driver;
  58
  59/* The tty_struct that the kernel associated with us */
  60static struct tty_struct *sclp_vt220_tty;
  61
  62/* Lock to protect internal data from concurrent access */
  63static spinlock_t sclp_vt220_lock;
  64
  65/* List of empty pages to be used as write request buffers */
  66static struct list_head sclp_vt220_empty;
  67
  68/* List of pending requests */
  69static struct list_head sclp_vt220_outqueue;
  70
  71/* Suspend mode flag */
  72static int sclp_vt220_suspended;
  73
  74/* Flag that output queue is currently running */
  75static int sclp_vt220_queue_running;
  76
  77/* Timer used for delaying write requests to merge subsequent messages into
  78 * a single buffer */
  79static struct timer_list sclp_vt220_timer;
  80
  81/* Pointer to current request buffer which has been partially filled but not
  82 * yet sent */
  83static struct sclp_vt220_request *sclp_vt220_current_request;
  84
  85/* Number of characters in current request buffer */
  86static int sclp_vt220_buffered_chars;
  87
  88/* Counter controlling core driver initialization. */
  89static int __initdata sclp_vt220_init_count;
  90
  91/* Flag indicating that sclp_vt220_current_request should really
  92 * have been already queued but wasn't because the SCLP was processing
  93 * another buffer */
  94static int sclp_vt220_flush_later;
  95
  96static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
  97static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
  98                                   enum sclp_pm_event sclp_pm_event);
  99static int __sclp_vt220_emit(struct sclp_vt220_request *request);
 100static void sclp_vt220_emit_current(void);
 101
 102/* Registration structure for our interest in SCLP event buffers */
 103static struct sclp_register sclp_vt220_register = {
 104        .send_mask              = EVTYP_VT220MSG_MASK,
 105        .receive_mask           = EVTYP_VT220MSG_MASK,
 106        .state_change_fn        = NULL,
 107        .receiver_fn            = sclp_vt220_receiver_fn,
 108        .pm_event_fn            = sclp_vt220_pm_event_fn,
 109};
 110
 111
 112/*
 113 * Put provided request buffer back into queue and check emit pending
 114 * buffers if necessary.
 115 */
 116static void
 117sclp_vt220_process_queue(struct sclp_vt220_request *request)
 118{
 119        unsigned long flags;
 120        void *page;
 121
 122        do {
 123                /* Put buffer back to list of empty buffers */
 124                page = request->sclp_req.sccb;
 125                spin_lock_irqsave(&sclp_vt220_lock, flags);
 126                /* Move request from outqueue to empty queue */
 127                list_del(&request->list);
 128                list_add_tail((struct list_head *) page, &sclp_vt220_empty);
 129                /* Check if there is a pending buffer on the out queue. */
 130                request = NULL;
 131                if (!list_empty(&sclp_vt220_outqueue))
 132                        request = list_entry(sclp_vt220_outqueue.next,
 133                                             struct sclp_vt220_request, list);
 134                if (!request || sclp_vt220_suspended) {
 135                        sclp_vt220_queue_running = 0;
 136                        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 137                        break;
 138                }
 139                spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 140        } while (__sclp_vt220_emit(request));
 141        if (request == NULL && sclp_vt220_flush_later)
 142                sclp_vt220_emit_current();
 143        /* Check if the tty needs a wake up call */
 144        if (sclp_vt220_tty != NULL) {
 145                tty_wakeup(sclp_vt220_tty);
 146        }
 147}
 148
 149#define SCLP_BUFFER_MAX_RETRY           1
 150
 151/*
 152 * Callback through which the result of a write request is reported by the
 153 * SCLP.
 154 */
 155static void
 156sclp_vt220_callback(struct sclp_req *request, void *data)
 157{
 158        struct sclp_vt220_request *vt220_request;
 159        struct sclp_vt220_sccb *sccb;
 160
 161        vt220_request = (struct sclp_vt220_request *) data;
 162        if (request->status == SCLP_REQ_FAILED) {
 163                sclp_vt220_process_queue(vt220_request);
 164                return;
 165        }
 166        sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
 167
 168        /* Check SCLP response code and choose suitable action  */
 169        switch (sccb->header.response_code) {
 170        case 0x0020 :
 171                break;
 172
 173        case 0x05f0: /* Target resource in improper state */
 174                break;
 175
 176        case 0x0340: /* Contained SCLP equipment check */
 177                if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
 178                        break;
 179                /* Remove processed buffers and requeue rest */
 180                if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
 181                        /* Not all buffers were processed */
 182                        sccb->header.response_code = 0x0000;
 183                        vt220_request->sclp_req.status = SCLP_REQ_FILLED;
 184                        if (sclp_add_request(request) == 0)
 185                                return;
 186                }
 187                break;
 188
 189        case 0x0040: /* SCLP equipment check */
 190                if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
 191                        break;
 192                sccb->header.response_code = 0x0000;
 193                vt220_request->sclp_req.status = SCLP_REQ_FILLED;
 194                if (sclp_add_request(request) == 0)
 195                        return;
 196                break;
 197
 198        default:
 199                break;
 200        }
 201        sclp_vt220_process_queue(vt220_request);
 202}
 203
 204/*
 205 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
 206 * otherwise.
 207 */
 208static int
 209__sclp_vt220_emit(struct sclp_vt220_request *request)
 210{
 211        if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
 212                request->sclp_req.status = SCLP_REQ_FAILED;
 213                return -EIO;
 214        }
 215        request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
 216        request->sclp_req.status = SCLP_REQ_FILLED;
 217        request->sclp_req.callback = sclp_vt220_callback;
 218        request->sclp_req.callback_data = (void *) request;
 219
 220        return sclp_add_request(&request->sclp_req);
 221}
 222
 223/*
 224 * Queue and emit current request.
 225 */
 226static void
 227sclp_vt220_emit_current(void)
 228{
 229        unsigned long flags;
 230        struct sclp_vt220_request *request;
 231        struct sclp_vt220_sccb *sccb;
 232
 233        spin_lock_irqsave(&sclp_vt220_lock, flags);
 234        if (sclp_vt220_current_request) {
 235                sccb = (struct sclp_vt220_sccb *) 
 236                                sclp_vt220_current_request->sclp_req.sccb;
 237                /* Only emit buffers with content */
 238                if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
 239                        list_add_tail(&sclp_vt220_current_request->list,
 240                                      &sclp_vt220_outqueue);
 241                        sclp_vt220_current_request = NULL;
 242                        if (timer_pending(&sclp_vt220_timer))
 243                                del_timer(&sclp_vt220_timer);
 244                }
 245                sclp_vt220_flush_later = 0;
 246        }
 247        if (sclp_vt220_queue_running || sclp_vt220_suspended)
 248                goto out_unlock;
 249        if (list_empty(&sclp_vt220_outqueue))
 250                goto out_unlock;
 251        request = list_first_entry(&sclp_vt220_outqueue,
 252                                   struct sclp_vt220_request, list);
 253        sclp_vt220_queue_running = 1;
 254        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 255
 256        if (__sclp_vt220_emit(request))
 257                sclp_vt220_process_queue(request);
 258        return;
 259out_unlock:
 260        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 261}
 262
 263#define SCLP_NORMAL_WRITE       0x00
 264
 265/*
 266 * Helper function to initialize a page with the sclp request structure.
 267 */
 268static struct sclp_vt220_request *
 269sclp_vt220_initialize_page(void *page)
 270{
 271        struct sclp_vt220_request *request;
 272        struct sclp_vt220_sccb *sccb;
 273
 274        /* Place request structure at end of page */
 275        request = ((struct sclp_vt220_request *)
 276                        ((addr_t) page + PAGE_SIZE)) - 1;
 277        request->retry_count = 0;
 278        request->sclp_req.sccb = page;
 279        /* SCCB goes at start of page */
 280        sccb = (struct sclp_vt220_sccb *) page;
 281        memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
 282        sccb->header.length = sizeof(struct sclp_vt220_sccb);
 283        sccb->header.function_code = SCLP_NORMAL_WRITE;
 284        sccb->header.response_code = 0x0000;
 285        sccb->evbuf.type = EVTYP_VT220MSG;
 286        sccb->evbuf.length = sizeof(struct evbuf_header);
 287
 288        return request;
 289}
 290
 291static inline unsigned int
 292sclp_vt220_space_left(struct sclp_vt220_request *request)
 293{
 294        struct sclp_vt220_sccb *sccb;
 295        sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
 296        return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
 297               sccb->header.length;
 298}
 299
 300static inline unsigned int
 301sclp_vt220_chars_stored(struct sclp_vt220_request *request)
 302{
 303        struct sclp_vt220_sccb *sccb;
 304        sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
 305        return sccb->evbuf.length - sizeof(struct evbuf_header);
 306}
 307
 308/*
 309 * Add msg to buffer associated with request. Return the number of characters
 310 * added.
 311 */
 312static int
 313sclp_vt220_add_msg(struct sclp_vt220_request *request,
 314                   const unsigned char *msg, int count, int convertlf)
 315{
 316        struct sclp_vt220_sccb *sccb;
 317        void *buffer;
 318        unsigned char c;
 319        int from;
 320        int to;
 321
 322        if (count > sclp_vt220_space_left(request))
 323                count = sclp_vt220_space_left(request);
 324        if (count <= 0)
 325                return 0;
 326
 327        sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
 328        buffer = (void *) ((addr_t) sccb + sccb->header.length);
 329
 330        if (convertlf) {
 331                /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
 332                for (from=0, to=0;
 333                     (from < count) && (to < sclp_vt220_space_left(request));
 334                     from++) {
 335                        /* Retrieve character */
 336                        c = msg[from];
 337                        /* Perform conversion */
 338                        if (c == 0x0a) {
 339                                if (to + 1 < sclp_vt220_space_left(request)) {
 340                                        ((unsigned char *) buffer)[to++] = c;
 341                                        ((unsigned char *) buffer)[to++] = 0x0d;
 342                                } else
 343                                        break;
 344
 345                        } else
 346                                ((unsigned char *) buffer)[to++] = c;
 347                }
 348                sccb->header.length += to;
 349                sccb->evbuf.length += to;
 350                return from;
 351        } else {
 352                memcpy(buffer, (const void *) msg, count);
 353                sccb->header.length += count;
 354                sccb->evbuf.length += count;
 355                return count;
 356        }
 357}
 358
 359/*
 360 * Emit buffer after having waited long enough for more data to arrive.
 361 */
 362static void
 363sclp_vt220_timeout(unsigned long data)
 364{
 365        sclp_vt220_emit_current();
 366}
 367
 368#define BUFFER_MAX_DELAY        HZ/20
 369
 370/* 
 371 * Internal implementation of the write function. Write COUNT bytes of data
 372 * from memory at BUF
 373 * to the SCLP interface. In case that the data does not fit into the current
 374 * write buffer, emit the current one and allocate a new one. If there are no
 375 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
 376 * is non-zero, the buffer will be scheduled for emitting after a timeout -
 377 * otherwise the user has to explicitly call the flush function.
 378 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
 379 * buffer should be converted to 0x0a 0x0d. After completion, return the number
 380 * of bytes written.
 381 */
 382static int
 383__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
 384                   int convertlf, int may_fail)
 385{
 386        unsigned long flags;
 387        void *page;
 388        int written;
 389        int overall_written;
 390
 391        if (count <= 0)
 392                return 0;
 393        overall_written = 0;
 394        spin_lock_irqsave(&sclp_vt220_lock, flags);
 395        do {
 396                /* Create an sclp output buffer if none exists yet */
 397                if (sclp_vt220_current_request == NULL) {
 398                        while (list_empty(&sclp_vt220_empty)) {
 399                                spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 400                                if (may_fail || sclp_vt220_suspended)
 401                                        goto out;
 402                                else
 403                                        sclp_sync_wait();
 404                                spin_lock_irqsave(&sclp_vt220_lock, flags);
 405                        }
 406                        page = (void *) sclp_vt220_empty.next;
 407                        list_del((struct list_head *) page);
 408                        sclp_vt220_current_request =
 409                                sclp_vt220_initialize_page(page);
 410                }
 411                /* Try to write the string to the current request buffer */
 412                written = sclp_vt220_add_msg(sclp_vt220_current_request,
 413                                             buf, count, convertlf);
 414                overall_written += written;
 415                if (written == count)
 416                        break;
 417                /*
 418                 * Not all characters could be written to the current
 419                 * output buffer. Emit the buffer, create a new buffer
 420                 * and then output the rest of the string.
 421                 */
 422                spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 423                sclp_vt220_emit_current();
 424                spin_lock_irqsave(&sclp_vt220_lock, flags);
 425                buf += written;
 426                count -= written;
 427        } while (count > 0);
 428        /* Setup timer to output current console buffer after some time */
 429        if (sclp_vt220_current_request != NULL &&
 430            !timer_pending(&sclp_vt220_timer) && do_schedule) {
 431                sclp_vt220_timer.function = sclp_vt220_timeout;
 432                sclp_vt220_timer.data = 0UL;
 433                sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
 434                add_timer(&sclp_vt220_timer);
 435        }
 436        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 437out:
 438        return overall_written;
 439}
 440
 441/*
 442 * This routine is called by the kernel to write a series of
 443 * characters to the tty device.  The characters may come from
 444 * user space or kernel space.  This routine will return the
 445 * number of characters actually accepted for writing.
 446 */
 447static int
 448sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
 449{
 450        return __sclp_vt220_write(buf, count, 1, 0, 1);
 451}
 452
 453#define SCLP_VT220_SESSION_ENDED        0x01
 454#define SCLP_VT220_SESSION_STARTED      0x80
 455#define SCLP_VT220_SESSION_DATA         0x00
 456
 457/*
 458 * Called by the SCLP to report incoming event buffers.
 459 */
 460static void
 461sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
 462{
 463        char *buffer;
 464        unsigned int count;
 465
 466        /* Ignore input if device is not open */
 467        if (sclp_vt220_tty == NULL)
 468                return;
 469
 470        buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
 471        count = evbuf->length - sizeof(struct evbuf_header);
 472
 473        switch (*buffer) {
 474        case SCLP_VT220_SESSION_ENDED:
 475        case SCLP_VT220_SESSION_STARTED:
 476                break;
 477        case SCLP_VT220_SESSION_DATA:
 478                /* Send input to line discipline */
 479                buffer++;
 480                count--;
 481                tty_insert_flip_string(sclp_vt220_tty, buffer, count);
 482                tty_flip_buffer_push(sclp_vt220_tty);
 483                break;
 484        }
 485}
 486
 487/*
 488 * This routine is called when a particular tty device is opened.
 489 */
 490static int
 491sclp_vt220_open(struct tty_struct *tty, struct file *filp)
 492{
 493        if (tty->count == 1) {
 494                sclp_vt220_tty = tty;
 495                tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
 496                if (tty->driver_data == NULL)
 497                        return -ENOMEM;
 498                tty->low_latency = 0;
 499                if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
 500                        tty->winsize.ws_row = 24;
 501                        tty->winsize.ws_col = 80;
 502                }
 503        }
 504        return 0;
 505}
 506
 507/*
 508 * This routine is called when a particular tty device is closed.
 509 */
 510static void
 511sclp_vt220_close(struct tty_struct *tty, struct file *filp)
 512{
 513        if (tty->count == 1) {
 514                sclp_vt220_tty = NULL;
 515                kfree(tty->driver_data);
 516                tty->driver_data = NULL;
 517        }
 518}
 519
 520/*
 521 * This routine is called by the kernel to write a single
 522 * character to the tty device.  If the kernel uses this routine,
 523 * it must call the flush_chars() routine (if defined) when it is
 524 * done stuffing characters into the driver.
 525 */
 526static int
 527sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
 528{
 529        return __sclp_vt220_write(&ch, 1, 0, 0, 1);
 530}
 531
 532/*
 533 * This routine is called by the kernel after it has written a
 534 * series of characters to the tty device using put_char().  
 535 */
 536static void
 537sclp_vt220_flush_chars(struct tty_struct *tty)
 538{
 539        if (!sclp_vt220_queue_running)
 540                sclp_vt220_emit_current();
 541        else
 542                sclp_vt220_flush_later = 1;
 543}
 544
 545/*
 546 * This routine returns the numbers of characters the tty driver
 547 * will accept for queuing to be written.  This number is subject
 548 * to change as output buffers get emptied, or if the output flow
 549 * control is acted.
 550 */
 551static int
 552sclp_vt220_write_room(struct tty_struct *tty)
 553{
 554        unsigned long flags;
 555        struct list_head *l;
 556        int count;
 557
 558        spin_lock_irqsave(&sclp_vt220_lock, flags);
 559        count = 0;
 560        if (sclp_vt220_current_request != NULL)
 561                count = sclp_vt220_space_left(sclp_vt220_current_request);
 562        list_for_each(l, &sclp_vt220_empty)
 563                count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
 564        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 565        return count;
 566}
 567
 568/*
 569 * Return number of buffered chars.
 570 */
 571static int
 572sclp_vt220_chars_in_buffer(struct tty_struct *tty)
 573{
 574        unsigned long flags;
 575        struct list_head *l;
 576        struct sclp_vt220_request *r;
 577        int count;
 578
 579        spin_lock_irqsave(&sclp_vt220_lock, flags);
 580        count = 0;
 581        if (sclp_vt220_current_request != NULL)
 582                count = sclp_vt220_chars_stored(sclp_vt220_current_request);
 583        list_for_each(l, &sclp_vt220_outqueue) {
 584                r = list_entry(l, struct sclp_vt220_request, list);
 585                count += sclp_vt220_chars_stored(r);
 586        }
 587        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 588        return count;
 589}
 590
 591/*
 592 * Pass on all buffers to the hardware. Return only when there are no more
 593 * buffers pending.
 594 */
 595static void
 596sclp_vt220_flush_buffer(struct tty_struct *tty)
 597{
 598        sclp_vt220_emit_current();
 599}
 600
 601/* Release allocated pages. */
 602static void __init __sclp_vt220_free_pages(void)
 603{
 604        struct list_head *page, *p;
 605
 606        list_for_each_safe(page, p, &sclp_vt220_empty) {
 607                list_del(page);
 608                free_page((unsigned long) page);
 609        }
 610}
 611
 612/* Release memory and unregister from sclp core. Controlled by init counting -
 613 * only the last invoker will actually perform these actions. */
 614static void __init __sclp_vt220_cleanup(void)
 615{
 616        sclp_vt220_init_count--;
 617        if (sclp_vt220_init_count != 0)
 618                return;
 619        sclp_unregister(&sclp_vt220_register);
 620        __sclp_vt220_free_pages();
 621}
 622
 623/* Allocate buffer pages and register with sclp core. Controlled by init
 624 * counting - only the first invoker will actually perform these actions. */
 625static int __init __sclp_vt220_init(int num_pages)
 626{
 627        void *page;
 628        int i;
 629        int rc;
 630
 631        sclp_vt220_init_count++;
 632        if (sclp_vt220_init_count != 1)
 633                return 0;
 634        spin_lock_init(&sclp_vt220_lock);
 635        INIT_LIST_HEAD(&sclp_vt220_empty);
 636        INIT_LIST_HEAD(&sclp_vt220_outqueue);
 637        init_timer(&sclp_vt220_timer);
 638        sclp_vt220_current_request = NULL;
 639        sclp_vt220_buffered_chars = 0;
 640        sclp_vt220_tty = NULL;
 641        sclp_vt220_flush_later = 0;
 642
 643        /* Allocate pages for output buffering */
 644        rc = -ENOMEM;
 645        for (i = 0; i < num_pages; i++) {
 646                page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
 647                if (!page)
 648                        goto out;
 649                list_add_tail(page, &sclp_vt220_empty);
 650        }
 651        rc = sclp_register(&sclp_vt220_register);
 652out:
 653        if (rc) {
 654                __sclp_vt220_free_pages();
 655                sclp_vt220_init_count--;
 656        }
 657        return rc;
 658}
 659
 660static const struct tty_operations sclp_vt220_ops = {
 661        .open = sclp_vt220_open,
 662        .close = sclp_vt220_close,
 663        .write = sclp_vt220_write,
 664        .put_char = sclp_vt220_put_char,
 665        .flush_chars = sclp_vt220_flush_chars,
 666        .write_room = sclp_vt220_write_room,
 667        .chars_in_buffer = sclp_vt220_chars_in_buffer,
 668        .flush_buffer = sclp_vt220_flush_buffer,
 669};
 670
 671/*
 672 * Register driver with SCLP and Linux and initialize internal tty structures.
 673 */
 674static int __init sclp_vt220_tty_init(void)
 675{
 676        struct tty_driver *driver;
 677        int rc;
 678
 679        /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
 680         * symmetry between VM and LPAR systems regarding ttyS1. */
 681        driver = alloc_tty_driver(1);
 682        if (!driver)
 683                return -ENOMEM;
 684        rc = __sclp_vt220_init(MAX_KMEM_PAGES);
 685        if (rc)
 686                goto out_driver;
 687
 688        driver->owner = THIS_MODULE;
 689        driver->driver_name = SCLP_VT220_DRIVER_NAME;
 690        driver->name = SCLP_VT220_DEVICE_NAME;
 691        driver->major = SCLP_VT220_MAJOR;
 692        driver->minor_start = SCLP_VT220_MINOR;
 693        driver->type = TTY_DRIVER_TYPE_SYSTEM;
 694        driver->subtype = SYSTEM_TYPE_TTY;
 695        driver->init_termios = tty_std_termios;
 696        driver->flags = TTY_DRIVER_REAL_RAW;
 697        tty_set_operations(driver, &sclp_vt220_ops);
 698
 699        rc = tty_register_driver(driver);
 700        if (rc)
 701                goto out_init;
 702        sclp_vt220_driver = driver;
 703        return 0;
 704
 705out_init:
 706        __sclp_vt220_cleanup();
 707out_driver:
 708        put_tty_driver(driver);
 709        return rc;
 710}
 711__initcall(sclp_vt220_tty_init);
 712
 713static void __sclp_vt220_flush_buffer(void)
 714{
 715        unsigned long flags;
 716
 717        sclp_vt220_emit_current();
 718        spin_lock_irqsave(&sclp_vt220_lock, flags);
 719        if (timer_pending(&sclp_vt220_timer))
 720                del_timer(&sclp_vt220_timer);
 721        while (sclp_vt220_queue_running) {
 722                spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 723                sclp_sync_wait();
 724                spin_lock_irqsave(&sclp_vt220_lock, flags);
 725        }
 726        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 727}
 728
 729/*
 730 * Resume console: If there are cached messages, emit them.
 731 */
 732static void sclp_vt220_resume(void)
 733{
 734        unsigned long flags;
 735
 736        spin_lock_irqsave(&sclp_vt220_lock, flags);
 737        sclp_vt220_suspended = 0;
 738        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 739        sclp_vt220_emit_current();
 740}
 741
 742/*
 743 * Suspend console: Set suspend flag and flush console
 744 */
 745static void sclp_vt220_suspend(void)
 746{
 747        unsigned long flags;
 748
 749        spin_lock_irqsave(&sclp_vt220_lock, flags);
 750        sclp_vt220_suspended = 1;
 751        spin_unlock_irqrestore(&sclp_vt220_lock, flags);
 752        __sclp_vt220_flush_buffer();
 753}
 754
 755static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
 756                                   enum sclp_pm_event sclp_pm_event)
 757{
 758        switch (sclp_pm_event) {
 759        case SCLP_PM_EVENT_FREEZE:
 760                sclp_vt220_suspend();
 761                break;
 762        case SCLP_PM_EVENT_RESTORE:
 763        case SCLP_PM_EVENT_THAW:
 764                sclp_vt220_resume();
 765                break;
 766        }
 767}
 768
 769#ifdef CONFIG_SCLP_VT220_CONSOLE
 770
 771static void
 772sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
 773{
 774        __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
 775}
 776
 777static struct tty_driver *
 778sclp_vt220_con_device(struct console *c, int *index)
 779{
 780        *index = 0;
 781        return sclp_vt220_driver;
 782}
 783
 784static int
 785sclp_vt220_notify(struct notifier_block *self,
 786                          unsigned long event, void *data)
 787{
 788        __sclp_vt220_flush_buffer();
 789        return NOTIFY_OK;
 790}
 791
 792static struct notifier_block on_panic_nb = {
 793        .notifier_call = sclp_vt220_notify,
 794        .priority = 1,
 795};
 796
 797static struct notifier_block on_reboot_nb = {
 798        .notifier_call = sclp_vt220_notify,
 799        .priority = 1,
 800};
 801
 802/* Structure needed to register with printk */
 803static struct console sclp_vt220_console =
 804{
 805        .name = SCLP_VT220_CONSOLE_NAME,
 806        .write = sclp_vt220_con_write,
 807        .device = sclp_vt220_con_device,
 808        .flags = CON_PRINTBUFFER,
 809        .index = SCLP_VT220_CONSOLE_INDEX
 810};
 811
 812static int __init
 813sclp_vt220_con_init(void)
 814{
 815        int rc;
 816
 817        if (!CONSOLE_IS_SCLP)
 818                return 0;
 819        rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
 820        if (rc)
 821                return rc;
 822        /* Attach linux console */
 823        atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
 824        register_reboot_notifier(&on_reboot_nb);
 825        register_console(&sclp_vt220_console);
 826        return 0;
 827}
 828
 829console_initcall(sclp_vt220_con_init);
 830#endif /* CONFIG_SCLP_VT220_CONSOLE */
 831
 832