Key points of driver development in embedded Linux

Source: Internet
Author: User
In Linux, there are three main types of device files: block devices, character devices, and network devices. This classification method can control the driver of the input/output device and other operating systems.

In Linux, there are three main types of device files: block devices, character devices, and network devices. This classification method can separate drivers that control input/output devices from other operating system software.

The main difference between a character device and a block device is that when a read/write request is sent to a character device, the actual hardware I/O usually follows. A block device does not use a system memory as a buffer. if the user process's request to the device meets the user's requirements, the request data is returned. otherwise, you can call the request function to perform actual I/O operations. Block devices are designed for disks and other slow devices to avoid excessive CPU time consumption. Network devices can access data through BSD interfaces.

Each device file has its file attributes (c/B), indicating whether it is a character device or a block device. In addition, each file has two device numbers. The first is the master device number, which identifies the driver. The second is the slave device number, identifies hardware devices that use the same device driver. The master device number of the device file must be the same as the master device number applied by the device driver during registration. Otherwise, the user process will not be able to access the driver.

The interface between the operating system kernel and applications during system calling. the device driver is the interface between the operating system kernel and the Machine hardware. The device driver is part of the kernel and implements the following functions:

◆ Device initialization and release

◆ Transmit data from the kernel to hardware and read data from hardware

◆ Read the data transmitted by the application to the device file and send back the data requested by the application.

◆ Detect and handle device errors

MTD (Memory Technology Device) devices are flash Memory chips, small flash cards, Memory stick and other devices that are increasingly used in embedded devices. The MTD driver is a new type of driver specially developed for the embedded environment in Linux. Compared with conventional block device drivers, MTD drivers provide better support and management for flash devices and provide better interfaces for sector-based erasure and read/write operations.

Driver structure

Linux Device Drivers can be divided into three main components:

1. Automatic configuration and initialization subroutines are used to monitor whether the hardware device to be driven exists and whether the hardware device can work normally. If the device is normal, initialize the software status required by the device and its related device drivers. These drivers are called only once during initialization.

2. subprograms serving I/O requests, also known as the upper half of the driver. This part of the program is called by the system. During the execution of these programs, the system still considers that the called process belongs to the same process, but the user state has changed to the core state, the runtime environment of the user program that calls this system. Therefore, you can call functions related to the runtime environment, such as sleep.

3. interrupt service subprogram, also known as the lower half of the driver program. In Linux, the interrupt service subroutine of the device driver is not directly called from the interrupt vector table, but is received by the Linux system and then called by the system. An interrupt can be generated when any process is running. Therefore, when the interrupt service program is called, it cannot depend on the status of any process, or call any function related to the process running environment. Because device drivers generally support several devices of the same type, when the system calls the interrupt service subroutine, one or more parameters are carried to uniquely identify the device requesting the service.

In the system, the storage/fetch of I/O devices is performed through a set of fixed entry points, which are provided by the driver of each device. In Linux, the entry points provided by the device driver are described by a file operation structure. The file_operation structure is defined in the linux/fs. h file.

  1. Struct file_operation {int (* lseek) (struct inode * inode, struct file * filp, off_t off, int pos); int (* read) (struct inode * inode, struct file * filp, char * buf, int count); int (* write) (struct inode * inode, struct file * filp, const char * buf, int count ); int (* readdir) (struct inode * inode, struct file * filp, struct dirent * dirent, int count); int (* select) (struct inode * inode, struct file * filp, int sel_type, select_table * wait); int (* ioctl) (struct inode * inode, struct file * filp, unsigned int cmd, unsigned int arg ); int (* mmap) (void); int (* open) (struct inode * inode, struct file * filp); int (* release) (struct inode * inode, struct file * filp); int (* fasync) (struct inode * inode, struct file * filp );};

In the file_operation structure, almost all the members are function pointers, so they are actually function jump tables. The operations performed by each process on the device are converted into access to the file_operation structure based on the major and minor device numbers.

Common operations include:

◆ Lseek: the position where the file pointer is moved. it can only be used for devices that can be accessed randomly.

◆ Read: perform read operations. The buf parameter is the buffer zone for storing read results, and the count parameter is the length of data to be read. If the returned value is negative, the read operation is incorrect. Otherwise, the actual number of bytes read is returned. For the bytes type, both the number of bytes read and the actual number of bytes returned must be multiples of the inode-i_blksize.

◆ Write, perform write operations, similar to read

◆ Readdir: Get the Next Directory entry point, which is used only by the device programs related to the file system.

◆ Select. If the driver does not provide a select entry, the select operation determines that the device is ready for any I/O operations.

◆ Ioctl for read and write operations. the cmd parameter is a custom command.

◆ Mmap is used to map device content to address space. it is generally used only by block device drivers.

◆ Open: open the device to prepare for I/O operations. If the return value is 0, the operation is successful. if the return value is negative, the operation fails. If the driver does not provide an open entry, The open is considered successful as long as the/dev/driver file exists.

◆ Release, that is, the close operation.

In your own driver, you must first complete the function implementation in the file_operation structure based on the driver's functions. You can initialize unnecessary function interfaces directly in the file_operation structure to NULL. The file_operation variable is registered to the system during driver initialization. When the operating system operates on the device, it calls the function pointer in the file_operation structure registered by the driver.

Linux interrupt handling

In a Linux system, interrupt handling is a core part of the system. Therefore, if you do not configure data exchange between the system and the system, the driver of the device must be a part of the core of the system. The device driver calls the request_irq function to request an interruption and uses free_irq to release the interruption. they are defined:

  1. # Include Int request_irq (unsigned int irq, void (* handler) (int irq, void dev_id, struct pt_regs * regs), unsigned long flags, const char * device, void * dev_id ); void free_irq (unsigned int irq, void * dev_id );

The irq parameter indicates the hardware interrupt number to be applied for. handler is the interrupt processing subroutine registered with the system. when the interrupt is generated, it is called by the system and the irq parameter is the interrupt number; dev_id indicates the device ID that the system is notified upon application; regs indicates the Register content when interruption occurs; device indicates the device name, which will appear in the/proc/interrupts file; flag is an option during application. it determines some features of the interrupt handler. The most important thing is whether the interrupt handler is a fast or slow handler. All interrupts are blocked when the fast processing program is running, while all the interruptions except the ones being processed are not blocked when the slow processing program is running. In Linux, interrupts can be shared by different interrupt handlers.

As part of the system's core, the device driver does not call malloc and free when applying for and releasing memory, but instead calls kmalloc and kfree. they are defined:

  1. # I nclude Void * kmalloc (unsigned int len, int priority); void kfree (void * obj );

The len parameter is the number of bytes to be applied for; obj is the memory pointer to be released; priority is the priority of memory allocation operations, that is, how to operate when there is not enough idle memory, GFP_KERNEL is generally used.

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.