当前位置:首页 » 《资源分享》 » 正文

Linux杂项设备驱动_the_wan的博客

13 人参与  2022年01月07日 10:01  分类 : 《资源分享》  评论

点击全文阅读


一、概念
        杂项设备是一种特殊的字符设备,在linux2.6之后出现,与platform虚拟总线相关。通常嵌套在 platform 总线驱动中,实现复杂的驱动。其主设备号固定为10,设备注册是用主设备号10来调用register_chrdev()来实现。调用class_create()和device_create()为每个杂项设备创建设备类和设备节点。杂项设备是对字符设备驱动的再次封装,降低了字符设备驱动的难度,同时节约了主设备号资源。

二、字符设备与杂项设备实现对比

1、字符设备驱动开发流程:
1)构造flie_operations结构体,其包含字符设备操作函数的集合;
2)实现flie_operations结构体中字符设备操作函数,如open/read/write/ioctl/release等;
3)构造设备号:MKDEV(int major,int minor);
4)将设备号注册到系统:支持静态申请register_chrdev_region或动态申请alloc_chrdev_region;
5)字符设备结构体cdev初始化,建立cdev和file_operations之间的连接:cdev_init;
6)内核注册一个struct cdev结构体,cdev_add;
7)创建设备文件:
手动创建:mknod filename type major minor
自动创建:先调用class_create创建设备类,再调用device_create创建设备节点;
8)告诉内核模块入口函数与出口函数:
static int __init xxx_init(void); 
static void __exit xxx_exit(void);
9)传统的创建设备过程

alloc_chrdev_region(); /* 申请设备号 */
cdev_init(); /* 初始化 cdev */
cdev_add(); /* 添加 cdev */
class_create(); /* 创建类 */
device_create(); /* 创建设备 */

10)传统的删除设备的过程

cdev_del(); /* 删除 cdev */
unregister_chrdev_region(); /* 注销设备号 */
device_destroy(); /* 删除设备 */
class_destroy(); /* 删除类 */

2、杂项设备驱动开发流程:
1)构造flie_operations结构体,其包含字符设备操作函数的集合;
2)实现flie_operations结构体中字符设备操作函数,如open/read/write/ioctl/release等;
3)构造杂项设备驱动实体struct miscdevice;
4)在模块入口函数调用misc_register()向系统注册杂项设备;
5)在模块出口函数调用misc_deregister()从系统注销杂项设备;
6)告诉内核模块入口函数与出口函数:
static int __init xxx_init(void); 
static void __exit xxx_exit(void);


三、结构体及函数原型

1、描述杂项设备的结构体struct miscdevice
头文件:#include <linux/miscdevice.h>
struct miscdevice  {
    int minor;                                /* 次设备号 */
    const char *name;                        /* 杂项设备的名字 */
    const struct file_operations *fops;        /* 设备的操作函数集 */
    struct list_head list;                    /* 将miscdevice串成链表使用 */
    struct device *parent;                 /* 父设备(设备模型有关) */
    struct device *this_device;         /* 指向当前设备的结构体(代表自己的设备,与设备模型有关) */
};
对于miscdevice结构体主要关注前三项:
minor:如果设置为MISC_DYNAMIC_MINOR,表示系统自动分配未使用的minor;
name:设备名称;
fops:操作集合的声明,将文件操作集合结构体地址赋值给它即可;

2、杂项设备注册函数
头文件:#include <linux/miscdevice.h>
函数原型:int misc_register(struct miscdevice *misc)
参数:描述砸向设备的struct miscdevice结构体类型指针;
函数功能:将杂项设备注册到内核的设备模型中;
返回值:返回值为一个整数,如果注册成功返回0,注册失败则返回失败的错误代码。
说明:该函数内部检查次设备号是否合法,如果次设备号被占用,则返回设备忙状态;如果miscdevice的成员minor为255,则尝试动态设备请一个次设备号。当次设备号可用时,函数会将杂项设备注册到内核的设备模型中去;

3、杂项设备注销函数
头文件:#include <linux/miscdevice.h>
函数原型:int misc_deregister(struct miscdevice *misc)
参数:描述杂项设备的struct miscdevice结构体类型指针;
函数功能:将杂项设备从内核的设备模型中注销;


点击全文阅读


本文链接:http://zhangshiyu.com/post/32838.html

设备  杂项  函数  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1