2 * HID raw devices, giving access to raw HID events.
4 * In comparison to hiddev, this device does not process the
5 * hid events at all (no parsing, no lookups). This lets applications
6 * to work on raw hid events as they want to, and avoids a need to
7 * use a transport-specific userspace libhid/libusb libraries.
9 * Copyright (c) 2007 Jiri Kosina
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/cdev.h>
30 #include <linux/poll.h>
31 #include <linux/device.h>
32 #include <linux/major.h>
33 #include <linux/slab.h>
34 #include <linux/hid.h>
35 #include <linux/mutex.h>
36 #include <linux/sched.h>
38 #include <linux/hidraw.h>
40 static int hidraw_major
;
41 static struct cdev hidraw_cdev
;
42 static struct class *hidraw_class
;
43 static struct hidraw
*hidraw_table
[HIDRAW_MAX_DEVICES
];
44 static DEFINE_MUTEX(minors_lock
);
46 static ssize_t
hidraw_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
48 struct hidraw_list
*list
= file
->private_data
;
50 DECLARE_WAITQUEUE(wait
, current
);
52 mutex_lock(&list
->read_mutex
);
55 if (list
->head
== list
->tail
) {
56 add_wait_queue(&list
->hidraw
->wait
, &wait
);
57 set_current_state(TASK_INTERRUPTIBLE
);
59 while (list
->head
== list
->tail
) {
60 if (file
->f_flags
& O_NONBLOCK
) {
64 if (signal_pending(current
)) {
68 if (!list
->hidraw
->exist
) {
73 /* allow O_NONBLOCK to work well from other threads */
74 mutex_unlock(&list
->read_mutex
);
76 mutex_lock(&list
->read_mutex
);
77 set_current_state(TASK_INTERRUPTIBLE
);
80 set_current_state(TASK_RUNNING
);
81 remove_wait_queue(&list
->hidraw
->wait
, &wait
);
87 len
= list
->buffer
[list
->tail
].len
> count
?
88 count
: list
->buffer
[list
->tail
].len
;
90 if (copy_to_user(buffer
, list
->buffer
[list
->tail
].value
, len
)) {
96 kfree(list
->buffer
[list
->tail
].value
);
97 list
->tail
= (list
->tail
+ 1) & (HIDRAW_BUFFER_SIZE
- 1);
100 mutex_unlock(&list
->read_mutex
);
104 /* the first byte is expected to be a report number */
105 static ssize_t
hidraw_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
107 unsigned int minor
= iminor(file
->f_path
.dentry
->d_inode
);
108 struct hid_device
*dev
;
112 mutex_lock(&minors_lock
);
114 if (!hidraw_table
[minor
]) {
119 dev
= hidraw_table
[minor
]->hid
;
121 if (!dev
->hid_output_raw_report
) {
126 if (count
> HID_MAX_BUFFER_SIZE
) {
127 hid_warn(dev
, "pid %d passed too large report\n",
128 task_pid_nr(current
));
134 hid_warn(dev
, "pid %d passed too short report\n",
135 task_pid_nr(current
));
140 buf
= kmalloc(count
* sizeof(__u8
), GFP_KERNEL
);
146 if (copy_from_user(buf
, buffer
, count
)) {
151 ret
= dev
->hid_output_raw_report(dev
, buf
, count
, HID_OUTPUT_REPORT
);
155 mutex_unlock(&minors_lock
);
159 static unsigned int hidraw_poll(struct file
*file
, poll_table
*wait
)
161 struct hidraw_list
*list
= file
->private_data
;
163 poll_wait(file
, &list
->hidraw
->wait
, wait
);
164 if (list
->head
!= list
->tail
)
165 return POLLIN
| POLLRDNORM
;
166 if (!list
->hidraw
->exist
)
167 return POLLERR
| POLLHUP
;
171 static int hidraw_open(struct inode
*inode
, struct file
*file
)
173 unsigned int minor
= iminor(inode
);
175 struct hidraw_list
*list
;
178 if (!(list
= kzalloc(sizeof(struct hidraw_list
), GFP_KERNEL
))) {
183 mutex_lock(&minors_lock
);
184 if (!hidraw_table
[minor
]) {
190 list
->hidraw
= hidraw_table
[minor
];
191 mutex_init(&list
->read_mutex
);
192 list_add_tail(&list
->node
, &hidraw_table
[minor
]->list
);
193 file
->private_data
= list
;
195 dev
= hidraw_table
[minor
];
197 err
= hid_hw_power(dev
->hid
, PM_HINT_FULLON
);
201 err
= hid_hw_open(dev
->hid
);
203 hid_hw_power(dev
->hid
, PM_HINT_NORMAL
);
209 mutex_unlock(&minors_lock
);
215 static int hidraw_release(struct inode
* inode
, struct file
* file
)
217 unsigned int minor
= iminor(inode
);
219 struct hidraw_list
*list
= file
->private_data
;
222 mutex_lock(&minors_lock
);
223 if (!hidraw_table
[minor
]) {
228 list_del(&list
->node
);
229 dev
= hidraw_table
[minor
];
231 if (list
->hidraw
->exist
) {
232 hid_hw_power(dev
->hid
, PM_HINT_NORMAL
);
233 hid_hw_close(dev
->hid
);
241 mutex_unlock(&minors_lock
);
246 static long hidraw_ioctl(struct file
*file
, unsigned int cmd
,
249 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
250 unsigned int minor
= iminor(inode
);
253 void __user
*user_arg
= (void __user
*) arg
;
255 mutex_lock(&minors_lock
);
256 dev
= hidraw_table
[minor
];
263 case HIDIOCGRDESCSIZE
:
264 if (put_user(dev
->hid
->rsize
, (int __user
*)arg
))
272 if (get_user(len
, (int __user
*)arg
))
274 else if (len
> HID_MAX_DESCRIPTOR_SIZE
- 1)
276 else if (copy_to_user(user_arg
+ offsetof(
277 struct hidraw_report_descriptor
,
280 min(dev
->hid
->rsize
, len
)))
286 struct hidraw_devinfo dinfo
;
288 dinfo
.bustype
= dev
->hid
->bus
;
289 dinfo
.vendor
= dev
->hid
->vendor
;
290 dinfo
.product
= dev
->hid
->product
;
291 if (copy_to_user(user_arg
, &dinfo
, sizeof(dinfo
)))
297 struct hid_device
*hid
= dev
->hid
;
298 if (_IOC_TYPE(cmd
) != 'H' || _IOC_DIR(cmd
) != _IOC_READ
) {
303 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGRAWNAME(0))) {
309 len
= strlen(hid
->name
) + 1;
310 if (len
> _IOC_SIZE(cmd
))
311 len
= _IOC_SIZE(cmd
);
312 ret
= copy_to_user(user_arg
, hid
->name
, len
) ?
317 if (_IOC_NR(cmd
) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
323 len
= strlen(hid
->phys
) + 1;
324 if (len
> _IOC_SIZE(cmd
))
325 len
= _IOC_SIZE(cmd
);
326 ret
= copy_to_user(user_arg
, hid
->phys
, len
) ?
335 mutex_unlock(&minors_lock
);
339 static const struct file_operations hidraw_ops
= {
340 .owner
= THIS_MODULE
,
342 .write
= hidraw_write
,
345 .release
= hidraw_release
,
346 .unlocked_ioctl
= hidraw_ioctl
,
348 .compat_ioctl
= hidraw_ioctl
,
350 .llseek
= noop_llseek
,
353 void hidraw_report_event(struct hid_device
*hid
, u8
*data
, int len
)
355 struct hidraw
*dev
= hid
->hidraw
;
356 struct hidraw_list
*list
;
358 list_for_each_entry(list
, &dev
->list
, node
) {
359 list
->buffer
[list
->head
].value
= kmemdup(data
, len
, GFP_ATOMIC
);
360 list
->buffer
[list
->head
].len
= len
;
361 list
->head
= (list
->head
+ 1) & (HIDRAW_BUFFER_SIZE
- 1);
362 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
365 wake_up_interruptible(&dev
->wait
);
367 EXPORT_SYMBOL_GPL(hidraw_report_event
);
369 int hidraw_connect(struct hid_device
*hid
)
374 /* we accept any HID device, no matter the applications */
376 dev
= kzalloc(sizeof(struct hidraw
), GFP_KERNEL
);
382 mutex_lock(&minors_lock
);
384 for (minor
= 0; minor
< HIDRAW_MAX_DEVICES
; minor
++) {
385 if (hidraw_table
[minor
])
387 hidraw_table
[minor
] = dev
;
393 mutex_unlock(&minors_lock
);
398 dev
->dev
= device_create(hidraw_class
, &hid
->dev
, MKDEV(hidraw_major
, minor
),
399 NULL
, "%s%d", "hidraw", minor
);
401 if (IS_ERR(dev
->dev
)) {
402 hidraw_table
[minor
] = NULL
;
403 mutex_unlock(&minors_lock
);
404 result
= PTR_ERR(dev
->dev
);
409 mutex_unlock(&minors_lock
);
410 init_waitqueue_head(&dev
->wait
);
411 INIT_LIST_HEAD(&dev
->list
);
423 EXPORT_SYMBOL_GPL(hidraw_connect
);
425 void hidraw_disconnect(struct hid_device
*hid
)
427 struct hidraw
*hidraw
= hid
->hidraw
;
431 mutex_lock(&minors_lock
);
432 hidraw_table
[hidraw
->minor
] = NULL
;
433 mutex_unlock(&minors_lock
);
435 device_destroy(hidraw_class
, MKDEV(hidraw_major
, hidraw
->minor
));
439 wake_up_interruptible(&hidraw
->wait
);
444 EXPORT_SYMBOL_GPL(hidraw_disconnect
);
446 int __init
hidraw_init(void)
451 result
= alloc_chrdev_region(&dev_id
, HIDRAW_FIRST_MINOR
,
452 HIDRAW_MAX_DEVICES
, "hidraw");
454 hidraw_major
= MAJOR(dev_id
);
457 pr_warn("can't get major number\n");
462 hidraw_class
= class_create(THIS_MODULE
, "hidraw");
463 if (IS_ERR(hidraw_class
)) {
464 result
= PTR_ERR(hidraw_class
);
465 unregister_chrdev(hidraw_major
, "hidraw");
469 cdev_init(&hidraw_cdev
, &hidraw_ops
);
470 cdev_add(&hidraw_cdev
, dev_id
, HIDRAW_MAX_DEVICES
);
475 void hidraw_exit(void)
477 dev_t dev_id
= MKDEV(hidraw_major
, 0);
479 cdev_del(&hidraw_cdev
);
480 class_destroy(hidraw_class
);
481 unregister_chrdev_region(dev_id
, HIDRAW_MAX_DEVICES
);