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