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
|
/* $NetBSD: linux_firmware.c,v 1.2 2021/12/19 12:01:04 riastradh Exp $ */
/*-
* Copyright (c) 2013 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R. Campbell.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_firmware.c,v 1.2 2021/12/19 12:01:04 riastradh Exp $");
#include <sys/types.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <linux/firmware.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/workqueue.h>
struct firmload_work {
char *flw_name;
void (*flw_callback)(const struct firmware *, void *);
void *flw_cookie;
struct device *flw_device;
struct module *flw_module;
struct work_struct flw_work;
};
int
request_firmware(const struct firmware **fwp, const char *image_name,
struct device *dev)
{
const char *drvname;
struct firmware *fw;
firmware_handle_t handle;
int ret;
fw = kmem_alloc(sizeof(*fw), KM_SLEEP);
/*
* If driver xyz(4) asks for xyz/foo/bar.bin, turn that into
* just foo/bar.bin. This leaves open the possibility of name
* collisions. Let's hope upstream is sensible about this.
*/
drvname = device_cfdriver(dev)->cd_name;
if ((strncmp(drvname, image_name, strlen(drvname)) == 0) &&
(image_name[strlen(drvname)] == '/'))
image_name += (strlen(drvname) + 1);
/* XXX errno NetBSD->Linux */
ret = -firmware_open(drvname, image_name, &handle);
if (ret)
goto fail0;
fw->size = firmware_get_size(handle);
fw->data = firmware_malloc(fw->size);
/* XXX errno NetBSD->Linux */
ret = -firmware_read(handle, 0, fw->data, fw->size);
(void)firmware_close(handle);
if (ret)
goto fail1;
/* Success! */
*fwp = fw;
return 0;
fail1: firmware_free(fw->data, fw->size);
fail0: KASSERT(ret);
kmem_free(fw, sizeof(*fw));
*fwp = NULL;
return ret;
}
int
request_firmware_direct(const struct firmware **fwp, const char *image_name,
struct device *dev)
{
return request_firmware(fwp, image_name, dev);
}
int
firmware_request_nowarn(const struct firmware **fwp, const char *image_name,
struct device *dev)
{
return request_firmware(fwp, image_name, dev);
}
static void
request_firmware_work(struct work_struct *wk)
{
struct firmload_work *work = container_of(wk, struct firmload_work,
flw_work);
const struct firmware *fw;
int ret;
/* Reqeust the firmware. If it failed, set it to NULL. */
ret = request_firmware(&fw, work->flw_name, work->flw_device);
if (ret)
fw = NULL;
/* Call the callback. */
(*work->flw_callback)(fw, work->flw_cookie);
/*
* Release the device and module references now that we're
* done.
*
* XXX Heh. What if the module gets unloaded _during_
* module_rele because it went to zero?
*/
/* XXX device_release */
if (work->flw_module)
module_rele(work->flw_module);
}
int
request_firmware_nowait(struct module *module, bool uevent, const char *name,
struct device *device, gfp_t gfp, void *cookie,
void (*callback)(const struct firmware *, void *))
{
char *namedup;
struct firmload_work *work;
/* Allocate memory for it, or fail if we can't. */
work = kzalloc(sizeof(*work), gfp);
if (work == NULL)
goto fail0;
/* Copy the name just in case. */
namedup = kstrdup(name, gfp);
if (namedup == NULL)
goto fail1;
/* Hold the module and device so they don't go away before callback. */
if (module)
module_hold(module);
/* XXX device_acquire(device) */
/* Initialize the work. */
work->flw_name = namedup;
work->flw_callback = callback;
work->flw_cookie = cookie;
work->flw_device = device;
work->flw_module = module;
INIT_WORK(&work->flw_work, request_firmware_work);
/* Kick it off. */
schedule_work(&work->flw_work);
/* Success! */
return 0;
fail1: kfree(work);
fail0: return -ENOMEM;
}
void
release_firmware(const struct firmware *fw)
{
if (fw != NULL) {
firmware_free(fw->data, fw->size);
kmem_free(__UNCONST(fw), sizeof(*fw));
}
}
|