Five things to do in life: 1. Fall in love, get married, and have children. 2. Try doing something you love for once. 3. Give back. 4. Have a faith. 5. Leave a legacy.

purpose

The purpose of writing this article is a record in recent study android and communications USB_HID equipment of the knowledge learned, found that while android has sealed inside a method to give us a call, but there are some parameters we don’t know about the USB knowledge is don’t know how to get there, how to communicate with hardware engineers over there, what kind of parameters, So you don’t know what is the actual parameters of method need, hardware engineer there and don’t know what I want to express, so, even as a software engineer, it is necessary to understand the knowledge of the USB or, then I’ll explain the knowledge I need to know the development process of USB, behind will have a concrete implementation of articles, knife don’t miss your job, Let’s take a look at some of the basics of USB, and I’ll highlight which parts of it can be used for Android development.

directory

Next, I will describe the following table of contents. If the contents of the table of contents are already known to the reader, you can skip them and watch them selectively. USB descriptors 2.1 Functions of USB descriptors 2.2 USB Descriptors and their Relationships 3. USB request format 4. USB Transmission mode 4.1 Control Transfer 4.2 Bulk Transfer 4.3 Interrupt Transfer 4.4 Isochronous Transfer 4.5 Comparison of the Four Transmission modes What is HID class device 5.1 HID class request

First, know USB

USB, short for Universal Serial Bus, is a Serial Bus standard that connects PCS to peripheral devices. Used to standardize the connection and communication between computers and external devices, mainly applied in the PC field, related products include mouse, keyboard, gamepad, photographic equipment and so on.

Second, USB descriptor

It can be understood that the descriptor is a kind of “ID card”, USB HOST is to identify the device through a variety of descriptors. The categories of descriptors are device descriptors, configuration descriptors, interface descriptors, endpoint descriptors, and string descriptors. And each descriptor has its own independent number. As shown in the figure below.



2.1 Functions of each descriptor

Device descriptor: Defines the standard device descriptor structure. A device has only one device descriptor.



PID and VID are what we need to learn from hardware engineers. Each company has a specific VID, and Android needs these two things to find the device we want.

(2) Configuration descriptors: Define the standard configuration information of the device. A device can have multiple configuration descriptors.



③ Interface descriptors: Describes the configurations provided by interfaces. The number of interfaces owned by a configuration is determined by the bNumInterfaces of the configuration descriptors.



(4) Endpoint descriptors: Each endpoint on the USB device has its own endpoint descriptor, which is determined by the number of bNumEndpoint in the interface descriptor.



⑤ String descriptors: String descriptors are optional. If string descriptors are not supported, all string descriptor indexes in the device, configuration, and interface descriptors must be 0.



2.2 USB descriptors and their relationships

① Configuration and interfaces are abstracted concepts for managing endpoints

② The same endpoint number can be used in different configurations

③ The same endpoint number cannot appear on different interfaces in the same configuration (except endpoint 0).

(4) A device is a collection of configurations, configurations are a collection of interfaces, and interfaces are a collection of endpoints.



Three, USB request format

There are some formats we need to pay attention to in the process of development, I will specially explain. The request format is as follows:

Parameter Description:

① bmRequestType: specifies the request feature

D7- Direction of data transmission. Zero: the host – > device, 1: device – > host;

D6 to D5- Request type. 0: standard request, 1: class request, 2: vendor-defined request, 3: reserved.

D4 to D0- Request objects. 0: device, 1: interface, 2: endpoint.

Therefore, for HID device class requests, there are only 10100001 and 00100001. And notice, you have to remember these two binary numbers, which you’re going to use when you’re communicating with each other, which we’ll talk about later.

② bRequest: Define the request type

Standard requests for USB devices are as follows:

GET_DESCRIPTOR: The request most commonly used in the enumeration process by which the host reads the various descriptors of the device.

SET_ADDRESS: the host requests the device to use the specified address.

SET_CONFIGURATION: used by the host to set the current configuration.

GET_CONFIGURATION: the host obtains the current configuration value.

CLEAR_FEATURE: Used to clear a specified feature (such as who provides Power for the Device and whether wakeup is supported).

SET_FEATURE: Used to set or enable a feature.

GET_STATUS: Returns the status of the indicated receiver.

③ mValue: This parameter takes up two bytes. The high byte and the low byte have different meanings.

The high byte indicates the type of descriptor:

0 x21: HID descriptor

0x22: Report descriptor

0x23: Physical descriptor

Low bytes not 0 are used to select the physical descriptor.

