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/errno.h>
31 #include <linux/mutex.h>
32 #include <linux/wait.h>
33 #include <linux/unistd.h>
34 #include <linux/kthread.h>
35 #include <linux/bitops.h>
36 #include <linux/device.h>
37 #include <linux/cdev.h>
39 #include <media/lirc.h>
40 #include <media/lirc_dev.h>
44 #define IRCTL_DEV_NAME "BaseRemoteCtl"
46 #define LOGHEAD "lirc_dev (%s[%d]): "
48 static dev_t lirc_base_dev
;
55 struct mutex irctl_lock
;
56 struct lirc_buffer
*buf
;
57 unsigned int chunk_size
;
59 struct task_struct
*task
;
65 static DEFINE_MUTEX(lirc_dev_lock
);
67 static struct irctl
*irctls
[MAX_IRCTL_DEVICES
];
69 /* Only used for sysfs but defined to void otherwise */
70 static struct class *lirc_class
;
73 * initializes the irctl structure
75 static void init_irctl(struct irctl
*ir
)
77 dev_dbg(ir
->d
.dev
, LOGHEAD
"initializing irctl\n",
78 ir
->d
.name
, ir
->d
.minor
);
79 mutex_init(&ir
->irctl_lock
);
83 static void cleanup(struct irctl
*ir
)
85 dev_dbg(ir
->d
.dev
, LOGHEAD
"cleaning up\n", ir
->d
.name
, ir
->d
.minor
);
87 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
89 if (ir
->buf
!= ir
->d
.rbuf
) {
90 lirc_buffer_free(ir
->buf
);
97 * reads key codes from driver and puts them into buffer
98 * returns 0 on success
100 static int add_to_buf(struct irctl
*ir
)
102 if (ir
->d
.add_to_buf
) {
107 * service the device as long as it is returning
108 * data and we have space
111 res
= ir
->d
.add_to_buf(ir
->d
.data
, ir
->buf
);
118 kthread_stop(ir
->task
);
120 return got_data
? 0 : res
;
126 /* main function of the polling thread
128 static int lirc_thread(void *irctl
)
130 struct irctl
*ir
= irctl
;
132 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread started\n",
133 ir
->d
.name
, ir
->d
.minor
);
137 if (ir
->jiffies_to_wait
) {
138 set_current_state(TASK_INTERRUPTIBLE
);
139 schedule_timeout(ir
->jiffies_to_wait
);
141 if (kthread_should_stop())
144 wake_up_interruptible(&ir
->buf
->wait_poll
);
146 set_current_state(TASK_INTERRUPTIBLE
);
149 } while (!kthread_should_stop());
151 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll thread ended\n",
152 ir
->d
.name
, ir
->d
.minor
);
158 static struct file_operations fops
= {
159 .owner
= THIS_MODULE
,
160 .read
= lirc_dev_fop_read
,
161 .write
= lirc_dev_fop_write
,
162 .poll
= lirc_dev_fop_poll
,
163 .unlocked_ioctl
= lirc_dev_fop_ioctl
,
164 .open
= lirc_dev_fop_open
,
165 .release
= lirc_dev_fop_close
,
168 static int lirc_cdev_add(struct irctl
*ir
)
171 struct lirc_driver
*d
= &ir
->d
;
174 cdev_init(&ir
->cdev
, d
->fops
);
175 ir
->cdev
.owner
= d
->owner
;
177 cdev_init(&ir
->cdev
, &fops
);
178 ir
->cdev
.owner
= THIS_MODULE
;
180 kobject_set_name(&ir
->cdev
.kobj
, "lirc%d", d
->minor
);
182 retval
= cdev_add(&ir
->cdev
, MKDEV(MAJOR(lirc_base_dev
), d
->minor
), 1);
184 kobject_put(&ir
->cdev
.kobj
);
189 int lirc_register_driver(struct lirc_driver
*d
)
194 unsigned int chunk_size
;
195 unsigned int buffer_size
;
199 printk(KERN_ERR
"lirc_dev: lirc_register_driver: "
200 "driver pointer must be not NULL!\n");
205 if (MAX_IRCTL_DEVICES
<= d
->minor
) {
206 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
207 "\"minor\" must be between 0 and %d (%d)!\n",
208 MAX_IRCTL_DEVICES
-1, d
->minor
);
213 if (1 > d
->code_length
|| (BUFLEN
* 8) < d
->code_length
) {
214 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
215 "code length in bits for minor (%d) "
216 "must be less than %d!\n",
217 d
->minor
, BUFLEN
* 8);
222 dev_dbg(d
->dev
, "lirc_dev: lirc_register_driver: sample_rate: %d\n",
224 if (d
->sample_rate
) {
225 if (2 > d
->sample_rate
|| HZ
< d
->sample_rate
) {
226 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
227 "sample_rate must be between 2 and %d!\n", HZ
);
231 if (!d
->add_to_buf
) {
232 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
233 "add_to_buf cannot be NULL when "
234 "sample_rate is set\n");
238 } else if (!(d
->fops
&& d
->fops
->read
) && !d
->rbuf
) {
239 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
240 "fops->read and rbuf cannot all be NULL!\n");
243 } else if (!d
->rbuf
) {
244 if (!(d
->fops
&& d
->fops
->read
&& d
->fops
->poll
&&
245 d
->fops
->unlocked_ioctl
)) {
246 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
247 "neither read, poll nor unlocked_ioctl can be NULL!\n");
253 mutex_lock(&lirc_dev_lock
);
258 /* find first free slot for driver */
259 for (minor
= 0; minor
< MAX_IRCTL_DEVICES
; minor
++)
262 if (MAX_IRCTL_DEVICES
== minor
) {
263 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
264 "no free slots for drivers!\n");
268 } else if (irctls
[minor
]) {
269 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
270 "minor (%d) just registered!\n", minor
);
275 ir
= kzalloc(sizeof(struct irctl
), GFP_KERNEL
);
284 if (d
->sample_rate
) {
285 ir
->jiffies_to_wait
= HZ
/ d
->sample_rate
;
287 /* it means - wait for external event in task queue */
288 ir
->jiffies_to_wait
= 0;
291 /* some safety check 8-) */
292 d
->name
[sizeof(d
->name
)-1] = '\0';
294 bytes_in_key
= BITS_TO_LONGS(d
->code_length
) +
295 (d
->code_length
% 8 ? 1 : 0);
296 buffer_size
= d
->buffer_size
? d
->buffer_size
: BUFLEN
/ bytes_in_key
;
297 chunk_size
= d
->chunk_size
? d
->chunk_size
: bytes_in_key
;
302 ir
->buf
= kmalloc(sizeof(struct lirc_buffer
), GFP_KERNEL
);
307 err
= lirc_buffer_init(ir
->buf
, chunk_size
, buffer_size
);
313 ir
->chunk_size
= ir
->buf
->chunk_size
;
315 if (d
->features
== 0)
316 d
->features
= LIRC_CAN_REC_LIRCCODE
;
321 device_create(lirc_class
, ir
->d
.dev
,
322 MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
), NULL
,
323 "lirc%u", ir
->d
.minor
);
325 if (d
->sample_rate
) {
326 /* try to fire up polling thread */
327 ir
->task
= kthread_run(lirc_thread
, (void *)ir
, "lirc_dev");
328 if (IS_ERR(ir
->task
)) {
329 dev_err(d
->dev
, "lirc_dev: lirc_register_driver: "
330 "cannot run poll thread for minor = %d\n",
337 err
= lirc_cdev_add(ir
);
342 mutex_unlock(&lirc_dev_lock
);
344 dev_info(ir
->d
.dev
, "lirc_dev: driver %s registered at minor = %d\n",
345 ir
->d
.name
, ir
->d
.minor
);
349 device_destroy(lirc_class
, MKDEV(MAJOR(lirc_base_dev
), ir
->d
.minor
));
351 mutex_unlock(&lirc_dev_lock
);
355 EXPORT_SYMBOL(lirc_register_driver
);
357 int lirc_unregister_driver(int minor
)
361 if (minor
< 0 || minor
>= MAX_IRCTL_DEVICES
) {
362 printk(KERN_ERR
"lirc_dev: lirc_unregister_driver: "
363 "\"minor (%d)\" must be between 0 and %d!\n",
364 minor
, MAX_IRCTL_DEVICES
-1);
370 mutex_lock(&lirc_dev_lock
);
372 if (ir
->d
.minor
!= minor
) {
373 printk(KERN_ERR
"lirc_dev: lirc_unregister_driver: "
374 "minor (%d) device not registered!", minor
);
375 mutex_unlock(&lirc_dev_lock
);
379 /* end up polling thread */
381 kthread_stop(ir
->task
);
383 dev_dbg(ir
->d
.dev
, "lirc_dev: driver %s unregistered from minor = %d\n",
384 ir
->d
.name
, ir
->d
.minor
);
388 dev_dbg(ir
->d
.dev
, LOGHEAD
"releasing opened driver\n",
389 ir
->d
.name
, ir
->d
.minor
);
390 wake_up_interruptible(&ir
->buf
->wait_poll
);
391 mutex_lock(&ir
->irctl_lock
);
392 ir
->d
.set_use_dec(ir
->d
.data
);
393 module_put(ir
->d
.owner
);
394 mutex_unlock(&ir
->irctl_lock
);
400 irctls
[minor
] = NULL
;
403 mutex_unlock(&lirc_dev_lock
);
407 EXPORT_SYMBOL(lirc_unregister_driver
);
409 int lirc_dev_fop_open(struct inode
*inode
, struct file
*file
)
414 if (iminor(inode
) >= MAX_IRCTL_DEVICES
) {
415 printk(KERN_WARNING
"lirc_dev [%d]: open result = -ENODEV\n",
420 if (mutex_lock_interruptible(&lirc_dev_lock
))
423 ir
= irctls
[iminor(inode
)];
428 file
->private_data
= ir
;
430 dev_dbg(ir
->d
.dev
, LOGHEAD
"open called\n", ir
->d
.name
, ir
->d
.minor
);
432 if (ir
->d
.minor
== NOPLUG
) {
442 if (try_module_get(ir
->d
.owner
)) {
444 retval
= ir
->d
.set_use_inc(ir
->d
.data
);
447 module_put(ir
->d
.owner
);
450 lirc_buffer_clear(ir
->buf
);
453 wake_up_process(ir
->task
);
458 dev_dbg(ir
->d
.dev
, LOGHEAD
"open result = %d\n",
459 ir
->d
.name
, ir
->d
.minor
, retval
);
461 mutex_unlock(&lirc_dev_lock
);
465 EXPORT_SYMBOL(lirc_dev_fop_open
);
467 int lirc_dev_fop_close(struct inode
*inode
, struct file
*file
)
469 struct irctl
*ir
= irctls
[iminor(inode
)];
471 dev_dbg(ir
->d
.dev
, LOGHEAD
"close called\n", ir
->d
.name
, ir
->d
.minor
);
473 WARN_ON(mutex_lock_killable(&lirc_dev_lock
));
477 ir
->d
.set_use_dec(ir
->d
.data
);
478 module_put(ir
->d
.owner
);
481 irctls
[ir
->d
.minor
] = NULL
;
485 mutex_unlock(&lirc_dev_lock
);
489 EXPORT_SYMBOL(lirc_dev_fop_close
);
491 unsigned int lirc_dev_fop_poll(struct file
*file
, poll_table
*wait
)
493 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
496 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll called\n", ir
->d
.name
, ir
->d
.minor
);
499 mutex_unlock(&ir
->irctl_lock
);
503 poll_wait(file
, &ir
->buf
->wait_poll
, wait
);
506 if (lirc_buffer_empty(ir
->buf
))
509 ret
= POLLIN
| POLLRDNORM
;
513 dev_dbg(ir
->d
.dev
, LOGHEAD
"poll result = %d\n",
514 ir
->d
.name
, ir
->d
.minor
, ret
);
518 EXPORT_SYMBOL(lirc_dev_fop_poll
);
520 long lirc_dev_fop_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
524 struct irctl
*ir
= file
->private_data
;
526 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl called (0x%x)\n",
527 ir
->d
.name
, ir
->d
.minor
, cmd
);
529 if (ir
->d
.minor
== NOPLUG
|| !ir
->attached
) {
530 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = -ENODEV\n",
531 ir
->d
.name
, ir
->d
.minor
);
535 mutex_lock(&ir
->irctl_lock
);
538 case LIRC_GET_FEATURES
:
539 result
= put_user(ir
->d
.features
, (unsigned long *)arg
);
541 case LIRC_GET_REC_MODE
:
542 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
547 result
= put_user(LIRC_REC2MODE
548 (ir
->d
.features
& LIRC_CAN_REC_MASK
),
549 (unsigned long *)arg
);
551 case LIRC_SET_REC_MODE
:
552 if (!(ir
->d
.features
& LIRC_CAN_REC_MASK
)) {
557 result
= get_user(mode
, (unsigned long *)arg
);
558 if (!result
&& !(LIRC_MODE2REC(mode
) & ir
->d
.features
))
561 case LIRC_GET_LENGTH
:
562 result
= put_user(ir
->d
.code_length
, (unsigned long *)arg
);
564 case LIRC_GET_MIN_TIMEOUT
:
565 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
566 ir
->d
.min_timeout
== 0) {
571 result
= put_user(ir
->d
.min_timeout
, (unsigned long *)arg
);
573 case LIRC_GET_MAX_TIMEOUT
:
574 if (!(ir
->d
.features
& LIRC_CAN_SET_REC_TIMEOUT
) ||
575 ir
->d
.max_timeout
== 0) {
580 result
= put_user(ir
->d
.max_timeout
, (unsigned long *)arg
);
586 dev_dbg(ir
->d
.dev
, LOGHEAD
"ioctl result = %d\n",
587 ir
->d
.name
, ir
->d
.minor
, result
);
589 mutex_unlock(&ir
->irctl_lock
);
593 EXPORT_SYMBOL(lirc_dev_fop_ioctl
);
595 ssize_t
lirc_dev_fop_read(struct file
*file
,
600 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
601 unsigned char buf
[ir
->chunk_size
];
602 int ret
= 0, written
= 0;
603 DECLARE_WAITQUEUE(wait
, current
);
605 dev_dbg(ir
->d
.dev
, LOGHEAD
"read called\n", ir
->d
.name
, ir
->d
.minor
);
607 if (mutex_lock_interruptible(&ir
->irctl_lock
))
610 mutex_unlock(&ir
->irctl_lock
);
614 if (length
% ir
->chunk_size
) {
615 dev_dbg(ir
->d
.dev
, LOGHEAD
"read result = -EINVAL\n",
616 ir
->d
.name
, ir
->d
.minor
);
617 mutex_unlock(&ir
->irctl_lock
);
622 * we add ourselves to the task queue before buffer check
623 * to avoid losing scan code (in case when queue is awaken somewhere
624 * between while condition checking and scheduling)
626 add_wait_queue(&ir
->buf
->wait_poll
, &wait
);
627 set_current_state(TASK_INTERRUPTIBLE
);
630 * while we didn't provide 'length' bytes, device is opened in blocking
631 * mode and 'copy_to_user' is happy, wait for data.
633 while (written
< length
&& ret
== 0) {
634 if (lirc_buffer_empty(ir
->buf
)) {
635 /* According to the read(2) man page, 'written' can be
636 * returned as less than 'length', instead of blocking
637 * again, returning -EWOULDBLOCK, or returning
641 if (file
->f_flags
& O_NONBLOCK
) {
645 if (signal_pending(current
)) {
650 mutex_unlock(&ir
->irctl_lock
);
652 set_current_state(TASK_INTERRUPTIBLE
);
654 if (mutex_lock_interruptible(&ir
->irctl_lock
)) {
656 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
657 set_current_state(TASK_RUNNING
);
666 lirc_buffer_read(ir
->buf
, buf
);
667 ret
= copy_to_user((void *)buffer
+written
, buf
,
668 ir
->buf
->chunk_size
);
669 written
+= ir
->buf
->chunk_size
;
673 remove_wait_queue(&ir
->buf
->wait_poll
, &wait
);
674 set_current_state(TASK_RUNNING
);
675 mutex_unlock(&ir
->irctl_lock
);
678 dev_dbg(ir
->d
.dev
, LOGHEAD
"read result = %s (%d)\n",
679 ir
->d
.name
, ir
->d
.minor
, ret
? "-EFAULT" : "OK", ret
);
681 return ret
? ret
: written
;
683 EXPORT_SYMBOL(lirc_dev_fop_read
);
685 void *lirc_get_pdata(struct file
*file
)
689 if (file
&& file
->f_dentry
&& file
->f_dentry
->d_inode
&&
690 file
->f_dentry
->d_inode
->i_rdev
) {
692 ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
698 EXPORT_SYMBOL(lirc_get_pdata
);
701 ssize_t
lirc_dev_fop_write(struct file
*file
, const char *buffer
,
702 size_t length
, loff_t
*ppos
)
704 struct irctl
*ir
= irctls
[iminor(file
->f_dentry
->d_inode
)];
706 dev_dbg(ir
->d
.dev
, LOGHEAD
"write called\n", ir
->d
.name
, ir
->d
.minor
);
713 EXPORT_SYMBOL(lirc_dev_fop_write
);
716 static int __init
lirc_dev_init(void)
720 lirc_class
= class_create(THIS_MODULE
, "lirc");
721 if (IS_ERR(lirc_class
)) {
722 retval
= PTR_ERR(lirc_class
);
723 printk(KERN_ERR
"lirc_dev: class_create failed\n");
727 retval
= alloc_chrdev_region(&lirc_base_dev
, 0, MAX_IRCTL_DEVICES
,
730 class_destroy(lirc_class
);
731 printk(KERN_ERR
"lirc_dev: alloc_chrdev_region failed\n");
736 printk(KERN_INFO
"lirc_dev: IR Remote Control driver registered, "
737 "major %d \n", MAJOR(lirc_base_dev
));
745 static void __exit
lirc_dev_exit(void)
747 class_destroy(lirc_class
);
748 unregister_chrdev_region(lirc_base_dev
, MAX_IRCTL_DEVICES
);
749 printk(KERN_INFO
"lirc_dev: module unloaded\n");
752 module_init(lirc_dev_init
);
753 module_exit(lirc_dev_exit
);
755 MODULE_DESCRIPTION("LIRC base driver module");
756 MODULE_AUTHOR("Artur Lipowski");
757 MODULE_LICENSE("GPL");
759 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
760 MODULE_PARM_DESC(debug
, "Enable debugging messages");