通常,我们测试和使用驱动代码有两种方法:
第一,单独编译成ko文件,使用模块编译命令insmod挂载到内核中使用;
第二,编译到内核当中,和内核一起启动和加载;
这里我大致介绍一下移植和编译驱动到linux内核的方法:
一,下载需要移植的代码,并把代码复制到kernel对应的drivers/ron子目录中;
二, 修改或是新建Makefile;
## Makefile for the robin core.#obj-$(CONFIG_KEY1_BUTTON) += button_input_drv.o
三,新建或是修改Kconfig,添加该部分的编译选项;
## robin subsystem configuration#menu "rk3399 key button support"config KEY1_BUTTONtristate "KEY1_BUTTON support"---help---rk3399 key1 button gpio testhaha this is first config menu itemconfig KEY2_BUTTONtristate "KEY2_BUTTON support"---help---rk3399 key1 button gpio testhaha this is first config menu itemendmenu
四, 让编译器知道你自定义的 Makefile 和 Kconfig 在该 ron 目录上级要修改 Makefile 和 Kconfig;
Makefile
中添加一行
obj-y += robin/
Kconfig 中添加一行
menu "Device Drivers"...source "drivers/robin/Kconfig"endmenu
五,启动配置菜
make menuconfig
robin@ubuntu:~/work/kernel-rockchip-nanopi4-linux-v4.4.y$
make menuconfig
检查配置结果
vi .config
六,
编译并烧录内核、重启开发板后调试;
root@SOM-RK3399v2:~# dmesg | grep robin[linaro-7.3-2018.05 revisiond29120a424ecfbc167ef90065c0eeb7f91977701] (Linaro GCC 7.3-2018.05) ) #12 SMPSun Aug 29 02:06:34 PDT 2021[ 0.000000] psci: probing for conduit method from DT.[ 0.632480] <robin> call button_drvier_init()[ 0.633165]input: robin key1 button input dev as/devices/virtual/input/input0[ 0.633872] IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING:3<robin> callbutton_driver_probe()[ 1.325048] usb usb1: Manufacturer: Linux 4.4.179-robin ehci_hcd[ 1.387568] usb usb2: Manufacturer: Linux 4.4.179-robin ohci_hcd[ 1.992571] usb usb3: Manufacturer: Linux 4.4.179-robin xhci-hcd[ 1.998997] usb usb4: Manufacturer: Linux 4.4.179-robin xhci-hcdroot@SOM-RK3399v2:~# cat /proc/bus/input/devicesI: Bus=0000 Vendor=0100 Product=0010 Version=1001N: Name="robin key1 button input dev"P: Phys=rk3399 key1S: Sysfs=/devices/virtual/input/input0U: Uniq=H: Handlers=kbd event0 cpufreqB: PROP=0B: EV=3B: KEY=4I: Bus=0000 Vendor=0000 Product=0000 Version=0000N: Name="Typec_Headphone"P: Phys=fusb302/typecS: Sysfs=/devices/platform/ff3d0000.i2c/i2c-4/4-0022/input/input1U: Uniq=H: Handlers=event1B: PROP=0B: EV=21B: SW=4root@SOM-RK3399v2:~# cd /drv_module/root@SOM-RK3399v2:/drv_module# lsbutton_input_drv.ko button_input_testroot@SOM-RK3399v2:/drv_module# ./button_input_test /dev/input/event0[ 195.858505] <robin> call button_handler()key1 pressed