hidraw.c 13.9 KB
Newer Older
J
Jiri Kosina 已提交
1 2 3 4 5 6 7 8
/*
 * HID raw devices, giving access to raw HID events.
 *
 * In comparison to hiddev, this device does not process the
 * hid events at all (no parsing, no lookups). This lets applications
 * to work on raw hid events as they want to, and avoids a need to
 * use a transport-specific userspace libhid/libusb libraries.
 *
9
 *  Copyright (c) 2007-2014 Jiri Kosina
J
Jiri Kosina 已提交
10 11 12 13 14 15 16 17 18 19 20 21
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 */

22 23
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

J
Jiri Kosina 已提交
24 25 26 27 28 29 30 31 32
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/major.h>
33
#include <linux/slab.h>
J
Jiri Kosina 已提交
34 35
#include <linux/hid.h>
#include <linux/mutex.h>
36
#include <linux/sched/signal.h>
37
#include <linux/string.h>
J
Jiri Kosina 已提交
38 39 40 41 42 43 44

#include <linux/hidraw.h>

static int hidraw_major;
static struct cdev hidraw_cdev;
static struct class *hidraw_class;
static struct hidraw *hidraw_table[HIDRAW_MAX_DEVICES];
O
Oliver Neukum 已提交
45
static DEFINE_MUTEX(minors_lock);
J
Jiri Kosina 已提交
46 47 48 49 50 51 52

static ssize_t hidraw_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
{
	struct hidraw_list *list = file->private_data;
	int ret = 0, len;
	DECLARE_WAITQUEUE(wait, current);

53
	mutex_lock(&list->read_mutex);
J
Jiri Kosina 已提交
54

55
	while (ret == 0) {
J
Jiri Kosina 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
		if (list->head == list->tail) {
			add_wait_queue(&list->hidraw->wait, &wait);
			set_current_state(TASK_INTERRUPTIBLE);

			while (list->head == list->tail) {
				if (signal_pending(current)) {
					ret = -ERESTARTSYS;
					break;
				}
				if (!list->hidraw->exist) {
					ret = -EIO;
					break;
				}
69 70 71 72
				if (file->f_flags & O_NONBLOCK) {
					ret = -EAGAIN;
					break;
				}
J
Jiri Kosina 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

				/* allow O_NONBLOCK to work well from other threads */
				mutex_unlock(&list->read_mutex);
				schedule();
				mutex_lock(&list->read_mutex);
				set_current_state(TASK_INTERRUPTIBLE);
			}

			set_current_state(TASK_RUNNING);
			remove_wait_queue(&list->hidraw->wait, &wait);
		}

		if (ret)
			goto out;

		len = list->buffer[list->tail].len > count ?
			count : list->buffer[list->tail].len;

91 92 93 94 95 96
		if (list->buffer[list->tail].value) {
			if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
				ret = -EFAULT;
				goto out;
			}
			ret = len;
J
Jiri Kosina 已提交
97 98 99
		}

		kfree(list->buffer[list->tail].value);
100
		list->buffer[list->tail].value = NULL;
J
Jiri Kosina 已提交
101 102 103 104 105 106 107
		list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
	}
out:
	mutex_unlock(&list->read_mutex);
	return ret;
}

108 109 110 111 112
/*
 * The first byte of the report buffer is expected to be a report number.
 *
 * This function is to be called with the minors_lock mutex held.
 */
113
static ssize_t hidraw_send_report(struct file *file, const char __user *buffer, size_t count, unsigned char report_type)
J
Jiri Kosina 已提交
114
{
A
Al Viro 已提交
115
	unsigned int minor = iminor(file_inode(file));
J
Jiri Kosina 已提交
116
	struct hid_device *dev;
J
Jiri Kosina 已提交
117 118 119
	__u8 *buf;
	int ret = 0;

120
	if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
121 122 123 124
		ret = -ENODEV;
		goto out;
	}

J
Jiri Kosina 已提交
125 126
	dev = hidraw_table[minor]->hid;

127
	if (count > HID_MAX_BUFFER_SIZE) {
128 129
		hid_warn(dev, "pid %d passed too large report\n",
			 task_pid_nr(current));
J
Jiri Kosina 已提交
130 131
		ret = -EINVAL;
		goto out;
J
Jiri Kosina 已提交
132 133 134
	}

	if (count < 2) {
135 136
		hid_warn(dev, "pid %d passed too short report\n",
			 task_pid_nr(current));
J
Jiri Kosina 已提交
137 138
		ret = -EINVAL;
		goto out;
J
Jiri Kosina 已提交
139 140
	}

