]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/hid/hidraw.c
HID: usbhid: base runtime PM on modern API
[mirror_ubuntu-hirsute-kernel.git] / drivers / hid / hidraw.c
CommitLineData
86166b7b
JK
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
4291ee30
JP
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
86166b7b
JK
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>
5a0e3ad6 33#include <linux/slab.h>
86166b7b
JK
34#include <linux/hid.h>
35#include <linux/mutex.h>
a99bbaf5 36#include <linux/sched.h>
86166b7b
JK
37
38#include <linux/hidraw.h>
39
40static int hidraw_major;
41static struct cdev hidraw_cdev;
42static struct class *hidraw_class;
43static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
7d672cd7 44static DEFINE_MUTEX(minors_lock);
86166b7b
JK
45
46static 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;
86166b7b
JK
50 DECLARE_WAITQUEUE(wait, current);
51
b0e14951 52 mutex_lock(&list->read_mutex);
86166b7b 53
b0e14951 54 while (ret == 0) {
86166b7b
JK
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
86166b7b
JK
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 }
94 ret += len;
95
96 kfree(list->buffer[list->tail].value);
97 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
98 }
99out:
100 mutex_unlock(&list->read_mutex);
101 return ret;
102}
103
104/* the first byte is expected to be a report number */
105static 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);
2e57480b 108 struct hid_device *dev;
86166b7b
JK
109 __u8 *buf;
110 int ret = 0;
111
2e57480b 112 mutex_lock(&minors_lock);
e42dee9a
AO
113
114 if (!hidraw_table[minor]) {
115 ret = -ENODEV;
116 goto out;
117 }
118
2e57480b
JK
119 dev = hidraw_table[minor]->hid;
120
121 if (!dev->hid_output_raw_report) {
122 ret = -ENODEV;
123 goto out;
124 }
86166b7b 125
2b107d62 126 if (count > HID_MAX_BUFFER_SIZE) {
4291ee30
JP
127 hid_warn(dev, "pid %d passed too large report\n",
128 task_pid_nr(current));
2e57480b
JK
129 ret = -EINVAL;
130 goto out;
86166b7b
JK
131 }
132
133 if (count < 2) {
4291ee30
JP
134 hid_warn(dev, "pid %d passed too short report\n",
135 task_pid_nr(current));
2e57480b
JK
136 ret = -EINVAL;
137 goto out;
86166b7b
JK
138 }
139
140 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
2e57480b
JK
141 if (!buf) {
142 ret = -ENOMEM;
143 goto out;
144 }
86166b7b
JK
145
146 if (copy_from_user(buf, buffer, count)) {
147 ret = -EFAULT;
2e57480b 148 goto out_free;
86166b7b
JK
149 }
150
d4bfa033 151 ret = dev->hid_output_raw_report(dev, buf, count, HID_OUTPUT_REPORT);
2e57480b 152out_free:
86166b7b 153 kfree(buf);
2e57480b
JK
154out:
155 mutex_unlock(&minors_lock);
86166b7b
JK
156 return ret;
157}
158
159static 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
171static 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
7d672cd7 183 mutex_lock(&minors_lock);
86166b7b 184 if (!hidraw_table[minor]) {
86166b7b
JK
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];
7d672cd7 196 if (!dev->open++) {
5bea7660
DT
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);
0361a28d 202 if (err < 0) {
5bea7660 203 hid_hw_power(dev->hid, PM_HINT_NORMAL);
7d672cd7 204 dev->open--;
0361a28d 205 }
7d672cd7 206 }
86166b7b
JK
207
208out_unlock:
7d672cd7 209 mutex_unlock(&minors_lock);
7d672cd7 210out:
86166b7b
JK
211 return err;
212
213}
214
215static 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;
cb174681 220 int ret;
86166b7b 221
cb174681
JS
222 mutex_lock(&minors_lock);
223 if (!hidraw_table[minor]) {
224 ret = -ENODEV;
225 goto unlock;
226 }
86166b7b
JK
227
228 list_del(&list->node);
229 dev = hidraw_table[minor];
b8a832b1 230 if (!--dev->open) {
0361a28d 231 if (list->hidraw->exist) {
5bea7660
DT
232 hid_hw_power(dev->hid, PM_HINT_NORMAL);
233 hid_hw_close(dev->hid);
0361a28d 234 } else {
86166b7b 235 kfree(list->hidraw);
0361a28d 236 }
86166b7b 237 }
4db1c62c 238 kfree(list);
cb174681
JS
239 ret = 0;
240unlock:
241 mutex_unlock(&minors_lock);
4db1c62c 242
cb174681 243 return ret;
86166b7b
JK
244}
245
979c407e
AC
246static long hidraw_ioctl(struct file *file, unsigned int cmd,
247 unsigned long arg)
86166b7b 248{
979c407e 249 struct inode *inode = file->f_path.dentry->d_inode;
86166b7b 250 unsigned int minor = iminor(inode);
979c407e 251 long ret = 0;
2e57480b 252 struct hidraw *dev;
86166b7b
JK
253 void __user *user_arg = (void __user*) arg;
254
2e57480b
JK
255 mutex_lock(&minors_lock);
256 dev = hidraw_table[minor];
d20d5ffa
AO
257 if (!dev) {
258 ret = -ENODEV;
259 goto out;
260 }
2e57480b 261
86166b7b
JK
262 switch (cmd) {
263 case HIDIOCGRDESCSIZE:
264 if (put_user(dev->hid->rsize, (int __user *)arg))
979c407e
AC
265 ret = -EFAULT;
266 break;
86166b7b
JK
267
268 case HIDIOCGRDESC:
269 {
270 __u32 len;
271
272 if (get_user(len, (int __user *)arg))
979c407e
AC
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;
86166b7b
JK
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)))
979c407e
AC
292 ret = -EFAULT;
293 break;
86166b7b
JK
294 }
295 default:
9188e79e
JK
296 {
297 struct hid_device *hid = dev->hid;
dfd395af
DC
298 if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
299 ret = -EINVAL;
300 break;
301 }
9188e79e
JK
302
303 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
304 int len;
38089c65
DC
305 if (!hid->name) {
306 ret = 0;
307 break;
308 }
9188e79e
JK
309 len = strlen(hid->name) + 1;
310 if (len > _IOC_SIZE(cmd))
311 len = _IOC_SIZE(cmd);
dfd395af 312 ret = copy_to_user(user_arg, hid->name, len) ?
9188e79e 313 -EFAULT : len;
dfd395af 314 break;
9188e79e
JK
315 }
316
317 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
318 int len;
38089c65
DC
319 if (!hid->phys) {
320 ret = 0;
321 break;
322 }
9188e79e
JK
323 len = strlen(hid->phys) + 1;
324 if (len > _IOC_SIZE(cmd))
325 len = _IOC_SIZE(cmd);
dfd395af 326 ret = copy_to_user(user_arg, hid->phys, len) ?
9188e79e 327 -EFAULT : len;
dfd395af 328 break;
9188e79e 329 }
81cd5843 330 }
9188e79e 331
dfd395af 332 ret = -ENOTTY;
86166b7b 333 }
d20d5ffa 334out:
2e57480b 335 mutex_unlock(&minors_lock);
979c407e 336 return ret;
86166b7b
JK
337}
338
339static 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,
979c407e 346 .unlocked_ioctl = hidraw_ioctl,
6038f373 347 .llseek = noop_llseek,
86166b7b
JK
348};
349
350void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
351{
352 struct hidraw *dev = hid->hidraw;
353 struct hidraw_list *list;
354
355 list_for_each_entry(list, &dev->list, node) {
356 list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
357 list->buffer[list->head].len = len;
358 list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
359 kill_fasync(&list->fasync, SIGIO, POLL_IN);
360 }
361
362 wake_up_interruptible(&dev->wait);
363}
364EXPORT_SYMBOL_GPL(hidraw_report_event);
365
366int hidraw_connect(struct hid_device *hid)
367{
709d27c0 368 int minor, result;
86166b7b
JK
369 struct hidraw *dev;
370
bbe281fa 371 /* we accept any HID device, no matter the applications */
86166b7b 372
709d27c0
MK
373 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
374 if (!dev)
375 return -ENOMEM;
376
377 result = -EINVAL;
86166b7b 378
7d672cd7 379 mutex_lock(&minors_lock);
86166b7b
JK
380
381 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
382 if (hidraw_table[minor])
383 continue;
384 hidraw_table[minor] = dev;
385 result = 0;
386 break;
387 }
388
709d27c0 389 if (result) {
7d672cd7 390 mutex_unlock(&minors_lock);
709d27c0 391 kfree(dev);
86166b7b 392 goto out;
709d27c0 393 }
86166b7b 394
aae6c286 395 dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
a9b12619 396 NULL, "%s%d", "hidraw", minor);
86166b7b
JK
397
398 if (IS_ERR(dev->dev)) {
86166b7b 399 hidraw_table[minor] = NULL;
7d672cd7 400 mutex_unlock(&minors_lock);
86166b7b 401 result = PTR_ERR(dev->dev);
709d27c0 402 kfree(dev);
86166b7b
JK
403 goto out;
404 }
405
7d672cd7 406 mutex_unlock(&minors_lock);
86166b7b
JK
407 init_waitqueue_head(&dev->wait);
408 INIT_LIST_HEAD(&dev->list);
409
410 dev->hid = hid;
411 dev->minor = minor;
412
413 dev->exist = 1;
414 hid->hidraw = dev;
415
416out:
417 return result;
418
419}
420EXPORT_SYMBOL_GPL(hidraw_connect);
421
422void hidraw_disconnect(struct hid_device *hid)
423{
424 struct hidraw *hidraw = hid->hidraw;
425
426 hidraw->exist = 0;
427
7d672cd7 428 mutex_lock(&minors_lock);
86166b7b 429 hidraw_table[hidraw->minor] = NULL;
7d672cd7 430 mutex_unlock(&minors_lock);
86166b7b
JK
431
432 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
433
434 if (hidraw->open) {
5bea7660 435 hid_hw_close(hid);
86166b7b
JK
436 wake_up_interruptible(&hidraw->wait);
437 } else {
438 kfree(hidraw);
439 }
440}
441EXPORT_SYMBOL_GPL(hidraw_disconnect);
442
443int __init hidraw_init(void)
444{
445 int result;
446 dev_t dev_id;
447
448 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
449 HIDRAW_MAX_DEVICES, "hidraw");
450
451 hidraw_major = MAJOR(dev_id);
452
453 if (result < 0) {
4291ee30 454 pr_warn("can't get major number\n");
86166b7b
JK
455 result = 0;
456 goto out;
457 }
458
459 hidraw_class = class_create(THIS_MODULE, "hidraw");
460 if (IS_ERR(hidraw_class)) {
461 result = PTR_ERR(hidraw_class);
462 unregister_chrdev(hidraw_major, "hidraw");
463 goto out;
464 }
465
466 cdev_init(&hidraw_cdev, &hidraw_ops);
467 cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
468out:
469 return result;
470}
471
140ae3eb 472void hidraw_exit(void)
86166b7b
JK
473{
474 dev_t dev_id = MKDEV(hidraw_major, 0);
475
476 cdev_del(&hidraw_cdev);
477 class_destroy(hidraw_class);
478 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
479
480}