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