1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "usb_raw_api.h"
17 #include "usb_raw_api_library.h"
18
19 #define HDF_LOG_TAG USB_RAW_API
20
UsbRawInit(struct UsbSession ** session)21 int32_t UsbRawInit(struct UsbSession **session)
22 {
23 return RawInit(session);
24 }
25
UsbRawExit(const struct UsbSession * session)26 int32_t UsbRawExit(const struct UsbSession *session)
27 {
28 return RawExit(session);
29 }
30
UsbRawOpenDevice(const struct UsbSession * session,uint8_t busNum,uint8_t usbAddr)31 UsbRawHandle *UsbRawOpenDevice(const struct UsbSession *session, uint8_t busNum, uint8_t usbAddr)
32 {
33 return (UsbRawHandle *)RawOpenDevice(session, busNum, usbAddr);
34 }
35
UsbRawCloseDevice(const UsbRawHandle * devHandle)36 int32_t UsbRawCloseDevice(const UsbRawHandle *devHandle)
37 {
38 if (devHandle == NULL) {
39 HDF_LOGE("%{public}s:%d devHandle is NULL", __func__, __LINE__);
40 return HDF_ERR_INVALID_PARAM;
41 }
42
43 return RawCloseDevice((const struct UsbDeviceHandle *)devHandle);
44 }
45
UsbRawSendControlRequest(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbControlRequestData * requestData)46 int32_t UsbRawSendControlRequest(
47 const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbControlRequestData *requestData)
48 {
49 if ((request == NULL) || (devHandle == NULL) || (requestData == NULL)) {
50 HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
51 return HDF_ERR_INVALID_PARAM;
52 }
53
54 return RawSendControlRequest(
55 (struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle, requestData);
56 }
57
UsbRawSendBulkRequest(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbRequestData * requestData)58 int32_t UsbRawSendBulkRequest(
59 const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData)
60 {
61 if ((request == NULL) || (devHandle == NULL) || (requestData == NULL)) {
62 HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
63 return HDF_ERR_INVALID_PARAM;
64 }
65
66 return RawSendBulkRequest(
67 (const struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle, requestData);
68 }
69
UsbRawSendInterruptRequest(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbRequestData * requestData)70 int32_t UsbRawSendInterruptRequest(
71 const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRequestData *requestData)
72 {
73 if ((request == NULL) || (devHandle == NULL) || (requestData == NULL)) {
74 HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
75 return HDF_ERR_INVALID_PARAM;
76 }
77
78 return RawSendInterruptRequest(
79 (const struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle, requestData);
80 }
81
UsbRawGetConfigDescriptor(const UsbRawDevice * rawDev,uint8_t configIndex,struct UsbRawConfigDescriptor ** const config)82 int32_t UsbRawGetConfigDescriptor(
83 const UsbRawDevice *rawDev, uint8_t configIndex, struct UsbRawConfigDescriptor ** const config)
84 {
85 if ((rawDev == NULL) || (config == NULL)) {
86 HDF_LOGE("%{public}s:%d rawDev or config is NULL", __func__, __LINE__);
87 return HDF_ERR_INVALID_PARAM;
88 }
89
90 return RawGetConfigDescriptor((const struct UsbDevice *)rawDev, configIndex, config);
91 }
92
UsbRawFreeConfigDescriptor(const struct UsbRawConfigDescriptor * config)93 void UsbRawFreeConfigDescriptor(const struct UsbRawConfigDescriptor *config)
94 {
95 if (config == NULL) {
96 HDF_LOGE("%{public}s:%d config is NULL", __func__, __LINE__);
97 return;
98 }
99
100 RawClearConfiguration((struct UsbRawConfigDescriptor *)config);
101 RawUsbMemFree((void *)config);
102 }
103
UsbRawGetConfiguration(const UsbRawHandle * const devHandle,int32_t * config)104 int32_t UsbRawGetConfiguration(const UsbRawHandle * const devHandle, int32_t *config)
105 {
106 if ((devHandle == NULL) || (config == NULL)) {
107 HDF_LOGE("%{public}s:%d dev or config is NULL", __func__, __LINE__);
108 return HDF_ERR_INVALID_PARAM;
109 }
110
111 return RawGetConfiguration((const struct UsbDeviceHandle *)devHandle, config);
112 }
113
UsbRawSetConfiguration(const UsbRawHandle * devHandle,int32_t config)114 int32_t UsbRawSetConfiguration(const UsbRawHandle *devHandle, int32_t config)
115 {
116 if (devHandle == NULL) {
117 HDF_LOGE("%{public}s:%d dev is NULL", __func__, __LINE__);
118 return HDF_ERR_INVALID_PARAM;
119 }
120
121 return RawSetConfiguration((const struct UsbDeviceHandle *)devHandle, config);
122 }
123
UsbRawGetDescriptor(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbRawDescriptorParam * param,const unsigned char * data)124 int32_t UsbRawGetDescriptor(const struct UsbRawRequest *request, const UsbRawHandle *devHandle,
125 const struct UsbRawDescriptorParam *param, const unsigned char *data)
126 {
127 if ((request == NULL) || (devHandle == NULL) || (param == NULL) || (data == NULL)) {
128 HDF_LOGE("%{public}s:%d request or devHandle is NULL", __func__, __LINE__);
129 return HDF_ERR_INVALID_PARAM;
130 }
131
132 return RawGetDescriptor(
133 (const struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle, param, data);
134 }
135
UsbRawGetDevice(const UsbRawHandle * const devHandle)136 UsbRawDevice *UsbRawGetDevice(const UsbRawHandle * const devHandle)
137 {
138 if (devHandle == NULL) {
139 HDF_LOGE("%{public}s:%d devHandle is NULL ", __func__, __LINE__);
140 return NULL;
141 }
142
143 return (UsbRawDevice *)RawGetDevice((const struct UsbDeviceHandle *)devHandle);
144 }
145
UsbRawGetDeviceDescriptor(const UsbRawDevice * rawDev,struct UsbDeviceDescriptor * desc)146 int32_t UsbRawGetDeviceDescriptor(const UsbRawDevice *rawDev, struct UsbDeviceDescriptor *desc)
147 {
148 if ((rawDev == NULL) || (desc == NULL)) {
149 HDF_LOGE("%{public}s:%d rawDev or desc is NULL", __func__, __LINE__);
150 return HDF_ERR_INVALID_PARAM;
151 }
152
153 return RawGetDeviceDescriptor((const struct UsbDevice *)rawDev, desc);
154 }
155
UsbRawClaimInterface(const UsbRawHandle * devHandle,int32_t interfaceNumber)156 int32_t UsbRawClaimInterface(const UsbRawHandle *devHandle, int32_t interfaceNumber)
157 {
158 if (devHandle == NULL) {
159 HDF_LOGE("%{public}s:%d devHandle is NULL", __func__, __LINE__);
160 return HDF_ERR_INVALID_PARAM;
161 }
162
163 return RawClaimInterface((struct UsbDeviceHandle *)devHandle, interfaceNumber);
164 }
165
UsbRawReleaseInterface(const UsbRawHandle * devHandle,int32_t interfaceNumber)166 int32_t UsbRawReleaseInterface(const UsbRawHandle *devHandle, int32_t interfaceNumber)
167 {
168 if (devHandle == NULL) {
169 HDF_LOGE("%{public}s:%d devHandle is NULL", __func__, __LINE__);
170 return HDF_ERR_INVALID_PARAM;
171 }
172
173 return RawReleaseInterface((struct UsbDeviceHandle *)devHandle, interfaceNumber);
174 }
175
UsbRawResetDevice(const UsbRawHandle * devHandle)176 int32_t UsbRawResetDevice(const UsbRawHandle *devHandle)
177 {
178 if (devHandle == NULL) {
179 HDF_LOGE("%{public}s:%d devHandle is NULL", __func__, __LINE__);
180 return HDF_ERR_INVALID_PARAM;
181 }
182
183 return RawResetDevice((const struct UsbDeviceHandle *)devHandle);
184 }
185
UsbRawAllocRequest(const UsbRawHandle * devHandle,int32_t isoPackets,int32_t length)186 struct UsbRawRequest *UsbRawAllocRequest(const UsbRawHandle *devHandle, int32_t isoPackets, int32_t length)
187 {
188 if (devHandle == NULL) {
189 HDF_LOGE("%{public}s:%d devHandle is NULL", __func__, __LINE__);
190 return NULL;
191 }
192
193 return (struct UsbRawRequest *)RawAllocRequest((const struct UsbDeviceHandle *)devHandle, isoPackets, length);
194 }
195
UsbRawFreeRequest(const struct UsbRawRequest * request)196 int32_t UsbRawFreeRequest(const struct UsbRawRequest *request)
197 {
198 if (request == NULL) {
199 HDF_LOGE("%{public}s:%d request is NULL", __func__, __LINE__);
200 return HDF_ERR_INVALID_PARAM;
201 }
202
203 return RawFreeRequest((const struct UsbHostRequest *)request);
204 }
205
UsbRawFillBulkRequest(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbRawFillRequestData * fillData)206 int32_t UsbRawFillBulkRequest(
207 const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData)
208 {
209 if ((request == NULL) || (devHandle == NULL) || (fillData == NULL)) {
210 HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
211 return HDF_ERR_INVALID_PARAM;
212 }
213
214 return RawFillBulkRequest((struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle,
215 (const struct UsbFillRequestData *)fillData);
216 }
217
UsbRawFillControlSetup(const unsigned char * setup,const struct UsbControlRequestData * requestData)218 int32_t UsbRawFillControlSetup(const unsigned char *setup, const struct UsbControlRequestData *requestData)
219 {
220 if ((setup == NULL) || (requestData == NULL)) {
221 HDF_LOGE("%{public}s:%d setup or requestData is NULL", __func__, __LINE__);
222 return HDF_ERR_INVALID_PARAM;
223 }
224
225 return RawFillControlSetup(setup, requestData);
226 }
227
UsbRawFillControlRequest(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbRawFillRequestData * fillData)228 int32_t UsbRawFillControlRequest(
229 const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData)
230 {
231 if ((request == NULL) || (devHandle == NULL) || (fillData == NULL)) {
232 HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
233 return HDF_ERR_INVALID_PARAM;
234 }
235
236 return RawFillControlRequest((struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle,
237 (const struct UsbFillRequestData *)fillData);
238 }
239
UsbRawFillInterruptRequest(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbRawFillRequestData * fillData)240 int32_t UsbRawFillInterruptRequest(
241 const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData)
242 {
243 if ((request == NULL) || (devHandle == NULL) || (fillData == NULL)) {
244 HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
245 return HDF_ERR_INVALID_PARAM;
246 }
247
248 return RawFillInterruptRequest((struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle,
249 (const struct UsbFillRequestData *)fillData);
250 }
251
UsbRawFillIsoRequest(const struct UsbRawRequest * request,const UsbRawHandle * devHandle,const struct UsbRawFillRequestData * fillData)252 int32_t UsbRawFillIsoRequest(
253 const struct UsbRawRequest *request, const UsbRawHandle *devHandle, const struct UsbRawFillRequestData *fillData)
254 {
255 if ((request == NULL) || (devHandle == NULL) || (fillData == NULL)) {
256 HDF_LOGE("%{public}s:%d invalid param", __func__, __LINE__);
257 return HDF_ERR_INVALID_PARAM;
258 }
259
260 return RawFillIsoRequest((struct UsbHostRequest *)request, (const struct UsbDeviceHandle *)devHandle,
261 (const struct UsbFillRequestData *)fillData);
262 }
263
UsbRawSubmitRequest(const struct UsbRawRequest * request)264 int32_t UsbRawSubmitRequest(const struct UsbRawRequest *request)
265 {
266 if (request == NULL) {
267 HDF_LOGE("%{public}s:%d request is NULL", __func__, __LINE__);
268 return HDF_ERR_INVALID_PARAM;
269 }
270
271 return RawSubmitRequest((const struct UsbHostRequest *)request);
272 }
273
UsbRawCancelRequest(const struct UsbRawRequest * request)274 int32_t UsbRawCancelRequest(const struct UsbRawRequest *request)
275 {
276 if (request == NULL) {
277 HDF_LOGE("%{public}s:%d request is NULL", __func__, __LINE__);
278 return HDF_ERR_INVALID_PARAM;
279 }
280
281 return RawCancelRequest((const struct UsbHostRequest *)request);
282 }
283
UsbRawHandleRequests(const UsbRawHandle * devHandle)284 int32_t UsbRawHandleRequests(const UsbRawHandle *devHandle)
285 {
286 if (devHandle == NULL) {
287 HDF_LOGE("%{public}s:%d devHandle is NULL", __func__, __LINE__);
288 return HDF_ERR_INVALID_PARAM;
289 }
290
291 return RawHandleRequest((const struct UsbDeviceHandle *)devHandle);
292 }
293