hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/iio/industrialio-buffer.c
....@@ -1,13 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* The industrial I/O core
23 *
34 * Copyright (c) 2008 Jonathan Cameron
45 *
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
- *
96 * Handling of buffer allocation / resizing.
10
- *
117 *
128 * Things to look at here.
139 * - Better memory allocation techniques?
....@@ -23,7 +19,9 @@
2319 #include <linux/sched/signal.h>
2420
2521 #include <linux/iio/iio.h>
22
+#include <linux/iio/iio-opaque.h>
2623 #include "iio_core.h"
24
+#include "iio_core_trigger.h"
2725 #include <linux/iio/sysfs.h>
2826 #include <linux/iio/buffer.h>
2927 #include <linux/iio/buffer_impl.h>
....@@ -91,7 +89,7 @@
9189 }
9290
9391 /**
94
- * iio_buffer_read_first_n_outer() - chrdev read for buffer access
92
+ * iio_buffer_read_outer() - chrdev read for buffer access
9593 * @filp: File structure pointer for the char device
9694 * @buf: Destination buffer for iio buffer read
9795 * @n: First n bytes to read
....@@ -103,8 +101,8 @@
103101 * Return: negative values corresponding to error codes or ret != 0
104102 * for ending the reading activity
105103 **/
106
-ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
107
- size_t n, loff_t *f_ps)
104
+ssize_t iio_buffer_read_outer(struct file *filp, char __user *buf,
105
+ size_t n, loff_t *f_ps)
108106 {
109107 struct iio_dev *indio_dev = filp->private_data;
110108 struct iio_buffer *rb = indio_dev->buffer;
....@@ -116,7 +114,7 @@
116114 if (!indio_dev->info)
117115 return -ENODEV;
118116
119
- if (!rb || !rb->access->read_first_n)
117
+ if (!rb || !rb->access->read)
120118 return -EINVAL;
121119
122120 datum_size = rb->bytes_per_datum;
....@@ -151,7 +149,7 @@
151149 continue;
152150 }
153151
154
- ret = rb->access->read_first_n(rb, n, buf);
152
+ ret = rb->access->read(rb, n, buf);
155153 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
156154 ret = -EAGAIN;
157155 } while (ret == 0);
....@@ -193,10 +191,12 @@
193191 */
194192 void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
195193 {
196
- if (!indio_dev->buffer)
194
+ struct iio_buffer *buffer = indio_dev->buffer;
195
+
196
+ if (!buffer)
197197 return;
198198
199
- wake_up(&indio_dev->buffer->pollq);
199
+ wake_up(&buffer->pollq);
200200 }
201201
202202 void iio_buffer_init(struct iio_buffer *buffer)
....@@ -266,10 +266,11 @@
266266 {
267267 int ret;
268268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
269
+ struct iio_buffer *buffer = indio_dev->buffer;
269270
270271 /* Ensure ret is 0 or 1. */
271272 ret = !!test_bit(to_iio_dev_attr(attr)->address,
272
- indio_dev->buffer->scan_mask);
273
+ buffer->scan_mask);
273274
274275 return sprintf(buf, "%d\n", ret);
275276 }
....@@ -320,8 +321,7 @@
320321 const unsigned long *mask;
321322 unsigned long *trialmask;
322323
323
- trialmask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
324
- sizeof(*trialmask), GFP_KERNEL);
324
+ trialmask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
325325 if (trialmask == NULL)
326326 return -ENOMEM;
327327 if (!indio_dev->masklength) {
....@@ -343,12 +343,12 @@
343343 }
344344 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
345345
346
- kfree(trialmask);
346
+ bitmap_free(trialmask);
347347
348348 return 0;
349349
350350 err_invalid_mask:
351
- kfree(trialmask);
351
+ bitmap_free(trialmask);
352352 return -EINVAL;
353353 }
354354
....@@ -386,7 +386,7 @@
386386 if (ret < 0)
387387 return ret;
388388 mutex_lock(&indio_dev->mlock);
389
- if (iio_buffer_is_active(indio_dev->buffer)) {
389
+ if (iio_buffer_is_active(buffer)) {
390390 ret = -EBUSY;
391391 goto error_ret;
392392 }
....@@ -415,7 +415,9 @@
415415 char *buf)
416416 {
417417 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
418
- return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
418
+ struct iio_buffer *buffer = indio_dev->buffer;
419
+
420
+ return sprintf(buf, "%d\n", buffer->scan_timestamp);
419421 }
420422
421423 static ssize_t iio_scan_el_ts_store(struct device *dev,
....@@ -425,6 +427,7 @@
425427 {
426428 int ret;
427429 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
430
+ struct iio_buffer *buffer = indio_dev->buffer;
428431 bool state;
429432
430433 ret = strtobool(buf, &state);
....@@ -432,11 +435,11 @@
432435 return ret;
433436
434437 mutex_lock(&indio_dev->mlock);
435
- if (iio_buffer_is_active(indio_dev->buffer)) {
438
+ if (iio_buffer_is_active(buffer)) {
436439 ret = -EBUSY;
437440 goto error_ret;
438441 }
439
- indio_dev->buffer->scan_timestamp = state;
442
+ buffer->scan_timestamp = state;
440443 error_ret:
441444 mutex_unlock(&indio_dev->mlock);
442445
....@@ -444,10 +447,10 @@
444447 }
445448
446449 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
450
+ struct iio_buffer *buffer,
447451 const struct iio_chan_spec *chan)
448452 {
449453 int ret, attrcount = 0;
450
- struct iio_buffer *buffer = indio_dev->buffer;
451454
452455 ret = __iio_add_chan_devattr("index",
453456 chan,
....@@ -523,7 +526,7 @@
523526 return len;
524527
525528 mutex_lock(&indio_dev->mlock);
526
- if (iio_buffer_is_active(indio_dev->buffer)) {
529
+ if (iio_buffer_is_active(buffer)) {
527530 ret = -EBUSY;
528531 } else {
529532 buffer->access->set_length(buffer, val);
....@@ -544,7 +547,9 @@
544547 char *buf)
545548 {
546549 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
547
- return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
550
+ struct iio_buffer *buffer = indio_dev->buffer;
551
+
552
+ return sprintf(buf, "%d\n", iio_buffer_is_active(buffer));
548553 }
549554
550555 static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
....@@ -595,8 +600,10 @@
595600 static void iio_buffer_activate(struct iio_dev *indio_dev,
596601 struct iio_buffer *buffer)
597602 {
603
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
604
+
598605 iio_buffer_get(buffer);
599
- list_add(&buffer->buffer_list, &indio_dev->buffer_list);
606
+ list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list);
600607 }
601608
602609 static void iio_buffer_deactivate(struct iio_buffer *buffer)
....@@ -608,10 +615,11 @@
608615
609616 static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
610617 {
618
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
611619 struct iio_buffer *buffer, *_buffer;
612620
613621 list_for_each_entry_safe(buffer, _buffer,
614
- &indio_dev->buffer_list, buffer_list)
622
+ &iio_dev_opaque->buffer_list, buffer_list)
615623 iio_buffer_deactivate(buffer);
616624 }
617625
....@@ -669,7 +677,7 @@
669677 {
670678 /* If the mask is dynamically allocated free it, otherwise do nothing */
671679 if (!indio_dev->available_scan_masks)
672
- kfree(mask);
680
+ bitmap_free(mask);
673681 }
674682
675683 struct iio_device_config {
....@@ -684,12 +692,20 @@
684692 struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
685693 struct iio_device_config *config)
686694 {
695
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
687696 unsigned long *compound_mask;
688697 const unsigned long *scan_mask;
689698 bool strict_scanmask = false;
690699 struct iio_buffer *buffer;
691700 bool scan_timestamp;
692701 unsigned int modes;
702
+
703
+ if (insert_buffer &&
704
+ bitmap_empty(insert_buffer->scan_mask, indio_dev->masklength)) {
705
+ dev_dbg(&indio_dev->dev,
706
+ "At least one scan element must be enabled first\n");
707
+ return -EINVAL;
708
+ }
693709
694710 memset(config, 0, sizeof(*config));
695711 config->watermark = ~0;
....@@ -699,12 +715,12 @@
699715 * to verify.
700716 */
701717 if (remove_buffer && !insert_buffer &&
702
- list_is_singular(&indio_dev->buffer_list))
718
+ list_is_singular(&iio_dev_opaque->buffer_list))
703719 return 0;
704720
705721 modes = indio_dev->modes;
706722
707
- list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
723
+ list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
708724 if (buffer == remove_buffer)
709725 continue;
710726 modes &= buffer->access->modes;
....@@ -725,7 +741,7 @@
725741 * Keep things simple for now and only allow a single buffer to
726742 * be connected in hardware mode.
727743 */
728
- if (insert_buffer && !list_empty(&indio_dev->buffer_list))
744
+ if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list))
729745 return -EINVAL;
730746 config->mode = INDIO_BUFFER_HARDWARE;
731747 strict_scanmask = true;
....@@ -739,14 +755,13 @@
739755 }
740756
741757 /* What scan mask do we actually have? */
742
- compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
743
- sizeof(long), GFP_KERNEL);
758
+ compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
744759 if (compound_mask == NULL)
745760 return -ENOMEM;
746761
747762 scan_timestamp = false;
748763
749
- list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
764
+ list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
750765 if (buffer == remove_buffer)
751766 continue;
752767 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
....@@ -765,7 +780,7 @@
765780 indio_dev->masklength,
766781 compound_mask,
767782 strict_scanmask);
768
- kfree(compound_mask);
783
+ bitmap_free(compound_mask);
769784 if (scan_mask == NULL)
770785 return -EINVAL;
771786 } else {
....@@ -892,10 +907,11 @@
892907
893908 static int iio_update_demux(struct iio_dev *indio_dev)
894909 {
910
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
895911 struct iio_buffer *buffer;
896912 int ret;
897913
898
- list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
914
+ list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
899915 ret = iio_buffer_update_demux(indio_dev, buffer);
900916 if (ret < 0)
901917 goto error_clear_mux_table;
....@@ -903,7 +919,7 @@
903919 return 0;
904920
905921 error_clear_mux_table:
906
- list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
922
+ list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list)
907923 iio_buffer_demux_free(buffer);
908924
909925 return ret;
....@@ -912,12 +928,14 @@
912928 static int iio_enable_buffers(struct iio_dev *indio_dev,
913929 struct iio_device_config *config)
914930 {
931
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
915932 struct iio_buffer *buffer;
916933 int ret;
917934
918935 indio_dev->active_scan_mask = config->scan_mask;
919936 indio_dev->scan_timestamp = config->scan_timestamp;
920937 indio_dev->scan_bytes = config->scan_bytes;
938
+ indio_dev->currentmode = config->mode;
921939
922940 iio_update_demux(indio_dev);
923941
....@@ -947,34 +965,44 @@
947965 indio_dev->info->hwfifo_set_watermark(indio_dev,
948966 config->watermark);
949967
950
- list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
968
+ list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
951969 ret = iio_buffer_enable(buffer, indio_dev);
952970 if (ret)
953971 goto err_disable_buffers;
954972 }
955973
956
- indio_dev->currentmode = config->mode;
974
+ if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
975
+ ret = iio_trigger_attach_poll_func(indio_dev->trig,
976
+ indio_dev->pollfunc);
977
+ if (ret)
978
+ goto err_disable_buffers;
979
+ }
957980
958981 if (indio_dev->setup_ops->postenable) {
959982 ret = indio_dev->setup_ops->postenable(indio_dev);
960983 if (ret) {
961984 dev_dbg(&indio_dev->dev,
962985 "Buffer not started: postenable failed (%d)\n", ret);
963
- goto err_disable_buffers;
986
+ goto err_detach_pollfunc;
964987 }
965988 }
966989
967990 return 0;
968991
992
+err_detach_pollfunc:
993
+ if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
994
+ iio_trigger_detach_poll_func(indio_dev->trig,
995
+ indio_dev->pollfunc);
996
+ }
969997 err_disable_buffers:
970
- list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list,
998
+ list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list,
971999 buffer_list)
9721000 iio_buffer_disable(buffer, indio_dev);
9731001 err_run_postdisable:
974
- indio_dev->currentmode = INDIO_DIRECT_MODE;
9751002 if (indio_dev->setup_ops->postdisable)
9761003 indio_dev->setup_ops->postdisable(indio_dev);
9771004 err_undo_config:
1005
+ indio_dev->currentmode = INDIO_DIRECT_MODE;
9781006 indio_dev->active_scan_mask = NULL;
9791007
9801008 return ret;
....@@ -982,12 +1010,13 @@
9821010
9831011 static int iio_disable_buffers(struct iio_dev *indio_dev)
9841012 {
1013
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
9851014 struct iio_buffer *buffer;
9861015 int ret = 0;
9871016 int ret2;
9881017
9891018 /* Wind down existing buffers - iff there are any */
990
- if (list_empty(&indio_dev->buffer_list))
1019
+ if (list_empty(&iio_dev_opaque->buffer_list))
9911020 return 0;
9921021
9931022 /*
....@@ -1003,13 +1032,16 @@
10031032 ret = ret2;
10041033 }
10051034
1006
- list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1035
+ if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
1036
+ iio_trigger_detach_poll_func(indio_dev->trig,
1037
+ indio_dev->pollfunc);
1038
+ }
1039
+
1040
+ list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
10071041 ret2 = iio_buffer_disable(buffer, indio_dev);
10081042 if (ret2 && !ret)
10091043 ret = ret2;
10101044 }
1011
-
1012
- indio_dev->currentmode = INDIO_DIRECT_MODE;
10131045
10141046 if (indio_dev->setup_ops->postdisable) {
10151047 ret2 = indio_dev->setup_ops->postdisable(indio_dev);
....@@ -1019,6 +1051,7 @@
10191051
10201052 iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
10211053 indio_dev->active_scan_mask = NULL;
1054
+ indio_dev->currentmode = INDIO_DIRECT_MODE;
10221055
10231056 return ret;
10241057 }
....@@ -1027,6 +1060,7 @@
10271060 struct iio_buffer *insert_buffer,
10281061 struct iio_buffer *remove_buffer)
10291062 {
1063
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
10301064 struct iio_device_config new_config;
10311065 int ret;
10321066
....@@ -1051,7 +1085,7 @@
10511085 iio_buffer_activate(indio_dev, insert_buffer);
10521086
10531087 /* If no buffers in list, we are done */
1054
- if (list_empty(&indio_dev->buffer_list))
1088
+ if (list_empty(&iio_dev_opaque->buffer_list))
10551089 return 0;
10561090
10571091 ret = iio_enable_buffers(indio_dev, &new_config);
....@@ -1128,6 +1162,7 @@
11281162 int ret;
11291163 bool requested_state;
11301164 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1165
+ struct iio_buffer *buffer = indio_dev->buffer;
11311166 bool inlist;
11321167
11331168 ret = strtobool(buf, &requested_state);
....@@ -1137,17 +1172,15 @@
11371172 mutex_lock(&indio_dev->mlock);
11381173
11391174 /* Find out if it is in the list */
1140
- inlist = iio_buffer_is_active(indio_dev->buffer);
1175
+ inlist = iio_buffer_is_active(buffer);
11411176 /* Already in desired state */
11421177 if (inlist == requested_state)
11431178 goto done;
11441179
11451180 if (requested_state)
1146
- ret = __iio_update_buffers(indio_dev,
1147
- indio_dev->buffer, NULL);
1181
+ ret = __iio_update_buffers(indio_dev, buffer, NULL);
11481182 else
1149
- ret = __iio_update_buffers(indio_dev,
1150
- NULL, indio_dev->buffer);
1183
+ ret = __iio_update_buffers(indio_dev, NULL, buffer);
11511184
11521185 done:
11531186 mutex_unlock(&indio_dev->mlock);
....@@ -1189,7 +1222,7 @@
11891222 goto out;
11901223 }
11911224
1192
- if (iio_buffer_is_active(indio_dev->buffer)) {
1225
+ if (iio_buffer_is_active(buffer)) {
11931226 ret = -EBUSY;
11941227 goto out;
11951228 }
....@@ -1206,11 +1239,9 @@
12061239 char *buf)
12071240 {
12081241 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1209
- size_t bytes;
1242
+ struct iio_buffer *buffer = indio_dev->buffer;
12101243
1211
- bytes = iio_buffer_data_available(indio_dev->buffer);
1212
-
1213
- return sprintf(buf, "%zu\n", bytes);
1244
+ return sprintf(buf, "%zu\n", iio_buffer_data_available(buffer));
12141245 }
12151246
12161247 static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
....@@ -1233,25 +1264,13 @@
12331264 &dev_attr_data_available.attr,
12341265 };
12351266
1236
-int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1267
+static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
1268
+ struct iio_dev *indio_dev)
12371269 {
12381270 struct iio_dev_attr *p;
12391271 struct attribute **attr;
1240
- struct iio_buffer *buffer = indio_dev->buffer;
1241
- int ret, i, attrn, attrcount, attrcount_orig = 0;
1272
+ int ret, i, attrn, attrcount;
12421273 const struct iio_chan_spec *channels;
1243
-
1244
- channels = indio_dev->channels;
1245
- if (channels) {
1246
- int ml = indio_dev->masklength;
1247
-
1248
- for (i = 0; i < indio_dev->num_channels; i++)
1249
- ml = max(ml, channels[i].scan_index + 1);
1250
- indio_dev->masklength = ml;
1251
- }
1252
-
1253
- if (!buffer)
1254
- return 0;
12551274
12561275 attrcount = 0;
12571276 if (buffer->attrs) {
....@@ -1282,12 +1301,7 @@
12821301
12831302 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
12841303
1285
- if (buffer->scan_el_attrs != NULL) {
1286
- attr = buffer->scan_el_attrs->attrs;
1287
- while (*attr++ != NULL)
1288
- attrcount_orig++;
1289
- }
1290
- attrcount = attrcount_orig;
1304
+ attrcount = 0;
12911305 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
12921306 channels = indio_dev->channels;
12931307 if (channels) {
....@@ -1296,7 +1310,7 @@
12961310 if (channels[i].scan_index < 0)
12971311 continue;
12981312
1299
- ret = iio_buffer_add_channel_sysfs(indio_dev,
1313
+ ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
13001314 &channels[i]);
13011315 if (ret < 0)
13021316 goto error_cleanup_dynamic;
....@@ -1306,9 +1320,8 @@
13061320 channels[i].scan_index;
13071321 }
13081322 if (indio_dev->masklength && buffer->scan_mask == NULL) {
1309
- buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
1310
- sizeof(*buffer->scan_mask),
1311
- GFP_KERNEL);
1323
+ buffer->scan_mask = bitmap_zalloc(indio_dev->masklength,
1324
+ GFP_KERNEL);
13121325 if (buffer->scan_mask == NULL) {
13131326 ret = -ENOMEM;
13141327 goto error_cleanup_dynamic;
....@@ -1325,10 +1338,7 @@
13251338 ret = -ENOMEM;
13261339 goto error_free_scan_mask;
13271340 }
1328
- if (buffer->scan_el_attrs)
1329
- memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
1330
- sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
1331
- attrn = attrcount_orig;
1341
+ attrn = 0;
13321342
13331343 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
13341344 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
....@@ -1337,23 +1347,51 @@
13371347 return 0;
13381348
13391349 error_free_scan_mask:
1340
- kfree(buffer->scan_mask);
1350
+ bitmap_free(buffer->scan_mask);
13411351 error_cleanup_dynamic:
13421352 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
1343
- kfree(indio_dev->buffer->buffer_group.attrs);
1353
+ kfree(buffer->buffer_group.attrs);
13441354
13451355 return ret;
13461356 }
13471357
1358
+int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1359
+{
1360
+ struct iio_buffer *buffer = indio_dev->buffer;
1361
+ const struct iio_chan_spec *channels;
1362
+ int i;
1363
+
1364
+ channels = indio_dev->channels;
1365
+ if (channels) {
1366
+ int ml = indio_dev->masklength;
1367
+
1368
+ for (i = 0; i < indio_dev->num_channels; i++)
1369
+ ml = max(ml, channels[i].scan_index + 1);
1370
+ indio_dev->masklength = ml;
1371
+ }
1372
+
1373
+ if (!buffer)
1374
+ return 0;
1375
+
1376
+ return __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev);
1377
+}
1378
+
1379
+static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer)
1380
+{
1381
+ bitmap_free(buffer->scan_mask);
1382
+ kfree(buffer->buffer_group.attrs);
1383
+ kfree(buffer->scan_el_group.attrs);
1384
+ iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
1385
+}
1386
+
13481387 void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
13491388 {
1350
- if (!indio_dev->buffer)
1389
+ struct iio_buffer *buffer = indio_dev->buffer;
1390
+
1391
+ if (!buffer)
13511392 return;
13521393
1353
- kfree(indio_dev->buffer->scan_mask);
1354
- kfree(indio_dev->buffer->buffer_group.attrs);
1355
- kfree(indio_dev->buffer->scan_el_group.attrs);
1356
- iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
1394
+ __iio_buffer_free_sysfs_and_mask(buffer);
13571395 }
13581396
13591397 /**
....@@ -1410,10 +1448,11 @@
14101448 */
14111449 int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
14121450 {
1451
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
14131452 int ret;
14141453 struct iio_buffer *buf;
14151454
1416
- list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
1455
+ list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) {
14171456 ret = iio_push_to_buffer(buf, data);
14181457 if (ret < 0)
14191458 return ret;