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
110 res = ir->d.add_to_buf(ir->d.data, ir->buf);
114 kthread_stop(ir->task);
116 return got_data ? 0 : res;
122 /* main function of the polling thread
124 static int lirc_thread(void *irctl)
126 struct irctl *ir = irctl;
130 if (ir->jiffies_to_wait) {
131 set_current_state(TASK_INTERRUPTIBLE);
132 schedule_timeout(ir->jiffies_to_wait);
134 if (kthread_should_stop())
136 if (!lirc_add_to_buf(ir))
137 wake_up_interruptible(&ir->buf->wait_poll);
139 set_current_state(TASK_INTERRUPTIBLE);
142 } while (!kthread_should_stop());
148 static const struct file_operations lirc_dev_fops = {
149 .owner = THIS_MODULE,
150 .read = lirc_dev_fop_read,
151 .write = lirc_dev_fop_write,
152 .poll = lirc_dev_fop_poll,
153 .unlocked_ioctl = lirc_dev_fop_ioctl,
155 .compat_ioctl = lirc_dev_fop_ioctl,
157 .open = lirc_dev_fop_open,
158 .release = lirc_dev_fop_close,
159 .llseek = noop_llseek,
162 static int lirc_cdev_add(struct irctl *ir)
164 int retval = -ENOMEM;
165 struct lirc_driver *d = &ir->d;
168 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
173 cdev_init(cdev, d->fops);
174 cdev->owner = d->owner;
176 cdev_init(cdev, &lirc_dev_fops);
177 cdev->owner = THIS_MODULE;
179 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
183 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
185 kobject_put(&cdev->kobj);
198 static int lirc_allocate_buffer(struct irctl *ir)
202 unsigned int chunk_size;
203 unsigned int buffer_size;
204 struct lirc_driver *d = &ir->d;
206 mutex_lock(&lirc_dev_lock);
208 bytes_in_key = BITS_TO_LONGS(d->code_length) +
209 (d->code_length % 8 ? 1 : 0);
210 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
211 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
216 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
222 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
228 ir->chunk_size = ir->buf->chunk_size;
231 mutex_unlock(&lirc_dev_lock);
236 static int lirc_allocate_driver(struct lirc_driver *d)
243 pr_err("driver pointer must be not NULL!\n");
248 pr_err("dev pointer not filled in!\n");
252 if (MAX_IRCTL_DEVICES <= d->minor) {
253 dev_err(d->dev, "minor must be between 0 and %d!\n",
254 MAX_IRCTL_DEVICES - 1);
258 if (1 > d->code_length || (BUFLEN * 8) < d->code_length) {
259 dev_err(d->dev, "code length must be less than %d bits\n",
264 if (d->sample_rate) {
265 if (2 > d->sample_rate || HZ < d->sample_rate) {
266 dev_err(d->dev, "invalid %d sample rate\n",
270 if (!d->add_to_buf) {
271 dev_err(d->dev, "add_to_buf not set\n");
274 } else if (!(d->fops && d->fops->read) && !d->rbuf) {
275 dev_err(d->dev, "fops->read and rbuf are NULL!\n");
277 } else if (!d->rbuf) {
278 if (!(d->fops && d->fops->read && d->fops->poll &&
279 d->fops->unlocked_ioctl)) {
280 dev_err(d->dev, "undefined read, poll, ioctl\n");
285 mutex_lock(&lirc_dev_lock);
290 /* find first free slot for driver */
291 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
294 if (MAX_IRCTL_DEVICES == minor) {
295 dev_err(d->dev, "no free slots for drivers!\n");
299 } else if (irctls[minor]) {
300 dev_err(d->dev, "minor (%d) just registered!\n", minor);
305 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
314 if (d->sample_rate) {
315 ir->jiffies_to_wait = HZ / d->sample_rate;
317 /* it means - wait for external event in task queue */
318 ir->jiffies_to_wait = 0;
321 /* some safety check 8-) */
322 d->name[sizeof(d->name)-1] = '\0';
324 if (d->features == 0)
325 d->features = LIRC_CAN_REC_LIRCCODE;
329 device_create(lirc_class, ir->d.dev,
330 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
331 "lirc%u", ir->d.minor);
333 if (d->sample_rate) {
334 /* try to fire up polling thread */
335 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
336 if (IS_ERR(ir->task)) {
337 dev_err(d->dev, "cannot run thread for minor = %d\n",
344 err = lirc_cdev_add(ir);
349 mutex_unlock(&lirc_dev_lock);
351 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
352 ir->d.name, ir->d.minor);
356 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
358 mutex_unlock(&lirc_dev_lock);
363 int lirc_register_driver(struct lirc_driver *d)
367 minor = lirc_allocate_driver(d);
371 if (LIRC_CAN_REC(d->features)) {
372 err = lirc_allocate_buffer(irctls[minor]);
374 lirc_unregister_driver(minor);
377 return err ? err : minor;
379 EXPORT_SYMBOL(lirc_register_driver);
381 int lirc_unregister_driver(int minor)
386 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
387 pr_err("minor (%d) must be between 0 and %d!\n",
388 minor, MAX_IRCTL_DEVICES - 1);
394 pr_err("failed to get irctl\n");
400 mutex_lock(&lirc_dev_lock);
402 if (ir->d.minor != minor) {
403 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
405 mutex_unlock(&lirc_dev_lock);
409 /* end up polling thread */
411 kthread_stop(ir->task);
413 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
414 ir->d.name, ir->d.minor);
418 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
419 ir->d.name, ir->d.minor);
420 wake_up_interruptible(&ir->buf->wait_poll);
421 mutex_lock(&ir->irctl_lock);
422 ir->d.set_use_dec(ir->d.data);
423 module_put(cdev->owner);
424 mutex_unlock(&ir->irctl_lock);
426 lirc_irctl_cleanup(ir);
430 irctls[minor] = NULL;
433 mutex_unlock(&lirc_dev_lock);
437 EXPORT_SYMBOL(lirc_unregister_driver);
439 int lirc_dev_fop_open(struct inode *inode, struct file *file)
445 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
446 pr_err("open result for %d is -ENODEV\n", iminor(inode));
450 if (mutex_lock_interruptible(&lirc_dev_lock))
453 ir = irctls[iminor(inode)];
459 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
461 if (ir->d.minor == NOPLUG) {
472 retval = rc_open(ir->d.rdev);
478 if (try_module_get(cdev->owner)) {
480 retval = ir->d.set_use_inc(ir->d.data);
483 module_put(cdev->owner);
486 lirc_buffer_clear(ir->buf);
489 wake_up_process(ir->task);
493 mutex_unlock(&lirc_dev_lock);
495 nonseekable_open(inode, file);
499 EXPORT_SYMBOL(lirc_dev_fop_open);
501 int lirc_dev_fop_close(struct inode *inode, struct file *file)
503 struct irctl *ir = irctls[iminor(inode)];
508 pr_err("called with invalid irctl\n");
514 ret = mutex_lock_killable(&lirc_dev_lock);
517 rc_close(ir->d.rdev);
521 ir->d.set_use_dec(ir->d.data);
522 module_put(cdev->owner);
524 lirc_irctl_cleanup(ir);
526 irctls[ir->d.minor] = NULL;
532 mutex_unlock(&lirc_dev_lock);
536 EXPORT_SYMBOL(lirc_dev_fop_close);
538 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
540 struct irctl *ir = irctls[iminor(file_inode(file))];
544 pr_err("called with invalid irctl\n");
552 poll_wait(file, &ir->buf->wait_poll, wait);
554 if (lirc_buffer_empty(ir->buf))
557 ret = POLLIN | POLLRDNORM;
561 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
562 ir->d.name, ir->d.minor, ret);
566 EXPORT_SYMBOL(lirc_dev_fop_poll);
568 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
572 struct irctl *ir = irctls[iminor(file_inode(file))];
575 pr_err("no irctl found!\n");
579 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
580 ir->d.name, ir->d.minor, cmd);
582 if (ir->d.minor == NOPLUG || !ir->attached) {
583 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
584 ir->d.name, ir->d.minor);
588 mutex_lock(&ir->irctl_lock);
591 case LIRC_GET_FEATURES:
592 result = put_user(ir->d.features, (__u32 __user *)arg);
594 case LIRC_GET_REC_MODE:
595 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
600 result = put_user(LIRC_REC2MODE
601 (ir->d.features & LIRC_CAN_REC_MASK),
602 (__u32 __user *)arg);
604 case LIRC_SET_REC_MODE:
605 if (!(ir->d.features & LIRC_CAN_REC_MASK)) {
610 result = get_user(mode, (__u32 __user *)arg);
611 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
614 * FIXME: We should actually set the mode somehow but
615 * for now, lirc_serial doesn't support mode changing either
618 case LIRC_GET_LENGTH:
619 result = put_user(ir->d.code_length, (__u32 __user *)arg);
621 case LIRC_GET_MIN_TIMEOUT:
622 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
623 ir->d.min_timeout == 0) {
628 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
630 case LIRC_GET_MAX_TIMEOUT:
631 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
632 ir->d.max_timeout == 0) {
637 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
643 mutex_unlock(&ir->irctl_lock);
647 EXPORT_SYMBOL(lirc_dev_fop_ioctl);
649 ssize_t lirc_dev_fop_read(struct file *file,
654 struct irctl *ir = irctls[iminor(file_inode(file))];
656 int ret = 0, written = 0;
657 DECLARE_WAITQUEUE(wait, current);
660 pr_err("called with invalid irctl\n");
664 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
666 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
670 if (mutex_lock_interruptible(&ir->irctl_lock)) {
679 if (length % ir->chunk_size) {
685 * we add ourselves to the task queue before buffer check
686 * to avoid losing scan code (in case when queue is awaken somewhere
687 * between while condition checking and scheduling)
689 add_wait_queue(&ir->buf->wait_poll, &wait);
690 set_current_state(TASK_INTERRUPTIBLE);
693 * while we didn't provide 'length' bytes, device is opened in blocking
694 * mode and 'copy_to_user' is happy, wait for data.
696 while (written < length && ret == 0) {
697 if (lirc_buffer_empty(ir->buf)) {
698 /* According to the read(2) man page, 'written' can be
699 * returned as less than 'length', instead of blocking
700 * again, returning -EWOULDBLOCK, or returning
704 if (file->f_flags & O_NONBLOCK) {
708 if (signal_pending(current)) {
713 mutex_unlock(&ir->irctl_lock);
715 set_current_state(TASK_INTERRUPTIBLE);
717 if (mutex_lock_interruptible(&ir->irctl_lock)) {
719 remove_wait_queue(&ir->buf->wait_poll, &wait);
720 set_current_state(TASK_RUNNING);
729 lirc_buffer_read(ir->buf, buf);
730 ret = copy_to_user((void __user *)buffer+written, buf,
731 ir->buf->chunk_size);
733 written += ir->buf->chunk_size;
739 remove_wait_queue(&ir->buf->wait_poll, &wait);
740 set_current_state(TASK_RUNNING);
743 mutex_unlock(&ir->irctl_lock);
748 return ret ? ret : written;
750 EXPORT_SYMBOL(lirc_dev_fop_read);
752 void *lirc_get_pdata(struct file *file)
754 return irctls[iminor(file_inode(file))]->d.data;
756 EXPORT_SYMBOL(lirc_get_pdata);
759 ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
760 size_t length, loff_t *ppos)
762 struct irctl *ir = irctls[iminor(file_inode(file))];
765 pr_err("called with invalid irctl\n");
774 EXPORT_SYMBOL(lirc_dev_fop_write);
777 static int __init lirc_dev_init(void)
781 lirc_class = class_create(THIS_MODULE, "lirc");
782 if (IS_ERR(lirc_class)) {
783 pr_err("class_create failed\n");
784 return PTR_ERR(lirc_class);
787 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
790 class_destroy(lirc_class);
791 pr_err("alloc_chrdev_region failed\n");
796 pr_info("IR Remote Control driver registered, major %d\n",
797 MAJOR(lirc_base_dev));
804 static void __exit lirc_dev_exit(void)
806 class_destroy(lirc_class);
807 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
808 pr_info("module unloaded\n");
811 module_init(lirc_dev_init);
812 module_exit(lirc_dev_exit);
814 MODULE_DESCRIPTION("LIRC base driver module");
815 MODULE_AUTHOR("Artur Lipowski");
816 MODULE_LICENSE("GPL");
818 module_param(debug, bool, S_IRUGO | S_IWUSR);
819 MODULE_PARM_DESC(debug, "Enable debugging messages");