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