"Original"--"character device"--the whole process

Source: Internet
Author: User

I. Overview

1, character device : Refers to a byte can only one byte read and write devices, can not randomly read the device memory of a certain data, read data need to follow the data. The character device is a stream-oriented device , and the common character devices are mouse, keyboard, serial port, console and led device.
2, block device : Refers to the device can be read from any location of a certain length of data equipment. Block devices include hard disks, disks, USB sticks, and SD cards.

Each character device or block device corresponds to a device file in the/dev directory. The Linux User program uses the driver to manipulate character devices and block devices through device files (or device nodes).

Second, the use of equipment flow:
1, generate the device number, dev_t
A character device or a block device has a master device number and a secondary device number, both of which are device numbers.

The main device number identifies the driver that is connected to the device file and is used to reflect the device type. The secondary device number is used by the driver to identify which device is operating and to distinguish between devices of the same type.


In the Linux kernel, the device number is described by dev_t, as defined in 2.6.28:
typedef u_long DEV_T;
In a 32-bit machine is 4 bytes, the high 12 bits represent the main device number, and the lower 12 bits represent the secondary device number.

You can use the following macros to obtain primary and secondary device numbers from dev_t:
MAJOR (dev_t Dev);
MINOR (dev_t Dev);

You can also use the following macros to generate dev_t with primary and secondary device numbers:

dev_t = MKDEV (int major,int minor);

2. Apply the device number (two methods) to the kernel:

(1) Static application:
int Register_chrdev_region (dev_t from, unsigned count, const char *name);

(2) Dynamic distribution:

int alloc_chrdev_region (dev_t *dev, unsigned baseminor, unsigned count, const char *name);

(3) Cancellation device number:

void Unregister_chrdev_region (dev_t from, unsigned count);

  

3. Registration

Cdev_init ()/cdev_alloc (), Cdev_add (), Cdev_del ()

See http://www.cnblogs.com/apolloenterprise/p/4663115.html

The above driver part of the basic analysis completed, the code is as follows:

Third, the app

VI. Development Board Mid-order

TFTP 50008000 Zimage

Bootm 50008000

  Mknod /dev/hand_one C 248 0//parameter for device name device type main device number secondary device number

Insmod./drv_hand_one.ko

./app_hand_one

Note: Mknod creates a device file:
Use cat/proc/devices to view the device name, device number, to which you are applying.
(1) manually created with Mknod:mknod filename Type major minor in the command line of the Development Board
(2) Automatic creation: The use of Udev (Mdev) to achieve automatic creation of device files, first of all to ensure that support Udev (Mdev), CONFIGURED by BusyBox. In the driver initialization code, call Class_create to create a class for the device, and then call device_create for each device to create the corresponding device.

Seven, the character device driver important data structure:
(1) struct file: Represents an open file descriptor, each open file in the system has an associated struct file in the kernel. It is created by the kernel at open and passed to any function that operates on the file until it is finally closed. When all instances of the file are closed, the kernel releases the data structure.

(2) struct inode: used to log the physical information of a file. It is different from the file structure that represents open. A file can correspond to multiple file structures, but only one inode structure. The inode is generally passed as a parameter to the function in the file_operations structure.
The inode is translated into Chinese as an index node. Partition of each storage device or storage device (storage device is hard disk, floppy disk, USB stick ...) After being formatted as a filesystem, there should be two parts, one for the inode, and the other for the Block,block to store the data. The inode is the information that is used to store this data, including file size, owner, attribution user group, read-write permission, and so on. The Inode indexes the information for each file, so there is a value for the inode. According to the instructions, the operating system can find the corresponding file by the Inode value.

(3) struct file_operations

"Original"--"character device"--the whole process

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.