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/rc-core.h>
39 #include <media/lirc.h>
40 #include <media/lirc_dev.h>
44 #define IRCTL_DEV_NAME "BaseRemoteCtl"
46 #define LOGHEAD "lirc_dev (%s[%d]): "
48 static dev_t lirc_base_dev
;
55 struct mutex irctl_lock
;
56 struct lirc_buffer
*buf
;
57 unsigned int chunk_size
;
61 struct task_struct
*task
;
65 static DEFINE_MUTEX(lirc_dev_lock
);
67 static struct irctl
*irctls
[MAX_IRCTL_DEVICES
];
69 /* Only used for sysfs but defined to void otherwise */
70 static struct class *lirc_class
;
73 * initializes the irctl structure
75 static void lirc_irctl_init(struct irctl
*ir
)
77 mutex_init(&ir
->irctl_lock
);
81 static void lirc_irctl_cleanup(struct irctl
*ir
)
83 dev_dbg(ir
->d
.dev
, LOGHEAD
"cleaning up\n", ir
->d
.name
, ir
->d
.minor
);
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
;
130 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread started\n",
131 ir
->d
.name
, ir
->d
.minor
);
135 if (ir
->jiffies_to_wait
) {
136 set_current_state(TASK_INTERRUPTIBLE
);
137 schedule_timeout(ir
->jiffies_to_wait
);
139 if (kthread_should_stop())
141 if (!lirc_add_to_buf(ir
))
142 wake_up_interruptible(&ir
->buf
->wait_poll
);
144 set_current_state(TASK_INTERRUPTIBLE
);
147 } while (!kthread_should_stop());
149 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread ended\n",
150 ir
->d
.name
, ir
->d
.minor
);
156 static const struct file_operations lirc_dev_fops
= {
157 .owner
= THIS_MODULE
,
158 .read
= lirc_dev_fop_read
,
159 .write
= lirc_dev_fop_write
,
160 .poll
= lirc_dev_fop_poll
,
161 .unlocked_ioctl
= lirc_dev_fop_ioctl
,
163 .compat_ioctl
= lirc_dev_fop_ioctl
,
165 .open
= lirc_dev_fop_open
,
166 .release
= lirc_dev_fop_close
,
167 .llseek
= noop_llseek
,
170 static int lirc_cdev_add(struct irctl
*ir
)
172 int retval
= -ENOMEM
;
173 struct lirc_driver
*d
= &ir
->d
;
176 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
181 cdev_init(cdev
, d
->fops
);
182 cdev
->owner
= d
->owner
;
184 cdev_init(cdev
, &lirc_dev_fops
);
185 cdev
->owner
= THIS_MODULE
;
187 retval
= kobject_set_name(&cdev
->kobj
, "lirc%d", d
->minor
);
191 retval
= cdev_add(cdev
, MKDEV(MAJOR(lirc_base_dev
), d
->minor
), 1);
193 kobject_put(&cdev
->kobj
);
206 int lirc_register_driver(struct lirc_driver
*d
)
211 unsigned int chunk_size
;
212 unsigned int buffer_size
;
216 printk(KERN_ERR
"lirc_dev: lirc_register_driver: "
217 "driver pointer must be not NULL!\n");
223 printk(KERN_ERR
"%s: dev pointer not filled in!\n", __func__
);
228 if (MAX_IRCTL_DEVICES
<= d
->minor
) {
229 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
230 "\"minor\" must be between 0 and %d (%d)!\n",
231 MAX_IRCTL_DEVICES
- 1, d
->minor
);
236 if (1 > d
->code_length
|| (BUFLEN
* 8) < d
->code_length
) {
237 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
238 "code length in bits for minor (%d) "
239 "must be less than %d!\n",
240 d
->minor
, BUFLEN
* 8);
245 dev_dbg(d
->dev
, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
247 if (d
->sample_rate
) {
248 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
249 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
250 "sample_rate must be between 2 and %d!\n", HZ
);
254 if (!d
->add_to_buf
) {
255 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
256 "add_to_buf cannot be NULL when "
257 "sample_rate is set\n");
261 } else if (!(d
->fops
&& d
->fops
->read
) && !d
->rbuf
) {
262 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
263 "fops->read and rbuf cannot all be NULL!\n");
266 } else if (!d
->rbuf
) {
267 if (!(d
->fops
&& d
->fops
->read
&& d
->fops
->poll
&&
268 d
->fops
->unlocked_ioctl
)) {
269 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
270 "neither read, poll nor unlocked_ioctl can be NULL!\n");
276 mutex_lock(&lirc_dev_lock
);
281 /* find first free slot for driver */
282 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
285 if (MAX_IRCTL_DEVICES
== minor
) {
286 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
287 "no free slots for drivers!\n");
291 } else if (irctls
[minor
]) {
292 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
293 "minor (%d) just registered!\n", minor
);
298 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
307 if (d
->sample_rate
) {
308 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
310 /* it means - wait for external event in task queue */
311 ir
->jiffies_to_wait
= 0;
314 /* some safety check 8-) */
315 d
->name
[sizeof(d
->name
)-1] = '\0';
317 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
318 (d
->code_length
% 8 ? 1 : 0);
319 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
320 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
325 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
330 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
336 ir
->chunk_size
= ir
->buf
->chunk_size
;
338 if (d
->features
== 0)
339 d
->features
= LIRC_CAN_REC_LIRCCODE
;
343 device_create(lirc_class
, ir
->d
.dev
,
344 MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
), NULL
,
345 "lirc%u", ir
->d
.minor
);
347 if (d
->sample_rate
) {
348 /* try to fire up polling thread */
349 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
350 if (IS_ERR(ir
->task
)) {
351 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
352 "cannot run poll thread for minor = %d\n",
359 err
= lirc_cdev_add(ir
);
364 mutex_unlock(&lirc_dev_lock
);
366 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
367 ir
->d
.name
, ir
->d
.minor
);
371 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
373 mutex_unlock(&lirc_dev_lock
);
377 EXPORT_SYMBOL(lirc_register_driver
);
379 int lirc_unregister_driver(int minor
)
384 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
385 printk(KERN_ERR
"lirc_dev: %s: minor (%d) must be between "
386 "0 and %d!\n", __func__
, minor
, MAX_IRCTL_DEVICES
- 1);
392 printk(KERN_ERR
"lirc_dev: %s: failed to get irctl struct "
393 "for minor %d!\n", __func__
, minor
);
399 mutex_lock(&lirc_dev_lock
);
401 if (ir
->d
.minor
!= minor
) {
402 printk(KERN_ERR
"lirc_dev: %s: minor (%d) device not "
403 "registered!\n", __func__
, minor
);
404 mutex_unlock(&lirc_dev_lock
);
408 /* end up polling thread */
410 kthread_stop(ir
->task
);
412 dev_dbg(ir
->d
.dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
413 ir
->d
.name
, ir
->d
.minor
);
417 dev_dbg(ir
->d
.dev
, LOGHEAD
"releasing opened driver\n",
418 ir
->d
.name
, ir
->d
.minor
);
419 wake_up_interruptible(&ir
->buf
->wait_poll
);
420 mutex_lock(&ir
->irctl_lock
);
421 ir
->d
.set_use_dec(ir
->d
.data
);
422 module_put(cdev
->owner
);
423 mutex_unlock(&ir
->irctl_lock
);
425 lirc_irctl_cleanup(ir
);
429 irctls
[minor
] = NULL
;
432 mutex_unlock(&lirc_dev_lock
);
436 EXPORT_SYMBOL(lirc_unregister_driver
);
438 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
444 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
445 printk(KERN_WARNING
"lirc_dev [%d]: open result = -ENODEV\n",
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
);
494 dev_dbg(ir
->d
.dev
, LOGHEAD
"open result = %d\n",
495 ir
->d
.name
, ir
->d
.minor
, retval
);
497 mutex_unlock(&lirc_dev_lock
);
499 nonseekable_open(inode
, file
);
503 EXPORT_SYMBOL(lirc_dev_fop_open
);
505 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
)
507 struct irctl
*ir
= irctls
[iminor(inode
)];
511 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
517 dev_dbg(ir
->d
.dev
, LOGHEAD
"close called\n", ir
->d
.name
, ir
->d
.minor
);
519 WARN_ON(mutex_lock_killable(&lirc_dev_lock
));
522 rc_close(ir
->d
.rdev
);
526 ir
->d
.set_use_dec(ir
->d
.data
);
527 module_put(cdev
->owner
);
529 lirc_irctl_cleanup(ir
);
531 irctls
[ir
->d
.minor
] = NULL
;
536 mutex_unlock(&lirc_dev_lock
);
540 EXPORT_SYMBOL(lirc_dev_fop_close
);
542 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
544 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
548 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
552 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll called\n", ir
->d
.name
, ir
->d
.minor
);
557 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
560 if (lirc_buffer_empty(ir
->buf
))
563 ret
= POLLIN
| POLLRDNORM
;
567 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
568 ir
->d
.name
, ir
->d
.minor
, ret
);
572 EXPORT_SYMBOL(lirc_dev_fop_poll
);
574 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
578 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
581 printk(KERN_ERR
"lirc_dev: %s: no irctl found!\n", __func__
);
585 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
586 ir
->d
.name
, ir
->d
.minor
, cmd
);
588 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
589 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
590 ir
->d
.name
, ir
->d
.minor
);
594 mutex_lock(&ir
->irctl_lock
);
597 case LIRC_GET_FEATURES
:
598 result
= put_user(ir
->d
.features
, (__u32
*)arg
);
600 case LIRC_GET_REC_MODE
:
601 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
606 result
= put_user(LIRC_REC2MODE
607 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
610 case LIRC_SET_REC_MODE
:
611 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
616 result
= get_user(mode
, (__u32
*)arg
);
617 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
620 * FIXME: We should actually set the mode somehow but
621 * for now, lirc_serial doesn't support mode changing either
624 case LIRC_GET_LENGTH
:
625 result
= put_user(ir
->d
.code_length
, (__u32
*)arg
);
627 case LIRC_GET_MIN_TIMEOUT
:
628 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
629 ir
->d
.min_timeout
== 0) {
634 result
= put_user(ir
->d
.min_timeout
, (__u32
*)arg
);
636 case LIRC_GET_MAX_TIMEOUT
:
637 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
638 ir
->d
.max_timeout
== 0) {
643 result
= put_user(ir
->d
.max_timeout
, (__u32
*)arg
);
649 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = %d\n",
650 ir
->d
.name
, ir
->d
.minor
, result
);
652 mutex_unlock(&ir
->irctl_lock
);
656 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
658 ssize_t
lirc_dev_fop_read(struct file
*file
,
663 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
665 int ret
= 0, written
= 0;
666 DECLARE_WAITQUEUE(wait
, current
);
669 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
673 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
675 buf
= kzalloc(ir
->chunk_size
, GFP_KERNEL
);
679 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
688 if (length
% ir
->chunk_size
) {
694 * we add ourselves to the task queue before buffer check
695 * to avoid losing scan code (in case when queue is awaken somewhere
696 * between while condition checking and scheduling)
698 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
699 set_current_state(TASK_INTERRUPTIBLE
);
702 * while we didn't provide 'length' bytes, device is opened in blocking
703 * mode and 'copy_to_user' is happy, wait for data.
705 while (written
< length
&& ret
== 0) {
706 if (lirc_buffer_empty(ir
->buf
)) {
707 /* According to the read(2) man page, 'written' can be
708 * returned as less than 'length', instead of blocking
709 * again, returning -EWOULDBLOCK, or returning
713 if (file
->f_flags
& O_NONBLOCK
) {
717 if (signal_pending(current
)) {
722 mutex_unlock(&ir
->irctl_lock
);
724 set_current_state(TASK_INTERRUPTIBLE
);
726 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
728 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
729 set_current_state(TASK_RUNNING
);
738 lirc_buffer_read(ir
->buf
, buf
);
739 ret
= copy_to_user((void *)buffer
+written
, buf
,
740 ir
->buf
->chunk_size
);
742 written
+= ir
->buf
->chunk_size
;
748 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
749 set_current_state(TASK_RUNNING
);
752 mutex_unlock(&ir
->irctl_lock
);
756 dev_dbg(ir
->d
.dev
, LOGHEAD
"read result = %s (%d)\n",
757 ir
->d
.name
, ir
->d
.minor
, ret
? "<fail>" : "<ok>", ret
);
759 return ret
? ret
: written
;
761 EXPORT_SYMBOL(lirc_dev_fop_read
);
763 void *lirc_get_pdata(struct file
*file
)
765 return irctls
[iminor(file_inode(file
))]->d
.data
;
767 EXPORT_SYMBOL(lirc_get_pdata
);
770 ssize_t
lirc_dev_fop_write(struct file
*file
, const char __user
*buffer
,
771 size_t length
, loff_t
*ppos
)
773 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
776 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
780 dev_dbg(ir
->d
.dev
, LOGHEAD
"write called\n", ir
->d
.name
, ir
->d
.minor
);
787 EXPORT_SYMBOL(lirc_dev_fop_write
);
790 static int __init
lirc_dev_init(void)
794 lirc_class
= class_create(THIS_MODULE
, "lirc");
795 if (IS_ERR(lirc_class
)) {
796 retval
= PTR_ERR(lirc_class
);
797 printk(KERN_ERR
"lirc_dev: class_create failed\n");
801 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
804 class_destroy(lirc_class
);
805 printk(KERN_ERR
"lirc_dev: alloc_chrdev_region failed\n");
810 printk(KERN_INFO
"lirc_dev: IR Remote Control driver registered, "
811 "major %d \n", MAJOR(lirc_base_dev
));
819 static void __exit
lirc_dev_exit(void)
821 class_destroy(lirc_class
);
822 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
823 printk(KERN_INFO
"lirc_dev: module unloaded\n");
826 module_init(lirc_dev_init
);
827 module_exit(lirc_dev_exit
);
829 MODULE_DESCRIPTION("LIRC base driver module");
830 MODULE_AUTHOR("Artur Lipowski");
831 MODULE_LICENSE("GPL");
833 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
834 MODULE_PARM_DESC(debug
, "Enable debugging messages");