Added support for v4.2 boards
[SCSI2SD.git] / software / SCSI2SD / v4 / SCSI2SD.cydsn / Generated_Source / PSoC5 / USBFS_std.c
1 /*******************************************************************************
2 * File Name: USBFS_std.c
3 * Version 2.60
4 *
5 * Description:
6 *  USB Standard request handler.
7 *
8 * Note:
9 *
10 ********************************************************************************
11 * Copyright 2008-2013, Cypress Semiconductor Corporation.  All rights reserved.
12 * You may use this file only in accordance with the license, terms, conditions,
13 * disclaimers, and limitations in the end user license agreement accompanying
14 * the software package with which this file was provided.
15 *******************************************************************************/
16
17 #include "USBFS.h"
18 #include "USBFS_cdc.h"
19 #include "USBFS_pvt.h"
20 #if defined(USBFS_ENABLE_MIDI_STREAMING) 
21     #include "USBFS_midi.h"
22 #endif /* End USBFS_ENABLE_MIDI_STREAMING*/
23
24
25 /***************************************
26 *   Static data allocation
27 ***************************************/
28
29 #if defined(USBFS_ENABLE_FWSN_STRING)
30     static volatile uint8 *USBFS_fwSerialNumberStringDescriptor;
31     static volatile uint8 USBFS_snStringConfirm = USBFS_FALSE;
32 #endif  /* USBFS_ENABLE_FWSN_STRING */
33
34 #if defined(USBFS_ENABLE_FWSN_STRING)
35
36
37     /*******************************************************************************
38     * Function Name: USBFS_SerialNumString
39     ********************************************************************************
40     *
41     * Summary:
42     *  Application firmware may supply the source of the USB device descriptors
43     *  serial number string during runtime.
44     *
45     * Parameters:
46     *  snString:  pointer to string.
47     *
48     * Return:
49     *  None.
50     *
51     * Reentrant:
52     *  No.
53     *
54     *******************************************************************************/
55     void  USBFS_SerialNumString(uint8 snString[]) 
56     {
57         USBFS_snStringConfirm = USBFS_FALSE;
58         if(snString != NULL)
59         {
60             USBFS_fwSerialNumberStringDescriptor = snString;
61             /* Check descriptor validation */
62             if( (snString[0u] > 1u ) && (snString[1u] == USBFS_DESCR_STRING) )
63             {
64                 USBFS_snStringConfirm = USBFS_TRUE;
65             }
66         }
67     }
68
69 #endif  /* USBFS_ENABLE_FWSN_STRING */
70
71
72 /*******************************************************************************
73 * Function Name: USBFS_HandleStandardRqst
74 ********************************************************************************
75 *
76 * Summary:
77 *  This Routine dispatches standard requests
78 *
79 * Parameters:
80 *  None.
81 *
82 * Return:
83 *  TRUE if request handled.
84 *
85 * Reentrant:
86 *  No.
87 *
88 *******************************************************************************/
89 uint8 USBFS_HandleStandardRqst(void) 
90 {
91     uint8 requestHandled = USBFS_FALSE;
92     uint8 interfaceNumber;
93     #if defined(USBFS_ENABLE_STRINGS)
94         volatile uint8 *pStr = 0u;
95         #if defined(USBFS_ENABLE_DESCRIPTOR_STRINGS)
96             uint8 nStr;
97             uint8 descrLength;
98         #endif /* USBFS_ENABLE_DESCRIPTOR_STRINGS */
99     #endif /* USBFS_ENABLE_STRINGS */
100     static volatile uint8 USBFS_tBuffer[USBFS_STATUS_LENGTH_MAX];
101     const T_USBFS_LUT CYCODE *pTmp;
102     USBFS_currentTD.count = 0u;
103
104     if ((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_DIR_MASK) == USBFS_RQST_DIR_D2H)
105     {
106         /* Control Read */
107         switch (CY_GET_REG8(USBFS_bRequest))
108         {
109             case USBFS_GET_DESCRIPTOR:
110                 if (CY_GET_REG8(USBFS_wValueHi) == USBFS_DESCR_DEVICE)
111                 {
112                     pTmp = USBFS_GetDeviceTablePtr();
113                     USBFS_currentTD.pData = (volatile uint8 *)pTmp->p_list;
114                     USBFS_currentTD.count = USBFS_DEVICE_DESCR_LENGTH;
115                     requestHandled  = USBFS_InitControlRead();
116                 }
117                 else if (CY_GET_REG8(USBFS_wValueHi) == USBFS_DESCR_CONFIG)
118                 {
119                     pTmp = USBFS_GetConfigTablePtr(CY_GET_REG8(USBFS_wValueLo));
120                     USBFS_currentTD.pData = (volatile uint8 *)pTmp->p_list;
121                     USBFS_currentTD.count = ((uint16)(USBFS_currentTD.pData)[ \
122                                       USBFS_CONFIG_DESCR_TOTAL_LENGTH_HI] << 8u) | \
123                                      (USBFS_currentTD.pData)[USBFS_CONFIG_DESCR_TOTAL_LENGTH_LOW];
124                     requestHandled  = USBFS_InitControlRead();
125                 }
126                 #if defined(USBFS_ENABLE_STRINGS)
127                 else if (CY_GET_REG8(USBFS_wValueHi) == USBFS_DESCR_STRING)
128                 {
129                     /* Descriptor Strings*/
130                     #if defined(USBFS_ENABLE_DESCRIPTOR_STRINGS)
131                         nStr = 0u;
132                         pStr = (volatile uint8 *)&USBFS_STRING_DESCRIPTORS[0u];
133                         while ( (CY_GET_REG8(USBFS_wValueLo) > nStr) && (*pStr != 0u) )
134                         {
135                             /* Read descriptor length from 1st byte */
136                             descrLength = *pStr;
137                             /* Move to next string descriptor */
138                             pStr = &pStr[descrLength];
139                             nStr++;
140                         }
141                     #endif /* End USBFS_ENABLE_DESCRIPTOR_STRINGS */
142                     /* Microsoft OS String*/
143                     #if defined(USBFS_ENABLE_MSOS_STRING)
144                         if( CY_GET_REG8(USBFS_wValueLo) == USBFS_STRING_MSOS )
145                         {
146                             pStr = (volatile uint8 *)&USBFS_MSOS_DESCRIPTOR[0u];
147                         }
148                     #endif /* End USBFS_ENABLE_MSOS_STRING*/
149                     /* SN string */
150                     #if defined(USBFS_ENABLE_SN_STRING)
151                         if( (CY_GET_REG8(USBFS_wValueLo) != 0u) &&
152                             (CY_GET_REG8(USBFS_wValueLo) ==
153                             USBFS_DEVICE0_DESCR[USBFS_DEVICE_DESCR_SN_SHIFT]) )
154                         {
155                             pStr = (volatile uint8 *)&USBFS_SN_STRING_DESCRIPTOR[0u];
156                             #if defined(USBFS_ENABLE_FWSN_STRING)
157                                 if(USBFS_snStringConfirm != USBFS_FALSE)
158                                 {
159                                     pStr = USBFS_fwSerialNumberStringDescriptor;
160                                 }
161                             #endif  /* USBFS_ENABLE_FWSN_STRING */
162                             #if defined(USBFS_ENABLE_IDSN_STRING)
163                                 /* Read DIE ID and generate string descriptor in RAM */
164                                 USBFS_ReadDieID(USBFS_idSerialNumberStringDescriptor);
165                                 pStr = USBFS_idSerialNumberStringDescriptor;
166                             #endif    /* End USBFS_ENABLE_IDSN_STRING */
167                         }
168                     #endif    /* End USBFS_ENABLE_SN_STRING */
169                     if (*pStr != 0u)
170                     {
171                         USBFS_currentTD.count = *pStr;
172                         USBFS_currentTD.pData = pStr;
173                         requestHandled  = USBFS_InitControlRead();
174                     }
175                 }
176                 #endif /* End USBFS_ENABLE_STRINGS */
177                 else
178                 {
179                     requestHandled = USBFS_DispatchClassRqst();
180                 }
181                 break;
182             case USBFS_GET_STATUS:
183                 switch ((CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_RCPT_MASK))
184                 {
185                     case USBFS_RQST_RCPT_EP:
186                         USBFS_currentTD.count = USBFS_EP_STATUS_LENGTH;
187                         USBFS_tBuffer[0u] = USBFS_EP[ \
188                                         CY_GET_REG8(USBFS_wIndexLo) & USBFS_DIR_UNUSED].hwEpState;
189                         USBFS_tBuffer[1u] = 0u;
190                         USBFS_currentTD.pData = &USBFS_tBuffer[0u];
191                         requestHandled  = USBFS_InitControlRead();
192                         break;
193                     case USBFS_RQST_RCPT_DEV:
194                         USBFS_currentTD.count = USBFS_DEVICE_STATUS_LENGTH;
195                         USBFS_tBuffer[0u] = USBFS_deviceStatus;
196                         USBFS_tBuffer[1u] = 0u;
197                         USBFS_currentTD.pData = &USBFS_tBuffer[0u];
198                         requestHandled  = USBFS_InitControlRead();
199                         break;
200                     default:    /* requestHandled is initialized as FALSE by default */
201                         break;
202                 }
203                 break;
204             case USBFS_GET_CONFIGURATION:
205                 USBFS_currentTD.count = 1u;
206                 USBFS_currentTD.pData = (volatile uint8 *)&USBFS_configuration;
207                 requestHandled  = USBFS_InitControlRead();
208                 break;
209             case USBFS_GET_INTERFACE:
210                 USBFS_currentTD.count = 1u;
211                 USBFS_currentTD.pData = (volatile uint8 *)&USBFS_interfaceSetting[ \
212                                                                             CY_GET_REG8(USBFS_wIndexLo)];
213                 requestHandled  = USBFS_InitControlRead();
214                 break;
215             default: /* requestHandled is initialized as FALSE by default */
216                 break;
217         }
218     }
219     else {
220         /* Control Write */
221         switch (CY_GET_REG8(USBFS_bRequest))
222         {
223             case USBFS_SET_ADDRESS:
224                 USBFS_deviceAddress = CY_GET_REG8(USBFS_wValueLo);
225                 requestHandled = USBFS_InitNoDataControlTransfer();
226                 break;
227             case USBFS_SET_CONFIGURATION:
228                 USBFS_configuration = CY_GET_REG8(USBFS_wValueLo);
229                 USBFS_configurationChanged = USBFS_TRUE;
230                 USBFS_Config(USBFS_TRUE);
231                 requestHandled = USBFS_InitNoDataControlTransfer();
232                 break;
233             case USBFS_SET_INTERFACE:
234                 if (USBFS_ValidateAlternateSetting() != 0u)
235                 {
236                     interfaceNumber = CY_GET_REG8(USBFS_wIndexLo);
237                     USBFS_interfaceNumber = interfaceNumber;
238                     USBFS_configurationChanged = USBFS_TRUE;
239                     #if ((USBFS_EP_MA == USBFS__MA_DYNAMIC) && \
240                          (USBFS_EP_MM == USBFS__EP_MANUAL) )
241                         USBFS_Config(USBFS_FALSE);
242                     #else
243                         USBFS_ConfigAltChanged();
244                     #endif /* End (USBFS_EP_MA == USBFS__MA_DYNAMIC) */
245                     /* Update handled Alt setting changes status */
246                     USBFS_interfaceSetting_last[interfaceNumber] =
247                          USBFS_interfaceSetting[interfaceNumber];
248                     requestHandled = USBFS_InitNoDataControlTransfer();
249                 }
250                 break;
251             case USBFS_CLEAR_FEATURE:
252                 switch (CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_RCPT_MASK)
253                 {
254                     case USBFS_RQST_RCPT_EP:
255                         if (CY_GET_REG8(USBFS_wValueLo) == USBFS_ENDPOINT_HALT)
256                         {
257                             requestHandled = USBFS_ClearEndpointHalt();
258                         }
259                         break;
260                     case USBFS_RQST_RCPT_DEV:
261                         /* Clear device REMOTE_WAKEUP */
262                         if (CY_GET_REG8(USBFS_wValueLo) == USBFS_DEVICE_REMOTE_WAKEUP)
263                         {
264                             USBFS_deviceStatus &= (uint8)~USBFS_DEVICE_STATUS_REMOTE_WAKEUP;
265                             requestHandled = USBFS_InitNoDataControlTransfer();
266                         }
267                         break;
268                     case USBFS_RQST_RCPT_IFC:
269                         /* Validate interfaceNumber */
270                         if (CY_GET_REG8(USBFS_wIndexLo) < USBFS_MAX_INTERFACES_NUMBER)
271                         {
272                             USBFS_interfaceStatus[CY_GET_REG8(USBFS_wIndexLo)] &=
273                                                                 (uint8)~(CY_GET_REG8(USBFS_wValueLo));
274                             requestHandled = USBFS_InitNoDataControlTransfer();
275                         }
276                         break;
277                     default:    /* requestHandled is initialized as FALSE by default */
278                         break;
279                 }
280                 break;
281             case USBFS_SET_FEATURE:
282                 switch (CY_GET_REG8(USBFS_bmRequestType) & USBFS_RQST_RCPT_MASK)
283                 {
284                     case USBFS_RQST_RCPT_EP:
285                         if (CY_GET_REG8(USBFS_wValueLo) == USBFS_ENDPOINT_HALT)
286                         {
287                             requestHandled = USBFS_SetEndpointHalt();
288                         }
289                         break;
290                     case USBFS_RQST_RCPT_DEV:
291                         /* Set device REMOTE_WAKEUP */
292                         if (CY_GET_REG8(USBFS_wValueLo) == USBFS_DEVICE_REMOTE_WAKEUP)
293                         {
294                             USBFS_deviceStatus |= USBFS_DEVICE_STATUS_REMOTE_WAKEUP;
295                             requestHandled = USBFS_InitNoDataControlTransfer();
296                         }
297                         break;
298                     case USBFS_RQST_RCPT_IFC:
299                         /* Validate interfaceNumber */
300                         if (CY_GET_REG8(USBFS_wIndexLo) < USBFS_MAX_INTERFACES_NUMBER)
301                         {
302                             USBFS_interfaceStatus[CY_GET_REG8(USBFS_wIndexLo)] &=
303                                                                 (uint8)~(CY_GET_REG8(USBFS_wValueLo));
304                             requestHandled = USBFS_InitNoDataControlTransfer();
305                         }
306                         break;
307                     default:    /* requestHandled is initialized as FALSE by default */
308                         break;
309                 }
310                 break;
311             default:    /* requestHandled is initialized as FALSE by default */
312                 break;
313         }
314     }
315     return(requestHandled);
316 }
317
318
319 #if defined(USBFS_ENABLE_IDSN_STRING)
320
321     /***************************************************************************
322     * Function Name: USBFS_ReadDieID
323     ****************************************************************************
324     *
325     * Summary:
326     *  This routine read Die ID and generate Serial Number string descriptor.
327     *
328     * Parameters:
329     *  descr:  pointer on string descriptor.
330     *
331     * Return:
332     *  None.
333     *
334     * Reentrant:
335     *  No.
336     *
337     ***************************************************************************/
338     void USBFS_ReadDieID(uint8 descr[]) 
339     {
340         uint8 i;
341         uint8 j = 0u;
342         uint8 value;
343         const char8 CYCODE hex[16u] = "0123456789ABCDEF";
344
345
346         /* Check descriptor validation */
347         if( descr != NULL)
348         {
349             descr[0u] = USBFS_IDSN_DESCR_LENGTH;
350             descr[1u] = USBFS_DESCR_STRING;
351
352             /* fill descriptor */
353             for(i = 2u; i < USBFS_IDSN_DESCR_LENGTH; i += 4u)
354             {
355                 value = CY_GET_XTND_REG8((void CYFAR *)(USBFS_DIE_ID + j));
356                 j++;
357                 descr[i] = (uint8)hex[value >> 4u];
358                 descr[i + 2u] = (uint8)hex[value & 0x0Fu];
359             }
360         }
361     }
362
363 #endif /* End USBFS_ENABLE_IDSN_STRING */
364
365
366 /*******************************************************************************
367 * Function Name: USBFS_ConfigReg
368 ********************************************************************************
369 *
370 * Summary:
371 *  This routine configures hardware registers from the variables.
372 *  It is called from USBFS_Config() function and from RestoreConfig
373 *  after Wakeup.
374 *
375 * Parameters:
376 *  None.
377 *
378 * Return:
379 *  None.
380 *
381 *******************************************************************************/
382 void USBFS_ConfigReg(void) 
383 {
384     uint8 ep;
385     uint8 i;
386     #if(USBFS_EP_MM == USBFS__EP_DMAAUTO)
387         uint8 ep_type = 0u;
388     #endif /* End USBFS_EP_MM == USBFS__EP_DMAAUTO */
389
390     /* Set the endpoint buffer addresses */
391     ep = USBFS_EP1;
392     for (i = 0u; i < 0x80u; i+= 0x10u)
393     {
394         CY_SET_REG8((reg8 *)(USBFS_ARB_EP1_CFG_IND + i), USBFS_ARB_EPX_CFG_CRC_BYPASS |
395                                                           USBFS_ARB_EPX_CFG_RESET);
396
397         #if(USBFS_EP_MM != USBFS__EP_MANUAL)
398             /* Enable all Arbiter EP Interrupts : err, buf under, buf over, dma gnt(mode2 only), in buf full */
399             CY_SET_REG8((reg8 *)(USBFS_ARB_EP1_INT_EN_IND + i), USBFS_ARB_EPX_INT_MASK);
400         #endif   /* End USBFS_EP_MM != USBFS__EP_MANUAL */
401
402         if(USBFS_EP[ep].epMode != USBFS_MODE_DISABLE)
403         {
404             if((USBFS_EP[ep].addr & USBFS_DIR_IN) != 0u )
405             {
406                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + i), USBFS_MODE_NAK_IN);
407             }
408             else
409             {
410                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + i), USBFS_MODE_NAK_OUT);
411                 /* Prepare EP type mask for automatic memory allocation */
412                 #if(USBFS_EP_MM == USBFS__EP_DMAAUTO)
413                     ep_type |= (uint8)(0x01u << (ep - USBFS_EP1));
414                 #endif /* End USBFS_EP_MM == USBFS__EP_DMAAUTO */
415             }
416         }
417         else
418         {
419             CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + i), USBFS_MODE_STALL_DATA_EP);
420         }
421
422         #if(USBFS_EP_MM != USBFS__EP_DMAAUTO)
423             CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CNT0_IND + i),   USBFS_EP[ep].bufferSize >> 8u);
424             CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CNT1_IND + i),   USBFS_EP[ep].bufferSize & 0xFFu);
425
426             CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_RA_IND + i),     USBFS_EP[ep].buffOffset & 0xFFu);
427             CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_RA_MSB_IND + i), USBFS_EP[ep].buffOffset >> 8u);
428             CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_WA_IND + i),     USBFS_EP[ep].buffOffset & 0xFFu);
429             CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_WA_MSB_IND + i), USBFS_EP[ep].buffOffset >> 8u);
430         #endif /* End USBFS_EP_MM != USBFS__EP_DMAAUTO */
431
432         ep++;
433     }
434
435     #if(USBFS_EP_MM == USBFS__EP_DMAAUTO)
436          /* BUF_SIZE depend on DMA_THRESS value: 55-32 bytes  44-16 bytes 33-8 bytes 22-4 bytes 11-2 bytes */
437         USBFS_BUF_SIZE_REG = USBFS_DMA_BUF_SIZE;
438         USBFS_DMA_THRES_REG = USBFS_DMA_BYTES_PER_BURST;   /* DMA burst threshold */
439         USBFS_DMA_THRES_MSB_REG = 0u;
440         USBFS_EP_ACTIVE_REG = USBFS_ARB_INT_MASK;
441         USBFS_EP_TYPE_REG = ep_type;
442         /* Cfg_cmp bit set to 1 once configuration is complete. */
443         USBFS_ARB_CFG_REG = USBFS_ARB_CFG_AUTO_DMA | USBFS_ARB_CFG_AUTO_MEM |
444                                        USBFS_ARB_CFG_CFG_CPM;
445         /* Cfg_cmp bit set to 0 during configuration of PFSUSB Registers. */
446         USBFS_ARB_CFG_REG = USBFS_ARB_CFG_AUTO_DMA | USBFS_ARB_CFG_AUTO_MEM;
447     #endif /* End USBFS_EP_MM == USBFS__EP_DMAAUTO */
448
449     CY_SET_REG8(USBFS_SIE_EP_INT_EN_PTR, 0xFFu);
450 }
451
452
453 /*******************************************************************************
454 * Function Name: USBFS_Config
455 ********************************************************************************
456 *
457 * Summary:
458 *  This routine configures endpoints for the entire configuration by scanning
459 *  the configuration descriptor.
460 *
461 * Parameters:
462 *  clearAltSetting: It configures the bAlternateSetting 0 for each interface.
463 *
464 * Return:
465 *  None.
466 *
467 * USBFS_interfaceClass - Initialized class array for each interface.
468 *   It is used for handling Class specific requests depend on interface class.
469 *   Different classes in multiple Alternate settings does not supported.
470 *
471 * Reentrant:
472 *  No.
473 *
474 *******************************************************************************/
475 void USBFS_Config(uint8 clearAltSetting) 
476 {
477     uint8 ep;
478     uint8 cur_ep;
479     uint8 i;
480     uint8 ep_type;
481     const uint8 *pDescr;
482     #if(USBFS_EP_MM != USBFS__EP_DMAAUTO)
483         uint16 buffCount = 0u;
484     #endif /* End USBFS_EP_MM != USBFS__EP_DMAAUTO */
485
486     const T_USBFS_LUT CYCODE *pTmp;
487     const T_USBFS_EP_SETTINGS_BLOCK CYCODE *pEP;
488
489     /* Clear all of the endpoints */
490     for (ep = 0u; ep < USBFS_MAX_EP; ep++)
491     {
492         USBFS_EP[ep].attrib = 0u;
493         USBFS_EP[ep].hwEpState = 0u;
494         USBFS_EP[ep].apiEpState = USBFS_NO_EVENT_PENDING;
495         USBFS_EP[ep].epToggle = 0u;
496         USBFS_EP[ep].epMode = USBFS_MODE_DISABLE;
497         USBFS_EP[ep].bufferSize = 0u;
498         USBFS_EP[ep].interface = 0u;
499
500     }
501
502     /* Clear Alternate settings for all interfaces */
503     if(clearAltSetting != 0u)
504     {
505         for (i = 0u; i < USBFS_MAX_INTERFACES_NUMBER; i++)
506         {
507             USBFS_interfaceSetting[i] = 0x00u;
508             USBFS_interfaceSetting_last[i] = 0x00u;
509         }
510     }
511
512     /* Init Endpoints and Device Status if configured */
513     if(USBFS_configuration > 0u)
514     {
515         pTmp = USBFS_GetConfigTablePtr(USBFS_configuration - 1u);
516         /* Set Power status for current configuration */
517         pDescr = (const uint8 *)pTmp->p_list;
518         if((pDescr[USBFS_CONFIG_DESCR_ATTRIB] & USBFS_CONFIG_DESCR_ATTRIB_SELF_POWERED) != 0u)
519         {
520             USBFS_deviceStatus |=  USBFS_DEVICE_STATUS_SELF_POWERED;
521         }
522         else
523         {
524             USBFS_deviceStatus &=  (uint8)~USBFS_DEVICE_STATUS_SELF_POWERED;
525         }
526         /* Move to next element */
527         pTmp = &pTmp[1u];
528         ep = pTmp->c;  /* For this table, c is the number of endpoints configurations  */
529
530         #if ((USBFS_EP_MA == USBFS__MA_DYNAMIC) && \
531              (USBFS_EP_MM == USBFS__EP_MANUAL) )
532             /* Configure for dynamic EP memory allocation */
533             /* p_list points the endpoint setting table. */
534             pEP = (T_USBFS_EP_SETTINGS_BLOCK *) pTmp->p_list;
535             for (i = 0u; i < ep; i++)
536             {
537                 /* Compare current Alternate setting with EP Alt*/
538                 if(USBFS_interfaceSetting[pEP->interface] == pEP->altSetting)
539                 {
540                     cur_ep = pEP->addr & USBFS_DIR_UNUSED;
541                     ep_type = pEP->attributes & USBFS_EP_TYPE_MASK;
542                     if (pEP->addr & USBFS_DIR_IN)
543                     {
544                         /* IN Endpoint */
545                         USBFS_EP[cur_ep].apiEpState = USBFS_EVENT_PENDING;
546                         USBFS_EP[cur_ep].epMode = (ep_type == USBFS_EP_TYPE_ISOC) ?
547                                                         USBFS_MODE_ISO_IN : USBFS_MODE_ACK_IN;
548                         #if defined(USBFS_ENABLE_CDC_CLASS)
549                             if(((pEP->bMisc == USBFS_CLASS_CDC_DATA) ||
550                                 (pEP->bMisc == USBFS_CLASS_CDC)) &&
551                                 (ep_type != USBFS_EP_TYPE_INT))
552                             {
553                                 USBFS_cdc_data_in_ep = cur_ep;
554                             }
555                         #endif  /* End USBFS_ENABLE_CDC_CLASS*/
556                         #if ( defined(USBFS_ENABLE_MIDI_STREAMING) && \
557                                              (USBFS_MIDI_IN_BUFF_SIZE > 0) )
558                             if((pEP->bMisc == USBFS_CLASS_AUDIO) &&
559                                (ep_type == USBFS_EP_TYPE_BULK))
560                             {
561                                 USBFS_midi_in_ep = cur_ep;
562                             }
563                         #endif  /* End USBFS_ENABLE_MIDI_STREAMING*/
564                     }
565                     else
566                     {
567                         /* OUT Endpoint */
568                         USBFS_EP[cur_ep].apiEpState = USBFS_NO_EVENT_PENDING;
569                         USBFS_EP[cur_ep].epMode = (ep_type == USBFS_EP_TYPE_ISOC) ?
570                                                     USBFS_MODE_ISO_OUT : USBFS_MODE_ACK_OUT;
571                         #if defined(USBFS_ENABLE_CDC_CLASS)
572                             if(((pEP->bMisc == USBFS_CLASS_CDC_DATA) ||
573                                 (pEP->bMisc == USBFS_CLASS_CDC)) &&
574                                 (ep_type != USBFS_EP_TYPE_INT))
575                             {
576                                 USBFS_cdc_data_out_ep = cur_ep;
577                             }
578                         #endif  /* End USBFS_ENABLE_CDC_CLASS*/
579                         #if ( defined(USBFS_ENABLE_MIDI_STREAMING) && \
580                                      (USBFS_MIDI_OUT_BUFF_SIZE > 0) )
581                             if((pEP->bMisc == USBFS_CLASS_AUDIO) &&
582                                (ep_type == USBFS_EP_TYPE_BULK))
583                             {
584                                 USBFS_midi_out_ep = cur_ep;
585                             }
586                         #endif  /* End USBFS_ENABLE_MIDI_STREAMING*/
587                     }
588                     USBFS_EP[cur_ep].bufferSize = pEP->bufferSize;
589                     USBFS_EP[cur_ep].addr = pEP->addr;
590                     USBFS_EP[cur_ep].attrib = pEP->attributes;
591                 }
592                 pEP = &pEP[1u];
593             }
594         #else /* Config for static EP memory allocation  */
595             for (i = USBFS_EP1; i < USBFS_MAX_EP; i++)
596             {
597                 /* p_list points the endpoint setting table. */
598                 pEP = (const T_USBFS_EP_SETTINGS_BLOCK CYCODE *) pTmp->p_list;
599                 /* Find max length for each EP and select it (length could be different in different Alt settings) */
600                 /* but other settings should be correct with regards to Interface alt Setting */
601                 for (cur_ep = 0u; cur_ep < ep; cur_ep++)
602                 {
603                     /* EP count is equal to EP # in table and we found larger EP length than have before*/
604                     if(i == (pEP->addr & USBFS_DIR_UNUSED))
605                     {
606                         if(USBFS_EP[i].bufferSize < pEP->bufferSize)
607                         {
608                             USBFS_EP[i].bufferSize = pEP->bufferSize;
609                         }
610                         /* Compare current Alternate setting with EP Alt*/
611                         if(USBFS_interfaceSetting[pEP->interface] == pEP->altSetting)
612                         {
613                             ep_type = pEP->attributes & USBFS_EP_TYPE_MASK;
614                             if ((pEP->addr & USBFS_DIR_IN) != 0u)
615                             {
616                                 /* IN Endpoint */
617                                 USBFS_EP[i].apiEpState = USBFS_EVENT_PENDING;
618                                 USBFS_EP[i].epMode = (ep_type == USBFS_EP_TYPE_ISOC) ?
619                                                         USBFS_MODE_ISO_IN : USBFS_MODE_ACK_IN;
620                                 /* Find and init CDC IN endpoint number */
621                                 #if defined(USBFS_ENABLE_CDC_CLASS)
622                                     if(((pEP->bMisc == USBFS_CLASS_CDC_DATA) ||
623                                         (pEP->bMisc == USBFS_CLASS_CDC)) &&
624                                         (ep_type != USBFS_EP_TYPE_INT))
625                                     {
626                                         USBFS_cdc_data_in_ep = i;
627                                     }
628                                 #endif  /* End USBFS_ENABLE_CDC_CLASS*/
629                                 #if ( defined(USBFS_ENABLE_MIDI_STREAMING) && \
630                                              (USBFS_MIDI_IN_BUFF_SIZE > 0) )
631                                     if((pEP->bMisc == USBFS_CLASS_AUDIO) &&
632                                        (ep_type == USBFS_EP_TYPE_BULK))
633                                     {
634                                         USBFS_midi_in_ep = i;
635                                     }
636                                 #endif  /* End USBFS_ENABLE_MIDI_STREAMING*/
637                             }
638                             else
639                             {
640                                 /* OUT Endpoint */
641                                 USBFS_EP[i].apiEpState = USBFS_NO_EVENT_PENDING;
642                                 USBFS_EP[i].epMode = (ep_type == USBFS_EP_TYPE_ISOC) ?
643                                                     USBFS_MODE_ISO_OUT : USBFS_MODE_ACK_OUT;
644                                 /* Find and init CDC IN endpoint number */
645                                 #if defined(USBFS_ENABLE_CDC_CLASS)
646                                     if(((pEP->bMisc == USBFS_CLASS_CDC_DATA) ||
647                                         (pEP->bMisc == USBFS_CLASS_CDC)) &&
648                                         (ep_type != USBFS_EP_TYPE_INT))
649                                     {
650                                         USBFS_cdc_data_out_ep = i;
651                                     }
652                                 #endif  /* End USBFS_ENABLE_CDC_CLASS*/
653                                 #if ( defined(USBFS_ENABLE_MIDI_STREAMING) && \
654                                              (USBFS_MIDI_OUT_BUFF_SIZE > 0) )
655                                     if((pEP->bMisc == USBFS_CLASS_AUDIO) &&
656                                        (ep_type == USBFS_EP_TYPE_BULK))
657                                     {
658                                         USBFS_midi_out_ep = i;
659                                     }
660                                 #endif  /* End USBFS_ENABLE_MIDI_STREAMING*/
661                             }
662                             USBFS_EP[i].addr = pEP->addr;
663                             USBFS_EP[i].attrib = pEP->attributes;
664
665                             #if(USBFS_EP_MM == USBFS__EP_DMAAUTO)
666                                 break;      /* use first EP setting in Auto memory managment */
667                             #endif /* End USBFS_EP_MM == USBFS__EP_DMAAUTO */
668                         }
669                     }
670                     pEP = &pEP[1u];
671                 }
672             }
673         #endif /* End (USBFS_EP_MA == USBFS__MA_DYNAMIC) */
674
675         /* Init class array for each interface and interface number for each EP.
676         *  It is used for handling Class specific requests directed to either an
677         *  interface or the endpoint.
678         */
679         /* p_list points the endpoint setting table. */
680         pEP = (const T_USBFS_EP_SETTINGS_BLOCK CYCODE *) pTmp->p_list;
681         for (i = 0u; i < ep; i++)
682         {
683             /* Configure interface number for each EP*/
684             USBFS_EP[pEP->addr & USBFS_DIR_UNUSED].interface = pEP->interface;
685             pEP = &pEP[1u];
686         }
687         /* Init pointer on interface class table*/
688         USBFS_interfaceClass = USBFS_GetInterfaceClassTablePtr();
689         /* Set the endpoint buffer addresses */
690
691         #if(USBFS_EP_MM != USBFS__EP_DMAAUTO)
692             for (ep = USBFS_EP1; ep < USBFS_MAX_EP; ep++)
693             {
694                 USBFS_EP[ep].buffOffset = buffCount;
695                  buffCount += USBFS_EP[ep].bufferSize;
696             }
697         #endif /* End USBFS_EP_MM != USBFS__EP_DMAAUTO */
698
699         /* Configure hardware registers */
700         USBFS_ConfigReg();
701     } /* USBFS_configuration > 0 */
702 }
703
704
705 /*******************************************************************************
706 * Function Name: USBFS_ConfigAltChanged
707 ********************************************************************************
708 *
709 * Summary:
710 *  This routine update configuration for the required endpoints only.
711 *  It is called after SET_INTERFACE request when Static memory allocation used.
712 *
713 * Parameters:
714 *  None.
715 *
716 * Return:
717 *  None.
718 *
719 * Reentrant:
720 *  No.
721 *
722 *******************************************************************************/
723 void USBFS_ConfigAltChanged(void) 
724 {
725     uint8 ep;
726     uint8 cur_ep;
727     uint8 i;
728     uint8 ep_type;
729     uint8 ri;
730
731     const T_USBFS_LUT CYCODE *pTmp;
732     const T_USBFS_EP_SETTINGS_BLOCK CYCODE *pEP;
733
734
735     /* Init Endpoints and Device Status if configured */
736     if(USBFS_configuration > 0u)
737     {
738         pTmp = USBFS_GetConfigTablePtr(USBFS_configuration - 1u);
739         pTmp = &pTmp[1u];
740         ep = pTmp->c;  /* For this table, c is the number of endpoints configurations  */
741
742         /* Do not touch EP which doesn't need reconfiguration */
743         /* When Alt setting changed, the only required endpoints need to be reconfigured */
744         /* p_list points the endpoint setting table. */
745         pEP = (const T_USBFS_EP_SETTINGS_BLOCK CYCODE *) pTmp->p_list;
746         for (i = 0u; i < ep; i++)
747         {
748             /*If Alt setting changed and new is same with EP Alt */
749             if((USBFS_interfaceSetting[pEP->interface] !=
750                 USBFS_interfaceSetting_last[pEP->interface] ) &&
751                (USBFS_interfaceSetting[pEP->interface] == pEP->altSetting) &&
752                (pEP->interface == CY_GET_REG8(USBFS_wIndexLo)))
753             {
754                 cur_ep = pEP->addr & USBFS_DIR_UNUSED;
755                 ri = ((cur_ep - USBFS_EP1) << USBFS_EPX_CNTX_ADDR_SHIFT);
756                 ep_type = pEP->attributes & USBFS_EP_TYPE_MASK;
757                 if ((pEP->addr & USBFS_DIR_IN) != 0u)
758                 {
759                     /* IN Endpoint */
760                     USBFS_EP[cur_ep].apiEpState = USBFS_EVENT_PENDING;
761                     USBFS_EP[cur_ep].epMode = (ep_type == USBFS_EP_TYPE_ISOC) ?
762                                                 USBFS_MODE_ISO_IN : USBFS_MODE_ACK_IN;
763                 }
764                 else
765                 {
766                     /* OUT Endpoint */
767                     USBFS_EP[cur_ep].apiEpState = USBFS_NO_EVENT_PENDING;
768                     USBFS_EP[cur_ep].epMode = (ep_type == USBFS_EP_TYPE_ISOC) ?
769                                                 USBFS_MODE_ISO_OUT : USBFS_MODE_ACK_OUT;
770                 }
771                  /* Change the SIE mode for the selected EP to NAK ALL */
772                  CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_NAK_IN_OUT);
773                 USBFS_EP[cur_ep].bufferSize = pEP->bufferSize;
774                 USBFS_EP[cur_ep].addr = pEP->addr;
775                 USBFS_EP[cur_ep].attrib = pEP->attributes;
776
777                 /* Clear the data toggle */
778                 USBFS_EP[cur_ep].epToggle = 0u;
779
780                 /* Dynamic reconfiguration for mode 3 transfer */
781             #if(USBFS_EP_MM == USBFS__EP_DMAAUTO)
782                 /* In_data_rdy for selected EP should be set to 0 */
783                 * (reg8 *)(USBFS_ARB_EP1_CFG_IND + ri) &= (uint8)~USBFS_ARB_EPX_CFG_IN_DATA_RDY;
784
785                 /* write the EP number for which reconfiguration is required */
786                 USBFS_DYN_RECONFIG_REG = (cur_ep - USBFS_EP1) <<
787                                                     USBFS_DYN_RECONFIG_EP_SHIFT;
788                 /* Set the dyn_config_en bit in dynamic reconfiguration register */
789                 USBFS_DYN_RECONFIG_REG |= USBFS_DYN_RECONFIG_ENABLE;
790                 /* wait for the dyn_config_rdy bit to set by the block,
791                 *  this bit will be set to 1 when block is ready for reconfiguration.
792                 */
793                 while((USBFS_DYN_RECONFIG_REG & USBFS_DYN_RECONFIG_RDY_STS) == 0u)
794                 {
795                     ;
796                 }
797                 /* Once dyn_config_rdy bit is set, FW can change the EP configuration. */
798                 /* Change EP Type with new direction */
799                 if((pEP->addr & USBFS_DIR_IN) == 0u)
800                 {
801                     USBFS_EP_TYPE_REG |= (uint8)(0x01u << (cur_ep - USBFS_EP1));
802                 }
803                 else
804                 {
805                     USBFS_EP_TYPE_REG &= (uint8)~(uint8)(0x01u << (cur_ep - USBFS_EP1));
806                 }
807                 /* dynamic reconfiguration enable bit cleared, pointers and control/status
808                 *  signals for the selected EP is cleared/re-initialized on negative edge
809                 *  of dynamic reconfiguration enable bit).
810                 */
811                 USBFS_DYN_RECONFIG_REG &= (uint8)~USBFS_DYN_RECONFIG_ENABLE;
812                 /* The main loop has to re-enable DMA and OUT endpoint*/
813             #else
814                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CNT0_IND + ri),
815                                                                 USBFS_EP[cur_ep].bufferSize >> 8u);
816                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CNT1_IND + ri),
817                                                                 USBFS_EP[cur_ep].bufferSize & 0xFFu);
818                 CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_RA_IND + ri),
819                                                                 USBFS_EP[cur_ep].buffOffset & 0xFFu);
820                 CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_RA_MSB_IND + ri),
821                                                                 USBFS_EP[cur_ep].buffOffset >> 8u);
822                 CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_WA_IND + ri),
823                                                                 USBFS_EP[cur_ep].buffOffset & 0xFFu);
824                 CY_SET_REG8((reg8 *)(USBFS_ARB_RW1_WA_MSB_IND + ri),
825                                                                 USBFS_EP[cur_ep].buffOffset >> 8u);
826             #endif /* End USBFS_EP_MM == USBFS__EP_DMAAUTO */
827             }
828             /* Get next EP element */
829             pEP = &pEP[1u];
830         }
831     }   /* USBFS_configuration > 0 */
832 }
833
834
835 /*******************************************************************************
836 * Function Name: USBFS_GetConfigTablePtr
837 ********************************************************************************
838 *
839 * Summary:
840 *  This routine returns a pointer a configuration table entry
841 *
842 * Parameters:
843 *  c:  Configuration Index
844 *
845 * Return:
846 *  Device Descriptor pointer.
847 *
848 *******************************************************************************/
849 const T_USBFS_LUT CYCODE *USBFS_GetConfigTablePtr(uint8 c)
850                                                         
851 {
852     /* Device Table */
853     const T_USBFS_LUT CYCODE *pTmp;
854
855     pTmp = (const T_USBFS_LUT CYCODE *) USBFS_TABLE[USBFS_device].p_list;
856
857     /* The first entry points to the Device Descriptor,
858     *  the rest configuration entries.
859         */
860     return( (const T_USBFS_LUT CYCODE *) pTmp[c + 1u].p_list );
861 }
862
863
864 /*******************************************************************************
865 * Function Name: USBFS_GetDeviceTablePtr
866 ********************************************************************************
867 *
868 * Summary:
869 *  This routine returns a pointer to the Device table
870 *
871 * Parameters:
872 *  None.
873 *
874 * Return:
875 *  Device Table pointer
876 *
877 *******************************************************************************/
878 const T_USBFS_LUT CYCODE *USBFS_GetDeviceTablePtr(void)
879                                                             
880 {
881     /* Device Table */
882     return( (const T_USBFS_LUT CYCODE *) USBFS_TABLE[USBFS_device].p_list );
883 }
884
885
886 /*******************************************************************************
887 * Function Name: USB_GetInterfaceClassTablePtr
888 ********************************************************************************
889 *
890 * Summary:
891 *  This routine returns Interface Class table pointer, which contains
892 *  the relation between interface number and interface class.
893 *
894 * Parameters:
895 *  None.
896 *
897 * Return:
898 *  Interface Class table pointer.
899 *
900 *******************************************************************************/
901 const uint8 CYCODE *USBFS_GetInterfaceClassTablePtr(void)
902                                                         
903 {
904     const T_USBFS_LUT CYCODE *pTmp;
905     uint8 currentInterfacesNum;
906
907     pTmp = USBFS_GetConfigTablePtr(USBFS_configuration - 1u);
908     currentInterfacesNum  = ((const uint8 *) pTmp->p_list)[USBFS_CONFIG_DESCR_NUM_INTERFACES];
909     /* Third entry in the LUT starts the Interface Table pointers */
910     /* The INTERFACE_CLASS table is located after all interfaces */
911     pTmp = &pTmp[currentInterfacesNum + 2u];
912     return( (const uint8 CYCODE *) pTmp->p_list );
913 }
914
915
916 /*******************************************************************************
917 * Function Name: USBFS_TerminateEP
918 ********************************************************************************
919 *
920 * Summary:
921 *  This function terminates the specified USBFS endpoint.
922 *  This function should be used before endpoint reconfiguration.
923 *
924 * Parameters:
925 *  Endpoint number.
926 *
927 * Return:
928 *  None.
929 *
930 * Reentrant:
931 *  No.
932 *
933 *******************************************************************************/
934 void USBFS_TerminateEP(uint8 ep) 
935 {
936     uint8 ri;
937
938     ep &= USBFS_DIR_UNUSED;
939     ri = ((ep - USBFS_EP1) << USBFS_EPX_CNTX_ADDR_SHIFT);
940
941     if ((ep > USBFS_EP0) && (ep < USBFS_MAX_EP))
942     {
943         /* Set the endpoint Halt */
944         USBFS_EP[ep].hwEpState |= (USBFS_ENDPOINT_STATUS_HALT);
945
946         /* Clear the data toggle */
947         USBFS_EP[ep].epToggle = 0u;
948         USBFS_EP[ep].apiEpState = USBFS_NO_EVENT_ALLOWED;
949
950         if ((USBFS_EP[ep].addr & USBFS_DIR_IN) != 0u)
951         {
952             /* IN Endpoint */
953             CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_NAK_IN);
954         }
955         else
956         {
957             /* OUT Endpoint */
958             CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_NAK_OUT);
959         }
960     }
961 }
962
963
964 /*******************************************************************************
965 * Function Name: USBFS_SetEndpointHalt
966 ********************************************************************************
967 *
968 * Summary:
969 *  This routine handles set endpoint halt.
970 *
971 * Parameters:
972 *  None.
973 *
974 * Return:
975 *  requestHandled.
976 *
977 * Reentrant:
978 *  No.
979 *
980 *******************************************************************************/
981 uint8 USBFS_SetEndpointHalt(void) 
982 {
983     uint8 ep;
984     uint8 ri;
985     uint8 requestHandled = USBFS_FALSE;
986
987     /* Set endpoint halt */
988     ep = CY_GET_REG8(USBFS_wIndexLo) & USBFS_DIR_UNUSED;
989     ri = ((ep - USBFS_EP1) << USBFS_EPX_CNTX_ADDR_SHIFT);
990
991     if ((ep > USBFS_EP0) && (ep < USBFS_MAX_EP))
992     {
993         /* Set the endpoint Halt */
994         USBFS_EP[ep].hwEpState |= (USBFS_ENDPOINT_STATUS_HALT);
995
996         /* Clear the data toggle */
997         USBFS_EP[ep].epToggle = 0u;
998         USBFS_EP[ep].apiEpState |= USBFS_NO_EVENT_ALLOWED;
999
1000         if ((USBFS_EP[ep].addr & USBFS_DIR_IN) != 0u)
1001         {
1002             /* IN Endpoint */
1003             CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_STALL_DATA_EP |
1004                                                                USBFS_MODE_ACK_IN);
1005         }
1006         else
1007         {
1008             /* OUT Endpoint */
1009             CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_STALL_DATA_EP |
1010                                                                USBFS_MODE_ACK_OUT);
1011         }
1012         requestHandled = USBFS_InitNoDataControlTransfer();
1013     }
1014
1015     return(requestHandled);
1016 }
1017
1018
1019 /*******************************************************************************
1020 * Function Name: USBFS_ClearEndpointHalt
1021 ********************************************************************************
1022 *
1023 * Summary:
1024 *  This routine handles clear endpoint halt.
1025 *
1026 * Parameters:
1027 *  None.
1028 *
1029 * Return:
1030 *  requestHandled.
1031 *
1032 * Reentrant:
1033 *  No.
1034 *
1035 *******************************************************************************/
1036 uint8 USBFS_ClearEndpointHalt(void) 
1037 {
1038     uint8 ep;
1039     uint8 ri;
1040     uint8 requestHandled = USBFS_FALSE;
1041
1042     /* Clear endpoint halt */
1043     ep = CY_GET_REG8(USBFS_wIndexLo) & USBFS_DIR_UNUSED;
1044     ri = ((ep - USBFS_EP1) << USBFS_EPX_CNTX_ADDR_SHIFT);
1045
1046     if ((ep > USBFS_EP0) && (ep < USBFS_MAX_EP))
1047     {
1048         /* Clear the endpoint Halt */
1049         USBFS_EP[ep].hwEpState &= (uint8)~(USBFS_ENDPOINT_STATUS_HALT);
1050
1051         /* Clear the data toggle */
1052         USBFS_EP[ep].epToggle = 0u;
1053         /* Clear toggle bit for already armed packet */
1054         CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CNT0_IND + ri), CY_GET_REG8(
1055                     (reg8 *)(USBFS_SIE_EP1_CNT0_IND + ri)) & (uint8)~USBFS_EPX_CNT_DATA_TOGGLE);
1056         /* Return API State as it was defined before */
1057         USBFS_EP[ep].apiEpState &= (uint8)~USBFS_NO_EVENT_ALLOWED;
1058
1059         if ((USBFS_EP[ep].addr & USBFS_DIR_IN) != 0u)
1060         {
1061             /* IN Endpoint */
1062             if(USBFS_EP[ep].apiEpState == USBFS_IN_BUFFER_EMPTY)
1063             {       /* Wait for next packet from application */
1064                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_NAK_IN);
1065             }
1066             else    /* Continue armed transfer */
1067             {
1068                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_ACK_IN);
1069             }
1070         }
1071         else
1072         {
1073             /* OUT Endpoint */
1074             if(USBFS_EP[ep].apiEpState == USBFS_OUT_BUFFER_FULL)
1075             {       /* Allow application to read full buffer */
1076                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_NAK_OUT);
1077             }
1078             else    /* Mark endpoint as empty, so it will be reloaded */
1079             {
1080                 CY_SET_REG8((reg8 *)(USBFS_SIE_EP1_CR0_IND + ri), USBFS_MODE_ACK_OUT);
1081             }
1082         }
1083         requestHandled = USBFS_InitNoDataControlTransfer();
1084     }
1085
1086     return(requestHandled);
1087 }
1088
1089
1090 /*******************************************************************************
1091 * Function Name: USBFS_ValidateAlternateSetting
1092 ********************************************************************************
1093 *
1094 * Summary:
1095 *  Validates (and records) a SET INTERFACE request.
1096 *
1097 * Parameters:
1098 *  None.
1099 *
1100 * Return:
1101 *  requestHandled.
1102 *
1103 * Reentrant:
1104 *  No.
1105 *
1106 *******************************************************************************/
1107 uint8 USBFS_ValidateAlternateSetting(void) 
1108 {
1109     uint8 requestHandled = USBFS_TRUE;
1110     uint8 interfaceNum;
1111     const T_USBFS_LUT CYCODE *pTmp;
1112     uint8 currentInterfacesNum;
1113
1114     interfaceNum = CY_GET_REG8(USBFS_wIndexLo);
1115     /* Validate interface setting, stall if invalid. */
1116     pTmp = USBFS_GetConfigTablePtr(USBFS_configuration - 1u);
1117     currentInterfacesNum  = ((const uint8 *) pTmp->p_list)[USBFS_CONFIG_DESCR_NUM_INTERFACES];
1118
1119     if((interfaceNum >= currentInterfacesNum) || (interfaceNum >= USBFS_MAX_INTERFACES_NUMBER))
1120     {   /* Wrong interface number */
1121         requestHandled = USBFS_FALSE;
1122     }
1123     else
1124     {
1125         /* Save current Alt setting to find out the difference in Config() function */
1126         USBFS_interfaceSetting_last[interfaceNum] = USBFS_interfaceSetting[interfaceNum];
1127         USBFS_interfaceSetting[interfaceNum] = CY_GET_REG8(USBFS_wValueLo);
1128     }
1129
1130     return (requestHandled);
1131 }
1132
1133
1134 /* [] END OF FILE */