hidraw.c 10.3 KB
Newer Older
J
Jiri Kosina 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * 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.
 *
 *  Copyright (c) 2007 Jiri Kosina
 */

/*
 * 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.
 */

#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>
#include <linux/hid.h>
#include <linux/mutex.h>
33
#include <linux/smp_lock.h>
J
Jiri Kosina 已提交
34 35 36 37 38 39 40

#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 已提交
41
static DEFINE_MUTEX(minors_lock);
J
Jiri Kosina 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

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;
	char *report;
	DECLARE_WAITQUEUE(wait, current);

	while (ret == 0) {

		mutex_lock(&list->read_mutex);

		if (list->head == list->tail) {
			add_wait_queue(&list->hidraw->wait, &wait);
			set_current_state(TASK_INTERRUPTIBLE);

			while (list->head == list->tail) {
				if (file->f_flags & O_NONBLOCK) {
					ret = -EAGAIN;
					break;
				}
				if (signal_pending(current)) {
					ret = -ERESTARTSYS;
					break;
				}
				if (!list->hidraw->exist) {
					ret = -EIO;
					break;
				}

				/* 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;

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

		if (copy_to_user(buffer, list->buffer[list->tail].value, len)) {
			ret = -EFAULT;
			goto out;
		}
		ret += len;

		kfree(list->buffer[list->tail].value);
		list->tail = (list->tail + 1) & (HIDRAW_BUFFER_SIZE - 1);
	}
out:
	mutex_unlock(&list->read_mutex);
	return ret;
}

/* the first byte is expected to be a report number */
static ssize_t hidraw_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
{
	unsigned int minor = iminor(file->f_path.dentry->d_inode);
108
	/* FIXME: What stops hidraw_table going NULL */
J
Jiri Kosina 已提交
109 110 111 112 113 114 115
	struct hid_device *dev = hidraw_table[minor]->hid;
	__u8 *buf;
	int ret = 0;

	if (!dev->hid_output_raw_report)
		return -ENODEV;

116
	if (count > HID_MAX_BUFFER_SIZE) {
J
Jiri Kosina 已提交
117
		printk(KERN_WARNING "hidraw: pid %d passed too large report\n",
118
				task_pid_nr(current));
J
Jiri Kosina 已提交
119 120 121 122 123
		return -EINVAL;
	}

	if (count < 2) {
		printk(KERN_WARNING "hidraw: pid %d passed too short report\n",
124
				task_pid_nr(current));
J
Jiri Kosina 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		return -EINVAL;
	}

	buf = kmalloc(count * sizeof(__u8), GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	if (copy_from_user(buf, buffer, count)) {
		ret = -EFAULT;
		goto out;
	}

	ret = dev->hid_output_raw_report(dev, buf, count);
out:
	kfree(buf);
	return ret;
}

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;
	int err = 0;

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

O
Oliver Neukum 已提交
167 168
	lock_kernel();
	mutex_lock(&minors_lock);
J
Jiri Kosina 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182
	if (!hidraw_table[minor]) {
		printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
				minor);
		kfree(list);
		err = -ENODEV;
		goto out_unlock;
	}

	list->hidraw = hidraw_table[minor];
	mutex_init(&list->read_mutex);
	list_add_tail(&list->node, &hidraw_table[minor]->list);
	file->private_data = list;

	dev = hidraw_table[minor];
O
Oliver Neukum 已提交
183
	if (!dev->open++) {
184 185 186 187 188
		if (dev->hid->ll_driver->power) {
			err = dev->hid->ll_driver->power(dev->hid, PM_HINT_FULLON);
			if (err < 0)
				goto out_unlock;
		}
O
Oliver Neukum 已提交
189
		err = dev->hid->ll_driver->open(dev->hid);
190 191 192
		if (err < 0) {
			if (dev->hid->ll_driver->power)
				dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
O
Oliver Neukum 已提交
193
			dev->open--;
194
		}
O
Oliver Neukum 已提交
195
	}
J
Jiri Kosina 已提交
196 197

out_unlock:
O
Oliver Neukum 已提交
198
	mutex_unlock(&minors_lock);
199
	unlock_kernel();
O
Oliver Neukum 已提交
200
out:
J
Jiri Kosina 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	return err;

}

static int hidraw_release(struct inode * inode, struct file * file)
{
	unsigned int minor = iminor(inode);
	struct hidraw *dev;
	struct hidraw_list *list = file->private_data;

	if (!hidraw_table[minor]) {
		printk(KERN_EMERG "hidraw device with minor %d doesn't exist\n",
				minor);
		return -ENODEV;
	}

	list_del(&list->node);
	dev = hidraw_table[minor];
219
	if (!--dev->open) {
220 221 222
		if (list->hidraw->exist) {
			if (dev->hid->ll_driver->power)
				dev->hid->ll_driver->power(dev->hid, PM_HINT_NORMAL);
J
Jiri Slaby 已提交
223
			dev->hid->ll_driver->close(dev->hid);
224
		} else {
J
Jiri Kosina 已提交
225
			kfree(list->hidraw);
226
		}
J
Jiri Kosina 已提交
227 228
	}

229 230
	kfree(list);

J
Jiri Kosina 已提交
231 232 233
	return 0;
}

234 235
static long hidraw_ioctl(struct file *file, unsigned int cmd,
							unsigned long arg)
J
Jiri Kosina 已提交
236
{
237
	struct inode *inode = file->f_path.dentry->d_inode;
J
Jiri Kosina 已提交
238
	unsigned int minor = iminor(inode);
239 240
	long ret = 0;
	/* FIXME: What stops hidraw_table going NULL */
J
Jiri Kosina 已提交
241 242 243
	struct hidraw *dev = hidraw_table[minor];
	void __user *user_arg = (void __user*) arg;

244
	lock_kernel();
J
Jiri Kosina 已提交
245 246 247
	switch (cmd) {
		case HIDIOCGRDESCSIZE:
			if (put_user(dev->hid->rsize, (int __user *)arg))
248 249
				ret = -EFAULT;
			break;
J
Jiri Kosina 已提交
250 251 252 253 254 255

		case HIDIOCGRDESC:
			{
				__u32 len;

				if (get_user(len, (int __user *)arg))
256 257 258 259 260 261 262 263 264 265
					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 已提交
266 267 268 269 270 271 272 273 274
			}
		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)))
275 276
					ret = -EFAULT;
				break;
J
Jiri Kosina 已提交
277 278
			}
		default:
