将驱动程序包含文件复制到 linux 文件夹

将驱动程序包含文件复制到 linux 文件夹

我正在为我的嵌入式 socfpga 系统使用 buildroot。我已经成功编写了一个驱动程序和一个应用程序来测试它,如下BR 用户手册

  • my_driver_dma :驱动程序
  • my_dma:应用程序

我遇到的问题是我必须在“my_dma.c”中包含“my_driver_dma.h”才能使用专用驱动程序函数。

我还没弄清楚如何自动找到“my_driver_dma.h”文件。为了编译它并出于测试目的,我将其复制到“my_dma”文件夹中,但这并不可靠。

我假设我必须将包含文件复制到“/build/linux-socfpga-4.16/include/linux/”文件夹之类的地方,但我不知道该怎么做。

我应该在文件中进行哪些更改才能在 my_dma 应用程序源文件“my_dma.c”中自动看到包含文件“my_driver_dma.h”?

这是我的文件:

my_apps/my_dma/Makefile
my_apps/my_dma/my_dma.c

my_drivers/my_driver_dma/Makefile
my_drivers/my_driver_dma/my_driver_dma.c
my_drivers/my_driver_dma/my_driver_dma.h

BR/package/my_apps/my_apps.mk
BR/package/my_apps/Config.in
BR/package/my_apps/my_dma/my_dma.mk
BR/package/my_apps/my_dma/Config_in

BR/package/my_drivers/my_drivers.mk
BR/package/my_drivers/Config.in
BR/package/my_drivers/my_driver_dma/my_driver_dma.mk
BR/package/my_drivers/my_driver_dma/Config_in

my_apps/my_dma/my_dma.c

#include <my_driver_dma.h>
[...]

my_apps/my_dma/Makefile

#
# sample Makefile for my_dma
#

SRC=my_dma.c
OBJ = my_dma.o
CFLAGS = -Wall -Wextra
TARGET=my_dma


# rules
all: $(TARGET)

$(TARGET): $(OBJ)
    $(CC) $^ -o $@

%.o: %.c
    $(CC) -c $(CFLAGS) $^ -o $@

.PHONY: clean
clean:
    rm -f $(OBJ)

install:
    cp my_dma $(DESTDIR)/bin/my_dma

my_drivers/my_driver_dma/Makefile

ifneq (${KERNELRELEASE},)

    obj-m  = my_driver_dma.o
else

    KERNELDIR        ?= /lib/modules/$(shell uname -r)/build
    MODULE_DIR       ?= $(shell pwd)
    ARCH             ?=
    CROSS_COMPILE    ?=
    INSTALL_MOD_PATH ?= /

all: modules

modules:
    export ARCH=$(ARCH)
    export CROSS_COMPILE=$(CROSS_COMPILE)
    ${MAKE}  -C ${KERNELDIR} SUBDIRS=${MODULE_DIR}  modules

modules_install:
    export ARCH=$(ARCH)
    export CROSS_COMPILE=$(CROSS_COMPILE)
    ${MAKE} INSTALL_MOD_PATH=${INSTALL_MOD_PATH} -C ${KERNELDIR} SUBDIRS=${MODULE_DIR}  modules_install

clean:
    rm -f *.o *.ko *.mod.c .*.o .*.ko .*.mod.c .*.cmd *~
    rm -f Module.symvers Module.markers modules.order
    rm -rf .tmp_versions
endif

BR/package/my_drivers/my_drivers.mk

include package/my_drivers/*/*.mk

BR/package/my_apps/my_apps.mk

include package/my_apps/*/*.mk

BR/package/my_apps/my_dma/my_dma.mk

MY_DMA_VERSION = 1.0.0
MY_DMA_SITE = $(TOPDIR)/my_apps/my_dma
MY_DMA_SITE_METHOD = local
MY_DMA_INSTALL_TARGET = YES
MY_DMA_INSTALL_STAGING = YES

define MY_DMA_BUILD_CMDS
    $(MAKE) CC="$(TARGET_CC)" -C $(@D) all 
endef

define MY_DMA_INSTALL_STAGING_CMDS
    $(MAKE) DESTDIR=$(STAGING_DIR) -C $(@D) install
endef

define MY_DMA_INSTALL_TARGET_CMDS
    $(INSTALL) -D -m 0755 $(@D)/my_dma $(TARGET_DIR)/usr/bin/my_dma
endef

$(eval $(generic-package))

BR/package/my_drivers/my_driver_dma/my_driver_dma.mk

MY_DRIVER_DMA_VERSION = 1.0.0
MY_DRIVER_DMA_SITE = $(TOPDIR)/my_drivers/my_driver_dma
MY_DRIVER_DMA_SITE_METHOD = local
MY_DRIVER_DMA_DEPENDENCIES = linux

define MY_DRIVER_DMA_BUILD_CMDS
    $(MAKE) -C $(@D) $(LINUX_MAKE_FLAGS) KERNELDIR=$(LINUX_DIR) modules
endef

define MY_DRIVER_DMA_INSTALL_TARGET_CMDS
    $(MAKE) -C $(@D) $(LINUX_MAKE_FLAGS) KERNELDIR=$(LINUX_DIR) modules_install
endef

$(eval $(kernel-module))
$(eval $(generic-package))

答案1

在您的 buildroot makefile (.mk) 中的 install 部分添加以下内容

define MY_DRIVER_DMA_INSTALL_TARGET_CMDS
    $(INSTALL) -D -m 644 $(@D)/my_driver_dma.h $(LINUX_DIR)/include/my_driver_dma.h
endef

这样就可以解决问题了。

相关内容