lirc_dev.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * LIRC base driver
 *
 * by Artur Lipowski <alipowski@interia.pl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/completion.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/unistd.h>
#include <linux/kthread.h>
#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/cdev.h>

40
#include <media/rc-core.h>
41
#include <media/lirc.h>
42
#include <media/lirc_dev.h>
43

44
static bool debug;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#define IRCTL_DEV_NAME	"BaseRemoteCtl"
#define NOPLUG		-1
#define LOGHEAD		"lirc_dev (%s[%d]): "

static dev_t lirc_base_dev;

struct irctl {
	struct lirc_driver d;
	int attached;
	int open;

	struct mutex irctl_lock;
	struct lirc_buffer *buf;
	unsigned int chunk_size;

61 62
	struct cdev *cdev;

63 64 65 66 67 68 69 70 71 72 73 74 75 76
	struct task_struct *task;
	long jiffies_to_wait;
};

static DEFINE_MUTEX(lirc_dev_lock);

static struct irctl *irctls[MAX_IRCTL_DEVICES];

/* Only used for sysfs but defined to void otherwise */
static struct class *lirc_class;

/*  helper function
 *  initializes the irctl structure
 */
77
static void lirc_irctl_init(struct irctl *ir)
78 79 80 81 82
{
	mutex_init(&ir->irctl_lock);
	ir->d.minor = NOPLUG;
}

83
static void lirc_irctl_cleanup(struct irctl *ir)
84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
	device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));

	if (ir->buf != ir->d.rbuf) {
		lirc_buffer_free(ir->buf);
		kfree(ir->buf);
	}
	ir->buf = NULL;
}

/*  helper function
 *  reads key codes from driver and puts them into buffer
 *  returns 0 on success
 */
