2010-07-03 04:06:57 +00:00
|
|
|
/*
|
|
|
|
* LIRC base driver
|
|
|
|
*
|
|
|
|
* by Artur Lipowski <alipowski@interia.pl>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-07-06 09:01:16 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2010-07-03 04:06:57 +00:00
|
|
|
#include <linux/module.h>
|
2017-02-02 18:15:33 +00:00
|
|
|
#include <linux/sched/signal.h>
|
2010-07-03 04:06:57 +00:00
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/cdev.h>
|
2017-06-25 12:32:05 +00:00
|
|
|
#include <linux/idr.h>
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-09-23 14:41:13 +00:00
|
|
|
#include "rc-core-priv.h"
|
2010-07-03 04:06:57 +00:00
|
|
|
#include <media/lirc.h>
|
2010-07-16 17:25:33 +00:00
|
|
|
#include <media/lirc_dev.h>
|
2010-07-03 04:06:57 +00:00
|
|
|
|
|
|
|
#define LOGHEAD "lirc_dev (%s[%d]): "
|
|
|
|
|
|
|
|
static dev_t lirc_base_dev;
|
|
|
|
|
2017-06-25 12:32:05 +00:00
|
|
|
/* Used to keep track of allocated lirc devices */
|
|
|
|
#define LIRC_MAX_DEVICES 256
|
|
|
|
static DEFINE_IDA(lirc_ida);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
|
|
|
/* Only used for sysfs but defined to void otherwise */
|
|
|
|
static struct class *lirc_class;
|
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
static void lirc_release_device(struct device *ld)
|
2010-07-03 04:06:57 +00:00
|
|
|
{
|
2017-06-25 12:32:36 +00:00
|
|
|
struct lirc_dev *d = container_of(ld, struct lirc_dev, dev);
|
2017-09-23 18:44:18 +00:00
|
|
|
struct rc_dev *rcdev = d->rdev;
|
2017-06-30 08:41:57 +00:00
|
|
|
|
2017-09-23 18:44:18 +00:00
|
|
|
if (rcdev->driver_type == RC_DRIVER_IR_RAW)
|
|
|
|
kfifo_free(&rcdev->rawir);
|
2017-01-30 15:49:58 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
kfree(d);
|
|
|
|
module_put(THIS_MODULE);
|
2017-09-23 18:44:18 +00:00
|
|
|
put_device(d->dev.parent);
|
2016-07-06 09:01:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 12:32:15 +00:00
|
|
|
struct lirc_dev *
|
|
|
|
lirc_allocate_device(void)
|
|
|
|
{
|
2017-06-25 12:32:36 +00:00
|
|
|
struct lirc_dev *d;
|
|
|
|
|
|
|
|
d = kzalloc(sizeof(*d), GFP_KERNEL);
|
|
|
|
if (d) {
|
|
|
|
mutex_init(&d->mutex);
|
|
|
|
device_initialize(&d->dev);
|
|
|
|
d->dev.class = lirc_class;
|
|
|
|
d->dev.release = lirc_release_device;
|
|
|
|
__module_get(THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
2017-06-25 12:32:15 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_allocate_device);
|
|
|
|
|
|
|
|
void lirc_free_device(struct lirc_dev *d)
|
|
|
|
{
|
2017-06-25 12:32:36 +00:00
|
|
|
if (!d)
|
|
|
|
return;
|
|
|
|
|
|
|
|
put_device(&d->dev);
|
2017-06-25 12:32:15 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_free_device);
|
|
|
|
|
2017-09-21 19:13:34 +00:00
|
|
|
int lirc_register_device(struct lirc_dev *d)
|
2016-07-06 09:01:13 +00:00
|
|
|
{
|
2017-09-23 16:05:59 +00:00
|
|
|
struct rc_dev *rcdev = d->rdev;
|
2017-06-25 12:32:05 +00:00
|
|
|
int minor;
|
2010-07-03 04:06:57 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!d) {
|
2016-07-06 09:01:16 +00:00
|
|
|
pr_err("driver pointer must be not NULL!\n");
|
2016-07-06 09:01:17 +00:00
|
|
|
return -EBADRQC;
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
if (!d->dev.parent) {
|
|
|
|
pr_err("dev parent pointer not filled in!\n");
|
2016-07-06 09:01:17 +00:00
|
|
|
return -EINVAL;
|
2010-10-18 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 16:04:21 +00:00
|
|
|
if (!d->fops) {
|
|
|
|
pr_err("fops pointer not filled in!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:41:57 +00:00
|
|
|
/* some safety check 8-) */
|
|
|
|
d->name[sizeof(d->name) - 1] = '\0';
|
|
|
|
|
2017-09-23 16:05:59 +00:00
|
|
|
if (rcdev->driver_type == RC_DRIVER_IR_RAW) {
|
2017-09-23 18:44:18 +00:00
|
|
|
if (kfifo_alloc(&rcdev->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL))
|
|
|
|
return -ENOMEM;
|
2017-06-30 08:41:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 18:44:18 +00:00
|
|
|
init_waitqueue_head(&rcdev->wait_poll);
|
|
|
|
|
2017-06-25 12:32:05 +00:00
|
|
|
minor = ida_simple_get(&lirc_ida, 0, LIRC_MAX_DEVICES, GFP_KERNEL);
|
2017-06-25 12:32:36 +00:00
|
|
|
if (minor < 0)
|
2017-06-25 12:32:05 +00:00
|
|
|
return minor;
|
2017-05-01 16:04:21 +00:00
|
|
|
|
2010-07-03 04:06:57 +00:00
|
|
|
d->minor = minor;
|
2017-06-25 12:32:36 +00:00
|
|
|
d->dev.devt = MKDEV(MAJOR(lirc_base_dev), d->minor);
|
|
|
|
dev_set_name(&d->dev, "lirc%d", d->minor);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
cdev_init(&d->cdev, d->fops);
|
|
|
|
d->cdev.owner = d->owner;
|
2017-01-30 15:49:58 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
err = cdev_device_add(&d->cdev, &d->dev);
|
2017-06-30 08:41:57 +00:00
|
|
|
if (err) {
|
2017-06-25 12:32:05 +00:00
|
|
|
ida_simple_remove(&lirc_ida, minor);
|
2017-06-30 08:41:57 +00:00
|
|
|
return err;
|
|
|
|
}
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
get_device(d->dev.parent);
|
2017-08-04 14:12:03 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
dev_info(&d->dev, "lirc_dev: driver %s registered at minor = %d\n",
|
|
|
|
d->name, d->minor);
|
2017-05-01 16:04:11 +00:00
|
|
|
|
2017-06-25 12:31:24 +00:00
|
|
|
return 0;
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|
2017-09-21 19:13:34 +00:00
|
|
|
EXPORT_SYMBOL(lirc_register_device);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-09-21 19:13:34 +00:00
|
|
|
void lirc_unregister_device(struct lirc_dev *d)
|
2010-07-03 04:06:57 +00:00
|
|
|
{
|
2017-09-23 18:44:18 +00:00
|
|
|
struct rc_dev *rcdev;
|
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
if (!d)
|
2017-06-25 12:31:24 +00:00
|
|
|
return;
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-09-23 18:44:18 +00:00
|
|
|
rcdev = d->rdev;
|
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
dev_dbg(&d->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
|
2017-06-25 12:31:24 +00:00
|
|
|
d->name, d->minor);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
mutex_lock(&d->mutex);
|
2017-06-30 08:41:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
if (d->open) {
|
|
|
|
dev_dbg(&d->dev, LOGHEAD "releasing opened driver\n",
|
2017-06-25 12:31:24 +00:00
|
|
|
d->name, d->minor);
|
2017-09-23 18:44:18 +00:00
|
|
|
wake_up_poll(&rcdev->wait_poll, POLLHUP);
|
2017-01-30 15:49:58 +00:00
|
|
|
}
|
2016-07-06 09:01:26 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
cdev_device_del(&d->cdev, &d->dev);
|
2017-06-25 12:32:05 +00:00
|
|
|
ida_simple_remove(&lirc_ida, d->minor);
|
2017-06-25 12:32:36 +00:00
|
|
|
put_device(&d->dev);
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|
2017-09-21 19:13:34 +00:00
|
|
|
EXPORT_SYMBOL(lirc_unregister_device);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
|
|
|
int lirc_dev_fop_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-06-25 12:32:36 +00:00
|
|
|
struct lirc_dev *d = container_of(inode->i_cdev, struct lirc_dev, cdev);
|
2017-09-23 18:44:18 +00:00
|
|
|
struct rc_dev *rcdev = d->rdev;
|
2017-06-25 12:31:19 +00:00
|
|
|
int retval;
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
dev_dbg(&d->dev, LOGHEAD "open called\n", d->name, d->minor);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
retval = mutex_lock_interruptible(&d->mutex);
|
2017-06-30 08:41:57 +00:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
2017-09-26 11:31:29 +00:00
|
|
|
if (!rcdev->registered) {
|
2017-06-30 08:41:57 +00:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
if (d->open) {
|
2017-06-30 08:41:57 +00:00
|
|
|
retval = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
if (d->rdev) {
|
|
|
|
retval = rc_open(d->rdev);
|
[media] media: lirc: Allow lirc dev to talk to rc device
The use case is simple, if any rc device has allowed protocols =
RC_TYPE_LIRC and map_name = RC_MAP_LIRC set, the driver open will be never
called. The reason for this is, all of the key maps except lirc have some
KEYS in there map, so during rc_register_device process these keys are
matched against the input drivers and open is performed, so for the case
of RC_MAP_EMPTY, a vt/keyboard is matched and the driver open is
performed.
In case of lirc, there is no match and result is that there is no open
performed, however the lirc-dev will go ahead and create a /dev/lirc0
node. Now when lircd/mode2 opens this device, no data is available
because the driver was never opened.
Other case pointed by Sean Young, As rc device gets opened via the
input interface. If the input device is never opened (e.g. embedded with
no console) then the rc open is never called and lirc will not work
either. So that's another case.
lirc_dev seems to have no link with actual rc device w.r.t open/close.
This patch adds rc_dev pointer to lirc_driver structure for cases like
this, so that it can do the open/close of the real driver in accordance
to lircd/mode2 open/close.
Without this patch its impossible to open a rc device which has
RC_TYPE_LIRC ad RC_MAP_LIRC set.
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@st.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2013-07-22 07:23:07 +00:00
|
|
|
if (retval)
|
2017-06-30 08:41:57 +00:00
|
|
|
goto out;
|
[media] media: lirc: Allow lirc dev to talk to rc device
The use case is simple, if any rc device has allowed protocols =
RC_TYPE_LIRC and map_name = RC_MAP_LIRC set, the driver open will be never
called. The reason for this is, all of the key maps except lirc have some
KEYS in there map, so during rc_register_device process these keys are
matched against the input drivers and open is performed, so for the case
of RC_MAP_EMPTY, a vt/keyboard is matched and the driver open is
performed.
In case of lirc, there is no match and result is that there is no open
performed, however the lirc-dev will go ahead and create a /dev/lirc0
node. Now when lircd/mode2 opens this device, no data is available
because the driver was never opened.
Other case pointed by Sean Young, As rc device gets opened via the
input interface. If the input device is never opened (e.g. embedded with
no console) then the rc open is never called and lirc will not work
either. So that's another case.
lirc_dev seems to have no link with actual rc device w.r.t open/close.
This patch adds rc_dev pointer to lirc_driver structure for cases like
this, so that it can do the open/close of the real driver in accordance
to lircd/mode2 open/close.
Without this patch its impossible to open a rc device which has
RC_TYPE_LIRC ad RC_MAP_LIRC set.
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@st.com>
Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
2013-07-22 07:23:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 18:44:18 +00:00
|
|
|
if (rcdev->driver_type == RC_DRIVER_IR_RAW)
|
|
|
|
kfifo_reset_out(&rcdev->rawir);
|
2017-05-01 16:03:46 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
d->open++;
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-09-23 14:41:13 +00:00
|
|
|
file->private_data = d->rdev;
|
2010-08-15 16:51:56 +00:00
|
|
|
nonseekable_open(inode, file);
|
2017-06-25 12:32:36 +00:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-08-15 16:51:56 +00:00
|
|
|
|
2017-06-25 12:31:19 +00:00
|
|
|
return 0;
|
2017-06-30 08:41:57 +00:00
|
|
|
|
|
|
|
out:
|
2017-06-25 12:32:36 +00:00
|
|
|
mutex_unlock(&d->mutex);
|
2017-06-30 08:41:57 +00:00
|
|
|
return retval;
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_open);
|
|
|
|
|
|
|
|
int lirc_dev_fop_close(struct inode *inode, struct file *file)
|
|
|
|
{
|
2017-09-23 14:41:13 +00:00
|
|
|
struct rc_dev *rcdev = file->private_data;
|
|
|
|
struct lirc_dev *d = rcdev->lirc_dev;
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
mutex_lock(&d->mutex);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2017-09-23 14:41:13 +00:00
|
|
|
rc_close(rcdev);
|
2017-06-25 12:32:36 +00:00
|
|
|
d->open--;
|
2017-06-30 08:41:57 +00:00
|
|
|
|
2017-06-25 12:32:36 +00:00
|
|
|
mutex_unlock(&d->mutex);
|
2010-07-03 04:06:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_dev_fop_close);
|
|
|
|
|
2017-09-23 14:41:13 +00:00
|
|
|
int __init lirc_dev_init(void)
|
2010-07-03 04:06:57 +00:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
lirc_class = class_create(THIS_MODULE, "lirc");
|
|
|
|
if (IS_ERR(lirc_class)) {
|
2016-07-06 09:01:16 +00:00
|
|
|
pr_err("class_create failed\n");
|
2016-07-06 09:01:17 +00:00
|
|
|
return PTR_ERR(lirc_class);
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 12:32:05 +00:00
|
|
|
retval = alloc_chrdev_region(&lirc_base_dev, 0, LIRC_MAX_DEVICES,
|
2017-05-01 16:04:47 +00:00
|
|
|
"BaseRemoteCtl");
|
2010-07-03 04:06:57 +00:00
|
|
|
if (retval) {
|
|
|
|
class_destroy(lirc_class);
|
2016-07-06 09:01:16 +00:00
|
|
|
pr_err("alloc_chrdev_region failed\n");
|
2016-07-06 09:01:17 +00:00
|
|
|
return retval;
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 09:01:16 +00:00
|
|
|
pr_info("IR Remote Control driver registered, major %d\n",
|
|
|
|
MAJOR(lirc_base_dev));
|
2010-07-03 04:06:57 +00:00
|
|
|
|
2016-07-06 09:01:17 +00:00
|
|
|
return 0;
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:41:13 +00:00
|
|
|
void __exit lirc_dev_exit(void)
|
2010-07-03 04:06:57 +00:00
|
|
|
{
|
|
|
|
class_destroy(lirc_class);
|
2017-06-25 12:32:05 +00:00
|
|
|
unregister_chrdev_region(lirc_base_dev, LIRC_MAX_DEVICES);
|
2010-07-03 04:06:57 +00:00
|
|
|
}
|