HID: fix possible deadlock in hidraw_read
[cascardo/linux.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 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>
33 #include <linux/smp_lock.h>
34
35 #include <linux/hidraw.h>
36
37 static int hidraw_major;
38 static struct cdev hidraw_cdev;
39 static struct class *hidraw_class;
40 static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
41 static DEFINE_MUTEX(minors_lock);
42
43 static 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
50         mutex_lock(&list->read_mutex);
51
52         while (ret == 0) {
53                 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         }
98 out:
99         mutex_unlock(&list->read_mutex);
100         return ret;
101 }
102
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)
105 {
106         unsigned int minor = iminor(file->f_path.dentry->d_inode);
107         /* FIXME: What stops hidraw_table going NULL */
108         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
115         if (count > HID_MAX_BUFFER_SIZE) {
116                 printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
117                                 task_pid_nr(current));
118                 return -EINVAL;
119         }
120
121         if (count < 2) {
122                 printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
123                                 task_pid_nr(current));
124                 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);
137 out:
138         kfree(buf);
139         return ret;
140 }
141
142 static 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
154 static 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
166         lock_kernel();
167         mutex_lock(&minors_lock);
168         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];
182         if (!dev->open++) {
183                 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                 }
188                 err = dev->hid->ll_driver->open(dev->hid);
189                 if (err < 0) {
190                         if (dev->hid->ll_driver->power)
191                                 dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
192                         dev->open--;
193                 }
194         }
195
196 out_unlock:
197         mutex_unlock(&minors_lock);
198         unlock_kernel();
199 out:
200         return err;
201
202 }
203
204 static 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];
218         if (!--dev->open) {
219                 if (list->hidraw->exist) {
220                         if (dev->hid->ll_driver->power)
221                                 dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
222                         dev->hid->ll_driver->close(dev->hid);
223                 } else {
224                         kfree(list->hidraw);
225                 }
226         }
227
228         kfree(list);
229
230         return 0;
231 }
232
233 static long hidraw_ioctl(struct file *file, unsigned int cmd,
234                                                         unsigned long arg)
235 {
236         struct inode *inode = file->f_path.dentry->d_inode;
237         unsigned int minor = iminor(inode);
238         long ret = 0;
239         /* FIXME: What stops hidraw_table going NULL */
240         struct hidraw *dev = hidraw_table[minor];
241         void __user *user_arg = (void __user*) arg;
242
243         lock_kernel();
244         switch (cmd) {
245                 case HIDIOCGRDESCSIZE:
246                         if (put_user(dev->hid->rsize, (int __user *)arg))
247                                 ret = -EFAULT;
248                         break;
249
250                 case HIDIOCGRDESC:
251                         {
252                                 __u32 len;
253
254                                 if (get_user(len, (int __user *)arg))
255                                         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;
265                         }
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)))
274                                         ret = -EFAULT;
275                                 break;
276                         }
277                 default:
278                         {
279                                 struct hid_device *hid = dev->hid;
280                                 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
281                                         ret = -EINVAL;
282                                         break;
283                                 }
284
285                                 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
286                                         int len;
287                                         if (!hid->name) {
288                                                 ret = 0;
289                                                 break;
290                                         }
291                                         len = strlen(hid->name) + 1;
292                                         if (len > _IOC_SIZE(cmd))
293                                                 len = _IOC_SIZE(cmd);
294                                         ret = copy_to_user(user_arg, hid->name, len) ?
295                                                 -EFAULT : len;
296                                         break;
297                                 }
298
299                                 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
300                                         int len;
301                                         if (!hid->phys) {
302                                                 ret = 0;
303                                                 break;
304                                         }
305                                         len = strlen(hid->phys) + 1;
306                                         if (len > _IOC_SIZE(cmd))
307                                                 len = _IOC_SIZE(cmd);
308                                         ret = copy_to_user(user_arg, hid->phys, len) ?
309                                                 -EFAULT : len;
310                                         break;
311                                 }
312                 }
313
314                 ret = -ENOTTY;
315         }
316         unlock_kernel();
317         return ret;
318 }
319
320 static 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,
327         .unlocked_ioctl = hidraw_ioctl,
328 };
329
330 void 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 }
344 EXPORT_SYMBOL_GPL(hidraw_report_event);
345
346 int hidraw_connect(struct hid_device *hid)
347 {
348         int minor, result;
349         struct hidraw *dev;
350
351         /* we accept any HID device, no matter the applications */
352
353         dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
354         if (!dev)
355                 return -ENOMEM;
356
357         result = -EINVAL;
358
359         mutex_lock(&minors_lock);
360
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
369         if (result) {
370                 mutex_unlock(&minors_lock);
371                 kfree(dev);
372                 goto out;
373         }
374
375         dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
376                                  NULL, "%s%d", "hidraw", minor);
377
378         if (IS_ERR(dev->dev)) {
379                 hidraw_table[minor] = NULL;
380                 mutex_unlock(&minors_lock);
381                 result = PTR_ERR(dev->dev);
382                 kfree(dev);
383                 goto out;
384         }
385
386         mutex_unlock(&minors_lock);
387         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
396 out:
397         return result;
398
399 }
400 EXPORT_SYMBOL_GPL(hidraw_connect);
401
402 void hidraw_disconnect(struct hid_device *hid)
403 {
404         struct hidraw *hidraw = hid->hidraw;
405
406         hidraw->exist = 0;
407
408         mutex_lock(&minors_lock);
409         hidraw_table[hidraw->minor] = NULL;
410         mutex_unlock(&minors_lock);
411
412         device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
413
414         if (hidraw->open) {
415                 hid->ll_driver->close(hid);
416                 wake_up_interruptible(&hidraw->wait);
417         } else {
418                 kfree(hidraw);
419         }
420 }
421 EXPORT_SYMBOL_GPL(hidraw_disconnect);
422
423 int __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);
448 out:
449         return result;
450 }
451
452 void hidraw_exit(void)
453 {
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 }