^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2011 Analog Devices Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/irq_work.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/iio/trigger.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct iio_sysfs_trig {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct iio_trigger *trig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct irq_work work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct list_head l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static LIST_HEAD(iio_sysfs_trig_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static DEFINE_MUTEX(iio_sysfs_trig_list_mut);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static int iio_sysfs_trigger_probe(int id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static ssize_t iio_sysfs_trig_add(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) ret = kstrtoul(buf, 10, &input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ret = iio_sysfs_trigger_probe(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static DEVICE_ATTR(add_trigger, S_IWUSR, NULL, &iio_sysfs_trig_add);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int iio_sysfs_trigger_remove(int id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static ssize_t iio_sysfs_trig_remove(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) ret = kstrtoul(buf, 10, &input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ret = iio_sysfs_trigger_remove(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static DEVICE_ATTR(remove_trigger, S_IWUSR, NULL, &iio_sysfs_trig_remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static struct attribute *iio_sysfs_trig_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) &dev_attr_add_trigger.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) &dev_attr_remove_trigger.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static const struct attribute_group iio_sysfs_trig_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .attrs = iio_sysfs_trig_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static const struct attribute_group *iio_sysfs_trig_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) &iio_sysfs_trig_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Nothing to actually do upon release */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void iio_trigger_sysfs_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static struct device iio_sysfs_trig_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .bus = &iio_bus_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .groups = iio_sysfs_trig_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .release = &iio_trigger_sysfs_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static void iio_sysfs_trigger_work(struct irq_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) iio_trigger_poll(trig->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static ssize_t iio_sysfs_trigger_poll(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct iio_trigger *trig = to_iio_trigger(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct iio_sysfs_trig *sysfs_trig = iio_trigger_get_drvdata(trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) irq_work_queue(&sysfs_trig->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static DEVICE_ATTR(trigger_now, S_IWUSR, NULL, iio_sysfs_trigger_poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static struct attribute *iio_sysfs_trigger_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) &dev_attr_trigger_now.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static const struct attribute_group iio_sysfs_trigger_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .attrs = iio_sysfs_trigger_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static const struct attribute_group *iio_sysfs_trigger_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) &iio_sysfs_trigger_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static const struct iio_trigger_ops iio_sysfs_trigger_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int iio_sysfs_trigger_probe(int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct iio_sysfs_trig *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) bool foundit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mutex_lock(&iio_sysfs_trig_list_mut);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) list_for_each_entry(t, &iio_sysfs_trig_list, l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (id == t->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) foundit = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (foundit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) t = kmalloc(sizeof(*t), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (t == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) t->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) t->trig = iio_trigger_alloc("sysfstrig%d", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!t->trig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) goto free_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) t->trig->dev.groups = iio_sysfs_trigger_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) t->trig->ops = &iio_sysfs_trigger_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) t->trig->dev.parent = &iio_sysfs_trig_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) iio_trigger_set_drvdata(t->trig, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) init_irq_work(&t->work, iio_sysfs_trigger_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = iio_trigger_register(t->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) list_add(&t->l, &iio_sysfs_trig_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mutex_unlock(&iio_sysfs_trig_list_mut);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) iio_trigger_free(t->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) free_t:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) kfree(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) mutex_unlock(&iio_sysfs_trig_list_mut);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int iio_sysfs_trigger_remove(int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) bool foundit = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct iio_sysfs_trig *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mutex_lock(&iio_sysfs_trig_list_mut);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) list_for_each_entry(t, &iio_sysfs_trig_list, l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (id == t->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) foundit = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (!foundit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) mutex_unlock(&iio_sysfs_trig_list_mut);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) iio_trigger_unregister(t->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) iio_trigger_free(t->trig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) list_del(&t->l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) kfree(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) mutex_unlock(&iio_sysfs_trig_list_mut);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int __init iio_sysfs_trig_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) device_initialize(&iio_sysfs_trig_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return device_add(&iio_sysfs_trig_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) module_init(iio_sysfs_trig_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static void __exit iio_sysfs_trig_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) device_unregister(&iio_sysfs_trig_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) module_exit(iio_sysfs_trig_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) MODULE_ALIAS("platform:iio-trig-sysfs");