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