]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/ThirdParty/USB_CDC/usbcore.c
Back out changes to LPC18xx UDP demo.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / ThirdParty / USB_CDC / usbcore.c
1 /*----------------------------------------------------------------------------
2  *      U S B  -  K e r n e l
3  *----------------------------------------------------------------------------
4  * Name:    usbcore.c
5  * Purpose: USB Core Module
6  * Version: V1.20
7  *----------------------------------------------------------------------------
8  *      This software is supplied "AS IS" without any warranties, express,
9  *      implied or statutory, including but not limited to the implied
10  *      warranties of fitness for purpose, satisfactory quality and
11  *      noninfringement. Keil extends you a royalty-free right to reproduce
12  *      and distribute executable files created using this software for use
13  *      on NXP Semiconductors LPC family microcontroller devices only. Nothing 
14  *      else gives you the right to use this software.
15  *
16  * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17  *----------------------------------------------------------------------------
18  * History:
19  *          V1.20 Added vendor specific requests
20  *                Changed string descriptor handling
21  *                Reworked Endpoint0
22  *          V1.00 Initial Version
23  *----------------------------------------------------------------------------*/
24 #include "lpc18xx.H"
25 #include "lpc_types.h"
26
27 #include "usb.h"
28 #include "usbcfg.h"
29 #include "usbhw.h"
30 #include "usbcore.h"
31 #include "usbdesc.h"
32 #include "usbuser.h"
33
34 #if (USB_CLASS)
35
36 #if (USB_AUDIO)
37 #include "audio.h"
38 #include "adcuser.h"
39 #endif
40
41 #if (USB_HID)
42 #include "hid.h"
43 #include "hiduser.h"
44 #endif
45
46 #if (USB_MSC)
47 #include "msc.h"
48 #include "mscuser.h"
49 extern MSC_CSW CSW;
50 #endif
51
52 #if (USB_CDC)
53 #include "cdc.h"
54 #include "cdcuser.h"
55 #endif
56
57 #endif
58
59 #if (USB_VENDOR)
60 #include "vendor.h"
61 #endif
62
63 #ifdef __CC_ARM
64 #pragma diag_suppress 111,177,1441
65 #endif
66
67 #if defined   (  __GNUC__  )
68 #define __packed __attribute__((__packed__))
69 #endif
70
71 uint16_t  USB_DeviceStatus;
72 uint8_t  USB_DeviceAddress;
73 uint8_t  USB_Configuration;
74 uint32_t USB_EndPointMask;
75 uint32_t USB_EndPointHalt;
76 uint32_t USB_EndPointStall;                         /* EP must stay stalled */
77 uint8_t  USB_NumInterfaces;
78 uint8_t  USB_AltSetting[USB_IF_NUM];
79
80 USB_EP_DATA EP0Data;
81
82 #pragma pack(4)
83 uint8_t  EP0Buf[USB_MAX_PACKET0];
84 USB_SETUP_PACKET SetupPacket;
85
86 extern volatile uint32_t DevStatusFS2HS;
87
88 /*
89  *  Reset USB Core
90  *    Parameters:      None
91  *    Return Value:    None
92  */
93
94 void USB_ResetCore (void) {
95
96   USB_DeviceStatus  = USB_POWER;
97   USB_DeviceAddress = 0;
98   USB_Configuration = 0;
99   USB_EndPointMask  = 0x00010001;
100   USB_EndPointHalt  = 0x00000000;
101   USB_EndPointStall = 0x00000000;
102 }
103
104
105 /*
106  *  USB Request - Setup Stage
107  *    Parameters:      None (global SetupPacket)
108  *    Return Value:    None
109  */
110
111 void USB_SetupStage (void) {
112   USB_ReadSetupPkt(0x00, (uint32_t *)&SetupPacket);
113 }
114
115
116 /*
117  *  USB Request - Data In Stage
118  *    Parameters:      None (global EP0Data)
119  *    Return Value:    None
120  */
121
122 void USB_DataInStage (void) {
123   uint32_t cnt;
124
125   if (EP0Data.Count > USB_MAX_PACKET0) {
126     cnt = USB_MAX_PACKET0;
127   } else {
128     cnt = EP0Data.Count;
129   }
130   cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
131   EP0Data.pData += cnt;
132   EP0Data.Count -= cnt;
133 }
134
135
136 /*
137  *  USB Request - Data Out Stage
138  *    Parameters:      None (global EP0Data)
139  *    Return Value:    None
140  */
141
142 void USB_DataOutStage (void) {
143   uint32_t cnt;
144
145   cnt = USB_ReadEP(0x00, EP0Data.pData);
146   EP0Data.pData += cnt;
147   EP0Data.Count -= cnt;
148 }
149
150
151 /*
152  *  USB Request - Status In Stage
153  *    Parameters:      None
154  *    Return Value:    None
155  */
156
157 void USB_StatusInStage (void) {
158   USB_WriteEP(0x80, NULL, 0);
159 }
160
161
162 /*
163  *  USB Request - Status Out Stage
164  *    Parameters:      None
165  *    Return Value:    None
166  */
167
168 void USB_StatusOutStage (void) {
169   USB_ReadEP(0x00, EP0Buf);
170 }
171
172
173 /*
174  *  Get Status USB Request
175  *    Parameters:      None (global SetupPacket)
176  *    Return Value:    TRUE - Success, FALSE - Error
177  */
178
179 INLINE uint32_t USB_ReqGetStatus (void) {
180   uint32_t n, m;
181
182   switch (SetupPacket.bmRequestType.BM.Recipient) {
183     case REQUEST_TO_DEVICE:
184       EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
185       break;
186     case REQUEST_TO_INTERFACE:
187       if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
188         *((__packed uint16_t *)EP0Buf) = 0;
189         EP0Data.pData = EP0Buf;
190       } else {
191         return (FALSE);
192       }
193       break;
194     case REQUEST_TO_ENDPOINT:
195       n = SetupPacket.wIndex.WB.L & 0x8F;
196       m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
197       if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
198         *((__packed uint16_t *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
199         EP0Data.pData = EP0Buf;
200       } else {
201         return (FALSE);
202       }
203       break;
204     default:
205       return (FALSE);
206   }
207   return (TRUE);
208 }
209
210
211 /*
212  *  Set/Clear Feature USB Request
213  *    Parameters:      sc:    0 - Clear, 1 - Set
214  *                            (global SetupPacket)
215  *    Return Value:    TRUE - Success, FALSE - Error
216  */
217
218 INLINE uint32_t USB_ReqSetClrFeature (uint32_t sc) {
219   uint32_t n, m;
220
221   switch (SetupPacket.bmRequestType.BM.Recipient) {
222     case REQUEST_TO_DEVICE:
223       if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
224         if (sc) {
225           USB_WakeUpCfg(TRUE);
226           USB_DeviceStatus |=  USB_GETSTATUS_REMOTE_WAKEUP;
227         } else {
228           USB_WakeUpCfg(FALSE);
229           USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
230         }
231       } else if (SetupPacket.wValue.W == USB_FEATURE_TEST_MODE) {
232           return USB_SetTestMode(SetupPacket.wIndex.WB.H);
233       } else {
234         return (FALSE);
235       }
236       break;
237     case REQUEST_TO_INTERFACE:
238       return (FALSE);
239     case REQUEST_TO_ENDPOINT:
240       n = SetupPacket.wIndex.WB.L & 0x8F;
241       m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
242       if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
243         if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
244           if (sc) {
245             USB_SetStallEP(n);
246             USB_EndPointHalt |=  m;
247           } else {
248             if ((USB_EndPointStall & m) != 0) {
249               return (TRUE);
250             }
251             USB_ClrStallEP(n);
252 #if (USB_MSC)
253             if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
254               /* Compliance Test: rewrite CSW after unstall */
255               if (CSW.dSignature == MSC_CSW_Signature) {
256                 USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
257               }
258             }
259 #endif
260             USB_EndPointHalt &= ~m;
261           }
262         } else {
263           return (FALSE);
264         }
265       } else {
266         return (FALSE);
267       }
268       break;
269     default:
270       return (FALSE);
271   }
272   return (TRUE);
273 }
274
275
276 /*
277  *  Set Address USB Request
278  *    Parameters:      None (global SetupPacket)
279  *    Return Value:    TRUE - Success, FALSE - Error
280  */
281
282 INLINE uint32_t USB_ReqSetAddress (void) {
283
284   switch (SetupPacket.bmRequestType.BM.Recipient) {
285     case REQUEST_TO_DEVICE:
286       USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
287       break;
288     default:
289       return (FALSE);
290   }
291   return (TRUE);
292 }
293
294
295 /*
296  *  Get Descriptor USB Request
297  *    Parameters:      None (global SetupPacket)
298  *    Return Value:    TRUE - Success, FALSE - Error
299  */
300
301 INLINE uint32_t USB_ReqGetDescriptor (void) {
302   uint8_t  *pD;
303   uint32_t len, n;
304
305   switch (SetupPacket.bmRequestType.BM.Recipient) {
306     case REQUEST_TO_DEVICE:
307       switch (SetupPacket.wValue.WB.H) {
308         case USB_DEVICE_DESCRIPTOR_TYPE:
309           EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
310           len = USB_DEVICE_DESC_SIZE;
311           break;
312         case USB_CONFIGURATION_DESCRIPTOR_TYPE:
313           if ( DevStatusFS2HS == FALSE ) { 
314             pD = (uint8_t *)USB_FSConfigDescriptor;
315           } else {
316             pD = (uint8_t *)USB_HSConfigDescriptor;
317                       }
318           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
319             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
320               pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
321             }
322           }
323           if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
324             return (FALSE);
325           }
326           EP0Data.pData = pD;
327           len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
328           break;
329         case USB_STRING_DESCRIPTOR_TYPE:
330           pD = (uint8_t *)USB_StringDescriptor;
331           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
332             if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
333               pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
334             }
335           }
336           if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
337             return (FALSE);
338           }
339           EP0Data.pData = pD;
340           len = ((USB_STRING_DESCRIPTOR *)pD)->bLength;
341           break;
342         case USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE:
343           /* USB Chapter 9. page 9.6.2 */
344           if ( DevStatusFS2HS == FALSE ) {
345                   return (FALSE);
346           }
347           else
348           {
349              EP0Data.pData = (uint8_t *)USB_DeviceQualifier;
350                    len = USB_DEVICE_QUALI_SIZE;
351           }
352           break;
353         case USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE:
354                       if ( DevStatusFS2HS == TRUE ) { 
355                 pD = (uint8_t *)USB_FSOtherSpeedConfiguration;
356               } else {
357                 pD = (uint8_t *)USB_HSOtherSpeedConfiguration;
358                       }
359           
360           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
361             if (((USB_OTHER_SPEED_CONFIGURATION *)pD)->bLength != 0) {
362               pD += ((USB_OTHER_SPEED_CONFIGURATION *)pD)->wTotalLength;
363             }
364           }
365           if (((USB_OTHER_SPEED_CONFIGURATION *)pD)->bLength == 0) {
366             return (FALSE);
367           }
368           EP0Data.pData = pD;
369           len = ((USB_OTHER_SPEED_CONFIGURATION *)pD)->wTotalLength;
370           break;
371         default:
372           return (FALSE);
373       }
374       break;
375     case REQUEST_TO_INTERFACE:
376       switch (SetupPacket.wValue.WB.H) {
377 #if USB_HID
378         case HID_HID_DESCRIPTOR_TYPE:
379           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
380             return (FALSE);    /* Only Single HID Interface is supported */
381           }
382                   if ( DevStatusFS2HS == FALSE ) { 
383             EP0Data.pData = (uint8_t *)USB_FSConfigDescriptor + HID_DESC_OFFSET;
384           } else {
385                     EP0Data.pData = (uint8_t *)USB_HSConfigDescriptor + HID_DESC_OFFSET;
386                   }
387           len = HID_DESC_SIZE;
388           break;
389         case HID_REPORT_DESCRIPTOR_TYPE:
390           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
391             return (FALSE);    /* Only Single HID Interface is supported */
392           }
393           EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
394           len = HID_ReportDescSize;
395           break;
396         case HID_PHYSICAL_DESCRIPTOR_TYPE:
397           return (FALSE);      /* HID Physical Descriptor is not supported */
398 #endif
399         default:
400           return (FALSE);
401       }
402       break;
403     default:
404       return (FALSE);
405   }
406
407   if (EP0Data.Count > len) {
408     EP0Data.Count = len;
409   }
410
411   return (TRUE);
412 }
413
414
415 /*
416  *  Get Configuration USB Request
417  *    Parameters:      None (global SetupPacket)
418  *    Return Value:    TRUE - Success, FALSE - Error
419  */
420
421 INLINE uint32_t USB_ReqGetConfiguration (void) {
422
423   switch (SetupPacket.bmRequestType.BM.Recipient) {
424     case REQUEST_TO_DEVICE:
425       EP0Data.pData = &USB_Configuration;
426       break;
427     default:
428       return (FALSE);
429   }
430   return (TRUE);
431 }
432
433
434 /*
435  *  Set Configuration USB Request
436  *    Parameters:      None (global SetupPacket)
437  *    Return Value:    TRUE - Success, FALSE - Error
438  */
439
440 INLINE uint32_t USB_ReqSetConfiguration (void) {
441   USB_COMMON_DESCRIPTOR *pD;
442   uint32_t alt = 0;
443   uint32_t n, m;
444   uint32_t new_addr;
445   switch (SetupPacket.bmRequestType.BM.Recipient) {
446     case REQUEST_TO_DEVICE:
447
448       if (SetupPacket.wValue.WB.L) {
449         if ( DevStatusFS2HS == FALSE ) { 
450           pD = (USB_COMMON_DESCRIPTOR *)USB_FSConfigDescriptor;
451             } else {
452               pD = (USB_COMMON_DESCRIPTOR *)USB_HSConfigDescriptor;
453             }
454         while (pD->bLength) {
455           switch (pD->bDescriptorType) {
456             case USB_CONFIGURATION_DESCRIPTOR_TYPE:
457               if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
458                 USB_Configuration = SetupPacket.wValue.WB.L;
459                 USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
460                 for (n = 0; n < USB_IF_NUM; n++) {
461                   USB_AltSetting[n] = 0;
462                 }
463               for (n = 1; n < USB_EP_NUM; n++) {
464                   if (USB_EndPointMask & (1 << n)) {
465                     USB_DisableEP(n);
466                   }
467                   if (USB_EndPointMask & ((1 << 16) << n)) {
468                     USB_DisableEP(n | 0x80);
469                   }
470                 }
471                 USB_EndPointMask = 0x00010001;
472                 USB_EndPointHalt = 0x00000000;
473                 USB_EndPointStall= 0x00000000;
474                 USB_Configure(TRUE);
475                 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
476                   USB_DeviceStatus |=  USB_GETSTATUS_SELF_POWERED;
477                 } else {
478                   USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
479                 }
480               } else {
481               new_addr = (uint32_t)pD + ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
482               pD = (USB_COMMON_DESCRIPTOR*)new_addr;
483                 continue;
484               }
485               break;
486             case USB_INTERFACE_DESCRIPTOR_TYPE:
487               alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
488               break;
489             case USB_ENDPOINT_DESCRIPTOR_TYPE:
490               if (alt == 0) {
491                 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
492                 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
493                 USB_EndPointMask |= m;
494                 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
495                 USB_EnableEP(n);
496                 USB_ResetEP(n);
497               }
498               break;
499           }
500         new_addr = (uint32_t)pD + pD->bLength;
501         pD = (USB_COMMON_DESCRIPTOR*)new_addr;
502         }
503       }
504       else {
505         USB_Configuration = 0;
506       for (n = 1; n < USB_EP_NUM; n++) {
507           if (USB_EndPointMask & (1 << n)) {
508             USB_DisableEP(n);
509           }
510           if (USB_EndPointMask & ((1 << 16) << n)) {
511             USB_DisableEP(n | 0x80);
512           }
513         }
514         USB_EndPointMask  = 0x00010001;
515         USB_EndPointHalt  = 0x00000000;
516         USB_EndPointStall = 0x00000000;
517         USB_Configure(FALSE);
518       }
519
520       if (USB_Configuration != SetupPacket.wValue.WB.L) {
521         return (FALSE);
522       }
523       break;
524     default:
525       return (FALSE);
526   }
527   return (TRUE);
528 }
529
530
531 /*
532  *  Get Interface USB Request
533  *    Parameters:      None (global SetupPacket)
534  *    Return Value:    TRUE - Success, FALSE - Error
535  */
536
537 INLINE uint32_t USB_ReqGetInterface (void) {
538
539   switch (SetupPacket.bmRequestType.BM.Recipient) {
540     case REQUEST_TO_INTERFACE:
541       if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
542         EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
543       } else {
544         return (FALSE);
545       }
546       break;
547     default:
548       return (FALSE);
549   }
550   return (TRUE);
551 }
552
553
554 /*
555  *  Set Interface USB Request
556  *    Parameters:      None (global SetupPacket)
557  *    Return Value:    TRUE - Success, FALSE - Error
558  */
559
560 INLINE uint32_t USB_ReqSetInterface (void) {
561   USB_COMMON_DESCRIPTOR *pD;
562   uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
563   uint32_t n, m;
564   uint32_t set, new_addr;
565
566   switch (SetupPacket.bmRequestType.BM.Recipient) {
567     case REQUEST_TO_INTERFACE:
568       if (USB_Configuration == 0) return (FALSE);
569       set = FALSE;
570       if ( DevStatusFS2HS == FALSE ) { 
571         pD  = (USB_COMMON_DESCRIPTOR *)USB_FSConfigDescriptor;
572       } else {
573         pD  = (USB_COMMON_DESCRIPTOR *)USB_HSConfigDescriptor;
574       }
575       while (pD->bLength) {
576         switch (pD->bDescriptorType) {
577           case USB_CONFIGURATION_DESCRIPTOR_TYPE:
578             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
579               new_addr = (uint32_t)pD + ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
580               pD = (USB_COMMON_DESCRIPTOR*)new_addr;
581               continue;
582             }
583             break;
584           case USB_INTERFACE_DESCRIPTOR_TYPE:
585             ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
586             alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
587             msk = 0;
588             if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
589               set = TRUE;
590               old = USB_AltSetting[ifn];
591               USB_AltSetting[ifn] = (uint8_t)alt;
592             }
593             break;
594           case USB_ENDPOINT_DESCRIPTOR_TYPE:
595             if (ifn == SetupPacket.wIndex.WB.L) {
596               n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
597               m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
598               if (alt == SetupPacket.wValue.WB.L) {
599                 USB_EndPointMask |=  m;
600                 USB_EndPointHalt &= ~m;
601                 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
602                 USB_EnableEP(n);
603                 USB_ResetEP(n);
604                 msk |= m;
605               }
606               else if ((alt == old) && ((msk & m) == 0)) {
607                 USB_EndPointMask &= ~m;
608                 USB_EndPointHalt &= ~m;
609                 USB_DisableEP(n);
610               }
611             }
612            break;
613         }
614         new_addr = (uint32_t)pD + pD->bLength;
615         pD = (USB_COMMON_DESCRIPTOR*)new_addr;
616       }
617       break;
618     default:
619       return (FALSE);
620   }
621
622   return (set);
623 }
624
625
626 /*
627  *  USB Endpoint 0 Event Callback
628  *    Parameters:      event
629  *    Return Value:    none
630  */
631  
632 void USB_EndPoint0 (uint32_t event) {
633
634   switch (event) {
635     case USB_EVT_SETUP:
636       USB_SetupStage();
637       USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
638       EP0Data.Count = SetupPacket.wLength;     /* Number of bytes to transfer */
639       switch (SetupPacket.bmRequestType.BM.Type) {
640
641         case REQUEST_STANDARD:
642           switch (SetupPacket.bRequest) {
643             case USB_REQUEST_GET_STATUS:
644               if (!USB_ReqGetStatus()) {
645                 goto stall_i;
646               }
647               USB_DataInStage();
648               break;
649
650             case USB_REQUEST_CLEAR_FEATURE:
651               if (!USB_ReqSetClrFeature(0)) {
652                 goto stall_i;
653               }
654               USB_StatusInStage();
655 #if USB_FEATURE_EVENT
656               USB_Feature_Event();
657 #endif
658               break;
659
660             case USB_REQUEST_SET_FEATURE:
661               if (!USB_ReqSetClrFeature(1)) {
662                 goto stall_i;
663               }
664               USB_StatusInStage();
665 #if USB_FEATURE_EVENT
666               USB_Feature_Event();
667 #endif
668               break;
669
670             case USB_REQUEST_SET_ADDRESS:
671               if (!USB_ReqSetAddress()) {
672                 goto stall_i;
673               }
674               USB_StatusInStage();
675               break;
676
677             case USB_REQUEST_GET_DESCRIPTOR:
678               if (!USB_ReqGetDescriptor()) {
679                 goto stall_i;
680               }
681               USB_DataInStage();
682               break;
683
684             case USB_REQUEST_SET_DESCRIPTOR:
685 /*stall_o:*/  USB_SetStallEP(0x00);            /* not supported */
686               EP0Data.Count = 0;
687               break;
688
689             case USB_REQUEST_GET_CONFIGURATION:
690               if (!USB_ReqGetConfiguration()) {
691                 goto stall_i;
692               }
693               USB_DataInStage();
694               break;
695
696             case USB_REQUEST_SET_CONFIGURATION:
697               if (!USB_ReqSetConfiguration()) {
698                 goto stall_i;
699               }
700               USB_StatusInStage();
701 #if USB_CONFIGURE_EVENT
702               USB_Configure_Event();
703 #endif
704               break;
705
706             case USB_REQUEST_GET_INTERFACE:
707               if (!USB_ReqGetInterface()) {
708                 goto stall_i;
709               }
710               USB_DataInStage();
711               break;
712
713             case USB_REQUEST_SET_INTERFACE:
714               if (!USB_ReqSetInterface()) {
715                 goto stall_i;
716               }
717               USB_StatusInStage();
718 #if USB_INTERFACE_EVENT
719               USB_Interface_Event();
720 #endif
721               break;
722
723             default:
724               goto stall_i;
725           }
726           break;  /* end case REQUEST_STANDARD */
727
728 #if USB_CLASS
729         case REQUEST_CLASS:
730           switch (SetupPacket.bmRequestType.BM.Recipient) {
731
732             case REQUEST_TO_DEVICE:
733               goto stall_i;                                              /* not supported */
734
735             case REQUEST_TO_INTERFACE:
736 #if USB_HID
737               if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {           /* IF number correct? */
738                 switch (SetupPacket.bRequest) {
739                   case HID_REQUEST_GET_REPORT:
740                     if (HID_GetReport()) {
741                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
742                       USB_DataInStage();                                 /* send requested data */
743                       goto setup_class_ok;
744                     }
745                     break;
746                   case HID_REQUEST_SET_REPORT:
747                     EP0Data.pData = EP0Buf;                              /* data to be received */ 
748                     goto setup_class_ok;
749                   case HID_REQUEST_GET_IDLE:
750                     if (HID_GetIdle()) {
751                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
752                       USB_DataInStage();                                 /* send requested data */
753                       goto setup_class_ok;
754                     }
755                     break;
756                   case HID_REQUEST_SET_IDLE:
757                     if (HID_SetIdle()) {
758                       USB_StatusInStage();                               /* send Acknowledge */
759                       goto setup_class_ok;
760                     }
761                     break;
762                   case HID_REQUEST_GET_PROTOCOL:
763                     if (HID_GetProtocol()) {
764                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
765                       USB_DataInStage();                                 /* send requested data */
766                       goto setup_class_ok;
767                     }
768                     break;
769                   case HID_REQUEST_SET_PROTOCOL:
770                     if (HID_SetProtocol()) {
771                       USB_StatusInStage();                               /* send Acknowledge */
772                       goto setup_class_ok;
773                     }
774                     break;
775                 }
776               }
777 #endif  /* USB_HID */
778 #if USB_MSC
779               if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {           /* IF number correct? */
780                 switch (SetupPacket.bRequest) {
781                   case MSC_REQUEST_RESET:
782                     if ((SetupPacket.wValue.W == 0) &&                   /* RESET with invalid parameters -> STALL */
783                         (SetupPacket.wLength  == 0)) {
784                       if (MSC_Reset()) {
785                         USB_StatusInStage();
786                         goto setup_class_ok;
787                       }
788                     }
789                     break;
790                   case MSC_REQUEST_GET_MAX_LUN:
791                     if ((SetupPacket.wValue.W == 0) &&                   /* GET_MAX_LUN with invalid parameters -> STALL */
792                         (SetupPacket.wLength  == 1)) { 
793                       if (MSC_GetMaxLUN()) {
794                         EP0Data.pData = EP0Buf;
795                         USB_DataInStage();
796                         goto setup_class_ok;
797                       }
798                     }
799                     break;
800                 }
801               }
802 #endif  /* USB_MSC */
803 #if USB_AUDIO
804               if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||       /* IF number correct? */
805                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
806                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
807                 switch (SetupPacket.bRequest) {
808                   case AUDIO_REQUEST_GET_CUR:
809                   case AUDIO_REQUEST_GET_MIN:
810                   case AUDIO_REQUEST_GET_MAX:
811                   case AUDIO_REQUEST_GET_RES:
812                     if (ADC_IF_GetRequest()) {
813                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
814                       USB_DataInStage();                                 /* send requested data */
815                       goto setup_class_ok;
816                     }
817                     break;
818                   case AUDIO_REQUEST_SET_CUR:
819 //                case AUDIO_REQUEST_SET_MIN:
820 //                case AUDIO_REQUEST_SET_MAX:
821 //                case AUDIO_REQUEST_SET_RES:
822                     EP0Data.pData = EP0Buf;                              /* data to be received */ 
823                     goto setup_class_ok;
824                 }
825               }
826 #endif  /* USB_AUDIO */
827 #if USB_CDC
828               if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  ||       /* IF number correct? */
829                   (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
830                 switch (SetupPacket.bRequest) {
831                   case CDC_SEND_ENCAPSULATED_COMMAND:
832                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
833                     goto setup_class_ok;
834                   case CDC_GET_ENCAPSULATED_RESPONSE:
835                     if (CDC_GetEncapsulatedResponse()) {
836                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
837                       USB_DataInStage();                                 /* send requested data */
838                       goto setup_class_ok;
839                     }
840                     break;
841                   case CDC_SET_COMM_FEATURE:
842                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
843                     goto setup_class_ok;
844                   case CDC_GET_COMM_FEATURE:
845                     if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
846                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
847                       USB_DataInStage();                                 /* send requested data */
848                       goto setup_class_ok;
849                     }
850                     break;
851                   case CDC_CLEAR_COMM_FEATURE:
852                     if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
853                       USB_StatusInStage();                               /* send Acknowledge */
854                       goto setup_class_ok;
855                     }
856                     break;
857                   case CDC_SET_LINE_CODING:
858                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
859                     goto setup_class_ok;
860                   case CDC_GET_LINE_CODING:
861                     if (CDC_GetLineCoding()) {
862                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
863                       USB_DataInStage();                                 /* send requested data */
864                       goto setup_class_ok;
865                     }
866                     break;
867                   case CDC_SET_CONTROL_LINE_STATE:
868                     if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
869                       USB_StatusInStage();                               /* send Acknowledge */
870                       goto setup_class_ok;
871                     }
872                     break;
873                   case CDC_SEND_BREAK:
874                     if (CDC_SendBreak(SetupPacket.wValue.W)) {
875                       USB_StatusInStage();                               /* send Acknowledge */
876                       goto setup_class_ok;
877                     }
878                     break;
879                 }
880               }
881 #endif  /* USB_CDC */
882               goto stall_i;                                              /* not supported */
883               /* end case REQUEST_TO_INTERFACE */
884
885             case REQUEST_TO_ENDPOINT:
886 #if USB_AUDIO
887               switch (SetupPacket.bRequest) {
888                 case AUDIO_REQUEST_GET_CUR:
889                 case AUDIO_REQUEST_GET_MIN:
890                 case AUDIO_REQUEST_GET_MAX:
891                 case AUDIO_REQUEST_GET_RES:
892                   if (ADC_EP_GetRequest()) {
893                     EP0Data.pData = EP0Buf;                              /* point to data to be sent */
894                     USB_DataInStage();                                   /* send requested data */
895                     goto setup_class_ok;
896                   }
897                   break;
898                 case AUDIO_REQUEST_SET_CUR:
899 //              case AUDIO_REQUEST_SET_MIN:
900 //              case AUDIO_REQUEST_SET_MAX:
901 //              case AUDIO_REQUEST_SET_RES:
902                   EP0Data.pData = EP0Buf;                                /* data to be received */ 
903                   goto setup_class_ok;
904               }
905 #endif  /* USB_AUDIO */
906               goto stall_i;
907               /* end case REQUEST_TO_ENDPOINT */
908
909             default:
910               goto stall_i;
911           }
912 setup_class_ok:                                                          /* request finished successfully */
913           break;  /* end case REQUEST_CLASS */
914 #endif  /* USB_CLASS */
915
916 #if USB_VENDOR
917         case REQUEST_VENDOR:
918           switch (SetupPacket.bmRequestType.BM.Recipient) {
919
920             case REQUEST_TO_DEVICE:
921               if (!USB_ReqVendorDev(TRUE)) {
922                 goto stall_i;                                            /* not supported */               
923               }
924               break;
925
926             case REQUEST_TO_INTERFACE:
927               if (!USB_ReqVendorIF(TRUE)) {
928                 goto stall_i;                                            /* not supported */               
929               }
930               break;
931
932             case REQUEST_TO_ENDPOINT:
933               if (!USB_ReqVendorEP(TRUE)) {
934                 goto stall_i;                                            /* not supported */               
935               }
936               break;
937
938             default:
939               goto stall_i;
940           }
941
942           if (SetupPacket.wLength) {
943             if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
944               USB_DataInStage();
945             }
946           } else {
947             USB_StatusInStage();
948           }
949
950           break;  /* end case REQUEST_VENDOR */ 
951 #endif  /* USB_VENDOR */
952
953         default:
954 stall_i:  USB_SetStallEP(0x80);
955           EP0Data.Count = 0;
956           break;
957       }
958       break;  /* end case USB_EVT_SETUP */
959
960     case USB_EVT_OUT_NAK:
961       if (SetupPacket.bmRequestType.BM.Dir == 0)
962       {
963         USB_ReadReqEP(0x00, EP0Data.pData, EP0Data.Count);
964       }
965       else
966       {
967         /* might be zero length pkt */
968         USB_ReadReqEP(0x00, EP0Data.pData, 0);
969       }
970       break;
971     case USB_EVT_OUT:
972       if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
973         if (EP0Data.Count) {                                             /* still data to receive ? */
974           USB_DataOutStage();                                            /* receive data */
975           if (EP0Data.Count == 0) {                                      /* data complete ? */
976             switch (SetupPacket.bmRequestType.BM.Type) {
977
978               case REQUEST_STANDARD:
979                 goto stall_i;                                            /* not supported */
980
981 #if (USB_CLASS) 
982               case REQUEST_CLASS:
983                 switch (SetupPacket.bmRequestType.BM.Recipient) {
984                   case REQUEST_TO_DEVICE:
985                     goto stall_i;                                        /* not supported */
986
987                   case REQUEST_TO_INTERFACE:
988 #if USB_HID
989                     if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {     /* IF number correct? */
990                       switch (SetupPacket.bRequest) {
991                         case HID_REQUEST_SET_REPORT:
992                           if (HID_SetReport()) {
993                             USB_StatusInStage();                         /* send Acknowledge */
994                             goto out_class_ok;
995                           }
996                           break;
997                       }
998                     }
999 #endif  /* USB_HID */  
1000 #if USB_AUDIO
1001                     if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  || /* IF number correct? */
1002                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
1003                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
1004                       switch (SetupPacket.bRequest) {
1005                         case AUDIO_REQUEST_SET_CUR:
1006 //                      case AUDIO_REQUEST_SET_MIN:
1007 //                      case AUDIO_REQUEST_SET_MAX:
1008 //                      case AUDIO_REQUEST_SET_RES:
1009                           if (ADC_IF_SetRequest()) {
1010                             USB_StatusInStage();                         /* send Acknowledge */
1011                             goto out_class_ok;
1012                           }
1013                           break;
1014                       }
1015                     }
1016 #endif  /* USB_AUDIO */
1017 #if USB_CDC
1018                     if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  || /* IF number correct? */
1019                         (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
1020                       switch (SetupPacket.bRequest) {
1021                         case CDC_SEND_ENCAPSULATED_COMMAND:
1022                           if (CDC_SendEncapsulatedCommand()) {
1023                             USB_StatusInStage();                         /* send Acknowledge */
1024                             goto out_class_ok;
1025                           }
1026                           break;
1027                         case CDC_SET_COMM_FEATURE:
1028                           if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
1029                             USB_StatusInStage();                         /* send Acknowledge */
1030                             goto out_class_ok;
1031                           }
1032                           break;
1033                         case CDC_SET_LINE_CODING:
1034                           if (CDC_SetLineCoding()) {
1035                             USB_StatusInStage();                         /* send Acknowledge */
1036                             goto out_class_ok;
1037                           }
1038                           break;
1039                       }
1040                     } 
1041 #endif  /* USB_CDC */
1042                     goto stall_i;
1043                     /* end case REQUEST_TO_INTERFACE */
1044
1045                   case REQUEST_TO_ENDPOINT:
1046 #if USB_AUDIO
1047                     switch (SetupPacket.bRequest) {
1048                       case AUDIO_REQUEST_SET_CUR:
1049 //                    case AUDIO_REQUEST_SET_MIN:
1050 //                    case AUDIO_REQUEST_SET_MAX:
1051 //                    case AUDIO_REQUEST_SET_RES:
1052                         if (ADC_EP_SetRequest()) {
1053                           USB_StatusInStage();                           /* send Acknowledge */
1054                           goto out_class_ok;
1055                         }
1056                         break;
1057                     }
1058 #endif  /* USB_AUDIO */
1059                     goto stall_i;
1060                     /* end case REQUEST_TO_ENDPOINT */
1061
1062                   default:
1063                     goto stall_i;
1064                 }
1065 out_class_ok:                                                            /* request finished successfully */
1066                 break; /* end case REQUEST_CLASS */
1067 #endif  /* USB_CLASS */
1068
1069 #if USB_VENDOR
1070               case REQUEST_VENDOR:
1071                 switch (SetupPacket.bmRequestType.BM.Recipient) {
1072       
1073                   case REQUEST_TO_DEVICE:
1074                     if (!USB_ReqVendorDev(FALSE)) {
1075                       goto stall_i;                                      /* not supported */               
1076                     }
1077                     break;
1078       
1079                   case REQUEST_TO_INTERFACE:
1080                     if (!USB_ReqVendorIF(FALSE)) {
1081                       goto stall_i;                                      /* not supported */               
1082                     }
1083                     break;
1084       
1085                   case REQUEST_TO_ENDPOINT:
1086                     if (!USB_ReqVendorEP(FALSE)) {
1087                       goto stall_i;                                      /* not supported */               
1088                     }
1089                     break;
1090       
1091                   default:
1092                     goto stall_i;
1093                 }
1094       
1095                 USB_StatusInStage();
1096       
1097                 break;  /* end case REQUEST_VENDOR */ 
1098 #endif  /* USB_VENDOR */
1099
1100               default:
1101                 goto stall_i;
1102             }
1103           }
1104         }
1105       } else {
1106         USB_StatusOutStage();                                            /* receive Acknowledge */
1107       }
1108       break;  /* end case USB_EVT_OUT */
1109
1110     case USB_EVT_IN :
1111       if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
1112         USB_DataInStage();                                               /* send data */
1113       } else {
1114         if (USB_DeviceAddress & 0x80) {
1115           USB_DeviceAddress &= 0x7F;
1116           USB_SetAddress(USB_DeviceAddress);
1117         }
1118       }
1119       break;  /* end case USB_EVT_IN */
1120
1121     case USB_EVT_OUT_STALL:
1122       USB_ClrStallEP(0x00);
1123       break;
1124
1125     case USB_EVT_IN_STALL:
1126       USB_ClrStallEP(0x80);
1127       break;
1128
1129   }
1130 }