Class in the Linux Device Driver Model)

Source: Internet
Author: User

First, I want to explain that the reason why I want to study class is that it can automatically create device nodes under/dev. Of course, the class also has another function, and the udev system is also used to automatically create device nodes. udev is in the user space, its automatic creation of device nodes is also dependent on the class provided by the sysfs file system. I have a problem. If my kernel has not been transplanted to the udev system, I just use the class ), can I automatically create a device node ??? To solve this problem, I want to write this article to clarify my ideas.

A class is an advanced view of a device, which abstracts low-level implementation details. the driver can see either a SCSI disk or an ATA disk. At the class level, they are all disks. class allows the user space to use devices based on what they do, rather than how they are connected or how they work.

Almost all classes appear under/sys/class in sysfs. therefore, for example, all network interfaces can be found in/sys/class/net regardless of the interface type. the input device can be in/sys/class/input, and the serial device can be in/sys/class/tty. one exception is Block devices. For historical reasons, the exception lies in/sys/block.

Earlier linux kernels (earlier than version 2.4) did not implement a unified device model. Generally, the creation of device nodes was manually created by the mknod command or by using the devfs file system. Early Linux releases generally use manual creation to create all commonly used nodes in advance, while embedded systems use devfs. At first, the linux2.6 kernel also supports devfs, but starting from 2.6.18, the kernel completely removes the devfs system and uses the udev method to dynamically create device nodes. Therefore, the new Linux releases use udev to manage device node files.

Udev relies on all device information that is output to the user space through sysfs and is notified by/sbin/hotplug to add or remove devices. policy decision, such as the name of a device, which can be specified in the user space outside the kernel. this ensures that the naming policy is removed from the kernel and allows the flexibility of a large number of device names.

Now let's verify how the class automatically creates a device node. The Code is as follows:

Note: When reading ldd3, the class interface mentioned in the book is class_simple, for example: class_simple_create (), class_simple_destory (), class_device_create (), class_device_destory () etc. I can see that this is the kernel of linux2.6.31 without these interfaces, and these interfaces have been modified to class_create (), class_destroy (), device_create (), device_destory (). You can view the kernel source code on your own.

#include <linux/device.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/cdev.h>#include <linux/fs.h>#define DEVNAME "hello" static dev_t dev; static struct class *hello_class; static struct cdev *hello_cdev;static int hello_open(struct inode *inode,struct file *flp){return 0;}static int hello_close(struct inode *inode,struct file *flp){return 0;}static struct file_operations hello_fops={.owner=THIS_MODULE,.open=hello_open,.release=hello_close,};static int __init hello_init(void){    int error;    error = alloc_chrdev_region(&dev, 0, 2, "hello");    if (error)    {        printk("hello: alloc_chardev_region failed! ");        goto out;    }    hello_cdev = cdev_alloc();    if (hello_cdev == NULL)    {        printk("hello: alloc cdev failed! ");        error = -ENOMEM;        goto out_chrdev;    }    hello_cdev->ops = &hello_fops;    hello_cdev->owner = THIS_MODULE;    error = cdev_add(hello_cdev, dev, 1);    if (error)    {        printk("hello: cdev_add failed! ");        goto out_cdev;    }    hello_class = class_create(THIS_MODULE, DEVNAME);     if (IS_ERR(hello_class))     {         error = PTR_ERR(hello_class);         goto out_chrdev;     }     device_create(hello_class, NULL, dev, NULL, DEVNAME);     //memset (hello_buf, 0, sizeof(hello_buf));    //memcpy(hello_buf, DEFAULT_MSG, sizeof(DEFAULT_MSG));    printk("hello: Hello World! ");    return 0;out_cdev:    cdev_del(hello_cdev);out_chrdev:    unregister_chrdev_region(hello_cdev->dev, 2);out:    return error;}static void __exit hello_exit(void){    device_destroy(hello_class, dev);     class_destroy(hello_class);     unregister_chrdev_region(hello_cdev->dev, 2);    cdev_del(hello_cdev);    printk("hello: Goodbye World ");}module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("YWW");MODULE_DESCRIPTION("HELLO_CLASS_AUTO_DEV");

The Code marked red in the above Code indicates that the class automatically creates a device node. Download to the Development Board and run it. We can view the/dev directory and/sys/class/directory to see if there are Directories such as/dev/hello and/sys/class/hello_class.

After brother's verification, there will be corresponding nodes in the dev, sys/class directories. It can be noted that the udev system has not been transplanted to my file system. That is to say, the class can automatically create device nodes without udev, and the class is required for udev to automatically create nodes.

To prove my first question, I need to explore the source code of class_create () and device_create.

It is certain that the node creation in sys/class is in the registration function of struct class, and the creation of/dev/node should also be in the registration function of device.

Analysis later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.