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