xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_firmware.c (revision f7058029e19ae715ad1ae05468b7086669fbbc8b)
1 /*	$NetBSD: linux_firmware.c,v 1.2 2021/12/19 12:01:04 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.2 2021/12/19 12:01:04 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
request_firmware(const struct firmware ** fwp,const char * image_name,struct device * dev)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
request_firmware_direct(const struct firmware ** fwp,const char * image_name,struct device * dev)101 request_firmware_direct(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 int
firmware_request_nowarn(const struct firmware ** fwp,const char * image_name,struct device * dev)109 firmware_request_nowarn(const struct firmware **fwp, const char *image_name,
110     struct device *dev)
111 {
112 
113 	return request_firmware(fwp, image_name, dev);
114 }
115 
116 static void
request_firmware_work(struct work_struct * wk)117 request_firmware_work(struct work_struct *wk)
118 {
119 	struct firmload_work *work = container_of(wk, struct firmload_work,
120 	    flw_work);
121 	const struct firmware *fw;
122 	int ret;
123 
124 	/* Reqeust the firmware.  If it failed, set it to NULL.  */
125 	ret = request_firmware(&fw, work->flw_name, work->flw_device);
126 	if (ret)
127 		fw = NULL;
128 
129 	/* Call the callback. */
130 	(*work->flw_callback)(fw, work->flw_cookie);
131 
132 	/*
133 	 * Release the device and module references now that we're
134 	 * done.
135 	 *
136 	 * XXX Heh. What if the module gets unloaded _during_
137 	 * module_rele because it went to zero?
138 	 */
139 	/* XXX device_release */
140 	if (work->flw_module)
141 		module_rele(work->flw_module);
142 }
143 
144 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 *))145 request_firmware_nowait(struct module *module, bool uevent, const char *name,
146     struct device *device, gfp_t gfp, void *cookie,
147     void (*callback)(const struct firmware *, void *))
148 {
149 	char *namedup;
150 	struct firmload_work *work;
151 
152 	/* Allocate memory for it, or fail if we can't.  */
153 	work = kzalloc(sizeof(*work), gfp);
154 	if (work == NULL)
155 		goto fail0;
156 
157 	/* Copy the name just in case.  */
158 	namedup = kstrdup(name, gfp);
159 	if (namedup == NULL)
160 		goto fail1;
161 
162 	/* Hold the module and device so they don't go away before callback. */
163 	if (module)
164 		module_hold(module);
165 	/* XXX device_acquire(device) */
166 
167 	/* Initialize the work.  */
168 	work->flw_name = namedup;
169 	work->flw_callback = callback;
170 	work->flw_cookie = cookie;
171 	work->flw_device = device;
172 	work->flw_module = module;
173 	INIT_WORK(&work->flw_work, request_firmware_work);
174 
175 	/* Kick it off.  */
176 	schedule_work(&work->flw_work);
177 
178 	/* Success!  */
179 	return 0;
180 
181 fail1:	kfree(work);
182 fail0:	return -ENOMEM;
183 }
184 
185 void
release_firmware(const struct firmware * fw)186 release_firmware(const struct firmware *fw)
187 {
188 
189 	if (fw != NULL) {
190 		firmware_free(fw->data, fw->size);
191 		kmem_free(__UNCONST(fw), sizeof(*fw));
192 	}
193 }
194