fs3270.c 13 KB

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