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