driver-peripherals-usb-des.md 68.6 KB
Newer Older
A
annie_wangli 已提交
1
# USB<a name="EN-US_TOPIC_0000001228574475"></a>
A
annie_wangli 已提交
2

A
annie_wangli 已提交
3 4
-   [Overview](#section127mcpsimp)
    -   [Available APIs](#section141mcpsimp)
A
annie_wangli 已提交
5

A
annie_wangli 已提交
6 7 8 9
-   [Development Guidelines](#section581mcpsimp)
    -   [Developing Driver Using Host DDK APIs](#section584mcpsimp)
    -   [Developing Driver Using Host Raw APIs](#section594mcpsimp)
    -   [Developing Driver Using Device DDK APIs](#section600mcpsimp)
A
annie_wangli 已提交
10

A
annie_wangli 已提交
11 12 13 14
-   [Development Examples](#section607mcpsimp)
    -   [Developing Driver Using Host DDK APIs](#section609mcpsimp)
    -   [Developing Driver Using Host Raw APIs](#section612mcpsimp)
    -   [Developing Driver Using Device DDK APIs](#section615mcpsimp)
A
annie_wangli 已提交
15 16


A
annie_wangli 已提交
17
## Overview<a name="section127mcpsimp"></a>
A
annie_wangli 已提交
18 19 20 21 22 23 24

USB host development aims to provide host-related functions, including protocol encapsulation, device management, and driver installation and uninstall.

USB device development aims to provide device-related functions, including device management, configuration management, and I/O management. These functions implement creation, configuration, and data communication of USB devices.

The following figures show the UBS host and device driver models.

A
annie_wangli 已提交
25
**Figure 1** USB host driver model<a name="fig1649563542917"></a>
A
annie_wangli 已提交
26 27
![](figures/USB_host_driver_model.png "USB host driver model")

A
annie_wangli 已提交
28
**Figure 2** USB device driver model<a name="fig8847615103013"></a>
A
annie_wangli 已提交
29 30 31 32
![](figures/USB_device_driver_model.png "USB device driver model")

The USB driver model offers the following APIs:

A
annie_wangli 已提交
33 34
-   The USB host Driver Development Kit (DDK) provides driver capability APIs that can be directly called in user mode. The APIs can be classified into the DDK initialization class, interface operation class, and request operation class by function. These APIs can be used to perform DDK initialization, bind/release and open/close an interface, allocate/release a request, and implement synchronous or asynchronous transfer.
-   The USB device DDK provides device management, I/O management, and configuration management APIs, which can be used to create and delete a device, obtain/open an interface, and perform synchronous or asynchronous transfer.
A
annie_wangli 已提交
35

A
annie_wangli 已提交
36
### Available APIs(<a name="section141mcpsimp"></a>)
A
annie_wangli 已提交
37

A
annie_wangli 已提交
38
[Figure 1](#fig1649563542917) describes the APIs provided by the USB host driver model.
A
annie_wangli 已提交
39 40 41

**Table 1** APIs provided by the USB host driver model

A
annie_wangli 已提交
42 43
<a name="table11474102882612"></a>
<table><thead align="left"><tr id="row1147413289268"><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.1"><p id="p1966391682713"><a name="p1966391682713"></a><a name="p1966391682713"></a>Header File</p>
A
annie_wangli 已提交
44
</th>
A
annie_wangli 已提交
45
<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.2"><p id="p166313167274"><a name="p166313167274"></a><a name="p166313167274"></a>API</p>
A
annie_wangli 已提交
46
</th>
A
annie_wangli 已提交
47
<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.3"><p id="p1066341602710"><a name="p1066341602710"></a><a name="p1066341602710"></a> Description</p>
A
annie_wangli 已提交
48 49 50
</th>
</tr>
</thead>
A
annie_wangli 已提交
51
<tbody><tr id="row1275799122710"><td class="cellrowborder" rowspan="16" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p4725109162716"><a name="p4725109162716"></a><a name="p4725109162716"></a>usb_ddk_interface.h</p>
A
annie_wangli 已提交
52
</td>
A
annie_wangli 已提交
53
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p17725698274"><a name="p17725698274"></a><a name="p17725698274"></a>int32_t UsbInitHostSdk(struct UsbSession **session);</p>
A
annie_wangli 已提交
54
</td>
A
annie_wangli 已提交
55
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p27252992712"><a name="p27252992712"></a><a name="p27252992712"></a>Initializes the USB host driver DDK.</p>
A
annie_wangli 已提交
56 57
</td>
</tr>
A
annie_wangli 已提交
58
<tr id="row775779172719"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p14725149172712"><a name="p14725149172712"></a><a name="p14725149172712"></a>int32_t UsbExitHostSdk(const struct UsbSession *session);</p>
A
annie_wangli 已提交
59
</td>
A
annie_wangli 已提交
60
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p172549192711"><a name="p172549192711"></a><a name="p172549192711"></a>Exits the USB host driver DDK.</p>
A
annie_wangli 已提交
61 62
</td>
</tr>
A
annie_wangli 已提交
63
<tr id="row975769172712"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p167251791278"><a name="p167251791278"></a><a name="p167251791278"></a>const struct UsbInterface *UsbClaimInterface(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex);</p>
A
annie_wangli 已提交
64
</td>
A
annie_wangli 已提交
65
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p187255962716"><a name="p187255962716"></a><a name="p187255962716"></a>Obtains a USB interface.</p>
A
annie_wangli 已提交
66 67
</td>
</tr>
A
annie_wangli 已提交
68
<tr id="row97577920273"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p16725497276"><a name="p16725497276"></a><a name="p16725497276"></a>int UsbReleaseInterface(const struct UsbInterface *interfaceObj);</p>
A
annie_wangli 已提交
69
</td>
A
annie_wangli 已提交
70
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p117251894270"><a name="p117251894270"></a><a name="p117251894270"></a>Releases a USB interface.</p>
A
annie_wangli 已提交
71 72
</td>
</tr>
A
annie_wangli 已提交
73
<tr id="row67574982718"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p872514915277"><a name="p872514915277"></a><a name="p872514915277"></a>int UsbAddOrRemoveInterface(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr, uint8_t interfaceIndex, UsbInterfaceStatus status);</p>
A
annie_wangli 已提交
74
</td>
A
annie_wangli 已提交
75
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p157251914278"><a name="p157251914278"></a><a name="p157251914278"></a>Adds or removes a USB interface.</p>
A
annie_wangli 已提交
76 77
</td>
</tr>
A
annie_wangli 已提交
78
<tr id="row47576942720"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p572517916275"><a name="p572517916275"></a><a name="p572517916275"></a>UsbInterfaceHandle *UsbOpenInterface(const struct UsbInterface *interfaceObj);</p>
A
annie_wangli 已提交
79
</td>
A
annie_wangli 已提交
80
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p67254915272"><a name="p67254915272"></a><a name="p67254915272"></a>Opens a USB interface.</p>
A
annie_wangli 已提交
81 82
</td>
</tr>
A
annie_wangli 已提交
83
<tr id="row197579952713"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p472514912712"><a name="p472514912712"></a><a name="p472514912712"></a>int32_t UsbCloseInterface(const UsbInterfaceHandle *interfaceHandle);</p>
A
annie_wangli 已提交
84
</td>
A
annie_wangli 已提交
85
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p67251095276"><a name="p67251095276"></a><a name="p67251095276"></a>Closes a USB interface.</p>
A
annie_wangli 已提交
86 87
</td>
</tr>
A
annie_wangli 已提交
88
<tr id="row15757894278"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p5725169192717"><a name="p5725169192717"></a><a name="p5725169192717"></a>int32_t UsbSelectInterfaceSetting(const UsbInterfaceHandle *interfaceHandle, uint8_t settingIndex, struct UsbInterface **interfaceObj);</p>
A
annie_wangli 已提交
89
</td>
A
annie_wangli 已提交
90
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p6725109112720"><a name="p6725109112720"></a><a name="p6725109112720"></a>Sets a USB interface.</p>
A
annie_wangli 已提交
91 92
</td>
</tr>
A
annie_wangli 已提交
93
<tr id="row107579932715"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p11725199182715"><a name="p11725199182715"></a><a name="p11725199182715"></a>int32_t UsbGetPipeInfo(const UsbInterfaceHandle *interfaceHandle, uint8_t settingIndex, uint8_t pipeId, struct UsbPipeInfo *pipeInfo);</p>
A
annie_wangli 已提交
94
</td>
A
annie_wangli 已提交
95
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p107261298272"><a name="p107261298272"></a><a name="p107261298272"></a>Obtains USB pipe information.</p>
A
annie_wangli 已提交
96 97
</td>
</tr>
A
annie_wangli 已提交
98
<tr id="row27577992716"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p8726890275"><a name="p8726890275"></a><a name="p8726890275"></a>int32_t UsbClearInterfaceHalt(const UsbInterfaceHandle *interfaceHandle, uint8_t pipeAddress);</p>
A
annie_wangli 已提交
99
</td>
A
annie_wangli 已提交
100
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p177261797274"><a name="p177261797274"></a><a name="p177261797274"></a>Clears the state of the pipe with the specified index.</p>
A
annie_wangli 已提交
101 102
</td>
</tr>
A
annie_wangli 已提交
103
<tr id="row1757189172714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1726998273"><a name="p1726998273"></a><a name="p1726998273"></a>struct UsbRequest *UsbAllocRequest(const UsbInterfaceHandle *interfaceHandle, int isoPackets, int length);</p>
A
annie_wangli 已提交
104
</td>
A
annie_wangli 已提交
105
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1272617982713"><a name="p1272617982713"></a><a name="p1272617982713"></a>Allocates a request object.</p>
A
annie_wangli 已提交
106 107
</td>
</tr>
A
annie_wangli 已提交
108
<tr id="row07579911279"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p187261911279"><a name="p187261911279"></a><a name="p187261911279"></a>int UsbFreeRequest(const struct UsbRequest *request);</p>
A
annie_wangli 已提交
109
</td>
A
annie_wangli 已提交
110
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p177271592271"><a name="p177271592271"></a><a name="p177271592271"></a>Releases a request object.</p>
A
annie_wangli 已提交
111 112
</td>
</tr>
A
annie_wangli 已提交
113
<tr id="row075759142715"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p3727896271"><a name="p3727896271"></a><a name="p3727896271"></a>int UsbSubmitRequestAsync(const struct UsbRequest *request);</p>
A
annie_wangli 已提交
114
</td>
A
annie_wangli 已提交
115
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p372714942718"><a name="p372714942718"></a><a name="p372714942718"></a>Sends an asynchronous request.</p>
A
annie_wangli 已提交
116 117
</td>
</tr>
A
annie_wangli 已提交
118
<tr id="row1475718919272"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1727698272"><a name="p1727698272"></a><a name="p1727698272"></a>int32_t UsbFillRequest(const struct UsbRequest *request, const UsbInterfaceHandle *interfaceHandle, const struct UsbRequestParams *params);</p>
A
annie_wangli 已提交
119
</td>
A
annie_wangli 已提交
120
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p872713910270"><a name="p872713910270"></a><a name="p872713910270"></a>Fills in a request.</p>
A
annie_wangli 已提交
121 122
</td>
</tr>
A
annie_wangli 已提交
123
<tr id="row117576932710"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p9727291278"><a name="p9727291278"></a><a name="p9727291278"></a>sint UsbCancelRequest(const struct UsbRequest *request);</p>
A
annie_wangli 已提交
124
</td>
A
annie_wangli 已提交
125
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p37271897276"><a name="p37271897276"></a><a name="p37271897276"></a>Cancels an asynchronous request.</p>
A
annie_wangli 已提交
126 127
</td>
</tr>
A
annie_wangli 已提交
128
<tr id="row1475714912715"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p2727698275"><a name="p2727698275"></a><a name="p2727698275"></a>int UsbSubmitRequestSync(const struct UsbRequest *request);</p>
A
annie_wangli 已提交
129
</td>
A
annie_wangli 已提交
130
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p87279914277"><a name="p87279914277"></a><a name="p87279914277"></a>Sends a synchronous request.</p>
A
annie_wangli 已提交
131 132
</td>
</tr>
A
annie_wangli 已提交
133
<tr id="row11756097274"><td class="cellrowborder" rowspan="27" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p12727179132710"><a name="p12727179132710"></a><a name="p12727179132710"></a>usb_raw_api.h</p>
A
annie_wangli 已提交
134
</td>
A
annie_wangli 已提交
135
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p11727391272"><a name="p11727391272"></a><a name="p11727391272"></a>int UsbRawInit(struct UsbSession **session);</p>
A
annie_wangli 已提交
136
</td>
A
annie_wangli 已提交
137
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p1572711919272"><a name="p1572711919272"></a><a name="p1572711919272"></a>Initializes the USB raw APIs.</p>
A
annie_wangli 已提交
138 139
</td>
</tr>
A
annie_wangli 已提交
140
<tr id="row1575629182713"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p372711918279"><a name="p372711918279"></a><a name="p372711918279"></a>int UsbRawExit(const struct UsbSession *session);</p>
A
annie_wangli 已提交
141
</td>
A
annie_wangli 已提交
142
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p372710942717"><a name="p372710942717"></a><a name="p372710942717"></a>Exits the USB raw APIs.</p>
A
annie_wangli 已提交
143 144
</td>
</tr>
A
annie_wangli 已提交
145
<tr id="row5756999270"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p8727291277"><a name="p8727291277"></a><a name="p8727291277"></a>UsbRawHandle *UsbRawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr);</p>
A
annie_wangli 已提交
146
</td>
A
annie_wangli 已提交
147
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1772719972717"><a name="p1772719972717"></a><a name="p1772719972717"></a>Opens a USB device.</p>
A
annie_wangli 已提交
148 149
</td>
</tr>
A
annie_wangli 已提交
150
<tr id="row117561292270"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p772709122715"><a name="p772709122715"></a><a name="p772709122715"></a>int UsbRawCloseDevice(const UsbRawHandle *devHandle);</p>
A
annie_wangli 已提交
151
</td>
A
annie_wangli 已提交
152
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1372715902711"><a name="p1372715902711"></a><a name="p1372715902711"></a>Closes a USB device.</p>
A
annie_wangli 已提交
153 154
</td>
</tr>
A
annie_wangli 已提交
155
<tr id="row775689182714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p197272918272"><a name="p197272918272"></a><a name="p197272918272"></a>int UsbRawSendControlRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbControlRequestData *requestData);</p>
A
annie_wangli 已提交
156
</td>
A
annie_wangli 已提交
157
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p272715932717"><a name="p272715932717"></a><a name="p272715932717"></a>Performs a control transfer synchronously.</p>
A
annie_wangli 已提交
158 159
</td>
</tr>
A
annie_wangli 已提交
160
<tr id="row3756179132714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1727296276"><a name="p1727296276"></a><a name="p1727296276"></a>int UsbRawSendBulkRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData);</p>
A
annie_wangli 已提交
161
</td>
A
annie_wangli 已提交
162
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p872789162713"><a name="p872789162713"></a><a name="p872789162713"></a>Performs a bulk transfer synchronously.</p>
A
annie_wangli 已提交
163 164
</td>
</tr>
A
annie_wangli 已提交
165
<tr id="row27568916276"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1072719992711"><a name="p1072719992711"></a><a name="p1072719992711"></a>int UsbRawSendInterruptRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData);</p>
A
annie_wangli 已提交
166
</td>
A
annie_wangli 已提交
167
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p117271895271"><a name="p117271895271"></a><a name="p117271895271"></a>Performs an interrupt transfer synchronously.</p>
A
annie_wangli 已提交
168 169
</td>
</tr>
A
annie_wangli 已提交
170
<tr id="row14756149112710"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p772711917274"><a name="p772711917274"></a><a name="p772711917274"></a>int UsbRawGetConfigDescriptor(const UsbRawDevice *rawDev, uint8_t configIndex, struct UsbRawConfigDescriptor **config);</p>
A
annie_wangli 已提交
171
</td>
A
annie_wangli 已提交
172
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p472714912710"><a name="p472714912710"></a><a name="p472714912710"></a>Obtains the configuration descriptor of a device.</p>
A
annie_wangli 已提交
173 174
</td>
</tr>
A
annie_wangli 已提交
175
<tr id="row1775614902711"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p37277942718"><a name="p37277942718"></a><a name="p37277942718"></a>void UsbRawFreeConfigDescriptor(const struct UsbRawConfigDescriptor *config);</p>
A
annie_wangli 已提交
176
</td>
A
annie_wangli 已提交
177
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p772759162711"><a name="p772759162711"></a><a name="p772759162711"></a>Releases the memory space of a configuration descriptor.</p>
A
annie_wangli 已提交
178 179
</td>
</tr>
A
annie_wangli 已提交
180
<tr id="row9756119182711"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p3727699274"><a name="p3727699274"></a><a name="p3727699274"></a>int UsbRawGetConfiguration(const UsbRawHandle *devHandle, int *config);</p>
A
annie_wangli 已提交
181
</td>
A
annie_wangli 已提交
182
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p13728109192717"><a name="p13728109192717"></a><a name="p13728109192717"></a>Obtains the configuration in use.</p>
A
annie_wangli 已提交
183 184
</td>
</tr>
A
annie_wangli 已提交
185
<tr id="row37567922714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1872849162718"><a name="p1872849162718"></a><a name="p1872849162718"></a>int UsbRawSetConfiguration(const UsbRawHandle *devHandle, int config);</p>
A
annie_wangli 已提交
186
</td>
A
annie_wangli 已提交
187
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p87280914277"><a name="p87280914277"></a><a name="p87280914277"></a>Sets the configuration in use.</p>
A
annie_wangli 已提交
188 189
</td>
</tr>
A
annie_wangli 已提交
190
<tr id="row1775612982711"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p127283902712"><a name="p127283902712"></a><a name="p127283902712"></a>int UsbRawGetDescriptor(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawDescriptorParam *param, const unsigned char *data);</p>
A
annie_wangli 已提交
191
</td>
A
annie_wangli 已提交
192
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p17728209122710"><a name="p17728209122710"></a><a name="p17728209122710"></a>Obtains descriptor information.</p>
A
annie_wangli 已提交
193 194
</td>
</tr>
A
annie_wangli 已提交
195
<tr id="row97564992719"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1672849122712"><a name="p1672849122712"></a><a name="p1672849122712"></a>UsbRawDevice *UsbRawGetDevice(const UsbRawHandle *devHandle);</p>
A
annie_wangli 已提交
196
</td>
A
annie_wangli 已提交
197
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p472879202719"><a name="p472879202719"></a><a name="p472879202719"></a>Obtains the device pointer based on the device handle.</p>
A
annie_wangli 已提交
198 199
</td>
</tr>
A
annie_wangli 已提交
200
<tr id="row1075612922718"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p172814982715"><a name="p172814982715"></a><a name="p172814982715"></a>int UsbRawGetDeviceDescriptor(const UsbRawDevice *rawDev, struct UsbDeviceDescriptor *desc);</p>
A
annie_wangli 已提交
201
</td>
A
annie_wangli 已提交
202
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p8728139172715"><a name="p8728139172715"></a><a name="p8728139172715"></a>Obtains the device descriptor of the specified USB device.</p>
A
annie_wangli 已提交
203 204
</td>
</tr>
A
annie_wangli 已提交
205
<tr id="row117561919273"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p072820912714"><a name="p072820912714"></a><a name="p072820912714"></a>int UsbRawClaimInterface(const UsbRawHandle *devHandle, int interfaceNumber);</p>
A
annie_wangli 已提交
206
</td>
A
annie_wangli 已提交
207
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p972817914279"><a name="p972817914279"></a><a name="p972817914279"></a>Declares the interface on the specified device handle.</p>
A
annie_wangli 已提交
208 209
</td>
</tr>
A
annie_wangli 已提交
210
<tr id="row87561920275"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p2728191276"><a name="p2728191276"></a><a name="p2728191276"></a>int UsbRawReleaseInterface(const UsbRawHandle *devHandle, int interfaceNumber);</p>
A
annie_wangli 已提交
211
</td>
A
annie_wangli 已提交
212
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p8728159102710"><a name="p8728159102710"></a><a name="p8728159102710"></a>Releases the previously declared interface.</p>
A
annie_wangli 已提交
213 214
</td>
</tr>
A
annie_wangli 已提交
215
<tr id="row375679152710"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p137281192274"><a name="p137281192274"></a><a name="p137281192274"></a>int UsbRawResetDevice(const UsbRawHandle *devHandle);</p>
A
annie_wangli 已提交
216
</td>
A
annie_wangli 已提交
217
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p107281992272"><a name="p107281992272"></a><a name="p107281992272"></a>Resets a device.</p>
A
annie_wangli 已提交
218 219
</td>
</tr>
A
annie_wangli 已提交
220
<tr id="row14756109152719"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p137288914273"><a name="p137288914273"></a><a name="p137288914273"></a>struct UsbRawRequest *UsbRawAllocRequest(const UsbRawHandle *devHandle, int isoPackets, int length);</p>
A
annie_wangli 已提交
221
</td>
A
annie_wangli 已提交
222
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p27281797274"><a name="p27281797274"></a><a name="p27281797274"></a>Allocates a transfer request with the specified number of sync packet descriptors.</p>
A
annie_wangli 已提交
223 224
</td>
</tr>
A
annie_wangli 已提交
225
<tr id="row07560932714"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p2072810912713"><a name="p2072810912713"></a><a name="p2072810912713"></a>int UsbRawFreeRequest(const struct UsbRawRequest *request);</p>
A
annie_wangli 已提交
226
</td>
A
annie_wangli 已提交
227
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p17281917278"><a name="p17281917278"></a><a name="p17281917278"></a>Releases the previously allocated transfer request.</p>
A
annie_wangli 已提交
228 229
</td>
</tr>
A
annie_wangli 已提交
230
<tr id="row1775615952717"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1872810915274"><a name="p1872810915274"></a><a name="p1872810915274"></a>int UsbRawFillBulkRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);</p>
A
annie_wangli 已提交
231
</td>
A
annie_wangli 已提交
232
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p117281090271"><a name="p117281090271"></a><a name="p117281090271"></a>Fills in a bulk transfer request.</p>
A
annie_wangli 已提交
233 234
</td>
</tr>
A
annie_wangli 已提交
235
<tr id="row675659172716"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1272820962719"><a name="p1272820962719"></a><a name="p1272820962719"></a>int UsbRawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData);</p>
A
annie_wangli 已提交
236
</td>
A
annie_wangli 已提交
237
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p177281299278"><a name="p177281299278"></a><a name="p177281299278"></a>Fills in a control setup packet.</p>
A
annie_wangli 已提交
238 239
</td>
</tr>
A
annie_wangli 已提交
240
<tr id="row97563912271"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1372817914277"><a name="p1372817914277"></a><a name="p1372817914277"></a>int UsbRawFillControlRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);</p>
A
annie_wangli 已提交
241
</td>
A
annie_wangli 已提交
242
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1372815952715"><a name="p1372815952715"></a><a name="p1372815952715"></a>Fills in a control transfer request.</p>
A
annie_wangli 已提交
243 244
</td>
</tr>
A
annie_wangli 已提交
245
<tr id="row117561932712"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p172899112714"><a name="p172899112714"></a><a name="p172899112714"></a>int UsbRawFillInterruptRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);</p>
A
annie_wangli 已提交
246
</td>
A
annie_wangli 已提交
247
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p19728092271"><a name="p19728092271"></a><a name="p19728092271"></a>Fills in an interrupt transfer request.</p>
A
annie_wangli 已提交
248 249
</td>
</tr>
A
annie_wangli 已提交
250
<tr id="row075617917271"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p072829132714"><a name="p072829132714"></a><a name="p072829132714"></a>int UsbRawFillIsoRequest(const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData);</p>
A
annie_wangli 已提交
251
</td>
A
annie_wangli 已提交
252
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p107281913275"><a name="p107281913275"></a><a name="p107281913275"></a>Fills in an isochronous transfer request.</p>
A
annie_wangli 已提交
253 254
</td>
</tr>
A
annie_wangli 已提交
255
<tr id="row87564917271"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p16728129152712"><a name="p16728129152712"></a><a name="p16728129152712"></a>int UsbRawSubmitRequest(const struct UsbRawRequest *request);</p>
A
annie_wangli 已提交
256
</td>
A
annie_wangli 已提交
257
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p187299922718"><a name="p187299922718"></a><a name="p187299922718"></a>Submits a transfer request.</p>
A
annie_wangli 已提交
258 259
</td>
</tr>
A
annie_wangli 已提交
260
<tr id="row975659192711"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p15729099278"><a name="p15729099278"></a><a name="p15729099278"></a>int UsbRawCancelRequest(const struct UsbRawRequest *request);</p>
A
annie_wangli 已提交
261
</td>
A
annie_wangli 已提交
262
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p872912911274"><a name="p872912911274"></a><a name="p872912911274"></a>Cancels a transfer request.</p>
A
annie_wangli 已提交
263 264
</td>
</tr>
A
annie_wangli 已提交
265
<tr id="row1675519932712"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1472929182710"><a name="p1472929182710"></a><a name="p1472929182710"></a>int UsbRawHandleRequests(const UsbRawHandle *devHandle);</p>
A
annie_wangli 已提交
266
</td>
A
annie_wangli 已提交
267
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p572929102710"><a name="p572929102710"></a><a name="p572929102710"></a>Handles a transfer request event.</p>
A
annie_wangli 已提交
268 269 270 271 272
</td>
</tr>
</tbody>
</table>

A
annie_wangli 已提交
273
[Figure 2](#fig8847615103013) describes the APIs provided by the USB device driver model.
A
annie_wangli 已提交
274 275 276

**Table 2** APIs provided by the USB device driver model

A
annie_wangli 已提交
277 278
<a name="table1172315391272"></a>
<table><thead align="left"><tr id="row207231239162719"><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.1"><p id="p1472363915270"><a name="p1472363915270"></a><a name="p1472363915270"></a>Header File</p>
A
annie_wangli 已提交
279
</th>
A
annie_wangli 已提交
280
<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.2"><p id="p672313910271"><a name="p672313910271"></a><a name="p672313910271"></a>API</p>
A
annie_wangli 已提交
281
</th>
A
annie_wangli 已提交
282
<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.2.4.1.3"><p id="p524922814284"><a name="p524922814284"></a><a name="p524922814284"></a>Description</p>
A
annie_wangli 已提交
283 284 285
</th>
</tr>
</thead>
A
annie_wangli 已提交
286
<tbody><tr id="row1526165392716"><td class="cellrowborder" rowspan="3" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p143125320275"><a name="p143125320275"></a><a name="p143125320275"></a>usbfn_device.h</p>
A
annie_wangli 已提交
287
</td>
A
annie_wangli 已提交
288
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p1644533274"><a name="p1644533274"></a><a name="p1644533274"></a>const struct UsbFnDevice *UsbFnCreateDevice(const char *udcName, const struct UsbFnDescriptorData *descriptor);</p>
A
annie_wangli 已提交
289
</td>
A
annie_wangli 已提交
290
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p241853152716"><a name="p241853152716"></a><a name="p241853152716"></a>Creates a USB device.</p>
A
annie_wangli 已提交
291 292
</td>
</tr>
A
annie_wangli 已提交
293
<tr id="row11261453112717"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p741553102715"><a name="p741553102715"></a><a name="p741553102715"></a>int UsbFnRemoveDevice(struct UsbFnDevice *fnDevice);</p>
A
annie_wangli 已提交
294
</td>
A
annie_wangli 已提交
295
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p5435318271"><a name="p5435318271"></a><a name="p5435318271"></a>Deletes a USB device.</p>
A
annie_wangli 已提交
296 297
</td>
</tr>
A
annie_wangli 已提交
298
<tr id="row126205320278"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p9425362716"><a name="p9425362716"></a><a name="p9425362716"></a>const struct UsbFnDevice *UsbFnGetDevice(const char *udcName);</p>
A
annie_wangli 已提交
299
</td>
A
annie_wangli 已提交
300
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p54135392719"><a name="p54135392719"></a><a name="p54135392719"></a>Obtains a USB device.</p>
A
annie_wangli 已提交
301 302
</td>
</tr>
A
annie_wangli 已提交
303
<tr id="row326125314279"><td class="cellrowborder" rowspan="6" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p144653142719"><a name="p144653142719"></a><a name="p144653142719"></a>usbfn_interface.h</p>
A
annie_wangli 已提交
304
</td>
A
annie_wangli 已提交
305
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p941453112713"><a name="p941453112713"></a><a name="p941453112713"></a>int UsbFnStartRecvInterfaceEvent(struct UsbFnInterface *interface, uint32_t eventMask, UsbFnEventCallback callback, void *context);</p>
A
annie_wangli 已提交
306
</td>
A
annie_wangli 已提交
307
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p1341530277"><a name="p1341530277"></a><a name="p1341530277"></a>Starts receiving events.</p>
A
annie_wangli 已提交
308 309
</td>
</tr>
A
annie_wangli 已提交
310
<tr id="row172613534273"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p124153152713"><a name="p124153152713"></a><a name="p124153152713"></a>int UsbFnStopRecvInterfaceEvent(struct UsbFnInterface *interface);</p>
A
annie_wangli 已提交
311
</td>
A
annie_wangli 已提交
312
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1441532271"><a name="p1441532271"></a><a name="p1441532271"></a>Stops receiving events.</p>
A
annie_wangli 已提交
313 314
</td>
</tr>
A
annie_wangli 已提交
315
<tr id="row1026653142717"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p24185317276"><a name="p24185317276"></a><a name="p24185317276"></a>UsbFnInterfaceHandle UsbFnOpenInterface(struct UsbFnInterface *interface);</p>
A
annie_wangli 已提交
316
</td>
A
annie_wangli 已提交
317
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p142537274"><a name="p142537274"></a><a name="p142537274"></a>Opens an interface.</p>
A
annie_wangli 已提交
318 319
</td>
</tr>
A
annie_wangli 已提交
320
<tr id="row226195362720"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p114175382715"><a name="p114175382715"></a><a name="p114175382715"></a>int UsbFnCloseInterface(UsbFnInterfaceHandle handle);</p>
A
annie_wangli 已提交
321
</td>
A
annie_wangli 已提交
322
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p124453132715"><a name="p124453132715"></a><a name="p124453132715"></a>Closes an interface.</p>
A
annie_wangli 已提交
323 324
</td>
</tr>
A
annie_wangli 已提交
325
<tr id="row12605311277"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p3565313278"><a name="p3565313278"></a><a name="p3565313278"></a>int UsbFnGetInterfacePipeInfo(struct UsbFnInterface *interface, uint8_t pipeId, struct UsbFnPipeInfo *info);</p>
A
annie_wangli 已提交
326
</td>
A
annie_wangli 已提交
327
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p054536274"><a name="p054536274"></a><a name="p054536274"></a>Obtains pipe information.</p>
A
annie_wangli 已提交
328 329
</td>
</tr>
A
annie_wangli 已提交
330
<tr id="row19261153132716"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1958532275"><a name="p1958532275"></a><a name="p1958532275"></a>int UsbFnSetInterfaceProp(const struct UsbFnInterface *interface, const char *name, const char *value);</p>
A
annie_wangli 已提交
331
</td>
A
annie_wangli 已提交
332
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p9516538277"><a name="p9516538277"></a><a name="p9516538277"></a>Sets custom properties.</p>
A
annie_wangli 已提交
333 334
</td>
</tr>
A
annie_wangli 已提交
335
<tr id="row3261853202716"><td class="cellrowborder" rowspan="8" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.1 "><p id="p65453192711"><a name="p65453192711"></a><a name="p65453192711"></a>usbfn_request.h</p>
A
annie_wangli 已提交
336
</td>
A
annie_wangli 已提交
337
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.2 "><p id="p3520539276"><a name="p3520539276"></a><a name="p3520539276"></a>struct UsbFnRequest *UsbFnAllocCtrlRequest(UsbFnInterfaceHandle handle, uint32_t len);</p>
A
annie_wangli 已提交
338
</td>
A
annie_wangli 已提交
339
<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.2.4.1.3 "><p id="p5514538278"><a name="p5514538278"></a><a name="p5514538278"></a>Applies for a control transfer request.</p>
A
annie_wangli 已提交
340 341
</td>
</tr>
A
annie_wangli 已提交
342
<tr id="row18261253112716"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p85115312277"><a name="p85115312277"></a><a name="p85115312277"></a>struct UsbFnRequest *UsbFnAllocRequest(UsbFnInterfaceHandle handle, uint8_t pipe, uint32_t len);</p>
A
annie_wangli 已提交
343
</td>
A
annie_wangli 已提交
344
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p105105342716"><a name="p105105342716"></a><a name="p105105342716"></a>Applies for a data request.</p>
A
annie_wangli 已提交
345 346
</td>
</tr>
A
annie_wangli 已提交
347
<tr id="row82665320272"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1461538279"><a name="p1461538279"></a><a name="p1461538279"></a>int UsbFnFreeRequest(struct UsbFnRequest *req);</p>
A
annie_wangli 已提交
348
</td>
A
annie_wangli 已提交
349
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p26165372718"><a name="p26165372718"></a><a name="p26165372718"></a>Releases a request.</p>
A
annie_wangli 已提交
350 351
</td>
</tr>
A
annie_wangli 已提交
352
<tr id="row162610537275"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p261653192714"><a name="p261653192714"></a><a name="p261653192714"></a>int UsbFnSubmitRequestAsync(struct UsbFnRequest *req);</p>
A
annie_wangli 已提交
353
</td>
A
annie_wangli 已提交
354
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p361253112718"><a name="p361253112718"></a><a name="p361253112718"></a>Sends an asynchronous request.</p>
A
annie_wangli 已提交
355 356
</td>
</tr>
A
annie_wangli 已提交
357
<tr id="row1326253122711"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p4611536275"><a name="p4611536275"></a><a name="p4611536275"></a>int UsbFnSubmitRequestSync(struct UsbFnRequest *req, uint32_t timeout);</p>
A
annie_wangli 已提交
358
</td>
A
annie_wangli 已提交
359
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p06853112710"><a name="p06853112710"></a><a name="p06853112710"></a>Sends a synchronous request.</p>
A
annie_wangli 已提交
360 361
</td>
</tr>
A
annie_wangli 已提交
362
<tr id="row15265539272"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p177453102712"><a name="p177453102712"></a><a name="p177453102712"></a>int UsbFnCancelRequest(struct UsbFnRequest *req);</p>
A
annie_wangli 已提交
363
</td>
A
annie_wangli 已提交
364
<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p13725312275"><a name="p13725312275"></a><a name="p13725312275"></a>Cancels a request.</p>
A
annie_wangli 已提交
365 366 367 368 369
</td>
</tr>
</tbody>
</table>

A
annie_wangli 已提交
370
## Development Guidelines<a name="section581mcpsimp"></a>
A
annie_wangli 已提交
371 372 373

The USB driver is developed based on the Hardware Driver Foundation (HDF), platform, and Operating System Abstraction Layer (OSAL) APIs. A unified driver model is provided for USB devices, irrespective of the operating system and chip architecture. This document uses a serial port as an example to describe how to develop drivers for the USB host and USB device.

A
annie_wangli 已提交
374
### How to Develop<a name="section583mcpsimp"></a>
A
annie_wangli 已提交
375

A
annie_wangli 已提交
376
### Developing Driver Using Host DDK APIs<a name="section584mcpsimp"></a>
A
annie_wangli 已提交
377 378 379 380 381 382 383 384 385 386

1.  Configure the driver mapping table.
2.  Initialize the USB host DDK.
3.  Obtain a **UsbInterface** object.
4.  Open the **UsbInterface** object to obtain the **UsbInterfaceHandle** object.
5.  Obtain pipe information of the specified **pipeIndex** based on the **UsbInterfaceHandle** object.
6.  Allocate an I/O request for the **UsbInterfaceHandle** object.
7.  Fill in the I/O request based on the input parameters.
8.  Submit the I/O request in synchronous or asynchronous mode.

A
annie_wangli 已提交
387
### Developing Driver Using Host Raw APIs<a name="section594mcpsimp"></a>
A
annie_wangli 已提交
388 389 390 391

1.  Configure the driver mapping table.
2.  Initialize the host raw data, open the USB device, obtain the descriptor, and then obtain interface and endpoint information based on the descriptor.
3.  Allocate a request and fill in the request based on the transfer type.
A
annie_wangli 已提交
392
4.  Submit the I/O request in synchronous or asynchronous mode.
A
annie_wangli 已提交
393

A
annie_wangli 已提交
394
### Developing Driver Using Device DDK APIs<a name="section600mcpsimp"></a>
A
annie_wangli 已提交
395 396 397 398 399 400 401

1.  Construct a descriptor.
2.  Instantiate a USB device using the descriptor constructed.
3.  Call **UsbFnDeviceGetInterface** to obtain an interface, call **UsbFnInterfaceGetPipeInfo** to obtain pipe information based on the interface, call **UsbFnInterfaceOpen** to open the interface to obtain the handle, and call **UsbFnRequestAlloc** to obtain the request based on the handle and pipe ID.
4.  Call **UsbFnInterfaceStartRecvEvent** to receive events such as Enable and Setup, and respond to the events in **UsbFnEventCallback**.
5.  Send and receive data in synchronous or asynchronous mode.

A
annie_wangli 已提交
402
## Development Examples<a name="section607mcpsimp"></a>
A
annie_wangli 已提交
403

A
annie_wangli 已提交
404
The following examples help you better understand the development of the USB serial port driver.
A
annie_wangli 已提交
405

A
annie_wangli 已提交
406
### Developing Driver Using Host DDK APIs<a name="section609mcpsimp"></a>
A
annie_wangli 已提交
407 408 409 410 411 412 413 414 415

```
root {
    module = "usb_pnp_device";
    usb_pnp_config {
        match_attr = "usb_pnp_match";
        usb_pnp_device_id = "UsbPnpDeviceId";
        UsbPnpDeviceId {
            idTableList = [
A
annie_wangli 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
                "host_acm_table"
            ];
            host_acm_table {
                // Driver module name, which must be the same as the value of moduleName in the driver entry structure.
                moduleName = "usbhost_acm";
                // Service name of the driver, which must be unique.
                serviceName = "usbhost_acm_pnp_service";
                // Keyword for matching private driver data.
                deviceMatchAttr = "usbhost_acm_pnp_matchAttr";
                // Data length starting from this field, in bytes.
                length = 21;
                // USB driver matching rule: vendorId+productId+interfaceSubClass+interfaceProtocol+interfaceNumber.
                matchFlag = 0x0303;
                // Vendor ID.
                vendorId = 0x12D1;
                // Product ID.
                productId = 0x5000;
                // The least significant 16 bits of the device sequence number.
                bcdDeviceLow = 0x0000;
                // The most significant 16 bits of the device sequence number.
                 bcdDeviceHigh = 0x0000;
                // Device class code allocated by the USB.
                deviceClass = 0;
                // Child class code allocated by the USB.
                deviceSubClass = 0;
                // Device protocol code allocated by the USB.
                deviceProtocol = 0;
                // Interface type. You can enter multiple types as needed.
                interfaceClass = [0];
                // Interface subtype. You can enter multiple subtypes as needed.
                interfaceSubClass = [2, 0];
                // Protocol that the interface complies with. You can enter multiple protocols as needed.
                interfaceProtocol = [1, 2];
                // Interface number. You can enter multiple interface numbers as needed.
                interfaceNumber = [2, 3];
            }
A
annie_wangli 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        }
    }
}

#include "usb_serial.h"
#include "hdf_base.h"
#include "hdf_log.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;
static struct UsbRequest *g_ctrlCmdRequest = NULL;
static bool g_acmReleaseFlag = false;
static uint8_t *g_acmReadBuffer = NULL;
...
static int SerialCtrlMsg(struct AcmDevice *acm, uint8_t request,
    uint16_t value, void *buf, uint16_t len)
{
    int ret;
    uint16_t index = acm->intPipe->interfaceId;
A
annie_wangli 已提交
478 479
    struct UsbControlParams controlParams;
    struct UsbRequestParams params;
A
annie_wangli 已提交
480
    if (acm == NULL || buf == NULL) {
A
annie_wangli 已提交
481
        HDF_LOGE("%s:invalid param", __func__);
A
annie_wangli 已提交
482 483 484 485 486
        return HDF_ERR_IO;
    }
    if (acm->ctrlReq == NULL) {
        acm->ctrlReq = UsbAllocRequest(acm->ctrDevHandle, 0, len);
        if (acm->ctrlReq == NULL) {
A
annie_wangli 已提交
487
            HDF_LOGE("%s: UsbAllocRequest failed", __func__);
A
annie_wangli 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500
            return HDF_ERR_IO;
        }
    }

    controlParams.request = request;
    controlParams.target = USB_REQUEST_TARGET_INTERFACE;
    controlParams.reqType = USB_REQUEST_TYPE_CLASS;
    controlParams.directon = USB_REQUEST_DIR_TO_DEVICE;
    controlParams.value = value;
    controlParams.index = index;
    controlParams.data = buf;
    controlParams.size = len;

A
annie_wangli 已提交
501 502 503 504 505 506 507
    params.interfaceId = USB_CTRL_INTERFACE_ID;
    params.pipeAddress = acm->ctrPipe->pipeAddress;
    params.pipeId = acm->ctrPipe->pipeId;
    params.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
    params.timeout = USB_CTRL_SET_TIMEOUT;
    params.ctrlReq = UsbControlSetUp(&controlParams);
    ret = UsbFillRequest(acm->ctrlReq, acm->ctrDevHandle, &params);
A
annie_wangli 已提交
508
    if (HDF_SUCCESS != ret) {
A
annie_wangli 已提交
509
        HDF_LOGE("%s: failed, ret=%d ", __func__, ret);
A
annie_wangli 已提交
510 511
        return ret;
    }
A
annie_wangli 已提交
512
    ret = UsbSubmitRequestSync(acm->ctrlReq);    // Send an I/O request synchronously.
A
annie_wangli 已提交
513
    if (HDF_SUCCESS != ret) {
A
annie_wangli 已提交
514
        HDF_LOGE("UsbSubmitRequestSync failed, ret=%d ", ret);
A
annie_wangli 已提交
515 516 517
        return ret;
    }
    if (!acm->ctrlReq->compInfo.status) {
A
annie_wangli 已提交
518
        HDF_LOGE("%s status=%d ", __func__, acm->ctrlReq->compInfo.status);
A
annie_wangli 已提交
519 520 521 522 523 524 525 526
    }
    return HDF_SUCCESS;
}
...
static struct UsbInterface *GetUsbInterfaceById(const struct AcmDevice *acm,
    uint8_t interfaceIndex)
{
    struct UsbInterface *tmpIf = NULL;
A
annie_wangli 已提交
527 528
    tmpIf = (struct UsbInterface *)UsbClaimInterface(acm->session, acm->busNum, 
            acm->devAddr, interfaceIndex); // Obtain the UsbInterface object.
A
annie_wangli 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    return tmpIf;
}
...
static struct UsbPipeInfo *EnumePipe(const struct AcmDevice *acm,
    uint8_t interfaceIndex, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
{
    uint8_t i;
    int ret;
    struct UsbInterfaceInfo *info = NULL;
    UsbInterfaceHandle *interfaceHandle = NULL;
    if (pipeType == USB_PIPE_TYPE_CONTROL)
    {
        info = &acm->ctrIface->info;
        interfaceHandle = acm->ctrDevHandle;
    }
    else
    {
        info = &acm->iface[interfaceIndex]->info;
        interfaceHandle = InterfaceIdToHandle(acm, info->interfaceIndex);
    }

    for (i = 0;  i <= info->pipeNum; i++) {
        struct UsbPipeInfo p;
        ret = UsbGetPipeInfo(interfaceHandle, info->curAltSetting, i, &p);// Obtain information about the pipe with index i.
        if (ret < 0) {
            continue;
        }
        if ((p.pipeDirection == pipeDirection) && (p.pipeType == pipeType)) {
            struct UsbPipeInfo *pi = OsalMemCalloc(sizeof(*pi));
            if (pi == NULL) {
A
annie_wangli 已提交
559
                HDF_LOGE("%s: Alloc pipe failed", __func__);
A
annie_wangli 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
                return NULL;
            }
            p.interfaceId = info->interfaceIndex;
            *pi = p;
            return pi;
        }
    }
    return NULL;
}

static struct UsbPipeInfo *GetPipe(const struct AcmDevice *acm,
    UsbPipeType pipeType, UsbPipeDirection pipeDirection)
{
    uint8_t i;
    if (acm == NULL) {
A
annie_wangli 已提交
575
        HDF_LOGE("%s: invalid params", __func__);
A
annie_wangli 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
        return NULL;
    }
    for (i = 0; i < acm->interfaceCnt; i++) {
        struct UsbPipeInfo *p = NULL;
        if (!acm->iface[i]) {
            continue;
        }
        p = EnumePipe(acm, i, pipeType, pipeDirection);
        if (p == NULL) {
            continue;
        }
        return p;
    }
    return NULL;
}

/* HdfDriverEntry implementations */
static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
{
    struct UsbPnpNotifyServiceInfo *info = NULL;
    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;
    }
    if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) {
A
annie_wangli 已提交
608
        HDF_LOGE("%s:%d OsalMutexInit failed", __func__, __LINE__);
A
annie_wangli 已提交
609 610 611 612
        goto error;
    }
    info = (struct UsbPnpNotifyServiceInfo *)device->priv;
    if (info != NULL) {
A
annie_wangli 已提交
613
        HDF_LOGD("%s:%d busNum=%d,devAddr=%d,interfaceLength=%d", 
A
annie_wangli 已提交
614 615 616 617 618 619 620
            __func__, __LINE__, info->busNum, info->devNum, info->interfaceLength);
        acm->busNum = info->busNum;
        acm->devAddr = info->devNum;
        acm->interfaceCnt = info->interfaceLength;
        err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES,
              (const void*)info->interfaceNumber, info->interfaceLength);
        if (err != EOK) {
A
annie_wangli 已提交
621
            HDF_LOGE("%s:%d memcpy_s failed err=%d",
A
annie_wangli 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
                __func__, __LINE__, err);
            goto lock_error;
        }
    } else {
        HDF_LOGE("%s:%d info is NULL!", __func__, __LINE__);
        goto lock_error;
    }
    acm->device  = device;
    device->service = &(acm->service);
    acm->device->service->Dispatch = UsbSerialDeviceDispatch;
    HDF_LOGD("UsbSerialDriverBind=========================OK");
    return HDF_SUCCESS;

lock_error:
    if (OsalMutexDestroy(&acm->lock)) {
A
annie_wangli 已提交
637
        HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__);
A
annie_wangli 已提交
638 639 640 641 642 643 644 645 646 647
    }
error:
    OsalMemFree(acm);
    acm = NULL;
    return HDF_FAILURE;
}
...
static int AcmAllocReadRequests(struct AcmDevice *acm)
{
    int ret;
A
annie_wangli 已提交
648
    struct UsbRequestParams readParams;
A
annie_wangli 已提交
649
    for (int i = 0; i < ACM_NR; i++) {
A
annie_wangli 已提交
650
        acm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize);    // Allocate the readReq I/O request to be sent.
A
annie_wangli 已提交
651
        if (!acm->readReq[i]) {
A
annie_wangli 已提交
652
            HDF_LOGE("readReq request failed");
A
annie_wangli 已提交
653 654
            goto error;
        }
A
annie_wangli 已提交
655 656 657 658 659 660 661 662 663 664 665
        readParams.userData = (void *)acm;
        readParams.pipeAddress = acm->dataInPipe->pipeAddress;
        readParams.pipeId = acm->dataInPipe->pipeId;
        readParams.interfaceId = acm->dataInPipe->interfaceId;
        readParams.callback = AcmReadBulk;
        readParams.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
        readParams.timeout = USB_CTRL_SET_TIMEOUT;
        readParams.dataReq.numIsoPackets = 0;
        readParams.dataReq.directon = (acm->dataInPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & 0x1;
        readParams.dataReq.length = acm->readSize;
        ret = UsbFillRequest(acm->readReq[i], InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParams);    // Fills in the readReq object to be sent.
A
annie_wangli 已提交
666
        if (HDF_SUCCESS != ret) {
A
annie_wangli 已提交
667
            HDF_LOGE("%s: UsbFillRequest failed, ret=%d n", __func__, ret);
A
annie_wangli 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680
            goto error;
        }
    }
    return HDF_SUCCESS;

error:
    AcmFreeReadRequests(acm);
    return HDF_ERR_MALLOC_FAIL;
}

static int AcmAllocNotifyRequest(struct AcmDevice *acm)
{
    int ret;
A
annie_wangli 已提交
681 682
    struct UsbRequestParams intParams = {};
    acm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(acm, acm->intPipe->interfaceId), 0, acm->intSize);    // Allocate the interrupt I/O request object to be sent.
A
annie_wangli 已提交
683
    if (!acm->notifyReq) {
A
annie_wangli 已提交
684
        HDF_LOGE("notifyReq request failed");
A
annie_wangli 已提交
685 686
        return HDF_ERR_MALLOC_FAIL;
    }
A
annie_wangli 已提交
687 688 689 690 691 692 693 694 695 696 697
    intParams.userData = (void *)acm;
    intParams.pipeAddress = acm->intPipe->pipeAddress;
    intParams.pipeId = acm->intPipe->pipeId;
    intParams.interfaceId = acm->intPipe->interfaceId;
    intParams.callback = AcmCtrlIrq;
    intParams.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
    intParams.timeout = USB_CTRL_SET_TIMEOUT;
    intParams.dataReq.numIsoPackets = 0;
    intParams.dataReq.directon = (acm->intPipe->pipeDirection >> USB_PIPE_DIR_OFFSET) & DIRECTION_MASK;
    intParams.dataReq.length = acm->intSize;
    ret = UsbFillRequest(acm->notifyReq, InterfaceIdToHandle(acm, acm->intPipe->interfaceId), &intParams);    // Fill in the interrupt I/O request.
A
annie_wangli 已提交
698
    if (HDF_SUCCESS != ret) {
A
annie_wangli 已提交
699
        HDF_LOGE("%s: UsbFillRequest failed, ret=%d n", __func__, ret);
A
annie_wangli 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
        goto error;
    }
    return HDF_SUCCESS;

error:
    AcmFreeNotifyReqeust(acm);
    return ret;
}

static void AcmReleaseInterfaces(struct AcmDevice *acm)
{
    for (int i = 0; i < acm->interfaceCnt; i++) {
        if (acm->iface[i]) {
            UsbReleaseInterface(acm->iface[i]);
            acm->iface[i] = NULL;
        }
    }
    if (acm->ctrIface) {
        UsbReleaseInterface(acm->ctrIface);
        acm->ctrIface = NULL;
    }
}

static int32_t AcmClaimInterfaces(struct AcmDevice *acm)
{
    for (int i = 0; i < acm->interfaceCnt; i++) {
A
annie_wangli 已提交
726
        acm->iface[i] = GetUsbInterfaceById((const struct AcmDevice *)acm, acm->interfaceIndex[i]);    // Obtain the UsbInterface object.
A
annie_wangli 已提交
727
        if (acm->iface[i] == NULL) {
A
annie_wangli 已提交
728
            HDF_LOGE("%s: interface%d is null", __func__, acm->interfaceIndex[i]);
A
annie_wangli 已提交
729 730 731 732
            goto error;
        }
    }

A
annie_wangli 已提交
733
    acm->ctrIface = GetUsbInterfaceById((const struct AcmDevice *)acm, USB_CTRL_INTERFACE_ID);     // Obtain the UsbInterface object corresponding to the control interface.
A
annie_wangli 已提交
734
    if (acm->ctrIface == NULL) {
A
annie_wangli 已提交
735
        HDF_LOGE("%s: GetUsbInterfaceById null", __func__);
A
annie_wangli 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
        goto error;
    }

    return HDF_SUCCESS;

 error:
    AcmReleaseInterfaces(acm);
    return HDF_FAILURE;
}

static void AcmCloseInterfaces(struct AcmDevice *acm)
{
    for (int i = 0; i < acm->interfaceCnt; i++) {
        if (acm->devHandle[i]) {
            UsbCloseInterface(acm->devHandle[i]);
            acm->devHandle[i] = NULL;
        }
    }
    if (acm->ctrDevHandle) {
        UsbCloseInterface(acm->ctrDevHandle);
        acm->ctrDevHandle = NULL;
    }
}

static int32_t AcmOpenInterfaces(struct AcmDevice *acm)
{
    for (int i = 0; i < acm->interfaceCnt; i++) {
        if (acm->iface[i]) {
A
annie_wangli 已提交
764
            acm->devHandle[i] = UsbOpenInterface(acm->iface[i]);    // Open the UsbInterface object obtained.
A
annie_wangli 已提交
765
            if (acm->devHandle[i] == NULL) {
A
annie_wangli 已提交
766
                HDF_LOGE("%s: UsbOpenInterface null", __func__);
A
annie_wangli 已提交
767 768 769 770 771 772
                goto error;
            }
        }
    }
    acm->ctrDevHandle = UsbOpenInterface(acm->ctrIface);
    if (acm->ctrDevHandle == NULL) {
A
annie_wangli 已提交
773
        HDF_LOGE("%s: ctrDevHandle UsbOpenInterface null", __func__);
A
annie_wangli 已提交
774 775 776 777 778 779 780 781 782 783 784 785
        goto error;
    }

    return HDF_SUCCESS;

error:
    AcmCloseInterfaces(acm);
    return HDF_FAILURE;
}

static int32_t AcmGetPipes(struct AcmDevice *acm)
{
A
annie_wangli 已提交
786
    acm->dataInPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN);// Obtain pipe information of dataInPipe.
A
annie_wangli 已提交
787 788 789 790 791 792 793 794 795 796 797
    if (acm->dataInPipe == NULL) {
        HDF_LOGE("dataInPipe is NULL");
        goto error;
    }

    acm->dataOutPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_OUT); // Obtain pipe information of dataOutPipe.
    if (acm->dataOutPipe == NULL) {
        HDF_LOGE("dataOutPipe is NULL");
        goto error;
    }

A
annie_wangli 已提交
798
    acm->ctrPipe = EnumePipe(acm, acm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT);    // Obtain pipe information of the control pipe.
A
annie_wangli 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    if (acm->ctrPipe == NULL) {
        HDF_LOGE("ctrPipe is NULL");
        goto error;
    }

    acm->intPipe = GetPipe(acm, USB_PIPE_TYPE_INTERRUPT, USB_PIPE_DIRECTION_IN); // Obtain pipe information of the interrupt pipe.
    if (acm->intPipe == NULL) {
        HDF_LOGE("intPipe is NULL");
        goto error;
    }

    acm->readSize  = acm->dataInPipe->maxPacketSize;
    acm->writeSize = acm->dataOutPipe->maxPacketSize;
    acm->ctrlSize  = acm->ctrPipe->maxPacketSize;
    acm->intSize   = acm->intPipe->maxPacketSize;

    return HDF_SUCCESS;

error:
    AcmFreePipes(acm);
    return HDF_FAILURE;
}

static void AcmFreeRequests(struct AcmDevice *acm)
{
    if (g_syncRequest != NULL) {
        UsbFreeRequest(g_syncRequest);
        g_syncRequest = NULL;
    }
    AcmFreeReadRequests(acm);
    AcmFreeNotifyReqeust(acm);
    AcmFreeWriteRequests(acm);
    AcmWriteBufFree(acm);
}

static int32_t AcmAllocRequests(struct AcmDevice *acm)
{
    int32_t ret;

    if (AcmWriteBufAlloc(acm) < 0) {
A
annie_wangli 已提交
839
        HDF_LOGE("%s: AcmWriteBufAlloc failed", __func__);
A
annie_wangli 已提交
840 841 842 843 844
        return HDF_ERR_MALLOC_FAIL;
    }

    for (int i = 0; i < ACM_NW; i++) {
        struct AcmWb *snd = &(acm->wb[i]);
A
annie_wangli 已提交
845
        snd->request = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataOutPipe->interfaceId), 0, acm->writeSize);    // Allocate the I/O request object to be sent.
A
annie_wangli 已提交
846 847
        snd->instance = acm;
        if (snd->request == NULL) {
A
annie_wangli 已提交
848
            HDF_LOGE("%s:%d snd request failed", __func__, __LINE__);
A
annie_wangli 已提交
849 850 851 852
            goto error_alloc_write_req;
        }
    }

A
annie_wangli 已提交
853
    ret = AcmAllocNotifyRequest(acm); // Allocate and fill in the interrupt I/O request object.
A
annie_wangli 已提交
854
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
855
        HDF_LOGE("%s:%d AcmAllocNotifyRequest failed", __func__, __LINE__);
A
annie_wangli 已提交
856 857 858
        goto error_alloc_int_req;
    }

A
annie_wangli 已提交
859
    ret = AcmAllocReadRequests(acm);    // Allocate and fill in the readReq I/O request object.
A
annie_wangli 已提交
860
    if (ret) {
A
annie_wangli 已提交
861
        HDF_LOGE("%s:%d AcmAllocReadRequests failed", __func__, __LINE__);
A
annie_wangli 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
        goto error_alloc_read_req;
    }

    return HDF_SUCCESS;

error_alloc_read_req:
    AcmFreeNotifyReqeust(acm);
error_alloc_int_req:
    AcmFreeWriteRequests(acm);
error_alloc_write_req:
    AcmWriteBufFree(acm);
    return HDF_FAILURE;
}

static int32_t AcmInit(struct AcmDevice *acm)
{
    int32_t ret;
    struct UsbSession *session = NULL;

    if (acm->initFlag == true) {
A
annie_wangli 已提交
882
        HDF_LOGE("%s:%d: initFlag is true", __func__, __LINE__);
A
annie_wangli 已提交
883 884 885
        return HDF_SUCCESS;
    }

A
annie_wangli 已提交
886
    ret = UsbInitHostSdk(NULL); // Initialize the Host DDK.
A
annie_wangli 已提交
887
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
888
        HDF_LOGE("%s: UsbInitHostSdk failed", __func__);
A
annie_wangli 已提交
889 890 891 892 893 894
        return HDF_ERR_IO;
    }
    acm->session = session;

    ret = AcmClaimInterfaces(acm);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
895
        HDF_LOGE("%s: AcmClaimInterfaces failed", __func__);
A
annie_wangli 已提交
896 897 898 899 900
        goto error_claim_interfaces;
    }

    ret = AcmOpenInterfaces(acm);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
901
        HDF_LOGE("%s: AcmOpenInterfaces failed", __func__);
A
annie_wangli 已提交
902 903 904 905 906
        goto error_open_interfaces;
    }

    ret = AcmGetPipes(acm);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
907
        HDF_LOGE("%s: AcmGetPipes failed", __func__);
A
annie_wangli 已提交
908 909 910 911 912
        goto error_get_pipes;
    }

    ret = AcmAllocRequests(acm);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
913
        HDF_LOGE("%s: AcmAllocRequests failed", __func__);
A
annie_wangli 已提交
914 915 916 917 918 919 920 921 922
        goto error_alloc_reqs;
    }

    acm->lineCoding.dwDTERate = CpuToLe32(DATARATE);
    acm->lineCoding.bCharFormat = CHARFORMAT;
    acm->lineCoding.bParityType = USB_CDC_NO_PARITY;
    acm->lineCoding.bDataBits = USB_CDC_1_STOP_BITS;
    acm->initFlag = true;

A
annie_wangli 已提交
923
    HDF_LOGD("%s:%d========OK", __func__, __LINE__);
A
annie_wangli 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
    return HDF_SUCCESS;

error_alloc_reqs:
    AcmFreePipes(acm);
error_get_pipes:
    AcmCloseInterfaces(acm);
error_open_interfaces:
    AcmReleaseInterfaces(acm);
error_claim_interfaces:
    UsbExitHostSdk(acm->session);
    acm->session = NULL;
    return ret;
}

static void AcmRelease(struct AcmDevice *acm)
{
    if (acm->initFlag == false) {
A
annie_wangli 已提交
941
        HDF_LOGE("%s:%d: initFlag is false", __func__, __LINE__);
A
annie_wangli 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
        return;
    }

    AcmFreeRequests(acm);
    AcmFreePipes(acm);
    AcmCloseInterfaces(acm);
    AcmReleaseInterfaces(acm);
    UsbExitHostSdk(acm->session);
    acm->session = NULL;

    acm->initFlag = false;
}

static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
{
    int32_t ret;
    struct AcmDevice *acm = NULL;

    if (device == NULL) {
A
annie_wangli 已提交
961
        HDF_LOGE("%s: device is null", __func__);
A
annie_wangli 已提交
962 963 964 965 966
        return HDF_ERR_INVALID_OBJECT;
    }
    acm = (struct AcmDevice *)device->service;
    OsalMutexInit(&acm->readLock);
    OsalMutexInit(&acm->writeLock);
A
annie_wangli 已提交
967
    HDF_LOGD("%s:%d busNum=%d,devAddr=%d",
A
annie_wangli 已提交
968 969 970 971
        __func__, __LINE__, acm->busNum, acm->devAddr);

    ret = UsbSerialDeviceAlloc(acm);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
972
        HDF_LOGE("%s: Serial Device alloc failed", __func__);
A
annie_wangli 已提交
973 974 975 976 977
    }

    acm->initFlag = false;
    g_acmReleaseFlag = false;

A
annie_wangli 已提交
978
    HDF_LOGD("%s:%d init ok!", __func__, __LINE__);
A
annie_wangli 已提交
979 980 981 982 983 984 985 986 987

    return ret;
}

static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
{
    struct AcmDevice *acm = NULL;

    if (device == NULL) {
A
annie_wangli 已提交
988
        HDF_LOGE("%s: device is NULL", __func__);
A
annie_wangli 已提交
989 990 991 992
        return;
    }
    acm = (struct AcmDevice *)device->service;
    if (acm == NULL) {
A
annie_wangli 已提交
993
        HDF_LOGE("%s: acm is null", __func__);
A
annie_wangli 已提交
994 995 996 997 998 999
        return;
    }

    g_acmReleaseFlag = true;

    if (acm->initFlag == true) {
A
annie_wangli 已提交
1000
        HDF_LOGE("%s:%d AcmRelease", __func__, __LINE__);
A
annie_wangli 已提交
1001 1002 1003 1004 1005 1006 1007 1008
        AcmRelease(acm);
    }
    UsbSeriaDevicelFree(acm);
    OsalMutexDestroy(&acm->writeLock);
    OsalMutexDestroy(&acm->readLock);
    OsalMutexDestroy(&acm->lock);
    OsalMemFree(acm);
    acm = NULL;
A
annie_wangli 已提交
1009
    HDF_LOGD("%s:%d exit", __func__, __LINE__);
A
annie_wangli 已提交
1010 1011 1012 1013
}

struct HdfDriverEntry g_usbSerialDriverEntry = {
    .moduleVersion = 1,
A
annie_wangli 已提交
1014
    .moduleName    = "usbhost_acm",    // Driver module name, which must be the same as that configured in the .hcs file.
A
annie_wangli 已提交
1015 1016 1017 1018 1019 1020 1021
    .Bind          = UsbSerialDriverBind,
    .Init          = UsbSerialDriverInit,
    .Release       = UsbSerialDriverRelease,
};
HDF_INIT(g_usbSerialDriverEntry);
```

A
annie_wangli 已提交
1022
### Developing Driver Using Host Raw APIs<a name="section612mcpsimp"></a>
A
annie_wangli 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

```
root {
    module = "usb_pnp_device";
    usb_pnp_config {
        match_attr = "usb_pnp_match";
        usb_pnp_device_id = "UsbPnpDeviceId";
        UsbPnpDeviceId {
            idTableList = [
                "host_acm_rawapi_table"
            ];
A
annie_wangli 已提交
1034 1035
            host_acm_rawapi_table {    // Driver mapping table information.
                // Driver module name, which must be the same as the value of moduleName in the driver entry structure.
A
annie_wangli 已提交
1036
                moduleName = "usbhost_acm_rawapi";
A
annie_wangli 已提交
1037
                // Service name of the driver, which must be unique.
A
annie_wangli 已提交
1038
                serviceName = "usbhost_acm_rawapi_service";
A
annie_wangli 已提交
1039
                // Keyword for matching private driver data.
A
annie_wangli 已提交
1040
                deviceMatchAttr = "usbhost_acm_rawapi_matchAttr";
A
annie_wangli 已提交
1041 1042 1043
                // Data length starting from this field, in bytes.
                length = 21;
                // USB driver matching rule: vendorId+productId+interfaceSubClass+interfaceProtocol+interfaceNumber.
A
annie_wangli 已提交
1044
                matchFlag = 0x0303;
A
annie_wangli 已提交
1045
                // Vendor ID.
A
annie_wangli 已提交
1046
                vendorId = 0x12D1;
A
annie_wangli 已提交
1047
                // Product ID.
A
annie_wangli 已提交
1048
                productId = 0x5000;
A
annie_wangli 已提交
1049
                // The least significant 16 bits of the device sequence number.
A
annie_wangli 已提交
1050
                bcdDeviceLow = 0x0000;
A
annie_wangli 已提交
1051
                // The most significant 16 bits of the device sequence number.
A
annie_wangli 已提交
1052
                bcdDeviceHigh = 0x0000;
A
annie_wangli 已提交
1053
                // Device class code allocated by the USB.
A
annie_wangli 已提交
1054
                deviceClass = 0;
A
annie_wangli 已提交
1055
                // Child class code allocated by the USB.
A
annie_wangli 已提交
1056
                deviceSubClass = 0;
A
annie_wangli 已提交
1057
                // Device protocol code allocated by the USB.
A
annie_wangli 已提交
1058
                deviceProtocol = 0;
A
annie_wangli 已提交
1059
                // Interface type. You can enter multiple types as needed.
A
annie_wangli 已提交
1060
                interfaceClass = [0];
A
annie_wangli 已提交
1061
                // Interface subtype. You can enter multiple subtypes as needed.
A
annie_wangli 已提交
1062
                interfaceSubClass = [2, 0];
A
annie_wangli 已提交
1063
                // Protocol that the interface complies with. You can enter multiple protocols as needed.
A
annie_wangli 已提交
1064
                interfaceProtocol = [1, 2];
A
annie_wangli 已提交
1065
                // Interface number. You can enter multiple interface numbers as needed.
A
annie_wangli 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
                interfaceNumber = [2, 3];
            }
        }
    }
}

#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"

#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 int UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConfigDescriptor **config)
{
    UsbRawDevice *dev = NULL;
    int activeConfig;
    int ret;

    if (devHandle == NULL) {
A
annie_wangli 已提交
1100
        HDF_LOGE("%s:%d devHandle is NULL",
A
annie_wangli 已提交
1101 1102 1103 1104 1105 1106
                 __func__, __LINE__);
        return HDF_ERR_INVALID_PARAM;
    }

    ret = UsbRawGetConfiguration(devHandle, &activeConfig);
    if (ret) {
A
annie_wangli 已提交
1107
        HDF_LOGE("%s:%d UsbRawGetConfiguration failed, ret=%d",
A
annie_wangli 已提交
1108 1109 1110
                 __func__, __LINE__, ret);
        return HDF_FAILURE;
    }
A
annie_wangli 已提交
1111
    HDF_LOGE("%s:%d activeConfig=%d", __func__, __LINE__, activeConfig);
A
annie_wangli 已提交
1112 1113
    dev = UsbRawGetDevice(devHandle);
    if (dev == NULL) {
A
annie_wangli 已提交
1114
        HDF_LOGE("%s:%d UsbRawGetDevice failed",
A
annie_wangli 已提交
1115 1116 1117 1118 1119 1120
                 __func__, __LINE__);
        return HDF_FAILURE;
    }

    ret = UsbRawGetConfigDescriptor(dev, activeConfig, config);
    if (ret) {
A
annie_wangli 已提交
1121
        HDF_LOGE("UsbRawGetConfigDescriptor failed, ret=%dn", ret);
A
annie_wangli 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        return HDF_FAILURE;
    }

    return HDF_SUCCESS;
}
...
static int UsbAllocWriteRequests(struct AcmDevice *acm)
{
    int i;

    for (i = 0; i < ACM_NW; i++) {
        struct AcmWb *snd = &acm->wb[i];
        snd->request = UsbRawAllocRequest(acm->devHandle, 0, acm->dataOutEp->maxPacketSize);
        snd->instance = acm;
        if (snd->request == NULL) {
A
annie_wangli 已提交
1137
            HDF_LOGE("%s: UsbRawAllocRequest failed", __func__);
A
annie_wangli 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
            return HDF_ERR_MALLOC_FAIL;
        }
    }

    return HDF_SUCCESS;
}
...
/* HdfDriverEntry implementations */
static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
{
    struct AcmDevice *acm = NULL;
    struct UsbPnpNotifyServiceInfo *info = NULL;
    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) {
A
annie_wangli 已提交
1163
        HDF_LOGE("%s:%d OsalMutexInit failed", __func__, __LINE__);
A
annie_wangli 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
        goto error;
    }

    info = (struct UsbPnpNotifyServiceInfo *)device->priv;
    if (info != NULL) {
        acm->busNum       = info->busNum;
        acm->devAddr      = info->devNum;
        acm->interfaceCnt = info->interfaceLength;
        err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES,
                       (const void*)info->interfaceNumber, info->interfaceLength);
        if (err != EOK) {
A
annie_wangli 已提交
1175
            HDF_LOGE("%s:%d memcpy_s failed err=%d",
A
annie_wangli 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
                __func__, __LINE__, err);
            goto lock_error;
        }
    } else {
        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:
    if (OsalMutexDestroy(&acm->lock)) {
A
annie_wangli 已提交
1192
        HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__);
A
annie_wangli 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    }
error:
    OsalMemFree(acm);
    acm = NULL;
    return HDF_FAILURE;
}
...
static int UsbAllocReadRequests(struct AcmDevice *acm)
{
    struct UsbRawFillRequestData reqData;
    int size = acm->dataInEp->maxPacketSize;
    int ret;

    for (int i = 0; i < ACM_NR; i++) {
        acm->readReq[i] = UsbRawAllocRequest(acm->devHandle, 0, size);
        if (!acm->readReq[i]) {
A
annie_wangli 已提交
1209
            HDF_LOGE("readReq request failed");
A
annie_wangli 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
            return HDF_ERR_MALLOC_FAIL;
        }

        reqData.endPoint      = acm->dataInEp->addr;
        reqData.numIsoPackets = 0;
        reqData.callback      = AcmReadBulkCallback;
        reqData.userData      = (void *)acm;
        reqData.timeout       = USB_CTRL_SET_TIMEOUT;
        reqData.length        = size;

        ret = UsbRawFillBulkRequest(acm->readReq[i], acm->devHandle, &reqData);
        if (ret) {
A
annie_wangli 已提交
1222
            HDF_LOGE("%s: FillBulkRequest failed, ret=%d n",
A
annie_wangli 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
                     __func__, ret);
            return HDF_FAILURE;
        }
    }

    return HDF_SUCCESS;
}
...
static int UsbAllocNotifyRequest(struct AcmDevice *acm)
{
    struct UsbRawFillRequestData fillRequestData;
    int size = acm->notifyEp->maxPacketSize;
    int ret;

    acm->notifyReq = UsbRawAllocRequest(acm->devHandle, 0, size);
    if (!acm->notifyReq) {
A
annie_wangli 已提交
1239
        HDF_LOGE("notifyReq request failed");
A
annie_wangli 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
        return HDF_ERR_MALLOC_FAIL;
    }

    fillRequestData.endPoint = acm->notifyEp->addr;
    fillRequestData.length = size;
    fillRequestData.numIsoPackets = 0;
    fillRequestData.callback = AcmNotifyReqCallback;
    fillRequestData.userData = (void *)acm;
    fillRequestData.timeout = USB_CTRL_SET_TIMEOUT;

    ret = UsbRawFillInterruptRequest(acm->notifyReq, acm->devHandle, &fillRequestData);
    if (ret) {
A
annie_wangli 已提交
1252
        HDF_LOGE("%s: FillInterruptRequest failed, ret=%d", __func__, ret);
A
annie_wangli 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
        return HDF_FAILURE;
    }

    return HDF_SUCCESS;
}
...
static int32_t UsbSerialInit(struct AcmDevice *acm)
{
    struct UsbSession *session = NULL;
    UsbRawHandle *devHandle = NULL;
    int32_t ret;

    if (acm->initFlag == true) {
A
annie_wangli 已提交
1266
        HDF_LOGE("%s:%d: initFlag is true", __func__, __LINE__);
A
annie_wangli 已提交
1267 1268 1269 1270 1271
        return HDF_SUCCESS;
    }

    ret = UsbRawInit(NULL);
    if (ret) {
A
annie_wangli 已提交
1272
        HDF_LOGE("%s:%d UsbRawInit failed", __func__, __LINE__);
A
annie_wangli 已提交
1273 1274 1275 1276 1277 1278
        return HDF_ERR_IO;
    }
    acm->session = session;

    devHandle = UsbRawOpenDevice(session, acm->busNum, acm->devAddr);
    if (devHandle == NULL) {
A
annie_wangli 已提交
1279
        HDF_LOGE("%s:%d UsbRawOpenDevice failed", __func__, __LINE__);
A
annie_wangli 已提交
1280 1281 1282 1283 1284 1285
        ret =  HDF_FAILURE;
        goto err_open_device;
    }
    acm->devHandle = devHandle;
    ret = UsbGetConfigDescriptor(devHandle, &acm->config);
    if (ret) {
A
annie_wangli 已提交
1286
        HDF_LOGE("%s:%d UsbGetConfigDescriptor failed", __func__, __LINE__);
A
annie_wangli 已提交
1287 1288 1289 1290 1291
        ret =  HDF_FAILURE;
        goto err_get_desc;
    }
    ret = UsbParseConfigDescriptor(acm, acm->config);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
1292
        HDF_LOGE("%s:%d UsbParseConfigDescriptor failed", __func__, __LINE__);
A
annie_wangli 已提交
1293 1294 1295 1296 1297 1298
        ret = HDF_FAILURE;
        goto err_parse_desc;
    }

    ret = AcmWriteBufAlloc(acm);
    if (ret < 0) {
A
annie_wangli 已提交
1299
        HDF_LOGE("%s:%d AcmWriteBufAlloc failed", __func__, __LINE__);
A
annie_wangli 已提交
1300 1301 1302 1303 1304
        ret = HDF_FAILURE;
        goto err_alloc_write_buf;
    }
    ret = UsbAllocWriteRequests(acm);
    if (ret < 0) {
A
annie_wangli 已提交
1305
        HDF_LOGE("%s:%d UsbAllocWriteRequests failed", __func__, __LINE__);
A
annie_wangli 已提交
1306 1307 1308 1309 1310
        ret = HDF_FAILURE;
        goto err_alloc_write_reqs;
    }
    ret = UsbAllocNotifyRequest(acm);
    if (ret) {
A
annie_wangli 已提交
1311
        HDF_LOGE("%s:%d UsbAllocNotifyRequests failed", __func__, __LINE__);
A
annie_wangli 已提交
1312 1313 1314 1315
        goto err_alloc_notify_req;
    }
    ret = UsbAllocReadRequests(acm);
    if (ret) {
A
annie_wangli 已提交
1316
        HDF_LOGE("%s:%d UsbAllocReadRequests failed", __func__, __LINE__);
A
annie_wangli 已提交
1317 1318 1319 1320
        goto err_alloc_read_reqs;
    }
    ret = UsbStartIo(acm);
    if (ret) {
A
annie_wangli 已提交
1321
        HDF_LOGE("%s:%d UsbAllocReadRequests failed", __func__, __LINE__);
A
annie_wangli 已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
        goto err_start_io;
    }

    acm->lineCoding.dwDTERate   = CpuToLe32(DATARATE);
    acm->lineCoding.bCharFormat = CHARFORMAT;
    acm->lineCoding.bParityType = USB_CDC_NO_PARITY;
    acm->lineCoding.bDataBits   = USB_CDC_1_STOP_BITS;

    ret = UsbRawSubmitRequest(acm->notifyReq);
    if (ret) {
A
annie_wangli 已提交
1332
        HDF_LOGE("%s:%d UsbRawSubmitRequest failed", __func__, __LINE__);
A
annie_wangli 已提交
1333 1334 1335 1336 1337
        goto err_submit_req;
    }

    acm->initFlag = true;

A
annie_wangli 已提交
1338
    HDF_LOGD("%s:%d=========================OK", __func__, __LINE__);
A
annie_wangli 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

    return HDF_SUCCESS;

err_submit_req:
    UsbStopIo(acm);
err_start_io:
    UsbFreeReadRequests(acm);
err_alloc_read_reqs:
    UsbFreeNotifyReqeust(acm);
 err_alloc_notify_req:
    UsbFreeWriteRequests(acm);
err_alloc_write_reqs:
    AcmWriteBufFree(acm);
err_alloc_write_buf:
    UsbReleaseInterfaces(acm);
err_parse_desc:
    UsbRawFreeConfigDescriptor(acm->config);
    acm->config = NULL;
err_get_desc:
    (void)UsbRawCloseDevice(devHandle);
err_open_device:
    UsbRawExit(acm->session);

    return ret;
}

static void UsbSerialRelease(struct AcmDevice *acm)
{
    if (acm->initFlag == false) {
A
annie_wangli 已提交
1368
        HDF_LOGE("%s:%d: initFlag is false", __func__, __LINE__);
A
annie_wangli 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
        return;
    }

    /* stop io thread and release all resources */
    UsbStopIo(acm);
    if (g_syncRequest != NULL) {
        UsbRawFreeRequest(g_syncRequest);
        g_syncRequest = NULL;
    }
    UsbFreeReadRequests(acm);
    UsbFreeNotifyReqeust(acm);
    UsbFreeWriteRequests(acm);
    AcmWriteBufFree(acm);
    (void)UsbRawCloseDevice(acm->devHandle);
    UsbReleaseInterfaces(acm);
    UsbRawFreeConfigDescriptor(acm->config);
    acm->config = NULL;
    UsbRawExit(acm->session);

    acm->initFlag = false;
}

static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
{
    struct AcmDevice *acm = NULL;
    int32_t ret;

    if (device == NULL) {
A
annie_wangli 已提交
1397
        HDF_LOGE("%s:%d device is null", __func__, __LINE__);
A
annie_wangli 已提交
1398 1399 1400 1401 1402 1403 1404 1405
        return HDF_ERR_INVALID_OBJECT;
    }
    acm = (struct AcmDevice *)device->service;
    OsalMutexInit(&acm->readLock);
    OsalMutexInit(&acm->writeLock);

    ret = UsbSerialDeviceAlloc(acm);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
1406
        HDF_LOGE("%s:%d UsbSerialDeviceAlloc failed", __func__, __LINE__);
A
annie_wangli 已提交
1407 1408 1409 1410 1411
    }

    acm->initFlag = false;
    g_rawAcmReleaseFlag = false;

A
annie_wangli 已提交
1412
    HDF_LOGD("%s:%d init ok!", __func__, __LINE__);
A
annie_wangli 已提交
1413 1414 1415 1416 1417 1418 1419 1420

    return ret;
}

static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
{
    struct AcmDevice *acm = NULL;
    if (device == NULL) {
A
annie_wangli 已提交
1421
        HDF_LOGE("%s: device is NULL", __func__);
A
annie_wangli 已提交
1422 1423 1424 1425 1426
        return;
    }

    acm = (struct AcmDevice *)device->service;
    if (acm == NULL) {
A
annie_wangli 已提交
1427
        HDF_LOGE("%s: acm is null", __func__);
A
annie_wangli 已提交
1428 1429 1430 1431 1432 1433
        return;
    }

    g_rawAcmReleaseFlag = true;

    if (acm->initFlag == true) {
A
annie_wangli 已提交
1434
        HDF_LOGE("%s:%d UsbSerialRelease", __func__, __LINE__);
A
annie_wangli 已提交
1435 1436 1437 1438 1439 1440 1441 1442
        UsbSerialRelease(acm);
    }
    UsbSeriaDevicelFree(acm);
    OsalMutexDestroy(&acm->writeLock);
    OsalMutexDestroy(&acm->readLock);
    OsalMutexDestroy(&acm->lock);
    OsalMemFree(acm);
    acm = NULL;
A
annie_wangli 已提交
1443
    HDF_LOGD("%s:%d exit", __func__, __LINE__);
A
annie_wangli 已提交
1444 1445 1446 1447
}

struct HdfDriverEntry g_usbSerialRawDriverEntry = {
    .moduleVersion = 1,
A
annie_wangli 已提交
1448
    ..moduleName    = "usbhost_acm_rawapi",    // Driver module name, which must be the same as that configured in the .hcs file.
A
annie_wangli 已提交
1449 1450 1451 1452 1453 1454 1455
    .Bind          = UsbSerialDriverBind,
    .Init          = UsbSerialDriverInit,
    .Release       = UsbSerialDriverRelease,
};
HDF_INIT(g_usbSerialRawDriverEntry);
```

A
annie_wangli 已提交
1456 1457 1458
### Developing Driver Using Device DDK APIs<a name="section615mcpsimp"></a>

The core code of the USB Abstract Control Model (ACM) device is available in **drivers\peripheral\usb\gadget\function\acm\cdcacm.c**. The following is an example:
A
annie_wangli 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467

```
1. Create a device.
static int32_t AcmCreateFuncDevice(struct UsbAcmDevice *acm,
    struct DeviceResourceIface *iface)
{
    struct UsbFnDevice *fnDev = NULL;
struct UsbFnDescriptorData descData;
uint8_t useHcs;
A
annie_wangli 已提交
1468
     ...
A
annie_wangli 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
if (useHcs == 0) {
    descData.type = USBFN_DESC_DATA_TYPE_DESC;
    descData.descriptor = &g_masterFuncDevice;
} else {
    descData.type = USBFN_DESC_DATA_TYPE_PROP;
    descData.property = device->property;
}
/* Create a device. */
    fnDev = (struct UsbFnDevice *)UsbFnDeviceCreate(acm->udcName, &descData);
    if (fnDev == NULL) {
A
annie_wangli 已提交
1479
        HDF_LOGE("%s: create usb function device failed", __func__);
A
annie_wangli 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
        return HDF_FAILURE;
    }
    ...
}
2. Obtain an interface and open the interface for pipe information.
static int32_t AcmParseEachPipe(struct UsbAcmDevice *acm, struct UsbAcmInterface *iface)
{
    ...
    for (i = 0; i < fnIface->info.numPipes; i++) {
        struct UsbFnPipeInfo pipeInfo;
/* Obtain pipe information. */
        ret = UsbFnInterfaceGetPipeInfo(fnIface, i, &pipeInfo);
        ...
    }
    return HDF_SUCCESS;
}
/* Obtain the interface and open the interface to obtain the handle. */
static int32_t AcmParseEachIface(struct UsbAcmDevice *acm, struct UsbFnDevice *fnDev)
{
    ...
    for (i = 0; i < fnDev->numInterfaces; i++) {
        /* Obtain an interface.*/
        fnIface = (struct UsbFnInterface *)UsbFnDeviceGetInterface(fnDev, i);
        ...
        /* Open the interface. */
        handle = UsbFnInterfaceOpen(fnIface);
        ...
    }
    return HDF_SUCCESS;
}
3. Receive events.
static int32_t AcmAllocCtrlRequests(struct UsbAcmDevice *acm, int num)
{
    ...
        req = UsbFnCtrlRequestAlloc(acm->ctrlIface.handle,
            sizeof(struct UsbCdcLineCoding) + sizeof(struct UsbCdcLineCoding));
    ...
}
static int32_t AcmDriverInit(struct HdfDeviceObject *device)
{
...    
/* Start to receive events.*/
    ret = UsbFnInterfaceStartRecvEvent(acm->ctrlIface.fn, 0xff, UsbAcmEventCallback, acm);
    ...
}
4. Perform USB communication (read and write).
static int32_t AcmSendNotifyRequest(struct UsbAcmDevice *acm, uint8_t type,
    uint16_t value, void *data, uint32_t length)
{
...
A
annie_wangli 已提交
1530
/* Send an asynchronous request.*/
A
annie_wangli 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
    ret = UsbFnRequestSubmitAsync(req); 
    ...
}
5. Close the interface, stop receiving events, and remove the device.
static int32_t AcmReleaseFuncDevice(struct UsbAcmDevice *acm)
{
int32_t ret;
/* Close the interface. */
    (void)UsbFnInterfaceClose(acm->ctrlIface.handle);
(void)UsbFnInterfaceClose(acm->dataIface.handle);
/* Stop receiving events. */
(void)UsbFnInterfaceStopRecvEvent(acm->ctrlIface.fn);
/* Remove the device. */
    ret = UsbFnDeviceRemove(acm->fnDev);
    if (ret != HDF_SUCCESS) {
A
annie_wangli 已提交
1546
        HDF_LOGE("%s: remove usb function device failed", __func__);
A
annie_wangli 已提交
1547 1548 1549 1550
    }
    return ret;
}
```