设备驱动开发 一

来源:互联网 发布:java nginx tomcat 编辑:程序博客网 时间:2024/04/29 16:17

     字符设备驱动程序的框架:

    1,定义结构体static struct file_operations,定义设备的打开、关闭、读、写、控制函数;

    2,在结构体外分别实现结构体中定义的函数;

    3,向内核注册驱动模块。

在linux环境中编写下面程序  test.c

#include <linux/types.h> /*基本的类型定义*/
#include <linux/fs.h> /*文件系统使用相关的头文件*/
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
#include <linux/module.h>
unsigned int test_major = 0;

//这个是read函数
static int read_test(struct inode *node, struct file *file, char *buf, int count){
  int left;
  if (access_ok(VERIFY_WRITE, buf, count))
  for(left=count;left>0;left--){
    __put_user('a', buf);
    buf++;
   }
  return count;
}

//这个是write函数
static int write_test(struct inode *inode, struct file *file, const char *buf, int count){ //33
  return count;
}

//这个是open函数
static int open_test(struct inode *inode, struct file *file ){ //36
  //MOD_INC_USE_COUNT; /*模块计数加1,表示当前内核有个设备加载内核当中去*/
   try_module_get(THIS_MODULE);
   return 0;
}

//这个是release函数
static void release_test(struct inode *inode, struct file *file ){ //41
  //MOD_DEC_USE_COUNT;
  module_put(THIS_MODULE);
}

 

//这个是关键的结构体
struct file_operations test_fops=
{
 .owner    = THIS_MODULE,
 .read     = read_test,
 .write    = write_test,
 .open     = open_test,
 .release  = release_test,
};

//这个是初始化函数
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;
}


void cleanup_module(void){
  unregister_chrdev(test_major, "test");
}

  再编写一个Makefile文件:

obj-m := test.o
KERNELDIR := /usr/src/linux-headers-2.6.38-8-generic (内核所在位置)

PWD       := $(shell pwd)
default:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

 

1,执行make,产生下面的文件:

exe.c为测试驱动设备的一个测试小程序,包含读写操作。a.out是exe.c编译后的

2,将驱动程序安装到系统中:insmod -f test.ko

3,lsmod 就可以看见一个 test 的设备了。

4,cat  /proc/devices 找到test,看它在内核中注册的设备号,如下图:test的设备号是250,一定要先执行这一步,要不下一步操作完会运行测试程序会打不开设备:

5,创建设备文件:mknod /dev/test c 250 0

到此设备驱动程序框架的编写就已完成,对具体的设备驱动,再改动程序里的test_open、test_read、test_write函数即可。

 

 

 

 

 

 

原创粉丝点击