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(sizeof(*status), GFP_KERNEL); 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(sizeof(struct gpib_descriptor), GFP_KERNEL); 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(sizeof(struct gpib_file_private), GFP_KERNEL); 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(sizeof(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(sizeof(struct gpib_descriptor), GFP_KERNEL); 1245 if (!file_priv->descriptors[i]) { 1246 mutex_unlock(&file_priv->descriptors_mutex); 1247 return -ENOMEM; 1248 } 1249 init_gpib_descriptor(file_priv->descriptors[i]); 1250 1251 file_priv->descriptors[i]->pad = open_dev_cmd.pad; 1252 file_priv->descriptors[i]->sad = open_dev_cmd.sad; 1253 file_priv->descriptors[i]->is_board = open_dev_cmd.is_board; 1254 mutex_unlock(&file_priv->descriptors_mutex); 1255 1256 retval = increment_open_device_count(board, &board->device_list, open_dev_cmd.pad, 1257 open_dev_cmd.sad); 1258 if (retval < 0) 1259 return retval; 1260 1261 /* 1262 * clear stuck srq state, since we may be able to find service request on 1263 * the new device 1264 */ 1265 atomic_set(&board->stuck_srq, 0); 1266 1267 open_dev_cmd.handle = i; 1268 retval = copy_to_user((void __user *)arg, &open_dev_cmd, sizeof(open_dev_cmd)); 1269 if (retval) 1270 return -EFAULT; 1271 1272 return 0; 1273 } 1274 1275 static int close_dev_ioctl(struct file *filep, struct gpib_board *board, unsigned long arg) 1276 { 1277 struct gpib_close_dev_ioctl cmd; 1278 struct gpib_file_private *file_priv = filep->private_data; 1279 int retval; 1280 1281 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1282 if (retval) 1283 return -EFAULT; 1284 1285 if (cmd.handle >= GPIB_MAX_NUM_DESCRIPTORS) 1286 return -EINVAL; 1287 if (!file_priv->descriptors[cmd.handle]) 1288 return -EINVAL; 1289 1290 retval = decrement_open_device_count(board, &board->device_list, 1291 file_priv->descriptors[cmd.handle]->pad, 1292 file_priv->descriptors[cmd.handle]->sad); 1293 if (retval < 0) 1294 return retval; 1295 1296 kfree(file_priv->descriptors[cmd.handle]); 1297 file_priv->descriptors[cmd.handle] = NULL; 1298 1299 return 0; 1300 } 1301 1302 static int serial_poll_ioctl(struct gpib_board *board, unsigned long arg) 1303 { 1304 struct gpib_serial_poll_ioctl serial_cmd; 1305 int retval; 1306 1307 retval = copy_from_user(&serial_cmd, (void __user *)arg, sizeof(serial_cmd)); 1308 if (retval) 1309 return -EFAULT; 1310 1311 retval = get_serial_poll_byte(board, serial_cmd.pad, serial_cmd.sad, board->usec_timeout, 1312 &serial_cmd.status_byte); 1313 if (retval < 0) 1314 return retval; 1315 1316 retval = copy_to_user((void __user *)arg, &serial_cmd, sizeof(serial_cmd)); 1317 if (retval) 1318 return -EFAULT; 1319 1320 return 0; 1321 } 1322 1323 static int wait_ioctl(struct gpib_file_private *file_priv, struct gpib_board *board, 1324 unsigned long arg) 1325 { 1326 struct gpib_wait_ioctl wait_cmd; 1327 int retval; 1328 struct gpib_descriptor *desc; 1329 1330 retval = copy_from_user(&wait_cmd, (void __user *)arg, sizeof(wait_cmd)); 1331 if (retval) 1332 return -EFAULT; 1333 1334 desc = handle_to_descriptor(file_priv, wait_cmd.handle); 1335 if (!desc) 1336 return -EINVAL; 1337 1338 retval = ibwait(board, wait_cmd.wait_mask, wait_cmd.clear_mask, 1339 wait_cmd.set_mask, &wait_cmd.ibsta, wait_cmd.usec_timeout, desc); 1340 if (retval < 0) 1341 return retval; 1342 1343 retval = copy_to_user((void __user *)arg, &wait_cmd, sizeof(wait_cmd)); 1344 if (retval) 1345 return -EFAULT; 1346 1347 return 0; 1348 } 1349 1350 static int parallel_poll_ioctl(struct gpib_board *board, unsigned long arg) 1351 { 1352 u8 poll_byte; 1353 int retval; 1354 1355 retval = ibrpp(board, &poll_byte); 1356 if (retval < 0) 1357 return retval; 1358 1359 retval = copy_to_user((void __user *)arg, &poll_byte, sizeof(poll_byte)); 1360 if (retval) 1361 return -EFAULT; 1362 1363 return 0; 1364 } 1365 1366 static int online_ioctl(struct gpib_board *board, unsigned long arg) 1367 { 1368 struct gpib_online_ioctl online_cmd; 1369 int retval; 1370 void __user *init_data = NULL; 1371 1372 board->config.init_data = NULL; 1373 1374 if (!capable(CAP_SYS_ADMIN)) 1375 return -EPERM; 1376 1377 retval = copy_from_user(&online_cmd, (void __user *)arg, sizeof(online_cmd)); 1378 if (retval) 1379 return -EFAULT; 1380 if (online_cmd.init_data_length > 0) { 1381 board->config.init_data = vmalloc(online_cmd.init_data_length); 1382 if (!board->config.init_data) 1383 return -ENOMEM; 1384 if (WARN_ON_ONCE(sizeof(init_data) > sizeof(online_cmd.init_data_ptr))) 1385 return -EFAULT; 1386 init_data = (void __user *)(unsigned long)(online_cmd.init_data_ptr); 1387 retval = copy_from_user(board->config.init_data, init_data, 1388 online_cmd.init_data_length); 1389 if (retval) { 1390 vfree(board->config.init_data); 1391 return -EFAULT; 1392 } 1393 board->config.init_data_length = online_cmd.init_data_length; 1394 } else { 1395 board->config.init_data = NULL; 1396 board->config.init_data_length = 0; 1397 } 1398 if (online_cmd.online) 1399 retval = ibonline(board); 1400 else 1401 retval = iboffline(board); 1402 if (board->config.init_data) { 1403 vfree(board->config.init_data); 1404 board->config.init_data = NULL; 1405 board->config.init_data_length = 0; 1406 } 1407 return retval; 1408 } 1409 1410 static int remote_enable_ioctl(struct gpib_board *board, unsigned long arg) 1411 { 1412 int enable; 1413 int retval; 1414 1415 retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); 1416 if (retval) 1417 return -EFAULT; 1418 1419 return ibsre(board, enable); 1420 } 1421 1422 static int take_control_ioctl(struct gpib_board *board, unsigned long arg) 1423 { 1424 int synchronous; 1425 int retval; 1426 1427 retval = copy_from_user(&synchronous, (void __user *)arg, sizeof(synchronous)); 1428 if (retval) 1429 return -EFAULT; 1430 1431 return ibcac(board, synchronous, 1); 1432 } 1433 1434 static int line_status_ioctl(struct gpib_board *board, unsigned long arg) 1435 { 1436 short lines; 1437 int retval; 1438 1439 retval = iblines(board, &lines); 1440 if (retval < 0) 1441 return retval; 1442 1443 retval = copy_to_user((void __user *)arg, &lines, sizeof(lines)); 1444 if (retval) 1445 return -EFAULT; 1446 1447 return 0; 1448 } 1449 1450 static int pad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1451 unsigned long arg) 1452 { 1453 struct gpib_pad_ioctl cmd; 1454 int retval; 1455 struct gpib_descriptor *desc; 1456 1457 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1458 if (retval) 1459 return -EFAULT; 1460 1461 desc = handle_to_descriptor(file_priv, cmd.handle); 1462 if (!desc) 1463 return -EINVAL; 1464 1465 if (desc->is_board) { 1466 retval = ibpad(board, cmd.pad); 1467 if (retval < 0) 1468 return retval; 1469 } else { 1470 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1471 desc->sad); 1472 if (retval < 0) 1473 return retval; 1474 1475 desc->pad = cmd.pad; 1476 1477 retval = increment_open_device_count(board, &board->device_list, desc->pad, 1478 desc->sad); 1479 if (retval < 0) 1480 return retval; 1481 } 1482 1483 return 0; 1484 } 1485 1486 static int sad_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1487 unsigned long arg) 1488 { 1489 struct gpib_sad_ioctl cmd; 1490 int retval; 1491 struct gpib_descriptor *desc; 1492 1493 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1494 if (retval) 1495 return -EFAULT; 1496 1497 desc = handle_to_descriptor(file_priv, cmd.handle); 1498 if (!desc) 1499 return -EINVAL; 1500 1501 if (desc->is_board) { 1502 retval = ibsad(board, cmd.sad); 1503 if (retval < 0) 1504 return retval; 1505 } else { 1506 retval = decrement_open_device_count(board, &board->device_list, desc->pad, 1507 desc->sad); 1508 if (retval < 0) 1509 return retval; 1510 1511 desc->sad = cmd.sad; 1512 1513 retval = increment_open_device_count(board, &board->device_list, desc->pad, 1514 desc->sad); 1515 if (retval < 0) 1516 return retval; 1517 } 1518 return 0; 1519 } 1520 1521 static int eos_ioctl(struct gpib_board *board, unsigned long arg) 1522 { 1523 struct gpib_eos_ioctl eos_cmd; 1524 int retval; 1525 1526 retval = copy_from_user(&eos_cmd, (void __user *)arg, sizeof(eos_cmd)); 1527 if (retval) 1528 return -EFAULT; 1529 1530 return ibeos(board, eos_cmd.eos, eos_cmd.eos_flags); 1531 } 1532 1533 static int request_service_ioctl(struct gpib_board *board, unsigned long arg) 1534 { 1535 u8 status_byte; 1536 int retval; 1537 1538 retval = copy_from_user(&status_byte, (void __user *)arg, sizeof(status_byte)); 1539 if (retval) 1540 return -EFAULT; 1541 1542 return ibrsv2(board, status_byte, status_byte & request_service_bit); 1543 } 1544 1545 static int request_service2_ioctl(struct gpib_board *board, unsigned long arg) 1546 { 1547 struct gpib_request_service2 request_service2_cmd; 1548 int retval; 1549 1550 retval = copy_from_user(&request_service2_cmd, (void __user *)arg, 1551 sizeof(struct gpib_request_service2)); 1552 if (retval) 1553 return -EFAULT; 1554 1555 return ibrsv2(board, request_service2_cmd.status_byte, 1556 request_service2_cmd.new_reason_for_service); 1557 } 1558 1559 static int iobase_ioctl(struct gpib_board_config *config, unsigned long arg) 1560 { 1561 u64 base_addr; 1562 int retval; 1563 1564 if (!capable(CAP_SYS_ADMIN)) 1565 return -EPERM; 1566 1567 retval = copy_from_user(&base_addr, (void __user *)arg, sizeof(base_addr)); 1568 if (retval) 1569 return -EFAULT; 1570 1571 if (WARN_ON_ONCE(sizeof(void *) > sizeof(base_addr))) 1572 return -EFAULT; 1573 config->ibbase = base_addr; 1574 1575 return 0; 1576 } 1577 1578 static int irq_ioctl(struct gpib_board_config *config, unsigned long arg) 1579 { 1580 unsigned int irq; 1581 int retval; 1582 1583 if (!capable(CAP_SYS_ADMIN)) 1584 return -EPERM; 1585 1586 retval = copy_from_user(&irq, (void __user *)arg, sizeof(irq)); 1587 if (retval) 1588 return -EFAULT; 1589 1590 config->ibirq = irq; 1591 1592 return 0; 1593 } 1594 1595 static int dma_ioctl(struct gpib_board_config *config, unsigned long arg) 1596 { 1597 unsigned int dma_channel; 1598 int retval; 1599 1600 if (!capable(CAP_SYS_ADMIN)) 1601 return -EPERM; 1602 1603 retval = copy_from_user(&dma_channel, (void __user *)arg, sizeof(dma_channel)); 1604 if (retval) 1605 return -EFAULT; 1606 1607 config->ibdma = dma_channel; 1608 1609 return 0; 1610 } 1611 1612 static int autospoll_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1613 unsigned long arg) 1614 { 1615 short enable; 1616 int retval; 1617 struct gpib_descriptor *desc; 1618 1619 retval = copy_from_user(&enable, (void __user *)arg, sizeof(enable)); 1620 if (retval) 1621 return -EFAULT; 1622 1623 desc = handle_to_descriptor(file_priv, 0); /* board handle is 0 */ 1624 1625 if (enable) { 1626 if (!desc->autopoll_enabled) { 1627 board->autospollers++; 1628 desc->autopoll_enabled = 1; 1629 } 1630 retval = 0; 1631 } else { 1632 if (desc->autopoll_enabled) { 1633 desc->autopoll_enabled = 0; 1634 if (board->autospollers > 0) { 1635 board->autospollers--; 1636 retval = 0; 1637 } else { 1638 dev_err(board->gpib_dev, 1639 "tried to set number of autospollers negative\n"); 1640 retval = -EINVAL; 1641 } 1642 } else { 1643 dev_err(board->gpib_dev, "autopoll disable requested before enable\n"); 1644 retval = -EINVAL; 1645 } 1646 } 1647 return retval; 1648 } 1649 1650 static int mutex_ioctl(struct gpib_board *board, struct gpib_file_private *file_priv, 1651 unsigned long arg) 1652 { 1653 int retval, lock_mutex; 1654 1655 retval = copy_from_user(&lock_mutex, (void __user *)arg, sizeof(lock_mutex)); 1656 if (retval) 1657 return -EFAULT; 1658 1659 if (lock_mutex) { 1660 retval = mutex_lock_interruptible(&board->user_mutex); 1661 if (retval) 1662 return -ERESTARTSYS; 1663 1664 spin_lock(&board->locking_pid_spinlock); 1665 board->locking_pid = current->pid; 1666 spin_unlock(&board->locking_pid_spinlock); 1667 1668 atomic_set(&file_priv->holding_mutex, 1); 1669 1670 dev_dbg(board->gpib_dev, "locked board mutex\n"); 1671 } else { 1672 spin_lock(&board->locking_pid_spinlock); 1673 if (current->pid != board->locking_pid) { 1674 dev_err(board->gpib_dev, "bug! pid %i tried to release mutex held by pid %i\n", 1675 current->pid, board->locking_pid); 1676 spin_unlock(&board->locking_pid_spinlock); 1677 return -EPERM; 1678 } 1679 board->locking_pid = 0; 1680 spin_unlock(&board->locking_pid_spinlock); 1681 1682 atomic_set(&file_priv->holding_mutex, 0); 1683 1684 mutex_unlock(&board->user_mutex); 1685 dev_dbg(board->gpib_dev, "unlocked board mutex\n"); 1686 } 1687 return 0; 1688 } 1689 1690 static int timeout_ioctl(struct gpib_board *board, unsigned long arg) 1691 { 1692 unsigned int timeout; 1693 int retval; 1694 1695 retval = copy_from_user(&timeout, (void __user *)arg, sizeof(timeout)); 1696 if (retval) 1697 return -EFAULT; 1698 1699 board->usec_timeout = timeout; 1700 dev_dbg(board->gpib_dev, "timeout set to %i usec\n", timeout); 1701 1702 return 0; 1703 } 1704 1705 static int ppc_ioctl(struct gpib_board *board, unsigned long arg) 1706 { 1707 struct gpib_ppoll_config_ioctl cmd; 1708 int retval; 1709 1710 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1711 if (retval) 1712 return -EFAULT; 1713 1714 if (cmd.set_ist) { 1715 board->ist = 1; 1716 board->interface->parallel_poll_response(board, board->ist); 1717 } else if (cmd.clear_ist) { 1718 board->ist = 0; 1719 board->interface->parallel_poll_response(board, board->ist); 1720 } 1721 1722 if (cmd.config) { 1723 retval = ibppc(board, cmd.config); 1724 if (retval < 0) 1725 return retval; 1726 } 1727 1728 return 0; 1729 } 1730 1731 static int set_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) 1732 { 1733 short cmd; 1734 int retval; 1735 1736 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 1737 if (retval) 1738 return -EFAULT; 1739 1740 if (!board->interface->local_parallel_poll_mode) 1741 return -ENOENT; 1742 board->local_ppoll_mode = cmd != 0; 1743 board->interface->local_parallel_poll_mode(board, board->local_ppoll_mode); 1744 1745 return 0; 1746 } 1747 1748 static int get_local_ppoll_mode_ioctl(struct gpib_board *board, unsigned long arg) 1749 { 1750 short cmd; 1751 int retval; 1752 1753 cmd = board->local_ppoll_mode; 1754 retval = copy_to_user((void __user *)arg, &cmd, sizeof(cmd)); 1755 if (retval) 1756 return -EFAULT; 1757 1758 return 0; 1759 } 1760 1761 static int query_board_rsv_ioctl(struct gpib_board *board, unsigned long arg) 1762 { 1763 int status; 1764 int retval; 1765 1766 status = board->interface->serial_poll_status(board); 1767 1768 retval = copy_to_user((void __user *)arg, &status, sizeof(status)); 1769 if (retval) 1770 return -EFAULT; 1771 1772 return 0; 1773 } 1774 1775 static int board_info_ioctl(const struct gpib_board *board, unsigned long arg) 1776 { 1777 struct gpib_board_info_ioctl info = { }; 1778 int retval; 1779 1780 info.pad = board->pad; 1781 info.sad = board->sad; 1782 info.parallel_poll_configuration = board->parallel_poll_configuration; 1783 info.is_system_controller = board->master; 1784 if (board->autospollers) 1785 info.autopolling = 1; 1786 else 1787 info.autopolling = 0; 1788 info.t1_delay = board->t1_nano_sec; 1789 info.ist = board->ist; 1790 info.no_7_bit_eos = board->interface->no_7_bit_eos; 1791 retval = copy_to_user((void __user *)arg, &info, sizeof(info)); 1792 if (retval) 1793 return -EFAULT; 1794 1795 return 0; 1796 } 1797 1798 static int interface_clear_ioctl(struct gpib_board *board, unsigned long arg) 1799 { 1800 unsigned int usec_duration; 1801 int retval; 1802 1803 retval = copy_from_user(&usec_duration, (void __user *)arg, sizeof(usec_duration)); 1804 if (retval) 1805 return -EFAULT; 1806 1807 return ibsic(board, usec_duration); 1808 } 1809 1810 static int select_pci_ioctl(struct gpib_board_config *config, unsigned long arg) 1811 { 1812 struct gpib_select_pci_ioctl selection; 1813 int retval; 1814 1815 if (!capable(CAP_SYS_ADMIN)) 1816 return -EPERM; 1817 1818 retval = copy_from_user(&selection, (void __user *)arg, sizeof(selection)); 1819 if (retval) 1820 return -EFAULT; 1821 1822 config->pci_bus = selection.pci_bus; 1823 config->pci_slot = selection.pci_slot; 1824 1825 return 0; 1826 } 1827 1828 static int select_device_path_ioctl(struct gpib_board_config *config, unsigned long arg) 1829 { 1830 struct gpib_select_device_path_ioctl *selection; 1831 int retval; 1832 1833 if (!capable(CAP_SYS_ADMIN)) 1834 return -EPERM; 1835 1836 selection = vmalloc(sizeof(struct gpib_select_device_path_ioctl)); 1837 if (!selection) 1838 return -ENOMEM; 1839 1840 retval = copy_from_user(selection, (void __user *)arg, 1841 sizeof(struct gpib_select_device_path_ioctl)); 1842 if (retval) { 1843 vfree(selection); 1844 return -EFAULT; 1845 } 1846 1847 selection->device_path[sizeof(selection->device_path) - 1] = '\0'; 1848 kfree(config->device_path); 1849 config->device_path = NULL; 1850 if (strlen(selection->device_path) > 0) 1851 config->device_path = kstrdup(selection->device_path, GFP_KERNEL); 1852 1853 vfree(selection); 1854 return 0; 1855 } 1856 1857 unsigned int num_gpib_events(const struct gpib_event_queue *queue) 1858 { 1859 return queue->num_events; 1860 } 1861 1862 static int push_gpib_event_nolock(struct gpib_board *board, short event_type) 1863 { 1864 struct gpib_event_queue *queue = &board->event_queue; 1865 struct list_head *head = &queue->event_head; 1866 struct gpib_event *event; 1867 static const unsigned int max_num_events = 1024; 1868 int retval; 1869 1870 if (num_gpib_events(queue) >= max_num_events) { 1871 short lost_event; 1872 1873 queue->dropped_event = 1; 1874 retval = pop_gpib_event_nolock(board, queue, &lost_event); 1875 if (retval < 0) 1876 return retval; 1877 } 1878 1879 event = kmalloc(sizeof(struct gpib_event), GFP_ATOMIC); 1880 if (!event) { 1881 queue->dropped_event = 1; 1882 dev_err(board->gpib_dev, "failed to allocate memory for event\n"); 1883 return -ENOMEM; 1884 } 1885 1886 INIT_LIST_HEAD(&event->list); 1887 event->event_type = event_type; 1888 1889 list_add_tail(&event->list, head); 1890 1891 queue->num_events++; 1892 1893 dev_dbg(board->gpib_dev, "pushed event %i, %i in queue\n", 1894 (int)event_type, num_gpib_events(queue)); 1895 1896 return 0; 1897 } 1898 1899 // push event onto back of event queue 1900 int push_gpib_event(struct gpib_board *board, short event_type) 1901 { 1902 unsigned long flags; 1903 int retval; 1904 1905 spin_lock_irqsave(&board->event_queue.lock, flags); 1906 retval = push_gpib_event_nolock(board, event_type); 1907 spin_unlock_irqrestore(&board->event_queue.lock, flags); 1908 1909 if (event_type == EVENT_DEV_TRG) 1910 board->status |= DTAS; 1911 if (event_type == EVENT_DEV_CLR) 1912 board->status |= DCAS; 1913 1914 return retval; 1915 } 1916 EXPORT_SYMBOL(push_gpib_event); 1917 1918 static int pop_gpib_event_nolock(struct gpib_board *board, 1919 struct gpib_event_queue *queue, short *event_type) 1920 { 1921 struct list_head *head = &queue->event_head; 1922 struct list_head *front = head->next; 1923 struct gpib_event *event; 1924 1925 if (num_gpib_events(queue) == 0) { 1926 *event_type = EVENT_NONE; 1927 return 0; 1928 } 1929 1930 if (front == head) 1931 return -EIO; 1932 1933 if (queue->dropped_event) { 1934 queue->dropped_event = 0; 1935 return -EPIPE; 1936 } 1937 1938 event = list_entry(front, struct gpib_event, list); 1939 *event_type = event->event_type; 1940 1941 list_del(front); 1942 kfree(event); 1943 1944 queue->num_events--; 1945 1946 dev_dbg(board->gpib_dev, "popped event %i, %i in queue\n", 1947 (int)*event_type, num_gpib_events(queue)); 1948 1949 return 0; 1950 } 1951 1952 // pop event from front of event queue 1953 int pop_gpib_event(struct gpib_board *board, struct gpib_event_queue *queue, short *event_type) 1954 { 1955 unsigned long flags; 1956 int retval; 1957 1958 spin_lock_irqsave(&queue->lock, flags); 1959 retval = pop_gpib_event_nolock(board, queue, event_type); 1960 spin_unlock_irqrestore(&queue->lock, flags); 1961 return retval; 1962 } 1963 1964 static int event_ioctl(struct gpib_board *board, unsigned long arg) 1965 { 1966 short user_event; 1967 int retval; 1968 short event; 1969 1970 retval = pop_gpib_event(board, &board->event_queue, &event); 1971 if (retval < 0) 1972 return retval; 1973 1974 user_event = event; 1975 1976 retval = copy_to_user((void __user *)arg, &user_event, sizeof(user_event)); 1977 if (retval) 1978 return -EFAULT; 1979 1980 return 0; 1981 } 1982 1983 static int request_system_control_ioctl(struct gpib_board *board, unsigned long arg) 1984 { 1985 int request_control; 1986 int retval; 1987 1988 retval = copy_from_user(&request_control, (void __user *)arg, sizeof(request_control)); 1989 if (retval) 1990 return -EFAULT; 1991 1992 return ibrsc(board, request_control); 1993 } 1994 1995 static int t1_delay_ioctl(struct gpib_board *board, unsigned long arg) 1996 { 1997 unsigned int cmd; 1998 unsigned int delay; 1999 int retval; 2000 2001 if (!board->interface->t1_delay) 2002 return -ENOENT; 2003 2004 retval = copy_from_user(&cmd, (void __user *)arg, sizeof(cmd)); 2005 if (retval) 2006 return -EFAULT; 2007 2008 delay = cmd; 2009 2010 retval = board->interface->t1_delay(board, delay); 2011 if (retval < 0) 2012 return retval; 2013 2014 board->t1_nano_sec = retval; 2015 return 0; 2016 } 2017 2018 static const struct file_operations ib_fops = { 2019 .owner = THIS_MODULE, 2020 .llseek = NULL, 2021 .unlocked_ioctl = &ibioctl, 2022 .compat_ioctl = &ibioctl, 2023 .open = &ibopen, 2024 .release = &ibclose, 2025 }; 2026 2027 struct gpib_board board_array[GPIB_MAX_NUM_BOARDS]; 2028 2029 LIST_HEAD(registered_drivers); 2030 2031 void init_gpib_descriptor(struct gpib_descriptor *desc) 2032 { 2033 desc->pad = 0; 2034 desc->sad = -1; 2035 desc->is_board = 0; 2036 desc->autopoll_enabled = 0; 2037 atomic_set(&desc->io_in_progress, 0); 2038 } 2039 2040 int gpib_register_driver(struct gpib_interface *interface, struct module *provider_module) 2041 { 2042 struct gpib_interface_list *entry; 2043 2044 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 2045 if (!entry) 2046 return -ENOMEM; 2047 2048 entry->interface = interface; 2049 entry->module = provider_module; 2050 list_add(&entry->list, ®istered_drivers); 2051 2052 return 0; 2053 } 2054 EXPORT_SYMBOL(gpib_register_driver); 2055 2056 void gpib_unregister_driver(struct gpib_interface *interface) 2057 { 2058 int i; 2059 struct list_head *list_ptr; 2060 2061 for (i = 0; i < GPIB_MAX_NUM_BOARDS; i++) { 2062 struct gpib_board *board = &board_array[i]; 2063 2064 if (board->interface == interface) { 2065 if (board->use_count > 0) 2066 pr_warn("gpib: Warning: deregistered interface %s in use\n", 2067 interface->name); 2068 iboffline(board); 2069 board->interface = NULL; 2070 } 2071 } 2072 for (list_ptr = registered_drivers.next; list_ptr != ®istered_drivers;) { 2073 struct gpib_interface_list *entry; 2074 2075 entry = list_entry(list_ptr, struct gpib_interface_list, list); 2076 list_ptr = list_ptr->next; 2077 if (entry->interface == interface) { 2078 list_del(&entry->list); 2079 kfree(entry); 2080 } 2081 } 2082 } 2083 EXPORT_SYMBOL(gpib_unregister_driver); 2084 2085 static void init_gpib_board_config(struct gpib_board_config *config) 2086 { 2087 memset(config, 0, sizeof(struct gpib_board_config)); 2088 config->pci_bus = -1; 2089 config->pci_slot = -1; 2090 } 2091 2092 void init_gpib_board(struct gpib_board *board) 2093 { 2094 board->interface = NULL; 2095 board->provider_module = NULL; 2096 board->buffer = NULL; 2097 board->buffer_length = 0; 2098 board->status = 0; 2099 init_waitqueue_head(&board->wait); 2100 mutex_init(&board->user_mutex); 2101 mutex_init(&board->big_gpib_mutex); 2102 board->locking_pid = 0; 2103 spin_lock_init(&board->locking_pid_spinlock); 2104 spin_lock_init(&board->spinlock); 2105 timer_setup(&board->timer, NULL, 0); 2106 board->dev = NULL; 2107 board->gpib_dev = NULL; 2108 init_gpib_board_config(&board->config); 2109 board->private_data = NULL; 2110 board->use_count = 0; 2111 INIT_LIST_HEAD(&board->device_list); 2112 board->pad = 0; 2113 board->sad = -1; 2114 board->usec_timeout = 3000000; 2115 board->parallel_poll_configuration = 0; 2116 board->online = 0; 2117 board->autospollers = 0; 2118 board->autospoll_task = NULL; 2119 init_event_queue(&board->event_queue); 2120 board->minor = -1; 2121 init_gpib_pseudo_irq(&board->pseudo_irq); 2122 board->master = 1; 2123 atomic_set(&board->stuck_srq, 0); 2124 board->local_ppoll_mode = 0; 2125 } 2126 2127 int gpib_allocate_board(struct gpib_board *board) 2128 { 2129 if (!board->buffer) { 2130 board->buffer_length = 0x4000; 2131 board->buffer = vmalloc(board->buffer_length); 2132 if (!board->buffer) { 2133 board->buffer_length = 0; 2134 return -ENOMEM; 2135 } 2136 } 2137 return 0; 2138 } 2139 2140 void gpib_deallocate_board(struct gpib_board *board) 2141 { 2142 short dummy; 2143 2144 if (board->buffer) { 2145 vfree(board->buffer); 2146 board->buffer = NULL; 2147 board->buffer_length = 0; 2148 } 2149 while (num_gpib_events(&board->event_queue)) 2150 pop_gpib_event(board, &board->event_queue, &dummy); 2151 } 2152 2153 static void init_board_array(struct gpib_board *board_array, unsigned int length) 2154 { 2155 int i; 2156 2157 for (i = 0; i < length; i++) { 2158 init_gpib_board(&board_array[i]); 2159 board_array[i].minor = i; 2160 } 2161 } 2162 2163 void init_gpib_status_queue(struct gpib_status_queue *device) 2164 { 2165 INIT_LIST_HEAD(&device->list); 2166 INIT_LIST_HEAD(&device->status_bytes); 2167 device->num_status_bytes = 0; 2168 device->reference_count = 0; 2169 device->dropped_byte = 0; 2170 } 2171 2172 static struct class *gpib_class; 2173 2174 static int __init gpib_common_init_module(void) 2175 { 2176 int i; 2177 2178 pr_info("GPIB core driver\n"); 2179 init_board_array(board_array, GPIB_MAX_NUM_BOARDS); 2180 if (register_chrdev(GPIB_CODE, "gpib", &ib_fops)) { 2181 pr_err("gpib: can't get major %d\n", GPIB_CODE); 2182 return -EIO; 2183 } 2184 gpib_class = class_create("gpib_common"); 2185 if (IS_ERR(gpib_class)) { 2186 pr_err("gpib: failed to create gpib class\n"); 2187 unregister_chrdev(GPIB_CODE, "gpib"); 2188 return PTR_ERR(gpib_class); 2189 } 2190 for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) 2191 board_array[i].gpib_dev = device_create(gpib_class, NULL, 2192 MKDEV(GPIB_CODE, i), NULL, "gpib%i", i); 2193 2194 return 0; 2195 } 2196 2197 static void __exit gpib_common_exit_module(void) 2198 { 2199 int i; 2200 2201 for (i = 0; i < GPIB_MAX_NUM_BOARDS; ++i) 2202 device_destroy(gpib_class, MKDEV(GPIB_CODE, i)); 2203 2204 class_destroy(gpib_class); 2205 unregister_chrdev(GPIB_CODE, "gpib"); 2206 } 2207 2208 int gpib_match_device_path(struct device *dev, const char *device_path_in) 2209 { 2210 if (device_path_in) { 2211 char *device_path; 2212 2213 device_path = kobject_get_path(&dev->kobj, GFP_KERNEL); 2214 if (!device_path) { 2215 dev_err(dev, "kobject_get_path returned NULL."); 2216 return 0; 2217 } 2218 if (strcmp(device_path_in, device_path) != 0) { 2219 kfree(device_path); 2220 return 0; 2221 } 2222 kfree(device_path); 2223 } 2224 return 1; 2225 } 2226 EXPORT_SYMBOL(gpib_match_device_path); 2227 2228 struct pci_dev *gpib_pci_get_device(const struct gpib_board_config *config, unsigned int vendor_id, 2229 unsigned int device_id, struct pci_dev *from) 2230 { 2231 struct pci_dev *pci_device = from; 2232 2233 while ((pci_device = pci_get_device(vendor_id, device_id, pci_device))) { 2234 if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) 2235 continue; 2236 if (config->pci_slot >= 0 && config->pci_slot != 2237 PCI_SLOT(pci_device->devfn)) 2238 continue; 2239 if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) 2240 continue; 2241 return pci_device; 2242 } 2243 return NULL; 2244 } 2245 EXPORT_SYMBOL(gpib_pci_get_device); 2246 2247 struct pci_dev *gpib_pci_get_subsys(const struct gpib_board_config *config, unsigned int vendor_id, 2248 unsigned int device_id, unsigned int ss_vendor, 2249 unsigned int ss_device, 2250 struct pci_dev *from) 2251 { 2252 struct pci_dev *pci_device = from; 2253 2254 while ((pci_device = pci_get_subsys(vendor_id, device_id, 2255 ss_vendor, ss_device, pci_device))) { 2256 if (config->pci_bus >= 0 && config->pci_bus != pci_device->bus->number) 2257 continue; 2258 if (config->pci_slot >= 0 && config->pci_slot != 2259 PCI_SLOT(pci_device->devfn)) 2260 continue; 2261 if (gpib_match_device_path(&pci_device->dev, config->device_path) == 0) 2262 continue; 2263 return pci_device; 2264 } 2265 return NULL; 2266 } 2267 EXPORT_SYMBOL(gpib_pci_get_subsys); 2268 2269 module_init(gpib_common_init_module); 2270 module_exit(gpib_common_exit_module); 2271 2272