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_serial.h"
17 #include "hdf_base.h"
18 #include "hdf_log.h"
19 #include "hdf_usb_pnp_manage.h"
20 #include "osal_mem.h"
21 #include "osal_time.h"
22 #include "securec.h"
23 #include "usb_ddk_interface.h"
24
25 #define HDF_LOG_TAG USB_HOST_ACM
26 #define STR_LEN 512
27
28 static struct UsbRequest *g_syncRequest = NULL;
29 static struct UsbRequest *g_ctrlCmdRequest = NULL;
30 static bool g_acmReleaseFlag = false;
31 static uint8_t *g_acmReadBuffer = NULL;
32
33 static int32_t SerialCtrlMsg(struct AcmDevice *acm, uint8_t request, uint16_t value, void *buf, uint16_t len);
34 static void AcmWriteBulk(struct UsbRequest * const req);
35 static int32_t AcmInit(struct AcmDevice *acm);
36 static void AcmRelease(struct AcmDevice *acm);
37 static struct UsbInterface *GetUsbInterfaceById(const struct AcmDevice *acm, uint8_t interfaceIndex);
38
AcmWbAlloc(const struct AcmDevice * acm)39 static int32_t AcmWbAlloc(const struct AcmDevice *acm)
40 {
41 struct AcmWb *wb = NULL;
42 int32_t i;
43
44 for (i = 0; i < ACM_NW; i++) {
45 wb = (struct AcmWb *)&acm->wb[i];
46 if (!wb->use) {
47 wb->use = 1;
48 wb->len = 0;
49 return i;
50 }
51 }
52 return -1;
53 }
54
UsbSerialAllocFifo(struct DataFifo * fifo,uint32_t size)55 static int32_t UsbSerialAllocFifo(struct DataFifo *fifo, uint32_t size)
56 {
57 if (!DataFifoIsInitialized(fifo)) {
58 void *data = OsalMemAlloc(size);
59 if (data == NULL) {
60 HDF_LOGE("%{public}s:allocate failed", __func__);
61 return HDF_ERR_MALLOC_FAIL;
62 }
63 DataFifoInit(fifo, size, data);
64 }
65 return HDF_SUCCESS;
66 }
67
UsbSerialFreeFifo(const struct DataFifo * fifo)68 static void UsbSerialFreeFifo(const struct DataFifo *fifo)
69 {
70 if (fifo == NULL) {
71 HDF_LOGE("%{public}s:%{public}d fifo is null", __func__, __LINE__);
72 return;
73 }
74
75 if (fifo->data != NULL) {
76 OsalMemFree(fifo->data);
77 }
78
79 DataFifoInit((struct DataFifo *)fifo, 0, NULL);
80 }
81
AcmWbIsAvail(const struct AcmDevice * acm)82 static int32_t AcmWbIsAvail(const struct AcmDevice *acm)
83 {
84 int32_t i, n;
85 n = ACM_NW;
86 OsalMutexLock((struct OsalMutex *)&acm->writeLock);
87 for (i = 0; i < ACM_NW; i++) {
88 n -= acm->wb[i].use;
89 }
90 OsalMutexUnlock((struct OsalMutex *)&acm->writeLock);
91 return n;
92 }
InterfaceIdToHandle(const struct AcmDevice * acm,uint8_t id)93 static UsbInterfaceHandle *InterfaceIdToHandle(const struct AcmDevice *acm, uint8_t id)
94 {
95 UsbInterfaceHandle *devHandle = NULL;
96
97 if (id == 0xFF) {
98 devHandle = acm->ctrDevHandle;
99 } else {
100 for (int32_t i = 0; i < acm->interfaceCnt; i++) {
101 if (acm->iface[i]->info.interfaceIndex == id) {
102 devHandle = acm->devHandle[i];
103 break;
104 }
105 }
106 }
107 return devHandle;
108 }
109
AcmStartWb(struct AcmDevice * acm,struct AcmWb * wb,struct UsbPipeInfo * pipe)110 static int32_t AcmStartWb(struct AcmDevice *acm, struct AcmWb *wb, struct UsbPipeInfo *pipe)
111 {
112 (void)pipe;
113 int32_t rc;
114 struct UsbRequestParams parmas = {};
115 acm->transmitting++;
116 parmas.interfaceId = acm->dataOutPipe->interfaceId;
117 parmas.pipeAddress = acm->dataOutPipe->pipeAddress;
118 parmas.pipeId = acm->dataOutPipe->pipeId;
119 parmas.callback = AcmWriteBulk;
120 parmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
121 parmas.timeout = USB_CTRL_SET_TIMEOUT;
122 parmas.dataReq.numIsoPackets = 0;
123 parmas.userData = (void *)wb;
124 parmas.dataReq.length = wb->len;
125 parmas.dataReq.buffer = wb->buf;
126 rc = UsbFillRequest(wb->request, InterfaceIdToHandle(acm, acm->dataOutPipe->interfaceId), &parmas);
127 if (rc != HDF_SUCCESS) {
128 HDF_LOGE("%{public}s:UsbFillRequest failed, ret=%{public}d ", __func__, rc);
129 return rc;
130 }
131 acm->writeReq = wb->request;
132 rc = UsbSubmitRequestAsync(wb->request);
133 if (rc < 0) {
134 HDF_LOGE("UsbSubmitRequestAsync failed, ret=%{public}d ", rc);
135 wb->use = 0;
136 acm->transmitting--;
137 }
138 return rc;
139 }
140
AcmStartWbSync(struct AcmDevice * acm,struct AcmWb * wb,struct UsbPipeInfo * pipe)141 static int32_t AcmStartWbSync(struct AcmDevice *acm, struct AcmWb *wb, struct UsbPipeInfo *pipe)
142 {
143 (void)pipe;
144 int32_t rc;
145 struct UsbRequestParams parmas = {};
146 parmas.interfaceId = acm->dataOutPipe->interfaceId;
147 parmas.pipeAddress = acm->dataOutPipe->pipeAddress;
148 parmas.pipeId = acm->dataOutPipe->pipeId;
149 parmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
150 parmas.timeout = USB_CTRL_SET_TIMEOUT;
151 parmas.dataReq.numIsoPackets = 0;
152 parmas.userData = (void *)wb;
153 parmas.dataReq.length = wb->len;
154 parmas.dataReq.buffer = wb->buf;
155 parmas.callback = NULL;
156 rc = UsbFillRequest(wb->request, InterfaceIdToHandle(acm, acm->dataOutPipe->interfaceId), &parmas);
157 if (rc != HDF_SUCCESS) {
158 HDF_LOGE("%{public}s:UsbFillRequest failed, ret = %{public}d", __func__, rc);
159 return rc;
160 }
161 acm->writeReq = wb->request;
162 rc = UsbSubmitRequestSync(wb->request);
163 if (rc < 0) {
164 HDF_LOGE("UsbSubmitRequestSync failed, ret = %{public}d", rc);
165 }
166 wb->use = 0;
167 return rc;
168 }
169
AcmWriteBufAlloc(const struct AcmDevice * acm)170 static int32_t AcmWriteBufAlloc(const struct AcmDevice *acm)
171 {
172 int32_t i;
173 struct AcmWb *wb;
174 for (wb = (struct AcmWb *)&acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
175 wb->buf = OsalMemCalloc(acm->writeSize);
176 if (!wb->buf) {
177 while (i != 0) {
178 --i;
179 --wb;
180 OsalMemFree(wb->buf);
181 wb->buf = NULL;
182 }
183 return -HDF_ERR_MALLOC_FAIL;
184 }
185 }
186 return 0;
187 }
188
AcmWriteBufFree(struct AcmDevice * acm)189 static void AcmWriteBufFree(struct AcmDevice *acm)
190 {
191 int32_t i;
192 struct AcmWb *wb;
193 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
194 if (wb->buf != NULL) {
195 OsalMemFree(wb->buf);
196 wb->buf = NULL;
197 }
198 }
199 return;
200 }
201
AcmWriteBulk(struct UsbRequest * const req)202 static void AcmWriteBulk(struct UsbRequest * const req)
203 {
204 if (req == NULL) {
205 HDF_LOGE("%{public}s:%{pulib}d req is null!", __func__, __LINE__);
206 goto EXIT;
207 }
208 int32_t status = req->compInfo.status;
209 struct AcmWb *wb = (struct AcmWb *)req->compInfo.userData;
210 switch (status) {
211 case 0:
212 if (wb != NULL) {
213 wb->use = 0;
214 }
215 break;
216 case -ECONNRESET:
217 case -ENOENT:
218 case -ESHUTDOWN:
219 return;
220 default:
221 goto EXIT;
222 }
223 EXIT:
224 return;
225 }
226
UsbControlSetUp(struct UsbControlParams * controlParams)227 static struct UsbControlRequest UsbControlSetUp(struct UsbControlParams *controlParams)
228 {
229 struct UsbControlRequest dr;
230 dr.target = controlParams->target;
231 dr.reqType = controlParams->reqType;
232 dr.directon = controlParams->directon;
233 dr.request = controlParams->request;
234 dr.value = CPU_TO_LE16(controlParams->value);
235 dr.index = CPU_TO_LE16(controlParams->index);
236 dr.buffer = controlParams->data;
237 dr.length = CPU_TO_LE16(controlParams->size);
238 return dr;
239 }
240
UsbGetDescriptor(struct UsbDescriptorParams * descParams)241 static int32_t UsbGetDescriptor(struct UsbDescriptorParams *descParams)
242 {
243 int32_t ret;
244 struct UsbControlParams controlParams = {};
245 struct UsbRequestParams parmas = {};
246 const int32_t offset = 8;
247
248 if ((descParams == NULL) || (descParams->devHandle == NULL) || (descParams->request == NULL) ||
249 (descParams->buf == NULL)) {
250 HDF_LOGE("%{public}s:null pointer failed", __func__);
251 return HDF_ERR_INVALID_PARAM;
252 }
253
254 controlParams.request = USB_DDK_REQ_GET_DESCRIPTOR;
255 controlParams.target = USB_REQUEST_TARGET_DEVICE;
256 controlParams.reqType = USB_REQUEST_TYPE_STANDARD;
257 controlParams.directon = USB_REQUEST_DIR_FROM_DEVICE;
258 controlParams.value = (((uint32_t)(descParams->type)) << offset) + descParams->index;
259 controlParams.index = 0;
260 controlParams.data = descParams->buf;
261 controlParams.size = descParams->size;
262
263 parmas.interfaceId = USB_CTRL_INTERFACE_ID;
264 parmas.pipeAddress = 0;
265 parmas.pipeId = 0;
266 parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
267 parmas.timeout = USB_CTRL_SET_TIMEOUT;
268 parmas.ctrlReq = UsbControlSetUp(&controlParams);
269 parmas.callback = NULL;
270 ret = UsbFillRequest(descParams->request, descParams->devHandle, &parmas);
271 if (ret != HDF_SUCCESS) {
272 HDF_LOGE("%{public}s: UsbFillRequest failed, ret=%{public}d ", __func__, ret);
273 return ret;
274 }
275 ret = UsbSubmitRequestSync(descParams->request);
276 if (ret != HDF_SUCCESS) {
277 HDF_LOGE("UsbSubmitRequestSync failed, ret=%{public}d ", ret);
278 return ret;
279 }
280 ret = memcpy_s(descParams->buf, descParams->size, descParams->request->compInfo.buffer,
281 descParams->request->compInfo.actualLength);
282 if (ret != EOK) {
283 HDF_LOGE("memcpy_s failed, ret=%{public}d", ret);
284 return ret;
285 }
286 return HDF_SUCCESS;
287 }
288
GetDeviceDescriptor(UsbInterfaceHandle * devHandle,struct UsbRequest * request,void * buf,uint16_t size)289 static int32_t GetDeviceDescriptor(UsbInterfaceHandle *devHandle, struct UsbRequest *request, void *buf, uint16_t size)
290 {
291 struct UsbDescriptorParams descParams = {};
292 descParams.devHandle = devHandle;
293 descParams.request = request;
294 descParams.type = USB_DDK_DT_DEVICE;
295 descParams.index = 0;
296 descParams.buf = buf;
297 descParams.size = size;
298 return UsbGetDescriptor(&descParams);
299 }
300
UsbGetStatus(UsbInterfaceHandle * devHandle,struct UsbRequest * request,uint16_t * status)301 static int32_t UsbGetStatus(UsbInterfaceHandle *devHandle, struct UsbRequest *request, uint16_t *status)
302 {
303 int32_t ret;
304 uint16_t ss;
305 struct UsbControlParams controlParams = {};
306 struct UsbRequestParams parmas = {};
307 if (NULL == devHandle || NULL == request) {
308 HDF_LOGE("%{public}s:null pointer failed", __func__);
309 return HDF_ERR_INVALID_PARAM;
310 }
311
312 controlParams.request = USB_DDK_REQ_GET_STATUS;
313 controlParams.target = USB_REQUEST_TARGET_DEVICE;
314 controlParams.reqType = USB_REQUEST_TYPE_STANDARD;
315 controlParams.directon = USB_REQUEST_DIR_FROM_DEVICE;
316 controlParams.value = 0;
317 controlParams.index = 0;
318 controlParams.data = (void *)(&ss);
319 controlParams.size = sizeof(ss);
320
321 parmas.interfaceId = USB_CTRL_INTERFACE_ID;
322 parmas.pipeAddress = 0;
323 parmas.pipeId = 0;
324 parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
325 parmas.timeout = USB_CTRL_SET_TIMEOUT;
326 parmas.ctrlReq = UsbControlSetUp(&controlParams);
327 parmas.callback = NULL;
328 ret = UsbFillRequest(request, devHandle, &parmas);
329 if (ret != HDF_SUCCESS) {
330 HDF_LOGE("%{public}s: UsbFillRequest failed, ret=%{public}d ", __func__, ret);
331 return ret;
332 }
333 ret = UsbSubmitRequestSync(request);
334 if (ret != HDF_SUCCESS) {
335 HDF_LOGE("UsbSubmitRequestSync failed, ret=%{public}d ", ret);
336 return ret;
337 }
338 if (request->compInfo.buffer) {
339 ret = memcpy_s((void *)(&ss), sizeof(ss), request->compInfo.buffer, request->compInfo.actualLength);
340 if (ret != EOK) {
341 HDF_LOGE("memcpy_s failed, ret=%{public}d", ret);
342 return ret;
343 }
344 }
345 *status = LE16_TO_CPU(ss);
346 return HDF_SUCCESS;
347 }
348
UsbGetInterface(const UsbInterfaceHandle * devHandle,const struct UsbRequest * request,const uint8_t * buf)349 static int32_t UsbGetInterface(
350 const UsbInterfaceHandle *devHandle, const struct UsbRequest *request, const uint8_t *buf)
351 {
352 int32_t ret;
353 struct UsbControlParams controlParams = {};
354 struct UsbRequestParams parmas = {};
355 if (NULL == devHandle || NULL == request) {
356 HDF_LOGE("%{public}s:null pointer failed", __func__);
357 return HDF_ERR_INVALID_PARAM;
358 }
359
360 controlParams.request = USB_DDK_REQ_GET_INTERFACE;
361 controlParams.target = USB_REQUEST_TARGET_INTERFACE;
362 controlParams.reqType = USB_REQUEST_TYPE_STANDARD;
363 controlParams.directon = USB_REQUEST_DIR_FROM_DEVICE;
364 controlParams.value = 0;
365 controlParams.index = 0;
366 controlParams.data = (void *)buf;
367 controlParams.size = 1;
368
369 parmas.interfaceId = USB_CTRL_INTERFACE_ID;
370 parmas.pipeAddress = 0;
371 parmas.pipeId = 0;
372 parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
373 parmas.timeout = USB_CTRL_SET_TIMEOUT;
374 parmas.ctrlReq = UsbControlSetUp(&controlParams);
375 parmas.callback = NULL;
376 ret = UsbFillRequest((struct UsbRequest *)request, (UsbInterfaceHandle *)devHandle, &parmas);
377 if (ret != HDF_SUCCESS) {
378 HDF_LOGE("%{public}s: UsbFillRequest failed, ret = %{public}d ", __func__, ret);
379 return ret;
380 }
381 ret = UsbSubmitRequestSync((struct UsbRequest *)request);
382 if (ret != HDF_SUCCESS) {
383 HDF_LOGE("%{public}s: UsbSubmitRequestSync failed, ret = %{public}d ", __func__, ret);
384 return ret;
385 }
386 return HDF_SUCCESS;
387 }
388
UsbGetConfig(const UsbInterfaceHandle * devHandle,const struct UsbRequest * request,const uint8_t * buf)389 static int32_t UsbGetConfig(const UsbInterfaceHandle *devHandle, const struct UsbRequest *request, const uint8_t *buf)
390 {
391 int32_t ret;
392 struct UsbControlParams controlParams = {};
393 struct UsbRequestParams parmas = {};
394 if (NULL == devHandle || NULL == request) {
395 HDF_LOGE("%{public}s:null pointer failed", __func__);
396 return HDF_ERR_INVALID_PARAM;
397 }
398
399 controlParams.request = USB_DDK_REQ_GET_CONFIGURATION;
400 controlParams.target = USB_REQUEST_TARGET_DEVICE;
401 controlParams.reqType = USB_REQUEST_TYPE_STANDARD;
402 controlParams.directon = USB_REQUEST_DIR_FROM_DEVICE;
403 controlParams.value = 0;
404 controlParams.index = 0;
405 controlParams.data = (void *)buf;
406 controlParams.size = 1;
407
408 parmas.interfaceId = USB_CTRL_INTERFACE_ID;
409 parmas.pipeAddress = 0;
410 parmas.pipeId = 0;
411 parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
412 parmas.timeout = USB_CTRL_SET_TIMEOUT;
413 parmas.ctrlReq = UsbControlSetUp(&controlParams);
414 parmas.callback = NULL;
415 ret = UsbFillRequest((struct UsbRequest *)request, (UsbInterfaceHandle *)devHandle, &parmas);
416 if (ret != HDF_SUCCESS) {
417 HDF_LOGE("%{public}s: UsbFillRequest failed, ret=%{public}d ", __func__, ret);
418 return ret;
419 }
420 ret = UsbSubmitRequestSync((struct UsbRequest *)request);
421 if (ret != HDF_SUCCESS) {
422 HDF_LOGE("UsbSubmitRequestSync failed, ret=%{public}d ", ret);
423 return ret;
424 }
425 return HDF_SUCCESS;
426 }
427
SerialCtrlMsg(struct AcmDevice * acm,uint8_t request,uint16_t value,void * buf,uint16_t len)428 static int32_t SerialCtrlMsg(struct AcmDevice *acm, uint8_t request, uint16_t value, void *buf, uint16_t len)
429 {
430 int32_t ret;
431 if (acm == NULL || buf == NULL || acm->intPipe == NULL) {
432 HDF_LOGE("%{public}s:invalid param", __func__);
433 return HDF_ERR_IO;
434 }
435 uint16_t index = acm->intPipe->interfaceId;
436 struct UsbControlParams controlParams = {};
437 struct UsbRequestParams parmas = {};
438 if (acm->ctrlReq == NULL) {
439 acm->ctrlReq = UsbAllocRequest(acm->ctrDevHandle, 0, len);
440 if (acm->ctrlReq == NULL) {
441 HDF_LOGE("%{public}s: UsbAllocRequest failed", __func__);
442 return HDF_ERR_IO;
443 }
444 }
445
446 controlParams.request = request;
447 controlParams.target = USB_REQUEST_TARGET_INTERFACE;
448 controlParams.reqType = USB_REQUEST_TYPE_CLASS;
449 controlParams.directon = USB_REQUEST_DIR_TO_DEVICE;
450 controlParams.value = value;
451 controlParams.index = index;
452 controlParams.data = buf;
453 controlParams.size = len;
454
455 parmas.interfaceId = USB_CTRL_INTERFACE_ID;
456 if (acm->ctrPipe != NULL) {
457 parmas.pipeAddress = acm->ctrPipe->pipeAddress;
458 parmas.pipeId = acm->ctrPipe->pipeId;
459 }
460 parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
461 parmas.timeout = USB_CTRL_SET_TIMEOUT;
462 parmas.ctrlReq = UsbControlSetUp(&controlParams);
463 parmas.callback = NULL;
464 ret = UsbFillRequest(acm->ctrlReq, acm->ctrDevHandle, &parmas);
465 if (ret != HDF_SUCCESS) {
466 HDF_LOGE("%{public}s: UsbFillRequest failed, ret = %{public}d ", __func__, ret);
467 return ret;
468 }
469 ret = UsbSubmitRequestSync(acm->ctrlReq);
470 if (ret != HDF_SUCCESS) {
471 HDF_LOGE("UsbSubmitRequestSync failed, ret = %{public}d ", ret);
472 return ret;
473 }
474 if (!acm->ctrlReq->compInfo.status) {
475 HDF_LOGE("%{public}s status=%{public}d ", __func__, acm->ctrlReq->compInfo.status);
476 }
477 return HDF_SUCCESS;
478 }
479
SerialCtrlAsyncMsg(UsbInterfaceHandle * devHandle,struct UsbRequest * request,void * buf,uint16_t size)480 static int32_t SerialCtrlAsyncMsg(UsbInterfaceHandle *devHandle, struct UsbRequest *request, void *buf, uint16_t size)
481 {
482 const int32_t offset = 8;
483 struct UsbControlParams controlParams = {};
484 struct UsbRequestParams parmas = {};
485 if (NULL == devHandle || NULL == request || NULL == buf) {
486 HDF_LOGE("%{public}s:null pointer failed", __func__);
487 return HDF_ERR_INVALID_PARAM;
488 }
489
490 controlParams.request = USB_DDK_REQ_GET_DESCRIPTOR;
491 controlParams.target = USB_REQUEST_TARGET_DEVICE;
492 controlParams.reqType = USB_REQUEST_TYPE_STANDARD;
493 controlParams.directon = USB_REQUEST_DIR_FROM_DEVICE;
494 controlParams.value = (((uint8_t)USB_DDK_DT_DEVICE) << offset);
495 controlParams.index = 0;
496 controlParams.data = buf;
497 controlParams.size = size;
498
499 parmas.interfaceId = USB_CTRL_INTERFACE_ID;
500 parmas.pipeAddress = 0;
501 parmas.pipeId = 0;
502 parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
503 parmas.timeout = USB_CTRL_SET_TIMEOUT;
504 parmas.ctrlReq = UsbControlSetUp(&controlParams);
505 int32_t ret = UsbFillRequest(request, devHandle, &parmas);
506 if (ret != HDF_SUCCESS) {
507 HDF_LOGE("%{public}s: UsbFillRequest failed, ret=%{public}d ", __func__, ret);
508 return ret;
509 }
510 ret = UsbSubmitRequestAsync(request);
511 if (ret != HDF_SUCCESS) {
512 HDF_LOGE("UsbRequestSubmitAsync failed, ret=%{public}d ", ret);
513 return ret;
514 }
515 OsalMSleep(500);
516 HDF_LOGE("SerialCtrlAsyncMsg length%{public}d ", request->compInfo.actualLength);
517 for (unsigned int i = 0; i < request->compInfo.actualLength; i++) {
518 HDF_LOGE("0x%{public}02x", ((uint8_t *)(request->compInfo.buffer))[i]);
519 }
520 ret = memcpy_s(buf, size, request->compInfo.buffer, request->compInfo.actualLength);
521 if (ret != EOK) {
522 HDF_LOGE("memcpy_s failed");
523 }
524 return HDF_SUCCESS;
525 }
526
UsbSerialDeviceAlloc(struct AcmDevice * acm)527 static int32_t UsbSerialDeviceAlloc(struct AcmDevice *acm)
528 {
529 struct SerialDevice *port = NULL;
530 if (acm == NULL) {
531 HDF_LOGE("%{public}s: acm null pointer", __func__);
532 return HDF_FAILURE;
533 }
534 port = (struct SerialDevice *)OsalMemCalloc(sizeof(*port));
535 if (port == NULL) {
536 HDF_LOGE("%{public}s: Alloc usb serial port failed", __func__);
537 return HDF_FAILURE;
538 }
539 if (OsalMutexInit(&port->lock) != HDF_SUCCESS) {
540 OsalMemFree(port);
541 port = NULL;
542 HDF_LOGE("%{public}s: init lock failed!", __func__);
543 return HDF_FAILURE;
544 }
545 port->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
546 port->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
547 port->lineCoding.bParityType = USB_CDC_NO_PARITY;
548 port->lineCoding.bDataBits = DATA_BITS_LENGTH;
549 acm->lineCoding = port->lineCoding;
550 acm->port = port;
551 port->acm = acm;
552 return HDF_SUCCESS;
553 }
554
UsbSeriaDevicelFree(struct AcmDevice * acm)555 static void UsbSeriaDevicelFree(struct AcmDevice *acm)
556 {
557 struct SerialDevice *port = acm->port;
558 if (port == NULL) {
559 HDF_LOGE("%{public}s: port is null", __func__);
560 return;
561 }
562 OsalMemFree(port);
563 port = NULL;
564 }
565
UsbSerialRead(struct SerialDevice * port,struct HdfSBuf * reply)566 static int32_t UsbSerialRead(struct SerialDevice *port, struct HdfSBuf *reply)
567 {
568 uint32_t len;
569 int32_t ret = HDF_SUCCESS;
570 struct AcmDevice *acm = port->acm;
571
572 for (int32_t i = 0; i < ACM_NR; i++) {
573 if (acm->readReq[i]->compInfo.status != USB_REQUEST_COMPLETED) {
574 HDF_LOGE("%{public}s:%{public}d i=%{public}d status=%{public}d!",
575 __func__, __LINE__, i, acm->readReq[i]->compInfo.status);
576 return HDF_FAILURE;
577 }
578 }
579
580 OsalMutexLock(&acm->readLock);
581
582 if (g_acmReadBuffer == NULL) {
583 OsalMutexUnlock(&acm->readLock);
584 HDF_LOGE("%{public}s:%{public}d g_acmReadBuffer is null", __func__, __LINE__);
585 return HDF_ERR_MALLOC_FAIL;
586 }
587
588 ret = memset_s(g_acmReadBuffer, READ_BUF_SIZE, 0, READ_BUF_SIZE);
589 if (ret != HDF_SUCCESS) {
590 HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
591 return ret;
592 }
593
594 if (DataFifoIsEmpty(&port->readFifo)) {
595 ret = HDF_SUCCESS;
596 goto OUT;
597 }
598
599 len = DataFifoRead(&port->readFifo, g_acmReadBuffer, DataFifoLen(&port->readFifo));
600 if (len == 0) {
601 HDF_LOGE("%{public}s:%{public}d no data", __func__, __LINE__);
602 ret = HDF_SUCCESS;
603 goto OUT;
604 }
605 OUT:
606 if (!HdfSbufWriteString(reply, (const char *)g_acmReadBuffer)) {
607 HDF_LOGE("%{public}s:%{public}d sbuf write buffer failed", __func__, __LINE__);
608 ret = HDF_ERR_IO;
609 }
610
611 OsalMutexUnlock(&acm->readLock);
612 return ret;
613 }
614
SerialSetBaudrate(struct SerialDevice * port,const struct HdfSBuf * data)615 static int32_t SerialSetBaudrate(struct SerialDevice *port, const struct HdfSBuf *data)
616 {
617 struct AcmDevice *acm = port->acm;
618 uint32_t baudRate = 0;
619
620 if (!HdfSbufReadUint32((struct HdfSBuf *)data, &baudRate)) {
621 HDF_LOGE("%{public}s: sbuf read buffer failed", __func__);
622 return HDF_ERR_IO;
623 }
624 port->lineCoding.dwDTERate = CPU_TO_LE32(baudRate);
625 if (memcmp(&acm->lineCoding, &port->lineCoding, sizeof(struct UsbCdcLineCoding))) {
626 int32_t ret =
627 memcpy_s(&acm->lineCoding, sizeof(struct UsbCdcLineCoding), &port->lineCoding, sizeof(port->lineCoding));
628 if (ret != EOK) {
629 HDF_LOGE("memcpy_s failed, ret = %{public}d", ret);
630 }
631 HDF_LOGE("%{public}s - set line: %{public}d %{public}d %{public}d %{public}d",
632 __func__, (port->lineCoding.dwDTERate), port->lineCoding.bCharFormat,
633 port->lineCoding.bParityType, port->lineCoding.bDataBits);
634 ret = SerialCtrlMsg(acm, USB_DDK_CDC_REQ_SET_LINE_CODING, 0, &acm->lineCoding, sizeof(struct UsbCdcLineCoding));
635 if (ret != HDF_SUCCESS) {
636 HDF_LOGE("SerialCtrlMsg failed");
637 return ret;
638 }
639 }
640 return HDF_SUCCESS;
641 }
642
UsbCtrlMsg(struct SerialDevice * port,struct HdfSBuf * data)643 static int32_t UsbCtrlMsg(struct SerialDevice *port, struct HdfSBuf *data)
644 {
645 (void)data;
646 int32_t ret;
647 struct AcmDevice *acm = port->acm;
648 struct UsbCdcLineCoding lineCoding = {
649 .dwDTERate = CPU_TO_LE32(DATARATE),
650 .bCharFormat = USB_CDC_1_STOP_BITS,
651 .bParityType = USB_CDC_NO_PARITY,
652 .bDataBits = DATA_BITS_LENGTH,
653 };
654 ret = SerialCtrlMsg(acm, USB_DDK_CDC_REQ_SET_LINE_CODING, 0, &lineCoding, sizeof(struct UsbCdcLineCoding));
655 if (ret) {
656 HDF_LOGE("SerialCtrlMsg failed.");
657 return ret;
658 }
659 return ret;
660 }
661
SerialGetBaudrate(struct SerialDevice * port,struct HdfSBuf * reply)662 static int32_t SerialGetBaudrate(struct SerialDevice *port, struct HdfSBuf *reply)
663 {
664 uint32_t baudRate = LE32_TO_CPU(port->lineCoding.dwDTERate);
665 if (!HdfSbufWriteUint32(reply, baudRate)) {
666 HDF_LOGE("%{public}s:%{public}d sbuf write buffer failed", __func__, __LINE__);
667 return HDF_ERR_IO;
668 }
669
670 HDF_LOGE("%{public}s:%{public}d baudRate = %{public}u", __func__, __LINE__, baudRate);
671 return HDF_SUCCESS;
672 }
673
UsbSerialReadSync(const struct SerialDevice * port,const struct HdfSBuf * reply)674 static int32_t UsbSerialReadSync(const struct SerialDevice *port, const struct HdfSBuf *reply)
675 {
676 struct AcmDevice *acm = port->acm;
677 uint8_t *data = NULL;
678 struct UsbRequestParams readParmas = {};
679 if (g_syncRequest == NULL) {
680 g_syncRequest = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize);
681 if (g_syncRequest == NULL) {
682 return HDF_ERR_MALLOC_FAIL;
683 }
684 }
685 readParmas.pipeAddress = acm->dataInPipe->pipeAddress;
686 readParmas.pipeId = acm->dataInPipe->pipeId;
687 readParmas.interfaceId = acm->dataInPipe->interfaceId;
688 readParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
689 readParmas.timeout = USB_CTRL_SET_TIMEOUT;
690 readParmas.dataReq.numIsoPackets = 0;
691 readParmas.dataReq.directon = (((uint8_t)acm->dataInPipe->pipeDirection) >> USB_DIR_OFFSET) & DIRECTION_MASK;
692 readParmas.dataReq.length = acm->readSize;
693 readParmas.callback = NULL;
694 int32_t ret = UsbFillRequest(g_syncRequest, InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParmas);
695 if (ret != HDF_SUCCESS) {
696 return ret;
697 }
698 ret = UsbSubmitRequestSync(g_syncRequest);
699 if (ret != HDF_SUCCESS) {
700 return ret;
701 }
702 uint32_t count = g_syncRequest->compInfo.actualLength;
703 data = (uint8_t *)OsalMemCalloc(count + 1);
704 if (data == NULL) {
705 return HDF_FAILURE;
706 }
707
708 ret = memcpy_s(data, g_syncRequest->compInfo.actualLength, g_syncRequest->compInfo.buffer, count);
709 if (ret != EOK) {
710 OsalMemFree(data);
711 data = NULL;
712 HDF_LOGE("memcpy_s error %{public}s, %{public}d", __func__, __LINE__);
713 return HDF_FAILURE;
714 }
715
716 if (!HdfSbufWriteString((struct HdfSBuf *)reply, (const char *)data)) {
717 HDF_LOGE("%{public}s:%{public}d sbuf write buffer failed", __func__, __LINE__);
718 }
719
720 OsalMemFree(data);
721 data = NULL;
722
723 return HDF_SUCCESS;
724 }
725
UsbStdCtrlCmd(struct SerialDevice * port,SerialOPCmd cmd,struct HdfSBuf * reply)726 static int32_t UsbStdCtrlCmd(struct SerialDevice *port, SerialOPCmd cmd, struct HdfSBuf *reply)
727 {
728 int32_t ret;
729 static uint16_t ss;
730 static uint8_t data;
731 static uint8_t id;
732 char str[STR_LEN] = {};
733 static struct UsbDeviceDescriptor des = {};
734 struct AcmDevice *acm = port->acm;
735 struct UsbRequestParams parmas = {};
736 parmas.interfaceId = USB_CTRL_INTERFACE_ID;
737 parmas.pipeAddress = 0;
738 parmas.pipeId = 0;
739 parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
740 parmas.timeout = USB_CTRL_SET_TIMEOUT;
741 if (g_ctrlCmdRequest == NULL) {
742 g_ctrlCmdRequest = UsbAllocRequest(acm->ctrDevHandle, 0, acm->readSize);
743 if (g_ctrlCmdRequest == NULL) {
744 HDF_LOGE("ctrlRequest request failed");
745 return HDF_ERR_MALLOC_FAIL;
746 }
747 }
748 switch (cmd) {
749 case CMD_STD_CTRL_GET_DESCRIPTOR_CMD:
750 ret = GetDeviceDescriptor(acm->ctrDevHandle, g_ctrlCmdRequest, (void *)(&des), sizeof(des));
751 if (ret != HDF_SUCCESS) {
752 HDF_LOGE("GetDeviceDescriptor failed ret:%{public}d", ret);
753 return HDF_FAILURE;
754 }
755 (void)snprintf_s(str, STR_LEN, STR_LEN - 1, "device descriptor info:[0x%04x 0x%04x 0x%02x 0x%02x 0x%02x]\n",
756 des.idVendor, des.idProduct, des.bDeviceClass, des.bDeviceSubClass, des.bDeviceProtocol);
757 if (!HdfSbufWriteString(reply, (const char *)str)) {
758 HDF_LOGE("%{public}s: sbuf write buffer failed", __func__);
759 return HDF_FAILURE;
760 }
761 for (unsigned int i = 0; i < sizeof(des); i++) {
762 HDF_LOGI("%{public}s: i is %{public}u", __func__, i);
763 }
764 break;
765 case CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC:
766 ret = SerialCtrlAsyncMsg(acm->ctrDevHandle, g_ctrlCmdRequest, (void *)(&des), sizeof(des));
767 if (ret != HDF_SUCCESS) {
768 HDF_LOGE("GetDeviceDescriptor async fail ret:%{public}d", ret);
769 return HDF_FAILURE;
770 }
771 (void)snprintf_s(str, STR_LEN, STR_LEN - 1, "device descriptor info:[0x%04x 0x%04x 0x%02x 0x%02x 0x%02x]\n",
772 des.idVendor, des.idProduct, des.bDeviceClass, des.bDeviceSubClass, des.bDeviceProtocol);
773 if (!HdfSbufWriteString(reply, (const char *)str)) {
774 HDF_LOGE("%{public}s: sbuf write buffer failed", __func__);
775 return HDF_FAILURE;
776 }
777 for (unsigned int i = 0; i < sizeof(des); i++) {
778 HDF_LOGI("%{public}s: i is %{public}u", __func__, i);
779 }
780 break;
781 case CMD_STD_CTRL_GET_STATUS_CMD:
782 ret = UsbGetStatus(acm->ctrDevHandle, g_ctrlCmdRequest, &ss);
783 if (ret != HDF_SUCCESS) {
784 HDF_LOGE("UsbGetStatus failed ret:%{public}d", ret);
785 return HDF_FAILURE;
786 }
787 ret = HdfSbufWriteUint16(reply, ss);
788 break;
789 case CMD_STD_CTRL_GET_CONFIGURATION:
790 ret = UsbGetConfig(acm->ctrDevHandle, g_ctrlCmdRequest, &data);
791 if (ret != HDF_SUCCESS) {
792 HDF_LOGE("UsbGetStatus failed ret:%{public}d", ret);
793 return HDF_FAILURE;
794 }
795 ret = HdfSbufWriteUint8(reply, data);
796 break;
797 case CMD_STD_CTRL_GET_INTERFACE:
798 ret = UsbGetInterface(acm->ctrDevHandle, g_ctrlCmdRequest, &id);
799 if (ret != HDF_SUCCESS) {
800 HDF_LOGE("UsbGetStatus failed ret:%{public}d", ret);
801 return HDF_FAILURE;
802 }
803 ret = HdfSbufWriteUint8(reply, id);
804 break;
805 default:
806 ret = -1;
807 break;
808 }
809 if (!ret) {
810 HDF_LOGE("cmd:%{public}d ret:%{public}d", cmd, ret);
811 }
812 return ret;
813 }
814
SerialWriteSync(const struct SerialDevice * port,const struct HdfSBuf * data)815 static int32_t SerialWriteSync(const struct SerialDevice *port, const struct HdfSBuf *data)
816 {
817 uint32_t size;
818 int32_t ret;
819 const char *tmp = NULL;
820 int32_t wbn;
821 struct AcmWb *wb = NULL;
822 if (port == NULL || data == NULL) {
823 HDF_LOGE("%{public}d: invalid param", __LINE__);
824 return HDF_ERR_INVALID_PARAM;
825 }
826 struct AcmDevice *acm = port->acm;
827 if (acm == NULL) {
828 HDF_LOGE("%{public}d: invalid param", __LINE__);
829 return HDF_ERR_INVALID_PARAM;
830 }
831 if (AcmWbIsAvail(acm)) {
832 wbn = AcmWbAlloc(acm);
833 } else {
834 HDF_LOGE("no write buf");
835 return 0;
836 }
837 if (wbn >= ACM_NW || wbn < 0) {
838 wbn = 0;
839 }
840 wb = &acm->wb[wbn];
841 if (wb == NULL) {
842 return HDF_ERR_INVALID_PARAM;
843 }
844 tmp = HdfSbufReadString((struct HdfSBuf *)data);
845 if (tmp == NULL) {
846 HDF_LOGE("%{public}s: sbuf read buffer failed", __func__);
847 return HDF_ERR_IO;
848 }
849 size = (uint32_t)strlen(tmp) + 1;
850 size = (size > acm->writeSize) ? acm->writeSize : size;
851 ret = memcpy_s(wb->buf, (size_t)acm->writeSize, tmp, (size_t)size);
852 if (ret != EOK) {
853 HDF_LOGE("memcpy_s failed, ret = %{public}d", ret);
854 }
855 wb->len = size;
856 if (acm->dataOutPipe == NULL) {
857 return HDF_ERR_INVALID_PARAM;
858 }
859
860 ret = AcmStartWbSync(acm, wb, acm->dataOutPipe);
861 if (ret != HDF_SUCCESS) {
862 HDF_LOGE("%{public}s: AcmStartWbSync failed, ret=%{public}d", __func__, ret);
863 return HDF_FAILURE;
864 }
865
866 return (int32_t)size;
867 }
868
FreeMem(void)869 static void FreeMem(void)
870 {
871 if (g_acmReadBuffer == NULL) {
872 return;
873 } else {
874 OsalMemFree(g_acmReadBuffer);
875 g_acmReadBuffer = NULL;
876 }
877 }
878
SerialOpen(const struct SerialDevice * port,struct HdfSBuf * data)879 static int32_t SerialOpen(const struct SerialDevice *port, struct HdfSBuf *data)
880 {
881 int32_t cmdType = HOST_ACM_ASYNC_READ;
882
883 if ((port == NULL) || (data == NULL)) {
884 HDF_LOGE("%{public}s: port or data is null", __func__);
885 return HDF_ERR_INVALID_PARAM;
886 }
887 struct AcmDevice *acm = port->acm;
888 if (acm == NULL) {
889 HDF_LOGE("%{public}s: acm is null", __func__);
890 return HDF_ERR_INVALID_PARAM;
891 }
892
893 if (!HdfSbufReadInt32(data, &cmdType)) {
894 HDF_LOGE("%{public}s: sbuf read cmdType failed", __func__);
895 return HDF_ERR_INVALID_PARAM;
896 }
897
898 if (AcmInit(acm) != HDF_SUCCESS) {
899 HDF_LOGE("%{public}s: AcmInit failed", __func__);
900 return HDF_FAILURE;
901 }
902
903 if (cmdType != HOST_ACM_ASYNC_READ) {
904 HDF_LOGD("%{public}s: asyncRead success", __func__);
905 return HDF_SUCCESS;
906 }
907
908 if (g_acmReadBuffer == NULL) {
909 g_acmReadBuffer = (uint8_t *)OsalMemCalloc(READ_BUF_SIZE);
910 if (g_acmReadBuffer == NULL) {
911 HDF_LOGE("%{public}s: OsalMemCalloc g_acmReadBuffer error", __func__);
912 return HDF_ERR_MALLOC_FAIL;
913 }
914 }
915
916 int32_t ret = UsbSerialAllocFifo((struct DataFifo *)&port->readFifo, READ_BUF_SIZE);
917 if (ret != HDF_SUCCESS) {
918 FreeMem();
919 HDF_LOGE("%{public}s: UsbSerialAllocFifo failed", __func__);
920 return HDF_ERR_INVALID_PARAM;
921 }
922 for (int32_t i = 0; i < ACM_NR; i++) {
923 ret = UsbSubmitRequestAsync(acm->readReq[i]);
924 if (ret != HDF_SUCCESS) {
925 HDF_LOGE("%{public}s: UsbSubmitRequestAsync failed", __func__);
926 goto ERR;
927 }
928 }
929 return HDF_SUCCESS;
930 ERR:
931 FreeMem();
932 UsbSerialFreeFifo((struct DataFifo *)&port->readFifo);
933 return ret;
934 }
935
SerialClose(const struct SerialDevice * port,struct HdfSBuf * data)936 static int32_t SerialClose(const struct SerialDevice *port, struct HdfSBuf *data)
937 {
938 int32_t cmdType = HOST_ACM_SYNC_READ;
939 struct AcmDevice *acm = NULL;
940
941 if ((port == NULL) || (data == NULL)) {
942 HDF_LOGE("%{public}s: invalid param", __func__);
943 return HDF_ERR_INVALID_PARAM;
944 }
945 acm = port->acm;
946 if (acm == NULL) {
947 HDF_LOGE("%{public}s: invalid param", __func__);
948 return HDF_ERR_INVALID_PARAM;
949 }
950
951 if (!HdfSbufReadInt32(data, &cmdType)) {
952 HDF_LOGE("%{public}s:%{public}d sbuf read cmdType failed", __func__, __LINE__);
953 return HDF_ERR_INVALID_PARAM;
954 }
955
956 if ((cmdType == HOST_ACM_SYNC_READ) || (cmdType == HOST_ACM_SYNC_WRITE) || (cmdType == HOST_ACM_ASYNC_WRITE) ||
957 (cmdType == HOST_ACM_ADD_INTERFACE) || (cmdType == HOST_ACM_REMOVE_INTERFACE)) {
958 HDF_LOGD("%{public}s:%{public}d cmdType=%{public}d success", __func__, __LINE__, cmdType);
959 return HDF_SUCCESS;
960 }
961
962 if (g_acmReadBuffer != NULL) {
963 OsalMemFree(g_acmReadBuffer);
964 g_acmReadBuffer = NULL;
965 }
966
967 UsbSerialFreeFifo((struct DataFifo *)&port->readFifo);
968 AcmRelease(acm);
969 return HDF_SUCCESS;
970 }
971
SerialWrite(const struct SerialDevice * port,struct HdfSBuf * data)972 static int32_t SerialWrite(const struct SerialDevice *port, struct HdfSBuf *data)
973 {
974 uint32_t size;
975 int32_t ret;
976 const char *tmp = NULL;
977
978 int32_t wbn;
979 struct AcmWb *wb = NULL;
980 if (port == NULL) {
981 HDF_LOGE("%{public}d: invalid param", __LINE__);
982 return HDF_ERR_INVALID_PARAM;
983 }
984 struct AcmDevice *acm = port->acm;
985 if (acm == NULL) {
986 HDF_LOGE("%{public}d: invalid param", __LINE__);
987 return HDF_ERR_INVALID_PARAM;
988 }
989 if (AcmWbIsAvail(acm)) {
990 wbn = AcmWbAlloc(acm);
991 } else {
992 HDF_LOGE("no write buf");
993 return 0;
994 }
995 if (wbn < 0) {
996 HDF_LOGE("AcmWbAlloc failed");
997 return HDF_FAILURE;
998 }
999 wb = &acm->wb[wbn];
1000
1001 tmp = HdfSbufReadString(data);
1002 if (tmp == NULL) {
1003 HDF_LOGE("%{public}s: sbuf read buffer failed", __func__);
1004 return HDF_ERR_IO;
1005 }
1006 size = (uint32_t)strlen(tmp) + 1;
1007 size = (size > acm->writeSize) ? acm->writeSize : size;
1008 ret = memcpy_s(wb->buf, (size_t)acm->writeSize, tmp, (size_t)size);
1009 if (ret != EOK) {
1010 HDF_LOGE("memcpy_s failed, ret = %{public}d", ret);
1011 }
1012 wb->len = size;
1013
1014 ret = AcmStartWb(acm, wb, acm->dataOutPipe);
1015 if (ret != HDF_SUCCESS) {
1016 HDF_LOGE("%{public}s: AcmStartWb failed, ret=%{public}d", __func__, ret);
1017 return HDF_FAILURE;
1018 }
1019 return (int32_t)size;
1020 }
1021
SerialAddOrRemoveInterface(int32_t cmd,const struct SerialDevice * port,const struct HdfSBuf * data)1022 static int32_t SerialAddOrRemoveInterface(int32_t cmd, const struct SerialDevice *port, const struct HdfSBuf *data)
1023 {
1024 struct AcmDevice *acm = port->acm;
1025 UsbInterfaceStatus status = 0;
1026 uint32_t index = 0;
1027
1028 if (!HdfSbufReadUint32((struct HdfSBuf *)data, &index)) {
1029 HDF_LOGE("%{public}s:%{public}d sbuf read interfaceNum failed", __func__, __LINE__);
1030 return HDF_ERR_INVALID_PARAM;
1031 }
1032
1033 if (cmd == CMD_ADD_INTERFACE) {
1034 status = USB_INTERFACE_STATUS_ADD;
1035 } else if (cmd == CMD_REMOVE_INTERFACE) {
1036 status = USB_INTERFACE_STATUS_REMOVE;
1037 } else {
1038 HDF_LOGE("%{public}s:%{public}d cmd=% is not define", __func__, __LINE__, cmd);
1039 return HDF_ERR_INVALID_PARAM;
1040 }
1041
1042 return UsbAddOrRemoveInterface(acm->session, acm->busNum, acm->devAddr, index, status);
1043 }
1044
UsbSerialCheckCmd(struct SerialDevice * port,int32_t cmd,struct HdfSBuf * data,const struct HdfSBuf * reply)1045 static int32_t UsbSerialCheckCmd(
1046 struct SerialDevice *port, int32_t cmd, struct HdfSBuf *data, const struct HdfSBuf *reply)
1047 {
1048 switch (cmd) {
1049 case CMD_OPEN_PARM:
1050 return SerialOpen(port, data);
1051 case CMD_CLOSE_PARM:
1052 return SerialClose(port, data);
1053 case CMD_WRITE_PARM:
1054 return SerialWrite(port, data);
1055 case CMD_READ_PARM:
1056 return UsbSerialRead(port, (struct HdfSBuf *)reply);
1057 case CMD_GET_BAUDRATE:
1058 return SerialGetBaudrate(port, (struct HdfSBuf *)reply);
1059 case CMD_SET_BAUDRATE:
1060 return SerialSetBaudrate(port, (struct HdfSBuf *)data);
1061 case CMD_WRITE_DATA_SYNC:
1062 return SerialWriteSync(port, data);
1063 case CMD_READ_DATA_SYNC:
1064 return UsbSerialReadSync(port, (struct HdfSBuf *)reply);
1065 case CMD_CLASS_CTRL_SYNC:
1066 return UsbCtrlMsg(port, (struct HdfSBuf *)reply);
1067 case CMD_STD_CTRL_GET_DESCRIPTOR_CMD:
1068 return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_DESCRIPTOR_CMD, (struct HdfSBuf *)reply);
1069 case CMD_STD_CTRL_GET_STATUS_CMD:
1070 return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_STATUS_CMD, (struct HdfSBuf *)reply);
1071 case CMD_STD_CTRL_GET_CONFIGURATION:
1072 return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_CONFIGURATION, (struct HdfSBuf *)reply);
1073 case CMD_STD_CTRL_GET_INTERFACE:
1074 return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_INTERFACE, (struct HdfSBuf *)reply);
1075 case CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC:
1076 return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC, (struct HdfSBuf *)reply);
1077 case CMD_ADD_INTERFACE:
1078 case CMD_REMOVE_INTERFACE:
1079 return SerialAddOrRemoveInterface(cmd, port, data);
1080 default:
1081 return HDF_ERR_NOT_SUPPORT;
1082 }
1083 }
1084
UsbSerialDeviceDispatch(struct HdfDeviceIoClient * client,int32_t cmd,struct HdfSBuf * data,struct HdfSBuf * reply)1085 static int32_t UsbSerialDeviceDispatch(
1086 struct HdfDeviceIoClient *client, int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
1087 {
1088 struct AcmDevice *acm = NULL;
1089 struct SerialDevice *port = NULL;
1090
1091 if (client == NULL) {
1092 HDF_LOGE("%{public}s:%{public}d client is null", __func__, __LINE__);
1093 return HDF_ERR_INVALID_OBJECT;
1094 }
1095 if (client->device == NULL) {
1096 HDF_LOGE("%{public}s:%{public}d client->device is null", __func__, __LINE__);
1097 return HDF_ERR_INVALID_OBJECT;
1098 }
1099 if (client->device->service == NULL) {
1100 HDF_LOGE("%{public}s:%{public}d client->device->service is null", __func__, __LINE__);
1101 return HDF_ERR_INVALID_OBJECT;
1102 }
1103 acm = (struct AcmDevice *)client->device->service;
1104 port = acm->port;
1105 if (port == NULL) {
1106 HDF_LOGE("%{public}s:%{public}d port is null", __func__, __LINE__);
1107 return HDF_ERR_INVALID_OBJECT;
1108 }
1109
1110 if (g_acmReleaseFlag) {
1111 HDF_LOGE("%{public}s:%{public}d g_acmReleaseFlag is true", __func__, __LINE__);
1112 return HDF_FAILURE;
1113 }
1114
1115 return UsbSerialCheckCmd(port, cmd, data, reply);
1116 }
1117
GetUsbInterfaceById(const struct AcmDevice * acm,uint8_t interfaceIndex)1118 static struct UsbInterface *GetUsbInterfaceById(const struct AcmDevice *acm, uint8_t interfaceIndex)
1119 {
1120 return UsbClaimInterface(acm->session, acm->busNum, acm->devAddr, interfaceIndex);
1121 }
1122
AcmFreePipes(struct AcmDevice * acm)1123 static void AcmFreePipes(struct AcmDevice *acm)
1124 {
1125 if (acm == NULL) {
1126 return;
1127 }
1128 if (acm->ctrPipe) {
1129 OsalMemFree(acm->ctrPipe);
1130 acm->ctrPipe = NULL;
1131 }
1132 if (acm->intPipe) {
1133 OsalMemFree(acm->intPipe);
1134 acm->intPipe = NULL;
1135 }
1136 if (acm->dataInPipe) {
1137 OsalMemFree(acm->dataInPipe);
1138 acm->dataInPipe = NULL;
1139 }
1140 if (acm->dataOutPipe) {
1141 OsalMemFree(acm->dataOutPipe);
1142 acm->dataOutPipe = NULL;
1143 }
1144 }
1145
EnumePipe(const struct AcmDevice * acm,uint8_t interfaceIndex,UsbPipeType pipeType,UsbPipeDirection pipeDirection)1146 static struct UsbPipeInfo *EnumePipe(
1147 const struct AcmDevice *acm, uint8_t interfaceIndex, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
1148 {
1149 struct UsbInterfaceInfo *info = NULL;
1150 UsbInterfaceHandle *interfaceHandle = NULL;
1151 if (pipeType == USB_PIPE_TYPE_CONTROL) {
1152 info = &acm->ctrIface->info;
1153 interfaceHandle = acm->ctrDevHandle;
1154 } else {
1155 info = &acm->iface[interfaceIndex]->info;
1156 interfaceHandle = InterfaceIdToHandle(acm, info->interfaceIndex);
1157 }
1158
1159 for (uint8_t i = 0; i <= info->pipeNum; i++) {
1160 struct UsbPipeInfo p;
1161 int32_t ret = UsbGetPipeInfo(interfaceHandle, info->curAltSetting, i, &p);
1162 if (ret < 0) {
1163 continue;
1164 }
1165 if ((p.pipeDirection == pipeDirection) && (p.pipeType == pipeType)) {
1166 struct UsbPipeInfo *pi = OsalMemCalloc(sizeof(*pi));
1167 if (pi == NULL) {
1168 HDF_LOGE("%{public}s: Alloc pipe failed", __func__);
1169 return NULL;
1170 }
1171 p.interfaceId = info->interfaceIndex;
1172 *pi = p;
1173 return pi;
1174 }
1175 }
1176 return NULL;
1177 }
1178
GetPipe(const struct AcmDevice * acm,UsbPipeType pipeType,UsbPipeDirection pipeDirection)1179 static struct UsbPipeInfo *GetPipe(const struct AcmDevice *acm, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
1180 {
1181 uint8_t i;
1182 if (acm == NULL) {
1183 HDF_LOGE("%{public}s: invalid param", __func__);
1184 return NULL;
1185 }
1186 for (i = 0; i < acm->interfaceCnt; i++) {
1187 struct UsbPipeInfo *p = NULL;
1188 if (!acm->iface[i]) {
1189 continue;
1190 }
1191 p = EnumePipe(acm, i, pipeType, pipeDirection);
1192 if (p == NULL) {
1193 continue;
1194 }
1195 return p;
1196 }
1197 return NULL;
1198 }
1199
1200 /* HdfDriverEntry implementations */
UsbSerialDriverBind(struct HdfDeviceObject * device)1201 static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
1202 {
1203 struct UsbPnpNotifyServiceInfo *info = NULL;
1204 errno_t err;
1205 struct AcmDevice *acm = NULL;
1206 if (device == NULL) {
1207 HDF_LOGE("%{public}s: device is null", __func__);
1208 return HDF_ERR_INVALID_OBJECT;
1209 }
1210 acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*acm));
1211 if (acm == NULL) {
1212 HDF_LOGE("%{public}s: Alloc usb serial device failed", __func__);
1213 return HDF_FAILURE;
1214 }
1215 if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) {
1216 HDF_LOGE("%{public}s:%{public}d OsalMutexInit failed", __func__, __LINE__);
1217 goto ERROR;
1218 }
1219 info = (struct UsbPnpNotifyServiceInfo *)device->priv;
1220 if (info != NULL) {
1221 HDF_LOGD("%{public}s:%{public}d busNum=%{public}d,devAddr=%{public}d,interfaceLength=%{public}d",
1222 __func__, __LINE__, info->busNum, info->devNum, info->interfaceLength);
1223 acm->busNum = (uint8_t)info->busNum;
1224 acm->devAddr = (uint8_t)info->devNum;
1225 acm->interfaceCnt = info->interfaceLength;
1226 err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, (const void *)info->interfaceNumber,
1227 info->interfaceLength);
1228 if (err != EOK) {
1229 HDF_LOGE("%{public}s:%{public}d memcpy_s failed err = %{public}d", __func__, __LINE__, err);
1230 goto LOCK_ERROR;
1231 }
1232 } else {
1233 HDF_LOGE("%{public}s:%{public}d info is null!", __func__, __LINE__);
1234 goto LOCK_ERROR;
1235 }
1236 acm->device = device;
1237 device->service = &(acm->service);
1238 acm->device->service->Dispatch = UsbSerialDeviceDispatch;
1239 HDF_LOGD("UsbSerialDriverBind=========================OK");
1240 return HDF_SUCCESS;
1241
1242 LOCK_ERROR:
1243 if (OsalMutexDestroy(&acm->lock)) {
1244 HDF_LOGE("%{public}s:%{public}d OsalMutexDestroy failed", __func__, __LINE__);
1245 }
1246 ERROR:
1247 OsalMemFree(acm);
1248 acm = NULL;
1249 return HDF_FAILURE;
1250 }
1251
AcmProcessNotification(const struct AcmDevice * acm,const unsigned char * buf)1252 static void AcmProcessNotification(const struct AcmDevice *acm, const unsigned char *buf)
1253 {
1254 (void)acm;
1255 struct UsbCdcNotification *dr = (struct UsbCdcNotification *)buf;
1256 switch (dr->bNotificationType) {
1257 case USB_DDK_CDC_NOTIFY_NETWORK_CONNECTION:
1258 HDF_LOGE("%{public}s - network connection: %{public}d", __func__, dr->wValue);
1259 break;
1260 case USB_DDK_CDC_NOTIFY_SERIAL_STATE:
1261 HDF_LOGE("the serial State change");
1262 break;
1263 default:
1264 HDF_LOGE("%{public}s-%{public}d received: index %{public}d len %{public}d",
1265 __func__, dr->bNotificationType, dr->wIndex, dr->wLength);
1266 }
1267 return;
1268 }
1269
AcmCtrlIrqCheckSize(struct UsbRequest * const req,struct AcmDevice * acm,struct UsbCdcNotification * dr)1270 static int32_t AcmCtrlIrqCheckSize(struct UsbRequest * const req, struct AcmDevice *acm, struct UsbCdcNotification *dr)
1271 {
1272 if ((req == NULL) || (acm == NULL) || (dr == NULL)) {
1273 HDF_LOGE("%{public}s:%{public}d Invalid parameter", __func__, __LINE__);
1274 return HDF_ERR_INVALID_PARAM;
1275 }
1276
1277 unsigned int currentSize = req->compInfo.actualLength;
1278 HDF_LOGD("actualLength:%{public}u", currentSize);
1279
1280 unsigned int expectedSize = sizeof(struct UsbCdcNotification) + LE16_TO_CPU(dr->wLength);
1281 if (currentSize < expectedSize) {
1282 if (acm->nbSize < expectedSize) {
1283 if (acm->nbSize) {
1284 OsalMemFree(acm->notificationBuffer);
1285 acm->nbSize = 0;
1286 }
1287 unsigned int allocSize = expectedSize;
1288 acm->notificationBuffer = OsalMemCalloc(allocSize);
1289 if (!acm->notificationBuffer) {
1290 return HDF_ERR_MALLOC_FAIL;
1291 }
1292 acm->nbSize = allocSize;
1293 }
1294 unsigned int copySize = MIN(currentSize, expectedSize - acm->nbIndex);
1295 if (memcpy_s(&acm->notificationBuffer[acm->nbIndex], acm->nbSize - acm->nbIndex, req->compInfo.buffer,
1296 copySize) != EOK) {
1297 HDF_LOGE("%{public}s:%{public}d memcpy_s failed", __func__, __LINE__);
1298 OsalMemFree(acm->notificationBuffer);
1299 acm->notificationBuffer = NULL;
1300 }
1301 acm->nbIndex += copySize;
1302 currentSize = acm->nbIndex;
1303 }
1304
1305 if (currentSize >= expectedSize) {
1306 AcmProcessNotification(acm, (unsigned char *)dr);
1307 acm->nbIndex = 0;
1308 }
1309 return HDF_SUCCESS;
1310 }
1311
AcmCtrlIrq(struct UsbRequest * const req)1312 static void AcmCtrlIrq(struct UsbRequest * const req)
1313 {
1314 if (req == NULL) {
1315 HDF_LOGE("%{public}s:%{public}d req is null!", __func__, __LINE__);
1316 goto EXIT;
1317 }
1318 int32_t ret;
1319 struct AcmDevice *acm = (struct AcmDevice *)req->compInfo.userData;
1320 int32_t status = req->compInfo.status;
1321 HDF_LOGD("Irqstatus:%{public}d", status);
1322
1323 struct UsbCdcNotification *dr = (struct UsbCdcNotification *)req->compInfo.buffer;
1324 if (status != 0) {
1325 goto EXIT;
1326 }
1327
1328 if ((acm != NULL) && acm->nbIndex) {
1329 dr = (struct UsbCdcNotification *)acm->notificationBuffer;
1330 }
1331 if ((dr == NULL) || (acm == NULL)) {
1332 HDF_LOGE("%{public}s:%{public}d dr or acm is null!", __func__, __LINE__);
1333 goto EXIT;
1334 }
1335
1336 ret = AcmCtrlIrqCheckSize(req, acm, dr);
1337 if (ret != HDF_SUCCESS) {
1338 goto EXIT;
1339 }
1340
1341 UsbSubmitRequestAsync(req);
1342
1343 EXIT:
1344 HDF_LOGE("%{public}s:%{public}d exit", __func__, __LINE__);
1345 }
1346
AcmReadBulk(struct UsbRequest * req)1347 static void AcmReadBulk(struct UsbRequest *req)
1348 {
1349 if (req == NULL) {
1350 HDF_LOGE("%{public}s:%{public}d req is null!", __func__, __LINE__);
1351 return;
1352 }
1353 int32_t retval;
1354 int32_t status = req->compInfo.status;
1355 size_t size = req->compInfo.actualLength;
1356 struct AcmDevice *acm = (struct AcmDevice *)req->compInfo.userData;
1357 if (acm == NULL || acm->port == NULL) {
1358 HDF_LOGE("%{public}s:%{public}d acm is null!", __func__, __LINE__);
1359 return;
1360 }
1361
1362 if (status != 0) {
1363 HDF_LOGE("%{public}s: status is not null", __func__);
1364 return;
1365 }
1366 HDF_LOGD("Bulk status: %{public}d+size:%{public}zu", status, size);
1367 if (size == 0) {
1368 uint8_t *data = req->compInfo.buffer;
1369 OsalMutexLock(&acm->readLock);
1370 if (DataFifoIsFull(&acm->port->readFifo)) {
1371 HDF_LOGD("%{public}s: DataFifoIsFull is success", __func__);
1372 DataFifoSkip(&acm->port->readFifo, size);
1373 }
1374 uint32_t count = DataFifoWrite(&acm->port->readFifo, data, size);
1375 if (count != size) {
1376 HDF_LOGW("%{public}s: write %{public}u less than expected %{public}zu", __func__, count, size);
1377 }
1378 OsalMutexUnlock(&acm->readLock);
1379 }
1380
1381 retval = UsbSubmitRequestAsync(req);
1382 if (retval && retval != -EPERM) {
1383 HDF_LOGE("%{public}s - usb_submit_urb failed: %{public}d", __func__, retval);
1384 }
1385 }
1386
AcmFreeWriteRequests(struct AcmDevice * acm)1387 static void AcmFreeWriteRequests(struct AcmDevice *acm)
1388 {
1389 int32_t i;
1390 struct AcmWb *snd = NULL;
1391 for (i = 0; i < ACM_NW; i++) {
1392 snd = &acm->wb[i];
1393 int32_t ret = UsbCancelRequest(snd->request);
1394 if (ret != HDF_SUCCESS) {
1395 HDF_LOGE("UsbCancelRequest rd failed, ret = %{public}d ", ret);
1396 }
1397 }
1398 for (i = 0; i < ACM_NW; i++) {
1399 snd = &acm->wb[i];
1400 if (snd->request != NULL) {
1401 UsbFreeRequest(snd->request);
1402 snd->request = NULL;
1403 }
1404 }
1405 }
1406
AcmFreeReadRequests(struct AcmDevice * acm)1407 static void AcmFreeReadRequests(struct AcmDevice *acm)
1408 {
1409 if (acm == NULL) {
1410 HDF_LOGE("%{public}s: acm is NULL", __func__);
1411 return;
1412 }
1413
1414 int32_t i;
1415 for (i = 0; i < ACM_NR; i++) {
1416 int32_t ret = UsbCancelRequest(acm->readReq[i]);
1417 if (ret != HDF_SUCCESS) {
1418 HDF_LOGE("UsbCancelRequest rd failed, ret=%{public}d ", ret);
1419 }
1420 }
1421 for (i = 0; i < ACM_NR; i++) {
1422 if (acm->readReq[i]) {
1423 UsbFreeRequest(acm->readReq[i]);
1424 acm->readReq[i] = NULL;
1425 }
1426 }
1427 }
1428
AcmFreeNotifyReqeust(struct AcmDevice * acm)1429 static void AcmFreeNotifyReqeust(struct AcmDevice *acm)
1430 {
1431 int32_t ret;
1432
1433 if ((acm == NULL) || (acm->notifyReq == NULL)) {
1434 HDF_LOGE("%{public}s: acm or notifyReq is null", __func__);
1435 return;
1436 }
1437 ret = UsbCancelRequest(acm->notifyReq);
1438 if (ret != HDF_SUCCESS) {
1439 HDF_LOGE("UsbCancelRequest rd failed, ret = %{public}d ", ret);
1440 }
1441 ret = UsbFreeRequest(acm->notifyReq);
1442 if (ret == HDF_SUCCESS) {
1443 acm->notifyReq = NULL;
1444 } else {
1445 HDF_LOGE("%{public}s: AcmFreeNotifyReqeust failed, ret = %{public}d", __func__, ret);
1446 }
1447 }
1448
AcmAllocReadRequests(struct AcmDevice * acm)1449 static int32_t AcmAllocReadRequests(struct AcmDevice *acm)
1450 {
1451 int32_t ret;
1452 struct UsbRequestParams readParmas = {};
1453 for (int32_t i = 0; i < ACM_NR; i++) {
1454 acm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize);
1455 if (!acm->readReq[i]) {
1456 HDF_LOGE("readReq request failed");
1457 goto ERROR;
1458 }
1459 readParmas.userData = (void *)acm;
1460 readParmas.pipeAddress = acm->dataInPipe->pipeAddress;
1461 readParmas.pipeId = acm->dataInPipe->pipeId;
1462 readParmas.interfaceId = acm->dataInPipe->interfaceId;
1463 readParmas.callback = AcmReadBulk;
1464 readParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
1465 readParmas.timeout = USB_CTRL_SET_TIMEOUT;
1466 readParmas.dataReq.numIsoPackets = 0;
1467 readParmas.dataReq.directon = (((uint8_t)acm->dataInPipe->pipeDirection) >> USB_PIPE_DIR_OFFSET) & 0x1;
1468 readParmas.dataReq.length = acm->readSize;
1469 ret = UsbFillRequest(acm->readReq[i], InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParmas);
1470 if (ret != HDF_SUCCESS) {
1471 HDF_LOGE("%{public}s: UsbFillRequest failed, ret=%{public}d ", __func__, ret);
1472 goto ERROR;
1473 }
1474 }
1475 return HDF_SUCCESS;
1476
1477 ERROR:
1478 AcmFreeReadRequests(acm);
1479 return HDF_ERR_MALLOC_FAIL;
1480 }
1481
AcmAllocNotifyRequest(struct AcmDevice * acm)1482 static int32_t AcmAllocNotifyRequest(struct AcmDevice *acm)
1483 {
1484 int32_t ret;
1485 struct UsbRequestParams intParmas = {};
1486 acm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(acm, acm->intPipe->interfaceId), 0, acm->intSize);
1487 if (!acm->notifyReq) {
1488 HDF_LOGE("notifyReq request failed.");
1489 return HDF_ERR_MALLOC_FAIL;
1490 }
1491 intParmas.userData = (void *)acm;
1492 intParmas.pipeAddress = acm->intPipe->pipeAddress;
1493 intParmas.pipeId = acm->intPipe->pipeId;
1494 intParmas.interfaceId = acm->intPipe->interfaceId;
1495 intParmas.callback = AcmCtrlIrq;
1496 intParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
1497 intParmas.timeout = USB_CTRL_SET_TIMEOUT;
1498 intParmas.dataReq.numIsoPackets = 0;
1499 intParmas.dataReq.directon = (((uint8_t)acm->intPipe->pipeDirection) >> USB_PIPE_DIR_OFFSET) & DIRECTION_MASK;
1500 intParmas.dataReq.length = acm->intSize;
1501 ret = UsbFillRequest(acm->notifyReq, InterfaceIdToHandle(acm, acm->intPipe->interfaceId), &intParmas);
1502 if (ret != HDF_SUCCESS) {
1503 HDF_LOGE("%{public}s: UsbFillRequest failed, ret = %{public}d", __func__, ret);
1504 goto ERROR;
1505 }
1506 return HDF_SUCCESS;
1507
1508 ERROR:
1509 AcmFreeNotifyReqeust(acm);
1510 return ret;
1511 }
1512
AcmReleaseInterfaces(struct AcmDevice * acm)1513 static void AcmReleaseInterfaces(struct AcmDevice *acm)
1514 {
1515 for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1516 if (acm->iface[i]) {
1517 UsbReleaseInterface(acm->iface[i]);
1518 acm->iface[i] = NULL;
1519 }
1520 }
1521 if (acm->ctrIface) {
1522 UsbReleaseInterface(acm->ctrIface);
1523 acm->ctrIface = NULL;
1524 }
1525 }
1526
AcmClaimInterfaces(struct AcmDevice * acm)1527 static int32_t AcmClaimInterfaces(struct AcmDevice *acm)
1528 {
1529 for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1530 acm->iface[i] = GetUsbInterfaceById((const struct AcmDevice *)acm, acm->interfaceIndex[i]);
1531 if (acm->iface[i] == NULL) {
1532 HDF_LOGE("%{public}s: interface%{public}d is null", __func__, acm->interfaceIndex[i]);
1533 goto ERROR;
1534 }
1535 }
1536
1537 acm->ctrIface = GetUsbInterfaceById((const struct AcmDevice *)acm, USB_CTRL_INTERFACE_ID);
1538 if (acm->ctrIface == NULL) {
1539 HDF_LOGE("%{public}s: GetUsbInterfaceById null", __func__);
1540 goto ERROR;
1541 }
1542
1543 return HDF_SUCCESS;
1544
1545 ERROR:
1546 AcmReleaseInterfaces(acm);
1547 return HDF_FAILURE;
1548 }
1549
AcmCloseInterfaces(struct AcmDevice * acm)1550 static void AcmCloseInterfaces(struct AcmDevice *acm)
1551 {
1552 for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1553 if (acm->devHandle[i]) {
1554 UsbCloseInterface(acm->devHandle[i], false);
1555 acm->devHandle[i] = NULL;
1556 }
1557 }
1558 if (acm->ctrDevHandle) {
1559 UsbCloseInterface(acm->ctrDevHandle, false);
1560 acm->ctrDevHandle = NULL;
1561 }
1562 }
1563
AcmOpenInterfaces(struct AcmDevice * acm)1564 static int32_t AcmOpenInterfaces(struct AcmDevice *acm)
1565 {
1566 for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1567 if (acm->iface[i]) {
1568 acm->devHandle[i] = UsbOpenInterface(acm->iface[i]);
1569 if (acm->devHandle[i] == NULL) {
1570 HDF_LOGE("%{public}s: UsbOpenInterface null", __func__);
1571 goto ERROR;
1572 }
1573 }
1574 }
1575 acm->ctrDevHandle = UsbOpenInterface(acm->ctrIface);
1576 if (acm->ctrDevHandle == NULL) {
1577 HDF_LOGE("%{public}s: ctrDevHandle UsbOpenInterface null", __func__);
1578 goto ERROR;
1579 }
1580
1581 return HDF_SUCCESS;
1582
1583 ERROR:
1584 AcmCloseInterfaces(acm);
1585 return HDF_FAILURE;
1586 }
1587
AcmGetPipes(struct AcmDevice * acm)1588 static int32_t AcmGetPipes(struct AcmDevice *acm)
1589 {
1590 acm->dataInPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN);
1591 if (acm->dataInPipe == NULL) {
1592 HDF_LOGE("dataInPipe is null");
1593 goto ERROR;
1594 }
1595
1596 acm->dataOutPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_OUT);
1597 if (acm->dataOutPipe == NULL) {
1598 HDF_LOGE("dataOutPipe is null");
1599 goto ERROR;
1600 }
1601
1602 acm->ctrPipe = EnumePipe(acm, acm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT);
1603 if (acm->ctrPipe == NULL) {
1604 HDF_LOGE("ctrPipe is null");
1605 goto ERROR;
1606 }
1607
1608 acm->intPipe = GetPipe(acm, USB_PIPE_TYPE_INTERRUPT, USB_PIPE_DIRECTION_IN);
1609 if (acm->intPipe == NULL) {
1610 HDF_LOGE("intPipe is null");
1611 goto ERROR;
1612 }
1613
1614 acm->readSize = acm->dataInPipe->maxPacketSize;
1615 acm->writeSize = acm->dataOutPipe->maxPacketSize;
1616 acm->ctrlSize = acm->ctrPipe->maxPacketSize;
1617 acm->intSize = acm->intPipe->maxPacketSize;
1618
1619 return HDF_SUCCESS;
1620
1621 ERROR:
1622 AcmFreePipes(acm);
1623 return HDF_FAILURE;
1624 }
1625
AcmFreeRequests(struct AcmDevice * acm)1626 static void AcmFreeRequests(struct AcmDevice *acm)
1627 {
1628 if (g_syncRequest != NULL) {
1629 UsbFreeRequest(g_syncRequest);
1630 g_syncRequest = NULL;
1631 }
1632 AcmFreeReadRequests(acm);
1633 AcmFreeNotifyReqeust(acm);
1634 AcmFreeWriteRequests(acm);
1635 AcmWriteBufFree(acm);
1636 }
1637
AcmAllocRequests(const struct AcmDevice * acm)1638 static int32_t AcmAllocRequests(const struct AcmDevice *acm)
1639 {
1640 int32_t ret;
1641
1642 if (AcmWriteBufAlloc(acm) < 0) {
1643 HDF_LOGE("%{public}s: AcmWriteBufAlloc failed", __func__);
1644 return HDF_ERR_MALLOC_FAIL;
1645 }
1646
1647 for (int32_t i = 0; i < ACM_NW; i++) {
1648 struct AcmWb *snd = (struct AcmWb *)&(acm->wb[i]);
1649 snd->request = UsbAllocRequest(
1650 InterfaceIdToHandle((struct AcmDevice *)acm, acm->dataOutPipe->interfaceId), 0, acm->writeSize);
1651 snd->instance = (struct AcmDevice *)acm;
1652 if (snd->request == NULL) {
1653 HDF_LOGE("%{public}s:%{public}d snd request fail", __func__, __LINE__);
1654 goto ERROR_ALLOC_WRITE_REQ;
1655 }
1656 }
1657
1658 ret = AcmAllocNotifyRequest((struct AcmDevice *)acm);
1659 if (ret != HDF_SUCCESS) {
1660 HDF_LOGE("%{public}s:%{public}d AcmAllocNotifyRequest fail", __func__, __LINE__);
1661 goto ERROR_ALLOC_INT_REQ;
1662 }
1663
1664 ret = AcmAllocReadRequests((struct AcmDevice *)acm);
1665 if (ret) {
1666 HDF_LOGE("%{public}s:%{public}d AcmAllocReadRequests fail", __func__, __LINE__);
1667 goto ERROR_ALLOC_READ_REQ;
1668 }
1669
1670 return HDF_SUCCESS;
1671
1672 ERROR_ALLOC_READ_REQ:
1673 AcmFreeNotifyReqeust((struct AcmDevice *)acm);
1674 ERROR_ALLOC_INT_REQ:
1675 AcmFreeWriteRequests((struct AcmDevice *)acm);
1676 ERROR_ALLOC_WRITE_REQ:
1677 AcmWriteBufFree((struct AcmDevice *)acm);
1678 return HDF_FAILURE;
1679 }
1680
AcmInit(struct AcmDevice * acm)1681 static int32_t AcmInit(struct AcmDevice *acm)
1682 {
1683 int32_t ret;
1684
1685 if (acm->initFlag) {
1686 HDF_LOGE("%{public}s: initFlag is true", __func__);
1687 return HDF_SUCCESS;
1688 }
1689
1690 ret = UsbInitHostSdk(NULL);
1691 if (ret != HDF_SUCCESS) {
1692 HDF_LOGE("%{public}s: UsbInitHostSdk failed", __func__);
1693 return HDF_ERR_IO;
1694 }
1695 acm->session = NULL;
1696
1697 ret = AcmClaimInterfaces(acm);
1698 if (ret != HDF_SUCCESS) {
1699 HDF_LOGE("%{public}s: AcmClaimInterfaces failed", __func__);
1700 goto ERROR_CLAIM_INTERFACES;
1701 }
1702
1703 ret = AcmOpenInterfaces(acm);
1704 if (ret != HDF_SUCCESS) {
1705 HDF_LOGE("%{public}s: AcmOpenInterfaces failed", __func__);
1706 goto ERROR_OPEN_INTERFACES;
1707 }
1708
1709 ret = AcmGetPipes(acm);
1710 if (ret != HDF_SUCCESS) {
1711 HDF_LOGE("%{public}s: AcmGetPipes failed", __func__);
1712 goto ERROR_GET_PIPES;
1713 }
1714
1715 ret = AcmAllocRequests(acm);
1716 if (ret != HDF_SUCCESS) {
1717 HDF_LOGE("%{public}s: AcmAllocRequests failed", __func__);
1718 goto ERROR_ALLOC_REQS;
1719 }
1720
1721 acm->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
1722 acm->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
1723 acm->lineCoding.bParityType = USB_CDC_NO_PARITY;
1724 acm->lineCoding.bDataBits = DATA_BITS_LENGTH;
1725 acm->initFlag = true;
1726
1727 return HDF_SUCCESS;
1728
1729 ERROR_ALLOC_REQS:
1730 AcmFreePipes(acm);
1731 ERROR_GET_PIPES:
1732 AcmCloseInterfaces(acm);
1733 ERROR_OPEN_INTERFACES:
1734 AcmReleaseInterfaces(acm);
1735 ERROR_CLAIM_INTERFACES:
1736 UsbExitHostSdk(acm->session);
1737 acm->session = NULL;
1738 return ret;
1739 }
1740
AcmRelease(struct AcmDevice * acm)1741 static void AcmRelease(struct AcmDevice *acm)
1742 {
1743 if (!(acm->initFlag)) {
1744 HDF_LOGE("%{public}s:%{public}d: initFlag is false", __func__, __LINE__);
1745 return;
1746 }
1747
1748 AcmCloseInterfaces(acm);
1749 AcmReleaseInterfaces(acm);
1750 AcmFreeRequests(acm);
1751 AcmFreePipes(acm);
1752 UsbExitHostSdk(acm->session);
1753 acm->session = NULL;
1754
1755 acm->initFlag = false;
1756 }
1757
UsbSerialDriverInit(struct HdfDeviceObject * device)1758 static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
1759 {
1760 int32_t ret;
1761 struct AcmDevice *acm = NULL;
1762
1763 if (device == NULL) {
1764 HDF_LOGE("%{public}s: device is null", __func__);
1765 return HDF_ERR_INVALID_OBJECT;
1766 }
1767 acm = (struct AcmDevice *)device->service;
1768 if (acm == NULL) {
1769 return HDF_ERR_INVALID_OBJECT;
1770 }
1771 OsalMutexInit(&acm->readLock);
1772 OsalMutexInit(&acm->writeLock);
1773 HDF_LOGD("%{public}s:%{public}d busNum = %{public}d,devAddr = %{public}d",
1774 __func__, __LINE__, acm->busNum, acm->devAddr);
1775
1776 ret = UsbSerialDeviceAlloc(acm);
1777 if (ret != HDF_SUCCESS) {
1778 HDF_LOGE("%{public}s: Serial Device alloc failed", __func__);
1779 }
1780
1781 acm->initFlag = false;
1782 g_acmReleaseFlag = false;
1783
1784 HDF_LOGD("%{public}s:%{public}d init ok!", __func__, __LINE__);
1785
1786 return ret;
1787 }
1788
UsbSerialDriverRelease(struct HdfDeviceObject * device)1789 static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
1790 {
1791 struct AcmDevice *acm = NULL;
1792
1793 if (device == NULL) {
1794 HDF_LOGE("%{public}s: device is null", __func__);
1795 return;
1796 }
1797 acm = (struct AcmDevice *)device->service;
1798 if (acm == NULL) {
1799 HDF_LOGE("%{public}s: acm is null", __func__);
1800 return;
1801 }
1802
1803 g_acmReleaseFlag = true;
1804
1805 if (acm->initFlag) {
1806 HDF_LOGE("%{public}s:%{public}d AcmRelease", __func__, __LINE__);
1807 AcmRelease(acm);
1808 }
1809 UsbSeriaDevicelFree(acm);
1810 OsalMutexDestroy(&acm->writeLock);
1811 OsalMutexDestroy(&acm->readLock);
1812 OsalMutexDestroy(&acm->lock);
1813 OsalMemFree(acm);
1814 acm = NULL;
1815 HDF_LOGD("%{public}s:%{public}d exit", __func__, __LINE__);
1816 }
1817
1818 struct HdfDriverEntry g_usbSerialDriverEntry = {
1819 .moduleVersion = 1,
1820 .moduleName = "usbhost_acm",
1821 .Bind = UsbSerialDriverBind,
1822 .Init = UsbSerialDriverInit,
1823 .Release = UsbSerialDriverRelease,
1824 };
1825 HDF_INIT(g_usbSerialDriverEntry);
1826