文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>如何编写Linux操作系统下的设备驱动程序(二)

如何编写Linux操作系统下的设备驱动程序(二)

时间:2006-03-16  来源:yongchenglv

如何编写Linux操作系统下的设备驱动程序

Roy G
二.实例剖析

    我们来写一个最简单的字符设备驱动程序.虽然它什么也不做,但是通过它
可以了解Linux的设备驱动程序的工作原理.把下面的C代码输入机器,你就会
获得一个真正的设备驱动程序.不过我的kernel是2.0.34,在低版本的kernel
上可能会出现问题,我还没测试过.//xixi

#define __NO_VERSION__
#include <linux/modules.h>
#include <linux/version.h>
char kernel_version [] = UTS_RELEASE;
这一段定义了一些版本信息,虽然用处不是很大,但也必不可少.Johnsonm说所
有的驱动程序的开头都要包含<linux/config.h>,但我看倒是未必.

由于用户进程是通过设备文件同硬件打交道,对设备文件的操作方式不外乎就
是一些系统调用,如 open,read,write,close...., 注意,不是fopen, fread.,
但是如何把系统调用和驱动程序关联起来呢?这需要了解一个非常关键的数据
结构:
   struct file_operations {
int (*seek) (struct inode * ,struct file *, off_t ,int);
int (*read) (struct inode * ,struct file *, char ,int);
int (*write) (struct inode * ,struct file *, off_t ,int);
int (*readdir) (struct inode * ,struct file *, struct dirent * ,int);
int (*select) (struct inode * ,struct file *, int ,select_table *);
int (*ioctl) (struct inode * ,struct file *, unsined int ,unsigned long);
int (*mmap) (struct inode * ,struct file *, struct vm_area_struct *);
int (*open) (struct inode * ,struct file *);
int (*release) (struct inode * ,struct file *);
int (*fsync) (struct inode * ,struct file *);
int (*fasync) (struct inode * ,struct file *,int);
int (*check_media_change) (struct inode * ,struct file *);
int (*revalidate) (dev_t dev);
}

   这个结构的每一个成员的名字都对应着一个系统调用.用户进程利用系统调用
在对设备文件进行诸如read/write操作时,系统调用通过设备文件的主设备号
找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制
权交给该函数.这是linux的设备驱动程序工作的基本原理.既然是这样,则编写
设备驱动程序的主要工作就是编写子函数,并填充file_operations的各个域.
相当简单,不是吗?
下面就开始写子程序.
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
unsigned int test_major = 0;

static int read_test(struct inode *node,struct file *file, 
char *buf,int count)
{
  int left;

  if (verify_area(VERIFY_WRITE,buf,count) == -EFAULT ) 
return -EFAULT;
  for(left = count ; left > 0 ; left--)
  {
   __put_user(1,buf,1);
   buf++;
  }
  return count;
}

这个函数是为read调用准备的.当调用read时,read_test()被调用,它把用户的
缓冲区全部写1.
buf 是read调用的一个参数.它是用户进程空间的一个地址.但是在read_test
被调用时,系统进入核心态.所以不能使用buf这个地址,必须用__put_user(),
这是kernel提供的一个函数,用于向用户传送数据.另外还有很多类似功能的
函数.请参考<linux/mm.h>.在向用户空间拷贝数据之前,必须验证buf是否可用.
这就用到函数verify_area.

static int write_tibet(struct inode *inode,struct file *file,
                      const char *buf,int count)
{
   return count;
}
static int open_tibet(struct inode *inode,struct file *file )
{
   MOD_INC_USE_COUNT;
  return 0;
}
static void release_tibet(struct inode *inode,struct file *file )
{
  MOD_DEC_USE_COUNT;
}
这几个函数都是空操作.实际调用发生时什么也不做,他们仅仅为下面的结构
提供函数指针。

struct file_operations test_fops = {
    NULL,         
    read_test,
    write_test,
    NULL,          /* test_readdir */
    NULL, 
    NULL,          /* test_ioctl */
    NULL,          /* test_mmap */
    open_test,
    release_test,
    NULL,          /* test_fsync */
    NULL,          /* test_fasync */
                   /* nothing more, fill with NULLs */
};

设备驱动程序的主体可以说是写好了。现在要把驱动程序嵌入内核。驱动程序
可以按照两种方式编译。一种是编译进kernel,另一种是编译成模块(modules),
如果编译进内核的话,会增加内核的大小,还要改动内核的源文件,而且不能
动态的卸载,不利于调试,所以推荐使用模块方式。

int init_module(void)
{
    int result;

    result = register_chrdev(0, "test", &test_fops);
    if (result < 0) {
        printk(KERN_INFO "test: can't get major number\n");
        return result;
    }
    if (test_major == 0) test_major = result; /* dynamic */
    return 0;
}
在用insmod命令将编译好的模块调入内存时,init_module 函数被调用。在
这里,init_module只做了一件事,就是向系统的字符设备表登记了一个字符
设备。register_chrdev需要三个参数,参数一是希望获得的设备号,如果是
零的话,系统将选择一个没有被占用的设备号返回。参数二是设备文件名,
参数三用来登记驱动程序实际执行操作的函数的指针。
如果登记成功,返回设备的主设备号,不成功,返回一个负值。

void cleanup_module(void)
{
    unregister_chrdev(test_major, "test");
}
在用rmmod卸载模块时,cleanup_module函数被调用,它释放字符设备test
在系统字符设备表中占有的表项。

一个极其简单的字符设备可以说写好了,文件名就叫test.c吧。
下面编译
$ gcc -O2 -DMODULE -D__KERNEL__ -c test.c
得到文件test.o就是一个设备驱动程序。
如果设备驱动程序有多个文件,把每个文件按上面的命令行编译,然后
ld -r file1.o file2.o -o modulename.

驱动程序已经编译好了,现在把它安装到系统中去。
$ insmod -f test.o
如果安装成功,在/proc/devices文件中就可以看到设备test,
并可以看到它的主设备号,。
要卸载的话,运行
$ rmmod test

下一步要创建设备文件。
mknod /dev/test c major minor
c 是指字符设备,major是主设备号,就是在/proc/devices里看到的。
用shell命令
$ cat /proc/devices | awk "\\$2==\"test\" {print \\$1}"
就可以获得主设备号,可以把上面的命令行加入你的shell script中去。
minor是从设备号,设置成0就可以了。

我们现在可以通过设备文件来访问我们的驱动程序。写一个小小的测试程序。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
  int testdev;
  int  i;
  char buf[10];

  testdev = open("/dev/test",O_RDWR);
  if ( testdev == -1 )
  {
   printf("Cann't open file \n");
   exit(0);
  }
  read(testdev,buf,10);
  for (i = 0; i < 10;i++)
   printf("%d\n",buf[i]);
  close(testdev);
}
编译运行,看看是不是打印出全1 ?

以上只是一个简单的演示。真正实用的驱动程序要复杂的多,要处理如中断,
DMA,I/O port等问题。这些才是真正的难点。请看下节,实际情况的处理。
相关阅读 更多 +
排行榜 更多 +
边境检察最后区域手机版下载

边境检察最后区域手机版下载

角色扮演 下载
酋长你别跑手游下载

酋长你别跑手游下载

休闲益智 下载
心动漫画app下载官方版

心动漫画app下载官方版

浏览阅读 下载