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
,
153 .open
= lirc_dev_fop_open
,
154 .release
= lirc_dev_fop_close
,
155 .llseek
= noop_llseek
,
158 static int lirc_cdev_add(struct irctl
*ir
)
160 struct lirc_driver
*d
= &ir
->d
;
170 cdev
->owner
= d
->owner
;
172 cdev
->ops
= &lirc_dev_fops
;
173 cdev
->owner
= THIS_MODULE
;
175 retval
= kobject_set_name(&cdev
->kobj
, "lirc%d", d
->minor
);
179 retval
= cdev_add(cdev
, MKDEV(MAJOR(lirc_base_dev
), d
->minor
), 1);
192 static int lirc_allocate_buffer(struct irctl
*ir
)
196 unsigned int chunk_size
;
197 unsigned int buffer_size
;
198 struct lirc_driver
*d
= &ir
->d
;
200 mutex_lock(&lirc_dev_lock
);
202 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
203 (d
->code_length
% 8 ? 1 : 0);
204 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
205 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
210 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
216 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
222 ir
->chunk_size
= ir
->buf
->chunk_size
;
225 mutex_unlock(&lirc_dev_lock
);
230 static int lirc_allocate_driver(struct lirc_driver
*d
)
237 pr_err("driver pointer must be not NULL!\n");
242 pr_err("dev pointer not filled in!\n");
246 if (d
->minor
>= MAX_IRCTL_DEVICES
) {
247 dev_err(d
->dev
, "minor must be between 0 and %d!\n",
248 MAX_IRCTL_DEVICES
- 1);
252 if (d
->code_length
< 1 || d
->code_length
> (BUFLEN
* 8)) {
253 dev_err(d
->dev
, "code length must be less than %d bits\n",
258 if (d
->sample_rate
) {
259 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
260 dev_err(d
->dev
, "invalid %d sample rate\n",
264 if (!d
->add_to_buf
) {
265 dev_err(d
->dev
, "add_to_buf not set\n");
268 } else if (!d
->rbuf
&& !(d
->fops
&& d
->fops
->read
&&
269 d
->fops
->poll
&& d
->fops
->unlocked_ioctl
)) {
270 dev_err(d
->dev
, "undefined read, poll, ioctl\n");
274 mutex_lock(&lirc_dev_lock
);
279 /* find first free slot for driver */
280 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
283 if (minor
== MAX_IRCTL_DEVICES
) {
284 dev_err(d
->dev
, "no free slots for drivers!\n");
288 } else if (irctls
[minor
]) {
289 dev_err(d
->dev
, "minor (%d) just registered!\n", minor
);
294 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
303 /* some safety check 8-) */
304 d
->name
[sizeof(d
->name
)-1] = '\0';
306 if (d
->features
== 0)
307 d
->features
= LIRC_CAN_REC_LIRCCODE
;
311 device_create(lirc_class
, ir
->d
.dev
,
312 MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
), NULL
,
313 "lirc%u", ir
->d
.minor
);
315 if (d
->sample_rate
) {
316 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
318 /* try to fire up polling thread */
319 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
320 if (IS_ERR(ir
->task
)) {
321 dev_err(d
->dev
, "cannot run thread for minor = %d\n",
327 /* it means - wait for external event in task queue */
328 ir
->jiffies_to_wait
= 0;
331 err
= lirc_cdev_add(ir
);
336 mutex_unlock(&lirc_dev_lock
);
338 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
339 ir
->d
.name
, ir
->d
.minor
);
343 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
345 mutex_unlock(&lirc_dev_lock
);
350 int lirc_register_driver(struct lirc_driver
*d
)
354 minor
= lirc_allocate_driver(d
);
358 if (LIRC_CAN_REC(d
->features
)) {
359 err
= lirc_allocate_buffer(irctls
[minor
]);
361 lirc_unregister_driver(minor
);
364 return err
? err
: minor
;
366 EXPORT_SYMBOL(lirc_register_driver
);
368 int lirc_unregister_driver(int minor
)
373 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
374 pr_err("minor (%d) must be between 0 and %d!\n",
375 minor
, MAX_IRCTL_DEVICES
- 1);
381 pr_err("failed to get irctl\n");
387 mutex_lock(&lirc_dev_lock
);
389 if (ir
->d
.minor
!= minor
) {
390 dev_err(ir
->d
.dev
, "lirc_dev: minor %d device not registered\n",
392 mutex_unlock(&lirc_dev_lock
);
396 /* end up polling thread */
398 kthread_stop(ir
->task
);
400 dev_dbg(ir
->d
.dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
401 ir
->d
.name
, ir
->d
.minor
);
405 dev_dbg(ir
->d
.dev
, LOGHEAD
"releasing opened driver\n",
406 ir
->d
.name
, ir
->d
.minor
);
407 wake_up_interruptible(&ir
->buf
->wait_poll
);
408 mutex_lock(&ir
->irctl_lock
);
410 if (ir
->d
.set_use_dec
)
411 ir
->d
.set_use_dec(ir
->d
.data
);
413 module_put(cdev
->owner
);
414 mutex_unlock(&ir
->irctl_lock
);
416 lirc_irctl_cleanup(ir
);
419 irctls
[minor
] = NULL
;
422 mutex_unlock(&lirc_dev_lock
);
426 EXPORT_SYMBOL(lirc_unregister_driver
);
428 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
434 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
435 pr_err("open result for %d is -ENODEV\n", iminor(inode
));
439 if (mutex_lock_interruptible(&lirc_dev_lock
))
442 ir
= irctls
[iminor(inode
)];
448 dev_dbg(ir
->d
.dev
, LOGHEAD
"open called\n", ir
->d
.name
, ir
->d
.minor
);
450 if (ir
->d
.minor
== NOPLUG
) {
461 retval
= rc_open(ir
->d
.rdev
);
467 if (try_module_get(cdev
->owner
)) {
469 if (ir
->d
.set_use_inc
)
470 retval
= ir
->d
.set_use_inc(ir
->d
.data
);
473 module_put(cdev
->owner
);
476 lirc_buffer_clear(ir
->buf
);
479 wake_up_process(ir
->task
);
483 mutex_unlock(&lirc_dev_lock
);
485 nonseekable_open(inode
, file
);
489 EXPORT_SYMBOL(lirc_dev_fop_open
);
491 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
)
493 struct irctl
*ir
= irctls
[iminor(inode
)];
498 pr_err("called with invalid irctl\n");
504 ret
= mutex_lock_killable(&lirc_dev_lock
);
507 rc_close(ir
->d
.rdev
);
511 if (ir
->d
.set_use_dec
)
512 ir
->d
.set_use_dec(ir
->d
.data
);
513 module_put(cdev
->owner
);
515 lirc_irctl_cleanup(ir
);
517 irctls
[ir
->d
.minor
] = NULL
;
522 mutex_unlock(&lirc_dev_lock
);
526 EXPORT_SYMBOL(lirc_dev_fop_close
);
528 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
530 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
534 pr_err("called with invalid irctl\n");
542 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
544 if (lirc_buffer_empty(ir
->buf
))
547 ret
= POLLIN
| POLLRDNORM
;
551 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
552 ir
->d
.name
, ir
->d
.minor
, ret
);
556 EXPORT_SYMBOL(lirc_dev_fop_poll
);
558 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
562 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
565 pr_err("no irctl found!\n");
569 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
570 ir
->d
.name
, ir
->d
.minor
, cmd
);
572 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
573 dev_err(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
574 ir
->d
.name
, ir
->d
.minor
);
578 mutex_lock(&ir
->irctl_lock
);
581 case LIRC_GET_FEATURES
:
582 result
= put_user(ir
->d
.features
, (__u32 __user
*)arg
);
584 case LIRC_GET_REC_MODE
:
585 if (LIRC_CAN_REC(ir
->d
.features
)) {
590 result
= put_user(LIRC_REC2MODE
591 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
592 (__u32 __user
*)arg
);
594 case LIRC_SET_REC_MODE
:
595 if (LIRC_CAN_REC(ir
->d
.features
)) {
600 result
= get_user(mode
, (__u32 __user
*)arg
);
601 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
604 * FIXME: We should actually set the mode somehow but
605 * for now, lirc_serial doesn't support mode changing either
608 case LIRC_GET_LENGTH
:
609 result
= put_user(ir
->d
.code_length
, (__u32 __user
*)arg
);
611 case LIRC_GET_MIN_TIMEOUT
:
612 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
613 ir
->d
.min_timeout
== 0) {
618 result
= put_user(ir
->d
.min_timeout
, (__u32 __user
*)arg
);
620 case LIRC_GET_MAX_TIMEOUT
:
621 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
622 ir
->d
.max_timeout
== 0) {
627 result
= put_user(ir
->d
.max_timeout
, (__u32 __user
*)arg
);
633 mutex_unlock(&ir
->irctl_lock
);
637 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
639 ssize_t
lirc_dev_fop_read(struct file
*file
,
644 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
646 int ret
= 0, written
= 0;
647 DECLARE_WAITQUEUE(wait
, current
);
650 pr_err("called with invalid irctl\n");
654 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
656 buf
= kzalloc(ir
->chunk_size
, GFP_KERNEL
);
660 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
669 if (length
% ir
->chunk_size
) {
675 * we add ourselves to the task queue before buffer check
676 * to avoid losing scan code (in case when queue is awaken somewhere
677 * between while condition checking and scheduling)
679 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
682 * while we didn't provide 'length' bytes, device is opened in blocking
683 * mode and 'copy_to_user' is happy, wait for data.
685 while (written
< length
&& ret
== 0) {
686 if (lirc_buffer_empty(ir
->buf
)) {
687 /* According to the read(2) man page, 'written' can be
688 * returned as less than 'length', instead of blocking
689 * again, returning -EWOULDBLOCK, or returning
694 if (file
->f_flags
& O_NONBLOCK
) {
698 if (signal_pending(current
)) {
703 mutex_unlock(&ir
->irctl_lock
);
704 set_current_state(TASK_INTERRUPTIBLE
);
706 set_current_state(TASK_RUNNING
);
708 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
710 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
719 lirc_buffer_read(ir
->buf
, buf
);
720 ret
= copy_to_user((void __user
*)buffer
+written
, buf
,
721 ir
->buf
->chunk_size
);
723 written
+= ir
->buf
->chunk_size
;
729 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
732 mutex_unlock(&ir
->irctl_lock
);
737 return ret
? ret
: written
;
739 EXPORT_SYMBOL(lirc_dev_fop_read
);
741 void *lirc_get_pdata(struct file
*file
)
743 return irctls
[iminor(file_inode(file
))]->d
.data
;
745 EXPORT_SYMBOL(lirc_get_pdata
);
748 ssize_t
lirc_dev_fop_write(struct file
*file
, const char __user
*buffer
,
749 size_t length
, loff_t
*ppos
)
751 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
754 pr_err("called with invalid irctl\n");
763 EXPORT_SYMBOL(lirc_dev_fop_write
);
766 static int __init
lirc_dev_init(void)
770 lirc_class
= class_create(THIS_MODULE
, "lirc");
771 if (IS_ERR(lirc_class
)) {
772 pr_err("class_create failed\n");
773 return PTR_ERR(lirc_class
);
776 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
779 class_destroy(lirc_class
);
780 pr_err("alloc_chrdev_region failed\n");
785 pr_info("IR Remote Control driver registered, major %d\n",
786 MAJOR(lirc_base_dev
));
793 static void __exit
lirc_dev_exit(void)
795 class_destroy(lirc_class
);
796 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
797 pr_info("module unloaded\n");
800 module_init(lirc_dev_init
);
801 module_exit(lirc_dev_exit
);
803 MODULE_DESCRIPTION("LIRC base driver module");
804 MODULE_AUTHOR("Artur Lipowski");
805 MODULE_LICENSE("GPL");
807 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
808 MODULE_PARM_DESC(debug
, "Enable debugging messages");