xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usbhost/source3/usb_host_p3/drvMassStorage_3.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2006-2008 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // (��MStar Confidential Information��) by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 #if defined(MSOS_TYPE_NOS)
95 #include "../USBHostConfig_3.h"
96 #include <stdlib.h>
97 #ifndef ATV_SERISE_USE
98 #include <stdio.h>
99 #endif
100 #include <string.h>
101 #ifndef ATV_SERISE_USE
102 #include "../../include/datatype.h"
103 #else
104 #include "UsbHostDataDef.h"
105 #endif
106 #include "../drvHostLib_3.h"
107 #include "../drvHost200_3.h"
108 #include "../drvscsi_3.h"
109 #include "../drvMassStorage_3.h"
110 #ifdef ATV_SERISE_USE
111 #include "uart.h"
112 #else
113 //#include "drvmiu.h"
114 #endif
115 #include "../drvUsbMain_3.h"
116 #include "MsCommon.h"
117 #include "MsIRQ.h"
118 #include "MsOS.h"
119 
120 #define DRV_MASS_STORAGE_DBG(x)  //x;
121 #define DRV_MASS_STORAGE_MSG(x)  MS_CRITICAL_MSG(x)
122 
123 #define ErrorRetryCount1 3
124 
125 UINT32 gwTagPort3=0x88888888;
126 extern U8 gSpeed_Port3;
127 
128 extern void Dump_Data_Port3(UINT16 addr,UINT16 sz);
129 #define Enable_UHCI_Recovery
130 U8 RecoveryFlag_Port3=0;
131 
132 
133 //===============================================================================================
134 //===============================================================================================
135 //===============================================================================================
136 //============================= Group-1: Mass Storage Class Implement ===========================
137 //===============================================================================================
138 //===============================================================================================
139 //===============================================================================================
140 //***************************************************************************************
141 // Function Name: Host20_MSCD_ClearFeature_Port3
142 // Description:
143 // Input:
144 // Output:
145 // Status:S
146 //***************************************************************************************
147 code  UINT8 HOST20_CLEAR_FEATURE_PORT3[8]     = {0x02,0x01,0x00,0x00,0x81,0x00,0x00,0x00}; //
148 
Host20_MSCD_ClearFeature_Port3(struct us_data * psMassStorage)149 UINT8 Host20_MSCD_ClearFeature_Port3(struct us_data *psMassStorage)
150 {
151   UINT8 bTemp;
152   UINT8 bCMD[8];
153 
154 
155   memcpy(bCMD,HOST20_CLEAR_FEATURE_PORT3,8);
156   //bCMD[4]=bEdNum;
157   if (psMassStorage->srb->sc_data_direction==SCSI_DATA_WRITE)
158     bCMD[4]=psAttachDevice_Port3->saCD[0].sInterface[0].sED[ pOTGH_PT_BLK_Port3->bOutQHDArrayNum].bED_EndpointAddress;
159   else
160     bCMD[4]=psAttachDevice_Port3->saCD[0].sInterface[0].sED[ pOTGH_PT_BLK_Port3->bInQHDArrayNum].bED_EndpointAddress;
161 
162   //if (pOTGH_PT_BLK_Port3->bInQHDArrayNum==0) bCMD[4]=0x81;
163   //else bCMD[4]=0x82;
164   //printf("clear stall:%02bx\n",bCMD[4]);
165 
166   if (flib_Host20_Issue_Control_Port3 (1,bCMD,0x08,&bTemp)>0)
167   {
168 
169     //printf(">>> Host20_MSCD_ClearFeature_Port3 Fail ...\r\n");
170     return (0);
171   }
172   else
173   {
174     //printf("Clear stall succeed\n");
175     return (1);
176   }
177 }
178 
179 #if 1
180 code   UINT8 HOST20_MSCD_GET_MAX_LUN_PORT3[8]     = {0xA1,0xFE,0x00,0x00,0x00,0x00,0x01,0x00}; //Get MAX LUN
181 
182 //***************************************************************************************
183 // Function Name: Host20_MSCD_GetMaxLUN_Port3
184 // Description:
185 // Input:
186 // Output:
187 // Status:S
188 //***************************************************************************************
Host20_MSCD_GetMaxLUN_Port3(void)189 UINT8 Host20_MSCD_GetMaxLUN_Port3(void)
190 {
191   UINT8 bMaxLun;
192   UINT8 bCMD[8];
193   //UINT8 HOST20_MSCD_GET_MAX_LUN[8]     = {0xA1,0xFE,0x00,0x00,0x00,0x00,0x01,0x00}; //Get MAX LUN
194 
195   //Setup=0xA1,0xFE,0x00,0x00,0x00,0x00,0x01,0x00
196   //In =1 bytes
197   //Ack
198   //printf("data buffer:%x  %lx\n",psAttachDevice_Port3->bDataBuffer,USB_BUFFER_START_ADR_4K_ALIGN);
199   memcpy(bCMD,HOST20_MSCD_GET_MAX_LUN_PORT3,8);
200 
201   if (flib_Host20_Issue_Control_Port3 (1,bCMD,0x01,&bMaxLun)>0)
202   {
203 
204     DRV_MASS_STORAGE_DBG(printf(">>>Get Max Lun Fail ...\n"));
205     return (0);
206   }
207   else
208   {
209     //printf("Get Max Lun succeed\n");
210     return (bMaxLun);
211 
212   }
213 
214 
215 
216 }
217 
218 #endif
219 
220 extern void Dump_Data_Port3(UINT16 addr,UINT16 sz);
221 
SwapWord_Port3(UINT32 dat1)222 UINT32 SwapWord_Port3(UINT32 dat1)
223 {
224   U8  t1,t2,t3,t4;
225   U32 temp;
226 
227   t1=(U8)(dat1 >> 24) & 0xff;
228   t2=(U8)(dat1 >> 16) & 0xff;
229   t3=(U8)(dat1 >> 8) & 0xff;
230   t4=(U8)(dat1) & 0xff;
231 
232   temp=( (U32) t4 <<24) | ((U32) t3 << 16) |  ((U32) t2 << 8) | t1  ;
233   return temp;
234 }
235 //***************************************************************************************
236 // Function Name:Host20_MSCD_issue_CBW_Port3
237 // Description:
238 // Input:
239 // Output:
240 // Status:P-OK
241 //***************************************************************************************
Host20_MSCD_issue_CBW_Port3(struct us_data * psMassStorage)242 UINT8 Host20_MSCD_issue_CBW_Port3(struct us_data *psMassStorage)
243 {
244   //Host20_BufferPointerArray_Structure aTemp;
245   U8 result;
246   U32 buf;
247 
248   //   printf("issue CBW...\n");
249   //<1>.Prepare the CBW data
250   pOTGH_PT_BLK_Port3->sCBW.u32Signature = OTGH_PT_CBW_SIGNATE;
251   pOTGH_PT_BLK_Port3->sCBW.u32DataTransferLength=psMassStorage->srb->request_bufflen;
252   pOTGH_PT_BLK_Port3->sCBW.u32Tag=gwTagPort3;
253   pOTGH_PT_BLK_Port3->sCBW.u8LUN=psMassStorage->srb->lun;
254 
255   switch (psMassStorage->srb->sc_data_direction)
256   {
257     case SCSI_DATA_WRITE:
258          //printf("data wr\n");
259          pOTGH_PT_BLK_Port3->sCBW.u8Flags=0x00;
260          break;
261     case SCSI_DATA_READ:
262          //printf("data rd\n");
263 
264          pOTGH_PT_BLK_Port3->sCBW.u8Flags=0x80;  //john 0x01
265          break;
266     //case SCSI_DATA_UNKNOWN:
267     //case SCSI_DATA_NONE:
268     //DRV_MASS_STORAGE_DBG(printf(">>> Unknow 'psMassStorage->srb->sc_data_direction'... \n"));
269     //while(1);
270     //break;
271   }
272 
273   pOTGH_PT_BLK_Port3->sCBW.u8LUN=psMassStorage->srb->lun;//Only support 1 LUN
274   pOTGH_PT_BLK_Port3->sCBW.u8CBLength=psMassStorage->srb->cmd_len;//Command Length
275   //memcpy((U8 xdata *)&(pOTGH_PT_BLK_Port3->sCBW.u8CB[0]),(U8 xdata *)&(psMassStorage->srb->cmnd[0]),16);
276 
277   //printf("signature:%lx\n",(pOTGH_PT_BLK_Port3->sCBW.u32Signature));
278   //printf("scbw:%x\n",(U16)&(pOTGH_PT_BLK_Port3->sCBW.u32Signature));
279   //Dump_Data((U16)&(pOTGH_PT_BLK_Port3->sCBW.u32Signature),31);
280   //buf=(U8 xdata *)psAttachDevice_Port3->bDataBuffer;
281   //memcpy(buf,(U8 xdata *)&(pOTGH_PT_BLK_Port3->sCBW.u32Signature),31);
282   buf=(U32)&(pOTGH_PT_BLK_Port3->sCBW.u32Signature);
283   result=flib_Host20_Issue_Bulk_Port3 (pOTGH_PT_BLK_Port3->bOutQHDArrayNum,31,buf,OTGH_Dir_Out);
284   return(result);
285 }
286 
287 //***************************************************************************************
288 // Function Name:Host20_MSCD_issue_CSW_Port3
289 // Description:
290 // Input:
291 // Output:
292 // Status:P-OK
293 //***************************************************************************************
Host20_MSCD_issue_CSW_Port3(void)294 UINT8 Host20_MSCD_issue_CSW_Port3(void)
295 {
296   UINT32 wCBWTag,wCSWTag;
297   //Host20_BufferPointerArray_Structure aTemp;
298   U32 buf;
299   U8 result,retry=0;
300 
301   //printf("Get CSW");
302   // buf=(U8 xdata *)psAttachDevice_Port3->bDataBuffer;
303     buf=(U32)&(pOTGH_PT_BLK_Port3->sCSW.u32Signature);
304 
305   redo2:
306        result=flib_Host20_Issue_Bulk_Port3 (pOTGH_PT_BLK_Port3->bInQHDArrayNum,13,buf,OTGH_Dir_IN);
307        if (result==HOST20_TRANSACTION_ERROR)
308        {
309 	     retry++;
310 		 if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
311 		   return HOST20_FATAL;
312 
313 	     #ifdef Enable_Low_Temperature_Patch
314 	     if(retry<=ErrorRetryCount1)
315          {
316 	       if(gSpeed_Port3==0)
317 	       {
318 	         //printf("\r\n CSW toggle!!",0);
319                 UTMI3_SETXBYTE(0x0a, UTMI3_READXBYTE(0x0a)^0x10); //invert CDR_CLOCK
320                 UTMI3_ORXBYTE(0x06,0x03);    //reset UTMI
321                 UTMI3_ANDXBYTE(0x06,0xfc);
322 	       }
323          }
324 	     #endif
325          //printf("csw trans err retry\n");
326          goto redo2;
327        }
328 
329    // printf("result:%02bx\n",result);
330    if (result==HOST20_FATAL) return HOST20_MSCD_DEVICE_FATAL_ERROR;
331    else if (result!=0)
332    {  //john    if (bTemp==HOST20_FAIL) {
333      return(HOST20_MSCD_CSW_Status_STALL);
334    }
335    //memcpy((U8 xdata *)&(pOTGH_PT_BLK_Port3->sCSW.u32Signature),(U8 xdata *)psAttachDevice_Port3->bDataBuffer,13);
336 
337    //<2>.Check the data-Tag
338    wCBWTag=(UINT32)(pOTGH_PT_BLK_Port3->sCBW.u32Tag);
339    wCSWTag=(UINT32)(pOTGH_PT_BLK_Port3->sCSW.u32Tag);
340    if (wCBWTag!=wCSWTag)
341    {
342      DRV_MASS_STORAGE_DBG(printf(">>> Error: Received CSW->Tag fail (Expected data:0x%x / Received Data:0x%x)\n",(UINT32)(wCBWTag),(UINT32)(wCSWTag)));
343      return(HOST20_MSCD_CSW_Status_NOT_VALID);
344    }
345 
346    //<3>.Check the data-Signature
347    if ((pOTGH_PT_BLK_Port3->sCSW.u32Signature)!=OTGH_PT_CSW_SIGNATE)
348    {
349      DRV_MASS_STORAGE_DBG(printf(">>> Error: Received CSW->Signature fail (Expected data:%d / Received Data:%d)\n",OTGH_PT_CSW_SIGNATE,(UINT32)(pOTGH_PT_BLK_Port3->sCSW.u32Tag)));
350      return(HOST20_MSCD_CSW_Status_NOT_VALID);
351    }
352 
353    //<4>.Checking Status OTGH_PT_CSW_PASS
354    if ((pOTGH_PT_BLK_Port3->sCSW.u8Status)!=HOST20_MSCD_CSW_Status_PASS)
355    {//printf(">>> Error: Received CSW->Status fail 'HOST20_MSCD_CSW_Status_FAIL'\n");
356      return(pOTGH_PT_BLK_Port3->sCSW.u8Status);
357    }
358    return(HOST20_MSCD_CSW_Status_PASS);
359 }
360 extern U16 TotalBytes_Port3;
361 
362 //***************************************************************************************
363 // Function Name: Host20_MSCD_issue_Data_Port3
364 // Description:
365 // Input:
366 // Output:
367 // Status:P-OK
368 //***************************************************************************************
Host20_MSCD_issue_Data_Port3(struct us_data * psMassStorage)369 UINT8 Host20_MSCD_issue_Data_Port3(struct us_data *psMassStorage)
370 {
371 
372   // UINT8 *pbDataPage[5];
373   UINT32 wTotalLengthRemain,Offset;
374   U32 buf;
375   U16 TransferLengh;
376   U8 result=HOST20_FAIL,retry;
377 
378   //printf("data phase\n");
379 
380 
381   //<1>.To fill the data buffer
382 
383   wTotalLengthRemain=psMassStorage->srb->request_bufflen;
384 
385     buf=psMassStorage->srb->request_buffer;
386 
387   //<2>.Issue Transfer
388 
389     switch (psMassStorage->srb->sc_data_direction)
390     {
391       case SCSI_DATA_WRITE://Out
392 	  Offset=0;
393 	  while(wTotalLengthRemain>0)
394 	  {
395 	    if(wTotalLengthRemain > Scsi_Max_Transfer_Len)
396 	    {
397 	      TransferLengh = Scsi_Max_Transfer_Len;
398 	    }
399 	    else
400 	    {
401 		  TransferLengh=wTotalLengthRemain;
402 	    }
403 
404 		result=flib_Host20_Issue_Bulk_Port3 (pOTGH_PT_BLK_Port3->bOutQHDArrayNum,TransferLengh
405 	                         ,buf+Offset,OTGH_Dir_Out);
406 		retry=0;
407         while (result==HOST20_TRANSACTION_ERROR)
408         {
409 		  retry++;
410 	      RecoveryFlag_Port3|=0x01;
411 	      if(pOTGH_PT_BLK_Port3->bOutQHDArrayNum)
412 	        pHost20_qHD_List_Bulk1_Port3->bDataToggleControl=1;
413 		  else
414 		    pHost20_qHD_List_Bulk0_Port3->bDataToggleControl=1;
415 
416 		  DRV_MASS_STORAGE_DBG(printf("\r\n retry OUT\n",0));
417 
418 		  if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
419 	      {
420 	        if(pOTGH_PT_BLK_Port3->bOutQHDArrayNum)
421 		      pHost20_qHD_List_Bulk1_Port3->bDataToggleControl=0;
422 	        else
423 		      pHost20_qHD_List_Bulk0_Port3->bDataToggleControl=0;
424 	        RecoveryFlag_Port3&=0xFE;
425 	        return HOST20_FATAL;
426 	      }
427 
428 		  result=flib_Host20_Issue_Bulk_Port3 (pOTGH_PT_BLK_Port3->bOutQHDArrayNum, TotalBytes_Port3,
429                      buf+Offset+(TransferLengh-TotalBytes_Port3),OTGH_Dir_Out);
430 
431 	      if(pOTGH_PT_BLK_Port3->bOutQHDArrayNum)
432 	        pHost20_qHD_List_Bulk1_Port3->bDataToggleControl=0;
433 		  else
434 		    pHost20_qHD_List_Bulk0_Port3->bDataToggleControl=0;
435 	      RecoveryFlag_Port3&=0xFE;
436 
437         }
438 		Offset+=TransferLengh;
439         wTotalLengthRemain-=TransferLengh;
440 	  }
441       return result;
442 
443       break;
444 
445       case SCSI_DATA_READ://In
446       //<2>.Issue the Loop2-InData & Compare
447       //	if ((psMassStorage->srb->phy_buf_adr & 0xfff)==0)
448       //      buf=(UINT8*)(psMassStorage->srb->phy_buf_adr);  //use original buffer
449 
450 	  Offset=0;
451       while(wTotalLengthRemain>0)
452 	  {
453 	    if(wTotalLengthRemain > Scsi_Max_Transfer_Len)
454 	    {
455 	      TransferLengh = Scsi_Max_Transfer_Len;
456 	    }
457 	    else
458 	    {
459 
460 		  TransferLengh=wTotalLengthRemain;
461 	    }
462 
463 		result=flib_Host20_Issue_Bulk_Port3 (pOTGH_PT_BLK_Port3->bInQHDArrayNum, TransferLengh
464 	                         ,buf+Offset,OTGH_Dir_IN);
465 
466         if (result==HOST20_TRANSACTION_ERROR)
467         {
468 		  retry=0;
469           XREDO:
470 		       retry++;
471                //printf("data in transerror retry\n");
472 			   if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
473 			     return HOST20_FATAL;
474 			   #ifdef Enable_Low_Temperature_Patch
475 	           if(retry<=ErrorRetryCount1)
476                {
477 	             if(gSpeed_Port3==0)
478 	             {
479 	               //printf("\r\n IN toggle!!",0);
480                         UTMI3_SETXBYTE(0x0a,UTMI3_READXBYTE(0x0a)^0x10);  //invert CDR_CLOCK
481                         UTMI3_ORXBYTE(0x06,0x03);    //reset UTMI
482                         UTMI3_ANDXBYTE(0x06,0xfc);
483 	             }
484                }
485 	           #endif
486                result=flib_Host20_Issue_Bulk_Port3 (pOTGH_PT_BLK_Port3->bInQHDArrayNum, TotalBytes_Port3,
487                    buf+Offset+(TransferLengh-TotalBytes_Port3),OTGH_Dir_IN);
488                if (result==HOST20_TRANSACTION_ERROR)
489                  goto XREDO;
490         }
491         else if (result==HOST20_FATAL)   return result;               //timeout
492         Offset+=TransferLengh;
493         wTotalLengthRemain-=TransferLengh;
494       }
495 	  //for (i=0 ; i < 16 ; i++)
496 	  //	printf("%02bx ", buf[i]);
497 	  //printf("\n");
498       //printf("\ndump data\n");
499       //Dump_Data((U16)psMassStorage->srb->request_buffer,psMassStorage->srb->request_bufflen);
500       //printf("\ndump buf\n");
501       //Dump_Data((U16)buf,36);
502 
503       return result;
504 
505       break;
506 
507 
508 
509     }
510 
511     return(result);
512 
513 }
514 
515 #ifdef  ENABLE_CBI_HOST
516 code UINT8 CBI_SET_COMMAND_PORT3[]    = {0x21,0x00,0x00,0x00,0x00,0x00,0x0c,0x00};
517 
Host20_MSDCD_usb_stor_CBI_protocol_Port3(struct us_data * psMassStorage)518 BOOLEAN Host20_MSDCD_usb_stor_CBI_protocol_Port3(struct us_data *psMassStorage)
519 {
520     UINT8 bCMD[8];
521     UINT8 bValue,result;
522     //printf("CBI protocol\n");
523     memcpy(bCMD,CBI_SET_COMMAND_PORT3,8);
524 
525     result=flib_Host20_Issue_Control_CBI_Port3(bCMD,12,(UINT8 *)&(pOTGH_PT_BLK_Port3->sCBW.u8CB[0]));
526     if (result== HOST20_DEVICE_STALL)
527     {
528         //  printf("stall result\n");
529         psMassStorage->srb->result   = SAM_STAT_CHECK_CONDITION;
530         return TRUE;           //that is a kind of success, CB doesn't have status phase,STALL mean CSW=1
531     }
532 
533     if (psMassStorage->srb->request_bufflen>0)
534     {
535         bValue=Host20_MSCD_issue_Data_Port3(psMassStorage);
536         if (bValue==HOST20_DEVICE_STALL)
537         {
538             DRV_MASS_STORAGE_MSG(printf("data phase stall\n"));
539             Host20_MSCD_ClearFeature_Port3(psMassStorage);
540         }
541         else if (bValue==HOST20_FATAL)
542         {
543             DRV_MASS_STORAGE_MSG(printf("data phase fatal\n"));
544             return FALSE;
545         }
546     }
547 
548     return TRUE;
549 }
550 #endif
551 
552 //***************************************************************************************
553 // Function Name: Host20_MSCD_usb_stor_control_thread_Port3
554 // Description:
555 //             <1>.Issue CBW
556 //             <2>.Issue Data Stage
557 //             <3>.Issue CSW
558 //             <4>.Fill the result & return
559 // Input:
560 // Output:
561 // Status:S
562 //***************************************************************************************
Host20_MSCD_usb_stor_control_thread_Port3(struct us_data * psMassStorage)563 BOOLEAN Host20_MSCD_usb_stor_control_thread_Port3(struct us_data *psMassStorage)
564 {
565   UINT8 bValue,retry;
566   UINT8 result;
567   //<1>.CBW issue
568 #ifdef ENABLE_CBI_HOST
569   if ((psAttachDevice_Port3->saCD[0].sInterface[0].bInterfaceProtocol==0x00)||(psAttachDevice_Port3->saCD[0].sInterface[0].bInterfaceProtocol==0x01))
570   {
571       return Host20_MSDCD_usb_stor_CBI_protocol_Port3(psMassStorage);
572   }
573 #endif
574   result=Host20_MSCD_issue_CBW_Port3(psMassStorage);
575   if (result==HOST20_FATAL) return FALSE;
576   else if (result==HOST20_TRANSACTION_ERROR)
577   {
578     DRV_MASS_STORAGE_DBG( printf("\r\n ??? 'Host20_MSCD_issue_CBW_Port3' Fail...(%bx)",pOTGH_PT_BLK_Port3->sCBW.u8Flags));
579     #ifdef Enable_UHCI_Recovery
580     DRV_MASS_STORAGE_DBG(printf("\r\n retry cbw",0));
581     RecoveryFlag_Port3|=0x01;
582     if(pOTGH_PT_BLK_Port3->bOutQHDArrayNum)
583 	  pHost20_qHD_List_Bulk1_Port3->bDataToggleControl=1;
584 	else
585 	  pHost20_qHD_List_Bulk0_Port3->bDataToggleControl=1;
586 	retry=0;
587     while(1)
588     {
589 	  retry++;
590 
591       if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
592 	  {
593 	    if(pOTGH_PT_BLK_Port3->bOutQHDArrayNum)
594 		  pHost20_qHD_List_Bulk1_Port3->bDataToggleControl=0;
595 	    else
596 		  pHost20_qHD_List_Bulk0_Port3->bDataToggleControl=0;
597 	    RecoveryFlag_Port3&=0xFE;
598 	    return FALSE;
599 	  }
600 
601 	  result=Host20_MSCD_issue_CBW_Port3(psMassStorage);
602 
603       if (result==HOST20_OK) break;
604       else if (result==HOST20_FATAL) return FALSE;
605     }
606     if(pOTGH_PT_BLK_Port3->bOutQHDArrayNum)
607 	  pHost20_qHD_List_Bulk1_Port3->bDataToggleControl=0;
608 	else
609 	  pHost20_qHD_List_Bulk0_Port3->bDataToggleControl=0;
610 	RecoveryFlag_Port3&=0xFE;
611     #else
612     return FALSE;
613     #endif
614   }
615 
616   //<2>.Data issue
617   retry=0;
618   if (psMassStorage->srb->request_bufflen>0)
619   {
620     bValue=Host20_MSCD_issue_Data_Port3(psMassStorage);
621     if (bValue==HOST20_DEVICE_STALL)
622       Host20_MSCD_ClearFeature_Port3(psMassStorage);
623     else if (bValue==HOST20_FATAL)   return FALSE;
624 
625   }
626   //<3>.CSW issue
627   //printf("Get CSW");
628   //printf("bULK 0:\n");
629   // Dump_Data(pHost20_qHD_List_Bulk0_Port3,0x30);
630 
631   bValue=Host20_MSCD_issue_CSW_Port3();
632   if (bValue==HOST20_MSCD_DEVICE_FATAL_ERROR) return FALSE;
633   if(bValue>0)
634   {
635     //printf("??? 'Host20_MSCD_issue_CSW_Port3' Fail...\n");
636     //For Stall=>try again
637     psMassStorage->srb->result = SAM_STAT_CHECK_CONDITION;        //problem happened
638     if (bValue==HOST20_MSCD_CSW_Status_STALL)
639     {
640       //printf(">>> Device Stall the Command, CSW issue again.\n");
641       //<3.1>.Clear Feature
642       MsOS_DelayTask(1);
643       Host20_MSCD_ClearFeature_Port3(psMassStorage);
644       //<3.2>.Read CSW again
645       MsOS_DelayTask(1);
646       bValue=Host20_MSCD_issue_CSW_Port3();
647       if(bValue>0)
648         psMassStorage->srb->result = SAM_STAT_CHECK_CONDITION;        //problem happened
649       else
650         psMassStorage->srb->result = SAM_STAT_GOOD;        //problem happened
651 
652 
653     }
654 
655 
656   }
657   else
658     psMassStorage->srb->result = SAM_STAT_GOOD;
659 
660   gwTagPort3++;
661 
662   return TRUE;
663 }
664 #endif //#if defined(MSOS_TYPE_NOS)
665 
666