blob: 2bc762d31ac70de9724df166422f31ab1c8687f4 [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;
Greg Kroah-Hartman32944852023-06-20 20:31:43 +020035static const struct class hidraw_class = {
36 .name = "hidraw",
37};
Jiri Kosina86166b72007-05-14 09:57:40 +020038static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
André Almeida85902222021-11-30 10:29:57 -030039static DECLARE_RWSEM(minors_rwsem);
Jiri Kosina86166b72007-05-14 09:57:40 +020040
41static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
42{
43 struct hidraw_list *list = file->private_data;
44 int ret = 0, len;
Jiri Kosina86166b72007-05-14 09:57:40 +020045 DECLARE_WAITQUEUE(wait, current);
46
Jiri Kosinab0e14952009-10-12 11:25:56 +020047 mutex_lock(&list->read_mutex);
48
Jiri Kosina86166b72007-05-14 09:57:40 +020049 while (ret == 0) {
Jiri Kosina86166b72007-05-14 09:57:40 +020050 if (list->head == list->tail) {
51 add_wait_queue(&list->hidraw->wait, &wait);
52 set_current_state(TASK_INTERRUPTIBLE);
53
54 while (list->head == list->tail) {
Jiri Kosina86166b72007-05-14 09:57:40 +020055 if (signal_pending(current)) {
56 ret = -ERESTARTSYS;
57 break;
58 }
59 if (!list->hidraw->exist) {
60 ret = -EIO;
61 break;
62 }
Founder Fang7611e8d2012-11-21 15:20:31 +080063 if (file->f_flags & O_NONBLOCK) {
64 ret = -EAGAIN;
65 break;
66 }
Jiri Kosina86166b72007-05-14 09:57:40 +020067
68 /* allow O_NONBLOCK to work well from other threads */
69 mutex_unlock(&list->read_mutex);
70 schedule();
71 mutex_lock(&list->read_mutex);
72 set_current_state(TASK_INTERRUPTIBLE);
73 }
74
75 set_current_state(TASK_RUNNING);
76 remove_wait_queue(&list->hidraw->wait, &wait);
77 }
78
79 if (ret)
80 goto out;
81
Jiri Kosina86166b72007-05-14 09:57:40 +020082 len = list->buffer[list->tail].len > count ?
83 count : list->buffer[list->tail].len;
84
Jiri Kosinab6787242012-04-27 00:56:08 +020085 if (list->buffer[list->tail].value) {
86 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
87 ret = -EFAULT;
88 goto out;
89 }
90 ret = len;
Jiri Kosina86166b72007-05-14 09:57:40 +020091 }
Jiri Kosina86166b72007-05-14 09:57:40 +020092
93 kfree(list->buffer[list->tail].value);
Matthieu CASTET4c7b4172012-06-28 16:51:56 +020094 list->buffer[list->tail].value = NULL;
Jiri Kosina86166b72007-05-14 09:57:40 +020095 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
96 }
97out:
98 mutex_unlock(&list->read_mutex);
99 return ret;
100}
101
Jiri Kosina61834532014-01-06 16:43:27 +0100102/*
103 * The first byte of the report buffer is expected to be a report number.
Jiri Kosina61834532014-01-06 16:43:27 +0100104 */
Alan Ottb4dbde92011-01-18 03:04:39 -0500105static 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 +0200106{
Al Viro496ad9a2013-01-23 17:07:38 -0500107 unsigned int minor = iminor(file_inode(file));
Jiri Kosina2e574802010-03-25 14:15:11 +0100108 struct hid_device *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200109 __u8 *buf;
110 int ret = 0;
111
André Almeida85902222021-11-30 10:29:57 -0300112 lockdep_assert_held(&minors_rwsem);
Jiri Kosinacc7ed492018-11-08 22:38:42 +0100113
Manoj Chourasia212a8712013-07-22 15:33:13 +0530114 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
Antonio Ospitee42dee92010-10-05 17:20:17 +0200115 ret = -ENODEV;
116 goto out;
117 }
118
Jiri Kosina2e574802010-03-25 14:15:11 +0100119 dev = hidraw_table[minor]->hid;
120
Jiri Kosina2b107d62008-09-17 19:41:58 +0200121 if (count > HID_MAX_BUFFER_SIZE) {
Joe Perches4291ee32010-12-09 19:29:03 -0800122 hid_warn(dev, "pid %d passed too large report\n",
123 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100124 ret = -EINVAL;
125 goto out;
Jiri Kosina86166b72007-05-14 09:57:40 +0200126 }
127
128 if (count < 2) {
Joe Perches4291ee32010-12-09 19:29:03 -0800129 hid_warn(dev, "pid %d passed too short report\n",
130 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100131 ret = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200132 goto out;
133 }
134
Dmitry Torokhov8f507c82016-03-16 16:59:38 -0700135 buf = memdup_user(buffer, count);
136 if (IS_ERR(buf)) {
137 ret = PTR_ERR(buf);
Jiri Kosina2e574802010-03-25 14:15:11 +0100138 goto out;
139 }
140
Benjamin Tissoirese534a932014-03-08 22:52:42 -0500141 if ((report_type == HID_OUTPUT_REPORT) &&
142 !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
Benjamin Tissoires3a75b242014-02-20 15:24:50 -0500143 ret = hid_hw_output_report(dev, buf, count);
144 /*
145 * compatibility with old implementation of USB-HID and I2C-HID:
146 * if the device does not support receiving output reports,
147 * on an interrupt endpoint, fallback to SET_REPORT HID command.
148 */
149 if (ret != -ENOSYS)
150 goto out_free;
151 }
152
153 ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
154 HID_REQ_SET_REPORT);
155
Jiri Kosina2e574802010-03-25 14:15:11 +0100156out_free:
Jiri Kosina86166b72007-05-14 09:57:40 +0200157 kfree(buf);
Jiri Kosina2e574802010-03-25 14:15:11 +0100158out:
Alan Ottb4dbde92011-01-18 03:04:39 -0500159 return ret;
160}
161
Alan Ottb4dbde92011-01-18 03:04:39 -0500162static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
163{
164 ssize_t ret;
André Almeida85902222021-11-30 10:29:57 -0300165 down_read(&minors_rwsem);
Alan Ottb4dbde92011-01-18 03:04:39 -0500166 ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
André Almeida85902222021-11-30 10:29:57 -0300167 up_read(&minors_rwsem);
Jiri Kosina86166b72007-05-14 09:57:40 +0200168 return ret;
169}
170
Alan Ottb4dbde92011-01-18 03:04:39 -0500171
Jiri Kosina61834532014-01-06 16:43:27 +0100172/*
173 * This function performs a Get_Report transfer over the control endpoint
Jiri Kosinad2a1cfe2011-03-27 20:29:02 +0200174 * per section 7.2.1 of the HID specification, version 1.1. The first byte
Dean Cameraf43d3872020-11-26 09:39:57 +1100175 * of buffer is the report number to request, or 0x0 if the device does not
Jiri Kosinad2a1cfe2011-03-27 20:29:02 +0200176 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
Jiri Kosina61834532014-01-06 16:43:27 +0100177 * or HID_INPUT_REPORT.
Jiri Kosina61834532014-01-06 16:43:27 +0100178 */
Alan Ottb4dbde92011-01-18 03:04:39 -0500179static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
180{
Al Viro496ad9a2013-01-23 17:07:38 -0500181 unsigned int minor = iminor(file_inode(file));
Alan Ottb4dbde92011-01-18 03:04:39 -0500182 struct hid_device *dev;
183 __u8 *buf;
184 int ret = 0, len;
185 unsigned char report_number;
186
André Almeida85902222021-11-30 10:29:57 -0300187 lockdep_assert_held(&minors_rwsem);
Jiri Kosinacc7ed492018-11-08 22:38:42 +0100188
Rodrigo Rivas Costaa9553582018-04-06 01:09:36 +0200189 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
190 ret = -ENODEV;
191 goto out;
192 }
193
Alan Ottb4dbde92011-01-18 03:04:39 -0500194 dev = hidraw_table[minor]->hid;
195
Benjamin Tissoirescafebc02014-02-05 16:33:22 -0500196 if (!dev->ll_driver->raw_request) {
Alan Ottb4dbde92011-01-18 03:04:39 -0500197 ret = -ENODEV;
198 goto out;
199 }
200
201 if (count > HID_MAX_BUFFER_SIZE) {
Rishi Guptab05cec62019-09-20 08:05:01 +0530202 hid_warn(dev, "pid %d passed too large report\n",
203 task_pid_nr(current));
Alan Ottb4dbde92011-01-18 03:04:39 -0500204 ret = -EINVAL;
205 goto out;
206 }
207
208 if (count < 2) {
Rishi Guptab05cec62019-09-20 08:05:01 +0530209 hid_warn(dev, "pid %d passed too short report\n",
210 task_pid_nr(current));
Alan Ottb4dbde92011-01-18 03:04:39 -0500211 ret = -EINVAL;
212 goto out;
213 }
214
Kees Cook6da2ec52018-06-12 13:55:00 -0700215 buf = kmalloc(count, GFP_KERNEL);
Alan Ottb4dbde92011-01-18 03:04:39 -0500216 if (!buf) {
217 ret = -ENOMEM;
218 goto out;
219 }
220
Jiri Kosina61834532014-01-06 16:43:27 +0100221 /*
222 * Read the first byte from the user. This is the report number,
Benjamin Tissoirescafebc02014-02-05 16:33:22 -0500223 * which is passed to hid_hw_raw_request().
Jiri Kosina61834532014-01-06 16:43:27 +0100224 */
Alan Ottb4dbde92011-01-18 03:04:39 -0500225 if (copy_from_user(&report_number, buffer, 1)) {
226 ret = -EFAULT;
227 goto out_free;
228 }
229
Benjamin Tissoirescafebc02014-02-05 16:33:22 -0500230 ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
231 HID_REQ_GET_REPORT);
Alan Ottb4dbde92011-01-18 03:04:39 -0500232
233 if (ret < 0)
234 goto out_free;
235
236 len = (ret < count) ? ret : count;
237
238 if (copy_to_user(buffer, buf, len)) {
239 ret = -EFAULT;
240 goto out_free;
241 }
242
243 ret = len;
244
245out_free:
246 kfree(buf);
247out:
248 return ret;
249}
250
Al Viroafc9a422017-07-03 06:39:46 -0400251static __poll_t hidraw_poll(struct file *file, poll_table *wait)
Jiri Kosina86166b72007-05-14 09:57:40 +0200252{
253 struct hidraw_list *list = file->private_data;
Jiri Kosina9e635c22020-01-10 15:32:51 +0100254 __poll_t mask = EPOLLOUT | EPOLLWRNORM; /* hidraw is always writable */
Jiri Kosina86166b72007-05-14 09:57:40 +0200255
256 poll_wait(file, &list->hidraw->wait, wait);
257 if (list->head != list->tail)
Jiri Kosina9e635c22020-01-10 15:32:51 +0100258 mask |= EPOLLIN | EPOLLRDNORM;
Jiri Kosina86166b72007-05-14 09:57:40 +0200259 if (!list->hidraw->exist)
Jiri Kosina9e635c22020-01-10 15:32:51 +0100260 mask |= EPOLLERR | EPOLLHUP;
261 return mask;
Jiri Kosina86166b72007-05-14 09:57:40 +0200262}
263
264static int hidraw_open(struct inode *inode, struct file *file)
265{
266 unsigned int minor = iminor(inode);
267 struct hidraw *dev;
268 struct hidraw_list *list;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800269 unsigned long flags;
Jiri Kosina86166b72007-05-14 09:57:40 +0200270 int err = 0;
271
272 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
273 err = -ENOMEM;
274 goto out;
275 }
276
Ludvig Michaelsson944ee772023-06-21 13:17:43 +0200277 /*
278 * Technically not writing to the hidraw_table but a write lock is
279 * required to protect the device refcount. This is symmetrical to
280 * hidraw_release().
281 */
282 down_write(&minors_rwsem);
Manoj Chourasia212a8712013-07-22 15:33:13 +0530283 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200284 err = -ENODEV;
285 goto out_unlock;
286 }
287
Jiri Kosina86166b72007-05-14 09:57:40 +0200288 dev = hidraw_table[minor];
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100289 if (!dev->open++) {
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800290 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
Amit Nagalf554ff82011-09-27 13:41:58 -0400291 if (err < 0) {
292 dev->open--;
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800293 goto out_unlock;
Amit Nagalf554ff82011-09-27 13:41:58 -0400294 }
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800295
296 err = hid_hw_open(dev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100297 if (err < 0) {
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800298 hid_hw_power(dev->hid, PM_HINT_NORMAL);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100299 dev->open--;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800300 goto out_unlock;
Oliver Neukum0361a282008-12-17 15:38:03 +0100301 }
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100302 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200303
Yonghua Zheng277fe442013-08-26 23:38:35 +0800304 list->hidraw = hidraw_table[minor];
305 mutex_init(&list->read_mutex);
306 spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
307 list_add_tail(&list->node, &hidraw_table[minor]->list);
308 spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
309 file->private_data = list;
Jiri Kosina86166b72007-05-14 09:57:40 +0200310out_unlock:
Ludvig Michaelsson944ee772023-06-21 13:17:43 +0200311 up_write(&minors_rwsem);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100312out:
Amit Nagal1a896232011-09-07 13:48:47 +0200313 if (err < 0)
314 kfree(list);
Jiri Kosina86166b72007-05-14 09:57:40 +0200315 return err;
316
317}
318
Andrew Dugganb5531312012-11-27 19:02:27 -0800319static int hidraw_fasync(int fd, struct file *file, int on)
320{
321 struct hidraw_list *list = file->private_data;
322
323 return fasync_helper(fd, file, on, &list->fasync);
324}
325
Manoj Chourasia212a8712013-07-22 15:33:13 +0530326static void drop_ref(struct hidraw *hidraw, int exists_bit)
327{
328 if (exists_bit) {
Manoj Chourasia212a8712013-07-22 15:33:13 +0530329 hidraw->exist = 0;
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530330 if (hidraw->open) {
331 hid_hw_close(hidraw->hid);
Manoj Chourasia212a8712013-07-22 15:33:13 +0530332 wake_up_interruptible(&hidraw->wait);
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530333 }
Greg Kroah-Hartman32944852023-06-20 20:31:43 +0200334 device_destroy(&hidraw_class,
Fernando Luis Vázquez Cao47587fc2014-02-26 16:51:24 +0900335 MKDEV(hidraw_major, hidraw->minor));
Manoj Chourasia212a8712013-07-22 15:33:13 +0530336 } else {
337 --hidraw->open;
338 }
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530339 if (!hidraw->open) {
340 if (!hidraw->exist) {
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530341 hidraw_table[hidraw->minor] = NULL;
342 kfree(hidraw);
343 } else {
344 /* close device for last reader */
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530345 hid_hw_close(hidraw->hid);
Dmitry Torokhov814b6d12017-09-19 18:37:46 -0700346 hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
Manoj Chourasia0f5a24c2013-10-01 15:39:00 +0530347 }
Manoj Chourasia212a8712013-07-22 15:33:13 +0530348 }
349}
350
Jiri Kosina86166b72007-05-14 09:57:40 +0200351static int hidraw_release(struct inode * inode, struct file * file)
352{
353 unsigned int minor = iminor(inode);
Jiri Kosina86166b72007-05-14 09:57:40 +0200354 struct hidraw_list *list = file->private_data;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800355 unsigned long flags;
Jiri Kosina86166b72007-05-14 09:57:40 +0200356
André Almeida85902222021-11-30 10:29:57 -0300357 down_write(&minors_rwsem);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100358
Yonghua Zheng277fe442013-08-26 23:38:35 +0800359 spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
Su Huia3bdcdd2024-01-25 14:32:26 +0800360 while (list->tail != list->head) {
361 kfree(list->buffer[list->tail].value);
362 list->buffer[list->tail].value = NULL;
363 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
364 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200365 list_del(&list->node);
Yonghua Zheng277fe442013-08-26 23:38:35 +0800366 spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
Jiri Kosina4db1c622008-06-24 14:45:27 +0200367 kfree(list);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100368
Manoj Chourasia212a8712013-07-22 15:33:13 +0530369 drop_ref(hidraw_table[minor], 0);
370
André Almeida85902222021-11-30 10:29:57 -0300371 up_write(&minors_rwsem);
Manoj Chourasia212a8712013-07-22 15:33:13 +0530372 return 0;
Jiri Kosina86166b72007-05-14 09:57:40 +0200373}
374
Alan Cox979c4072008-05-26 11:25:26 +0200375static long hidraw_ioctl(struct file *file, unsigned int cmd,
376 unsigned long arg)
Jiri Kosina86166b72007-05-14 09:57:40 +0200377{
Al Viro496ad9a2013-01-23 17:07:38 -0500378 struct inode *inode = file_inode(file);
Jiri Kosina86166b72007-05-14 09:57:40 +0200379 unsigned int minor = iminor(inode);
Alan Cox979c4072008-05-26 11:25:26 +0200380 long ret = 0;
Jiri Kosina2e574802010-03-25 14:15:11 +0100381 struct hidraw *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200382 void __user *user_arg = (void __user*) arg;
383
André Almeida85902222021-11-30 10:29:57 -0300384 down_read(&minors_rwsem);
Jiri Kosina2e574802010-03-25 14:15:11 +0100385 dev = hidraw_table[minor];
Alan Stern416dacb2019-08-21 13:27:12 -0400386 if (!dev || !dev->exist) {
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200387 ret = -ENODEV;
388 goto out;
389 }
Jiri Kosina2e574802010-03-25 14:15:11 +0100390
Jiri Kosina86166b72007-05-14 09:57:40 +0200391 switch (cmd) {
392 case HIDIOCGRDESCSIZE:
393 if (put_user(dev->hid->rsize, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200394 ret = -EFAULT;
395 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200396
397 case HIDIOCGRDESC:
398 {
399 __u32 len;
400
401 if (get_user(len, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200402 ret = -EFAULT;
403 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
404 ret = -EINVAL;
405 else if (copy_to_user(user_arg + offsetof(
406 struct hidraw_report_descriptor,
407 value[0]),
408 dev->hid->rdesc,
409 min(dev->hid->rsize, len)))
410 ret = -EFAULT;
411 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200412 }
413 case HIDIOCGRAWINFO:
414 {
415 struct hidraw_devinfo dinfo;
416
417 dinfo.bustype = dev->hid->bus;
418 dinfo.vendor = dev->hid->vendor;
419 dinfo.product = dev->hid->product;
420 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
Alan Cox979c4072008-05-26 11:25:26 +0200421 ret = -EFAULT;
422 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200423 }
424 default:
Jiri Kosina9188e792008-11-12 16:14:08 +0100425 {
426 struct hid_device *hid = dev->hid;
Alan Ottb4dbde92011-01-18 03:04:39 -0500427 if (_IOC_TYPE(cmd) != 'H') {
428 ret = -EINVAL;
429 break;
430 }
431
432 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
433 int len = _IOC_SIZE(cmd);
434 ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
435 break;
436 }
437 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
438 int len = _IOC_SIZE(cmd);
439 ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
440 break;
441 }
442
Dean Cameraf43d3872020-11-26 09:39:57 +1100443 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSINPUT(0))) {
444 int len = _IOC_SIZE(cmd);
445 ret = hidraw_send_report(file, user_arg, len, HID_INPUT_REPORT);
446 break;
447 }
448 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGINPUT(0))) {
449 int len = _IOC_SIZE(cmd);
450 ret = hidraw_get_report(file, user_arg, len, HID_INPUT_REPORT);
451 break;
452 }
453
454 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSOUTPUT(0))) {
455 int len = _IOC_SIZE(cmd);
456 ret = hidraw_send_report(file, user_arg, len, HID_OUTPUT_REPORT);
457 break;
458 }
459 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGOUTPUT(0))) {
460 int len = _IOC_SIZE(cmd);
461 ret = hidraw_get_report(file, user_arg, len, HID_OUTPUT_REPORT);
462 break;
463 }
464
Alan Ottb4dbde92011-01-18 03:04:39 -0500465 /* Begin Read-only ioctls. */
466 if (_IOC_DIR(cmd) != _IOC_READ) {
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300467 ret = -EINVAL;
468 break;
469 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100470
471 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
Daniel Mackdd2ed4872011-05-15 18:07:42 +0200472 int len = strlen(hid->name) + 1;
Jiri Kosina9188e792008-11-12 16:14:08 +0100473 if (len > _IOC_SIZE(cmd))
474 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300475 ret = copy_to_user(user_arg, hid->name, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100476 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300477 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100478 }
479
480 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
Daniel Mackdd2ed4872011-05-15 18:07:42 +0200481 int len = strlen(hid->phys) + 1;
Jiri Kosina9188e792008-11-12 16:14:08 +0100482 if (len > _IOC_SIZE(cmd))
483 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300484 ret = copy_to_user(user_arg, hid->phys, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100485 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300486 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100487 }
Marcel Holtmann2f488652019-12-04 04:41:09 +0100488
489 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWUNIQ(0))) {
490 int len = strlen(hid->uniq) + 1;
491 if (len > _IOC_SIZE(cmd))
492 len = _IOC_SIZE(cmd);
493 ret = copy_to_user(user_arg, hid->uniq, len) ?
494 -EFAULT : len;
495 break;
496 }
Alan Ottb4dbde92011-01-18 03:04:39 -0500497 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100498
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300499 ret = -ENOTTY;
Jiri Kosina86166b72007-05-14 09:57:40 +0200500 }
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200501out:
André Almeida85902222021-11-30 10:29:57 -0300502 up_read(&minors_rwsem);
Alan Cox979c4072008-05-26 11:25:26 +0200503 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200504}
505
506static const struct file_operations hidraw_ops = {
507 .owner = THIS_MODULE,
508 .read = hidraw_read,
509 .write = hidraw_write,
510 .poll = hidraw_poll,
511 .open = hidraw_open,
512 .release = hidraw_release,
Alan Cox979c4072008-05-26 11:25:26 +0200513 .unlocked_ioctl = hidraw_ioctl,
Andrew Dugganb5531312012-11-27 19:02:27 -0800514 .fasync = hidraw_fasync,
Arnd Bergmann1832f2d2018-09-11 21:59:08 +0200515 .compat_ioctl = compat_ptr_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200516 .llseek = noop_llseek,
Jiri Kosina86166b72007-05-14 09:57:40 +0200517};
518
Jiri Kosinab6787242012-04-27 00:56:08 +0200519int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
Jiri Kosina86166b72007-05-14 09:57:40 +0200520{
521 struct hidraw *dev = hid->hidraw;
522 struct hidraw_list *list;
Jiri Kosinab6787242012-04-27 00:56:08 +0200523 int ret = 0;
Yonghua Zheng277fe442013-08-26 23:38:35 +0800524 unsigned long flags;
Jiri Kosina86166b72007-05-14 09:57:40 +0200525
Yonghua Zheng277fe442013-08-26 23:38:35 +0800526 spin_lock_irqsave(&dev->list_lock, flags);
Jiri Kosina86166b72007-05-14 09:57:40 +0200527 list_for_each_entry(list, &dev->list, node) {
Matthieu CASTET4c7b4172012-06-28 16:51:56 +0200528 int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
529
530 if (new_head == list->tail)
531 continue;
532
Jiri Kosinab6787242012-04-27 00:56:08 +0200533 if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
534 ret = -ENOMEM;
535 break;
536 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200537 list->buffer[list->head].len = len;
Matthieu CASTET4c7b4172012-06-28 16:51:56 +0200538 list->head = new_head;
Jiri Kosina86166b72007-05-14 09:57:40 +0200539 kill_fasync(&list->fasync, SIGIO, POLL_IN);
540 }
Yonghua Zheng277fe442013-08-26 23:38:35 +0800541 spin_unlock_irqrestore(&dev->list_lock, flags);
Jiri Kosina86166b72007-05-14 09:57:40 +0200542
543 wake_up_interruptible(&dev->wait);
Jiri Kosinab6787242012-04-27 00:56:08 +0200544 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200545}
546EXPORT_SYMBOL_GPL(hidraw_report_event);
547
548int hidraw_connect(struct hid_device *hid)
549{
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200550 int minor, result;
Jiri Kosina86166b72007-05-14 09:57:40 +0200551 struct hidraw *dev;
552
Jiri Kosina61834532014-01-06 16:43:27 +0100553 /* we accept any HID device, all applications */
Jiri Kosina86166b72007-05-14 09:57:40 +0200554
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200555 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
556 if (!dev)
557 return -ENOMEM;
558
559 result = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200560
André Almeida85902222021-11-30 10:29:57 -0300561 down_write(&minors_rwsem);
Jiri Kosina86166b72007-05-14 09:57:40 +0200562
563 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
564 if (hidraw_table[minor])
565 continue;
566 hidraw_table[minor] = dev;
567 result = 0;
568 break;
569 }
570
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200571 if (result) {
André Almeida85902222021-11-30 10:29:57 -0300572 up_write(&minors_rwsem);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200573 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200574 goto out;
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200575 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200576
Greg Kroah-Hartman32944852023-06-20 20:31:43 +0200577 dev->dev = device_create(&hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700578 NULL, "%s%d", "hidraw", minor);
Jiri Kosina86166b72007-05-14 09:57:40 +0200579
580 if (IS_ERR(dev->dev)) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200581 hidraw_table[minor] = NULL;
André Almeida85902222021-11-30 10:29:57 -0300582 up_write(&minors_rwsem);
Jiri Kosina86166b72007-05-14 09:57:40 +0200583 result = PTR_ERR(dev->dev);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200584 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200585 goto out;
586 }
587
588 init_waitqueue_head(&dev->wait);
Yonghua Zheng277fe442013-08-26 23:38:35 +0800589 spin_lock_init(&dev->list_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200590 INIT_LIST_HEAD(&dev->list);
591
592 dev->hid = hid;
593 dev->minor = minor;
594
595 dev->exist = 1;
596 hid->hidraw = dev;
597
André Almeida85902222021-11-30 10:29:57 -0300598 up_write(&minors_rwsem);
Jiri Kosina86166b72007-05-14 09:57:40 +0200599out:
600 return result;
601
602}
603EXPORT_SYMBOL_GPL(hidraw_connect);
604
605void hidraw_disconnect(struct hid_device *hid)
606{
607 struct hidraw *hidraw = hid->hidraw;
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100608
André Almeida85902222021-11-30 10:29:57 -0300609 down_write(&minors_rwsem);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100610
Manoj Chourasia212a8712013-07-22 15:33:13 +0530611 drop_ref(hidraw, 1);
Jiri Kosinadf0cfd62012-11-01 11:33:26 +0100612
André Almeida85902222021-11-30 10:29:57 -0300613 up_write(&minors_rwsem);
Jiri Kosina86166b72007-05-14 09:57:40 +0200614}
615EXPORT_SYMBOL_GPL(hidraw_disconnect);
616
617int __init hidraw_init(void)
618{
619 int result;
620 dev_t dev_id;
621
622 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
623 HIDRAW_MAX_DEVICES, "hidraw");
Jiri Kosina86166b72007-05-14 09:57:40 +0200624 if (result < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800625 pr_warn("can't get major number\n");
Jiri Kosina86166b72007-05-14 09:57:40 +0200626 goto out;
627 }
628
Dan Carpenter6edac6f2016-04-02 07:45:01 +0300629 hidraw_major = MAJOR(dev_id);
630
Greg Kroah-Hartman32944852023-06-20 20:31:43 +0200631 result = class_register(&hidraw_class);
632 if (result)
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400633 goto error_cdev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200634
635 cdev_init(&hidraw_cdev, &hidraw_ops);
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400636 result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
637 if (result < 0)
638 goto error_class;
639
Rishi Guptab05cec62019-09-20 08:05:01 +0530640 pr_info("raw HID events driver (C) Jiri Kosina\n");
Jiri Kosina86166b72007-05-14 09:57:40 +0200641out:
642 return result;
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400643
644error_class:
Greg Kroah-Hartman32944852023-06-20 20:31:43 +0200645 class_unregister(&hidraw_class);
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400646error_cdev:
647 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
648 goto out;
Jiri Kosina86166b72007-05-14 09:57:40 +0200649}
650
Jiri Slaby140ae3e2008-10-17 18:04:48 +0200651void hidraw_exit(void)
Jiri Kosina86166b72007-05-14 09:57:40 +0200652{
653 dev_t dev_id = MKDEV(hidraw_major, 0);
654
655 cdev_del(&hidraw_cdev);
Greg Kroah-Hartman32944852023-06-20 20:31:43 +0200656 class_unregister(&hidraw_class);
Jiri Kosina86166b72007-05-14 09:57:40 +0200657 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
658
659}