未验证 提交 82e000a2 编写于 作者: O openharmony_ci 提交者: Gitee

!19330 fix:Refresh USB Community Driver Document

Merge pull request !19330 from 王易朋/master
...@@ -84,69 +84,69 @@ USB主机端驱动程序开发相关接口(普通模式)如下,具体接 ...@@ -84,69 +84,69 @@ USB主机端驱动程序开发相关接口(普通模式)如下,具体接
**表1** USB主机端驱动程序开发相关接口(普通模式) **表1** USB主机端驱动程序开发相关接口(普通模式)
| 接口名称 | 功能描述 | | 接口名称 | 功能描述 |
| -------- | -------- | | -------- | -------- |
| int32_t UsbInitHostSdk(struct UsbSession \*\*session); | USB主机端驱动开发工具包初始化 | | int32_t UsbInitHostSdk(struct UsbSession \*\*session); | USB主机端驱动开发工具包初始化 |
| const&nbsp;struct&nbsp;UsbInterface&nbsp;\*UsbClaimInterface(const<br/>struct&nbsp;UsbSession&nbsp;\*session,&nbsp;uint8_t&nbsp;busNum,&nbsp;uint8_t<br/>usbAddr,&nbsp;uint8_t&nbsp;interfaceIndex); | 获取USB接口对象 | | struct UsbInterface *UsbClaimInterface(const struct<br/> UsbSession *session, uint8_t busNum, uint8_t usbAddr, <br/>uint8_t interfaceIndex) | 获取USB接口对象 |
| UsbInterfaceHandle&nbsp;\*UsbOpenInterface(const&nbsp;struct<br/>UsbInterface&nbsp;\*interfaceObj); | 打开USB对象接口 | | UsbInterfaceHandle&nbsp;\*UsbOpenInterface(const&nbsp;struct<br/>UsbInterface&nbsp;\*interfaceObj); | 打开USB对象接口 |
| int32_t&nbsp;UsbGetPipeInfo(const&nbsp;UsbInterfaceHandle<br/>\*interfaceHandle,&nbsp;uint8_t&nbsp;settingIndex,&nbsp;uint8_t&nbsp;pipeId,<br/>struct&nbsp;UsbPipeInfo&nbsp;\*pipeInfo); | 获取指定可选设置的管道信息 | | int32_t&nbsp;UsbGetPipeInfo(const&nbsp;UsbInterfaceHandle<br/>\*interfaceHandle,&nbsp;uint8_t&nbsp;settingIndex,&nbsp;uint8_t&nbsp;pipeId,<br/>struct&nbsp;UsbPipeInfo&nbsp;\*pipeInfo); | 获取指定可选设置的管道信息 |
| struct&nbsp;UsbRequest&nbsp;\*UsbAllocRequest(const<br/>UsbInterfaceHandle&nbsp;\*interfaceHandle,&nbsp;int32_t&nbsp;isoPackets<br/>,&nbsp;int32_t&nbsp;length); | 分配请求对象 | | struct&nbsp;UsbRequest&nbsp;\*UsbAllocRequest(const<br/>UsbInterfaceHandle&nbsp;\*interfaceHandle,&nbsp;int32_t&nbsp;isoPackets<br/>,&nbsp;int32_t&nbsp;length); | 分配请求对象 |
| int32_t&nbsp;UsbFillRequest(const&nbsp;struct&nbsp;UsbRequest<br/>\*request,&nbsp;const&nbsp;UsbInterfaceHandle&nbsp;\*interfaceHandle,<br/>const&nbsp;struct&nbsp;UsbRequestParams&nbsp;\*params); | 填充请求 | | int32_t&nbsp;UsbFillRequest(const&nbsp;struct&nbsp;UsbRequest<br/>\*request,&nbsp;const&nbsp;UsbInterfaceHandle&nbsp;\*interfaceHandle,<br/>const&nbsp;struct&nbsp;UsbRequestParams&nbsp;\*params); | 填充请求 |
| int32_t&nbsp;UsbSubmitRequestSync(const&nbsp;struct&nbsp;UsbRequest<br/>\*request); | 发送同步请求 | | int32_t&nbsp;UsbSubmitRequestSync(const&nbsp;struct&nbsp;UsbRequest<br/>\*request); | 发送同步请求 |
USB主机端驱动程序开发相关接口(专家模式)如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/host/usb_raw_api.h) USB主机端驱动程序开发相关接口(专家模式)如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/host/usb_raw_api.h)
**表2** USB主机端驱动程序开发相关接口(专家模式) **表2** USB主机端驱动程序开发相关接口(专家模式)
| 接口名称 | 功能描述 | | 接口名称 | 功能描述 |
| -------- | -------- | | -------- | -------- |
| int32_t&nbsp;UsbRawInit(struct&nbsp;UsbSession&nbsp;\*\*session); | USB驱动开发工具包专家模式初始化 | | int32_t&nbsp;UsbRawInit(struct&nbsp;UsbSession&nbsp;\*\*session); | USB驱动开发工具包专家模式初始化 |
| UsbRawHandle&nbsp;\*UsbRawOpenDevice(const&nbsp;struct<br/>UsbSession&nbsp;\*session,&nbsp;uint8_t&nbsp;busNum,&nbsp;uint8_t<br/>usbAddr); | 打开USB设备对象 | | UsbRawHandle&nbsp;\*UsbRawOpenDevice(const&nbsp;struct<br/>UsbSession&nbsp;\*session,&nbsp;uint8_t&nbsp;busNum,&nbsp;uint8_t<br/>usbAddr); | 打开USB设备对象 |
| int32_t&nbsp;UsbRawSendControlRequest(const&nbsp;struct<br/>UsbRawRequest&nbsp;\*request,&nbsp;const&nbsp;UsbRawHandle<br/>\*devHandle,&nbsp;const&nbsp;struct&nbsp;UsbControlRequestData<br/>\*requestData); | 执行同步控制传输 | | int32_t&nbsp;UsbRawSendControlRequest(const&nbsp;struct<br/>UsbRawRequest&nbsp;\*request,&nbsp;const&nbsp;UsbRawHandle<br/>\*devHandle,&nbsp;const&nbsp;struct&nbsp;UsbControlRequestData<br/>\*requestData); | 执行同步控制传输 |
| int32_t&nbsp;UsbRawSendBulkRequest(const&nbsp;struct<br/>UsbRawRequest&nbsp;\*request,&nbsp;const&nbsp;UsbRawHandle<br/>\*devHandle,&nbsp;const&nbsp;struct&nbsp;UsbRequestData<br/>\*requestData); | 执行同步批量传输 | | int32_t&nbsp;UsbRawSendBulkRequest(const&nbsp;struct<br/>UsbRawRequest&nbsp;\*request,&nbsp;const&nbsp;UsbRawHandle<br/>\*devHandle,&nbsp;const&nbsp;struct&nbsp;UsbRequestData<br/>\*requestData); | 执行同步批量传输 |
| int32_t&nbsp;UsbRawSendInterruptRequest(const&nbsp;struct<br/>UsbRawRequest&nbsp;\*request,&nbsp;const&nbsp;UsbRawHandle<br/>\*devHandle,&nbsp;const&nbsp;struct&nbsp;UsbRequestData<br/>\*requestData); | 执行同步中断传输 | | int32_t&nbsp;UsbRawSendInterruptRequest(const&nbsp;struct<br/>UsbRawRequest&nbsp;\*request,&nbsp;const&nbsp;UsbRawHandle<br/>\*devHandle,&nbsp;const&nbsp;struct&nbsp;UsbRequestData<br/>\*requestData); | 执行同步中断传输 |
| int32_t&nbsp;UsbRawGetConfigDescriptor(const&nbsp;UsbRawDevice<br/>\*rawDev,&nbsp;uint8_t&nbsp;configIndex,&nbsp;struct<br/>UsbRawConfigDescriptor&nbsp;\*\*config); | 获取给定设备指定ID的设备配置描述符 | | int32_t&nbsp;UsbRawGetConfigDescriptor(const&nbsp;UsbRawDevice<br/>\*rawDev,&nbsp;uint8_t&nbsp;configIndex,&nbsp;struct<br/>UsbRawConfigDescriptor&nbsp;\*\*config); | 获取给定设备指定ID的设备配置描述符 |
| int32_t&nbsp;UsbRawFillInterruptRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request,&nbsp;const&nbsp;UsbRawHandle&nbsp;\*devHandle,&nbsp;const&nbsp;struct<br/>UsbRawFillRequestData&nbsp;\*fillData); | 填充中断传输请求所需信息 | | int32_t&nbsp;UsbRawFillInterruptRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request,&nbsp;const&nbsp;UsbRawHandle&nbsp;\*devHandle,&nbsp;const&nbsp;struct<br/>UsbRawFillRequestData&nbsp;\*fillData); | 填充中断传输请求所需信息 |
| int32_t&nbsp;UsbRawFillIsoRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request,&nbsp;const&nbsp;UsbRawHandle&nbsp;\*devHandle,&nbsp;const&nbsp;struct<br/>UsbRawFillRequestData&nbsp;\*fillData); | 填充同步传输(Isochronous&nbsp;Transfers)请求所需信息 | | int32_t&nbsp;UsbRawFillIsoRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request,&nbsp;const&nbsp;UsbRawHandle&nbsp;\*devHandle,&nbsp;const&nbsp;struct<br/>UsbRawFillRequestData&nbsp;\*fillData); | 填充同步传输(Isochronous&nbsp;Transfers)请求所需信息 |
| int32_t&nbsp;UsbRawSubmitRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request); | 提交一个传输请求 | | int32_t&nbsp;UsbRawSubmitRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request); | 提交一个传输请求 |
| int32_t&nbsp;UsbRawCancelRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request); | 取消一个传输请求 | | int32_t&nbsp;UsbRawCancelRequest(const&nbsp;struct&nbsp;UsbRawRequest<br/>\*request); | 取消一个传输请求 |
| int32_t&nbsp;UsbRawHandleRequests(const&nbsp;UsbRawHandle<br/>\*devHandle); | 传输请求事件完成处理 | | int32_t&nbsp;UsbRawHandleRequests(const&nbsp;UsbRawHandle<br/>\*devHandle); | 传输请求事件完成处理 |
USB设备端用于管理USB设备的相关接口如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_device.h) USB设备端用于管理USB设备的相关接口如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_device.h)
**表3** USB设备端用于管理USB设备的相关接口 **表3** USB设备端用于管理USB设备的相关接口
| 接口名称 | 功能描述 | | 接口名称 | 功能描述 |
| -------- | -------- | | -------- | -------- |
| const&nbsp;struct&nbsp;UsbFnDevice&nbsp;\*UsbFnCreateDevice(const<br/>char&nbsp;\*udcName,&nbsp;const&nbsp;struct&nbsp;UsbFnDescriptorData<br/>\*descriptor); | 创建USB设备 | | const&nbsp;struct&nbsp;UsbFnDevice&nbsp;\*UsbFnCreateDevice(const<br/>char&nbsp;\*udcName,&nbsp;const&nbsp;struct&nbsp;UsbFnDescriptorData<br/>\*descriptor); | 创建USB设备 |
| int32_t&nbsp;UsbFnRemoveDevice(struct&nbsp;UsbFnDevice<br/>\*fnDevice); | 删除USB设备 | | int32_t&nbsp;UsbFnRemoveDevice(struct&nbsp;UsbFnDevice<br/>\*fnDevice); | 删除USB设备 |
| const&nbsp;struct&nbsp;UsbFnDevice&nbsp;\*UsbFnGetDevice(const&nbsp;char<br/>\*udcName); | 获取USB设备 | | const&nbsp;struct&nbsp;UsbFnDevice&nbsp;\*UsbFnGetDevice(const&nbsp;char<br/>\*udcName); | 获取USB设备 |
USB设备端用于USB接口定义的相关接口如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_interface.h) USB设备端用于USB接口定义的相关接口如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_interface.h)
**表4** USB设备端用于USB接口定义的相关接口 **表4** USB设备端用于USB接口定义的相关接口
| 接口名称 | 功能描述 | | 接口名称 | 功能描述 |
| -------- | -------- | | -------- | -------- |
| int32_t&nbsp;UsbFnStartRecvInterfaceEvent(struct<br/>UsbFnInterface&nbsp;\*interface,&nbsp;uint32_t&nbsp;eventMask,<br/>UsbFnEventCallback&nbsp;callback,&nbsp;void&nbsp;\*context); | 开始接受Event事件 | | int32_t&nbsp;UsbFnStartRecvInterfaceEvent(struct<br/>UsbFnInterface&nbsp;\*interface,&nbsp;uint32_t&nbsp;eventMask,<br/>UsbFnEventCallback&nbsp;callback,&nbsp;void&nbsp;\*context); | 开始接受Event事件 |
| int32_t&nbsp;UsbFnStopRecvInterfaceEvent(struct<br/>UsbFnInterface&nbsp;\*interface); | 停止接受Event事件 | | int32_t&nbsp;UsbFnStopRecvInterfaceEvent(struct<br/>UsbFnInterface&nbsp;\*interface); | 停止接受Event事件 |
| UsbFnInterfaceHandle&nbsp;UsbFnOpenInterface(struct&nbsp;UsbFnInterface&nbsp;\*interface); | 打开一个接口 | | UsbFnInterfaceHandle&nbsp;UsbFnOpenInterface(struct&nbsp;UsbFnInterface&nbsp;\*interface); | 打开一个接口 |
| int32_t&nbsp;UsbFnCloseInterface(UsbFnInterfaceHandle&nbsp;handle); | 关闭一个接口 | | int32_t&nbsp;UsbFnCloseInterface(UsbFnInterfaceHandle&nbsp;handle); | 关闭一个接口 |
| int32_t&nbsp;UsbFnGetInterfacePipeInfo(struct&nbsp;UsbFnInterface<br/>\*interface,&nbsp;uint8_t&nbsp;pipeId,&nbsp;struct&nbsp;UsbFnPipeInfo&nbsp;\*info); | 获取管道信息 | | int32_t&nbsp;UsbFnGetInterfacePipeInfo(struct&nbsp;UsbFnInterface<br/>\*interface,&nbsp;uint8_t&nbsp;pipeId,&nbsp;struct&nbsp;UsbFnPipeInfo&nbsp;\*info); | 获取管道信息 |
| int32_t&nbsp;UsbFnSetInterfaceProp(const&nbsp;struct&nbsp;UsbFnInterface<br/>\*interface,&nbsp;const&nbsp;char&nbsp;\*name,&nbsp;const&nbsp;char&nbsp;\*value); | 设置自定义属性 | | int32_t&nbsp;UsbFnSetInterfaceProp(const&nbsp;struct&nbsp;UsbFnInterface<br/>\*interface,&nbsp;const&nbsp;char&nbsp;\*name,&nbsp;const&nbsp;char&nbsp;\*value); | 设置自定义属性 |
USB设备端用于管理USB数据请求的相关接口如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_request.h) USB设备端用于管理USB数据请求的相关接口如下,具体接口定义[见源码](https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_request.h)
**表5** USB设备端用于管理USB数据请求的相关接口 **表5** USB设备端用于管理USB数据请求的相关接口
| 接口名称 | 功能描述 | | 接口名称 | 功能描述 |
| -------- | -------- | | -------- | -------- |
| struct&nbsp;UsbFnRequest<br/>\*UsbFnAllocCtrlRequest(UsbFnInterfaceHandle&nbsp;handle,<br/>uint32_t&nbsp;len); | 申请一个控制请求 | | struct&nbsp;UsbFnRequest<br/>\*UsbFnAllocCtrlRequest(UsbFnInterfaceHandle&nbsp;handle,<br/>uint32_t&nbsp;len); | 申请一个控制请求 |
| struct&nbsp;UsbFnRequest&nbsp;\*UsbFnAllocRequest(UsbFnInterfaceHandle&nbsp;handle,<br/>uint8_t&nbsp;pipe,&nbsp;uint32_t&nbsp;len); | 申请一个数据请求 | | struct&nbsp;UsbFnRequest&nbsp;\*UsbFnAllocRequest(UsbFnInterfaceHandle&nbsp;handle,<br/>uint8_t&nbsp;pipe,&nbsp;uint32_t&nbsp;len); | 申请一个数据请求 |
| int32_t&nbsp;UsbFnFreeRequest(struct&nbsp;UsbFnRequest&nbsp;\*req); | 释放一个请求 | | int32_t&nbsp;UsbFnFreeRequest(struct&nbsp;UsbFnRequest&nbsp;\*req); | 释放一个请求 |
| int32_t&nbsp;UsbFnSubmitRequestAsync(struct&nbsp;UsbFnRequest<br/>\*req); | 发送异步请求 | | int32_t&nbsp;UsbFnSubmitRequestAsync(struct&nbsp;UsbFnRequest<br/>\*req); | 发送异步请求 |
| int32_t&nbsp;UsbFnSubmitRequestSync(struct&nbsp;UsbFnRequest<br/>\*req,&nbsp;uint32_t&nbsp;timeout); | 发送同步请求 | | int32_t&nbsp;UsbFnSubmitRequestSync(struct&nbsp;UsbFnRequest<br/>\*req,&nbsp;uint32_t&nbsp;timeout); | 发送同步请求 |
| int32_t&nbsp;UsbFnCancelRequest(struct&nbsp;UsbFnRequest&nbsp;\*req); | 取消请求 | | int32_t&nbsp;UsbFnCancelRequest(struct&nbsp;UsbFnRequest&nbsp;\*req); | 取消请求 |
### 开发步骤 ### 开发步骤
...@@ -295,14 +295,13 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -295,14 +295,13 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
1. 在设备功能代码中构造描述符。 1. 在设备功能代码中构造描述符。
```cpp ```cpp
static struct UsbFnFunction g_acmFunction = { // 功能描述符 static struct UsbFnFunction g_acmFunction = {
.enable = true, .funcName = "f_generic.a", //功能名称
.funcName = "f_generic.a", .strings = g_acmStrings, //字符串
.strings = g_acmStrings, .fsDescriptors = g_acmFsFunction, //初始化fs描述符
.fsDescriptors = g_acmFsFunction, .hsDescriptors = g_acmHsFunction, //初始化hs描述符
.hsDescriptors = g_acmHsFunction, .ssDescriptors = g_acmSsFunction, //初始化ss描述符
.ssDescriptors = g_acmSsFunction, .sspDescriptors = nullptr, //ss描述符置空
.sspDescriptors = NULL,
}; };
struct UsbFnFunction *g_functions[] = { struct UsbFnFunction *g_functions[] = {
#ifdef CDC_ECM #ifdef CDC_ECM
...@@ -311,8 +310,7 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -311,8 +310,7 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
#ifdef CDC_ACM #ifdef CDC_ACM
&g_acmFunction, &g_acmFunction,
#endif #endif
NULL nullptr};
};
static struct UsbFnConfiguration g_masterConfig = { // 配置描述符 static struct UsbFnConfiguration g_masterConfig = { // 配置描述符
.configurationValue = 1, .configurationValue = 1,
.iConfiguration = USB_FUNC_CONFIG_IDX, .iConfiguration = USB_FUNC_CONFIG_IDX,
...@@ -322,10 +320,10 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -322,10 +320,10 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
}; };
static struct UsbFnConfiguration *g_configs[] = { static struct UsbFnConfiguration *g_configs[] = {
&g_masterConfig, &g_masterConfig,
NULL, nullptr,
}; };
static struct UsbDeviceDescriptor g_cdcMasterDeviceDesc = { // 设备描述符 static struct UsbDeviceDescriptor g_cdcUsbFnDeviceDesc = { // 设备描述符
.bLength = sizeof(g_cdcMasterDeviceDesc), .bLength = sizeof(g_cdcUsbFnDeviceDesc),
.bDescriptorType = USB_DDK_DT_DEVICE, .bDescriptorType = USB_DDK_DT_DEVICE,
.bcdUSB = CpuToLe16(BCD_USB), .bcdUSB = CpuToLe16(BCD_USB),
.bDeviceClass = 0, .bDeviceClass = 0,
...@@ -340,10 +338,10 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -340,10 +338,10 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
.iSerialNumber = USB_FUNC_SERIAL_IDX, .iSerialNumber = USB_FUNC_SERIAL_IDX,
.bNumConfigurations = 1, .bNumConfigurations = 1,
}; };
static struct UsbFnDeviceDesc g_masterFuncDevice = { // 描述符入口 struct UsbFnDeviceDesc g_acmFnDevice = { //描述符入口
.deviceDesc = &g_cdcMasterDeviceDesc, .deviceDesc = &g_cdcUsbFnDeviceDesc,
.deviceStrings = g_devStrings, .deviceStrings = g_devStrings,
.configs = g_configs, .configs = g_configs,
}; };
``` ```
...@@ -351,14 +349,14 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -351,14 +349,14 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
```cpp ```cpp
if (useHcs == 0) { // 使用代码编写的描述符 if (useHcs == 0) { // 使用代码编写的描述符
descData.type = USBFN_DESC_DATA_TYPE_DESC; descData.type = USBFN_DESC_DATA_TYPE_DESC;
descData.descriptor = &g_acmFuncDevice; descData.descriptor = &g_acmFnDevice;
} else { // 使用hcs编写的描述符 } else { // 使用hcs编写的描述符
descData.type = USBFN_DESC_DATA_TYPE_PROP; devMgr->descData.type = USBFN_DESC_DATA_TYPE_PROP;
descData.property = acm->device->property; devMgr->descData.property = device->property;
} }
// 创建设备 // 创建设备
fnDev = (struct UsbFnDevice *) UsbFnCreateDevice(acm->udcName, &descData); fnDev = (struct UsbFnDevice *)UsbFnCreateDevice(devMgr->udcName, &devMgr->descData);
``` ```
3. 设备创建后,使用UsbFnGetInterface函数获取UsbInterface接口对象,并通过UsbFnGetInterfacePipeInfo函数获取USB管道信息。 3. 设备创建后,使用UsbFnGetInterface函数获取UsbInterface接口对象,并通过UsbFnGetInterfacePipeInfo函数获取USB管道信息。
...@@ -371,56 +369,53 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -371,56 +369,53 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
// 获取Handle // 获取Handle
handle = UsbFnOpenInterface(fnIface); handle = UsbFnOpenInterface(fnIface);
// 获取控制(EP0)Request // 获取控制(EP0)Request
req = UsbFnAllocCtrlRequest(acm->ctrlIface.handle, req = UsbFnAllocCtrlRequest(acmDevice->ctrlIface.handle, sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
// 获取Request // 获取Request
req = UsbFnAllocCtrlRequest(acm->ctrlIface.handle, req = UsbFnAllocCtrlRequest(acmDevice->ctrlIface.handle, sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
``` ```
4. 通过UsbFnStartRecvInterfaceEvent函数接收Event事件,并通过UsbFnEventCallback回调函数对Event事件做出响应。 4. 通过UsbFnStartRecvInterfaceEvent函数接收Event事件,并通过UsbFnEventCallback回调函数对Event事件做出响应。
```cpp ```cpp
// 开始接收Event事件 // 开始接收Event事件
ret = UsbFnStartRecvInterfaceEvent(acm->ctrlIface.fn, 0xff, UsbAcmEventCallback, acm); ret = UsbFnStartRecvInterfaceEvent(acmDevice->ctrlIface.fn, 0xff, AcmEventCallback, acmDevice);
// Event处理回调函数 // Event处理回调函数
static void UsbAcmEventCallback(struct UsbFnEvent *event) static void UsbAcmEventCallback(struct UsbFnEvent *event)
{ {
struct UsbAcmDevice *acm = NULL; struct UsbAcmDevice *acm = NULL;
if (event == NULL || event->context == NULL) { if (event == NULL || event->context == NULL) {
HDF_LOGE("%s: event is null", __func__); HDF_LOGE("%{public}s: event is null", __func__);
return; return;
} }
acm = (struct UsbAcmDevice *)event->context; acm = (struct UsbAcmDevice *)event->context;
switch (event->type) { switch (event->type) {
case USBFN_STATE_BIND: case USBFN_STATE_BIND:
HDF_LOGI("%s: receive bind event", __func__); HDF_LOGI("%{public}s: receive bind event", __func__);
break; break;
case USBFN_STATE_UNBIND: case USBFN_STATE_UNBIND:
HDF_LOGI("%s: receive unbind event", __func__); HDF_LOGI("%{public}s: receive unbind event", __func__);
break; break;
case USBFN_STATE_ENABLE: case USBFN_STATE_ENABLE:
HDF_LOGI("%s: receive enable event", __func__); HDF_LOGI("%{public}s: receive enable event", __func__);
AcmEnable(acm); AcmEnable(acm);
break; break;
case USBFN_STATE_DISABLE: case USBFN_STATE_DISABLE:
HDF_LOGI("%s: receive disable event", __func__); HDF_LOGI("%{public}s: receive disable event", __func__);
AcmDisable(acm); AcmDisable(acm);
acm->enableEvtCnt = 0; acm->enableEvtCnt = 0;
break; break;
case USBFN_STATE_SETUP: case USBFN_STATE_SETUP:
HDF_LOGI("%s: receive setup event", __func__); HDF_LOGI("%{public}s: receive setup event", __func__);
if (event->setup != NULL) { if (event->setup != NULL) {
AcmSetup(acm, event->setup); AcmSetup(acm, event->setup);
} }
break; break;
case USBFN_STATE_SUSPEND: case USBFN_STATE_SUSPEND:
HDF_LOGI("%s: receive suspend event", __func__); HDF_LOGI("%{public}s: receive suspend event", __func__);
AcmSuspend(acm); AcmSuspend(acm);
break; break;
case USBFN_STATE_RESUME: case USBFN_STATE_RESUME:
HDF_LOGI("%s: receive resume event", __func__); HDF_LOGI("%{public}s: receive resume event", __func__);
AcmResume(acm); AcmResume(acm);
break; break;
default: default:
...@@ -434,10 +429,16 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -434,10 +429,16 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
```cpp ```cpp
notify = (struct UsbCdcNotification *)req->buf; notify = (struct UsbCdcNotification *)req->buf;
... ...
if (memcpy_s((void *)(notify + 1), length, data, length) != EOK) { ret = memcpy_s((void *)(notify + 1), length, data, length);
if (ret != EOK) {
HDF_LOGE("%s: memcpy_s failed", __func__);
return HDF_FAILURE; return HDF_FAILURE;
} }
ret = UsbFnSubmitRequestAsync(req); // 异步发送 ret = UsbFnSubmitRequestAsync(req); // 异步发送
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: send notify request failed", __func__);
acm->notifyReq = req;
}
``` ```
### 开发实例 ### 开发实例
...@@ -450,34 +451,35 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分 ...@@ -450,34 +451,35 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
#include "usb_serial.h" #include "usb_serial.h"
#include "hdf_base.h" #include "hdf_base.h"
#include "hdf_log.h" #include "hdf_log.h"
#include "hdf_usb_pnp_manage.h"
#include "osal_mem.h" #include "osal_mem.h"
#include "osal_time.h" #include "osal_time.h"
#include "securec.h" #include "securec.h"
#include "usb_ddk_interface.h" #include "usb_ddk_interface.h"
#include "hdf_usb_pnp_manage.h"
#define HDF_LOG_TAG USB_HOST_ACM #define HDF_LOG_TAG USB_HOST_ACM
#define STR_LEN 512 #define STR_LEN 512
static struct UsbRequest *g_syncRequest = NULL; // 定义一个USB请求 static struct UsbRequest *g_syncRequest = NULL; // 定义一个USB请求
static struct UsbRequest *g_ctrlCmdRequest = NULL; static struct UsbRequest *g_ctrlCmdRequest = NULL;
static bool g_acmReleaseFlag = false; static bool g_acmReleaseFlag = false;
static uint8_t *g_acmReadBuffer = NULL; static uint8_t *g_acmReadBuffer = NULL;
... ...
static int32_t SerialCtrlMsg(struct AcmDevice *acm, uint8_t request, static int32_t SerialCtrlMsg(struct AcmDevice *acm, uint8_t request, uint16_t value, void *buf, uint16_t len)
uint16_t value, void *buf, uint16_t len)
{ {
int32_t ret; int32_t ret;
uint16_t index = acm->intPipe->interfaceId; if (acm == NULL || buf == NULL || acm->intPipe == NULL) {
struct UsbControlParams controlParams; HDF_LOGE("%s:invalid param", __func__);
struct UsbRequestParams params; // 定义一个USB请求参数对象
if (acm == NULL || buf == NULL) {
return HDF_ERR_IO; return HDF_ERR_IO;
} }
uint16_t index = acm->intPipe->interfaceId;
struct UsbControlParams controlParams = {};
struct UsbRequestParams parmas = {}; // 定义一个USB请求参数对象
if (acm->ctrlReq == NULL) { if (acm->ctrlReq == NULL) {
// 为获取到的UsbInterfaceHandle预先分配待发送的IO Request对象 // 为获取到的UsbInterfaceHandle预先分配待发送的IO Request对象
acm->ctrlReq = UsbAllocRequest(acm->ctrDevHandle, 0, len); acm->ctrlReq = UsbAllocRequest(acm->ctrDevHandle, 0, len);
if (acm->ctrlReq == NULL) { if (acm->ctrlReq == NULL) {
HDF_LOGE("%s: UsbAllocRequest failed", __func__);
return HDF_ERR_IO; return HDF_ERR_IO;
} }
} }
...@@ -485,72 +487,70 @@ static int32_t SerialCtrlMsg(struct AcmDevice *acm, uint8_t request, ...@@ -485,72 +487,70 @@ static int32_t SerialCtrlMsg(struct AcmDevice *acm, uint8_t request,
controlParams.request = request; controlParams.request = request;
controlParams.target = USB_REQUEST_TARGET_INTERFACE; // 接口对象 controlParams.target = USB_REQUEST_TARGET_INTERFACE; // 接口对象
controlParams.reqType = USB_REQUEST_TYPE_CLASS; // 请求类型 controlParams.reqType = USB_REQUEST_TYPE_CLASS; // 请求类型
controlParams.direction = USB_REQUEST_DIR_TO_DEVICE; // 从主机到设备的数据传输 controlParams.directon = USB_REQUEST_DIR_TO_DEVICE; // 从主机到设备的数据传输
controlParams.value = value; controlParams.value = value;
controlParams.index = index; controlParams.index = index;
controlParams.data = buf; controlParams.data = buf;
controlParams.size = len; controlParams.size = len;
params.interfaceId = USB_CTRL_INTERFACE_ID; // 定义USB控制接口的默认ID parmas.interfaceId = USB_CTRL_INTERFACE_ID; // 定义USB控制接口的默认ID
params.pipeAddress = acm->ctrPipe->pipeAddress; if (acm->ctrPipe != NULL) {
params.pipeId = acm->ctrPipe->pipeId; parmas.pipeAddress = acm->ctrPipe->pipeAddress;
params.requestType = USB_REQUEST_PARAMS_CTRL_TYPE; // 控制类型 parmas.pipeId = acm->ctrPipe->pipeId;
params.timeout = USB_CTRL_SET_TIMEOUT; // 设置超时时间 }
params.ctrlReq = UsbControlSetUp(&controlParams); parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE; // 控制类型
parmas.timeout = USB_CTRL_SET_TIMEOUT; // 设置超时时间
parmas.ctrlReq = UsbControlSetUp(&controlParams);
parmas.callback = NULL;
// 根据params填充预先分配的IO Request // 根据params填充预先分配的IO Request
ret = UsbFillRequest(acm->ctrlReq, acm->ctrDevHandle, &params); ret = UsbFillRequest(acm->ctrlReq, acm->ctrDevHandle, &parmas);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: UsbFillRequest failed, ret = %d ", __func__, ret);
return ret; return ret;
} }
// 发送同步IO Request // 发送同步IO Request
ret = UsbSubmitRequestSync(acm->ctrlReq); ret = UsbSubmitRequestSync(acm->ctrlReq);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
HDF_LOGE("UsbSubmitRequestSync failed, ret = %d ", ret);
return ret; return ret;
} }
if (!acm->ctrlReq->compInfo.status) { if (!acm->ctrlReq->compInfo.status) {
HDF_LOGE("%s status=%d ", __func__, acm->ctrlReq->compInfo.status); HDF_LOGE("%s status=%d ", __func__, acm->ctrlReq->compInfo.status);
} }
return HDF_SUCCESS; return HDF_SUCCESS;
} }
... ...
static struct UsbInterface *GetUsbInterfaceById(const struct AcmDevice *acm, static struct UsbInterface *GetUsbInterfaceById(const struct AcmDevice *acm, uint8_t interfaceIndex)
uint8_t interfaceIndex)
{ {
struct UsbInterface *tmpIf = NULL;
// 获取UsbInterface接口对象 // 获取UsbInterface接口对象
tmpIf = (struct UsbInterface *)UsbClaimInterface(acm->session, acm->busNum, acm->devAddr, interfaceIndex); return UsbClaimInterface(acm->session, acm->busNum, acm->devAddr, interfaceIndex);
return tmpIf;
} }
... ...
static struct UsbPipeInfo *EnumePipe(const struct AcmDevice *acm, static struct UsbPipeInfo *EnumePipe(
uint8_t interfaceIndex, UsbPipeType pipeType, UsbPipeDirection pipeDirection) const struct AcmDevice *acm, uint8_t interfaceIndex, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
{ {
uint8_t i;
int32_t ret;
struct UsbInterfaceInfo *info = NULL; // 定义一个USB接口信息对象 struct UsbInterfaceInfo *info = NULL; // 定义一个USB接口信息对象
UsbInterfaceHandle *interfaceHandle = NULL; // 定义一个USB接口操作句柄,就是void *类型 UsbInterfaceHandle *interfaceHandle = NULL; // 定义一个USB接口操作句柄,就是void *类型
if (pipeType == USB_PIPE_TYPE_CONTROL) if (pipeType == USB_PIPE_TYPE_CONTROL) {
{
info = &acm->ctrIface->info; info = &acm->ctrIface->info;
interfaceHandle = acm->ctrDevHandle; interfaceHandle = acm->ctrDevHandle;
} } else {
else
{
info = &acm->iface[interfaceIndex]->info;
// 根据interfaceIndex获取设备句柄 // 根据interfaceIndex获取设备句柄
info = &acm->iface[interfaceIndex]->info;
interfaceHandle = InterfaceIdToHandle(acm, info->interfaceIndex); interfaceHandle = InterfaceIdToHandle(acm, info->interfaceIndex);
} }
for (i = 0; i <= info->pipeNum; i++) { for (uint8_t i = 0; i <= info->pipeNum; i++) {
struct UsbPipeInfo p; struct UsbPipeInfo p;
// 获取指定索引为i的pipeInfo信息 // 获取指定索引为i的pipeInfo信息
ret = UsbGetPipeInfo(interfaceHandle, info->curAltSetting, i, &p); int32_t ret = UsbGetPipeInfo(interfaceHandle, info->curAltSetting, i, &p);
if (ret < 0) { if (ret < 0) {
continue; continue;
} }
if ((p.pipeDirection == pipeDirection) && (p.pipeType == pipeType)) { if ((p.pipeDirection == pipeDirection) && (p.pipeType == pipeType)) {
struct UsbPipeInfo *pi = OsalMemCalloc(sizeof(*pi)); // 开辟内存并初始化 struct UsbPipeInfo *pi = OsalMemCalloc(sizeof(*pi)); // 开辟内存并初始化
if (pi == NULL) { if (pi == NULL) {
HDF_LOGE("%s: Alloc pipe failed", __func__);
return NULL; return NULL;
} }
p.interfaceId = info->interfaceIndex; p.interfaceId = info->interfaceIndex;
...@@ -561,11 +561,11 @@ static struct UsbPipeInfo *EnumePipe(const struct AcmDevice *acm, ...@@ -561,11 +561,11 @@ static struct UsbPipeInfo *EnumePipe(const struct AcmDevice *acm,
return NULL; return NULL;
} }
static struct UsbPipeInfo *GetPipe(const struct AcmDevice *acm, static struct UsbPipeInfo *GetPipe(const struct AcmDevice *acm, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
UsbPipeType pipeType, UsbPipeDirection pipeDirection)
{ {
uint8_t i; uint8_t i;
if (acm == NULL) { if (acm == NULL) {
HDF_LOGE("%s: invalid param", __func__);
return NULL; return NULL;
} }
for (i = 0; i < acm->interfaceCnt; i++) { for (i = 0; i < acm->interfaceCnt; i++) {
...@@ -590,39 +590,48 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) ...@@ -590,39 +590,48 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
errno_t err; errno_t err;
struct AcmDevice *acm = NULL; struct AcmDevice *acm = NULL;
if (device == NULL) { if (device == NULL) {
HDF_LOGE("%s: device is null", __func__);
return HDF_ERR_INVALID_OBJECT; return HDF_ERR_INVALID_OBJECT;
} }
//开辟内存空间
acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*acm)); acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*acm));
if (acm == NULL) { if (acm == NULL) {
HDF_LOGE("%s: Alloc usb serial device failed", __func__);
return HDF_FAILURE; return HDF_FAILURE;
} }
// 初始化互斥锁,&acm->lock表示指向互斥量的指针 // 初始化互斥锁,&acm->lock表示指向互斥量的指针
if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) { if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) {
goto error; HDF_LOGE("%s:%d OsalMutexInit failed", __func__, __LINE__);
goto ERROR;
} }
info = (struct UsbPnpNotifyServiceInfo *)device->priv; info = (struct UsbPnpNotifyServiceInfo *)device->priv;
if (info != NULL) { if (info != NULL) {
acm->busNum = info->busNum; HDF_LOGD("%s:%d busNum=%d,devAddr=%d,interfaceLength=%d", __func__, __LINE__, info->busNum, info->devNum,
acm->devAddr = info->devNum; info->interfaceLength);
acm->busNum = (uint8_t)info->busNum;
acm->devAddr = (uint8_t)info->devNum;
acm->interfaceCnt = info->interfaceLength; acm->interfaceCnt = info->interfaceLength;
err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, (const void *)info->interfaceNumber,
(const void*)info->interfaceNumber, info->interfaceLength); info->interfaceLength);
if (err != EOK) { if (err != EOK) {
goto lock_error; HDF_LOGE("%s:%d memcpy_s failed err = %d", __func__, __LINE__, err);
goto LOCK_ERROR;
} }
} else { } else {
goto lock_error; HDF_LOGE("%s:%d info is null!", __func__, __LINE__);
goto LOCK_ERROR;
} }
acm->device = device; acm->device = device;
device->service = &(acm->service); device->service = &(acm->service);
acm->device->service->Dispatch = UsbSerialDeviceDispatch; acm->device->service->Dispatch = UsbSerialDeviceDispatch;
HDF_LOGD("UsbSerialDriverBind=========================OK");
return HDF_SUCCESS; return HDF_SUCCESS;
lock_error: LOCK_ERROR:
if (OsalMutexDestroy(&acm->lock)) { if (OsalMutexDestroy(&acm->lock)) {
HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__); HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__);
} }
error: ERROR:
OsalMemFree(acm); OsalMemFree(acm);
acm = NULL; acm = NULL;
return HDF_FAILURE; return HDF_FAILURE;
...@@ -631,32 +640,34 @@ error: ...@@ -631,32 +640,34 @@ error:
static int32_t AcmAllocReadRequests(struct AcmDevice *acm) static int32_t AcmAllocReadRequests(struct AcmDevice *acm)
{ {
int32_t ret; int32_t ret;
struct UsbRequestParams readParams; struct UsbRequestParams readParmas = {};
for (int32_t i = 0; i < ACM_NR; i++) { for (int32_t i = 0; i < ACM_NR; i++) {
// 分配待发送的readReq IO Request对象 // 分配待发送的readReq IO Request对象
acm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize); acm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize);
if (!acm->readReq[i]) { if (!acm->readReq[i]) {
goto error; HDF_LOGE("readReq request failed\n");
goto ERROR;
} }
readParams.userData = (void *)acm; readParmas.userData = (void *)acm;
readParams.pipeAddress = acm->dataInPipe->pipeAddress; readParmas.pipeAddress = acm->dataInPipe->pipeAddress;
readParams.pipeId = acm->dataInPipe->pipeId; readParmas.pipeId = acm->dataInPipe->pipeId;
readParams.interfaceId = acm->dataInPipe->interfaceId; readParmas.interfaceId = acm->dataInPipe->interfaceId;
readParams.callback = AcmReadBulk; readParmas.callback = AcmReadBulk;
readParams.requestType = USB_REQUEST_PARAMS_DATA_TYPE; /* Data type */ readParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; /* Data type */
readParams.timeout = USB_CTRL_SET_TIMEOUT; readParmas.timeout = USB_CTRL_SET_TIMEOUT;
readParams.dataReq.numIsoPackets = 0; readParmas.dataReq.numIsoPackets = 0;
readParams.dataReq.direction = (acm->dataInPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & 0x1; readParmas.dataReq.directon = (((uint8_t)acm->dataInPipe->pipeDirection) >> USB_PIPE_DIR_OFFSET) & 0x1;
readParams.dataReq.length = acm->readSize; readParmas.dataReq.length = (int)acm->readSize;
// 根据readParams填充预先分配待发送的readReq IO Request对象 // 根据readParams填充预先分配待发送的readReq IO Request对象
ret = UsbFillRequest(acm->readReq[i], InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParams); ret = UsbFillRequest(acm->readReq[i], InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParmas);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
goto error; HDF_LOGE("%s: UsbFillRequest failed, ret=%d \n", __func__, ret);
goto ERROR;
} }
} }
return HDF_SUCCESS; return HDF_SUCCESS;
error: ERROR:
AcmFreeReadRequests(acm); AcmFreeReadRequests(acm);
return HDF_ERR_MALLOC_FAIL; return HDF_ERR_MALLOC_FAIL;
} }
...@@ -664,37 +675,39 @@ error: ...@@ -664,37 +675,39 @@ error:
static int32_t AcmAllocNotifyRequest(struct AcmDevice *acm) static int32_t AcmAllocNotifyRequest(struct AcmDevice *acm)
{ {
int32_t ret; int32_t ret;
struct UsbRequestParams intParams = {}; struct UsbRequestParams intParmas = {};
// 分配待发送的中断IO Request对象 // 分配待发送的中断IO Request对象
acm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(acm, acm->intPipe->interfaceId), 0, acm->intSize); acm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(acm, acm->intPipe->interfaceId), 0, acm->intSize);
if (!acm->notifyReq) { if (!acm->notifyReq) {
HDF_LOGE("notifyReq request failed.\n");
return HDF_ERR_MALLOC_FAIL; return HDF_ERR_MALLOC_FAIL;
} }
intParams.userData = (void *)acm; intParmas.userData = (void *)acm;
intParams.pipeAddress = acm->intPipe->pipeAddress; intParmas.pipeAddress = acm->intPipe->pipeAddress;
intParams.pipeId = acm->intPipe->pipeId; intParmas.pipeId = acm->intPipe->pipeId;
intParams.interfaceId = acm->intPipe->interfaceId; intParmas.interfaceId = acm->intPipe->interfaceId;
intParams.callback = AcmCtrlIrq; intParmas.callback = AcmCtrlIrq;
intParams.requestType = USB_REQUEST_PARAMS_DATA_TYPE; intParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
intParams.timeout = USB_CTRL_SET_TIMEOUT; intParmas.timeout = USB_CTRL_SET_TIMEOUT;
intParams.dataReq.numIsoPackets = 0; intParmas.dataReq.numIsoPackets = 0;
intParams.dataReq.direction = (acm->intPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & DIRECTION_MASK; intParmas.dataReq.directon = (((uint8_t)acm->intPipe->pipeDirection) >> USB_PIPE_DIR_OFFSET) & DIRECTION_MASK;
intParams.dataReq.length = acm->intSize; intParmas.dataReq.length = (int)acm->intSize;
// 填充预先分配的中断IO Request // 填充预先分配的中断IO Request
ret = UsbFillRequest(acm->notifyReq, InterfaceIdToHandle(acm, acm->intPipe->interfaceId), &intParams); ret = UsbFillRequest(acm->notifyReq, InterfaceIdToHandle(acm, acm->intPipe->interfaceId), &intParmas);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
goto error; HDF_LOGE("%s: UsbFillRequest failed, ret = %d", __func__, ret);
goto ERROR;
} }
return HDF_SUCCESS; return HDF_SUCCESS;
error: ERROR:
AcmFreeNotifyRequest(acm); AcmFreeNotifyReqeust(acm);
return ret; return ret;
} }
static void AcmReleaseInterfaces(struct AcmDevice *acm) static void AcmReleaseInterfaces(struct AcmDevice *acm)
{ {
for (int32_t i = 0; i < acm->interfaceCnt; i++) { for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
if (acm->iface[i]) { if (acm->iface[i]) {
// 释放一个USB接口对象 // 释放一个USB接口对象
UsbReleaseInterface(acm->iface[i]); UsbReleaseInterface(acm->iface[i]);
...@@ -709,23 +722,24 @@ static void AcmReleaseInterfaces(struct AcmDevice *acm) ...@@ -709,23 +722,24 @@ static void AcmReleaseInterfaces(struct AcmDevice *acm)
static int32_t AcmClaimInterfaces(struct AcmDevice *acm) static int32_t AcmClaimInterfaces(struct AcmDevice *acm)
{ {
for (int32_t i = 0; i < acm->interfaceCnt; i++) { for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
// 获取UsbInterface接口对象 // 获取UsbInterface接口对象
acm->iface[i] = GetUsbInterfaceById((const struct AcmDevice *)acm, acm->interfaceIndex[i]); acm->iface[i] = GetUsbInterfaceById((const struct AcmDevice *)acm, acm->interfaceIndex[i]);
if (acm->iface[i] == NULL) { if (acm->iface[i] == NULL) {
goto error; HDF_LOGE("%s: interface%d is null", __func__, acm->interfaceIndex[i]);
goto ERROR;
} }
} }
// 获取控制接口对应的UsbInterface接口对象
// 获取控制接口对应的UsbInterface接口对象
acm->ctrIface = GetUsbInterfaceById((const struct AcmDevice *)acm, USB_CTRL_INTERFACE_ID); acm->ctrIface = GetUsbInterfaceById((const struct AcmDevice *)acm, USB_CTRL_INTERFACE_ID);
if (acm->ctrIface == NULL) { if (acm->ctrIface == NULL) {
goto error; HDF_LOGE("%s: GetUsbInterfaceById null", __func__);
goto ERROR;
} }
return HDF_SUCCESS; return HDF_SUCCESS;
error: ERROR:
// 根据acm->interfaceCnt循环释放接口对象 // 根据acm->interfaceCnt循环释放接口对象
AcmReleaseInterfaces(acm); AcmReleaseInterfaces(acm);
return HDF_FAILURE; return HDF_FAILURE;
...@@ -733,7 +747,7 @@ static int32_t AcmClaimInterfaces(struct AcmDevice *acm) ...@@ -733,7 +747,7 @@ static int32_t AcmClaimInterfaces(struct AcmDevice *acm)
static void AcmCloseInterfaces(struct AcmDevice *acm) static void AcmCloseInterfaces(struct AcmDevice *acm)
{ {
for (int32_t i = 0; i < acm->interfaceCnt; i++) { for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
if (acm->devHandle[i]) { if (acm->devHandle[i]) {
// 关闭一个USB设备对象 // 关闭一个USB设备对象
UsbCloseInterface(acm->devHandle[i]); UsbCloseInterface(acm->devHandle[i]);
...@@ -748,23 +762,25 @@ static void AcmCloseInterfaces(struct AcmDevice *acm) ...@@ -748,23 +762,25 @@ static void AcmCloseInterfaces(struct AcmDevice *acm)
static int32_t AcmOpenInterfaces(struct AcmDevice *acm) static int32_t AcmOpenInterfaces(struct AcmDevice *acm)
{ {
for (int32_t i = 0; i < acm->interfaceCnt; i++) { for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
if (acm->iface[i]) { if (acm->iface[i]) {
// 打开获取到的UsbInterface接口对象 // 打开获取到的UsbInterface接口对象
acm->devHandle[i] = UsbOpenInterface(acm->iface[i]); acm->devHandle[i] = UsbOpenInterface(acm->iface[i]);
if (acm->devHandle[i] == NULL) { if (acm->devHandle[i] == NULL) {
goto error; HDF_LOGE("%s: UsbOpenInterface null", __func__);
goto ERROR;
} }
} }
} }
acm->ctrDevHandle = UsbOpenInterface(acm->ctrIface); acm->ctrDevHandle = UsbOpenInterface(acm->ctrIface);
if (acm->ctrDevHandle == NULL) { if (acm->ctrDevHandle == NULL) {
goto error; HDF_LOGE("%s: ctrDevHandle UsbOpenInterface null", __func__);
goto ERROR;
} }
return HDF_SUCCESS; return HDF_SUCCESS;
error: ERROR:
// 关闭所有UsbInterface接口对象 // 关闭所有UsbInterface接口对象
AcmCloseInterfaces(acm); AcmCloseInterfaces(acm);
return HDF_FAILURE; return HDF_FAILURE;
...@@ -775,34 +791,36 @@ static int32_t AcmGetPipes(struct AcmDevice *acm) ...@@ -775,34 +791,36 @@ static int32_t AcmGetPipes(struct AcmDevice *acm)
// 获取dataInPipe的pipeInfo信息 // 获取dataInPipe的pipeInfo信息
acm->dataInPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN); acm->dataInPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN);
if (acm->dataInPipe == NULL) { if (acm->dataInPipe == NULL) {
goto error; HDF_LOGE("dataInPipe is null");
goto ERROR;
} }
// 获取dataOutPipe的pipeInfo信息
// 获取dataOutPipe的pipeInfo信息
acm->dataOutPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_OUT); acm->dataOutPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_OUT);
if (acm->dataOutPipe == NULL) { if (acm->dataOutPipe == NULL) {
goto error; HDF_LOGE("dataOutPipe is null");
goto ERROR;
} }
// 获取控制pipe的pipeInfo信息
// 获取控制pipe的pipeInfo信息
acm->ctrPipe = EnumePipe(acm, acm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT); acm->ctrPipe = EnumePipe(acm, acm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT);
if (acm->ctrPipe == NULL) { if (acm->ctrPipe == NULL) {
goto error; HDF_LOGE("ctrPipe is null");
goto ERROR;
} }
// 获取中断pipe的pipeInfo信息 // 获取中断pipe的pipeInfo信息
acm->intPipe = GetPipe(acm, USB_PIPE_TYPE_INTERRUPT, USB_PIPE_DIRECTION_IN); acm->intPipe = GetPipe(acm, USB_PIPE_TYPE_INTERRUPT, USB_PIPE_DIRECTION_IN);
if (acm->intPipe == NULL) { if (acm->intPipe == NULL) {
goto error; HDF_LOGE("intPipe is null");
goto ERROR;
} }
acm->readSize = acm->dataInPipe->maxPacketSize; acm->readSize = acm->dataInPipe->maxPacketSize;
acm->writeSize = acm->dataOutPipe->maxPacketSize; acm->writeSize = acm->dataOutPipe->maxPacketSize;
acm->ctrlSize = acm->ctrPipe->maxPacketSize; acm->ctrlSize = acm->ctrPipe->maxPacketSize;
acm->intSize = acm->intPipe->maxPacketSize; acm->intSize = acm->intPipe->maxPacketSize;
return HDF_SUCCESS; return HDF_SUCCESS;
error: ERROR:
// 释放设备中所有的管道信息 // 释放设备中所有的管道信息
AcmFreePipes(acm); AcmFreePipes(acm);
return HDF_FAILURE; return HDF_FAILURE;
...@@ -815,105 +833,112 @@ static void AcmFreeRequests(struct AcmDevice *acm) ...@@ -815,105 +833,112 @@ static void AcmFreeRequests(struct AcmDevice *acm)
g_syncRequest = NULL; g_syncRequest = NULL;
} }
AcmFreeReadRequests(acm); AcmFreeReadRequests(acm);
AcmFreeNotifyRequest(acm); AcmFreeNotifyReqeust(acm);
AcmFreeWriteRequests(acm); AcmFreeWriteRequests(acm);
AcmWriteBufFree(acm); AcmWriteBufFree(acm);
} }
static int32_t AcmAllocRequests(struct AcmDevice *acm) static int32_t AcmAllocRequests(const struct AcmDevice *acm)
{ {
int32_t ret; int32_t ret;
if (AcmWriteBufAlloc(acm) < 0) { if (AcmWriteBufAlloc(acm) < 0) {
HDF_LOGE("%s: AcmWriteBufAlloc failed", __func__);
return HDF_ERR_MALLOC_FAIL; return HDF_ERR_MALLOC_FAIL;
} }
for (int32_t i = 0; i < ACM_NW; i++) { for (int32_t i = 0; i < ACM_NW; i++) {
struct AcmWb *snd = &(acm->wb[i]); struct AcmWb *snd = (struct AcmWb *)&(acm->wb[i]);
// 分配待发送的IO Request对象 // 分配待发送的IO Request对象
snd->request = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataOutPipe->interfaceId), 0, acm->writeSize); snd->request = UsbAllocRequest(
snd->instance = acm; InterfaceIdToHandle((struct AcmDevice *)acm, acm->dataOutPipe->interfaceId), 0, acm->writeSize);
snd->instance = (struct AcmDevice *)acm;
if (snd->request == NULL) { if (snd->request == NULL) {
goto error_alloc_write_req; HDF_LOGE("%s:%d snd request fail", __func__, __LINE__);
goto ERROR_ALLOC_WRITE_REQ;
} }
} }
ret = AcmAllocNotifyRequest(acm); // 分配并填充中断IO Request对象 ret = AcmAllocNotifyRequest((struct AcmDevice *)acm); // 分配并填充中断IO Request对象
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
goto error_alloc_int_req; HDF_LOGE("%s:%d AcmAllocNotifyRequest fail", __func__, __LINE__);
goto ERROR_ALLOC_INT_REQ;
} }
ret = AcmAllocReadRequests(acm); // 分配并填充readReq IO Request对象 ret = AcmAllocReadRequests((struct AcmDevice *)acm); // 分配并填充readReq IO Request对象
if (ret) { if (ret) {
goto error_alloc_read_req; HDF_LOGE("%s:%d AcmAllocReadRequests fail", __func__, __LINE__);
goto ERROR_ALLOC_READ_REQ;
} }
return HDF_SUCCESS; return HDF_SUCCESS;
error_alloc_read_req: ERROR_ALLOC_READ_REQ:
AcmFreeNotifyRequest(acm); AcmFreeNotifyReqeust((struct AcmDevice *)acm);
error_alloc_int_req: ERROR_ALLOC_INT_REQ:
AcmFreeWriteRequests(acm); AcmFreeWriteRequests((struct AcmDevice *)acm);
error_alloc_write_req: ERROR_ALLOC_WRITE_REQ:
AcmWriteBufFree(acm); AcmWriteBufFree((struct AcmDevice *)acm);
return HDF_FAILURE; return HDF_FAILURE;
} }
static int32_t AcmInit(struct AcmDevice *acm) static int32_t AcmInit(struct AcmDevice *acm)
{ {
int32_t ret; int32_t ret;
struct UsbSession *session = NULL;
if (acm->initFlag == true) { if (acm->initFlag) {
HDF_LOGE("%{public}s: initFlag is true", __func__);
return HDF_SUCCESS; return HDF_SUCCESS;
} }
// 初始化Host DDK
// 初始化Host DDK
ret = UsbInitHostSdk(NULL); ret = UsbInitHostSdk(NULL);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
HDF_LOGE("%{public}s: UsbInitHostSdk failed", __func__);
return HDF_ERR_IO; return HDF_ERR_IO;
} }
acm->session = session; acm->session = NULL;
// 根据acm->interfaceIndex[i]分别获取UsbInterface接口对象
// 根据acm->interfaceIndex[i]分别获取UsbInterface接口对象
ret = AcmClaimInterfaces(acm); ret = AcmClaimInterfaces(acm);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
goto error_claim_interfaces; HDF_LOGE("%{public}s: AcmClaimInterfaces failed", __func__);
goto ERROR_CLAIM_INTERFACES;
} }
// 根据acm->iface[i]分别打开UsbInterface接口对象 // 根据acm->iface[i]分别打开UsbInterface接口对象
ret = AcmOpenInterfaces(acm); ret = AcmOpenInterfaces(acm);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
goto error_open_interfaces; HDF_LOGE("%{public}s: AcmOpenInterfaces failed", __func__);
goto ERROR_OPEN_INTERFACES;
} }
// 获取管道信息的指针 // 获取管道信息的指针
ret = AcmGetPipes(acm); ret = AcmGetPipes(acm);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
goto error_get_pipes; HDF_LOGE("%{public}s: AcmGetPipes failed", __func__);
goto ERROR_GET_PIPES;
} }
ret = AcmAllocRequests(acm); ret = AcmAllocRequests(acm);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
goto error_alloc_reqs; HDF_LOGE("%{public}s: AcmAllocRequests failed", __func__);
goto ERROR_ALLOC_REQS;
} }
acm->lineCoding.dwDTERate = CpuToLe32(DATARATE); // 转换为小端数据 acm->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
acm->lineCoding.bCharFormat = CHARFORMAT; // 8 acm->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
acm->lineCoding.bParityType = USB_CDC_NO_PARITY; acm->lineCoding.bParityType = USB_CDC_NO_PARITY;
acm->lineCoding.bDataBits = USB_CDC_1_STOP_BITS; acm->lineCoding.bDataBits = DATA_BITS_LENGTH;
acm->initFlag = true; acm->initFlag = true;
return HDF_SUCCESS; return HDF_SUCCESS;
error_alloc_reqs: ERROR_ALLOC_REQS:
AcmFreePipes(acm); AcmFreePipes(acm);
error_get_pipes: ERROR_GET_PIPES:
// 关闭所有UsbInterface接口对象 // 关闭所有UsbInterface接口对象
AcmCloseInterfaces(acm); AcmCloseInterfaces(acm);
error_open_interfaces: ERROR_OPEN_INTERFACES:
// 释放所有UsbInterface接口对象 // 释放所有UsbInterface接口对象
AcmReleaseInterfaces(acm); AcmReleaseInterfaces(acm);
error_claim_interfaces: ERROR_CLAIM_INTERFACES:
// 在主机端退出USB DDK,acm->session代表指向会话上下文的指针 // 在主机端退出USB DDK,acm->session代表指向会话上下文的指针
UsbExitHostSdk(acm->session); UsbExitHostSdk(acm->session);
acm->session = NULL; acm->session = NULL;
...@@ -922,17 +947,19 @@ error_claim_interfaces: ...@@ -922,17 +947,19 @@ error_claim_interfaces:
static void AcmRelease(struct AcmDevice *acm) static void AcmRelease(struct AcmDevice *acm)
{ {
if (acm->initFlag == false) { if (!(acm->initFlag)) {
HDF_LOGE("%s:%d: initFlag is false", __func__, __LINE__);
return; return;
} }
AcmFreeRequests(acm);
AcmFreePipes(acm);
AcmCloseInterfaces(acm); AcmCloseInterfaces(acm);
AcmReleaseInterfaces(acm); AcmReleaseInterfaces(acm);
AcmFreeRequests(acm);
AcmFreePipes(acm);
// 在主机端退出USB DDK // 在主机端退出USB DDK
UsbExitHostSdk(acm->session); UsbExitHostSdk(acm->session);
acm->session = NULL; acm->session = NULL;
acm->initFlag = false; acm->initFlag = false;
} }
...@@ -942,14 +969,17 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device) ...@@ -942,14 +969,17 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
struct AcmDevice *acm = NULL; struct AcmDevice *acm = NULL;
if (device == NULL) { if (device == NULL) {
HDF_LOGE("%s: device is null", __func__);
return HDF_ERR_INVALID_OBJECT; return HDF_ERR_INVALID_OBJECT;
} }
acm = (struct AcmDevice *)device->service; acm = (struct AcmDevice *)device->service;
// 初始化互斥锁,&acm->readLock表示指向互斥量的指针 // 初始化互斥锁,&acm->readLock表示指向互斥量的指针
if (acm == NULL) {
return HDF_ERR_INVALID_OBJECT;
}
OsalMutexInit(&acm->readLock); OsalMutexInit(&acm->readLock);
OsalMutexInit(&acm->writeLock); OsalMutexInit(&acm->writeLock);
HDF_LOGD("%s:%d busNum=%d,devAddr=%d", __func__, __LINE__, acm->busNum, acm->devAddr); HDF_LOGD("%s:%d busNum = %d,devAddr = %d", __func__, __LINE__, acm->busNum, acm->devAddr);
// 给USB串口设备信息开辟空间并赋值 // 给USB串口设备信息开辟空间并赋值
ret = UsbSerialDeviceAlloc(acm); ret = UsbSerialDeviceAlloc(acm);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
...@@ -958,6 +988,9 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device) ...@@ -958,6 +988,9 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
acm->initFlag = false; acm->initFlag = false;
g_acmReleaseFlag = false; g_acmReleaseFlag = false;
HDF_LOGD("%s:%d init ok!", __func__, __LINE__);
return ret; return ret;
} }
...@@ -966,16 +999,19 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) ...@@ -966,16 +999,19 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
struct AcmDevice *acm = NULL; struct AcmDevice *acm = NULL;
if (device == NULL) { if (device == NULL) {
HDF_LOGE("%s: device is null", __func__);
return; return;
} }
acm = (struct AcmDevice *)device->service; acm = (struct AcmDevice *)device->service;
if (acm == NULL) { if (acm == NULL) {
HDF_LOGE("%s: acm is null", __func__);
return; return;
} }
g_acmReleaseFlag = true; g_acmReleaseFlag = true;
if (acm->initFlag == true) { if (acm->initFlag) {
HDF_LOGE("%s:%d AcmRelease", __func__, __LINE__);
AcmRelease(acm); AcmRelease(acm);
} }
// 释放usb串口设备信息 // 释放usb串口设备信息
...@@ -986,21 +1022,21 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) ...@@ -986,21 +1022,21 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
OsalMutexDestroy(&acm->lock); OsalMutexDestroy(&acm->lock);
OsalMemFree(acm); OsalMemFree(acm);
acm = NULL; acm = NULL;
HDF_LOGD("%s:%d exit", __func__, __LINE__);
} }
// 驱动的Bind、Init、及Release操作 // 驱动的Bind、Init、及Release操作
struct HdfDriverEntry g_usbSerialDriverEntry = { struct HdfDriverEntry g_usbSerialDriverEntry = {
.moduleVersion = 1, .moduleVersion = 1,
.moduleName = "usbhost_acm", // 驱动模块名称,必须与hcs文件中配置的名称一致 .moduleName = "usbhost_acm", // 驱动模块名称,必须与hcs文件中配置的名称一致
.Bind = UsbSerialDriverBind, .Bind = UsbSerialDriverBind,
.Init = UsbSerialDriverInit, .Init = UsbSerialDriverInit,
.Release = UsbSerialDriverRelease, .Release = UsbSerialDriverRelease,
}; };
HDF_INIT(g_usbSerialDriverEntry); // 驱动入口 HDF_INIT(g_usbSerialDriverEntry); // 驱动入口
``` ```
#### Host RAW API驱动开发 #### Host RAW API驱动开发
```cpp ```cpp
root { root {
module = "usb_pnp_device"; module = "usb_pnp_device";
...@@ -1051,27 +1087,27 @@ root { ...@@ -1051,27 +1087,27 @@ root {
``` ```
```cpp ```cpp
#include "usb_serial_rawapi.h"
#include <unistd.h> #include <unistd.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "securec.h"
#include "hdf_base.h" #include "hdf_base.h"
#include "hdf_log.h" #include "hdf_log.h"
#include "hdf_usb_pnp_manage.h" #include "hdf_usb_pnp_manage.h"
#include "osal_mem.h"
#include "osal_time.h"
#include "securec.h"
#include "usb_serial_rawapi.h"
#define HDF_LOG_TAG USB_HOST_ACM_RAW_API // 日志中可查寻的标签 #define HDF_LOG_TAG USB_HOST_ACM_RAW_API
#define USB_CTRL_REQ_SIZE 64 #define USB_CTRL_REQ_SIZE 64
#define USB_IO_THREAD_STACK_SIZE 8192 #define USB_IO_THREAD_STACK_SIZE 8192
#define USB_RAW_IO_SLEEP_MS_TIME 100 #define USB_RAW_IO_SLEEP_MS_TIME 100
#define USB_RAW_IO_STOP_WAIT_MAX_TIME 3 #define USB_RAW_IO_STOP_WAIT_MAX_TIME 3
static struct UsbRawRequest *g_syncRequest = NULL; static struct UsbRawRequest *g_syncRequest = NULL;
static UsbRawIoProcessStatusType g_stopIoStatus = USB_RAW_IO_PROCESS_RUNNING; static UsbRawIoProcessStatusType g_stopIoStatus = USB_RAW_IO_PROCESS_RUNNING;
struct OsalMutex g_stopIoLock; struct OsalMutex g_stopIoLock;
static bool g_rawAcmReleaseFlag = false; static bool g_rawAcmReleaseFlag = false;
...... ...
static int32_t UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConfigDescriptor **config) static int32_t UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConfigDescriptor **config)
{ {
UsbRawDevice *dev = NULL; UsbRawDevice *dev = NULL;
...@@ -1079,30 +1115,33 @@ static int32_t UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConf ...@@ -1079,30 +1115,33 @@ static int32_t UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConf
int32_t ret; int32_t ret;
if (devHandle == NULL) { if (devHandle == NULL) {
HDF_LOGE("%s:%d devHandle is null", __func__, __LINE__);
return HDF_ERR_INVALID_PARAM; return HDF_ERR_INVALID_PARAM;
} }
// 获取主用设备配置 // 获取主用设备配置
ret = UsbRawGetConfiguration(devHandle, &activeConfig); ret = UsbRawGetConfiguration(devHandle, &activeConfig);
if (ret != HDF_SUCCESS) { if (ret) {
HDF_LOGE("%s:%d UsbRawGetConfiguration failed, ret = %d", __func__, __LINE__, ret);
return HDF_FAILURE; return HDF_FAILURE;
} }
HDF_LOGE("%s:%d activeConfig = %d", __func__, __LINE__, activeConfig);
// 根据指定的设备句柄获取设备指针 // 根据指定的设备句柄获取设备指针
dev = UsbRawGetDevice(devHandle); dev = UsbRawGetDevice(devHandle);
if (dev == NULL) { if (dev == NULL) {
HDF_LOGE("%s:%d UsbRawGetDevice failed", __func__, __LINE__);
return HDF_FAILURE; return HDF_FAILURE;
} }
// 根据指定的设备ID获取设备配置描述符 // 根据指定的设备ID获取设备配置描述符
ret = UsbRawGetConfigDescriptor(dev, activeConfig, config); ret = UsbRawGetConfigDescriptor(dev, activeConfig, config);
if (ret != HDF_SUCCESS) { if (ret) {
HDF_LOGE("UsbRawGetConfigDescriptor failed, ret=%d\n", ret); HDF_LOGE("UsbRawGetConfigDescriptor failed, ret = %d\n", ret);
return HDF_FAILURE;
} }
return ret;
return HDF_SUCCESS;
} }
... ...
static int32_t UsbAllocWriteRequests(struct AcmDevice *acm) static int32_t UsbAllocWriteRequests(struct AcmDevice *acm)
{ {
int32_t i; int32_t i;
...@@ -1112,6 +1151,7 @@ static int32_t UsbAllocWriteRequests(struct AcmDevice *acm) ...@@ -1112,6 +1151,7 @@ static int32_t UsbAllocWriteRequests(struct AcmDevice *acm)
snd->request = UsbRawAllocRequest(acm->devHandle, 0, acm->dataOutEp->maxPacketSize); snd->request = UsbRawAllocRequest(acm->devHandle, 0, acm->dataOutEp->maxPacketSize);
snd->instance = acm; snd->instance = acm;
if (snd->request == NULL) { if (snd->request == NULL) {
HDF_LOGE("%s: UsbRawAllocRequest failed", __func__);
return HDF_ERR_MALLOC_FAIL; return HDF_ERR_MALLOC_FAIL;
} }
} }
...@@ -1127,41 +1167,47 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device) ...@@ -1127,41 +1167,47 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
errno_t err; errno_t err;
if (device == NULL) { if (device == NULL) {
HDF_LOGE("%s: device is null", __func__);
return HDF_ERR_INVALID_OBJECT; return HDF_ERR_INVALID_OBJECT;
} }
acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*acm)); acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*acm));
if (acm == NULL) { if (acm == NULL) {
HDF_LOGE("%s: Alloc usb serial device failed", __func__);
return HDF_FAILURE; return HDF_FAILURE;
} }
if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) { if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) {
goto error; HDF_LOGE("%s:%d OsalMutexInit fail", __func__, __LINE__);
goto ERROR;
} }
info = (struct UsbPnpNotifyServiceInfo *)device->priv; info = (struct UsbPnpNotifyServiceInfo *)device->priv;
if (info != NULL) { if (info != NULL) {
acm->busNum = info->busNum; acm->busNum = (uint8_t)info->busNum;
acm->devAddr = info->devNum; acm->devAddr = (uint8_t)info->devNum;
acm->interfaceCnt = info->interfaceLength; acm->interfaceCnt = info->interfaceLength;
err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, (const void *)info->interfaceNumber,
(const void*)info->interfaceNumber, info->interfaceLength); info->interfaceLength);
if (err != EOK) { if (err != EOK) {
goto lock_error; HDF_LOGE("%s:%d memcpy_s failed err=%d", __func__, __LINE__, err);
goto LOCK_ERROR;
} }
} else { } else {
goto lock_error; HDF_LOGE("%s:%d info is NULL!", __func__, __LINE__);
goto LOCK_ERROR;
} }
device->service = &(acm->service); device->service = &(acm->service);
device->service->Dispatch = UsbSerialDeviceDispatch; device->service->Dispatch = UsbSerialDeviceDispatch;
acm->device = device; acm->device = device;
HDF_LOGD("UsbSerialDriverBind=========================OK");
return HDF_SUCCESS; return HDF_SUCCESS;
lock_error: LOCK_ERROR:
if (OsalMutexDestroy(&acm->lock)) { if (OsalMutexDestroy(&acm->lock)) {
HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__); HDF_LOGE("%s:%d OsalMutexDestroy fail", __func__, __LINE__);
} }
error: ERROR:
OsalMemFree(acm); OsalMemFree(acm);
acm = NULL; acm = NULL;
return HDF_FAILURE; return HDF_FAILURE;
...@@ -1170,26 +1216,26 @@ error: ...@@ -1170,26 +1216,26 @@ error:
static int32_t UsbAllocReadRequests(struct AcmDevice *acm) static int32_t UsbAllocReadRequests(struct AcmDevice *acm)
{ {
struct UsbRawFillRequestData reqData; struct UsbRawFillRequestData reqData;
int32_t size = acm->dataInEp->maxPacketSize; uint32_t size = acm->dataInEp->maxPacketSize;
int32_t ret;
for (int32_t i = 0; i < ACM_NR; i++) { for (int32_t i = 0; i < ACM_NR; i++) {
// 分配一个具有指定数目的同步传输分组描述符的传输请求 // 分配一个具有指定数目的同步传输分组描述符的传输请求
acm->readReq[i] = UsbRawAllocRequest(acm->devHandle, 0, size); acm->readReq[i] = UsbRawAllocRequest(acm->devHandle, 0, size);
if (!acm->readReq[i]) { if (!acm->readReq[i]) {
HDF_LOGE("readReq request failed\n");
return HDF_ERR_MALLOC_FAIL; return HDF_ERR_MALLOC_FAIL;
} }
reqData.endPoint = acm->dataInEp->addr; reqData.endPoint = acm->dataInEp->addr;
reqData.numIsoPackets = 0; reqData.numIsoPackets = 0;
reqData.callback = AcmReadBulkCallback; reqData.callback = AcmReadBulkCallback;
reqData.userData = (void *)acm; reqData.userData = (void *)acm;
reqData.timeout = USB_CTRL_SET_TIMEOUT; reqData.timeout = USB_CTRL_SET_TIMEOUT;
reqData.length = size; reqData.length = size;
// 在批量传输请求中填写所需信息 // 在批量传输请求中填写所需信息
ret = UsbRawFillBulkRequest(acm->readReq[i], acm->devHandle, &reqData); int32_t ret = UsbRawFillBulkRequest(acm->readReq[i], acm->devHandle, &reqData);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: FillBulkRequest failed, ret=%d\n", __func__, ret);
return HDF_FAILURE; return HDF_FAILURE;
} }
} }
...@@ -1200,12 +1246,12 @@ static int32_t UsbAllocReadRequests(struct AcmDevice *acm) ...@@ -1200,12 +1246,12 @@ static int32_t UsbAllocReadRequests(struct AcmDevice *acm)
static int32_t UsbAllocNotifyRequest(struct AcmDevice *acm) static int32_t UsbAllocNotifyRequest(struct AcmDevice *acm)
{ {
struct UsbRawFillRequestData fillRequestData; struct UsbRawFillRequestData fillRequestData;
int32_t size = acm->notifyEp->maxPacketSize; uint32_t size = acm->notifyEp->maxPacketSize;
int32_t ret; int32_t ret;
// 分配一个具有指定数目的同步传输分组描述符的传输请求 // 分配一个具有指定数目的同步传输分组描述符的传输请求
acm->notifyReq = UsbRawAllocRequest(acm->devHandle, 0, size); acm->notifyReq = UsbRawAllocRequest(acm->devHandle, 0, size);
if (!acm->notifyReq) { if (!acm->notifyReq) {
HDF_LOGE("notifyReq request fail\n");
return HDF_ERR_MALLOC_FAIL; return HDF_ERR_MALLOC_FAIL;
} }
...@@ -1215,10 +1261,10 @@ static int32_t UsbAllocNotifyRequest(struct AcmDevice *acm) ...@@ -1215,10 +1261,10 @@ static int32_t UsbAllocNotifyRequest(struct AcmDevice *acm)
fillRequestData.callback = AcmNotifyReqCallback; fillRequestData.callback = AcmNotifyReqCallback;
fillRequestData.userData = (void *)acm; fillRequestData.userData = (void *)acm;
fillRequestData.timeout = USB_CTRL_SET_TIMEOUT; fillRequestData.timeout = USB_CTRL_SET_TIMEOUT;
// 在中断传输请求中填充所需的信息 // 在中断传输请求中填充所需的信息
ret = UsbRawFillInterruptRequest(acm->notifyReq, acm->devHandle, &fillRequestData); ret = UsbRawFillInterruptRequest(acm->notifyReq, acm->devHandle, &fillRequestData);
if (ret != HDF_SUCCESS) { if (ret) {
HDF_LOGE("%s: FillInterruptRequest failed, ret=%d", __func__, ret);
return HDF_FAILURE; return HDF_FAILURE;
} }
...@@ -1231,98 +1277,111 @@ static int32_t UsbSerialInit(struct AcmDevice *acm) ...@@ -1231,98 +1277,111 @@ static int32_t UsbSerialInit(struct AcmDevice *acm)
UsbRawHandle *devHandle = NULL; UsbRawHandle *devHandle = NULL;
int32_t ret; int32_t ret;
if (acm->initFlag == true) { if (acm->initFlag) {
HDF_LOGE("%s:%d: initFlag is true", __func__, __LINE__);
return HDF_SUCCESS; return HDF_SUCCESS;
} }
// 以专家模式初始化USB DDK // 以专家模式初始化USB DDK
ret = UsbRawInit(NULL); ret = UsbRawInit(NULL);
if (ret != HDF_SUCCESS) { if (ret) {
HDF_LOGE("%s:%d UsbRawInit failed", __func__, __LINE__);
return HDF_ERR_IO; return HDF_ERR_IO;
} }
acm->session = session; acm->session = session;
// 打开一个USB设备对象 // 打开一个USB设备对象
devHandle = UsbRawOpenDevice(session, acm->busNum, acm->devAddr); devHandle = UsbRawOpenDevice(session, acm->busNum, acm->devAddr);
if (devHandle == NULL) { if (devHandle == NULL) {
ret = HDF_FAILURE; HDF_LOGE("%s:%d UsbRawOpenDevice failed", __func__, __LINE__);
goto err_open_device; ret = HDF_FAILURE;
goto ERR_OPEN_DEVICE;
} }
acm->devHandle = devHandle; acm->devHandle = devHandle;
// 获取主用设备配置、设备指针及配置描述符 // 获取主用设备配置、设备指针及配置描述符
ret = UsbGetConfigDescriptor(devHandle, &acm->config); ret = UsbGetConfigDescriptor(devHandle, &acm->config);
if (ret != HDF_SUCCESS) { if (ret) {
ret = HDF_FAILURE; HDF_LOGE("%s:%d UsbGetConfigDescriptor failed", __func__, __LINE__);
goto err_get_desc; ret = HDF_FAILURE;
goto ERR_GET_DESC;
} }
ret = UsbParseConfigDescriptor(acm, acm->config); ret = UsbParseConfigDescriptor(acm, acm->config);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
HDF_LOGE("%s:%d UsbParseConfigDescriptor failed", __func__, __LINE__);
ret = HDF_FAILURE; ret = HDF_FAILURE;
goto err_parse_desc; goto ERR_PARSE_DESC;
} }
ret = AcmWriteBufAlloc(acm); ret = AcmWriteBufAlloc(acm);
if (ret < 0) { if (ret < 0) {
HDF_LOGE("%s:%d AcmWriteBufAlloc failed", __func__, __LINE__);
ret = HDF_FAILURE; ret = HDF_FAILURE;
goto err_alloc_write_buf; goto ERR_ALLOC_WRITE_BUF;
} }
ret = UsbAllocWriteRequests(acm); ret = UsbAllocWriteRequests(acm);
if (ret < 0) { if (ret < 0) {
HDF_LOGE("%s:%d UsbAllocWriteRequests failed", __func__, __LINE__);
ret = HDF_FAILURE; ret = HDF_FAILURE;
goto err_alloc_write_reqs; goto ERR_ALLOC_WRITE_REQS;
} }
ret = UsbAllocNotifyRequest(acm); ret = UsbAllocNotifyRequest(acm);
if (ret) { if (ret) {
goto err_alloc_notify_req; HDF_LOGE("%s:%d UsbAllocNotifyRequests failed", __func__, __LINE__);
goto ERR_ALLOC_NOTIFY_REQ;
} }
ret = UsbAllocReadRequests(acm); ret = UsbAllocReadRequests(acm);
if (ret) { if (ret) {
goto err_alloc_read_reqs; HDF_LOGE("%s:%d UsbAllocReadRequests failed", __func__, __LINE__);
goto ERR_ALLOC_READ_REQS;
} }
ret = UsbStartIo(acm); ret = UsbStartIo(acm);
if (ret) { if (ret) {
goto err_start_io; HDF_LOGE("%s:%d UsbAllocReadRequests failed", __func__, __LINE__);
goto ERR_START_IO;
} }
acm->lineCoding.dwDTERate = CpuToLe32(DATARATE); acm->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
acm->lineCoding.bCharFormat = CHARFORMAT; acm->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
acm->lineCoding.bParityType = USB_CDC_NO_PARITY; acm->lineCoding.bParityType = USB_CDC_NO_PARITY;
acm->lineCoding.bDataBits = USB_CDC_1_STOP_BITS; acm->lineCoding.bDataBits = DATA_BITS_LENGTH;
ret = UsbRawSubmitRequest(acm->notifyReq); ret = UsbRawSubmitRequest(acm->notifyReq);
if (ret) { if (ret) {
goto err_submit_req; HDF_LOGE("%s:%d UsbRawSubmitRequest failed", __func__, __LINE__);
goto ERR_SUBMIT_REQ;
} }
acm->initFlag = true; acm->initFlag = true;
HDF_LOGD("%s:%d=========================OK", __func__, __LINE__);
return HDF_SUCCESS; return HDF_SUCCESS;
err_submit_req: ERR_SUBMIT_REQ:
UsbStopIo(acm); // 停止IO线程并释放所有资源 UsbStopIo(acm); // 停止IO线程并释放所有资源
err_start_io: ERR_START_IO:
UsbFreeReadRequests(acm); UsbFreeReadRequests(acm);
err_alloc_read_reqs: ERR_ALLOC_READ_REQS:
UsbFreeNotifyRequest(acm); UsbFreeNotifyReqeust(acm);
err_alloc_notify_req: ERR_ALLOC_NOTIFY_REQ:
UsbFreeWriteRequests(acm); UsbFreeWriteRequests(acm);
err_alloc_write_reqs: ERR_ALLOC_WRITE_REQS:
AcmWriteBufFree(acm); AcmWriteBufFree(acm);
err_alloc_write_buf: ERR_ALLOC_WRITE_BUF:
UsbReleaseInterfaces(acm); UsbReleaseInterfaces(acm);
err_parse_desc: ERR_PARSE_DESC:
UsbRawFreeConfigDescriptor(acm->config); UsbRawFreeConfigDescriptor(acm->config);
acm->config = NULL; acm->config = NULL;
err_get_desc: ERR_GET_DESC:
(void)UsbRawCloseDevice(devHandle); // 关闭USB设备对象 (void)UsbRawCloseDevice(devHandle); // 关闭USB设备对象
err_open_device: ERR_OPEN_DEVICE:
UsbRawExit(acm->session); // 退出USB DDK的专家模式 UsbRawExit(acm->session); // 退出USB DDK的专家模式
return ret; return ret;
} }
...
static void UsbSerialRelease(struct AcmDevice *acm) static void UsbSerialRelease(struct AcmDevice *acm)
{ {
if (acm->initFlag == false) { if (!(acm->initFlag)) {
HDF_LOGE("%s:%d: initFlag is false", __func__, __LINE__);
return; return;
} }
...@@ -1333,11 +1392,11 @@ static void UsbSerialRelease(struct AcmDevice *acm) ...@@ -1333,11 +1392,11 @@ static void UsbSerialRelease(struct AcmDevice *acm)
g_syncRequest = NULL; g_syncRequest = NULL;
} }
UsbFreeReadRequests(acm); UsbFreeReadRequests(acm);
UsbFreeNotifyRequest(acm); UsbFreeNotifyReqeust(acm);
UsbFreeWriteRequests(acm); UsbFreeWriteRequests(acm);
AcmWriteBufFree(acm); AcmWriteBufFree(acm);
(void)UsbRawCloseDevice(acm->devHandle);
UsbReleaseInterfaces(acm); UsbReleaseInterfaces(acm);
(void)UsbRawCloseDevice(acm->devHandle);
UsbRawFreeConfigDescriptor(acm->config); UsbRawFreeConfigDescriptor(acm->config);
acm->config = NULL; acm->config = NULL;
// 退出USB DDK的专家模式 // 退出USB DDK的专家模式
...@@ -1352,12 +1411,16 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device) ...@@ -1352,12 +1411,16 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
int32_t ret; int32_t ret;
if (device == NULL) { if (device == NULL) {
HDF_LOGE("%s:%d device is null", __func__, __LINE__);
return HDF_ERR_INVALID_OBJECT; return HDF_ERR_INVALID_OBJECT;
} }
acm = (struct AcmDevice *)device->service; acm = (struct AcmDevice *)device->service;
if (acm == NULL) {
return HDF_ERR_INVALID_OBJECT;
}
OsalMutexInit(&acm->readLock); OsalMutexInit(&acm->readLock);
OsalMutexInit(&acm->writeLock); OsalMutexInit(&acm->writeLock);
// 设备申请连续的内存
ret = UsbSerialDeviceAlloc(acm); ret = UsbSerialDeviceAlloc(acm);
if (ret != HDF_SUCCESS) { if (ret != HDF_SUCCESS) {
HDF_LOGE("%s:%d UsbSerialDeviceAlloc failed", __func__, __LINE__); HDF_LOGE("%s:%d UsbSerialDeviceAlloc failed", __func__, __LINE__);
...@@ -1365,6 +1428,7 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device) ...@@ -1365,6 +1428,7 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
acm->initFlag = false; acm->initFlag = false;
g_rawAcmReleaseFlag = false; g_rawAcmReleaseFlag = false;
HDF_LOGD("%s:%d init ok!", __func__, __LINE__);
return ret; return ret;
} }
...@@ -1372,17 +1436,20 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) ...@@ -1372,17 +1436,20 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
{ {
struct AcmDevice *acm = NULL; struct AcmDevice *acm = NULL;
if (device == NULL) { if (device == NULL) {
HDF_LOGE("%s: device is null", __func__);
return; return;
} }
acm = (struct AcmDevice *)device->service; acm = (struct AcmDevice *)device->service;
if (acm == NULL) { if (acm == NULL) {
HDF_LOGE("%s: acm is null", __func__);
return; return;
} }
g_rawAcmReleaseFlag = true; g_rawAcmReleaseFlag = true;
if (acm->initFlag == true) { if (acm->initFlag) {
HDF_LOGE("%s:%d UsbSerialRelease", __func__, __LINE__);
UsbSerialRelease(acm); UsbSerialRelease(acm);
} }
UsbSeriaDevicelFree(acm); UsbSeriaDevicelFree(acm);
...@@ -1391,14 +1458,15 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device) ...@@ -1391,14 +1458,15 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
OsalMutexDestroy(&acm->lock); OsalMutexDestroy(&acm->lock);
OsalMemFree(acm); OsalMemFree(acm);
acm = NULL; acm = NULL;
HDF_LOGD("%s:%d exit", __func__, __LINE__);
} }
struct HdfDriverEntry g_usbSerialRawDriverEntry = { struct HdfDriverEntry g_usbSerialRawDriverEntry = {
.moduleVersion = 1, .moduleVersion = 1,
.moduleName = "usbhost_acm_rawapi", // 驱动模块名称,必须与hcs文件中配置的名称一致 .moduleName = "usbhost_acm_rawapi", // 驱动模块名称,必须与hcs文件中配置的名称一致
.Bind = UsbSerialDriverBind, .Bind = UsbSerialDriverBind,
.Init = UsbSerialDriverInit, .Init = UsbSerialDriverInit,
.Release = UsbSerialDriverRelease, .Release = UsbSerialDriverRelease,
}; };
HDF_INIT(g_usbSerialRawDriverEntry); HDF_INIT(g_usbSerialRawDriverEntry);
``` ```
...@@ -1412,24 +1480,30 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc ...@@ -1412,24 +1480,30 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc
```cpp ```cpp
static int32_t AcmCreateFuncDevice(struct UsbAcmDevice *acm, struct DeviceResourceIface *iface) static int32_t AcmCreateFuncDevice(struct UsbAcmDevice *acm, struct DeviceResourceIface *iface)
{ {
int32_t ret;
struct UsbFnDevice *fnDev = NULL; struct UsbFnDevice *fnDev = NULL;
struct UsbFnDescriptorData descData; // 读取hcs文件中的udc_name节点的字符串值
uint8_t useHcs; if (iface->GetString(acm->device->property, "udc_name", (const char **)&acm->udcName, UDC_NAME) != HDF_SUCCESS) {
... HDF_LOGE("%s: read udc_name failed, use default", __func__);
if (useHcs == 0) { // 描述符来自于代码中编码 return HDF_FAILURE;
descData.type = USBFN_DESC_DATA_TYPE_DESC;
descData.descriptor = &g_masterFuncDevice;
} else { // 描述符来自于解析hcs文件
descData.type = USBFN_DESC_DATA_TYPE_PROP;
descData.property = device->property;
} }
/* 创建设备 */
fnDev = (struct UsbFnDevice *)UsbFnDeviceCreate(acm->udcName, &descData); fnDev = (struct UsbFnDevice *)UsbFnGetDevice(acm->udcName);
if (fnDev == NULL) { if (fnDev == NULL) {
HDF_LOGE("%s: create usb function device failed", __func__);
return HDF_FAILURE; return HDF_FAILURE;
} }
... // 解析acm每一个Iface
ret = AcmParseEachIface(acm, fnDev);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: get pipes failed", __func__);
return HDF_FAILURE;
}
acm->fnDev = fnDev;
return HDF_SUCCESS;
} }
``` ```
2. 获取接口,打开接口,获取Pipe信息 2. 获取接口,打开接口,获取Pipe信息
...@@ -1437,28 +1511,69 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc ...@@ -1437,28 +1511,69 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc
```cpp ```cpp
static int32_t AcmParseEachPipe(struct UsbAcmDevice *acm, struct UsbAcmInterface *iface) static int32_t AcmParseEachPipe(struct UsbAcmDevice *acm, struct UsbAcmInterface *iface)
{ {
... struct UsbFnInterface *fnIface = iface->fn;
for (i = 0; i < fnIface->info.numPipes; i++) { for (uint32_t i = 0; i < fnIface->info.numPipes; i++) {
struct UsbFnPipeInfo pipeInfo; struct UsbFnPipeInfo pipeInfo;
// pipeInfo清除缓存区内容
(void)memset_s(&pipeInfo, sizeof(pipeInfo), 0, sizeof(pipeInfo));
/* 获取pipe信息 */ /* 获取pipe信息 */
ret = UsbFnInterfaceGetPipeInfo(fnIface, i, &pipeInfo); int32_t ret = UsbFnGetInterfacePipeInfo(fnIface, i, &pipeInfo);
... if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: get pipe info error", __func__);
return ret;
}
// PIPE的中断和管脚
switch (pipeInfo.type) {
case USB_PIPE_TYPE_INTERRUPT:
acm->notifyPipe.id = pipeInfo.id;
acm->notifyPipe.maxPacketSize = pipeInfo.maxPacketSize;
acm->ctrlIface = *iface;
break;
case USB_PIPE_TYPE_BULK:
if (pipeInfo.dir == USB_PIPE_DIRECTION_IN) {
acm->dataInPipe.id = pipeInfo.id;
acm->dataInPipe.maxPacketSize = pipeInfo.maxPacketSize;
acm->dataIface = *iface;
} else {
acm->dataOutPipe.id = pipeInfo.id;
acm->dataOutPipe.maxPacketSize = pipeInfo.maxPacketSize;
}
break;
default:
HDF_LOGE("%s: pipe type %d don't support", __func__, pipeInfo.type);
break;
}
} }
return HDF_SUCCESS; return HDF_SUCCESS;
} }
/* 获取接口,打开接口获取handle */ /* 获取接口,打开接口获取handle */
static int32_t AcmParseEachIface(struct UsbAcmDevice *acm, struct UsbFnDevice *fnDev) static int32_t AcmParseEachIface(struct UsbAcmDevice *acm, struct UsbFnDevice *fnDev)
{ {
... struct UsbFnInterface *fnIface = NULL;
uint32_t i;
if (fnDev == NULL) {
return HDF_FAILURE;
}
for (i = 0; i < fnDev->numInterfaces; i++) { for (i = 0; i < fnDev->numInterfaces; i++) {
/* 获取接口 */
fnIface = (struct UsbFnInterface *)UsbFnGetInterface(fnDev, i); fnIface = (struct UsbFnInterface *)UsbFnGetInterface(fnDev, i);
... if (fnIface == NULL) {
/* 打开接口 */ HDF_LOGE("%s: get interface failed", __func__);
handle = UsbFnInterfaceOpen(fnIface); return HDF_FAILURE;
... }
if (fnIface->info.subclass == USB_DDK_CDC_SUBCLASS_ACM) {
(void)AcmParseAcmIface(acm, fnIface);
fnIface = (struct UsbFnInterface *)UsbFnGetInterface(fnDev, i + 1);
if (fnIface == NULL) {
HDF_LOGE("%s: get interface failed", __func__);
return HDF_FAILURE;
}
(void)AcmParseAcmIface(acm, fnIface);
return HDF_SUCCESS;
}
} }
return HDF_SUCCESS; return HDF_FAILURE;
} }
``` ```
...@@ -1467,30 +1582,77 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc ...@@ -1467,30 +1582,77 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc
```cpp ```cpp
static int32_t AcmAllocCtrlRequests(struct UsbAcmDevice *acm, int32_t num) static int32_t AcmAllocCtrlRequests(struct UsbAcmDevice *acm, int32_t num)
{ {
... struct DListHead *head = &acm->ctrlPool;
req = UsbFnCtrlRequestAlloc(acm->ctrlIface.handle, struct UsbFnRequest *req = NULL;
sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding)); struct CtrlInfo *ctrlInfo = NULL;
... int32_t i;
}
static int32_t AcmDriverInit(struct HdfDeviceObject *device) DListHeadInit(&acm->ctrlPool);
{ acm->ctrlReqNum = 0;
...
/* 开始接收Event */ for (i = 0; i < num; i++) {
ret = UsbFnInterfaceStartRecvEvent(acm->ctrlIface.fn, 0xff, UsbAcmEventCallback, acm); // 申请内存
... ctrlInfo = (struct CtrlInfo *)OsalMemCalloc(sizeof(*ctrlInfo));
if (ctrlInfo == NULL) {
HDF_LOGE("%s: Allocate ctrlInfo failed", __func__);
goto OUT;
}
ctrlInfo->acm = acm;
req = UsbFnAllocCtrlRequest(
acm->ctrlIface.handle, sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
if (req == NULL) {
goto OUT;
}
req->complete = AcmCtrlComplete;
req->context = ctrlInfo;
DListInsertTail(&req->list, head);
acm->ctrlReqNum++;
}
return HDF_SUCCESS;
OUT:
return DListIsEmpty(head) ? HDF_FAILURE : HDF_SUCCESS;
} }
``` ```
4. 进行USB通信(读写等) 4. 进行USB通信(读写等)
```cpp ```cpp
static int32_t AcmSendNotifyRequest(struct UsbAcmDevice *acm, uint8_t type, static int32_t AcmSendNotifyRequest(
uint16_t value, void *data, uint32_t length) struct UsbAcmDevice *acm, uint8_t type, uint16_t value, const void *data, uint32_t length)
{ {
... struct UsbFnRequest *req = acm->notifyReq;
/* 异步发送 */ struct UsbCdcNotification *notify = NULL;
ret = UsbFnRequestSubmitAsync(req); int32_t ret;
...
if (req == NULL || req->buf == NULL) {
HDF_LOGE("%s: req is null", __func__);
return HDF_FAILURE;
}
acm->notifyReq = NULL;
acm->pending = false;
req->length = sizeof(*notify) + length;
notify = (struct UsbCdcNotification *)req->buf;
notify->bmRequestType = USB_DDK_DIR_IN | USB_DDK_TYPE_CLASS | USB_DDK_RECIP_INTERFACE;
notify->bNotificationType = type;
notify->wValue = CPU_TO_LE16(value);
notify->wIndex = CPU_TO_LE16(acm->ctrlIface.fn->info.index);
notify->wLength = CPU_TO_LE16(length);
ret = memcpy_s((void *)(notify + 1), length, data, length);
if (ret != EOK) {
HDF_LOGE("%s: memcpy_s failed", __func__);
return HDF_FAILURE;
}
ret = UsbFnSubmitRequestAsync(req);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: send notify request failed", __func__);
acm->notifyReq = req;
}
return ret;
} }
``` ```
...@@ -1512,6 +1674,22 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc ...@@ -1512,6 +1674,22 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc
} }
return ret; return ret;
} }
static int32_t AcmReleaseFuncDevice(struct UsbAcmDevice *acm)
{
int32_t ret = HDF_SUCCESS;
if (acm->fnDev == NULL) {
HDF_LOGE("%s: fnDev is null", __func__);
return HDF_FAILURE;
}
//释放通知请求
AcmFreeCtrlRequests(acm);
AcmFreeNotifyRequest(acm);
/* 停止接收Event EP0控制传输 */
(void)UsbFnCloseInterface(acm->ctrlIface.handle);
(void)UsbFnCloseInterface(acm->dataIface.handle);
(void)UsbFnStopRecvInterfaceEvent(acm->ctrlIface.fn);
return ret;
}
``` ```
## 参考 ## 参考
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册