diff mbox series

[60/75] backports: Add sysfs_emit_at()

Message ID 20240627234808.1253337-61-hauke@hauke-m.de (mailing list archive)
State New, archived
Headers show
Series backports: Update to kernel 6.1.95 | expand

Commit Message

Hauke Mehrtens June 27, 2024, 11:47 p.m. UTC
The sysfs_emit_at() function is used by mhi host driver.
The code was copied from mainline Linux kernel.

Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
---
 backport/backport-include/linux/sysfs.h |  9 ++++++++
 backport/compat/backport-5.10.c         | 28 +++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
diff mbox series

Patch

diff --git a/backport/backport-include/linux/sysfs.h b/backport/backport-include/linux/sysfs.h
index 7126fb03..5fd25c9c 100644
--- a/backport/backport-include/linux/sysfs.h
+++ b/backport/backport-include/linux/sysfs.h
@@ -10,15 +10,24 @@ 
 
 #if LINUX_VERSION_IS_LESS(5,10,0)
 #define sysfs_emit LINUX_BACKPORT(sysfs_emit)
+#define sysfs_emit_at LINUX_BACKPORT(sysfs_emit_at)
 #ifdef CONFIG_SYSFS
 __printf(2, 3)
 int sysfs_emit(char *buf, const char *fmt, ...);
+__printf(3, 4)
+int sysfs_emit_at(char *buf, int at, const char *fmt, ...);
 #else /* CONFIG_SYSFS */
 __printf(2, 3)
 static inline int sysfs_emit(char *buf, const char *fmt, ...)
 {
 	return 0;
 }
+
+__printf(3, 4)
+static inline int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
+{
+	retur 0;
+}
 #endif /* CONFIG_SYSFS */
 #endif /* < 5.10 */
 
diff --git a/backport/compat/backport-5.10.c b/backport/compat/backport-5.10.c
index c3a7b7d3..c3b8e336 100644
--- a/backport/compat/backport-5.10.c
+++ b/backport/compat/backport-5.10.c
@@ -79,3 +79,31 @@  int sysfs_emit(char *buf, const char *fmt, ...)
 	return len;
 }
 EXPORT_SYMBOL_GPL(sysfs_emit);
+
+/**
+ *	sysfs_emit_at - scnprintf equivalent, aware of PAGE_SIZE buffer.
+ *	@buf:	start of PAGE_SIZE buffer.
+ *	@at:	offset in @buf to start write in bytes
+ *		@at must be >= 0 && < PAGE_SIZE
+ *	@fmt:	format
+ *	@...:	optional arguments to @fmt
+ *
+ *
+ * Returns number of characters written starting at &@buf[@at].
+ */
+int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
+{
+	va_list args;
+	int len;
+
+	if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
+		 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
+		return 0;
+
+	va_start(args, fmt);
+	len = vscnprintf(buf + at, PAGE_SIZE - at, fmt, args);
+	va_end(args);
+
+	return len;
+}
+EXPORT_SYMBOL_GPL(sysfs_emit_at);