fs3270.c 12 KB

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