背景
在使用外围模块器件时,经常有时候会进行固件的启动加载和升级,比如LCD,TP;之前的方式是把固件转换为十六进制放在代码的数组中,在需要加载固件时传入数组名即可;但是这种数组方式不够灵活,不方便后面的固件升级,也会导致内核镜像文件变大;最近发现还有一种高级的方式:使用request_firmware固件子系统,驱动在遇到需要加载固件时,使用request_firmware接口向用户空间请求文件数据。
驱动
Device Drivers --->
Generic Driver Options --->
[*] Select only drivers that don't need compile-time external firmware
[*] Prevent firmware from being built
{*} Userspace firmware loading support
[*] Include in-kernel firmware blobs in kernel binary
() External firmware blobs to build into the kernel binary
[*] Fallback user-helper invocation for firmware loading
代码
#if defined(CONFIG_FW_LOADER) || (defined(CONFIG_FW_LOADER_MODULE) && defined(MODULE))
int request_firmware(const struct firmware **fw, const char *name,
struct device *device);
int request_firmware_nowait(
struct module *module, bool uevent,
const char *name, struct device *device, gfp_t gfp, void *context,
void (*cont)(const struct firmware *fw, void *context));
int request_firmware_direct(const struct firmware **fw, const char *name,
struct device *device);
int request_firmware_into_buf(const struct firmware **firmware_p,
const char *name, struct device *device, void *buf, size_t size);
void release_firmware(const struct firmware *fw);
使用
在驱动中使用request_firmware方式,需要把驱动编译成ko模块,待跟文件系统挂载后加载模块,才能正常的加载固件;
固件需要放在跟文件系统的/lib/firmware下面,然后把文件路径传给name指针。
调试
# USB Support
CONFIG_PACKAGE_kmod-usb-core=y
CONFIG_PACKAGE_kmod-usb3=y