1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Set of common sysfs utilities.
5 * Copyright (C) 2018 Google, Inc.
8 /* The functions described here are a set of utilities to allow each file in the
9 * Gasket driver framework to manage their own set of sysfs entries, instead of
10 * centralizing all that work in one file.
12 * The goal of these utilities is to allow for sysfs entries to be easily
13 * created without causing a proliferation of sysfs "show" functions. This
14 * requires O(N) string lookups during show function execution, but as reading
15 * sysfs entries is rarely performance-critical, this is likely acceptible.
17 #ifndef __GASKET_SYSFS_H__
18 #define __GASKET_SYSFS_H__
20 #include "gasket_constants.h"
21 #include "gasket_core.h"
22 #include <linux/device.h>
23 #include <linux/stringify.h>
24 #include <linux/sysfs.h>
26 /* The maximum number of mappings/devices a driver needs to support. */
27 #define GASKET_SYSFS_NUM_MAPPINGS (GASKET_FRAMEWORK_DESC_MAX * GASKET_DEV_MAX)
29 /* The maximum number of sysfs nodes in a directory.
31 #define GASKET_SYSFS_MAX_NODES 196
33 /* End markers for sysfs struct arrays. */
34 #define GASKET_ARRAY_END_TOKEN GASKET_RESERVED_ARRAY_END
35 #define GASKET_ARRAY_END_MARKER __stringify(GASKET_ARRAY_END_TOKEN)
38 * Terminator struct for a gasket_sysfs_attr array. Must be at the end of
39 * all gasket_sysfs_attribute arrays.
41 #define GASKET_END_OF_ATTR_ARRAY \
43 .attr = __ATTR_NULL, \
44 .data.attr_type = 0, \
48 * Pairing of sysfs attribute and user data.
49 * Used in lookups in sysfs "show" functions to return attribute metadata.
51 struct gasket_sysfs_attribute {
52 /* The underlying sysfs device attribute associated with this data. */
53 struct device_attribute attr;
55 /* User-specified data to associate with the attribute. */
65 * Function pointer to a callback to be invoked when this attribute is
66 * written (if so configured). The arguments are to the Gasket device
67 * pointer, the enclosing gasket_attr structure, and the value written.
68 * The callback should perform any logging necessary, as errors cannot
69 * be returned from the callback.
71 void (*write_callback)(struct gasket_dev *dev,
72 struct gasket_sysfs_attribute *attr,
76 #define GASKET_SYSFS_RO(_name, _show_function, _attr_type) \
78 .attr = __ATTR(_name, S_IRUGO, _show_function, NULL), \
79 .data.attr_type = _attr_type \
82 /* Initializes the Gasket sysfs subsystem.
84 * Description: Performs one-time initialization. Must be called before usage
85 * at [Gasket] module load time.
87 void gasket_sysfs_init(void);
90 * Create an entry in mapping_data between a device and a Gasket device.
91 * @device: Device struct to map to.
92 * @gasket_dev: The dev struct associated with the driver controlling @device.
94 * Description: This function maps a gasket_dev* to a device*. This mapping can
95 * be used in sysfs_show functions to get a handle to the gasket_dev struct
96 * controlling the device node.
98 * If this function is not called before gasket_sysfs_create_entries, a warning
101 int gasket_sysfs_create_mapping(struct device *device,
102 struct gasket_dev *gasket_dev);
105 * Creates bulk entries in sysfs.
106 * @device: Kernel device structure.
107 * @attrs: List of attributes/sysfs entries to create.
109 * Description: Creates each sysfs entry described in "attrs". Can be called
110 * multiple times for a given @device. If the gasket_dev specified in
111 * gasket_sysfs_create_mapping had a legacy device, the entries will be created
114 int gasket_sysfs_create_entries(struct device *device,
115 const struct gasket_sysfs_attribute *attrs);
118 * Removes a device mapping from the global table.
119 * @device: Device to unmap.
121 * Description: Removes the device->Gasket device mapping from the internal
124 void gasket_sysfs_remove_mapping(struct device *device);
127 * User data lookup based on kernel device structure.
128 * @device: Kernel device structure.
130 * Description: Returns the user data associated with "device" in a prior call
131 * to gasket_sysfs_create_entries. Returns NULL if no mapping can be found.
132 * Upon success, this call take a reference to internal sysfs data that must be
133 * released with gasket_sysfs_put_device_data. While this reference is held, the
134 * underlying device sysfs information/structure will remain valid/will not be
137 struct gasket_dev *gasket_sysfs_get_device_data(struct device *device);
140 * Releases a references to internal data.
141 * @device: Kernel device structure.
142 * @dev: Gasket device descriptor (returned by gasket_sysfs_get_device_data).
144 void gasket_sysfs_put_device_data(struct device *device,
145 struct gasket_dev *gasket_dev);
148 * Gasket-specific attribute lookup.
149 * @device: Kernel device structure.
150 * @attr: Device attribute to look up.
152 * Returns the Gasket sysfs attribute associated with the kernel device
153 * attribute and device structure itself. Upon success, this call will take a
154 * reference to internal sysfs data that must be released with a call to
155 * gasket_sysfs_put_attr. While this reference is held, the underlying device
156 * sysfs information/structure will remain valid/will not be deleted.
158 struct gasket_sysfs_attribute *
159 gasket_sysfs_get_attr(struct device *device, struct device_attribute *attr);
162 * Releases a references to internal data.
163 * @device: Kernel device structure.
164 * @attr: Gasket sysfs attribute descriptor (returned by
165 * gasket_sysfs_get_attr).
167 void gasket_sysfs_put_attr(struct device *device,
168 struct gasket_sysfs_attribute *attr);
171 * Write to a register sysfs node.
172 * @buf: NULL-terminated data being written.
173 * @count: number of bytes in the "buf" argument.
175 ssize_t gasket_sysfs_register_store(struct device *device,
176 struct device_attribute *attr,
177 const char *buf, size_t count);
179 #endif /* __GASKET_SYSFS_H__ */