1. Environmental introduction
Operating system: ubuntu18.04 64 bit
Two, write Makefile (PC)
First enter the uname -r command to view the current system version.
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# uname -r
5.3.0-40-generic
Check whether the path of the system kernel exists:
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# ls /usr/src/
linux-headers-5.3.0-40 linux-headers-5.4.0-53-generic linux-hwe-5.4-headers-5.4.0-47
linux-headers-5.3.0-40-generic linux-hwe-5.4-headers-5.4.0-42 linux-hwe-5.4-headers-5.4.0-48
linux-headers-5.4.0-48-generic linux-hwe-5.4-headers-5.4.0-45 linux-hwe-5.4-headers-5.4.0-53
According to the current system version, determine the corresponding path: /usr/src/linux-headers-5.3.0-40
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# ls /usr/src/
linux-headers-5.3.0-40 linux-headers-5.4.0-53-generic linux-hwe-5.4-headers-5.4.0-47
linux-headers-5.3.0-40-generic linux-hwe-5.4-headers-5.4.0-42 linux-hwe-5.4-headers-5.4.0-48
linux-headers-5.4.0-48-generic linux-hwe-5.4-headers-5.4.0-45 linux-hwe-5.4-headers-5.4.0-53
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# ls /usr/src/linux-headers-5.3.0-40
arch certs Documentation fs init Kbuild kernel Makefile net scripts sound ubuntu virt
block crypto drivers include ipc Kconfig lib mm samples security tools usr
The complete Makefile is as follows:
Linux_ADD=/usr/src/linux-headers-5.3.0-40-generic
app_dev:
make -C $(Linux_ADD) M=`pwd` modules clean
make -C $(Linux_ADD) M=`pwd` modules
gcc spectrometer_app.c -o app
obj-m +=spectrometer_usb_drv.o
Compile and install test:
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# insmod spectrometer_usb_drv.ko
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# modinfo spectrometer_usb_drv.ko
filename: /mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu/spectrometer_usb_drv.ko
license: GPL
author: xiaolong
srcversion: 40F0CA1A1C1E3BA787F1B57
alias: usb:v0661p294Bd*dc*dsc*dp*ic*isc*ip*in*
alias: usb:v148Fp5370d*dc*dsc*dp*ic*isc*ip*in*
depends:
retpoline: Y
name: spectrometer_usb_drv
vermagic: 5.3.0-40-generic SMP mod_unload
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# rmmod spectrometer_usb_drv.ko
Regarding the log display problem: the driver normally uses printk to print debugging information to the terminal. If the current terminal does not respond after the driver is installed, you can first use the dmesg command to check whether there is output in the log.
root@wbyq:/mnt/hgfs/linux-share-dir/linux_c/usb_dev_ubuntu# dmesg
[137747.993110] 总线名称:usb
[137747.993111] 端点号[0]:129
[137747.993111] 端点[0] 输入端点(设备到主机)
[137747.993112] 端点[0] 设备支持批量传输.
[137747.993112] 端点[0] 传输的数据大小:512
[137747.993112] probe:dev->bulk_in_endpointAddr=129
[137747.993113] 端点号[1]:1
[137747.993113] 端点[1] 输出端点(主机到设备)
[137747.993113] 端点[1] 设备支持批量传输.
[137747.993114] 端点[1] 传输的数据大小:512
[137747.993114] probe:dev->bulk_out_endpointAddr=1
[137747.993114] 端点号[2]:2
[137747.993115] 端点[2] 输出端点(主机到设备)
[137747.993115] 端点[2] 设备支持批量传输.
[137747.993115] 端点[2] 传输的数据大小:512
[137747.993115] 端点号[3]:3
[137747.993116] 端点[3] 输出端点(主机到设备)
[137747.993116] 端点[3] 设备支持批量传输.
[137747.993116] 端点[3] 传输的数据大小:512
[137747.993116] 端点号[4]:4
[137747.993116] 端点[4] 输出端点(主机到设备)
[137747.993117] 端点[4] 设备支持批量传输.
[137747.993117] 端点[4] 传输的数据大小:512
[137747.993157] USB光谱仪设备节点注册成功:/dev/spectrometer_usb_drv ,主设备号:10,次设备号:53
[137747.993173] usbcore: registered new interface driver spectrometer_usb_drv
Three, by the way, the way to compile the Makefile for the embedded Linux driver
Linux_ADD=/home/wbyq/work/linux-3.5/linux-3.5
app_dev:
make -C $(Linux_ADD) M=`pwd` modules clean
make -C $(Linux_ADD) M=`pwd` modules
cp *.ko /home/wbyq/project/ -fv
arm-linux-gcc spectrometer_app.c -o app
cp app /home/wbyq/project/ -fv
obj-m +=spectrometer_usb_drv.o