141 142 143
	buf = memdup_user(buffer, count);
	if (IS_ERR(buf)) {
		ret = PTR_ERR(buf);
J
Jiri Kosina 已提交
144 145
		goto out;
	}
J
Jiri Kosina 已提交
146

147 148
	if ((report_type == HID_OUTPUT_REPORT) &&
	    !(dev->quirks & HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP)) {
149 150 151 152 153 154 155 156 157 158 159 160 161
		ret = hid_hw_output_report(dev, buf, count);
		/*
		 * compatibility with old implementation of USB-HID and I2C-HID:
		 * if the device does not support receiving output reports,
		 * on an interrupt endpoint, fallback to SET_REPORT HID command.
		 */
		if (ret != -ENOSYS)
			goto out_free;
	}

	ret = hid_hw_raw_request(dev, buf[0], buf, count, report_type,
				HID_REQ_SET_REPORT);

J
Jiri Kosina 已提交
162
out_free:
J
Jiri Kosina 已提交
163
	kfree(buf);
J
Jiri Kosina 已提交
164
out:
165 166 167 168 169 170 171 172
	return ret;
}

static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
{
	ssize_t ret;
	mutex_lock(&minors_lock);
	ret = hidraw_send_report(file, buffer, count, HID_OUTPUT_REPORT);
J
Jiri Kosina 已提交
173
	mutex_unlock(&minors_lock);
J
Jiri Kosina 已提交
174 175 176
	return ret;
}

177

178 179
/*
 * This function performs a Get_Report transfer over the control endpoint
J
Jiri Kosina 已提交
180 181 182
 * per section 7.2.1 of the HID specification, version 1.1.  The first byte
 * of buffer is the report number to request, or 0x0 if the defice does not
 * use numbered reports. The report_type parameter can be HID_FEATURE_REPORT
183 184 185 186
 * or HID_INPUT_REPORT.
 *
 * This function is to be called with the minors_lock mutex held.
 */
187 188
static ssize_t hidraw_get_report(struct file *file, char __user *buffer, size_t count, unsigned char report_type)
{
A
Al Viro 已提交
189
	unsigned int minor = iminor(file_inode(file));
190 191 192 193 194 195 196
	struct hid_device *dev;
	__u8 *buf;
	int ret = 0, len;
	unsigned char report_number;

	dev = hidraw_table[minor]->hid;

197
	if (!dev->ll_driver->raw_request) {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		ret = -ENODEV;
		goto out;
	}

	if (count > HID_MAX_BUFFER_SIZE) {
		printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
				task_pid_nr(current));
		ret = -EINVAL;
		goto out;
	}

	if (count < 2) {
		printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
				task_pid_nr(current));
		ret = -EINVAL;
		goto out;
	}

	buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
	if (!buf) {
		ret = -ENOMEM;
		goto out;
	}

222 223
	/*
	 * Read the first byte from the user. This is the report number,
224
	 * which is passed to hid_hw_raw_request().
225
	 */
226 227 228 229 230
	if (copy_from_user(&report_number, buffer, 1)) {
		ret = -EFAULT;
		goto out_free;
	}

231 232
	ret = hid_hw_raw_request(dev, report_number, buf, count, report_type,
				 HID_REQ_GET_REPORT);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

	if (ret < 0)
		goto out_free;

	len = (ret < count) ? ret : count;

	if (copy_to_user(buffer, buf, len)) {
		ret = -EFAULT;
		goto out_free;
	}

	ret = len;

out_free:
	kfree(buf);
out:
	return ret;
}

J
Jiri Kosina 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static unsigned int hidraw_poll(struct file *file, poll_table *wait)
{
	struct hidraw_list *list = file->private_data;

	poll_wait(file, &list->hidraw->wait, wait);
	if (list->head != list->tail)
		return POLLIN | POLLRDNORM;
	if (!list->hidraw->exist)
		return POLLERR | POLLHUP;
	return 0;
}

