Character Device Driver note-MISC Device Driver Analysis

Source: Internet
Author: User

Linux contains many Device Driver types. No matter how detailed the categories are, there will always be some network leakage. This is what we often say about "others.
In Linux, devices that cannot be classified are defined as hybrid devices (described by the miscdevice struct ). The miscdevice provided by Linux/kernel is highly inclusive. Such as NVRAM, watchdog, ds1286 and other real-time clock, character LCD, amd 768 random number generator.
Miscdevice shares a master device number misc_major (10). However, different device numbers form a linked list for all miscdevice devices. when accessing a device, the kernel searches for the corresponding miscdevice device according to the device number, then, call the file operation interface process operation registered in the file_operations struct.

Struct miscdevice {
Int minor;
Const char * Name;
Const struct file_operations * fops;
Struct list_head list;
Struct device * parent;
Struct device * this_device;
};

In essence, miscdevice is a character device, but is added with layer encapsulation. Therefore, the main task of its driver is the file_operations member function.
The following two APIs are used to register and deregister miscdevice:
Int misc_register (struct miscdevice * MISC)
Int misc_deregister (struct miscdevice * MISC)

/* What happened when the MISC device was registered? */
------------------------------------------------------------------------
Misc_register (& led_misc );
/* 1. Find a device number for us */
If (Misc-> minor = misc_dynamic_minor ){
Int I = dynamic_minors;
While (-- I> = 0)
If (misc_minors [I> 3] & (1 <(I & 7) = 0)
Break;
If (I <0 ){
Mutex_unlock (& misc_ctx );
Return-ebusy;
}
Misc-> minor = I;
}
If (Misc-> minor <dynamic_minors)
Misc_minors [MISC-> minor> 3] | = 1 <(Misc-> minor & 7 );
/* 2. Add it to the linked list */
List_add (& MISC-> list, & misc_list );

/* The process in which the MISC device registers the device driver with characters up */
-----------------------------------------------------------------------------
Static int _ init misc_init (void)
Misc_class = class_create (this_module, "Misc ");
If (register_chrdev (misc_major, "Misc", & misc_fops)/* register a character device driver with the kernel */

About the misc_fops struct:
------------------------------------------------------------------------------
Static const struct file_operations misc_fops = {
. Owner = this_module,
. Open = misc_open,
};

/* What happened when the MISC device was turned on */
------------------------------------------------------------------------------
Static int misc_open (struct inode * inode, struct file * file)
Int minor = iminor (inode);/* get the device Number */

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.