Path: blob/main/sys/compat/linuxkpi/common/src/linux_firmware.c
39586 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2020-2021 The FreeBSD Foundation4* Copyright (c) 2022 Bjoern A. Zeeb5*6* This software was developed by Björn Zeeb under sponsorship from7* the FreeBSD Foundation.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/param.h>32#include <sys/kernel.h>33#include <sys/types.h>34#include <sys/malloc.h>35#include <sys/firmware.h>36#include <sys/queue.h>37#include <sys/taskqueue.h>3839#include <linux/types.h>40#include <linux/device.h>4142#include <linux/firmware.h>43#undef firmware4445MALLOC_DEFINE(M_LKPI_FW, "lkpifw", "LinuxKPI firmware");4647struct lkpi_fw_task {48/* Task and arguments for the "nowait" callback. */49struct task fw_task;50gfp_t gfp;51const char *fw_name;52struct device *dev;53void *drv;54void(*cont)(const struct linuxkpi_firmware *, void *);55};5657static int58_linuxkpi_request_firmware(const char *fw_name, const struct linuxkpi_firmware **fw,59struct device *dev, gfp_t gfp __unused, bool enoentok, bool warn)60{61const struct firmware *fbdfw;62struct linuxkpi_firmware *lfw;63const char *fwimg;64char *p;65uint32_t flags;6667if (fw_name == NULL || fw == NULL || dev == NULL) {68*fw = NULL;69return (-EINVAL);70}7172/* Set independent on "warn". To debug, bootverbose is avail. */73flags = FIRMWARE_GET_NOWARN;7475KASSERT(gfp == GFP_KERNEL, ("%s: gfp %#x\n", __func__, gfp));76lfw = malloc(sizeof(*lfw), M_LKPI_FW, M_WAITOK | M_ZERO);7778/*79* Linux can have a path in the firmware which is hard to replicate80* for auto-firmware-module-loading.81* On FreeBSD, depending on what people do, the firmware will either82* be called "fw", or "dir_fw", or "modname_dir_fw". The latter the83* driver author has to deal with herself (requesting the special name).84* We also optionally flatten '/'s and '.'s as some firmware modules do.85* We probe in the least-of-work order avoiding memory operations.86* It will be preferred to build the firmware .ko in a well matching87* way rather than adding more name-mangling-hacks here in the future88* (though we could if needed).89*/90/* (1) Try the original name. */91fbdfw = firmware_get_flags(fw_name, flags);92/* (2) Try any name removed of path, if we have not yet. */93if (fbdfw == NULL) {94fwimg = strrchr(fw_name, '/');95if (fwimg != NULL)96fwimg++;97if (fwimg == NULL || *fwimg == '\0')98fwimg = fw_name;99if (fwimg != fw_name)100fbdfw = firmware_get_flags(fwimg, flags);101}102/* (3) Flatten '/', '.' and '-' to '_' and try with adjusted name. */103if (fbdfw == NULL &&104(strchr(fw_name, '/') != NULL || strchr(fw_name, '.') != NULL ||105strchr(fw_name, '-'))) {106fwimg = strdup(fw_name, M_LKPI_FW);107if (fwimg != NULL) {108while ((p = strchr(fwimg, '/')) != NULL)109*p = '_';110fbdfw = firmware_get_flags(fwimg, flags);111if (fbdfw == NULL) {112while ((p = strchr(fwimg, '.')) != NULL)113*p = '_';114fbdfw = firmware_get_flags(fwimg, flags);115}116if (fbdfw == NULL) {117while ((p = strchr(fwimg, '-')) != NULL)118*p = '_';119fbdfw = firmware_get_flags(fwimg, flags);120}121free(__DECONST(void *, fwimg), M_LKPI_FW);122}123}124if (fbdfw == NULL) {125if (enoentok)126*fw = lfw;127else {128free(lfw, M_LKPI_FW);129*fw = NULL;130}131if (warn)132device_printf(dev->bsddev, "could not load firmware "133"image '%s'\n", fw_name);134return (-ENOENT);135}136137device_printf(dev->bsddev,"successfully loaded firmware image '%s'\n",138fw_name);139lfw->fbdfw = fbdfw;140lfw->data = (const uint8_t *)fbdfw->data;141lfw->size = fbdfw->datasize;142*fw = lfw;143return (0);144}145146static void147lkpi_fw_task(void *ctx, int pending)148{149struct lkpi_fw_task *lfwt;150const struct linuxkpi_firmware *fw;151152KASSERT(ctx != NULL && pending == 1, ("%s: lfwt %p, pending %d\n",153__func__, ctx, pending));154155lfwt = ctx;156if (lfwt->cont == NULL)157goto out;158159_linuxkpi_request_firmware(lfwt->fw_name, &fw, lfwt->dev,160lfwt->gfp, true, true);161162/*163* Linux seems to run the callback if it cannot find the firmware.164* We call it in all cases as it is the only feedback to the requester.165*/166lfwt->cont(fw, lfwt->drv);167/* Do not assume fw is still valid! */168169out:170free(lfwt, M_LKPI_FW);171}172173int174linuxkpi_request_firmware_nowait(struct module *mod __unused, bool _t __unused,175const char *fw_name, struct device *dev, gfp_t gfp, void *drv,176void(*cont)(const struct linuxkpi_firmware *, void *))177{178struct lkpi_fw_task *lfwt;179int error;180181lfwt = malloc(sizeof(*lfwt), M_LKPI_FW, M_WAITOK | M_ZERO);182lfwt->gfp = gfp;183lfwt->fw_name = fw_name;184lfwt->dev = dev;185lfwt->drv = drv;186lfwt->cont = cont;187TASK_INIT(&lfwt->fw_task, 0, lkpi_fw_task, lfwt);188error = taskqueue_enqueue(taskqueue_thread, &lfwt->fw_task);189190if (error)191return (-error);192return (0);193}194195int196linuxkpi_request_firmware(const struct linuxkpi_firmware **fw,197const char *fw_name, struct device *dev)198{199200return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,201true));202}203204int205linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **fw,206const char *fw_name, struct device *dev)207{208209return (_linuxkpi_request_firmware(fw_name, fw, dev, GFP_KERNEL, false,210false));211}212213void214linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)215{216217if (fw == NULL)218return;219220if (fw->fbdfw)221firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);222free(__DECONST(void *, fw), M_LKPI_FW);223}224225int226linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware **fw,227const char *fw_name, struct device *dev, uint8_t *buf, size_t buflen,228size_t offset)229{230const struct linuxkpi_firmware *lfw;231int error;232233error = linuxkpi_request_firmware(fw, fw_name, dev);234if (error != 0)235return (error);236237lfw = *fw;238if ((offset + buflen) >= lfw->size) {239linuxkpi_release_firmware(lfw);240return (-ERANGE);241}242243memcpy(buf, lfw->data + offset, buflen);244245return (0);246}247248249