Linux driver development-I/O memory access process, driver development-I

Source: Internet
Author: User

Linux driver development-I/O memory access process, driver development-I

A device usually provides a set of registers to control the device, read/write devices, and obtain the device status, including the control registers, data registers, and status registers, which may be in the I/O space, it may also be in the memory space. When it is in the I/O space, it is usually called the I/O port. When it is in the memory space, the corresponding memory space is called the I/O memory (usually unified addressing)

1. For I/O Ports

There are dedicated functions to read data on the port, such as reading and writing the byte port (8 byte width)

Unsigned inb (unsigned port );

Unsigned outb (unsigned char byte, unsignedport );

2. For I/O memory

Before accessing the I/O memory in the kernel, you must use the ioremap () function to map the physical address of the device to the virtual address. The ioremap () prototype is as follows:

Void * ioremap (unsigned long offset, unsignedlong size );

Similar to vmalloc (), Ioremap () also needs to create a new page table, but it does not allocate memory in vmalloc. Ioremap () returns a special virtual address that can be used to access a specific physical address range. The virtual address obtained through ioremap () should be released by the iounmap () function,

Void iounmap (void * addr );

After the physical address of the device is mapped to a virtual address, you can directly access these addresses through pointers, however, you can use the following functions of the Linux kernel to read and write virtual addresses mapped to the device memory. These functions are as follows:

A) read I/O memory

Unsigned intioread8 (void * addr );

Unsigned intioread16 (void * addr );

Unsigned intioread32 (void * addr );

B) write I/O memory

Void iowrite8 (u8value, void * addr );

Void iowrite16 (u16value, void * addr );

Void iowrite32 (u32value, void * addr );

3. map I/O ports to memory space

Void * ioport_map (unsigned long port, unsigned int count );

Using this function, you can remap the count consecutive I/O ports starting with port to a "memory space". Then, you can access these I/O ports at the address they return, just like accessing the I/O memory. If this ing is not required, you need to call the following function to cancel the ing.

Voidioport_unmap (void * addr );

The ing of Ioport_map () to memory space is actually an illusion for developers and is not mapped to the kernel virtual address, only to allow engineers to use the same I/O memory access interface to access the I/O port

 

4. Apply for and release device I/O Ports and I/O memory

I/O port application

Struct resource * request_region (unsignedlong first, unsigned long n, const char * name );

This function applies to the kernel for n ports. These ports start from first and the name parameter is the device name. If the allocation is successful, the return value is not NULL.

Void relese_region (unsigned longstart, unsigned long n );

I/O memory Application

Struct resoutce * request_mem_region (unsignedlong start, unsigned long len, char * name );

Apply for n memory addresses from the kernel. These addresses start from first and the name parameter is the device name.

Void relese_mem_region (unsigned longstart, unsigned long len );

5. device I/O port and I/O memory access process

A) I/O port access method:

Directly use the I/O port operation function to apply for the I/O port when the device is enabled or the driver module is loaded, and then use inb () outb () for access, finally, release the I/O port range when the device is disabled or the driver is uninstalled.

Another way is to map the I/O port to memory for access. when the device is turned on or the driver module is loaded, apply for the I/O port area and map it To the memory using ioport_map, then use the I/O memory function for port access. Finally, release the I/O and use the ing when the device is disabled or the driver is detached.

C) I/O Memory Access Method

First, use request_mem_region () to apply for resources, and then map the Register address to the virtual address of the kernel space through ioremap (). Then, you can access the registers of these devices through the linux Device Access programming interface, after the access is complete, use the Virtual Address requested by ioremap () to release and release the I/O memory resources applied by relese_mem_region ().

 

6. Map device addresses to user Spaces

Generally, the user space cannot or should not directly access the device. However, the mmap () function can be implemented in the device driver. This function allows the user space to directly access the physical address of the device, mmap () associates a memory segment of the user space with the device memory. When the user accesses the user space, the address segment is actually converted to the access to the device. If the user space can directly access the video memory through memory ing, the pixels at each point in the screen frame no longer need a replication process from the user space to the kernel space, mmap () paging must be performed in PAGE_SIZE units

From the file_operatoions file operation structure, we can see that the prototype of the mmap () function in the driver is as follows:

Int (* mmap) (struct file *, structvm_area_struct *);

User calls:

Caddr_t mmap (caddr_t addr, size_t len, intport, int flags, int fd, off_t offset );

When you call mmap (), the Kernel performs the following operations:

Search for a VMA in the virtual space of the process

Map this VMA

If the device driver or file_operations of the file system defines the mmap () operation, call it

Insert the vma into the VMA linked list of the process.

The first parameter of the mmap () function in File_operations is the VMA found in step 1.

For mmap () in file_operations, other systems need to call remap_pfn_range ()

 

Most device drivers do not need to provide device memory-to-user space ing capabilities, because it is meaningless to implement this ing for stream-oriented devices such as serial ports, for devices such as display and video, setting up ing can reduce memory copies between user space and kernel space.

Reference: linux device driver development details

 

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.