• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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("%s:UsbFillRequest failed, ret=%d \n", __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=%d \n", 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("%s:UsbFillRequest failed, ret = %d\n", __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 = %d\n", 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("%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("%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("%s: UsbFillRequest failed, ret=%d ", __func__, ret);
273         return ret;
274     }
275     ret = UsbSubmitRequestSync(descParams->request);
276     if (ret != HDF_SUCCESS) {
277         HDF_LOGE("UsbSubmitRequestSync failed, ret=%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=%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("%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("%s: UsbFillRequest failed, ret=%d ", __func__, ret);
331         return ret;
332     }
333     ret = UsbSubmitRequestSync(request);
334     if (ret != HDF_SUCCESS) {
335         HDF_LOGE("UsbSubmitRequestSync failed, ret=%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=%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("%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("%s: UsbFillRequest failed, ret = %d ", __func__, ret);
379         return ret;
380     }
381     ret = UsbSubmitRequestSync((struct UsbRequest *)request);
382     if (ret != HDF_SUCCESS) {
383         HDF_LOGE("%s: UsbSubmitRequestSync failed, ret = %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("%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("%s: UsbFillRequest failed, ret=%d ", __func__, ret);
418         return ret;
419     }
420     ret = UsbSubmitRequestSync((struct UsbRequest *)request);
421     if (ret != HDF_SUCCESS) {
422         HDF_LOGE("UsbSubmitRequestSync failed, ret=%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("%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("%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("%s: UsbFillRequest failed, ret = %d ", __func__, ret);
467         return ret;
468     }
469     ret = UsbSubmitRequestSync(acm->ctrlReq);
470     if (ret != HDF_SUCCESS) {
471         HDF_LOGE("UsbSubmitRequestSync failed, ret = %d ", ret);
472         return ret;
473     }
474     if (!acm->ctrlReq->compInfo.status) {
475         HDF_LOGE("%s  status=%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("%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("%s: UsbFillRequest failed, ret=%d ", __func__, ret);
508         return ret;
509     }
510     ret = UsbSubmitRequestAsync(request);
511     if (ret != HDF_SUCCESS) {
512         HDF_LOGE("UsbRequestSubmitAsync failed, ret=%d ", ret);
513         return ret;
514     }
515     OsalMSleep(500);
516     HDF_LOGE("SerialCtrlAsyncMsg  length%d ", request->compInfo.actualLength);
517     for (unsigned int i = 0; i < request->compInfo.actualLength; i++) {
518         HDF_LOGE("0x%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\n");
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("%s: acm null pointer", __func__);
532         return HDF_FAILURE;
533     }
534     port = (struct SerialDevice *)OsalMemCalloc(sizeof(*port));
535     if (port == NULL) {
536         HDF_LOGE("%s: Alloc usb serial port failed", __func__);
537         return HDF_FAILURE;
538     }
539     if (OsalMutexInit(&port->lock) != HDF_SUCCESS) {
540         HDF_LOGE("%s: init lock failed!", __func__);
541         return HDF_FAILURE;
542     }
543     port->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
544     port->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
545     port->lineCoding.bParityType = USB_CDC_NO_PARITY;
546     port->lineCoding.bDataBits = DATA_BITS_LENGTH;
547     acm->lineCoding = port->lineCoding;
548     acm->port = port;
549     port->acm = acm;
550     return HDF_SUCCESS;
551 }
552 
UsbSeriaDevicelFree(struct AcmDevice * acm)553 static void UsbSeriaDevicelFree(struct AcmDevice *acm)
554 {
555     struct SerialDevice *port = acm->port;
556     if (port == NULL) {
557         HDF_LOGE("%s: port is null", __func__);
558         return;
559     }
560     OsalMemFree(port);
561     port = NULL;
562 }
563 
UsbSerialRead(struct SerialDevice * port,struct HdfSBuf * reply)564 static int32_t UsbSerialRead(struct SerialDevice *port, struct HdfSBuf *reply)
565 {
566     uint32_t len;
567     int32_t ret = HDF_SUCCESS;
568     struct AcmDevice *acm = port->acm;
569 
570     for (int32_t i = 0; i < ACM_NR; i++) {
571         if (acm->readReq[i]->compInfo.status != USB_REQUEST_COMPLETED) {
572             HDF_LOGE("%s:%d i=%d status=%d!", __func__, __LINE__, i, acm->readReq[i]->compInfo.status);
573             return HDF_FAILURE;
574         }
575     }
576 
577     OsalMutexLock(&acm->readLock);
578 
579     if (g_acmReadBuffer == NULL) {
580         OsalMutexUnlock(&acm->readLock);
581         HDF_LOGE("%s:%d g_acmReadBuffer is null", __func__, __LINE__);
582         return HDF_ERR_MALLOC_FAIL;
583     }
584 
585     ret = memset_s(g_acmReadBuffer, READ_BUF_SIZE, 0, READ_BUF_SIZE);
586     if (ret != HDF_SUCCESS) {
587         HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__);
588         return ret;
589     }
590 
591     if (DataFifoIsEmpty(&port->readFifo)) {
592         ret = HDF_SUCCESS;
593         goto OUT;
594     }
595 
596     len = DataFifoRead(&port->readFifo, g_acmReadBuffer, DataFifoLen(&port->readFifo));
597     if (len == 0) {
598         HDF_LOGE("%s:%d no data", __func__, __LINE__);
599         ret = HDF_SUCCESS;
600         goto OUT;
601     }
602 OUT:
603     if (!HdfSbufWriteString(reply, (const char *)g_acmReadBuffer)) {
604         HDF_LOGE("%s:%d sbuf write buffer failed", __func__, __LINE__);
605         ret = HDF_ERR_IO;
606     }
607 
608     OsalMutexUnlock(&acm->readLock);
609     return ret;
610 }
611 
SerialSetBaudrate(struct SerialDevice * port,const struct HdfSBuf * data)612 static int32_t SerialSetBaudrate(struct SerialDevice *port, const struct HdfSBuf *data)
613 {
614     struct AcmDevice *acm = port->acm;
615     uint32_t baudRate = 0;
616 
617     if (!HdfSbufReadUint32((struct HdfSBuf *)data, &baudRate)) {
618         HDF_LOGE("%s: sbuf read buffer failed", __func__);
619         return HDF_ERR_IO;
620     }
621     port->lineCoding.dwDTERate = CPU_TO_LE32(baudRate);
622     if (memcmp(&acm->lineCoding, &port->lineCoding, sizeof(struct UsbCdcLineCoding))) {
623         int32_t ret =
624             memcpy_s(&acm->lineCoding, sizeof(struct UsbCdcLineCoding), &port->lineCoding, sizeof(port->lineCoding));
625         if (ret != EOK) {
626             HDF_LOGE("memcpy_s failed, ret = %d", ret);
627         }
628         HDF_LOGE("%s - set line: %d %d %d %d\n", __func__, (port->lineCoding.dwDTERate), port->lineCoding.bCharFormat,
629             port->lineCoding.bParityType, port->lineCoding.bDataBits);
630         ret = SerialCtrlMsg(acm, USB_DDK_CDC_REQ_SET_LINE_CODING, 0, &acm->lineCoding, sizeof(struct UsbCdcLineCoding));
631         if (ret != HDF_SUCCESS) {
632             HDF_LOGE("SerialCtrlMsg failed\n");
633             return ret;
634         }
635     }
636     return HDF_SUCCESS;
637 }
638 
UsbCtrlMsg(struct SerialDevice * port,struct HdfSBuf * data)639 static int32_t UsbCtrlMsg(struct SerialDevice *port, struct HdfSBuf *data)
640 {
641     (void)data;
642     int32_t ret;
643     struct AcmDevice *acm = port->acm;
644     struct UsbCdcLineCoding lineCoding = {
645         .dwDTERate = CPU_TO_LE32(DATARATE),
646         .bCharFormat = USB_CDC_1_STOP_BITS,
647         .bParityType = USB_CDC_NO_PARITY,
648         .bDataBits = DATA_BITS_LENGTH,
649     };
650     ret = SerialCtrlMsg(acm, USB_DDK_CDC_REQ_SET_LINE_CODING, 0, &lineCoding, sizeof(struct UsbCdcLineCoding));
651     if (ret) {
652         HDF_LOGE("SerialCtrlMsg failed.\n");
653         return ret;
654     }
655     return ret;
656 }
657 
SerialGetBaudrate(struct SerialDevice * port,struct HdfSBuf * reply)658 static int32_t SerialGetBaudrate(struct SerialDevice *port, struct HdfSBuf *reply)
659 {
660     uint32_t baudRate = LE32_TO_CPU(port->lineCoding.dwDTERate);
661     if (!HdfSbufWriteUint32(reply, baudRate)) {
662         HDF_LOGE("%s:%d sbuf write buffer failed", __func__, __LINE__);
663         return HDF_ERR_IO;
664     }
665 
666     HDF_LOGE("%s:%d baudRate = %u", __func__, __LINE__, baudRate);
667     return HDF_SUCCESS;
668 }
669 
UsbSerialReadSync(const struct SerialDevice * port,const struct HdfSBuf * reply)670 static int32_t UsbSerialReadSync(const struct SerialDevice *port, const struct HdfSBuf *reply)
671 {
672     struct AcmDevice *acm = port->acm;
673     uint8_t *data = NULL;
674     struct UsbRequestParams readParmas = {};
675     if (g_syncRequest == NULL) {
676         g_syncRequest = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize);
677         if (g_syncRequest == NULL) {
678             return HDF_ERR_MALLOC_FAIL;
679         }
680     }
681     readParmas.pipeAddress = acm->dataInPipe->pipeAddress;
682     readParmas.pipeId = acm->dataInPipe->pipeId;
683     readParmas.interfaceId = acm->dataInPipe->interfaceId;
684     readParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
685     readParmas.timeout = USB_CTRL_SET_TIMEOUT;
686     readParmas.dataReq.numIsoPackets = 0;
687     readParmas.dataReq.directon = (((uint8_t)acm->dataInPipe->pipeDirection) >> USB_DIR_OFFSET) & DIRECTION_MASK;
688     readParmas.dataReq.length = (int)acm->readSize;
689     readParmas.callback = NULL;
690     int32_t ret = UsbFillRequest(g_syncRequest, InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParmas);
691     if (ret != HDF_SUCCESS) {
692         return ret;
693     }
694     ret = UsbSubmitRequestSync(g_syncRequest);
695     if (ret != HDF_SUCCESS) {
696         return ret;
697     }
698     uint32_t count = g_syncRequest->compInfo.actualLength;
699     data = (uint8_t *)OsalMemCalloc(count + 1);
700     if ((data == NULL) || (!g_syncRequest)) {
701         return HDF_FAILURE;
702     }
703 
704     ret = memcpy_s(data, g_syncRequest->compInfo.actualLength, g_syncRequest->compInfo.buffer, count);
705     if (ret != EOK) {
706         HDF_LOGE("memcpy_s error %s, %d", __func__, __LINE__);
707         return HDF_FAILURE;
708     }
709 
710     if (!HdfSbufWriteString((struct HdfSBuf *)reply, (const char *)data)) {
711         HDF_LOGE("%s:%d sbuf write buffer failed", __func__, __LINE__);
712     }
713 
714     OsalMemFree(data);
715     data = NULL;
716 
717     return HDF_SUCCESS;
718 }
719 
UsbStdCtrlCmd(struct SerialDevice * port,SerialOPCmd cmd,struct HdfSBuf * reply)720 static int32_t UsbStdCtrlCmd(struct SerialDevice *port, SerialOPCmd cmd, struct HdfSBuf *reply)
721 {
722     int32_t ret;
723     static uint16_t ss;
724     static uint8_t data;
725     static uint8_t id;
726     char str[STR_LEN] = {};
727     static struct UsbDeviceDescriptor des = {};
728     struct AcmDevice *acm = port->acm;
729     struct UsbRequestParams parmas = {};
730     parmas.interfaceId = USB_CTRL_INTERFACE_ID;
731     parmas.pipeAddress = 0;
732     parmas.pipeId = 0;
733     parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE;
734     parmas.timeout = USB_CTRL_SET_TIMEOUT;
735     if (g_ctrlCmdRequest == NULL) {
736         g_ctrlCmdRequest = UsbAllocRequest(acm->ctrDevHandle, 0, acm->readSize);
737         if (g_ctrlCmdRequest == NULL) {
738             HDF_LOGE("ctrlRequest request failed");
739             return HDF_ERR_MALLOC_FAIL;
740         }
741     }
742     switch (cmd) {
743         case CMD_STD_CTRL_GET_DESCRIPTOR_CMD:
744             ret = GetDeviceDescriptor(acm->ctrDevHandle, g_ctrlCmdRequest, (void *)(&des), sizeof(des));
745             if (ret != HDF_SUCCESS) {
746                 HDF_LOGE("GetDeviceDescriptor failed ret:%d", ret);
747                 return HDF_FAILURE;
748             }
749             (void)snprintf_s(str, STR_LEN, STR_LEN - 1, "device descriptor info:[0x%04x 0x%04x 0x%02x 0x%02x 0x%02x]\n",
750                 des.idVendor, des.idProduct, des.bDeviceClass, des.bDeviceSubClass, des.bDeviceProtocol);
751             if (!HdfSbufWriteString(reply, (const char *)str)) {
752                 HDF_LOGE("%s: sbuf write buffer failed", __func__);
753                 return HDF_FAILURE;
754             }
755             for (unsigned int i = 0; i < sizeof(des); i++) {
756                 HDF_LOGI("%s: i is %u", __func__, i);
757             }
758             break;
759         case CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC:
760             ret = SerialCtrlAsyncMsg(acm->ctrDevHandle, g_ctrlCmdRequest, (void *)(&des), sizeof(des));
761             if (ret != HDF_SUCCESS) {
762                 HDF_LOGE("GetDeviceDescriptor async fail ret:%d", ret);
763                 return HDF_FAILURE;
764             }
765             (void)snprintf_s(str, STR_LEN, STR_LEN - 1, "device descriptor info:[0x%04x 0x%04x 0x%02x 0x%02x 0x%02x]\n",
766                 des.idVendor, des.idProduct, des.bDeviceClass, des.bDeviceSubClass, des.bDeviceProtocol);
767             if (!HdfSbufWriteString(reply, (const char *)str)) {
768                 HDF_LOGE("%s: sbuf write buffer failed", __func__);
769                 return HDF_FAILURE;
770             }
771             for (unsigned int i = 0; i < sizeof(des); i++) {
772                 HDF_LOGI("%s: i is %u", __func__, i);
773             }
774             break;
775         case CMD_STD_CTRL_GET_STATUS_CMD:
776             ret = UsbGetStatus(acm->ctrDevHandle, g_ctrlCmdRequest, &ss);
777             if (ret != HDF_SUCCESS) {
778                 HDF_LOGE("UsbGetStatus failed ret:%d", ret);
779                 return HDF_FAILURE;
780             }
781             ret = HdfSbufWriteUint16(reply, ss);
782             break;
783         case CMD_STD_CTRL_GET_CONFIGURATION:
784             ret = UsbGetConfig(acm->ctrDevHandle, g_ctrlCmdRequest, &data);
785             if (ret != HDF_SUCCESS) {
786                 HDF_LOGE("UsbGetStatus failed ret:%d", ret);
787                 return HDF_FAILURE;
788             }
789             ret = HdfSbufWriteUint8(reply, data);
790             break;
791         case CMD_STD_CTRL_GET_INTERFACE:
792             ret = UsbGetInterface(acm->ctrDevHandle, g_ctrlCmdRequest, &id);
793             if (ret != HDF_SUCCESS) {
794                 HDF_LOGE("UsbGetStatus failed ret:%d", ret);
795                 return HDF_FAILURE;
796             }
797             ret = HdfSbufWriteUint8(reply, id);
798             break;
799         default:
800             ret = -1;
801             break;
802     }
803     if (!ret) {
804         HDF_LOGE("cmd:%d ret:%d", cmd, ret);
805     }
806     return ret;
807 }
808 
SerialWriteSync(const struct SerialDevice * port,const struct HdfSBuf * data)809 static int32_t SerialWriteSync(const struct SerialDevice *port, const struct HdfSBuf *data)
810 {
811     uint32_t size;
812     int32_t ret;
813     const char *tmp = NULL;
814     int32_t wbn;
815     struct AcmWb *wb = NULL;
816     if (port == NULL || data == NULL) {
817         HDF_LOGE("%d: invalid param", __LINE__);
818         return HDF_ERR_INVALID_PARAM;
819     }
820     struct AcmDevice *acm = port->acm;
821     if (acm == NULL) {
822         HDF_LOGE("%d: invalid param", __LINE__);
823         return HDF_ERR_INVALID_PARAM;
824     }
825     if (AcmWbIsAvail(acm)) {
826         wbn = AcmWbAlloc(acm);
827     } else {
828         HDF_LOGE("no write buf\n");
829         return 0;
830     }
831     if (wbn >= ACM_NW || wbn < 0) {
832         wbn = 0;
833     }
834     wb = &acm->wb[wbn];
835     if (wb == NULL) {
836         return HDF_ERR_INVALID_PARAM;
837     }
838     tmp = HdfSbufReadString((struct HdfSBuf *)data);
839     if (tmp == NULL) {
840         HDF_LOGE("%s: sbuf read buffer failed", __func__);
841         return HDF_ERR_IO;
842     }
843     size = (uint32_t)strlen(tmp) + 1;
844     size = (size > acm->writeSize) ? acm->writeSize : size;
845     ret = memcpy_s(wb->buf, (size_t)acm->writeSize, tmp, (size_t)size);
846     if (ret != EOK) {
847         HDF_LOGE("memcpy_s failed, ret = %d", ret);
848     }
849     wb->len = size;
850     if (acm->dataOutPipe == NULL) {
851         return HDF_ERR_INVALID_PARAM;
852     }
853 
854     ret = AcmStartWbSync(acm, wb, acm->dataOutPipe);
855     if (ret != HDF_SUCCESS) {
856         HDF_LOGE("%{public}s: AcmStartWbSync failed, ret=%{public}d", __func__, ret);
857         return HDF_FAILURE;
858     }
859 
860     return (int32_t)size;
861 }
862 
SerialOpen(const struct SerialDevice * port,struct HdfSBuf * data)863 static int32_t SerialOpen(const struct SerialDevice *port, struct HdfSBuf *data)
864 {
865     int32_t ret;
866     int32_t cmdType = HOST_ACM_ASYNC_READ;
867 
868     if ((port == NULL) || (data == NULL)) {
869         HDF_LOGE("%{public}s: port or data is null", __func__);
870         return HDF_ERR_INVALID_PARAM;
871     }
872     struct AcmDevice *acm = port->acm;
873     if (acm == NULL) {
874         HDF_LOGE("%{public}s: acm is null", __func__);
875         return HDF_ERR_INVALID_PARAM;
876     }
877 
878     if (!HdfSbufReadInt32(data, &cmdType)) {
879         HDF_LOGE("%{public}s: sbuf read cmdType failed", __func__);
880         return HDF_ERR_INVALID_PARAM;
881     }
882 
883     if (AcmInit(acm) != HDF_SUCCESS) {
884         HDF_LOGE("%{public}s: AcmInit failed", __func__);
885         return HDF_FAILURE;
886     }
887 
888     if (cmdType != HOST_ACM_ASYNC_READ) {
889         HDF_LOGD("%{public}s: asyncRead success", __func__);
890         return HDF_SUCCESS;
891     }
892 
893     if (g_acmReadBuffer == NULL) {
894         g_acmReadBuffer = (uint8_t *)OsalMemCalloc(READ_BUF_SIZE);
895         if (g_acmReadBuffer == NULL) {
896             HDF_LOGE("%{public}s: OsalMemCalloc g_acmReadBuffer error", __func__);
897             return HDF_ERR_MALLOC_FAIL;
898         }
899     }
900 
901     ret = UsbSerialAllocFifo((struct DataFifo *)&port->readFifo, READ_BUF_SIZE);
902     if (ret != HDF_SUCCESS) {
903         HDF_LOGE("%{public}s: UsbSerialAllocFifo failed", __func__);
904         return HDF_ERR_INVALID_PARAM;
905     }
906     for (int32_t i = 0; i < ACM_NR; i++) {
907         ret = UsbSubmitRequestAsync(acm->readReq[i]);
908         if (ret != HDF_SUCCESS) {
909             HDF_LOGE("%{public}s: UsbSubmitRequestAsync failed", __func__);
910             goto ERR;
911         }
912     }
913     return HDF_SUCCESS;
914 ERR:
915     OsalMemFree(g_acmReadBuffer);
916     g_acmReadBuffer = NULL;
917     UsbSerialFreeFifo((struct DataFifo *)&port->readFifo);
918     return ret;
919 }
920 
SerialClose(const struct SerialDevice * port,struct HdfSBuf * data)921 static int32_t SerialClose(const struct SerialDevice *port, struct HdfSBuf *data)
922 {
923     int32_t cmdType = HOST_ACM_SYNC_READ;
924     struct AcmDevice *acm = NULL;
925 
926     if ((port == NULL) || (data == NULL)) {
927         HDF_LOGE("%s: invalid param", __func__);
928         return HDF_ERR_INVALID_PARAM;
929     }
930     acm = port->acm;
931     if (acm == NULL) {
932         HDF_LOGE("%s: invalid param", __func__);
933         return HDF_ERR_INVALID_PARAM;
934     }
935 
936     if (!HdfSbufReadInt32(data, &cmdType)) {
937         HDF_LOGE("%s:%d sbuf read cmdType failed", __func__, __LINE__);
938         return HDF_ERR_INVALID_PARAM;
939     }
940 
941     if ((cmdType == HOST_ACM_SYNC_READ) || (cmdType == HOST_ACM_SYNC_WRITE) || (cmdType == HOST_ACM_ASYNC_WRITE) ||
942         (cmdType == HOST_ACM_ADD_INTERFACE) || (cmdType == HOST_ACM_REMOVE_INTERFACE)) {
943         HDF_LOGD("%s:%d cmdType=%d success", __func__, __LINE__, cmdType);
944         return HDF_SUCCESS;
945     }
946 
947     if (g_acmReadBuffer != NULL) {
948         OsalMemFree(g_acmReadBuffer);
949         g_acmReadBuffer = NULL;
950     }
951 
952     UsbSerialFreeFifo((struct DataFifo *)&port->readFifo);
953     AcmRelease(acm);
954     return HDF_SUCCESS;
955 }
956 
SerialWrite(const struct SerialDevice * port,struct HdfSBuf * data)957 static int32_t SerialWrite(const struct SerialDevice *port, struct HdfSBuf *data)
958 {
959     uint32_t size;
960     int32_t ret;
961     const char *tmp = NULL;
962 
963     int32_t wbn;
964     struct AcmWb *wb = NULL;
965     if (port == NULL) {
966         HDF_LOGE("%d: invalid param", __LINE__);
967         return HDF_ERR_INVALID_PARAM;
968     }
969     struct AcmDevice *acm = port->acm;
970     if (acm == NULL) {
971         HDF_LOGE("%d: invalid param", __LINE__);
972         return HDF_ERR_INVALID_PARAM;
973     }
974     if (AcmWbIsAvail(acm)) {
975         wbn = AcmWbAlloc(acm);
976     } else {
977         HDF_LOGE("no write buf\n");
978         return 0;
979     }
980     if (wbn < 0) {
981         HDF_LOGE("AcmWbAlloc failed\n");
982         return HDF_FAILURE;
983     }
984     wb = &acm->wb[wbn];
985 
986     tmp = HdfSbufReadString(data);
987     if (tmp == NULL) {
988         HDF_LOGE("%s: sbuf read buffer failed", __func__);
989         return HDF_ERR_IO;
990     }
991     size = (uint32_t)strlen(tmp) + 1;
992     size = (size > acm->writeSize) ? acm->writeSize : size;
993     ret = memcpy_s(wb->buf, (size_t)acm->writeSize, tmp, (size_t)size);
994     if (ret != EOK) {
995         HDF_LOGE("memcpy_s failed, ret = %d", ret);
996     }
997     wb->len = size;
998 
999     ret = AcmStartWb(acm, wb, acm->dataOutPipe);
1000     if (ret != HDF_SUCCESS) {
1001         HDF_LOGE("%{public}s: AcmStartWb failed, ret=%d", __func__, ret);
1002         return HDF_FAILURE;
1003     }
1004     return (int32_t)size;
1005 }
1006 
SerialAddOrRemoveInterface(int32_t cmd,const struct SerialDevice * port,const struct HdfSBuf * data)1007 static int32_t SerialAddOrRemoveInterface(int32_t cmd, const struct SerialDevice *port, const struct HdfSBuf *data)
1008 {
1009     struct AcmDevice *acm = port->acm;
1010     UsbInterfaceStatus status = 0;
1011     uint32_t index = 0;
1012 
1013     if (!HdfSbufReadUint32((struct HdfSBuf *)data, &index)) {
1014         HDF_LOGE("%s:%d sbuf read interfaceNum failed", __func__, __LINE__);
1015         return HDF_ERR_INVALID_PARAM;
1016     }
1017 
1018     if (cmd == CMD_ADD_INTERFACE) {
1019         status = USB_INTERFACE_STATUS_ADD;
1020     } else if (cmd == CMD_REMOVE_INTERFACE) {
1021         status = USB_INTERFACE_STATUS_REMOVE;
1022     } else {
1023         HDF_LOGE("%s:%d cmd=% is not define", __func__, __LINE__, cmd);
1024         return HDF_ERR_INVALID_PARAM;
1025     }
1026 
1027     return UsbAddOrRemoveInterface(acm->session, acm->busNum, acm->devAddr, index, status);
1028 }
1029 
UsbSerialCheckCmd(struct SerialDevice * port,int32_t cmd,struct HdfSBuf * data,const struct HdfSBuf * reply)1030 static int32_t UsbSerialCheckCmd(
1031     struct SerialDevice *port, int32_t cmd, struct HdfSBuf *data, const struct HdfSBuf *reply)
1032 {
1033     switch (cmd) {
1034         case CMD_OPEN_PARM:
1035             return SerialOpen(port, data);
1036         case CMD_CLOSE_PARM:
1037             return SerialClose(port, data);
1038         case CMD_WRITE_PARM:
1039             return SerialWrite(port, data);
1040         case CMD_READ_PARM:
1041             return UsbSerialRead(port, (struct HdfSBuf *)reply);
1042         case CMD_GET_BAUDRATE:
1043             return SerialGetBaudrate(port, (struct HdfSBuf *)reply);
1044         case CMD_SET_BAUDRATE:
1045             return SerialSetBaudrate(port, (struct HdfSBuf *)data);
1046         case CMD_WRITE_DATA_SYNC:
1047             return SerialWriteSync(port, data);
1048         case CMD_READ_DATA_SYNC:
1049             return UsbSerialReadSync(port, (struct HdfSBuf *)reply);
1050         case CMD_CLASS_CTRL_SYNC:
1051             return UsbCtrlMsg(port, (struct HdfSBuf *)reply);
1052         case CMD_STD_CTRL_GET_DESCRIPTOR_CMD:
1053             return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_DESCRIPTOR_CMD, (struct HdfSBuf *)reply);
1054         case CMD_STD_CTRL_GET_STATUS_CMD:
1055             return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_STATUS_CMD, (struct HdfSBuf *)reply);
1056         case CMD_STD_CTRL_GET_CONFIGURATION:
1057             return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_CONFIGURATION, (struct HdfSBuf *)reply);
1058         case CMD_STD_CTRL_GET_INTERFACE:
1059             return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_INTERFACE, (struct HdfSBuf *)reply);
1060         case CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC:
1061             return UsbStdCtrlCmd(port, CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC, (struct HdfSBuf *)reply);
1062         case CMD_ADD_INTERFACE:
1063         case CMD_REMOVE_INTERFACE:
1064             return SerialAddOrRemoveInterface(cmd, port, data);
1065         default:
1066             return HDF_ERR_NOT_SUPPORT;
1067     }
1068 }
1069 
UsbSerialDeviceDispatch(struct HdfDeviceIoClient * client,int32_t cmd,struct HdfSBuf * data,struct HdfSBuf * reply)1070 static int32_t UsbSerialDeviceDispatch(
1071     struct HdfDeviceIoClient *client, int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
1072 {
1073     struct AcmDevice *acm = NULL;
1074     struct SerialDevice *port = NULL;
1075 
1076     if (client == NULL) {
1077         HDF_LOGE("%s:%d client is null", __func__, __LINE__);
1078         return HDF_ERR_INVALID_OBJECT;
1079     }
1080     if (client->device == NULL) {
1081         HDF_LOGE("%s:%d client->device is null", __func__, __LINE__);
1082         return HDF_ERR_INVALID_OBJECT;
1083     }
1084     if (client->device->service == NULL) {
1085         HDF_LOGE("%s:%d client->device->service is null", __func__, __LINE__);
1086         return HDF_ERR_INVALID_OBJECT;
1087     }
1088     acm = (struct AcmDevice *)client->device->service;
1089     port = acm->port;
1090     if (port == NULL) {
1091         HDF_LOGE("%s:%d port is null", __func__, __LINE__);
1092         return HDF_ERR_INVALID_OBJECT;
1093     }
1094 
1095     if (g_acmReleaseFlag) {
1096         HDF_LOGE("%s:%d g_acmReleaseFlag is true", __func__, __LINE__);
1097         return HDF_FAILURE;
1098     }
1099 
1100     return UsbSerialCheckCmd(port, cmd, data, reply);
1101 }
1102 
GetUsbInterfaceById(const struct AcmDevice * acm,uint8_t interfaceIndex)1103 static struct UsbInterface *GetUsbInterfaceById(const struct AcmDevice *acm, uint8_t interfaceIndex)
1104 {
1105     return UsbClaimInterface(acm->session, acm->busNum, acm->devAddr, interfaceIndex);
1106 }
1107 
AcmFreePipes(struct AcmDevice * acm)1108 static void AcmFreePipes(struct AcmDevice *acm)
1109 {
1110     if (acm == NULL) {
1111         return;
1112     }
1113     if (acm->ctrPipe) {
1114         OsalMemFree(acm->ctrPipe);
1115         acm->ctrPipe = NULL;
1116     }
1117     if (acm->intPipe) {
1118         OsalMemFree(acm->intPipe);
1119         acm->intPipe = NULL;
1120     }
1121     if (acm->dataInPipe) {
1122         OsalMemFree(acm->dataInPipe);
1123         acm->dataInPipe = NULL;
1124     }
1125     if (acm->dataOutPipe) {
1126         OsalMemFree(acm->dataOutPipe);
1127         acm->dataOutPipe = NULL;
1128     }
1129 }
1130 
EnumePipe(const struct AcmDevice * acm,uint8_t interfaceIndex,UsbPipeType pipeType,UsbPipeDirection pipeDirection)1131 static struct UsbPipeInfo *EnumePipe(
1132     const struct AcmDevice *acm, uint8_t interfaceIndex, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
1133 {
1134     struct UsbInterfaceInfo *info = NULL;
1135     UsbInterfaceHandle *interfaceHandle = NULL;
1136     if (pipeType == USB_PIPE_TYPE_CONTROL) {
1137         info = &acm->ctrIface->info;
1138         interfaceHandle = acm->ctrDevHandle;
1139     } else {
1140         info = &acm->iface[interfaceIndex]->info;
1141         interfaceHandle = InterfaceIdToHandle(acm, info->interfaceIndex);
1142     }
1143 
1144     for (uint8_t i = 0; i <= info->pipeNum; i++) {
1145         struct UsbPipeInfo p;
1146         int32_t ret = UsbGetPipeInfo(interfaceHandle, info->curAltSetting, i, &p);
1147         if (ret < 0) {
1148             continue;
1149         }
1150         if ((p.pipeDirection == pipeDirection) && (p.pipeType == pipeType)) {
1151             struct UsbPipeInfo *pi = OsalMemCalloc(sizeof(*pi));
1152             if (pi == NULL) {
1153                 HDF_LOGE("%s: Alloc pipe failed", __func__);
1154                 return NULL;
1155             }
1156             p.interfaceId = info->interfaceIndex;
1157             *pi = p;
1158             return pi;
1159         }
1160     }
1161     return NULL;
1162 }
1163 
GetPipe(const struct AcmDevice * acm,UsbPipeType pipeType,UsbPipeDirection pipeDirection)1164 static struct UsbPipeInfo *GetPipe(const struct AcmDevice *acm, UsbPipeType pipeType, UsbPipeDirection pipeDirection)
1165 {
1166     uint8_t i;
1167     if (acm == NULL) {
1168         HDF_LOGE("%s: invalid param", __func__);
1169         return NULL;
1170     }
1171     for (i = 0; i < acm->interfaceCnt; i++) {
1172         struct UsbPipeInfo *p = NULL;
1173         if (!acm->iface[i]) {
1174             continue;
1175         }
1176         p = EnumePipe(acm, i, pipeType, pipeDirection);
1177         if (p == NULL) {
1178             continue;
1179         }
1180         return p;
1181     }
1182     return NULL;
1183 }
1184 
1185 /* HdfDriverEntry implementations */
UsbSerialDriverBind(struct HdfDeviceObject * device)1186 static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
1187 {
1188     struct UsbPnpNotifyServiceInfo *info = NULL;
1189     errno_t err;
1190     struct AcmDevice *acm = NULL;
1191     if (device == NULL) {
1192         HDF_LOGE("%s: device is null", __func__);
1193         return HDF_ERR_INVALID_OBJECT;
1194     }
1195     acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*acm));
1196     if (acm == NULL) {
1197         HDF_LOGE("%s: Alloc usb serial device failed", __func__);
1198         return HDF_FAILURE;
1199     }
1200     if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) {
1201         HDF_LOGE("%s:%d OsalMutexInit failed", __func__, __LINE__);
1202         goto ERROR;
1203     }
1204     info = (struct UsbPnpNotifyServiceInfo *)device->priv;
1205     if (info != NULL) {
1206         HDF_LOGD("%s:%d busNum=%d,devAddr=%d,interfaceLength=%d", __func__, __LINE__, info->busNum, info->devNum,
1207             info->interfaceLength);
1208         acm->busNum = (uint8_t)info->busNum;
1209         acm->devAddr = (uint8_t)info->devNum;
1210         acm->interfaceCnt = info->interfaceLength;
1211         err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, (const void *)info->interfaceNumber,
1212             info->interfaceLength);
1213         if (err != EOK) {
1214             HDF_LOGE("%s:%d memcpy_s failed err = %d", __func__, __LINE__, err);
1215             goto LOCK_ERROR;
1216         }
1217     } else {
1218         HDF_LOGE("%s:%d info is null!", __func__, __LINE__);
1219         goto LOCK_ERROR;
1220     }
1221     acm->device = device;
1222     device->service = &(acm->service);
1223     acm->device->service->Dispatch = UsbSerialDeviceDispatch;
1224     HDF_LOGD("UsbSerialDriverBind=========================OK");
1225     return HDF_SUCCESS;
1226 
1227 LOCK_ERROR:
1228     if (OsalMutexDestroy(&acm->lock)) {
1229         HDF_LOGE("%s:%d OsalMutexDestroy failed", __func__, __LINE__);
1230     }
1231 ERROR:
1232     OsalMemFree(acm);
1233     acm = NULL;
1234     return HDF_FAILURE;
1235 }
1236 
AcmProcessNotification(const struct AcmDevice * acm,const unsigned char * buf)1237 static void AcmProcessNotification(const struct AcmDevice *acm, const unsigned char *buf)
1238 {
1239     (void)acm;
1240     struct UsbCdcNotification *dr = (struct UsbCdcNotification *)buf;
1241     switch (dr->bNotificationType) {
1242         case USB_DDK_CDC_NOTIFY_NETWORK_CONNECTION:
1243             HDF_LOGE("%s - network connection: %d\n", __func__, dr->wValue);
1244             break;
1245         case USB_DDK_CDC_NOTIFY_SERIAL_STATE:
1246             HDF_LOGE("the serial State change\n");
1247             break;
1248         default:
1249             HDF_LOGE("%s-%d received: index %d len %d\n", __func__, dr->bNotificationType, dr->wIndex, dr->wLength);
1250     }
1251     return;
1252 }
1253 
AcmCtrlIrqCheckSize(struct UsbRequest * const req,struct AcmDevice * acm,struct UsbCdcNotification * dr)1254 static int32_t AcmCtrlIrqCheckSize(struct UsbRequest * const req, struct AcmDevice *acm, struct UsbCdcNotification *dr)
1255 {
1256     if ((req == NULL) || (acm == NULL) || (dr == NULL)) {
1257         HDF_LOGE("%s:%d Invalid parameter", __func__, __LINE__);
1258         return HDF_ERR_INVALID_PARAM;
1259     }
1260 
1261     unsigned int currentSize = req->compInfo.actualLength;
1262     HDF_LOGD("actualLength:%u\n", currentSize);
1263 
1264     unsigned int expectedSize = sizeof(struct UsbCdcNotification) + LE16_TO_CPU(dr->wLength);
1265     if (currentSize < expectedSize) {
1266         if (acm->nbSize < expectedSize) {
1267             if (acm->nbSize) {
1268                 OsalMemFree(acm->notificationBuffer);
1269                 acm->nbSize = 0;
1270             }
1271             unsigned int allocSize = expectedSize;
1272             acm->notificationBuffer = OsalMemCalloc(allocSize);
1273             if (!acm->notificationBuffer) {
1274                 return HDF_ERR_MALLOC_FAIL;
1275             }
1276             acm->nbSize = allocSize;
1277         }
1278         unsigned int copySize = MIN(currentSize, expectedSize - acm->nbIndex);
1279         if (memcpy_s(&acm->notificationBuffer[acm->nbIndex], acm->nbSize - acm->nbIndex, req->compInfo.buffer,
1280                 copySize) != EOK) {
1281             HDF_LOGE("%{public}s:%{public}d memcpy_s failed", __func__, __LINE__);
1282         }
1283         acm->nbIndex += copySize;
1284         currentSize = acm->nbIndex;
1285     }
1286 
1287     if (currentSize >= expectedSize) {
1288         AcmProcessNotification(acm, (unsigned char *)dr);
1289         acm->nbIndex = 0;
1290     }
1291     return HDF_SUCCESS;
1292 }
1293 
AcmCtrlIrq(struct UsbRequest * const req)1294 static void AcmCtrlIrq(struct UsbRequest * const req)
1295 {
1296     if (req == NULL) {
1297         HDF_LOGE("%s:%d req is null!", __func__, __LINE__);
1298         goto EXIT;
1299     }
1300     int32_t ret;
1301     struct AcmDevice *acm = (struct AcmDevice *)req->compInfo.userData;
1302     int32_t status = req->compInfo.status;
1303     HDF_LOGD("Irqstatus:%d", status);
1304 
1305     struct UsbCdcNotification *dr = (struct UsbCdcNotification *)req->compInfo.buffer;
1306     switch (status) {
1307         case 0:
1308             break;
1309         default:
1310             goto EXIT;
1311     }
1312     if ((acm != NULL) && acm->nbIndex) {
1313         dr = (struct UsbCdcNotification *)acm->notificationBuffer;
1314     }
1315     if ((dr == NULL) || (acm == NULL)) {
1316         HDF_LOGE("%s:%d dr or acm is null!", __func__, __LINE__);
1317         goto EXIT;
1318     }
1319 
1320     ret = AcmCtrlIrqCheckSize(req, acm, dr);
1321     if (ret != HDF_SUCCESS) {
1322         goto EXIT;
1323     }
1324 
1325     UsbSubmitRequestAsync(req);
1326 
1327 EXIT:
1328     HDF_LOGE("%s:%d exit", __func__, __LINE__);
1329 }
1330 
AcmReadBulk(struct UsbRequest * req)1331 static void AcmReadBulk(struct UsbRequest *req)
1332 {
1333     if (req == NULL) {
1334         HDF_LOGE("%s:%d req is null!", __func__, __LINE__);
1335         return;
1336     }
1337     int32_t retval;
1338     int32_t status = req->compInfo.status;
1339     size_t size = req->compInfo.actualLength;
1340     struct AcmDevice *acm = (struct AcmDevice *)req->compInfo.userData;
1341     if (acm == NULL || acm->port == NULL) {
1342         HDF_LOGE("%s:%d acm is null!", __func__, __LINE__);
1343         return;
1344     }
1345 
1346     switch (status) {
1347         case 0:
1348             HDF_LOGD("Bulk status: %d+size:%zu\n", status, size);
1349             if (size) {
1350                 uint8_t *data = req->compInfo.buffer;
1351                 OsalMutexLock(&acm->readLock);
1352                 if (DataFifoIsFull(&acm->port->readFifo)) {
1353                     HDF_LOGD("%s:%d", __func__, __LINE__);
1354                     DataFifoSkip(&acm->port->readFifo, size);
1355                 }
1356                 uint32_t count = DataFifoWrite(&acm->port->readFifo, data, size);
1357                 if (count != size) {
1358                     HDF_LOGW("%s: write %u less than expected %zu", __func__, count, size);
1359                 }
1360                 OsalMutexUnlock(&acm->readLock);
1361             }
1362             break;
1363         default:
1364             HDF_LOGE("%s:%d status=%d", __func__, __LINE__, status);
1365             return;
1366     }
1367 
1368     retval = UsbSubmitRequestAsync(req);
1369     if (retval && retval != -EPERM) {
1370         HDF_LOGE("%s - usb_submit_urb failed: %d", __func__, retval);
1371     }
1372 }
1373 
AcmFreeWriteRequests(struct AcmDevice * acm)1374 static void AcmFreeWriteRequests(struct AcmDevice *acm)
1375 {
1376     int32_t i;
1377     struct AcmWb *snd = NULL;
1378     for (i = 0; i < ACM_NW; i++) {
1379         snd = &acm->wb[i];
1380         int32_t ret = UsbCancelRequest(snd->request);
1381         if (ret != HDF_SUCCESS) {
1382             HDF_LOGE("UsbCancelRequest rd failed, ret = %d ", ret);
1383         }
1384     }
1385     for (i = 0; i < ACM_NW; i++) {
1386         snd = &acm->wb[i];
1387         if (snd->request != NULL) {
1388             UsbFreeRequest(snd->request);
1389             snd->request = NULL;
1390         }
1391     }
1392 }
1393 
AcmFreeReadRequests(struct AcmDevice * acm)1394 static void AcmFreeReadRequests(struct AcmDevice *acm)
1395 {
1396     if (acm == NULL) {
1397         HDF_LOGE("%s: acm is NULL", __func__);
1398         return;
1399     }
1400 
1401     int32_t i;
1402     for (i = 0; i < ACM_NR; i++) {
1403         int32_t ret = UsbCancelRequest(acm->readReq[i]);
1404         if (ret != HDF_SUCCESS) {
1405             HDF_LOGE("UsbCancelRequest rd failed, ret=%d ", ret);
1406         }
1407     }
1408     for (i = 0; i < ACM_NR; i++) {
1409         if (acm->readReq[i]) {
1410             UsbFreeRequest(acm->readReq[i]);
1411             acm->readReq[i] = NULL;
1412         }
1413     }
1414 }
1415 
AcmFreeNotifyReqeust(struct AcmDevice * acm)1416 static void AcmFreeNotifyReqeust(struct AcmDevice *acm)
1417 {
1418     int32_t ret;
1419 
1420     if ((acm == NULL) || (acm->notifyReq == NULL)) {
1421         HDF_LOGE("%s: acm or notifyReq is null", __func__);
1422         return;
1423     }
1424     ret = UsbCancelRequest(acm->notifyReq);
1425     if (ret != HDF_SUCCESS) {
1426         HDF_LOGE("UsbCancelRequest rd failed, ret = %d ", ret);
1427     }
1428     ret = UsbFreeRequest(acm->notifyReq);
1429     if (ret == HDF_SUCCESS) {
1430         acm->notifyReq = NULL;
1431     } else {
1432         HDF_LOGE("%s: AcmFreeNotifyReqeust failed, ret = %d", __func__, ret);
1433     }
1434 }
1435 
AcmAllocReadRequests(struct AcmDevice * acm)1436 static int32_t AcmAllocReadRequests(struct AcmDevice *acm)
1437 {
1438     int32_t ret;
1439     struct UsbRequestParams readParmas = {};
1440     for (int32_t i = 0; i < ACM_NR; i++) {
1441         acm->readReq[i] = UsbAllocRequest(InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), 0, acm->readSize);
1442         if (!acm->readReq[i]) {
1443             HDF_LOGE("readReq request failed\n");
1444             goto ERROR;
1445         }
1446         readParmas.userData = (void *)acm;
1447         readParmas.pipeAddress = acm->dataInPipe->pipeAddress;
1448         readParmas.pipeId = acm->dataInPipe->pipeId;
1449         readParmas.interfaceId = acm->dataInPipe->interfaceId;
1450         readParmas.callback = AcmReadBulk;
1451         readParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
1452         readParmas.timeout = USB_CTRL_SET_TIMEOUT;
1453         readParmas.dataReq.numIsoPackets = 0;
1454         readParmas.dataReq.directon = (((uint8_t)acm->dataInPipe->pipeDirection) >> USB_PIPE_DIR_OFFSET) & 0x1;
1455         readParmas.dataReq.length = (int)acm->readSize;
1456         ret = UsbFillRequest(acm->readReq[i], InterfaceIdToHandle(acm, acm->dataInPipe->interfaceId), &readParmas);
1457         if (ret != HDF_SUCCESS) {
1458             HDF_LOGE("%s: UsbFillRequest failed, ret=%d \n", __func__, ret);
1459             goto ERROR;
1460         }
1461     }
1462     return HDF_SUCCESS;
1463 
1464 ERROR:
1465     AcmFreeReadRequests(acm);
1466     return HDF_ERR_MALLOC_FAIL;
1467 }
1468 
AcmAllocNotifyRequest(struct AcmDevice * acm)1469 static int32_t AcmAllocNotifyRequest(struct AcmDevice *acm)
1470 {
1471     int32_t ret;
1472     struct UsbRequestParams intParmas = {};
1473     acm->notifyReq = UsbAllocRequest(InterfaceIdToHandle(acm, acm->intPipe->interfaceId), 0, acm->intSize);
1474     if (!acm->notifyReq) {
1475         HDF_LOGE("notifyReq request failed.\n");
1476         return HDF_ERR_MALLOC_FAIL;
1477     }
1478     intParmas.userData = (void *)acm;
1479     intParmas.pipeAddress = acm->intPipe->pipeAddress;
1480     intParmas.pipeId = acm->intPipe->pipeId;
1481     intParmas.interfaceId = acm->intPipe->interfaceId;
1482     intParmas.callback = AcmCtrlIrq;
1483     intParmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE;
1484     intParmas.timeout = USB_CTRL_SET_TIMEOUT;
1485     intParmas.dataReq.numIsoPackets = 0;
1486     intParmas.dataReq.directon = (((uint8_t)acm->intPipe->pipeDirection) >> USB_PIPE_DIR_OFFSET) & DIRECTION_MASK;
1487     intParmas.dataReq.length = (int)acm->intSize;
1488     ret = UsbFillRequest(acm->notifyReq, InterfaceIdToHandle(acm, acm->intPipe->interfaceId), &intParmas);
1489     if (ret != HDF_SUCCESS) {
1490         HDF_LOGE("%s: UsbFillRequest failed, ret = %d", __func__, ret);
1491         goto ERROR;
1492     }
1493     return HDF_SUCCESS;
1494 
1495 ERROR:
1496     AcmFreeNotifyReqeust(acm);
1497     return ret;
1498 }
1499 
AcmReleaseInterfaces(struct AcmDevice * acm)1500 static void AcmReleaseInterfaces(struct AcmDevice *acm)
1501 {
1502     for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1503         if (acm->iface[i]) {
1504             UsbReleaseInterface(acm->iface[i]);
1505             acm->iface[i] = NULL;
1506         }
1507     }
1508     if (acm->ctrIface) {
1509         UsbReleaseInterface(acm->ctrIface);
1510         acm->ctrIface = NULL;
1511     }
1512 }
1513 
AcmClaimInterfaces(struct AcmDevice * acm)1514 static int32_t AcmClaimInterfaces(struct AcmDevice *acm)
1515 {
1516     for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1517         acm->iface[i] = GetUsbInterfaceById((const struct AcmDevice *)acm, acm->interfaceIndex[i]);
1518         if (acm->iface[i] == NULL) {
1519             HDF_LOGE("%s: interface%d is null", __func__, acm->interfaceIndex[i]);
1520             goto ERROR;
1521         }
1522     }
1523 
1524     acm->ctrIface = GetUsbInterfaceById((const struct AcmDevice *)acm, USB_CTRL_INTERFACE_ID);
1525     if (acm->ctrIface == NULL) {
1526         HDF_LOGE("%s: GetUsbInterfaceById null", __func__);
1527         goto ERROR;
1528     }
1529 
1530     return HDF_SUCCESS;
1531 
1532 ERROR:
1533     AcmReleaseInterfaces(acm);
1534     return HDF_FAILURE;
1535 }
1536 
AcmCloseInterfaces(struct AcmDevice * acm)1537 static void AcmCloseInterfaces(struct AcmDevice *acm)
1538 {
1539     for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1540         if (acm->devHandle[i]) {
1541             UsbCloseInterface(acm->devHandle[i]);
1542             acm->devHandle[i] = NULL;
1543         }
1544     }
1545     if (acm->ctrDevHandle) {
1546         UsbCloseInterface(acm->ctrDevHandle);
1547         acm->ctrDevHandle = NULL;
1548     }
1549 }
1550 
AcmOpenInterfaces(struct AcmDevice * acm)1551 static int32_t AcmOpenInterfaces(struct AcmDevice *acm)
1552 {
1553     for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
1554         if (acm->iface[i]) {
1555             acm->devHandle[i] = UsbOpenInterface(acm->iface[i]);
1556             if (acm->devHandle[i] == NULL) {
1557                 HDF_LOGE("%s: UsbOpenInterface null", __func__);
1558                 goto ERROR;
1559             }
1560         }
1561     }
1562     acm->ctrDevHandle = UsbOpenInterface(acm->ctrIface);
1563     if (acm->ctrDevHandle == NULL) {
1564         HDF_LOGE("%s: ctrDevHandle UsbOpenInterface null", __func__);
1565         goto ERROR;
1566     }
1567 
1568     return HDF_SUCCESS;
1569 
1570 ERROR:
1571     AcmCloseInterfaces(acm);
1572     return HDF_FAILURE;
1573 }
1574 
AcmGetPipes(struct AcmDevice * acm)1575 static int32_t AcmGetPipes(struct AcmDevice *acm)
1576 {
1577     acm->dataInPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_IN);
1578     if (acm->dataInPipe == NULL) {
1579         HDF_LOGE("dataInPipe is null");
1580         goto ERROR;
1581     }
1582 
1583     acm->dataOutPipe = GetPipe(acm, USB_PIPE_TYPE_BULK, USB_PIPE_DIRECTION_OUT);
1584     if (acm->dataOutPipe == NULL) {
1585         HDF_LOGE("dataOutPipe is null");
1586         goto ERROR;
1587     }
1588 
1589     acm->ctrPipe = EnumePipe(acm, acm->ctrIface->info.interfaceIndex, USB_PIPE_TYPE_CONTROL, USB_PIPE_DIRECTION_OUT);
1590     if (acm->ctrPipe == NULL) {
1591         HDF_LOGE("ctrPipe is null");
1592         goto ERROR;
1593     }
1594 
1595     acm->intPipe = GetPipe(acm, USB_PIPE_TYPE_INTERRUPT, USB_PIPE_DIRECTION_IN);
1596     if (acm->intPipe == NULL) {
1597         HDF_LOGE("intPipe is null");
1598         goto ERROR;
1599     }
1600 
1601     acm->readSize = acm->dataInPipe->maxPacketSize;
1602     acm->writeSize = acm->dataOutPipe->maxPacketSize;
1603     acm->ctrlSize = acm->ctrPipe->maxPacketSize;
1604     acm->intSize = acm->intPipe->maxPacketSize;
1605 
1606     return HDF_SUCCESS;
1607 
1608 ERROR:
1609     AcmFreePipes(acm);
1610     return HDF_FAILURE;
1611 }
1612 
AcmFreeRequests(struct AcmDevice * acm)1613 static void AcmFreeRequests(struct AcmDevice *acm)
1614 {
1615     if (g_syncRequest != NULL) {
1616         UsbFreeRequest(g_syncRequest);
1617         g_syncRequest = NULL;
1618     }
1619     AcmFreeReadRequests(acm);
1620     AcmFreeNotifyReqeust(acm);
1621     AcmFreeWriteRequests(acm);
1622     AcmWriteBufFree(acm);
1623 }
1624 
AcmAllocRequests(const struct AcmDevice * acm)1625 static int32_t AcmAllocRequests(const struct AcmDevice *acm)
1626 {
1627     int32_t ret;
1628 
1629     if (AcmWriteBufAlloc(acm) < 0) {
1630         HDF_LOGE("%s: AcmWriteBufAlloc failed", __func__);
1631         return HDF_ERR_MALLOC_FAIL;
1632     }
1633 
1634     for (int32_t i = 0; i < ACM_NW; i++) {
1635         struct AcmWb *snd = (struct AcmWb *)&(acm->wb[i]);
1636         snd->request = UsbAllocRequest(
1637             InterfaceIdToHandle((struct AcmDevice *)acm, acm->dataOutPipe->interfaceId), 0, acm->writeSize);
1638         snd->instance = (struct AcmDevice *)acm;
1639         if (snd->request == NULL) {
1640             HDF_LOGE("%s:%d snd request fail", __func__, __LINE__);
1641             goto ERROR_ALLOC_WRITE_REQ;
1642         }
1643     }
1644 
1645     ret = AcmAllocNotifyRequest((struct AcmDevice *)acm);
1646     if (ret != HDF_SUCCESS) {
1647         HDF_LOGE("%s:%d AcmAllocNotifyRequest fail", __func__, __LINE__);
1648         goto ERROR_ALLOC_INT_REQ;
1649     }
1650 
1651     ret = AcmAllocReadRequests((struct AcmDevice *)acm);
1652     if (ret) {
1653         HDF_LOGE("%s:%d AcmAllocReadRequests fail", __func__, __LINE__);
1654         goto ERROR_ALLOC_READ_REQ;
1655     }
1656 
1657     return HDF_SUCCESS;
1658 
1659 ERROR_ALLOC_READ_REQ:
1660     AcmFreeNotifyReqeust((struct AcmDevice *)acm);
1661 ERROR_ALLOC_INT_REQ:
1662     AcmFreeWriteRequests((struct AcmDevice *)acm);
1663 ERROR_ALLOC_WRITE_REQ:
1664     AcmWriteBufFree((struct AcmDevice *)acm);
1665     return HDF_FAILURE;
1666 }
1667 
AcmInit(struct AcmDevice * acm)1668 static int32_t AcmInit(struct AcmDevice *acm)
1669 {
1670     int32_t ret;
1671 
1672     if (acm->initFlag) {
1673         HDF_LOGE("%{public}s: initFlag is true", __func__);
1674         return HDF_SUCCESS;
1675     }
1676 
1677     ret = UsbInitHostSdk(NULL);
1678     if (ret != HDF_SUCCESS) {
1679         HDF_LOGE("%{public}s: UsbInitHostSdk failed", __func__);
1680         return HDF_ERR_IO;
1681     }
1682     acm->session = NULL;
1683 
1684     ret = AcmClaimInterfaces(acm);
1685     if (ret != HDF_SUCCESS) {
1686         HDF_LOGE("%{public}s: AcmClaimInterfaces failed", __func__);
1687         goto ERROR_CLAIM_INTERFACES;
1688     }
1689 
1690     ret = AcmOpenInterfaces(acm);
1691     if (ret != HDF_SUCCESS) {
1692         HDF_LOGE("%{public}s: AcmOpenInterfaces failed", __func__);
1693         goto ERROR_OPEN_INTERFACES;
1694     }
1695 
1696     ret = AcmGetPipes(acm);
1697     if (ret != HDF_SUCCESS) {
1698         HDF_LOGE("%{public}s: AcmGetPipes failed", __func__);
1699         goto ERROR_GET_PIPES;
1700     }
1701 
1702     ret = AcmAllocRequests(acm);
1703     if (ret != HDF_SUCCESS) {
1704         HDF_LOGE("%{public}s: AcmAllocRequests failed", __func__);
1705         goto ERROR_ALLOC_REQS;
1706     }
1707 
1708     acm->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
1709     acm->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
1710     acm->lineCoding.bParityType = USB_CDC_NO_PARITY;
1711     acm->lineCoding.bDataBits = DATA_BITS_LENGTH;
1712     acm->initFlag = true;
1713 
1714     return HDF_SUCCESS;
1715 
1716 ERROR_ALLOC_REQS:
1717     AcmFreePipes(acm);
1718 ERROR_GET_PIPES:
1719     AcmCloseInterfaces(acm);
1720 ERROR_OPEN_INTERFACES:
1721     AcmReleaseInterfaces(acm);
1722 ERROR_CLAIM_INTERFACES:
1723     UsbExitHostSdk(acm->session);
1724     acm->session = NULL;
1725     return ret;
1726 }
1727 
AcmRelease(struct AcmDevice * acm)1728 static void AcmRelease(struct AcmDevice *acm)
1729 {
1730     if (!(acm->initFlag)) {
1731         HDF_LOGE("%s:%d: initFlag is false", __func__, __LINE__);
1732         return;
1733     }
1734 
1735     AcmCloseInterfaces(acm);
1736     AcmReleaseInterfaces(acm);
1737     AcmFreeRequests(acm);
1738     AcmFreePipes(acm);
1739     UsbExitHostSdk(acm->session);
1740     acm->session = NULL;
1741 
1742     acm->initFlag = false;
1743 }
1744 
UsbSerialDriverInit(struct HdfDeviceObject * device)1745 static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
1746 {
1747     int32_t ret;
1748     struct AcmDevice *acm = NULL;
1749 
1750     if (device == NULL) {
1751         HDF_LOGE("%s: device is null", __func__);
1752         return HDF_ERR_INVALID_OBJECT;
1753     }
1754     acm = (struct AcmDevice *)device->service;
1755     if (acm == NULL) {
1756         return HDF_ERR_INVALID_OBJECT;
1757     }
1758     OsalMutexInit(&acm->readLock);
1759     OsalMutexInit(&acm->writeLock);
1760     HDF_LOGD("%s:%d busNum = %d,devAddr = %d", __func__, __LINE__, acm->busNum, acm->devAddr);
1761 
1762     ret = UsbSerialDeviceAlloc(acm);
1763     if (ret != HDF_SUCCESS) {
1764         HDF_LOGE("%s: Serial Device alloc failed", __func__);
1765     }
1766 
1767     acm->initFlag = false;
1768     g_acmReleaseFlag = false;
1769 
1770     HDF_LOGD("%s:%d init ok!", __func__, __LINE__);
1771 
1772     return ret;
1773 }
1774 
UsbSerialDriverRelease(struct HdfDeviceObject * device)1775 static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
1776 {
1777     struct AcmDevice *acm = NULL;
1778 
1779     if (device == NULL) {
1780         HDF_LOGE("%s: device is null", __func__);
1781         return;
1782     }
1783     acm = (struct AcmDevice *)device->service;
1784     if (acm == NULL) {
1785         HDF_LOGE("%s: acm is null", __func__);
1786         return;
1787     }
1788 
1789     g_acmReleaseFlag = true;
1790 
1791     if (acm->initFlag) {
1792         HDF_LOGE("%s:%d AcmRelease", __func__, __LINE__);
1793         AcmRelease(acm);
1794     }
1795     UsbSeriaDevicelFree(acm);
1796     OsalMutexDestroy(&acm->writeLock);
1797     OsalMutexDestroy(&acm->readLock);
1798     OsalMutexDestroy(&acm->lock);
1799     OsalMemFree(acm);
1800     acm = NULL;
1801     HDF_LOGD("%s:%d exit", __func__, __LINE__);
1802 }
1803 
1804 struct HdfDriverEntry g_usbSerialDriverEntry = {
1805     .moduleVersion = 1,
1806     .moduleName = "usbhost_acm",
1807     .Bind = UsbSerialDriverBind,
1808     .Init = UsbSerialDriverInit,
1809     .Release = UsbSerialDriverRelease,
1810 };
1811 HDF_INIT(g_usbSerialDriverEntry);
1812