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