Merge to Fedora kernel-2.6.7-1.441
[linux-2.6.git] / drivers / usb / input / 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/config.h>
29 #include <linux/poll.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/smp_lock.h>
34 #include <linux/input.h>
35 #include <linux/usb.h>
36 #include "hid.h"
37 #include <linux/hiddev.h>
38 #include <linux/devfs_fs_kernel.h>
39
40 #ifdef CONFIG_USB_DYNAMIC_MINORS
41 #define HIDDEV_MINOR_BASE       0
42 #define HIDDEV_MINORS           256
43 #else
44 #define HIDDEV_MINOR_BASE       96
45 #define HIDDEV_MINORS           16
46 #endif
47 #define HIDDEV_BUFFER_SIZE      64
48
49 struct hiddev {
50         int exist;
51         int open;
52         wait_queue_head_t wait;
53         struct hid_device *hid;
54         struct hiddev_list *list;
55 };
56
57 struct hiddev_list {
58         struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
59         int head;
60         int tail;
61         unsigned flags;
62         struct fasync_struct *fasync;
63         struct hiddev *hiddev;
64         struct hiddev_list *next;
65 };
66
67 static struct hiddev *hiddev_table[HIDDEV_MINORS];
68
69 /* forward reference to make our lives easier */
70 extern struct usb_driver hiddev_driver;
71
72 /*
73  * Find a report, given the report's type and ID.  The ID can be specified
74  * indirectly by REPORT_ID_FIRST (which returns the first report of the given
75  * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
76  * given type which follows old_id.
77  */
78 static struct hid_report *
79 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
80 {
81         unsigned flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
82         struct hid_report_enum *report_enum;
83         struct list_head *list;
84
85         if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
86             rinfo->report_type > HID_REPORT_TYPE_MAX) 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                 list = report_enum->report_list.next;
97                 if (list == &report_enum->report_list)
98                         return NULL;
99                 rinfo->report_id = ((struct hid_report *) list)->id;
100                 break;
101                 
102         case HID_REPORT_ID_NEXT:
103                 list = (struct list_head *)
104                         report_enum->report_id_hash[rinfo->report_id & HID_REPORT_ID_MASK];
105                 if (list == NULL)
106                         return NULL;
107                 list = list->next;
108                 if (list == &report_enum->report_list)
109                         return NULL;
110                 rinfo->report_id = ((struct hid_report *) list)->id;
111                 break;
112                 
113         default:
114                 return NULL;
115         }
116
117         return report_enum->report_id_hash[rinfo->report_id];
118 }
119
120 /*
121  * Perform an exhaustive search of the report table for a usage, given its
122  * type and usage id.
123  */
124 static struct hid_field *
125 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
126 {
127         int i, j;
128         struct hid_report *report;
129         struct hid_report_enum *report_enum;
130         struct list_head *list;
131         struct hid_field *field;
132
133         if (uref->report_type < HID_REPORT_TYPE_MIN ||
134             uref->report_type > HID_REPORT_TYPE_MAX) return NULL;
135
136         report_enum = hid->report_enum +
137                 (uref->report_type - HID_REPORT_TYPE_MIN);
138         list = report_enum->report_list.next;
139         while (list != &report_enum->report_list) {
140                 report = (struct hid_report *) list;
141                 for (i = 0; i < report->maxfield; i++) {
142                         field = report->field[i];
143                         for (j = 0; j < field->maxusage; j++) {
144                                 if (field->usage[j].hid == uref->usage_code) {
145                                         uref->report_id = report->id;
146                                         uref->field_index = i;
147                                         uref->usage_index = j;
148                                         return field;
149                                 }
150                         }
151                 }
152                 list = list->next;
153         }
154
155         return NULL;
156 }
157
158 static void hiddev_send_event(struct hid_device *hid,
159                               struct hiddev_usage_ref *uref)
160 {
161         struct hiddev *hiddev = hid->hiddev;
162         struct hiddev_list *list = hiddev->list;
163
164         while (list) {
165                 if (uref->field_index != HID_FIELD_INDEX_NONE ||
166                     (list->flags & HIDDEV_FLAG_REPORT) != 0) {
167                         list->buffer[list->head] = *uref;
168                         list->head = (list->head + 1) & 
169                                 (HIDDEV_BUFFER_SIZE - 1);
170                         kill_fasync(&list->fasync, SIGIO, POLL_IN);
171                 }
172
173                 list = list->next;
174         }
175
176         wake_up_interruptible(&hiddev->wait);
177 }
178
179 /*
180  * This is where hid.c calls into hiddev to pass an event that occurred over
181  * the interrupt pipe
182  */
183 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
184                       struct hid_usage *usage, __s32 value, struct pt_regs *regs)
185 {
186         unsigned type = field->report_type;
187         struct hiddev_usage_ref uref;
188
189         uref.report_type = 
190           (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
191           ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : 
192            ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
193         uref.report_id = field->report->id;
194         uref.field_index = field->index;
195         uref.usage_index = (usage - field->usage);
196         uref.usage_code = usage->hid;
197         uref.value = value;
198
199         hiddev_send_event(hid, &uref);
200 }
201
202
203 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
204 {
205         unsigned type = report->type;
206         struct hiddev_usage_ref uref;
207
208         memset(&uref, 0, sizeof(uref));
209         uref.report_type = 
210           (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
211           ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : 
212            ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE:0));
213         uref.report_id = report->id;
214         uref.field_index = HID_FIELD_INDEX_NONE;
215
216         hiddev_send_event(hid, &uref);
217 }
218 /*
219  * fasync file op
220  */
221 static int hiddev_fasync(int fd, struct file *file, int on)
222 {
223         int retval;
224         struct hiddev_list *list = file->private_data;
225         retval = fasync_helper(fd, file, on, &list->fasync);
226         return retval < 0 ? retval : 0;
227 }
228
229 /*
230  * De-allocate a hiddev structure
231  */
232 static struct usb_class_driver hiddev_class;
233 static void hiddev_cleanup(struct hiddev *hiddev)
234 {
235         hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
236         usb_deregister_dev(hiddev->hid->intf, &hiddev_class);
237         kfree(hiddev);
238 }
239
240 /*
241  * release file op
242  */
243 static int hiddev_release(struct inode * inode, struct file * file)
244 {
245         struct hiddev_list *list = file->private_data;
246         struct hiddev_list **listptr;
247
248         listptr = &list->hiddev->list;
249         hiddev_fasync(-1, file, 0);
250
251         while (*listptr && (*listptr != list))
252                 listptr = &((*listptr)->next);
253         *listptr = (*listptr)->next;
254
255         if (!--list->hiddev->open) {
256                 if (list->hiddev->exist) 
257                         hid_close(list->hiddev->hid);
258                 else
259                         hiddev_cleanup(list->hiddev);
260         }
261
262         kfree(list);
263
264         return 0;
265 }
266
267 /*
268  * open file op
269  */
270 static int hiddev_open(struct inode * inode, struct file * file) {
271         struct hiddev_list *list;
272
273         int i = iminor(inode) - HIDDEV_MINOR_BASE;
274
275         if (i >= HIDDEV_MINORS || !hiddev_table[i])
276                 return -ENODEV;
277
278         if (!(list = kmalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
279                 return -ENOMEM;
280         memset(list, 0, sizeof(struct hiddev_list));
281
282         list->hiddev = hiddev_table[i];
283         list->next = hiddev_table[i]->list;
284         hiddev_table[i]->list = list;
285
286         file->private_data = list;
287
288         if (!list->hiddev->open++)
289                 if (list->hiddev->exist)
290                         hid_open(hiddev_table[i]->hid);
291
292         return 0;
293 }
294
295 /*
296  * "write" file op
297  */
298 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
299 {
300         return -EINVAL;
301 }
302
303 /*
304  * "read" file op
305  */
306 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
307 {
308         DECLARE_WAITQUEUE(wait, current);
309         struct hiddev_list *list = file->private_data;
310         int event_size;
311         int retval = 0;
312
313         event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
314                 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
315
316         if (count < event_size)
317                 return 0;
318
319         while (retval == 0) {
320                 if (list->head == list->tail) {
321                         add_wait_queue(&list->hiddev->wait, &wait);
322                         set_current_state(TASK_INTERRUPTIBLE);
323                         
324                         while (list->head == list->tail) {
325                                 if (file->f_flags & O_NONBLOCK) {
326                                         retval = -EAGAIN;
327                                         break;
328                                 }
329                                 if (signal_pending(current)) {
330                                         retval = -ERESTARTSYS;
331                                         break;
332                                 }
333                                 if (!list->hiddev->exist) {
334                                         retval = -EIO;
335                                         break;
336                                 }
337                                 
338                                 schedule();
339                         }
340
341                         set_current_state(TASK_RUNNING);
342                         remove_wait_queue(&list->hiddev->wait, &wait);
343                 }
344
345                 if (retval)
346                         return retval;
347
348
349                 while (list->head != list->tail && 
350                        retval + event_size <= count) {
351                         if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
352                                 if (list->buffer[list->tail].field_index !=
353                                     HID_FIELD_INDEX_NONE) {
354                                         struct hiddev_event event;
355                                         event.hid = list->buffer[list->tail].usage_code;
356                                         event.value = list->buffer[list->tail].value;
357                                         if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
358                                                 return -EFAULT;
359                                         retval += sizeof(struct hiddev_event);
360                                 }
361                         } else {
362                                 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
363                                     (list->flags & HIDDEV_FLAG_REPORT) != 0) {
364                                         if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
365                                                 return -EFAULT;
366                                         retval += sizeof(struct hiddev_usage_ref);
367                                 }
368                         }
369                         list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
370                 }
371
372         }
373
374         return retval;
375 }
376
377 /*
378  * "poll" file op
379  * No kernel lock - fine
380  */
381 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
382 {
383         struct hiddev_list *list = file->private_data;
384         poll_wait(file, &list->hiddev->wait, wait);
385         if (list->head != list->tail)
386                 return POLLIN | POLLRDNORM;
387         if (!list->hiddev->exist)
388                 return POLLERR | POLLHUP;
389         return 0;
390 }
391
392 /*
393  * "ioctl" file op
394  */
395 static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
396 {
397         struct hiddev_list *list = file->private_data;
398         struct hiddev *hiddev = list->hiddev;
399         struct hid_device *hid = hiddev->hid;
400         struct usb_device *dev = hid->dev;
401         struct hiddev_collection_info cinfo;
402         struct hiddev_report_info rinfo;
403         struct hiddev_field_info finfo;
404         struct hiddev_usage_ref_multi *uref_multi=NULL;
405         struct hiddev_usage_ref *uref;
406         struct hiddev_devinfo dinfo;
407         struct hid_report *report;
408         struct hid_field *field;
409         void __user *user_arg = (void __user *)arg;
410         int i;
411
412         if (!hiddev->exist)
413                 return -EIO;
414
415         switch (cmd) {
416
417         case HIDIOCGVERSION:
418                 return put_user(HID_VERSION, (int __user *)arg);
419
420         case HIDIOCAPPLICATION:
421                 if (arg < 0 || arg >= hid->maxapplication)
422                         return -EINVAL;
423
424                 for (i = 0; i < hid->maxcollection; i++)
425                         if (hid->collection[i].type == 
426                             HID_COLLECTION_APPLICATION && arg-- == 0)
427                                 break;
428                 
429                 if (i == hid->maxcollection)
430                         return -EINVAL;
431
432                 return hid->collection[i].usage;
433
434         case HIDIOCGDEVINFO:
435                 dinfo.bustype = BUS_USB;
436                 dinfo.busnum = dev->bus->busnum;
437                 dinfo.devnum = dev->devnum;
438                 dinfo.ifnum = hid->ifnum;
439                 dinfo.vendor = dev->descriptor.idVendor;
440                 dinfo.product = dev->descriptor.idProduct;
441                 dinfo.version = dev->descriptor.bcdDevice;
442                 dinfo.num_applications = hid->maxapplication;
443                 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
444                         return -EFAULT;
445
446                 return 0;
447
448         case HIDIOCGFLAG:
449                 if (put_user(list->flags, (int __user *)arg))
450                         return -EFAULT;
451
452                 return 0;
453
454         case HIDIOCSFLAG:
455                 {
456                         int newflags;
457                         if (get_user(newflags, (int __user *)arg))
458                                 return -EFAULT;
459
460                         if ((newflags & ~HIDDEV_FLAGS) != 0 ||
461                             ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
462                              (newflags & HIDDEV_FLAG_UREF) == 0))
463                                 return -EINVAL;
464
465                         list->flags = newflags;
466
467                         return 0;
468                 }
469
470         case HIDIOCGSTRING:
471                 {
472                         int idx, len;
473                         char *buf;
474
475                         if (get_user(idx, (int __user *)arg))
476                                 return -EFAULT;
477
478                         if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
479                                 return -ENOMEM;
480
481                         if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
482                                 kfree(buf);
483                                 return -EINVAL;
484                         }
485
486                         if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
487                                 kfree(buf);
488                                 return -EFAULT;
489                         }
490
491                         kfree(buf);
492
493                         return len;
494                 }
495
496         case HIDIOCINITREPORT:
497                 hid_init_reports(hid);
498
499                 return 0;
500
501         case HIDIOCGREPORT:
502                 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
503                         return -EFAULT;
504
505                 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
506                         return -EINVAL;
507
508                 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
509                         return -EINVAL;
510
511                 hid_submit_report(hid, report, USB_DIR_IN);
512                 hid_wait_io(hid);
513
514                 return 0;
515
516         case HIDIOCSREPORT:
517                 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
518                         return -EFAULT;
519
520                 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
521                         return -EINVAL;
522
523                 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
524                         return -EINVAL;
525
526                 hid_submit_report(hid, report, USB_DIR_OUT);
527
528                 return 0;
529
530         case HIDIOCGREPORTINFO:
531                 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
532                         return -EFAULT;
533
534                 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
535                         return -EINVAL;
536
537                 rinfo.num_fields = report->maxfield;
538
539                 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
540                         return -EFAULT;
541
542                 return 0;
543
544         case HIDIOCGFIELDINFO:
545                 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
546                         return -EFAULT;
547                 rinfo.report_type = finfo.report_type;
548                 rinfo.report_id = finfo.report_id;
549                 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
550                         return -EINVAL;
551
552                 if (finfo.field_index >= report->maxfield)
553                         return -EINVAL;
554
555                 field = report->field[finfo.field_index];
556                 memset(&finfo, 0, sizeof(finfo));
557                 finfo.report_type = rinfo.report_type;
558                 finfo.report_id = rinfo.report_id;
559                 finfo.field_index = field->report_count - 1;
560                 finfo.maxusage = field->maxusage;
561                 finfo.flags = field->flags;
562                 finfo.physical = field->physical;
563                 finfo.logical = field->logical;
564                 finfo.application = field->application;
565                 finfo.logical_minimum = field->logical_minimum;
566                 finfo.logical_maximum = field->logical_maximum;
567                 finfo.physical_minimum = field->physical_minimum;
568                 finfo.physical_maximum = field->physical_maximum;
569                 finfo.unit_exponent = field->unit_exponent;
570                 finfo.unit = field->unit;
571
572                 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
573                         return -EFAULT;
574
575                 return 0;
576
577         case HIDIOCGUCODE:
578                 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
579                 if (!uref_multi)
580                         return -ENOMEM;
581                 uref = &uref_multi->uref;
582                 if (copy_from_user(uref, user_arg, sizeof(*uref))) 
583                         goto fault;
584
585                 rinfo.report_type = uref->report_type;
586                 rinfo.report_id = uref->report_id;
587                 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
588                         goto inval;
589
590                 if (uref->field_index >= report->maxfield)
591                         goto inval;
592
593                 field = report->field[uref->field_index];
594                 if (uref->usage_index >= field->maxusage)
595                         goto inval;
596
597                 uref->usage_code = field->usage[uref->usage_index].hid;
598
599                 if (copy_to_user(user_arg, uref, sizeof(*uref)))
600                         goto fault;
601
602                 kfree(uref_multi);
603                 return 0;
604
605         case HIDIOCGUSAGE:
606         case HIDIOCSUSAGE:
607         case HIDIOCGUSAGES:
608         case HIDIOCSUSAGES:
609         case HIDIOCGCOLLECTIONINDEX:
610                 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
611                 if (!uref_multi)
612                         return -ENOMEM;
613                 uref = &uref_multi->uref;
614                 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
615                         if (copy_from_user(uref_multi, user_arg, 
616                                            sizeof(*uref_multi)))
617                                 goto fault;
618                 } else {
619                         if (copy_from_user(uref, user_arg, sizeof(*uref)))
620                                 goto fault;
621                 }
622
623                 if (cmd != HIDIOCGUSAGE && 
624                     cmd != HIDIOCGUSAGES &&
625                     uref->report_type == HID_REPORT_TYPE_INPUT)
626                         goto inval;
627
628                 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
629                         field = hiddev_lookup_usage(hid, uref);
630                         if (field == NULL)
631                                 goto inval;
632                 } else {
633                         rinfo.report_type = uref->report_type;
634                         rinfo.report_id = uref->report_id;
635                         if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
636                                 goto inval;
637
638                         if (uref->field_index >= report->maxfield)
639                                 goto inval;
640
641                         field = report->field[uref->field_index];
642                         if (uref->usage_index >= field->maxusage)
643                                 goto inval;
644
645                         if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
646                                 if (uref_multi->num_values >= HID_MAX_USAGES || 
647                                     uref->usage_index >= field->maxusage || 
648                                    (uref->usage_index + uref_multi->num_values) >= field->maxusage)
649                                         goto inval;
650                         }
651                 }
652
653                 switch (cmd) {
654                         case HIDIOCGUSAGE:
655                                 uref->value = field->value[uref->usage_index];
656                                 if (copy_to_user(user_arg, uref, sizeof(*uref)))
657                                         goto fault;
658                                 goto goodreturn;
659
660                         case HIDIOCSUSAGE:
661                                 field->value[uref->usage_index] = uref->value;
662                                 goto goodreturn;
663
664                         case HIDIOCGCOLLECTIONINDEX:
665                                 kfree(uref_multi);
666                                 return field->usage[uref->usage_index].collection_index;
667                         case HIDIOCGUSAGES:
668                                 for (i = 0; i < uref_multi->num_values; i++)
669                                         uref_multi->values[i] = 
670                                             field->value[uref->usage_index + i];
671                                 if (copy_to_user(user_arg, uref_multi, 
672                                                  sizeof(*uref_multi)))
673                                         goto fault;
674                                 goto goodreturn;
675                         case HIDIOCSUSAGES:
676                                 for (i = 0; i < uref_multi->num_values; i++)
677                                         field->value[uref->usage_index + i] = 
678                                             uref_multi->values[i];
679                                 goto goodreturn;
680                 }
681
682 goodreturn:
683                 kfree(uref_multi);
684                 return 0;
685 fault:
686                 kfree(uref_multi);
687                 return -EFAULT;
688 inval:          
689                 kfree(uref_multi);
690                 return -EINVAL;
691
692         case HIDIOCGCOLLECTIONINFO:
693                 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
694                         return -EFAULT;
695
696                 if (cinfo.index >= hid->maxcollection)
697                         return -EINVAL;
698
699                 cinfo.type = hid->collection[cinfo.index].type;
700                 cinfo.usage = hid->collection[cinfo.index].usage;
701                 cinfo.level = hid->collection[cinfo.index].level;
702
703                 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
704                         return -EFAULT;
705                 return 0;
706
707         default:
708
709                 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
710                         return -EINVAL;
711
712                 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
713                         int len;
714                         if (!hid->name)
715                                 return 0;
716                         len = strlen(hid->name) + 1;
717                         if (len > _IOC_SIZE(cmd))
718                                  len = _IOC_SIZE(cmd);
719                         return copy_to_user(user_arg, hid->name, len) ?
720                                 -EFAULT : len;
721                 }
722
723                 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
724                         int len;
725                         if (!hid->phys)
726                                 return 0;
727                         len = strlen(hid->phys) + 1;
728                         if (len > _IOC_SIZE(cmd))
729                                 len = _IOC_SIZE(cmd);
730                         return copy_to_user(user_arg, hid->phys, len) ?
731                                 -EFAULT : len;
732                 }
733         }
734         return -EINVAL;
735 }
736
737 static struct file_operations hiddev_fops = {
738         .owner =        THIS_MODULE,
739         .read =         hiddev_read,
740         .write =        hiddev_write,
741         .poll =         hiddev_poll,
742         .open =         hiddev_open,
743         .release =      hiddev_release,
744         .ioctl =        hiddev_ioctl,
745         .fasync =       hiddev_fasync,
746 };
747
748 static struct usb_class_driver hiddev_class = {
749         .name =         "usb/hid/hiddev%d",
750         .fops =         &hiddev_fops,
751         .mode =         S_IFCHR | S_IRUGO | S_IWUSR,
752         .minor_base =   HIDDEV_MINOR_BASE,
753 };
754
755 /*
756  * This is where hid.c calls us to connect a hid device to the hiddev driver
757  */
758 int hiddev_connect(struct hid_device *hid)
759 {
760         struct hiddev *hiddev;
761         int i;
762         int retval;
763
764         for (i = 0; i < hid->maxcollection; i++)
765                 if (hid->collection[i].type == 
766                     HID_COLLECTION_APPLICATION &&
767                     !IS_INPUT_APPLICATION(hid->collection[i].usage))
768                         break;
769
770         if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
771                 return -1;
772
773         if (!(hiddev = kmalloc(sizeof(struct hiddev), GFP_KERNEL)))
774                 return -1;
775         memset(hiddev, 0, sizeof(struct hiddev));
776
777         retval = usb_register_dev(hid->intf, &hiddev_class);
778         if (retval) {
779                 err("Not able to get a minor for this device.");
780                 kfree(hiddev);
781                 return -1;
782         }
783
784         init_waitqueue_head(&hiddev->wait);
785
786         hiddev_table[hid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
787
788         hiddev->hid = hid;
789         hiddev->exist = 1;
790
791         hid->minor = hid->intf->minor;
792         hid->hiddev = hiddev;
793
794         return 0;
795 }
796
797 /*
798  * This is where hid.c calls us to disconnect a hiddev device from the
799  * corresponding hid device (usually because the usb device has disconnected)
800  */
801 void hiddev_disconnect(struct hid_device *hid)
802 {
803         struct hiddev *hiddev = hid->hiddev;
804
805         hiddev->exist = 0;
806
807         if (hiddev->open) {
808                 hid_close(hiddev->hid);
809                 wake_up_interruptible(&hiddev->wait);
810         } else {
811                 hiddev_cleanup(hiddev);
812         }
813 }
814
815 /* Currently this driver is a USB driver.  It's not a conventional one in
816  * the sense that it doesn't probe at the USB level.  Instead it waits to
817  * be connected by HID through the hiddev_connect / hiddev_disconnect
818  * routines.  The reason to register as a USB device is to gain part of the
819  * minor number space from the USB major.
820  *
821  * In theory, should the HID code be generalized to more than one physical
822  * medium (say, IEEE 1384), this driver will probably need to register its
823  * own major number, and in doing so, no longer need to register with USB.
824  * At that point the probe routine and hiddev_driver struct below will no
825  * longer be useful.
826  */
827
828
829 /* We never attach in this manner, and rely on HID to connect us.  This
830  * is why there is no disconnect routine defined in the usb_driver either.
831  */
832 static int hiddev_usbd_probe(struct usb_interface *intf, 
833                              const struct usb_device_id *hiddev_info)
834 {
835         return -ENODEV;
836 }
837
838
839 static /* const */ struct usb_driver hiddev_driver = {
840         .owner =        THIS_MODULE,
841         .name =         "hiddev",
842         .probe =        hiddev_usbd_probe,
843 };
844
845 int __init hiddev_init(void)
846 {
847         devfs_mk_dir("usb/hid");
848         return usb_register(&hiddev_driver);
849 }
850
851 void hiddev_exit(void)
852 {
853         usb_deregister(&hiddev_driver);
854         devfs_remove("usb/hid");
855 }