首页 > 运维 > linux运维 > 正文

Linux驱动开发: 杂项字符设备

看不見的法師
发布: 2025-07-17 08:34:01
原创
838人浏览过

一、什么是杂项设备?

杂项设备(misc device)在嵌入式系统中应用广泛,是一种特殊的设备驱动。

在Linux内核的include/linux目录下,有一个Miscdevice.h文件,定义了misc设备及其相关的内核函数。

这些字符设备因为不符合预定的字符设备范畴,所以它们使用主设备号10,并统一归为misc device。misc_register函数实际上是使用主设备号10调用register_chrdev()的。

换句话说,misc设备就是特殊的字符设备。

在Linux驱动中,无法归类的各种设备被定义为杂项设备(使用miscdevice结构体来表示)。

miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号各不相同。

所有的miscdevice设备组成一个链表,内核在访问设备时会根据次设备号查找对应的miscdevice设备,并调用其file_operations结构中注册的文件操作接口进行操作。

内核中用struct miscdevice表示miscdevice设备,并通过调用其file_operations结构中注册的文件操作接口进行操作。

miscdevice的API实现位于drivers/char/misc.c中。

Linux驱动开发: 杂项字符设备

二、描述杂项设备的结构

内核使用struct miscdevice结构体来描述杂项设备:

struct miscdevice  {
    int minor;                       //次设备号,杂项设备的主设备号为10
    const char *name;                //设备的名称
    const struct file_operations *fops;   //文件操作
    /* 下面的成员是供内核使用,驱动编写不需要理会 */
    struct list_head list;      //misc_list的链表头
    struct device *parent;      //父设备
    struct device *this_device; //当前设备,是device_create的返回值
};
登录后复制

minor:次设备号,范围为0~255,当传递255时,内核会自动分配一个可用的次设备号。

name:设备名称,用于在/dev/目录下生成设备节点名称。

fops:文件操作方法指针。

特点:安装此类驱动后,会在系统的/dev目录下生成相应的设备节点文件。

三、内核提供编写杂项设备的API函数

3.1 注册函数

函数原型 int misc_register(struct miscdevice *misc)
头文件 #include <linux/miscdevice.h>
功能 注册一个杂项设备
参数 misc - 杂项设备的核心结构指针,至少已经实现minor、name、fops三个成员
返回值 0,表示注册成功;负数,表示注册失败

3.2 注销函数

函数原型 int misc_deregister(struct miscdevice *misc)
头文件 #include <linux/miscdevice.h>
功能 注销一个已存在的杂项设备
参数 misc - 杂项设备的核心结构指针,已注册的struct miscdevice结构
返回值 0,表示注销成功;负数,表示注销失败

四、杂项设备的设备号与特征

设备号用于标识设备,分为主设备号和次设备号。杂项设备的设备号如下:

字狐AI
字狐AI

由GPT-4 驱动的AI全能助手,支持回答复杂问题、撰写邮件、阅读文章、智能搜索

字狐AI26
查看详情 字狐AI
  • 主设备号:固定为10。
  • 次设备号:0~255。

a. 主设备号固定为10。

b. 注册后会自动在/dev/目录下生成设备文件。

c. 使用一个核心结构struct miscdevice封装。

五、编写驱动程序步骤如下:

1)编写一个基本的模块代码。

2)添加设备模型所需的头文件。

3)在模块的初始化函数中注册设备对应的结构体。

4)在模块的出口函数中注销设备对应的结构。

5)根据注册函数所需的参数,反向推导出应有的结构体成员。

针对杂项模型:

1)定义一个struct miscdevice,并填充minor、fops、name成员。

2)定义一个struct file_operations,并填充需要的成员。

3)在初始化函数中调用misc_register注册上一步实现的struct miscdevice结构变量。

4)在出口函数中调用misc_deregister注销上一步实现的struct miscdevice结构变量。

5)编写一个应用程序来测试驱动是否按预期运行,并对比结果。

最核心的工作是实现file_operations的接口函数,这些函数才是真正操作硬件的代码。其余部分属于模型代码。

六、杂项设备示例

6.1. 驱动程序代码清单

