最简单的 Linux 驱动程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linsi/article/details/53348735

前些天打开以前的硬盘,翻看到以前学驱动的笔记,于是想将这些东西的一部分记录下来,以后有空可以经常翻阅。


1.编译/运行环境

    Ubuntu 14.04 LTS, Linux 3.19.0-25-generic, gcc version 4.8.4 


2.程序代码

#include <linux/init.h>
#include <linux/module.h>

static int __init hello_init(void)
{
	printk(KERN_INFO "init the hello driver\n");
	
	return 0;
}

static void __exit hello_exit(void)
{
	printk(KERN_INFO "exit hello driver\n");
}

int hello_add_integar(int a, int b)
{
	return (a+b);
}
int hello_sub_integar(int a, int b)
{
	return (a-b);
}

module_init(hello_init);
module_exit(hello_exit);

EXPORT_SYMBOL(hello_add_integar);
EXPORT_SYMBOL(hello_sub_integar);

MODULE_AUTHOR("Linsc");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION("0.0.2");
MODULE_DESCRIPTION("Linsc's hello driver.");


3.Makefile

ifneq ($(KERNELRELEASE),)
	obj-m := hello.o
else
KERNELDIR ?=/lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)
modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

clean:
	-rm -rf *.o .depend 


4.编译

make clean; make


5.查看内核打印信息

dmesg | grep "hello"


6.查看符号信息

cat /proc/kallsyms | grep hello



猜你喜欢

转载自blog.csdn.net/linsi/article/details/53348735