1 // SPDX-License-Identifier: GPL-2.0 2 3 /*************************************************************************** 4 * copyright : (C) 2001, 2002 by Frank Mori Hess 5 ***************************************************************************/ 6 7 #define dev_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include "ibsys.h" 10 #include <linux/delay.h> 11 #include <linux/kthread.h> 12 #include <linux/vmalloc.h> 13 14 /* 15 * IBCAC 16 * Return to the controller active state from the 17 * controller standby state, i.e., turn ATN on. Note 18 * that in order to enter the controller active state 19 * from the controller idle state, ibsic must be called. 20 * If sync is non-zero, attempt to take control synchronously. 21 * If fallback_to_async is non-zero, try to take control asynchronously 22 * if synchronous attempt fails. 23 */ 24 int ibcac(struct gpib_board *board, int sync, int fallback_to_async) 25 { 26 int status = ibstatus(board); 27 int retval; 28 29 if ((status & CIC) == 0) 30 return -EINVAL; 31 32 if (status & ATN) 33 return 0; 34 35 if (sync && (status & LACS) == 0) 36 /* 37 * tcs (take control synchronously) can only possibly work when 38 * controller is listener. Error code also needs to be -ETIMEDOUT 39 * or it will giveout without doing fallback. 40 */ 41 retval = -ETIMEDOUT; 42 else 43 retval = board->interface->take_control(board, sync); 44 45 if (retval < 0 && fallback_to_async) { 46 if (sync && retval == -ETIMEDOUT) 47 retval = board->interface->take_control(board, 0); 48 } 49 board->interface->update_status(board, 0); 50 51 return retval; 52 } 53 54 /* 55 * After ATN is asserted, it should cause any connected devices 56 * to start listening for command bytes and leave acceptor idle state. 57 * So if ATN is asserted and neither NDAC or NRFD are asserted, 58 * then there are no devices and ibcmd should error out immediately. 59 * Some gpib hardware sees itself asserting NDAC/NRFD when it 60 * is controller in charge, in which case this check will 61 * do nothing useful (but shouldn't cause any harm either). 62 * Drivers that don't need this check (ni_usb for example) may 63 * set the skip_check_for_command_acceptors flag in their 64 * gpib_interface_struct to avoid useless overhead. 65 */ 66 static int check_for_command_acceptors(struct gpib_board *board) 67 { 68 int lines; 69 70 if (board->interface->skip_check_for_command_acceptors) 71 return 0; 72 if (!board->interface->line_status) 73 return 0; 74 75 udelay(2); // allow time for devices to respond to ATN if it was just asserted 76 77 lines = board->interface->line_status(board); 78 if (lines < 0) 79 return lines; 80 81 if ((lines & VALID_NRFD) && (lines & VALID_NDAC)) { 82 if ((lines & BUS_NRFD) == 0 && (lines & BUS_NDAC) == 0) 83 return -ENOTCONN; 84 } 85 86 return 0; 87 } 88 89 /* 90 * IBCMD 91 * Write cnt command bytes from buf to the GPIB. The 92 * command operation terminates only on I/O complete. 93 * 94 * NOTE: 95 * 1. Prior to beginning the command, the interface is 96 * placed in the controller active state. 97 * 2. Before calling ibcmd for the first time, ibsic 98 * must be called to initialize the GPIB and enable 99 * the interface to leave the controller idle state. 100 */ 101 int ibcmd(struct gpib_board *board, u8 *buf, size_t length, size_t *bytes_written) 102 { 103 ssize_t ret = 0; 104 int status; 105 106 *bytes_written = 0; 107 108 status = ibstatus(board); 109 110 if ((status & CIC) == 0) 111 return -EINVAL; 112 113 os_start_timer(board, board->usec_timeout); 114 115 ret = ibcac(board, 1, 1); 116 if (ret == 0) { 117 ret = check_for_command_acceptors(board); 118 if (ret == 0) 119 ret = board->interface->command(board, buf, length, bytes_written); 120 } 121 122 os_remove_timer(board); 123 124 if (io_timed_out(board)) 125 ret = -ETIMEDOUT; 126 127 return ret; 128 } 129 130 /* 131 * IBGTS 132 * Go to the controller standby state from the controller 133 * active state, i.e., turn ATN off. 134 */ 135 136 int ibgts(struct gpib_board *board) 137 { 138 int status = ibstatus(board); 139 int retval; 140 141 if ((status & CIC) == 0) 142 return -EINVAL; 143 144 retval = board->interface->go_to_standby(board); /* go to standby */ 145 146 board->interface->update_status(board, 0); 147 148 return retval; 149 } 150 151 static int autospoll_wait_should_wake_up(struct gpib_board *board) 152 { 153 int retval; 154 155 mutex_lock(&board->big_gpib_mutex); 156 157 retval = board->master && board->autospollers > 0 && 158 !atomic_read(&board->stuck_srq) && 159 test_and_clear_bit(SRQI_NUM, &board->status); 160 161 mutex_unlock(&board->big_gpib_mutex); 162 return retval; 163 } 164 165 static int autospoll_thread(void *board_void) 166 { 167 struct gpib_board *board = board_void; 168 int retval = 0; 169 170 dev_dbg(board->gpib_dev, "entering autospoll thread\n"); 171 172 while (1) { 173 wait_event_interruptible(board->wait, 174 kthread_should_stop() || 175 autospoll_wait_should_wake_up(board)); 176 dev_dbg(board->gpib_dev, "autospoll wait satisfied\n"); 177 if (kthread_should_stop()) 178 break; 179 180 mutex_lock(&board->big_gpib_mutex); 181 /* make sure we are still good after we have lock */ 182 if (board->autospollers <= 0 || board->master == 0) { 183 mutex_unlock(&board->big_gpib_mutex); 184 continue; 185 } 186 mutex_unlock(&board->big_gpib_mutex); 187 188 if (try_module_get(board->provider_module)) { 189 retval = autopoll_all_devices(board); 190 module_put(board->provider_module); 191 } else { 192 dev_err(board->gpib_dev, "try_module_get() failed!\n"); 193 } 194 if (retval <= 0) { 195 dev_err(board->gpib_dev, "stuck SRQ\n"); 196 197 atomic_set(&board->stuck_srq, 1); // XXX could be better 198 set_bit(SRQI_NUM, &board->status); 199 } 200 } 201 return retval; 202 } 203 204 int ibonline(struct gpib_board *board) 205 { 206 int retval; 207 208 if (board->online) 209 return -EBUSY; 210 if (!board->interface) 211 return -ENODEV; 212 retval = gpib_allocate_board(board); 213 if (retval < 0) 214 return retval; 215 216 board->dev = NULL; 217 board->local_ppoll_mode = 0; 218 retval = board->interface->attach(board, &board->config); 219 if (retval < 0) { 220 board->interface->detach(board); 221 return retval; 222 } 223 /* 224 * nios2nommu on 2.6.11 uclinux kernel has weird problems 225 * with autospoll thread causing huge slowdowns 226 */ 227 #ifndef CONFIG_NIOS2 228 board->autospoll_task = kthread_run(&autospoll_thread, board, 229 "gpib%d_autospoll_kthread", board->minor); 230 retval = IS_ERR(board->autospoll_task); 231 if (retval) { 232 dev_err(board->gpib_dev, "failed to create autospoll thread\n"); 233 board->interface->detach(board); 234 return retval; 235 } 236 #endif 237 board->online = 1; 238 dev_dbg(board->gpib_dev, "board online\n"); 239 240 return 0; 241 } 242 243 /* XXX need to make sure board is generally not in use (grab board lock?) */ 244 int iboffline(struct gpib_board *board) 245 { 246 int retval; 247 248 if (board->online == 0) 249 return 0; 250 if (!board->interface) 251 return -ENODEV; 252 253 if (board->autospoll_task && !IS_ERR(board->autospoll_task)) { 254 retval = kthread_stop(board->autospoll_task); 255 if (retval) 256 dev_err(board->gpib_dev, "kthread_stop returned %i\n", retval); 257 board->autospoll_task = NULL; 258 } 259 260 board->interface->detach(board); 261 gpib_deallocate_board(board); 262 board->online = 0; 263 dev_dbg(board->gpib_dev, "board offline\n"); 264 265 return 0; 266 } 267 268 /* 269 * IBLINES 270 * Poll the GPIB control lines and return their status in buf. 271 * 272 * LSB (bits 0-7) - VALID lines mask (lines that can be monitored). 273 * Next LSB (bits 8-15) - STATUS lines mask (lines that are currently set). 274 * 275 */ 276 int iblines(const struct gpib_board *board, short *lines) 277 { 278 int retval; 279 280 *lines = 0; 281 if (!board->interface->line_status) 282 return 0; 283 retval = board->interface->line_status(board); 284 if (retval < 0) 285 return retval; 286 *lines = retval; 287 return 0; 288 } 289 290 /* 291 * IBRD 292 * Read up to 'length' bytes of data from the GPIB into buf. End 293 * on detection of END (EOI and or EOS) and set 'end_flag'. 294 * 295 * NOTE: 296 * 1. The interface is placed in the controller standby 297 * state prior to beginning the read. 298 * 2. Prior to calling ibrd, the intended devices as well 299 * as the interface board itself must be addressed by 300 * calling ibcmd. 301 */ 302 303 int ibrd(struct gpib_board *board, u8 *buf, size_t length, int *end_flag, size_t *nbytes) 304 { 305 ssize_t ret = 0; 306 int retval; 307 size_t bytes_read; 308 309 *nbytes = 0; 310 *end_flag = 0; 311 if (length == 0) 312 return 0; 313 314 if (board->master) { 315 retval = ibgts(board); 316 if (retval < 0) 317 return retval; 318 } 319 /* 320 * XXX resetting timer here could cause timeouts take longer than they should, 321 * since read_ioctl calls this 322 * function in a loop, there is probably a similar problem with writes/commands 323 */ 324 os_start_timer(board, board->usec_timeout); 325 326 do { 327 ret = board->interface->read(board, buf, length - *nbytes, end_flag, &bytes_read); 328 if (ret < 0) 329 goto ibrd_out; 330 331 buf += bytes_read; 332 *nbytes += bytes_read; 333 if (need_resched()) 334 schedule(); 335 } while (ret == 0 && *nbytes > 0 && *nbytes < length && *end_flag == 0); 336 ibrd_out: 337 os_remove_timer(board); 338 339 return ret; 340 } 341 342 /* 343 * IBRPP 344 * Conduct a parallel poll and return the byte in buf. 345 * 346 * NOTE: 347 * 1. Prior to conducting the poll the interface is placed 348 * in the controller active state. 349 */ 350 int ibrpp(struct gpib_board *board, u8 *result) 351 { 352 int retval = 0; 353 354 os_start_timer(board, board->usec_timeout); 355 retval = ibcac(board, 1, 1); 356 if (retval) 357 return -1; 358 359 retval = board->interface->parallel_poll(board, result); 360 361 os_remove_timer(board); 362 return retval; 363 } 364 365 int ibppc(struct gpib_board *board, u8 configuration) 366 { 367 configuration &= 0x1f; 368 board->interface->parallel_poll_configure(board, configuration); 369 board->parallel_poll_configuration = configuration; 370 371 return 0; 372 } 373 374 int ibrsv2(struct gpib_board *board, u8 status_byte, int new_reason_for_service) 375 { 376 int board_status = ibstatus(board); 377 const unsigned int MSS = status_byte & request_service_bit; 378 379 if ((board_status & CIC)) 380 return -EINVAL; 381 382 if (MSS == 0 && new_reason_for_service) 383 return -EINVAL; 384 385 if (board->interface->serial_poll_response2) { 386 board->interface->serial_poll_response2(board, status_byte, new_reason_for_service); 387 // fall back on simpler serial_poll_response if the behavior would be the same 388 } else if (board->interface->serial_poll_response && 389 (MSS == 0 || (MSS && new_reason_for_service))) { 390 board->interface->serial_poll_response(board, status_byte); 391 } else { 392 return -EOPNOTSUPP; 393 } 394 395 return 0; 396 } 397 398 /* 399 * IBSIC 400 * Send IFC for at least 100 microseconds. 401 * 402 * NOTE: 403 * 1. Ibsic must be called prior to the first call to 404 * ibcmd in order to initialize the bus and enable the 405 * interface to leave the controller idle state. 406 */ 407 int ibsic(struct gpib_board *board, unsigned int usec_duration) 408 { 409 if (board->master == 0) 410 return -EINVAL; 411 412 if (usec_duration < 100) 413 usec_duration = 100; 414 if (usec_duration > 1000) 415 usec_duration = 1000; 416 417 dev_dbg(board->gpib_dev, "sending interface clear, delay = %ius\n", usec_duration); 418 board->interface->interface_clear(board, 1); 419 udelay(usec_duration); 420 board->interface->interface_clear(board, 0); 421 422 return 0; 423 } 424 425 int ibrsc(struct gpib_board *board, int request_control) 426 { 427 int retval; 428 429 if (!board->interface->request_system_control) 430 return -EPERM; 431 432 retval = board->interface->request_system_control(board, request_control); 433 434 if (retval) 435 return retval; 436 437 board->master = request_control != 0; 438 439 return 0; 440 } 441 442 /* 443 * IBSRE 444 * Send REN true if v is non-zero or false if v is zero. 445 */ 446 int ibsre(struct gpib_board *board, int enable) 447 { 448 if (board->master == 0) 449 return -EINVAL; 450 451 board->interface->remote_enable(board, enable); /* set or clear REN */ 452 if (!enable) 453 usleep_range(100, 150); 454 455 return 0; 456 } 457 458 /* 459 * IBPAD 460 * change the GPIB address of the interface board. The address 461 * must be 0 through 30. ibonl resets the address to PAD. 462 */ 463 int ibpad(struct gpib_board *board, unsigned int addr) 464 { 465 if (addr > MAX_GPIB_PRIMARY_ADDRESS) 466 return -EINVAL; 467 468 board->pad = addr; 469 if (board->online) 470 board->interface->primary_address(board, board->pad); 471 dev_dbg(board->gpib_dev, "set primary addr to %i\n", board->pad); 472 return 0; 473 } 474 475 /* 476 * IBSAD 477 * change the secondary GPIB address of the interface board. 478 * The address must be 0 through 30, or negative disables. ibonl resets the 479 * address to SAD. 480 */ 481 int ibsad(struct gpib_board *board, int addr) 482 { 483 if (addr > MAX_GPIB_SECONDARY_ADDRESS) 484 return -EINVAL; 485 board->sad = addr; 486 if (board->online) { 487 if (board->sad >= 0) 488 board->interface->secondary_address(board, board->sad, 1); 489 else 490 board->interface->secondary_address(board, 0, 0); 491 } 492 dev_dbg(board->gpib_dev, "set secondary addr to %i\n", board->sad); 493 494 return 0; 495 } 496 497 /* 498 * IBEOS 499 * Set the end-of-string modes for I/O operations to v. 500 * 501 */ 502 int ibeos(struct gpib_board *board, int eos, int eosflags) 503 { 504 int retval; 505 506 if (eosflags & ~EOS_MASK) 507 return -EINVAL; 508 if (eosflags & REOS) { 509 retval = board->interface->enable_eos(board, eos, eosflags & BIN); 510 } else { 511 board->interface->disable_eos(board); 512 retval = 0; 513 } 514 return retval; 515 } 516 517 int ibstatus(struct gpib_board *board) 518 { 519 return general_ibstatus(board, NULL, 0, 0, NULL); 520 } 521 522 int general_ibstatus(struct gpib_board *board, const struct gpib_status_queue *device, 523 int clear_mask, int set_mask, struct gpib_descriptor *desc) 524 { 525 int status = 0; 526 short line_status; 527 528 if (board->private_data) { 529 status = board->interface->update_status(board, clear_mask); 530 /* 531 * XXX should probably stop having drivers use TIMO bit in 532 * board->status to avoid confusion 533 */ 534 status &= ~TIMO; 535 /* get real SRQI status if we can */ 536 if (iblines(board, &line_status) == 0) { 537 if ((line_status & VALID_SRQ)) { 538 if ((line_status & BUS_SRQ)) 539 status |= SRQI; 540 else 541 status &= ~SRQI; 542 } 543 } 544 } 545 if (device) 546 if (num_status_bytes(device)) 547 status |= RQS; 548 549 if (desc) { 550 if (set_mask & CMPL) 551 atomic_set(&desc->io_in_progress, 0); 552 else if (clear_mask & CMPL) 553 atomic_set(&desc->io_in_progress, 1); 554 555 if (atomic_read(&desc->io_in_progress)) 556 status &= ~CMPL; 557 else 558 status |= CMPL; 559 } 560 if (num_gpib_events(&board->event_queue)) 561 status |= EVENT; 562 else 563 status &= ~EVENT; 564 565 return status; 566 } 567 568 struct wait_info { 569 struct gpib_board *board; 570 struct timer_list timer; 571 int timed_out; 572 unsigned long usec_timeout; 573 }; 574 575 static void wait_timeout(struct timer_list *t) 576 { 577 struct wait_info *winfo = timer_container_of(winfo, t, timer); 578 579 winfo->timed_out = 1; 580 wake_up_interruptible(&winfo->board->wait); 581 } 582 583 static void init_wait_info(struct wait_info *winfo) 584 { 585 winfo->board = NULL; 586 winfo->timed_out = 0; 587 timer_setup_on_stack(&winfo->timer, wait_timeout, 0); 588 } 589 590 static int wait_satisfied(struct wait_info *winfo, struct gpib_status_queue *status_queue, 591 int wait_mask, int *status, struct gpib_descriptor *desc) 592 { 593 struct gpib_board *board = winfo->board; 594 int temp_status; 595 596 if (mutex_lock_interruptible(&board->big_gpib_mutex)) 597 return -ERESTARTSYS; 598 599 temp_status = general_ibstatus(board, status_queue, 0, 0, desc); 600 601 mutex_unlock(&board->big_gpib_mutex); 602 603 if (winfo->timed_out) 604 temp_status |= TIMO; 605 else 606 temp_status &= ~TIMO; 607 if (wait_mask & temp_status) { 608 *status = temp_status; 609 return 1; 610 } 611 // XXX does wait for END work? 612 return 0; 613 } 614 615 /* install timer interrupt handler */ 616 static void start_wait_timer(struct wait_info *winfo) 617 /* Starts the timeout task */ 618 { 619 winfo->timed_out = 0; 620 621 if (winfo->usec_timeout > 0) 622 mod_timer(&winfo->timer, jiffies + usec_to_jiffies(winfo->usec_timeout)); 623 } 624 625 static void remove_wait_timer(struct wait_info *winfo) 626 { 627 timer_delete_sync(&winfo->timer); 628 timer_destroy_on_stack(&winfo->timer); 629 } 630 631 /* 632 * IBWAIT 633 * Check or wait for a GPIB event to occur. The mask argument 634 * is a bit vector corresponding to the status bit vector. It 635 * has a bit set for each condition which can terminate the wait 636 * If the mask is 0 then 637 * no condition is waited for. 638 */ 639 int ibwait(struct gpib_board *board, int wait_mask, int clear_mask, int set_mask, 640 int *status, unsigned long usec_timeout, struct gpib_descriptor *desc) 641 { 642 int retval = 0; 643 struct gpib_status_queue *status_queue; 644 struct wait_info winfo; 645 646 if (desc->is_board) 647 status_queue = NULL; 648 else 649 status_queue = get_gpib_status_queue(board, desc->pad, desc->sad); 650 651 if (wait_mask == 0) { 652 *status = general_ibstatus(board, status_queue, clear_mask, set_mask, desc); 653 return 0; 654 } 655 656 mutex_unlock(&board->big_gpib_mutex); 657 658 init_wait_info(&winfo); 659 winfo.board = board; 660 winfo.usec_timeout = usec_timeout; 661 start_wait_timer(&winfo); 662 663 if (wait_event_interruptible(board->wait, wait_satisfied(&winfo, status_queue, 664 wait_mask, status, desc))) { 665 dev_dbg(board->gpib_dev, "wait interrupted\n"); 666 retval = -ERESTARTSYS; 667 } 668 remove_wait_timer(&winfo); 669 670 if (retval) 671 return retval; 672 if (mutex_lock_interruptible(&board->big_gpib_mutex)) 673 return -ERESTARTSYS; 674 675 /* make sure we only clear status bits that we are reporting */ 676 if (*status & clear_mask || set_mask) 677 general_ibstatus(board, status_queue, *status & clear_mask, set_mask, NULL); 678 679 return 0; 680 } 681 682 /* 683 * IBWRT 684 * Write cnt bytes of data from buf to the GPIB. The write 685 * operation terminates only on I/O complete. 686 * 687 * NOTE: 688 * 1. Prior to beginning the write, the interface is 689 * placed in the controller standby state. 690 * 2. Prior to calling ibwrt, the intended devices as 691 * well as the interface board itself must be 692 * addressed by calling ibcmd. 693 */ 694 int ibwrt(struct gpib_board *board, u8 *buf, size_t cnt, int send_eoi, size_t *bytes_written) 695 { 696 int ret = 0; 697 int retval; 698 699 if (cnt == 0) 700 return 0; 701 702 if (board->master) { 703 retval = ibgts(board); 704 if (retval < 0) 705 return retval; 706 } 707 os_start_timer(board, board->usec_timeout); 708 ret = board->interface->write(board, buf, cnt, send_eoi, bytes_written); 709 710 if (io_timed_out(board)) 711 ret = -ETIMEDOUT; 712 713 os_remove_timer(board); 714 715 return ret; 716 } 717 718