blob: e793127f971eae29903298b7cb6d3ec64a156325 [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;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +020054 spinlock_t list_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
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;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040064 struct list_head node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
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{
Dmitry Torokhov826d5982006-07-19 01:09:10 -040078 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
79 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct hid_report_enum *report_enum;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040081 struct hid_report *report;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct list_head *list;
83
84 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -040085 rinfo->report_type > HID_REPORT_TYPE_MAX)
86 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 report_enum = hid->report_enum +
89 (rinfo->report_type - HID_REPORT_TYPE_MIN);
90
91 switch (flags) {
92 case 0: /* Nothing to do -- report_id is already set correctly */
93 break;
94
95 case HID_REPORT_ID_FIRST:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040096 if (list_empty(&report_enum->report_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040098
99 list = report_enum->report_list.next;
100 report = list_entry(list, struct hid_report, list);
101 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 case HID_REPORT_ID_NEXT:
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400105 report = report_enum->report_id_hash[rid];
106 if (!report)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400108
109 list = report->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (list == &report_enum->report_list)
111 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400112
113 report = list_entry(list, struct hid_report, list);
114 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 default:
118 return NULL;
119 }
120
121 return report_enum->report_id_hash[rinfo->report_id];
122}
123
124/*
125 * Perform an exhaustive search of the report table for a usage, given its
126 * type and usage id.
127 */
128static struct hid_field *
129hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
130{
131 int i, j;
132 struct hid_report *report;
133 struct hid_report_enum *report_enum;
134 struct hid_field *field;
135
136 if (uref->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400137 uref->report_type > HID_REPORT_TYPE_MAX)
138 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 report_enum = hid->report_enum +
141 (uref->report_type - HID_REPORT_TYPE_MIN);
142
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400143 list_for_each_entry(report, &report_enum->report_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 for (i = 0; i < report->maxfield; i++) {
145 field = report->field[i];
146 for (j = 0; j < field->maxusage; j++) {
147 if (field->usage[j].hid == uref->usage_code) {
148 uref->report_id = report->id;
149 uref->field_index = i;
150 uref->usage_index = j;
151 return field;
152 }
153 }
154 }
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return NULL;
158}
159
160static void hiddev_send_event(struct hid_device *hid,
161 struct hiddev_usage_ref *uref)
162{
163 struct hiddev *hiddev = hid->hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400164 struct hiddev_list *list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200165 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200167 spin_lock_irqsave(&hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400168 list_for_each_entry(list, &hiddev->list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (uref->field_index != HID_FIELD_INDEX_NONE ||
170 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
171 list->buffer[list->head] = *uref;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500172 list->head = (list->head + 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 (HIDDEV_BUFFER_SIZE - 1);
174 kill_fasync(&list->fasync, SIGIO, POLL_IN);
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200177 spin_unlock_irqrestore(&hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 wake_up_interruptible(&hiddev->wait);
180}
181
182/*
183 * This is where hid.c calls into hiddev to pass an event that occurred over
184 * the interrupt pipe
185 */
186void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
David Howells7d12e782006-10-05 14:55:46 +0100187 struct hid_usage *usage, __s32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 unsigned type = field->report_type;
190 struct hiddev_usage_ref uref;
191
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500192 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500194 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400195 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 uref.report_id = field->report->id;
197 uref.field_index = field->index;
198 uref.usage_index = (usage - field->usage);
199 uref.usage_code = usage->hid;
200 uref.value = value;
201
202 hiddev_send_event(hid, &uref);
203}
Jiri Kosina229695e2006-12-08 18:40:53 +0100204EXPORT_SYMBOL_GPL(hiddev_hid_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
207{
208 unsigned type = report->type;
209 struct hiddev_usage_ref uref;
210
211 memset(&uref, 0, sizeof(uref));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500212 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500214 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400215 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 uref.report_id = report->id;
217 uref.field_index = HID_FIELD_INDEX_NONE;
218
219 hiddev_send_event(hid, &uref);
220}
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * fasync file op
224 */
225static int hiddev_fasync(int fd, struct file *file, int on)
226{
227 int retval;
228 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 retval = fasync_helper(fd, file, on, &list->fasync);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return retval < 0 ? retval : 0;
233}
234
235
236/*
237 * release file op
238 */
239static int hiddev_release(struct inode * inode, struct file * file)
240{
241 struct hiddev_list *list = file->private_data;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200242 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 hiddev_fasync(-1, file, 0);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200245
246 spin_lock_irqsave(&list->hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400247 list_del(&list->node);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200248 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (!--list->hiddev->open) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500251 if (list->hiddev->exist)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100252 usbhid_close(list->hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 else
254 kfree(list->hiddev);
255 }
256
257 kfree(list);
258
259 return 0;
260}
261
262/*
263 * open file op
264 */
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400265static int hiddev_open(struct inode *inode, struct file *file)
266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct hiddev_list *list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200268 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 int i = iminor(inode) - HIDDEV_MINOR_BASE;
271
272 if (i >= HIDDEV_MINORS || !hiddev_table[i])
273 return -ENODEV;
274
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100275 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 list->hiddev = hiddev_table[i];
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200279
280 spin_lock_irqsave(&list->hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400281 list_add_tail(&list->node, &hiddev_table[i]->list);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200282 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 file->private_data = list;
285
286 if (!list->hiddev->open++)
287 if (list->hiddev->exist)
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100288 usbhid_open(hiddev_table[i]->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return 0;
291}
292
293/*
294 * "write" file op
295 */
296static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
297{
298 return -EINVAL;
299}
300
301/*
302 * "read" file op
303 */
304static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
305{
306 DECLARE_WAITQUEUE(wait, current);
307 struct hiddev_list *list = file->private_data;
308 int event_size;
309 int retval = 0;
310
311 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
312 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
313
314 if (count < event_size)
315 return 0;
316
317 while (retval == 0) {
318 if (list->head == list->tail) {
319 add_wait_queue(&list->hiddev->wait, &wait);
320 set_current_state(TASK_INTERRUPTIBLE);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 while (list->head == list->tail) {
323 if (file->f_flags & O_NONBLOCK) {
324 retval = -EAGAIN;
325 break;
326 }
327 if (signal_pending(current)) {
328 retval = -ERESTARTSYS;
329 break;
330 }
331 if (!list->hiddev->exist) {
332 retval = -EIO;
333 break;
334 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 schedule();
Micon, David48d70552006-05-20 14:59:59 -0700337 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 set_current_state(TASK_RUNNING);
341 remove_wait_queue(&list->hiddev->wait, &wait);
342 }
343
344 if (retval)
345 return retval;
346
347
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500348 while (list->head != list->tail &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 retval + event_size <= count) {
350 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
351 if (list->buffer[list->tail].field_index !=
352 HID_FIELD_INDEX_NONE) {
353 struct hiddev_event event;
354 event.hid = list->buffer[list->tail].usage_code;
355 event.value = list->buffer[list->tail].value;
356 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event)))
357 return -EFAULT;
358 retval += sizeof(struct hiddev_event);
359 }
360 } else {
361 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
362 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
363 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref)))
364 return -EFAULT;
365 retval += sizeof(struct hiddev_usage_ref);
366 }
367 }
368 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
369 }
370
371 }
372
373 return retval;
374}
375
376/*
377 * "poll" file op
378 * No kernel lock - fine
379 */
380static unsigned int hiddev_poll(struct file *file, poll_table *wait)
381{
382 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 poll_wait(file, &list->hiddev->wait, wait);
385 if (list->head != list->tail)
386 return POLLIN | POLLRDNORM;
387 if (!list->hiddev->exist)
388 return POLLERR | POLLHUP;
389 return 0;
390}
391
392/*
393 * "ioctl" file op
394 */
395static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
396{
397 struct hiddev_list *list = file->private_data;
398 struct hiddev *hiddev = list->hiddev;
399 struct hid_device *hid = hiddev->hid;
Anssi Hannulabe820972007-01-19 19:28:17 +0200400 struct usb_device *dev = hid_to_usb_dev(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct hiddev_collection_info cinfo;
402 struct hiddev_report_info rinfo;
403 struct hiddev_field_info finfo;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400404 struct hiddev_usage_ref_multi *uref_multi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct hiddev_usage_ref *uref;
406 struct hiddev_devinfo dinfo;
407 struct hid_report *report;
408 struct hid_field *field;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100409 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 void __user *user_arg = (void __user *)arg;
411 int i;
412
413 if (!hiddev->exist)
414 return -EIO;
415
416 switch (cmd) {
417
418 case HIDIOCGVERSION:
419 return put_user(HID_VERSION, (int __user *)arg);
420
421 case HIDIOCAPPLICATION:
422 if (arg < 0 || arg >= hid->maxapplication)
423 return -EINVAL;
424
425 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500426 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 HID_COLLECTION_APPLICATION && arg-- == 0)
428 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (i == hid->maxcollection)
431 return -EINVAL;
432
433 return hid->collection[i].usage;
434
435 case HIDIOCGDEVINFO:
436 dinfo.bustype = BUS_USB;
437 dinfo.busnum = dev->bus->busnum;
438 dinfo.devnum = dev->devnum;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100439 dinfo.ifnum = usbhid->ifnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
441 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
442 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
443 dinfo.num_applications = hid->maxapplication;
444 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
445 return -EFAULT;
446
447 return 0;
448
449 case HIDIOCGFLAG:
450 if (put_user(list->flags, (int __user *)arg))
451 return -EFAULT;
452
453 return 0;
454
455 case HIDIOCSFLAG:
456 {
457 int newflags;
458 if (get_user(newflags, (int __user *)arg))
459 return -EFAULT;
460
461 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
462 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
463 (newflags & HIDDEV_FLAG_UREF) == 0))
464 return -EINVAL;
465
466 list->flags = newflags;
467
468 return 0;
469 }
470
471 case HIDIOCGSTRING:
472 {
473 int idx, len;
474 char *buf;
475
476 if (get_user(idx, (int __user *)arg))
477 return -EFAULT;
478
479 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
480 return -ENOMEM;
481
482 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
483 kfree(buf);
484 return -EINVAL;
485 }
486
487 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
488 kfree(buf);
489 return -EFAULT;
490 }
491
492 kfree(buf);
493
494 return len;
495 }
496
497 case HIDIOCINITREPORT:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100498 usbhid_init_reports(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 return 0;
501
502 case HIDIOCGREPORT:
503 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
504 return -EFAULT;
505
506 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
507 return -EINVAL;
508
509 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
510 return -EINVAL;
511
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100512 usbhid_submit_report(hid, report, USB_DIR_IN);
513 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 return 0;
516
517 case HIDIOCSREPORT:
518 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
519 return -EFAULT;
520
521 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
522 return -EINVAL;
523
524 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
525 return -EINVAL;
526
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100527 usbhid_submit_report(hid, report, USB_DIR_OUT);
528 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 return 0;
531
532 case HIDIOCGREPORTINFO:
533 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo)))
534 return -EFAULT;
535
536 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
537 return -EINVAL;
538
539 rinfo.num_fields = report->maxfield;
540
541 if (copy_to_user(user_arg, &rinfo, sizeof(rinfo)))
542 return -EFAULT;
543
544 return 0;
545
546 case HIDIOCGFIELDINFO:
547 if (copy_from_user(&finfo, user_arg, sizeof(finfo)))
548 return -EFAULT;
549 rinfo.report_type = finfo.report_type;
550 rinfo.report_id = finfo.report_id;
551 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
552 return -EINVAL;
553
554 if (finfo.field_index >= report->maxfield)
555 return -EINVAL;
556
557 field = report->field[finfo.field_index];
558 memset(&finfo, 0, sizeof(finfo));
559 finfo.report_type = rinfo.report_type;
560 finfo.report_id = rinfo.report_id;
561 finfo.field_index = field->report_count - 1;
562 finfo.maxusage = field->maxusage;
563 finfo.flags = field->flags;
564 finfo.physical = field->physical;
565 finfo.logical = field->logical;
566 finfo.application = field->application;
567 finfo.logical_minimum = field->logical_minimum;
568 finfo.logical_maximum = field->logical_maximum;
569 finfo.physical_minimum = field->physical_minimum;
570 finfo.physical_maximum = field->physical_maximum;
571 finfo.unit_exponent = field->unit_exponent;
572 finfo.unit = field->unit;
573
574 if (copy_to_user(user_arg, &finfo, sizeof(finfo)))
575 return -EFAULT;
576
577 return 0;
578
579 case HIDIOCGUCODE:
580 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
581 if (!uref_multi)
582 return -ENOMEM;
583 uref = &uref_multi->uref;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500584 if (copy_from_user(uref, user_arg, sizeof(*uref)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto fault;
586
587 rinfo.report_type = uref->report_type;
588 rinfo.report_id = uref->report_id;
589 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
590 goto inval;
591
592 if (uref->field_index >= report->maxfield)
593 goto inval;
594
595 field = report->field[uref->field_index];
596 if (uref->usage_index >= field->maxusage)
597 goto inval;
598
599 uref->usage_code = field->usage[uref->usage_index].hid;
600
601 if (copy_to_user(user_arg, uref, sizeof(*uref)))
602 goto fault;
603
604 kfree(uref_multi);
605 return 0;
606
607 case HIDIOCGUSAGE:
608 case HIDIOCSUSAGE:
609 case HIDIOCGUSAGES:
610 case HIDIOCSUSAGES:
611 case HIDIOCGCOLLECTIONINDEX:
612 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
613 if (!uref_multi)
614 return -ENOMEM;
615 uref = &uref_multi->uref;
616 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500617 if (copy_from_user(uref_multi, user_arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 sizeof(*uref_multi)))
619 goto fault;
620 } else {
621 if (copy_from_user(uref, user_arg, sizeof(*uref)))
622 goto fault;
623 }
624
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500625 if (cmd != HIDIOCGUSAGE &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 cmd != HIDIOCGUSAGES &&
627 uref->report_type == HID_REPORT_TYPE_INPUT)
628 goto inval;
629
630 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
631 field = hiddev_lookup_usage(hid, uref);
632 if (field == NULL)
633 goto inval;
634 } else {
635 rinfo.report_type = uref->report_type;
636 rinfo.report_id = uref->report_id;
637 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
638 goto inval;
639
640 if (uref->field_index >= report->maxfield)
641 goto inval;
642
643 field = report->field[uref->field_index];
644
645 if (cmd == HIDIOCGCOLLECTIONINDEX) {
646 if (uref->usage_index >= field->maxusage)
647 goto inval;
648 } else if (uref->usage_index >= field->report_count)
649 goto inval;
650
651 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
652 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
Ben Collins6dea9342006-01-31 01:31:13 -0500653 uref->usage_index + uref_multi->num_values > field->report_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto inval;
655 }
656
657 switch (cmd) {
658 case HIDIOCGUSAGE:
659 uref->value = field->value[uref->usage_index];
660 if (copy_to_user(user_arg, uref, sizeof(*uref)))
661 goto fault;
662 goto goodreturn;
663
664 case HIDIOCSUSAGE:
665 field->value[uref->usage_index] = uref->value;
666 goto goodreturn;
667
668 case HIDIOCGCOLLECTIONINDEX:
669 kfree(uref_multi);
670 return field->usage[uref->usage_index].collection_index;
671 case HIDIOCGUSAGES:
672 for (i = 0; i < uref_multi->num_values; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500673 uref_multi->values[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 field->value[uref->usage_index + i];
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500675 if (copy_to_user(user_arg, uref_multi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 sizeof(*uref_multi)))
677 goto fault;
678 goto goodreturn;
679 case HIDIOCSUSAGES:
680 for (i = 0; i < uref_multi->num_values; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500681 field->value[uref->usage_index + i] =
682 uref_multi->values[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto goodreturn;
684 }
685
686goodreturn:
687 kfree(uref_multi);
688 return 0;
689fault:
690 kfree(uref_multi);
691 return -EFAULT;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500692inval:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree(uref_multi);
694 return -EINVAL;
695
696 case HIDIOCGCOLLECTIONINFO:
697 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo)))
698 return -EFAULT;
699
700 if (cinfo.index >= hid->maxcollection)
701 return -EINVAL;
702
703 cinfo.type = hid->collection[cinfo.index].type;
704 cinfo.usage = hid->collection[cinfo.index].usage;
705 cinfo.level = hid->collection[cinfo.index].level;
706
707 if (copy_to_user(user_arg, &cinfo, sizeof(cinfo)))
708 return -EFAULT;
709 return 0;
710
711 default:
712
713 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
714 return -EINVAL;
715
716 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
717 int len;
718 if (!hid->name)
719 return 0;
720 len = strlen(hid->name) + 1;
721 if (len > _IOC_SIZE(cmd))
722 len = _IOC_SIZE(cmd);
723 return copy_to_user(user_arg, hid->name, len) ?
724 -EFAULT : len;
725 }
726
727 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
728 int len;
729 if (!hid->phys)
730 return 0;
731 len = strlen(hid->phys) + 1;
732 if (len > _IOC_SIZE(cmd))
733 len = _IOC_SIZE(cmd);
734 return copy_to_user(user_arg, hid->phys, len) ?
735 -EFAULT : len;
736 }
737 }
738 return -EINVAL;
739}
740
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300741static const struct file_operations hiddev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .owner = THIS_MODULE,
743 .read = hiddev_read,
744 .write = hiddev_write,
745 .poll = hiddev_poll,
746 .open = hiddev_open,
747 .release = hiddev_release,
748 .ioctl = hiddev_ioctl,
749 .fasync = hiddev_fasync,
750};
751
752static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700753 .name = "hiddev%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 .fops = &hiddev_fops,
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500755 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756};
757
758/*
759 * This is where hid.c calls us to connect a hid device to the hiddev driver
760 */
761int hiddev_connect(struct hid_device *hid)
762{
763 struct hiddev *hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100764 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int i;
766 int retval;
767
768 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500769 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 HID_COLLECTION_APPLICATION &&
771 !IS_INPUT_APPLICATION(hid->collection[i].usage))
772 break;
773
774 if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0)
775 return -1;
776
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100777 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100780 retval = usb_register_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (retval) {
Jiri Kosina58037eb2007-05-30 15:07:13 +0200782 err_hid("Not able to get a minor for this device.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 kfree(hiddev);
784 return -1;
785 }
786
787 init_waitqueue_head(&hiddev->wait);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400788 INIT_LIST_HEAD(&hiddev->list);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200789 spin_lock_init(&hiddev->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 hiddev->hid = hid;
791 hiddev->exist = 1;
792
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100793 hid->minor = usbhid->intf->minor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 hid->hiddev = hiddev;
795
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100796 hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799}
800
801/*
802 * This is where hid.c calls us to disconnect a hiddev device from the
803 * corresponding hid device (usually because the usb device has disconnected)
804 */
805static struct usb_class_driver hiddev_class;
806void hiddev_disconnect(struct hid_device *hid)
807{
808 struct hiddev *hiddev = hid->hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100809 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 hiddev->exist = 0;
812
813 hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100814 usb_deregister_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (hiddev->open) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100817 usbhid_close(hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 wake_up_interruptible(&hiddev->wait);
819 } else {
820 kfree(hiddev);
821 }
822}
823
824/* Currently this driver is a USB driver. It's not a conventional one in
825 * the sense that it doesn't probe at the USB level. Instead it waits to
826 * be connected by HID through the hiddev_connect / hiddev_disconnect
827 * routines. The reason to register as a USB device is to gain part of the
828 * minor number space from the USB major.
829 *
830 * In theory, should the HID code be generalized to more than one physical
831 * medium (say, IEEE 1384), this driver will probably need to register its
832 * own major number, and in doing so, no longer need to register with USB.
833 * At that point the probe routine and hiddev_driver struct below will no
834 * longer be useful.
835 */
836
837
838/* We never attach in this manner, and rely on HID to connect us. This
839 * is why there is no disconnect routine defined in the usb_driver either.
840 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500841static int hiddev_usbd_probe(struct usb_interface *intf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 const struct usb_device_id *hiddev_info)
843{
844 return -ENODEV;
845}
846
847
848static /* const */ struct usb_driver hiddev_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 .name = "hiddev",
850 .probe = hiddev_usbd_probe,
851};
852
853int __init hiddev_init(void)
854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return usb_register(&hiddev_driver);
856}
857
858void hiddev_exit(void)
859{
860 usb_deregister(&hiddev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}