Linux drive device number and create device node

Source: Internet
Author: User

Device number:

1, self-allocation of the initiative

Major = Register_chrdev (0, "First_drv", &first_sdv_fops);//Register

When you register your device with the device number 0, the kernel will voluntarily assign a master device number to return.

2. Manual designation

Find the main device numbers under the Proc/devices file. Assuming it's not being used, we can take it and use it.

Application Device Node:

We will use open this function to take a device, then open ("dev/xxx") dev/xxx This device node how to come?

1, with mknod/dev/xxx C main equipment number device number

2. Create your own initiative:

Tips:

Mdev: Creating device nodes based on system information

Method:

Define two variables:

static struct class *firstdrv_class;
static struct class_devices *firstdrv_class_dev;

In the entry function, add:

Firstdrv_class = Class_create (This_module, "first_drv"), if (Is_err (Firstdrv_class)) return Ptr_err (Firstdrv_class); Firstdrv_class_dev = Device_create (Firstdrv_class,null,mkdev (major,0), NULL, "Wq_device"), if (Unlikely (IS_ERR ( Firstdrv_class_dev))) return Ptr_err (Firstdrv_class_dev);

In the Exit function, add:

Device_unregister (Firstdrv_class_dev); Class_destroy (Firstdrv_class);

This will generate a wq_device device in/dev. And will find a wq_device device under the/sys/class/first_drv folder,

There will be a dev file in/sys/class/first_drv/wq_device with the content: "252:0"

Mdev will create device nodes based on this information.

Why does Mdev create device nodes based on this information when the Sys folder is changed?

The reason is that in the RCS file in our/etc/init.d/folder there is something like the following:

Echo/sbin/mdev >/proc/sys/kernel/hotplug

Once the kernel is loaded or unloaded, the application indicated by/proc/sys/kernel/hotplug is invoked.



Linux drive device number and create device node

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.