blob: b079a9c59958db7d9116d83e32dd613c187c16a3 [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>
31#include <linux/hid.h>
32#include <linux/mutex.h>
Jonathan Corbet702e57d2008-05-15 10:25:44 -060033#include <linux/smp_lock.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020034
35#include <linux/hidraw.h>
36
37static int hidraw_major;
38static struct cdev hidraw_cdev;
39static struct class *hidraw_class;
40static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
Oliver Neukum7d672cd2008-10-31 00:07:23 +010041static DEFINE_MUTEX(minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +020042
43static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
44{
45 struct hidraw_list *list = file->private_data;
46 int ret = 0, len;
47 char *report;
48 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
85 report = list->buffer[list->tail].value;
86 len = list->buffer[list->tail].len > count ?
87 count : list->buffer[list->tail].len;
88
89 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
90 ret = -EFAULT;
91 goto out;
92 }
93 ret += len;
94
95 kfree(list->buffer[list->tail].value);
96 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
97 }
98out:
99 mutex_unlock(&list->read_mutex);
100 return ret;
101}
102
103/* the first byte is expected to be a report number */
104static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
105{
106 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Alan Cox979c4072008-05-26 11:25:26 +0200107 /* FIXME: What stops hidraw_table going NULL */
Jiri Kosina86166b72007-05-14 09:57:40 +0200108 struct hid_device *dev = hidraw_table[minor]->hid;
109 __u8 *buf;
110 int ret = 0;
111
112 if (!dev->hid_output_raw_report)
113 return -ENODEV;
114
Jiri Kosina2b107d62008-09-17 19:41:58 +0200115 if (count > HID_MAX_BUFFER_SIZE) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200116 printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700117 task_pid_nr(current));
Jiri Kosina86166b72007-05-14 09:57:40 +0200118 return -EINVAL;
119 }
120
121 if (count < 2) {
122 printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700123 task_pid_nr(current));
Jiri Kosina86166b72007-05-14 09:57:40 +0200124 return -EINVAL;
125 }
126
127 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
128 if (!buf)
129 return -ENOMEM;
130
131 if (copy_from_user(buf, buffer, count)) {
132 ret = -EFAULT;
133 goto out;
134 }
135
136 ret = dev->hid_output_raw_report(dev, buf, count);
137out:
138 kfree(buf);
139 return ret;
140}
141
142static unsigned int hidraw_poll(struct file *file, poll_table *wait)
143{
144 struct hidraw_list *list = file->private_data;
145
146 poll_wait(file, &list->hidraw->wait, wait);
147 if (list->head != list->tail)
148 return POLLIN | POLLRDNORM;
149 if (!list->hidraw->exist)
150 return POLLERR | POLLHUP;
151 return 0;
152}
153
154static int hidraw_open(struct inode *inode, struct file *file)
155{
156 unsigned int minor = iminor(inode);
157 struct hidraw *dev;
158 struct hidraw_list *list;
159 int err = 0;
160
161 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
162 err = -ENOMEM;
163 goto out;
164 }
165
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100166 lock_kernel();
167 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200168 if (!hidraw_table[minor]) {
169 printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
170 minor);
171 kfree(list);
172 err = -ENODEV;
173 goto out_unlock;
174 }
175
176 list->hidraw = hidraw_table[minor];
177 mutex_init(&list->read_mutex);
178 list_add_tail(&list->node, &hidraw_table[minor]->list);
179 file->private_data = list;
180
181 dev = hidraw_table[minor];
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100182 if (!dev->open++) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100183 if (dev->hid->ll_driver->power) {
184 err = dev->hid->ll_driver->power(dev->hid, PM_HINT_FULLON);
185 if (err < 0)
186 goto out_unlock;
187 }
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100188 err = dev->hid->ll_driver->open(dev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100189 if (err < 0) {
190 if (dev->hid->ll_driver->power)
191 dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100192 dev->open--;
Oliver Neukum0361a282008-12-17 15:38:03 +0100193 }
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100194 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200195
196out_unlock:
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100197 mutex_unlock(&minors_lock);
Jonathan Corbet702e57d2008-05-15 10:25:44 -0600198 unlock_kernel();
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100199out:
Jiri Kosina86166b72007-05-14 09:57:40 +0200200 return err;
201
202}
203
204static int hidraw_release(struct inode * inode, struct file * file)
205{
206 unsigned int minor = iminor(inode);
207 struct hidraw *dev;
208 struct hidraw_list *list = file->private_data;
209
210 if (!hidraw_table[minor]) {
211 printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
212 minor);
213 return -ENODEV;
214 }
215
216 list_del(&list->node);
217 dev = hidraw_table[minor];
Oliver Neukumb8a832b2008-12-15 13:12:08 +0100218 if (!--dev->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100219 if (list->hidraw->exist) {
220 if (dev->hid->ll_driver->power)
221 dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
Jiri Slabyc500c972008-05-16 11:49:16 +0200222 dev->hid->ll_driver->close(dev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100223 } else {
Jiri Kosina86166b72007-05-14 09:57:40 +0200224 kfree(list->hidraw);
Oliver Neukum0361a282008-12-17 15:38:03 +0100225 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200226 }
227
Jiri Kosina4db1c622008-06-24 14:45:27 +0200228 kfree(list);
229
Jiri Kosina86166b72007-05-14 09:57:40 +0200230 return 0;
231}
232
Alan Cox979c4072008-05-26 11:25:26 +0200233static long hidraw_ioctl(struct file *file, unsigned int cmd,
234 unsigned long arg)
Jiri Kosina86166b72007-05-14 09:57:40 +0200235{
Alan Cox979c4072008-05-26 11:25:26 +0200236 struct inode *inode = file->f_path.dentry->d_inode;
Jiri Kosina86166b72007-05-14 09:57:40 +0200237 unsigned int minor = iminor(inode);
Alan Cox979c4072008-05-26 11:25:26 +0200238 long ret = 0;
239 /* FIXME: What stops hidraw_table going NULL */
Jiri Kosina86166b72007-05-14 09:57:40 +0200240 struct hidraw *dev = hidraw_table[minor];
241 void __user *user_arg = (void __user*) arg;
242
Alan Cox979c4072008-05-26 11:25:26 +0200243 lock_kernel();
Jiri Kosina86166b72007-05-14 09:57:40 +0200244 switch (cmd) {
245 case HIDIOCGRDESCSIZE:
246 if (put_user(dev->hid->rsize, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200247 ret = -EFAULT;
248 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200249
250 case HIDIOCGRDESC:
251 {
252 __u32 len;
253
254 if (get_user(len, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200255 ret = -EFAULT;
256 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
257 ret = -EINVAL;
258 else if (copy_to_user(user_arg + offsetof(
259 struct hidraw_report_descriptor,
260 value[0]),
261 dev->hid->rdesc,
262 min(dev->hid->rsize, len)))
263 ret = -EFAULT;
264 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200265 }
266 case HIDIOCGRAWINFO:
267 {
268 struct hidraw_devinfo dinfo;
269
270 dinfo.bustype = dev->hid->bus;
271 dinfo.vendor = dev->hid->vendor;
272 dinfo.product = dev->hid->product;
273 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
Alan Cox979c4072008-05-26 11:25:26 +0200274 ret = -EFAULT;
275 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200276 }
277 default:
Jiri Kosina9188e792008-11-12 16:14:08 +0100278 {
279 struct hid_device *hid = dev->hid;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300280 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
281 ret = -EINVAL;
282 break;
283 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100284
285 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
286 int len;
Dan Carpenter38089c62009-04-07 16:35:56 +0200287 if (!hid->name) {
288 ret = 0;
289 break;
290 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100291 len = strlen(hid->name) + 1;
292 if (len > _IOC_SIZE(cmd))
293 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300294 ret = copy_to_user(user_arg, hid->name, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100295 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300296 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100297 }
298
299 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
300 int len;
Dan Carpenter38089c62009-04-07 16:35:56 +0200301 if (!hid->phys) {
302 ret = 0;
303 break;
304 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100305 len = strlen(hid->phys) + 1;
306 if (len > _IOC_SIZE(cmd))
307 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300308 ret = copy_to_user(user_arg, hid->phys, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100309 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300310 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100311 }
312 }
313
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300314 ret = -ENOTTY;
Jiri Kosina86166b72007-05-14 09:57:40 +0200315 }
Jiri Slaby0f2c5942008-10-23 01:47:30 +0200316 unlock_kernel();
Alan Cox979c4072008-05-26 11:25:26 +0200317 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200318}
319
320static const struct file_operations hidraw_ops = {
321 .owner = THIS_MODULE,
322 .read = hidraw_read,
323 .write = hidraw_write,
324 .poll = hidraw_poll,
325 .open = hidraw_open,
326 .release = hidraw_release,
Alan Cox979c4072008-05-26 11:25:26 +0200327 .unlocked_ioctl = hidraw_ioctl,
Jiri Kosina86166b72007-05-14 09:57:40 +0200328};
329
330void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
331{
332 struct hidraw *dev = hid->hidraw;
333 struct hidraw_list *list;
334
335 list_for_each_entry(list, &dev->list, node) {
336 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
337 list->buffer[list->head].len = len;
338 list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
339 kill_fasync(&list->fasync, SIGIO, POLL_IN);
340 }
341
342 wake_up_interruptible(&dev->wait);
343}
344EXPORT_SYMBOL_GPL(hidraw_report_event);
345
346int hidraw_connect(struct hid_device *hid)
347{
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200348 int minor, result;
Jiri Kosina86166b72007-05-14 09:57:40 +0200349 struct hidraw *dev;
350
Jiri Kosinabbe281f2009-06-04 15:44:25 +0200351 /* we accept any HID device, no matter the applications */
Jiri Kosina86166b72007-05-14 09:57:40 +0200352
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200353 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
354 if (!dev)
355 return -ENOMEM;
356
357 result = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200358
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100359 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200360
361 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
362 if (hidraw_table[minor])
363 continue;
364 hidraw_table[minor] = dev;
365 result = 0;
366 break;
367 }
368
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200369 if (result) {
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100370 mutex_unlock(&minors_lock);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200371 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200372 goto out;
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200373 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200374
Jiri Kosinaaae6c282008-12-04 16:16:46 +0100375 dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700376 NULL, "%s%d", "hidraw", minor);
Jiri Kosina86166b72007-05-14 09:57:40 +0200377
378 if (IS_ERR(dev->dev)) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200379 hidraw_table[minor] = NULL;
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100380 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200381 result = PTR_ERR(dev->dev);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200382 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200383 goto out;
384 }
385
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100386 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200387 init_waitqueue_head(&dev->wait);
388 INIT_LIST_HEAD(&dev->list);
389
390 dev->hid = hid;
391 dev->minor = minor;
392
393 dev->exist = 1;
394 hid->hidraw = dev;
395
396out:
397 return result;
398
399}
400EXPORT_SYMBOL_GPL(hidraw_connect);
401
402void hidraw_disconnect(struct hid_device *hid)
403{
404 struct hidraw *hidraw = hid->hidraw;
405
406 hidraw->exist = 0;
407
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100408 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200409 hidraw_table[hidraw->minor] = NULL;
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100410 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200411
412 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
413
414 if (hidraw->open) {
Jiri Slabyc500c972008-05-16 11:49:16 +0200415 hid->ll_driver->close(hid);
Jiri Kosina86166b72007-05-14 09:57:40 +0200416 wake_up_interruptible(&hidraw->wait);
417 } else {
418 kfree(hidraw);
419 }
420}
421EXPORT_SYMBOL_GPL(hidraw_disconnect);
422
423int __init hidraw_init(void)
424{
425 int result;
426 dev_t dev_id;
427
428 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
429 HIDRAW_MAX_DEVICES, "hidraw");
430
431 hidraw_major = MAJOR(dev_id);
432
433 if (result < 0) {
434 printk(KERN_WARNING "hidraw: can't get major number\n");
435 result = 0;
436 goto out;
437 }
438
439 hidraw_class = class_create(THIS_MODULE, "hidraw");
440 if (IS_ERR(hidraw_class)) {
441 result = PTR_ERR(hidraw_class);
442 unregister_chrdev(hidraw_major, "hidraw");
443 goto out;
444 }
445
446 cdev_init(&hidraw_cdev, &hidraw_ops);
447 cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
448out:
449 return result;
450}
451
Jiri Slaby140ae3e2008-10-17 18:04:48 +0200452void hidraw_exit(void)
Jiri Kosina86166b72007-05-14 09:57:40 +0200453{
454 dev_t dev_id = MKDEV(hidraw_major, 0);
455
456 cdev_del(&hidraw_cdev);
457 class_destroy(hidraw_class);
458 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
459
460}