1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * IBM/3270 Driver - fullscreen driver. 4 * 5 * Author(s): 6 * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global) 7 * Rewritten for 2.5/2.6 by Martin Schwidefsky <schwidefsky@de.ibm.com> 8 * Copyright IBM Corp. 2003, 2009 9 */ 10 11 #include <linux/memblock.h> 12 #include <linux/console.h> 13 #include <linux/init.h> 14 #include <linux/interrupt.h> 15 #include <linux/sched/signal.h> 16 #include <linux/module.h> 17 #include <linux/list.h> 18 #include <linux/slab.h> 19 #include <linux/types.h> 20 21 #include <uapi/asm/fs3270.h> 22 #include <asm/ccwdev.h> 23 #include <asm/cio.h> 24 #include <asm/ebcdic.h> 25 #include <asm/idals.h> 26 27 #include "raw3270.h" 28 #include "ctrlchar.h" 29 30 static struct raw3270_fn fs3270_fn; 31 32 struct fs3270 { 33 struct raw3270_view view; 34 struct pid *fs_pid; /* Pid of controlling program. */ 35 int read_command; /* ccw command to use for reads. */ 36 int write_command; /* ccw command to use for writes. */ 37 int attention; /* Got attention. */ 38 int active; /* Fullscreen view is active. */ 39 struct raw3270_request *init; /* single init request. */ 40 wait_queue_head_t wait; /* Init & attention wait queue. */ 41 struct idal_buffer *rdbuf; /* full-screen-deactivate buffer */ 42 size_t rdbuf_size; /* size of data returned by RDBUF */ 43 }; 44 45 static DEFINE_MUTEX(fs3270_mutex); 46 47 static void fs3270_wake_up(struct raw3270_request *rq, void *data) 48 { 49 wake_up((wait_queue_head_t *)data); 50 } 51 52 static inline int fs3270_working(struct fs3270 *fp) 53 { 54 /* 55 * The fullscreen view is in working order if the view 56 * has been activated AND the initial request is finished. 57 */ 58 return fp->active && raw3270_request_final(fp->init); 59 } 60 61 static int fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq) 62 { 63 struct fs3270 *fp; 64 int rc; 65 66 fp = (struct fs3270 *)view; 67 rq->callback = fs3270_wake_up; 68 rq->callback_data = &fp->wait; 69 70 do { 71 if (!fs3270_working(fp)) { 72 /* Fullscreen view isn't ready yet. */ 73 rc = wait_event_interruptible(fp->wait, 74 fs3270_working(fp)); 75 if (rc != 0) 76 break; 77 } 78 rc = raw3270_start(view, rq); 79 if (rc == 0) { 80 /* Started successfully. Now wait for completion. */ 81 wait_event(fp->wait, raw3270_request_final(rq)); 82 } 83 } while (rc == -EACCES); 84 return rc; 85 } 86 87 /* 88 * Switch to the fullscreen view. 89 */ 90 static void fs3270_reset_callback(struct raw3270_request *rq, void *data) 91 { 92 struct fs3270 *fp; 93 94 fp = (struct fs3270 *)rq->view; 95 raw3270_request_reset(rq); 96 wake_up(&fp->wait); 97 } 98 99 static void fs3270_restore_callback(struct raw3270_request *rq, void *data) 100 { 101 struct fs3270 *fp; 102 103 fp = (struct fs3270 *)rq->view; 104 if (rq->rc != 0 || rq->rescnt != 0) { 105 if (fp->fs_pid) 106 kill_pid(fp->fs_pid, SIGHUP, 1); 107 } 108 fp->rdbuf_size = 0; 109 raw3270_request_reset(rq); 110 wake_up(&fp->wait); 111 } 112 113 static int fs3270_activate(struct raw3270_view *view) 114 { 115 struct fs3270 *fp; 116 char *cp; 117 int rc; 118 119 fp = (struct fs3270 *)view; 120 121 /* If an old init command is still running just return. */ 122 if (!raw3270_request_final(fp->init)) 123 return 0; 124 125 raw3270_request_set_cmd(fp->init, TC_EWRITEA); 126 raw3270_request_set_idal(fp->init, fp->rdbuf); 127 fp->init->rescnt = 0; 128 cp = dma64_to_virt(fp->rdbuf->data[0]); 129 if (fp->rdbuf_size == 0) { 130 /* No saved buffer. Just clear the screen. */ 131 fp->init->ccw.count = 1; 132 fp->init->callback = fs3270_reset_callback; 133 cp[0] = 0; 134 } else { 135 /* Restore fullscreen buffer saved by fs3270_deactivate. */ 136 fp->init->ccw.count = fp->rdbuf_size; 137 fp->init->callback = fs3270_restore_callback; 138 cp[0] = TW_KR; 139 cp[1] = TO_SBA; 140 cp[2] = cp[6]; 141 cp[3] = cp[7]; 142 cp[4] = TO_IC; 143 cp[5] = TO_SBA; 144 cp[6] = 0x40; 145 cp[7] = 0x40; 146 } 147 rc = raw3270_start_locked(view, fp->init); 148 fp->init->rc = rc; 149 if (rc) 150 fp->init->callback(fp->init, NULL); 151 else 152 fp->active = 1; 153 return rc; 154 } 155 156 /* 157 * Shutdown fullscreen view. 158 */ 159 static void fs3270_save_callback(struct raw3270_request *rq, void *data) 160 { 161 struct fs3270 *fp; 162 163 fp = (struct fs3270 *)rq->view; 164 165 /* Correct idal buffer element 0 address. */ 166 fp->rdbuf->data[0] = dma64_add(fp->rdbuf->data[0], -5); 167 fp->rdbuf->size += 5; 168 169 /* 170 * If the rdbuf command failed or the idal buffer is 171 * to small for the amount of data returned by the 172 * rdbuf command, then we have no choice but to send 173 * a SIGHUP to the application. 174 */ 175 if (rq->rc != 0 || rq->rescnt == 0) { 176 if (fp->fs_pid) 177 kill_pid(fp->fs_pid, SIGHUP, 1); 178 fp->rdbuf_size = 0; 179 } else { 180 fp->rdbuf_size = fp->rdbuf->size - rq->rescnt; 181 } 182 raw3270_request_reset(rq); 183 wake_up(&fp->wait); 184 } 185 186 static void fs3270_deactivate(struct raw3270_view *view) 187 { 188 struct fs3270 *fp; 189 190 fp = (struct fs3270 *)view; 191 fp->active = 0; 192 193 /* If an old init command is still running just return. */ 194 if (!raw3270_request_final(fp->init)) 195 return; 196 197 /* Prepare read-buffer request. */ 198 raw3270_request_set_cmd(fp->init, TC_RDBUF); 199 /* 200 * Hackish: skip first 5 bytes of the idal buffer to make 201 * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence 202 * in the activation command. 203 */ 204 fp->rdbuf->data[0] = dma64_add(fp->rdbuf->data[0], 5); 205 fp->rdbuf->size -= 5; 206 raw3270_request_set_idal(fp->init, fp->rdbuf); 207 fp->init->rescnt = 0; 208 fp->init->callback = fs3270_save_callback; 209 210 /* Start I/O to read in the 3270 buffer. */ 211 fp->init->rc = raw3270_start_locked(view, fp->init); 212 if (fp->init->rc) 213 fp->init->callback(fp->init, NULL); 214 } 215 216 static void fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, 217 struct irb *irb) 218 { 219 /* Handle ATTN. Set indication and wake waiters for attention. */ 220 if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) { 221 fp->attention = 1; 222 wake_up(&fp->wait); 223 } 224 225 if (rq) { 226 if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) 227 rq->rc = -EIO; 228 else 229 /* Normal end. Copy residual count. */ 230 rq->rescnt = irb->scsw.cmd.count; 231 } 232 } 233 234 /* 235 * Process reads from fullscreen 3270. 236 */ 237 static ssize_t fs3270_read(struct file *filp, char __user *data, 238 size_t count, loff_t *off) 239 { 240 struct fs3270 *fp; 241 struct raw3270_request *rq; 242 struct idal_buffer *ib; 243 ssize_t rc; 244 245 if (count == 0 || count > 65535) 246 return -EINVAL; 247 fp = filp->private_data; 248 if (!fp) 249 return -ENODEV; 250 ib = idal_buffer_alloc(count, 0); 251 if (IS_ERR(ib)) 252 return -ENOMEM; 253 rq = raw3270_request_alloc(0); 254 if (!IS_ERR(rq)) { 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); 260 fp->attention = 0; 261 if (rc == 0) { 262 rc = fs3270_do_io(&fp->view, rq); 263 if (rc == 0) { 264 count -= rq->rescnt; 265 if (idal_buffer_to_user(ib, data, count) != 0) 266 rc = -EFAULT; 267 else 268 rc = count; 269 } 270 } 271 raw3270_request_free(rq); 272 } else { 273 rc = PTR_ERR(rq); 274 } 275 idal_buffer_free(ib); 276 return rc; 277 } 278 279 /* 280 * Process writes to fullscreen 3270. 281 */ 282 static ssize_t fs3270_write(struct file *filp, const char __user *data, 283 size_t count, loff_t *off) 284 { 285 struct fs3270 *fp; 286 struct raw3270_request *rq; 287 struct idal_buffer *ib; 288 int write_command; 289 ssize_t rc; 290 291 fp = filp->private_data; 292 if (!fp) 293 return -ENODEV; 294 ib = idal_buffer_alloc(count, 0); 295 if (IS_ERR(ib)) 296 return -ENOMEM; 297 rq = raw3270_request_alloc(0); 298 if (!IS_ERR(rq)) { 299 if (idal_buffer_from_user(ib, data, count) == 0) { 300 write_command = fp->write_command ? : 1; 301 if (write_command == 5) 302 write_command = 13; 303 raw3270_request_set_cmd(rq, write_command); 304 raw3270_request_set_idal(rq, ib); 305 rc = fs3270_do_io(&fp->view, rq); 306 if (rc == 0) 307 rc = count - rq->rescnt; 308 } else { 309 rc = -EFAULT; 310 } 311 raw3270_request_free(rq); 312 } else { 313 rc = PTR_ERR(rq); 314 } 315 idal_buffer_free(ib); 316 return rc; 317 } 318 319 /* 320 * process ioctl commands for the tube driver 321 */ 322 static long fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 323 { 324 char __user *argp; 325 struct fs3270 *fp; 326 struct raw3270_iocb iocb; 327 int rc; 328 329 fp = filp->private_data; 330 if (!fp) 331 return -ENODEV; 332 argp = (char __user *)arg; 333 rc = 0; 334 mutex_lock(&fs3270_mutex); 335 switch (cmd) { 336 case TUBICMD: 337 fp->read_command = arg; 338 break; 339 case TUBOCMD: 340 fp->write_command = arg; 341 break; 342 case TUBGETI: 343 rc = put_user(fp->read_command, argp); 344 break; 345 case TUBGETO: 346 rc = put_user(fp->write_command, argp); 347 break; 348 case TUBGETMOD: 349 iocb.model = fp->view.model; 350 iocb.line_cnt = fp->view.rows; 351 iocb.col_cnt = fp->view.cols; 352 iocb.pf_cnt = 24; 353 iocb.re_cnt = 20; 354 iocb.map = 0; 355 if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb))) 356 rc = -EFAULT; 357 break; 358 } 359 mutex_unlock(&fs3270_mutex); 360 return rc; 361 } 362 363 /* 364 * Allocate fs3270 structure. 365 */ 366 static struct fs3270 *fs3270_alloc_view(void) 367 { 368 struct fs3270 *fp; 369 370 fp = kzalloc(sizeof(*fp), GFP_KERNEL); 371 if (!fp) 372 return ERR_PTR(-ENOMEM); 373 fp->init = raw3270_request_alloc(0); 374 if (IS_ERR(fp->init)) { 375 kfree(fp); 376 return ERR_PTR(-ENOMEM); 377 } 378 return fp; 379 } 380 381 /* 382 * Free fs3270 structure. 383 */ 384 static void fs3270_free_view(struct raw3270_view *view) 385 { 386 struct fs3270 *fp; 387 388 fp = (struct fs3270 *)view; 389 if (fp->rdbuf) 390 idal_buffer_free(fp->rdbuf); 391 raw3270_request_free(((struct fs3270 *)view)->init); 392 kfree(view); 393 } 394 395 /* 396 * Unlink fs3270 data structure from filp. 397 */ 398 static void fs3270_release(struct raw3270_view *view) 399 { 400 struct fs3270 *fp; 401 402 fp = (struct fs3270 *)view; 403 if (fp->fs_pid) 404 kill_pid(fp->fs_pid, SIGHUP, 1); 405 } 406 407 /* View to a 3270 device. Can be console, tty or fullscreen. */ 408 static struct raw3270_fn fs3270_fn = { 409 .activate = fs3270_activate, 410 .deactivate = fs3270_deactivate, 411 .intv = (void *)fs3270_irq, 412 .release = fs3270_release, 413 .free = fs3270_free_view 414 }; 415 416 /* 417 * This routine is called whenever a 3270 fullscreen device is opened. 418 */ 419 static int fs3270_open(struct inode *inode, struct file *filp) 420 { 421 struct fs3270 *fp; 422 struct idal_buffer *ib; 423 int minor, rc = 0; 424 425 if (imajor(file_inode(filp)) != IBM_FS3270_MAJOR) 426 return -ENODEV; 427 minor = iminor(file_inode(filp)); 428 /* Check for minor 0 multiplexer. */ 429 if (minor == 0) { 430 struct tty_struct *tty = get_current_tty(); 431 432 if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) { 433 tty_kref_put(tty); 434 return -ENODEV; 435 } 436 minor = tty->index; 437 tty_kref_put(tty); 438 } 439 mutex_lock(&fs3270_mutex); 440 /* Check if some other program is already using fullscreen mode. */ 441 fp = (struct fs3270 *)raw3270_find_view(&fs3270_fn, minor); 442 if (!IS_ERR(fp)) { 443 raw3270_put_view(&fp->view); 444 rc = -EBUSY; 445 goto out; 446 } 447 /* Allocate fullscreen view structure. */ 448 fp = fs3270_alloc_view(); 449 if (IS_ERR(fp)) { 450 rc = PTR_ERR(fp); 451 goto out; 452 } 453 454 init_waitqueue_head(&fp->wait); 455 fp->fs_pid = get_pid(task_pid(current)); 456 rc = raw3270_add_view(&fp->view, &fs3270_fn, minor, 457 RAW3270_VIEW_LOCK_BH); 458 if (rc) { 459 fs3270_free_view(&fp->view); 460 goto out; 461 } 462 463 /* Allocate idal-buffer. */ 464 ib = idal_buffer_alloc(2 * fp->view.rows * fp->view.cols + 5, 0); 465 if (IS_ERR(ib)) { 466 raw3270_put_view(&fp->view); 467 raw3270_del_view(&fp->view); 468 rc = PTR_ERR(ib); 469 goto out; 470 } 471 fp->rdbuf = ib; 472 473 rc = raw3270_activate_view(&fp->view); 474 if (rc) { 475 raw3270_put_view(&fp->view); 476 raw3270_del_view(&fp->view); 477 goto out; 478 } 479 stream_open(inode, filp); 480 filp->private_data = fp; 481 out: 482 mutex_unlock(&fs3270_mutex); 483 return rc; 484 } 485 486 /* 487 * This routine is called when the 3270 tty is closed. We wait 488 * for the remaining request to be completed. Then we clean up. 489 */ 490 static int fs3270_close(struct inode *inode, struct file *filp) 491 { 492 struct fs3270 *fp; 493 494 fp = filp->private_data; 495 filp->private_data = NULL; 496 if (fp) { 497 put_pid(fp->fs_pid); 498 fp->fs_pid = NULL; 499 raw3270_reset(&fp->view); 500 raw3270_put_view(&fp->view); 501 raw3270_del_view(&fp->view); 502 } 503 return 0; 504 } 505 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 .open = fs3270_open, /* open */ 512 .release = fs3270_close, /* release */ 513 }; 514 515 static void fs3270_create_cb(int minor) 516 { 517 __register_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub", &fs3270_fops); 518 device_create(&class3270, NULL, MKDEV(IBM_FS3270_MAJOR, minor), 519 NULL, "3270/tub%d", minor); 520 } 521 522 static void fs3270_destroy_cb(int minor) 523 { 524 device_destroy(&class3270, MKDEV(IBM_FS3270_MAJOR, minor)); 525 __unregister_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub"); 526 } 527 528 static struct raw3270_notifier fs3270_notifier = { 529 .create = fs3270_create_cb, 530 .destroy = fs3270_destroy_cb, 531 }; 532 533 /* 534 * 3270 fullscreen driver initialization. 535 */ 536 static int __init fs3270_init(void) 537 { 538 int rc; 539 540 rc = __register_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270", &fs3270_fops); 541 if (rc) 542 return rc; 543 device_create(&class3270, NULL, MKDEV(IBM_FS3270_MAJOR, 0), 544 NULL, "3270/tub"); 545 raw3270_register_notifier(&fs3270_notifier); 546 return 0; 547 } 548 549 static void __exit fs3270_exit(void) 550 { 551 raw3270_unregister_notifier(&fs3270_notifier); 552 device_destroy(&class3270, MKDEV(IBM_FS3270_MAJOR, 0)); 553 __unregister_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270"); 554 } 555 556 MODULE_DESCRIPTION("IBM/3270 Driver - fullscreen driver"); 557 MODULE_LICENSE("GPL"); 558 MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR); 559 560 module_init(fs3270_init); 561 module_exit(fs3270_exit); 562