static int hidraw_open(struct inode *inode, struct file *file)
{
	unsigned int minor = iminor(inode);
	struct hidraw *dev;
	struct hidraw_list *list;
269
	unsigned long flags;
J
Jiri Kosina 已提交
270 271 272 273 274 275 276
	int err = 0;

	if (!(list = kzalloc(sizeof(struct hidraw_list), GFP_KERNEL))) {
		err = -ENOMEM;
		goto out;
	}

O
Oliver Neukum 已提交
277
	mutex_lock(&minors_lock);
278
	if (!hidraw_table[minor] || !hidraw_table[minor]->exist) {
J
Jiri Kosina 已提交
279 280 281 282 283
		err = -ENODEV;
		goto out_unlock;
	}

	dev = hidraw_table[minor];
O
Oliver Neukum 已提交
284
	if (!dev->open++) {
285
		err = hid_hw_power(dev->hid, PM_HINT_FULLON);
286 287
		if (err < 0) {
			dev->open--;
288
			goto out_unlock;
289
		}
290 291

		err = hid_hw_open(dev->hid);
292
		if (err < 0) {
293
			hid_hw_power(dev->hid, PM_HINT_NORMAL);
O
Oliver Neukum 已提交
294
			dev->open--;
295
			goto out_unlock;
296
		}
O
Oliver Neukum 已提交
297
	}
J
Jiri Kosina 已提交
298

299 300 301 302 303 304
	list->hidraw = hidraw_table[minor];
	mutex_init(&list->read_mutex);
	spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
	list_add_tail(&list->node, &hidraw_table[minor]->list);
	spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
	file->private_data = list;
J
Jiri Kosina 已提交
305
out_unlock:
O
Oliver Neukum 已提交
306 307
	mutex_unlock(&minors_lock);
out:
308 309
	if (err < 0)
		kfree(list);
J
Jiri Kosina 已提交
310 311 312 313
	return err;

}

314 315 316 317 318 319 320
static int hidraw_fasync(int fd, struct file *file, int on)
{
	struct hidraw_list *list = file->private_data;

	return fasync_helper(fd, file, on, &list->fasync);
}

321 322 323 324
static void drop_ref(struct hidraw *hidraw, int exists_bit)
{
	if (exists_bit) {
		hidraw->exist = 0;
325 326
		if (hidraw->open) {
			hid_hw_close(hidraw->hid);
327
			wake_up_interruptible(&hidraw->wait);
328
		}
329 330
		device_destroy(hidraw_class,
			       MKDEV(hidraw_major, hidraw->minor));
331 332 333
	} else {
		--hidraw->open;
	}
334 335 336 337 338 339 340
	if (!hidraw->open) {
		if (!hidraw->exist) {
			hidraw_table[hidraw->minor] = NULL;
			kfree(hidraw);
		} else {
			/* close device for last reader */
			hid_hw_close(hidraw->hid);
341
			hid_hw_power(hidraw->hid, PM_HINT_NORMAL);
342
		}
343 344 345
	}
}

J
Jiri Kosina 已提交
346 347 348 349
static int hidraw_release(struct inode * inode, struct file * file)
{
	unsigned int minor = iminor(inode);
	struct hidraw_list *list = file->private_data;
350
	unsigned long flags;
351 352

	mutex_lock(&minors_lock);
J
Jiri Kosina 已提交
353

354
	spin_lock_irqsave(&hidraw_table[minor]->list_lock, flags);
J
Jiri Kosina 已提交
355
	list_del(&list->node);
356
	spin_unlock_irqrestore(&hidraw_table[minor]->list_lock, flags);
357
	kfree(list);
358

359 360 361 362
	drop_ref(hidraw_table[minor], 0);

	mutex_unlock(&minors_lock);
	return 0;
J
Jiri Kosina 已提交
363 364
}

365 366
static long hidraw_ioctl(struct file *file, unsigned int cmd,
							unsigned long arg)
