// SPDX-License-Identifier: GPL-2.0-only /* The industrial I/O core, trigger handling functions * * Copyright (c) 2008 Jonathan Cameron */ #include <linux/cleanup.h> #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 <linux/slab.h> #include <linux/iio/iio.h> #include <linux/iio/iio-opaque.h> #include <linux/iio/trigger.h> #include "iio_core.h" #include "iio_core_trigger.h" #include <linux/iio/trigger_consumer.h> /* 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? */ static DEFINE_IDA(iio_trigger_ida); /* Single list of all available triggers */ static LIST_HEAD(iio_trigger_list); static DEFINE_MUTEX(iio_trigger_list_lock); /** * name_show() - retrieve useful identifying name * @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. */ static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) { … } static DEVICE_ATTR_RO(name); static struct attribute *iio_trig_dev_attrs[] = …; ATTRIBUTE_GROUPS(…); static struct iio_trigger *__iio_trigger_find_by_name(const char *name); int iio_trigger_register(struct iio_trigger *trig_info) { … } EXPORT_SYMBOL(…); void iio_trigger_unregister(struct iio_trigger *trig_info) { … } EXPORT_SYMBOL(…); int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig) { … } EXPORT_SYMBOL(…); /* Search for trigger by name, assuming iio_trigger_list_lock held */ static struct iio_trigger *__iio_trigger_find_by_name(const char *name) { … } static struct iio_trigger *iio_trigger_acquire_by_name(const char *name) { … } static void iio_reenable_work_fn(struct work_struct *work) { … } /* * In general, reenable callbacks may need to sleep and this path is * not performance sensitive, so just queue up a work item * to reneable the trigger for us. * * Races that can cause this. * 1) A handler occurs entirely in interrupt context so the counter * the final decrement is still in this interrupt. * 2) The trigger has been removed, but one last interrupt gets through. * * For (1) we must call reenable, but not in atomic context. * For (2) it should be safe to call reenanble, if drivers never blindly * reenable after state is off. */ static void iio_trigger_notify_done_atomic(struct iio_trigger *trig) { … } /** * iio_trigger_poll() - Call the IRQ trigger handler of the consumers * @trig: trigger which occurred * * This function should only be called from a hard IRQ context. */ void iio_trigger_poll(struct iio_trigger *trig) { … } EXPORT_SYMBOL(…); irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private) { … } EXPORT_SYMBOL(…); /** * iio_trigger_poll_nested() - Call the threaded trigger handler of the * consumers * @trig: trigger which occurred * * This function should only be called from a kernel thread context. */ void iio_trigger_poll_nested(struct iio_trigger *trig) { … } EXPORT_SYMBOL(…); void iio_trigger_notify_done(struct iio_trigger *trig) { … } EXPORT_SYMBOL(…); /* Trigger Consumer related functions */ static int iio_trigger_get_irq(struct iio_trigger *trig) { … } static void iio_trigger_put_irq(struct iio_trigger *trig, int irq) { … } /* 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. */ /* Worth protecting against double additions? */ int iio_trigger_attach_poll_func(struct iio_trigger *trig, struct iio_poll_func *pf) { … } int iio_trigger_detach_poll_func(struct iio_trigger *trig, struct iio_poll_func *pf) { … } irqreturn_t iio_pollfunc_store_time(int irq, void *p) { … } EXPORT_SYMBOL(…); struct iio_poll_func *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p), irqreturn_t (*thread)(int irq, void *p), int type, struct iio_dev *indio_dev, const char *fmt, ...) { … } EXPORT_SYMBOL_GPL(…); void iio_dealloc_pollfunc(struct iio_poll_func *pf) { … } EXPORT_SYMBOL_GPL(…); /** * current_trigger_show() - trigger consumer sysfs query current trigger * @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 * * For trigger consumers the current_trigger interface allows the trigger * used by the device to be queried. * * Return: a negative number on failure, the number of characters written * on success or 0 if no trigger is available */ static ssize_t current_trigger_show(struct device *dev, struct device_attribute *attr, char *buf) { … } /** * current_trigger_store() - trigger consumer sysfs set current trigger * @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 * * For trigger consumers the current_trigger interface allows the trigger * used for this device to be specified at run time based on the trigger's * name. * * Return: negative error code on failure or length of the buffer * on success */ static ssize_t current_trigger_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { … } static DEVICE_ATTR_RW(current_trigger); static struct attribute *iio_trigger_consumer_attrs[] = …; static const struct attribute_group iio_trigger_consumer_attr_group = …; static void iio_trig_release(struct device *device) { … } static const struct device_type iio_trig_type = …; static void iio_trig_subirqmask(struct irq_data *d) { … } static void iio_trig_subirqunmask(struct irq_data *d) { … } static __printf(3, 0) struct iio_trigger *viio_trigger_alloc(struct device *parent, struct module *this_mod, const char *fmt, va_list vargs) { … } /** * __iio_trigger_alloc - Allocate a trigger * @parent: Device to allocate iio_trigger for * @this_mod: module allocating the trigger * @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. * RETURNS: * Pointer to allocated iio_trigger on success, NULL on failure. */ struct iio_trigger *__iio_trigger_alloc(struct device *parent, struct module *this_mod, const char *fmt, ...) { … } EXPORT_SYMBOL(…); void iio_trigger_free(struct iio_trigger *trig) { … } EXPORT_SYMBOL(…); static void devm_iio_trigger_release(struct device *dev, void *res) { … } /** * __devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc() * Managed iio_trigger_alloc. iio_trigger allocated with this function is * automatically freed on driver detach. * @parent: Device to allocate iio_trigger for * @this_mod: module allocating the trigger * @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. * * * RETURNS: * Pointer to allocated iio_trigger on success, NULL on failure. */ struct iio_trigger *__devm_iio_trigger_alloc(struct device *parent, struct module *this_mod, const char *fmt, ...) { … } EXPORT_SYMBOL_GPL(…); static void devm_iio_trigger_unreg(void *trigger_info) { … } /** * devm_iio_trigger_register - Resource-managed iio_trigger_register() * @dev: device this trigger was allocated for * @trig_info: trigger to register * * 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. */ int devm_iio_trigger_register(struct device *dev, struct iio_trigger *trig_info) { … } EXPORT_SYMBOL_GPL(…); bool iio_trigger_using_own(struct iio_dev *indio_dev) { … } EXPORT_SYMBOL(…); /** * iio_validate_own_trigger - Check if a trigger and IIO device belong to * the same device * @idev: the IIO device to check * @trig: the IIO trigger to check * * This function can be used as the validate_trigger 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_validate_own_trigger(struct iio_dev *idev, struct iio_trigger *trig) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } EXPORT_SYMBOL(…); int iio_device_register_trigger_consumer(struct iio_dev *indio_dev) { … } void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev) { … }