GNU Linux-libre 4.14.295-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 request_firmware_nowait(
46         struct module *module, bool uevent,
47         const char *name, struct device *device, gfp_t gfp, void *context,
48         void (*cont)(const struct firmware *fw, void *context));
49 int request_firmware_direct(const struct firmware **fw, const char *name,
50                             struct device *device);
51 int request_firmware_into_buf(const struct firmware **firmware_p,
52         const char *name, struct device *device, void *buf, size_t size);
53
54 void release_firmware(const struct firmware *fw);
55 #else
56 static inline int request_firmware(const struct firmware **fw,
57                                    const char *name,
58                                    struct device *device)
59 {
60         return -EINVAL;
61 }
62 static inline int request_firmware_nowait(
63         struct module *module, bool uevent,
64         const char *name, struct device *device, gfp_t gfp, void *context,
65         void (*cont)(const struct firmware *fw, void *context))
66 {
67         return -EINVAL;
68 }
69
70 static inline void release_firmware(const struct firmware *fw)
71 {
72 }
73
74 static inline int request_firmware_direct(const struct firmware **fw,
75                                           const char *name,
76                                           struct device *device)
77 {
78         return -EINVAL;
79 }
80
81 static inline int request_firmware_into_buf(const struct firmware **firmware_p,
82         const char *name, struct device *device, void *buf, size_t size)
83 {
84         return -EINVAL;
85 }
86
87 #endif
88 #ifndef _LINUX_LIBRE_FIRMWARE_H
89 #define _LINUX_LIBRE_FIRMWARE_H
90
91 #include <linux/device.h>
92
93 #define NONFREE_FIRMWARE "/*(DEBLOBBED)*/"
94
95 static inline int
96 is_nonfree_firmware(const char *name)
97 {
98         return strstr(name, NONFREE_FIRMWARE) != 0;
99 }
100
101 static inline int
102 report_missing_free_firmware(const char *name, const char *what)
103 {
104         printk(KERN_ERR "%s: Missing Free %s (non-Free firmware loading is disabled)\n", name,
105                what ? what : "firmware");
106         return -ENOENT;
107 }
108 static inline int
109 reject_firmware(const struct firmware **fw,
110                 const char *name, struct device *device)
111 {
112         const struct firmware *xfw = NULL;
113         int retval, retval0;
114         retval0 = report_missing_free_firmware(dev_name(device), NULL);
115         retval = request_firmware(&xfw, NONFREE_FIRMWARE, device);
116         if (!retval) {
117                 release_firmware(xfw);
118                 retval = retval0;
119         }
120         return -EINVAL;
121 }
122 static inline int
123 maybe_reject_firmware(const struct firmware **fw,
124                       const char *name, struct device *device)
125 {
126         if (is_nonfree_firmware(name))
127                 return reject_firmware(fw, name, device);
128         else
129                 return request_firmware(fw, name, device);
130 }
131 static inline int
132 reject_firmware_direct(const struct firmware **fw,
133                 const char *name, struct device *device)
134 {
135         const struct firmware *xfw = NULL;
136         int retval, retval0;
137         if (fw) *fw = xfw;
138         retval0 = report_missing_free_firmware(dev_name(device), NULL);
139         retval = request_firmware_direct(&xfw, NONFREE_FIRMWARE, device);
140         if (!retval) {
141                 release_firmware(xfw);
142                 retval = retval0;
143         }
144         return retval;
145 }
146 static inline int
147 reject_firmware_nowait(struct module *module, int uevent,
148                        const char *name, struct device *device,
149                        gfp_t gfp, void *context,
150                        void (*cont)(const struct firmware *fw,
151                                     void *context))
152 {
153         report_missing_free_firmware(dev_name(device), NULL);
154         /* We assume NONFREE_FIRMWARE will not be found; how could it?  */
155         return request_firmware_nowait(module, uevent, NONFREE_FIRMWARE,
156                                        device, gfp, context, cont);
157 }
158 static inline int
159 maybe_reject_firmware_nowait(struct module *module, int uevent,
160                              const char *name, struct device *device,
161                              gfp_t gfp, void *context,
162                              void (*cont)(const struct firmware *fw,
163                                           void *context))
164 {
165         if (is_nonfree_firmware(name))
166                 return reject_firmware_nowait(module, uevent, name,
167                                               device, gfp, context, cont);
168         else
169                 return request_firmware_nowait(module, uevent, name,
170                                                device, gfp, context, cont);
171 }
172 static inline int
173 reject_firmware_into_buf(const struct firmware **firmware_p, const char *name,
174                          struct device *device, void *buf, size_t size)
175 {
176         const struct firmware *xfw = NULL;
177         int retval, retval0;
178         if (firmware_p) *firmware_p = xfw;
179         retval0 = report_missing_free_firmware(dev_name(device), NULL);
180         retval = request_firmware_into_buf(&xfw, NONFREE_FIRMWARE, device, buf, size);
181         if (!retval) {
182                 release_firmware(xfw);
183                 retval = retval0;
184         }
185         return retval;
186 }
187 static inline int
188 maybe_reject_firmware_into_buf(const struct firmware **firmware_p, const char *name,
189                                struct device *device, void *buf, size_t size)
190 {
191         if (is_nonfree_firmware(name))
192                 return reject_firmware_into_buf(firmware_p, name, device, buf, size);
193         else
194                 return request_firmware_into_buf(firmware_p, name, device, buf, size);
195 }
196
197 #endif /* _LINUX_LIBRE_FIRMWARE_H */
198
199 #endif