1e41f4b71Sopenharmony_ci# USB DDK Development 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe USB Driver Development Kit (USB DDK) is a toolset that helps you develop USB device drivers at the application layer based on the user mode. It provides a series of device access APIs, for example, opening and closing USB interfaces, and performing non-isochronous transfer, isochronous transfer, control transfer, and interrupt transfer over USB pipes. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Constraints 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci* The open APIs of the USB DDK can be used to develop drivers of non-standard USB peripherals. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci* The open APIs of the USB DDK can be used only within the DriverExtensionAbility lifecycle. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci* To use the open APIs of the USB DDK, you need to declare the matching ACL permissions in **module.json5**, for example, **ohos.permission.ACCESS_DDK_USB**. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Available APIs 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci| Name| Description| 18e41f4b71Sopenharmony_ci| -------- | -------- | 19e41f4b71Sopenharmony_ci| OH_Usb_Init(void) | Initializes the USB DDK.| 20e41f4b71Sopenharmony_ci| OH_Usb_Release(void) | Releases the USB DDK.| 21e41f4b71Sopenharmony_ci| OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc) | Obtains a device descriptor.| 22e41f4b71Sopenharmony_ci| OH_Usb_GetConfigDescriptor(uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor **const config) | Obtains a configuration descriptor. To avoid memory leakage, use **OH_Usb_FreeConfigDescriptor()** to release a descriptor after use.| 23e41f4b71Sopenharmony_ci| OH_Usb_FreeConfigDescriptor(const struct UsbDdkConfigDescriptor *const config) | Releases a configuration descriptor. To avoid memory leakage, release a descriptor in time after use.| 24e41f4b71Sopenharmony_ci| OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle) | Declares a USB interface.| 25e41f4b71Sopenharmony_ci| OH_Usb_SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex) | Activates the alternate setting of a USB interface.| 26e41f4b71Sopenharmony_ci| OH_Usb_GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t \*settingIndex) | Obtains the alternate setting of a USB interface.| 27e41f4b71Sopenharmony_ci| OH_Usb_SendControlReadRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup \*setup, uint32_t timeout, uint8_t \*data, uint32_t \*dataLen) | Sends a control read transfer request. This API returns the result synchronously.| 28e41f4b71Sopenharmony_ci| OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct UsbControlRequestSetup \*setup, uint32_t, const uint8_t \*data, uint32_t dataLen) | Sends a control write transfer request. This API returns the result synchronously.| 29e41f4b71Sopenharmony_ci| OH_Usb_ReleaseInterface(uint64_t interfaceHandle) | Releases a USB interface.| 30e41f4b71Sopenharmony_ci| OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap) | Sends a pipe request. This API returns the result synchronously. It applies to interrupt transfer and bulk transfer.| 31e41f4b71Sopenharmony_ci| OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap) | Creates a buffer. To avoid resource leakage, use **OH_Usb_DestroyDeviceMemMap()** to destroy a buffer after use.| 32e41f4b71Sopenharmony_ci| OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap) | Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use.| 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciFor details about the APIs, see [USB DDK](../reference/apis-driverdevelopment-kit/_usb_ddk.md). 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci## How to Develop 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciTo develop a USB driver using the USB DDK, perform the following steps: 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**Adding Dynamic Link Libraries** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ciAdd the following library to **CMakeLists.txt**: 43e41f4b71Sopenharmony_ci```txt 44e41f4b71Sopenharmony_cilibusb_ndk.z.so 45e41f4b71Sopenharmony_ci``` 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Including Header Files** 48e41f4b71Sopenharmony_ci```c++ 49e41f4b71Sopenharmony_ci#include <usb/usb_ddk_api.h> 50e41f4b71Sopenharmony_ci#include <usb/usb_ddk_types.h> 51e41f4b71Sopenharmony_ci``` 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci1. Obtains a device descriptor. 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci Call **OH_Usb_Init** of **usb_ddk_api.h** to initialize the USB DDK, and call **OH_Usb_GetDeviceDescriptor** to obtain the device descriptor. 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci ```c++ 58e41f4b71Sopenharmony_ci // Initialize the USB DDK. 59e41f4b71Sopenharmony_ci OH_Usb_Init(); 60e41f4b71Sopenharmony_ci struct UsbDeviceDescriptor devDesc; 61e41f4b71Sopenharmony_ci uint64_t deviceId = 0; 62e41f4b71Sopenharmony_ci // Obtain the device descriptor. 63e41f4b71Sopenharmony_ci OH_Usb_GetDeviceDescriptor(deviceId, &devDesc); 64e41f4b71Sopenharmony_ci ``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci2. Obtain a configuration descriptor, and declare the USB interface. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci Call **OH_Usb_GetConfigDescriptor** of **usb_ddk_api.h** to obtain the configuration descriptor **config**, and call **OH_Usb_ClaimInterface** to declare claiming of the USB interface. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci ```c++ 71e41f4b71Sopenharmony_ci struct UsbDdkConfigDescriptor *config = nullptr; 72e41f4b71Sopenharmony_ci // Obtain the configuration descriptor. 73e41f4b71Sopenharmony_ci OH_Usb_GetConfigDescriptor(deviceId, 1, &config); 74e41f4b71Sopenharmony_ci // Obtain the index of the target USB interface based on the configuration descriptor. 75e41f4b71Sopenharmony_ci uint8_t interfaceIndex = 0; 76e41f4b71Sopenharmony_ci // Declare the USB interface. 77e41f4b71Sopenharmony_ci uint64_t interfaceHandle = 0; 78e41f4b71Sopenharmony_ci OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle); 79e41f4b71Sopenharmony_ci // Release the configuration descriptor. 80e41f4b71Sopenharmony_ci OH_Usb_FreeConfigDescriptor(config); 81e41f4b71Sopenharmony_ci ``` 82e41f4b71Sopenharmony_ci3. Obtain the activated alternate setting of a USB interface. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci Call **OH_Usb_GetCurrentInterfaceSetting** of **usb_ddk_api.h** to obtain the alternate setting, and call **OH_Usb_SelectInterfaceSetting** to activate it. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci ```c++ 87e41f4b71Sopenharmony_ci uint8_t settingIndex = 0; 88e41f4b71Sopenharmony_ci // Obtain the alternate setting. 89e41f4b71Sopenharmony_ci OH_Usb_GetCurrentInterfaceSetting(interfaceHandle, &settingIndex); 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci // Activate the alternate setting. 92e41f4b71Sopenharmony_ci OH_Usb_SelectInterfaceSetting(interfaceHandle, &settingIndex); 93e41f4b71Sopenharmony_ci ``` 94e41f4b71Sopenharmony_ci4. Send control read requests and control write requests. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci Call **OH_Usb_SendControlReadRequest** of **usb_ddk_api.h** to send a control read request, or call **OH_Usb_SendControlWriteRequest** to send a control write request. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci ```c++ 99e41f4b71Sopenharmony_ci // Timeout interval. Set it to 1s. 100e41f4b71Sopenharmony_ci uint32_t timeout = 1000; 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci struct UsbControlRequestSetup setupRead; 103e41f4b71Sopenharmony_ci setupRead.bmRequestType = 0x80; 104e41f4b71Sopenharmony_ci setupRead.bRequest = 0x08; 105e41f4b71Sopenharmony_ci setupRead.wValue = 0; 106e41f4b71Sopenharmony_ci setupRead.wIndex = 0; 107e41f4b71Sopenharmony_ci setupRead.wLength = 0x01; 108e41f4b71Sopenharmony_ci uint8_t dataRead[256] = {0}; 109e41f4b71Sopenharmony_ci uint32_t dataReadLen = 256; 110e41f4b71Sopenharmony_ci // Send a control read request. 111e41f4b71Sopenharmony_ci OH_Usb_SendControlReadRequest(interfaceHandle, &setupRead, timeout, dataRead, &dataReadLen); 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci struct UsbControlRequestSetup setupWrite; 114e41f4b71Sopenharmony_ci setupWrite.bmRequestType = 0; 115e41f4b71Sopenharmony_ci setupWrite.bRequest = 0x09; 116e41f4b71Sopenharmony_ci setupWrite.wValue = 1; 117e41f4b71Sopenharmony_ci setupWrite.wIndex = 0; 118e41f4b71Sopenharmony_ci setupWrite.wLength = 0; 119e41f4b71Sopenharmony_ci uint8_t dataWrite[256] = {0}; 120e41f4b71Sopenharmony_ci uint32_t dataWriteLen = 256; 121e41f4b71Sopenharmony_ci // Send a control write request. 122e41f4b71Sopenharmony_ci OH_Usb_SendControlWriteRequest(interfaceHandle, &setupWrite, timeout, dataWrite, &dataWriteLen); 123e41f4b71Sopenharmony_ci ``` 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci5. Create a buffer, and send a request. 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci Call **OH_Usb_CreateDeviceMemMap** of **usb_ddk_api.h** to create the buffer **devMmap**, and call **OH_Usb_SendPipeRequest** to send a request. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci ```c++ 130e41f4b71Sopenharmony_ci struct UsbDeviceMemMap *devMmap = nullptr; 131e41f4b71Sopenharmony_ci // Create a buffer for storing data. 132e41f4b71Sopenharmony_ci size_t bufferLen = 10; 133e41f4b71Sopenharmony_ci OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMmap); 134e41f4b71Sopenharmony_ci struct UsbRequestPipe pipe; 135e41f4b71Sopenharmony_ci pipe.interfaceHandle = interfaceHandle; 136e41f4b71Sopenharmony_ci // Obtain the target endpoint based on the configuration descriptor. 137e41f4b71Sopenharmony_ci pipe.endpoint = 128; 138e41f4b71Sopenharmony_ci pipe.timeout = UINT32_MAX; 139e41f4b71Sopenharmony_ci // Send a request. 140e41f4b71Sopenharmony_ci OH_Usb_SendPipeRequest(&pipe, devMmap); 141e41f4b71Sopenharmony_ci ``` 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci6. Release resources. 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci After all requests are processed and before the application exits, call **OH_Usb_DestroyDeviceMemMap** of **usb_ddk_api.h** to destroy the buffer, call **OH_Usb_ReleaseInterface** to release the USB interface, , and call **OH_Usb_Release** to release the USB DDK. 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci ```c++ 148e41f4b71Sopenharmony_ci // Destroy the buffer. 149e41f4b71Sopenharmony_ci OH_Usb_DestroyDeviceMemMap(devMmap); 150e41f4b71Sopenharmony_ci // Release the USB interface. 151e41f4b71Sopenharmony_ci OH_Usb_ReleaseInterface(interfaceHandle); 152e41f4b71Sopenharmony_ci // Release the USB DDK. 153e41f4b71Sopenharmony_ci OH_Usb_Release(); 154e41f4b71Sopenharmony_ci ``` 155