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