fs3270.c 12 KB

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