博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符设备驱动程序快速参考
阅读量:4125 次
发布时间:2019-05-25

本文共 1292 字,大约阅读时间需要 4 分钟。

管理设备号

#include<linux/types.h>
dev_t

内核中用来表示设备编号的数据类型,32位,12位用来表示主设备号,其余20位表示次设备号

int MAJOR(dev_t dev);int MINOR(dev_t dev);

这两个宏从设备编号中抽取出主、次设备号

dev_t MKDEV(unsigned int major, unsigned int minor);

这个宏由主、次设备号构造一个dev_t数据项

#include<linux/fs.h>

文件系统头文件,它是编写设备驱动程序必须的头文件,声明了很多重要的函数和结构

int register_chdev_region(dev_t first, unsigned int count, char *name);int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);void unregister_chrdev_region(dev_t first, unsigned int count);

提供给驱动程序分配和释放设备编号的函数。在期望主设备号预先知道的情况下,应调用register_chdev_region;而对于动态分配,使用 alloc_chrdev_region

struct file_operations;struct file;struct inode;

file_operations结构保存字符设备驱动程序的方法;struct file表示一个打开的文件;struct inode表示磁盘上的文件

管理字符设备

#include<linux/cdev.h>
struct cdev *cdev_alloc(void);    void cdev_init(struct cdev *dev, struct file_operations *fops);    int cdev_add(struct cdev *dev, dev_t num, unsigned int count);    void cdev_del(struct cdev *dev);

用来管理cdev数据结构的函数,内核中用该结构表示字符设备

#include<kernel.h>
container_of(pointer, type, field);

一个方便的宏,它可用于从包含在某个结构中的指针获得结构本身的指针

#include<asm/uaccess.h>

该头文件声明了在内核代码和用户空间之间移动数据的函数

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

在用户空间和内核空间之间拷贝数据

转载地址:http://mkhpi.baihongyu.com/

你可能感兴趣的文章
飞机换乘次数最少问题的两种解决方案
查看>>
有向无回路图的理解
查看>>
设计模式中英文汇总分类
查看>>
WPF实现蜘蛛纸牌游戏
查看>>
单例模式
查看>>
工厂方法模式
查看>>
模板方法模式
查看>>
数据结构之队列、栈
查看>>
数据结构之树
查看>>
数据结构之二叉树
查看>>
二叉树非递归遍历算法思悟
查看>>
红黑树算法思悟
查看>>
从山寨Spring中学习Spring IOC原理-自动装配注解
查看>>
实例区别BeanFactory和FactoryBean
查看>>
Spring后置处理器BeanPostProcessor的应用
查看>>
Spring框架的ImportSelector到底可以干嘛
查看>>
Mysql中下划线问题
查看>>
微信小程序中使用npm过程中提示:npm WARN saveError ENOENT: no such file or directory
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>