Bulk Driver Design for USB devices

Source: Internet
Author: User
Tags semaphore

Bulk Driver Design for USB devices



Universal Serial Bus (USB) is a serial interface with automatic configuration and good compatibility. It simplifies the connection between computers and peripherals and is widely accepted by computer peripherals hardware manufacturers. After the USB bus standard is upgraded from version 1.1 to version 2.0, the transmission rate increases from 12 Mbps to 480 Mbps, which is more suitable for high-speed data transmission. When USB devices support printers, scanners, digital cameras, and other peripherals, drivers must use Bulk mode for high-speed data transmission due to the large amount of data transmitted between these peripherals and the host.

Overall structure of USB device driver

The overall structure of the USB device driver consists of the following five main components: USB application interface, USB device driver function, USB interrupt service program, USB callback interface program, and USB standard event processing program.

 USB application interface

The main functions of the USB application interface are to initialize the hardware and software of the USB driver, open the port, close the port, read port, write port, and port control. When the device drive is mounted to the system device table, the I/O system calls this application interface.

Functions contained in a routine of a USB application interface:
· USB _ Init () -- installation and initialization of USB port driver functions and hardware configuration.
· USB _ open () -- open the USB port.
· USB _ close () -- disable the USB port.
· USB _ read ()-read the USB port.
· USB _ write () -- write the USB port.
· USB _ ioctl ()-I/O control the USB device.
USB device driver Function
  1. usb_init () -- initialize the USB port
The usb_init function initializes a specific USB port drive for hardware and software configuration.
The initialization steps are as follows:
(1) install the USB drive to the I/O system device table.
(2) obtain the interrupt number used by the USB controller.
(3) obtain system resources required for each port, including memory, semaphores, and message queues.
(4) initialize the USB drive data structure and USB port status register.
(5) Start the USB standard event handler.
(6) Enable the minimum support set for control port 0 and USB interrupt.
  2. usb_open () -- open the USB port

The usb_open function allows the application to open a USB port and select the DMA data transmission mode.

The typical steps for executing an open call are as follows:

(1) If it is not the default control port 0, check whether the port status is "configured ".

(2) If the port cannot be opened multiple times, make sure the port is not opened.

(3) check whether the port is valid for the selected interface.

(4) Select DMA transmission and set the DMA controller to use the FIFO port as the destination address.

(5) set the port to open.

  3. usb_close () -- disable the USB port

The usb_close function allows the USB application to close a port and disable the DMA channel.

The typical steps for disabling a call are as follows:

(1) disable the DMA channel and discard the use of the port on the DMA controller.

(2) set the port to disabled.

  4. usb_read () -- read the USB port

The usb_read function allows USB applications to read data from the output port or control port.

Typical steps for calling a READ function are:

(1) set the port number, type, and direction.

(2) Confirm that the port is open.

(3) set a port semaphore to avoid multiple calls.

(4) Call readdma () for DMA write operations. The execution steps in the function are as follows:

(A) Start the DMA to receive FIFO read operations from the port and transmit data from the port to the memory.

(B) wait until the DMA is interrupted. (When a DMA interrupt is sent to the port message queue, the data transmission is completed .)

(C) Repeat ()~ (C) Step until all data is received or the transfer ends on the USB host. If the USB controller detects a short packet interruption or zero-byte packet, or waits for a timeout, the DMA transfer is stopped and executed in step (d.

(D) Send a zero-byte packet to the USB host to complete the control status step.

(5) release the port semaphore.

(6) return the number of received bytes or error message.

5. usb_write () -- write the USB port

The usb_write function allows USB applications to write data to the input port or control port.

Typical steps for executing a write call are:

(1) confirm the port number, type, and direction.
(2) Confirm that the port is open.

(3) obtain the port semaphore to avoid multiple calls.

(4) Call writedma () for DMA write. The execution steps in the function are as follows:

(A) DMA transfers the memory data to the FIFO of the target port and waits for the DMA to be interrupted.

(B) If a zero-byte packet is received from the receiving FIFO to the USB host, or a waiting timeout occurs, the system exits and returns the number of transmitted bytes.

(C) Repeated execution ()~ (C) Go to step (d) until all data is transferred.

(D) forcibly send the last Zero-byte package or short package to the USB host to end the transmission process.

(5) If it is a control port, wait for the Zero-byte packet from the USB host to complete the control status step.

(6) release the port semaphore.

(7) return the number of transmitted bytes or error message.

6. usb_ioctl () -- perform I/O control on the USB device

The usb_ioctl function sets the Port Status Register and implements the I/O port control function.

The USB application calls the controlioctl () and epioctl () functions that provide the application's ability to control the USB interface according to different control objects. The controlioctl () function executes the overall I/O output control function of the USB controller. The epioctl () function executes the I/O control function for individual USB ports ..

1) controlioctl () -- Controller Control Function

