blob: f4c67a5021c733e730708ff1fb545cd52755272e [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/input.h>
33#include <linux/usb.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010034#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/hiddev.h>
Philip Langdalebb6c8d82007-10-14 12:03:58 +020036#include <linux/compat.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
Jiri Kosinaaffbb8c2009-08-20 12:04:14 +020046#define HIDDEV_BUFFER_SIZE 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48struct hiddev {
49 int exist;
50 int open;
Oliver Neukum07903402008-12-16 10:55:15 +010051 struct mutex existancelock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 wait_queue_head_t wait;
53 struct hid_device *hid;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040054 struct list_head list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +020055 spinlock_t list_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58struct hiddev_list {
59 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
60 int head;
61 int tail;
62 unsigned flags;
63 struct fasync_struct *fasync;
64 struct hiddev *hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040065 struct list_head node;
Oliver Neukum07903402008-12-16 10:55:15 +010066 struct mutex thread_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400228
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700229 return fasync_helper(fd, file, on, &list->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232
233/*
234 * release file op
235 */
236static int hiddev_release(struct inode * inode, struct file * file)
237{
238 struct hiddev_list *list = file->private_data;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200239 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200241 spin_lock_irqsave(&list->hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400242 list_del(&list->node);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200243 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200245 mutex_lock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (!--list->hiddev->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100247 if (list->hiddev->exist) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100248 usbhid_close(list->hiddev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100249 usbhid_put_power(list->hiddev->hid);
250 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 kfree(list->hiddev);
Oliver Neukum0361a282008-12-17 15:38:03 +0100252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 kfree(list);
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200256 mutex_unlock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return 0;
259}
260
261/*
262 * open file op
263 */
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400264static int hiddev_open(struct inode *inode, struct file *file)
265{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct hiddev_list *list;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200267 struct usb_interface *intf;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200268 struct hid_device *hid;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200269 struct hiddev *hiddev;
270 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +0200272 intf = usbhid_find_interface(iminor(inode));
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200273 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -ENODEV;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200275 hid = usb_get_intfdata(intf);
276 hiddev = hid->hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100278 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -ENOMEM;
Oliver Neukum07903402008-12-16 10:55:15 +0100280 mutex_init(&list->thread_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200281 list->hiddev = hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 file->private_data = list;
283
Oliver Neukum07903402008-12-16 10:55:15 +0100284 /*
285 * no need for locking because the USB major number
286 * is shared which usbcore guards against disconnect
287 */
288 if (list->hiddev->exist) {
289 if (!list->hiddev->open++) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200290 res = usbhid_open(hiddev->hid);
Oliver Neukum07903402008-12-16 10:55:15 +0100291 if (res < 0) {
292 res = -EIO;
293 goto bail;
294 }
295 }
296 } else {
297 res = -ENODEV;
298 goto bail;
299 }
300
301 spin_lock_irq(&list->hiddev->list_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200302 list_add_tail(&list->node, &hiddev->list);
Oliver Neukum07903402008-12-16 10:55:15 +0100303 spin_unlock_irq(&list->hiddev->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200305 mutex_lock(&hiddev->existancelock);
Oliver Neukum0361a282008-12-17 15:38:03 +0100306 if (!list->hiddev->open++)
307 if (list->hiddev->exist) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200308 struct hid_device *hid = hiddev->hid;
Oliver Neukum0361a282008-12-17 15:38:03 +0100309 res = usbhid_get_power(hid);
310 if (res < 0) {
311 res = -EIO;
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200312 goto bail_unlock;
Oliver Neukum0361a282008-12-17 15:38:03 +0100313 }
314 usbhid_open(hid);
315 }
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200316 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200318bail_unlock:
319 mutex_unlock(&hiddev->existancelock);
Oliver Neukum07903402008-12-16 10:55:15 +0100320bail:
321 file->private_data = NULL;
Johannes Weiner48e7a3c2009-03-10 22:43:56 +0100322 kfree(list);
Oliver Neukum07903402008-12-16 10:55:15 +0100323 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/*
327 * "write" file op
328 */
329static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
330{
331 return -EINVAL;
332}
333
334/*
335 * "read" file op
336 */
337static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
338{
Johannes Weiner96fe2ab2009-03-10 22:44:01 +0100339 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct hiddev_list *list = file->private_data;
341 int event_size;
Oliver Neukum07903402008-12-16 10:55:15 +0100342 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
345 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
346
347 if (count < event_size)
348 return 0;
349
Oliver Neukum07903402008-12-16 10:55:15 +0100350 /* lock against other threads */
351 retval = mutex_lock_interruptible(&list->thread_lock);
352 if (retval)
353 return -ERESTARTSYS;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 while (retval == 0) {
356 if (list->head == list->tail) {
Oliver Neukum07903402008-12-16 10:55:15 +0100357 prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 while (list->head == list->tail) {
360 if (file->f_flags & O_NONBLOCK) {
361 retval = -EAGAIN;
362 break;
363 }
364 if (signal_pending(current)) {
365 retval = -ERESTARTSYS;
366 break;
367 }
368 if (!list->hiddev->exist) {
369 retval = -EIO;
370 break;
371 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500372
Oliver Neukum07903402008-12-16 10:55:15 +0100373 /* let O_NONBLOCK tasks run */
374 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 schedule();
Oliver Neukum07903402008-12-16 10:55:15 +0100376 if (mutex_lock_interruptible(&list->thread_lock))
377 return -EINTR;
Micon, David48d70552006-05-20 14:59:59 -0700378 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Oliver Neukum07903402008-12-16 10:55:15 +0100380 finish_wait(&list->hiddev->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Oliver Neukum07903402008-12-16 10:55:15 +0100384 if (retval) {
385 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return retval;
Oliver Neukum07903402008-12-16 10:55:15 +0100387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500390 while (list->head != list->tail &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 retval + event_size <= count) {
392 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100393 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct hiddev_event event;
Oliver Neukum07903402008-12-16 10:55:15 +0100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 event.hid = list->buffer[list->tail].usage_code;
397 event.value = list->buffer[list->tail].value;
Oliver Neukum07903402008-12-16 10:55:15 +0100398 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
399 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return -EFAULT;
Oliver Neukum07903402008-12-16 10:55:15 +0100401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 retval += sizeof(struct hiddev_event);
403 }
404 } else {
405 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
406 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100407
408 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
409 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -EFAULT;
Oliver Neukum07903402008-12-16 10:55:15 +0100411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 retval += sizeof(struct hiddev_usage_ref);
413 }
414 }
415 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
416 }
417
418 }
Oliver Neukum07903402008-12-16 10:55:15 +0100419 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 return retval;
422}
423
424/*
425 * "poll" file op
426 * No kernel lock - fine
427 */
428static unsigned int hiddev_poll(struct file *file, poll_table *wait)
429{
430 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 poll_wait(file, &list->hiddev->wait, wait);
433 if (list->head != list->tail)
434 return POLLIN | POLLRDNORM;
435 if (!list->hiddev->exist)
436 return POLLERR | POLLHUP;
437 return 0;
438}
439
440/*
441 * "ioctl" file op
442 */
Jean Delvarecf2a299e2008-03-03 11:48:43 +0100443static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
444{
445 struct hid_device *hid = hiddev->hid;
446 struct hiddev_report_info rinfo;
447 struct hiddev_usage_ref_multi *uref_multi = NULL;
448 struct hiddev_usage_ref *uref;
449 struct hid_report *report;
450 struct hid_field *field;
451 int i;
452
453 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
454 if (!uref_multi)
455 return -ENOMEM;
456 uref = &uref_multi->uref;
457 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
458 if (copy_from_user(uref_multi, user_arg,
459 sizeof(*uref_multi)))
460 goto fault;
461 } else {
462 if (copy_from_user(uref, user_arg, sizeof(*uref)))
463 goto fault;
464 }
465
466 switch (cmd) {
467 case HIDIOCGUCODE:
468 rinfo.report_type = uref->report_type;
469 rinfo.report_id = uref->report_id;
470 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
471 goto inval;
472
473 if (uref->field_index >= report->maxfield)
474 goto inval;
475
476 field = report->field[uref->field_index];
477 if (uref->usage_index >= field->maxusage)
478 goto inval;
479
480 uref->usage_code = field->usage[uref->usage_index].hid;
481
482 if (copy_to_user(user_arg, uref, sizeof(*uref)))
483 goto fault;
484
Jiri Slabyeb991082008-10-23 01:47:34 +0200485 goto goodreturn;
Jean Delvarecf2a299e2008-03-03 11:48:43 +0100486
487 default:
488 if (cmd != HIDIOCGUSAGE &&
489 cmd != HIDIOCGUSAGES &&
490 uref->report_type == HID_REPORT_TYPE_INPUT)
491 goto inval;
492
493 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
494 field = hiddev_lookup_usage(hid, uref);
495 if (field == NULL)
496 goto inval;
497 } else {
498 rinfo.report_type = uref->report_type;
499 rinfo.report_id = uref->report_id;
500 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
501 goto inval;
502
503 if (uref->field_index >= report->maxfield)
504 goto inval;
505
506 field = report->field[uref->field_index];
507
508 if (cmd == HIDIOCGCOLLECTIONINDEX) {
509 if (uref->usage_index >= field->maxusage)
510 goto inval;
511 } else if (uref->usage_index >= field->report_count)
512 goto inval;
513
514 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
515 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
516 uref->usage_index + uref_multi->num_values > field->report_count))
517 goto inval;
Dan Carpenterac065bf2011-03-26 04:47:35 +0300518 }
Jean Delvarecf2a299e2008-03-03 11:48:43 +0100519
520 switch (cmd) {
521 case HIDIOCGUSAGE:
522 uref->value = field->value[uref->usage_index];
523 if (copy_to_user(user_arg, uref, sizeof(*uref)))
524 goto fault;
525 goto goodreturn;
526
527 case HIDIOCSUSAGE:
528 field->value[uref->usage_index] = uref->value;
529 goto goodreturn;
530
531 case HIDIOCGCOLLECTIONINDEX:
Jiri Slaby48594842009-06-19 23:24:11 +0200532 i = field->usage[uref->usage_index].collection_index;
Jean Delvarecf2a299e2008-03-03 11:48:43 +0100533 kfree(uref_multi);
Jiri Slaby48594842009-06-19 23:24:11 +0200534 return i;
Jean Delvarecf2a299e2008-03-03 11:48:43 +0100535 case HIDIOCGUSAGES:
536 for (i = 0; i < uref_multi->num_values; i++)
537 uref_multi->values[i] =
538 field->value[uref->usage_index + i];
539 if (copy_to_user(user_arg, uref_multi,
540 sizeof(*uref_multi)))
541 goto fault;
542 goto goodreturn;
543 case HIDIOCSUSAGES:
544 for (i = 0; i < uref_multi->num_values; i++)
545 field->value[uref->usage_index + i] =
546 uref_multi->values[i];
547 goto goodreturn;
548 }
549
550goodreturn:
551 kfree(uref_multi);
552 return 0;
553fault:
554 kfree(uref_multi);
555 return -EFAULT;
556inval:
557 kfree(uref_multi);
558 return -EINVAL;
559 }
560}
561
562static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
563{
564 struct hid_device *hid = hiddev->hid;
565 struct usb_device *dev = hid_to_usb_dev(hid);
566 int idx, len;
567 char *buf;
568
569 if (get_user(idx, (int __user *)user_arg))
570 return -EFAULT;
571
572 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
573 return -ENOMEM;
574
575 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
576 kfree(buf);
577 return -EINVAL;
578 }
579
580 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
581 kfree(buf);
582 return -EFAULT;
583 }
584
585 kfree(buf);
586
587 return len;
588}
589
Alan Cox7961df12008-05-26 11:25:20 +0200590static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct hiddev_list *list = file->private_data;
593 struct hiddev *hiddev = list->hiddev;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300594 struct hid_device *hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct hiddev_collection_info cinfo;
596 struct hiddev_report_info rinfo;
597 struct hiddev_field_info finfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct hiddev_devinfo dinfo;
599 struct hid_report *report;
600 struct hid_field *field;
601 void __user *user_arg = (void __user *)arg;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300602 int i, r = -EINVAL;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300603
Alan Cox7961df12008-05-26 11:25:20 +0200604 /* Called without BKL by compat methods so no BKL taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300606 mutex_lock(&hiddev->existancelock);
607 if (!hiddev->exist) {
608 r = -ENODEV;
609 goto ret_unlock;
610 }
611
612 hid = hiddev->hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 switch (cmd) {
615
616 case HIDIOCGVERSION:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300617 r = put_user(HID_VERSION, (int __user *)arg) ?
618 -EFAULT : 0;
619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 case HIDIOCAPPLICATION:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300622 if (arg < 0 || arg >= hid->maxapplication)
623 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500626 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 HID_COLLECTION_APPLICATION && arg-- == 0)
628 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500629
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300630 if (i < hid->maxcollection)
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300631 r = hid->collection[i].usage;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 case HIDIOCGDEVINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300635 {
636 struct usb_device *dev = hid_to_usb_dev(hid);
637 struct usbhid_device *usbhid = hid->driver_data;
638
639 dinfo.bustype = BUS_USB;
640 dinfo.busnum = dev->bus->busnum;
641 dinfo.devnum = dev->devnum;
642 dinfo.ifnum = usbhid->ifnum;
643 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
644 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
645 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
646 dinfo.num_applications = hid->maxapplication;
647
648 r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
649 -EFAULT : 0;
650 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300651 }
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 case HIDIOCGFLAG:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300654 r = put_user(list->flags, (int __user *)arg) ?
655 -EFAULT : 0;
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 case HIDIOCSFLAG:
659 {
660 int newflags;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300661
662 if (get_user(newflags, (int __user *)arg)) {
663 r = -EFAULT;
664 break;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
668 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
669 (newflags & HIDDEV_FLAG_UREF) == 0))
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 list->flags = newflags;
673
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300674 r = 0;
675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 case HIDIOCGSTRING:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300679 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 case HIDIOCINITREPORT:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100683 usbhid_init_reports(hid);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300684 r = 0;
685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 case HIDIOCGREPORT:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300688 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
689 r = -EFAULT;
690 break;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300696 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300697 if (report == NULL)
698 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300699
700 usbhid_submit_report(hid, report, USB_DIR_IN);
701 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300703 r = 0;
704 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 case HIDIOCSREPORT:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300707 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
708 r = -EFAULT;
709 break;
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300715 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300716 if (report == NULL)
717 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300718
719 usbhid_submit_report(hid, report, USB_DIR_OUT);
720 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300722 r = 0;
723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 case HIDIOCGREPORTINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300726 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
727 r = -EFAULT;
728 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300729 }
730
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300731 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300732 if (report == NULL)
733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 rinfo.num_fields = report->maxfield;
736
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300737 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
738 -EFAULT : 0;
739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 case HIDIOCGFIELDINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300742 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
743 r = -EFAULT;
744 break;
745 }
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 rinfo.report_type = finfo.report_type;
748 rinfo.report_id = finfo.report_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300750 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300751 if (report == NULL)
752 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300753
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300754 if (finfo.field_index >= report->maxfield)
755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 field = report->field[finfo.field_index];
758 memset(&finfo, 0, sizeof(finfo));
759 finfo.report_type = rinfo.report_type;
760 finfo.report_id = rinfo.report_id;
761 finfo.field_index = field->report_count - 1;
762 finfo.maxusage = field->maxusage;
763 finfo.flags = field->flags;
764 finfo.physical = field->physical;
765 finfo.logical = field->logical;
766 finfo.application = field->application;
767 finfo.logical_minimum = field->logical_minimum;
768 finfo.logical_maximum = field->logical_maximum;
769 finfo.physical_minimum = field->physical_minimum;
770 finfo.physical_maximum = field->physical_maximum;
771 finfo.unit_exponent = field->unit_exponent;
772 finfo.unit = field->unit;
773
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300774 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
775 -EFAULT : 0;
776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 case HIDIOCGUCODE:
Jean Delvarecf2a299e2008-03-03 11:48:43 +0100779 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 case HIDIOCGUSAGE:
781 case HIDIOCSUSAGE:
782 case HIDIOCGUSAGES:
783 case HIDIOCSUSAGES:
784 case HIDIOCGCOLLECTIONINDEX:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300785 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 case HIDIOCGCOLLECTIONINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300789 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
790 r = -EFAULT;
791 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300792 }
793
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300794 if (cinfo.index >= hid->maxcollection)
795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 cinfo.type = hid->collection[cinfo.index].type;
798 cinfo.usage = hid->collection[cinfo.index].usage;
799 cinfo.level = hid->collection[cinfo.index].level;
800
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300801 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
802 -EFAULT : 0;
803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200810 int len = strlen(hid->name) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (len > _IOC_SIZE(cmd))
812 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300813 r = copy_to_user(user_arg, hid->name, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 -EFAULT : len;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
818 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200819 int len = strlen(hid->phys) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (len > _IOC_SIZE(cmd))
821 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300822 r = copy_to_user(user_arg, hid->phys, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 -EFAULT : len;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 }
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300827
828ret_unlock:
829 mutex_unlock(&hiddev->existancelock);
830 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200833#ifdef CONFIG_COMPAT
834static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
835{
Jiri Kosina88af45b2008-05-27 11:36:40 +0200836 return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200837}
838#endif
839
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300840static const struct file_operations hiddev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 .owner = THIS_MODULE,
842 .read = hiddev_read,
843 .write = hiddev_write,
844 .poll = hiddev_poll,
845 .open = hiddev_open,
846 .release = hiddev_release,
Alan Cox7961df12008-05-26 11:25:20 +0200847 .unlocked_ioctl = hiddev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 .fasync = hiddev_fasync,
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200849#ifdef CONFIG_COMPAT
850 .compat_ioctl = hiddev_compat_ioctl,
851#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200852 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853};
854
Kay Sieverse454cea2009-09-18 23:01:12 +0200855static char *hiddev_devnode(struct device *dev, mode_t *mode)
Kay Sieversf7a386c2009-04-30 15:23:42 +0200856{
857 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700861 .name = "hiddev%d",
Kay Sieverse454cea2009-09-18 23:01:12 +0200862 .devnode = hiddev_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 .fops = &hiddev_fops,
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500864 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
866
867/*
868 * This is where hid.c calls us to connect a hid device to the hiddev driver
869 */
Jiri Slaby93c10132008-06-27 00:04:24 +0200870int hiddev_connect(struct hid_device *hid, unsigned int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct hiddev *hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100873 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int retval;
875
Jiri Slaby93c10132008-06-27 00:04:24 +0200876 if (!force) {
877 unsigned int i;
878 for (i = 0; i < hid->maxcollection; i++)
879 if (hid->collection[i].type ==
880 HID_COLLECTION_APPLICATION &&
881 !IS_INPUT_APPLICATION(hid->collection[i].usage))
882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Jiri Slaby93c10132008-06-27 00:04:24 +0200884 if (i == hid->maxcollection)
885 return -1;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100888 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Oliver Neukum07903402008-12-16 10:55:15 +0100891 init_waitqueue_head(&hiddev->wait);
892 INIT_LIST_HEAD(&hiddev->list);
893 spin_lock_init(&hiddev->list_lock);
894 mutex_init(&hiddev->existancelock);
Jiri Kosina76052742009-01-07 13:25:36 +0100895 hid->hiddev = hiddev;
Oliver Neukum07903402008-12-16 10:55:15 +0100896 hiddev->hid = hid;
897 hiddev->exist = 1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100898 retval = usb_register_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800900 hid_err(hid, "Not able to get a minor for this device\n");
Jiri Kosina76052742009-01-07 13:25:36 +0100901 hid->hiddev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 kfree(hiddev);
903 return -1;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return 0;
906}
907
908/*
909 * This is where hid.c calls us to disconnect a hiddev device from the
910 * corresponding hid device (usually because the usb device has disconnected)
911 */
912static struct usb_class_driver hiddev_class;
913void hiddev_disconnect(struct hid_device *hid)
914{
915 struct hiddev *hiddev = hid->hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100916 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Oliver Neukum07903402008-12-16 10:55:15 +0100918 mutex_lock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 hiddev->exist = 0;
920
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100921 usb_deregister_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (hiddev->open) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100924 usbhid_close(hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 wake_up_interruptible(&hiddev->wait);
926 } else {
927 kfree(hiddev);
928 }
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200929 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}