blob: 5a46c57a038b1231ed2541a6402d442048d1cfe3 [file] [log] [blame]
Jonathan Cameron14555b12011-09-21 11:15:57 +01001/* 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 *
9 * Handling of buffer allocation / resizing.
10 *
11 *
12 * Things to look at here.
13 * - Better memory allocation techniques?
14 * - Alternative access techniques?
15 */
16#include <linux/kernel.h>
Paul Gortmaker8e336a72011-07-10 13:09:12 -040017#include <linux/export.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010018#include <linux/device.h>
19#include <linux/fs.h>
20#include <linux/cdev.h>
21#include <linux/slab.h>
22#include <linux/poll.h>
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +010023#include <linux/sched.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010024
Jonathan Cameron06458e22012-04-25 15:54:58 +010025#include <linux/iio/iio.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010026#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010027#include <linux/iio/sysfs.h>
28#include <linux/iio/buffer.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010029
30static const char * const iio_endian_prefix[] = {
31 [IIO_BE] = "be",
32 [IIO_LE] = "le",
33};
34
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010035static bool iio_buffer_is_active(struct iio_buffer *buf)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010036{
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010037 return !list_empty(&buf->buffer_list);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010038}
39
Jonathan Cameron14555b12011-09-21 11:15:57 +010040/**
41 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
42 *
43 * This function relies on all buffer implementations having an
44 * iio_buffer as their first element.
45 **/
46ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
47 size_t n, loff_t *f_ps)
48{
49 struct iio_dev *indio_dev = filp->private_data;
50 struct iio_buffer *rb = indio_dev->buffer;
51
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010052 if (!indio_dev->info)
53 return -ENODEV;
54
Jonathan Cameron96e00f12011-10-26 17:27:45 +010055 if (!rb || !rb->access->read_first_n)
Jonathan Cameron14555b12011-09-21 11:15:57 +010056 return -EINVAL;
57 return rb->access->read_first_n(rb, n, buf);
58}
59
60/**
61 * iio_buffer_poll() - poll the buffer to find out if it has data
62 */
63unsigned int iio_buffer_poll(struct file *filp,
64 struct poll_table_struct *wait)
65{
66 struct iio_dev *indio_dev = filp->private_data;
67 struct iio_buffer *rb = indio_dev->buffer;
68
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010069 if (!indio_dev->info)
70 return -ENODEV;
71
Jonathan Cameron14555b12011-09-21 11:15:57 +010072 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... */
76 return 0;
77}
78
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +010079/**
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 */
86void 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
Jonathan Cameronf79a9092011-12-05 22:18:29 +000094void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +010095{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000096 INIT_LIST_HEAD(&buffer->demux_list);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010097 INIT_LIST_HEAD(&buffer->buffer_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +010098 init_waitqueue_head(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010099 kref_init(&buffer->ref);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100100}
101EXPORT_SYMBOL(iio_buffer_init);
102
103static ssize_t iio_show_scan_index(struct device *dev,
104 struct device_attribute *attr,
105 char *buf)
106{
107 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
108}
109
110static 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);
115 u8 type = this_attr->c->scan_type.endianness;
116
117 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +0100118#ifdef __LITTLE_ENDIAN
119 type = IIO_LE;
120#else
121 type = IIO_BE;
122#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100123 }
124 return sprintf(buf, "%s:%c%d/%d>>%u\n",
125 iio_endian_prefix[type],
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
132static ssize_t iio_scan_el_show(struct device *dev,
133 struct device_attribute *attr,
134 char *buf)
135{
136 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200137 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100138
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000139 ret = test_bit(to_iio_dev_attr(attr)->address,
140 indio_dev->buffer->scan_mask);
141
Jonathan Cameron14555b12011-09-21 11:15:57 +0100142 return sprintf(buf, "%d\n", ret);
143}
144
145static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
146{
147 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100148 return 0;
149}
150
151static ssize_t iio_scan_el_store(struct device *dev,
152 struct device_attribute *attr,
153 const char *buf,
154 size_t len)
155{
Jonathan Camerona714af22012-04-21 10:09:32 +0100156 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100157 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200158 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100159 struct iio_buffer *buffer = indio_dev->buffer;
160 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
161
Jonathan Camerona714af22012-04-21 10:09:32 +0100162 ret = strtobool(buf, &state);
163 if (ret < 0)
164 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100165 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100166 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100167 ret = -EBUSY;
168 goto error_ret;
169 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000170 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100171 if (ret < 0)
172 goto error_ret;
173 if (!state && ret) {
174 ret = iio_scan_mask_clear(buffer, this_attr->address);
175 if (ret)
176 goto error_ret;
177 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000178 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100179 if (ret)
180 goto error_ret;
181 }
182
183error_ret:
184 mutex_unlock(&indio_dev->mlock);
185
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100186 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100187
188}
189
190static ssize_t iio_scan_el_ts_show(struct device *dev,
191 struct device_attribute *attr,
192 char *buf)
193{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200194 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100195 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100196}
197
198static ssize_t iio_scan_el_ts_store(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf,
201 size_t len)
202{
Jonathan Camerona714af22012-04-21 10:09:32 +0100203 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200204 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100205 bool state;
206
Jonathan Camerona714af22012-04-21 10:09:32 +0100207 ret = strtobool(buf, &state);
208 if (ret < 0)
209 return ret;
210
Jonathan Cameron14555b12011-09-21 11:15:57 +0100211 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100212 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100213 ret = -EBUSY;
214 goto error_ret;
215 }
216 indio_dev->buffer->scan_timestamp = state;
217error_ret:
218 mutex_unlock(&indio_dev->mlock);
219
220 return ret ? ret : len;
221}
222
223static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
224 const struct iio_chan_spec *chan)
225{
226 int ret, attrcount = 0;
227 struct iio_buffer *buffer = indio_dev->buffer;
228
229 ret = __iio_add_chan_devattr("index",
230 chan,
231 &iio_show_scan_index,
232 NULL,
233 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100234 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100235 &indio_dev->dev,
236 &buffer->scan_el_dev_attr_list);
237 if (ret)
238 goto error_ret;
239 attrcount++;
240 ret = __iio_add_chan_devattr("type",
241 chan,
242 &iio_show_fixed_type,
243 NULL,
244 0,
245 0,
246 &indio_dev->dev,
247 &buffer->scan_el_dev_attr_list);
248 if (ret)
249 goto error_ret;
250 attrcount++;
251 if (chan->type != IIO_TIMESTAMP)
252 ret = __iio_add_chan_devattr("en",
253 chan,
254 &iio_scan_el_show,
255 &iio_scan_el_store,
256 chan->scan_index,
257 0,
258 &indio_dev->dev,
259 &buffer->scan_el_dev_attr_list);
260 else
261 ret = __iio_add_chan_devattr("en",
262 chan,
263 &iio_scan_el_ts_show,
264 &iio_scan_el_ts_store,
265 chan->scan_index,
266 0,
267 &indio_dev->dev,
268 &buffer->scan_el_dev_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100269 if (ret)
270 goto error_ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100271 attrcount++;
272 ret = attrcount;
273error_ret:
274 return ret;
275}
276
277static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
278 struct iio_dev_attr *p)
279{
280 kfree(p->dev_attr.attr.name);
281 kfree(p);
282}
283
284static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
285{
286 struct iio_dev_attr *p, *n;
287 struct iio_buffer *buffer = indio_dev->buffer;
288
289 list_for_each_entry_safe(p, n,
290 &buffer->scan_el_dev_attr_list, l)
291 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
292}
293
294static const char * const iio_scan_elements_group_name = "scan_elements";
295
296int iio_buffer_register(struct iio_dev *indio_dev,
297 const struct iio_chan_spec *channels,
298 int num_channels)
299{
300 struct iio_dev_attr *p;
301 struct attribute **attr;
302 struct iio_buffer *buffer = indio_dev->buffer;
303 int ret, i, attrn, attrcount, attrcount_orig = 0;
304
305 if (buffer->attrs)
306 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
307
308 if (buffer->scan_el_attrs != NULL) {
309 attr = buffer->scan_el_attrs->attrs;
310 while (*attr++ != NULL)
311 attrcount_orig++;
312 }
313 attrcount = attrcount_orig;
314 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
315 if (channels) {
316 /* new magic */
317 for (i = 0; i < num_channels; i++) {
Lars-Peter Clausenf5b81dd2012-06-18 18:33:47 +0200318 if (channels[i].scan_index < 0)
319 continue;
320
Jonathan Cameron14555b12011-09-21 11:15:57 +0100321 /* Establish necessary mask length */
322 if (channels[i].scan_index >
323 (int)indio_dev->masklength - 1)
324 indio_dev->masklength
Lars-Peter Clausene1dc7be2012-07-02 14:52:56 +0200325 = channels[i].scan_index + 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100326
327 ret = iio_buffer_add_channel_sysfs(indio_dev,
328 &channels[i]);
329 if (ret < 0)
330 goto error_cleanup_dynamic;
331 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000332 if (channels[i].type == IIO_TIMESTAMP)
Jonathan Cameronf1264802012-04-21 10:09:34 +0100333 indio_dev->scan_index_timestamp =
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000334 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100335 }
336 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100337 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
338 sizeof(*buffer->scan_mask),
339 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100340 if (buffer->scan_mask == NULL) {
341 ret = -ENOMEM;
342 goto error_cleanup_dynamic;
343 }
344 }
345 }
346
347 buffer->scan_el_group.name = iio_scan_elements_group_name;
348
Thomas Meyerd83fb182011-11-29 22:08:00 +0100349 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
350 sizeof(buffer->scan_el_group.attrs[0]),
351 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100352 if (buffer->scan_el_group.attrs == NULL) {
353 ret = -ENOMEM;
354 goto error_free_scan_mask;
355 }
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);
359 attrn = attrcount_orig;
360
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;
364
365 return 0;
366
367error_free_scan_mask:
368 kfree(buffer->scan_mask);
369error_cleanup_dynamic:
370 __iio_buffer_attr_cleanup(indio_dev);
371
372 return ret;
373}
374EXPORT_SYMBOL(iio_buffer_register);
375
376void iio_buffer_unregister(struct iio_dev *indio_dev)
377{
378 kfree(indio_dev->buffer->scan_mask);
379 kfree(indio_dev->buffer->scan_el_group.attrs);
380 __iio_buffer_attr_cleanup(indio_dev);
381}
382EXPORT_SYMBOL(iio_buffer_unregister);
383
384ssize_t iio_buffer_read_length(struct device *dev,
385 struct device_attribute *attr,
386 char *buf)
387{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200388 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100389 struct iio_buffer *buffer = indio_dev->buffer;
390
391 if (buffer->access->get_length)
392 return sprintf(buf, "%d\n",
393 buffer->access->get_length(buffer));
394
395 return 0;
396}
397EXPORT_SYMBOL(iio_buffer_read_length);
398
399ssize_t iio_buffer_write_length(struct device *dev,
400 struct device_attribute *attr,
401 const char *buf,
402 size_t len)
403{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200404 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100405 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100406 unsigned int val;
407 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100408
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100409 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100410 if (ret)
411 return ret;
412
413 if (buffer->access->get_length)
414 if (val == buffer->access->get_length(buffer))
415 return len;
416
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100417 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100418 if (iio_buffer_is_active(indio_dev->buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100419 ret = -EBUSY;
420 } else {
Lars-Peter Clausen869871b2011-12-19 15:23:48 +0100421 if (buffer->access->set_length)
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100422 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100423 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100424 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100425 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100426
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100427 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100428}
429EXPORT_SYMBOL(iio_buffer_write_length);
430
Jonathan Cameron14555b12011-09-21 11:15:57 +0100431ssize_t iio_buffer_show_enable(struct device *dev,
432 struct device_attribute *attr,
433 char *buf)
434{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200435 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100436 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100437}
438EXPORT_SYMBOL(iio_buffer_show_enable);
439
Peter Meerwald95725882013-09-17 23:42:00 +0100440/* Note NULL used as error indicator as it doesn't make sense. */
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100441static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100442 unsigned int masklength,
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100443 const unsigned long *mask)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100444{
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
Peter Meerwald183f4172013-09-18 22:10:00 +0100455static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
456 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000457{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000458 const struct iio_chan_spec *ch;
459 unsigned bytes = 0;
460 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100461
462 /* How much space will the demuxed element take? */
463 for_each_set_bit(i, mask,
464 indio_dev->masklength) {
465 ch = iio_find_channel_from_si(indio_dev, i);
466 length = ch->scan_type.storagebits / 8;
467 bytes = ALIGN(bytes, length);
468 bytes += length;
469 }
470 if (timestamp) {
471 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100472 indio_dev->scan_index_timestamp);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100473 length = ch->scan_type.storagebits / 8;
474 bytes = ALIGN(bytes, length);
475 bytes += length;
476 }
477 return bytes;
478}
479
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100480static 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
487static void iio_buffer_deactivate(struct iio_buffer *buffer)
488{
489 list_del_init(&buffer->buffer_list);
490 iio_buffer_put(buffer);
491}
492
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100493void 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)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100505 iio_buffer_deactivate(buffer);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100506
507 indio_dev->currentmode = INDIO_DIRECT_MODE;
508 if (indio_dev->setup_ops->postdisable)
509 indio_dev->setup_ops->postdisable(indio_dev);
510}
511
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100512int iio_update_buffers(struct iio_dev *indio_dev,
513 struct iio_buffer *insert_buffer,
514 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100515{
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100516 int ret;
517 int success = 0;
518 struct iio_buffer *buffer;
519 unsigned long *compound_mask;
520 const unsigned long *old_mask;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000521
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100522 /* 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)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100542 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100543 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100544 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100545
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 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000553
Peter Meerwald95725882013-09-17 23:42:00 +0100554 /* What scan mask do we actually have? */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100555 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) {
Jonathan Cameron959d2952011-12-05 21:37:13 +0000570 indio_dev->active_scan_mask =
571 iio_scan_mask_match(indio_dev->available_scan_masks,
572 indio_dev->masklength,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100573 compound_mask);
574 if (indio_dev->active_scan_mask == NULL) {
575 /*
576 * Roll back.
577 * Note can only occur when adding a buffer.
578 */
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100579 iio_buffer_deactivate(insert_buffer);
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100580 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 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100589 }
590 } else {
591 indio_dev->active_scan_mask = compound_mask;
592 }
Lars-Peter Clausenaff1eb42012-06-15 18:08:59 +0200593
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000594 iio_update_demux(indio_dev);
595
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100596 /* 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
Michał Mirosławbec18892013-05-04 14:19:00 +0100601 "Buffer not started: buffer preenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100602 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
Michał Mirosławbec18892013-05-04 14:19:00 +0100614 "Buffer not started: buffer parameter update failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100615 goto error_run_postdisable;
616 }
617 }
618 if (indio_dev->info->update_scan_mode) {
619 ret = indio_dev->info
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000620 ->update_scan_mode(indio_dev,
621 indio_dev->active_scan_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100622 if (ret < 0) {
Michał Mirosławbec18892013-05-04 14:19:00 +0100623 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100624 goto error_run_postdisable;
625 }
626 }
Peter Meerwald95725882013-09-17 23:42:00 +0100627 /* Definitely possible for devices to support both of these. */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100628 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;
Peter Meerwald95725882013-09-17 23:42:00 +0100638 } else { /* Should never be reached */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100639 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
Michał Mirosławbec18892013-05-04 14:19:00 +0100647 "Buffer not started: postenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100648 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
662error_disable_all_buffers:
663 indio_dev->currentmode = INDIO_DIRECT_MODE;
664error_run_postdisable:
665 if (indio_dev->setup_ops->postdisable)
666 indio_dev->setup_ops->postdisable(indio_dev);
667error_remove_inserted:
668
669 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100670 iio_buffer_deactivate(insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100671 indio_dev->active_scan_mask = old_mask;
672 kfree(compound_mask);
673error_ret:
674
675 return ret;
676}
677EXPORT_SYMBOL_GPL(iio_update_buffers);
678
679ssize_t iio_buffer_store_enable(struct device *dev,
680 struct device_attribute *attr,
681 const char *buf,
682 size_t len)
683{
684 int ret;
685 bool requested_state;
686 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100687 bool inlist;
688
689 ret = strtobool(buf, &requested_state);
690 if (ret < 0)
691 return ret;
692
693 mutex_lock(&indio_dev->mlock);
694
695 /* Find out if it is in the list */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100696 inlist = iio_buffer_is_active(indio_dev->buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100697 /* Already in desired state */
698 if (inlist == requested_state)
699 goto done;
700
701 if (requested_state)
702 ret = iio_update_buffers(indio_dev,
703 indio_dev->buffer, NULL);
704 else
705 ret = iio_update_buffers(indio_dev,
706 NULL, indio_dev->buffer);
707
708 if (ret < 0)
709 goto done;
710done:
711 mutex_unlock(&indio_dev->mlock);
712 return (ret < 0) ? ret : len;
713}
714EXPORT_SYMBOL(iio_buffer_store_enable);
715
716int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
717{
718 struct iio_buffer *buffer;
719 unsigned bytes;
720 dev_dbg(&indio_dev->dev, "%s\n", __func__);
721
722 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
723 if (buffer->access->set_bytes_per_datum) {
724 bytes = iio_compute_scan_bytes(indio_dev,
725 buffer->scan_mask,
726 buffer->scan_timestamp);
727
728 buffer->access->set_bytes_per_datum(buffer, bytes);
729 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000730 return 0;
731}
732EXPORT_SYMBOL(iio_sw_buffer_preenable);
733
Jonathan Cameron14555b12011-09-21 11:15:57 +0100734/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100735 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
736 * @indio_dev: the iio device
737 * @mask: scan mask to be checked
738 *
739 * Return true if exactly one bit is set in the scan mask, false otherwise. It
740 * can be used for devices where only one channel can be active for sampling at
741 * a time.
742 */
743bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
744 const unsigned long *mask)
745{
746 return bitmap_weight(mask, indio_dev->masklength) == 1;
747}
748EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
749
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100750static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
751 const unsigned long *mask)
752{
753 if (!indio_dev->setup_ops->validate_scan_mask)
754 return true;
755
756 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
757}
758
Jonathan Cameron14555b12011-09-21 11:15:57 +0100759/**
760 * iio_scan_mask_set() - set particular bit in the scan mask
Peter Meerwald95725882013-09-17 23:42:00 +0100761 * @indio_dev: the iio device
Jonathan Cameron14555b12011-09-21 11:15:57 +0100762 * @buffer: the buffer whose scan mask we are interested in
763 * @bit: the bit to be set.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100764 *
765 * Note that at this point we have no way of knowing what other
766 * buffers might request, hence this code only verifies that the
767 * individual buffers request is plausible.
768 */
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000769int iio_scan_mask_set(struct iio_dev *indio_dev,
770 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100771{
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100772 const unsigned long *mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100773 unsigned long *trialmask;
774
775 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100776 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100777 GFP_KERNEL);
778
779 if (trialmask == NULL)
780 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100781 if (!indio_dev->masklength) {
Peter Meerwald95725882013-09-17 23:42:00 +0100782 WARN_ON("Trying to set scanmask prior to registering buffer\n");
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100783 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100784 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100785 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100786 set_bit(bit, trialmask);
787
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100788 if (!iio_validate_scan_mask(indio_dev, trialmask))
789 goto err_invalid_mask;
790
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100791 if (indio_dev->available_scan_masks) {
792 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
793 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100794 trialmask);
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100795 if (!mask)
796 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100797 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100798 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100799
800 kfree(trialmask);
801
802 return 0;
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100803
804err_invalid_mask:
805 kfree(trialmask);
806 return -EINVAL;
807}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100808EXPORT_SYMBOL_GPL(iio_scan_mask_set);
809
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000810int iio_scan_mask_query(struct iio_dev *indio_dev,
811 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100812{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100813 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100814 return -EINVAL;
815
816 if (!buffer->scan_mask)
817 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100818
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100819 return test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100820};
821EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000822
823/**
824 * struct iio_demux_table() - table describing demux memcpy ops
825 * @from: index to copy from
Peter Meerwald99698b42012-08-26 13:43:00 +0100826 * @to: index to copy to
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000827 * @length: how many bytes to copy
828 * @l: list head used for management
829 */
830struct iio_demux_table {
831 unsigned from;
832 unsigned to;
833 unsigned length;
834 struct list_head l;
835};
836
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100837static const void *iio_demux(struct iio_buffer *buffer,
838 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000839{
840 struct iio_demux_table *t;
841
842 if (list_empty(&buffer->demux_list))
843 return datain;
844 list_for_each_entry(t, &buffer->demux_list, l)
845 memcpy(buffer->demux_bounce + t->to,
846 datain + t->from, t->length);
847
848 return buffer->demux_bounce;
849}
850
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100851static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000852{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100853 const void *dataout = iio_demux(buffer, data);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000854
Lars-Peter Clausence56ade2012-09-04 13:38:00 +0100855 return buffer->access->store_to(buffer, dataout);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000856}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000857
Jonathan Cameron842cd102012-04-21 10:09:45 +0100858static void iio_buffer_demux_free(struct iio_buffer *buffer)
859{
860 struct iio_demux_table *p, *q;
861 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
862 list_del(&p->l);
863 kfree(p);
864 }
865}
866
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100867
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100868int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100869{
870 int ret;
871 struct iio_buffer *buf;
872
873 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
874 ret = iio_push_to_buffer(buf, data);
875 if (ret < 0)
876 return ret;
877 }
878
879 return 0;
880}
881EXPORT_SYMBOL_GPL(iio_push_to_buffers);
882
883static int iio_buffer_update_demux(struct iio_dev *indio_dev,
884 struct iio_buffer *buffer)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000885{
886 const struct iio_chan_spec *ch;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000887 int ret, in_ind = -1, out_ind, length;
888 unsigned in_loc = 0, out_loc = 0;
Jonathan Cameron842cd102012-04-21 10:09:45 +0100889 struct iio_demux_table *p;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000890
891 /* Clear out any old demux */
Jonathan Cameron842cd102012-04-21 10:09:45 +0100892 iio_buffer_demux_free(buffer);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000893 kfree(buffer->demux_bounce);
894 buffer->demux_bounce = NULL;
895
896 /* First work out which scan mode we will actually have */
897 if (bitmap_equal(indio_dev->active_scan_mask,
898 buffer->scan_mask,
899 indio_dev->masklength))
900 return 0;
901
902 /* Now we have the two masks, work from least sig and build up sizes */
903 for_each_set_bit(out_ind,
904 indio_dev->active_scan_mask,
905 indio_dev->masklength) {
906 in_ind = find_next_bit(indio_dev->active_scan_mask,
907 indio_dev->masklength,
908 in_ind + 1);
909 while (in_ind != out_ind) {
910 in_ind = find_next_bit(indio_dev->active_scan_mask,
911 indio_dev->masklength,
912 in_ind + 1);
913 ch = iio_find_channel_from_si(indio_dev, in_ind);
914 length = ch->scan_type.storagebits/8;
915 /* Make sure we are aligned */
916 in_loc += length;
917 if (in_loc % length)
918 in_loc += length - in_loc % length;
919 }
920 p = kmalloc(sizeof(*p), GFP_KERNEL);
921 if (p == NULL) {
922 ret = -ENOMEM;
923 goto error_clear_mux_table;
924 }
925 ch = iio_find_channel_from_si(indio_dev, in_ind);
926 length = ch->scan_type.storagebits/8;
927 if (out_loc % length)
928 out_loc += length - out_loc % length;
929 if (in_loc % length)
930 in_loc += length - in_loc % length;
931 p->from = in_loc;
932 p->to = out_loc;
933 p->length = length;
934 list_add_tail(&p->l, &buffer->demux_list);
935 out_loc += length;
936 in_loc += length;
937 }
938 /* Relies on scan_timestamp being last */
939 if (buffer->scan_timestamp) {
940 p = kmalloc(sizeof(*p), GFP_KERNEL);
941 if (p == NULL) {
942 ret = -ENOMEM;
943 goto error_clear_mux_table;
944 }
945 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100946 indio_dev->scan_index_timestamp);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000947 length = ch->scan_type.storagebits/8;
948 if (out_loc % length)
949 out_loc += length - out_loc % length;
950 if (in_loc % length)
951 in_loc += length - in_loc % length;
952 p->from = in_loc;
953 p->to = out_loc;
954 p->length = length;
955 list_add_tail(&p->l, &buffer->demux_list);
956 out_loc += length;
957 in_loc += length;
958 }
959 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
960 if (buffer->demux_bounce == NULL) {
961 ret = -ENOMEM;
962 goto error_clear_mux_table;
963 }
964 return 0;
965
966error_clear_mux_table:
Jonathan Cameron842cd102012-04-21 10:09:45 +0100967 iio_buffer_demux_free(buffer);
968
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000969 return ret;
970}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100971
972int iio_update_demux(struct iio_dev *indio_dev)
973{
974 struct iio_buffer *buffer;
975 int ret;
976
977 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
978 ret = iio_buffer_update_demux(indio_dev, buffer);
979 if (ret < 0)
980 goto error_clear_mux_table;
981 }
982 return 0;
983
984error_clear_mux_table:
985 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
986 iio_buffer_demux_free(buffer);
987
988 return ret;
989}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000990EXPORT_SYMBOL_GPL(iio_update_demux);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100991
992/**
993 * iio_buffer_release() - Free a buffer's resources
994 * @ref: Pointer to the kref embedded in the iio_buffer struct
995 *
996 * This function is called when the last reference to the buffer has been
997 * dropped. It will typically free all resources allocated by the buffer. Do not
998 * call this function manually, always use iio_buffer_put() when done using a
999 * buffer.
1000 */
1001static void iio_buffer_release(struct kref *ref)
1002{
1003 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1004
1005 buffer->access->release(buffer);
1006}
1007
1008/**
1009 * iio_buffer_get() - Grab a reference to the buffer
1010 * @buffer: The buffer to grab a reference for, may be NULL
1011 *
1012 * Returns the pointer to the buffer that was passed into the function.
1013 */
1014struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1015{
1016 if (buffer)
1017 kref_get(&buffer->ref);
1018
1019 return buffer;
1020}
1021EXPORT_SYMBOL_GPL(iio_buffer_get);
1022
1023/**
1024 * iio_buffer_put() - Release the reference to the buffer
1025 * @buffer: The buffer to release the reference for, may be NULL
1026 */
1027void iio_buffer_put(struct iio_buffer *buffer)
1028{
1029 if (buffer)
1030 kref_put(&buffer->ref, iio_buffer_release);
1031}
1032EXPORT_SYMBOL_GPL(iio_buffer_put);