一、设备号基础
一般来说,使用ls -l命令在时间一列的前一列的数字表示的是文件大小,但如果该文件表示的是一个设备的话,那时间一列的前一列将有两个数字,用逗号分隔开,如下图:
前一个数字表示主设备号,后一个数组表示次设备号。主设备号用来区分不同种类的设备,而次设备号用来区分同一类型的多个设备。
实际上主设备号和次设备号组合起来就是一个32位的数字,用dev_t类型表示,前12位表示主设备号,后20位表示次设备号。
可以通过MAJOR提取出主设备号,可以通过MINOR提取出次设备号,或者用MKDEV将主设备号和次设备号组合成一个dev_t类型的设备号,它们的定义见下图:
注意是在linux/kdev_t.h中定义的
在内核代码Documentation目录下的devices.txt中记录了已经使用的各个设备的主设备号和次设备号如下图:
比如/dev/mem主设备号为1,次设备号为1,这么说/dev/ram1主次设备号与/dev/mem一样,是不是就冲突了呢,事实上并没有/dev/ram1这个文件,如下图:
二、分配主设备号
方法一:静态申请
使用register_chrdev_region函数,该函数的原型如下:
/** * register_chrdev_region() - register a range of device numbers * @from: the first in the desired range of device numbers; must include * the major number. * @count: the number of consecutive device numbers required * @name: the name of the device or driver. * * Return value is zero on success, a negative error code on failure. */ int register_chrdev_region(dev_t from, unsigned count, const char *name)
功能:注册从from开始的count个设备号
参数:
from:希望注册的设备号。必须包含主设备号
count:希望注册的设备号数目
name:设备名(体现在/proc/devices)
返回值:
成功返回0,失败返回错误码
注意:这个函数注册的设备号并不是主设备号不变,次设备号增加,比如from写的是(1, (2^20 - 1)),注册两个设备,则将会注册(1, (2^20 - 1))和(2, 0)两个设备,这两个设备的主设备号是不一样的,也就是说在内核里边认为这是两个不同类型的设备。
方法二:动态分配
使用alloc_chrdev_region函数,该函数的原型如下:
/** * alloc_chrdev_region() - register a range of char device numbers * @dev: output parameter for first assigned number * @baseminor: first of the requested range of minor numbers * @count: the number of minor numbers required * @name: the name of the associated device or driver * * Allocates a range of char device numbers. The major number will be * chosen dynamically, and returned (along with the first minor number) * in @dev. Returns zero or a negative error code. */ int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
功能:申请一个范围的设备号
参数:
dev:输出参数,为申请到的第一个设备号
baseminor:起始次设备号
count:需要分配的设备号数量
mame:设备名(体现在/proc/devices)
返回值:
成功返回0,失败返回错误码
得到了设备号之后,在/proc/devices中就可以看到该设备对应的主设备号了。
三、注销设备号
使用函数unregister_chrdev_region,原型如下:
/** * unregister_chrdev_region() - unregister a range of device numbers * @from: the first in the range of numbers to unregister * @count: the number of device numbers to unregister * * This function will unregister a range of @count device numbers, * starting with @from. The caller should normally be the one who * allocated those numbers in the first place... */ void unregister_chrdev_region(dev_t from, unsigned count)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?