blob: 6dd6666961787434653fcde4add8eb0e25bb2ab0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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
48struct 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
56struct 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
66static 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 */
74static struct hid_report *
75hiddev_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;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 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 */
120static struct hid_field *
121hiddev_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
150static 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;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500160 list->head = (list->head + 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 (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 */
175void 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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500181 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500183 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ((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
195void 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));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500201 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500203 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 ((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 */
213static 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 */
225static 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) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500238 if (list->hiddev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 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 */
252static 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
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100260 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 list->hiddev = hiddev_table[i];
264 list->next = hiddev_table[i]->list;
265 hiddev_table[i]->list = list;
266
267 file->private_data = list;
268
269 if (!list->hiddev->open++)
270 if (list->hiddev->exist)
271 hid_open(hiddev_table[i]->hid);
272
273 return 0;
274}
275
276/*
277 * "write" file op
278 */
279static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
280{
281 return -EINVAL;
282}
283
284/*
285 * "read" file op
286 */
287static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
288{
289 DECLARE_WAITQUEUE(wait, current);
290 struct hiddev_list *list = file->private_data;
291 int event_size;
292 int retval = 0;
293
294 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
295 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
296
297 if (count < event_size)
298 return 0;
299
300 while (retval == 0) {
301 if (list->head == list->tail) {
302 add_wait_queue(&list->hiddev->wait, &wait);
303 set_current_state(TASK_INTERRUPTIBLE);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 while (list->head == list->tail) {
306 if (file->f_flags & O_NONBLOCK) {
307 retval = -EAGAIN;
308 break;
309 }
310 if (signal_pending(current)) {
311 retval = -ERESTARTSYS;
312 break;
313 }
314 if (!list->hiddev->exist) {
315 retval = -EIO;
316 break;
317 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 schedule();
320 }
321
322 set_current_state(TASK_RUNNING);
323 remove_wait_queue(&list->hiddev->wait, &wait);
324 }
325
326 if (retval)
327 return retval;
328
329
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500330 while (list->head != list->tail &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 retval + event_size <= count) {
332 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
333 if (list->buffer[list->tail].field_index !=
334 HID_FIELD_INDEX_NONE) {
335 struct hiddev_event event;
336 event.hid = list->buffer[list->tail].usage_code;
337 event.value = list->buffer[list->tail].value;
338 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
339 return -EFAULT;
340 retval += sizeof(struct hiddev_event);
341 }
342 } else {
343 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
344 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
345 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
346 return -EFAULT;
347 retval += sizeof(struct hiddev_usage_ref);
348 }
349 }
350 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
351 }
352
353 }
354
355 return retval;
356}
357
358/*
359 * "poll" file op
360 * No kernel lock - fine
361 */
362static unsigned int hiddev_poll(struct file *file, poll_table *wait)
363{
364 struct hiddev_list *list = file->private_data;
365 poll_wait(file, &list->hiddev->wait, wait);
366 if (list->head != list->tail)
367 return POLLIN | POLLRDNORM;
368 if (!list->hiddev->exist)
369 return POLLERR | POLLHUP;
370 return 0;
371}
372
373/*
374 * "ioctl" file op
375 */
376static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
377{
378 struct hiddev_list *list = file->private_data;
379 struct hiddev *hiddev = list->hiddev;
380 struct hid_device *hid = hiddev->hid;
381 struct usb_device *dev = hid->dev;
382 struct hiddev_collection_info cinfo;
383 struct hiddev_report_info rinfo;
384 struct hiddev_field_info finfo;
385 struct hiddev_usage_ref_multi *uref_multi=NULL;
386 struct hiddev_usage_ref *uref;
387 struct hiddev_devinfo dinfo;
388 struct hid_report *report;
389 struct hid_field *field;
390 void __user *user_arg = (void __user *)arg;
391 int i;
392
393 if (!hiddev->exist)
394 return -EIO;
395
396 switch (cmd) {
397
398 case HIDIOCGVERSION:
399 return put_user(HID_VERSION, (int __user *)arg);
400
401 case HIDIOCAPPLICATION:
402 if (arg < 0 || arg >= hid->maxapplication)
403 return -EINVAL;
404
405 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500406 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 HID_COLLECTION_APPLICATION && arg-- == 0)
408 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (i == hid->maxcollection)
411 return -EINVAL;
412
413 return hid->collection[i].usage;
414
415 case HIDIOCGDEVINFO:
416 dinfo.bustype = BUS_USB;
417 dinfo.busnum = dev->bus->busnum;
418 dinfo.devnum = dev->devnum;
419 dinfo.ifnum = hid->ifnum;
420 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
421 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
422 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
423 dinfo.num_applications = hid->maxapplication;
424 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
425 return -EFAULT;
426
427 return 0;
428
429 case HIDIOCGFLAG:
430 if (put_user(list->flags, (int __user *)arg))
431 return -EFAULT;
432
433 return 0;
434
435 case HIDIOCSFLAG:
436 {
437 int newflags;
438 if (get_user(newflags, (int __user *)arg))
439 return -EFAULT;
440
441 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
442 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
443 (newflags & HIDDEV_FLAG_UREF) == 0))
444 return -EINVAL;
445
446 list->flags = newflags;
447
448 return 0;
449 }
450
451 case HIDIOCGSTRING:
452 {
453 int idx, len;
454 char *buf;
455
456 if (get_user(idx, (int __user *)arg))
457 return -EFAULT;
458
459 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
460 return -ENOMEM;
461
462 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
463 kfree(buf);
464 return -EINVAL;
465 }
466
467 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
468 kfree(buf);
469 return -EFAULT;
470 }
471
472 kfree(buf);
473
474 return len;
475 }
476
477 case HIDIOCINITREPORT:
478 hid_init_reports(hid);
479
480 return 0;
481
482 case HIDIOCGREPORT:
483 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
484 return -EFAULT;
485
486 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
487 return -EINVAL;
488
489 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
490 return -EINVAL;
491
492 hid_submit_report(hid, report, USB_DIR_IN);
493 hid_wait_io(hid);
494
495 return 0;
496
497 case HIDIOCSREPORT:
498 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
499 return -EFAULT;
500
501 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
502 return -EINVAL;
503
504 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
505 return -EINVAL;
506
507 hid_submit_report(hid, report, USB_DIR_OUT);
Stefan Nickl010988e2005-09-05 01:57:46 -0500508 hid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 return 0;
511
512 case HIDIOCGREPORTINFO:
513 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
514 return -EFAULT;
515
516 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
517 return -EINVAL;
518
519 rinfo.num_fields = report->maxfield;
520
521 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
522 return -EFAULT;
523
524 return 0;
525
526 case HIDIOCGFIELDINFO:
527 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
528 return -EFAULT;
529 rinfo.report_type = finfo.report_type;
530 rinfo.report_id = finfo.report_id;
531 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
532 return -EINVAL;
533
534 if (finfo.field_index >= report->maxfield)
535 return -EINVAL;
536
537 field = report->field[finfo.field_index];
538 memset(&finfo, 0, sizeof(finfo));
539 finfo.report_type = rinfo.report_type;
540 finfo.report_id = rinfo.report_id;
541 finfo.field_index = field->report_count - 1;
542 finfo.maxusage = field->maxusage;
543 finfo.flags = field->flags;
544 finfo.physical = field->physical;
545 finfo.logical = field->logical;
546 finfo.application = field->application;
547 finfo.logical_minimum = field->logical_minimum;
548 finfo.logical_maximum = field->logical_maximum;
549 finfo.physical_minimum = field->physical_minimum;
550 finfo.physical_maximum = field->physical_maximum;
551 finfo.unit_exponent = field->unit_exponent;
552 finfo.unit = field->unit;
553
554 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
555 return -EFAULT;
556
557 return 0;
558
559 case HIDIOCGUCODE:
560 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
561 if (!uref_multi)
562 return -ENOMEM;
563 uref = &uref_multi->uref;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500564 if (copy_from_user(uref, user_arg, sizeof(*uref)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto fault;
566
567 rinfo.report_type = uref->report_type;
568 rinfo.report_id = uref->report_id;
569 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
570 goto inval;
571
572 if (uref->field_index >= report->maxfield)
573 goto inval;
574
575 field = report->field[uref->field_index];
576 if (uref->usage_index >= field->maxusage)
577 goto inval;
578
579 uref->usage_code = field->usage[uref->usage_index].hid;
580
581 if (copy_to_user(user_arg, uref, sizeof(*uref)))
582 goto fault;
583
584 kfree(uref_multi);
585 return 0;
586
587 case HIDIOCGUSAGE:
588 case HIDIOCSUSAGE:
589 case HIDIOCGUSAGES:
590 case HIDIOCSUSAGES:
591 case HIDIOCGCOLLECTIONINDEX:
592 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
593 if (!uref_multi)
594 return -ENOMEM;
595 uref = &uref_multi->uref;
596 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500597 if (copy_from_user(uref_multi, user_arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 sizeof(*uref_multi)))
599 goto fault;
600 } else {
601 if (copy_from_user(uref, user_arg, sizeof(*uref)))
602 goto fault;
603 }
604
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500605 if (cmd != HIDIOCGUSAGE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 cmd != HIDIOCGUSAGES &&
607 uref->report_type == HID_REPORT_TYPE_INPUT)
608 goto inval;
609
610 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
611 field = hiddev_lookup_usage(hid, uref);
612 if (field == NULL)
613 goto inval;
614 } else {
615 rinfo.report_type = uref->report_type;
616 rinfo.report_id = uref->report_id;
617 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
618 goto inval;
619
620 if (uref->field_index >= report->maxfield)
621 goto inval;
622
623 field = report->field[uref->field_index];
624
625 if (cmd == HIDIOCGCOLLECTIONINDEX) {
626 if (uref->usage_index >= field->maxusage)
627 goto inval;
628 } else if (uref->usage_index >= field->report_count)
629 goto inval;
630
631 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
632 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
Ben Collins6dea9342006-01-31 01:31:13 -0500633 uref->usage_index + uref_multi->num_values > field->report_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto inval;
635 }
636
637 switch (cmd) {
638 case HIDIOCGUSAGE:
639 uref->value = field->value[uref->usage_index];
640 if (copy_to_user(user_arg, uref, sizeof(*uref)))
641 goto fault;
642 goto goodreturn;
643
644 case HIDIOCSUSAGE:
645 field->value[uref->usage_index] = uref->value;
646 goto goodreturn;
647
648 case HIDIOCGCOLLECTIONINDEX:
649 kfree(uref_multi);
650 return field->usage[uref->usage_index].collection_index;
651 case HIDIOCGUSAGES:
652 for (i = 0; i < uref_multi->num_values; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500653 uref_multi->values[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 field->value[uref->usage_index + i];
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500655 if (copy_to_user(user_arg, uref_multi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 sizeof(*uref_multi)))
657 goto fault;
658 goto goodreturn;
659 case HIDIOCSUSAGES:
660 for (i = 0; i < uref_multi->num_values; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500661 field->value[uref->usage_index + i] =
662 uref_multi->values[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto goodreturn;
664 }
665
666goodreturn:
667 kfree(uref_multi);
668 return 0;
669fault:
670 kfree(uref_multi);
671 return -EFAULT;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500672inval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 kfree(uref_multi);
674 return -EINVAL;
675
676 case HIDIOCGCOLLECTIONINFO:
677 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
678 return -EFAULT;
679
680 if (cinfo.index >= hid->maxcollection)
681 return -EINVAL;
682
683 cinfo.type = hid->collection[cinfo.index].type;
684 cinfo.usage = hid->collection[cinfo.index].usage;
685 cinfo.level = hid->collection[cinfo.index].level;
686
687 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
688 return -EFAULT;
689 return 0;
690
691 default:
692
693 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
694 return -EINVAL;
695
696 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
697 int len;
698 if (!hid->name)
699 return 0;
700 len = strlen(hid->name) + 1;
701 if (len > _IOC_SIZE(cmd))
702 len = _IOC_SIZE(cmd);
703 return copy_to_user(user_arg, hid->name, len) ?
704 -EFAULT : len;
705 }
706
707 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
708 int len;
709 if (!hid->phys)
710 return 0;
711 len = strlen(hid->phys) + 1;
712 if (len > _IOC_SIZE(cmd))
713 len = _IOC_SIZE(cmd);
714 return copy_to_user(user_arg, hid->phys, len) ?
715 -EFAULT : len;
716 }
717 }
718 return -EINVAL;
719}
720
721static struct file_operations hiddev_fops = {
722 .owner = THIS_MODULE,
723 .read = hiddev_read,
724 .write = hiddev_write,
725 .poll = hiddev_poll,
726 .open = hiddev_open,
727 .release = hiddev_release,
728 .ioctl = hiddev_ioctl,
729 .fasync = hiddev_fasync,
730};
731
732static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700733 .name = "hiddev%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 .fops = &hiddev_fops,
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500735 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736};
737
738/*
739 * This is where hid.c calls us to connect a hid device to the hiddev driver
740 */
741int hiddev_connect(struct hid_device *hid)
742{
743 struct hiddev *hiddev;
744 int i;
745 int retval;
746
747 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500748 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 HID_COLLECTION_APPLICATION &&
750 !IS_INPUT_APPLICATION(hid->collection[i].usage))
751 break;
752
753 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
754 return -1;
755
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100756 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500759 retval = usb_register_dev(hid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (retval) {
761 err("Not able to get a minor for this device.");
762 kfree(hiddev);
763 return -1;
764 }
765
766 init_waitqueue_head(&hiddev->wait);
767
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500768 hiddev_table[hid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 hiddev->hid = hid;
771 hiddev->exist = 1;
772
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500773 hid->minor = hid->intf->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 hid->hiddev = hiddev;
775
776 return 0;
777}
778
779/*
780 * This is where hid.c calls us to disconnect a hiddev device from the
781 * corresponding hid device (usually because the usb device has disconnected)
782 */
783static struct usb_class_driver hiddev_class;
784void hiddev_disconnect(struct hid_device *hid)
785{
786 struct hiddev *hiddev = hid->hiddev;
787
788 hiddev->exist = 0;
789
790 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
791 usb_deregister_dev(hiddev->hid->intf, &hiddev_class);
792
793 if (hiddev->open) {
794 hid_close(hiddev->hid);
795 wake_up_interruptible(&hiddev->wait);
796 } else {
797 kfree(hiddev);
798 }
799}
800
801/* Currently this driver is a USB driver. It's not a conventional one in
802 * the sense that it doesn't probe at the USB level. Instead it waits to
803 * be connected by HID through the hiddev_connect / hiddev_disconnect
804 * routines. The reason to register as a USB device is to gain part of the
805 * minor number space from the USB major.
806 *
807 * In theory, should the HID code be generalized to more than one physical
808 * medium (say, IEEE 1384), this driver will probably need to register its
809 * own major number, and in doing so, no longer need to register with USB.
810 * At that point the probe routine and hiddev_driver struct below will no
811 * longer be useful.
812 */
813
814
815/* We never attach in this manner, and rely on HID to connect us. This
816 * is why there is no disconnect routine defined in the usb_driver either.
817 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500818static int hiddev_usbd_probe(struct usb_interface *intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 const struct usb_device_id *hiddev_info)
820{
821 return -ENODEV;
822}
823
824
825static /* const */ struct usb_driver hiddev_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 .name = "hiddev",
827 .probe = hiddev_usbd_probe,
828};
829
830int __init hiddev_init(void)
831{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return usb_register(&hiddev_driver);
833}
834
835void hiddev_exit(void)
836{
837 usb_deregister(&hiddev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}