J
Jiri Kosina 已提交
367
{
A
Al Viro 已提交
368
	struct inode *inode = file_inode(file);
J
Jiri Kosina 已提交
369
	unsigned int minor = iminor(inode);
370
	long ret = 0;
J
Jiri Kosina 已提交
371
	struct hidraw *dev;
J
Jiri Kosina 已提交
372 373
	void __user *user_arg = (void __user*) arg;

J
Jiri Kosina 已提交
374 375
	mutex_lock(&minors_lock);
	dev = hidraw_table[minor];
376 377 378 379
	if (!dev) {
		ret = -ENODEV;
		goto out;
	}
J
Jiri Kosina 已提交
380

J
Jiri Kosina 已提交
381 382 383
	switch (cmd) {
		case HIDIOCGRDESCSIZE:
			if (put_user(dev->hid->rsize, (int __user *)arg))
384 385
				ret = -EFAULT;
			break;
J
Jiri Kosina 已提交
386 387 388 389 390 391

		case HIDIOCGRDESC:
			{
				__u32 len;

				if (get_user(len, (int __user *)arg))
392 393 394 395 396 397 398 399 400 401
					ret = -EFAULT;
				else if (len > HID_MAX_DESCRIPTOR_SIZE - 1)
					ret = -EINVAL;
				else if (copy_to_user(user_arg + offsetof(
					struct hidraw_report_descriptor,
					value[0]),
					dev->hid->rdesc,
					min(dev->hid->rsize, len)))
					ret = -EFAULT;
				break;
J
Jiri Kosina 已提交
402 403 404 405 406 407 408 409 410
			}
		case HIDIOCGRAWINFO:
			{
				struct hidraw_devinfo dinfo;

				dinfo.bustype = dev->hid->bus;
				dinfo.vendor = dev->hid->vendor;
				dinfo.product = dev->hid->product;
				if (copy_to_user(user_arg, &dinfo, sizeof(dinfo)))
411 412
					ret = -EFAULT;
				break;
J
Jiri Kosina 已提交
413 414
			}
		default:
415 416
			{
				struct hid_device *hid = dev->hid;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
				if (_IOC_TYPE(cmd) != 'H') {
					ret = -EINVAL;
					break;
				}

				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCSFEATURE(0))) {
					int len = _IOC_SIZE(cmd);
					ret = hidraw_send_report(file, user_arg, len, HID_FEATURE_REPORT);
					break;
				}
				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGFEATURE(0))) {
					int len = _IOC_SIZE(cmd);
					ret = hidraw_get_report(file, user_arg, len, HID_FEATURE_REPORT);
					break;
				}

				/* Begin Read-only ioctls. */
				if (_IOC_DIR(cmd) != _IOC_READ) {
435 436 437
					ret = -EINVAL;
					break;
				}
438 439

				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
440
					int len = strlen(hid->name) + 1;
441 442
					if (len > _IOC_SIZE(cmd))
						len = _IOC_SIZE(cmd);
443
					ret = copy_to_user(user_arg, hid->name, len) ?
444
						-EFAULT : len;
445
					break;
446 447 448
				}

				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
449
					int len = strlen(hid->phys) + 1;
450 451
					if (len > _IOC_SIZE(cmd))
						len = _IOC_SIZE(cmd);
452
					ret = copy_to_user(user_arg, hid->phys, len) ?
453
						-EFAULT : len;
454
					break;
455
				}
456
			}
457

458
		ret = -ENOTTY;
J
Jiri Kosina 已提交
459
	}
460
out:
J
Jiri Kosina 已提交
461
	mutex_unlock(&minors_lock);
462
	return ret;
J
Jiri Kosina 已提交
463 464 465 466 467 468 469 470 471
}

static const struct file_operations hidraw_ops = {
	.owner =        THIS_MODULE,
	.read =         hidraw_read,
	.write =        hidraw_write,
	.poll =         hidraw_poll,
	.open =         hidraw_open,
	.release =      hidraw_release,
472
	.unlocked_ioctl = hidraw_ioctl,
473
	.fasync =	hidraw_fasync,
474 475 476
#ifdef CONFIG_COMPAT
	.compat_ioctl   = hidraw_ioctl,
#endif
477
	.llseek =	noop_llseek,
J
Jiri Kosina 已提交
478 479
};

480
int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
J
Jiri Kosina 已提交
481 482 483
{
	struct hidraw *dev = hid->hidraw;
	struct hidraw_list *list;
484
	int ret = 0;
485
	unsigned long flags;
J
Jiri Kosina 已提交
486

487
	spin_lock_irqsave(&dev->list_lock, flags);
J
Jiri Kosina 已提交
488
	list_for_each_entry(list, &dev->list, node) {
489 490 491 492 493
		int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);

		if (new_head == list->tail)
			continue;

494 495 496 497
		if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
			ret = -ENOMEM;
			break;
		}
J
Jiri Kosina 已提交
498
		list->buffer[list->head].len = len;
499
		list->head = new_head;
J
Jiri Kosina 已提交
500 501
		kill_fasync(&list->fasync, SIGIO, POLL_IN);
	}
