Linux driver Learning (4)

Source: Internet
Author: User

After learning about Linux platform devices and drivers in the first three articles, this article describes how drivers automatically generate device files.

If you have written a Linux driver, you may know that a kind of device in Linux is called a Hybrid device. You can also find that you do not need to manually generate a device file after registering this type of device. Well, let's start with the registration functions of these devices to learn how to automatically generate device files. Let's take a look at the definition of the mixed device registration function in/Drivers/Char/Misc. C:

 1 int misc_register(struct miscdevice * misc) 2 { 3         struct miscdevice *c; 4         dev_t dev; 5         int err = 0; 6  7         INIT_LIST_HEAD(&misc->list); 8  9         mutex_lock(&misc_mtx);10         list_for_each_entry(c, &misc_list, list) {11                 if (c->minor == misc->minor) {12                         mutex_unlock(&misc_mtx);13                         return -EBUSY;14                 }15         }16 17         if (misc->minor == MISC_DYNAMIC_MINOR) {18                 int i = find_first_zero_bit(misc_minors, DYNAMIC_MINORS);19                 if (i >= DYNAMIC_MINORS) {20                         mutex_unlock(&misc_mtx);21                         return -EBUSY;22                 }23                 misc->minor = DYNAMIC_MINORS - i - 1;24                 set_bit(i, misc_minors);25         }26         // MISC_MAJOR = 1027         dev = MKDEV(MISC_MAJOR, misc->minor);28 29         misc->this_device = device_create(misc_class, misc->parent, dev,30                                           misc, "%s", misc->name);31         if (IS_ERR(misc->this_device)) {32                 int i = DYNAMIC_MINORS - misc->minor - 1;33                 if (i < DYNAMIC_MINORS && i >= 0)34                         clear_bit(i, misc_minors);35                 err = PTR_ERR(misc->this_device);36                 goto out;37         }38 39         /*40          * Add it to the front, so that later devices can "override"41          * earlier defaults42          */43         list_add(&misc->list, &misc_list);44  out:45         mutex_unlock(&misc_mtx);46         return err;47 }

17th ~ Row 25 is used to allocate a device number that has never been used before (the master device Number of the Hybrid device is the same); row 27th, generate a device number (consisting of the primary device number and secondary device number ). In row 3, the "xuanjicang" that automatically generates the device file is in it. See its definition in drivers/base/CORE. C:

 1 struct device *device_create(struct class *class, struct device *parent, 2                              dev_t devt, void *drvdata, const char *fmt, ...) 3 {        4         va_list vargs; 5         struct device *dev; 6  7         va_start(vargs, fmt); 8         dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs); 9         va_end(vargs);10         return dev;11 }

The key is the definition of device_create_vargs () in Row 3:

 1 struct device *device_create_vargs(struct class *class, struct device *parent, 2                                    dev_t devt, void *drvdata, const char *fmt, 3                                    va_list args) 4 { 5         struct device *dev = NULL; 6         int retval = -ENODEV; 7  8         if (class == NULL || IS_ERR(class)) 9                 goto error;10 11         dev = kzalloc(sizeof(*dev), GFP_KERNEL);12         if (!dev) {13                 retval = -ENOMEM;14                 goto error;15         }16 17         dev->devt = devt;18         dev->class = class;19         dev->parent = parent;20         dev->release = device_create_release;21         dev_set_drvdata(dev, drvdata);22 23         retval = kobject_set_name_vargs(&dev->kobj, fmt, args);24         if (retval)25                 goto error;26 27         retval = device_register(dev);28         if (retval)29                 goto error;30 31         return dev;32 33 error:34         put_device(dev);35         return ERR_PTR(retval);36 }

17th ~ Line 21, obviously, is to set the values of some members of the device variable; line 3, how is it, very familiar? I have analyzed it before, so I will not explain it again here.

 

The following summarizes how to enable the kernel to automatically generate the device file when writing the driver, that is, to call two more functions:

1. class_create ()

This function is defined in include/Linux/device. h.

2. The device_create () mentioned above ()

The key is to regard the return value of the class_create () function as the value of the first parameter of the device_create () function. The meaning of other parameters is quite clear.

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.