diff --git a/en/device-dev/driver/driver-peripherals-usb-des.md b/en/device-dev/driver/driver-peripherals-usb-des.md index 0dd421ec369c8ff8d4deaee0560608a2090d6fef..5f53fc9ff007afb9f5b471286b485fba5c85e9ab 100644 --- a/en/device-dev/driver/driver-peripherals-usb-des.md +++ b/en/device-dev/driver/driver-peripherals-usb-des.md @@ -1,20 +1,20 @@ -# USB +# USB -- [Overview](#section175431838101617) - - [Available APIs](#section17667171301711) +- [Overview](#section127mcpsimp) + - [Available APIs](#section141mcpsimp) -- [Development Guidelines](#section65745222184) - - [Developing Driver Using Host DDK APIs](#section865734181916) - - [Developing Driver Using Host Raw APIs](#section865734181916) - - [Developing Driver Using Device DDK APIs](#section865734181916) +- [Development Guidelines](#section581mcpsimp) + - [Developing Driver Using Host DDK APIs](#section584mcpsimp) + - [Developing Driver Using Host Raw APIs](#section594mcpsimp) + - [Developing Driver Using Device DDK APIs](#section600mcpsimp) -- [Development Examples](#section263714411191) - - [Developing Driver Using Host DDK APIs](#section18249155619195) - - [Developing Driver Using Host Raw APIs](#section3571192072014) - - [Developing Driver Using Device DDK APIs](#section6356758162015) +- [Development Examples](#section607mcpsimp) + - [Developing Driver Using Host DDK APIs](#section609mcpsimp) + - [Developing Driver Using Host Raw APIs](#section612mcpsimp) + - [Developing Driver Using Device DDK APIs](#section615mcpsimp) -## Overview +## Overview USB host development aims to provide host-related functions, including protocol encapsulation, device management, and driver installation and uninstall. @@ -22,379 +22,358 @@ USB device development aims to provide device-related functions, including devic The following figures show the UBS host and device driver models. -**Figure 1** USB host driver model +**Figure 1** USB host driver model ![](figures/USB_host_driver_model.png "USB host driver model") -**Figure 2** USB device driver model +**Figure 2** USB device driver model ![](figures/USB_device_driver_model.png "USB device driver model") The USB driver model offers the following APIs: -- The USB host Driver Development Kit (DDK) provides driver capability APIs that can be directly called in user mode. The APIs can be classified into the DDK initialization class, interface operation class, and request operation class by function. These APIs can be used to perform DDK initialization, bind/release and open/close an interface, allocate/release a request, and implement isochronous or non-isochronous transfer. -- The USB device DDK provides device management, I/O management, and configuration management APIs, which can be used to create and delete a device, obtain/open an interface, and perform isochronous or non-isochronous transfer. +- The USB host Driver Development Kit (DDK) provides driver capability APIs that can be directly called in user mode. The APIs can be classified into the DDK initialization class, interface operation class, and request operation class by function. These APIs can be used to perform DDK initialization, bind/release and open/close an interface, allocate/release a request, and implement synchronous or asynchronous transfer. +- The USB device DDK provides device management, I/O management, and configuration management APIs, which can be used to create and delete a device, obtain/open an interface, and perform synchronous or asynchronous transfer. -### Available APIs +### Available APIs() -Table 1 describes the APIs provided by the USB host driver model. +[Figure 1](#fig1649563542917) describes the APIs provided by the USB host driver model. **Table 1** APIs provided by the USB host driver model - -

Header File

+ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Header File

API

+

API

Description

+

Description

usb_ddk_interface.h

-

-

+

usb_ddk_interface.h

int32_t UsbInitHostSdk(struct UsbSession **session);

+

int32_t UsbInitHostSdk(struct UsbSession **session);

Initializes the USB host driver DDK.

+

Initializes the USB host driver DDK.

int32_t UsbExitHostSdk(const struct UsbSession *session);

+

int32_t UsbExitHostSdk(const struct UsbSession *session);

Exits the USB host driver DDK.

+

Exits the USB host driver DDK.

const struct UsbInterface *UsbClaimInterface(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex);

+

const struct UsbInterface *UsbClaimInterface(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex);

Obtains a USB interface.

+

Obtains a USB interface.

int UsbReleaseInterface(const struct UsbInterface *interfaceObj);

+

int UsbReleaseInterface(const struct UsbInterface *interfaceObj);

Releases a USB interface.

+

Releases a USB interface.

int UsbAddOrRemoveInterface(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex, UsbInterfaceStatus status);

+

int UsbAddOrRemoveInterface(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex, UsbInterfaceStatus status);

Adds or removes a USB interface.

+

Adds or removes a USB interface.

UsbInterfaceHandle *UsbOpenInterface(const struct UsbInterface *interfaceObj);

+

UsbInterfaceHandle *UsbOpenInterface(const struct UsbInterface *interfaceObj);

Opens a USB interface.

+

Opens a USB interface.

int32_t UsbCloseInterface(const UsbInterfaceHandle *interfaceHandle);

+

int32_t UsbCloseInterface(const UsbInterfaceHandle *interfaceHandle);

Closes a USB interface.

+

Closes a USB interface.

int32_t UsbSelectInterfaceSetting(const UsbInterfaceHandle *interfaceHandle, uint8_t settingIndex, struct UsbInterface **interfaceObj);

+

int32_t UsbSelectInterfaceSetting(const UsbInterfaceHandle *interfaceHandle, uint8_t settingIndex, struct UsbInterface **interfaceObj);

Sets a USB interface.

+

Sets a USB interface.

int32_t UsbGetPipeInfo(const UsbInterfaceHandle *interfaceHandle, uint8_t settingIndex, uint8_t pipeId, struct UsbPipeInfo *pipeInfo);

+

int32_t UsbGetPipeInfo(const UsbInterfaceHandle *interfaceHandle, uint8_t settingIndex, uint8_t pipeId, struct UsbPipeInfo *pipeInfo);

Obtains USB pipe information.

+

Obtains USB pipe information.

int32_t UsbClearInterfaceHalt(const UsbInterfaceHandle *interfaceHandle, uint8_t pipeAddress);

+

int32_t UsbClearInterfaceHalt(const UsbInterfaceHandle *interfaceHandle, uint8_t pipeAddress);

Clears the state of the pipe with the specified index.

+

Clears the state of the pipe with the specified index.

struct UsbRequest *UsbAllocRequest(const UsbInterfaceHandle *interfaceHandle, int isoPackets, int length);

+

struct UsbRequest *UsbAllocRequest(const UsbInterfaceHandle *interfaceHandle, int isoPackets, int length);

Allocates a request object.

+

Allocates a request object.

int UsbFreeRequest(const struct UsbRequest *request);

+

int UsbFreeRequest(const struct UsbRequest *request);

Releases a request object.

+

Releases a request object.

int UsbSubmitRequestAsync(const struct UsbRequest *request);

+

int UsbSubmitRequestAsync(const struct UsbRequest *request);

Sends a request asynchronously.

+

Sends an asynchronous request.

int32_t UsbFillRequest(const struct UsbRequest *request, const UsbInterfaceHandle *interfaceHandle, const struct UsbRequestParams *params);

+

int32_t UsbFillRequest(const struct UsbRequest *request, const UsbInterfaceHandle *interfaceHandle, const struct UsbRequestParams *params);

