blob: 0ff3e7e70c8df48095906d1e9933e89b45611fd3 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010030#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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>
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -070038#include <linux/vmalloc.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070050struct hiddev_list {
51 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
52 int head;
53 int tail;
54 unsigned flags;
55 struct fasync_struct *fasync;
56 struct hiddev *hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040057 struct list_head node;
Oliver Neukum07903402008-12-16 10:55:15 +010058 struct mutex thread_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Find a report, given the report's type and ID. The ID can be specified
63 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
64 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
65 * given type which follows old_id.
66 */
67static struct hid_report *
68hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
69{
Dmitry Torokhov826d5982006-07-19 01:09:10 -040070 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
71 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct hid_report_enum *report_enum;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040073 struct hid_report *report;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct list_head *list;
75
76 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -040077 rinfo->report_type > HID_REPORT_TYPE_MAX)
78 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 report_enum = hid->report_enum +
81 (rinfo->report_type - HID_REPORT_TYPE_MIN);
82
83 switch (flags) {
84 case 0: /* Nothing to do -- report_id is already set correctly */
85 break;
86
87 case HID_REPORT_ID_FIRST:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040088 if (list_empty(&report_enum->report_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -040090
91 list = report_enum->report_list.next;
92 report = list_entry(list, struct hid_report, list);
93 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 case HID_REPORT_ID_NEXT:
Dmitry Torokhov826d5982006-07-19 01:09:10 -040097 report = report_enum->report_id_hash[rid];
98 if (!report)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400100
101 list = report->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (list == &report_enum->report_list)
103 return NULL;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400104
105 report = list_entry(list, struct hid_report, list);
106 rinfo->report_id = report->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 default:
110 return NULL;
111 }
112
113 return report_enum->report_id_hash[rinfo->report_id];
114}
115
116/*
117 * Perform an exhaustive search of the report table for a usage, given its
118 * type and usage id.
119 */
120static struct hid_field *
121hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
122{
123 int i, j;
124 struct hid_report *report;
125 struct hid_report_enum *report_enum;
126 struct hid_field *field;
127
128 if (uref->report_type < HID_REPORT_TYPE_MIN ||
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400129 uref->report_type > HID_REPORT_TYPE_MAX)
130 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 report_enum = hid->report_enum +
133 (uref->report_type - HID_REPORT_TYPE_MIN);
134
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400135 list_for_each_entry(report, &report_enum->report_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 for (i = 0; i < report->maxfield; i++) {
137 field = report->field[i];
138 for (j = 0; j < field->maxusage; j++) {
139 if (field->usage[j].hid == uref->usage_code) {
140 uref->report_id = report->id;
141 uref->field_index = i;
142 uref->usage_index = j;
143 return field;
144 }
145 }
146 }
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return NULL;
150}
151
152static void hiddev_send_event(struct hid_device *hid,
153 struct hiddev_usage_ref *uref)
154{
155 struct hiddev *hiddev = hid->hiddev;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400156 struct hiddev_list *list;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200157 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200159 spin_lock_irqsave(&hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400160 list_for_each_entry(list, &hiddev->list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (uref->field_index != HID_FIELD_INDEX_NONE ||
162 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
163 list->buffer[list->head] = *uref;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500164 list->head = (list->head + 1) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 (HIDDEV_BUFFER_SIZE - 1);
166 kill_fasync(&list->fasync, SIGIO, POLL_IN);
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200169 spin_unlock_irqrestore(&hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 wake_up_interruptible(&hiddev->wait);
172}
173
174/*
175 * This is where hid.c calls into hiddev to pass an event that occurred over
176 * the interrupt pipe
177 */
178void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
David Howells7d12e782006-10-05 14:55:46 +0100179 struct hid_usage *usage, __s32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 unsigned type = field->report_type;
182 struct hiddev_usage_ref uref;
183
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500184 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500186 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400187 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 uref.report_id = field->report->id;
189 uref.field_index = field->index;
190 uref.usage_index = (usage - field->usage);
191 uref.usage_code = usage->hid;
192 uref.value = value;
193
194 hiddev_send_event(hid, &uref);
195}
Jiri Kosina229695e2006-12-08 18:40:53 +0100196EXPORT_SYMBOL_GPL(hiddev_hid_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
199{
200 unsigned type = report->type;
201 struct hiddev_usage_ref uref;
202
203 memset(&uref, 0, sizeof(uref));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500204 uref.report_type =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500206 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400207 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 uref.report_id = report->id;
209 uref.field_index = HID_FIELD_INDEX_NONE;
210
211 hiddev_send_event(hid, &uref);
212}
Jiri Kosinaaa8de2f2006-12-08 18:41:17 +0100213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * fasync file op
216 */
217static int hiddev_fasync(int fd, struct file *file, int on)
218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct hiddev_list *list = file->private_data;
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400220
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700221 return fasync_helper(fd, file, on, &list->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224
225/*
226 * release file op
227 */
228static int hiddev_release(struct inode * inode, struct file * file)
229{
230 struct hiddev_list *list = file->private_data;
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200231 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200233 spin_lock_irqsave(&list->hiddev->list_lock, flags);
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400234 list_del(&list->node);
Jiri Kosinacdcb44e2007-05-10 08:45:56 +0200235 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200237 mutex_lock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!--list->hiddev->open) {
Oliver Neukum0361a282008-12-17 15:38:03 +0100239 if (list->hiddev->exist) {
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700240 hid_hw_close(list->hiddev->hid);
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700241 hid_hw_power(list->hiddev->hid, PM_HINT_NORMAL);
Oliver Neukum0361a282008-12-17 15:38:03 +0100242 } else {
Dan Carpenter5c699d72011-05-26 11:49:16 +0300243 mutex_unlock(&list->hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 kfree(list->hiddev);
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -0700245 vfree(list);
Dan Carpenter5c699d72011-05-26 11:49:16 +0300246 return 0;
Oliver Neukum0361a282008-12-17 15:38:03 +0100247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200250 mutex_unlock(&list->hiddev->existancelock);
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -0700251 vfree(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 return 0;
254}
255
256/*
257 * open file op
258 */
Dmitry Torokhov826d5982006-07-19 01:09:10 -0400259static int hiddev_open(struct inode *inode, struct file *file)
260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct hiddev_list *list;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200262 struct usb_interface *intf;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200263 struct hid_device *hid;
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200264 struct hiddev *hiddev;
265 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Guillaume Chazarain8fe294c2010-09-12 21:32:35 +0200267 intf = usbhid_find_interface(iminor(inode));
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200268 if (!intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -ENODEV;
Jiri Kosina9c9e54a2010-08-13 12:19:45 +0200270 hid = usb_get_intfdata(intf);
271 hiddev = hid->hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Havard Skinnemoend4f0e4da2012-04-26 11:16:00 -0700273 if (!(list = vzalloc(sizeof(struct hiddev_list))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -ENOMEM;
Oliver Neukum07903402008-12-16 10:55:15 +0100275 mutex_init(&list->thread_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200276 list->hiddev = hiddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 file->private_data = list;
278
Oliver Neukum07903402008-12-16 10:55:15 +0100279 /*
280 * no need for locking because the USB major number
281 * is shared which usbcore guards against disconnect
282 */
283 if (list->hiddev->exist) {
284 if (!list->hiddev->open++) {
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700285 res = hid_hw_open(hiddev->hid);
286 if (res < 0)
Oliver Neukum07903402008-12-16 10:55:15 +0100287 goto bail;
Oliver Neukum07903402008-12-16 10:55:15 +0100288 }
289 } else {
290 res = -ENODEV;
291 goto bail;
292 }
293
294 spin_lock_irq(&list->hiddev->list_lock);
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200295 list_add_tail(&list->node, &hiddev->list);
Oliver Neukum07903402008-12-16 10:55:15 +0100296 spin_unlock_irq(&list->hiddev->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200298 mutex_lock(&hiddev->existancelock);
Oliver Neukum0361a282008-12-17 15:38:03 +0100299 if (!list->hiddev->open++)
300 if (list->hiddev->exist) {
Arnd Bergmannbd25f4d2010-07-11 15:34:05 +0200301 struct hid_device *hid = hiddev->hid;
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700302 res = hid_hw_power(hid, PM_HINT_FULLON);
303 if (res < 0)
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200304 goto bail_unlock;
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700305 res = hid_hw_open(hid);
306 if (res < 0)
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700307 goto bail_normal_power;
Oliver Neukum0361a282008-12-17 15:38:03 +0100308 }
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200309 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
Dmitry Torokhov9a835632017-06-06 23:59:32 -0700311bail_normal_power:
312 hid_hw_power(hid, PM_HINT_NORMAL);
Jiri Kosina6cb4b042011-05-20 10:50:13 +0200313bail_unlock:
314 mutex_unlock(&hiddev->existancelock);
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)
431 return POLLIN | POLLRDNORM;
432 if (!list->hiddev->exist)
433 return POLLERR | POLLHUP;
434 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;
472
473 field = report->field[uref->field_index];
474 if (uref->usage_index >= field->maxusage)
475 goto inval;
476
477 uref->usage_code = field->usage[uref->usage_index].hid;
478
479 if (copy_to_user(user_arg, uref, sizeof(*uref)))
480 goto fault;
481
Jiri Slabyeb991082008-10-23 01:47:34 +0200482 goto goodreturn;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100483
484 default:
485 if (cmd != HIDIOCGUSAGE &&
486 cmd != HIDIOCGUSAGES &&
487 uref->report_type == HID_REPORT_TYPE_INPUT)
488 goto inval;
489
490 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
491 field = hiddev_lookup_usage(hid, uref);
492 if (field == NULL)
493 goto inval;
494 } else {
495 rinfo.report_type = uref->report_type;
496 rinfo.report_id = uref->report_id;
497 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
498 goto inval;
499
500 if (uref->field_index >= report->maxfield)
501 goto inval;
502
503 field = report->field[uref->field_index];
504
505 if (cmd == HIDIOCGCOLLECTIONINDEX) {
506 if (uref->usage_index >= field->maxusage)
507 goto inval;
508 } else if (uref->usage_index >= field->report_count)
509 goto inval;
Dan Carpenterac065bf2011-03-26 04:47:35 +0300510 }
Jean Delvarecf2a2992008-03-03 11:48:43 +0100511
Scott Bauer93a20012016-06-23 08:59:47 -0600512 if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
513 (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
514 uref->usage_index + uref_multi->num_values > field->report_count))
515 goto inval;
516
Jean Delvarecf2a2992008-03-03 11:48:43 +0100517 switch (cmd) {
518 case HIDIOCGUSAGE:
519 uref->value = field->value[uref->usage_index];
520 if (copy_to_user(user_arg, uref, sizeof(*uref)))
521 goto fault;
522 goto goodreturn;
523
524 case HIDIOCSUSAGE:
525 field->value[uref->usage_index] = uref->value;
526 goto goodreturn;
527
528 case HIDIOCGCOLLECTIONINDEX:
Jiri Slaby48594842009-06-19 23:24:11 +0200529 i = field->usage[uref->usage_index].collection_index;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100530 kfree(uref_multi);
Jiri Slaby48594842009-06-19 23:24:11 +0200531 return i;
Jean Delvarecf2a2992008-03-03 11:48:43 +0100532 case HIDIOCGUSAGES:
533 for (i = 0; i < uref_multi->num_values; i++)
534 uref_multi->values[i] =
535 field->value[uref->usage_index + i];
536 if (copy_to_user(user_arg, uref_multi,
537 sizeof(*uref_multi)))
538 goto fault;
539 goto goodreturn;
540 case HIDIOCSUSAGES:
541 for (i = 0; i < uref_multi->num_values; i++)
542 field->value[uref->usage_index + i] =
543 uref_multi->values[i];
544 goto goodreturn;
545 }
546
547goodreturn:
548 kfree(uref_multi);
549 return 0;
550fault:
551 kfree(uref_multi);
552 return -EFAULT;
553inval:
554 kfree(uref_multi);
555 return -EINVAL;
556 }
557}
558
559static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
560{
561 struct hid_device *hid = hiddev->hid;
562 struct usb_device *dev = hid_to_usb_dev(hid);
563 int idx, len;
564 char *buf;
565
566 if (get_user(idx, (int __user *)user_arg))
567 return -EFAULT;
568
569 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
570 return -ENOMEM;
571
572 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
573 kfree(buf);
574 return -EINVAL;
575 }
576
577 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
578 kfree(buf);
579 return -EFAULT;
580 }
581
582 kfree(buf);
583
584 return len;
585}
586
Alan Cox7961df12008-05-26 11:25:20 +0200587static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct hiddev_list *list = file->private_data;
590 struct hiddev *hiddev = list->hiddev;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300591 struct hid_device *hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct hiddev_collection_info cinfo;
593 struct hiddev_report_info rinfo;
594 struct hiddev_field_info finfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct hiddev_devinfo dinfo;
596 struct hid_report *report;
597 struct hid_field *field;
598 void __user *user_arg = (void __user *)arg;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300599 int i, r = -EINVAL;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300600
Alan Cox7961df12008-05-26 11:25:20 +0200601 /* Called without BKL by compat methods so no BKL taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300603 mutex_lock(&hiddev->existancelock);
604 if (!hiddev->exist) {
605 r = -ENODEV;
606 goto ret_unlock;
607 }
608
609 hid = hiddev->hid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 switch (cmd) {
612
613 case HIDIOCGVERSION:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300614 r = put_user(HID_VERSION, (int __user *)arg) ?
615 -EFAULT : 0;
616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 case HIDIOCAPPLICATION:
Tushar Beherad339f612012-11-16 12:20:43 +0530619 if (arg >= hid->maxapplication)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300620 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 for (i = 0; i < hid->maxcollection; i++)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500623 if (hid->collection[i].type ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 HID_COLLECTION_APPLICATION && arg-- == 0)
625 break;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500626
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300627 if (i < hid->maxcollection)
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300628 r = hid->collection[i].usage;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300629 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 case HIDIOCGDEVINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300632 {
633 struct usb_device *dev = hid_to_usb_dev(hid);
634 struct usbhid_device *usbhid = hid->driver_data;
635
Dan Carpenter9561f7f2011-09-23 09:21:13 +0300636 memset(&dinfo, 0, sizeof(dinfo));
637
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300638 dinfo.bustype = BUS_USB;
639 dinfo.busnum = dev->bus->busnum;
640 dinfo.devnum = dev->devnum;
641 dinfo.ifnum = usbhid->ifnum;
642 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
643 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
644 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
645 dinfo.num_applications = hid->maxapplication;
646
647 r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
648 -EFAULT : 0;
649 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 case HIDIOCGFLAG:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300653 r = put_user(list->flags, (int __user *)arg) ?
654 -EFAULT : 0;
655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 case HIDIOCSFLAG:
658 {
659 int newflags;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300660
661 if (get_user(newflags, (int __user *)arg)) {
662 r = -EFAULT;
663 break;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
667 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
668 (newflags & HIDDEV_FLAG_UREF) == 0))
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300669 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 list->flags = newflags;
672
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300673 r = 0;
674 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 case HIDIOCGSTRING:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300678 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 case HIDIOCINITREPORT:
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100682 usbhid_init_reports(hid);
Benjamin Tissoires91430592017-03-08 15:11:14 +0100683 hiddev->initialized = true;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300684 r = 0;
685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 case HIDIOCGREPORT:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300688 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
689 r = -EFAULT;
690 break;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300694 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300696 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300697 if (report == NULL)
698 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300699
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100700 hid_hw_request(hid, report, HID_REQ_GET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100701 hid_hw_wait(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300703 r = 0;
704 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 case HIDIOCSREPORT:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300707 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
708 r = -EFAULT;
709 break;
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300715 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300716 if (report == NULL)
717 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300718
Benjamin Tissoiresd88142722013-02-25 11:31:46 +0100719 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
Benjamin Tissoiresb7966a42013-02-25 11:31:47 +0100720 hid_hw_wait(hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300722 r = 0;
723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 case HIDIOCGREPORTINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300726 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
727 r = -EFAULT;
728 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300729 }
730
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300731 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300732 if (report == NULL)
733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 rinfo.num_fields = report->maxfield;
736
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300737 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
738 -EFAULT : 0;
739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 case HIDIOCGFIELDINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300742 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
743 r = -EFAULT;
744 break;
745 }
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 rinfo.report_type = finfo.report_type;
748 rinfo.report_id = finfo.report_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300750 report = hiddev_lookup_report(hid, &rinfo);
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300751 if (report == NULL)
752 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300753
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300754 if (finfo.field_index >= report->maxfield)
755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 field = report->field[finfo.field_index];
758 memset(&finfo, 0, sizeof(finfo));
759 finfo.report_type = rinfo.report_type;
760 finfo.report_id = rinfo.report_id;
761 finfo.field_index = field->report_count - 1;
762 finfo.maxusage = field->maxusage;
763 finfo.flags = field->flags;
764 finfo.physical = field->physical;
765 finfo.logical = field->logical;
766 finfo.application = field->application;
767 finfo.logical_minimum = field->logical_minimum;
768 finfo.logical_maximum = field->logical_maximum;
769 finfo.physical_minimum = field->physical_minimum;
770 finfo.physical_maximum = field->physical_maximum;
771 finfo.unit_exponent = field->unit_exponent;
772 finfo.unit = field->unit;
773
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300774 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
775 -EFAULT : 0;
776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 case HIDIOCGUCODE:
Jean Delvarecf2a2992008-03-03 11:48:43 +0100779 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 case HIDIOCGUSAGE:
781 case HIDIOCSUSAGE:
782 case HIDIOCGUSAGES:
783 case HIDIOCSUSAGES:
784 case HIDIOCGCOLLECTIONINDEX:
Benjamin Tissoires91430592017-03-08 15:11:14 +0100785 if (!hiddev->initialized) {
786 usbhid_init_reports(hid);
787 hiddev->initialized = true;
788 }
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300789 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 case HIDIOCGCOLLECTIONINFO:
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300793 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
794 r = -EFAULT;
795 break;
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300796 }
797
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300798 if (cinfo.index >= hid->maxcollection)
799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 cinfo.type = hid->collection[cinfo.index].type;
802 cinfo.usage = hid->collection[cinfo.index].usage;
803 cinfo.level = hid->collection[cinfo.index].level;
804
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300805 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
806 -EFAULT : 0;
807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300811 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200814 int len = strlen(hid->name) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (len > _IOC_SIZE(cmd))
816 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300817 r = copy_to_user(user_arg, hid->name, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 -EFAULT : len;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
822 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200823 int len = strlen(hid->phys) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (len > _IOC_SIZE(cmd))
825 len = _IOC_SIZE(cmd);
Valentine Barshak1a8e8fa2010-12-06 17:51:41 +0300826 r = copy_to_user(user_arg, hid->phys, len) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 -EFAULT : len;
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
Valentine Barshak33d6eb52010-12-06 18:16:11 +0300831
832ret_unlock:
833 mutex_unlock(&hiddev->existancelock);
834 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200837#ifdef CONFIG_COMPAT
838static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
839{
Jiri Kosina88af45b2008-05-27 11:36:40 +0200840 return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200841}
842#endif
843
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300844static const struct file_operations hiddev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 .owner = THIS_MODULE,
846 .read = hiddev_read,
847 .write = hiddev_write,
848 .poll = hiddev_poll,
849 .open = hiddev_open,
850 .release = hiddev_release,
Alan Cox7961df12008-05-26 11:25:20 +0200851 .unlocked_ioctl = hiddev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 .fasync = hiddev_fasync,
Philip Langdalebb6c8d82007-10-14 12:03:58 +0200853#ifdef CONFIG_COMPAT
854 .compat_ioctl = hiddev_compat_ioctl,
855#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200856 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858
Al Viro2c9ede52011-07-23 20:24:48 -0400859static char *hiddev_devnode(struct device *dev, umode_t *mode)
Kay Sieversf7a386c2009-04-30 15:23:42 +0200860{
861 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static struct usb_class_driver hiddev_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700865 .name = "hiddev%d",
Kay Sieverse454cea2009-09-18 23:01:12 +0200866 .devnode = hiddev_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .fops = &hiddev_fops,
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500868 .minor_base = HIDDEV_MINOR_BASE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869};
870
871/*
872 * This is where hid.c calls us to connect a hid device to the hiddev driver
873 */
Jiri Slaby93c10132008-06-27 00:04:24 +0200874int hiddev_connect(struct hid_device *hid, unsigned int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct hiddev *hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100877 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int retval;
879
Jiri Slaby93c10132008-06-27 00:04:24 +0200880 if (!force) {
881 unsigned int i;
882 for (i = 0; i < hid->maxcollection; i++)
883 if (hid->collection[i].type ==
884 HID_COLLECTION_APPLICATION &&
885 !IS_INPUT_APPLICATION(hid->collection[i].usage))
886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Jiri Slaby93c10132008-06-27 00:04:24 +0200888 if (i == hid->maxcollection)
889 return -1;
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Oliver Neukumbbdb7da2006-01-06 20:54:29 +0100892 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Oliver Neukum07903402008-12-16 10:55:15 +0100895 init_waitqueue_head(&hiddev->wait);
896 INIT_LIST_HEAD(&hiddev->list);
897 spin_lock_init(&hiddev->list_lock);
898 mutex_init(&hiddev->existancelock);
Jiri Kosina76052742009-01-07 13:25:36 +0100899 hid->hiddev = hiddev;
Oliver Neukum07903402008-12-16 10:55:15 +0100900 hiddev->hid = hid;
901 hiddev->exist = 1;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100902 retval = usb_register_dev(usbhid->intf, &hiddev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800904 hid_err(hid, "Not able to get a minor for this device\n");
Jiri Kosina76052742009-01-07 13:25:36 +0100905 hid->hiddev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 kfree(hiddev);
907 return -1;
908 }
Benjamin Tissoires91430592017-03-08 15:11:14 +0100909
910 /*
911 * If HID_QUIRK_NO_INIT_REPORTS is set, make sure we don't initialize
912 * the reports.
913 */
914 hiddev->initialized = hid->quirks & HID_QUIRK_NO_INIT_REPORTS;
915
Jaejoong Kim733aca92017-03-03 17:54:01 +0900916 hiddev->minor = usbhid->intf->minor;
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return 0;
919}
920
921/*
922 * This is where hid.c calls us to disconnect a hiddev device from the
923 * corresponding hid device (usually because the usb device has disconnected)
924 */
925static struct usb_class_driver hiddev_class;
926void hiddev_disconnect(struct hid_device *hid)
927{
928 struct hiddev *hiddev = hid->hiddev;
Jiri Kosina4916b3a2006-12-08 18:41:03 +0100929 struct usbhid_device *usbhid = hid->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Ming Leiba183112012-01-12 17:42:22 +0800931 usb_deregister_dev(usbhid->intf, &hiddev_class);
932
Oliver Neukum07903402008-12-16 10:55:15 +0100933 mutex_lock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 hiddev->exist = 0;
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (hiddev->open) {
Jiri Kosina7f778972011-05-24 11:43:18 +0200937 mutex_unlock(&hiddev->existancelock);
Dmitry Torokhovd36b7d42017-06-06 23:59:31 -0700938 hid_hw_close(hiddev->hid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 wake_up_interruptible(&hiddev->wait);
940 } else {
Jiri Kosina7f778972011-05-24 11:43:18 +0200941 mutex_unlock(&hiddev->existancelock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 kfree(hiddev);
943 }
944}