16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/poll.h>
29 static const char *
const iio_endian_prefix[] = {
41 size_t n, loff_t *f_ps)
44 struct iio_buffer *
rb = indio_dev->
buffer;
46 if (!rb || !rb->access->read_first_n)
48 return rb->access->read_first_n(rb, n, buf);
58 struct iio_buffer *
rb = indio_dev->
buffer;
60 poll_wait(filp, &rb->pollq, wait);
69 INIT_LIST_HEAD(&buffer->demux_list);
86 u8 type = this_attr->
c->scan_type.endianness;
89 #ifdef __LITTLE_ENDIAN
95 return sprintf(buf,
"%s:%c%d/%d>>%u\n",
96 iio_endian_prefix[type],
97 this_attr->
c->scan_type.sign,
98 this_attr->
c->scan_type.realbits,
99 this_attr->
c->scan_type.storagebits,
100 this_attr->
c->scan_type.shift);
108 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
111 indio_dev->
buffer->scan_mask);
113 return sprintf(buf,
"%d\n", ret);
116 static int iio_scan_mask_clear(
struct iio_buffer *
buffer,
int bit)
129 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
137 if (iio_buffer_enabled(indio_dev)) {
145 ret = iio_scan_mask_clear(buffer, this_attr->
address);
148 }
else if (state && !ret) {
157 return ret < 0 ? ret : len;
165 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
166 return sprintf(buf,
"%d\n", indio_dev->
buffer->scan_timestamp);
175 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
183 if (iio_buffer_enabled(indio_dev)) {
192 return ret ? ret : len;
195 static int iio_buffer_add_channel_sysfs(
struct iio_dev *indio_dev,
198 int ret, attrcount = 0;
199 struct iio_buffer *buffer = indio_dev->
buffer;
203 &iio_show_scan_index,
208 &buffer->scan_el_dev_attr_list);
214 &iio_show_fixed_type,
219 &buffer->scan_el_dev_attr_list);
231 &buffer->scan_el_dev_attr_list);
235 &iio_scan_el_ts_show,
236 &iio_scan_el_ts_store,
240 &buffer->scan_el_dev_attr_list);
247 static void iio_buffer_remove_and_free_scan_dev_attr(
struct iio_dev *indio_dev,
254 static void __iio_buffer_attr_cleanup(
struct iio_dev *indio_dev)
257 struct iio_buffer *buffer = indio_dev->
buffer;
260 &buffer->scan_el_dev_attr_list,
l)
261 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
264 static
const char *
const iio_scan_elements_group_name = "scan_elements";
272 struct iio_buffer *buffer = indio_dev->buffer;
273 int ret,
i, attrn, attrcount, attrcount_orig = 0;
276 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
278 if (buffer->scan_el_attrs !=
NULL) {
279 attr = buffer->scan_el_attrs->attrs;
280 while (*attr++ !=
NULL)
283 attrcount = attrcount_orig;
284 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
288 if (channels[i].scan_index < 0)
292 if (channels[i].scan_index >
293 (
int)indio_dev->masklength - 1)
294 indio_dev->masklength
295 = channels[
i].scan_index + 1;
297 ret = iio_buffer_add_channel_sysfs(indio_dev,
300 goto error_cleanup_dynamic;
303 indio_dev->scan_index_timestamp =
304 channels[
i].scan_index;
306 if (indio_dev->masklength && buffer->scan_mask ==
NULL) {
307 buffer->scan_mask = kcalloc(
BITS_TO_LONGS(indio_dev->masklength),
308 sizeof(*buffer->scan_mask),
310 if (buffer->scan_mask ==
NULL) {
312 goto error_cleanup_dynamic;
317 buffer->scan_el_group.name = iio_scan_elements_group_name;
319 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
320 sizeof(buffer->scan_el_group.attrs[0]),
322 if (buffer->scan_el_group.attrs ==
NULL) {
324 goto error_free_scan_mask;
326 if (buffer->scan_el_attrs)
327 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
328 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
329 attrn = attrcount_orig;
332 buffer->scan_el_group.attrs[attrn++] = &p->
dev_attr.attr;
333 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
337 error_free_scan_mask:
338 kfree(buffer->scan_mask);
339 error_cleanup_dynamic:
340 __iio_buffer_attr_cleanup(indio_dev);
350 __iio_buffer_attr_cleanup(indio_dev);
358 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
359 struct iio_buffer *buffer = indio_dev->
buffer;
361 if (buffer->access->get_length)
363 buffer->access->get_length(buffer));
376 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
377 struct iio_buffer *buffer = indio_dev->
buffer;
383 if (buffer->access->get_length)
384 if (val == buffer->access->get_length(buffer))
388 if (iio_buffer_enabled(indio_dev)) {
391 if (buffer->access->set_length)
392 buffer->access->set_length(buffer, val);
397 return ret ? ret : len;
407 bool requested_state, current_state;
409 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
410 struct iio_buffer *buffer = indio_dev->
buffer;
414 requested_state = !(buf[0] ==
'0');
415 current_state = iio_buffer_enabled(indio_dev);
416 if (current_state == requested_state) {
420 if (requested_state) {
422 ret = indio_dev->
setup_ops->preenable(indio_dev);
425 "Buffer not started: "
426 "buffer preenable failed\n");
430 if (buffer->access->request_update) {
431 ret = buffer->access->request_update(buffer);
434 "Buffer not started: "
435 "buffer parameter update failed\n");
441 if (!indio_dev->
trig) {
443 "Buffer not started: no trigger\n");
456 ret = indio_dev->
setup_ops->postenable(indio_dev);
459 "Buffer not started: "
460 "postenable failed\n");
464 postdisable(indio_dev);
470 ret = indio_dev->
setup_ops->predisable(indio_dev);
476 ret = indio_dev->
setup_ops->postdisable(indio_dev);
495 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
496 return sprintf(buf,
"%d\n", iio_buffer_enabled(indio_dev));
501 static const unsigned long *iio_scan_mask_match(
const unsigned long *av_masks,
503 const unsigned long *
mask)
505 if (bitmap_empty(mask, masklength))
508 if (bitmap_subset(mask, av_masks, masklength))
515 static int iio_compute_scan_bytes(
struct iio_dev *indio_dev,
const long *mask,
527 bytes =
ALIGN(bytes, length);
534 bytes =
ALIGN(bytes, length);
542 struct iio_buffer *buffer = indio_dev->
buffer;
547 iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
549 buffer->access->set_bytes_per_datum(buffer, indio_dev->
scan_bytes);
565 if (indio_dev->
info->update_scan_mode)
566 return indio_dev->
info
567 ->update_scan_mode(indio_dev,
583 const unsigned long *mask)
585 return bitmap_weight(mask, indio_dev->
masklength) == 1;
589 static bool iio_validate_scan_mask(
struct iio_dev *indio_dev,
590 const unsigned long *mask)
592 if (!indio_dev->
setup_ops->validate_scan_mask)
595 return indio_dev->
setup_ops->validate_scan_mask(indio_dev, mask);
604 struct iio_buffer *buffer,
int bit)
606 const unsigned long *
mask;
607 unsigned long *trialmask;
609 trialmask =
kmalloc(
sizeof(*trialmask)*
613 if (trialmask ==
NULL)
616 WARN_ON(
"trying to set scanmask prior to registering buffer\n");
617 goto err_invalid_mask;
619 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->
masklength);
622 if (!iio_validate_scan_mask(indio_dev, trialmask))
623 goto err_invalid_mask;
630 goto err_invalid_mask;
632 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->
masklength);
645 struct iio_buffer *buffer,
int bit)
650 if (!buffer->scan_mask)
653 return test_bit(bit, buffer->scan_mask);
671 static unsigned char *iio_demux(
struct iio_buffer *buffer,
672 unsigned char *datain)
676 if (list_empty(&buffer->demux_list))
679 memcpy(buffer->demux_bounce + t->
to,
680 datain + t->
from, t->length);
682 return buffer->demux_bounce;
687 unsigned char *dataout = iio_demux(buffer, data);
689 return buffer->access->store_to(buffer, dataout);
693 static void iio_buffer_demux_free(
struct iio_buffer *buffer)
705 struct iio_buffer *buffer = indio_dev->
buffer;
707 unsigned in_loc = 0, out_loc = 0;
711 iio_buffer_demux_free(buffer);
712 kfree(buffer->demux_bounce);
713 buffer->demux_bounce =
NULL;
728 while (in_ind != out_ind) {
737 in_loc += length - in_loc %
length;
742 goto error_clear_mux_table;
746 if (out_loc % length)
747 out_loc += length - out_loc %
length;
749 in_loc += length - in_loc %
length;
758 if (buffer->scan_timestamp) {
762 goto error_clear_mux_table;
767 if (out_loc % length)
768 out_loc += length - out_loc %
length;
770 in_loc += length - in_loc %
length;
778 buffer->demux_bounce = kzalloc(out_loc,
GFP_KERNEL);
779 if (buffer->demux_bounce ==
NULL) {
781 goto error_clear_mux_table;
785 error_clear_mux_table:
786 iio_buffer_demux_free(buffer);