LDD3学习笔记(6):字符驱动4

本文介绍了Linux字符设备驱动的基础知识,包括设备号的管理和分配、关键的数据结构如struct file_operations和struct file,以及用于设备注册和注销的重要函数。此外还涉及了用户空间与内核空间之间的数据交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、快速参考

本章介绍了下面符号和头文件.structfile_operationsstructfile中的成员的列表这里不重复了.

#include<linux/types.h>

dev_t

dev_t是用来在内核里代表设备号的类型.

intMAJOR(dev_tdev);

intMINOR(dev_tdev);

从设备编号中抽取主次编号的宏.

dev_tMKDEV(unsignedintmajor,unsignedintminor);

从主次编号来建立dev_t数据项的宏定义.

#include<linux/fs.h>

"文件系统"头文件是编写设备驱动需要的头文件.许多重要的函数和数据结构在此定义.

intregister_chrdev_region(dev_tfirst,unsignedintcount,char*name)

intalloc_chrdev_region(dev_t*dev,unsignedintfirstminor,unsignedintcount,char*name)

voidunregister_chrdev_region(dev_tfirst,unsignedintcount);

允许驱动分配和释放设备编号的范围的函数.register_chrdev_region应当用在事先知道需要

的主编号时;对于动态分配,使用alloc_chrdev_region代替.

intregister_chrdev(unsignedintmajor,constchar*name,structfile_operations*fops);

老的(2.6之前)字符设备注册函数.它在2.6内核中被模拟,但是不应当给新代码使用.如果

主编号不是0,可以不变地用它;否则一个动态编号被分配给这个设备.

intunregister_chrdev(unsignedintmajor,constchar*name);

恢复一个由register_chrdev所作的注册的函数.majorname字符串必须包含之前用来注册

设备时同样的值.

structfile_operations;

structfile;

structinode;

大部分设备驱动使用的3个重要数据结构.file_operations结构持有一个字符驱动的方法;

structfile代表一个打开的文件,structinode代表磁盘上的一个文件.

#include<linux/cdev.h>

structcdev*cdev_alloc(void);

voidcdev_init(structcdev*dev,structfile_operations*fops);

intcdev_add(structcdev*dev,dev_tnum,unsignedintcount);

voidcdev_del(structcdev*dev);

cdev结构管理的函数,它代表内核中的字符设备.

#include<linux/kernel.h>

container_of(pointer,type,field);

一个传统宏定义,可用来获取一个结构指针,从它里面包含的某个其他结构的指针.

#include<asm/uaccess.h>

这个包含文件声明内核代码使用的函数来移动数据到和从用户空间.

unsignedlongcopy_from_user(void*to,constvoid*from,unsignedlongcount);

unsignedlongcopy_to_user(void*to,constvoid*from,unsignedlongcount);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值