891ff531cf347726d87d909b475f2d15c98ff575
[linux-2.6.git] / drivers / hid / usbhid / hiddev.c
1 /*
2  *  Copyright (c) 2001 Paul Stewart
3  *  Copyright (c) 2001 Vojtech Pavlik
4  *
5  *  HID char devices, giving access to raw HID device events.
6  *
7  */
8
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * Should you need to contact me, the author, you can do so either by
25  * e-mail - mail your message to Paul Stewart <stewart@wetlogic.net>
26  */
27
28 #include <linux/poll.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/input.h>
33 #include <linux/usb.h>
34 #include <linux/hid.h>
35 #include <linux/hiddev.h>
36 #include <linux/compat.h>
37 #include "usbhid.h"
38
39 #ifdef CONFIG_USB_DYNAMIC_MINORS
40 #define HIDDEV_MINOR_BASE       0
41 #define HIDDEV_MINORS           256
42 #else
43 #define HIDDEV_MINOR_BASE       96
44 #define HIDDEV_MINORS           16
45 #endif
46 #define HIDDEV_BUFFER_SIZE      2048
47
48 struct hiddev {
49         int exist;
50         int open;
51         struct mutex existancelock;
52         wait_queue_head_t wait;
53         struct hid_device *hid;
54         struct list_head list;
55         spinlock_t list_lock;
56 };
57
58 struct hiddev_list {
59         struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
60         int head;
61         int tail;
62         unsigned flags;
63         struct fasync_struct *fasync;
64         struct hiddev *hiddev;
65         struct list_head node;
66         struct mutex thread_lock;
67 };
68
69 /*
70  * Find a report, given the report's type and ID.  The ID can be specified
71  * indirectly by REPORT_ID_FIRST (which returns the first report of the given
72  * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
73  * given type which follows old_id.
74  */
75 static struct hid_report *
76 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
77 {
78         unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
79         unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
80         struct hid_report_enum *report_enum;
81         struct hid_report *report;
82         struct list_head *list;
83
84         if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
85             rinfo->report_type > HID_REPORT_TYPE_MAX)
86                 return NULL;
87
88         report_enum = hid->report_enum +
89                 (rinfo->report_type - HID_REPORT_TYPE_MIN);
90
91         switch (flags) {
92         case 0: /* Nothing to do -- report_id is already set correctly */
93                 break;
94
95         case HID_REPORT_ID_FIRST:
96                 if (list_empty(&report_enum->report_list))
97                         return NULL;
98
99                 list = report_enum->report_list.next;
100                 report = list_entry(list, struct hid_report, list);
101                 rinfo->report_id = report->id;
102                 break;
103
104         case HID_REPORT_ID_NEXT:
105                 report = report_enum->report_id_hash[rid];
106                 if (!report)
107                         return NULL;
108
109                 list = report->list.next;
110                 if (list == &report_enum->report_list)
111                         return NULL;
112
113                 report = list_entry(list, struct hid_report, list);
114                 rinfo->report_id = report->id;
115                 break;
116
117         default:
118                 return NULL;
119         }
120
121         return report_enum->report_id_hash[rinfo->report_id];
122 }
123
124 /*
125  * Perform an exhaustive search of the report table for a usage, given its
126  * type and usage id.
127  */
128 static struct hid_field *
129 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
130 {
131         int i, j;
132         struct hid_report *report;
133         struct hid_report_enum *report_enum;
134         struct hid_field *field;
135
136         if (uref->report_type < HID_REPORT_TYPE_MIN ||
137             uref->report_type > HID_REPORT_TYPE_MAX)
138                 return NULL;
139
140         report_enum = hid->report_enum +
141                 (uref->report_type - HID_REPORT_TYPE_MIN);
142
143         list_for_each_entry(report, &report_enum->report_list, list) {
144                 for (i = 0; i < report->maxfield; i++) {
145                         field = report->field[i];
146                         for (j = 0; j < field->maxusage; j++) {
147                                 if (field->usage[j].hid == uref->usage_code) {
148                                         uref->report_id = report->id;
149                                         uref->field_index = i;
150                                         uref->usage_index = j;
151                                         return field;
152                                 }
153                         }
154                 }
155         }
156
157         return NULL;
158 }
159
160 static void hiddev_send_event(struct hid_device *hid,
161                               struct hiddev_usage_ref *uref)
162 {
163         struct hiddev *hiddev = hid->hiddev;
164         struct hiddev_list *list;
165         unsigned long flags;
166
167         spin_lock_irqsave(&hiddev->list_lock, flags);
168         list_for_each_entry(list, &hiddev->list, node) {
169                 if (uref->field_index != HID_FIELD_INDEX_NONE ||
170                     (list->flags & HIDDEV_FLAG_REPORT) != 0) {
171                         list->buffer[list->head] = *uref;
172                         list->head = (list->head + 1) &
173                                 (HIDDEV_BUFFER_SIZE - 1);
174                         kill_fasync(&list->fasync, SIGIO, POLL_IN);
175                 }
176         }
177         spin_unlock_irqrestore(&hiddev->list_lock, flags);
178
179         wake_up_interruptible(&hiddev->wait);
180 }
181
182 /*
183  * This is where hid.c calls into hiddev to pass an event that occurred over
184  * the interrupt pipe
185  */
186 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
187                       struct hid_usage *usage, __s32 value)
188 {
189         unsigned type = field->report_type;
190         struct hiddev_usage_ref uref;
191
192         uref.report_type =
193           (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
194           ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
195            ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
196         uref.report_id = field->report->id;
197         uref.field_index = field->index;
198         uref.usage_index = (usage - field->usage);
199         uref.usage_code = usage->hid;
200         uref.value = value;
201
202         hiddev_send_event(hid, &uref);
203 }
204 EXPORT_SYMBOL_GPL(hiddev_hid_event);
205
206 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
207 {
208         unsigned type = report->type;
209         struct hiddev_usage_ref uref;
210
211         memset(&uref, 0, sizeof(uref));
212         uref.report_type =
213           (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
214           ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
215            ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
216         uref.report_id = report->id;
217         uref.field_index = HID_FIELD_INDEX_NONE;
218
219         hiddev_send_event(hid, &uref);
220 }
221
222 /*
223  * fasync file op
224  */
225 static int hiddev_fasync(int fd, struct file *file, int on)
226 {
227         struct hiddev_list *list = file->private_data;
228
229         return fasync_helper(fd, file, on, &list->fasync);
230 }
231
232
233 /*
234  * release file op
235  */
236 static int hiddev_release(struct inode * inode, struct file * file)
237 {
238         struct hiddev_list *list = file->private_data;
239         unsigned long flags;
240
241         spin_lock_irqsave(&list->hiddev->list_lock, flags);
242         list_del(&list->node);
243         spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
244
245         if (!--list->hiddev->open) {
246                 if (list->hiddev->exist) {
247                         usbhid_close(list->hiddev->hid);
248                         usbhid_put_power(list->hiddev->hid);
249                 } else {
250                         kfree(list->hiddev);
251                 }
252         }
253
254         kfree(list);
255
256         return 0;
257 }
258
259 /*
260  * open file op
261  */
262 static int hiddev_open(struct inode *inode, struct file *file)
263 {
264         struct hiddev_list *list;
265         struct usb_interface *intf;
266         struct hid_device *hid;
267         struct hiddev *hiddev;
268         int res;
269
270         intf = usbhid_find_interface(iminor(inode));
271         if (!intf)
272                 return -ENODEV;
273         hid = usb_get_intfdata(intf);
274         hiddev = hid->hiddev;
275
276         if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
277                 return -ENOMEM;
278         mutex_init(&list->thread_lock);
279         list->hiddev = hiddev;
280         file->private_data = list;
281
282         /*
283          * no need for locking because the USB major number
284          * is shared which usbcore guards against disconnect
285          */
286         if (list->hiddev->exist) {
287                 if (!list->hiddev->open++) {
288                         res = usbhid_open(hiddev->hid);
289                         if (res < 0) {
290                                 res = -EIO;
291                                 goto bail;
292                         }
293                 }
294         } else {
295                 res = -ENODEV;
296                 goto bail;
297         }
298
299         spin_lock_irq(&list->hiddev->list_lock);
300         list_add_tail(&list->node, &hiddev->list);
301         spin_unlock_irq(&list->hiddev->list_lock);
302
303         if (!list->hiddev->open++)
304                 if (list->hiddev->exist) {
305                         struct hid_device *hid = hiddev->hid;
306                         res = usbhid_get_power(hid);
307                         if (res < 0) {
308                                 res = -EIO;
309                                 goto bail;
310                         }
311                         usbhid_open(hid);
312                 }
313         return 0;
314 bail:
315         file->private_data = NULL;
316         kfree(list);
317         return res;
318 }
319
320 /*
321  * "write" file op
322  */
323 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
324 {
325         return -EINVAL;
326 }
327
328 /*
329  * "read" file op
330  */
331 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
332 {
333         DEFINE_WAIT(wait);
334         struct hiddev_list *list = file->private_data;
335         int event_size;
336         int retval;
337
338         event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
339                 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
340
341         if (count < event_size)
342                 return 0;
343
344         /* lock against other threads */
345         retval = mutex_lock_interruptible(&list->thread_lock);
346         if (retval)
347                 return -ERESTARTSYS;
348
349         while (retval == 0) {
350                 if (list->head == list->tail) {
351                         prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
352
353                         while (list->head == list->tail) {
354                                 if (file->f_flags & O_NONBLOCK) {
355                                         retval = -EAGAIN;
356                                         break;
357                                 }
358                                 if (signal_pending(current)) {
359                                         retval = -ERESTARTSYS;
360                                         break;
361                                 }
362                                 if (!list->hiddev->exist) {
363                                         retval = -EIO;
364                                         break;
365                                 }
366
367                                 /* let O_NONBLOCK tasks run */
368                                 mutex_unlock(&list->thread_lock);
369                                 schedule();
370                                 if (mutex_lock_interruptible(&list->thread_lock)) {
371                                         finish_wait(&list->hiddev->wait, &wait);
372                                         return -EINTR;
373                                 }
374                                 set_current_state(TASK_INTERRUPTIBLE);
375                         }
376                         finish_wait(&list->hiddev->wait, &wait);
377
378                 }
379
380                 if (retval) {
381                         mutex_unlock(&list->thread_lock);
382                         return retval;
383                 }
384
385
386                 while (list->head != list->tail &&
387                        retval + event_size <= count) {
388                         if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
389                                 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
390                                         struct hiddev_event event;
391
392                                         event.hid = list->buffer[list->tail].usage_code;
393                                         event.value = list->buffer[list->tail].value;
394                                         if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
395                                                 mutex_unlock(&list->thread_lock);
396                                                 return -EFAULT;
397                                         }
398                                         retval += sizeof(struct hiddev_event);
399                                 }
400                         } else {
401                                 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
402                                     (list->flags & HIDDEV_FLAG_REPORT) != 0) {
403
404                                         if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
405                                                 mutex_unlock(&list->thread_lock);
406                                                 return -EFAULT;
407                                         }
408                                         retval += sizeof(struct hiddev_usage_ref);
409                                 }
410                         }
411                         list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
412                 }
413
414         }
415         mutex_unlock(&list->thread_lock);
416
417         return retval;
418 }
419
420 /*
421  * "poll" file op
422  * No kernel lock - fine
423  */
424 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
425 {
426         struct hiddev_list *list = file->private_data;
427
428         poll_wait(file, &list->hiddev->wait, wait);
429         if (list->head != list->tail)
430                 return POLLIN | POLLRDNORM;
431         if (!list->hiddev->exist)
432                 return POLLERR | POLLHUP;
433         return 0;
434 }
435
436 /*
437  * "ioctl" file op
438  */
439 static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
440 {
441         struct hid_device *hid = hiddev->hid;
442         struct hiddev_report_info rinfo;
443         struct hiddev_usage_ref_multi *uref_multi = NULL;
444         struct hiddev_usage_ref *uref;
445         struct hid_report *report;
446         struct hid_field *field;
447         int i;
448
449         uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
450         if (!uref_multi)
451                 return -ENOMEM;
452         uref = &uref_multi->uref;
453         if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
454                 if (copy_from_user(uref_multi, user_arg,
455                                    sizeof(*uref_multi)))
456                         goto fault;
457         } else {
458                 if (copy_from_user(uref, user_arg, sizeof(*uref)))
459                         goto fault;
460         }
461
462         switch (cmd) {
463         case HIDIOCGUCODE:
464                 rinfo.report_type = uref->report_type;
465                 rinfo.report_id = uref->report_id;
466                 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
467                         goto inval;
468
469                 if (uref->field_index >= report->maxfield)
470                         goto inval;
471
472                 field = report->field[uref->field_index];
473                 if (uref->usage_index >= field->maxusage)
474                         goto inval;
475
476                 uref->usage_code = field->usage[uref->usage_index].hid;
477
478                 if (copy_to_user(user_arg, uref, sizeof(*uref)))
479                         goto fault;
480
481                 goto goodreturn;
482
483         default:
484                 if (cmd != HIDIOCGUSAGE &&
485                     cmd != HIDIOCGUSAGES &&
486                     uref->report_type == HID_REPORT_TYPE_INPUT)
487                         goto inval;
488
489                 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
490                         field = hiddev_lookup_usage(hid, uref);
491                         if (field == NULL)
492                                 goto inval;
493                 } else {
494                         rinfo.report_type = uref->report_type;
495                         rinfo.report_id = uref->report_id;
496                         if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
497                                 goto inval;
498
499                         if (uref->field_index >= report->maxfield)
500                                 goto inval;
501
502                         field = report->field[uref->field_index];
503
504                         if (cmd == HIDIOCGCOLLECTIONINDEX) {
505                                 if (uref->usage_index >= field->maxusage)
506                                         goto inval;
507                         } else if (uref->usage_index >= field->report_count)
508                                 goto inval;
509
510                         else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
511                                  (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
512                                   uref->usage_index + uref_multi->num_values > field->report_count))
513                                 goto inval;
514                         }
515
516                 switch (cmd) {
517                 case HIDIOCGUSAGE:
518                         uref->value = field->value[uref->usage_index];
519                         if (copy_to_user(user_arg, uref, sizeof(*uref)))
520                                 goto fault;
521                         goto goodreturn;
522
523                 case HIDIOCSUSAGE:
524                         field->value[uref->usage_index] = uref->value;
525                         goto goodreturn;
526
527                 case HIDIOCGCOLLECTIONINDEX:
528                         i = field->usage[uref->usage_index].collection_index;
529                         kfree(uref_multi);
530                         return i;
531                 case HIDIOCGUSAGES:
532                         for (i = 0; i < uref_multi->num_values; i++)
533                                 uref_multi->values[i] =
534                                     field->value[uref->usage_index + i];
535                         if (copy_to_user(user_arg, uref_multi,
536                                          sizeof(*uref_multi)))
537                                 goto fault;
538                         goto goodreturn;
539                 case HIDIOCSUSAGES:
540                         for (i = 0; i < uref_multi->num_values; i++)
541                                 field->value[uref->usage_index + i] =
542                                     uref_multi->values[i];
543                         goto goodreturn;
544                 }
545
546 goodreturn:
547                 kfree(uref_multi);
548                 return 0;
549 fault:
550                 kfree(uref_multi);
551                 return -EFAULT;
552 inval:
553                 kfree(uref_multi);
554                 return -EINVAL;
555         }
556 }
557
558 static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
559 {
560         struct hid_device *hid = hiddev->hid;
561         struct usb_device *dev = hid_to_usb_dev(hid);
562         int idx, len;
563         char *buf;
564
565         if (get_user(idx, (int __user *)user_arg))
566                 return -EFAULT;
567
568         if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
569                 return -ENOMEM;
570
571         if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
572                 kfree(buf);
573                 return -EINVAL;
574         }
575
576         if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
577                 kfree(buf);
578                 return -EFAULT;
579         }
580
581         kfree(buf);
582
583         return len;
584 }
585
586 static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
587 {
588         struct hiddev_list *list = file->private_data;
589         struct hiddev *hiddev = list->hiddev;
590         struct hid_device *hid;
591         struct hiddev_collection_info cinfo;
592         struct hiddev_report_info rinfo;
593         struct hiddev_field_info finfo;
594         struct hiddev_devinfo dinfo;
595         struct hid_report *report;
596         struct hid_field *field;
597         void __user *user_arg = (void __user *)arg;
598         int i, r = -EINVAL;
599
600         /* Called without BKL by compat methods so no BKL taken */
601
602         mutex_lock(&hiddev->existancelock);
603         if (!hiddev->exist) {
604                 r = -ENODEV;
605                 goto ret_unlock;
606         }
607
608         hid = hiddev->hid;
609
610         switch (cmd) {
611
612         case HIDIOCGVERSION:
613                 r = put_user(HID_VERSION, (int __user *)arg) ?
614                         -EFAULT : 0;
615                 break;
616
617         case HIDIOCAPPLICATION:
618                 if (arg < 0 || arg >= hid->maxapplication)
619                         break;
620
621                 for (i = 0; i < hid->maxcollection; i++)
622                         if (hid->collection[i].type ==
623                             HID_COLLECTION_APPLICATION && arg-- == 0)
624                                 break;
625
626                 if (i < hid->maxcollection)
627                         r = hid->collection[i].usage;
628                 break;
629
630         case HIDIOCGDEVINFO:
631                 {
632                         struct usb_device *dev = hid_to_usb_dev(hid);
633                         struct usbhid_device *usbhid = hid->driver_data;
634
635                         dinfo.bustype = BUS_USB;
636                         dinfo.busnum = dev->bus->busnum;
637                         dinfo.devnum = dev->devnum;
638                         dinfo.ifnum = usbhid->ifnum;
639                         dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
640                         dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
641                         dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
642                         dinfo.num_applications = hid->maxapplication;
643
644                         r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
645                                 -EFAULT : 0;
646                         break;
647                 }
648
649         case HIDIOCGFLAG:
650                 r = put_user(list->flags, (int __user *)arg) ?
651                         -EFAULT : 0;
652                 break;
653
654         case HIDIOCSFLAG:
655                 {
656                         int newflags;
657
658                         if (get_user(newflags, (int __user *)arg)) {
659                                 r = -EFAULT;
660                                 break;
661                         }
662
663                         if ((newflags & ~HIDDEV_FLAGS) != 0 ||
664                             ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
665                              (newflags & HIDDEV_FLAG_UREF) == 0))
666                                 break;
667
668                         list->flags = newflags;
669
670                         r = 0;
671                         break;
672                 }
673
674         case HIDIOCGSTRING:
675                 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
676                 break;
677
678         case HIDIOCINITREPORT:
679                 usbhid_init_reports(hid);
680                 r = 0;
681                 break;
682
683         case HIDIOCGREPORT:
684                 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
685                         r = -EFAULT;
686                         break;
687                 }
688
689                 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
690                         break;
691
692                 report = hiddev_lookup_report(hid, &rinfo);
693                 if (report == NULL)
694                         break;
695
696                 usbhid_submit_report(hid, report, USB_DIR_IN);
697                 usbhid_wait_io(hid);
698
699                 r = 0;
700                 break;
701
702         case HIDIOCSREPORT:
703                 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
704                         r = -EFAULT;
705                         break;
706                 }
707
708                 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
709                         break;
710
711                 report = hiddev_lookup_report(hid, &rinfo);
712                 if (report == NULL)
713                         break;
714
715                 usbhid_submit_report(hid, report, USB_DIR_OUT);
716                 usbhid_wait_io(hid);
717
718                 r = 0;
719                 break;
720
721         case HIDIOCGREPORTINFO:
722                 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
723                         r = -EFAULT;
724                         break;
725                 }
726
727                 report = hiddev_lookup_report(hid, &rinfo);
728                 if (report == NULL)
729                         break;
730
731                 rinfo.num_fields = report->maxfield;
732
733                 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
734                         -EFAULT : 0;
735                 break;
736
737         case HIDIOCGFIELDINFO:
738                 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
739                         r = -EFAULT;
740                         break;
741                 }
742
743                 rinfo.report_type = finfo.report_type;
744                 rinfo.report_id = finfo.report_id;
745
746                 report = hiddev_lookup_report(hid, &rinfo);
747                 if (report == NULL)
748                         break;
749
750                 if (finfo.field_index >= report->maxfield)
751                         break;
752
753                 field = report->field[finfo.field_index];
754                 memset(&finfo, 0, sizeof(finfo));
755                 finfo.report_type = rinfo.report_type;
756                 finfo.report_id = rinfo.report_id;
757                 finfo.field_index = field->report_count - 1;
758                 finfo.maxusage = field->maxusage;
759                 finfo.flags = field->flags;
760                 finfo.physical = field->physical;
761                 finfo.logical = field->logical;
762                 finfo.application = field->application;
763                 finfo.logical_minimum = field->logical_minimum;
764                 finfo.logical_maximum = field->logical_maximum;
765                 finfo.physical_minimum = field->physical_minimum;
766                 finfo.physical_maximum = field->physical_maximum;
767                 finfo.unit_exponent = field->unit_exponent;
768                 finfo.unit = field->unit;
769
770                 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
771                         -EFAULT : 0;
772                 break;
773
774         case HIDIOCGUCODE:
775                 /* fall through */
776         case HIDIOCGUSAGE:
777         case HIDIOCSUSAGE:
778         case HIDIOCGUSAGES:
779         case HIDIOCSUSAGES:
780         case HIDIOCGCOLLECTIONINDEX:
781                 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
782                 break;
783
784         case HIDIOCGCOLLECTIONINFO:
785                 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
786                         r = -EFAULT;
787                         break;
788                 }
789
790                 if (cinfo.index >= hid->maxcollection)
791                         break;
792
793                 cinfo.type = hid->collection[cinfo.index].type;
794                 cinfo.usage = hid->collection[cinfo.index].usage;
795                 cinfo.level = hid->collection[cinfo.index].level;
796
797                 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
798                         -EFAULT : 0;
799                 break;
800
801         default:
802                 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
803                         break;
804
805                 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
806                         int len;
807
808                         if (!hid->name) {
809                                 r = 0;
810                                 break;
811                         }
812
813                         len = strlen(hid->name) + 1;
814                         if (len > _IOC_SIZE(cmd))
815                                  len = _IOC_SIZE(cmd);
816                         r = copy_to_user(user_arg, hid->name, len) ?
817                                 -EFAULT : len;
818                         break;
819                 }
820
821                 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
822                         int len;
823
824                         if (!hid->phys) {
825                                 r = 0;
826                                 break;
827                         }
828
829                         len = strlen(hid->phys) + 1;
830                         if (len > _IOC_SIZE(cmd))
831                                 len = _IOC_SIZE(cmd);
832                         r = copy_to_user(user_arg, hid->phys, len) ?
833                                 -EFAULT : len;
834                         break;
835                 }
836         }
837
838 ret_unlock:
839         mutex_unlock(&hiddev->existancelock);
840         return r;
841 }
842
843 #ifdef CONFIG_COMPAT
844 static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
845 {
846         return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
847 }
848 #endif
849
850 static const struct file_operations hiddev_fops = {
851         .owner =        THIS_MODULE,
852         .read =         hiddev_read,
853         .write =        hiddev_write,
854         .poll =         hiddev_poll,
855         .open =         hiddev_open,
856         .release =      hiddev_release,
857         .unlocked_ioctl =       hiddev_ioctl,
858         .fasync =       hiddev_fasync,
859 #ifdef CONFIG_COMPAT
860         .compat_ioctl   = hiddev_compat_ioctl,
861 #endif
862         .llseek         = noop_llseek,
863 };
864
865 static char *hiddev_devnode(struct device *dev, mode_t *mode)
866 {
867         return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
868 }
869
870 static struct usb_class_driver hiddev_class = {
871         .name =         "hiddev%d",
872         .devnode =      hiddev_devnode,
873         .fops =         &hiddev_fops,
874         .minor_base =   HIDDEV_MINOR_BASE,
875 };
876
877 /*
878  * This is where hid.c calls us to connect a hid device to the hiddev driver
879  */
880 int hiddev_connect(struct hid_device *hid, unsigned int force)
881 {
882         struct hiddev *hiddev;
883         struct usbhid_device *usbhid = hid->driver_data;
884         int retval;
885
886         if (!force) {
887                 unsigned int i;
888                 for (i = 0; i < hid->maxcollection; i++)
889                         if (hid->collection[i].type ==
890                             HID_COLLECTION_APPLICATION &&
891                             !IS_INPUT_APPLICATION(hid->collection[i].usage))
892                                 break;
893
894                 if (i == hid->maxcollection)
895                         return -1;
896         }
897
898         if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
899                 return -1;
900
901         init_waitqueue_head(&hiddev->wait);
902         INIT_LIST_HEAD(&hiddev->list);
903         spin_lock_init(&hiddev->list_lock);
904         mutex_init(&hiddev->existancelock);
905         hid->hiddev = hiddev;
906         hiddev->hid = hid;
907         hiddev->exist = 1;
908         retval = usb_register_dev(usbhid->intf, &hiddev_class);
909         if (retval) {
910                 hid_err(hid, "Not able to get a minor for this device\n");
911                 hid->hiddev = NULL;
912                 kfree(hiddev);
913                 return -1;
914         }
915         return 0;
916 }
917
918 /*
919  * This is where hid.c calls us to disconnect a hiddev device from the
920  * corresponding hid device (usually because the usb device has disconnected)
921  */
922 static struct usb_class_driver hiddev_class;
923 void hiddev_disconnect(struct hid_device *hid)
924 {
925         struct hiddev *hiddev = hid->hiddev;
926         struct usbhid_device *usbhid = hid->driver_data;
927
928         mutex_lock(&hiddev->existancelock);
929         hiddev->exist = 0;
930         mutex_unlock(&hiddev->existancelock);
931
932         usb_deregister_dev(usbhid->intf, &hiddev_class);
933
934         if (hiddev->open) {
935                 usbhid_close(hiddev->hid);
936                 wake_up_interruptible(&hiddev->wait);
937         } else {
938                 kfree(hiddev);
939         }
940 }