Linux device driver (2) character device

Source: Internet
Author: User

Device number

The device number consists of the main device number and the secondary device number. Linux, all devices are files, all devices can find the corresponding files in the/ dev directory. These files, in addition to their names, have different device numbers for each device file;
Typically, the main device number corresponds to a type of drive device, and the secondary device number is used to drive the same type of device. such as serial port, all the serial port share a main device number, each serial port has different secondary device number.

The dev_t type is used to hold the device number (including the main device number and the secondary device number), which is actually a 32-bit integer, 12 bits to represent the main device number, and the last 20 bits to represent the secondary device number.

#define MINORBITS   20#define MINORMASK   ((1U << MINORBITS) - 1)//提取主设备号#define MAJOR(dev)  ((unsigned int) ((dev) >> MINORBITS))//提取次设备号#define MINOR(dev)  ((unsigned int) ((dev) & MINORMASK))//生成设备号#define MKDEV(ma,mi)    (((ma) << MINORBITS) | (mi))
Allocation and release of device numbers static allocation
/** * register_chrdev_region ()-Register a range ofDevice numbers * @ from: the  First inch  theDesired Range ofDevice numbers; Must include * theMajor Number. * @Count: the  Number  ofConsecutive device numbers required * @name: the name  of  theDeviceorDriver. * * Return value isZero onSuccess, a negativeErrorCode onFailure. */int Register_chrdev_region (dev_t from, unsignedCount, const char *name)

Specifies the number of device numbers to be requested from the device number from, and name in /proc/devices

Dynamic allocation
intunsignedunsignedconstchar *name)

Dynamic requests count number of device numbers starting from the secondary device number Baseminor, name in/proc/devices, and the assigned device number to the calling function via the dev pointer.

Release
voidfrom, unsigned count)
Device Registration

Character device struct CDEV

struct cdev {    struct kobject kobj;    struct module *owner;//一般初始化为THIS_MODULE    conststruct file_operations *ops;//文件操作结构体    structlist;    dev_t dev;//设备号    unsignedint count;//添加的设备个数};

Three Steps to register:
1) allocation of Cdev;
2) initialization of Cdev;
3) Add Cdev;

Distribution

Directly define the struct Cdev test_cdev;

or dynamically assigned
Truct cdev* Test_cdev;
Test_cdev = Cdev_alloc ();

Initialization
/** * cdev_init() - initialize a cdev structure * @cdev: the structure to initialize * @fops: the file_operations for this device * * Initializes @cdev, remembering @fops, making it ready to add to the * system with cdev_add(). */voidconst struct file_operations *fops)

This function has done two things:
1) The kernel fills in the contents of list and kobj in the structure itself
2) Fill in the incoming file operation structure

In general, the struct member owner is also defined manually.

Add to

Associate the CDEV structure with the device number:

int cdev_add(structunsigned count)

Parameters:
Cdev: Specifies the CDEV structure to be added;
Dev: the corresponding device number
Count: Add count devices starting from the device number dev.

The function did two things too:
1) Assign values to the two member Dev and count that are not yet populated in the CDEV structure according to the passed-in parameters.
2) Pass the CDEV structure into the kernel so that the kernel knows the corresponding device number and the specific file operation structure.

Delete
voidfrom, unsigned count)

Linux device driver (2) character device

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.