RPM: Cdev_alloc and Cdev_add in the Linux kernel

Source: Internet
Author: User

Each character in the kernelThe device corresponds to a variable of the CDEV structure, and here is its definition:
Linux-2.6.22/include/linux/cdev.h
struct Cdev {
struct Kobject kobj; Each cdev is a kobject
structModule *owner; Point to implementDriven byModule
const struct File_operations *ops; Manipulate this character deviceMethod of the file
struct List_head list; Inode->i_devices of the Cdev corresponding to the character device file
dev_t Dev; Start Device number
unsigned int count; Device Range Number Size
};
A cdev generally it has two ways of defining initialization: static and dynamic.
StaticMemory Definition initialization:
struct Cdev My_cdev;
Cdev_init (&my_cdev, &fops);
My_cdev.owner = This_module;
Dynamic memory definition initialization:
struct Cdev *my_cdev = Cdev_alloc ();
My_cdev->ops = &fops;
My_cdev->owner = This_module;

Two ways of usingfunction is the same, but the use of the memory area is not the same, generally depending on the actual data structure requirements.
The code for two functions is posted below to see the difference between them.
struct Cdev *cdev_alloc (void)
{
struct Cdev *p = kzalloc (sizeof (struct cdev), gfp_kernel);
if (p) {
Init_list_head (&p->list);
Kobject_init (&p->kobj, &ktype_cdev_dynamic);
}
return p;
}
void Cdev_init (struct cdev *cdev, const struct file_operations *fops)
{
memset (cdev, 0, sizeof *cdev); Note 1;
Init_list_head (&cdev->list);
Kobject_init (&cdev->kobj, &ktype_cdev_default);
Cdev->ops = FoPs;
}
Thus, the function of two functions is basically consistent, but Cdev_init () also assigns a cdev->ops value.
After initializing the Cdev, you need to add it to theSystem. You can call the Cdev_add () function for this purpose. The pointer to the CDEV structure, the starting 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);
}
About the Kobj_map () function is not expanded, I just roughly describe its principle. All character devices in the kernel are recorded in the Cdev_map variable of a KOBJ_MAP structure. The variable in this structure contains a hash table for quick access to all objects. The Kobj_map () function is used to save the character device number and the CDEV structure variable to the CDEV_MAP hash table. When you subsequently open a character device file, by calling the Kobj_lookup () function, you can find the CDEV structure variable based on the device number to remove the Ops field from it.
When a character device driver is no longer needed (such as a module unload), you can use the Cdev_del () function to release the memory that Cdev occupies.
void Cdev_del (struct Cdev *p)
{
Cdev_unmap (P->dev, P->count);
Kobject_put (&p->kobj);
}
where Cdev_unmap () calls Kobj_unmap () to release objects from the Cdev_map hash list. Kobject_put () releases the CDEV structure itself.

Note 1:
Memset is used to complete a memory spaceSet to a character, typically used to initialize a defined string to ' or ';
Example: Char a[100];memset (A, ' n ', sizeof (a));
memcpy is used to make a memory copy, you can copy any data type object, you can specify the length of the copied data.
Example: Char a[100],b[50]; memcpy (b, A, sizeof (b)); Note that using sizeof (a) will cause the memory address of B to overflow.
Strcpy can only copy a string, it encounters ' "" to end the copy.
Example: Char a[100],b[50];strcpy (A, b), as with strcpy (B,a), it is important to note that the string length in a (before the first ' \ ") is more than 50 bits, which, if exceeded, will cause the memory address of B to overflow.

The main application of memset is to initialize a memory space.
memcpy is the data used to copy the source space into the destination space.

strcpy is used for string copy, and will end when it encounters '/'.

This article comes from Chinaunix blog, if you look at the original point:http://blog.chinaunix.net/u3/101356/showart_2050054.html

RPM: Cdev_alloc and Cdev_add in the Linux kernel

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.