98
static int lirc_add_to_buf(struct irctl *ir)
99
{
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	int res;
	int got_data = -1;

	if (!ir->d.add_to_buf)
		return 0;

	/*
	 * service the device as long as it is returning
	 * data and we have space
	 */
	do {
		got_data++;
		res = ir->d.add_to_buf(ir->d.data, ir->buf);
	} while (!res);

	if (res == -ENODEV)
		kthread_stop(ir->task);
117

118
	return got_data ? 0 : res;
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

/* main function of the polling thread
 */
static int lirc_thread(void *irctl)
{
	struct irctl *ir = irctl;

	do {
		if (ir->open) {
			if (ir->jiffies_to_wait) {
				set_current_state(TASK_INTERRUPTIBLE);
				schedule_timeout(ir->jiffies_to_wait);
			}
			if (kthread_should_stop())
				break;
135
			if (!lirc_add_to_buf(ir))
136 137 138 139 140 141 142 143 144 145 146
				wake_up_interruptible(&ir->buf->wait_poll);
		} else {
			set_current_state(TASK_INTERRUPTIBLE);
			schedule();
		}
	} while (!kthread_should_stop());

	return 0;
}


147
static const struct file_operations lirc_dev_fops = {
148 149 150 151
	.owner		= THIS_MODULE,
	.read		= lirc_dev_fop_read,
	.write		= lirc_dev_fop_write,
	.poll		= lirc_dev_fop_poll,
152
	.unlocked_ioctl	= lirc_dev_fop_ioctl,
153 154 155
#ifdef CONFIG_COMPAT
	.compat_ioctl	= lirc_dev_fop_ioctl,
#endif
156 157
	.open		= lirc_dev_fop_open,
	.release	= lirc_dev_fop_close,
158
	.llseek		= noop_llseek,
159 160 161 162
};

static int lirc_cdev_add(struct irctl *ir)
{
163
	int retval = -ENOMEM;
164
	struct lirc_driver *d = &ir->d;
165 166 167 168 169
	struct cdev *cdev;

	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
	if (!cdev)
		goto err_out;
170 171

	if (d->fops) {
172 173
		cdev_init(cdev, d->fops);
		cdev->owner = d->owner;
174
	} else {
175 176
		cdev_init(cdev, &lirc_dev_fops);
		cdev->owner = THIS_MODULE;
177
	}
178 179
	retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
	if (retval)
180
		goto err_out;
181

182
	retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
183
	if (retval) {
184
		kobject_put(&cdev->kobj);
185 186
		goto err_out;
	}
187

188 189 190 191 192 193
	ir->cdev = cdev;

	return 0;

err_out:
	kfree(cdev);
194 195 196
	return retval;
}

197
static int lirc_allocate_buffer(struct irctl *ir)
198
{
199
	int err = 0;
200 201 202
	int bytes_in_key;
	unsigned int chunk_size;
	unsigned int buffer_size;
203 204
	struct lirc_driver *d = &ir->d;

205 206
	mutex_lock(&lirc_dev_lock);

207 208 209 210 211 212 213 214 215
	bytes_in_key = BITS_TO_LONGS(d->code_length) +
						(d->code_length % 8 ? 1 : 0);
	buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
	chunk_size  = d->chunk_size  ? d->chunk_size  : bytes_in_key;

	if (d->rbuf) {
		ir->buf = d->rbuf;
	} else {
		ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
216 217 218 219
		if (!ir->buf) {
			err = -ENOMEM;
			goto out;
		}
220 221 222 223

		err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
		if (err) {
			kfree(ir->buf);
224
			goto out;
225 226 227 228
		}
	}
	ir->chunk_size = ir->buf->chunk_size;

229 230 231 232
out:
	mutex_unlock(&lirc_dev_lock);

	return err;
233 234
}

235
static int lirc_allocate_driver(struct lirc_driver *d)
236 237 238
{
	struct irctl *ir;
	int minor;
239 240 241
	int err;

	if (!d) {
242
		pr_err("driver pointer must be not NULL!\n");
243
		return -EBADRQC;
244 245
	}

246
	if (!d->dev) {
247
		pr_err("dev pointer not filled in!\n");
248
		return -EINVAL;
249 250
	}

251
	if (MAX_IRCTL_DEVICES <= d->minor) {
252 253
		dev_err(d->dev, "minor must be between 0 and %d!\n",
						MAX_IRCTL_DEVICES - 1);
254
		return -EBADRQC;
255 256 257
	}

	if (1 > d->code_length || (BUFLEN * 8) < d->code_length) {
258 259
		dev_err(d->dev, "code length must be less than %d bits\n",
								BUFLEN * 8);
260
		return -EBADRQC;
261 262 263 264
	}

	if (d->sample_rate) {
		if (2 > d->sample_rate || HZ < d->sample_rate) {
265 266
			dev_err(d->dev, "invalid %d sample rate\n",
							d->sample_rate);
267
			return -EBADRQC;
268 269
		}
		if (!d->add_to_buf) {
270
			dev_err(d->dev, "add_to_buf not set\n");
271
			return -EBADRQC;
272 273
		}
	} else if (!(d->fops && d->fops->read) && !d->rbuf) {
274
		dev_err(d->dev, "fops->read and rbuf are NULL!\n");
275
		return -EBADRQC;
276 277
	} else if (!d->rbuf) {
		if (!(d->fops && d->fops->read && d->fops->poll &&
278
		      d->fops->unlocked_ioctl)) {
279
			dev_err(d->dev, "undefined read, poll, ioctl\n");
280
			return -EBADRQC;
281 282 283 284 285 286 287 288 289 290 291 292 293
		}
	}

	mutex_lock(&lirc_dev_lock);

	minor = d->minor;

	if (minor < 0) {
		/* find first free slot for driver */
		for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
			if (!irctls[minor])
				break;
		if (MAX_IRCTL_DEVICES == minor) {
294
			dev_err(d->dev, "no free slots for drivers!\n");
295 296 297 298
			err = -ENOMEM;
			goto out_lock;
		}
	} else if (irctls[minor]) {
299
		dev_err(d->dev, "minor (%d) just registered!\n", minor);
300 301 302 303 304 305 306 307 308
		err = -EBUSY;
		goto out_lock;
	}

	ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
	if (!ir) {
		err = -ENOMEM;
		goto out_lock;
	}
309
	lirc_irctl_init(ir);
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	irctls[minor] = ir;
	d->minor = minor;

	/* some safety check 8-) */
	d->name[sizeof(d->name)-1] = '\0';

	if (d->features == 0)
		d->features = LIRC_CAN_REC_LIRCCODE;

	ir->d = *d;

	device_create(lirc_class, ir->d.dev,
		      MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
		      "lirc%u", ir->d.minor);

	if (d->sample_rate) {
326 327
		ir->jiffies_to_wait = HZ / d->sample_rate;

328 329 330
		/* try to fire up polling thread */
		ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
		if (IS_ERR(ir->task)) {
331 332
			dev_err(d->dev, "cannot run thread for minor = %d\n",
								d->minor);
333 334 335
			err = -ECHILD;
			goto out_sysfs;
		}
336 337 338
	} else {
		/* it means - wait for external event in task queue */
		ir->jiffies_to_wait = 0;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	}

	err = lirc_cdev_add(ir);
	if (err)
		goto out_sysfs;

	ir->attached = 1;
	mutex_unlock(&lirc_dev_lock);

	dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
		 ir->d.name, ir->d.minor);
	return minor;

out_sysfs:
	device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
out_lock:
	mutex_unlock(&lirc_dev_lock);
356

357 358
	return err;
}
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

