blob: e421cdf2d1a4ec5554535e8b241592d6c8f3afa7 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (c) 2001 Paul Stewart
4 * Copyright (c) 2001 Vojtech Pavlik
5 *
6 * HID char devices, giving access to raw HID device events.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Should you need to contact me, the author, you can do so either by
12 * e-mail - mail your message to Paul Stewart <stewart@wetlogic.net>
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/poll.h>
16#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/input.h>
21#include <linux/usb.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010022#include <linux/hid.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/hiddev.h>
Philip Langdalebb6c8d82007-10-14 12:03:58 +020024#include <linux/compat.h>
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -070025#include <linux/vmalloc.h>
Gustavo A. R. Silva4f652452018-06-29 17:08:44 -050026#include <linux/nospec.h>
Jiri Kosinadde58452006-12-08 18:40:44 +010027#include "usbhid.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#ifdef CONFIG_USB_DYNAMIC_MINORS
30#define HIDDEV_MINOR_BASE 0
31#define HIDDEV_MINORS 256
32#else
33#define HIDDEV_MINOR_BASE 96
34#define HIDDEV_MINORS 16
35#endif
Jiri Kosinaaffbb8c2009-08-20 12:04:14 +020036#define HIDDEV_BUFFER_SIZE 2048
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct hiddev_list {
39 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
40 int head;
41 int tail;
42 unsigned flags;
43 struct fasync_struct *fasync;
44 struct hiddev *hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040045 struct list_head node;
Oliver Neukum07903402008-12-16 10:55:15 +010046 struct mutex thread_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Find a report, given the report's type and ID. The ID can be specified
51 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
52 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
53 * given type which follows old_id.
54 */
55static struct hid_report *
56hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
57{
Dmitry Torokhov826d5982006-07-19 01:09:10 -040058 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
59 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct hid_report_enum *report_enum;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040061 struct hid_report *report;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct list_head *list;
63
64 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -040065 rinfo->report_type > HID_REPORT_TYPE_MAX)
66 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 report_enum = hid->report_enum +
69 (rinfo->report_type - HID_REPORT_TYPE_MIN);
70
71 switch (flags) {
72 case 0: /* Nothing to do -- report_id is already set correctly */
73 break;
74
75 case HID_REPORT_ID_FIRST:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040076 if (list_empty(&report_enum->report_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040078
79 list = report_enum->report_list.next;
80 report = list_entry(list, struct hid_report, list);
81 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 case HID_REPORT_ID_NEXT:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040085 report = report_enum->report_id_hash[rid];
86 if (!report)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040088
89 list = report->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (list == &report_enum->report_list)
91 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040092
93 report = list_entry(list, struct hid_report, list);
94 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 default:
98 return NULL;
99 }
100
101 return report_enum->report_id_hash[rinfo->report_id];
102}
103
104/*
105 * Perform an exhaustive search of the report table for a usage, given its
106 * type and usage id.
107 */
108static struct hid_field *
109hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
110{
111 int i, j;
112 struct hid_report *report;
113 struct hid_report_enum *report_enum;
114 struct hid_field *field;
115
116 if (uref->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400117 uref->report_type > HID_REPORT_TYPE_MAX)
118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 report_enum = hid->report_enum +
121 (uref->report_type - HID_REPORT_TYPE_MIN);
122
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400123 list_for_each_entry(report, &report_enum->report_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 for (i = 0; i < report->maxfield; i++) {
125 field = report->field[i];
126 for (j = 0; j < field->maxusage; j++) {
127 if (field->usage[j].hid == uref->usage_code) {
128 uref->report_id = report->id;
129 uref->field_index = i;
130 uref->usage_index = j;
131 return field;
132 }
133 }
134 }
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 return NULL;
138}
139
140static void hiddev_send_event(struct hid_device *hid,
141 struct hiddev_usage_ref *uref)
142{
143 struct hiddev *hiddev = hid->hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400144 struct hiddev_list *list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200145 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200147 spin_lock_irqsave(&hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400148 list_for_each_entry(list, &hiddev->list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (uref->field_index != HID_FIELD_INDEX_NONE ||
150 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
151 list->buffer[list->head] = *uref;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500152 list->head = (list->head + 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 (HIDDEV_BUFFER_SIZE - 1);
154 kill_fasync(&list->fasync, SIGIO, POLL_IN);
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200157 spin_unlock_irqrestore(&hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 wake_up_interruptible(&hiddev->wait);
160}
161
162/*
163 * This is where hid.c calls into hiddev to pass an event that occurred over
164 * the interrupt pipe
165 */
166void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
David Howells7d12e782006-10-05 14:55:46 +0100167 struct hid_usage *usage, __s32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 unsigned type = field->report_type;
170 struct hiddev_usage_ref uref;
171
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500172 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500174 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400175 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 uref.report_id = field->report->id;
177 uref.field_index = field->index;
178 uref.usage_index = (usage - field->usage);
179 uref.usage_code = usage->hid;
180 uref.value = value;
181
182 hiddev_send_event(hid, &uref);
183}
Jiri Kosina229695e2006-12-08 18:40:53 +0100184EXPORT_SYMBOL_GPL(hiddev_hid_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
187{
188 unsigned type = report->type;
189 struct hiddev_usage_ref uref;
190
191 memset(&uref, 0, sizeof(uref));
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 = report->id;
197 uref.field_index = HID_FIELD_INDEX_NONE;
198
199 hiddev_send_event(hid, &uref);
200}
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
203 * fasync file op
204 */
205static int hiddev_fasync(int fd, struct file *file, int on)
206{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400208
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700209 return fasync_helper(fd, file, on, &list->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212
213/*
214 * release file op
215 */
216static int hiddev_release(struct inode * inode, struct file * file)
217{
218 struct hiddev_list *list = file->private_data;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200219 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200221 spin_lock_irqsave(&list->hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400222 list_del(&list->node);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200223 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200225 mutex_lock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!--list->hiddev->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100227 if (list->hiddev->exist) {
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700228 hid_hw_close(list->hiddev->hid);
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700229 hid_hw_power(list->hiddev->hid, PM_HINT_NORMAL);
Oliver Neukum0361a282008-12-17 15:38:03 +0100230 } else {
Dan Carpenter5c699d72011-05-26 11:49:16 +0300231 mutex_unlock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 kfree(list->hiddev);
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -0700233 vfree(list);
Dan Carpenter5c699d72011-05-26 11:49:16 +0300234 return 0;
Oliver Neukum0361a282008-12-17 15:38:03 +0100235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200238 mutex_unlock(&list->hiddev->existancelock);
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -0700239 vfree(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return 0;
242}
243
244/*
245 * open file op
246 */
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400247static int hiddev_open(struct inode *inode, struct file *file)
248{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct hiddev_list *list;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200250 struct usb_interface *intf;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200251 struct hid_device *hid;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200252 struct hiddev *hiddev;
253 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +0200255 intf = usbhid_find_interface(iminor(inode));
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200256 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -ENODEV;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200258 hid = usb_get_intfdata(intf);
259 hiddev = hid->hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -0700261 if (!(list = vzalloc(sizeof(struct hiddev_list))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -ENOMEM;
Oliver Neukum07903402008-12-16 10:55:15 +0100263 mutex_init(&list->thread_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200264 list->hiddev = hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 file->private_data = list;
266
Oliver Neukum07903402008-12-16 10:55:15 +0100267 /*
268 * no need for locking because the USB major number
269 * is shared which usbcore guards against disconnect
270 */
271 if (list->hiddev->exist) {
272 if (!list->hiddev->open++) {
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700273 res = hid_hw_open(hiddev->hid);
274 if (res < 0)
Oliver Neukum07903402008-12-16 10:55:15 +0100275 goto bail;
Oliver Neukum07903402008-12-16 10:55:15 +0100276 }
277 } else {
278 res = -ENODEV;
279 goto bail;
280 }
281
282 spin_lock_irq(&list->hiddev->list_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200283 list_add_tail(&list->node, &hiddev->list);
Oliver Neukum07903402008-12-16 10:55:15 +0100284 spin_unlock_irq(&list->hiddev->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200286 mutex_lock(&hiddev->existancelock);
Hillf Danton9c09b212019-08-06 16:38:58 +0800287 /*
288 * recheck exist with existance lock held to
289 * avoid opening a disconnected device
290 */
291 if (!list->hiddev->exist) {
292 res = -ENODEV;
293 goto bail_unlock;
294 }
Oliver Neukum0361a282008-12-17 15:38:03 +0100295 if (!list->hiddev->open++)
296 if (list->hiddev->exist) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200297 struct hid_device *hid = hiddev->hid;
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700298 res = hid_hw_power(hid, PM_HINT_FULLON);
299 if (res < 0)
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200300 goto bail_unlock;
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700301 res = hid_hw_open(hid);
302 if (res < 0)
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700303 goto bail_normal_power;
Oliver Neukum0361a282008-12-17 15:38:03 +0100304 }
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200305 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700307bail_normal_power:
308 hid_hw_power(hid, PM_HINT_NORMAL);
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200309bail_unlock:
310 mutex_unlock(&hiddev->existancelock);
Hillf Danton6d4472d2019-08-06 16:40:15 +0800311
312 spin_lock_irq(&list->hiddev->list_lock);
313 list_del(&list->node);
314 spin_unlock_irq(&list->hiddev->list_lock);
Oliver Neukum07903402008-12-16 10:55:15 +0100315bail:
316 file->private_data = NULL;
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -0700317 vfree(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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (signal_pending(current)) {
356 retval = -ERESTARTSYS;
357 break;
358 }
359 if (!list->hiddev->exist) {
360 retval = -EIO;
361 break;
362 }
Jiri Kosina13f19622012-11-28 00:10:44 +0100363 if (file->f_flags & O_NONBLOCK) {
364 retval = -EAGAIN;
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();
Peter Waechtler06268b22011-04-28 20:53:58 +0200371 if (mutex_lock_interruptible(&list->thread_lock)) {
372 finish_wait(&list->hiddev->wait, &wait);
Oliver Neukum07903402008-12-16 10:55:15 +0100373 return -EINTR;
Peter Waechtler06268b22011-04-28 20:53:58 +0200374 }
Micon, David48d70552006-05-20 14:59:59 -0700375 set_current_state(TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Oliver Neukum07903402008-12-16 10:55:15 +0100377 finish_wait(&list->hiddev->wait, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Oliver Neukum07903402008-12-16 10:55:15 +0100381 if (retval) {
382 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return retval;
Oliver Neukum07903402008-12-16 10:55:15 +0100384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500387 while (list->head != list->tail &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 retval + event_size <= count) {
389 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100390 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct hiddev_event event;
Oliver Neukum07903402008-12-16 10:55:15 +0100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 event.hid = list->buffer[list->tail].usage_code;
394 event.value = list->buffer[list->tail].value;
Oliver Neukum07903402008-12-16 10:55:15 +0100395 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
396 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -EFAULT;
Oliver Neukum07903402008-12-16 10:55:15 +0100398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 retval += sizeof(struct hiddev_event);
400 }
401 } else {
402 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
403 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
Oliver Neukum07903402008-12-16 10:55:15 +0100404
405 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
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_usage_ref);
410 }
411 }
412 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
413 }
414
415 }
Oliver Neukum07903402008-12-16 10:55:15 +0100416 mutex_unlock(&list->thread_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 return retval;
419}
420
421/*
422 * "poll" file op
423 * No kernel lock - fine
424 */
Al Viroafc9a422017-07-03 06:39:46 -0400425static __poll_t hiddev_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 poll_wait(file, &list->hiddev->wait, wait);
430 if (list->head != list->tail)
Fabian Hennekec801aff2019-07-18 22:50:58 +0200431 return EPOLLIN | EPOLLRDNORM | EPOLLOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!list->hiddev->exist)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800433 return EPOLLERR | EPOLLHUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
437/*
438 * "ioctl" file op
439 */
Jean Delvarecf2a2992008-03-03 11:48:43 +0100440static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
441{
442 struct hid_device *hid = hiddev->hid;
443 struct hiddev_report_info rinfo;
444 struct hiddev_usage_ref_multi *uref_multi = NULL;
445 struct hiddev_usage_ref *uref;
446 struct hid_report *report;
447 struct hid_field *field;
448 int i;
449
450 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
451 if (!uref_multi)
452 return -ENOMEM;
453 uref = &uref_multi->uref;
454 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
455 if (copy_from_user(uref_multi, user_arg,
456 sizeof(*uref_multi)))
457 goto fault;
458 } else {
459 if (copy_from_user(uref, user_arg, sizeof(*uref)))
460 goto fault;
461 }
462
463 switch (cmd) {
464 case HIDIOCGUCODE:
465 rinfo.report_type = uref->report_type;
466 rinfo.report_id = uref->report_id;
467 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
468 goto inval;
469
470 if (uref->field_index >= report->maxfield)
471 goto inval;
Gustavo A. R. Silva4f652452018-06-29 17:08:44 -0500472 uref->field_index = array_index_nospec(uref->field_index,
473 report->maxfield);
Jean Delvarecf2a2992008-03-03 11:48:43 +0100474
475 field = report->field[uref->field_index];
476 if (uref->usage_index >= field->maxusage)
477 goto inval;
Gustavo A. R. Silva4f652452018-06-29 17:08:44 -0500478 uref->usage_index = array_index_nospec(uref->usage_index,
479 field->maxusage);
Jean Delvarecf2a2992008-03-03 11:48:43 +0100480
481 uref->usage_code = field->usage[uref->usage_index].hid;
482
483 if (copy_to_user(user_arg, uref, sizeof(*uref)))
484 goto fault;
485
Jiri Slabyeb991082008-10-23 01:47:34 +0200486 goto goodreturn;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100487
488 default:
489 if (cmd != HIDIOCGUSAGE &&
490 cmd != HIDIOCGUSAGES &&
491 uref->report_type == HID_REPORT_TYPE_INPUT)
492 goto inval;
493
494 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
495 field = hiddev_lookup_usage(hid, uref);
496 if (field == NULL)
497 goto inval;
498 } else {
499 rinfo.report_type = uref->report_type;
500 rinfo.report_id = uref->report_id;
501 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
502 goto inval;
503
504 if (uref->field_index >= report->maxfield)
505 goto inval;
Gustavo A. R. Silva4f652452018-06-29 17:08:44 -0500506 uref->field_index = array_index_nospec(uref->field_index,
507 report->maxfield);
Jean Delvarecf2a2992008-03-03 11:48:43 +0100508
509 field = report->field[uref->field_index];
510
511 if (cmd == HIDIOCGCOLLECTIONINDEX) {
512 if (uref->usage_index >= field->maxusage)
513 goto inval;
Breno Leitaof1127432018-10-19 17:01:33 -0300514 uref->usage_index =
515 array_index_nospec(uref->usage_index,
516 field->maxusage);
Jean Delvarecf2a2992008-03-03 11:48:43 +0100517 } else if (uref->usage_index >= field->report_count)
518 goto inval;
Dan Carpenterac065bf2011-03-26 04:47:35 +0300519 }
Jean Delvarecf2a2992008-03-03 11:48:43 +0100520
Breno Leitaof1127432018-10-19 17:01:33 -0300521 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
522 if (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
523 uref->usage_index + uref_multi->num_values >
524 field->report_count)
525 goto inval;
526
527 uref->usage_index =
528 array_index_nospec(uref->usage_index,
529 field->report_count -
530 uref_multi->num_values);
531 }
Scott Bauer93a20012016-06-23 08:59:47 -0600532
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 Barshak33d6eb52010-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 Barshak33d6eb52010-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 Barshak33d6eb52010-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 Barshak33d6eb52010-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 Barshak33d6eb52010-12-06 18:16:11 +0300643 if (i < hid->maxcollection)
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300644 r = hid->collection[i].usage;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 case HIDIOCGDEVINFO:
Valentine Barshak33d6eb52010-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 Barshak33d6eb52010-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 Barshak33d6eb52010-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 Barshak33d6eb52010-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 Barshak33d6eb52010-12-06 18:16:11 +0300685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 list->flags = newflags;
688
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300689 r = 0;
690 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 case HIDIOCGSTRING:
Valentine Barshak33d6eb52010-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);
Benjamin Tissoires91430592017-03-08 15:11:14 +0100699 hiddev->initialized = true;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300700 r = 0;
701 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 case HIDIOCGREPORT:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300704 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
705 r = -EFAULT;
706 break;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300712 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300713 if (report == NULL)
714 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300715
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100716 hid_hw_request(hid, report, HID_REQ_GET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100717 hid_hw_wait(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300719 r = 0;
720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 case HIDIOCSREPORT:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300723 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
724 r = -EFAULT;
725 break;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
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;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300734
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100735 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100736 hid_hw_wait(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300738 r = 0;
739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 case HIDIOCGREPORTINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300742 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
743 r = -EFAULT;
744 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300745 }
746
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300747 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300748 if (report == NULL)
749 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 rinfo.num_fields = report->maxfield;
752
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300753 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
754 -EFAULT : 0;
755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 case HIDIOCGFIELDINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300758 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
759 r = -EFAULT;
760 break;
761 }
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 rinfo.report_type = finfo.report_type;
764 rinfo.report_id = finfo.report_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300766 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300767 if (report == NULL)
768 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300769
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300770 if (finfo.field_index >= report->maxfield)
771 break;
Gustavo A. R. Silva4f652452018-06-29 17:08:44 -0500772 finfo.field_index = array_index_nospec(finfo.field_index,
773 report->maxfield);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 field = report->field[finfo.field_index];
776 memset(&finfo, 0, sizeof(finfo));
777 finfo.report_type = rinfo.report_type;
778 finfo.report_id = rinfo.report_id;
779 finfo.field_index = field->report_count - 1;
780 finfo.maxusage = field->maxusage;
781 finfo.flags = field->flags;
782 finfo.physical = field->physical;
783 finfo.logical = field->logical;
784 finfo.application = field->application;
785 finfo.logical_minimum = field->logical_minimum;
786 finfo.logical_maximum = field->logical_maximum;
787 finfo.physical_minimum = field->physical_minimum;
788 finfo.physical_maximum = field->physical_maximum;
789 finfo.unit_exponent = field->unit_exponent;
790 finfo.unit = field->unit;
791
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300792 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
793 -EFAULT : 0;
794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 case HIDIOCGUCODE:
Jean Delvarecf2a2992008-03-03 11:48:43 +0100797 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 case HIDIOCGUSAGE:
799 case HIDIOCSUSAGE:
800 case HIDIOCGUSAGES:
801 case HIDIOCSUSAGES:
802 case HIDIOCGCOLLECTIONINDEX:
Benjamin Tissoires91430592017-03-08 15:11:14 +0100803 if (!hiddev->initialized) {
804 usbhid_init_reports(hid);
805 hiddev->initialized = true;
806 }
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300807 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 case HIDIOCGCOLLECTIONINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300811 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
812 r = -EFAULT;
813 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300814 }
815
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300816 if (cinfo.index >= hid->maxcollection)
817 break;
Gustavo A. R. Silva4f652452018-06-29 17:08:44 -0500818 cinfo.index = array_index_nospec(cinfo.index,
819 hid->maxcollection);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 cinfo.type = hid->collection[cinfo.index].type;
822 cinfo.usage = hid->collection[cinfo.index].usage;
823 cinfo.level = hid->collection[cinfo.index].level;
824
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300825 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
826 -EFAULT : 0;
827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200834 int len = strlen(hid->name) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (len > _IOC_SIZE(cmd))
836 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300837 r = copy_to_user(user_arg, hid->name, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 -EFAULT : len;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200843 int len = strlen(hid->phys) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (len > _IOC_SIZE(cmd))
845 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300846 r = copy_to_user(user_arg, hid->phys, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 -EFAULT : len;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 }
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300851
852ret_unlock:
853 mutex_unlock(&hiddev->existancelock);
854 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300857static const struct file_operations hiddev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 .owner = THIS_MODULE,
859 .read = hiddev_read,
860 .write = hiddev_write,
861 .poll = hiddev_poll,
862 .open = hiddev_open,
863 .release = hiddev_release,
Alan Cox7961df12008-05-26 11:25:20 +0200864 .unlocked_ioctl = hiddev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 .fasync = hiddev_fasync,
Arnd Bergmann407e9ef2018-09-11 17:23:00 +0200866 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200867 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868};
869
Al Viro2c9ede52011-07-23 20:24:48 -0400870static char *hiddev_devnode(struct device *dev, umode_t *mode)
Kay Sieversf7a386c2009-04-30 15:23:42 +0200871{
872 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700876 .name = "hiddev%d",
Kay Sieverse454cea2009-09-18 23:01:12 +0200877 .devnode = hiddev_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 .fops = &hiddev_fops,
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500879 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880};
881
882/*
883 * This is where hid.c calls us to connect a hid device to the hiddev driver
884 */
Jiri Slaby93c10132008-06-27 00:04:24 +0200885int hiddev_connect(struct hid_device *hid, unsigned int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct hiddev *hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100888 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int retval;
890
Jiri Slaby93c10132008-06-27 00:04:24 +0200891 if (!force) {
892 unsigned int i;
893 for (i = 0; i < hid->maxcollection; i++)
894 if (hid->collection[i].type ==
895 HID_COLLECTION_APPLICATION &&
896 !IS_INPUT_APPLICATION(hid->collection[i].usage))
897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Jiri Slaby93c10132008-06-27 00:04:24 +0200899 if (i == hid->maxcollection)
900 return -1;
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100903 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Oliver Neukum07903402008-12-16 10:55:15 +0100906 init_waitqueue_head(&hiddev->wait);
907 INIT_LIST_HEAD(&hiddev->list);
908 spin_lock_init(&hiddev->list_lock);
909 mutex_init(&hiddev->existancelock);
Jiri Kosina76052742009-01-07 13:25:36 +0100910 hid->hiddev = hiddev;
Oliver Neukum07903402008-12-16 10:55:15 +0100911 hiddev->hid = hid;
912 hiddev->exist = 1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100913 retval = usb_register_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800915 hid_err(hid, "Not able to get a minor for this device\n");
Jiri Kosina76052742009-01-07 13:25:36 +0100916 hid->hiddev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 kfree(hiddev);
918 return -1;
919 }
Benjamin Tissoires91430592017-03-08 15:11:14 +0100920
921 /*
922 * If HID_QUIRK_NO_INIT_REPORTS is set, make sure we don't initialize
923 * the reports.
924 */
925 hiddev->initialized = hid->quirks & HID_QUIRK_NO_INIT_REPORTS;
926
Jaejoong Kim733aca92017-03-03 17:54:01 +0900927 hiddev->minor = usbhid->intf->minor;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
932/*
933 * This is where hid.c calls us to disconnect a hiddev device from the
934 * corresponding hid device (usually because the usb device has disconnected)
935 */
936static struct usb_class_driver hiddev_class;
937void hiddev_disconnect(struct hid_device *hid)
938{
939 struct hiddev *hiddev = hid->hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100940 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Ming Leiba183112012-01-12 17:42:22 +0800942 usb_deregister_dev(usbhid->intf, &hiddev_class);
943
Oliver Neukum07903402008-12-16 10:55:15 +0100944 mutex_lock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 hiddev->exist = 0;
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (hiddev->open) {
Jiri Kosina7f778972011-05-24 11:43:18 +0200948 mutex_unlock(&hiddev->existancelock);
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700949 hid_hw_close(hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 wake_up_interruptible(&hiddev->wait);
951 } else {
Jiri Kosina7f778972011-05-24 11:43:18 +0200952 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree(hiddev);
954 }
955}