Linux character device driver cdev_init () [perfect]

Source: Internet
Author: User

1. Each character device in the kernel corresponds to a variable in the cdev structure. The following is its definition:

Linux-2.6.22/include/linux/cdev. hstruct cdev {13 struct kobject kobj; 14 struct module * owner; 15 const struct file_operations * ops; 16 struct list_head list; 17 dev_t dev; 18 unsigned int count; 19 }; 1> kobj is a kernel object embedded in this structure. It is used for general management of the data structure. 2> the owner points to the module that provides the driver. 3> ops is a set of file operations that implement specific hardware communication operations. 4> dev specifies the device number 5> count to indicate the number of slave devices associated with the device 6> list to implement a linked list, contains all inode.2. A cdev that represents the device's special files. Generally, it has two initialization Methods: Static and Dynamic 1> static memory definition initialization: struct cdev my_cdev; cdev_init (& my_cdev, & fops); my_cdev.owner = THIS_MODULE; 2> dynamic memory definition initialization: struct cdev * my_cdev = cdev_alloc (); my_cdev-> ops = & fops; my_cdev-> owner = THIS_MODULE; the functions of the two methods are the same, but the memory areas used are different, generally depending on the actual data structure requirements. 3. implementation 1> struct cdve * cdev_alloc (void) struct cdev * cdev_alloc (void) {struct cdev * p = kzarloc (sizeof (struct cdev), GFP_KERNEL ); if (p) {INIT_LIST_HEAD (& p-> list); kobject_init (& p-> kobj, & ktype_cdev_dynamic);} return p ;} 2> void cdev_init (struct cdev * cdev, const struct file_operations * fops) function void cdev_init (struct cdev * cdev, const struct file_operations * fops) {memset (cdev, 0, sizeof * cdev); IN IT_LIST_HEAD (& cdev-> list); kobject_init (& cdev-> kobj, & ktype_cdev_default); cdev-> ops = fops;} both functions are basically the same after they are completed, only cdev_init () has assigned a cdev-> ops value. The fops parameter of cdev_init contains some function pointers pointing to the function that processes the actual communication with the device. 4. After cdev is initialized, add it to the system. Therefore, you can call the cdev_add () function. The pointer to the cdev structure, the start device number, and the device number range. Int cdev_add (struct cdev * p, dev_t dev, unsigned count) {p-> dev = dev; p-> count = count; return kobj_map (cdev_map, dev, count, NULL, exact_match, exact_lock, p);} 1> the count parameter of cdev_add indicates the number of Slave Device numbers provided by the device. After cdev_add returns successfully, the device enters the active state. 2> All character devices in the kobj_map () kernel are recorded in a cdev_map variable in the kobj_map structure. The variable in this structure contains a hash to quickly access all objects. The kobj_map () function is used to save the character device number and the cdev structure variable to the cdev_map hash list. When you want to open a character device file in the future, call the kobj_lookup () function to find the cdev Structure Variable Based on the device number and retrieve the ops field. 5. when the driver of a character device is no longer needed, such as uninstalling the module, you can use the cdev_del () function to release the memory void cdev_del (struct cdev * p) occupied by cdev) {cdev_unmap (p-> dev, p-> count); kobject_put (& p-> kobj);} cdev_unmap () calls kobj_unmap () to release objects in the cdev_map hash. Kobject_put () releases the cdev structure itself.

This article from the "Yi fall Dusk" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/1082163

Related Article

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.