502
	spin_unlock_irqrestore(&dev->list_lock, flags);
J
Jiri Kosina 已提交
503 504

	wake_up_interruptible(&dev->wait);
505
	return ret;
J
Jiri Kosina 已提交
506 507 508 509 510
}
EXPORT_SYMBOL_GPL(hidraw_report_event);

int hidraw_connect(struct hid_device *hid)
{
511
	int minor, result;
J
Jiri Kosina 已提交
512 513
	struct hidraw *dev;

514
	/* we accept any HID device, all applications */
J
Jiri Kosina 已提交
515

516 517 518 519 520
	dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;

	result = -EINVAL;
J
Jiri Kosina 已提交
521

O
Oliver Neukum 已提交
522
	mutex_lock(&minors_lock);
J
Jiri Kosina 已提交
523 524 525 526 527 528 529 530 531

	for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
		if (hidraw_table[minor])
			continue;
		hidraw_table[minor] = dev;
		result = 0;
		break;
	}

532
	if (result) {
O
Oliver Neukum 已提交
533
		mutex_unlock(&minors_lock);
534
		kfree(dev);
J
Jiri Kosina 已提交
535
		goto out;
536
	}
J
Jiri Kosina 已提交
537

538
	dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
539
				 NULL, "%s%d", "hidraw", minor);
J
Jiri Kosina 已提交
540 541 542

	if (IS_ERR(dev->dev)) {
		hidraw_table[minor] = NULL;
O
Oliver Neukum 已提交
543
		mutex_unlock(&minors_lock);
J
Jiri Kosina 已提交
544
		result = PTR_ERR(dev->dev);
545
		kfree(dev);
J
Jiri Kosina 已提交
546 547 548 549
		goto out;
	}

	init_waitqueue_head(&dev->wait);
550
	spin_lock_init(&dev->list_lock);
J
Jiri Kosina 已提交
551 552 553 554 555 556 557 558
	INIT_LIST_HEAD(&dev->list);

	dev->hid = hid;
	dev->minor = minor;

	dev->exist = 1;
	hid->hidraw = dev;

559
	mutex_unlock(&minors_lock);
J
Jiri Kosina 已提交
560 561 562 563 564 565 566 567 568
out:
	return result;

}
EXPORT_SYMBOL_GPL(hidraw_connect);

void hidraw_disconnect(struct hid_device *hid)
{
	struct hidraw *hidraw = hid->hidraw;
569 570 571

	mutex_lock(&minors_lock);

572
	drop_ref(hidraw, 1);
573 574

	mutex_unlock(&minors_lock);
J
Jiri Kosina 已提交
575 576 577 578 579 580 581 582 583 584 585
}
EXPORT_SYMBOL_GPL(hidraw_disconnect);

int __init hidraw_init(void)
{
	int result;
	dev_t dev_id;

	result = alloc_chrdev_region(&dev_id, HIDRAW_FIRST_MINOR,
			HIDRAW_MAX_DEVICES, "hidraw");
	if (result < 0) {
586
		pr_warn("can't get major number\n");
J
Jiri Kosina 已提交
587 588 589
		goto out;
	}

590 591
	hidraw_major = MAJOR(dev_id);

J
Jiri Kosina 已提交
592 593 594
	hidraw_class = class_create(THIS_MODULE, "hidraw");
	if (IS_ERR(hidraw_class)) {
		result = PTR_ERR(hidraw_class);
595
		goto error_cdev;
J
Jiri Kosina 已提交
596 597 598
	}

        cdev_init(&hidraw_cdev, &hidraw_ops);
599 600 601 602
	result = cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
	if (result < 0)
		goto error_class;

603
	printk(KERN_INFO "hidraw: raw HID events driver (C) Jiri Kosina\n");
J
Jiri Kosina 已提交
604 605
out:
	return result;
606 607 608 609 610 611

error_class:
	class_destroy(hidraw_class);
error_cdev:
	unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);
	goto out;
J
Jiri Kosina 已提交
612 613
}

J
Jiri Slaby 已提交
614
void hidraw_exit(void)
J
Jiri Kosina 已提交
615 616 617 618 619 620 621 622
{
	dev_t dev_id = MKDEV(hidraw_major, 0);

	cdev_del(&hidraw_cdev);
	class_destroy(hidraw_class);
	unregister_chrdev_region(dev_id, HIDRAW_MAX_DEVICES);

}