Fills in a request.

+

Fills in a request.

sint UsbCancelRequest(const struct UsbRequest *request);

+

sint UsbCancelRequest(const struct UsbRequest *request);

Cancels an asynchronous request.

+

Cancels an asynchronous request.

int UsbSubmitRequestSync(const struct UsbRequest *request);

+

int UsbSubmitRequestSync(const struct UsbRequest *request);

Sends a synchronous request.

+

Sends a synchronous request.

usb_raw_api.h

-

-

-

-

-

+

usb_raw_api.h

int UsbRawInit(struct UsbSession **session);

+

int UsbRawInit(struct UsbSession **session);

Initializes the USB raw APIs.

+

Initializes the USB raw APIs.

int UsbRawExit(const struct UsbSession *session);

+

int UsbRawExit(const struct UsbSession *session);

Exits the USB raw APIs.

+

Exits the USB raw APIs.

UsbRawHandle *UsbRawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr);

+

UsbRawHandle *UsbRawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr);

Opens a USB device.

+

Opens a USB device.

int UsbRawCloseDevice(const UsbRawHandle *devHandle);

+

int UsbRawCloseDevice(const UsbRawHandle *devHandle);

Closes a USB device.

+

Closes a USB device.

int UsbRawSendControlRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbControlRequestData *requestData);

+

int UsbRawSendControlRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbControlRequestData *requestData);

Performs a control transfer synchronously.

+

Performs a control transfer synchronously.

int UsbRawSendBulkRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData);

+

int UsbRawSendBulkRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData);

Performs a bulk transfer synchronously.

+

Performs a bulk transfer synchronously.

int UsbRawSendInterruptRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData);

+

int UsbRawSendInterruptRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData);

Performs an interrupt transfer synchronously.

+

Performs an interrupt transfer synchronously.

int UsbRawGetConfigDescriptor(const UsbRawDevice *rawDev, uint8_t configIndex, struct UsbRawConfigDescriptor **config);

+

int UsbRawGetConfigDescriptor(const UsbRawDevice *rawDev, uint8_t configIndex, struct UsbRawConfigDescriptor **config);

Obtains the configuration descriptor of a device.

+

Obtains the configuration descriptor of a device.

void UsbRawFreeConfigDescriptor(const struct UsbRawConfigDescriptor *config);

+

void UsbRawFreeConfigDescriptor(const struct UsbRawConfigDescriptor *config);

Releases the memory space of a configuration descriptor.

+

Releases the memory space of a configuration descriptor.

int UsbRawGetConfiguration(const UsbRawHandle *devHandle, int *config);

+

int UsbRawGetConfiguration(const UsbRawHandle *devHandle, int *config);

Obtains the configuration in use.

+

Obtains the configuration in use.

int UsbRawSetConfiguration(const UsbRawHandle *devHandle, int config);

+

int UsbRawSetConfiguration(const UsbRawHandle *devHandle, int config);

Sets the configuration in use.

+

Sets the configuration in use.

int UsbRawGetDescriptor(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawDescriptorParam *param, const unsigned char *data);

+

int UsbRawGetDescriptor(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawDescriptorParam *param, const unsigned char *data);

Obtains descriptor information.

+

Obtains descriptor information.

UsbRawDevice *UsbRawGetDevice(const UsbRawHandle *devHandle);

+

UsbRawDevice *UsbRawGetDevice(const UsbRawHandle *devHandle);

Obtains the device pointer based on the device handle.

+

Obtains the device pointer based on the device handle.

int UsbRawGetDeviceDescriptor(const UsbRawDevice *rawDev, struct UsbDeviceDescriptor *desc);

+

int UsbRawGetDeviceDescriptor(const UsbRawDevice *rawDev, struct UsbDeviceDescriptor *desc);

Obtains the device descriptor of the specified USB device.

+

Obtains the device descriptor of the specified USB device.

int UsbRawClaimInterface(const UsbRawHandle *devHandle, int interfaceNumber);

+

int UsbRawClaimInterface(const UsbRawHandle *devHandle, int interfaceNumber);

Declares the interface on the specified device handle.

+

Declares the interface on the specified device handle.

int UsbRawReleaseInterface(const UsbRawHandle *devHandle, int interfaceNumber);

+

int UsbRawReleaseInterface(const UsbRawHandle *devHandle, int interfaceNumber);

Releases the previously declared interface.

+

Releases the previously declared interface.

int UsbRawResetDevice(const UsbRawHandle *devHandle);

+

int UsbRawResetDevice(const UsbRawHandle *devHandle);

Resets a device.

+

Resets a device.

struct UsbRawRequest *UsbRawAllocRequest(const UsbRawHandle *devHandle, int isoPackets, int length);

+

struct UsbRawRequest *UsbRawAllocRequest(const UsbRawHandle *devHandle, int isoPackets, int length);

Allocates a transfer request with the specified number of sync packet descriptors.

+

Allocates a transfer request with the specified number of sync packet descriptors.

int UsbRawFreeRequest(const struct UsbRawRequest *request);

+

int UsbRawFreeRequest(const struct UsbRawRequest *request);

Releases the previously allocated transfer request.

+

Releases the previously allocated transfer request.

int UsbRawFillBulkRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

+

int UsbRawFillBulkRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

Fills in the bulk transfer request.

+

Fills in a bulk transfer request.

int UsbRawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData);

+

int UsbRawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData);

Fills in the control setup packet.

+

Fills in a control setup packet.

int UsbRawFillControlRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

+

int UsbRawFillControlRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

Fills in the control transfer request.

+

Fills in a control transfer request.

int UsbRawFillInterruptRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

+

int UsbRawFillInterruptRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

Fills in the interrupt transfer request.

+

Fills in an interrupt transfer request.

int UsbRawFillIsoRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

+

int UsbRawFillIsoRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);

Fills in the isochronous transfer request.

+

Fills in an isochronous transfer request.

int UsbRawSubmitRequest(const struct UsbRawRequest *request);

+

int UsbRawSubmitRequest(const struct UsbRawRequest *request);

Submits a transfer request.

+

Submits a transfer request.

int UsbRawCancelRequest(const struct UsbRawRequest *request);

+

int UsbRawCancelRequest(const struct UsbRawRequest *request);

Cancels a transfer request.

+

Cancels a transfer request.

int UsbRawHandleRequests(const UsbRawHandle *devHandle);

+

int UsbRawHandleRequests(const UsbRawHandle *devHandle);

Handles a transfer request event.

+

Handles a transfer request event.

