Linux驱动:动态加载hello world模块
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境:
主机:Ubuntu 10.04
说明:
开始做android驱动开发,所以重新温习linux下驱动编写
注意:
1.Makefile不能写成makefile
2.加载模块命令insmod ./hello.ko,卸载模块命令rmmod hello
3.insmod和rmmod需要管理员权限,如非root账户,在命令前输入sudo
4.printk输出在/var/log/messages中,用dmesg | tail -10命令输出最后10条记录
源码:
- #include <linux/init.h>
- #include <linux/module.h>
- MODULE_LICENSE("Dual BSD/GPL");
- static int hello_init(void)
- {
- printk(KERN_ALERT "Hello,jdh\n");
- return 0;
- }
- static void hello_exit(void)
- {
- printk(KERN_ALERT "Goodbye,jdh\n");
- }
- module_init(hello_init);
- module_exit(hello_exit);
Makefile:
- ifneq ($(KERNELRELEASE),)
- obj-m := hello.o
- else
- KERNELDIR ?= /lib/modules/$(shell uname -r)/build
- PWD := $(shell pwd)
- default:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
- endif
参考:
1.《LINUX设备驱动程序》第三版