1 // SPDX-License-Identifier: GPL-2.0
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 IBM Corp. 2003, 2009
11 #include <linux/bootmem.h>
12 #include <linux/console.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/compat.h>
16 #include <linux/sched/signal.h>
17 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
22 #include <asm/compat.h>
23 #include <asm/ccwdev.h>
25 #include <asm/ebcdic.h>
26 #include <asm/idals.h>
31 static struct raw3270_fn fs3270_fn;
34 struct raw3270_view view;
35 struct pid *fs_pid; /* Pid of controlling program. */
36 int read_command; /* ccw command to use for reads. */
37 int write_command; /* ccw command to use for writes. */
38 int attention; /* Got attention. */
39 int active; /* Fullscreen view is active. */
40 struct raw3270_request *init; /* single init request. */
41 wait_queue_head_t wait; /* Init & attention wait queue. */
42 struct idal_buffer *rdbuf; /* full-screen-deactivate buffer */
43 size_t rdbuf_size; /* size of data returned by RDBUF */
46 static DEFINE_MUTEX(fs3270_mutex);
49 fs3270_wake_up(struct raw3270_request *rq, void *data)
51 wake_up((wait_queue_head_t *) data);
55 fs3270_working(struct fs3270 *fp)
58 * The fullscreen view is in working order if the view
59 * has been activated AND the initial request is finished.
61 return fp->active && raw3270_request_final(fp->init);
65 fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
70 fp = (struct fs3270 *) view;
71 rq->callback = fs3270_wake_up;
72 rq->callback_data = &fp->wait;
75 if (!fs3270_working(fp)) {
76 /* Fullscreen view isn't ready yet. */
77 rc = wait_event_interruptible(fp->wait,
82 rc = raw3270_start(view, rq);
84 /* Started successfully. Now wait for completion. */
85 wait_event(fp->wait, raw3270_request_final(rq));
87 } while (rc == -EACCES);
92 * Switch to the fullscreen view.
95 fs3270_reset_callback(struct raw3270_request *rq, void *data)
99 fp = (struct fs3270 *) rq->view;
100 raw3270_request_reset(rq);
105 fs3270_restore_callback(struct raw3270_request *rq, void *data)
109 fp = (struct fs3270 *) rq->view;
110 if (rq->rc != 0 || rq->rescnt != 0) {
112 kill_pid(fp->fs_pid, SIGHUP, 1);
115 raw3270_request_reset(rq);
120 fs3270_activate(struct raw3270_view *view)
126 fp = (struct fs3270 *) view;
128 /* If an old init command is still running just return. */
129 if (!raw3270_request_final(fp->init))
132 if (fp->rdbuf_size == 0) {
133 /* No saved buffer. Just clear the screen. */
134 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
135 fp->init->callback = fs3270_reset_callback;
137 /* Restore fullscreen buffer saved by fs3270_deactivate. */
138 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
139 raw3270_request_set_idal(fp->init, fp->rdbuf);
140 fp->init->ccw.count = fp->rdbuf_size;
141 cp = fp->rdbuf->data[0];
150 fp->init->rescnt = 0;
151 fp->init->callback = fs3270_restore_callback;
153 rc = fp->init->rc = raw3270_start_locked(view, fp->init);
155 fp->init->callback(fp->init, NULL);
162 * Shutdown fullscreen view.
165 fs3270_save_callback(struct raw3270_request *rq, void *data)
169 fp = (struct fs3270 *) rq->view;
171 /* Correct idal buffer element 0 address. */
172 fp->rdbuf->data[0] -= 5;
173 fp->rdbuf->size += 5;
176 * If the rdbuf command failed or the idal buffer is
177 * to small for the amount of data returned by the
178 * rdbuf command, then we have no choice but to send
179 * a SIGHUP to the application.
181 if (rq->rc != 0 || rq->rescnt == 0) {
183 kill_pid(fp->fs_pid, SIGHUP, 1);
186 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
187 raw3270_request_reset(rq);
192 fs3270_deactivate(struct raw3270_view *view)
196 fp = (struct fs3270 *) view;
199 /* If an old init command is still running just return. */
200 if (!raw3270_request_final(fp->init))
203 /* Prepare read-buffer request. */
204 raw3270_request_set_cmd(fp->init, TC_RDBUF);
206 * Hackish: skip first 5 bytes of the idal buffer to make
207 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
208 * in the activation command.
210 fp->rdbuf->data[0] += 5;
211 fp->rdbuf->size -= 5;
212 raw3270_request_set_idal(fp->init, fp->rdbuf);
213 fp->init->rescnt = 0;
214 fp->init->callback = fs3270_save_callback;
216 /* Start I/O to read in the 3270 buffer. */
217 fp->init->rc = raw3270_start_locked(view, fp->init);
219 fp->init->callback(fp->init, NULL);
223 fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
225 /* Handle ATTN. Set indication and wake waiters for attention. */
226 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
232 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
235 /* Normal end. Copy residual count. */
236 rq->rescnt = irb->scsw.cmd.count;
241 * Process reads from fullscreen 3270.
244 fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
247 struct raw3270_request *rq;
248 struct idal_buffer *ib;
251 if (count == 0 || count > 65535)
253 fp = filp->private_data;
256 ib = idal_buffer_alloc(count, 0);
259 rq = raw3270_request_alloc(0);
261 if (fp->read_command == 0 && fp->write_command != 0)
262 fp->read_command = 6;
263 raw3270_request_set_cmd(rq, fp->read_command ? : 2);
264 raw3270_request_set_idal(rq, ib);
265 rc = wait_event_interruptible(fp->wait, fp->attention);
268 rc = fs3270_do_io(&fp->view, rq);
271 if (idal_buffer_to_user(ib, data, count) != 0)
278 raw3270_request_free(rq);
281 idal_buffer_free(ib);
286 * Process writes to fullscreen 3270.
289 fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
292 struct raw3270_request *rq;
293 struct idal_buffer *ib;
297 fp = filp->private_data;
300 ib = idal_buffer_alloc(count, 0);
303 rq = raw3270_request_alloc(0);
305 if (idal_buffer_from_user(ib, data, count) == 0) {
306 write_command = fp->write_command ? : 1;
307 if (write_command == 5)
309 raw3270_request_set_cmd(rq, write_command);
310 raw3270_request_set_idal(rq, ib);
311 rc = fs3270_do_io(&fp->view, rq);
313 rc = count - rq->rescnt;
316 raw3270_request_free(rq);
319 idal_buffer_free(ib);
324 * process ioctl commands for the tube driver
327 fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
331 struct raw3270_iocb iocb;
334 fp = filp->private_data;
337 if (is_compat_task())
338 argp = compat_ptr(arg);
340 argp = (char __user *)arg;
342 mutex_lock(&fs3270_mutex);
345 fp->read_command = arg;
348 fp->write_command = arg;
351 rc = put_user(fp->read_command, argp);
354 rc = put_user(fp->write_command, argp);
357 iocb.model = fp->view.model;
358 iocb.line_cnt = fp->view.rows;
359 iocb.col_cnt = fp->view.cols;
363 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
367 mutex_unlock(&fs3270_mutex);
372 * Allocate fs3270 structure.
374 static struct fs3270 *
375 fs3270_alloc_view(void)
379 fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
381 return ERR_PTR(-ENOMEM);
382 fp->init = raw3270_request_alloc(0);
383 if (IS_ERR(fp->init)) {
385 return ERR_PTR(-ENOMEM);
391 * Free fs3270 structure.
394 fs3270_free_view(struct raw3270_view *view)
398 fp = (struct fs3270 *) view;
400 idal_buffer_free(fp->rdbuf);
401 raw3270_request_free(((struct fs3270 *) view)->init);
406 * Unlink fs3270 data structure from filp.
409 fs3270_release(struct raw3270_view *view)
413 fp = (struct fs3270 *) view;
415 kill_pid(fp->fs_pid, SIGHUP, 1);
418 /* View to a 3270 device. Can be console, tty or fullscreen. */
419 static struct raw3270_fn fs3270_fn = {
420 .activate = fs3270_activate,
421 .deactivate = fs3270_deactivate,
422 .intv = (void *) fs3270_irq,
423 .release = fs3270_release,
424 .free = fs3270_free_view
428 * This routine is called whenever a 3270 fullscreen device is opened.
431 fs3270_open(struct inode *inode, struct file *filp)
434 struct idal_buffer *ib;
437 if (imajor(file_inode(filp)) != IBM_FS3270_MAJOR)
439 minor = iminor(file_inode(filp));
440 /* Check for minor 0 multiplexer. */
442 struct tty_struct *tty = get_current_tty();
443 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
450 mutex_lock(&fs3270_mutex);
451 /* Check if some other program is already using fullscreen mode. */
452 fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
454 raw3270_put_view(&fp->view);
458 /* Allocate fullscreen view structure. */
459 fp = fs3270_alloc_view();
465 init_waitqueue_head(&fp->wait);
466 fp->fs_pid = get_pid(task_pid(current));
467 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
469 fs3270_free_view(&fp->view);
473 /* Allocate idal-buffer. */
474 ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
476 raw3270_put_view(&fp->view);
477 raw3270_del_view(&fp->view);
483 rc = raw3270_activate_view(&fp->view);
485 raw3270_put_view(&fp->view);
486 raw3270_del_view(&fp->view);
489 nonseekable_open(inode, filp);
490 filp->private_data = fp;
492 mutex_unlock(&fs3270_mutex);
497 * This routine is called when the 3270 tty is closed. We wait
498 * for the remaining request to be completed. Then we clean up.
501 fs3270_close(struct inode *inode, struct file *filp)
505 fp = filp->private_data;
506 filp->private_data = NULL;
510 raw3270_reset(&fp->view);
511 raw3270_put_view(&fp->view);
512 raw3270_del_view(&fp->view);
517 static const struct file_operations fs3270_fops = {
518 .owner = THIS_MODULE, /* owner */
519 .read = fs3270_read, /* read */
520 .write = fs3270_write, /* write */
521 .unlocked_ioctl = fs3270_ioctl, /* ioctl */
522 .compat_ioctl = fs3270_ioctl, /* ioctl */
523 .open = fs3270_open, /* open */
524 .release = fs3270_close, /* release */
528 static void fs3270_create_cb(int minor)
530 __register_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub", &fs3270_fops);
531 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, minor),
532 NULL, "3270/tub%d", minor);
535 static void fs3270_destroy_cb(int minor)
537 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, minor));
538 __unregister_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub");
541 static struct raw3270_notifier fs3270_notifier =
543 .create = fs3270_create_cb,
544 .destroy = fs3270_destroy_cb,
548 * 3270 fullscreen driver initialization.
555 rc = __register_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270", &fs3270_fops);
558 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, 0),
560 raw3270_register_notifier(&fs3270_notifier);
567 raw3270_unregister_notifier(&fs3270_notifier);
568 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, 0));
569 __unregister_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270");
572 MODULE_LICENSE("GPL");
573 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
575 module_init(fs3270_init);
576 module_exit(fs3270_exit);