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 */
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 */
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
58e0c11a8bSRuss Weight static inline int register_firmware_config_sysctl(void)
59e0c11a8bSRuss Weight {
60e0c11a8bSRuss Weight 	return 0;
61e0c11a8bSRuss Weight }
62e0c11a8bSRuss Weight 
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 */
66e0c11a8bSRuss Weight static inline int register_sysfs_loader(void)
67e0c11a8bSRuss Weight {
68e0c11a8bSRuss Weight 	return 0;
69e0c11a8bSRuss Weight }
70e0c11a8bSRuss Weight 
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 };
83e0c11a8bSRuss Weight 
84e0c11a8bSRuss Weight static inline struct fw_sysfs *to_fw_sysfs(struct device *dev)
85e0c11a8bSRuss Weight {
86e0c11a8bSRuss Weight 	return container_of(dev, struct fw_sysfs, dev);
87e0c11a8bSRuss Weight }
88e0c11a8bSRuss Weight 
89e0c11a8bSRuss Weight void __fw_load_abort(struct fw_priv *fw_priv);
90e0c11a8bSRuss Weight 
91e0c11a8bSRuss Weight static inline void fw_load_abort(struct fw_sysfs *fw_sysfs)
92e0c11a8bSRuss Weight {
93e0c11a8bSRuss Weight 	struct fw_priv *fw_priv = fw_sysfs->fw_priv;
94e0c11a8bSRuss Weight 
95e0c11a8bSRuss Weight 	__fw_load_abort(fw_priv);
96e0c11a8bSRuss Weight }
97e0c11a8bSRuss Weight 
98e0c11a8bSRuss Weight struct fw_sysfs *
99e0c11a8bSRuss Weight fw_create_instance(struct firmware *firmware, const char *fw_name,
100e0c11a8bSRuss Weight 		   struct device *device, u32 opt_flags);
101e0c11a8bSRuss Weight 
102f8ae07f4SRuss Weight #ifdef CONFIG_FW_UPLOAD
103f8ae07f4SRuss Weight extern struct device_attribute dev_attr_status;
104f8ae07f4SRuss Weight extern struct device_attribute dev_attr_error;
105f8ae07f4SRuss Weight extern struct device_attribute dev_attr_cancel;
106f8ae07f4SRuss Weight extern struct device_attribute dev_attr_remaining_size;
107f8ae07f4SRuss Weight 
108f8ae07f4SRuss Weight int fw_upload_start(struct fw_sysfs *fw_sysfs);
109*789bba82SRuss Weight void fw_upload_free(struct fw_sysfs *fw_sysfs);
110f8ae07f4SRuss Weight umode_t fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n);
111f8ae07f4SRuss Weight #else
112f8ae07f4SRuss Weight static inline int fw_upload_start(struct fw_sysfs *fw_sysfs)
113f8ae07f4SRuss Weight {
114f8ae07f4SRuss Weight 	return 0;
115f8ae07f4SRuss Weight }
116*789bba82SRuss Weight 
117*789bba82SRuss Weight static inline void fw_upload_free(struct fw_sysfs *fw_sysfs)
118*789bba82SRuss Weight {
119*789bba82SRuss Weight }
120f8ae07f4SRuss Weight #endif
121f8ae07f4SRuss Weight 
122e0c11a8bSRuss Weight #endif /* __FIRMWARE_SYSFS_H */
123