]>
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 LT |
244 | |
245 | if (!--list->hiddev->open) { | |
0361a28d | 246 | if (list->hiddev->exist) { |
4916b3a5 | 247 | usbhid_close(list->hiddev->hid); |
0361a28d ON |
248 | usbhid_put_power(list->hiddev->hid); |
249 | } else { | |
1da177e4 | 250 | kfree(list->hiddev); |
0361a28d | 251 | } |
1da177e4 LT |
252 | } |
253 | ||
254 | kfree(list); | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
259 | /* | |
260 | * open file op | |
261 | */ | |
826d5982 DT |
262 | static int hiddev_open(struct inode *inode, struct file *file) |
263 | { | |
1da177e4 | 264 | struct hiddev_list *list; |
bd25f4dd | 265 | struct usb_interface *intf; |
9c9e54a8 | 266 | struct hid_device *hid; |
bd25f4dd AB |
267 | struct hiddev *hiddev; |
268 | int res; | |
1da177e4 | 269 | |
8fe294ca | 270 | intf = usbhid_find_interface(iminor(inode)); |
bd25f4dd | 271 | if (!intf) |
1da177e4 | 272 | return -ENODEV; |
9c9e54a8 JK |
273 | hid = usb_get_intfdata(intf); |
274 | hiddev = hid->hiddev; | |
1da177e4 | 275 | |
bbdb7daf | 276 | if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL))) |
1da177e4 | 277 | return -ENOMEM; |
07903407 | 278 | mutex_init(&list->thread_lock); |
bd25f4dd | 279 | list->hiddev = hiddev; |
1da177e4 LT |
280 | file->private_data = list; |
281 | ||
07903407 ON |
282 | /* |
283 | * no need for locking because the USB major number | |
284 | * is shared which usbcore guards against disconnect | |
285 | */ | |
286 | if (list->hiddev->exist) { | |
287 | if (!list->hiddev->open++) { | |
bd25f4dd | 288 | res = usbhid_open(hiddev->hid); |
07903407 ON |
289 | if (res < 0) { |
290 | res = -EIO; | |
291 | goto bail; | |
292 | } | |
293 | } | |
294 | } else { | |
295 | res = -ENODEV; | |
296 | goto bail; | |
297 | } | |
298 | ||
299 | spin_lock_irq(&list->hiddev->list_lock); | |
bd25f4dd | 300 | list_add_tail(&list->node, &hiddev->list); |
07903407 | 301 | spin_unlock_irq(&list->hiddev->list_lock); |
1da177e4 | 302 | |
0361a28d ON |
303 | if (!list->hiddev->open++) |
304 | if (list->hiddev->exist) { | |
bd25f4dd | 305 | struct hid_device *hid = hiddev->hid; |
0361a28d ON |
306 | res = usbhid_get_power(hid); |
307 | if (res < 0) { | |
308 | res = -EIO; | |
309 | goto bail; | |
310 | } | |
311 | usbhid_open(hid); | |
312 | } | |
1da177e4 | 313 | return 0; |
07903407 ON |
314 | bail: |
315 | file->private_data = NULL; | |
48e7a3c9 | 316 | kfree(list); |
07903407 | 317 | return res; |
1da177e4 LT |
318 | } |
319 | ||
320 | /* | |
321 | * "write" file op | |
322 | */ | |
323 | static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) | |
324 | { | |
325 | return -EINVAL; | |
326 | } | |
327 | ||
328 | /* | |
329 | * "read" file op | |
330 | */ | |
331 | static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) | |
332 | { | |
96fe2ab8 | 333 | DEFINE_WAIT(wait); |
1da177e4 LT |
334 | struct hiddev_list *list = file->private_data; |
335 | int event_size; | |
07903407 | 336 | int retval; |
1da177e4 LT |
337 | |
338 | event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ? | |
339 | sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event); | |
340 | ||
341 | if (count < event_size) | |
342 | return 0; | |
343 | ||
07903407 ON |
344 | /* lock against other threads */ |
345 | retval = mutex_lock_interruptible(&list->thread_lock); | |
346 | if (retval) | |
347 | return -ERESTARTSYS; | |
348 | ||
1da177e4 LT |
349 | while (retval == 0) { |
350 | if (list->head == list->tail) { | |
07903407 | 351 | prepare_to_wait(&list->hiddev->wait, &wait, TASK_INTERRUPTIBLE); |
05f091ab | 352 | |
1da177e4 LT |
353 | while (list->head == list->tail) { |
354 | if (file->f_flags & O_NONBLOCK) { | |
355 | retval = -EAGAIN; | |
356 | break; | |
357 | } | |
358 | if (signal_pending(current)) { | |
359 | retval = -ERESTARTSYS; | |
360 | break; | |
361 | } | |
362 | if (!list->hiddev->exist) { | |
363 | retval = -EIO; | |
364 | break; | |
365 | } | |
05f091ab | 366 | |
07903407 ON |
367 | /* let O_NONBLOCK tasks run */ |
368 | mutex_unlock(&list->thread_lock); | |
1da177e4 | 369 | schedule(); |
07903407 ON |
370 | if (mutex_lock_interruptible(&list->thread_lock)) |
371 | return -EINTR; | |
48d70552 | 372 | set_current_state(TASK_INTERRUPTIBLE); |
1da177e4 | 373 | } |
07903407 | 374 | finish_wait(&list->hiddev->wait, &wait); |
1da177e4 | 375 | |
1da177e4 LT |
376 | } |
377 | ||
07903407 ON |
378 | if (retval) { |
379 | mutex_unlock(&list->thread_lock); | |
1da177e4 | 380 | return retval; |
07903407 | 381 | } |
1da177e4 LT |
382 | |
383 | ||
05f091ab | 384 | while (list->head != list->tail && |
1da177e4 LT |
385 | retval + event_size <= count) { |
386 | if ((list->flags & HIDDEV_FLAG_UREF) == 0) { | |
07903407 | 387 | if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE) { |
1da177e4 | 388 | struct hiddev_event event; |
07903407 | 389 | |
1da177e4 LT |
390 | event.hid = list->buffer[list->tail].usage_code; |
391 | event.value = list->buffer[list->tail].value; | |
07903407 ON |
392 | if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) { |
393 | mutex_unlock(&list->thread_lock); | |
1da177e4 | 394 | return -EFAULT; |
07903407 | 395 | } |
1da177e4 LT |
396 | retval += sizeof(struct hiddev_event); |
397 | } | |
398 | } else { | |
399 | if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE || | |
400 | (list->flags & HIDDEV_FLAG_REPORT) != 0) { | |
07903407 ON |
401 | |
402 | if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) { | |
403 | mutex_unlock(&list->thread_lock); | |
1da177e4 | 404 | return -EFAULT; |
07903407 | 405 | } |
1da177e4 LT |
406 | retval += sizeof(struct hiddev_usage_ref); |
407 | } | |
408 | } | |
409 | list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1); | |
410 | } | |
411 | ||
412 | } | |
07903407 | 413 | mutex_unlock(&list->thread_lock); |
1da177e4 LT |
414 | |
415 | return retval; | |
416 | } | |
417 | ||
418 | /* | |
419 | * "poll" file op | |
420 | * No kernel lock - fine | |
421 | */ | |
422 | static unsigned int hiddev_poll(struct file *file, poll_table *wait) | |
423 | { | |
424 | struct hiddev_list *list = file->private_data; | |
826d5982 | 425 | |
1da177e4 LT |
426 | poll_wait(file, &list->hiddev->wait, wait); |
427 | if (list->head != list->tail) | |
428 | return POLLIN | POLLRDNORM; | |
429 | if (!list->hiddev->exist) | |
430 | return POLLERR | POLLHUP; | |
431 | return 0; | |
432 | } | |
433 | ||
434 | /* | |
435 | * "ioctl" file op | |
436 | */ | |
cf2a299e JD |
437 | static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg) |
438 | { | |
439 | struct hid_device *hid = hiddev->hid; | |
440 | struct hiddev_report_info rinfo; | |
441 | struct hiddev_usage_ref_multi *uref_multi = NULL; | |
442 | struct hiddev_usage_ref *uref; | |
443 | struct hid_report *report; | |
444 | struct hid_field *field; | |
445 | int i; | |
446 | ||
447 | uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL); | |
448 | if (!uref_multi) | |
449 | return -ENOMEM; | |
450 | uref = &uref_multi->uref; | |
451 | if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) { | |
452 | if (copy_from_user(uref_multi, user_arg, | |
453 | sizeof(*uref_multi))) | |
454 | goto fault; | |
455 | } else { | |
456 | if (copy_from_user(uref, user_arg, sizeof(*uref))) | |
457 | goto fault; | |
458 | } | |
459 | ||
460 | switch (cmd) { | |
461 | case HIDIOCGUCODE: | |
462 | rinfo.report_type = uref->report_type; | |
463 | rinfo.report_id = uref->report_id; | |
464 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) | |
465 | goto inval; | |
466 | ||
467 | if (uref->field_index >= report->maxfield) | |
468 | goto inval; | |
469 | ||
470 | field = report->field[uref->field_index]; | |
471 | if (uref->usage_index >= field->maxusage) | |
472 | goto inval; | |
473 | ||
474 | uref->usage_code = field->usage[uref->usage_index].hid; | |
475 | ||
476 | if (copy_to_user(user_arg, uref, sizeof(*uref))) | |
477 | goto fault; | |
478 | ||
eb991089 | 479 | goto goodreturn; |
cf2a299e JD |
480 | |
481 | default: | |
482 | if (cmd != HIDIOCGUSAGE && | |
483 | cmd != HIDIOCGUSAGES && | |
484 | uref->report_type == HID_REPORT_TYPE_INPUT) | |
485 | goto inval; | |
486 | ||
487 | if (uref->report_id == HID_REPORT_ID_UNKNOWN) { | |
488 | field = hiddev_lookup_usage(hid, uref); | |
489 | if (field == NULL) | |
490 | goto inval; | |
491 | } else { | |
492 | rinfo.report_type = uref->report_type; | |
493 | rinfo.report_id = uref->report_id; | |
494 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) | |
495 | goto inval; | |
496 | ||
497 | if (uref->field_index >= report->maxfield) | |
498 | goto inval; | |
499 | ||
500 | field = report->field[uref->field_index]; | |
501 | ||
502 | if (cmd == HIDIOCGCOLLECTIONINDEX) { | |
503 | if (uref->usage_index >= field->maxusage) | |
504 | goto inval; | |
505 | } else if (uref->usage_index >= field->report_count) | |
506 | goto inval; | |
507 | ||
508 | else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) && | |
509 | (uref_multi->num_values > HID_MAX_MULTI_USAGES || | |
510 | uref->usage_index + uref_multi->num_values > field->report_count)) | |
511 | goto inval; | |
ac065bf2 | 512 | } |
cf2a299e JD |
513 | |
514 | switch (cmd) { | |
515 | case HIDIOCGUSAGE: | |
516 | uref->value = field->value[uref->usage_index]; | |
517 | if (copy_to_user(user_arg, uref, sizeof(*uref))) | |
518 | goto fault; | |
519 | goto goodreturn; | |
520 | ||
521 | case HIDIOCSUSAGE: | |
522 | field->value[uref->usage_index] = uref->value; | |
523 | goto goodreturn; | |
524 | ||
525 | case HIDIOCGCOLLECTIONINDEX: | |
4859484b | 526 | i = field->usage[uref->usage_index].collection_index; |
cf2a299e | 527 | kfree(uref_multi); |
4859484b | 528 | return i; |
cf2a299e JD |
529 | case HIDIOCGUSAGES: |
530 | for (i = 0; i < uref_multi->num_values; i++) | |
531 | uref_multi->values[i] = | |
532 | field->value[uref->usage_index + i]; | |
533 | if (copy_to_user(user_arg, uref_multi, | |
534 | sizeof(*uref_multi))) | |
535 | goto fault; | |
536 | goto goodreturn; | |
537 | case HIDIOCSUSAGES: | |
538 | for (i = 0; i < uref_multi->num_values; i++) | |
539 | field->value[uref->usage_index + i] = | |
540 | uref_multi->values[i]; | |
541 | goto goodreturn; | |
542 | } | |
543 | ||
544 | goodreturn: | |
545 | kfree(uref_multi); | |
546 | return 0; | |
547 | fault: | |
548 | kfree(uref_multi); | |
549 | return -EFAULT; | |
550 | inval: | |
551 | kfree(uref_multi); | |
552 | return -EINVAL; | |
553 | } | |
554 | } | |
555 | ||
556 | static noinline int hiddev_ioctl_string(struct hiddev *hiddev, unsigned int cmd, void __user *user_arg) | |
557 | { | |
558 | struct hid_device *hid = hiddev->hid; | |
559 | struct usb_device *dev = hid_to_usb_dev(hid); | |
560 | int idx, len; | |
561 | char *buf; | |
562 | ||
563 | if (get_user(idx, (int __user *)user_arg)) | |
564 | return -EFAULT; | |
565 | ||
566 | if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL) | |
567 | return -ENOMEM; | |
568 | ||
569 | if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) { | |
570 | kfree(buf); | |
571 | return -EINVAL; | |
572 | } | |
573 | ||
574 | if (copy_to_user(user_arg+sizeof(int), buf, len+1)) { | |
575 | kfree(buf); | |
576 | return -EFAULT; | |
577 | } | |
578 | ||
579 | kfree(buf); | |
580 | ||
581 | return len; | |
582 | } | |
583 | ||
7961df16 | 584 | static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
1da177e4 LT |
585 | { |
586 | struct hiddev_list *list = file->private_data; | |
587 | struct hiddev *hiddev = list->hiddev; | |
1a8e8fab | 588 | struct hid_device *hid; |
1da177e4 LT |
589 | struct hiddev_collection_info cinfo; |
590 | struct hiddev_report_info rinfo; | |
591 | struct hiddev_field_info finfo; | |
1da177e4 LT |
592 | struct hiddev_devinfo dinfo; |
593 | struct hid_report *report; | |
594 | struct hid_field *field; | |
595 | void __user *user_arg = (void __user *)arg; | |
33d6eb57 | 596 | int i, r = -EINVAL; |
1a8e8fab | 597 | |
7961df16 | 598 | /* Called without BKL by compat methods so no BKL taken */ |
1da177e4 | 599 | |
33d6eb57 VB |
600 | mutex_lock(&hiddev->existancelock); |
601 | if (!hiddev->exist) { | |
602 | r = -ENODEV; | |
603 | goto ret_unlock; | |
604 | } | |
605 | ||
606 | hid = hiddev->hid; | |
1da177e4 LT |
607 | |
608 | switch (cmd) { | |
609 | ||
610 | case HIDIOCGVERSION: | |
33d6eb57 VB |
611 | r = put_user(HID_VERSION, (int __user *)arg) ? |
612 | -EFAULT : 0; | |
613 | break; | |
1da177e4 LT |
614 | |
615 | case HIDIOCAPPLICATION: | |
33d6eb57 VB |
616 | if (arg < 0 || arg >= hid->maxapplication) |
617 | break; | |
1da177e4 LT |
618 | |
619 | for (i = 0; i < hid->maxcollection; i++) | |
05f091ab | 620 | if (hid->collection[i].type == |
1da177e4 LT |
621 | HID_COLLECTION_APPLICATION && arg-- == 0) |
622 | break; | |
05f091ab | 623 | |
33d6eb57 | 624 | if (i < hid->maxcollection) |
1a8e8fab | 625 | r = hid->collection[i].usage; |
33d6eb57 | 626 | break; |
1da177e4 LT |
627 | |
628 | case HIDIOCGDEVINFO: | |
33d6eb57 VB |
629 | { |
630 | struct usb_device *dev = hid_to_usb_dev(hid); | |
631 | struct usbhid_device *usbhid = hid->driver_data; | |
632 | ||
633 | dinfo.bustype = BUS_USB; | |
634 | dinfo.busnum = dev->bus->busnum; | |
635 | dinfo.devnum = dev->devnum; | |
636 | dinfo.ifnum = usbhid->ifnum; | |
637 | dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor); | |
638 | dinfo.product = le16_to_cpu(dev->descriptor.idProduct); | |
639 | dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice); | |
640 | dinfo.num_applications = hid->maxapplication; | |
641 | ||
642 | r = copy_to_user(user_arg, &dinfo, sizeof(dinfo)) ? | |
643 | -EFAULT : 0; | |
644 | break; | |
1a8e8fab VB |
645 | } |
646 | ||
1da177e4 | 647 | case HIDIOCGFLAG: |
33d6eb57 VB |
648 | r = put_user(list->flags, (int __user *)arg) ? |
649 | -EFAULT : 0; | |
650 | break; | |
1da177e4 LT |
651 | |
652 | case HIDIOCSFLAG: | |
653 | { | |
654 | int newflags; | |
33d6eb57 VB |
655 | |
656 | if (get_user(newflags, (int __user *)arg)) { | |
657 | r = -EFAULT; | |
658 | break; | |
659 | } | |
1da177e4 LT |
660 | |
661 | if ((newflags & ~HIDDEV_FLAGS) != 0 || | |
662 | ((newflags & HIDDEV_FLAG_REPORT) != 0 && | |
663 | (newflags & HIDDEV_FLAG_UREF) == 0)) | |
33d6eb57 | 664 | break; |
1da177e4 LT |
665 | |
666 | list->flags = newflags; | |
667 | ||
33d6eb57 VB |
668 | r = 0; |
669 | break; | |
1da177e4 LT |
670 | } |
671 | ||
672 | case HIDIOCGSTRING: | |
33d6eb57 VB |
673 | r = hiddev_ioctl_string(hiddev, cmd, user_arg); |
674 | break; | |
1da177e4 LT |
675 | |
676 | case HIDIOCINITREPORT: | |
4916b3a5 | 677 | usbhid_init_reports(hid); |
33d6eb57 VB |
678 | r = 0; |
679 | break; | |
1da177e4 LT |
680 | |
681 | case HIDIOCGREPORT: | |
33d6eb57 VB |
682 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) { |
683 | r = -EFAULT; | |
684 | break; | |
685 | } | |
1da177e4 LT |
686 | |
687 | if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT) | |
33d6eb57 | 688 | break; |
1a8e8fab | 689 | |
1a8e8fab | 690 | report = hiddev_lookup_report(hid, &rinfo); |
33d6eb57 VB |
691 | if (report == NULL) |
692 | break; | |
1a8e8fab VB |
693 | |
694 | usbhid_submit_report(hid, report, USB_DIR_IN); | |
695 | usbhid_wait_io(hid); | |
1da177e4 | 696 | |
33d6eb57 VB |
697 | r = 0; |
698 | break; | |
1da177e4 LT |
699 | |
700 | case HIDIOCSREPORT: | |
33d6eb57 VB |
701 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) { |
702 | r = -EFAULT; | |
703 | break; | |
704 | } | |
1da177e4 LT |
705 | |
706 | if (rinfo.report_type == HID_REPORT_TYPE_INPUT) | |
33d6eb57 | 707 | break; |
1a8e8fab | 708 | |
1a8e8fab | 709 | report = hiddev_lookup_report(hid, &rinfo); |
33d6eb57 VB |
710 | if (report == NULL) |
711 | break; | |
1a8e8fab VB |
712 | |
713 | usbhid_submit_report(hid, report, USB_DIR_OUT); | |
714 | usbhid_wait_io(hid); | |
1da177e4 | 715 | |
33d6eb57 VB |
716 | r = 0; |
717 | break; | |
1da177e4 LT |
718 | |
719 | case HIDIOCGREPORTINFO: | |
33d6eb57 VB |
720 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) { |
721 | r = -EFAULT; | |
722 | break; | |
1a8e8fab VB |
723 | } |
724 | ||
1a8e8fab | 725 | report = hiddev_lookup_report(hid, &rinfo); |
33d6eb57 VB |
726 | if (report == NULL) |
727 | break; | |
1da177e4 LT |
728 | |
729 | rinfo.num_fields = report->maxfield; | |
1da177e4 | 730 | |
33d6eb57 VB |
731 | r = copy_to_user(user_arg, &rinfo, sizeof(rinfo)) ? |
732 | -EFAULT : 0; | |
733 | break; | |
1da177e4 LT |
734 | |
735 | case HIDIOCGFIELDINFO: | |
33d6eb57 VB |
736 | if (copy_from_user(&finfo, user_arg, sizeof(finfo))) { |
737 | r = -EFAULT; | |
738 | break; | |
739 | } | |
740 | ||
1da177e4 LT |
741 | rinfo.report_type = finfo.report_type; |
742 | rinfo.report_id = finfo.report_id; | |
1da177e4 | 743 | |
1a8e8fab | 744 | report = hiddev_lookup_report(hid, &rinfo); |
33d6eb57 VB |
745 | if (report == NULL) |
746 | break; | |
1a8e8fab | 747 | |
33d6eb57 VB |
748 | if (finfo.field_index >= report->maxfield) |
749 | break; | |
1da177e4 LT |
750 | |
751 | field = report->field[finfo.field_index]; | |
752 | memset(&finfo, 0, sizeof(finfo)); | |
753 | finfo.report_type = rinfo.report_type; | |
754 | finfo.report_id = rinfo.report_id; | |
755 | finfo.field_index = field->report_count - 1; | |
756 | finfo.maxusage = field->maxusage; | |
757 | finfo.flags = field->flags; | |
758 | finfo.physical = field->physical; | |
759 | finfo.logical = field->logical; | |
760 | finfo.application = field->application; | |
761 | finfo.logical_minimum = field->logical_minimum; | |
762 | finfo.logical_maximum = field->logical_maximum; | |
763 | finfo.physical_minimum = field->physical_minimum; | |
764 | finfo.physical_maximum = field->physical_maximum; | |
765 | finfo.unit_exponent = field->unit_exponent; | |
766 | finfo.unit = field->unit; | |
1da177e4 | 767 | |
33d6eb57 VB |
768 | r = copy_to_user(user_arg, &finfo, sizeof(finfo)) ? |
769 | -EFAULT : 0; | |
770 | break; | |
1da177e4 LT |
771 | |
772 | case HIDIOCGUCODE: | |
cf2a299e | 773 | /* fall through */ |
1da177e4 LT |
774 | case HIDIOCGUSAGE: |
775 | case HIDIOCSUSAGE: | |
776 | case HIDIOCGUSAGES: | |
777 | case HIDIOCSUSAGES: | |
778 | case HIDIOCGCOLLECTIONINDEX: | |
33d6eb57 VB |
779 | r = hiddev_ioctl_usage(hiddev, cmd, user_arg); |
780 | break; | |
1da177e4 LT |
781 | |
782 | case HIDIOCGCOLLECTIONINFO: | |
33d6eb57 VB |
783 | if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) { |
784 | r = -EFAULT; | |
785 | break; | |
1a8e8fab VB |
786 | } |
787 | ||
33d6eb57 VB |
788 | if (cinfo.index >= hid->maxcollection) |
789 | break; | |
1da177e4 LT |
790 | |
791 | cinfo.type = hid->collection[cinfo.index].type; | |
792 | cinfo.usage = hid->collection[cinfo.index].usage; | |
793 | cinfo.level = hid->collection[cinfo.index].level; | |
794 | ||
33d6eb57 VB |
795 | r = copy_to_user(user_arg, &cinfo, sizeof(cinfo)) ? |
796 | -EFAULT : 0; | |
797 | break; | |
1da177e4 LT |
798 | |
799 | default: | |
1da177e4 | 800 | if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) |
33d6eb57 | 801 | break; |
1da177e4 LT |
802 | |
803 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) { | |
804 | int len; | |
1a8e8fab | 805 | |
1a8e8fab VB |
806 | if (!hid->name) { |
807 | r = 0; | |
33d6eb57 | 808 | break; |
1a8e8fab VB |
809 | } |
810 | ||
1da177e4 LT |
811 | len = strlen(hid->name) + 1; |
812 | if (len > _IOC_SIZE(cmd)) | |
813 | len = _IOC_SIZE(cmd); | |
1a8e8fab | 814 | r = copy_to_user(user_arg, hid->name, len) ? |
1da177e4 | 815 | -EFAULT : len; |
33d6eb57 | 816 | break; |
1da177e4 LT |
817 | } |
818 | ||
819 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) { | |
820 | int len; | |
1a8e8fab | 821 | |
1a8e8fab VB |
822 | if (!hid->phys) { |
823 | r = 0; | |
33d6eb57 | 824 | break; |
1a8e8fab VB |
825 | } |
826 | ||
1da177e4 LT |
827 | len = strlen(hid->phys) + 1; |
828 | if (len > _IOC_SIZE(cmd)) | |
829 | len = _IOC_SIZE(cmd); | |
1a8e8fab | 830 | r = copy_to_user(user_arg, hid->phys, len) ? |
1da177e4 | 831 | -EFAULT : len; |
33d6eb57 | 832 | break; |
1da177e4 LT |
833 | } |
834 | } | |
33d6eb57 VB |
835 | |
836 | ret_unlock: | |
837 | mutex_unlock(&hiddev->existancelock); | |
838 | return r; | |
1da177e4 LT |
839 | } |
840 | ||
bb6c8d8f PL |
841 | #ifdef CONFIG_COMPAT |
842 | static long hiddev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
843 | { | |
88af45ba | 844 | return hiddev_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); |
bb6c8d8f PL |
845 | } |
846 | #endif | |
847 | ||
066202dd | 848 | static const struct file_operations hiddev_fops = { |
1da177e4 LT |
849 | .owner = THIS_MODULE, |
850 | .read = hiddev_read, | |
851 | .write = hiddev_write, | |
852 | .poll = hiddev_poll, | |
853 | .open = hiddev_open, | |
854 | .release = hiddev_release, | |
7961df16 | 855 | .unlocked_ioctl = hiddev_ioctl, |
1da177e4 | 856 | .fasync = hiddev_fasync, |
bb6c8d8f PL |
857 | #ifdef CONFIG_COMPAT |
858 | .compat_ioctl = hiddev_compat_ioctl, | |
859 | #endif | |
6038f373 | 860 | .llseek = noop_llseek, |
1da177e4 LT |
861 | }; |
862 | ||
e454cea2 | 863 | static char *hiddev_devnode(struct device *dev, mode_t *mode) |
f7a386c5 KS |
864 | { |
865 | return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev)); | |
866 | } | |
867 | ||
1da177e4 | 868 | static struct usb_class_driver hiddev_class = { |
d6e5bcf4 | 869 | .name = "hiddev%d", |
e454cea2 | 870 | .devnode = hiddev_devnode, |
1da177e4 | 871 | .fops = &hiddev_fops, |
05f091ab | 872 | .minor_base = HIDDEV_MINOR_BASE, |
1da177e4 LT |
873 | }; |
874 | ||
875 | /* | |
876 | * This is where hid.c calls us to connect a hid device to the hiddev driver | |
877 | */ | |
93c10132 | 878 | int hiddev_connect(struct hid_device *hid, unsigned int force) |
1da177e4 LT |
879 | { |
880 | struct hiddev *hiddev; | |
4916b3a5 | 881 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 LT |
882 | int retval; |
883 | ||
93c10132 JS |
884 | if (!force) { |
885 | unsigned int i; | |
886 | for (i = 0; i < hid->maxcollection; i++) | |
887 | if (hid->collection[i].type == | |
888 | HID_COLLECTION_APPLICATION && | |
889 | !IS_INPUT_APPLICATION(hid->collection[i].usage)) | |
890 | break; | |
1da177e4 | 891 | |
93c10132 JS |
892 | if (i == hid->maxcollection) |
893 | return -1; | |
894 | } | |
1da177e4 | 895 | |
bbdb7daf | 896 | if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL))) |
1da177e4 | 897 | return -1; |
1da177e4 | 898 | |
1da177e4 | 899 | init_waitqueue_head(&hiddev->wait); |
826d5982 | 900 | INIT_LIST_HEAD(&hiddev->list); |
cdcb44e8 | 901 | spin_lock_init(&hiddev->list_lock); |
07903407 | 902 | mutex_init(&hiddev->existancelock); |
76052749 | 903 | hid->hiddev = hiddev; |
1da177e4 LT |
904 | hiddev->hid = hid; |
905 | hiddev->exist = 1; | |
07903407 ON |
906 | retval = usb_register_dev(usbhid->intf, &hiddev_class); |
907 | if (retval) { | |
4291ee30 | 908 | hid_err(hid, "Not able to get a minor for this device\n"); |
76052749 | 909 | hid->hiddev = NULL; |
07903407 ON |
910 | kfree(hiddev); |
911 | return -1; | |
07903407 | 912 | } |
1da177e4 LT |
913 | return 0; |
914 | } | |
915 | ||
916 | /* | |
917 | * This is where hid.c calls us to disconnect a hiddev device from the | |
918 | * corresponding hid device (usually because the usb device has disconnected) | |
919 | */ | |
920 | static struct usb_class_driver hiddev_class; | |
921 | void hiddev_disconnect(struct hid_device *hid) | |
922 | { | |
923 | struct hiddev *hiddev = hid->hiddev; | |
4916b3a5 | 924 | struct usbhid_device *usbhid = hid->driver_data; |
1da177e4 | 925 | |
07903407 | 926 | mutex_lock(&hiddev->existancelock); |
1da177e4 | 927 | hiddev->exist = 0; |
07903407 | 928 | mutex_unlock(&hiddev->existancelock); |
1da177e4 | 929 | |
4916b3a5 | 930 | usb_deregister_dev(usbhid->intf, &hiddev_class); |
1da177e4 LT |
931 | |
932 | if (hiddev->open) { | |
4916b3a5 | 933 | usbhid_close(hiddev->hid); |
1da177e4 LT |
934 | wake_up_interruptible(&hiddev->wait); |
935 | } else { | |
936 | kfree(hiddev); | |
937 | } | |
938 | } |