2010-07-03 12:06:57 +08: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 17:01:16 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
#include <linux/module.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/cdev.h>
|
2017-06-25 20:32:05 +08:00
|
|
|
#include <linux/idr.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
#include "rc-core-priv.h"
|
2010-07-03 12:06:57 +08:00
|
|
|
#include <media/lirc.h>
|
2010-07-17 01:25:33 +08:00
|
|
|
#include <media/lirc_dev.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
#define LOGHEAD "lirc_dev (%s[%d]): "
|
|
|
|
|
|
|
|
static dev_t lirc_base_dev;
|
|
|
|
|
2017-06-25 20:32:05 +08:00
|
|
|
/* Used to keep track of allocated lirc devices */
|
|
|
|
#define LIRC_MAX_DEVICES 256
|
|
|
|
static DEFINE_IDA(lirc_ida);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
/* Only used for sysfs but defined to void otherwise */
|
|
|
|
static struct class *lirc_class;
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
static void lirc_release_device(struct device *ld)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
struct lirc_dev *d = container_of(ld, struct lirc_dev, dev);
|
2017-09-24 02:44:18 +08:00
|
|
|
struct rc_dev *rcdev = d->rdev;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-09-24 02:44:18 +08:00
|
|
|
if (rcdev->driver_type == RC_DRIVER_IR_RAW)
|
|
|
|
kfifo_free(&rcdev->rawir);
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
kfree(d);
|
|
|
|
module_put(THIS_MODULE);
|
2017-09-24 02:44:18 +08:00
|
|
|
put_device(d->dev.parent);
|
2016-07-06 17:01:13 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:15 +08:00
|
|
|
struct lirc_dev *
|
|
|
|
lirc_allocate_device(void)
|
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
struct lirc_dev *d;
|
|
|
|
|
|
|
|
d = kzalloc(sizeof(*d), GFP_KERNEL);
|
|
|
|
if (d) {
|
|
|
|
device_initialize(&d->dev);
|
|
|
|
d->dev.class = lirc_class;
|
|
|
|
d->dev.release = lirc_release_device;
|
|
|
|
__module_get(THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
2017-06-25 20:32:15 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_allocate_device);
|
|
|
|
|
|
|
|
void lirc_free_device(struct lirc_dev *d)
|
|
|
|
{
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d)
|
|
|
|
return;
|
|
|
|
|
|
|
|
put_device(&d->dev);
|
2017-06-25 20:32:15 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(lirc_free_device);
|
|
|
|
|
2017-09-22 03:13:34 +08:00
|
|
|
int lirc_register_device(struct lirc_dev *d)
|
2016-07-06 17:01:13 +08:00
|
|
|
{
|
2017-09-24 00:05:59 +08:00
|
|
|
struct rc_dev *rcdev = d->rdev;
|
2017-06-25 20:32:05 +08:00
|
|
|
int minor;
|
2010-07-03 12:06:57 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!d) {
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_err("driver pointer must be not NULL!\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return -EBADRQC;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d->dev.parent) {
|
|
|
|
pr_err("dev parent pointer not filled in!\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return -EINVAL;
|
2010-10-18 23:02:01 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 00:04:21 +08:00
|
|
|
if (!d->fops) {
|
|
|
|
pr_err("fops pointer not filled in!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-09-24 00:05:59 +08:00
|
|
|
if (rcdev->driver_type == RC_DRIVER_IR_RAW) {
|
2017-09-24 02:44:18 +08:00
|
|
|
if (kfifo_alloc(&rcdev->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL))
|
|
|
|
return -ENOMEM;
|
2017-06-30 16:41:57 +08:00
|
|
|
}
|
|
|
|
|
2017-09-24 02:44:18 +08:00
|
|
|
init_waitqueue_head(&rcdev->wait_poll);
|
|
|
|
|
2017-06-25 20:32:05 +08:00
|
|
|
minor = ida_simple_get(&lirc_ida, 0, LIRC_MAX_DEVICES, GFP_KERNEL);
|
2017-06-25 20:32:36 +08:00
|
|
|
if (minor < 0)
|
2017-06-25 20:32:05 +08:00
|
|
|
return minor;
|
2017-05-02 00:04:21 +08:00
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
d->minor = minor;
|
2017-06-25 20:32:36 +08:00
|
|
|
d->dev.devt = MKDEV(MAJOR(lirc_base_dev), d->minor);
|
|
|
|
dev_set_name(&d->dev, "lirc%d", d->minor);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
cdev_init(&d->cdev, d->fops);
|
|
|
|
d->cdev.owner = d->owner;
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
err = cdev_device_add(&d->cdev, &d->dev);
|
2017-06-30 16:41:57 +08:00
|
|
|
if (err) {
|
2017-06-25 20:32:05 +08:00
|
|
|
ida_simple_remove(&lirc_ida, minor);
|
2017-06-30 16:41:57 +08:00
|
|
|
return err;
|
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
get_device(d->dev.parent);
|
2017-08-04 22:12:03 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_info(&d->dev, "lirc_dev: driver %s registered at minor = %d\n",
|
2017-09-26 19:56:39 +08:00
|
|
|
rcdev->driver_name, d->minor);
|
2017-05-02 00:04:11 +08:00
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
return 0;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
2017-09-22 03:13:34 +08:00
|
|
|
EXPORT_SYMBOL(lirc_register_device);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-22 03:13:34 +08:00
|
|
|
void lirc_unregister_device(struct lirc_dev *d)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-09-24 02:44:18 +08:00
|
|
|
struct rc_dev *rcdev;
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
if (!d)
|
2017-06-25 20:31:24 +08:00
|
|
|
return;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-24 02:44:18 +08:00
|
|
|
rcdev = d->rdev;
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_dbg(&d->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
|
2017-09-26 19:56:39 +08:00
|
|
|
rcdev->driver_name, d->minor);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-26 19:44:20 +08:00
|
|
|
mutex_lock(&rcdev->lock);
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-09-26 19:44:20 +08:00
|
|
|
if (rcdev->lirc_open) {
|
2017-06-25 20:32:36 +08:00
|
|
|
dev_dbg(&d->dev, LOGHEAD "releasing opened driver\n",
|
2017-09-26 19:56:39 +08:00
|
|
|
rcdev->driver_name, d->minor);
|
2017-09-24 02:44:18 +08:00
|
|
|
wake_up_poll(&rcdev->wait_poll, POLLHUP);
|
2017-01-30 23:49:58 +08:00
|
|
|
}
|
2016-07-06 17:01:26 +08:00
|
|
|
|
2017-09-26 19:44:20 +08:00
|
|
|
mutex_unlock(&rcdev->lock);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
cdev_device_del(&d->cdev, &d->dev);
|
2017-06-25 20:32:05 +08:00
|
|
|
ida_simple_remove(&lirc_ida, d->minor);
|
2017-06-25 20:32:36 +08:00
|
|
|
put_device(&d->dev);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
2017-09-22 03:13:34 +08:00
|
|
|
EXPORT_SYMBOL(lirc_unregister_device);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
int __init lirc_dev_init(void)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
lirc_class = class_create(THIS_MODULE, "lirc");
|
|
|
|
if (IS_ERR(lirc_class)) {
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_err("class_create failed\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return PTR_ERR(lirc_class);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-25 20:32:05 +08:00
|
|
|
retval = alloc_chrdev_region(&lirc_base_dev, 0, LIRC_MAX_DEVICES,
|
2017-05-02 00:04:47 +08:00
|
|
|
"BaseRemoteCtl");
|
2010-07-03 12:06:57 +08:00
|
|
|
if (retval) {
|
|
|
|
class_destroy(lirc_class);
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_err("alloc_chrdev_region failed\n");
|
2016-07-06 17:01:17 +08:00
|
|
|
return retval;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2016-07-06 17:01:16 +08:00
|
|
|
pr_info("IR Remote Control driver registered, major %d\n",
|
|
|
|
MAJOR(lirc_base_dev));
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2016-07-06 17:01:17 +08:00
|
|
|
return 0;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
void __exit lirc_dev_exit(void)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
|
|
|
class_destroy(lirc_class);
|
2017-06-25 20:32:05 +08:00
|
|
|
unregister_chrdev_region(lirc_base_dev, LIRC_MAX_DEVICES);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|