Linux Device File System Analysis and usage

Source: Internet
Author: User

1. What is a Linux Device File System?

First, we do not look at the definition. The definition is always too abstract and hard to understand. Let's look at the phenomenon first. After porting a new file system to the Development Board (if drivers of various devices are also transplanted), start the Development Board and use the serial port tool to enter the Development Board and view the system/dev directory, there are usually no or only null, console, and other system-required device files, and there are no device files. Why are there no device files for various device drivers we have transplanted? If you want to use these devices, you do not need to manually create the device file nodes of these devices one by one, this makes it extremely inconvenient for us to use the device (this is the case for porting various device drivers mentioned in the previous article ).

The device file system is the key to solving this problem. It can dynamically create Device File nodes of various devices in the/dev directory during system device initialization (that is, after the system is started, the/dev directory contains the device files of various devices, which can be used directly ). In addition, it can also automatically delete the corresponding device file nodes under/dev after the device is uninstalled (this is useful for some hot swapping devices and is automatically created when plugged in, is automatically deleted when unplugging ). Another advantage is that when writing a device driver, you do not have to specify the primary device number for the device. When registering the device, you can use 0 to dynamically obtain the available primary device number, then, create and destroy the device files in the driver. (This is generally implemented in the driver module's loading and uninstalling functions ).

Ii. Types of Device File Systems

The device file systems include devfs, udev, and mdev.

Mdev is a simplified udev version and is a program in busybox. It is most suitable for embedded systems, while udev is generally used in Linux on PC, which is more complex than mdev; devfs is introduced in the 2.4 kernel, but it is replaced by udev In the 2.6 kernel. They share the same advantages, but there are some unrepaired bugs in devfs, the author also stopped maintenance for him. The most significant difference is that when devfs is used, when an existing device node is turned on, but it can automatically load the corresponding driver, while udev cannot. udev thinks that the corresponding driver module should not be loaded when opening a device node that does not exist, because it is useless to load it, A waste of system resources.

Iii. Use of udev or mdev File System

1.First, let everyone understand the problem: whether it is udev or mdev, they are an application, just like other applications (such as the boa Service), you can use it after configuration. For convenience, we use a mdev that comes with busybox. In this way, when configuring and compiling busybox, we only need to select the mdev support options, after compilation, it will include the application of the mdev Device File System (of course, you can also download the udev source code for compilation and transplantation without using the built-in busybox)

# Cd busybox-<? XML: namespace prefix = ST1/>/
# Make menuconfig

Linux system utilities --->
[*] Mdev
[*] Support/etc/mdev. conf
[*] Support subdirs/symlinks
[*] Support regular expressions substitutions when renaming Device
[*] Support Command Execution At device addition/removal

2. udev or mdev requires support from the kernel sysfs and tmpfs virtual file systems. sysfs provides the device entry and uevent channel for udev, and tmpfs provides storage space for udev device files. Therefore, add the following content (in red) to the/etc/fstab configuration file ):

# Device Mount-point type options dump fsck order
#----------------------------------------------------------------
Procfs/proc defaults 0 0
Sysfs/sys sysfs defaults 0 0
Tmpfs/dev/SHM tmpfs defaults 0 0
Usbfs/proc/bus/USB usbfs defaults 0 0
Ramfs/dev ramfs defaults 0 0
None/dev/PTS devpts mode = 0622 0 0

3. initialize the configuration file/etc/init in the system. d. Mount the sysfs file system and tmpfs file system to be used by mdev in the RC file system, and start the mdev application in the/sbin directory to search for the system devices (in red ).

# Mount virtual filesystem
/Bin/Mount-T proc procfs/proc
/Bin/Mount-n-t sysfs/sys
/Bin/Mount-n-t usbfs/proc/bus/USB
/Bin/Mount-T ramfs/dev

# Make dir
/Bin/mkdir-P/dev/PTS
/Bin/mkdir-P/dev/SHM
/Bin/mkdir-P/var/log
/Bin/Mount-n-t devpts NONE/dev/PTS-o mode = 0622
/Bin/Mount-n-t tmpfs/dev/SHM

# Make device Node
Echo/sbin/mdev>/proc/sys/kernel/hotplug
/Sbin/mdev-S

4. add support for device interfaces in the device driver, that is, create and destroy device files in the driver loading and uninstalling functions, for example, add (red part) to the key driver of the previous section ):

# Include <Linux/device. h> // header file used by the device class

Static int device_major = device_major; // used to save the dynamically generated master device number.

Static struct class * button_class; // defines a class

Static int _ init button_init (void)
{
// Register a character device. device_major = 0 is defined here, so that the system can allocate the device. After successful registration, the dynamically assigned master device number will be returned.
Device_major = register_chrdev (device_major, device_name, & buttons_fops );

If (device_major <0)
{
Printk (device_name "register faild! \ N ");
Return device_major;
}

// Register a device class so that mdev can create a device node in the/dev/directory.
Button_class = class_create (this_module, device_name );

If (is_err (button_class ))
{
Printk (device_name "create class faild! \ N ");
Return-1;
}

// Create a device node named device_name (my2440_buttons)

// Note that the function name in earlier versions of kernel 2.6 is class_device_create, Which is device_create
Device_create (button_class, null, mkdev (device_major, 0), null, device_name );

Return 0;
}

Static void _ exit button_exit (void)
{
// Deregister the character device
Unregister_chrdev (device_major, device_name );

// Delete the device node. Note that the function name of earlier versions of kernel 2.6 is class_device_destroy, Which is device_destroy.
Device_destroy (button_class, mkdev (device_major, 0 ));

// Logout class
Class_destroy (button_class );
}

4. For the mdev configuration file/etc/mdev. conf, this is dispensable, just to set some rules for the device file. I will leave him alone and leave him empty.

5. after completing the above steps, recompile the file system, download it to the Development Board, start the development board, and enter the/dev directory of the Development Board to view it. Many system device nodes are generated here, we can directly use these device nodes.

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.