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>
39 #include <linux/nospec.h>
42 #ifdef CONFIG_USB_DYNAMIC_MINORS
43 #define HIDDEV_MINOR_BASE 0
44 #define HIDDEV_MINORS 256
46 #define HIDDEV_MINOR_BASE 96
47 #define HIDDEV_MINORS 16
49 #define HIDDEV_BUFFER_SIZE 2048
52 struct hiddev_usage_ref buffer
[HIDDEV_BUFFER_SIZE
];
56 struct fasync_struct
*fasync
;
57 struct hiddev
*hiddev
;
58 struct list_head node
;
59 struct mutex thread_lock
;
63 * Find a report, given the report's type and ID. The ID can be specified
64 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
65 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
66 * given type which follows old_id.
68 static struct hid_report
*
69 hiddev_lookup_report(struct hid_device
*hid
, struct hiddev_report_info
*rinfo
)
71 unsigned int flags
= rinfo
->report_id
& ~HID_REPORT_ID_MASK
;
72 unsigned int rid
= rinfo
->report_id
& HID_REPORT_ID_MASK
;
73 struct hid_report_enum
*report_enum
;
74 struct hid_report
*report
;
75 struct list_head
*list
;
77 if (rinfo
->report_type
< HID_REPORT_TYPE_MIN
||
78 rinfo
->report_type
> HID_REPORT_TYPE_MAX
)
81 report_enum
= hid
->report_enum
+
82 (rinfo
->report_type
- HID_REPORT_TYPE_MIN
);
85 case 0: /* Nothing to do -- report_id is already set correctly */
88 case HID_REPORT_ID_FIRST
:
89 if (list_empty(&report_enum
->report_list
))
92 list
= report_enum
->report_list
.next
;
93 report
= list_entry(list
, struct hid_report
, list
);
94 rinfo
->report_id
= report
->id
;
97 case HID_REPORT_ID_NEXT
:
98 report
= report_enum
->report_id_hash
[rid
];
102 list
= report
->list
.next
;
103 if (list
== &report_enum
->report_list
)
106 report
= list_entry(list
, struct hid_report
, list
);
107 rinfo
->report_id
= report
->id
;
114 return report_enum
->report_id_hash
[rinfo
->report_id
];
118 * Perform an exhaustive search of the report table for a usage, given its
121 static struct hid_field
*
122 hiddev_lookup_usage(struct hid_device
*hid
, struct hiddev_usage_ref
*uref
)
125 struct hid_report
*report
;
126 struct hid_report_enum
*report_enum
;
127 struct hid_field
*field
;
129 if (uref
->report_type
< HID_REPORT_TYPE_MIN
||
130 uref
->report_type
> HID_REPORT_TYPE_MAX
)
133 report_enum
= hid
->report_enum
+
134 (uref
->report_type
- HID_REPORT_TYPE_MIN
);
136 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
137 for (i
= 0; i
< report
->maxfield
; i
++) {
138 field
= report
->field
[i
];
139 for (j
= 0; j
< field
->maxusage
; j
++) {
140 if (field
->usage
[j
].hid
== uref
->usage_code
) {
141 uref
->report_id
= report
->id
;
142 uref
->field_index
= i
;
143 uref
->usage_index
= j
;
153 static void hiddev_send_event(struct hid_device
*hid
,
154 struct hiddev_usage_ref
*uref
)
156 struct hiddev
*hiddev
= hid
->hiddev
;
157 struct hiddev_list
*list
;
160 spin_lock_irqsave(&hiddev
->list_lock
, flags
);
161 list_for_each_entry(list
, &hiddev
->list
, node
) {
162 if (uref
->field_index
!= HID_FIELD_INDEX_NONE
||
163 (list
->flags
& HIDDEV_FLAG_REPORT
) != 0) {
164 list
->buffer
[list
->head
] = *uref
;
165 list
->head
= (list
->head
+ 1) &
166 (HIDDEV_BUFFER_SIZE
- 1);
167 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
170 spin_unlock_irqrestore(&hiddev
->list_lock
, flags
);
172 wake_up_interruptible(&hiddev
->wait
);
176 * This is where hid.c calls into hiddev to pass an event that occurred over
179 void hiddev_hid_event(struct hid_device
*hid
, struct hid_field
*field
,
180 struct hid_usage
*usage
, __s32 value
)
182 unsigned type
= field
->report_type
;
183 struct hiddev_usage_ref uref
;
186 (type
== HID_INPUT_REPORT
) ? HID_REPORT_TYPE_INPUT
:
187 ((type
== HID_OUTPUT_REPORT
) ? HID_REPORT_TYPE_OUTPUT
:
188 ((type
== HID_FEATURE_REPORT
) ? HID_REPORT_TYPE_FEATURE
: 0));
189 uref
.report_id
= field
->report
->id
;
190 uref
.field_index
= field
->index
;
191 uref
.usage_index
= (usage
- field
->usage
);
192 uref
.usage_code
= usage
->hid
;
195 hiddev_send_event(hid
, &uref
);
197 EXPORT_SYMBOL_GPL(hiddev_hid_event
);
199 void hiddev_report_event(struct hid_device
*hid
, struct hid_report
*report
)
201 unsigned type
= report
->type
;
202 struct hiddev_usage_ref uref
;
204 memset(&uref
, 0, sizeof(uref
));
206 (type
== HID_INPUT_REPORT
) ? HID_REPORT_TYPE_INPUT
:
207 ((type
== HID_OUTPUT_REPORT
) ? HID_REPORT_TYPE_OUTPUT
:
208 ((type
== HID_FEATURE_REPORT
) ? HID_REPORT_TYPE_FEATURE
: 0));
209 uref
.report_id
= report
->id
;
210 uref
.field_index
= HID_FIELD_INDEX_NONE
;
212 hiddev_send_event(hid
, &uref
);
218 static int hiddev_fasync(int fd
, struct file
*file
, int on
)
220 struct hiddev_list
*list
= file
->private_data
;
222 return fasync_helper(fd
, file
, on
, &list
->fasync
);
229 static int hiddev_release(struct inode
* inode
, struct file
* file
)
231 struct hiddev_list
*list
= file
->private_data
;
234 spin_lock_irqsave(&list
->hiddev
->list_lock
, flags
);
235 list_del(&list
->node
);
236 spin_unlock_irqrestore(&list
->hiddev
->list_lock
, flags
);
238 mutex_lock(&list
->hiddev
->existancelock
);
239 if (!--list
->hiddev
->open
) {
240 if (list
->hiddev
->exist
) {
241 hid_hw_close(list
->hiddev
->hid
);
242 hid_hw_power(list
->hiddev
->hid
, PM_HINT_NORMAL
);
244 mutex_unlock(&list
->hiddev
->existancelock
);
251 mutex_unlock(&list
->hiddev
->existancelock
);
257 static int __hiddev_open(struct hiddev
*hiddev
, struct file
*file
)
259 struct hiddev_list
*list
;
262 lockdep_assert_held(&hiddev
->existancelock
);
264 list
= vzalloc(sizeof(*list
));
268 mutex_init(&list
->thread_lock
);
269 list
->hiddev
= hiddev
;
271 if (!hiddev
->open
++) {
272 error
= hid_hw_power(hiddev
->hid
, PM_HINT_FULLON
);
276 error
= hid_hw_open(hiddev
->hid
);
278 goto err_normal_power
;
281 spin_lock_irq(&hiddev
->list_lock
);
282 list_add_tail(&list
->node
, &hiddev
->list
);
283 spin_unlock_irq(&hiddev
->list_lock
);
285 file
->private_data
= list
;
290 hid_hw_power(hiddev
->hid
, PM_HINT_NORMAL
);
300 static int hiddev_open(struct inode
*inode
, struct file
*file
)
302 struct usb_interface
*intf
;
303 struct hid_device
*hid
;
304 struct hiddev
*hiddev
;
307 intf
= usbhid_find_interface(iminor(inode
));
311 hid
= usb_get_intfdata(intf
);
312 hiddev
= hid
->hiddev
;
314 mutex_lock(&hiddev
->existancelock
);
315 res
= hiddev
->exist
? __hiddev_open(hiddev
, file
) : -ENODEV
;
316 mutex_unlock(&hiddev
->existancelock
);
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 __poll_t
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 EPOLLIN
| EPOLLRDNORM
;
432 if (!list
->hiddev
->exist
)
433 return EPOLLERR
| EPOLLHUP
;
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
)
472 uref
->field_index
= array_index_nospec(uref
->field_index
,
475 field
= report
->field
[uref
->field_index
];
476 if (uref
->usage_index
>= field
->maxusage
)
478 uref
->usage_index
= array_index_nospec(uref
->usage_index
,
481 uref
->usage_code
= field
->usage
[uref
->usage_index
].hid
;
483 if (copy_to_user(user_arg
, uref
, sizeof(*uref
)))
489 if (cmd
!= HIDIOCGUSAGE
&&
490 cmd
!= HIDIOCGUSAGES
&&
491 uref
->report_type
== HID_REPORT_TYPE_INPUT
)
494 if (uref
->report_id
== HID_REPORT_ID_UNKNOWN
) {
495 field
= hiddev_lookup_usage(hid
, uref
);
499 rinfo
.report_type
= uref
->report_type
;
500 rinfo
.report_id
= uref
->report_id
;
501 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
504 if (uref
->field_index
>= report
->maxfield
)
506 uref
->field_index
= array_index_nospec(uref
->field_index
,
509 field
= report
->field
[uref
->field_index
];
511 if (cmd
== HIDIOCGCOLLECTIONINDEX
) {
512 if (uref
->usage_index
>= field
->maxusage
)
515 array_index_nospec(uref
->usage_index
,
517 } else if (uref
->usage_index
>= field
->report_count
)
521 if (cmd
== HIDIOCGUSAGES
|| cmd
== HIDIOCSUSAGES
) {
522 if (uref_multi
->num_values
> HID_MAX_MULTI_USAGES
||
523 uref
->usage_index
+ uref_multi
->num_values
>
528 array_index_nospec(uref
->usage_index
,
529 field
->report_count
-
530 uref_multi
->num_values
);
535 uref
->value
= field
->value
[uref
->usage_index
];
536 if (copy_to_user(user_arg
, uref
, sizeof(*uref
)))
541 field
->value
[uref
->usage_index
] = uref
->value
;
544 case HIDIOCGCOLLECTIONINDEX
:
545 i
= field
->usage
[uref
->usage_index
].collection_index
;
549 for (i
= 0; i
< uref_multi
->num_values
; i
++)
550 uref_multi
->values
[i
] =
551 field
->value
[uref
->usage_index
+ i
];
552 if (copy_to_user(user_arg
, uref_multi
,
553 sizeof(*uref_multi
)))
557 for (i
= 0; i
< uref_multi
->num_values
; i
++)
558 field
->value
[uref
->usage_index
+ i
] =
559 uref_multi
->values
[i
];
575 static noinline
int hiddev_ioctl_string(struct hiddev
*hiddev
, unsigned int cmd
, void __user
*user_arg
)
577 struct hid_device
*hid
= hiddev
->hid
;
578 struct usb_device
*dev
= hid_to_usb_dev(hid
);
582 if (get_user(idx
, (int __user
*)user_arg
))
585 if ((buf
= kmalloc(HID_STRING_SIZE
, GFP_KERNEL
)) == NULL
)
588 if ((len
= usb_string(dev
, idx
, buf
, HID_STRING_SIZE
-1)) < 0) {
593 if (copy_to_user(user_arg
+sizeof(int), buf
, len
+1)) {
603 static long hiddev_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
605 struct hiddev_list
*list
= file
->private_data
;
606 struct hiddev
*hiddev
= list
->hiddev
;
607 struct hid_device
*hid
;
608 struct hiddev_collection_info cinfo
;
609 struct hiddev_report_info rinfo
;
610 struct hiddev_field_info finfo
;
611 struct hiddev_devinfo dinfo
;
612 struct hid_report
*report
;
613 struct hid_field
*field
;
614 void __user
*user_arg
= (void __user
*)arg
;
617 /* Called without BKL by compat methods so no BKL taken */
619 mutex_lock(&hiddev
->existancelock
);
620 if (!hiddev
->exist
) {
630 r
= put_user(HID_VERSION
, (int __user
*)arg
) ?
634 case HIDIOCAPPLICATION
:
635 if (arg
>= hid
->maxapplication
)
638 for (i
= 0; i
< hid
->maxcollection
; i
++)
639 if (hid
->collection
[i
].type
==
640 HID_COLLECTION_APPLICATION
&& arg
-- == 0)
643 if (i
< hid
->maxcollection
)
644 r
= hid
->collection
[i
].usage
;
649 struct usb_device
*dev
= hid_to_usb_dev(hid
);
650 struct usbhid_device
*usbhid
= hid
->driver_data
;
652 memset(&dinfo
, 0, sizeof(dinfo
));
654 dinfo
.bustype
= BUS_USB
;
655 dinfo
.busnum
= dev
->bus
->busnum
;
656 dinfo
.devnum
= dev
->devnum
;
657 dinfo
.ifnum
= usbhid
->ifnum
;
658 dinfo
.vendor
= le16_to_cpu(dev
->descriptor
.idVendor
);
659 dinfo
.product
= le16_to_cpu(dev
->descriptor
.idProduct
);
660 dinfo
.version
= le16_to_cpu(dev
->descriptor
.bcdDevice
);
661 dinfo
.num_applications
= hid
->maxapplication
;
663 r
= copy_to_user(user_arg
, &dinfo
, sizeof(dinfo
)) ?
669 r
= put_user(list
->flags
, (int __user
*)arg
) ?
677 if (get_user(newflags
, (int __user
*)arg
)) {
682 if ((newflags
& ~HIDDEV_FLAGS
) != 0 ||
683 ((newflags
& HIDDEV_FLAG_REPORT
) != 0 &&
684 (newflags
& HIDDEV_FLAG_UREF
) == 0))
687 list
->flags
= newflags
;
694 r
= hiddev_ioctl_string(hiddev
, cmd
, user_arg
);
697 case HIDIOCINITREPORT
:
698 usbhid_init_reports(hid
);
699 hiddev
->initialized
= true;
704 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
))) {
709 if (rinfo
.report_type
== HID_REPORT_TYPE_OUTPUT
)
712 report
= hiddev_lookup_report(hid
, &rinfo
);
716 hid_hw_request(hid
, report
, HID_REQ_GET_REPORT
);
723 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
))) {
728 if (rinfo
.report_type
== HID_REPORT_TYPE_INPUT
)
731 report
= hiddev_lookup_report(hid
, &rinfo
);
735 hid_hw_request(hid
, report
, HID_REQ_SET_REPORT
);
741 case HIDIOCGREPORTINFO
:
742 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
))) {
747 report
= hiddev_lookup_report(hid
, &rinfo
);
751 rinfo
.num_fields
= report
->maxfield
;
753 r
= copy_to_user(user_arg
, &rinfo
, sizeof(rinfo
)) ?
757 case HIDIOCGFIELDINFO
:
758 if (copy_from_user(&finfo
, user_arg
, sizeof(finfo
))) {
763 rinfo
.report_type
= finfo
.report_type
;
764 rinfo
.report_id
= finfo
.report_id
;
766 report
= hiddev_lookup_report(hid
, &rinfo
);
770 if (finfo
.field_index
>= report
->maxfield
)
772 finfo
.field_index
= array_index_nospec(finfo
.field_index
,
775 field
= report
->field
[finfo
.field_index
];
776 memset(&finfo
, 0, sizeof(finfo
));
777 finfo
.report_type
= rinfo
.report_type
;
778 finfo
.report_id
= rinfo
.report_id
;
779 finfo
.field_index
= field
->report_count
- 1;
780 finfo
.maxusage
= field
->maxusage
;
781 finfo
.flags
= field
->flags
;
782 finfo
.physical
= field
->physical
;
783 finfo
.logical
= field
->logical
;
784 finfo
.application
= field
->application
;
785 finfo
.logical_minimum
= field
->logical_minimum
;
786 finfo
.logical_maximum
= field
->logical_maximum
;
787 finfo
.physical_minimum
= field
->physical_minimum
;
788 finfo
.physical_maximum
= field
->physical_maximum
;
789 finfo
.unit_exponent
= field
->unit_exponent
;
790 finfo
.unit
= field
->unit
;
792 r
= copy_to_user(user_arg
, &finfo
, sizeof(finfo
)) ?
802 case HIDIOCGCOLLECTIONINDEX
:
803 if (!hiddev
->initialized
) {
804 usbhid_init_reports(hid
);
805 hiddev
->initialized
= true;
807 r
= hiddev_ioctl_usage(hiddev
, cmd
, user_arg
);
810 case HIDIOCGCOLLECTIONINFO
:
811 if (copy_from_user(&cinfo
, user_arg
, sizeof(cinfo
))) {
816 if (cinfo
.index
>= hid
->maxcollection
)
818 cinfo
.index
= array_index_nospec(cinfo
.index
,
821 cinfo
.type
= hid
->collection
[cinfo
.index
].type
;
822 cinfo
.usage
= hid
->collection
[cinfo
.index
].usage
;
823 cinfo
.level
= hid
->collection
[cinfo
.index
].level
;
825 r
= copy_to_user(user_arg
, &cinfo
, sizeof(cinfo
)) ?
830 if (_IOC_TYPE(cmd
) != 'H' || _IOC_DIR(cmd
) != _IOC_READ
)
833 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGNAME(0))) {
834 int len
= strlen(hid
->name
) + 1;
835 if (len
> _IOC_SIZE(cmd
))
836 len
= _IOC_SIZE(cmd
);
837 r
= copy_to_user(user_arg
, hid
->name
, len
) ?
842 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGPHYS(0))) {
843 int len
= strlen(hid
->phys
) + 1;
844 if (len
> _IOC_SIZE(cmd
))
845 len
= _IOC_SIZE(cmd
);
846 r
= copy_to_user(user_arg
, hid
->phys
, len
) ?
853 mutex_unlock(&hiddev
->existancelock
);
858 static long hiddev_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
860 return hiddev_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
864 static const struct file_operations hiddev_fops
= {
865 .owner
= THIS_MODULE
,
867 .write
= hiddev_write
,
870 .release
= hiddev_release
,
871 .unlocked_ioctl
= hiddev_ioctl
,
872 .fasync
= hiddev_fasync
,
874 .compat_ioctl
= hiddev_compat_ioctl
,
876 .llseek
= noop_llseek
,
879 static char *hiddev_devnode(struct device
*dev
, umode_t
*mode
)
881 return kasprintf(GFP_KERNEL
, "usb/%s", dev_name(dev
));
884 static struct usb_class_driver hiddev_class
= {
886 .devnode
= hiddev_devnode
,
887 .fops
= &hiddev_fops
,
888 .minor_base
= HIDDEV_MINOR_BASE
,
892 * This is where hid.c calls us to connect a hid device to the hiddev driver
894 int hiddev_connect(struct hid_device
*hid
, unsigned int force
)
896 struct hiddev
*hiddev
;
897 struct usbhid_device
*usbhid
= hid
->driver_data
;
902 for (i
= 0; i
< hid
->maxcollection
; i
++)
903 if (hid
->collection
[i
].type
==
904 HID_COLLECTION_APPLICATION
&&
905 !IS_INPUT_APPLICATION(hid
->collection
[i
].usage
))
908 if (i
== hid
->maxcollection
)
912 if (!(hiddev
= kzalloc(sizeof(struct hiddev
), GFP_KERNEL
)))
915 init_waitqueue_head(&hiddev
->wait
);
916 INIT_LIST_HEAD(&hiddev
->list
);
917 spin_lock_init(&hiddev
->list_lock
);
918 mutex_init(&hiddev
->existancelock
);
919 hid
->hiddev
= hiddev
;
922 retval
= usb_register_dev(usbhid
->intf
, &hiddev_class
);
924 hid_err(hid
, "Not able to get a minor for this device\n");
931 * If HID_QUIRK_NO_INIT_REPORTS is set, make sure we don't initialize
934 hiddev
->initialized
= hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
;
936 hiddev
->minor
= usbhid
->intf
->minor
;
942 * This is where hid.c calls us to disconnect a hiddev device from the
943 * corresponding hid device (usually because the usb device has disconnected)
945 static struct usb_class_driver hiddev_class
;
946 void hiddev_disconnect(struct hid_device
*hid
)
948 struct hiddev
*hiddev
= hid
->hiddev
;
949 struct usbhid_device
*usbhid
= hid
->driver_data
;
951 usb_deregister_dev(usbhid
->intf
, &hiddev_class
);
953 mutex_lock(&hiddev
->existancelock
);
957 hid_hw_close(hiddev
->hid
);
958 wake_up_interruptible(&hiddev
->wait
);
959 mutex_unlock(&hiddev
->existancelock
);
961 mutex_unlock(&hiddev
->existancelock
);