The difference between Misc_register and Register_chrdev

Source: Internet
Author: User
Tags data structures


Registering miscellaneous character devices that use the same main device number 10
Data structures used by miscellaneous character devices

struct Miscdevice {
int minor;
const char *name;
struct File_operations *fops;
struct List_head list;
struct device *dev;
struct Class_device *class;
Char devfs_name[64];
};









Miscellaneous equipment (Misc device)
Miscellaneous equipment is also used in embedded systems more than a device driver. In the Linux kernel of the include/linux/miscdevice.h file, you have to define your own misc device from the device here. In fact, because these character devices do not meet the predefined character equipment category, all of these devices using the main device Number 10, together attributed to Misc device, in fact, Misc_register is the main device Number 10 call Register_chrdev (). In other words, the misc device is actually a special character device.



Misc_device is a special character device. Registering the driver is registered with the Misc_register function, which automatically creates the device node, which is the device file. Device files are not created without mknod directives. Because Misc_register () will call Class_device_create () or device_create ().

Character devices (char device)
When registering a character device driver with Register_chrdev (led_major,device_name,&dev_fops), if multiple devices use the function to register the driver, Led_major cannot be the same, or several devices cannot be registered (I have verified). If the module registers with this method and Led_major is 0 (automatically assigns the main device number), loading the module with the Insmod command displays the assigned master number and the secondary device number at the terminal, and establishes the node in the/dev directory, such as the device LEDs, If the module is loaded with the main device number and the secondary number 253 and 0, the node is established: Mknod LEDs C 253 0. When registering a character device driver using Register_chrdev (led_major,device_name,&dev_fops), you must manually establish the node, otherwise the device cannot be opened by the application.



------------------------------------------------------

#include <linux/miscdevice.h>//struct Miscdevice
#include <linux/fs.h>//struct file_operations

#define Kda_minor 44

static int Kda_open (struct inode *inode, struct file *file)
{
PRINTK ("%s,%d/n", __function__, __line__);
return 0;
}

static struct File_operations Kda_fops = {
. Owner = This_module,
. open = Kda_open,
};

static struct Miscdevice Kda_dev = {
Kda_minor,
"Kda",
&kda_fops
};

static int __init hello_init (void)
{
int err;

PRINTK ("<1>hello World 1./n");

Err = Misc_register (&kda_dev);
if (err) {
PRINTK ("Envctrl:unable to get Misc minor%d/n",
Kda_dev.minor);
}


return 0;
}

static void __exit hello_exit (void)
{
PRINTK (Kern_alert "Goodbye World 1./n");
Misc_deregister (&kda_dev);
}

Module_init (Hello_init);
Module_exit (Hello_exit);

Module_license ("GPL");
Module_author ("Zengxiaolong");
Module_description ("A sample Driver");
Module_supported_device ("Testdevice");

Note:
1. The above code can automatically generate the device node, but the node is in the/dev directory, not the/dev/misc directory
2.
Mkdir/dev/misc
Mknod/dev/misc/kda C 10 44
Ls/dev/kda
User-state programs, read and write/dev/kda and/dev/misc/kda are the same, equivalent to reading and writing the same 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.