int lirc_register_driver(struct lirc_driver *d)
{
	int minor, err = 0;

	minor = lirc_allocate_driver(d);
	if (minor < 0)
		return minor;

	if (LIRC_CAN_REC(d->features)) {
		err = lirc_allocate_buffer(irctls[minor]);
		if (err)
			lirc_unregister_driver(minor);
	}

	return err ? err : minor;
}
376 377 378 379 380
EXPORT_SYMBOL(lirc_register_driver);

int lirc_unregister_driver(int minor)
{
	struct irctl *ir;
381
	struct cdev *cdev;
382 383

	if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
384 385
		pr_err("minor (%d) must be between 0 and %d!\n",
					minor, MAX_IRCTL_DEVICES - 1);
386 387 388 389
		return -EBADRQC;
	}

	ir = irctls[minor];
390
	if (!ir) {
391
		pr_err("failed to get irctl\n");
392 393
		return -ENOENT;
	}
394

395
	cdev = ir->cdev;
396 397 398 399

	mutex_lock(&lirc_dev_lock);

	if (ir->d.minor != minor) {
400 401
		dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
									minor);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
		mutex_unlock(&lirc_dev_lock);
		return -ENOENT;
	}

	/* end up polling thread */
	if (ir->task)
		kthread_stop(ir->task);

	dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
		ir->d.name, ir->d.minor);

	ir->attached = 0;
	if (ir->open) {
		dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
			ir->d.name, ir->d.minor);
		wake_up_interruptible(&ir->buf->wait_poll);
		mutex_lock(&ir->irctl_lock);
		ir->d.set_use_dec(ir->d.data);
420
		module_put(cdev->owner);
421 422
		mutex_unlock(&ir->irctl_lock);
	} else {
423
		lirc_irctl_cleanup(ir);
424
		cdev_del(cdev);
425
		kfree(cdev);
426 427 428 429 430 431 432 433 434 435 436 437 438
		kfree(ir);
		irctls[minor] = NULL;
	}

	mutex_unlock(&lirc_dev_lock);

	return 0;
}
EXPORT_SYMBOL(lirc_unregister_driver);

int lirc_dev_fop_open(struct inode *inode, struct file *file)
{
	struct irctl *ir;
439
	struct cdev *cdev;
440 441 442
	int retval = 0;

	if (iminor(inode) >= MAX_IRCTL_DEVICES) {
443
		pr_err("open result for %d is -ENODEV\n", iminor(inode));
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		return -ENODEV;
	}

	if (mutex_lock_interruptible(&lirc_dev_lock))
		return -ERESTARTSYS;

	ir = irctls[iminor(inode)];
	if (!ir) {
		retval = -ENODEV;
		goto error;
	}

	dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);

	if (ir->d.minor == NOPLUG) {
		retval = -ENODEV;
		goto error;
	}

	if (ir->open) {
		retval = -EBUSY;
		goto error;
	}

468 469 470 471 472 473
	if (ir->d.rdev) {
		retval = rc_open(ir->d.rdev);
		if (retval)
			goto error;
	}

474
	cdev = ir->cdev;
475 476
	if (try_module_get(cdev->owner)) {
		ir->open++;
477 478 479
		retval = ir->d.set_use_inc(ir->d.data);

		if (retval) {
480 481
			module_put(cdev->owner);
			ir->open--;
482 483 484 485 486 487 488 489 490 491
		} else {
			lirc_buffer_clear(ir->buf);
		}
		if (ir->task)
			wake_up_process(ir->task);
	}

error:
	mutex_unlock(&lirc_dev_lock);

A
Arnd Bergmann 已提交
492 493
	nonseekable_open(inode, file);

494 495 496 497 498 499 500
	return retval;
}
EXPORT_SYMBOL(lirc_dev_fop_open);

