2  *  drivers/s390/char/fs3270.c
 
   3  *    IBM/3270 Driver - fullscreen driver.
 
   6  *    Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
 
   7  *    Rewritten for 2.5/2.6 by Martin Schwidefsky <schwidefsky@de.ibm.com>
 
   8  *      -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
 
  11 #include <linux/bootmem.h>
 
  12 #include <linux/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/smp_lock.h>
 
  19 #include <asm/ccwdev.h>
 
  21 #include <asm/ebcdic.h>
 
  22 #include <asm/idals.h>
 
  27 static struct raw3270_fn fs3270_fn;
 
  30         struct raw3270_view view;
 
  31         struct pid *fs_pid;             /* Pid of controlling program. */
 
  32         int read_command;               /* ccw command to use for reads. */
 
  33         int write_command;              /* ccw command to use for writes. */
 
  34         int attention;                  /* Got attention. */
 
  35         int active;                     /* Fullscreen view is active. */
 
  36         struct raw3270_request *init;   /* single init request. */
 
  37         wait_queue_head_t wait;         /* Init & attention wait queue. */
 
  38         struct idal_buffer *rdbuf;      /* full-screen-deactivate buffer */
 
  39         size_t rdbuf_size;              /* size of data returned by RDBUF */
 
  43 fs3270_wake_up(struct raw3270_request *rq, void *data)
 
  45         wake_up((wait_queue_head_t *) data);
 
  49 fs3270_working(struct fs3270 *fp)
 
  52          * The fullscreen view is in working order if the view
 
  53          * has been activated AND the initial request is finished.
 
  55         return fp->active && raw3270_request_final(fp->init);
 
  59 fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
 
  64         fp = (struct fs3270 *) view;
 
  65         rq->callback = fs3270_wake_up;
 
  66         rq->callback_data = &fp->wait;
 
  69                 if (!fs3270_working(fp)) {
 
  70                         /* Fullscreen view isn't ready yet. */
 
  71                         rc = wait_event_interruptible(fp->wait,
 
  76                 rc = raw3270_start(view, rq);
 
  78                         /* Started sucessfully. Now wait for completion. */
 
  79                         wait_event(fp->wait, raw3270_request_final(rq));
 
  81         } while (rc == -EACCES);
 
  86  * Switch to the fullscreen view.
 
  89 fs3270_reset_callback(struct raw3270_request *rq, void *data)
 
  93         fp = (struct fs3270 *) rq->view;
 
  94         raw3270_request_reset(rq);
 
  99 fs3270_restore_callback(struct raw3270_request *rq, void *data)
 
 103         fp = (struct fs3270 *) rq->view;
 
 104         if (rq->rc != 0 || rq->rescnt != 0) {
 
 106                         kill_pid(fp->fs_pid, SIGHUP, 1);
 
 109         raw3270_request_reset(rq);
 
 114 fs3270_activate(struct raw3270_view *view)
 
 120         fp = (struct fs3270 *) view;
 
 122         /* If an old init command is still running just return. */
 
 123         if (!raw3270_request_final(fp->init))
 
 126         if (fp->rdbuf_size == 0) {
 
 127                 /* No saved buffer. Just clear the screen. */
 
 128                 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
 
 129                 fp->init->callback = fs3270_reset_callback;
 
 131                 /* Restore fullscreen buffer saved by fs3270_deactivate. */
 
 132                 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
 
 133                 raw3270_request_set_idal(fp->init, fp->rdbuf);
 
 134                 fp->init->ccw.count = fp->rdbuf_size;
 
 135                 cp = fp->rdbuf->data[0];
 
 144                 fp->init->rescnt = 0;
 
 145                 fp->init->callback = fs3270_restore_callback;
 
 147         rc = fp->init->rc = raw3270_start_locked(view, fp->init);
 
 149                 fp->init->callback(fp->init, NULL);
 
 156  * Shutdown fullscreen view.
 
 159 fs3270_save_callback(struct raw3270_request *rq, void *data)
 
 163         fp = (struct fs3270 *) rq->view;
 
 165         /* Correct idal buffer element 0 address. */
 
 166         fp->rdbuf->data[0] -= 5;
 
 167         fp->rdbuf->size += 5;
 
 170          * If the rdbuf command failed or the idal buffer is
 
 171          * to small for the amount of data returned by the
 
 172          * rdbuf command, then we have no choice but to send
 
 173          * a SIGHUP to the application.
 
 175         if (rq->rc != 0 || rq->rescnt == 0) {
 
 177                         kill_pid(fp->fs_pid, SIGHUP, 1);
 
 180                 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
 
 181         raw3270_request_reset(rq);
 
 186 fs3270_deactivate(struct raw3270_view *view)
 
 190         fp = (struct fs3270 *) view;
 
 193         /* If an old init command is still running just return. */
 
 194         if (!raw3270_request_final(fp->init))
 
 197         /* Prepare read-buffer request. */
 
 198         raw3270_request_set_cmd(fp->init, TC_RDBUF);
 
 200          * Hackish: skip first 5 bytes of the idal buffer to make
 
 201          * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
 
 202          * in the activation command.
 
 204         fp->rdbuf->data[0] += 5;
 
 205         fp->rdbuf->size -= 5;
 
 206         raw3270_request_set_idal(fp->init, fp->rdbuf);
 
 207         fp->init->rescnt = 0;
 
 208         fp->init->callback = fs3270_save_callback;
 
 210         /* Start I/O to read in the 3270 buffer. */
 
 211         fp->init->rc = raw3270_start_locked(view, fp->init);
 
 213                 fp->init->callback(fp->init, NULL);
 
 217 fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
 
 219         /* Handle ATTN. Set indication and wake waiters for attention. */
 
 220         if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
 
 226                 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
 
 229                         /* Normal end. Copy residual count. */
 
 230                         rq->rescnt = irb->scsw.cmd.count;
 
 232         return RAW3270_IO_DONE;
 
 236  * Process reads from fullscreen 3270.
 
 239 fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
 
 242         struct raw3270_request *rq;
 
 243         struct idal_buffer *ib;
 
 246         if (count == 0 || count > 65535)
 
 248         fp = filp->private_data;
 
 251         ib = idal_buffer_alloc(count, 0);
 
 254         rq = raw3270_request_alloc(0);
 
 256                 if (fp->read_command == 0 && fp->write_command != 0)
 
 257                         fp->read_command = 6;
 
 258                 raw3270_request_set_cmd(rq, fp->read_command ? : 2);
 
 259                 raw3270_request_set_idal(rq, ib);
 
 260                 rc = wait_event_interruptible(fp->wait, fp->attention);
 
 263                         rc = fs3270_do_io(&fp->view, rq);
 
 266                                 if (idal_buffer_to_user(ib, data, count) != 0)
 
 273                 raw3270_request_free(rq);
 
 276         idal_buffer_free(ib);
 
 281  * Process writes to fullscreen 3270.
 
 284 fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
 
 287         struct raw3270_request *rq;
 
 288         struct idal_buffer *ib;
 
 292         fp = filp->private_data;
 
 295         ib = idal_buffer_alloc(count, 0);
 
 298         rq = raw3270_request_alloc(0);
 
 300                 if (idal_buffer_from_user(ib, data, count) == 0) {
 
 301                         write_command = fp->write_command ? : 1;
 
 302                         if (write_command == 5)
 
 304                         raw3270_request_set_cmd(rq, write_command);
 
 305                         raw3270_request_set_idal(rq, ib);
 
 306                         rc = fs3270_do_io(&fp->view, rq);
 
 308                                 rc = count - rq->rescnt;
 
 311                 raw3270_request_free(rq);
 
 314         idal_buffer_free(ib);
 
 319  * process ioctl commands for the tube driver
 
 322 fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 
 325         struct raw3270_iocb iocb;
 
 328         fp = filp->private_data;
 
 335                 fp->read_command = arg;
 
 338                 fp->write_command = arg;
 
 341                 rc = put_user(fp->read_command, (char __user *) arg);
 
 344                 rc = put_user(fp->write_command,(char __user *) arg);
 
 347                 iocb.model = fp->view.model;
 
 348                 iocb.line_cnt = fp->view.rows;
 
 349                 iocb.col_cnt = fp->view.cols;
 
 353                 if (copy_to_user((char __user *) arg, &iocb,
 
 354                                  sizeof(struct raw3270_iocb)))
 
 363  * Allocate fs3270 structure.
 
 365 static struct fs3270 *
 
 366 fs3270_alloc_view(void)
 
 370         fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
 
 372                 return ERR_PTR(-ENOMEM);
 
 373         fp->init = raw3270_request_alloc(0);
 
 374         if (IS_ERR(fp->init)) {
 
 376                 return ERR_PTR(-ENOMEM);
 
 382  * Free fs3270 structure.
 
 385 fs3270_free_view(struct raw3270_view *view)
 
 389         fp = (struct fs3270 *) view;
 
 391                 idal_buffer_free(fp->rdbuf);
 
 392         raw3270_request_free(((struct fs3270 *) view)->init);
 
 397  * Unlink fs3270 data structure from filp.
 
 400 fs3270_release(struct raw3270_view *view)
 
 404 /* View to a 3270 device. Can be console, tty or fullscreen. */
 
 405 static struct raw3270_fn fs3270_fn = {
 
 406         .activate = fs3270_activate,
 
 407         .deactivate = fs3270_deactivate,
 
 408         .intv = (void *) fs3270_irq,
 
 409         .release = fs3270_release,
 
 410         .free = fs3270_free_view
 
 414  * This routine is called whenever a 3270 fullscreen device is opened.
 
 417 fs3270_open(struct inode *inode, struct file *filp)
 
 420         struct idal_buffer *ib;
 
 423         if (imajor(filp->f_path.dentry->d_inode) != IBM_FS3270_MAJOR)
 
 425         minor = iminor(filp->f_path.dentry->d_inode);
 
 426         /* Check for minor 0 multiplexer. */
 
 428                 struct tty_struct *tty = get_current_tty();
 
 429                 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
 
 433                 minor = tty->index + RAW3270_FIRSTMINOR;
 
 437         /* Check if some other program is already using fullscreen mode. */
 
 438         fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
 
 440                 raw3270_put_view(&fp->view);
 
 444         /* Allocate fullscreen view structure. */
 
 445         fp = fs3270_alloc_view();
 
 451         init_waitqueue_head(&fp->wait);
 
 452         fp->fs_pid = get_pid(task_pid(current));
 
 453         rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
 
 455                 fs3270_free_view(&fp->view);
 
 459         /* Allocate idal-buffer. */
 
 460         ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
 
 462                 raw3270_put_view(&fp->view);
 
 463                 raw3270_del_view(&fp->view);
 
 469         rc = raw3270_activate_view(&fp->view);
 
 471                 raw3270_put_view(&fp->view);
 
 472                 raw3270_del_view(&fp->view);
 
 475         filp->private_data = fp;
 
 482  * This routine is called when the 3270 tty is closed. We wait
 
 483  * for the remaining request to be completed. Then we clean up.
 
 486 fs3270_close(struct inode *inode, struct file *filp)
 
 490         fp = filp->private_data;
 
 491         filp->private_data = NULL;
 
 495                 raw3270_reset(&fp->view);
 
 496                 raw3270_put_view(&fp->view);
 
 497                 raw3270_del_view(&fp->view);
 
 502 static const struct file_operations fs3270_fops = {
 
 503         .owner           = THIS_MODULE,         /* owner */
 
 504         .read            = fs3270_read,         /* read */
 
 505         .write           = fs3270_write,        /* write */
 
 506         .unlocked_ioctl  = fs3270_ioctl,        /* ioctl */
 
 507         .compat_ioctl    = fs3270_ioctl,        /* ioctl */
 
 508         .open           = fs3270_open,          /* open */
 
 509         .release        = fs3270_close,         /* release */
 
 513  * 3270 fullscreen driver initialization.
 
 520         rc = register_chrdev(IBM_FS3270_MAJOR, "fs3270", &fs3270_fops);
 
 529         unregister_chrdev(IBM_FS3270_MAJOR, "fs3270");
 
 532 MODULE_LICENSE("GPL");
 
 533 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
 
 535 module_init(fs3270_init);
 
 536 module_exit(fs3270_exit);