git: 968650dbdaa1 - main - linuxkpi: Add `BIN_ATTR*()` macros

From: Jean-Sébastien Pédron <dumbbell_at_FreeBSD.org>
Date: Mon, 07 Apr 2025 18:01:28 UTC
The branch main has been updated by dumbbell:

URL: https://cgit.FreeBSD.org/src/commit/?id=968650dbdaa16ac2fac91782522b5074d2202078

commit 968650dbdaa16ac2fac91782522b5074d2202078
Author:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
AuthorDate: 2025-02-25 23:22:26 +0000
Commit:     Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
CommitDate: 2025-04-07 17:35:57 +0000

    linuxkpi: Add `BIN_ATTR*()` macros
    
    They are helpers to declare static `struct bin_attribute`.
    
    The amdgpu DRM driver started to use them for some time but the code was
    commented out (and this is still the case as of this commit). In Linux
    6.8, it declared a new BIN_ATTR. This new code is not commented out.
    
    While here, change the first argument of the `read` and `write`
    callbacks to be a `struct linux_file *` instead of a `struct file *`.
    This ensures the right structure is being referenced at compile time.
    
    Reviewed by:    bz
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D49378
---
 sys/compat/linuxkpi/common/include/linux/sysfs.h | 41 +++++++++++++++++++++---
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/sysfs.h b/sys/compat/linuxkpi/common/include/linux/sysfs.h
index c6692b803789..f11db5ece515 100644
--- a/sys/compat/linuxkpi/common/include/linux/sysfs.h
+++ b/sys/compat/linuxkpi/common/include/linux/sysfs.h
@@ -53,10 +53,10 @@ struct attribute_group {
 struct bin_attribute {
 	struct attribute	attr;
 	size_t			size;
-	ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
-			char *, loff_t, size_t);
-	ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
-			 char *, loff_t, size_t);
+	ssize_t (*read)(struct linux_file *, struct kobject *,
+			struct bin_attribute *, char *, loff_t, size_t);
+	ssize_t (*write)(struct linux_file *, struct kobject *,
+			 struct bin_attribute *, char *, loff_t, size_t);
 };
 
 #define	__ATTR(_name, _mode, _show, _store) {				\
@@ -81,6 +81,39 @@ struct bin_attribute {
 		NULL,							\
 	}
 
+#define	__BIN_ATTR(_name, _mode, _read, _write, _size) {		\
+	.attr = { .name = __stringify(_name), .mode = _mode },		\
+	.read = _read, .write  = _write, .size = _size,			\
+}
+#define	__BIN_ATTR_RO(_name, _size) {					\
+	.attr = { .name = __stringify(_name), .mode = 0444 },		\
+	.read = _name##_read, .size = _size,				\
+}
+#define	__BIN_ATTR_WO(_name, _size) {					\
+	.attr = { .name = __stringify(_name), .mode = 0200 },		\
+	.write = _name##_write, .size = _size,				\
+}
+#define	__BIN_ATTR_WR(_name, _size) {					\
+	.attr = { .name = __stringify(_name), .mode = 0644 },		\
+	.read = _name##_read, .write = _name##_write, .size = _size,	\
+}
+
+#define	BIN_ATTR(_name, _mode, _read, _write, _size) \
+struct bin_attribute bin_attr_##_name = \
+    __BIN_ATTR(_name, _mode, _read, _write, _size);
+
+#define	BIN_ATTR_RO(_name, _size) \
+struct bin_attribute bin_attr_##_name = \
+    __BIN_ATTR_RO(_name, _size);
+
+#define	BIN_ATTR_WO(_name, _size) \
+struct bin_attribute bin_attr_##_name = \
+    __BIN_ATTR_WO(_name, _size);
+
+#define	BIN_ATTR_WR(_name, _size) \
+struct bin_attribute bin_attr_##_name = \
+    __BIN_ATTR_WR(_name, _size);
+
 /*
  * Handle our generic '\0' terminated 'C' string.
  * Two cases: