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
43MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware");
44
45static 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 then '.' to '_' and try with adjusted name. */
89	if (fbdfw == NULL &&
90	    (strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL)) {
91		fwimg = strdup(fw_name, M_LKPI_FW);
92		if (fwimg != NULL) {
93			while ((p = strchr(fwimg, '/')) != NULL)
94				*p = '_';
95			fbdfw = firmware_get_flags(fwimg, flags);
96			if (fbdfw == NULL) {
97				while ((p = strchr(fwimg, '.')) != NULL)
98					*p = '_';
99				fbdfw = firmware_get_flags(fwimg, flags);
100			}
101			free(__DECONST(void *, fwimg), M_LKPI_FW);
102		}
103	}
104	if (fbdfw == NULL) {
105		if (enoentok)
106			*fw = lfw;
107		else {
108			free(lfw, M_LKPI_FW);
109			*fw = NULL;
110		}
111		if (warn)
112			device_printf(dev->bsddev, "could not load firmware "
113			    "image '%s'\n", fw_name);
114		return (-ENOENT);
115	}
116
117	device_printf(dev->bsddev,"successfully loaded firmware image '%s'\n",
118	    fw_name);
119	lfw->fbdfw = fbdfw;
120	lfw->data = (const uint8_t *)fbdfw->data;
121	lfw->size = fbdfw->datasize;
122	*fw = lfw;
123	return (0);
124}
125
126int
127linuxkpi_request_firmware_nowait(struct module *mod __unused, bool _t __unused,
128    const char *fw_name, struct device *dev, gfp_t gfp, void *drv,
129    void(*cont)(const struct linuxkpi_firmware *, void *))
130{
131	const struct linuxkpi_firmware *lfw;
132	int error;
133
134	/*
135	 * Linux seems to run the callback if it cannot find the firmware.
136	 * The fact that this is "_nowait()" and has a callback seems to
137	 * imply that this is run in a deferred conext which we currently
138	 * do not do.  Should it become necessary (a driver actually requiring
139	 * it) we would need to implement it here.
140	 */
141	error = _linuxkpi_request_firmware(fw_name, &lfw, dev, gfp, true, true);
142	if (error == -ENOENT)
143		error = 0;
144	if (error == 0)
145		cont(lfw, drv);
146
147	return (error);
148}
149
150int
151linuxkpi_request_firmware(const struct linuxkpi_firmware **fw,
152    const char *fw_name, struct device *dev)
153{
154
155	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
156	    true));
157}
158
159int
160linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw,
161    const char *fw_name, struct device *dev)
162{
163
164	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
165	    false));
166}
167
168void
169linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)
170{
171
172	if (fw == NULL)
173		return;
174
175	if (fw->fbdfw)
176		firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);
177	free(__DECONST(void *, fw), M_LKPI_FW);
178}
179