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->use_count == 0) { 548 int retval; 549 550 retval = request_module("gpib%i", minor); 551 if (retval) 552 dev_dbg(board->gpib_dev, "request module returned %i\n", retval); 553 } 554 if (board->interface) { 555 if (!try_module_get(board->provider_module)) { 556 dev_err(board->gpib_dev, "try_module_get() failed\n"); 557 return -EIO; 558 } 559 board->use_count++; 560 priv->got_module = 1; 561 } 562 return 0; 563 } 564 565 int ibclose(struct inode *inode, struct file *filep) 566 { 567 unsigned int minor = iminor(inode); 568 struct gpib_board *board; 569 struct gpib_file_private *priv = filep->private_data; 570 struct gpib_descriptor *desc; 571 572 if (minor >= GPIB_MAX_NUM_BOARDS) { 573 pr_err("gpib: invalid minor number of device file\n"); 574 return -ENODEV; 575 } 576 577 board = &board_array[minor]; 578 579 if (priv) { 580 desc = handle_to_descriptor(priv, 0); 581 if (desc) { 582 if (desc->autopoll_enabled) { 583 dev_dbg(board->gpib_dev, "decrementing autospollers\n"); 584 if (board->autospollers > 0) 585 board->autospollers--; 586 else 587 dev_err(board->gpib_dev, 588 "Attempt to decrement zero autospollers\n"); 589 } 590 } else { 591 dev_err(board->gpib_dev, "Unexpected null gpib_descriptor\n"); 592 } 593 594 cleanup_open_devices(priv, board); 595 596 if (atomic_read(&priv->holding_mutex)) 597 mutex_unlock(&board->user_mutex); 598 599 if (priv->got_module && board->use_count) { 600 module_put(board->provider_module); 601 --board->use_count; 602 } 603 604 kfree(filep->private_data); 605 filep->private_data = NULL; 606 } 607 608 return 0; 609 } 610 611 long ibioctl(struct file *filep, unsigned int cmd, unsigned long arg) 612 { 613 unsigned int minor = iminor(file_inode(filep)); 614 struct gpib_board *board; 615 struct gpib_file_private *file_priv = filep->private_data; 616 long retval = -ENOTTY; 617 618 if (minor >= GPIB_MAX_NUM_BOARDS) { 619 pr_err("gpib: invalid minor number of device file\n"); 620 return -ENODEV; 621 } 622 board = &board_array[minor]; 623 624 if (mutex_lock_interruptible(&board->big_gpib_mutex)) 625 return -ERESTARTSYS; 626 627 dev_dbg(board->gpib_dev, "ioctl %d, interface=%s, use=%d, onl=%d\n", 628 cmd & 0xff, 629 board->interface ? board->interface->name : "", 630 board->use_count, 631 board->online); 632 633 switch (cmd) { 634 case CFCBOARDTYPE: 635 retval = board_type_ioctl(file_priv, board, arg); 636 goto done; 637 case IBONL: 638 retval = online_ioctl(board, arg); 639 goto done; 640 default: 641 break; 642 } 643 if (!board->interface) { 644 dev_err(board->gpib_dev, "no gpib board configured\n"); 645 retval = -ENODEV; 646 goto done; 647 } 648 if (file_priv->got_module == 0) { 649 if (!try_module_get(board->provider_module)) { 650 dev_err(board->gpib_dev, "try_module_get() failed\n"); 651 retval = -EIO; 652 goto done; 653 } 654 file_priv->got_module = 1; 655 board->use_count++; 656 } 657 switch (cmd) { 658 case CFCBASE: 659 retval = iobase_ioctl(&board->config, arg); 660 goto done; 661 case CFCIRQ: 662 retval = irq_ioctl(&board->config, arg); 663 goto done; 664 case CFCDMA: 665 retval = dma_ioctl(&board->config, arg); 666 goto done; 667 case IBAUTOSPOLL: 668 retval = autospoll_ioctl(board, file_priv, arg); 669 goto done; 670 case IBBOARD_INFO: 671 retval = board_info_ioctl(board, arg); 672 goto done; 673 case IBMUTEX: 674 /* 675 * Need to unlock board->big_gpib_mutex before potentially locking board->user_mutex 676 * to maintain consistent locking order 677 */ 678 mutex_unlock(&board->big_gpib_mutex); 679 return mutex_ioctl(board, file_priv, arg); 680 case IBPAD: 681 retval = pad_ioctl(board, file_priv, arg); 682 goto done; 683 case IBSAD: 684 retval = sad_ioctl(board, file_priv, arg); 685 goto done; 686 case IBSELECT_PCI: 687 retval = select_pci_ioctl(&board->config, arg); 688 goto done; 689 case IBSELECT_DEVICE_PATH: 690 retval = select_device_path_ioctl(&board->config, arg); 691 goto done; 692 default: 693 break; 694 } 695 696 if (!board->online) { 697 retval = -EINVAL; 698 goto done; 699 } 700 701 switch (cmd) { 702 case IBEVENT: 703 retval = event_ioctl(board, arg); 704 goto done; 705 case IBCLOSEDEV: 706 retval = close_dev_ioctl(filep, board, arg); 707 goto done; 708 case IBOPENDEV: 709 retval = open_dev_ioctl(filep, board, arg); 710 goto done; 711 case IBSPOLL_BYTES: 712 retval = status_bytes_ioctl(board, arg); 713 goto done; 714 case IBWAIT: 715 retval = wait_ioctl(file_priv, board, arg); 716 if (retval == -ERESTARTSYS) 717 return retval; 718 goto done; 719 case IBLINES: 720 retval = line_status_ioctl(board, arg); 721 goto done; 722 case IBLOC: 723 board->interface->return_to_local(board); 724 retval = 0; 725 goto done; 726 default: 727 break; 728 } 729 730 spin_lock(&board->locking_pid_spinlock); 731 if (current->pid != board->locking_pid) { 732 spin_unlock(&board->locking_pid_spinlock); 733 retval = -EPERM; 734 goto done; 735 } 736 spin_unlock(&board->locking_pid_spinlock); 737 738 switch (cmd) { 739 case IB_T1_DELAY: 740 retval = t1_delay_ioctl(board, arg); 741 goto done; 742 case IBCAC: 743 retval = take_control_ioctl(board, arg); 744 goto done; 745 case IBCMD: 746 /* 747 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex 748 * before we call them. 749 */ 750 mutex_unlock(&board->big_gpib_mutex); 751 return command_ioctl(file_priv, board, arg); 752 case IBEOS: 753 retval = eos_ioctl(board, arg); 754 goto done; 755 case IBGTS: 756 retval = ibgts(board); 757 goto done; 758 case IBPPC: 759 retval = ppc_ioctl(board, arg); 760 goto done; 761 case IBPP2_SET: 762 retval = set_local_ppoll_mode_ioctl(board, arg); 763 goto done; 764 case IBPP2_GET: 765 retval = get_local_ppoll_mode_ioctl(board, arg); 766 goto done; 767 case IBQUERY_BOARD_RSV: 768 retval = query_board_rsv_ioctl(board, arg); 769 goto done; 770 case IBRD: 771 /* 772 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex 773 * before we call them. 774 */ 775 mutex_unlock(&board->big_gpib_mutex); 776 return read_ioctl(file_priv, board, arg); 777 case IBRPP: 778 retval = parallel_poll_ioctl(board, arg); 779 goto done; 780 case IBRSC: 781 retval = request_system_control_ioctl(board, arg); 782 goto done; 783 case IBRSP: 784 retval = serial_poll_ioctl(board, arg); 785 goto done; 786 case IBRSV: 787 retval = request_service_ioctl(board, arg); 788 goto done; 789 case IBRSV2: 790 retval = request_service2_ioctl(board, arg); 791 goto done; 792 case IBSIC: 793 retval = interface_clear_ioctl(board, arg); 794 goto done; 795 case IBSRE: 796 retval = remote_enable_ioctl(board, arg); 797 goto done; 798 case IBTMO: 799 retval = timeout_ioctl(board, arg); 800 goto done; 801 case IBWRT: 802 /* 803 * IO ioctls can take a long time, we need to unlock board->big_gpib_mutex 804 * before we call them. 805 */ 806 mutex_unlock(&board->big_gpib_mutex); 807 return write_ioctl(file_priv, board, arg); 808 default: 809 retval = -ENOTTY; 810 goto done; 811 } 812 813 done: 814 mutex_unlock(&board->big_gpib_mutex); 815 dev_dbg(board->gpib_dev, "ioctl done status = 0x%lx\n", board->status); 816 return retval; 817 } 818 819 static int board_type_ioctl(struct gpib_file_private *file_priv, 820 struct gpib_board *board, unsigned long arg) 821 { 822 struct list_head *list_ptr; 823 struct gpib_board_type_ioctl cmd; 824 int retval; 825 826 if (!capable(CAP_SYS_ADMIN)) 827 return -EPERM; 828 if (board->online) 829 return -EBUSY; 830 831 retval = copy_from_user(&cmd, (void __user *)arg, 832 sizeof(struct gpib_board_type_ioctl)); 833 if (retval) 834 return -EFAULT; 835 836 for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers; 837 list_ptr = list_ptr->next) { 838 struct gpib_interface_list *entry; 839 840 entry = list_entry(list_ptr, struct gpib_interface_list, list); 841 if (strcmp(entry->interface->name, cmd.name) == 0) { 842 int i; 843 int had_module = file_priv->got_module; 844 845 if (board->use_count) { 846 for (i = 0; i < board->use_count; ++i) 847 module_put(board->provider_module); 848 board->interface = NULL; 849 file_priv->got_module = 0; 850 } 851 board->interface = entry->interface; 852 board->provider_module = entry->module; 853 for (i = 0; i < board->use_count; ++i) { 854 if (!try_module_get(entry->module)) { 855 board->use_count = i; 856 return -EIO; 857 } 858 } 859 if (had_module == 0) { 860 if (!try_module_get(entry->module)) 861 return -EIO; 862 ++board->use_count; 863 } 864 file_priv->got_module = 1; 865 return 0; 866 } 867 } 868 869 return -EINVAL; 870 } 871 872 static int read_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 873 unsigned long arg) 874 { 875 struct gpib_read_write_ioctl read_cmd; 876 u8 __user *userbuf; 877 unsigned long remain; 878 int end_flag = 0; 879 int retval; 880 ssize_t read_ret = 0; 881 struct gpib_descriptor *desc; 882 size_t nbytes; 883 884 retval = copy_from_user(&read_cmd, (void __user *)arg, sizeof(read_cmd)); 885 if (retval) 886 return -EFAULT; 887 888 if (read_cmd.completed_transfer_count > read_cmd.requested_transfer_count) 889 return -EINVAL; 890 891 desc = handle_to_descriptor(file_priv, read_cmd.handle); 892 if (!desc) 893 return -EINVAL; 894 895 if (WARN_ON_ONCE(sizeof(userbuf) > sizeof(read_cmd.buffer_ptr))) 896 return -EFAULT; 897 898 userbuf = (u8 __user *)(unsigned long)read_cmd.buffer_ptr; 899 userbuf += read_cmd.completed_transfer_count; 900 901 remain = read_cmd.requested_transfer_count - read_cmd.completed_transfer_count; 902 903 /* Check write access to buffer */ 904 if (!access_ok(userbuf, remain)) 905 return -EFAULT; 906 907 atomic_set(&desc->io_in_progress, 1); 908 909 /* Read buffer loads till we fill the user supplied buffer */ 910 while (remain > 0 && end_flag == 0) { 911 nbytes = 0; 912 read_ret = ibrd(board, board->buffer, (board->buffer_length < remain) ? 913 board->buffer_length : remain, &end_flag, &nbytes); 914 if (nbytes == 0) 915 break; 916 retval = copy_to_user(userbuf, board->buffer, nbytes); 917 if (retval) { 918 retval = -EFAULT; 919 break; 920 } 921 remain -= nbytes; 922 userbuf += nbytes; 923 if (read_ret < 0) 924 break; 925 } 926 read_cmd.completed_transfer_count = read_cmd.requested_transfer_count - remain; 927 read_cmd.end = end_flag; 928 /* 929 * suppress errors (for example due to timeout or interruption by device clear) 930 * if all bytes got sent. This prevents races that can occur in the various drivers 931 * if a device receives a device clear immediately after a transfer completes and 932 * the driver code wasn't careful enough to handle that case. 933 */ 934 if (remain == 0 || end_flag) 935 read_ret = 0; 936 if (retval == 0) 937 retval = copy_to_user((void __user *)arg, &read_cmd, sizeof(read_cmd)); 938 939 atomic_set(&desc->io_in_progress, 0); 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 desc = handle_to_descriptor(file_priv, cmd.handle); 968 if (!desc) 969 return -EINVAL; 970 971 userbuf = (u8 __user *)(unsigned long)cmd.buffer_ptr; 972 userbuf += cmd.completed_transfer_count; 973 974 no_clear_io_in_prog = cmd.end; 975 cmd.end = 0; 976 977 remain = cmd.requested_transfer_count - cmd.completed_transfer_count; 978 979 /* Check read access to buffer */ 980 if (!access_ok(userbuf, remain)) 981 return -EFAULT; 982 983 /* 984 * Write buffer loads till we empty the user supplied buffer. 985 * Call drivers at least once, even if remain is zero, in 986 * order to allow them to insure previous commands were 987 * completely finished, in the case of a restarted ioctl. 988 */ 989 990 atomic_set(&desc->io_in_progress, 1); 991 992 do { 993 fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ? 994 board->buffer_length : remain); 995 if (fault) { 996 retval = -EFAULT; 997 bytes_written = 0; 998 } else { 999 retval = ibcmd(board, board->buffer, (board->buffer_length < remain) ? 1000 board->buffer_length : remain, &bytes_written); 1001 } 1002 remain -= bytes_written; 1003 userbuf += bytes_written; 1004 if (retval < 0) { 1005 atomic_set(&desc->io_in_progress, 0); 1006 1007 wake_up_interruptible(&board->wait); 1008 break; 1009 } 1010 } while (remain > 0); 1011 1012 cmd.completed_transfer_count = cmd.requested_transfer_count - remain; 1013 1014 if (fault == 0) 1015 fault = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); 1016 1017 /* 1018 * no_clear_io_in_prog (cmd.end) is true when io_in_progress should 1019 * not be set to zero because the cmd in progress is the address setup 1020 * operation for an async read or write. This causes CMPL not to be set 1021 * in general_ibstatus until the async read or write completes. 1022 */ 1023 if (!no_clear_io_in_prog || fault) 1024 atomic_set(&desc->io_in_progress, 0); 1025 1026 wake_up_interruptible(&board->wait); 1027 if (fault) 1028 return -EFAULT; 1029 1030 return retval; 1031 } 1032 1033 static int write_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 1034 unsigned long arg) 1035 { 1036 struct gpib_read_write_ioctl write_cmd; 1037 u8 __user *userbuf; 1038 unsigned long remain; 1039 int retval = 0; 1040 int fault; 1041 struct gpib_descriptor *desc; 1042 1043 fault = copy_from_user(&write_cmd, (void __user *)arg, sizeof(write_cmd)); 1044 if (fault) 1045 return -EFAULT; 1046 1047 if (write_cmd.completed_transfer_count > write_cmd.requested_transfer_count) 1048 return -EINVAL; 1049 1050 desc = handle_to_descriptor(file_priv, write_cmd.handle); 1051 if (!desc) 1052 return -EINVAL; 1053 1054 userbuf = (u8 __user *)(unsigned long)write_cmd.buffer_ptr; 1055 userbuf += write_cmd.completed_transfer_count; 1056 1057 remain = write_cmd.requested_transfer_count - write_cmd.completed_transfer_count; 1058 1059 /* Check read access to buffer */ 1060 if (!access_ok(userbuf, remain)) 1061 return -EFAULT; 1062 1063 atomic_set(&desc->io_in_progress, 1); 1064 1065 /* Write buffer loads till we empty the user supplied buffer */ 1066 while (remain > 0) { 1067 int send_eoi; 1068 size_t bytes_written = 0; 1069 1070 send_eoi = remain <= board->buffer_length && write_cmd.end; 1071 fault = copy_from_user(board->buffer, userbuf, (board->buffer_length < remain) ? 1072 board->buffer_length : remain); 1073 if (fault) { 1074 retval = -EFAULT; 1075 break; 1076 } 1077 retval = ibwrt(board, board->buffer, (board->buffer_length < remain) ? 1078 board->buffer_length : remain, send_eoi, &bytes_written); 1079 remain -= bytes_written; 1080 userbuf += bytes_written; 1081 if (retval < 0) 1082 break; 1083 } 1084 write_cmd.completed_transfer_count = write_cmd.requested_transfer_count - remain; 1085 /* 1086 * suppress errors (for example due to timeout or interruption by device clear) 1087 * if all bytes got sent. This prevents races that can occur in the various drivers 1088 * if a device receives a device clear immediately after a transfer completes and 1089 * the driver code wasn't careful enough to handle that case. 1090 */ 1091 if (remain == 0) 1092 retval = 0; 1093 if (fault == 0) 1094 fault = copy_to_user((void __user *)arg, &write_cmd, sizeof(write_cmd)); 1095 1096 atomic_set(&desc->io_in_progress, 0); 1097 1098 wake_up_interruptible(&board->wait); 1099 if (fault) 1100 return -EFAULT; 1101 1102 return retval; 1103 } 1104 1105 static int status_bytes_ioctl(struct gpib_board *board, unsigned long arg) 1106 { 1107 struct gpib_status_queue *device; 1108 struct gpib_spoll_bytes_ioctl cmd; 1109 int retval; 1110 1111 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1112 if (retval) 1113 return -EFAULT; 1114 1115 device = get_gpib_status_queue(board, cmd.pad, cmd.sad); 1116 if (!device) 1117 cmd.num_bytes = 0; 1118 else 1119 cmd.num_bytes = num_status_bytes(device); 1120 1121 retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); 1122 if (retval) 1123 return -EFAULT; 1124 1125 return 0; 1126 } 1127 1128 static int increment_open_device_count(struct gpib_board *board, struct list_head *head, 1129 unsigned int pad, int sad) 1130 { 1131 struct list_head *list_ptr; 1132 struct gpib_status_queue *device; 1133 1134 /* 1135 * first see if address has already been opened, then increment 1136 * open count 1137 */ 1138 for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { 1139 device = list_entry(list_ptr, struct gpib_status_queue, list); 1140 if (gpib_address_equal(device->pad, device->sad, pad, sad)) { 1141 dev_dbg(board->gpib_dev, "incrementing open count for pad %i, sad %i\n", 1142 device->pad, device->sad); 1143 device->reference_count++; 1144 return 0; 1145 } 1146 } 1147 1148 /* otherwise we need to allocate a new struct gpib_status_queue */ 1149 device = kmalloc_obj(struct gpib_status_queue, GFP_ATOMIC); 1150 if (!device) 1151 return -ENOMEM; 1152 init_gpib_status_queue(device); 1153 device->pad = pad; 1154 device->sad = sad; 1155 device->reference_count = 1; 1156 1157 list_add(&device->list, head); 1158 1159 dev_dbg(board->gpib_dev, "opened pad %i, sad %i\n", device->pad, device->sad); 1160 1161 return 0; 1162 } 1163 1164 static int subtract_open_device_count(struct gpib_board *board, struct list_head *head, 1165 unsigned int pad, int sad, unsigned int count) 1166 { 1167 struct gpib_status_queue *device; 1168 struct list_head *list_ptr; 1169 1170 for (list_ptr = head->next; list_ptr != head; list_ptr = list_ptr->next) { 1171 device = list_entry(list_ptr, struct gpib_status_queue, list); 1172 if (gpib_address_equal(device->pad, device->sad, pad, sad)) { 1173 dev_dbg(board->gpib_dev, "decrementing open count for pad %i, sad %i\n", 1174 device->pad, device->sad); 1175 if (count > device->reference_count) { 1176 dev_err(board->gpib_dev, "bug! in %s()\n", __func__); 1177 return -EINVAL; 1178 } 1179 device->reference_count -= count; 1180 if (device->reference_count == 0) { 1181 dev_dbg(board->gpib_dev, "closing pad %i, sad %i\n", 1182 device->pad, device->sad); 1183 list_del(list_ptr); 1184 kfree(device); 1185 } 1186 return 0; 1187 } 1188 } 1189 dev_err(board->gpib_dev, "bug! tried to close address that was never opened!\n"); 1190 return -EINVAL; 1191 } 1192 1193 static inline int decrement_open_device_count(struct gpib_board *board, struct list_head *head, 1194 unsigned int pad, int sad) 1195 { 1196 return subtract_open_device_count(board, head, pad, sad, 1); 1197 } 1198 1199 static int cleanup_open_devices(struct gpib_file_private *file_priv, struct gpib_board *board) 1200 { 1201 int retval = 0; 1202 int i; 1203 1204 for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) { 1205 struct gpib_descriptor *desc; 1206 1207 desc = file_priv->descriptors[i]; 1208 if (!desc) 1209 continue; 1210 1211 if (desc->is_board == 0) { 1212 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1213 desc->sad); 1214 if (retval < 0) 1215 return retval; 1216 } 1217 kfree(desc); 1218 file_priv->descriptors[i] = NULL; 1219 } 1220 1221 return 0; 1222 } 1223 1224 static int open_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg) 1225 { 1226 struct gpib_open_dev_ioctl open_dev_cmd; 1227 int retval; 1228 struct gpib_file_private *file_priv = filep->private_data; 1229 int i; 1230 1231 retval = copy_from_user(&open_dev_cmd, (void __user *)arg, sizeof(open_dev_cmd)); 1232 if (retval) 1233 return -EFAULT; 1234 1235 if (mutex_lock_interruptible(&file_priv->descriptors_mutex)) 1236 return -ERESTARTSYS; 1237 for (i = 0; i < GPIB_MAX_NUM_DESCRIPTORS; i++) 1238 if (!file_priv->descriptors[i]) 1239 break; 1240 if (i == GPIB_MAX_NUM_DESCRIPTORS) { 1241 mutex_unlock(&file_priv->descriptors_mutex); 1242 return -ERANGE; 1243 } 1244 file_priv->descriptors[i] = kmalloc_obj(struct gpib_descriptor, 1245 GFP_KERNEL); 1246 if (!file_priv->descriptors[i]) { 1247 mutex_unlock(&file_priv->descriptors_mutex); 1248 return -ENOMEM; 1249 } 1250 init_gpib_descriptor(file_priv->descriptors[i]); 1251 1252 file_priv->descriptors[i]->pad = open_dev_cmd.pad; 1253 file_priv->descriptors[i]->sad = open_dev_cmd.sad; 1254 file_priv->descriptors[i]->is_board = open_dev_cmd.is_board; 1255 mutex_unlock(&file_priv->descriptors_mutex); 1256 1257 retval = increment_open_device_count(board, &board->device_list, open_dev_cmd.pad, 1258 open_dev_cmd.sad); 1259 if (retval < 0) 1260 return retval; 1261 1262 /* 1263 * clear stuck srq state, since we may be able to find service request on 1264 * the new device 1265 */ 1266 atomic_set(&board->stuck_srq, 0); 1267 1268 open_dev_cmd.handle = i; 1269 retval = copy_to_user((void __user *)arg, &open_dev_cmd, sizeof(open_dev_cmd)); 1270 if (retval) 1271 return -EFAULT; 1272 1273 return 0; 1274 } 1275 1276 static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg) 1277 { 1278 struct gpib_close_dev_ioctl cmd; 1279 struct gpib_file_private *file_priv = filep->private_data; 1280 int retval; 1281 1282 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1283 if (retval) 1284 return -EFAULT; 1285 1286 if (cmd.handle >= GPIB_MAX_NUM_DESCRIPTORS) 1287 return -EINVAL; 1288 if (!file_priv->descriptors[cmd.handle]) 1289 return -EINVAL; 1290 1291 retval = decrement_open_device_count(board, &board->device_list, 1292 file_priv->descriptors[cmd.handle]->pad, 1293 file_priv->descriptors[cmd.handle]->sad); 1294 if (retval < 0) 1295 return retval; 1296 1297 kfree(file_priv->descriptors[cmd.handle]); 1298 file_priv->descriptors[cmd.handle] = NULL; 1299 1300 return 0; 1301 } 1302 1303 static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg) 1304 { 1305 struct gpib_serial_poll_ioctl serial_cmd; 1306 int retval; 1307 1308 retval = copy_from_user(&serial_cmd, (void __user *)arg, sizeof(serial_cmd)); 1309 if (retval) 1310 return -EFAULT; 1311 1312 retval = get_serial_poll_byte(board, serial_cmd.pad, serial_cmd.sad, board->usec_timeout, 1313 &serial_cmd.status_byte); 1314 if (retval < 0) 1315 return retval; 1316 1317 retval = copy_to_user((void __user *)arg, &serial_cmd, sizeof(serial_cmd)); 1318 if (retval) 1319 return -EFAULT; 1320 1321 return 0; 1322 } 1323 1324 static int wait_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 1325 unsigned long arg) 1326 { 1327 struct gpib_wait_ioctl wait_cmd; 1328 int retval; 1329 struct gpib_descriptor *desc; 1330 1331 retval = copy_from_user(&wait_cmd, (void __user *)arg, sizeof(wait_cmd)); 1332 if (retval) 1333 return -EFAULT; 1334 1335 desc = handle_to_descriptor(file_priv, wait_cmd.handle); 1336 if (!desc) 1337 return -EINVAL; 1338 1339 retval = ibwait(board, wait_cmd.wait_mask, wait_cmd.clear_mask, 1340 wait_cmd.set_mask, &wait_cmd.ibsta, wait_cmd.usec_timeout, desc); 1341 if (retval < 0) 1342 return retval; 1343 1344 retval = copy_to_user((void __user *)arg, &wait_cmd, sizeof(wait_cmd)); 1345 if (retval) 1346 return -EFAULT; 1347 1348 return 0; 1349 } 1350 1351 static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg) 1352 { 1353 u8 poll_byte; 1354 int retval; 1355 1356 retval = ibrpp(board, &poll_byte); 1357 if (retval < 0) 1358 return retval; 1359 1360 retval = copy_to_user((void __user *)arg, &poll_byte, sizeof(poll_byte)); 1361 if (retval) 1362 return -EFAULT; 1363 1364 return 0; 1365 } 1366 1367 static int online_ioctl(struct gpib_board *board, unsigned long arg) 1368 { 1369 struct gpib_online_ioctl online_cmd; 1370 int retval; 1371 void __user *init_data = NULL; 1372 1373 board->config.init_data = NULL; 1374 1375 if (!capable(CAP_SYS_ADMIN)) 1376 return -EPERM; 1377 1378 retval = copy_from_user(&online_cmd, (void __user *)arg, sizeof(online_cmd)); 1379 if (retval) 1380 return -EFAULT; 1381 if (online_cmd.init_data_length > 0) { 1382 board->config.init_data = vmalloc(online_cmd.init_data_length); 1383 if (!board->config.init_data) 1384 return -ENOMEM; 1385 if (WARN_ON_ONCE(sizeof(init_data) > sizeof(online_cmd.init_data_ptr))) 1386 return -EFAULT; 1387 init_data = (void __user *)(unsigned long)(online_cmd.init_data_ptr); 1388 retval = copy_from_user(board->config.init_data, init_data, 1389 online_cmd.init_data_length); 1390 if (retval) { 1391 vfree(board->config.init_data); 1392 return -EFAULT; 1393 } 1394 board->config.init_data_length = online_cmd.init_data_length; 1395 } else { 1396 board->config.init_data = NULL; 1397 board->config.init_data_length = 0; 1398 } 1399 if (online_cmd.online) 1400 retval = ibonline(board); 1401 else 1402 retval = iboffline(board); 1403 if (board->config.init_data) { 1404 vfree(board->config.init_data); 1405 board->config.init_data = NULL; 1406 board->config.init_data_length = 0; 1407 } 1408 return retval; 1409 } 1410 1411 static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg) 1412 { 1413 int enable; 1414 int retval; 1415 1416 retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); 1417 if (retval) 1418 return -EFAULT; 1419 1420 return ibsre(board, enable); 1421 } 1422 1423 static int take_control_ioctl(struct gpib_board *board, unsigned long arg) 1424 { 1425 int synchronous; 1426 int retval; 1427 1428 retval = copy_from_user(&synchronous, (void __user *)arg, sizeof(synchronous)); 1429 if (retval) 1430 return -EFAULT; 1431 1432 return ibcac(board, synchronous, 1); 1433 } 1434 1435 static int line_status_ioctl(struct gpib_board *board, unsigned long arg) 1436 { 1437 short lines; 1438 int retval; 1439 1440 retval = iblines(board, &lines); 1441 if (retval < 0) 1442 return retval; 1443 1444 retval = copy_to_user((void __user *)arg, &lines, sizeof(lines)); 1445 if (retval) 1446 return -EFAULT; 1447 1448 return 0; 1449 } 1450 1451 static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1452 unsigned long arg) 1453 { 1454 struct gpib_pad_ioctl cmd; 1455 int retval; 1456 struct gpib_descriptor *desc; 1457 1458 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1459 if (retval) 1460 return -EFAULT; 1461 1462 desc = handle_to_descriptor(file_priv, cmd.handle); 1463 if (!desc) 1464 return -EINVAL; 1465 1466 if (desc->is_board) { 1467 retval = ibpad(board, cmd.pad); 1468 if (retval < 0) 1469 return retval; 1470 } else { 1471 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1472 desc->sad); 1473 if (retval < 0) 1474 return retval; 1475 1476 desc->pad = cmd.pad; 1477 1478 retval = increment_open_device_count(board, &board->device_list, desc->pad, 1479 desc->sad); 1480 if (retval < 0) 1481 return retval; 1482 } 1483 1484 return 0; 1485 } 1486 1487 static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1488 unsigned long arg) 1489 { 1490 struct gpib_sad_ioctl cmd; 1491 int retval; 1492 struct gpib_descriptor *desc; 1493 1494 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1495 if (retval) 1496 return -EFAULT; 1497 1498 desc = handle_to_descriptor(file_priv, cmd.handle); 1499 if (!desc) 1500 return -EINVAL; 1501 1502 if (desc->is_board) { 1503 retval = ibsad(board, cmd.sad); 1504 if (retval < 0) 1505 return retval; 1506 } else { 1507 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1508 desc->sad); 1509 if (retval < 0) 1510 return retval; 1511 1512 desc->sad = cmd.sad; 1513 1514 retval = increment_open_device_count(board, &board->device_list, desc->pad, 1515 desc->sad); 1516 if (retval < 0) 1517 return retval; 1518 } 1519 return 0; 1520 } 1521 1522 static int eos_ioctl(struct gpib_board *board, unsigned long arg) 1523 { 1524 struct gpib_eos_ioctl eos_cmd; 1525 int retval; 1526 1527 retval = copy_from_user(&eos_cmd, (void __user *)arg, sizeof(eos_cmd)); 1528 if (retval) 1529 return -EFAULT; 1530 1531 return ibeos(board, eos_cmd.eos, eos_cmd.eos_flags); 1532 } 1533 1534 static int request_service_ioctl(struct gpib_board *board, unsigned long arg) 1535 { 1536 u8 status_byte; 1537 int retval; 1538 1539 retval = copy_from_user(&status_byte, (void __user *)arg, sizeof(status_byte)); 1540 if (retval) 1541 return -EFAULT; 1542 1543 return ibrsv2(board, status_byte, status_byte & request_service_bit); 1544 } 1545 1546 static int request_service2_ioctl(struct gpib_board *board, unsigned long arg) 1547 { 1548 struct gpib_request_service2 request_service2_cmd; 1549 int retval; 1550 1551 retval = copy_from_user(&request_service2_cmd, (void __user *)arg, 1552 sizeof(struct gpib_request_service2)); 1553 if (retval) 1554 return -EFAULT; 1555 1556 return ibrsv2(board, request_service2_cmd.status_byte, 1557 request_service2_cmd.new_reason_for_service); 1558 } 1559 1560 static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg) 1561 { 1562 u64 base_addr; 1563 int retval; 1564 1565 if (!capable(CAP_SYS_ADMIN)) 1566 return -EPERM; 1567 1568 retval = copy_from_user(&base_addr, (void __user *)arg, sizeof(base_addr)); 1569 if (retval) 1570 return -EFAULT; 1571 1572 if (WARN_ON_ONCE(sizeof(void *) > sizeof(base_addr))) 1573 return -EFAULT; 1574 config->ibbase = base_addr; 1575 1576 return 0; 1577 } 1578 1579 static int irq_ioctl(struct gpib_board_config *config, unsigned long arg) 1580 { 1581 unsigned int irq; 1582 int retval; 1583 1584 if (!capable(CAP_SYS_ADMIN)) 1585 return -EPERM; 1586 1587 retval = copy_from_user(&irq, (void __user *)arg, sizeof(irq)); 1588 if (retval) 1589 return -EFAULT; 1590 1591 config->ibirq = irq; 1592 1593 return 0; 1594 } 1595 1596 static int dma_ioctl(struct gpib_board_config *config, unsigned long arg) 1597 { 1598 unsigned int dma_channel; 1599 int retval; 1600 1601 if (!capable(CAP_SYS_ADMIN)) 1602 return -EPERM; 1603 1604 retval = copy_from_user(&dma_channel, (void __user *)arg, sizeof(dma_channel)); 1605 if (retval) 1606 return -EFAULT; 1607 1608 config->ibdma = dma_channel; 1609 1610 return 0; 1611 } 1612 1613 static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1614 unsigned long arg) 1615 { 1616 short enable; 1617 int retval; 1618 struct gpib_descriptor *desc; 1619 1620 retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); 1621 if (retval) 1622 return -EFAULT; 1623 1624 desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */ 1625 1626 if (enable) { 1627 if (!desc->autopoll_enabled) { 1628 board->autospollers++; 1629 desc->autopoll_enabled = 1; 1630 } 1631 retval = 0; 1632 } else { 1633 if (desc->autopoll_enabled) { 1634 desc->autopoll_enabled = 0; 1635 if (board->autospollers > 0) { 1636 board->autospollers--; 1637 retval = 0; 1638 } else { 1639 dev_err(board->gpib_dev, 1640 "tried to set number of autospollers negative\n"); 1641 retval = -EINVAL; 1642 } 1643 } else { 1644 dev_err(board->gpib_dev, "autopoll disable requested before enable\n"); 1645 retval = -EINVAL; 1646 } 1647 } 1648 return retval; 1649 } 1650 1651 static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1652 unsigned long arg) 1653 { 1654 int retval, lock_mutex; 1655 1656 retval = copy_from_user(&lock_mutex, (void __user *)arg, sizeof(lock_mutex)); 1657 if (retval) 1658 return -EFAULT; 1659 1660 if (lock_mutex) { 1661 retval = mutex_lock_interruptible(&board->user_mutex); 1662 if (retval) 1663 return -ERESTARTSYS; 1664 1665 spin_lock(&board->locking_pid_spinlock); 1666 board->locking_pid = current->pid; 1667 spin_unlock(&board->locking_pid_spinlock); 1668 1669 atomic_set(&file_priv->holding_mutex, 1); 1670 1671 dev_dbg(board->gpib_dev, "locked board mutex\n"); 1672 } else { 1673 spin_lock(&board->locking_pid_spinlock); 1674 if (current->pid != board->locking_pid) { 1675 dev_err(board->gpib_dev, "bug! pid %i tried to release mutex held by pid %i\n", 1676 current->pid, board->locking_pid); 1677 spin_unlock(&board->locking_pid_spinlock); 1678 return -EPERM; 1679 } 1680 board->locking_pid = 0; 1681 spin_unlock(&board->locking_pid_spinlock); 1682 1683 atomic_set(&file_priv->holding_mutex, 0); 1684 1685 mutex_unlock(&board->user_mutex); 1686 dev_dbg(board->gpib_dev, "unlocked board mutex\n"); 1687 } 1688 return 0; 1689 } 1690 1691 static int timeout_ioctl(struct gpib_board *board, unsigned long arg) 1692 { 1693 unsigned int timeout; 1694 int retval; 1695 1696 retval = copy_from_user(&timeout, (void __user *)arg, sizeof(timeout)); 1697 if (retval) 1698 return -EFAULT; 1699 1700 board->usec_timeout = timeout; 1701 dev_dbg(board->gpib_dev, "timeout set to %i usec\n", timeout); 1702 1703 return 0; 1704 } 1705 1706 static int ppc_ioctl(struct gpib_board *board, unsigned long arg) 1707 { 1708 struct gpib_ppoll_config_ioctl cmd; 1709 int retval; 1710 1711 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1712 if (retval) 1713 return -EFAULT; 1714 1715 if (cmd.set_ist) { 1716 board->ist = 1; 1717 board->interface->parallel_poll_response(board, board->ist); 1718 } else if (cmd.clear_ist) { 1719 board->ist = 0; 1720 board->interface->parallel_poll_response(board, board->ist); 1721 } 1722 1723 if (cmd.config) { 1724 retval = ibppc(board, cmd.config); 1725 if (retval < 0) 1726 return retval; 1727 } 1728 1729 return 0; 1730 } 1731 1732 static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) 1733 { 1734 short cmd; 1735 int retval; 1736 1737 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1738 if (retval) 1739 return -EFAULT; 1740 1741 if (!board->interface->local_parallel_poll_mode) 1742 return -ENOENT; 1743 board->local_ppoll_mode = cmd != 0; 1744 board->interface->local_parallel_poll_mode(board, board->local_ppoll_mode); 1745 1746 return 0; 1747 } 1748 1749 static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) 1750 { 1751 short cmd; 1752 int retval; 1753 1754 cmd = board->local_ppoll_mode; 1755 retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); 1756 if (retval) 1757 return -EFAULT; 1758 1759 return 0; 1760 } 1761 1762 static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg) 1763 { 1764 int status; 1765 int retval; 1766 1767 status = board->interface->serial_poll_status(board); 1768 1769 retval = copy_to_user((void __user *)arg, &status, sizeof(status)); 1770 if (retval) 1771 return -EFAULT; 1772 1773 return 0; 1774 } 1775 1776 static int board_info_ioctl(const struct gpib_board *board, unsigned long arg) 1777 { 1778 struct gpib_board_info_ioctl info = { }; 1779 int retval; 1780 1781 info.pad = board->pad; 1782 info.sad = board->sad; 1783 info.parallel_poll_configuration = board->parallel_poll_configuration; 1784 info.is_system_controller = board->master; 1785 if (board->autospollers) 1786 info.autopolling = 1; 1787 else 1788 info.autopolling = 0; 1789 info.t1_delay = board->t1_nano_sec; 1790 info.ist = board->ist; 1791 info.no_7_bit_eos = board->interface->no_7_bit_eos; 1792 retval = copy_to_user((void __user *)arg, &info, sizeof(info)); 1793 if (retval) 1794 return -EFAULT; 1795 1796 return 0; 1797 } 1798 1799 static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg) 1800 { 1801 unsigned int usec_duration; 1802 int retval; 1803 1804 retval = copy_from_user(&usec_duration, (void __user *)arg, sizeof(usec_duration)); 1805 if (retval) 1806 return -EFAULT; 1807 1808 return ibsic(board, usec_duration); 1809 } 1810 1811 static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg) 1812 { 1813 struct gpib_select_pci_ioctl selection; 1814 int retval; 1815 1816 if (!capable(CAP_SYS_ADMIN)) 1817 return -EPERM; 1818 1819 retval = copy_from_user(&selection, (void __user *)arg, sizeof(selection)); 1820 if (retval) 1821 return -EFAULT; 1822 1823 config->pci_bus = selection.pci_bus; 1824 config->pci_slot = selection.pci_slot; 1825 1826 return 0; 1827 } 1828 1829 static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg) 1830 { 1831 struct gpib_select_device_path_ioctl *selection; 1832 int retval; 1833 1834 if (!capable(CAP_SYS_ADMIN)) 1835 return -EPERM; 1836 1837 selection = vmalloc(sizeof(struct gpib_select_device_path_ioctl)); 1838 if (!selection) 1839 return -ENOMEM; 1840 1841 retval = copy_from_user(selection, (void __user *)arg, 1842 sizeof(struct gpib_select_device_path_ioctl)); 1843 if (retval) { 1844 vfree(selection); 1845 return -EFAULT; 1846 } 1847 1848 selection->device_path[sizeof(selection->device_path) - 1] = '\0'; 1849 kfree(config->device_path); 1850 config->device_path = NULL; 1851 if (strlen(selection->device_path) > 0) 1852 config->device_path = kstrdup(selection->device_path, GFP_KERNEL); 1853 1854 vfree(selection); 1855 return 0; 1856 } 1857 1858 unsigned int num_gpib_events(const struct gpib_event_queue *queue) 1859 { 1860 return queue->num_events; 1861 } 1862 1863 static int push_gpib_event_nolock(struct gpib_board *board, short event_type) 1864 { 1865 struct gpib_event_queue *queue = &board->event_queue; 1866 struct list_head *head = &queue->event_head; 1867 struct gpib_event *event; 1868 static const unsigned int max_num_events = 1024; 1869 int retval; 1870 1871 if (num_gpib_events(queue) >= max_num_events) { 1872 short lost_event; 1873 1874 queue->dropped_event = 1; 1875 retval = pop_gpib_event_nolock(board, queue, &lost_event); 1876 if (retval < 0) 1877 return retval; 1878 } 1879 1880 event = kmalloc_obj(struct gpib_event, GFP_ATOMIC); 1881 if (!event) { 1882 queue->dropped_event = 1; 1883 dev_err(board->gpib_dev, "failed to allocate memory for event\n"); 1884 return -ENOMEM; 1885 } 1886 1887 INIT_LIST_HEAD(&event->list); 1888 event->event_type = event_type; 1889 1890 list_add_tail(&event->list, head); 1891 1892 queue->num_events++; 1893 1894 dev_dbg(board->gpib_dev, "pushed event %i, %i in queue\n", 1895 (int)event_type, num_gpib_events(queue)); 1896 1897 return 0; 1898 } 1899 1900 // push event onto back of event queue 1901 int push_gpib_event(struct gpib_board *board, short event_type) 1902 { 1903 unsigned long flags; 1904 int retval; 1905 1906 spin_lock_irqsave(&board->event_queue.lock, flags); 1907 retval = push_gpib_event_nolock(board, event_type); 1908 spin_unlock_irqrestore(&board->event_queue.lock, flags); 1909 1910 if (event_type == EVENT_DEV_TRG) 1911 board->status |= DTAS; 1912 if (event_type == EVENT_DEV_CLR) 1913 board->status |= DCAS; 1914 1915 return retval; 1916 } 1917 EXPORT_SYMBOL(push_gpib_event); 1918 1919 static int pop_gpib_event_nolock(struct gpib_board *board, 1920 struct gpib_event_queue *queue, short *event_type) 1921 { 1922 struct list_head *head = &queue->event_head; 1923 struct list_head *front = head->next; 1924 struct gpib_event *event; 1925 1926 if (num_gpib_events(queue) == 0) { 1927 *event_type = EVENT_NONE; 1928 return 0; 1929 } 1930 1931 if (front == head) 1932 return -EIO; 1933 1934 if (queue->dropped_event) { 1935 queue->dropped_event = 0; 1936 return -EPIPE; 1937 } 1938 1939 event = list_entry(front, struct gpib_event, list); 1940 *event_type = event->event_type; 1941 1942 list_del(front); 1943 kfree(event); 1944 1945 queue->num_events--; 1946 1947 dev_dbg(board->gpib_dev, "popped event %i, %i in queue\n", 1948 (int)*event_type, num_gpib_events(queue)); 1949 1950 return 0; 1951 } 1952 1953 // pop event from front of event queue 1954 int pop_gpib_event(struct gpib_board *board, struct gpib_event_queue *queue, short *event_type) 1955 { 1956 unsigned long flags; 1957 int retval; 1958 1959 spin_lock_irqsave(&queue->lock, flags); 1960 retval = pop_gpib_event_nolock(board, queue, event_type); 1961 spin_unlock_irqrestore(&queue->lock, flags); 1962 return retval; 1963 } 1964 1965 static int event_ioctl(struct gpib_board *board, unsigned long arg) 1966 { 1967 short user_event; 1968 int retval; 1969 short event; 1970 1971 retval = pop_gpib_event(board, &board->event_queue, &event); 1972 if (retval < 0) 1973 return retval; 1974 1975 user_event = event; 1976 1977 retval = copy_to_user((void __user *)arg, &user_event, sizeof(user_event)); 1978 if (retval) 1979 return -EFAULT; 1980 1981 return 0; 1982 } 1983 1984 static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg) 1985 { 1986 int request_control; 1987 int retval; 1988 1989 retval = copy_from_user(&request_control, (void __user *)arg, sizeof(request_control)); 1990 if (retval) 1991 return -EFAULT; 1992 1993 return ibrsc(board, request_control); 1994 } 1995 1996 static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg) 1997 { 1998 unsigned int cmd; 1999 unsigned int delay; 2000 int retval; 2001 2002 if (!board->interface->t1_delay) 2003 return -ENOENT; 2004 2005 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 2006 if (retval) 2007 return -EFAULT; 2008 2009 delay = cmd; 2010 2011 retval = board->interface->t1_delay(board, delay); 2012 if (retval < 0) 2013 return retval; 2014 2015 board->t1_nano_sec = retval; 2016 return 0; 2017 } 2018 2019 static const struct file_operations ib_fops = { 2020 .owner = THIS_MODULE, 2021 .llseek = NULL, 2022 .unlocked_ioctl = &ibioctl, 2023 .compat_ioctl = &ibioctl, 2024 .open = &ibopen, 2025 .release = &ibclose, 2026 }; 2027 2028 struct gpib_board board_array[GPIB_MAX_NUM_BOARDS]; 2029 2030 LIST_HEAD(registered_drivers); 2031 2032 void init_gpib_descriptor(struct gpib_descriptor *desc) 2033 { 2034 desc->pad = 0; 2035 desc->sad = -1; 2036 desc->is_board = 0; 2037 desc->autopoll_enabled = 0; 2038 atomic_set(&desc->io_in_progress, 0); 2039 } 2040 2041 int gpib_register_driver(struct gpib_interface *interface, struct module *provider_module) 2042 { 2043 struct gpib_interface_list *entry; 2044 2045 entry = kmalloc_obj(*entry); 2046 if (!entry) 2047 return -ENOMEM; 2048 2049 entry->interface = interface; 2050 entry->module = provider_module; 2051 list_add(&entry->list, ®istered_drivers); 2052 2053 return 0; 2054 } 2055 EXPORT_SYMBOL(gpib_register_driver); 2056 2057 void gpib_unregister_driver(struct gpib_interface *interface) 2058 { 2059 int i; 2060 struct list_head *list_ptr; 2061 2062 for (i = 0; i < GPIB_MAX_NUM_BOARDS; i++) { 2063 struct gpib_board *board = &board_array[i]; 2064 2065 if (board->interface == interface) { 2066 if (board->use_count > 0) 2067 pr_warn("gpib: Warning: deregistered interface %s in use\n", 2068 interface->name); 2069 iboffline(board); 2070 board->interface = NULL; 2071 } 2072 } 2073 for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers;) { 2074 struct gpib_interface_list *entry; 2075 2076 entry = list_entry(list_ptr, struct gpib_interface_list, list); 2077 list_ptr = list_ptr->next; 2078 if (entry->interface == interface) { 2079 list_del(&entry->list); 2080 kfree(entry); 2081 } 2082 } 2083 } 2084 EXPORT_SYMBOL(gpib_unregister_driver); 2085 2086 static void init_gpib_board_config(struct gpib_board_config *config) 2087 { 2088 memset(config, 0, sizeof(struct gpib_board_config)); 2089 config->pci_bus = -1; 2090 config->pci_slot = -1; 2091 } 2092 2093 void init_gpib_board(struct gpib_board *board) 2094 { 2095 board->interface = NULL; 2096 board->provider_module = NULL; 2097 board->buffer = NULL; 2098 board->buffer_length = 0; 2099 board->status = 0; 2100 init_waitqueue_head(&board->wait); 2101 mutex_init(&board->user_mutex); 2102 mutex_init(&board->big_gpib_mutex); 2103 board->locking_pid = 0; 2104 spin_lock_init(&board->locking_pid_spinlock); 2105 spin_lock_init(&board->spinlock); 2106 timer_setup(&board->timer, NULL, 0); 2107 board->dev = NULL; 2108 board->gpib_dev = NULL; 2109 init_gpib_board_config(&board->config); 2110 board->private_data = NULL; 2111 board->use_count = 0; 2112 INIT_LIST_HEAD(&board->device_list); 2113 board->pad = 0; 2114 board->sad = -1; 2115 board->usec_timeout = 3000000; 2116 board->parallel_poll_configuration = 0; 2117 board->online = 0; 2118 board->autospollers = 0; 2119 board->autospoll_task = NULL; 2120 init_event_queue(&board->event_queue); 2121 board->minor = -1; 2122 init_gpib_pseudo_irq(&board->pseudo_irq); 2123 board->master = 1; 2124 atomic_set(&board->stuck_srq, 0); 2125 board->local_ppoll_mode = 0; 2126 } 2127 2128 int gpib_allocate_board(struct gpib_board *board) 2129 { 2130 if (!board->buffer) { 2131 board->buffer_length = 0x4000; 2132 board->buffer = vmalloc(board->buffer_length); 2133 if (!board->buffer) { 2134 board->buffer_length = 0; 2135 return -ENOMEM; 2136 } 2137 } 2138 return 0; 2139 } 2140 2141 void gpib_deallocate_board(struct gpib_board *board) 2142 { 2143 short dummy; 2144 2145 if (board->buffer) { 2146 vfree(board->buffer); 2147 board->buffer = NULL; 2148 board->buffer_length = 0; 2149 } 2150 while (num_gpib_events(&board->event_queue)) 2151 pop_gpib_event(board, &board->event_queue, &dummy); 2152 } 2153 2154 static void init_board_array(struct gpib_board *board_array, unsigned int length) 2155 { 2156 int i; 2157 2158 for (i = 0; i < length; i++) { 2159 init_gpib_board(&board_array[i]); 2160 board_array[i].minor = i; 2161 } 2162 } 2163 2164 void init_gpib_status_queue(struct gpib_status_queue *device) 2165 { 2166 INIT_LIST_HEAD(&device->list); 2167 INIT_LIST_HEAD(&device->status_bytes); 2168 device->num_status_bytes = 0; 2169 device->reference_count = 0; 2170 device->dropped_byte = 0; 2171 } 2172 2173 static struct class *gpib_class; 2174 2175 static int __init gpib_common_init_module(void) 2176 { 2177 int i; 2178 2179 pr_info("GPIB core driver\n"); 2180 init_board_array(board_array, GPIB_MAX_NUM_BOARDS); 2181 if (register_chrdev(GPIB_CODE, "gpib", &ib_fops)) { 2182 pr_err("gpib: can't get major %d\n", GPIB_CODE); 2183 return -EIO; 2184 } 2185 gpib_class = class_create("gpib_common"); 2186 if (IS_ERR(gpib_class)) { 2187 pr_err("gpib: failed to create gpib class\n"); 2188 unregister_chrdev(GPIB_CODE, "gpib"); 2189 return PTR_ERR(gpib_class); 2190 } 2191 for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) 2192 board_array[i].gpib_dev = device_create(gpib_class, NULL, 2193 MKDEV(GPIB_CODE, i), NULL, "gpib%i", i); 2194 2195 return 0; 2196 } 2197 2198 static void __exit gpib_common_exit_module(void) 2199 { 2200 int i; 2201 2202 for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) 2203 device_destroy(gpib_class, MKDEV(GPIB_CODE, i)); 2204 2205 class_destroy(gpib_class); 2206 unregister_chrdev(GPIB_CODE, "gpib"); 2207 } 2208 2209 int gpib_match_device_path(struct device *dev, const char *device_path_in) 2210 { 2211 if (device_path_in) { 2212 char *device_path; 2213 2214 device_path = kobject_get_path(&dev->kobj, GFP_KERNEL); 2215 if (!device_path) { 2216 dev_err(dev, "kobject_get_path returned NULL."); 2217 return 0; 2218 } 2219 if (strcmp(device_path_in, device_path) != 0) { 2220 kfree(device_path); 2221 return 0; 2222 } 2223 kfree(device_path); 2224 } 2225 return 1; 2226 } 2227 EXPORT_SYMBOL(gpib_match_device_path); 2228 2229 struct pci_dev *gpib_pci_get_device(const struct gpib_board_config *config, unsigned int vendor_id, 2230 unsigned int device_id, struct pci_dev *from) 2231 { 2232 struct pci_dev *pci_device = from; 2233 2234 while ((pci_device = pci_get_device(vendor_id, device_id, pci_device))) { 2235 if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) 2236 continue; 2237 if (config->pci_slot >= 0 && config->pci_slot != 2238 PCI_SLOT(pci_device->devfn)) 2239 continue; 2240 if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) 2241 continue; 2242 return pci_device; 2243 } 2244 return NULL; 2245 } 2246 EXPORT_SYMBOL(gpib_pci_get_device); 2247 2248 struct pci_dev *gpib_pci_get_subsys(const struct gpib_board_config *config, unsigned int vendor_id, 2249 unsigned int device_id, unsigned int ss_vendor, 2250 unsigned int ss_device, 2251 struct pci_dev *from) 2252 { 2253 struct pci_dev *pci_device = from; 2254 2255 while ((pci_device = pci_get_subsys(vendor_id, device_id, 2256 ss_vendor, ss_device, pci_device))) { 2257 if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) 2258 continue; 2259 if (config->pci_slot >= 0 && config->pci_slot != 2260 PCI_SLOT(pci_device->devfn)) 2261 continue; 2262 if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) 2263 continue; 2264 return pci_device; 2265 } 2266 return NULL; 2267 } 2268 EXPORT_SYMBOL(gpib_pci_get_subsys); 2269 2270 module_init(gpib_common_init_module); 2271 module_exit(gpib_common_exit_module); 2272 2273