blob: 5dfe802089326be314bdb4868b3d1a06cc05ccf7 [file] [log] [blame]
Thomas Gleixnera32dd672019-05-29 07:18:11 -07001// SPDX-License-Identifier: GPL-2.0-only
Jiri Kosina86166b72007-05-14 09:57:40 +02002/*
3 * HID raw devices, giving access to raw HID events.
4 *
5 * In comparison to hiddev, this device does not process the
6 * hid events at all (no parsing, no lookups). This lets applications
7 * to work on raw hid events as they want to, and avoids a need to
8 * use a transport-specific userspace libhid/libusb libraries.
9 *
Jiri Kosina61834532014-01-06 16:43:27 +010010 * Copyright (c) 2007-2014 Jiri Kosina
Jiri Kosina86166b72007-05-14 09:57:40 +020011 */
12
Jiri Kosina86166b72007-05-14 09:57:40 +020013
Joe Perches4291ee32010-12-09 19:29:03 -080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Jiri Kosina86166b72007-05-14 09:57:40 +020016#include <linux/fs.h>
17#include <linux/module.h>
18#include <linux/errno.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/cdev.h>
22#include <linux/poll.h>
23#include <linux/device.h>
24#include <linux/major.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020026#include <linux/hid.h>
27#include <linux/mutex.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028#include <linux/sched/signal.h>
Dmitry Torokhov8f507c82016-03-16 16:59:38 -070029#include <linux/string.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020030
31#include <linux/hidraw.h>
32
33static int hidraw_major;
34static struct cdev hidraw_cdev;
35static struct class *hidraw_class;
36static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
Oliver Neukum7d672cd2008-10-31 00:07:23 +010037static DEFINE_MUTEX(minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +020038
39static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
40{
41 struct hidraw_list *list = file->private_data;
42 int ret = 0, len;
Jiri Kosina86166b72007-05-14 09:57:40 +020043 DECLARE_WAITQUEUE(wait, current);
44
Jiri Kosinab0e14952009-10-12 11:25:56 +020045 mutex_lock(&list->read_mutex);
46
Jiri Kosina86166b72007-05-14 09:57:40 +020047 while (ret == 0) {
Jiri Kosina86166b72007-05-14 09:57:40 +020048 if (list->head == list->tail) {
49 add_wait_queue(&list->hidraw->wait, &wait);
50 set_current_state(TASK_INTERRUPTIBLE);
51
52 while (list->head == list->tail) {
Jiri Kosina86166b72007-05-14 09:57:40 +020053 if (signal_pending(current)) {
54 ret = -ERESTARTSYS;
55 break;
56 }
57 if (!list->hidraw->exist) {
58 ret = -EIO;
59 break;
60 }
Founder Fang7611e8d2012-11-21 15:20:31 +080061 if (file->f_flags & O_NONBLOCK) {
62 ret = -EAGAIN;
63 break;
64 }
Jiri Kosina86166b72007-05-14 09:57:40 +020065
66 /* allow O_NONBLOCK to work well from other threads */
67 mutex_unlock(&list->read_mutex);
68 schedule();
69 mutex_lock(&list->read_mutex);
70 set_current_state(TASK_INTERRUPTIBLE);
71 }
72
73 set_current_state(TASK_RUNNING);
74 remove_wait_queue(&list->hidraw->wait, &wait);
75 }
76
77 if (ret)
78 goto out;
79
Jiri Kosina86166b72007-05-14 09:57:40 +020080 len = list->buffer[list->tail].len > count ?
81 count : list->buffer[list->tail].len;
82
Jiri Kosinab6787242012-04-27 00:56:08 +020083 if (list->buffer[list->tail].value) {
84 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
85 ret = -EFAULT;
86 goto out;
87 }
88 ret = len;
Jiri Kosina86166b72007-05-14 09:57:40 +020089 }
Jiri Kosina86166b72007-05-14 09:57:40 +020090
91 kfree(list->buffer[list->tail].value);
Matthieu CASTET4c7b4172012-06-28 16:51:56 +020092 list->buffer[list->tail].value = NULL;
Jiri Kosina86166b72007-05-14 09:57:40 +020093 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
94 }
95out:
96 mutex_unlock(&list->read_mutex);
97 return ret;
98}
99
Jiri Kosina61834532014-01-06 16:43:27 +0100100/*
101 * The first byte of the report buffer is expected to be a report number.
Jiri Kosina61834532014-01-06 16:43:27 +0100102 */
Alan Ottb4dbde92011-01-18 03:04:39 -0500103static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
Jiri Kosina86166b72007-05-14 09:57:40 +0200104{
Al Viro496ad9a2013-01-23 17:07:38 -0500105 unsigned int minor = iminor(file_inode(file));
Jiri Kosina2e574802010-03-25 14:15:11 +0100106 struct hid_device *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200107 __u8 *buf;
108 int ret = 0;
109
Jiri Kosinacc7ed492018-11-08 22:38:42 +0100110 lockdep_assert_held(&minors_lock);
111
Manoj Chourasia212a8712013-07-22 15:33:13 +0530112 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
Antonio Ospitee42dee92010-10-05 17:20:17 +0200113 ret = -ENODEV;
114 goto out;
115 }
116
Jiri Kosina2e574802010-03-25 14:15:11 +0100117 dev = hidraw_table[minor]->hid;
118
Jiri Kosina2b107d62008-09-17 19:41:58 +0200119 if (count > HID_MAX_BUFFER_SIZE) {
Joe Perches4291ee32010-12-09 19:29:03 -0800120 hid_warn(dev, "pid %d passed too large report\n",
121 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100122 ret = -EINVAL;
123 goto out;
Jiri Kosina86166b72007-05-14 09:57:40 +0200124 }
125
126 if (count < 2) {
Joe Perches4291ee32010-12-09 19:29:03 -0800127 hid_warn(dev, "pid %d passed too short report\n",
128 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100129 ret = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200130 goto out;
131 }
132
Dmitry Torokhov8f507c82016-03-16 16:59:38 -0700133 buf = memdup_user(buffer, count);
134 if (IS_ERR(buf)) {
135 ret = PTR_ERR(buf);
Jiri Kosina2e574802010-03-25 14:15:11 +0100136 goto out;
137 }
138
Benjamin Tissoirese534a932014-03-08 22:52:42 -0500139 if ((report_type == HID_OUTPUT_REPORT) &&
140 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
Benjamin Tissoires3a75b242014-02-20 15:24:50 -0500141 ret = hid_hw_output_report(dev, buf, count);
142 /*
143 * compatibility with old implementation of USB-HID and I2C-HID:
144 * if the device does not support receiving output reports,
145 * on an interrupt endpoint, fallback to SET_REPORT HID command.
146 */
147 if (ret != -ENOSYS)
148 goto out_free;
149 }
150
151 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
152 HID_REQ_SET_REPORT);
153
Jiri Kosina2e574802010-03-25 14:15:11 +0100154out_free:
Jiri Kosina86166b72007-05-14 09:57:40 +0200155 kfree(buf);
Jiri Kosina2e574802010-03-25 14:15:11 +0100156out:
Alan Ottb4dbde92011-01-18 03:04:39 -0500157 return ret;
158}
159
Alan Ottb4dbde92011-01-18 03:04:39 -0500160static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
161{
162 ssize_t ret;
163 mutex_lock(&minors_lock);
164 ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
Jiri Kosina2e574802010-03-25 14:15:11 +0100165 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200166 return ret;
167}
168
Alan Ottb4dbde92011-01-18 03:04:39 -0500169
Jiri Kosina61834532014-01-06 16:43:27 +0100170/*
171 * This function performs a Get_Report transfer over the control endpoint
Jiri Kosinad2a1cfe2011-03-27 20:29:02 +0200172 * per section 7.2.1 of the HID specification, version 1.1. The first byte
173 * of buffer is the report number to request, or 0x0 if the defice does not
174 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
Jiri Kosina61834532014-01-06 16:43:27 +0100175 * or HID_INPUT_REPORT.
Jiri Kosina61834532014-01-06 16:43:27 +0100176 */
Alan Ottb4dbde92011-01-18 03:04:39 -0500177static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
178{
Al Viro496ad9a2013-01-23 17:07:38 -0500179 unsigned int minor = iminor(file_inode(file));
Alan Ottb4dbde92011-01-18 03:04:39 -0500180 struct hid_device *dev;
181 __u8 *buf;
182 int ret = 0, len;
183 unsigned char report_number;
184
Jiri Kosinacc7ed492018-11-08 22:38:42 +0100185 lockdep_assert_held(&minors_lock);
186
Rodrigo Rivas Costaa9553582018-04-06 01:09:36 +0200187 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
188 ret = -ENODEV;
189 goto out;
190 }
191
Alan Ottb4dbde92011-01-18 03:04:39 -0500192 dev = hidraw_table[minor]->hid;
193
Benjamin Tissoirescafebc02014-02-05 16:33:22 -0500194 if (!dev->ll_driver->raw_request) {
Alan Ottb4dbde92011-01-18 03:04:39 -0500195 ret = -ENODEV;
196 goto out;
197 }
198
199 if (count > HID_MAX_BUFFER_SIZE) {
Rishi Guptab05cec62019-09-20 08:05:01 +0530200 hid_warn(dev, "pid %d passed too large report\n",
201 task_pid_nr(current));
Alan Ottb4dbde92011-01-18 03:04:39 -0500202 ret = -EINVAL;
203 goto out;
204 }
205
206 if (count < 2) {
Rishi Guptab05cec62019-09-20 08:05:01 +0530207 hid_warn(dev, "pid %d passed too short report\n",
208 task_pid_nr(current));
Alan Ottb4dbde92011-01-18 03:04:39 -0500209 ret = -EINVAL;
210 goto out;
211 }
212
Kees Cook6da2ec52018-06-12 13:55:00 -0700213 buf = kmalloc(count, GFP_KERNEL);
Alan Ottb4dbde92011-01-18 03:04:39 -0500214 if (!buf) {
215 ret = -ENOMEM;
216 goto out;
217 }
218
Jiri Kosina61834532014-01-06 16:43:27 +0100219 /*
220 * Read the first byte from the user. This is the report number,
Benjamin Tissoirescafebc02014-02-05 16:33:22 -0500221 * which is passed to hid_hw_raw_request().
Jiri Kosina61834532014-01-06 16:43:27 +0100222 */
Alan Ottb4dbde92011-01-18 03:04:39 -0500223 if (copy_from_user(&report_number, buffer, 1)) {
224 ret = -EFAULT;
225 goto out_free;
226 }
227
Benjamin Tissoirescafebc02014-02-05 16:33:22 -0500228 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
229 HID_REQ_GET_REPORT);
Alan Ottb4dbde92011-01-18 03:04:39 -0500230
231 if (ret < 0)
232 goto out_free;
233
234 len = (ret < count) ? ret : count;
235
236 if (copy_to_user(buffer, buf, len)) {
237 ret = -EFAULT;
238 goto out_free;
239 }
240
241 ret = len;
242
243out_free:
244 kfree(buf);
245out:
246 return ret;
247}
248
Al Viroafc9a422017-07-03 06:39:46 -0400249static __poll_t hidraw_poll(struct file *file, poll_table *wait)
Jiri Kosina86166b72007-05-14 09:57:40 +0200250{
251 struct hidraw_list *list = file->private_data;
252
253 poll_wait(file, &list->hidraw->wait, wait);
254 if (list->head != list->tail)
Fabian Henneke378b8032019-07-09 13:03:37 +0200255 return EPOLLIN | EPOLLRDNORM | EPOLLOUT;
Jiri Kosina86166b72007-05-14 09:57:40 +0200256 if (!list->hidraw->exist)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800257 return EPOLLERR | EPOLLHUP;
Jiri Kosina86166b72007-05-14 09:57:40 +0200258 return 0;
259}
260
261static int hidraw_open(struct inode *inode, struct file *file)
262{
263 unsigned int minor = iminor(inode);
264 struct hidraw *dev;
265 struct hidraw_list *list;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800266 unsigned long flags;
Jiri Kosina86166b72007-05-14 09:57:40 +0200267 int err = 0;
268
269 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
270 err = -ENOMEM;
271 goto out;
272 }
273
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100274 mutex_lock(&minors_lock);
Manoj Chourasia212a8712013-07-22 15:33:13 +0530275 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200276 err = -ENODEV;
277 goto out_unlock;
278 }
279
Jiri Kosina86166b72007-05-14 09:57:40 +0200280 dev = hidraw_table[minor];
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100281 if (!dev->open++) {
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800282 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
Amit Nagalf554ff82011-09-27 13:41:58 -0400283 if (err < 0) {
284 dev->open--;
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800285 goto out_unlock;
Amit Nagalf554ff82011-09-27 13:41:58 -0400286 }
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800287
288 err = hid_hw_open(dev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100289 if (err < 0) {
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800290 hid_hw_power(dev->hid, PM_HINT_NORMAL);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100291 dev->open--;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800292 goto out_unlock;
Oliver Neukum0361a282008-12-17 15:38:03 +0100293 }
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100294 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200295
Yonghua Zheng277fe442013-08-26 23:38:35 +0800296 list->hidraw = hidraw_table[minor];
297 mutex_init(&list->read_mutex);
298 spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
299 list_add_tail(&list->node, &hidraw_table[minor]->list);
300 spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
301 file->private_data = list;
Jiri Kosina86166b72007-05-14 09:57:40 +0200302out_unlock:
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100303 mutex_unlock(&minors_lock);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100304out:
Amit Nagal1a896232011-09-07 13:48:47 +0200305 if (err < 0)
306 kfree(list);
Jiri Kosina86166b72007-05-14 09:57:40 +0200307 return err;
308
309}
310
Andrew Dugganb5531312012-11-27 19:02:27 -0800311static int hidraw_fasync(int fd, struct file *file, int on)
312{
313 struct hidraw_list *list = file->private_data;
314
315 return fasync_helper(fd, file, on, &list->fasync);
316}
317
Manoj Chourasia212a8712013-07-22 15:33:13 +0530318static void drop_ref(struct hidraw *hidraw, int exists_bit)
319{
320 if (exists_bit) {
Manoj Chourasia212a8712013-07-22 15:33:13 +0530321 hidraw->exist = 0;
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530322 if (hidraw->open) {
323 hid_hw_close(hidraw->hid);
Manoj Chourasia212a8712013-07-22 15:33:13 +0530324 wake_up_interruptible(&hidraw->wait);
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530325 }
Fernando Luis Vázquez Cao47587fc2014-02-26 16:51:24 +0900326 device_destroy(hidraw_class,
327 MKDEV(hidraw_major, hidraw->minor));
Manoj Chourasia212a8712013-07-22 15:33:13 +0530328 } else {
329 --hidraw->open;
330 }
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530331 if (!hidraw->open) {
332 if (!hidraw->exist) {
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530333 hidraw_table[hidraw->minor] = NULL;
334 kfree(hidraw);
335 } else {
336 /* close device for last reader */
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530337 hid_hw_close(hidraw->hid);
Dmitry Torokhov814b6d12017-09-19 18:37:46 -0700338 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530339 }
Manoj Chourasia212a8712013-07-22 15:33:13 +0530340 }
341}
342
Jiri Kosina86166b72007-05-14 09:57:40 +0200343static int hidraw_release(struct inode * inode, struct file * file)
344{
345 unsigned int minor = iminor(inode);
Jiri Kosina86166b72007-05-14 09:57:40 +0200346 struct hidraw_list *list = file->private_data;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800347 unsigned long flags;
Jiri Kosina86166b72007-05-14 09:57:40 +0200348
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100349 mutex_lock(&minors_lock);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100350
Yonghua Zheng277fe442013-08-26 23:38:35 +0800351 spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
Jiri Kosina86166b72007-05-14 09:57:40 +0200352 list_del(&list->node);
Yonghua Zheng277fe442013-08-26 23:38:35 +0800353 spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
Jiri Kosina4db1c622008-06-24 14:45:27 +0200354 kfree(list);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100355
Manoj Chourasia212a8712013-07-22 15:33:13 +0530356 drop_ref(hidraw_table[minor], 0);
357
358 mutex_unlock(&minors_lock);
359 return 0;
Jiri Kosina86166b72007-05-14 09:57:40 +0200360}
361
Alan Cox979c4072008-05-26 11:25:26 +0200362static long hidraw_ioctl(struct file *file, unsigned int cmd,
363 unsigned long arg)
Jiri Kosina86166b72007-05-14 09:57:40 +0200364{
Al Viro496ad9a2013-01-23 17:07:38 -0500365 struct inode *inode = file_inode(file);
Jiri Kosina86166b72007-05-14 09:57:40 +0200366 unsigned int minor = iminor(inode);
Alan Cox979c4072008-05-26 11:25:26 +0200367 long ret = 0;
Jiri Kosina2e574802010-03-25 14:15:11 +0100368 struct hidraw *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200369 void __user *user_arg = (void __user*) arg;
370
Jiri Kosina2e574802010-03-25 14:15:11 +0100371 mutex_lock(&minors_lock);
372 dev = hidraw_table[minor];
Alan Stern416dacb2019-08-21 13:27:12 -0400373 if (!dev || !dev->exist) {
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200374 ret = -ENODEV;
375 goto out;
376 }
Jiri Kosina2e574802010-03-25 14:15:11 +0100377
Jiri Kosina86166b72007-05-14 09:57:40 +0200378 switch (cmd) {
379 case HIDIOCGRDESCSIZE:
380 if (put_user(dev->hid->rsize, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200381 ret = -EFAULT;
382 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200383
384 case HIDIOCGRDESC:
385 {
386 __u32 len;
387
388 if (get_user(len, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200389 ret = -EFAULT;
390 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
391 ret = -EINVAL;
392 else if (copy_to_user(user_arg + offsetof(
393 struct hidraw_report_descriptor,
394 value[0]),
395 dev->hid->rdesc,
396 min(dev->hid->rsize, len)))
397 ret = -EFAULT;
398 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200399 }
400 case HIDIOCGRAWINFO:
401 {
402 struct hidraw_devinfo dinfo;
403
404 dinfo.bustype = dev->hid->bus;
405 dinfo.vendor = dev->hid->vendor;
406 dinfo.product = dev->hid->product;
407 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
Alan Cox979c4072008-05-26 11:25:26 +0200408 ret = -EFAULT;
409 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200410 }
411 default:
Jiri Kosina9188e792008-11-12 16:14:08 +0100412 {
413 struct hid_device *hid = dev->hid;
Alan Ottb4dbde92011-01-18 03:04:39 -0500414 if (_IOC_TYPE(cmd) != 'H') {
415 ret = -EINVAL;
416 break;
417 }
418
419 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
420 int len = _IOC_SIZE(cmd);
421 ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
422 break;
423 }
424 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
425 int len = _IOC_SIZE(cmd);
426 ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
427 break;
428 }
429
430 /* Begin Read-only ioctls. */
431 if (_IOC_DIR(cmd) != _IOC_READ) {
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300432 ret = -EINVAL;
433 break;
434 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100435
436 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200437 int len = strlen(hid->name) + 1;
Jiri Kosina9188e792008-11-12 16:14:08 +0100438 if (len > _IOC_SIZE(cmd))
439 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300440 ret = copy_to_user(user_arg, hid->name, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100441 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300442 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100443 }
444
445 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200446 int len = strlen(hid->phys) + 1;
Jiri Kosina9188e792008-11-12 16:14:08 +0100447 if (len > _IOC_SIZE(cmd))
448 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300449 ret = copy_to_user(user_arg, hid->phys, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100450 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300451 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100452 }
Marcel Holtmann2f488652019-12-04 04:41:09 +0100453
454 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWUNIQ(0))) {
455 int len = strlen(hid->uniq) + 1;
456 if (len > _IOC_SIZE(cmd))
457 len = _IOC_SIZE(cmd);
458 ret = copy_to_user(user_arg, hid->uniq, len) ?
459 -EFAULT : len;
460 break;
461 }
Alan Ottb4dbde92011-01-18 03:04:39 -0500462 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100463
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300464 ret = -ENOTTY;
Jiri Kosina86166b72007-05-14 09:57:40 +0200465 }
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200466out:
Jiri Kosina2e574802010-03-25 14:15:11 +0100467 mutex_unlock(&minors_lock);
Alan Cox979c4072008-05-26 11:25:26 +0200468 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200469}
470
471static const struct file_operations hidraw_ops = {
472 .owner = THIS_MODULE,
473 .read = hidraw_read,
474 .write = hidraw_write,
475 .poll = hidraw_poll,
476 .open = hidraw_open,
477 .release = hidraw_release,
Alan Cox979c4072008-05-26 11:25:26 +0200478 .unlocked_ioctl = hidraw_ioctl,
Andrew Dugganb5531312012-11-27 19:02:27 -0800479 .fasync = hidraw_fasync,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200480 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200481 .llseek = noop_llseek,
Jiri Kosina86166b72007-05-14 09:57:40 +0200482};
483
Jiri Kosinab6787242012-04-27 00:56:08 +0200484int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
Jiri Kosina86166b72007-05-14 09:57:40 +0200485{
486 struct hidraw *dev = hid->hidraw;
487 struct hidraw_list *list;
Jiri Kosinab6787242012-04-27 00:56:08 +0200488 int ret = 0;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800489 unsigned long flags;
Jiri Kosina86166b72007-05-14 09:57:40 +0200490
Yonghua Zheng277fe442013-08-26 23:38:35 +0800491 spin_lock_irqsave(&dev->list_lock, flags);
Jiri Kosina86166b72007-05-14 09:57:40 +0200492 list_for_each_entry(list, &dev->list, node) {
Matthieu CASTET4c7b4172012-06-28 16:51:56 +0200493 int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
494
495 if (new_head == list->tail)
496 continue;
497
Jiri Kosinab6787242012-04-27 00:56:08 +0200498 if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
499 ret = -ENOMEM;
500 break;
501 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200502 list->buffer[list->head].len = len;
Matthieu CASTET4c7b4172012-06-28 16:51:56 +0200503 list->head = new_head;
Jiri Kosina86166b72007-05-14 09:57:40 +0200504 kill_fasync(&list->fasync, SIGIO, POLL_IN);
505 }
Yonghua Zheng277fe442013-08-26 23:38:35 +0800506 spin_unlock_irqrestore(&dev->list_lock, flags);
Jiri Kosina86166b72007-05-14 09:57:40 +0200507
508 wake_up_interruptible(&dev->wait);
Jiri Kosinab6787242012-04-27 00:56:08 +0200509 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200510}
511EXPORT_SYMBOL_GPL(hidraw_report_event);
512
513int hidraw_connect(struct hid_device *hid)
514{
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200515 int minor, result;
Jiri Kosina86166b72007-05-14 09:57:40 +0200516 struct hidraw *dev;
517
Jiri Kosina61834532014-01-06 16:43:27 +0100518 /* we accept any HID device, all applications */
Jiri Kosina86166b72007-05-14 09:57:40 +0200519
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200520 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
521 if (!dev)
522 return -ENOMEM;
523
524 result = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200525
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100526 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200527
528 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
529 if (hidraw_table[minor])
530 continue;
531 hidraw_table[minor] = dev;
532 result = 0;
533 break;
534 }
535
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200536 if (result) {
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100537 mutex_unlock(&minors_lock);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200538 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200539 goto out;
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200540 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200541
Jiri Kosinaaae6c282008-12-04 16:16:46 +0100542 dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700543 NULL, "%s%d", "hidraw", minor);
Jiri Kosina86166b72007-05-14 09:57:40 +0200544
545 if (IS_ERR(dev->dev)) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200546 hidraw_table[minor] = NULL;
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100547 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200548 result = PTR_ERR(dev->dev);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200549 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200550 goto out;
551 }
552
553 init_waitqueue_head(&dev->wait);
Yonghua Zheng277fe442013-08-26 23:38:35 +0800554 spin_lock_init(&dev->list_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200555 INIT_LIST_HEAD(&dev->list);
556
557 dev->hid = hid;
558 dev->minor = minor;
559
560 dev->exist = 1;
561 hid->hidraw = dev;
562
Yonghua Zheng8e552e52013-07-30 14:16:10 +0800563 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200564out:
565 return result;
566
567}
568EXPORT_SYMBOL_GPL(hidraw_connect);
569
570void hidraw_disconnect(struct hid_device *hid)
571{
572 struct hidraw *hidraw = hid->hidraw;
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100573
574 mutex_lock(&minors_lock);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100575
Manoj Chourasia212a8712013-07-22 15:33:13 +0530576 drop_ref(hidraw, 1);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100577
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100578 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200579}
580EXPORT_SYMBOL_GPL(hidraw_disconnect);
581
582int __init hidraw_init(void)
583{
584 int result;
585 dev_t dev_id;
586
587 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
588 HIDRAW_MAX_DEVICES, "hidraw");
Jiri Kosina86166b72007-05-14 09:57:40 +0200589 if (result < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800590 pr_warn("can't get major number\n");
Jiri Kosina86166b72007-05-14 09:57:40 +0200591 goto out;
592 }
593
Dan Carpenter6edac6f2016-04-02 07:45:01 +0300594 hidraw_major = MAJOR(dev_id);
595
Jiri Kosina86166b72007-05-14 09:57:40 +0200596 hidraw_class = class_create(THIS_MODULE, "hidraw");
597 if (IS_ERR(hidraw_class)) {
598 result = PTR_ERR(hidraw_class);
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400599 goto error_cdev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200600 }
601
602 cdev_init(&hidraw_cdev, &hidraw_ops);
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400603 result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
604 if (result < 0)
605 goto error_class;
606
Rishi Guptab05cec62019-09-20 08:05:01 +0530607 pr_info("raw HID events driver (C) Jiri Kosina\n");
Jiri Kosina86166b72007-05-14 09:57:40 +0200608out:
609 return result;
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400610
611error_class:
612 class_destroy(hidraw_class);
613error_cdev:
614 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
615 goto out;
Jiri Kosina86166b72007-05-14 09:57:40 +0200616}
617
Jiri Slaby140ae3e2008-10-17 18:04:48 +0200618void hidraw_exit(void)
Jiri Kosina86166b72007-05-14 09:57:40 +0200619{
620 dev_t dev_id = MKDEV(hidraw_major, 0);
621
622 cdev_del(&hidraw_cdev);
623 class_destroy(hidraw_class);
624 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
625
626}