xref: /utopia/UTPA2-700.0.x/modules/usb/drv/usbhost/source/usb_host_p1/drvMassStorage.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.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.h"
107 #include "../drvHost200.h"
108 #include "../drvscsi.h"
109 #include "../drvMassStorage.h"
110 #ifdef ATV_SERISE_USE
111 #include "uart.h"
112 #else
113 //#include "drvmiu.h"
114 #endif
115 #include "../drvUsbMain.h"
116 
117 #include "MsCommon.h"
118 #include "MsIRQ.h"
119 #include "MsOS.h"
120 
121 #define DRV_MASS_STORAGE_DBG(x)  //x;
122 #define DRV_MASS_STORAGE_MSG(x)  MS_CRITICAL_MSG(x)
123 
124 #define ErrorRetryCount1 3
125 
126 UINT32 gwTag=0x88888888;
127 extern U8 gSpeed;
128 
129 extern void Dump_Data(UINT16 addr,UINT16 sz);
130 #define Enable_UHCI_Recovery
131 U8 RecoveryFlag=0;
132 
133 
134 //===============================================================================================
135 //===============================================================================================
136 //===============================================================================================
137 //============================= Group-1: Mass Storage Class Implement ===========================
138 //===============================================================================================
139 //===============================================================================================
140 //===============================================================================================
141 //***************************************************************************************
142 // Function Name: Host20_MSCD_ClearFeature
143 // Description:
144 // Input:
145 // Output:
146 // Status:S
147 //***************************************************************************************
148 code  UINT8 HOST20_CLEAR_FEATURE[8]     = {0x02,0x01,0x00,0x00,0x81,0x00,0x00,0x00}; //
149 
Host20_MSCD_ClearFeature(struct us_data * psMassStorage)150 UINT8 Host20_MSCD_ClearFeature(struct us_data *psMassStorage)
151 {
152   UINT8 bTemp;
153   UINT8 bCMD[8];
154 
155 
156   memcpy(bCMD,HOST20_CLEAR_FEATURE,8);
157   //bCMD[4]=bEdNum;
158   if (psMassStorage->srb->sc_data_direction==SCSI_DATA_WRITE)
159     bCMD[4]=sAttachDevice.saCD[0].sInterface[0].sED[ OTGH_PT_BLK.bOutQHDArrayNum].bED_EndpointAddress;
160   else
161     bCMD[4]=sAttachDevice.saCD[0].sInterface[0].sED[ OTGH_PT_BLK.bInQHDArrayNum].bED_EndpointAddress;
162 
163   //if (OTGH_PT_BLK.bInQHDArrayNum==0) bCMD[4]=0x81;
164   //else bCMD[4]=0x82;
165   //printf("clear stall:%02bx\n",bCMD[4]);
166 
167   if (flib_Host20_Issue_Control (1,bCMD,0x08,&bTemp)>0)
168   {
169 
170     //printf(">>> Host20_MSCD_ClearFeature Fail ...\r\n");
171     return (0);
172   }
173   else
174   {
175     //printf("Clear stall succeed\n");
176     return (1);
177   }
178 }
179 
180 #if 1
181 code   UINT8 HOST20_MSCD_GET_MAX_LUN[8]     = {0xA1,0xFE,0x00,0x00,0x00,0x00,0x01,0x00}; //Get MAX LUN
182 
183 //***************************************************************************************
184 // Function Name: Host20_MSCD_GetMaxLUN
185 // Description:
186 // Input:
187 // Output:
188 // Status:S
189 //***************************************************************************************
Host20_MSCD_GetMaxLUN(void)190 UINT8 Host20_MSCD_GetMaxLUN(void)
191 {
192   UINT8 bMaxLun;
193   UINT8 bCMD[8];
194   //UINT8 HOST20_MSCD_GET_MAX_LUN[8]     = {0xA1,0xFE,0x00,0x00,0x00,0x00,0x01,0x00}; //Get MAX LUN
195 
196   //Setup=0xA1,0xFE,0x00,0x00,0x00,0x00,0x01,0x00
197   //In =1 bytes
198   //Ack
199   //printf("data buffer:%x  %lx\n",sAttachDevice.bDataBuffer,USB_BUFFER_START_ADR_4K_ALIGN);
200   memcpy(bCMD,HOST20_MSCD_GET_MAX_LUN,8);
201 
202   if (flib_Host20_Issue_Control (1,bCMD,0x01,&bMaxLun)>0)
203   {
204 
205     DRV_MASS_STORAGE_DBG(printf(">>>Get Max Lun Fail ...\n"));
206     return (0);
207   }
208   else
209   {
210     //printf("Get Max Lun succeed\n");
211     return (bMaxLun);
212 
213   }
214 
215 
216 
217 }
218 
219 #endif
220 
221 extern void Dump_Data(UINT16 addr,UINT16 sz);
222 
SwapWord(UINT32 dat1)223 UINT32 SwapWord(UINT32 dat1)
224 {
225   U8  t1,t2,t3,t4;
226   U32 temp;
227 
228   t1=(U8)(dat1 >> 24) & 0xff;
229   t2=(U8)(dat1 >> 16) & 0xff;
230   t3=(U8)(dat1 >> 8) & 0xff;
231   t4=(U8)(dat1) & 0xff;
232 
233   temp=( (U32) t4 <<24) | ((U32) t3 << 16) |  ((U32) t2 << 8) | t1  ;
234   return temp;
235 }
236 //***************************************************************************************
237 // Function Name:Host20_MSCD_issue_CBW
238 // Description:
239 // Input:
240 // Output:
241 // Status:P-OK
242 //***************************************************************************************
Host20_MSCD_issue_CBW(struct us_data * psMassStorage)243 UINT8 Host20_MSCD_issue_CBW(struct us_data *psMassStorage)
244 {
245   //Host20_BufferPointerArray_Structure aTemp;
246   U8 result;
247   U32 buf;
248 
249   //   printf("issue CBW...\n");
250   //<1>.Prepare the CBW data
251   OTGH_PT_BLK.sCBW.u32Signature = OTGH_PT_CBW_SIGNATE;
252   OTGH_PT_BLK.sCBW.u32DataTransferLength=psMassStorage->srb->request_bufflen;
253   OTGH_PT_BLK.sCBW.u32Tag=gwTag;
254   OTGH_PT_BLK.sCBW.u8LUN=psMassStorage->srb->lun;
255 
256   switch (psMassStorage->srb->sc_data_direction)
257   {
258     case SCSI_DATA_WRITE:
259          //printf("data wr\n");
260          OTGH_PT_BLK.sCBW.u8Flags=0x00;
261          break;
262     case SCSI_DATA_READ:
263          //printf("data rd\n");
264 
265          OTGH_PT_BLK.sCBW.u8Flags=0x80;  //john 0x01
266          break;
267     //case SCSI_DATA_UNKNOWN:
268     //case SCSI_DATA_NONE:
269     //DRV_MASS_STORAGE_DBG(printf(">>> Unknow 'psMassStorage->srb->sc_data_direction'... \n"));
270     //while(1);
271     //break;
272   }
273 
274   OTGH_PT_BLK.sCBW.u8LUN=psMassStorage->srb->lun;//Only support 1 LUN
275   OTGH_PT_BLK.sCBW.u8CBLength=psMassStorage->srb->cmd_len;//Command Length
276   //memcpy((U8 xdata *)&(OTGH_PT_BLK.sCBW.u8CB[0]),(U8 xdata *)&(psMassStorage->srb->cmnd[0]),16);
277 
278   //printf("signature:%lx\n",(OTGH_PT_BLK.sCBW.u32Signature));
279   //printf("scbw:%x\n",(U16)&(OTGH_PT_BLK.sCBW.u32Signature));
280   //Dump_Data((U16)&(OTGH_PT_BLK.sCBW.u32Signature),31);
281   //buf=(U8 xdata *)sAttachDevice.bDataBuffer;
282   //memcpy(buf,(U8 xdata *)&(OTGH_PT_BLK.sCBW.u32Signature),31);
283       buf=((U32)&(OTGH_PT_BLK.sCBW.u32Signature));
284 
285   result=flib_Host20_Issue_Bulk (OTGH_PT_BLK.bOutQHDArrayNum,31,buf,OTGH_Dir_Out);
286   return(result);
287 }
288 
289 //***************************************************************************************
290 // Function Name:Host20_MSCD_issue_CSW
291 // Description:
292 // Input:
293 // Output:
294 // Status:P-OK
295 //***************************************************************************************
Host20_MSCD_issue_CSW(void)296 UINT8 Host20_MSCD_issue_CSW(void)
297 {
298   UINT32 wCBWTag,wCSWTag;
299   //Host20_BufferPointerArray_Structure aTemp;
300   U32 buf;
301   U8 result,retry=0;
302 
303   //printf("Get CSW");
304   // buf=(U8 xdata *)sAttachDevice.bDataBuffer;
305     buf=(U32)&(OTGH_PT_BLK.sCSW.u32Signature);
306 
307   redo2:
308        result=flib_Host20_Issue_Bulk (OTGH_PT_BLK.bInQHDArrayNum,13,buf,OTGH_Dir_IN);
309        if (result==HOST20_TRANSACTION_ERROR)
310        {
311 	     retry++;
312 		 if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
313 		   return HOST20_FATAL;
314 
315 	     #ifdef Enable_Low_Temperature_Patch
316 	     if(retry<=ErrorRetryCount1)
317          {
318 	       if(gSpeed==0)
319 	       {
320 	         //printf("\r\n CSW toggle!!",0);
321                 UTMI_SETXBYTE(0x0a, UTMI_READXBYTE(0x0a)^0x10); //invert CDR_CLOCK
322                 UTMI_ORXBYTE(0x06,0x03);    //reset UTMI
323                 UTMI_ANDXBYTE(0x06,0xfc);
324 
325 	       }
326          }
327 	     #endif
328          //printf("csw trans err retry\n");
329          goto redo2;
330        }
331 
332    // printf("result:%02bx\n",result);
333    if (result==HOST20_FATAL) return HOST20_MSCD_DEVICE_FATAL_ERROR;
334    else if (result!=0)
335    {  //john    if (bTemp==HOST20_FAIL) {
336      return(HOST20_MSCD_CSW_Status_STALL);
337    }
338  //  memcpy((U8 xdata *)&(OTGH_PT_BLK.sCSW.u32Signature),(U8 xdata *)sAttachDevice.bDataBuffer,13);
339 
340    //<2>.Check the data-Tag
341    wCBWTag=(UINT32)(OTGH_PT_BLK.sCBW.u32Tag);
342    wCSWTag=(UINT32)(OTGH_PT_BLK.sCSW.u32Tag);
343    if (wCBWTag!=wCSWTag)
344    {
345      DRV_MASS_STORAGE_DBG(printf(">>> Error: Received CSW->Tag fail (Expected data:0x%x / Received Data:0x%x)\n",(UINT32)(wCBWTag),(UINT32)(wCSWTag)));
346      return(HOST20_MSCD_CSW_Status_NOT_VALID);
347    }
348 
349    //<3>.Check the data-Signature
350    if ((OTGH_PT_BLK.sCSW.u32Signature)!=OTGH_PT_CSW_SIGNATE)
351    {
352      DRV_MASS_STORAGE_DBG(printf(">>> Error: Received CSW->Signature fail (Expected data:%d / Received Data:%d)\n",OTGH_PT_CSW_SIGNATE,(UINT32)(OTGH_PT_BLK.sCSW.u32Tag)));
353      return(HOST20_MSCD_CSW_Status_NOT_VALID);
354    }
355 
356    //<4>.Checking Status OTGH_PT_CSW_PASS
357    if ((OTGH_PT_BLK.sCSW.u8Status)!=HOST20_MSCD_CSW_Status_PASS)
358    {//printf(">>> Error: Received CSW->Status fail 'HOST20_MSCD_CSW_Status_FAIL'\n");
359      return(OTGH_PT_BLK.sCSW.u8Status);
360    }
361    return(HOST20_MSCD_CSW_Status_PASS);
362 }
363 extern U16 TotalBytes;
364 
365 //***************************************************************************************
366 // Function Name: Host20_MSCD_issue_Data
367 // Description:
368 // Input:
369 // Output:
370 // Status:P-OK
371 //***************************************************************************************
Host20_MSCD_issue_Data(struct us_data * psMassStorage)372 UINT8 Host20_MSCD_issue_Data(struct us_data *psMassStorage)
373 {
374 
375   // UINT8 *pbDataPage[5];
376   UINT32 wTotalLengthRemain,Offset;
377   U32 buf;
378   U16 TransferLengh;
379   U8 result=HOST20_FAIL,retry;
380 
381   //printf("data phase\n");
382 
383 
384   //<1>.To fill the data buffer
385 
386   wTotalLengthRemain=psMassStorage->srb->request_bufflen;
387 
388 
389 
390     buf=psMassStorage->srb->request_buffer;
391 
392 
393   //<2>.Issue Transfer
394 
395     switch (psMassStorage->srb->sc_data_direction)
396     {
397       case SCSI_DATA_WRITE://Out
398 	  Offset=0;
399 	  while(wTotalLengthRemain>0)
400 	  {
401 	    if(wTotalLengthRemain > Scsi_Max_Transfer_Len)
402 	    {
403 	      TransferLengh = Scsi_Max_Transfer_Len;
404 	    }
405 	    else
406 	    {
407 		  TransferLengh=wTotalLengthRemain;
408 	    }
409 
410 		result=flib_Host20_Issue_Bulk (OTGH_PT_BLK.bOutQHDArrayNum,TransferLengh
411 	                         ,buf+Offset,OTGH_Dir_Out);
412 		retry=0;
413         while (result==HOST20_TRANSACTION_ERROR)
414         {
415 		  retry++;
416 	      RecoveryFlag|=0x01;
417 	      if(OTGH_PT_BLK.bOutQHDArrayNum)
418 	        pHost20_qHD_List_Bulk1->bDataToggleControl=1;
419 		  else
420 		    pHost20_qHD_List_Bulk0->bDataToggleControl=1;
421 
422 		  DRV_MASS_STORAGE_DBG(printf("\r\n retry OUT\n",0));
423 
424 		  if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
425 	      {
426 	        if(OTGH_PT_BLK.bOutQHDArrayNum)
427 		      pHost20_qHD_List_Bulk1->bDataToggleControl=0;
428 	        else
429 		      pHost20_qHD_List_Bulk0->bDataToggleControl=0;
430 	        RecoveryFlag&=0xFE;
431 	        return HOST20_FATAL;
432 	      }
433 
434 		  result=flib_Host20_Issue_Bulk (OTGH_PT_BLK.bOutQHDArrayNum, TotalBytes,
435                      buf+Offset+(TransferLengh-TotalBytes),OTGH_Dir_Out);
436 
437 	      if(OTGH_PT_BLK.bOutQHDArrayNum)
438 	        pHost20_qHD_List_Bulk1->bDataToggleControl=0;
439 		  else
440 		    pHost20_qHD_List_Bulk0->bDataToggleControl=0;
441 	      RecoveryFlag&=0xFE;
442 
443         }
444 		Offset+=TransferLengh;
445         wTotalLengthRemain-=TransferLengh;
446 	  }
447       return result;
448 
449       break;
450 
451       case SCSI_DATA_READ://In
452       //<2>.Issue the Loop2-InData & Compare
453       //	if ((psMassStorage->srb->phy_buf_adr & 0xfff)==0)
454       //      buf=(UINT8*)(psMassStorage->srb->phy_buf_adr);  //use original buffer
455 
456 	  Offset=0;
457       while(wTotalLengthRemain>0)
458 	  {
459 	    if(wTotalLengthRemain > Scsi_Max_Transfer_Len)
460 	    {
461 	      TransferLengh = Scsi_Max_Transfer_Len;
462 	    }
463 	    else
464 	    {
465 
466 		  TransferLengh=wTotalLengthRemain;
467 	    }
468 
469 		result=flib_Host20_Issue_Bulk (OTGH_PT_BLK.bInQHDArrayNum, TransferLengh
470 	                         ,buf+Offset,OTGH_Dir_IN);
471 
472         if (result==HOST20_TRANSACTION_ERROR)
473         {
474 		  retry=0;
475           XREDO:
476 		       retry++;
477                //printf("data in transerror retry\n");
478 			   if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
479 			     return HOST20_FATAL;
480 			   #ifdef Enable_Low_Temperature_Patch
481 	           if(retry<=ErrorRetryCount1)
482                {
483 	             if(gSpeed==0)
484 	             {
485 	               //printf("\r\n IN toggle!!",0);
486                         UTMI_SETXBYTE(0x0a,UTMI_READXBYTE(0x0a)^0x10);  //invert CDR_CLOCK
487                         UTMI_ORXBYTE(0x06,0x03);    //reset UTMI
488                         UTMI_ANDXBYTE(0x06,0xfc);
489 
490 	             }
491                }
492 	           #endif
493                result=flib_Host20_Issue_Bulk (OTGH_PT_BLK.bInQHDArrayNum, TotalBytes,
494                    buf+Offset+(TransferLengh-TotalBytes),OTGH_Dir_IN);
495                if (result==HOST20_TRANSACTION_ERROR)
496                  goto XREDO;
497         }
498         else if (result==HOST20_FATAL)   return result;               //timeout
499         Offset+=TransferLengh;
500         wTotalLengthRemain-=TransferLengh;
501       }
502 	  //for (i=0 ; i < 16 ; i++)
503 	  //	printf("%02bx ", buf[i]);
504 	  //printf("\n");
505       //printf("\ndump data\n");
506       //Dump_Data((U16)psMassStorage->srb->request_buffer,psMassStorage->srb->request_bufflen);
507       //printf("\ndump buf\n");
508       //Dump_Data((U16)buf,36);
509 
510       return result;
511 
512       break;
513 
514 
515 
516     }
517 
518     return(result);
519 
520 }
521 
522 #ifdef  ENABLE_CBI_HOST
523 code UINT8 CBI_SET_COMMAND[]    = {0x21,0x00,0x00,0x00,0x00,0x00,0x0c,0x00};
524 
Host20_MSDCD_usb_stor_CBI_protocol(struct us_data * psMassStorage)525 BOOLEAN Host20_MSDCD_usb_stor_CBI_protocol(struct us_data *psMassStorage)
526 {
527     UINT8 bCMD[8];
528     UINT8 bValue,result;
529     //printf("CBI protocol\n");
530     memcpy(bCMD,CBI_SET_COMMAND,8);
531 
532     result=flib_Host20_Issue_Control_CBI (bCMD,12,(UINT8 *)&(OTGH_PT_BLK.sCBW.u8CB[0]));
533     if (result== HOST20_DEVICE_STALL)
534     {
535         //  printf("stall result\n");
536         psMassStorage->srb->result   = SAM_STAT_CHECK_CONDITION;
537         return TRUE;           //that is a kind of success, CB doesn't have status phase,STALL mean CSW=1
538     }
539 
540     if (psMassStorage->srb->request_bufflen>0)
541     {
542         bValue=Host20_MSCD_issue_Data(psMassStorage);
543         if (bValue==HOST20_DEVICE_STALL)
544         {
545             DRV_MASS_STORAGE_DBG(printf("data phase stall\n"));
546             Host20_MSCD_ClearFeature(psMassStorage);
547         }
548         else if (bValue==HOST20_FATAL)
549         {
550             DRV_MASS_STORAGE_DBG(printf("data phase fatal\n"));
551             return FALSE;
552         }
553     }
554 
555     return TRUE;
556 }
557 #endif
558 
559 //***************************************************************************************
560 // Function Name: Host20_MSCD_usb_stor_control_thread
561 // Description:
562 //             <1>.Issue CBW
563 //             <2>.Issue Data Stage
564 //             <3>.Issue CSW
565 //             <4>.Fill the result & return
566 // Input:
567 // Output:
568 // Status:S
569 //***************************************************************************************
Host20_MSCD_usb_stor_control_thread(struct us_data * psMassStorage)570 BOOLEAN Host20_MSCD_usb_stor_control_thread(struct us_data *psMassStorage)
571 {
572   UINT8 bValue,retry;
573   UINT8 result;
574   //<1>.CBW issue
575 #ifdef ENABLE_CBI_HOST
576   if ((sAttachDevice.saCD[0].sInterface[0].bInterfaceProtocol==0x00)||(sAttachDevice.saCD[0].sInterface[0].bInterfaceProtocol==0x01))
577   {
578       return Host20_MSDCD_usb_stor_CBI_protocol(psMassStorage);
579   }
580 #endif
581   result=Host20_MSCD_issue_CBW(psMassStorage);
582   if (result==HOST20_FATAL) return FALSE;
583   else if (result==HOST20_TRANSACTION_ERROR)
584   {
585     DRV_MASS_STORAGE_DBG( printf("\r\n ??? 'Host20_MSCD_issue_CBW' Fail...(%bx)",OTGH_PT_BLK.sCBW.u8Flags));
586     #ifdef Enable_UHCI_Recovery
587     DRV_MASS_STORAGE_DBG(printf("\r\n retry cbw",0));
588     RecoveryFlag|=0x01;
589     if(OTGH_PT_BLK.bOutQHDArrayNum)
590 	  pHost20_qHD_List_Bulk1->bDataToggleControl=1;
591 	else
592 	  pHost20_qHD_List_Bulk0->bDataToggleControl=1;
593 	retry=0;
594     while(1)
595     {
596 	  retry++;
597 
598       if(mwHost20_PORTSC_ConnectStatus_Rd()==0 || (retry>ErrorRetryCount1))
599 	  {
600 	    if(OTGH_PT_BLK.bOutQHDArrayNum)
601 		  pHost20_qHD_List_Bulk1->bDataToggleControl=0;
602 	    else
603 		  pHost20_qHD_List_Bulk0->bDataToggleControl=0;
604 	    RecoveryFlag&=0xFE;
605 	    return FALSE;
606 	  }
607 
608 	  result=Host20_MSCD_issue_CBW(psMassStorage);
609 
610       if (result==HOST20_OK) break;
611       else if (result==HOST20_FATAL) return FALSE;
612     }
613     if(OTGH_PT_BLK.bOutQHDArrayNum)
614 	  pHost20_qHD_List_Bulk1->bDataToggleControl=0;
615 	else
616 	  pHost20_qHD_List_Bulk0->bDataToggleControl=0;
617 	RecoveryFlag&=0xFE;
618     #else
619     return FALSE;
620     #endif
621   }
622 
623   //<2>.Data issue
624   retry=0;
625   if (psMassStorage->srb->request_bufflen>0)
626   {
627     bValue=Host20_MSCD_issue_Data(psMassStorage);
628     if (bValue==HOST20_DEVICE_STALL)
629       Host20_MSCD_ClearFeature(psMassStorage);
630     else if (bValue==HOST20_FATAL)   return FALSE;
631 
632   }
633   //<3>.CSW issue
634   //printf("Get CSW");
635   //printf("bULK 0:\n");
636   // Dump_Data(&Host20_qHD_List_Bulk0,0x30);
637 
638   bValue=Host20_MSCD_issue_CSW();
639   if (bValue==HOST20_MSCD_DEVICE_FATAL_ERROR) return FALSE;
640   if(bValue>0)
641   {
642     //printf("??? 'Host20_MSCD_issue_CSW' Fail...\n");
643     //For Stall=>try again
644     psMassStorage->srb->result = SAM_STAT_CHECK_CONDITION;        //problem happened
645     if (bValue==HOST20_MSCD_CSW_Status_STALL)
646     {
647       //printf(">>> Device Stall the Command, CSW issue again.\n");
648       //<3.1>.Clear Feature
649       MsOS_DelayTask(1);
650       Host20_MSCD_ClearFeature(psMassStorage);
651       MsOS_DelayTask(1);
652       //<3.2>.Read CSW again
653       bValue=Host20_MSCD_issue_CSW();
654       if(bValue>0)
655         psMassStorage->srb->result = SAM_STAT_CHECK_CONDITION;        //problem happened
656       else
657         psMassStorage->srb->result = SAM_STAT_GOOD;        //problem happened
658 
659 
660     }
661 
662 
663   }
664   else
665     psMassStorage->srb->result = SAM_STAT_GOOD;
666 
667   gwTag++;
668 
669   return TRUE;
670 }
671 
672 #endif //#if defined(MSOS_TYPE_NOS)
673 
674 
675