The controlioctl () function controls the USB controller. Obtain the USB controller semaphore before performing the I/O function to avoid multiple calls affecting the port on which data is being transferred. Release the semaphore after completing the I/O operation.

The following control functions should be supported by the USB controller:

· Supports remote wakeup.

· Set the USB port to enter/exit the pending status.

· Reset the USB port.

· Set the timeout value for the message queue of the USB controller.

· Select Port FIFO for DMA.

· Allows/disables optional interruptions.

· Read the start position of the frame timestamp.

· Perform enumeration testing.

· The interface, backup interface, and current USB port configuration status are returned.

2) epioctl () -- Port Control Function

The epioctl () function controls the port. Obtain the USB controller semaphore before performing the I/O function to avoid multiple calls affecting the port on which data is being transferred. Release the semaphore after completing the I/O operation.

The port should support the following control functions:

(1) obtain the USB port status.

(2) set the USB port to enter/exit the blocking status.

(3) set the timeout for message queue on the USB port.


 USB interrupt service program

The USB controller causes a single interruption and multiple ports are shared. Each port causes ACK, Nack, or error interruptions. The output port generates zero-byte packets or short packets. Control port 0 is interrupted when receiving the set package. The USB controller causes USB event interruptions, such as frame start (SOF), suspension, recovery, and reset.

The USB interrupt service program performs the following steps:

(1) identify the type of USB interrupt.

(2) Clear the conditions for interruption.

(3) read the USB Status Register to obtain the current configuration, interface, or frame start timestamp status information.

(4) send a message to the receiving message queue of the USB controller message queue or callback function.

 USB standard event handler

After the USB drive is initialized, start the USB standard event handler to handle the enumeration process and asynchronous USB events.

The event handler uses the control port 0 until the enumeration process is completed. When a USB application is inactive, ports other than control port 0 are not accessible. The event handler executes control operations on port 0, responds to USB standard requests, and notifies the USB application of completing enumeration and Interface Activity Status, USB events are transmitted to the USB Peripheral application through the callback interface. After the USB port enumeration is completed, the USB application can open and use the USB port.

The execution process of a USB task is as follows:

(1) read the message queue of the USB controller.

(2) If the configuration package is received, the standard request processing function is called.

(3) If an event is received, the USB event handler is called.

(4) determine the current status and valid configurations/interfaces.

(5) update the data structure of the USB controller and port.

(6) Repetition (1 )~ (5) step.

USB callback interface program

The callback application interface is an interface that provides feedback to the application, including a message that notifies the application of a USB event, such as reset, configuration change, interface change, suspension, recovery, and frame start. Applications that use USB need to respond to these messages in the following ways:

(1) reset and close the port and wait for the enumeration test.

(2) Change the configuration to close the port and press the new configuration to open the port.

(3) change the interface to close the port and open the port from the new interface.

(4) suspended to low power consumption mode.

(5) Resume and exit the low power consumption mode.

(6) the process specified by the application is executed at the beginning of the frame.


This article provides a process of designing the USB port bulk driver to achieve high-speed data transmission between the USB interface device and the host machine, high-speed data transmission for USB devices in embedded and windows multi-threaded Environments

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.