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);


目录
相关文章
|
9天前
|
Linux
linux中查看某个文件夹下文件的个数和大小
这篇文章介绍了在Linux系统中使用各种命令(如`stat`、`wc`、`du`和`ls`)来查看文件夹下文件的个数和大小的方法。
42 4
linux中查看某个文件夹下文件的个数和大小
|
4天前
|
安全 Linux 数据安全/隐私保护
探索Linux操作系统的文件权限管理
【9月更文挑战第29天】在数字世界中,文件权限管理如同保护我们隐私的锁。本文将带你了解如何在Linux系统中设置和管理文件权限,确保你的数据安全。我们将一起学习如何通过命令行工具来控制文件访问,就像学习一门新语言一样有趣。准备好了吗?让我们一起开启这场技术之旅!
|
6天前
|
Linux
深入理解Linux中的cp命令:文件与目录的复制利器
深入理解Linux中的cp命令:文件与目录的复制利器
|
6天前
|
Linux Shell
10-9|linux上统计文件中单词次数
10-9|linux上统计文件中单词次数
|
7天前
|
存储 Linux Shell
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
这篇文章介绍了Vim编辑器的基本使用、常用命令和模式,以及Linux系统中用户和组的管理方法,包括用户和组相关文件如/etc/passwd、/etc/shadow和/etc/group的说明。
常用vim命令和vim基本使用及Linux用户的管理,用户和组相关文件
|
22天前
|
存储 安全 Linux
探索Linux操作系统的心脏:内核
在这篇文章中,我们将深入探讨Linux操作系统的核心—内核。通过简单易懂的语言和比喻,我们会发现内核是如何像心脏一样为系统提供动力,处理数据,并保持一切顺畅运行。从文件系统的管理到进程调度,再到设备驱动,我们将一探究竟,看看内核是怎样支撑起整个操作系统的大厦。无论你是计算机新手还是资深用户,这篇文章都将带你领略Linux内核的魅力,让你对这台复杂机器的内部运作有一个清晰的认识。
51 3
|
1月前
|
Ubuntu Linux Shell
Linux系统命令 安装和文件相关命令
本文档详细介绍了Linux系统中的常用命令,包括软件安装卸载命令如`dpkg`和`apt-get`,压缩与解压命令如`gzip`、`bzip2`和`xz`,以及`tar`命令用于打包和解包。此外还介绍了文件分割命令`split`,文件操作命令如`cat`、`head`、`tail`、`more`、`less`等,管道命令和`wc`、`grep`、`find`、`cut`、`sort`、`uniq`、`diff`等实用工具。最后,文档还讲解了文件属性相关的命令如`chmod`、`chown`、`chgrp`以及创建硬链接和软链接的`ln`命令。
|
1月前
|
网络协议 Ubuntu Linux
用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核
用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核
|
1月前
|
Linux
用clang编译Linux内核
用clang编译Linux内核
|
1月前
|
Linux API 调度
关于在Linux内核中使用不同延迟/休眠机制 【ChatGPT】
关于在Linux内核中使用不同延迟/休眠机制 【ChatGPT】
下一篇
无影云桌面