1 /* $NetBSD: firmware.h,v 1.9 2018/08/27 13:40:15 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 47 struct device; 48 49 struct firmware { 50 char *data; 51 size_t size; 52 }; 53 54 struct 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 63 static inline int 64 request_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 101 fail1: firmware_free(fw->data, fw->size); 102 fail0: KASSERT(ret); 103 kmem_free(fw, sizeof(*fw)); 104 *fwp = NULL; 105 return ret; 106 } 107 108 static inline 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 static inline 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 static inline 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 187 #endif /* _LINUX_FIRMWARE_H_ */ 188