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