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
)
103 if (!ir
->d
.add_to_buf
)
107 * service the device as long as it is returning
108 * data and we have space
112 res
= ir
->d
.add_to_buf(ir
->d
.data
, ir
->buf
);
116 kthread_stop(ir
->task
);
118 return got_data
? 0 : res
;
121 /* main function of the polling thread
123 static int lirc_thread(void *irctl
)
125 struct irctl
*ir
= irctl
;
129 if (ir
->jiffies_to_wait
) {
130 set_current_state(TASK_INTERRUPTIBLE
);
131 schedule_timeout(ir
->jiffies_to_wait
);
133 if (kthread_should_stop())
135 if (!lirc_add_to_buf(ir
))
136 wake_up_interruptible(&ir
->buf
->wait_poll
);
138 set_current_state(TASK_INTERRUPTIBLE
);
141 } while (!kthread_should_stop());
147 static const struct file_operations lirc_dev_fops
= {
148 .owner
= THIS_MODULE
,
149 .read
= lirc_dev_fop_read
,
150 .write
= lirc_dev_fop_write
,
151 .poll
= lirc_dev_fop_poll
,
152 .unlocked_ioctl
= lirc_dev_fop_ioctl
,
154 .compat_ioctl
= lirc_dev_fop_ioctl
,
156 .open
= lirc_dev_fop_open
,
157 .release
= lirc_dev_fop_close
,
158 .llseek
= noop_llseek
,
161 static int lirc_cdev_add(struct irctl
*ir
)
163 int retval
= -ENOMEM
;
164 struct lirc_driver
*d
= &ir
->d
;
167 cdev
= kzalloc(sizeof(*cdev
), GFP_KERNEL
);
172 cdev_init(cdev
, d
->fops
);
173 cdev
->owner
= d
->owner
;
175 cdev_init(cdev
, &lirc_dev_fops
);
176 cdev
->owner
= THIS_MODULE
;
178 retval
= kobject_set_name(&cdev
->kobj
, "lirc%d", d
->minor
);
182 retval
= cdev_add(cdev
, MKDEV(MAJOR(lirc_base_dev
), d
->minor
), 1);
184 kobject_put(&cdev
->kobj
);
197 static int lirc_allocate_buffer(struct irctl
*ir
)
201 unsigned int chunk_size
;
202 unsigned int buffer_size
;
203 struct lirc_driver
*d
= &ir
->d
;
205 mutex_lock(&lirc_dev_lock
);
207 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
208 (d
->code_length
% 8 ? 1 : 0);
209 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
210 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
215 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
221 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
227 ir
->chunk_size
= ir
->buf
->chunk_size
;
230 mutex_unlock(&lirc_dev_lock
);
235 static int lirc_allocate_driver(struct lirc_driver
*d
)
242 pr_err("driver pointer must be not NULL!\n");
247 pr_err("dev pointer not filled in!\n");
251 if (d
->minor
>= MAX_IRCTL_DEVICES
) {
252 dev_err(d
->dev
, "minor must be between 0 and %d!\n",
253 MAX_IRCTL_DEVICES
- 1);
257 if (d
->code_length
< 1 || d
->code_length
> (BUFLEN
* 8)) {
258 dev_err(d
->dev
, "code length must be less than %d bits\n",
263 if (d
->sample_rate
) {
264 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
265 dev_err(d
->dev
, "invalid %d sample rate\n",
269 if (!d
->add_to_buf
) {
270 dev_err(d
->dev
, "add_to_buf not set\n");
273 } else if (!d
->rbuf
&& !(d
->fops
&& d
->fops
->read
&&
274 d
->fops
->poll
&& d
->fops
->unlocked_ioctl
)) {
275 dev_err(d
->dev
, "undefined read, poll, ioctl\n");
279 mutex_lock(&lirc_dev_lock
);
284 /* find first free slot for driver */
285 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
288 if (minor
== MAX_IRCTL_DEVICES
) {
289 dev_err(d
->dev
, "no free slots for drivers!\n");
293 } else if (irctls
[minor
]) {
294 dev_err(d
->dev
, "minor (%d) just registered!\n", minor
);
299 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
308 /* some safety check 8-) */
309 d
->name
[sizeof(d
->name
)-1] = '\0';
311 if (d
->features
== 0)
312 d
->features
= LIRC_CAN_REC_LIRCCODE
;
316 device_create(lirc_class
, ir
->d
.dev
,
317 MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
), NULL
,
318 "lirc%u", ir
->d
.minor
);
320 if (d
->sample_rate
) {
321 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
323 /* try to fire up polling thread */
324 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
325 if (IS_ERR(ir
->task
)) {
326 dev_err(d
->dev
, "cannot run thread for minor = %d\n",
332 /* it means - wait for external event in task queue */
333 ir
->jiffies_to_wait
= 0;
336 err
= lirc_cdev_add(ir
);
341 mutex_unlock(&lirc_dev_lock
);
343 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
344 ir
->d
.name
, ir
->d
.minor
);
348 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
350 mutex_unlock(&lirc_dev_lock
);
355 int lirc_register_driver(struct lirc_driver
*d
)
359 minor
= lirc_allocate_driver(d
);
363 if (LIRC_CAN_REC(d
->features
)) {
364 err
= lirc_allocate_buffer(irctls
[minor
]);
366 lirc_unregister_driver(minor
);
369 return err
? err
: minor
;
371 EXPORT_SYMBOL(lirc_register_driver
);
373 int lirc_unregister_driver(int minor
)
378 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
379 pr_err("minor (%d) must be between 0 and %d!\n",
380 minor
, MAX_IRCTL_DEVICES
- 1);
386 pr_err("failed to get irctl\n");
392 mutex_lock(&lirc_dev_lock
);
394 if (ir
->d
.minor
!= minor
) {
395 dev_err(ir
->d
.dev
, "lirc_dev: minor %d device not registered\n",
397 mutex_unlock(&lirc_dev_lock
);
401 /* end up polling thread */
403 kthread_stop(ir
->task
);
405 dev_dbg(ir
->d
.dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
406 ir
->d
.name
, ir
->d
.minor
);
410 dev_dbg(ir
->d
.dev
, LOGHEAD
"releasing opened driver\n",
411 ir
->d
.name
, ir
->d
.minor
);
412 wake_up_interruptible(&ir
->buf
->wait_poll
);
413 mutex_lock(&ir
->irctl_lock
);
415 if (ir
->d
.set_use_dec
)
416 ir
->d
.set_use_dec(ir
->d
.data
);
418 module_put(cdev
->owner
);
419 mutex_unlock(&ir
->irctl_lock
);
421 lirc_irctl_cleanup(ir
);
425 irctls
[minor
] = NULL
;
428 mutex_unlock(&lirc_dev_lock
);
432 EXPORT_SYMBOL(lirc_unregister_driver
);
434 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
440 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
441 pr_err("open result for %d is -ENODEV\n", iminor(inode
));
445 if (mutex_lock_interruptible(&lirc_dev_lock
))
448 ir
= irctls
[iminor(inode
)];
449 mutex_unlock(&lirc_dev_lock
);
456 dev_dbg(ir
->d
.dev
, LOGHEAD
"open called\n", ir
->d
.name
, ir
->d
.minor
);
458 if (ir
->d
.minor
== NOPLUG
) {
469 retval
= rc_open(ir
->d
.rdev
);
475 if (try_module_get(cdev
->owner
)) {
477 if (ir
->d
.set_use_inc
)
478 retval
= ir
->d
.set_use_inc(ir
->d
.data
);
481 module_put(cdev
->owner
);
484 lirc_buffer_clear(ir
->buf
);
487 wake_up_process(ir
->task
);
491 nonseekable_open(inode
, file
);
495 EXPORT_SYMBOL(lirc_dev_fop_open
);
497 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
)
499 struct irctl
*ir
= irctls
[iminor(inode
)];
504 pr_err("called with invalid irctl\n");
510 ret
= mutex_lock_killable(&lirc_dev_lock
);
513 rc_close(ir
->d
.rdev
);
517 if (ir
->d
.set_use_dec
)
518 ir
->d
.set_use_dec(ir
->d
.data
);
519 module_put(cdev
->owner
);
521 lirc_irctl_cleanup(ir
);
523 irctls
[ir
->d
.minor
] = NULL
;
529 mutex_unlock(&lirc_dev_lock
);
533 EXPORT_SYMBOL(lirc_dev_fop_close
);
535 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
537 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
541 pr_err("called with invalid irctl\n");
549 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
551 if (lirc_buffer_empty(ir
->buf
))
554 ret
= POLLIN
| POLLRDNORM
;
558 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
559 ir
->d
.name
, ir
->d
.minor
, ret
);
563 EXPORT_SYMBOL(lirc_dev_fop_poll
);
565 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
569 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
572 pr_err("no irctl found!\n");
576 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
577 ir
->d
.name
, ir
->d
.minor
, cmd
);
579 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
580 dev_err(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
581 ir
->d
.name
, ir
->d
.minor
);
585 mutex_lock(&ir
->irctl_lock
);
588 case LIRC_GET_FEATURES
:
589 result
= put_user(ir
->d
.features
, (__u32 __user
*)arg
);
591 case LIRC_GET_REC_MODE
:
592 if (!LIRC_CAN_REC(ir
->d
.features
)) {
597 result
= put_user(LIRC_REC2MODE
598 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
599 (__u32 __user
*)arg
);
601 case LIRC_SET_REC_MODE
:
602 if (!LIRC_CAN_REC(ir
->d
.features
)) {
607 result
= get_user(mode
, (__u32 __user
*)arg
);
608 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
611 * FIXME: We should actually set the mode somehow but
612 * for now, lirc_serial doesn't support mode changing either
615 case LIRC_GET_LENGTH
:
616 result
= put_user(ir
->d
.code_length
, (__u32 __user
*)arg
);
618 case LIRC_GET_MIN_TIMEOUT
:
619 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
620 ir
->d
.min_timeout
== 0) {
625 result
= put_user(ir
->d
.min_timeout
, (__u32 __user
*)arg
);
627 case LIRC_GET_MAX_TIMEOUT
:
628 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
629 ir
->d
.max_timeout
== 0) {
634 result
= put_user(ir
->d
.max_timeout
, (__u32 __user
*)arg
);
640 mutex_unlock(&ir
->irctl_lock
);
644 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
646 ssize_t
lirc_dev_fop_read(struct file
*file
,
651 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
653 int ret
= 0, written
= 0;
654 DECLARE_WAITQUEUE(wait
, current
);
657 pr_err("called with invalid irctl\n");
661 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
663 buf
= kzalloc(ir
->chunk_size
, GFP_KERNEL
);
667 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
676 if (length
% ir
->chunk_size
) {
682 * we add ourselves to the task queue before buffer check
683 * to avoid losing scan code (in case when queue is awaken somewhere
684 * between while condition checking and scheduling)
686 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
687 set_current_state(TASK_INTERRUPTIBLE
);
690 * while we didn't provide 'length' bytes, device is opened in blocking
691 * mode and 'copy_to_user' is happy, wait for data.
693 while (written
< length
&& ret
== 0) {
694 if (lirc_buffer_empty(ir
->buf
)) {
695 /* According to the read(2) man page, 'written' can be
696 * returned as less than 'length', instead of blocking
697 * again, returning -EWOULDBLOCK, or returning
702 if (file
->f_flags
& O_NONBLOCK
) {
706 if (signal_pending(current
)) {
711 mutex_unlock(&ir
->irctl_lock
);
713 set_current_state(TASK_INTERRUPTIBLE
);
715 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
717 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
718 set_current_state(TASK_RUNNING
);
727 lirc_buffer_read(ir
->buf
, buf
);
728 ret
= copy_to_user((void __user
*)buffer
+written
, buf
,
729 ir
->buf
->chunk_size
);
731 written
+= ir
->buf
->chunk_size
;
737 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
738 set_current_state(TASK_RUNNING
);
741 mutex_unlock(&ir
->irctl_lock
);
746 return ret
? ret
: written
;
748 EXPORT_SYMBOL(lirc_dev_fop_read
);
750 void *lirc_get_pdata(struct file
*file
)
752 return irctls
[iminor(file_inode(file
))]->d
.data
;
754 EXPORT_SYMBOL(lirc_get_pdata
);
757 ssize_t
lirc_dev_fop_write(struct file
*file
, const char __user
*buffer
,
758 size_t length
, loff_t
*ppos
)
760 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
763 pr_err("called with invalid irctl\n");
772 EXPORT_SYMBOL(lirc_dev_fop_write
);
775 static int __init
lirc_dev_init(void)
779 lirc_class
= class_create(THIS_MODULE
, "lirc");
780 if (IS_ERR(lirc_class
)) {
781 pr_err("class_create failed\n");
782 return PTR_ERR(lirc_class
);
785 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
788 class_destroy(lirc_class
);
789 pr_err("alloc_chrdev_region failed\n");
794 pr_info("IR Remote Control driver registered, major %d\n",
795 MAJOR(lirc_base_dev
));
802 static void __exit
lirc_dev_exit(void)
804 class_destroy(lirc_class
);
805 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
806 pr_info("module unloaded\n");
809 module_init(lirc_dev_init
);
810 module_exit(lirc_dev_exit
);
812 MODULE_DESCRIPTION("LIRC base driver module");
813 MODULE_AUTHOR("Artur Lipowski");
814 MODULE_LICENSE("GPL");
816 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
817 MODULE_PARM_DESC(debug
, "Enable debugging messages");