Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 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> |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 12 | #include <linux/list.h> |
Lars-Peter Clausen | f38bc92 | 2012-09-06 10:05:00 +0100 | [diff] [blame] | 13 | #include <linux/irq_work.h> |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 14 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 15 | #include <linux/iio/iio.h> |
| 16 | #include <linux/iio/trigger.h> |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 17 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 18 | struct iio_sysfs_trig { |
| 19 | struct iio_trigger *trig; |
Lars-Peter Clausen | f38bc92 | 2012-09-06 10:05:00 +0100 | [diff] [blame] | 20 | struct irq_work work; |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 21 | int id; |
| 22 | struct list_head l; |
| 23 | }; |
| 24 | |
| 25 | static LIST_HEAD(iio_sysfs_trig_list); |
Denis CIOCCA | 10a485c | 2013-09-23 11:49:00 +0100 | [diff] [blame] | 26 | static DEFINE_MUTEX(iio_sysfs_trig_list_mut); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 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 | |
Jingoo Han | ddeb64f | 2013-06-01 08:10:00 +0100 | [diff] [blame] | 37 | ret = kstrtoul(buf, 10, &input); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 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 | |
Jingoo Han | ddeb64f | 2013-06-01 08:10:00 +0100 | [diff] [blame] | 56 | ret = kstrtoul(buf, 10, &input); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 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 | |
Jonathan Cameron | 9c0c22b | 2011-08-24 17:28:35 +0100 | [diff] [blame] | 82 | |
| 83 | /* Nothing to actually do upon release */ |
| 84 | static void iio_trigger_sysfs_release(struct device *dev) |
| 85 | { |
| 86 | } |
| 87 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 88 | static struct device iio_sysfs_trig_dev = { |
| 89 | .bus = &iio_bus_type, |
| 90 | .groups = iio_sysfs_trig_groups, |
Jonathan Cameron | 9c0c22b | 2011-08-24 17:28:35 +0100 | [diff] [blame] | 91 | .release = &iio_trigger_sysfs_release, |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
Lars-Peter Clausen | f38bc92 | 2012-09-06 10:05:00 +0100 | [diff] [blame] | 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 | |
Peter Meerwald | 398fd22 | 2014-12-06 06:46:00 +0000 | [diff] [blame] | 99 | iio_trigger_poll(trig->trig); |
Lars-Peter Clausen | f38bc92 | 2012-09-06 10:05:00 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 102 | static ssize_t iio_sysfs_trigger_poll(struct device *dev, |
| 103 | struct device_attribute *attr, const char *buf, size_t count) |
| 104 | { |
Lars-Peter Clausen | 4bf8172 | 2012-06-21 19:10:59 +0200 | [diff] [blame] | 105 | struct iio_trigger *trig = to_iio_trigger(dev); |
Lars-Peter Clausen | 1e9663c | 2013-03-25 08:58:00 +0000 | [diff] [blame] | 106 | struct iio_sysfs_trig *sysfs_trig = iio_trigger_get_drvdata(trig); |
Lars-Peter Clausen | f38bc92 | 2012-09-06 10:05:00 +0100 | [diff] [blame] | 107 | |
| 108 | irq_work_queue(&sysfs_trig->work); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 109 | |
| 110 | return count; |
| 111 | } |
| 112 | |
| 113 | static DEVICE_ATTR(trigger_now, S_IWUSR, NULL, iio_sysfs_trigger_poll); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 114 | |
| 115 | static struct attribute *iio_sysfs_trigger_attrs[] = { |
| 116 | &dev_attr_trigger_now.attr, |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 117 | NULL, |
| 118 | }; |
| 119 | |
| 120 | static const struct attribute_group iio_sysfs_trigger_attr_group = { |
| 121 | .attrs = iio_sysfs_trigger_attrs, |
| 122 | }; |
| 123 | |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 124 | static const struct attribute_group *iio_sysfs_trigger_attr_groups[] = { |
| 125 | &iio_sysfs_trigger_attr_group, |
| 126 | NULL |
| 127 | }; |
| 128 | |
Jonathan Cameron | d29f73d | 2011-08-12 17:08:38 +0100 | [diff] [blame] | 129 | static const struct iio_trigger_ops iio_sysfs_trigger_ops = { |
| 130 | .owner = THIS_MODULE, |
| 131 | }; |
| 132 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 133 | static int iio_sysfs_trigger_probe(int id) |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 134 | { |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 135 | struct iio_sysfs_trig *t; |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 136 | int ret; |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 137 | bool foundit = false; |
Denis CIOCCA | 10a485c | 2013-09-23 11:49:00 +0100 | [diff] [blame] | 138 | mutex_lock(&iio_sysfs_trig_list_mut); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 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; |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 146 | goto out1; |
| 147 | } |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 148 | t = kmalloc(sizeof(*t), GFP_KERNEL); |
| 149 | if (t == NULL) { |
| 150 | ret = -ENOMEM; |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 151 | goto out1; |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 152 | } |
| 153 | t->id = id; |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 154 | t->trig = iio_trigger_alloc("sysfstrig%d", id); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 155 | if (!t->trig) { |
| 156 | ret = -ENOMEM; |
| 157 | goto free_t; |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Jonathan Cameron | 59c85e8 | 2011-05-18 14:42:22 +0100 | [diff] [blame] | 160 | t->trig->dev.groups = iio_sysfs_trigger_attr_groups; |
Jonathan Cameron | d29f73d | 2011-08-12 17:08:38 +0100 | [diff] [blame] | 161 | t->trig->ops = &iio_sysfs_trigger_ops; |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 162 | t->trig->dev.parent = &iio_sysfs_trig_dev; |
Lars-Peter Clausen | 1e9663c | 2013-03-25 08:58:00 +0000 | [diff] [blame] | 163 | iio_trigger_set_drvdata(t->trig, t); |
Lars-Peter Clausen | f38bc92 | 2012-09-06 10:05:00 +0100 | [diff] [blame] | 164 | |
| 165 | init_irq_work(&t->work, iio_sysfs_trigger_work); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 166 | |
| 167 | ret = iio_trigger_register(t->trig); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 168 | if (ret) |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 169 | goto out2; |
| 170 | list_add(&t->l, &iio_sysfs_trig_list); |
| 171 | __module_get(THIS_MODULE); |
Denis CIOCCA | 10a485c | 2013-09-23 11:49:00 +0100 | [diff] [blame] | 172 | mutex_unlock(&iio_sysfs_trig_list_mut); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 173 | return 0; |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 174 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 175 | out2: |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 176 | iio_trigger_put(t->trig); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 177 | free_t: |
| 178 | kfree(t); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 179 | out1: |
Denis CIOCCA | 10a485c | 2013-09-23 11:49:00 +0100 | [diff] [blame] | 180 | mutex_unlock(&iio_sysfs_trig_list_mut); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 181 | return ret; |
| 182 | } |
| 183 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 184 | static int iio_sysfs_trigger_remove(int id) |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 185 | { |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 186 | bool foundit = false; |
| 187 | struct iio_sysfs_trig *t; |
Denis CIOCCA | 10a485c | 2013-09-23 11:49:00 +0100 | [diff] [blame] | 188 | mutex_lock(&iio_sysfs_trig_list_mut); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 189 | list_for_each_entry(t, &iio_sysfs_trig_list, l) |
| 190 | if (id == t->id) { |
| 191 | foundit = true; |
| 192 | break; |
| 193 | } |
| 194 | if (!foundit) { |
Denis CIOCCA | 10a485c | 2013-09-23 11:49:00 +0100 | [diff] [blame] | 195 | mutex_unlock(&iio_sysfs_trig_list_mut); |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | } |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 198 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 199 | iio_trigger_unregister(t->trig); |
Lars-Peter Clausen | 7cbb753 | 2012-04-26 13:35:01 +0200 | [diff] [blame] | 200 | iio_trigger_free(t->trig); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 201 | |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 202 | list_del(&t->l); |
| 203 | kfree(t); |
| 204 | module_put(THIS_MODULE); |
Denis CIOCCA | 10a485c | 2013-09-23 11:49:00 +0100 | [diff] [blame] | 205 | mutex_unlock(&iio_sysfs_trig_list_mut); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 209 | |
| 210 | static int __init iio_sysfs_trig_init(void) |
| 211 | { |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 212 | device_initialize(&iio_sysfs_trig_dev); |
| 213 | dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger"); |
| 214 | return device_add(&iio_sysfs_trig_dev); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 215 | } |
| 216 | module_init(iio_sysfs_trig_init); |
| 217 | |
| 218 | static void __exit iio_sysfs_trig_exit(void) |
| 219 | { |
Jonathan Cameron | 1f78568 | 2011-05-18 14:42:11 +0100 | [diff] [blame] | 220 | device_unregister(&iio_sysfs_trig_dev); |
Michael Hennerich | f9a7e9b | 2011-02-07 11:05:46 +0100 | [diff] [blame] | 221 | } |
| 222 | module_exit(iio_sysfs_trig_exit); |
| 223 | |
| 224 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 225 | MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem"); |
| 226 | MODULE_LICENSE("GPL v2"); |
| 227 | MODULE_ALIAS("platform:iio-trig-sysfs"); |