1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __FIRMWARE_SYSFS_H
3 #define __FIRMWARE_SYSFS_H
4 
5 #include <linux/device.h>
6 
7 #include "firmware.h"
8 
9 MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);
10 
11 extern struct firmware_fallback_config fw_fallback_config;
12 extern struct device_attribute dev_attr_loading;
13 
14 #ifdef CONFIG_FW_LOADER_USER_HELPER
15 /**
16  * struct firmware_fallback_config - firmware fallback configuration settings
17  *
18  * Helps describe and fine tune the fallback mechanism.
19  *
20  * @force_sysfs_fallback: force the sysfs fallback mechanism to be used
21  *	as if one had enabled CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y.
22  *	Useful to help debug a CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y
23  *	functionality on a kernel where that config entry has been disabled.
24  * @ignore_sysfs_fallback: force to disable the sysfs fallback mechanism.
25  *	This emulates the behaviour as if we had set the kernel
26  *	config CONFIG_FW_LOADER_USER_HELPER=n.
27  * @old_timeout: for internal use
28  * @loading_timeout: the timeout to wait for the fallback mechanism before
29  *	giving up, in seconds.
30  */
31 struct firmware_fallback_config {
32 	unsigned int force_sysfs_fallback;
33 	unsigned int ignore_sysfs_fallback;
34 	int old_timeout;
35 	int loading_timeout;
36 };
37 
38 /* These getters are vetted to use int properly */
__firmware_loading_timeout(void)39 static inline int __firmware_loading_timeout(void)
40 {
41 	return fw_fallback_config.loading_timeout;
42 }
43 
44 /* These setters are vetted to use int properly */
__fw_fallback_set_timeout(int timeout)45 static inline void __fw_fallback_set_timeout(int timeout)
46 {
47 	fw_fallback_config.loading_timeout = timeout;
48 }
49 #endif
50 
51 #ifdef CONFIG_FW_LOADER_SYSFS
52 int register_sysfs_loader(void);
53 void unregister_sysfs_loader(void);
54 #if defined(CONFIG_FW_LOADER_USER_HELPER) && defined(CONFIG_SYSCTL)
55 int register_firmware_config_sysctl(void);
56 void unregister_firmware_config_sysctl(void);
57 #else
register_firmware_config_sysctl(void)58 static inline int register_firmware_config_sysctl(void)
59 {
60 	return 0;
61 }
62 
unregister_firmware_config_sysctl(void)63 static inline void unregister_firmware_config_sysctl(void) { }
64 #endif /* CONFIG_FW_LOADER_USER_HELPER && CONFIG_SYSCTL */
65 #else /* CONFIG_FW_LOADER_SYSFS */
register_sysfs_loader(void)66 static inline int register_sysfs_loader(void)
67 {
68 	return 0;
69 }
70 
unregister_sysfs_loader(void)71 static inline void unregister_sysfs_loader(void)
72 {
73 }
74 #endif /* CONFIG_FW_LOADER_SYSFS */
75 
76 struct fw_sysfs {
77 	bool nowait;
78 	struct device dev;
79 	struct fw_priv *fw_priv;
80 	struct firmware *fw;
81 	void *fw_upload_priv;
82 };
83 
to_fw_sysfs(struct device * dev)84 static inline struct fw_sysfs *to_fw_sysfs(struct device *dev)
85 {
86 	return container_of(dev, struct fw_sysfs, dev);
87 }
88 
89 void __fw_load_abort(struct fw_priv *fw_priv);
90 
fw_load_abort(struct fw_sysfs * fw_sysfs)91 static inline void fw_load_abort(struct fw_sysfs *fw_sysfs)
92 {
93 	struct fw_priv *fw_priv = fw_sysfs->fw_priv;
94 
95 	__fw_load_abort(fw_priv);
96 }
97 
98 struct fw_sysfs *
99 fw_create_instance(struct firmware *firmware, const char *fw_name,
100 		   struct device *device, u32 opt_flags);
101 
102 #ifdef CONFIG_FW_UPLOAD
103 extern struct device_attribute dev_attr_status;
104 extern struct device_attribute dev_attr_error;
105 extern struct device_attribute dev_attr_cancel;
106 extern struct device_attribute dev_attr_remaining_size;
107 
108 int fw_upload_start(struct fw_sysfs *fw_sysfs);
109 void fw_upload_free(struct fw_sysfs *fw_sysfs);
110 umode_t fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n);
111 #else
fw_upload_start(struct fw_sysfs * fw_sysfs)112 static inline int fw_upload_start(struct fw_sysfs *fw_sysfs)
113 {
114 	return 0;
115 }
116 
fw_upload_free(struct fw_sysfs * fw_sysfs)117 static inline void fw_upload_free(struct fw_sysfs *fw_sysfs)
118 {
119 }
120 #endif
121 
122 #endif /* __FIRMWARE_SYSFS_H */
123