blob: 17d15bb610d15ac4b544e11af3007cd5b6357fd8 [file] [log] [blame]
Jiri Kosina86166b72007-05-14 09:57:40 +02001/*
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 Perches4291ee32010-12-09 19:29:03 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Jiri Kosina86166b72007-05-14 09:57:40 +020024#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 Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020034#include <linux/hid.h>
35#include <linux/mutex.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040036#include <linux/sched.h>
Jiri Kosina86166b72007-05-14 09:57:40 +020037
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];
Oliver Neukum7d672cd2008-10-31 00:07:23 +010044static DEFINE_MUTEX(minors_lock);
Ratan Nalumasu4fe9f8e2012-09-22 11:46:30 -070045static void drop_ref(struct hidraw *hid, int exists_bit);
Jiri Kosina86166b72007-05-14 09:57:40 +020046
47static 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 Kosina86166b72007-05-14 09:57:40 +020051 DECLARE_WAITQUEUE(wait, current);
52
Jiri Kosinab0e14952009-10-12 11:25:56 +020053 mutex_lock(&list->read_mutex);
54
Jiri Kosina86166b72007-05-14 09:57:40 +020055 while (ret == 0) {
Jiri Kosina86166b72007-05-14 09:57:40 +020056 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 Kosina86166b72007-05-14 09:57:40 +020088 len = list->buffer[list->tail].len > count ?
89 count : list->buffer[list->tail].len;
90
Jiri Kosinab6787242012-04-27 00:56:08 +020091 if (list->buffer[list->tail].value) {
92 if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
93 ret = -EFAULT;
94 goto out;
95 }
96 ret = len;
Jiri Kosina86166b72007-05-14 09:57:40 +020097 }
Jiri Kosina86166b72007-05-14 09:57:40 +020098
99 kfree(list->buffer[list->tail].value);
Matthieu CASTET4c7b4172012-06-28 16:51:56 +0200100 list->buffer[list->tail].value = NULL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200101 list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
102 }
103out:
104 mutex_unlock(&list->read_mutex);
105 return ret;
106}
107
Jiri Kosinad2a1cfe2011-03-27 20:29:02 +0200108/* The first byte is expected to be a report number.
109 * This function is to be called with the minors_lock mutex held */
Alan Ottb4dbde92011-01-18 03:04:39 -0500110static 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 +0200111{
112 unsigned int minor = iminor(file->f_path.dentry->d_inode);
Jiri Kosina2e574802010-03-25 14:15:11 +0100113 struct hid_device *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200114 __u8 *buf;
115 int ret = 0;
116
Ratan Nalumasu4fe9f8e2012-09-22 11:46:30 -0700117 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
Antonio Ospitee42dee92010-10-05 17:20:17 +0200118 ret = -ENODEV;
119 goto out;
120 }
121
Jiri Kosina2e574802010-03-25 14:15:11 +0100122 dev = hidraw_table[minor]->hid;
123
124 if (!dev->hid_output_raw_report) {
125 ret = -ENODEV;
126 goto out;
127 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200128
Jiri Kosina2b107d62008-09-17 19:41:58 +0200129 if (count > HID_MAX_BUFFER_SIZE) {
Joe Perches4291ee32010-12-09 19:29:03 -0800130 hid_warn(dev, "pid %d passed too large report\n",
131 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100132 ret = -EINVAL;
133 goto out;
Jiri Kosina86166b72007-05-14 09:57:40 +0200134 }
135
136 if (count < 2) {
Joe Perches4291ee32010-12-09 19:29:03 -0800137 hid_warn(dev, "pid %d passed too short report\n",
138 task_pid_nr(current));
Jiri Kosina2e574802010-03-25 14:15:11 +0100139 ret = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200140 goto out;
141 }
142
Jiri Kosina2e574802010-03-25 14:15:11 +0100143 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
144 if (!buf) {
145 ret = -ENOMEM;
146 goto out;
147 }
148
149 if (copy_from_user(buf, buffer, count)) {
150 ret = -EFAULT;
151 goto out_free;
152 }
153
Alan Ottb4dbde92011-01-18 03:04:39 -0500154 ret = dev->hid_output_raw_report(dev, buf, count, report_type);
Jiri Kosina2e574802010-03-25 14:15:11 +0100155out_free:
Jiri Kosina86166b72007-05-14 09:57:40 +0200156 kfree(buf);
Jiri Kosina2e574802010-03-25 14:15:11 +0100157out:
Alan Ottb4dbde92011-01-18 03:04:39 -0500158 return ret;
159}
160
161/* the first byte is expected to be a report number */
162static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
163{
164 ssize_t ret;
165 mutex_lock(&minors_lock);
166 ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
Jiri Kosina2e574802010-03-25 14:15:11 +0100167 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200168 return ret;
169}
170
Alan Ottb4dbde92011-01-18 03:04:39 -0500171
172/* This function performs a Get_Report transfer over the control endpoint
Jiri Kosinad2a1cfe2011-03-27 20:29:02 +0200173 * per section 7.2.1 of the HID specification, version 1.1. The first byte
174 * of buffer is the report number to request, or 0x0 if the defice does not
175 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
176 * or HID_INPUT_REPORT. This function is to be called with the minors_lock
177 * mutex held. */
Alan Ottb4dbde92011-01-18 03:04:39 -0500178static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
179{
180 unsigned int minor = iminor(file->f_path.dentry->d_inode);
181 struct hid_device *dev;
182 __u8 *buf;
183 int ret = 0, len;
184 unsigned char report_number;
185
186 dev = hidraw_table[minor]->hid;
187
188 if (!dev->hid_get_raw_report) {
189 ret = -ENODEV;
190 goto out;
191 }
192
193 if (count > HID_MAX_BUFFER_SIZE) {
194 printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
195 task_pid_nr(current));
196 ret = -EINVAL;
197 goto out;
198 }
199
200 if (count < 2) {
201 printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
202 task_pid_nr(current));
203 ret = -EINVAL;
204 goto out;
205 }
206
207 buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
208 if (!buf) {
209 ret = -ENOMEM;
210 goto out;
211 }
212
213 /* Read the first byte from the user. This is the report number,
Jiri Kosinad2a1cfe2011-03-27 20:29:02 +0200214 * which is passed to dev->hid_get_raw_report(). */
Alan Ottb4dbde92011-01-18 03:04:39 -0500215 if (copy_from_user(&report_number, buffer, 1)) {
216 ret = -EFAULT;
217 goto out_free;
218 }
219
220 ret = dev->hid_get_raw_report(dev, report_number, buf, count, report_type);
221
222 if (ret < 0)
223 goto out_free;
224
225 len = (ret < count) ? ret : count;
226
227 if (copy_to_user(buffer, buf, len)) {
228 ret = -EFAULT;
229 goto out_free;
230 }
231
232 ret = len;
233
234out_free:
235 kfree(buf);
236out:
237 return ret;
238}
239
Jiri Kosina86166b72007-05-14 09:57:40 +0200240static unsigned int hidraw_poll(struct file *file, poll_table *wait)
241{
242 struct hidraw_list *list = file->private_data;
243
244 poll_wait(file, &list->hidraw->wait, wait);
245 if (list->head != list->tail)
246 return POLLIN | POLLRDNORM;
247 if (!list->hidraw->exist)
248 return POLLERR | POLLHUP;
249 return 0;
250}
251
252static int hidraw_open(struct inode *inode, struct file *file)
253{
254 unsigned int minor = iminor(inode);
255 struct hidraw *dev;
256 struct hidraw_list *list;
257 int err = 0;
258
259 if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
260 err = -ENOMEM;
261 goto out;
262 }
263
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100264 mutex_lock(&minors_lock);
Ratan Nalumasu4fe9f8e2012-09-22 11:46:30 -0700265 if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200266 err = -ENODEV;
267 goto out_unlock;
268 }
269
270 list->hidraw = hidraw_table[minor];
271 mutex_init(&list->read_mutex);
272 list_add_tail(&list->node, &hidraw_table[minor]->list);
273 file->private_data = list;
274
275 dev = hidraw_table[minor];
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100276 if (!dev->open++) {
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800277 err = hid_hw_power(dev->hid, PM_HINT_FULLON);
Amit Nagalf554ff82011-09-27 13:41:58 -0400278 if (err < 0) {
279 dev->open--;
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800280 goto out_unlock;
Amit Nagalf554ff82011-09-27 13:41:58 -0400281 }
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800282
283 err = hid_hw_open(dev->hid);
Oliver Neukum0361a282008-12-17 15:38:03 +0100284 if (err < 0) {
Dmitry Torokhov5bea7662010-12-07 23:02:48 -0800285 hid_hw_power(dev->hid, PM_HINT_NORMAL);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100286 dev->open--;
Oliver Neukum0361a282008-12-17 15:38:03 +0100287 }
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100288 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200289
290out_unlock:
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100291 mutex_unlock(&minors_lock);
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100292out:
Amit Nagal1a896232011-09-07 13:48:47 +0200293 if (err < 0)
294 kfree(list);
Jiri Kosina86166b72007-05-14 09:57:40 +0200295 return err;
296
297}
298
299static int hidraw_release(struct inode * inode, struct file * file)
300{
301 unsigned int minor = iminor(inode);
Jiri Kosina86166b72007-05-14 09:57:40 +0200302 struct hidraw_list *list = file->private_data;
303
Ratan Nalumasu4fe9f8e2012-09-22 11:46:30 -0700304 drop_ref(hidraw_table[minor], 0);
Jiri Kosina86166b72007-05-14 09:57:40 +0200305 list_del(&list->node);
Jiri Kosina4db1c622008-06-24 14:45:27 +0200306 kfree(list);
Ratan Nalumasu4fe9f8e2012-09-22 11:46:30 -0700307 return 0;
Jiri Kosina86166b72007-05-14 09:57:40 +0200308}
309
Alan Cox979c4072008-05-26 11:25:26 +0200310static long hidraw_ioctl(struct file *file, unsigned int cmd,
311 unsigned long arg)
Jiri Kosina86166b72007-05-14 09:57:40 +0200312{
Alan Cox979c4072008-05-26 11:25:26 +0200313 struct inode *inode = file->f_path.dentry->d_inode;
Jiri Kosina86166b72007-05-14 09:57:40 +0200314 unsigned int minor = iminor(inode);
Alan Cox979c4072008-05-26 11:25:26 +0200315 long ret = 0;
Jiri Kosina2e574802010-03-25 14:15:11 +0100316 struct hidraw *dev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200317 void __user *user_arg = (void __user*) arg;
318
Jiri Kosina2e574802010-03-25 14:15:11 +0100319 mutex_lock(&minors_lock);
320 dev = hidraw_table[minor];
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200321 if (!dev) {
322 ret = -ENODEV;
323 goto out;
324 }
Jiri Kosina2e574802010-03-25 14:15:11 +0100325
Jiri Kosina86166b72007-05-14 09:57:40 +0200326 switch (cmd) {
327 case HIDIOCGRDESCSIZE:
328 if (put_user(dev->hid->rsize, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200329 ret = -EFAULT;
330 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200331
332 case HIDIOCGRDESC:
333 {
334 __u32 len;
335
336 if (get_user(len, (int __user *)arg))
Alan Cox979c4072008-05-26 11:25:26 +0200337 ret = -EFAULT;
338 else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
339 ret = -EINVAL;
340 else if (copy_to_user(user_arg + offsetof(
341 struct hidraw_report_descriptor,
342 value[0]),
343 dev->hid->rdesc,
344 min(dev->hid->rsize, len)))
345 ret = -EFAULT;
346 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200347 }
348 case HIDIOCGRAWINFO:
349 {
350 struct hidraw_devinfo dinfo;
351
352 dinfo.bustype = dev->hid->bus;
353 dinfo.vendor = dev->hid->vendor;
354 dinfo.product = dev->hid->product;
355 if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
Alan Cox979c4072008-05-26 11:25:26 +0200356 ret = -EFAULT;
357 break;
Jiri Kosina86166b72007-05-14 09:57:40 +0200358 }
359 default:
Jiri Kosina9188e792008-11-12 16:14:08 +0100360 {
361 struct hid_device *hid = dev->hid;
Alan Ottb4dbde92011-01-18 03:04:39 -0500362 if (_IOC_TYPE(cmd) != 'H') {
363 ret = -EINVAL;
364 break;
365 }
366
367 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
368 int len = _IOC_SIZE(cmd);
369 ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
370 break;
371 }
372 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
373 int len = _IOC_SIZE(cmd);
374 ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
375 break;
376 }
377
378 /* Begin Read-only ioctls. */
379 if (_IOC_DIR(cmd) != _IOC_READ) {
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300380 ret = -EINVAL;
381 break;
382 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100383
384 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200385 int len = strlen(hid->name) + 1;
Jiri Kosina9188e792008-11-12 16:14:08 +0100386 if (len > _IOC_SIZE(cmd))
387 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300388 ret = copy_to_user(user_arg, hid->name, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100389 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300390 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100391 }
392
393 if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
Daniel Mackdd2ed482011-05-15 18:07:42 +0200394 int len = strlen(hid->phys) + 1;
Jiri Kosina9188e792008-11-12 16:14:08 +0100395 if (len > _IOC_SIZE(cmd))
396 len = _IOC_SIZE(cmd);
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300397 ret = copy_to_user(user_arg, hid->phys, len) ?
Jiri Kosina9188e792008-11-12 16:14:08 +0100398 -EFAULT : len;
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300399 break;
Jiri Kosina9188e792008-11-12 16:14:08 +0100400 }
Alan Ottb4dbde92011-01-18 03:04:39 -0500401 }
Jiri Kosina9188e792008-11-12 16:14:08 +0100402
Dan Carpenterdfd395af2009-02-03 16:35:17 +0300403 ret = -ENOTTY;
Jiri Kosina86166b72007-05-14 09:57:40 +0200404 }
Antonio Ospited20d5ff2010-10-05 17:20:16 +0200405out:
Jiri Kosina2e574802010-03-25 14:15:11 +0100406 mutex_unlock(&minors_lock);
Alan Cox979c4072008-05-26 11:25:26 +0200407 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200408}
409
410static const struct file_operations hidraw_ops = {
411 .owner = THIS_MODULE,
412 .read = hidraw_read,
413 .write = hidraw_write,
414 .poll = hidraw_poll,
415 .open = hidraw_open,
416 .release = hidraw_release,
Alan Cox979c4072008-05-26 11:25:26 +0200417 .unlocked_ioctl = hidraw_ioctl,
Alan Ottae5e49c2011-01-04 00:37:22 -0500418#ifdef CONFIG_COMPAT
419 .compat_ioctl = hidraw_ioctl,
420#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +0200421 .llseek = noop_llseek,
Jiri Kosina86166b72007-05-14 09:57:40 +0200422};
423
Jiri Kosinab6787242012-04-27 00:56:08 +0200424int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
Jiri Kosina86166b72007-05-14 09:57:40 +0200425{
426 struct hidraw *dev = hid->hidraw;
427 struct hidraw_list *list;
Jiri Kosinab6787242012-04-27 00:56:08 +0200428 int ret = 0;
Jiri Kosina86166b72007-05-14 09:57:40 +0200429
430 list_for_each_entry(list, &dev->list, node) {
Matthieu CASTET4c7b4172012-06-28 16:51:56 +0200431 int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
432
433 if (new_head == list->tail)
434 continue;
435
Jiri Kosinab6787242012-04-27 00:56:08 +0200436 if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
437 ret = -ENOMEM;
438 break;
439 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200440 list->buffer[list->head].len = len;
Matthieu CASTET4c7b4172012-06-28 16:51:56 +0200441 list->head = new_head;
Jiri Kosina86166b72007-05-14 09:57:40 +0200442 kill_fasync(&list->fasync, SIGIO, POLL_IN);
443 }
444
445 wake_up_interruptible(&dev->wait);
Jiri Kosinab6787242012-04-27 00:56:08 +0200446 return ret;
Jiri Kosina86166b72007-05-14 09:57:40 +0200447}
448EXPORT_SYMBOL_GPL(hidraw_report_event);
449
450int hidraw_connect(struct hid_device *hid)
451{
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200452 int minor, result;
Jiri Kosina86166b72007-05-14 09:57:40 +0200453 struct hidraw *dev;
454
Jiri Kosinabbe281f2009-06-04 15:44:25 +0200455 /* we accept any HID device, no matter the applications */
Jiri Kosina86166b72007-05-14 09:57:40 +0200456
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200457 dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
458 if (!dev)
459 return -ENOMEM;
460
461 result = -EINVAL;
Jiri Kosina86166b72007-05-14 09:57:40 +0200462
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100463 mutex_lock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200464
465 for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
466 if (hidraw_table[minor])
467 continue;
468 hidraw_table[minor] = dev;
469 result = 0;
470 break;
471 }
472
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200473 if (result) {
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100474 mutex_unlock(&minors_lock);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200475 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200476 goto out;
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200477 }
Jiri Kosina86166b72007-05-14 09:57:40 +0200478
Jiri Kosinaaae6c282008-12-04 16:16:46 +0100479 dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700480 NULL, "%s%d", "hidraw", minor);
Jiri Kosina86166b72007-05-14 09:57:40 +0200481
482 if (IS_ERR(dev->dev)) {
Jiri Kosina86166b72007-05-14 09:57:40 +0200483 hidraw_table[minor] = NULL;
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100484 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200485 result = PTR_ERR(dev->dev);
Mariusz Kozlowski709d27c2007-09-27 11:24:55 +0200486 kfree(dev);
Jiri Kosina86166b72007-05-14 09:57:40 +0200487 goto out;
488 }
489
Oliver Neukum7d672cd2008-10-31 00:07:23 +0100490 mutex_unlock(&minors_lock);
Jiri Kosina86166b72007-05-14 09:57:40 +0200491 init_waitqueue_head(&dev->wait);
492 INIT_LIST_HEAD(&dev->list);
493
494 dev->hid = hid;
495 dev->minor = minor;
496
497 dev->exist = 1;
498 hid->hidraw = dev;
499
500out:
501 return result;
502
503}
504EXPORT_SYMBOL_GPL(hidraw_connect);
505
506void hidraw_disconnect(struct hid_device *hid)
507{
508 struct hidraw *hidraw = hid->hidraw;
Ratan Nalumasu4fe9f8e2012-09-22 11:46:30 -0700509 drop_ref(hidraw, 1);
Jiri Kosina86166b72007-05-14 09:57:40 +0200510}
511EXPORT_SYMBOL_GPL(hidraw_disconnect);
512
513int __init hidraw_init(void)
514{
515 int result;
516 dev_t dev_id;
517
518 result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
519 HIDRAW_MAX_DEVICES, "hidraw");
520
521 hidraw_major = MAJOR(dev_id);
522
523 if (result < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800524 pr_warn("can't get major number\n");
Jiri Kosina86166b72007-05-14 09:57:40 +0200525 goto out;
526 }
527
528 hidraw_class = class_create(THIS_MODULE, "hidraw");
529 if (IS_ERR(hidraw_class)) {
530 result = PTR_ERR(hidraw_class);
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400531 goto error_cdev;
Jiri Kosina86166b72007-05-14 09:57:40 +0200532 }
533
534 cdev_init(&hidraw_cdev, &hidraw_ops);
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400535 result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
536 if (result < 0)
537 goto error_class;
538
Jiri Kosina86166b72007-05-14 09:57:40 +0200539out:
540 return result;
Alexey Khoroshilovbcb4a752012-08-15 23:31:45 +0400541
542error_class:
543 class_destroy(hidraw_class);
544error_cdev:
545 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
546 goto out;
Jiri Kosina86166b72007-05-14 09:57:40 +0200547}
548
Jiri Slaby140ae3e2008-10-17 18:04:48 +0200549void hidraw_exit(void)
Jiri Kosina86166b72007-05-14 09:57:40 +0200550{
551 dev_t dev_id = MKDEV(hidraw_major, 0);
552
553 cdev_del(&hidraw_cdev);
554 class_destroy(hidraw_class);
555 unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
556
557}
Ratan Nalumasu4fe9f8e2012-09-22 11:46:30 -0700558
559static void drop_ref(struct hidraw *hidraw, int exists_bit)
560{
561 mutex_lock(&minors_lock);
562 if (exists_bit) {
563 hid_hw_close(hidraw->hid);
564 hidraw->exist = 0;
565 if (hidraw->open)
566 wake_up_interruptible(&hidraw->wait);
567 } else {
568 --hidraw->open;
569 }
570
571 if (!hidraw->open && !hidraw->exist) {
572 device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));
573 hidraw_table[hidraw->minor] = NULL;
574 kfree(hidraw);
575 }
576 mutex_unlock(&minors_lock);
577}