-Table 2 describes the APIs provided by the USB device driver model. +[Figure 2](#fig8847615103013) describes the APIs provided by the USB device driver model. **Table 2** APIs provided by the USB device driver model - -

Header File

+ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Header File

API

+

API

Description

+

Description

usbfn_device.h

-

-

+

usbfn_device.h

const struct UsbFnDevice *UsbFnCreateDevice(const char *udcName, const struct UsbFnDescriptorData *descriptor);

+

const struct UsbFnDevice *UsbFnCreateDevice(const char *udcName, const struct UsbFnDescriptorData *descriptor);

Creates a USB device.

+

Creates a USB device.

int UsbFnRemoveDevice(struct UsbFnDevice *fnDevice);

+

int UsbFnRemoveDevice(struct UsbFnDevice *fnDevice);

Deletes a USB device.

+

Deletes a USB device.

const struct UsbFnDevice *UsbFnGetDevice(const char *udcName);

+

const struct UsbFnDevice *UsbFnGetDevice(const char *udcName);

Obtains a USB device.

+

Obtains a USB device.

usbfn_interface.h

-

-

-

-

-

+

usbfn_interface.h

int UsbFnStartRecvInterfaceEvent(struct UsbFnInterface *interface, uint32_t eventMask, UsbFnEventCallback callback, void *context);

+

int UsbFnStartRecvInterfaceEvent(struct UsbFnInterface *interface, uint32_t eventMask, UsbFnEventCallback callback, void *context);

Starts to receive events.

+

Starts receiving events.

int UsbFnStopRecvInterfaceEvent(struct UsbFnInterface *interface);

+

int UsbFnStopRecvInterfaceEvent(struct UsbFnInterface *interface);

Stops receiving events.

+

Stops receiving events.

UsbFnInterfaceHandle UsbFnOpenInterface(struct UsbFnInterface *interface);

+

UsbFnInterfaceHandle UsbFnOpenInterface(struct UsbFnInterface *interface);

Opens an interface.

+

Opens an interface.

int UsbFnCloseInterface(UsbFnInterfaceHandle handle);

+

int UsbFnCloseInterface(UsbFnInterfaceHandle handle);

Closes an interface.

+

Closes an interface.

int UsbFnGetInterfacePipeInfo(struct UsbFnInterface *interface, uint8_t pipeId, struct UsbFnPipeInfo *info);

+

int UsbFnGetInterfacePipeInfo(struct UsbFnInterface *interface, uint8_t pipeId, struct UsbFnPipeInfo *info);

Obtains pipe information.

+

Obtains pipe information.

int UsbFnSetInterfaceProp(const struct UsbFnInterface *interface, const char *name, const char *value);

+

int UsbFnSetInterfaceProp(const struct UsbFnInterface *interface, const char *name, const char *value);

Sets custom properties.

+

Sets custom properties.

usbfn_request.h

-

-

-

-

-

-

-

+

usbfn_request.h

struct UsbFnRequest *UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle, uint32_t len);

+

struct UsbFnRequest *UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle, uint32_t len);

Applies for a control transfer request.

+

Applies for a control transfer request.

struct UsbFnRequest *UsbFnAllocRequest(UsbFnInterfaceHandle handle, uint8_t pipe, uint32_t len);

+

struct UsbFnRequest *UsbFnAllocRequest(UsbFnInterfaceHandle handle, uint8_t pipe, uint32_t len);

Applies for a data request.

+

Applies for a data request.

int UsbFnFreeRequest(struct UsbFnRequest *req);

+

int UsbFnFreeRequest(struct UsbFnRequest *req);

Releases a request.

+

Releases a request.

int UsbFnSubmitRequestAsync(struct UsbFnRequest *req);

+

int UsbFnSubmitRequestAsync(struct UsbFnRequest *req);

Sends a request asynchronously.

+

Sends an asynchronous request.

int UsbFnSubmitRequestSync(struct UsbFnRequest *req, uint32_t timeout);

+

int UsbFnSubmitRequestSync(struct UsbFnRequest *req, uint32_t timeout);

Sends a request synchronously.

+

Sends a synchronous request.

int UsbFnCancelRequest(struct UsbFnRequest *req);

+

int UsbFnCancelRequest(struct UsbFnRequest *req);

Cancels a request.

+

Cancels a request.

