fs3270.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/module.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <asm/compat.h>
  19. #include <asm/ccwdev.h>
  20. #include <asm/cio.h>
  21. #include <asm/ebcdic.h>
  22. #include <asm/idals.h>
  23. #include "raw3270.h"
  24. #include "ctrlchar.h"
  25. static struct raw3270_fn fs3270_fn;
  26. struct fs3270 {
  27. struct raw3270_view view;
  28. struct pid *fs_pid; /* Pid of controlling program. */
  29. int read_command; /* ccw command to use for reads. */
  30. int write_command; /* ccw command to use for writes. */
  31. int attention; /* Got attention. */
  32. int active; /* Fullscreen view is active. */
  33. struct raw3270_request *init; /* single init request. */
  34. wait_queue_head_t wait; /* Init & attention wait queue. */
  35. struct idal_buffer *rdbuf; /* full-screen-deactivate buffer */
  36. size_t rdbuf_size; /* size of data returned by RDBUF */
  37. };
  38. static DEFINE_MUTEX(fs3270_mutex);
  39. static void
  40. fs3270_wake_up(struct raw3270_request *rq, void *data)
  41. {
  42. wake_up((wait_queue_head_t *) data);
  43. }
  44. static inline int
  45. 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
  54. fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
  55. {
  56. struct fs3270 *fp;
  57. int rc;
  58. fp = (struct fs3270 *) view;
  59. rq->callback = fs3270_wake_up;
  60. rq->callback_data = &fp->wait;
  61. do {
  62. if (!fs3270_working(fp)) {
  63. /* Fullscreen view isn't ready yet. */
  64. rc = wait_event_interruptible(fp->wait,
  65. fs3270_working(fp));
  66. if (rc != 0)
  67. break;
  68. }
  69. rc = raw3270_start(view, rq);
  70. if (rc == 0) {
  71. /* Started successfully. Now wait for completion. */
  72. wait_event(fp->wait, raw3270_request_final(rq));
  73. }
  74. } while (rc == -EACCES);
  75. return rc;
  76. }
  77. /*
  78. * Switch to the fullscreen view.
  79. */
  80. static void
  81. fs3270_reset_callback(struct raw3270_request *rq, void *data)
  82. {
  83. struct fs3270 *fp;
  84. fp = (struct fs3270 *) rq->view;
  85. raw3270_request_reset(rq);
  86. wake_up(&fp->wait);
  87. }
  88. static void
  89. fs3270_restore_callback(struct raw3270_request *rq, void *data)
  90. {
  91. struct fs3270 *fp;
  92. fp = (struct fs3270 *) rq->view;
  93. if (rq->rc != 0 || rq->rescnt != 0) {
  94. if (fp->fs_pid)
  95. kill_pid(fp->fs_pid, SIGHUP, 1);
  96. }
  97. fp->rdbuf_size = 0;
  98. raw3270_request_reset(rq);
  99. wake_up(&fp->wait);
  100. }
  101. static int
  102. fs3270_activate(struct raw3270_view *view)
  103. {
  104. struct fs3270 *fp;
  105. char *cp;
  106. int rc;
  107. fp = (struct fs3270 *) view;
  108. /* If an old init command is still running just return. */
  109. if (!raw3270_request_final(fp->init))
  110. return 0;
  111. if (fp->rdbuf_size == 0) {
  112. /* No saved buffer. Just clear the screen. */
  113. raw3270_request_set_cmd(fp->init, TC_EWRITEA);
  114. fp->init->callback = fs3270_reset_callback;
  115. } else {
  116. /* Restore fullscreen buffer saved by fs3270_deactivate. */
  117. raw3270_request_set_cmd(fp->init, TC_EWRITEA);
  118. raw3270_request_set_idal(fp->init, fp->rdbuf);
  119. fp->init->ccw.count = fp->rdbuf_size;
  120. cp = fp->rdbuf->data[0];
  121. cp[0] = TW_KR;
  122. cp[1] = TO_SBA;
  123. cp[2] = cp[6];
  124. cp[3] = cp[7];
  125. cp[4] = TO_IC;
  126. cp[5] = TO_SBA;
  127. cp[6] = 0x40;
  128. cp[7] = 0x40;
  129. fp->init->rescnt = 0;
  130. fp->init->callback = fs3270_restore_callback;
  131. }
  132. rc = fp->init->rc = raw3270_start_locked(view, fp->init);
  133. if (rc)
  134. fp->init->callback(fp->init, NULL);
  135. else
  136. fp->active = 1;
  137. return rc;
  138. }
  139. /*
  140. * Shutdown fullscreen view.
  141. */
  142. static void
  143. fs3270_save_callback(struct raw3270_request *rq, void *data)
  144. {
  145. struct fs3270 *fp;
  146. fp = (struct fs3270 *) rq->view;
  147. /* Correct idal buffer element 0 address. */
  148. fp->rdbuf->data[0] -= 5;
  149. fp->rdbuf->size += 5;
  150. /*
  151. * If the rdbuf command failed or the idal buffer is
  152. * to small for the amount of data returned by the
  153. * rdbuf command, then we have no choice but to send
  154. * a SIGHUP to the application.
  155. */
  156. if (rq->rc != 0 || rq->rescnt == 0) {
  157. if (fp->fs_pid)
  158. kill_pid(fp->fs_pid, SIGHUP, 1);
  159. fp->rdbuf_size = 0;
  160. } else
  161. fp->rdbuf_size = fp->rdbuf->size - rq->rescnt;
  162. raw3270_request_reset(rq);
  163. wake_up(&fp->wait);
  164. }
  165. static void
  166. fs3270_deactivate(struct raw3270_view *view)
  167. {
  168. struct fs3270 *fp;
  169. fp = (struct fs3270 *) view;
  170. fp->active = 0;
  171. /* If an old init command is still running just return. */
  172. if (!raw3270_request_final(fp->init))
  173. return;
  174. /* Prepare read-buffer request. */
  175. raw3270_request_set_cmd(fp->init, TC_RDBUF);
  176. /*
  177. * Hackish: skip first 5 bytes of the idal buffer to make
  178. * room for the TW_KR/TO_SBA/<address>/<address>/TO_IC sequence
  179. * in the activation command.
  180. */
  181. fp->rdbuf->data[0] += 5;
  182. fp->rdbuf->size -= 5;
  183. raw3270_request_set_idal(fp->init, fp->rdbuf);
  184. fp->init->rescnt = 0;
  185. fp->init->callback = fs3270_save_callback;
  186. /* Start I/O to read in the 3270 buffer. */
  187. fp->init->rc = raw3270_start_locked(view, fp->init);
  188. if (fp->init->rc)
  189. fp->init->callback(fp->init, NULL);
  190. }
  191. static int
  192. fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
  193. {
  194. /* Handle ATTN. Set indication and wake waiters for attention. */
  195. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  196. fp->attention = 1;
  197. wake_up(&fp->wait);
  198. }
  199. if (rq) {
  200. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)
  201. rq->rc = -EIO;
  202. else
  203. /* Normal end. Copy residual count. */
  204. rq->rescnt = irb->scsw.cmd.count;
  205. }
  206. return RAW3270_IO_DONE;
  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. if (rc) {
  421. fs3270_free_view(&fp->view);
  422. goto out;
  423. }
  424. /* Allocate idal-buffer. */
  425. ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
  426. if (IS_ERR(ib)) {
  427. raw3270_put_view(&fp->view);
  428. raw3270_del_view(&fp->view);
  429. rc = PTR_ERR(ib);
  430. goto out;
  431. }
  432. fp->rdbuf = ib;
  433. rc = raw3270_activate_view(&fp->view);
  434. if (rc) {
  435. raw3270_put_view(&fp->view);
  436. raw3270_del_view(&fp->view);
  437. goto out;
  438. }
  439. nonseekable_open(inode, filp);
  440. filp->private_data = fp;
  441. out:
  442. mutex_unlock(&fs3270_mutex);
  443. return rc;
  444. }
  445. /*
  446. * This routine is called when the 3270 tty is closed. We wait
  447. * for the remaining request to be completed. Then we clean up.
  448. */
  449. static int
  450. fs3270_close(struct inode *inode, struct file *filp)
  451. {
  452. struct fs3270 *fp;
  453. fp = filp->private_data;
  454. filp->private_data = NULL;
  455. if (fp) {
  456. put_pid(fp->fs_pid);
  457. fp->fs_pid = NULL;
  458. raw3270_reset(&fp->view);
  459. raw3270_put_view(&fp->view);
  460. raw3270_del_view(&fp->view);
  461. }
  462. return 0;
  463. }
  464. static const struct file_operations fs3270_fops = {
  465. .owner = THIS_MODULE, /* owner */
  466. .read = fs3270_read, /* read */
  467. .write = fs3270_write, /* write */
  468. .unlocked_ioctl = fs3270_ioctl, /* ioctl */
  469. .compat_ioctl = fs3270_ioctl, /* ioctl */
  470. .open = fs3270_open, /* open */
  471. .release = fs3270_close, /* release */
  472. .llseek = no_llseek,
  473. };
  474. static void fs3270_create_cb(int minor)
  475. {
  476. __register_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub", &fs3270_fops);
  477. device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, minor),
  478. NULL, "3270/tub%d", minor);
  479. }
  480. static void fs3270_destroy_cb(int minor)
  481. {
  482. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, minor));
  483. __unregister_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub");
  484. }
  485. static struct raw3270_notifier fs3270_notifier =
  486. {
  487. .create = fs3270_create_cb,
  488. .destroy = fs3270_destroy_cb,
  489. };
  490. /*
  491. * 3270 fullscreen driver initialization.
  492. */
  493. static int __init
  494. fs3270_init(void)
  495. {
  496. int rc;
  497. rc = __register_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270", &fs3270_fops);
  498. if (rc)
  499. return rc;
  500. device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, 0),
  501. NULL, "3270/tub");
  502. raw3270_register_notifier(&fs3270_notifier);
  503. return 0;
  504. }
  505. static void __exit
  506. fs3270_exit(void)
  507. {
  508. raw3270_unregister_notifier(&fs3270_notifier);
  509. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, 0));
  510. __unregister_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270");
  511. }
  512. MODULE_LICENSE("GPL");
  513. MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
  514. module_init(fs3270_init);
  515. module_exit(fs3270_exit);