blob: a8b3d66cd4988b6f133f8804de4a643faeabacc3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Jiri Kosinadde58452006-12-08 18:40:44 +010035#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/hiddev.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010037#include "usbhid.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;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040053 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
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;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040063 struct list_head node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
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{
Dmitry Torokhov826d5982006-07-19 01:09:10 -040077 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
78 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct hid_report_enum *report_enum;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040080 struct hid_report *report;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct list_head *list;
82
83 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -040084 rinfo->report_type > HID_REPORT_TYPE_MAX)
85 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
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:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040095 if (list_empty(&report_enum->report_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040097
98 list = report_enum->report_list.next;
99 report = list_entry(list, struct hid_report, list);
100 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 break;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 case HID_REPORT_ID_NEXT:
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400104 report = report_enum->report_id_hash[rid];
105 if (!report)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400107
108 list = report->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (list == &report_enum->report_list)
110 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400111
112 report = list_entry(list, struct hid_report, list);
113 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 break;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 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 */
127static struct hid_field *
128hiddev_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 ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400136 uref->report_type > HID_REPORT_TYPE_MAX)
137 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 report_enum = hid->report_enum +
140 (uref->report_type - HID_REPORT_TYPE_MIN);
141
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400142 list_for_each_entry(report, &report_enum->report_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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 }
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return NULL;
157}
158
159static void hiddev_send_event(struct hid_device *hid,
160 struct hiddev_usage_ref *uref)
161{
162 struct hiddev *hiddev = hid->hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400163 struct hiddev_list *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400165 list_for_each_entry(list, &hiddev->list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (uref->field_index != HID_FIELD_INDEX_NONE ||
167 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
168 list->buffer[list->head] = *uref;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500169 list->head = (list->head + 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 (HIDDEV_BUFFER_SIZE - 1);
171 kill_fasync(&list->fasync, SIGIO, POLL_IN);
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
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 */
182void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
David Howells7d12e782006-10-05 14:55:46 +0100183 struct hid_usage *usage, __s32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 unsigned type = field->report_type;
186 struct hiddev_usage_ref uref;
187
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500188 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500190 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400191 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 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}
Jiri Kosina229695e2006-12-08 18:40:53 +0100200EXPORT_SYMBOL_GPL(hiddev_hid_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202void 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));
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500208 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500210 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400211 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 uref.report_id = report->id;
213 uref.field_index = HID_FIELD_INDEX_NONE;
214
215 hiddev_send_event(hid, &uref);
216}
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * fasync file op
220 */
221static int hiddev_fasync(int fd, struct file *file, int on)
222{
223 int retval;
224 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 retval = fasync_helper(fd, file, on, &list->fasync);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return retval < 0 ? retval : 0;
229}
230
231
232/*
233 * release file op
234 */
235static int hiddev_release(struct inode * inode, struct file * file)
236{
237 struct hiddev_list *list = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 hiddev_fasync(-1, file, 0);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400240 list_del(&list->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 if (!--list->hiddev->open) {
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500243 if (list->hiddev->exist)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100244 usbhid_close(list->hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 else
246 kfree(list->hiddev);
247 }
248
249 kfree(list);
250
251 return 0;
252}
253
254/*
255 * open file op
256 */
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400257static int hiddev_open(struct inode *inode, struct file *file)
258{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct hiddev_list *list;
260
261 int i = iminor(inode) - HIDDEV_MINOR_BASE;
262
263 if (i >= HIDDEV_MINORS || !hiddev_table[i])
264 return -ENODEV;
265
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100266 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 list->hiddev = hiddev_table[i];
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400270 list_add_tail(&list->node, &hiddev_table[i]->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 file->private_data = list;
272
273 if (!list->hiddev->open++)
274 if (list->hiddev->exist)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100275 usbhid_open(hiddev_table[i]->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 return 0;
278}
279
280/*
281 * "write" file op
282 */
283static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
284{
285 return -EINVAL;
286}
287
288/*
289 * "read" file op
290 */
291static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
292{
293 DECLARE_WAITQUEUE(wait, current);
294 struct hiddev_list *list = file->private_data;
295 int event_size;
296 int retval = 0;
297
298 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
299 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
300
301 if (count < event_size)
302 return 0;
303
304 while (retval == 0) {
305 if (list->head == list->tail) {
306 add_wait_queue(&list->hiddev->wait, &wait);
307 set_current_state(TASK_INTERRUPTIBLE);
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 while (list->head == list->tail) {
310 if (file->f_flags & O_NONBLOCK) {
311 retval = -EAGAIN;
312 break;
313 }
314 if (signal_pending(current)) {
315 retval = -ERESTARTSYS;
316 break;
317 }
318 if (!list->hiddev->exist) {
319 retval = -EIO;
320 break;
321 }
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 schedule();
Micon, David48d70552006-05-20 14:59:59 -0700324 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
327 set_current_state(TASK_RUNNING);
328 remove_wait_queue(&list->hiddev->wait, &wait);
329 }
330
331 if (retval)
332 return retval;
333
334
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500335 while (list->head != list->tail &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 retval + event_size <= count) {
337 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
338 if (list->buffer[list->tail].field_index !=
339 HID_FIELD_INDEX_NONE) {
340 struct hiddev_event event;
341 event.hid = list->buffer[list->tail].usage_code;
342 event.value = list->buffer[list->tail].value;
343 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
344 return -EFAULT;
345 retval += sizeof(struct hiddev_event);
346 }
347 } else {
348 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
349 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
350 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
351 return -EFAULT;
352 retval += sizeof(struct hiddev_usage_ref);
353 }
354 }
355 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
356 }
357
358 }
359
360 return retval;
361}
362
363/*
364 * "poll" file op
365 * No kernel lock - fine
366 */
367static unsigned int hiddev_poll(struct file *file, poll_table *wait)
368{
369 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 poll_wait(file, &list->hiddev->wait, wait);
372 if (list->head != list->tail)
373 return POLLIN | POLLRDNORM;
374 if (!list->hiddev->exist)
375 return POLLERR | POLLHUP;
376 return 0;
377}
378
379/*
380 * "ioctl" file op
381 */
382static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
383{
384 struct hiddev_list *list = file->private_data;
385 struct hiddev *hiddev = list->hiddev;
386 struct hid_device *hid = hiddev->hid;
Anssi Hannulabe820972007-01-19 19:28:17 +0200387 struct usb_device *dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct hiddev_collection_info cinfo;
389 struct hiddev_report_info rinfo;
390 struct hiddev_field_info finfo;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400391 struct hiddev_usage_ref_multi *uref_multi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct hiddev_usage_ref *uref;
393 struct hiddev_devinfo dinfo;
394 struct hid_report *report;
395 struct hid_field *field;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100396 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 void __user *user_arg = (void __user *)arg;
398 int i;
399
400 if (!hiddev->exist)
401 return -EIO;
402
403 switch (cmd) {
404
405 case HIDIOCGVERSION:
406 return put_user(HID_VERSION, (int __user *)arg);
407
408 case HIDIOCAPPLICATION:
409 if (arg < 0 || arg >= hid->maxapplication)
410 return -EINVAL;
411
412 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500413 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 HID_COLLECTION_APPLICATION && arg-- == 0)
415 break;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (i == hid->maxcollection)
418 return -EINVAL;
419
420 return hid->collection[i].usage;
421
422 case HIDIOCGDEVINFO:
423 dinfo.bustype = BUS_USB;
424 dinfo.busnum = dev->bus->busnum;
425 dinfo.devnum = dev->devnum;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100426 dinfo.ifnum = usbhid->ifnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
428 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
429 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
430 dinfo.num_applications = hid->maxapplication;
431 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
432 return -EFAULT;
433
434 return 0;
435
436 case HIDIOCGFLAG:
437 if (put_user(list->flags, (int __user *)arg))
438 return -EFAULT;
439
440 return 0;
441
442 case HIDIOCSFLAG:
443 {
444 int newflags;
445 if (get_user(newflags, (int __user *)arg))
446 return -EFAULT;
447
448 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
449 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
450 (newflags & HIDDEV_FLAG_UREF) == 0))
451 return -EINVAL;
452
453 list->flags = newflags;
454
455 return 0;
456 }
457
458 case HIDIOCGSTRING:
459 {
460 int idx, len;
461 char *buf;
462
463 if (get_user(idx, (int __user *)arg))
464 return -EFAULT;
465
466 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
467 return -ENOMEM;
468
469 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
470 kfree(buf);
471 return -EINVAL;
472 }
473
474 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
475 kfree(buf);
476 return -EFAULT;
477 }
478
479 kfree(buf);
480
481 return len;
482 }
483
484 case HIDIOCINITREPORT:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100485 usbhid_init_reports(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 return 0;
488
489 case HIDIOCGREPORT:
490 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
491 return -EFAULT;
492
493 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
494 return -EINVAL;
495
496 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
497 return -EINVAL;
498
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100499 usbhid_submit_report(hid, report, USB_DIR_IN);
500 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 return 0;
503
504 case HIDIOCSREPORT:
505 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
506 return -EFAULT;
507
508 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
509 return -EINVAL;
510
511 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
512 return -EINVAL;
513
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100514 usbhid_submit_report(hid, report, USB_DIR_OUT);
515 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 return 0;
518
519 case HIDIOCGREPORTINFO:
520 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
521 return -EFAULT;
522
523 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
524 return -EINVAL;
525
526 rinfo.num_fields = report->maxfield;
527
528 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
529 return -EFAULT;
530
531 return 0;
532
533 case HIDIOCGFIELDINFO:
534 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
535 return -EFAULT;
536 rinfo.report_type = finfo.report_type;
537 rinfo.report_id = finfo.report_id;
538 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
539 return -EINVAL;
540
541 if (finfo.field_index >= report->maxfield)
542 return -EINVAL;
543
544 field = report->field[finfo.field_index];
545 memset(&finfo, 0, sizeof(finfo));
546 finfo.report_type = rinfo.report_type;
547 finfo.report_id = rinfo.report_id;
548 finfo.field_index = field->report_count - 1;
549 finfo.maxusage = field->maxusage;
550 finfo.flags = field->flags;
551 finfo.physical = field->physical;
552 finfo.logical = field->logical;
553 finfo.application = field->application;
554 finfo.logical_minimum = field->logical_minimum;
555 finfo.logical_maximum = field->logical_maximum;
556 finfo.physical_minimum = field->physical_minimum;
557 finfo.physical_maximum = field->physical_maximum;
558 finfo.unit_exponent = field->unit_exponent;
559 finfo.unit = field->unit;
560
561 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
562 return -EFAULT;
563
564 return 0;
565
566 case HIDIOCGUCODE:
567 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
568 if (!uref_multi)
569 return -ENOMEM;
570 uref = &uref_multi->uref;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500571 if (copy_from_user(uref, user_arg, sizeof(*uref)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto fault;
573
574 rinfo.report_type = uref->report_type;
575 rinfo.report_id = uref->report_id;
576 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
577 goto inval;
578
579 if (uref->field_index >= report->maxfield)
580 goto inval;
581
582 field = report->field[uref->field_index];
583 if (uref->usage_index >= field->maxusage)
584 goto inval;
585
586 uref->usage_code = field->usage[uref->usage_index].hid;
587
588 if (copy_to_user(user_arg, uref, sizeof(*uref)))
589 goto fault;
590
591 kfree(uref_multi);
592 return 0;
593
594 case HIDIOCGUSAGE:
595 case HIDIOCSUSAGE:
596 case HIDIOCGUSAGES:
597 case HIDIOCSUSAGES:
598 case HIDIOCGCOLLECTIONINDEX:
599 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
600 if (!uref_multi)
601 return -ENOMEM;
602 uref = &uref_multi->uref;
603 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500604 if (copy_from_user(uref_multi, user_arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 sizeof(*uref_multi)))
606 goto fault;
607 } else {
608 if (copy_from_user(uref, user_arg, sizeof(*uref)))
609 goto fault;
610 }
611
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500612 if (cmd != HIDIOCGUSAGE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 cmd != HIDIOCGUSAGES &&
614 uref->report_type == HID_REPORT_TYPE_INPUT)
615 goto inval;
616
617 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
618 field = hiddev_lookup_usage(hid, uref);
619 if (field == NULL)
620 goto inval;
621 } else {
622 rinfo.report_type = uref->report_type;
623 rinfo.report_id = uref->report_id;
624 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
625 goto inval;
626
627 if (uref->field_index >= report->maxfield)
628 goto inval;
629
630 field = report->field[uref->field_index];
631
632 if (cmd == HIDIOCGCOLLECTIONINDEX) {
633 if (uref->usage_index >= field->maxusage)
634 goto inval;
635 } else if (uref->usage_index >= field->report_count)
636 goto inval;
637
638 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
639 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
Ben Collins6dea9342006-01-31 01:31:13 -0500640 uref->usage_index + uref_multi->num_values > field->report_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto inval;
642 }
643
644 switch (cmd) {
645 case HIDIOCGUSAGE:
646 uref->value = field->value[uref->usage_index];
647 if (copy_to_user(user_arg, uref, sizeof(*uref)))
648 goto fault;
649 goto goodreturn;
650
651 case HIDIOCSUSAGE:
652 field->value[uref->usage_index] = uref->value;
653 goto goodreturn;
654
655 case HIDIOCGCOLLECTIONINDEX:
656 kfree(uref_multi);
657 return field->usage[uref->usage_index].collection_index;
658 case HIDIOCGUSAGES:
659 for (i = 0; i < uref_multi->num_values; i++)
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500660 uref_multi->values[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 field->value[uref->usage_index + i];
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500662 if (copy_to_user(user_arg, uref_multi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sizeof(*uref_multi)))
664 goto fault;
665 goto goodreturn;
666 case HIDIOCSUSAGES:
667 for (i = 0; i < uref_multi->num_values; i++)
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500668 field->value[uref->usage_index + i] =
669 uref_multi->values[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goto goodreturn;
671 }
672
673goodreturn:
674 kfree(uref_multi);
675 return 0;
676fault:
677 kfree(uref_multi);
678 return -EFAULT;
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500679inval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 kfree(uref_multi);
681 return -EINVAL;
682
683 case HIDIOCGCOLLECTIONINFO:
684 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
685 return -EFAULT;
686
687 if (cinfo.index >= hid->maxcollection)
688 return -EINVAL;
689
690 cinfo.type = hid->collection[cinfo.index].type;
691 cinfo.usage = hid->collection[cinfo.index].usage;
692 cinfo.level = hid->collection[cinfo.index].level;
693
694 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
695 return -EFAULT;
696 return 0;
697
698 default:
699
700 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
701 return -EINVAL;
702
703 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
704 int len;
705 if (!hid->name)
706 return 0;
707 len = strlen(hid->name) + 1;
708 if (len > _IOC_SIZE(cmd))
709 len = _IOC_SIZE(cmd);
710 return copy_to_user(user_arg, hid->name, len) ?
711 -EFAULT : len;
712 }
713
714 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
715 int len;
716 if (!hid->phys)
717 return 0;
718 len = strlen(hid->phys) + 1;
719 if (len > _IOC_SIZE(cmd))
720 len = _IOC_SIZE(cmd);
721 return copy_to_user(user_arg, hid->phys, len) ?
722 -EFAULT : len;
723 }
724 }
725 return -EINVAL;
726}
727
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300728static const struct file_operations hiddev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 .owner = THIS_MODULE,
730 .read = hiddev_read,
731 .write = hiddev_write,
732 .poll = hiddev_poll,
733 .open = hiddev_open,
734 .release = hiddev_release,
735 .ioctl = hiddev_ioctl,
736 .fasync = hiddev_fasync,
737};
738
739static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700740 .name = "hiddev%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 .fops = &hiddev_fops,
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500742 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743};
744
745/*
746 * This is where hid.c calls us to connect a hid device to the hiddev driver
747 */
748int hiddev_connect(struct hid_device *hid)
749{
750 struct hiddev *hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100751 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int i;
753 int retval;
754
755 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500756 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 HID_COLLECTION_APPLICATION &&
758 !IS_INPUT_APPLICATION(hid->collection[i].usage))
759 break;
760
761 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
762 return -1;
763
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100764 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100767 retval = usb_register_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (retval) {
769 err("Not able to get a minor for this device.");
770 kfree(hiddev);
771 return -1;
772 }
773
774 init_waitqueue_head(&hiddev->wait);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400775 INIT_LIST_HEAD(&hiddev->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 hiddev->hid = hid;
777 hiddev->exist = 1;
778
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100779 hid->minor = usbhid->intf->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 hid->hiddev = hiddev;
781
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100782 hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785}
786
787/*
788 * This is where hid.c calls us to disconnect a hiddev device from the
789 * corresponding hid device (usually because the usb device has disconnected)
790 */
791static struct usb_class_driver hiddev_class;
792void hiddev_disconnect(struct hid_device *hid)
793{
794 struct hiddev *hiddev = hid->hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100795 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 hiddev->exist = 0;
798
799 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100800 usb_deregister_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (hiddev->open) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100803 usbhid_close(hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 wake_up_interruptible(&hiddev->wait);
805 } else {
806 kfree(hiddev);
807 }
808}
809
810/* Currently this driver is a USB driver. It's not a conventional one in
811 * the sense that it doesn't probe at the USB level. Instead it waits to
812 * be connected by HID through the hiddev_connect / hiddev_disconnect
813 * routines. The reason to register as a USB device is to gain part of the
814 * minor number space from the USB major.
815 *
816 * In theory, should the HID code be generalized to more than one physical
817 * medium (say, IEEE 1384), this driver will probably need to register its
818 * own major number, and in doing so, no longer need to register with USB.
819 * At that point the probe routine and hiddev_driver struct below will no
820 * longer be useful.
821 */
822
823
824/* We never attach in this manner, and rely on HID to connect us. This
825 * is why there is no disconnect routine defined in the usb_driver either.
826 */
Dmitry Torokhov05f091a2005-05-29 02:29:01 -0500827static int hiddev_usbd_probe(struct usb_interface *intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 const struct usb_device_id *hiddev_info)
829{
830 return -ENODEV;
831}
832
833
834static /* const */ struct usb_driver hiddev_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .name = "hiddev",
836 .probe = hiddev_usbd_probe,
837};
838
839int __init hiddev_init(void)
840{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return usb_register(&hiddev_driver);
842}
843
844void hiddev_exit(void)
845{
846 usb_deregister(&hiddev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}