linux_firmware.c revision 1.1 1 1.1 riastrad /* $NetBSD: linux_firmware.c,v 1.1 2021/12/19 10:50:47 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_firmware.c,v 1.1 2021/12/19 10:50:47 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/device.h>
37 1.1 riastrad #include <sys/kmem.h>
38 1.1 riastrad #include <sys/module.h>
39 1.1 riastrad #include <sys/systm.h>
40 1.1 riastrad
41 1.1 riastrad #include <linux/firmware.h>
42 1.1 riastrad #include <linux/slab.h>
43 1.1 riastrad #include <linux/string.h>
44 1.1 riastrad #include <linux/workqueue.h>
45 1.1 riastrad
46 1.1 riastrad struct firmload_work {
47 1.1 riastrad char *flw_name;
48 1.1 riastrad void (*flw_callback)(const struct firmware *, void *);
49 1.1 riastrad void *flw_cookie;
50 1.1 riastrad struct device *flw_device;
51 1.1 riastrad struct module *flw_module;
52 1.1 riastrad struct work_struct flw_work;
53 1.1 riastrad };
54 1.1 riastrad
55 1.1 riastrad int
56 1.1 riastrad request_firmware(const struct firmware **fwp, const char *image_name,
57 1.1 riastrad struct device *dev)
58 1.1 riastrad {
59 1.1 riastrad const char *drvname;
60 1.1 riastrad struct firmware *fw;
61 1.1 riastrad firmware_handle_t handle;
62 1.1 riastrad int ret;
63 1.1 riastrad
64 1.1 riastrad fw = kmem_alloc(sizeof(*fw), KM_SLEEP);
65 1.1 riastrad
66 1.1 riastrad /*
67 1.1 riastrad * If driver xyz(4) asks for xyz/foo/bar.bin, turn that into
68 1.1 riastrad * just foo/bar.bin. This leaves open the possibility of name
69 1.1 riastrad * collisions. Let's hope upstream is sensible about this.
70 1.1 riastrad */
71 1.1 riastrad drvname = device_cfdriver(dev)->cd_name;
72 1.1 riastrad if ((strncmp(drvname, image_name, strlen(drvname)) == 0) &&
73 1.1 riastrad (image_name[strlen(drvname)] == '/'))
74 1.1 riastrad image_name += (strlen(drvname) + 1);
75 1.1 riastrad
76 1.1 riastrad /* XXX errno NetBSD->Linux */
77 1.1 riastrad ret = -firmware_open(drvname, image_name, &handle);
78 1.1 riastrad if (ret)
79 1.1 riastrad goto fail0;
80 1.1 riastrad fw->size = firmware_get_size(handle);
81 1.1 riastrad fw->data = firmware_malloc(fw->size);
82 1.1 riastrad
83 1.1 riastrad /* XXX errno NetBSD->Linux */
84 1.1 riastrad ret = -firmware_read(handle, 0, fw->data, fw->size);
85 1.1 riastrad (void)firmware_close(handle);
86 1.1 riastrad if (ret)
87 1.1 riastrad goto fail1;
88 1.1 riastrad
89 1.1 riastrad /* Success! */
90 1.1 riastrad *fwp = fw;
91 1.1 riastrad return 0;
92 1.1 riastrad
93 1.1 riastrad fail1: firmware_free(fw->data, fw->size);
94 1.1 riastrad fail0: KASSERT(ret);
95 1.1 riastrad kmem_free(fw, sizeof(*fw));
96 1.1 riastrad *fwp = NULL;
97 1.1 riastrad return ret;
98 1.1 riastrad }
99 1.1 riastrad
100 1.1 riastrad int
101 1.1 riastrad firmware_request_nowarn(const struct firmware **fwp, const char *image_name,
102 1.1 riastrad struct device *dev)
103 1.1 riastrad {
104 1.1 riastrad
105 1.1 riastrad return request_firmware(fwp, image_name, dev);
106 1.1 riastrad }
107 1.1 riastrad
108 1.1 riastrad static void
109 1.1 riastrad request_firmware_work(struct work_struct *wk)
110 1.1 riastrad {
111 1.1 riastrad struct firmload_work *work = container_of(wk, struct firmload_work,
112 1.1 riastrad flw_work);
113 1.1 riastrad const struct firmware *fw;
114 1.1 riastrad int ret;
115 1.1 riastrad
116 1.1 riastrad /* Reqeust the firmware. If it failed, set it to NULL. */
117 1.1 riastrad ret = request_firmware(&fw, work->flw_name, work->flw_device);
118 1.1 riastrad if (ret)
119 1.1 riastrad fw = NULL;
120 1.1 riastrad
121 1.1 riastrad /* Call the callback. */
122 1.1 riastrad (*work->flw_callback)(fw, work->flw_cookie);
123 1.1 riastrad
124 1.1 riastrad /*
125 1.1 riastrad * Release the device and module references now that we're
126 1.1 riastrad * done.
127 1.1 riastrad *
128 1.1 riastrad * XXX Heh. What if the module gets unloaded _during_
129 1.1 riastrad * module_rele because it went to zero?
130 1.1 riastrad */
131 1.1 riastrad /* XXX device_release */
132 1.1 riastrad if (work->flw_module)
133 1.1 riastrad module_rele(work->flw_module);
134 1.1 riastrad }
135 1.1 riastrad
136 1.1 riastrad int
137 1.1 riastrad request_firmware_nowait(struct module *module, bool uevent, const char *name,
138 1.1 riastrad struct device *device, gfp_t gfp, void *cookie,
139 1.1 riastrad void (*callback)(const struct firmware *, void *))
140 1.1 riastrad {
141 1.1 riastrad char *namedup;
142 1.1 riastrad struct firmload_work *work;
143 1.1 riastrad
144 1.1 riastrad /* Allocate memory for it, or fail if we can't. */
145 1.1 riastrad work = kzalloc(sizeof(*work), gfp);
146 1.1 riastrad if (work == NULL)
147 1.1 riastrad goto fail0;
148 1.1 riastrad
149 1.1 riastrad /* Copy the name just in case. */
150 1.1 riastrad namedup = kstrdup(name, gfp);
151 1.1 riastrad if (namedup == NULL)
152 1.1 riastrad goto fail1;
153 1.1 riastrad
154 1.1 riastrad /* Hold the module and device so they don't go away before callback. */
155 1.1 riastrad if (module)
156 1.1 riastrad module_hold(module);
157 1.1 riastrad /* XXX device_acquire(device) */
158 1.1 riastrad
159 1.1 riastrad /* Initialize the work. */
160 1.1 riastrad work->flw_name = namedup;
161 1.1 riastrad work->flw_callback = callback;
162 1.1 riastrad work->flw_cookie = cookie;
163 1.1 riastrad work->flw_device = device;
164 1.1 riastrad work->flw_module = module;
165 1.1 riastrad INIT_WORK(&work->flw_work, request_firmware_work);
166 1.1 riastrad
167 1.1 riastrad /* Kick it off. */
168 1.1 riastrad schedule_work(&work->flw_work);
169 1.1 riastrad
170 1.1 riastrad /* Success! */
171 1.1 riastrad return 0;
172 1.1 riastrad
173 1.1 riastrad fail1: kfree(work);
174 1.1 riastrad fail0: return -ENOMEM;
175 1.1 riastrad }
176 1.1 riastrad
177 1.1 riastrad void
178 1.1 riastrad release_firmware(const struct firmware *fw)
179 1.1 riastrad {
180 1.1 riastrad
181 1.1 riastrad if (fw != NULL) {
182 1.1 riastrad firmware_free(fw->data, fw->size);
183 1.1 riastrad kmem_free(__UNCONST(fw), sizeof(*fw));
184 1.1 riastrad }
185 1.1 riastrad }
186