dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / hid / hidraw.c
blobc0c4df1987259b7486f12a5cd3f6c64a7bfba342
1 /*
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-2014 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
24 #include <linux/fs.h>
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;
49 int ret = 0, len;
50 DECLARE_WAITQUEUE(wait, current);
52 mutex_lock(&list->read_mutex);
54 while (ret == 0) {
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 (signal_pending(current)) {
61 ret = -ERESTARTSYS;
62 break;
64 if (!list->hidraw->exist) {
65 ret = -EIO;
66 break;
68 if (file->f_flags & O_NONBLOCK) {
69 ret = -EAGAIN;
70 break;
73 /* allow O_NONBLOCK to work well from other threads */
74 mutex_unlock(&list->read_mutex);
75 schedule();
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);
84 if (ret)
85 goto out;
87 len = list->buffer[list->tail].len > count ?
88 count : list->buffer[list->tail].len;
90 if (list->buffer[list->tail].value) {
91 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
92 ret = -EFAULT;
93 goto out;
95 ret = len;
98 kfree(list->buffer[list->tail].value);
99 list->buffer[list->tail].value = NULL;
100 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
102 out:
103 mutex_unlock(&list->read_mutex);
104 return ret;
108 * The first byte of the report buffer is expected to be a report number.
110 * This function is to be called with the minors_lock mutex held.
112 static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
114 unsigned int minor = iminor(file_inode(file));
115 struct hid_device *dev;
116 __u8 *buf;
117 int ret = 0;
119 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
120 ret = -ENODEV;
121 goto out;
124 dev = hidraw_table[minor]->hid;
127 if (count > HID_MAX_BUFFER_SIZE) {
128 hid_warn(dev, "pid %d passed too large report\n",
129 task_pid_nr(current));
130 ret = -EINVAL;
131 goto out;
134 if (count < 2) {
135 hid_warn(dev, "pid %d passed too short report\n",
136 task_pid_nr(current));
137 ret = -EINVAL;
138 goto out;
141 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
142 if (!buf) {
143 ret = -ENOMEM;
144 goto out;
147 if (copy_from_user(buf, buffer, count)) {
148 ret = -EFAULT;
149 goto out_free;
152 if ((report_type == HID_OUTPUT_REPORT) &&
153 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
154 ret = hid_hw_output_report(dev, buf, count);
156 * compatibility with old implementation of USB-HID and I2C-HID:
157 * if the device does not support receiving output reports,
158 * on an interrupt endpoint, fallback to SET_REPORT HID command.
160 if (ret != -ENOSYS)
161 goto out_free;
164 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
165 HID_REQ_SET_REPORT);
167 out_free:
168 kfree(buf);
169 out:
170 return ret;
173 static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
175 ssize_t ret;
176 mutex_lock(&minors_lock);
177 ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
178 mutex_unlock(&minors_lock);
179 return ret;
184 * This function performs a Get_Report transfer over the control endpoint
185 * per section 7.2.1 of the HID specification, version 1.1. The first byte
186 * of buffer is the report number to request, or 0x0 if the defice does not
187 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
188 * or HID_INPUT_REPORT.
190 * This function is to be called with the minors_lock mutex held.
192 static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
194 unsigned int minor = iminor(file_inode(file));
195 struct hid_device *dev;
196 __u8 *buf;
197 int ret = 0, len;
198 unsigned char report_number;
200 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
201 ret = -ENODEV;
202 goto out;
205 dev = hidraw_table[minor]->hid;
207 if (!dev->ll_driver->raw_request) {
208 ret = -ENODEV;
209 goto out;
212 if (count > HID_MAX_BUFFER_SIZE) {
213 printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
214 task_pid_nr(current));
215 ret = -EINVAL;
216 goto out;
219 if (count < 2) {
220 printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
221 task_pid_nr(current));
222 ret = -EINVAL;
223 goto out;
226 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
227 if (!buf) {
228 ret = -ENOMEM;
229 goto out;
233 * Read the first byte from the user. This is the report number,
234 * which is passed to hid_hw_raw_request().
236 if (copy_from_user(&report_number, buffer, 1)) {
237 ret = -EFAULT;
238 goto out_free;
241 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
242 HID_REQ_GET_REPORT);
244 if (ret < 0)
245 goto out_free;
247 len = (ret < count) ? ret : count;
249 if (copy_to_user(buffer, buf, len)) {
250 ret = -EFAULT;
251 goto out_free;
254 ret = len;
256 out_free:
257 kfree(buf);
258 out:
259 return ret;
262 static unsigned int hidraw_poll(struct file *file, poll_table *wait)
264 struct hidraw_list *list = file->private_data;
266 poll_wait(file, &list->hidraw->wait, wait);
267 if (list->head != list->tail)
268 return POLLIN | POLLRDNORM;
269 if (!list->hidraw->exist)
270 return POLLERR | POLLHUP;
271 return 0;
274 static int hidraw_open(struct inode *inode, struct file *file)
276 unsigned int minor = iminor(inode);
277 struct hidraw *dev;
278 struct hidraw_list *list;
279 unsigned long flags;
280 int err = 0;
282 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
283 err = -ENOMEM;
284 goto out;
287 mutex_lock(&minors_lock);
288 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
289 err = -ENODEV;
290 goto out_unlock;
293 dev = hidraw_table[minor];
294 if (!dev->open++) {
295 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
296 if (err < 0) {
297 dev->open--;
298 goto out_unlock;
301 err = hid_hw_open(dev->hid);
302 if (err < 0) {
303 hid_hw_power(dev->hid, PM_HINT_NORMAL);
304 dev->open--;
305 goto out_unlock;
309 list->hidraw = hidraw_table[minor];
310 mutex_init(&list->read_mutex);
311 spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
312 list_add_tail(&list->node, &hidraw_table[minor]->list);
313 spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
314 file->private_data = list;
315 out_unlock:
316 mutex_unlock(&minors_lock);
317 out:
318 if (err < 0)
319 kfree(list);
320 return err;
324 static int hidraw_fasync(int fd, struct file *file, int on)
326 struct hidraw_list *list = file->private_data;
328 return fasync_helper(fd, file, on, &list->fasync);
331 static void drop_ref(struct hidraw *hidraw, int exists_bit)
333 if (exists_bit) {
334 hidraw->exist = 0;
335 if (hidraw->open) {
336 hid_hw_close(hidraw->hid);
337 wake_up_interruptible(&hidraw->wait);
339 device_destroy(hidraw_class,
340 MKDEV(hidraw_major, hidraw->minor));
341 } else {
342 --hidraw->open;
344 if (!hidraw->open) {
345 if (!hidraw->exist) {
346 hidraw_table[hidraw->minor] = NULL;
347 kfree(hidraw);
348 } else {
349 /* close device for last reader */
350 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
351 hid_hw_close(hidraw->hid);
356 static int hidraw_release(struct inode * inode, struct file * file)
358 unsigned int minor = iminor(inode);
359 struct hidraw_list *list = file->private_data;
360 unsigned long flags;
362 mutex_lock(&minors_lock);
364 spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
365 list_del(&list->node);
366 spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
367 kfree(list);
369 drop_ref(hidraw_table[minor], 0);
371 mutex_unlock(&minors_lock);
372 return 0;
375 static long hidraw_ioctl(struct file *file, unsigned int cmd,
376 unsigned long arg)
378 struct inode *inode = file_inode(file);
379 unsigned int minor = iminor(inode);
380 long ret = 0;
381 struct hidraw *dev;
382 void __user *user_arg = (void __user*) arg;
384 mutex_lock(&minors_lock);
385 dev = hidraw_table[minor];
386 if (!dev) {
387 ret = -ENODEV;
388 goto out;
391 switch (cmd) {
392 case HIDIOCGRDESCSIZE:
393 if (put_user(dev->hid->rsize, (int __user *)arg))
394 ret = -EFAULT;
395 break;
397 case HIDIOCGRDESC:
399 __u32 len;
401 if (get_user(len, (int __user *)arg))
402 ret = -EFAULT;
403 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
404 ret = -EINVAL;
405 else if (copy_to_user(user_arg + offsetof(
406 struct hidraw_report_descriptor,
407 value[0]),
408 dev->hid->rdesc,
409 min(dev->hid->rsize, len)))
410 ret = -EFAULT;
411 break;
413 case HIDIOCGRAWINFO:
415 struct hidraw_devinfo dinfo;
417 dinfo.bustype = dev->hid->bus;
418 dinfo.vendor = dev->hid->vendor;
419 dinfo.product = dev->hid->product;
420 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
421 ret = -EFAULT;
422 break;
424 default:
426 struct hid_device *hid = dev->hid;
427 if (_IOC_TYPE(cmd) != 'H') {
428 ret = -EINVAL;
429 break;
432 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
433 int len = _IOC_SIZE(cmd);
434 ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
435 break;
437 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
438 int len = _IOC_SIZE(cmd);
439 ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
440 break;
443 /* Begin Read-only ioctls. */
444 if (_IOC_DIR(cmd) != _IOC_READ) {
445 ret = -EINVAL;
446 break;
449 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
450 int len = strlen(hid->name) + 1;
451 if (len > _IOC_SIZE(cmd))
452 len = _IOC_SIZE(cmd);
453 ret = copy_to_user(user_arg, hid->name, len) ?
454 -EFAULT : len;
455 break;
458 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
459 int len = strlen(hid->phys) + 1;
460 if (len > _IOC_SIZE(cmd))
461 len = _IOC_SIZE(cmd);
462 ret = copy_to_user(user_arg, hid->phys, len) ?
463 -EFAULT : len;
464 break;
468 ret = -ENOTTY;
470 out:
471 mutex_unlock(&minors_lock);
472 return ret;
475 static const struct file_operations hidraw_ops = {
476 .owner = THIS_MODULE,
477 .read = hidraw_read,
478 .write = hidraw_write,
479 .poll = hidraw_poll,
480 .open = hidraw_open,
481 .release = hidraw_release,
482 .unlocked_ioctl = hidraw_ioctl,
483 .fasync = hidraw_fasync,
484 #ifdef CONFIG_COMPAT
485 .compat_ioctl = hidraw_ioctl,
486 #endif
487 .llseek = noop_llseek,
490 int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
492 struct hidraw *dev = hid->hidraw;
493 struct hidraw_list *list;
494 int ret = 0;
495 unsigned long flags;
497 spin_lock_irqsave(&dev->list_lock, flags);
498 list_for_each_entry(list, &dev->list, node) {
499 int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
501 if (new_head == list->tail)
502 continue;
504 if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
505 ret = -ENOMEM;
506 break;
508 list->buffer[list->head].len = len;
509 list->head = new_head;
510 kill_fasync(&list->fasync, SIGIO, POLL_IN);
512 spin_unlock_irqrestore(&dev->list_lock, flags);
514 wake_up_interruptible(&dev->wait);
515 return ret;
517 EXPORT_SYMBOL_GPL(hidraw_report_event);
519 int hidraw_connect(struct hid_device *hid)
521 int minor, result;
522 struct hidraw *dev;
524 /* we accept any HID device, all applications */
526 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
527 if (!dev)
528 return -ENOMEM;
530 result = -EINVAL;
532 mutex_lock(&minors_lock);
534 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
535 if (hidraw_table[minor])
536 continue;
537 hidraw_table[minor] = dev;
538 result = 0;
539 break;
542 if (result) {
543 mutex_unlock(&minors_lock);
544 kfree(dev);
545 goto out;
548 dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
549 NULL, "%s%d", "hidraw", minor);
551 if (IS_ERR(dev->dev)) {
552 hidraw_table[minor] = NULL;
553 mutex_unlock(&minors_lock);
554 result = PTR_ERR(dev->dev);
555 kfree(dev);
556 goto out;
559 init_waitqueue_head(&dev->wait);
560 spin_lock_init(&dev->list_lock);
561 INIT_LIST_HEAD(&dev->list);
563 dev->hid = hid;
564 dev->minor = minor;
566 dev->exist = 1;
567 hid->hidraw = dev;
569 mutex_unlock(&minors_lock);
570 out:
571 return result;
574 EXPORT_SYMBOL_GPL(hidraw_connect);
576 void hidraw_disconnect(struct hid_device *hid)
578 struct hidraw *hidraw = hid->hidraw;
580 mutex_lock(&minors_lock);
582 drop_ref(hidraw, 1);
584 mutex_unlock(&minors_lock);
586 EXPORT_SYMBOL_GPL(hidraw_disconnect);
588 int __init hidraw_init(void)
590 int result;
591 dev_t dev_id;
593 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
594 HIDRAW_MAX_DEVICES, "hidraw");
596 hidraw_major = MAJOR(dev_id);
598 if (result < 0) {
599 pr_warn("can't get major number\n");
600 goto out;
603 hidraw_class = class_create(THIS_MODULE, "hidraw");
604 if (IS_ERR(hidraw_class)) {
605 result = PTR_ERR(hidraw_class);
606 goto error_cdev;
609 cdev_init(&hidraw_cdev, &hidraw_ops);
610 result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
611 if (result < 0)
612 goto error_class;
614 printk(KERN_INFO "hidraw: raw HID events driver (C) Jiri Kosina\n");
615 out:
616 return result;
618 error_class:
619 class_destroy(hidraw_class);
620 error_cdev:
621 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
622 goto out;
625 void hidraw_exit(void)
627 dev_t dev_id = MKDEV(hidraw_major, 0);
629 cdev_del(&hidraw_cdev);
630 class_destroy(hidraw_class);
631 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);