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