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