omap3530/beagle_drivers/wb/drivers/cyasusbinit.cpp
changeset 27 117faf51deac
child 52 254b9435d75e
equal deleted inserted replaced
26:b7e488c49d0d 27:117faf51deac
       
     1 #include <kern_priv.h>
       
     2 #include <beagle/beagle_gpio.h>
       
     3 #include <beagle/variant.h>
       
     4 #include <assp/omap3530_assp/omap3530_assp_priv.h>
       
     5 #include <assp/omap3530_assp/omap3530_irqmap.h> // GPIO interrupts
       
     6 #include <assp/omap3530_assp/omap3530_gpio.h>
       
     7 
       
     8 #include <assp.h> // Required for definition of TIsr
       
     9 
       
    10 #include <cyastoria.h>
       
    11 #include <cyasmtp.h>
       
    12 #include <cyasusbinit.h>
       
    13 #include <cyasusbdescs.h>
       
    14 
       
    15 TmtpAstDev g_AstDevice;
       
    16 TmtpAstDev * g_pAstDevice = &g_AstDevice ;
       
    17 
       
    18 static uint16_t replybuf[512] ;
       
    19 static uint8_t *replyptr = (uint8_t *) replybuf ;
       
    20 
       
    21 static uint8_t *
       
    22 GetReplyArea(void)
       
    23 {
       
    24     /*assert(replyptr != 0) ;*/
       
    25     replyptr = 0 ;
       
    26     return (uint8_t *) replybuf ;
       
    27 }
       
    28 
       
    29 static void
       
    30 RestoreReplyArea(void)
       
    31 {
       
    32     replyptr = (uint8_t *) replybuf ;
       
    33 }
       
    34 
       
    35 /* Globals */
       
    36 /*static uint8_t pktbuffer3[512] ;*/
       
    37 
       
    38 #ifdef CY_AS_USB_TB_FOUR
       
    39 static uint8_t pktbuffer7[512] ;
       
    40 #endif
       
    41 
       
    42 #ifdef CY_AS_USB_TB_SIX
       
    43 static uint8_t pktbuffer11[512] ;
       
    44 #endif
       
    45 
       
    46 /*static uint8_t turbopktbuffer[512] ;*/
       
    47 
       
    48 static CyBool gUsbTestDone = CyFalse ;
       
    49 static volatile CyBool gSetConfig = CyFalse ;
       
    50 static volatile CyBool gAsyncStallDone = CyFalse ;
       
    51 
       
    52 static volatile CyBool gStorageReleaseBus0 = CyFalse ;
       
    53 static volatile CyBool gStorageReleaseBus1 = CyFalse ;
       
    54 
       
    55 static volatile CyAsHalDeviceTag g_tag ;
       
    56 
       
    57 static uint8_t MyConfiguration = 0 ;
       
    58 static CyCh9ConfigurationDesc *desc_p = 0 ;
       
    59 static CyCh9ConfigurationDesc *other_p = 0 ;
       
    60 static CyBool gSetupPending = CyFalse ;
       
    61 
       
    62 static volatile uint8_t gAsyncStallStale = 0;
       
    63 
       
    64 /* Forward declarations */
       
    65 static int SetupUSBPPort(CyAsDeviceHandle h, uint8_t media, CyBool isTurbo) ;
       
    66 static void MyUsbEventCallbackMS(CyAsDeviceHandle h, CyAsUsbEvent ev, void *evdata) ;
       
    67 static void PrintData(const char *name, uint8_t *data, uint16_t size) ;
       
    68 
       
    69 static void
       
    70 StallCallback(CyAsDeviceHandle h, CyAsReturnStatus_t status, uint32_t tag, CyAsFunctCBType cbtype, void *cbdata)
       
    71 {
       
    72     (void)h ;
       
    73     (void)cbtype ;
       
    74     (void)cbdata ;
       
    75 
       
    76     if (tag == 1)
       
    77         CyAsHalPrintMessage("*** Nak callback - status = %d\n", status) ;
       
    78     else
       
    79         CyAsHalPrintMessage("*** Stall callback - status = %d\n", status) ;
       
    80 }
       
    81 
       
    82 static void
       
    83 StallCallbackEX(CyAsDeviceHandle h,
       
    84                 CyAsReturnStatus_t status,
       
    85                 uint32_t tag,
       
    86                 CyAsFunctCBType type,
       
    87                 void*   data)
       
    88 {
       
    89     (void)h ;
       
    90     (void)type ;
       
    91     (void)data ;
       
    92     (void)status ;
       
    93 
       
    94     if(tag == 18)
       
    95     {
       
    96         CyAsReturnStatus_t ret = CyAsUsbClearStall(h, 3, StallCallbackEX, 21);
       
    97         CyAsHalAssert(ret == CY_AS_ERROR_SUCCESS) ;
       
    98     }
       
    99     else
       
   100         gAsyncStallDone = CyTrue ;
       
   101 }
       
   102 
       
   103 static void
       
   104 StallCallbackAsync(CyAsDeviceHandle h, CyAsReturnStatus_t status, uint32_t tag, CyAsFunctCBType cbtype, void *cbdata)
       
   105 {
       
   106     CyAsReturnStatus_t ret ;
       
   107     (void)cbtype ;
       
   108     (void)cbdata ;
       
   109     (void)tag ;
       
   110     (void)status ;
       
   111 
       
   112     if(gAsyncStallStale == 0)
       
   113     {
       
   114         gAsyncStallStale++;
       
   115         ret = CyAsUsbClearStall(h, 3, StallCallbackAsync, 21);
       
   116         CyAsHalAssert(ret == CY_AS_ERROR_SUCCESS) ;
       
   117     }
       
   118     else
       
   119     {
       
   120         gAsyncStallDone = CyTrue ;
       
   121     }
       
   122 }
       
   123 
       
   124 static void
       
   125 MyStorageEventCBMS(CyAsDeviceHandle h, CyAsBusNumber_t bus, uint32_t device, CyAsStorageEvent evtype, void *evdata)
       
   126 {
       
   127     (void)h ;
       
   128     (void)evdata ;
       
   129 
       
   130     switch (evtype)
       
   131     {
       
   132     case CyAsStorageAntioch:
       
   133         CyAsHalPrintMessage("CyAsStorageAntioch Event: bus=%d, device=%d\n", bus, device) ;
       
   134         switch (bus)
       
   135         {
       
   136         case 0:
       
   137             gStorageReleaseBus0 = CyTrue ;
       
   138             break;
       
   139         case 1:
       
   140             gStorageReleaseBus1 = CyTrue ;
       
   141             break;
       
   142         default:
       
   143             break;
       
   144         }
       
   145         break;
       
   146 
       
   147     case CyAsStorageProcessor:
       
   148         CyAsHalPrintMessage("CyAsStorageProcessor Event: bus=%d, device %d\n", bus, device) ;
       
   149         break;
       
   150 
       
   151     case CyAsStorageRemoved:
       
   152         CyAsHalPrintMessage("Bus %d, device %d has been removed\n", bus, device) ;
       
   153         break;
       
   154 
       
   155     case CyAsStorageInserted:
       
   156         CyAsHalPrintMessage("Bus %d, device %d has been inserted\n", bus, device) ;
       
   157         break;
       
   158 
       
   159     default:
       
   160         break;
       
   161     }
       
   162 }
       
   163 
       
   164 
       
   165 /*
       
   166 * This function exercises the USB module
       
   167 */
       
   168 int CyAsAPIUsbInit(const char *pgm, CyAsDeviceHandle h, CyAsHalDeviceTag tag)
       
   169 {
       
   170     CyAsReturnStatus_t ret ;
       
   171     /*char buffer[16] ;*/
       
   172 
       
   173 	g_tag = tag ;
       
   174 	memset(g_pAstDevice,0, sizeof(g_AstDevice));
       
   175 	g_pAstDevice->astHalTag = tag ;
       
   176 	g_pAstDevice->astDevHandle = h ;
       
   177 
       
   178     /*
       
   179     * Give a delay to allow any equipment to be ready (e.g. CATC)
       
   180     */
       
   181     /*CyAsHalPrintMessage("Press enter to begin USB operation (%s): ", "P Port Enumeration") ;
       
   182     fgets(buffer, sizeof(buffer), stdin) ;*/
       
   183 
       
   184 	CyAsHalPrintMessage("*** CyAsStorageStart...\n") ;
       
   185     ret = CyAsStorageStart(h, 0, 0) ;
       
   186     if (ret != CY_AS_ERROR_SUCCESS)
       
   187     {
       
   188         CyAsHalPrintMessage("%s: CyAsStorageStart returned error code %d\n", pgm, ret) ;
       
   189         return 0 ;
       
   190     }
       
   191     CyAsHalPrintMessage("*** CyAsStorageStart...Done\n") ;
       
   192     /*
       
   193     * Register a storage event call-back so that the USB attached storage can be
       
   194     * release when the USB connection has been made.
       
   195     */
       
   196     CyAsHalPrintMessage("*** CyAsStorageRegisterCallback...\n") ;
       
   197     ret = CyAsStorageRegisterCallback(h, MyStorageEventCBMS) ;
       
   198     if (ret != CY_AS_ERROR_SUCCESS)
       
   199     {
       
   200         CyAsHalPrintMessage("%s: CyAsStorageRegisterCallbackMS returned error code %d\n", pgm, ret) ;
       
   201         return 0 ;
       
   202     }
       
   203     CyAsHalPrintMessage("*** CyAsStorageRegisterCallback...Done\n") ;
       
   204 
       
   205 	ret = CyAsStorageRelease(h, 1, 0, 0, 0) ;
       
   206 	if (ret != CY_AS_ERROR_SUCCESS)
       
   207 	{
       
   208 		CyAsHalPrintMessage("CyAsStorageReleaseMS returned error code %d\n", ret) ;
       
   209 		return 0 ;
       
   210 	}
       
   211 	gStorageReleaseBus1 = CyFalse ;
       
   212 
       
   213     /*
       
   214      * Use the 24 MHz operating frequency, if the SD card is a low speed one.
       
   215      */
       
   216     ret = CyAsMiscSetLowSpeedSDFreq(h, CY_AS_SD_RATED_FREQ, 0, 0) ;
       
   217     if ((ret != CY_AS_ERROR_SUCCESS) && (ret != CY_AS_ERROR_INVALID_RESPONSE))
       
   218     {
       
   219         CyAsHalPrintMessage("%s: CyAsMiscSetLowSpeedSDFreq returned error code %d\n", pgm, ret) ;
       
   220         return 0 ;
       
   221     }
       
   222 
       
   223     /*
       
   224     * We are using P Port based enumeration
       
   225     */
       
   226 #ifdef DEBUG_ZERO
       
   227     if (!SetupUSBPPort(h, 2, 0))
       
   228 #else
       
   229     if (!SetupUSBPPort(h, 2, 1))
       
   230 #endif
       
   231         return 0 ;
       
   232     /*
       
   233     * Now we let the enumeration process happen via callbacks.  When the set configuration
       
   234     * request is processed, we are done with enumeration and ready to perform our function.
       
   235     */
       
   236     while (!gSetConfig)
       
   237         CyAsHalSleep(100) ;
       
   238 
       
   239     CyAsHalPrintMessage("*** Configuration complete, starting echo function\n") ;
       
   240 
       
   241     return 1 ;
       
   242 }
       
   243 
       
   244 static void
       
   245 MyCyAsMTPEventCallback(
       
   246     CyAsDeviceHandle handle,
       
   247     CyAsMTPEvent evtype,
       
   248     void* evdata)
       
   249 {
       
   250 	(void) handle;
       
   251 	switch(evtype)
       
   252 	{
       
   253 	case CyAsMTPSendObjectComplete:
       
   254 		{
       
   255 			CyAsMTPSendObjectCompleteData* sendObjData = (CyAsMTPSendObjectCompleteData*) evdata ;
       
   256 			CyAsHalPrintMessage("MTP EVENT: SendObjectComplete\n");
       
   257 			CyAsHalPrintMessage("Bytes sent = %d\nSend status = %d",sendObjData->byte_count,sendObjData->status);
       
   258 			g_pAstDevice->tmtpSendCompleteData.byte_count = sendObjData->byte_count;
       
   259 			g_pAstDevice->tmtpSendCompleteData.status = sendObjData->status;
       
   260 			g_pAstDevice->tmtpSendCompleteData.transaction_id = sendObjData->transaction_id ;
       
   261 			g_pAstDevice->tmtpSendComplete = CyTrue ;
       
   262 			break;
       
   263 		}
       
   264 	case CyAsMTPGetObjectComplete:
       
   265 		{
       
   266 			CyAsMTPGetObjectCompleteData*  getObjData = (CyAsMTPGetObjectCompleteData*) evdata ;
       
   267 			CyAsHalPrintMessage("MTP EVENT: GetObjectComplete\n");
       
   268 			CyAsHalPrintMessage("Bytes got = %d\nGet status = %d",getObjData->byte_count,getObjData->status);
       
   269 			g_pAstDevice->tmtpGetCompleteData.byte_count = getObjData->byte_count;
       
   270 			g_pAstDevice->tmtpGetCompleteData.status = getObjData->status ;
       
   271 			g_pAstDevice->tmtpGetComplete = CyTrue ;
       
   272 			break;
       
   273 		}
       
   274 	case CyAsMTPBlockTableNeeded:
       
   275 		g_pAstDevice->tmtpNeedNewBlkTbl = CyTrue ;
       
   276 		break;
       
   277 	default:
       
   278 		;
       
   279 	}
       
   280 
       
   281 }
       
   282 /*
       
   283 * This function is responsible for initializing the USB function within West Bridge.  This
       
   284 * function initializes West Bridge for P port based enumeration.
       
   285 */
       
   286 int SetupUSBPPort(CyAsDeviceHandle h, uint8_t bus, CyBool isTurbo)
       
   287 {
       
   288     CyAsReturnStatus_t ret ;
       
   289     CyAsUsbEnumControl config ;
       
   290 #ifdef DEBUG_ZERO
       
   291     CyAsUsbEndPointConfig epconfig ;
       
   292 #endif
       
   293     uint32_t count = 0 ;
       
   294     char *media_name = "SD";
       
   295 
       
   296     gUsbTestDone = CyFalse ;
       
   297 
       
   298     CyAsHalPrintMessage("*** SetupUSBPPort...\n") ;
       
   299     /*
       
   300     * Intialize the primary descriptor to be the full speed descriptor and the
       
   301     * other descriptor to by the high speed descriptor.  This will swap if we see a
       
   302     * high speed event.
       
   303     */
       
   304 #ifdef DEBUG_ZERO
       
   305     desc_p = (CyCh9ConfigurationDesc *)&ConfigFSDesc ;
       
   306     other_p = (CyCh9ConfigurationDesc *)&ConfigHSDesc ;
       
   307 #else
       
   308     desc_p = (CyCh9ConfigurationDesc *)&ZeroDesc ;
       
   309     other_p = (CyCh9ConfigurationDesc *)&ZeroDesc ;
       
   310 #endif
       
   311     /* Step 1: Release the USB D+ and D- pins
       
   312     *
       
   313     * This code releases control of the D+ and D- pins if they have been previously
       
   314     * acquired by the P Port processor.  The physical D+ and D- pins are controlled either by
       
   315     * West Bridge, or by some other hardware external to West Bridge.  If external hardware is using
       
   316     * these pins, West Bridge must put these pins in a high impedence state in order to insure there
       
   317     * is no coflict over the use of the pins.  Before we can initialize the USB capabilities of
       
   318     * West Bridge, we must be sure West Bridge has ownership of the D+ and D- signals.  West Bridge will take
       
   319     * ownership of these pins as long as the P port processor has released them.  This call
       
   320     * releases control of these pins.  Before calling the CyAsMiscReleaseResource(), the P port API
       
   321     * must configure the hardware to release control of the D+ and D- pins by any external hardware.
       
   322     *
       
   323     * Note that this call can be made anywhere in the intialization sequence as long as it is done
       
   324     * before the call to CyAsUsbConnect().  If not, when the CyAsUsbConnect() call is made, West Bridge
       
   325     * will detect that it does not own the D+ and D- pins and the call to CyAsUsbConnect will fail.
       
   326     */
       
   327     ret = CyAsMiscReleaseResource(h, CyAsBusUSB) ;
       
   328     if (ret != CY_AS_ERROR_SUCCESS && ret != CY_AS_ERROR_RESOURCE_NOT_OWNED)
       
   329     {
       
   330         CyAsHalPrintMessage("Cannot Release USB reousrce: CyAsMiscReleaseResourceMS failed with error code %d\n", ret) ;
       
   331         return 0 ;
       
   332     }
       
   333 
       
   334     /*
       
   335     * Step 2: Start the USB stack
       
   336     *
       
   337     * This code initializes the USB stack.  It takes a handle to an West Bridge device
       
   338     * previously created with a call to CyAsMiscCreateDevice().
       
   339     */
       
   340     ret = CyAsUsbStart(h, 0, 0) ;
       
   341     if (ret != CY_AS_ERROR_SUCCESS)
       
   342     {
       
   343         CyAsHalPrintMessage("CyAsUsbStart failed with error code %d\n", ret) ;
       
   344         return 0 ;
       
   345     }
       
   346 
       
   347     /*
       
   348     * Step 3: Register a callback
       
   349     *
       
   350     * This code registers a callback to handle USB events.  This callback function will handle
       
   351     * all setup packets during enumeration as well as other USB events (SUSPEND, RESUME, CONNECT,
       
   352     * DISCONNECT, etc.)
       
   353     */
       
   354     ret = CyAsUsbRegisterCallback(h, MyUsbEventCallbackMS) ;
       
   355     if (ret != CY_AS_ERROR_SUCCESS)
       
   356     {
       
   357         CyAsHalPrintMessage("CyAsUsbRegisterCallbackMS failed with error code %d\n", ret) ;
       
   358         return 0 ;
       
   359     }
       
   360 
       
   361 #ifdef DEBUG_ZERO
       
   362     if ( isTurbo )
       
   363 #endif
       
   364     {
       
   365 		/*
       
   366 		 * The SD/MMC resource needs to be released before the device
       
   367 		 * can be successfully initialized by the firmware.
       
   368 		 */
       
   369 		ret = CyAsMiscReleaseResource(h, CyAsBus_1) ;
       
   370 		if (ret != CY_AS_ERROR_SUCCESS && ret != CY_AS_ERROR_RESOURCE_NOT_OWNED)
       
   371 		{
       
   372 			CyAsHalPrintMessage("CyAsMtpApp: CyAsMiscReleaseResource failed with error code %d\n", ret) ;
       
   373 			return -ret;
       
   374 		}
       
   375 
       
   376 		ret = CyAsStorageStart(h, 0, 0) ;
       
   377 		if (ret != CY_AS_ERROR_SUCCESS)
       
   378 		{
       
   379 			CyAsHalPrintMessage("CyAsMtpApp: CyAsStorageStart failed with error code %d\n", ret) ;
       
   380 			return -ret;
       
   381 		}
       
   382 
       
   383 		ret = CyAsStorageQueryMedia(h, CyAsMediaSDFlash, &count, 0, 0) ;
       
   384 		if (ret != CY_AS_ERROR_SUCCESS)
       
   385 		{
       
   386 		  CyAsHalPrintMessage("CyAsMtpApp: Cannot query %s device count - Reason code %d\n", media_name, ret) ;
       
   387 		  return -ret ;
       
   388 		}
       
   389 		CyAsHalPrintMessage("CyAsMtpApp: %d %s device(s) found\n", count, media_name) ;
       
   390 
       
   391 		ret = CyAsStorageClaim(h, CyAsBus_1, 0, 0, 0) ;
       
   392 		if (ret != CY_AS_ERROR_SUCCESS)
       
   393 		{
       
   394 		  CyAsHalPrintMessage("CyAsMtpApp: Cannot claim %s media - Reason code %d\n", media_name, ret) ;
       
   395 		  return -ret;
       
   396 		}
       
   397 
       
   398 		/* We know that there is only one */
       
   399 		g_pAstDevice->dev_data.bus = CyAsBus_1 ;
       
   400 		g_pAstDevice->dev_data.device = 0 ;
       
   401 		ret = CyAsStorageQueryDevice(h, &(g_pAstDevice->dev_data), 0, 0) ;
       
   402 		if (ret != CY_AS_ERROR_SUCCESS)
       
   403 		{
       
   404 		  CyAsHalPrintMessage("CyAsMtpApp: Cannot query %s device - Reason code %d\n", media_name, ret) ;
       
   405 		  return -ret ;
       
   406 		}
       
   407 		CyAsHalPrintMessage("CyAsMtpApp: blocksize %d, %d units found\n",
       
   408 			g_pAstDevice->dev_data.desc_p.block_size, g_pAstDevice->dev_data.desc_p.number_units) ;
       
   409 
       
   410 		g_pAstDevice->unit_data.bus = CyAsBus_1 ;
       
   411 		g_pAstDevice->unit_data.device = 0 ;
       
   412 		g_pAstDevice->unit_data.unit = 0 ;
       
   413 		/* We know that there is only one */
       
   414 		ret = CyAsStorageQueryUnit(h, &(g_pAstDevice->unit_data), 0, 0) ;
       
   415 		if (ret != CY_AS_ERROR_SUCCESS)
       
   416 		{
       
   417 		  CyAsHalPrintMessage("CyAsMtpApp: Cannot query %s device unit - Reason code %d\n", media_name, ret) ;
       
   418 		  return -ret ;
       
   419 		}
       
   420 		CyAsHalPrintMessage("CyAsMtpApp: blocksize %d, %d Block(s) found\n",
       
   421 			g_pAstDevice->unit_data.desc_p.block_size, g_pAstDevice->unit_data.desc_p.unit_size) ;
       
   422 
       
   423 			CyAsHalPrintMessage("CyAsMtpApp: Starting TMTP...\n");
       
   424 		ret = CyAsMTPStart(h, MyCyAsMTPEventCallback, 0, 0) ;
       
   425 		if (ret != CY_AS_ERROR_SUCCESS)
       
   426 		{
       
   427 			CyAsHalPrintMessage("CyAsMTPStart failed with error code %d\n", ret) ;
       
   428 			return 0 ;
       
   429 		}
       
   430     }
       
   431 
       
   432     /*
       
   433     * Step 4: Setup the enumeration mode
       
   434     *
       
   435     * This code tells the West Bridge API how enumeration will be done.  Specifically in this
       
   436     * example we are configuring the API for P Port processor based enumeraton.  This will cause
       
   437     * all setup packets to be relayed to the P port processor via the USB event callback.  See
       
   438     * the function CyAsUsbRegisterEventCallback() for more information about this callback.
       
   439     */
       
   440     config.antioch_enumeration = CyFalse ;                      /* P port will do enumeration, not West Bridge */
       
   441 
       
   442     /* Set the media to enumerate through USB */
       
   443 #ifdef DEBUG_ZERO
       
   444     config.devices_to_enumerate[0][0] = CyFalse;
       
   445     config.devices_to_enumerate[1][0] = CyFalse;
       
   446 #else
       
   447     config.devices_to_enumerate[0][0] = (bus & CY_TEST_BUS_0) ? CyTrue : CyFalse;
       
   448     config.devices_to_enumerate[1][0] = (bus & CY_TEST_BUS_1) ? CyTrue : CyFalse;
       
   449 #endif
       
   450 
       
   451     if (isTurbo)
       
   452     {
       
   453         /* Force SD bus */
       
   454         config.devices_to_enumerate[0][0] = CyFalse;
       
   455         config.devices_to_enumerate[1][0] =  CyTrue ;
       
   456         /* No MSC in Turbo */
       
   457         config.mass_storage_interface = 0 ;
       
   458         config.mtp_interface = 1 ;
       
   459     }
       
   460     else
       
   461     {
       
   462         /* not Turbo here */
       
   463         config.mtp_interface = 0 ;
       
   464 
       
   465 #ifndef DEBUG_MSC
       
   466         config.mass_storage_interface = 0 ;
       
   467         config.mass_storage_callbacks = CyFalse ;
       
   468 #else
       
   469         /* Force SD bus */
       
   470         config.devices_to_enumerate[1][0] =  CyTrue ;
       
   471         config.mass_storage_interface = 1 ;
       
   472         config.mass_storage_callbacks = CyTrue ;
       
   473 #endif
       
   474     }
       
   475     ret = CyAsUsbSetEnumConfig(h, &config, 0, 0) ;
       
   476     if (ret != CY_AS_ERROR_SUCCESS)
       
   477     {
       
   478         CyAsHalPrintMessage("CyAsUsbSetEnumConfigMS failed with error code %d\n", ret) ;
       
   479         return 0 ;
       
   480     }
       
   481 
       
   482 #ifndef DEBUG_MSC
       
   483     /*
       
   484      * Step 5: set physical configuration
       
   485      */
       
   486 #ifdef DEBUG_ZERO
       
   487     /*nxz-debug-z ret = CyAsUsbSetPhysicalConfiguration(h, 5) ;*/
       
   488     ret = CyAsUsbSetPhysicalConfiguration(h, 1) ;
       
   489 #else
       
   490     ret = CyAsUsbSetPhysicalConfiguration(h, 5) ;
       
   491 #endif
       
   492     if (ret != CY_AS_ERROR_SUCCESS)
       
   493     {
       
   494         CyAsHalPrintMessage("CyAsUsbSetPhysicalConfiguration failed with error code %d\n", ret) ;
       
   495         return 0 ;
       
   496     }
       
   497 
       
   498     /*
       
   499     * Step 5: Commit the endpoint configuration
       
   500     */
       
   501 #ifdef DEBUG_ZERO
       
   502     epconfig.enabled = CyTrue ;
       
   503     epconfig.dir = CyAsUsbOut ;
       
   504     epconfig.type = CyAsUsbBulk ;
       
   505     epconfig.size = 0 ;
       
   506     epconfig.physical = 1 ;
       
   507     ret = CyAsUsbSetEndPointConfig(h, 3, &epconfig) ;
       
   508     if ( ret != CY_AS_ERROR_SUCCESS )
       
   509     {
       
   510 	CyAsHalPrintMessage("CyAsUsbSetEndPointConfig failed with error code %d\n", ret) ;
       
   511         return 0 ;
       
   512     }
       
   513 
       
   514     epconfig.enabled = CyTrue ;
       
   515     epconfig.dir = CyAsUsbIn ;
       
   516     epconfig.type = CyAsUsbBulk ;
       
   517     epconfig.size = 0 ;
       
   518     epconfig.physical = 3 ;
       
   519     ret = CyAsUsbSetEndPointConfig(h, 5, &epconfig) ;
       
   520     if ( ret != CY_AS_ERROR_SUCCESS )
       
   521     {
       
   522 	CyAsHalPrintMessage("CyAsUsbSetEndPointConfig failed with error code %d\n", ret) ;
       
   523         return 0 ;
       
   524     }
       
   525 
       
   526     /*nxz-debug-z */
       
   527     epconfig.enabled = CyTrue ;
       
   528     epconfig.dir = CyAsUsbIn ;
       
   529     epconfig.type = CyAsUsbInt ;
       
   530     epconfig.size = 64 ;
       
   531     epconfig.physical = 2 ;
       
   532     ret = CyAsUsbSetEndPointConfig(h, 7, &epconfig) ;
       
   533     if ( ret != CY_AS_ERROR_SUCCESS )
       
   534     {
       
   535 	CyAsHalPrintMessage("CyAsUsbSetEndPointConfig failed with error code %d\n", ret) ;
       
   536         return 0 ;
       
   537     }
       
   538 #endif
       
   539 
       
   540     /*
       
   541     * This code commits the endpoint configuration to the West Bridge hardware.
       
   542     */
       
   543     ret = CyAsUsbCommitConfig(h, 0, 0) ;
       
   544     if (ret != CY_AS_ERROR_SUCCESS)
       
   545     {
       
   546         CyAsHalPrintMessage("CyAsUsbCommitConfig failed with error code %d\n", ret) ;
       
   547         return 0 ;
       
   548     }
       
   549 #endif
       
   550     /*
       
   551     * Step 6: Connect to the USB host.
       
   552     *
       
   553     * This code actually connects the D+ and D- signals internal to West Bridge to the D+ and D- pins
       
   554     * on the device.  If the host is already physically connected, this will begin the enumeration
       
   555     * process.  Otherwise, the enumeration process will being when the host is connected.
       
   556     */
       
   557     ret = CyAsUsbConnect(h, 0, 0) ;
       
   558     if (ret != CY_AS_ERROR_SUCCESS)
       
   559     {
       
   560         CyAsHalPrintMessage("CyAsUsbConnect failed with error code %d\n", ret) ;
       
   561         return 0 ;
       
   562     }
       
   563 
       
   564     CyAsHalPrintMessage("*** SetupUSBPPort...Done\n") ;
       
   565     return 1 ;
       
   566 }
       
   567 
       
   568 /*
       
   569 * Print a block of data, useful for displaying data during debug.
       
   570 */
       
   571 static void PrintData(const char *name_p, uint8_t *data, uint16_t size)
       
   572 {
       
   573     uint32_t i = 0 ;
       
   574     uint32_t linecnt = 0 ;
       
   575 
       
   576     while (i < size)
       
   577     {
       
   578         if (linecnt == 0)
       
   579             CyAsHalPrintMessage("%s @ %02x:", name_p, i) ;
       
   580 
       
   581         CyAsHalPrintMessage(" %02x", data[i]) ;
       
   582 
       
   583         linecnt++ ;
       
   584         i++ ;
       
   585 
       
   586         if (linecnt == 16)
       
   587         {
       
   588             CyAsHalPrintMessage("\n") ;
       
   589             linecnt = 0 ;
       
   590         }
       
   591     }
       
   592 
       
   593     if (linecnt != 0)
       
   594         CyAsHalPrintMessage("\n") ;
       
   595 }
       
   596 
       
   597 /*
       
   598 * This is the write callback for writes that happen as part of the setup operation.
       
   599 */
       
   600 static void SetupWriteCallback(CyAsDeviceHandle h, CyAsEndPointNumber_t ep, uint32_t count, void *buf_p, CyAsReturnStatus_t status)
       
   601 {
       
   602     (void)count ;
       
   603     (void)h ;
       
   604     (void)buf_p ;
       
   605 
       
   606     /*assert(ep == 0) ;
       
   607     assert(buf_p == replybuf) ;*/
       
   608 
       
   609     RestoreReplyArea() ;
       
   610     if (status != CY_AS_ERROR_SUCCESS)
       
   611         CyAsHalPrintMessage("Error returned in SetupWriteCallback - %d\n", status) ;
       
   612 
       
   613     gSetupPending = CyFalse ;
       
   614 }
       
   615 
       
   616 static CyAsReturnStatus_t
       
   617 SetupWrite(CyAsDeviceHandle h, uint32_t requested, uint32_t dsize, void *data)
       
   618 {
       
   619     CyBool spacket = CyTrue ;
       
   620 
       
   621     if (requested == dsize)
       
   622         spacket = CyFalse ;
       
   623 
       
   624     return CyAsUsbWriteDataAsync(h, 0, dsize, data, spacket, SetupWriteCallback) ;
       
   625 }
       
   626 
       
   627 int mystrlen(char* str)
       
   628 {
       
   629 	int len = 0 ;
       
   630 
       
   631 	while( str && (*str != '\0') )
       
   632 	{
       
   633 		len++;
       
   634 		str++;
       
   635 	}
       
   636 
       
   637 	return len ;
       
   638 }
       
   639 /*
       
   640 * Send the USB host a string descriptor.  If the index is zero, send the
       
   641 * array of supported languages, otherwise send the string itself per the USB
       
   642 * Ch9 specification.
       
   643 */
       
   644 static void SendStringDescriptor(CyAsDeviceHandle h, uint8_t *data)
       
   645 {
       
   646     CyAsReturnStatus_t ret = CY_AS_ERROR_SUCCESS;
       
   647     int i = data[2] ;
       
   648     int langid = data[4] | (data[5] << 8) ;
       
   649     uint16_t reqlen = data[6] | (data[7] << 8) ;
       
   650 
       
   651     CyAsHalPrintMessage("**** CY_CH9_GD_STRING - %d\n", i) ;
       
   652     if (i == 0)
       
   653     {
       
   654         uint8_t *reply ;
       
   655 
       
   656         reply = GetReplyArea() ;
       
   657         reply[0] = 4 ;
       
   658         reply[1] = CY_CH9_GD_STRING ;
       
   659         reply[2] = CY_CH9_LANGID_ENGLISH_UNITED_STATES & 0xff ;
       
   660         reply[3] = (CY_CH9_LANGID_ENGLISH_UNITED_STATES >> 8) & 0xff ;
       
   661         ret = SetupWrite(h, reqlen, 4, reply) ;
       
   662     }
       
   663     else if (i <= sizeof(UsbStrings)/sizeof(UsbStrings[0]) && langid == CY_CH9_LANGID_ENGLISH_UNITED_STATES)
       
   664     {
       
   665         uint8_t *reply ;
       
   666         uint16_t len = (uint16_t)mystrlen(UsbStrings[i - 1]) ;
       
   667 
       
   668         CyAsHalPrintMessage("*** Sending string '%s'\n", UsbStrings[i - 1]) ;
       
   669 
       
   670         reply = GetReplyArea() ;
       
   671         reply[0] = (uint8_t)(len * 2 + 2) ;
       
   672         reply[1] = CY_CH9_GD_STRING ;
       
   673         /* nxz-linux-port */
       
   674 	/*memcpy(reply + 2, UsbStrings[i - 1], len ) ;
       
   675         ret = SetupWrite(h, reqlen, len  + 2, reply) ;*/
       
   676         {
       
   677             uint16_t index ;
       
   678             uint16_t *rpy = (uint16_t *)(reply + 2) ;
       
   679             for (index = 0; index < len; index++)
       
   680             {
       
   681                 *rpy = (uint16_t)(UsbStrings[i - 1][index]) ;
       
   682                 rpy++ ;
       
   683             }
       
   684         }
       
   685         ret = SetupWrite(h, reqlen, len * 2 + 2, reply) ;
       
   686     }
       
   687     else
       
   688     {
       
   689         /*
       
   690         * If the host asks for an invalid string, we must stall EP 0
       
   691         */
       
   692         ret = CyAsUsbSetStall(h, 0, StallCallback, 0) ;
       
   693         if (ret != CY_AS_ERROR_SUCCESS)
       
   694             CyAsHalPrintMessage("**** cannot set stall state on EP 0\n") ;
       
   695 
       
   696         CyAsHalPrintMessage("Host asked for invalid string or langid, index = 0x%04x, langid = 0x%04x\n", i, langid) ;
       
   697     }
       
   698 
       
   699     if (ret != CY_AS_ERROR_SUCCESS)
       
   700         CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   701     else
       
   702         CyAsHalPrintMessage("** Write Sucessful\n") ;
       
   703 }
       
   704 
       
   705 static CyAsReturnStatus_t
       
   706 SendSetupData(CyAsDeviceHandle h, uint32_t reqlen, uint32_t size, void *data_p)
       
   707 {
       
   708     CyAsReturnStatus_t ret ;
       
   709     uint8_t *reply ;
       
   710 
       
   711     /*
       
   712     * Never send more data than was requested
       
   713     */
       
   714     if (size > reqlen)
       
   715         size = reqlen ;
       
   716 
       
   717     reply = GetReplyArea() ;
       
   718     /*assert(reply != 0) ;*/
       
   719 
       
   720     memcpy(reply, data_p, size) ;
       
   721     ret = SetupWrite(h, reqlen, size, reply) ;
       
   722     if (ret != CY_AS_ERROR_SUCCESS)
       
   723         RestoreReplyArea() ;
       
   724 
       
   725     return ret ;
       
   726 }
       
   727 
       
   728 /*
       
   729 * This function processes the GET DESCRIPTOR usb request.
       
   730 */
       
   731 static void ProcessGetDescriptorRequest(CyAsDeviceHandle h, uint8_t *data)
       
   732 {
       
   733     CyAsReturnStatus_t ret ;
       
   734     uint16_t reqlen = data[6] | (data[7] << 8) ;
       
   735 
       
   736     if (data[3] == CY_CH9_GD_DEVICE)
       
   737     {
       
   738         /*
       
   739         * Return the device descriptor
       
   740         */
       
   741         CyAsHalPrintMessage("**** CY_CH9_GD_DEVICE (size = %d)\n", sizeof(pport_device_desc)) ;
       
   742         PrintData("DD", (uint8_t *)&pport_device_desc, sizeof(pport_device_desc)) ;
       
   743 
       
   744         ret = SendSetupData(h, reqlen, sizeof(pport_device_desc), &pport_device_desc) ;
       
   745         if (ret != CY_AS_ERROR_SUCCESS)
       
   746             CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   747         else
       
   748             CyAsHalPrintMessage("** Write Sucessful\n") ;
       
   749     }
       
   750     else if (data[3] == CY_CH9_GD_DEVICE_QUALIFIER)
       
   751     {
       
   752         /*
       
   753         * Return the device descriptor
       
   754         */
       
   755         CyAsHalPrintMessage("**** CY_CH9_GD_DEVICE (size = %d)\n", sizeof(device_qualifier)) ;
       
   756         PrintData("DD", (uint8_t *)&device_qualifier, sizeof(device_qualifier)) ;
       
   757 
       
   758         ret = SendSetupData(h, reqlen, sizeof(device_qualifier), &device_qualifier) ;
       
   759         if (ret != CY_AS_ERROR_SUCCESS)
       
   760             CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   761         else
       
   762             CyAsHalPrintMessage("** Write Sucessful\n") ;
       
   763     }
       
   764     else if (data[3] == CY_CH9_GD_CONFIGURATION)
       
   765     {
       
   766         const char *desc_name_p ;
       
   767         uint16_t size ;
       
   768 
       
   769         /*
       
   770         * Return the CONFIGURATION descriptor.
       
   771         */
       
   772         if (desc_p == (CyCh9ConfigurationDesc *)&ConfigHSDesc)
       
   773         {
       
   774             desc_name_p = "HighSpeed" ;
       
   775             size = sizeof(ConfigHSDesc) ;
       
   776         }
       
   777         else if (desc_p == (CyCh9ConfigurationDesc *)&ConfigFSDesc)
       
   778         {
       
   779             desc_name_p = "FullSpeed" ;
       
   780             size = sizeof(ConfigFSDesc) ;
       
   781         }
       
   782         else if (desc_p == &ZeroDesc)
       
   783         {
       
   784             desc_name_p = "ZeroDesc" ;
       
   785             size = sizeof(ZeroDesc) ;
       
   786         }
       
   787         else
       
   788         {
       
   789             desc_name_p = "UNKNOWN" ;
       
   790             size = 0 ;
       
   791         }
       
   792         CyAsHalPrintMessage("**** CY_CH9_GD_CONFIGURATION - %s (size = %d)\n", desc_name_p, size) ;
       
   793         if (size > 0)
       
   794         {
       
   795             PrintData("CFG", (uint8_t *)desc_p, size) ;
       
   796             desc_p->bDescriptorType = CY_CH9_GD_CONFIGURATION;
       
   797             ret = SendSetupData(h, reqlen, size, desc_p) ;
       
   798             if (ret != CY_AS_ERROR_SUCCESS)
       
   799                 CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   800             else
       
   801                 CyAsHalPrintMessage("** Write Sucessful\n") ;
       
   802         }
       
   803     }
       
   804     else if (data[3] == CY_CH9_GD_OTHER_SPEED_CONFIGURATION)
       
   805     {
       
   806         const char *desc_name_p ;
       
   807         uint16_t size ;
       
   808 
       
   809         /*
       
   810         * Return the CONFIGURATION descriptor.
       
   811         */
       
   812         if (other_p == (CyCh9ConfigurationDesc *)&ConfigHSDesc)
       
   813         {
       
   814             desc_name_p = "HighSpeed" ;
       
   815             size = sizeof(ConfigHSDesc) ;
       
   816         }
       
   817         else if (other_p == (CyCh9ConfigurationDesc *)&ConfigFSDesc)
       
   818         {
       
   819             desc_name_p = "FullSpeed" ;
       
   820             size = sizeof(ConfigFSDesc) ;
       
   821         }
       
   822         else if (other_p == &ZeroDesc)
       
   823         {
       
   824             desc_name_p = "ZeroDesc" ;
       
   825             size = sizeof(ZeroDesc) ;
       
   826         }
       
   827         else
       
   828         {
       
   829             desc_name_p = "UNKNOWN" ;
       
   830             size = 0 ;
       
   831         }
       
   832         CyAsHalPrintMessage("**** CY_CH9_GD_OTHER_SPEED_CONFIGURATION - %s (size = %d)\n", desc_name_p, size) ;
       
   833         if (size > 0)
       
   834         {
       
   835             PrintData("CFG", (uint8_t *)other_p, size) ;
       
   836             other_p->bDescriptorType = CY_CH9_GD_OTHER_SPEED_CONFIGURATION;
       
   837             ret = SendSetupData(h, reqlen, size, other_p) ;
       
   838             if (ret != CY_AS_ERROR_SUCCESS)
       
   839                 CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   840             else
       
   841                 CyAsHalPrintMessage("** Write Sucessful\n") ;
       
   842         }
       
   843     }
       
   844     else if (data[3] == CY_CH9_GD_STRING)
       
   845     {
       
   846         SendStringDescriptor(h, data) ;
       
   847     }
       
   848     else if (data[3] == CY_CH9_GD_REPORT)
       
   849     {
       
   850         CyAsHalPrintMessage("**** CY_CH9_GD_REPORT\n") ;
       
   851     }
       
   852     else if (data[3] == CY_CH9_GD_HID)
       
   853     {
       
   854         CyAsHalPrintMessage("**** CY_CH9_GD_HID\n") ;
       
   855     }
       
   856     else
       
   857     {
       
   858         CyAsHalPrintMessage("**** Unknown Descriptor request\n") ;
       
   859     }
       
   860 }
       
   861 
       
   862 /*
       
   863 static void EP0DataCallback(CyAsDeviceHandle h, CyAsEndPointNumber_t ep, uint32_t count, void *buf_p, CyAsReturnStatus_t status)
       
   864 {
       
   865     (void)ep ;
       
   866     (void)h ;
       
   867 
       
   868     if (status == CY_AS_ERROR_SUCCESS)
       
   869     {
       
   870         CyAsHalPrintMessage("Read data phase of setup packet from EP0\n") ;
       
   871         PrintData("SetupData", buf_p, (uint16_t)count) ;
       
   872     }
       
   873     else
       
   874     {
       
   875         CyAsHalPrintMessage("Error reading data from EP0\n") ;
       
   876     }
       
   877 }
       
   878 */
       
   879 
       
   880 static void
       
   881 ProcessSetupPacketRequest(CyAsDeviceHandle h, uint8_t *data)
       
   882 {
       
   883     CyAsReturnStatus_t ret ;
       
   884     uint16_t reqlen = data[6] | (data[7] << 8) ;
       
   885 
       
   886     RestoreReplyArea() ;
       
   887 
       
   888     if ((data[0] & CY_CH9_SETUP_TYPE_MASK) == CY_CH9_SETUP_STANDARD_REQUEST)
       
   889     {
       
   890         switch(data[1])
       
   891         {
       
   892         case CY_CH9_SC_GET_DESCRIPTOR:
       
   893             CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_GET_DESCRIPTOR request\n") ;
       
   894             ProcessGetDescriptorRequest(h, data) ;
       
   895             break ;
       
   896 
       
   897         case CY_CH9_SC_GET_INTERFACE:
       
   898             {
       
   899                 uint8_t *response = GetReplyArea() ;
       
   900 
       
   901                 *response = 0 ;
       
   902                 CyAsHalPrintMessage("************* USB EP0: CY_CH9_SC_GET_INTERFACE request - RETURNING ZERO\n") ;
       
   903                 ret = SetupWrite(h, reqlen, 1, response) ;
       
   904                 if (ret != CY_AS_ERROR_SUCCESS)
       
   905                 {
       
   906                     CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   907                 }
       
   908             }
       
   909             break ;
       
   910 
       
   911         case CY_CH9_SC_SET_INTERFACE:
       
   912             CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_SET_INTERFACE request\n") ;
       
   913             break ;
       
   914 
       
   915         case CY_CH9_SC_SET_CONFIGURATION:
       
   916 			{
       
   917 				/*CyAsReturnStatus_t ret = CY_AS_ERROR_SUCCESS ;*/
       
   918 
       
   919 				CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_SET_CONFIGURATION request (%02x)\n", data[2]) ;
       
   920 				{
       
   921 					gAsyncStallDone = CyFalse ;
       
   922 					gAsyncStallStale = 0;
       
   923 					CyAsUsbSetStall(h, 3, StallCallbackEX, 18);
       
   924 				}
       
   925 				gSetConfig = CyTrue ;
       
   926 				g_pAstDevice->configDone = 1 ;
       
   927 				MyConfiguration = data[2];
       
   928 
       
   929 				/* Set configuration is the last step before host send MTP data to EP2 */
       
   930 #if 0
       
   931 				ret = CyAsUsbReadDataAsync(g_pAstDevice->astDevHandle,
       
   932 						CY_AS_MTP_BULK_OUT_EP, CyFalse, 512,
       
   933 						g_pAstDevice->astEPBuf, TmtpReadCallback) ;
       
   934 				if(ret != CY_AS_ERROR_SUCCESS && ret != CY_AS_ERROR_ASYNC_PENDING)
       
   935 				{
       
   936 					/*handle error in reading*/
       
   937 					CyAsHalPrintMessage("CyAsMtpApp: CyAsUsbReadDataAsync Failed. Reason code: %d\n",ret) ;
       
   938 					return ;
       
   939 				}
       
   940 #endif
       
   941 			}
       
   942             break ;
       
   943 
       
   944         case CY_CH9_SC_GET_CONFIGURATION:
       
   945             {
       
   946                 uint8_t *response = GetReplyArea() ;
       
   947 
       
   948                 *response = MyConfiguration ;
       
   949                 CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_GET_INTERFACE request\n") ;
       
   950                 ret = SetupWrite(h, reqlen, 1, response) ;
       
   951                 if (ret != CY_AS_ERROR_SUCCESS)
       
   952                 {
       
   953                     CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   954                 }
       
   955             }
       
   956             CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_GET_CONFIGURATION request\n") ;
       
   957             break ;
       
   958 
       
   959         case CY_CH9_SC_GET_STATUS:
       
   960             {
       
   961                 uint16_t *response = (uint16_t *)GetReplyArea() ;
       
   962 
       
   963                 *response = 0 ;
       
   964                 CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_GET_STATUS request\n") ;
       
   965                 ret = SetupWrite(h, reqlen, 2, response) ;
       
   966                 if (ret != CY_AS_ERROR_SUCCESS)
       
   967                 {
       
   968                     CyAsHalPrintMessage("****** ERROR WRITING USB DATA - %d\n", ret) ;
       
   969                 }
       
   970             }
       
   971             break ;
       
   972 
       
   973         case CY_CH9_SC_CLEAR_FEATURE:
       
   974             {
       
   975                 uint16_t feature = data[2] | (data[3] << 8) ;
       
   976                 CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_CLEAR_FEATURE request\n") ;
       
   977 
       
   978                 if ((data[0] & CY_CH9_SETUP_DEST_MASK) == CY_CH9_SETUP_DEST_ENDPOINT && feature == 0)
       
   979                 {
       
   980                     CyAsEndPointNumber_t ep = data[4] | (data[5] << 8) ;
       
   981                     /* This is a clear feature/endpoint halt on an endpoint */
       
   982                     CyAsHalPrintMessage("Calling ClearStall on EP %d\n", ep) ;
       
   983                     ret = CyAsUsbClearStall(h, ep, StallCallback, 0) ;
       
   984                     if (ret != CY_AS_ERROR_SUCCESS)
       
   985                     {
       
   986                         CyAsHalPrintMessage("******* ERROR SEND CLEAR STALL REQUEST - %d\n", ret) ;
       
   987                     }
       
   988                 }
       
   989             }
       
   990             break ;
       
   991 
       
   992         case CY_CH9_SC_SET_FEATURE:
       
   993             {
       
   994                 uint16_t feature = data[2] | (data[3] << 8) ;
       
   995                 CyAsHalPrintMessage("USB EP0 : CY_CH9_SC_SET_FEATURE request\n") ;
       
   996 
       
   997                 if ((data[0] & CY_CH9_SETUP_DEST_MASK) == CY_CH9_SETUP_DEST_ENDPOINT && feature == 0)
       
   998                 {
       
   999                     CyAsEndPointNumber_t ep = data[4] | (data[5] << 8) ;
       
  1000                     /* This is a clear feature/endpoint halt on an endpoint */
       
  1001                     CyAsHalPrintMessage("Calling SetStall on EP %d\n", ep) ;
       
  1002                     ret = CyAsUsbSetStall(h, ep, StallCallback, 0) ;
       
  1003                     if (ret != CY_AS_ERROR_SUCCESS)
       
  1004                     {
       
  1005                         CyAsHalPrintMessage("******* ERROR SEND SET STALL REQUEST - %d\n", ret) ;
       
  1006                     }
       
  1007                 }
       
  1008             }
       
  1009             break;
       
  1010         }
       
  1011     }
       
  1012     else if ((data[0] & CY_CH9_SETUP_TYPE_MASK) == CY_CH9_SETUP_CLASS_REQUEST)
       
  1013     {
       
  1014         /*
       
  1015         * Handle class requests other than Mass Storage
       
  1016         */
       
  1017         ret = CyAsUsbSetStall(h, 0, StallCallback, 0) ;
       
  1018         CyAsHalPrintMessage("Sending stall request\n") ;
       
  1019         if (ret != CY_AS_ERROR_SUCCESS)
       
  1020             CyAsHalPrintMessage("**** cannot set stall state on EP 0\n") ;
       
  1021     }
       
  1022     else
       
  1023     {
       
  1024         static char buf[1024] ;
       
  1025 
       
  1026         if ((data[0] & 0x80) == 0)
       
  1027         {
       
  1028             if (reqlen != 0)
       
  1029             {
       
  1030 				CyAsHalPrintMessage("OUT setup request with additional data\n") ;
       
  1031                 /* This is an OUT setup request, with additional data to follow */
       
  1032                 /*ret = CyAsUsbReadDataAsync(h, 0, CyFalse, reqlen, buf, EP0DataCallback) ;
       
  1033 				if(ret != CY_AS_ERROR_SUCCESS && ret != CY_AS_ERROR_ASYNC_PENDING)
       
  1034 				{
       
  1035 					CyAsHalPrintMessage("CyAsMtpApp: CyAsUsbReadDataAsync Failed. Reason code: %d\n",ret) ;
       
  1036 				}*/
       
  1037             }
       
  1038             else
       
  1039             {
       
  1040                 CyAsHalPrintMessage("Call setnak\n") ;
       
  1041                 ret = CyAsUsbSetNak(h, 3, StallCallback, 1) ;
       
  1042                 if (ret != CY_AS_ERROR_SUCCESS)
       
  1043                 {
       
  1044                     CyAsHalPrintMessage("Error in CyAsUsbSetNak - %d\n", ret) ;
       
  1045                 }
       
  1046             }
       
  1047         }
       
  1048         else
       
  1049         {
       
  1050             if (reqlen != 0)
       
  1051             {
       
  1052                 /*
       
  1053                 * This is an unknown setup packet, probably some type of generated packet or a class
       
  1054                 * packet we do not understand.  We just send back some data.
       
  1055                 */
       
  1056                 CyAsHalMemSet(buf, 0x44, sizeof(buf)) ;
       
  1057                 ret = SendSetupData(h, reqlen, reqlen, buf) ;
       
  1058                 if (ret != CY_AS_ERROR_SUCCESS)
       
  1059                 {
       
  1060                     CyAsHalPrintMessage("Error ending setup data in response to unknown packet\n") ;
       
  1061                 }
       
  1062                 else
       
  1063                 {
       
  1064                     CyAsHalPrintMessage("Sent setup data associated with the unknown setup packet\n") ;
       
  1065                 }
       
  1066             }
       
  1067             else
       
  1068             {
       
  1069                 CyAsHalPrintMessage("Call setnak\n") ;
       
  1070                 ret = CyAsUsbSetNak(h, 3, StallCallback, 1) ;
       
  1071                 if (ret != CY_AS_ERROR_SUCCESS)
       
  1072                 {
       
  1073                     CyAsHalPrintMessage("Error in CyAsUsbSetNak - %d\n", ret) ;
       
  1074                 }
       
  1075             }
       
  1076         }
       
  1077     }
       
  1078 }
       
  1079 
       
  1080 static void
       
  1081 MyUsbEventCallbackMS(CyAsDeviceHandle h, CyAsUsbEvent ev, void *evdata)
       
  1082 {
       
  1083     CyAsHalPrintMessage("------------------------------ IN -------------------------------------\n") ;
       
  1084     switch(ev)
       
  1085     {
       
  1086     case CyAsEventUsbSuspend:
       
  1087         CyAsHalPrintMessage("CyAsEventUsbSuspend received\n") ;
       
  1088         break ;
       
  1089     case CyAsEventUsbResume:
       
  1090 		{
       
  1091 			/*CyAsReturnStatus_t ret = CY_AS_ERROR_SUCCESS;*/
       
  1092 			CyAsHalPrintMessage("CyAsEventUsbResume received\n") ;
       
  1093 			/*ret = CyAsUsbReadDataAsync(g_pAstDevice->astDevHandle, CY_AS_MTP_BULK_OUT_EP,
       
  1094 						CyFalse, 512, g_pAstDevice->astEPBuf,
       
  1095 						TmtpReadCallback) ;
       
  1096 
       
  1097 			if(ret != CY_AS_ERROR_SUCCESS && ret != CY_AS_ERROR_ASYNC_PENDING)
       
  1098 			{
       
  1099 				CyAsHalPrintMessage("CyAsMtpApp: CyAsUsbReadDataAsync Failed. Reason code: %d\n",ret) ;
       
  1100 				return ;
       
  1101 			}*/
       
  1102 		}
       
  1103         break ;
       
  1104     case CyAsEventUsbReset:
       
  1105 #ifdef DEBUG_ZERO
       
  1106 	desc_p = (CyCh9ConfigurationDesc *)&ConfigFSDesc ;
       
  1107         other_p = (CyCh9ConfigurationDesc *)&ConfigHSDesc ;
       
  1108 #else
       
  1109         desc_p = (CyCh9ConfigurationDesc *)&ZeroDesc ;
       
  1110         other_p = (CyCh9ConfigurationDesc *)&ZeroDesc ;
       
  1111 #endif
       
  1112         CyAsHalPrintMessage("CyAsEventUsbReset received\n") ;
       
  1113         break ;
       
  1114     case CyAsEventUsbSpeedChange:
       
  1115 
       
  1116 #ifdef DEBUG_ZERO
       
  1117 	desc_p = (CyCh9ConfigurationDesc *)&ConfigHSDesc ;
       
  1118         other_p = (CyCh9ConfigurationDesc *)&ConfigFSDesc ;
       
  1119 #else
       
  1120         desc_p = (CyCh9ConfigurationDesc *)&ZeroDesc ;
       
  1121         other_p = (CyCh9ConfigurationDesc *)&ZeroDesc ;
       
  1122 #endif
       
  1123         CyAsHalPrintMessage("CyAsEventUsbSpeedChange received\n") ;
       
  1124         break ;
       
  1125     case CyAsEventUsbSetConfig:
       
  1126         CyAsHalPrintMessage("CyAsEventUsbSetConfig received\n") ;
       
  1127         gSetConfig = CyTrue ;
       
  1128         break ;
       
  1129     case CyAsEventUsbSetupPacket:
       
  1130         PrintData("CyAsEventUsbSetupPacket received: ", (uint8_t*)evdata, 8) ;
       
  1131         ProcessSetupPacketRequest(h, (uint8_t *)evdata) ;
       
  1132         break ;
       
  1133     case CyAsEventUsbStatusPacket:
       
  1134         CyAsHalPrintMessage("CyAsEventUsbStatusPacket received\n") ;
       
  1135         break ;
       
  1136     case CyAsEventUsbInquiryBefore:
       
  1137         CyAsHalPrintMessage("CyAsEventUsbInquiryBefore received\n") ;
       
  1138         {
       
  1139             CyAsUsbInquiryData *data = (CyAsUsbInquiryData *)evdata ;
       
  1140             data->updated = CyTrue ;
       
  1141             data = data ;
       
  1142         }
       
  1143         break ;
       
  1144     case CyAsEventUsbInquiryAfter:
       
  1145         CyAsHalPrintMessage("CyAsEventUsbInquiryAfter received\n") ;
       
  1146         break ;
       
  1147     case CyAsEventUsbStartStop:
       
  1148         CyAsHalPrintMessage("CyAsEventUsbStartStop received\n") ;
       
  1149         {
       
  1150             CyAsUsbStartStopData *data = (CyAsUsbStartStopData *)evdata ;
       
  1151             data = data ;
       
  1152         }
       
  1153         break ;
       
  1154     default:
       
  1155         break;
       
  1156     }
       
  1157     CyAsHalPrintMessage("------------------------------ OUT -------------------------------------\n") ;
       
  1158 }
       
  1159 
       
  1160 
       
  1161 int CyAsSymbianStorageTest(const char *pgm, CyAsDeviceHandle h, CyAsHalDeviceTag tag)
       
  1162 {
       
  1163     CyAsReturnStatus_t ret ;
       
  1164 	uint32_t count = 0 ;
       
  1165 	char *media_name = "SD";
       
  1166 
       
  1167 	g_tag = tag ;
       
  1168 	memset(g_pAstDevice,0, sizeof(g_AstDevice));
       
  1169 	g_pAstDevice->astHalTag = tag ;
       
  1170 	g_pAstDevice->astDevHandle = h ;
       
  1171 
       
  1172     /*
       
  1173     * Give a delay to allow any equipment to be ready (e.g. CATC)
       
  1174     */
       
  1175     /*CyAsHalPrintMessage("Press enter to begin USB operation (%s): ", "P Port Enumeration") ;
       
  1176     fgets(buffer, sizeof(buffer), stdin) ;*/
       
  1177 
       
  1178     CyAsHalPrintMessage("*** CyAsStorageStart...\n") ;
       
  1179     ret = CyAsStorageStart(h, 0, 0) ;
       
  1180     if (ret != CY_AS_ERROR_SUCCESS)
       
  1181     {
       
  1182         CyAsHalPrintMessage("%s: CyAsStorageStart returned error code %d\n", pgm, ret) ;
       
  1183         return 0 ;
       
  1184     }
       
  1185 
       
  1186 	CyAsHalPrintMessage("*** CyAsStorageQueryMedia...\n") ;
       
  1187 	ret = CyAsStorageQueryMedia(h, CyAsMediaSDFlash, &count, 0, 0) ;
       
  1188 	if (ret != CY_AS_ERROR_SUCCESS)
       
  1189 	{
       
  1190 	  CyAsHalPrintMessage("%s: Cannot query %s device count - Reason code %d\n", pgm, media_name, ret) ;
       
  1191 	  return 0 ;
       
  1192 	}
       
  1193 	CyAsHalPrintMessage("%d %s device(s) found\n", count, media_name) ;
       
  1194 
       
  1195 	CyAsHalPrintMessage("*** CyAsStorageClaim...\n") ;
       
  1196 	ret = CyAsStorageClaim(h, CyAsBus_1, 0, 0, 0) ;
       
  1197 	if (ret != CY_AS_ERROR_SUCCESS)
       
  1198 	{
       
  1199 	  CyAsHalPrintMessage("%s: Cannot claim %s media - Reason code %d\n", pgm, media_name, ret) ;
       
  1200 	  return 0;
       
  1201 	}
       
  1202 
       
  1203 	/* We know that there is only one */
       
  1204 	g_pAstDevice->dev_data.bus = CyAsBus_1 ;
       
  1205 	g_pAstDevice->dev_data.device = 0 ;
       
  1206 	ret = CyAsStorageQueryDevice(h, &(g_pAstDevice->dev_data), 0, 0) ;
       
  1207 	if (ret != CY_AS_ERROR_SUCCESS)
       
  1208 	{
       
  1209 	  CyAsHalPrintMessage("%s: Cannot query %s device - Reason code %d\n", pgm, media_name, ret) ;
       
  1210 	  return 0 ;
       
  1211 	}
       
  1212 	CyAsHalPrintMessage("blocksize %d, %d units found\n",
       
  1213 		g_pAstDevice->dev_data.desc_p.block_size, g_pAstDevice->dev_data.desc_p.number_units) ;
       
  1214 
       
  1215 	g_pAstDevice->unit_data.bus = CyAsBus_1 ;
       
  1216 	g_pAstDevice->unit_data.device = 0 ;
       
  1217 	g_pAstDevice->unit_data.unit = 0 ;
       
  1218 	/* We know that there is only one */
       
  1219 	ret = CyAsStorageQueryUnit(h, &(g_pAstDevice->unit_data), 0, 0) ;
       
  1220 	if (ret != CY_AS_ERROR_SUCCESS)
       
  1221 	{
       
  1222 	  CyAsHalPrintMessage("%s: Cannot query %s device unit - Reason code %d\n", pgm, media_name, ret) ;
       
  1223 	  return 0 ;
       
  1224 	}
       
  1225 	CyAsHalPrintMessage("blocksize %d, %d Block(s) found\n",
       
  1226 		g_pAstDevice->unit_data.desc_p.block_size, g_pAstDevice->unit_data.desc_p.unit_size) ;
       
  1227 
       
  1228 
       
  1229 	{
       
  1230 		int i = 0 , j;
       
  1231 		char buf[512] = {0} ;
       
  1232 		char expBuf[512] = {0} ;
       
  1233 		memset(buf, 0xa5, 512);
       
  1234 		memset(expBuf, 0xa5, 512);
       
  1235 
       
  1236 		CyAsHalPrintMessage("Read SD card\n");
       
  1237 		ret = CyAsStorageRead(	h, 1, 0, 0, 0, buf, 1);
       
  1238 		if (ret != CY_AS_ERROR_SUCCESS)
       
  1239 		{
       
  1240 			CyAsHalPrintMessage("%s: Cannot read first sector of SD card\n", pgm) ;
       
  1241 			return 0 ;
       
  1242 		}
       
  1243 		CyAsHalPrintMessage("Read Result\n");
       
  1244 		for ( i = 0 ; i < 512; i++ )
       
  1245 		{
       
  1246 			if ( (i != 0 ) && (i % 16 == 0))
       
  1247 			{
       
  1248 
       
  1249 				CyAsHalPrintMessage("\t");
       
  1250 				for ( j = 0 ; j < 16 ; j++ )
       
  1251 				{
       
  1252 					if ( (buf[i+j] > 32) && (buf[i+j] < 127))
       
  1253 					{
       
  1254 						CyAsHalPrintMessage("%c",buf[i+j]);
       
  1255 					}
       
  1256 					else
       
  1257 					{
       
  1258 						CyAsHalPrintMessage(".");
       
  1259 					}
       
  1260 				}
       
  1261 				CyAsHalPrintMessage("\n");
       
  1262 			}
       
  1263 			CyAsHalPrintMessage("%02x ",buf[i]);
       
  1264 		}
       
  1265 		CyAsHalPrintMessage("\n");
       
  1266 
       
  1267 		ret = CyAsStorageWrite(h, 1, 0, 0, 0, expBuf, 1);
       
  1268 		if ( ret != CY_AS_ERROR_SUCCESS )
       
  1269 		{
       
  1270 			CyAsHalPrintMessage("%s: Cannot write to first sector of SD card\n",pgm) ;
       
  1271 			return 0 ;
       
  1272 		}
       
  1273 
       
  1274 		memset(buf, 0xa5, 512);
       
  1275 		ret = CyAsStorageRead(h, 1, 0, 0, 0, buf, 1);
       
  1276 		if (ret != CY_AS_ERROR_SUCCESS)
       
  1277 		{
       
  1278 			CyAsHalPrintMessage("%s: Cannot read first sector of SD card\n",pgm) ;
       
  1279 			return 0 ;
       
  1280 		}
       
  1281 
       
  1282 		for ( i = 0 ; i < 512; i++ )
       
  1283 		{
       
  1284 			if ( buf[i] != expBuf[i] )
       
  1285 			{
       
  1286 
       
  1287 				CyAsHalPrintMessage("EXP[%d]: 0x%02x",i,expBuf[i]);
       
  1288 				CyAsHalPrintMessage("ACT[%d]: 0x%02x",i,buf[i]);
       
  1289 			}
       
  1290 		}
       
  1291 	}
       
  1292 
       
  1293 
       
  1294 	return 1 ;
       
  1295 }
       
  1296 
       
  1297 int CyAsAPIGetHandle( CyAsDeviceHandle h, CyAsHalDeviceTag tag)
       
  1298 {
       
  1299 	g_tag = tag ;
       
  1300 	memset(g_pAstDevice,0, sizeof(g_AstDevice));
       
  1301 	g_pAstDevice->astHalTag = tag ;
       
  1302 	g_pAstDevice->astDevHandle = h ;
       
  1303 
       
  1304 	return 1 ;
       
  1305 }
       
  1306