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