班级和班级设备的目的是什么?(What is the purpose of class and class device?)

我跟着一些教程来解释如何编写Linux内核模块,我有点困惑。 即使阅读官方的“文件” ,我对概念的理解也很差。

创建字符设备( register_chrdev )后,我发现使用以下功能的组合很常见:

class_create

class_device_create

device_create

我无法理解,什么是类,设备和类设备和驱动程序?

其中哪一个实际负责在/proc/ ?下创建条目

I followed some tutorials that explained how to write Linux kernel modules and I am a bit confused. Even after reading the official "documentation", I have poor understanding of the concepts.

After creating a character device (register_chrdev), I see it is common to use a combination of the following functions:

class_create

class_device_create

device_create

I was not able to understand, what is a class, device and, class device and driver?

Which one of these actually responsible to create an entry under /proc/?

最满意答案

我不会进入什么课程 ,或者什么是设备 (我不是Linux内核的专家),我将如下解决这个问题。

创建字符设备后,您希望能够从用户空间访问它。 为此,您需要在/dev下添加设备节点。 你可以用两种方法来做到这一点。

使用mknod手动添加设备节点(旧)

mknod /dev/<name> c <major> <minor>

要么

使用udev

这是class_create和device_create或class_device_create (old)进来的地方。

要从内核模块通知udev ,首先使用以下命令创建一个虚拟设备类

struct class * class_create(owner, name)

现在,这个名字将出现在/sys/class/<name> 。

然后,创建一个设备并使用sysfs注册它。

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

现在,设备名称将出现在/sys/devices/virtual/<class name>/<device name>和/dev/<device name>

目前还不清楚你对/proc条目提出的问题。

加载模块后,它将出现在/proc/modules (做一个cat /proc/modules来查看它)。 而且,在分配设备编号之后,请说出

int register_chrdev_region(dev_t first, unsigned int count, char *name)

,这个名字将出现在/proc/devices (做一个cat /proc/devices来查看它)。

而且,请检查这些函数的内核源代码,因为它们提供了他们在评论中做什么的很好的描述。

好的旧LDD3不提供这些机制,但它是一个很好的来源。

Rather than going into what's a class, or what's a device (I'm no expert in Linux kernel), I will address the question as follows.

After creating the character device, you want to be able to access it from the user space. To do this, you need to add a device node under /dev. You can do this in two ways.

Use mknod to manually add a device node (old)

mknod /dev/<name> c <major> <minor>

OR

Use udev

This is where the class_create and device_create or class_device_create (old) come in.

To notify udev from your kernel module, you first create a virtual device class using

struct class * class_create(owner, name)

Now, the name will appear in /sys/class/<name>.

Then, create a device and register it with sysfs.

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

Now, device name will appear in /sys/devices/virtual/<class name>/<device name> and /dev/<device name>

It's not clear what you are asking about the /proc entry.

After your module is loaded, it will appear in /proc/modules (do a cat /proc/modules to see it). And, after you allocate the device numbers, say with

int register_chrdev_region(dev_t first, unsigned int count, char *name)

, the name will appear in /proc/devices (do a cat /proc/devices to see it).

And, please check the kernel sources for these functions as well, as they provide a good description of what they do in their comments.

The good old LDD3 does not provide these mechanisms, but it's a very good source.

班级和班级设备的目的是什么?(What is the purpose of class and class device?)

我跟着一些教程来解释如何编写Linux内核模块,我有点困惑。 即使阅读官方的“文件” ,我对概念的理解也很差。

创建字符设备( register_chrdev )后,我发现使用以下功能的组合很常见:

class_create

class_device_create

device_create

我无法理解,什么是类,设备和类设备和驱动程序?

其中哪一个实际负责在/proc/ ?下创建条目

I followed some tutorials that explained how to write Linux kernel modules and I am a bit confused. Even after reading the official "documentation", I have poor understanding of the concepts.

After creating a character device (register_chrdev), I see it is common to use a combination of the following functions:

class_create

class_device_create

device_create

I was not able to understand, what is a class, device and, class device and driver?

Which one of these actually responsible to create an entry under /proc/?

最满意答案

我不会进入什么课程 ,或者什么是设备 (我不是Linux内核的专家),我将如下解决这个问题。

创建字符设备后,您希望能够从用户空间访问它。 为此,您需要在/dev下添加设备节点。 你可以用两种方法来做到这一点。

使用mknod手动添加设备节点(旧)

mknod /dev/<name> c <major> <minor>

要么

使用udev

这是class_create和device_create或class_device_create (old)进来的地方。

要从内核模块通知udev ,首先使用以下命令创建一个虚拟设备类

struct class * class_create(owner, name)

现在,这个名字将出现在/sys/class/<name> 。

然后,创建一个设备并使用sysfs注册它。

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

现在,设备名称将出现在/sys/devices/virtual/<class name>/<device name>和/dev/<device name>

目前还不清楚你对/proc条目提出的问题。

加载模块后,它将出现在/proc/modules (做一个cat /proc/modules来查看它)。 而且,在分配设备编号之后,请说出

int register_chrdev_region(dev_t first, unsigned int count, char *name)

,这个名字将出现在/proc/devices (做一个cat /proc/devices来查看它)。

而且,请检查这些函数的内核源代码,因为它们提供了他们在评论中做什么的很好的描述。

好的旧LDD3不提供这些机制,但它是一个很好的来源。

Rather than going into what's a class, or what's a device (I'm no expert in Linux kernel), I will address the question as follows.

After creating the character device, you want to be able to access it from the user space. To do this, you need to add a device node under /dev. You can do this in two ways.

Use mknod to manually add a device node (old)

mknod /dev/<name> c <major> <minor>

OR

Use udev

This is where the class_create and device_create or class_device_create (old) come in.

To notify udev from your kernel module, you first create a virtual device class using

struct class * class_create(owner, name)

Now, the name will appear in /sys/class/<name>.

Then, create a device and register it with sysfs.

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

Now, device name will appear in /sys/devices/virtual/<class name>/<device name> and /dev/<device name>

It's not clear what you are asking about the /proc entry.

After your module is loaded, it will appear in /proc/modules (do a cat /proc/modules to see it). And, after you allocate the device numbers, say with

int register_chrdev_region(dev_t first, unsigned int count, char *name)

, the name will appear in /proc/devices (do a cat /proc/devices to see it).

And, please check the kernel sources for these functions as well, as they provide a good description of what they do in their comments.

The good old LDD3 does not provide these mechanisms, but it's a very good source.