Linux系统的嵌入式驱动开发代码流程简单讲解

第一步:了解一个重要的数据结构

struct file_operation

{

ssize_t (*read)(......);

ssize_t(*write)(......);

int (*ioctl)(..........);

int (*open)(.......);

int (*release)(.......);

//..............

}

结构体file_operation是设备驱动程序的接口,它定义在include/linux/fs.h.

第二步:设备注册

驱动程序模块通过函数register_chrdev来完成内核的注册。该函数为:

int register_chrdev(unsigned int major,const char *name,struct file_operation *fops);

//major为主设备号,name为设备名,fops为结构体

第三步:设备卸载

驱动程序模块通过unregister_chrdev函数来完成设备的卸载。该函数:

int  unregister_chrdev(unsigned int major,const char *name);

第四步:打开/释放设备

int open(const char *pathname,int flags);

static int release(struct inode *inode,struct filp*filp);

注:*inode和*filp相当于fd(文件描述符)

第五步:读写设备

ssize_t read(............);

ssize_t write(...........);

unsigned long copy_to_user(void *to,const void *from,unsigned long count);

unsigned long copy_from_user(......);

第六步:读写以外的I/O操作

int (*iotcl)(struct innode*inode,struct file*filp,unsigned int cmd,unsigned long arg);//cmd是用户空间传递到驱动程序的命令,arg附加命令的参数


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值