2 * IBM/3270 Driver - fullscreen driver.
5 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
6 * Rewritten for 2.5/2.6 by Martin Schwidefsky <schwidefsky@de.ibm.com>
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/module.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
19 #include <asm/compat.h>
20 #include <asm/ccwdev.h>
22 #include <asm/ebcdic.h>
23 #include <asm/idals.h>
28 static struct raw3270_fn fs3270_fn
;
31 struct raw3270_view view
;
32 struct pid
*fs_pid
; /* Pid of controlling program. */
33 int read_command
; /* ccw command to use for reads. */
34 int write_command
; /* ccw command to use for writes. */
35 int attention
; /* Got attention. */
36 int active
; /* Fullscreen view is active. */
37 struct raw3270_request
*init
; /* single init request. */
38 wait_queue_head_t wait
; /* Init & attention wait queue. */
39 struct idal_buffer
*rdbuf
; /* full-screen-deactivate buffer */
40 size_t rdbuf_size
; /* size of data returned by RDBUF */
43 static DEFINE_MUTEX(fs3270_mutex
);
46 fs3270_wake_up(struct raw3270_request
*rq
, void *data
)
48 wake_up((wait_queue_head_t
*) data
);
52 fs3270_working(struct fs3270
*fp
)
55 * The fullscreen view is in working order if the view
56 * has been activated AND the initial request is finished.
58 return fp
->active
&& raw3270_request_final(fp
->init
);
62 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.
92 fs3270_reset_callback(struct raw3270_request
*rq
, void *data
)
96 fp
= (struct fs3270
*) rq
->view
;
97 raw3270_request_reset(rq
);
102 fs3270_restore_callback(struct raw3270_request
*rq
, void *data
)
106 fp
= (struct fs3270
*) rq
->view
;
107 if (rq
->rc
!= 0 || rq
->rescnt
!= 0) {
109 kill_pid(fp
->fs_pid
, SIGHUP
, 1);
112 raw3270_request_reset(rq
);
117 fs3270_activate(struct raw3270_view
*view
)
123 fp
= (struct fs3270
*) view
;
125 /* If an old init command is still running just return. */
126 if (!raw3270_request_final(fp
->init
))
129 if (fp
->rdbuf_size
== 0) {
130 /* No saved buffer. Just clear the screen. */
131 raw3270_request_set_cmd(fp
->init
, TC_EWRITEA
);
132 fp
->init
->callback
= fs3270_reset_callback
;
134 /* Restore fullscreen buffer saved by fs3270_deactivate. */
135 raw3270_request_set_cmd(fp
->init
, TC_EWRITEA
);
136 raw3270_request_set_idal(fp
->init
, fp
->rdbuf
);
137 fp
->init
->ccw
.count
= fp
->rdbuf_size
;
138 cp
= fp
->rdbuf
->data
[0];
147 fp
->init
->rescnt
= 0;
148 fp
->init
->callback
= fs3270_restore_callback
;
150 rc
= fp
->init
->rc
= raw3270_start_locked(view
, fp
->init
);
152 fp
->init
->callback(fp
->init
, NULL
);
159 * Shutdown fullscreen view.
162 fs3270_save_callback(struct raw3270_request
*rq
, void *data
)
166 fp
= (struct fs3270
*) rq
->view
;
168 /* Correct idal buffer element 0 address. */
169 fp
->rdbuf
->data
[0] -= 5;
170 fp
->rdbuf
->size
+= 5;
173 * If the rdbuf command failed or the idal buffer is
174 * to small for the amount of data returned by the
175 * rdbuf command, then we have no choice but to send
176 * a SIGHUP to the application.
178 if (rq
->rc
!= 0 || rq
->rescnt
== 0) {
180 kill_pid(fp
->fs_pid
, SIGHUP
, 1);
183 fp
->rdbuf_size
= fp
->rdbuf
->size
- rq
->rescnt
;
184 raw3270_request_reset(rq
);
189 fs3270_deactivate(struct raw3270_view
*view
)
193 fp
= (struct fs3270
*) view
;
196 /* If an old init command is still running just return. */
197 if (!raw3270_request_final(fp
->init
))
200 /* Prepare read-buffer request. */
201 raw3270_request_set_cmd(fp
->init
, TC_RDBUF
);
203 * Hackish: skip first 5 bytes of the idal buffer to make
204 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
205 * in the activation command.
207 fp
->rdbuf
->data
[0] += 5;
208 fp
->rdbuf
->size
-= 5;
209 raw3270_request_set_idal(fp
->init
, fp
->rdbuf
);
210 fp
->init
->rescnt
= 0;
211 fp
->init
->callback
= fs3270_save_callback
;
213 /* Start I/O to read in the 3270 buffer. */
214 fp
->init
->rc
= raw3270_start_locked(view
, fp
->init
);
216 fp
->init
->callback(fp
->init
, NULL
);
220 fs3270_irq(struct fs3270
*fp
, struct raw3270_request
*rq
, struct irb
*irb
)
222 /* Handle ATTN. Set indication and wake waiters for attention. */
223 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_ATTENTION
) {
229 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
)
232 /* Normal end. Copy residual count. */
233 rq
->rescnt
= irb
->scsw
.cmd
.count
;
235 return RAW3270_IO_DONE
;
239 * Process reads from fullscreen 3270.
242 fs3270_read(struct file
*filp
, char __user
*data
, size_t count
, loff_t
*off
)
245 struct raw3270_request
*rq
;
246 struct idal_buffer
*ib
;
249 if (count
== 0 || count
> 65535)
251 fp
= filp
->private_data
;
254 ib
= idal_buffer_alloc(count
, 0);
257 rq
= raw3270_request_alloc(0);
259 if (fp
->read_command
== 0 && fp
->write_command
!= 0)
260 fp
->read_command
= 6;
261 raw3270_request_set_cmd(rq
, fp
->read_command
? : 2);
262 raw3270_request_set_idal(rq
, ib
);
263 rc
= wait_event_interruptible(fp
->wait
, fp
->attention
);
266 rc
= fs3270_do_io(&fp
->view
, rq
);
269 if (idal_buffer_to_user(ib
, data
, count
) != 0)
276 raw3270_request_free(rq
);
279 idal_buffer_free(ib
);
284 * Process writes to fullscreen 3270.
287 fs3270_write(struct file
*filp
, const char __user
*data
, size_t count
, loff_t
*off
)
290 struct raw3270_request
*rq
;
291 struct idal_buffer
*ib
;
295 fp
= filp
->private_data
;
298 ib
= idal_buffer_alloc(count
, 0);
301 rq
= raw3270_request_alloc(0);
303 if (idal_buffer_from_user(ib
, data
, count
) == 0) {
304 write_command
= fp
->write_command
? : 1;
305 if (write_command
== 5)
307 raw3270_request_set_cmd(rq
, write_command
);
308 raw3270_request_set_idal(rq
, ib
);
309 rc
= fs3270_do_io(&fp
->view
, rq
);
311 rc
= count
- rq
->rescnt
;
314 raw3270_request_free(rq
);
317 idal_buffer_free(ib
);
322 * process ioctl commands for the tube driver
325 fs3270_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
329 struct raw3270_iocb iocb
;
332 fp
= filp
->private_data
;
335 if (is_compat_task())
336 argp
= compat_ptr(arg
);
338 argp
= (char __user
*)arg
;
340 mutex_lock(&fs3270_mutex
);
343 fp
->read_command
= arg
;
346 fp
->write_command
= arg
;
349 rc
= put_user(fp
->read_command
, argp
);
352 rc
= put_user(fp
->write_command
, argp
);
355 iocb
.model
= fp
->view
.model
;
356 iocb
.line_cnt
= fp
->view
.rows
;
357 iocb
.col_cnt
= fp
->view
.cols
;
361 if (copy_to_user(argp
, &iocb
, sizeof(struct raw3270_iocb
)))
365 mutex_unlock(&fs3270_mutex
);
370 * Allocate fs3270 structure.
372 static struct fs3270
*
373 fs3270_alloc_view(void)
377 fp
= kzalloc(sizeof(struct fs3270
),GFP_KERNEL
);
379 return ERR_PTR(-ENOMEM
);
380 fp
->init
= raw3270_request_alloc(0);
381 if (IS_ERR(fp
->init
)) {
383 return ERR_PTR(-ENOMEM
);
389 * Free fs3270 structure.
392 fs3270_free_view(struct raw3270_view
*view
)
396 fp
= (struct fs3270
*) view
;
398 idal_buffer_free(fp
->rdbuf
);
399 raw3270_request_free(((struct fs3270
*) view
)->init
);
404 * Unlink fs3270 data structure from filp.
407 fs3270_release(struct raw3270_view
*view
)
411 fp
= (struct fs3270
*) view
;
413 kill_pid(fp
->fs_pid
, SIGHUP
, 1);
416 /* View to a 3270 device. Can be console, tty or fullscreen. */
417 static struct raw3270_fn fs3270_fn
= {
418 .activate
= fs3270_activate
,
419 .deactivate
= fs3270_deactivate
,
420 .intv
= (void *) fs3270_irq
,
421 .release
= fs3270_release
,
422 .free
= fs3270_free_view
426 * This routine is called whenever a 3270 fullscreen device is opened.
429 fs3270_open(struct inode
*inode
, struct file
*filp
)
432 struct idal_buffer
*ib
;
435 if (imajor(filp
->f_path
.dentry
->d_inode
) != IBM_FS3270_MAJOR
)
437 minor
= iminor(filp
->f_path
.dentry
->d_inode
);
438 /* Check for minor 0 multiplexer. */
440 struct tty_struct
*tty
= get_current_tty();
441 if (!tty
|| tty
->driver
->major
!= IBM_TTY3270_MAJOR
) {
445 minor
= tty
->index
+ RAW3270_FIRSTMINOR
;
448 mutex_lock(&fs3270_mutex
);
449 /* Check if some other program is already using fullscreen mode. */
450 fp
= (struct fs3270
*) raw3270_find_view(&fs3270_fn
, minor
);
452 raw3270_put_view(&fp
->view
);
456 /* Allocate fullscreen view structure. */
457 fp
= fs3270_alloc_view();
463 init_waitqueue_head(&fp
->wait
);
464 fp
->fs_pid
= get_pid(task_pid(current
));
465 rc
= raw3270_add_view(&fp
->view
, &fs3270_fn
, minor
);
467 fs3270_free_view(&fp
->view
);
471 /* Allocate idal-buffer. */
472 ib
= idal_buffer_alloc(2*fp
->view
.rows
*fp
->view
.cols
+ 5, 0);
474 raw3270_put_view(&fp
->view
);
475 raw3270_del_view(&fp
->view
);
481 rc
= raw3270_activate_view(&fp
->view
);
483 raw3270_put_view(&fp
->view
);
484 raw3270_del_view(&fp
->view
);
487 nonseekable_open(inode
, filp
);
488 filp
->private_data
= fp
;
490 mutex_unlock(&fs3270_mutex
);
495 * This routine is called when the 3270 tty is closed. We wait
496 * for the remaining request to be completed. Then we clean up.
499 fs3270_close(struct inode
*inode
, struct file
*filp
)
503 fp
= filp
->private_data
;
504 filp
->private_data
= NULL
;
508 raw3270_reset(&fp
->view
);
509 raw3270_put_view(&fp
->view
);
510 raw3270_del_view(&fp
->view
);
515 static const struct file_operations fs3270_fops
= {
516 .owner
= THIS_MODULE
, /* owner */
517 .read
= fs3270_read
, /* read */
518 .write
= fs3270_write
, /* write */
519 .unlocked_ioctl
= fs3270_ioctl
, /* ioctl */
520 .compat_ioctl
= fs3270_ioctl
, /* ioctl */
521 .open
= fs3270_open
, /* open */
522 .release
= fs3270_close
, /* release */
527 * 3270 fullscreen driver initialization.
534 rc
= register_chrdev(IBM_FS3270_MAJOR
, "fs3270", &fs3270_fops
);
543 unregister_chrdev(IBM_FS3270_MAJOR
, "fs3270");
546 MODULE_LICENSE("GPL");
547 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR
);
549 module_init(fs3270_init
);
550 module_exit(fs3270_exit
);