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