summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/usb/storage/Class/mscTransport.c
blob: a30f6f358263518c1555c6ca5f9c06382765ca37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
********************************************************************************************************************
*                                              usb host driver
*
*                              (c) Copyright 2007-2010, javen.China
*                                       All Rights Reserved
*
* File Name     : mscTransport.c
*
* Author        : javen
*
* Version       : 2.0
*
* Date          : 2010.03.02
*
* Description   :
*
* History       :
*
********************************************************************************************************************
*/
#include  "usb_os_platform.h"
#include  "error.h"
#include  "usb_host_common.h"
#include  "urb.h"
#include  "usb_gen_hub.h"
#include  "usb_msc_i.h"
#include  "mscTransport.h"
#include  "Scsi2.h"

/*
*******************************************************************************
*                     mscUrbCallBack
*
* Description:
*    urb call back函数
*
* Parameters:
*    urb : input. 需要提交的URB
*
* Return value:
*    无
*
* note:
*
*
*******************************************************************************
*/
static void mscUrbCallBack(struct urb *urb)
{
    hal_sem_t urb_done = (hal_sem_t)urb->context;

    if (urb_done != NULL)
    {
        hal_sem_post(urb_done);
    }
    else
    {
        hal_log_err("ERR: mscUrbCallBack: urb_done == NULL");
    }
}

/*
*******************************************************************************
*                     mscTimeOut
*
* Description:
*
*
* Parameters:
*
*
* Return value:
*
*
* note:
*
*
*******************************************************************************
*/
static void mscTimeOut(void *parg)
{
    __mscDev_t *mscDev = (__mscDev_t *)parg;

    if (mscDev == NULL)
    {
        hal_log_err("ERR: mscTimeOut: mscDev == NULL");
        return;
    }

    hal_log_info("ERR: mscTimeOut: CurrentUrb = %x", (unsigned int)mscDev->CurrentUrb);

    if (mscDev->ScsiCmnd)
    {
        hal_log_err("ERR: mscTimeOut, Cmnd = %x, Timeout = %dms, retries = %d, allowed = %d",
                   ((__u8 *)(mscDev->ScsiCmnd->cmnd.CommandBlock))[0],
                   mscDev->ScsiCmnd->cmnd.Timeout,
                   mscDev->ScsiCmnd->retries,
                   mscDev->ScsiCmnd->allowed);
    }

    if (mscDev->busy)
    {
        usb_unlink_urb(mscDev->CurrentUrb);
        mscDev->CurrentUrb->status = -ETIMEDOUT;
    }

    return;
}

