fs3270.c 13 KB

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