驱动模块装载全纪录模块驱动源代码demo.c如下:/************************************************************************** **my first linux driver************************************************************************** *///#ifndef_KERNEL_//#define_KERNEL_/*缂..杩..??///#endif#ifdef MODULE/*浠ユā?..寮.?璇./#include<linux/module.h>#ifdef CONFIG_DEVFS_FS#include<linux/devfs_fs_kernel.h>/*璁惧??.欢绯荤?澶存.浠?/#endif#include<linux/init.h>/*初始化相关头文件*/#include<linux/kernel.h>/*与printk()等函数有关的头文件*/#include<linux/slab.h>/*与kmalloc()等函数有关的头文件*/#include<linux/fs.h>/*与文件系统有关的头文件*/#include<linux/errno.h>/*错误代码处理头文件error codes*/#include<linux/types.h>/*数据类型头文件size_t*/#include<linux/proc_fs.h>/*与进程调度相关的头文件*/#include<linux/fcntl.h>/*O_ACCMODE*/#include<linux/poll.h>/*COPY_TO_USER*/#include<asm/system.h>/*cli(),*_flag*/#define DEVICE_NAME"ZJD demo"/*该驱动的设备名*/#define DEMORAW_MINOR1#define DEMO_Devfs_path"demo/0"/*驱动目录*///#define demo_MAJOR254/*主设备号*///#define demo_MINOR0/*次设备号*/static int demoMajor=0;static int MAX_BUF_LEN=1024;/*瀹.?涓缂..?烘.澶ч.搴?/static char drv_buf[1024];/*瀹.?涓缂..??/static int WRI_LENGTH=0;/************************************************************************名称:static void do_write()*功能:逆序排列缓冲区数据*入口参数:无*出口参数:无**********************************************************************/static void do_write(void){int i;int len=WRI_LENGTH;int mid=len>>1;char tmp;for(i=0;i<mid;i++,len--){tmp=drv_buf[len-1];drv_buf[len-1]=drv_buf[i];drv_buf[i]=tmp;}}/************************************************************************名称:demo_write()*功能:对应用户空间的write系统调用,从用户空间拷贝给定长度缓冲区数据到内核空间*入口参数:*filp操作设备文件的ID,*buffer对应用户空间的缓冲区的起始地址,count用户空间数据缓冲区长度*出口参数:返回用户空间数据缓冲区长度**********************************************************************/static ssize_t demo_write(struct file*filp,const char__user*buffer,size_t count,loff_t*ppos){if(count>MAX_BUF_LEN)count=MAX_BUF_LEN;copy_from_user(drv_buf,buffer,count);/**/WRI_LENGTH=count;//printk("user write data to driver\n");do_write();return count;}/************************************************************************名称:demo_read()*功能:对应用户空间的read系统调用,从内核空间拷贝给定长度缓冲区数据到用户空间*入口参数:*filp操作设备文件的ID,*buffer对应用户空间的缓冲区的起始地址,count用户空间数据缓冲区长度,*ppos用户在文件中进行存储操作的位置*出口参数:返回用户空间数据缓冲区长度**********************************************************************/static ssize_t demo_read(struct file*filp,char__user*buffer,size_t count,loff_t*ppos){if(count>MAX_BUF_LEN)count=MAX_BUF_LEN;icopy_to_user(buffer,drv_buf,count);/**///WRI_LENGTH=count;//printk("user read data from driver\n");//do_write();return count;}/************************************************************************名称:demo_ioctl()*功能:对应用户空间的ioctl系统调用,对用户空间传递过来的命令进行swith判断,并进行相应处理,本函数只对用户空间传递过来的1,2做简单的处理,打印一条信息,该信息可以在var/log/messages 文件尾查阅到*入口参数:*filp操作设备文件的ID,cmd对应用户空间的cmd,arg对应用户空间传递过来的参数列表*出口参数:正确返回0,错误命令返回default的提示内容**********************************************************************/static int demo_ioctl(struct inode*inode,struct file*filp,unsigned int cmd,unsigned long arg){switch(cmd){case1:printk("running command1\n");break;case2:printk("running command2\n");break;default:printk("error cmd number\n");break;}return0;}/*********************************************************************** *名称:static void demo_open()*功能:设备文件打开函数,对应用户空间open系统调用,*入口参数:设备文件节点*出口参数:无**********************************************************************/ static int demo_open(struct inode*inode,struct file*filp){//MOD_INC_USE_COUNT;//sprintf(drv_buf,"device open success!\n");printk("device open success!\n");return0;}/*********************************************************************** *名称:static void demo_release()*功能:设备文件释放函数,对应用户空间close系统调用,*入口参数:设备文件节点*出口参数:无**********************************************************************/ static int demo_release(struct inode*inode,struct file*filp){//MOD_DEC_USE_COUNT;printk("device release!\n");return0;}/*********************************************************************** *名称:demo_fops设备文件结构*功能:设备驱动文件结构体**********************************************************************/static struct file_operations demo_fops={owner:THIS_MODULE,write:demo_write,read:demo_read,ioctl:demo_ioctl,open:demo_open,release:demo_release,};/************************************************************************名称:static void demo_init()*功能:设备注册函数,通过devfs_register向设备文件系统/dev目录下注册设备,通过register_chrdev 向内核字符设备链表注册该字符设备*入口参数:无*出口参数:无**********************************************************************/static int__init demo_init(void){int ret;ret=register_chrdev(0,DEVICE_NAME,&demo_fops);if(ret<0){printk(DEVICE_NAME"can't get major number\n");return ret;}demoMajor=ret;#ifdef CONFIG_DEVFS_FSdevfs_mk_cdev(MKDEV(demoMajor,DEMORAW_MINOR),S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP,DEMO_Devfs_path);#endifreturn0;}/************************************************************************名称:static void demo_exit()*功能:设备注销函数,通过unregister_chrdev向内核字符设备链表注销该字符设备*入口参数:无*出口参数:无**********************************************************************/#ifdef MODULEvoid__exit demo_exit(void){#ifdef CONFIG_DEVFS_FSdevfs_remove(DEMO_Devfs_path);#endifunregister_chrdev(demoMajor,DEVICE_NAME);}module_exit(demo_exit);#endif/***********************************************************************/ module_init(demo_init);MODULE_LICENSE("DUAL BSD/GPL");#endif//MODULE用户测试程序test_demo.c代码如下:#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<unistd.h>#include<sys/ioctl.h>void showbuf(char*buf);int MAX_LEN=32;int main(){int fd;int i;char buf[255];for(i=0;i<MAX_LEN;i++){buf[i]=i;}fd=open("/dev/demo/0",O_RDWR);if(fd<0){printf("***********DEMO device open fail**********\n");return(-1);}printf("write%d bytes data to to/dev/mydemo/0\n",MAX_LEN);showbuf(buf);write(fd,buf,MAX_LEN);printf("read%d bytes from/dev/mydemo/0\n",MAX_LEN);read(fd,buf,MAX_LEN);showbuf(buf);close(fd);return0;}void showbuf(char*buf){int i,j=0;for(i=0;i<MAX_LEN;i++){if(i%4==0){printf("\n%4d:",j++);}printf("%4d",buf[i]);}printf("\n***********************zjd**********************************\n"); }Makefile代码如下:TARGET=test_demo#CROSS_COMPILE=arm-linux-CC=$(CROSS_COMPILE)gccSTRIP=$(CROSS_COMPILE)strip#CFLAGS=-O2ifeq($(KERNELRELEASE),)#KERNELDIR?=/up-Star2410/kernel/linux-2.6.24.4/#ARM驱动.KERNELDIR?=/usr/src/kernels/2.6.9-42.EL-smp-i686#PC驱动路径.PWD:=$(shell pwd)all:$(TARGET)modules$(TARGET):$(CC)-o$(TARGET)$(TARGET).cmodules:$(MAKE)-C$(KERNELDIR)M=$(PWD)modulesmodules_install:$(MAKE)-C$(KERNELDIR)M=$(PWD)modules_installclean:rm-rf*.o*~core.depend.*.cmd*.ko*.mod.c.tmp_versions$(TARGET) .PHONY:modules modules_install cleanelseobj-m:demo.oendif生成模块:出现,找不到头文件的错误,如下仔细研究上面的编译及出错纪录,发现编译模块,把text_demo.c也认为是模块了,一定是Makefile写错了,果然在obj-m:后面有test_demo.o,删了即不出现错误。