blob: b59b15d4caa9638dd3a601442af4bfe71bf8f736 [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>
Havard Skinnemoend4f0e4d2012-04-26 11:16:00 -070037#include <linux/vmalloc.h>
Gustavo A. R. Silva82e360c2018-06-29 17:08:44 -050038#include <linux/nospec.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010039#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#ifdef CONFIG_USB_DYNAMIC_MINORS
42#define HIDDEV_MINOR_BASE 0
43#define HIDDEV_MINORS 256
44#else
45#define HIDDEV_MINOR_BASE 96
46#define HIDDEV_MINORS 16
47#endif
Jiri Kosinaaffbb8c2009-08-20 12:04:14 +020048#define HIDDEV_BUFFER_SIZE 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50struct hiddev {
51 int exist;
52 int open;
Oliver Neukum07903402008-12-16 10:55:15 +010053 struct mutex existancelock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 wait_queue_head_t wait;
55 struct hid_device *hid;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040056 struct list_head list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +020057 spinlock_t list_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
60struct hiddev_list {
61 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
62 int head;
63 int tail;
64 unsigned flags;
65 struct fasync_struct *fasync;
66 struct hiddev *hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040067 struct list_head node;
Oliver Neukum07903402008-12-16 10:55:15 +010068 struct mutex thread_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * Find a report, given the report's type and ID. The ID can be specified
73 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
74 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
75 * given type which follows old_id.
76 */
77static struct hid_report *
78hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
79{
Dmitry Torokhov826d5982006-07-19 01:09:10 -040080 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
81 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct hid_report_enum *report_enum;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040083 struct hid_report *report;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct list_head *list;
85
86 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -040087 rinfo->report_type > HID_REPORT_TYPE_MAX)
88 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 report_enum = hid->report_enum +
91 (rinfo->report_type - HID_REPORT_TYPE_MIN);
92
93 switch (flags) {
94 case 0: /* Nothing to do -- report_id is already set correctly */
95 break;
96
97 case HID_REPORT_ID_FIRST:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040098 if (list_empty(&report_enum->report_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400100
101 list = report_enum->report_list.next;
102 report = list_entry(list, struct hid_report, list);
103 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 case HID_REPORT_ID_NEXT:
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400107 report = report_enum->report_id_hash[rid];
108 if (!report)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400110
111 list = report->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (list == &report_enum->report_list)
113 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400114
115 report = list_entry(list, struct hid_report, list);
116 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 default:
120 return NULL;
121 }
122
123 return report_enum->report_id_hash[rinfo->report_id];
124}
125
126/*
127 * Perform an exhaustive search of the report table for a usage, given its
128 * type and usage id.
129 */
130static struct hid_field *
131hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
132{
133 int i, j;
134 struct hid_report *report;
135 struct hid_report_enum *report_enum;
136 struct hid_field *field;
137
138 if (uref->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400139 uref->report_type > HID_REPORT_TYPE_MAX)
140 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 report_enum = hid->report_enum +
143 (uref->report_type - HID_REPORT_TYPE_MIN);
144
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400145 list_for_each_entry(report, &report_enum->report_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 for (i = 0; i < report->maxfield; i++) {
147 field = report->field[i];
148 for (j = 0; j < field->maxusage; j++) {
149 if (field->usage[j].hid == uref->usage_code) {
150 uref->report_id = report->id;
151 uref->field_index = i;
152 uref->usage_index = j;
153 return field;
154 }
155 }
156 }
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return NULL;
160}
161
162static void hiddev_send_event(struct hid_device *hid,
163 struct hiddev_usage_ref *uref)
164{
165 struct hiddev *hiddev = hid->hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400166 struct hiddev_list *list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200167 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200169 spin_lock_irqsave(&hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400170 list_for_each_entry(list, &hiddev->list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (uref->field_index != HID_FIELD_INDEX_NONE ||
172 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
173 list->buffer[list->head] = *uref;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500174 list->head = (list->head + 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 (HIDDEV_BUFFER_SIZE - 1);
176 kill_fasync(&list->fasync, SIGIO, POLL_IN);
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200179 spin_unlock_irqrestore(&hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 wake_up_interruptible(&hiddev->wait);
182}
183
184/*
185 * This is where hid.c calls into hiddev to pass an event that occurred over
186 * the interrupt pipe
187 */
188void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
David Howells7d12e782006-10-05 14:55:46 +0100189 struct hid_usage *usage, __s32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 unsigned type = field->report_type;
192 struct hiddev_usage_ref uref;
193
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500194 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500196 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400197 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 uref.report_id = field->report->id;
199 uref.field_index = field->index;
200 uref.usage_index = (usage - field->usage);
201 uref.usage_code = usage->hid;
202 uref.value = value;
203
204 hiddev_send_event(hid, &uref);
205}
Jiri Kosina229695e2006-12-08 18:40:53 +0100206EXPORT_SYMBOL_GPL(hiddev_hid_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
209{
210 unsigned type = report->type;
211 struct hiddev_usage_ref uref;
212
213 memset(&uref, 0, sizeof(uref));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500214 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500216 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400217 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 uref.report_id = report->id;
219 uref.field_index = HID_FIELD_INDEX_NONE;
220
221 hiddev_send_event(hid, &uref);
222}
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * fasync file op
226 */
227static int hiddev_fasync(int fd, struct file *file, int on)
228{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400230
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700231 return fasync_helper(fd, file, on, &list->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234
235/*
236 * release file op
237 */
238static int hiddev_release(struct inode * inode, struct file * file)
239{
240 struct hiddev_list *list = file->private_data;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200241 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200243 spin_lock_irqsave(&list->hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400244 list_del(&list->node);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200245 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200247 mutex_lock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!--list->hiddev->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100249 if (list->hiddev->exist) {
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100250 usbhid_close(list->hiddev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100251 usbhid_put_power(list->hiddev->hid);
252 } else {
Dan Carpenter5c699d72011-05-26 11:49:16 +0300253 mutex_unlock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 kfree(list->hiddev);
Havard Skinnemoend4f0e4d2012-04-26 11:16:00 -0700255 vfree(list);
Dan Carpenter5c699d72011-05-26 11:49:16 +0300256 return 0;
Oliver Neukum0361a282008-12-17 15:38:03 +0100257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200260 mutex_unlock(&list->hiddev->existancelock);
Havard Skinnemoend4f0e4d2012-04-26 11:16:00 -0700261 vfree(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return 0;
264}
265
266/*
267 * open file op
268 */
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400269static int hiddev_open(struct inode *inode, struct file *file)
270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct hiddev_list *list;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200272 struct usb_interface *intf;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200273 struct hid_device *hid;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200274 struct hiddev *hiddev;
275 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +0200277 intf = usbhid_find_interface(iminor(inode));
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200278 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -ENODEV;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200280 hid = usb_get_intfdata(intf);
281 hiddev = hid->hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Havard Skinnemoend4f0e4d2012-04-26 11:16:00 -0700283 if (!(list = vzalloc(sizeof(struct hiddev_list))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -ENOMEM;
Oliver Neukum07903402008-12-16 10:55:15 +0100285 mutex_init(&list->thread_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200286 list->hiddev = hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 file->private_data = list;
288
Oliver Neukum07903402008-12-16 10:55:15 +0100289 /*
290 * no need for locking because the USB major number
291 * is shared which usbcore guards against disconnect
292 */
293 if (list->hiddev->exist) {
294 if (!list->hiddev->open++) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200295 res = usbhid_open(hiddev->hid);
Oliver Neukum07903402008-12-16 10:55:15 +0100296 if (res < 0) {
297 res = -EIO;
298 goto bail;
299 }
300 }
301 } else {
302 res = -ENODEV;
303 goto bail;
304 }
305
306 spin_lock_irq(&list->hiddev->list_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200307 list_add_tail(&list->node, &hiddev->list);
Oliver Neukum07903402008-12-16 10:55:15 +0100308 spin_unlock_irq(&list->hiddev->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200310 mutex_lock(&hiddev->existancelock);
Oliver Neukum0361a282008-12-17 15:38:03 +0100311 if (!list->hiddev->open++)
312 if (list->hiddev->exist) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200313 struct hid_device *hid = hiddev->hid;
Oliver Neukum0361a282008-12-17 15:38:03 +0100314 res = usbhid_get_power(hid);
315 if (res < 0) {
316 res = -EIO;
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200317 goto bail_unlock;
Oliver Neukum0361a282008-12-17 15:38:03 +0100318 }
319 usbhid_open(hid);
320 }
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200321 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200323bail_unlock:
324 mutex_unlock(&hiddev->existancelock);
Oliver Neukum07903402008-12-16 10:55:15 +0100325bail:
326 file->private_data = NULL;
Havard Skinnemoend4f0e4d2012-04-26 11:16:00 -0700327 vfree(list);
Oliver Neukum07903402008-12-16 10:55:15 +0100328 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/*
332 * "write" file op
333 */
334static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
335{
336 return -EINVAL;
337}
338
339/*
340 * "read" file op
341 */
342static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
343{
Johannes Weiner96fe2ab2009-03-10 22:44:01 +0100344 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct hiddev_list *list = file->private_data;
346 int event_size;
Oliver Neukum07903402008-12-16 10:55:15 +0100347 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
350 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
351
352 if (count < event_size)
353 return 0;
354
Oliver Neukum07903402008-12-16 10:55:15 +0100355 /* lock against other threads */
356 retval = mutex_lock_interruptible(&list->thread_lock);
357 if (retval)
358 return -ERESTARTSYS;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 while (retval == 0) {
361 if (list->head == list->tail) {
Oliver Neukum07903402008-12-16 10:55:15 +0100362 prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 while (list->head == list->tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (signal_pending(current)) {
366 retval = -ERESTARTSYS;
367 break;
368 }
369 if (!list->hiddev->exist) {
370 retval = -EIO;
371 break;
372 }
Jiri Kosina13f19622012-11-28 00:10:44 +0100373 if (file->f_flags & O_NONBLOCK) {
374 retval = -EAGAIN;
375 break;
376 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500377
Oliver Neukum07903402008-12-16 10:55:15 +0100378 /* let O_NONBLOCK tasks run */
379 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 schedule();
Peter Waechtler06268b22011-04-28 20:53:58 +0200381 if (mutex_lock_interruptible(&list->thread_lock)) {
382 finish_wait(&list->hiddev->wait, &wait);
Oliver Neukum07903402008-12-16 10:55:15 +0100383 return -EINTR;
Peter Waechtler06268b22011-04-28 20:53:58 +0200384 }
Micon, David48d70552006-05-20 14:59:59 -0700385 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Oliver Neukum07903402008-12-16 10:55:15 +0100387 finish_wait(&list->hiddev->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Oliver Neukum07903402008-12-16 10:55:15 +0100391 if (retval) {
392 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return retval;
Oliver Neukum07903402008-12-16 10:55:15 +0100394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500397 while (list->head != list->tail &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 retval + event_size <= count) {
399 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100400 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct hiddev_event event;
Oliver Neukum07903402008-12-16 10:55:15 +0100402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 event.hid = list->buffer[list->tail].usage_code;
404 event.value = list->buffer[list->tail].value;
Oliver Neukum07903402008-12-16 10:55:15 +0100405 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
406 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -EFAULT;
Oliver Neukum07903402008-12-16 10:55:15 +0100408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 retval += sizeof(struct hiddev_event);
410 }
411 } else {
412 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
413 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100414
415 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
416 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EFAULT;
Oliver Neukum07903402008-12-16 10:55:15 +0100418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 retval += sizeof(struct hiddev_usage_ref);
420 }
421 }
422 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
423 }
424
425 }
Oliver Neukum07903402008-12-16 10:55:15 +0100426 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 return retval;
429}
430
431/*
432 * "poll" file op
433 * No kernel lock - fine
434 */
435static unsigned int hiddev_poll(struct file *file, poll_table *wait)
436{
437 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 poll_wait(file, &list->hiddev->wait, wait);
440 if (list->head != list->tail)
441 return POLLIN | POLLRDNORM;
442 if (!list->hiddev->exist)
443 return POLLERR | POLLHUP;
444 return 0;
445}
446
447/*
448 * "ioctl" file op
449 */
Jean Delvarecf2a2992008-03-03 11:48:43 +0100450static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
451{
452 struct hid_device *hid = hiddev->hid;
453 struct hiddev_report_info rinfo;
454 struct hiddev_usage_ref_multi *uref_multi = NULL;
455 struct hiddev_usage_ref *uref;
456 struct hid_report *report;
457 struct hid_field *field;
458 int i;
459
460 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
461 if (!uref_multi)
462 return -ENOMEM;
463 uref = &uref_multi->uref;
464 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
465 if (copy_from_user(uref_multi, user_arg,
466 sizeof(*uref_multi)))
467 goto fault;
468 } else {
469 if (copy_from_user(uref, user_arg, sizeof(*uref)))
470 goto fault;
471 }
472
473 switch (cmd) {
474 case HIDIOCGUCODE:
475 rinfo.report_type = uref->report_type;
476 rinfo.report_id = uref->report_id;
477 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
478 goto inval;
479
480 if (uref->field_index >= report->maxfield)
481 goto inval;
Gustavo A. R. Silva82e360c2018-06-29 17:08:44 -0500482 uref->field_index = array_index_nospec(uref->field_index,
483 report->maxfield);
Jean Delvarecf2a2992008-03-03 11:48:43 +0100484
485 field = report->field[uref->field_index];
486 if (uref->usage_index >= field->maxusage)
487 goto inval;
Gustavo A. R. Silva82e360c2018-06-29 17:08:44 -0500488 uref->usage_index = array_index_nospec(uref->usage_index,
489 field->maxusage);
Jean Delvarecf2a2992008-03-03 11:48:43 +0100490
491 uref->usage_code = field->usage[uref->usage_index].hid;
492
493 if (copy_to_user(user_arg, uref, sizeof(*uref)))
494 goto fault;
495
Jiri Slabyeb991082008-10-23 01:47:34 +0200496 goto goodreturn;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100497
498 default:
499 if (cmd != HIDIOCGUSAGE &&
500 cmd != HIDIOCGUSAGES &&
501 uref->report_type == HID_REPORT_TYPE_INPUT)
502 goto inval;
503
504 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
505 field = hiddev_lookup_usage(hid, uref);
506 if (field == NULL)
507 goto inval;
508 } else {
509 rinfo.report_type = uref->report_type;
510 rinfo.report_id = uref->report_id;
511 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
512 goto inval;
513
514 if (uref->field_index >= report->maxfield)
515 goto inval;
Gustavo A. R. Silva82e360c2018-06-29 17:08:44 -0500516 uref->field_index = array_index_nospec(uref->field_index,
517 report->maxfield);
Jean Delvarecf2a2992008-03-03 11:48:43 +0100518
519 field = report->field[uref->field_index];
520
521 if (cmd == HIDIOCGCOLLECTIONINDEX) {
522 if (uref->usage_index >= field->maxusage)
523 goto inval;
524 } else if (uref->usage_index >= field->report_count)
525 goto inval;
Dan Carpenterac065bf2011-03-26 04:47:35 +0300526 }
Jean Delvarecf2a2992008-03-03 11:48:43 +0100527
Scott Bauer93a20012016-06-23 08:59:47 -0600528 if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
529 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
530 uref->usage_index + uref_multi->num_values > field->report_count))
531 goto inval;
532
Jean Delvarecf2a2992008-03-03 11:48:43 +0100533 switch (cmd) {
534 case HIDIOCGUSAGE:
535 uref->value = field->value[uref->usage_index];
536 if (copy_to_user(user_arg, uref, sizeof(*uref)))
537 goto fault;
538 goto goodreturn;
539
540 case HIDIOCSUSAGE:
541 field->value[uref->usage_index] = uref->value;
542 goto goodreturn;
543
544 case HIDIOCGCOLLECTIONINDEX:
Jiri Slaby48594842009-06-19 23:24:11 +0200545 i = field->usage[uref->usage_index].collection_index;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100546 kfree(uref_multi);
Jiri Slaby48594842009-06-19 23:24:11 +0200547 return i;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100548 case HIDIOCGUSAGES:
549 for (i = 0; i < uref_multi->num_values; i++)
550 uref_multi->values[i] =
551 field->value[uref->usage_index + i];
552 if (copy_to_user(user_arg, uref_multi,
553 sizeof(*uref_multi)))
554 goto fault;
555 goto goodreturn;
556 case HIDIOCSUSAGES:
557 for (i = 0; i < uref_multi->num_values; i++)
558 field->value[uref->usage_index + i] =
559 uref_multi->values[i];
560 goto goodreturn;
561 }
562
563goodreturn:
564 kfree(uref_multi);
565 return 0;
566fault:
567 kfree(uref_multi);
568 return -EFAULT;
569inval:
570 kfree(uref_multi);
571 return -EINVAL;
572 }
573}
574
575static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
576{
577 struct hid_device *hid = hiddev->hid;
578 struct usb_device *dev = hid_to_usb_dev(hid);
579 int idx, len;
580 char *buf;
581
582 if (get_user(idx, (int __user *)user_arg))
583 return -EFAULT;
584
585 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
586 return -ENOMEM;
587
588 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
589 kfree(buf);
590 return -EINVAL;
591 }
592
593 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
594 kfree(buf);
595 return -EFAULT;
596 }
597
598 kfree(buf);
599
600 return len;
601}
602
Alan Cox7961df12008-05-26 11:25:20 +0200603static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct hiddev_list *list = file->private_data;
606 struct hiddev *hiddev = list->hiddev;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300607 struct hid_device *hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct hiddev_collection_info cinfo;
609 struct hiddev_report_info rinfo;
610 struct hiddev_field_info finfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct hiddev_devinfo dinfo;
612 struct hid_report *report;
613 struct hid_field *field;
614 void __user *user_arg = (void __user *)arg;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300615 int i, r = -EINVAL;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300616
Alan Cox7961df12008-05-26 11:25:20 +0200617 /* Called without BKL by compat methods so no BKL taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300619 mutex_lock(&hiddev->existancelock);
620 if (!hiddev->exist) {
621 r = -ENODEV;
622 goto ret_unlock;
623 }
624
625 hid = hiddev->hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 switch (cmd) {
628
629 case HIDIOCGVERSION:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300630 r = put_user(HID_VERSION, (int __user *)arg) ?
631 -EFAULT : 0;
632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 case HIDIOCAPPLICATION:
Tushar Beherad339f612012-11-16 12:20:43 +0530635 if (arg >= hid->maxapplication)
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500639 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 HID_COLLECTION_APPLICATION && arg-- == 0)
641 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500642
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300643 if (i < hid->maxcollection)
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300644 r = hid->collection[i].usage;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 case HIDIOCGDEVINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300648 {
649 struct usb_device *dev = hid_to_usb_dev(hid);
650 struct usbhid_device *usbhid = hid->driver_data;
651
Dan Carpenter9561f7f2011-09-23 09:21:13 +0300652 memset(&dinfo, 0, sizeof(dinfo));
653
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300654 dinfo.bustype = BUS_USB;
655 dinfo.busnum = dev->bus->busnum;
656 dinfo.devnum = dev->devnum;
657 dinfo.ifnum = usbhid->ifnum;
658 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
659 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
660 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
661 dinfo.num_applications = hid->maxapplication;
662
663 r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
664 -EFAULT : 0;
665 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300666 }
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 case HIDIOCGFLAG:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300669 r = put_user(list->flags, (int __user *)arg) ?
670 -EFAULT : 0;
671 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 case HIDIOCSFLAG:
674 {
675 int newflags;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300676
677 if (get_user(newflags, (int __user *)arg)) {
678 r = -EFAULT;
679 break;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
683 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
684 (newflags & HIDDEV_FLAG_UREF) == 0))
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 list->flags = newflags;
688
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300689 r = 0;
690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 case HIDIOCGSTRING:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300694 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
695 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 case HIDIOCINITREPORT:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100698 usbhid_init_reports(hid);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300699 r = 0;
700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 case HIDIOCGREPORT:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300703 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
704 r = -EFAULT;
705 break;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300709 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300711 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300712 if (report == NULL)
713 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300714
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100715 hid_hw_request(hid, report, HID_REQ_GET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100716 hid_hw_wait(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300718 r = 0;
719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 case HIDIOCSREPORT:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300722 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
723 r = -EFAULT;
724 break;
725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300730 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300731 if (report == NULL)
732 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300733
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100734 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100735 hid_hw_wait(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300737 r = 0;
738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 case HIDIOCGREPORTINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300741 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
742 r = -EFAULT;
743 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300744 }
745
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300746 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300747 if (report == NULL)
748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 rinfo.num_fields = report->maxfield;
751
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300752 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
753 -EFAULT : 0;
754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 case HIDIOCGFIELDINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300757 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
758 r = -EFAULT;
759 break;
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 rinfo.report_type = finfo.report_type;
763 rinfo.report_id = finfo.report_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300765 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300766 if (report == NULL)
767 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300768
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300769 if (finfo.field_index >= report->maxfield)
770 break;
Gustavo A. R. Silva82e360c2018-06-29 17:08:44 -0500771 finfo.field_index = array_index_nospec(finfo.field_index,
772 report->maxfield);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 field = report->field[finfo.field_index];
775 memset(&finfo, 0, sizeof(finfo));
776 finfo.report_type = rinfo.report_type;
777 finfo.report_id = rinfo.report_id;
778 finfo.field_index = field->report_count - 1;
779 finfo.maxusage = field->maxusage;
780 finfo.flags = field->flags;
781 finfo.physical = field->physical;
782 finfo.logical = field->logical;
783 finfo.application = field->application;
784 finfo.logical_minimum = field->logical_minimum;
785 finfo.logical_maximum = field->logical_maximum;
786 finfo.physical_minimum = field->physical_minimum;
787 finfo.physical_maximum = field->physical_maximum;
788 finfo.unit_exponent = field->unit_exponent;
789 finfo.unit = field->unit;
790
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300791 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
792 -EFAULT : 0;
793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 case HIDIOCGUCODE:
Jean Delvarecf2a2992008-03-03 11:48:43 +0100796 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case HIDIOCGUSAGE:
798 case HIDIOCSUSAGE:
799 case HIDIOCGUSAGES:
800 case HIDIOCSUSAGES:
801 case HIDIOCGCOLLECTIONINDEX:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300802 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 case HIDIOCGCOLLECTIONINFO:
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300806 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
807 r = -EFAULT;
808 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300809 }
810
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300811 if (cinfo.index >= hid->maxcollection)
812 break;
Gustavo A. R. Silva82e360c2018-06-29 17:08:44 -0500813 cinfo.index = array_index_nospec(cinfo.index,
814 hid->maxcollection);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 cinfo.type = hid->collection[cinfo.index].type;
817 cinfo.usage = hid->collection[cinfo.index].usage;
818 cinfo.level = hid->collection[cinfo.index].level;
819
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300820 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
821 -EFAULT : 0;
822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300826 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200829 int len = strlen(hid->name) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (len > _IOC_SIZE(cmd))
831 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300832 r = copy_to_user(user_arg, hid->name, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 -EFAULT : len;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
837 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200838 int len = strlen(hid->phys) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (len > _IOC_SIZE(cmd))
840 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300841 r = copy_to_user(user_arg, hid->phys, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 -EFAULT : len;
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 }
Valentine Barshak33d6eb572010-12-06 18:16:11 +0300846
847ret_unlock:
848 mutex_unlock(&hiddev->existancelock);
849 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200852#ifdef CONFIG_COMPAT
853static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
854{
Jiri Kosina88af45b2008-05-27 11:36:40 +0200855 return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200856}
857#endif
858
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300859static const struct file_operations hiddev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .owner = THIS_MODULE,
861 .read = hiddev_read,
862 .write = hiddev_write,
863 .poll = hiddev_poll,
864 .open = hiddev_open,
865 .release = hiddev_release,
Alan Cox7961df12008-05-26 11:25:20 +0200866 .unlocked_ioctl = hiddev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .fasync = hiddev_fasync,
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200868#ifdef CONFIG_COMPAT
869 .compat_ioctl = hiddev_compat_ioctl,
870#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200871 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872};
873
Al Viro2c9ede52011-07-23 20:24:48 -0400874static char *hiddev_devnode(struct device *dev, umode_t *mode)
Kay Sieversf7a386c2009-04-30 15:23:42 +0200875{
876 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700880 .name = "hiddev%d",
Kay Sieverse454cea2009-09-18 23:01:12 +0200881 .devnode = hiddev_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 .fops = &hiddev_fops,
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500883 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884};
885
886/*
887 * This is where hid.c calls us to connect a hid device to the hiddev driver
888 */
Jiri Slaby93c10132008-06-27 00:04:24 +0200889int hiddev_connect(struct hid_device *hid, unsigned int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 struct hiddev *hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100892 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int retval;
894
Jiri Slaby93c10132008-06-27 00:04:24 +0200895 if (!force) {
896 unsigned int i;
897 for (i = 0; i < hid->maxcollection; i++)
898 if (hid->collection[i].type ==
899 HID_COLLECTION_APPLICATION &&
900 !IS_INPUT_APPLICATION(hid->collection[i].usage))
901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Jiri Slaby93c10132008-06-27 00:04:24 +0200903 if (i == hid->maxcollection)
904 return -1;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100907 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Oliver Neukum07903402008-12-16 10:55:15 +0100910 init_waitqueue_head(&hiddev->wait);
911 INIT_LIST_HEAD(&hiddev->list);
912 spin_lock_init(&hiddev->list_lock);
913 mutex_init(&hiddev->existancelock);
Jiri Kosina76052742009-01-07 13:25:36 +0100914 hid->hiddev = hiddev;
Oliver Neukum07903402008-12-16 10:55:15 +0100915 hiddev->hid = hid;
916 hiddev->exist = 1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100917 retval = usb_register_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800919 hid_err(hid, "Not able to get a minor for this device\n");
Jiri Kosina76052742009-01-07 13:25:36 +0100920 hid->hiddev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 kfree(hiddev);
922 return -1;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
926
927/*
928 * This is where hid.c calls us to disconnect a hiddev device from the
929 * corresponding hid device (usually because the usb device has disconnected)
930 */
931static struct usb_class_driver hiddev_class;
932void hiddev_disconnect(struct hid_device *hid)
933{
934 struct hiddev *hiddev = hid->hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100935 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Ming Leiba183112012-01-12 17:42:22 +0800937 usb_deregister_dev(usbhid->intf, &hiddev_class);
938
Oliver Neukum07903402008-12-16 10:55:15 +0100939 mutex_lock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 hiddev->exist = 0;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (hiddev->open) {
Jiri Kosina7f778972011-05-24 11:43:18 +0200943 mutex_unlock(&hiddev->existancelock);
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100944 usbhid_close(hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 wake_up_interruptible(&hiddev->wait);
946 } else {
Jiri Kosina7f778972011-05-24 11:43:18 +0200947 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 kfree(hiddev);
949 }
950}