/*
*******************************************************************************
*                     mscUsbTransport
*
* Description:
*    发送URB
*
* Parameters:
*    mscDev   :  input. 目标设备
*    TimeOut  :  input. 超时时间
*
* Return value:
*    返回URB状态
*
* note:
*
*
*******************************************************************************
*/
static int mscUsbTransport(__mscDev_t *mscDev, unsigned int TimeOut)
{
    int ret = 0;
    __u8 err = 0;

    if (mscDev == NULL)
    {
        hal_log_err("ERR: mscUSBTransport: input error, mscDev = %x", mscDev);
        return -EINVAL;
    }

    if (mscDev->state != MSC_DEV_ONLINE)
    {
        hal_log_err("ERR: mscUSBTransport: Can't transport for device is not online");
        return -EINVAL;
    }

    /* fill URB */
        mscDev->CurrentUrb->context       = (void *)mscDev->UrbWait;
        mscDev->CurrentUrb->actual_length = 0;
        mscDev->CurrentUrb->error_count   = 0;
        mscDev->CurrentUrb->status        = 0;
        /* 设置buffer传输方式, 如果使用msc的buff,则不使用DMA,需要palloc分配内存 */
        mscDev->CurrentUrb->transfer_flags = URB_ASYNC_UNLINK;
    //    mscDev->CurrentUrb->transfer_flags = URB_ASYNC_UNLINK | URB_NO_SETUP_DMA_MAP;
    
    //    if (mscDev->CurrentUrb->transfer_buffer == mscDev->iobuf)
    //    {
    //        mscDev->CurrentUrb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
    //    }
    

    mscDev->CurrentUrb->transfer_dma = 0;
    mscDev->CurrentUrb->setup_dma    = 0;

    /* create timer */
    if (TimeOut)
    {
        // unsigned long time_interval = rt_tick_from_millisecond(TimeOut);
        mscDev->TimerHdle = osal_timer_create("mscTime_timer",
                               (timeout_func)mscTimeOut,
                               (void *)mscDev,
                               TimeOut / (1000 / CONFIG_HZ),
                               OSAL_TIMER_FLAG_ONE_SHOT);
        if (mscDev->TimerHdle == NULL)
        {
            hal_log_err("PANIC : create timer for urb false\n");
            return -1;
        }
    }
    /* set mscDev busy */
    mscDev->busy = 1;
    /* submit urb */
    ret = usb_submit_urb(mscDev->CurrentUrb, 0);
    if (ret != 0)
    {
        hal_log_err("ERR: submit urb failed. ret = %d", ret);
        if (mscDev->TimerHdle)
        {
            osal_timer_delete(mscDev->TimerHdle);
            mscDev->TimerHdle = NULL;
        }

        mscDev->busy = 0;
        return -EINVAL;
    }

    /* start timer */
    if (mscDev->TimerHdle)
    {
        osal_timer_start(mscDev->TimerHdle);
    }

    /* wait urb done */
    hal_sem_timedwait(mscDev->UrbWait, TimeOut / (1000 / CONFIG_HZ));
    /* urb is done, then set mscDev free */
    mscDev->busy = 0;

    /* kill timer */
    if (mscDev->TimerHdle)
    {
        osal_timer_stop(mscDev->TimerHdle);
        osal_timer_delete(mscDev->TimerHdle);
        mscDev->TimerHdle = NULL;
    }

    return mscDev->CurrentUrb->status;
}

/*
*******************************************************************************
*                     mscCtrlMsg
*
* Description:
*    USB控制消息函数
*
* Parameters:
*    mscDev         :  input.  目标设备
*    Pipe           :  input.  URB通道
*    Request        :  input.  请求类型
*    RequestType    :  input.  请求方向(读/写)和请求类型
*    Value          :  input.  值
*    Index          :  input.  索引
*    Buffer         :  input.  输入/输出缓存区
*    BufferLen      :  input.  输入/输出缓存区大小
*    TimeOut        :  input.  URB超时时间
*
* Return value:
*    返回URB状态
*
* note:
*
*
*******************************************************************************
*/
static int mscSendCtrlReq(__mscDev_t *mscDev,
                            int Pipe,
                            __u8  Request,
                            __u8  RequestType,
                            __u16 Value,
                            __u16 Index,
                            void *Buffer,
                            __u16 BufferLen,
                            unsigned int TimeOut)
{
    if (mscDev == NULL)
    {
        hal_log_err("ERR: USBStorageCtrlMsg : mscDev = %x", mscDev);
        return USB_ERR_BAD_ARGUMENTS;
    }

      /* 填充控制请求结构体 */
      mscDev->CtrlReq->bRequest     = Request;                 // 请求类型
      mscDev->CtrlReq->bRequestType = RequestType;             // 请求方向(读/写)和请求类型
      mscDev->CtrlReq->wValue       = cpu_to_le16(Value);       // 值, 使用小端字节序
      mscDev->CtrlReq->wIndex       = cpu_to_le16(Index);       // 索引, 使用小端字节序
      mscDev->CtrlReq->wLength      = cpu_to_le16(BufferLen);   // 缓冲区大小, 使用小端字节序
      /* 填充并提交Urb */
      memset(mscDev->CurrentUrb, 0x00, sizeof(struct urb));
      usb_fill_control_urb(mscDev->CurrentUrb,
                           mscDev->pusb_dev,
                           Pipe,
                           (unsigned char *)mscDev->CtrlReq,
                           Buffer,
                           BufferLen,
                           mscUrbCallBack,
                           NULL);
      /* 发送请求并等待响应 */
      return mscUsbTransport(mscDev, TimeOut);
}

