Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / media / rc / lirc_dev.c
blobf4509ef9922b7908461615e2e397db67bab2ef94
1 /*
2 * LIRC base driver
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>
29 #include <linux/fs.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>
44 static bool debug;
46 #define IRCTL_DEV_NAME "BaseRemoteCtl"
47 #define NOPLUG -1
48 #define LOGHEAD "lirc_dev (%s[%d]): "
50 static dev_t lirc_base_dev;
52 struct irctl {
53 struct lirc_driver d;
54 int attached;
55 int open;
57 struct mutex irctl_lock;
58 struct lirc_buffer *buf;
59 unsigned int chunk_size;
61 struct cdev *cdev;
63 struct task_struct *task;
64 long jiffies_to_wait;
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;
74 /* helper function
75 * initializes the irctl structure
77 static void lirc_irctl_init(struct irctl *ir)
79 mutex_init(&ir->irctl_lock);
80 ir->d.minor = NOPLUG;
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);
89 kfree(ir->buf);
91 ir->buf = NULL;
94 /* helper function
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)
100 int res;
101 int got_data = -1;
103 if (!ir->d.add_to_buf)
104 return 0;
107 * service the device as long as it is returning
108 * data and we have space
110 do {
111 got_data++;
112 res = ir->d.add_to_buf(ir->d.data, ir->buf);
113 } while (!res);
115 if (res == -ENODEV)
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;
127 do {
128 if (ir->open) {
129 if (ir->jiffies_to_wait) {
130 set_current_state(TASK_INTERRUPTIBLE);
131 schedule_timeout(ir->jiffies_to_wait);
133 if (kthread_should_stop())
134 break;
135 if (!lirc_add_to_buf(ir))
136 wake_up_interruptible(&ir->buf->wait_poll);
137 } else {
138 set_current_state(TASK_INTERRUPTIBLE);
139 schedule();
141 } while (!kthread_should_stop());
143 return 0;
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 #ifdef CONFIG_COMPAT
154 .compat_ioctl = lirc_dev_fop_ioctl,
155 #endif
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 int retval = -ENOMEM;
164 struct lirc_driver *d = &ir->d;
165 struct cdev *cdev;
167 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
168 if (!cdev)
169 goto err_out;
171 if (d->fops) {
172 cdev_init(cdev, d->fops);
173 cdev->owner = d->owner;
174 } else {
175 cdev_init(cdev, &lirc_dev_fops);
176 cdev->owner = THIS_MODULE;
178 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
179 if (retval)
180 goto err_out;
182 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
183 if (retval) {
184 kobject_put(&cdev->kobj);
185 goto err_out;
188 ir->cdev = cdev;
190 return 0;
192 err_out:
193 kfree(cdev);
194 return retval;
197 static int lirc_allocate_buffer(struct irctl *ir)
199 int err = 0;
200 int bytes_in_key;
201 unsigned int chunk_size;
202 unsigned int buffer_size;
203 struct lirc_driver *d = &ir->d;
205 mutex_lock(&lirc_dev_lock);
207 bytes_in_key = BITS_TO_LONGS(d->code_length) +
208 (d->code_length % 8 ? 1 : 0);
209 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key;
210 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key;
212 if (d->rbuf) {
213 ir->buf = d->rbuf;
214 } else {
215 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
216 if (!ir->buf) {
217 err = -ENOMEM;
218 goto out;
221 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
222 if (err) {
223 kfree(ir->buf);
224 goto out;
227 ir->chunk_size = ir->buf->chunk_size;
229 out:
230 mutex_unlock(&lirc_dev_lock);
232 return err;
235 static int lirc_allocate_driver(struct lirc_driver *d)
237 struct irctl *ir;
238 int minor;
239 int err;
241 if (!d) {
242 pr_err("driver pointer must be not NULL!\n");
243 return -EBADRQC;
246 if (!d->dev) {
247 pr_err("dev pointer not filled in!\n");
248 return -EINVAL;
251 if (d->minor >= MAX_IRCTL_DEVICES) {
252 dev_err(d->dev, "minor must be between 0 and %d!\n",
253 MAX_IRCTL_DEVICES - 1);
254 return -EBADRQC;
257 if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) {
258 dev_err(d->dev, "code length must be less than %d bits\n",
259 BUFLEN * 8);
260 return -EBADRQC;
263 if (d->sample_rate) {
264 if (2 > d->sample_rate || HZ < d->sample_rate) {
265 dev_err(d->dev, "invalid %d sample rate\n",
266 d->sample_rate);
267 return -EBADRQC;
269 if (!d->add_to_buf) {
270 dev_err(d->dev, "add_to_buf not set\n");
271 return -EBADRQC;
273 } else if (!d->rbuf && !(d->fops && d->fops->read &&
274 d->fops->poll && d->fops->unlocked_ioctl)) {
275 dev_err(d->dev, "undefined read, poll, ioctl\n");
276 return -EBADRQC;
279 mutex_lock(&lirc_dev_lock);
281 minor = d->minor;
283 if (minor < 0) {
284 /* find first free slot for driver */
285 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
286 if (!irctls[minor])
287 break;
288 if (minor == MAX_IRCTL_DEVICES) {
289 dev_err(d->dev, "no free slots for drivers!\n");
290 err = -ENOMEM;
291 goto out_lock;
293 } else if (irctls[minor]) {
294 dev_err(d->dev, "minor (%d) just registered!\n", minor);
295 err = -EBUSY;
296 goto out_lock;
299 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
300 if (!ir) {
301 err = -ENOMEM;
302 goto out_lock;
304 lirc_irctl_init(ir);
305 irctls[minor] = ir;
306 d->minor = minor;
308 /* some safety check 8-) */
309 d->name[sizeof(d->name)-1] = '\0';
311 if (d->features == 0)
312 d->features = LIRC_CAN_REC_LIRCCODE;
314 ir->d = *d;
316 device_create(lirc_class, ir->d.dev,
317 MKDEV(MAJOR(lirc_base_dev), ir->d.minor), NULL,
318 "lirc%u", ir->d.minor);
320 if (d->sample_rate) {
321 ir->jiffies_to_wait = HZ / d->sample_rate;
323 /* try to fire up polling thread */
324 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev");
325 if (IS_ERR(ir->task)) {
326 dev_err(d->dev, "cannot run thread for minor = %d\n",
327 d->minor);
328 err = -ECHILD;
329 goto out_sysfs;
331 } else {
332 /* it means - wait for external event in task queue */
333 ir->jiffies_to_wait = 0;
336 err = lirc_cdev_add(ir);
337 if (err)
338 goto out_sysfs;
340 ir->attached = 1;
341 mutex_unlock(&lirc_dev_lock);
343 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n",
344 ir->d.name, ir->d.minor);
345 return minor;
347 out_sysfs:
348 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
349 out_lock:
350 mutex_unlock(&lirc_dev_lock);
352 return err;
355 int lirc_register_driver(struct lirc_driver *d)
357 int minor, err = 0;
359 minor = lirc_allocate_driver(d);
360 if (minor < 0)
361 return minor;
363 if (LIRC_CAN_REC(d->features)) {
364 err = lirc_allocate_buffer(irctls[minor]);
365 if (err)
366 lirc_unregister_driver(minor);
369 return err ? err : minor;
371 EXPORT_SYMBOL(lirc_register_driver);
373 int lirc_unregister_driver(int minor)
375 struct irctl *ir;
376 struct cdev *cdev;
378 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) {
379 pr_err("minor (%d) must be between 0 and %d!\n",
380 minor, MAX_IRCTL_DEVICES - 1);
381 return -EBADRQC;
384 ir = irctls[minor];
385 if (!ir) {
386 pr_err("failed to get irctl\n");
387 return -ENOENT;
390 cdev = ir->cdev;
392 mutex_lock(&lirc_dev_lock);
394 if (ir->d.minor != minor) {
395 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n",
396 minor);
397 mutex_unlock(&lirc_dev_lock);
398 return -ENOENT;
401 /* end up polling thread */
402 if (ir->task)
403 kthread_stop(ir->task);
405 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n",
406 ir->d.name, ir->d.minor);
408 ir->attached = 0;
409 if (ir->open) {
410 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n",
411 ir->d.name, ir->d.minor);
412 wake_up_interruptible(&ir->buf->wait_poll);
413 mutex_lock(&ir->irctl_lock);
415 if (ir->d.set_use_dec)
416 ir->d.set_use_dec(ir->d.data);
418 module_put(cdev->owner);
419 mutex_unlock(&ir->irctl_lock);
420 } else {
421 lirc_irctl_cleanup(ir);
422 cdev_del(cdev);
423 kfree(cdev);
424 kfree(ir);
425 irctls[minor] = NULL;
428 mutex_unlock(&lirc_dev_lock);
430 return 0;
432 EXPORT_SYMBOL(lirc_unregister_driver);
434 int lirc_dev_fop_open(struct inode *inode, struct file *file)
436 struct irctl *ir;
437 struct cdev *cdev;
438 int retval = 0;
440 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
441 pr_err("open result for %d is -ENODEV\n", iminor(inode));
442 return -ENODEV;
445 if (mutex_lock_interruptible(&lirc_dev_lock))
446 return -ERESTARTSYS;
448 ir = irctls[iminor(inode)];
449 mutex_unlock(&lirc_dev_lock);
451 if (!ir) {
452 retval = -ENODEV;
453 goto error;
456 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
458 if (ir->d.minor == NOPLUG) {
459 retval = -ENODEV;
460 goto error;
463 if (ir->open) {
464 retval = -EBUSY;
465 goto error;
468 if (ir->d.rdev) {
469 retval = rc_open(ir->d.rdev);
470 if (retval)
471 goto error;
474 cdev = ir->cdev;
475 if (try_module_get(cdev->owner)) {
476 ir->open++;
477 if (ir->d.set_use_inc)
478 retval = ir->d.set_use_inc(ir->d.data);
480 if (retval) {
481 module_put(cdev->owner);
482 ir->open--;
483 } else {
484 lirc_buffer_clear(ir->buf);
486 if (ir->task)
487 wake_up_process(ir->task);
490 error:
491 nonseekable_open(inode, file);
493 return retval;
495 EXPORT_SYMBOL(lirc_dev_fop_open);
497 int lirc_dev_fop_close(struct inode *inode, struct file *file)
499 struct irctl *ir = irctls[iminor(inode)];
500 struct cdev *cdev;
501 int ret;
503 if (!ir) {
504 pr_err("called with invalid irctl\n");
505 return -EINVAL;
508 cdev = ir->cdev;
510 ret = mutex_lock_killable(&lirc_dev_lock);
511 WARN_ON(ret);
513 rc_close(ir->d.rdev);
515 ir->open--;
516 if (ir->attached) {
517 if (ir->d.set_use_dec)
518 ir->d.set_use_dec(ir->d.data);
519 module_put(cdev->owner);
520 } else {
521 lirc_irctl_cleanup(ir);
522 cdev_del(cdev);
523 irctls[ir->d.minor] = NULL;
524 kfree(cdev);
525 kfree(ir);
528 if (!ret)
529 mutex_unlock(&lirc_dev_lock);
531 return 0;
533 EXPORT_SYMBOL(lirc_dev_fop_close);
535 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait)
537 struct irctl *ir = irctls[iminor(file_inode(file))];
538 unsigned int ret;
540 if (!ir) {
541 pr_err("called with invalid irctl\n");
542 return POLLERR;
545 if (!ir->attached)
546 return POLLERR;
548 if (ir->buf) {
549 poll_wait(file, &ir->buf->wait_poll, wait);
551 if (lirc_buffer_empty(ir->buf))
552 ret = 0;
553 else
554 ret = POLLIN | POLLRDNORM;
555 } else
556 ret = POLLERR;
558 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
559 ir->d.name, ir->d.minor, ret);
561 return ret;
563 EXPORT_SYMBOL(lirc_dev_fop_poll);
565 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
567 __u32 mode;
568 int result = 0;
569 struct irctl *ir = irctls[iminor(file_inode(file))];
571 if (!ir) {
572 pr_err("no irctl found!\n");
573 return -ENODEV;
576 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n",
577 ir->d.name, ir->d.minor, cmd);
579 if (ir->d.minor == NOPLUG || !ir->attached) {
580 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n",
581 ir->d.name, ir->d.minor);
582 return -ENODEV;
585 mutex_lock(&ir->irctl_lock);
587 switch (cmd) {
588 case LIRC_GET_FEATURES:
589 result = put_user(ir->d.features, (__u32 __user *)arg);
590 break;
591 case LIRC_GET_REC_MODE:
592 if (!LIRC_CAN_REC(ir->d.features)) {
593 result = -ENOTTY;
594 break;
597 result = put_user(LIRC_REC2MODE
598 (ir->d.features & LIRC_CAN_REC_MASK),
599 (__u32 __user *)arg);
600 break;
601 case LIRC_SET_REC_MODE:
602 if (!LIRC_CAN_REC(ir->d.features)) {
603 result = -ENOTTY;
604 break;
607 result = get_user(mode, (__u32 __user *)arg);
608 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
609 result = -EINVAL;
611 * FIXME: We should actually set the mode somehow but
612 * for now, lirc_serial doesn't support mode changing either
614 break;
615 case LIRC_GET_LENGTH:
616 result = put_user(ir->d.code_length, (__u32 __user *)arg);
617 break;
618 case LIRC_GET_MIN_TIMEOUT:
619 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
620 ir->d.min_timeout == 0) {
621 result = -ENOTTY;
622 break;
625 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
626 break;
627 case LIRC_GET_MAX_TIMEOUT:
628 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
629 ir->d.max_timeout == 0) {
630 result = -ENOTTY;
631 break;
634 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
635 break;
636 default:
637 result = -EINVAL;
640 mutex_unlock(&ir->irctl_lock);
642 return result;
644 EXPORT_SYMBOL(lirc_dev_fop_ioctl);
646 ssize_t lirc_dev_fop_read(struct file *file,
647 char __user *buffer,
648 size_t length,
649 loff_t *ppos)
651 struct irctl *ir = irctls[iminor(file_inode(file))];
652 unsigned char *buf;
653 int ret = 0, written = 0;
654 DECLARE_WAITQUEUE(wait, current);
656 if (!ir) {
657 pr_err("called with invalid irctl\n");
658 return -ENODEV;
661 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
663 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
664 if (!buf)
665 return -ENOMEM;
667 if (mutex_lock_interruptible(&ir->irctl_lock)) {
668 ret = -ERESTARTSYS;
669 goto out_unlocked;
671 if (!ir->attached) {
672 ret = -ENODEV;
673 goto out_locked;
676 if (length % ir->chunk_size) {
677 ret = -EINVAL;
678 goto out_locked;
682 * we add ourselves to the task queue before buffer check
683 * to avoid losing scan code (in case when queue is awaken somewhere
684 * between while condition checking and scheduling)
686 add_wait_queue(&ir->buf->wait_poll, &wait);
687 set_current_state(TASK_INTERRUPTIBLE);
690 * while we didn't provide 'length' bytes, device is opened in blocking
691 * mode and 'copy_to_user' is happy, wait for data.
693 while (written < length && ret == 0) {
694 if (lirc_buffer_empty(ir->buf)) {
695 /* According to the read(2) man page, 'written' can be
696 * returned as less than 'length', instead of blocking
697 * again, returning -EWOULDBLOCK, or returning
698 * -ERESTARTSYS
700 if (written)
701 break;
702 if (file->f_flags & O_NONBLOCK) {
703 ret = -EWOULDBLOCK;
704 break;
706 if (signal_pending(current)) {
707 ret = -ERESTARTSYS;
708 break;
711 mutex_unlock(&ir->irctl_lock);
712 schedule();
713 set_current_state(TASK_INTERRUPTIBLE);
715 if (mutex_lock_interruptible(&ir->irctl_lock)) {
716 ret = -ERESTARTSYS;
717 remove_wait_queue(&ir->buf->wait_poll, &wait);
718 set_current_state(TASK_RUNNING);
719 goto out_unlocked;
722 if (!ir->attached) {
723 ret = -ENODEV;
724 break;
726 } else {
727 lirc_buffer_read(ir->buf, buf);
728 ret = copy_to_user((void __user *)buffer+written, buf,
729 ir->buf->chunk_size);
730 if (!ret)
731 written += ir->buf->chunk_size;
732 else
733 ret = -EFAULT;
737 remove_wait_queue(&ir->buf->wait_poll, &wait);
738 set_current_state(TASK_RUNNING);
740 out_locked:
741 mutex_unlock(&ir->irctl_lock);
743 out_unlocked:
744 kfree(buf);
746 return ret ? ret : written;
748 EXPORT_SYMBOL(lirc_dev_fop_read);
750 void *lirc_get_pdata(struct file *file)
752 return irctls[iminor(file_inode(file))]->d.data;
754 EXPORT_SYMBOL(lirc_get_pdata);
757 ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer,
758 size_t length, loff_t *ppos)
760 struct irctl *ir = irctls[iminor(file_inode(file))];
762 if (!ir) {
763 pr_err("called with invalid irctl\n");
764 return -ENODEV;
767 if (!ir->attached)
768 return -ENODEV;
770 return -EINVAL;
772 EXPORT_SYMBOL(lirc_dev_fop_write);
775 static int __init lirc_dev_init(void)
777 int retval;
779 lirc_class = class_create(THIS_MODULE, "lirc");
780 if (IS_ERR(lirc_class)) {
781 pr_err("class_create failed\n");
782 return PTR_ERR(lirc_class);
785 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES,
786 IRCTL_DEV_NAME);
787 if (retval) {
788 class_destroy(lirc_class);
789 pr_err("alloc_chrdev_region failed\n");
790 return retval;
794 pr_info("IR Remote Control driver registered, major %d\n",
795 MAJOR(lirc_base_dev));
797 return 0;
802 static void __exit lirc_dev_exit(void)
804 class_destroy(lirc_class);
805 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES);
806 pr_info("module unloaded\n");
809 module_init(lirc_dev_init);
810 module_exit(lirc_dev_exit);
812 MODULE_DESCRIPTION("LIRC base driver module");
813 MODULE_AUTHOR("Artur Lipowski");
814 MODULE_LICENSE("GPL");
816 module_param(debug, bool, S_IRUGO | S_IWUSR);
817 MODULE_PARM_DESC(debug, "Enable debugging messages");