279 280
			{
				struct hid_device *hid = dev->hid;
281 282 283 284
				if (_IOC_TYPE(cmd) != 'H' || _IOC_DIR(cmd) != _IOC_READ) {
					ret = -EINVAL;
					break;
				}
285 286 287 288 289 290 291 292

				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWNAME(0))) {
					int len;
					if (!hid->name)
						return 0;
					len = strlen(hid->name) + 1;
					if (len > _IOC_SIZE(cmd))
						len = _IOC_SIZE(cmd);
293
					ret = copy_to_user(user_arg, hid->name, len) ?
294
						-EFAULT : len;
295
					break;
296 297 298 299 300 301 302 303 304
				}

				if (_IOC_NR(cmd) == _IOC_NR(HIDIOCGRAWPHYS(0))) {
					int len;
					if (!hid->phys)
						return 0;
					len = strlen(hid->phys) + 1;
					if (len > _IOC_SIZE(cmd))
						len = _IOC_SIZE(cmd);
305
					ret = copy_to_user(user_arg, hid->phys, len) ?
306
						-EFAULT : len;
307
					break;
308 309 310
				}
                }

311
		ret = -ENOTTY;
J
Jiri Kosina 已提交
312
	}
J
Jiri Slaby 已提交
313
	unlock_kernel();
314
	return ret;
