Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
82e000a2
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
82e000a2
编写于
6月 12, 2023
作者:
O
openharmony_ci
提交者:
Gitee
6月 12, 2023
浏览文件
操作
浏览文件
下载
差异文件
!19330 fix:Refresh USB Community Driver Document
Merge pull request !19330 from 王易朋/master
上级
d31322d7
b1259d65
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
524 addition
and
346 deletion
+524
-346
zh-cn/device-dev/driver/driver-peripherals-usb-des.md
zh-cn/device-dev/driver/driver-peripherals-usb-des.md
+524
-346
未找到文件。
zh-cn/device-dev/driver/driver-peripherals-usb-des.md
浏览文件 @
82e000a2
...
...
@@ -84,69 +84,69 @@ USB主机端驱动程序开发相关接口(普通模式)如下,具体接
**表1**
USB主机端驱动程序开发相关接口(普通模式)
| 接口名称 | 功能描述 |
| 接口名称 | 功能描述 |
| -------- | -------- |
| int32_t
UsbInitHostSdk(struct
UsbSession
\*\*session); | USB主机端驱动开发工具包初始化 |
|
const
struct
UsbInterface
\*UsbClaimInterface(const
<br/>
struct
UsbSession
\*session,
uint8_t
busNum,
uint8_t
<br/>
usbAddr,
uint8_t
interfaceIndex); | 获取USB接口对象 |
| UsbInterfaceHandle
\*UsbOpenInterface(const
struct
<br/>
UsbInterface
\*interfaceObj); | 打开USB对象接口 |
| int32_t
UsbGetPipeInfo(const
UsbInterfaceHandle
<br/>
\*interfaceHandle,
uint8_t
settingIndex,
uint8_t
pipeId,
<br/>
struct
UsbPipeInfo
\*pipeInfo); | 获取指定可选设置的管道信息 |
| struct
UsbRequest
\*UsbAllocRequest(const
<br/>
UsbInterfaceHandle
\*interfaceHandle,
int32_t
isoPackets
<br/>
,
int32_t
length); | 分配请求对象 |
| int32_t
UsbFillRequest(const
struct
UsbRequest
<br/>
\*request,
const
UsbInterfaceHandle
\*interfaceHandle,
<br/>
const
struct
UsbRequestParams
\*params); | 填充请求 |
| int32_t
UsbSubmitRequestSync(const
struct
UsbRequest
<br/>
\*request); | 发送同步请求 |
| int32_t
UsbInitHostSdk(struct
UsbSession
\*\*session); | USB主机端驱动开发工具包初始化 |
|
struct UsbInterface
*UsbClaimInterface(const struct<br/> UsbSession *
session, uint8_t busNum, uint8_t usbAddr,
<br/>
uint8_t interfaceIndex) | 获取USB接口对象 |
| UsbInterfaceHandle
\*UsbOpenInterface(const
struct
<br/>
UsbInterface
\*interfaceObj); | 打开USB对象接口 |
| int32_t
UsbGetPipeInfo(const
UsbInterfaceHandle
<br/>
\*interfaceHandle,
uint8_t
settingIndex,
uint8_t
pipeId,
<br/>
struct
UsbPipeInfo
\*pipeInfo); | 获取指定可选设置的管道信息 |
| struct
UsbRequest
\*UsbAllocRequest(const
<br/>
UsbInterfaceHandle
\*interfaceHandle,
int32_t
isoPackets
<br/>
,
int32_t
length); | 分配请求对象 |
| int32_t
UsbFillRequest(const
struct
UsbRequest
<br/>
\*request,
const
UsbInterfaceHandle
\*interfaceHandle,
<br/>
const
struct
UsbRequestParams
\*params); | 填充请求 |
| int32_t
UsbSubmitRequestSync(const
struct
UsbRequest
<br/>
\*request); | 发送同步请求 |
USB主机端驱动程序开发相关接口(专家模式)如下,具体接口定义
[
见源码
](
https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/host/usb_raw_api.h
)
。
**表2**
USB主机端驱动程序开发相关接口(专家模式)
| 接口名称 | 功能描述 |
| 接口名称 | 功能描述 |
| -------- | -------- |
| int32_t
UsbRawInit(struct
UsbSession
\*\*session); | USB驱动开发工具包专家模式初始化 |
| UsbRawHandle
\*UsbRawOpenDevice(const
struct
<br/>
UsbSession
\*session,
uint8_t
busNum,
uint8_t
<br/>
usbAddr); | 打开USB设备对象 |
| int32_t
UsbRawSendControlRequest(const
struct
<br/>
UsbRawRequest
\*request,
const
UsbRawHandle
<br/>
\*devHandle,
const
struct
UsbControlRequestData
<br/>
\*requestData); | 执行同步控制传输 |
| int32_t
UsbRawSendBulkRequest(const
struct
<br/>
UsbRawRequest
\*request,
const
UsbRawHandle
<br/>
\*devHandle,
const
struct
UsbRequestData
<br/>
\*requestData); | 执行同步批量传输 |
| int32_t
UsbRawSendInterruptRequest(const
struct
<br/>
UsbRawRequest
\*request,
const
UsbRawHandle
<br/>
\*devHandle,
const
struct
UsbRequestData
<br/>
\*requestData); | 执行同步中断传输 |
| int32_t
UsbRawGetConfigDescriptor(const
UsbRawDevice
<br/>
\*rawDev,
uint8_t
configIndex,
struct
<br/>
UsbRawConfigDescriptor
\*\*config); | 获取给定设备指定ID的设备配置描述符 |
| int32_t
UsbRawFillInterruptRequest(const
struct
UsbRawRequest
<br/>
\*request,
const
UsbRawHandle
\*devHandle,
const
struct
<br/>
UsbRawFillRequestData
\*fillData); | 填充中断传输请求所需信息 |
| int32_t
UsbRawFillIsoRequest(const
struct
UsbRawRequest
<br/>
\*request,
const
UsbRawHandle
\*devHandle,
const
struct
<br/>
UsbRawFillRequestData
\*fillData); | 填充同步传输(Isochronous
Transfers)请求所需信息 |
| int32_t
UsbRawSubmitRequest(const
struct
UsbRawRequest
<br/>
\*request); | 提交一个传输请求 |
| int32_t
UsbRawCancelRequest(const
struct
UsbRawRequest
<br/>
\*request); | 取消一个传输请求 |
| int32_t
UsbRawHandleRequests(const
UsbRawHandle
<br/>
\*devHandle); | 传输请求事件完成处理 |
| int32_t
UsbRawInit(struct
UsbSession
\*\*session); | USB驱动开发工具包专家模式初始化 |
| UsbRawHandle
\*UsbRawOpenDevice(const
struct
<br/>
UsbSession
\*session,
uint8_t
busNum,
uint8_t
<br/>
usbAddr); | 打开USB设备对象 |
| int32_t
UsbRawSendControlRequest(const
struct
<br/>
UsbRawRequest
\*request,
const
UsbRawHandle
<br/>
\*devHandle,
const
struct
UsbControlRequestData
<br/>
\*requestData); | 执行同步控制传输 |
| int32_t
UsbRawSendBulkRequest(const
struct
<br/>
UsbRawRequest
\*request,
const
UsbRawHandle
<br/>
\*devHandle,
const
struct
UsbRequestData
<br/>
\*requestData); | 执行同步批量传输 |
| int32_t
UsbRawSendInterruptRequest(const
struct
<br/>
UsbRawRequest
\*request,
const
UsbRawHandle
<br/>
\*devHandle,
const
struct
UsbRequestData
<br/>
\*requestData); | 执行同步中断传输 |
| int32_t
UsbRawGetConfigDescriptor(const
UsbRawDevice
<br/>
\*rawDev,
uint8_t
configIndex,
struct
<br/>
UsbRawConfigDescriptor
\*\*config); | 获取给定设备指定ID的设备配置描述符 |
| int32_t
UsbRawFillInterruptRequest(const
struct
UsbRawRequest
<br/>
\*request,
const
UsbRawHandle
\*devHandle,
const
struct
<br/>
UsbRawFillRequestData
\*fillData); | 填充中断传输请求所需信息 |
| int32_t
UsbRawFillIsoRequest(const
struct
UsbRawRequest
<br/>
\*request,
const
UsbRawHandle
\*devHandle,
const
struct
<br/>
UsbRawFillRequestData
\*fillData); | 填充同步传输(Isochronous
Transfers)请求所需信息 |
| int32_t
UsbRawSubmitRequest(const
struct
UsbRawRequest
<br/>
\*request); | 提交一个传输请求 |
| int32_t
UsbRawCancelRequest(const
struct
UsbRawRequest
<br/>
\*request); | 取消一个传输请求 |
| int32_t
UsbRawHandleRequests(const
UsbRawHandle
<br/>
\*devHandle); | 传输请求事件完成处理 |
USB设备端用于管理USB设备的相关接口如下,具体接口定义
[
见源码
](
https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_device.h
)
。
**表3**
USB设备端用于管理USB设备的相关接口
| 接口名称 | 功能描述 |
| 接口名称 | 功能描述 |
| -------- | -------- |
| const
struct
UsbFnDevice
\*UsbFnCreateDevice(const
<br/>
char
\*udcName,
const
struct
UsbFnDescriptorData
<br/>
\*descriptor); | 创建USB设备 |
| int32_t
UsbFnRemoveDevice(struct
UsbFnDevice
<br/>
\*fnDevice); | 删除USB设备 |
| const
struct
UsbFnDevice
\*UsbFnGetDevice(const
char
<br/>
\*udcName); | 获取USB设备 |
| const
struct
UsbFnDevice
\*UsbFnCreateDevice(const
<br/>
char
\*udcName,
const
struct
UsbFnDescriptorData
<br/>
\*descriptor); | 创建USB设备 |
| int32_t
UsbFnRemoveDevice(struct
UsbFnDevice
<br/>
\*fnDevice); | 删除USB设备 |
| const
struct
UsbFnDevice
\*UsbFnGetDevice(const
char
<br/>
\*udcName); | 获取USB设备 |
USB设备端用于USB接口定义的相关接口如下,具体接口定义
[
见源码
](
https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_interface.h
)
。
**表4**
USB设备端用于USB接口定义的相关接口
| 接口名称 | 功能描述 |
| 接口名称 | 功能描述 |
| -------- | -------- |
| int32_t
UsbFnStartRecvInterfaceEvent(struct
<br/>
UsbFnInterface
\*interface,
uint32_t
eventMask,
<br/>
UsbFnEventCallback
callback,
void
\*context); | 开始接受Event事件 |
| int32_t
UsbFnStopRecvInterfaceEvent(struct
<br/>
UsbFnInterface
\*interface); | 停止接受Event事件 |
| UsbFnInterfaceHandle
UsbFnOpenInterface(struct
UsbFnInterface
\*interface); | 打开一个接口 |
| int32_t
UsbFnCloseInterface(UsbFnInterfaceHandle
handle); | 关闭一个接口 |
| int32_t
UsbFnGetInterfacePipeInfo(struct
UsbFnInterface
<br/>
\*interface,
uint8_t
pipeId,
struct
UsbFnPipeInfo
\*info); | 获取管道信息 |
| int32_t
UsbFnSetInterfaceProp(const
struct
UsbFnInterface
<br/>
\*interface,
const
char
\*name,
const
char
\*value); | 设置自定义属性 |
| int32_t
UsbFnStartRecvInterfaceEvent(struct
<br/>
UsbFnInterface
\*interface,
uint32_t
eventMask,
<br/>
UsbFnEventCallback
callback,
void
\*context); | 开始接受Event事件 |
| int32_t
UsbFnStopRecvInterfaceEvent(struct
<br/>
UsbFnInterface
\*interface); | 停止接受Event事件 |
| UsbFnInterfaceHandle
UsbFnOpenInterface(struct
UsbFnInterface
\*interface); | 打开一个接口 |
| int32_t
UsbFnCloseInterface(UsbFnInterfaceHandle
handle); | 关闭一个接口 |
| int32_t
UsbFnGetInterfacePipeInfo(struct
UsbFnInterface
<br/>
\*interface,
uint8_t
pipeId,
struct
UsbFnPipeInfo
\*info); | 获取管道信息 |
| int32_t
UsbFnSetInterfaceProp(const
struct
UsbFnInterface
<br/>
\*interface,
const
char
\*name,
const
char
\*value); | 设置自定义属性 |
USB设备端用于管理USB数据请求的相关接口如下,具体接口定义
[
见源码
](
https://gitee.com/openharmony/drivers_peripheral/blob/master/usb/interfaces/ddk/device/usbfn_request.h
)
。
**表5**
USB设备端用于管理USB数据请求的相关接口
| 接口名称 | 功能描述 |
| 接口名称 | 功能描述 |
| -------- | -------- |
| struct
UsbFnRequest
<br/>
\*UsbFnAllocCtrlRequest(UsbFnInterfaceHandle
handle,
<br/>
uint32_t
len); | 申请一个控制请求 |
| struct
UsbFnRequest
\*UsbFnAllocRequest(UsbFnInterfaceHandle
handle,
<br/>
uint8_t
pipe,
uint32_t
len); | 申请一个数据请求 |
| int32_t
UsbFnFreeRequest(struct
UsbFnRequest
\*req); | 释放一个请求 |
| int32_t
UsbFnSubmitRequestAsync(struct
UsbFnRequest
<br/>
\*req); | 发送异步请求 |
| int32_t
UsbFnSubmitRequestSync(struct
UsbFnRequest
<br/>
\*req,
uint32_t
timeout); | 发送同步请求 |
| int32_t
UsbFnCancelRequest(struct
UsbFnRequest
\*req); | 取消请求 |
| struct
UsbFnRequest
<br/>
\*UsbFnAllocCtrlRequest(UsbFnInterfaceHandle
handle,
<br/>
uint32_t
len); | 申请一个控制请求 |
| struct
UsbFnRequest
\*UsbFnAllocRequest(UsbFnInterfaceHandle
handle,
<br/>
uint8_t
pipe,
uint32_t
len); | 申请一个数据请求 |
| int32_t
UsbFnFreeRequest(struct
UsbFnRequest
\*req); | 释放一个请求 |
| int32_t
UsbFnSubmitRequestAsync(struct
UsbFnRequest
<br/>
\*req); | 发送异步请求 |
| int32_t
UsbFnSubmitRequestSync(struct
UsbFnRequest
<br/>
\*req,
uint32_t
timeout); | 发送同步请求 |
| int32_t
UsbFnCancelRequest(struct
UsbFnRequest
\*req); | 取消请求 |
### 开发步骤
...
...
@@ -295,14 +295,13 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
1.
在设备功能代码中构造描述符。
```cpp
static struct UsbFnFunction g_acmFunction = { // 功能描述符
.enable = true,
.funcName = "f_generic.a",
.strings = g_acmStrings,
.fsDescriptors = g_acmFsFunction,
.hsDescriptors = g_acmHsFunction,
.ssDescriptors = g_acmSsFunction,
.sspDescriptors = NULL,
static struct UsbFnFunction g_acmFunction = {
.funcName = "f_generic.a", //功能名称
.strings = g_acmStrings, //字符串
.fsDescriptors = g_acmFsFunction, //初始化fs描述符
.hsDescriptors = g_acmHsFunction, //初始化hs描述符
.ssDescriptors = g_acmSsFunction, //初始化ss描述符
.sspDescriptors = nullptr, //ss描述符置空
};
struct UsbFnFunction *g_functions[] = {
#ifdef CDC_ECM
...
...
@@ -311,8 +310,7 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
#ifdef CDC_ACM
&g_acmFunction,
#endif
NULL
};
nullptr};
static struct UsbFnConfiguration g_masterConfig = { // 配置描述符
.configurationValue = 1,
.iConfiguration = USB_FUNC_CONFIG_IDX,
...
...
@@ -322,10 +320,10 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
};
static struct UsbFnConfiguration *g_configs[] = {
&g_masterConfig,
NULL
,
nullptr
,
};
static struct UsbDeviceDescriptor g_cdc
Master
DeviceDesc = { // 设备描述符
.bLength = sizeof(g_cdc
Master
DeviceDesc),
static struct UsbDeviceDescriptor g_cdc
UsbFn
DeviceDesc = { // 设备描述符
.bLength = sizeof(g_cdc
UsbFn
DeviceDesc),
.bDescriptorType = USB_DDK_DT_DEVICE,
.bcdUSB = CpuToLe16(BCD_USB),
.bDeviceClass = 0,
...
...
@@ -340,10 +338,10 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
.iSerialNumber = USB_FUNC_SERIAL_IDX,
.bNumConfigurations = 1,
};
st
atic struct UsbFnDeviceDesc g_masterFuncDevice = { //
描述符入口
.deviceDesc
= &g_cdcMaster
DeviceDesc,
st
ruct UsbFnDeviceDesc g_acmFnDevice = { //
描述符入口
.deviceDesc
= &g_cdcUsbFn
DeviceDesc,
.deviceStrings = g_devStrings,
.configs
= g_configs,
.configs = g_configs,
};
```
...
...
@@ -351,14 +349,14 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
```cpp
if (useHcs == 0) { // 使用代码编写的描述符
descData.type
= USBFN_DESC_DATA_TYPE_DESC;
descData.descriptor = &g_acmF
unc
Device;
descData.type = USBFN_DESC_DATA_TYPE_DESC;
descData.descriptor = &g_acmF
n
Device;
} else { // 使用hcs编写的描述符
de
scData.type
= USBFN_DESC_DATA_TYPE_PROP;
de
scData.property = acm->
device->property;
de
vMgr->descData.type
= USBFN_DESC_DATA_TYPE_PROP;
de
vMgr->descData.property =
device->property;
}
// 创建设备
fnDev = (struct UsbFnDevice *)
UsbFnCreateDevice(acm->udcName, &
descData);
fnDev = (struct UsbFnDevice *)
UsbFnCreateDevice(devMgr->udcName, &devMgr->
descData);
```
3.
设备创建后,使用UsbFnGetInterface函数获取UsbInterface接口对象,并通过UsbFnGetInterfacePipeInfo函数获取USB管道信息。
...
...
@@ -371,56 +369,53 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
// 获取Handle
handle = UsbFnOpenInterface(fnIface);
// 获取控制(EP0)Request
req = UsbFnAllocCtrlRequest(acm->ctrlIface.handle,
sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
req = UsbFnAllocCtrlRequest(acmDevice->ctrlIface.handle, sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
// 获取Request
req = UsbFnAllocCtrlRequest(acm->ctrlIface.handle,
sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
req = UsbFnAllocCtrlRequest(acmDevice->ctrlIface.handle, sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
```
4.
通过UsbFnStartRecvInterfaceEvent函数接收Event事件,并通过UsbFnEventCallback回调函数对Event事件做出响应。
```cpp
// 开始接收Event事件
ret = UsbFnStartRecvInterfaceEvent(acm
->ctrlIface.fn, 0xff, UsbAcmEventCallback, acm
);
ret = UsbFnStartRecvInterfaceEvent(acm
Device->ctrlIface.fn, 0xff, AcmEventCallback, acmDevice
);
// Event处理回调函数
static void UsbAcmEventCallback(struct UsbFnEvent *event)
{
struct UsbAcmDevice *acm = NULL;
if (event == NULL || event->context == NULL) {
HDF_LOGE("%s: event is null", __func__);
HDF_LOGE("%
{public}
s: event is null", __func__);
return;
}
acm = (struct UsbAcmDevice *)event->context;
switch (event->type) {
case USBFN_STATE_BIND:
HDF_LOGI("%s: receive bind event", __func__);
HDF_LOGI("%
{public}
s: receive bind event", __func__);
break;
case USBFN_STATE_UNBIND:
HDF_LOGI("%s: receive unbind event", __func__);
HDF_LOGI("%
{public}
s: receive unbind event", __func__);
break;
case USBFN_STATE_ENABLE:
HDF_LOGI("%s: receive enable event", __func__);
HDF_LOGI("%
{public}
s: receive enable event", __func__);
AcmEnable(acm);
break;
case USBFN_STATE_DISABLE:
HDF_LOGI("%s: receive disable event", __func__);
HDF_LOGI("%
{public}
s: receive disable event", __func__);
AcmDisable(acm);
acm->enableEvtCnt = 0;
break;
case USBFN_STATE_SETUP:
HDF_LOGI("%s: receive setup event", __func__);
HDF_LOGI("%
{public}
s: receive setup event", __func__);
if (event->setup != NULL) {
AcmSetup(acm, event->setup);
}
break;
case USBFN_STATE_SUSPEND:
HDF_LOGI("%s: receive suspend event", __func__);
HDF_LOGI("%
{public}
s: receive suspend event", __func__);
AcmSuspend(acm);
break;
case USBFN_STATE_RESUME:
HDF_LOGI("%s: receive resume event", __func__);
HDF_LOGI("%
{public}
s: receive resume event", __func__);
AcmResume(acm);
break;
default:
...
...
@@ -434,10 +429,16 @@ USB驱动基于HDF框架、Platform和OSAL基础接口进行开发,不区分
```cpp
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;
}
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基础接口进行开发,不区分
#include "usb_serial.h"
#include "hdf_base.h"
#include "hdf_log.h"
#include "hdf_usb_pnp_manage.h"
#include "osal_mem.h"
#include "osal_time.h"
#include "securec.h"
#include "usb_ddk_interface.h"
#include "hdf_usb_pnp_manage.h"
#define HDF_LOG_TAG USB_HOST_ACM
#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
bool
g_acmReleaseFlag
=
false
;
static
uint8_t
*
g_acmReadBuffer
=
NULL
;
...
static
int32_t
SerialCtrlMsg
(
struct
AcmDevice
*
acm
,
uint8_t
request
,
uint16_t
value
,
void
*
buf
,
uint16_t
len
)
static
int32_t
SerialCtrlMsg
(
struct
AcmDevice
*
acm
,
uint8_t
request
,
uint16_t
value
,
void
*
buf
,
uint16_t
len
)
{
int32_t
ret
;
uint16_t
index
=
acm
->
intPipe
->
interfaceId
;
struct
UsbControlParams
controlParams
;
struct
UsbRequestParams
params
;
// 定义一个USB请求参数对象
if
(
acm
==
NULL
||
buf
==
NULL
)
{
if
(
acm
==
NULL
||
buf
==
NULL
||
acm
->
intPipe
==
NULL
)
{
HDF_LOGE
(
"%s:invalid param"
,
__func__
);
return
HDF_ERR_IO
;
}
uint16_t
index
=
acm
->
intPipe
->
interfaceId
;
struct
UsbControlParams
controlParams
=
{};
struct
UsbRequestParams
parmas
=
{};
// 定义一个USB请求参数对象
if
(
acm
->
ctrlReq
==
NULL
)
{
// 为获取到的UsbInterfaceHandle预先分配待发送的IO Request对象
acm
->
ctrlReq
=
UsbAllocRequest
(
acm
->
ctrDevHandle
,
0
,
len
);
if
(
acm
->
ctrlReq
==
NULL
)
{
HDF_LOGE
(
"%s: UsbAllocRequest failed"
,
__func__
);
return
HDF_ERR_IO
;
}
}
...
...
@@ -485,72 +487,70 @@ static int32_t SerialCtrlMsg(struct AcmDevice *acm, uint8_t request,
controlParams
.
request
=
request
;
controlParams
.
target
=
USB_REQUEST_TARGET_INTERFACE
;
// 接口对象
controlParams
.
reqType
=
USB_REQUEST_TYPE_CLASS
;
// 请求类型
controlParams
.
direct
i
on
=
USB_REQUEST_DIR_TO_DEVICE
;
// 从主机到设备的数据传输
controlParams
.
directon
=
USB_REQUEST_DIR_TO_DEVICE
;
// 从主机到设备的数据传输
controlParams
.
value
=
value
;
controlParams
.
index
=
index
;
controlParams
.
data
=
buf
;
controlParams
.
size
=
len
;
params
.
interfaceId
=
USB_CTRL_INTERFACE_ID
;
// 定义USB控制接口的默认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
);
parmas
.
interfaceId
=
USB_CTRL_INTERFACE_ID
;
// 定义USB控制接口的默认ID
if
(
acm
->
ctrPipe
!=
NULL
)
{
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
);
parmas
.
callback
=
NULL
;
// 根据params填充预先分配的IO Request
ret
=
UsbFillRequest
(
acm
->
ctrlReq
,
acm
->
ctrDevHandle
,
&
par
am
s
);
ret
=
UsbFillRequest
(
acm
->
ctrlReq
,
acm
->
ctrDevHandle
,
&
par
ma
s
);
if
(
ret
!=
HDF_SUCCESS
)
{
HDF_LOGE
(
"%s: UsbFillRequest failed, ret = %d "
,
__func__
,
ret
);
return
ret
;
}
// 发送同步IO Request
ret
=
UsbSubmitRequestSync
(
acm
->
ctrlReq
);
if
(
ret
!=
HDF_SUCCESS
)
{
HDF_LOGE
(
"UsbSubmitRequestSync failed, ret = %d "
,
ret
);
return
ret
;
}
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
;
}
...
static
struct
UsbInterface
*
GetUsbInterfaceById
(
const
struct
AcmDevice
*
acm
,
uint8_t
interfaceIndex
)
static
struct
UsbInterface
*
GetUsbInterfaceById
(
const
struct
AcmDevice
*
acm
,
uint8_t
interfaceIndex
)
{
struct
UsbInterface
*
tmpIf
=
NULL
;
// 获取UsbInterface接口对象
tmpIf
=
(
struct
UsbInterface
*
)
UsbClaimInterface
(
acm
->
session
,
acm
->
busNum
,
acm
->
devAddr
,
interfaceIndex
);
return
tmpIf
;
return
UsbClaimInterface
(
acm
->
session
,
acm
->
busNum
,
acm
->
devAddr
,
interfaceIndex
);
}
...
static
struct
UsbPipeInfo
*
EnumePipe
(
const
struct
AcmDevice
*
acm
,
uint8_t
interfaceIndex
,
UsbPipeType
pipeType
,
UsbPipeDirection
pipeDirection
)
static
struct
UsbPipeInfo
*
EnumePipe
(
const
struct
AcmDevice
*
acm
,
uint8_t
interfaceIndex
,
UsbPipeType
pipeType
,
UsbPipeDirection
pipeDirection
)
{
uint8_t
i
;
int32_t
ret
;
struct
UsbInterfaceInfo
*
info
=
NULL
;
// 定义一个USB接口信息对象
UsbInterfaceHandle
*
interfaceHandle
=
NULL
;
// 定义一个USB接口操作句柄,就是void *类型
if
(
pipeType
==
USB_PIPE_TYPE_CONTROL
)
{
if
(
pipeType
==
USB_PIPE_TYPE_CONTROL
)
{
info
=
&
acm
->
ctrIface
->
info
;
interfaceHandle
=
acm
->
ctrDevHandle
;
}
else
{
info
=
&
acm
->
iface
[
interfaceIndex
]
->
info
;
}
else
{
// 根据interfaceIndex获取设备句柄
info
=
&
acm
->
iface
[
interfaceIndex
]
->
info
;
interfaceHandle
=
InterfaceIdToHandle
(
acm
,
info
->
interfaceIndex
);
}
for
(
i
=
0
;
i
<=
info
->
pipeNum
;
i
++
)
{
for
(
uint8_t
i
=
0
;
i
<=
info
->
pipeNum
;
i
++
)
{
struct
UsbPipeInfo
p
;
// 获取指定索引为i的pipeInfo信息
ret
=
UsbGetPipeInfo
(
interfaceHandle
,
info
->
curAltSetting
,
i
,
&
p
);
int32_t
ret
=
UsbGetPipeInfo
(
interfaceHandle
,
info
->
curAltSetting
,
i
,
&
p
);
if
(
ret
<
0
)
{
continue
;
}
if
((
p
.
pipeDirection
==
pipeDirection
)
&&
(
p
.
pipeType
==
pipeType
))
{
struct
UsbPipeInfo
*
pi
=
OsalMemCalloc
(
sizeof
(
*
pi
));
// 开辟内存并初始化
if
(
pi
==
NULL
)
{
HDF_LOGE
(
"%s: Alloc pipe failed"
,
__func__
);
return
NULL
;
}
p
.
interfaceId
=
info
->
interfaceIndex
;
...
...
@@ -561,11 +561,11 @@ static struct UsbPipeInfo *EnumePipe(const struct AcmDevice *acm,
return
NULL
;
}
static
struct
UsbPipeInfo
*
GetPipe
(
const
struct
AcmDevice
*
acm
,
UsbPipeType
pipeType
,
UsbPipeDirection
pipeDirection
)
static
struct
UsbPipeInfo
*
GetPipe
(
const
struct
AcmDevice
*
acm
,
UsbPipeType
pipeType
,
UsbPipeDirection
pipeDirection
)
{
uint8_t
i
;
if
(
acm
==
NULL
)
{
HDF_LOGE
(
"%s: invalid param"
,
__func__
);
return
NULL
;
}
for
(
i
=
0
;
i
<
acm
->
interfaceCnt
;
i
++
)
{
...
...
@@ -590,39 +590,48 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
errno_t
err
;
struct
AcmDevice
*
acm
=
NULL
;
if
(
device
==
NULL
)
{
HDF_LOGE
(
"%s: device is null"
,
__func__
);
return
HDF_ERR_INVALID_OBJECT
;
}
//开辟内存空间
acm
=
(
struct
AcmDevice
*
)
OsalMemCalloc
(
sizeof
(
*
acm
));
if
(
acm
==
NULL
)
{
HDF_LOGE
(
"%s: Alloc usb serial device failed"
,
__func__
);
return
HDF_FAILURE
;
}
// 初始化互斥锁,&acm->lock表示指向互斥量的指针
if
(
OsalMutexInit
(
&
acm
->
lock
)
!=
HDF_SUCCESS
)
{
goto
error
;
HDF_LOGE
(
"%s:%d OsalMutexInit failed"
,
__func__
,
__LINE__
);
goto
ERROR
;
}
info
=
(
struct
UsbPnpNotifyServiceInfo
*
)
device
->
priv
;
if
(
info
!=
NULL
)
{
acm
->
busNum
=
info
->
busNum
;
acm
->
devAddr
=
info
->
devNum
;
HDF_LOGD
(
"%s:%d busNum=%d,devAddr=%d,interfaceLength=%d"
,
__func__
,
__LINE__
,
info
->
busNum
,
info
->
devNum
,
info
->
interfaceLength
);
acm
->
busNum
=
(
uint8_t
)
info
->
busNum
;
acm
->
devAddr
=
(
uint8_t
)
info
->
devNum
;
acm
->
interfaceCnt
=
info
->
interfaceLength
;
err
=
memcpy_s
((
void
*
)(
acm
->
interfaceIndex
),
USB_MAX_INTERFACES
,
(
const
void
*
)
info
->
interfaceNumber
,
info
->
interfaceLength
);
err
=
memcpy_s
((
void
*
)(
acm
->
interfaceIndex
),
USB_MAX_INTERFACES
,
(
const
void
*
)
info
->
interfaceNumber
,
info
->
interfaceLength
);
if
(
err
!=
EOK
)
{
goto
lock_error
;
HDF_LOGE
(
"%s:%d memcpy_s failed err = %d"
,
__func__
,
__LINE__
,
err
);
goto
LOCK_ERROR
;
}
}
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
);
acm
->
device
->
service
->
Dispatch
=
UsbSerialDeviceDispatch
;
HDF_LOGD
(
"UsbSerialDriverBind=========================OK"
);
return
HDF_SUCCESS
;
lock_error
:
LOCK_ERROR
:
if
(
OsalMutexDestroy
(
&
acm
->
lock
))
{
HDF_LOGE
(
"%s:%d OsalMutexDestroy failed"
,
__func__
,
__LINE__
);
}
error
:
ERROR
:
OsalMemFree
(
acm
);
acm
=
NULL
;
return
HDF_FAILURE
;
...
...
@@ -631,32 +640,34 @@ error:
static
int32_t
AcmAllocReadRequests
(
struct
AcmDevice
*
acm
)
{
int32_t
ret
;
struct
UsbRequestParams
readPar
ams
;
struct
UsbRequestParams
readPar
mas
=
{}
;
for
(
int32_t
i
=
0
;
i
<
ACM_NR
;
i
++
)
{
// 分配待发送的readReq IO Request对象
acm
->
readReq
[
i
]
=
UsbAllocRequest
(
InterfaceIdToHandle
(
acm
,
acm
->
dataInPipe
->
interfaceId
),
0
,
acm
->
readSize
);
if
(
!
acm
->
readReq
[
i
])
{
goto
error
;
HDF_LOGE
(
"readReq request failed
\n
"
);
goto
ERROR
;
}
readPar
am
s
.
userData
=
(
void
*
)
acm
;
readPar
am
s
.
pipeAddress
=
acm
->
dataInPipe
->
pipeAddress
;
readPar
am
s
.
pipeId
=
acm
->
dataInPipe
->
pipeId
;
readPar
am
s
.
interfaceId
=
acm
->
dataInPipe
->
interfaceId
;
readPar
am
s
.
callback
=
AcmReadBulk
;
readPar
am
s
.
requestType
=
USB_REQUEST_PARAMS_DATA_TYPE
;
/* Data type */
readPar
am
s
.
timeout
=
USB_CTRL_SET_TIMEOUT
;
readPar
am
s
.
dataReq
.
numIsoPackets
=
0
;
readPar
ams
.
dataReq
.
direction
=
(
acm
->
dataInPipe
->
pipeDirection
>>
USB_PIPE_DIR_OFFSET
)
&
0x1
;
readPar
ams
.
dataReq
.
length
=
acm
->
readSize
;
readPar
ma
s
.
userData
=
(
void
*
)
acm
;
readPar
ma
s
.
pipeAddress
=
acm
->
dataInPipe
->
pipeAddress
;
readPar
ma
s
.
pipeId
=
acm
->
dataInPipe
->
pipeId
;
readPar
ma
s
.
interfaceId
=
acm
->
dataInPipe
->
interfaceId
;
readPar
ma
s
.
callback
=
AcmReadBulk
;
readPar
ma
s
.
requestType
=
USB_REQUEST_PARAMS_DATA_TYPE
;
/* Data type */
readPar
ma
s
.
timeout
=
USB_CTRL_SET_TIMEOUT
;
readPar
ma
s
.
dataReq
.
numIsoPackets
=
0
;
readPar
mas
.
dataReq
.
directon
=
(((
uint8_t
)
acm
->
dataInPipe
->
pipeDirection
)
>>
USB_PIPE_DIR_OFFSET
)
&
0x1
;
readPar
mas
.
dataReq
.
length
=
(
int
)
acm
->
readSize
;
// 根据readParams填充预先分配待发送的readReq IO Request对象
ret
=
UsbFillRequest
(
acm
->
readReq
[
i
],
InterfaceIdToHandle
(
acm
,
acm
->
dataInPipe
->
interfaceId
),
&
readPar
am
s
);
ret
=
UsbFillRequest
(
acm
->
readReq
[
i
],
InterfaceIdToHandle
(
acm
,
acm
->
dataInPipe
->
interfaceId
),
&
readPar
ma
s
);
if
(
ret
!=
HDF_SUCCESS
)
{
goto
error
;
HDF_LOGE
(
"%s: UsbFillRequest failed, ret=%d
\n
"
,
__func__
,
ret
);
goto
ERROR
;
}
}
return
HDF_SUCCESS
;
error
:
ERROR
:
AcmFreeReadRequests
(
acm
);
return
HDF_ERR_MALLOC_FAIL
;
}
...
...
@@ -664,37 +675,39 @@ error:
static
int32_t
AcmAllocNotifyRequest
(
struct
AcmDevice
*
acm
)
{
int32_t
ret
;
struct
UsbRequestParams
intPar
am
s
=
{};
struct
UsbRequestParams
intPar
ma
s
=
{};
// 分配待发送的中断IO Request对象
acm
->
notifyReq
=
UsbAllocRequest
(
InterfaceIdToHandle
(
acm
,
acm
->
intPipe
->
interfaceId
),
0
,
acm
->
intSize
);
if
(
!
acm
->
notifyReq
)
{
HDF_LOGE
(
"notifyReq request failed.
\n
"
);
return
HDF_ERR_MALLOC_FAIL
;
}
intPar
am
s
.
userData
=
(
void
*
)
acm
;
intPar
am
s
.
pipeAddress
=
acm
->
intPipe
->
pipeAddress
;
intPar
am
s
.
pipeId
=
acm
->
intPipe
->
pipeId
;
intPar
am
s
.
interfaceId
=
acm
->
intPipe
->
interfaceId
;
intPar
am
s
.
callback
=
AcmCtrlIrq
;
intPar
am
s
.
requestType
=
USB_REQUEST_PARAMS_DATA_TYPE
;
intPar
am
s
.
timeout
=
USB_CTRL_SET_TIMEOUT
;
intPar
am
s
.
dataReq
.
numIsoPackets
=
0
;
intPar
ams
.
dataReq
.
direction
=
(
acm
->
intPipe
->
pipeDirection
>>
USB_PIPE_DIR_OFFSET
)
&
DIRECTION_MASK
;
intPar
ams
.
dataReq
.
length
=
acm
->
intSize
;
intPar
ma
s
.
userData
=
(
void
*
)
acm
;
intPar
ma
s
.
pipeAddress
=
acm
->
intPipe
->
pipeAddress
;
intPar
ma
s
.
pipeId
=
acm
->
intPipe
->
pipeId
;
intPar
ma
s
.
interfaceId
=
acm
->
intPipe
->
interfaceId
;
intPar
ma
s
.
callback
=
AcmCtrlIrq
;
intPar
ma
s
.
requestType
=
USB_REQUEST_PARAMS_DATA_TYPE
;
intPar
ma
s
.
timeout
=
USB_CTRL_SET_TIMEOUT
;
intPar
ma
s
.
dataReq
.
numIsoPackets
=
0
;
intPar
mas
.
dataReq
.
directon
=
(((
uint8_t
)
acm
->
intPipe
->
pipeDirection
)
>>
USB_PIPE_DIR_OFFSET
)
&
DIRECTION_MASK
;
intPar
mas
.
dataReq
.
length
=
(
int
)
acm
->
intSize
;
// 填充预先分配的中断IO Request
ret
=
UsbFillRequest
(
acm
->
notifyReq
,
InterfaceIdToHandle
(
acm
,
acm
->
intPipe
->
interfaceId
),
&
intPar
am
s
);
ret
=
UsbFillRequest
(
acm
->
notifyReq
,
InterfaceIdToHandle
(
acm
,
acm
->
intPipe
->
interfaceId
),
&
intPar
ma
s
);
if
(
ret
!=
HDF_SUCCESS
)
{
goto
error
;
HDF_LOGE
(
"%s: UsbFillRequest failed, ret = %d"
,
__func__
,
ret
);
goto
ERROR
;
}
return
HDF_SUCCESS
;
error
:
AcmFreeNotifyReq
ue
st
(
acm
);
ERROR
:
AcmFreeNotifyReq
eu
st
(
acm
);
return
ret
;
}
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
])
{
// 释放一个USB接口对象
UsbReleaseInterface
(
acm
->
iface
[
i
]);
...
...
@@ -709,23 +722,24 @@ static void AcmReleaseInterfaces(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接口对象
acm
->
iface
[
i
]
=
GetUsbInterfaceById
((
const
struct
AcmDevice
*
)
acm
,
acm
->
interfaceIndex
[
i
]);
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
);
if
(
acm
->
ctrIface
==
NULL
)
{
goto
error
;
HDF_LOGE
(
"%s: GetUsbInterfaceById null"
,
__func__
);
goto
ERROR
;
}
return
HDF_SUCCESS
;
error
:
ERROR
:
// 根据acm->interfaceCnt循环释放接口对象
AcmReleaseInterfaces
(
acm
);
return
HDF_FAILURE
;
...
...
@@ -733,7 +747,7 @@ static int32_t AcmClaimInterfaces(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
])
{
// 关闭一个USB设备对象
UsbCloseInterface
(
acm
->
devHandle
[
i
]);
...
...
@@ -748,23 +762,25 @@ static void AcmCloseInterfaces(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
])
{
// 打开获取到的UsbInterface接口对象
acm
->
devHandle
[
i
]
=
UsbOpenInterface
(
acm
->
iface
[
i
]);
if
(
acm
->
devHandle
[
i
]
==
NULL
)
{
goto
error
;
HDF_LOGE
(
"%s: UsbOpenInterface null"
,
__func__
);
goto
ERROR
;
}
}
}
acm
->
ctrDevHandle
=
UsbOpenInterface
(
acm
->
ctrIface
);
if
(
acm
->
ctrDevHandle
==
NULL
)
{
goto
error
;
HDF_LOGE
(
"%s: ctrDevHandle UsbOpenInterface null"
,
__func__
);
goto
ERROR
;
}
return
HDF_SUCCESS
;
error
:
ERROR
:
// 关闭所有UsbInterface接口对象
AcmCloseInterfaces
(
acm
);
return
HDF_FAILURE
;
...
...
@@ -775,34 +791,36 @@ static int32_t AcmGetPipes(struct AcmDevice *acm)
// 获取dataInPipe的pipeInfo信息
acm
->
dataInPipe
=
GetPipe
(
acm
,
USB_PIPE_TYPE_BULK
,
USB_PIPE_DIRECTION_IN
);
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
);
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
);
if
(
acm
->
ctrPipe
==
NULL
)
{
goto
error
;
HDF_LOGE
(
"ctrPipe is null"
);
goto
ERROR
;
}
// 获取中断pipe的pipeInfo信息
acm
->
intPipe
=
GetPipe
(
acm
,
USB_PIPE_TYPE_INTERRUPT
,
USB_PIPE_DIRECTION_IN
);
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
->
ctrlSize
=
acm
->
ctrPipe
->
maxPacketSize
;
acm
->
intSize
=
acm
->
intPipe
->
maxPacketSize
;
acm
->
ctrlSize
=
acm
->
ctrPipe
->
maxPacketSize
;
acm
->
intSize
=
acm
->
intPipe
->
maxPacketSize
;
return
HDF_SUCCESS
;
error
:
ERROR
:
// 释放设备中所有的管道信息
AcmFreePipes
(
acm
);
return
HDF_FAILURE
;
...
...
@@ -815,105 +833,112 @@ static void AcmFreeRequests(struct AcmDevice *acm)
g_syncRequest
=
NULL
;
}
AcmFreeReadRequests
(
acm
);
AcmFreeNotifyReq
ue
st
(
acm
);
AcmFreeNotifyReq
eu
st
(
acm
);
AcmFreeWriteRequests
(
acm
);
AcmWriteBufFree
(
acm
);
}
static
int32_t
AcmAllocRequests
(
struct
AcmDevice
*
acm
)
static
int32_t
AcmAllocRequests
(
const
struct
AcmDevice
*
acm
)
{
int32_t
ret
;
if
(
AcmWriteBufAlloc
(
acm
)
<
0
)
{
HDF_LOGE
(
"%s: AcmWriteBufAlloc failed"
,
__func__
);
return
HDF_ERR_MALLOC_FAIL
;
}
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对象
snd
->
request
=
UsbAllocRequest
(
InterfaceIdToHandle
(
acm
,
acm
->
dataOutPipe
->
interfaceId
),
0
,
acm
->
writeSize
);
snd
->
instance
=
acm
;
snd
->
request
=
UsbAllocRequest
(
InterfaceIdToHandle
((
struct
AcmDevice
*
)
acm
,
acm
->
dataOutPipe
->
interfaceId
),
0
,
acm
->
writeSize
);
snd
->
instance
=
(
struct
AcmDevice
*
)
acm
;
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
)
{
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
)
{
goto
error_alloc_read_req
;
HDF_LOGE
(
"%s:%d AcmAllocReadRequests fail"
,
__func__
,
__LINE__
);
goto
ERROR_ALLOC_READ_REQ
;
}
return
HDF_SUCCESS
;
error_alloc_read_req
:
AcmFreeNotifyReq
uest
(
acm
);
error_alloc_int_req
:
AcmFreeWriteRequests
(
acm
);
error_alloc_write_req
:
AcmWriteBufFree
(
acm
);
ERROR_ALLOC_READ_REQ
:
AcmFreeNotifyReq
eust
((
struct
AcmDevice
*
)
acm
);
ERROR_ALLOC_INT_REQ
:
AcmFreeWriteRequests
(
(
struct
AcmDevice
*
)
acm
);
ERROR_ALLOC_WRITE_REQ
:
AcmWriteBufFree
(
(
struct
AcmDevice
*
)
acm
);
return
HDF_FAILURE
;
}
static
int32_t
AcmInit
(
struct
AcmDevice
*
acm
)
{
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
;
}
// 初始化Host DDK
// 初始化Host DDK
ret
=
UsbInitHostSdk
(
NULL
);
if
(
ret
!=
HDF_SUCCESS
)
{
HDF_LOGE
(
"%{public}s: UsbInitHostSdk failed"
,
__func__
);
return
HDF_ERR_IO
;
}
acm
->
session
=
session
;
// 根据acm->interfaceIndex[i]分别获取UsbInterface接口对象
acm
->
session
=
NULL
;
// 根据acm->interfaceIndex[i]分别获取UsbInterface接口对象
ret
=
AcmClaimInterfaces
(
acm
);
if
(
ret
!=
HDF_SUCCESS
)
{
goto
error_claim_interfaces
;
HDF_LOGE
(
"%{public}s: AcmClaimInterfaces failed"
,
__func__
);
goto
ERROR_CLAIM_INTERFACES
;
}
// 根据acm->iface[i]分别打开UsbInterface接口对象
ret
=
AcmOpenInterfaces
(
acm
);
if
(
ret
!=
HDF_SUCCESS
)
{
goto
error_open_interfaces
;
HDF_LOGE
(
"%{public}s: AcmOpenInterfaces failed"
,
__func__
);
goto
ERROR_OPEN_INTERFACES
;
}
// 获取管道信息的指针
ret
=
AcmGetPipes
(
acm
);
if
(
ret
!=
HDF_SUCCESS
)
{
goto
error_get_pipes
;
HDF_LOGE
(
"%{public}s: AcmGetPipes failed"
,
__func__
);
goto
ERROR_GET_PIPES
;
}
ret
=
AcmAllocRequests
(
acm
);
if
(
ret
!=
HDF_SUCCESS
)
{
goto
error_alloc_reqs
;
HDF_LOGE
(
"%{public}s: AcmAllocRequests failed"
,
__func__
);
goto
ERROR_ALLOC_REQS
;
}
acm
->
lineCoding
.
dwDTERate
=
C
puToLe32
(
DATARATE
);
// 转换为小端数据
acm
->
lineCoding
.
bCharFormat
=
CHARFORMAT
;
// 8
acm
->
lineCoding
.
dwDTERate
=
C
PU_TO_LE32
(
DATARATE
);
acm
->
lineCoding
.
bCharFormat
=
USB_CDC_1_STOP_BITS
;
acm
->
lineCoding
.
bParityType
=
USB_CDC_NO_PARITY
;
acm
->
lineCoding
.
bDataBits
=
USB_CDC_1_STOP_BITS
;
acm
->
lineCoding
.
bDataBits
=
DATA_BITS_LENGTH
;
acm
->
initFlag
=
true
;
return
HDF_SUCCESS
;
error_alloc_reqs
:
ERROR_ALLOC_REQS
:
AcmFreePipes
(
acm
);
error_get_pipes
:
// 关闭所有UsbInterface接口对象
ERROR_GET_PIPES
:
// 关闭所有UsbInterface接口对象
AcmCloseInterfaces
(
acm
);
error_open_interfaces
:
// 释放所有UsbInterface接口对象
ERROR_OPEN_INTERFACES
:
// 释放所有UsbInterface接口对象
AcmReleaseInterfaces
(
acm
);
error_claim_interfaces
:
ERROR_CLAIM_INTERFACES
:
// 在主机端退出USB DDK,acm->session代表指向会话上下文的指针
UsbExitHostSdk
(
acm
->
session
);
acm
->
session
=
NULL
;
...
...
@@ -922,17 +947,19 @@ error_claim_interfaces:
static
void
AcmRelease
(
struct
AcmDevice
*
acm
)
{
if
(
acm
->
initFlag
==
false
)
{
if
(
!
(
acm
->
initFlag
))
{
HDF_LOGE
(
"%s:%d: initFlag is false"
,
__func__
,
__LINE__
);
return
;
}
AcmFreeRequests
(
acm
);
AcmFreePipes
(
acm
);
AcmCloseInterfaces
(
acm
);
AcmReleaseInterfaces
(
acm
);
AcmFreeRequests
(
acm
);
AcmFreePipes
(
acm
);
// 在主机端退出USB DDK
UsbExitHostSdk
(
acm
->
session
);
acm
->
session
=
NULL
;
acm
->
initFlag
=
false
;
}
...
...
@@ -942,14 +969,17 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
struct
AcmDevice
*
acm
=
NULL
;
if
(
device
==
NULL
)
{
HDF_LOGE
(
"%s: device is null"
,
__func__
);
return
HDF_ERR_INVALID_OBJECT
;
}
acm
=
(
struct
AcmDevice
*
)
device
->
service
;
// 初始化互斥锁,&acm->readLock表示指向互斥量的指针
if
(
acm
==
NULL
)
{
return
HDF_ERR_INVALID_OBJECT
;
}
OsalMutexInit
(
&
acm
->
readLock
);
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串口设备信息开辟空间并赋值
ret
=
UsbSerialDeviceAlloc
(
acm
);
if
(
ret
!=
HDF_SUCCESS
)
{
...
...
@@ -958,6 +988,9 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
acm
->
initFlag
=
false
;
g_acmReleaseFlag
=
false
;
HDF_LOGD
(
"%s:%d init ok!"
,
__func__
,
__LINE__
);
return
ret
;
}
...
...
@@ -966,16 +999,19 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
struct
AcmDevice
*
acm
=
NULL
;
if
(
device
==
NULL
)
{
HDF_LOGE
(
"%s: device is null"
,
__func__
);
return
;
}
acm
=
(
struct
AcmDevice
*
)
device
->
service
;
if
(
acm
==
NULL
)
{
HDF_LOGE
(
"%s: acm is null"
,
__func__
);
return
;
}
g_acmReleaseFlag
=
true
;
if
(
acm
->
initFlag
==
true
)
{
if
(
acm
->
initFlag
)
{
HDF_LOGE
(
"%s:%d AcmRelease"
,
__func__
,
__LINE__
);
AcmRelease
(
acm
);
}
// 释放usb串口设备信息
...
...
@@ -986,21 +1022,21 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
OsalMutexDestroy
(
&
acm
->
lock
);
OsalMemFree
(
acm
);
acm
=
NULL
;
HDF_LOGD
(
"%s:%d exit"
,
__func__
,
__LINE__
);
}
// 驱动的Bind、Init、及Release操作
struct
HdfDriverEntry
g_usbSerialDriverEntry
=
{
.
moduleVersion
=
1
,
.
moduleName
=
"usbhost_acm"
,
// 驱动模块名称,必须与hcs文件中配置的名称一致
.
Bind
=
UsbSerialDriverBind
,
.
Init
=
UsbSerialDriverInit
,
.
Release
=
UsbSerialDriverRelease
,
.
moduleName
=
"usbhost_acm"
,
// 驱动模块名称,必须与hcs文件中配置的名称一致
.
Bind
=
UsbSerialDriverBind
,
.
Init
=
UsbSerialDriverInit
,
.
Release
=
UsbSerialDriverRelease
,
};
HDF_INIT
(
g_usbSerialDriverEntry
);
// 驱动入口
```
#### Host RAW API驱动开发
```
cpp
root
{
module
=
"usb_pnp_device"
;
...
...
@@ -1051,27 +1087,27 @@ root {
```
```
cpp
#include "usb_serial_rawapi.h"
#include <unistd.h>
#include "osal_mem.h"
#include "osal_time.h"
#include "securec.h"
#include "hdf_base.h"
#include "hdf_log.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 USB_CTRL_REQ_SIZE
64
#define USB_IO_THREAD_STACK_SIZE
8192
#define USB_RAW_IO_SLEEP_MS_TIME
100
#define USB_RAW_IO_STOP_WAIT_MAX_TIME
3
#define HDF_LOG_TAG
USB_HOST_ACM_RAW_API
#define USB_CTRL_REQ_SIZE 64
#define USB_IO_THREAD_STACK_SIZE 8192
#define USB_RAW_IO_SLEEP_MS_TIME 100
#define USB_RAW_IO_STOP_WAIT_MAX_TIME 3
static
struct
UsbRawRequest
*
g_syncRequest
=
NULL
;
static
UsbRawIoProcessStatusType
g_stopIoStatus
=
USB_RAW_IO_PROCESS_RUNNING
;
struct
OsalMutex
g_stopIoLock
;
static
bool
g_rawAcmReleaseFlag
=
false
;
......
...
static
int32_t
UsbGetConfigDescriptor
(
UsbRawHandle
*
devHandle
,
struct
UsbRawConfigDescriptor
**
config
)
{
UsbRawDevice
*
dev
=
NULL
;
...
...
@@ -1079,30 +1115,33 @@ static int32_t UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConf
int32_t
ret
;
if
(
devHandle
==
NULL
)
{
HDF_LOGE
(
"%s:%d devHandle is null"
,
__func__
,
__LINE__
);
return
HDF_ERR_INVALID_PARAM
;
}
// 获取主用设备配置
ret
=
UsbRawGetConfiguration
(
devHandle
,
&
activeConfig
);
if
(
ret
!=
HDF_SUCCESS
)
{
if
(
ret
)
{
HDF_LOGE
(
"%s:%d UsbRawGetConfiguration failed, ret = %d"
,
__func__
,
__LINE__
,
ret
);
return
HDF_FAILURE
;
}
HDF_LOGE
(
"%s:%d activeConfig = %d"
,
__func__
,
__LINE__
,
activeConfig
);
// 根据指定的设备句柄获取设备指针
dev
=
UsbRawGetDevice
(
devHandle
);
if
(
dev
==
NULL
)
{
HDF_LOGE
(
"%s:%d UsbRawGetDevice failed"
,
__func__
,
__LINE__
);
return
HDF_FAILURE
;
}
// 根据指定的设备ID获取设备配置描述符
ret
=
UsbRawGetConfigDescriptor
(
dev
,
activeConfig
,
config
);
if
(
ret
!=
HDF_SUCCESS
)
{
HDF_LOGE
(
"UsbRawGetConfigDescriptor failed, ret=%d
\n
"
,
ret
);
if
(
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
;
...
...
@@ -1112,6 +1151,7 @@ static int32_t UsbAllocWriteRequests(struct AcmDevice *acm)
snd
->
request
=
UsbRawAllocRequest
(
acm
->
devHandle
,
0
,
acm
->
dataOutEp
->
maxPacketSize
);
snd
->
instance
=
acm
;
if
(
snd
->
request
==
NULL
)
{
HDF_LOGE
(
"%s: UsbRawAllocRequest failed"
,
__func__
);
return
HDF_ERR_MALLOC_FAIL
;
}
}
...
...
@@ -1127,41 +1167,47 @@ static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
errno_t
err
;
if
(
device
==
NULL
)
{
HDF_LOGE
(
"%s: device is null"
,
__func__
);
return
HDF_ERR_INVALID_OBJECT
;
}
acm
=
(
struct
AcmDevice
*
)
OsalMemCalloc
(
sizeof
(
*
acm
));
if
(
acm
==
NULL
)
{
HDF_LOGE
(
"%s: Alloc usb serial device failed"
,
__func__
);
return
HDF_FAILURE
;
}
if
(
OsalMutexInit
(
&
acm
->
lock
)
!=
HDF_SUCCESS
)
{
goto
error
;
HDF_LOGE
(
"%s:%d OsalMutexInit fail"
,
__func__
,
__LINE__
);
goto
ERROR
;
}
info
=
(
struct
UsbPnpNotifyServiceInfo
*
)
device
->
priv
;
if
(
info
!=
NULL
)
{
acm
->
busNum
=
info
->
busNum
;
acm
->
devAddr
=
info
->
devNum
;
acm
->
busNum
=
(
uint8_t
)
info
->
busNum
;
acm
->
devAddr
=
(
uint8_t
)
info
->
devNum
;
acm
->
interfaceCnt
=
info
->
interfaceLength
;
err
=
memcpy_s
((
void
*
)(
acm
->
interfaceIndex
),
USB_MAX_INTERFACES
,
(
const
void
*
)
info
->
interfaceNumber
,
info
->
interfaceLength
);
err
=
memcpy_s
((
void
*
)(
acm
->
interfaceIndex
),
USB_MAX_INTERFACES
,
(
const
void
*
)
info
->
interfaceNumber
,
info
->
interfaceLength
);
if
(
err
!=
EOK
)
{
goto
lock_error
;
HDF_LOGE
(
"%s:%d memcpy_s failed err=%d"
,
__func__
,
__LINE__
,
err
);
goto
LOCK_ERROR
;
}
}
else
{
goto
lock_error
;
HDF_LOGE
(
"%s:%d info is NULL!"
,
__func__
,
__LINE__
);
goto
LOCK_ERROR
;
}
device
->
service
=
&
(
acm
->
service
);
device
->
service
->
Dispatch
=
UsbSerialDeviceDispatch
;
acm
->
device
=
device
;
HDF_LOGD
(
"UsbSerialDriverBind=========================OK"
);
return
HDF_SUCCESS
;
lock_error
:
LOCK_ERROR
:
if
(
OsalMutexDestroy
(
&
acm
->
lock
))
{
HDF_LOGE
(
"%s:%d OsalMutexDestroy fail
ed
"
,
__func__
,
__LINE__
);
HDF_LOGE
(
"%s:%d OsalMutexDestroy fail"
,
__func__
,
__LINE__
);
}
error
:
ERROR
:
OsalMemFree
(
acm
);
acm
=
NULL
;
return
HDF_FAILURE
;
...
...
@@ -1170,26 +1216,26 @@ error:
static
int32_t
UsbAllocReadRequests
(
struct
AcmDevice
*
acm
)
{
struct
UsbRawFillRequestData
reqData
;
int32_t
size
=
acm
->
dataInEp
->
maxPacketSize
;
int32_t
ret
;
uint32_t
size
=
acm
->
dataInEp
->
maxPacketSize
;
for
(
int32_t
i
=
0
;
i
<
ACM_NR
;
i
++
)
{
// 分配一个具有指定数目的同步传输分组描述符的传输请求
acm
->
readReq
[
i
]
=
UsbRawAllocRequest
(
acm
->
devHandle
,
0
,
size
);
if
(
!
acm
->
readReq
[
i
])
{
HDF_LOGE
(
"readReq request failed
\n
"
);
return
HDF_ERR_MALLOC_FAIL
;
}
reqData
.
endPoint
=
acm
->
dataInEp
->
addr
;
reqData
.
endPoint
=
acm
->
dataInEp
->
addr
;
reqData
.
numIsoPackets
=
0
;
reqData
.
callback
=
AcmReadBulkCallback
;
reqData
.
userData
=
(
void
*
)
acm
;
reqData
.
timeout
=
USB_CTRL_SET_TIMEOUT
;
reqData
.
length
=
size
;
reqData
.
callback
=
AcmReadBulkCallback
;
reqData
.
userData
=
(
void
*
)
acm
;
reqData
.
timeout
=
USB_CTRL_SET_TIMEOUT
;
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
)
{
HDF_LOGE
(
"%s: FillBulkRequest failed, ret=%d
\n
"
,
__func__
,
ret
);
return
HDF_FAILURE
;
}
}
...
...
@@ -1200,12 +1246,12 @@ static int32_t UsbAllocReadRequests(struct AcmDevice *acm)
static
int32_t
UsbAllocNotifyRequest
(
struct
AcmDevice
*
acm
)
{
struct
UsbRawFillRequestData
fillRequestData
;
int32_t
size
=
acm
->
notifyEp
->
maxPacketSize
;
u
int32_t
size
=
acm
->
notifyEp
->
maxPacketSize
;
int32_t
ret
;
// 分配一个具有指定数目的同步传输分组描述符的传输请求
acm
->
notifyReq
=
UsbRawAllocRequest
(
acm
->
devHandle
,
0
,
size
);
if
(
!
acm
->
notifyReq
)
{
HDF_LOGE
(
"notifyReq request fail
\n
"
);
return
HDF_ERR_MALLOC_FAIL
;
}
...
...
@@ -1215,10 +1261,10 @@ static int32_t UsbAllocNotifyRequest(struct AcmDevice *acm)
fillRequestData
.
callback
=
AcmNotifyReqCallback
;
fillRequestData
.
userData
=
(
void
*
)
acm
;
fillRequestData
.
timeout
=
USB_CTRL_SET_TIMEOUT
;
// 在中断传输请求中填充所需的信息
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
;
}
...
...
@@ -1231,98 +1277,111 @@ static int32_t UsbSerialInit(struct AcmDevice *acm)
UsbRawHandle
*
devHandle
=
NULL
;
int32_t
ret
;
if
(
acm
->
initFlag
==
true
)
{
if
(
acm
->
initFlag
)
{
HDF_LOGE
(
"%s:%d: initFlag is true"
,
__func__
,
__LINE__
);
return
HDF_SUCCESS
;
}
// 以专家模式初始化USB DDK
ret
=
UsbRawInit
(
NULL
);
if
(
ret
!=
HDF_SUCCESS
)
{
if
(
ret
)
{
HDF_LOGE
(
"%s:%d UsbRawInit failed"
,
__func__
,
__LINE__
);
return
HDF_ERR_IO
;
}
acm
->
session
=
session
;
// 打开一个USB设备对象
devHandle
=
UsbRawOpenDevice
(
session
,
acm
->
busNum
,
acm
->
devAddr
);
if
(
devHandle
==
NULL
)
{
ret
=
HDF_FAILURE
;
goto
err_open_device
;
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_SUCCESS
)
{
ret
=
HDF_FAILURE
;
goto
err_get_desc
;
if
(
ret
)
{
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
(
"%s:%d UsbParseConfigDescriptor failed"
,
__func__
,
__LINE__
);
ret
=
HDF_FAILURE
;
goto
err_parse_desc
;
goto
ERR_PARSE_DESC
;
}
ret
=
AcmWriteBufAlloc
(
acm
);
if
(
ret
<
0
)
{
HDF_LOGE
(
"%s:%d AcmWriteBufAlloc failed"
,
__func__
,
__LINE__
);
ret
=
HDF_FAILURE
;
goto
err_alloc_write_buf
;
goto
ERR_ALLOC_WRITE_BUF
;
}
ret
=
UsbAllocWriteRequests
(
acm
);
if
(
ret
<
0
)
{
HDF_LOGE
(
"%s:%d UsbAllocWriteRequests failed"
,
__func__
,
__LINE__
);
ret
=
HDF_FAILURE
;
goto
err_alloc_write_reqs
;
goto
ERR_ALLOC_WRITE_REQS
;
}
ret
=
UsbAllocNotifyRequest
(
acm
);
if
(
ret
)
{
goto
err_alloc_notify_req
;
HDF_LOGE
(
"%s:%d UsbAllocNotifyRequests failed"
,
__func__
,
__LINE__
);
goto
ERR_ALLOC_NOTIFY_REQ
;
}
ret
=
UsbAllocReadRequests
(
acm
);
if
(
ret
)
{
goto
err_alloc_read_reqs
;
HDF_LOGE
(
"%s:%d UsbAllocReadRequests failed"
,
__func__
,
__LINE__
);
goto
ERR_ALLOC_READ_REQS
;
}
ret
=
UsbStartIo
(
acm
);
if
(
ret
)
{
goto
err_start_io
;
HDF_LOGE
(
"%s:%d UsbAllocReadRequests failed"
,
__func__
,
__LINE__
);
goto
ERR_START_IO
;
}
acm
->
lineCoding
.
dwDTERate
=
CpuToLe
32
(
DATARATE
);
acm
->
lineCoding
.
bCharFormat
=
CHARFORMAT
;
acm
->
lineCoding
.
dwDTERate
=
CPU_TO_LE
32
(
DATARATE
);
acm
->
lineCoding
.
bCharFormat
=
USB_CDC_1_STOP_BITS
;
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
);
if
(
ret
)
{
goto
err_submit_req
;
HDF_LOGE
(
"%s:%d UsbRawSubmitRequest failed"
,
__func__
,
__LINE__
);
goto
ERR_SUBMIT_REQ
;
}
acm
->
initFlag
=
true
;
HDF_LOGD
(
"%s:%d=========================OK"
,
__func__
,
__LINE__
);
return
HDF_SUCCESS
;
err_submit_req
:
ERR_SUBMIT_REQ
:
UsbStopIo
(
acm
);
// 停止IO线程并释放所有资源
err_start_io
:
ERR_START_IO
:
UsbFreeReadRequests
(
acm
);
err_alloc_read_reqs
:
UsbFreeNotifyReq
ue
st
(
acm
);
err_alloc_notify_req
:
ERR_ALLOC_READ_REQS
:
UsbFreeNotifyReq
eu
st
(
acm
);
ERR_ALLOC_NOTIFY_REQ
:
UsbFreeWriteRequests
(
acm
);
err_alloc_write_reqs
:
ERR_ALLOC_WRITE_REQS
:
AcmWriteBufFree
(
acm
);
err_alloc_write_buf
:
ERR_ALLOC_WRITE_BUF
:
UsbReleaseInterfaces
(
acm
);
err_parse_desc
:
ERR_PARSE_DESC
:
UsbRawFreeConfigDescriptor
(
acm
->
config
);
acm
->
config
=
NULL
;
err_get_desc
:
ERR_GET_DESC
:
(
void
)
UsbRawCloseDevice
(
devHandle
);
// 关闭USB设备对象
err_open_device
:
ERR_OPEN_DEVICE
:
UsbRawExit
(
acm
->
session
);
// 退出USB DDK的专家模式
return
ret
;
}
...
static
void
UsbSerialRelease
(
struct
AcmDevice
*
acm
)
{
if
(
acm
->
initFlag
==
false
)
{
if
(
!
(
acm
->
initFlag
))
{
HDF_LOGE
(
"%s:%d: initFlag is false"
,
__func__
,
__LINE__
);
return
;
}
...
...
@@ -1333,11 +1392,11 @@ static void UsbSerialRelease(struct AcmDevice *acm)
g_syncRequest
=
NULL
;
}
UsbFreeReadRequests
(
acm
);
UsbFreeNotifyReq
ue
st
(
acm
);
UsbFreeNotifyReq
eu
st
(
acm
);
UsbFreeWriteRequests
(
acm
);
AcmWriteBufFree
(
acm
);
(
void
)
UsbRawCloseDevice
(
acm
->
devHandle
);
UsbReleaseInterfaces
(
acm
);
(
void
)
UsbRawCloseDevice
(
acm
->
devHandle
);
UsbRawFreeConfigDescriptor
(
acm
->
config
);
acm
->
config
=
NULL
;
// 退出USB DDK的专家模式
...
...
@@ -1352,12 +1411,16 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
int32_t
ret
;
if
(
device
==
NULL
)
{
HDF_LOGE
(
"%s:%d device is null"
,
__func__
,
__LINE__
);
return
HDF_ERR_INVALID_OBJECT
;
}
acm
=
(
struct
AcmDevice
*
)
device
->
service
;
if
(
acm
==
NULL
)
{
return
HDF_ERR_INVALID_OBJECT
;
}
OsalMutexInit
(
&
acm
->
readLock
);
OsalMutexInit
(
&
acm
->
writeLock
);
// 设备申请连续的内存
ret
=
UsbSerialDeviceAlloc
(
acm
);
if
(
ret
!=
HDF_SUCCESS
)
{
HDF_LOGE
(
"%s:%d UsbSerialDeviceAlloc failed"
,
__func__
,
__LINE__
);
...
...
@@ -1365,6 +1428,7 @@ static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
acm
->
initFlag
=
false
;
g_rawAcmReleaseFlag
=
false
;
HDF_LOGD
(
"%s:%d init ok!"
,
__func__
,
__LINE__
);
return
ret
;
}
...
...
@@ -1372,17 +1436,20 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
{
struct
AcmDevice
*
acm
=
NULL
;
if
(
device
==
NULL
)
{
HDF_LOGE
(
"%s: device is null"
,
__func__
);
return
;
}
acm
=
(
struct
AcmDevice
*
)
device
->
service
;
if
(
acm
==
NULL
)
{
HDF_LOGE
(
"%s: acm is null"
,
__func__
);
return
;
}
g_rawAcmReleaseFlag
=
true
;
if
(
acm
->
initFlag
==
true
)
{
if
(
acm
->
initFlag
)
{
HDF_LOGE
(
"%s:%d UsbSerialRelease"
,
__func__
,
__LINE__
);
UsbSerialRelease
(
acm
);
}
UsbSeriaDevicelFree
(
acm
);
...
...
@@ -1391,14 +1458,15 @@ static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
OsalMutexDestroy
(
&
acm
->
lock
);
OsalMemFree
(
acm
);
acm
=
NULL
;
HDF_LOGD
(
"%s:%d exit"
,
__func__
,
__LINE__
);
}
struct
HdfDriverEntry
g_usbSerialRawDriverEntry
=
{
.
moduleVersion
=
1
,
.
moduleName
=
"usbhost_acm_rawapi"
,
// 驱动模块名称,必须与hcs文件中配置的名称一致
.
Bind
=
UsbSerialDriverBind
,
.
Init
=
UsbSerialDriverInit
,
.
Release
=
UsbSerialDriverRelease
,
.
moduleName
=
"usbhost_acm_rawapi"
,
// 驱动模块名称,必须与hcs文件中配置的名称一致
.
Bind
=
UsbSerialDriverBind
,
.
Init
=
UsbSerialDriverInit
,
.
Release
=
UsbSerialDriverRelease
,
};
HDF_INIT
(
g_usbSerialRawDriverEntry
);
```
...
...
@@ -1412,24 +1480,30 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc
```cpp
static int32_t AcmCreateFuncDevice(struct UsbAcmDevice *acm, struct DeviceResourceIface *iface)
{
int32_t ret;
struct UsbFnDevice *fnDev = NULL;
struct UsbFnDescriptorData descData;
uint8_t useHcs;
...
if (useHcs == 0) { // 描述符来自于代码中编码
descData.type = USBFN_DESC_DATA_TYPE_DESC;
descData.descriptor = &g_masterFuncDevice;
} else { // 描述符来自于解析hcs文件
descData.type = USBFN_DESC_DATA_TYPE_PROP;
descData.property = device->property;
// 读取hcs文件中的udc_name节点的字符串值
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__);
return HDF_FAILURE;
}
/* 创建设备 */
fnDev = (struct UsbFnDevice *)UsbFn
DeviceCreate(acm->udcName, &descData
);
fnDev = (struct UsbFnDevice *)UsbFn
GetDevice(acm->udcName
);
if (fnDev == NULL) {
HDF_LOGE("%s: create usb function device failed", __func__);
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信息
...
...
@@ -1437,28 +1511,69 @@ USB ACM设备核心代码路径为drivers\peripheral\usb\gadget\function\acm\cdc
```cpp
static int32_t AcmParseEachPipe(struct UsbAcmDevice *acm, struct UsbAcmInterface *iface)
{
...
for (i = 0; i < fnIface->info.numPipes; i++) {
struct UsbFnInterface *fnIface = iface->fn;
for (
uint32_t
i = 0; i < fnIface->info.numPipes; i++) {
struct UsbFnPipeInfo pipeInfo;
// pipeInfo清除缓存区内容
(void)memset_s(&pipeInfo, sizeof(pipeInfo), 0, sizeof(pipeInfo));
/* 获取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;
}
/* 获取接口,打开接口获取handle */
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++) {
/* 获取接口 */
fnIface = (struct UsbFnInterface *)UsbFnGetInterface(fnDev, i);
...
/* 打开接口 */
handle = UsbFnInterfaceOpen(fnIface);
...
if (fnIface == NULL) {
HDF_LOGE("%s: get interface failed", __func__);
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
```cpp
static int32_t AcmAllocCtrlRequests(struct UsbAcmDevice *acm, int32_t num)
{
...
req = UsbFnCtrlRequestAlloc(acm->ctrlIface.handle,
sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
...
}
static int32_t AcmDriverInit(struct HdfDeviceObject *device)
{
...
/* 开始接收Event */
ret = UsbFnInterfaceStartRecvEvent(acm->ctrlIface.fn, 0xff, UsbAcmEventCallback, acm);
...
struct DListHead *head = &acm->ctrlPool;
struct UsbFnRequest *req = NULL;
struct CtrlInfo *ctrlInfo = NULL;
int32_t i;
DListHeadInit(&acm->ctrlPool);
acm->ctrlReqNum = 0;
for (i = 0; i < num; i++) {
// 申请内存
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通信(读写等)
```cpp
static int32_t AcmSendNotifyRequest(
struct UsbAcmDevice *acm, uint8_t type,
uint16_t value,
void *data, uint32_t length)
static int32_t AcmSendNotifyRequest(
struct UsbAcmDevice *acm, uint8_t type, uint16_t value, const
void *data, uint32_t length)
{
...
/* 异步发送 */
ret = UsbFnRequestSubmitAsync(req);
...
struct UsbFnRequest *req = acm->notifyReq;
struct UsbCdcNotification *notify = NULL;
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
}
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录