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 "usbfn_io_mgr.h"
17 #include "usbd_wrapper.h"
18
19 #define HDF_LOG_TAG usbfn_io_mgr
20
ReqToIoData(struct UsbFnRequest * req,struct IoData * ioData,uint32_t aio,uint32_t timeout)21 static int32_t ReqToIoData(struct UsbFnRequest *req, struct IoData *ioData, uint32_t aio, uint32_t timeout)
22 {
23 if (req == NULL || ioData == NULL) {
24 HDF_LOGE("%{public}s invalid param", __func__);
25 return HDF_ERR_INVALID_PARAM;
26 }
27 struct ReqList *reqList = (struct ReqList *)req;
28 ioData->aio = aio;
29 if (req->type == USB_REQUEST_TYPE_PIPE_WRITE) {
30 ioData->read = 0;
31 } else if (req->type == USB_REQUEST_TYPE_PIPE_READ) {
32 ioData->read = 1;
33 }
34 ioData->buf = reqList->buf;
35 ioData->len = req->length;
36 ioData->timeout = timeout;
37
38 return 0;
39 }
40
OpenEp0AndMapAddr(struct UsbFnFuncMgr * funcMgr)41 int32_t OpenEp0AndMapAddr(struct UsbFnFuncMgr *funcMgr)
42 {
43 int32_t ret;
44 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
45 funcMgr->fd = fnOps->openPipe(funcMgr->name, 0);
46 if (funcMgr->fd <= 0) {
47 HDF_LOGE("%{public}s:%{public}d openPipe failed", __func__, __LINE__);
48 return HDF_ERR_IO;
49 }
50
51 ret = fnOps->queueInit(funcMgr->fd);
52 if (ret) {
53 HDF_LOGE("%{public}s:%{public}d queueInit failed", __func__, __LINE__);
54 return HDF_ERR_IO;
55 }
56 return 0;
57 }
58
GetReqType(struct UsbHandleMgr * handle,uint8_t pipe)59 static UsbFnRequestType GetReqType(struct UsbHandleMgr *handle, uint8_t pipe)
60 {
61 struct UsbFnPipeInfo info = {0};
62 UsbFnRequestType type = USB_REQUEST_TYPE_INVALID;
63 if (pipe > 0) {
64 int32_t ret = UsbFnIoMgrInterfaceGetPipeInfo(&(handle->intfMgr->interface), pipe - 1, &info);
65 if (ret) {
66 HDF_LOGE("%{public}s:%{public}d UsbFnMgrInterfaceGetPipeInfo err", __func__, __LINE__);
67 type = USB_REQUEST_TYPE_INVALID;
68 }
69 if (info.dir == USB_PIPE_DIRECTION_IN) {
70 type = USB_REQUEST_TYPE_PIPE_WRITE;
71 } else if (info.dir == USB_PIPE_DIRECTION_OUT) {
72 type = USB_REQUEST_TYPE_PIPE_READ;
73 }
74 }
75 return type;
76 }
77
UsbFnIoMgrRequestAlloc(struct UsbHandleMgr * handle,uint8_t pipe,uint32_t len)78 struct UsbFnRequest *UsbFnIoMgrRequestAlloc(struct UsbHandleMgr *handle, uint8_t pipe, uint32_t len)
79 {
80 int32_t ep;
81 struct UsbFnInterfaceMgr *intfMgr = handle->intfMgr;
82 struct UsbFnFuncMgr *funcMgr = intfMgr->funcMgr;
83 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
84 if (pipe == 0) {
85 if (funcMgr->fd <= 0) {
86 int32_t ret = OpenEp0AndMapAddr(funcMgr);
87 if (ret) {
88 return NULL;
89 }
90 }
91 ep = funcMgr->fd;
92 } else if (pipe <= MAX_EP) {
93 ep = handle->fds[pipe - 1];
94 } else {
95 return NULL;
96 }
97 uint8_t *mapAddr = fnOps->mapAddr(ep, len);
98 if (mapAddr == NULL || mapAddr == (uint8_t *)-1) {
99 HDF_LOGE("%{public}s:%{public}d mapAddr %{public}d to %{public}p failed", __func__, __LINE__, ep, mapAddr);
100 return NULL;
101 }
102
103 struct ReqList *reqList = UsbFnMemCalloc(sizeof(struct ReqList));
104 if (reqList == NULL) {
105 HDF_LOGE("%{public}s:%{public}d UsbFnMemCalloc err", __func__, __LINE__);
106 return NULL;
107 }
108 struct UsbFnRequest *req = &reqList->req;
109
110 if (pipe == 0) {
111 DListInsertTail(&reqList->entry, &funcMgr->reqEntry);
112 } else {
113 DListInsertTail(&reqList->entry, &handle->reqEntry);
114 }
115 reqList->handle = handle;
116 reqList->fd = ep;
117 reqList->buf = (uintptr_t)mapAddr;
118 reqList->pipe = pipe;
119 reqList->bufLen = len;
120 req->length = len;
121 req->obj = handle->intfMgr->interface.object;
122 req->buf = mapAddr;
123 req->type = GetReqType(handle, pipe);
124
125 return req;
126 }
127
UsbFnIoMgrRequestFree(struct UsbFnRequest * req)128 int32_t UsbFnIoMgrRequestFree(struct UsbFnRequest *req)
129 {
130 struct GenericMemory mem;
131 int32_t ret;
132 if (req == NULL) {
133 HDF_LOGE("%{public}s invalid param", __func__);
134 return HDF_ERR_INVALID_PARAM;
135 }
136
137 struct ReqList *reqList = (struct ReqList *)req;
138 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
139
140 ret = fnOps->unmapAddr(req->buf, reqList->bufLen);
141 if (ret) {
142 HDF_LOGE("%{public}s:%{public}d ummapAddr failed, ret=%{public}d ", __func__, __LINE__, ret);
143 return HDF_ERR_DEVICE_BUSY;
144 }
145 mem.size = reqList->bufLen;
146 mem.buf = (uint64_t)req->buf;
147 ret = fnOps->releaseBuf(reqList->fd, &mem);
148 if (ret) {
149 HDF_LOGE("%{public}s releaseBuf err::%{public}d", __func__, ret);
150 return HDF_ERR_INVALID_PARAM;
151 }
152
153 DListRemove(&reqList->entry);
154 UsbFnMemFree(reqList);
155 return 0;
156 }
157
UsbFnIoMgrRequestSubmitAsync(struct UsbFnRequest * req)158 int32_t UsbFnIoMgrRequestSubmitAsync(struct UsbFnRequest *req)
159 {
160 int32_t ret;
161 struct IoData ioData = {0};
162 struct ReqList *reqList = NULL;
163 if (req == NULL) {
164 HDF_LOGE("%{public}s invalid param", __func__);
165 return HDF_ERR_INVALID_PARAM;
166 }
167 reqList = (struct ReqList *)req;
168 if (ReqToIoData(req, &ioData, 1, 0)) {
169 return HDF_ERR_IO;
170 }
171
172 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
173 ret = fnOps->pipeIo(reqList->fd, &ioData);
174 if (ret != HDF_SUCCESS) {
175 HDF_LOGE("%{public}s pipeIo failed fd:%{public}d read:%{public}u", __func__, reqList->fd, ioData.read);
176 }
177
178 return ret;
179 }
180
UsbFnIoMgrRequestCancel(struct UsbFnRequest * req)181 int32_t UsbFnIoMgrRequestCancel(struct UsbFnRequest *req)
182 {
183 int32_t ret;
184 struct IoData ioData = {0};
185 struct ReqList *reqList = NULL;
186 if (req == NULL) {
187 HDF_LOGE("%{public}s invalid param", __func__);
188 return HDF_ERR_INVALID_PARAM;
189 }
190 reqList = (struct ReqList *)req;
191 if (ReqToIoData(req, &ioData, 1, 0)) {
192 return HDF_ERR_IO;
193 }
194 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
195 ret = fnOps->cancelIo(reqList->fd, &ioData);
196
197 return ret;
198 }
199
UsbFnIoMgrRequestGetStatus(struct UsbFnRequest * req,UsbRequestStatus * status)200 int32_t UsbFnIoMgrRequestGetStatus(struct UsbFnRequest *req, UsbRequestStatus *status)
201 {
202 struct IoData ioData = {0};
203 struct ReqList *reqList;
204 if (req == NULL) {
205 HDF_LOGE("%{public}s invalid param", __func__);
206 return HDF_ERR_INVALID_PARAM;
207 }
208 reqList = (struct ReqList *)req;
209 if (ReqToIoData(req, &ioData, 1, 0)) {
210 return HDF_ERR_IO;
211 }
212 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
213 *status = -(fnOps->getReqStatus(reqList->fd, &ioData));
214
215 return 0;
216 }
217
UsbFnIoMgrRequestSubmitSync(struct UsbFnRequest * req,uint32_t timeout)218 int32_t UsbFnIoMgrRequestSubmitSync(struct UsbFnRequest *req, uint32_t timeout)
219 {
220 int32_t ret;
221 struct IoData ioData = {0};
222 struct ReqList *reqList;
223
224 if (req == NULL) {
225 HDF_LOGE("%{public}s invalid param", __func__);
226 return HDF_ERR_INVALID_PARAM;
227 }
228 reqList = (struct ReqList *)req;
229 if (ReqToIoData(req, &ioData, 0, timeout)) {
230 return HDF_ERR_IO;
231 }
232 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
233 ret = fnOps->pipeIo(reqList->fd, &ioData);
234 if (ret > 0) {
235 req->status = USB_REQUEST_COMPLETED;
236 req->actual = (uint32_t)ret;
237 return 0;
238 }
239
240 return ret;
241 }
242
HandleInit(struct UsbHandleMgr * handle,struct UsbFnInterfaceMgr * interfaceMgr)243 static int32_t HandleInit(struct UsbHandleMgr *handle, struct UsbFnInterfaceMgr *interfaceMgr)
244 {
245 int32_t ret;
246 uint32_t i, j;
247 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
248
249 DListHeadInit(&handle->reqEntry);
250 handle->numFd = interfaceMgr->interface.info.numPipes;
251 for (i = 0; i < handle->numFd; i++) {
252 handle->fds[i] = fnOps->openPipe(interfaceMgr->funcMgr->name, interfaceMgr->startEpId + i);
253 if (handle->fds[i] <= 0) {
254 return HDF_ERR_IO;
255 }
256
257 ret = fnOps->queueInit(handle->fds[i]);
258 if (ret) {
259 HDF_LOGE("%{public}s: queueInit failed ret = %{public}d", __func__, ret);
260 return HDF_ERR_IO;
261 }
262
263 handle->reqEvent[i] = UsbFnMemCalloc(sizeof(struct UsbFnReqEvent) * MAX_REQUEST);
264 if (handle->reqEvent[i] == NULL) {
265 HDF_LOGE("%{public}s: UsbFnMemCalloc failed", __func__);
266 goto FREE_EVENT;
267 }
268 }
269 handle->intfMgr = interfaceMgr;
270 return 0;
271
272 FREE_EVENT:
273 for (j = 0; j < i; j++) {
274 UsbFnMemFree(handle->reqEvent[j]);
275 }
276 return HDF_ERR_IO;
277 }
278
UsbFnIoMgrInterfaceOpen(struct UsbFnInterface * interface)279 struct UsbHandleMgr *UsbFnIoMgrInterfaceOpen(struct UsbFnInterface *interface)
280 {
281 int32_t ret;
282 if (interface == NULL) {
283 return NULL;
284 }
285 struct UsbFnInterfaceMgr *interfaceMgr = (struct UsbFnInterfaceMgr *)interface;
286 if (interfaceMgr->isOpen) {
287 HDF_LOGE("%{public}s: interface has opened", __func__);
288 return NULL;
289 }
290 struct UsbHandleMgr *handle = UsbFnMemCalloc(sizeof(struct UsbHandleMgr));
291 if (handle == NULL) {
292 HDF_LOGE("%{public}s: malloc UsbHandleMgr failed", __func__);
293 return NULL;
294 }
295
296 ret = HandleInit(handle, interfaceMgr);
297 if (ret) {
298 HDF_LOGE("%{public}s: HandleInit failed", __func__);
299 UsbFnMemFree(handle);
300 return NULL;
301 }
302
303 interfaceMgr->isOpen = true;
304 interfaceMgr->handle = handle;
305 return handle;
306 }
307
UsbFnIoMgrInterfaceClose(struct UsbHandleMgr * handle)308 int32_t UsbFnIoMgrInterfaceClose(struct UsbHandleMgr *handle)
309 {
310 if (handle == NULL) {
311 HDF_LOGE("%{public}s invalid param", __func__);
312 return HDF_ERR_INVALID_PARAM;
313 }
314
315 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
316 struct UsbFnInterfaceMgr *interfaceMgr = handle->intfMgr;
317 if (interfaceMgr == NULL || interfaceMgr->isOpen == false) {
318 HDF_LOGE("%{public}s invalid param", __func__);
319 return HDF_ERR_INVALID_PARAM;
320 }
321 for (uint32_t i = 0; i < handle->numFd; i++) {
322 int32_t ret = fnOps->queueDel(handle->fds[i]);
323 if (ret) {
324 HDF_LOGE("%{public}s:%{public}d queueDel failed, ret=%{public}d ", __func__, __LINE__, ret);
325 return HDF_ERR_DEVICE_BUSY;
326 }
327
328 ret = fnOps->closePipe(handle->fds[i]);
329 if (ret) {
330 HDF_LOGE("%{public}s:%{public}d closePipe failed, ret=%{public}d ", __func__, __LINE__, ret);
331 return HDF_ERR_DEVICE_BUSY;
332 }
333 handle->fds[i] = -1;
334 UsbFnMemFree(handle->reqEvent[i]);
335 handle->reqEvent[i] = NULL;
336 }
337
338 UsbFnMemFree(handle);
339 handle = NULL;
340 interfaceMgr->isOpen = false;
341 interfaceMgr->handle = NULL;
342 return 0;
343 }
344
UsbFnIoMgrInterfaceGetPipeInfo(struct UsbFnInterface * interface,uint8_t pipeId,struct UsbFnPipeInfo * info)345 int32_t UsbFnIoMgrInterfaceGetPipeInfo(struct UsbFnInterface *interface, uint8_t pipeId, struct UsbFnPipeInfo *info)
346 {
347 int32_t ret;
348 int32_t fd;
349 if (info == NULL || interface == NULL || pipeId >= interface->info.numPipes) {
350 HDF_LOGE("%{public}s invalid param", __func__);
351 return HDF_ERR_INVALID_PARAM;
352 }
353 struct UsbFnAdapterOps *fnOps = UsbFnAdapterGetOps();
354 struct UsbFnInterfaceMgr *interfaceMgr = (struct UsbFnInterfaceMgr *)interface;
355 if (interfaceMgr->isOpen) {
356 if (pipeId >= MAX_EP) {
357 HDF_LOGE("%{public}s pipeId overflow", __func__);
358 return HDF_ERR_INVALID_PARAM;
359 }
360 fd = interfaceMgr->handle->fds[pipeId];
361 ret = fnOps->getPipeInfo(fd, info);
362 if (ret) {
363 HDF_LOGE("%{public}s: getPipeInfo failed", __func__);
364 return HDF_ERR_DEVICE_BUSY;
365 }
366 } else {
367 fd = fnOps->openPipe(interfaceMgr->funcMgr->name, interfaceMgr->startEpId + pipeId);
368 if (fd <= 0) {
369 HDF_LOGE("%{public}s: openPipe failed", __func__);
370 return HDF_ERR_IO;
371 }
372 ret = fnOps->getPipeInfo(fd, info);
373 if (ret) {
374 fnOps->closePipe(fd);
375 HDF_LOGE("%{public}s: getPipeInfo failed", __func__);
376 return HDF_ERR_DEVICE_BUSY;
377 }
378 fnOps->closePipe(fd);
379 }
380
381 info->id = pipeId;
382 return ret;
383 }
384