This high byte indicates the report type when HID communication uses SET_REPORT and GET_REPORT requests: 0x01 — input, 0x02 — output, 0x03 — feature, and Other — reserve. The low byte represents the Report ID. For the Report ID, in Android development, we need to know what we need to know when we use these two requests for control transfer (if we use feature reports). This can be asked by the hardware engineer. Note, however, that the high and low values of this position are reversed. For example, if you use 0x0203 on a computer, you can achieve communication, but not on Android, you have to change it to 0x0302.

④ wIndex: This parameter also takes up two bytes and has different meanings according to different requests.

This will also be highlighted later when HID communication uses SET_REPORT and GET_REPORT requests. But one thing to mention is the slogan, when the equipment transmission on the computer, choose the slogan in the device no words will step by step to the interface 0 to look for, that is to say, I choose 3 for data transmission interface on the computer, but not defined in the equipment interface. 3, then automatically find interface 2, interface did not seek interface 1, 2 until you find the interface 0 for transmission, But android is not automatically to find, this must ask the hardware side, otherwise you do not even know where the problem is.

⑤wLength: indicates the data length.

4. Four transmission modes of USB

The transmission modes include Control Transfer, Bulk Transfer, Interrupt Transfer and Isochronous Transfer. Let’s take a look at what these four transports are, and then summarize the comparison of the four transports.

4.1 Control Transfer

Function: USB system software is used to query configuration and send universal commands to USB devices.

Features: Control transmission is two-way transmission, the amount of data is usually small; Data transfer is nondestructive. Data width: Control transmission mode has 8, 16, 32, 64 bytes of data. Typical application: Transfer between endpoint 0(EP0) between the main computer and USB peripherals. The control transfer that Android uses later also uses endpoint 0.

4.2 Bulk Transfer

Role: in the need for a large amount of data transmission and receiving data at the same time there is no bandwidth and interval time requirements can be applied.

Features: Guaranteed transmission is required, suitable for very slow transmission and large amounts of delayed transmission, data can be transmitted and received after all other types of data transmission is completed. Printers and scanners are examples of this type of transport.

4.3 Interrupt Transfer

Function: Periodically queries whether the device has interrupted data to be transmitted.

Features: the structure of the device’s endpoint schemer determines its query frequency from 1 to 255ms. This transport mode is one-way, with only input mode for hosts. The mouse is a device of this type of transport, and the hardware I tested was defined as a mouse.

4.4 Isochronous Transfer

Purpose: For time-critical and fault-tolerant streaming data transmission, or for real-time applications requiring constant data transfer rates.

Features: Ensure the synchronization of transmission, ensure that there is a fixed amount of transmission per second. Note that synchronous transmission allows a certain bit error rate. The typical application is voip, which performs instant calls.

4.5 Comparison of four Transmission modes



Five, what is HID class device

HID class devices belong to human-computer interaction devices, enumerating devices into HID devices can complete communication without developing drivers, using the OPERATING system’s own HID class drivers.

5.1 HID class requests

The HID protocol defines 6 specific requests for HID classes :(note that this is what we need to know when developing USB_HID devices)

0x01:GET_REPORT- The host receives data from the device with a control transport, and all HID devices must support this request.

0x02:GET_IDLE- The host reads the current idle rate of the device. The device may not support this request.

0x03:GET_PROTOCOL- Applies only to HID devices (Boot Device) that support the Boot function.

0x09:SET_REPORT- The device uses the control to transmit and receive data from the host. The device may not support this request.

0x0A:SET_IDLE- Sets the idle state. The device does not support this request.

0x0B:SET_PROTOCOL- Applies only to HID devices that support Boot (Boot Device).

The parameter Settings of the six request types will be explained later, and the communication with HID devices will be realized through SET_REPORT and GET_REPORT.



Because HID device is the focus of our grasp, the following are a few HID specific request parameters. (Note that these parameters are related to calling control transport methods in Android, so it is important to know.)

GET_REPORT: The host gets a Report from the control endpoint.



SET_REPORT: The host sends a Report to the device to set input,output, or feature.



GET_IDLE: the host reads the idle rate of the device. The device may not support this request.



SET_IDLE: Sets the idle state. The device does not support this request.



GET_PROTOCOL: applies only to HID devices (Boot devices) that support the Boot function.



SET_PROTOCOL: applies only to HID devices (Boot devices) that support the Boot function.



conclusion

The above is I in the development of Android and USB_HID device communication learned and understand the knowledge of USB, as a record, next week I will be on Android how to achieve the process of writing an article published, if there is anything wrong, welcome to point out, if useful to you modify O(∩_∩)O, click a like ~