J
Jiri Kosina 已提交
315 316 317 318 319 320 321 322 323
}

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,
324
	.unlocked_ioctl = hidraw_ioctl,
J
Jiri Kosina 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
};

void hidraw_report_event(struct hid_device *hid, u8 *data, int len)
{
	struct hidraw *dev = hid->hidraw;
	struct hidraw_list *list;

	list_for_each_entry(list, &dev->list, node) {
		list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC);
		list->buffer[list->head].len = len;
		list->head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
		kill_fasync(&list->fasync, SIGIO, POLL_IN);
	}

	wake_up_interruptible(&dev->wait);
}
EXPORT_SYMBOL_GPL(hidraw_report_event);

int hidraw_connect(struct hid_device *hid)
{
345
	int minor, result;
J
Jiri Kosina 已提交
346 347 348 349 350 351 352
	struct hidraw *dev;

	/* TODO currently we accept any HID device. This should later
	 * probably be fixed to accept only those devices which provide
	 * non-input applications
	 */

353 354 355 356 357
	dev = kzalloc(sizeof(struct hidraw), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;

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

O
Oliver Neukum 已提交
359
	mutex_lock(&minors_lock);
J
Jiri Kosina 已提交
360 361 362 363 364 365 366 367 368

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

369
	if (result) {
O
Oliver Neukum 已提交
370
		mutex_unlock(&minors_lock);
371
		kfree(dev);
J
Jiri Kosina 已提交
372
		goto out;
373
	}
J
Jiri Kosina 已提交
374

375
	dev->dev = device_create(hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
376
				 NULL, "%s%d", "hidraw", minor);
J
Jiri Kosina 已提交
377 378 379

	if (IS_ERR(dev->dev)) {
		hidraw_table[minor] = NULL;
O
Oliver Neukum 已提交
380
		mutex_unlock(&minors_lock);
J
Jiri Kosina 已提交
381
		result = PTR_ERR(dev->dev);
382
		kfree(dev);
J
Jiri Kosina 已提交
383 384 385
		goto out;
	}

O
Oliver Neukum 已提交
386
	mutex_unlock(&minors_lock);
J
Jiri Kosina 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	init_waitqueue_head(&dev->wait);
	INIT_LIST_HEAD(&dev->list);

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

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

out:
	return result;

}
EXPORT_SYMBOL_GPL(hidraw_connect);

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

	hidraw->exist = 0;

O
Oliver Neukum 已提交
408
	mutex_lock(&minors_lock);
J
Jiri Kosina 已提交
409
	hidraw_table[hidraw->minor] = NULL;
O
Oliver Neukum 已提交
410
	mutex_unlock(&minors_lock);
J
Jiri Kosina 已提交
411 412 413 414

	device_destroy(hidraw_class, MKDEV(hidraw_major, hidraw->minor));

	if (hidraw->open) {
J
Jiri Slaby 已提交
415
		hid->ll_driver->close(hid);
J
Jiri Kosina 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
		wake_up_interruptible(&hidraw->wait);
	} else {
		kfree(hidraw);
	}
}
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");

	hidraw_major = MAJOR(dev_id);

	if (result < 0) {
		printk(KERN_WARNING "hidraw: can't get major number\n");
		result = 0;
		goto out;
	}

	hidraw_class = class_create(THIS_MODULE, "hidraw");
	if (IS_ERR(hidraw_class)) {
		result = PTR_ERR(hidraw_class);
		unregister_chrdev(hidraw_major, "hidraw");
		goto out;
	}

        cdev_init(&hidraw_cdev, &hidraw_ops);
        cdev_add(&hidraw_cdev, dev_id, HIDRAW_MAX_DEVICES);
out:
	return result;
}

J
Jiri Slaby 已提交
452
void hidraw_exit(void)
J
Jiri Kosina 已提交
453 454 455 456 457 458 459 460
{
	dev_t dev_id = MKDEV(hidraw_major, 0);

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

}