fs3270.c 12 KB

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