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 <unistd.h>
17
18 #include "hdf_base.h"
19 #include "hdf_log.h"
20 #include "hdf_usb_pnp_manage.h"
21 #include "osal_mem.h"
22 #include "osal_time.h"
23 #include "securec.h"
24 #include "usb_serial_rawapi.h"
25
26 #define HDF_LOG_TAG USB_HOST_ACM_RAW_API
27 #define USB_CTRL_REQ_SIZE 64
28 #define USB_IO_THREAD_STACK_SIZE 8192
29 #define USB_RAW_IO_SLEEP_MS_TIME 100
30 #define USB_RAW_IO_STOP_WAIT_MAX_TIME 3
31
32 static struct UsbRawRequest *g_syncRequest = NULL;
33 static UsbRawIoProcessStatusType g_stopIoStatus = USB_RAW_IO_PROCESS_RUNNING;
34 struct OsalMutex g_stopIoLock;
35 static bool g_rawAcmReleaseFlag = false;
36
37 static int32_t SerialSendCtrlMsg(struct AcmDevice *acm, uint8_t request, uint16_t value, void *buf, uint16_t len);
38 static void AcmWriteBulkCallback(const void *requestArg);
39 static int32_t UsbSerialInit(struct AcmDevice *acm);
40 static void UsbSerialRelease(struct AcmDevice *acm);
41
UsbIoThread(void * data)42 static int32_t UsbIoThread(void *data)
43 {
44 int32_t ret;
45 struct AcmDevice *acm = (struct AcmDevice *)data;
46
47 for (;;) {
48 if (acm == NULL) {
49 HDF_LOGE("%{public}s:%{public}d acm is null", __func__, __LINE__);
50 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME);
51 continue;
52 }
53
54 if (acm->devHandle == NULL) {
55 HDF_LOGE("%{public}s:%{public}d acm->devHandle is null", __func__, __LINE__);
56 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME);
57 continue;
58 }
59
60 ret = UsbRawHandleRequests(acm->devHandle);
61 if ((ret < 0) || (g_stopIoStatus != USB_RAW_IO_PROCESS_RUNNING)) {
62 HDF_LOGE("%{public}s:%{public}d UsbIoThread failed, g_stopIoStatus=%{public}d ret=%{public}d ",
63 __func__, __LINE__, g_stopIoStatus, ret);
64 break;
65 }
66 }
67
68 OsalMutexLock(&g_stopIoLock);
69 g_stopIoStatus = USB_RAW_IO_PROCESS_STOPED;
70 OsalMutexUnlock(&g_stopIoLock);
71
72 HDF_LOGD("%{public}s:%{public}d exit", __func__, __LINE__);
73
74 return HDF_SUCCESS;
75 }
76
UsbStartIo(struct AcmDevice * acm)77 static int32_t UsbStartIo(struct AcmDevice *acm)
78 {
79 struct OsalThreadParam threadCfg;
80 int32_t ret;
81
82 HDF_LOGI("%{public}s start", __func__);
83
84 OsalMutexInit(&g_stopIoLock);
85
86 OsalMutexLock(&g_stopIoLock);
87 g_stopIoStatus = USB_RAW_IO_PROCESS_RUNNING;
88 OsalMutexUnlock(&g_stopIoLock);
89
90 /* create Io thread */
91 (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
92 threadCfg.name = "usb io thread";
93 threadCfg.priority = OSAL_THREAD_PRI_LOW;
94 threadCfg.stackSize = USB_IO_THREAD_STACK_SIZE;
95
96 ret = OsalThreadCreate(&acm->ioThread, (OsalThreadEntry)UsbIoThread, (void *)acm);
97 if (ret != HDF_SUCCESS) {
98 HDF_LOGE("%{public}s:%{public}d OsalThreadCreate failed, ret = %{public}d", __func__, __LINE__, ret);
99 return ret;
100 }
101
102 ret = OsalThreadStart(&acm->ioThread, &threadCfg);
103 if (ret != HDF_SUCCESS) {
104 HDF_LOGE("%{public}s:%{public}d OsalThreadStart failed, ret = %{public}d", __func__, __LINE__, ret);
105 return ret;
106 }
107
108 return HDF_SUCCESS;
109 }
110
UsbStopIo(struct AcmDevice * acm)111 static void UsbStopIo(struct AcmDevice *acm)
112 {
113 int32_t ret;
114 int32_t i = 0;
115
116 if (g_stopIoStatus != USB_RAW_IO_PROCESS_STOPED) {
117 HDF_LOGD("%{public}s:%{public}d not stopped", __func__, __LINE__);
118 OsalMutexLock(&g_stopIoLock);
119 g_stopIoStatus = USB_RAW_IO_PROCESS_STOP;
120 OsalMutexUnlock(&g_stopIoLock);
121 } else {
122 HDF_LOGD("%{public}s:%{public}d stopped", __func__, __LINE__);
123 }
124
125 while (g_stopIoStatus != USB_RAW_IO_PROCESS_STOPED) {
126 i++;
127 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME);
128 if (i > USB_RAW_IO_STOP_WAIT_MAX_TIME) {
129 HDF_LOGD("%{public}s:%{public}d", __func__, __LINE__);
130 break;
131 }
132 }
133
134 ret = OsalThreadDestroy(&acm->ioThread);
135 if (ret != HDF_SUCCESS) {
136 HDF_LOGE("%{public}s:%{public}d OsalThreadDestroy failed, ret = %{public}d", __func__, __LINE__, ret);
137 }
138
139 OsalMutexDestroy(&g_stopIoLock);
140
141 return;
142 }
143
UsbGetConfigDescriptor(UsbRawHandle * devHandle,struct UsbRawConfigDescriptor ** config)144 static int32_t UsbGetConfigDescriptor(UsbRawHandle *devHandle, struct UsbRawConfigDescriptor **config)
145 {
146 UsbRawDevice *dev = NULL;
147 int32_t activeConfig;
148 int32_t ret;
149
150 if (devHandle == NULL) {
151 HDF_LOGE("%{public}s:%{public}d devHandle is null", __func__, __LINE__);
152 return HDF_ERR_INVALID_PARAM;
153 }
154
155 ret = UsbRawGetConfiguration(devHandle, &activeConfig);
156 if (ret) {
157 HDF_LOGE("%{public}s:%{public}d UsbRawGetConfiguration failed, ret = %{public}d", __func__, __LINE__, ret);
158 return HDF_FAILURE;
159 }
160 HDF_LOGE("%{public}s:%{public}d activeConfig = %{public}d", __func__, __LINE__, activeConfig);
161 dev = UsbRawGetDevice(devHandle);
162 if (dev == NULL) {
163 HDF_LOGE("%{public}s:%{public}d UsbRawGetDevice failed", __func__, __LINE__);
164 return HDF_FAILURE;
165 }
166
167 ret = UsbRawGetConfigDescriptor(dev, activeConfig, config);
168 if (ret) {
169 HDF_LOGE("UsbRawGetConfigDescriptor failed, ret = %{public}d", ret);
170 return HDF_FAILURE;
171 }
172
173 return HDF_SUCCESS;
174 }
175
UsbGetBulkEndpoint(struct AcmDevice * acm,const struct UsbRawEndpointDescriptor * endPoint)176 static int32_t UsbGetBulkEndpoint(struct AcmDevice *acm, const struct UsbRawEndpointDescriptor *endPoint)
177 {
178 if ((endPoint->endpointDescriptor.bEndpointAddress & USB_DDK_ENDPOINT_DIR_MASK) == USB_DDK_DIR_IN) {
179 /* get bulk in endpoint */
180 acm->dataInEp = OsalMemAlloc(sizeof(struct UsbEndpoint));
181 if (acm->dataInEp == NULL) {
182 HDF_LOGE("%{public}s:%{public}d allocate dataInEp failed", __func__, __LINE__);
183 return HDF_FAILURE;
184 }
185 acm->dataInEp->addr = endPoint->endpointDescriptor.bEndpointAddress;
186 acm->dataInEp->interval = endPoint->endpointDescriptor.bInterval;
187 acm->dataInEp->maxPacketSize = endPoint->endpointDescriptor.wMaxPacketSize;
188 } else {
189 /* get bulk out endpoint */
190 acm->dataOutEp = OsalMemAlloc(sizeof(struct UsbEndpoint));
191 if (acm->dataOutEp == NULL) {
192 HDF_LOGE("%{public}s:%{public}d allocate dataOutEp failed", __func__, __LINE__);
193 return HDF_FAILURE;
194 }
195 acm->dataOutEp->addr = endPoint->endpointDescriptor.bEndpointAddress;
196 acm->dataOutEp->interval = endPoint->endpointDescriptor.bInterval;
197 acm->dataOutEp->maxPacketSize = endPoint->endpointDescriptor.wMaxPacketSize;
198 }
199
200 return HDF_SUCCESS;
201 }
202
UsbParseConfigDescriptorProcess(struct AcmDevice * acm,const struct UsbRawInterface * interface,uint8_t interfaceIndex)203 static void UsbParseConfigDescriptorProcess(
204 struct AcmDevice *acm, const struct UsbRawInterface *interface, uint8_t interfaceIndex)
205 {
206 uint8_t ifaceClass = interface->altsetting->interfaceDescriptor.bInterfaceClass;
207 uint8_t numEndpoints = interface->altsetting->interfaceDescriptor.bNumEndpoints;
208
209 switch (ifaceClass) {
210 case USB_DDK_CLASS_COMM:
211 acm->ctrlIface = interfaceIndex;
212 acm->notifyEp = OsalMemAlloc(sizeof(struct UsbEndpoint));
213 if (acm->notifyEp == NULL) {
214 HDF_LOGE("%{public}s:%{public}d allocate endpoint failed", __func__, __LINE__);
215 break;
216 }
217 /* get the first endpoint by default */
218 acm->notifyEp->addr = interface->altsetting->endPoint[0].endpointDescriptor.bEndpointAddress;
219 acm->notifyEp->interval = interface->altsetting->endPoint[0].endpointDescriptor.bInterval;
220 acm->notifyEp->maxPacketSize = interface->altsetting->endPoint[0].endpointDescriptor.wMaxPacketSize;
221 break;
222 case USB_DDK_CLASS_CDC_DATA:
223 acm->dataIface = interfaceIndex;
224 for (uint8_t j = 0; j < numEndpoints; j++) {
225 const struct UsbRawEndpointDescriptor *endPoint = &interface->altsetting->endPoint[j];
226 if (UsbGetBulkEndpoint(acm, endPoint) != HDF_SUCCESS) {
227 break;
228 }
229 }
230 break;
231 default:
232 HDF_LOGE("%{public}s:%{public}d wrong descriptor type", __func__, __LINE__);
233 break;
234 }
235 }
236
UsbParseConfigDescriptor(struct AcmDevice * acm,struct UsbRawConfigDescriptor * config)237 static int32_t UsbParseConfigDescriptor(struct AcmDevice *acm, struct UsbRawConfigDescriptor *config)
238 {
239 if ((acm == NULL) || (config == NULL)) {
240 HDF_LOGE("%{public}s:%{public}d acm or config is null", __func__, __LINE__);
241 return HDF_ERR_INVALID_PARAM;
242 }
243
244 for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
245 uint8_t interfaceIndex = acm->interfaceIndex[i];
246 const struct UsbRawInterface *interface = config->interface[interfaceIndex];
247
248 int32_t ret = UsbRawClaimInterface(acm->devHandle, interfaceIndex);
249 if (ret != HDF_SUCCESS) {
250 HDF_LOGE("%{public}s:%{public}d claim interface %{public}hhu failed", __func__, __LINE__, i);
251 continue;
252 }
253
254 UsbParseConfigDescriptorProcess(acm, interface, interfaceIndex);
255 }
256
257 return HDF_SUCCESS;
258 }
259
UsbReleaseInterfaces(struct AcmDevice * acm)260 static void UsbReleaseInterfaces(struct AcmDevice *acm)
261 {
262 if ((acm == NULL) || (acm->devHandle == NULL)) {
263 HDF_LOGE("%{public}s:%{public}d acm is null", __func__, __LINE__);
264 return;
265 }
266
267 (void)UsbRawReleaseInterface(acm->devHandle, acm->ctrlIface);
268 (void)UsbRawReleaseInterface(acm->devHandle, acm->dataIface);
269
270 if (acm->notifyEp) {
271 OsalMemFree(acm->notifyEp);
272 acm->notifyEp = NULL;
273 }
274 if (acm->dataInEp) {
275 OsalMemFree(acm->dataInEp);
276 acm->dataInEp = NULL;
277 }
278 if (acm->dataOutEp) {
279 OsalMemFree(acm->dataOutEp);
280 acm->dataOutEp = NULL;
281 }
282 }
283
UsbAllocWriteRequests(struct AcmDevice * acm)284 static int32_t UsbAllocWriteRequests(struct AcmDevice *acm)
285 {
286 int32_t i;
287
288 for (i = 0; i < ACM_NW; i++) {
289 struct AcmWb *snd = &acm->wb[i];
290 snd->request = UsbRawAllocRequest(acm->devHandle, 0, acm->dataOutEp->maxPacketSize);
291 snd->instance = acm;
292 if (snd->request == NULL) {
293 HDF_LOGE("%{public}s: UsbRawAllocRequest failed", __func__);
294 return HDF_ERR_MALLOC_FAIL;
295 }
296 }
297
298 return HDF_SUCCESS;
299 }
300
UsbFreeWriteRequests(struct AcmDevice * acm)301 static void UsbFreeWriteRequests(struct AcmDevice *acm)
302 {
303 int32_t i;
304 struct AcmWb *snd = NULL;
305
306 for (i = 0; i < ACM_NW; i++) {
307 snd = &acm->wb[i];
308 if (snd->request != NULL) {
309 UsbRawFreeRequest(snd->request);
310 snd->request = NULL;
311 }
312 }
313 }
314
AcmWbAlloc(const struct AcmDevice * acm)315 static int32_t AcmWbAlloc(const struct AcmDevice *acm)
316 {
317 struct AcmWb *wb = NULL;
318 int32_t i;
319
320 for (i = 0; i < ACM_NW; i++) {
321 wb = (struct AcmWb *)&acm->wb[i];
322 if (!wb->use) {
323 wb->use = 1;
324 wb->len = 0;
325 return i;
326 }
327 }
328 return -1;
329 }
330
UsbSerialAllocFifo(struct DataFifo * fifo,uint32_t size)331 static int32_t UsbSerialAllocFifo(struct DataFifo *fifo, uint32_t size)
332 {
333 if (!DataFifoIsInitialized(fifo)) {
334 void *data = OsalMemAlloc(size);
335 if (data == NULL) {
336 HDF_LOGE("%{public}s:allocate failed", __func__);
337 return HDF_ERR_MALLOC_FAIL;
338 }
339 DataFifoInit(fifo, size, data);
340 }
341 return HDF_SUCCESS;
342 }
343
UsbSerialFreeFifo(const struct DataFifo * fifo)344 static void UsbSerialFreeFifo(const struct DataFifo *fifo)
345 {
346 if (fifo == NULL) {
347 HDF_LOGE("%{public}s:%{public}d fifo is null", __func__, __LINE__);
348 return;
349 }
350
351 if (fifo->data != NULL) {
352 OsalMemFree((void *)fifo->data);
353 }
354
355 DataFifoInit((struct DataFifo *)fifo, 0, NULL);
356 }
357
AcmWbIsAvail(const struct AcmDevice * acm)358 static int32_t AcmWbIsAvail(const struct AcmDevice *acm)
359 {
360 int32_t i;
361 int32_t n = ACM_NW;
362
363 OsalMutexLock((struct OsalMutex *)&acm->writeLock);
364 for (i = 0; i < ACM_NW; i++) {
365 n -= acm->wb[i].use;
366 }
367 OsalMutexUnlock((struct OsalMutex *)&acm->writeLock);
368 return n;
369 }
370
AcmStartWb(struct AcmDevice * acm,struct AcmWb * wb)371 static int32_t AcmStartWb(struct AcmDevice *acm, struct AcmWb *wb)
372 {
373 struct UsbRawFillRequestData reqData;
374 int32_t ret;
375 if ((acm == NULL) || (wb == NULL) || (acm->dataOutEp == NULL) || (acm->devHandle == NULL) ||
376 (wb->request == NULL)) {
377 return HDF_ERR_INVALID_PARAM;
378 }
379
380 acm->transmitting++;
381
382 reqData.endPoint = acm->dataOutEp->addr;
383 reqData.numIsoPackets = 0;
384 reqData.callback = AcmWriteBulkCallback;
385 reqData.userData = (void *)wb;
386 reqData.timeout = USB_CTRL_SET_TIMEOUT;
387 reqData.buffer = wb->buf;
388 reqData.length = wb->len;
389
390 ret = UsbRawFillBulkRequest(wb->request, acm->devHandle, &reqData);
391 if (ret) {
392 HDF_LOGE("%{public}s: FillInterruptRequest failed, ret = %{public}d", __func__, ret);
393 return HDF_FAILURE;
394 }
395
396 acm->writeReq = wb->request;
397 ret = UsbRawSubmitRequest(wb->request);
398 if (ret) {
399 HDF_LOGE("UsbRawSubmitRequest failed, ret = %{public}d", ret);
400 wb->use = 0;
401 acm->transmitting--;
402 }
403
404 return ret;
405 }
406
AcmWriteBufAlloc(const struct AcmDevice * acm)407 static int32_t AcmWriteBufAlloc(const struct AcmDevice *acm)
408 {
409 struct AcmWb *wb = (struct AcmWb *)&acm->wb[0];
410 int32_t i;
411
412 for (i = 0; i < ACM_NW; i++, wb++) {
413 wb->buf = OsalMemCalloc(acm->dataOutEp->maxPacketSize);
414 if (!wb->buf) {
415 while (i > 0) {
416 --i;
417 --wb;
418 OsalMemFree(wb->buf);
419 wb->buf = NULL;
420 }
421 return -HDF_ERR_MALLOC_FAIL;
422 }
423 }
424 return HDF_SUCCESS;
425 }
426
AcmWriteBufFree(struct AcmDevice * acm)427 static void AcmWriteBufFree(struct AcmDevice *acm)
428 {
429 struct AcmWb *wb = &acm->wb[0];
430 int32_t i;
431
432 for (i = 0; i < ACM_NW; i++, wb++) {
433 if (wb->buf) {
434 OsalMemFree(wb->buf);
435 wb->buf = NULL;
436 }
437 }
438 return;
439 }
440
AcmWriteBulkCallback(const void * requestArg)441 static void AcmWriteBulkCallback(const void *requestArg)
442 {
443 struct UsbRawRequest *req = (struct UsbRawRequest *)requestArg;
444 if (req == NULL) {
445 HDF_LOGE("%{public}s:%{public}d req is null!", __func__, __LINE__);
446 return;
447 }
448 struct AcmWb *wb = (struct AcmWb *)req->userData;
449 if (wb == NULL) {
450 HDF_LOGE("%{public}s:%{public}d userData(wb) is null!", __func__, __LINE__);
451 return;
452 }
453
454 if (req->status != USB_REQUEST_COMPLETED) {
455 HDF_LOGE("%{public}s: write req failed, status = %{public}d", __func__, req->status);
456 }
457
458 wb->use = 0;
459 }
460
SerialSendCtrlMsg(struct AcmDevice * acm,uint8_t request,uint16_t value,void * buf,uint16_t len)461 static int32_t SerialSendCtrlMsg(struct AcmDevice *acm, uint8_t request, uint16_t value, void *buf, uint16_t len)
462 {
463 struct UsbControlRequestData ctrlReq;
464 int32_t ret;
465
466 if (acm == NULL || buf == NULL) {
467 HDF_LOGE("%{public}s:invalid param", __func__);
468 return HDF_ERR_INVALID_PARAM;
469 }
470 if (acm->ctrlReq == NULL) {
471 acm->ctrlReq = UsbRawAllocRequest(acm->devHandle, 0, USB_CTRL_REQ_SIZE);
472 if (acm->ctrlReq == NULL) {
473 HDF_LOGE("%{public}s: UsbRawAllocRequest failed", __func__);
474 return HDF_ERR_MALLOC_FAIL;
475 }
476 }
477
478 ctrlReq.requestType = USB_DDK_DIR_OUT | USB_DDK_TYPE_CLASS | USB_DDK_RECIP_INTERFACE;
479 ctrlReq.requestCmd = request;
480 ctrlReq.value = CPU_TO_LE16(value);
481 ctrlReq.index = 0;
482 ctrlReq.data = buf;
483 ctrlReq.length = len;
484 ctrlReq.timeout = USB_CTRL_SET_TIMEOUT;
485
486 ret = UsbRawSendControlRequest(acm->ctrlReq, acm->devHandle, &ctrlReq);
487 if (ret < HDF_SUCCESS) {
488 HDF_LOGE("%{public}s: UsbRawSendControlRequest failed, ret=%{public}d", __func__, ret);
489 return ret;
490 }
491 if (acm->ctrlReq->status) {
492 HDF_LOGE("%{public}s status=%{public}d ", __func__, acm->ctrlReq->status);
493 }
494 return HDF_SUCCESS;
495 }
496
UsbSerialDeviceAlloc(struct AcmDevice * acm)497 static int32_t UsbSerialDeviceAlloc(struct AcmDevice *acm)
498 {
499 struct SerialDevice *port = NULL;
500
501 if (acm == NULL) {
502 HDF_LOGE("%{public}s: acm null pointer", __func__);
503 return HDF_FAILURE;
504 }
505
506 port = (struct SerialDevice *)OsalMemCalloc(sizeof(*port));
507 if (port == NULL) {
508 HDF_LOGE("%{public}s: Alloc usb serial port failed", __func__);
509 return HDF_FAILURE;
510 }
511 if (OsalMutexInit(&port->lock) != HDF_SUCCESS) {
512 HDF_LOGE("%{public}s: init lock fail!", __func__);
513 OsalMemFree(port);
514 return HDF_FAILURE;
515 }
516 port->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
517 port->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
518 port->lineCoding.bParityType = USB_CDC_NO_PARITY;
519 port->lineCoding.bDataBits = DATA_BITS_LENGTH;
520 acm->lineCoding = port->lineCoding;
521 acm->port = port;
522 port->acm = acm;
523
524 return HDF_SUCCESS;
525 }
526
UsbSeriaDevicelFree(struct AcmDevice * acm)527 static void UsbSeriaDevicelFree(struct AcmDevice *acm)
528 {
529 struct SerialDevice *port = acm->port;
530
531 if (port == NULL) {
532 HDF_LOGE("%{public}s: port is null", __func__);
533 return;
534 }
535 OsalMemFree(port);
536 port = NULL;
537 }
538
UsbSerialRead(struct SerialDevice * port,struct HdfSBuf * reply)539 static int32_t UsbSerialRead(struct SerialDevice *port, struct HdfSBuf *reply)
540 {
541 struct AcmDevice *acm = port->acm;
542 uint8_t *buf = NULL;
543 int32_t ret = HDF_SUCCESS;
544 uint32_t len;
545
546 for (int32_t i = 0; i < ACM_NR; i++) {
547 if (acm->readReq[i]->status != USB_REQUEST_COMPLETED) {
548 HDF_LOGE("%{public}s:%{public}d i=%{public}d status=%{public}d!",
549 __func__, __LINE__, i, acm->readReq[i]->status);
550 return HDF_FAILURE;
551 }
552 }
553
554 if (DataFifoIsEmpty(&port->readFifo)) {
555 if (!HdfSbufWriteString(reply, NULL)) {
556 HDF_LOGE("%{public}s:%{public}d sbuf write buffer failed", __func__, __LINE__);
557 return HDF_ERR_IO;
558 }
559 return HDF_SUCCESS;
560 }
561
562 buf = (uint8_t *)OsalMemCalloc(DataFifoLen(&port->readFifo) + 1);
563 if (buf == NULL) {
564 HDF_LOGE("%{public}s:%{public}d OsalMemCalloc error", __func__, __LINE__);
565 return HDF_ERR_MALLOC_FAIL;
566 }
567
568 OsalMutexLock(&acm->readLock);
569 len = DataFifoRead(&port->readFifo, buf, DataFifoLen(&port->readFifo));
570 if (len == 0) {
571 HDF_LOGE("%{public}s:%{public}d no data", __func__, __LINE__);
572 ret = HDF_SUCCESS;
573 OsalMutexUnlock(&acm->readLock);
574 goto OUT;
575 }
576 OsalMutexUnlock(&acm->readLock);
577
578 if (!HdfSbufWriteString(reply, (const char *)buf)) {
579 HDF_LOGE("%{public}s:%{public}d sbuf write buffer failed", __func__, __LINE__);
580 ret = HDF_ERR_IO;
581 }
582
583 OUT:
584 OsalMemFree(buf);
585 return ret;
586 }
587
SerialSetBaudrate(struct SerialDevice * port,const struct HdfSBuf * data)588 static int32_t SerialSetBaudrate(struct SerialDevice *port, const struct HdfSBuf *data)
589 {
590 struct AcmDevice *acm = port->acm;
591 uint32_t baudRate = 0;
592
593 if (!HdfSbufReadUint32((struct HdfSBuf *)data, &baudRate)) {
594 HDF_LOGE("%{public}s: sbuf read buffer failed", __func__);
595 return HDF_ERR_IO;
596 }
597 port->lineCoding.dwDTERate = CPU_TO_LE32(baudRate);
598 if (memcmp(&acm->lineCoding, &port->lineCoding, sizeof(struct UsbCdcLineCoding))) {
599 int32_t ret =
600 memcpy_s(&acm->lineCoding, sizeof(struct UsbCdcLineCoding), &port->lineCoding, sizeof(port->lineCoding));
601 if (ret != EOK) {
602 HDF_LOGE("memcpy_s fail, ret=%{public}d", ret);
603 return ret;
604 }
605
606 HDF_LOGE("%{public}s - set line: %{public}d %{public}d %{public}d %{public}d",
607 __func__, (port->lineCoding.dwDTERate), port->lineCoding.bCharFormat,
608 port->lineCoding.bParityType, port->lineCoding.bDataBits);
609
610 ret = SerialSendCtrlMsg(
611 acm, USB_DDK_CDC_REQ_SET_LINE_CODING, 0, &acm->lineCoding, sizeof(struct UsbCdcLineCoding));
612 if (ret) {
613 HDF_LOGE("SerialSendCtrlMsg fail");
614 return ret;
615 }
616 }
617 return HDF_SUCCESS;
618 }
619
SerialGetBaudrate(struct SerialDevice * port,struct HdfSBuf * reply)620 static int32_t SerialGetBaudrate(struct SerialDevice *port, struct HdfSBuf *reply)
621 {
622 uint32_t baudRate = LE32_TO_CPU(port->lineCoding.dwDTERate);
623
624 if (!HdfSbufWriteUint32(reply, baudRate)) {
625 HDF_LOGE("%{public}s:%{public}d sbuf write buffer failed", __func__, __LINE__);
626 return HDF_ERR_IO;
627 }
628
629 HDF_LOGE("%{public}s:%{public}d baudRate=%{public}d", __func__, __LINE__, baudRate);
630
631 return HDF_SUCCESS;
632 }
633
SerialOpen(struct SerialDevice * port,struct HdfSBuf * data)634 static int32_t SerialOpen(struct SerialDevice *port, struct HdfSBuf *data)
635 {
636 struct AcmDevice *acm = NULL;
637 int32_t ret;
638 int32_t cmdType = HOST_ACM_ASYNC_READ;
639
640 if ((port == NULL) || (data == NULL)) {
641 HDF_LOGE("%{public}s: invalid parma", __func__);
642 return HDF_ERR_INVALID_PARAM;
643 }
644
645 acm = port->acm;
646 if (acm == NULL) {
647 HDF_LOGE("%{public}s: invalid parma", __func__);
648 return HDF_ERR_INVALID_PARAM;
649 }
650
651 if (!HdfSbufReadInt32(data, &cmdType)) {
652 HDF_LOGE("%{public}s:%{public}d sbuf read cmdType failed", __func__, __LINE__);
653 return HDF_ERR_INVALID_PARAM;
654 }
655
656 ret = UsbSerialInit(acm);
657 if (ret != HDF_SUCCESS) {
658 HDF_LOGE("%{public}s:%{public}d UsbSerialInit failed", __func__, __LINE__);
659 return HDF_FAILURE;
660 }
661
662 if (cmdType != HOST_ACM_ASYNC_READ) {
663 HDF_LOGD("%{public}s:%{public}d asyncRead success", __func__, __LINE__);
664 return HDF_SUCCESS;
665 }
666
667 ret = UsbSerialAllocFifo(&port->readFifo, READ_BUF_SIZE);
668 if (ret != HDF_SUCCESS) {
669 HDF_LOGE("%{public}s: UsbSerialAllocFifo failed", __func__);
670 return HDF_ERR_INVALID_PARAM;
671 }
672 for (int32_t i = 0; i < ACM_NR; i++) {
673 ret = UsbRawSubmitRequest(acm->readReq[i]);
674 if (ret) {
675 HDF_LOGE("%{public}s: UsbRawSubmitRequest failed, ret=%{public}d ", __func__, ret);
676 goto ERR;
677 }
678 }
679 return HDF_SUCCESS;
680
681 ERR:
682 UsbSerialFreeFifo(&port->readFifo);
683 return ret;
684 }
685
SerialClose(struct SerialDevice * port,struct HdfSBuf * data)686 static int32_t SerialClose(struct SerialDevice *port, struct HdfSBuf *data)
687 {
688 int32_t cmdType = HOST_ACM_SYNC_READ;
689
690 if ((port == NULL) || (data == NULL)) {
691 HDF_LOGE("%{public}s:%{public}d invalid parma", __func__, __LINE__);
692 return HDF_ERR_INVALID_PARAM;
693 }
694
695 if (port->acm == NULL) {
696 HDF_LOGE("%{public}s:%{public}d acm is NULL invalid parma", __func__, __LINE__);
697 return HDF_ERR_INVALID_PARAM;
698 }
699
700 if (!HdfSbufReadInt32(data, &cmdType)) {
701 HDF_LOGE("%{public}s:%{public}d sbuf read cmdType failed", __func__, __LINE__);
702 return HDF_ERR_INVALID_PARAM;
703 }
704
705 if ((cmdType == HOST_ACM_SYNC_READ) || (cmdType == HOST_ACM_SYNC_WRITE) || (cmdType == HOST_ACM_ASYNC_WRITE)) {
706 HDF_LOGD("%{public}s:%{public}d cmdType=%{public}d success", __func__, __LINE__, cmdType);
707 return HDF_SUCCESS;
708 }
709
710 OsalMutexLock(&port->acm->readLock);
711 UsbSerialFreeFifo(&port->readFifo);
712 OsalMutexUnlock(&port->acm->readLock);
713
714 UsbSerialRelease(port->acm);
715
716 return HDF_SUCCESS;
717 }
718
SerialWrite(struct SerialDevice * port,struct HdfSBuf * data)719 static int32_t SerialWrite(struct SerialDevice *port, struct HdfSBuf *data)
720 {
721 struct AcmDevice *acm = NULL;
722 struct AcmWb *wb = NULL;
723 const char *tmp = NULL;
724 int32_t size;
725 int32_t wbn;
726
727 if (port == NULL) {
728 HDF_LOGE("%{public}s: port is null", __func__);
729 return HDF_ERR_INVALID_PARAM;
730 }
731 acm = port->acm;
732 if (acm == NULL) {
733 HDF_LOGE("%{public}s: acm is null", __func__);
734 return HDF_ERR_INVALID_PARAM;
735 }
736 if (AcmWbIsAvail(acm)) {
737 wbn = AcmWbAlloc(acm);
738 } else {
739 HDF_LOGE("%{public}s: no write buf", __func__);
740 return HDF_SUCCESS;
741 }
742 if (wbn < 0 || wbn >= ACM_NW) {
743 HDF_LOGE("%{public}s: AcmWbAlloc failed", __func__);
744 return HDF_FAILURE;
745 }
746 wb = &acm->wb[wbn];
747 if (wb == NULL) {
748 return HDF_FAILURE;
749 }
750 tmp = HdfSbufReadString(data);
751 if (tmp == NULL) {
752 HDF_LOGE("%{public}s: sbuf read buffer failed", __func__);
753 return HDF_ERR_IO;
754 }
755 size = (int32_t)strlen(tmp) + 1;
756 if (acm->dataOutEp != NULL) {
757 size = (size > acm->dataOutEp->maxPacketSize) ? acm->dataOutEp->maxPacketSize : size;
758 if (memcpy_s(wb->buf, acm->dataOutEp->maxPacketSize, tmp, size) != EOK) {
759 HDF_LOGE("%{public}s: memcpy_s fail", __func__);
760 }
761 }
762 wb->len = (uint32_t)size;
763
764 if (AcmStartWb(acm, wb) != HDF_SUCCESS) {
765 HDF_LOGE("%{public}s: AcmStartWb failed", __func__);
766 return HDF_FAILURE;
767 }
768 return size;
769 }
770
AcmStartWbSync(struct AcmDevice * acm,struct AcmWb * wb)771 static int32_t AcmStartWbSync(struct AcmDevice *acm, struct AcmWb *wb)
772 {
773 int32_t ret;
774 int32_t size;
775 struct UsbRequestData requestData;
776
777 requestData.endPoint = acm->dataOutEp->addr;
778 requestData.data = wb->buf;
779 requestData.length = wb->len;
780 requestData.requested = &size;
781 requestData.timeout = USB_CTRL_SET_TIMEOUT;
782
783 acm->writeReq = wb->request;
784 ret = UsbRawSendBulkRequest(wb->request, acm->devHandle, &requestData);
785 if (ret) {
786 HDF_LOGE("UsbRawSendBulkRequest failed, ret=%{public}d", ret);
787 }
788
789 wb->use = 0;
790
791 return ret;
792 }
793
SerialWriteSync(const struct SerialDevice * port,const struct HdfSBuf * data)794 static int32_t SerialWriteSync(const struct SerialDevice *port, const struct HdfSBuf *data)
795 {
796 struct AcmDevice *acm = NULL;
797 struct AcmWb *wb = NULL;
798 const char *tmp = NULL;
799 int32_t size;
800 int32_t wbn;
801
802 if (port == NULL) {
803 HDF_LOGE("%{public}s: invalid parma", __func__);
804 return HDF_ERR_INVALID_PARAM;
805 }
806 acm = port->acm;
807 if (acm == NULL) {
808 HDF_LOGE("%{public}s: invalid parma", __func__);
809 return HDF_ERR_INVALID_PARAM;
810 }
811
812 if (AcmWbIsAvail(acm)) {
813 wbn = AcmWbAlloc(acm);
814 } else {
815 HDF_LOGE("%{public}s: no write buf", __func__);
816 return HDF_SUCCESS;
817 }
818
819 if (wbn >= ACM_NW || wbn < 0) {
820 wbn = 0;
821 }
822 wb = &acm->wb[wbn];
823 if ((wb == NULL) || (wb->buf == NULL)) {
824 return HDF_ERR_INVALID_PARAM;
825 }
826 tmp = HdfSbufReadString((struct HdfSBuf *)data);
827 if (tmp == NULL) {
828 HDF_LOGE("%{public}s: sbuf read buffer failed", __func__);
829 return HDF_ERR_IO;
830 }
831 size = (int32_t)strlen(tmp) + 1;
832 if (acm->dataOutEp == NULL) {
833 return HDF_ERR_IO;
834 }
835 size = (size > acm->dataOutEp->maxPacketSize) ? acm->dataOutEp->maxPacketSize : size;
836 if (memcpy_s(wb->buf, acm->dataOutEp->maxPacketSize, tmp, size) != EOK) {
837 HDF_LOGE("%{public}s: memcpy_s failed", __func__);
838 }
839 wb->len = (uint32_t)size;
840
841 if (AcmStartWbSync(acm, wb) != HDF_SUCCESS) {
842 HDF_LOGE("%{public}s: AcmStartWbSync failed", __func__);
843 return HDF_FAILURE;
844 }
845
846 return size;
847 }
848
UsbSerialReadSync(const struct SerialDevice * port,const struct HdfSBuf * reply)849 static int32_t UsbSerialReadSync(const struct SerialDevice *port, const struct HdfSBuf *reply)
850 {
851 int32_t ret;
852 int32_t size;
853 struct AcmDevice *acm = port->acm;
854 uint8_t *data = NULL;
855 struct UsbRequestData requestData;
856
857 if (g_syncRequest == NULL) {
858 g_syncRequest = UsbRawAllocRequest(acm->devHandle, 0, acm->dataInEp->maxPacketSize);
859 if (g_syncRequest == NULL) {
860 HDF_LOGE("UsbRawAllocRequest g_syncRequest failed");
861 return HDF_ERR_MALLOC_FAIL;
862 }
863 }
864 HDF_LOGD("%{public}s:%{public}d g_syncRequest ", __func__, __LINE__);
865
866 requestData.endPoint = acm->dataInEp->addr;
867 requestData.data = g_syncRequest->buffer;
868 requestData.length = acm->dataInEp->maxPacketSize;
869 requestData.requested = &size;
870 requestData.timeout = USB_CTRL_SET_TIMEOUT;
871
872 ret = UsbRawSendBulkRequest(g_syncRequest, acm->devHandle, &requestData);
873 if (ret) {
874 HDF_LOGE("UsbRawSendBulkRequest failed, ret=%{public}d", ret);
875 return ret;
876 }
877
878 uint32_t count = (uint32_t)g_syncRequest->actualLength;
879 data = (uint8_t *)OsalMemCalloc(count + 1);
880 if (data == NULL) {
881 HDF_LOGE("%{public}s: OsalMemCalloc error", __func__);
882 return HDF_ERR_MALLOC_FAIL;
883 }
884 HDF_LOGD("buffer actualLength:%{public}u", count);
885
886 do {
887 ret = memcpy_s(data, g_syncRequest->actualLength, g_syncRequest->buffer, count);
888 if (ret != EOK) {
889 HDF_LOGE("%{public}s: memcpy_s error", __func__);
890 break;
891 }
892
893 if (!HdfSbufWriteString((struct HdfSBuf *)reply, (char *)data)) {
894 HDF_LOGE("%{public}s: sbuf write buffer failed", __func__);
895 ret = HDF_ERR_IO;
896 break;
897 }
898 } while (0);
899
900 OsalMemFree(data);
901 data = NULL;
902 return ret;
903 }
904
SerialAddOrRemoveInterface(int32_t cmd,const struct SerialDevice * port,const struct HdfSBuf * data)905 static int32_t SerialAddOrRemoveInterface(int32_t cmd, const struct SerialDevice *port, const struct HdfSBuf *data)
906 {
907 (void)cmd;
908 (void)port;
909 (void)data;
910
911 return HDF_SUCCESS;
912 }
913
UsbSerialDeviceDispatch(struct HdfDeviceIoClient * client,int32_t cmd,struct HdfSBuf * data,struct HdfSBuf * reply)914 static int32_t UsbSerialDeviceDispatch(
915 struct HdfDeviceIoClient *client, int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
916 {
917 struct AcmDevice *acm = NULL;
918 struct SerialDevice *port = NULL;
919
920 if ((client == NULL) || (client->device == NULL)) {
921 HDF_LOGE("%{public}s: client or client->device is NULL", __func__);
922 return HDF_ERR_INVALID_OBJECT;
923 }
924
925 if (client->device->service == NULL) {
926 HDF_LOGE("%{public}s: client->device->service is NULL", __func__);
927 return HDF_ERR_INVALID_OBJECT;
928 }
929
930 if (g_rawAcmReleaseFlag == true) {
931 HDF_LOGE("%{public}s: g_rawAcmReleaseFlag is true", __func__);
932 return HDF_FAILURE;
933 }
934
935 acm = (struct AcmDevice *)client->device->service;
936 port = acm->port;
937 if (port == NULL) {
938 return HDF_FAILURE;
939 }
940 switch (cmd) {
941 case CMD_OPEN_PARM:
942 return SerialOpen(port, data);
943 case CMD_CLOSE_PARM:
944 return SerialClose(port, data);
945 case CMD_WRITE_PARM:
946 return SerialWrite(port, data);
947 case CMD_READ_PARM:
948 return UsbSerialRead(port, reply);
949 case CMD_GET_BAUDRATE:
950 return SerialGetBaudrate(port, reply);
951 case CMD_SET_BAUDRATE:
952 return SerialSetBaudrate(port, data);
953 case CMD_WRITE_DATA_SYNC:
954 return SerialWriteSync(port, data);
955 case CMD_READ_DATA_SYNC:
956 return UsbSerialReadSync(port, reply);
957 case CMD_ADD_INTERFACE:
958 case CMD_REMOVE_INTERFACE:
959 return SerialAddOrRemoveInterface(cmd, port, data);
960 default:
961 return HDF_ERR_NOT_SUPPORT;
962 }
963 }
964
965 /* HdfDriverEntry implementations */
UsbSerialDriverBind(struct HdfDeviceObject * device)966 static int32_t UsbSerialDriverBind(struct HdfDeviceObject *device)
967 {
968 struct AcmDevice *acm = NULL;
969 struct UsbPnpNotifyServiceInfo *info = NULL;
970 errno_t err;
971
972 if (device == NULL) {
973 HDF_LOGE("%{public}s: device is null", __func__);
974 return HDF_ERR_INVALID_OBJECT;
975 }
976
977 acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*acm));
978 if (acm == NULL) {
979 HDF_LOGE("%{public}s: Alloc usb serial device failed", __func__);
980 return HDF_FAILURE;
981 }
982 if (OsalMutexInit(&acm->lock) != HDF_SUCCESS) {
983 HDF_LOGE("%{public}s:%{public}d OsalMutexInit fail", __func__, __LINE__);
984 goto ERROR;
985 }
986
987 info = (struct UsbPnpNotifyServiceInfo *)device->priv;
988 if (info != NULL) {
989 acm->busNum = (uint8_t)info->busNum;
990 acm->devAddr = (uint8_t)info->devNum;
991 acm->interfaceCnt = info->interfaceLength;
992 err = memcpy_s((void *)(acm->interfaceIndex), USB_MAX_INTERFACES, (const void *)info->interfaceNumber,
993 info->interfaceLength);
994 if (err != EOK) {
995 HDF_LOGE("%{public}s:%{public}d memcpy_s failed err=%{public}d", __func__, __LINE__, err);
996 goto LOCK_ERROR;
997 }
998 } else {
999 HDF_LOGE("%{public}s:%{public}d info is NULL!", __func__, __LINE__);
1000 goto LOCK_ERROR;
1001 }
1002
1003 device->service = &(acm->service);
1004 device->service->Dispatch = UsbSerialDeviceDispatch;
1005 acm->device = device;
1006 HDF_LOGD("UsbSerialDriverBind=========================OK");
1007 return HDF_SUCCESS;
1008
1009 LOCK_ERROR:
1010 if (OsalMutexDestroy(&acm->lock)) {
1011 HDF_LOGE("%{public}s:%{public}d OsalMutexDestroy fail", __func__, __LINE__);
1012 }
1013 ERROR:
1014 OsalMemFree(acm);
1015 acm = NULL;
1016 return HDF_FAILURE;
1017 }
1018
AcmProcessNotification(const struct AcmDevice * acm,const unsigned char * buf)1019 static void AcmProcessNotification(const struct AcmDevice *acm, const unsigned char *buf)
1020 {
1021 (void)acm;
1022 struct UsbCdcNotification *dr = (struct UsbCdcNotification *)buf;
1023
1024 switch (dr->bNotificationType) {
1025 case USB_DDK_CDC_NOTIFY_NETWORK_CONNECTION:
1026 HDF_LOGE("%{public}s - network connection: %{public}d", __func__, dr->wValue);
1027 break;
1028 case USB_DDK_CDC_NOTIFY_SERIAL_STATE:
1029 HDF_LOGE("the serial State change");
1030 break;
1031 default:
1032 HDF_LOGE("%{public}s-%{public}d received: index %{public}d len %{public}d",
1033 __func__, dr->bNotificationType, dr->wIndex, dr->wLength);
1034 }
1035 }
1036
AcmNotificationBufferProcess(const struct UsbRawRequest * req,struct AcmDevice * acm,unsigned int currentSize,unsigned int expectedSize)1037 static int32_t AcmNotificationBufferProcess(
1038 const struct UsbRawRequest *req, struct AcmDevice *acm, unsigned int currentSize, unsigned int expectedSize)
1039 {
1040 if (acm->nbSize < expectedSize) {
1041 if (acm->nbSize) {
1042 OsalMemFree(acm->notificationBuffer);
1043 acm->nbSize = 0;
1044 }
1045 unsigned int allocSize = expectedSize;
1046 acm->notificationBuffer = (uint8_t *)OsalMemCalloc(allocSize);
1047 if (!acm->notificationBuffer) {
1048 return HDF_FAILURE;
1049 }
1050 acm->nbSize = allocSize;
1051 }
1052 unsigned int copySize = MIN(currentSize, expectedSize - acm->nbIndex);
1053 int32_t ret = memcpy_s(&acm->notificationBuffer[acm->nbIndex], acm->nbSize - acm->nbIndex, req->buffer, copySize);
1054 if (ret != EOK) {
1055 HDF_LOGE("memcpy_s fail ret=%{public}d", ret);
1056 }
1057 acm->nbIndex += copySize;
1058
1059 return HDF_SUCCESS;
1060 }
1061
AcmNotifyReqCallback(const void * requestArg)1062 static void AcmNotifyReqCallback(const void *requestArg)
1063 {
1064 struct UsbRawRequest *req = (struct UsbRawRequest *)requestArg;
1065 if (req == NULL) {
1066 HDF_LOGE("%{public}s:%{public}d req is NULL!", __func__, __LINE__);
1067 return;
1068 }
1069 struct AcmDevice *acm = (struct AcmDevice *)req->userData;
1070 if (acm == NULL) {
1071 HDF_LOGE("%{public}s:%{public}d userData(acm) is NULL!", __func__, __LINE__);
1072 return;
1073 }
1074 struct UsbCdcNotification *dr = (struct UsbCdcNotification *)req->buffer;
1075 if (dr == NULL) {
1076 HDF_LOGE("%{public}s:%{public}d req->buffer(dr) is NULL!", __func__, __LINE__);
1077 return;
1078 }
1079 unsigned int currentSize = (unsigned int)req->actualLength;
1080 unsigned int expectedSize = 0;
1081
1082 HDF_LOGD("Irqstatus:%{public}d,actualLength:%{public}u", req->status, currentSize);
1083
1084 if (req->status != USB_REQUEST_COMPLETED) {
1085 goto EXIT;
1086 }
1087
1088 if (acm->nbIndex) {
1089 dr = (struct UsbCdcNotification *)acm->notificationBuffer;
1090 }
1091 if (dr != NULL) {
1092 expectedSize = sizeof(struct UsbCdcNotification) + LE16_TO_CPU(dr->wLength);
1093 } else {
1094 HDF_LOGE("%{public}s:%{public}d dr is NULL!", __func__, __LINE__);
1095 return;
1096 }
1097 if (currentSize < expectedSize) {
1098 if (AcmNotificationBufferProcess(req, acm, currentSize, expectedSize) != HDF_SUCCESS) {
1099 goto EXIT;
1100 }
1101 currentSize = acm->nbIndex;
1102 }
1103 if (currentSize >= expectedSize) {
1104 AcmProcessNotification(acm, (unsigned char *)dr);
1105 acm->nbIndex = 0;
1106 }
1107
1108 if (UsbRawSubmitRequest(req) != HDF_SUCCESS) {
1109 HDF_LOGE("%{public}s - UsbRawSubmitRequest failed", __func__);
1110 }
1111
1112 EXIT:
1113 HDF_LOGE("%{public}s:%{public}d exit", __func__, __LINE__);
1114 }
1115
AcmReadBulkCallback(const void * requestArg)1116 static void AcmReadBulkCallback(const void *requestArg)
1117 {
1118 struct UsbRawRequest *req = (struct UsbRawRequest *)requestArg;
1119 if (req == NULL) {
1120 HDF_LOGE("%{public}s:%{public}d req is NULL!", __func__, __LINE__);
1121 return;
1122 }
1123 struct AcmDevice *acm = (struct AcmDevice *)req->userData;
1124 if (acm == NULL || acm->port == NULL) {
1125 HDF_LOGE("%{public}s:%{public}d request userData is NULL!", __func__, __LINE__);
1126 return;
1127 }
1128 size_t size = (size_t)req->actualLength;
1129
1130 if (req->status != USB_REQUEST_COMPLETED) {
1131 HDF_LOGW("%{public}s: the request is failed, status=%{public}d", __func__, req->status);
1132 return;
1133 }
1134 HDF_LOGD("Bulk status: %{public}d+size:%{public}zu", req->status, size);
1135 if (size == 0) {
1136 uint8_t *data = req->buffer;
1137 OsalMutexLock(&acm->readLock);
1138 if (DataFifoIsFull(&acm->port->readFifo)) {
1139 DataFifoSkip(&acm->port->readFifo, size);
1140 }
1141 uint32_t count = DataFifoWrite(&acm->port->readFifo, data, size);
1142 if (count != size) {
1143 HDF_LOGW("%{public}s: write %{public}u less than expected %{public}zu", __func__, count, size);
1144 }
1145 OsalMutexUnlock(&acm->readLock);
1146 }
1147
1148 if (UsbRawSubmitRequest(req) != HDF_SUCCESS) {
1149 HDF_LOGE("%{public}s UsbRawSubmitRequest failed", __func__);
1150 }
1151 }
1152
UsbAllocReadRequests(struct AcmDevice * acm)1153 static int32_t UsbAllocReadRequests(struct AcmDevice *acm)
1154 {
1155 struct UsbRawFillRequestData reqData;
1156 uint32_t size = acm->dataInEp->maxPacketSize;
1157
1158 for (int32_t i = 0; i < ACM_NR; i++) {
1159 acm->readReq[i] = UsbRawAllocRequest(acm->devHandle, 0, size);
1160 if (!acm->readReq[i]) {
1161 HDF_LOGE("readReq request failed");
1162 return HDF_ERR_MALLOC_FAIL;
1163 }
1164
1165 reqData.endPoint = acm->dataInEp->addr;
1166 reqData.numIsoPackets = 0;
1167 reqData.callback = AcmReadBulkCallback;
1168 reqData.userData = (void *)acm;
1169 reqData.timeout = USB_CTRL_SET_TIMEOUT;
1170 reqData.length = size;
1171
1172 int32_t ret = UsbRawFillBulkRequest(acm->readReq[i], acm->devHandle, &reqData);
1173 if (ret != HDF_SUCCESS) {
1174 HDF_LOGE("%{public}s: FillBulkRequest failed, ret=%{public}d", __func__, ret);
1175 return HDF_FAILURE;
1176 }
1177 }
1178
1179 return HDF_SUCCESS;
1180 }
1181
UsbFreeReadRequests(struct AcmDevice * acm)1182 static void UsbFreeReadRequests(struct AcmDevice *acm)
1183 {
1184 int32_t i;
1185
1186 if (acm == NULL) {
1187 HDF_LOGE("%{public}s: acm is NULL", __func__);
1188 return;
1189 }
1190
1191 for (i = 0; i < ACM_NR; i++) {
1192 if (acm->readReq[i]) {
1193 UsbRawFreeRequest(acm->readReq[i]);
1194 acm->readReq[i] = NULL;
1195 }
1196 }
1197 }
1198
UsbAllocNotifyRequest(struct AcmDevice * acm)1199 static int32_t UsbAllocNotifyRequest(struct AcmDevice *acm)
1200 {
1201 struct UsbRawFillRequestData fillRequestData;
1202 if ((acm == NULL) || (acm->notifyEp == NULL)) {
1203 HDF_LOGE("%{public}s: acm or notifyEp is NULL", __func__);
1204 return HDF_ERR_INVALID_OBJECT;
1205 }
1206 uint32_t size = acm->notifyEp->maxPacketSize;
1207 int32_t ret;
1208
1209 acm->notifyReq = UsbRawAllocRequest(acm->devHandle, 0, size);
1210 if (!acm->notifyReq) {
1211 HDF_LOGE("notifyReq request fail");
1212 return HDF_ERR_MALLOC_FAIL;
1213 }
1214
1215 fillRequestData.endPoint = acm->notifyEp->addr;
1216 fillRequestData.length = size;
1217 fillRequestData.numIsoPackets = 0;
1218 fillRequestData.callback = AcmNotifyReqCallback;
1219 fillRequestData.userData = (void *)acm;
1220 fillRequestData.timeout = USB_CTRL_SET_TIMEOUT;
1221
1222 ret = UsbRawFillInterruptRequest(acm->notifyReq, acm->devHandle, &fillRequestData);
1223 if (ret) {
1224 HDF_LOGE("%{public}s: FillInterruptRequest failed, ret=%{public}d", __func__, ret);
1225 return HDF_FAILURE;
1226 }
1227
1228 return HDF_SUCCESS;
1229 }
1230
UsbFreeNotifyReqeust(struct AcmDevice * acm)1231 static void UsbFreeNotifyReqeust(struct AcmDevice *acm)
1232 {
1233 int32_t ret;
1234
1235 if ((acm == NULL) || (acm->notifyReq == NULL)) {
1236 HDF_LOGE("%{public}s: acm or notifyReq is NULL", __func__);
1237 return;
1238 }
1239
1240 ret = UsbRawFreeRequest(acm->notifyReq);
1241 if (ret == HDF_SUCCESS) {
1242 acm->notifyReq = NULL;
1243 } else {
1244 HDF_LOGE("%{public}s: UsbFreeNotifyReqeust failed, ret=%{public}d", __func__, ret);
1245 }
1246 }
1247
UsbAllocRequests(struct AcmDevice * acm,int32_t ret)1248 static void UsbAllocRequests(struct AcmDevice *acm, int32_t ret)
1249 {
1250 ret = UsbAllocWriteRequests(acm);
1251 if (ret < 0) {
1252 HDF_LOGE("%{public}s:%{public}d UsbAllocWriteRequests failed", __func__, __LINE__);
1253 ret = HDF_FAILURE;
1254 goto ERR_ALLOC_WRITE_REQS;
1255 }
1256 ret = UsbAllocNotifyRequest(acm);
1257 if (ret) {
1258 HDF_LOGE("%{public}s:%{public}d UsbAllocNotifyRequests failed", __func__, __LINE__);
1259 goto ERR_ALLOC_NOTIFY_REQ;
1260 }
1261 ret = UsbAllocReadRequests(acm);
1262 if (ret) {
1263 HDF_LOGE("%{public}s:%{public}d UsbAllocReadRequests failed", __func__, __LINE__);
1264 goto ERR_ALLOC_READ_REQS;
1265 }
1266 ret = UsbStartIo(acm);
1267 if (ret) {
1268 HDF_LOGE("%{public}s:%{public}d UsbAllocReadRequests failed", __func__, __LINE__);
1269 goto ERR_START_IO;
1270 }
1271
1272 acm->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
1273 acm->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
1274 acm->lineCoding.bParityType = USB_CDC_NO_PARITY;
1275 acm->lineCoding.bDataBits = DATA_BITS_LENGTH;
1276
1277 ret = UsbRawSubmitRequest(acm->notifyReq);
1278 if (ret) {
1279 HDF_LOGE("%{public}s:%{public}d UsbRawSubmitRequest failed", __func__, __LINE__);
1280 goto ERR_SUBMIT_REQ;
1281 }
1282 ret = AcmWriteBufAlloc(acm);
1283 if (ret < 0) {
1284 HDF_LOGE("%{public}s:%{public}d AcmWriteBufAlloc failed", __func__, __LINE__);
1285 ret = HDF_FAILURE;
1286 goto ERR_ALLOC_WRITE_BUF;
1287 }
1288 ERR_SUBMIT_REQ:
1289 UsbStopIo(acm);
1290 ERR_START_IO:
1291 UsbFreeReadRequests(acm);
1292 ERR_ALLOC_READ_REQS:
1293 UsbFreeNotifyReqeust(acm);
1294 ERR_ALLOC_NOTIFY_REQ:
1295 UsbFreeWriteRequests(acm);
1296 ERR_ALLOC_WRITE_REQS:
1297 AcmWriteBufFree(acm);
1298 ERR_ALLOC_WRITE_BUF:
1299 UsbReleaseInterfaces(acm);
1300 }
1301
UsbSerialInit(struct AcmDevice * acm)1302 static int32_t UsbSerialInit(struct AcmDevice *acm)
1303 {
1304 struct UsbSession *session = NULL;
1305 UsbRawHandle *devHandle = NULL;
1306 int32_t ret;
1307 if (acm->initFlag) {
1308 HDF_LOGE("%{public}s:%{public}d: initFlag is true", __func__, __LINE__);
1309 return HDF_SUCCESS;
1310 }
1311 ret = UsbRawInit(NULL);
1312 if (ret) {
1313 HDF_LOGE("%{public}s:%{public}d UsbRawInit failed", __func__, __LINE__);
1314 return HDF_ERR_IO;
1315 }
1316 acm->session = session;
1317 devHandle = UsbRawOpenDevice(session, acm->busNum, acm->devAddr);
1318 if (devHandle == NULL) {
1319 HDF_LOGE("%{public}s:%{public}d UsbRawOpenDevice failed", __func__, __LINE__);
1320 ret = HDF_FAILURE;
1321 goto ERR_OPEN_DEVICE;
1322 }
1323 acm->devHandle = devHandle;
1324 ret = UsbGetConfigDescriptor(devHandle, &acm->config);
1325 if (ret) {
1326 HDF_LOGE("%{public}s:%{public}d UsbGetConfigDescriptor failed", __func__, __LINE__);
1327 ret = HDF_FAILURE;
1328 goto ERR_GET_DESC;
1329 }
1330 ret = UsbParseConfigDescriptor(acm, acm->config);
1331 if (ret != HDF_SUCCESS) {
1332 HDF_LOGE("%{public}s:%{public}d UsbParseConfigDescriptor failed", __func__, __LINE__);
1333 ret = HDF_FAILURE;
1334 goto ERR_PARSE_DESC;
1335 }
1336 UsbAllocRequests(acm, ret);
1337 acm->initFlag = true;
1338 HDF_LOGD("%{public}s:%{public}d=========================OK", __func__, __LINE__);
1339 return HDF_SUCCESS;
1340 ERR_PARSE_DESC:
1341 UsbRawFreeConfigDescriptor(acm->config);
1342 acm->config = NULL;
1343 ERR_GET_DESC:
1344 (void)UsbRawCloseDevice(devHandle);
1345 ERR_OPEN_DEVICE:
1346 UsbRawExit(acm->session);
1347 return ret;
1348 }
1349
UsbSerialRelease(struct AcmDevice * acm)1350 static void UsbSerialRelease(struct AcmDevice *acm)
1351 {
1352 if (!(acm->initFlag)) {
1353 HDF_LOGE("%{public}s:%{public}d: initFlag is false", __func__, __LINE__);
1354 return;
1355 }
1356
1357 /* stop io thread and release all resources */
1358 UsbStopIo(acm);
1359 if (g_syncRequest != NULL) {
1360 UsbRawFreeRequest(g_syncRequest);
1361 g_syncRequest = NULL;
1362 }
1363 UsbFreeReadRequests(acm);
1364 UsbFreeNotifyReqeust(acm);
1365 UsbFreeWriteRequests(acm);
1366 AcmWriteBufFree(acm);
1367 UsbReleaseInterfaces(acm);
1368 (void)UsbRawCloseDevice(acm->devHandle);
1369 UsbRawFreeConfigDescriptor(acm->config);
1370 acm->config = NULL;
1371 UsbRawExit(acm->session);
1372
1373 acm->initFlag = false;
1374 }
1375
UsbSerialDriverInit(struct HdfDeviceObject * device)1376 static int32_t UsbSerialDriverInit(struct HdfDeviceObject *device)
1377 {
1378 struct AcmDevice *acm = NULL;
1379 int32_t ret;
1380
1381 if (device == NULL) {
1382 HDF_LOGE("%{public}s:%{public}d device is null", __func__, __LINE__);
1383 return HDF_ERR_INVALID_OBJECT;
1384 }
1385 acm = (struct AcmDevice *)device->service;
1386 if (acm == NULL) {
1387 return HDF_ERR_INVALID_OBJECT;
1388 }
1389 OsalMutexInit(&acm->readLock);
1390 OsalMutexInit(&acm->writeLock);
1391
1392 ret = UsbSerialDeviceAlloc(acm);
1393 if (ret != HDF_SUCCESS) {
1394 HDF_LOGE("%{public}s:%{public}d UsbSerialDeviceAlloc failed", __func__, __LINE__);
1395 }
1396
1397 acm->initFlag = false;
1398 g_rawAcmReleaseFlag = false;
1399
1400 HDF_LOGD("%{public}s:%{public}d init ok!", __func__, __LINE__);
1401
1402 return ret;
1403 }
1404
UsbSerialDriverRelease(struct HdfDeviceObject * device)1405 static void UsbSerialDriverRelease(struct HdfDeviceObject *device)
1406 {
1407 struct AcmDevice *acm = NULL;
1408 if (device == NULL) {
1409 HDF_LOGE("%{public}s: device is null", __func__);
1410 return;
1411 }
1412
1413 acm = (struct AcmDevice *)device->service;
1414 if (acm == NULL) {
1415 HDF_LOGE("%{public}s: acm is null", __func__);
1416 return;
1417 }
1418
1419 g_rawAcmReleaseFlag = true;
1420
1421 if (acm->initFlag) {
1422 HDF_LOGE("%{public}s:%{public}d UsbSerialRelease", __func__, __LINE__);
1423 UsbSerialRelease(acm);
1424 }
1425 UsbSeriaDevicelFree(acm);
1426 OsalMutexDestroy(&acm->writeLock);
1427 OsalMutexDestroy(&acm->readLock);
1428 OsalMutexDestroy(&acm->lock);
1429 OsalMemFree(acm);
1430 acm = NULL;
1431 device->service = NULL;
1432 HDF_LOGD("%{public}s:%{public}d exit", __func__, __LINE__);
1433 }
1434
1435 struct HdfDriverEntry g_usbSerialRawDriverEntry = {
1436 .moduleVersion = 1,
1437 .moduleName = "usbhost_acm_rawapi",
1438 .Bind = UsbSerialDriverBind,
1439 .Init = UsbSerialDriverInit,
1440 .Release = UsbSerialDriverRelease,
1441 };
1442 HDF_INIT(g_usbSerialRawDriverEntry);
1443