/*驱动代码 misc.c */
#include <linux/module.h>       /* Needed by all modules */
#include <linux/init.h>         /* Needed for the module-macros */
#include <linux/fs.h>
#include <linux/miscdevice.h>
<p>static ssize_t misc_read (struct file <em>pfile,
char __user </em>buff,
size_t size, loff_t *off)
{
printk(KERN_EMERG "line:%d,%s is call\n",<strong>LINE</strong>,<strong>FUNCTION</strong>);
return 0;
}</p><p>static ssize_t misc_write(struct file <em>pfile,
const char __user </em>buff,
size_t size, loff_t *off)
{
printk(KERN_EMERG "line:%d,%s is call\n",<strong>LINE</strong>,<strong>FUNCTION</strong>);
return 0;
}</p><p>static int  misc_open(struct inode <em>pinode, struct file </em>pfile)
{
printk(KERN_EMERG "line:%d,%s is call\n",<strong>LINE</strong>,<strong>FUNCTION</strong>);
return 0;
}</p><p>static int misc_release(struct inode <em>pinode, struct file </em>pfile)
{
printk(KERN_EMERG "line:%d,%s is call\n",<strong>LINE</strong>,<strong>FUNCTION</strong>);
return 0;
}</p><p>//文件操作方法:根据具体设备实现需要的功能
static const  struct file_operations misc_fops = {
.read     = misc_read,
.write    = misc_write,
.release  = misc_release,
.open     = misc_open,
};</p><h1>define DEV_NAME  "abc"</h1><h1>if 1  //C99 标准 新增加的结构体初始化方法,这种可以选其中部分进行初始化,初始化顺序不限</h1><p>static struct miscdevice  misc_dev =  {
.fops  =  &misc_fops,       /<em> 设备文件操作方法 </em>/
.minor = 255,                    /<em> 次设备号 </em>/
.name  =  DEV_NAME,           /<em> 设备名/dev/下的设备节点名   </em>/
};</p><h1>else  //c89标准风格的结构体成员初始,只能按顺序初始化成员(做了解)</h1><p>static struct miscdevice  misc_dev =  {
255,              /<em> 次设备号 </em>/
DEV_NAME,         /<em> 设备名/dev/下的设备节点名   </em>/
&misc_fops        /<em> 设备文件操作方法 </em>/
};</p><h1>endif</h1><p>static int __init hello_init(void)  {
misc_register(&misc_dev);
printk(KERN_EMERG "misc init \n");
return 0;
}</p><p>static void  __exit hello_exit(void){
misc_deregister(&misc_dev);
printk(KERN_EMERG "Goodbye,misc\n");
}</p><p>module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
登录后复制

6.2. 应用程序代码

#include <stdio.h></p><h1>include <stdlib.h></h1><h1>include <string.h></h1><h1>include <unistd.h></h1><h1>include <sys/types.h></h1><h1>include <sys/stat.h></h1><h1>include <fcntl.h></h1><h1>define DEV_NAME  "/dev/abc"</h1><p>int main(void){
char buf[] = {1, 0, 0, 0};
int i = 0;
int fd;       //打开设备文件 O_RDWR,  O_RDONLY, O_WRONLY,
fd = open(DEV_NAME, O_RDWR);
if(fd < 0){
perror("open");
exit(1);
}</p><pre class="brush:php;toolbar:false;"><code>while(1){
    i++;
    if(i > 10000)
        break;
    write(fd, buf, 4);
    sleep(1);
}
close(fd);
return 0;
登录后复制

}

6.3. Makefile 代码

obj-m := misc.o
KDIR   := /home/work/linux-3.5</p><p>all:
make -C $(KDIR) M=$(PWD) modules
rm -f <em>.o </em>.mod.o <em>.mod.c </em>.symvers <em>.markers </em>.unsigned <em>.order </em>~
arm-linux-gcc app.c -o app
cp -f *.ko  app /home/work/rootfs/root</p><p>clean:
rm -f <em>.ko </em>.o <em>.mod.o </em>.mod.c <em>.symvers </em>.markers <em>.unsigned </em>.order *~
登录后复制

以上就是Linux驱动开发: 杂项字符设备的详细内容,更多请关注php中文网其它相关文章!

驱动精灵
驱动精灵

驱动精灵基于驱动之家十余年的专业数据积累,驱动支持度高,已经为数亿用户解决了各种电脑驱动问题、系统故障,是目前有效的驱动软件,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号