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