Linux-libre 4.9.30-gnu
[librecmc/linux-libre.git] / include / linux / firmware.h
1 #ifndef _LINUX_FIRMWARE_H
2 #define _LINUX_FIRMWARE_H
3
4 #include <linux/types.h>
5 #include <linux/compiler.h>
6 #include <linux/gfp.h>
7
8 #define FW_ACTION_NOHOTPLUG 0
9 #define FW_ACTION_HOTPLUG 1
10
11 struct firmware {
12         size_t size;
13         const u8 *data;
14         struct page **pages;
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 request_firmware_nowait(
45         struct module *module, bool uevent,
46         const char *name, struct device *device, gfp_t gfp, void *context,
47         void (*cont)(const struct firmware *fw, void *context));
48 int request_firmware_direct(const struct firmware **fw, const char *name,
49                             struct device *device);
50 int request_firmware_into_buf(const struct firmware **firmware_p,
51         const char *name, struct device *device, void *buf, size_t size);
52
53 void release_firmware(const struct firmware *fw);
54 #else
55 static inline int request_firmware(const struct firmware **fw,
56                                    const char *name,
57                                    struct device *device)
58 {
59         return -EINVAL;
60 }
61 static inline int request_firmware_nowait(
62         struct module *module, bool uevent,
63         const char *name, struct device *device, gfp_t gfp, void *context,
64         void (*cont)(const struct firmware *fw, void *context))
65 {
66         return -EINVAL;
67 }
68
69 static inline void release_firmware(const struct firmware *fw)
70 {
71 }
72
73 static inline int request_firmware_direct(const struct firmware **fw,
74                                           const char *name,
75                                           struct device *device)
76 {
77         return -EINVAL;
78 }
79
80 static inline int request_firmware_into_buf(const struct firmware **firmware_p,
81         const char *name, struct device *device, void *buf, size_t size)
82 {
83         return -EINVAL;
84 }
85
86 #endif
87 #ifndef _LINUX_LIBRE_FIRMWARE_H
88 #define _LINUX_LIBRE_FIRMWARE_H
89
90 #include <linux/device.h>
91
92 #define NONFREE_FIRMWARE "/*(DEBLOBBED)*/"
93
94 static inline int
95 is_nonfree_firmware(const char *name)
96 {
97   return strstr(name, NONFREE_FIRMWARE) != 0;
98 }
99
100 static inline int
101 report_missing_free_firmware(const char *name, const char *what)
102 {
103         printk(KERN_ERR "%s: Missing Free %s (non-Free firmware loading is disabled)\n", name,
104                what ? what : "firmware");
105         return -EINVAL;
106 }
107 static inline int
108 reject_firmware(const struct firmware **fw,
109                 const char *name, struct device *device)
110 {
111         const struct firmware *xfw = NULL;
112         int retval;
113         report_missing_free_firmware(dev_name(device), NULL);
114         retval = request_firmware(&xfw, NONFREE_FIRMWARE, device);
115         if (!retval)
116                 release_firmware(xfw);
117         return -EINVAL;
118 }
119 static inline int
120 maybe_reject_firmware(const struct firmware **fw,
121                       const char *name, struct device *device)
122 {
123         if (is_nonfree_firmware(name))
124                 return reject_firmware(fw, name, device);
125         else
126                 return request_firmware(fw, name, device);
127 }
128 static inline int
129 reject_firmware_direct(const struct firmware **fw,
130                 const char *name, struct device *device)
131 {
132         const struct firmware *xfw = NULL;
133         int retval;
134         report_missing_free_firmware(dev_name(device), NULL);
135         retval = request_firmware_direct(&xfw, NONFREE_FIRMWARE, device);
136         if (!retval)
137                 release_firmware(xfw);
138         return -EINVAL;
139 }
140 static inline void
141 discard_rejected_firmware(const struct firmware *fw, void *context)
142 {
143         release_firmware(fw);
144 }
145 static inline int
146 reject_firmware_nowait(struct module *module, int uevent,
147                        const char *name, struct device *device,
148                        gfp_t gfp, void *context,
149                        void (*cont)(const struct firmware *fw,
150                                     void *context))
151 {
152         int retval;
153         report_missing_free_firmware(dev_name(device), NULL);
154         retval = request_firmware_nowait(module, uevent, NONFREE_FIRMWARE,
155                                          device, gfp, NULL,
156                                          discard_rejected_firmware);
157         if (retval)
158                 return retval;
159         return -EINVAL;
160 }
161 static inline int
162 maybe_reject_firmware_nowait(struct module *module, int uevent,
163                              const char *name, struct device *device,
164                              gfp_t gfp, void *context,
165                              void (*cont)(const struct firmware *fw,
166                                           void *context))
167 {
168         if (is_nonfree_firmware(name))
169                 return reject_firmware_nowait(module, uevent, name,
170                                               device, gfp, context, cont);
171         else
172                 return request_firmware_nowait(module, uevent, name,
173                                                device, gfp, context, cont);
174 }
175
176 #endif /* _LINUX_LIBRE_FIRMWARE_H */
177
178 #endif