1/* 2 * Copyright (c) 2001 Paul Stewart 3 * Copyright (c) 2001 Vojtech Pavlik 4 * 5 * HID char devices, giving access to raw HID device events. 6 * 7 */ 8 9/* 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 * 24 * Should you need to contact me, the author, you can do so either by 25 * e-mail - mail your message to Paul Stewart <stewart@wetlogic.net> 26 */ 27 28#include <linux/poll.h> 29#include <linux/slab.h> 30#include <linux/module.h> 31#include <linux/init.h> 32#include <linux/smp_lock.h> 33#include <linux/input.h> 34#include <linux/usb.h> 35#include <linux/hid.h> 36#include <linux/hiddev.h> 37#include "usbhid.h" 38 39#ifdef CONFIG_USB_DYNAMIC_MINORS 40#define HIDDEV_MINOR_BASE 0 41#define HIDDEV_MINORS 256 42#else 43#define HIDDEV_MINOR_BASE 96 44#define HIDDEV_MINORS 16 45#endif 46#define HIDDEV_BUFFER_SIZE 64 47 48struct hiddev { 49 int exist; 50 int open; 51 wait_queue_head_t wait; 52 struct hid_device *hid; 53 struct list_head list; 54 spinlock_t list_lock; 55}; 56 57struct hiddev_list { 58 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE]; 59 int head; 60 int tail; 61 unsigned flags; 62 struct fasync_struct *fasync; 63 struct hiddev *hiddev; 64 struct list_head node; 65}; 66 67static struct hiddev *hiddev_table[HIDDEV_MINORS]; 68 69/* 70 * Find a report, given the report's type and ID. The ID can be specified 71 * indirectly by REPORT_ID_FIRST (which returns the first report of the given 72 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the 73 * given type which follows old_id. 74 */ 75static struct hid_report * 76hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo) 77{ 78 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK; 79 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK; 80 struct hid_report_enum *report_enum; 81 struct hid_report *report; 82 struct list_head *list; 83 84 if (rinfo->report_type < HID_REPORT_TYPE_MIN || 85 rinfo->report_type > HID_REPORT_TYPE_MAX) 86 return NULL; 87 88 report_enum = hid->report_enum + 89 (rinfo->report_type - HID_REPORT_TYPE_MIN); 90 91 switch (flags) { 92 case 0: /* Nothing to do -- report_id is already set correctly */ 93 break; 94 95 case HID_REPORT_ID_FIRST: 96 if (list_empty(&report_enum->report_list)) 97 return NULL; 98 99 list = report_enum->report_list.next; 100 report = list_entry(list, struct hid_report, list); 101 rinfo->report_id = report->id; 102 break; 103 104 case HID_REPORT_ID_NEXT: 105 report = report_enum->report_id_hash[rid]; 106 if (!report) 107 return NULL; 108 109 list = report->list.next; 110 if (list == &report_enum->report_list) 111 return NULL; 112 113 report = list_entry(list, struct hid_report, list); 114 rinfo->report_id = report->id; 115 break; 116 117 default: 118 return NULL; 119 } 120 121 return report_enum->report_id_hash[rinfo->report_id]; 122} 123 124/* 125 * Perform an exhaustive search of the report table for a usage, given its 126 * type and usage id. 127 */ 128static struct hid_field * 129hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref) 130{ 131 int i, j; 132 struct hid_report *report; 133 struct hid_report_enum *report_enum; 134 struct hid_field *field; 135 136 if (uref->report_type < HID_REPORT_TYPE_MIN || 137 uref->report_type > HID_REPORT_TYPE_MAX) 138 return NULL; 139 140 report_enum = hid->report_enum + 141 (uref->report_type - HID_REPORT_TYPE_MIN); 142 143 list_for_each_entry(report, &report_enum->report_list, list) { 144 for (i = 0; i < report->maxfield; i++) { 145 field = report->field[i]; 146 for (j = 0; j < field->maxusage; j++) { 147 if (field->usage[j].hid == uref->usage_code) { 148 uref->report_id = report->id; 149 uref->field_index = i; 150 uref->usage_index = j; 151 return field; 152 } 153 } 154 } 155 } 156 157 return NULL; 158} 159 160static void hiddev_send_event(struct hid_device *hid, 161 struct hiddev_usage_ref *uref) 162{ 163 struct hiddev *hiddev = hid->hiddev; 164 struct hiddev_list *list; 165 unsigned long flags; 166 167 spin_lock_irqsave(&hiddev->list_lock, flags); 168 list_for_each_entry(list, &hiddev->list, node) { 169 if (uref->field_index != HID_FIELD_INDEX_NONE || 170 (list->flags & HIDDEV_FLAG_REPORT) != 0) { 171 list->buffer[list->head] = *uref; 172 list->head = (list->head + 1) & 173 (HIDDEV_BUFFER_SIZE - 1); 174 kill_fasync(&list->fasync, SIGIO, POLL_IN); 175 } 176 } 177 spin_unlock_irqrestore(&hiddev->list_lock, flags); 178 179 wake_up_interruptible(&hiddev->wait); 180} 181 182/* 183 * This is where hid.c calls into hiddev to pass an event that occurred over 184 * the interrupt pipe 185 */ 186void hiddev_hid_event(struct hid_device *hid, struct hid_field *field, 187 struct hid_usage *usage, __s32 value) 188{ 189 unsigned type = field->report_type; 190 struct hiddev_usage_ref uref; 191 192 uref.report_type = 193 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT : 194 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : 195 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0)); 196 uref.report_id = field->report->id; 197 uref.field_index = field->index; 198 uref.usage_index = (usage - field->usage); 199 uref.usage_code = usage->hid; 200 uref.value = value; 201 202 hiddev_send_event(hid, &uref); 203} 204EXPORT_SYMBOL_GPL(hiddev_hid_event); 205 206void hiddev_report_event(struct hid_device *hid, struct hid_report *report) 207{ 208 unsigned type = report->type; 209 struct hiddev_usage_ref uref; 210 211 memset(&uref, 0, sizeof(uref)); 212 uref.report_type = 213 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT : 214 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : 215 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0)); 216 uref.report_id = report->id; 217 uref.field_index = HID_FIELD_INDEX_NONE; 218 219 hiddev_send_event(hid, &uref); 220} 221 222/* 223 * fasync file op 224 */ 225static int hiddev_fasync(int fd, struct file *file, int on) 226{ 227 int retval; 228 struct hiddev_list *list = file->private_data; 229 230 retval = fasync_helper(fd, file, on, &list->fasync); 231 232 return retval < 0 ? retval : 0; 233} 234 235 236/* 237 * release file op 238 */ 239static int hiddev_release(struct inode * inode, struct file * file) 240{ 241 struct hiddev_list *list = file->private_data; 242 unsigned long flags; 243 244 hiddev_fasync(-1, file, 0); 245 246 spin_lock_irqsave(&list->hiddev->list_lock, flags); 247 list_del(&list->node); 248 spin_unlock_irqrestore(&list->hiddev->list_lock, flags); 249 250 if (!--list->hiddev->open) { 251 if (list->hiddev->exist) 252 usbhid_close(list->hiddev->hid); 253 else 254 kfree(list->hiddev); 255 } 256 257 kfree(list); 258 259 return 0; 260} 261 262/* 263 * open file op 264 */ 265static int hiddev_open(struct inode *inode, struct file *file) 266{ 267 struct hiddev_list *list; 268 unsigned long flags; 269 270 int i = iminor(inode) - HIDDEV_MINOR_BASE; 271 272 if (i >= HIDDEV_MINORS || !hiddev_table[i]) 273 return -ENODEV; 274 275 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL))) 276 return -ENOMEM; 277 278 list->hiddev = hiddev_table[i]; 279 280 spin_lock_irqsave(&list->hiddev->list_lock, flags); 281 list_add_tail(&list->node, &hiddev_table[i]->list); 282 spin_unlock_irqrestore(&list->hiddev->list_lock, flags); 283 284 file->private_data = list; 285 286 if (!list->hiddev->open++) 287 if (list->hiddev->exist) 288 usbhid_open(hiddev_table[i]->hid); 289 290 return 0; 291} 292 293/* 294 * "write" file op 295 */ 296static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) 297{ 298 return -EINVAL; 299} 300 301/* 302 * "read" file op 303 */ 304static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) 305{ 306 DECLARE_WAITQUEUE(wait, current); 307 struct hiddev_list *list = file->private_data; 308 int event_size; 309 int retval = 0; 310 311 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ? 312 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event); 313 314 if (count < event_size) 315 return 0; 316 317 while (retval == 0) { 318 if (list->head == list->tail) { 319 add_wait_queue(&list->hiddev->wait, &wait); 320 set_current_state(TASK_INTERRUPTIBLE); 321 322 while (list->head == list->tail) { 323 if (file->f_flags & O_NONBLOCK) { 324 retval = -EAGAIN; 325 break; 326 } 327 if (signal_pending(current)) { 328 retval = -ERESTARTSYS; 329 break; 330 } 331 if (!list->hiddev->exist) { 332 retval = -EIO; 333 break; 334 } 335 336 schedule(); 337 set_current_state(TASK_INTERRUPTIBLE); 338 } 339 340 set_current_state(TASK_RUNNING); 341 remove_wait_queue(&list->hiddev->wait, &wait); 342 } 343 344 if (retval) 345 return retval; 346 347 348 while (list->head != list->tail && 349 retval + event_size <= count) { 350 if ((list->flags & HIDDEV_FLAG_UREF) == 0) { 351 if (list->buffer[list->tail].field_index != 352 HID_FIELD_INDEX_NONE) { 353 struct hiddev_event event; 354 event.hid = list->buffer[list->tail].usage_code; 355 event.value = list->buffer[list->tail].value; 356 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) 357 return -EFAULT; 358 retval += sizeof(struct hiddev_event); 359 } 360 } else { 361 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE || 362 (list->flags & HIDDEV_FLAG_REPORT) != 0) { 363 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) 364 return -EFAULT; 365 retval += sizeof(struct hiddev_usage_ref); 366 } 367 } 368 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1); 369 } 370 371 } 372 373 return retval; 374} 375 376/* 377 * "poll" file op 378 * No kernel lock - fine 379 */ 380static unsigned int hiddev_poll(struct file *file, poll_table *wait) 381{ 382 struct hiddev_list *list = file->private_data; 383 384 poll_wait(file, &list->hiddev->wait, wait); 385 if (list->head != list->tail) 386 return POLLIN | POLLRDNORM; 387 if (!list->hiddev->exist) 388 return POLLERR | POLLHUP; 389 return 0; 390} 391 392/* 393 * "ioctl" file op 394 */ 395static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 396{ 397 struct hiddev_list *list = file->private_data; 398 struct hiddev *hiddev = list->hiddev; 399 struct hid_device *hid = hiddev->hid; 400 struct usb_device *dev = hid_to_usb_dev(hid); 401 struct hiddev_collection_info cinfo; 402 struct hiddev_report_info rinfo; 403 struct hiddev_field_info finfo; 404 struct hiddev_usage_ref_multi *uref_multi = NULL; 405 struct hiddev_usage_ref *uref; 406 struct hiddev_devinfo dinfo; 407 struct hid_report *report; 408 struct hid_field *field; 409 struct usbhid_device *usbhid = hid->driver_data; 410 void __user *user_arg = (void __user *)arg; 411 int i; 412 413 if (!hiddev->exist) 414 return -EIO; 415 416 switch (cmd) { 417 418 case HIDIOCGVERSION: 419 return put_user(HID_VERSION, (int __user *)arg); 420 421 case HIDIOCAPPLICATION: 422 if (arg < 0 || arg >= hid->maxapplication) 423 return -EINVAL; 424 425 for (i = 0; i < hid->maxcollection; i++) 426 if (hid->collection[i].type == 427 HID_COLLECTION_APPLICATION && arg-- == 0) 428 break; 429 430 if (i == hid->maxcollection) 431 return -EINVAL; 432 433 return hid->collection[i].usage; 434 435 case HIDIOCGDEVINFO: 436 dinfo.bustype = BUS_USB; 437 dinfo.busnum = dev->bus->busnum; 438 dinfo.devnum = dev->devnum; 439 dinfo.ifnum = usbhid->ifnum; 440 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor); 441 dinfo.product = le16_to_cpu(dev->descriptor.idProduct); 442 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice); 443 dinfo.num_applications = hid->maxapplication; 444 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo))) 445 return -EFAULT; 446 447 return 0; 448 449 case HIDIOCGFLAG: 450 if (put_user(list->flags, (int __user *)arg)) 451 return -EFAULT; 452 453 return 0; 454 455 case HIDIOCSFLAG: 456 { 457 int newflags; 458 if (get_user(newflags, (int __user *)arg)) 459 return -EFAULT; 460 461 if ((newflags & ~HIDDEV_FLAGS) != 0 || 462 ((newflags & HIDDEV_FLAG_REPORT) != 0 && 463 (newflags & HIDDEV_FLAG_UREF) == 0)) 464 return -EINVAL; 465 466 list->flags = newflags; 467 468 return 0; 469 } 470 471 case HIDIOCGSTRING: 472 { 473 int idx, len; 474 char *buf; 475 476 if (get_user(idx, (int __user *)arg)) 477 return -EFAULT; 478 479 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL) 480 return -ENOMEM; 481 482 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) { 483 kfree(buf); 484 return -EINVAL; 485 } 486 487 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) { 488 kfree(buf); 489 return -EFAULT; 490 } 491 492 kfree(buf); 493 494 return len; 495 } 496 497 case HIDIOCINITREPORT: 498 usbhid_init_reports(hid); 499 500 return 0; 501 502 case HIDIOCGREPORT: 503 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) 504 return -EFAULT; 505 506 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT) 507 return -EINVAL; 508 509 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) 510 return -EINVAL; 511 512 usbhid_submit_report(hid, report, USB_DIR_IN); 513 usbhid_wait_io(hid); 514 515 return 0; 516 517 case HIDIOCSREPORT: 518 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) 519 return -EFAULT; 520 521 if (rinfo.report_type == HID_REPORT_TYPE_INPUT) 522 return -EINVAL; 523 524 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) 525 return -EINVAL; 526 527 usbhid_submit_report(hid, report, USB_DIR_OUT); 528 usbhid_wait_io(hid); 529 530 return 0; 531 532 case HIDIOCGREPORTINFO: 533 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) 534 return -EFAULT; 535 536 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) 537 return -EINVAL; 538 539 rinfo.num_fields = report->maxfield; 540 541 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo))) 542 return -EFAULT; 543 544 return 0; 545 546 case HIDIOCGFIELDINFO: 547 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) 548 return -EFAULT; 549 rinfo.report_type = finfo.report_type; 550 rinfo.report_id = finfo.report_id; 551 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) 552 return -EINVAL; 553 554 if (finfo.field_index >= report->maxfield) 555 return -EINVAL; 556 557 field = report->field[finfo.field_index]; 558 memset(&finfo, 0, sizeof(finfo)); 559 finfo.report_type = rinfo.report_type; 560 finfo.report_id = rinfo.report_id; 561 finfo.field_index = field->report_count - 1; 562 finfo.maxusage = field->maxusage; 563 finfo.flags = field->flags; 564 finfo.physical = field->physical; 565 finfo.logical = field->logical; 566 finfo.application = field->application; 567 finfo.logical_minimum = field->logical_minimum; 568 finfo.logical_maximum = field->logical_maximum; 569 finfo.physical_minimum = field->physical_minimum; 570 finfo.physical_maximum = field->physical_maximum; 571 finfo.unit_exponent = field->unit_exponent; 572 finfo.unit = field->unit; 573 574 if (copy_to_user(user_arg, &finfo, sizeof(finfo))) 575 return -EFAULT; 576 577 return 0; 578 579 case HIDIOCGUCODE: 580 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL); 581 if (!uref_multi) 582 return -ENOMEM; 583 uref = &uref_multi->uref; 584 if (copy_from_user(uref, user_arg, sizeof(*uref))) 585 goto fault; 586 587 rinfo.report_type = uref->report_type; 588 rinfo.report_id = uref->report_id; 589 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) 590 goto inval; 591 592 if (uref->field_index >= report->maxfield) 593 goto inval; 594 595 field = report->field[uref->field_index]; 596 if (uref->usage_index >= field->maxusage) 597 goto inval; 598 599 uref->usage_code = field->usage[uref->usage_index].hid; 600 601 if (copy_to_user(user_arg, uref, sizeof(*uref))) 602 goto fault; 603 604 kfree(uref_multi); 605 return 0; 606 607 case HIDIOCGUSAGE: 608 case HIDIOCSUSAGE: 609 case HIDIOCGUSAGES: 610 case HIDIOCSUSAGES: 611 case HIDIOCGCOLLECTIONINDEX: 612 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL); 613 if (!uref_multi) 614 return -ENOMEM; 615 uref = &uref_multi->uref; 616 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) { 617 if (copy_from_user(uref_multi, user_arg, 618 sizeof(*uref_multi))) 619 goto fault; 620 } else { 621 if (copy_from_user(uref, user_arg, sizeof(*uref))) 622 goto fault; 623 } 624 625 if (cmd != HIDIOCGUSAGE && 626 cmd != HIDIOCGUSAGES && 627 uref->report_type == HID_REPORT_TYPE_INPUT) 628 goto inval; 629 630 if (uref->report_id == HID_REPORT_ID_UNKNOWN) { 631 field = hiddev_lookup_usage(hid, uref); 632 if (field == NULL) 633 goto inval; 634 } else { 635 rinfo.report_type = uref->report_type; 636 rinfo.report_id = uref->report_id; 637 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) 638 goto inval; 639 640 if (uref->field_index >= report->maxfield) 641 goto inval; 642 643 field = report->field[uref->field_index]; 644 645 if (cmd == HIDIOCGCOLLECTIONINDEX) { 646 if (uref->usage_index >= field->maxusage) 647 goto inval; 648 } else if (uref->usage_index >= field->report_count) 649 goto inval; 650 651 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) && 652 (uref_multi->num_values > HID_MAX_MULTI_USAGES || 653 uref->usage_index + uref_multi->num_values > field->report_count)) 654 goto inval; 655 } 656 657 switch (cmd) { 658 case HIDIOCGUSAGE: 659 uref->value = field->value[uref->usage_index]; 660 if (copy_to_user(user_arg, uref, sizeof(*uref))) 661 goto fault; 662 goto goodreturn; 663 664 case HIDIOCSUSAGE: 665 field->value[uref->usage_index] = uref->value; 666 goto goodreturn; 667 668 case HIDIOCGCOLLECTIONINDEX: 669 kfree(uref_multi); 670 return field->usage[uref->usage_index].collection_index; 671 case HIDIOCGUSAGES: 672 for (i = 0; i < uref_multi->num_values; i++) 673 uref_multi->values[i] = 674 field->value[uref->usage_index + i]; 675 if (copy_to_user(user_arg, uref_multi, 676 sizeof(*uref_multi))) 677 goto fault; 678 goto goodreturn; 679 case HIDIOCSUSAGES: 680 for (i = 0; i < uref_multi->num_values; i++) 681 field->value[uref->usage_index + i] = 682 uref_multi->values[i]; 683 goto goodreturn; 684 } 685 686goodreturn: 687 kfree(uref_multi); 688 return 0; 689fault: 690 kfree(uref_multi); 691 return -EFAULT; 692inval: 693 kfree(uref_multi); 694 return -EINVAL; 695 696 case HIDIOCGCOLLECTIONINFO: 697 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) 698 return -EFAULT; 699 700 if (cinfo.index >= hid->maxcollection) 701 return -EINVAL; 702 703 cinfo.type = hid->collection[cinfo.index].type; 704 cinfo.usage = hid->collection[cinfo.index].usage; 705 cinfo.level = hid->collection[cinfo.index].level; 706 707 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo))) 708 return -EFAULT; 709 return 0; 710 711 default: 712 713 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) 714 return -EINVAL; 715 716 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) { 717 int len; 718 if (!hid->name) 719 return 0; 720 len = strlen(hid->name) + 1; 721 if (len > _IOC_SIZE(cmd)) 722 len = _IOC_SIZE(cmd); 723 return copy_to_user(user_arg, hid->name, len) ? 724 -EFAULT : len; 725 } 726 727 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) { 728 int len; 729 if (!hid->phys) 730 return 0; 731 len = strlen(hid->phys) + 1; 732 if (len > _IOC_SIZE(cmd)) 733 len = _IOC_SIZE(cmd); 734 return copy_to_user(user_arg, hid->phys, len) ? 735 -EFAULT : len; 736 } 737 } 738 return -EINVAL; 739} 740 741static const struct file_operations hiddev_fops = { 742 .owner = THIS_MODULE, 743 .read = hiddev_read, 744 .write = hiddev_write, 745 .poll = hiddev_poll, 746 .open = hiddev_open, 747 .release = hiddev_release, 748 .ioctl = hiddev_ioctl, 749 .fasync = hiddev_fasync, 750}; 751 752static struct usb_class_driver hiddev_class = { 753 .name = "hiddev%d", 754 .fops = &hiddev_fops, 755 .minor_base = HIDDEV_MINOR_BASE, 756}; 757 758/* 759 * This is where hid.c calls us to connect a hid device to the hiddev driver 760 */ 761int hiddev_connect(struct hid_device *hid) 762{ 763 struct hiddev *hiddev; 764 struct usbhid_device *usbhid = hid->driver_data; 765 int i; 766 int retval; 767 768 for (i = 0; i < hid->maxcollection; i++) 769 if (hid->collection[i].type == 770 HID_COLLECTION_APPLICATION && 771 !IS_INPUT_APPLICATION(hid->collection[i].usage)) 772 break; 773 774 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0) 775 return -1; 776 777 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL))) 778 return -1; 779 780 retval = usb_register_dev(usbhid->intf, &hiddev_class); 781 if (retval) { 782 err("Not able to get a minor for this device."); 783 kfree(hiddev); 784 return -1; 785 } 786 787 init_waitqueue_head(&hiddev->wait); 788 INIT_LIST_HEAD(&hiddev->list); 789 spin_lock_init(&hiddev->list_lock); 790 hiddev->hid = hid; 791 hiddev->exist = 1; 792 793 hid->minor = usbhid->intf->minor; 794 hid->hiddev = hiddev; 795 796 hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev; 797 798 return 0; 799} 800 801/* 802 * This is where hid.c calls us to disconnect a hiddev device from the 803 * corresponding hid device (usually because the usb device has disconnected) 804 */ 805static struct usb_class_driver hiddev_class; 806void hiddev_disconnect(struct hid_device *hid) 807{ 808 struct hiddev *hiddev = hid->hiddev; 809 struct usbhid_device *usbhid = hid->driver_data; 810 811 hiddev->exist = 0; 812 813 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL; 814 usb_deregister_dev(usbhid->intf, &hiddev_class); 815 816 if (hiddev->open) { 817 usbhid_close(hiddev->hid); 818 wake_up_interruptible(&hiddev->wait); 819 } else { 820 kfree(hiddev); 821 } 822} 823 824/* Currently this driver is a USB driver. It's not a conventional one in 825 * the sense that it doesn't probe at the USB level. Instead it waits to 826 * be connected by HID through the hiddev_connect / hiddev_disconnect 827 * routines. The reason to register as a USB device is to gain part of the 828 * minor number space from the USB major. 829 * 830 * In theory, should the HID code be generalized to more than one physical 831 * medium (say, IEEE 1384), this driver will probably need to register its 832 * own major number, and in doing so, no longer need to register with USB. 833 * At that point the probe routine and hiddev_driver struct below will no 834 * longer be useful. 835 */ 836 837 838/* We never attach in this manner, and rely on HID to connect us. This 839 * is why there is no disconnect routine defined in the usb_driver either. 840 */ 841static int hiddev_usbd_probe(struct usb_interface *intf, 842 const struct usb_device_id *hiddev_info) 843{ 844 return -ENODEV; 845} 846 847 848static /* const */ struct usb_driver hiddev_driver = { 849 .name = "hiddev", 850 .probe = hiddev_usbd_probe, 851}; 852 853int __init hiddev_init(void) 854{ 855 return usb_register(&hiddev_driver); 856} 857 858void hiddev_exit(void) 859{ 860 usb_deregister(&hiddev_driver); 861} 862