int lirc_dev_fop_close(struct inode *inode, struct file *file)
{
	struct irctl *ir = irctls[iminor(inode)];
501
	struct cdev *cdev;
502
	int ret;
503

504
	if (!ir) {
505
		pr_err("called with invalid irctl\n");
506 507
		return -EINVAL;
	}
508

509 510
	cdev = ir->cdev;

511 512
	ret = mutex_lock_killable(&lirc_dev_lock);
	WARN_ON(ret);
513

514
	rc_close(ir->d.rdev);
515

516
	ir->open--;
517 518
	if (ir->attached) {
		ir->d.set_use_dec(ir->d.data);
519
		module_put(cdev->owner);
520
	} else {
521
		lirc_irctl_cleanup(ir);
522
		cdev_del(cdev);
523
		irctls[ir->d.minor] = NULL;
524
		kfree(cdev);
525 526 527
		kfree(ir);
	}

528 529
	if (!ret)
		mutex_unlock(&lirc_dev_lock);
530 531 532 533 534 535 536

	return 0;
}
EXPORT_SYMBOL(lirc_dev_fop_close);

unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
{
A
Al Viro 已提交
537
	struct irctl *ir = irctls[iminor(file_inode(file))];
538 539
	unsigned int ret;

540
	if (!ir) {
541
		pr_err("called with invalid irctl\n");
542 543 544
		return POLLERR;
	}

545
	if (!ir->attached)
546 547
		return POLLERR;

548 549
	if (ir->buf) {
		poll_wait(file, &ir->buf->wait_poll, wait);
550 551 552 553 554

		if (lirc_buffer_empty(ir->buf))
			ret = 0;
		else
			ret = POLLIN | POLLRDNORM;
555
	} else
556 557 558 559 560 561 562 563 564
		ret = POLLERR;

	dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
		ir->d.name, ir->d.minor, ret);

	return ret;
}
EXPORT_SYMBOL(lirc_dev_fop_poll);

565
long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
566
{
567
	__u32 mode;
568
	int result = 0;
A
Al Viro 已提交
569
	struct irctl *ir = irctls[iminor(file_inode(file))];
570

571
	if (!ir) {
572
		pr_err("no irctl found!\n");
573 574
		return -ENODEV;
	}
575 576 577 578 579

	dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
		ir->d.name, ir->d.minor, cmd);

	if (ir->d.minor == NOPLUG || !ir->attached) {
580
		dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
581 582 583 584 585 586 587 588
			ir->d.name, ir->d.minor);
		return -ENODEV;
	}

	mutex_lock(&ir->irctl_lock);

	switch (cmd) {
	case LIRC_GET_FEATURES:
589
		result = put_user(ir->d.features, (__u32 __user *)arg);
590 591 592 593 594 595 596 597 598
		break;
	case LIRC_GET_REC_MODE:
		if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
			result = -ENOSYS;
			break;
		}

		result = put_user(LIRC_REC2MODE
				  (ir->d.features & LIRC_CAN_REC_MASK),
599
				  (__u32 __user *)arg);
600 601 602 603 604 605 606
		break;
	case LIRC_SET_REC_MODE:
		if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
			result = -ENOSYS;
			break;
		}

607
		result = get_user(mode, (__u32 __user *)arg);
608 609 610 611 612 613 614 615
		if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
			result = -EINVAL;
		/*
		 * FIXME: We should actually set the mode somehow but
		 * for now, lirc_serial doesn't support mode changing either
		 */
		break;
	case LIRC_GET_LENGTH:
616
		result = put_user(ir->d.code_length, (__u32 __user *)arg);
617 618 619 620 621 622 623 624
		break;
	case LIRC_GET_MIN_TIMEOUT:
		if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
		    ir->d.min_timeout == 0) {
			result = -ENOSYS;
			break;
		}

625
		result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
626 627 628 629 630 631 632 633
		break;
	case LIRC_GET_MAX_TIMEOUT:
		if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
		    ir->d.max_timeout == 0) {
			result = -ENOSYS;
			break;
		}

634
		result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
635 636 637 638 639 640 641 642 643 644 645 646
		break;
	default:
		result = -EINVAL;
	}

	mutex_unlock(&ir->irctl_lock);

	return result;
}
EXPORT_SYMBOL(lirc_dev_fop_ioctl);

ssize_t lirc_dev_fop_read(struct file *file,
647
			  char __user *buffer,
648 649 650
			  size_t length,
			  loff_t *ppos)
{
A
Al Viro 已提交
651
	struct irctl *ir = irctls[iminor(file_inode(file))];
652
	unsigned char *buf;
653 654 655
	int ret = 0, written = 0;
	DECLARE_WAITQUEUE(wait, current);

656
	if (!ir) {
657
		pr_err("called with invalid irctl\n");
658 659 660
		return -ENODEV;
	}

661 662
	dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);

