linux内核驱动知识三,自动创建设备文件devfs与udev

简介: Linux  2.4内核中devfs 创建设备目录:     devfs_handle_t devfs_mk_dir(devfs_handle_t dir, const char *name, void info); 创建设备文件: devfs_handle...
Linux  2.4 内核中devfs

创建设备目录:

    devfs_handle_t devfs_mk_dir(devfs_handle_t dir, const char *name, void info);

创建设备文件:

devfs_handle_t devfs_register(devfs_handle_t dir, const char * name.

 unsigned int flags, unsigned int major,

 unsigned int minor, umode_t mode,

 void *ops, void *info) ;        

    在指定的目录中创建设备文件:dir 目录名,为空表示在/dev/目录下创建;name  文件名;flags  创建标志;major 主设备号;minor 次设备号;mode创建模式;ops 操作函数集;info  通常为空

撤销设备文件:

void devfs_unregister(devfs_handlle_t de);

使用实例:

Static devfs_handle_t devfs_handle ;

 

Devfs_handle = devfs-register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, major, 0 , S_IFCHR | S_IRUSR | S_IWUSR, &XXX_fops);

 

Devfs_unregister(devfs_handle);

此方法在linux 2.6任然被支持,但是是一种过时的做法

 

 Linux  2.6内核中udev

linux 2.6.13开始,devfs不复存在,udev 成为devfs的替代品。相比devfs,udev(mdev)存在于应用层。利用udev(mdev)来实现设备文件的自动创建 很简单,在驱动代码里调用class_create 为该设备创建一个class,在为每个设备调用device_create创建对应的设备。

 

1

struct class *myclass

static struct device *dev;
myclass =  class_create(THIS_MODULE, my_device_driver);

if (IS_ERR(my_class)) {
printk("Err: failed in creating class.\n");
return ;

}

dev = device_create(myclass, NULL, MKDEV(major_num, minor_num), NULL, my_device); 

 

device_destroy(my_class, MKDEV(major_num, minor_num));
class_destroy(my_class);

 

2

现在,好使的是device_create ,从2.6.18开始可用

struct device *device_create(struct class *class, struct device *parent,
dev_t devt, const char *fmt, ...)

 

2.6.26起又多了一个参数drvdata the data to be added to the device for callbacks不会用就给个NULL.

struct device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt, ...)

 

dev=device_create(usb_class, NULL, test_usb_devno, shortname);
//2.6.26以上
// dev=device_create(usb_class, NULL, test_usb_devno, NULL,shortname);

 

  device_create( my_class, NULL, MKDEV(hello_major, 0), "hello" "%d", 0 );

如果成功,它将会在/dev目录下产生/dev/hello0设备。

 

当驱动被加载时,udev(mdev)就会自动在/dev下创建my_device设备文件。

 

#include // device_create()

 

class_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class *class_create(struct module *owner, const char *name)
    class_create - create a struct class structure
    @owner: pointer to the module that is to "own" this struct class
    @name: pointer to a string for the name of this class.
/sys/class/下创建类目录

 

class_device_create()
-------------------------------------------------
linux-2.6.22/include/linux/device.h
struct class_device *class_device_create(struct class        *cls,
                                         struct class_device *parent,
                                         dev_t               devt,
                                         struct device       *device,
                                         const  char          *fmt, ...)

    class_device_create - creates a class device and registers it with sysfs
    @cls: pointer to the struct class that this device should be registered to.
    @parent: pointer to the parent struct class_device of this new device, if any.
    @devt: the dev_t for the char device to be added.
    @device: a pointer to a struct device that is assiociated with this class device.
    @fmt: string for the class device's name

 

PS:

2.6.15中的函数:

       Class_device_create(struct class *cls,  struct class_device *parent,  dev_t devt, struct device *device, const  char *fmt, ...);

       Class_devcie_destroy(struct class *class, dev_t devt);

      

2.6.27中变为:

       Device_create(struct class *class, struct device *parent,

                          dev_t devt, void *drvdata, const char *fmt, ..);

       Device_destroy(struct class *class, dev_t devt);


相关文章
|
3月前
|
安全 网络协议 Linux
深入理解Linux内核模块:加载机制、参数传递与实战开发
本文深入解析了Linux内核模块的加载机制、参数传递方式及实战开发技巧。内容涵盖模块基础概念、加载与卸载流程、生命周期管理、参数配置方法,并通过“Hello World”模块和字符设备驱动实例,带领读者逐步掌握模块开发技能。同时,介绍了调试手段、常见问题排查、开发规范及高级特性,如内核线程、模块间通信与性能优化策略。适合希望深入理解Linux内核机制、提升系统编程能力的技术人员阅读与实践。
412 1
|
3月前
|
Ubuntu Linux
Ubuntu 23.04 用上 Linux 6.2 内核,预计下放到 22.04 LTS 版本
Linux 6.2 带来了多项内容更新,修复了 AMD 锐龙处理器设备在启用 fTPM 后的运行卡顿问题,还增强了文件系统。
|
3月前
|
Ubuntu Linux
Ubuntu 23.10 现在由Linux内核6.3提供支持
如果你想在你的个人电脑上测试一下Ubuntu 23.10的最新开发快照,你可以从官方下载服务器下载最新的每日构建ISO。然而,请记住,这是一个预发布版本,所以不要在生产机器上使用或安装它。
|
3月前
|
传感器 监控 Ubuntu
10 月发布,Ubuntu 23.10 已升级到 Linux Kernel 6.3 内核
硬件方面,Linux 6.3 引入了在 HID 中引入了原生的 Steam Deck 控制器接口,允许罗技 G923 Xbox 版赛车方向盘在 Linux 上运行;改善 8BitDo Pro 2 有线控制器的行为;并为一系列华硕 Ryzen 主板添加传感器监控。
|
2月前
|
安全 Linux 网络安全
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
97 0
Nipper 3.9.0 for Windows & Linux - 网络设备漏洞评估
|
3月前
|
数据采集 编解码 运维
一文讲完说懂 WowKey -- WowKey 是一款 Linux 类设备的命令行(CLT)运维工具
WowKey 是一款面向 Linux 类设备的命令行运维工具,支持自动登录、批量执行及标准化维护,适用于企业、团队或个人管理多台设备,显著提升运维效率与质量。
|
3月前
|
监控 Ubuntu Linux
什么Linux,Linux内核及Linux操作系统
上面只是简单的介绍了一下Linux操作系统的几个核心组件,其实Linux的整体架构要复杂的多。单纯从Linux内核的角度,它要管理CPU、内存、网卡、硬盘和输入输出等设备,因此内核本身分为进程调度,内存管理,虚拟文件系统,网络接口等4个核心子系统。
301 0
|
3月前
|
Web App开发 缓存 Rust
|
3月前
|
Ubuntu 安全 Linux
Ubuntu 发行版更新 Linux 内核,修复 17 个安全漏洞
本地攻击者可以利用上述漏洞,攻击 Ubuntu 22.10、Ubuntu 22.04、Ubuntu 20.04 LTS 发行版,导致拒绝服务(系统崩溃)或执行任意代码。
下一篇
oss云网关配置