[Arm-linux Development] Main device number--the link between the drive module and the device node __linux

Source: Internet
Author: User
Tags goto int size
first, how to operate the equipment
The operation of the device in Linux is done by means of a file, including open, read, and write.
For device files, it is generally called a device node,
The node has a property that is the device number (the primary device number, the secondary device number), where the main device corresponds to the device file and the drive module


When we open a device node, we tell kernel to operate the node that is the main device number XX, then kernel will find the appropriate memory module through XX, and then call the Open function defined in the memory module.

Because the kernel requires information about the main device number before the operation of the node, the application of the master number, the addition of a character device with that master number, needs to be performed in the initialization function of the driver module

second, the application of the main equipment numberIt is recommended that the dynamic application of the main device number, Linux has many devices, each device corresponds to a main device number, dynamic request is allocated by the kernel a useless master

Device number,
The dynamic request function is alloc_chrdev_region, and the corresponding release function is unregister_chrdev_region.
After the application is completed, you can read the assigned master number from the/proc/devices, and then you need to set up the device node later

add character devices to KernleThe previous step to the kernel to request the main device number, you can add a character device to the kernel
A character device in kennel corresponds to a struct Cdev that defines how the character device is operated file_operations (including open, read, write), which also needs to be defined beforehand in the driver module.


Add steps for character device structure Cdev:
Cdev initialization: Cdev_init, which corresponds file_operations to Cdev
Add to Kernel: Cdev_add, which maps the main device number to the CDEV structure

When the open device node is used, the main device number is first found through the node, then the character device Cdev corresponding to the main device number is searched in the kernel, and then the open method defined by the file_operations structure in Cdev is moved (this open is to be implemented by itself)


Four, 3 important structural bodiesOne is File_operations, which contains mainly the main implementation methods of the drive
One is the inode, this is the node information, contains the main device number and the CDEV structure
One is file, and when the node is first opened, a file structure is created in the kernel, and the file structure acts as a link to the method in the File_operations, or the Read and Wirte methods know how to operate the data of that device.

Custom content in file (the data that drives it) is generally defined in open, and then read and write can manipulate custom data.



Here's a simple example of how the driver corresponds to the custom open method and the main device number.


#include <linux/module.h>/* It defines the API, type, and macros (Module_license, Module_author, etc.) of the module, and all kernel modules must contain this header file. */
#include <linux/init.h>
#include <linux/fs.h>//device number correlation function
#include <linux/slab.h>//memory allocation related functions
#include <linux/types.h>
#include <linux/kdev_t.h>//device number correlation function
#include <linux/cdev.h>//character device header file
#include <linux/module.h>

struct CHAR_DEV
{
int size;
Char *data;
Character devices in the struct Cdev cdev;//kernel
};

int major = 0;
int minor = 0;
struct Char_dev char_devices;

int Char_open (struct inode *inode, struct file *filep)
{
int Major = 0;
Major = Major (Inode->i_rdev);
PRINTK ("Open My_char_dev Major:%d\n", Major);

return 0;
}

struct File_operations char_fops = {
. Owner = This_module,
. open = Char_open,
};




static void Char_exit (void)//If the function is called in the init function, there should be no __exit
{
dev_t Dev;
PRINTK ("char device driver exit \ n");
Release device number
dev = Mkdev (major, minor);
Unregister_chrdev_region (Dev, 1);
PRINTK ("Release Major%d\n", Major);

Free memory
if (char_devices.data) {
Kfree (Char_devices.data);
}

Remove a character device from the kernel
Cdev_del (& (Char_devices.cdev));
}

The static int __init char_init (void)//__init a token indicating that it is an initialization function
{
Initialized code
dev_t Dev;
int result;
PRINTK ("char device driver init \ n");

Dynamically request a device number to the kernel
result = Alloc_chrdev_region (&dev, 0, 1, "My_char_dev");
Major = major (Dev);
Minor = minor (dev);
PRINTK ("Alloc major%d\n", Major);
if (Result < 0) {
PRINTK (kern_warning "My_char_dev:can ' t get major%d\n", Major);
return result;
}

Assigning a piece of memory to a device
char_devices.size = 100;
Char_devices.data = (char*) kmalloc (char_devices.size, Gfp_kernel);
if (!char_devices.data) {
result =-enomem;
Goto fail; Cannot exit function directly, need to release device number
}

Add a character device to the kernel Cdev
Cdev_init (& (Char_devices.cdev), &char_fops);
Char_devices.cdev.owner = This_module;
Char_devices.cdev.ops = &char_fops;
result = Cdev_add (& (Char_devices.cdev), Dev, 1);
if ((Result < 0)) {
PRINTK (kern_warning "Error%d adding my_char_dev\n", result);
Goto fail;
}

return 0; Success
Fail
Char_exit ();
return result;
}

Module_license ("Dual BSD/GPL");
When the module is loaded, the Moudle_init function is executed, and the function calls the initialization function
Module_init (Char_init);
When module uninstall, invoke, release resources
Module_exit (Char_exit);


kdir=/usr/src/linux-headers-$ (Shell Uname-r)


pwd=$ (Shell PWD)


Obj-m = CHARDEVICE.O


All
$ (make)-C $ (Kdir) m=$ (PWD)


Note: After driving the insmod, view the main device number through the/proc/devices, then create the device node by Mknod in/dev, note that the main equipment is in good agreement, the current node only supports the Open method and can be validated in demsg.







Related Article

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.