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); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 45 | |
| 46 | static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
| 47 | { |
| 48 | struct hidraw_list *list = file->private_data; |
| 49 | int ret = 0, len; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 50 | DECLARE_WAITQUEUE(wait, current); |
| 51 | |
Jiri Kosina | b0e1495 | 2009-10-12 11:25:56 +0200 | [diff] [blame] | 52 | mutex_lock(&list->read_mutex); |
| 53 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 54 | while (ret == 0) { |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 55 | if (list->head == list->tail) { |
| 56 | add_wait_queue(&list->hidraw->wait, &wait); |
| 57 | set_current_state(TASK_INTERRUPTIBLE); |
| 58 | |
| 59 | while (list->head == list->tail) { |
| 60 | if (file->f_flags & O_NONBLOCK) { |
| 61 | ret = -EAGAIN; |
| 62 | break; |
| 63 | } |
| 64 | if (signal_pending(current)) { |
| 65 | ret = -ERESTARTSYS; |
| 66 | break; |
| 67 | } |
| 68 | if (!list->hidraw->exist) { |
| 69 | ret = -EIO; |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | /* allow O_NONBLOCK to work well from other threads */ |
| 74 | mutex_unlock(&list->read_mutex); |
| 75 | schedule(); |
| 76 | mutex_lock(&list->read_mutex); |
| 77 | set_current_state(TASK_INTERRUPTIBLE); |
| 78 | } |
| 79 | |
| 80 | set_current_state(TASK_RUNNING); |
| 81 | remove_wait_queue(&list->hidraw->wait, &wait); |
| 82 | } |
| 83 | |
| 84 | if (ret) |
| 85 | goto out; |
| 86 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 87 | len = list->buffer[list->tail].len > count ? |
| 88 | count : list->buffer[list->tail].len; |
| 89 | |
| 90 | if (copy_to_user(buffer, list->buffer[list->tail].value, len)) { |
| 91 | ret = -EFAULT; |
| 92 | goto out; |
| 93 | } |
Alan Ott | cf28a67 | 2011-01-26 22:25:18 -0500 | [diff] [blame^] | 94 | ret = len; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 95 | |
| 96 | kfree(list->buffer[list->tail].value); |
| 97 | list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1); |
| 98 | } |
| 99 | out: |
| 100 | mutex_unlock(&list->read_mutex); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | /* the first byte is expected to be a report number */ |
| 105 | static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
| 106 | { |
| 107 | unsigned int minor = iminor(file->f_path.dentry->d_inode); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 108 | struct hid_device *dev; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 109 | __u8 *buf; |
| 110 | int ret = 0; |
| 111 | |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 112 | mutex_lock(&minors_lock); |
Antonio Ospite | e42dee9 | 2010-10-05 17:20:17 +0200 | [diff] [blame] | 113 | |
| 114 | if (!hidraw_table[minor]) { |
| 115 | ret = -ENODEV; |
| 116 | goto out; |
| 117 | } |
| 118 | |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 119 | dev = hidraw_table[minor]->hid; |
| 120 | |
| 121 | if (!dev->hid_output_raw_report) { |
| 122 | ret = -ENODEV; |
| 123 | goto out; |
| 124 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 125 | |
Jiri Kosina | 2b107d6 | 2008-09-17 19:41:58 +0200 | [diff] [blame] | 126 | if (count > HID_MAX_BUFFER_SIZE) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 127 | hid_warn(dev, "pid %d passed too large report\n", |
| 128 | task_pid_nr(current)); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 129 | ret = -EINVAL; |
| 130 | goto out; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (count < 2) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 134 | hid_warn(dev, "pid %d passed too short report\n", |
| 135 | task_pid_nr(current)); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 136 | ret = -EINVAL; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 137 | goto out; |
| 138 | } |
| 139 | |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 140 | buf = kmalloc(count * sizeof(__u8), GFP_KERNEL); |
| 141 | if (!buf) { |
| 142 | ret = -ENOMEM; |
| 143 | goto out; |
| 144 | } |
| 145 | |
| 146 | if (copy_from_user(buf, buffer, count)) { |
| 147 | ret = -EFAULT; |
| 148 | goto out_free; |
| 149 | } |
| 150 | |
Jiri Kosina | d4bfa03 | 2010-01-29 15:03:36 +0100 | [diff] [blame] | 151 | ret = dev->hid_output_raw_report(dev, buf, count, HID_OUTPUT_REPORT); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 152 | out_free: |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 153 | kfree(buf); |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 154 | out: |
| 155 | mutex_unlock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static unsigned int hidraw_poll(struct file *file, poll_table *wait) |
| 160 | { |
| 161 | struct hidraw_list *list = file->private_data; |
| 162 | |
| 163 | poll_wait(file, &list->hidraw->wait, wait); |
| 164 | if (list->head != list->tail) |
| 165 | return POLLIN | POLLRDNORM; |
| 166 | if (!list->hidraw->exist) |
| 167 | return POLLERR | POLLHUP; |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int hidraw_open(struct inode *inode, struct file *file) |
| 172 | { |
| 173 | unsigned int minor = iminor(inode); |
| 174 | struct hidraw *dev; |
| 175 | struct hidraw_list *list; |
| 176 | int err = 0; |
| 177 | |
| 178 | if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) { |
| 179 | err = -ENOMEM; |
| 180 | goto out; |
| 181 | } |
| 182 | |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 183 | mutex_lock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 184 | if (!hidraw_table[minor]) { |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 185 | kfree(list); |
| 186 | err = -ENODEV; |
| 187 | goto out_unlock; |
| 188 | } |
| 189 | |
| 190 | list->hidraw = hidraw_table[minor]; |
| 191 | mutex_init(&list->read_mutex); |
| 192 | list_add_tail(&list->node, &hidraw_table[minor]->list); |
| 193 | file->private_data = list; |
| 194 | |
| 195 | dev = hidraw_table[minor]; |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 196 | if (!dev->open++) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 197 | err = hid_hw_power(dev->hid, PM_HINT_FULLON); |
| 198 | if (err < 0) |
| 199 | goto out_unlock; |
| 200 | |
| 201 | err = hid_hw_open(dev->hid); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 202 | if (err < 0) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 203 | hid_hw_power(dev->hid, PM_HINT_NORMAL); |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 204 | dev->open--; |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 205 | } |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 206 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 207 | |
| 208 | out_unlock: |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 209 | mutex_unlock(&minors_lock); |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 210 | out: |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 211 | return err; |
| 212 | |
| 213 | } |
| 214 | |
| 215 | static int hidraw_release(struct inode * inode, struct file * file) |
| 216 | { |
| 217 | unsigned int minor = iminor(inode); |
| 218 | struct hidraw *dev; |
| 219 | struct hidraw_list *list = file->private_data; |
Jiri Slaby | cb17468 | 2010-10-19 11:29:55 +0200 | [diff] [blame] | 220 | int ret; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 221 | |
Jiri Slaby | cb17468 | 2010-10-19 11:29:55 +0200 | [diff] [blame] | 222 | mutex_lock(&minors_lock); |
| 223 | if (!hidraw_table[minor]) { |
| 224 | ret = -ENODEV; |
| 225 | goto unlock; |
| 226 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 227 | |
| 228 | list_del(&list->node); |
| 229 | dev = hidraw_table[minor]; |
Oliver Neukum | b8a832b | 2008-12-15 13:12:08 +0100 | [diff] [blame] | 230 | if (!--dev->open) { |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 231 | if (list->hidraw->exist) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 232 | hid_hw_power(dev->hid, PM_HINT_NORMAL); |
| 233 | hid_hw_close(dev->hid); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 234 | } else { |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 235 | kfree(list->hidraw); |
Oliver Neukum | 0361a28 | 2008-12-17 15:38:03 +0100 | [diff] [blame] | 236 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 237 | } |
Jiri Kosina | 4db1c62 | 2008-06-24 14:45:27 +0200 | [diff] [blame] | 238 | kfree(list); |
Jiri Slaby | cb17468 | 2010-10-19 11:29:55 +0200 | [diff] [blame] | 239 | ret = 0; |
| 240 | unlock: |
| 241 | mutex_unlock(&minors_lock); |
Jiri Kosina | 4db1c62 | 2008-06-24 14:45:27 +0200 | [diff] [blame] | 242 | |
Jiri Slaby | cb17468 | 2010-10-19 11:29:55 +0200 | [diff] [blame] | 243 | return ret; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 246 | static long hidraw_ioctl(struct file *file, unsigned int cmd, |
| 247 | unsigned long arg) |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 248 | { |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 249 | struct inode *inode = file->f_path.dentry->d_inode; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 250 | unsigned int minor = iminor(inode); |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 251 | long ret = 0; |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 252 | struct hidraw *dev; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 253 | void __user *user_arg = (void __user*) arg; |
| 254 | |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 255 | mutex_lock(&minors_lock); |
| 256 | dev = hidraw_table[minor]; |
Antonio Ospite | d20d5ff | 2010-10-05 17:20:16 +0200 | [diff] [blame] | 257 | if (!dev) { |
| 258 | ret = -ENODEV; |
| 259 | goto out; |
| 260 | } |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 261 | |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 262 | switch (cmd) { |
| 263 | case HIDIOCGRDESCSIZE: |
| 264 | if (put_user(dev->hid->rsize, (int __user *)arg)) |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 265 | ret = -EFAULT; |
| 266 | break; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 267 | |
| 268 | case HIDIOCGRDESC: |
| 269 | { |
| 270 | __u32 len; |
| 271 | |
| 272 | if (get_user(len, (int __user *)arg)) |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 273 | ret = -EFAULT; |
| 274 | else if (len > HID_MAX_DESCRIPTOR_SIZE - 1) |
| 275 | ret = -EINVAL; |
| 276 | else if (copy_to_user(user_arg + offsetof( |
| 277 | struct hidraw_report_descriptor, |
| 278 | value[0]), |
| 279 | dev->hid->rdesc, |
| 280 | min(dev->hid->rsize, len))) |
| 281 | ret = -EFAULT; |
| 282 | break; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 283 | } |
| 284 | case HIDIOCGRAWINFO: |
| 285 | { |
| 286 | struct hidraw_devinfo dinfo; |
| 287 | |
| 288 | dinfo.bustype = dev->hid->bus; |
| 289 | dinfo.vendor = dev->hid->vendor; |
| 290 | dinfo.product = dev->hid->product; |
| 291 | if (copy_to_user(user_arg, &dinfo, sizeof(dinfo))) |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 292 | ret = -EFAULT; |
| 293 | break; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 294 | } |
| 295 | default: |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 296 | { |
| 297 | struct hid_device *hid = dev->hid; |
Dan Carpenter | dfd395a | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 298 | if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) { |
| 299 | ret = -EINVAL; |
| 300 | break; |
| 301 | } |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 302 | |
| 303 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) { |
| 304 | int len; |
Dan Carpenter | 38089c6 | 2009-04-07 16:35:56 +0200 | [diff] [blame] | 305 | if (!hid->name) { |
| 306 | ret = 0; |
| 307 | break; |
| 308 | } |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 309 | len = strlen(hid->name) + 1; |
| 310 | if (len > _IOC_SIZE(cmd)) |
| 311 | len = _IOC_SIZE(cmd); |
Dan Carpenter | dfd395a | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 312 | ret = copy_to_user(user_arg, hid->name, len) ? |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 313 | -EFAULT : len; |
Dan Carpenter | dfd395a | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 314 | break; |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) { |
| 318 | int len; |
Dan Carpenter | 38089c6 | 2009-04-07 16:35:56 +0200 | [diff] [blame] | 319 | if (!hid->phys) { |
| 320 | ret = 0; |
| 321 | break; |
| 322 | } |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 323 | len = strlen(hid->phys) + 1; |
| 324 | if (len > _IOC_SIZE(cmd)) |
| 325 | len = _IOC_SIZE(cmd); |
Dan Carpenter | dfd395a | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 326 | ret = copy_to_user(user_arg, hid->phys, len) ? |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 327 | -EFAULT : len; |
Dan Carpenter | dfd395a | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 328 | break; |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 329 | } |
Antonio Ospite | 81cd584 | 2010-04-30 21:49:58 +0200 | [diff] [blame] | 330 | } |
Jiri Kosina | 9188e79 | 2008-11-12 16:14:08 +0100 | [diff] [blame] | 331 | |
Dan Carpenter | dfd395a | 2009-02-03 16:35:17 +0300 | [diff] [blame] | 332 | ret = -ENOTTY; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 333 | } |
Antonio Ospite | d20d5ff | 2010-10-05 17:20:16 +0200 | [diff] [blame] | 334 | out: |
Jiri Kosina | 2e57480 | 2010-03-25 14:15:11 +0100 | [diff] [blame] | 335 | mutex_unlock(&minors_lock); |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 336 | return ret; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static const struct file_operations hidraw_ops = { |
| 340 | .owner = THIS_MODULE, |
| 341 | .read = hidraw_read, |
| 342 | .write = hidraw_write, |
| 343 | .poll = hidraw_poll, |
| 344 | .open = hidraw_open, |
| 345 | .release = hidraw_release, |
Alan Cox | 979c407 | 2008-05-26 11:25:26 +0200 | [diff] [blame] | 346 | .unlocked_ioctl = hidraw_ioctl, |
Alan Ott | ae5e49c | 2011-01-04 00:37:22 -0500 | [diff] [blame] | 347 | #ifdef CONFIG_COMPAT |
| 348 | .compat_ioctl = hidraw_ioctl, |
| 349 | #endif |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 350 | .llseek = noop_llseek, |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | void hidraw_report_event(struct hid_device *hid, u8 *data, int len) |
| 354 | { |
| 355 | struct hidraw *dev = hid->hidraw; |
| 356 | struct hidraw_list *list; |
| 357 | |
| 358 | list_for_each_entry(list, &dev->list, node) { |
| 359 | list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC); |
| 360 | list->buffer[list->head].len = len; |
| 361 | list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1); |
| 362 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 363 | } |
| 364 | |
| 365 | wake_up_interruptible(&dev->wait); |
| 366 | } |
| 367 | EXPORT_SYMBOL_GPL(hidraw_report_event); |
| 368 | |
| 369 | int hidraw_connect(struct hid_device *hid) |
| 370 | { |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 371 | int minor, result; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 372 | struct hidraw *dev; |
| 373 | |
Jiri Kosina | bbe281f | 2009-06-04 15:44:25 +0200 | [diff] [blame] | 374 | /* we accept any HID device, no matter the applications */ |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 375 | |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 376 | dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL); |
| 377 | if (!dev) |
| 378 | return -ENOMEM; |
| 379 | |
| 380 | result = -EINVAL; |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 381 | |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 382 | mutex_lock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 383 | |
| 384 | for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) { |
| 385 | if (hidraw_table[minor]) |
| 386 | continue; |
| 387 | hidraw_table[minor] = dev; |
| 388 | result = 0; |
| 389 | break; |
| 390 | } |
| 391 | |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 392 | if (result) { |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 393 | mutex_unlock(&minors_lock); |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 394 | kfree(dev); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 395 | goto out; |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 396 | } |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 397 | |
Jiri Kosina | aae6c28 | 2008-12-04 16:16:46 +0100 | [diff] [blame] | 398 | 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] | 399 | NULL, "%s%d", "hidraw", minor); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 400 | |
| 401 | if (IS_ERR(dev->dev)) { |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 402 | hidraw_table[minor] = NULL; |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 403 | mutex_unlock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 404 | result = PTR_ERR(dev->dev); |
Mariusz Kozlowski | 709d27c | 2007-09-27 11:24:55 +0200 | [diff] [blame] | 405 | kfree(dev); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 406 | goto out; |
| 407 | } |
| 408 | |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 409 | mutex_unlock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 410 | init_waitqueue_head(&dev->wait); |
| 411 | INIT_LIST_HEAD(&dev->list); |
| 412 | |
| 413 | dev->hid = hid; |
| 414 | dev->minor = minor; |
| 415 | |
| 416 | dev->exist = 1; |
| 417 | hid->hidraw = dev; |
| 418 | |
| 419 | out: |
| 420 | return result; |
| 421 | |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(hidraw_connect); |
| 424 | |
| 425 | void hidraw_disconnect(struct hid_device *hid) |
| 426 | { |
| 427 | struct hidraw *hidraw = hid->hidraw; |
| 428 | |
| 429 | hidraw->exist = 0; |
| 430 | |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 431 | mutex_lock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 432 | hidraw_table[hidraw->minor] = NULL; |
Oliver Neukum | 7d672cd | 2008-10-31 00:07:23 +0100 | [diff] [blame] | 433 | mutex_unlock(&minors_lock); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 434 | |
| 435 | device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor)); |
| 436 | |
| 437 | if (hidraw->open) { |
Dmitry Torokhov | 5bea766 | 2010-12-07 23:02:48 -0800 | [diff] [blame] | 438 | hid_hw_close(hid); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 439 | wake_up_interruptible(&hidraw->wait); |
| 440 | } else { |
| 441 | kfree(hidraw); |
| 442 | } |
| 443 | } |
| 444 | EXPORT_SYMBOL_GPL(hidraw_disconnect); |
| 445 | |
| 446 | int __init hidraw_init(void) |
| 447 | { |
| 448 | int result; |
| 449 | dev_t dev_id; |
| 450 | |
| 451 | result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR, |
| 452 | HIDRAW_MAX_DEVICES, "hidraw"); |
| 453 | |
| 454 | hidraw_major = MAJOR(dev_id); |
| 455 | |
| 456 | if (result < 0) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 457 | pr_warn("can't get major number\n"); |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 458 | result = 0; |
| 459 | goto out; |
| 460 | } |
| 461 | |
| 462 | hidraw_class = class_create(THIS_MODULE, "hidraw"); |
| 463 | if (IS_ERR(hidraw_class)) { |
| 464 | result = PTR_ERR(hidraw_class); |
| 465 | unregister_chrdev(hidraw_major, "hidraw"); |
| 466 | goto out; |
| 467 | } |
| 468 | |
| 469 | cdev_init(&hidraw_cdev, &hidraw_ops); |
| 470 | cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES); |
| 471 | out: |
| 472 | return result; |
| 473 | } |
| 474 | |
Jiri Slaby | 140ae3e | 2008-10-17 18:04:48 +0200 | [diff] [blame] | 475 | void hidraw_exit(void) |
Jiri Kosina | 86166b7 | 2007-05-14 09:57:40 +0200 | [diff] [blame] | 476 | { |
| 477 | dev_t dev_id = MKDEV(hidraw_major, 0); |
| 478 | |
| 479 | cdev_del(&hidraw_cdev); |
| 480 | class_destroy(hidraw_class); |
| 481 | unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES); |
| 482 | |
| 483 | } |