fs3270.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. struct raw3270_request *clear; /* single clear request. */
  32. wait_queue_head_t attn_wait; /* Attention wait queue. */
  33. };
  34. static void
  35. fs3270_wake_up(struct raw3270_request *rq, void *data)
  36. {
  37. wake_up((wait_queue_head_t *) data);
  38. }
  39. static int
  40. fs3270_do_io(struct raw3270_view *view, struct raw3270_request *rq)
  41. {
  42. wait_queue_head_t wq;
  43. int rc;
  44. init_waitqueue_head(&wq);
  45. rq->callback = fs3270_wake_up;
  46. rq->callback_data = &wq;
  47. rc = raw3270_start(view, rq);
  48. if (rc)
  49. return rc;
  50. /* Started sucessfully. Now wait for completion. */
  51. wait_event(wq, raw3270_request_final(rq));
  52. return rq->rc;
  53. }
  54. static void
  55. fs3270_reset_callback(struct raw3270_request *rq, void *data)
  56. {
  57. raw3270_request_reset(rq);
  58. }
  59. /*
  60. * Switch to the fullscreen view.
  61. */
  62. static int
  63. fs3270_activate(struct raw3270_view *view)
  64. {
  65. struct fs3270 *fp;
  66. fp = (struct fs3270 *) view;
  67. raw3270_request_set_cmd(fp->clear, TC_EWRITEA);
  68. fp->clear->callback = fs3270_reset_callback;
  69. return raw3270_start(view, fp->clear);
  70. }
  71. /*
  72. * Shutdown fullscreen view.
  73. */
  74. static void
  75. fs3270_deactivate(struct raw3270_view *view)
  76. {
  77. // FIXME: is this a good idea? The user program using fullscreen 3270
  78. // will die just because a console message appeared. On the other
  79. // hand the fullscreen device is unoperational now.
  80. struct fs3270 *fp;
  81. fp = (struct fs3270 *) view;
  82. if (fp->fs_pid != 0)
  83. kill_proc(fp->fs_pid, SIGHUP, 1);
  84. fp->fs_pid = 0;
  85. }
  86. static int
  87. fs3270_irq(struct fs3270 *fp, struct raw3270_request *rq, struct irb *irb)
  88. {
  89. /* Handle ATTN. Set indication and wake waiters for attention. */
  90. if (irb->scsw.dstat & DEV_STAT_ATTENTION) {
  91. fp->attention = 1;
  92. wake_up(&fp->attn_wait);
  93. }
  94. if (rq) {
  95. if (irb->scsw.dstat & DEV_STAT_UNIT_CHECK)
  96. rq->rc = -EIO;
  97. else
  98. /* Normal end. Copy residual count. */
  99. rq->rescnt = irb->scsw.count;
  100. }
  101. return RAW3270_IO_DONE;
  102. }
  103. /*
  104. * Process reads from fullscreen 3270.
  105. */
  106. static ssize_t
  107. fs3270_read(struct file *filp, char *data, size_t count, loff_t *off)
  108. {
  109. struct fs3270 *fp;
  110. struct raw3270_request *rq;
  111. struct idal_buffer *ib;
  112. int rc;
  113. if (count == 0 || count > 65535)
  114. return -EINVAL;
  115. fp = filp->private_data;
  116. if (!fp)
  117. return -ENODEV;
  118. ib = idal_buffer_alloc(count, 0);
  119. if (!ib)
  120. return -ENOMEM;
  121. rq = raw3270_request_alloc(0);
  122. if (!IS_ERR(rq)) {
  123. if (fp->read_command == 0 && fp->write_command != 0)
  124. fp->read_command = 6;
  125. raw3270_request_set_cmd(rq, fp->read_command ? : 2);
  126. raw3270_request_set_idal(rq, ib);
  127. wait_event(fp->attn_wait, fp->attention);
  128. rc = fs3270_do_io(&fp->view, rq);
  129. if (rc == 0 && idal_buffer_to_user(ib, data, count))
  130. rc = -EFAULT;
  131. raw3270_request_free(rq);
  132. } else
  133. rc = PTR_ERR(rq);
  134. idal_buffer_free(ib);
  135. return rc;
  136. }
  137. /*
  138. * Process writes to fullscreen 3270.
  139. */
  140. static ssize_t
  141. fs3270_write(struct file *filp, const char *data, size_t count, loff_t *off)
  142. {
  143. struct fs3270 *fp;
  144. struct raw3270_request *rq;
  145. struct idal_buffer *ib;
  146. int write_command;
  147. int rc;
  148. fp = filp->private_data;
  149. if (!fp)
  150. return -ENODEV;
  151. ib = idal_buffer_alloc(count, 0);
  152. if (!ib)
  153. return -ENOMEM;
  154. rq = raw3270_request_alloc(0);
  155. if (!IS_ERR(rq)) {
  156. if (idal_buffer_from_user(ib, data, count) == 0) {
  157. write_command = fp->write_command ? : 1;
  158. if (write_command == 5)
  159. write_command = 13;
  160. raw3270_request_set_cmd(rq, write_command);
  161. raw3270_request_set_idal(rq, ib);
  162. rc = fs3270_do_io(&fp->view, rq);
  163. } else
  164. rc = -EFAULT;
  165. raw3270_request_free(rq);
  166. } else
  167. rc = PTR_ERR(rq);
  168. idal_buffer_free(ib);
  169. return rc;
  170. }
  171. /*
  172. * process ioctl commands for the tube driver
  173. */
  174. static int
  175. fs3270_ioctl(struct inode *inode, struct file *filp,
  176. unsigned int cmd, unsigned long arg)
  177. {
  178. struct fs3270 *fp;
  179. struct raw3270_iocb iocb;
  180. int rc;
  181. fp = filp->private_data;
  182. if (!fp)
  183. return -ENODEV;
  184. rc = 0;
  185. switch (cmd) {
  186. case TUBICMD:
  187. fp->read_command = arg;
  188. break;
  189. case TUBOCMD:
  190. fp->write_command = arg;
  191. break;
  192. case TUBGETI:
  193. rc = put_user(fp->read_command, (char *) arg);
  194. break;
  195. case TUBGETO:
  196. rc = put_user(fp->write_command,(char *) arg);
  197. break;
  198. case TUBGETMOD:
  199. iocb.model = fp->view.model;
  200. iocb.line_cnt = fp->view.rows;
  201. iocb.col_cnt = fp->view.cols;
  202. iocb.pf_cnt = 24;
  203. iocb.re_cnt = 20;
  204. iocb.map = 0;
  205. if (copy_to_user((char *) arg, &iocb,
  206. sizeof(struct raw3270_iocb)))
  207. rc = -EFAULT;
  208. break;
  209. }
  210. return rc;
  211. }
  212. /*
  213. * Allocate tty3270 structure.
  214. */
  215. static struct fs3270 *
  216. fs3270_alloc_view(void)
  217. {
  218. struct fs3270 *fp;
  219. fp = (struct fs3270 *) kmalloc(sizeof(struct fs3270),GFP_KERNEL);
  220. if (!fp)
  221. return ERR_PTR(-ENOMEM);
  222. memset(fp, 0, sizeof(struct fs3270));
  223. fp->clear = raw3270_request_alloc(0);
  224. if (!IS_ERR(fp->clear)) {
  225. kfree(fp);
  226. return ERR_PTR(-ENOMEM);
  227. }
  228. return fp;
  229. }
  230. /*
  231. * Free tty3270 structure.
  232. */
  233. static void
  234. fs3270_free_view(struct raw3270_view *view)
  235. {
  236. raw3270_request_free(((struct fs3270 *) view)->clear);
  237. kfree(view);
  238. }
  239. /*
  240. * Unlink fs3270 data structure from filp.
  241. */
  242. static void
  243. fs3270_release(struct raw3270_view *view)
  244. {
  245. }
  246. /* View to a 3270 device. Can be console, tty or fullscreen. */
  247. struct raw3270_fn fs3270_fn = {
  248. .activate = fs3270_activate,
  249. .deactivate = fs3270_deactivate,
  250. .intv = (void *) fs3270_irq,
  251. .release = fs3270_release,
  252. .free = fs3270_free_view
  253. };
  254. /*
  255. * This routine is called whenever a 3270 fullscreen device is opened.
  256. */
  257. static int
  258. fs3270_open(struct inode *inode, struct file *filp)
  259. {
  260. struct fs3270 *fp;
  261. int minor, rc;
  262. if (imajor(filp->f_dentry->d_inode) != IBM_FS3270_MAJOR)
  263. return -ENODEV;
  264. minor = iminor(filp->f_dentry->d_inode);
  265. /* Check if some other program is already using fullscreen mode. */
  266. fp = (struct fs3270 *) raw3270_find_view(&fs3270_fn, minor);
  267. if (!IS_ERR(fp)) {
  268. raw3270_put_view(&fp->view);
  269. return -EBUSY;
  270. }
  271. /* Allocate fullscreen view structure. */
  272. fp = fs3270_alloc_view();
  273. if (IS_ERR(fp))
  274. return PTR_ERR(fp);
  275. init_waitqueue_head(&fp->attn_wait);
  276. fp->fs_pid = current->pid;
  277. rc = raw3270_add_view(&fp->view, &fs3270_fn, minor);
  278. if (rc) {
  279. fs3270_free_view(&fp->view);
  280. return rc;
  281. }
  282. rc = raw3270_activate_view(&fp->view);
  283. if (rc) {
  284. raw3270_del_view(&fp->view);
  285. return rc;
  286. }
  287. filp->private_data = fp;
  288. return 0;
  289. }
  290. /*
  291. * This routine is called when the 3270 tty is closed. We wait
  292. * for the remaining request to be completed. Then we clean up.
  293. */
  294. static int
  295. fs3270_close(struct inode *inode, struct file *filp)
  296. {
  297. struct fs3270 *fp;
  298. fp = filp->private_data;
  299. filp->private_data = 0;
  300. if (fp)
  301. raw3270_del_view(&fp->view);
  302. return 0;
  303. }
  304. static struct file_operations fs3270_fops = {
  305. .owner = THIS_MODULE, /* owner */
  306. .read = fs3270_read, /* read */
  307. .write = fs3270_write, /* write */
  308. .ioctl = fs3270_ioctl, /* ioctl */
  309. .open = fs3270_open, /* open */
  310. .release = fs3270_close, /* release */
  311. };
  312. /*
  313. * 3270 fullscreen driver initialization.
  314. */
  315. static int __init
  316. fs3270_init(void)
  317. {
  318. int rc;
  319. rc = register_chrdev(IBM_FS3270_MAJOR, "fs3270", &fs3270_fops);
  320. if (rc) {
  321. printk(KERN_ERR "fs3270 can't get major number %d: errno %d\n",
  322. IBM_FS3270_MAJOR, rc);
  323. return rc;
  324. }
  325. return 0;
  326. }
  327. static void __exit
  328. fs3270_exit(void)
  329. {
  330. unregister_chrdev(IBM_FS3270_MAJOR, "fs3270");
  331. }
  332. MODULE_LICENSE("GPL");
  333. MODULE_ALIAS_CHARDEV_MAJOR(IBM_FS3270_MAJOR);
  334. module_init(fs3270_init);
  335. module_exit(fs3270_exit);