[RAMEN9610-21380]HID: hiddev: avoid opening a disconnected device
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / hid / usbhid / hiddev.c
1 /*
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
28 #include <linux/poll.h>
29 #include <linux/slab.h>
30 #include <linux/sched/signal.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/input.h>
34 #include <linux/usb.h>
35 #include <linux/hid.h>
36 #include <linux/hiddev.h>
37 #include <linux/compat.h>
38 #include <linux/vmalloc.h>
39 #include <linux/nospec.h>
40 #include "usbhid.h"
41
42 #ifdef CONFIG_USB_DYNAMIC_MINORS
43 #define HIDDEV_MINOR_BASE 0
44 #define HIDDEV_MINORS 256
45 #else
46 #define HIDDEV_MINOR_BASE 96
47 #define HIDDEV_MINORS 16
48 #endif
49 #define HIDDEV_BUFFER_SIZE 2048
50
51 struct hiddev_list {
52 struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE];
53 int head;
54 int tail;
55 unsigned flags;
56 struct fasync_struct *fasync;
57 struct hiddev *hiddev;
58 struct list_head node;
59 struct mutex thread_lock;
60 };
61
62 /*
63 * Find a report, given the report's type and ID. The ID can be specified
64 * indirectly by REPORT_ID_FIRST (which returns the first report of the given
65 * type) or by (REPORT_ID_NEXT | old_id), which returns the next report of the
66 * given type which follows old_id.
67 */
68 static struct hid_report *
69 hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo)
70 {
71 unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK;
72 unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK;
73 struct hid_report_enum *report_enum;
74 struct hid_report *report;
75 struct list_head *list;
76
77 if (rinfo->report_type < HID_REPORT_TYPE_MIN ||
78 rinfo->report_type > HID_REPORT_TYPE_MAX)
79 return NULL;
80
81 report_enum = hid->report_enum +
82 (rinfo->report_type - HID_REPORT_TYPE_MIN);
83
84 switch (flags) {
85 case 0: /* Nothing to do -- report_id is already set correctly */
86 break;
87
88 case HID_REPORT_ID_FIRST:
89 if (list_empty(&report_enum->report_list))
90 return NULL;
91
92 list = report_enum->report_list.next;
93 report = list_entry(list, struct hid_report, list);
94 rinfo->report_id = report->id;
95 break;
96
97 case HID_REPORT_ID_NEXT:
98 report = report_enum->report_id_hash[rid];
99 if (!report)
100 return NULL;
101
102 list = report->list.next;
103 if (list == &report_enum->report_list)
104 return NULL;
105
106 report = list_entry(list, struct hid_report, list);
107 rinfo->report_id = report->id;
108 break;
109
110 default:
111 return NULL;
112 }
113
114 return report_enum->report_id_hash[rinfo->report_id];
115 }
116
117 /*
118 * Perform an exhaustive search of the report table for a usage, given its
119 * type and usage id.
120 */
121 static struct hid_field *
122 hiddev_lookup_usage(struct hid_device *hid, struct hiddev_usage_ref *uref)
123 {
124 int i, j;
125 struct hid_report *report;
126 struct hid_report_enum *report_enum;
127 struct hid_field *field;
128
129 if (uref->report_type < HID_REPORT_TYPE_MIN ||
130 uref->report_type > HID_REPORT_TYPE_MAX)
131 return NULL;
132
133 report_enum = hid->report_enum +
134 (uref->report_type - HID_REPORT_TYPE_MIN);
135
136 list_for_each_entry(report, &report_enum->report_list, list) {
137 for (i = 0; i < report->maxfield; i++) {
138 field = report->field[i];
139 for (j = 0; j < field->maxusage; j++) {
140 if (field->usage[j].hid == uref->usage_code) {
141 uref->report_id = report->id;
142 uref->field_index = i;
143 uref->usage_index = j;
144 return field;
145 }
146 }
147 }
148 }
149
150 return NULL;
151 }
152
153 static void hiddev_send_event(struct hid_device *hid,
154 struct hiddev_usage_ref *uref)
155 {
156 struct hiddev *hiddev = hid->hiddev;
157 struct hiddev_list *list;
158 unsigned long flags;
159
160 spin_lock_irqsave(&hiddev->list_lock, flags);
161 list_for_each_entry(list, &hiddev->list, node) {
162 if (uref->field_index != HID_FIELD_INDEX_NONE ||
163 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
164 list->buffer[list->head] = *uref;
165 list->head = (list->head + 1) &
166 (HIDDEV_BUFFER_SIZE - 1);
167 kill_fasync(&list->fasync, SIGIO, POLL_IN);
168 }
169 }
170 spin_unlock_irqrestore(&hiddev->list_lock, flags);
171
172 wake_up_interruptible(&hiddev->wait);
173 }
174
175 /*
176 * This is where hid.c calls into hiddev to pass an event that occurred over
177 * the interrupt pipe
178 */
179 void hiddev_hid_event(struct hid_device *hid, struct hid_field *field,
180 struct hid_usage *usage, __s32 value)
181 {
182 unsigned type = field->report_type;
183 struct hiddev_usage_ref uref;
184
185 uref.report_type =
186 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
187 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
188 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
189 uref.report_id = field->report->id;
190 uref.field_index = field->index;
191 uref.usage_index = (usage - field->usage);
192 uref.usage_code = usage->hid;
193 uref.value = value;
194
195 hiddev_send_event(hid, &uref);
196 }
197 EXPORT_SYMBOL_GPL(hiddev_hid_event);
198
199 void hiddev_report_event(struct hid_device *hid, struct hid_report *report)
200 {
201 unsigned type = report->type;
202 struct hiddev_usage_ref uref;
203
204 memset(&uref, 0, sizeof(uref));
205 uref.report_type =
206 (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT :
207 ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT :
208 ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0));
209 uref.report_id = report->id;
210 uref.field_index = HID_FIELD_INDEX_NONE;
211
212 hiddev_send_event(hid, &uref);
213 }
214
215 /*
216 * fasync file op
217 */
218 static int hiddev_fasync(int fd, struct file *file, int on)
219 {
220 struct hiddev_list *list = file->private_data;
221
222 return fasync_helper(fd, file, on, &list->fasync);
223 }
224
225
226 /*
227 * release file op
228 */
229 static int hiddev_release(struct inode * inode, struct file * file)
230 {
231 struct hiddev_list *list = file->private_data;
232 unsigned long flags;
233
234 spin_lock_irqsave(&list->hiddev->list_lock, flags);
235 list_del(&list->node);
236 spin_unlock_irqrestore(&list->hiddev->list_lock, flags);
237
238 mutex_lock(&list->hiddev->existancelock);
239 if (!--list->hiddev->open) {
240 if (list->hiddev->exist) {
241 hid_hw_close(list->hiddev->hid);
242 hid_hw_power(list->hiddev->hid, PM_HINT_NORMAL);
243 } else {
244 mutex_unlock(&list->hiddev->existancelock);
245 kfree(list->hiddev);
246 vfree(list);
247 return 0;
248 }
249 }
250
251 mutex_unlock(&list->hiddev->existancelock);
252 vfree(list);
253
254 return 0;
255 }
256
257 /*
258 * open file op
259 */
260 static int hiddev_open(struct inode *inode, struct file *file)
261 {
262 struct hiddev_list *list;
263 struct usb_interface *intf;
264 struct hid_device *hid;
265 struct hiddev *hiddev;
266 int res;
267
268 intf = usbhid_find_interface(iminor(inode));
269 if (!intf)
270 return -ENODEV;
271 hid = usb_get_intfdata(intf);
272 hiddev = hid->hiddev;
273
274 if (!(list = vzalloc(sizeof(struct hiddev_list))))
275 return -ENOMEM;
276 mutex_init(&list->thread_lock);
277 list->hiddev = hiddev;
278 file->private_data = list;
279
280 /*
281 * no need for locking because the USB major number
282 * is shared which usbcore guards against disconnect
283 */
284 if (list->hiddev->exist) {
285 if (!list->hiddev->open++) {
286 res = hid_hw_open(hiddev->hid);
287 if (res < 0)
288 goto bail;
289 }
290 } else {
291 res = -ENODEV;
292 goto bail;
293 }
294
295 spin_lock_irq(&list->hiddev->list_lock);
296 list_add_tail(&list->node, &hiddev->list);
297 spin_unlock_irq(&list->hiddev->list_lock);
298
299 mutex_lock(&hiddev->existancelock);
300 /*
301 * recheck exist with existance lock held to
302 * avoid opening a disconnected device
303 */
304 if (!list->hiddev->exist) {
305 res = -ENODEV;
306 goto bail_unlock;
307 }
308 if (!list->hiddev->open++)
309 if (list->hiddev->exist) {
310 struct hid_device *hid = hiddev->hid;
311 res = hid_hw_power(hid, PM_HINT_FULLON);
312 if (res < 0)
313 goto bail_unlock;
314 res = hid_hw_open(hid);
315 if (res < 0)
316 goto bail_normal_power;
317 }
318 mutex_unlock(&hiddev->existancelock);
319 return 0;
320 bail_normal_power:
321 hid_hw_power(hid, PM_HINT_NORMAL);
322 bail_unlock:
323 mutex_unlock(&hiddev->existancelock);
324 bail:
325 file->private_data = NULL;
326 vfree(list);
327 return res;
328 }
329
330 /*
331 * "write" file op
332 */
333 static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
334 {
335 return -EINVAL;
336 }
337
338 /*
339 * "read" file op
340 */
341 static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
342 {
343 DEFINE_WAIT(wait);
344 struct hiddev_list *list = file->private_data;
345 int event_size;
346 int retval;
347
348 event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ?
349 sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event);
350
351 if (count < event_size)
352 return 0;
353
354 /* lock against other threads */
355 retval = mutex_lock_interruptible(&list->thread_lock);
356 if (retval)
357 return -ERESTARTSYS;
358
359 while (retval == 0) {
360 if (list->head == list->tail) {
361 prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE);
362
363 while (list->head == list->tail) {
364 if (signal_pending(current)) {
365 retval = -ERESTARTSYS;
366 break;
367 }
368 if (!list->hiddev->exist) {
369 retval = -EIO;
370 break;
371 }
372 if (file->f_flags & O_NONBLOCK) {
373 retval = -EAGAIN;
374 break;
375 }
376
377 /* let O_NONBLOCK tasks run */
378 mutex_unlock(&list->thread_lock);
379 schedule();
380 if (mutex_lock_interruptible(&list->thread_lock)) {
381 finish_wait(&list->hiddev->wait, &wait);
382 return -EINTR;
383 }
384 set_current_state(TASK_INTERRUPTIBLE);
385 }
386 finish_wait(&list->hiddev->wait, &wait);
387
388 }
389
390 if (retval) {
391 mutex_unlock(&list->thread_lock);
392 return retval;
393 }
394
395
396 while (list->head != list->tail &&
397 retval + event_size <= count) {
398 if ((list->flags & HIDDEV_FLAG_UREF) == 0) {
399 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) {
400 struct hiddev_event event;
401
402 event.hid = list->buffer[list->tail].usage_code;
403 event.value = list->buffer[list->tail].value;
404 if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) {
405 mutex_unlock(&list->thread_lock);
406 return -EFAULT;
407 }
408 retval += sizeof(struct hiddev_event);
409 }
410 } else {
411 if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE ||
412 (list->flags & HIDDEV_FLAG_REPORT) != 0) {
413
414 if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) {
415 mutex_unlock(&list->thread_lock);
416 return -EFAULT;
417 }
418 retval += sizeof(struct hiddev_usage_ref);
419 }
420 }
421 list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1);
422 }
423
424 }
425 mutex_unlock(&list->thread_lock);
426
427 return retval;
428 }
429
430 /*
431 * "poll" file op
432 * No kernel lock - fine
433 */
434 static unsigned int hiddev_poll(struct file *file, poll_table *wait)
435 {
436 struct hiddev_list *list = file->private_data;
437
438 poll_wait(file, &list->hiddev->wait, wait);
439 if (list->head != list->tail)
440 return POLLIN | POLLRDNORM;
441 if (!list->hiddev->exist)
442 return POLLERR | POLLHUP;
443 return 0;
444 }
445
446 /*
447 * "ioctl" file op
448 */
449 static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
450 {
451 struct hid_device *hid = hiddev->hid;
452 struct hiddev_report_info rinfo;
453 struct hiddev_usage_ref_multi *uref_multi = NULL;
454 struct hiddev_usage_ref *uref;
455 struct hid_report *report;
456 struct hid_field *field;
457 int i;
458
459 uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL);
460 if (!uref_multi)
461 return -ENOMEM;
462 uref = &uref_multi->uref;
463 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
464 if (copy_from_user(uref_multi, user_arg,
465 sizeof(*uref_multi)))
466 goto fault;
467 } else {
468 if (copy_from_user(uref, user_arg, sizeof(*uref)))
469 goto fault;
470 }
471
472 switch (cmd) {
473 case HIDIOCGUCODE:
474 rinfo.report_type = uref->report_type;
475 rinfo.report_id = uref->report_id;
476 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
477 goto inval;
478
479 if (uref->field_index >= report->maxfield)
480 goto inval;
481 uref->field_index = array_index_nospec(uref->field_index,
482 report->maxfield);
483
484 field = report->field[uref->field_index];
485 if (uref->usage_index >= field->maxusage)
486 goto inval;
487 uref->usage_index = array_index_nospec(uref->usage_index,
488 field->maxusage);
489
490 uref->usage_code = field->usage[uref->usage_index].hid;
491
492 if (copy_to_user(user_arg, uref, sizeof(*uref)))
493 goto fault;
494
495 goto goodreturn;
496
497 default:
498 if (cmd != HIDIOCGUSAGE &&
499 cmd != HIDIOCGUSAGES &&
500 uref->report_type == HID_REPORT_TYPE_INPUT)
501 goto inval;
502
503 if (uref->report_id == HID_REPORT_ID_UNKNOWN) {
504 field = hiddev_lookup_usage(hid, uref);
505 if (field == NULL)
506 goto inval;
507 } else {
508 rinfo.report_type = uref->report_type;
509 rinfo.report_id = uref->report_id;
510 if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL)
511 goto inval;
512
513 if (uref->field_index >= report->maxfield)
514 goto inval;
515 uref->field_index = array_index_nospec(uref->field_index,
516 report->maxfield);
517
518 field = report->field[uref->field_index];
519
520 if (cmd == HIDIOCGCOLLECTIONINDEX) {
521 if (uref->usage_index >= field->maxusage)
522 goto inval;
523 uref->usage_index =
524 array_index_nospec(uref->usage_index,
525 field->maxusage);
526 } else if (uref->usage_index >= field->report_count)
527 goto inval;
528 }
529
530 if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
531 if (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
532 uref->usage_index + uref_multi->num_values >
533 field->report_count)
534 goto inval;
535
536 uref->usage_index =
537 array_index_nospec(uref->usage_index,
538 field->report_count -
539 uref_multi->num_values);
540 }
541
542 switch (cmd) {
543 case HIDIOCGUSAGE:
544 uref->value = field->value[uref->usage_index];
545 if (copy_to_user(user_arg, uref, sizeof(*uref)))
546 goto fault;
547 goto goodreturn;
548
549 case HIDIOCSUSAGE:
550 field->value[uref->usage_index] = uref->value;
551 goto goodreturn;
552
553 case HIDIOCGCOLLECTIONINDEX:
554 i = field->usage[uref->usage_index].collection_index;
555 kfree(uref_multi);
556 return i;
557 case HIDIOCGUSAGES:
558 for (i = 0; i < uref_multi->num_values; i++)
559 uref_multi->values[i] =
560 field->value[uref->usage_index + i];
561 if (copy_to_user(user_arg, uref_multi,
562 sizeof(*uref_multi)))
563 goto fault;
564 goto goodreturn;
565 case HIDIOCSUSAGES:
566 for (i = 0; i < uref_multi->num_values; i++)
567 field->value[uref->usage_index + i] =
568 uref_multi->values[i];
569 goto goodreturn;
570 }
571
572 goodreturn:
573 kfree(uref_multi);
574 return 0;
575 fault:
576 kfree(uref_multi);
577 return -EFAULT;
578 inval:
579 kfree(uref_multi);
580 return -EINVAL;
581 }
582 }
583
584 static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg)
585 {
586 struct hid_device *hid = hiddev->hid;
587 struct usb_device *dev = hid_to_usb_dev(hid);
588 int idx, len;
589 char *buf;
590
591 if (get_user(idx, (int __user *)user_arg))
592 return -EFAULT;
593
594 if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL)
595 return -ENOMEM;
596
597 if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) {
598 kfree(buf);
599 return -EINVAL;
600 }
601
602 if (copy_to_user(user_arg+sizeof(int), buf, len+1)) {
603 kfree(buf);
604 return -EFAULT;
605 }
606
607 kfree(buf);
608
609 return len;
610 }
611
612 static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
613 {
614 struct hiddev_list *list = file->private_data;
615 struct hiddev *hiddev = list->hiddev;
616 struct hid_device *hid;
617 struct hiddev_collection_info cinfo;
618 struct hiddev_report_info rinfo;
619 struct hiddev_field_info finfo;
620 struct hiddev_devinfo dinfo;
621 struct hid_report *report;
622 struct hid_field *field;
623 void __user *user_arg = (void __user *)arg;
624 int i, r = -EINVAL;
625
626 /* Called without BKL by compat methods so no BKL taken */
627
628 mutex_lock(&hiddev->existancelock);
629 if (!hiddev->exist) {
630 r = -ENODEV;
631 goto ret_unlock;
632 }
633
634 hid = hiddev->hid;
635
636 switch (cmd) {
637
638 case HIDIOCGVERSION:
639 r = put_user(HID_VERSION, (int __user *)arg) ?
640 -EFAULT : 0;
641 break;
642
643 case HIDIOCAPPLICATION:
644 if (arg >= hid->maxapplication)
645 break;
646
647 for (i = 0; i < hid->maxcollection; i++)
648 if (hid->collection[i].type ==
649 HID_COLLECTION_APPLICATION && arg-- == 0)
650 break;
651
652 if (i < hid->maxcollection)
653 r = hid->collection[i].usage;
654 break;
655
656 case HIDIOCGDEVINFO:
657 {
658 struct usb_device *dev = hid_to_usb_dev(hid);
659 struct usbhid_device *usbhid = hid->driver_data;
660
661 memset(&dinfo, 0, sizeof(dinfo));
662
663 dinfo.bustype = BUS_USB;
664 dinfo.busnum = dev->bus->busnum;
665 dinfo.devnum = dev->devnum;
666 dinfo.ifnum = usbhid->ifnum;
667 dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor);
668 dinfo.product = le16_to_cpu(dev->descriptor.idProduct);
669 dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice);
670 dinfo.num_applications = hid->maxapplication;
671
672 r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ?
673 -EFAULT : 0;
674 break;
675 }
676
677 case HIDIOCGFLAG:
678 r = put_user(list->flags, (int __user *)arg) ?
679 -EFAULT : 0;
680 break;
681
682 case HIDIOCSFLAG:
683 {
684 int newflags;
685
686 if (get_user(newflags, (int __user *)arg)) {
687 r = -EFAULT;
688 break;
689 }
690
691 if ((newflags & ~HIDDEV_FLAGS) != 0 ||
692 ((newflags & HIDDEV_FLAG_REPORT) != 0 &&
693 (newflags & HIDDEV_FLAG_UREF) == 0))
694 break;
695
696 list->flags = newflags;
697
698 r = 0;
699 break;
700 }
701
702 case HIDIOCGSTRING:
703 r = hiddev_ioctl_string(hiddev, cmd, user_arg);
704 break;
705
706 case HIDIOCINITREPORT:
707 usbhid_init_reports(hid);
708 hiddev->initialized = true;
709 r = 0;
710 break;
711
712 case HIDIOCGREPORT:
713 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
714 r = -EFAULT;
715 break;
716 }
717
718 if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT)
719 break;
720
721 report = hiddev_lookup_report(hid, &rinfo);
722 if (report == NULL)
723 break;
724
725 hid_hw_request(hid, report, HID_REQ_GET_REPORT);
726 hid_hw_wait(hid);
727
728 r = 0;
729 break;
730
731 case HIDIOCSREPORT:
732 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
733 r = -EFAULT;
734 break;
735 }
736
737 if (rinfo.report_type == HID_REPORT_TYPE_INPUT)
738 break;
739
740 report = hiddev_lookup_report(hid, &rinfo);
741 if (report == NULL)
742 break;
743
744 hid_hw_request(hid, report, HID_REQ_SET_REPORT);
745 hid_hw_wait(hid);
746
747 r = 0;
748 break;
749
750 case HIDIOCGREPORTINFO:
751 if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) {
752 r = -EFAULT;
753 break;
754 }
755
756 report = hiddev_lookup_report(hid, &rinfo);
757 if (report == NULL)
758 break;
759
760 rinfo.num_fields = report->maxfield;
761
762 r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ?
763 -EFAULT : 0;
764 break;
765
766 case HIDIOCGFIELDINFO:
767 if (copy_from_user(&finfo, user_arg, sizeof(finfo))) {
768 r = -EFAULT;
769 break;
770 }
771
772 rinfo.report_type = finfo.report_type;
773 rinfo.report_id = finfo.report_id;
774
775 report = hiddev_lookup_report(hid, &rinfo);
776 if (report == NULL)
777 break;
778
779 if (finfo.field_index >= report->maxfield)
780 break;
781 finfo.field_index = array_index_nospec(finfo.field_index,
782 report->maxfield);
783
784 field = report->field[finfo.field_index];
785 memset(&finfo, 0, sizeof(finfo));
786 finfo.report_type = rinfo.report_type;
787 finfo.report_id = rinfo.report_id;
788 finfo.field_index = field->report_count - 1;
789 finfo.maxusage = field->maxusage;
790 finfo.flags = field->flags;
791 finfo.physical = field->physical;
792 finfo.logical = field->logical;
793 finfo.application = field->application;
794 finfo.logical_minimum = field->logical_minimum;
795 finfo.logical_maximum = field->logical_maximum;
796 finfo.physical_minimum = field->physical_minimum;
797 finfo.physical_maximum = field->physical_maximum;
798 finfo.unit_exponent = field->unit_exponent;
799 finfo.unit = field->unit;
800
801 r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ?
802 -EFAULT : 0;
803 break;
804
805 case HIDIOCGUCODE:
806 /* fall through */
807 case HIDIOCGUSAGE:
808 case HIDIOCSUSAGE:
809 case HIDIOCGUSAGES:
810 case HIDIOCSUSAGES:
811 case HIDIOCGCOLLECTIONINDEX:
812 if (!hiddev->initialized) {
813 usbhid_init_reports(hid);
814 hiddev->initialized = true;
815 }
816 r = hiddev_ioctl_usage(hiddev, cmd, user_arg);
817 break;
818
819 case HIDIOCGCOLLECTIONINFO:
820 if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) {
821 r = -EFAULT;
822 break;
823 }
824
825 if (cinfo.index >= hid->maxcollection)
826 break;
827 cinfo.index = array_index_nospec(cinfo.index,
828 hid->maxcollection);
829
830 cinfo.type = hid->collection[cinfo.index].type;
831 cinfo.usage = hid->collection[cinfo.index].usage;
832 cinfo.level = hid->collection[cinfo.index].level;
833
834 r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ?
835 -EFAULT : 0;
836 break;
837
838 default:
839 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ)
840 break;
841
842 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) {
843 int len = strlen(hid->name) + 1;
844 if (len > _IOC_SIZE(cmd))
845 len = _IOC_SIZE(cmd);
846 r = copy_to_user(user_arg, hid->name, len) ?
847 -EFAULT : len;
848 break;
849 }
850
851 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) {
852 int len = strlen(hid->phys) + 1;
853 if (len > _IOC_SIZE(cmd))
854 len = _IOC_SIZE(cmd);
855 r = copy_to_user(user_arg, hid->phys, len) ?
856 -EFAULT : len;
857 break;
858 }
859 }
860
861 ret_unlock:
862 mutex_unlock(&hiddev->existancelock);
863 return r;
864 }
865
866 #ifdef CONFIG_COMPAT
867 static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
868 {
869 return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
870 }
871 #endif
872
873 static const struct file_operations hiddev_fops = {
874 .owner = THIS_MODULE,
875 .read = hiddev_read,
876 .write = hiddev_write,
877 .poll = hiddev_poll,
878 .open = hiddev_open,
879 .release = hiddev_release,
880 .unlocked_ioctl = hiddev_ioctl,
881 .fasync = hiddev_fasync,
882 #ifdef CONFIG_COMPAT
883 .compat_ioctl = hiddev_compat_ioctl,
884 #endif
885 .llseek = noop_llseek,
886 };
887
888 static char *hiddev_devnode(struct device *dev, umode_t *mode)
889 {
890 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
891 }
892
893 static struct usb_class_driver hiddev_class = {
894 .name = "hiddev%d",
895 .devnode = hiddev_devnode,
896 .fops = &hiddev_fops,
897 .minor_base = HIDDEV_MINOR_BASE,
898 };
899
900 /*
901 * This is where hid.c calls us to connect a hid device to the hiddev driver
902 */
903 int hiddev_connect(struct hid_device *hid, unsigned int force)
904 {
905 struct hiddev *hiddev;
906 struct usbhid_device *usbhid = hid->driver_data;
907 int retval;
908
909 if (!force) {
910 unsigned int i;
911 for (i = 0; i < hid->maxcollection; i++)
912 if (hid->collection[i].type ==
913 HID_COLLECTION_APPLICATION &&
914 !IS_INPUT_APPLICATION(hid->collection[i].usage))
915 break;
916
917 if (i == hid->maxcollection)
918 return -1;
919 }
920
921 if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
922 return -1;
923
924 init_waitqueue_head(&hiddev->wait);
925 INIT_LIST_HEAD(&hiddev->list);
926 spin_lock_init(&hiddev->list_lock);
927 mutex_init(&hiddev->existancelock);
928 hid->hiddev = hiddev;
929 hiddev->hid = hid;
930 hiddev->exist = 1;
931 retval = usb_register_dev(usbhid->intf, &hiddev_class);
932 if (retval) {
933 hid_err(hid, "Not able to get a minor for this device\n");
934 hid->hiddev = NULL;
935 kfree(hiddev);
936 return -1;
937 }
938
939 /*
940 * If HID_QUIRK_NO_INIT_REPORTS is set, make sure we don't initialize
941 * the reports.
942 */
943 hiddev->initialized = hid->quirks & HID_QUIRK_NO_INIT_REPORTS;
944
945 hiddev->minor = usbhid->intf->minor;
946
947 return 0;
948 }
949
950 /*
951 * This is where hid.c calls us to disconnect a hiddev device from the
952 * corresponding hid device (usually because the usb device has disconnected)
953 */
954 static struct usb_class_driver hiddev_class;
955 void hiddev_disconnect(struct hid_device *hid)
956 {
957 struct hiddev *hiddev = hid->hiddev;
958 struct usbhid_device *usbhid = hid->driver_data;
959
960 usb_deregister_dev(usbhid->intf, &hiddev_class);
961
962 mutex_lock(&hiddev->existancelock);
963 hiddev->exist = 0;
964
965 if (hiddev->open) {
966 mutex_unlock(&hiddev->existancelock);
967 hid_hw_close(hiddev->hid);
968 wake_up_interruptible(&hiddev->wait);
969 } else {
970 mutex_unlock(&hiddev->existancelock);
971 kfree(hiddev);
972 }
973 }