Skip to contentSkip to conversation

macintosh/macio: Fix sysfs output in compatible_show()

PATCH New Thorsten Blum 1 participant 1 message Nov 26 Linux for power macintosh

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 macintosh/macio: Fix sysfs output in compatible_show()

compatible_show() incorrectly advances the buffer pointer by the accumulated output length instead of the length of the last string. If the property contains three or more strings, this leaves gaps in the output and can truncate or omit later strings.

For example, a PowerMac11,2 OF dump [1] shows the compatible strings "mac-io-i2c-control", "k2-i2c", and "keywest-i2c". The first two strings take 19 and 7 bytes, but buf is advanced by 19 and then by 26, so the third string is written at offset 45. However, only 38 bytes are returned, so "keywest-i2c" is omitted from the sysfs output.

Use of_property_for_each_string() to iterate over the compatible strings and sysfs_emit_at() to append each string at the correct offset.

Fixes b5bf5b6786cc ("[PATCH] openfirmware: add sysfs nodes for open firmware devices")

---
Compile-tested only.

 drivers/macintosh/macio_sysfs.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)
drivers/macintosh/macio_sysfs.c +4 -16
index 2bbe359b26d9..59fb2a51de2d 100644
@@ -3,30 +3,18 @@
 #include <linux/of.h>
 #include <linux/of_device.h>
 #include <linux/stat.h>
+#include <linux/sysfs.h>
 #include <asm/macio.h>
 
 static ssize_t
 compatible_show (struct device *dev, struct device_attribute *attr, char *buf)
 {
-	struct platform_device *of;
+	struct property *prop;
 	const char *compat;
-	int cplen;
 	int length = 0;
 
-	of = &to_macio_device (dev)->ofdev;
-	compat = of_get_property(of->dev.of_node, "compatible", &cplen);
-	if (!compat) {
-		*buf = '\0';
-		return 0;
-	}
-	while (cplen > 0) {
-		int l;
-		length += sprintf (buf, "%s\n", compat);
-		buf += length;
-		l = strlen (compat) + 1;
-		compat += l;
-		cplen -= l;
-	}
+	of_property_for_each_string(dev->of_node, "compatible", prop, compat)
+		length += sysfs_emit_at(buf, length, "%s\n", compat);
 
 	return length;
 }