2 * Copyright (c) 2001 Paul Stewart
3 * Copyright (c) 2001 Vojtech Pavlik
5 * HID char devices, giving access to raw HID device events.
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/sched/signal.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/input.h>
34 #include <linux/usb.h>
35 #include <linux/hid.h>
36 #include <linux/hiddev.h>
37 #include <linux/compat.h>
38 #include <linux/vmalloc.h>
41 #ifdef CONFIG_USB_DYNAMIC_MINORS
42 #define HIDDEV_MINOR_BASE 0
43 #define HIDDEV_MINORS 256
45 #define HIDDEV_MINOR_BASE 96
46 #define HIDDEV_MINORS 16
48 #define HIDDEV_BUFFER_SIZE 2048
51 struct hiddev_usage_ref buffer
[HIDDEV_BUFFER_SIZE
];
55 struct fasync_struct
*fasync
;
56 struct hiddev
*hiddev
;
57 struct list_head node
;
58 struct mutex thread_lock
;
62 * Find a report, given the report's type and ID. The ID can be specified
63 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
64 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
65 * given type which follows old_id.
67 static struct hid_report
*
68 hiddev_lookup_report(struct hid_device
*hid
, struct hiddev_report_info
*rinfo
)
70 unsigned int flags
= rinfo
->report_id
& ~HID_REPORT_ID_MASK
;
71 unsigned int rid
= rinfo
->report_id
& HID_REPORT_ID_MASK
;
72 struct hid_report_enum
*report_enum
;
73 struct hid_report
*report
;
74 struct list_head
*list
;
76 if (rinfo
->report_type
< HID_REPORT_TYPE_MIN
||
77 rinfo
->report_type
> HID_REPORT_TYPE_MAX
)
80 report_enum
= hid
->report_enum
+
81 (rinfo
->report_type
- HID_REPORT_TYPE_MIN
);
84 case 0: /* Nothing to do -- report_id is already set correctly */
87 case HID_REPORT_ID_FIRST
:
88 if (list_empty(&report_enum
->report_list
))
91 list
= report_enum
->report_list
.next
;
92 report
= list_entry(list
, struct hid_report
, list
);
93 rinfo
->report_id
= report
->id
;
96 case HID_REPORT_ID_NEXT
:
97 report
= report_enum
->report_id_hash
[rid
];
101 list
= report
->list
.next
;
102 if (list
== &report_enum
->report_list
)
105 report
= list_entry(list
, struct hid_report
, list
);
106 rinfo
->report_id
= report
->id
;
113 return report_enum
->report_id_hash
[rinfo
->report_id
];
117 * Perform an exhaustive search of the report table for a usage, given its
120 static struct hid_field
*
121 hiddev_lookup_usage(struct hid_device
*hid
, struct hiddev_usage_ref
*uref
)
124 struct hid_report
*report
;
125 struct hid_report_enum
*report_enum
;
126 struct hid_field
*field
;
128 if (uref
->report_type
< HID_REPORT_TYPE_MIN
||
129 uref
->report_type
> HID_REPORT_TYPE_MAX
)
132 report_enum
= hid
->report_enum
+
133 (uref
->report_type
- HID_REPORT_TYPE_MIN
);
135 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
136 for (i
= 0; i
< report
->maxfield
; i
++) {
137 field
= report
->field
[i
];
138 for (j
= 0; j
< field
->maxusage
; j
++) {
139 if (field
->usage
[j
].hid
== uref
->usage_code
) {
140 uref
->report_id
= report
->id
;
141 uref
->field_index
= i
;
142 uref
->usage_index
= j
;
152 static void hiddev_send_event(struct hid_device
*hid
,
153 struct hiddev_usage_ref
*uref
)
155 struct hiddev
*hiddev
= hid
->hiddev
;
156 struct hiddev_list
*list
;
159 spin_lock_irqsave(&hiddev
->list_lock
, flags
);
160 list_for_each_entry(list
, &hiddev
->list
, node
) {
161 if (uref
->field_index
!= HID_FIELD_INDEX_NONE
||
162 (list
->flags
& HIDDEV_FLAG_REPORT
) != 0) {
163 list
->buffer
[list
->head
] = *uref
;
164 list
->head
= (list
->head
+ 1) &
165 (HIDDEV_BUFFER_SIZE
- 1);
166 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
169 spin_unlock_irqrestore(&hiddev
->list_lock
, flags
);
171 wake_up_interruptible(&hiddev
->wait
);
175 * This is where hid.c calls into hiddev to pass an event that occurred over
178 void hiddev_hid_event(struct hid_device
*hid
, struct hid_field
*field
,
179 struct hid_usage
*usage
, __s32 value
)
181 unsigned type
= field
->report_type
;
182 struct hiddev_usage_ref uref
;
185 (type
== HID_INPUT_REPORT
) ? HID_REPORT_TYPE_INPUT
:
186 ((type
== HID_OUTPUT_REPORT
) ? HID_REPORT_TYPE_OUTPUT
:
187 ((type
== HID_FEATURE_REPORT
) ? HID_REPORT_TYPE_FEATURE
: 0));
188 uref
.report_id
= field
->report
->id
;
189 uref
.field_index
= field
->index
;
190 uref
.usage_index
= (usage
- field
->usage
);
191 uref
.usage_code
= usage
->hid
;
194 hiddev_send_event(hid
, &uref
);
196 EXPORT_SYMBOL_GPL(hiddev_hid_event
);
198 void hiddev_report_event(struct hid_device
*hid
, struct hid_report
*report
)
200 unsigned type
= report
->type
;
201 struct hiddev_usage_ref uref
;
203 memset(&uref
, 0, sizeof(uref
));
205 (type
== HID_INPUT_REPORT
) ? HID_REPORT_TYPE_INPUT
:
206 ((type
== HID_OUTPUT_REPORT
) ? HID_REPORT_TYPE_OUTPUT
:
207 ((type
== HID_FEATURE_REPORT
) ? HID_REPORT_TYPE_FEATURE
: 0));
208 uref
.report_id
= report
->id
;
209 uref
.field_index
= HID_FIELD_INDEX_NONE
;
211 hiddev_send_event(hid
, &uref
);
217 static int hiddev_fasync(int fd
, struct file
*file
, int on
)
219 struct hiddev_list
*list
= file
->private_data
;
221 return fasync_helper(fd
, file
, on
, &list
->fasync
);
228 static int hiddev_release(struct inode
* inode
, struct file
* file
)
230 struct hiddev_list
*list
= file
->private_data
;
233 spin_lock_irqsave(&list
->hiddev
->list_lock
, flags
);
234 list_del(&list
->node
);
235 spin_unlock_irqrestore(&list
->hiddev
->list_lock
, flags
);
237 mutex_lock(&list
->hiddev
->existancelock
);
238 if (!--list
->hiddev
->open
) {
239 if (list
->hiddev
->exist
) {
240 hid_hw_close(list
->hiddev
->hid
);
241 hid_hw_power(list
->hiddev
->hid
, PM_HINT_NORMAL
);
243 mutex_unlock(&list
->hiddev
->existancelock
);
250 mutex_unlock(&list
->hiddev
->existancelock
);
259 static int hiddev_open(struct inode
*inode
, struct file
*file
)
261 struct hiddev_list
*list
;
262 struct usb_interface
*intf
;
263 struct hid_device
*hid
;
264 struct hiddev
*hiddev
;
267 intf
= usbhid_find_interface(iminor(inode
));
270 hid
= usb_get_intfdata(intf
);
271 hiddev
= hid
->hiddev
;
273 if (!(list
= vzalloc(sizeof(struct hiddev_list
))))
275 mutex_init(&list
->thread_lock
);
276 list
->hiddev
= hiddev
;
277 file
->private_data
= list
;
280 * no need for locking because the USB major number
281 * is shared which usbcore guards against disconnect
283 if (list
->hiddev
->exist
) {
284 if (!list
->hiddev
->open
++) {
285 res
= hid_hw_open(hiddev
->hid
);
294 spin_lock_irq(&list
->hiddev
->list_lock
);
295 list_add_tail(&list
->node
, &hiddev
->list
);
296 spin_unlock_irq(&list
->hiddev
->list_lock
);
298 mutex_lock(&hiddev
->existancelock
);
299 if (!list
->hiddev
->open
++)
300 if (list
->hiddev
->exist
) {
301 struct hid_device
*hid
= hiddev
->hid
;
302 res
= hid_hw_power(hid
, PM_HINT_FULLON
);
305 res
= hid_hw_open(hid
);
307 goto bail_normal_power
;
309 mutex_unlock(&hiddev
->existancelock
);
312 hid_hw_power(hid
, PM_HINT_NORMAL
);
314 mutex_unlock(&hiddev
->existancelock
);
316 file
->private_data
= NULL
;
324 static ssize_t
hiddev_write(struct file
* file
, const char __user
* buffer
, size_t count
, loff_t
*ppos
)
332 static ssize_t
hiddev_read(struct file
* file
, char __user
* buffer
, size_t count
, loff_t
*ppos
)
335 struct hiddev_list
*list
= file
->private_data
;
339 event_size
= ((list
->flags
& HIDDEV_FLAG_UREF
) != 0) ?
340 sizeof(struct hiddev_usage_ref
) : sizeof(struct hiddev_event
);
342 if (count
< event_size
)
345 /* lock against other threads */
346 retval
= mutex_lock_interruptible(&list
->thread_lock
);
350 while (retval
== 0) {
351 if (list
->head
== list
->tail
) {
352 prepare_to_wait(&list
->hiddev
->wait
, &wait
, TASK_INTERRUPTIBLE
);
354 while (list
->head
== list
->tail
) {
355 if (signal_pending(current
)) {
356 retval
= -ERESTARTSYS
;
359 if (!list
->hiddev
->exist
) {
363 if (file
->f_flags
& O_NONBLOCK
) {
368 /* let O_NONBLOCK tasks run */
369 mutex_unlock(&list
->thread_lock
);
371 if (mutex_lock_interruptible(&list
->thread_lock
)) {
372 finish_wait(&list
->hiddev
->wait
, &wait
);
375 set_current_state(TASK_INTERRUPTIBLE
);
377 finish_wait(&list
->hiddev
->wait
, &wait
);
382 mutex_unlock(&list
->thread_lock
);
387 while (list
->head
!= list
->tail
&&
388 retval
+ event_size
<= count
) {
389 if ((list
->flags
& HIDDEV_FLAG_UREF
) == 0) {
390 if (list
->buffer
[list
->tail
].field_index
!= HID_FIELD_INDEX_NONE
) {
391 struct hiddev_event event
;
393 event
.hid
= list
->buffer
[list
->tail
].usage_code
;
394 event
.value
= list
->buffer
[list
->tail
].value
;
395 if (copy_to_user(buffer
+ retval
, &event
, sizeof(struct hiddev_event
))) {
396 mutex_unlock(&list
->thread_lock
);
399 retval
+= sizeof(struct hiddev_event
);
402 if (list
->buffer
[list
->tail
].field_index
!= HID_FIELD_INDEX_NONE
||
403 (list
->flags
& HIDDEV_FLAG_REPORT
) != 0) {
405 if (copy_to_user(buffer
+ retval
, list
->buffer
+ list
->tail
, sizeof(struct hiddev_usage_ref
))) {
406 mutex_unlock(&list
->thread_lock
);
409 retval
+= sizeof(struct hiddev_usage_ref
);
412 list
->tail
= (list
->tail
+ 1) & (HIDDEV_BUFFER_SIZE
- 1);
416 mutex_unlock(&list
->thread_lock
);
423 * No kernel lock - fine
425 static unsigned int hiddev_poll(struct file
*file
, poll_table
*wait
)
427 struct hiddev_list
*list
= file
->private_data
;
429 poll_wait(file
, &list
->hiddev
->wait
, wait
);
430 if (list
->head
!= list
->tail
)
431 return POLLIN
| POLLRDNORM
;
432 if (!list
->hiddev
->exist
)
433 return POLLERR
| POLLHUP
;
440 static noinline
int hiddev_ioctl_usage(struct hiddev
*hiddev
, unsigned int cmd
, void __user
*user_arg
)
442 struct hid_device
*hid
= hiddev
->hid
;
443 struct hiddev_report_info rinfo
;
444 struct hiddev_usage_ref_multi
*uref_multi
= NULL
;
445 struct hiddev_usage_ref
*uref
;
446 struct hid_report
*report
;
447 struct hid_field
*field
;
450 uref_multi
= kmalloc(sizeof(struct hiddev_usage_ref_multi
), GFP_KERNEL
);
453 uref
= &uref_multi
->uref
;
454 if (cmd
== HIDIOCGUSAGES
|| cmd
== HIDIOCSUSAGES
) {
455 if (copy_from_user(uref_multi
, user_arg
,
456 sizeof(*uref_multi
)))
459 if (copy_from_user(uref
, user_arg
, sizeof(*uref
)))
465 rinfo
.report_type
= uref
->report_type
;
466 rinfo
.report_id
= uref
->report_id
;
467 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
470 if (uref
->field_index
>= report
->maxfield
)
473 field
= report
->field
[uref
->field_index
];
474 if (uref
->usage_index
>= field
->maxusage
)
477 uref
->usage_code
= field
->usage
[uref
->usage_index
].hid
;
479 if (copy_to_user(user_arg
, uref
, sizeof(*uref
)))
485 if (cmd
!= HIDIOCGUSAGE
&&
486 cmd
!= HIDIOCGUSAGES
&&
487 uref
->report_type
== HID_REPORT_TYPE_INPUT
)
490 if (uref
->report_id
== HID_REPORT_ID_UNKNOWN
) {
491 field
= hiddev_lookup_usage(hid
, uref
);
495 rinfo
.report_type
= uref
->report_type
;
496 rinfo
.report_id
= uref
->report_id
;
497 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
500 if (uref
->field_index
>= report
->maxfield
)
503 field
= report
->field
[uref
->field_index
];
505 if (cmd
== HIDIOCGCOLLECTIONINDEX
) {
506 if (uref
->usage_index
>= field
->maxusage
)
508 } else if (uref
->usage_index
>= field
->report_count
)
512 if ((cmd
== HIDIOCGUSAGES
|| cmd
== HIDIOCSUSAGES
) &&
513 (uref_multi
->num_values
> HID_MAX_MULTI_USAGES
||
514 uref
->usage_index
+ uref_multi
->num_values
> field
->report_count
))
519 uref
->value
= field
->value
[uref
->usage_index
];
520 if (copy_to_user(user_arg
, uref
, sizeof(*uref
)))
525 field
->value
[uref
->usage_index
] = uref
->value
;
528 case HIDIOCGCOLLECTIONINDEX
:
529 i
= field
->usage
[uref
->usage_index
].collection_index
;
533 for (i
= 0; i
< uref_multi
->num_values
; i
++)
534 uref_multi
->values
[i
] =
535 field
->value
[uref
->usage_index
+ i
];
536 if (copy_to_user(user_arg
, uref_multi
,
537 sizeof(*uref_multi
)))
541 for (i
= 0; i
< uref_multi
->num_values
; i
++)
542 field
->value
[uref
->usage_index
+ i
] =
543 uref_multi
->values
[i
];
559 static noinline
int hiddev_ioctl_string(struct hiddev
*hiddev
, unsigned int cmd
, void __user
*user_arg
)
561 struct hid_device
*hid
= hiddev
->hid
;
562 struct usb_device
*dev
= hid_to_usb_dev(hid
);
566 if (get_user(idx
, (int __user
*)user_arg
))
569 if ((buf
= kmalloc(HID_STRING_SIZE
, GFP_KERNEL
)) == NULL
)
572 if ((len
= usb_string(dev
, idx
, buf
, HID_STRING_SIZE
-1)) < 0) {
577 if (copy_to_user(user_arg
+sizeof(int), buf
, len
+1)) {
587 static long hiddev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
589 struct hiddev_list
*list
= file
->private_data
;
590 struct hiddev
*hiddev
= list
->hiddev
;
591 struct hid_device
*hid
;
592 struct hiddev_collection_info cinfo
;
593 struct hiddev_report_info rinfo
;
594 struct hiddev_field_info finfo
;
595 struct hiddev_devinfo dinfo
;
596 struct hid_report
*report
;
597 struct hid_field
*field
;
598 void __user
*user_arg
= (void __user
*)arg
;
601 /* Called without BKL by compat methods so no BKL taken */
603 mutex_lock(&hiddev
->existancelock
);
604 if (!hiddev
->exist
) {
614 r
= put_user(HID_VERSION
, (int __user
*)arg
) ?
618 case HIDIOCAPPLICATION
:
619 if (arg
>= hid
->maxapplication
)
622 for (i
= 0; i
< hid
->maxcollection
; i
++)
623 if (hid
->collection
[i
].type
==
624 HID_COLLECTION_APPLICATION
&& arg
-- == 0)
627 if (i
< hid
->maxcollection
)
628 r
= hid
->collection
[i
].usage
;
633 struct usb_device
*dev
= hid_to_usb_dev(hid
);
634 struct usbhid_device
*usbhid
= hid
->driver_data
;
636 memset(&dinfo
, 0, sizeof(dinfo
));
638 dinfo
.bustype
= BUS_USB
;
639 dinfo
.busnum
= dev
->bus
->busnum
;
640 dinfo
.devnum
= dev
->devnum
;
641 dinfo
.ifnum
= usbhid
->ifnum
;
642 dinfo
.vendor
= le16_to_cpu(dev
->descriptor
.idVendor
);
643 dinfo
.product
= le16_to_cpu(dev
->descriptor
.idProduct
);
644 dinfo
.version
= le16_to_cpu(dev
->descriptor
.bcdDevice
);
645 dinfo
.num_applications
= hid
->maxapplication
;
647 r
= copy_to_user(user_arg
, &dinfo
, sizeof(dinfo
)) ?
653 r
= put_user(list
->flags
, (int __user
*)arg
) ?
661 if (get_user(newflags
, (int __user
*)arg
)) {
666 if ((newflags
& ~HIDDEV_FLAGS
) != 0 ||
667 ((newflags
& HIDDEV_FLAG_REPORT
) != 0 &&
668 (newflags
& HIDDEV_FLAG_UREF
) == 0))
671 list
->flags
= newflags
;
678 r
= hiddev_ioctl_string(hiddev
, cmd
, user_arg
);
681 case HIDIOCINITREPORT
:
682 usbhid_init_reports(hid
);
683 hiddev
->initialized
= true;
688 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
))) {
693 if (rinfo
.report_type
== HID_REPORT_TYPE_OUTPUT
)
696 report
= hiddev_lookup_report(hid
, &rinfo
);
700 hid_hw_request(hid
, report
, HID_REQ_GET_REPORT
);
707 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
))) {
712 if (rinfo
.report_type
== HID_REPORT_TYPE_INPUT
)
715 report
= hiddev_lookup_report(hid
, &rinfo
);
719 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
725 case HIDIOCGREPORTINFO
:
726 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
))) {
731 report
= hiddev_lookup_report(hid
, &rinfo
);
735 rinfo
.num_fields
= report
->maxfield
;
737 r
= copy_to_user(user_arg
, &rinfo
, sizeof(rinfo
)) ?
741 case HIDIOCGFIELDINFO
:
742 if (copy_from_user(&finfo
, user_arg
, sizeof(finfo
))) {
747 rinfo
.report_type
= finfo
.report_type
;
748 rinfo
.report_id
= finfo
.report_id
;
750 report
= hiddev_lookup_report(hid
, &rinfo
);
754 if (finfo
.field_index
>= report
->maxfield
)
757 field
= report
->field
[finfo
.field_index
];
758 memset(&finfo
, 0, sizeof(finfo
));
759 finfo
.report_type
= rinfo
.report_type
;
760 finfo
.report_id
= rinfo
.report_id
;
761 finfo
.field_index
= field
->report_count
- 1;
762 finfo
.maxusage
= field
->maxusage
;
763 finfo
.flags
= field
->flags
;
764 finfo
.physical
= field
->physical
;
765 finfo
.logical
= field
->logical
;
766 finfo
.application
= field
->application
;
767 finfo
.logical_minimum
= field
->logical_minimum
;
768 finfo
.logical_maximum
= field
->logical_maximum
;
769 finfo
.physical_minimum
= field
->physical_minimum
;
770 finfo
.physical_maximum
= field
->physical_maximum
;
771 finfo
.unit_exponent
= field
->unit_exponent
;
772 finfo
.unit
= field
->unit
;
774 r
= copy_to_user(user_arg
, &finfo
, sizeof(finfo
)) ?
784 case HIDIOCGCOLLECTIONINDEX
:
785 if (!hiddev
->initialized
) {
786 usbhid_init_reports(hid
);
787 hiddev
->initialized
= true;
789 r
= hiddev_ioctl_usage(hiddev
, cmd
, user_arg
);
792 case HIDIOCGCOLLECTIONINFO
:
793 if (copy_from_user(&cinfo
, user_arg
, sizeof(cinfo
))) {
798 if (cinfo
.index
>= hid
->maxcollection
)
801 cinfo
.type
= hid
->collection
[cinfo
.index
].type
;
802 cinfo
.usage
= hid
->collection
[cinfo
.index
].usage
;
803 cinfo
.level
= hid
->collection
[cinfo
.index
].level
;
805 r
= copy_to_user(user_arg
, &cinfo
, sizeof(cinfo
)) ?
810 if (_IOC_TYPE(cmd
) != 'H' || _IOC_DIR(cmd
) != _IOC_READ
)
813 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGNAME(0))) {
814 int len
= strlen(hid
->name
) + 1;
815 if (len
> _IOC_SIZE(cmd
))
816 len
= _IOC_SIZE(cmd
);
817 r
= copy_to_user(user_arg
, hid
->name
, len
) ?
822 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGPHYS(0))) {
823 int len
= strlen(hid
->phys
) + 1;
824 if (len
> _IOC_SIZE(cmd
))
825 len
= _IOC_SIZE(cmd
);
826 r
= copy_to_user(user_arg
, hid
->phys
, len
) ?
833 mutex_unlock(&hiddev
->existancelock
);
838 static long hiddev_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
840 return hiddev_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
844 static const struct file_operations hiddev_fops
= {
845 .owner
= THIS_MODULE
,
847 .write
= hiddev_write
,
850 .release
= hiddev_release
,
851 .unlocked_ioctl
= hiddev_ioctl
,
852 .fasync
= hiddev_fasync
,
854 .compat_ioctl
= hiddev_compat_ioctl
,
856 .llseek
= noop_llseek
,
859 static char *hiddev_devnode(struct device
*dev
, umode_t
*mode
)
861 return kasprintf(GFP_KERNEL
, "usb/%s", dev_name(dev
));
864 static struct usb_class_driver hiddev_class
= {
866 .devnode
= hiddev_devnode
,
867 .fops
= &hiddev_fops
,
868 .minor_base
= HIDDEV_MINOR_BASE
,
872 * This is where hid.c calls us to connect a hid device to the hiddev driver
874 int hiddev_connect(struct hid_device
*hid
, unsigned int force
)
876 struct hiddev
*hiddev
;
877 struct usbhid_device
*usbhid
= hid
->driver_data
;
882 for (i
= 0; i
< hid
->maxcollection
; i
++)
883 if (hid
->collection
[i
].type
==
884 HID_COLLECTION_APPLICATION
&&
885 !IS_INPUT_APPLICATION(hid
->collection
[i
].usage
))
888 if (i
== hid
->maxcollection
)
892 if (!(hiddev
= kzalloc(sizeof(struct hiddev
), GFP_KERNEL
)))
895 init_waitqueue_head(&hiddev
->wait
);
896 INIT_LIST_HEAD(&hiddev
->list
);
897 spin_lock_init(&hiddev
->list_lock
);
898 mutex_init(&hiddev
->existancelock
);
899 hid
->hiddev
= hiddev
;
902 retval
= usb_register_dev(usbhid
->intf
, &hiddev_class
);
904 hid_err(hid
, "Not able to get a minor for this device\n");
911 * If HID_QUIRK_NO_INIT_REPORTS is set, make sure we don't initialize
914 hiddev
->initialized
= hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
;
916 hiddev
->minor
= usbhid
->intf
->minor
;
922 * This is where hid.c calls us to disconnect a hiddev device from the
923 * corresponding hid device (usually because the usb device has disconnected)
925 static struct usb_class_driver hiddev_class
;
926 void hiddev_disconnect(struct hid_device
*hid
)
928 struct hiddev
*hiddev
= hid
->hiddev
;
929 struct usbhid_device
*usbhid
= hid
->driver_data
;
931 usb_deregister_dev(usbhid
->intf
, &hiddev_class
);
933 mutex_lock(&hiddev
->existancelock
);
937 mutex_unlock(&hiddev
->existancelock
);
938 hid_hw_close(hiddev
->hid
);
939 wake_up_interruptible(&hiddev
->wait
);
941 mutex_unlock(&hiddev
->existancelock
);