/*
*******************************************************************************
*                     mscClearHalt
*
* Description:
*    清除端点状态
*
* Parameters:
*    mscDev     :  input. 目标设备
*    Pipe       :  input. urb的pipe
*
* Return value:
*    返回URB执行结果
*
* note:
*
*
*******************************************************************************
*/
static int mscClearHalt(__mscDev_t *mscDev, unsigned int Pipe)
{
    int Result = 0;
    unsigned int endp = usb_pipeendpoint(Pipe);

    if (usb_pipein(Pipe))
    {
        endp |= USB_DIR_IN;
    }

    Result = mscSendCtrlReq(mscDev,
                            mscDev->CtrlOut,
                            USB_REQ_CLEAR_FEATURE,
                            USB_RECIP_ENDPOINT,
                            USB_ENDPOINT_HALT,
                            (__u16)endp,
                            NULL,
                            0,
                            USB_STOR_CTRL_MSG_TIME);

    /* reset the endpoint toggle */
    if (Result >= 0)
    {
        usb_settoggle(mscDev->pusb_dev, usb_pipeendpoint(Pipe), usb_pipeout(Pipe), 0);
    }

    if (Result == 0)
    {
        Result = USB_ERR_SUCCESS;
    }
    else
    {
        Result = USB_ERR_UNKOWN_ERROR;
    }

    return Result;
}

/*
*******************************************************************************
*                     AnalyseBlukUrbState
*
* Description:
*    分析Bulk Urb状态
*
* Parameters:
*    mscDev     :  input. 目标设备
*    UrbState   :  input. URB执行状态
*    Pipe       :  input. urb的pipe
*    WantLen    :  input. 原始要求传输数据长度
*    ActLen     :  input. 实际传输数据长度
*
* Return value:
*    返回USB操作结果
*
* note:
*
*
*******************************************************************************
*/
static int AnalyseBulkUrbState(__mscDev_t *mscDev,
                                 int UrbState,
                                 int Pipe,
                                 unsigned int WantLen,
                                 unsigned int ActLen)
{
    switch (UrbState)
    {
        case 0:     /* no error code; did we send all the data? */
            if (WantLen != ActLen)
            {
                hal_log_err("Wrn: short transfer, urb_state(%d), want_len(%d), real_len(%d)",
                           UrbState, WantLen, ActLen);
                return USB_STOR_XFER_SHORT;
            }

            return USB_STOR_XFER_GOOD;

        case -EPIPE:    /* stalled */
            hal_log_err("ERR: ep stalled, need clear feature");

            /* for control endpoints, (used by CB[I]) a stall indicates a failed command */
            if (usb_pipecontrol(Pipe))
            {
                hal_log_err("stall on control pipe, urb_state(%d), want_len(%d), real_len(%d)",
                           UrbState, WantLen, ActLen);
                return USB_STOR_XFER_STALLED;
            }

            /* for other sorts of endpoint, clear the stall */
            if (mscClearHalt(mscDev, Pipe) != USB_ERR_SUCCESS)
            {
                hal_log_err("ERR: mscClearHalt ep failed, urb_state(%d), want_len(%d), real_len(%d)",
                           UrbState, WantLen, ActLen);
                return USB_STOR_XFER_ERROR;
            }

            return USB_STOR_XFER_STALLED;

        /* timeout or excessively long NAK */
        case -ETIMEDOUT:
            hal_log_err("ERR: timeout or NAK, urb_state(%d), want_len(%d), real_len(%d)",
                       UrbState, WantLen, ActLen);
            return USB_STOR_XFER_TIME_OUT;

        /* babble - the device tried to send more than we wanted to read */
        case -EOVERFLOW:
            hal_log_err("ERR: babble, data overflow, urb_state(%d), want_len(%d), real_len(%d)",
                       UrbState, WantLen, ActLen);
            return USB_STOR_XFER_LONG;

        /* the transfer was cancelled by abort, disconnect, or timeout */
        case -ECONNRESET:
            hal_log_err("ERR: transfer cancelled, urb_state(%d), want_len(%d), real_len(%d)",
                       UrbState, WantLen, ActLen);
            return USB_STOR_XFER_ERROR;

        /* short scatter-gather read transfer */
        case -EREMOTEIO:
            hal_log_err("ERR: short read transfer, urb_state(%d), want_len(%d), real_len(%d)",
                       UrbState, WantLen, ActLen);
            return USB_STOR_XFER_SHORT;

        /* abort or disconnect in progress */
        case -EIO:
            hal_log_err("ERR: abort or disconnect in progress, urb_state(%d), want_len(%d), real_len(%d)",
                       UrbState, WantLen, ActLen);
            return USB_STOR_XFER_ERROR;

        default:
            hal_log_err("ERR: unkown urb state, urb_state(%d), want_len(%d), real_len(%d)",
                       UrbState, WantLen, ActLen);
            return USB_STOR_XFER_ERROR;
    }
}

