2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-08-18 17:06:26 +00:00
|
|
|
/* The industrial I/O core, trigger handling functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Jonathan Cameron
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/idr.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/list.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2009-08-18 17:06:26 +00:00
|
|
|
|
2012-04-25 14:54:58 +00:00
|
|
|
#include <linux/iio/iio.h>
|
|
|
|
#include <linux/iio/trigger.h>
|
2011-08-12 16:56:03 +00:00
|
|
|
#include "iio_core.h"
|
2011-08-24 16:28:38 +00:00
|
|
|
#include "iio_core_trigger.h"
|
2012-04-25 14:54:58 +00:00
|
|
|
#include <linux/iio/trigger_consumer.h>
|
2009-08-18 17:06:26 +00:00
|
|
|
|
|
|
|
/* RFC - Question of approach
|
|
|
|
* Make the common case (single sensor single trigger)
|
|
|
|
* simple by starting trigger capture from when first sensors
|
|
|
|
* is added.
|
|
|
|
*
|
|
|
|
* Complex simultaneous start requires use of 'hold' functionality
|
|
|
|
* of the trigger. (not implemented)
|
|
|
|
*
|
|
|
|
* Any other suggestions?
|
|
|
|
*/
|
|
|
|
|
2011-08-30 11:41:07 +00:00
|
|
|
static DEFINE_IDA(iio_trigger_ida);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
|
|
|
/* Single list of all available triggers */
|
|
|
|
static LIST_HEAD(iio_trigger_list);
|
|
|
|
static DEFINE_MUTEX(iio_trigger_list_lock);
|
|
|
|
|
2011-05-18 13:42:22 +00:00
|
|
|
/**
|
|
|
|
* iio_trigger_read_name() - retrieve useful identifying name
|
2015-08-06 11:56:02 +00:00
|
|
|
* @dev: device associated with the iio_trigger
|
|
|
|
* @attr: pointer to the device_attribute structure that is
|
|
|
|
* being processed
|
|
|
|
* @buf: buffer to print the name into
|
|
|
|
*
|
|
|
|
* Return: a negative number on failure or the number of written
|
|
|
|
* characters on success.
|
|
|
|
*/
|
2011-05-18 13:42:22 +00:00
|
|
|
static ssize_t iio_trigger_read_name(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2012-06-21 17:11:00 +00:00
|
|
|
struct iio_trigger *trig = to_iio_trigger(dev);
|
2011-05-18 13:42:22 +00:00
|
|
|
return sprintf(buf, "%s\n", trig->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
|
|
|
|
|
2012-07-05 08:57:06 +00:00
|
|
|
static struct attribute *iio_trig_dev_attrs[] = {
|
|
|
|
&dev_attr_name.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
2013-12-02 02:57:00 +00:00
|
|
|
ATTRIBUTE_GROUPS(iio_trig_dev);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
2016-05-23 18:40:01 +00:00
|
|
|
static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
|
|
|
|
|
2017-07-23 16:25:44 +00:00
|
|
|
int __iio_trigger_register(struct iio_trigger *trig_info,
|
|
|
|
struct module *this_mod)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2017-07-23 16:25:44 +00:00
|
|
|
trig_info->owner = this_mod;
|
|
|
|
|
2011-08-30 11:41:07 +00:00
|
|
|
trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
|
2014-02-16 11:53:00 +00:00
|
|
|
if (trig_info->id < 0)
|
|
|
|
return trig_info->id;
|
|
|
|
|
2009-08-18 17:06:26 +00:00
|
|
|
/* Set the name used for the sysfs directory etc */
|
|
|
|
dev_set_name(&trig_info->dev, "trigger%ld",
|
|
|
|
(unsigned long) trig_info->id);
|
|
|
|
|
|
|
|
ret = device_add(&trig_info->dev);
|
|
|
|
if (ret)
|
|
|
|
goto error_unregister_id;
|
|
|
|
|
|
|
|
/* Add to list of available triggers held by the IIO core */
|
|
|
|
mutex_lock(&iio_trigger_list_lock);
|
2016-05-23 18:40:01 +00:00
|
|
|
if (__iio_trigger_find_by_name(trig_info->name)) {
|
|
|
|
pr_err("Duplicate trigger name '%s'\n", trig_info->name);
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto error_device_del;
|
|
|
|
}
|
2009-08-18 17:06:26 +00:00
|
|
|
list_add_tail(&trig_info->list, &iio_trigger_list);
|
|
|
|
mutex_unlock(&iio_trigger_list_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2016-05-23 18:40:01 +00:00
|
|
|
error_device_del:
|
|
|
|
mutex_unlock(&iio_trigger_list_lock);
|
|
|
|
device_del(&trig_info->dev);
|
2009-08-18 17:06:26 +00:00
|
|
|
error_unregister_id:
|
2011-08-30 11:41:07 +00:00
|
|
|
ida_simple_remove(&iio_trigger_ida, trig_info->id);
|
2009-08-18 17:06:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2017-07-23 16:25:44 +00:00
|
|
|
EXPORT_SYMBOL(__iio_trigger_register);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
|
|
|
void iio_trigger_unregister(struct iio_trigger *trig_info)
|
|
|
|
{
|
|
|
|
mutex_lock(&iio_trigger_list_lock);
|
2011-04-15 17:55:54 +00:00
|
|
|
list_del(&trig_info->list);
|
2009-08-18 17:06:26 +00:00
|
|
|
mutex_unlock(&iio_trigger_list_lock);
|
|
|
|
|
2011-08-30 11:41:07 +00:00
|
|
|
ida_simple_remove(&iio_trigger_ida, trig_info->id);
|
2009-08-18 17:06:26 +00:00
|
|
|
/* Possible issue in here */
|
2013-06-22 11:00:04 +00:00
|
|
|
device_del(&trig_info->dev);
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_unregister);
|
|
|
|
|
2016-09-03 06:36:15 +00:00
|
|
|
int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
|
|
|
|
{
|
|
|
|
if (!indio_dev || !trig)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&indio_dev->mlock);
|
|
|
|
WARN_ON(indio_dev->trig_readonly);
|
|
|
|
|
|
|
|
indio_dev->trig = iio_trigger_get(trig);
|
|
|
|
indio_dev->trig_readonly = true;
|
|
|
|
mutex_unlock(&indio_dev->mlock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_set_immutable);
|
|
|
|
|
2016-05-23 18:40:01 +00:00
|
|
|
/* Search for trigger by name, assuming iio_trigger_list_lock held */
|
|
|
|
static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
|
|
|
|
{
|
|
|
|
struct iio_trigger *iter;
|
|
|
|
|
|
|
|
list_for_each_entry(iter, &iio_trigger_list, list)
|
|
|
|
if (!strcmp(iter->name, name))
|
|
|
|
return iter;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-22 03:28:52 +00:00
|
|
|
static struct iio_trigger *iio_trigger_acquire_by_name(const char *name)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
2011-04-15 17:55:53 +00:00
|
|
|
struct iio_trigger *trig = NULL, *iter;
|
2010-04-26 08:49:10 +00:00
|
|
|
|
2009-08-18 17:06:26 +00:00
|
|
|
mutex_lock(&iio_trigger_list_lock);
|
2011-04-15 17:55:53 +00:00
|
|
|
list_for_each_entry(iter, &iio_trigger_list, list)
|
|
|
|
if (sysfs_streq(iter->name, name)) {
|
|
|
|
trig = iter;
|
2017-01-22 03:28:52 +00:00
|
|
|
iio_trigger_get(trig);
|
2009-08-18 17:06:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&iio_trigger_list_lock);
|
|
|
|
|
2011-04-15 17:55:53 +00:00
|
|
|
return trig;
|
2010-05-04 21:20:54 +00:00
|
|
|
}
|
2009-08-18 17:06:26 +00:00
|
|
|
|
2014-12-06 06:46:00 +00:00
|
|
|
void iio_trigger_poll(struct iio_trigger *trig)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
2011-05-18 13:41:18 +00:00
|
|
|
int i;
|
2013-07-16 14:28:00 +00:00
|
|
|
|
|
|
|
if (!atomic_read(&trig->use_count)) {
|
|
|
|
atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
|
|
|
|
if (trig->subirqs[i].enabled)
|
2011-05-18 13:41:18 +00:00
|
|
|
generic_handle_irq(trig->subirq_base + i);
|
2013-07-16 14:28:00 +00:00
|
|
|
else
|
|
|
|
iio_trigger_notify_done(trig);
|
|
|
|
}
|
|
|
|
}
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_poll);
|
2011-05-18 13:41:21 +00:00
|
|
|
|
|
|
|
irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
|
|
|
|
{
|
2014-12-06 06:46:00 +00:00
|
|
|
iio_trigger_poll(private);
|
2011-05-18 13:41:21 +00:00
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
2014-12-06 06:46:00 +00:00
|
|
|
void iio_trigger_poll_chained(struct iio_trigger *trig)
|
2011-05-18 13:42:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
2013-07-16 14:28:00 +00:00
|
|
|
|
|
|
|
if (!atomic_read(&trig->use_count)) {
|
|
|
|
atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
|
|
|
|
if (trig->subirqs[i].enabled)
|
2011-05-18 13:42:11 +00:00
|
|
|
handle_nested_irq(trig->subirq_base + i);
|
2013-07-16 14:28:00 +00:00
|
|
|
else
|
|
|
|
iio_trigger_notify_done(trig);
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 13:42:11 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_poll_chained);
|
|
|
|
|
2009-08-18 17:06:26 +00:00
|
|
|
void iio_trigger_notify_done(struct iio_trigger *trig)
|
|
|
|
{
|
2017-07-23 16:25:45 +00:00
|
|
|
if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
|
|
|
|
trig->ops->try_reenable)
|
2011-10-26 16:27:39 +00:00
|
|
|
if (trig->ops->try_reenable(trig))
|
2013-02-04 11:36:00 +00:00
|
|
|
/* Missed an interrupt so launch new poll now */
|
2014-12-06 06:46:00 +00:00
|
|
|
iio_trigger_poll(trig);
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_notify_done);
|
|
|
|
|
|
|
|
/* Trigger Consumer related functions */
|
2011-08-24 16:28:41 +00:00
|
|
|
static int iio_trigger_get_irq(struct iio_trigger *trig)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mutex_lock(&trig->pool_lock);
|
|
|
|
ret = bitmap_find_free_region(trig->pool,
|
|
|
|
CONFIG_IIO_CONSUMERS_PER_TRIGGER,
|
|
|
|
ilog2(1));
|
|
|
|
mutex_unlock(&trig->pool_lock);
|
|
|
|
if (ret >= 0)
|
|
|
|
ret += trig->subirq_base;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
|
|
|
|
{
|
|
|
|
mutex_lock(&trig->pool_lock);
|
|
|
|
clear_bit(irq - trig->subirq_base, trig->pool);
|
|
|
|
mutex_unlock(&trig->pool_lock);
|
|
|
|
}
|
2009-08-18 17:06:26 +00:00
|
|
|
|
|
|
|
/* Complexity in here. With certain triggers (datardy) an acknowledgement
|
|
|
|
* may be needed if the pollfuncs do not include the data read for the
|
|
|
|
* triggering device.
|
|
|
|
* This is not currently handled. Alternative of not enabling trigger unless
|
|
|
|
* the relevant function is in there may be the best option.
|
|
|
|
*/
|
2013-02-04 11:36:00 +00:00
|
|
|
/* Worth protecting against double additions? */
|
2020-05-25 11:38:53 +00:00
|
|
|
int iio_trigger_attach_poll_func(struct iio_trigger *trig,
|
|
|
|
struct iio_poll_func *pf)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2011-05-18 13:41:42 +00:00
|
|
|
bool notinuse
|
|
|
|
= bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
|
|
|
|
|
2013-02-04 11:36:00 +00:00
|
|
|
/* Prevent the module from being removed whilst attached to a trigger */
|
2017-07-23 16:25:43 +00:00
|
|
|
__module_get(pf->indio_dev->driver_module);
|
2016-05-03 12:27:09 +00:00
|
|
|
|
|
|
|
/* Get irq number */
|
2011-05-18 13:41:42 +00:00
|
|
|
pf->irq = iio_trigger_get_irq(trig);
|
2019-02-20 16:49:10 +00:00
|
|
|
if (pf->irq < 0) {
|
|
|
|
pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
|
|
|
|
trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
|
2016-05-03 12:27:09 +00:00
|
|
|
goto out_put_module;
|
2019-02-20 16:49:10 +00:00
|
|
|
}
|
2016-05-03 12:27:09 +00:00
|
|
|
|
|
|
|
/* Request irq */
|
2011-05-18 13:41:42 +00:00
|
|
|
ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
|
|
|
|
pf->type, pf->name,
|
|
|
|
pf);
|
2016-05-03 12:27:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto out_put_irq;
|
2011-10-26 16:27:41 +00:00
|
|
|
|
2016-05-03 12:27:09 +00:00
|
|
|
/* Enable trigger in driver */
|
2017-07-23 16:25:45 +00:00
|
|
|
if (trig->ops && trig->ops->set_trigger_state && notinuse) {
|
2011-08-12 16:08:38 +00:00
|
|
|
ret = trig->ops->set_trigger_state(trig, true);
|
2011-10-26 16:27:41 +00:00
|
|
|
if (ret < 0)
|
2016-05-03 12:27:09 +00:00
|
|
|
goto out_free_irq;
|
2011-10-26 16:27:41 +00:00
|
|
|
}
|
2011-05-18 13:41:18 +00:00
|
|
|
|
2016-09-01 08:27:17 +00:00
|
|
|
/*
|
|
|
|
* Check if we just registered to our own trigger: we determine that
|
|
|
|
* this is the case if the IIO device and the trigger device share the
|
|
|
|
* same parent device.
|
|
|
|
*/
|
|
|
|
if (pf->indio_dev->dev.parent == trig->dev.parent)
|
|
|
|
trig->attached_own_device = true;
|
|
|
|
|
2009-08-18 17:06:26 +00:00
|
|
|
return ret;
|
2016-05-03 12:27:09 +00:00
|
|
|
|
|
|
|
out_free_irq:
|
|
|
|
free_irq(pf->irq, pf);
|
|
|
|
out_put_irq:
|
|
|
|
iio_trigger_put_irq(trig, pf->irq);
|
|
|
|
out_put_module:
|
2017-07-23 16:25:43 +00:00
|
|
|
module_put(pf->indio_dev->driver_module);
|
2016-05-03 12:27:09 +00:00
|
|
|
return ret;
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 11:38:53 +00:00
|
|
|
int iio_trigger_detach_poll_func(struct iio_trigger *trig,
|
|
|
|
struct iio_poll_func *pf)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
2011-05-18 13:41:42 +00:00
|
|
|
int ret = 0;
|
|
|
|
bool no_other_users
|
|
|
|
= (bitmap_weight(trig->pool,
|
|
|
|
CONFIG_IIO_CONSUMERS_PER_TRIGGER)
|
|
|
|
== 1);
|
2017-07-23 16:25:45 +00:00
|
|
|
if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
|
2011-08-12 16:08:38 +00:00
|
|
|
ret = trig->ops->set_trigger_state(trig, false);
|
2011-05-18 13:41:42 +00:00
|
|
|
if (ret)
|
2014-02-16 11:53:00 +00:00
|
|
|
return ret;
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
2016-09-01 08:27:17 +00:00
|
|
|
if (pf->indio_dev->dev.parent == trig->dev.parent)
|
|
|
|
trig->attached_own_device = false;
|
2011-05-18 13:41:42 +00:00
|
|
|
iio_trigger_put_irq(trig, pf->irq);
|
|
|
|
free_irq(pf->irq, pf);
|
2017-07-23 16:25:43 +00:00
|
|
|
module_put(pf->indio_dev->driver_module);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-18 13:41:18 +00:00
|
|
|
irqreturn_t iio_pollfunc_store_time(int irq, void *p)
|
|
|
|
{
|
|
|
|
struct iio_poll_func *pf = p;
|
2016-03-09 18:05:49 +00:00
|
|
|
pf->timestamp = iio_get_time_ns(pf->indio_dev);
|
2011-05-18 13:41:18 +00:00
|
|
|
return IRQ_WAKE_THREAD;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_pollfunc_store_time);
|
|
|
|
|
2011-05-18 13:42:32 +00:00
|
|
|
struct iio_poll_func
|
|
|
|
*iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
|
|
|
|
irqreturn_t (*thread)(int irq, void *p),
|
|
|
|
int type,
|
2011-08-24 16:28:36 +00:00
|
|
|
struct iio_dev *indio_dev,
|
2011-05-18 13:42:32 +00:00
|
|
|
const char *fmt,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list vargs;
|
|
|
|
struct iio_poll_func *pf;
|
|
|
|
|
|
|
|
pf = kmalloc(sizeof *pf, GFP_KERNEL);
|
|
|
|
if (pf == NULL)
|
|
|
|
return NULL;
|
|
|
|
va_start(vargs, fmt);
|
|
|
|
pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
if (pf->name == NULL) {
|
|
|
|
kfree(pf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pf->h = h;
|
|
|
|
pf->thread = thread;
|
|
|
|
pf->type = type;
|
2011-08-24 16:28:36 +00:00
|
|
|
pf->indio_dev = indio_dev;
|
2011-05-18 13:42:32 +00:00
|
|
|
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
|
|
|
|
|
|
|
|
void iio_dealloc_pollfunc(struct iio_poll_func *pf)
|
|
|
|
{
|
|
|
|
kfree(pf->name);
|
|
|
|
kfree(pf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
|
|
|
|
|
2009-08-18 17:06:26 +00:00
|
|
|
/**
|
2013-02-04 11:36:00 +00:00
|
|
|
* iio_trigger_read_current() - trigger consumer sysfs query current trigger
|
2015-08-06 11:56:02 +00:00
|
|
|
* @dev: device associated with an industrial I/O device
|
|
|
|
* @attr: pointer to the device_attribute structure that
|
|
|
|
* is being processed
|
|
|
|
* @buf: buffer where the current trigger name will be printed into
|
2009-08-18 17:06:26 +00:00
|
|
|
*
|
|
|
|
* For trigger consumers the current_trigger interface allows the trigger
|
|
|
|
* used by the device to be queried.
|
2015-08-06 11:56:02 +00:00
|
|
|
*
|
|
|
|
* Return: a negative number on failure, the number of characters written
|
|
|
|
* on success or 0 if no trigger is available
|
|
|
|
*/
|
2009-08-18 17:06:26 +00:00
|
|
|
static ssize_t iio_trigger_read_current(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2012-05-12 13:39:33 +00:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2011-08-24 16:28:40 +00:00
|
|
|
|
2011-10-06 16:14:35 +00:00
|
|
|
if (indio_dev->trig)
|
|
|
|
return sprintf(buf, "%s\n", indio_dev->trig->name);
|
2011-08-24 16:28:40 +00:00
|
|
|
return 0;
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-04 11:36:00 +00:00
|
|
|
* iio_trigger_write_current() - trigger consumer sysfs set current trigger
|
2015-08-06 11:56:02 +00:00
|
|
|
* @dev: device associated with an industrial I/O device
|
|
|
|
* @attr: device attribute that is being processed
|
|
|
|
* @buf: string buffer that holds the name of the trigger
|
|
|
|
* @len: length of the trigger name held by buf
|
2009-08-18 17:06:26 +00:00
|
|
|
*
|
|
|
|
* For trigger consumers the current_trigger interface allows the trigger
|
2013-11-12 21:49:00 +00:00
|
|
|
* used for this device to be specified at run time based on the trigger's
|
2009-08-18 17:06:26 +00:00
|
|
|
* name.
|
2015-08-06 11:56:02 +00:00
|
|
|
*
|
|
|
|
* Return: negative error code on failure or length of the buffer
|
|
|
|
* on success
|
|
|
|
*/
|
2009-08-18 17:06:26 +00:00
|
|
|
static ssize_t iio_trigger_write_current(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
2012-05-12 13:39:33 +00:00
|
|
|
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
|
2011-10-06 16:14:35 +00:00
|
|
|
struct iio_trigger *oldtrig = indio_dev->trig;
|
2011-06-27 12:07:09 +00:00
|
|
|
struct iio_trigger *trig;
|
|
|
|
int ret;
|
|
|
|
|
2011-10-06 16:14:35 +00:00
|
|
|
mutex_lock(&indio_dev->mlock);
|
|
|
|
if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
|
|
|
|
mutex_unlock(&indio_dev->mlock);
|
2009-08-18 17:06:26 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
2016-09-03 06:36:15 +00:00
|
|
|
if (indio_dev->trig_readonly) {
|
|
|
|
mutex_unlock(&indio_dev->mlock);
|
|
|
|
return -EPERM;
|
|
|
|
}
|
2011-10-06 16:14:35 +00:00
|
|
|
mutex_unlock(&indio_dev->mlock);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
2017-01-22 03:28:52 +00:00
|
|
|
trig = iio_trigger_acquire_by_name(buf);
|
|
|
|
if (oldtrig == trig) {
|
|
|
|
ret = len;
|
|
|
|
goto out_trigger_put;
|
|
|
|
}
|
2011-06-27 12:07:09 +00:00
|
|
|
|
2011-10-06 16:14:35 +00:00
|
|
|
if (trig && indio_dev->info->validate_trigger) {
|
|
|
|
ret = indio_dev->info->validate_trigger(indio_dev, trig);
|
2011-06-27 12:07:09 +00:00
|
|
|
if (ret)
|
2017-01-22 03:28:52 +00:00
|
|
|
goto out_trigger_put;
|
2011-06-27 12:07:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 16:25:45 +00:00
|
|
|
if (trig && trig->ops && trig->ops->validate_device) {
|
2011-10-06 16:14:35 +00:00
|
|
|
ret = trig->ops->validate_device(trig, indio_dev);
|
2011-06-27 12:07:09 +00:00
|
|
|
if (ret)
|
2017-01-22 03:28:52 +00:00
|
|
|
goto out_trigger_put;
|
2011-06-27 12:07:09 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 16:14:35 +00:00
|
|
|
indio_dev->trig = trig;
|
2011-06-27 12:07:09 +00:00
|
|
|
|
2015-08-20 19:37:39 +00:00
|
|
|
if (oldtrig) {
|
|
|
|
if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
|
|
|
|
iio_trigger_detach_poll_func(oldtrig,
|
|
|
|
indio_dev->pollfunc_event);
|
2012-04-26 11:35:01 +00:00
|
|
|
iio_trigger_put(oldtrig);
|
2015-08-20 19:37:39 +00:00
|
|
|
}
|
|
|
|
if (indio_dev->trig) {
|
|
|
|
if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
|
|
|
|
iio_trigger_attach_poll_func(indio_dev->trig,
|
|
|
|
indio_dev->pollfunc_event);
|
|
|
|
}
|
2009-08-18 17:06:26 +00:00
|
|
|
|
|
|
|
return len;
|
2017-01-22 03:28:52 +00:00
|
|
|
|
|
|
|
out_trigger_put:
|
2017-05-18 07:12:06 +00:00
|
|
|
if (trig)
|
|
|
|
iio_trigger_put(trig);
|
2017-01-22 03:28:52 +00:00
|
|
|
return ret;
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
|
|
|
|
2010-05-05 05:34:00 +00:00
|
|
|
static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
|
|
|
|
iio_trigger_read_current,
|
|
|
|
iio_trigger_write_current);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
|
|
|
static struct attribute *iio_trigger_consumer_attrs[] = {
|
|
|
|
&dev_attr_current_trigger.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group iio_trigger_consumer_attr_group = {
|
|
|
|
.name = "trigger",
|
|
|
|
.attrs = iio_trigger_consumer_attrs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void iio_trig_release(struct device *device)
|
|
|
|
{
|
|
|
|
struct iio_trigger *trig = to_iio_trigger(device);
|
2011-05-18 13:41:18 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (trig->subirq_base) {
|
|
|
|
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
|
|
|
|
irq_modify_status(trig->subirq_base + i,
|
|
|
|
IRQ_NOAUTOEN,
|
|
|
|
IRQ_NOREQUEST | IRQ_NOPROBE);
|
|
|
|
irq_set_chip(trig->subirq_base + i,
|
|
|
|
NULL);
|
|
|
|
irq_set_handler(trig->subirq_base + i,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
irq_free_descs(trig->subirq_base,
|
|
|
|
CONFIG_IIO_CONSUMERS_PER_TRIGGER);
|
|
|
|
}
|
2011-05-18 13:42:22 +00:00
|
|
|
kfree(trig->name);
|
2009-08-18 17:06:26 +00:00
|
|
|
kfree(trig);
|
|
|
|
}
|
|
|
|
|
2017-01-21 16:59:49 +00:00
|
|
|
static const struct device_type iio_trig_type = {
|
2009-08-18 17:06:26 +00:00
|
|
|
.release = iio_trig_release,
|
2013-12-02 02:57:00 +00:00
|
|
|
.groups = iio_trig_dev_groups,
|
2009-08-18 17:06:26 +00:00
|
|
|
};
|
|
|
|
|
2011-05-18 13:41:18 +00:00
|
|
|
static void iio_trig_subirqmask(struct irq_data *d)
|
|
|
|
{
|
|
|
|
struct irq_chip *chip = irq_data_get_irq_chip(d);
|
|
|
|
struct iio_trigger *trig
|
|
|
|
= container_of(chip,
|
|
|
|
struct iio_trigger, subirq_chip);
|
|
|
|
trig->subirqs[d->irq - trig->subirq_base].enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iio_trig_subirqunmask(struct irq_data *d)
|
|
|
|
{
|
|
|
|
struct irq_chip *chip = irq_data_get_irq_chip(d);
|
|
|
|
struct iio_trigger *trig
|
|
|
|
= container_of(chip,
|
|
|
|
struct iio_trigger, subirq_chip);
|
|
|
|
trig->subirqs[d->irq - trig->subirq_base].enabled = true;
|
|
|
|
}
|
|
|
|
|
2013-08-16 13:11:00 +00:00
|
|
|
static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
|
|
|
struct iio_trigger *trig;
|
2017-01-21 04:55:58 +00:00
|
|
|
int i;
|
|
|
|
|
2009-08-18 17:06:26 +00:00
|
|
|
trig = kzalloc(sizeof *trig, GFP_KERNEL);
|
2017-01-21 04:55:58 +00:00
|
|
|
if (!trig)
|
|
|
|
return NULL;
|
2013-08-16 13:11:00 +00:00
|
|
|
|
2017-01-21 04:55:58 +00:00
|
|
|
trig->dev.type = &iio_trig_type;
|
|
|
|
trig->dev.bus = &iio_bus_type;
|
|
|
|
device_initialize(&trig->dev);
|
|
|
|
|
|
|
|
mutex_init(&trig->pool_lock);
|
|
|
|
trig->subirq_base = irq_alloc_descs(-1, 0,
|
|
|
|
CONFIG_IIO_CONSUMERS_PER_TRIGGER,
|
|
|
|
0);
|
|
|
|
if (trig->subirq_base < 0)
|
|
|
|
goto free_trig;
|
|
|
|
|
|
|
|
trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
|
|
|
|
if (trig->name == NULL)
|
|
|
|
goto free_descs;
|
|
|
|
|
|
|
|
trig->subirq_chip.name = trig->name;
|
|
|
|
trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
|
|
|
|
trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
|
|
|
|
for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
|
|
|
|
irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
|
|
|
|
irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
|
|
|
|
irq_modify_status(trig->subirq_base + i,
|
|
|
|
IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
2017-01-21 04:55:58 +00:00
|
|
|
get_device(&trig->dev);
|
2013-08-16 13:11:00 +00:00
|
|
|
|
|
|
|
return trig;
|
2017-01-21 04:55:58 +00:00
|
|
|
|
|
|
|
free_descs:
|
|
|
|
irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
|
|
|
|
free_trig:
|
|
|
|
kfree(trig);
|
|
|
|
return NULL;
|
2013-08-16 13:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct iio_trigger *trig;
|
|
|
|
va_list vargs;
|
|
|
|
|
|
|
|
va_start(vargs, fmt);
|
|
|
|
trig = viio_trigger_alloc(fmt, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
|
2009-08-18 17:06:26 +00:00
|
|
|
return trig;
|
|
|
|
}
|
2012-04-26 11:35:01 +00:00
|
|
|
EXPORT_SYMBOL(iio_trigger_alloc);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
2012-04-26 11:35:01 +00:00
|
|
|
void iio_trigger_free(struct iio_trigger *trig)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
|
|
|
if (trig)
|
|
|
|
put_device(&trig->dev);
|
|
|
|
}
|
2012-04-26 11:35:01 +00:00
|
|
|
EXPORT_SYMBOL(iio_trigger_free);
|
2009-08-18 17:06:26 +00:00
|
|
|
|
2013-08-16 13:11:00 +00:00
|
|
|
static void devm_iio_trigger_release(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
iio_trigger_free(*(struct iio_trigger **)res);
|
|
|
|
}
|
|
|
|
|
2013-10-29 11:39:00 +00:00
|
|
|
/**
|
|
|
|
* devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
|
|
|
|
* @dev: Device to allocate iio_trigger for
|
|
|
|
* @fmt: trigger name format. If it includes format
|
|
|
|
* specifiers, the additional arguments following
|
|
|
|
* format are formatted and inserted in the resulting
|
|
|
|
* string replacing their respective specifiers.
|
|
|
|
*
|
|
|
|
* Managed iio_trigger_alloc. iio_trigger allocated with this function is
|
|
|
|
* automatically freed on driver detach.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* Pointer to allocated iio_trigger on success, NULL on failure.
|
|
|
|
*/
|
2013-08-16 13:11:00 +00:00
|
|
|
struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct iio_trigger **ptr, *trig;
|
|
|
|
va_list vargs;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* use raw alloc_dr for kmalloc caller tracing */
|
|
|
|
va_start(vargs, fmt);
|
|
|
|
trig = viio_trigger_alloc(fmt, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
if (trig) {
|
|
|
|
*ptr = trig;
|
|
|
|
devres_add(dev, ptr);
|
|
|
|
} else {
|
|
|
|
devres_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return trig;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
|
|
|
|
|
2016-09-02 18:47:54 +00:00
|
|
|
static void devm_iio_trigger_unreg(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
iio_trigger_unregister(*(struct iio_trigger **)res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-23 16:25:44 +00:00
|
|
|
* __devm_iio_trigger_register - Resource-managed iio_trigger_register()
|
2016-09-02 18:47:54 +00:00
|
|
|
* @dev: device this trigger was allocated for
|
|
|
|
* @trig_info: trigger to register
|
2017-07-23 16:25:44 +00:00
|
|
|
* @this_mod: module registering the trigger
|
2016-09-02 18:47:54 +00:00
|
|
|
*
|
|
|
|
* Managed iio_trigger_register(). The IIO trigger registered with this
|
|
|
|
* function is automatically unregistered on driver detach. This function
|
|
|
|
* calls iio_trigger_register() internally. Refer to that function for more
|
|
|
|
* information.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 0 on success, negative error number on failure.
|
|
|
|
*/
|
2017-07-23 16:25:44 +00:00
|
|
|
int __devm_iio_trigger_register(struct device *dev,
|
|
|
|
struct iio_trigger *trig_info,
|
|
|
|
struct module *this_mod)
|
2016-09-02 18:47:54 +00:00
|
|
|
{
|
|
|
|
struct iio_trigger **ptr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_iio_trigger_unreg, sizeof(*ptr), GFP_KERNEL);
|
|
|
|
if (!ptr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*ptr = trig_info;
|
2017-07-23 16:25:44 +00:00
|
|
|
ret = __iio_trigger_register(trig_info, this_mod);
|
2016-09-02 18:47:54 +00:00
|
|
|
if (!ret)
|
|
|
|
devres_add(dev, ptr);
|
|
|
|
else
|
|
|
|
devres_free(ptr);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-07-23 16:25:44 +00:00
|
|
|
EXPORT_SYMBOL_GPL(__devm_iio_trigger_register);
|
2016-09-02 18:47:54 +00:00
|
|
|
|
2016-09-01 08:27:17 +00:00
|
|
|
bool iio_trigger_using_own(struct iio_dev *indio_dev)
|
|
|
|
{
|
|
|
|
return indio_dev->trig->attached_own_device;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_using_own);
|
|
|
|
|
2016-09-23 15:19:41 +00:00
|
|
|
/**
|
|
|
|
* iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
|
|
|
|
* the same device
|
|
|
|
* @trig: The IIO trigger to check
|
|
|
|
* @indio_dev: the IIO device to check
|
|
|
|
*
|
|
|
|
* This function can be used as the validate_device callback for triggers that
|
|
|
|
* can only be attached to their own device.
|
|
|
|
*
|
|
|
|
* Return: 0 if both the trigger and the IIO device belong to the same
|
|
|
|
* device, -EINVAL otherwise.
|
|
|
|
*/
|
|
|
|
int iio_trigger_validate_own_device(struct iio_trigger *trig,
|
|
|
|
struct iio_dev *indio_dev)
|
|
|
|
{
|
|
|
|
if (indio_dev->dev.parent != trig->dev.parent)
|
|
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_trigger_validate_own_device);
|
|
|
|
|
2011-10-26 16:27:38 +00:00
|
|
|
void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
2011-10-06 16:14:35 +00:00
|
|
|
indio_dev->groups[indio_dev->groupcounter++] =
|
2011-09-02 16:14:40 +00:00
|
|
|
&iio_trigger_consumer_attr_group;
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 16:14:35 +00:00
|
|
|
void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
|
2009-08-18 17:06:26 +00:00
|
|
|
{
|
2013-02-04 11:36:00 +00:00
|
|
|
/* Clean up an associated but not attached trigger reference */
|
2011-10-06 16:14:35 +00:00
|
|
|
if (indio_dev->trig)
|
2012-04-26 11:35:01 +00:00
|
|
|
iio_trigger_put(indio_dev->trig);
|
2009-08-18 17:06:26 +00:00
|
|
|
}
|