加载新驱动的方法

加载新驱动的方法(以字符驱动为例)

1. 将驱动编译进内核(方法一)

将新驱动文件放入字符驱动对应的目录下,然后更改该目录下的Kconfig和Makefile文件:

  • Kconfig添加config选项,参照已有驱动格式添加。
config TELCLOCK
	tristate "Telecom clock driver for ATCA SBC"
	depends on EXPERIMENTAL && X86
	default n
	help
	  The telecom clock device is specific to the MPCBL0010 and MPCBL0050
	  ATCA computers and allows direct userspace access to the
	  configuration of the telecom clock configuration settings.  This
	  device is used for hardware synchronization across the ATCA backplane
	  fabric.  Upon loading, the driver exports a sysfs directory,
	  /sys/devices/platform/telco_clock, with a number of files for
	  controlling the behavior of this hardware.
  • Makefile添加编译项。
obj-$(CONFIG_TELCLOCK)		+= tlclk.o
  • 此时多了新驱动的模块选择选项,需要配置Make menuconfig来将宏CONFIG_TELCLOCK打开,然后新驱动模块就会加入编译选项中。

2. 将驱动编译成.ko文件(方法二)

该方法可以将驱动程序放在任意位置,但是需要在Makefile指定内核的目录路径。

Makefile如下:

KERN_DIR = /work/system/linux-xx.xx

all:
	make -C $(KERN_DIR) M=`pwd` modules 

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order

obj-m	+= My_drv.o

采用这种编译方式可以把内核驱动编译成.ko文件。
然后将.ko文件放入根文件系统,利用insmod加载命令加载驱动模块insmod My_drv.ko.,可以查看/proc/device文件浏览已加载的驱动。

猜你喜欢

转载自blog.csdn.net/qq_40179743/article/details/115123115