Linux Framebuffer Driver Analysis II-driver framework, interface implementation and usage
Following the previous article "one of the Linux Framebuffer driver analysis-software requirements", this article deeply analyzes the driver framework and interface implementation and use of the LinuxFramebuffer subsystem.
I. LinuxFramebuffer software requirements
The previous article elaborated on the software requirements of LinuxFramebuffer (please first understand the first article and then read this article). The summary is as follows:
1. program the LCD control registers of the SOC to support different LCD screens to maximize the application scenarios of the SOC. This is a requirement related to the hardware platform. It corresponds to the Linux source code path arch \ arm \ mach-s5pv210 \ XXX210-lcds.c in the implementation content.
2. it provides you with an interface for ing process space to the actual display of physical memory (mmap), so that the application can display image resources to the LCD screen at a copy. This requires the support of soc mmu (Memory Management Unit), Linux operating system memory management, and soc sdram (memory. As a common component of the operating system, Memory Management provides independent interfaces for other subsystems. Therefore, we can think that framebuffer is a non-platform-related requirement to call the memory management interface to implement the ing interface.
3. Framebuffer supports 32 display caches. It is abstracted internally, that is, its upstream application is uniformly abstracted as a character master device, and different display caches are considered as different characters from the device. The management of display slave devices is a function of the Framebuffer internal framework.
4. Linux device drivers support various types of device drivers. In addition to Framebuffer, they also include input, USB, watchdog, MTD, and so on. Different types of devices are generally expressed as different master devices. The Linux Device Driver framework is used to manage different main devices. In addition, because Linux regards the device as a file, a virtual File System (VFS) is also set up on the device driver. Therefore, in fact, the Application Layer interacts with the VFS interface corresponding to framebuffer. For example:
For Driver developers, the first part of register programming (red part) is only required for the specific hardware platform SOC and the specific LCD (welded to the SOC pin ). The second, third, and fourth parts (the green part) have been abstracted and implemented in the source code released by the Linux driver, LCD Driver developers only need to understand the internal framework and Interface Usage of framebuffer. In fact, for drivers of other devices, including buttons, touch screens, SD cards, and usb, Linuxdriver has implemented most of the non-platform-related requirements, developers only need to understand the internal framework and interface of the driver.
Next, we will analyze in detail how LinuxFramebuffer supports the second and third requirements. It corresponds to implementation content such as driver \ video \ fbmem. c. First, analyze the third driver framework, and then analyze the ing interface.
Ii. Internal Driving framework of LinuxFramebuffer
1. Initialization
-- Driver \ video \ fbmem. c
As a sub-system, Framebuffer registers a 29-character device driver with the master device number in fbmem_init through the register_chrdev interface. Use class_create to create a graphics device class and use the mdev mechanism to generate a device file (in the/dev directory) for user access ). For details about the device files and mdev mechanisms, see create a Linux Device File and mdev.
The interface set fb_fops of the Framebuffer device driver is defined:
In the linux Device Driver, all display cache devices are managed by the framebuffer subsystem. That is, the linux Device Driver framework only recognizes one framebuffer device with the primary device number 29. All accesses to the display cache (up to 32) at the application layer will be pushed to fb_fops for further distribution.
2. register_framebuffer
A single display cache is regarded as a framebuffer slave device. During driver loading initialization, it needs to register itself with the framebuffer subsystem through the register_framebuffer interface. In this way, you can use the framebuffer subsystem for operation management and distribution only when the application layer needs to access the slave device. Let's follow up:
Each slave device needs to pass a Data Structure pointer of fb_info, which represents a single display cache device. From this, we can see that fb_info will eventually be stored in the Global Array struct fb_info * registered_fb [FB_MAX]. FB_MAX is 32. Here we can also see that framebuffer supports up to 32 slave devices. In addition, each registered device creates a device in the/sys/class/graphics/device class, And mdev generates the corresponding device file in the/dev/directory. Assume that M sub-devices call the register_framebuffer interface to generate M device files in/dev, such as/dev/fb0,/dev/fb1,/dev/fb2, and so on. The master device numbers of M devices are both 29, and the slave device numbers are 0, 1, and 2.
3. fb_info struct
The fb_info struct represents a single display cache slave device. before calling the register_framebuffer interface, you must initialize important data members. It is defined as follows:
The fb_var_screeninfo and fb_fix_screeninfo structures are related to the LCD hardware attributes. fb_var_screeninfo indicates the LCD display parameters that can be modified, such as resolution and pixel bit numbers. fb_fix_screeninfo indicates the unchangeable LCD attribute parameters, for example, the physical address and length of the memory are displayed. Another important member is fb_ops, which is a set of underlying LCD hardware operation interfaces.
Fb_ops hardware operation interface set contains many interfaces, such as setting variable parameters fb_set_par, setting color register fb_setcolreg, clear screen interface fb_blank, drawing Bitmap interface fb_imageb.pdf, and memory ing fb_mmap.
The fb_info struct completes initialization before calling register_framebuffer. Generally, the LCD device is a platform device and its initialization is completed through the probe interface driven by the platform device. The hardware initialization (required in the first part) of the LCD device is completed in the initialization of the platform device. For the analysis of platform device drivers, I will write another article.
4. fb_open Interface
When a framebuffer device completes initialization, its corresponding fb_info structure is recorded in the Global Array registered_fb and located at the location equal to the device number, a/dev/fbx device file is also generated in the/dev directory. The following analysis assumes that the first framebuffer device is accessed:
For application layer open ("/dev/fb0 ",...) For access to the framebuffer device, vfs first obtains the master device (29) and slave device (0) of the device through the device name (/dev/fb0 ). The linux Device Driver framework finds the device driver interface set fb_fops corresponding to the device through the master device 29. For the analysis process of the Linux driver framework, see Linux character device driver analysis. Next, the driver framework of the linux device will call fb_open of fb_fops. Let's follow up:
This interface is very simple, that is, find the corresponding fb_info data structure in the Global Array registered_fb and set it to the private data of the file pointer, after that, the user can directly find the corresponding fb_info by accessing and calling other interfaces such as mmap and ioctl. In the end, fb_info-> fb_ops-> fb_open will also be called. However, this interface is generally not dry and can be assigned NULL.
Therefore, the internal driving framework of the framebuffer subsystem is responsible for finding the corresponding device (fb_info of the specific LCD operation interface) from the device number ).
5. Driver framework Summary
After the above analysis, this figure should be easy to understand.
Iii. mmap ing
The most important function of the framebuffer driver is to provide users with an interface (mmap) to map the process space to the actual display physical memory ). When both the user image data buffer and the kernel virtual address space buffer correspond to the same physical memory, when the resource data is copied to the user image data buffer, it is directly copied to the display physical memory.
The mmap ing interface at the application layer is called at the following levels:
1. sys_mmap
Sys_mmap is the ing Implementation of the virtual file system layer. It will apply for a virtual memory in the virtual space of the user process (0 to 3 GB), and finally call the fb_mmap of the framebuffer subsystem, and pass the vm_area_struct struct pointer to it, which includes the memory address information of the process user space.
The Vfs virtual file system is an important part of the linux system. It is not expanded here and will be analyzed later.
2. fb_mmap
To trace:
This interface also finds the specific LCD operation interface set corresponding to the device and calls the fb_mmap interface.
Let's select a specific LCD interface implementation:
The remap_pfn_range interface establishes a ing between the process address space (0 to 3 GB) and the actual display physical memory. Among them, LCD _mem is applied for when the fb_info struct is initialized using kzmalloc, which represents the first address of the virtual memory in the kernel state (3G to 4G. Virt_to_phys converts a virtual address to a physical address, updates data members of vm_area_struct, and affects the page table settings of the process and MMU settings.
After the interface is completed, the first address (0 to 3G) of the process space is returned to the application layer ). In this way, the application layer can directly access this memory for read/write operations. It directly accesses the actual physical address through MMU without the need for driver management.
Iv. Interface Usage
As follows, is the operation very simple? However, you should remember that framebuffer is only responsible for bitmap display, while many images are encoded, such as JPG, which must be decoded before being sent to framebuffer.
After writing so much, I found that I wrote another article on how to develop the LCD driver to implement the first part of framebuffer requirements and to analyze the previous driver framework, the background of the LCD driver and framebuffer subsystem will be more clearly understood. This can be arranged after the analysis of the platform device driver, because most of the drivers are platform device drivers. We should first talk about the platform device driver framework and then go back to the LCD driver development, including the relevant components of the LCD platform device (GPIO resources, interrupt resource configuration, etc.) and LCD driver components.