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/signal.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
;
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_release(struct device
*ld
)
82 struct irctl
*ir
= container_of(ld
, struct irctl
, dev
);
84 put_device(ir
->dev
.parent
);
86 if (ir
->buf
!= ir
->d
.rbuf
) {
87 lirc_buffer_free(ir
->buf
);
91 mutex_lock(&lirc_dev_lock
);
92 irctls
[ir
->d
.minor
] = NULL
;
93 mutex_unlock(&lirc_dev_lock
);
98 * reads key codes from driver and puts them into buffer
99 * returns 0 on success
101 static int lirc_add_to_buf(struct irctl
*ir
)
106 if (!ir
->d
.add_to_buf
)
110 * service the device as long as it is returning
111 * data and we have space
115 res
= ir
->d
.add_to_buf(ir
->d
.data
, ir
->buf
);
119 kthread_stop(ir
->task
);
121 return got_data
? 0 : res
;
124 /* main function of the polling thread
126 static int lirc_thread(void *irctl
)
128 struct irctl
*ir
= irctl
;
132 if (ir
->jiffies_to_wait
) {
133 set_current_state(TASK_INTERRUPTIBLE
);
134 schedule_timeout(ir
->jiffies_to_wait
);
136 if (kthread_should_stop())
138 if (!lirc_add_to_buf(ir
))
139 wake_up_interruptible(&ir
->buf
->wait_poll
);
141 set_current_state(TASK_INTERRUPTIBLE
);
144 } while (!kthread_should_stop());
150 static const struct file_operations lirc_dev_fops
= {
151 .owner
= THIS_MODULE
,
152 .read
= lirc_dev_fop_read
,
153 .write
= lirc_dev_fop_write
,
154 .poll
= lirc_dev_fop_poll
,
155 .unlocked_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 struct lirc_driver
*d
= &ir
->d
;
170 cdev_init(cdev
, d
->fops
);
171 cdev
->owner
= d
->owner
;
173 cdev_init(cdev
, &lirc_dev_fops
);
174 cdev
->owner
= THIS_MODULE
;
176 retval
= kobject_set_name(&cdev
->kobj
, "lirc%d", d
->minor
);
180 cdev
->kobj
.parent
= &ir
->dev
.kobj
;
181 return cdev_add(cdev
, ir
->dev
.devt
, 1);
184 static int lirc_allocate_buffer(struct irctl
*ir
)
188 unsigned int chunk_size
;
189 unsigned int buffer_size
;
190 struct lirc_driver
*d
= &ir
->d
;
192 mutex_lock(&lirc_dev_lock
);
194 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
195 (d
->code_length
% 8 ? 1 : 0);
196 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
197 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
202 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
208 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
214 ir
->chunk_size
= ir
->buf
->chunk_size
;
217 mutex_unlock(&lirc_dev_lock
);
222 static int lirc_allocate_driver(struct lirc_driver
*d
)
229 pr_err("driver pointer must be not NULL!\n");
234 pr_err("dev pointer not filled in!\n");
238 if (d
->minor
>= MAX_IRCTL_DEVICES
) {
239 dev_err(d
->dev
, "minor must be between 0 and %d!\n",
240 MAX_IRCTL_DEVICES
- 1);
244 if (d
->code_length
< 1 || d
->code_length
> (BUFLEN
* 8)) {
245 dev_err(d
->dev
, "code length must be less than %d bits\n",
250 if (d
->sample_rate
) {
251 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
252 dev_err(d
->dev
, "invalid %d sample rate\n",
256 if (!d
->add_to_buf
) {
257 dev_err(d
->dev
, "add_to_buf not set\n");
260 } else if (!d
->rbuf
&& !(d
->fops
&& d
->fops
->read
&&
261 d
->fops
->poll
&& d
->fops
->unlocked_ioctl
)) {
262 dev_err(d
->dev
, "undefined read, poll, ioctl\n");
266 mutex_lock(&lirc_dev_lock
);
271 /* find first free slot for driver */
272 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
275 if (minor
== MAX_IRCTL_DEVICES
) {
276 dev_err(d
->dev
, "no free slots for drivers!\n");
280 } else if (irctls
[minor
]) {
281 dev_err(d
->dev
, "minor (%d) just registered!\n", minor
);
286 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
295 /* some safety check 8-) */
296 d
->name
[sizeof(d
->name
)-1] = '\0';
298 if (d
->features
== 0)
299 d
->features
= LIRC_CAN_REC_LIRCCODE
;
303 ir
->dev
.devt
= MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
);
304 ir
->dev
.class = lirc_class
;
305 ir
->dev
.parent
= d
->dev
;
306 ir
->dev
.release
= lirc_release
;
307 dev_set_name(&ir
->dev
, "lirc%d", ir
->d
.minor
);
308 device_initialize(&ir
->dev
);
310 if (d
->sample_rate
) {
311 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
313 /* try to fire up polling thread */
314 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
315 if (IS_ERR(ir
->task
)) {
316 dev_err(d
->dev
, "cannot run thread for minor = %d\n",
322 /* it means - wait for external event in task queue */
323 ir
->jiffies_to_wait
= 0;
326 err
= lirc_cdev_add(ir
);
332 err
= device_add(&ir
->dev
);
336 mutex_unlock(&lirc_dev_lock
);
338 get_device(ir
->dev
.parent
);
340 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
341 ir
->d
.name
, ir
->d
.minor
);
346 put_device(&ir
->dev
);
348 mutex_unlock(&lirc_dev_lock
);
353 int lirc_register_driver(struct lirc_driver
*d
)
357 minor
= lirc_allocate_driver(d
);
361 if (LIRC_CAN_REC(d
->features
)) {
362 err
= lirc_allocate_buffer(irctls
[minor
]);
364 lirc_unregister_driver(minor
);
367 return err
? err
: minor
;
369 EXPORT_SYMBOL(lirc_register_driver
);
371 int lirc_unregister_driver(int minor
)
375 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
376 pr_err("minor (%d) must be between 0 and %d!\n",
377 minor
, MAX_IRCTL_DEVICES
- 1);
383 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
);
410 mutex_lock(&ir
->irctl_lock
);
412 if (ir
->d
.set_use_dec
)
413 ir
->d
.set_use_dec(ir
->d
.data
);
415 mutex_unlock(&ir
->irctl_lock
);
416 mutex_unlock(&lirc_dev_lock
);
418 device_del(&ir
->dev
);
420 put_device(&ir
->dev
);
424 EXPORT_SYMBOL(lirc_unregister_driver
);
426 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
431 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
432 pr_err("open result for %d is -ENODEV\n", iminor(inode
));
436 if (mutex_lock_interruptible(&lirc_dev_lock
))
439 ir
= irctls
[iminor(inode
)];
440 mutex_unlock(&lirc_dev_lock
);
447 dev_dbg(ir
->d
.dev
, LOGHEAD
"open called\n", ir
->d
.name
, ir
->d
.minor
);
449 if (ir
->d
.minor
== NOPLUG
) {
460 retval
= rc_open(ir
->d
.rdev
);
466 if (ir
->d
.set_use_inc
)
467 retval
= ir
->d
.set_use_inc(ir
->d
.data
);
472 lirc_buffer_clear(ir
->buf
);
474 wake_up_process(ir
->task
);
478 nonseekable_open(inode
, file
);
482 EXPORT_SYMBOL(lirc_dev_fop_open
);
484 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
)
486 struct irctl
*ir
= irctls
[iminor(inode
)];
490 pr_err("called with invalid irctl\n");
494 ret
= mutex_lock_killable(&lirc_dev_lock
);
497 rc_close(ir
->d
.rdev
);
500 if (ir
->d
.set_use_dec
)
501 ir
->d
.set_use_dec(ir
->d
.data
);
503 mutex_unlock(&lirc_dev_lock
);
507 EXPORT_SYMBOL(lirc_dev_fop_close
);
509 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
511 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
515 pr_err("called with invalid irctl\n");
523 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
525 if (lirc_buffer_empty(ir
->buf
))
528 ret
= POLLIN
| POLLRDNORM
;
532 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
533 ir
->d
.name
, ir
->d
.minor
, ret
);
537 EXPORT_SYMBOL(lirc_dev_fop_poll
);
539 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
543 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
546 pr_err("no irctl found!\n");
550 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
551 ir
->d
.name
, ir
->d
.minor
, cmd
);
553 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
554 dev_err(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
555 ir
->d
.name
, ir
->d
.minor
);
559 mutex_lock(&ir
->irctl_lock
);
562 case LIRC_GET_FEATURES
:
563 result
= put_user(ir
->d
.features
, (__u32 __user
*)arg
);
565 case LIRC_GET_REC_MODE
:
566 if (!LIRC_CAN_REC(ir
->d
.features
)) {
571 result
= put_user(LIRC_REC2MODE
572 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
573 (__u32 __user
*)arg
);
575 case LIRC_SET_REC_MODE
:
576 if (!LIRC_CAN_REC(ir
->d
.features
)) {
581 result
= get_user(mode
, (__u32 __user
*)arg
);
582 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
585 * FIXME: We should actually set the mode somehow but
586 * for now, lirc_serial doesn't support mode changing either
589 case LIRC_GET_LENGTH
:
590 result
= put_user(ir
->d
.code_length
, (__u32 __user
*)arg
);
592 case LIRC_GET_MIN_TIMEOUT
:
593 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
594 ir
->d
.min_timeout
== 0) {
599 result
= put_user(ir
->d
.min_timeout
, (__u32 __user
*)arg
);
601 case LIRC_GET_MAX_TIMEOUT
:
602 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
603 ir
->d
.max_timeout
== 0) {
608 result
= put_user(ir
->d
.max_timeout
, (__u32 __user
*)arg
);
614 mutex_unlock(&ir
->irctl_lock
);
618 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
620 ssize_t
lirc_dev_fop_read(struct file
*file
,
625 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
627 int ret
= 0, written
= 0;
628 DECLARE_WAITQUEUE(wait
, current
);
631 pr_err("called with invalid irctl\n");
635 if (!LIRC_CAN_REC(ir
->d
.features
))
638 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
640 buf
= kzalloc(ir
->chunk_size
, GFP_KERNEL
);
644 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
653 if (length
% ir
->chunk_size
) {
659 * we add ourselves to the task queue before buffer check
660 * to avoid losing scan code (in case when queue is awaken somewhere
661 * between while condition checking and scheduling)
663 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
666 * while we didn't provide 'length' bytes, device is opened in blocking
667 * mode and 'copy_to_user' is happy, wait for data.
669 while (written
< length
&& ret
== 0) {
670 if (lirc_buffer_empty(ir
->buf
)) {
671 /* According to the read(2) man page, 'written' can be
672 * returned as less than 'length', instead of blocking
673 * again, returning -EWOULDBLOCK, or returning
678 if (file
->f_flags
& O_NONBLOCK
) {
682 if (signal_pending(current
)) {
687 mutex_unlock(&ir
->irctl_lock
);
688 set_current_state(TASK_INTERRUPTIBLE
);
690 set_current_state(TASK_RUNNING
);
692 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
694 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
703 lirc_buffer_read(ir
->buf
, buf
);
704 ret
= copy_to_user((void __user
*)buffer
+written
, buf
,
705 ir
->buf
->chunk_size
);
707 written
+= ir
->buf
->chunk_size
;
713 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
716 mutex_unlock(&ir
->irctl_lock
);
721 return ret
? ret
: written
;
723 EXPORT_SYMBOL(lirc_dev_fop_read
);
725 void *lirc_get_pdata(struct file
*file
)
727 return irctls
[iminor(file_inode(file
))]->d
.data
;
729 EXPORT_SYMBOL(lirc_get_pdata
);
732 ssize_t
lirc_dev_fop_write(struct file
*file
, const char __user
*buffer
,
733 size_t length
, loff_t
*ppos
)
735 struct irctl
*ir
= irctls
[iminor(file_inode(file
))];
738 pr_err("called with invalid irctl\n");
747 EXPORT_SYMBOL(lirc_dev_fop_write
);
750 static int __init
lirc_dev_init(void)
754 lirc_class
= class_create(THIS_MODULE
, "lirc");
755 if (IS_ERR(lirc_class
)) {
756 pr_err("class_create failed\n");
757 return PTR_ERR(lirc_class
);
760 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
763 class_destroy(lirc_class
);
764 pr_err("alloc_chrdev_region failed\n");
768 pr_info("IR Remote Control driver registered, major %d\n",
769 MAJOR(lirc_base_dev
));
774 static void __exit
lirc_dev_exit(void)
776 class_destroy(lirc_class
);
777 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
778 pr_info("module unloaded\n");
781 module_init(lirc_dev_init
);
782 module_exit(lirc_dev_exit
);
784 MODULE_DESCRIPTION("LIRC base driver module");
785 MODULE_AUTHOR("Artur Lipowski");
786 MODULE_LICENSE("GPL");
788 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
789 MODULE_PARM_DESC(debug
, "Enable debugging messages");