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