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