1# USB DDK Development 2 3## When to Use 4 5USB Driver Development Kit (USB DDK) is a tool kit provided by OpenHarmony to help you develop USB device drivers for your applications based on the user mode. It provides a series of device access APIs, which implement functions such as opening and closing USB interfaces, performing non-isochronous and isochronous data transfer, and implementing control transfer and interrupt transfer over USB pipes, etc. 6 7## Available APIs 8 9| Name| Description| 10| -------- | -------- | 11| OH_Usb_Init(void) | Initializes the DDK.| 12| OH_Usb_Release(void) | Releases the DDK.| 13| OH_Usb_GetDeviceDescriptor(uint64_t deviceId, struct UsbDeviceDescriptor *desc) | Obtains the device descriptor.| 14| OH_Usb_GetConfigDescriptor(uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor **const config) | Obtains the configuration descriptor. To avoid memory leakage, use **OH_Usb_FreeConfigDescriptor()** to release a descriptor after use.| 15| OH_Usb_FreeConfigDescriptor(const struct UsbDdkConfigDescriptor *const config) | Releases a configuration descriptor. To avoid memory leakage, release a descriptor after use.| 16| OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle) | Declares a USB interface.| 17| OH_Usb_ReleaseInterface(uint64_t interfaceHandle) | Releases a USB interface.| 18| OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap) | Sends a pipe request. This API works in a synchronous manner. It applies to interrupt transfer and bulk transfer.| 19| OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap) | Creates a buffer. To avoid memory leakage, use **OH_Usb_DestroyDeviceMemMap()** to destroy a buffer after use.| 20| OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap) | Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use.| 21 22For details about the APIs, see [USB DDK](../reference/native-apis/_usb_ddk.md). 23 24## How to Develop 25 26To develop a USB driver using the USB DDK in OpenHarmony, perform the following steps: 27 281. Obtain the device descriptor. Initialize the DDK by calling **OH_Usb_Init** of **usb_ddk_api.h**, and obtain the device descriptor by calling **OH_Usb_GetDeviceDescriptor**. 29 30 ```c++ 31 // Initialize the USB DDK. 32 OH_Usb_Init(); 33 struct UsbDeviceDescriptor devDesc; 34 uint64_t deviceId = 0; 35 // Obtain the device descriptor. 36 OH_Usb_GetDeviceDescriptor(deviceId, &devDesc); 37 ``` 38 392. Obtain the configuration descriptor, and declare the USB interface. Obtain the configuration descriptor **config** by calling **OH_Usb_GetConfigDescriptor** of **usb_ddk_api.h**, and declare the USB interface by calling **OH_Usb_ClaimInterface**. 40 41 ```c++ 42 struct UsbDdkConfigDescriptor *config = nullptr; 43 // Obtain the configuration descriptor. 44 OH_Usb_GetConfigDescriptor(deviceId, 1, &config); 45 // Obtain the index of the target USB interface based on the configuration descriptor. 46 uint8_t interfaceIndex = 0; 47 // Declare the USB interface. 48 uint64_t interfaceHandle = 0; 49 OH_Usb_ClaimInterface(deviceId, interfaceIndex, &interfaceHandle); 50 // Release the configuration descriptor. 51 OH_Usb_FreeConfigDescriptor(config); 52 ``` 53 543. Create a device memory map, and send a request. Create the device memory map **devMmap** by calling **OH_Usb_CreateDeviceMemMap** of **usb_ddk_api.h**, and send a request by calling **OH_Usb_SendPipeRequest**. 55 56 ```c++ 57 struct UsbDeviceMemMap *devMmap = nullptr; 58 // Create a buffer for storing data. 59 size_t bufferLen = 10; 60 OH_Usb_CreateDeviceMemMap(deviceId, bufferLen, &devMmap); 61 struct UsbRequestPipe pipe; 62 pipe.interfaceHandle = interfaceHandle; 63 // Obtain the target endpoint based on the configuration descriptor. 64 pipe.endpoint = 128; 65 pipe.timeout = UINT32_MAX; 66 // Send a request. 67 OH_Usb_SendPipeRequest(&pipe, devMmap); 68 ``` 69 704. Release resources. After all requests are processed and before the application exits, destroy the buffer by calling **OH_Usb_DestroyDeviceMemMap** of **usb_ddk_api.h** to release resources. Release the USB interface by calling **OH_Usb_ReleaseInterface**. Release the USB DDK by calling **OH_Usb_Release**. 71 72 ```c++ 73 // Destroy the buffer. 74 OH_Usb_DestroyDeviceMemMap(devMmap); 75 // Release the USB interface. 76 OH_Usb_ReleaseInterface(interfaceHandle); 77 // Release the USB DDK. 78 OH_Usb_Release(); 79 ``` 80