-## Development Guidelines +## Development Guidelines The USB driver is developed based on the Hardware Driver Foundation (HDF), platform, and Operating System Abstraction Layer (OSAL) APIs. A unified driver model is provided for USB devices, irrespective of the operating system and chip architecture. This document uses a serial port as an example to describe how to develop drivers for the USB host and USB device. -### How to Develop +### How to Develop -### Developing Driver Using Host DDK APIs +### Developing Driver Using Host DDK APIs 1. Configure the driver mapping table. 2. Initialize the USB host DDK. @@ -405,15 +384,14 @@ The USB driver is developed based on the Hardware Driver Foundation (HDF), platf 7. Fill in the I/O request based on the input parameters. 8. Submit the I/O request in synchronous or asynchronous mode. - -### Developing Driver Using Host Raw APIs +### Developing Driver Using Host Raw APIs 1. Configure the driver mapping table. 2. Initialize the host raw data, open the USB device, obtain the descriptor, and then obtain interface and endpoint information based on the descriptor. 3. Allocate a request and fill in the request based on the transfer type. -4. Submit the I/O request object in synchronous or asynchronous mode. +4. Submit the I/O request in synchronous or asynchronous mode. -### Developing Driver Using Device DDK APIs +### Developing Driver Using Device DDK APIs 1. Construct a descriptor. 2. Instantiate a USB device using the descriptor constructed. @@ -421,11 +399,11 @@ The USB driver is developed based on the Hardware Driver Foundation (HDF), platf 4. Call **UsbFnInterfaceStartRecvEvent** to receive events such as Enable and Setup, and respond to the events in **UsbFnEventCallback**. 5. Send and receive data in synchronous or asynchronous mode. -## Development Examples +## Development Examples -The following example helps you better understand the development of the USB serial port driver. +The following examples help you better understand the development of the USB serial port driver. -### Developing Driver Using Host DDK APIs +### Developing Driver Using Host DDK APIs ``` root { @@ -435,42 +413,42 @@ root { usb_pnp_device_id = "UsbPnpDeviceId"; UsbPnpDeviceId { idTableList = [ - "host_acm_table" - ]; - host_acm_table { - // Driver module name, which must be the same as the value of moduleName in the driver entry structure. - moduleName = "usbhost_acm"; - // Service name of the driver, which must be unique. - serviceName = "usbhost_acm_pnp_service"; - // Keyword for matching private driver data. - deviceMatchAttr = "usbhost_acm_pnp_matchAttr"; - // Data length starting from this field, in bytes. - length = 21; - // USB driver matching rule: vendorId+productId+interfaceSubClass+interfaceProtocol+interfaceNumber. - matchFlag = 0x0303; - // Vendor ID. - vendorId = 0x12D1; - // Product ID. - productId = 0x5000; - // The least significant 16 bits of the device sequence number. - bcdDeviceLow = 0x0000; - // The most significant 16 bits of the device sequence number. - bcdDeviceHigh = 0x0000; - // Device class code allocated by the USB. - deviceClass = 0; - // Child class code allocated by the USB. - deviceSubClass = 0; - // Device protocol code allocated by the USB. - deviceProtocol = 0; - // Interface type. You can enter multiple types as needed. - interfaceClass = [0]; - // Interface subtype. You can enter multiple subtypes as needed. - interfaceSubClass = [2, 0]; - // Protocol that the interface complies with. You can enter multiple protocols as needed. - interfaceProtocol = [1, 2]; - // Interface numer. You can enter multiple interface numbers as needed. - interfaceNumber = [2, 3]; - } + "host_acm_table" + ]; + host_acm_table { + // Driver module name, which must be the same as the value of moduleName in the driver entry structure. + moduleName = "usbhost_acm"; + // Service name of the driver, which must be unique. + serviceName = "usbhost_acm_pnp_service"; + // Keyword for matching private driver data. + deviceMatchAttr = "usbhost_acm_pnp_matchAttr"; + // Data length starting from this field, in bytes. + length = 21; + // USB driver matching rule: vendorId+productId+interfaceSubClass+interfaceProtocol+interfaceNumber. + matchFlag = 0x0303; + // Vendor ID. + vendorId = 0x12D1; + // Product ID. + productId = 0x5000; + // The least significant 16 bits of the device sequence number. + bcdDeviceLow = 0x0000; + // The most significant 16 bits of the device sequence number. + bcdDeviceHigh = 0x0000; + // Device class code allocated by the USB. + deviceClass = 0; + // Child class code allocated by the USB. + deviceSubClass = 0; + // Device protocol code allocated by the USB. + deviceProtocol = 0; + // Interface type. You can enter multiple types as needed. + interfaceClass = [0]; + // Interface subtype. You can enter multiple subtypes as needed. + interfaceSubClass = [2, 0]; + // Protocol that the interface complies with. You can enter multiple protocols as needed. + interfaceProtocol = [1, 2]; + // Interface number. You can enter multiple interface numbers as needed. + interfaceNumber = [2, 3]; + } } } } @@ -497,16 +475,16 @@ static int SerialCtrlMsg(struct AcmDevice *acm, uint8_t request, { int ret; uint16_t index = acm->intPipe->interfaceId; - struct UsbControlParams controlParams = {}; - struct UsbRequestParams parmas = {}; + struct UsbControlParams controlParams; + struct UsbRequestParams params; if (acm == NULL || buf == NULL) { - HDF_LOGE("%{public}s:invalid param", __func__); + HDF_LOGE("%s:invalid param", __func__); return HDF_ERR_IO; } if (acm->ctrlReq == NULL) { acm->ctrlReq = UsbAllocRequest(acm->ctrDevHandle, 0, len); if (acm->ctrlReq == NULL) { - HDF_LOGE("%{public}s: UsbAllocRequest failed", __func__); + HDF_LOGE("%s: UsbAllocRequest failed", __func__); return HDF_ERR_IO; } } @@ -520,24 +498,24 @@ static int SerialCtrlMsg(struct AcmDevice *acm, uint8_t request, controlParams.data = buf; controlParams.size = len; - parmas.interfaceId = USB_CTRL_INTERFACE_ID; - parmas.pipeAddress = acm->ctrPipe->pipeAddress; - parmas.pipeId = acm->ctrPipe->pipeId; - parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE; - parmas.timeout = USB_CTRL_SET_TIMEOUT; - parmas.ctrlReq = UsbControlSetUp(&controlParams); - ret = UsbFillRequest(acm->ctrlReq, acm->ctrDevHandle, &parmas); + params.interfaceId = USB_CTRL_INTERFACE_ID; + params.pipeAddress = acm->ctrPipe->pipeAddress; + params.pipeId = acm->ctrPipe->pipeId; + params.requestType = USB_REQUEST_PARAMS_CTRL_TYPE; + params.timeout = USB_CTRL_SET_TIMEOUT; + params.ctrlReq = UsbControlSetUp(&controlParams); + ret = UsbFillRequest(acm->ctrlReq, acm->ctrDevHandle, ¶ms); if (HDF_SUCCESS != ret) { - HDF_LOGE("%{public}s: failed, ret=%{public}d ", __func__, ret); + HDF_LOGE("%s: failed, ret=%d ", __func__, ret); return ret; } - ret = UsbSubmitRequestSync(acm->ctrlReq); // Send an I/O request synchronously. + ret = UsbSubmitRequestSync(acm->ctrlReq); // Send an I/O request synchronously. if (HDF_SUCCESS != ret) { - HDF_LOGE("UsbSubmitRequestSync faile, ret=%{public}d ", ret); + HDF_LOGE("UsbSubmitRequestSync failed, ret=%d ", ret); return ret; } if (!acm->ctrlReq->compInfo.status) { - HDF_LOGE("%{public}s status=%{public}d ", __func__, acm->ctrlReq->compInfo.status); + HDF_LOGE("%s status=%d ", __func__, acm->ctrlReq->compInfo.status); } return HDF_SUCCESS; } @@ -546,8 +524,8 @@ static struct UsbInterface *GetUsbInterfaceById(const struct AcmDevice *acm, uint8_t interfaceIndex) { struct UsbInterface *tmpIf = NULL; - tmpIf = (struct UsbInterface *)UsbClaimInterface(acm->session, acm->busNum, \ - acm->devAddr, interfaceIndex); // Obtain the UsbInterface object. + tmpIf = (struct UsbInterface *)UsbClaimInterface(acm->session, acm->busNum, + acm->devAddr, interfaceIndex); // Obtain the UsbInterface object. return tmpIf; } ... @@ -578,7 +556,7 @@ static struct UsbPipeInfo *EnumePipe(const struct AcmDevice *acm, if ((p.pipeDirection == pipeDirection) && (p.pipeType == pipeType)) { struct UsbPipeInfo *pi = OsalMemCalloc(sizeof(*pi)); if (pi == NULL) { - HDF_LOGE("%{public}s: Alloc pipe failed", __func__); + HDF_LOGE("%s: Alloc pipe failed", __func__); return NULL; } p.interfaceId = info->interfaceIndex; @@ -594,7 +572,7 @@ static struct UsbPipeInfo *GetPipe(const struct AcmDevice *acm, { uint8_t i; if (acm == NULL) { - HDF_LOGE("%{public}s: invalid parmas", __func__); + HDF_LOGE("%s: invalid params", __func__); return NULL; } for (i = 0; i < acm->interfaceCnt; i++) { @@ -627,12 +605,12 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) return HDF_FAILURE; } if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) { - HDF_LOGE("%s:%d OsalMutexInit fail", __func__, __LINE__); + HDF_LOGE("%s:%d OsalMutexInit failed", __func__, __LINE__); goto error; } info = (struct UsbPnpNotifyServiceInfo *)device->priv; if (info != NULL) { - HDF_LOGD("%s:%d busNum=%d,devAddr=%d,interfaceLength=%d", \ + HDF_LOGD("%s:%d busNum=%d,devAddr=%d,interfaceLength=%d", __func__, __LINE__, info->busNum, info->devNum, info->interfaceLength); acm->busNum = info->busNum; acm->devAddr = info->devNum; @@ -640,7 +618,7 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, (const void*)info->interfaceNumber, info->interfaceLength); if (err != EOK) { - HDF_LOGE("%s:%d memcpy_s failed err=%d", \ + HDF_LOGE("%s:%d memcpy_s failed err=%d", __func__, __LINE__, err); goto lock_error; } @@ -656,7 +634,7 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) lock_error: if (OsalMutexDestroy(&acm->lock)) { - HDF_LOGE("%s:%d OsalMutexDestroy fail", __func__, __LINE__); + HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__); } error: OsalMemFree(acm); @@ -667,26 +645,26 @@ error: static int AcmAllocReadRequests(struct AcmDevice *acm) { int ret; - struct UsbRequestParams readParmas = {}; + struct UsbRequestParams readParams; for (int i = 0; i < ACM_NR; i++) { - acm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize); // Allocate the readReq I/O request object to be sent. + acm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize); // Allocate the readReq I/O request to be sent. if (!acm->readReq[i]) { - HDF_LOGE("readReq request failed\n"); + HDF_LOGE("readReq request failed"); goto error; } - readParmas.userData = (void *)acm; - readParmas.pipeAddress = acm->dataInPipe->pipeAddress; - readParmas.pipeId = acm->dataInPipe->pipeId; - readParmas.interfaceId = acm->dataInPipe->interfaceId; - readParmas.callback = AcmReadBulk; - readParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; - readParmas.timeout = USB_CTRL_SET_TIMEOUT; - readParmas.dataReq.numIsoPackets = 0; - readParmas.dataReq.directon = (acm->dataInPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & 0x1; - readParmas.dataReq.length = acm->readSize; - ret = UsbFillRequest(acm->readReq[i], InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParmas); // Fill in the readReq object to be sent. + readParams.userData = (void *)acm; + readParams.pipeAddress = acm->dataInPipe->pipeAddress; + readParams.pipeId = acm->dataInPipe->pipeId; + readParams.interfaceId = acm->dataInPipe->interfaceId; + readParams.callback = AcmReadBulk; + readParams.requestType = USB_REQUEST_PARAMS_DATA_TYPE; + readParams.timeout = USB_CTRL_SET_TIMEOUT; + readParams.dataReq.numIsoPackets = 0; + readParams.dataReq.directon = (acm->dataInPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & 0x1; + readParams.dataReq.length = acm->readSize; + ret = UsbFillRequest(acm->readReq[i], InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParams); // Fills in the readReq object to be sent. if (HDF_SUCCESS != ret) { - HDF_LOGE("%{public}s: UsbFillRequest faile, ret=%{public}d \n", __func__, ret); + HDF_LOGE("%s: UsbFillRequest failed, ret=%d n", __func__, ret); goto error; } } @@ -700,25 +678,25 @@ error: static int AcmAllocNotifyRequest(struct AcmDevice *acm) { int ret; - struct UsbRequestParams intParmas = {}; - acm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(acm, acm->intPipe->interfaceId), 0, acm->intSize); // Allocate the interrupt I/O request object to be sent. + struct UsbRequestParams intParams = {}; + acm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(acm, acm->intPipe->interfaceId), 0, acm->intSize); // Allocate the interrupt I/O request object to be sent. if (!acm->notifyReq) { - HDF_LOGE("notifyReq request fail\n"); + HDF_LOGE("notifyReq request failed"); return HDF_ERR_MALLOC_FAIL; } - intParmas.userData = (void *)acm; - intParmas.pipeAddress = acm->intPipe->pipeAddress; - intParmas.pipeId = acm->intPipe->pipeId; - intParmas.interfaceId = acm->intPipe->interfaceId; - intParmas.callback = AcmCtrlIrq; - intParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; - intParmas.timeout = USB_CTRL_SET_TIMEOUT; - intParmas.dataReq.numIsoPackets = 0; - intParmas.dataReq.directon = (acm->intPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & DIRECTION_MASK; - intParmas.dataReq.length = acm->intSize; - ret = UsbFillRequest(acm->notifyReq, InterfaceIdToHandle(acm, acm->intPipe->interfaceId), &intParmas); // Fill in the interrupt I/O request. + intParams.userData = (void *)acm; + intParams.pipeAddress = acm->intPipe->pipeAddress; + intParams.pipeId = acm->intPipe->pipeId; + intParams.interfaceId = acm->intPipe->interfaceId; + intParams.callback = AcmCtrlIrq; + intParams.requestType = USB_REQUEST_PARAMS_DATA_TYPE; + intParams.timeout = USB_CTRL_SET_TIMEOUT; + intParams.dataReq.numIsoPackets = 0; + intParams.dataReq.directon = (acm->intPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & DIRECTION_MASK; + intParams.dataReq.length = acm->intSize; + ret = UsbFillRequest(acm->notifyReq, InterfaceIdToHandle(acm, acm->intPipe->interfaceId), &intParams); // Fill in the interrupt I/O request. if (HDF_SUCCESS != ret) { - HDF_LOGE("%{public}s: UsbFillRequest faile, ret=%{public}d \n", __func__, ret); + HDF_LOGE("%s: UsbFillRequest failed, ret=%d n", __func__, ret); goto error; } return HDF_SUCCESS; @@ -745,16 +723,16 @@ static void AcmReleaseInterfaces(struct AcmDevice *acm) static int32_t AcmClaimInterfaces(struct AcmDevice *acm) { for (int i = 0; i < acm->interfaceCnt; i++) { - acm->iface[i] = GetUsbInterfaceById((const struct AcmDevice *)acm, acm->interfaceIndex[i]); // Obtain the UsbInterface object. + acm->iface[i] = GetUsbInterfaceById((const struct AcmDevice *)acm, acm->interfaceIndex[i]); // Obtain the UsbInterface object. if (acm->iface[i] == NULL) { - HDF_LOGE("%{public}s: interface%{public}d is null", __func__, acm->interfaceIndex[i]); + HDF_LOGE("%s: interface%d is null", __func__, acm->interfaceIndex[i]); goto error; } } - acm->ctrIface = GetUsbInterfaceById((const struct AcmDevice *)acm, USB_CTRL_INTERFACE_ID); // Obtain the UsbInterface object corresponding to the control interface. + acm->ctrIface = GetUsbInterfaceById((const struct AcmDevice *)acm, USB_CTRL_INTERFACE_ID); // Obtain the UsbInterface object corresponding to the control interface. if (acm->ctrIface == NULL) { - HDF_LOGE("%{public}s: GetUsbInterfaceById null", __func__); + HDF_LOGE("%s: GetUsbInterfaceById null", __func__); goto error; } @@ -783,16 +761,16 @@ static int32_t AcmOpenInterfaces(struct AcmDevice *acm) { for (int i = 0; i < acm->interfaceCnt; i++) { if (acm->iface[i]) { - acm->devHandle[i] = UsbOpenInterface(acm->iface[i]); // Open the UsbInterface object obtained. + acm->devHandle[i] = UsbOpenInterface(acm->iface[i]); // Open the UsbInterface object obtained. if (acm->devHandle[i] == NULL) { - HDF_LOGE("%{public}s: UsbOpenInterface null", __func__); + HDF_LOGE("%s: UsbOpenInterface null", __func__); goto error; } } } acm->ctrDevHandle = UsbOpenInterface(acm->ctrIface); if (acm->ctrDevHandle == NULL) { - HDF_LOGE("%{public}s: ctrDevHandle UsbOpenInterface null", __func__); + HDF_LOGE("%s: ctrDevHandle UsbOpenInterface null", __func__); goto error; } @@ -805,7 +783,7 @@ error: static int32_t AcmGetPipes(struct AcmDevice *acm) { - acm->dataInPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN); // Obtain pipe information of dataInPipe. + acm->dataInPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN);// Obtain pipe information of dataInPipe. if (acm->dataInPipe == NULL) { HDF_LOGE("dataInPipe is NULL"); goto error; @@ -817,7 +795,7 @@ static int32_t AcmGetPipes(struct AcmDevice *acm) goto error; } - acm->ctrPipe = EnumePipe(acm, acm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT); // Obtain pipe information of the control pipe. + acm->ctrPipe = EnumePipe(acm, acm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT); // Obtain pipe information of the control pipe. if (acm->ctrPipe == NULL) { HDF_LOGE("ctrPipe is NULL"); goto error; @@ -858,29 +836,29 @@ static int32_t AcmAllocRequests(struct AcmDevice *acm) int32_t ret; if (AcmWriteBufAlloc(acm) < 0) { - HDF_LOGE("%{public}s: AcmWriteBufAlloc failed", __func__); + HDF_LOGE("%s: AcmWriteBufAlloc failed", __func__); return HDF_ERR_MALLOC_FAIL; } for (int i = 0; i < ACM_NW; i++) { struct AcmWb *snd = &(acm->wb[i]); - snd->request = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataOutPipe->interfaceId), 0, acm->writeSize); // Allocate the I/O request object to be sent. + snd->request = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataOutPipe->interfaceId), 0, acm->writeSize); // Allocate the I/O request object to be sent. snd->instance = acm; if (snd->request == NULL) { - HDF_LOGE("%{public}s:%{public}d snd request fail", __func__, __LINE__); + HDF_LOGE("%s:%d snd request failed", __func__, __LINE__); goto error_alloc_write_req; } } - ret = AcmAllocNotifyRequest(acm); // Allocate and fill in the interrupt I/O request object. + ret = AcmAllocNotifyRequest(acm); // Allocate and fill in the interrupt I/O request object. if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s:%{public}d AcmAllocNotifyRequest fail", __func__, __LINE__); + HDF_LOGE("%s:%d AcmAllocNotifyRequest failed", __func__, __LINE__); goto error_alloc_int_req; } - ret = AcmAllocReadRequests(acm); // Allocate and fill in the readReq I/O request object. + ret = AcmAllocReadRequests(acm); // Allocate and fill in the readReq I/O request object. if (ret) { - HDF_LOGE("%{public}s:%{public}d AcmAllocReadRequests fail", __func__, __LINE__); + HDF_LOGE("%s:%d AcmAllocReadRequests failed", __func__, __LINE__); goto error_alloc_read_req; } @@ -901,38 +879,38 @@ static int32_t AcmInit(struct AcmDevice *acm) struct UsbSession *session = NULL; if (acm->initFlag == true) { - HDF_LOGE("%{public}s:%{public}d: initFlag is true", __func__, __LINE__); + HDF_LOGE("%s:%d: initFlag is true", __func__, __LINE__); return HDF_SUCCESS; } - ret = UsbInitHostSdk(NULL); // Initialize the host DDK. + ret = UsbInitHostSdk(NULL); // Initialize the Host DDK. if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s: UsbInitHostSdk failed", __func__); + HDF_LOGE("%s: UsbInitHostSdk failed", __func__); return HDF_ERR_IO; } acm->session = session; ret = AcmClaimInterfaces(acm); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s: AcmClaimInterfaces failed", __func__); + HDF_LOGE("%s: AcmClaimInterfaces failed", __func__); goto error_claim_interfaces; } ret = AcmOpenInterfaces(acm); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s: AcmOpenInterfaces failed", __func__); + HDF_LOGE("%s: AcmOpenInterfaces failed", __func__); goto error_open_interfaces; } ret = AcmGetPipes(acm); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s: AcmGetPipes failed", __func__); + HDF_LOGE("%s: AcmGetPipes failed", __func__); goto error_get_pipes; } ret = AcmAllocRequests(acm); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s: AcmAllocRequests failed", __func__); + HDF_LOGE("%s: AcmAllocRequests failed", __func__); goto error_alloc_reqs; } @@ -942,7 +920,7 @@ static int32_t AcmInit(struct AcmDevice *acm) acm->lineCoding.bDataBits = USB_CDC_1_STOP_BITS; acm->initFlag = true; - HDF_LOGD("%{public}s:%{public}d========OK", __func__, __LINE__); + HDF_LOGD("%s:%d========OK", __func__, __LINE__); return HDF_SUCCESS; error_alloc_reqs: @@ -960,7 +938,7 @@ error_claim_interfaces: static void AcmRelease(struct AcmDevice *acm) { if (acm->initFlag == false) { - HDF_LOGE("%{public}s:%{public}d: initFlag is false", __func__, __LINE__); + HDF_LOGE("%s:%d: initFlag is false", __func__, __LINE__); return; } @@ -980,24 +958,24 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device) struct AcmDevice *acm = NULL; if (device == NULL) { - HDF_LOGE("%{public}s: device is null", __func__); + HDF_LOGE("%s: device is null", __func__); return HDF_ERR_INVALID_OBJECT; } acm = (struct AcmDevice *)device->service; OsalMutexInit(&acm->readLock); OsalMutexInit(&acm->writeLock); - HDF_LOGD("%{public}s:%{public}d busNum=%{public}d,devAddr=%{public}d", \ + HDF_LOGD("%s:%d busNum=%d,devAddr=%d", __func__, __LINE__, acm->busNum, acm->devAddr); ret = UsbSerialDeviceAlloc(acm); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s: Serial Device alloc failed", __func__); + HDF_LOGE("%s: Serial Device alloc failed", __func__); } acm->initFlag = false; g_acmReleaseFlag = false; - HDF_LOGD("%{public}s:%{public}d init ok!", __func__, __LINE__); + HDF_LOGD("%s:%d init ok!", __func__, __LINE__); return ret; } @@ -1007,19 +985,19 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) struct AcmDevice *acm = NULL; if (device == NULL) { - HDF_LOGE("%{public}s: device is NULL", __func__); + HDF_LOGE("%s: device is NULL", __func__); return; } acm = (struct AcmDevice *)device->service; if (acm == NULL) { - HDF_LOGE("%{public}s: acm is null", __func__); + HDF_LOGE("%s: acm is null", __func__); return; } g_acmReleaseFlag = true; if (acm->initFlag == true) { - HDF_LOGE("%{public}s:%{public}d AcmRelease", __func__, __LINE__); + HDF_LOGE("%s:%d AcmRelease", __func__, __LINE__); AcmRelease(acm); } UsbSeriaDevicelFree(acm); @@ -1028,12 +1006,12 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) OsalMutexDestroy(&acm->lock); OsalMemFree(acm); acm = NULL; - HDF_LOGD("%{public}s:%{public}d exit", __func__, __LINE__); + HDF_LOGD("%s:%d exit", __func__, __LINE__); } struct HdfDriverEntry g_usbSerialDriverEntry = { .moduleVersion = 1, - .moduleName = "usbhost_acm", // Driver module name, which must be the same as that configured in the .hcs file. + .moduleName = "usbhost_acm", // Driver module name, which must be the same as that configured in the .hcs file. .Bind = UsbSerialDriverBind, .Init = UsbSerialDriverInit, .Release = UsbSerialDriverRelease, @@ -1041,7 +1019,7 @@ struct HdfDriverEntry g_usbSerialDriverEntry = { HDF_INIT(g_usbSerialDriverEntry); ``` -### Developing Driver Using Host Raw APIs +### Developing Driver Using Host Raw APIs ``` root { @@ -1053,38 +1031,38 @@ root { idTableList = [ "host_acm_rawapi_table" ]; - host_acm_rawapi_table { // Driver mapping table information. - // Driver module name, which must be the same as the value of moduleName in the driver entry structure. + host_acm_rawapi_table { // Driver mapping table information. + // Driver module name, which must be the same as the value of moduleName in the driver entry structure. moduleName = "usbhost_acm_rawapi"; - // Service name of the driver, which must be unique. + // Service name of the driver, which must be unique. serviceName = "usbhost_acm_rawapi_service"; - // Keyword for matching private driver data. + // Keyword for matching private driver data. deviceMatchAttr = "usbhost_acm_rawapi_matchAttr"; - // Data length starting from this field, in bytes. - length = 21; - // USB driver matching rule: vendorId+productId+interfaceSubClass+interfaceProtocol+interfaceNumber. + // Data length starting from this field, in bytes. + length = 21; + // USB driver matching rule: vendorId+productId+interfaceSubClass+interfaceProtocol+interfaceNumber. matchFlag = 0x0303; - // Vendor ID. + // Vendor ID. vendorId = 0x12D1; - // Product ID. + // Product ID. productId = 0x5000; - // The least significant 16 bits of the device sequence number. + // The least significant 16 bits of the device sequence number. bcdDeviceLow = 0x0000; - // The most significant 16 bits of the device sequence number. + // The most significant 16 bits of the device sequence number. bcdDeviceHigh = 0x0000; - // Device class code allocated by the USB. + // Device class code allocated by the USB. deviceClass = 0; - // Child class code allocated by the USB. + // Child class code allocated by the USB. deviceSubClass = 0; - // Device protocol code allocated by the USB. + // Device protocol code allocated by the USB. deviceProtocol = 0; - // Interface type. You can enter multiple types as needed. + // Interface type. You can enter multiple types as needed. interfaceClass = [0]; - // Interface subtype. You can enter multiple subtypes as needed. + // Interface subtype. You can enter multiple subtypes as needed. interfaceSubClass = [2, 0]; - // Protocol that the interface complies with. You can enter multiple protocols as needed. + // Protocol that the interface complies with. You can enter multiple protocols as needed. interfaceProtocol = [1, 2]; - // Interface numer. You can enter multiple interface numbers as needed. + // Interface number. You can enter multiple interface numbers as needed. interfaceNumber = [2, 3]; } } @@ -1119,28 +1097,28 @@ static int UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConfigDe int ret; if (devHandle == NULL) { - HDF_LOGE("%{public}s:%{public}d devHandle is NULL", + HDF_LOGE("%s:%d devHandle is NULL", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } ret = UsbRawGetConfiguration(devHandle, &activeConfig); if (ret) { - HDF_LOGE("%{public}s:%{public}d UsbRawGetConfiguration failed, ret=%{public}d", + HDF_LOGE("%s:%d UsbRawGetConfiguration failed, ret=%d", __func__, __LINE__, ret); return HDF_FAILURE; } - HDF_LOGE("%{public}s:%{public}d activeConfig=%{public}d", __func__, __LINE__, activeConfig); + HDF_LOGE("%s:%d activeConfig=%d", __func__, __LINE__, activeConfig); dev = UsbRawGetDevice(devHandle); if (dev == NULL) { - HDF_LOGE("%{public}s:%{public}d UsbRawGetDevice failed", + HDF_LOGE("%s:%d UsbRawGetDevice failed", __func__, __LINE__); return HDF_FAILURE; } ret = UsbRawGetConfigDescriptor(dev, activeConfig, config); if (ret) { - HDF_LOGE("UsbRawGetConfigDescriptor failed, ret=%{public}d\n", ret); + HDF_LOGE("UsbRawGetConfigDescriptor failed, ret=%dn", ret); return HDF_FAILURE; } @@ -1156,7 +1134,7 @@ static int UsbAllocWriteRequests(struct AcmDevice *acm) snd->request = UsbRawAllocRequest(acm->devHandle, 0, acm->dataOutEp->maxPacketSize); snd->instance = acm; if (snd->request == NULL) { - HDF_LOGE("%{public}s: UsbRawAllocRequest failed", __func__); + HDF_LOGE("%s: UsbRawAllocRequest failed", __func__); return HDF_ERR_MALLOC_FAIL; } } @@ -1182,7 +1160,7 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) return HDF_FAILURE; } if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) { - HDF_LOGE("%s:%d OsalMutexInit fail", __func__, __LINE__); + HDF_LOGE("%s:%d OsalMutexInit failed", __func__, __LINE__); goto error; } @@ -1194,7 +1172,7 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, (const void*)info->interfaceNumber, info->interfaceLength); if (err != EOK) { - HDF_LOGE("%s:%d memcpy_s failed err=%d", \ + HDF_LOGE("%s:%d memcpy_s failed err=%d", __func__, __LINE__, err); goto lock_error; } @@ -1211,7 +1189,7 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) lock_error: if (OsalMutexDestroy(&acm->lock)) { - HDF_LOGE("%s:%d OsalMutexDestroy fail", __func__, __LINE__); + HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__); } error: OsalMemFree(acm); @@ -1228,7 +1206,7 @@ static int UsbAllocReadRequests(struct AcmDevice *acm) for (int i = 0; i < ACM_NR; i++) { acm->readReq[i] = UsbRawAllocRequest(acm->devHandle, 0, size); if (!acm->readReq[i]) { - HDF_LOGE("readReq request failed\n"); + HDF_LOGE("readReq request failed"); return HDF_ERR_MALLOC_FAIL; } @@ -1241,7 +1219,7 @@ static int UsbAllocReadRequests(struct AcmDevice *acm) ret = UsbRawFillBulkRequest(acm->readReq[i], acm->devHandle, &reqData); if (ret) { - HDF_LOGE("%{public}s: FillBulkRequest faile, ret=%{public}d \n", + HDF_LOGE("%s: FillBulkRequest failed, ret=%d n", __func__, ret); return HDF_FAILURE; } @@ -1258,7 +1236,7 @@ static int UsbAllocNotifyRequest(struct AcmDevice *acm) acm->notifyReq = UsbRawAllocRequest(acm->devHandle, 0, size); if (!acm->notifyReq) { - HDF_LOGE("notifyReq request fail\n"); + HDF_LOGE("notifyReq request failed"); return HDF_ERR_MALLOC_FAIL; } @@ -1271,7 +1249,7 @@ static int UsbAllocNotifyRequest(struct AcmDevice *acm) ret = UsbRawFillInterruptRequest(acm->notifyReq, acm->devHandle, &fillRequestData); if (ret) { - HDF_LOGE("%{public}s: FillInterruptRequest faile, ret=%{public}d", __func__, ret); + HDF_LOGE("%s: FillInterruptRequest failed, ret=%d", __func__, ret); return HDF_FAILURE; } @@ -1285,62 +1263,62 @@ static int32_t UsbSerialInit(struct AcmDevice *acm) int32_t ret; if (acm->initFlag == true) { - HDF_LOGE("%{public}s:%{public}d: initFlag is true", __func__, __LINE__); + HDF_LOGE("%s:%d: initFlag is true", __func__, __LINE__); return HDF_SUCCESS; } ret = UsbRawInit(NULL); if (ret) { - HDF_LOGE("%{public}s:%{public}d UsbRawInit failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbRawInit failed", __func__, __LINE__); return HDF_ERR_IO; } acm->session = session; devHandle = UsbRawOpenDevice(session, acm->busNum, acm->devAddr); if (devHandle == NULL) { - HDF_LOGE("%{public}s:%{public}d UsbRawOpenDevice failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbRawOpenDevice failed", __func__, __LINE__); ret = HDF_FAILURE; goto err_open_device; } acm->devHandle = devHandle; ret = UsbGetConfigDescriptor(devHandle, &acm->config); if (ret) { - HDF_LOGE("%{public}s:%{public}d UsbGetConfigDescriptor failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbGetConfigDescriptor failed", __func__, __LINE__); ret = HDF_FAILURE; goto err_get_desc; } ret = UsbParseConfigDescriptor(acm, acm->config); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s:%{public}d UsbParseConfigDescriptor failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbParseConfigDescriptor failed", __func__, __LINE__); ret = HDF_FAILURE; goto err_parse_desc; } ret = AcmWriteBufAlloc(acm); if (ret < 0) { - HDF_LOGE("%{public}s:%{public}d AcmWriteBufAlloc failed", __func__, __LINE__); + HDF_LOGE("%s:%d AcmWriteBufAlloc failed", __func__, __LINE__); ret = HDF_FAILURE; goto err_alloc_write_buf; } ret = UsbAllocWriteRequests(acm); if (ret < 0) { - HDF_LOGE("%{public}s:%{public}d UsbAllocWriteRequests failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbAllocWriteRequests failed", __func__, __LINE__); ret = HDF_FAILURE; goto err_alloc_write_reqs; } ret = UsbAllocNotifyRequest(acm); if (ret) { - HDF_LOGE("%{public}s:%{public}d UsbAllocNotifyRequests failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbAllocNotifyRequests failed", __func__, __LINE__); goto err_alloc_notify_req; } ret = UsbAllocReadRequests(acm); if (ret) { - HDF_LOGE("%{public}s:%{public}d UsbAllocReadRequests failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbAllocReadRequests failed", __func__, __LINE__); goto err_alloc_read_reqs; } ret = UsbStartIo(acm); if (ret) { - HDF_LOGE("%{public}s:%{public}d UsbAllocReadRequests failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbAllocReadRequests failed", __func__, __LINE__); goto err_start_io; } @@ -1351,13 +1329,13 @@ static int32_t UsbSerialInit(struct AcmDevice *acm) ret = UsbRawSubmitRequest(acm->notifyReq); if (ret) { - HDF_LOGE("%{public}s:%{public}d UsbRawSubmitRequest failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbRawSubmitRequest failed", __func__, __LINE__); goto err_submit_req; } acm->initFlag = true; - HDF_LOGD("%{public}s:%{public}d=========================OK", __func__, __LINE__); + HDF_LOGD("%s:%d=========================OK", __func__, __LINE__); return HDF_SUCCESS; @@ -1387,7 +1365,7 @@ err_open_device: static void UsbSerialRelease(struct AcmDevice *acm) { if (acm->initFlag == false) { - HDF_LOGE("%{public}s:%{public}d: initFlag is false", __func__, __LINE__); + HDF_LOGE("%s:%d: initFlag is false", __func__, __LINE__); return; } @@ -1416,7 +1394,7 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device) int32_t ret; if (device == NULL) { - HDF_LOGE("%{public}s:%{public}d device is null", __func__, __LINE__); + HDF_LOGE("%s:%d device is null", __func__, __LINE__); return HDF_ERR_INVALID_OBJECT; } acm = (struct AcmDevice *)device->service; @@ -1425,13 +1403,13 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device) ret = UsbSerialDeviceAlloc(acm); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s:%{public}d UsbSerialDeviceAlloc failed", __func__, __LINE__); + HDF_LOGE("%s:%d UsbSerialDeviceAlloc failed", __func__, __LINE__); } acm->initFlag = false; g_rawAcmReleaseFlag = false; - HDF_LOGD("%{public}s:%{public}d init ok!", __func__, __LINE__); + HDF_LOGD("%s:%d init ok!", __func__, __LINE__); return ret; } @@ -1440,20 +1418,20 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) { struct AcmDevice *acm = NULL; if (device == NULL) { - HDF_LOGE("%{public}s: device is NULL", __func__); + HDF_LOGE("%s: device is NULL", __func__); return; } acm = (struct AcmDevice *)device->service; if (acm == NULL) { - HDF_LOGE("%{public}s: acm is null", __func__); + HDF_LOGE("%s: acm is null", __func__); return; } g_rawAcmReleaseFlag = true; if (acm->initFlag == true) { - HDF_LOGE("%{public}s:%{public}d UsbSerialRelease", __func__, __LINE__); + HDF_LOGE("%s:%d UsbSerialRelease", __func__, __LINE__); UsbSerialRelease(acm); } UsbSeriaDevicelFree(acm); @@ -1462,12 +1440,12 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) OsalMutexDestroy(&acm->lock); OsalMemFree(acm); acm = NULL; - HDF_LOGD("%{public}s:%{public}d exit", __func__, __LINE__); + HDF_LOGD("%s:%d exit", __func__, __LINE__); } struct HdfDriverEntry g_usbSerialRawDriverEntry = { .moduleVersion = 1, - .moduleName = "usbhost_acm_rawapi", // Driver module name, which must be the same as that configured in the .hcs file. + ..moduleName = "usbhost_acm_rawapi", // Driver module name, which must be the same as that configured in the .hcs file. .Bind = UsbSerialDriverBind, .Init = UsbSerialDriverInit, .Release = UsbSerialDriverRelease, @@ -1475,8 +1453,9 @@ struct HdfDriverEntry g_usbSerialRawDriverEntry = { HDF_INIT(g_usbSerialRawDriverEntry); ``` -### Developing Driver Using Device DDK APIs -The core code of the USB Abstract Control Model (ACM) device is available in **drivers\peripheral\usb\gadget\function\acm\cdcacm.c**. The following is an example. +### Developing Driver Using Device DDK APIs + +The core code of the USB Abstract Control Model (ACM) device is available in **drivers\peripheral\usb\gadget\function\acm\cdcacm.c**. The following is an example: ``` 1. Create a device. @@ -1486,7 +1465,7 @@ static int32_t AcmCreateFuncDevice(struct UsbAcmDevice *acm, struct UsbFnDevice *fnDev = NULL; struct UsbFnDescriptorData descData; uint8_t useHcs; - ... + ... if (useHcs == 0) { descData.type = USBFN_DESC_DATA_TYPE_DESC; descData.descriptor = &g_masterFuncDevice; @@ -1497,7 +1476,7 @@ if (useHcs == 0) { /* Create a device. */ fnDev = (struct UsbFnDevice *)UsbFnDeviceCreate(acm->udcName, &descData); if (fnDev == NULL) { - HDF_LOGE("%{public}s: create usb function device failed", __func__); + HDF_LOGE("%s: create usb function device failed", __func__); return HDF_FAILURE; } ... @@ -1548,7 +1527,7 @@ static int32_t AcmSendNotifyRequest(struct UsbAcmDevice *acm, uint8_t type, uint16_t value, void *data, uint32_t length) { ... -/* Send a request asynchronously. */ +/* Send an asynchronous request.*/ ret = UsbFnRequestSubmitAsync(req); ... } @@ -1564,7 +1543,7 @@ int32_t ret; /* Remove the device. */ ret = UsbFnDeviceRemove(acm->fnDev); if (ret != HDF_SUCCESS) { - HDF_LOGE("%{public}s: remove usb function device failed", __func__); + HDF_LOGE("%s: remove usb function device failed", __func__); } return ret; }