fs3270.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * IBM/3270 Driver - fullscreen driver.
  3. *
  4. * Author(s):
  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
  8. */
  9. #include <linux/bootmem.h>
  10. #include <linux/console.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/compat.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/module.h>
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <asm/compat.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/ebcdic.h>
  23. #include <asm/idals.h>
  24. #include "raw3270.h"
  25. #include "ctrlchar.h"
  26. static struct raw3270_fn fs3270_fn;
  27. struct fs3270 {
  28. struct raw3270_view view;
  29. struct pid *fs_pid; /* Pid of controlling program. */
  30. int read_command; /* ccw command to use for reads. */
  31. int write_command; /* ccw command to use for writes. */
  32. int attention; /* Got attention. */
  33. int active; /* Fullscreen view is active. */
  34. struct raw3270_request *init; /* single init request. */
  35. wait_queue_head_t wait; /* Init & attention wait queue. */
  36. struct idal_buffer *rdbuf; /* full-screen-deactivate buffer */
  37. size_t rdbuf_size; /* size of data returned by RDBUF */
  38. };
  39. static DEFINE_MUTEX(fs3270_mutex);
  40. static void
  41. fs3270_wake_up(struct raw3270_request *rq, void *data)
  42. {
  43. wake_up((wait_queue_head_t *) data);
  44. }
  45. static inline int
  46. fs3270_working(struct fs3270 *fp)
  47. {
  48. /*
  49. * The fullscreen view is in working order if the view
  50. * has been activated AND the initial request is finished.
  51. */
  52. return fp->active && raw3270_request_final(fp->init);
  53. }
  54. static int
  55. fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
  56. {
  57. struct fs3270 *fp;
  58. int rc;
  59. fp = (struct fs3270 *) view;
  60. rq->callback = fs3270_wake_up;
  61. rq->callback_data = &fp->wait;
  62. do {
  63. if (!fs3270_working(fp)) {
  64. /* Fullscreen view isn't ready yet. */
  65. rc = wait_event_interruptible(fp->wait,
  66. fs3270_working(fp));
  67. if (rc != 0)
  68. break;
  69. }
  70. rc = raw3270_start(view, rq);
  71. if (rc == 0) {
  72. /* Started successfully. Now wait for completion. */
  73. wait_event(fp->wait, raw3270_request_final(rq));
  74. }
  75. } while (rc == -EACCES);
  76. return rc;
  77. }
  78. /*
  79. * Switch to the fullscreen view.
  80. */
  81. static void
  82. fs3270_reset_callback(struct raw3270_request *rq, void *data)
  83. {
  84. struct fs3270 *fp;
  85. fp = (struct fs3270 *) rq->view;
  86. raw3270_request_reset(rq);
  87. wake_up(&fp->wait);
  88. }
  89. static void
  90. fs3270_restore_callback(struct raw3270_request *rq, void *data)
  91. {
  92. struct fs3270 *fp;
  93. fp = (struct fs3270 *) rq->view;
  94. if (rq->rc != 0 || rq->rescnt != 0) {
  95. if (fp->fs_pid)
  96. kill_pid(fp->fs_pid, SIGHUP, 1);
  97. }
  98. fp->rdbuf_size = 0;
  99. raw3270_request_reset(rq);
  100. wake_up(&fp->wait);
  101. }
  102. static int
  103. fs3270_activate(struct raw3270_view *view)
  104. {
  105. struct fs3270 *fp;
  106. char *cp;
  107. int rc;
  108. fp = (struct fs3270 *) view;
  109. /* If an old init command is still running just return. */
  110. if (!raw3270_request_final(fp->init))
  111. return 0;
  112. if (fp->rdbuf_size == 0) {
  113. /* No saved buffer. Just clear the screen. */
  114. raw3270_request_set_cmd(fp->init, TC_EWRITEA);
  115. fp->init->callback = fs3270_reset_callback;
  116. } else {
  117. /* Restore fullscreen buffer saved by fs3270_deactivate. */
  118. raw3270_request_set_cmd(fp->init, TC_EWRITEA);
  119. raw3270_request_set_idal(fp->init, fp->rdbuf);
  120. fp->init->ccw.count = fp->rdbuf_size;
  121. cp = fp->rdbuf->data[0];
  122. cp[0] = TW_KR;
  123. cp[1] = TO_SBA;
  124. cp[2] = cp[6];
  125. cp[3] = cp[7];
  126. cp[4] = TO_IC;
  127. cp[5] = TO_SBA;
  128. cp[6] = 0x40;
  129. cp[7] = 0x40;
  130. fp->init->rescnt = 0;
  131. fp->init->callback = fs3270_restore_callback;
  132. }
  133. rc = fp->init->rc = raw3270_start_locked(view, fp->init);
  134. if (rc)
  135. fp->init->callback(fp->init, NULL);
  136. else
  137. fp->active = 1;
  138. return rc;
  139. }
  140. /*
  141. * Shutdown fullscreen view.
  142. */
  143. static void
  144. fs3270_save_callback(struct raw3270_request *rq, void *data)
  145. {
  146. struct fs3270 *fp;
  147. fp = (struct fs3270 *) rq->view;
  148. /* Correct idal buffer element 0 address. */
  149. fp->rdbuf->data[0] -= 5;
  150. fp->rdbuf->size += 5;
  151. /*
  152. * If the rdbuf command failed or the idal buffer is
  153. * to small for the amount of data returned by the
  154. * rdbuf command, then we have no choice but to send
  155. * a SIGHUP to the application.
  156. */
  157. if (rq->rc != 0 || rq->rescnt == 0) {
  158. if (fp->fs_pid)
  159. kill_pid(fp->fs_pid, SIGHUP, 1);
  160. fp->rdbuf_size = 0;
  161. } else
  162. fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
  163. raw3270_request_reset(rq);
  164. wake_up(&fp->wait);
  165. }
  166. static void
  167. fs3270_deactivate(struct raw3270_view *view)
  168. {
  169. struct fs3270 *fp;
  170. fp = (struct fs3270 *) view;
  171. fp->active = 0;
  172. /* If an old init command is still running just return. */
  173. if (!raw3270_request_final(fp->init))
  174. return;
  175. /* Prepare read-buffer request. */
  176. raw3270_request_set_cmd(fp->init, TC_RDBUF);
  177. /*
  178. * Hackish: skip first 5 bytes of the idal buffer to make
  179. * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
  180. * in the activation command.
  181. */
  182. fp->rdbuf->data[0] += 5;
  183. fp->rdbuf->size -= 5;
  184. raw3270_request_set_idal(fp->init, fp->rdbuf);
  185. fp->init->rescnt = 0;
  186. fp->init->callback = fs3270_save_callback;
  187. /* Start I/O to read in the 3270 buffer. */
  188. fp->init->rc = raw3270_start_locked(view, fp->init);
  189. if (fp->init->rc)
  190. fp->init->callback(fp->init, NULL);
  191. }
  192. static void
  193. fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
  194. {
  195. /* Handle ATTN. Set indication and wake waiters for attention. */
  196. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  197. fp->attention = 1;
  198. wake_up(&fp->wait);
  199. }
  200. if (rq) {
  201. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  202. rq->rc = -EIO;
  203. else
  204. /* Normal end. Copy residual count. */
  205. rq->rescnt = irb->scsw.cmd.count;
  206. }
  207. }
  208. /*
  209. * Process reads from fullscreen 3270.
  210. */
  211. static ssize_t
  212. fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
  213. {
  214. struct fs3270 *fp;
  215. struct raw3270_request *rq;
  216. struct idal_buffer *ib;
  217. ssize_t rc;
  218. if (count == 0 || count > 65535)
  219. return -EINVAL;
  220. fp = filp->private_data;
  221. if (!fp)
  222. return -ENODEV;
  223. ib = idal_buffer_alloc(count, 0);
  224. if (IS_ERR(ib))
  225. return -ENOMEM;
  226. rq = raw3270_request_alloc(0);
  227. if (!IS_ERR(rq)) {
  228. if (fp->read_command == 0 && fp->write_command != 0)
  229. fp->read_command = 6;
  230. raw3270_request_set_cmd(rq, fp->read_command ? : 2);
  231. raw3270_request_set_idal(rq, ib);
  232. rc = wait_event_interruptible(fp->wait, fp->attention);
  233. fp->attention = 0;
  234. if (rc == 0) {
  235. rc = fs3270_do_io(&fp->view, rq);
  236. if (rc == 0) {
  237. count -= rq->rescnt;
  238. if (idal_buffer_to_user(ib, data, count) != 0)
  239. rc = -EFAULT;
  240. else
  241. rc = count;
  242. }
  243. }
  244. raw3270_request_free(rq);
  245. } else
  246. rc = PTR_ERR(rq);
  247. idal_buffer_free(ib);
  248. return rc;
  249. }
  250. /*
  251. * Process writes to fullscreen 3270.
  252. */
  253. static ssize_t
  254. fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
  255. {
  256. struct fs3270 *fp;
  257. struct raw3270_request *rq;
  258. struct idal_buffer *ib;
  259. int write_command;
  260. ssize_t rc;
  261. fp = filp->private_data;
  262. if (!fp)
  263. return -ENODEV;
  264. ib = idal_buffer_alloc(count, 0);
  265. if (IS_ERR(ib))
  266. return -ENOMEM;
  267. rq = raw3270_request_alloc(0);
  268. if (!IS_ERR(rq)) {
  269. if (idal_buffer_from_user(ib, data, count) == 0) {
  270. write_command = fp->write_command ? : 1;
  271. if (write_command == 5)
  272. write_command = 13;
  273. raw3270_request_set_cmd(rq, write_command);
  274. raw3270_request_set_idal(rq, ib);
  275. rc = fs3270_do_io(&fp->view, rq);
  276. if (rc == 0)
  277. rc = count - rq->rescnt;
  278. } else
  279. rc = -EFAULT;
  280. raw3270_request_free(rq);
  281. } else
  282. rc = PTR_ERR(rq);
  283. idal_buffer_free(ib);
  284. return rc;
  285. }
  286. /*
  287. * process ioctl commands for the tube driver
  288. */
  289. static long
  290. fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  291. {
  292. char __user *argp;
  293. struct fs3270 *fp;
  294. struct raw3270_iocb iocb;
  295. int rc;
  296. fp = filp->private_data;
  297. if (!fp)
  298. return -ENODEV;
  299. if (is_compat_task())
  300. argp = compat_ptr(arg);
  301. else
  302. argp = (char __user *)arg;
  303. rc = 0;
  304. mutex_lock(&fs3270_mutex);
  305. switch (cmd) {
  306. case TUBICMD:
  307. fp->read_command = arg;
  308. break;
  309. case TUBOCMD:
  310. fp->write_command = arg;
  311. break;
  312. case TUBGETI:
  313. rc = put_user(fp->read_command, argp);
  314. break;
  315. case TUBGETO:
  316. rc = put_user(fp->write_command, argp);
  317. break;
  318. case TUBGETMOD:
  319. iocb.model = fp->view.model;
  320. iocb.line_cnt = fp->view.rows;
  321. iocb.col_cnt = fp->view.cols;
  322. iocb.pf_cnt = 24;
  323. iocb.re_cnt = 20;
  324. iocb.map = 0;
  325. if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
  326. rc = -EFAULT;
  327. break;
  328. }
  329. mutex_unlock(&fs3270_mutex);
  330. return rc;
  331. }
  332. /*
  333. * Allocate fs3270 structure.
  334. */
  335. static struct fs3270 *
  336. fs3270_alloc_view(void)
  337. {
  338. struct fs3270 *fp;
  339. fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
  340. if (!fp)
  341. return ERR_PTR(-ENOMEM);
  342. fp->init = raw3270_request_alloc(0);
  343. if (IS_ERR(fp->init)) {
  344. kfree(fp);
  345. return ERR_PTR(-ENOMEM);
  346. }
  347. return fp;
  348. }
  349. /*
  350. * Free fs3270 structure.
  351. */
  352. static void
  353. fs3270_free_view(struct raw3270_view *view)
  354. {
  355. struct fs3270 *fp;
  356. fp = (struct fs3270 *) view;
  357. if (fp->rdbuf)
  358. idal_buffer_free(fp->rdbuf);
  359. raw3270_request_free(((struct fs3270 *) view)->init);
  360. kfree(view);
  361. }
  362. /*
  363. * Unlink fs3270 data structure from filp.
  364. */
  365. static void
  366. fs3270_release(struct raw3270_view *view)
  367. {
  368. struct fs3270 *fp;
  369. fp = (struct fs3270 *) view;
  370. if (fp->fs_pid)
  371. kill_pid(fp->fs_pid, SIGHUP, 1);
  372. }
  373. /* View to a 3270 device. Can be console, tty or fullscreen. */
  374. static struct raw3270_fn fs3270_fn = {
  375. .activate = fs3270_activate,
  376. .deactivate = fs3270_deactivate,
  377. .intv = (void *) fs3270_irq,
  378. .release = fs3270_release,
  379. .free = fs3270_free_view
  380. };
  381. /*
  382. * This routine is called whenever a 3270 fullscreen device is opened.
  383. */
  384. static int
  385. fs3270_open(struct inode *inode, struct file *filp)
  386. {
  387. struct fs3270 *fp;
  388. struct idal_buffer *ib;
  389. int minor, rc = 0;
  390. if (imajor(file_inode(filp)) != IBM_FS3270_MAJOR)
  391. return -ENODEV;
  392. minor = iminor(file_inode(filp));
  393. /* Check for minor 0 multiplexer. */
  394. if (minor == 0) {
  395. struct tty_struct *tty = get_current_tty();
  396. if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
  397. tty_kref_put(tty);
  398. return -ENODEV;
  399. }
  400. minor = tty->index;
  401. tty_kref_put(tty);
  402. }
  403. mutex_lock(&fs3270_mutex);
  404. /* Check if some other program is already using fullscreen mode. */
  405. fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
  406. if (!IS_ERR(fp)) {
  407. raw3270_put_view(&fp->view);
  408. rc = -EBUSY;
  409. goto out;
  410. }
  411. /* Allocate fullscreen view structure. */
  412. fp = fs3270_alloc_view();
  413. if (IS_ERR(fp)) {
  414. rc = PTR_ERR(fp);
  415. goto out;
  416. }
  417. init_waitqueue_head(&fp->wait);
  418. fp->fs_pid = get_pid(task_pid(current));
  419. rc = raw3270_add_view(&fp->view, &fs3270_fn, minor,
  420. RAW3270_VIEW_LOCK_BH);
  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);