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 void
  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. }
  207. /*
  208. * Process reads from fullscreen 3270.
  209. */
  210. static ssize_t
  211. fs3270_read(struct file *filp, char __user *data, size_t count, loff_t *off)
  212. {
  213. struct fs3270 *fp;
  214. struct raw3270_request *rq;
  215. struct idal_buffer *ib;
  216. ssize_t rc;
  217. if (count == 0 || count > 65535)
  218. return -EINVAL;
  219. fp = filp->private_data;
  220. if (!fp)
  221. return -ENODEV;
  222. ib = idal_buffer_alloc(count, 0);
  223. if (IS_ERR(ib))
  224. return -ENOMEM;
  225. rq = raw3270_request_alloc(0);
  226. if (!IS_ERR(rq)) {
  227. if (fp->read_command == 0 && fp->write_command != 0)
  228. fp->read_command = 6;
  229. raw3270_request_set_cmd(rq, fp->read_command ? : 2);
  230. raw3270_request_set_idal(rq, ib);
  231. rc = wait_event_interruptible(fp->wait, fp->attention);
  232. fp->attention = 0;
  233. if (rc == 0) {
  234. rc = fs3270_do_io(&fp->view, rq);
  235. if (rc == 0) {
  236. count -= rq->rescnt;
  237. if (idal_buffer_to_user(ib, data, count) != 0)
  238. rc = -EFAULT;
  239. else
  240. rc = count;
  241. }
  242. }
  243. raw3270_request_free(rq);
  244. } else
  245. rc = PTR_ERR(rq);
  246. idal_buffer_free(ib);
  247. return rc;
  248. }
  249. /*
  250. * Process writes to fullscreen 3270.
  251. */
  252. static ssize_t
  253. fs3270_write(struct file *filp, const char __user *data, size_t count, loff_t *off)
  254. {
  255. struct fs3270 *fp;
  256. struct raw3270_request *rq;
  257. struct idal_buffer *ib;
  258. int write_command;
  259. ssize_t rc;
  260. fp = filp->private_data;
  261. if (!fp)
  262. return -ENODEV;
  263. ib = idal_buffer_alloc(count, 0);
  264. if (IS_ERR(ib))
  265. return -ENOMEM;
  266. rq = raw3270_request_alloc(0);
  267. if (!IS_ERR(rq)) {
  268. if (idal_buffer_from_user(ib, data, count) == 0) {
  269. write_command = fp->write_command ? : 1;
  270. if (write_command == 5)
  271. write_command = 13;
  272. raw3270_request_set_cmd(rq, write_command);
  273. raw3270_request_set_idal(rq, ib);
  274. rc = fs3270_do_io(&fp->view, rq);
  275. if (rc == 0)
  276. rc = count - rq->rescnt;
  277. } else
  278. rc = -EFAULT;
  279. raw3270_request_free(rq);
  280. } else
  281. rc = PTR_ERR(rq);
  282. idal_buffer_free(ib);
  283. return rc;
  284. }
  285. /*
  286. * process ioctl commands for the tube driver
  287. */
  288. static long
  289. fs3270_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  290. {
  291. char __user *argp;
  292. struct fs3270 *fp;
  293. struct raw3270_iocb iocb;
  294. int rc;
  295. fp = filp->private_data;
  296. if (!fp)
  297. return -ENODEV;
  298. if (is_compat_task())
  299. argp = compat_ptr(arg);
  300. else
  301. argp = (char __user *)arg;
  302. rc = 0;
  303. mutex_lock(&fs3270_mutex);
  304. switch (cmd) {
  305. case TUBICMD:
  306. fp->read_command = arg;
  307. break;
  308. case TUBOCMD:
  309. fp->write_command = arg;
  310. break;
  311. case TUBGETI:
  312. rc = put_user(fp->read_command, argp);
  313. break;
  314. case TUBGETO:
  315. rc = put_user(fp->write_command, argp);
  316. break;
  317. case TUBGETMOD:
  318. iocb.model = fp->view.model;
  319. iocb.line_cnt = fp->view.rows;
  320. iocb.col_cnt = fp->view.cols;
  321. iocb.pf_cnt = 24;
  322. iocb.re_cnt = 20;
  323. iocb.map = 0;
  324. if (copy_to_user(argp, &iocb, sizeof(struct raw3270_iocb)))
  325. rc = -EFAULT;
  326. break;
  327. }
  328. mutex_unlock(&fs3270_mutex);
  329. return rc;
  330. }
  331. /*
  332. * Allocate fs3270 structure.
  333. */
  334. static struct fs3270 *
  335. fs3270_alloc_view(void)
  336. {
  337. struct fs3270 *fp;
  338. fp = kzalloc(sizeof(struct fs3270),GFP_KERNEL);
  339. if (!fp)
  340. return ERR_PTR(-ENOMEM);
  341. fp->init = raw3270_request_alloc(0);
  342. if (IS_ERR(fp->init)) {
  343. kfree(fp);
  344. return ERR_PTR(-ENOMEM);
  345. }
  346. return fp;
  347. }
  348. /*
  349. * Free fs3270 structure.
  350. */
  351. static void
  352. fs3270_free_view(struct raw3270_view *view)
  353. {
  354. struct fs3270 *fp;
  355. fp = (struct fs3270 *) view;
  356. if (fp->rdbuf)
  357. idal_buffer_free(fp->rdbuf);
  358. raw3270_request_free(((struct fs3270 *) view)->init);
  359. kfree(view);
  360. }
  361. /*
  362. * Unlink fs3270 data structure from filp.
  363. */
  364. static void
  365. fs3270_release(struct raw3270_view *view)
  366. {
  367. struct fs3270 *fp;
  368. fp = (struct fs3270 *) view;
  369. if (fp->fs_pid)
  370. kill_pid(fp->fs_pid, SIGHUP, 1);
  371. }
  372. /* View to a 3270 device. Can be console, tty or fullscreen. */
  373. static struct raw3270_fn fs3270_fn = {
  374. .activate = fs3270_activate,
  375. .deactivate = fs3270_deactivate,
  376. .intv = (void *) fs3270_irq,
  377. .release = fs3270_release,
  378. .free = fs3270_free_view
  379. };
  380. /*
  381. * This routine is called whenever a 3270 fullscreen device is opened.
  382. */
  383. static int
  384. fs3270_open(struct inode *inode, struct file *filp)
  385. {
  386. struct fs3270 *fp;
  387. struct idal_buffer *ib;
  388. int minor, rc = 0;
  389. if (imajor(file_inode(filp)) != IBM_FS3270_MAJOR)
  390. return -ENODEV;
  391. minor = iminor(file_inode(filp));
  392. /* Check for minor 0 multiplexer. */
  393. if (minor == 0) {
  394. struct tty_struct *tty = get_current_tty();
  395. if (!tty || tty->driver->major != IBM_TTY3270_MAJOR) {
  396. tty_kref_put(tty);
  397. return -ENODEV;
  398. }
  399. minor = tty->index;
  400. tty_kref_put(tty);
  401. }
  402. mutex_lock(&fs3270_mutex);
  403. /* Check if some other program is already using fullscreen mode. */
  404. fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
  405. if (!IS_ERR(fp)) {
  406. raw3270_put_view(&fp->view);
  407. rc = -EBUSY;
  408. goto out;
  409. }
  410. /* Allocate fullscreen view structure. */
  411. fp = fs3270_alloc_view();
  412. if (IS_ERR(fp)) {
  413. rc = PTR_ERR(fp);
  414. goto out;
  415. }
  416. init_waitqueue_head(&fp->wait);
  417. fp->fs_pid = get_pid(task_pid(current));
  418. rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
  419. if (rc) {
  420. fs3270_free_view(&fp->view);
  421. goto out;
  422. }
  423. /* Allocate idal-buffer. */
  424. ib = idal_buffer_alloc(2*fp->view.rows*fp->view.cols + 5, 0);
  425. if (IS_ERR(ib)) {
  426. raw3270_put_view(&fp->view);
  427. raw3270_del_view(&fp->view);
  428. rc = PTR_ERR(ib);
  429. goto out;
  430. }
  431. fp->rdbuf = ib;
  432. rc = raw3270_activate_view(&fp->view);
  433. if (rc) {
  434. raw3270_put_view(&fp->view);
  435. raw3270_del_view(&fp->view);
  436. goto out;
  437. }
  438. nonseekable_open(inode, filp);
  439. filp->private_data = fp;
  440. out:
  441. mutex_unlock(&fs3270_mutex);
  442. return rc;
  443. }
  444. /*
  445. * This routine is called when the 3270 tty is closed. We wait
  446. * for the remaining request to be completed. Then we clean up.
  447. */
  448. static int
  449. fs3270_close(struct inode *inode, struct file *filp)
  450. {
  451. struct fs3270 *fp;
  452. fp = filp->private_data;
  453. filp->private_data = NULL;
  454. if (fp) {
  455. put_pid(fp->fs_pid);
  456. fp->fs_pid = NULL;
  457. raw3270_reset(&fp->view);
  458. raw3270_put_view(&fp->view);
  459. raw3270_del_view(&fp->view);
  460. }
  461. return 0;
  462. }
  463. static const struct file_operations fs3270_fops = {
  464. .owner = THIS_MODULE, /* owner */
  465. .read = fs3270_read, /* read */
  466. .write = fs3270_write, /* write */
  467. .unlocked_ioctl = fs3270_ioctl, /* ioctl */
  468. .compat_ioctl = fs3270_ioctl, /* ioctl */
  469. .open = fs3270_open, /* open */
  470. .release = fs3270_close, /* release */
  471. .llseek = no_llseek,
  472. };
  473. static void fs3270_create_cb(int minor)
  474. {
  475. __register_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub", &fs3270_fops);
  476. device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, minor),
  477. NULL, "3270/tub%d", minor);
  478. }
  479. static void fs3270_destroy_cb(int minor)
  480. {
  481. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, minor));
  482. __unregister_chrdev(IBM_FS3270_MAJOR, minor, 1, "tub");
  483. }
  484. static struct raw3270_notifier fs3270_notifier =
  485. {
  486. .create = fs3270_create_cb,
  487. .destroy = fs3270_destroy_cb,
  488. };
  489. /*
  490. * 3270 fullscreen driver initialization.
  491. */
  492. static int __init
  493. fs3270_init(void)
  494. {
  495. int rc;
  496. rc = __register_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270", &fs3270_fops);
  497. if (rc)
  498. return rc;
  499. device_create(class3270, NULL, MKDEV(IBM_FS3270_MAJOR, 0),
  500. NULL, "3270/tub");
  501. raw3270_register_notifier(&fs3270_notifier);
  502. return 0;
  503. }
  504. static void __exit
  505. fs3270_exit(void)
  506. {
  507. raw3270_unregister_notifier(&fs3270_notifier);
  508. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, 0));
  509. __unregister_chrdev(IBM_FS3270_MAJOR, 0, 1, "fs3270");
  510. }
  511. MODULE_LICENSE("GPL");
  512. MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
  513. module_init(fs3270_init);
  514. module_exit(fs3270_exit);