Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/poll.h> |
| 16 | #include <linux/slab.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/input.h> |
| 21 | #include <linux/usb.h> |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 22 | #include <linux/hid.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/hiddev.h> |
Philip Langdale | bb6c8d8 | 2007-10-14 12:03:58 +0200 | [diff] [blame] | 24 | #include <linux/compat.h> |
Havard Skinnemoen | d4f0e4da | 2012-04-26 11:16:00 -0700 | [diff] [blame] | 25 | #include <linux/vmalloc.h> |
Gustavo A. R. Silva | 4f65245 | 2018-06-29 17:08:44 -0500 | [diff] [blame] | 26 | #include <linux/nospec.h> |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 27 | #include "usbhid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 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 Kosina | affbb8c | 2009-08-20 12:04:14 +0200 | [diff] [blame] | 36 | #define HIDDEV_BUFFER_SIZE 2048 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct 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 Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 45 | struct list_head node; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 46 | struct mutex thread_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
| 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 | */ |
| 55 | static struct hid_report * |
| 56 | hiddev_lookup_report(struct hid_device *hid, struct hiddev_report_info *rinfo) |
| 57 | { |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 58 | unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK; |
| 59 | unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct hid_report_enum *report_enum; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 61 | struct hid_report *report; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct list_head *list; |
| 63 | |
| 64 | if (rinfo->report_type < HID_REPORT_TYPE_MIN || |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 65 | rinfo->report_type > HID_REPORT_TYPE_MAX) |
| 66 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 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 Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 76 | if (list_empty(&report_enum->report_list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return NULL; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 78 | |
| 79 | list = report_enum->report_list.next; |
| 80 | report = list_entry(list, struct hid_report, list); |
| 81 | rinfo->report_id = report->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | break; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | case HID_REPORT_ID_NEXT: |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 85 | report = report_enum->report_id_hash[rid]; |
| 86 | if (!report) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return NULL; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 88 | |
| 89 | list = report->list.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (list == &report_enum->report_list) |
| 91 | return NULL; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 92 | |
| 93 | report = list_entry(list, struct hid_report, list); |
| 94 | rinfo->report_id = report->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | break; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 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 | */ |
| 108 | static struct hid_field * |
| 109 | hiddev_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 Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 117 | uref->report_type > HID_REPORT_TYPE_MAX) |
| 118 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | report_enum = hid->report_enum + |
| 121 | (uref->report_type - HID_REPORT_TYPE_MIN); |
| 122 | |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 123 | list_for_each_entry(report, &report_enum->report_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 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 Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | static void hiddev_send_event(struct hid_device *hid, |
| 141 | struct hiddev_usage_ref *uref) |
| 142 | { |
| 143 | struct hiddev *hiddev = hid->hiddev; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 144 | struct hiddev_list *list; |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 145 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 147 | spin_lock_irqsave(&hiddev->list_lock, flags); |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 148 | list_for_each_entry(list, &hiddev->list, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (uref->field_index != HID_FIELD_INDEX_NONE || |
| 150 | (list->flags & HIDDEV_FLAG_REPORT) != 0) { |
| 151 | list->buffer[list->head] = *uref; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 152 | list->head = (list->head + 1) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | (HIDDEV_BUFFER_SIZE - 1); |
| 154 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 157 | spin_unlock_irqrestore(&hiddev->list_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 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 | */ |
| 166 | void hiddev_hid_event(struct hid_device *hid, struct hid_field *field, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 167 | struct hid_usage *usage, __s32 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
| 169 | unsigned type = field->report_type; |
| 170 | struct hiddev_usage_ref uref; |
| 171 | |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 172 | uref.report_type = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT : |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 174 | ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 175 | ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | 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 Kosina | 229695e | 2006-12-08 18:40:53 +0100 | [diff] [blame] | 184 | EXPORT_SYMBOL_GPL(hiddev_hid_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | void 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 Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 192 | uref.report_type = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT : |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 194 | ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 195 | ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | uref.report_id = report->id; |
| 197 | uref.field_index = HID_FIELD_INDEX_NONE; |
| 198 | |
| 199 | hiddev_send_event(hid, &uref); |
| 200 | } |
Jiri Kosina | aa8de2f | 2006-12-08 18:41:17 +0100 | [diff] [blame] | 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | /* |
| 203 | * fasync file op |
| 204 | */ |
| 205 | static int hiddev_fasync(int fd, struct file *file, int on) |
| 206 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | struct hiddev_list *list = file->private_data; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 208 | |
Jonathan Corbet | 60aa492 | 2009-02-01 14:52:56 -0700 | [diff] [blame] | 209 | return fasync_helper(fd, file, on, &list->fasync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
| 213 | /* |
| 214 | * release file op |
| 215 | */ |
| 216 | static int hiddev_release(struct inode * inode, struct file * file) |
| 217 | { |
| 218 | struct hiddev_list *list = file->private_data; |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 219 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 221 | spin_lock_irqsave(&list->hiddev->list_lock, flags); |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 222 | list_del(&list->node); |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 223 | spin_unlock_irqrestore(&list->hiddev->list_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Jiri Kosina | 6cb4b04 | 2011-05-20 10:50:13 +0200 | [diff] [blame] | 225 | mutex_lock(&list->hiddev->existancelock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (!--list->hiddev->open) { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 227 | if (list->hiddev->exist) { |
Dmitry Torokhov | d36b7d4 | 2017-06-06 23:59:31 -0700 | [diff] [blame] | 228 | hid_hw_close(list->hiddev->hid); |
Dmitry Torokhov | 9a83563 | 2017-06-06 23:59:32 -0700 | [diff] [blame] | 229 | hid_hw_power(list->hiddev->hid, PM_HINT_NORMAL); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 230 | } else { |
Dan Carpenter | 5c699d7 | 2011-05-26 11:49:16 +0300 | [diff] [blame] | 231 | mutex_unlock(&list->hiddev->existancelock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | kfree(list->hiddev); |
Havard Skinnemoen | d4f0e4da | 2012-04-26 11:16:00 -0700 | [diff] [blame] | 233 | vfree(list); |
Dan Carpenter | 5c699d7 | 2011-05-26 11:49:16 +0300 | [diff] [blame] | 234 | return 0; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jiri Kosina | 6cb4b04 | 2011-05-20 10:50:13 +0200 | [diff] [blame] | 238 | mutex_unlock(&list->hiddev->existancelock); |
Havard Skinnemoen | d4f0e4da | 2012-04-26 11:16:00 -0700 | [diff] [blame] | 239 | vfree(list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * open file op |
| 246 | */ |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 247 | static int hiddev_open(struct inode *inode, struct file *file) |
| 248 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | struct hiddev_list *list; |
Arnd Bergmann | bd25f4d | 2010-07-11 15:34:05 +0200 | [diff] [blame] | 250 | struct usb_interface *intf; |
Jiri Kosina | 9c9e54a | 2010-08-13 12:19:45 +0200 | [diff] [blame] | 251 | struct hid_device *hid; |
Arnd Bergmann | bd25f4d | 2010-07-11 15:34:05 +0200 | [diff] [blame] | 252 | struct hiddev *hiddev; |
| 253 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Guillaume Chazarain | 8fe294c | 2010-09-12 21:32:35 +0200 | [diff] [blame] | 255 | intf = usbhid_find_interface(iminor(inode)); |
Arnd Bergmann | bd25f4d | 2010-07-11 15:34:05 +0200 | [diff] [blame] | 256 | if (!intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return -ENODEV; |
Jiri Kosina | 9c9e54a | 2010-08-13 12:19:45 +0200 | [diff] [blame] | 258 | hid = usb_get_intfdata(intf); |
| 259 | hiddev = hid->hiddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Havard Skinnemoen | d4f0e4da | 2012-04-26 11:16:00 -0700 | [diff] [blame] | 261 | if (!(list = vzalloc(sizeof(struct hiddev_list)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return -ENOMEM; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 263 | mutex_init(&list->thread_lock); |
Arnd Bergmann | bd25f4d | 2010-07-11 15:34:05 +0200 | [diff] [blame] | 264 | list->hiddev = hiddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | file->private_data = list; |
| 266 | |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 267 | /* |
| 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 Torokhov | d36b7d4 | 2017-06-06 23:59:31 -0700 | [diff] [blame] | 273 | res = hid_hw_open(hiddev->hid); |
| 274 | if (res < 0) |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 275 | goto bail; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 276 | } |
| 277 | } else { |
| 278 | res = -ENODEV; |
| 279 | goto bail; |
| 280 | } |
| 281 | |
| 282 | spin_lock_irq(&list->hiddev->list_lock); |
Arnd Bergmann | bd25f4d | 2010-07-11 15:34:05 +0200 | [diff] [blame] | 283 | list_add_tail(&list->node, &hiddev->list); |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 284 | spin_unlock_irq(&list->hiddev->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Jiri Kosina | 6cb4b04 | 2011-05-20 10:50:13 +0200 | [diff] [blame] | 286 | mutex_lock(&hiddev->existancelock); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 287 | if (!list->hiddev->open++) |
| 288 | if (list->hiddev->exist) { |
Arnd Bergmann | bd25f4d | 2010-07-11 15:34:05 +0200 | [diff] [blame] | 289 | struct hid_device *hid = hiddev->hid; |
Dmitry Torokhov | 9a83563 | 2017-06-06 23:59:32 -0700 | [diff] [blame] | 290 | res = hid_hw_power(hid, PM_HINT_FULLON); |
| 291 | if (res < 0) |
Jiri Kosina | 6cb4b04 | 2011-05-20 10:50:13 +0200 | [diff] [blame] | 292 | goto bail_unlock; |
Dmitry Torokhov | d36b7d4 | 2017-06-06 23:59:31 -0700 | [diff] [blame] | 293 | res = hid_hw_open(hid); |
| 294 | if (res < 0) |
Dmitry Torokhov | 9a83563 | 2017-06-06 23:59:32 -0700 | [diff] [blame] | 295 | goto bail_normal_power; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 296 | } |
Jiri Kosina | 6cb4b04 | 2011-05-20 10:50:13 +0200 | [diff] [blame] | 297 | mutex_unlock(&hiddev->existancelock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return 0; |
Dmitry Torokhov | 9a83563 | 2017-06-06 23:59:32 -0700 | [diff] [blame] | 299 | bail_normal_power: |
| 300 | hid_hw_power(hid, PM_HINT_NORMAL); |
Jiri Kosina | 6cb4b04 | 2011-05-20 10:50:13 +0200 | [diff] [blame] | 301 | bail_unlock: |
| 302 | mutex_unlock(&hiddev->existancelock); |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 303 | bail: |
| 304 | file->private_data = NULL; |
Havard Skinnemoen | d4f0e4da | 2012-04-26 11:16:00 -0700 | [diff] [blame] | 305 | vfree(list); |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 306 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * "write" file op |
| 311 | */ |
| 312 | static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) |
| 313 | { |
| 314 | return -EINVAL; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * "read" file op |
| 319 | */ |
| 320 | static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) |
| 321 | { |
Johannes Weiner | 96fe2ab | 2009-03-10 22:44:01 +0100 | [diff] [blame] | 322 | DEFINE_WAIT(wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | struct hiddev_list *list = file->private_data; |
| 324 | int event_size; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 325 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ? |
| 328 | sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event); |
| 329 | |
| 330 | if (count < event_size) |
| 331 | return 0; |
| 332 | |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 333 | /* lock against other threads */ |
| 334 | retval = mutex_lock_interruptible(&list->thread_lock); |
| 335 | if (retval) |
| 336 | return -ERESTARTSYS; |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | while (retval == 0) { |
| 339 | if (list->head == list->tail) { |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 340 | prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | while (list->head == list->tail) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | if (signal_pending(current)) { |
| 344 | retval = -ERESTARTSYS; |
| 345 | break; |
| 346 | } |
| 347 | if (!list->hiddev->exist) { |
| 348 | retval = -EIO; |
| 349 | break; |
| 350 | } |
Jiri Kosina | 13f1962 | 2012-11-28 00:10:44 +0100 | [diff] [blame] | 351 | if (file->f_flags & O_NONBLOCK) { |
| 352 | retval = -EAGAIN; |
| 353 | break; |
| 354 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 355 | |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 356 | /* let O_NONBLOCK tasks run */ |
| 357 | mutex_unlock(&list->thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | schedule(); |
Peter Waechtler | 06268b2 | 2011-04-28 20:53:58 +0200 | [diff] [blame] | 359 | if (mutex_lock_interruptible(&list->thread_lock)) { |
| 360 | finish_wait(&list->hiddev->wait, &wait); |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 361 | return -EINTR; |
Peter Waechtler | 06268b2 | 2011-04-28 20:53:58 +0200 | [diff] [blame] | 362 | } |
Micon, David | 48d7055 | 2006-05-20 14:59:59 -0700 | [diff] [blame] | 363 | set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 365 | finish_wait(&list->hiddev->wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 369 | if (retval) { |
| 370 | mutex_unlock(&list->thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | return retval; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 375 | while (list->head != list->tail && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | retval + event_size <= count) { |
| 377 | if ((list->flags & HIDDEV_FLAG_UREF) == 0) { |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 378 | if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | struct hiddev_event event; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | event.hid = list->buffer[list->tail].usage_code; |
| 382 | event.value = list->buffer[list->tail].value; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 383 | if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) { |
| 384 | mutex_unlock(&list->thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return -EFAULT; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | retval += sizeof(struct hiddev_event); |
| 388 | } |
| 389 | } else { |
| 390 | if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE || |
| 391 | (list->flags & HIDDEV_FLAG_REPORT) != 0) { |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 392 | |
| 393 | if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) { |
| 394 | mutex_unlock(&list->thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return -EFAULT; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | retval += sizeof(struct hiddev_usage_ref); |
| 398 | } |
| 399 | } |
| 400 | list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1); |
| 401 | } |
| 402 | |
| 403 | } |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 404 | mutex_unlock(&list->thread_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | return retval; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * "poll" file op |
| 411 | * No kernel lock - fine |
| 412 | */ |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 413 | static __poll_t hiddev_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
| 415 | struct hiddev_list *list = file->private_data; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | poll_wait(file, &list->hiddev->wait, wait); |
| 418 | if (list->head != list->tail) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 419 | return EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (!list->hiddev->exist) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 421 | return EPOLLERR | EPOLLHUP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * "ioctl" file op |
| 427 | */ |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 428 | static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg) |
| 429 | { |
| 430 | struct hid_device *hid = hiddev->hid; |
| 431 | struct hiddev_report_info rinfo; |
| 432 | struct hiddev_usage_ref_multi *uref_multi = NULL; |
| 433 | struct hiddev_usage_ref *uref; |
| 434 | struct hid_report *report; |
| 435 | struct hid_field *field; |
| 436 | int i; |
| 437 | |
| 438 | uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL); |
| 439 | if (!uref_multi) |
| 440 | return -ENOMEM; |
| 441 | uref = &uref_multi->uref; |
| 442 | if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) { |
| 443 | if (copy_from_user(uref_multi, user_arg, |
| 444 | sizeof(*uref_multi))) |
| 445 | goto fault; |
| 446 | } else { |
| 447 | if (copy_from_user(uref, user_arg, sizeof(*uref))) |
| 448 | goto fault; |
| 449 | } |
| 450 | |
| 451 | switch (cmd) { |
| 452 | case HIDIOCGUCODE: |
| 453 | rinfo.report_type = uref->report_type; |
| 454 | rinfo.report_id = uref->report_id; |
| 455 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 456 | goto inval; |
| 457 | |
| 458 | if (uref->field_index >= report->maxfield) |
| 459 | goto inval; |
Gustavo A. R. Silva | 4f65245 | 2018-06-29 17:08:44 -0500 | [diff] [blame] | 460 | uref->field_index = array_index_nospec(uref->field_index, |
| 461 | report->maxfield); |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 462 | |
| 463 | field = report->field[uref->field_index]; |
| 464 | if (uref->usage_index >= field->maxusage) |
| 465 | goto inval; |
Gustavo A. R. Silva | 4f65245 | 2018-06-29 17:08:44 -0500 | [diff] [blame] | 466 | uref->usage_index = array_index_nospec(uref->usage_index, |
| 467 | field->maxusage); |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 468 | |
| 469 | uref->usage_code = field->usage[uref->usage_index].hid; |
| 470 | |
| 471 | if (copy_to_user(user_arg, uref, sizeof(*uref))) |
| 472 | goto fault; |
| 473 | |
Jiri Slaby | eb99108 | 2008-10-23 01:47:34 +0200 | [diff] [blame] | 474 | goto goodreturn; |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 475 | |
| 476 | default: |
| 477 | if (cmd != HIDIOCGUSAGE && |
| 478 | cmd != HIDIOCGUSAGES && |
| 479 | uref->report_type == HID_REPORT_TYPE_INPUT) |
| 480 | goto inval; |
| 481 | |
| 482 | if (uref->report_id == HID_REPORT_ID_UNKNOWN) { |
| 483 | field = hiddev_lookup_usage(hid, uref); |
| 484 | if (field == NULL) |
| 485 | goto inval; |
| 486 | } else { |
| 487 | rinfo.report_type = uref->report_type; |
| 488 | rinfo.report_id = uref->report_id; |
| 489 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 490 | goto inval; |
| 491 | |
| 492 | if (uref->field_index >= report->maxfield) |
| 493 | goto inval; |
Gustavo A. R. Silva | 4f65245 | 2018-06-29 17:08:44 -0500 | [diff] [blame] | 494 | uref->field_index = array_index_nospec(uref->field_index, |
| 495 | report->maxfield); |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 496 | |
| 497 | field = report->field[uref->field_index]; |
| 498 | |
| 499 | if (cmd == HIDIOCGCOLLECTIONINDEX) { |
| 500 | if (uref->usage_index >= field->maxusage) |
| 501 | goto inval; |
Breno Leitao | f112743 | 2018-10-19 17:01:33 -0300 | [diff] [blame] | 502 | uref->usage_index = |
| 503 | array_index_nospec(uref->usage_index, |
| 504 | field->maxusage); |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 505 | } else if (uref->usage_index >= field->report_count) |
| 506 | goto inval; |
Dan Carpenter | ac065bf | 2011-03-26 04:47:35 +0300 | [diff] [blame] | 507 | } |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 508 | |
Breno Leitao | f112743 | 2018-10-19 17:01:33 -0300 | [diff] [blame] | 509 | if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) { |
| 510 | if (uref_multi->num_values > HID_MAX_MULTI_USAGES || |
| 511 | uref->usage_index + uref_multi->num_values > |
| 512 | field->report_count) |
| 513 | goto inval; |
| 514 | |
| 515 | uref->usage_index = |
| 516 | array_index_nospec(uref->usage_index, |
| 517 | field->report_count - |
| 518 | uref_multi->num_values); |
| 519 | } |
Scott Bauer | 93a2001 | 2016-06-23 08:59:47 -0600 | [diff] [blame] | 520 | |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 521 | switch (cmd) { |
| 522 | case HIDIOCGUSAGE: |
| 523 | uref->value = field->value[uref->usage_index]; |
| 524 | if (copy_to_user(user_arg, uref, sizeof(*uref))) |
| 525 | goto fault; |
| 526 | goto goodreturn; |
| 527 | |
| 528 | case HIDIOCSUSAGE: |
| 529 | field->value[uref->usage_index] = uref->value; |
| 530 | goto goodreturn; |
| 531 | |
| 532 | case HIDIOCGCOLLECTIONINDEX: |
Jiri Slaby | 4859484 | 2009-06-19 23:24:11 +0200 | [diff] [blame] | 533 | i = field->usage[uref->usage_index].collection_index; |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 534 | kfree(uref_multi); |
Jiri Slaby | 4859484 | 2009-06-19 23:24:11 +0200 | [diff] [blame] | 535 | return i; |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 536 | case HIDIOCGUSAGES: |
| 537 | for (i = 0; i < uref_multi->num_values; i++) |
| 538 | uref_multi->values[i] = |
| 539 | field->value[uref->usage_index + i]; |
| 540 | if (copy_to_user(user_arg, uref_multi, |
| 541 | sizeof(*uref_multi))) |
| 542 | goto fault; |
| 543 | goto goodreturn; |
| 544 | case HIDIOCSUSAGES: |
| 545 | for (i = 0; i < uref_multi->num_values; i++) |
| 546 | field->value[uref->usage_index + i] = |
| 547 | uref_multi->values[i]; |
| 548 | goto goodreturn; |
| 549 | } |
| 550 | |
| 551 | goodreturn: |
| 552 | kfree(uref_multi); |
| 553 | return 0; |
| 554 | fault: |
| 555 | kfree(uref_multi); |
| 556 | return -EFAULT; |
| 557 | inval: |
| 558 | kfree(uref_multi); |
| 559 | return -EINVAL; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg) |
| 564 | { |
| 565 | struct hid_device *hid = hiddev->hid; |
| 566 | struct usb_device *dev = hid_to_usb_dev(hid); |
| 567 | int idx, len; |
| 568 | char *buf; |
| 569 | |
| 570 | if (get_user(idx, (int __user *)user_arg)) |
| 571 | return -EFAULT; |
| 572 | |
| 573 | if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL) |
| 574 | return -ENOMEM; |
| 575 | |
| 576 | if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) { |
| 577 | kfree(buf); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | if (copy_to_user(user_arg+sizeof(int), buf, len+1)) { |
| 582 | kfree(buf); |
| 583 | return -EFAULT; |
| 584 | } |
| 585 | |
| 586 | kfree(buf); |
| 587 | |
| 588 | return len; |
| 589 | } |
| 590 | |
Alan Cox | 7961df1 | 2008-05-26 11:25:20 +0200 | [diff] [blame] | 591 | static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
| 593 | struct hiddev_list *list = file->private_data; |
| 594 | struct hiddev *hiddev = list->hiddev; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 595 | struct hid_device *hid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | struct hiddev_collection_info cinfo; |
| 597 | struct hiddev_report_info rinfo; |
| 598 | struct hiddev_field_info finfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | struct hiddev_devinfo dinfo; |
| 600 | struct hid_report *report; |
| 601 | struct hid_field *field; |
| 602 | void __user *user_arg = (void __user *)arg; |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 603 | int i, r = -EINVAL; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 604 | |
Alan Cox | 7961df1 | 2008-05-26 11:25:20 +0200 | [diff] [blame] | 605 | /* Called without BKL by compat methods so no BKL taken */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 607 | mutex_lock(&hiddev->existancelock); |
| 608 | if (!hiddev->exist) { |
| 609 | r = -ENODEV; |
| 610 | goto ret_unlock; |
| 611 | } |
| 612 | |
| 613 | hid = hiddev->hid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
| 615 | switch (cmd) { |
| 616 | |
| 617 | case HIDIOCGVERSION: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 618 | r = put_user(HID_VERSION, (int __user *)arg) ? |
| 619 | -EFAULT : 0; |
| 620 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | case HIDIOCAPPLICATION: |
Tushar Behera | d339f61 | 2012-11-16 12:20:43 +0530 | [diff] [blame] | 623 | if (arg >= hid->maxapplication) |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 624 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | for (i = 0; i < hid->maxcollection; i++) |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 627 | if (hid->collection[i].type == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | HID_COLLECTION_APPLICATION && arg-- == 0) |
| 629 | break; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 630 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 631 | if (i < hid->maxcollection) |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 632 | r = hid->collection[i].usage; |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 633 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | case HIDIOCGDEVINFO: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 636 | { |
| 637 | struct usb_device *dev = hid_to_usb_dev(hid); |
| 638 | struct usbhid_device *usbhid = hid->driver_data; |
| 639 | |
Dan Carpenter | 9561f7f | 2011-09-23 09:21:13 +0300 | [diff] [blame] | 640 | memset(&dinfo, 0, sizeof(dinfo)); |
| 641 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 642 | dinfo.bustype = BUS_USB; |
| 643 | dinfo.busnum = dev->bus->busnum; |
| 644 | dinfo.devnum = dev->devnum; |
| 645 | dinfo.ifnum = usbhid->ifnum; |
| 646 | dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor); |
| 647 | dinfo.product = le16_to_cpu(dev->descriptor.idProduct); |
| 648 | dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice); |
| 649 | dinfo.num_applications = hid->maxapplication; |
| 650 | |
| 651 | r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ? |
| 652 | -EFAULT : 0; |
| 653 | break; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 654 | } |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | case HIDIOCGFLAG: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 657 | r = put_user(list->flags, (int __user *)arg) ? |
| 658 | -EFAULT : 0; |
| 659 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
| 661 | case HIDIOCSFLAG: |
| 662 | { |
| 663 | int newflags; |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 664 | |
| 665 | if (get_user(newflags, (int __user *)arg)) { |
| 666 | r = -EFAULT; |
| 667 | break; |
| 668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
| 670 | if ((newflags & ~HIDDEV_FLAGS) != 0 || |
| 671 | ((newflags & HIDDEV_FLAG_REPORT) != 0 && |
| 672 | (newflags & HIDDEV_FLAG_UREF) == 0)) |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 673 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | list->flags = newflags; |
| 676 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 677 | r = 0; |
| 678 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | case HIDIOCGSTRING: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 682 | r = hiddev_ioctl_string(hiddev, cmd, user_arg); |
| 683 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
| 685 | case HIDIOCINITREPORT: |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 686 | usbhid_init_reports(hid); |
Benjamin Tissoires | 9143059 | 2017-03-08 15:11:14 +0100 | [diff] [blame] | 687 | hiddev->initialized = true; |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 688 | r = 0; |
| 689 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
| 691 | case HIDIOCGREPORT: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 692 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) { |
| 693 | r = -EFAULT; |
| 694 | break; |
| 695 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT) |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 698 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 700 | report = hiddev_lookup_report(hid, &rinfo); |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 701 | if (report == NULL) |
| 702 | break; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 703 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 704 | hid_hw_request(hid, report, HID_REQ_GET_REPORT); |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 705 | hid_hw_wait(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 707 | r = 0; |
| 708 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
| 710 | case HIDIOCSREPORT: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 711 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) { |
| 712 | r = -EFAULT; |
| 713 | break; |
| 714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
| 716 | if (rinfo.report_type == HID_REPORT_TYPE_INPUT) |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 717 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 719 | report = hiddev_lookup_report(hid, &rinfo); |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 720 | if (report == NULL) |
| 721 | break; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 722 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 723 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 724 | hid_hw_wait(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 726 | r = 0; |
| 727 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
| 729 | case HIDIOCGREPORTINFO: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 730 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) { |
| 731 | r = -EFAULT; |
| 732 | break; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 733 | } |
| 734 | |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 735 | report = hiddev_lookup_report(hid, &rinfo); |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 736 | if (report == NULL) |
| 737 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
| 739 | rinfo.num_fields = report->maxfield; |
| 740 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 741 | r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ? |
| 742 | -EFAULT : 0; |
| 743 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
| 745 | case HIDIOCGFIELDINFO: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 746 | if (copy_from_user(&finfo, user_arg, sizeof(finfo))) { |
| 747 | r = -EFAULT; |
| 748 | break; |
| 749 | } |
| 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | rinfo.report_type = finfo.report_type; |
| 752 | rinfo.report_id = finfo.report_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 754 | report = hiddev_lookup_report(hid, &rinfo); |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 755 | if (report == NULL) |
| 756 | break; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 757 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 758 | if (finfo.field_index >= report->maxfield) |
| 759 | break; |
Gustavo A. R. Silva | 4f65245 | 2018-06-29 17:08:44 -0500 | [diff] [blame] | 760 | finfo.field_index = array_index_nospec(finfo.field_index, |
| 761 | report->maxfield); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
| 763 | field = report->field[finfo.field_index]; |
| 764 | memset(&finfo, 0, sizeof(finfo)); |
| 765 | finfo.report_type = rinfo.report_type; |
| 766 | finfo.report_id = rinfo.report_id; |
| 767 | finfo.field_index = field->report_count - 1; |
| 768 | finfo.maxusage = field->maxusage; |
| 769 | finfo.flags = field->flags; |
| 770 | finfo.physical = field->physical; |
| 771 | finfo.logical = field->logical; |
| 772 | finfo.application = field->application; |
| 773 | finfo.logical_minimum = field->logical_minimum; |
| 774 | finfo.logical_maximum = field->logical_maximum; |
| 775 | finfo.physical_minimum = field->physical_minimum; |
| 776 | finfo.physical_maximum = field->physical_maximum; |
| 777 | finfo.unit_exponent = field->unit_exponent; |
| 778 | finfo.unit = field->unit; |
| 779 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 780 | r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ? |
| 781 | -EFAULT : 0; |
| 782 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
| 784 | case HIDIOCGUCODE: |
Jean Delvare | cf2a299 | 2008-03-03 11:48:43 +0100 | [diff] [blame] | 785 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | case HIDIOCGUSAGE: |
| 787 | case HIDIOCSUSAGE: |
| 788 | case HIDIOCGUSAGES: |
| 789 | case HIDIOCSUSAGES: |
| 790 | case HIDIOCGCOLLECTIONINDEX: |
Benjamin Tissoires | 9143059 | 2017-03-08 15:11:14 +0100 | [diff] [blame] | 791 | if (!hiddev->initialized) { |
| 792 | usbhid_init_reports(hid); |
| 793 | hiddev->initialized = true; |
| 794 | } |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 795 | r = hiddev_ioctl_usage(hiddev, cmd, user_arg); |
| 796 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
| 798 | case HIDIOCGCOLLECTIONINFO: |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 799 | if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) { |
| 800 | r = -EFAULT; |
| 801 | break; |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 802 | } |
| 803 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 804 | if (cinfo.index >= hid->maxcollection) |
| 805 | break; |
Gustavo A. R. Silva | 4f65245 | 2018-06-29 17:08:44 -0500 | [diff] [blame] | 806 | cinfo.index = array_index_nospec(cinfo.index, |
| 807 | hid->maxcollection); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | cinfo.type = hid->collection[cinfo.index].type; |
| 810 | cinfo.usage = hid->collection[cinfo.index].usage; |
| 811 | cinfo.level = hid->collection[cinfo.index].level; |
| 812 | |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 813 | r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ? |
| 814 | -EFAULT : 0; |
| 815 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
| 817 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 819 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) { |
Daniel Mack | dd2ed48 | 2011-05-15 18:07:42 +0200 | [diff] [blame] | 822 | int len = strlen(hid->name) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | if (len > _IOC_SIZE(cmd)) |
| 824 | len = _IOC_SIZE(cmd); |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 825 | r = copy_to_user(user_arg, hid->name, len) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | -EFAULT : len; |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 827 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) { |
Daniel Mack | dd2ed48 | 2011-05-15 18:07:42 +0200 | [diff] [blame] | 831 | int len = strlen(hid->phys) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | if (len > _IOC_SIZE(cmd)) |
| 833 | len = _IOC_SIZE(cmd); |
Valentine Barshak | 1a8e8fa | 2010-12-06 17:51:41 +0300 | [diff] [blame] | 834 | r = copy_to_user(user_arg, hid->phys, len) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | -EFAULT : len; |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 836 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | } |
Valentine Barshak | 33d6eb5 | 2010-12-06 18:16:11 +0300 | [diff] [blame] | 839 | |
| 840 | ret_unlock: |
| 841 | mutex_unlock(&hiddev->existancelock); |
| 842 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Philip Langdale | bb6c8d8 | 2007-10-14 12:03:58 +0200 | [diff] [blame] | 845 | #ifdef CONFIG_COMPAT |
| 846 | static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 847 | { |
Jiri Kosina | 88af45b | 2008-05-27 11:36:40 +0200 | [diff] [blame] | 848 | return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
Philip Langdale | bb6c8d8 | 2007-10-14 12:03:58 +0200 | [diff] [blame] | 849 | } |
| 850 | #endif |
| 851 | |
Luiz Fernando N. Capitulino | 066202d | 2006-08-05 20:37:11 -0300 | [diff] [blame] | 852 | static const struct file_operations hiddev_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | .owner = THIS_MODULE, |
| 854 | .read = hiddev_read, |
| 855 | .write = hiddev_write, |
| 856 | .poll = hiddev_poll, |
| 857 | .open = hiddev_open, |
| 858 | .release = hiddev_release, |
Alan Cox | 7961df1 | 2008-05-26 11:25:20 +0200 | [diff] [blame] | 859 | .unlocked_ioctl = hiddev_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | .fasync = hiddev_fasync, |
Philip Langdale | bb6c8d8 | 2007-10-14 12:03:58 +0200 | [diff] [blame] | 861 | #ifdef CONFIG_COMPAT |
| 862 | .compat_ioctl = hiddev_compat_ioctl, |
| 863 | #endif |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 864 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | }; |
| 866 | |
Al Viro | 2c9ede5 | 2011-07-23 20:24:48 -0400 | [diff] [blame] | 867 | static char *hiddev_devnode(struct device *dev, umode_t *mode) |
Kay Sievers | f7a386c | 2009-04-30 15:23:42 +0200 | [diff] [blame] | 868 | { |
| 869 | return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev)); |
| 870 | } |
| 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | static struct usb_class_driver hiddev_class = { |
Greg Kroah-Hartman | d6e5bcf | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 873 | .name = "hiddev%d", |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 874 | .devnode = hiddev_devnode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | .fops = &hiddev_fops, |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 876 | .minor_base = HIDDEV_MINOR_BASE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | }; |
| 878 | |
| 879 | /* |
| 880 | * This is where hid.c calls us to connect a hid device to the hiddev driver |
| 881 | */ |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 882 | int hiddev_connect(struct hid_device *hid, unsigned int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
| 884 | struct hiddev *hiddev; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 885 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | int retval; |
| 887 | |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 888 | if (!force) { |
| 889 | unsigned int i; |
| 890 | for (i = 0; i < hid->maxcollection; i++) |
| 891 | if (hid->collection[i].type == |
| 892 | HID_COLLECTION_APPLICATION && |
| 893 | !IS_INPUT_APPLICATION(hid->collection[i].usage)) |
| 894 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 896 | if (i == hid->maxcollection) |
| 897 | return -1; |
| 898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Oliver Neukum | bbdb7da | 2006-01-06 20:54:29 +0100 | [diff] [blame] | 900 | if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 903 | init_waitqueue_head(&hiddev->wait); |
| 904 | INIT_LIST_HEAD(&hiddev->list); |
| 905 | spin_lock_init(&hiddev->list_lock); |
| 906 | mutex_init(&hiddev->existancelock); |
Jiri Kosina | 7605274 | 2009-01-07 13:25:36 +0100 | [diff] [blame] | 907 | hid->hiddev = hiddev; |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 908 | hiddev->hid = hid; |
| 909 | hiddev->exist = 1; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 910 | retval = usb_register_dev(usbhid->intf, &hiddev_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 912 | hid_err(hid, "Not able to get a minor for this device\n"); |
Jiri Kosina | 7605274 | 2009-01-07 13:25:36 +0100 | [diff] [blame] | 913 | hid->hiddev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | kfree(hiddev); |
| 915 | return -1; |
| 916 | } |
Benjamin Tissoires | 9143059 | 2017-03-08 15:11:14 +0100 | [diff] [blame] | 917 | |
| 918 | /* |
| 919 | * If HID_QUIRK_NO_INIT_REPORTS is set, make sure we don't initialize |
| 920 | * the reports. |
| 921 | */ |
| 922 | hiddev->initialized = hid->quirks & HID_QUIRK_NO_INIT_REPORTS; |
| 923 | |
Jaejoong Kim | 733aca9 | 2017-03-03 17:54:01 +0900 | [diff] [blame] | 924 | hiddev->minor = usbhid->intf->minor; |
| 925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | * This is where hid.c calls us to disconnect a hiddev device from the |
| 931 | * corresponding hid device (usually because the usb device has disconnected) |
| 932 | */ |
| 933 | static struct usb_class_driver hiddev_class; |
| 934 | void hiddev_disconnect(struct hid_device *hid) |
| 935 | { |
| 936 | struct hiddev *hiddev = hid->hiddev; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 937 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Ming Lei | ba18311 | 2012-01-12 17:42:22 +0800 | [diff] [blame] | 939 | usb_deregister_dev(usbhid->intf, &hiddev_class); |
| 940 | |
Oliver Neukum | 0790340 | 2008-12-16 10:55:15 +0100 | [diff] [blame] | 941 | mutex_lock(&hiddev->existancelock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | hiddev->exist = 0; |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | if (hiddev->open) { |
Jiri Kosina | 7f77897 | 2011-05-24 11:43:18 +0200 | [diff] [blame] | 945 | mutex_unlock(&hiddev->existancelock); |
Dmitry Torokhov | d36b7d4 | 2017-06-06 23:59:31 -0700 | [diff] [blame] | 946 | hid_hw_close(hiddev->hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | wake_up_interruptible(&hiddev->wait); |
| 948 | } else { |
Jiri Kosina | 7f77897 | 2011-05-24 11:43:18 +0200 | [diff] [blame] | 949 | mutex_unlock(&hiddev->existancelock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | kfree(hiddev); |
| 951 | } |
| 952 | } |