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 - core functions. |
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 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/module.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/wait.h> |
| 18 | |
| 19 | #include <asm/ccwdev.h> |
| 20 | #include <asm/cio.h> |
| 21 | #include <asm/ebcdic.h> |
Michael Holzheu | 0a87c5c | 2007-08-22 13:51:40 +0200 | [diff] [blame] | 22 | #include <asm/diag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include "raw3270.h" |
| 25 | |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 26 | #include <linux/major.h> |
| 27 | #include <linux/kdev_t.h> |
| 28 | #include <linux/device.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 30 | |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 31 | struct class *class3270; |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /* The main 3270 data structure. */ |
| 34 | struct raw3270 { |
| 35 | struct list_head list; |
| 36 | struct ccw_device *cdev; |
| 37 | int minor; |
| 38 | |
| 39 | short model, rows, cols; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 40 | unsigned int state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | unsigned long flags; |
| 42 | |
| 43 | struct list_head req_queue; /* Request queue. */ |
| 44 | struct list_head view_list; /* List of available views. */ |
| 45 | struct raw3270_view *view; /* Active view. */ |
| 46 | |
| 47 | struct timer_list timer; /* Device timer. */ |
| 48 | |
| 49 | unsigned char *ascebc; /* ascii -> ebcdic table */ |
Martin Schwidefsky | 132fab1 | 2006-06-29 14:57:39 +0200 | [diff] [blame] | 50 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 51 | struct raw3270_view init_view; |
| 52 | struct raw3270_request init_reset; |
| 53 | struct raw3270_request init_readpart; |
| 54 | struct raw3270_request init_readmod; |
Martin Schwidefsky | 132fab1 | 2006-06-29 14:57:39 +0200 | [diff] [blame] | 55 | unsigned char init_data[256]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 58 | /* raw3270->state */ |
| 59 | #define RAW3270_STATE_INIT 0 /* Initial state */ |
| 60 | #define RAW3270_STATE_RESET 1 /* Reset command is pending */ |
| 61 | #define RAW3270_STATE_W4ATTN 2 /* Wait for attention interrupt */ |
| 62 | #define RAW3270_STATE_READMOD 3 /* Read partition is pending */ |
| 63 | #define RAW3270_STATE_READY 4 /* Device is usable by views */ |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* raw3270->flags */ |
| 66 | #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */ |
| 67 | #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */ |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 68 | #define RAW3270_FLAGS_CONSOLE 2 /* Device is the console. */ |
| 69 | #define RAW3270_FLAGS_FROZEN 3 /* set if 3270 is frozen for suspend */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* Semaphore to protect global data of raw3270 (devices, views, etc). */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 72 | static DEFINE_MUTEX(raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* List of 3270 devices. */ |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 75 | static LIST_HEAD(raw3270_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Flag to indicate if the driver has been registered. Some operations |
| 79 | * like waiting for the end of i/o need to be done differently as long |
| 80 | * as the kernel is still starting up (console support). |
| 81 | */ |
| 82 | static int raw3270_registered; |
| 83 | |
| 84 | /* Module parameters */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 85 | static bool tubxcorrect = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | module_param(tubxcorrect, bool, 0); |
| 87 | |
| 88 | /* |
| 89 | * Wait queue for device init/delete, view delete. |
| 90 | */ |
| 91 | DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue); |
| 92 | |
| 93 | /* |
| 94 | * Encode array for 12 bit 3270 addresses. |
| 95 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 96 | static unsigned char raw3270_ebcgraf[64] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, |
| 98 | 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, |
| 99 | 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, |
| 100 | 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, |
| 101 | 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
| 102 | 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 103 | 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
| 104 | 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f |
| 105 | }; |
| 106 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 107 | static inline int raw3270_state_ready(struct raw3270 *rp) |
| 108 | { |
| 109 | return rp->state == RAW3270_STATE_READY; |
| 110 | } |
| 111 | |
| 112 | static inline int raw3270_state_final(struct raw3270 *rp) |
| 113 | { |
| 114 | return rp->state == RAW3270_STATE_INIT || |
| 115 | rp->state == RAW3270_STATE_READY; |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | void |
| 119 | raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr) |
| 120 | { |
| 121 | if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) { |
| 122 | cp[0] = (addr >> 8) & 0x3f; |
| 123 | cp[1] = addr & 0xff; |
| 124 | } else { |
| 125 | cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f]; |
| 126 | cp[1] = raw3270_ebcgraf[addr & 0x3f]; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Allocate a new 3270 ccw request |
| 132 | */ |
| 133 | struct raw3270_request * |
| 134 | raw3270_request_alloc(size_t size) |
| 135 | { |
| 136 | struct raw3270_request *rq; |
| 137 | |
| 138 | /* Allocate request structure */ |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 139 | rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (!rq) |
| 141 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | /* alloc output buffer. */ |
| 144 | if (size > 0) { |
| 145 | rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA); |
| 146 | if (!rq->buffer) { |
| 147 | kfree(rq); |
| 148 | return ERR_PTR(-ENOMEM); |
| 149 | } |
| 150 | } |
| 151 | rq->size = size; |
| 152 | INIT_LIST_HEAD(&rq->list); |
| 153 | |
| 154 | /* |
| 155 | * Setup ccw. |
| 156 | */ |
| 157 | rq->ccw.cda = __pa(rq->buffer); |
| 158 | rq->ccw.flags = CCW_FLAG_SLI; |
| 159 | |
| 160 | return rq; |
| 161 | } |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* |
| 164 | * Free 3270 ccw request |
| 165 | */ |
| 166 | void |
| 167 | raw3270_request_free (struct raw3270_request *rq) |
| 168 | { |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame] | 169 | kfree(rq->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | kfree(rq); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Reset request to initial state. |
| 175 | */ |
| 176 | void |
| 177 | raw3270_request_reset(struct raw3270_request *rq) |
| 178 | { |
| 179 | BUG_ON(!list_empty(&rq->list)); |
| 180 | rq->ccw.cmd_code = 0; |
| 181 | rq->ccw.count = 0; |
| 182 | rq->ccw.cda = __pa(rq->buffer); |
| 183 | rq->ccw.flags = CCW_FLAG_SLI; |
| 184 | rq->rescnt = 0; |
| 185 | rq->rc = 0; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Set command code to ccw of a request. |
| 190 | */ |
| 191 | void |
| 192 | raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd) |
| 193 | { |
| 194 | rq->ccw.cmd_code = cmd; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Add data fragment to output buffer. |
| 199 | */ |
| 200 | int |
| 201 | raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size) |
| 202 | { |
| 203 | if (size + rq->ccw.count > rq->size) |
| 204 | return -E2BIG; |
| 205 | memcpy(rq->buffer + rq->ccw.count, data, size); |
| 206 | rq->ccw.count += size; |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Set address/length pair to ccw of a request. |
| 212 | */ |
| 213 | void |
| 214 | raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size) |
| 215 | { |
| 216 | rq->ccw.cda = __pa(data); |
| 217 | rq->ccw.count = size; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Set idal buffer to ccw of a request. |
| 222 | */ |
| 223 | void |
| 224 | raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib) |
| 225 | { |
| 226 | rq->ccw.cda = __pa(ib->data); |
| 227 | rq->ccw.count = ib->size; |
| 228 | rq->ccw.flags |= CCW_FLAG_IDA; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Stop running ccw. |
| 233 | */ |
| 234 | static int |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 235 | __raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 237 | int retries; |
| 238 | int rc; |
| 239 | |
| 240 | if (raw3270_request_final(rq)) |
| 241 | return 0; |
| 242 | /* Check if interrupt has already been processed */ |
| 243 | for (retries = 0; retries < 5; retries++) { |
| 244 | if (retries < 2) |
| 245 | rc = ccw_device_halt(rp->cdev, (long) rq); |
| 246 | else |
| 247 | rc = ccw_device_clear(rp->cdev, (long) rq); |
| 248 | if (rc == 0) |
| 249 | break; /* termination successful */ |
| 250 | } |
| 251 | return rc; |
| 252 | } |
| 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | /* |
| 255 | * Add the request to the request queue, try to start it if the |
| 256 | * 3270 device is idle. Return without waiting for end of i/o. |
| 257 | */ |
| 258 | static int |
| 259 | __raw3270_start(struct raw3270 *rp, struct raw3270_view *view, |
| 260 | struct raw3270_request *rq) |
| 261 | { |
| 262 | rq->view = view; |
| 263 | raw3270_get_view(view); |
| 264 | if (list_empty(&rp->req_queue) && |
| 265 | !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) { |
| 266 | /* No other requests are on the queue. Start this one. */ |
| 267 | rq->rc = ccw_device_start(rp->cdev, &rq->ccw, |
| 268 | (unsigned long) rq, 0, 0); |
| 269 | if (rq->rc) { |
| 270 | raw3270_put_view(view); |
| 271 | return rq->rc; |
| 272 | } |
| 273 | } |
| 274 | list_add_tail(&rq->list, &rp->req_queue); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int |
Martin Schwidefsky | 233faec | 2014-03-21 09:25:24 +0100 | [diff] [blame] | 279 | raw3270_view_active(struct raw3270_view *view) |
| 280 | { |
| 281 | struct raw3270 *rp = view->dev; |
| 282 | |
| 283 | return rp && rp->view == view && |
| 284 | !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags); |
| 285 | } |
| 286 | |
| 287 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | raw3270_start(struct raw3270_view *view, struct raw3270_request *rq) |
| 289 | { |
| 290 | unsigned long flags; |
| 291 | struct raw3270 *rp; |
| 292 | int rc; |
| 293 | |
| 294 | spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags); |
| 295 | rp = view->dev; |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 296 | if (!rp || rp->view != view || |
| 297 | test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | rc = -EACCES; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 299 | else if (!raw3270_state_ready(rp)) |
| 300 | rc = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | else |
| 302 | rc = __raw3270_start(rp, view, rq); |
| 303 | spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags); |
| 304 | return rc; |
| 305 | } |
| 306 | |
| 307 | int |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 308 | raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq) |
| 309 | { |
| 310 | struct raw3270 *rp; |
| 311 | int rc; |
| 312 | |
| 313 | rp = view->dev; |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 314 | if (!rp || rp->view != view || |
| 315 | test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 316 | rc = -EACCES; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 317 | else if (!raw3270_state_ready(rp)) |
| 318 | rc = -EBUSY; |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 319 | else |
| 320 | rc = __raw3270_start(rp, view, rq); |
| 321 | return rc; |
| 322 | } |
| 323 | |
| 324 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq) |
| 326 | { |
| 327 | struct raw3270 *rp; |
| 328 | |
| 329 | rp = view->dev; |
| 330 | rq->view = view; |
| 331 | raw3270_get_view(view); |
| 332 | list_add_tail(&rq->list, &rp->req_queue); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * 3270 interrupt routine, called from the ccw_device layer |
| 338 | */ |
| 339 | static void |
| 340 | raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb) |
| 341 | { |
| 342 | struct raw3270 *rp; |
| 343 | struct raw3270_view *view; |
| 344 | struct raw3270_request *rq; |
| 345 | int rc; |
| 346 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 347 | rp = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if (!rp) |
| 349 | return; |
| 350 | rq = (struct raw3270_request *) intparm; |
| 351 | view = rq ? rq->view : rp->view; |
| 352 | |
| 353 | if (IS_ERR(irb)) |
| 354 | rc = RAW3270_IO_RETRY; |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 355 | else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | rq->rc = -EIO; |
| 357 | rc = RAW3270_IO_DONE; |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 358 | } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END | |
| 359 | DEV_STAT_UNIT_EXCEP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | /* Handle CE-DE-UE and subsequent UDE */ |
| 361 | set_bit(RAW3270_FLAGS_BUSY, &rp->flags); |
| 362 | rc = RAW3270_IO_BUSY; |
| 363 | } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) { |
| 364 | /* Wait for UDE if busy flag is set. */ |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 365 | if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | clear_bit(RAW3270_FLAGS_BUSY, &rp->flags); |
| 367 | /* Got it, now retry. */ |
| 368 | rc = RAW3270_IO_RETRY; |
| 369 | } else |
| 370 | rc = RAW3270_IO_BUSY; |
| 371 | } else if (view) |
| 372 | rc = view->fn->intv(view, rq, irb); |
| 373 | else |
| 374 | rc = RAW3270_IO_DONE; |
| 375 | |
| 376 | switch (rc) { |
| 377 | case RAW3270_IO_DONE: |
| 378 | break; |
| 379 | case RAW3270_IO_BUSY: |
| 380 | /* |
| 381 | * Intervention required by the operator. We have to wait |
| 382 | * for unsolicited device end. |
| 383 | */ |
| 384 | return; |
| 385 | case RAW3270_IO_RETRY: |
| 386 | if (!rq) |
| 387 | break; |
| 388 | rq->rc = ccw_device_start(rp->cdev, &rq->ccw, |
| 389 | (unsigned long) rq, 0, 0); |
| 390 | if (rq->rc == 0) |
Daniel Mack | 3ad2f3f | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 391 | return; /* Successfully restarted. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | break; |
| 393 | case RAW3270_IO_STOP: |
| 394 | if (!rq) |
| 395 | break; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 396 | __raw3270_halt_io(rp, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | rq->rc = -EIO; |
| 398 | break; |
| 399 | default: |
| 400 | BUG(); |
| 401 | } |
| 402 | if (rq) { |
| 403 | BUG_ON(list_empty(&rq->list)); |
| 404 | /* The request completed, remove from queue and do callback. */ |
| 405 | list_del_init(&rq->list); |
| 406 | if (rq->callback) |
| 407 | rq->callback(rq, rq->callback_data); |
| 408 | /* Do put_device for get_device in raw3270_start. */ |
| 409 | raw3270_put_view(view); |
| 410 | } |
| 411 | /* |
| 412 | * Try to start each request on request queue until one is |
| 413 | * started successful. |
| 414 | */ |
| 415 | while (!list_empty(&rp->req_queue)) { |
| 416 | rq = list_entry(rp->req_queue.next,struct raw3270_request,list); |
| 417 | rq->rc = ccw_device_start(rp->cdev, &rq->ccw, |
| 418 | (unsigned long) rq, 0, 0); |
| 419 | if (rq->rc == 0) |
| 420 | break; |
| 421 | /* Start failed. Remove request and do callback. */ |
| 422 | list_del_init(&rq->list); |
| 423 | if (rq->callback) |
| 424 | rq->callback(rq, rq->callback_data); |
| 425 | /* Do put_device for get_device in raw3270_start. */ |
| 426 | raw3270_put_view(view); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /* |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 431 | * To determine the size of the 3270 device we need to do: |
| 432 | * 1) send a 'read partition' data stream to the device |
| 433 | * 2) wait for the attn interrupt that precedes the query reply |
| 434 | * 3) do a read modified to get the query reply |
| 435 | * To make things worse we have to cope with intervention |
| 436 | * required (3270 device switched to 'stand-by') and command |
| 437 | * rejects (old devices that can't do 'read partition'). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | struct raw3270_ua { /* Query Reply structure for Usable Area */ |
| 440 | struct { /* Usable Area Query Reply Base */ |
| 441 | short l; /* Length of this structured field */ |
| 442 | char sfid; /* 0x81 if Query Reply */ |
| 443 | char qcode; /* 0x81 if Usable Area */ |
| 444 | char flags0; |
| 445 | char flags1; |
| 446 | short w; /* Width of usable area */ |
| 447 | short h; /* Heigth of usavle area */ |
| 448 | char units; /* 0x00:in; 0x01:mm */ |
| 449 | int xr; |
| 450 | int yr; |
| 451 | char aw; |
| 452 | char ah; |
| 453 | short buffsz; /* Character buffer size, bytes */ |
| 454 | char xmin; |
| 455 | char ymin; |
| 456 | char xmax; |
| 457 | char ymax; |
| 458 | } __attribute__ ((packed)) uab; |
| 459 | struct { /* Alternate Usable Area Self-Defining Parameter */ |
| 460 | char l; /* Length of this Self-Defining Parm */ |
| 461 | char sdpid; /* 0x02 if Alternate Usable Area */ |
| 462 | char res; |
| 463 | char auaid; /* 0x01 is Id for the A U A */ |
| 464 | short wauai; /* Width of AUAi */ |
| 465 | short hauai; /* Height of AUAi */ |
| 466 | char auaunits; /* 0x00:in, 0x01:mm */ |
| 467 | int auaxr; |
| 468 | int auayr; |
| 469 | char awauai; |
| 470 | char ahauai; |
| 471 | } __attribute__ ((packed)) aua; |
| 472 | } __attribute__ ((packed)); |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | static void |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 475 | raw3270_size_device_vm(struct raw3270 *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | int rc, model; |
Cornelia Huck | 9a92fe4 | 2007-05-10 15:45:42 +0200 | [diff] [blame] | 478 | struct ccw_dev_id dev_id; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 479 | struct diag210 diag_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Cornelia Huck | 9a92fe4 | 2007-05-10 15:45:42 +0200 | [diff] [blame] | 481 | ccw_device_get_id(rp->cdev, &dev_id); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 482 | diag_data.vrdcdvno = dev_id.devno; |
| 483 | diag_data.vrdclen = sizeof(struct diag210); |
| 484 | rc = diag210(&diag_data); |
| 485 | model = diag_data.vrdccrmd; |
| 486 | /* Use default model 2 if the size could not be detected */ |
| 487 | if (rc || model < 2 || model > 5) |
| 488 | model = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | switch (model) { |
| 490 | case 2: |
| 491 | rp->model = model; |
| 492 | rp->rows = 24; |
| 493 | rp->cols = 80; |
| 494 | break; |
| 495 | case 3: |
| 496 | rp->model = model; |
| 497 | rp->rows = 32; |
| 498 | rp->cols = 80; |
| 499 | break; |
| 500 | case 4: |
| 501 | rp->model = model; |
| 502 | rp->rows = 43; |
| 503 | rp->cols = 80; |
| 504 | break; |
| 505 | case 5: |
| 506 | rp->model = model; |
| 507 | rp->rows = 27; |
| 508 | rp->cols = 132; |
| 509 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 513 | static void |
| 514 | raw3270_size_device(struct raw3270 *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | struct raw3270_ua *uap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* Got a Query Reply */ |
Martin Schwidefsky | 132fab1 | 2006-06-29 14:57:39 +0200 | [diff] [blame] | 519 | uap = (struct raw3270_ua *) (rp->init_data + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | /* Paranoia check. */ |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 521 | if (rp->init_readmod.rc || rp->init_data[0] != 0x88 || |
| 522 | uap->uab.qcode != 0x81) { |
| 523 | /* Couldn't detect size. Use default model 2. */ |
| 524 | rp->model = 2; |
| 525 | rp->rows = 24; |
| 526 | rp->cols = 80; |
| 527 | return; |
| 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | /* Copy rows/columns of default Usable Area */ |
| 530 | rp->rows = uap->uab.h; |
| 531 | rp->cols = uap->uab.w; |
| 532 | /* Check for 14 bit addressing */ |
| 533 | if ((uap->uab.flags0 & 0x0d) == 0x01) |
| 534 | set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags); |
| 535 | /* Check for Alternate Usable Area */ |
| 536 | if (uap->uab.l == sizeof(struct raw3270_ua) && |
| 537 | uap->aua.sdpid == 0x02) { |
| 538 | rp->rows = uap->aua.hauai; |
| 539 | rp->cols = uap->aua.wauai; |
| 540 | } |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 541 | /* Try to find a model. */ |
| 542 | rp->model = 0; |
| 543 | if (rp->rows == 24 && rp->cols == 80) |
| 544 | rp->model = 2; |
| 545 | if (rp->rows == 32 && rp->cols == 80) |
| 546 | rp->model = 3; |
| 547 | if (rp->rows == 43 && rp->cols == 80) |
| 548 | rp->model = 4; |
| 549 | if (rp->rows == 27 && rp->cols == 132) |
| 550 | rp->model = 5; |
| 551 | } |
| 552 | |
| 553 | static void |
| 554 | raw3270_size_device_done(struct raw3270 *rp) |
| 555 | { |
| 556 | struct raw3270_view *view; |
| 557 | |
| 558 | rp->view = NULL; |
| 559 | rp->state = RAW3270_STATE_READY; |
| 560 | /* Notify views about new size */ |
| 561 | list_for_each_entry(view, &rp->view_list, list) |
| 562 | if (view->fn->resize) |
| 563 | view->fn->resize(view, rp->model, rp->rows, rp->cols); |
| 564 | /* Setup processing done, now activate a view */ |
| 565 | list_for_each_entry(view, &rp->view_list, list) { |
| 566 | rp->view = view; |
| 567 | if (view->fn->activate(view) == 0) |
| 568 | break; |
| 569 | rp->view = NULL; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | static void |
| 574 | raw3270_read_modified_cb(struct raw3270_request *rq, void *data) |
| 575 | { |
| 576 | struct raw3270 *rp = rq->view->dev; |
| 577 | |
| 578 | raw3270_size_device(rp); |
| 579 | raw3270_size_device_done(rp); |
| 580 | } |
| 581 | |
| 582 | static void |
| 583 | raw3270_read_modified(struct raw3270 *rp) |
| 584 | { |
| 585 | if (rp->state != RAW3270_STATE_W4ATTN) |
| 586 | return; |
| 587 | /* Use 'read modified' to get the result of a read partition. */ |
| 588 | memset(&rp->init_readmod, 0, sizeof(rp->init_readmod)); |
| 589 | memset(&rp->init_data, 0, sizeof(rp->init_data)); |
| 590 | rp->init_readmod.ccw.cmd_code = TC_READMOD; |
| 591 | rp->init_readmod.ccw.flags = CCW_FLAG_SLI; |
| 592 | rp->init_readmod.ccw.count = sizeof(rp->init_data); |
| 593 | rp->init_readmod.ccw.cda = (__u32) __pa(rp->init_data); |
| 594 | rp->init_readmod.callback = raw3270_read_modified_cb; |
| 595 | rp->state = RAW3270_STATE_READMOD; |
| 596 | raw3270_start_irq(&rp->init_view, &rp->init_readmod); |
| 597 | } |
| 598 | |
| 599 | static void |
| 600 | raw3270_writesf_readpart(struct raw3270 *rp) |
| 601 | { |
| 602 | static const unsigned char wbuf[] = |
| 603 | { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 }; |
| 604 | |
| 605 | /* Store 'read partition' data stream to init_data */ |
| 606 | memset(&rp->init_readpart, 0, sizeof(rp->init_readpart)); |
| 607 | memset(&rp->init_data, 0, sizeof(rp->init_data)); |
| 608 | memcpy(&rp->init_data, wbuf, sizeof(wbuf)); |
| 609 | rp->init_readpart.ccw.cmd_code = TC_WRITESF; |
| 610 | rp->init_readpart.ccw.flags = CCW_FLAG_SLI; |
| 611 | rp->init_readpart.ccw.count = sizeof(wbuf); |
| 612 | rp->init_readpart.ccw.cda = (__u32) __pa(&rp->init_data); |
| 613 | rp->state = RAW3270_STATE_W4ATTN; |
| 614 | raw3270_start_irq(&rp->init_view, &rp->init_readpart); |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Device reset |
| 619 | */ |
| 620 | static void |
| 621 | raw3270_reset_device_cb(struct raw3270_request *rq, void *data) |
| 622 | { |
| 623 | struct raw3270 *rp = rq->view->dev; |
| 624 | |
| 625 | if (rp->state != RAW3270_STATE_RESET) |
| 626 | return; |
Martin Schwidefsky | dc3ac5f | 2013-10-14 16:48:29 +0200 | [diff] [blame] | 627 | if (rq->rc) { |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 628 | /* Reset command failed. */ |
| 629 | rp->state = RAW3270_STATE_INIT; |
Martin Schwidefsky | 01a7cfa | 2013-10-15 13:41:05 +0200 | [diff] [blame] | 630 | } else if (MACHINE_IS_VM) { |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 631 | raw3270_size_device_vm(rp); |
| 632 | raw3270_size_device_done(rp); |
| 633 | } else |
| 634 | raw3270_writesf_readpart(rp); |
Martin Schwidefsky | bd1cb5d | 2014-03-21 15:24:27 +0100 | [diff] [blame] | 635 | memset(&rp->init_reset, 0, sizeof(rp->init_reset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static int |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 639 | __raw3270_reset_device(struct raw3270 *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | { |
| 641 | int rc; |
| 642 | |
Martin Schwidefsky | bd1cb5d | 2014-03-21 15:24:27 +0100 | [diff] [blame] | 643 | /* Check if reset is already pending */ |
| 644 | if (rp->init_reset.view) |
| 645 | return -EBUSY; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 646 | /* Store reset data stream to init_data/init_reset */ |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 647 | rp->init_data[0] = TW_KR; |
| 648 | rp->init_reset.ccw.cmd_code = TC_EWRITEA; |
| 649 | rp->init_reset.ccw.flags = CCW_FLAG_SLI; |
| 650 | rp->init_reset.ccw.count = 1; |
| 651 | rp->init_reset.ccw.cda = (__u32) __pa(rp->init_data); |
| 652 | rp->init_reset.callback = raw3270_reset_device_cb; |
| 653 | rc = __raw3270_start(rp, &rp->init_view, &rp->init_reset); |
| 654 | if (rc == 0 && rp->state == RAW3270_STATE_INIT) |
| 655 | rp->state = RAW3270_STATE_RESET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return rc; |
| 657 | } |
| 658 | |
| 659 | static int |
| 660 | raw3270_reset_device(struct raw3270 *rp) |
| 661 | { |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 662 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | int rc; |
| 664 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 665 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
| 666 | rc = __raw3270_reset_device(rp); |
| 667 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | return rc; |
| 669 | } |
| 670 | |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 671 | int |
| 672 | raw3270_reset(struct raw3270_view *view) |
| 673 | { |
| 674 | struct raw3270 *rp; |
| 675 | int rc; |
| 676 | |
| 677 | rp = view->dev; |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 678 | if (!rp || rp->view != view || |
| 679 | test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 680 | rc = -EACCES; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 681 | else if (!raw3270_state_ready(rp)) |
| 682 | rc = -EBUSY; |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 683 | else |
| 684 | rc = raw3270_reset_device(view->dev); |
| 685 | return rc; |
| 686 | } |
| 687 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 688 | static int |
| 689 | raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq, |
| 690 | struct irb *irb) |
| 691 | { |
| 692 | struct raw3270 *rp; |
| 693 | |
| 694 | /* |
| 695 | * Unit-Check Processing: |
| 696 | * Expect Command Reject or Intervention Required. |
| 697 | */ |
| 698 | if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) { |
| 699 | /* Request finished abnormally. */ |
| 700 | if (irb->ecw[0] & SNS0_INTERVENTION_REQ) { |
| 701 | set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags); |
| 702 | return RAW3270_IO_BUSY; |
| 703 | } |
| 704 | } |
| 705 | if (rq) { |
| 706 | if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) { |
| 707 | if (irb->ecw[0] & SNS0_CMD_REJECT) |
| 708 | rq->rc = -EOPNOTSUPP; |
| 709 | else |
| 710 | rq->rc = -EIO; |
| 711 | } |
| 712 | } |
| 713 | if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) { |
| 714 | /* Queue read modified after attention interrupt */ |
| 715 | rp = view->dev; |
| 716 | raw3270_read_modified(rp); |
| 717 | } |
| 718 | return RAW3270_IO_DONE; |
| 719 | } |
| 720 | |
| 721 | static struct raw3270_fn raw3270_init_fn = { |
| 722 | .intv = raw3270_init_irq |
| 723 | }; |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * Setup new 3270 device. |
| 727 | */ |
| 728 | static int |
| 729 | raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc) |
| 730 | { |
| 731 | struct list_head *l; |
| 732 | struct raw3270 *tmp; |
| 733 | int minor; |
| 734 | |
| 735 | memset(rp, 0, sizeof(struct raw3270)); |
| 736 | /* Copy ebcdic -> ascii translation table. */ |
| 737 | memcpy(ascebc, _ascebc, 256); |
| 738 | if (tubxcorrect) { |
| 739 | /* correct brackets and circumflex */ |
| 740 | ascebc['['] = 0xad; |
| 741 | ascebc[']'] = 0xbd; |
| 742 | ascebc['^'] = 0xb0; |
| 743 | } |
| 744 | rp->ascebc = ascebc; |
| 745 | |
| 746 | /* Set defaults. */ |
| 747 | rp->rows = 24; |
| 748 | rp->cols = 80; |
| 749 | |
| 750 | INIT_LIST_HEAD(&rp->req_queue); |
| 751 | INIT_LIST_HEAD(&rp->view_list); |
| 752 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 753 | rp->init_view.dev = rp; |
| 754 | rp->init_view.fn = &raw3270_init_fn; |
| 755 | rp->view = &rp->init_view; |
| 756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | /* |
| 758 | * Add device to list and find the smallest unused minor |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 759 | * number for it. Note: there is no device with minor 0, |
| 760 | * see special case for fs3270.c:fs3270_open(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 762 | mutex_lock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* Keep the list sorted. */ |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 764 | minor = RAW3270_FIRSTMINOR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | rp->minor = -1; |
| 766 | list_for_each(l, &raw3270_devices) { |
| 767 | tmp = list_entry(l, struct raw3270, list); |
| 768 | if (tmp->minor > minor) { |
| 769 | rp->minor = minor; |
| 770 | __list_add(&rp->list, l->prev, l); |
| 771 | break; |
| 772 | } |
| 773 | minor++; |
| 774 | } |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 775 | if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | rp->minor = minor; |
| 777 | list_add_tail(&rp->list, &raw3270_devices); |
| 778 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 779 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | /* No free minor number? Then give up. */ |
| 781 | if (rp->minor == -1) |
| 782 | return -EUSERS; |
| 783 | rp->cdev = cdev; |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 784 | dev_set_drvdata(&cdev->dev, rp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | cdev->handler = raw3270_irq; |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | #ifdef CONFIG_TN3270_CONSOLE |
Sebastian Ott | 2253e8d | 2014-01-27 13:26:10 +0100 | [diff] [blame] | 790 | /* Tentative definition - see below for actual definition. */ |
| 791 | static struct ccw_driver raw3270_ccw_driver; |
| 792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | /* |
| 794 | * Setup 3270 device configured as console. |
| 795 | */ |
Sebastian Ott | 2253e8d | 2014-01-27 13:26:10 +0100 | [diff] [blame] | 796 | struct raw3270 __init *raw3270_setup_console(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
Sebastian Ott | 2253e8d | 2014-01-27 13:26:10 +0100 | [diff] [blame] | 798 | struct ccw_device *cdev; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 799 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | struct raw3270 *rp; |
| 801 | char *ascebc; |
| 802 | int rc; |
| 803 | |
Sebastian Ott | 1e53209 | 2014-01-27 13:28:10 +0100 | [diff] [blame] | 804 | cdev = ccw_device_create_console(&raw3270_ccw_driver); |
Sebastian Ott | 2253e8d | 2014-01-27 13:26:10 +0100 | [diff] [blame] | 805 | if (IS_ERR(cdev)) |
| 806 | return ERR_CAST(cdev); |
| 807 | |
Heiko Carstens | 33403dc | 2009-06-22 12:08:05 +0200 | [diff] [blame] | 808 | rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA); |
| 809 | ascebc = kzalloc(256, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | rc = raw3270_setup_device(cdev, rp, ascebc); |
| 811 | if (rc) |
| 812 | return ERR_PTR(rc); |
| 813 | set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags); |
Sebastian Ott | 1e53209 | 2014-01-27 13:28:10 +0100 | [diff] [blame] | 814 | |
| 815 | rc = ccw_device_enable_console(cdev); |
| 816 | if (rc) { |
| 817 | ccw_device_destroy_console(cdev); |
| 818 | return ERR_PTR(rc); |
| 819 | } |
| 820 | |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 821 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
| 822 | do { |
| 823 | __raw3270_reset_device(rp); |
| 824 | while (!raw3270_state_final(rp)) { |
Sebastian Ott | 188561a | 2013-04-13 12:53:21 +0200 | [diff] [blame] | 825 | ccw_device_wait_idle(rp->cdev); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 826 | barrier(); |
| 827 | } |
| 828 | } while (rp->state != RAW3270_STATE_READY); |
| 829 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return rp; |
| 831 | } |
| 832 | |
| 833 | void |
| 834 | raw3270_wait_cons_dev(struct raw3270 *rp) |
| 835 | { |
| 836 | unsigned long flags; |
| 837 | |
| 838 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
Sebastian Ott | 188561a | 2013-04-13 12:53:21 +0200 | [diff] [blame] | 839 | ccw_device_wait_idle(rp->cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 841 | } |
| 842 | |
| 843 | #endif |
| 844 | |
| 845 | /* |
| 846 | * Create a 3270 device structure. |
| 847 | */ |
| 848 | static struct raw3270 * |
| 849 | raw3270_create_device(struct ccw_device *cdev) |
| 850 | { |
| 851 | struct raw3270 *rp; |
| 852 | char *ascebc; |
| 853 | int rc; |
| 854 | |
Martin Schwidefsky | bd1cb5d | 2014-03-21 15:24:27 +0100 | [diff] [blame] | 855 | rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (!rp) |
| 857 | return ERR_PTR(-ENOMEM); |
| 858 | ascebc = kmalloc(256, GFP_KERNEL); |
| 859 | if (!ascebc) { |
| 860 | kfree(rp); |
| 861 | return ERR_PTR(-ENOMEM); |
| 862 | } |
| 863 | rc = raw3270_setup_device(cdev, rp, ascebc); |
| 864 | if (rc) { |
| 865 | kfree(rp->ascebc); |
| 866 | kfree(rp); |
| 867 | rp = ERR_PTR(rc); |
| 868 | } |
| 869 | /* Get reference to ccw_device structure. */ |
| 870 | get_device(&cdev->dev); |
| 871 | return rp; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * Activate a view. |
| 876 | */ |
| 877 | int |
| 878 | raw3270_activate_view(struct raw3270_view *view) |
| 879 | { |
| 880 | struct raw3270 *rp; |
| 881 | struct raw3270_view *oldview, *nv; |
| 882 | unsigned long flags; |
| 883 | int rc; |
| 884 | |
| 885 | rp = view->dev; |
| 886 | if (!rp) |
| 887 | return -ENODEV; |
| 888 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
| 889 | if (rp->view == view) |
| 890 | rc = 0; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 891 | else if (!raw3270_state_ready(rp)) |
| 892 | rc = -EBUSY; |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 893 | else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) |
| 894 | rc = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | else { |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 896 | oldview = NULL; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 897 | if (rp->view && rp->view->fn->deactivate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | oldview = rp->view; |
| 899 | oldview->fn->deactivate(oldview); |
| 900 | } |
| 901 | rp->view = view; |
| 902 | rc = view->fn->activate(view); |
| 903 | if (rc) { |
| 904 | /* Didn't work. Try to reactivate the old view. */ |
| 905 | rp->view = oldview; |
| 906 | if (!oldview || oldview->fn->activate(oldview) != 0) { |
| 907 | /* Didn't work as well. Try any other view. */ |
| 908 | list_for_each_entry(nv, &rp->view_list, list) |
| 909 | if (nv != view && nv != oldview) { |
| 910 | rp->view = nv; |
| 911 | if (nv->fn->activate(nv) == 0) |
| 912 | break; |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 913 | rp->view = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 919 | return rc; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Deactivate current view. |
| 924 | */ |
| 925 | void |
| 926 | raw3270_deactivate_view(struct raw3270_view *view) |
| 927 | { |
| 928 | unsigned long flags; |
| 929 | struct raw3270 *rp; |
| 930 | |
| 931 | rp = view->dev; |
| 932 | if (!rp) |
| 933 | return; |
| 934 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
| 935 | if (rp->view == view) { |
| 936 | view->fn->deactivate(view); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 937 | rp->view = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* Move deactivated view to end of list. */ |
| 939 | list_del_init(&view->list); |
| 940 | list_add_tail(&view->list, &rp->view_list); |
| 941 | /* Try to activate another view. */ |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 942 | if (raw3270_state_ready(rp) && |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 943 | !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) { |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 944 | list_for_each_entry(view, &rp->view_list, list) { |
| 945 | rp->view = view; |
| 946 | if (view->fn->activate(view) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | break; |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 948 | rp->view = NULL; |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Add view to device with minor "minor". |
| 957 | */ |
| 958 | int |
| 959 | raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor) |
| 960 | { |
| 961 | unsigned long flags; |
| 962 | struct raw3270 *rp; |
| 963 | int rc; |
| 964 | |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 965 | if (minor <= 0) |
| 966 | return -ENODEV; |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 967 | mutex_lock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | rc = -ENODEV; |
| 969 | list_for_each_entry(rp, &raw3270_devices, list) { |
| 970 | if (rp->minor != minor) |
| 971 | continue; |
| 972 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 973 | atomic_set(&view->ref_count, 2); |
| 974 | view->dev = rp; |
| 975 | view->fn = fn; |
| 976 | view->model = rp->model; |
| 977 | view->rows = rp->rows; |
| 978 | view->cols = rp->cols; |
| 979 | view->ascebc = rp->ascebc; |
| 980 | spin_lock_init(&view->lock); |
| 981 | list_add(&view->list, &rp->view_list); |
| 982 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 984 | break; |
| 985 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 986 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | return rc; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Find specific view of device with minor "minor". |
| 992 | */ |
| 993 | struct raw3270_view * |
| 994 | raw3270_find_view(struct raw3270_fn *fn, int minor) |
| 995 | { |
| 996 | struct raw3270 *rp; |
| 997 | struct raw3270_view *view, *tmp; |
| 998 | unsigned long flags; |
| 999 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1000 | mutex_lock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | view = ERR_PTR(-ENODEV); |
| 1002 | list_for_each_entry(rp, &raw3270_devices, list) { |
| 1003 | if (rp->minor != minor) |
| 1004 | continue; |
| 1005 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 1006 | list_for_each_entry(tmp, &rp->view_list, list) { |
| 1007 | if (tmp->fn == fn) { |
| 1008 | raw3270_get_view(tmp); |
| 1009 | view = tmp; |
| 1010 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 1014 | break; |
| 1015 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1016 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | return view; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
| 1021 | * Remove view from device and free view structure via call to view->fn->free. |
| 1022 | */ |
| 1023 | void |
| 1024 | raw3270_del_view(struct raw3270_view *view) |
| 1025 | { |
| 1026 | unsigned long flags; |
| 1027 | struct raw3270 *rp; |
| 1028 | struct raw3270_view *nv; |
| 1029 | |
| 1030 | rp = view->dev; |
| 1031 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
| 1032 | if (rp->view == view) { |
| 1033 | view->fn->deactivate(view); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1034 | rp->view = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | list_del_init(&view->list); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 1037 | if (!rp->view && raw3270_state_ready(rp) && |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1038 | !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | /* Try to activate another view. */ |
| 1040 | list_for_each_entry(nv, &rp->view_list, list) { |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 1041 | if (nv->fn->activate(nv) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | rp->view = nv; |
| 1043 | break; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 1048 | /* Wait for reference counter to drop to zero. */ |
| 1049 | atomic_dec(&view->ref_count); |
| 1050 | wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0); |
| 1051 | if (view->fn->free) |
| 1052 | view->fn->free(view); |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * Remove a 3270 device structure. |
| 1057 | */ |
| 1058 | static void |
| 1059 | raw3270_delete_device(struct raw3270 *rp) |
| 1060 | { |
| 1061 | struct ccw_device *cdev; |
| 1062 | |
| 1063 | /* Remove from device chain. */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1064 | mutex_lock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | list_del_init(&rp->list); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1066 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
| 1068 | /* Disconnect from ccw_device. */ |
| 1069 | cdev = rp->cdev; |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1070 | rp->cdev = NULL; |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1071 | dev_set_drvdata(&cdev->dev, NULL); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1072 | cdev->handler = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
| 1074 | /* Put ccw_device structure. */ |
| 1075 | put_device(&cdev->dev); |
| 1076 | |
| 1077 | /* Now free raw3270 structure. */ |
| 1078 | kfree(rp->ascebc); |
| 1079 | kfree(rp); |
| 1080 | } |
| 1081 | |
| 1082 | static int |
| 1083 | raw3270_probe (struct ccw_device *cdev) |
| 1084 | { |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * Additional attributes for a 3270 device |
| 1090 | */ |
| 1091 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 1092 | raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
| 1094 | return snprintf(buf, PAGE_SIZE, "%i\n", |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1095 | ((struct raw3270 *) dev_get_drvdata(dev))->model); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | } |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1097 | static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
| 1099 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 1100 | raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | { |
| 1102 | return snprintf(buf, PAGE_SIZE, "%i\n", |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1103 | ((struct raw3270 *) dev_get_drvdata(dev))->rows); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1105 | static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
| 1107 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 1108 | raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | { |
| 1110 | return snprintf(buf, PAGE_SIZE, "%i\n", |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1111 | ((struct raw3270 *) dev_get_drvdata(dev))->cols); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | } |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1113 | static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | static struct attribute * raw3270_attrs[] = { |
| 1116 | &dev_attr_model.attr, |
| 1117 | &dev_attr_rows.attr, |
| 1118 | &dev_attr_columns.attr, |
| 1119 | NULL, |
| 1120 | }; |
| 1121 | |
| 1122 | static struct attribute_group raw3270_attr_group = { |
| 1123 | .attrs = raw3270_attrs, |
| 1124 | }; |
| 1125 | |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 1126 | static int raw3270_create_attributes(struct raw3270 *rp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | { |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1128 | return sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * Notifier for device addition/removal |
| 1133 | */ |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 1134 | static LIST_HEAD(raw3270_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1136 | int raw3270_register_notifier(struct raw3270_notifier *notifier) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | struct raw3270 *rp; |
| 1139 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1140 | mutex_lock(&raw3270_mutex); |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1141 | list_add_tail(¬ifier->list, &raw3270_notifier); |
| 1142 | list_for_each_entry(rp, &raw3270_devices, list) |
| 1143 | notifier->create(rp->minor); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1144 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | return 0; |
| 1146 | } |
| 1147 | |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1148 | void raw3270_unregister_notifier(struct raw3270_notifier *notifier) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | { |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1150 | struct raw3270 *rp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1152 | mutex_lock(&raw3270_mutex); |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1153 | list_for_each_entry(rp, &raw3270_devices, list) |
| 1154 | notifier->destroy(rp->minor); |
| 1155 | list_del(¬ifier->list); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1156 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Set 3270 device online. |
| 1161 | */ |
| 1162 | static int |
| 1163 | raw3270_set_online (struct ccw_device *cdev) |
| 1164 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | struct raw3270_notifier *np; |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1166 | struct raw3270 *rp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | int rc; |
| 1168 | |
| 1169 | rp = raw3270_create_device(cdev); |
| 1170 | if (IS_ERR(rp)) |
| 1171 | return PTR_ERR(rp); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 1172 | rc = raw3270_create_attributes(rp); |
| 1173 | if (rc) |
| 1174 | goto failure; |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 1175 | raw3270_reset_device(rp); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1176 | mutex_lock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | list_for_each_entry(np, &raw3270_notifier, list) |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1178 | np->create(rp->minor); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1179 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | return 0; |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 1181 | |
| 1182 | failure: |
| 1183 | raw3270_delete_device(rp); |
| 1184 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* |
| 1188 | * Remove 3270 device structure. |
| 1189 | */ |
| 1190 | static void |
| 1191 | raw3270_remove (struct ccw_device *cdev) |
| 1192 | { |
| 1193 | unsigned long flags; |
| 1194 | struct raw3270 *rp; |
| 1195 | struct raw3270_view *v; |
| 1196 | struct raw3270_notifier *np; |
| 1197 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1198 | rp = dev_get_drvdata(&cdev->dev); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 1199 | /* |
| 1200 | * _remove is the opposite of _probe; it's probe that |
| 1201 | * should set up rp. raw3270_remove gets entered for |
| 1202 | * devices even if they haven't been varied online. |
| 1203 | * Thus, rp may validly be NULL here. |
| 1204 | */ |
| 1205 | if (rp == NULL) |
| 1206 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
| 1208 | sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group); |
| 1209 | |
| 1210 | /* Deactivate current view and remove all views. */ |
| 1211 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1212 | if (rp->view) { |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 1213 | if (rp->view->fn->deactivate) |
| 1214 | rp->view->fn->deactivate(rp->view); |
Heiko Carstens | d2c993d | 2006-07-12 16:41:55 +0200 | [diff] [blame] | 1215 | rp->view = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | } |
| 1217 | while (!list_empty(&rp->view_list)) { |
| 1218 | v = list_entry(rp->view_list.next, struct raw3270_view, list); |
| 1219 | if (v->fn->release) |
| 1220 | v->fn->release(v); |
| 1221 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1222 | raw3270_del_view(v); |
| 1223 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1224 | } |
| 1225 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1226 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1227 | mutex_lock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | list_for_each_entry(np, &raw3270_notifier, list) |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1229 | np->destroy(rp->minor); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1230 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
| 1232 | /* Reset 3270 device. */ |
| 1233 | raw3270_reset_device(rp); |
| 1234 | /* And finally remove it. */ |
| 1235 | raw3270_delete_device(rp); |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * Set 3270 device offline. |
| 1240 | */ |
| 1241 | static int |
| 1242 | raw3270_set_offline (struct ccw_device *cdev) |
| 1243 | { |
| 1244 | struct raw3270 *rp; |
| 1245 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1246 | rp = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) |
| 1248 | return -EBUSY; |
| 1249 | raw3270_remove(cdev); |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1253 | static int raw3270_pm_stop(struct ccw_device *cdev) |
| 1254 | { |
| 1255 | struct raw3270 *rp; |
| 1256 | struct raw3270_view *view; |
| 1257 | unsigned long flags; |
| 1258 | |
Martin Schwidefsky | 4f0076f | 2009-06-22 12:08:19 +0200 | [diff] [blame] | 1259 | rp = dev_get_drvdata(&cdev->dev); |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1260 | if (!rp) |
| 1261 | return 0; |
| 1262 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 1263 | if (rp->view && rp->view->fn->deactivate) |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1264 | rp->view->fn->deactivate(rp->view); |
| 1265 | if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) { |
| 1266 | /* |
| 1267 | * Release tty and fullscreen for all non-console |
| 1268 | * devices. |
| 1269 | */ |
| 1270 | list_for_each_entry(view, &rp->view_list, list) { |
| 1271 | if (view->fn->release) |
| 1272 | view->fn->release(view); |
| 1273 | } |
| 1274 | } |
| 1275 | set_bit(RAW3270_FLAGS_FROZEN, &rp->flags); |
| 1276 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | static int raw3270_pm_start(struct ccw_device *cdev) |
| 1281 | { |
| 1282 | struct raw3270 *rp; |
| 1283 | unsigned long flags; |
| 1284 | |
Martin Schwidefsky | 4f0076f | 2009-06-22 12:08:19 +0200 | [diff] [blame] | 1285 | rp = dev_get_drvdata(&cdev->dev); |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1286 | if (!rp) |
| 1287 | return 0; |
| 1288 | spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags); |
| 1289 | clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags); |
Martin Schwidefsky | 4d334fd | 2013-01-04 14:55:13 +0100 | [diff] [blame] | 1290 | if (rp->view && rp->view->fn->activate) |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1291 | rp->view->fn->activate(rp->view); |
| 1292 | spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | void raw3270_pm_unfreeze(struct raw3270_view *view) |
| 1297 | { |
Sebastian Ott | 34483ca | 2009-10-06 10:33:56 +0200 | [diff] [blame] | 1298 | #ifdef CONFIG_TN3270_CONSOLE |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1299 | struct raw3270 *rp; |
| 1300 | |
| 1301 | rp = view->dev; |
| 1302 | if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) |
Sebastian Ott | f10ccca | 2013-04-13 12:56:51 +0200 | [diff] [blame] | 1303 | ccw_device_force_console(rp->cdev); |
Sebastian Ott | 34483ca | 2009-10-06 10:33:56 +0200 | [diff] [blame] | 1304 | #endif |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1305 | } |
| 1306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | static struct ccw_device_id raw3270_id[] = { |
| 1308 | { CCW_DEVICE(0x3270, 0) }, |
| 1309 | { CCW_DEVICE(0x3271, 0) }, |
| 1310 | { CCW_DEVICE(0x3272, 0) }, |
| 1311 | { CCW_DEVICE(0x3273, 0) }, |
| 1312 | { CCW_DEVICE(0x3274, 0) }, |
| 1313 | { CCW_DEVICE(0x3275, 0) }, |
| 1314 | { CCW_DEVICE(0x3276, 0) }, |
| 1315 | { CCW_DEVICE(0x3277, 0) }, |
| 1316 | { CCW_DEVICE(0x3278, 0) }, |
| 1317 | { CCW_DEVICE(0x3279, 0) }, |
| 1318 | { CCW_DEVICE(0x3174, 0) }, |
| 1319 | { /* end of list */ }, |
| 1320 | }; |
| 1321 | |
| 1322 | static struct ccw_driver raw3270_ccw_driver = { |
Sebastian Ott | 3bda058 | 2011-03-23 10:16:02 +0100 | [diff] [blame] | 1323 | .driver = { |
| 1324 | .name = "3270", |
| 1325 | .owner = THIS_MODULE, |
| 1326 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | .ids = raw3270_id, |
| 1328 | .probe = &raw3270_probe, |
| 1329 | .remove = &raw3270_remove, |
| 1330 | .set_online = &raw3270_set_online, |
| 1331 | .set_offline = &raw3270_set_offline, |
Martin Schwidefsky | 4b214a0 | 2009-06-16 10:30:47 +0200 | [diff] [blame] | 1332 | .freeze = &raw3270_pm_stop, |
| 1333 | .thaw = &raw3270_pm_start, |
| 1334 | .restore = &raw3270_pm_start, |
Heiko Carstens | 420f42e | 2013-01-02 15:18:18 +0100 | [diff] [blame] | 1335 | .int_class = IRQIO_C70, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | }; |
| 1337 | |
| 1338 | static int |
| 1339 | raw3270_init(void) |
| 1340 | { |
| 1341 | struct raw3270 *rp; |
| 1342 | int rc; |
| 1343 | |
| 1344 | if (raw3270_registered) |
| 1345 | return 0; |
| 1346 | raw3270_registered = 1; |
| 1347 | rc = ccw_driver_register(&raw3270_ccw_driver); |
| 1348 | if (rc == 0) { |
| 1349 | /* Create attributes for early (= console) device. */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1350 | mutex_lock(&raw3270_mutex); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 1351 | class3270 = class_create(THIS_MODULE, "3270"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | list_for_each_entry(rp, &raw3270_devices, list) { |
| 1353 | get_device(&rp->cdev->dev); |
| 1354 | raw3270_create_attributes(rp); |
| 1355 | } |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 1356 | mutex_unlock(&raw3270_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | } |
| 1358 | return rc; |
| 1359 | } |
| 1360 | |
| 1361 | static void |
| 1362 | raw3270_exit(void) |
| 1363 | { |
| 1364 | ccw_driver_unregister(&raw3270_ccw_driver); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 1365 | class_destroy(class3270); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | MODULE_LICENSE("GPL"); |
| 1369 | |
| 1370 | module_init(raw3270_init); |
| 1371 | module_exit(raw3270_exit); |
| 1372 | |
Martin Schwidefsky | c95571e | 2013-01-08 15:31:11 +0100 | [diff] [blame] | 1373 | EXPORT_SYMBOL(class3270); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | EXPORT_SYMBOL(raw3270_request_alloc); |
| 1375 | EXPORT_SYMBOL(raw3270_request_free); |
| 1376 | EXPORT_SYMBOL(raw3270_request_reset); |
| 1377 | EXPORT_SYMBOL(raw3270_request_set_cmd); |
| 1378 | EXPORT_SYMBOL(raw3270_request_add_data); |
| 1379 | EXPORT_SYMBOL(raw3270_request_set_data); |
| 1380 | EXPORT_SYMBOL(raw3270_request_set_idal); |
| 1381 | EXPORT_SYMBOL(raw3270_buffer_address); |
| 1382 | EXPORT_SYMBOL(raw3270_add_view); |
| 1383 | EXPORT_SYMBOL(raw3270_del_view); |
| 1384 | EXPORT_SYMBOL(raw3270_find_view); |
| 1385 | EXPORT_SYMBOL(raw3270_activate_view); |
| 1386 | EXPORT_SYMBOL(raw3270_deactivate_view); |
| 1387 | EXPORT_SYMBOL(raw3270_start); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 1388 | EXPORT_SYMBOL(raw3270_start_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | EXPORT_SYMBOL(raw3270_start_irq); |
Richard Hitt | ed3cb6f | 2005-10-30 15:00:10 -0800 | [diff] [blame] | 1390 | EXPORT_SYMBOL(raw3270_reset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | EXPORT_SYMBOL(raw3270_register_notifier); |
| 1392 | EXPORT_SYMBOL(raw3270_unregister_notifier); |
| 1393 | EXPORT_SYMBOL(raw3270_wait_queue); |