linux 2.6 驱动笔记(一)

本文作为linux 2.6 驱动笔记,记录环境搭建及linux基本内核模块编译加载。

环境搭建:

硬件:OK6410开发板

目标板操作系统:linux 2.6

交叉编译环境:windows 7 + vmware work station + redhat 9 + arm-gcc-linux

步骤:

编写简单内核模块,如下

#include<linux/init.h>
#include<linux/module.h>
static int hello_init(void){
    printk("hello_init");
    return 0;
}
static void hello_exit(void){
    printk("hello_exit");   
}
module_init(hello_init); //加载时调用
modeule_exit(hello_exit);//卸载时调用

编写makefile(网上现成的模板):

KERNELDIR = /home/linux-2.6.36/linux-2.6.36.2-v1.05 //交叉编译环境中的内核源码路径
PWD := $(shell pwd)
CC    = arm-linux-gcc

obj-m := driver01.o //与编译的.c文件同名

modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules //前面要有tag

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules clean

编译产生driver01.ko,拷贝至目标板(采用SD卡),执行:insmod driver01.ko,控制台打印“hello_init”。执行lsmod后显示:

备注:

1. “ error: generated/bounds.h: No such file or directory” 编译错误,makefile中的kerneldir是内核源码的地址,在编译时用于头文件的引用。bounds.h是需要linux编译后生成,因此出现该问题需要make一下linux源码;

2. "module license 'unspecified' taints kernel" 告警,原因是内核模块源码中未增加“MODULE_LICENSE("GPL");

3. arm-linux-gcc安装完后,在redhat linux中通过修改etc/profile增加环境变量,如下:

# Path manipulation
if [ `id -u` = 0 ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
    pathmunge /home/arm/usr/local/arm/4.3.2/bin 
fi

4. 在windows 7下通过ssh客户端连接vmware上的redhat时,IP用的是vmawre的虚拟网卡,而非xp时真实的网口ip。

原文地址:https://www.cnblogs.com/Fredric-2013/p/3145005.html