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 */