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