2 * IBM/3270 Driver - fullscreen driver.
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7 * Copyright IBM Corp. 2003, 2009
10 #include <linux/bootmem.h>
11 #include <linux/console.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/list.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
18 #include <asm/compat.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 */
42 static DEFINE_MUTEX(fs3270_mutex);
45 fs3270_wake_up(struct raw3270_request *rq, void *data)
47 wake_up((wait_queue_head_t *) data);
51 fs3270_working(struct fs3270 *fp)
54 * The fullscreen view is in working order if the view
55 * has been activated AND the initial request is finished.
57 return fp->active && raw3270_request_final(fp->init);
61 fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
66 fp = (struct fs3270 *) view;
67 rq->callback = fs3270_wake_up;
68 rq->callback_data = &fp->wait;
71 if (!fs3270_working(fp)) {
72 /* Fullscreen view isn't ready yet. */
73 rc = wait_event_interruptible(fp->wait,
78 rc = raw3270_start(view, rq);
80 /* Started successfully. Now wait for completion. */
81 wait_event(fp->wait, raw3270_request_final(rq));
83 } while (rc == -EACCES);
88 * Switch to the fullscreen view.
91 fs3270_reset_callback(struct raw3270_request *rq, void *data)
95 fp = (struct fs3270 *) rq->view;
96 raw3270_request_reset(rq);
101 fs3270_restore_callback(struct raw3270_request *rq, void *data)
105 fp = (struct fs3270 *) rq->view;
106 if (rq->rc != 0 || rq->rescnt != 0) {
108 kill_pid(fp->fs_pid, SIGHUP, 1);
111 raw3270_request_reset(rq);
116 fs3270_activate(struct raw3270_view *view)
122 fp = (struct fs3270 *) view;
124 /* If an old init command is still running just return. */
125 if (!raw3270_request_final(fp->init))
128 if (fp->rdbuf_size == 0) {
129 /* No saved buffer. Just clear the screen. */
130 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
131 fp->init->callback = fs3270_reset_callback;
133 /* Restore fullscreen buffer saved by fs3270_deactivate. */
134 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
135 raw3270_request_set_idal(fp->init, fp->rdbuf);
136 fp->init->ccw.count = fp->rdbuf_size;
137 cp = fp->rdbuf->data[0];
146 fp->init->rescnt = 0;
147 fp->init->callback = fs3270_restore_callback;
149 rc = fp->init->rc = raw3270_start_locked(view, fp->init);
151 fp->init->callback(fp->init, NULL);
158 * Shutdown fullscreen view.
161 fs3270_save_callback(struct raw3270_request *rq, void *data)
165 fp = (struct fs3270 *) rq->view;
167 /* Correct idal buffer element 0 address. */
168 fp->rdbuf->data[0] -= 5;
169 fp->rdbuf->size += 5;
172 * If the rdbuf command failed or the idal buffer is
173 * to small for the amount of data returned by the
174 * rdbuf command, then we have no choice but to send
175 * a SIGHUP to the application.
177 if (rq->rc != 0 || rq->rescnt == 0) {
179 kill_pid(fp->fs_pid, SIGHUP, 1);
182 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
183 raw3270_request_reset(rq);
188 fs3270_deactivate(struct raw3270_view *view)
192 fp = (struct fs3270 *) view;
195 /* If an old init command is still running just return. */
196 if (!raw3270_request_final(fp->init))
199 /* Prepare read-buffer request. */
200 raw3270_request_set_cmd(fp->init, TC_RDBUF);
202 * Hackish: skip first 5 bytes of the idal buffer to make
203 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
204 * in the activation command.
206 fp->rdbuf->data[0] += 5;
207 fp->rdbuf->size -= 5;
208 raw3270_request_set_idal(fp->init, fp->rdbuf);
209 fp->init->rescnt = 0;
210 fp->init->callback = fs3270_save_callback;
212 /* Start I/O to read in the 3270 buffer. */
213 fp->init->rc = raw3270_start_locked(view, fp->init);
215 fp->init->callback(fp->init, NULL);
219 fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
221 /* Handle ATTN. Set indication and wake waiters for attention. */
222 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
228 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
231 /* Normal end. Copy residual count. */
232 rq->rescnt = irb->scsw.cmd.count;
234 return RAW3270_IO_DONE;
238 * Process reads from fullscreen 3270.
241 fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
244 struct raw3270_request *rq;
245 struct idal_buffer *ib;
248 if (count == 0 || count > 65535)
250 fp = filp->private_data;
253 ib = idal_buffer_alloc(count, 0);
256 rq = raw3270_request_alloc(0);
258 if (fp->read_command == 0 && fp->write_command != 0)
259 fp->read_command = 6;
260 raw3270_request_set_cmd(rq, fp->read_command ? : 2);
261 raw3270_request_set_idal(rq, ib);
262 rc = wait_event_interruptible(fp->wait, fp->attention);
265 rc = fs3270_do_io(&fp->view, rq);
268 if (idal_buffer_to_user(ib, data, count) != 0)
275 raw3270_request_free(rq);
278 idal_buffer_free(ib);
283 * Process writes to fullscreen 3270.
286 fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
289 struct raw3270_request *rq;
290 struct idal_buffer *ib;
294 fp = filp->private_data;
297 ib = idal_buffer_alloc(count, 0);
300 rq = raw3270_request_alloc(0);
302 if (idal_buffer_from_user(ib, data, count) == 0) {
303 write_command = fp->write_command ? : 1;
304 if (write_command == 5)
306 raw3270_request_set_cmd(rq, write_command);
307 raw3270_request_set_idal(rq, ib);
308 rc = fs3270_do_io(&fp->view, rq);
310 rc = count - rq->rescnt;
313 raw3270_request_free(rq);
316 idal_buffer_free(ib);
321 * process ioctl commands for the tube driver
324 fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
328 struct raw3270_iocb iocb;
331 fp = filp->private_data;
334 if (is_compat_task())
335 argp = compat_ptr(arg);
337 argp = (char __user *)arg;
339 mutex_lock(&fs3270_mutex);
342 fp->read_command = arg;
345 fp->write_command = arg;
348 rc = put_user(fp->read_command, argp);
351 rc = put_user(fp->write_command, argp);
354 iocb.model = fp->view.model;
355 iocb.line_cnt = fp->view.rows;
356 iocb.col_cnt = fp->view.cols;
360 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
364 mutex_unlock(&fs3270_mutex);
369 * Allocate fs3270 structure.
371 static struct fs3270 *
372 fs3270_alloc_view(void)
376 fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
378 return ERR_PTR(-ENOMEM);
379 fp->init = raw3270_request_alloc(0);
380 if (IS_ERR(fp->init)) {
382 return ERR_PTR(-ENOMEM);
388 * Free fs3270 structure.
391 fs3270_free_view(struct raw3270_view *view)
395 fp = (struct fs3270 *) view;
397 idal_buffer_free(fp->rdbuf);
398 raw3270_request_free(((struct fs3270 *) view)->init);
403 * Unlink fs3270 data structure from filp.
406 fs3270_release(struct raw3270_view *view)
410 fp = (struct fs3270 *) view;
412 kill_pid(fp->fs_pid, SIGHUP, 1);
415 /* View to a 3270 device. Can be console, tty or fullscreen. */
416 static struct raw3270_fn fs3270_fn = {
417 .activate = fs3270_activate,
418 .deactivate = fs3270_deactivate,
419 .intv = (void *) fs3270_irq,
420 .release = fs3270_release,
421 .free = fs3270_free_view
425 * This routine is called whenever a 3270 fullscreen device is opened.
428 fs3270_open(struct inode *inode, struct file *filp)
431 struct idal_buffer *ib;
434 if (imajor(filp->f_path.dentry->d_inode) != IBM_FS3270_MAJOR)
436 minor = iminor(filp->f_path.dentry->d_inode);
437 /* Check for minor 0 multiplexer. */
439 struct tty_struct *tty = get_current_tty();
440 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
444 minor = tty->index + RAW3270_FIRSTMINOR;
447 mutex_lock(&fs3270_mutex);
448 /* Check if some other program is already using fullscreen mode. */
449 fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
451 raw3270_put_view(&fp->view);
455 /* Allocate fullscreen view structure. */
456 fp = fs3270_alloc_view();
462 init_waitqueue_head(&fp->wait);
463 fp->fs_pid = get_pid(task_pid(current));
464 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
466 fs3270_free_view(&fp->view);
470 /* Allocate idal-buffer. */
471 ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
473 raw3270_put_view(&fp->view);
474 raw3270_del_view(&fp->view);
480 rc = raw3270_activate_view(&fp->view);
482 raw3270_put_view(&fp->view);
483 raw3270_del_view(&fp->view);
486 nonseekable_open(inode, filp);
487 filp->private_data = fp;
489 mutex_unlock(&fs3270_mutex);
494 * This routine is called when the 3270 tty is closed. We wait
495 * for the remaining request to be completed. Then we clean up.
498 fs3270_close(struct inode *inode, struct file *filp)
502 fp = filp->private_data;
503 filp->private_data = NULL;
507 raw3270_reset(&fp->view);
508 raw3270_put_view(&fp->view);
509 raw3270_del_view(&fp->view);
514 static const struct file_operations fs3270_fops = {
515 .owner = THIS_MODULE, /* owner */
516 .read = fs3270_read, /* read */
517 .write = fs3270_write, /* write */
518 .unlocked_ioctl = fs3270_ioctl, /* ioctl */
519 .compat_ioctl = fs3270_ioctl, /* ioctl */
520 .open = fs3270_open, /* open */
521 .release = fs3270_close, /* release */
526 * 3270 fullscreen driver initialization.
533 rc = register_chrdev(IBM_FS3270_MAJOR, "fs3270", &fs3270_fops);
542 unregister_chrdev(IBM_FS3270_MAJOR, "fs3270");
545 MODULE_LICENSE("GPL");
546 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
548 module_init(fs3270_init);
549 module_exit(fs3270_exit);