OSDN Git Service

usb: core: Add PM runtime calls to usb_hcd_platform_shutdown
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / hid / hidraw.c
1 /*
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-2014 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
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>
37
38 #include <linux/hidraw.h>
39
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);
45
46 static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
47 {
48         struct hidraw_list *list = file->private_data;
49         int ret = 0, len;
50         DECLARE_WAITQUEUE(wait, current);
51
52         mutex_lock(&list->read_mutex);
53
54         while (ret == 0) {
55                 if (list->head == list->tail) {
56                         add_wait_queue(&list->hidraw->wait, &wait);
57                         set_current_state(TASK_INTERRUPTIBLE);
58
59                         while (list->head == list->tail) {
60                                 if (signal_pending(current)) {
61                                         ret = -ERESTARTSYS;
62                                         break;
63                                 }
64                                 if (!list->hidraw->exist) {
65                                         ret = -EIO;
66                                         break;
67                                 }
68                                 if (file->f_flags & O_NONBLOCK) {
69                                         ret = -EAGAIN;
70                                         break;
71                                 }
72
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);
78                         }
79
80                         set_current_state(TASK_RUNNING);
81                         remove_wait_queue(&list->hidraw->wait, &wait);
82                 }
83
84                 if (ret)
85                         goto out;
86
87                 len = list->buffer[list->tail].len > count ?
88                         count : list->buffer[list->tail].len;
89
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;
94                         }
95                         ret = len;
96                 }
97
98                 kfree(list->buffer[list->tail].value);
99                 list->buffer[list->tail].value = NULL;
100                 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
101         }
102 out:
103         mutex_unlock(&list->read_mutex);
104         return ret;
105 }
106
107 /*
108  * The first byte of the report buffer is expected to be a report number.
109  *
110  * This function is to be called with the minors_lock mutex held.
111  */
112 static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
113 {
114         unsigned int minor = iminor(file_inode(file));
115         struct hid_device *dev;
116         __u8 *buf;
117         int ret = 0;
118
119         if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
120                 ret = -ENODEV;
121                 goto out;
122         }
123
124         dev = hidraw_table[minor]->hid;
125
126
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;
132         }
133
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;
139         }
140
141         buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
142         if (!buf) {
143                 ret = -ENOMEM;
144                 goto out;
145         }
146
147         if (copy_from_user(buf, buffer, count)) {
148                 ret = -EFAULT;
149                 goto out_free;
150         }
151
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);
155                 /*
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.
159                  */
160                 if (ret != -ENOSYS)
161                         goto out_free;
162         }
163
164         ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
165                                 HID_REQ_SET_REPORT);
166
167 out_free:
168         kfree(buf);
169 out:
170         return ret;
171 }
172
173 static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
174 {
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;
180 }
181
182
183 /*
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.
189  *
190  * This function is to be called with the minors_lock mutex held.
191  */
192 static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
193 {
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;
199
200         if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
201                 ret = -ENODEV;
202                 goto out;
203         }
204
205         dev = hidraw_table[minor]->hid;
206
207         if (!dev->ll_driver->raw_request) {
208                 ret = -ENODEV;
209                 goto out;
210         }
211
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;
217         }
218
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;
224         }
225
226         buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
227         if (!buf) {
228                 ret = -ENOMEM;
229                 goto out;
230         }
231
232         /*
233          * Read the first byte from the user. This is the report number,
234          * which is passed to hid_hw_raw_request().
235          */
236         if (copy_from_user(&report_number, buffer, 1)) {
237                 ret = -EFAULT;
238                 goto out_free;
239         }
240
241         ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
242                                  HID_REQ_GET_REPORT);
243
244         if (ret < 0)
245                 goto out_free;
246
247         len = (ret < count) ? ret : count;
248
249         if (copy_to_user(buffer, buf, len)) {
250                 ret = -EFAULT;
251                 goto out_free;
252         }
253
254         ret = len;
255
256 out_free:
257         kfree(buf);
258 out:
259         return ret;
260 }
261
262 static unsigned int hidraw_poll(struct file *file, poll_table *wait)
263 {
264         struct hidraw_list *list = file->private_data;
265
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;
272 }
273
274 static int hidraw_open(struct inode *inode, struct file *file)
275 {
276         unsigned int minor = iminor(inode);
277         struct hidraw *dev;
278         struct hidraw_list *list;
279         unsigned long flags;
280         int err = 0;
281
282         if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
283                 err = -ENOMEM;
284                 goto out;
285         }
286
287         mutex_lock(&minors_lock);
288         if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
289                 err = -ENODEV;
290                 goto out_unlock;
291         }
292
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;
299                 }
300
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;
306                 }
307         }
308
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;
321
322 }
323
324 static int hidraw_fasync(int fd, struct file *file, int on)
325 {
326         struct hidraw_list *list = file->private_data;
327
328         return fasync_helper(fd, file, on, &list->fasync);
329 }
330
331 static void drop_ref(struct hidraw *hidraw, int exists_bit)
332 {
333         if (exists_bit) {
334                 hidraw->exist = 0;
335                 if (hidraw->open) {
336                         hid_hw_close(hidraw->hid);
337                         wake_up_interruptible(&hidraw->wait);
338                 }
339                 device_destroy(hidraw_class,
340                                MKDEV(hidraw_major, hidraw->minor));
341         } else {
342                 --hidraw->open;
343         }
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);
352                 }
353         }
354 }
355
356 static int hidraw_release(struct inode * inode, struct file * file)
357 {
358         unsigned int minor = iminor(inode);
359         struct hidraw_list *list = file->private_data;
360         unsigned long flags;
361
362         mutex_lock(&minors_lock);
363
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);
368
369         drop_ref(hidraw_table[minor], 0);
370
371         mutex_unlock(&minors_lock);
372         return 0;
373 }
374
375 static long hidraw_ioctl(struct file *file, unsigned int cmd,
376                                                         unsigned long arg)
377 {
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;
383
384         mutex_lock(&minors_lock);
385         dev = hidraw_table[minor];
386         if (!dev) {
387                 ret = -ENODEV;
388                 goto out;
389         }
390
391         switch (cmd) {
392                 case HIDIOCGRDESCSIZE:
393                         if (put_user(dev->hid->rsize, (int __user *)arg))
394                                 ret = -EFAULT;
395                         break;
396
397                 case HIDIOCGRDESC:
398                         {
399                                 __u32 len;
400
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;
412                         }
413                 case HIDIOCGRAWINFO:
414                         {
415                                 struct hidraw_devinfo dinfo;
416
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;
423                         }
424                 default:
425                         {
426                                 struct hid_device *hid = dev->hid;
427                                 if (_IOC_TYPE(cmd) != 'H') {
428                                         ret = -EINVAL;
429                                         break;
430                                 }
431
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;
436                                 }
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;
441                                 }
442
443                                 /* Begin Read-only ioctls. */
444                                 if (_IOC_DIR(cmd) != _IOC_READ) {
445                                         ret = -EINVAL;
446                                         break;
447                                 }
448
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;
456                                 }
457
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;
465                                 }
466                         }
467
468                 ret = -ENOTTY;
469         }
470 out:
471         mutex_unlock(&minors_lock);
472         return ret;
473 }
474
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,
488 };
489
490 int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
491 {
492         struct hidraw *dev = hid->hidraw;
493         struct hidraw_list *list;
494         int ret = 0;
495         unsigned long flags;
496
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);
500
501                 if (new_head == list->tail)
502                         continue;
503
504                 if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
505                         ret = -ENOMEM;
506                         break;
507                 }
508                 list->buffer[list->head].len = len;
509                 list->head = new_head;
510                 kill_fasync(&list->fasync, SIGIO, POLL_IN);
511         }
512         spin_unlock_irqrestore(&dev->list_lock, flags);
513
514         wake_up_interruptible(&dev->wait);
515         return ret;
516 }
517 EXPORT_SYMBOL_GPL(hidraw_report_event);
518
519 int hidraw_connect(struct hid_device *hid)
520 {
521         int minor, result;
522         struct hidraw *dev;
523
524         /* we accept any HID device, all applications */
525
526         dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
527         if (!dev)
528                 return -ENOMEM;
529
530         result = -EINVAL;
531
532         mutex_lock(&minors_lock);
533
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;
540         }
541
542         if (result) {
543                 mutex_unlock(&minors_lock);
544                 kfree(dev);
545                 goto out;
546         }
547
548         dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
549                                  NULL, "%s%d", "hidraw", minor);
550
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;
557         }
558
559         init_waitqueue_head(&dev->wait);
560         spin_lock_init(&dev->list_lock);
561         INIT_LIST_HEAD(&dev->list);
562
563         dev->hid = hid;
564         dev->minor = minor;
565
566         dev->exist = 1;
567         hid->hidraw = dev;
568
569         mutex_unlock(&minors_lock);
570 out:
571         return result;
572
573 }
574 EXPORT_SYMBOL_GPL(hidraw_connect);
575
576 void hidraw_disconnect(struct hid_device *hid)
577 {
578         struct hidraw *hidraw = hid->hidraw;
579
580         mutex_lock(&minors_lock);
581
582         drop_ref(hidraw, 1);
583
584         mutex_unlock(&minors_lock);
585 }
586 EXPORT_SYMBOL_GPL(hidraw_disconnect);
587
588 int __init hidraw_init(void)
589 {
590         int result;
591         dev_t dev_id;
592
593         result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
594                         HIDRAW_MAX_DEVICES, "hidraw");
595
596         hidraw_major = MAJOR(dev_id);
597
598         if (result < 0) {
599                 pr_warn("can't get major number\n");
600                 goto out;
601         }
602
603         hidraw_class = class_create(THIS_MODULE, "hidraw");
604         if (IS_ERR(hidraw_class)) {
605                 result = PTR_ERR(hidraw_class);
606                 goto error_cdev;
607         }
608
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;
613
614         printk(KERN_INFO "hidraw: raw HID events driver (C) Jiri Kosina\n");
615 out:
616         return result;
617
618 error_class:
619         class_destroy(hidraw_class);
620 error_cdev:
621         unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
622         goto out;
623 }
624
625 void hidraw_exit(void)
626 {
627         dev_t dev_id = MKDEV(hidraw_major, 0);
628
629         cdev_del(&hidraw_cdev);
630         class_destroy(hidraw_class);
631         unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
632
633 }