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