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 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/errno.h>
26 #include <linux/ioctl.h>
28 #include <linux/poll.h>
29 #include <linux/completion.h>
30 #include <linux/mutex.h>
31 #include <linux/wait.h>
32 #include <linux/unistd.h>
33 #include <linux/kthread.h>
34 #include <linux/bitops.h>
35 #include <linux/device.h>
36 #include <linux/cdev.h>
38 #include <media/lirc.h>
39 #include <media/lirc_dev.h>
43 #define IRCTL_DEV_NAME "BaseRemoteCtl"
45 #define LOGHEAD "lirc_dev (%s[%d]): "
47 static dev_t lirc_base_dev
;
54 struct mutex irctl_lock
;
55 struct lirc_buffer
*buf
;
56 unsigned int chunk_size
;
60 struct task_struct
*task
;
64 static DEFINE_MUTEX(lirc_dev_lock
);
66 static struct irctl
*irctls
[MAX_IRCTL_DEVICES
];
68 /* Only used for sysfs but defined to void otherwise */
69 static struct class *lirc_class
;
72 * initializes the irctl structure
74 static void lirc_irctl_init(struct irctl
*ir
)
76 mutex_init(&ir
->irctl_lock
);
80 static void lirc_irctl_cleanup(struct irctl
*ir
)
82 dev_dbg(ir
->d
.dev
, LOGHEAD
"cleaning up\n", ir
->d
.name
, ir
->d
.minor
);
84 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
86 if (ir
->buf
!= ir
->d
.rbuf
) {
87 lirc_buffer_free(ir
->buf
);
94 * reads key codes from driver and puts them into buffer
95 * returns 0 on success
97 static int lirc_add_to_buf(struct irctl
*ir
)
99 if (ir
->d
.add_to_buf
) {
104 * service the device as long as it is returning
105 * data and we have space
108 res
= ir
->d
.add_to_buf(ir
->d
.data
, ir
->buf
);
115 kthread_stop(ir
->task
);
117 return got_data
? 0 : res
;
123 /* main function of the polling thread
125 static int lirc_thread(void *irctl
)
127 struct irctl
*ir
= irctl
;
129 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread started\n",
130 ir
->d
.name
, ir
->d
.minor
);
134 if (ir
->jiffies_to_wait
) {
135 set_current_state(TASK_INTERRUPTIBLE
);
136 schedule_timeout(ir
->jiffies_to_wait
);
138 if (kthread_should_stop())
140 if (!lirc_add_to_buf(ir
))
141 wake_up_interruptible(&ir
->buf
->wait_poll
);
143 set_current_state(TASK_INTERRUPTIBLE
);
146 } while (!kthread_should_stop());
148 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread ended\n",
149 ir
->d
.name
, ir
->d
.minor
);
155 static struct file_operations lirc_dev_fops
= {
156 .owner
= THIS_MODULE
,
157 .read
= lirc_dev_fop_read
,
158 .write
= lirc_dev_fop_write
,
159 .poll
= lirc_dev_fop_poll
,
160 .unlocked_ioctl
= lirc_dev_fop_ioctl
,
162 .compat_ioctl
= lirc_dev_fop_ioctl
,
164 .open
= lirc_dev_fop_open
,
165 .release
= lirc_dev_fop_close
,
166 .llseek
= noop_llseek
,
169 static int lirc_cdev_add(struct irctl
*ir
)
171 int retval
= -ENOMEM
;
172 struct lirc_driver
*d
= &ir
->d
;
175 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
180 cdev_init(cdev
, d
->fops
);
181 cdev
->owner
= d
->owner
;
183 cdev_init(cdev
, &lirc_dev_fops
);
184 cdev
->owner
= THIS_MODULE
;
186 retval
= kobject_set_name(&cdev
->kobj
, "lirc%d", d
->minor
);
190 retval
= cdev_add(cdev
, MKDEV(MAJOR(lirc_base_dev
), d
->minor
), 1);
192 kobject_put(&cdev
->kobj
);
205 int lirc_register_driver(struct lirc_driver
*d
)
210 unsigned int chunk_size
;
211 unsigned int buffer_size
;
215 printk(KERN_ERR
"lirc_dev: lirc_register_driver: "
216 "driver pointer must be not NULL!\n");
222 printk(KERN_ERR
"%s: dev pointer not filled in!\n", __func__
);
227 if (MAX_IRCTL_DEVICES
<= d
->minor
) {
228 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
229 "\"minor\" must be between 0 and %d (%d)!\n",
230 MAX_IRCTL_DEVICES
- 1, d
->minor
);
235 if (1 > d
->code_length
|| (BUFLEN
* 8) < d
->code_length
) {
236 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
237 "code length in bits for minor (%d) "
238 "must be less than %d!\n",
239 d
->minor
, BUFLEN
* 8);
244 dev_dbg(d
->dev
, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
246 if (d
->sample_rate
) {
247 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
248 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
249 "sample_rate must be between 2 and %d!\n", HZ
);
253 if (!d
->add_to_buf
) {
254 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
255 "add_to_buf cannot be NULL when "
256 "sample_rate is set\n");
260 } else if (!(d
->fops
&& d
->fops
->read
) && !d
->rbuf
) {
261 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
262 "fops->read and rbuf cannot all be NULL!\n");
265 } else if (!d
->rbuf
) {
266 if (!(d
->fops
&& d
->fops
->read
&& d
->fops
->poll
&&
267 d
->fops
->unlocked_ioctl
)) {
268 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
269 "neither read, poll nor unlocked_ioctl can be NULL!\n");
275 mutex_lock(&lirc_dev_lock
);
280 /* find first free slot for driver */
281 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
284 if (MAX_IRCTL_DEVICES
== minor
) {
285 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
286 "no free slots for drivers!\n");
290 } else if (irctls
[minor
]) {
291 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
292 "minor (%d) just registered!\n", minor
);
297 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
306 if (d
->sample_rate
) {
307 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
309 /* it means - wait for external event in task queue */
310 ir
->jiffies_to_wait
= 0;
313 /* some safety check 8-) */
314 d
->name
[sizeof(d
->name
)-1] = '\0';
316 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
317 (d
->code_length
% 8 ? 1 : 0);
318 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
319 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
324 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
329 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
335 ir
->chunk_size
= ir
->buf
->chunk_size
;
337 if (d
->features
== 0)
338 d
->features
= LIRC_CAN_REC_LIRCCODE
;
342 device_create(lirc_class
, ir
->d
.dev
,
343 MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
), NULL
,
344 "lirc%u", ir
->d
.minor
);
346 if (d
->sample_rate
) {
347 /* try to fire up polling thread */
348 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
349 if (IS_ERR(ir
->task
)) {
350 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
351 "cannot run poll thread for minor = %d\n",
358 err
= lirc_cdev_add(ir
);
363 mutex_unlock(&lirc_dev_lock
);
365 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
366 ir
->d
.name
, ir
->d
.minor
);
370 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
372 mutex_unlock(&lirc_dev_lock
);
376 EXPORT_SYMBOL(lirc_register_driver
);
378 int lirc_unregister_driver(int minor
)
383 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
384 printk(KERN_ERR
"lirc_dev: %s: minor (%d) must be between "
385 "0 and %d!\n", __func__
, minor
, MAX_IRCTL_DEVICES
- 1);
391 printk(KERN_ERR
"lirc_dev: %s: failed to get irctl struct "
392 "for minor %d!\n", __func__
, minor
);
398 mutex_lock(&lirc_dev_lock
);
400 if (ir
->d
.minor
!= minor
) {
401 printk(KERN_ERR
"lirc_dev: %s: minor (%d) device not "
402 "registered!\n", __func__
, minor
);
403 mutex_unlock(&lirc_dev_lock
);
407 /* end up polling thread */
409 kthread_stop(ir
->task
);
411 dev_dbg(ir
->d
.dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
412 ir
->d
.name
, ir
->d
.minor
);
416 dev_dbg(ir
->d
.dev
, LOGHEAD
"releasing opened driver\n",
417 ir
->d
.name
, ir
->d
.minor
);
418 wake_up_interruptible(&ir
->buf
->wait_poll
);
419 mutex_lock(&ir
->irctl_lock
);
420 ir
->d
.set_use_dec(ir
->d
.data
);
421 module_put(cdev
->owner
);
422 mutex_unlock(&ir
->irctl_lock
);
424 lirc_irctl_cleanup(ir
);
428 irctls
[minor
] = NULL
;
431 mutex_unlock(&lirc_dev_lock
);
435 EXPORT_SYMBOL(lirc_unregister_driver
);
437 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
443 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
444 printk(KERN_WARNING
"lirc_dev [%d]: open result = -ENODEV\n",
449 if (mutex_lock_interruptible(&lirc_dev_lock
))
452 ir
= irctls
[iminor(inode
)];
458 dev_dbg(ir
->d
.dev
, LOGHEAD
"open called\n", ir
->d
.name
, ir
->d
.minor
);
460 if (ir
->d
.minor
== NOPLUG
) {
471 if (try_module_get(cdev
->owner
)) {
473 retval
= ir
->d
.set_use_inc(ir
->d
.data
);
476 module_put(cdev
->owner
);
479 lirc_buffer_clear(ir
->buf
);
482 wake_up_process(ir
->task
);
487 dev_dbg(ir
->d
.dev
, LOGHEAD
"open result = %d\n",
488 ir
->d
.name
, ir
->d
.minor
, retval
);
490 mutex_unlock(&lirc_dev_lock
);
492 nonseekable_open(inode
, file
);
496 EXPORT_SYMBOL(lirc_dev_fop_open
);
498 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
)
500 struct irctl
*ir
= irctls
[iminor(inode
)];
504 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
510 dev_dbg(ir
->d
.dev
, LOGHEAD
"close called\n", ir
->d
.name
, ir
->d
.minor
);
512 WARN_ON(mutex_lock_killable(&lirc_dev_lock
));
516 ir
->d
.set_use_dec(ir
->d
.data
);
517 module_put(cdev
->owner
);
519 lirc_irctl_cleanup(ir
);
521 irctls
[ir
->d
.minor
] = NULL
;
526 mutex_unlock(&lirc_dev_lock
);
530 EXPORT_SYMBOL(lirc_dev_fop_close
);
532 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
534 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
538 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
542 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll called\n", ir
->d
.name
, ir
->d
.minor
);
547 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
550 if (lirc_buffer_empty(ir
->buf
))
553 ret
= POLLIN
| POLLRDNORM
;
557 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
558 ir
->d
.name
, ir
->d
.minor
, ret
);
562 EXPORT_SYMBOL(lirc_dev_fop_poll
);
564 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
568 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
571 printk(KERN_ERR
"lirc_dev: %s: no irctl found!\n", __func__
);
575 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
576 ir
->d
.name
, ir
->d
.minor
, cmd
);
578 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
579 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
580 ir
->d
.name
, ir
->d
.minor
);
584 mutex_lock(&ir
->irctl_lock
);
587 case LIRC_GET_FEATURES
:
588 result
= put_user(ir
->d
.features
, (__u32
*)arg
);
590 case LIRC_GET_REC_MODE
:
591 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
596 result
= put_user(LIRC_REC2MODE
597 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
600 case LIRC_SET_REC_MODE
:
601 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
606 result
= get_user(mode
, (__u32
*)arg
);
607 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
610 * FIXME: We should actually set the mode somehow but
611 * for now, lirc_serial doesn't support mode changing either
614 case LIRC_GET_LENGTH
:
615 result
= put_user(ir
->d
.code_length
, (__u32
*)arg
);
617 case LIRC_GET_MIN_TIMEOUT
:
618 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
619 ir
->d
.min_timeout
== 0) {
624 result
= put_user(ir
->d
.min_timeout
, (__u32
*)arg
);
626 case LIRC_GET_MAX_TIMEOUT
:
627 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
628 ir
->d
.max_timeout
== 0) {
633 result
= put_user(ir
->d
.max_timeout
, (__u32
*)arg
);
639 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = %d\n",
640 ir
->d
.name
, ir
->d
.minor
, result
);
642 mutex_unlock(&ir
->irctl_lock
);
646 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
648 ssize_t
lirc_dev_fop_read(struct file
*file
,
653 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
655 int ret
= 0, written
= 0;
656 DECLARE_WAITQUEUE(wait
, current
);
659 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
663 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
665 buf
= kzalloc(ir
->chunk_size
, GFP_KERNEL
);
669 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
678 if (length
% ir
->chunk_size
) {
684 * we add ourselves to the task queue before buffer check
685 * to avoid losing scan code (in case when queue is awaken somewhere
686 * between while condition checking and scheduling)
688 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
689 set_current_state(TASK_INTERRUPTIBLE
);
692 * while we didn't provide 'length' bytes, device is opened in blocking
693 * mode and 'copy_to_user' is happy, wait for data.
695 while (written
< length
&& ret
== 0) {
696 if (lirc_buffer_empty(ir
->buf
)) {
697 /* According to the read(2) man page, 'written' can be
698 * returned as less than 'length', instead of blocking
699 * again, returning -EWOULDBLOCK, or returning
703 if (file
->f_flags
& O_NONBLOCK
) {
707 if (signal_pending(current
)) {
712 mutex_unlock(&ir
->irctl_lock
);
714 set_current_state(TASK_INTERRUPTIBLE
);
716 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
718 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
719 set_current_state(TASK_RUNNING
);
728 lirc_buffer_read(ir
->buf
, buf
);
729 ret
= copy_to_user((void *)buffer
+written
, buf
,
730 ir
->buf
->chunk_size
);
732 written
+= ir
->buf
->chunk_size
;
738 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
739 set_current_state(TASK_RUNNING
);
742 mutex_unlock(&ir
->irctl_lock
);
746 dev_dbg(ir
->d
.dev
, LOGHEAD
"read result = %s (%d)\n",
747 ir
->d
.name
, ir
->d
.minor
, ret
? "<fail>" : "<ok>", ret
);
749 return ret
? ret
: written
;
751 EXPORT_SYMBOL(lirc_dev_fop_read
);
753 void *lirc_get_pdata(struct file
*file
)
757 if (file
&& file
->f_dentry
&& file
->f_dentry
->d_inode
&&
758 file
->f_dentry
->d_inode
->i_rdev
) {
760 ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
766 EXPORT_SYMBOL(lirc_get_pdata
);
769 ssize_t
lirc_dev_fop_write(struct file
*file
, const char __user
*buffer
,
770 size_t length
, loff_t
*ppos
)
772 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
775 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
779 dev_dbg(ir
->d
.dev
, LOGHEAD
"write called\n", ir
->d
.name
, ir
->d
.minor
);
786 EXPORT_SYMBOL(lirc_dev_fop_write
);
789 static int __init
lirc_dev_init(void)
793 lirc_class
= class_create(THIS_MODULE
, "lirc");
794 if (IS_ERR(lirc_class
)) {
795 retval
= PTR_ERR(lirc_class
);
796 printk(KERN_ERR
"lirc_dev: class_create failed\n");
800 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
803 class_destroy(lirc_class
);
804 printk(KERN_ERR
"lirc_dev: alloc_chrdev_region failed\n");
809 printk(KERN_INFO
"lirc_dev: IR Remote Control driver registered, "
810 "major %d \n", MAJOR(lirc_base_dev
));
818 static void __exit
lirc_dev_exit(void)
820 class_destroy(lirc_class
);
821 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
822 printk(KERN_INFO
"lirc_dev: module unloaded\n");
825 module_init(lirc_dev_init
);
826 module_exit(lirc_dev_exit
);
828 MODULE_DESCRIPTION("LIRC base driver module");
829 MODULE_AUTHOR("Artur Lipowski");
830 MODULE_LICENSE("GPL");
832 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
833 MODULE_PARM_DESC(debug
, "Enable debugging messages");