0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/memblock.h>
0012 #include <linux/console.h>
0013 #include <linux/init.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/compat.h>
0016 #include <linux/sched/signal.h>
0017 #include <linux/module.h>
0018 #include <linux/list.h>
0019 #include <linux/slab.h>
0020 #include <linux/types.h>
0021
0022 #include <asm/ccwdev.h>
0023 #include <asm/cio.h>
0024 #include <asm/ebcdic.h>
0025 #include <asm/idals.h>
0026
0027 #include "raw3270.h"
0028 #include "ctrlchar.h"
0029
0030 static struct raw3270_fn fs3270_fn;
0031
0032 struct fs3270 {
0033 struct raw3270_view view;
0034 struct pid *fs_pid;
0035 int read_command;
0036 int write_command;
0037 int attention;
0038 int active;
0039 struct raw3270_request *init;
0040 wait_queue_head_t wait;
0041 struct idal_buffer *rdbuf;
0042 size_t rdbuf_size;
0043 };
0044
0045 static DEFINE_MUTEX(fs3270_mutex);
0046
0047 static void
0048 fs3270_wake_up(struct raw3270_request *rq, void *data)
0049 {
0050 wake_up((wait_queue_head_t *) data);
0051 }
0052
0053 static inline int
0054 fs3270_working(struct fs3270 *fp)
0055 {
0056
0057
0058
0059
0060 return fp->active && raw3270_request_final(fp->init);
0061 }
0062
0063 static int
0064 fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
0065 {
0066 struct fs3270 *fp;
0067 int rc;
0068
0069 fp = (struct fs3270 *) view;
0070 rq->callback = fs3270_wake_up;
0071 rq->callback_data = &fp->wait;
0072
0073 do {
0074 if (!fs3270_working(fp)) {
0075
0076 rc = wait_event_interruptible(fp->wait,
0077 fs3270_working(fp));
0078 if (rc != 0)
0079 break;
0080 }
0081 rc = raw3270_start(view, rq);
0082 if (rc == 0) {
0083
0084 wait_event(fp->wait, raw3270_request_final(rq));
0085 }
0086 } while (rc == -EACCES);
0087 return rc;
0088 }
0089
0090
0091
0092
0093 static void
0094 fs3270_reset_callback(struct raw3270_request *rq, void *data)
0095 {
0096 struct fs3270 *fp;
0097
0098 fp = (struct fs3270 *) rq->view;
0099 raw3270_request_reset(rq);
0100 wake_up(&fp->wait);
0101 }
0102
0103 static void
0104 fs3270_restore_callback(struct raw3270_request *rq, void *data)
0105 {
0106 struct fs3270 *fp;
0107
0108 fp = (struct fs3270 *) rq->view;
0109 if (rq->rc != 0 || rq->rescnt != 0) {
0110 if (fp->fs_pid)
0111 kill_pid(fp->fs_pid, SIGHUP, 1);
0112 }
0113 fp->rdbuf_size = 0;
0114 raw3270_request_reset(rq);
0115 wake_up(&fp->wait);
0116 }
0117
0118 static int
0119 fs3270_activate(struct raw3270_view *view)
0120 {
0121 struct fs3270 *fp;
0122 char *cp;
0123 int rc;
0124
0125 fp = (struct fs3270 *) view;
0126
0127
0128 if (!raw3270_request_final(fp->init))
0129 return 0;
0130
0131 if (fp->rdbuf_size == 0) {
0132
0133 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
0134 fp->init->callback = fs3270_reset_callback;
0135 } else {
0136
0137 raw3270_request_set_cmd(fp->init, TC_EWRITEA);
0138 raw3270_request_set_idal(fp->init, fp->rdbuf);
0139 fp->init->ccw.count = fp->rdbuf_size;
0140 cp = fp->rdbuf->data[0];
0141 cp[0] = TW_KR;
0142 cp[1] = TO_SBA;
0143 cp[2] = cp[6];
0144 cp[3] = cp[7];
0145 cp[4] = TO_IC;
0146 cp[5] = TO_SBA;
0147 cp[6] = 0x40;
0148 cp[7] = 0x40;
0149 fp->init->rescnt = 0;
0150 fp->init->callback = fs3270_restore_callback;
0151 }
0152 rc = fp->init->rc = raw3270_start_locked(view, fp->init);
0153 if (rc)
0154 fp->init->callback(fp->init, NULL);
0155 else
0156 fp->active = 1;
0157 return rc;
0158 }
0159
0160
0161
0162
0163 static void
0164 fs3270_save_callback(struct raw3270_request *rq, void *data)
0165 {
0166 struct fs3270 *fp;
0167
0168 fp = (struct fs3270 *) rq->view;
0169
0170
0171 fp->rdbuf->data[0] -= 5;
0172 fp->rdbuf->size += 5;
0173
0174
0175
0176
0177
0178
0179
0180 if (rq->rc != 0 || rq->rescnt == 0) {
0181 if (fp->fs_pid)
0182 kill_pid(fp->fs_pid, SIGHUP, 1);
0183 fp->rdbuf_size = 0;
0184 } else
0185 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
0186 raw3270_request_reset(rq);
0187 wake_up(&fp->wait);
0188 }
0189
0190 static void
0191 fs3270_deactivate(struct raw3270_view *view)
0192 {
0193 struct fs3270 *fp;
0194
0195 fp = (struct fs3270 *) view;
0196 fp->active = 0;
0197
0198
0199 if (!raw3270_request_final(fp->init))
0200 return;
0201
0202
0203 raw3270_request_set_cmd(fp->init, TC_RDBUF);
0204
0205
0206
0207
0208
0209 fp->rdbuf->data[0] += 5;
0210 fp->rdbuf->size -= 5;
0211 raw3270_request_set_idal(fp->init, fp->rdbuf);
0212 fp->init->rescnt = 0;
0213 fp->init->callback = fs3270_save_callback;
0214
0215
0216 fp->init->rc = raw3270_start_locked(view, fp->init);
0217 if (fp->init->rc)
0218 fp->init->callback(fp->init, NULL);
0219 }
0220
0221 static void
0222 fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
0223 {
0224
0225 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
0226 fp->attention = 1;
0227 wake_up(&fp->wait);
0228 }
0229
0230 if (rq) {
0231 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
0232 rq->rc = -EIO;
0233 else
0234
0235 rq->rescnt = irb->scsw.cmd.count;
0236 }
0237 }
0238
0239
0240
0241
0242 static ssize_t
0243 fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
0244 {
0245 struct fs3270 *fp;
0246 struct raw3270_request *rq;
0247 struct idal_buffer *ib;
0248 ssize_t rc;
0249
0250 if (count == 0 || count > 65535)
0251 return -EINVAL;
0252 fp = filp->private_data;
0253 if (!fp)
0254 return -ENODEV;
0255 ib = idal_buffer_alloc(count, 0);
0256 if (IS_ERR(ib))
0257 return -ENOMEM;
0258 rq = raw3270_request_alloc(0);
0259 if (!IS_ERR(rq)) {
0260 if (fp->read_command == 0 && fp->write_command != 0)
0261 fp->read_command = 6;
0262 raw3270_request_set_cmd(rq, fp->read_command ? : 2);
0263 raw3270_request_set_idal(rq, ib);
0264 rc = wait_event_interruptible(fp->wait, fp->attention);
0265 fp->attention = 0;
0266 if (rc == 0) {
0267 rc = fs3270_do_io(&fp->view, rq);
0268 if (rc == 0) {
0269 count -= rq->rescnt;
0270 if (idal_buffer_to_user(ib, data, count) != 0)
0271 rc = -EFAULT;
0272 else
0273 rc = count;
0274
0275 }
0276 }
0277 raw3270_request_free(rq);
0278 } else
0279 rc = PTR_ERR(rq);
0280 idal_buffer_free(ib);
0281 return rc;
0282 }
0283
0284
0285
0286
0287 static ssize_t
0288 fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
0289 {
0290 struct fs3270 *fp;
0291 struct raw3270_request *rq;
0292 struct idal_buffer *ib;
0293 int write_command;
0294 ssize_t rc;
0295
0296 fp = filp->private_data;
0297 if (!fp)
0298 return -ENODEV;
0299 ib = idal_buffer_alloc(count, 0);
0300 if (IS_ERR(ib))
0301 return -ENOMEM;
0302 rq = raw3270_request_alloc(0);
0303 if (!IS_ERR(rq)) {
0304 if (idal_buffer_from_user(ib, data, count) == 0) {
0305 write_command = fp->write_command ? : 1;
0306 if (write_command == 5)
0307 write_command = 13;
0308 raw3270_request_set_cmd(rq, write_command);
0309 raw3270_request_set_idal(rq, ib);
0310 rc = fs3270_do_io(&fp->view, rq);
0311 if (rc == 0)
0312 rc = count - rq->rescnt;
0313 } else
0314 rc = -EFAULT;
0315 raw3270_request_free(rq);
0316 } else
0317 rc = PTR_ERR(rq);
0318 idal_buffer_free(ib);
0319 return rc;
0320 }
0321
0322
0323
0324
0325 static long
0326 fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0327 {
0328 char __user *argp;
0329 struct fs3270 *fp;
0330 struct raw3270_iocb iocb;
0331 int rc;
0332
0333 fp = filp->private_data;
0334 if (!fp)
0335 return -ENODEV;
0336 if (is_compat_task())
0337 argp = compat_ptr(arg);
0338 else
0339 argp = (char __user *)arg;
0340 rc = 0;
0341 mutex_lock(&fs3270_mutex);
0342 switch (cmd) {
0343 case TUBICMD:
0344 fp->read_command = arg;
0345 break;
0346 case TUBOCMD:
0347 fp->write_command = arg;
0348 break;
0349 case TUBGETI:
0350 rc = put_user(fp->read_command, argp);
0351 break;
0352 case TUBGETO:
0353 rc = put_user(fp->write_command, argp);
0354 break;
0355 case TUBGETMOD:
0356 iocb.model = fp->view.model;
0357 iocb.line_cnt = fp->view.rows;
0358 iocb.col_cnt = fp->view.cols;
0359 iocb.pf_cnt = 24;
0360 iocb.re_cnt = 20;
0361 iocb.map = 0;
0362 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
0363 rc = -EFAULT;
0364 break;
0365 }
0366 mutex_unlock(&fs3270_mutex);
0367 return rc;
0368 }
0369
0370
0371
0372
0373 static struct fs3270 *
0374 fs3270_alloc_view(void)
0375 {
0376 struct fs3270 *fp;
0377
0378 fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
0379 if (!fp)
0380 return ERR_PTR(-ENOMEM);
0381 fp->init = raw3270_request_alloc(0);
0382 if (IS_ERR(fp->init)) {
0383 kfree(fp);
0384 return ERR_PTR(-ENOMEM);
0385 }
0386 return fp;
0387 }
0388
0389
0390
0391
0392 static void
0393 fs3270_free_view(struct raw3270_view *view)
0394 {
0395 struct fs3270 *fp;
0396
0397 fp = (struct fs3270 *) view;
0398 if (fp->rdbuf)
0399 idal_buffer_free(fp->rdbuf);
0400 raw3270_request_free(((struct fs3270 *) view)->init);
0401 kfree(view);
0402 }
0403
0404
0405
0406
0407 static void
0408 fs3270_release(struct raw3270_view *view)
0409 {
0410 struct fs3270 *fp;
0411
0412 fp = (struct fs3270 *) view;
0413 if (fp->fs_pid)
0414 kill_pid(fp->fs_pid, SIGHUP, 1);
0415 }
0416
0417
0418 static struct raw3270_fn fs3270_fn = {
0419 .activate = fs3270_activate,
0420 .deactivate = fs3270_deactivate,
0421 .intv = (void *) fs3270_irq,
0422 .release = fs3270_release,
0423 .free = fs3270_free_view
0424 };
0425
0426
0427
0428
0429 static int
0430 fs3270_open(struct inode *inode, struct file *filp)
0431 {
0432 struct fs3270 *fp;
0433 struct idal_buffer *ib;
0434 int minor, rc = 0;
0435
0436 if (imajor(file_inode(filp)) != IBM_FS3270_MAJOR)
0437 return -ENODEV;
0438 minor = iminor(file_inode(filp));
0439
0440 if (minor == 0) {
0441 struct tty_struct *tty = get_current_tty();
0442 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
0443 tty_kref_put(tty);
0444 return -ENODEV;
0445 }
0446 minor = tty->index;
0447 tty_kref_put(tty);
0448 }
0449 mutex_lock(&fs3270_mutex);
0450
0451 fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
0452 if (!IS_ERR(fp)) {
0453 raw3270_put_view(&fp->view);
0454 rc = -EBUSY;
0455 goto out;
0456 }
0457
0458 fp = fs3270_alloc_view();
0459 if (IS_ERR(fp)) {
0460 rc = PTR_ERR(fp);
0461 goto out;
0462 }
0463
0464 init_waitqueue_head(&fp->wait);
0465 fp->fs_pid = get_pid(task_pid(current));
0466 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor,
0467 RAW3270_VIEW_LOCK_BH);
0468 if (rc) {
0469 fs3270_free_view(&fp->view);
0470 goto out;
0471 }
0472
0473
0474 ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
0475 if (IS_ERR(ib)) {
0476 raw3270_put_view(&fp->view);
0477 raw3270_del_view(&fp->view);
0478 rc = PTR_ERR(ib);
0479 goto out;
0480 }
0481 fp->rdbuf = ib;
0482
0483 rc = raw3270_activate_view(&fp->view);
0484 if (rc) {
0485 raw3270_put_view(&fp->view);
0486 raw3270_del_view(&fp->view);
0487 goto out;
0488 }
0489 stream_open(inode, filp);
0490 filp->private_data = fp;
0491 out:
0492 mutex_unlock(&fs3270_mutex);
0493 return rc;
0494 }
0495
0496
0497
0498
0499
0500 static int
0501 fs3270_close(struct inode *inode, struct file *filp)
0502 {
0503 struct fs3270 *fp;
0504
0505 fp = filp->private_data;
0506 filp->private_data = NULL;
0507 if (fp) {
0508 put_pid(fp->fs_pid);
0509 fp->fs_pid = NULL;
0510 raw3270_reset(&fp->view);
0511 raw3270_put_view(&fp->view);
0512 raw3270_del_view(&fp->view);
0513 }
0514 return 0;
0515 }
0516
0517 static const struct file_operations fs3270_fops = {
0518 .owner = THIS_MODULE,
0519 .read = fs3270_read,
0520 .write = fs3270_write,
0521 .unlocked_ioctl = fs3270_ioctl,
0522 .compat_ioctl = fs3270_ioctl,
0523 .open = fs3270_open,
0524 .release = fs3270_close,
0525 .llseek = no_llseek,
0526 };
0527
0528 static void fs3270_create_cb(int minor)
0529 {
0530 __register_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub", &fs3270_fops);
0531 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, minor),
0532 NULL, "3270/tub%d", minor);
0533 }
0534
0535 static void fs3270_destroy_cb(int minor)
0536 {
0537 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, minor));
0538 __unregister_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub");
0539 }
0540
0541 static struct raw3270_notifier fs3270_notifier =
0542 {
0543 .create = fs3270_create_cb,
0544 .destroy = fs3270_destroy_cb,
0545 };
0546
0547
0548
0549
0550 static int __init
0551 fs3270_init(void)
0552 {
0553 int rc;
0554
0555 rc = __register_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270", &fs3270_fops);
0556 if (rc)
0557 return rc;
0558 device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, 0),
0559 NULL, "3270/tub");
0560 raw3270_register_notifier(&fs3270_notifier);
0561 return 0;
0562 }
0563
0564 static void __exit
0565 fs3270_exit(void)
0566 {
0567 raw3270_unregister_notifier(&fs3270_notifier);
0568 device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, 0));
0569 __unregister_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270");
0570 }
0571
0572 MODULE_LICENSE("GPL");
0573 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
0574
0575 module_init(fs3270_init);
0576 module_exit(fs3270_exit);