Linux character device driver Overview

Source: Internet
Author: User

I. Overview:

1. in Linux, there is a philosophical saying "all files under Linux ".

The device driver shields the application from hardware details. In this way, the hardware device is only a device file, and the application can operate the hardware device like a common file.

However, device files are different from common files.

What is the difference between a device and a common file?

Let's take a look at two figures:

Common file:

-RW-r -- 1 Stella 3699 my_usbtmcapp.c

-Rwxr-XR-x 1 Stella 8763 Tiger

-RW-r -- 1 Stella 441 tiger. c

DEVICE file:

CrW ------- 1 Root 252, 4 USB mon4

CrW ------- 1 Root 252, 5 USB mon5

CrW-RW ---- 1 root tty 7, 0 VCs

CrW-RW ---- 1 root tty 7, 1 vcs1

1> before the access permission, the letters B or C indicate Block devices and character devices respectively.

2> the device file does not have a file length, but the other two values are added: the master device number and the slave device number. The two form a unique number, and the kernel can find the corresponding device driver.

3> the reason why a device file is assigned a name is that it is easier for users to remember the symbol name rather than a number, but the name cannot represent the actual function of the device file, this indicates a device by using the Master/Slave Device number. The directory of the device file is also unrelated to its function.

However, the following standard method is still used for naming device files:

Mknod is used to create a device file.

2. the kernel uses the Master/Slave Device number to identify the matched driver.

Why use two numbers to identify the driver?

1> first, the system may contain several devices of the same type, managed by the same device driver (it does not make sense to load the same code to the kernel multiple times)

2> second, similar devices can be combined to facilitate insertion into the kernel data structure for management.

3. Linux device drivers:

1> Linux device drivers are divided into character device drivers (unbuffered and sequential access only) and block Device Drivers (with buffering and random access ). Each character device and block device must have the primary and secondary device numbers. devices with the same primary device numbers are similar devices (using the same driver)

(1) Block devices: devices in the system that can access chunks of a fixed size randomly (in no order) are called Block devices; they are all used to install the file system.

(2) character devices: character devices are accessed sequentially by character Flow

2> some of these devices are abstract to the actual physical hardware, while some are functions provided by the kernel itself (not dependent on specific physical hardware, also known as "Virtual Devices ")

(1) Each device has a corresponding file (node) under the/dev directory)

You can use the following command to view

CD/dev

Ls-Al

The first two columns of the date indicate the Primary and Secondary device numbers of the corresponding device.

(2) You can run the CAT/proc/devices command to view the master device Number of the currently loaded device driver. The first column is the master device number, and the second column is the device name.

3> the primary device numbers of Block devices and character devices may be the same. Therefore, unless both the device number and device type (Block device/character device) are specified, the driver found may not be unique.

4. Master/Slave Device number

1> in the kernel, The dev_t type is used to save the device number (including the primary device number and secondary device number). dev_t is a 32-bit number, and 12-bit indicates the primary device number, 20 indicates the next device number

(1) master device number = major (dev_t Dev)

(2) Sub-device number = minor (dev_t Dev)

(3) device number = mkdev (INT major, int minor)

2> the primary device number is a concept corresponding to the driver. The same type of device generally uses the same primary device number. Different types of devices generally use different primary device numbers. Because the same driver supports multiple similar devices, the device Number of the driver is described as the serial number of the device. Generally, the serial number starts from 0.

5. mkdev () macro implementation

# Define mkdev (MA, mi) (MA) <minorbits) | (MI ))

# Define mkdev (MA, mi) (MA) <8 | (MI ))

Obtain the location of a device in the device table.

6. dev_t is an unsigned long model.

1> typedef u_long dev_t;

2> typeddef unsigned long u_long;

7. Assign and release device numbers:

(1) int register_chrdev_region (dev_t first, unsigned int count, char * Name );

(2) int alloc_chrdev_region (dev_t * Dev, unsigned int firstminor, unsigned int count, char * Name );

(3) void unregister_chrdev_region (dev_t first, unsigned int count );

1> the register_chardev_region function is used to know the start device number. During static application, if the master device number is not the same as the master device number of a character in the system, this function will certainly return a success; if the primary device number of a registered character device in the system is the same as the applied primary device number, the kernel also needs to check whether the range of the secondary device numbers of the two devices overlap, then register_chrdev_region returns-ebusy, indicating that the system is busy and the requested device number is occupied. If no overlap is found, register_chrdev_region returns 0, indicating that the request is successful.

2> alloc_chrdev_region () is used when the device number is unknown and the system dynamically requests the device number. A device number is dynamically allocated to the driver by calling this function, combine the assigned master device number and the baseminor parameter into a device number, and return it to the user through the output parameter Dev.

3> compared with register_chrdev_region (), alloc_chrdev_region () automatically avoids duplicate device numbers. However, when alloc_chrdev_region is used to dynamically apply for a device number, you need to obtain the master device number through major = mmor (Dev). If you use the static device number application, you only need ~ Between 2 ^ 12-1, the next device number is 0 ~ 2 ^ 20-1, and the device number does not conflict, the kernel will successfully register the device number. If the user uses a dynamically assigned device number, the number of the primary device allocated by the kernel for the user is only 1 ~ Between 254, if all the 254 device numbers are occupied, Dynamic Allocation of device numbers will fail, but there are only over 30 commonly used devices, so basically, you don't have to worry about Kernel failure during dynamic allocation.

4> after the cdev_del () function is called to log out of the character device from the system, the unregister_chrdev_region () function should be called to release the original device number.

Bytes -----------------------------------------------------------------------------------------

For details about the register_chrdev_region () function series, see register_chrdev_region, a Linux character device driver.

Bytes -----------------------------------------------------------------------------------------

8. Register character Devices

After obtaining the device number range, you must add the device to the character Device database to activate the device. You need to use the cdev_init function to initialize a struct cdev instance, and then call the cdev_add function.

(1) void cdev_init (struct cdev * cdev, struct file_operation * FoPs );

(2) int cdev_add (struct cdev * Dev, dev_t num, unsigned int count)

(3) void cdev_del (stuct cdev * Dev)

1> the cdev_init () function is used to initialize cdev members and establish connections between cdev and file_operations.

2> the cdev_add () function and the cdev_del () function add and delete a cdev respectively to the system to complete registration and cancellation of character devices. The call to cdev_add () usually occurs in the character device driver module loading function, while the call to the cdev_del () function usually occurs in the character device driver module uninstallation function.

3> you must apply for a device number before registering a character device.

Bytes -----------------------------------------------------------------------------------------

For the specific implementation of the cdev_init () function series, see Linux character device driver cdev_init ().

Bytes -----------------------------------------------------------------------------------------

9. Data Copying between the kernel space and the user space

(1) unsigned long copy_to_user (void _ User * To, const void * From, unsigned Long Count );

(2) unsigned long copy_from_user (void * To, const void _ User * From, unsigned Long Count );

Because the kernel space and the user space memory cannot be directly accessed from each other, the copy_from_user () function is used to copy the user space to the kernel space. The copy_to_user () function copies the kernel space to the user space.

Reprinted from: http://blog.csdn.net/tigerjb/article/details/6412438

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.