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