sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / rc / lirc_dev.c
blob3854809e853110ea74867d5ecda8f08acb3609b6
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 .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;
161 struct cdev *cdev;
162 int retval;
164 cdev = cdev_alloc();
165 if (!cdev)
166 return -ENOMEM;
168 if (d->fops) {
169 cdev->ops = d->fops;
170 cdev->owner = d->owner;
171 } else {
172 cdev->ops = &lirc_dev_fops;
173 cdev->owner = THIS_MODULE;
175 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor);
176 if (retval)
177 goto err_out;
179 retval = cdev_add(cdev, MKDEV(MAJOR(lirc_base_dev), d->minor), 1);
180 if (retval)
181 goto err_out;
183 ir->cdev = cdev;
185 return 0;
187 err_out:
188 cdev_del(cdev);
189 return retval;
192 static int lirc_allocate_buffer(struct irctl *ir)
194 int err = 0;
195 int bytes_in_key;
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;
207 if (d->rbuf) {
208 ir->buf = d->rbuf;
209 } else {
210 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
211 if (!ir->buf) {
212 err = -ENOMEM;
213 goto out;
216 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size);
217 if (err) {
218 kfree(ir->buf);
219 goto out;
222 ir->chunk_size = ir->buf->chunk_size;
224 out:
225 mutex_unlock(&lirc_dev_lock);
227 return err;
230 static int lirc_allocate_driver(struct lirc_driver *d)
232 struct irctl *ir;
233 int minor;
234 int err;
236 if (!d) {
237 pr_err("driver pointer must be not NULL!\n");
238 return -EBADRQC;
241 if (!d->dev) {
242 pr_err("dev pointer not filled in!\n");
243 return -EINVAL;
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);
249 return -EBADRQC;
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",
254 BUFLEN * 8);
255 return -EBADRQC;
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",
261 d->sample_rate);
262 return -EBADRQC;
264 if (!d->add_to_buf) {
265 dev_err(d->dev, "add_to_buf not set\n");
266 return -EBADRQC;
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");
271 return -EBADRQC;
274 mutex_lock(&lirc_dev_lock);
276 minor = d->minor;
278 if (minor < 0) {
279 /* find first free slot for driver */
280 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++)
281 if (!irctls[minor])
282 break;
283 if (minor == MAX_IRCTL_DEVICES) {
284 dev_err(d->dev, "no free slots for drivers!\n");
285 err = -ENOMEM;
286 goto out_lock;
288 } else if (irctls[minor]) {
289 dev_err(d->dev, "minor (%d) just registered!\n", minor);
290 err = -EBUSY;
291 goto out_lock;
294 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL);
295 if (!ir) {
296 err = -ENOMEM;
297 goto out_lock;
299 lirc_irctl_init(ir);
300 irctls[minor] = ir;
301 d->minor = minor;
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;
309 ir->d = *d;
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",
322 d->minor);
323 err = -ECHILD;
324 goto out_sysfs;
326 } else {
327 /* it means - wait for external event in task queue */
328 ir->jiffies_to_wait = 0;
331 err = lirc_cdev_add(ir);
332 if (err)
333 goto out_sysfs;
335 ir->attached = 1;
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);
340 return minor;
342 out_sysfs:
343 device_destroy(lirc_class, MKDEV(MAJOR(lirc_base_dev), ir->d.minor));
344 out_lock:
345 mutex_unlock(&lirc_dev_lock);
347 return err;
350 int lirc_register_driver(struct lirc_driver *d)
352 int minor, err = 0;
354 minor = lirc_allocate_driver(d);
355 if (minor < 0)
356 return minor;
358 if (LIRC_CAN_REC(d->features)) {
359 err = lirc_allocate_buffer(irctls[minor]);
360 if (err)
361 lirc_unregister_driver(minor);
364 return err ? err : minor;
366 EXPORT_SYMBOL(lirc_register_driver);
368 int lirc_unregister_driver(int minor)
370 struct irctl *ir;
371 struct cdev *cdev;
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);
376 return -EBADRQC;
379 ir = irctls[minor];
380 if (!ir) {
381 pr_err("failed to get irctl\n");
382 return -ENOENT;
385 cdev = ir->cdev;
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",
391 minor);
392 mutex_unlock(&lirc_dev_lock);
393 return -ENOENT;
396 /* end up polling thread */
397 if (ir->task)
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);
403 ir->attached = 0;
404 if (ir->open) {
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);
415 } else {
416 lirc_irctl_cleanup(ir);
417 cdev_del(cdev);
418 kfree(ir);
419 irctls[minor] = NULL;
422 mutex_unlock(&lirc_dev_lock);
424 return 0;
426 EXPORT_SYMBOL(lirc_unregister_driver);
428 int lirc_dev_fop_open(struct inode *inode, struct file *file)
430 struct irctl *ir;
431 struct cdev *cdev;
432 int retval = 0;
434 if (iminor(inode) >= MAX_IRCTL_DEVICES) {
435 pr_err("open result for %d is -ENODEV\n", iminor(inode));
436 return -ENODEV;
439 if (mutex_lock_interruptible(&lirc_dev_lock))
440 return -ERESTARTSYS;
442 ir = irctls[iminor(inode)];
443 if (!ir) {
444 retval = -ENODEV;
445 goto error;
448 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor);
450 if (ir->d.minor == NOPLUG) {
451 retval = -ENODEV;
452 goto error;
455 if (ir->open) {
456 retval = -EBUSY;
457 goto error;
460 if (ir->d.rdev) {
461 retval = rc_open(ir->d.rdev);
462 if (retval)
463 goto error;
466 cdev = ir->cdev;
467 if (try_module_get(cdev->owner)) {
468 ir->open++;
469 if (ir->d.set_use_inc)
470 retval = ir->d.set_use_inc(ir->d.data);
472 if (retval) {
473 module_put(cdev->owner);
474 ir->open--;
475 } else {
476 lirc_buffer_clear(ir->buf);
478 if (ir->task)
479 wake_up_process(ir->task);
482 error:
483 mutex_unlock(&lirc_dev_lock);
485 nonseekable_open(inode, file);
487 return retval;
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)];
494 struct cdev *cdev;
495 int ret;
497 if (!ir) {
498 pr_err("called with invalid irctl\n");
499 return -EINVAL;
502 cdev = ir->cdev;
504 ret = mutex_lock_killable(&lirc_dev_lock);
505 WARN_ON(ret);
507 rc_close(ir->d.rdev);
509 ir->open--;
510 if (ir->attached) {
511 if (ir->d.set_use_dec)
512 ir->d.set_use_dec(ir->d.data);
513 module_put(cdev->owner);
514 } else {
515 lirc_irctl_cleanup(ir);
516 cdev_del(cdev);
517 irctls[ir->d.minor] = NULL;
518 kfree(ir);
521 if (!ret)
522 mutex_unlock(&lirc_dev_lock);
524 return 0;
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))];
531 unsigned int ret;
533 if (!ir) {
534 pr_err("called with invalid irctl\n");
535 return POLLERR;
538 if (!ir->attached)
539 return POLLERR;
541 if (ir->buf) {
542 poll_wait(file, &ir->buf->wait_poll, wait);
544 if (lirc_buffer_empty(ir->buf))
545 ret = 0;
546 else
547 ret = POLLIN | POLLRDNORM;
548 } else
549 ret = POLLERR;
551 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n",
552 ir->d.name, ir->d.minor, ret);
554 return ret;
556 EXPORT_SYMBOL(lirc_dev_fop_poll);
558 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
560 __u32 mode;
561 int result = 0;
562 struct irctl *ir = irctls[iminor(file_inode(file))];
564 if (!ir) {
565 pr_err("no irctl found!\n");
566 return -ENODEV;
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);
575 return -ENODEV;
578 mutex_lock(&ir->irctl_lock);
580 switch (cmd) {
581 case LIRC_GET_FEATURES:
582 result = put_user(ir->d.features, (__u32 __user *)arg);
583 break;
584 case LIRC_GET_REC_MODE:
585 if (LIRC_CAN_REC(ir->d.features)) {
586 result = -ENOTTY;
587 break;
590 result = put_user(LIRC_REC2MODE
591 (ir->d.features & LIRC_CAN_REC_MASK),
592 (__u32 __user *)arg);
593 break;
594 case LIRC_SET_REC_MODE:
595 if (LIRC_CAN_REC(ir->d.features)) {
596 result = -ENOTTY;
597 break;
600 result = get_user(mode, (__u32 __user *)arg);
601 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features))
602 result = -EINVAL;
604 * FIXME: We should actually set the mode somehow but
605 * for now, lirc_serial doesn't support mode changing either
607 break;
608 case LIRC_GET_LENGTH:
609 result = put_user(ir->d.code_length, (__u32 __user *)arg);
610 break;
611 case LIRC_GET_MIN_TIMEOUT:
612 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
613 ir->d.min_timeout == 0) {
614 result = -ENOTTY;
615 break;
618 result = put_user(ir->d.min_timeout, (__u32 __user *)arg);
619 break;
620 case LIRC_GET_MAX_TIMEOUT:
621 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) ||
622 ir->d.max_timeout == 0) {
623 result = -ENOTTY;
624 break;
627 result = put_user(ir->d.max_timeout, (__u32 __user *)arg);
628 break;
629 default:
630 result = -EINVAL;
633 mutex_unlock(&ir->irctl_lock);
635 return result;
637 EXPORT_SYMBOL(lirc_dev_fop_ioctl);
639 ssize_t lirc_dev_fop_read(struct file *file,
640 char __user *buffer,
641 size_t length,
642 loff_t *ppos)
644 struct irctl *ir = irctls[iminor(file_inode(file))];
645 unsigned char *buf;
646 int ret = 0, written = 0;
647 DECLARE_WAITQUEUE(wait, current);
649 if (!ir) {
650 pr_err("called with invalid irctl\n");
651 return -ENODEV;
654 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor);
656 buf = kzalloc(ir->chunk_size, GFP_KERNEL);
657 if (!buf)
658 return -ENOMEM;
660 if (mutex_lock_interruptible(&ir->irctl_lock)) {
661 ret = -ERESTARTSYS;
662 goto out_unlocked;
664 if (!ir->attached) {
665 ret = -ENODEV;
666 goto out_locked;
669 if (length % ir->chunk_size) {
670 ret = -EINVAL;
671 goto out_locked;
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
690 * -ERESTARTSYS
692 if (written)
693 break;
694 if (file->f_flags & O_NONBLOCK) {
695 ret = -EWOULDBLOCK;
696 break;
698 if (signal_pending(current)) {
699 ret = -ERESTARTSYS;
700 break;
703 mutex_unlock(&ir->irctl_lock);
704 set_current_state(TASK_INTERRUPTIBLE);
705 schedule();
706 set_current_state(TASK_RUNNING);
708 if (mutex_lock_interruptible(&ir->irctl_lock)) {
709 ret = -ERESTARTSYS;
710 remove_wait_queue(&ir->buf->wait_poll, &wait);
711 goto out_unlocked;
714 if (!ir->attached) {
715 ret = -ENODEV;
716 goto out_locked;
718 } else {
719 lirc_buffer_read(ir->buf, buf);
720 ret = copy_to_user((void __user *)buffer+written, buf,
721 ir->buf->chunk_size);
722 if (!ret)
723 written += ir->buf->chunk_size;
724 else
725 ret = -EFAULT;
729 remove_wait_queue(&ir->buf->wait_poll, &wait);
731 out_locked:
732 mutex_unlock(&ir->irctl_lock);
734 out_unlocked:
735 kfree(buf);
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))];
753 if (!ir) {
754 pr_err("called with invalid irctl\n");
755 return -ENODEV;
758 if (!ir->attached)
759 return -ENODEV;
761 return -EINVAL;
763 EXPORT_SYMBOL(lirc_dev_fop_write);
766 static int __init lirc_dev_init(void)
768 int retval;
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,
777 IRCTL_DEV_NAME);
778 if (retval) {
779 class_destroy(lirc_class);
780 pr_err("alloc_chrdev_region failed\n");
781 return retval;
785 pr_info("IR Remote Control driver registered, major %d\n",
786 MAJOR(lirc_base_dev));
788 return 0;
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");