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