BKL: remove extraneous #include <smp_lock.h>
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / hid / usbhid / hiddev.c
blob984feb351a5a3205de716fdb68ad9e5f983bdadd
1 /*
2 * Copyright (c) 2001 Paul Stewart
3 * Copyright (c) 2001 Vojtech Pavlik
5 * HID char devices, giving access to raw HID device events.
7 */
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.
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.
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
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>
28 #include <linux/poll.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/input.h>
33 #include <linux/usb.h>
34 #include <linux/hid.h>
35 #include <linux/hiddev.h>
36 #include <linux/compat.h>
37 #include "usbhid.h"
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 2048
48 struct hiddev {
49 int exist;
50 int open;
51 struct mutex existancelock;
52 wait_queue_head_t wait;
53 struct hid_device *hid;
54 struct list_head list;
55 spinlock_t list_lock;
58 struct hiddev_list {
59 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
60 int head;
61 int tail;
62 unsigned flags;
63 struct fasync_struct *fasync;
64 struct hiddev *hiddev;
65 struct list_head node;
66 struct mutex thread_lock;
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.
75 static struct hid_report *
76 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
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;
84 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
85 rinfo->report_type > HID_REPORT_TYPE_MAX)
86 return NULL;
88 report_enum = hid->report_enum +
89 (rinfo->report_type - HID_REPORT_TYPE_MIN);
91 switch (flags) {
92 case 0: /* Nothing to do -- report_id is already set correctly */
93 break;
95 case HID_REPORT_ID_FIRST:
96 if (list_empty(&report_enum->report_list))
97 return NULL;
99 list = report_enum->report_list.next;
100 report = list_entry(list, struct hid_report, list);
101 rinfo->report_id = report->id;
102 break;
104 case HID_REPORT_ID_NEXT:
105 report = report_enum->report_id_hash[rid];
106 if (!report)
107 return NULL;
109 list = report->list.next;
110 if (list == &report_enum->report_list)
111 return NULL;
113 report = list_entry(list, struct hid_report, list);
114 rinfo->report_id = report->id;
115 break;
117 default:
118 return NULL;
121 return report_enum->report_id_hash[rinfo->report_id];
125 * Perform an exhaustive search of the report table for a usage, given its
126 * type and usage id.
128 static struct hid_field *
129 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
131 int i, j;
132 struct hid_report *report;
133 struct hid_report_enum *report_enum;
134 struct hid_field *field;
136 if (uref->report_type < HID_REPORT_TYPE_MIN ||
137 uref->report_type > HID_REPORT_TYPE_MAX)
138 return NULL;
140 report_enum = hid->report_enum +
141 (uref->report_type - HID_REPORT_TYPE_MIN);
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;
157 return NULL;
160 static void hiddev_send_event(struct hid_device *hid,
161 struct hiddev_usage_ref *uref)
163 struct hiddev *hiddev = hid->hiddev;
164 struct hiddev_list *list;
165 unsigned long flags;
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);
177 spin_unlock_irqrestore(&hiddev->list_lock, flags);
179 wake_up_interruptible(&hiddev->wait);
183 * This is where hid.c calls into hiddev to pass an event that occurred over
184 * the interrupt pipe
186 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
187 struct hid_usage *usage, __s32 value)
189 unsigned type = field->report_type;
190 struct hiddev_usage_ref uref;
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;
202 hiddev_send_event(hid, &uref);
204 EXPORT_SYMBOL_GPL(hiddev_hid_event);
206 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
208 unsigned type = report->type;
209 struct hiddev_usage_ref uref;
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;
219 hiddev_send_event(hid, &uref);
223 * fasync file op
225 static int hiddev_fasync(int fd, struct file *file, int on)
227 struct hiddev_list *list = file->private_data;
229 return fasync_helper(fd, file, on, &list->fasync);
234 * release file op
236 static int hiddev_release(struct inode * inode, struct file * file)
238 struct hiddev_list *list = file->private_data;
239 unsigned long flags;
241 spin_lock_irqsave(&list->hiddev->list_lock, flags);
242 list_del(&list->node);
243 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
245 if (!--list->hiddev->open) {
246 if (list->hiddev->exist) {
247 usbhid_close(list->hiddev->hid);
248 usbhid_put_power(list->hiddev->hid);
249 } else {
250 kfree(list->hiddev);
254 kfree(list);
256 return 0;
260 * open file op
262 static int hiddev_open(struct inode *inode, struct file *file)
264 struct hiddev_list *list;
265 struct usb_interface *intf;
266 struct hid_device *hid;
267 struct hiddev *hiddev;
268 int res;
270 intf = usbhid_find_interface(iminor(inode));
271 if (!intf)
272 return -ENODEV;
273 hid = usb_get_intfdata(intf);
274 hiddev = hid->hiddev;
276 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
277 return -ENOMEM;
278 mutex_init(&list->thread_lock);
279 list->hiddev = hiddev;
280 file->private_data = list;
283 * no need for locking because the USB major number
284 * is shared which usbcore guards against disconnect
286 if (list->hiddev->exist) {
287 if (!list->hiddev->open++) {
288 res = usbhid_open(hiddev->hid);
289 if (res < 0) {
290 res = -EIO;
291 goto bail;
294 } else {
295 res = -ENODEV;
296 goto bail;
299 spin_lock_irq(&list->hiddev->list_lock);
300 list_add_tail(&list->node, &hiddev->list);
301 spin_unlock_irq(&list->hiddev->list_lock);
303 if (!list->hiddev->open++)
304 if (list->hiddev->exist) {
305 struct hid_device *hid = hiddev->hid;
306 res = usbhid_get_power(hid);
307 if (res < 0) {
308 res = -EIO;
309 goto bail;
311 usbhid_open(hid);
313 return 0;
314 bail:
315 file->private_data = NULL;
316 kfree(list);
317 return res;
321 * "write" file op
323 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
325 return -EINVAL;
329 * "read" file op
331 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
333 DEFINE_WAIT(wait);
334 struct hiddev_list *list = file->private_data;
335 int event_size;
336 int retval;
338 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
339 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
341 if (count < event_size)
342 return 0;
344 /* lock against other threads */
345 retval = mutex_lock_interruptible(&list->thread_lock);
346 if (retval)
347 return -ERESTARTSYS;
349 while (retval == 0) {
350 if (list->head == list->tail) {
351 prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
353 while (list->head == list->tail) {
354 if (file->f_flags & O_NONBLOCK) {
355 retval = -EAGAIN;
356 break;
358 if (signal_pending(current)) {
359 retval = -ERESTARTSYS;
360 break;
362 if (!list->hiddev->exist) {
363 retval = -EIO;
364 break;
367 /* let O_NONBLOCK tasks run */
368 mutex_unlock(&list->thread_lock);
369 schedule();
370 if (mutex_lock_interruptible(&list->thread_lock))
371 return -EINTR;
372 set_current_state(TASK_INTERRUPTIBLE);
374 finish_wait(&list->hiddev->wait, &wait);
378 if (retval) {
379 mutex_unlock(&list->thread_lock);
380 return retval;
384 while (list->head != list->tail &&
385 retval + event_size <= count) {
386 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
387 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
388 struct hiddev_event event;
390 event.hid = list->buffer[list->tail].usage_code;
391 event.value = list->buffer[list->tail].value;
392 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
393 mutex_unlock(&list->thread_lock);
394 return -EFAULT;
396 retval += sizeof(struct hiddev_event);
398 } else {
399 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
400 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
402 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
403 mutex_unlock(&list->thread_lock);
404 return -EFAULT;
406 retval += sizeof(struct hiddev_usage_ref);
409 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
413 mutex_unlock(&list->thread_lock);
415 return retval;
419 * "poll" file op
420 * No kernel lock - fine
422 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
424 struct hiddev_list *list = file->private_data;
426 poll_wait(file, &list->hiddev->wait, wait);
427 if (list->head != list->tail)
428 return POLLIN | POLLRDNORM;
429 if (!list->hiddev->exist)
430 return POLLERR | POLLHUP;
431 return 0;
435 * "ioctl" file op
437 static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
439 struct hid_device *hid = hiddev->hid;
440 struct hiddev_report_info rinfo;
441 struct hiddev_usage_ref_multi *uref_multi = NULL;
442 struct hiddev_usage_ref *uref;
443 struct hid_report *report;
444 struct hid_field *field;
445 int i;
447 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
448 if (!uref_multi)
449 return -ENOMEM;
450 uref = &uref_multi->uref;
451 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
452 if (copy_from_user(uref_multi, user_arg,
453 sizeof(*uref_multi)))
454 goto fault;
455 } else {
456 if (copy_from_user(uref, user_arg, sizeof(*uref)))
457 goto fault;
460 switch (cmd) {
461 case HIDIOCGUCODE:
462 rinfo.report_type = uref->report_type;
463 rinfo.report_id = uref->report_id;
464 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
465 goto inval;
467 if (uref->field_index >= report->maxfield)
468 goto inval;
470 field = report->field[uref->field_index];
471 if (uref->usage_index >= field->maxusage)
472 goto inval;
474 uref->usage_code = field->usage[uref->usage_index].hid;
476 if (copy_to_user(user_arg, uref, sizeof(*uref)))
477 goto fault;
479 goto goodreturn;
481 default:
482 if (cmd != HIDIOCGUSAGE &&
483 cmd != HIDIOCGUSAGES &&
484 uref->report_type == HID_REPORT_TYPE_INPUT)
485 goto inval;
487 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
488 field = hiddev_lookup_usage(hid, uref);
489 if (field == NULL)
490 goto inval;
491 } else {
492 rinfo.report_type = uref->report_type;
493 rinfo.report_id = uref->report_id;
494 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
495 goto inval;
497 if (uref->field_index >= report->maxfield)
498 goto inval;
500 field = report->field[uref->field_index];
502 if (cmd == HIDIOCGCOLLECTIONINDEX) {
503 if (uref->usage_index >= field->maxusage)
504 goto inval;
505 } else if (uref->usage_index >= field->report_count)
506 goto inval;
508 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
509 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
510 uref->usage_index + uref_multi->num_values > field->report_count))
511 goto inval;
514 switch (cmd) {
515 case HIDIOCGUSAGE:
516 uref->value = field->value[uref->usage_index];
517 if (copy_to_user(user_arg, uref, sizeof(*uref)))
518 goto fault;
519 goto goodreturn;
521 case HIDIOCSUSAGE:
522 field->value[uref->usage_index] = uref->value;
523 goto goodreturn;
525 case HIDIOCGCOLLECTIONINDEX:
526 i = field->usage[uref->usage_index].collection_index;
527 kfree(uref_multi);
528 return i;
529 case HIDIOCGUSAGES:
530 for (i = 0; i < uref_multi->num_values; i++)
531 uref_multi->values[i] =
532 field->value[uref->usage_index + i];
533 if (copy_to_user(user_arg, uref_multi,
534 sizeof(*uref_multi)))
535 goto fault;
536 goto goodreturn;
537 case HIDIOCSUSAGES:
538 for (i = 0; i < uref_multi->num_values; i++)
539 field->value[uref->usage_index + i] =
540 uref_multi->values[i];
541 goto goodreturn;
544 goodreturn:
545 kfree(uref_multi);
546 return 0;
547 fault:
548 kfree(uref_multi);
549 return -EFAULT;
550 inval:
551 kfree(uref_multi);
552 return -EINVAL;
556 static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
558 struct hid_device *hid = hiddev->hid;
559 struct usb_device *dev = hid_to_usb_dev(hid);
560 int idx, len;
561 char *buf;
563 if (get_user(idx, (int __user *)user_arg))
564 return -EFAULT;
566 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
567 return -ENOMEM;
569 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
570 kfree(buf);
571 return -EINVAL;
574 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
575 kfree(buf);
576 return -EFAULT;
579 kfree(buf);
581 return len;
584 static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
586 struct hiddev_list *list = file->private_data;
587 struct hiddev *hiddev = list->hiddev;
588 struct hid_device *hid = hiddev->hid;
589 struct usb_device *dev;
590 struct hiddev_collection_info cinfo;
591 struct hiddev_report_info rinfo;
592 struct hiddev_field_info finfo;
593 struct hiddev_devinfo dinfo;
594 struct hid_report *report;
595 struct hid_field *field;
596 struct usbhid_device *usbhid = hid->driver_data;
597 void __user *user_arg = (void __user *)arg;
598 int i, r;
600 /* Called without BKL by compat methods so no BKL taken */
602 /* FIXME: Who or what stop this racing with a disconnect ?? */
603 if (!hiddev->exist || !hid)
604 return -EIO;
606 dev = hid_to_usb_dev(hid);
608 switch (cmd) {
610 case HIDIOCGVERSION:
611 return put_user(HID_VERSION, (int __user *)arg);
613 case HIDIOCAPPLICATION:
614 if (arg < 0 || arg >= hid->maxapplication)
615 return -EINVAL;
617 for (i = 0; i < hid->maxcollection; i++)
618 if (hid->collection[i].type ==
619 HID_COLLECTION_APPLICATION && arg-- == 0)
620 break;
622 if (i == hid->maxcollection)
623 return -EINVAL;
625 return hid->collection[i].usage;
627 case HIDIOCGDEVINFO:
628 dinfo.bustype = BUS_USB;
629 dinfo.busnum = dev->bus->busnum;
630 dinfo.devnum = dev->devnum;
631 dinfo.ifnum = usbhid->ifnum;
632 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
633 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
634 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
635 dinfo.num_applications = hid->maxapplication;
636 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
637 return -EFAULT;
639 return 0;
641 case HIDIOCGFLAG:
642 if (put_user(list->flags, (int __user *)arg))
643 return -EFAULT;
645 return 0;
647 case HIDIOCSFLAG:
649 int newflags;
650 if (get_user(newflags, (int __user *)arg))
651 return -EFAULT;
653 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
654 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
655 (newflags & HIDDEV_FLAG_UREF) == 0))
656 return -EINVAL;
658 list->flags = newflags;
660 return 0;
663 case HIDIOCGSTRING:
664 mutex_lock(&hiddev->existancelock);
665 if (hiddev->exist)
666 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
667 else
668 r = -ENODEV;
669 mutex_unlock(&hiddev->existancelock);
670 return r;
672 case HIDIOCINITREPORT:
673 mutex_lock(&hiddev->existancelock);
674 if (!hiddev->exist) {
675 mutex_unlock(&hiddev->existancelock);
676 return -ENODEV;
678 usbhid_init_reports(hid);
679 mutex_unlock(&hiddev->existancelock);
681 return 0;
683 case HIDIOCGREPORT:
684 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
685 return -EFAULT;
687 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
688 return -EINVAL;
690 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
691 return -EINVAL;
693 mutex_lock(&hiddev->existancelock);
694 if (hiddev->exist) {
695 usbhid_submit_report(hid, report, USB_DIR_IN);
696 usbhid_wait_io(hid);
698 mutex_unlock(&hiddev->existancelock);
700 return 0;
702 case HIDIOCSREPORT:
703 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
704 return -EFAULT;
706 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
707 return -EINVAL;
709 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
710 return -EINVAL;
712 mutex_lock(&hiddev->existancelock);
713 if (hiddev->exist) {
714 usbhid_submit_report(hid, report, USB_DIR_OUT);
715 usbhid_wait_io(hid);
717 mutex_unlock(&hiddev->existancelock);
719 return 0;
721 case HIDIOCGREPORTINFO:
722 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
723 return -EFAULT;
725 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
726 return -EINVAL;
728 rinfo.num_fields = report->maxfield;
730 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
731 return -EFAULT;
733 return 0;
735 case HIDIOCGFIELDINFO:
736 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
737 return -EFAULT;
738 rinfo.report_type = finfo.report_type;
739 rinfo.report_id = finfo.report_id;
740 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
741 return -EINVAL;
743 if (finfo.field_index >= report->maxfield)
744 return -EINVAL;
746 field = report->field[finfo.field_index];
747 memset(&finfo, 0, sizeof(finfo));
748 finfo.report_type = rinfo.report_type;
749 finfo.report_id = rinfo.report_id;
750 finfo.field_index = field->report_count - 1;
751 finfo.maxusage = field->maxusage;
752 finfo.flags = field->flags;
753 finfo.physical = field->physical;
754 finfo.logical = field->logical;
755 finfo.application = field->application;
756 finfo.logical_minimum = field->logical_minimum;
757 finfo.logical_maximum = field->logical_maximum;
758 finfo.physical_minimum = field->physical_minimum;
759 finfo.physical_maximum = field->physical_maximum;
760 finfo.unit_exponent = field->unit_exponent;
761 finfo.unit = field->unit;
763 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
764 return -EFAULT;
766 return 0;
768 case HIDIOCGUCODE:
769 /* fall through */
770 case HIDIOCGUSAGE:
771 case HIDIOCSUSAGE:
772 case HIDIOCGUSAGES:
773 case HIDIOCSUSAGES:
774 case HIDIOCGCOLLECTIONINDEX:
775 mutex_lock(&hiddev->existancelock);
776 if (hiddev->exist)
777 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
778 else
779 r = -ENODEV;
780 mutex_unlock(&hiddev->existancelock);
781 return r;
783 case HIDIOCGCOLLECTIONINFO:
784 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
785 return -EFAULT;
787 if (cinfo.index >= hid->maxcollection)
788 return -EINVAL;
790 cinfo.type = hid->collection[cinfo.index].type;
791 cinfo.usage = hid->collection[cinfo.index].usage;
792 cinfo.level = hid->collection[cinfo.index].level;
794 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
795 return -EFAULT;
796 return 0;
798 default:
800 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
801 return -EINVAL;
803 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
804 int len;
805 if (!hid->name)
806 return 0;
807 len = strlen(hid->name) + 1;
808 if (len > _IOC_SIZE(cmd))
809 len = _IOC_SIZE(cmd);
810 return copy_to_user(user_arg, hid->name, len) ?
811 -EFAULT : len;
814 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
815 int len;
816 if (!hid->phys)
817 return 0;
818 len = strlen(hid->phys) + 1;
819 if (len > _IOC_SIZE(cmd))
820 len = _IOC_SIZE(cmd);
821 return copy_to_user(user_arg, hid->phys, len) ?
822 -EFAULT : len;
825 return -EINVAL;
828 #ifdef CONFIG_COMPAT
829 static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
831 return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
833 #endif
835 static const struct file_operations hiddev_fops = {
836 .owner = THIS_MODULE,
837 .read = hiddev_read,
838 .write = hiddev_write,
839 .poll = hiddev_poll,
840 .open = hiddev_open,
841 .release = hiddev_release,
842 .unlocked_ioctl = hiddev_ioctl,
843 .fasync = hiddev_fasync,
844 #ifdef CONFIG_COMPAT
845 .compat_ioctl = hiddev_compat_ioctl,
846 #endif
847 .llseek = noop_llseek,
850 static char *hiddev_devnode(struct device *dev, mode_t *mode)
852 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
855 static struct usb_class_driver hiddev_class = {
856 .name = "hiddev%d",
857 .devnode = hiddev_devnode,
858 .fops = &hiddev_fops,
859 .minor_base = HIDDEV_MINOR_BASE,
863 * This is where hid.c calls us to connect a hid device to the hiddev driver
865 int hiddev_connect(struct hid_device *hid, unsigned int force)
867 struct hiddev *hiddev;
868 struct usbhid_device *usbhid = hid->driver_data;
869 int retval;
871 if (!force) {
872 unsigned int i;
873 for (i = 0; i < hid->maxcollection; i++)
874 if (hid->collection[i].type ==
875 HID_COLLECTION_APPLICATION &&
876 !IS_INPUT_APPLICATION(hid->collection[i].usage))
877 break;
879 if (i == hid->maxcollection)
880 return -1;
883 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
884 return -1;
886 init_waitqueue_head(&hiddev->wait);
887 INIT_LIST_HEAD(&hiddev->list);
888 spin_lock_init(&hiddev->list_lock);
889 mutex_init(&hiddev->existancelock);
890 hid->hiddev = hiddev;
891 hiddev->hid = hid;
892 hiddev->exist = 1;
893 retval = usb_register_dev(usbhid->intf, &hiddev_class);
894 if (retval) {
895 err_hid("Not able to get a minor for this device.");
896 hid->hiddev = NULL;
897 kfree(hiddev);
898 return -1;
900 return 0;
904 * This is where hid.c calls us to disconnect a hiddev device from the
905 * corresponding hid device (usually because the usb device has disconnected)
907 static struct usb_class_driver hiddev_class;
908 void hiddev_disconnect(struct hid_device *hid)
910 struct hiddev *hiddev = hid->hiddev;
911 struct usbhid_device *usbhid = hid->driver_data;
913 mutex_lock(&hiddev->existancelock);
914 hiddev->exist = 0;
915 mutex_unlock(&hiddev->existancelock);
917 usb_deregister_dev(usbhid->intf, &hiddev_class);
919 if (hiddev->open) {
920 usbhid_close(hiddev->hid);
921 wake_up_interruptible(&hiddev->wait);
922 } else {
923 kfree(hiddev);