/*
*******************************************************************************
*                     mscSendBlukReq
*
* Description:
*    发送bulk请求
*
* Parameters:
*    mscDev     :  input. 目标设备
*    Pipe       :  input. urb的pipe
*    Buffer     :  input. 数据缓冲区
*    BufferLen  :  input. 数据缓冲区大小
*    ActLen     :  input. 实际传输数据长度
*    TimeOut    :  input. URB超时时间
*
* Return value:
*    返回USB操作结果
*
* note:
*
*
*******************************************************************************
*/
static int mscSendBulkReq(__mscDev_t *mscDev,
                            int Pipe,
                            void *Buffer,
                            unsigned int BufferLen,
                            unsigned int *ActLen,
                            unsigned int TimeOut)
{
    /* fill urb */
    memset(mscDev->CurrentUrb, 0x00, sizeof(struct urb));
    usb_fill_bulk_urb(mscDev->CurrentUrb,
                      mscDev->pusb_dev,
                      Pipe,
                      Buffer,
                      BufferLen,
                      mscUrbCallBack,
                      NULL);
    /* submit urb */
    mscUsbTransport(mscDev, TimeOut);

    if (ActLen)
    {
        *ActLen = (unsigned int)(mscDev->CurrentUrb->actual_length);
    }

    return AnalyseBulkUrbState(mscDev,
                               mscDev->CurrentUrb->status,
                               Pipe,
                               BufferLen,
                               (unsigned int)(mscDev->CurrentUrb->actual_length));
}

/*
*******************************************************************************
*                     GetMaxLun
*
* Description:
*    获取当前设备的最大Lun编号,如果获取失败,则默认设备只有1个Lun
*
* Parameters:
*    mscDev  :  input. 目标设备
*
* Return value:
*    Lun 编号
*
* note:
*
*
*******************************************************************************
*/
unsigned int mscGetMaxLun(__mscDev_t *mscDev)
{
    int ret = 0;
    unsigned int MaxLun = 0;

    if (mscDev == NULL)
    {
        hal_log_err("ERR: GetMaxLun: input error, mscDev = %x", mscDev);
        return 0;
    }

    hal_sem_wait(mscDev->DevLock);
    /* issue the command */
    ret = mscSendCtrlReq(mscDev,
                         mscDev->CtrlIn,
                         USB_BULK_GET_MAX_LUN,
                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
                         0,
                         mscDev->InterfaceNo,
                         mscDev->iobuf,
                         1,
                         USB_STOR_CTRL_MSG_TIME);

    if (ret == 0)
    {
        hal_log_info("[msc]: GetMaxLUN successful, max lun is %d", mscDev->iobuf[0]);
        MaxLun = mscDev->iobuf[0];
    }
    else
    {
        hal_log_info("[msc]: GetMaxLUN failed, max lun is zero");
        MaxLun = 0;

        if (ret == -EPIPE)
        {
            mscClearHalt(mscDev, mscDev->BulkIn);
            mscClearHalt(mscDev, mscDev->BulkOut);
        }
    }

    hal_sem_post(mscDev->DevLock);
    return MaxLun;
}

