Commit | Line | Data |
---|---|---|
0a769a95 LPC |
1 | /* Industrial I/O event handling |
2 | * | |
3 | * Copyright (c) 2008 Jonathan Cameron | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | * | |
9 | * Based on elements of hwmon and input subsystems. | |
10 | */ | |
11 | ||
12 | #include <linux/anon_inodes.h> | |
13 | #include <linux/device.h> | |
14 | #include <linux/fs.h> | |
15 | #include <linux/kernel.h> | |
2c00193f | 16 | #include <linux/kfifo.h> |
0a769a95 | 17 | #include <linux/module.h> |
e18045ed | 18 | #include <linux/poll.h> |
0a769a95 LPC |
19 | #include <linux/sched.h> |
20 | #include <linux/slab.h> | |
21 | #include <linux/uaccess.h> | |
22 | #include <linux/wait.h> | |
06458e27 | 23 | #include <linux/iio/iio.h> |
0a769a95 | 24 | #include "iio_core.h" |
06458e27 JC |
25 | #include <linux/iio/sysfs.h> |
26 | #include <linux/iio/events.h> | |
0a769a95 | 27 | |
0a769a95 LPC |
28 | /** |
29 | * struct iio_event_interface - chrdev interface for an event line | |
30 | * @wait: wait queue to allow blocking reads of events | |
0a769a95 | 31 | * @det_events: list of detected events |
0a769a95 LPC |
32 | * @dev_attr_list: list of event interface sysfs attribute |
33 | * @flags: file operations related flags including busy flag. | |
34 | * @group: event interface sysfs attribute group | |
35 | */ | |
36 | struct iio_event_interface { | |
37 | wait_queue_head_t wait; | |
2c00193f LPC |
38 | DECLARE_KFIFO(det_events, struct iio_event_data, 16); |
39 | ||
0a769a95 LPC |
40 | struct list_head dev_attr_list; |
41 | unsigned long flags; | |
42 | struct attribute_group group; | |
43 | }; | |
44 | ||
a7e57dce SK |
45 | /** |
46 | * iio_push_event() - try to add event to the list for userspace reading | |
47 | * @indio_dev: IIO device structure | |
48 | * @ev_code: What event | |
49 | * @timestamp: When the event occurred | |
50 | **/ | |
0a769a95 LPC |
51 | int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp) |
52 | { | |
53 | struct iio_event_interface *ev_int = indio_dev->event_interface; | |
2c00193f | 54 | struct iio_event_data ev; |
1b9dc91e | 55 | unsigned long flags; |
2c00193f | 56 | int copied; |
0a769a95 LPC |
57 | |
58 | /* Does anyone care? */ | |
1b9dc91e | 59 | spin_lock_irqsave(&ev_int->wait.lock, flags); |
0a769a95 | 60 | if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) { |
0a769a95 | 61 | |
2c00193f LPC |
62 | ev.id = ev_code; |
63 | ev.timestamp = timestamp; | |
64 | ||
498d319b | 65 | copied = kfifo_put(&ev_int->det_events, ev); |
2c00193f | 66 | if (copied != 0) |
e18045ed | 67 | wake_up_locked_poll(&ev_int->wait, POLLIN); |
43ba1100 | 68 | } |
1b9dc91e | 69 | spin_unlock_irqrestore(&ev_int->wait.lock, flags); |
0a769a95 | 70 | |
2c00193f | 71 | return 0; |
0a769a95 LPC |
72 | } |
73 | EXPORT_SYMBOL(iio_push_event); | |
74 | ||
e18045ed LPC |
75 | /** |
76 | * iio_event_poll() - poll the event queue to find out if it has data | |
77 | */ | |
78 | static unsigned int iio_event_poll(struct file *filep, | |
79 | struct poll_table_struct *wait) | |
80 | { | |
cadc2125 LPC |
81 | struct iio_dev *indio_dev = filep->private_data; |
82 | struct iio_event_interface *ev_int = indio_dev->event_interface; | |
e18045ed LPC |
83 | unsigned int events = 0; |
84 | ||
f18e7a06 LPC |
85 | if (!indio_dev->info) |
86 | return -ENODEV; | |
87 | ||
e18045ed LPC |
88 | poll_wait(filep, &ev_int->wait, wait); |
89 | ||
1b9dc91e | 90 | spin_lock_irq(&ev_int->wait.lock); |
e18045ed LPC |
91 | if (!kfifo_is_empty(&ev_int->det_events)) |
92 | events = POLLIN | POLLRDNORM; | |
1b9dc91e | 93 | spin_unlock_irq(&ev_int->wait.lock); |
e18045ed LPC |
94 | |
95 | return events; | |
96 | } | |
97 | ||
0a769a95 LPC |
98 | static ssize_t iio_event_chrdev_read(struct file *filep, |
99 | char __user *buf, | |
100 | size_t count, | |
101 | loff_t *f_ps) | |
102 | { | |
cadc2125 LPC |
103 | struct iio_dev *indio_dev = filep->private_data; |
104 | struct iio_event_interface *ev_int = indio_dev->event_interface; | |
2c00193f | 105 | unsigned int copied; |
0a769a95 LPC |
106 | int ret; |
107 | ||
f18e7a06 LPC |
108 | if (!indio_dev->info) |
109 | return -ENODEV; | |
110 | ||
2c00193f | 111 | if (count < sizeof(struct iio_event_data)) |
0a769a95 LPC |
112 | return -EINVAL; |
113 | ||
1b9dc91e | 114 | spin_lock_irq(&ev_int->wait.lock); |
2c00193f | 115 | if (kfifo_is_empty(&ev_int->det_events)) { |
0a769a95 LPC |
116 | if (filep->f_flags & O_NONBLOCK) { |
117 | ret = -EAGAIN; | |
43ba1100 | 118 | goto error_unlock; |
0a769a95 | 119 | } |
0a769a95 | 120 | /* Blocking on device; waiting for something to be there */ |
1b9dc91e | 121 | ret = wait_event_interruptible_locked_irq(ev_int->wait, |
d2f0a48f LPC |
122 | !kfifo_is_empty(&ev_int->det_events) || |
123 | indio_dev->info == NULL); | |
0a769a95 | 124 | if (ret) |
43ba1100 | 125 | goto error_unlock; |
d2f0a48f LPC |
126 | if (indio_dev->info == NULL) { |
127 | ret = -ENODEV; | |
128 | goto error_unlock; | |
129 | } | |
0a769a95 | 130 | /* Single access device so no one else can get the data */ |
0a769a95 LPC |
131 | } |
132 | ||
2c00193f | 133 | ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied); |
0a769a95 | 134 | |
43ba1100 | 135 | error_unlock: |
1b9dc91e | 136 | spin_unlock_irq(&ev_int->wait.lock); |
43ba1100 | 137 | |
2c00193f | 138 | return ret ? ret : copied; |
0a769a95 LPC |
139 | } |
140 | ||
141 | static int iio_event_chrdev_release(struct inode *inode, struct file *filep) | |
142 | { | |
cadc2125 LPC |
143 | struct iio_dev *indio_dev = filep->private_data; |
144 | struct iio_event_interface *ev_int = indio_dev->event_interface; | |
0a769a95 | 145 | |
1b9dc91e | 146 | spin_lock_irq(&ev_int->wait.lock); |
a046c1e8 | 147 | __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
0a769a95 LPC |
148 | /* |
149 | * In order to maintain a clean state for reopening, | |
150 | * clear out any awaiting events. The mask will prevent | |
151 | * any new __iio_push_event calls running. | |
152 | */ | |
2c00193f | 153 | kfifo_reset_out(&ev_int->det_events); |
1b9dc91e | 154 | spin_unlock_irq(&ev_int->wait.lock); |
0a769a95 | 155 | |
cadc2125 LPC |
156 | iio_device_put(indio_dev); |
157 | ||
0a769a95 LPC |
158 | return 0; |
159 | } | |
160 | ||
161 | static const struct file_operations iio_event_chrdev_fileops = { | |
162 | .read = iio_event_chrdev_read, | |
e18045ed | 163 | .poll = iio_event_poll, |
0a769a95 LPC |
164 | .release = iio_event_chrdev_release, |
165 | .owner = THIS_MODULE, | |
166 | .llseek = noop_llseek, | |
167 | }; | |
168 | ||
169 | int iio_event_getfd(struct iio_dev *indio_dev) | |
170 | { | |
171 | struct iio_event_interface *ev_int = indio_dev->event_interface; | |
172 | int fd; | |
173 | ||
174 | if (ev_int == NULL) | |
175 | return -ENODEV; | |
176 | ||
1b9dc91e | 177 | spin_lock_irq(&ev_int->wait.lock); |
a046c1e8 | 178 | if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) { |
1b9dc91e | 179 | spin_unlock_irq(&ev_int->wait.lock); |
0a769a95 LPC |
180 | return -EBUSY; |
181 | } | |
1b9dc91e | 182 | spin_unlock_irq(&ev_int->wait.lock); |
cadc2125 LPC |
183 | iio_device_get(indio_dev); |
184 | ||
185 | fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops, | |
e2aad1d5 | 186 | indio_dev, O_RDONLY | O_CLOEXEC); |
0a769a95 | 187 | if (fd < 0) { |
1b9dc91e | 188 | spin_lock_irq(&ev_int->wait.lock); |
a046c1e8 | 189 | __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
1b9dc91e | 190 | spin_unlock_irq(&ev_int->wait.lock); |
cadc2125 | 191 | iio_device_put(indio_dev); |
0a769a95 LPC |
192 | } |
193 | return fd; | |
194 | } | |
195 | ||
196 | static const char * const iio_ev_type_text[] = { | |
197 | [IIO_EV_TYPE_THRESH] = "thresh", | |
198 | [IIO_EV_TYPE_MAG] = "mag", | |
199 | [IIO_EV_TYPE_ROC] = "roc", | |
200 | [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive", | |
201 | [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive", | |
202 | }; | |
203 | ||
204 | static const char * const iio_ev_dir_text[] = { | |
205 | [IIO_EV_DIR_EITHER] = "either", | |
206 | [IIO_EV_DIR_RISING] = "rising", | |
207 | [IIO_EV_DIR_FALLING] = "falling" | |
208 | }; | |
209 | ||
b4e3ac0a LPC |
210 | static const char * const iio_ev_info_text[] = { |
211 | [IIO_EV_INFO_ENABLE] = "en", | |
212 | [IIO_EV_INFO_VALUE] = "value", | |
ec6670ae | 213 | [IIO_EV_INFO_HYSTERESIS] = "hysteresis", |
b4e3ac0a LPC |
214 | }; |
215 | ||
216 | static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr) | |
217 | { | |
218 | return attr->c->event_spec[attr->address & 0xffff].dir; | |
219 | } | |
220 | ||
221 | static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr) | |
222 | { | |
223 | return attr->c->event_spec[attr->address & 0xffff].type; | |
224 | } | |
225 | ||
226 | static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr) | |
227 | { | |
228 | return (attr->address >> 16) & 0xffff; | |
229 | } | |
230 | ||
0a769a95 LPC |
231 | static ssize_t iio_ev_state_store(struct device *dev, |
232 | struct device_attribute *attr, | |
233 | const char *buf, | |
234 | size_t len) | |
235 | { | |
e53f5ac5 | 236 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
0a769a95 LPC |
237 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
238 | int ret; | |
239 | bool val; | |
240 | ||
241 | ret = strtobool(buf, &val); | |
242 | if (ret < 0) | |
243 | return ret; | |
244 | ||
b4e3ac0a LPC |
245 | if (indio_dev->info->write_event_config) |
246 | ret = indio_dev->info->write_event_config(indio_dev, | |
247 | this_attr->address, val); | |
248 | else | |
249 | ret = indio_dev->info->write_event_config_new(indio_dev, | |
250 | this_attr->c, iio_ev_attr_type(this_attr), | |
251 | iio_ev_attr_dir(this_attr), val); | |
252 | ||
0a769a95 LPC |
253 | return (ret < 0) ? ret : len; |
254 | } | |
255 | ||
256 | static ssize_t iio_ev_state_show(struct device *dev, | |
257 | struct device_attribute *attr, | |
258 | char *buf) | |
259 | { | |
e53f5ac5 | 260 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
0a769a95 | 261 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
b4e3ac0a | 262 | int val; |
0a769a95 | 263 | |
b4e3ac0a LPC |
264 | if (indio_dev->info->read_event_config) |
265 | val = indio_dev->info->read_event_config(indio_dev, | |
266 | this_attr->address); | |
267 | else | |
268 | val = indio_dev->info->read_event_config_new(indio_dev, | |
269 | this_attr->c, iio_ev_attr_type(this_attr), | |
270 | iio_ev_attr_dir(this_attr)); | |
0a769a95 LPC |
271 | if (val < 0) |
272 | return val; | |
273 | else | |
274 | return sprintf(buf, "%d\n", val); | |
275 | } | |
276 | ||
277 | static ssize_t iio_ev_value_show(struct device *dev, | |
278 | struct device_attribute *attr, | |
279 | char *buf) | |
280 | { | |
e53f5ac5 | 281 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
0a769a95 | 282 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
b4e3ac0a LPC |
283 | int val, val2; |
284 | int ret; | |
0a769a95 | 285 | |
b4e3ac0a LPC |
286 | if (indio_dev->info->read_event_value) { |
287 | ret = indio_dev->info->read_event_value(indio_dev, | |
288 | this_attr->address, &val); | |
289 | if (ret < 0) | |
290 | return ret; | |
291 | return sprintf(buf, "%d\n", val); | |
292 | } else { | |
293 | ret = indio_dev->info->read_event_value_new(indio_dev, | |
294 | this_attr->c, iio_ev_attr_type(this_attr), | |
295 | iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr), | |
296 | &val, &val2); | |
297 | if (ret < 0) | |
298 | return ret; | |
299 | return iio_format_value(buf, ret, val, val2); | |
300 | } | |
0a769a95 LPC |
301 | } |
302 | ||
303 | static ssize_t iio_ev_value_store(struct device *dev, | |
304 | struct device_attribute *attr, | |
305 | const char *buf, | |
306 | size_t len) | |
307 | { | |
e53f5ac5 | 308 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
0a769a95 | 309 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
b4e3ac0a | 310 | int val, val2; |
0a769a95 LPC |
311 | int ret; |
312 | ||
b4e3ac0a LPC |
313 | if (!indio_dev->info->write_event_value && |
314 | !indio_dev->info->write_event_value_new) | |
0a769a95 LPC |
315 | return -EINVAL; |
316 | ||
b4e3ac0a LPC |
317 | if (indio_dev->info->write_event_value) { |
318 | ret = kstrtoint(buf, 10, &val); | |
319 | if (ret) | |
320 | return ret; | |
321 | ret = indio_dev->info->write_event_value(indio_dev, | |
322 | this_attr->address, val); | |
323 | } else { | |
324 | ret = iio_str_to_fixpoint(buf, 100000, &val, &val2); | |
325 | if (ret) | |
326 | return ret; | |
327 | ret = indio_dev->info->write_event_value_new(indio_dev, | |
328 | this_attr->c, iio_ev_attr_type(this_attr), | |
329 | iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr), | |
330 | val, val2); | |
331 | } | |
0a769a95 LPC |
332 | if (ret < 0) |
333 | return ret; | |
334 | ||
335 | return len; | |
336 | } | |
337 | ||
b4e3ac0a LPC |
338 | static int iio_device_add_event(struct iio_dev *indio_dev, |
339 | const struct iio_chan_spec *chan, unsigned int spec_index, | |
340 | enum iio_event_type type, enum iio_event_direction dir, | |
341 | enum iio_shared_by shared_by, const unsigned long *mask) | |
342 | { | |
343 | ssize_t (*show)(struct device *, struct device_attribute *, char *); | |
344 | ssize_t (*store)(struct device *, struct device_attribute *, | |
345 | const char *, size_t); | |
346 | unsigned int attrcount = 0; | |
347 | unsigned int i; | |
348 | char *postfix; | |
349 | int ret; | |
350 | ||
351 | for_each_set_bit(i, mask, sizeof(*mask)) { | |
352 | postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", | |
353 | iio_ev_type_text[type], iio_ev_dir_text[dir], | |
354 | iio_ev_info_text[i]); | |
355 | if (postfix == NULL) | |
356 | return -ENOMEM; | |
357 | ||
358 | if (i == IIO_EV_INFO_ENABLE) { | |
359 | show = iio_ev_state_show; | |
360 | store = iio_ev_state_store; | |
361 | } else { | |
362 | show = iio_ev_value_show; | |
363 | store = iio_ev_value_store; | |
364 | } | |
365 | ||
366 | ret = __iio_add_chan_devattr(postfix, chan, show, store, | |
367 | (i << 16) | spec_index, shared_by, &indio_dev->dev, | |
368 | &indio_dev->event_interface->dev_attr_list); | |
369 | kfree(postfix); | |
370 | ||
371 | if (ret) | |
372 | return ret; | |
373 | ||
374 | attrcount++; | |
375 | } | |
376 | ||
377 | return attrcount; | |
378 | } | |
379 | ||
380 | static int iio_device_add_event_sysfs_new(struct iio_dev *indio_dev, | |
381 | struct iio_chan_spec const *chan) | |
382 | { | |
383 | int ret = 0, i, attrcount = 0; | |
384 | enum iio_event_direction dir; | |
385 | enum iio_event_type type; | |
386 | ||
387 | for (i = 0; i < chan->num_event_specs; i++) { | |
388 | type = chan->event_spec[i].type; | |
389 | dir = chan->event_spec[i].dir; | |
390 | ||
391 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, | |
392 | IIO_SEPARATE, &chan->event_spec[i].mask_separate); | |
393 | if (ret < 0) | |
394 | goto error_ret; | |
395 | attrcount += ret; | |
396 | ||
397 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, | |
398 | IIO_SHARED_BY_TYPE, | |
399 | &chan->event_spec[i].mask_shared_by_type); | |
400 | if (ret < 0) | |
401 | goto error_ret; | |
402 | attrcount += ret; | |
403 | ||
404 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, | |
405 | IIO_SHARED_BY_DIR, | |
406 | &chan->event_spec[i].mask_shared_by_dir); | |
407 | if (ret < 0) | |
408 | goto error_ret; | |
409 | attrcount += ret; | |
410 | ||
411 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, | |
412 | IIO_SHARED_BY_ALL, | |
413 | &chan->event_spec[i].mask_shared_by_all); | |
414 | if (ret < 0) | |
415 | goto error_ret; | |
416 | attrcount += ret; | |
417 | } | |
418 | ret = attrcount; | |
419 | error_ret: | |
420 | return ret; | |
421 | } | |
422 | ||
423 | static int iio_device_add_event_sysfs_old(struct iio_dev *indio_dev, | |
0a769a95 LPC |
424 | struct iio_chan_spec const *chan) |
425 | { | |
426 | int ret = 0, i, attrcount = 0; | |
427 | u64 mask = 0; | |
428 | char *postfix; | |
429 | if (!chan->event_mask) | |
430 | return 0; | |
431 | ||
432 | for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) { | |
433 | postfix = kasprintf(GFP_KERNEL, "%s_%s_en", | |
434 | iio_ev_type_text[i/IIO_EV_DIR_MAX], | |
435 | iio_ev_dir_text[i%IIO_EV_DIR_MAX]); | |
436 | if (postfix == NULL) { | |
437 | ret = -ENOMEM; | |
438 | goto error_ret; | |
439 | } | |
440 | if (chan->modified) | |
2b0774df | 441 | mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2, |
0a769a95 LPC |
442 | i/IIO_EV_DIR_MAX, |
443 | i%IIO_EV_DIR_MAX); | |
444 | else if (chan->differential) | |
445 | mask = IIO_EVENT_CODE(chan->type, | |
446 | 0, 0, | |
447 | i%IIO_EV_DIR_MAX, | |
448 | i/IIO_EV_DIR_MAX, | |
449 | 0, | |
450 | chan->channel, | |
451 | chan->channel2); | |
452 | else | |
453 | mask = IIO_UNMOD_EVENT_CODE(chan->type, | |
454 | chan->channel, | |
455 | i/IIO_EV_DIR_MAX, | |
456 | i%IIO_EV_DIR_MAX); | |
457 | ||
458 | ret = __iio_add_chan_devattr(postfix, | |
459 | chan, | |
460 | &iio_ev_state_show, | |
461 | iio_ev_state_store, | |
462 | mask, | |
463 | 0, | |
464 | &indio_dev->dev, | |
465 | &indio_dev->event_interface-> | |
466 | dev_attr_list); | |
467 | kfree(postfix); | |
468 | if (ret) | |
469 | goto error_ret; | |
470 | attrcount++; | |
471 | postfix = kasprintf(GFP_KERNEL, "%s_%s_value", | |
472 | iio_ev_type_text[i/IIO_EV_DIR_MAX], | |
473 | iio_ev_dir_text[i%IIO_EV_DIR_MAX]); | |
474 | if (postfix == NULL) { | |
475 | ret = -ENOMEM; | |
476 | goto error_ret; | |
477 | } | |
478 | ret = __iio_add_chan_devattr(postfix, chan, | |
479 | iio_ev_value_show, | |
480 | iio_ev_value_store, | |
481 | mask, | |
482 | 0, | |
483 | &indio_dev->dev, | |
484 | &indio_dev->event_interface-> | |
485 | dev_attr_list); | |
486 | kfree(postfix); | |
487 | if (ret) | |
488 | goto error_ret; | |
489 | attrcount++; | |
490 | } | |
491 | ret = attrcount; | |
492 | error_ret: | |
493 | return ret; | |
494 | } | |
495 | ||
b4e3ac0a LPC |
496 | |
497 | static int iio_device_add_event_sysfs(struct iio_dev *indio_dev, | |
498 | struct iio_chan_spec const *chan) | |
499 | { | |
500 | if (chan->event_mask) | |
501 | return iio_device_add_event_sysfs_old(indio_dev, chan); | |
502 | else | |
503 | return iio_device_add_event_sysfs_new(indio_dev, chan); | |
504 | } | |
505 | ||
0a769a95 LPC |
506 | static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev) |
507 | { | |
508 | int j, ret, attrcount = 0; | |
509 | ||
0a769a95 LPC |
510 | /* Dynically created from the channels array */ |
511 | for (j = 0; j < indio_dev->num_channels; j++) { | |
512 | ret = iio_device_add_event_sysfs(indio_dev, | |
513 | &indio_dev->channels[j]); | |
514 | if (ret < 0) | |
e3db9ef6 | 515 | return ret; |
0a769a95 LPC |
516 | attrcount += ret; |
517 | } | |
518 | return attrcount; | |
0a769a95 LPC |
519 | } |
520 | ||
521 | static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev) | |
522 | { | |
523 | int j; | |
524 | ||
b4e3ac0a | 525 | for (j = 0; j < indio_dev->num_channels; j++) { |
0a769a95 LPC |
526 | if (indio_dev->channels[j].event_mask != 0) |
527 | return true; | |
b4e3ac0a LPC |
528 | if (indio_dev->channels[j].num_event_specs != 0) |
529 | return true; | |
530 | } | |
0a769a95 LPC |
531 | return false; |
532 | } | |
533 | ||
534 | static void iio_setup_ev_int(struct iio_event_interface *ev_int) | |
535 | { | |
2c00193f | 536 | INIT_KFIFO(ev_int->det_events); |
0a769a95 LPC |
537 | init_waitqueue_head(&ev_int->wait); |
538 | } | |
539 | ||
540 | static const char *iio_event_group_name = "events"; | |
541 | int iio_device_register_eventset(struct iio_dev *indio_dev) | |
542 | { | |
543 | struct iio_dev_attr *p; | |
544 | int ret = 0, attrcount_orig = 0, attrcount, attrn; | |
545 | struct attribute **attr; | |
546 | ||
547 | if (!(indio_dev->info->event_attrs || | |
548 | iio_check_for_dynamic_events(indio_dev))) | |
549 | return 0; | |
550 | ||
551 | indio_dev->event_interface = | |
552 | kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL); | |
553 | if (indio_dev->event_interface == NULL) { | |
554 | ret = -ENOMEM; | |
555 | goto error_ret; | |
556 | } | |
557 | ||
46b24311 SH |
558 | INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list); |
559 | ||
0a769a95 LPC |
560 | iio_setup_ev_int(indio_dev->event_interface); |
561 | if (indio_dev->info->event_attrs != NULL) { | |
562 | attr = indio_dev->info->event_attrs->attrs; | |
563 | while (*attr++ != NULL) | |
564 | attrcount_orig++; | |
565 | } | |
566 | attrcount = attrcount_orig; | |
567 | if (indio_dev->channels) { | |
568 | ret = __iio_add_event_config_attrs(indio_dev); | |
569 | if (ret < 0) | |
570 | goto error_free_setup_event_lines; | |
571 | attrcount += ret; | |
572 | } | |
573 | ||
574 | indio_dev->event_interface->group.name = iio_event_group_name; | |
575 | indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1, | |
576 | sizeof(indio_dev->event_interface->group.attrs[0]), | |
577 | GFP_KERNEL); | |
578 | if (indio_dev->event_interface->group.attrs == NULL) { | |
579 | ret = -ENOMEM; | |
580 | goto error_free_setup_event_lines; | |
581 | } | |
582 | if (indio_dev->info->event_attrs) | |
583 | memcpy(indio_dev->event_interface->group.attrs, | |
584 | indio_dev->info->event_attrs->attrs, | |
585 | sizeof(indio_dev->event_interface->group.attrs[0]) | |
586 | *attrcount_orig); | |
587 | attrn = attrcount_orig; | |
588 | /* Add all elements from the list. */ | |
589 | list_for_each_entry(p, | |
590 | &indio_dev->event_interface->dev_attr_list, | |
591 | l) | |
592 | indio_dev->event_interface->group.attrs[attrn++] = | |
593 | &p->dev_attr.attr; | |
594 | indio_dev->groups[indio_dev->groupcounter++] = | |
595 | &indio_dev->event_interface->group; | |
596 | ||
597 | return 0; | |
598 | ||
599 | error_free_setup_event_lines: | |
84088ebd | 600 | iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list); |
0a769a95 LPC |
601 | kfree(indio_dev->event_interface); |
602 | error_ret: | |
603 | ||
604 | return ret; | |
605 | } | |
606 | ||
d2f0a48f LPC |
607 | /** |
608 | * iio_device_wakeup_eventset - Wakes up the event waitqueue | |
609 | * @indio_dev: The IIO device | |
610 | * | |
611 | * Wakes up the event waitqueue used for poll() and blocking read(). | |
612 | * Should usually be called when the device is unregistered. | |
613 | */ | |
614 | void iio_device_wakeup_eventset(struct iio_dev *indio_dev) | |
615 | { | |
616 | if (indio_dev->event_interface == NULL) | |
617 | return; | |
618 | wake_up(&indio_dev->event_interface->wait); | |
619 | } | |
620 | ||
0a769a95 LPC |
621 | void iio_device_unregister_eventset(struct iio_dev *indio_dev) |
622 | { | |
623 | if (indio_dev->event_interface == NULL) | |
624 | return; | |
84088ebd | 625 | iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list); |
0a769a95 LPC |
626 | kfree(indio_dev->event_interface->group.attrs); |
627 | kfree(indio_dev->event_interface); | |
628 | } |