aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/linuxkpi/common/src/linux_firmware.c
blob: 17da9138128081c25aa047af22250048b46f4277 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2020-2021 The FreeBSD Foundation
 * Copyright (c) 2022 Bjoern A. Zeeb
 *
 * This software was developed by Björn Zeeb under sponsorship from
 * the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/firmware.h>
#include <sys/queue.h>
#include <sys/taskqueue.h>

#include <linux/types.h>
#include <linux/device.h>

#include <linux/firmware.h>
#undef firmware

MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware");

struct lkpi_fw_task {
	/* Task and arguments for the "nowait" callback. */
	struct task		fw_task;
	gfp_t			gfp;
	const char		*fw_name;
	struct device		*dev;
	void			*drv;
	void(*cont)(const struct linuxkpi_firmware *, void *);
};

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)
{
	const struct firmware *fbdfw;
	struct linuxkpi_firmware *lfw;
	const char *fwimg;
	char *p;
	uint32_t flags;

	if (fw_name == NULL || fw == NULL || dev == NULL) {
		*fw = NULL;
		return (-EINVAL);
	}

	/* Set independent on "warn". To debug, bootverbose is avail. */
	flags = FIRMWARE_GET_NOWARN;

	KASSERT(gfp == GFP_KERNEL, ("%s: gfp %#x\n", __func__, gfp));
	lfw = malloc(sizeof(*lfw), M_LKPI_FW, M_WAITOK | M_ZERO);

	/*
	 * Linux can have a path in the firmware which is hard to replicate
	 * for auto-firmware-module-loading.
	 * On FreeBSD, depending on what people do, the firmware will either
	 * be called "fw", or "dir_fw", or "modname_dir_fw".  The latter the
	 * driver author has to deal with herself (requesting the special name).
	 * We also optionally flatten '/'s and '.'s as some firmware modules do.
	 * We probe in the least-of-work order avoiding memory operations.
	 * It will be preferred to build the firmware .ko in a well matching
	 * way rather than adding more name-mangling-hacks here in the future
	 * (though we could if needed).
	 */
	/* (1) Try any name removed of path. */
	fwimg = strrchr(fw_name, '/');
	if (fwimg != NULL)
		fwimg++;
	if (fwimg == NULL || *fwimg == '\0')
		fwimg = fw_name;
	fbdfw = firmware_get_flags(fwimg, flags);
	/* (2) Try the original name if we have not yet. */
	if (fbdfw == NULL && fwimg != fw_name) {
		fwimg = fw_name;
		fbdfw = firmware_get_flags(fwimg, flags);
	}
	/* (3) Flatten '/', '.' and '-' to '_' and try with adjusted name. */
	if (fbdfw == NULL &&
	    (strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL ||
	    strchr(fw_name, '-'))) {
		fwimg = strdup(fw_name, M_LKPI_FW);
		if (fwimg != NULL) {
			while ((p = strchr(fwimg, '/')) != NULL)
				*p = '_';
			fbdfw = firmware_get_flags(fwimg, flags);
			if (fbdfw == NULL) {
				while ((p = strchr(fwimg, '.')) != NULL)
					*p = '_';
				fbdfw = firmware_get_flags(fwimg, flags);
			}
			if (fbdfw == NULL) {
				while ((p = strchr(fwimg, '-')) != NULL)
					*p = '_';
				fbdfw = firmware_get_flags(fwimg, flags);
			}
			free(__DECONST(void *, fwimg), M_LKPI_FW);
		}
	}
	if (fbdfw == NULL) {
		if (enoentok)
			*fw = lfw;
		else {
			free(lfw, M_LKPI_FW);
			*fw = NULL;
		}
		if (warn)
			device_printf(dev->bsddev, "could not load firmware "
			    "image '%s'\n", fw_name);
		return (-ENOENT);
	}

	device_printf(dev->bsddev,"successfully loaded firmware image '%s'\n",
	    fw_name);
	lfw->fbdfw = fbdfw;
	lfw->data = (const uint8_t *)fbdfw->data;
	lfw->size = fbdfw->datasize;
	*fw = lfw;
	return (0);
}

static void
lkpi_fw_task(void *ctx, int pending)
{
	struct lkpi_fw_task *lfwt;
	const struct linuxkpi_firmware *fw;

	KASSERT(ctx != NULL && pending == 1, ("%s: lfwt %p, pending %d\n",
	    __func__, ctx, pending));

	lfwt = ctx;
	if (lfwt->cont == NULL)
		goto out;

	_linuxkpi_request_firmware(lfwt->fw_name, &fw, lfwt->dev,
	    lfwt->gfp, true, true);

	/*
	 * Linux seems to run the callback if it cannot find the firmware.
	 * We call it in all cases as it is the only feedback to the requester.
	 */
	lfwt->cont(fw, lfwt->drv);
	/* Do not assume fw is still valid! */

out:
	free(lfwt, M_LKPI_FW);
}

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 *))
{
	struct lkpi_fw_task *lfwt;
	int error;

	lfwt = malloc(sizeof(*lfwt), M_LKPI_FW, M_WAITOK | M_ZERO);
	lfwt->gfp = gfp;
	lfwt->fw_name = fw_name;
	lfwt->dev = dev;
	lfwt->drv = drv;
	lfwt->cont = cont;
	TASK_INIT(&lfwt->fw_task, 0, lkpi_fw_task, lfwt);
	error = taskqueue_enqueue(taskqueue_thread, &lfwt->fw_task);

	if (error)
		return (-error);
	return (0);
}

int
linuxkpi_request_firmware(const struct linuxkpi_firmware **fw,
    const char *fw_name, struct device *dev)
{

	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
	    true));
}

int
linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw,
    const char *fw_name, struct device *dev)
{

	return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,
	    false));
}

void
linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)
{

	if (fw == NULL)
		return;

	if (fw->fbdfw)
		firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);
	free(__DECONST(void *, fw), M_LKPI_FW);
}

int
linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware **fw,
    const char *fw_name, struct device *dev, uint8_t *buf, size_t buflen,
    size_t offset)
{
	const struct linuxkpi_firmware *lfw;
	int error;

	error = linuxkpi_request_firmware(fw, fw_name, dev);
	if (error != 0)
		return (error);

	lfw = *fw;
	if ((offset + buflen) >= lfw->size) {
		linuxkpi_release_firmware(lfw);
		return (-ERANGE);
	}

	memcpy(buf, lfw->data + offset, buflen);

	return (0);
}