]>
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 | ||
26d25ae3 JC |
386 | static const char * const iio_scan_elements_group_name = "scan_elements"; |
387 | ||
3e1b6c95 | 388 | int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) |
1d892719 | 389 | { |
26d25ae3 JC |
390 | struct iio_dev_attr *p; |
391 | struct attribute **attr; | |
14555b14 | 392 | struct iio_buffer *buffer = indio_dev->buffer; |
26d25ae3 | 393 | int ret, i, attrn, attrcount, attrcount_orig = 0; |
3e1b6c95 LPC |
394 | const struct iio_chan_spec *channels; |
395 | ||
396 | if (!buffer) | |
397 | return 0; | |
26d25ae3 | 398 | |
14555b14 JC |
399 | if (buffer->attrs) |
400 | indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs; | |
bf32963c | 401 | |
14555b14 JC |
402 | if (buffer->scan_el_attrs != NULL) { |
403 | attr = buffer->scan_el_attrs->attrs; | |
26d25ae3 JC |
404 | while (*attr++ != NULL) |
405 | attrcount_orig++; | |
406 | } | |
407 | attrcount = attrcount_orig; | |
14555b14 | 408 | INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); |
3e1b6c95 | 409 | channels = indio_dev->channels; |
1d892719 JC |
410 | if (channels) { |
411 | /* new magic */ | |
3e1b6c95 | 412 | for (i = 0; i < indio_dev->num_channels; i++) { |
f5b81ddd LPC |
413 | if (channels[i].scan_index < 0) |
414 | continue; | |
415 | ||
32b5eeca JC |
416 | /* Establish necessary mask length */ |
417 | if (channels[i].scan_index > | |
418 | (int)indio_dev->masklength - 1) | |
419 | indio_dev->masklength | |
e1dc7bee | 420 | = channels[i].scan_index + 1; |
32b5eeca | 421 | |
14555b14 | 422 | ret = iio_buffer_add_channel_sysfs(indio_dev, |
1aa04278 | 423 | &channels[i]); |
1d892719 | 424 | if (ret < 0) |
26d25ae3 JC |
425 | goto error_cleanup_dynamic; |
426 | attrcount += ret; | |
beb80600 | 427 | if (channels[i].type == IIO_TIMESTAMP) |
f1264809 | 428 | indio_dev->scan_index_timestamp = |
beb80600 | 429 | channels[i].scan_index; |
1d892719 | 430 | } |
14555b14 | 431 | if (indio_dev->masklength && buffer->scan_mask == NULL) { |
d83fb184 TM |
432 | buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
433 | sizeof(*buffer->scan_mask), | |
434 | GFP_KERNEL); | |
14555b14 | 435 | if (buffer->scan_mask == NULL) { |
32b5eeca | 436 | ret = -ENOMEM; |
26d25ae3 | 437 | goto error_cleanup_dynamic; |
32b5eeca JC |
438 | } |
439 | } | |
1d892719 JC |
440 | } |
441 | ||
14555b14 | 442 | buffer->scan_el_group.name = iio_scan_elements_group_name; |
26d25ae3 | 443 | |
d83fb184 TM |
444 | buffer->scan_el_group.attrs = kcalloc(attrcount + 1, |
445 | sizeof(buffer->scan_el_group.attrs[0]), | |
446 | GFP_KERNEL); | |
14555b14 | 447 | if (buffer->scan_el_group.attrs == NULL) { |
26d25ae3 JC |
448 | ret = -ENOMEM; |
449 | goto error_free_scan_mask; | |
450 | } | |
14555b14 JC |
451 | if (buffer->scan_el_attrs) |
452 | memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, | |
453 | sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); | |
26d25ae3 JC |
454 | attrn = attrcount_orig; |
455 | ||
14555b14 JC |
456 | list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) |
457 | buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; | |
458 | indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; | |
26d25ae3 | 459 | |
1d892719 | 460 | return 0; |
26d25ae3 JC |
461 | |
462 | error_free_scan_mask: | |
14555b14 | 463 | kfree(buffer->scan_mask); |
1d892719 | 464 | error_cleanup_dynamic: |
84088ebd | 465 | iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); |
26d25ae3 | 466 | |
7026ea4b JC |
467 | return ret; |
468 | } | |
1d892719 | 469 | |
3e1b6c95 | 470 | void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) |
7026ea4b | 471 | { |
3e1b6c95 LPC |
472 | if (!indio_dev->buffer) |
473 | return; | |
474 | ||
14555b14 JC |
475 | kfree(indio_dev->buffer->scan_mask); |
476 | kfree(indio_dev->buffer->scan_el_group.attrs); | |
84088ebd | 477 | iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); |
7026ea4b | 478 | } |
7026ea4b | 479 | |
14555b14 JC |
480 | ssize_t iio_buffer_read_length(struct device *dev, |
481 | struct device_attribute *attr, | |
482 | char *buf) | |
7026ea4b | 483 | { |
e53f5ac5 | 484 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
14555b14 | 485 | struct iio_buffer *buffer = indio_dev->buffer; |
7026ea4b | 486 | |
14555b14 | 487 | if (buffer->access->get_length) |
8d213f24 | 488 | return sprintf(buf, "%d\n", |
14555b14 | 489 | buffer->access->get_length(buffer)); |
7026ea4b | 490 | |
8d213f24 | 491 | return 0; |
7026ea4b | 492 | } |
14555b14 | 493 | EXPORT_SYMBOL(iio_buffer_read_length); |
7026ea4b | 494 | |
14555b14 JC |
495 | ssize_t iio_buffer_write_length(struct device *dev, |
496 | struct device_attribute *attr, | |
497 | const char *buf, | |
498 | size_t len) | |
7026ea4b | 499 | { |
e53f5ac5 | 500 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
14555b14 | 501 | struct iio_buffer *buffer = indio_dev->buffer; |
948ad205 LPC |
502 | unsigned int val; |
503 | int ret; | |
8d213f24 | 504 | |
948ad205 | 505 | ret = kstrtouint(buf, 10, &val); |
7026ea4b JC |
506 | if (ret) |
507 | return ret; | |
508 | ||
14555b14 JC |
509 | if (buffer->access->get_length) |
510 | if (val == buffer->access->get_length(buffer)) | |
7026ea4b JC |
511 | return len; |
512 | ||
e38c79e0 | 513 | mutex_lock(&indio_dev->mlock); |
705ee2c9 | 514 | if (iio_buffer_is_active(indio_dev->buffer)) { |
e38c79e0 LPC |
515 | ret = -EBUSY; |
516 | } else { | |
869871b5 | 517 | if (buffer->access->set_length) |
e38c79e0 | 518 | buffer->access->set_length(buffer, val); |
e38c79e0 | 519 | ret = 0; |
7026ea4b | 520 | } |
e38c79e0 | 521 | mutex_unlock(&indio_dev->mlock); |
7026ea4b | 522 | |
e38c79e0 | 523 | return ret ? ret : len; |
7026ea4b | 524 | } |
14555b14 | 525 | EXPORT_SYMBOL(iio_buffer_write_length); |
7026ea4b | 526 | |
14555b14 JC |
527 | ssize_t iio_buffer_show_enable(struct device *dev, |
528 | struct device_attribute *attr, | |
529 | char *buf) | |
7026ea4b | 530 | { |
e53f5ac5 | 531 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
705ee2c9 | 532 | return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); |
7026ea4b | 533 | } |
14555b14 | 534 | EXPORT_SYMBOL(iio_buffer_show_enable); |
7026ea4b | 535 | |
183f4173 PM |
536 | static int iio_compute_scan_bytes(struct iio_dev *indio_dev, |
537 | const unsigned long *mask, bool timestamp) | |
959d2952 | 538 | { |
959d2952 JC |
539 | const struct iio_chan_spec *ch; |
540 | unsigned bytes = 0; | |
541 | int length, i; | |
959d2952 JC |
542 | |
543 | /* How much space will the demuxed element take? */ | |
6b3b58ed | 544 | for_each_set_bit(i, mask, |
959d2952 JC |
545 | indio_dev->masklength) { |
546 | ch = iio_find_channel_from_si(indio_dev, i); | |
0ee8546a SP |
547 | if (ch->scan_type.repeat > 1) |
548 | length = ch->scan_type.storagebits / 8 * | |
549 | ch->scan_type.repeat; | |
550 | else | |
551 | length = ch->scan_type.storagebits / 8; | |
959d2952 JC |
552 | bytes = ALIGN(bytes, length); |
553 | bytes += length; | |
554 | } | |
6b3b58ed | 555 | if (timestamp) { |
959d2952 | 556 | ch = iio_find_channel_from_si(indio_dev, |
f1264809 | 557 | indio_dev->scan_index_timestamp); |
0ee8546a SP |
558 | if (ch->scan_type.repeat > 1) |
559 | length = ch->scan_type.storagebits / 8 * | |
560 | ch->scan_type.repeat; | |
561 | else | |
562 | length = ch->scan_type.storagebits / 8; | |
959d2952 JC |
563 | bytes = ALIGN(bytes, length); |
564 | bytes += length; | |
565 | } | |
6b3b58ed JC |
566 | return bytes; |
567 | } | |
568 | ||
9e69c935 LPC |
569 | static void iio_buffer_activate(struct iio_dev *indio_dev, |
570 | struct iio_buffer *buffer) | |
571 | { | |
572 | iio_buffer_get(buffer); | |
573 | list_add(&buffer->buffer_list, &indio_dev->buffer_list); | |
574 | } | |
575 | ||
576 | static void iio_buffer_deactivate(struct iio_buffer *buffer) | |
577 | { | |
578 | list_del_init(&buffer->buffer_list); | |
579 | iio_buffer_put(buffer); | |
580 | } | |
581 | ||
a87c82e4 LPC |
582 | void iio_disable_all_buffers(struct iio_dev *indio_dev) |
583 | { | |
584 | struct iio_buffer *buffer, *_buffer; | |
585 | ||
586 | if (list_empty(&indio_dev->buffer_list)) | |
587 | return; | |
588 | ||
589 | if (indio_dev->setup_ops->predisable) | |
590 | indio_dev->setup_ops->predisable(indio_dev); | |
591 | ||
592 | list_for_each_entry_safe(buffer, _buffer, | |
593 | &indio_dev->buffer_list, buffer_list) | |
9e69c935 | 594 | iio_buffer_deactivate(buffer); |
a87c82e4 LPC |
595 | |
596 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
597 | if (indio_dev->setup_ops->postdisable) | |
598 | indio_dev->setup_ops->postdisable(indio_dev); | |
e086ed76 LPC |
599 | |
600 | if (indio_dev->available_scan_masks == NULL) | |
601 | kfree(indio_dev->active_scan_mask); | |
a87c82e4 LPC |
602 | } |
603 | ||
8e050996 LPC |
604 | static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, |
605 | struct iio_buffer *buffer) | |
606 | { | |
607 | unsigned int bytes; | |
608 | ||
609 | if (!buffer->access->set_bytes_per_datum) | |
610 | return; | |
611 | ||
612 | bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, | |
613 | buffer->scan_timestamp); | |
614 | ||
615 | buffer->access->set_bytes_per_datum(buffer, bytes); | |
616 | } | |
617 | ||
a9519456 | 618 | static int __iio_update_buffers(struct iio_dev *indio_dev, |
84b36ce5 JC |
619 | struct iio_buffer *insert_buffer, |
620 | struct iio_buffer *remove_buffer) | |
6b3b58ed | 621 | { |
84b36ce5 JC |
622 | int ret; |
623 | int success = 0; | |
624 | struct iio_buffer *buffer; | |
625 | unsigned long *compound_mask; | |
626 | const unsigned long *old_mask; | |
6b3b58ed | 627 | |
84b36ce5 JC |
628 | /* Wind down existing buffers - iff there are any */ |
629 | if (!list_empty(&indio_dev->buffer_list)) { | |
630 | if (indio_dev->setup_ops->predisable) { | |
631 | ret = indio_dev->setup_ops->predisable(indio_dev); | |
632 | if (ret) | |
92825ff9 | 633 | return ret; |
84b36ce5 JC |
634 | } |
635 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
636 | if (indio_dev->setup_ops->postdisable) { | |
637 | ret = indio_dev->setup_ops->postdisable(indio_dev); | |
638 | if (ret) | |
92825ff9 | 639 | return ret; |
84b36ce5 JC |
640 | } |
641 | } | |
642 | /* Keep a copy of current setup to allow roll back */ | |
643 | old_mask = indio_dev->active_scan_mask; | |
644 | if (!indio_dev->available_scan_masks) | |
645 | indio_dev->active_scan_mask = NULL; | |
646 | ||
647 | if (remove_buffer) | |
9e69c935 | 648 | iio_buffer_deactivate(remove_buffer); |
84b36ce5 | 649 | if (insert_buffer) |
9e69c935 | 650 | iio_buffer_activate(indio_dev, insert_buffer); |
84b36ce5 JC |
651 | |
652 | /* If no buffers in list, we are done */ | |
653 | if (list_empty(&indio_dev->buffer_list)) { | |
654 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
655 | if (indio_dev->available_scan_masks == NULL) | |
656 | kfree(old_mask); | |
657 | return 0; | |
658 | } | |
959d2952 | 659 | |
9572588c | 660 | /* What scan mask do we actually have? */ |
84b36ce5 JC |
661 | compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
662 | sizeof(long), GFP_KERNEL); | |
663 | if (compound_mask == NULL) { | |
664 | if (indio_dev->available_scan_masks == NULL) | |
665 | kfree(old_mask); | |
666 | return -ENOMEM; | |
667 | } | |
668 | indio_dev->scan_timestamp = 0; | |
669 | ||
670 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { | |
671 | bitmap_or(compound_mask, compound_mask, buffer->scan_mask, | |
672 | indio_dev->masklength); | |
673 | indio_dev->scan_timestamp |= buffer->scan_timestamp; | |
674 | } | |
675 | if (indio_dev->available_scan_masks) { | |
959d2952 JC |
676 | indio_dev->active_scan_mask = |
677 | iio_scan_mask_match(indio_dev->available_scan_masks, | |
678 | indio_dev->masklength, | |
84b36ce5 JC |
679 | compound_mask); |
680 | if (indio_dev->active_scan_mask == NULL) { | |
681 | /* | |
682 | * Roll back. | |
683 | * Note can only occur when adding a buffer. | |
684 | */ | |
9e69c935 | 685 | iio_buffer_deactivate(insert_buffer); |
d66e0452 PM |
686 | if (old_mask) { |
687 | indio_dev->active_scan_mask = old_mask; | |
688 | success = -EINVAL; | |
689 | } | |
690 | else { | |
691 | kfree(compound_mask); | |
692 | ret = -EINVAL; | |
92825ff9 | 693 | return ret; |
d66e0452 | 694 | } |
84b36ce5 JC |
695 | } |
696 | } else { | |
697 | indio_dev->active_scan_mask = compound_mask; | |
698 | } | |
aff1eb4e | 699 | |
5ada4ea9 JC |
700 | iio_update_demux(indio_dev); |
701 | ||
84b36ce5 JC |
702 | /* Wind up again */ |
703 | if (indio_dev->setup_ops->preenable) { | |
704 | ret = indio_dev->setup_ops->preenable(indio_dev); | |
705 | if (ret) { | |
706 | printk(KERN_ERR | |
bec1889d | 707 | "Buffer not started: buffer preenable failed (%d)\n", ret); |
84b36ce5 JC |
708 | goto error_remove_inserted; |
709 | } | |
710 | } | |
711 | indio_dev->scan_bytes = | |
712 | iio_compute_scan_bytes(indio_dev, | |
713 | indio_dev->active_scan_mask, | |
714 | indio_dev->scan_timestamp); | |
8e050996 LPC |
715 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { |
716 | iio_buffer_update_bytes_per_datum(indio_dev, buffer); | |
84b36ce5 JC |
717 | if (buffer->access->request_update) { |
718 | ret = buffer->access->request_update(buffer); | |
719 | if (ret) { | |
720 | printk(KERN_INFO | |
bec1889d | 721 | "Buffer not started: buffer parameter update failed (%d)\n", ret); |
84b36ce5 JC |
722 | goto error_run_postdisable; |
723 | } | |
724 | } | |
8e050996 | 725 | } |
84b36ce5 JC |
726 | if (indio_dev->info->update_scan_mode) { |
727 | ret = indio_dev->info | |
5ada4ea9 JC |
728 | ->update_scan_mode(indio_dev, |
729 | indio_dev->active_scan_mask); | |
84b36ce5 | 730 | if (ret < 0) { |
bec1889d | 731 | printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret); |
84b36ce5 JC |
732 | goto error_run_postdisable; |
733 | } | |
734 | } | |
9572588c | 735 | /* Definitely possible for devices to support both of these. */ |
84b36ce5 JC |
736 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { |
737 | if (!indio_dev->trig) { | |
738 | printk(KERN_INFO "Buffer not started: no trigger\n"); | |
739 | ret = -EINVAL; | |
740 | /* Can only occur on first buffer */ | |
741 | goto error_run_postdisable; | |
742 | } | |
743 | indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; | |
744 | } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { | |
745 | indio_dev->currentmode = INDIO_BUFFER_HARDWARE; | |
9572588c | 746 | } else { /* Should never be reached */ |
84b36ce5 JC |
747 | ret = -EINVAL; |
748 | goto error_run_postdisable; | |
749 | } | |
750 | ||
751 | if (indio_dev->setup_ops->postenable) { | |
752 | ret = indio_dev->setup_ops->postenable(indio_dev); | |
753 | if (ret) { | |
754 | printk(KERN_INFO | |
bec1889d | 755 | "Buffer not started: postenable failed (%d)\n", ret); |
84b36ce5 JC |
756 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
757 | if (indio_dev->setup_ops->postdisable) | |
758 | indio_dev->setup_ops->postdisable(indio_dev); | |
759 | goto error_disable_all_buffers; | |
760 | } | |
761 | } | |
762 | ||
763 | if (indio_dev->available_scan_masks) | |
764 | kfree(compound_mask); | |
765 | else | |
766 | kfree(old_mask); | |
767 | ||
768 | return success; | |
769 | ||
770 | error_disable_all_buffers: | |
771 | indio_dev->currentmode = INDIO_DIRECT_MODE; | |
772 | error_run_postdisable: | |
773 | if (indio_dev->setup_ops->postdisable) | |
774 | indio_dev->setup_ops->postdisable(indio_dev); | |
775 | error_remove_inserted: | |
84b36ce5 | 776 | if (insert_buffer) |
9e69c935 | 777 | iio_buffer_deactivate(insert_buffer); |
84b36ce5 JC |
778 | indio_dev->active_scan_mask = old_mask; |
779 | kfree(compound_mask); | |
84b36ce5 JC |
780 | return ret; |
781 | } | |
a9519456 LPC |
782 | |
783 | int iio_update_buffers(struct iio_dev *indio_dev, | |
784 | struct iio_buffer *insert_buffer, | |
785 | struct iio_buffer *remove_buffer) | |
786 | { | |
787 | int ret; | |
788 | ||
3909fab5 LPC |
789 | if (insert_buffer == remove_buffer) |
790 | return 0; | |
791 | ||
a9519456 LPC |
792 | mutex_lock(&indio_dev->info_exist_lock); |
793 | mutex_lock(&indio_dev->mlock); | |
794 | ||
3909fab5 LPC |
795 | if (insert_buffer && iio_buffer_is_active(insert_buffer)) |
796 | insert_buffer = NULL; | |
797 | ||
798 | if (remove_buffer && !iio_buffer_is_active(remove_buffer)) | |
799 | remove_buffer = NULL; | |
800 | ||
801 | if (!insert_buffer && !remove_buffer) { | |
802 | ret = 0; | |
803 | goto out_unlock; | |
804 | } | |
805 | ||
a9519456 LPC |
806 | if (indio_dev->info == NULL) { |
807 | ret = -ENODEV; | |
808 | goto out_unlock; | |
809 | } | |
810 | ||
811 | ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); | |
812 | ||
813 | out_unlock: | |
814 | mutex_unlock(&indio_dev->mlock); | |
815 | mutex_unlock(&indio_dev->info_exist_lock); | |
816 | ||
817 | return ret; | |
818 | } | |
84b36ce5 JC |
819 | EXPORT_SYMBOL_GPL(iio_update_buffers); |
820 | ||
821 | ssize_t iio_buffer_store_enable(struct device *dev, | |
822 | struct device_attribute *attr, | |
823 | const char *buf, | |
824 | size_t len) | |
825 | { | |
826 | int ret; | |
827 | bool requested_state; | |
828 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); | |
84b36ce5 JC |
829 | bool inlist; |
830 | ||
831 | ret = strtobool(buf, &requested_state); | |
832 | if (ret < 0) | |
833 | return ret; | |
834 | ||
835 | mutex_lock(&indio_dev->mlock); | |
836 | ||
837 | /* Find out if it is in the list */ | |
705ee2c9 | 838 | inlist = iio_buffer_is_active(indio_dev->buffer); |
84b36ce5 JC |
839 | /* Already in desired state */ |
840 | if (inlist == requested_state) | |
841 | goto done; | |
842 | ||
843 | if (requested_state) | |
a9519456 | 844 | ret = __iio_update_buffers(indio_dev, |
84b36ce5 JC |
845 | indio_dev->buffer, NULL); |
846 | else | |
a9519456 | 847 | ret = __iio_update_buffers(indio_dev, |
84b36ce5 JC |
848 | NULL, indio_dev->buffer); |
849 | ||
850 | if (ret < 0) | |
851 | goto done; | |
852 | done: | |
853 | mutex_unlock(&indio_dev->mlock); | |
854 | return (ret < 0) ? ret : len; | |
855 | } | |
856 | EXPORT_SYMBOL(iio_buffer_store_enable); | |
857 | ||
81636632 LPC |
858 | /** |
859 | * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected | |
860 | * @indio_dev: the iio device | |
861 | * @mask: scan mask to be checked | |
862 | * | |
863 | * Return true if exactly one bit is set in the scan mask, false otherwise. It | |
864 | * can be used for devices where only one channel can be active for sampling at | |
865 | * a time. | |
866 | */ | |
867 | bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, | |
868 | const unsigned long *mask) | |
869 | { | |
870 | return bitmap_weight(mask, indio_dev->masklength) == 1; | |
871 | } | |
872 | EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); | |
873 | ||
f79a9098 JC |
874 | int iio_scan_mask_query(struct iio_dev *indio_dev, |
875 | struct iio_buffer *buffer, int bit) | |
32b5eeca | 876 | { |
f8c6f4e9 | 877 | if (bit > indio_dev->masklength) |
32b5eeca JC |
878 | return -EINVAL; |
879 | ||
14555b14 | 880 | if (!buffer->scan_mask) |
32b5eeca | 881 | return 0; |
32b5eeca | 882 | |
2076a20f AB |
883 | /* Ensure return value is 0 or 1. */ |
884 | return !!test_bit(bit, buffer->scan_mask); | |
32b5eeca JC |
885 | }; |
886 | EXPORT_SYMBOL_GPL(iio_scan_mask_query); | |
5ada4ea9 JC |
887 | |
888 | /** | |
889 | * struct iio_demux_table() - table describing demux memcpy ops | |
890 | * @from: index to copy from | |
99698b45 | 891 | * @to: index to copy to |
5ada4ea9 JC |
892 | * @length: how many bytes to copy |
893 | * @l: list head used for management | |
894 | */ | |
895 | struct iio_demux_table { | |
896 | unsigned from; | |
897 | unsigned to; | |
898 | unsigned length; | |
899 | struct list_head l; | |
900 | }; | |
901 | ||
5d65d920 LPC |
902 | static const void *iio_demux(struct iio_buffer *buffer, |
903 | const void *datain) | |
5ada4ea9 JC |
904 | { |
905 | struct iio_demux_table *t; | |
906 | ||
907 | if (list_empty(&buffer->demux_list)) | |
908 | return datain; | |
909 | list_for_each_entry(t, &buffer->demux_list, l) | |
910 | memcpy(buffer->demux_bounce + t->to, | |
911 | datain + t->from, t->length); | |
912 | ||
913 | return buffer->demux_bounce; | |
914 | } | |
915 | ||
5d65d920 | 916 | static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) |
5ada4ea9 | 917 | { |
5d65d920 | 918 | const void *dataout = iio_demux(buffer, data); |
5ada4ea9 | 919 | |
ce56ade6 | 920 | return buffer->access->store_to(buffer, dataout); |
5ada4ea9 | 921 | } |
5ada4ea9 | 922 | |
842cd100 JC |
923 | static void iio_buffer_demux_free(struct iio_buffer *buffer) |
924 | { | |
925 | struct iio_demux_table *p, *q; | |
926 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { | |
927 | list_del(&p->l); | |
928 | kfree(p); | |
929 | } | |
930 | } | |
931 | ||
84b36ce5 | 932 | |
5d65d920 | 933 | int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) |
84b36ce5 JC |
934 | { |
935 | int ret; | |
936 | struct iio_buffer *buf; | |
937 | ||
938 | list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { | |
939 | ret = iio_push_to_buffer(buf, data); | |
940 | if (ret < 0) | |
941 | return ret; | |
942 | } | |
943 | ||
944 | return 0; | |
945 | } | |
946 | EXPORT_SYMBOL_GPL(iio_push_to_buffers); | |
947 | ||
cbe88bcc LPC |
948 | static int iio_buffer_add_demux(struct iio_buffer *buffer, |
949 | struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, | |
950 | unsigned int length) | |
951 | { | |
952 | ||
953 | if (*p && (*p)->from + (*p)->length == in_loc && | |
954 | (*p)->to + (*p)->length == out_loc) { | |
955 | (*p)->length += length; | |
956 | } else { | |
7cdca178 | 957 | *p = kmalloc(sizeof(**p), GFP_KERNEL); |
cbe88bcc LPC |
958 | if (*p == NULL) |
959 | return -ENOMEM; | |
960 | (*p)->from = in_loc; | |
961 | (*p)->to = out_loc; | |
962 | (*p)->length = length; | |
963 | list_add_tail(&(*p)->l, &buffer->demux_list); | |
964 | } | |
965 | ||
966 | return 0; | |
967 | } | |
968 | ||
84b36ce5 JC |
969 | static int iio_buffer_update_demux(struct iio_dev *indio_dev, |
970 | struct iio_buffer *buffer) | |
5ada4ea9 JC |
971 | { |
972 | const struct iio_chan_spec *ch; | |
5ada4ea9 JC |
973 | int ret, in_ind = -1, out_ind, length; |
974 | unsigned in_loc = 0, out_loc = 0; | |
cbe88bcc | 975 | struct iio_demux_table *p = NULL; |
5ada4ea9 JC |
976 | |
977 | /* Clear out any old demux */ | |
842cd100 | 978 | iio_buffer_demux_free(buffer); |
5ada4ea9 JC |
979 | kfree(buffer->demux_bounce); |
980 | buffer->demux_bounce = NULL; | |
981 | ||
982 | /* First work out which scan mode we will actually have */ | |
983 | if (bitmap_equal(indio_dev->active_scan_mask, | |
984 | buffer->scan_mask, | |
985 | indio_dev->masklength)) | |
986 | return 0; | |
987 | ||
988 | /* Now we have the two masks, work from least sig and build up sizes */ | |
989 | for_each_set_bit(out_ind, | |
61bd55ce | 990 | buffer->scan_mask, |
5ada4ea9 JC |
991 | indio_dev->masklength) { |
992 | in_ind = find_next_bit(indio_dev->active_scan_mask, | |
993 | indio_dev->masklength, | |
994 | in_ind + 1); | |
995 | while (in_ind != out_ind) { | |
996 | in_ind = find_next_bit(indio_dev->active_scan_mask, | |
997 | indio_dev->masklength, | |
998 | in_ind + 1); | |
999 | ch = iio_find_channel_from_si(indio_dev, in_ind); | |
0ee8546a SP |
1000 | if (ch->scan_type.repeat > 1) |
1001 | length = ch->scan_type.storagebits / 8 * | |
1002 | ch->scan_type.repeat; | |
1003 | else | |
1004 | length = ch->scan_type.storagebits / 8; | |
5ada4ea9 | 1005 | /* Make sure we are aligned */ |
61072dbc | 1006 | in_loc = roundup(in_loc, length) + length; |
5ada4ea9 JC |
1007 | } |
1008 | ch = iio_find_channel_from_si(indio_dev, in_ind); | |
0ee8546a SP |
1009 | if (ch->scan_type.repeat > 1) |
1010 | length = ch->scan_type.storagebits / 8 * | |
1011 | ch->scan_type.repeat; | |
1012 | else | |
1013 | length = ch->scan_type.storagebits / 8; | |
61072dbc LPC |
1014 | out_loc = roundup(out_loc, length); |
1015 | in_loc = roundup(in_loc, length); | |
cbe88bcc LPC |
1016 | ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); |
1017 | if (ret) | |
1018 | goto error_clear_mux_table; | |
5ada4ea9 JC |
1019 | out_loc += length; |
1020 | in_loc += length; | |
1021 | } | |
1022 | /* Relies on scan_timestamp being last */ | |
1023 | if (buffer->scan_timestamp) { | |
5ada4ea9 | 1024 | ch = iio_find_channel_from_si(indio_dev, |
f1264809 | 1025 | indio_dev->scan_index_timestamp); |
0ee8546a SP |
1026 | if (ch->scan_type.repeat > 1) |
1027 | length = ch->scan_type.storagebits / 8 * | |
1028 | ch->scan_type.repeat; | |
1029 | else | |
1030 | length = ch->scan_type.storagebits / 8; | |
61072dbc LPC |
1031 | out_loc = roundup(out_loc, length); |
1032 | in_loc = roundup(in_loc, length); | |
cbe88bcc LPC |
1033 | ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); |
1034 | if (ret) | |
1035 | goto error_clear_mux_table; | |
5ada4ea9 JC |
1036 | out_loc += length; |
1037 | in_loc += length; | |
1038 | } | |
1039 | buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); | |
1040 | if (buffer->demux_bounce == NULL) { | |
1041 | ret = -ENOMEM; | |
1042 | goto error_clear_mux_table; | |
1043 | } | |
1044 | return 0; | |
1045 | ||
1046 | error_clear_mux_table: | |
842cd100 JC |
1047 | iio_buffer_demux_free(buffer); |
1048 | ||
5ada4ea9 JC |
1049 | return ret; |
1050 | } | |
84b36ce5 JC |
1051 | |
1052 | int iio_update_demux(struct iio_dev *indio_dev) | |
1053 | { | |
1054 | struct iio_buffer *buffer; | |
1055 | int ret; | |
1056 | ||
1057 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { | |
1058 | ret = iio_buffer_update_demux(indio_dev, buffer); | |
1059 | if (ret < 0) | |
1060 | goto error_clear_mux_table; | |
1061 | } | |
1062 | return 0; | |
1063 | ||
1064 | error_clear_mux_table: | |
1065 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) | |
1066 | iio_buffer_demux_free(buffer); | |
1067 | ||
1068 | return ret; | |
1069 | } | |
5ada4ea9 | 1070 | EXPORT_SYMBOL_GPL(iio_update_demux); |
9e69c935 LPC |
1071 | |
1072 | /** | |
1073 | * iio_buffer_release() - Free a buffer's resources | |
1074 | * @ref: Pointer to the kref embedded in the iio_buffer struct | |
1075 | * | |
1076 | * This function is called when the last reference to the buffer has been | |
1077 | * dropped. It will typically free all resources allocated by the buffer. Do not | |
1078 | * call this function manually, always use iio_buffer_put() when done using a | |
1079 | * buffer. | |
1080 | */ | |
1081 | static void iio_buffer_release(struct kref *ref) | |
1082 | { | |
1083 | struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); | |
1084 | ||
1085 | buffer->access->release(buffer); | |
1086 | } | |
1087 | ||
1088 | /** | |
1089 | * iio_buffer_get() - Grab a reference to the buffer | |
1090 | * @buffer: The buffer to grab a reference for, may be NULL | |
1091 | * | |
1092 | * Returns the pointer to the buffer that was passed into the function. | |
1093 | */ | |
1094 | struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) | |
1095 | { | |
1096 | if (buffer) | |
1097 | kref_get(&buffer->ref); | |
1098 | ||
1099 | return buffer; | |
1100 | } | |
1101 | EXPORT_SYMBOL_GPL(iio_buffer_get); | |
1102 | ||
1103 | /** | |
1104 | * iio_buffer_put() - Release the reference to the buffer | |
1105 | * @buffer: The buffer to release the reference for, may be NULL | |
1106 | */ | |
1107 | void iio_buffer_put(struct iio_buffer *buffer) | |
1108 | { | |
1109 | if (buffer) | |
1110 | kref_put(&buffer->ref, iio_buffer_release); | |
1111 | } | |
1112 | EXPORT_SYMBOL_GPL(iio_buffer_put); |