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