本帖最后由 bearkey 于 2023-3-8 10:41 编辑
4. Develop Android Driver Sample
4.1 SimpleDriver Sample
4.1.1 Enter kernel driver dir and create hello dir and hello.c:
$ cd kernel/drivers$ mkdir hello$ vim hello.c
4.1.2 Start codingin hello.c: #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "hello,Iam a simple driver sample\n"); return 0; } static void hello_exit(void) { printk(KERN_ALERT "goodbye,kernel\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("Beiqi"); MODULE_DESCRIPTION("This is a simpledriver example!\n"); MODULE_ALIAS("A simplestexample"); 4.1.3 Create Makefile: obj-m := hello.o KERNELBUILD:=/lib/modules/$(shell uname -r)/build all: make -C $(KERNELBUILD)M=$(shell pwd) modules clean: rm -rf *.o *.ko *.mod.c.*.cmd *.markers *.order *.symvers .tmp_versions 4.1.4 Build module:$ make
4.1.5 install module:$ sudo insmod hello.ko
4.1.5 run to see log:$ dmesg | tail -5
4.1.6 unstall module:$ sudo rmmod hello.ko
|