xref: /freebsd/sys/compat/linuxkpi/common/src/linux_firmware.c (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
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/types.h>
34 #include <sys/malloc.h>
35 #include <sys/firmware.h>
36 
37 #include <linux/types.h>
38 #include <linux/device.h>
39 
40 #include <linux/firmware.h>
41 #undef firmware
42 
43 MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware");
44 
45 static int
46 _linuxkpi_request_firmware(const char *fw_name, const struct linuxkpi_firmware **fw,
47     struct device *dev, gfp_t gfp __unused, bool enoentok, bool warn)
48 {
49 	const struct firmware *fbdfw;
50 	struct linuxkpi_firmware *lfw;
51 	const char *fwimg;
52 	char *p;
53 	uint32_t flags;
54 
55 	if (fw_name == NULL || fw == NULL || dev == NULL)
56 		return (-EINVAL);
57 
58 	/* Set independent on "warn". To debug, bootverbose is avail. */
59 	flags = FIRMWARE_GET_NOWARN;
60 
61 	KASSERT(gfp == GFP_KERNEL, ("%s: gfp %#x\n", __func__, gfp));
62 	lfw = malloc(sizeof(*lfw), M_LKPI_FW, M_WAITOK | M_ZERO);
63 
64 	/*
65 	 * Linux can have a path in the firmware which is hard to replicate
66 	 * for auto-firmware-module-loading.
67 	 * On FreeBSD, depending on what people do, the firmware will either
68 	 * be called "fw", or "dir_fw", or "modname_dir_fw".  The latter the
69 	 * driver author has to deal with herself (requesting the special name).
70 	 * We also optionally flatten '/'s and '.'s as some firmware modules do.
71 	 * We probe in the least-of-work order avoiding memory operations.
72 	 * It will be preferred to build the firmware .ko in a well matching
73 	 * way rather than adding more name-mangling-hacks here in the future
74 	 * (though we could if needed).
75 	 */
76 	/* (1) Try any name removed of path. */
77 	fwimg = strrchr(fw_name, '/');
78 	if (fwimg != NULL)
79 		fwimg++;
80 	if (fwimg == NULL || *fwimg == '\0')
81 		fwimg = fw_name;
82 	fbdfw = firmware_get_flags(fwimg, flags);
83 	/* (2) Try the original name if we have not yet. */
84 	if (fbdfw == NULL && fwimg != fw_name) {
85 		fwimg = fw_name;
86 		fbdfw = firmware_get_flags(fwimg, flags);
87 	}
88 	/* (3) Flatten '/', '.' and '-' to '_' and try with adjusted name. */
89 	if (fbdfw == NULL &&
90 	    (strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL ||
91 	    strchr(fw_name, '-'))) {
92 		fwimg = strdup(fw_name, M_LKPI_FW);
93 		if (fwimg != NULL) {
94 			while ((p = strchr(fwimg, '/')) != NULL)
95 				*p = '_';
96 			fbdfw = firmware_get_flags(fwimg, flags);
97 			if (fbdfw == NULL) {
98 				while ((p = strchr(fwimg, '.')) != NULL)
99 					*p = '_';
100 				fbdfw = firmware_get_flags(fwimg, flags);
101 			}
102 			if (fbdfw == NULL) {
103 				while ((p = strchr(fwimg, '-')) != NULL)
104 					*p = '_';
105 				fbdfw = firmware_get_flags(fwimg, flags);
106 			}
107 			free(__DECONST(void *, fwimg), M_LKPI_FW);
108 		}
109 	}
110 	if (fbdfw == NULL) {
111 		if (enoentok)
112 			*fw = lfw;
113 		else {
114 			free(lfw, M_LKPI_FW);
115 			*fw = NULL;
116 		}
117 		if (warn)
118 			device_printf(dev->bsddev, "could not load firmware "
119 			    "image '%s'\n", fw_name);
120 		return (-ENOENT);
121 	}
122 
123 	device_printf(dev->bsddev,"successfully loaded firmware image '%s'\n",
124 	    fw_name);
125 	lfw->fbdfw = fbdfw;
126 	lfw->data = (const uint8_t *)fbdfw->data;
127 	lfw->size = fbdfw->datasize;
128 	*fw = lfw;
129 	return (0);
130 }
131 
132 int
133 linuxkpi_request_firmware_nowait(struct module *mod __unused, bool _t __unused,
134     const char *fw_name, struct device *dev, gfp_t gfp, void *drv,
135     void(*cont)(const struct linuxkpi_firmware *, void *))
136 {
137 	const struct linuxkpi_firmware *lfw;
138 	int error;
139 
140 	/*
141 	 * Linux seems to run the callback if it cannot find the firmware.
142 	 * The fact that this is "_nowait()" and has a callback seems to
143 	 * imply that this is run in a deferred conext which we currently
144 	 * do not do.  Should it become necessary (a driver actually requiring
145 	 * it) we would need to implement it here.
146 	 */
147 	error = _linuxkpi_request_firmware(fw_name, &lfw, dev, gfp, true, true);
148 	if (error == -ENOENT)
149 		error = 0;
150 	if (error == 0)
151 		cont(lfw, drv);
152 
153 	return (error);
154 }
155 
156 int
157 linuxkpi_request_firmware(const struct linuxkpi_firmware **fw,
158     const char *fw_name, struct device *dev)
159 {
160 
161 	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
162 	    true));
163 }
164 
165 int
166 linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw,
167     const char *fw_name, struct device *dev)
168 {
169 
170 	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
171 	    false));
172 }
173 
174 void
175 linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)
176 {
177 
178 	if (fw == NULL)
179 		return;
180 
181 	if (fw->fbdfw)
182 		firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);
183 	free(__DECONST(void *, fw), M_LKPI_FW);
184 }
185