引子:驱动如果夹杂在内核目录底下会比较混乱,不利于代码的管理,如果把驱动独立于内核目录以外,则会带来许多方便。通常修改的是驱动而不是内核.这样做可以把内核相对固定,驱动可以随意变化,但不会引起混乱,因为这样的拓朴结果比较合理.
//把linux驱动从内核里独立出来,单独放在一个目录drv下,内核与驱动目录的组织架构
#----|linux-2.6.x.x
# |driver---hook/Makefile
# |---------led/Makefile
# |---------Makefile 总的Makefile
./drv/Makefile内容如下
#驱动所在的总目录
export WORKSPACE=$(PWD)
#led驱动所在的目录
export PROJECT_LED=$(WORKSPACE)/led
#hook驱动所在的目录
export PROJECT_HOOK=$(WORKSPACE)/hook
.PHONY : led ledclean ledinstall hook
hookclean hookinstall driver driverclean
led:
cd $(PROJECT_LED);make -f ./Makefile
ledclean:
cd $(PROJECT_LED);make -f ./Makefile clean
ledinstall:
cd $(PROJECT_LED);make -f ./Makefile
install
hook:
cd $(PROJECT_HOOK);make -f ./Makefile
hookclean:
cd $(PROJECT_HOOK);make -f ./Makefile clean
hookinstall:
cd $(PROJECT_HOOK);make -f ./Makefile
install
#编译所有的驱动
driver:led hook
#清除所有的驱动编译的中间结果
driverclean:ledclean hookclean
#安装驱动模块到目录底下/lib/modules/2.6.12/extra/
driverinstall:ledinstall hookinstall
#路径诊断输出
diagnose:
@echo "PROJECT_HOOK=$(PROJECT_HOOK)"
@echo "PROJECT_LED=$(PROJECT_LED)"
@echo "WORKSPACE=$(WORKSPACE)"
//hook 驱动底下的Makefile
KERNEL_MAKE=-C
/home/skygoround/skygoround/czw_linux/tobacco
obj-m=xn_hook.o
xn_hook-y += xino_hook.o
default:
make $(KERNEL_MAKE) M=$(PWD) modules
clean:
make $(KERNEL_MAKE) M=$(PWD) clean
modules_install:
make $(KERNEL_MAKE) M=$(PWD)
modules_install
//led驱动底下的MakefileKERNEL_MAKE=-C
/home/skygoround/skygoround/czw_linux/tobacco
obj-m := xn_led.o
xn_led-y += xino_led.o
default:
make $(KERNEL_MAKE) M=$(PWD) modules
clean:
make $(KERNEL_MAKE) M=$(PWD) clean
modules_install:
make $(KERNEL_MAKE) M=$(PWD)
modules_install
试验测试一下:
对一些关键路径的诊断
bash-3.2$ make
diagnosePROJECT_HOOK=/home/skygoround/skygoround/czw_linux/driver/hook
PROJECT_LED=/home/skygoround/skygoround/czw_linux/driver/led
WORKSPACE=/home/skygoround/skygoround/czw_linux/driver
清除所有驱动编译的中间结果
bash-3.2$ make drivercleancd
/home/skygoround/skygoround/czw_linux/driver/led;make -f ./Makefile
clean
make[1]: Entering directory
`/home/skygoround/skygoround/czw_linux/driver/led'
make -C /home/skygoround/skygoround/czw_linux/tobacco
M=/home/skygoround/skygoround/czw_linux/driver/led clean
make[2]: Entering directory
`/home/skygoround/skygoround/czw_linux/tobacco'
CLEAN /home/skygoround/skygoround/czw_linux/driver/led/.tmp_versions
make[2]: Leaving directory
`/home/skygoround/skygoround/czw_linux/tobacco'
make[1]: Leaving directory
`/home/skygoround/skygoround/czw_linux/driver/led'
cd /home/skygoround/skygoround/czw_linux/driver/hook;make -f
./Makefile clean
make[1]: Entering directory
`/home/skygoround/skygoround/czw_linux/driver/hook'
make -C /home/skygoround/skygoround/czw_linux/tobacco
M=/home/skygoround/skygoround/czw_linux/driver/hook clean
make[2]: Entering directory
`/home/skygoround/skygoround/czw_linux/tobacco'
CLEAN /home/skygoround/skygoround/czw_linux/driver/hook/.tmp_versions
make[2]: Leaving directory
`/home/skygoround/skygoround/czw_linux/tobacco'
make[1]: Leaving directory
`/home/skygoround/skygoround/czw_linux/driver/hook'
编译所有的驱动
bash-3.2$ make driver
cd /home/skygoround/skygoround/czw_linux/driver/led;make -f
./Makefile
make[1]: Entering directory
`/home/skygoround/skygoround/czw_linux/driver/led'
make -C /home/skygoround/skygoround/czw_linux/tobacco
M=/home/skygoround/skygoround/czw_linux/driver/led modules
make[2]: Entering directory
`/home/skygoround/skygoround/czw_linux/tobacco'
CC [M] /home/skygoround/skygoround/czw_linux/driver/led/xino_led.o
LD [M] /home/skygoround/skygoround/czw_linux/driver/led/xn_led.o
Building modules, stage 2.
MODPOST
CC /home/skygoround/skygoround/czw_linux/driver/led/xn_led.mod.o
LD [M] /home/skygoround/skygoround/czw_linux/driver/led/xn_led.ko
make[2]: Leaving directory
`/home/skygoround/skygoround/czw_linux/tobacco'
make[1]: Leaving directory
`/home/skygoround/skygoround/czw_linux/driver/led'
cd /home/skygoround/skygoround/czw_linux/driver/hook;make -f
./Makefile
make[1]: Entering directory
`/home/skygoround/skygoround/czw_linux/driver/hook'
make -C /home/skygoround/skygoround/czw_linux/tobacco
M=/home/skygoround/skygoround/czw_linux/driver/hook modules
make[2]: Entering directory
`/home/skygoround/skygoround/czw_linux/tobacco'
CC [M] /home/skygoround/skygoround/czw_linux/driver/hook/xino_hook.o
LD [M] /home/skygoround/skygoround/czw_linux/driver/hook/xn_hook.o
Building modules, stage 2.
MODPOST
CC /home/skygoround/skygoround/czw_linux/driver/hook/xn_hook.mod.o
LD [M] /home/skygoround/skygoround/czw_linux/driver/hook/xn_hook.ko
make[2]: Leaving directory
`/home/skygoround/skygoround/czw_linux/tobacco'
make[1]: Leaving directory
`/home/skygoround/skygoround/czw_linux/driver/hook'
安装驱动模块
bash-3.2$ make driverinstall
cd /home/skygoround/skygoround/czw_linux/driver/led;make -f
./Makefile modules_install
make[1]: Entering directory
`/home/skygoround/skygoround/czw_linux/driver/led'
make -C
/home/skygoround/skygoround/czw_linux/tobacco
M=/home/skygoround/skygoround/czw_linux/driver/led
modules_install
make[2]: Entering directory
`/home/skygoround/skygoround/czw_linux/tobacco'
INSTALL
/home/skygoround/skygoround/czw_linux/driver/led/xn_led.ko
make[2]: Leaving directory
`/home/skygoround/skygoround/czw_linux/tobacco'
make[1]: Leaving directory
`/home/skygoround/skygoround/czw_linux/driver/led'
cd /home/skygoround/skygoround/czw_linux/driver/hook;make -f
./Makefile modules_install
make[1]: Entering directory
`/home/skygoround/skygoround/czw_linux/driver/hook'
make -C
/home/skygoround/skygoround/czw_linux/tobacco
M=/home/skygoround/skygoround/czw_linux/driver/hook
modules_install
make[2]: Entering directory
`/home/skygoround/skygoround/czw_linux/tobacco'
INSTALL
/home/skygoround/skygoround/czw_linux/driver/hook/xn_hook.ko
make[2]: Leaving directory
`/home/skygoround/skygoround/czw_linux/tobacco'
make[1]: Leaving directory
`/home/skygoround/skygoround/czw_linux/driver/hook'
bash-3.2$ cd /lib/modules/2.6.12/extra/
bash-3.2$ ls
xn_hook.ko xn_led.ko