Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/poll.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/smp_lock.h> |
| 33 | #include <linux/input.h> |
| 34 | #include <linux/usb.h> |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 35 | #include <linux/hid.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/hiddev.h> |
Jiri Kosina | dde5845 | 2006-12-08 18:40:44 +0100 | [diff] [blame] | 37 | #include "usbhid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 46 | #define HIDDEV_BUFFER_SIZE 64 |
| 47 | |
| 48 | struct hiddev { |
| 49 | int exist; |
| 50 | int open; |
| 51 | wait_queue_head_t wait; |
| 52 | struct hid_device *hid; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 53 | struct list_head list; |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 54 | spinlock_t list_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | struct hiddev_list { |
| 58 | struct hiddev_usage_ref buffer[HIDDEV_BUFFER_SIZE]; |
| 59 | int head; |
| 60 | int tail; |
| 61 | unsigned flags; |
| 62 | struct fasync_struct *fasync; |
| 63 | struct hiddev *hiddev; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 64 | struct list_head node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static struct hiddev *hiddev_table[HIDDEV_MINORS]; |
| 68 | |
| 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 | { |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 78 | unsigned int flags = rinfo->report_id & ~HID_REPORT_ID_MASK; |
| 79 | unsigned int rid = rinfo->report_id & HID_REPORT_ID_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | struct hid_report_enum *report_enum; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 81 | struct hid_report *report; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | struct list_head *list; |
| 83 | |
| 84 | if (rinfo->report_type < HID_REPORT_TYPE_MIN || |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 85 | rinfo->report_type > HID_REPORT_TYPE_MAX) |
| 86 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 96 | if (list_empty(&report_enum->report_list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return NULL; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 98 | |
| 99 | list = report_enum->report_list.next; |
| 100 | report = list_entry(list, struct hid_report, list); |
| 101 | rinfo->report_id = report->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | break; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | case HID_REPORT_ID_NEXT: |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 105 | report = report_enum->report_id_hash[rid]; |
| 106 | if (!report) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return NULL; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 108 | |
| 109 | list = report->list.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (list == &report_enum->report_list) |
| 111 | return NULL; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 112 | |
| 113 | report = list_entry(list, struct hid_report, list); |
| 114 | rinfo->report_id = report->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | break; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 || |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 137 | uref->report_type > HID_REPORT_TYPE_MAX) |
| 138 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | report_enum = hid->report_enum + |
| 141 | (uref->report_type - HID_REPORT_TYPE_MIN); |
| 142 | |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 143 | list_for_each_entry(report, &report_enum->report_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 164 | struct hiddev_list *list; |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 165 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 167 | spin_lock_irqsave(&hiddev->list_lock, flags); |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 168 | list_for_each_entry(list, &hiddev->list, node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | if (uref->field_index != HID_FIELD_INDEX_NONE || |
| 170 | (list->flags & HIDDEV_FLAG_REPORT) != 0) { |
| 171 | list->buffer[list->head] = *uref; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 172 | list->head = (list->head + 1) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | (HIDDEV_BUFFER_SIZE - 1); |
| 174 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 177 | spin_unlock_irqrestore(&hiddev->list_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 187 | struct hid_usage *usage, __s32 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
| 189 | unsigned type = field->report_type; |
| 190 | struct hiddev_usage_ref uref; |
| 191 | |
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 = 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 | } |
Jiri Kosina | 229695e | 2006-12-08 18:40:53 +0100 | [diff] [blame] | 204 | EXPORT_SYMBOL_GPL(hiddev_hid_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 212 | uref.report_type = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | (type == HID_INPUT_REPORT) ? HID_REPORT_TYPE_INPUT : |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 214 | ((type == HID_OUTPUT_REPORT) ? HID_REPORT_TYPE_OUTPUT : |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 215 | ((type == HID_FEATURE_REPORT) ? HID_REPORT_TYPE_FEATURE : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | uref.report_id = report->id; |
| 217 | uref.field_index = HID_FIELD_INDEX_NONE; |
| 218 | |
| 219 | hiddev_send_event(hid, &uref); |
| 220 | } |
Jiri Kosina | aa8de2f | 2006-12-08 18:41:17 +0100 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* |
| 223 | * fasync file op |
| 224 | */ |
| 225 | static int hiddev_fasync(int fd, struct file *file, int on) |
| 226 | { |
| 227 | int retval; |
| 228 | struct hiddev_list *list = file->private_data; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | retval = fasync_helper(fd, file, on, &list->fasync); |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return retval < 0 ? retval : 0; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /* |
| 237 | * release file op |
| 238 | */ |
| 239 | static int hiddev_release(struct inode * inode, struct file * file) |
| 240 | { |
| 241 | struct hiddev_list *list = file->private_data; |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 242 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | hiddev_fasync(-1, file, 0); |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 245 | |
| 246 | spin_lock_irqsave(&list->hiddev->list_lock, flags); |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 247 | list_del(&list->node); |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 248 | spin_unlock_irqrestore(&list->hiddev->list_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | if (!--list->hiddev->open) { |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 251 | if (list->hiddev->exist) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 252 | usbhid_close(list->hiddev->hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | else |
| 254 | kfree(list->hiddev); |
| 255 | } |
| 256 | |
| 257 | kfree(list); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * open file op |
| 264 | */ |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 265 | static int hiddev_open(struct inode *inode, struct file *file) |
| 266 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | struct hiddev_list *list; |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 268 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | int i = iminor(inode) - HIDDEV_MINOR_BASE; |
| 271 | |
| 272 | if (i >= HIDDEV_MINORS || !hiddev_table[i]) |
| 273 | return -ENODEV; |
| 274 | |
Oliver Neukum | bbdb7da | 2006-01-06 20:54:29 +0100 | [diff] [blame] | 275 | if (!(list = kzalloc(sizeof(struct hiddev_list), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | list->hiddev = hiddev_table[i]; |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 279 | |
| 280 | spin_lock_irqsave(&list->hiddev->list_lock, flags); |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 281 | list_add_tail(&list->node, &hiddev_table[i]->list); |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 282 | spin_unlock_irqrestore(&list->hiddev->list_lock, flags); |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | file->private_data = list; |
| 285 | |
| 286 | if (!list->hiddev->open++) |
| 287 | if (list->hiddev->exist) |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 288 | usbhid_open(hiddev_table[i]->hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * "write" file op |
| 295 | */ |
| 296 | static ssize_t hiddev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) |
| 297 | { |
| 298 | return -EINVAL; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * "read" file op |
| 303 | */ |
| 304 | static ssize_t hiddev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) |
| 305 | { |
| 306 | DECLARE_WAITQUEUE(wait, current); |
| 307 | struct hiddev_list *list = file->private_data; |
| 308 | int event_size; |
| 309 | int retval = 0; |
| 310 | |
| 311 | event_size = ((list->flags & HIDDEV_FLAG_UREF) != 0) ? |
| 312 | sizeof(struct hiddev_usage_ref) : sizeof(struct hiddev_event); |
| 313 | |
| 314 | if (count < event_size) |
| 315 | return 0; |
| 316 | |
| 317 | while (retval == 0) { |
| 318 | if (list->head == list->tail) { |
| 319 | add_wait_queue(&list->hiddev->wait, &wait); |
| 320 | set_current_state(TASK_INTERRUPTIBLE); |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | while (list->head == list->tail) { |
| 323 | if (file->f_flags & O_NONBLOCK) { |
| 324 | retval = -EAGAIN; |
| 325 | break; |
| 326 | } |
| 327 | if (signal_pending(current)) { |
| 328 | retval = -ERESTARTSYS; |
| 329 | break; |
| 330 | } |
| 331 | if (!list->hiddev->exist) { |
| 332 | retval = -EIO; |
| 333 | break; |
| 334 | } |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | schedule(); |
Micon, David | 48d7055 | 2006-05-20 14:59:59 -0700 | [diff] [blame] | 337 | set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | set_current_state(TASK_RUNNING); |
| 341 | remove_wait_queue(&list->hiddev->wait, &wait); |
| 342 | } |
| 343 | |
| 344 | if (retval) |
| 345 | return retval; |
| 346 | |
| 347 | |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 348 | while (list->head != list->tail && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | retval + event_size <= count) { |
| 350 | if ((list->flags & HIDDEV_FLAG_UREF) == 0) { |
| 351 | if (list->buffer[list->tail].field_index != |
| 352 | HID_FIELD_INDEX_NONE) { |
| 353 | struct hiddev_event event; |
| 354 | event.hid = list->buffer[list->tail].usage_code; |
| 355 | event.value = list->buffer[list->tail].value; |
| 356 | if (copy_to_user(buffer + retval, &event, sizeof(struct hiddev_event))) |
| 357 | return -EFAULT; |
| 358 | retval += sizeof(struct hiddev_event); |
| 359 | } |
| 360 | } else { |
| 361 | if (list->buffer[list->tail].field_index != HID_FIELD_INDEX_NONE || |
| 362 | (list->flags & HIDDEV_FLAG_REPORT) != 0) { |
| 363 | if (copy_to_user(buffer + retval, list->buffer + list->tail, sizeof(struct hiddev_usage_ref))) |
| 364 | return -EFAULT; |
| 365 | retval += sizeof(struct hiddev_usage_ref); |
| 366 | } |
| 367 | } |
| 368 | list->tail = (list->tail + 1) & (HIDDEV_BUFFER_SIZE - 1); |
| 369 | } |
| 370 | |
| 371 | } |
| 372 | |
| 373 | return retval; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * "poll" file op |
| 378 | * No kernel lock - fine |
| 379 | */ |
| 380 | static unsigned int hiddev_poll(struct file *file, poll_table *wait) |
| 381 | { |
| 382 | struct hiddev_list *list = file->private_data; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | poll_wait(file, &list->hiddev->wait, wait); |
| 385 | if (list->head != list->tail) |
| 386 | return POLLIN | POLLRDNORM; |
| 387 | if (!list->hiddev->exist) |
| 388 | return POLLERR | POLLHUP; |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * "ioctl" file op |
| 394 | */ |
| 395 | static int hiddev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) |
| 396 | { |
| 397 | struct hiddev_list *list = file->private_data; |
| 398 | struct hiddev *hiddev = list->hiddev; |
| 399 | struct hid_device *hid = hiddev->hid; |
Anssi Hannula | be82097 | 2007-01-19 19:28:17 +0200 | [diff] [blame] | 400 | struct usb_device *dev = hid_to_usb_dev(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | struct hiddev_collection_info cinfo; |
| 402 | struct hiddev_report_info rinfo; |
| 403 | struct hiddev_field_info finfo; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 404 | struct hiddev_usage_ref_multi *uref_multi = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | struct hiddev_usage_ref *uref; |
| 406 | struct hiddev_devinfo dinfo; |
| 407 | struct hid_report *report; |
| 408 | struct hid_field *field; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 409 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | void __user *user_arg = (void __user *)arg; |
| 411 | int i; |
| 412 | |
| 413 | if (!hiddev->exist) |
| 414 | return -EIO; |
| 415 | |
| 416 | switch (cmd) { |
| 417 | |
| 418 | case HIDIOCGVERSION: |
| 419 | return put_user(HID_VERSION, (int __user *)arg); |
| 420 | |
| 421 | case HIDIOCAPPLICATION: |
| 422 | if (arg < 0 || arg >= hid->maxapplication) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | for (i = 0; i < hid->maxcollection; i++) |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 426 | if (hid->collection[i].type == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | HID_COLLECTION_APPLICATION && arg-- == 0) |
| 428 | break; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (i == hid->maxcollection) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | return hid->collection[i].usage; |
| 434 | |
| 435 | case HIDIOCGDEVINFO: |
| 436 | dinfo.bustype = BUS_USB; |
| 437 | dinfo.busnum = dev->bus->busnum; |
| 438 | dinfo.devnum = dev->devnum; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 439 | dinfo.ifnum = usbhid->ifnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | dinfo.vendor = le16_to_cpu(dev->descriptor.idVendor); |
| 441 | dinfo.product = le16_to_cpu(dev->descriptor.idProduct); |
| 442 | dinfo.version = le16_to_cpu(dev->descriptor.bcdDevice); |
| 443 | dinfo.num_applications = hid->maxapplication; |
| 444 | if (copy_to_user(user_arg, &dinfo, sizeof(dinfo))) |
| 445 | return -EFAULT; |
| 446 | |
| 447 | return 0; |
| 448 | |
| 449 | case HIDIOCGFLAG: |
| 450 | if (put_user(list->flags, (int __user *)arg)) |
| 451 | return -EFAULT; |
| 452 | |
| 453 | return 0; |
| 454 | |
| 455 | case HIDIOCSFLAG: |
| 456 | { |
| 457 | int newflags; |
| 458 | if (get_user(newflags, (int __user *)arg)) |
| 459 | return -EFAULT; |
| 460 | |
| 461 | if ((newflags & ~HIDDEV_FLAGS) != 0 || |
| 462 | ((newflags & HIDDEV_FLAG_REPORT) != 0 && |
| 463 | (newflags & HIDDEV_FLAG_UREF) == 0)) |
| 464 | return -EINVAL; |
| 465 | |
| 466 | list->flags = newflags; |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | case HIDIOCGSTRING: |
| 472 | { |
| 473 | int idx, len; |
| 474 | char *buf; |
| 475 | |
| 476 | if (get_user(idx, (int __user *)arg)) |
| 477 | return -EFAULT; |
| 478 | |
| 479 | if ((buf = kmalloc(HID_STRING_SIZE, GFP_KERNEL)) == NULL) |
| 480 | return -ENOMEM; |
| 481 | |
| 482 | if ((len = usb_string(dev, idx, buf, HID_STRING_SIZE-1)) < 0) { |
| 483 | kfree(buf); |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | if (copy_to_user(user_arg+sizeof(int), buf, len+1)) { |
| 488 | kfree(buf); |
| 489 | return -EFAULT; |
| 490 | } |
| 491 | |
| 492 | kfree(buf); |
| 493 | |
| 494 | return len; |
| 495 | } |
| 496 | |
| 497 | case HIDIOCINITREPORT: |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 498 | usbhid_init_reports(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | |
| 502 | case HIDIOCGREPORT: |
| 503 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) |
| 504 | return -EFAULT; |
| 505 | |
| 506 | if (rinfo.report_type == HID_REPORT_TYPE_OUTPUT) |
| 507 | return -EINVAL; |
| 508 | |
| 509 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 510 | return -EINVAL; |
| 511 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 512 | usbhid_submit_report(hid, report, USB_DIR_IN); |
| 513 | usbhid_wait_io(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | |
| 517 | case HIDIOCSREPORT: |
| 518 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) |
| 519 | return -EFAULT; |
| 520 | |
| 521 | if (rinfo.report_type == HID_REPORT_TYPE_INPUT) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 525 | return -EINVAL; |
| 526 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 527 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 528 | usbhid_wait_io(hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | return 0; |
| 531 | |
| 532 | case HIDIOCGREPORTINFO: |
| 533 | if (copy_from_user(&rinfo, user_arg, sizeof(rinfo))) |
| 534 | return -EFAULT; |
| 535 | |
| 536 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 537 | return -EINVAL; |
| 538 | |
| 539 | rinfo.num_fields = report->maxfield; |
| 540 | |
| 541 | if (copy_to_user(user_arg, &rinfo, sizeof(rinfo))) |
| 542 | return -EFAULT; |
| 543 | |
| 544 | return 0; |
| 545 | |
| 546 | case HIDIOCGFIELDINFO: |
| 547 | if (copy_from_user(&finfo, user_arg, sizeof(finfo))) |
| 548 | return -EFAULT; |
| 549 | rinfo.report_type = finfo.report_type; |
| 550 | rinfo.report_id = finfo.report_id; |
| 551 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 552 | return -EINVAL; |
| 553 | |
| 554 | if (finfo.field_index >= report->maxfield) |
| 555 | return -EINVAL; |
| 556 | |
| 557 | field = report->field[finfo.field_index]; |
| 558 | memset(&finfo, 0, sizeof(finfo)); |
| 559 | finfo.report_type = rinfo.report_type; |
| 560 | finfo.report_id = rinfo.report_id; |
| 561 | finfo.field_index = field->report_count - 1; |
| 562 | finfo.maxusage = field->maxusage; |
| 563 | finfo.flags = field->flags; |
| 564 | finfo.physical = field->physical; |
| 565 | finfo.logical = field->logical; |
| 566 | finfo.application = field->application; |
| 567 | finfo.logical_minimum = field->logical_minimum; |
| 568 | finfo.logical_maximum = field->logical_maximum; |
| 569 | finfo.physical_minimum = field->physical_minimum; |
| 570 | finfo.physical_maximum = field->physical_maximum; |
| 571 | finfo.unit_exponent = field->unit_exponent; |
| 572 | finfo.unit = field->unit; |
| 573 | |
| 574 | if (copy_to_user(user_arg, &finfo, sizeof(finfo))) |
| 575 | return -EFAULT; |
| 576 | |
| 577 | return 0; |
| 578 | |
| 579 | case HIDIOCGUCODE: |
| 580 | uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL); |
| 581 | if (!uref_multi) |
| 582 | return -ENOMEM; |
| 583 | uref = &uref_multi->uref; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 584 | if (copy_from_user(uref, user_arg, sizeof(*uref))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | goto fault; |
| 586 | |
| 587 | rinfo.report_type = uref->report_type; |
| 588 | rinfo.report_id = uref->report_id; |
| 589 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 590 | goto inval; |
| 591 | |
| 592 | if (uref->field_index >= report->maxfield) |
| 593 | goto inval; |
| 594 | |
| 595 | field = report->field[uref->field_index]; |
| 596 | if (uref->usage_index >= field->maxusage) |
| 597 | goto inval; |
| 598 | |
| 599 | uref->usage_code = field->usage[uref->usage_index].hid; |
| 600 | |
| 601 | if (copy_to_user(user_arg, uref, sizeof(*uref))) |
| 602 | goto fault; |
| 603 | |
| 604 | kfree(uref_multi); |
| 605 | return 0; |
| 606 | |
| 607 | case HIDIOCGUSAGE: |
| 608 | case HIDIOCSUSAGE: |
| 609 | case HIDIOCGUSAGES: |
| 610 | case HIDIOCSUSAGES: |
| 611 | case HIDIOCGCOLLECTIONINDEX: |
| 612 | uref_multi = kmalloc(sizeof(struct hiddev_usage_ref_multi), GFP_KERNEL); |
| 613 | if (!uref_multi) |
| 614 | return -ENOMEM; |
| 615 | uref = &uref_multi->uref; |
| 616 | if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) { |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 617 | if (copy_from_user(uref_multi, user_arg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | sizeof(*uref_multi))) |
| 619 | goto fault; |
| 620 | } else { |
| 621 | if (copy_from_user(uref, user_arg, sizeof(*uref))) |
| 622 | goto fault; |
| 623 | } |
| 624 | |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 625 | if (cmd != HIDIOCGUSAGE && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | cmd != HIDIOCGUSAGES && |
| 627 | uref->report_type == HID_REPORT_TYPE_INPUT) |
| 628 | goto inval; |
| 629 | |
| 630 | if (uref->report_id == HID_REPORT_ID_UNKNOWN) { |
| 631 | field = hiddev_lookup_usage(hid, uref); |
| 632 | if (field == NULL) |
| 633 | goto inval; |
| 634 | } else { |
| 635 | rinfo.report_type = uref->report_type; |
| 636 | rinfo.report_id = uref->report_id; |
| 637 | if ((report = hiddev_lookup_report(hid, &rinfo)) == NULL) |
| 638 | goto inval; |
| 639 | |
| 640 | if (uref->field_index >= report->maxfield) |
| 641 | goto inval; |
| 642 | |
| 643 | field = report->field[uref->field_index]; |
| 644 | |
| 645 | if (cmd == HIDIOCGCOLLECTIONINDEX) { |
| 646 | if (uref->usage_index >= field->maxusage) |
| 647 | goto inval; |
| 648 | } else if (uref->usage_index >= field->report_count) |
| 649 | goto inval; |
| 650 | |
| 651 | else if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) && |
| 652 | (uref_multi->num_values > HID_MAX_MULTI_USAGES || |
Ben Collins | 6dea934 | 2006-01-31 01:31:13 -0500 | [diff] [blame] | 653 | uref->usage_index + uref_multi->num_values > field->report_count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | goto inval; |
| 655 | } |
| 656 | |
| 657 | switch (cmd) { |
| 658 | case HIDIOCGUSAGE: |
| 659 | uref->value = field->value[uref->usage_index]; |
| 660 | if (copy_to_user(user_arg, uref, sizeof(*uref))) |
| 661 | goto fault; |
| 662 | goto goodreturn; |
| 663 | |
| 664 | case HIDIOCSUSAGE: |
| 665 | field->value[uref->usage_index] = uref->value; |
| 666 | goto goodreturn; |
| 667 | |
| 668 | case HIDIOCGCOLLECTIONINDEX: |
| 669 | kfree(uref_multi); |
| 670 | return field->usage[uref->usage_index].collection_index; |
| 671 | case HIDIOCGUSAGES: |
| 672 | for (i = 0; i < uref_multi->num_values; i++) |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 673 | uref_multi->values[i] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | field->value[uref->usage_index + i]; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 675 | if (copy_to_user(user_arg, uref_multi, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | sizeof(*uref_multi))) |
| 677 | goto fault; |
| 678 | goto goodreturn; |
| 679 | case HIDIOCSUSAGES: |
| 680 | for (i = 0; i < uref_multi->num_values; i++) |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 681 | field->value[uref->usage_index + i] = |
| 682 | uref_multi->values[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | goto goodreturn; |
| 684 | } |
| 685 | |
| 686 | goodreturn: |
| 687 | kfree(uref_multi); |
| 688 | return 0; |
| 689 | fault: |
| 690 | kfree(uref_multi); |
| 691 | return -EFAULT; |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 692 | inval: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | kfree(uref_multi); |
| 694 | return -EINVAL; |
| 695 | |
| 696 | case HIDIOCGCOLLECTIONINFO: |
| 697 | if (copy_from_user(&cinfo, user_arg, sizeof(cinfo))) |
| 698 | return -EFAULT; |
| 699 | |
| 700 | if (cinfo.index >= hid->maxcollection) |
| 701 | return -EINVAL; |
| 702 | |
| 703 | cinfo.type = hid->collection[cinfo.index].type; |
| 704 | cinfo.usage = hid->collection[cinfo.index].usage; |
| 705 | cinfo.level = hid->collection[cinfo.index].level; |
| 706 | |
| 707 | if (copy_to_user(user_arg, &cinfo, sizeof(cinfo))) |
| 708 | return -EFAULT; |
| 709 | return 0; |
| 710 | |
| 711 | default: |
| 712 | |
| 713 | if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) |
| 714 | return -EINVAL; |
| 715 | |
| 716 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGNAME(0))) { |
| 717 | int len; |
| 718 | if (!hid->name) |
| 719 | return 0; |
| 720 | len = strlen(hid->name) + 1; |
| 721 | if (len > _IOC_SIZE(cmd)) |
| 722 | len = _IOC_SIZE(cmd); |
| 723 | return copy_to_user(user_arg, hid->name, len) ? |
| 724 | -EFAULT : len; |
| 725 | } |
| 726 | |
| 727 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGPHYS(0))) { |
| 728 | int len; |
| 729 | if (!hid->phys) |
| 730 | return 0; |
| 731 | len = strlen(hid->phys) + 1; |
| 732 | if (len > _IOC_SIZE(cmd)) |
| 733 | len = _IOC_SIZE(cmd); |
| 734 | return copy_to_user(user_arg, hid->phys, len) ? |
| 735 | -EFAULT : len; |
| 736 | } |
| 737 | } |
| 738 | return -EINVAL; |
| 739 | } |
| 740 | |
Luiz Fernando N. Capitulino | 066202d | 2006-08-05 20:37:11 -0300 | [diff] [blame] | 741 | static const struct file_operations hiddev_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | .owner = THIS_MODULE, |
| 743 | .read = hiddev_read, |
| 744 | .write = hiddev_write, |
| 745 | .poll = hiddev_poll, |
| 746 | .open = hiddev_open, |
| 747 | .release = hiddev_release, |
| 748 | .ioctl = hiddev_ioctl, |
| 749 | .fasync = hiddev_fasync, |
| 750 | }; |
| 751 | |
| 752 | static struct usb_class_driver hiddev_class = { |
Greg Kroah-Hartman | d6e5bcf | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 753 | .name = "hiddev%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | .fops = &hiddev_fops, |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 755 | .minor_base = HIDDEV_MINOR_BASE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | }; |
| 757 | |
| 758 | /* |
| 759 | * This is where hid.c calls us to connect a hid device to the hiddev driver |
| 760 | */ |
| 761 | int hiddev_connect(struct hid_device *hid) |
| 762 | { |
| 763 | struct hiddev *hiddev; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 764 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | int i; |
| 766 | int retval; |
| 767 | |
| 768 | for (i = 0; i < hid->maxcollection; i++) |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 769 | if (hid->collection[i].type == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | HID_COLLECTION_APPLICATION && |
| 771 | !IS_INPUT_APPLICATION(hid->collection[i].usage)) |
| 772 | break; |
| 773 | |
| 774 | if (i == hid->maxcollection && (hid->quirks & HID_QUIRK_HIDDEV) == 0) |
| 775 | return -1; |
| 776 | |
Oliver Neukum | bbdb7da | 2006-01-06 20:54:29 +0100 | [diff] [blame] | 777 | if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 780 | retval = usb_register_dev(usbhid->intf, &hiddev_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (retval) { |
Jiri Kosina | 58037eb | 2007-05-30 15:07:13 +0200 | [diff] [blame] | 782 | err_hid("Not able to get a minor for this device."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | kfree(hiddev); |
| 784 | return -1; |
| 785 | } |
| 786 | |
| 787 | init_waitqueue_head(&hiddev->wait); |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 788 | INIT_LIST_HEAD(&hiddev->list); |
Jiri Kosina | cdcb44e | 2007-05-10 08:45:56 +0200 | [diff] [blame] | 789 | spin_lock_init(&hiddev->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | hiddev->hid = hid; |
| 791 | hiddev->exist = 1; |
| 792 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 793 | hid->minor = usbhid->intf->minor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | hid->hiddev = hiddev; |
| 795 | |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 796 | hiddev_table[usbhid->intf->minor - HIDDEV_MINOR_BASE] = hiddev; |
Dmitry Torokhov | 826d598 | 2006-07-19 01:09:10 -0400 | [diff] [blame] | 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * This is where hid.c calls us to disconnect a hiddev device from the |
| 803 | * corresponding hid device (usually because the usb device has disconnected) |
| 804 | */ |
| 805 | static struct usb_class_driver hiddev_class; |
| 806 | void hiddev_disconnect(struct hid_device *hid) |
| 807 | { |
| 808 | struct hiddev *hiddev = hid->hiddev; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 809 | struct usbhid_device *usbhid = hid->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | hiddev->exist = 0; |
| 812 | |
| 813 | hiddev_table[hiddev->hid->minor - HIDDEV_MINOR_BASE] = NULL; |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 814 | usb_deregister_dev(usbhid->intf, &hiddev_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
| 816 | if (hiddev->open) { |
Jiri Kosina | 4916b3a | 2006-12-08 18:41:03 +0100 | [diff] [blame] | 817 | usbhid_close(hiddev->hid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | wake_up_interruptible(&hiddev->wait); |
| 819 | } else { |
| 820 | kfree(hiddev); |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* Currently this driver is a USB driver. It's not a conventional one in |
| 825 | * the sense that it doesn't probe at the USB level. Instead it waits to |
| 826 | * be connected by HID through the hiddev_connect / hiddev_disconnect |
| 827 | * routines. The reason to register as a USB device is to gain part of the |
| 828 | * minor number space from the USB major. |
| 829 | * |
| 830 | * In theory, should the HID code be generalized to more than one physical |
| 831 | * medium (say, IEEE 1384), this driver will probably need to register its |
| 832 | * own major number, and in doing so, no longer need to register with USB. |
| 833 | * At that point the probe routine and hiddev_driver struct below will no |
| 834 | * longer be useful. |
| 835 | */ |
| 836 | |
| 837 | |
| 838 | /* We never attach in this manner, and rely on HID to connect us. This |
| 839 | * is why there is no disconnect routine defined in the usb_driver either. |
| 840 | */ |
Dmitry Torokhov | 05f091ab | 2005-05-29 02:29:01 -0500 | [diff] [blame] | 841 | static int hiddev_usbd_probe(struct usb_interface *intf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | const struct usb_device_id *hiddev_info) |
| 843 | { |
| 844 | return -ENODEV; |
| 845 | } |
| 846 | |
| 847 | |
| 848 | static /* const */ struct usb_driver hiddev_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | .name = "hiddev", |
| 850 | .probe = hiddev_usbd_probe, |
| 851 | }; |
| 852 | |
| 853 | int __init hiddev_init(void) |
| 854 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | return usb_register(&hiddev_driver); |
| 856 | } |
| 857 | |
| 858 | void hiddev_exit(void) |
| 859 | { |
| 860 | usb_deregister(&hiddev_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | } |