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