Commit | Line | Data |
---|---|---|
7026ea4b JC |
1 | /* The industrial I/O core |
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 | * | |
14555b14 | 9 | * Handling of buffer allocation / resizing. |
7026ea4b JC |
10 | * |
11 | * | |
12 | * Things to look at here. | |
13 | * - Better memory allocation techniques? | |
14 | * - Alternative access techniques? | |
15 | */ | |
16 | #include <linux/kernel.h> | |
8e336a72 | 17 | #include <linux/export.h> |
7026ea4b | 18 | #include <linux/device.h> |
7026ea4b | 19 | #include <linux/fs.h> |
7026ea4b | 20 | #include <linux/cdev.h> |
5a0e3ad6 | 21 | #include <linux/slab.h> |
a7348347 | 22 | #include <linux/poll.h> |
d2f0a48f | 23 | #include <linux/sched.h> |
7026ea4b | 24 | |
06458e27 | 25 | #include <linux/iio/iio.h> |
df9c1c42 | 26 | #include "iio_core.h" |
06458e27 JC |
27 | #include <linux/iio/sysfs.h> |
28 | #include <linux/iio/buffer.h> | |
7026ea4b | 29 | |
8310b86c JC |
30 | static const char * const iio_endian_prefix[] = { |
31 | [IIO_BE] = "be", | |
32 | [IIO_LE] = "le", | |
33 | }; | |
7026ea4b | 34 | |
705ee2c9 | 35 | static bool iio_buffer_is_active(struct iio_buffer *buf) |
84b36ce5 | 36 | { |
705ee2c9 | 37 | return !list_empty(&buf->buffer_list); |
84b36ce5 JC |
38 | } |
39 | ||
647cc7b9 LPC |
40 | static bool iio_buffer_data_available(struct iio_buffer *buf) |
41 | { | |
42 | if (buf->access->data_available) | |
43 | return buf->access->data_available(buf); | |
44 | ||
45 | return buf->stufftoread; | |
46 | } | |
47 | ||
7026ea4b | 48 | /** |
14555b14 | 49 | * iio_buffer_read_first_n_outer() - chrdev read for buffer access |
7026ea4b | 50 | * |
14555b14 JC |
51 | * This function relies on all buffer implementations having an |
52 | * iio_buffer as their first element. | |
7026ea4b | 53 | **/ |
14555b14 JC |
54 | ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf, |
55 | size_t n, loff_t *f_ps) | |
7026ea4b | 56 | { |
1aa04278 | 57 | struct iio_dev *indio_dev = filp->private_data; |
14555b14 | 58 | struct iio_buffer *rb = indio_dev->buffer; |
ee551a10 | 59 | int ret; |
d5857d65 | 60 | |
f18e7a06 LPC |
61 | if (!indio_dev->info) |
62 | return -ENODEV; | |
63 | ||
96e00f11 | 64 | if (!rb || !rb->access->read_first_n) |
7026ea4b | 65 | return -EINVAL; |
ee551a10 LPC |
66 | |
67 | do { | |
68 | if (!iio_buffer_data_available(rb)) { | |
69 | if (filp->f_flags & O_NONBLOCK) | |
70 | return -EAGAIN; | |
71 | ||
72 | ret = wait_event_interruptible(rb->pollq, | |
73 | iio_buffer_data_available(rb) || | |
74 | indio_dev->info == NULL); | |
75 | if (ret) | |
76 | return ret; | |
77 | if (indio_dev->info == NULL) | |
78 | return -ENODEV; | |
79 | } | |
80 | ||
81 | ret = rb->access->read_first_n(rb, n, buf); | |
82 | if (ret == 0 && (filp->f_flags & O_NONBLOCK)) | |
83 | ret = -EAGAIN; | |
84 | } while (ret == 0); | |
85 | ||
86 | return ret; | |
7026ea4b JC |
87 | } |
88 | ||
a7348347 | 89 | /** |
14555b14 | 90 | * iio_buffer_poll() - poll the buffer to find out if it has data |
a7348347 | 91 | */ |
14555b14 JC |
92 | unsigned int iio_buffer_poll(struct file *filp, |
93 | struct poll_table_struct *wait) | |
a7348347 | 94 | { |
1aa04278 | 95 | struct iio_dev *indio_dev = filp->private_data; |
14555b14 | 96 | struct iio_buffer *rb = indio_dev->buffer; |
a7348347 | 97 | |
f18e7a06 LPC |
98 | if (!indio_dev->info) |
99 | return -ENODEV; | |
100 | ||
a7348347 | 101 | poll_wait(filp, &rb->pollq, wait); |
647cc7b9 | 102 | if (iio_buffer_data_available(rb)) |
a7348347 JC |
103 | return POLLIN | POLLRDNORM; |
104 | /* need a way of knowing if there may be enough data... */ | |
8d213f24 | 105 | return 0; |
a7348347 JC |
106 | } |
107 | ||
d2f0a48f LPC |
108 | /** |
109 | * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue | |
110 | * @indio_dev: The IIO device | |
111 | * | |
112 | * Wakes up the event waitqueue used for poll(). Should usually | |
113 | * be called when the device is unregistered. | |
114 | */ | |
115 | void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) | |
116 | { | |
117 | if (!indio_dev->buffer) | |
118 | return; | |
119 | ||
120 | wake_up(&indio_dev->buffer->pollq); | |
121 | } | |
122 | ||
f79a9098 | 123 | void iio_buffer_init(struct iio_buffer *buffer) |
7026ea4b | 124 | { |
5ada4ea9 | 125 | INIT_LIST_HEAD(&buffer->demux_list); |
705ee2c9 | 126 | INIT_LIST_HEAD(&buffer->buffer_list); |
14555b14 | 127 | init_waitqueue_head(&buffer->pollq); |
9e69c935 | 128 | kref_init(&buffer->ref); |
7026ea4b | 129 | } |
14555b14 | 130 | EXPORT_SYMBOL(iio_buffer_init); |
7026ea4b | 131 | |
1d892719 | 132 | static ssize_t iio_show_scan_index(struct device *dev, |
8d213f24 JC |
133 | struct device_attribute *attr, |
134 | char *buf) | |
1d892719 | 135 | { |
8d213f24 | 136 | return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); |
1d892719 JC |
137 | } |
138 | ||
139 | static ssize_t iio_show_fixed_type(struct device *dev, | |
140 | struct device_attribute *attr, | |
141 | char *buf) | |
142 | { | |
143 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
8310b86c JC |
144 | u8 type = this_attr->c->scan_type.endianness; |
145 | ||
146 | if (type == IIO_CPU) { | |
9d5d1153 JC |
147 | #ifdef __LITTLE_ENDIAN |
148 | type = IIO_LE; | |
149 | #else | |
150 | type = IIO_BE; | |
151 | #endif | |
8310b86c JC |
152 | } |
153 | return sprintf(buf, "%s:%c%d/%d>>%u\n", | |
154 | iio_endian_prefix[type], | |
1d892719 JC |
155 | this_attr->c->scan_type.sign, |
156 | this_attr->c->scan_type.realbits, | |
157 | this_attr->c->scan_type.storagebits, | |
158 | this_attr->c->scan_type.shift); | |
159 | } | |
160 | ||
8d213f24 JC |
161 | static ssize_t iio_scan_el_show(struct device *dev, |
162 | struct device_attribute *attr, | |
163 | char *buf) | |
164 | { | |
165 | int ret; | |
e53f5ac5 | 166 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
8d213f24 | 167 | |
2076a20f AB |
168 | /* Ensure ret is 0 or 1. */ |
169 | ret = !!test_bit(to_iio_dev_attr(attr)->address, | |
5ada4ea9 JC |
170 | indio_dev->buffer->scan_mask); |
171 | ||
8d213f24 JC |
172 | return sprintf(buf, "%d\n", ret); |
173 | } | |
174 | ||
14555b14 | 175 | static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) |
8d213f24 | 176 | { |
14555b14 | 177 | clear_bit(bit, buffer->scan_mask); |
8d213f24 JC |
178 | return 0; |
179 | } | |
180 | ||
181 | static ssize_t iio_scan_el_store(struct device *dev, | |
182 | struct device_attribute *attr, | |
183 | const char *buf, | |
184 | size_t len) | |
185 | { | |
a714af27 | 186 | int ret; |
8d213f24 | 187 | bool state; |
e53f5ac5 | 188 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
14555b14 | 189 | struct iio_buffer *buffer = indio_dev->buffer; |
8d213f24 JC |
190 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
191 | ||
a714af27 JC |
192 | ret = strtobool(buf, &state); |
193 | if (ret < 0) | |
194 | return ret; | |
8d213f24 | 195 | mutex_lock(&indio_dev->mlock); |
705ee2c9 | 196 | if (iio_buffer_is_active(indio_dev->buffer)) { |
8d213f24 JC |
197 | ret = -EBUSY; |
198 | goto error_ret; | |
199 | } | |
f79a9098 | 200 | ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); |
8d213f24 JC |
201 | if (ret < 0) |
202 | goto error_ret; | |
203 | if (!state && ret) { | |
14555b14 | 204 | ret = iio_scan_mask_clear(buffer, this_attr->address); |
8d213f24 JC |
205 | if (ret) |
206 | goto error_ret; | |
207 | } else if (state && !ret) { | |
f79a9098 | 208 | ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); |
8d213f24 JC |
209 | if (ret) |
210 | goto error_ret; | |
211 | } | |
212 | ||
213 | error_ret: | |
214 | mutex_unlock(&indio_dev->mlock); | |
215 | ||
5a2a6e11 | 216 | return ret < 0 ? ret : len; |
8d213f24 JC |
217 | |
218 | } | |
219 | ||
220 | static ssize_t iio_scan_el_ts_show(struct device *dev, | |
221 | struct device_attribute *attr, | |
222 | char *buf) | |
223 | { | |
e53f5ac5 | 224 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
f8c6f4e9 | 225 | return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); |
8d213f24 JC |
226 | } |
227 | ||
228 | static ssize_t iio_scan_el_ts_store(struct device *dev, | |
229 | struct device_attribute *attr, | |
230 | const char *buf, | |
231 | size_t len) | |
232 | { | |
a714af27 | 233 | int ret; |
e53f5ac5 | 234 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
8d213f24 | 235 | bool state; |
1aa04278 | 236 | |
a714af27 JC |
237 | ret = strtobool(buf, &state); |
238 | if (ret < 0) | |
239 | return ret; | |
240 | ||
8d213f24 | 241 | mutex_lock(&indio_dev->mlock); |
705ee2c9 | 242 | if (iio_buffer_is_active(indio_dev->buffer)) { |
8d213f24 JC |
243 | ret = -EBUSY; |
244 | goto error_ret; | |
245 | } | |
14555b14 | 246 | indio_dev->buffer->scan_timestamp = state; |
8d213f24 JC |
247 | error_ret: |
248 | mutex_unlock(&indio_dev->mlock); | |
249 | ||
250 | return ret ? ret : len; | |
251 | } | |
252 | ||
14555b14 JC |
253 | static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, |
254 | const struct iio_chan_spec *chan) | |
1d892719 | 255 | { |
26d25ae3 | 256 | int ret, attrcount = 0; |
14555b14 | 257 | struct iio_buffer *buffer = indio_dev->buffer; |
1d892719 | 258 | |
26d25ae3 | 259 | ret = __iio_add_chan_devattr("index", |
1d892719 JC |
260 | chan, |
261 | &iio_show_scan_index, | |
262 | NULL, | |
263 | 0, | |
3704432f | 264 | IIO_SEPARATE, |
1aa04278 | 265 | &indio_dev->dev, |
14555b14 | 266 | &buffer->scan_el_dev_attr_list); |
1d892719 | 267 | if (ret) |
92825ff9 | 268 | return ret; |
26d25ae3 JC |
269 | attrcount++; |
270 | ret = __iio_add_chan_devattr("type", | |
1d892719 JC |
271 | chan, |
272 | &iio_show_fixed_type, | |
273 | NULL, | |
274 | 0, | |
275 | 0, | |
1aa04278 | 276 | &indio_dev->dev, |
14555b14 | 277 | &buffer->scan_el_dev_attr_list); |
1d892719 | 278 | if (ret) |
92825ff9 | 279 | return ret; |
26d25ae3 | 280 | attrcount++; |
a88b3ebc | 281 | if (chan->type != IIO_TIMESTAMP) |
26d25ae3 | 282 | ret = __iio_add_chan_devattr("en", |
a88b3ebc JC |
283 | chan, |
284 | &iio_scan_el_show, | |
285 | &iio_scan_el_store, | |
286 | chan->scan_index, | |
287 | 0, | |
1aa04278 | 288 | &indio_dev->dev, |
14555b14 | 289 | &buffer->scan_el_dev_attr_list); |
a88b3ebc | 290 | else |
26d25ae3 | 291 | ret = __iio_add_chan_devattr("en", |
a88b3ebc JC |
292 | chan, |
293 | &iio_scan_el_ts_show, | |
294 | &iio_scan_el_ts_store, | |
295 | chan->scan_index, | |
296 | 0, | |
1aa04278 | 297 | &indio_dev->dev, |
14555b14 | 298 | &buffer->scan_el_dev_attr_list); |
9572588c | 299 | if (ret) |
92825ff9 | 300 | return ret; |
26d25ae3 JC |
301 | attrcount++; |
302 | ret = attrcount; | |
1d892719 JC |
303 | return ret; |
304 | } | |
305 | ||
26d25ae3 JC |
306 | static const char * const iio_scan_elements_group_name = "scan_elements"; |
307 | ||
14555b14 JC |
308 | int iio_buffer_register(struct iio_dev *indio_dev, |
309 | const struct iio_chan_spec *channels, | |
310 | int num_channels) | |
1d892719 | 311 | { |
26d25ae3 JC |
312 | struct iio_dev_attr *p; |
313 | struct attribute **attr; | |
14555b14 | 314 | struct iio_buffer *buffer = indio_dev->buffer; |
26d25ae3 JC |
315 | int ret, i, attrn, attrcount, attrcount_orig = 0; |
316 | ||
14555b14 JC |
317 | if (buffer->attrs) |
318 | indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs; | |
bf32963c | 319 | |
14555b14 JC |
320 | if (buffer->scan_el_attrs != NULL) { |
321 | attr = buffer->scan_el_attrs->attrs; | |
26d25ae3 JC |
322 | while (*attr++ != NULL) |
323 | attrcount_orig++; | |
324 | } | |
325 | attrcount = attrcount_orig; | |
14555b14 | 326 | INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); |
1d892719 JC |
327 | if (channels) { |
328 | /* new magic */ | |
329 | for (i = 0; i < num_channels; i++) { | |
f5b81ddd LPC |
330 | if (channels[i].scan_index < 0) |
331 | continue; | |
332 | ||
32b5eeca JC |
333 | /* Establish necessary mask length */ |
334 | if (channels[i].scan_index > | |
335 | (int)indio_dev->masklength - 1) | |
336 | indio_dev->masklength | |
e1dc7bee | 337 | = channels[i].scan_index + 1; |
32b5eeca | 338 | |
14555b14 | 339 | ret = iio_buffer_add_channel_sysfs(indio_dev, |
1aa04278 | 340 | &channels[i]); |
1d892719 | 341 | if (ret < 0) |
26d25ae3 JC |
342 | goto error_cleanup_dynamic; |
343 | attrcount += ret; | |
beb80600 | 344 | if (channels[i].type == IIO_TIMESTAMP) |
f1264809 | 345 | indio_dev->scan_index_timestamp = |
beb80600 | 346 | channels[i].scan_index; |
1d892719 | 347 | } |
14555b14 | 348 | if (indio_dev->masklength && buffer->scan_mask == NULL) { |
d83fb184 TM |
349 | buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
350 | sizeof(*buffer->scan_mask), | |
351 | GFP_KERNEL); | |
14555b14 | 352 | if (buffer->scan_mask == NULL) { |
32b5eeca | 353 | ret = -ENOMEM; |
26d25ae3 | 354 | goto error_cleanup_dynamic; |
32b5eeca JC |
355 | } |
356 | } | |
1d892719 JC |
357 | } |
358 | ||
14555b14 | 359 | buffer->scan_el_group.name = iio_scan_elements_group_name; |
26d25ae3 | 360 | |
d83fb184 TM |
361 | buffer->scan_el_group.attrs = kcalloc(attrcount + 1, |
362 | sizeof(buffer->scan_el_group.attrs[0]), | |
363 | GFP_KERNEL); | |
14555b14 | 364 | if (buffer->scan_el_group.attrs == NULL) { |
26d25ae3 JC |
365 | ret = -ENOMEM; |
366 | goto error_free_scan_mask; | |
367 | } | |
14555b14 JC |
368 | if (buffer->scan_el_attrs) |
369 | memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, | |
370 | sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); | |
26d25ae3 JC |
371 | attrn = attrcount_orig; |
372 | ||
14555b14 JC |
373 | list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) |
374 | buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; | |
375 | indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; | |
26d25ae3 | 376 | |
1d892719 | 377 | return 0; |
26d25ae3 JC |
378 | |
379 | error_free_scan_mask: | |
14555b14 | 380 | kfree(buffer->scan_mask); |
1d892719 | 381 | error_cleanup_dynamic: |
84088ebd | 382 | iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); |
26d25ae3 | 383 | |
7026ea4b JC |
384 | return ret; |
385 | } | |
14555b14 | 386 | EXPORT_SYMBOL(iio_buffer_register); |
1d892719 | 387 | |
14555b14 | 388 | void iio_buffer_unregister(struct iio_dev *indio_dev) |
7026ea4b | 389 | { |
14555b14 JC |
390 | kfree(indio_dev->buffer->scan_mask); |
391 | kfree(indio_dev->buffer->scan_el_group.attrs); | |
84088ebd | 392 | iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); |
7026ea4b | 393 | } |
14555b14 | 394 | EXPORT_SYMBOL(iio_buffer_unregister); |
7026ea4b | 395 | |
14555b14 JC |
396 | ssize_t iio_buffer_read_length(struct device *dev, |
397 | struct device_attribute *attr, | |
398 | char *buf) | |
7026ea4b | 399 | { |
e53f5ac5 | 400 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
14555b14 | 401 | struct iio_buffer *buffer = indio_dev->buffer; |
7026ea4b | 402 | |
14555b14 | 403 | if (buffer->access->get_length) |
8d213f24 | 404 | return sprintf(buf, "%d\n", |
14555b14 | 405 | buffer->access->get_length(buffer)); |
7026ea4b | 406 | |
8d213f24 | 407 | return 0; |
7026ea4b | 408 | } |
14555b14 | 409 | EXPORT_SYMBOL(iio_buffer_read_length); |
7026ea4b | 410 | |
14555b14 JC |
411 | ssize_t iio_buffer_write_length(struct device *dev, |
412 | struct device_attribute *attr, | |
413 | const char *buf, | |
414 | size_t len) | |
7026ea4b | 415 | { |
e53f5ac5 | 416 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
14555b14 | 417 | struct iio_buffer *buffer = indio_dev->buffer; |
948ad205 LPC |
418 | unsigned int val; |
419 | int ret; | |
8d213f24 | 420 | |
948ad205 | 421 | ret = kstrtouint(buf, 10, &val); |
7026ea4b JC |
422 | if (ret) |
423 | return ret; | |
424 | ||
14555b14 JC |
425 | if (buffer->access->get_length) |
426 | if (val == buffer->access->get_length(buffer)) | |
7026ea4b JC |
427 | return len; |
428 | ||
e38c79e0 | 429 | mutex_lock(&indio_dev->mlock); |
705ee2c9 | 430 | if (iio_buffer_is_active(indio_dev->buffer)) { |
e38c79e0 LPC |
431 | ret = -EBUSY; |
432 | } else { | |
869871b5 | 433 | if (buffer->access->set_length) |
e38c79e0 | 434 | buffer->access->set_length(buffer, val); |
e38c79e0 | 435 | ret = 0; |
7026ea4b | 436 | } |
e38c79e0 | 437 | mutex_unlock(&indio_dev->mlock); |
7026ea4b | 438 | |
e38c79e0 | 439 | return ret ? ret : len; |
7026ea4b | 440 | } |
14555b14 | 441 | EXPORT_SYMBOL(iio_buffer_write_length); |
7026ea4b | 442 | |
14555b14 JC |
443 | ssize_t iio_buffer_show_enable(struct device *dev, |
444 | struct device_attribute *attr, | |
445 | char *buf) | |
7026ea4b | 446 | { |
e53f5ac5 | 447 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
705ee2c9 | 448 | return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); |
7026ea4b | 449 | } |
14555b14 | 450 | EXPORT_SYMBOL(iio_buffer_show_enable); |
7026ea4b | 451 | |
9572588c | 452 | /* Note NULL used as error indicator as it doesn't make sense. */ |
cd4361c7 | 453 | static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, |
32b5eeca | 454 | unsigned int masklength, |
cd4361c7 | 455 | const unsigned long *mask) |
32b5eeca JC |
456 | { |
457 | if (bitmap_empty(mask, masklength)) | |
458 | return NULL; | |
459 | while (*av_masks) { | |
460 | if (bitmap_subset(mask, av_masks, masklength)) | |
461 | return av_masks; | |
462 | av_masks += BITS_TO_LONGS(masklength); | |
463 | } | |
464 | return NULL; | |
465 | } | |
466 | ||
183f4173 PM |
467 | static int iio_compute_scan_bytes(struct iio_dev *indio_dev, |
468 | const unsigned long *mask, bool timestamp) | |
959d2952 | 469 | { |
959d2952 JC |
470 | const struct iio_chan_spec *ch; |
471 | unsigned bytes = 0; | |
472 | int length, i; | |
959d2952 JC |
473 | |
474 | /* How much space will the demuxed element take? */ | |
6b3b58ed | 475 | for_each_set_bit(i, mask, |
959d2952 JC |
476 | indio_dev->masklength) { |
477 | ch = iio_find_channel_from_si(indio_dev, i); | |
6b3b58ed | 478 | length = ch->scan_type.storagebits / 8; |
959d2952 JC |
479 | bytes = ALIGN(bytes, length); |
480 | bytes += length; | |
481 | } | |
6b3b58ed | 482 | if (timestamp) { |
959d2952 | 483 | ch = iio_find_channel_from_si(indio_dev, |
f1264809 | 484 | indio_dev->scan_index_timestamp); |
6b3b58ed | 485 | length = ch->scan_type.storagebits / 8; |
959d2952 JC |
486 | bytes = ALIGN(bytes, length); |
487 | bytes += length; | |
488 | } | |
6b3b58ed JC |
489 | return bytes; |
490 | } | |
491 | ||
9e69c935 LPC |
492 | static void iio_buffer_activate(struct iio_dev *indio_dev, |
493 | struct iio_buffer *buffer) | |
494 | { | |
495 | iio_buffer_get(buffer); | |
496 | list_add(&buffer->buffer_list, &indio_dev->buffer_list); | |
497 | } | |
498 | ||
499 | static void iio_buffer_deactivate(struct iio_buffer *buffer) | |
500 | { | |
501 | list_del_init(&buffer->buffer_list); | |
502 | iio_buffer_put(buffer); | |
503 | } | |
504 | ||
a87c82e4 LPC |
505 | void iio_disable_all_buffers(struct iio_dev *indio_dev) |
506 | { | |
507 | struct iio_buffer *buffer, *_buffer; | |
508 | ||
509 | if (list_empty(&indio_dev->buffer_list)) | |
510 | return; | |
511 | ||
512 | if (indio_dev->setup_ops->predisable) | |
513 | indio_dev->setup_ops->predisable(indio_dev); | |
514 | ||
515 | list_for_each_entry_safe(buffer, _buffer, | |
516 | &indio_dev->buffer_list, buffer_list) | |
9e69c935 | 517 | iio_buffer_deactivate(buffer); |
a87c82e4 LPC |
518 | |
519 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
520 | if (indio_dev->setup_ops->postdisable) | |
521 | indio_dev->setup_ops->postdisable(indio_dev); | |
e086ed76 LPC |
522 | |
523 | if (indio_dev->available_scan_masks == NULL) | |
524 | kfree(indio_dev->active_scan_mask); | |
a87c82e4 LPC |
525 | } |
526 | ||
8e050996 LPC |
527 | static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, |
528 | struct iio_buffer *buffer) | |
529 | { | |
530 | unsigned int bytes; | |
531 | ||
532 | if (!buffer->access->set_bytes_per_datum) | |
533 | return; | |
534 | ||
535 | bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, | |
536 | buffer->scan_timestamp); | |
537 | ||
538 | buffer->access->set_bytes_per_datum(buffer, bytes); | |
539 | } | |
540 | ||
a9519456 | 541 | static int __iio_update_buffers(struct iio_dev *indio_dev, |
84b36ce5 JC |
542 | struct iio_buffer *insert_buffer, |
543 | struct iio_buffer *remove_buffer) | |
6b3b58ed | 544 | { |
84b36ce5 JC |
545 | int ret; |
546 | int success = 0; | |
547 | struct iio_buffer *buffer; | |
548 | unsigned long *compound_mask; | |
549 | const unsigned long *old_mask; | |
6b3b58ed | 550 | |
84b36ce5 JC |
551 | /* Wind down existing buffers - iff there are any */ |
552 | if (!list_empty(&indio_dev->buffer_list)) { | |
553 | if (indio_dev->setup_ops->predisable) { | |
554 | ret = indio_dev->setup_ops->predisable(indio_dev); | |
555 | if (ret) | |
92825ff9 | 556 | return ret; |
84b36ce5 JC |
557 | } |
558 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
559 | if (indio_dev->setup_ops->postdisable) { | |
560 | ret = indio_dev->setup_ops->postdisable(indio_dev); | |
561 | if (ret) | |
92825ff9 | 562 | return ret; |
84b36ce5 JC |
563 | } |
564 | } | |
565 | /* Keep a copy of current setup to allow roll back */ | |
566 | old_mask = indio_dev->active_scan_mask; | |
567 | if (!indio_dev->available_scan_masks) | |
568 | indio_dev->active_scan_mask = NULL; | |
569 | ||
570 | if (remove_buffer) | |
9e69c935 | 571 | iio_buffer_deactivate(remove_buffer); |
84b36ce5 | 572 | if (insert_buffer) |
9e69c935 | 573 | iio_buffer_activate(indio_dev, insert_buffer); |
84b36ce5 JC |
574 | |
575 | /* If no buffers in list, we are done */ | |
576 | if (list_empty(&indio_dev->buffer_list)) { | |
577 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
578 | if (indio_dev->available_scan_masks == NULL) | |
579 | kfree(old_mask); | |
580 | return 0; | |
581 | } | |
959d2952 | 582 | |
9572588c | 583 | /* What scan mask do we actually have? */ |
84b36ce5 JC |
584 | compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
585 | sizeof(long), GFP_KERNEL); | |
586 | if (compound_mask == NULL) { | |
587 | if (indio_dev->available_scan_masks == NULL) | |
588 | kfree(old_mask); | |
589 | return -ENOMEM; | |
590 | } | |
591 | indio_dev->scan_timestamp = 0; | |
592 | ||
593 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { | |
594 | bitmap_or(compound_mask, compound_mask, buffer->scan_mask, | |
595 | indio_dev->masklength); | |
596 | indio_dev->scan_timestamp |= buffer->scan_timestamp; | |
597 | } | |
598 | if (indio_dev->available_scan_masks) { | |
959d2952 JC |
599 | indio_dev->active_scan_mask = |
600 | iio_scan_mask_match(indio_dev->available_scan_masks, | |
601 | indio_dev->masklength, | |
84b36ce5 JC |
602 | compound_mask); |
603 | if (indio_dev->active_scan_mask == NULL) { | |
604 | /* | |
605 | * Roll back. | |
606 | * Note can only occur when adding a buffer. | |
607 | */ | |
9e69c935 | 608 | iio_buffer_deactivate(insert_buffer); |
d66e0452 PM |
609 | if (old_mask) { |
610 | indio_dev->active_scan_mask = old_mask; | |
611 | success = -EINVAL; | |
612 | } | |
613 | else { | |
614 | kfree(compound_mask); | |
615 | ret = -EINVAL; | |
92825ff9 | 616 | return ret; |
d66e0452 | 617 | } |
84b36ce5 JC |
618 | } |
619 | } else { | |
620 | indio_dev->active_scan_mask = compound_mask; | |
621 | } | |
aff1eb4e | 622 | |
5ada4ea9 JC |
623 | iio_update_demux(indio_dev); |
624 | ||
84b36ce5 JC |
625 | /* Wind up again */ |
626 | if (indio_dev->setup_ops->preenable) { | |
627 | ret = indio_dev->setup_ops->preenable(indio_dev); | |
628 | if (ret) { | |
629 | printk(KERN_ERR | |
bec1889d | 630 | "Buffer not started: buffer preenable failed (%d)\n", ret); |
84b36ce5 JC |
631 | goto error_remove_inserted; |
632 | } | |
633 | } | |
634 | indio_dev->scan_bytes = | |
635 | iio_compute_scan_bytes(indio_dev, | |
636 | indio_dev->active_scan_mask, | |
637 | indio_dev->scan_timestamp); | |
8e050996 LPC |
638 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { |
639 | iio_buffer_update_bytes_per_datum(indio_dev, buffer); | |
84b36ce5 JC |
640 | if (buffer->access->request_update) { |
641 | ret = buffer->access->request_update(buffer); | |
642 | if (ret) { | |
643 | printk(KERN_INFO | |
bec1889d | 644 | "Buffer not started: buffer parameter update failed (%d)\n", ret); |
84b36ce5 JC |
645 | goto error_run_postdisable; |
646 | } | |
647 | } | |
8e050996 | 648 | } |
84b36ce5 JC |
649 | if (indio_dev->info->update_scan_mode) { |
650 | ret = indio_dev->info | |
5ada4ea9 JC |
651 | ->update_scan_mode(indio_dev, |
652 | indio_dev->active_scan_mask); | |
84b36ce5 | 653 | if (ret < 0) { |
bec1889d | 654 | printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret); |
84b36ce5 JC |
655 | goto error_run_postdisable; |
656 | } | |
657 | } | |
9572588c | 658 | /* Definitely possible for devices to support both of these. */ |
84b36ce5 JC |
659 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { |
660 | if (!indio_dev->trig) { | |
661 | printk(KERN_INFO "Buffer not started: no trigger\n"); | |
662 | ret = -EINVAL; | |
663 | /* Can only occur on first buffer */ | |
664 | goto error_run_postdisable; | |
665 | } | |
666 | indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; | |
667 | } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { | |
668 | indio_dev->currentmode = INDIO_BUFFER_HARDWARE; | |
9572588c | 669 | } else { /* Should never be reached */ |
84b36ce5 JC |
670 | ret = -EINVAL; |
671 | goto error_run_postdisable; | |
672 | } | |
673 | ||
674 | if (indio_dev->setup_ops->postenable) { | |
675 | ret = indio_dev->setup_ops->postenable(indio_dev); | |
676 | if (ret) { | |
677 | printk(KERN_INFO | |
bec1889d | 678 | "Buffer not started: postenable failed (%d)\n", ret); |
84b36ce5 JC |
679 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
680 | if (indio_dev->setup_ops->postdisable) | |
681 | indio_dev->setup_ops->postdisable(indio_dev); | |
682 | goto error_disable_all_buffers; | |
683 | } | |
684 | } | |
685 | ||
686 | if (indio_dev->available_scan_masks) | |
687 | kfree(compound_mask); | |
688 | else | |
689 | kfree(old_mask); | |
690 | ||
691 | return success; | |
692 | ||
693 | error_disable_all_buffers: | |
694 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
695 | error_run_postdisable: | |
696 | if (indio_dev->setup_ops->postdisable) | |
697 | indio_dev->setup_ops->postdisable(indio_dev); | |
698 | error_remove_inserted: | |
84b36ce5 | 699 | if (insert_buffer) |
9e69c935 | 700 | iio_buffer_deactivate(insert_buffer); |
84b36ce5 JC |
701 | indio_dev->active_scan_mask = old_mask; |
702 | kfree(compound_mask); | |
84b36ce5 JC |
703 | return ret; |
704 | } | |
a9519456 LPC |
705 | |
706 | int iio_update_buffers(struct iio_dev *indio_dev, | |
707 | struct iio_buffer *insert_buffer, | |
708 | struct iio_buffer *remove_buffer) | |
709 | { | |
710 | int ret; | |
711 | ||
3909fab5 LPC |
712 | if (insert_buffer == remove_buffer) |
713 | return 0; | |
714 | ||
a9519456 LPC |
715 | mutex_lock(&indio_dev->info_exist_lock); |
716 | mutex_lock(&indio_dev->mlock); | |
717 | ||
3909fab5 LPC |
718 | if (insert_buffer && iio_buffer_is_active(insert_buffer)) |
719 | insert_buffer = NULL; | |
720 | ||
721 | if (remove_buffer && !iio_buffer_is_active(remove_buffer)) | |
722 | remove_buffer = NULL; | |
723 | ||
724 | if (!insert_buffer && !remove_buffer) { | |
725 | ret = 0; | |
726 | goto out_unlock; | |
727 | } | |
728 | ||
a9519456 LPC |
729 | if (indio_dev->info == NULL) { |
730 | ret = -ENODEV; | |
731 | goto out_unlock; | |
732 | } | |
733 | ||
734 | ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); | |
735 | ||
736 | out_unlock: | |
737 | mutex_unlock(&indio_dev->mlock); | |
738 | mutex_unlock(&indio_dev->info_exist_lock); | |
739 | ||
740 | return ret; | |
741 | } | |
84b36ce5 JC |
742 | EXPORT_SYMBOL_GPL(iio_update_buffers); |
743 | ||
744 | ssize_t iio_buffer_store_enable(struct device *dev, | |
745 | struct device_attribute *attr, | |
746 | const char *buf, | |
747 | size_t len) | |
748 | { | |
749 | int ret; | |
750 | bool requested_state; | |
751 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
84b36ce5 JC |
752 | bool inlist; |
753 | ||
754 | ret = strtobool(buf, &requested_state); | |
755 | if (ret < 0) | |
756 | return ret; | |
757 | ||
758 | mutex_lock(&indio_dev->mlock); | |
759 | ||
760 | /* Find out if it is in the list */ | |
705ee2c9 | 761 | inlist = iio_buffer_is_active(indio_dev->buffer); |
84b36ce5 JC |
762 | /* Already in desired state */ |
763 | if (inlist == requested_state) | |
764 | goto done; | |
765 | ||
766 | if (requested_state) | |
a9519456 | 767 | ret = __iio_update_buffers(indio_dev, |
84b36ce5 JC |
768 | indio_dev->buffer, NULL); |
769 | else | |
a9519456 | 770 | ret = __iio_update_buffers(indio_dev, |
84b36ce5 JC |
771 | NULL, indio_dev->buffer); |
772 | ||
773 | if (ret < 0) | |
774 | goto done; | |
775 | done: | |
776 | mutex_unlock(&indio_dev->mlock); | |
777 | return (ret < 0) ? ret : len; | |
778 | } | |
779 | EXPORT_SYMBOL(iio_buffer_store_enable); | |
780 | ||
81636632 LPC |
781 | /** |
782 | * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected | |
783 | * @indio_dev: the iio device | |
784 | * @mask: scan mask to be checked | |
785 | * | |
786 | * Return true if exactly one bit is set in the scan mask, false otherwise. It | |
787 | * can be used for devices where only one channel can be active for sampling at | |
788 | * a time. | |
789 | */ | |
790 | bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, | |
791 | const unsigned long *mask) | |
792 | { | |
793 | return bitmap_weight(mask, indio_dev->masklength) == 1; | |
794 | } | |
795 | EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); | |
796 | ||
939546d1 LPC |
797 | static bool iio_validate_scan_mask(struct iio_dev *indio_dev, |
798 | const unsigned long *mask) | |
799 | { | |
800 | if (!indio_dev->setup_ops->validate_scan_mask) | |
801 | return true; | |
802 | ||
803 | return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); | |
804 | } | |
805 | ||
32b5eeca JC |
806 | /** |
807 | * iio_scan_mask_set() - set particular bit in the scan mask | |
9572588c | 808 | * @indio_dev: the iio device |
14555b14 | 809 | * @buffer: the buffer whose scan mask we are interested in |
32b5eeca | 810 | * @bit: the bit to be set. |
84b36ce5 JC |
811 | * |
812 | * Note that at this point we have no way of knowing what other | |
813 | * buffers might request, hence this code only verifies that the | |
814 | * individual buffers request is plausible. | |
815 | */ | |
f79a9098 JC |
816 | int iio_scan_mask_set(struct iio_dev *indio_dev, |
817 | struct iio_buffer *buffer, int bit) | |
32b5eeca | 818 | { |
cd4361c7 | 819 | const unsigned long *mask; |
32b5eeca JC |
820 | unsigned long *trialmask; |
821 | ||
822 | trialmask = kmalloc(sizeof(*trialmask)* | |
f8c6f4e9 | 823 | BITS_TO_LONGS(indio_dev->masklength), |
32b5eeca JC |
824 | GFP_KERNEL); |
825 | ||
826 | if (trialmask == NULL) | |
827 | return -ENOMEM; | |
f8c6f4e9 | 828 | if (!indio_dev->masklength) { |
9572588c | 829 | WARN_ON("Trying to set scanmask prior to registering buffer\n"); |
939546d1 | 830 | goto err_invalid_mask; |
32b5eeca | 831 | } |
f8c6f4e9 | 832 | bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); |
32b5eeca JC |
833 | set_bit(bit, trialmask); |
834 | ||
939546d1 LPC |
835 | if (!iio_validate_scan_mask(indio_dev, trialmask)) |
836 | goto err_invalid_mask; | |
837 | ||
f8c6f4e9 JC |
838 | if (indio_dev->available_scan_masks) { |
839 | mask = iio_scan_mask_match(indio_dev->available_scan_masks, | |
840 | indio_dev->masklength, | |
32b5eeca | 841 | trialmask); |
939546d1 LPC |
842 | if (!mask) |
843 | goto err_invalid_mask; | |
32b5eeca | 844 | } |
f8c6f4e9 | 845 | bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); |
32b5eeca JC |
846 | |
847 | kfree(trialmask); | |
848 | ||
849 | return 0; | |
939546d1 LPC |
850 | |
851 | err_invalid_mask: | |
852 | kfree(trialmask); | |
853 | return -EINVAL; | |
854 | } | |
32b5eeca JC |
855 | EXPORT_SYMBOL_GPL(iio_scan_mask_set); |
856 | ||
f79a9098 JC |
857 | int iio_scan_mask_query(struct iio_dev *indio_dev, |
858 | struct iio_buffer *buffer, int bit) | |
32b5eeca | 859 | { |
f8c6f4e9 | 860 | if (bit > indio_dev->masklength) |
32b5eeca JC |
861 | return -EINVAL; |
862 | ||
14555b14 | 863 | if (!buffer->scan_mask) |
32b5eeca | 864 | return 0; |
32b5eeca | 865 | |
2076a20f AB |
866 | /* Ensure return value is 0 or 1. */ |
867 | return !!test_bit(bit, buffer->scan_mask); | |
32b5eeca JC |
868 | }; |
869 | EXPORT_SYMBOL_GPL(iio_scan_mask_query); | |
5ada4ea9 JC |
870 | |
871 | /** | |
872 | * struct iio_demux_table() - table describing demux memcpy ops | |
873 | * @from: index to copy from | |
99698b45 | 874 | * @to: index to copy to |
5ada4ea9 JC |
875 | * @length: how many bytes to copy |
876 | * @l: list head used for management | |
877 | */ | |
878 | struct iio_demux_table { | |
879 | unsigned from; | |
880 | unsigned to; | |
881 | unsigned length; | |
882 | struct list_head l; | |
883 | }; | |
884 | ||
5d65d920 LPC |
885 | static const void *iio_demux(struct iio_buffer *buffer, |
886 | const void *datain) | |
5ada4ea9 JC |
887 | { |
888 | struct iio_demux_table *t; | |
889 | ||
890 | if (list_empty(&buffer->demux_list)) | |
891 | return datain; | |
892 | list_for_each_entry(t, &buffer->demux_list, l) | |
893 | memcpy(buffer->demux_bounce + t->to, | |
894 | datain + t->from, t->length); | |
895 | ||
896 | return buffer->demux_bounce; | |
897 | } | |
898 | ||
5d65d920 | 899 | static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) |
5ada4ea9 | 900 | { |
5d65d920 | 901 | const void *dataout = iio_demux(buffer, data); |
5ada4ea9 | 902 | |
ce56ade6 | 903 | return buffer->access->store_to(buffer, dataout); |
5ada4ea9 | 904 | } |
5ada4ea9 | 905 | |
842cd100 JC |
906 | static void iio_buffer_demux_free(struct iio_buffer *buffer) |
907 | { | |
908 | struct iio_demux_table *p, *q; | |
909 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { | |
910 | list_del(&p->l); | |
911 | kfree(p); | |
912 | } | |
913 | } | |
914 | ||
84b36ce5 | 915 | |
5d65d920 | 916 | int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) |
84b36ce5 JC |
917 | { |
918 | int ret; | |
919 | struct iio_buffer *buf; | |
920 | ||
921 | list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { | |
922 | ret = iio_push_to_buffer(buf, data); | |
923 | if (ret < 0) | |
924 | return ret; | |
925 | } | |
926 | ||
927 | return 0; | |
928 | } | |
929 | EXPORT_SYMBOL_GPL(iio_push_to_buffers); | |
930 | ||
931 | static int iio_buffer_update_demux(struct iio_dev *indio_dev, | |
932 | struct iio_buffer *buffer) | |
5ada4ea9 JC |
933 | { |
934 | const struct iio_chan_spec *ch; | |
5ada4ea9 JC |
935 | int ret, in_ind = -1, out_ind, length; |
936 | unsigned in_loc = 0, out_loc = 0; | |
842cd100 | 937 | struct iio_demux_table *p; |
5ada4ea9 JC |
938 | |
939 | /* Clear out any old demux */ | |
842cd100 | 940 | iio_buffer_demux_free(buffer); |
5ada4ea9 JC |
941 | kfree(buffer->demux_bounce); |
942 | buffer->demux_bounce = NULL; | |
943 | ||
944 | /* First work out which scan mode we will actually have */ | |
945 | if (bitmap_equal(indio_dev->active_scan_mask, | |
946 | buffer->scan_mask, | |
947 | indio_dev->masklength)) | |
948 | return 0; | |
949 | ||
950 | /* Now we have the two masks, work from least sig and build up sizes */ | |
951 | for_each_set_bit(out_ind, | |
952 | indio_dev->active_scan_mask, | |
953 | indio_dev->masklength) { | |
954 | in_ind = find_next_bit(indio_dev->active_scan_mask, | |
955 | indio_dev->masklength, | |
956 | in_ind + 1); | |
957 | while (in_ind != out_ind) { | |
958 | in_ind = find_next_bit(indio_dev->active_scan_mask, | |
959 | indio_dev->masklength, | |
960 | in_ind + 1); | |
961 | ch = iio_find_channel_from_si(indio_dev, in_ind); | |
962 | length = ch->scan_type.storagebits/8; | |
963 | /* Make sure we are aligned */ | |
964 | in_loc += length; | |
965 | if (in_loc % length) | |
966 | in_loc += length - in_loc % length; | |
967 | } | |
968 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
969 | if (p == NULL) { | |
970 | ret = -ENOMEM; | |
971 | goto error_clear_mux_table; | |
972 | } | |
973 | ch = iio_find_channel_from_si(indio_dev, in_ind); | |
974 | length = ch->scan_type.storagebits/8; | |
975 | if (out_loc % length) | |
976 | out_loc += length - out_loc % length; | |
977 | if (in_loc % length) | |
978 | in_loc += length - in_loc % length; | |
979 | p->from = in_loc; | |
980 | p->to = out_loc; | |
981 | p->length = length; | |
982 | list_add_tail(&p->l, &buffer->demux_list); | |
983 | out_loc += length; | |
984 | in_loc += length; | |
985 | } | |
986 | /* Relies on scan_timestamp being last */ | |
987 | if (buffer->scan_timestamp) { | |
988 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
989 | if (p == NULL) { | |
990 | ret = -ENOMEM; | |
991 | goto error_clear_mux_table; | |
992 | } | |
993 | ch = iio_find_channel_from_si(indio_dev, | |
f1264809 | 994 | indio_dev->scan_index_timestamp); |
5ada4ea9 JC |
995 | length = ch->scan_type.storagebits/8; |
996 | if (out_loc % length) | |
997 | out_loc += length - out_loc % length; | |
998 | if (in_loc % length) | |
999 | in_loc += length - in_loc % length; | |
1000 | p->from = in_loc; | |
1001 | p->to = out_loc; | |
1002 | p->length = length; | |
1003 | list_add_tail(&p->l, &buffer->demux_list); | |
1004 | out_loc += length; | |
1005 | in_loc += length; | |
1006 | } | |
1007 | buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); | |
1008 | if (buffer->demux_bounce == NULL) { | |
1009 | ret = -ENOMEM; | |
1010 | goto error_clear_mux_table; | |
1011 | } | |
1012 | return 0; | |
1013 | ||
1014 | error_clear_mux_table: | |
842cd100 JC |
1015 | iio_buffer_demux_free(buffer); |
1016 | ||
5ada4ea9 JC |
1017 | return ret; |
1018 | } | |
84b36ce5 JC |
1019 | |
1020 | int iio_update_demux(struct iio_dev *indio_dev) | |
1021 | { | |
1022 | struct iio_buffer *buffer; | |
1023 | int ret; | |
1024 | ||
1025 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { | |
1026 | ret = iio_buffer_update_demux(indio_dev, buffer); | |
1027 | if (ret < 0) | |
1028 | goto error_clear_mux_table; | |
1029 | } | |
1030 | return 0; | |
1031 | ||
1032 | error_clear_mux_table: | |
1033 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) | |
1034 | iio_buffer_demux_free(buffer); | |
1035 | ||
1036 | return ret; | |
1037 | } | |
5ada4ea9 | 1038 | EXPORT_SYMBOL_GPL(iio_update_demux); |
9e69c935 LPC |
1039 | |
1040 | /** | |
1041 | * iio_buffer_release() - Free a buffer's resources | |
1042 | * @ref: Pointer to the kref embedded in the iio_buffer struct | |
1043 | * | |
1044 | * This function is called when the last reference to the buffer has been | |
1045 | * dropped. It will typically free all resources allocated by the buffer. Do not | |
1046 | * call this function manually, always use iio_buffer_put() when done using a | |
1047 | * buffer. | |
1048 | */ | |
1049 | static void iio_buffer_release(struct kref *ref) | |
1050 | { | |
1051 | struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); | |
1052 | ||
1053 | buffer->access->release(buffer); | |
1054 | } | |
1055 | ||
1056 | /** | |
1057 | * iio_buffer_get() - Grab a reference to the buffer | |
1058 | * @buffer: The buffer to grab a reference for, may be NULL | |
1059 | * | |
1060 | * Returns the pointer to the buffer that was passed into the function. | |
1061 | */ | |
1062 | struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) | |
1063 | { | |
1064 | if (buffer) | |
1065 | kref_get(&buffer->ref); | |
1066 | ||
1067 | return buffer; | |
1068 | } | |
1069 | EXPORT_SYMBOL_GPL(iio_buffer_get); | |
1070 | ||
1071 | /** | |
1072 | * iio_buffer_put() - Release the reference to the buffer | |
1073 | * @buffer: The buffer to release the reference for, may be NULL | |
1074 | */ | |
1075 | void iio_buffer_put(struct iio_buffer *buffer) | |
1076 | { | |
1077 | if (buffer) | |
1078 | kref_put(&buffer->ref, iio_buffer_release); | |
1079 | } | |
1080 | EXPORT_SYMBOL_GPL(iio_buffer_put); |