惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
腾讯CDC
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Hacker News
The Hacker News
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
B
Blog
IT之家
IT之家
Spread Privacy
Spread Privacy
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cisco Blogs
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
AI
AI
I
InfoQ
H
Heimdal Security Blog
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
W
WeLiveSecurity
SecWiki News
SecWiki News
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
O
OpenAI News
阮一峰的网络日志
阮一峰的网络日志
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
T
Tor Project blog
有赞技术团队
有赞技术团队
Schneier on Security
Schneier on Security
Last Week in AI
Last Week in AI

博客园 - 靖意风

使用QT 将可执行程序 进行打包 对使用的屏幕的整理 对mmc 设备进行分区 开发板启动时间优化 nmcli 设置网络配置 虚拟机下 安装 ubuntu 18.04 硬件基础知识__串口 字节对齐问题和 小端格式 在ubuntu下 编译23位的测试文件 31_了解 x6818开发板是 如何使用/dev/ttySAC0 进行打印log 10_linux dirver platform 框架 ubuntu24.04.02 下安装软件 记录 ubuntu 更新源 vmware 虚拟机的三种网卡 vmware 安装 ubuntu 24.04.02 04_kernel编程框架和 printk传参 05_使用linux内核的gpio库函数 03_uboot 命令整理 01_stm32 裸板程序 01_了解嵌入式开发
07_linux 字符设备了解
靖意风 · 2025-06-01 · via 博客园 - 靖意风

1. 创建字符设备之前,需要先申请字符设备id

int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
功能:向内核申请设备号
参数:
dev:保存内核给你分配的设备号,注意:主设备号就一个,其中的次设备号是起始的次设备号
baseminor:希望起始的次设备号,一般给0
count:次设备号的个数, 例如:count=4并且baseminor=0, 则次设备号:0,1,2,3
name:设备名称(不是设备文件名,两码事), 主要用于调试:执行cat /proc/devices能够看到申请的设备号信息

void unregister_chrdev_region(dev_t from, unsigned count)
功能:释放设备号资源
from:传递之前申请的设备号
count:次设备号的个数

2. 

对应的头文件:include/linux/fs.h
对应的源文件:fs/char_dev.c

cat /proc/devices //查看申请到的主设备号

案例:加载驱动,申请设备号,卸载驱动,释放设备号

 3. 字符设备创建和使用的接口

include/linux/fs.h
struct file_operations {
struct module *owner;
ssize_t (*read) (struct file *file, char __user *buf, size_t count, loff_t *ppos);
ssize_t (*write) (struct file *file, const char __user *buf,  size_t count,  loff_t *ppos);
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
long (*unlocked_ioctl) (struct file *file, unsigned int cmd, unsigned long buf); 

};

include/linux/cdev.h

struct cdev {};
void cdev_init(struct cdev *, const struct file_operations *);
int cdev_add (struct cdev *, dev_t, unsigned);
void cdev_del (struct cdev *);

include/linux/uaccess.h

int copy_from_user(void *to, const void __user *from, int n);
int copy_to_user(void __user *to, const void *from, int n);