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/list.h>
15 #include <linux/types.h>
16 #include <linux/smp_lock.h>
18 #include <asm/ccwdev.h>
20 #include <asm/ebcdic.h>
21 #include <asm/idals.h>
26 static struct raw3270_fn fs3270_fn
;
29 struct raw3270_view view
;
30 struct pid
*fs_pid
; /* Pid of controlling program. */
31 int read_command
; /* ccw command to use for reads. */
32 int write_command
; /* ccw command to use for writes. */
33 int attention
; /* Got attention. */
34 int active
; /* Fullscreen view is active. */
35 struct raw3270_request
*init
; /* single init request. */
36 wait_queue_head_t wait
; /* Init & attention wait queue. */
37 struct idal_buffer
*rdbuf
; /* full-screen-deactivate buffer */
38 size_t rdbuf_size
; /* size of data returned by RDBUF */
42 fs3270_wake_up(struct raw3270_request
*rq
, void *data
)
44 wake_up((wait_queue_head_t
*) data
);
48 fs3270_working(struct fs3270
*fp
)
51 * The fullscreen view is in working order if the view
52 * has been activated AND the initial request is finished.
54 return fp
->active
&& raw3270_request_final(fp
->init
);
58 fs3270_do_io(struct raw3270_view
*view
, struct raw3270_request
*rq
)
63 fp
= (struct fs3270
*) view
;
64 rq
->callback
= fs3270_wake_up
;
65 rq
->callback_data
= &fp
->wait
;
68 if (!fs3270_working(fp
)) {
69 /* Fullscreen view isn't ready yet. */
70 rc
= wait_event_interruptible(fp
->wait
,
75 rc
= raw3270_start(view
, rq
);
77 /* Started sucessfully. Now wait for completion. */
78 wait_event(fp
->wait
, raw3270_request_final(rq
));
80 } while (rc
== -EACCES
);
85 * Switch to the fullscreen view.
88 fs3270_reset_callback(struct raw3270_request
*rq
, void *data
)
92 fp
= (struct fs3270
*) rq
->view
;
93 raw3270_request_reset(rq
);
98 fs3270_restore_callback(struct raw3270_request
*rq
, void *data
)
102 fp
= (struct fs3270
*) rq
->view
;
103 if (rq
->rc
!= 0 || rq
->rescnt
!= 0) {
105 kill_pid(fp
->fs_pid
, SIGHUP
, 1);
108 raw3270_request_reset(rq
);
113 fs3270_activate(struct raw3270_view
*view
)
119 fp
= (struct fs3270
*) view
;
121 /* If an old init command is still running just return. */
122 if (!raw3270_request_final(fp
->init
))
125 if (fp
->rdbuf_size
== 0) {
126 /* No saved buffer. Just clear the screen. */
127 raw3270_request_set_cmd(fp
->init
, TC_EWRITEA
);
128 fp
->init
->callback
= fs3270_reset_callback
;
130 /* Restore fullscreen buffer saved by fs3270_deactivate. */
131 raw3270_request_set_cmd(fp
->init
, TC_EWRITEA
);
132 raw3270_request_set_idal(fp
->init
, fp
->rdbuf
);
133 fp
->init
->ccw
.count
= fp
->rdbuf_size
;
134 cp
= fp
->rdbuf
->data
[0];
143 fp
->init
->rescnt
= 0;
144 fp
->init
->callback
= fs3270_restore_callback
;
146 rc
= fp
->init
->rc
= raw3270_start_locked(view
, fp
->init
);
148 fp
->init
->callback(fp
->init
, NULL
);
155 * Shutdown fullscreen view.
158 fs3270_save_callback(struct raw3270_request
*rq
, void *data
)
162 fp
= (struct fs3270
*) rq
->view
;
164 /* Correct idal buffer element 0 address. */
165 fp
->rdbuf
->data
[0] -= 5;
166 fp
->rdbuf
->size
+= 5;
169 * If the rdbuf command failed or the idal buffer is
170 * to small for the amount of data returned by the
171 * rdbuf command, then we have no choice but to send
172 * a SIGHUP to the application.
174 if (rq
->rc
!= 0 || rq
->rescnt
== 0) {
176 kill_pid(fp
->fs_pid
, SIGHUP
, 1);
179 fp
->rdbuf_size
= fp
->rdbuf
->size
- rq
->rescnt
;
180 raw3270_request_reset(rq
);
185 fs3270_deactivate(struct raw3270_view
*view
)
189 fp
= (struct fs3270
*) view
;
192 /* If an old init command is still running just return. */
193 if (!raw3270_request_final(fp
->init
))
196 /* Prepare read-buffer request. */
197 raw3270_request_set_cmd(fp
->init
, TC_RDBUF
);
199 * Hackish: skip first 5 bytes of the idal buffer to make
200 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
201 * in the activation command.
203 fp
->rdbuf
->data
[0] += 5;
204 fp
->rdbuf
->size
-= 5;
205 raw3270_request_set_idal(fp
->init
, fp
->rdbuf
);
206 fp
->init
->rescnt
= 0;
207 fp
->init
->callback
= fs3270_save_callback
;
209 /* Start I/O to read in the 3270 buffer. */
210 fp
->init
->rc
= raw3270_start_locked(view
, fp
->init
);
212 fp
->init
->callback(fp
->init
, NULL
);
216 fs3270_irq(struct fs3270
*fp
, struct raw3270_request
*rq
, struct irb
*irb
)
218 /* Handle ATTN. Set indication and wake waiters for attention. */
219 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_ATTENTION
) {
225 if (irb
->scsw
.cmd
.dstat
& DEV_STAT_UNIT_CHECK
)
228 /* Normal end. Copy residual count. */
229 rq
->rescnt
= irb
->scsw
.cmd
.count
;
231 return RAW3270_IO_DONE
;
235 * Process reads from fullscreen 3270.
238 fs3270_read(struct file
*filp
, char __user
*data
, size_t count
, loff_t
*off
)
241 struct raw3270_request
*rq
;
242 struct idal_buffer
*ib
;
245 if (count
== 0 || count
> 65535)
247 fp
= filp
->private_data
;
250 ib
= idal_buffer_alloc(count
, 0);
253 rq
= raw3270_request_alloc(0);
255 if (fp
->read_command
== 0 && fp
->write_command
!= 0)
256 fp
->read_command
= 6;
257 raw3270_request_set_cmd(rq
, fp
->read_command
? : 2);
258 raw3270_request_set_idal(rq
, ib
);
259 rc
= wait_event_interruptible(fp
->wait
, fp
->attention
);
262 rc
= fs3270_do_io(&fp
->view
, rq
);
265 if (idal_buffer_to_user(ib
, data
, count
) != 0)
272 raw3270_request_free(rq
);
275 idal_buffer_free(ib
);
280 * Process writes to fullscreen 3270.
283 fs3270_write(struct file
*filp
, const char __user
*data
, size_t count
, loff_t
*off
)
286 struct raw3270_request
*rq
;
287 struct idal_buffer
*ib
;
291 fp
= filp
->private_data
;
294 ib
= idal_buffer_alloc(count
, 0);
297 rq
= raw3270_request_alloc(0);
299 if (idal_buffer_from_user(ib
, data
, count
) == 0) {
300 write_command
= fp
->write_command
? : 1;
301 if (write_command
== 5)
303 raw3270_request_set_cmd(rq
, write_command
);
304 raw3270_request_set_idal(rq
, ib
);
305 rc
= fs3270_do_io(&fp
->view
, rq
);
307 rc
= count
- rq
->rescnt
;
310 raw3270_request_free(rq
);
313 idal_buffer_free(ib
);
318 * process ioctl commands for the tube driver
321 fs3270_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
324 struct raw3270_iocb iocb
;
327 fp
= filp
->private_data
;
334 fp
->read_command
= arg
;
337 fp
->write_command
= arg
;
340 rc
= put_user(fp
->read_command
, (char __user
*) arg
);
343 rc
= put_user(fp
->write_command
,(char __user
*) arg
);
346 iocb
.model
= fp
->view
.model
;
347 iocb
.line_cnt
= fp
->view
.rows
;
348 iocb
.col_cnt
= fp
->view
.cols
;
352 if (copy_to_user((char __user
*) arg
, &iocb
,
353 sizeof(struct raw3270_iocb
)))
362 * Allocate fs3270 structure.
364 static struct fs3270
*
365 fs3270_alloc_view(void)
369 fp
= kzalloc(sizeof(struct fs3270
),GFP_KERNEL
);
371 return ERR_PTR(-ENOMEM
);
372 fp
->init
= raw3270_request_alloc(0);
373 if (IS_ERR(fp
->init
)) {
375 return ERR_PTR(-ENOMEM
);
381 * Free fs3270 structure.
384 fs3270_free_view(struct raw3270_view
*view
)
388 fp
= (struct fs3270
*) view
;
390 idal_buffer_free(fp
->rdbuf
);
391 raw3270_request_free(((struct fs3270
*) view
)->init
);
396 * Unlink fs3270 data structure from filp.
399 fs3270_release(struct raw3270_view
*view
)
403 fp
= (struct fs3270
*) view
;
405 kill_pid(fp
->fs_pid
, SIGHUP
, 1);
408 /* View to a 3270 device. Can be console, tty or fullscreen. */
409 static struct raw3270_fn fs3270_fn
= {
410 .activate
= fs3270_activate
,
411 .deactivate
= fs3270_deactivate
,
412 .intv
= (void *) fs3270_irq
,
413 .release
= fs3270_release
,
414 .free
= fs3270_free_view
418 * This routine is called whenever a 3270 fullscreen device is opened.
421 fs3270_open(struct inode
*inode
, struct file
*filp
)
424 struct idal_buffer
*ib
;
427 if (imajor(filp
->f_path
.dentry
->d_inode
) != IBM_FS3270_MAJOR
)
429 minor
= iminor(filp
->f_path
.dentry
->d_inode
);
430 /* Check for minor 0 multiplexer. */
432 struct tty_struct
*tty
= get_current_tty();
433 if (!tty
|| tty
->driver
->major
!= IBM_TTY3270_MAJOR
) {
437 minor
= tty
->index
+ RAW3270_FIRSTMINOR
;
441 /* Check if some other program is already using fullscreen mode. */
442 fp
= (struct fs3270
*) raw3270_find_view(&fs3270_fn
, minor
);
444 raw3270_put_view(&fp
->view
);
448 /* Allocate fullscreen view structure. */
449 fp
= fs3270_alloc_view();
455 init_waitqueue_head(&fp
->wait
);
456 fp
->fs_pid
= get_pid(task_pid(current
));
457 rc
= raw3270_add_view(&fp
->view
, &fs3270_fn
, minor
);
459 fs3270_free_view(&fp
->view
);
463 /* Allocate idal-buffer. */
464 ib
= idal_buffer_alloc(2*fp
->view
.rows
*fp
->view
.cols
+ 5, 0);
466 raw3270_put_view(&fp
->view
);
467 raw3270_del_view(&fp
->view
);
473 rc
= raw3270_activate_view(&fp
->view
);
475 raw3270_put_view(&fp
->view
);
476 raw3270_del_view(&fp
->view
);
479 filp
->private_data
= fp
;
486 * This routine is called when the 3270 tty is closed. We wait
487 * for the remaining request to be completed. Then we clean up.
490 fs3270_close(struct inode
*inode
, struct file
*filp
)
494 fp
= filp
->private_data
;
495 filp
->private_data
= NULL
;
499 raw3270_reset(&fp
->view
);
500 raw3270_put_view(&fp
->view
);
501 raw3270_del_view(&fp
->view
);
506 static const struct file_operations fs3270_fops
= {
507 .owner
= THIS_MODULE
, /* owner */
508 .read
= fs3270_read
, /* read */
509 .write
= fs3270_write
, /* write */
510 .unlocked_ioctl
= fs3270_ioctl
, /* ioctl */
511 .compat_ioctl
= fs3270_ioctl
, /* ioctl */
512 .open
= fs3270_open
, /* open */
513 .release
= fs3270_close
, /* release */
517 * 3270 fullscreen driver initialization.
524 rc
= register_chrdev(IBM_FS3270_MAJOR
, "fs3270", &fs3270_fops
);
533 unregister_chrdev(IBM_FS3270_MAJOR
, "fs3270");
536 MODULE_LICENSE("GPL");
537 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR
);
539 module_init(fs3270_init
);
540 module_exit(fs3270_exit
);