2019-05-27 14:55:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-07-03 12:06:57 +08:00
|
|
|
/*
|
|
|
|
* LIRC base driver
|
|
|
|
*
|
|
|
|
* by Artur Lipowski <alipowski@interia.pl>
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/device.h>
|
2018-05-27 19:24:09 +08:00
|
|
|
#include <linux/file.h>
|
2017-06-25 20:32:05 +08:00
|
|
|
#include <linux/idr.h>
|
2017-09-26 21:34:47 +08:00
|
|
|
#include <linux/poll.h>
|
2017-11-03 04:39:16 +08:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/wait.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-23 22:41:13 +08:00
|
|
|
#include "rc-core-priv.h"
|
2017-11-03 04:44:21 +08:00
|
|
|
#include <uapi/linux/lirc.h>
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2019-06-13 16:49:26 +08:00
|
|
|
#define LIRCBUF_SIZE 1024
|
2010-07-03 12:06:57 +08:00
|
|
|
|
|
|
|
static dev_t lirc_base_dev;
|
|
|
|
|
2017-06-25 20:32:05 +08:00
|
|
|
/* Used to keep track of allocated lirc devices */
|
|
|
|
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-11-03 04:39:16 +08:00
|
|
|
/**
|
2020-08-25 03:10:45 +08:00
|
|
|
* lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace
|
2017-11-03 04:39:16 +08:00
|
|
|
*
|
|
|
|
* @dev: the struct rc_dev descriptor of the device
|
|
|
|
* @ev: the struct ir_raw_event descriptor of the pulse/space
|
|
|
|
*/
|
2020-08-25 03:10:45 +08:00
|
|
|
void lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
unsigned long flags;
|
|
|
|
struct lirc_fh *fh;
|
2017-11-03 04:39:16 +08:00
|
|
|
int sample;
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
/* Packet start */
|
|
|
|
if (ev.reset) {
|
|
|
|
/*
|
|
|
|
* Userspace expects a long space event before the start of
|
|
|
|
* the signal to use as a sync. This may be done with repeat
|
|
|
|
* packets and normal samples. But if a reset has been sent
|
|
|
|
* then we assume that a long time has passed, so we send a
|
|
|
|
* space with the maximum time value.
|
|
|
|
*/
|
|
|
|
sample = LIRC_SPACE(LIRC_VALUE_MASK);
|
2018-02-12 20:27:50 +08:00
|
|
|
dev_dbg(&dev->dev, "delivering reset sync space to lirc_dev\n");
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
/* Carrier reports */
|
|
|
|
} else if (ev.carrier_report) {
|
|
|
|
sample = LIRC_FREQUENCY(ev.carrier);
|
2018-02-12 20:27:50 +08:00
|
|
|
dev_dbg(&dev->dev, "carrier report (freq: %d)\n", sample);
|
2016-07-06 17:01:13 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
/* Packet end */
|
|
|
|
} else if (ev.timeout) {
|
|
|
|
dev->gap_start = ktime_get();
|
2017-05-01 21:32:34 +08:00
|
|
|
|
2020-08-24 01:23:05 +08:00
|
|
|
sample = LIRC_TIMEOUT(ev.duration);
|
2018-02-12 20:27:50 +08:00
|
|
|
dev_dbg(&dev->dev, "timeout report (duration: %d)\n", sample);
|
2016-07-06 17:01:13 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
/* Normal sample */
|
|
|
|
} else {
|
2022-01-13 19:29:01 +08:00
|
|
|
if (dev->gap_start) {
|
|
|
|
u64 duration = ktime_us_delta(ktime_get(),
|
|
|
|
dev->gap_start);
|
2017-11-03 04:39:16 +08:00
|
|
|
|
2020-08-24 01:23:05 +08:00
|
|
|
/* Cap by LIRC_VALUE_MASK */
|
2022-01-13 19:29:01 +08:00
|
|
|
duration = min_t(u64, duration, LIRC_VALUE_MASK);
|
2017-11-03 04:39:16 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_lock_irqsave(&dev->lirc_fh_lock, flags);
|
|
|
|
list_for_each_entry(fh, &dev->lirc_fh, list)
|
2022-01-13 19:29:01 +08:00
|
|
|
kfifo_put(&fh->rawir, LIRC_SPACE(duration));
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
|
2022-01-13 19:29:01 +08:00
|
|
|
dev->gap_start = 0;
|
2017-11-03 04:39:16 +08:00
|
|
|
}
|
2016-07-06 17:01:13 +08:00
|
|
|
|
2020-08-24 01:23:05 +08:00
|
|
|
sample = ev.pulse ? LIRC_PULSE(ev.duration) :
|
|
|
|
LIRC_SPACE(ev.duration);
|
2018-02-12 20:27:50 +08:00
|
|
|
dev_dbg(&dev->dev, "delivering %uus %s to lirc_dev\n",
|
2020-08-24 01:23:05 +08:00
|
|
|
ev.duration, TO_STR(ev.pulse));
|
2017-06-25 20:32:36 +08:00
|
|
|
}
|
|
|
|
|
2018-05-27 19:24:09 +08:00
|
|
|
/*
|
|
|
|
* bpf does not care about the gap generated above; that exists
|
|
|
|
* for backwards compatibility
|
|
|
|
*/
|
|
|
|
lirc_bpf_run(dev, sample);
|
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_lock_irqsave(&dev->lirc_fh_lock, flags);
|
|
|
|
list_for_each_entry(fh, &dev->lirc_fh, list) {
|
|
|
|
if (kfifo_put(&fh->rawir, sample))
|
2018-02-12 06:34:03 +08:00
|
|
|
wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
|
2017-11-03 05:21:13 +08:00
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
|
2017-06-25 20:32:15 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
/**
|
2020-08-25 03:10:45 +08:00
|
|
|
* lirc_scancode_event() - Send scancode data to lirc to be relayed to
|
2017-11-03 05:21:13 +08:00
|
|
|
* userspace. This can be called in atomic context.
|
2017-11-03 04:39:16 +08:00
|
|
|
* @dev: the struct rc_dev descriptor of the device
|
|
|
|
* @lsc: the struct lirc_scancode describing the decoded scancode
|
|
|
|
*/
|
2020-08-25 03:10:45 +08:00
|
|
|
void lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc)
|
2016-07-06 17:01:13 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
unsigned long flags;
|
|
|
|
struct lirc_fh *fh;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
lsc->timestamp = ktime_get_ns();
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_lock_irqsave(&dev->lirc_fh_lock, flags);
|
|
|
|
list_for_each_entry(fh, &dev->lirc_fh, list) {
|
|
|
|
if (kfifo_put(&fh->scancodes, *lsc))
|
2018-02-12 06:34:03 +08:00
|
|
|
wake_up_poll(&fh->wait_poll, EPOLLIN | EPOLLRDNORM);
|
2010-10-18 23:02:01 +08:00
|
|
|
}
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
|
2017-11-03 04:39:16 +08:00
|
|
|
}
|
2020-08-25 03:10:45 +08:00
|
|
|
EXPORT_SYMBOL_GPL(lirc_scancode_event);
|
2010-10-18 23:02:01 +08:00
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static int lirc_open(struct inode *inode, struct file *file)
|
2017-11-03 04:39:16 +08:00
|
|
|
{
|
|
|
|
struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev,
|
|
|
|
lirc_cdev);
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
|
|
|
|
unsigned long flags;
|
2017-11-03 04:39:16 +08:00
|
|
|
int retval;
|
2017-05-02 00:04:21 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (!fh)
|
|
|
|
return -ENOMEM;
|
2017-06-25 20:31:45 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
get_device(&dev->dev);
|
2017-06-25 20:31:45 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (!dev->registered) {
|
|
|
|
retval = -ENODEV;
|
2017-11-03 05:21:13 +08:00
|
|
|
goto out_fh;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (dev->driver_type == RC_DRIVER_IR_RAW) {
|
|
|
|
if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
goto out_fh;
|
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
|
|
|
|
if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) {
|
|
|
|
retval = -ENOMEM;
|
|
|
|
goto out_rawir;
|
|
|
|
}
|
2017-06-30 16:41:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
fh->send_mode = LIRC_MODE_PULSE;
|
|
|
|
fh->rc = dev;
|
2017-05-02 00:04:21 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (dev->driver_type == RC_DRIVER_SCANCODE)
|
|
|
|
fh->rec_mode = LIRC_MODE_SCANCODE;
|
|
|
|
else
|
|
|
|
fh->rec_mode = LIRC_MODE_MODE2;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
retval = rc_open(dev);
|
|
|
|
if (retval)
|
|
|
|
goto out_kfifo;
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
init_waitqueue_head(&fh->wait_poll);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
file->private_data = fh;
|
|
|
|
spin_lock_irqsave(&dev->lirc_fh_lock, flags);
|
|
|
|
list_add(&fh->list, &dev->lirc_fh);
|
|
|
|
spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
|
2017-08-04 22:12:03 +08:00
|
|
|
|
2019-03-27 04:51:19 +08:00
|
|
|
stream_open(inode, file);
|
2017-05-02 00:04:11 +08:00
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
return 0;
|
2017-11-03 05:21:13 +08:00
|
|
|
out_kfifo:
|
|
|
|
if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
|
|
|
|
kfifo_free(&fh->scancodes);
|
|
|
|
out_rawir:
|
|
|
|
if (dev->driver_type == RC_DRIVER_IR_RAW)
|
|
|
|
kfifo_free(&fh->rawir);
|
|
|
|
out_fh:
|
|
|
|
kfree(fh);
|
|
|
|
put_device(&dev->dev);
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
return retval;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static int lirc_close(struct inode *inode, struct file *file)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = file->private_data;
|
|
|
|
struct rc_dev *dev = fh->rc;
|
|
|
|
unsigned long flags;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_lock_irqsave(&dev->lirc_fh_lock, flags);
|
|
|
|
list_del(&fh->list);
|
|
|
|
spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (dev->driver_type == RC_DRIVER_IR_RAW)
|
|
|
|
kfifo_free(&fh->rawir);
|
|
|
|
if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
|
|
|
|
kfifo_free(&fh->scancodes);
|
|
|
|
kfree(fh);
|
2016-07-06 17:01:26 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
rc_close(dev);
|
2017-11-03 05:21:13 +08:00
|
|
|
put_device(&dev->dev);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
return 0;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static ssize_t lirc_transmit(struct file *file, const char __user *buf,
|
|
|
|
size_t n, loff_t *ppos)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = file->private_data;
|
|
|
|
struct rc_dev *dev = fh->rc;
|
2017-12-14 05:09:21 +08:00
|
|
|
unsigned int *txbuf;
|
2017-11-03 04:39:16 +08:00
|
|
|
struct ir_raw_event *raw = NULL;
|
2017-11-04 20:30:45 +08:00
|
|
|
ssize_t ret;
|
2017-11-03 04:39:16 +08:00
|
|
|
size_t count;
|
|
|
|
ktime_t start;
|
|
|
|
s64 towait;
|
|
|
|
unsigned int duration = 0; /* signal duration in us */
|
|
|
|
int i;
|
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = mutex_lock_interruptible(&dev->lock);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
if (!dev->registered) {
|
|
|
|
ret = -ENODEV;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (!dev->tx_ir) {
|
|
|
|
ret = -EINVAL;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-06-30 16:41:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (fh->send_mode == LIRC_MODE_SCANCODE) {
|
2017-11-03 04:39:16 +08:00
|
|
|
struct lirc_scancode scan;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
if (n != sizeof(scan)) {
|
|
|
|
ret = -EINVAL;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
[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 15:23:07 +08:00
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
if (copy_from_user(&scan, buf, sizeof(scan))) {
|
|
|
|
ret = -EFAULT;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2017-05-02 00:03:46 +08:00
|
|
|
|
2020-10-30 19:52:30 +08:00
|
|
|
if (scan.flags || scan.keycode || scan.timestamp ||
|
|
|
|
scan.rc_proto > RC_PROTO_MAX) {
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -EINVAL;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2020-01-18 00:46:36 +08:00
|
|
|
/* We only have encoders for 32-bit protocols. */
|
2017-11-03 04:39:16 +08:00
|
|
|
if (scan.scancode > U32_MAX ||
|
2017-11-04 20:30:45 +08:00
|
|
|
!rc_validate_scancode(scan.rc_proto, scan.scancode)) {
|
|
|
|
ret = -EINVAL;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2010-08-16 00:51:56 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL);
|
2017-11-04 20:30:45 +08:00
|
|
|
if (!raw) {
|
|
|
|
ret = -ENOMEM;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode,
|
|
|
|
raw, LIRCBUF_SIZE);
|
|
|
|
if (ret < 0)
|
2017-12-20 00:48:25 +08:00
|
|
|
goto out_kfree_raw;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
count = ret;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
|
|
|
|
if (!txbuf) {
|
|
|
|
ret = -ENOMEM;
|
2017-12-20 00:48:25 +08:00
|
|
|
goto out_kfree_raw;
|
2017-11-03 04:39:16 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
for (i = 0; i < count; i++)
|
2020-08-24 01:23:05 +08:00
|
|
|
txbuf[i] = raw[i].duration;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (dev->s_tx_carrier) {
|
|
|
|
int carrier = ir_raw_encode_carrier(scan.rc_proto);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (carrier > 0)
|
|
|
|
dev->s_tx_carrier(dev, carrier);
|
|
|
|
}
|
|
|
|
} else {
|
2017-11-04 20:30:45 +08:00
|
|
|
if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) {
|
|
|
|
ret = -EINVAL;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
count = n / sizeof(unsigned int);
|
2017-11-04 20:30:45 +08:00
|
|
|
if (count > LIRCBUF_SIZE || count % 2 == 0) {
|
|
|
|
ret = -EINVAL;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
txbuf = memdup_user(buf, n);
|
2017-11-04 20:30:45 +08:00
|
|
|
if (IS_ERR(txbuf)) {
|
|
|
|
ret = PTR_ERR(txbuf);
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_unlock;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2017-11-03 04:39:16 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
for (i = 0; i < count; i++) {
|
2020-08-24 01:23:05 +08:00
|
|
|
if (txbuf[i] > IR_MAX_DURATION - duration || !txbuf[i]) {
|
2017-11-03 04:39:16 +08:00
|
|
|
ret = -EINVAL;
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_kfree;
|
2017-11-03 04:39:16 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
duration += txbuf[i];
|
2017-06-25 20:32:36 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2018-02-12 22:00:28 +08:00
|
|
|
start = ktime_get();
|
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
ret = dev->tx_ir(dev, txbuf, count);
|
|
|
|
if (ret < 0)
|
2017-12-14 05:09:21 +08:00
|
|
|
goto out_kfree;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
2017-12-14 05:30:22 +08:00
|
|
|
kfree(txbuf);
|
|
|
|
kfree(raw);
|
|
|
|
mutex_unlock(&dev->lock);
|
|
|
|
|
2017-12-12 06:12:09 +08:00
|
|
|
/*
|
|
|
|
* The lircd gap calculation expects the write function to
|
|
|
|
* wait for the actual IR signal to be transmitted before
|
|
|
|
* returning.
|
|
|
|
*/
|
|
|
|
towait = ktime_us_delta(ktime_add_us(start, duration),
|
|
|
|
ktime_get());
|
|
|
|
if (towait > 0) {
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
schedule_timeout(usecs_to_jiffies(towait));
|
2017-11-03 04:39:16 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-12-14 05:30:22 +08:00
|
|
|
return n;
|
2017-12-14 05:09:21 +08:00
|
|
|
out_kfree:
|
2017-11-03 04:39:16 +08:00
|
|
|
kfree(txbuf);
|
2017-12-20 00:48:25 +08:00
|
|
|
out_kfree_raw:
|
2017-11-03 04:39:16 +08:00
|
|
|
kfree(raw);
|
2017-12-14 05:09:21 +08:00
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&dev->lock);
|
2010-07-03 12:06:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static long lirc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = file->private_data;
|
|
|
|
struct rc_dev *dev = fh->rc;
|
2017-11-03 04:39:16 +08:00
|
|
|
u32 __user *argp = (u32 __user *)(arg);
|
2017-11-04 20:30:45 +08:00
|
|
|
u32 val = 0;
|
|
|
|
int ret;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
if (_IOC_DIR(cmd) & _IOC_WRITE) {
|
|
|
|
ret = get_user(val, argp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = mutex_lock_interruptible(&dev->lock);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
if (!dev->registered) {
|
|
|
|
ret = -ENODEV;
|
2017-06-30 16:41:57 +08:00
|
|
|
goto out;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case LIRC_GET_FEATURES:
|
2017-11-03 04:39:16 +08:00
|
|
|
if (dev->driver_type == RC_DRIVER_SCANCODE)
|
|
|
|
val |= LIRC_CAN_REC_SCANCODE;
|
|
|
|
|
|
|
|
if (dev->driver_type == RC_DRIVER_IR_RAW) {
|
2017-12-29 03:58:26 +08:00
|
|
|
val |= LIRC_CAN_REC_MODE2;
|
2017-11-03 04:39:16 +08:00
|
|
|
if (dev->rx_resolution)
|
|
|
|
val |= LIRC_CAN_GET_REC_RESOLUTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->tx_ir) {
|
2017-12-29 03:58:26 +08:00
|
|
|
val |= LIRC_CAN_SEND_PULSE;
|
2017-11-03 04:39:16 +08:00
|
|
|
if (dev->s_tx_mask)
|
|
|
|
val |= LIRC_CAN_SET_TRANSMITTER_MASK;
|
|
|
|
if (dev->s_tx_carrier)
|
|
|
|
val |= LIRC_CAN_SET_SEND_CARRIER;
|
|
|
|
if (dev->s_tx_duty_cycle)
|
|
|
|
val |= LIRC_CAN_SET_SEND_DUTY_CYCLE;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (dev->s_rx_carrier_range)
|
|
|
|
val |= LIRC_CAN_SET_REC_CARRIER |
|
|
|
|
LIRC_CAN_SET_REC_CARRIER_RANGE;
|
|
|
|
|
2021-07-03 17:04:40 +08:00
|
|
|
if (dev->s_wideband_receiver)
|
2017-11-03 04:39:16 +08:00
|
|
|
val |= LIRC_CAN_USE_WIDEBAND_RECEIVER;
|
|
|
|
|
|
|
|
if (dev->s_carrier_report)
|
|
|
|
val |= LIRC_CAN_MEASURE_CARRIER;
|
|
|
|
|
|
|
|
if (dev->max_timeout)
|
|
|
|
val |= LIRC_CAN_SET_REC_TIMEOUT;
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
/* mode support */
|
|
|
|
case LIRC_GET_REC_MODE:
|
|
|
|
if (dev->driver_type == RC_DRIVER_IR_RAW_TX)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
|
|
|
val = fh->rec_mode;
|
2017-11-03 04:39:16 +08:00
|
|
|
break;
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
case LIRC_SET_REC_MODE:
|
2017-11-03 04:39:16 +08:00
|
|
|
switch (dev->driver_type) {
|
|
|
|
case RC_DRIVER_IR_RAW_TX:
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
case RC_DRIVER_SCANCODE:
|
|
|
|
if (val != LIRC_MODE_SCANCODE)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -EINVAL;
|
2017-11-03 04:39:16 +08:00
|
|
|
break;
|
|
|
|
case RC_DRIVER_IR_RAW:
|
|
|
|
if (!(val == LIRC_MODE_MODE2 ||
|
|
|
|
val == LIRC_MODE_SCANCODE))
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -EINVAL;
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
if (!ret)
|
|
|
|
fh->rec_mode = val;
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
case LIRC_GET_SEND_MODE:
|
|
|
|
if (!dev->tx_ir)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
|
|
|
val = fh->send_mode;
|
2017-11-03 04:39:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIRC_SET_SEND_MODE:
|
|
|
|
if (!dev->tx_ir)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE))
|
|
|
|
ret = -EINVAL;
|
|
|
|
else
|
|
|
|
fh->send_mode = val;
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
/* TX settings */
|
|
|
|
case LIRC_SET_TRANSMITTER_MASK:
|
|
|
|
if (!dev->s_tx_mask)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
|
|
|
ret = dev->s_tx_mask(dev, val);
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
case LIRC_SET_SEND_CARRIER:
|
|
|
|
if (!dev->s_tx_carrier)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
|
|
|
ret = dev->s_tx_carrier(dev, val);
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
case LIRC_SET_SEND_DUTY_CYCLE:
|
|
|
|
if (!dev->s_tx_duty_cycle)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else if (val <= 0 || val >= 100)
|
|
|
|
ret = -EINVAL;
|
|
|
|
else
|
|
|
|
ret = dev->s_tx_duty_cycle(dev, val);
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
/* RX settings */
|
|
|
|
case LIRC_SET_REC_CARRIER:
|
|
|
|
if (!dev->s_rx_carrier_range)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else if (val <= 0)
|
|
|
|
ret = -EINVAL;
|
|
|
|
else
|
|
|
|
ret = dev->s_rx_carrier_range(dev, fh->carrier_low,
|
|
|
|
val);
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
case LIRC_SET_REC_CARRIER_RANGE:
|
|
|
|
if (!dev->s_rx_carrier_range)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else if (val <= 0)
|
|
|
|
ret = -EINVAL;
|
|
|
|
else
|
|
|
|
fh->carrier_low = val;
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
case LIRC_GET_REC_RESOLUTION:
|
|
|
|
if (!dev->rx_resolution)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
2020-08-24 01:23:05 +08:00
|
|
|
val = dev->rx_resolution;
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
case LIRC_SET_WIDEBAND_RECEIVER:
|
2021-07-03 17:04:40 +08:00
|
|
|
if (!dev->s_wideband_receiver)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
2021-07-03 17:04:40 +08:00
|
|
|
ret = dev->s_wideband_receiver(dev, !!val);
|
2010-07-03 12:06:57 +08:00
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
case LIRC_SET_MEASURE_CARRIER_MODE:
|
|
|
|
if (!dev->s_carrier_report)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
|
|
|
ret = dev->s_carrier_report(dev, !!val);
|
|
|
|
break;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
/* Generic timeout support */
|
|
|
|
case LIRC_GET_MIN_TIMEOUT:
|
|
|
|
if (!dev->max_timeout)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
2020-08-24 01:23:05 +08:00
|
|
|
val = dev->min_timeout;
|
2017-11-03 04:39:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIRC_GET_MAX_TIMEOUT:
|
|
|
|
if (!dev->max_timeout)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
|
|
|
else
|
2020-08-24 01:23:05 +08:00
|
|
|
val = dev->max_timeout;
|
2017-11-03 04:39:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LIRC_SET_REC_TIMEOUT:
|
2017-11-04 20:30:45 +08:00
|
|
|
if (!dev->max_timeout) {
|
|
|
|
ret = -ENOTTY;
|
|
|
|
} else {
|
2020-08-24 01:23:05 +08:00
|
|
|
if (val < dev->min_timeout || val > dev->max_timeout)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
else if (dev->s_timeout)
|
2020-08-24 01:23:05 +08:00
|
|
|
ret = dev->s_timeout(dev, val);
|
2018-02-12 21:59:00 +08:00
|
|
|
else
|
2020-08-24 01:23:05 +08:00
|
|
|
dev->timeout = val;
|
2017-11-04 20:30:45 +08:00
|
|
|
}
|
2017-11-03 04:39:16 +08:00
|
|
|
break;
|
|
|
|
|
2018-03-24 04:59:52 +08:00
|
|
|
case LIRC_GET_REC_TIMEOUT:
|
2017-11-03 04:39:16 +08:00
|
|
|
if (!dev->timeout)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
2018-03-24 04:59:52 +08:00
|
|
|
else
|
2020-08-24 01:23:05 +08:00
|
|
|
val = dev->timeout;
|
2018-03-24 04:59:52 +08:00
|
|
|
break;
|
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
case LIRC_SET_REC_TIMEOUT_REPORTS:
|
2018-03-24 20:02:48 +08:00
|
|
|
if (dev->driver_type != RC_DRIVER_IR_RAW)
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
2017-11-03 04:39:16 +08:00
|
|
|
break;
|
|
|
|
|
2010-07-03 12:06:57 +08:00
|
|
|
default:
|
2017-11-04 20:30:45 +08:00
|
|
|
ret = -ENOTTY;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 20:30:45 +08:00
|
|
|
if (!ret && _IOC_DIR(cmd) & _IOC_READ)
|
2017-11-03 04:39:16 +08:00
|
|
|
ret = put_user(val, argp);
|
|
|
|
|
2017-06-30 16:41:57 +08:00
|
|
|
out:
|
2017-11-04 20:30:45 +08:00
|
|
|
mutex_unlock(&dev->lock);
|
2017-11-03 04:39:16 +08:00
|
|
|
return ret;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static __poll_t lirc_poll(struct file *file, struct poll_table_struct *wait)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = file->private_data;
|
|
|
|
struct rc_dev *rcdev = fh->rc;
|
2018-02-07 03:27:48 +08:00
|
|
|
__poll_t events = 0;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
poll_wait(file, &fh->wait_poll, wait);
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
if (!rcdev->registered) {
|
2018-02-12 06:34:03 +08:00
|
|
|
events = EPOLLHUP | EPOLLERR;
|
2017-11-03 04:39:16 +08:00
|
|
|
} else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) {
|
2017-11-03 05:21:13 +08:00
|
|
|
if (fh->rec_mode == LIRC_MODE_SCANCODE &&
|
|
|
|
!kfifo_is_empty(&fh->scancodes))
|
2018-02-12 06:34:03 +08:00
|
|
|
events = EPOLLIN | EPOLLRDNORM;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (fh->rec_mode == LIRC_MODE_MODE2 &&
|
|
|
|
!kfifo_is_empty(&fh->rawir))
|
2018-02-12 06:34:03 +08:00
|
|
|
events = EPOLLIN | EPOLLRDNORM;
|
2017-11-03 04:39:16 +08:00
|
|
|
}
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
return events;
|
|
|
|
}
|
2010-10-18 23:02:01 +08:00
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static ssize_t lirc_read_mode2(struct file *file, char __user *buffer,
|
|
|
|
size_t length)
|
2017-11-03 04:39:16 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = file->private_data;
|
|
|
|
struct rc_dev *rcdev = fh->rc;
|
2017-11-03 04:39:16 +08:00
|
|
|
unsigned int copied;
|
|
|
|
int ret;
|
2017-06-25 20:32:36 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (length < sizeof(unsigned int) || length % sizeof(unsigned int))
|
|
|
|
return -EINVAL;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
do {
|
2017-11-03 05:21:13 +08:00
|
|
|
if (kfifo_is_empty(&fh->rawir)) {
|
2017-11-03 04:39:16 +08:00
|
|
|
if (file->f_flags & O_NONBLOCK)
|
|
|
|
return -EAGAIN;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
ret = wait_event_interruptible(fh->wait_poll,
|
|
|
|
!kfifo_is_empty(&fh->rawir) ||
|
2017-11-03 04:39:16 +08:00
|
|
|
!rcdev->registered);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (!rcdev->registered)
|
|
|
|
return -ENODEV;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
ret = mutex_lock_interruptible(&rcdev->lock);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-11-03 05:21:13 +08:00
|
|
|
ret = kfifo_to_user(&fh->rawir, buffer, length, &copied);
|
2017-11-03 04:39:16 +08:00
|
|
|
mutex_unlock(&rcdev->lock);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} while (copied == 0);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static ssize_t lirc_read_scancode(struct file *file, char __user *buffer,
|
|
|
|
size_t length)
|
2017-11-03 04:39:16 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = file->private_data;
|
|
|
|
struct rc_dev *rcdev = fh->rc;
|
2017-11-03 04:39:16 +08:00
|
|
|
unsigned int copied;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (length < sizeof(struct lirc_scancode) ||
|
|
|
|
length % sizeof(struct lirc_scancode))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
do {
|
2017-11-03 05:21:13 +08:00
|
|
|
if (kfifo_is_empty(&fh->scancodes)) {
|
2017-11-03 04:39:16 +08:00
|
|
|
if (file->f_flags & O_NONBLOCK)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
ret = wait_event_interruptible(fh->wait_poll,
|
|
|
|
!kfifo_is_empty(&fh->scancodes) ||
|
2017-11-03 04:39:16 +08:00
|
|
|
!rcdev->registered);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (!rcdev->registered)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
ret = mutex_lock_interruptible(&rcdev->lock);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-11-03 05:21:13 +08:00
|
|
|
ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied);
|
2017-11-03 04:39:16 +08:00
|
|
|
mutex_unlock(&rcdev->lock);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} while (copied == 0);
|
|
|
|
|
|
|
|
return copied;
|
|
|
|
}
|
2010-11-17 13:20:15 +08:00
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
static ssize_t lirc_read(struct file *file, char __user *buffer, size_t length,
|
|
|
|
loff_t *ppos)
|
2017-11-03 04:39:16 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
struct lirc_fh *fh = file->private_data;
|
|
|
|
struct rc_dev *rcdev = fh->rc;
|
2017-11-03 04:39:16 +08:00
|
|
|
|
|
|
|
if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX)
|
|
|
|
return -EINVAL;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
if (!rcdev->registered)
|
|
|
|
return -ENODEV;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
if (fh->rec_mode == LIRC_MODE_MODE2)
|
2020-08-25 03:10:45 +08:00
|
|
|
return lirc_read_mode2(file, buffer, length);
|
2017-11-03 04:39:16 +08:00
|
|
|
else /* LIRC_MODE_SCANCODE */
|
2020-08-25 03:10:45 +08:00
|
|
|
return lirc_read_scancode(file, buffer, length);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2017-11-03 04:39:16 +08:00
|
|
|
static const struct file_operations lirc_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
2020-08-25 03:10:45 +08:00
|
|
|
.write = lirc_transmit,
|
|
|
|
.unlocked_ioctl = lirc_ioctl,
|
2018-09-12 03:59:08 +08:00
|
|
|
.compat_ioctl = compat_ptr_ioctl,
|
2020-08-25 03:10:45 +08:00
|
|
|
.read = lirc_read,
|
|
|
|
.poll = lirc_poll,
|
|
|
|
.open = lirc_open,
|
|
|
|
.release = lirc_close,
|
2017-11-03 04:39:16 +08:00
|
|
|
.llseek = no_llseek,
|
|
|
|
};
|
|
|
|
|
2017-06-25 20:32:36 +08:00
|
|
|
static void lirc_release_device(struct device *ld)
|
2017-06-25 20:31:40 +08:00
|
|
|
{
|
2017-09-26 21:34:47 +08:00
|
|
|
struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
|
2017-06-25 20:31:40 +08:00
|
|
|
|
2017-09-26 21:34:47 +08:00
|
|
|
put_device(&rcdev->dev);
|
2017-06-25 20:31:40 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
int lirc_register(struct rc_dev *dev)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2018-03-27 23:24:05 +08:00
|
|
|
const char *rx_type, *tx_type;
|
2017-09-26 21:34:47 +08:00
|
|
|
int err, minor;
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-26 21:34:47 +08:00
|
|
|
minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
|
2017-11-03 05:21:13 +08:00
|
|
|
if (minor < 0)
|
|
|
|
return minor;
|
2017-06-30 16:41:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
device_initialize(&dev->lirc_dev);
|
|
|
|
dev->lirc_dev.class = lirc_class;
|
2017-09-26 21:34:47 +08:00
|
|
|
dev->lirc_dev.parent = &dev->dev;
|
2017-11-03 05:21:13 +08:00
|
|
|
dev->lirc_dev.release = lirc_release_device;
|
2017-09-26 21:34:47 +08:00
|
|
|
dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
|
|
|
|
dev_set_name(&dev->lirc_dev, "lirc%d", minor);
|
2017-06-25 20:31:40 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
INIT_LIST_HEAD(&dev->lirc_fh);
|
|
|
|
spin_lock_init(&dev->lirc_fh_lock);
|
|
|
|
|
2017-09-26 21:34:47 +08:00
|
|
|
cdev_init(&dev->lirc_cdev, &lirc_fops);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-26 21:34:47 +08:00
|
|
|
err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
|
|
|
|
if (err)
|
|
|
|
goto out_ida;
|
2017-01-30 23:49:58 +08:00
|
|
|
|
2017-09-26 21:34:47 +08:00
|
|
|
get_device(&dev->dev);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2018-03-27 23:24:05 +08:00
|
|
|
switch (dev->driver_type) {
|
|
|
|
case RC_DRIVER_SCANCODE:
|
|
|
|
rx_type = "scancode";
|
|
|
|
break;
|
|
|
|
case RC_DRIVER_IR_RAW:
|
|
|
|
rx_type = "raw IR";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rx_type = "no";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->tx_ir)
|
|
|
|
tx_type = "raw IR";
|
|
|
|
else
|
|
|
|
tx_type = "no";
|
|
|
|
|
|
|
|
dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d, %s receiver, %s transmitter",
|
|
|
|
dev->driver_name, minor, rx_type, tx_type);
|
2017-05-02 00:04:11 +08:00
|
|
|
|
2017-06-25 20:31:24 +08:00
|
|
|
return 0;
|
2017-09-26 21:34:47 +08:00
|
|
|
|
|
|
|
out_ida:
|
|
|
|
ida_simple_remove(&lirc_ida, minor);
|
|
|
|
return err;
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 03:10:45 +08:00
|
|
|
void lirc_unregister(struct rc_dev *dev)
|
2010-07-03 12:06:57 +08:00
|
|
|
{
|
2017-11-03 05:21:13 +08:00
|
|
|
unsigned long flags;
|
|
|
|
struct lirc_fh *fh;
|
|
|
|
|
2017-09-26 21:34:47 +08:00
|
|
|
dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
|
|
|
|
dev->driver_name, MINOR(dev->lirc_dev.devt));
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_lock_irqsave(&dev->lirc_fh_lock, flags);
|
|
|
|
list_for_each_entry(fh, &dev->lirc_fh, list)
|
2018-02-12 06:34:03 +08:00
|
|
|
wake_up_poll(&fh->wait_poll, EPOLLHUP | EPOLLERR);
|
2017-11-03 05:21:13 +08:00
|
|
|
spin_unlock_irqrestore(&dev->lirc_fh_lock, flags);
|
2010-07-03 12:06:57 +08:00
|
|
|
|
2017-09-26 21:34:47 +08:00
|
|
|
cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
|
|
|
|
ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
|
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
|
|
|
}
|
|
|
|
|
2020-08-23 20:32:41 +08:00
|
|
|
retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, "lirc");
|
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
|
|
|
}
|
|
|
|
|
2018-02-13 19:11:35 +08:00
|
|
|
pr_debug("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-09-26 21:34:47 +08:00
|
|
|
unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
|
2010-07-03 12:06:57 +08:00
|
|
|
}
|
|
|
|
|
2018-05-27 19:24:09 +08:00
|
|
|
struct rc_dev *rc_dev_get_from_fd(int fd)
|
|
|
|
{
|
|
|
|
struct fd f = fdget(fd);
|
|
|
|
struct lirc_fh *fh;
|
|
|
|
struct rc_dev *dev;
|
|
|
|
|
|
|
|
if (!f.file)
|
|
|
|
return ERR_PTR(-EBADF);
|
|
|
|
|
|
|
|
if (f.file->f_op != &lirc_fops) {
|
|
|
|
fdput(f);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
fh = f.file->private_data;
|
|
|
|
dev = fh->rc;
|
|
|
|
|
|
|
|
get_device(&dev->dev);
|
|
|
|
fdput(f);
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2017-12-29 03:45:12 +08:00
|
|
|
MODULE_ALIAS("lirc_dev");
|