cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

industrialio-trigger.c (20034B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/* The industrial I/O core, trigger handling functions
      3 *
      4 * Copyright (c) 2008 Jonathan Cameron
      5 */
      6
      7#include <linux/kernel.h>
      8#include <linux/idr.h>
      9#include <linux/err.h>
     10#include <linux/device.h>
     11#include <linux/interrupt.h>
     12#include <linux/list.h>
     13#include <linux/slab.h>
     14
     15#include <linux/iio/iio.h>
     16#include <linux/iio/iio-opaque.h>
     17#include <linux/iio/trigger.h>
     18#include "iio_core.h"
     19#include "iio_core_trigger.h"
     20#include <linux/iio/trigger_consumer.h>
     21
     22/* RFC - Question of approach
     23 * Make the common case (single sensor single trigger)
     24 * simple by starting trigger capture from when first sensors
     25 * is added.
     26 *
     27 * Complex simultaneous start requires use of 'hold' functionality
     28 * of the trigger. (not implemented)
     29 *
     30 * Any other suggestions?
     31 */
     32
     33static DEFINE_IDA(iio_trigger_ida);
     34
     35/* Single list of all available triggers */
     36static LIST_HEAD(iio_trigger_list);
     37static DEFINE_MUTEX(iio_trigger_list_lock);
     38
     39/**
     40 * iio_trigger_read_name() - retrieve useful identifying name
     41 * @dev:	device associated with the iio_trigger
     42 * @attr:	pointer to the device_attribute structure that is
     43 *		being processed
     44 * @buf:	buffer to print the name into
     45 *
     46 * Return: a negative number on failure or the number of written
     47 *	   characters on success.
     48 */
     49static ssize_t iio_trigger_read_name(struct device *dev,
     50				     struct device_attribute *attr,
     51				     char *buf)
     52{
     53	struct iio_trigger *trig = to_iio_trigger(dev);
     54	return sysfs_emit(buf, "%s\n", trig->name);
     55}
     56
     57static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
     58
     59static struct attribute *iio_trig_dev_attrs[] = {
     60	&dev_attr_name.attr,
     61	NULL,
     62};
     63ATTRIBUTE_GROUPS(iio_trig_dev);
     64
     65static struct iio_trigger *__iio_trigger_find_by_name(const char *name);
     66
     67int __iio_trigger_register(struct iio_trigger *trig_info,
     68			   struct module *this_mod)
     69{
     70	int ret;
     71
     72	trig_info->owner = this_mod;
     73
     74	trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
     75	if (trig_info->id < 0)
     76		return trig_info->id;
     77
     78	/* Set the name used for the sysfs directory etc */
     79	dev_set_name(&trig_info->dev, "trigger%d", trig_info->id);
     80
     81	ret = device_add(&trig_info->dev);
     82	if (ret)
     83		goto error_unregister_id;
     84
     85	/* Add to list of available triggers held by the IIO core */
     86	mutex_lock(&iio_trigger_list_lock);
     87	if (__iio_trigger_find_by_name(trig_info->name)) {
     88		pr_err("Duplicate trigger name '%s'\n", trig_info->name);
     89		ret = -EEXIST;
     90		goto error_device_del;
     91	}
     92	list_add_tail(&trig_info->list, &iio_trigger_list);
     93	mutex_unlock(&iio_trigger_list_lock);
     94
     95	return 0;
     96
     97error_device_del:
     98	mutex_unlock(&iio_trigger_list_lock);
     99	device_del(&trig_info->dev);
    100error_unregister_id:
    101	ida_simple_remove(&iio_trigger_ida, trig_info->id);
    102	return ret;
    103}
    104EXPORT_SYMBOL(__iio_trigger_register);
    105
    106void iio_trigger_unregister(struct iio_trigger *trig_info)
    107{
    108	mutex_lock(&iio_trigger_list_lock);
    109	list_del(&trig_info->list);
    110	mutex_unlock(&iio_trigger_list_lock);
    111
    112	ida_simple_remove(&iio_trigger_ida, trig_info->id);
    113	/* Possible issue in here */
    114	device_del(&trig_info->dev);
    115}
    116EXPORT_SYMBOL(iio_trigger_unregister);
    117
    118int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
    119{
    120	struct iio_dev_opaque *iio_dev_opaque;
    121
    122	if (!indio_dev || !trig)
    123		return -EINVAL;
    124
    125	iio_dev_opaque = to_iio_dev_opaque(indio_dev);
    126	mutex_lock(&indio_dev->mlock);
    127	WARN_ON(iio_dev_opaque->trig_readonly);
    128
    129	indio_dev->trig = iio_trigger_get(trig);
    130	iio_dev_opaque->trig_readonly = true;
    131	mutex_unlock(&indio_dev->mlock);
    132
    133	return 0;
    134}
    135EXPORT_SYMBOL(iio_trigger_set_immutable);
    136
    137/* Search for trigger by name, assuming iio_trigger_list_lock held */
    138static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
    139{
    140	struct iio_trigger *iter;
    141
    142	list_for_each_entry(iter, &iio_trigger_list, list)
    143		if (!strcmp(iter->name, name))
    144			return iter;
    145
    146	return NULL;
    147}
    148
    149static struct iio_trigger *iio_trigger_acquire_by_name(const char *name)
    150{
    151	struct iio_trigger *trig = NULL, *iter;
    152
    153	mutex_lock(&iio_trigger_list_lock);
    154	list_for_each_entry(iter, &iio_trigger_list, list)
    155		if (sysfs_streq(iter->name, name)) {
    156			trig = iter;
    157			iio_trigger_get(trig);
    158			break;
    159		}
    160	mutex_unlock(&iio_trigger_list_lock);
    161
    162	return trig;
    163}
    164
    165static void iio_reenable_work_fn(struct work_struct *work)
    166{
    167	struct iio_trigger *trig = container_of(work, struct iio_trigger,
    168						reenable_work);
    169
    170	/*
    171	 * This 'might' occur after the trigger state is set to disabled -
    172	 * in that case the driver should skip reenabling.
    173	 */
    174	trig->ops->reenable(trig);
    175}
    176
    177/*
    178 * In general, reenable callbacks may need to sleep and this path is
    179 * not performance sensitive, so just queue up a work item
    180 * to reneable the trigger for us.
    181 *
    182 * Races that can cause this.
    183 * 1) A handler occurs entirely in interrupt context so the counter
    184 *    the final decrement is still in this interrupt.
    185 * 2) The trigger has been removed, but one last interrupt gets through.
    186 *
    187 * For (1) we must call reenable, but not in atomic context.
    188 * For (2) it should be safe to call reenanble, if drivers never blindly
    189 * reenable after state is off.
    190 */
    191static void iio_trigger_notify_done_atomic(struct iio_trigger *trig)
    192{
    193	if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
    194	    trig->ops->reenable)
    195		schedule_work(&trig->reenable_work);
    196}
    197
    198void iio_trigger_poll(struct iio_trigger *trig)
    199{
    200	int i;
    201
    202	if (!atomic_read(&trig->use_count)) {
    203		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
    204
    205		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
    206			if (trig->subirqs[i].enabled)
    207				generic_handle_irq(trig->subirq_base + i);
    208			else
    209				iio_trigger_notify_done_atomic(trig);
    210		}
    211	}
    212}
    213EXPORT_SYMBOL(iio_trigger_poll);
    214
    215irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
    216{
    217	iio_trigger_poll(private);
    218	return IRQ_HANDLED;
    219}
    220EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
    221
    222void iio_trigger_poll_chained(struct iio_trigger *trig)
    223{
    224	int i;
    225
    226	if (!atomic_read(&trig->use_count)) {
    227		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
    228
    229		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
    230			if (trig->subirqs[i].enabled)
    231				handle_nested_irq(trig->subirq_base + i);
    232			else
    233				iio_trigger_notify_done(trig);
    234		}
    235	}
    236}
    237EXPORT_SYMBOL(iio_trigger_poll_chained);
    238
    239void iio_trigger_notify_done(struct iio_trigger *trig)
    240{
    241	if (atomic_dec_and_test(&trig->use_count) && trig->ops &&
    242	    trig->ops->reenable)
    243		trig->ops->reenable(trig);
    244}
    245EXPORT_SYMBOL(iio_trigger_notify_done);
    246
    247/* Trigger Consumer related functions */
    248static int iio_trigger_get_irq(struct iio_trigger *trig)
    249{
    250	int ret;
    251
    252	mutex_lock(&trig->pool_lock);
    253	ret = bitmap_find_free_region(trig->pool,
    254				      CONFIG_IIO_CONSUMERS_PER_TRIGGER,
    255				      ilog2(1));
    256	mutex_unlock(&trig->pool_lock);
    257	if (ret >= 0)
    258		ret += trig->subirq_base;
    259
    260	return ret;
    261}
    262
    263static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
    264{
    265	mutex_lock(&trig->pool_lock);
    266	clear_bit(irq - trig->subirq_base, trig->pool);
    267	mutex_unlock(&trig->pool_lock);
    268}
    269
    270/* Complexity in here.  With certain triggers (datardy) an acknowledgement
    271 * may be needed if the pollfuncs do not include the data read for the
    272 * triggering device.
    273 * This is not currently handled.  Alternative of not enabling trigger unless
    274 * the relevant function is in there may be the best option.
    275 */
    276/* Worth protecting against double additions? */
    277int iio_trigger_attach_poll_func(struct iio_trigger *trig,
    278				 struct iio_poll_func *pf)
    279{
    280	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
    281	bool notinuse =
    282		bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
    283	int ret = 0;
    284
    285	/* Prevent the module from being removed whilst attached to a trigger */
    286	__module_get(iio_dev_opaque->driver_module);
    287
    288	/* Get irq number */
    289	pf->irq = iio_trigger_get_irq(trig);
    290	if (pf->irq < 0) {
    291		pr_err("Could not find an available irq for trigger %s, CONFIG_IIO_CONSUMERS_PER_TRIGGER=%d limit might be exceeded\n",
    292			trig->name, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
    293		goto out_put_module;
    294	}
    295
    296	/* Request irq */
    297	ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
    298				   pf->type, pf->name,
    299				   pf);
    300	if (ret < 0)
    301		goto out_put_irq;
    302
    303	/* Enable trigger in driver */
    304	if (trig->ops && trig->ops->set_trigger_state && notinuse) {
    305		ret = trig->ops->set_trigger_state(trig, true);
    306		if (ret < 0)
    307			goto out_free_irq;
    308	}
    309
    310	/*
    311	 * Check if we just registered to our own trigger: we determine that
    312	 * this is the case if the IIO device and the trigger device share the
    313	 * same parent device.
    314	 */
    315	if (pf->indio_dev->dev.parent == trig->dev.parent)
    316		trig->attached_own_device = true;
    317
    318	return ret;
    319
    320out_free_irq:
    321	free_irq(pf->irq, pf);
    322out_put_irq:
    323	iio_trigger_put_irq(trig, pf->irq);
    324out_put_module:
    325	module_put(iio_dev_opaque->driver_module);
    326	return ret;
    327}
    328
    329int iio_trigger_detach_poll_func(struct iio_trigger *trig,
    330				 struct iio_poll_func *pf)
    331{
    332	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(pf->indio_dev);
    333	bool no_other_users =
    334		bitmap_weight(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER) == 1;
    335	int ret = 0;
    336
    337	if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
    338		ret = trig->ops->set_trigger_state(trig, false);
    339		if (ret)
    340			return ret;
    341	}
    342	if (pf->indio_dev->dev.parent == trig->dev.parent)
    343		trig->attached_own_device = false;
    344	iio_trigger_put_irq(trig, pf->irq);
    345	free_irq(pf->irq, pf);
    346	module_put(iio_dev_opaque->driver_module);
    347
    348	return ret;
    349}
    350
    351irqreturn_t iio_pollfunc_store_time(int irq, void *p)
    352{
    353	struct iio_poll_func *pf = p;
    354
    355	pf->timestamp = iio_get_time_ns(pf->indio_dev);
    356	return IRQ_WAKE_THREAD;
    357}
    358EXPORT_SYMBOL(iio_pollfunc_store_time);
    359
    360struct iio_poll_func
    361*iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
    362		    irqreturn_t (*thread)(int irq, void *p),
    363		    int type,
    364		    struct iio_dev *indio_dev,
    365		    const char *fmt,
    366		    ...)
    367{
    368	va_list vargs;
    369	struct iio_poll_func *pf;
    370
    371	pf = kmalloc(sizeof *pf, GFP_KERNEL);
    372	if (pf == NULL)
    373		return NULL;
    374	va_start(vargs, fmt);
    375	pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
    376	va_end(vargs);
    377	if (pf->name == NULL) {
    378		kfree(pf);
    379		return NULL;
    380	}
    381	pf->h = h;
    382	pf->thread = thread;
    383	pf->type = type;
    384	pf->indio_dev = indio_dev;
    385
    386	return pf;
    387}
    388EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
    389
    390void iio_dealloc_pollfunc(struct iio_poll_func *pf)
    391{
    392	kfree(pf->name);
    393	kfree(pf);
    394}
    395EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
    396
    397/**
    398 * iio_trigger_read_current() - trigger consumer sysfs query current trigger
    399 * @dev:	device associated with an industrial I/O device
    400 * @attr:	pointer to the device_attribute structure that
    401 *		is being processed
    402 * @buf:	buffer where the current trigger name will be printed into
    403 *
    404 * For trigger consumers the current_trigger interface allows the trigger
    405 * used by the device to be queried.
    406 *
    407 * Return: a negative number on failure, the number of characters written
    408 *	   on success or 0 if no trigger is available
    409 */
    410static ssize_t iio_trigger_read_current(struct device *dev,
    411					struct device_attribute *attr,
    412					char *buf)
    413{
    414	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
    415
    416	if (indio_dev->trig)
    417		return sysfs_emit(buf, "%s\n", indio_dev->trig->name);
    418	return 0;
    419}
    420
    421/**
    422 * iio_trigger_write_current() - trigger consumer sysfs set current trigger
    423 * @dev:	device associated with an industrial I/O device
    424 * @attr:	device attribute that is being processed
    425 * @buf:	string buffer that holds the name of the trigger
    426 * @len:	length of the trigger name held by buf
    427 *
    428 * For trigger consumers the current_trigger interface allows the trigger
    429 * used for this device to be specified at run time based on the trigger's
    430 * name.
    431 *
    432 * Return: negative error code on failure or length of the buffer
    433 *	   on success
    434 */
    435static ssize_t iio_trigger_write_current(struct device *dev,
    436					 struct device_attribute *attr,
    437					 const char *buf,
    438					 size_t len)
    439{
    440	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
    441	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
    442	struct iio_trigger *oldtrig = indio_dev->trig;
    443	struct iio_trigger *trig;
    444	int ret;
    445
    446	mutex_lock(&indio_dev->mlock);
    447	if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
    448		mutex_unlock(&indio_dev->mlock);
    449		return -EBUSY;
    450	}
    451	if (iio_dev_opaque->trig_readonly) {
    452		mutex_unlock(&indio_dev->mlock);
    453		return -EPERM;
    454	}
    455	mutex_unlock(&indio_dev->mlock);
    456
    457	trig = iio_trigger_acquire_by_name(buf);
    458	if (oldtrig == trig) {
    459		ret = len;
    460		goto out_trigger_put;
    461	}
    462
    463	if (trig && indio_dev->info->validate_trigger) {
    464		ret = indio_dev->info->validate_trigger(indio_dev, trig);
    465		if (ret)
    466			goto out_trigger_put;
    467	}
    468
    469	if (trig && trig->ops && trig->ops->validate_device) {
    470		ret = trig->ops->validate_device(trig, indio_dev);
    471		if (ret)
    472			goto out_trigger_put;
    473	}
    474
    475	indio_dev->trig = trig;
    476
    477	if (oldtrig) {
    478		if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
    479			iio_trigger_detach_poll_func(oldtrig,
    480						     indio_dev->pollfunc_event);
    481		iio_trigger_put(oldtrig);
    482	}
    483	if (indio_dev->trig) {
    484		if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
    485			iio_trigger_attach_poll_func(indio_dev->trig,
    486						     indio_dev->pollfunc_event);
    487	}
    488
    489	return len;
    490
    491out_trigger_put:
    492	if (trig)
    493		iio_trigger_put(trig);
    494	return ret;
    495}
    496
    497static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
    498		   iio_trigger_read_current,
    499		   iio_trigger_write_current);
    500
    501static struct attribute *iio_trigger_consumer_attrs[] = {
    502	&dev_attr_current_trigger.attr,
    503	NULL,
    504};
    505
    506static const struct attribute_group iio_trigger_consumer_attr_group = {
    507	.name = "trigger",
    508	.attrs = iio_trigger_consumer_attrs,
    509};
    510
    511static void iio_trig_release(struct device *device)
    512{
    513	struct iio_trigger *trig = to_iio_trigger(device);
    514	int i;
    515
    516	if (trig->subirq_base) {
    517		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
    518			irq_modify_status(trig->subirq_base + i,
    519					  IRQ_NOAUTOEN,
    520					  IRQ_NOREQUEST | IRQ_NOPROBE);
    521			irq_set_chip(trig->subirq_base + i,
    522				     NULL);
    523			irq_set_handler(trig->subirq_base + i,
    524					NULL);
    525		}
    526
    527		irq_free_descs(trig->subirq_base,
    528			       CONFIG_IIO_CONSUMERS_PER_TRIGGER);
    529	}
    530	kfree(trig->name);
    531	kfree(trig);
    532}
    533
    534static const struct device_type iio_trig_type = {
    535	.release = iio_trig_release,
    536	.groups = iio_trig_dev_groups,
    537};
    538
    539static void iio_trig_subirqmask(struct irq_data *d)
    540{
    541	struct irq_chip *chip = irq_data_get_irq_chip(d);
    542	struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
    543
    544	trig->subirqs[d->irq - trig->subirq_base].enabled = false;
    545}
    546
    547static void iio_trig_subirqunmask(struct irq_data *d)
    548{
    549	struct irq_chip *chip = irq_data_get_irq_chip(d);
    550	struct iio_trigger *trig = container_of(chip, struct iio_trigger, subirq_chip);
    551
    552	trig->subirqs[d->irq - trig->subirq_base].enabled = true;
    553}
    554
    555static __printf(2, 0)
    556struct iio_trigger *viio_trigger_alloc(struct device *parent,
    557				       const char *fmt,
    558				       va_list vargs)
    559{
    560	struct iio_trigger *trig;
    561	int i;
    562
    563	trig = kzalloc(sizeof *trig, GFP_KERNEL);
    564	if (!trig)
    565		return NULL;
    566
    567	trig->dev.parent = parent;
    568	trig->dev.type = &iio_trig_type;
    569	trig->dev.bus = &iio_bus_type;
    570	device_initialize(&trig->dev);
    571	INIT_WORK(&trig->reenable_work, iio_reenable_work_fn);
    572
    573	mutex_init(&trig->pool_lock);
    574	trig->subirq_base = irq_alloc_descs(-1, 0,
    575					    CONFIG_IIO_CONSUMERS_PER_TRIGGER,
    576					    0);
    577	if (trig->subirq_base < 0)
    578		goto free_trig;
    579
    580	trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
    581	if (trig->name == NULL)
    582		goto free_descs;
    583
    584	trig->subirq_chip.name = trig->name;
    585	trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
    586	trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
    587	for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
    588		irq_set_chip(trig->subirq_base + i, &trig->subirq_chip);
    589		irq_set_handler(trig->subirq_base + i, &handle_simple_irq);
    590		irq_modify_status(trig->subirq_base + i,
    591				  IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
    592	}
    593
    594	return trig;
    595
    596free_descs:
    597	irq_free_descs(trig->subirq_base, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
    598free_trig:
    599	kfree(trig);
    600	return NULL;
    601}
    602
    603/**
    604 * iio_trigger_alloc - Allocate a trigger
    605 * @parent:		Device to allocate iio_trigger for
    606 * @fmt:		trigger name format. If it includes format
    607 *			specifiers, the additional arguments following
    608 *			format are formatted and inserted in the resulting
    609 *			string replacing their respective specifiers.
    610 * RETURNS:
    611 * Pointer to allocated iio_trigger on success, NULL on failure.
    612 */
    613struct iio_trigger *iio_trigger_alloc(struct device *parent, const char *fmt, ...)
    614{
    615	struct iio_trigger *trig;
    616	va_list vargs;
    617
    618	va_start(vargs, fmt);
    619	trig = viio_trigger_alloc(parent, fmt, vargs);
    620	va_end(vargs);
    621
    622	return trig;
    623}
    624EXPORT_SYMBOL(iio_trigger_alloc);
    625
    626void iio_trigger_free(struct iio_trigger *trig)
    627{
    628	if (trig)
    629		put_device(&trig->dev);
    630}
    631EXPORT_SYMBOL(iio_trigger_free);
    632
    633static void devm_iio_trigger_release(struct device *dev, void *res)
    634{
    635	iio_trigger_free(*(struct iio_trigger **)res);
    636}
    637
    638/**
    639 * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
    640 * Managed iio_trigger_alloc.  iio_trigger allocated with this function is
    641 * automatically freed on driver detach.
    642 * @parent:		Device to allocate iio_trigger for
    643 * @fmt:		trigger name format. If it includes format
    644 *			specifiers, the additional arguments following
    645 *			format are formatted and inserted in the resulting
    646 *			string replacing their respective specifiers.
    647 *
    648 *
    649 * RETURNS:
    650 * Pointer to allocated iio_trigger on success, NULL on failure.
    651 */
    652struct iio_trigger *devm_iio_trigger_alloc(struct device *parent, const char *fmt, ...)
    653{
    654	struct iio_trigger **ptr, *trig;
    655	va_list vargs;
    656
    657	ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
    658			   GFP_KERNEL);
    659	if (!ptr)
    660		return NULL;
    661
    662	/* use raw alloc_dr for kmalloc caller tracing */
    663	va_start(vargs, fmt);
    664	trig = viio_trigger_alloc(parent, fmt, vargs);
    665	va_end(vargs);
    666	if (trig) {
    667		*ptr = trig;
    668		devres_add(parent, ptr);
    669	} else {
    670		devres_free(ptr);
    671	}
    672
    673	return trig;
    674}
    675EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
    676
    677static void devm_iio_trigger_unreg(void *trigger_info)
    678{
    679	iio_trigger_unregister(trigger_info);
    680}
    681
    682/**
    683 * __devm_iio_trigger_register - Resource-managed iio_trigger_register()
    684 * @dev:	device this trigger was allocated for
    685 * @trig_info:	trigger to register
    686 * @this_mod:   module registering the trigger
    687 *
    688 * Managed iio_trigger_register().  The IIO trigger registered with this
    689 * function is automatically unregistered on driver detach. This function
    690 * calls iio_trigger_register() internally. Refer to that function for more
    691 * information.
    692 *
    693 * RETURNS:
    694 * 0 on success, negative error number on failure.
    695 */
    696int __devm_iio_trigger_register(struct device *dev,
    697				struct iio_trigger *trig_info,
    698				struct module *this_mod)
    699{
    700	int ret;
    701
    702	ret = __iio_trigger_register(trig_info, this_mod);
    703	if (ret)
    704		return ret;
    705
    706	return devm_add_action_or_reset(dev, devm_iio_trigger_unreg, trig_info);
    707}
    708EXPORT_SYMBOL_GPL(__devm_iio_trigger_register);
    709
    710bool iio_trigger_using_own(struct iio_dev *indio_dev)
    711{
    712	return indio_dev->trig->attached_own_device;
    713}
    714EXPORT_SYMBOL(iio_trigger_using_own);
    715
    716/**
    717 * iio_trigger_validate_own_device - Check if a trigger and IIO device belong to
    718 *  the same device
    719 * @trig: The IIO trigger to check
    720 * @indio_dev: the IIO device to check
    721 *
    722 * This function can be used as the validate_device callback for triggers that
    723 * can only be attached to their own device.
    724 *
    725 * Return: 0 if both the trigger and the IIO device belong to the same
    726 * device, -EINVAL otherwise.
    727 */
    728int iio_trigger_validate_own_device(struct iio_trigger *trig,
    729				    struct iio_dev *indio_dev)
    730{
    731	if (indio_dev->dev.parent != trig->dev.parent)
    732		return -EINVAL;
    733	return 0;
    734}
    735EXPORT_SYMBOL(iio_trigger_validate_own_device);
    736
    737int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
    738{
    739	return iio_device_register_sysfs_group(indio_dev,
    740					       &iio_trigger_consumer_attr_group);
    741}
    742
    743void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
    744{
    745	/* Clean up an associated but not attached trigger reference */
    746	if (indio_dev->trig)
    747		iio_trigger_put(indio_dev->trig);
    748}