hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/iio/industrialio-core.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* The industrial I/O core
23 *
34 * 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.
85 *
96 * Based on elements of hwmon and input subsystems.
107 */
....@@ -19,6 +16,7 @@
1916 #include <linux/device.h>
2017 #include <linux/fs.h>
2118 #include <linux/poll.h>
19
+#include <linux/property.h>
2220 #include <linux/sched.h>
2321 #include <linux/wait.h>
2422 #include <linux/cdev.h>
....@@ -27,6 +25,7 @@
2725 #include <linux/debugfs.h>
2826 #include <linux/mutex.h>
2927 #include <linux/iio/iio.h>
28
+#include <linux/iio/iio-opaque.h>
3029 #include "iio_core.h"
3130 #include "iio_core_trigger.h"
3231 #include <linux/iio/sysfs.h>
....@@ -87,6 +86,17 @@
8786 [IIO_GRAVITY] = "gravity",
8887 [IIO_POSITIONRELATIVE] = "positionrelative",
8988 [IIO_PHASE] = "phase",
89
+ [IIO_MASSCONCENTRATION] = "massconcentration",
90
+#ifdef CONFIG_NO_GKI
91
+ [IIO_SIGN_MOTION] = "signmotion",
92
+ [IIO_STEP_DETECTOR] = "stepdetector",
93
+ [IIO_STEP_COUNTER] = "stepcounter",
94
+ [IIO_TILT] = "tilt",
95
+ [IIO_TAP] = "tap",
96
+ [IIO_TAP_TAP] = "taptap",
97
+ [IIO_WRIST_TILT_GESTURE] = "wristtiltgesture",
98
+ [IIO_GESTURE] = "gesture",
99
+#endif
90100 };
91101
92102 static const char * const iio_modifier_names[] = {
....@@ -127,6 +137,13 @@
127137 [IIO_MOD_Q] = "q",
128138 [IIO_MOD_CO2] = "co2",
129139 [IIO_MOD_VOC] = "voc",
140
+ [IIO_MOD_PM1] = "pm1",
141
+ [IIO_MOD_PM2P5] = "pm2p5",
142
+ [IIO_MOD_PM4] = "pm4",
143
+ [IIO_MOD_PM10] = "pm10",
144
+ [IIO_MOD_ETHANOL] = "ethanol",
145
+ [IIO_MOD_H2] = "h2",
146
+ [IIO_MOD_O2] = "o2",
130147 };
131148
132149 /* relies on pairs of these shared then separate */
....@@ -158,7 +175,22 @@
158175 [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
159176 [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
160177 [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
178
+ [IIO_CHAN_INFO_THERMOCOUPLE_TYPE] = "thermocouple_type",
179
+ [IIO_CHAN_INFO_CALIBAMBIENT] = "calibambient",
161180 };
181
+
182
+#if defined(CONFIG_DEBUG_FS)
183
+/*
184
+ * There's also a CONFIG_DEBUG_FS guard in include/linux/iio/iio.h for
185
+ * iio_get_debugfs_dentry() to make it inline if CONFIG_DEBUG_FS is undefined
186
+ */
187
+struct dentry *iio_get_debugfs_dentry(struct iio_dev *indio_dev)
188
+{
189
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
190
+ return iio_dev_opaque->debugfs_dentry;
191
+}
192
+EXPORT_SYMBOL_GPL(iio_get_debugfs_dentry);
193
+#endif
162194
163195 /**
164196 * iio_find_channel_from_si() - get channel from its scan index
....@@ -185,10 +217,16 @@
185217 }
186218 EXPORT_SYMBOL(iio_read_const_attr);
187219
188
-static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
220
+/**
221
+ * iio_device_set_clock() - Set current timestamping clock for the device
222
+ * @indio_dev: IIO device structure containing the device
223
+ * @clock_id: timestamping clock posix identifier to set.
224
+ */
225
+int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
189226 {
190227 int ret;
191
- const struct iio_event_interface *ev_int = indio_dev->event_interface;
228
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
229
+ const struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
192230
193231 ret = mutex_lock_interruptible(&indio_dev->mlock);
194232 if (ret)
....@@ -203,6 +241,7 @@
203241
204242 return 0;
205243 }
244
+EXPORT_SYMBOL(iio_device_set_clock);
206245
207246 /**
208247 * iio_get_time_ns() - utility function to get a time stamp for events etc
....@@ -225,9 +264,9 @@
225264 ktime_get_coarse_ts64(&tp);
226265 return timespec64_to_ns(&tp);
227266 case CLOCK_BOOTTIME:
228
- return ktime_get_boot_ns();
267
+ return ktime_get_boottime_ns();
229268 case CLOCK_TAI:
230
- return ktime_get_tai_ns();
269
+ return ktime_get_clocktai_ns();
231270 default:
232271 BUG();
233272 }
....@@ -297,28 +336,37 @@
297336 size_t count, loff_t *ppos)
298337 {
299338 struct iio_dev *indio_dev = file->private_data;
300
- char buf[20];
339
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
301340 unsigned val = 0;
302
- ssize_t len;
303341 int ret;
304342
343
+ if (*ppos > 0)
344
+ return simple_read_from_buffer(userbuf, count, ppos,
345
+ iio_dev_opaque->read_buf,
346
+ iio_dev_opaque->read_buf_len);
347
+
305348 ret = indio_dev->info->debugfs_reg_access(indio_dev,
306
- indio_dev->cached_reg_addr,
349
+ iio_dev_opaque->cached_reg_addr,
307350 0, &val);
308351 if (ret) {
309352 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
310353 return ret;
311354 }
312355
313
- len = snprintf(buf, sizeof(buf), "0x%X\n", val);
356
+ iio_dev_opaque->read_buf_len = snprintf(iio_dev_opaque->read_buf,
357
+ sizeof(iio_dev_opaque->read_buf),
358
+ "0x%X\n", val);
314359
315
- return simple_read_from_buffer(userbuf, count, ppos, buf, len);
360
+ return simple_read_from_buffer(userbuf, count, ppos,
361
+ iio_dev_opaque->read_buf,
362
+ iio_dev_opaque->read_buf_len);
316363 }
317364
318365 static ssize_t iio_debugfs_write_reg(struct file *file,
319366 const char __user *userbuf, size_t count, loff_t *ppos)
320367 {
321368 struct iio_dev *indio_dev = file->private_data;
369
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
322370 unsigned reg, val;
323371 char buf[80];
324372 int ret;
....@@ -333,10 +381,10 @@
333381
334382 switch (ret) {
335383 case 1:
336
- indio_dev->cached_reg_addr = reg;
384
+ iio_dev_opaque->cached_reg_addr = reg;
337385 break;
338386 case 2:
339
- indio_dev->cached_reg_addr = reg;
387
+ iio_dev_opaque->cached_reg_addr = reg;
340388 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
341389 val, NULL);
342390 if (ret) {
....@@ -360,42 +408,33 @@
360408
361409 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
362410 {
363
- debugfs_remove_recursive(indio_dev->debugfs_dentry);
411
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
412
+ debugfs_remove_recursive(iio_dev_opaque->debugfs_dentry);
364413 }
365414
366
-static int iio_device_register_debugfs(struct iio_dev *indio_dev)
415
+static void iio_device_register_debugfs(struct iio_dev *indio_dev)
367416 {
368
- struct dentry *d;
417
+ struct iio_dev_opaque *iio_dev_opaque;
369418
370419 if (indio_dev->info->debugfs_reg_access == NULL)
371
- return 0;
420
+ return;
372421
373422 if (!iio_debugfs_dentry)
374
- return 0;
423
+ return;
375424
376
- indio_dev->debugfs_dentry =
425
+ iio_dev_opaque = to_iio_dev_opaque(indio_dev);
426
+
427
+ iio_dev_opaque->debugfs_dentry =
377428 debugfs_create_dir(dev_name(&indio_dev->dev),
378429 iio_debugfs_dentry);
379
- if (indio_dev->debugfs_dentry == NULL) {
380
- dev_warn(indio_dev->dev.parent,
381
- "Failed to create debugfs directory\n");
382
- return -EFAULT;
383
- }
384430
385
- d = debugfs_create_file("direct_reg_access", 0644,
386
- indio_dev->debugfs_dentry,
387
- indio_dev, &iio_debugfs_reg_fops);
388
- if (!d) {
389
- iio_device_unregister_debugfs(indio_dev);
390
- return -ENOMEM;
391
- }
392
-
393
- return 0;
431
+ debugfs_create_file("direct_reg_access", 0644,
432
+ iio_dev_opaque->debugfs_dentry, indio_dev,
433
+ &iio_debugfs_reg_fops);
394434 }
395435 #else
396
-static int iio_device_register_debugfs(struct iio_dev *indio_dev)
436
+static void iio_device_register_debugfs(struct iio_dev *indio_dev)
397437 {
398
- return 0;
399438 }
400439
401440 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
....@@ -525,8 +564,8 @@
525564 EXPORT_SYMBOL_GPL(iio_show_mount_matrix);
526565
527566 /**
528
- * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from
529
- * device-tree "mount-matrix" property
567
+ * iio_read_mount_matrix() - retrieve iio device mounting matrix from
568
+ * device "mount-matrix" property
530569 * @dev: device the mounting matrix property is assigned to
531570 * @propname: device specific mounting matrix property name
532571 * @matrix: where to store retrieved matrix
....@@ -536,40 +575,29 @@
536575 *
537576 * Return: 0 if success, or a negative error code on failure.
538577 */
539
-#ifdef CONFIG_OF
540
-int of_iio_read_mount_matrix(const struct device *dev,
541
- const char *propname,
542
- struct iio_mount_matrix *matrix)
578
+int iio_read_mount_matrix(struct device *dev, const char *propname,
579
+ struct iio_mount_matrix *matrix)
543580 {
544
- if (dev->of_node) {
545
- int err = of_property_read_string_array(dev->of_node,
546
- propname, matrix->rotation,
547
- ARRAY_SIZE(iio_mount_idmatrix.rotation));
581
+ size_t len = ARRAY_SIZE(iio_mount_idmatrix.rotation);
582
+ int err;
548583
549
- if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation))
550
- return 0;
584
+ err = device_property_read_string_array(dev, propname,
585
+ matrix->rotation, len);
586
+ if (err == len)
587
+ return 0;
551588
552
- if (err >= 0)
553
- /* Invalid number of matrix entries. */
554
- return -EINVAL;
589
+ if (err >= 0)
590
+ /* Invalid number of matrix entries. */
591
+ return -EINVAL;
555592
556
- if (err != -EINVAL)
557
- /* Invalid matrix declaration format. */
558
- return err;
559
- }
593
+ if (err != -EINVAL)
594
+ /* Invalid matrix declaration format. */
595
+ return err;
560596
561597 /* Matrix was not declared at all: fallback to identity. */
562598 return iio_setup_mount_idmatrix(dev, matrix);
563599 }
564
-#else
565
-int of_iio_read_mount_matrix(const struct device *dev,
566
- const char *propname,
567
- struct iio_mount_matrix *matrix)
568
-{
569
- return iio_setup_mount_idmatrix(dev, matrix);
570
-}
571
-#endif
572
-EXPORT_SYMBOL(of_iio_read_mount_matrix);
600
+EXPORT_SYMBOL(iio_read_mount_matrix);
573601
574602 static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
575603 int size, const int *vals)
....@@ -580,44 +608,46 @@
580608
581609 switch (type) {
582610 case IIO_VAL_INT:
583
- return snprintf(buf, len, "%d", vals[0]);
611
+ return scnprintf(buf, len, "%d", vals[0]);
584612 case IIO_VAL_INT_PLUS_MICRO_DB:
585613 scale_db = true;
586
- /* fall through */
614
+ fallthrough;
587615 case IIO_VAL_INT_PLUS_MICRO:
588616 if (vals[1] < 0)
589
- return snprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
617
+ return scnprintf(buf, len, "-%d.%06u%s", abs(vals[0]),
590618 -vals[1], scale_db ? " dB" : "");
591619 else
592
- return snprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
620
+ return scnprintf(buf, len, "%d.%06u%s", vals[0], vals[1],
593621 scale_db ? " dB" : "");
594622 case IIO_VAL_INT_PLUS_NANO:
595623 if (vals[1] < 0)
596
- return snprintf(buf, len, "-%d.%09u", abs(vals[0]),
624
+ return scnprintf(buf, len, "-%d.%09u", abs(vals[0]),
597625 -vals[1]);
598626 else
599
- return snprintf(buf, len, "%d.%09u", vals[0], vals[1]);
627
+ return scnprintf(buf, len, "%d.%09u", vals[0], vals[1]);
600628 case IIO_VAL_FRACTIONAL:
601629 tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
602630 tmp1 = vals[1];
603631 tmp0 = (int)div_s64_rem(tmp, 1000000000, &tmp1);
604
- return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
632
+ return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
605633 case IIO_VAL_FRACTIONAL_LOG2:
606634 tmp = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
607635 tmp0 = (int)div_s64_rem(tmp, 1000000000LL, &tmp1);
608
- return snprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
636
+ return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
609637 case IIO_VAL_INT_MULTIPLE:
610638 {
611639 int i;
612640 int l = 0;
613641
614642 for (i = 0; i < size; ++i) {
615
- l += snprintf(&buf[l], len - l, "%d ", vals[i]);
643
+ l += scnprintf(&buf[l], len - l, "%d ", vals[i]);
616644 if (l >= len)
617645 break;
618646 }
619647 return l;
620648 }
649
+ case IIO_VAL_CHAR:
650
+ return scnprintf(buf, len, "%c", (char)vals[0]);
621651 default:
622652 return 0;
623653 }
....@@ -688,10 +718,10 @@
688718 if (len >= PAGE_SIZE)
689719 return -EFBIG;
690720 if (i < length - 1)
691
- len += snprintf(buf + len, PAGE_SIZE - len,
721
+ len += scnprintf(buf + len, PAGE_SIZE - len,
692722 " ");
693723 else
694
- len += snprintf(buf + len, PAGE_SIZE - len,
724
+ len += scnprintf(buf + len, PAGE_SIZE - len,
695725 "\n");
696726 if (len >= PAGE_SIZE)
697727 return -EFBIG;
....@@ -704,10 +734,10 @@
704734 if (len >= PAGE_SIZE)
705735 return -EFBIG;
706736 if (i < length / 2 - 1)
707
- len += snprintf(buf + len, PAGE_SIZE - len,
737
+ len += scnprintf(buf + len, PAGE_SIZE - len,
708738 " ");
709739 else
710
- len += snprintf(buf + len, PAGE_SIZE - len,
740
+ len += scnprintf(buf + len, PAGE_SIZE - len,
711741 "\n");
712742 if (len >= PAGE_SIZE)
713743 return -EFBIG;
....@@ -731,10 +761,10 @@
731761 if (len >= PAGE_SIZE)
732762 return -EFBIG;
733763 if (i < 2)
734
- len += snprintf(buf + len, PAGE_SIZE - len,
764
+ len += scnprintf(buf + len, PAGE_SIZE - len,
735765 " ");
736766 else
737
- len += snprintf(buf + len, PAGE_SIZE - len,
767
+ len += scnprintf(buf + len, PAGE_SIZE - len,
738768 "]\n");
739769 if (len >= PAGE_SIZE)
740770 return -EFBIG;
....@@ -747,10 +777,10 @@
747777 if (len >= PAGE_SIZE)
748778 return -EFBIG;
749779 if (i < 2)
750
- len += snprintf(buf + len, PAGE_SIZE - len,
780
+ len += scnprintf(buf + len, PAGE_SIZE - len,
751781 " ");
752782 else
753
- len += snprintf(buf + len, PAGE_SIZE - len,
783
+ len += scnprintf(buf + len, PAGE_SIZE - len,
754784 "]\n");
755785 if (len >= PAGE_SIZE)
756786 return -EFBIG;
....@@ -788,17 +818,18 @@
788818 }
789819
790820 /**
791
- * iio_str_to_fixpoint() - Parse a fixed-point number from a string
821
+ * __iio_str_to_fixpoint() - Parse a fixed-point number from a string
792822 * @str: The string to parse
793823 * @fract_mult: Multiplier for the first decimal place, should be a power of 10
794824 * @integer: The integer part of the number
795825 * @fract: The fractional part of the number
826
+ * @scale_db: True if this should parse as dB
796827 *
797828 * Returns 0 on success, or a negative error code if the string could not be
798829 * parsed.
799830 */
800
-int iio_str_to_fixpoint(const char *str, int fract_mult,
801
- int *integer, int *fract)
831
+static int __iio_str_to_fixpoint(const char *str, int fract_mult,
832
+ int *integer, int *fract, bool scale_db)
802833 {
803834 int i = 0, f = 0;
804835 bool integer_part = true, negative = false;
....@@ -829,6 +860,14 @@
829860 break;
830861 else
831862 return -EINVAL;
863
+ } else if (!strncmp(str, " dB", sizeof(" dB") - 1) && scale_db) {
864
+ /* Ignore the dB suffix */
865
+ str += sizeof(" dB") - 1;
866
+ continue;
867
+ } else if (!strncmp(str, "dB", sizeof("dB") - 1) && scale_db) {
868
+ /* Ignore the dB suffix */
869
+ str += sizeof("dB") - 1;
870
+ continue;
832871 } else if (*str == '.' && integer_part) {
833872 integer_part = false;
834873 } else {
....@@ -849,6 +888,22 @@
849888
850889 return 0;
851890 }
891
+
892
+/**
893
+ * iio_str_to_fixpoint() - Parse a fixed-point number from a string
894
+ * @str: The string to parse
895
+ * @fract_mult: Multiplier for the first decimal place, should be a power of 10
896
+ * @integer: The integer part of the number
897
+ * @fract: The fractional part of the number
898
+ *
899
+ * Returns 0 on success, or a negative error code if the string could not be
900
+ * parsed.
901
+ */
902
+int iio_str_to_fixpoint(const char *str, int fract_mult,
903
+ int *integer, int *fract)
904
+{
905
+ return __iio_str_to_fixpoint(str, fract_mult, integer, fract, false);
906
+}
852907 EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
853908
854909 static ssize_t iio_write_channel_info(struct device *dev,
....@@ -859,7 +914,9 @@
859914 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
860915 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
861916 int ret, fract_mult = 100000;
862
- int integer, fract;
917
+ int integer, fract = 0;
918
+ bool is_char = false;
919
+ bool scale_db = false;
863920
864921 /* Assumes decimal - precision based on number of digits */
865922 if (!indio_dev->info->write_raw)
....@@ -871,19 +928,34 @@
871928 case IIO_VAL_INT:
872929 fract_mult = 0;
873930 break;
931
+ case IIO_VAL_INT_PLUS_MICRO_DB:
932
+ scale_db = true;
933
+ fallthrough;
874934 case IIO_VAL_INT_PLUS_MICRO:
875935 fract_mult = 100000;
876936 break;
877937 case IIO_VAL_INT_PLUS_NANO:
878938 fract_mult = 100000000;
879939 break;
940
+ case IIO_VAL_CHAR:
941
+ is_char = true;
942
+ break;
880943 default:
881944 return -EINVAL;
882945 }
883946
884
- ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
885
- if (ret)
886
- return ret;
947
+ if (is_char) {
948
+ char ch;
949
+
950
+ if (sscanf(buf, "%c", &ch) != 1)
951
+ return -EINVAL;
952
+ integer = ch;
953
+ } else {
954
+ ret = __iio_str_to_fixpoint(buf, fract_mult, &integer, &fract,
955
+ scale_db);
956
+ if (ret)
957
+ return ret;
958
+ }
887959
888960 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
889961 integer, fract, this_attr->address);
....@@ -1080,6 +1152,7 @@
10801152 enum iio_shared_by shared_by,
10811153 const long *infomask)
10821154 {
1155
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
10831156 int i, ret, attrcount = 0;
10841157
10851158 for_each_set_bit(i, infomask, sizeof(*infomask)*8) {
....@@ -1092,7 +1165,7 @@
10921165 i,
10931166 shared_by,
10941167 &indio_dev->dev,
1095
- &indio_dev->channel_attr_list);
1168
+ &iio_dev_opaque->channel_attr_list);
10961169 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
10971170 continue;
10981171 else if (ret < 0)
....@@ -1108,10 +1181,13 @@
11081181 enum iio_shared_by shared_by,
11091182 const long *infomask)
11101183 {
1184
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
11111185 int i, ret, attrcount = 0;
11121186 char *avail_postfix;
11131187
11141188 for_each_set_bit(i, infomask, sizeof(*infomask) * 8) {
1189
+ if (i >= ARRAY_SIZE(iio_chan_info_postfix))
1190
+ return -EINVAL;
11151191 avail_postfix = kasprintf(GFP_KERNEL,
11161192 "%s_available",
11171193 iio_chan_info_postfix[i]);
....@@ -1125,7 +1201,7 @@
11251201 i,
11261202 shared_by,
11271203 &indio_dev->dev,
1128
- &indio_dev->channel_attr_list);
1204
+ &iio_dev_opaque->channel_attr_list);
11291205 kfree(avail_postfix);
11301206 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
11311207 continue;
....@@ -1140,6 +1216,7 @@
11401216 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
11411217 struct iio_chan_spec const *chan)
11421218 {
1219
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
11431220 int ret, attrcount = 0;
11441221 const struct iio_chan_spec_ext_info *ext_info;
11451222
....@@ -1215,7 +1292,7 @@
12151292 i,
12161293 ext_info->shared,
12171294 &indio_dev->dev,
1218
- &indio_dev->channel_attr_list);
1295
+ &iio_dev_opaque->channel_attr_list);
12191296 i++;
12201297 if (ret == -EBUSY && ext_info->shared)
12211298 continue;
....@@ -1257,6 +1334,16 @@
12571334 }
12581335
12591336 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
1337
+
1338
+static ssize_t iio_show_dev_label(struct device *dev,
1339
+ struct device_attribute *attr,
1340
+ char *buf)
1341
+{
1342
+ struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1343
+ return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->label);
1344
+}
1345
+
1346
+static DEVICE_ATTR(label, S_IRUGO, iio_show_dev_label, NULL);
12601347
12611348 static ssize_t iio_show_timestamp_clock(struct device *dev,
12621349 struct device_attribute *attr,
....@@ -1340,6 +1427,7 @@
13401427
13411428 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
13421429 {
1430
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
13431431 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
13441432 struct iio_dev_attr *p;
13451433 struct attribute **attr, *clk = NULL;
....@@ -1369,58 +1457,66 @@
13691457 attrcount += ret;
13701458 }
13711459
1372
- if (indio_dev->event_interface)
1460
+ if (iio_dev_opaque->event_interface)
13731461 clk = &dev_attr_current_timestamp_clock.attr;
13741462
13751463 if (indio_dev->name)
13761464 attrcount++;
1465
+ if (indio_dev->label)
1466
+ attrcount++;
13771467 if (clk)
13781468 attrcount++;
13791469
1380
- indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
1381
- sizeof(indio_dev->chan_attr_group.attrs[0]),
1382
- GFP_KERNEL);
1383
- if (indio_dev->chan_attr_group.attrs == NULL) {
1470
+ iio_dev_opaque->chan_attr_group.attrs =
1471
+ kcalloc(attrcount + 1,
1472
+ sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
1473
+ GFP_KERNEL);
1474
+ if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
13841475 ret = -ENOMEM;
13851476 goto error_clear_attrs;
13861477 }
13871478 /* Copy across original attributes */
13881479 if (indio_dev->info->attrs)
1389
- memcpy(indio_dev->chan_attr_group.attrs,
1480
+ memcpy(iio_dev_opaque->chan_attr_group.attrs,
13901481 indio_dev->info->attrs->attrs,
1391
- sizeof(indio_dev->chan_attr_group.attrs[0])
1482
+ sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
13921483 *attrcount_orig);
13931484 attrn = attrcount_orig;
13941485 /* Add all elements from the list. */
1395
- list_for_each_entry(p, &indio_dev->channel_attr_list, l)
1396
- indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1486
+ list_for_each_entry(p, &iio_dev_opaque->channel_attr_list, l)
1487
+ iio_dev_opaque->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
13971488 if (indio_dev->name)
1398
- indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1489
+ iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1490
+ if (indio_dev->label)
1491
+ iio_dev_opaque->chan_attr_group.attrs[attrn++] = &dev_attr_label.attr;
13991492 if (clk)
1400
- indio_dev->chan_attr_group.attrs[attrn++] = clk;
1493
+ iio_dev_opaque->chan_attr_group.attrs[attrn++] = clk;
14011494
14021495 indio_dev->groups[indio_dev->groupcounter++] =
1403
- &indio_dev->chan_attr_group;
1496
+ &iio_dev_opaque->chan_attr_group;
14041497
14051498 return 0;
14061499
14071500 error_clear_attrs:
1408
- iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1501
+ iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
14091502
14101503 return ret;
14111504 }
14121505
14131506 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
14141507 {
1508
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
14151509
1416
- iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
1417
- kfree(indio_dev->chan_attr_group.attrs);
1418
- indio_dev->chan_attr_group.attrs = NULL;
1510
+ iio_free_chan_devattr_list(&iio_dev_opaque->channel_attr_list);
1511
+ kfree(iio_dev_opaque->chan_attr_group.attrs);
1512
+ iio_dev_opaque->chan_attr_group.attrs = NULL;
14191513 }
14201514
14211515 static void iio_dev_release(struct device *device)
14221516 {
14231517 struct iio_dev *indio_dev = dev_to_iio_dev(device);
1518
+ struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1519
+
14241520 if (indio_dev->modes & INDIO_ALL_TRIGGERED_MODES)
14251521 iio_device_unregister_trigger_consumer(indio_dev);
14261522 iio_device_unregister_eventset(indio_dev);
....@@ -1429,7 +1525,7 @@
14291525 iio_buffer_put(indio_dev->buffer);
14301526
14311527 ida_simple_remove(&iio_ida, indio_dev->id);
1432
- kfree(indio_dev);
1528
+ kfree(iio_dev_opaque);
14331529 }
14341530
14351531 struct device_type iio_device_type = {
....@@ -1439,43 +1535,48 @@
14391535
14401536 /**
14411537 * iio_device_alloc() - allocate an iio_dev from a driver
1538
+ * @parent: Parent device.
14421539 * @sizeof_priv: Space to allocate for private structure.
14431540 **/
1444
-struct iio_dev *iio_device_alloc(int sizeof_priv)
1541
+struct iio_dev *iio_device_alloc(struct device *parent, int sizeof_priv)
14451542 {
1543
+ struct iio_dev_opaque *iio_dev_opaque;
14461544 struct iio_dev *dev;
14471545 size_t alloc_size;
14481546
1449
- alloc_size = sizeof(struct iio_dev);
1547
+ alloc_size = sizeof(struct iio_dev_opaque);
14501548 if (sizeof_priv) {
14511549 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
14521550 alloc_size += sizeof_priv;
14531551 }
1454
- /* ensure 32-byte alignment of whole construct ? */
1455
- alloc_size += IIO_ALIGN - 1;
14561552
1457
- dev = kzalloc(alloc_size, GFP_KERNEL);
1553
+ iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL);
1554
+ if (!iio_dev_opaque)
1555
+ return NULL;
14581556
1459
- if (dev) {
1460
- dev->dev.groups = dev->groups;
1461
- dev->dev.type = &iio_device_type;
1462
- dev->dev.bus = &iio_bus_type;
1463
- device_initialize(&dev->dev);
1464
- dev_set_drvdata(&dev->dev, (void *)dev);
1465
- mutex_init(&dev->mlock);
1466
- mutex_init(&dev->info_exist_lock);
1467
- INIT_LIST_HEAD(&dev->channel_attr_list);
1557
+ dev = &iio_dev_opaque->indio_dev;
1558
+ dev->priv = (char *)iio_dev_opaque +
1559
+ ALIGN(sizeof(struct iio_dev_opaque), IIO_ALIGN);
14681560
1469
- dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1470
- if (dev->id < 0) {
1471
- /* cannot use a dev_err as the name isn't available */
1472
- pr_err("failed to get device id\n");
1473
- kfree(dev);
1474
- return NULL;
1475
- }
1476
- dev_set_name(&dev->dev, "iio:device%d", dev->id);
1477
- INIT_LIST_HEAD(&dev->buffer_list);
1561
+ dev->dev.parent = parent;
1562
+ dev->dev.groups = dev->groups;
1563
+ dev->dev.type = &iio_device_type;
1564
+ dev->dev.bus = &iio_bus_type;
1565
+ device_initialize(&dev->dev);
1566
+ dev_set_drvdata(&dev->dev, (void *)dev);
1567
+ mutex_init(&dev->mlock);
1568
+ mutex_init(&dev->info_exist_lock);
1569
+ INIT_LIST_HEAD(&iio_dev_opaque->channel_attr_list);
1570
+
1571
+ dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1572
+ if (dev->id < 0) {
1573
+ /* cannot use a dev_err as the name isn't available */
1574
+ pr_err("failed to get device id\n");
1575
+ kfree(iio_dev_opaque);
1576
+ return NULL;
14781577 }
1578
+ dev_set_name(&dev->dev, "iio:device%d", dev->id);
1579
+ INIT_LIST_HEAD(&iio_dev_opaque->buffer_list);
14791580
14801581 return dev;
14811582 }
....@@ -1497,32 +1598,18 @@
14971598 iio_device_free(*(struct iio_dev **)res);
14981599 }
14991600
1500
-int devm_iio_device_match(struct device *dev, void *res, void *data)
1501
-{
1502
- struct iio_dev **r = res;
1503
- if (!r || !*r) {
1504
- WARN_ON(!r || !*r);
1505
- return 0;
1506
- }
1507
- return *r == data;
1508
-}
1509
-EXPORT_SYMBOL_GPL(devm_iio_device_match);
1510
-
15111601 /**
15121602 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
1513
- * @dev: Device to allocate iio_dev for
1603
+ * @parent: Device to allocate iio_dev for, and parent for this IIO device
15141604 * @sizeof_priv: Space to allocate for private structure.
15151605 *
15161606 * Managed iio_device_alloc. iio_dev allocated with this function is
15171607 * automatically freed on driver detach.
15181608 *
1519
- * If an iio_dev allocated with this function needs to be freed separately,
1520
- * devm_iio_device_free() must be used.
1521
- *
15221609 * RETURNS:
15231610 * Pointer to allocated iio_dev on success, NULL on failure.
15241611 */
1525
-struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1612
+struct iio_dev *devm_iio_device_alloc(struct device *parent, int sizeof_priv)
15261613 {
15271614 struct iio_dev **ptr, *iio_dev;
15281615
....@@ -1531,10 +1618,10 @@
15311618 if (!ptr)
15321619 return NULL;
15331620
1534
- iio_dev = iio_device_alloc(sizeof_priv);
1621
+ iio_dev = iio_device_alloc(parent, sizeof_priv);
15351622 if (iio_dev) {
15361623 *ptr = iio_dev;
1537
- devres_add(dev, ptr);
1624
+ devres_add(parent, ptr);
15381625 } else {
15391626 devres_free(ptr);
15401627 }
....@@ -1542,23 +1629,6 @@
15421629 return iio_dev;
15431630 }
15441631 EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
1545
-
1546
-/**
1547
- * devm_iio_device_free - Resource-managed iio_device_free()
1548
- * @dev: Device this iio_dev belongs to
1549
- * @iio_dev: the iio_dev associated with the device
1550
- *
1551
- * Free iio_dev allocated with devm_iio_device_alloc().
1552
- */
1553
-void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
1554
-{
1555
- int rc;
1556
-
1557
- rc = devres_release(dev, devm_iio_device_release,
1558
- devm_iio_device_match, iio_dev);
1559
- WARN_ON(rc);
1560
-}
1561
-EXPORT_SYMBOL_GPL(devm_iio_device_free);
15621632
15631633 /**
15641634 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
....@@ -1623,14 +1693,14 @@
16231693 }
16241694
16251695 static const struct file_operations iio_buffer_fileops = {
1626
- .read = iio_buffer_read_first_n_outer_addr,
1696
+ .read = iio_buffer_read_outer_addr,
16271697 .release = iio_chrdev_release,
16281698 .open = iio_chrdev_open,
16291699 .poll = iio_buffer_poll_addr,
16301700 .owner = THIS_MODULE,
16311701 .llseek = noop_llseek,
16321702 .unlocked_ioctl = iio_ioctl,
1633
- .compat_ioctl = iio_ioctl,
1703
+ .compat_ioctl = compat_ptr_ioctl,
16341704 };
16351705
16361706 static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
....@@ -1662,10 +1732,16 @@
16621732 {
16631733 int ret;
16641734
1735
+ if (!indio_dev->info)
1736
+ return -EINVAL;
1737
+
16651738 indio_dev->driver_module = this_mod;
16661739 /* If the calling driver did not initialize of_node, do it here */
16671740 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
16681741 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1742
+
1743
+ indio_dev->label = of_get_property(indio_dev->dev.of_node, "label",
1744
+ NULL);
16691745
16701746 ret = iio_check_unique_scan_index(indio_dev);
16711747 if (ret < 0)
....@@ -1674,12 +1750,7 @@
16741750 /* configure elements for the chrdev */
16751751 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
16761752
1677
- ret = iio_device_register_debugfs(indio_dev);
1678
- if (ret) {
1679
- dev_err(indio_dev->dev.parent,
1680
- "Failed to register debugfs interfaces\n");
1681
- return ret;
1682
- }
1753
+ iio_device_register_debugfs(indio_dev);
16831754
16841755 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
16851756 if (ret) {
....@@ -1779,23 +1850,6 @@
17791850 return ret;
17801851 }
17811852 EXPORT_SYMBOL_GPL(__devm_iio_device_register);
1782
-
1783
-/**
1784
- * devm_iio_device_unregister - Resource-managed iio_device_unregister()
1785
- * @dev: Device this iio_dev belongs to
1786
- * @indio_dev: the iio_dev associated with the device
1787
- *
1788
- * Unregister iio_dev registered with devm_iio_device_register().
1789
- */
1790
-void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
1791
-{
1792
- int rc;
1793
-
1794
- rc = devres_release(dev, devm_iio_device_unreg,
1795
- devm_iio_device_match, indio_dev);
1796
- WARN_ON(rc);
1797
-}
1798
-EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
17991853
18001854 /**
18011855 * iio_device_claim_direct_mode - Keep device in direct mode