/*
*******************************************************************************
*                     mscBoReset
*
* Description:
*
*
* Parameters:
*
*
* Return value:
*
*
* note:
*
*
*******************************************************************************
*/
#if 0
static int mscBoReset(__mscDev_t *mscDev)
{
    int ret = 0;

    if (mscDev == NULL)
    {
        printf("ERR: mscBoReset: input error, mscDev = %x", mscDev);
        return USB_ERR_BAD_ARGUMENTS;
    }

    /* send reset request */
    ret = mscSendCtrlReq(mscDev,
                         mscDev->CtrlOut,
                         USB_BULK_RESET_REQUEST,
                         USB_TYPE_CLASS | USB_RECIP_INTERFACE,
                         0,
                         mscDev->InterfaceNo,
                         NULL,
                         0,
                         USB_STOR_CTRL_MSG_TIME);

    if (ret != USB_ERR_SUCCESS)
    {
        printf("ERR: Soft reset failed 1");
        return ret;
    }

    /* clear ep status */
    ret = mscClearHalt(mscDev, mscDev->BulkIn);

    if (ret != USB_ERR_SUCCESS)
    {
        printf("ERR: Soft reset failed 2");
        return ret;
    }

    ret = mscClearHalt(mscDev, mscDev->BulkOut);

    if (ret != USB_ERR_SUCCESS)
    {
        printf("ERR: Soft reset failed 3");
        return ret;
    }

    return ret;
}
#endif

/*
*******************************************************************************
*                     mscPortReset
*
* Description:
*    重置设备
*
* Parameters:
*    mscDev  :  input. 目标设备
*
* Return value:
*    返回成功或失败
*
* note:
*
*
*******************************************************************************
*/
static int mscPortReset(__mscDev_t *mscDev)
{
    int ret = 0;

    if (mscDev == NULL)
    {
        hal_log_err("ERR: mscPortReset: input error, mscDev = %x", mscDev);
        return USB_ERR_BAD_ARGUMENTS;
    }

    /* device online? */
    if (mscDev->state == MSC_DEV_OFFLINE)
    {
        hal_log_err("ERR: mscPortReset: device is offline");
        return USB_ERR_IO_DEVICE_OFFLINE;
    }

    /* reset a multi-interface device must be wariness */
    if (mscDev->pusb_dev->actconfig->desc.bNumInterfaces != 1)
    {
        hal_log_err("ERR: Refusing to reset a multi-interface device");
        return USB_ERR_IO_DEVICE_BUSY;
    }

    /* reset device */
    ret = usb_reset_device(mscDev->pusb_dev);

    if (ret != 0)
    {
        hal_log_err("ERR: reset device failed");
        return USB_ERR_RESET_POERT_FAILED;
    }

    return USB_ERR_SUCCESS;
}

int mscResetRecovery(__mscDev_t *mscDev)
{
    return mscPortReset(mscDev);
}

