linux/drivers/s390/char/con3270.c
<<
>>
Prefs
   1/*
   2 *  drivers/s390/char/con3270.c
   3 *    IBM/3270 Driver - console view.
   4 *
   5 *  Author(s):
   6 *    Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
   7 *    Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
   8 *      -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
   9 */
  10
  11#include <linux/bootmem.h>
  12#include <linux/console.h>
  13#include <linux/init.h>
  14#include <linux/interrupt.h>
  15#include <linux/list.h>
  16#include <linux/types.h>
  17#include <linux/err.h>
  18
  19#include <asm/ccwdev.h>
  20#include <asm/cio.h>
  21#include <asm/cpcmd.h>
  22#include <asm/ebcdic.h>
  23
  24#include "raw3270.h"
  25#include "tty3270.h"
  26#include "ctrlchar.h"
  27
  28#define CON3270_OUTPUT_BUFFER_SIZE 1024
  29#define CON3270_STRING_PAGES 4
  30
  31static struct raw3270_fn con3270_fn;
  32
  33/*
  34 * Main 3270 console view data structure.
  35 */
  36struct con3270 {
  37        struct raw3270_view view;
  38        spinlock_t lock;
  39        struct list_head freemem;       /* list of free memory for strings. */
  40
  41        /* Output stuff. */
  42        struct list_head lines;         /* list of lines. */
  43        struct list_head update;        /* list of lines to update. */
  44        int line_nr;                    /* line number for next update. */
  45        int nr_lines;                   /* # lines in list. */
  46        int nr_up;                      /* # lines up in history. */
  47        unsigned long update_flags;     /* Update indication bits. */
  48        struct string *cline;           /* current output line. */
  49        struct string *status;          /* last line of display. */
  50        struct raw3270_request *write;  /* single write request. */
  51        struct timer_list timer;
  52
  53        /* Input stuff. */
  54        struct string *input;           /* input string for read request. */
  55        struct raw3270_request *read;   /* single read request. */
  56        struct raw3270_request *kreset; /* single keyboard reset request. */
  57        struct tasklet_struct readlet;  /* tasklet to issue read request. */
  58};
  59
  60static struct con3270 *condev;
  61
  62/* con3270->update_flags. See con3270_update for details. */
  63#define CON_UPDATE_ERASE        1       /* Use EWRITEA instead of WRITE. */
  64#define CON_UPDATE_LIST         2       /* Update lines in tty3270->update. */
  65#define CON_UPDATE_STATUS       4       /* Update status line. */
  66#define CON_UPDATE_ALL          7
  67
  68static void con3270_update(struct con3270 *);
  69
  70/*
  71 * Setup timeout for a device. On timeout trigger an update.
  72 */
  73static void con3270_set_timer(struct con3270 *cp, int expires)
  74{
  75        if (expires == 0) {
  76                if (timer_pending(&cp->timer))
  77                        del_timer(&cp->timer);
  78                return;
  79        }
  80        if (timer_pending(&cp->timer) &&
  81            mod_timer(&cp->timer, jiffies + expires))
  82                return;
  83        cp->timer.function = (void (*)(unsigned long)) con3270_update;
  84        cp->timer.data = (unsigned long) cp;
  85        cp->timer.expires = jiffies + expires;
  86        add_timer(&cp->timer);
  87}
  88
  89/*
  90 * The status line is the last line of the screen. It shows the string
  91 * "console view" in the lower left corner and "Running"/"More..."/"Holding"
  92 * in the lower right corner of the screen.
  93 */
  94static void
  95con3270_update_status(struct con3270 *cp)
  96{
  97        char *str;
  98
  99        str = (cp->nr_up != 0) ? "History" : "Running";
 100        memcpy(cp->status->string + 24, str, 7);
 101        codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
 102        cp->update_flags |= CON_UPDATE_STATUS;
 103}
 104
 105static void
 106con3270_create_status(struct con3270 *cp)
 107{
 108        static const unsigned char blueprint[] =
 109                { TO_SBA, 0, 0, TO_SF,TF_LOG,TO_SA,TAT_COLOR, TAC_GREEN,
 110                  'c','o','n','s','o','l','e',' ','v','i','e','w',
 111                  TO_RA,0,0,0,'R','u','n','n','i','n','g',TO_SF,TF_LOG };
 112
 113        cp->status = alloc_string(&cp->freemem, sizeof(blueprint));
 114        /* Copy blueprint to status line */
 115        memcpy(cp->status->string, blueprint, sizeof(blueprint));
 116        /* Set TO_RA addresses. */
 117        raw3270_buffer_address(cp->view.dev, cp->status->string + 1,
 118                               cp->view.cols * (cp->view.rows - 1));
 119        raw3270_buffer_address(cp->view.dev, cp->status->string + 21,
 120                               cp->view.cols * cp->view.rows - 8);
 121        /* Convert strings to ebcdic. */
 122        codepage_convert(cp->view.ascebc, cp->status->string + 8, 12);
 123        codepage_convert(cp->view.ascebc, cp->status->string + 24, 7);
 124}
 125
 126/*
 127 * Set output offsets to 3270 datastream fragment of a console string.
 128 */
 129static void
 130con3270_update_string(struct con3270 *cp, struct string *s, int nr)
 131{
 132        if (s->len >= cp->view.cols - 5)
 133                return;
 134        raw3270_buffer_address(cp->view.dev, s->string + s->len - 3,
 135                               cp->view.cols * (nr + 1));
 136}
 137
 138/*
 139 * Rebuild update list to print all lines.
 140 */
 141static void
 142con3270_rebuild_update(struct con3270 *cp)
 143{
 144        struct string *s, *n;
 145        int nr;
 146
 147        /* 
 148         * Throw away update list and create a new one,
 149         * containing all lines that will fit on the screen.
 150         */
 151        list_for_each_entry_safe(s, n, &cp->update, update)
 152                list_del_init(&s->update);
 153        nr = cp->view.rows - 2 + cp->nr_up;
 154        list_for_each_entry_reverse(s, &cp->lines, list) {
 155                if (nr < cp->view.rows - 1)
 156                        list_add(&s->update, &cp->update);
 157                if (--nr < 0)
 158                        break;
 159        }
 160        cp->line_nr = 0;
 161        cp->update_flags |= CON_UPDATE_LIST;
 162}
 163
 164/*
 165 * Alloc string for size bytes. Free strings from history if necessary.
 166 */
 167static struct string *
 168con3270_alloc_string(struct con3270 *cp, size_t size)
 169{
 170        struct string *s, *n;
 171
 172        s = alloc_string(&cp->freemem, size);
 173        if (s)
 174                return s;
 175        list_for_each_entry_safe(s, n, &cp->lines, list) {
 176                list_del(&s->list);
 177                if (!list_empty(&s->update))
 178                        list_del(&s->update);
 179                cp->nr_lines--;
 180                if (free_string(&cp->freemem, s) >= size)
 181                        break;
 182        }
 183        s = alloc_string(&cp->freemem, size);
 184        BUG_ON(!s);
 185        if (cp->nr_up != 0 && cp->nr_up + cp->view.rows > cp->nr_lines) {
 186                cp->nr_up = cp->nr_lines - cp->view.rows + 1;
 187                con3270_rebuild_update(cp);
 188                con3270_update_status(cp);
 189        }
 190        return s;
 191}
 192
 193/*
 194 * Write completion callback.
 195 */
 196static void
 197con3270_write_callback(struct raw3270_request *rq, void *data)
 198{
 199        raw3270_request_reset(rq);
 200        xchg(&((struct con3270 *) rq->view)->write, rq);
 201}
 202
 203/*
 204 * Update console display.
 205 */
 206static void
 207con3270_update(struct con3270 *cp)
 208{
 209        struct raw3270_request *wrq;
 210        char wcc, prolog[6];
 211        unsigned long flags;
 212        unsigned long updated;
 213        struct string *s, *n;
 214        int rc;
 215
 216        if (cp->view.dev)
 217                raw3270_activate_view(&cp->view);
 218
 219        wrq = xchg(&cp->write, 0);
 220        if (!wrq) {
 221                con3270_set_timer(cp, 1);
 222                return;
 223        }
 224
 225        spin_lock_irqsave(&cp->view.lock, flags);
 226        updated = 0;
 227        if (cp->update_flags & CON_UPDATE_ERASE) {
 228                /* Use erase write alternate to initialize display. */
 229                raw3270_request_set_cmd(wrq, TC_EWRITEA);
 230                updated |= CON_UPDATE_ERASE;
 231        } else
 232                raw3270_request_set_cmd(wrq, TC_WRITE);
 233
 234        wcc = TW_NONE;
 235        raw3270_request_add_data(wrq, &wcc, 1);
 236
 237        /*
 238         * Update status line.
 239         */
 240        if (cp->update_flags & CON_UPDATE_STATUS)
 241                if (raw3270_request_add_data(wrq, cp->status->string,
 242                                             cp->status->len) == 0)
 243                        updated |= CON_UPDATE_STATUS;
 244
 245        if (cp->update_flags & CON_UPDATE_LIST) {
 246                prolog[0] = TO_SBA;
 247                prolog[3] = TO_SA;
 248                prolog[4] = TAT_COLOR;
 249                prolog[5] = TAC_TURQ;
 250                raw3270_buffer_address(cp->view.dev, prolog + 1,
 251                                       cp->view.cols * cp->line_nr);
 252                raw3270_request_add_data(wrq, prolog, 6);
 253                /* Write strings in the update list to the screen. */
 254                list_for_each_entry_safe(s, n, &cp->update, update) {
 255                        if (s != cp->cline)
 256                                con3270_update_string(cp, s, cp->line_nr);
 257                        if (raw3270_request_add_data(wrq, s->string,
 258                                                     s->len) != 0)
 259                                break;
 260                        list_del_init(&s->update);
 261                        if (s != cp->cline)
 262                                cp->line_nr++;
 263                }
 264                if (list_empty(&cp->update))
 265                        updated |= CON_UPDATE_LIST;
 266        }
 267        wrq->callback = con3270_write_callback;
 268        rc = raw3270_start(&cp->view, wrq);
 269        if (rc == 0) {
 270                cp->update_flags &= ~updated;
 271                if (cp->update_flags)
 272                        con3270_set_timer(cp, 1);
 273        } else {
 274                raw3270_request_reset(wrq);
 275                xchg(&cp->write, wrq);
 276        }
 277        spin_unlock_irqrestore(&cp->view.lock, flags);
 278}
 279
 280/*
 281 * Read tasklet.
 282 */
 283static void
 284con3270_read_tasklet(struct raw3270_request *rrq)
 285{
 286        static char kreset_data = TW_KR;
 287        struct con3270 *cp;
 288        unsigned long flags;
 289        int nr_up, deactivate;
 290
 291        cp = (struct con3270 *) rrq->view;
 292        spin_lock_irqsave(&cp->view.lock, flags);
 293        nr_up = cp->nr_up;
 294        deactivate = 0;
 295        /* Check aid byte. */
 296        switch (cp->input->string[0]) {
 297        case 0x7d:      /* enter: jump to bottom. */
 298                nr_up = 0;
 299                break;
 300        case 0xf3:      /* PF3: deactivate the console view. */
 301                deactivate = 1;
 302                break;
 303        case 0x6d:      /* clear: start from scratch. */
 304                con3270_rebuild_update(cp);
 305                cp->update_flags = CON_UPDATE_ALL;
 306                con3270_set_timer(cp, 1);
 307                break;
 308        case 0xf7:      /* PF7: do a page up in the console log. */
 309                nr_up += cp->view.rows - 2;
 310                if (nr_up + cp->view.rows - 1 > cp->nr_lines) {
 311                        nr_up = cp->nr_lines - cp->view.rows + 1;
 312                        if (nr_up < 0)
 313                                nr_up = 0;
 314                }
 315                break;
 316        case 0xf8:      /* PF8: do a page down in the console log. */
 317                nr_up -= cp->view.rows - 2;
 318                if (nr_up < 0)
 319                        nr_up = 0;
 320                break;
 321        }
 322        if (nr_up != cp->nr_up) {
 323                cp->nr_up = nr_up;
 324                con3270_rebuild_update(cp);
 325                con3270_update_status(cp);
 326                con3270_set_timer(cp, 1);
 327        }
 328        spin_unlock_irqrestore(&cp->view.lock, flags);
 329
 330        /* Start keyboard reset command. */
 331        raw3270_request_reset(cp->kreset);
 332        raw3270_request_set_cmd(cp->kreset, TC_WRITE);
 333        raw3270_request_add_data(cp->kreset, &kreset_data, 1);
 334        raw3270_start(&cp->view, cp->kreset);
 335
 336        if (deactivate)
 337                raw3270_deactivate_view(&cp->view);
 338
 339        raw3270_request_reset(rrq);
 340        xchg(&cp->read, rrq);
 341        raw3270_put_view(&cp->view);
 342}
 343
 344/*
 345 * Read request completion callback.
 346 */
 347static void
 348con3270_read_callback(struct raw3270_request *rq, void *data)
 349{
 350        raw3270_get_view(rq->view);
 351        /* Schedule tasklet to pass input to tty. */
 352        tasklet_schedule(&((struct con3270 *) rq->view)->readlet);
 353}
 354
 355/*
 356 * Issue a read request. Called only from interrupt function.
 357 */
 358static void
 359con3270_issue_read(struct con3270 *cp)
 360{
 361        struct raw3270_request *rrq;
 362        int rc;
 363
 364        rrq = xchg(&cp->read, 0);
 365        if (!rrq)
 366                /* Read already scheduled. */
 367                return;
 368        rrq->callback = con3270_read_callback;
 369        rrq->callback_data = cp;
 370        raw3270_request_set_cmd(rrq, TC_READMOD);
 371        raw3270_request_set_data(rrq, cp->input->string, cp->input->len);
 372        /* Issue the read modified request. */
 373        rc = raw3270_start_irq(&cp->view, rrq);
 374        if (rc)
 375                raw3270_request_reset(rrq);
 376}
 377
 378/*
 379 * Switch to the console view.
 380 */
 381static int
 382con3270_activate(struct raw3270_view *view)
 383{
 384        unsigned long flags;
 385        struct con3270 *cp;
 386
 387        cp = (struct con3270 *) view;
 388        spin_lock_irqsave(&cp->view.lock, flags);
 389        cp->nr_up = 0;
 390        con3270_rebuild_update(cp);
 391        con3270_update_status(cp);
 392        cp->update_flags = CON_UPDATE_ALL;
 393        con3270_set_timer(cp, 1);
 394        spin_unlock_irqrestore(&cp->view.lock, flags);
 395        return 0;
 396}
 397
 398static void
 399con3270_deactivate(struct raw3270_view *view)
 400{
 401        unsigned long flags;
 402        struct con3270 *cp;
 403
 404        cp = (struct con3270 *) view;
 405        spin_lock_irqsave(&cp->view.lock, flags);
 406        del_timer(&cp->timer);
 407        spin_unlock_irqrestore(&cp->view.lock, flags);
 408}
 409
 410static int
 411con3270_irq(struct con3270 *cp, struct raw3270_request *rq, struct irb *irb)
 412{
 413        /* Handle ATTN. Schedule tasklet to read aid. */
 414        if (irb->scsw.dstat & DEV_STAT_ATTENTION)
 415                con3270_issue_read(cp);
 416
 417        if (rq) {
 418                if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
 419                        rq->rc = -EIO;
 420                else
 421                        /* Normal end. Copy residual count. */
 422                        rq->rescnt = irb->scsw.count;
 423        }
 424        return RAW3270_IO_DONE;
 425}
 426
 427/* Console view to a 3270 device. */
 428static struct raw3270_fn con3270_fn = {
 429        .activate = con3270_activate,
 430        .deactivate = con3270_deactivate,
 431        .intv = (void *) con3270_irq
 432};
 433
 434static inline void
 435con3270_cline_add(struct con3270 *cp)
 436{
 437        if (!list_empty(&cp->cline->list))
 438                /* Already added. */
 439                return;
 440        list_add_tail(&cp->cline->list, &cp->lines);
 441        cp->nr_lines++;
 442        con3270_rebuild_update(cp);
 443}
 444
 445static inline void
 446con3270_cline_insert(struct con3270 *cp, unsigned char c)
 447{
 448        cp->cline->string[cp->cline->len++] = 
 449                cp->view.ascebc[(c < ' ') ? ' ' : c];
 450        if (list_empty(&cp->cline->update)) {
 451                list_add_tail(&cp->cline->update, &cp->update);
 452                cp->update_flags |= CON_UPDATE_LIST;
 453        }
 454}
 455
 456static inline void
 457con3270_cline_end(struct con3270 *cp)
 458{
 459        struct string *s;
 460        unsigned int size;
 461
 462        /* Copy cline. */
 463        size = (cp->cline->len < cp->view.cols - 5) ?
 464                cp->cline->len + 4 : cp->view.cols;
 465        s = con3270_alloc_string(cp, size);
 466        memcpy(s->string, cp->cline->string, cp->cline->len);
 467        if (s->len < cp->view.cols - 5) {
 468                s->string[s->len - 4] = TO_RA;
 469                s->string[s->len - 1] = 0;
 470        } else {
 471                while (--size > cp->cline->len)
 472                        s->string[size] = cp->view.ascebc[' '];
 473        }
 474        /* Replace cline with allocated line s and reset cline. */
 475        list_add(&s->list, &cp->cline->list);
 476        list_del_init(&cp->cline->list);
 477        if (!list_empty(&cp->cline->update)) {
 478                list_add(&s->update, &cp->cline->update);
 479                list_del_init(&cp->cline->update);
 480        }
 481        cp->cline->len = 0;
 482}
 483
 484/*
 485 * Write a string to the 3270 console
 486 */
 487static void
 488con3270_write(struct console *co, const char *str, unsigned int count)
 489{
 490        struct con3270 *cp;
 491        unsigned long flags;
 492        unsigned char c;
 493
 494        cp = condev;
 495        spin_lock_irqsave(&cp->view.lock, flags);
 496        while (count-- > 0) {
 497                c = *str++;
 498                if (cp->cline->len == 0)
 499                        con3270_cline_add(cp);
 500                if (c != '\n')
 501                        con3270_cline_insert(cp, c);
 502                if (c == '\n' || cp->cline->len >= cp->view.cols)
 503                        con3270_cline_end(cp);
 504        }
 505        /* Setup timer to output current console buffer after 1/10 second */
 506        if (cp->view.dev && !timer_pending(&cp->timer))
 507                con3270_set_timer(cp, HZ/10);
 508        spin_unlock_irqrestore(&cp->view.lock,flags);
 509}
 510
 511static struct tty_driver *
 512con3270_device(struct console *c, int *index)
 513{
 514        *index = c->index;
 515        return tty3270_driver;
 516}
 517
 518/*
 519 * Wait for end of write request.
 520 */
 521static void
 522con3270_wait_write(struct con3270 *cp)
 523{
 524        while (!cp->write) {
 525                raw3270_wait_cons_dev(cp->view.dev);
 526                barrier();
 527        }
 528}
 529
 530/*
 531 * panic() calls console_unblank before the system enters a
 532 * disabled, endless loop.
 533 */
 534static void
 535con3270_unblank(void)
 536{
 537        struct con3270 *cp;
 538        unsigned long flags;
 539
 540        cp = condev;
 541        if (!cp->view.dev)
 542                return;
 543        spin_lock_irqsave(&cp->view.lock, flags);
 544        con3270_wait_write(cp);
 545        cp->nr_up = 0;
 546        con3270_rebuild_update(cp);
 547        con3270_update_status(cp);
 548        while (cp->update_flags != 0) {
 549                spin_unlock_irqrestore(&cp->view.lock, flags);
 550                con3270_update(cp);
 551                spin_lock_irqsave(&cp->view.lock, flags);
 552                con3270_wait_write(cp);
 553        }
 554        spin_unlock_irqrestore(&cp->view.lock, flags);
 555}
 556
 557/*
 558 *  The console structure for the 3270 console
 559 */
 560static struct console con3270 = {
 561        .name    = "tty3270",
 562        .write   = con3270_write,
 563        .device  = con3270_device,
 564        .unblank = con3270_unblank,
 565        .flags   = CON_PRINTBUFFER,
 566};
 567
 568/*
 569 * 3270 console initialization code called from console_init().
 570 * NOTE: This is called before kmalloc is available.
 571 */
 572static int __init
 573con3270_init(void)
 574{
 575        struct ccw_device *cdev;
 576        struct raw3270 *rp;
 577        void *cbuf;
 578        int i;
 579
 580        /* Check if 3270 is to be the console */
 581        if (!CONSOLE_IS_3270)
 582                return -ENODEV;
 583
 584        /* Set the console mode for VM */
 585        if (MACHINE_IS_VM) {
 586                cpcmd("TERM CONMODE 3270", NULL, 0, NULL);
 587                cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
 588        }
 589
 590        cdev = ccw_device_probe_console();
 591        if (IS_ERR(cdev))
 592                return -ENODEV;
 593        rp = raw3270_setup_console(cdev);
 594        if (IS_ERR(rp))
 595                return PTR_ERR(rp);
 596
 597        condev = (struct con3270 *) alloc_bootmem_low(sizeof(struct con3270));
 598        memset(condev, 0, sizeof(struct con3270));
 599        condev->view.dev = rp;
 600
 601        condev->read = raw3270_request_alloc_bootmem(0);
 602        condev->read->callback = con3270_read_callback;
 603        condev->read->callback_data = condev;
 604        condev->write = 
 605                raw3270_request_alloc_bootmem(CON3270_OUTPUT_BUFFER_SIZE);
 606        condev->kreset = raw3270_request_alloc_bootmem(1);
 607
 608        INIT_LIST_HEAD(&condev->lines);
 609        INIT_LIST_HEAD(&condev->update);
 610        init_timer(&condev->timer);
 611        tasklet_init(&condev->readlet, 
 612                     (void (*)(unsigned long)) con3270_read_tasklet,
 613                     (unsigned long) condev->read);
 614
 615        raw3270_add_view(&condev->view, &con3270_fn, 1);
 616
 617        INIT_LIST_HEAD(&condev->freemem);
 618        for (i = 0; i < CON3270_STRING_PAGES; i++) {
 619                cbuf = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
 620                add_string_memory(&condev->freemem, cbuf, PAGE_SIZE);
 621        }
 622        condev->cline = alloc_string(&condev->freemem, condev->view.cols);
 623        condev->cline->len = 0;
 624        con3270_create_status(condev);
 625        condev->input = alloc_string(&condev->freemem, 80);
 626        register_console(&con3270);
 627        return 0;
 628}
 629
 630console_initcall(con3270_init);
 631
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.