Linux kernel header file Cdev.h parsing

Source: Internet
Author: User



Encountered a kernel api--cdev_init to find here.



#ifndef _linux_cdev_h#define _linux_cdev_h#include <linux/kobject.h#include <linux/kdev_t.h> #include < Linux/list.h>struct file_operations;struct inode;struct module;struct cdev {        struct kobject kobj;        struct module *owner;        const struct File_operations *ops;        struct List_head list;        dev_t Dev;        unsigned int count;}; void Cdev_init (struct Cdev *, const struct file_operations *);//Initialize the character device to perform the file operation--file_operations The structure records all the operations that can be performed on the character device described by the CDEV structure Cdev *cdev_alloc (void); void Cdev_put (struct cdev *p); int cdev_add (struct Cdev * , dev_t, unsigned), void Cdev_del (struct Cdev *), void cd_forget (struct inode *), extern struct Backing_dev_info directly_ma Ppable_cdev_bdi; #endif


The author also does not write a description of the API ... Later use the other API to update ... No Zuo no die ...


I didn't expect to update it after one hours.


update:2014 July 29 Morning


Char Device Registration

As we mentioned, the kernel uses structures of typestruct Cdev to represent char devices internally. Before the kernel invokes your device ' s operations, you must allocate and registers one or more these structures.

Apply for CDEV structure first
Your code should include <linux/cdev.h>, where the structure and its associated helper functions is Defin Ed.

There is ways of allocating and initializing one of these structures. If you wish to obtain a standal one CDEV structure at runtime, you may do so with code such as:

struct Cdev *my_cdev = Cdev_alloc (); my_cdev->ops = &my_fops;
Initialize the CDEV structure body

Chances is, however, that's you'll want to embed the CDEV structure within a device-specific structure of your own; That's what scull does. In this case, you should initialize the structure, which has already allocated with:

void Cdev_init (struct cdev *cdev, struct file_operations *fops);

Either, there is one, and the other struct Cdev field, the need to initialize. Like the file_operations structure, a struct Cdev has a owner field that should is set to This_module. (Here is, for example, struct cdev* dev; dev->owener = This_module.)


After the initialization of the Cdev struct is done, the device needs to be added to the kernel, calling Cdev_add

Once The CDEV structure is set up, the final step was to tell the kernel about it with a call To:int

Cdev_add (struct Cdev *dev, dev_t num, unsigned int count);

Here,dev is the Cdev structure,num are the first device number to which this device responds, and count is the number of de Vice numbers that should is associated with the device. Often Count is one, but there was situations where it makes sense to has more than one device number correspond to a spec ific device. Consider, for example, the SCSI tape driver, which allows user space to select operating modes (such as density) by assign ing multiple minor numbers to each physical device.



attention!

There is a couple of important things to keep on mind when using Cdev_add. The first is, this, the call can fail. If it returns a negative error code, your device has not been added to the system. It almost always succeeds, however, and which brings up the other point:as soon as Cdev_add returns, your device is "live" and its operations
Can is called by the kernel. You should don't call Cdev_add until your driver are completely ready to handle operations on the device.


Call Cdev_dev without cdev the device

To remove a char device from the system, call:

void Cdev_del (struct cdev *dev);

Clearly, you should not access the CDEV structure after passing it to Cdev_del.



There are a few API useless to meet the fate of it ... Ha ha haha
















Linux kernel header file Cdev.h parsing

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.