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 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 24 | #include <linux/fs.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/cdev.h> |
| 30 | #include <linux/poll.h> |
| 31 | #include <linux/device.h> |
| 32 | #include <linux/major.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 34 | #include <linux/hid.h> |
| 35 | #include <linux/mutex.h> |
Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 36 | #include <linux/sched.h> |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 37 | |
| 38 | #include <linux/hidraw.h> |
| 39 | |
| 40 | static int hidraw_major; |
| 41 | static struct cdev hidraw_cdev; |
| 42 | static struct class *hidraw_class; |
| 43 | static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES]; |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 44 | static DEFINE_MUTEX(minors_lock); |
Ratan Nalumasu | 4fe9f8e | 2012-09-22 11:46:30 -0700 | [diff] [blame] | 45 | static void drop_ref(struct hidraw *hid, int exists_bit); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 46 | |
| 47 | static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
| 48 | { |
| 49 | struct hidraw_list *list = file->private_data; |
| 50 | int ret = 0, len; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 51 | DECLARE_WAITQUEUE(wait, current); |
| 52 | |
Jiri Kosina | b0e1495 | 2009-10-12 11:25:56 +0200 | [diff] [blame] | 53 | mutex_lock(&list->read_mutex); |
| 54 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 55 | while (ret == 0) { |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 56 | if (list->head == list->tail) { |
| 57 | add_wait_queue(&list->hidraw->wait, &wait); |
| 58 | set_current_state(TASK_INTERRUPTIBLE); |
| 59 | |
| 60 | while (list->head == list->tail) { |
| 61 | if (file->f_flags & O_NONBLOCK) { |
| 62 | ret = -EAGAIN; |
| 63 | break; |
| 64 | } |
| 65 | if (signal_pending(current)) { |
| 66 | ret = -ERESTARTSYS; |
| 67 | break; |
| 68 | } |
| 69 | if (!list->hidraw->exist) { |
| 70 | ret = -EIO; |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | /* allow O_NONBLOCK to work well from other threads */ |
| 75 | mutex_unlock(&list->read_mutex); |
| 76 | schedule(); |
| 77 | mutex_lock(&list->read_mutex); |
| 78 | set_current_state(TASK_INTERRUPTIBLE); |
| 79 | } |
| 80 | |
| 81 | set_current_state(TASK_RUNNING); |
| 82 | remove_wait_queue(&list->hidraw->wait, &wait); |
| 83 | } |
| 84 | |
| 85 | if (ret) |
| 86 | goto out; |
| 87 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 88 | len = list->buffer[list->tail].len > count ? |
| 89 | count : list->buffer[list->tail].len; |
| 90 | |
Jiri Kosina | b678724 | 2012-04-27 00:56:08 +0200 | [diff] [blame] | 91 | if (list->buffer[list->tail].value) { |
| 92 | if (copy_to_user(buffer, list->buffer[list->tail].value, len)) { |
| 93 | ret = -EFAULT; |
| 94 | goto out; |
| 95 | } |
| 96 | ret = len; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 97 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 98 | |
| 99 | kfree(list->buffer[list->tail].value); |
Matthieu CASTET | 4c7b417 | 2012-06-28 16:51:56 +0200 | [diff] [blame] | 100 | list->buffer[list->tail].value = NULL; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 101 | list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1); |
| 102 | } |
| 103 | out: |
| 104 | mutex_unlock(&list->read_mutex); |
| 105 | return ret; |
| 106 | } |
| 107 | |
Jiri Kosina | d2a1cfe | 2011-03-27 20:29:02 +0200 | [diff] [blame] | 108 | /* The first byte is expected to be a report number. |
| 109 | * This function is to be called with the minors_lock mutex held */ |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 110 | static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type) |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 111 | { |
| 112 | unsigned int minor = iminor(file->f_path.dentry->d_inode); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 113 | struct hid_device *dev; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 114 | __u8 *buf; |
| 115 | int ret = 0; |
| 116 | |
Ratan Nalumasu | 4fe9f8e | 2012-09-22 11:46:30 -0700 | [diff] [blame] | 117 | if (!hidraw_table[minor] || !hidraw_table[minor]->exist) { |
Antonio Ospite | e42dee9 | 2010-10-05 17:20:17 +0200 | [diff] [blame] | 118 | ret = -ENODEV; |
| 119 | goto out; |
| 120 | } |
| 121 | |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 122 | dev = hidraw_table[minor]->hid; |
| 123 | |
| 124 | if (!dev->hid_output_raw_report) { |
| 125 | ret = -ENODEV; |
| 126 | goto out; |
| 127 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 128 | |
Jiri Kosina | 2b107d6 | 2008-09-17 19:41:58 +0200 | [diff] [blame] | 129 | if (count > HID_MAX_BUFFER_SIZE) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 130 | hid_warn(dev, "pid %d passed too large report\n", |
| 131 | task_pid_nr(current)); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 132 | ret = -EINVAL; |
| 133 | goto out; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (count < 2) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 137 | hid_warn(dev, "pid %d passed too short report\n", |
| 138 | task_pid_nr(current)); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 139 | ret = -EINVAL; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 140 | goto out; |
| 141 | } |
| 142 | |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 143 | buf = kmalloc(count * sizeof(__u8), GFP_KERNEL); |
| 144 | if (!buf) { |
| 145 | ret = -ENOMEM; |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | if (copy_from_user(buf, buffer, count)) { |
| 150 | ret = -EFAULT; |
| 151 | goto out_free; |
| 152 | } |
| 153 | |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 154 | ret = dev->hid_output_raw_report(dev, buf, count, report_type); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 155 | out_free: |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 156 | kfree(buf); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 157 | out: |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* the first byte is expected to be a report number */ |
| 162 | static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
| 163 | { |
| 164 | ssize_t ret; |
| 165 | mutex_lock(&minors_lock); |
| 166 | ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 167 | mutex_unlock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 171 | |
| 172 | /* This function performs a Get_Report transfer over the control endpoint |
Jiri Kosina | d2a1cfe | 2011-03-27 20:29:02 +0200 | [diff] [blame] | 173 | * per section 7.2.1 of the HID specification, version 1.1. The first byte |
| 174 | * of buffer is the report number to request, or 0x0 if the defice does not |
| 175 | * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT |
| 176 | * or HID_INPUT_REPORT. This function is to be called with the minors_lock |
| 177 | * mutex held. */ |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 178 | static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type) |
| 179 | { |
| 180 | unsigned int minor = iminor(file->f_path.dentry->d_inode); |
| 181 | struct hid_device *dev; |
| 182 | __u8 *buf; |
| 183 | int ret = 0, len; |
| 184 | unsigned char report_number; |
| 185 | |
| 186 | dev = hidraw_table[minor]->hid; |
| 187 | |
| 188 | if (!dev->hid_get_raw_report) { |
| 189 | ret = -ENODEV; |
| 190 | goto out; |
| 191 | } |
| 192 | |
| 193 | if (count > HID_MAX_BUFFER_SIZE) { |
| 194 | printk(KERN_WARNING "hidraw: pid %d passed too large report\n", |
| 195 | task_pid_nr(current)); |
| 196 | ret = -EINVAL; |
| 197 | goto out; |
| 198 | } |
| 199 | |
| 200 | if (count < 2) { |
| 201 | printk(KERN_WARNING "hidraw: pid %d passed too short report\n", |
| 202 | task_pid_nr(current)); |
| 203 | ret = -EINVAL; |
| 204 | goto out; |
| 205 | } |
| 206 | |
| 207 | buf = kmalloc(count * sizeof(__u8), GFP_KERNEL); |
| 208 | if (!buf) { |
| 209 | ret = -ENOMEM; |
| 210 | goto out; |
| 211 | } |
| 212 | |
| 213 | /* Read the first byte from the user. This is the report number, |
Jiri Kosina | d2a1cfe | 2011-03-27 20:29:02 +0200 | [diff] [blame] | 214 | * which is passed to dev->hid_get_raw_report(). */ |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 215 | if (copy_from_user(&report_number, buffer, 1)) { |
| 216 | ret = -EFAULT; |
| 217 | goto out_free; |
| 218 | } |
| 219 | |
| 220 | ret = dev->hid_get_raw_report(dev, report_number, buf, count, report_type); |
| 221 | |
| 222 | if (ret < 0) |
| 223 | goto out_free; |
| 224 | |
| 225 | len = (ret < count) ? ret : count; |
| 226 | |
| 227 | if (copy_to_user(buffer, buf, len)) { |
| 228 | ret = -EFAULT; |
| 229 | goto out_free; |
| 230 | } |
| 231 | |
| 232 | ret = len; |
| 233 | |
| 234 | out_free: |
| 235 | kfree(buf); |
| 236 | out: |
| 237 | return ret; |
| 238 | } |
| 239 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 240 | static unsigned int hidraw_poll(struct file *file, poll_table *wait) |
| 241 | { |
| 242 | struct hidraw_list *list = file->private_data; |
| 243 | |
| 244 | poll_wait(file, &list->hidraw->wait, wait); |
| 245 | if (list->head != list->tail) |
| 246 | return POLLIN | POLLRDNORM; |
| 247 | if (!list->hidraw->exist) |
| 248 | return POLLERR | POLLHUP; |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int hidraw_open(struct inode *inode, struct file *file) |
| 253 | { |
| 254 | unsigned int minor = iminor(inode); |
| 255 | struct hidraw *dev; |
| 256 | struct hidraw_list *list; |
| 257 | int err = 0; |
| 258 | |
| 259 | if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) { |
| 260 | err = -ENOMEM; |
| 261 | goto out; |
| 262 | } |
| 263 | |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 264 | mutex_lock(&minors_lock); |
Ratan Nalumasu | 4fe9f8e | 2012-09-22 11:46:30 -0700 | [diff] [blame] | 265 | if (!hidraw_table[minor] || !hidraw_table[minor]->exist) { |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 266 | err = -ENODEV; |
| 267 | goto out_unlock; |
| 268 | } |
| 269 | |
| 270 | list->hidraw = hidraw_table[minor]; |
| 271 | mutex_init(&list->read_mutex); |
| 272 | list_add_tail(&list->node, &hidraw_table[minor]->list); |
| 273 | file->private_data = list; |
| 274 | |
| 275 | dev = hidraw_table[minor]; |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 276 | if (!dev->open++) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 277 | err = hid_hw_power(dev->hid, PM_HINT_FULLON); |
Amit Nagal | f554ff8 | 2011-09-27 13:41:58 -0400 | [diff] [blame] | 278 | if (err < 0) { |
| 279 | dev->open--; |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 280 | goto out_unlock; |
Amit Nagal | f554ff8 | 2011-09-27 13:41:58 -0400 | [diff] [blame] | 281 | } |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 282 | |
| 283 | err = hid_hw_open(dev->hid); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 284 | if (err < 0) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 285 | hid_hw_power(dev->hid, PM_HINT_NORMAL); |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 286 | dev->open--; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 287 | } |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 288 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 289 | |
| 290 | out_unlock: |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 291 | mutex_unlock(&minors_lock); |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 292 | out: |
Amit Nagal | 1a89623 | 2011-09-07 13:48:47 +0200 | [diff] [blame] | 293 | if (err < 0) |
| 294 | kfree(list); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 295 | return err; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | static int hidraw_release(struct inode * inode, struct file * file) |
| 300 | { |
| 301 | unsigned int minor = iminor(inode); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 302 | struct hidraw_list *list = file->private_data; |
| 303 | |
Ratan Nalumasu | 4fe9f8e | 2012-09-22 11:46:30 -0700 | [diff] [blame] | 304 | drop_ref(hidraw_table[minor], 0); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 305 | list_del(&list->node); |
Jiri Kosina | 4db1c62 | 2008-06-24 14:45:27 +0200 | [diff] [blame] | 306 | kfree(list); |
Ratan Nalumasu | 4fe9f8e | 2012-09-22 11:46:30 -0700 | [diff] [blame] | 307 | return 0; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 310 | static long hidraw_ioctl(struct file *file, unsigned int cmd, |
| 311 | unsigned long arg) |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 312 | { |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 313 | struct inode *inode = file->f_path.dentry->d_inode; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 314 | unsigned int minor = iminor(inode); |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 315 | long ret = 0; |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 316 | struct hidraw *dev; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 317 | void __user *user_arg = (void __user*) arg; |
| 318 | |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 319 | mutex_lock(&minors_lock); |
| 320 | dev = hidraw_table[minor]; |
Antonio Ospite | d20d5ff | 2010-10-05 17:20:16 +0200 | [diff] [blame] | 321 | if (!dev) { |
| 322 | ret = -ENODEV; |
| 323 | goto out; |
| 324 | } |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 325 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 326 | switch (cmd) { |
| 327 | case HIDIOCGRDESCSIZE: |
| 328 | if (put_user(dev->hid->rsize, (int __user *)arg)) |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 329 | ret = -EFAULT; |
| 330 | break; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 331 | |
| 332 | case HIDIOCGRDESC: |
| 333 | { |
| 334 | __u32 len; |
| 335 | |
| 336 | if (get_user(len, (int __user *)arg)) |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 337 | ret = -EFAULT; |
| 338 | else if (len > HID_MAX_DESCRIPTOR_SIZE - 1) |
| 339 | ret = -EINVAL; |
| 340 | else if (copy_to_user(user_arg + offsetof( |
| 341 | struct hidraw_report_descriptor, |
| 342 | value[0]), |
| 343 | dev->hid->rdesc, |
| 344 | min(dev->hid->rsize, len))) |
| 345 | ret = -EFAULT; |
| 346 | break; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 347 | } |
| 348 | case HIDIOCGRAWINFO: |
| 349 | { |
| 350 | struct hidraw_devinfo dinfo; |
| 351 | |
| 352 | dinfo.bustype = dev->hid->bus; |
| 353 | dinfo.vendor = dev->hid->vendor; |
| 354 | dinfo.product = dev->hid->product; |
| 355 | if (copy_to_user(user_arg, &dinfo, sizeof(dinfo))) |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 356 | ret = -EFAULT; |
| 357 | break; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 358 | } |
| 359 | default: |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 360 | { |
| 361 | struct hid_device *hid = dev->hid; |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 362 | if (_IOC_TYPE(cmd) != 'H') { |
| 363 | ret = -EINVAL; |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) { |
| 368 | int len = _IOC_SIZE(cmd); |
| 369 | ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT); |
| 370 | break; |
| 371 | } |
| 372 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) { |
| 373 | int len = _IOC_SIZE(cmd); |
| 374 | ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | /* Begin Read-only ioctls. */ |
| 379 | if (_IOC_DIR(cmd) != _IOC_READ) { |
Dan Carpenter | dfd395af | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 380 | ret = -EINVAL; |
| 381 | break; |
| 382 | } |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 383 | |
| 384 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) { |
Daniel Mack | dd2ed48 | 2011-05-15 18:07:42 +0200 | [diff] [blame] | 385 | int len = strlen(hid->name) + 1; |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 386 | if (len > _IOC_SIZE(cmd)) |
| 387 | len = _IOC_SIZE(cmd); |
Dan Carpenter | dfd395af | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 388 | ret = copy_to_user(user_arg, hid->name, len) ? |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 389 | -EFAULT : len; |
Dan Carpenter | dfd395af | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 390 | break; |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) { |
Daniel Mack | dd2ed48 | 2011-05-15 18:07:42 +0200 | [diff] [blame] | 394 | int len = strlen(hid->phys) + 1; |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 395 | if (len > _IOC_SIZE(cmd)) |
| 396 | len = _IOC_SIZE(cmd); |
Dan Carpenter | dfd395af | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 397 | ret = copy_to_user(user_arg, hid->phys, len) ? |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 398 | -EFAULT : len; |
Dan Carpenter | dfd395af | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 399 | break; |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 400 | } |
Alan Ott | b4dbde9 | 2011-01-18 03:04:39 -0500 | [diff] [blame] | 401 | } |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 402 | |
Dan Carpenter | dfd395af | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 403 | ret = -ENOTTY; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 404 | } |
Antonio Ospite | d20d5ff | 2010-10-05 17:20:16 +0200 | [diff] [blame] | 405 | out: |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 406 | mutex_unlock(&minors_lock); |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 407 | return ret; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static const struct file_operations hidraw_ops = { |
| 411 | .owner = THIS_MODULE, |
| 412 | .read = hidraw_read, |
| 413 | .write = hidraw_write, |
| 414 | .poll = hidraw_poll, |
| 415 | .open = hidraw_open, |
| 416 | .release = hidraw_release, |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 417 | .unlocked_ioctl = hidraw_ioctl, |
Alan Ott | ae5e49c | 2011-01-04 00:37:22 -0500 | [diff] [blame] | 418 | #ifdef CONFIG_COMPAT |
| 419 | .compat_ioctl = hidraw_ioctl, |
| 420 | #endif |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 421 | .llseek = noop_llseek, |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 422 | }; |
| 423 | |
Jiri Kosina | b678724 | 2012-04-27 00:56:08 +0200 | [diff] [blame] | 424 | int hidraw_report_event(struct hid_device *hid, u8 *data, int len) |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 425 | { |
| 426 | struct hidraw *dev = hid->hidraw; |
| 427 | struct hidraw_list *list; |
Jiri Kosina | b678724 | 2012-04-27 00:56:08 +0200 | [diff] [blame] | 428 | int ret = 0; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 429 | |
| 430 | list_for_each_entry(list, &dev->list, node) { |
Matthieu CASTET | 4c7b417 | 2012-06-28 16:51:56 +0200 | [diff] [blame] | 431 | int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1); |
| 432 | |
| 433 | if (new_head == list->tail) |
| 434 | continue; |
| 435 | |
Jiri Kosina | b678724 | 2012-04-27 00:56:08 +0200 | [diff] [blame] | 436 | if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) { |
| 437 | ret = -ENOMEM; |
| 438 | break; |
| 439 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 440 | list->buffer[list->head].len = len; |
Matthieu CASTET | 4c7b417 | 2012-06-28 16:51:56 +0200 | [diff] [blame] | 441 | list->head = new_head; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 442 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 443 | } |
| 444 | |
| 445 | wake_up_interruptible(&dev->wait); |
Jiri Kosina | b678724 | 2012-04-27 00:56:08 +0200 | [diff] [blame] | 446 | return ret; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 447 | } |
| 448 | EXPORT_SYMBOL_GPL(hidraw_report_event); |
| 449 | |
| 450 | int hidraw_connect(struct hid_device *hid) |
| 451 | { |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 452 | int minor, result; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 453 | struct hidraw *dev; |
| 454 | |
Jiri Kosina | bbe281f | 2009-06-04 15:44:25 +0200 | [diff] [blame] | 455 | /* we accept any HID device, no matter the applications */ |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 456 | |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 457 | dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL); |
| 458 | if (!dev) |
| 459 | return -ENOMEM; |
| 460 | |
| 461 | result = -EINVAL; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 462 | |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 463 | mutex_lock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 464 | |
| 465 | for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) { |
| 466 | if (hidraw_table[minor]) |
| 467 | continue; |
| 468 | hidraw_table[minor] = dev; |
| 469 | result = 0; |
| 470 | break; |
| 471 | } |
| 472 | |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 473 | if (result) { |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 474 | mutex_unlock(&minors_lock); |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 475 | kfree(dev); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 476 | goto out; |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 477 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 478 | |
Jiri Kosina | aae6c28 | 2008-12-04 16:16:46 +0100 | [diff] [blame] | 479 | dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor), |
Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 480 | NULL, "%s%d", "hidraw", minor); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 481 | |
| 482 | if (IS_ERR(dev->dev)) { |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 483 | hidraw_table[minor] = NULL; |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 484 | mutex_unlock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 485 | result = PTR_ERR(dev->dev); |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 486 | kfree(dev); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 487 | goto out; |
| 488 | } |
| 489 | |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 490 | mutex_unlock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 491 | init_waitqueue_head(&dev->wait); |
| 492 | INIT_LIST_HEAD(&dev->list); |
| 493 | |
| 494 | dev->hid = hid; |
| 495 | dev->minor = minor; |
| 496 | |
| 497 | dev->exist = 1; |
| 498 | hid->hidraw = dev; |
| 499 | |
| 500 | out: |
| 501 | return result; |
| 502 | |
| 503 | } |
| 504 | EXPORT_SYMBOL_GPL(hidraw_connect); |
| 505 | |
| 506 | void hidraw_disconnect(struct hid_device *hid) |
| 507 | { |
| 508 | struct hidraw *hidraw = hid->hidraw; |
Ratan Nalumasu | 4fe9f8e | 2012-09-22 11:46:30 -0700 | [diff] [blame] | 509 | drop_ref(hidraw, 1); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 510 | } |
| 511 | EXPORT_SYMBOL_GPL(hidraw_disconnect); |
| 512 | |
| 513 | int __init hidraw_init(void) |
| 514 | { |
| 515 | int result; |
| 516 | dev_t dev_id; |
| 517 | |
| 518 | result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR, |
| 519 | HIDRAW_MAX_DEVICES, "hidraw"); |
| 520 | |
| 521 | hidraw_major = MAJOR(dev_id); |
| 522 | |
| 523 | if (result < 0) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 524 | pr_warn("can't get major number\n"); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 525 | goto out; |
| 526 | } |
| 527 | |
| 528 | hidraw_class = class_create(THIS_MODULE, "hidraw"); |
| 529 | if (IS_ERR(hidraw_class)) { |
| 530 | result = PTR_ERR(hidraw_class); |
Alexey Khoroshilov | bcb4a75 | 2012-08-15 23:31:45 +0400 | [diff] [blame] | 531 | goto error_cdev; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | cdev_init(&hidraw_cdev, &hidraw_ops); |
Alexey Khoroshilov | bcb4a75 | 2012-08-15 23:31:45 +0400 | [diff] [blame] | 535 | result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES); |
| 536 | if (result < 0) |
| 537 | goto error_class; |
| 538 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 539 | out: |
| 540 | return result; |
Alexey Khoroshilov | bcb4a75 | 2012-08-15 23:31:45 +0400 | [diff] [blame] | 541 | |
| 542 | error_class: |
| 543 | class_destroy(hidraw_class); |
| 544 | error_cdev: |
| 545 | unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES); |
| 546 | goto out; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Jiri Slaby | 140ae3e | 2008-10-17 18:04:48 +0200 | [diff] [blame] | 549 | void hidraw_exit(void) |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 550 | { |
| 551 | dev_t dev_id = MKDEV(hidraw_major, 0); |
| 552 | |
| 553 | cdev_del(&hidraw_cdev); |
| 554 | class_destroy(hidraw_class); |
| 555 | unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES); |
| 556 | |
| 557 | } |
Ratan Nalumasu | 4fe9f8e | 2012-09-22 11:46:30 -0700 | [diff] [blame] | 558 | |
| 559 | static void drop_ref(struct hidraw *hidraw, int exists_bit) |
| 560 | { |
| 561 | mutex_lock(&minors_lock); |
| 562 | if (exists_bit) { |
| 563 | hid_hw_close(hidraw->hid); |
| 564 | hidraw->exist = 0; |
| 565 | if (hidraw->open) |
| 566 | wake_up_interruptible(&hidraw->wait); |
| 567 | } else { |
| 568 | --hidraw->open; |
| 569 | } |
| 570 | |
| 571 | if (!hidraw->open && !hidraw->exist) { |
| 572 | device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor)); |
| 573 | hidraw_table[hidraw->minor] = NULL; |
| 574 | kfree(hidraw); |
| 575 | } |
| 576 | mutex_unlock(&minors_lock); |
| 577 | } |