blob: fb78f75d49a9442cf0e503d3db64657e98af4c96 [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>
Philip Langdalebb6c8d82007-10-14 12:03:58 +020037#include <linux/compat.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010038#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#ifdef CONFIG_USB_DYNAMIC_MINORS
41#define HIDDEV_MINOR_BASE 0
42#define HIDDEV_MINORS 256
43#else
44#define HIDDEV_MINOR_BASE 96
45#define HIDDEV_MINORS 16
46#endif
Jiri Kosinaaffbb8c2009-08-20 12:04:14 +020047#define HIDDEV_BUFFER_SIZE 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49struct hiddev {
50 int exist;
51 int open;
Oliver Neukum07903402008-12-16 10:55:15 +010052 struct mutex existancelock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 wait_queue_head_t wait;
54 struct hid_device *hid;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040055 struct list_head list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +020056 spinlock_t list_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59struct hiddev_list {
60 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
61 int head;
62 int tail;
63 unsigned flags;
64 struct fasync_struct *fasync;
65 struct hiddev *hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040066 struct list_head node;
Oliver Neukum07903402008-12-16 10:55:15 +010067 struct mutex thread_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Find a report, given the report's type and ID. The ID can be specified
72 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
73 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
74 * given type which follows old_id.
75 */
76static struct hid_report *
77hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
78{
Dmitry Torokhov826d5982006-07-19 01:09:10 -040079 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
80 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct hid_report_enum *report_enum;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040082 struct hid_report *report;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct list_head *list;
84
85 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -040086 rinfo->report_type > HID_REPORT_TYPE_MAX)
87 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 report_enum = hid->report_enum +
90 (rinfo->report_type - HID_REPORT_TYPE_MIN);
91
92 switch (flags) {
93 case 0: /* Nothing to do -- report_id is already set correctly */
94 break;
95
96 case HID_REPORT_ID_FIRST:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040097 if (list_empty(&report_enum->report_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040099
100 list = report_enum->report_list.next;
101 report = list_entry(list, struct hid_report, list);
102 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 case HID_REPORT_ID_NEXT:
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400106 report = report_enum->report_id_hash[rid];
107 if (!report)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400109
110 list = report->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (list == &report_enum->report_list)
112 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400113
114 report = list_entry(list, struct hid_report, list);
115 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 default:
119 return NULL;
120 }
121
122 return report_enum->report_id_hash[rinfo->report_id];
123}
124
125/*
126 * Perform an exhaustive search of the report table for a usage, given its
127 * type and usage id.
128 */
129static struct hid_field *
130hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
131{
132 int i, j;
133 struct hid_report *report;
134 struct hid_report_enum *report_enum;
135 struct hid_field *field;
136
137 if (uref->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400138 uref->report_type > HID_REPORT_TYPE_MAX)
139 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 report_enum = hid->report_enum +
142 (uref->report_type - HID_REPORT_TYPE_MIN);
143
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400144 list_for_each_entry(report, &report_enum->report_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 for (i = 0; i < report->maxfield; i++) {
146 field = report->field[i];
147 for (j = 0; j < field->maxusage; j++) {
148 if (field->usage[j].hid == uref->usage_code) {
149 uref->report_id = report->id;
150 uref->field_index = i;
151 uref->usage_index = j;
152 return field;
153 }
154 }
155 }
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return NULL;
159}
160
161static void hiddev_send_event(struct hid_device *hid,
162 struct hiddev_usage_ref *uref)
163{
164 struct hiddev *hiddev = hid->hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400165 struct hiddev_list *list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200166 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200168 spin_lock_irqsave(&hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400169 list_for_each_entry(list, &hiddev->list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (uref->field_index != HID_FIELD_INDEX_NONE ||
171 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
172 list->buffer[list->head] = *uref;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500173 list->head = (list->head + 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 (HIDDEV_BUFFER_SIZE - 1);
175 kill_fasync(&list->fasync, SIGIO, POLL_IN);
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200178 spin_unlock_irqrestore(&hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 wake_up_interruptible(&hiddev->wait);
181}
182
183/*
184 * This is where hid.c calls into hiddev to pass an event that occurred over
185 * the interrupt pipe
186 */
187void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
David Howells7d12e782006-10-05 14:55:46 +0100188 struct hid_usage *usage, __s32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 unsigned type = field->report_type;
191 struct hiddev_usage_ref uref;
192
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500193 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500195 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400196 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 uref.report_id = field->report->id;
198 uref.field_index = field->index;
199 uref.usage_index = (usage - field->usage);
200 uref.usage_code = usage->hid;
201 uref.value = value;
202
203 hiddev_send_event(hid, &uref);
204}
Jiri Kosina229695e2006-12-08 18:40:53 +0100205EXPORT_SYMBOL_GPL(hiddev_hid_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
208{
209 unsigned type = report->type;
210 struct hiddev_usage_ref uref;
211
212 memset(&uref, 0, sizeof(uref));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500213 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500215 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400216 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 uref.report_id = report->id;
218 uref.field_index = HID_FIELD_INDEX_NONE;
219
220 hiddev_send_event(hid, &uref);
221}
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*
224 * fasync file op
225 */
226static int hiddev_fasync(int fd, struct file *file, int on)
227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400229
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700230 return fasync_helper(fd, file, on, &list->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233
234/*
235 * release file op
236 */
237static int hiddev_release(struct inode * inode, struct file * file)
238{
239 struct hiddev_list *list = file->private_data;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200240 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200242 spin_lock_irqsave(&list->hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400243 list_del(&list->node);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200244 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 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);
256
257 return 0;
258}
259
260/*
261 * open file op
262 */
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400263static int hiddev_open(struct inode *inode, struct file *file)
264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct hiddev_list *list;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200266 struct usb_interface *intf;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200267 struct hid_device *hid;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200268 struct hiddev *hiddev;
269 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +0200271 intf = usbhid_find_interface(iminor(inode));
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200272 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -ENODEV;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200274 hid = usb_get_intfdata(intf);
275 hiddev = hid->hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100277 if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -ENOMEM;
Oliver Neukum07903402008-12-16 10:55:15 +0100279 mutex_init(&list->thread_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200280 list->hiddev = hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 file->private_data = list;
282
Oliver Neukum07903402008-12-16 10:55:15 +0100283 /*
284 * no need for locking because the USB major number
285 * is shared which usbcore guards against disconnect
286 */
287 if (list->hiddev->exist) {
288 if (!list->hiddev->open++) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200289 res = usbhid_open(hiddev->hid);
Oliver Neukum07903402008-12-16 10:55:15 +0100290 if (res < 0) {
291 res = -EIO;
292 goto bail;
293 }
294 }
295 } else {
296 res = -ENODEV;
297 goto bail;
298 }
299
300 spin_lock_irq(&list->hiddev->list_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200301 list_add_tail(&list->node, &hiddev->list);
Oliver Neukum07903402008-12-16 10:55:15 +0100302 spin_unlock_irq(&list->hiddev->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Oliver Neukum0361a282008-12-17 15:38:03 +0100304 if (!list->hiddev->open++)
305 if (list->hiddev->exist) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200306 struct hid_device *hid = hiddev->hid;
Oliver Neukum0361a282008-12-17 15:38:03 +0100307 res = usbhid_get_power(hid);
308 if (res < 0) {
309 res = -EIO;
310 goto bail;
311 }
312 usbhid_open(hid);
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
Oliver Neukum07903402008-12-16 10:55:15 +0100315bail:
316 file->private_data = NULL;
Johannes Weiner48e7a3c2009-03-10 22:43:56 +0100317 kfree(list);
Oliver Neukum07903402008-12-16 10:55:15 +0100318 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
322 * "write" file op
323 */
324static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
325{
326 return -EINVAL;
327}
328
329/*
330 * "read" file op
331 */
332static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
333{
Johannes Weiner96fe2ab2009-03-10 22:44:01 +0100334 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct hiddev_list *list = file->private_data;
336 int event_size;
Oliver Neukum07903402008-12-16 10:55:15 +0100337 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
340 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
341
342 if (count < event_size)
343 return 0;
344
Oliver Neukum07903402008-12-16 10:55:15 +0100345 /* lock against other threads */
346 retval = mutex_lock_interruptible(&list->thread_lock);
347 if (retval)
348 return -ERESTARTSYS;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 while (retval == 0) {
351 if (list->head == list->tail) {
Oliver Neukum07903402008-12-16 10:55:15 +0100352 prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 while (list->head == list->tail) {
355 if (file->f_flags & O_NONBLOCK) {
356 retval = -EAGAIN;
357 break;
358 }
359 if (signal_pending(current)) {
360 retval = -ERESTARTSYS;
361 break;
362 }
363 if (!list->hiddev->exist) {
364 retval = -EIO;
365 break;
366 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500367
Oliver Neukum07903402008-12-16 10:55:15 +0100368 /* let O_NONBLOCK tasks run */
369 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 schedule();
Oliver Neukum07903402008-12-16 10:55:15 +0100371 if (mutex_lock_interruptible(&list->thread_lock))
372 return -EINTR;
Micon, David48d70552006-05-20 14:59:59 -0700373 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Oliver Neukum07903402008-12-16 10:55:15 +0100375 finish_wait(&list->hiddev->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
Oliver Neukum07903402008-12-16 10:55:15 +0100379 if (retval) {
380 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return retval;
Oliver Neukum07903402008-12-16 10:55:15 +0100382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500385 while (list->head != list->tail &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 retval + event_size <= count) {
387 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100388 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct hiddev_event event;
Oliver Neukum07903402008-12-16 10:55:15 +0100390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 event.hid = list->buffer[list->tail].usage_code;
392 event.value = list->buffer[list->tail].value;
Oliver Neukum07903402008-12-16 10:55:15 +0100393 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
394 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EFAULT;
Oliver Neukum07903402008-12-16 10:55:15 +0100396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 retval += sizeof(struct hiddev_event);
398 }
399 } else {
400 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
401 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100402
403 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
404 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -EFAULT;
Oliver Neukum07903402008-12-16 10:55:15 +0100406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 retval += sizeof(struct hiddev_usage_ref);
408 }
409 }
410 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
411 }
412
413 }
Oliver Neukum07903402008-12-16 10:55:15 +0100414 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return retval;
417}
418
419/*
420 * "poll" file op
421 * No kernel lock - fine
422 */
423static unsigned int hiddev_poll(struct file *file, poll_table *wait)
424{
425 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 poll_wait(file, &list->hiddev->wait, wait);
428 if (list->head != list->tail)
429 return POLLIN | POLLRDNORM;
430 if (!list->hiddev->exist)
431 return POLLERR | POLLHUP;
432 return 0;
433}
434
435/*
436 * "ioctl" file op
437 */
Jean Delvarecf2a2992008-03-03 11:48:43 +0100438static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
439{
440 struct hid_device *hid = hiddev->hid;
441 struct hiddev_report_info rinfo;
442 struct hiddev_usage_ref_multi *uref_multi = NULL;
443 struct hiddev_usage_ref *uref;
444 struct hid_report *report;
445 struct hid_field *field;
446 int i;
447
448 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
449 if (!uref_multi)
450 return -ENOMEM;
451 uref = &uref_multi->uref;
452 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
453 if (copy_from_user(uref_multi, user_arg,
454 sizeof(*uref_multi)))
455 goto fault;
456 } else {
457 if (copy_from_user(uref, user_arg, sizeof(*uref)))
458 goto fault;
459 }
460
461 switch (cmd) {
462 case HIDIOCGUCODE:
463 rinfo.report_type = uref->report_type;
464 rinfo.report_id = uref->report_id;
465 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
466 goto inval;
467
468 if (uref->field_index >= report->maxfield)
469 goto inval;
470
471 field = report->field[uref->field_index];
472 if (uref->usage_index >= field->maxusage)
473 goto inval;
474
475 uref->usage_code = field->usage[uref->usage_index].hid;
476
477 if (copy_to_user(user_arg, uref, sizeof(*uref)))
478 goto fault;
479
Jiri Slabyeb991082008-10-23 01:47:34 +0200480 goto goodreturn;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100481
482 default:
483 if (cmd != HIDIOCGUSAGE &&
484 cmd != HIDIOCGUSAGES &&
485 uref->report_type == HID_REPORT_TYPE_INPUT)
486 goto inval;
487
488 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
489 field = hiddev_lookup_usage(hid, uref);
490 if (field == NULL)
491 goto inval;
492 } else {
493 rinfo.report_type = uref->report_type;
494 rinfo.report_id = uref->report_id;
495 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
496 goto inval;
497
498 if (uref->field_index >= report->maxfield)
499 goto inval;
500
501 field = report->field[uref->field_index];
502
503 if (cmd == HIDIOCGCOLLECTIONINDEX) {
504 if (uref->usage_index >= field->maxusage)
505 goto inval;
506 } else if (uref->usage_index >= field->report_count)
507 goto inval;
508
509 else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
510 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
511 uref->usage_index + uref_multi->num_values > field->report_count))
512 goto inval;
513 }
514
515 switch (cmd) {
516 case HIDIOCGUSAGE:
517 uref->value = field->value[uref->usage_index];
518 if (copy_to_user(user_arg, uref, sizeof(*uref)))
519 goto fault;
520 goto goodreturn;
521
522 case HIDIOCSUSAGE:
523 field->value[uref->usage_index] = uref->value;
524 goto goodreturn;
525
526 case HIDIOCGCOLLECTIONINDEX:
Jiri Slaby48594842009-06-19 23:24:11 +0200527 i = field->usage[uref->usage_index].collection_index;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100528 kfree(uref_multi);
Jiri Slaby48594842009-06-19 23:24:11 +0200529 return i;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100530 case HIDIOCGUSAGES:
531 for (i = 0; i < uref_multi->num_values; i++)
532 uref_multi->values[i] =
533 field->value[uref->usage_index + i];
534 if (copy_to_user(user_arg, uref_multi,
535 sizeof(*uref_multi)))
536 goto fault;
537 goto goodreturn;
538 case HIDIOCSUSAGES:
539 for (i = 0; i < uref_multi->num_values; i++)
540 field->value[uref->usage_index + i] =
541 uref_multi->values[i];
542 goto goodreturn;
543 }
544
545goodreturn:
546 kfree(uref_multi);
547 return 0;
548fault:
549 kfree(uref_multi);
550 return -EFAULT;
551inval:
552 kfree(uref_multi);
553 return -EINVAL;
554 }
555}
556
557static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
558{
559 struct hid_device *hid = hiddev->hid;
560 struct usb_device *dev = hid_to_usb_dev(hid);
561 int idx, len;
562 char *buf;
563
564 if (get_user(idx, (int __user *)user_arg))
565 return -EFAULT;
566
567 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
568 return -ENOMEM;
569
570 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
571 kfree(buf);
572 return -EINVAL;
573 }
574
575 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
576 kfree(buf);
577 return -EFAULT;
578 }
579
580 kfree(buf);
581
582 return len;
583}
584
Alan Cox7961df12008-05-26 11:25:20 +0200585static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct hiddev_list *list = file->private_data;
588 struct hiddev *hiddev = list->hiddev;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300589 struct hid_device *hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct hiddev_collection_info cinfo;
591 struct hiddev_report_info rinfo;
592 struct hiddev_field_info finfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct hiddev_devinfo dinfo;
594 struct hid_report *report;
595 struct hid_field *field;
596 void __user *user_arg = (void __user *)arg;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300597 int i, r = -EINVAL;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300598
Alan Cox7961df12008-05-26 11:25:20 +0200599 /* Called without BKL by compat methods so no BKL taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300601 mutex_lock(&hiddev->existancelock);
602 if (!hiddev->exist) {
603 r = -ENODEV;
604 goto ret_unlock;
605 }
606
607 hid = hiddev->hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 switch (cmd) {
610
611 case HIDIOCGVERSION:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300612 r = put_user(HID_VERSION, (int __user *)arg) ?
613 -EFAULT : 0;
614 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 case HIDIOCAPPLICATION:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300617 if (arg < 0 || arg >= hid->maxapplication)
618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500621 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 HID_COLLECTION_APPLICATION && arg-- == 0)
623 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500624
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300625 if (i < hid->maxcollection)
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300626 r = hid->collection[i].usage;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 case HIDIOCGDEVINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300630 {
631 struct usb_device *dev = hid_to_usb_dev(hid);
632 struct usbhid_device *usbhid = hid->driver_data;
633
634 dinfo.bustype = BUS_USB;
635 dinfo.busnum = dev->bus->busnum;
636 dinfo.devnum = dev->devnum;
637 dinfo.ifnum = usbhid->ifnum;
638 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
639 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
640 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
641 dinfo.num_applications = hid->maxapplication;
642
643 r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
644 -EFAULT : 0;
645 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300646 }
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 case HIDIOCGFLAG:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300649 r = put_user(list->flags, (int __user *)arg) ?
650 -EFAULT : 0;
651 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 case HIDIOCSFLAG:
654 {
655 int newflags;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300656
657 if (get_user(newflags, (int __user *)arg)) {
658 r = -EFAULT;
659 break;
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
663 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
664 (newflags & HIDDEV_FLAG_UREF) == 0))
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300665 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 list->flags = newflags;
668
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300669 r = 0;
670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 case HIDIOCGSTRING:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300674 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
675 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 case HIDIOCINITREPORT:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100678 usbhid_init_reports(hid);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300679 r = 0;
680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 case HIDIOCGREPORT:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300683 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
684 r = -EFAULT;
685 break;
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300689 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300691 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300692 if (report == NULL)
693 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300694
695 usbhid_submit_report(hid, report, USB_DIR_IN);
696 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300698 r = 0;
699 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 case HIDIOCSREPORT:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300702 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
703 r = -EFAULT;
704 break;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300710 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300711 if (report == NULL)
712 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300713
714 usbhid_submit_report(hid, report, USB_DIR_OUT);
715 usbhid_wait_io(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300717 r = 0;
718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 case HIDIOCGREPORTINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300721 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
722 r = -EFAULT;
723 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300724 }
725
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300726 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300727 if (report == NULL)
728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 rinfo.num_fields = report->maxfield;
731
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300732 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
733 -EFAULT : 0;
734 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 case HIDIOCGFIELDINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300737 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
738 r = -EFAULT;
739 break;
740 }
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 rinfo.report_type = finfo.report_type;
743 rinfo.report_id = finfo.report_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300745 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300746 if (report == NULL)
747 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300748
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300749 if (finfo.field_index >= report->maxfield)
750 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 field = report->field[finfo.field_index];
753 memset(&finfo, 0, sizeof(finfo));
754 finfo.report_type = rinfo.report_type;
755 finfo.report_id = rinfo.report_id;
756 finfo.field_index = field->report_count - 1;
757 finfo.maxusage = field->maxusage;
758 finfo.flags = field->flags;
759 finfo.physical = field->physical;
760 finfo.logical = field->logical;
761 finfo.application = field->application;
762 finfo.logical_minimum = field->logical_minimum;
763 finfo.logical_maximum = field->logical_maximum;
764 finfo.physical_minimum = field->physical_minimum;
765 finfo.physical_maximum = field->physical_maximum;
766 finfo.unit_exponent = field->unit_exponent;
767 finfo.unit = field->unit;
768
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300769 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
770 -EFAULT : 0;
771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 case HIDIOCGUCODE:
Jean Delvarecf2a2992008-03-03 11:48:43 +0100774 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 case HIDIOCGUSAGE:
776 case HIDIOCSUSAGE:
777 case HIDIOCGUSAGES:
778 case HIDIOCSUSAGES:
779 case HIDIOCGCOLLECTIONINDEX:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300780 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
781 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 case HIDIOCGCOLLECTIONINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300784 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
785 r = -EFAULT;
786 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300787 }
788
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300789 if (cinfo.index >= hid->maxcollection)
790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 cinfo.type = hid->collection[cinfo.index].type;
793 cinfo.usage = hid->collection[cinfo.index].usage;
794 cinfo.level = hid->collection[cinfo.index].level;
795
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300796 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
797 -EFAULT : 0;
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
805 int len;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300806
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300807 if (!hid->name) {
808 r = 0;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300809 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300810 }
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 len = strlen(hid->name) + 1;
813 if (len > _IOC_SIZE(cmd))
814 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300815 r = copy_to_user(user_arg, hid->name, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 -EFAULT : len;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
820 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
821 int len;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300822
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300823 if (!hid->phys) {
824 r = 0;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300825 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300826 }
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 len = strlen(hid->phys) + 1;
829 if (len > _IOC_SIZE(cmd))
830 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300831 r = copy_to_user(user_arg, hid->phys, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 -EFAULT : len;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 }
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300836
837ret_unlock:
838 mutex_unlock(&hiddev->existancelock);
839 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200842#ifdef CONFIG_COMPAT
843static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
844{
Jiri Kosina88af45b2008-05-27 11:36:40 +0200845 return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200846}
847#endif
848
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300849static const struct file_operations hiddev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .owner = THIS_MODULE,
851 .read = hiddev_read,
852 .write = hiddev_write,
853 .poll = hiddev_poll,
854 .open = hiddev_open,
855 .release = hiddev_release,
Alan Cox7961df12008-05-26 11:25:20 +0200856 .unlocked_ioctl = hiddev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .fasync = hiddev_fasync,
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200858#ifdef CONFIG_COMPAT
859 .compat_ioctl = hiddev_compat_ioctl,
860#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200861 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862};
863
Kay Sieverse454cea2009-09-18 23:01:12 +0200864static char *hiddev_devnode(struct device *dev, mode_t *mode)
Kay Sieversf7a386c2009-04-30 15:23:42 +0200865{
866 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
867}
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700870 .name = "hiddev%d",
Kay Sieverse454cea2009-09-18 23:01:12 +0200871 .devnode = hiddev_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 .fops = &hiddev_fops,
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500873 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874};
875
876/*
877 * This is where hid.c calls us to connect a hid device to the hiddev driver
878 */
Jiri Slaby93c10132008-06-27 00:04:24 +0200879int hiddev_connect(struct hid_device *hid, unsigned int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct hiddev *hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100882 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int retval;
884
Jiri Slaby93c10132008-06-27 00:04:24 +0200885 if (!force) {
886 unsigned int i;
887 for (i = 0; i < hid->maxcollection; i++)
888 if (hid->collection[i].type ==
889 HID_COLLECTION_APPLICATION &&
890 !IS_INPUT_APPLICATION(hid->collection[i].usage))
891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Jiri Slaby93c10132008-06-27 00:04:24 +0200893 if (i == hid->maxcollection)
894 return -1;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100897 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Oliver Neukum07903402008-12-16 10:55:15 +0100900 init_waitqueue_head(&hiddev->wait);
901 INIT_LIST_HEAD(&hiddev->list);
902 spin_lock_init(&hiddev->list_lock);
903 mutex_init(&hiddev->existancelock);
Jiri Kosina76052742009-01-07 13:25:36 +0100904 hid->hiddev = hiddev;
Oliver Neukum07903402008-12-16 10:55:15 +0100905 hiddev->hid = hid;
906 hiddev->exist = 1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100907 retval = usb_register_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800909 hid_err(hid, "Not able to get a minor for this device\n");
Jiri Kosina76052742009-01-07 13:25:36 +0100910 hid->hiddev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 kfree(hiddev);
912 return -1;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return 0;
915}
916
917/*
918 * This is where hid.c calls us to disconnect a hiddev device from the
919 * corresponding hid device (usually because the usb device has disconnected)
920 */
921static struct usb_class_driver hiddev_class;
922void hiddev_disconnect(struct hid_device *hid)
923{
924 struct hiddev *hiddev = hid->hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100925 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Oliver Neukum07903402008-12-16 10:55:15 +0100927 mutex_lock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 hiddev->exist = 0;
Oliver Neukum07903402008-12-16 10:55:15 +0100929 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100931 usb_deregister_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 if (hiddev->open) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100934 usbhid_close(hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 wake_up_interruptible(&hiddev->wait);
936 } else {
937 kfree(hiddev);
938 }
939}