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.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/errno.h>
24 #include <linux/ioctl.h>
26 #include <linux/poll.h>
27 #include <linux/completion.h>
28 #include <linux/mutex.h>
29 #include <linux/wait.h>
30 #include <linux/unistd.h>
31 #include <linux/kthread.h>
32 #include <linux/bitops.h>
33 #include <linux/device.h>
34 #include <linux/cdev.h>
36 #include <media/rc-core.h>
37 #include <media/lirc.h>
38 #include <media/lirc_dev.h>
42 #define IRCTL_DEV_NAME "BaseRemoteCtl"
44 #define LOGHEAD "lirc_dev (%s[%d]): "
46 static dev_t lirc_base_dev
;
53 struct mutex irctl_lock
;
54 struct lirc_buffer
*buf
;
55 unsigned int chunk_size
;
59 struct task_struct
*task
;
63 static DEFINE_MUTEX(lirc_dev_lock
);
65 static struct irctl
*irctls
[MAX_IRCTL_DEVICES
];
67 /* Only used for sysfs but defined to void otherwise */
68 static struct class *lirc_class
;
71 * initializes the irctl structure
73 static void lirc_irctl_init(struct irctl
*ir
)
75 mutex_init(&ir
->irctl_lock
);
79 static void lirc_irctl_cleanup(struct irctl
*ir
)
81 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
83 if (ir
->buf
!= ir
->d
.rbuf
) {
84 lirc_buffer_free(ir
->buf
);
91 * reads key codes from driver and puts them into buffer
92 * returns 0 on success
94 static int lirc_add_to_buf(struct irctl
*ir
)
99 if (!ir
->d
.add_to_buf
)
103 * service the device as long as it is returning
104 * data and we have space
108 res
= ir
->d
.add_to_buf(ir
->d
.data
, ir
->buf
);
112 kthread_stop(ir
->task
);
114 return got_data
? 0 : res
;
117 /* main function of the polling thread
119 static int lirc_thread(void *irctl
)
121 struct irctl
*ir
= irctl
;
125 if (ir
->jiffies_to_wait
) {
126 set_current_state(TASK_INTERRUPTIBLE
);
127 schedule_timeout(ir
->jiffies_to_wait
);
129 if (kthread_should_stop())
131 if (!lirc_add_to_buf(ir
))
132 wake_up_interruptible(&ir
->buf
->wait_poll
);
134 set_current_state(TASK_INTERRUPTIBLE
);
137 } while (!kthread_should_stop());
143 static const struct file_operations lirc_dev_fops
= {
144 .owner
= THIS_MODULE
,
145 .read
= lirc_dev_fop_read
,
146 .write
= lirc_dev_fop_write
,
147 .poll
= lirc_dev_fop_poll
,
148 .unlocked_ioctl
= lirc_dev_fop_ioctl
,
149 .open
= lirc_dev_fop_open
,
150 .release
= lirc_dev_fop_close
,
151 .llseek
= noop_llseek
,
154 static int lirc_cdev_add(struct irctl
*ir
)
156 struct lirc_driver
*d
= &ir
->d
;
166 cdev
->owner
= d
->owner
;
168 cdev
->ops
= &lirc_dev_fops
;
169 cdev
->owner
= THIS_MODULE
;
171 retval
= kobject_set_name(&cdev
->kobj
, "lirc%d", d
->minor
);
175 retval
= cdev_add(cdev
, MKDEV(MAJOR(lirc_base_dev
), d
->minor
), 1);
188 static int lirc_allocate_buffer(struct irctl
*ir
)
192 unsigned int chunk_size
;
193 unsigned int buffer_size
;
194 struct lirc_driver
*d
= &ir
->d
;
196 mutex_lock(&lirc_dev_lock
);
198 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
199 (d
->code_length
% 8 ? 1 : 0);
200 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
201 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
206 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
212 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
218 ir
->chunk_size
= ir
->buf
->chunk_size
;
221 mutex_unlock(&lirc_dev_lock
);
226 static int lirc_allocate_driver(struct lirc_driver
*d
)
233 pr_err("driver pointer must be not NULL!\n");
238 pr_err("dev pointer not filled in!\n");
242 if (d
->minor
>= MAX_IRCTL_DEVICES
) {
243 dev_err(d
->dev
, "minor must be between 0 and %d!\n",
244 MAX_IRCTL_DEVICES
- 1);
248 if (d
->code_length
< 1 || d
->code_length
> (BUFLEN
* 8)) {
249 dev_err(d
->dev
, "code length must be less than %d bits\n",
254 if (d
->sample_rate
) {
255 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
256 dev_err(d
->dev
, "invalid %d sample rate\n",
260 if (!d
->add_to_buf
) {
261 dev_err(d
->dev
, "add_to_buf not set\n");
264 } else if (!d
->rbuf
&& !(d
->fops
&& d
->fops
->read
&&
265 d
->fops
->poll
&& d
->fops
->unlocked_ioctl
)) {
266 dev_err(d
->dev
, "undefined read, poll, ioctl\n");
270 mutex_lock(&lirc_dev_lock
);
275 /* find first free slot for driver */
276 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
279 if (minor
== MAX_IRCTL_DEVICES
) {
280 dev_err(d
->dev
, "no free slots for drivers!\n");
284 } else if (irctls
[minor
]) {
285 dev_err(d
->dev
, "minor (%d) just registered!\n", minor
);
290 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
299 /* some safety check 8-) */
300 d
->name
[sizeof(d
->name
)-1] = '\0';
302 if (d
->features
== 0)
303 d
->features
= LIRC_CAN_REC_LIRCCODE
;
307 device_create(lirc_class
, ir
->d
.dev
,
308 MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
), NULL
,
309 "lirc%u", ir
->d
.minor
);
311 if (d
->sample_rate
) {
312 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
314 /* try to fire up polling thread */
315 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
316 if (IS_ERR(ir
->task
)) {
317 dev_err(d
->dev
, "cannot run thread for minor = %d\n",
323 /* it means - wait for external event in task queue */
324 ir
->jiffies_to_wait
= 0;
327 err
= lirc_cdev_add(ir
);
332 mutex_unlock(&lirc_dev_lock
);
334 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
335 ir
->d
.name
, ir
->d
.minor
);
339 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
341 mutex_unlock(&lirc_dev_lock
);
346 int lirc_register_driver(struct lirc_driver
*d
)
350 minor
= lirc_allocate_driver(d
);
354 if (LIRC_CAN_REC(d
->features
)) {
355 err
= lirc_allocate_buffer(irctls
[minor
]);
357 lirc_unregister_driver(minor
);
360 return err
? err
: minor
;
362 EXPORT_SYMBOL(lirc_register_driver
);
364 int lirc_unregister_driver(int minor
)
369 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
370 pr_err("minor (%d) must be between 0 and %d!\n",
371 minor
, MAX_IRCTL_DEVICES
- 1);
377 pr_err("failed to get irctl\n");
383 mutex_lock(&lirc_dev_lock
);
385 if (ir
->d
.minor
!= minor
) {
386 dev_err(ir
->d
.dev
, "lirc_dev: minor %d device not registered\n",
388 mutex_unlock(&lirc_dev_lock
);
392 /* end up polling thread */
394 kthread_stop(ir
->task
);
396 dev_dbg(ir
->d
.dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
397 ir
->d
.name
, ir
->d
.minor
);
401 dev_dbg(ir
->d
.dev
, LOGHEAD
"releasing opened driver\n",
402 ir
->d
.name
, ir
->d
.minor
);
403 wake_up_interruptible(&ir
->buf
->wait_poll
);
404 mutex_lock(&ir
->irctl_lock
);
406 if (ir
->d
.set_use_dec
)
407 ir
->d
.set_use_dec(ir
->d
.data
);
409 module_put(cdev
->owner
);
410 mutex_unlock(&ir
->irctl_lock
);
412 lirc_irctl_cleanup(ir
);
415 irctls
[minor
] = NULL
;
418 mutex_unlock(&lirc_dev_lock
);
422 EXPORT_SYMBOL(lirc_unregister_driver
);
424 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
430 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
431 pr_err("open result for %d is -ENODEV\n", iminor(inode
));
435 if (mutex_lock_interruptible(&lirc_dev_lock
))
438 ir
= irctls
[iminor(inode
)];
444 dev_dbg(ir
->d
.dev
, LOGHEAD
"open called\n", ir
->d
.name
, ir
->d
.minor
);
446 if (ir
->d
.minor
== NOPLUG
) {
457 retval
= rc_open(ir
->d
.rdev
);
463 if (try_module_get(cdev
->owner
)) {
465 if (ir
->d
.set_use_inc
)
466 retval
= ir
->d
.set_use_inc(ir
->d
.data
);
469 module_put(cdev
->owner
);
471 } else if (ir
->buf
) {
472 lirc_buffer_clear(ir
->buf
);
475 wake_up_process(ir
->task
);
479 mutex_unlock(&lirc_dev_lock
);
481 nonseekable_open(inode
, file
);
485 EXPORT_SYMBOL(lirc_dev_fop_open
);
487 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
)
489 struct irctl
*ir
= irctls
[iminor(inode
)];
494 pr_err("called with invalid irctl\n");
500 ret
= mutex_lock_killable(&lirc_dev_lock
);
503 rc_close(ir
->d
.rdev
);
507 if (ir
->d
.set_use_dec
)
508 ir
->d
.set_use_dec(ir
->d
.data
);
509 module_put(cdev
->owner
);
511 lirc_irctl_cleanup(ir
);
513 irctls
[ir
->d
.minor
] = NULL
;
518 mutex_unlock(&lirc_dev_lock
);
522 EXPORT_SYMBOL(lirc_dev_fop_close
);
524 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
526 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
530 pr_err("called with invalid irctl\n");
538 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
540 if (lirc_buffer_empty(ir
->buf
))
543 ret
= POLLIN
| POLLRDNORM
;
547 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
548 ir
->d
.name
, ir
->d
.minor
, ret
);
552 EXPORT_SYMBOL(lirc_dev_fop_poll
);
554 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
558 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
561 pr_err("no irctl found!\n");
565 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
566 ir
->d
.name
, ir
->d
.minor
, cmd
);
568 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
569 dev_err(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
570 ir
->d
.name
, ir
->d
.minor
);
574 mutex_lock(&ir
->irctl_lock
);
577 case LIRC_GET_FEATURES
:
578 result
= put_user(ir
->d
.features
, (__u32 __user
*)arg
);
580 case LIRC_GET_REC_MODE
:
581 if (!LIRC_CAN_REC(ir
->d
.features
)) {
586 result
= put_user(LIRC_REC2MODE
587 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
588 (__u32 __user
*)arg
);
590 case LIRC_SET_REC_MODE
:
591 if (!LIRC_CAN_REC(ir
->d
.features
)) {
596 result
= get_user(mode
, (__u32 __user
*)arg
);
597 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
600 * FIXME: We should actually set the mode somehow but
601 * for now, lirc_serial doesn't support mode changing either
604 case LIRC_GET_LENGTH
:
605 result
= put_user(ir
->d
.code_length
, (__u32 __user
*)arg
);
607 case LIRC_GET_MIN_TIMEOUT
:
608 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
609 ir
->d
.min_timeout
== 0) {
614 result
= put_user(ir
->d
.min_timeout
, (__u32 __user
*)arg
);
616 case LIRC_GET_MAX_TIMEOUT
:
617 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
618 ir
->d
.max_timeout
== 0) {
623 result
= put_user(ir
->d
.max_timeout
, (__u32 __user
*)arg
);
629 mutex_unlock(&ir
->irctl_lock
);
633 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
635 ssize_t
lirc_dev_fop_read(struct file
*file
,
640 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
642 int ret
= 0, written
= 0;
643 DECLARE_WAITQUEUE(wait
, current
);
646 pr_err("called with invalid irctl\n");
650 if (!LIRC_CAN_REC(ir
->d
.features
))
653 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
655 buf
= kzalloc(ir
->chunk_size
, GFP_KERNEL
);
659 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
668 if (length
% ir
->chunk_size
) {
674 * we add ourselves to the task queue before buffer check
675 * to avoid losing scan code (in case when queue is awaken somewhere
676 * between while condition checking and scheduling)
678 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
681 * while we didn't provide 'length' bytes, device is opened in blocking
682 * mode and 'copy_to_user' is happy, wait for data.
684 while (written
< length
&& ret
== 0) {
685 if (lirc_buffer_empty(ir
->buf
)) {
686 /* According to the read(2) man page, 'written' can be
687 * returned as less than 'length', instead of blocking
688 * again, returning -EWOULDBLOCK, or returning
693 if (file
->f_flags
& O_NONBLOCK
) {
697 if (signal_pending(current
)) {
702 mutex_unlock(&ir
->irctl_lock
);
703 set_current_state(TASK_INTERRUPTIBLE
);
705 set_current_state(TASK_RUNNING
);
707 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
709 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
718 lirc_buffer_read(ir
->buf
, buf
);
719 ret
= copy_to_user((void __user
*)buffer
+written
, buf
,
720 ir
->buf
->chunk_size
);
722 written
+= ir
->buf
->chunk_size
;
728 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
731 mutex_unlock(&ir
->irctl_lock
);
736 return ret
? ret
: written
;
738 EXPORT_SYMBOL(lirc_dev_fop_read
);
740 void *lirc_get_pdata(struct file
*file
)
742 return irctls
[iminor(file_inode(file
))]->d
.data
;
744 EXPORT_SYMBOL(lirc_get_pdata
);
747 ssize_t
lirc_dev_fop_write(struct file
*file
, const char __user
*buffer
,
748 size_t length
, loff_t
*ppos
)
750 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
753 pr_err("called with invalid irctl\n");
762 EXPORT_SYMBOL(lirc_dev_fop_write
);
765 static int __init
lirc_dev_init(void)
769 lirc_class
= class_create(THIS_MODULE
, "lirc");
770 if (IS_ERR(lirc_class
)) {
771 pr_err("class_create failed\n");
772 return PTR_ERR(lirc_class
);
775 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
778 class_destroy(lirc_class
);
779 pr_err("alloc_chrdev_region failed\n");
784 pr_info("IR Remote Control driver registered, major %d\n",
785 MAJOR(lirc_base_dev
));
792 static void __exit
lirc_dev_exit(void)
794 class_destroy(lirc_class
);
795 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
796 pr_info("module unloaded\n");
799 module_init(lirc_dev_init
);
800 module_exit(lirc_dev_exit
);
802 MODULE_DESCRIPTION("LIRC base driver module");
803 MODULE_AUTHOR("Artur Lipowski");
804 MODULE_LICENSE("GPL");
806 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
807 MODULE_PARM_DESC(debug
, "Enable debugging messages");