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 JC |
23 | |
24 | #include "iio.h" | |
df9c1c42 | 25 | #include "iio_core.h" |
9dd1cb30 | 26 | #include "sysfs.h" |
af5046af | 27 | #include "buffer.h" |
7026ea4b | 28 | |
8310b86c JC |
29 | static const char * const iio_endian_prefix[] = { |
30 | [IIO_BE] = "be", | |
31 | [IIO_LE] = "le", | |
32 | }; | |
7026ea4b JC |
33 | |
34 | /** | |
14555b14 | 35 | * iio_buffer_read_first_n_outer() - chrdev read for buffer access |
7026ea4b | 36 | * |
14555b14 JC |
37 | * This function relies on all buffer implementations having an |
38 | * iio_buffer as their first element. | |
7026ea4b | 39 | **/ |
14555b14 JC |
40 | ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf, |
41 | size_t n, loff_t *f_ps) | |
7026ea4b | 42 | { |
1aa04278 | 43 | struct iio_dev *indio_dev = filp->private_data; |
14555b14 | 44 | struct iio_buffer *rb = indio_dev->buffer; |
d5857d65 | 45 | |
96e00f11 | 46 | if (!rb || !rb->access->read_first_n) |
7026ea4b | 47 | return -EINVAL; |
8d213f24 | 48 | return rb->access->read_first_n(rb, n, buf); |
7026ea4b JC |
49 | } |
50 | ||
a7348347 | 51 | /** |
14555b14 | 52 | * iio_buffer_poll() - poll the buffer to find out if it has data |
a7348347 | 53 | */ |
14555b14 JC |
54 | unsigned int iio_buffer_poll(struct file *filp, |
55 | struct poll_table_struct *wait) | |
a7348347 | 56 | { |
1aa04278 | 57 | struct iio_dev *indio_dev = filp->private_data; |
14555b14 | 58 | struct iio_buffer *rb = indio_dev->buffer; |
a7348347 JC |
59 | |
60 | poll_wait(filp, &rb->pollq, wait); | |
61 | if (rb->stufftoread) | |
62 | return POLLIN | POLLRDNORM; | |
63 | /* need a way of knowing if there may be enough data... */ | |
8d213f24 | 64 | return 0; |
a7348347 JC |
65 | } |
66 | ||
30eb82f0 | 67 | int iio_chrdev_buffer_open(struct iio_dev *indio_dev) |
7026ea4b | 68 | { |
14555b14 | 69 | struct iio_buffer *rb = indio_dev->buffer; |
30eb82f0 | 70 | if (!rb) |
96e00f11 | 71 | return 0; |
30eb82f0 | 72 | if (rb->access->mark_in_use) |
1aa04278 | 73 | rb->access->mark_in_use(rb); |
30eb82f0 | 74 | return 0; |
7026ea4b | 75 | } |
7026ea4b | 76 | |
14555b14 | 77 | void iio_chrdev_buffer_release(struct iio_dev *indio_dev) |
7026ea4b | 78 | { |
14555b14 | 79 | struct iio_buffer *rb = indio_dev->buffer; |
758d988c | 80 | |
96e00f11 JC |
81 | if (!rb) |
82 | return; | |
1aa04278 JC |
83 | clear_bit(IIO_BUSY_BIT_POS, &rb->flags); |
84 | if (rb->access->unmark_in_use) | |
85 | rb->access->unmark_in_use(rb); | |
7026ea4b JC |
86 | } |
87 | ||
f79a9098 | 88 | void iio_buffer_init(struct iio_buffer *buffer) |
7026ea4b | 89 | { |
5ada4ea9 | 90 | INIT_LIST_HEAD(&buffer->demux_list); |
14555b14 | 91 | init_waitqueue_head(&buffer->pollq); |
7026ea4b | 92 | } |
14555b14 | 93 | EXPORT_SYMBOL(iio_buffer_init); |
7026ea4b | 94 | |
1d892719 | 95 | static ssize_t iio_show_scan_index(struct device *dev, |
8d213f24 JC |
96 | struct device_attribute *attr, |
97 | char *buf) | |
1d892719 | 98 | { |
8d213f24 | 99 | return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); |
1d892719 JC |
100 | } |
101 | ||
102 | static ssize_t iio_show_fixed_type(struct device *dev, | |
103 | struct device_attribute *attr, | |
104 | char *buf) | |
105 | { | |
106 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
8310b86c JC |
107 | u8 type = this_attr->c->scan_type.endianness; |
108 | ||
109 | if (type == IIO_CPU) { | |
9d5d1153 JC |
110 | #ifdef __LITTLE_ENDIAN |
111 | type = IIO_LE; | |
112 | #else | |
113 | type = IIO_BE; | |
114 | #endif | |
8310b86c JC |
115 | } |
116 | return sprintf(buf, "%s:%c%d/%d>>%u\n", | |
117 | iio_endian_prefix[type], | |
1d892719 JC |
118 | this_attr->c->scan_type.sign, |
119 | this_attr->c->scan_type.realbits, | |
120 | this_attr->c->scan_type.storagebits, | |
121 | this_attr->c->scan_type.shift); | |
122 | } | |
123 | ||
8d213f24 JC |
124 | static ssize_t iio_scan_el_show(struct device *dev, |
125 | struct device_attribute *attr, | |
126 | char *buf) | |
127 | { | |
128 | int ret; | |
f8c6f4e9 | 129 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
8d213f24 | 130 | |
5ada4ea9 JC |
131 | ret = test_bit(to_iio_dev_attr(attr)->address, |
132 | indio_dev->buffer->scan_mask); | |
133 | ||
8d213f24 JC |
134 | return sprintf(buf, "%d\n", ret); |
135 | } | |
136 | ||
14555b14 | 137 | static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) |
8d213f24 | 138 | { |
14555b14 | 139 | clear_bit(bit, buffer->scan_mask); |
8d213f24 JC |
140 | return 0; |
141 | } | |
142 | ||
143 | static ssize_t iio_scan_el_store(struct device *dev, | |
144 | struct device_attribute *attr, | |
145 | const char *buf, | |
146 | size_t len) | |
147 | { | |
148 | int ret = 0; | |
149 | bool state; | |
1aa04278 | 150 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
14555b14 | 151 | struct iio_buffer *buffer = indio_dev->buffer; |
8d213f24 JC |
152 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
153 | ||
154 | state = !(buf[0] == '0'); | |
155 | mutex_lock(&indio_dev->mlock); | |
5fd6218c | 156 | if (iio_buffer_enabled(indio_dev)) { |
8d213f24 JC |
157 | ret = -EBUSY; |
158 | goto error_ret; | |
159 | } | |
f79a9098 | 160 | ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); |
8d213f24 JC |
161 | if (ret < 0) |
162 | goto error_ret; | |
163 | if (!state && ret) { | |
14555b14 | 164 | ret = iio_scan_mask_clear(buffer, this_attr->address); |
8d213f24 JC |
165 | if (ret) |
166 | goto error_ret; | |
167 | } else if (state && !ret) { | |
f79a9098 | 168 | ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); |
8d213f24 JC |
169 | if (ret) |
170 | goto error_ret; | |
171 | } | |
172 | ||
173 | error_ret: | |
174 | mutex_unlock(&indio_dev->mlock); | |
175 | ||
5a2a6e11 | 176 | return ret < 0 ? ret : len; |
8d213f24 JC |
177 | |
178 | } | |
179 | ||
180 | static ssize_t iio_scan_el_ts_show(struct device *dev, | |
181 | struct device_attribute *attr, | |
182 | char *buf) | |
183 | { | |
f8c6f4e9 JC |
184 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
185 | return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); | |
8d213f24 JC |
186 | } |
187 | ||
188 | static ssize_t iio_scan_el_ts_store(struct device *dev, | |
189 | struct device_attribute *attr, | |
190 | const char *buf, | |
191 | size_t len) | |
192 | { | |
193 | int ret = 0; | |
1aa04278 | 194 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
8d213f24 | 195 | bool state; |
1aa04278 | 196 | |
8d213f24 JC |
197 | state = !(buf[0] == '0'); |
198 | mutex_lock(&indio_dev->mlock); | |
5fd6218c | 199 | if (iio_buffer_enabled(indio_dev)) { |
8d213f24 JC |
200 | ret = -EBUSY; |
201 | goto error_ret; | |
202 | } | |
14555b14 | 203 | indio_dev->buffer->scan_timestamp = state; |
8d213f24 JC |
204 | error_ret: |
205 | mutex_unlock(&indio_dev->mlock); | |
206 | ||
207 | return ret ? ret : len; | |
208 | } | |
209 | ||
14555b14 JC |
210 | static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, |
211 | const struct iio_chan_spec *chan) | |
1d892719 | 212 | { |
26d25ae3 | 213 | int ret, attrcount = 0; |
14555b14 | 214 | struct iio_buffer *buffer = indio_dev->buffer; |
1d892719 | 215 | |
26d25ae3 | 216 | ret = __iio_add_chan_devattr("index", |
1d892719 JC |
217 | chan, |
218 | &iio_show_scan_index, | |
219 | NULL, | |
220 | 0, | |
221 | 0, | |
1aa04278 | 222 | &indio_dev->dev, |
14555b14 | 223 | &buffer->scan_el_dev_attr_list); |
1d892719 JC |
224 | if (ret) |
225 | goto error_ret; | |
26d25ae3 JC |
226 | attrcount++; |
227 | ret = __iio_add_chan_devattr("type", | |
1d892719 JC |
228 | chan, |
229 | &iio_show_fixed_type, | |
230 | NULL, | |
231 | 0, | |
232 | 0, | |
1aa04278 | 233 | &indio_dev->dev, |
14555b14 | 234 | &buffer->scan_el_dev_attr_list); |
1d892719 JC |
235 | if (ret) |
236 | goto error_ret; | |
26d25ae3 | 237 | attrcount++; |
a88b3ebc | 238 | if (chan->type != IIO_TIMESTAMP) |
26d25ae3 | 239 | ret = __iio_add_chan_devattr("en", |
a88b3ebc JC |
240 | chan, |
241 | &iio_scan_el_show, | |
242 | &iio_scan_el_store, | |
243 | chan->scan_index, | |
244 | 0, | |
1aa04278 | 245 | &indio_dev->dev, |
14555b14 | 246 | &buffer->scan_el_dev_attr_list); |
a88b3ebc | 247 | else |
26d25ae3 | 248 | ret = __iio_add_chan_devattr("en", |
a88b3ebc JC |
249 | chan, |
250 | &iio_scan_el_ts_show, | |
251 | &iio_scan_el_ts_store, | |
252 | chan->scan_index, | |
253 | 0, | |
1aa04278 | 254 | &indio_dev->dev, |
14555b14 | 255 | &buffer->scan_el_dev_attr_list); |
26d25ae3 JC |
256 | attrcount++; |
257 | ret = attrcount; | |
1d892719 JC |
258 | error_ret: |
259 | return ret; | |
260 | } | |
261 | ||
14555b14 JC |
262 | static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev, |
263 | struct iio_dev_attr *p) | |
1d892719 | 264 | { |
1d892719 JC |
265 | kfree(p->dev_attr.attr.name); |
266 | kfree(p); | |
267 | } | |
268 | ||
14555b14 | 269 | static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev) |
1d892719 JC |
270 | { |
271 | struct iio_dev_attr *p, *n; | |
14555b14 | 272 | struct iio_buffer *buffer = indio_dev->buffer; |
26d25ae3 | 273 | |
1d892719 | 274 | list_for_each_entry_safe(p, n, |
14555b14 JC |
275 | &buffer->scan_el_dev_attr_list, l) |
276 | iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p); | |
1d892719 JC |
277 | } |
278 | ||
26d25ae3 JC |
279 | static const char * const iio_scan_elements_group_name = "scan_elements"; |
280 | ||
14555b14 JC |
281 | int iio_buffer_register(struct iio_dev *indio_dev, |
282 | const struct iio_chan_spec *channels, | |
283 | int num_channels) | |
1d892719 | 284 | { |
26d25ae3 JC |
285 | struct iio_dev_attr *p; |
286 | struct attribute **attr; | |
14555b14 | 287 | struct iio_buffer *buffer = indio_dev->buffer; |
26d25ae3 JC |
288 | int ret, i, attrn, attrcount, attrcount_orig = 0; |
289 | ||
14555b14 JC |
290 | if (buffer->attrs) |
291 | indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs; | |
bf32963c | 292 | |
14555b14 JC |
293 | if (buffer->scan_el_attrs != NULL) { |
294 | attr = buffer->scan_el_attrs->attrs; | |
26d25ae3 JC |
295 | while (*attr++ != NULL) |
296 | attrcount_orig++; | |
297 | } | |
298 | attrcount = attrcount_orig; | |
14555b14 | 299 | INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); |
1d892719 JC |
300 | if (channels) { |
301 | /* new magic */ | |
302 | for (i = 0; i < num_channels; i++) { | |
32b5eeca JC |
303 | /* Establish necessary mask length */ |
304 | if (channels[i].scan_index > | |
305 | (int)indio_dev->masklength - 1) | |
306 | indio_dev->masklength | |
307 | = indio_dev->channels[i].scan_index + 1; | |
308 | ||
14555b14 | 309 | ret = iio_buffer_add_channel_sysfs(indio_dev, |
1aa04278 | 310 | &channels[i]); |
1d892719 | 311 | if (ret < 0) |
26d25ae3 JC |
312 | goto error_cleanup_dynamic; |
313 | attrcount += ret; | |
beb80600 JC |
314 | if (channels[i].type == IIO_TIMESTAMP) |
315 | buffer->scan_index_timestamp = | |
316 | channels[i].scan_index; | |
1d892719 | 317 | } |
14555b14 | 318 | if (indio_dev->masklength && buffer->scan_mask == NULL) { |
d83fb184 TM |
319 | buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
320 | sizeof(*buffer->scan_mask), | |
321 | GFP_KERNEL); | |
14555b14 | 322 | if (buffer->scan_mask == NULL) { |
32b5eeca | 323 | ret = -ENOMEM; |
26d25ae3 | 324 | goto error_cleanup_dynamic; |
32b5eeca JC |
325 | } |
326 | } | |
1d892719 JC |
327 | } |
328 | ||
14555b14 | 329 | buffer->scan_el_group.name = iio_scan_elements_group_name; |
26d25ae3 | 330 | |
d83fb184 TM |
331 | buffer->scan_el_group.attrs = kcalloc(attrcount + 1, |
332 | sizeof(buffer->scan_el_group.attrs[0]), | |
333 | GFP_KERNEL); | |
14555b14 | 334 | if (buffer->scan_el_group.attrs == NULL) { |
26d25ae3 JC |
335 | ret = -ENOMEM; |
336 | goto error_free_scan_mask; | |
337 | } | |
14555b14 JC |
338 | if (buffer->scan_el_attrs) |
339 | memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, | |
340 | sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); | |
26d25ae3 JC |
341 | attrn = attrcount_orig; |
342 | ||
14555b14 JC |
343 | list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) |
344 | buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; | |
345 | indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; | |
26d25ae3 | 346 | |
1d892719 | 347 | return 0; |
26d25ae3 JC |
348 | |
349 | error_free_scan_mask: | |
14555b14 | 350 | kfree(buffer->scan_mask); |
1d892719 | 351 | error_cleanup_dynamic: |
14555b14 | 352 | __iio_buffer_attr_cleanup(indio_dev); |
26d25ae3 | 353 | |
7026ea4b JC |
354 | return ret; |
355 | } | |
14555b14 | 356 | EXPORT_SYMBOL(iio_buffer_register); |
1d892719 | 357 | |
14555b14 | 358 | void iio_buffer_unregister(struct iio_dev *indio_dev) |
7026ea4b | 359 | { |
14555b14 JC |
360 | kfree(indio_dev->buffer->scan_mask); |
361 | kfree(indio_dev->buffer->scan_el_group.attrs); | |
362 | __iio_buffer_attr_cleanup(indio_dev); | |
7026ea4b | 363 | } |
14555b14 | 364 | EXPORT_SYMBOL(iio_buffer_unregister); |
7026ea4b | 365 | |
14555b14 JC |
366 | ssize_t iio_buffer_read_length(struct device *dev, |
367 | struct device_attribute *attr, | |
368 | char *buf) | |
7026ea4b | 369 | { |
1aa04278 | 370 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
14555b14 | 371 | struct iio_buffer *buffer = indio_dev->buffer; |
7026ea4b | 372 | |
14555b14 | 373 | if (buffer->access->get_length) |
8d213f24 | 374 | return sprintf(buf, "%d\n", |
14555b14 | 375 | buffer->access->get_length(buffer)); |
7026ea4b | 376 | |
8d213f24 | 377 | return 0; |
7026ea4b | 378 | } |
14555b14 | 379 | EXPORT_SYMBOL(iio_buffer_read_length); |
7026ea4b | 380 | |
14555b14 JC |
381 | ssize_t iio_buffer_write_length(struct device *dev, |
382 | struct device_attribute *attr, | |
383 | const char *buf, | |
384 | size_t len) | |
7026ea4b JC |
385 | { |
386 | int ret; | |
387 | ulong val; | |
1aa04278 | 388 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
14555b14 | 389 | struct iio_buffer *buffer = indio_dev->buffer; |
8d213f24 | 390 | |
7026ea4b JC |
391 | ret = strict_strtoul(buf, 10, &val); |
392 | if (ret) | |
393 | return ret; | |
394 | ||
14555b14 JC |
395 | if (buffer->access->get_length) |
396 | if (val == buffer->access->get_length(buffer)) | |
7026ea4b JC |
397 | return len; |
398 | ||
14555b14 JC |
399 | if (buffer->access->set_length) { |
400 | buffer->access->set_length(buffer, val); | |
401 | if (buffer->access->mark_param_change) | |
402 | buffer->access->mark_param_change(buffer); | |
7026ea4b JC |
403 | } |
404 | ||
405 | return len; | |
406 | } | |
14555b14 | 407 | EXPORT_SYMBOL(iio_buffer_write_length); |
7026ea4b | 408 | |
14555b14 JC |
409 | ssize_t iio_buffer_store_enable(struct device *dev, |
410 | struct device_attribute *attr, | |
411 | const char *buf, | |
412 | size_t len) | |
7026ea4b JC |
413 | { |
414 | int ret; | |
415 | bool requested_state, current_state; | |
416 | int previous_mode; | |
f8c6f4e9 JC |
417 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
418 | struct iio_buffer *buffer = indio_dev->buffer; | |
7026ea4b | 419 | |
f8c6f4e9 JC |
420 | mutex_lock(&indio_dev->mlock); |
421 | previous_mode = indio_dev->currentmode; | |
7026ea4b | 422 | requested_state = !(buf[0] == '0'); |
d4a6882e | 423 | current_state = iio_buffer_enabled(indio_dev); |
7026ea4b | 424 | if (current_state == requested_state) { |
14555b14 | 425 | printk(KERN_INFO "iio-buffer, current state requested again\n"); |
7026ea4b JC |
426 | goto done; |
427 | } | |
428 | if (requested_state) { | |
1612244f JC |
429 | if (indio_dev->setup_ops->preenable) { |
430 | ret = indio_dev->setup_ops->preenable(indio_dev); | |
7026ea4b JC |
431 | if (ret) { |
432 | printk(KERN_ERR | |
433 | "Buffer not started:" | |
14555b14 | 434 | "buffer preenable failed\n"); |
7026ea4b JC |
435 | goto error_ret; |
436 | } | |
437 | } | |
14555b14 JC |
438 | if (buffer->access->request_update) { |
439 | ret = buffer->access->request_update(buffer); | |
7026ea4b JC |
440 | if (ret) { |
441 | printk(KERN_INFO | |
442 | "Buffer not started:" | |
14555b14 | 443 | "buffer parameter update failed\n"); |
7026ea4b JC |
444 | goto error_ret; |
445 | } | |
446 | } | |
14555b14 JC |
447 | if (buffer->access->mark_in_use) |
448 | buffer->access->mark_in_use(buffer); | |
7026ea4b | 449 | /* Definitely possible for devices to support both of these.*/ |
f8c6f4e9 JC |
450 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { |
451 | if (!indio_dev->trig) { | |
7026ea4b JC |
452 | printk(KERN_INFO |
453 | "Buffer not started: no trigger\n"); | |
454 | ret = -EINVAL; | |
14555b14 JC |
455 | if (buffer->access->unmark_in_use) |
456 | buffer->access->unmark_in_use(buffer); | |
7026ea4b JC |
457 | goto error_ret; |
458 | } | |
f8c6f4e9 JC |
459 | indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; |
460 | } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) | |
461 | indio_dev->currentmode = INDIO_BUFFER_HARDWARE; | |
7026ea4b JC |
462 | else { /* should never be reached */ |
463 | ret = -EINVAL; | |
464 | goto error_ret; | |
465 | } | |
466 | ||
1612244f JC |
467 | if (indio_dev->setup_ops->postenable) { |
468 | ret = indio_dev->setup_ops->postenable(indio_dev); | |
7026ea4b JC |
469 | if (ret) { |
470 | printk(KERN_INFO | |
471 | "Buffer not started:" | |
472 | "postenable failed\n"); | |
14555b14 JC |
473 | if (buffer->access->unmark_in_use) |
474 | buffer->access->unmark_in_use(buffer); | |
f8c6f4e9 | 475 | indio_dev->currentmode = previous_mode; |
1612244f JC |
476 | if (indio_dev->setup_ops->postdisable) |
477 | indio_dev->setup_ops-> | |
f8c6f4e9 | 478 | postdisable(indio_dev); |
7026ea4b JC |
479 | goto error_ret; |
480 | } | |
481 | } | |
482 | } else { | |
1612244f JC |
483 | if (indio_dev->setup_ops->predisable) { |
484 | ret = indio_dev->setup_ops->predisable(indio_dev); | |
7026ea4b JC |
485 | if (ret) |
486 | goto error_ret; | |
487 | } | |
14555b14 JC |
488 | if (buffer->access->unmark_in_use) |
489 | buffer->access->unmark_in_use(buffer); | |
f8c6f4e9 | 490 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
1612244f JC |
491 | if (indio_dev->setup_ops->postdisable) { |
492 | ret = indio_dev->setup_ops->postdisable(indio_dev); | |
7026ea4b JC |
493 | if (ret) |
494 | goto error_ret; | |
495 | } | |
496 | } | |
497 | done: | |
f8c6f4e9 | 498 | mutex_unlock(&indio_dev->mlock); |
7026ea4b JC |
499 | return len; |
500 | ||
501 | error_ret: | |
f8c6f4e9 | 502 | mutex_unlock(&indio_dev->mlock); |
7026ea4b JC |
503 | return ret; |
504 | } | |
14555b14 | 505 | EXPORT_SYMBOL(iio_buffer_store_enable); |
8d213f24 | 506 | |
14555b14 JC |
507 | ssize_t iio_buffer_show_enable(struct device *dev, |
508 | struct device_attribute *attr, | |
509 | char *buf) | |
7026ea4b | 510 | { |
f8c6f4e9 | 511 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
d4a6882e | 512 | return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev)); |
7026ea4b | 513 | } |
14555b14 | 514 | EXPORT_SYMBOL(iio_buffer_show_enable); |
7026ea4b | 515 | |
32b5eeca JC |
516 | /* note NULL used as error indicator as it doesn't make sense. */ |
517 | static unsigned long *iio_scan_mask_match(unsigned long *av_masks, | |
518 | unsigned int masklength, | |
519 | unsigned long *mask) | |
520 | { | |
521 | if (bitmap_empty(mask, masklength)) | |
522 | return NULL; | |
523 | while (*av_masks) { | |
524 | if (bitmap_subset(mask, av_masks, masklength)) | |
525 | return av_masks; | |
526 | av_masks += BITS_TO_LONGS(masklength); | |
527 | } | |
528 | return NULL; | |
529 | } | |
530 | ||
959d2952 JC |
531 | int iio_sw_buffer_preenable(struct iio_dev *indio_dev) |
532 | { | |
533 | struct iio_buffer *buffer = indio_dev->buffer; | |
534 | const struct iio_chan_spec *ch; | |
535 | unsigned bytes = 0; | |
536 | int length, i; | |
537 | dev_dbg(&indio_dev->dev, "%s\n", __func__); | |
538 | ||
539 | /* How much space will the demuxed element take? */ | |
540 | for_each_set_bit(i, buffer->scan_mask, | |
541 | indio_dev->masklength) { | |
542 | ch = iio_find_channel_from_si(indio_dev, i); | |
543 | length = ch->scan_type.storagebits/8; | |
544 | bytes = ALIGN(bytes, length); | |
545 | bytes += length; | |
546 | } | |
547 | if (buffer->scan_timestamp) { | |
548 | ch = iio_find_channel_from_si(indio_dev, | |
549 | buffer->scan_index_timestamp); | |
550 | length = ch->scan_type.storagebits/8; | |
551 | bytes = ALIGN(bytes, length); | |
552 | bytes += length; | |
553 | } | |
554 | buffer->access->set_bytes_per_datum(buffer, bytes); | |
555 | ||
556 | /* What scan mask do we actually have ?*/ | |
557 | if (indio_dev->available_scan_masks) | |
558 | indio_dev->active_scan_mask = | |
559 | iio_scan_mask_match(indio_dev->available_scan_masks, | |
560 | indio_dev->masklength, | |
561 | buffer->scan_mask); | |
562 | else | |
563 | indio_dev->active_scan_mask = buffer->scan_mask; | |
5ada4ea9 JC |
564 | iio_update_demux(indio_dev); |
565 | ||
566 | if (indio_dev->info->update_scan_mode) | |
567 | return indio_dev->info | |
568 | ->update_scan_mode(indio_dev, | |
569 | indio_dev->active_scan_mask); | |
959d2952 JC |
570 | return 0; |
571 | } | |
572 | EXPORT_SYMBOL(iio_sw_buffer_preenable); | |
573 | ||
32b5eeca JC |
574 | /** |
575 | * iio_scan_mask_set() - set particular bit in the scan mask | |
14555b14 | 576 | * @buffer: the buffer whose scan mask we are interested in |
32b5eeca JC |
577 | * @bit: the bit to be set. |
578 | **/ | |
f79a9098 JC |
579 | int iio_scan_mask_set(struct iio_dev *indio_dev, |
580 | struct iio_buffer *buffer, int bit) | |
32b5eeca | 581 | { |
32b5eeca JC |
582 | unsigned long *mask; |
583 | unsigned long *trialmask; | |
584 | ||
585 | trialmask = kmalloc(sizeof(*trialmask)* | |
f8c6f4e9 | 586 | BITS_TO_LONGS(indio_dev->masklength), |
32b5eeca JC |
587 | GFP_KERNEL); |
588 | ||
589 | if (trialmask == NULL) | |
590 | return -ENOMEM; | |
f8c6f4e9 | 591 | if (!indio_dev->masklength) { |
14555b14 | 592 | WARN_ON("trying to set scanmask prior to registering buffer\n"); |
32b5eeca JC |
593 | kfree(trialmask); |
594 | return -EINVAL; | |
595 | } | |
f8c6f4e9 | 596 | bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); |
32b5eeca JC |
597 | set_bit(bit, trialmask); |
598 | ||
f8c6f4e9 JC |
599 | if (indio_dev->available_scan_masks) { |
600 | mask = iio_scan_mask_match(indio_dev->available_scan_masks, | |
601 | indio_dev->masklength, | |
32b5eeca JC |
602 | trialmask); |
603 | if (!mask) { | |
604 | kfree(trialmask); | |
605 | return -EINVAL; | |
606 | } | |
607 | } | |
f8c6f4e9 | 608 | bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); |
32b5eeca JC |
609 | |
610 | kfree(trialmask); | |
611 | ||
612 | return 0; | |
613 | }; | |
614 | EXPORT_SYMBOL_GPL(iio_scan_mask_set); | |
615 | ||
f79a9098 JC |
616 | int iio_scan_mask_query(struct iio_dev *indio_dev, |
617 | struct iio_buffer *buffer, int bit) | |
32b5eeca | 618 | { |
f8c6f4e9 | 619 | if (bit > indio_dev->masklength) |
32b5eeca JC |
620 | return -EINVAL; |
621 | ||
14555b14 | 622 | if (!buffer->scan_mask) |
32b5eeca | 623 | return 0; |
32b5eeca | 624 | |
5a2a6e11 | 625 | return test_bit(bit, buffer->scan_mask); |
32b5eeca JC |
626 | }; |
627 | EXPORT_SYMBOL_GPL(iio_scan_mask_query); | |
5ada4ea9 JC |
628 | |
629 | /** | |
630 | * struct iio_demux_table() - table describing demux memcpy ops | |
631 | * @from: index to copy from | |
632 | * @to: index to copy to | |
633 | * @length: how many bytes to copy | |
634 | * @l: list head used for management | |
635 | */ | |
636 | struct iio_demux_table { | |
637 | unsigned from; | |
638 | unsigned to; | |
639 | unsigned length; | |
640 | struct list_head l; | |
641 | }; | |
642 | ||
643 | static unsigned char *iio_demux(struct iio_buffer *buffer, | |
644 | unsigned char *datain) | |
645 | { | |
646 | struct iio_demux_table *t; | |
647 | ||
648 | if (list_empty(&buffer->demux_list)) | |
649 | return datain; | |
650 | list_for_each_entry(t, &buffer->demux_list, l) | |
651 | memcpy(buffer->demux_bounce + t->to, | |
652 | datain + t->from, t->length); | |
653 | ||
654 | return buffer->demux_bounce; | |
655 | } | |
656 | ||
657 | int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data, | |
658 | s64 timestamp) | |
659 | { | |
660 | unsigned char *dataout = iio_demux(buffer, data); | |
661 | ||
662 | return buffer->access->store_to(buffer, dataout, timestamp); | |
663 | } | |
664 | EXPORT_SYMBOL_GPL(iio_push_to_buffer); | |
665 | ||
666 | int iio_update_demux(struct iio_dev *indio_dev) | |
667 | { | |
668 | const struct iio_chan_spec *ch; | |
669 | struct iio_buffer *buffer = indio_dev->buffer; | |
670 | int ret, in_ind = -1, out_ind, length; | |
671 | unsigned in_loc = 0, out_loc = 0; | |
672 | struct iio_demux_table *p, *q; | |
673 | ||
674 | /* Clear out any old demux */ | |
675 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { | |
676 | list_del(&p->l); | |
677 | kfree(p); | |
678 | } | |
679 | kfree(buffer->demux_bounce); | |
680 | buffer->demux_bounce = NULL; | |
681 | ||
682 | /* First work out which scan mode we will actually have */ | |
683 | if (bitmap_equal(indio_dev->active_scan_mask, | |
684 | buffer->scan_mask, | |
685 | indio_dev->masklength)) | |
686 | return 0; | |
687 | ||
688 | /* Now we have the two masks, work from least sig and build up sizes */ | |
689 | for_each_set_bit(out_ind, | |
690 | indio_dev->active_scan_mask, | |
691 | indio_dev->masklength) { | |
692 | in_ind = find_next_bit(indio_dev->active_scan_mask, | |
693 | indio_dev->masklength, | |
694 | in_ind + 1); | |
695 | while (in_ind != out_ind) { | |
696 | in_ind = find_next_bit(indio_dev->active_scan_mask, | |
697 | indio_dev->masklength, | |
698 | in_ind + 1); | |
699 | ch = iio_find_channel_from_si(indio_dev, in_ind); | |
700 | length = ch->scan_type.storagebits/8; | |
701 | /* Make sure we are aligned */ | |
702 | in_loc += length; | |
703 | if (in_loc % length) | |
704 | in_loc += length - in_loc % length; | |
705 | } | |
706 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
707 | if (p == NULL) { | |
708 | ret = -ENOMEM; | |
709 | goto error_clear_mux_table; | |
710 | } | |
711 | ch = iio_find_channel_from_si(indio_dev, in_ind); | |
712 | length = ch->scan_type.storagebits/8; | |
713 | if (out_loc % length) | |
714 | out_loc += length - out_loc % length; | |
715 | if (in_loc % length) | |
716 | in_loc += length - in_loc % length; | |
717 | p->from = in_loc; | |
718 | p->to = out_loc; | |
719 | p->length = length; | |
720 | list_add_tail(&p->l, &buffer->demux_list); | |
721 | out_loc += length; | |
722 | in_loc += length; | |
723 | } | |
724 | /* Relies on scan_timestamp being last */ | |
725 | if (buffer->scan_timestamp) { | |
726 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
727 | if (p == NULL) { | |
728 | ret = -ENOMEM; | |
729 | goto error_clear_mux_table; | |
730 | } | |
731 | ch = iio_find_channel_from_si(indio_dev, | |
732 | buffer->scan_index_timestamp); | |
733 | length = ch->scan_type.storagebits/8; | |
734 | if (out_loc % length) | |
735 | out_loc += length - out_loc % length; | |
736 | if (in_loc % length) | |
737 | in_loc += length - in_loc % length; | |
738 | p->from = in_loc; | |
739 | p->to = out_loc; | |
740 | p->length = length; | |
741 | list_add_tail(&p->l, &buffer->demux_list); | |
742 | out_loc += length; | |
743 | in_loc += length; | |
744 | } | |
745 | buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); | |
746 | if (buffer->demux_bounce == NULL) { | |
747 | ret = -ENOMEM; | |
748 | goto error_clear_mux_table; | |
749 | } | |
750 | return 0; | |
751 | ||
752 | error_clear_mux_table: | |
753 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { | |
754 | list_del(&p->l); | |
755 | kfree(p); | |
756 | } | |
757 | return ret; | |
758 | } | |
759 | EXPORT_SYMBOL_GPL(iio_update_demux); |