注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 Linux全攻略--文件和磁盘管理
 帮助

linux驱动学习笔记(1)


2007-09-15 10:19:08
 标签:linux 驱动   [推送到技术圈]

驱动程序的大体框架:
 
# define  MAJOR_NUM 254定义主设备号  
    
头文件,这些常用,可以每次写驱动都加上
#include<linux/module.h>
#include<linux/config.h>
#include<linux/version.h>
#include<asm/uaccess.h>
#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 ssize_t read_test(struct file *file,char *buf,size_t count,loff_t *f_pos) {}
static ssize_t write_test(struct file *file, const char *buf, size_t count, loff_t *f_pos){}
static int open_test(struct inode *inode,struct file *file ) {}
static int release_test(struct inode *inode,struct file *file ) {}
通常还有ioctl()
 
定义结构体,给出用户函数和内核函数的对应关系:
struct file_operations test_fops = {
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");
}其中销毁设备驱动程序
 
附注
MODULE_LICENSE("GPL");
MODULE_AUTHOR("LXY");
 
这样一个简单的程序框架搭建好了


上一篇 进程环境  下一篇 进程控制



    文章评论
 
2007-09-15 21:33:22
厉害
驱动程序一向很神秘,原来是这样出来的啊

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: