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)
8 * Copyright IBM Corp. 2003, 2009
11 #include <linux/memblock.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 <uapi/asm/fs3270.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);
48 static void fs3270_wake_up(struct raw3270_request *rq, void *data)
50 wake_up((wait_queue_head_t *)data);
53 static inline int fs3270_working(struct fs3270 *fp)
56 * The fullscreen view is in working order if the view
57 * has been activated AND the initial request is finished.
59 return fp->active && raw3270_request_final(fp->init);
62 static int fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
67 fp = (struct fs3270 *)view;
68 rq->callback = fs3270_wake_up;
69 rq->callback_data = &fp->wait;
72 if (!fs3270_working(fp)) {
73 /* Fullscreen view isn't ready yet. */
74 rc = wait_event_interruptible(fp->wait,
79 rc = raw3270_start(view, rq);
81 /* Started successfully. Now wait for completion. */
82 wait_event(fp->wait, raw3270_request_final(rq));
84 } while (rc == -EACCES);
89 * Switch to the fullscreen view.
91 static void fs3270_reset_callback(struct raw3270_request *rq, void *data)
95 fp = (struct fs3270 *)rq->view;
96 raw3270_request_reset(rq);
100 static void fs3270_restore_callback(struct raw3270_request *rq, void *data)
104 fp = (struct fs3270 *)rq->view;
105 if (rq->rc != 0 || rq->rescnt != 0) {
107 kill_pid(fp->fs_pid, SIGHUP, 1);
110 raw3270_request_reset(rq);
114 static int 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 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
127 raw3270_request_set_idal(fp->init, fp->rdbuf);
128 fp->init->rescnt = 0;
129 cp = fp->rdbuf->data[0];
130 if (fp->rdbuf_size == 0) {
131 /* No saved buffer. Just clear the screen. */
132 fp->init->ccw.count = 1;
133 fp->init->callback = fs3270_reset_callback;
136 /* Restore fullscreen buffer saved by fs3270_deactivate. */
137 fp->init->ccw.count = fp->rdbuf_size;
138 fp->init->callback = fs3270_restore_callback;
148 rc = raw3270_start_locked(view, fp->init);
151 fp->init->callback(fp->init, NULL);
158 * Shutdown fullscreen view.
160 static void fs3270_save_callback(struct raw3270_request *rq, void *data)
164 fp = (struct fs3270 *)rq->view;
166 /* Correct idal buffer element 0 address. */
167 fp->rdbuf->data[0] -= 5;
168 fp->rdbuf->size += 5;
171 * If the rdbuf command failed or the idal buffer is
172 * to small for the amount of data returned by the
173 * rdbuf command, then we have no choice but to send
174 * a SIGHUP to the application.
176 if (rq->rc != 0 || rq->rescnt == 0) {
178 kill_pid(fp->fs_pid, SIGHUP, 1);
181 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
183 raw3270_request_reset(rq);
187 static void fs3270_deactivate(struct raw3270_view *view)
191 fp = (struct fs3270 *)view;
194 /* If an old init command is still running just return. */
195 if (!raw3270_request_final(fp->init))
198 /* Prepare read-buffer request. */
199 raw3270_request_set_cmd(fp->init, TC_RDBUF);
201 * Hackish: skip first 5 bytes of the idal buffer to make
202 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
203 * in the activation command.
205 fp->rdbuf->data[0] += 5;
206 fp->rdbuf->size -= 5;
207 raw3270_request_set_idal(fp->init, fp->rdbuf);
208 fp->init->rescnt = 0;
209 fp->init->callback = fs3270_save_callback;
211 /* Start I/O to read in the 3270 buffer. */
212 fp->init->rc = raw3270_start_locked(view, fp->init);
214 fp->init->callback(fp->init, NULL);
217 static void fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq,
220 /* Handle ATTN. Set indication and wake waiters for attention. */
221 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
227 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
230 /* Normal end. Copy residual count. */
231 rq->rescnt = irb->scsw.cmd.count;
236 * Process reads from fullscreen 3270.
238 static ssize_t fs3270_read(struct file *filp, char __user *data,
239 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)
272 raw3270_request_free(rq);
276 idal_buffer_free(ib);
281 * Process writes to fullscreen 3270.
283 static ssize_t fs3270_write(struct file *filp, const char __user *data,
284 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;
312 raw3270_request_free(rq);
316 idal_buffer_free(ib);
321 * process ioctl commands for the tube driver
323 static long fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
327 struct raw3270_iocb iocb;
330 fp = filp->private_data;
333 if (is_compat_task())
334 argp = compat_ptr(arg);
336 argp = (char __user *)arg;
338 mutex_lock(&fs3270_mutex);
341 fp->read_command = arg;
344 fp->write_command = arg;
347 rc = put_user(fp->read_command, argp);
350 rc = put_user(fp->write_command, argp);
353 iocb.model = fp->view.model;
354 iocb.line_cnt = fp->view.rows;
355 iocb.col_cnt = fp->view.cols;
359 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
363 mutex_unlock(&fs3270_mutex);
368 * Allocate fs3270 structure.
370 static struct fs3270 *fs3270_alloc_view(void)
374 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
376 return ERR_PTR(-ENOMEM);
377 fp->init = raw3270_request_alloc(0);
378 if (IS_ERR(fp->init)) {
380 return ERR_PTR(-ENOMEM);
386 * Free fs3270 structure.
388 static void fs3270_free_view(struct raw3270_view *view)
392 fp = (struct fs3270 *)view;
394 idal_buffer_free(fp->rdbuf);
395 raw3270_request_free(((struct fs3270 *)view)->init);
400 * Unlink fs3270 data structure from filp.
402 static void fs3270_release(struct raw3270_view *view)
406 fp = (struct fs3270 *)view;
408 kill_pid(fp->fs_pid, SIGHUP, 1);
411 /* View to a 3270 device. Can be console, tty or fullscreen. */
412 static struct raw3270_fn fs3270_fn = {
413 .activate = fs3270_activate,
414 .deactivate = fs3270_deactivate,
415 .intv = (void *)fs3270_irq,
416 .release = fs3270_release,
417 .free = fs3270_free_view
421 * This routine is called whenever a 3270 fullscreen device is opened.
423 static int fs3270_open(struct inode *inode, struct file *filp)
426 struct idal_buffer *ib;
429 if (imajor(file_inode(filp)) != IBM_FS3270_MAJOR)
431 minor = iminor(file_inode(filp));
432 /* Check for minor 0 multiplexer. */
434 struct tty_struct *tty = get_current_tty();
436 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
443 mutex_lock(&fs3270_mutex);
444 /* Check if some other program is already using fullscreen mode. */
445 fp = (struct fs3270 *)raw3270_find_view(&fs3270_fn, minor);
447 raw3270_put_view(&fp->view);
451 /* Allocate fullscreen view structure. */
452 fp = fs3270_alloc_view();
458 init_waitqueue_head(&fp->wait);
459 fp->fs_pid = get_pid(task_pid(current));
460 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor,
461 RAW3270_VIEW_LOCK_BH);
463 fs3270_free_view(&fp->view);
467 /* Allocate idal-buffer. */
468 ib = idal_buffer_alloc(2 * fp->view.rows * fp->view.cols + 5, 0);
470 raw3270_put_view(&fp->view);
471 raw3270_del_view(&fp->view);
477 rc = raw3270_activate_view(&fp->view);
479 raw3270_put_view(&fp->view);
480 raw3270_del_view(&fp->view);
483 stream_open(inode, filp);
484 filp->private_data = fp;
486 mutex_unlock(&fs3270_mutex);
491 * This routine is called when the 3270 tty is closed. We wait
492 * for the remaining request to be completed. Then we clean up.
494 static int fs3270_close(struct inode *inode, struct file *filp)
498 fp = filp->private_data;
499 filp->private_data = NULL;
503 raw3270_reset(&fp->view);
504 raw3270_put_view(&fp->view);
505 raw3270_del_view(&fp->view);
510 static const struct file_operations fs3270_fops = {
511 .owner = THIS_MODULE, /* owner */
512 .read = fs3270_read, /* read */
513 .write = fs3270_write, /* write */
514 .unlocked_ioctl = fs3270_ioctl, /* ioctl */
515 .compat_ioctl = fs3270_ioctl, /* ioctl */
516 .open = fs3270_open, /* open */
517 .release = fs3270_close, /* release */
521 static void fs3270_create_cb(int minor)
523 __register_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub", &fs3270_fops);
524 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, minor),
525 NULL, "3270/tub%d", minor);
528 static void fs3270_destroy_cb(int minor)
530 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, minor));
531 __unregister_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub");
534 static struct raw3270_notifier fs3270_notifier = {
535 .create = fs3270_create_cb,
536 .destroy = fs3270_destroy_cb,
540 * 3270 fullscreen driver initialization.
542 static int __init fs3270_init(void)
546 rc = __register_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270", &fs3270_fops);
549 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, 0),
551 raw3270_register_notifier(&fs3270_notifier);
555 static void __exit fs3270_exit(void)
557 raw3270_unregister_notifier(&fs3270_notifier);
558 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, 0));
559 __unregister_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270");
562 MODULE_LICENSE("GPL");
563 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
565 module_init(fs3270_init);
566 module_exit(fs3270_exit);