4 * by Artur Lipowski <alipowski@interia.pl>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/ioctl.h>
30 #include <linux/poll.h>
31 #include <linux/completion.h>
32 #include <linux/mutex.h>
33 #include <linux/wait.h>
34 #include <linux/unistd.h>
35 #include <linux/kthread.h>
36 #include <linux/bitops.h>
37 #include <linux/device.h>
38 #include <linux/cdev.h>
40 #include <media/rc-core.h>
41 #include <media/lirc.h>
42 #include <media/lirc_dev.h>
46 #define IRCTL_DEV_NAME "BaseRemoteCtl"
48 #define LOGHEAD "lirc_dev (%s[%d]): "
50 static dev_t lirc_base_dev;
57 struct mutex irctl_lock;
58 struct lirc_buffer *buf;
59 unsigned int chunk_size;
63 struct task_struct *task;
67 static DEFINE_MUTEX(lirc_dev_lock);
69 static struct irctl *irctls[MAX_IRCTL_DEVICES];
71 /* Only used for sysfs but defined to void otherwise */
72 static struct class *lirc_class;
75 * initializes the irctl structure
77 static void lirc_irctl_init(struct irctl *ir)
79 mutex_init(&ir->irctl_lock);
83 static void lirc_irctl_cleanup(struct irctl *ir)
85 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
87 if (ir->buf != ir->d.rbuf) {
88 lirc_buffer_free(ir->buf);
95 * reads key codes from driver and puts them into buffer
96 * returns 0 on success
98 static int lirc_add_to_buf(struct irctl *ir)
100 if (ir->d.add_to_buf) {
105 * service the device as long as it is returning
106 * data and we have space
109 res = ir->d.add_to_buf(ir->d.data, ir->buf);
116 kthread_stop(ir->task);
118 return got_data ? 0 : res;
124 /* main function of the polling thread
126 static int lirc_thread(void *irctl)
128 struct irctl *ir = irctl;
132 if (ir->jiffies_to_wait) {
133 set_current_state(TASK_INTERRUPTIBLE);
134 schedule_timeout(ir->jiffies_to_wait);
136 if (kthread_should_stop())
138 if (!lirc_add_to_buf(ir))
139 wake_up_interruptible(&ir->buf->wait_poll);
141 set_current_state(TASK_INTERRUPTIBLE);
144 } while (!kthread_should_stop());
150 static const struct file_operations lirc_dev_fops = {
151 .owner = THIS_MODULE,
152 .read = lirc_dev_fop_read,
153 .write = lirc_dev_fop_write,
154 .poll = lirc_dev_fop_poll,
155 .unlocked_ioctl = lirc_dev_fop_ioctl,
157 .compat_ioctl = lirc_dev_fop_ioctl,
159 .open = lirc_dev_fop_open,
160 .release = lirc_dev_fop_close,
161 .llseek = noop_llseek,
164 static int lirc_cdev_add(struct irctl *ir)
166 int retval = -ENOMEM;
167 struct lirc_driver *d = &ir->d;
170 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
175 cdev_init(cdev, d->fops);
176 cdev->owner = d->owner;
178 cdev_init(cdev, &lirc_dev_fops);
179 cdev->owner = THIS_MODULE;
181 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
185 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
187 kobject_put(&cdev->kobj);
200 static int lirc_allocate_buffer(struct irctl *ir)
204 unsigned int chunk_size;
205 unsigned int buffer_size;
206 struct lirc_driver *d = &ir->d;
208 mutex_lock(&lirc_dev_lock);
210 bytes_in_key = BITS_TO_LONGS(d->code_length) +
211 (d->code_length % 8 ? 1 : 0);
212 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
213 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
218 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
224 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
230 ir->chunk_size = ir->buf->chunk_size;
233 mutex_unlock(&lirc_dev_lock);
238 static int lirc_allocate_driver(struct lirc_driver *d)
245 pr_err("driver pointer must be not NULL!\n");
250 pr_err("dev pointer not filled in!\n");
254 if (MAX_IRCTL_DEVICES <= d->minor) {
255 dev_err(d->dev, "minor must be between 0 and %d!\n",
256 MAX_IRCTL_DEVICES - 1);
260 if (1 > d->code_length || (BUFLEN * 8) < d->code_length) {
261 dev_err(d->dev, "code length must be less than %d bits\n",
266 if (d->sample_rate) {
267 if (2 > d->sample_rate || HZ < d->sample_rate) {
268 dev_err(d->dev, "invalid %d sample rate\n",
272 if (!d->add_to_buf) {
273 dev_err(d->dev, "add_to_buf not set\n");
276 } else if (!(d->fops && d->fops->read) && !d->rbuf) {
277 dev_err(d->dev, "fops->read and rbuf are NULL!\n");
279 } else if (!d->rbuf) {
280 if (!(d->fops && d->fops->read && d->fops->poll &&
281 d->fops->unlocked_ioctl)) {
282 dev_err(d->dev, "undefined read, poll, ioctl\n");
287 mutex_lock(&lirc_dev_lock);
292 /* find first free slot for driver */
293 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
296 if (MAX_IRCTL_DEVICES == minor) {
297 dev_err(d->dev, "no free slots for drivers!\n");
301 } else if (irctls[minor]) {
302 dev_err(d->dev, "minor (%d) just registered!\n", minor);
307 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
316 if (d->sample_rate) {
317 ir->jiffies_to_wait = HZ / d->sample_rate;
319 /* it means - wait for external event in task queue */
320 ir->jiffies_to_wait = 0;
323 /* some safety check 8-) */
324 d->name[sizeof(d->name)-1] = '\0';
326 if (d->features == 0)
327 d->features = LIRC_CAN_REC_LIRCCODE;
331 device_create(lirc_class, ir->d.dev,
332 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
333 "lirc%u", ir->d.minor);
335 if (d->sample_rate) {
336 /* try to fire up polling thread */
337 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
338 if (IS_ERR(ir->task)) {
339 dev_err(d->dev, "cannot run thread for minor = %d\n",
346 err = lirc_cdev_add(ir);
351 mutex_unlock(&lirc_dev_lock);
353 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
354 ir->d.name, ir->d.minor);
358 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
360 mutex_unlock(&lirc_dev_lock);
365 int lirc_register_driver(struct lirc_driver *d)
369 minor = lirc_allocate_driver(d);
373 if (LIRC_CAN_REC(d->features)) {
374 err = lirc_allocate_buffer(irctls[minor]);
376 lirc_unregister_driver(minor);
379 return err ? err : minor;
381 EXPORT_SYMBOL(lirc_register_driver);
383 int lirc_unregister_driver(int minor)
388 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
389 pr_err("minor (%d) must be between 0 and %d!\n",
390 minor, MAX_IRCTL_DEVICES - 1);
396 pr_err("failed to get irctl\n");
402 mutex_lock(&lirc_dev_lock);
404 if (ir->d.minor != minor) {
405 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
407 mutex_unlock(&lirc_dev_lock);
411 /* end up polling thread */
413 kthread_stop(ir->task);
415 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
416 ir->d.name, ir->d.minor);
420 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
421 ir->d.name, ir->d.minor);
422 wake_up_interruptible(&ir->buf->wait_poll);
423 mutex_lock(&ir->irctl_lock);
424 ir->d.set_use_dec(ir->d.data);
425 module_put(cdev->owner);
426 mutex_unlock(&ir->irctl_lock);
428 lirc_irctl_cleanup(ir);
432 irctls[minor] = NULL;
435 mutex_unlock(&lirc_dev_lock);
439 EXPORT_SYMBOL(lirc_unregister_driver);
441 int lirc_dev_fop_open(struct inode *inode, struct file *file)
447 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
448 pr_err("open result for %d is -ENODEV\n", iminor(inode));
452 if (mutex_lock_interruptible(&lirc_dev_lock))
455 ir = irctls[iminor(inode)];
461 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
463 if (ir->d.minor == NOPLUG) {
474 retval = rc_open(ir->d.rdev);
480 if (try_module_get(cdev->owner)) {
482 retval = ir->d.set_use_inc(ir->d.data);
485 module_put(cdev->owner);
488 lirc_buffer_clear(ir->buf);
491 wake_up_process(ir->task);
495 mutex_unlock(&lirc_dev_lock);
497 nonseekable_open(inode, file);
501 EXPORT_SYMBOL(lirc_dev_fop_open);
503 int lirc_dev_fop_close(struct inode *inode, struct file *file)
505 struct irctl *ir = irctls[iminor(inode)];
510 pr_err("called with invalid irctl\n");
516 ret = mutex_lock_killable(&lirc_dev_lock);
519 rc_close(ir->d.rdev);
523 ir->d.set_use_dec(ir->d.data);
524 module_put(cdev->owner);
526 lirc_irctl_cleanup(ir);
528 irctls[ir->d.minor] = NULL;
534 mutex_unlock(&lirc_dev_lock);
538 EXPORT_SYMBOL(lirc_dev_fop_close);
540 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
542 struct irctl *ir = irctls[iminor(file_inode(file))];
546 pr_err("called with invalid irctl\n");
554 poll_wait(file, &ir->buf->wait_poll, wait);
556 if (lirc_buffer_empty(ir->buf))
559 ret = POLLIN | POLLRDNORM;
563 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
564 ir->d.name, ir->d.minor, ret);
568 EXPORT_SYMBOL(lirc_dev_fop_poll);
570 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
574 struct irctl *ir = irctls[iminor(file_inode(file))];
577 pr_err("no irctl found!\n");
581 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
582 ir->d.name, ir->d.minor, cmd);
584 if (ir->d.minor == NOPLUG || !ir->attached) {
585 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
586 ir->d.name, ir->d.minor);
590 mutex_lock(&ir->irctl_lock);
593 case LIRC_GET_FEATURES:
594 result = put_user(ir->d.features, (__u32 __user *)arg);
596 case LIRC_GET_REC_MODE:
597 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
602 result = put_user(LIRC_REC2MODE
603 (ir->d.features & LIRC_CAN_REC_MASK),
604 (__u32 __user *)arg);
606 case LIRC_SET_REC_MODE:
607 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
612 result = get_user(mode, (__u32 __user *)arg);
613 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
616 * FIXME: We should actually set the mode somehow but
617 * for now, lirc_serial doesn't support mode changing either
620 case LIRC_GET_LENGTH:
621 result = put_user(ir->d.code_length, (__u32 __user *)arg);
623 case LIRC_GET_MIN_TIMEOUT:
624 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
625 ir->d.min_timeout == 0) {
630 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
632 case LIRC_GET_MAX_TIMEOUT:
633 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
634 ir->d.max_timeout == 0) {
639 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
645 mutex_unlock(&ir->irctl_lock);
649 EXPORT_SYMBOL(lirc_dev_fop_ioctl);
651 ssize_t lirc_dev_fop_read(struct file *file,
656 struct irctl *ir = irctls[iminor(file_inode(file))];
658 int ret = 0, written = 0;
659 DECLARE_WAITQUEUE(wait, current);
662 pr_err("called with invalid irctl\n");
666 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
668 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
672 if (mutex_lock_interruptible(&ir->irctl_lock)) {
681 if (length % ir->chunk_size) {
687 * we add ourselves to the task queue before buffer check
688 * to avoid losing scan code (in case when queue is awaken somewhere
689 * between while condition checking and scheduling)
691 add_wait_queue(&ir->buf->wait_poll, &wait);
692 set_current_state(TASK_INTERRUPTIBLE);
695 * while we didn't provide 'length' bytes, device is opened in blocking
696 * mode and 'copy_to_user' is happy, wait for data.
698 while (written < length && ret == 0) {
699 if (lirc_buffer_empty(ir->buf)) {
700 /* According to the read(2) man page, 'written' can be
701 * returned as less than 'length', instead of blocking
702 * again, returning -EWOULDBLOCK, or returning
706 if (file->f_flags & O_NONBLOCK) {
710 if (signal_pending(current)) {
715 mutex_unlock(&ir->irctl_lock);
717 set_current_state(TASK_INTERRUPTIBLE);
719 if (mutex_lock_interruptible(&ir->irctl_lock)) {
721 remove_wait_queue(&ir->buf->wait_poll, &wait);
722 set_current_state(TASK_RUNNING);
731 lirc_buffer_read(ir->buf, buf);
732 ret = copy_to_user((void __user *)buffer+written, buf,
733 ir->buf->chunk_size);
735 written += ir->buf->chunk_size;
741 remove_wait_queue(&ir->buf->wait_poll, &wait);
742 set_current_state(TASK_RUNNING);
745 mutex_unlock(&ir->irctl_lock);
750 return ret ? ret : written;
752 EXPORT_SYMBOL(lirc_dev_fop_read);
754 void *lirc_get_pdata(struct file *file)
756 return irctls[iminor(file_inode(file))]->d.data;
758 EXPORT_SYMBOL(lirc_get_pdata);
761 ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
762 size_t length, loff_t *ppos)
764 struct irctl *ir = irctls[iminor(file_inode(file))];
767 pr_err("called with invalid irctl\n");
776 EXPORT_SYMBOL(lirc_dev_fop_write);
779 static int __init lirc_dev_init(void)
783 lirc_class = class_create(THIS_MODULE, "lirc");
784 if (IS_ERR(lirc_class)) {
785 pr_err("class_create failed\n");
786 return PTR_ERR(lirc_class);
789 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
792 class_destroy(lirc_class);
793 pr_err("alloc_chrdev_region failed\n");
798 pr_info("IR Remote Control driver registered, major %d\n",
799 MAJOR(lirc_base_dev));
806 static void __exit lirc_dev_exit(void)
808 class_destroy(lirc_class);
809 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
810 pr_info("module unloaded\n");
813 module_init(lirc_dev_init);
814 module_exit(lirc_dev_exit);
816 MODULE_DESCRIPTION("LIRC base driver module");
817 MODULE_AUTHOR("Artur Lipowski");
818 MODULE_LICENSE("GPL");
820 module_param(debug, bool, S_IRUGO | S_IWUSR);
821 MODULE_PARM_DESC(debug, "Enable debugging messages");