| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | * HID raw devices, giving access to raw HID events. | 
|  | 3 | * | 
|  | 4 | * In comparison to hiddev, this device does not process the | 
|  | 5 | * hid events at all (no parsing, no lookups). This lets applications | 
|  | 6 | * to work on raw hid events as they want to, and avoids a need to | 
|  | 7 | * use a transport-specific userspace libhid/libusb libraries. | 
|  | 8 | * | 
|  | 9 | *  Copyright (c) 2007 Jiri Kosina | 
|  | 10 | */ | 
|  | 11 |  | 
|  | 12 | /* | 
|  | 13 | * This program is free software; you can redistribute it and/or modify it | 
|  | 14 | * under the terms and conditions of the GNU General Public License, | 
|  | 15 | * version 2, as published by the Free Software Foundation. | 
|  | 16 | * | 
|  | 17 | * You should have received a copy of the GNU General Public License along with | 
|  | 18 | * this program; if not, write to the Free Software Foundation, Inc., | 
|  | 19 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 20 | */ | 
|  | 21 |  | 
|  | 22 | #include <linux/fs.h> | 
|  | 23 | #include <linux/module.h> | 
|  | 24 | #include <linux/errno.h> | 
|  | 25 | #include <linux/kernel.h> | 
|  | 26 | #include <linux/init.h> | 
|  | 27 | #include <linux/cdev.h> | 
|  | 28 | #include <linux/poll.h> | 
|  | 29 | #include <linux/device.h> | 
|  | 30 | #include <linux/major.h> | 
|  | 31 | #include <linux/hid.h> | 
|  | 32 | #include <linux/mutex.h> | 
|  | 33 |  | 
|  | 34 | #include <linux/hidraw.h> | 
|  | 35 |  | 
|  | 36 | static int hidraw_major; | 
|  | 37 | static struct cdev hidraw_cdev; | 
|  | 38 | static struct class *hidraw_class; | 
|  | 39 | static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES]; | 
|  | 40 | static DEFINE_SPINLOCK(minors_lock); | 
|  | 41 |  | 
|  | 42 | static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) | 
|  | 43 | { | 
|  | 44 | struct hidraw_list *list = file->private_data; | 
|  | 45 | int ret = 0, len; | 
|  | 46 | char *report; | 
|  | 47 | DECLARE_WAITQUEUE(wait, current); | 
|  | 48 |  | 
|  | 49 | while (ret == 0) { | 
|  | 50 |  | 
|  | 51 | mutex_lock(&list->read_mutex); | 
|  | 52 |  | 
|  | 53 | if (list->head == list->tail) { | 
|  | 54 | add_wait_queue(&list->hidraw->wait, &wait); | 
|  | 55 | set_current_state(TASK_INTERRUPTIBLE); | 
|  | 56 |  | 
|  | 57 | while (list->head == list->tail) { | 
|  | 58 | if (file->f_flags & O_NONBLOCK) { | 
|  | 59 | ret = -EAGAIN; | 
|  | 60 | break; | 
|  | 61 | } | 
|  | 62 | if (signal_pending(current)) { | 
|  | 63 | ret = -ERESTARTSYS; | 
|  | 64 | break; | 
|  | 65 | } | 
|  | 66 | if (!list->hidraw->exist) { | 
|  | 67 | ret = -EIO; | 
|  | 68 | break; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | /* allow O_NONBLOCK to work well from other threads */ | 
|  | 72 | mutex_unlock(&list->read_mutex); | 
|  | 73 | schedule(); | 
|  | 74 | mutex_lock(&list->read_mutex); | 
|  | 75 | set_current_state(TASK_INTERRUPTIBLE); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | set_current_state(TASK_RUNNING); | 
|  | 79 | remove_wait_queue(&list->hidraw->wait, &wait); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | if (ret) | 
|  | 83 | goto out; | 
|  | 84 |  | 
|  | 85 | report = list->buffer[list->tail].value; | 
|  | 86 | len = list->buffer[list->tail].len > count ? | 
|  | 87 | count : list->buffer[list->tail].len; | 
|  | 88 |  | 
|  | 89 | if (copy_to_user(buffer, list->buffer[list->tail].value, len)) { | 
|  | 90 | ret = -EFAULT; | 
|  | 91 | goto out; | 
|  | 92 | } | 
|  | 93 | ret += len; | 
|  | 94 |  | 
|  | 95 | kfree(list->buffer[list->tail].value); | 
|  | 96 | list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1); | 
|  | 97 | } | 
|  | 98 | out: | 
|  | 99 | mutex_unlock(&list->read_mutex); | 
|  | 100 | return ret; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | /* the first byte is expected to be a report number */ | 
|  | 104 | static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) | 
|  | 105 | { | 
|  | 106 | unsigned int minor = iminor(file->f_path.dentry->d_inode); | 
|  | 107 | struct hid_device *dev = hidraw_table[minor]->hid; | 
|  | 108 | __u8 *buf; | 
|  | 109 | int ret = 0; | 
|  | 110 |  | 
|  | 111 | if (!dev->hid_output_raw_report) | 
|  | 112 | return -ENODEV; | 
|  | 113 |  | 
|  | 114 | if (count > HID_MIN_BUFFER_SIZE) { | 
|  | 115 | printk(KERN_WARNING "hidraw: pid %d passed too large report\n", | 
| Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 116 | task_pid_nr(current)); | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 117 | return -EINVAL; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | if (count < 2) { | 
|  | 121 | printk(KERN_WARNING "hidraw: pid %d passed too short report\n", | 
| Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 122 | task_pid_nr(current)); | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 123 | return -EINVAL; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | buf = kmalloc(count * sizeof(__u8), GFP_KERNEL); | 
|  | 127 | if (!buf) | 
|  | 128 | return -ENOMEM; | 
|  | 129 |  | 
|  | 130 | if (copy_from_user(buf, buffer, count)) { | 
|  | 131 | ret = -EFAULT; | 
|  | 132 | goto out; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | ret = dev->hid_output_raw_report(dev, buf, count); | 
|  | 136 | out: | 
|  | 137 | kfree(buf); | 
|  | 138 | return ret; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | static unsigned int hidraw_poll(struct file *file, poll_table *wait) | 
|  | 142 | { | 
|  | 143 | struct hidraw_list *list = file->private_data; | 
|  | 144 |  | 
|  | 145 | poll_wait(file, &list->hidraw->wait, wait); | 
|  | 146 | if (list->head != list->tail) | 
|  | 147 | return POLLIN | POLLRDNORM; | 
|  | 148 | if (!list->hidraw->exist) | 
|  | 149 | return POLLERR | POLLHUP; | 
|  | 150 | return 0; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | static int hidraw_open(struct inode *inode, struct file *file) | 
|  | 154 | { | 
|  | 155 | unsigned int minor = iminor(inode); | 
|  | 156 | struct hidraw *dev; | 
|  | 157 | struct hidraw_list *list; | 
|  | 158 | int err = 0; | 
|  | 159 |  | 
|  | 160 | if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) { | 
|  | 161 | err = -ENOMEM; | 
|  | 162 | goto out; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | spin_lock(&minors_lock); | 
|  | 166 | if (!hidraw_table[minor]) { | 
|  | 167 | printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n", | 
|  | 168 | minor); | 
|  | 169 | kfree(list); | 
|  | 170 | err = -ENODEV; | 
|  | 171 | goto out_unlock; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | list->hidraw = hidraw_table[minor]; | 
|  | 175 | mutex_init(&list->read_mutex); | 
|  | 176 | list_add_tail(&list->node, &hidraw_table[minor]->list); | 
|  | 177 | file->private_data = list; | 
|  | 178 |  | 
|  | 179 | dev = hidraw_table[minor]; | 
|  | 180 | if (!dev->open++) | 
|  | 181 | dev->hid->hid_open(dev->hid); | 
|  | 182 |  | 
|  | 183 | out_unlock: | 
|  | 184 | spin_unlock(&minors_lock); | 
|  | 185 | out: | 
|  | 186 | return err; | 
|  | 187 |  | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | static int hidraw_release(struct inode * inode, struct file * file) | 
|  | 191 | { | 
|  | 192 | unsigned int minor = iminor(inode); | 
|  | 193 | struct hidraw *dev; | 
|  | 194 | struct hidraw_list *list = file->private_data; | 
|  | 195 |  | 
|  | 196 | if (!hidraw_table[minor]) { | 
|  | 197 | printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n", | 
|  | 198 | minor); | 
|  | 199 | return -ENODEV; | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | list_del(&list->node); | 
|  | 203 | dev = hidraw_table[minor]; | 
|  | 204 | if (!dev->open--) { | 
|  | 205 | if (list->hidraw->exist) | 
|  | 206 | dev->hid->hid_close(dev->hid); | 
|  | 207 | else | 
|  | 208 | kfree(list->hidraw); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | return 0; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | static int hidraw_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) | 
|  | 215 | { | 
|  | 216 | unsigned int minor = iminor(inode); | 
|  | 217 | struct hidraw *dev = hidraw_table[minor]; | 
|  | 218 | void __user *user_arg = (void __user*) arg; | 
|  | 219 |  | 
|  | 220 | switch (cmd) { | 
|  | 221 | case HIDIOCGRDESCSIZE: | 
|  | 222 | if (put_user(dev->hid->rsize, (int __user *)arg)) | 
|  | 223 | return -EFAULT; | 
|  | 224 | return 0; | 
|  | 225 |  | 
|  | 226 | case HIDIOCGRDESC: | 
|  | 227 | { | 
|  | 228 | __u32 len; | 
|  | 229 |  | 
|  | 230 | if (get_user(len, (int __user *)arg)) | 
|  | 231 | return -EFAULT; | 
| Jiri Kosina | 57d292b | 2007-10-15 15:17:41 +0200 | [diff] [blame] | 232 |  | 
|  | 233 | if (len > HID_MAX_DESCRIPTOR_SIZE - 1) | 
|  | 234 | return -EINVAL; | 
|  | 235 |  | 
|  | 236 | if (copy_to_user(user_arg + offsetof( | 
|  | 237 | struct hidraw_report_descriptor, | 
|  | 238 | value[0]), | 
|  | 239 | dev->hid->rdesc, | 
|  | 240 | min(dev->hid->rsize, len))) | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 241 | return -EFAULT; | 
|  | 242 | return 0; | 
|  | 243 | } | 
|  | 244 | case HIDIOCGRAWINFO: | 
|  | 245 | { | 
|  | 246 | struct hidraw_devinfo dinfo; | 
|  | 247 |  | 
|  | 248 | dinfo.bustype = dev->hid->bus; | 
|  | 249 | dinfo.vendor = dev->hid->vendor; | 
|  | 250 | dinfo.product = dev->hid->product; | 
|  | 251 | if (copy_to_user(user_arg, &dinfo, sizeof(dinfo))) | 
|  | 252 | return -EFAULT; | 
|  | 253 |  | 
|  | 254 | return 0; | 
|  | 255 | } | 
|  | 256 | default: | 
|  | 257 | printk(KERN_EMERG "hidraw: unsupported ioctl() %x\n", | 
|  | 258 | cmd); | 
|  | 259 | } | 
|  | 260 | return -EINVAL; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | static const struct file_operations hidraw_ops = { | 
|  | 264 | .owner =        THIS_MODULE, | 
|  | 265 | .read =         hidraw_read, | 
|  | 266 | .write =        hidraw_write, | 
|  | 267 | .poll =         hidraw_poll, | 
|  | 268 | .open =         hidraw_open, | 
|  | 269 | .release =      hidraw_release, | 
|  | 270 | .ioctl =        hidraw_ioctl, | 
|  | 271 | }; | 
|  | 272 |  | 
|  | 273 | void hidraw_report_event(struct hid_device *hid, u8 *data, int len) | 
|  | 274 | { | 
|  | 275 | struct hidraw *dev = hid->hidraw; | 
|  | 276 | struct hidraw_list *list; | 
|  | 277 |  | 
|  | 278 | list_for_each_entry(list, &dev->list, node) { | 
|  | 279 | list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC); | 
|  | 280 | list->buffer[list->head].len = len; | 
|  | 281 | list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1); | 
|  | 282 | kill_fasync(&list->fasync, SIGIO, POLL_IN); | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | wake_up_interruptible(&dev->wait); | 
|  | 286 | } | 
|  | 287 | EXPORT_SYMBOL_GPL(hidraw_report_event); | 
|  | 288 |  | 
|  | 289 | int hidraw_connect(struct hid_device *hid) | 
|  | 290 | { | 
| Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 291 | int minor, result; | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 292 | struct hidraw *dev; | 
|  | 293 |  | 
|  | 294 | /* TODO currently we accept any HID device. This should later | 
|  | 295 | * probably be fixed to accept only those devices which provide | 
|  | 296 | * non-input applications | 
|  | 297 | */ | 
|  | 298 |  | 
| Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 299 | dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL); | 
|  | 300 | if (!dev) | 
|  | 301 | return -ENOMEM; | 
|  | 302 |  | 
|  | 303 | result = -EINVAL; | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 304 |  | 
|  | 305 | spin_lock(&minors_lock); | 
|  | 306 |  | 
|  | 307 | for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) { | 
|  | 308 | if (hidraw_table[minor]) | 
|  | 309 | continue; | 
|  | 310 | hidraw_table[minor] = dev; | 
|  | 311 | result = 0; | 
|  | 312 | break; | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | spin_unlock(&minors_lock); | 
|  | 316 |  | 
| Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 317 | if (result) { | 
|  | 318 | kfree(dev); | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 319 | goto out; | 
| Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 320 | } | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 321 |  | 
|  | 322 | dev->dev = device_create(hidraw_class, NULL, MKDEV(hidraw_major, minor), | 
|  | 323 | "%s%d", "hidraw", minor); | 
|  | 324 |  | 
|  | 325 | if (IS_ERR(dev->dev)) { | 
|  | 326 | spin_lock(&minors_lock); | 
|  | 327 | hidraw_table[minor] = NULL; | 
|  | 328 | spin_unlock(&minors_lock); | 
|  | 329 | result = PTR_ERR(dev->dev); | 
| Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 330 | kfree(dev); | 
| Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 331 | goto out; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | init_waitqueue_head(&dev->wait); | 
|  | 335 | INIT_LIST_HEAD(&dev->list); | 
|  | 336 |  | 
|  | 337 | dev->hid = hid; | 
|  | 338 | dev->minor = minor; | 
|  | 339 |  | 
|  | 340 | dev->exist = 1; | 
|  | 341 | hid->hidraw = dev; | 
|  | 342 |  | 
|  | 343 | out: | 
|  | 344 | return result; | 
|  | 345 |  | 
|  | 346 | } | 
|  | 347 | EXPORT_SYMBOL_GPL(hidraw_connect); | 
|  | 348 |  | 
|  | 349 | void hidraw_disconnect(struct hid_device *hid) | 
|  | 350 | { | 
|  | 351 | struct hidraw *hidraw = hid->hidraw; | 
|  | 352 |  | 
|  | 353 | hidraw->exist = 0; | 
|  | 354 |  | 
|  | 355 | spin_lock(&minors_lock); | 
|  | 356 | hidraw_table[hidraw->minor] = NULL; | 
|  | 357 | spin_unlock(&minors_lock); | 
|  | 358 |  | 
|  | 359 | device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor)); | 
|  | 360 |  | 
|  | 361 | if (hidraw->open) { | 
|  | 362 | hid->hid_close(hid); | 
|  | 363 | wake_up_interruptible(&hidraw->wait); | 
|  | 364 | } else { | 
|  | 365 | kfree(hidraw); | 
|  | 366 | } | 
|  | 367 | } | 
|  | 368 | EXPORT_SYMBOL_GPL(hidraw_disconnect); | 
|  | 369 |  | 
|  | 370 | int __init hidraw_init(void) | 
|  | 371 | { | 
|  | 372 | int result; | 
|  | 373 | dev_t dev_id; | 
|  | 374 |  | 
|  | 375 | result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR, | 
|  | 376 | HIDRAW_MAX_DEVICES, "hidraw"); | 
|  | 377 |  | 
|  | 378 | hidraw_major = MAJOR(dev_id); | 
|  | 379 |  | 
|  | 380 | if (result < 0) { | 
|  | 381 | printk(KERN_WARNING "hidraw: can't get major number\n"); | 
|  | 382 | result = 0; | 
|  | 383 | goto out; | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | hidraw_class = class_create(THIS_MODULE, "hidraw"); | 
|  | 387 | if (IS_ERR(hidraw_class)) { | 
|  | 388 | result = PTR_ERR(hidraw_class); | 
|  | 389 | unregister_chrdev(hidraw_major, "hidraw"); | 
|  | 390 | goto out; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | cdev_init(&hidraw_cdev, &hidraw_ops); | 
|  | 394 | cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES); | 
|  | 395 | out: | 
|  | 396 | return result; | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | void __exit hidraw_exit(void) | 
|  | 400 | { | 
|  | 401 | dev_t dev_id = MKDEV(hidraw_major, 0); | 
|  | 402 |  | 
|  | 403 | cdev_del(&hidraw_cdev); | 
|  | 404 | class_destroy(hidraw_class); | 
|  | 405 | unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES); | 
|  | 406 |  | 
|  | 407 | } |