一。 make menuconfig 打开 Enable Enable loadable module support
--------------------------------------------------------------------------------------------
+----------------------------- Linux Kernel Configuration ------------------------------+
| Arrow keys navigate the menu. <Enter> selects submenus --->. Highlighted letters |
| are hotkeys. Pressing <Y> includes, <N> excludes, <M> modularizes features. Press |
| <Esc><Esc> to exit, <?> for Help, </> for Search. Legend: [*] built-in [ ] |
| excluded <M> module < > module capable |
| +-----------------------------------------------------------------------------------+ |
| | General setup ---> | |
| | [*] Enable loadable module support ---> | |
| | -*- Enable the block layer ---> | |
| | Linux Kernel Study & Practice ---> | |
同时在下级菜单使能模块卸载,否则卸载的时候会报错 rmmod: delete_module 'hello' failed (errno 38)
| --- Enable loadable module support | |
| | [ ] Forced module loading | |
| | [*] Module unloading | |
| | [ ] Forced module unloading (NEW) | |
| | [ ] Module versioning support | |
| | [ ] Source checksum for all modules
二。把对应源码的config配置修改为模块方式
Kconfig文件:
config HELLO
tristate "First Android Driver"
default n
help
This is the first android driver.
menuconfig设为M ,模块方式
+-----------------------------------------------------------------------------------+ |
| | --- kernel study start support | |
| | <M> First Android Driver
Makefile 文件
obj-$(CONFIG_HELLO) += hello.o
三 编译模块
先 make
再 make modules
如果最后连接报错,有些系统接口找不到,是应该依赖模块的obj文件还没有生成, 那就先make 一下再make modules
四 加载模块
使用adb工具 push 到 目标系统
# adb remount /*解决只读问题*/
# adb push hello.ko /system/ /*推送模块到目标板*/
# insmod /system/hello.ko /*加载模块到内核*/
# lsmod /*显示已加载的模块*/
# root@android:/ # lsmod
# hello 4964 0 - Live 0xbf000000
五 卸载模块
root@android:/ # rmmod hello /*卸载模块*/
root@android:/ #