663 664 665 666
	buf = kzalloc(ir->chunk_size, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

667 668 669 670
	if (mutex_lock_interruptible(&ir->irctl_lock)) {
		ret = -ERESTARTSYS;
		goto out_unlocked;
	}
671
	if (!ir->attached) {
672 673
		ret = -ENODEV;
		goto out_locked;
674 675 676
	}

	if (length % ir->chunk_size) {
677 678
		ret = -EINVAL;
		goto out_locked;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	}

	/*
	 * we add ourselves to the task queue before buffer check
	 * to avoid losing scan code (in case when queue is awaken somewhere
	 * between while condition checking and scheduling)
	 */
	add_wait_queue(&ir->buf->wait_poll, &wait);
	set_current_state(TASK_INTERRUPTIBLE);

	/*
	 * while we didn't provide 'length' bytes, device is opened in blocking
	 * mode and 'copy_to_user' is happy, wait for data.
	 */
	while (written < length && ret == 0) {
		if (lirc_buffer_empty(ir->buf)) {
			/* According to the read(2) man page, 'written' can be
			 * returned as less than 'length', instead of blocking
			 * again, returning -EWOULDBLOCK, or returning
			 * -ERESTARTSYS */
			if (written)
				break;
			if (file->f_flags & O_NONBLOCK) {
				ret = -EWOULDBLOCK;
				break;
			}
			if (signal_pending(current)) {
				ret = -ERESTARTSYS;
				break;
			}

			mutex_unlock(&ir->irctl_lock);
			schedule();
			set_current_state(TASK_INTERRUPTIBLE);

			if (mutex_lock_interruptible(&ir->irctl_lock)) {
				ret = -ERESTARTSYS;
716 717 718
				remove_wait_queue(&ir->buf->wait_poll, &wait);
				set_current_state(TASK_RUNNING);
				goto out_unlocked;
719 720 721 722 723 724 725 726
			}

			if (!ir->attached) {
				ret = -ENODEV;
				break;
			}
		} else {
			lirc_buffer_read(ir->buf, buf);
727
			ret = copy_to_user((void __user *)buffer+written, buf,
728
					   ir->buf->chunk_size);
729 730 731 732
			if (!ret)
				written += ir->buf->chunk_size;
			else
				ret = -EFAULT;
733 734 735 736 737
		}
	}

	remove_wait_queue(&ir->buf->wait_poll, &wait);
	set_current_state(TASK_RUNNING);
738 739

out_locked:
740 741
	mutex_unlock(&ir->irctl_lock);

742
out_unlocked:
743
	kfree(buf);
744 745 746 747 748 749 750

	return ret ? ret : written;
}
EXPORT_SYMBOL(lirc_dev_fop_read);

void *lirc_get_pdata(struct file *file)
{
A
Al Viro 已提交
751
	return irctls[iminor(file_inode(file))]->d.data;
752 753 754 755
}
EXPORT_SYMBOL(lirc_get_pdata);


756
ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
757 758
			   size_t length, loff_t *ppos)
{
A
Al Viro 已提交
759
	struct irctl *ir = irctls[iminor(file_inode(file))];
760

761
	if (!ir) {
762
		pr_err("called with invalid irctl\n");
763 764 765
		return -ENODEV;
	}

766 767 768 769 770 771 772 773 774 775 776 777 778 779
	if (!ir->attached)
		return -ENODEV;

	return -EINVAL;
}
EXPORT_SYMBOL(lirc_dev_fop_write);


static int __init lirc_dev_init(void)
{
	int retval;

	lirc_class = class_create(THIS_MODULE, "lirc");
	if (IS_ERR(lirc_class)) {
780
		pr_err("class_create failed\n");
781
		return PTR_ERR(lirc_class);
782 783 784 785 786 787
	}

	retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
				     IRCTL_DEV_NAME);
	if (retval) {
		class_destroy(lirc_class);
788
		pr_err("alloc_chrdev_region failed\n");
789
		return retval;
790 791 792
	}


793 794
	pr_info("IR Remote Control driver registered, major %d\n",
						MAJOR(lirc_base_dev));
795

796
	return 0;
797 798 799 800 801 802 803 804
}



static void __exit lirc_dev_exit(void)
{
	class_destroy(lirc_class);
	unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
805
	pr_info("module unloaded\n");
806 807 808 809 810 811 812 813 814 815 816
}

module_init(lirc_dev_init);
module_exit(lirc_dev_exit);

MODULE_DESCRIPTION("LIRC base driver module");
MODULE_AUTHOR("Artur Lipowski");
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Enable debugging messages");