/*
*******************************************************************************
*                     mscBoTransport
*
* Description:
*
*
* Parameters:
*
*
* Return value:
*
*
* note:
*
*
*******************************************************************************
*/
int mscBoTransport(__mscDev_t *mscDev, __ScsiCmnd_t *ScsiCmnd)
{
    __CBW_t *CBW  = NULL;
    __CSW_t *CSW  = NULL;
    unsigned int ActLen  = 0;
    unsigned int TimeOut = 0;
    unsigned int CSWRepeat = 0;
    int Pipe    = 0;
    int ret     = 0;

    if (mscDev == NULL || ScsiCmnd == NULL)
    {
        hal_log_err("ERR: mscBoTransport: input error, mscDev = %x,ScsiCmnd = %x", mscDev, ScsiCmnd);
        return USB_STOR_TRANSPORT_ERROR;
    }

    /* is scsi command valid */
    if (ScsiCmnd->DataTransferLength)
    {
        switch (ScsiCmnd->cmnd.data_direction)
        {
            case DATA_FROM_DEVICE:
                Pipe = mscDev->BulkIn;
                break;

            case DATA_TO_DEVICE:
                Pipe = mscDev->BulkOut;
                break;

            default:
                Pipe = 0;
                hal_log_err("ERR: data phase do not kwon data direction");
                return USB_STOR_TRANSPORT_ERROR;
        }
    }

    hal_sem_wait(mscDev->DevLock);
    mscDev->ScsiCmnd = ScsiCmnd;
    //----------------------------------------------------
    //  Command Block Transport  (CBW)
    //----------------------------------------------------
    /* build the active CBW */
    CBW = (__CBW_t *)mscDev->iobuf;
    memset(CBW, 0, sizeof(__CBW_t));
    mscDev->Tag++;
    TimeOut = USB_STOR_CBW_CSW_TIME;
    CBW->dCBWSignature          = CBW_SIGNATURE;
    CBW->dCBWTag                = mscDev->Tag;
    CBW->dCBWDataTransferLength = ScsiCmnd->DataTransferLength;
    CBW->bmCBWFlags             = (ScsiCmnd->cmnd.data_direction == DATA_FROM_DEVICE)
                                  ? CBW_FLAGS_DATA_IN : CBW_FLAGS_DATA_OUT;
    CBW->bCBWLUN                = ScsiCmnd->cmnd.dwLun;
    CBW->bCBWCBLength           = ScsiCmnd->cmnd.CBLen;
    memcpy((void *)CBW->CBWCB, ScsiCmnd->cmnd.CommandBlock, CBW->bCBWCBLength);
    /* Command Block Transport */
    ret = mscSendBulkReq(mscDev,
                         mscDev->BulkOut,
                         CBW,
                         sizeof(__CBW_t),
                         NULL,
                         TimeOut);

    if (ret != USB_STOR_XFER_GOOD)
    {
        hal_log_err("ERR: mscBoTransport: Command Block Transport failed, reset");
        mscDev->ResetRecovery(mscDev);
        ret = USB_STOR_TRANSPORT_ERROR;
        goto TransportDone;
    }

    //----------------------------------------------------
    //  Data Transport
    //----------------------------------------------------
    if (ScsiCmnd->DataTransferLength)
    {
        TimeOut = ScsiCmnd->cmnd.Timeout;
        ret = mscSendBulkReq(mscDev,
                             Pipe,
                             ScsiCmnd->buffer,
                             ScsiCmnd->DataTransferLength,
                             &ActLen,
                             TimeOut);

        /* 有些设备在read/write数据时会发生endpoint stall的情况,
        解除stall可能需要进行clear feature操作,所以需要进行重试
        */
        if (ret == USB_STOR_XFER_STALLED)
        {
            __u8  Command = 0;
            Command = ((__u8 *)(ScsiCmnd->cmnd.CommandBlock))[0];

            if (Command == SCSI_READ6 || Command == SCSI_READ10 || Command == SCSI_READ16
                || Command == SCSI_WRITE6 || Command == SCSI_WRITE10 || Command == SCSI_WRITE16)
            {
                hal_log_err("ERR: Command(%x) execute failed, for ep stall, need retry", Command);
                mscDev->ResetRecovery(mscDev);
                ret = USB_STOR_TRANSPORT_ERROR;
                goto TransportDone;
            }
        }

        if (ret == USB_STOR_XFER_ERROR)
        {
            hal_log_err("ERR: mscBoTransport: Data Transport failed, reset");
            mscDev->ResetRecovery(mscDev);
            ret = USB_STOR_TRANSPORT_ERROR;
            goto TransportDone;
        }

        ScsiCmnd->ActualLength = ActLen;
    }

    //----------------------------------------------------
    //  Command Status Transport (CSW)
    //----------------------------------------------------
RETRY_CSW:

    if (ScsiCmnd->DataTransferLength)
    {
        TimeOut = USB_STOR_CBW_CSW_TIME;
    }
    else
    {
        TimeOut = ScsiCmnd->cmnd.Timeout;
    }

    CSW = (__CSW_t *)mscDev->iobuf;
    memset(CSW, 0, sizeof(__CSW_t));
    ret = mscSendBulkReq(mscDev,
                         mscDev->BulkIn,
                         CSW,
                         sizeof(__CSW_t),
                         &ActLen,
                         TimeOut);

    if (ret == USB_STOR_XFER_SHORT && ActLen == 0 && ++CSWRepeat < MAX_BOT_CSW_REPEAT)
    {
        hal_log_err("ERR: Received 0-length CSW, must Receive the CSW again");
        goto RETRY_CSW;
    }

    if (ret == USB_STOR_XFER_STALLED && ++CSWRepeat < MAX_BOT_CSW_REPEAT)
    {
        hal_log_err("ERR: EP stall, must Receive the CSW again");
        goto RETRY_CSW;
    }

    if (ret != USB_STOR_XFER_GOOD)
    {
        hal_log_err("ERR: unkown error happen during receive cmd(%x)'s CSW, must reset",
                   ((__u8 *)(ScsiCmnd->cmnd.CommandBlock))[0]);
        mscDev->ResetRecovery(mscDev);
        ret = USB_STOR_TRANSPORT_ERROR;
        goto TransportDone;
    }

    /* check CSW size */
    if (sizeof(__CSW_t) != ActLen)
    {
        hal_log_err("ERR: Invalid Csw size: (%d, %d), must reset", sizeof(__CSW_t), ActLen);
        mscDev->ResetRecovery(mscDev);
        ret = USB_STOR_TRANSPORT_ERROR;
        goto TransportDone;
    }

    /* check CSW Signature */
    if (CSW->dCSWSignature != CSW_SIGNATURE)
    {
        hal_log_err("ERR: Invalid Csw Signature: %x, must reset", CSW->dCSWSignature);
        mscDev->ResetRecovery(mscDev);
        ret = USB_STOR_TRANSPORT_ERROR;
        goto TransportDone;
    }

    /* check CSW Tags */
    if (CSW->dCSWTag != mscDev->Tag)
    {
        hal_log_err("ERR: Invalid Csw Tags: (%x, %x), must reset", mscDev->Tag, CSW->dCSWTag);
        mscDev->ResetRecovery(mscDev);
        ret = USB_STOR_TRANSPORT_ERROR;
        goto TransportDone;
    }

    /* check CSW Status, command failed, need sense */
    if (CSW->bCSWStatus == 1)
    {
        hal_log_err("WRN: command(%x) failed, need sense", ((__u8 *)(ScsiCmnd->cmnd.CommandBlock))[0]);
        ret = USB_STOR_TRANSPORT_FAILED;
        goto TransportDone;
    }

    /* check CSW Status, Phase Error */
    if (CSW->bCSWStatus == 2)
    {
        hal_log_err("ERR: command Phase Error, must reset");
        ret = USB_STOR_TRANSPORT_ERROR;
        goto TransportDone;
    }

    mscDev->ScsiCmnd = NULL;
    hal_sem_post(mscDev->DevLock);
    return USB_STOR_TRANSPORT_GOOD;
TransportDone:
    mscDev->ScsiCmnd = NULL;
    hal_sem_post(mscDev->DevLock);
    return ret;
}


/*
*******************************************************************************
*                     mscBoStopTransport
*
* Description:
*
*
* Parameters:
*
*
* Return value:
*
*
* note:
*
*
*******************************************************************************
*/
int mscBoStopTransport(__mscDev_t *mscDev)
{
    if (mscDev == NULL)
    {
        hal_log_err("ERR: mscBoStopTransport: input error, mscDev = %x", mscDev);
        return USB_ERR_BAD_ARGUMENTS;
    }

    if (mscDev->TimerHdle)
    {
        osal_timer_stop(mscDev->TimerHdle);
        osal_timer_delete(mscDev->TimerHdle);
        mscDev->TimerHdle = NULL;
    }

    if (mscDev->busy)
    {
        usb_unlink_urb(mscDev->CurrentUrb);
        mscDev->CurrentUrb->status = -ESHUTDOWN;
    }

    return USB_ERR_SUCCESS;
}