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 "usbhost_sdkraw_speed.h"
17
18 #include <dirent.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <inttypes.h>
22 #include <osal_thread.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <sys/mman.h>
29 #include <sys/time.h>
30 #include <unistd.h>
31
32 #include "hdf_base.h"
33 #include "hdf_log.h"
34 #include "hdf_usb_pnp_manage.h"
35 #include "osal_mem.h"
36 #include "osal_time.h"
37 #include "securec.h"
38 #include "usb_ddk_interface.h"
39
40 #define HDF_LOG_TAG USB_HOST_ACM_RAW_API
41
42 #define USB_CTRL_REQ_SIZE 64
43 #define USB_IO_THREAD_STACK_SIZE 8192
44 #define USB_RAW_IO_SLEEP_MS_TIME 500
45 #define USB_RAW_IO_STOP_WAIT_MAX_TIME 2
46
47 static struct AcmDevice *g_acm = NULL;
48 static bool g_stopIoThreadFlag = false;
49 static unsigned int g_speedFlag = 0;
50 static uint64_t g_send_count = 0;
51 static uint64_t g_recv_count = 0;
52 static uint64_t g_byteTotal = 0;
53 static bool g_writeOrRead = TEST_WRITE;
54 static bool g_printData = false;
55 static struct OsalSem sem;
56
57 static void AcmTestBulkCallback(const void *requestArg);
58 static int32_t SerialBegin(struct AcmDevice *acm);
59
UsbIoThread(void * data)60 static int32_t UsbIoThread(void *data)
61 {
62 int32_t ret;
63 struct AcmDevice *acm = (struct AcmDevice *)data;
64
65 for (;;) {
66 if (acm == NULL) {
67 HDF_LOGE("%s:%d acm is NULL", __func__, __LINE__);
68 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME);
69 continue;
70 }
71
72 if (acm->devHandle == NULL) {
73 HDF_LOGE("%s:%d acm->devHandle is NULL!", __func__, __LINE__);
74 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME);
75 continue;
76 }
77
78 ret = UsbRawHandleRequests(acm->devHandle);
79 if (ret < 0) {
80 HDF_LOGE("%s:%d UsbRawHandleRequests failed, ret=%d ", __func__, __LINE__, ret);
81 if (ret == USB_REQUEST_NO_DEVICE) {
82 HDF_LOGE("%s:%d, ret=%d", __func__, __LINE__, ret);
83 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME);
84 }
85 }
86
87 if (g_stopIoThreadFlag) {
88 HDF_LOGD("%s:%d", __func__, __LINE__);
89 g_stopIoThreadFlag = false;
90 break;
91 }
92 }
93
94 HDF_LOGE("%s:%d exit", __func__, __LINE__);
95
96 return HDF_SUCCESS;
97 }
98
UsbIoSendThread(void * data)99 static int32_t UsbIoSendThread(void *data)
100 {
101 struct AcmDevice *acm = (struct AcmDevice *)data;
102
103 for (;;) {
104 OsalSemWait(&sem, HDF_WAIT_FOREVER);
105 if (!g_speedFlag) {
106 if (SerialBegin(acm) != HDF_SUCCESS) {
107 HDF_LOGW("%s:%d SerialBegin error!", __func__, __LINE__);
108 }
109 g_send_count++;
110 }
111 }
112 }
113
UsbStartIo(struct AcmDevice * const acm)114 static int32_t UsbStartIo(struct AcmDevice * const acm)
115 {
116 struct OsalThreadParam threadCfg;
117 int32_t ret;
118
119 HDF_LOGI("%s start", __func__);
120
121 /* create Io thread */
122 (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
123 threadCfg.name = "usb io thread";
124 threadCfg.priority = OSAL_THREAD_PRI_DEFAULT;
125 threadCfg.stackSize = USB_IO_THREAD_STACK_SIZE;
126
127 ret = OsalThreadCreate(&acm->ioThread, (OsalThreadEntry)UsbIoThread, (void *)acm);
128 if (ret != HDF_SUCCESS) {
129 HDF_LOGE("%s:%d OsalThreadCreate failed, ret=%d ", __func__, __LINE__, ret);
130 return ret;
131 }
132
133 ret = OsalThreadStart(&acm->ioThread, &threadCfg);
134 if (ret != HDF_SUCCESS) {
135 HDF_LOGE("%s:%d OsalThreadStart failed, ret=%d ", __func__, __LINE__, ret);
136 return ret;
137 }
138
139 (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
140 threadCfg.name = "usb io send thread";
141 threadCfg.priority = OSAL_THREAD_PRI_DEFAULT;
142 threadCfg.stackSize = USB_IO_THREAD_STACK_SIZE;
143 ret = OsalThreadCreate(&acm->ioSendThread, (OsalThreadEntry)UsbIoSendThread, (void *)acm);
144 if (ret != HDF_SUCCESS) {
145 HDF_LOGE("%s:%d OsalThreadCreate failed, ret=%d ", __func__, __LINE__, ret);
146 return ret;
147 }
148
149 ret = OsalThreadStart(&acm->ioSendThread, &threadCfg);
150 if (ret != HDF_SUCCESS) {
151 HDF_LOGE("%s:%d OsalThreadStart failed, ret=%d ", __func__, __LINE__, ret);
152 return ret;
153 }
154
155 return HDF_SUCCESS;
156 }
157
UsbStopIo(struct AcmDevice * const acm)158 static int32_t UsbStopIo(struct AcmDevice * const acm)
159 {
160 int32_t ret;
161 int32_t i = 0;
162
163 HDF_LOGD("%s:%d", __func__, __LINE__);
164 if (!g_stopIoThreadFlag) {
165 HDF_LOGD("%s:%d", __func__, __LINE__);
166 g_stopIoThreadFlag = true;
167 }
168 HDF_LOGD("%s:%d", __func__, __LINE__);
169
170 while (g_stopIoThreadFlag) {
171 i++;
172 OsalMSleep(USB_RAW_IO_SLEEP_MS_TIME);
173 if (i > USB_RAW_IO_STOP_WAIT_MAX_TIME) {
174 HDF_LOGD("%s:%d", __func__, __LINE__);
175 g_stopIoThreadFlag = false;
176 }
177 }
178 HDF_LOGD("%s:%d", __func__, __LINE__);
179
180 ret = OsalThreadDestroy(&acm->ioThread);
181 if (ret != HDF_SUCCESS) {
182 HDF_LOGE("%s:%d OsalThreadDestroy failed, ret=%d ", __func__, __LINE__, ret);
183 return ret;
184 }
185
186 ret = OsalThreadDestroy(&acm->ioSendThread);
187 if (ret != HDF_SUCCESS) {
188 HDF_LOGE("%s:%d OsalThreadDestroy failed, ret=%d ", __func__, __LINE__, ret);
189 return ret;
190 }
191
192 return HDF_SUCCESS;
193 }
194
UsbGetConfigDescriptor(UsbRawHandle * const devHandle,struct UsbRawConfigDescriptor ** const config)195 static int32_t UsbGetConfigDescriptor(UsbRawHandle * const devHandle, struct UsbRawConfigDescriptor ** const config)
196 {
197 UsbRawDevice *dev = NULL;
198 int32_t activeConfig;
199 int32_t ret;
200
201 if (devHandle == NULL) {
202 HDF_LOGE("%{public}s devHandle is NULL", __func__);
203 return HDF_ERR_INVALID_PARAM;
204 }
205
206 ret = UsbRawGetConfiguration(devHandle, &activeConfig);
207 if (ret != HDF_SUCCESS) {
208 HDF_LOGE("%s:%d UsbRawGetConfiguration failed, ret=%d", __func__, __LINE__, ret);
209 return HDF_FAILURE;
210 }
211 HDF_LOGE("%{public}s activeConfig=%{public}d", __func__, activeConfig);
212 dev = UsbRawGetDevice(devHandle);
213 if (dev == NULL) {
214 HDF_LOGE("%{public}s UsbRawGetDevice failed", __func__);
215 return HDF_FAILURE;
216 }
217
218 ret = UsbRawGetConfigDescriptor(dev, activeConfig, config);
219 if (ret != HDF_SUCCESS) {
220 HDF_LOGE("UsbRawGetConfigDescriptor failed, ret=%d\n", ret);
221 return HDF_FAILURE;
222 }
223
224 return HDF_SUCCESS;
225 }
226
UsbGetBulkEndpoint(struct AcmDevice * const acm,const struct UsbRawEndpointDescriptor * endPoint)227 static int32_t UsbGetBulkEndpoint(struct AcmDevice * const acm, const struct UsbRawEndpointDescriptor *endPoint)
228 {
229 if ((endPoint->endpointDescriptor.bEndpointAddress & USB_DDK_ENDPOINT_DIR_MASK) == USB_DDK_DIR_IN) {
230 /* get bulk in endpoint */
231 acm->dataInEp = OsalMemAlloc(sizeof(struct UsbEndpoint));
232 if (acm->dataInEp == NULL) {
233 HDF_LOGE("%s:%d allocate dataInEp failed", __func__, __LINE__);
234 return HDF_FAILURE;
235 }
236 acm->dataInEp->addr = endPoint->endpointDescriptor.bEndpointAddress;
237 acm->dataInEp->interval = endPoint->endpointDescriptor.bInterval;
238 acm->dataInEp->maxPacketSize = endPoint->endpointDescriptor.wMaxPacketSize;
239 } else {
240 /* get bulk out endpoint */
241 acm->dataOutEp = OsalMemAlloc(sizeof(struct UsbEndpoint));
242 if (acm->dataOutEp == NULL) {
243 HDF_LOGE("%s:%d allocate dataOutEp failed", __func__, __LINE__);
244 return HDF_FAILURE;
245 }
246 acm->dataOutEp->addr = endPoint->endpointDescriptor.bEndpointAddress;
247 acm->dataOutEp->interval = endPoint->endpointDescriptor.bInterval;
248 acm->dataOutEp->maxPacketSize = endPoint->endpointDescriptor.wMaxPacketSize;
249 }
250
251 return HDF_SUCCESS;
252 }
253
UsbParseConfigDescriptorProcess(struct AcmDevice * const acm,const struct UsbRawInterface * const interface,uint8_t interfaceIndex)254 static void UsbParseConfigDescriptorProcess(
255 struct AcmDevice * const acm, const struct UsbRawInterface * const interface, uint8_t interfaceIndex)
256 {
257 uint8_t ifaceClass = interface->altsetting->interfaceDescriptor.bInterfaceClass;
258 uint8_t numEndpoints = interface->altsetting->interfaceDescriptor.bNumEndpoints;
259
260 switch (ifaceClass) {
261 case USB_DDK_CLASS_COMM:
262 acm->ctrlIface = interfaceIndex;
263 acm->notifyEp = OsalMemAlloc(sizeof(struct UsbEndpoint));
264 if (acm->notifyEp == NULL) {
265 HDF_LOGE("%s:%d allocate endpoint failed", __func__, __LINE__);
266 break;
267 }
268 /* get the first endpoint by default */
269 acm->notifyEp->addr = interface->altsetting->endPoint[0].endpointDescriptor.bEndpointAddress;
270 acm->notifyEp->interval = interface->altsetting->endPoint[0].endpointDescriptor.bInterval;
271 acm->notifyEp->maxPacketSize = interface->altsetting->endPoint[0].endpointDescriptor.wMaxPacketSize;
272 break;
273 case USB_DDK_CLASS_CDC_DATA:
274 acm->dataIface = interfaceIndex;
275 for (uint8_t j = 0; j < numEndpoints; j++) {
276 const struct UsbRawEndpointDescriptor *endPoint = &interface->altsetting->endPoint[j];
277 if (UsbGetBulkEndpoint(acm, endPoint) != HDF_SUCCESS) {
278 break;
279 }
280 }
281 break;
282 default:
283 HDF_LOGE("%s:%d wrong descriptor type", __func__, __LINE__);
284 break;
285 }
286 }
287
UsbParseConfigDescriptor(struct AcmDevice * const acm,struct UsbRawConfigDescriptor * const config)288 static int32_t UsbParseConfigDescriptor(struct AcmDevice * const acm, struct UsbRawConfigDescriptor * const config)
289 {
290 if (acm == NULL || config == NULL) {
291 HDF_LOGE("%s:%d acm or config is NULL", __func__, __LINE__);
292 return HDF_ERR_INVALID_PARAM;
293 }
294
295 for (uint8_t i = 0; i < acm->interfaceCnt; i++) {
296 uint8_t interfaceIndex = acm->interfaceIndex[i];
297 const struct UsbRawInterface *interface = config->interface[interfaceIndex];
298
299 int32_t ret = UsbRawClaimInterface(acm->devHandle, interfaceIndex);
300 if (ret != HDF_SUCCESS) {
301 HDF_LOGE("%s:%d claim interface %u failed", __func__, __LINE__, i);
302 return ret;
303 }
304
305 UsbParseConfigDescriptorProcess(acm, interface, interfaceIndex);
306 }
307
308 return HDF_SUCCESS;
309 }
310
UsbAllocDataRequests(struct AcmDevice * const acm)311 static int32_t UsbAllocDataRequests(struct AcmDevice * const acm)
312 {
313 for (int32_t i = 0; i < TEST_CYCLE; i++) {
314 struct AcmDb *snd = &acm->db[i];
315 snd->request = UsbRawAllocRequest(acm->devHandle, 0, acm->dataEp->maxPacketSize);
316 snd->instance = acm;
317 if (snd->request == NULL) {
318 HDF_LOGE("%s: UsbRawAllocRequest failed", __func__);
319 return HDF_ERR_MALLOC_FAIL;
320 }
321 struct UsbRawFillRequestData reqData;
322 reqData.endPoint = acm->dataEp->addr;
323 reqData.numIsoPackets = 0;
324 reqData.callback = AcmTestBulkCallback;
325 reqData.userData = (void *)snd;
326 reqData.timeout = USB_CTRL_SET_TIMEOUT;
327 reqData.buffer = snd->buf;
328 reqData.length = acm->dataSize;
329
330 int32_t ret = UsbRawFillBulkRequest(snd->request, acm->devHandle, &reqData);
331 if (ret != HDF_SUCCESS) {
332 HDF_LOGE("%s: FillInterruptRequest failed, ret=%d", __func__, ret);
333 return HDF_FAILURE;
334 }
335 }
336
337 return HDF_SUCCESS;
338 }
UsbSerialDeviceAlloc(struct AcmDevice * acm)339 static int32_t UsbSerialDeviceAlloc(struct AcmDevice *acm)
340 {
341 struct SerialDevice *port = NULL;
342
343 if (acm == NULL) {
344 HDF_LOGE("%s: acm null pointer", __func__);
345 return HDF_FAILURE;
346 }
347
348 port = (struct SerialDevice *)OsalMemCalloc(sizeof(*port));
349 if (port == NULL) {
350 HDF_LOGE("%s: Alloc usb serial port failed", __func__);
351 return HDF_FAILURE;
352 }
353 if (OsalMutexInit(&port->lock) != HDF_SUCCESS) {
354 HDF_LOGE("%s: init lock fail!", __func__);
355 return HDF_FAILURE;
356 }
357 port->lineCoding.dwDTERate = CPU_TO_LE32(DATARATE);
358 port->lineCoding.bCharFormat = USB_CDC_1_STOP_BITS;
359 port->lineCoding.bParityType = USB_CDC_NO_PARITY;
360 port->lineCoding.bDataBits = DATA_BITS_LENGTH;
361 acm->lineCoding = port->lineCoding;
362 acm->port = port;
363 port->acm = acm;
364
365 return HDF_SUCCESS;
366 }
367
AcmDbAlloc(struct AcmDevice * const acm)368 static int32_t AcmDbAlloc(struct AcmDevice * const acm)
369 {
370 struct AcmDb *db = NULL;
371 int32_t i;
372
373 for (i = 0; i < TEST_CYCLE; i++) {
374 db = &acm->db[i];
375 if (!db->use) {
376 db->use = 1;
377 db->len = 0;
378 return i;
379 }
380 }
381 return -1;
382 }
383
AcmDbIsAvail(struct AcmDevice * const acm)384 static int32_t AcmDbIsAvail(struct AcmDevice * const acm)
385 {
386 int32_t i;
387 int32_t n = TEST_CYCLE;
388
389 for (i = 0; i < TEST_CYCLE; i++) {
390 n -= acm->db[i].use;
391 }
392 return n;
393 }
394
AcmStartdb(struct AcmDevice * acm,struct AcmDb * const db)395 static int32_t AcmStartdb(struct AcmDevice *acm, struct AcmDb * const db)
396 {
397 (void)acm;
398 int32_t ret;
399 ret = UsbRawSubmitRequest(db->request);
400 if (ret != HDF_SUCCESS) {
401 HDF_LOGE("UsbRawSubmitRequest failed, ret=%d", ret);
402 db->use = 0;
403 }
404 return ret;
405 }
406
AcmDataBufAlloc(struct AcmDevice * const acm)407 static int32_t AcmDataBufAlloc(struct AcmDevice * const acm)
408 {
409 struct AcmDb *db = &acm->db[0];
410 int32_t i;
411
412 for (i = 0; i < TEST_CYCLE; i++, db++) {
413 db->buf = OsalMemCalloc(acm->dataEp->maxPacketSize);
414 if (!db->buf) {
415 while (i != 0) {
416 --i;
417 --db;
418 OsalMemFree(db->buf);
419 db->buf = NULL;
420 }
421 return -HDF_ERR_MALLOC_FAIL;
422 } else {
423 memset_s(db->buf, acm->dataSize, 'b', acm->dataSize);
424 db->instance = acm;
425 }
426 }
427 return HDF_SUCCESS;
428 }
429
AcmTestBulkCallback(const void * requestArg)430 static void AcmTestBulkCallback(const void *requestArg)
431 {
432 struct UsbRawRequest *req = (struct UsbRawRequest *)requestArg;
433 struct itimerval new_value, old_value;
434 if (req == NULL) {
435 HDF_LOGE("%s:%{pulib}d req is NULL!", __func__, __LINE__);
436 return;
437 }
438 struct AcmDb *db = (struct AcmDb *)req->userData;
439 if (db == NULL) {
440 HDF_LOGE("%s:%{pulib}d userData(db) is NULL!", __func__, __LINE__);
441 return;
442 }
443
444 if (req->status == USB_REQUEST_COMPLETED) {
445 if (g_byteTotal == 0) {
446 new_value.it_value.tv_sec = TEST_PRINT_TIME;
447 new_value.it_value.tv_usec = 0;
448 new_value.it_interval.tv_sec = TEST_PRINT_TIME;
449 new_value.it_interval.tv_usec = 0;
450 setitimer(ITIMER_REAL, &new_value, &old_value);
451 }
452 g_recv_count++;
453 g_byteTotal += req->actualLength;
454 } else {
455 printf("status error\n");
456 }
457
458 if (g_printData) {
459 for (int32_t i = 0; i < req->actualLength; i++) {
460 printf("%c", req->buffer[i]);
461 }
462 fflush(stdout);
463 } else if (g_recv_count % TEST_RECV_COUNT == 0) {
464 printf("#");
465 fflush(stdout);
466 }
467
468 db->use = 0;
469 OsalSemPost(&sem);
470 }
471
SerialBegin(struct AcmDevice * acm)472 static int32_t SerialBegin(struct AcmDevice *acm)
473 {
474 uint32_t size = acm->dataSize;
475 int32_t ret;
476 struct AcmDb *db = NULL;
477 int32_t dbn;
478 if (AcmDbIsAvail(acm) != 0) {
479 dbn = AcmDbAlloc(acm);
480 } else {
481 HDF_LOGE("no buf\n");
482 return 0;
483 }
484 if (dbn < 0) {
485 HDF_LOGE("AcmDbAlloc failed\n");
486 return HDF_FAILURE;
487 }
488 db = &acm->db[dbn];
489 db->len = acm->dataSize;
490 if (g_writeOrRead == TEST_READ) {
491 memset_s(db->buf, TEST_LENGTH, '0', TEST_LENGTH);
492 }
493 struct UsbRawFillRequestData reqData;
494 reqData.endPoint = acm->dataEp->addr;
495 reqData.numIsoPackets = 0;
496 reqData.callback = AcmTestBulkCallback;
497 reqData.userData = (void *)db;
498 reqData.timeout = USB_CTRL_SET_TIMEOUT;
499 reqData.buffer = db->buf;
500 reqData.length = acm->dataSize;
501
502 ret = UsbRawFillBulkRequest(db->request, acm->devHandle, &reqData);
503 if (ret != HDF_SUCCESS) {
504 HDF_LOGE("%s: FillInterruptRequest failed, ret=%d", __func__, ret);
505 return HDF_FAILURE;
506 }
507
508 ret = AcmStartdb(acm, db);
509 return (int32_t)size;
510 }
511
SignalHandler(int32_t signo)512 static void SignalHandler(int32_t signo)
513 {
514 static uint32_t sigCnt = 0;
515 struct itimerval new_value, old_value;
516 double speed = 0;
517 switch (signo) {
518 case SIGALRM:
519 sigCnt++;
520 if (sigCnt * TEST_PRINT_TIME >= TEST_TIME) {
521 g_speedFlag = 1;
522 break;
523 }
524
525 speed = (g_byteTotal * TEST_FLOAT_COUNT) / (sigCnt * TEST_PRINT_TIME * TEST_BYTE_COUNT * TEST_BYTE_COUNT);
526 printf("\nSpeed:%f MB/s\n", speed);
527 new_value.it_value.tv_sec = TEST_PRINT_TIME;
528 new_value.it_value.tv_usec = 0;
529 new_value.it_interval.tv_sec = TEST_PRINT_TIME;
530 new_value.it_interval.tv_usec = 0;
531 setitimer(ITIMER_REAL, &new_value, &old_value);
532 break;
533 case SIGINT:
534 g_speedFlag = 1;
535 break;
536 default:
537 break;
538 }
539 }
540
ShowHelp(const char * name)541 static void ShowHelp(const char *name)
542 {
543 printf(">> usage:\n");
544 printf(">> %s [<busNum> <devAddr>] <ifaceNum> <w>/<r> [printdata]> \n", name);
545 printf("\n");
546 }
547
CheckParam(int32_t argc,const char * argv[])548 static int32_t CheckParam(int32_t argc, const char *argv[])
549 {
550 int32_t busNum = 1;
551 int32_t devAddr = 2;
552 int32_t ifaceNum = 3;
553 int32_t ret = HDF_SUCCESS;
554
555 if (argc == TEST_SIX_TYPE) {
556 busNum = atoi(argv[TEST_ONE_TYPE]);
557 devAddr = atoi(argv[TEST_TWO_TYPE]);
558 ifaceNum = atoi(argv[TEST_THREE_TYPE]);
559 g_writeOrRead = (strncmp(argv[TEST_FOUR_TYPE], "r", TEST_ONE_TYPE)) ? TEST_WRITE : TEST_READ;
560 if (g_writeOrRead == TEST_READ) {
561 g_printData = (strncmp(argv[TEST_FIVE_TYPE], "printdata", TEST_ONE_TYPE)) ? false : true;
562 }
563 } else if (argc == TEST_FIVE_TYPE) {
564 busNum = atoi(argv[TEST_ONE_TYPE]);
565 devAddr = atoi(argv[TEST_TWO_TYPE]);
566 ifaceNum = atoi(argv[TEST_THREE_TYPE]);
567 g_writeOrRead = (strncmp(argv[TEST_FOUR_TYPE], "r", TEST_ONE_TYPE)) ? TEST_WRITE : TEST_READ;
568 } else if (argc == TEST_THREE_TYPE) {
569 ifaceNum = atoi(argv[TEST_ONE_TYPE]);
570 g_writeOrRead = (strncmp(argv[TEST_TWO_TYPE], "r", TEST_ONE_TYPE)) ? TEST_WRITE : TEST_READ;
571 } else {
572 printf("Error: parameter error!\n\n");
573 ShowHelp(argv[TEST_ZERO_TYPE]);
574 ret = HDF_FAILURE;
575 goto END;
576 }
577 OsalSemInit(&sem, 0);
578
579 g_acm = (struct AcmDevice *)OsalMemCalloc(sizeof(*g_acm));
580 if (g_acm == NULL) {
581 HDF_LOGE("%s: Alloc usb serial device failed", __func__);
582 ret = HDF_FAILURE;
583 goto END;
584 }
585
586 g_acm->busNum = busNum;
587 g_acm->devAddr = devAddr;
588 g_acm->interfaceCnt = 1;
589 g_acm->interfaceIndex[0] = ifaceNum;
590 END:
591 return ret;
592 }
593
InitUsbDdk(void)594 static int32_t InitUsbDdk(void)
595 {
596 int32_t ret;
597 struct UsbSession *session = NULL;
598 UsbRawHandle *devHandle = NULL;
599
600 ret = UsbRawInit(&session);
601 if (ret != HDF_SUCCESS) {
602 HDF_LOGE("%s: UsbRawInit failed", __func__);
603 goto END;
604 }
605
606 ret = UsbSerialDeviceAlloc(g_acm);
607 if (ret != HDF_SUCCESS) {
608 HDF_LOGE("%s: UsbSerialDeviceAlloc failed", __func__);
609 goto END;
610 }
611
612 devHandle = UsbRawOpenDevice(session, g_acm->busNum, g_acm->devAddr);
613 if (devHandle == NULL) {
614 HDF_LOGE("%{public}s: UsbRawOpenDevice failed", __func__);
615 ret = HDF_FAILURE;
616 goto END;
617 }
618 g_acm->devHandle = devHandle;
619 ret = UsbGetConfigDescriptor(devHandle, &g_acm->config);
620 if (ret != HDF_SUCCESS) {
621 HDF_LOGE("%s: UsbGetConfigDescriptor failed", __func__);
622 goto END;
623 }
624 ret = UsbParseConfigDescriptor(g_acm, g_acm->config);
625 if (ret != HDF_SUCCESS) {
626 HDF_LOGE("%s: UsbParseConfigDescriptor failed", __func__);
627 ret = HDF_FAILURE;
628 goto END;
629 }
630 g_acm->dataSize = TEST_LENGTH;
631 g_acm->dataEp = (g_writeOrRead == TEST_WRITE) ? g_acm->dataOutEp : g_acm->dataInEp;
632
633 ret = AcmDataBufAlloc(g_acm);
634 if (ret < 0) {
635 HDF_LOGE("%s: AcmDataBufAlloc failed", __func__);
636 goto END;
637 }
638 ret = UsbAllocDataRequests(g_acm);
639 if (ret < 0) {
640 HDF_LOGE("%s: UsbAllocDataRequests failed", __func__);
641 goto END;
642 }
643
644 END:
645 return ret;
646 }
647
main(int32_t argc,char * argv[])648 int32_t main(int32_t argc, char *argv[])
649 {
650 int32_t ret;
651 struct timeval time;
652 int32_t i;
653
654 ret = CheckParam(argc, (const char **)argv);
655 if (ret != HDF_SUCCESS) {
656 goto END;
657 }
658
659 ret = InitUsbDdk();
660 if (ret != HDF_SUCCESS) {
661 goto END;
662 }
663
664 ret = UsbStartIo(g_acm);
665 if (ret != HDF_SUCCESS) {
666 HDF_LOGE("%s: UsbAllocReadRequests failed", __func__);
667 goto END;
668 }
669
670 if (signal(SIGINT, SignalHandler) == SIG_ERR) {
671 HDF_LOGE("signal SIGINT failed");
672 return HDF_FAILURE;
673 }
674 if (signal(SIGALRM, SignalHandler) == SIG_ERR) {
675 HDF_LOGE("signal SIGINT failed");
676 return HDF_FAILURE;
677 }
678 gettimeofday(&time, NULL);
679
680 printf("test SDK rawAPI [%s]\n", g_writeOrRead ? "write" : "read");
681
682 for (i = 0; i < TEST_CYCLE; i++) {
683 if (SerialBegin(g_acm) != HDF_SUCCESS) {
684 printf("SerialBegin error!\n");
685 }
686 g_send_count++;
687 }
688
689 while (!g_speedFlag) {
690 OsalMSleep(TEST_SLEEP_TIME);
691 }
692
693 if (UsbStopIo(g_acm) != HDF_SUCCESS) {
694 printf("UsbStopIo error!\n");
695 }
696 END:
697 if (ret != HDF_SUCCESS) {
698 printf("please check whether usb drv so is existing or not,like acm,ecm,if not, remove it and test again!\n");
699 }
700 return ret;
701 }
702