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/lirc.h>
39 #include <media/lirc_dev.h>
43 #define IRCTL_DEV_NAME "BaseRemoteCtl"
45 #define LOGHEAD "lirc_dev (%s[%d]): "
47 static dev_t lirc_base_dev
;
54 struct mutex irctl_lock
;
55 struct lirc_buffer
*buf
;
56 unsigned int chunk_size
;
58 struct task_struct
*task
;
62 static DEFINE_MUTEX(lirc_dev_lock
);
64 static struct irctl
*irctls
[MAX_IRCTL_DEVICES
];
65 static struct cdev cdevs
[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 dev_dbg(ir
->d
.dev
, LOGHEAD
"cleaning up\n", ir
->d
.name
, ir
->d
.minor
);
83 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
85 if (ir
->buf
!= ir
->d
.rbuf
) {
86 lirc_buffer_free(ir
->buf
);
93 * reads key codes from driver and puts them into buffer
94 * returns 0 on success
96 static int lirc_add_to_buf(struct irctl
*ir
)
98 if (ir
->d
.add_to_buf
) {
103 * service the device as long as it is returning
104 * data and we have space
107 res
= ir
->d
.add_to_buf(ir
->d
.data
, ir
->buf
);
114 kthread_stop(ir
->task
);
116 return got_data
? 0 : res
;
122 /* main function of the polling thread
124 static int lirc_thread(void *irctl
)
126 struct irctl
*ir
= irctl
;
128 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread started\n",
129 ir
->d
.name
, ir
->d
.minor
);
133 if (ir
->jiffies_to_wait
) {
134 set_current_state(TASK_INTERRUPTIBLE
);
135 schedule_timeout(ir
->jiffies_to_wait
);
137 if (kthread_should_stop())
139 if (!lirc_add_to_buf(ir
))
140 wake_up_interruptible(&ir
->buf
->wait_poll
);
142 set_current_state(TASK_INTERRUPTIBLE
);
145 } while (!kthread_should_stop());
147 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread ended\n",
148 ir
->d
.name
, ir
->d
.minor
);
154 static struct file_operations lirc_dev_fops
= {
155 .owner
= THIS_MODULE
,
156 .read
= lirc_dev_fop_read
,
157 .write
= lirc_dev_fop_write
,
158 .poll
= lirc_dev_fop_poll
,
159 .unlocked_ioctl
= lirc_dev_fop_ioctl
,
161 .compat_ioctl
= lirc_dev_fop_ioctl
,
163 .open
= lirc_dev_fop_open
,
164 .release
= lirc_dev_fop_close
,
165 .llseek
= noop_llseek
,
168 static int lirc_cdev_add(struct irctl
*ir
)
171 struct lirc_driver
*d
= &ir
->d
;
172 struct cdev
*cdev
= &cdevs
[d
->minor
];
175 cdev_init(cdev
, d
->fops
);
176 cdev
->owner
= d
->owner
;
178 cdev_init(cdev
, &lirc_dev_fops
);
179 cdev
->owner
= THIS_MODULE
;
181 retval
= kobject_set_name(&cdev
->kobj
, "lirc%d", d
->minor
);
185 retval
= cdev_add(cdev
, MKDEV(MAJOR(lirc_base_dev
), d
->minor
), 1);
187 kobject_put(&cdev
->kobj
);
192 int lirc_register_driver(struct lirc_driver
*d
)
197 unsigned int chunk_size
;
198 unsigned int buffer_size
;
202 printk(KERN_ERR
"lirc_dev: lirc_register_driver: "
203 "driver pointer must be not NULL!\n");
209 printk(KERN_ERR
"%s: dev pointer not filled in!\n", __func__
);
214 if (MAX_IRCTL_DEVICES
<= d
->minor
) {
215 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
216 "\"minor\" must be between 0 and %d (%d)!\n",
217 MAX_IRCTL_DEVICES
-1, d
->minor
);
222 if (1 > d
->code_length
|| (BUFLEN
* 8) < d
->code_length
) {
223 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
224 "code length in bits for minor (%d) "
225 "must be less than %d!\n",
226 d
->minor
, BUFLEN
* 8);
231 dev_dbg(d
->dev
, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
233 if (d
->sample_rate
) {
234 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
235 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
236 "sample_rate must be between 2 and %d!\n", HZ
);
240 if (!d
->add_to_buf
) {
241 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
242 "add_to_buf cannot be NULL when "
243 "sample_rate is set\n");
247 } else if (!(d
->fops
&& d
->fops
->read
) && !d
->rbuf
) {
248 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
249 "fops->read and rbuf cannot all be NULL!\n");
252 } else if (!d
->rbuf
) {
253 if (!(d
->fops
&& d
->fops
->read
&& d
->fops
->poll
&&
254 d
->fops
->unlocked_ioctl
)) {
255 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
256 "neither read, poll nor unlocked_ioctl can be NULL!\n");
262 mutex_lock(&lirc_dev_lock
);
267 /* find first free slot for driver */
268 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
271 if (MAX_IRCTL_DEVICES
== minor
) {
272 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
273 "no free slots for drivers!\n");
277 } else if (irctls
[minor
]) {
278 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
279 "minor (%d) just registered!\n", minor
);
284 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
293 if (d
->sample_rate
) {
294 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
296 /* it means - wait for external event in task queue */
297 ir
->jiffies_to_wait
= 0;
300 /* some safety check 8-) */
301 d
->name
[sizeof(d
->name
)-1] = '\0';
303 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
304 (d
->code_length
% 8 ? 1 : 0);
305 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
306 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
311 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
316 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
322 ir
->chunk_size
= ir
->buf
->chunk_size
;
324 if (d
->features
== 0)
325 d
->features
= LIRC_CAN_REC_LIRCCODE
;
329 device_create(lirc_class
, ir
->d
.dev
,
330 MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
), NULL
,
331 "lirc%u", ir
->d
.minor
);
333 if (d
->sample_rate
) {
334 /* try to fire up polling thread */
335 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
336 if (IS_ERR(ir
->task
)) {
337 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
338 "cannot run poll thread for minor = %d\n",
345 err
= lirc_cdev_add(ir
);
350 mutex_unlock(&lirc_dev_lock
);
352 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
353 ir
->d
.name
, ir
->d
.minor
);
357 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
359 mutex_unlock(&lirc_dev_lock
);
363 EXPORT_SYMBOL(lirc_register_driver
);
365 int lirc_unregister_driver(int minor
)
370 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
371 printk(KERN_ERR
"lirc_dev: %s: minor (%d) must be between "
372 "0 and %d!\n", __func__
, minor
, MAX_IRCTL_DEVICES
-1);
378 printk(KERN_ERR
"lirc_dev: %s: failed to get irctl struct "
379 "for minor %d!\n", __func__
, minor
);
383 cdev
= &cdevs
[minor
];
385 mutex_lock(&lirc_dev_lock
);
387 if (ir
->d
.minor
!= minor
) {
388 printk(KERN_ERR
"lirc_dev: %s: minor (%d) device not "
389 "registered!\n", __func__
, minor
);
390 mutex_unlock(&lirc_dev_lock
);
394 /* end up polling thread */
396 kthread_stop(ir
->task
);
398 dev_dbg(ir
->d
.dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
399 ir
->d
.name
, ir
->d
.minor
);
403 dev_dbg(ir
->d
.dev
, LOGHEAD
"releasing opened driver\n",
404 ir
->d
.name
, ir
->d
.minor
);
405 wake_up_interruptible(&ir
->buf
->wait_poll
);
406 mutex_lock(&ir
->irctl_lock
);
407 ir
->d
.set_use_dec(ir
->d
.data
);
408 module_put(cdev
->owner
);
409 mutex_unlock(&ir
->irctl_lock
);
411 lirc_irctl_cleanup(ir
);
414 irctls
[minor
] = NULL
;
417 mutex_unlock(&lirc_dev_lock
);
421 EXPORT_SYMBOL(lirc_unregister_driver
);
423 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
429 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
430 printk(KERN_WARNING
"lirc_dev [%d]: open result = -ENODEV\n",
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
) {
456 cdev
= &cdevs
[iminor(inode
)];
457 if (try_module_get(cdev
->owner
)) {
459 retval
= ir
->d
.set_use_inc(ir
->d
.data
);
462 module_put(cdev
->owner
);
465 lirc_buffer_clear(ir
->buf
);
468 wake_up_process(ir
->task
);
473 dev_dbg(ir
->d
.dev
, LOGHEAD
"open result = %d\n",
474 ir
->d
.name
, ir
->d
.minor
, retval
);
476 mutex_unlock(&lirc_dev_lock
);
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
)];
487 struct cdev
*cdev
= &cdevs
[iminor(inode
)];
490 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
494 dev_dbg(ir
->d
.dev
, LOGHEAD
"close called\n", ir
->d
.name
, ir
->d
.minor
);
496 WARN_ON(mutex_lock_killable(&lirc_dev_lock
));
500 ir
->d
.set_use_dec(ir
->d
.data
);
501 module_put(cdev
->owner
);
503 lirc_irctl_cleanup(ir
);
505 irctls
[ir
->d
.minor
] = NULL
;
509 mutex_unlock(&lirc_dev_lock
);
513 EXPORT_SYMBOL(lirc_dev_fop_close
);
515 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
517 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
521 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
525 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll called\n", ir
->d
.name
, ir
->d
.minor
);
530 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
533 if (lirc_buffer_empty(ir
->buf
))
536 ret
= POLLIN
| POLLRDNORM
;
540 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
541 ir
->d
.name
, ir
->d
.minor
, ret
);
545 EXPORT_SYMBOL(lirc_dev_fop_poll
);
547 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
551 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
554 printk(KERN_ERR
"lirc_dev: %s: no irctl found!\n", __func__
);
558 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
559 ir
->d
.name
, ir
->d
.minor
, cmd
);
561 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
562 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
563 ir
->d
.name
, ir
->d
.minor
);
567 mutex_lock(&ir
->irctl_lock
);
570 case LIRC_GET_FEATURES
:
571 result
= put_user(ir
->d
.features
, (__u32
*)arg
);
573 case LIRC_GET_REC_MODE
:
574 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
579 result
= put_user(LIRC_REC2MODE
580 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
583 case LIRC_SET_REC_MODE
:
584 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
589 result
= get_user(mode
, (__u32
*)arg
);
590 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
593 * FIXME: We should actually set the mode somehow but
594 * for now, lirc_serial doesn't support mode changing either
597 case LIRC_GET_LENGTH
:
598 result
= put_user(ir
->d
.code_length
, (__u32
*)arg
);
600 case LIRC_GET_MIN_TIMEOUT
:
601 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
602 ir
->d
.min_timeout
== 0) {
607 result
= put_user(ir
->d
.min_timeout
, (__u32
*)arg
);
609 case LIRC_GET_MAX_TIMEOUT
:
610 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
611 ir
->d
.max_timeout
== 0) {
616 result
= put_user(ir
->d
.max_timeout
, (__u32
*)arg
);
622 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = %d\n",
623 ir
->d
.name
, ir
->d
.minor
, result
);
625 mutex_unlock(&ir
->irctl_lock
);
629 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
631 ssize_t
lirc_dev_fop_read(struct file
*file
,
636 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
638 int ret
= 0, written
= 0;
639 DECLARE_WAITQUEUE(wait
, current
);
642 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
646 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
648 buf
= kzalloc(ir
->chunk_size
, GFP_KERNEL
);
652 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
661 if (length
% ir
->chunk_size
) {
667 * we add ourselves to the task queue before buffer check
668 * to avoid losing scan code (in case when queue is awaken somewhere
669 * between while condition checking and scheduling)
671 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
672 set_current_state(TASK_INTERRUPTIBLE
);
675 * while we didn't provide 'length' bytes, device is opened in blocking
676 * mode and 'copy_to_user' is happy, wait for data.
678 while (written
< length
&& ret
== 0) {
679 if (lirc_buffer_empty(ir
->buf
)) {
680 /* According to the read(2) man page, 'written' can be
681 * returned as less than 'length', instead of blocking
682 * again, returning -EWOULDBLOCK, or returning
686 if (file
->f_flags
& O_NONBLOCK
) {
690 if (signal_pending(current
)) {
695 mutex_unlock(&ir
->irctl_lock
);
697 set_current_state(TASK_INTERRUPTIBLE
);
699 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
701 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
702 set_current_state(TASK_RUNNING
);
711 lirc_buffer_read(ir
->buf
, buf
);
712 ret
= copy_to_user((void *)buffer
+written
, buf
,
713 ir
->buf
->chunk_size
);
715 written
+= ir
->buf
->chunk_size
;
721 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
722 set_current_state(TASK_RUNNING
);
725 mutex_unlock(&ir
->irctl_lock
);
729 dev_dbg(ir
->d
.dev
, LOGHEAD
"read result = %s (%d)\n",
730 ir
->d
.name
, ir
->d
.minor
, ret
? "<fail>" : "<ok>", ret
);
732 return ret
? ret
: written
;
734 EXPORT_SYMBOL(lirc_dev_fop_read
);
736 void *lirc_get_pdata(struct file
*file
)
740 if (file
&& file
->f_dentry
&& file
->f_dentry
->d_inode
&&
741 file
->f_dentry
->d_inode
->i_rdev
) {
743 ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
749 EXPORT_SYMBOL(lirc_get_pdata
);
752 ssize_t
lirc_dev_fop_write(struct file
*file
, const char __user
*buffer
,
753 size_t length
, loff_t
*ppos
)
755 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
758 printk(KERN_ERR
"%s: called with invalid irctl\n", __func__
);
762 dev_dbg(ir
->d
.dev
, LOGHEAD
"write called\n", ir
->d
.name
, ir
->d
.minor
);
769 EXPORT_SYMBOL(lirc_dev_fop_write
);
772 static int __init
lirc_dev_init(void)
776 lirc_class
= class_create(THIS_MODULE
, "lirc");
777 if (IS_ERR(lirc_class
)) {
778 retval
= PTR_ERR(lirc_class
);
779 printk(KERN_ERR
"lirc_dev: class_create failed\n");
783 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
786 class_destroy(lirc_class
);
787 printk(KERN_ERR
"lirc_dev: alloc_chrdev_region failed\n");
792 printk(KERN_INFO
"lirc_dev: IR Remote Control driver registered, "
793 "major %d \n", MAJOR(lirc_base_dev
));
801 static void __exit
lirc_dev_exit(void)
803 class_destroy(lirc_class
);
804 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
805 printk(KERN_INFO
"lirc_dev: module unloaded\n");
808 module_init(lirc_dev_init
);
809 module_exit(lirc_dev_exit
);
811 MODULE_DESCRIPTION("LIRC base driver module");
812 MODULE_AUTHOR("Artur Lipowski");
813 MODULE_LICENSE("GPL");
815 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
816 MODULE_PARM_DESC(debug
, "Enable debugging messages");