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/module.h>
31 #include <linux/init.h>
32 #include <linux/smp_lock.h>
33 #include <linux/input.h>
34 #include <linux/usb.h>
36 #include <linux/hiddev.h>
38 #ifdef CONFIG_USB_DYNAMIC_MINORS
39 #define HIDDEV_MINOR_BASE 0
40 #define HIDDEV_MINORS 256
42 #define HIDDEV_MINOR_BASE 96
43 #define HIDDEV_MINORS 16
45 #define HIDDEV_BUFFER_SIZE 64
50 wait_queue_head_t wait
;
51 struct hid_device
*hid
;
52 struct list_head list
;
56 struct hiddev_usage_ref buffer
[HIDDEV_BUFFER_SIZE
];
60 struct fasync_struct
*fasync
;
61 struct hiddev
*hiddev
;
62 struct list_head node
;
65 static struct hiddev
*hiddev_table
[HIDDEV_MINORS
];
68 * Find a report, given the report's type and ID. The ID can be specified
69 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
70 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
71 * given type which follows old_id.
73 static struct hid_report
*
74 hiddev_lookup_report(struct hid_device
*hid
, struct hiddev_report_info
*rinfo
)
76 unsigned int flags
= rinfo
->report_id
& ~HID_REPORT_ID_MASK
;
77 unsigned int rid
= rinfo
->report_id
& HID_REPORT_ID_MASK
;
78 struct hid_report_enum
*report_enum
;
79 struct hid_report
*report
;
80 struct list_head
*list
;
82 if (rinfo
->report_type
< HID_REPORT_TYPE_MIN
||
83 rinfo
->report_type
> HID_REPORT_TYPE_MAX
)
86 report_enum
= hid
->report_enum
+
87 (rinfo
->report_type
- HID_REPORT_TYPE_MIN
);
90 case 0: /* Nothing to do -- report_id is already set correctly */
93 case HID_REPORT_ID_FIRST
:
94 if (list_empty(&report_enum
->report_list
))
97 list
= report_enum
->report_list
.next
;
98 report
= list_entry(list
, struct hid_report
, list
);
99 rinfo
->report_id
= report
->id
;
102 case HID_REPORT_ID_NEXT
:
103 report
= report_enum
->report_id_hash
[rid
];
107 list
= report
->list
.next
;
108 if (list
== &report_enum
->report_list
)
111 report
= list_entry(list
, struct hid_report
, list
);
112 rinfo
->report_id
= report
->id
;
119 return report_enum
->report_id_hash
[rinfo
->report_id
];
123 * Perform an exhaustive search of the report table for a usage, given its
126 static struct hid_field
*
127 hiddev_lookup_usage(struct hid_device
*hid
, struct hiddev_usage_ref
*uref
)
130 struct hid_report
*report
;
131 struct hid_report_enum
*report_enum
;
132 struct hid_field
*field
;
134 if (uref
->report_type
< HID_REPORT_TYPE_MIN
||
135 uref
->report_type
> HID_REPORT_TYPE_MAX
)
138 report_enum
= hid
->report_enum
+
139 (uref
->report_type
- HID_REPORT_TYPE_MIN
);
141 list_for_each_entry(report
, &report_enum
->report_list
, list
) {
142 for (i
= 0; i
< report
->maxfield
; i
++) {
143 field
= report
->field
[i
];
144 for (j
= 0; j
< field
->maxusage
; j
++) {
145 if (field
->usage
[j
].hid
== uref
->usage_code
) {
146 uref
->report_id
= report
->id
;
147 uref
->field_index
= i
;
148 uref
->usage_index
= j
;
158 static void hiddev_send_event(struct hid_device
*hid
,
159 struct hiddev_usage_ref
*uref
)
161 struct hiddev
*hiddev
= hid
->hiddev
;
162 struct hiddev_list
*list
;
164 list_for_each_entry(list
, &hiddev
->list
, node
) {
165 if (uref
->field_index
!= HID_FIELD_INDEX_NONE
||
166 (list
->flags
& HIDDEV_FLAG_REPORT
) != 0) {
167 list
->buffer
[list
->head
] = *uref
;
168 list
->head
= (list
->head
+ 1) &
169 (HIDDEV_BUFFER_SIZE
- 1);
170 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
174 wake_up_interruptible(&hiddev
->wait
);
178 * This is where hid.c calls into hiddev to pass an event that occurred over
181 void hiddev_hid_event(struct hid_device
*hid
, struct hid_field
*field
,
182 struct hid_usage
*usage
, __s32 value
)
184 unsigned type
= field
->report_type
;
185 struct hiddev_usage_ref uref
;
188 (type
== HID_INPUT_REPORT
) ? HID_REPORT_TYPE_INPUT
:
189 ((type
== HID_OUTPUT_REPORT
) ? HID_REPORT_TYPE_OUTPUT
:
190 ((type
== HID_FEATURE_REPORT
) ? HID_REPORT_TYPE_FEATURE
: 0));
191 uref
.report_id
= field
->report
->id
;
192 uref
.field_index
= field
->index
;
193 uref
.usage_index
= (usage
- field
->usage
);
194 uref
.usage_code
= usage
->hid
;
197 hiddev_send_event(hid
, &uref
);
201 void hiddev_report_event(struct hid_device
*hid
, struct hid_report
*report
)
203 unsigned type
= report
->type
;
204 struct hiddev_usage_ref uref
;
206 memset(&uref
, 0, sizeof(uref
));
208 (type
== HID_INPUT_REPORT
) ? HID_REPORT_TYPE_INPUT
:
209 ((type
== HID_OUTPUT_REPORT
) ? HID_REPORT_TYPE_OUTPUT
:
210 ((type
== HID_FEATURE_REPORT
) ? HID_REPORT_TYPE_FEATURE
: 0));
211 uref
.report_id
= report
->id
;
212 uref
.field_index
= HID_FIELD_INDEX_NONE
;
214 hiddev_send_event(hid
, &uref
);
219 static int hiddev_fasync(int fd
, struct file
*file
, int on
)
222 struct hiddev_list
*list
= file
->private_data
;
224 retval
= fasync_helper(fd
, file
, on
, &list
->fasync
);
226 return retval
< 0 ? retval
: 0;
233 static int hiddev_release(struct inode
* inode
, struct file
* file
)
235 struct hiddev_list
*list
= file
->private_data
;
237 hiddev_fasync(-1, file
, 0);
238 list_del(&list
->node
);
240 if (!--list
->hiddev
->open
) {
241 if (list
->hiddev
->exist
)
242 hid_close(list
->hiddev
->hid
);
255 static int hiddev_open(struct inode
*inode
, struct file
*file
)
257 struct hiddev_list
*list
;
259 int i
= iminor(inode
) - HIDDEV_MINOR_BASE
;
261 if (i
>= HIDDEV_MINORS
|| !hiddev_table
[i
])
264 if (!(list
= kzalloc(sizeof(struct hiddev_list
), GFP_KERNEL
)))
267 list
->hiddev
= hiddev_table
[i
];
268 list_add_tail(&list
->node
, &hiddev_table
[i
]->list
);
269 file
->private_data
= list
;
271 if (!list
->hiddev
->open
++)
272 if (list
->hiddev
->exist
)
273 hid_open(hiddev_table
[i
]->hid
);
281 static ssize_t
hiddev_write(struct file
* file
, const char __user
* buffer
, size_t count
, loff_t
*ppos
)
289 static ssize_t
hiddev_read(struct file
* file
, char __user
* buffer
, size_t count
, loff_t
*ppos
)
291 DECLARE_WAITQUEUE(wait
, current
);
292 struct hiddev_list
*list
= file
->private_data
;
296 event_size
= ((list
->flags
& HIDDEV_FLAG_UREF
) != 0) ?
297 sizeof(struct hiddev_usage_ref
) : sizeof(struct hiddev_event
);
299 if (count
< event_size
)
302 while (retval
== 0) {
303 if (list
->head
== list
->tail
) {
304 add_wait_queue(&list
->hiddev
->wait
, &wait
);
305 set_current_state(TASK_INTERRUPTIBLE
);
307 while (list
->head
== list
->tail
) {
308 if (file
->f_flags
& O_NONBLOCK
) {
312 if (signal_pending(current
)) {
313 retval
= -ERESTARTSYS
;
316 if (!list
->hiddev
->exist
) {
322 set_current_state(TASK_INTERRUPTIBLE
);
325 set_current_state(TASK_RUNNING
);
326 remove_wait_queue(&list
->hiddev
->wait
, &wait
);
333 while (list
->head
!= list
->tail
&&
334 retval
+ event_size
<= count
) {
335 if ((list
->flags
& HIDDEV_FLAG_UREF
) == 0) {
336 if (list
->buffer
[list
->tail
].field_index
!=
337 HID_FIELD_INDEX_NONE
) {
338 struct hiddev_event event
;
339 event
.hid
= list
->buffer
[list
->tail
].usage_code
;
340 event
.value
= list
->buffer
[list
->tail
].value
;
341 if (copy_to_user(buffer
+ retval
, &event
, sizeof(struct hiddev_event
)))
343 retval
+= sizeof(struct hiddev_event
);
346 if (list
->buffer
[list
->tail
].field_index
!= HID_FIELD_INDEX_NONE
||
347 (list
->flags
& HIDDEV_FLAG_REPORT
) != 0) {
348 if (copy_to_user(buffer
+ retval
, list
->buffer
+ list
->tail
, sizeof(struct hiddev_usage_ref
)))
350 retval
+= sizeof(struct hiddev_usage_ref
);
353 list
->tail
= (list
->tail
+ 1) & (HIDDEV_BUFFER_SIZE
- 1);
363 * No kernel lock - fine
365 static unsigned int hiddev_poll(struct file
*file
, poll_table
*wait
)
367 struct hiddev_list
*list
= file
->private_data
;
369 poll_wait(file
, &list
->hiddev
->wait
, wait
);
370 if (list
->head
!= list
->tail
)
371 return POLLIN
| POLLRDNORM
;
372 if (!list
->hiddev
->exist
)
373 return POLLERR
| POLLHUP
;
380 static int hiddev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
382 struct hiddev_list
*list
= file
->private_data
;
383 struct hiddev
*hiddev
= list
->hiddev
;
384 struct hid_device
*hid
= hiddev
->hid
;
385 struct usb_device
*dev
= hid
->dev
;
386 struct hiddev_collection_info cinfo
;
387 struct hiddev_report_info rinfo
;
388 struct hiddev_field_info finfo
;
389 struct hiddev_usage_ref_multi
*uref_multi
= NULL
;
390 struct hiddev_usage_ref
*uref
;
391 struct hiddev_devinfo dinfo
;
392 struct hid_report
*report
;
393 struct hid_field
*field
;
394 void __user
*user_arg
= (void __user
*)arg
;
403 return put_user(HID_VERSION
, (int __user
*)arg
);
405 case HIDIOCAPPLICATION
:
406 if (arg
< 0 || arg
>= hid
->maxapplication
)
409 for (i
= 0; i
< hid
->maxcollection
; i
++)
410 if (hid
->collection
[i
].type
==
411 HID_COLLECTION_APPLICATION
&& arg
-- == 0)
414 if (i
== hid
->maxcollection
)
417 return hid
->collection
[i
].usage
;
420 dinfo
.bustype
= BUS_USB
;
421 dinfo
.busnum
= dev
->bus
->busnum
;
422 dinfo
.devnum
= dev
->devnum
;
423 dinfo
.ifnum
= hid
->ifnum
;
424 dinfo
.vendor
= le16_to_cpu(dev
->descriptor
.idVendor
);
425 dinfo
.product
= le16_to_cpu(dev
->descriptor
.idProduct
);
426 dinfo
.version
= le16_to_cpu(dev
->descriptor
.bcdDevice
);
427 dinfo
.num_applications
= hid
->maxapplication
;
428 if (copy_to_user(user_arg
, &dinfo
, sizeof(dinfo
)))
434 if (put_user(list
->flags
, (int __user
*)arg
))
442 if (get_user(newflags
, (int __user
*)arg
))
445 if ((newflags
& ~HIDDEV_FLAGS
) != 0 ||
446 ((newflags
& HIDDEV_FLAG_REPORT
) != 0 &&
447 (newflags
& HIDDEV_FLAG_UREF
) == 0))
450 list
->flags
= newflags
;
460 if (get_user(idx
, (int __user
*)arg
))
463 if ((buf
= kmalloc(HID_STRING_SIZE
, GFP_KERNEL
)) == NULL
)
466 if ((len
= usb_string(dev
, idx
, buf
, HID_STRING_SIZE
-1)) < 0) {
471 if (copy_to_user(user_arg
+sizeof(int), buf
, len
+1)) {
481 case HIDIOCINITREPORT
:
482 hid_init_reports(hid
);
487 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
)))
490 if (rinfo
.report_type
== HID_REPORT_TYPE_OUTPUT
)
493 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
496 hid_submit_report(hid
, report
, USB_DIR_IN
);
502 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
)))
505 if (rinfo
.report_type
== HID_REPORT_TYPE_INPUT
)
508 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
511 hid_submit_report(hid
, report
, USB_DIR_OUT
);
516 case HIDIOCGREPORTINFO
:
517 if (copy_from_user(&rinfo
, user_arg
, sizeof(rinfo
)))
520 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
523 rinfo
.num_fields
= report
->maxfield
;
525 if (copy_to_user(user_arg
, &rinfo
, sizeof(rinfo
)))
530 case HIDIOCGFIELDINFO
:
531 if (copy_from_user(&finfo
, user_arg
, sizeof(finfo
)))
533 rinfo
.report_type
= finfo
.report_type
;
534 rinfo
.report_id
= finfo
.report_id
;
535 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
538 if (finfo
.field_index
>= report
->maxfield
)
541 field
= report
->field
[finfo
.field_index
];
542 memset(&finfo
, 0, sizeof(finfo
));
543 finfo
.report_type
= rinfo
.report_type
;
544 finfo
.report_id
= rinfo
.report_id
;
545 finfo
.field_index
= field
->report_count
- 1;
546 finfo
.maxusage
= field
->maxusage
;
547 finfo
.flags
= field
->flags
;
548 finfo
.physical
= field
->physical
;
549 finfo
.logical
= field
->logical
;
550 finfo
.application
= field
->application
;
551 finfo
.logical_minimum
= field
->logical_minimum
;
552 finfo
.logical_maximum
= field
->logical_maximum
;
553 finfo
.physical_minimum
= field
->physical_minimum
;
554 finfo
.physical_maximum
= field
->physical_maximum
;
555 finfo
.unit_exponent
= field
->unit_exponent
;
556 finfo
.unit
= field
->unit
;
558 if (copy_to_user(user_arg
, &finfo
, sizeof(finfo
)))
564 uref_multi
= kmalloc(sizeof(struct hiddev_usage_ref_multi
), GFP_KERNEL
);
567 uref
= &uref_multi
->uref
;
568 if (copy_from_user(uref
, user_arg
, sizeof(*uref
)))
571 rinfo
.report_type
= uref
->report_type
;
572 rinfo
.report_id
= uref
->report_id
;
573 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
576 if (uref
->field_index
>= report
->maxfield
)
579 field
= report
->field
[uref
->field_index
];
580 if (uref
->usage_index
>= field
->maxusage
)
583 uref
->usage_code
= field
->usage
[uref
->usage_index
].hid
;
585 if (copy_to_user(user_arg
, uref
, sizeof(*uref
)))
595 case HIDIOCGCOLLECTIONINDEX
:
596 uref_multi
= kmalloc(sizeof(struct hiddev_usage_ref_multi
), GFP_KERNEL
);
599 uref
= &uref_multi
->uref
;
600 if (cmd
== HIDIOCGUSAGES
|| cmd
== HIDIOCSUSAGES
) {
601 if (copy_from_user(uref_multi
, user_arg
,
602 sizeof(*uref_multi
)))
605 if (copy_from_user(uref
, user_arg
, sizeof(*uref
)))
609 if (cmd
!= HIDIOCGUSAGE
&&
610 cmd
!= HIDIOCGUSAGES
&&
611 uref
->report_type
== HID_REPORT_TYPE_INPUT
)
614 if (uref
->report_id
== HID_REPORT_ID_UNKNOWN
) {
615 field
= hiddev_lookup_usage(hid
, uref
);
619 rinfo
.report_type
= uref
->report_type
;
620 rinfo
.report_id
= uref
->report_id
;
621 if ((report
= hiddev_lookup_report(hid
, &rinfo
)) == NULL
)
624 if (uref
->field_index
>= report
->maxfield
)
627 field
= report
->field
[uref
->field_index
];
629 if (cmd
== HIDIOCGCOLLECTIONINDEX
) {
630 if (uref
->usage_index
>= field
->maxusage
)
632 } else if (uref
->usage_index
>= field
->report_count
)
635 else if ((cmd
== HIDIOCGUSAGES
|| cmd
== HIDIOCSUSAGES
) &&
636 (uref_multi
->num_values
> HID_MAX_MULTI_USAGES
||
637 uref
->usage_index
+ uref_multi
->num_values
> field
->report_count
))
643 uref
->value
= field
->value
[uref
->usage_index
];
644 if (copy_to_user(user_arg
, uref
, sizeof(*uref
)))
649 field
->value
[uref
->usage_index
] = uref
->value
;
652 case HIDIOCGCOLLECTIONINDEX
:
654 return field
->usage
[uref
->usage_index
].collection_index
;
656 for (i
= 0; i
< uref_multi
->num_values
; i
++)
657 uref_multi
->values
[i
] =
658 field
->value
[uref
->usage_index
+ i
];
659 if (copy_to_user(user_arg
, uref_multi
,
660 sizeof(*uref_multi
)))
664 for (i
= 0; i
< uref_multi
->num_values
; i
++)
665 field
->value
[uref
->usage_index
+ i
] =
666 uref_multi
->values
[i
];
680 case HIDIOCGCOLLECTIONINFO
:
681 if (copy_from_user(&cinfo
, user_arg
, sizeof(cinfo
)))
684 if (cinfo
.index
>= hid
->maxcollection
)
687 cinfo
.type
= hid
->collection
[cinfo
.index
].type
;
688 cinfo
.usage
= hid
->collection
[cinfo
.index
].usage
;
689 cinfo
.level
= hid
->collection
[cinfo
.index
].level
;
691 if (copy_to_user(user_arg
, &cinfo
, sizeof(cinfo
)))
697 if (_IOC_TYPE(cmd
) != 'H' || _IOC_DIR(cmd
) != _IOC_READ
)
700 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGNAME(0))) {
704 len
= strlen(hid
->name
) + 1;
705 if (len
> _IOC_SIZE(cmd
))
706 len
= _IOC_SIZE(cmd
);
707 return copy_to_user(user_arg
, hid
->name
, len
) ?
711 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGPHYS(0))) {
715 len
= strlen(hid
->phys
) + 1;
716 if (len
> _IOC_SIZE(cmd
))
717 len
= _IOC_SIZE(cmd
);
718 return copy_to_user(user_arg
, hid
->phys
, len
) ?
725 static const struct file_operations hiddev_fops
= {
726 .owner
= THIS_MODULE
,
728 .write
= hiddev_write
,
731 .release
= hiddev_release
,
732 .ioctl
= hiddev_ioctl
,
733 .fasync
= hiddev_fasync
,
736 static struct usb_class_driver hiddev_class
= {
738 .fops
= &hiddev_fops
,
739 .minor_base
= HIDDEV_MINOR_BASE
,
743 * This is where hid.c calls us to connect a hid device to the hiddev driver
745 int hiddev_connect(struct hid_device
*hid
)
747 struct hiddev
*hiddev
;
751 for (i
= 0; i
< hid
->maxcollection
; i
++)
752 if (hid
->collection
[i
].type
==
753 HID_COLLECTION_APPLICATION
&&
754 !IS_INPUT_APPLICATION(hid
->collection
[i
].usage
))
757 if (i
== hid
->maxcollection
&& (hid
->quirks
& HID_QUIRK_HIDDEV
) == 0)
760 if (!(hiddev
= kzalloc(sizeof(struct hiddev
), GFP_KERNEL
)))
763 retval
= usb_register_dev(hid
->intf
, &hiddev_class
);
765 err("Not able to get a minor for this device.");
770 init_waitqueue_head(&hiddev
->wait
);
771 INIT_LIST_HEAD(&hiddev
->list
);
775 hid
->minor
= hid
->intf
->minor
;
776 hid
->hiddev
= hiddev
;
778 hiddev_table
[hid
->intf
->minor
- HIDDEV_MINOR_BASE
] = hiddev
;
784 * This is where hid.c calls us to disconnect a hiddev device from the
785 * corresponding hid device (usually because the usb device has disconnected)
787 static struct usb_class_driver hiddev_class
;
788 void hiddev_disconnect(struct hid_device
*hid
)
790 struct hiddev
*hiddev
= hid
->hiddev
;
794 hiddev_table
[hiddev
->hid
->minor
- HIDDEV_MINOR_BASE
] = NULL
;
795 usb_deregister_dev(hiddev
->hid
->intf
, &hiddev_class
);
798 hid_close(hiddev
->hid
);
799 wake_up_interruptible(&hiddev
->wait
);
805 /* Currently this driver is a USB driver. It's not a conventional one in
806 * the sense that it doesn't probe at the USB level. Instead it waits to
807 * be connected by HID through the hiddev_connect / hiddev_disconnect
808 * routines. The reason to register as a USB device is to gain part of the
809 * minor number space from the USB major.
811 * In theory, should the HID code be generalized to more than one physical
812 * medium (say, IEEE 1384), this driver will probably need to register its
813 * own major number, and in doing so, no longer need to register with USB.
814 * At that point the probe routine and hiddev_driver struct below will no
819 /* We never attach in this manner, and rely on HID to connect us. This
820 * is why there is no disconnect routine defined in the usb_driver either.
822 static int hiddev_usbd_probe(struct usb_interface
*intf
,
823 const struct usb_device_id
*hiddev_info
)
829 static /* const */ struct usb_driver hiddev_driver
= {
831 .probe
= hiddev_usbd_probe
,
834 int __init
hiddev_init(void)
836 return usb_register(&hiddev_driver
);
839 void hiddev_exit(void)
841 usb_deregister(&hiddev_driver
);