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