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.
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/cdev.h>
28 #include <linux/poll.h>
29 #include <linux/device.h>
30 #include <linux/major.h>
31 #include <linux/hid.h>
32 #include <linux/mutex.h>
34 #include <linux/hidraw.h>
36 static int hidraw_major;
37 static struct cdev hidraw_cdev;
38 static struct class *hidraw_class;
39 static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
40 static DEFINE_SPINLOCK(minors_lock);
42 static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
44 struct hidraw_list *list = file->private_data;
47 DECLARE_WAITQUEUE(wait, current);
51 mutex_lock(&list->read_mutex);
53 if (list->head == list->tail) {
54 add_wait_queue(&list->hidraw->wait, &wait);
55 set_current_state(TASK_INTERRUPTIBLE);
57 while (list->head == list->tail) {
58 if (file->f_flags & O_NONBLOCK) {
62 if (signal_pending(current)) {
66 if (!list->hidraw->exist) {
71 /* allow O_NONBLOCK to work well from other threads */
72 mutex_unlock(&list->read_mutex);
74 mutex_lock(&list->read_mutex);
75 set_current_state(TASK_INTERRUPTIBLE);
78 set_current_state(TASK_RUNNING);
79 remove_wait_queue(&list->hidraw->wait, &wait);
85 report = list->buffer[list->tail].value;
86 len = list->buffer[list->tail].len > count ?
87 count : list->buffer[list->tail].len;
89 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
95 kfree(list->buffer[list->tail].value);
96 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
99 mutex_unlock(&list->read_mutex);
103 /* the first byte is expected to be a report number */
104 static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
106 unsigned int minor = iminor(file->f_path.dentry->d_inode);
107 struct hid_device *dev = hidraw_table[minor]->hid;
111 if (!dev->hid_output_raw_report)
114 if (count > HID_MIN_BUFFER_SIZE) {
115 printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
121 printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
126 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
130 if (copy_from_user(buf, buffer, count)) {
135 ret = dev->hid_output_raw_report(dev, buf, count);
141 static unsigned int hidraw_poll(struct file *file, poll_table *wait)
143 struct hidraw_list *list = file->private_data;
145 poll_wait(file, &list->hidraw->wait, wait);
146 if (list->head != list->tail)
147 return POLLIN | POLLRDNORM;
148 if (!list->hidraw->exist)
149 return POLLERR | POLLHUP;
153 static int hidraw_open(struct inode *inode, struct file *file)
155 unsigned int minor = iminor(inode);
157 struct hidraw_list *list;
160 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
165 spin_lock(&minors_lock);
166 if (!hidraw_table[minor]) {
167 printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
174 list->hidraw = hidraw_table[minor];
175 mutex_init(&list->read_mutex);
176 list_add_tail(&list->node, &hidraw_table[minor]->list);
177 file->private_data = list;
179 dev = hidraw_table[minor];
181 dev->hid->hid_open(dev->hid);
184 spin_unlock(&minors_lock);
190 static int hidraw_release(struct inode * inode, struct file * file)
192 unsigned int minor = iminor(inode);
194 struct hidraw_list *list = file->private_data;
196 if (!hidraw_table[minor]) {
197 printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
202 list_del(&list->node);
203 dev = hidraw_table[minor];
205 if (list->hidraw->exist)
206 dev->hid->hid_close(dev->hid);
214 static int hidraw_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
216 unsigned int minor = iminor(inode);
217 struct hidraw *dev = hidraw_table[minor];
218 void __user *user_arg = (void __user*) arg;
221 case HIDIOCGRDESCSIZE:
222 if (put_user(dev->hid->rsize, (int __user *)arg))
230 if (get_user(len, (int __user *)arg))
232 if (copy_to_user(*((__u8 **)(user_arg +
234 dev->hid->rdesc, len))
240 struct hidraw_devinfo dinfo;
242 dinfo.bustype = dev->hid->bus;
243 dinfo.vendor = dev->hid->vendor;
244 dinfo.product = dev->hid->product;
245 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
251 printk(KERN_EMERG "hidraw: unsupported ioctl() %x\n",
257 static const struct file_operations hidraw_ops = {
258 .owner = THIS_MODULE,
260 .write = hidraw_write,
263 .release = hidraw_release,
264 .ioctl = hidraw_ioctl,
267 void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
269 struct hidraw *dev = hid->hidraw;
270 struct hidraw_list *list;
272 list_for_each_entry(list, &dev->list, node) {
273 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
274 list->buffer[list->head].len = len;
275 list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
276 kill_fasync(&list->fasync, SIGIO, POLL_IN);
279 wake_up_interruptible(&dev->wait);
281 EXPORT_SYMBOL_GPL(hidraw_report_event);
283 int hidraw_connect(struct hid_device *hid)
285 int minor, result = -EINVAL;
288 /* TODO currently we accept any HID device. This should later
289 * probably be fixed to accept only those devices which provide
290 * non-input applications
293 if (!(dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL)))
296 spin_lock(&minors_lock);
298 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
299 if (hidraw_table[minor])
301 hidraw_table[minor] = dev;
306 spin_unlock(&minors_lock);
311 dev->dev = device_create(hidraw_class, NULL, MKDEV(hidraw_major, minor),
312 "%s%d", "hidraw", minor);
314 if (IS_ERR(dev->dev)) {
315 spin_lock(&minors_lock);
316 hidraw_table[minor] = NULL;
317 spin_unlock(&minors_lock);
318 result = PTR_ERR(dev->dev);
322 init_waitqueue_head(&dev->wait);
323 INIT_LIST_HEAD(&dev->list);
335 EXPORT_SYMBOL_GPL(hidraw_connect);
337 void hidraw_disconnect(struct hid_device *hid)
339 struct hidraw *hidraw = hid->hidraw;
343 spin_lock(&minors_lock);
344 hidraw_table[hidraw->minor] = NULL;
345 spin_unlock(&minors_lock);
347 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
351 wake_up_interruptible(&hidraw->wait);
356 EXPORT_SYMBOL_GPL(hidraw_disconnect);
358 int __init hidraw_init(void)
363 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
364 HIDRAW_MAX_DEVICES, "hidraw");
366 hidraw_major = MAJOR(dev_id);
369 printk(KERN_WARNING "hidraw: can't get major number\n");
374 hidraw_class = class_create(THIS_MODULE, "hidraw");
375 if (IS_ERR(hidraw_class)) {
376 result = PTR_ERR(hidraw_class);
377 unregister_chrdev(hidraw_major, "hidraw");
381 cdev_init(&hidraw_cdev, &hidraw_ops);
382 cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
387 void __exit hidraw_exit(void)
389 dev_t dev_id = MKDEV(hidraw_major, 0);
391 cdev_del(&hidraw_cdev);
392 class_destroy(hidraw_class);
393 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);