blob: e1f07483691f7823b46db14c38dc0d8daa536a13 [file] [log] [blame]
Jiri Kosina86166b72007-05-14 09:57:40 +02001/*
2 * HID raw devices, giving access to raw HID events.
3 *
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.
8 *
9 * Copyright (c) 2007 Jiri Kosina
10 */
11
12/*
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.
16 *
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.
20 */
21
22#include <linux/fs.h>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020032#include <linux/hid.h>
33#include <linux/mutex.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040034#include <linux/sched.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020035
36#include <linux/hidraw.h>
37
38static int hidraw_major;
39static struct cdev hidraw_cdev;
40static struct class *hidraw_class;
41static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
Oliver Neukum7d672cd2008-10-31 00:07:23 +010042static DEFINE_MUTEX(minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +020043
44static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
45{
46 struct hidraw_list *list = file->private_data;
47 int ret = 0, len;
Jiri Kosina86166b72007-05-14 09:57:40 +020048 DECLARE_WAITQUEUE(wait, current);
49
Jiri Kosinab0e14952009-10-12 11:25:56 +020050 mutex_lock(&list->read_mutex);
51
Jiri Kosina86166b72007-05-14 09:57:40 +020052 while (ret == 0) {
Jiri Kosina86166b72007-05-14 09:57:40 +020053 if (list->head == list->tail) {
54 add_wait_queue(&list->hidraw->wait, &wait);
55 set_current_state(TASK_INTERRUPTIBLE);
56
57 while (list->head == list->tail) {
58 if (file->f_flags & O_NONBLOCK) {
59 ret = -EAGAIN;
60 break;
61 }
62 if (signal_pending(current)) {
63 ret = -ERESTARTSYS;
64 break;
65 }
66 if (!list->hidraw->exist) {
67 ret = -EIO;
68 break;
69 }
70
71 /* allow O_NONBLOCK to work well from other threads */
72 mutex_unlock(&list->read_mutex);
73 schedule();
74 mutex_lock(&list->read_mutex);
75 set_current_state(TASK_INTERRUPTIBLE);
76 }
77
78 set_current_state(TASK_RUNNING);
79 remove_wait_queue(&list->hidraw->wait, &wait);
80 }
81
82 if (ret)
83 goto out;
84
Jiri Kosina86166b72007-05-14 09:57:40 +020085 len = list->buffer[list->tail].len > count ?
86 count : list->buffer[list->tail].len;
87
88 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
89 ret = -EFAULT;
90 goto out;
91 }
92 ret += len;
93
94 kfree(list->buffer[list->tail].value);
95 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
96 }
97out:
98 mutex_unlock(&list->read_mutex);
99 return ret;
100}
101
102/* the first byte is expected to be a report number */
103static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
104{
105 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Jiri Kosina2e574802010-03-25 14:15:11 +0100106 struct hid_device *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200107 __u8 *buf;
108 int ret = 0;
109
Jiri Kosina2e574802010-03-25 14:15:11 +0100110 mutex_lock(&minors_lock);
Antonio Ospitee42dee92010-10-05 17:20:17 +0200111
112 if (!hidraw_table[minor]) {
113 ret = -ENODEV;
114 goto out;
115 }
116
Jiri Kosina2e574802010-03-25 14:15:11 +0100117 dev = hidraw_table[minor]->hid;
118
119 if (!dev->hid_output_raw_report) {
120 ret = -ENODEV;
121 goto out;
122 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200123
Jiri Kosina2b107d62008-09-17 19:41:58 +0200124 if (count > HID_MAX_BUFFER_SIZE) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200125 printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700126 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100127 ret = -EINVAL;
128 goto out;
Jiri Kosina86166b72007-05-14 09:57:40 +0200129 }
130
131 if (count < 2) {
132 printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700133 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100134 ret = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200135 goto out;
136 }
137
Jiri Kosina2e574802010-03-25 14:15:11 +0100138 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
139 if (!buf) {
140 ret = -ENOMEM;
141 goto out;
142 }
143
144 if (copy_from_user(buf, buffer, count)) {
145 ret = -EFAULT;
146 goto out_free;
147 }
148
Jiri Kosinad4bfa032010-01-29 15:03:36 +0100149 ret = dev->hid_output_raw_report(dev, buf, count, HID_OUTPUT_REPORT);
Jiri Kosina2e574802010-03-25 14:15:11 +0100150out_free:
Jiri Kosina86166b72007-05-14 09:57:40 +0200151 kfree(buf);
Jiri Kosina2e574802010-03-25 14:15:11 +0100152out:
153 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200154 return ret;
155}
156
157static unsigned int hidraw_poll(struct file *file, poll_table *wait)
158{
159 struct hidraw_list *list = file->private_data;
160
161 poll_wait(file, &list->hidraw->wait, wait);
162 if (list->head != list->tail)
163 return POLLIN | POLLRDNORM;
164 if (!list->hidraw->exist)
165 return POLLERR | POLLHUP;
166 return 0;
167}
168
169static int hidraw_open(struct inode *inode, struct file *file)
170{
171 unsigned int minor = iminor(inode);
172 struct hidraw *dev;
173 struct hidraw_list *list;
174 int err = 0;
175
176 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
177 err = -ENOMEM;
178 goto out;
179 }
180
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100181 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200182 if (!hidraw_table[minor]) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200183 kfree(list);
184 err = -ENODEV;
185 goto out_unlock;
186 }
187
188 list->hidraw = hidraw_table[minor];
189 mutex_init(&list->read_mutex);
190 list_add_tail(&list->node, &hidraw_table[minor]->list);
191 file->private_data = list;
192
193 dev = hidraw_table[minor];
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100194 if (!dev->open++) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100195 if (dev->hid->ll_driver->power) {
196 err = dev->hid->ll_driver->power(dev->hid, PM_HINT_FULLON);
197 if (err < 0)
198 goto out_unlock;
199 }
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100200 err = dev->hid->ll_driver->open(dev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100201 if (err < 0) {
202 if (dev->hid->ll_driver->power)
203 dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100204 dev->open--;
Oliver Neukum0361a282008-12-17 15:38:03 +0100205 }
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100206 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200207
208out_unlock:
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100209 mutex_unlock(&minors_lock);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100210out:
Jiri Kosina86166b72007-05-14 09:57:40 +0200211 return err;
212
213}
214
215static int hidraw_release(struct inode * inode, struct file * file)
216{
217 unsigned int minor = iminor(inode);
218 struct hidraw *dev;
219 struct hidraw_list *list = file->private_data;
Jiri Slabycb174682010-10-19 11:29:55 +0200220 int ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200221
Jiri Slabycb174682010-10-19 11:29:55 +0200222 mutex_lock(&minors_lock);
223 if (!hidraw_table[minor]) {
224 ret = -ENODEV;
225 goto unlock;
226 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200227
228 list_del(&list->node);
229 dev = hidraw_table[minor];
Oliver Neukumb8a832b2008-12-15 13:12:08 +0100230 if (!--dev->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100231 if (list->hidraw->exist) {
232 if (dev->hid->ll_driver->power)
233 dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
Jiri Slabyc500c972008-05-16 11:49:16 +0200234 dev->hid->ll_driver->close(dev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100235 } else {
Jiri Kosina86166b72007-05-14 09:57:40 +0200236 kfree(list->hidraw);
Oliver Neukum0361a282008-12-17 15:38:03 +0100237 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200238 }
Jiri Kosina4db1c622008-06-24 14:45:27 +0200239 kfree(list);
Jiri Slabycb174682010-10-19 11:29:55 +0200240 ret = 0;
241unlock:
242 mutex_unlock(&minors_lock);
Jiri Kosina4db1c622008-06-24 14:45:27 +0200243
Jiri Slabycb174682010-10-19 11:29:55 +0200244 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200245}
246
Alan Cox979c4072008-05-26 11:25:26 +0200247static long hidraw_ioctl(struct file *file, unsigned int cmd,
248 unsigned long arg)
Jiri Kosina86166b72007-05-14 09:57:40 +0200249{
Alan Cox979c4072008-05-26 11:25:26 +0200250 struct inode *inode = file->f_path.dentry->d_inode;
Jiri Kosina86166b72007-05-14 09:57:40 +0200251 unsigned int minor = iminor(inode);
Alan Cox979c4072008-05-26 11:25:26 +0200252 long ret = 0;
Jiri Kosina2e574802010-03-25 14:15:11 +0100253 struct hidraw *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200254 void __user *user_arg = (void __user*) arg;
255
Jiri Kosina2e574802010-03-25 14:15:11 +0100256 mutex_lock(&minors_lock);
257 dev = hidraw_table[minor];
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200258 if (!dev) {
259 ret = -ENODEV;
260 goto out;
261 }
Jiri Kosina2e574802010-03-25 14:15:11 +0100262
Jiri Kosina86166b72007-05-14 09:57:40 +0200263 switch (cmd) {
264 case HIDIOCGRDESCSIZE:
265 if (put_user(dev->hid->rsize, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200266 ret = -EFAULT;
267 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200268
269 case HIDIOCGRDESC:
270 {
271 __u32 len;
272
273 if (get_user(len, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200274 ret = -EFAULT;
275 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
276 ret = -EINVAL;
277 else if (copy_to_user(user_arg + offsetof(
278 struct hidraw_report_descriptor,
279 value[0]),
280 dev->hid->rdesc,
281 min(dev->hid->rsize, len)))
282 ret = -EFAULT;
283 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200284 }
285 case HIDIOCGRAWINFO:
286 {
287 struct hidraw_devinfo dinfo;
288
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)))
Alan Cox979c4072008-05-26 11:25:26 +0200293 ret = -EFAULT;
294 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200295 }
296 default:
Jiri Kosina9188e792008-11-12 16:14:08 +0100297 {
298 struct hid_device *hid = dev->hid;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300299 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
300 ret = -EINVAL;
301 break;
302 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100303
304 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
305 int len;
Dan Carpenter38089c62009-04-07 16:35:56 +0200306 if (!hid->name) {
307 ret = 0;
308 break;
309 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100310 len = strlen(hid->name) + 1;
311 if (len > _IOC_SIZE(cmd))
312 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300313 ret = copy_to_user(user_arg, hid->name, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100314 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300315 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100316 }
317
318 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
319 int len;
Dan Carpenter38089c62009-04-07 16:35:56 +0200320 if (!hid->phys) {
321 ret = 0;
322 break;
323 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100324 len = strlen(hid->phys) + 1;
325 if (len > _IOC_SIZE(cmd))
326 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300327 ret = copy_to_user(user_arg, hid->phys, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100328 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300329 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100330 }
Antonio Ospite81cd5842010-04-30 21:49:58 +0200331 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100332
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300333 ret = -ENOTTY;
Jiri Kosina86166b72007-05-14 09:57:40 +0200334 }
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200335out:
Jiri Kosina2e574802010-03-25 14:15:11 +0100336 mutex_unlock(&minors_lock);
Alan Cox979c4072008-05-26 11:25:26 +0200337 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200338}
339
340static const struct file_operations hidraw_ops = {
341 .owner = THIS_MODULE,
342 .read = hidraw_read,
343 .write = hidraw_write,
344 .poll = hidraw_poll,
345 .open = hidraw_open,
346 .release = hidraw_release,
Alan Cox979c4072008-05-26 11:25:26 +0200347 .unlocked_ioctl = hidraw_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200348 .llseek = noop_llseek,
Jiri Kosina86166b72007-05-14 09:57:40 +0200349};
350
351void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
352{
353 struct hidraw *dev = hid->hidraw;
354 struct hidraw_list *list;
355
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);
361 }
362
363 wake_up_interruptible(&dev->wait);
364}
365EXPORT_SYMBOL_GPL(hidraw_report_event);
366
367int hidraw_connect(struct hid_device *hid)
368{
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200369 int minor, result;
Jiri Kosina86166b72007-05-14 09:57:40 +0200370 struct hidraw *dev;
371
Jiri Kosinabbe281f2009-06-04 15:44:25 +0200372 /* we accept any HID device, no matter the applications */
Jiri Kosina86166b72007-05-14 09:57:40 +0200373
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200374 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
375 if (!dev)
376 return -ENOMEM;
377
378 result = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200379
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100380 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200381
382 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
383 if (hidraw_table[minor])
384 continue;
385 hidraw_table[minor] = dev;
386 result = 0;
387 break;
388 }
389
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200390 if (result) {
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100391 mutex_unlock(&minors_lock);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200392 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200393 goto out;
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200394 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200395
Jiri Kosinaaae6c282008-12-04 16:16:46 +0100396 dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700397 NULL, "%s%d", "hidraw", minor);
Jiri Kosina86166b72007-05-14 09:57:40 +0200398
399 if (IS_ERR(dev->dev)) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200400 hidraw_table[minor] = NULL;
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100401 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200402 result = PTR_ERR(dev->dev);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200403 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200404 goto out;
405 }
406
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100407 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200408 init_waitqueue_head(&dev->wait);
409 INIT_LIST_HEAD(&dev->list);
410
411 dev->hid = hid;
412 dev->minor = minor;
413
414 dev->exist = 1;
415 hid->hidraw = dev;
416
417out:
418 return result;
419
420}
421EXPORT_SYMBOL_GPL(hidraw_connect);
422
423void hidraw_disconnect(struct hid_device *hid)
424{
425 struct hidraw *hidraw = hid->hidraw;
426
427 hidraw->exist = 0;
428
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100429 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200430 hidraw_table[hidraw->minor] = NULL;
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100431 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200432
433 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
434
435 if (hidraw->open) {
Jiri Slabyc500c972008-05-16 11:49:16 +0200436 hid->ll_driver->close(hid);
Jiri Kosina86166b72007-05-14 09:57:40 +0200437 wake_up_interruptible(&hidraw->wait);
438 } else {
439 kfree(hidraw);
440 }
441}
442EXPORT_SYMBOL_GPL(hidraw_disconnect);
443
444int __init hidraw_init(void)
445{
446 int result;
447 dev_t dev_id;
448
449 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
450 HIDRAW_MAX_DEVICES, "hidraw");
451
452 hidraw_major = MAJOR(dev_id);
453
454 if (result < 0) {
455 printk(KERN_WARNING "hidraw: can't get major number\n");
456 result = 0;
457 goto out;
458 }
459
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");
464 goto out;
465 }
466
467 cdev_init(&hidraw_cdev, &hidraw_ops);
468 cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
469out:
470 return result;
471}
472
Jiri Slaby140ae3e2008-10-17 18:04:48 +0200473void hidraw_exit(void)
Jiri Kosina86166b72007-05-14 09:57:40 +0200474{
475 dev_t dev_id = MKDEV(hidraw_major, 0);
476
477 cdev_del(&hidraw_cdev);
478 class_destroy(hidraw_class);
479 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
480
481}