I want to build a device driver

I want to build a device driver to run on my router which has kernel 5.10 i already write my driver code and make file ,but don't know how to cross compile for target and how to do it using openWRT? here the makefile

MODULE = ioport

ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m  := $(MODULE).o
$(MODULE)-y := ioport_interface.o

ccflags-y := -std=gnu89 -g -Wall -Wno-unused-function -Wno-declaration-after-statement

else

# normal makefile
ifeq ($(KDIR),)

DEFAULT_KDIR := /lib/modules/$(shell uname -r)/build

ERR_TEXT=export KDIR=<linux_src_dir> is empty. Using /lib/modules/$(shell uname -r)/build
$(warning $(ERR_TEXT))

KDIR := $(DEFAULT_KDIR)

endif

all:
        $(MAKE) clean
        $(MAKE) -C $(KDIR) M=$$PWD

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

endif
~                                                                                                                                                                                                          
~                                                                                                                                                                                                          
~

I've done it twice so I'm not an expert.

Look here.

It helped me to look over other kernel module packages that were similar to what I was doing.

Good luck.

EDIT: the instructions above are more for an "out of tree" kernel module. If you want your module to be "in tree" then follow the instructions for patching the kernel.

I had do a bit both in that I was importing a kmod not already existing in openwrt or the linux kernel (so I made it an out of tree kernel module package); however, I felt I needed to patch some of the "in tree" kernel headers. I suspect I might be able to get away with just copying the "in tree" kernel headers to my "out of tree" kernel module package and avoid patching anything in the kernel.

2 Likes