1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2020-2021 The FreeBSD Foundation
5 *
6 * This software was developed by Björn Zeeb under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/firmware.h>
38 #include <sys/queue.h>
39 #include <sys/taskqueue.h>
40
41 #include <linux/types.h>
42 #include <linux/device.h>
43
44 #include <linux/firmware.h>
45 #undef firmware
46
47 MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware");
48
49 struct lkpi_fw_task {
50 /* Task and arguments for the "nowait" callback. */
51 struct task fw_task;
52 gfp_t gfp;
53 const char *fw_name;
54 struct device *dev;
55 void *drv;
56 void(*cont)(const struct linuxkpi_firmware *, void *);
57 };
58
59 static int
_linuxkpi_request_firmware(const char * fw_name,const struct linuxkpi_firmware ** fw,struct device * dev,gfp_t gfp __unused,bool enoentok,bool warn)60 _linuxkpi_request_firmware(const char *fw_name, const struct linuxkpi_firmware **fw,
61 struct device *dev, gfp_t gfp __unused, bool enoentok, bool warn)
62 {
63 const struct firmware *fbdfw;
64 struct linuxkpi_firmware *lfw;
65 const char *fwimg;
66 char *p;
67 uint32_t flags;
68
69 if (fw_name == NULL || fw == NULL || dev == NULL) {
70 *fw = NULL;
71 return (-EINVAL);
72 }
73
74 /* Set independent on "warn". To debug, bootverbose is avail. */
75 flags = FIRMWARE_GET_NOWARN;
76
77 KASSERT(gfp == GFP_KERNEL, ("%s: gfp %#x\n", __func__, gfp));
78 lfw = malloc(sizeof(*lfw), M_LKPI_FW, M_WAITOK | M_ZERO);
79
80 /*
81 * Linux can have a path in the firmware which is hard to replicate
82 * for auto-firmware-module-loading.
83 * On FreeBSD, depending on what people do, the firmware will either
84 * be called "fw", or "dir_fw", or "modname_dir_fw". The latter the
85 * driver author has to deal with herself (requesting the special name).
86 * We also optionally flatten '/'s and '.'s as some firmware modules do.
87 * We probe in the least-of-work order avoiding memory operations.
88 * It will be preferred to build the firmware .ko in a well matching
89 * way rather than adding more name-mangling-hacks here in the future
90 * (though we could if needed).
91 */
92 /* (1) Try any name removed of path. */
93 fwimg = strrchr(fw_name, '/');
94 if (fwimg != NULL)
95 fwimg++;
96 if (fwimg == NULL || *fwimg == '\0')
97 fwimg = fw_name;
98 fbdfw = firmware_get_flags(fwimg, flags);
99 /* (2) Try the original name if we have not yet. */
100 if (fbdfw == NULL && fwimg != fw_name) {
101 fwimg = fw_name;
102 fbdfw = firmware_get_flags(fwimg, flags);
103 }
104 /* (3) Flatten '/', '.' and '-' to '_' and try with adjusted name. */
105 if (fbdfw == NULL &&
106 (strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL ||
107 strchr(fw_name, '-'))) {
108 fwimg = strdup(fw_name, M_LKPI_FW);
109 if (fwimg != NULL) {
110 while ((p = strchr(fwimg, '/')) != NULL)
111 *p = '_';
112 fbdfw = firmware_get_flags(fwimg, flags);
113 if (fbdfw == NULL) {
114 while ((p = strchr(fwimg, '.')) != NULL)
115 *p = '_';
116 fbdfw = firmware_get_flags(fwimg, flags);
117 }
118 if (fbdfw == NULL) {
119 while ((p = strchr(fwimg, '-')) != NULL)
120 *p = '_';
121 fbdfw = firmware_get_flags(fwimg, flags);
122 }
123 free(__DECONST(void *, fwimg), M_LKPI_FW);
124 }
125 }
126 if (fbdfw == NULL) {
127 if (enoentok)
128 *fw = lfw;
129 else {
130 free(lfw, M_LKPI_FW);
131 *fw = NULL;
132 }
133 if (warn)
134 device_printf(dev->bsddev, "could not load firmware "
135 "image '%s'\n", fw_name);
136 return (-ENOENT);
137 }
138
139 device_printf(dev->bsddev,"successfully loaded firmware image '%s'\n",
140 fw_name);
141 lfw->fbdfw = fbdfw;
142 lfw->data = (const uint8_t *)fbdfw->data;
143 lfw->size = fbdfw->datasize;
144 *fw = lfw;
145 return (0);
146 }
147
148 static void
lkpi_fw_task(void * ctx,int pending)149 lkpi_fw_task(void *ctx, int pending)
150 {
151 struct lkpi_fw_task *lfwt;
152 const struct linuxkpi_firmware *fw;
153
154 KASSERT(ctx != NULL && pending == 1, ("%s: lfwt %p, pending %d\n",
155 __func__, ctx, pending));
156
157 lfwt = ctx;
158 if (lfwt->cont == NULL)
159 goto out;
160
161 _linuxkpi_request_firmware(lfwt->fw_name, &fw, lfwt->dev,
162 lfwt->gfp, true, true);
163
164 /*
165 * Linux seems to run the callback if it cannot find the firmware.
166 * We call it in all cases as it is the only feedback to the requester.
167 */
168 lfwt->cont(fw, lfwt->drv);
169 /* Do not assume fw is still valid! */
170
171 out:
172 free(lfwt, M_LKPI_FW);
173 }
174
175 int
linuxkpi_request_firmware_nowait(struct module * mod __unused,bool _t __unused,const char * fw_name,struct device * dev,gfp_t gfp,void * drv,void (* cont)(const struct linuxkpi_firmware *,void *))176 linuxkpi_request_firmware_nowait(struct module *mod __unused, bool _t __unused,
177 const char *fw_name, struct device *dev, gfp_t gfp, void *drv,
178 void(*cont)(const struct linuxkpi_firmware *, void *))
179 {
180 struct lkpi_fw_task *lfwt;
181 int error;
182
183 lfwt = malloc(sizeof(*lfwt), M_LKPI_FW, M_WAITOK | M_ZERO);
184 lfwt->gfp = gfp;
185 lfwt->fw_name = fw_name;
186 lfwt->dev = dev;
187 lfwt->drv = drv;
188 lfwt->cont = cont;
189 TASK_INIT(&lfwt->fw_task, 0, lkpi_fw_task, lfwt);
190 error = taskqueue_enqueue(taskqueue_thread, &lfwt->fw_task);
191
192 if (error)
193 return (-error);
194 return (0);
195 }
196
197 int
linuxkpi_request_firmware(const struct linuxkpi_firmware ** fw,const char * fw_name,struct device * dev)198 linuxkpi_request_firmware(const struct linuxkpi_firmware **fw,
199 const char *fw_name, struct device *dev)
200 {
201
202 return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
203 true));
204 }
205
206 int
linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware ** fw,const char * fw_name,struct device * dev)207 linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw,
208 const char *fw_name, struct device *dev)
209 {
210
211 return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
212 false));
213 }
214
215 void
linuxkpi_release_firmware(const struct linuxkpi_firmware * fw)216 linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)
217 {
218
219 if (fw == NULL)
220 return;
221
222 if (fw->fbdfw)
223 firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);
224 free(__DECONST(void *, fw), M_LKPI_FW);
225 }
226