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>
37 #include <linux/smp_lock.h>
39 #include <linux/hidraw.h>
41 static int hidraw_major;
42 static struct cdev hidraw_cdev;
43 static struct class *hidraw_class;
44 static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
45 static DEFINE_MUTEX(minors_lock);
47 static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
49 struct hidraw_list *list = file->private_data;
51 DECLARE_WAITQUEUE(wait, current);
53 mutex_lock(&list->read_mutex);
56 if (list->head == list->tail) {
57 add_wait_queue(&list->hidraw->wait, &wait);
58 set_current_state(TASK_INTERRUPTIBLE);
60 while (list->head == list->tail) {
61 if (file->f_flags & O_NONBLOCK) {
65 if (signal_pending(current)) {
69 if (!list->hidraw->exist) {
74 /* allow O_NONBLOCK to work well from other threads */
75 mutex_unlock(&list->read_mutex);
77 mutex_lock(&list->read_mutex);
78 set_current_state(TASK_INTERRUPTIBLE);
81 set_current_state(TASK_RUNNING);
82 remove_wait_queue(&list->hidraw->wait, &wait);
88 len = list->buffer[list->tail].len > count ?
89 count : list->buffer[list->tail].len;
91 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
97 kfree(list->buffer[list->tail].value);
98 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
101 mutex_unlock(&list->read_mutex);
105 /* the first byte is expected to be a report number */
106 static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
108 unsigned int minor = iminor(file->f_path.dentry->d_inode);
109 struct hid_device *dev;
113 mutex_lock(&minors_lock);
115 if (!hidraw_table[minor]) {
120 dev = hidraw_table[minor]->hid;
122 if (!dev->hid_output_raw_report) {
127 if (count > HID_MAX_BUFFER_SIZE) {
128 hid_warn(dev, "pid %d passed too large report\n",
129 task_pid_nr(current));
135 hid_warn(dev, "pid %d passed too short report\n",
136 task_pid_nr(current));
141 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
147 if (copy_from_user(buf, buffer, count)) {
152 ret = dev->hid_output_raw_report(dev, buf, count, HID_OUTPUT_REPORT);
156 mutex_unlock(&minors_lock);
160 static unsigned int hidraw_poll(struct file *file, poll_table *wait)
162 struct hidraw_list *list = file->private_data;
164 poll_wait(file, &list->hidraw->wait, wait);
165 if (list->head != list->tail)
166 return POLLIN | POLLRDNORM;
167 if (!list->hidraw->exist)
168 return POLLERR | POLLHUP;
172 static int hidraw_open(struct inode *inode, struct file *file)
174 unsigned int minor = iminor(inode);
176 struct hidraw_list *list;
179 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
184 mutex_lock(&minors_lock);
185 if (!hidraw_table[minor]) {
191 list->hidraw = hidraw_table[minor];
192 mutex_init(&list->read_mutex);
193 list_add_tail(&list->node, &hidraw_table[minor]->list);
194 file->private_data = list;
196 dev = hidraw_table[minor];
198 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
202 err = hid_hw_open(dev->hid);
204 hid_hw_power(dev->hid, PM_HINT_NORMAL);
210 mutex_unlock(&minors_lock);
216 static int hidraw_release(struct inode * inode, struct file * file)
218 unsigned int minor = iminor(inode);
220 struct hidraw_list *list = file->private_data;
223 mutex_lock(&minors_lock);
224 if (!hidraw_table[minor]) {
229 list_del(&list->node);
230 dev = hidraw_table[minor];
232 if (list->hidraw->exist) {
233 hid_hw_power(dev->hid, PM_HINT_NORMAL);
234 hid_hw_close(dev->hid);
242 mutex_unlock(&minors_lock);
247 static long hidraw_ioctl(struct file *file, unsigned int cmd,
250 struct inode *inode = file->f_path.dentry->d_inode;
251 unsigned int minor = iminor(inode);
254 void __user *user_arg = (void __user*) arg;
256 mutex_lock(&minors_lock);
257 dev = hidraw_table[minor];
264 case HIDIOCGRDESCSIZE:
265 if (put_user(dev->hid->rsize, (int __user *)arg))
273 if (get_user(len, (int __user *)arg))
275 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
277 else if (copy_to_user(user_arg + offsetof(
278 struct hidraw_report_descriptor,
281 min(dev->hid->rsize, len)))
287 struct hidraw_devinfo dinfo;
289 dinfo.bustype = dev->hid->bus;
290 dinfo.vendor = dev->hid->vendor;
291 dinfo.product = dev->hid->product;
292 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
298 struct hid_device *hid = dev->hid;
299 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
304 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
310 len = strlen(hid->name) + 1;
311 if (len > _IOC_SIZE(cmd))
312 len = _IOC_SIZE(cmd);
313 ret = copy_to_user(user_arg, hid->name, len) ?
318 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
324 len = strlen(hid->phys) + 1;
325 if (len > _IOC_SIZE(cmd))
326 len = _IOC_SIZE(cmd);
327 ret = copy_to_user(user_arg, hid->phys, len) ?
336 mutex_unlock(&minors_lock);
340 static const struct file_operations hidraw_ops = {
341 .owner = THIS_MODULE,
343 .write = hidraw_write,
346 .release = hidraw_release,
347 .unlocked_ioctl = hidraw_ioctl,
348 .llseek = noop_llseek,
351 void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
353 struct hidraw *dev = hid->hidraw;
354 struct hidraw_list *list;
356 list_for_each_entry(list, &dev->list, node) {
357 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
358 list->buffer[list->head].len = len;
359 list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
360 kill_fasync(&list->fasync, SIGIO, POLL_IN);
363 wake_up_interruptible(&dev->wait);
365 EXPORT_SYMBOL_GPL(hidraw_report_event);
367 int hidraw_connect(struct hid_device *hid)
372 /* we accept any HID device, no matter the applications */
374 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
380 mutex_lock(&minors_lock);
382 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
383 if (hidraw_table[minor])
385 hidraw_table[minor] = dev;
391 mutex_unlock(&minors_lock);
396 dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
397 NULL, "%s%d", "hidraw", minor);
399 if (IS_ERR(dev->dev)) {
400 hidraw_table[minor] = NULL;
401 mutex_unlock(&minors_lock);
402 result = PTR_ERR(dev->dev);
407 mutex_unlock(&minors_lock);
408 init_waitqueue_head(&dev->wait);
409 INIT_LIST_HEAD(&dev->list);
421 EXPORT_SYMBOL_GPL(hidraw_connect);
423 void hidraw_disconnect(struct hid_device *hid)
425 struct hidraw *hidraw = hid->hidraw;
429 mutex_lock(&minors_lock);
430 hidraw_table[hidraw->minor] = NULL;
431 mutex_unlock(&minors_lock);
433 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
437 wake_up_interruptible(&hidraw->wait);
442 EXPORT_SYMBOL_GPL(hidraw_disconnect);
444 int __init hidraw_init(void)
449 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
450 HIDRAW_MAX_DEVICES, "hidraw");
452 hidraw_major = MAJOR(dev_id);
455 pr_warn("can't get major number\n");
460 hidraw_class = class_create(THIS_MODULE, "hidraw");
461 if (IS_ERR(hidraw_class)) {
462 result = PTR_ERR(hidraw_class);
463 unregister_chrdev(hidraw_major, "hidraw");
467 cdev_init(&hidraw_cdev, &hidraw_ops);
468 cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
473 void hidraw_exit(void)
475 dev_t dev_id = MKDEV(hidraw_major, 0);
477 cdev_del(&hidraw_cdev);
478 class_destroy(hidraw_class);
479 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);