Use mdev to automatically create Device File nodes in Embedded Linux

Source: Internet
Author: User
Use mdev to automatically create Device File nodes in Embedded Linux

Mdev is a simplified version of udev that comes with busybox. It automatically generates the node files required by the driver when the system starts and hot swapping or dynamically loads the driver, the device nodes in the/dev directory in the file system are all created by mdev.
Mdev scans all the class device directories in/sys/class and/sys/block. If the directory contains a file named "Dev" and the file contains a device number, then mdev uses this information to create a device node for the device under/dev.

To use mdev to automatically create a device file node, perform the following steps:

(1) Add mdev support during compilation.

Linux system utilities --->

Mdev

Support/etc/mdev. conf

Support Command Execution At device addition/removal

(2) Add the mdev command at startup.

Add the following startup command to the start script file's RC file to mount the Sys File System and tmpfs file system, and start the mdev application.

/Bin/Mount-n-t tmpfs/dev/SHM
/Bin/Mount-n-t sysfs NONE/sys


Echo/sbin/mdev>/proc/sys/kernel/hotplug
/Sbin/mdev-S

/Bin/hotplug

 

(3) add support for Class Device interfaces to the device driver.

In the driver's device detection function probe, you can use the following similar statements to add an attribute file named "Dev" containing the device number in the category Device directory, and use mdev to generate corresponding device files in the/dev directory, for example:

# Define device_name "leds0"

Mixed_class = class_create (this_module, device_name); // register the device class

 

Supplement:

There are two methods to create a device file, namely the mknod command used during file system creation. The command is in the following format:

Mknod filename (type, C, B, L) device No.

The type indicates the type of the device (character Device C, block Device B, socket L). The master device number is used to determine the type of the device, the next device number is used to determine a device in this type of device www.it165.net.

For example, mknod memdev0 C 555 0 creates a character device with the master device Number 555 and the secondary device number 0.

 

2. dynamically adding a device file node to the driver will reduce the trouble.

The specific implementation mainly includes two processes.

1. Create a device Class Based on the class_create () function.

2. Create a device file based on the device class, mainly based on device_create ()

The basic implementation process should be to create a device class after the device number is obtained during device driver initialization, and implement it using class_create.

Class_create (owner, name)

The meaning of the parameter: Owner refers to the owner of the device. Therefore, you can directly copy this_module to the owner, and name indicates the name of the device class. The returned value is a pointer of a device class. In this way, a device class is created.

 

After the device is added to the kernel, create a device file based on the device class and implement the function based on device_create. The function form is as follows.

Struct device * device_create (struct class * class, struct device * parent, dev_t devt, void * drvdata, const char * FMT ,...)

The parameters indicate the device pointer, the device's parent device, the device number, the device data, and the name of the device file. The parameters are optional.

We also need to release the allocated resources when the device exits. Use device_destroy to release the allocated device file,

Void device_destroy (struct class * class, dev_t devt)
Parameters are mainly device type and device number.

Release the device category. The main function is class_destroy ()

Void class_destroy (struct class * CLs)
The parameter is a device type.

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.