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