1 // SPDX-License-Identifier: GPL-2.0 2 3 /*************************************************************************** 4 * copyright : (C) 2001, 2004 by Frank Mori Hess 5 *************************************************************************** 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 #define dev_fmt pr_fmt 10 11 #include "ibsys.h" 12 #include <linux/module.h> 13 #include <linux/wait.h> 14 #include <linux/list.h> 15 #include <linux/fs.h> 16 #include <linux/pci.h> 17 #include <linux/device.h> 18 #include <linux/init.h> 19 #include <linux/string.h> 20 #include <linux/vmalloc.h> 21 #include <linux/fcntl.h> 22 #include <linux/kmod.h> 23 #include <linux/uaccess.h> 24 25 MODULE_LICENSE("GPL"); 26 MODULE_DESCRIPTION("GPIB base support"); 27 MODULE_ALIAS_CHARDEV_MAJOR(GPIB_CODE); 28 29 static int board_type_ioctl(struct gpib_file_private *file_priv, 30 struct gpib_board *board, unsigned long arg); 31 static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 32 unsigned long arg); 33 static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 34 unsigned long arg); 35 static int command_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 36 unsigned long arg); 37 static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg); 38 static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg); 39 static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg); 40 static int wait_ioctl(struct gpib_file_private *file_priv, 41 struct gpib_board *board, unsigned long arg); 42 static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg); 43 static int online_ioctl(struct gpib_board *board, unsigned long arg); 44 static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg); 45 static int take_control_ioctl(struct gpib_board *board, unsigned long arg); 46 static int line_status_ioctl(struct gpib_board *board, unsigned long arg); 47 static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 48 unsigned long arg); 49 static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 50 unsigned long arg); 51 static int eos_ioctl(struct gpib_board *board, unsigned long arg); 52 static int request_service_ioctl(struct gpib_board *board, unsigned long arg); 53 static int request_service2_ioctl(struct gpib_board *board, unsigned long arg); 54 static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg); 55 static int irq_ioctl(struct gpib_board_config *config, unsigned long arg); 56 static int dma_ioctl(struct gpib_board_config *config, unsigned long arg); 57 static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 58 unsigned long arg); 59 static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 60 unsigned long arg); 61 static int timeout_ioctl(struct gpib_board *board, unsigned long arg); 62 static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg); 63 static int board_info_ioctl(const struct gpib_board *board, unsigned long arg); 64 static int ppc_ioctl(struct gpib_board *board, unsigned long arg); 65 static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg); 66 static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg); 67 static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg); 68 static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg); 69 static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg); 70 static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg); 71 static int event_ioctl(struct gpib_board *board, unsigned long arg); 72 static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg); 73 static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg); 74 75 static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board); 76 77 static int pop_gpib_event_nolock(struct gpib_board *board, 78 struct gpib_event_queue *queue, short *event_type); 79 80 /* 81 * Timer functions 82 */ 83 84 /* Watchdog timeout routine */ 85 86 static void watchdog_timeout(struct timer_list *t) 87 { 88 struct gpib_board *board = timer_container_of(board, t, timer); 89 90 set_bit(TIMO_NUM, &board->status); 91 wake_up_interruptible(&board->wait); 92 } 93 94 /* install timer interrupt handler */ 95 void os_start_timer(struct gpib_board *board, unsigned int usec_timeout) 96 /* Starts the timeout task */ 97 { 98 if (timer_pending(&board->timer)) { 99 dev_err(board->gpib_dev, "bug! timer already running?\n"); 100 return; 101 } 102 clear_bit(TIMO_NUM, &board->status); 103 104 if (usec_timeout > 0) { 105 board->timer.function = watchdog_timeout; 106 /* set number of ticks */ 107 mod_timer(&board->timer, jiffies + usec_to_jiffies(usec_timeout)); 108 } 109 } 110 111 void os_remove_timer(struct gpib_board *board) 112 /* Removes the timeout task */ 113 { 114 if (timer_pending(&board->timer)) 115 timer_delete_sync(&board->timer); 116 } 117 118 int io_timed_out(struct gpib_board *board) 119 { 120 if (test_bit(TIMO_NUM, &board->status)) 121 return 1; 122 return 0; 123 } 124 125 /* 126 * this is a function instead of a constant because of Suse 127 * defining HZ to be a function call to get_hz() 128 */ 129 static inline int pseudo_irq_period(void) 130 { 131 return (HZ + 99) / 100; 132 } 133 134 static void pseudo_irq_handler(struct timer_list *t) 135 { 136 struct gpib_pseudo_irq *pseudo_irq = timer_container_of(pseudo_irq, t, 137 timer); 138 139 if (pseudo_irq->handler) 140 pseudo_irq->handler(0, pseudo_irq->board); 141 else 142 pr_err("gpib: bug! pseudo_irq.handler is NULL\n"); 143 144 if (atomic_read(&pseudo_irq->active)) 145 mod_timer(&pseudo_irq->timer, jiffies + pseudo_irq_period()); 146 } 147 148 int gpib_request_pseudo_irq(struct gpib_board *board, irqreturn_t (*handler)(int, void *)) 149 { 150 if (timer_pending(&board->pseudo_irq.timer) || board->pseudo_irq.handler) { 151 dev_err(board->gpib_dev, "only one pseudo interrupt per board allowed\n"); 152 return -1; 153 } 154 155 board->pseudo_irq.handler = handler; 156 board->pseudo_irq.timer.function = pseudo_irq_handler; 157 board->pseudo_irq.board = board; 158 159 atomic_set(&board->pseudo_irq.active, 1); 160 161 mod_timer(&board->pseudo_irq.timer, jiffies + pseudo_irq_period()); 162 163 return 0; 164 } 165 EXPORT_SYMBOL(gpib_request_pseudo_irq); 166 167 void gpib_free_pseudo_irq(struct gpib_board *board) 168 { 169 atomic_set(&board->pseudo_irq.active, 0); 170 171 timer_delete_sync(&board->pseudo_irq.timer); 172 board->pseudo_irq.handler = NULL; 173 } 174 EXPORT_SYMBOL(gpib_free_pseudo_irq); 175 176 static const unsigned int serial_timeout = 1000000; 177 178 unsigned int num_status_bytes(const struct gpib_status_queue *dev) 179 { 180 if (!dev) 181 return 0; 182 return dev->num_status_bytes; 183 } 184 185 // push status byte onto back of status byte fifo 186 int push_status_byte(struct gpib_board *board, struct gpib_status_queue *device, u8 poll_byte) 187 { 188 struct list_head *head = &device->status_bytes; 189 struct gpib_status_byte *status; 190 static const unsigned int max_num_status_bytes = 1024; 191 int retval; 192 193 if (num_status_bytes(device) >= max_num_status_bytes) { 194 u8 lost_byte; 195 196 device->dropped_byte = 1; 197 retval = pop_status_byte(board, device, &lost_byte); 198 if (retval < 0) 199 return retval; 200 } 201 202 status = kmalloc_obj(*status); 203 if (!status) 204 return -ENOMEM; 205 206 INIT_LIST_HEAD(&status->list); 207 status->poll_byte = poll_byte; 208 209 list_add_tail(&status->list, head); 210 211 device->num_status_bytes++; 212 213 dev_dbg(board->gpib_dev, "pushed status byte 0x%x, %i in queue\n", 214 (int)poll_byte, num_status_bytes(device)); 215 216 return 0; 217 } 218 219 // pop status byte from front of status byte fifo 220 int pop_status_byte(struct gpib_board *board, struct gpib_status_queue *device, u8 *poll_byte) 221 { 222 struct list_head *head = &device->status_bytes; 223 struct list_head *front = head->next; 224 struct gpib_status_byte *status; 225 226 if (num_status_bytes(device) == 0) 227 return -EIO; 228 229 if (front == head) 230 return -EIO; 231 232 if (device->dropped_byte) { 233 device->dropped_byte = 0; 234 return -EPIPE; 235 } 236 237 status = list_entry(front, struct gpib_status_byte, list); 238 *poll_byte = status->poll_byte; 239 240 list_del(front); 241 kfree(status); 242 243 device->num_status_bytes--; 244 245 dev_dbg(board->gpib_dev, "popped status byte 0x%x, %i in queue\n", 246 (int)*poll_byte, num_status_bytes(device)); 247 248 return 0; 249 } 250 251 struct gpib_status_queue *get_gpib_status_queue(struct gpib_board *board, unsigned int pad, int sad) 252 { 253 struct gpib_status_queue *device; 254 struct list_head *list_ptr; 255 const struct list_head *head = &board->device_list; 256 257 for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { 258 device = list_entry(list_ptr, struct gpib_status_queue, list); 259 if (gpib_address_equal(device->pad, device->sad, pad, sad)) 260 return device; 261 } 262 263 return NULL; 264 } 265 266 int get_serial_poll_byte(struct gpib_board *board, unsigned int pad, int sad, 267 unsigned int usec_timeout, u8 *poll_byte) 268 { 269 struct gpib_status_queue *device; 270 271 device = get_gpib_status_queue(board, pad, sad); 272 if (num_status_bytes(device)) 273 return pop_status_byte(board, device, poll_byte); 274 else 275 return dvrsp(board, pad, sad, usec_timeout, poll_byte); 276 } 277 278 int autopoll_all_devices(struct gpib_board *board) 279 { 280 int retval; 281 282 if (mutex_lock_interruptible(&board->user_mutex)) 283 return -ERESTARTSYS; 284 if (mutex_lock_interruptible(&board->big_gpib_mutex)) { 285 mutex_unlock(&board->user_mutex); 286 return -ERESTARTSYS; 287 } 288 289 dev_dbg(board->gpib_dev, "autopoll has board lock\n"); 290 291 retval = serial_poll_all(board, serial_timeout); 292 if (retval < 0) { 293 mutex_unlock(&board->big_gpib_mutex); 294 mutex_unlock(&board->user_mutex); 295 return retval; 296 } 297 298 dev_dbg(board->gpib_dev, "complete\n"); 299 /* 300 * need to wake wait queue in case someone is 301 * waiting on RQS 302 */ 303 wake_up_interruptible(&board->wait); 304 mutex_unlock(&board->big_gpib_mutex); 305 mutex_unlock(&board->user_mutex); 306 307 return retval; 308 } 309 310 static int setup_serial_poll(struct gpib_board *board, unsigned int usec_timeout) 311 { 312 u8 cmd_string[8]; 313 int i; 314 size_t bytes_written; 315 int ret; 316 317 os_start_timer(board, usec_timeout); 318 ret = ibcac(board, 1, 1); 319 if (ret < 0) { 320 os_remove_timer(board); 321 return ret; 322 } 323 324 i = 0; 325 cmd_string[i++] = UNL; 326 cmd_string[i++] = MLA(board->pad); /* controller's listen address */ 327 if (board->sad >= 0) 328 cmd_string[i++] = MSA(board->sad); 329 cmd_string[i++] = SPE; // serial poll enable 330 331 ret = board->interface->command(board, cmd_string, i, &bytes_written); 332 if (ret < 0 || bytes_written < i) { 333 dev_dbg(board->gpib_dev, "failed to setup serial poll\n"); 334 os_remove_timer(board); 335 return -EIO; 336 } 337 os_remove_timer(board); 338 339 return 0; 340 } 341 342 static int read_serial_poll_byte(struct gpib_board *board, unsigned int pad, 343 int sad, unsigned int usec_timeout, u8 *result) 344 { 345 u8 cmd_string[8]; 346 int end_flag; 347 int ret; 348 int i; 349 size_t nbytes; 350 351 dev_dbg(board->gpib_dev, "entering pad=%i sad=%i\n", pad, sad); 352 353 os_start_timer(board, usec_timeout); 354 ret = ibcac(board, 1, 1); 355 if (ret < 0) { 356 os_remove_timer(board); 357 return ret; 358 } 359 360 i = 0; 361 // send talk address 362 cmd_string[i++] = MTA(pad); 363 if (sad >= 0) 364 cmd_string[i++] = MSA(sad); 365 366 ret = board->interface->command(board, cmd_string, i, &nbytes); 367 if (ret < 0 || nbytes < i) { 368 dev_err(board->gpib_dev, "failed to setup serial poll\n"); 369 os_remove_timer(board); 370 return -EIO; 371 } 372 373 ibgts(board); 374 375 // read poll result 376 ret = board->interface->read(board, result, 1, &end_flag, &nbytes); 377 if (ret < 0 || nbytes < 1) { 378 dev_err(board->gpib_dev, "serial poll failed\n"); 379 os_remove_timer(board); 380 return -EIO; 381 } 382 os_remove_timer(board); 383 384 return 0; 385 } 386 387 static int cleanup_serial_poll(struct gpib_board *board, unsigned int usec_timeout) 388 { 389 u8 cmd_string[8]; 390 int ret; 391 size_t bytes_written; 392 393 os_start_timer(board, usec_timeout); 394 ret = ibcac(board, 1, 1); 395 if (ret < 0) { 396 os_remove_timer(board); 397 return ret; 398 } 399 400 cmd_string[0] = SPD; /* disable serial poll bytes */ 401 cmd_string[1] = UNT; 402 ret = board->interface->command(board, cmd_string, 2, &bytes_written); 403 if (ret < 0 || bytes_written < 2) { 404 dev_err(board->gpib_dev, "failed to disable serial poll\n"); 405 os_remove_timer(board); 406 return -EIO; 407 } 408 os_remove_timer(board); 409 410 return 0; 411 } 412 413 static int serial_poll_single(struct gpib_board *board, unsigned int pad, int sad, 414 unsigned int usec_timeout, u8 *result) 415 { 416 int retval, cleanup_retval; 417 418 retval = setup_serial_poll(board, usec_timeout); 419 if (retval < 0) 420 return retval; 421 retval = read_serial_poll_byte(board, pad, sad, usec_timeout, result); 422 cleanup_retval = cleanup_serial_poll(board, usec_timeout); 423 if (retval < 0) 424 return retval; 425 if (cleanup_retval < 0) 426 return retval; 427 428 return 0; 429 } 430 431 int serial_poll_all(struct gpib_board *board, unsigned int usec_timeout) 432 { 433 int retval = 0; 434 struct list_head *cur; 435 const struct list_head *head = NULL; 436 struct gpib_status_queue *device; 437 u8 result; 438 unsigned int num_bytes = 0; 439 440 head = &board->device_list; 441 if (head->next == head) 442 return 0; 443 444 retval = setup_serial_poll(board, usec_timeout); 445 if (retval < 0) 446 return retval; 447 448 for (cur = head->next; cur != head; cur = cur->next) { 449 device = list_entry(cur, struct gpib_status_queue, list); 450 retval = read_serial_poll_byte(board, 451 device->pad, device->sad, usec_timeout, &result); 452 if (retval < 0) 453 continue; 454 if (result & request_service_bit) { 455 retval = push_status_byte(board, device, result); 456 if (retval < 0) 457 continue; 458 num_bytes++; 459 } 460 } 461 462 retval = cleanup_serial_poll(board, usec_timeout); 463 if (retval < 0) 464 return retval; 465 466 return num_bytes; 467 } 468 469 /* 470 * DVRSP 471 * This function performs a serial poll of the device with primary 472 * address pad and secondary address sad. If the device has no 473 * secondary address, pass a negative number in for this argument. At the 474 * end of a successful serial poll the response is returned in result. 475 * SPD and UNT are sent at the completion of the poll. 476 */ 477 478 int dvrsp(struct gpib_board *board, unsigned int pad, int sad, 479 unsigned int usec_timeout, u8 *result) 480 { 481 int status = ibstatus(board); 482 int retval; 483 484 if ((status & CIC) == 0) { 485 dev_err(board->gpib_dev, "not CIC during serial poll\n"); 486 return -1; 487 } 488 489 if (pad > MAX_GPIB_PRIMARY_ADDRESS || sad > MAX_GPIB_SECONDARY_ADDRESS || sad < -1) { 490 dev_err(board->gpib_dev, "bad address for serial poll"); 491 return -1; 492 } 493 494 retval = serial_poll_single(board, pad, sad, usec_timeout, result); 495 if (io_timed_out(board)) 496 retval = -ETIMEDOUT; 497 498 return retval; 499 } 500 501 static struct gpib_descriptor *handle_to_descriptor(const struct gpib_file_private *file_priv, 502 int handle) 503 { 504 if (handle < 0 || handle >= GPIB_MAX_NUM_DESCRIPTORS) { 505 pr_err("gpib: invalid handle %i\n", handle); 506 return NULL; 507 } 508 509 return file_priv->descriptors[handle]; 510 } 511 512 static int init_gpib_file_private(struct gpib_file_private *priv) 513 { 514 memset(priv, 0, sizeof(*priv)); 515 atomic_set(&priv->holding_mutex, 0); 516 priv->descriptors[0] = kmalloc_obj(struct gpib_descriptor); 517 if (!priv->descriptors[0]) { 518 pr_err("gpib: failed to allocate default board descriptor\n"); 519 return -ENOMEM; 520 } 521 init_gpib_descriptor(priv->descriptors[0]); 522 priv->descriptors[0]->is_board = 1; 523 mutex_init(&priv->descriptors_mutex); 524 return 0; 525 } 526 527 int ibopen(struct inode *inode, struct file *filep) 528 { 529 unsigned int minor = iminor(inode); 530 struct gpib_board *board; 531 struct gpib_file_private *priv; 532 533 if (minor >= GPIB_MAX_NUM_BOARDS) { 534 pr_err("gpib: invalid minor number of device file\n"); 535 return -ENXIO; 536 } 537 538 board = &board_array[minor]; 539 540 filep->private_data = kmalloc_obj(struct gpib_file_private); 541 if (!filep->private_data) 542 return -ENOMEM; 543 544 priv = filep->private_data; 545 init_gpib_file_private((struct gpib_file_private *)filep->private_data); 546 547 if (board->interface) { 548 if (!try_module_get(board->provider_module)) { 549 dev_err(board->gpib_dev, "try_module_get() failed\n"); 550 return -EIO; 551 } 552 board->use_count++; 553 priv->got_module = 1; 554 } 555 return 0; 556 } 557 558 int ibclose(struct inode *inode, struct file *filep) 559 { 560 unsigned int minor = iminor(inode); 561 struct gpib_board *board; 562 struct gpib_file_private *priv = filep->private_data; 563 struct gpib_descriptor *desc; 564 565 if (minor >= GPIB_MAX_NUM_BOARDS) { 566 pr_err("gpib: invalid minor number of device file\n"); 567 return -ENODEV; 568 } 569 570 board = &board_array[minor]; 571 572 if (priv) { 573 desc = handle_to_descriptor(priv, 0); 574 if (desc) { 575 if (desc->autopoll_enabled) { 576 dev_dbg(board->gpib_dev, "decrementing autospollers\n"); 577 if (board->autospollers > 0) 578 board->autospollers--; 579 else 580 dev_err(board->gpib_dev, 581 "Attempt to decrement zero autospollers\n"); 582 } 583 } else { 584 dev_err(board->gpib_dev, "Unexpected null gpib_descriptor\n"); 585 } 586 587 cleanup_open_devices(priv, board); 588 589 if (atomic_read(&priv->holding_mutex)) 590 mutex_unlock(&board->user_mutex); 591 592 if (priv->got_module && board->use_count) { 593 module_put(board->provider_module); 594 --board->use_count; 595 } 596 597 kfree(filep->private_data); 598 filep->private_data = NULL; 599 } 600 601 return 0; 602 } 603 604 long ibioctl(struct file *filep, unsigned int cmd, unsigned long arg) 605 { 606 unsigned int minor = iminor(file_inode(filep)); 607 struct gpib_board *board; 608 struct gpib_file_private *file_priv = filep->private_data; 609 long retval = -EBADRQC; 610 611 if (minor >= GPIB_MAX_NUM_BOARDS) { 612 pr_err("gpib: invalid minor number of device file\n"); 613 return -ENODEV; 614 } 615 board = &board_array[minor]; 616 617 if (mutex_lock_interruptible(&board->big_gpib_mutex)) 618 return -ERESTARTSYS; 619 620 dev_dbg(board->gpib_dev, "ioctl %d, interface=%s, use=%d, onl=%d\n", 621 cmd & 0xff, 622 board->interface ? board->interface->name : "", 623 board->use_count, 624 board->online); 625 626 switch (cmd) { 627 case CFCBOARDTYPE: 628 retval = board_type_ioctl(file_priv, board, arg); 629 goto done; 630 case IBONL: 631 retval = online_ioctl(board, arg); 632 goto done; 633 default: 634 break; 635 } 636 if (!board->interface) { 637 dev_err(board->gpib_dev, "no gpib board configured\n"); 638 retval = -ENODEV; 639 goto done; 640 } 641 if (file_priv->got_module == 0) { 642 if (!try_module_get(board->provider_module)) { 643 dev_err(board->gpib_dev, "try_module_get() failed\n"); 644 retval = -EIO; 645 goto done; 646 } 647 file_priv->got_module = 1; 648 board->use_count++; 649 } 650 switch (cmd) { 651 case CFCBASE: 652 retval = iobase_ioctl(&board->config, arg); 653 goto done; 654 case CFCIRQ: 655 retval = irq_ioctl(&board->config, arg); 656 goto done; 657 case CFCDMA: 658 retval = dma_ioctl(&board->config, arg); 659 goto done; 660 case IBAUTOSPOLL: 661 retval = autospoll_ioctl(board, file_priv, arg); 662 goto done; 663 case IBBOARD_INFO: 664 retval = board_info_ioctl(board, arg); 665 goto done; 666 case IBMUTEX: 667 /* 668 * Need to unlock board->big_gpib_mutex before potentially locking board->user_mutex 669 * to maintain consistent locking order 670 */ 671 mutex_unlock(&board->big_gpib_mutex); 672 return mutex_ioctl(board, file_priv, arg); 673 case IBPAD: 674 retval = pad_ioctl(board, file_priv, arg); 675 goto done; 676 case IBSAD: 677 retval = sad_ioctl(board, file_priv, arg); 678 goto done; 679 case IBSELECT_PCI: 680 retval = select_pci_ioctl(&board->config, arg); 681 goto done; 682 case IBSELECT_DEVICE_PATH: 683 retval = select_device_path_ioctl(&board->config, arg); 684 goto done; 685 default: 686 break; 687 } 688 689 if (!board->online) { 690 retval = -EINVAL; 691 goto done; 692 } 693 694 switch (cmd) { 695 case IBEVENT: 696 retval = event_ioctl(board, arg); 697 goto done; 698 case IBCLOSEDEV: 699 retval = close_dev_ioctl(filep, board, arg); 700 goto done; 701 case IBOPENDEV: 702 retval = open_dev_ioctl(filep, board, arg); 703 goto done; 704 case IBSPOLL_BYTES: 705 retval = status_bytes_ioctl(board, arg); 706 goto done; 707 case IBWAIT: 708 retval = wait_ioctl(file_priv, board, arg); 709 if (retval == -ERESTARTSYS) 710 return retval; 711 goto done; 712 case IBLINES: 713 retval = line_status_ioctl(board, arg); 714 goto done; 715 case IBLOC: 716 board->interface->return_to_local(board); 717 retval = 0; 718 goto done; 719 default: 720 break; 721 } 722 723 spin_lock(&board->locking_pid_spinlock); 724 if (current->pid != board->locking_pid) { 725 spin_unlock(&board->locking_pid_spinlock); 726 retval = -EPERM; 727 goto done; 728 } 729 spin_unlock(&board->locking_pid_spinlock); 730 731 switch (cmd) { 732 case IB_T1_DELAY: 733 retval = t1_delay_ioctl(board, arg); 734 goto done; 735 case IBCAC: 736 retval = take_control_ioctl(board, arg); 737 goto done; 738 case IBCMD: 739 /* 740 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex 741 * before we call them. 742 */ 743 mutex_unlock(&board->big_gpib_mutex); 744 return command_ioctl(file_priv, board, arg); 745 case IBEOS: 746 retval = eos_ioctl(board, arg); 747 goto done; 748 case IBGTS: 749 retval = ibgts(board); 750 goto done; 751 case IBPPC: 752 retval = ppc_ioctl(board, arg); 753 goto done; 754 case IBPP2_SET: 755 retval = set_local_ppoll_mode_ioctl(board, arg); 756 goto done; 757 case IBPP2_GET: 758 retval = get_local_ppoll_mode_ioctl(board, arg); 759 goto done; 760 case IBQUERY_BOARD_RSV: 761 retval = query_board_rsv_ioctl(board, arg); 762 goto done; 763 case IBRD: 764 /* 765 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex 766 * before we call them. 767 */ 768 mutex_unlock(&board->big_gpib_mutex); 769 return read_ioctl(file_priv, board, arg); 770 case IBRPP: 771 retval = parallel_poll_ioctl(board, arg); 772 goto done; 773 case IBRSC: 774 retval = request_system_control_ioctl(board, arg); 775 goto done; 776 case IBRSP: 777 retval = serial_poll_ioctl(board, arg); 778 goto done; 779 case IBRSV: 780 retval = request_service_ioctl(board, arg); 781 goto done; 782 case IBRSV2: 783 retval = request_service2_ioctl(board, arg); 784 goto done; 785 case IBSIC: 786 retval = interface_clear_ioctl(board, arg); 787 goto done; 788 case IBSRE: 789 retval = remote_enable_ioctl(board, arg); 790 goto done; 791 case IBTMO: 792 retval = timeout_ioctl(board, arg); 793 goto done; 794 case IBWRT: 795 /* 796 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex 797 * before we call them. 798 */ 799 mutex_unlock(&board->big_gpib_mutex); 800 return write_ioctl(file_priv, board, arg); 801 default: 802 goto done; 803 } 804 805 done: 806 mutex_unlock(&board->big_gpib_mutex); 807 dev_dbg(board->gpib_dev, "ioctl done status = 0x%lx\n", board->status); 808 return retval; 809 } 810 811 static int board_type_ioctl(struct gpib_file_private *file_priv, 812 struct gpib_board *board, unsigned long arg) 813 { 814 struct list_head *list_ptr; 815 struct gpib_board_type_ioctl cmd; 816 int retval; 817 818 if (!capable(CAP_SYS_ADMIN)) 819 return -EPERM; 820 if (board->online) 821 return -EBUSY; 822 823 retval = copy_from_user(&cmd, (void __user *)arg, 824 sizeof(struct gpib_board_type_ioctl)); 825 if (retval) 826 return -EFAULT; 827 828 for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers; 829 list_ptr = list_ptr->next) { 830 struct gpib_interface_list *entry; 831 832 entry = list_entry(list_ptr, struct gpib_interface_list, list); 833 if (strcmp(entry->interface->name, cmd.name) == 0) { 834 int i; 835 int had_module = file_priv->got_module; 836 837 if (board->use_count) { 838 for (i = 0; i < board->use_count; ++i) 839 module_put(board->provider_module); 840 board->interface = NULL; 841 file_priv->got_module = 0; 842 } 843 board->interface = entry->interface; 844 board->provider_module = entry->module; 845 for (i = 0; i < board->use_count; ++i) { 846 if (!try_module_get(entry->module)) { 847 board->use_count = i; 848 return -EIO; 849 } 850 } 851 if (had_module == 0) { 852 if (!try_module_get(entry->module)) 853 return -EIO; 854 ++board->use_count; 855 } 856 file_priv->got_module = 1; 857 return 0; 858 } 859 } 860 861 return -EINVAL; 862 } 863 864 static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 865 unsigned long arg) 866 { 867 struct gpib_read_write_ioctl read_cmd; 868 u8 __user *userbuf; 869 unsigned long remain; 870 int end_flag = 0; 871 int retval; 872 ssize_t read_ret = 0; 873 struct gpib_descriptor *desc; 874 size_t nbytes; 875 876 retval = copy_from_user(&read_cmd, (void __user *)arg, sizeof(read_cmd)); 877 if (retval) 878 return -EFAULT; 879 880 if (read_cmd.completed_transfer_count > read_cmd.requested_transfer_count) 881 return -EINVAL; 882 883 if (WARN_ON_ONCE(sizeof(userbuf) > sizeof(read_cmd.buffer_ptr))) 884 return -EFAULT; 885 886 userbuf = (u8 __user *)(unsigned long)read_cmd.buffer_ptr; 887 userbuf += read_cmd.completed_transfer_count; 888 889 remain = read_cmd.requested_transfer_count - read_cmd.completed_transfer_count; 890 891 /* Check write access to buffer */ 892 if (!access_ok(userbuf, remain)) 893 return -EFAULT; 894 895 /* Lock descriptors to prevent concurrent close from freeing descriptor */ 896 if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) 897 return -ERESTARTSYS; 898 desc = handle_to_descriptor(file_priv, read_cmd.handle); 899 if (!desc) { 900 mutex_unlock(&file_priv->descriptors_mutex); 901 return -EINVAL; 902 } 903 atomic_inc(&desc->descriptor_busy); 904 mutex_unlock(&file_priv->descriptors_mutex); 905 906 atomic_set(&desc->io_in_progress, 1); 907 908 /* Read buffer loads till we fill the user supplied buffer */ 909 while (remain > 0 && end_flag == 0) { 910 nbytes = 0; 911 read_ret = ibrd(board, board->buffer, (board->buffer_length < remain) ? 912 board->buffer_length : remain, &end_flag, &nbytes); 913 if (nbytes == 0) 914 break; 915 retval = copy_to_user(userbuf, board->buffer, nbytes); 916 if (retval) { 917 retval = -EFAULT; 918 break; 919 } 920 remain -= nbytes; 921 userbuf += nbytes; 922 if (read_ret < 0) 923 break; 924 } 925 read_cmd.completed_transfer_count = read_cmd.requested_transfer_count - remain; 926 read_cmd.end = end_flag; 927 /* 928 * suppress errors (for example due to timeout or interruption by device clear) 929 * if all bytes got sent. This prevents races that can occur in the various drivers 930 * if a device receives a device clear immediately after a transfer completes and 931 * the driver code wasn't careful enough to handle that case. 932 */ 933 if (remain == 0 || end_flag) 934 read_ret = 0; 935 if (retval == 0) 936 retval = copy_to_user((void __user *)arg, &read_cmd, sizeof(read_cmd)); 937 938 atomic_set(&desc->io_in_progress, 0); 939 atomic_dec(&desc->descriptor_busy); 940 941 wake_up_interruptible(&board->wait); 942 if (retval) 943 return -EFAULT; 944 945 return read_ret; 946 } 947 948 static int command_ioctl(struct gpib_file_private *file_priv, 949 struct gpib_board *board, unsigned long arg) 950 { 951 struct gpib_read_write_ioctl cmd; 952 u8 __user *userbuf; 953 unsigned long remain; 954 int retval; 955 int fault = 0; 956 struct gpib_descriptor *desc; 957 size_t bytes_written; 958 int no_clear_io_in_prog; 959 960 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 961 if (retval) 962 return -EFAULT; 963 964 if (cmd.completed_transfer_count > cmd.requested_transfer_count) 965 return -EINVAL; 966 967 userbuf = (u8 __user *)(unsigned long)cmd.buffer_ptr; 968 userbuf += cmd.completed_transfer_count; 969 970 no_clear_io_in_prog = cmd.end; 971 cmd.end = 0; 972 973 remain = cmd.requested_transfer_count - cmd.completed_transfer_count; 974 975 /* Check read access to buffer */ 976 if (!access_ok(userbuf, remain)) 977 return -EFAULT; 978 979 /* Lock descriptors to prevent concurrent close from freeing descriptor */ 980 if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) 981 return -ERESTARTSYS; 982 desc = handle_to_descriptor(file_priv, cmd.handle); 983 if (!desc) { 984 mutex_unlock(&file_priv->descriptors_mutex); 985 return -EINVAL; 986 } 987 atomic_inc(&desc->descriptor_busy); 988 mutex_unlock(&file_priv->descriptors_mutex); 989 990 /* 991 * Write buffer loads till we empty the user supplied buffer. 992 * Call drivers at least once, even if remain is zero, in 993 * order to allow them to insure previous commands were 994 * completely finished, in the case of a restarted ioctl. 995 */ 996 997 atomic_set(&desc->io_in_progress, 1); 998 999 do { 1000 fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ? 1001 board->buffer_length : remain); 1002 if (fault) { 1003 retval = -EFAULT; 1004 bytes_written = 0; 1005 } else { 1006 retval = ibcmd(board, board->buffer, (board->buffer_length < remain) ? 1007 board->buffer_length : remain, &bytes_written); 1008 } 1009 remain -= bytes_written; 1010 userbuf += bytes_written; 1011 if (retval < 0) { 1012 atomic_set(&desc->io_in_progress, 0); 1013 atomic_dec(&desc->descriptor_busy); 1014 1015 wake_up_interruptible(&board->wait); 1016 break; 1017 } 1018 } while (remain > 0); 1019 1020 cmd.completed_transfer_count = cmd.requested_transfer_count - remain; 1021 1022 if (fault == 0) 1023 fault = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); 1024 1025 /* 1026 * no_clear_io_in_prog (cmd.end) is true when io_in_progress should 1027 * not be set to zero because the cmd in progress is the address setup 1028 * operation for an async read or write. This causes CMPL not to be set 1029 * in general_ibstatus until the async read or write completes. 1030 */ 1031 if (!no_clear_io_in_prog || fault) 1032 atomic_set(&desc->io_in_progress, 0); 1033 atomic_dec(&desc->descriptor_busy); 1034 1035 wake_up_interruptible(&board->wait); 1036 if (fault) 1037 return -EFAULT; 1038 1039 return retval; 1040 } 1041 1042 static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 1043 unsigned long arg) 1044 { 1045 struct gpib_read_write_ioctl write_cmd; 1046 u8 __user *userbuf; 1047 unsigned long remain; 1048 int retval = 0; 1049 int fault; 1050 struct gpib_descriptor *desc; 1051 1052 fault = copy_from_user(&write_cmd, (void __user *)arg, sizeof(write_cmd)); 1053 if (fault) 1054 return -EFAULT; 1055 1056 if (write_cmd.completed_transfer_count > write_cmd.requested_transfer_count) 1057 return -EINVAL; 1058 1059 userbuf = (u8 __user *)(unsigned long)write_cmd.buffer_ptr; 1060 userbuf += write_cmd.completed_transfer_count; 1061 1062 remain = write_cmd.requested_transfer_count - write_cmd.completed_transfer_count; 1063 1064 /* Check read access to buffer */ 1065 if (!access_ok(userbuf, remain)) 1066 return -EFAULT; 1067 1068 /* Lock descriptors to prevent concurrent close from freeing descriptor */ 1069 if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) 1070 return -ERESTARTSYS; 1071 desc = handle_to_descriptor(file_priv, write_cmd.handle); 1072 if (!desc) { 1073 mutex_unlock(&file_priv->descriptors_mutex); 1074 return -EINVAL; 1075 } 1076 atomic_inc(&desc->descriptor_busy); 1077 mutex_unlock(&file_priv->descriptors_mutex); 1078 1079 atomic_set(&desc->io_in_progress, 1); 1080 1081 /* Write buffer loads till we empty the user supplied buffer */ 1082 while (remain > 0) { 1083 int send_eoi; 1084 size_t bytes_written = 0; 1085 1086 send_eoi = remain <= board->buffer_length && write_cmd.end; 1087 fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ? 1088 board->buffer_length : remain); 1089 if (fault) { 1090 retval = -EFAULT; 1091 break; 1092 } 1093 retval = ibwrt(board, board->buffer, (board->buffer_length < remain) ? 1094 board->buffer_length : remain, send_eoi, &bytes_written); 1095 remain -= bytes_written; 1096 userbuf += bytes_written; 1097 if (retval < 0) 1098 break; 1099 } 1100 write_cmd.completed_transfer_count = write_cmd.requested_transfer_count - remain; 1101 /* 1102 * suppress errors (for example due to timeout or interruption by device clear) 1103 * if all bytes got sent. This prevents races that can occur in the various drivers 1104 * if a device receives a device clear immediately after a transfer completes and 1105 * the driver code wasn't careful enough to handle that case. 1106 */ 1107 if (remain == 0) 1108 retval = 0; 1109 if (fault == 0) 1110 fault = copy_to_user((void __user *)arg, &write_cmd, sizeof(write_cmd)); 1111 1112 atomic_set(&desc->io_in_progress, 0); 1113 atomic_dec(&desc->descriptor_busy); 1114 1115 wake_up_interruptible(&board->wait); 1116 if (fault) 1117 return -EFAULT; 1118 1119 return retval; 1120 } 1121 1122 static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg) 1123 { 1124 struct gpib_status_queue *device; 1125 struct gpib_spoll_bytes_ioctl cmd; 1126 int retval; 1127 1128 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1129 if (retval) 1130 return -EFAULT; 1131 1132 device = get_gpib_status_queue(board, cmd.pad, cmd.sad); 1133 if (!device) 1134 cmd.num_bytes = 0; 1135 else 1136 cmd.num_bytes = num_status_bytes(device); 1137 1138 retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); 1139 if (retval) 1140 return -EFAULT; 1141 1142 return 0; 1143 } 1144 1145 static int increment_open_device_count(struct gpib_board *board, struct list_head *head, 1146 unsigned int pad, int sad) 1147 { 1148 struct list_head *list_ptr; 1149 struct gpib_status_queue *device; 1150 1151 /* 1152 * first see if address has already been opened, then increment 1153 * open count 1154 */ 1155 for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { 1156 device = list_entry(list_ptr, struct gpib_status_queue, list); 1157 if (gpib_address_equal(device->pad, device->sad, pad, sad)) { 1158 dev_dbg(board->gpib_dev, "incrementing open count for pad %i, sad %i\n", 1159 device->pad, device->sad); 1160 device->reference_count++; 1161 return 0; 1162 } 1163 } 1164 1165 /* otherwise we need to allocate a new struct gpib_status_queue */ 1166 device = kmalloc_obj(struct gpib_status_queue, GFP_ATOMIC); 1167 if (!device) 1168 return -ENOMEM; 1169 init_gpib_status_queue(device); 1170 device->pad = pad; 1171 device->sad = sad; 1172 device->reference_count = 1; 1173 1174 list_add(&device->list, head); 1175 1176 dev_dbg(board->gpib_dev, "opened pad %i, sad %i\n", device->pad, device->sad); 1177 1178 return 0; 1179 } 1180 1181 static int subtract_open_device_count(struct gpib_board *board, struct list_head *head, 1182 unsigned int pad, int sad, unsigned int count) 1183 { 1184 struct gpib_status_queue *device; 1185 struct list_head *list_ptr; 1186 1187 for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { 1188 device = list_entry(list_ptr, struct gpib_status_queue, list); 1189 if (gpib_address_equal(device->pad, device->sad, pad, sad)) { 1190 dev_dbg(board->gpib_dev, "decrementing open count for pad %i, sad %i\n", 1191 device->pad, device->sad); 1192 if (count > device->reference_count) { 1193 dev_err(board->gpib_dev, "bug! in %s()\n", __func__); 1194 return -EINVAL; 1195 } 1196 device->reference_count -= count; 1197 if (device->reference_count == 0) { 1198 dev_dbg(board->gpib_dev, "closing pad %i, sad %i\n", 1199 device->pad, device->sad); 1200 list_del(list_ptr); 1201 kfree(device); 1202 } 1203 return 0; 1204 } 1205 } 1206 dev_err(board->gpib_dev, "bug! tried to close address that was never opened!\n"); 1207 return -EINVAL; 1208 } 1209 1210 static inline int decrement_open_device_count(struct gpib_board *board, struct list_head *head, 1211 unsigned int pad, int sad) 1212 { 1213 return subtract_open_device_count(board, head, pad, sad, 1); 1214 } 1215 1216 static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board) 1217 { 1218 int retval = 0; 1219 int i; 1220 1221 for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) { 1222 struct gpib_descriptor *desc; 1223 1224 desc = file_priv->descriptors[i]; 1225 if (!desc) 1226 continue; 1227 1228 if (desc->is_board == 0) { 1229 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1230 desc->sad); 1231 if (retval < 0) 1232 return retval; 1233 } 1234 kfree(desc); 1235 file_priv->descriptors[i] = NULL; 1236 } 1237 1238 return 0; 1239 } 1240 1241 static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg) 1242 { 1243 struct gpib_open_dev_ioctl open_dev_cmd; 1244 int retval; 1245 struct gpib_file_private *file_priv = filep->private_data; 1246 int i; 1247 1248 retval = copy_from_user(&open_dev_cmd, (void __user *)arg, sizeof(open_dev_cmd)); 1249 if (retval) 1250 return -EFAULT; 1251 1252 if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) 1253 return -ERESTARTSYS; 1254 for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) 1255 if (!file_priv->descriptors[i]) 1256 break; 1257 if (i == GPIB_MAX_NUM_DESCRIPTORS) { 1258 mutex_unlock(&file_priv->descriptors_mutex); 1259 return -ERANGE; 1260 } 1261 file_priv->descriptors[i] = kmalloc_obj(struct gpib_descriptor); 1262 if (!file_priv->descriptors[i]) { 1263 mutex_unlock(&file_priv->descriptors_mutex); 1264 return -ENOMEM; 1265 } 1266 init_gpib_descriptor(file_priv->descriptors[i]); 1267 1268 file_priv->descriptors[i]->pad = open_dev_cmd.pad; 1269 file_priv->descriptors[i]->sad = open_dev_cmd.sad; 1270 file_priv->descriptors[i]->is_board = open_dev_cmd.is_board; 1271 mutex_unlock(&file_priv->descriptors_mutex); 1272 1273 retval = increment_open_device_count(board, &board->device_list, open_dev_cmd.pad, 1274 open_dev_cmd.sad); 1275 if (retval < 0) 1276 return retval; 1277 1278 /* 1279 * clear stuck srq state, since we may be able to find service request on 1280 * the new device 1281 */ 1282 atomic_set(&board->stuck_srq, 0); 1283 1284 open_dev_cmd.handle = i; 1285 retval = copy_to_user((void __user *)arg, &open_dev_cmd, sizeof(open_dev_cmd)); 1286 if (retval) 1287 return -EFAULT; 1288 1289 return 0; 1290 } 1291 1292 static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg) 1293 { 1294 struct gpib_close_dev_ioctl cmd; 1295 struct gpib_file_private *file_priv = filep->private_data; 1296 struct gpib_descriptor *desc; 1297 unsigned int pad; 1298 int sad; 1299 int retval; 1300 1301 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1302 if (retval) 1303 return -EFAULT; 1304 1305 if (cmd.handle >= GPIB_MAX_NUM_DESCRIPTORS) 1306 return -EINVAL; 1307 1308 mutex_lock(&file_priv->descriptors_mutex); 1309 desc = file_priv->descriptors[cmd.handle]; 1310 if (!desc) { 1311 mutex_unlock(&file_priv->descriptors_mutex); 1312 return -EINVAL; 1313 } 1314 if (atomic_read(&desc->descriptor_busy)) { 1315 mutex_unlock(&file_priv->descriptors_mutex); 1316 return -EBUSY; 1317 } 1318 /* Remove from table while holding lock to prevent new IO from starting */ 1319 file_priv->descriptors[cmd.handle] = NULL; 1320 pad = desc->pad; 1321 sad = desc->sad; 1322 mutex_unlock(&file_priv->descriptors_mutex); 1323 1324 retval = decrement_open_device_count(board, &board->device_list, pad, sad); 1325 1326 kfree(desc); 1327 return retval; 1328 } 1329 1330 static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg) 1331 { 1332 struct gpib_serial_poll_ioctl serial_cmd; 1333 int retval; 1334 1335 retval = copy_from_user(&serial_cmd, (void __user *)arg, sizeof(serial_cmd)); 1336 if (retval) 1337 return -EFAULT; 1338 1339 retval = get_serial_poll_byte(board, serial_cmd.pad, serial_cmd.sad, board->usec_timeout, 1340 &serial_cmd.status_byte); 1341 if (retval < 0) 1342 return retval; 1343 1344 retval = copy_to_user((void __user *)arg, &serial_cmd, sizeof(serial_cmd)); 1345 if (retval) 1346 return -EFAULT; 1347 1348 return 0; 1349 } 1350 1351 static int wait_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 1352 unsigned long arg) 1353 { 1354 struct gpib_wait_ioctl wait_cmd; 1355 int retval; 1356 struct gpib_descriptor *desc; 1357 1358 retval = copy_from_user(&wait_cmd, (void __user *)arg, sizeof(wait_cmd)); 1359 if (retval) 1360 return -EFAULT; 1361 1362 /* 1363 * Lock descriptors to prevent concurrent close from freeing 1364 * descriptor. ibwait() releases big_gpib_mutex when wait_mask 1365 * is non-zero, so desc must be pinned with descriptor_busy. 1366 */ 1367 mutex_lock(&file_priv->descriptors_mutex); 1368 desc = handle_to_descriptor(file_priv, wait_cmd.handle); 1369 if (!desc) { 1370 mutex_unlock(&file_priv->descriptors_mutex); 1371 return -EINVAL; 1372 } 1373 atomic_inc(&desc->descriptor_busy); 1374 mutex_unlock(&file_priv->descriptors_mutex); 1375 1376 retval = ibwait(board, wait_cmd.wait_mask, wait_cmd.clear_mask, 1377 wait_cmd.set_mask, &wait_cmd.ibsta, wait_cmd.usec_timeout, desc); 1378 1379 atomic_dec(&desc->descriptor_busy); 1380 1381 if (retval < 0) 1382 return retval; 1383 1384 retval = copy_to_user((void __user *)arg, &wait_cmd, sizeof(wait_cmd)); 1385 if (retval) 1386 return -EFAULT; 1387 1388 return 0; 1389 } 1390 1391 static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg) 1392 { 1393 u8 poll_byte; 1394 int retval; 1395 1396 retval = ibrpp(board, &poll_byte); 1397 if (retval < 0) 1398 return retval; 1399 1400 retval = copy_to_user((void __user *)arg, &poll_byte, sizeof(poll_byte)); 1401 if (retval) 1402 return -EFAULT; 1403 1404 return 0; 1405 } 1406 1407 static int online_ioctl(struct gpib_board *board, unsigned long arg) 1408 { 1409 struct gpib_online_ioctl online_cmd; 1410 int retval; 1411 void __user *init_data = NULL; 1412 1413 board->config.init_data = NULL; 1414 1415 if (!capable(CAP_SYS_ADMIN)) 1416 return -EPERM; 1417 1418 retval = copy_from_user(&online_cmd, (void __user *)arg, sizeof(online_cmd)); 1419 if (retval) 1420 return -EFAULT; 1421 if (online_cmd.init_data_length > 0) { 1422 board->config.init_data = vmalloc(online_cmd.init_data_length); 1423 if (!board->config.init_data) 1424 return -ENOMEM; 1425 if (WARN_ON_ONCE(sizeof(init_data) > sizeof(online_cmd.init_data_ptr))) 1426 return -EFAULT; 1427 init_data = (void __user *)(unsigned long)(online_cmd.init_data_ptr); 1428 retval = copy_from_user(board->config.init_data, init_data, 1429 online_cmd.init_data_length); 1430 if (retval) { 1431 vfree(board->config.init_data); 1432 return -EFAULT; 1433 } 1434 board->config.init_data_length = online_cmd.init_data_length; 1435 } else { 1436 board->config.init_data = NULL; 1437 board->config.init_data_length = 0; 1438 } 1439 if (online_cmd.online) 1440 retval = ibonline(board); 1441 else 1442 retval = iboffline(board); 1443 if (board->config.init_data) { 1444 vfree(board->config.init_data); 1445 board->config.init_data = NULL; 1446 board->config.init_data_length = 0; 1447 } 1448 return retval; 1449 } 1450 1451 static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg) 1452 { 1453 int enable; 1454 int retval; 1455 1456 retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); 1457 if (retval) 1458 return -EFAULT; 1459 1460 return ibsre(board, enable); 1461 } 1462 1463 static int take_control_ioctl(struct gpib_board *board, unsigned long arg) 1464 { 1465 int synchronous; 1466 int retval; 1467 1468 retval = copy_from_user(&synchronous, (void __user *)arg, sizeof(synchronous)); 1469 if (retval) 1470 return -EFAULT; 1471 1472 return ibcac(board, synchronous, 1); 1473 } 1474 1475 static int line_status_ioctl(struct gpib_board *board, unsigned long arg) 1476 { 1477 short lines; 1478 int retval; 1479 1480 retval = iblines(board, &lines); 1481 if (retval < 0) 1482 return retval; 1483 1484 retval = copy_to_user((void __user *)arg, &lines, sizeof(lines)); 1485 if (retval) 1486 return -EFAULT; 1487 1488 return 0; 1489 } 1490 1491 static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1492 unsigned long arg) 1493 { 1494 struct gpib_pad_ioctl cmd; 1495 int retval; 1496 struct gpib_descriptor *desc; 1497 1498 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1499 if (retval) 1500 return -EFAULT; 1501 1502 desc = handle_to_descriptor(file_priv, cmd.handle); 1503 if (!desc) 1504 return -EINVAL; 1505 1506 if (desc->is_board) { 1507 retval = ibpad(board, cmd.pad); 1508 if (retval < 0) 1509 return retval; 1510 } else { 1511 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1512 desc->sad); 1513 if (retval < 0) 1514 return retval; 1515 1516 desc->pad = cmd.pad; 1517 1518 retval = increment_open_device_count(board, &board->device_list, desc->pad, 1519 desc->sad); 1520 if (retval < 0) 1521 return retval; 1522 } 1523 1524 return 0; 1525 } 1526 1527 static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1528 unsigned long arg) 1529 { 1530 struct gpib_sad_ioctl cmd; 1531 int retval; 1532 struct gpib_descriptor *desc; 1533 1534 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1535 if (retval) 1536 return -EFAULT; 1537 1538 desc = handle_to_descriptor(file_priv, cmd.handle); 1539 if (!desc) 1540 return -EINVAL; 1541 1542 if (desc->is_board) { 1543 retval = ibsad(board, cmd.sad); 1544 if (retval < 0) 1545 return retval; 1546 } else { 1547 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1548 desc->sad); 1549 if (retval < 0) 1550 return retval; 1551 1552 desc->sad = cmd.sad; 1553 1554 retval = increment_open_device_count(board, &board->device_list, desc->pad, 1555 desc->sad); 1556 if (retval < 0) 1557 return retval; 1558 } 1559 return 0; 1560 } 1561 1562 static int eos_ioctl(struct gpib_board *board, unsigned long arg) 1563 { 1564 struct gpib_eos_ioctl eos_cmd; 1565 int retval; 1566 1567 retval = copy_from_user(&eos_cmd, (void __user *)arg, sizeof(eos_cmd)); 1568 if (retval) 1569 return -EFAULT; 1570 1571 return ibeos(board, eos_cmd.eos, eos_cmd.eos_flags); 1572 } 1573 1574 static int request_service_ioctl(struct gpib_board *board, unsigned long arg) 1575 { 1576 u8 status_byte; 1577 int retval; 1578 1579 retval = copy_from_user(&status_byte, (void __user *)arg, sizeof(status_byte)); 1580 if (retval) 1581 return -EFAULT; 1582 1583 return ibrsv2(board, status_byte, status_byte & request_service_bit); 1584 } 1585 1586 static int request_service2_ioctl(struct gpib_board *board, unsigned long arg) 1587 { 1588 struct gpib_request_service2 request_service2_cmd; 1589 int retval; 1590 1591 retval = copy_from_user(&request_service2_cmd, (void __user *)arg, 1592 sizeof(struct gpib_request_service2)); 1593 if (retval) 1594 return -EFAULT; 1595 1596 return ibrsv2(board, request_service2_cmd.status_byte, 1597 request_service2_cmd.new_reason_for_service); 1598 } 1599 1600 static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg) 1601 { 1602 u64 base_addr; 1603 int retval; 1604 1605 if (!capable(CAP_SYS_ADMIN)) 1606 return -EPERM; 1607 1608 retval = copy_from_user(&base_addr, (void __user *)arg, sizeof(base_addr)); 1609 if (retval) 1610 return -EFAULT; 1611 1612 if (WARN_ON_ONCE(sizeof(void *) > sizeof(base_addr))) 1613 return -EFAULT; 1614 config->ibbase = base_addr; 1615 1616 return 0; 1617 } 1618 1619 static int irq_ioctl(struct gpib_board_config *config, unsigned long arg) 1620 { 1621 unsigned int irq; 1622 int retval; 1623 1624 if (!capable(CAP_SYS_ADMIN)) 1625 return -EPERM; 1626 1627 retval = copy_from_user(&irq, (void __user *)arg, sizeof(irq)); 1628 if (retval) 1629 return -EFAULT; 1630 1631 config->ibirq = irq; 1632 1633 return 0; 1634 } 1635 1636 static int dma_ioctl(struct gpib_board_config *config, unsigned long arg) 1637 { 1638 unsigned int dma_channel; 1639 int retval; 1640 1641 if (!capable(CAP_SYS_ADMIN)) 1642 return -EPERM; 1643 1644 retval = copy_from_user(&dma_channel, (void __user *)arg, sizeof(dma_channel)); 1645 if (retval) 1646 return -EFAULT; 1647 1648 config->ibdma = dma_channel; 1649 1650 return 0; 1651 } 1652 1653 static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1654 unsigned long arg) 1655 { 1656 short enable; 1657 int retval; 1658 struct gpib_descriptor *desc; 1659 1660 retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); 1661 if (retval) 1662 return -EFAULT; 1663 1664 desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */ 1665 1666 if (enable) { 1667 if (!desc->autopoll_enabled) { 1668 board->autospollers++; 1669 desc->autopoll_enabled = 1; 1670 } 1671 retval = 0; 1672 } else { 1673 if (desc->autopoll_enabled) { 1674 desc->autopoll_enabled = 0; 1675 if (board->autospollers > 0) { 1676 board->autospollers--; 1677 retval = 0; 1678 } else { 1679 dev_err(board->gpib_dev, 1680 "tried to set number of autospollers negative\n"); 1681 retval = -EINVAL; 1682 } 1683 } else { 1684 dev_err(board->gpib_dev, "autopoll disable requested before enable\n"); 1685 retval = -EINVAL; 1686 } 1687 } 1688 return retval; 1689 } 1690 1691 static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1692 unsigned long arg) 1693 { 1694 int retval, lock_mutex; 1695 1696 retval = copy_from_user(&lock_mutex, (void __user *)arg, sizeof(lock_mutex)); 1697 if (retval) 1698 return -EFAULT; 1699 1700 if (lock_mutex) { 1701 retval = mutex_lock_interruptible(&board->user_mutex); 1702 if (retval) 1703 return -ERESTARTSYS; 1704 1705 spin_lock(&board->locking_pid_spinlock); 1706 board->locking_pid = current->pid; 1707 spin_unlock(&board->locking_pid_spinlock); 1708 1709 atomic_set(&file_priv->holding_mutex, 1); 1710 1711 dev_dbg(board->gpib_dev, "locked board mutex\n"); 1712 } else { 1713 spin_lock(&board->locking_pid_spinlock); 1714 if (current->pid != board->locking_pid) { 1715 dev_err(board->gpib_dev, "bug! pid %i tried to release mutex held by pid %i\n", 1716 current->pid, board->locking_pid); 1717 spin_unlock(&board->locking_pid_spinlock); 1718 return -EPERM; 1719 } 1720 board->locking_pid = 0; 1721 spin_unlock(&board->locking_pid_spinlock); 1722 1723 atomic_set(&file_priv->holding_mutex, 0); 1724 1725 mutex_unlock(&board->user_mutex); 1726 dev_dbg(board->gpib_dev, "unlocked board mutex\n"); 1727 } 1728 return 0; 1729 } 1730 1731 static int timeout_ioctl(struct gpib_board *board, unsigned long arg) 1732 { 1733 unsigned int timeout; 1734 int retval; 1735 1736 retval = copy_from_user(&timeout, (void __user *)arg, sizeof(timeout)); 1737 if (retval) 1738 return -EFAULT; 1739 1740 board->usec_timeout = timeout; 1741 dev_dbg(board->gpib_dev, "timeout set to %i usec\n", timeout); 1742 1743 return 0; 1744 } 1745 1746 static int ppc_ioctl(struct gpib_board *board, unsigned long arg) 1747 { 1748 struct gpib_ppoll_config_ioctl cmd; 1749 int retval; 1750 1751 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1752 if (retval) 1753 return -EFAULT; 1754 1755 if (cmd.set_ist) { 1756 board->ist = 1; 1757 board->interface->parallel_poll_response(board, board->ist); 1758 } else if (cmd.clear_ist) { 1759 board->ist = 0; 1760 board->interface->parallel_poll_response(board, board->ist); 1761 } 1762 1763 if (cmd.config) { 1764 retval = ibppc(board, cmd.config); 1765 if (retval < 0) 1766 return retval; 1767 } 1768 1769 return 0; 1770 } 1771 1772 static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) 1773 { 1774 short cmd; 1775 int retval; 1776 1777 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1778 if (retval) 1779 return -EFAULT; 1780 1781 if (!board->interface->local_parallel_poll_mode) 1782 return -ENOENT; 1783 board->local_ppoll_mode = cmd != 0; 1784 board->interface->local_parallel_poll_mode(board, board->local_ppoll_mode); 1785 1786 return 0; 1787 } 1788 1789 static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) 1790 { 1791 short cmd; 1792 int retval; 1793 1794 cmd = board->local_ppoll_mode; 1795 retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); 1796 if (retval) 1797 return -EFAULT; 1798 1799 return 0; 1800 } 1801 1802 static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg) 1803 { 1804 int status; 1805 int retval; 1806 1807 status = board->interface->serial_poll_status(board); 1808 1809 retval = copy_to_user((void __user *)arg, &status, sizeof(status)); 1810 if (retval) 1811 return -EFAULT; 1812 1813 return 0; 1814 } 1815 1816 static int board_info_ioctl(const struct gpib_board *board, unsigned long arg) 1817 { 1818 struct gpib_board_info_ioctl info = { }; 1819 int retval; 1820 1821 info.pad = board->pad; 1822 info.sad = board->sad; 1823 info.parallel_poll_configuration = board->parallel_poll_configuration; 1824 info.is_system_controller = board->master; 1825 if (board->autospollers) 1826 info.autopolling = 1; 1827 else 1828 info.autopolling = 0; 1829 info.t1_delay = board->t1_nano_sec; 1830 info.ist = board->ist; 1831 info.no_7_bit_eos = board->interface->no_7_bit_eos; 1832 retval = copy_to_user((void __user *)arg, &info, sizeof(info)); 1833 if (retval) 1834 return -EFAULT; 1835 1836 return 0; 1837 } 1838 1839 static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg) 1840 { 1841 unsigned int usec_duration; 1842 int retval; 1843 1844 retval = copy_from_user(&usec_duration, (void __user *)arg, sizeof(usec_duration)); 1845 if (retval) 1846 return -EFAULT; 1847 1848 return ibsic(board, usec_duration); 1849 } 1850 1851 static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg) 1852 { 1853 struct gpib_select_pci_ioctl selection; 1854 int retval; 1855 1856 if (!capable(CAP_SYS_ADMIN)) 1857 return -EPERM; 1858 1859 retval = copy_from_user(&selection, (void __user *)arg, sizeof(selection)); 1860 if (retval) 1861 return -EFAULT; 1862 1863 config->pci_bus = selection.pci_bus; 1864 config->pci_slot = selection.pci_slot; 1865 1866 return 0; 1867 } 1868 1869 static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg) 1870 { 1871 struct gpib_select_device_path_ioctl *selection; 1872 int retval; 1873 1874 if (!capable(CAP_SYS_ADMIN)) 1875 return -EPERM; 1876 1877 selection = vmalloc(sizeof(struct gpib_select_device_path_ioctl)); 1878 if (!selection) 1879 return -ENOMEM; 1880 1881 retval = copy_from_user(selection, (void __user *)arg, 1882 sizeof(struct gpib_select_device_path_ioctl)); 1883 if (retval) { 1884 vfree(selection); 1885 return -EFAULT; 1886 } 1887 1888 selection->device_path[sizeof(selection->device_path) - 1] = '\0'; 1889 kfree(config->device_path); 1890 config->device_path = NULL; 1891 if (strlen(selection->device_path) > 0) 1892 config->device_path = kstrdup(selection->device_path, GFP_KERNEL); 1893 1894 vfree(selection); 1895 return 0; 1896 } 1897 1898 unsigned int num_gpib_events(const struct gpib_event_queue *queue) 1899 { 1900 return queue->num_events; 1901 } 1902 1903 static int push_gpib_event_nolock(struct gpib_board *board, short event_type) 1904 { 1905 struct gpib_event_queue *queue = &board->event_queue; 1906 struct list_head *head = &queue->event_head; 1907 struct gpib_event *event; 1908 static const unsigned int max_num_events = 1024; 1909 int retval; 1910 1911 if (num_gpib_events(queue) >= max_num_events) { 1912 short lost_event; 1913 1914 queue->dropped_event = 1; 1915 retval = pop_gpib_event_nolock(board, queue, &lost_event); 1916 if (retval < 0) 1917 return retval; 1918 } 1919 1920 event = kmalloc_obj(struct gpib_event, GFP_ATOMIC); 1921 if (!event) { 1922 queue->dropped_event = 1; 1923 dev_err(board->gpib_dev, "failed to allocate memory for event\n"); 1924 return -ENOMEM; 1925 } 1926 1927 INIT_LIST_HEAD(&event->list); 1928 event->event_type = event_type; 1929 1930 list_add_tail(&event->list, head); 1931 1932 queue->num_events++; 1933 1934 dev_dbg(board->gpib_dev, "pushed event %i, %i in queue\n", 1935 (int)event_type, num_gpib_events(queue)); 1936 1937 return 0; 1938 } 1939 1940 // push event onto back of event queue 1941 int push_gpib_event(struct gpib_board *board, short event_type) 1942 { 1943 unsigned long flags; 1944 int retval; 1945 1946 spin_lock_irqsave(&board->event_queue.lock, flags); 1947 retval = push_gpib_event_nolock(board, event_type); 1948 spin_unlock_irqrestore(&board->event_queue.lock, flags); 1949 1950 if (event_type == EVENT_DEV_TRG) 1951 board->status |= DTAS; 1952 if (event_type == EVENT_DEV_CLR) 1953 board->status |= DCAS; 1954 1955 return retval; 1956 } 1957 EXPORT_SYMBOL(push_gpib_event); 1958 1959 static int pop_gpib_event_nolock(struct gpib_board *board, 1960 struct gpib_event_queue *queue, short *event_type) 1961 { 1962 struct list_head *head = &queue->event_head; 1963 struct list_head *front = head->next; 1964 struct gpib_event *event; 1965 1966 if (num_gpib_events(queue) == 0) { 1967 *event_type = EVENT_NONE; 1968 return 0; 1969 } 1970 1971 if (front == head) 1972 return -EIO; 1973 1974 if (queue->dropped_event) { 1975 queue->dropped_event = 0; 1976 return -EPIPE; 1977 } 1978 1979 event = list_entry(front, struct gpib_event, list); 1980 *event_type = event->event_type; 1981 1982 list_del(front); 1983 kfree(event); 1984 1985 queue->num_events--; 1986 1987 dev_dbg(board->gpib_dev, "popped event %i, %i in queue\n", 1988 (int)*event_type, num_gpib_events(queue)); 1989 1990 return 0; 1991 } 1992 1993 // pop event from front of event queue 1994 int pop_gpib_event(struct gpib_board *board, struct gpib_event_queue *queue, short *event_type) 1995 { 1996 unsigned long flags; 1997 int retval; 1998 1999 spin_lock_irqsave(&queue->lock, flags); 2000 retval = pop_gpib_event_nolock(board, queue, event_type); 2001 spin_unlock_irqrestore(&queue->lock, flags); 2002 return retval; 2003 } 2004 2005 static int event_ioctl(struct gpib_board *board, unsigned long arg) 2006 { 2007 short user_event; 2008 int retval; 2009 short event; 2010 2011 retval = pop_gpib_event(board, &board->event_queue, &event); 2012 if (retval < 0) 2013 return retval; 2014 2015 user_event = event; 2016 2017 retval = copy_to_user((void __user *)arg, &user_event, sizeof(user_event)); 2018 if (retval) 2019 return -EFAULT; 2020 2021 return 0; 2022 } 2023 2024 static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg) 2025 { 2026 int request_control; 2027 int retval; 2028 2029 retval = copy_from_user(&request_control, (void __user *)arg, sizeof(request_control)); 2030 if (retval) 2031 return -EFAULT; 2032 2033 return ibrsc(board, request_control); 2034 } 2035 2036 static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg) 2037 { 2038 unsigned int cmd; 2039 unsigned int delay; 2040 int retval; 2041 2042 if (!board->interface->t1_delay) 2043 return -ENOENT; 2044 2045 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 2046 if (retval) 2047 return -EFAULT; 2048 2049 delay = cmd; 2050 2051 retval = board->interface->t1_delay(board, delay); 2052 if (retval < 0) 2053 return retval; 2054 2055 board->t1_nano_sec = retval; 2056 return 0; 2057 } 2058 2059 static const struct file_operations ib_fops = { 2060 .owner = THIS_MODULE, 2061 .llseek = NULL, 2062 .unlocked_ioctl = &ibioctl, 2063 .compat_ioctl = &ibioctl, 2064 .open = &ibopen, 2065 .release = &ibclose, 2066 }; 2067 2068 struct gpib_board board_array[GPIB_MAX_NUM_BOARDS]; 2069 2070 LIST_HEAD(registered_drivers); 2071 2072 void init_gpib_descriptor(struct gpib_descriptor *desc) 2073 { 2074 desc->pad = 0; 2075 desc->sad = -1; 2076 desc->is_board = 0; 2077 desc->autopoll_enabled = 0; 2078 atomic_set(&desc->io_in_progress, 0); 2079 atomic_set(&desc->descriptor_busy, 0); 2080 } 2081 2082 int gpib_register_driver(struct gpib_interface *interface, struct module *provider_module) 2083 { 2084 struct gpib_interface_list *entry; 2085 2086 entry = kmalloc_obj(*entry); 2087 if (!entry) 2088 return -ENOMEM; 2089 2090 entry->interface = interface; 2091 entry->module = provider_module; 2092 list_add(&entry->list, ®istered_drivers); 2093 2094 return 0; 2095 } 2096 EXPORT_SYMBOL(gpib_register_driver); 2097 2098 void gpib_unregister_driver(struct gpib_interface *interface) 2099 { 2100 int i; 2101 struct list_head *list_ptr; 2102 2103 for (i = 0; i < GPIB_MAX_NUM_BOARDS; i++) { 2104 struct gpib_board *board = &board_array[i]; 2105 2106 if (board->interface == interface) { 2107 if (board->use_count > 0) 2108 pr_warn("gpib: Warning: deregistered interface %s in use\n", 2109 interface->name); 2110 iboffline(board); 2111 board->interface = NULL; 2112 } 2113 } 2114 for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers;) { 2115 struct gpib_interface_list *entry; 2116 2117 entry = list_entry(list_ptr, struct gpib_interface_list, list); 2118 list_ptr = list_ptr->next; 2119 if (entry->interface == interface) { 2120 list_del(&entry->list); 2121 kfree(entry); 2122 } 2123 } 2124 } 2125 EXPORT_SYMBOL(gpib_unregister_driver); 2126 2127 static void init_gpib_board_config(struct gpib_board_config *config) 2128 { 2129 memset(config, 0, sizeof(struct gpib_board_config)); 2130 config->pci_bus = -1; 2131 config->pci_slot = -1; 2132 } 2133 2134 void init_gpib_board(struct gpib_board *board) 2135 { 2136 board->interface = NULL; 2137 board->provider_module = NULL; 2138 board->buffer = NULL; 2139 board->buffer_length = 0; 2140 board->status = 0; 2141 init_waitqueue_head(&board->wait); 2142 mutex_init(&board->user_mutex); 2143 mutex_init(&board->big_gpib_mutex); 2144 board->locking_pid = 0; 2145 spin_lock_init(&board->locking_pid_spinlock); 2146 spin_lock_init(&board->spinlock); 2147 timer_setup(&board->timer, NULL, 0); 2148 board->dev = NULL; 2149 board->gpib_dev = NULL; 2150 init_gpib_board_config(&board->config); 2151 board->private_data = NULL; 2152 board->use_count = 0; 2153 INIT_LIST_HEAD(&board->device_list); 2154 board->pad = 0; 2155 board->sad = -1; 2156 board->usec_timeout = 3000000; 2157 board->parallel_poll_configuration = 0; 2158 board->online = 0; 2159 board->autospollers = 0; 2160 board->autospoll_task = NULL; 2161 init_event_queue(&board->event_queue); 2162 board->minor = -1; 2163 init_gpib_pseudo_irq(&board->pseudo_irq); 2164 board->master = 1; 2165 atomic_set(&board->stuck_srq, 0); 2166 board->local_ppoll_mode = 0; 2167 } 2168 2169 int gpib_allocate_board(struct gpib_board *board) 2170 { 2171 if (!board->buffer) { 2172 board->buffer_length = 0x4000; 2173 board->buffer = vmalloc(board->buffer_length); 2174 if (!board->buffer) { 2175 board->buffer_length = 0; 2176 return -ENOMEM; 2177 } 2178 } 2179 return 0; 2180 } 2181 2182 void gpib_deallocate_board(struct gpib_board *board) 2183 { 2184 short dummy; 2185 2186 if (board->buffer) { 2187 vfree(board->buffer); 2188 board->buffer = NULL; 2189 board->buffer_length = 0; 2190 } 2191 while (num_gpib_events(&board->event_queue)) 2192 pop_gpib_event(board, &board->event_queue, &dummy); 2193 } 2194 2195 static void init_board_array(struct gpib_board *board_array, unsigned int length) 2196 { 2197 int i; 2198 2199 for (i = 0; i < length; i++) { 2200 init_gpib_board(&board_array[i]); 2201 board_array[i].minor = i; 2202 } 2203 } 2204 2205 void init_gpib_status_queue(struct gpib_status_queue *device) 2206 { 2207 INIT_LIST_HEAD(&device->list); 2208 INIT_LIST_HEAD(&device->status_bytes); 2209 device->num_status_bytes = 0; 2210 device->reference_count = 0; 2211 device->dropped_byte = 0; 2212 } 2213 2214 static const struct class gpib_class = { 2215 .name = "gpib_common", 2216 }; 2217 2218 static int __init gpib_common_init_module(void) 2219 { 2220 int err; 2221 int i; 2222 2223 pr_info("GPIB core driver\n"); 2224 init_board_array(board_array, GPIB_MAX_NUM_BOARDS); 2225 if (register_chrdev(GPIB_CODE, "gpib", &ib_fops)) { 2226 pr_err("gpib: can't get major %d\n", GPIB_CODE); 2227 return -EIO; 2228 } 2229 err = class_register(&gpib_class); 2230 if (err) { 2231 pr_err("gpib: failed to create gpib class\n"); 2232 unregister_chrdev(GPIB_CODE, "gpib"); 2233 return err; 2234 } 2235 for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) 2236 board_array[i].gpib_dev = device_create(&gpib_class, NULL, 2237 MKDEV(GPIB_CODE, i), NULL, "gpib%i", i); 2238 2239 return 0; 2240 } 2241 2242 static void __exit gpib_common_exit_module(void) 2243 { 2244 int i; 2245 2246 for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) 2247 device_destroy(&gpib_class, MKDEV(GPIB_CODE, i)); 2248 2249 class_unregister(&gpib_class); 2250 unregister_chrdev(GPIB_CODE, "gpib"); 2251 } 2252 2253 int gpib_match_device_path(struct device *dev, const char *device_path_in) 2254 { 2255 if (device_path_in) { 2256 char *device_path; 2257 2258 device_path = kobject_get_path(&dev->kobj, GFP_KERNEL); 2259 if (!device_path) { 2260 dev_err(dev, "kobject_get_path returned NULL."); 2261 return 0; 2262 } 2263 if (strcmp(device_path_in, device_path) != 0) { 2264 kfree(device_path); 2265 return 0; 2266 } 2267 kfree(device_path); 2268 } 2269 return 1; 2270 } 2271 EXPORT_SYMBOL(gpib_match_device_path); 2272 2273 struct pci_dev *gpib_pci_get_device(const struct gpib_board_config *config, unsigned int vendor_id, 2274 unsigned int device_id, struct pci_dev *from) 2275 { 2276 struct pci_dev *pci_device = from; 2277 2278 while ((pci_device = pci_get_device(vendor_id, device_id, pci_device))) { 2279 if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) 2280 continue; 2281 if (config->pci_slot >= 0 && config->pci_slot != 2282 PCI_SLOT(pci_device->devfn)) 2283 continue; 2284 if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) 2285 continue; 2286 return pci_device; 2287 } 2288 return NULL; 2289 } 2290 EXPORT_SYMBOL(gpib_pci_get_device); 2291 2292 struct pci_dev *gpib_pci_get_subsys(const struct gpib_board_config *config, unsigned int vendor_id, 2293 unsigned int device_id, unsigned int ss_vendor, 2294 unsigned int ss_device, 2295 struct pci_dev *from) 2296 { 2297 struct pci_dev *pci_device = from; 2298 2299 while ((pci_device = pci_get_subsys(vendor_id, device_id, 2300 ss_vendor, ss_device, pci_device))) { 2301 if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) 2302 continue; 2303 if (config->pci_slot >= 0 && config->pci_slot != 2304 PCI_SLOT(pci_device->devfn)) 2305 continue; 2306 if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) 2307 continue; 2308 return pci_device; 2309 } 2310 return NULL; 2311 } 2312 EXPORT_SYMBOL(gpib_pci_get_subsys); 2313 2314 module_init(gpib_common_init_module); 2315 module_exit(gpib_common_exit_module); 2316 2317