Linux Driver Development process

Source: Internet
Author: User

Embedded Linux Driver Development process
In embedded systems, the operating system is driven by a variety of drivers to control hardware devices. A device driver is an interface between an operating system kernel and a hardware device that shields the application of hardware details so that, in the application's view, a hardware device is simply a device file that can operate on a hardware device as if it were a normal file. The device driver is part of the kernel and completes the following functions:
◇ Registration and logoff of driver program.
◇ the opening and release of the equipment.
◇ Read and write operation of equipment.
◇ control operation of the equipment.
◇ equipment interruption and polling processing.
Linux mainly divides the devices into three categories: character devices, block devices, and network devices. A character device is a device that sends and receives data in the form of a character without a buffer, and a block device is a device that sends and receives data in the form of an entire data buffer, and a network device is a BSD socket interface accessed by a network device. The following is an example of a character device that writes out its driver writing framework:
First, write the driver initialization function
The initialization of the driver is done in function xxx_init (), including hardware initialization, interrupt functions, registering drivers to the kernel, and so on.
First understand the hardware structure, figure out its functions, interface registers and how the CPU accesses these registers and so on.
Next, register the driver with the kernel. The device driver can be compiled directly into the kernel, initialized when the system starts, or dynamically loaded into the kernel as needed, in a modular way. Each character device or block device is registered through the Register_chrdev () function, which can then be applied to the system to request a master device number, and the device name will appear in/proc/devices.
In addition, when shutting down the device, it is necessary to remove the original device registration, need to have a clear function, in Xxx_exit () through the Unregister_chrdev () function in the implementation, after which the device will disappear from the/proc/devices.
When the driver is compiled into a module, using the Insmod load module, the module's initialization function Xxx_init () is called, registers the driver with the kernel, and uses the Rmmod unload module, the module's Purge function Xxx_exit () is called.
The various member functions to be used in constructing the file_operations structure
The Linux operating system sees all the devices as files and accesses the device in the form of files. The application cannot manipulate the hardware directly, and the hardware driver is called using a unified interface function, and this set of interfaces becomes a system call. Each system call has a corresponding function (open, release, read, write, IOCTL, and so on), and in the character driver, these functions are set in a file_operations type of data structure. Take a keyboard driver as an example:
struct File_operations key7279_fops =
{
. open = Key7279_open,
. IOCTL = Key7279_ioctl,
. Release = Key7279_close,
. Read = Key7279_read,
};
1, the opening and release of equipment
Opening the device is done by the open () function, and in most device drivers, open completes the following tasks:
◇ Increment counter
◇ Check special cases of specific equipment
◇ Initialization Equipment
◇ identification of the secondary device number
The release device is completed by the release () function. When a process releases a device, other processes can continue to use the device, except that the process temporarily stops using the device, and when a process shuts down the device, other processes must reopen the device to be able to use it. Release completes the following tasks:
◇ Decrement Count
◇ turn off the device during the last release of the device operation
2, the device read and write operation
The primary task of the read-write device is to copy the data from the kernel space to the user space, or from the user space to the kernel space, which is to copy the data in the kernel space buffer into the buffer of the user space or vice versa. The character device uses its own read () function and the write () function to read and write data.
3, the control operation of the equipment
In addition to the ability to read and write, most devices can perform operations beyond the simple data transfer, so device drivers must also have the ability to perform a variety of hardware control operations. These operations are often supported by the IOCTL method. Unlike read and write operations, the use of IOCTL () is closely related to specific devices. Take keyboard Key7279_ioctl as an example:
static int key7279_ioctl (struct inode *inode,struct file *file,unsigned int cmd, unsigned long arg)
{
Switch (CMD)
{
Case Key7279_getkey:
return Key7279_getkey ();
Default
PRINTK ("Unkown Keyboard Command id.\n");
}
return 0;
}
The value and meaning of CMD are related to the specific device, except for the IOCTL (), the device driver may have other control functions, such as Llseek ().
When an application opens a device using a function such as open, release, the corresponding member in the file_operations structure of the device driver is called.
Iii. interruption and polling processing of equipment
For devices that do not support interrupts, you need to poll the device state when reading and writing, and whether you need to continue the data transfer. For example, a printer. If the device supports interrupts, it can be done in the interrupt mode.
The module requests an interrupt channel (or IRQ interrupt request) before using the interrupt and releases it after use. Use the REQUEST_IRQ () function to register the interrupt, FREE_IRQ () function to release.
Iv. Driver Testing
Debugging a driver can be done by printing, by adding the PRINTK () print function to the driver to track the execution of the driver to determine the problem.
The above is based on my own study summary, may write relatively simple, for more complex driving function, will add more functions, but the general framework is this.

Operating system-based drivers are hardware interface functions without operating systems plus operating system jackets

The approximate process for implementing an embedded Linux device driver is as follows:
(l) Review the schematic to understand how the device works.
(2) Define the main device number. The device is identified by a master device number and a secondary device number. The main device number uniquely identifies the set
Type, or device driver type, that is the index of a device table item in a block device table or character device table. Secondary device number only
Interpreted by the device driver to differentiate a separate device from being controlled by a device driver.
(3) Implement the initialization function. Implement driver registration and uninstallation in the driver.
(4) Design the file operation to be implemented, define the file--operations structure.
(5) Implement the required file operation calls, such as Read,write.
(6) To implement interrupt service and register with REQUEST--IRQ to the kernel, interrupts are not required for each device driver.
(7) Compile the driver into the kernel, or load the module with the Insmod command.
(8) test the device, write the application, and test the driver.

Typical character device driver authoring framework:

1 Writing hardware interface functions

2 establish the interface between the file system and the device driver, such as: struct file_operations struct

3 registering the device into the CHRDEVFS global array, registering or unregistering the device can be at any time, but typically registers the device when the module is loaded, and unregisters the device when the module exits. (Module_init (); Module_exit ();)

4 Compile the drive source code in a modular manner and load it into the kernel

5 Creating a Device node, Mknode

6 writing an application to access the underlying device

Linux Driver Development 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.