firmware.h revision 1.10
1/* $NetBSD: firmware.h,v 1.10 2021/12/19 10:50:37 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#ifndef _LINUX_FIRMWARE_H_ 33#define _LINUX_FIRMWARE_H_ 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 <dev/firmload.h> 42 43#include <linux/slab.h> 44#include <linux/string.h> 45#include <linux/workqueue.h> 46 47struct device; 48 49struct firmware { 50 char *data; 51 size_t size; 52}; 53 54struct firmload_work { 55 char *flw_name; 56 void (*flw_callback)(const struct firmware *, void *); 57 void *flw_cookie; 58 struct device *flw_device; 59 struct module *flw_module; 60 struct work_struct flw_work; 61}; 62 63static inline int 64request_firmware(const struct firmware **fwp, const char *image_name, 65 struct device *dev) 66{ 67 const char *drvname; 68 struct firmware *fw; 69 firmware_handle_t handle; 70 int ret; 71 72 fw = kmem_alloc(sizeof(*fw), KM_SLEEP); 73 74 /* 75 * If driver xyz(4) asks for xyz/foo/bar.bin, turn that into 76 * just foo/bar.bin. This leaves open the possibility of name 77 * collisions. Let's hope upstream is sensible about this. 78 */ 79 drvname = device_cfdriver(dev)->cd_name; 80 if ((strncmp(drvname, image_name, strlen(drvname)) == 0) && 81 (image_name[strlen(drvname)] == '/')) 82 image_name += (strlen(drvname) + 1); 83 84 /* XXX errno NetBSD->Linux */ 85 ret = -firmware_open(drvname, image_name, &handle); 86 if (ret) 87 goto fail0; 88 fw->size = firmware_get_size(handle); 89 fw->data = firmware_malloc(fw->size); 90 91 /* XXX errno NetBSD->Linux */ 92 ret = -firmware_read(handle, 0, fw->data, fw->size); 93 (void)firmware_close(handle); 94 if (ret) 95 goto fail1; 96 97 /* Success! */ 98 *fwp = fw; 99 return 0; 100 101fail1: firmware_free(fw->data, fw->size); 102fail0: KASSERT(ret); 103 kmem_free(fw, sizeof(*fw)); 104 *fwp = NULL; 105 return ret; 106} 107 108static inline int 109firmware_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 116static inline void 117request_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 144static inline int 145request_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 181fail1: kfree(work); 182fail0: return -ENOMEM; 183} 184 185static inline void 186release_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 195#endif /* _LINUX_FIRMWARE_H_ */ 196