GNU Linux-libre 5.15.54-gnu
[releases.git] / include / linux / firmware.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FIRMWARE_H
3 #define _LINUX_FIRMWARE_H
4
5 #include <linux/types.h>
6 #include <linux/compiler.h>
7 #include <linux/gfp.h>
8
9 #define FW_ACTION_NOUEVENT 0
10 #define FW_ACTION_UEVENT 1
11
12 struct firmware {
13         size_t size;
14         const u8 *data;
15
16         /* firmware loader private fields */
17         void *priv;
18 };
19
20 struct module;
21 struct device;
22
23 struct builtin_fw {
24         char *name;
25         void *data;
26         unsigned long size;
27 };
28
29 /* We have to play tricks here much like stringify() to get the
30    __COUNTER__ macro to be expanded as we want it */
31 #define __fw_concat1(x, y) x##y
32 #define __fw_concat(x, y) __fw_concat1(x, y)
33
34 #define DECLARE_BUILTIN_FIRMWARE(name, blob)                                 \
35         DECLARE_BUILTIN_FIRMWARE_SIZE(name, &(blob), sizeof(blob))
36
37 #define DECLARE_BUILTIN_FIRMWARE_SIZE(name, blob, size)                      \
38         static const struct builtin_fw __fw_concat(__builtin_fw,__COUNTER__) \
39         __used __section(".builtin_fw") = { name, blob, size }
40
41 #if defined(CONFIG_FW_LOADER) || (defined(CONFIG_FW_LOADER_MODULE) && defined(MODULE))
42 int request_firmware(const struct firmware **fw, const char *name,
43                      struct device *device);
44 int firmware_request_nowarn(const struct firmware **fw, const char *name,
45                             struct device *device);
46 int firmware_request_platform(const struct firmware **fw, const char *name,
47                               struct device *device);
48 int request_firmware_nowait(
49         struct module *module, bool uevent,
50         const char *name, struct device *device, gfp_t gfp, void *context,
51         void (*cont)(const struct firmware *fw, void *context));
52 int request_firmware_direct(const struct firmware **fw, const char *name,
53                             struct device *device);
54 int request_firmware_into_buf(const struct firmware **firmware_p,
55         const char *name, struct device *device, void *buf, size_t size);
56 int request_partial_firmware_into_buf(const struct firmware **firmware_p,
57                                       const char *name, struct device *device,
58                                       void *buf, size_t size, size_t offset);
59
60 void release_firmware(const struct firmware *fw);
61 #else
62 static inline int request_firmware(const struct firmware **fw,
63                                    const char *name,
64                                    struct device *device)
65 {
66         return -EINVAL;
67 }
68
69 static inline int firmware_request_nowarn(const struct firmware **fw,
70                                           const char *name,
71                                           struct device *device)
72 {
73         return -EINVAL;
74 }
75
76 static inline int firmware_request_platform(const struct firmware **fw,
77                                             const char *name,
78                                             struct device *device)
79 {
80         return -EINVAL;
81 }
82
83 static inline int request_firmware_nowait(
84         struct module *module, bool uevent,
85         const char *name, struct device *device, gfp_t gfp, void *context,
86         void (*cont)(const struct firmware *fw, void *context))
87 {
88         return -EINVAL;
89 }
90
91 static inline void release_firmware(const struct firmware *fw)
92 {
93 }
94
95 static inline int request_firmware_direct(const struct firmware **fw,
96                                           const char *name,
97                                           struct device *device)
98 {
99         return -EINVAL;
100 }
101
102 static inline int request_firmware_into_buf(const struct firmware **firmware_p,
103         const char *name, struct device *device, void *buf, size_t size)
104 {
105         return -EINVAL;
106 }
107
108 static inline int request_partial_firmware_into_buf
109                                         (const struct firmware **firmware_p,
110                                          const char *name,
111                                          struct device *device,
112                                          void *buf, size_t size, size_t offset)
113 {
114         return -EINVAL;
115 }
116
117 #endif
118
119 int firmware_request_cache(struct device *device, const char *name);
120
121 #ifndef _LINUX_LIBRE_FIRMWARE_H
122 #define _LINUX_LIBRE_FIRMWARE_H
123
124 #include <linux/device.h>
125
126 #define NONFREE_FIRMWARE "/*(DEBLOBBED)*/"
127
128 static inline int
129 is_nonfree_firmware(const char *name)
130 {
131         return strstr(name, NONFREE_FIRMWARE) != 0;
132 }
133
134 static inline int
135 report_missing_free_firmware(const char *name, const char *what)
136 {
137         printk(KERN_ERR "%s: Missing Free %s (non-Free firmware loading is disabled)\n", name,
138                what ? what : "firmware");
139         return -ENOENT;
140 }
141 static inline int
142 firmware_reject_nowarn(const struct firmware **fw,
143                        const char *name, struct device *device)
144 {
145         const struct firmware *xfw = NULL;
146         int retval, retval0 = -ENOENT;
147         if (fw) *fw = xfw;
148         retval = firmware_request_nowarn(&xfw, NONFREE_FIRMWARE, device);
149         if (!retval) {
150                 release_firmware(xfw);
151                 retval = retval0;
152         }
153         return retval;
154 }
155 static inline int
156 reject_firmware(const struct firmware **fw,
157                 const char *name, struct device *device)
158 {
159         int retval, retval0;
160         retval0 = report_missing_free_firmware(dev_name(device), NULL);
161         retval = firmware_reject_nowarn(fw, name, device);
162         if (!retval) {
163                 retval = retval0;
164         }
165         return retval;
166 }
167 static inline int
168 maybe_reject_firmware(const struct firmware **fw,
169                       const char *name, struct device *device)
170 {
171         if (is_nonfree_firmware(name))
172                 return reject_firmware(fw, name, device);
173         else
174                 return request_firmware(fw, name, device);
175 }
176 static inline int
177 reject_firmware_direct(const struct firmware **fw,
178                 const char *name, struct device *device)
179 {
180         const struct firmware *xfw = NULL;
181         int retval, retval0;
182         if (fw) *fw = xfw;
183         retval0 = report_missing_free_firmware(dev_name(device), NULL);
184         retval = request_firmware_direct(&xfw, NONFREE_FIRMWARE, device);
185         if (!retval) {
186                 release_firmware(xfw);
187                 retval = retval0;
188         }
189         return retval;
190 }
191 static inline int
192 reject_firmware_nowait(struct module *module, int uevent,
193                        const char *name, struct device *device,
194                        gfp_t gfp, void *context,
195                        void (*cont)(const struct firmware *fw,
196                                     void *context))
197 {
198         report_missing_free_firmware(dev_name(device), NULL);
199         /* We assume NONFREE_FIRMWARE will not be found; how could it?  */
200         return request_firmware_nowait(module, uevent, NONFREE_FIRMWARE,
201                                        device, gfp, context, cont);
202 }
203 static inline int
204 maybe_reject_firmware_nowait(struct module *module, int uevent,
205                              const char *name, struct device *device,
206                              gfp_t gfp, void *context,
207                              void (*cont)(const struct firmware *fw,
208                                           void *context))
209 {
210         if (is_nonfree_firmware(name))
211                 return reject_firmware_nowait(module, uevent, name,
212                                               device, gfp, context, cont);
213         else
214                 return request_firmware_nowait(module, uevent, name,
215                                                device, gfp, context, cont);
216 }
217 static inline int
218 reject_firmware_into_buf(const struct firmware **firmware_p, const char *name,
219                          struct device *device, void *buf, size_t size)
220 {
221         const struct firmware *xfw = NULL;
222         int retval, retval0;
223         if (firmware_p) *firmware_p = xfw;
224         retval0 = report_missing_free_firmware(dev_name(device), NULL);
225         retval = request_firmware_into_buf(&xfw, NONFREE_FIRMWARE, device, buf, size);
226         if (!retval) {
227                 release_firmware(xfw);
228                 retval = retval0;
229         }
230         return retval;
231 }
232 static inline int
233 maybe_reject_firmware_into_buf(const struct firmware **firmware_p, const char *name,
234                                struct device *device, void *buf, size_t size)
235 {
236         if (is_nonfree_firmware(name))
237                 return reject_firmware_into_buf(firmware_p, name, device, buf, size);
238         else
239                 return request_firmware_into_buf(firmware_p, name, device, buf, size);
240 }
241 static inline int
242 reject_partial_firmware_into_buf(const struct firmware **firmware_p, const char *name,
243                                  struct device *device, void *buf, size_t size, size_t offset)
244 {
245         const struct firmware *xfw = NULL;
246         int retval, retval0;
247         if (firmware_p) *firmware_p = xfw;
248         retval0 = report_missing_free_firmware(dev_name(device), NULL);
249         retval = request_partial_firmware_into_buf(&xfw, NONFREE_FIRMWARE, device, buf, size, offset);
250         if (!retval) {
251                 release_firmware(xfw);
252                 retval = retval0;
253         }
254         return retval;
255 }
256 static inline int
257 maybe_reject_partial_firmware_into_buf(const struct firmware **firmware_p, const char *name,
258                                        struct device *device, void *buf, size_t size, size_t offset)
259 {
260         if (is_nonfree_firmware(name))
261                 return reject_partial_firmware_into_buf(firmware_p, name, device, buf, size, offset);
262         else
263                 return request_partial_firmware_into_buf(firmware_p, name, device, buf, size, offset);
264 }
265
266 #endif /* _LINUX_LIBRE_FIRMWARE_H */
267
268 #endif