These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbcore.c Source File

usbcore.c

00001 /*----------------------------------------------------------------------------
00002  *      U S B  -  K e r n e l
00003  *----------------------------------------------------------------------------
00004  *      Name:    USBCORE.C
00005  *      Purpose: USB Core Module
00006  *      Version: V1.10
00007  *----------------------------------------------------------------------------
00008  *      This software is supplied "AS IS" without any warranties, express,
00009  *      implied or statutory, including but not limited to the implied
00010  *      warranties of fitness for purpose, satisfactory quality and
00011  *      noninfringement. Keil extends you a royalty-free right to reproduce
00012  *      and distribute executable files created using this software for use
00013  *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
00014  *      else gives you the right to use this software.
00015  *
00016  *      Copyright (c) 2005-2009 Keil Software.
00017  *---------------------------------------------------------------------------*/
00018 
00019 #include "lpc_types.h"
00020 
00021 #include "usb.h"
00022 #include "usbcfg.h"
00023 #include "usbhw.h"
00024 #include "usbcore.h"
00025 #include "usbdesc.h"
00026 #include "usbuser.h"
00027 
00028 #if (USB_CLASS)
00029 
00030 #if (USB_AUDIO)
00031 #include "audio.h"
00032 #include "adcuser.h"
00033 #endif
00034 
00035 #if (USB_HID)
00036 #include "hid.h"
00037 #include "hiduser.h"
00038 #endif
00039 
00040 #if (USB_MSC)
00041 #include "msc.h"
00042 #include "mscuser.h"
00043 extern MSC_CSW CSW;
00044 #endif
00045 
00046 #if (USB_CDC)
00047 #include "cdc.h"
00048 #include "cdcuser.h"
00049 #endif
00050 
00051 #endif
00052 
00053 #if (USB_VENDOR)
00054 #include "vendor.h"
00055 #endif
00056 
00057 #ifndef __IAR_SYSTEMS_ICC__
00058 #pragma diag_suppress 111,1441
00059 #endif
00060 
00061 uint16_t  USB_DeviceStatus;
00062 uint8_t  USB_DeviceAddress;
00063 uint8_t  USB_Configuration;
00064 uint32_t USB_EndPointMask;
00065 uint32_t USB_EndPointHalt;
00066 uint8_t  USB_NumInterfaces;
00067 uint8_t  USB_AltSetting[USB_IF_NUM];
00068 
00069 uint8_t  EP0Buf[USB_MAX_PACKET0];
00070 
00071 
00072 USB_EP_DATA EP0Data;
00073 
00074 USB_SETUP_PACKET SetupPacket;
00075 
00076 
00077 /*
00078  *  Reset USB Core
00079  *    Parameters:      None
00080  *    Return Value:    None
00081  */
00082 
00083 void USB_ResetCore (void) {
00084 
00085   USB_DeviceStatus  = USB_POWER;
00086   USB_DeviceAddress = 0;
00087   USB_Configuration = 0;
00088   USB_EndPointMask  = 0x00010001;
00089   USB_EndPointHalt  = 0x00000000;
00090 }
00091 
00092 
00093 /*
00094  *  USB Request - Setup Stage
00095  *    Parameters:      None (global SetupPacket)
00096  *    Return Value:    None
00097  */
00098 
00099 void USB_SetupStage (void) {
00100   USB_ReadEP(0x00, (uint8_t *)&SetupPacket);
00101 }
00102 
00103 
00104 /*
00105  *  USB Request - Data In Stage
00106  *    Parameters:      None (global EP0Data)
00107  *    Return Value:    None
00108  */
00109 
00110 void USB_DataInStage (void) {
00111   uint32_t cnt;
00112 
00113   if (EP0Data.Count > USB_MAX_PACKET0) {
00114     cnt = USB_MAX_PACKET0;
00115   } else {
00116     cnt = EP0Data.Count;
00117   }
00118   cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
00119   EP0Data.pData += cnt;
00120   EP0Data.Count -= cnt;
00121 }
00122 
00123 
00124 /*
00125  *  USB Request - Data Out Stage
00126  *    Parameters:      None (global EP0Data)
00127  *    Return Value:    None
00128  */
00129 
00130 void USB_DataOutStage (void) {
00131   uint32_t cnt;
00132 
00133   cnt = USB_ReadEP(0x00, EP0Data.pData);
00134   EP0Data.pData += cnt;
00135   EP0Data.Count -= cnt;
00136 }
00137 
00138 
00139 /*
00140  *  USB Request - Status In Stage
00141  *    Parameters:      None
00142  *    Return Value:    None
00143  */
00144 
00145 void USB_StatusInStage (void) {
00146   USB_WriteEP(0x80, NULL, 0);
00147 }
00148 
00149 
00150 /*
00151  *  USB Request - Status Out Stage
00152  *    Parameters:      None
00153  *    Return Value:    None
00154  */
00155 
00156 void USB_StatusOutStage (void) {
00157   USB_ReadEP(0x00, EP0Buf);
00158 }
00159 
00160 
00161 /*
00162  *  Get Status USB Request
00163  *    Parameters:      None (global SetupPacket)
00164  *    Return Value:    TRUE - Success, FALSE - Error
00165  */
00166 
00167 #ifdef __IAR_SYSTEMS_ICC__
00168 inline uint32_t USB_GetStatus (void) {
00169 #else
00170 __inline uint32_t USB_GetStatus (void) {
00171 #endif
00172   uint32_t n, m;
00173 
00174   switch (SetupPacket.bmRequestType.BM.Recipient) {
00175     case REQUEST_TO_DEVICE:
00176       EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
00177       USB_DataInStage();
00178       break;
00179     case REQUEST_TO_INTERFACE:
00180       if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
00181         *((__packed uint16_t *)EP0Buf) = 0;
00182         EP0Data.pData = EP0Buf;
00183         USB_DataInStage();
00184       } else {
00185         return (FALSE);
00186       }
00187       break;
00188     case REQUEST_TO_ENDPOINT:
00189       n = SetupPacket.wIndex.WB.L & 0x8F;
00190       m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00191       if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
00192         *((__packed uint16_t *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
00193         EP0Data.pData = EP0Buf;
00194         USB_DataInStage();
00195       } else {
00196         return (FALSE);
00197       }
00198       break;
00199     default:
00200       return (FALSE);
00201   }
00202   return (TRUE);
00203 }
00204 
00205 
00206 /*
00207  *  Set/Clear Feature USB Request
00208  *    Parameters:      sc:    0 - Clear, 1 - Set
00209  *                            None (global SetupPacket)
00210  *    Return Value:    TRUE - Success, FALSE - Error
00211  */
00212 
00213 #ifdef __IAR_SYSTEMS_ICC__
00214 inline uint32_t USB_SetClrFeature (uint32_t sc) {
00215 #else
00216 __inline uint32_t USB_SetClrFeature (uint32_t sc) {
00217 #endif
00218   uint32_t n, m;
00219 
00220   switch (SetupPacket.bmRequestType.BM.Recipient) {
00221     case REQUEST_TO_DEVICE:
00222       if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
00223         if (sc) {
00224           USB_WakeUpCfg(TRUE);
00225           USB_DeviceStatus |=  USB_GETSTATUS_REMOTE_WAKEUP;
00226         } else {
00227           USB_WakeUpCfg(FALSE);
00228           USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
00229         }
00230       } else {
00231         return (FALSE);
00232       }
00233       break;
00234     case REQUEST_TO_INTERFACE:
00235       return (FALSE);
00236     case REQUEST_TO_ENDPOINT:
00237       n = SetupPacket.wIndex.WB.L & 0x8F;
00238       m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00239       if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
00240         if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
00241           if (sc) {
00242             USB_SetStallEP(n);
00243             USB_EndPointHalt |=  m;
00244           } else {
00245             USB_ClrStallEP(n);
00246             USB_EndPointHalt &= ~m;
00247           }
00248         } else {
00249           return (FALSE);
00250         }
00251       } else {
00252         return (FALSE);
00253       }
00254       break;
00255     default:
00256       return (FALSE);
00257   }
00258   return (TRUE);
00259 }
00260 
00261 
00262 /*
00263  *  Get Descriptor USB Request
00264  *    Parameters:      None (global SetupPacket)
00265  *    Return Value:    TRUE - Success, FALSE - Error
00266  */
00267 
00268 #ifdef __IAR_SYSTEMS_ICC__
00269 inline uint32_t USB_GetDescriptor (void) {
00270 #else
00271 __inline uint32_t USB_GetDescriptor (void) {
00272 #endif
00273   uint8_t  *pD;
00274   uint32_t len, n;
00275 
00276   switch (SetupPacket.bmRequestType.BM.Recipient) {
00277     case REQUEST_TO_DEVICE:
00278       switch (SetupPacket.wValue.WB.H) {
00279         case USB_DEVICE_DESCRIPTOR_TYPE:
00280           EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
00281           len = USB_DEVICE_DESC_SIZE;
00282           break;
00283         case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00284           pD = (uint8_t *)USB_ConfigDescriptor;
00285           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
00286             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
00287               pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00288             }
00289           }
00290           if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
00291             return (FALSE);
00292           }
00293           EP0Data.pData = pD;
00294           len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00295           break;
00296         case USB_STRING_DESCRIPTOR_TYPE:
00297           EP0Data.pData = (uint8_t *)USB_StringDescriptor + SetupPacket.wValue.WB.L;
00298           len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
00299           break;
00300         default:
00301           return (FALSE);
00302       }
00303       break;
00304     case REQUEST_TO_INTERFACE:
00305       switch (SetupPacket.wValue.WB.H) {
00306 #if USB_HID
00307         case HID_HID_DESCRIPTOR_TYPE:
00308           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
00309             return (FALSE);    /* Only Single HID Interface is supported */
00310           }
00311           EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
00312           len = HID_DESC_SIZE;
00313           break;
00314         case HID_REPORT_DESCRIPTOR_TYPE:
00315           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
00316             return (FALSE);    /* Only Single HID Interface is supported */
00317           }
00318           EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
00319           len = HID_ReportDescSize;
00320           break;
00321         case HID_PHYSICAL_DESCRIPTOR_TYPE:
00322           return (FALSE);      /* HID Physical Descriptor is not supported */
00323 #endif
00324         default:
00325           return (FALSE);
00326       }
00327     default:
00328       return (FALSE);
00329   }
00330 
00331   if (EP0Data.Count > len) {
00332     EP0Data.Count = len;
00333   }
00334   USB_DataInStage();
00335 
00336   return (TRUE);
00337 }
00338 
00339 
00340 /*
00341  *  Set Configuration USB Request
00342  *    Parameters:      None (global SetupPacket)
00343  *    Return Value:    TRUE - Success, FALSE - Error
00344  */
00345 
00346 #ifdef __IAR_SYSTEMS_ICC__
00347 inline uint32_t USB_SetConfiguration (void) {
00348 #else
00349 __inline uint32_t USB_SetConfiguration (void) {
00350 #endif
00351   USB_COMMON_DESCRIPTOR *pD;
00352     uint32_t  alt, n, m;
00353     uint32_t tmp;
00354 
00355   if (SetupPacket.wValue.WB.L) {
00356     pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
00357     while (pD->bLength) {
00358       switch (pD->bDescriptorType) {
00359         case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00360           if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
00361             USB_Configuration = SetupPacket.wValue.WB.L;
00362             USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
00363             for (n = 0; n < USB_IF_NUM; n++) {
00364               USB_AltSetting[n] = 0;
00365             }
00366             for (n = 1; n < 16; n++) {
00367               if (USB_EndPointMask & (1 << n)) {
00368                 USB_DisableEP(n);
00369               }
00370               if (USB_EndPointMask & ((1 << 16) << n)) {
00371                 USB_DisableEP(n | 0x80);
00372               }
00373             }
00374             USB_EndPointMask = 0x00010001;
00375             USB_EndPointHalt = 0x00000000;
00376             USB_Configure(TRUE);
00377             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_SELF_POWERED) {
00378               USB_DeviceStatus |=  USB_GETSTATUS_SELF_POWERED;
00379             } else {
00380               USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
00381             }
00382           } else {
00383             //(uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00384             tmp = (uint32_t) pD;
00385             tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00386             pD = (USB_COMMON_DESCRIPTOR *)tmp;
00387             continue;
00388           }
00389           break;
00390         case USB_INTERFACE_DESCRIPTOR_TYPE:
00391           alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
00392           break;
00393         case USB_ENDPOINT_DESCRIPTOR_TYPE:
00394           if (alt == 0) {
00395             n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
00396             m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00397             USB_EndPointMask |= m;
00398             USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
00399             USB_EnableEP(n);
00400             USB_ResetEP(n);
00401           }
00402           break;
00403       }
00404       //(uint8_t *)pD += pD->bLength;
00405       tmp = (uint32_t) pD;
00406       tmp += pD->bLength;
00407       pD = (USB_COMMON_DESCRIPTOR *)tmp;
00408     }
00409   }
00410   else {
00411     USB_Configuration = 0;
00412     for (n = 1; n < 16; n++) {
00413       if (USB_EndPointMask & (1 << n)) {
00414         USB_DisableEP(n);
00415       }
00416       if (USB_EndPointMask & ((1 << 16) << n)) {
00417         USB_DisableEP(n | 0x80);
00418       }
00419     }
00420     USB_EndPointMask  = 0x00010001;
00421     USB_EndPointHalt  = 0x00000000;
00422     USB_Configure(FALSE);
00423   }
00424 
00425   if (USB_Configuration == SetupPacket.wValue.WB.L) {
00426     return (TRUE);
00427   } else {
00428     return (FALSE);
00429   }
00430 }
00431 
00432 
00433 /*
00434  *  Set Interface USB Request
00435  *    Parameters:      None (global SetupPacket)
00436  *    Return Value:    TRUE - Success, FALSE - Error
00437  */
00438 
00439 #ifdef __IAR_SYSTEMS_ICC__
00440 inline uint32_t USB_SetInterface (void) {
00441 #else
00442 __inline uint32_t USB_SetInterface (void) {
00443 #endif
00444   USB_COMMON_DESCRIPTOR *pD;
00445   uint32_t                  ifn, alt, old, msk, n, m;
00446   uint32_t                   set;
00447   uint32_t tmp;
00448 
00449   if (USB_Configuration == 0) return (FALSE);
00450 
00451   set = FALSE;
00452   pD  = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
00453   while (pD->bLength) {
00454     switch (pD->bDescriptorType) {
00455       case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00456         if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
00457           //(uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00458             tmp = (uint32_t)pD;
00459             tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00460             pD = (USB_COMMON_DESCRIPTOR *)tmp;
00461           continue;
00462         }
00463         break;
00464       case USB_INTERFACE_DESCRIPTOR_TYPE:
00465         ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
00466         alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
00467         msk = 0;
00468         if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
00469           set = TRUE;
00470           old = USB_AltSetting[ifn];
00471           USB_AltSetting[ifn] = (uint8_t)alt;
00472         }
00473         break;
00474       case USB_ENDPOINT_DESCRIPTOR_TYPE:
00475         if (ifn == SetupPacket.wIndex.WB.L) {
00476           n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
00477           m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00478           if (alt == SetupPacket.wValue.WB.L) {
00479             USB_EndPointMask |=  m;
00480             USB_EndPointHalt &= ~m;
00481             USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
00482             USB_EnableEP(n);
00483             USB_ResetEP(n);
00484             msk |= m;
00485           }
00486           else if ((alt == old) && ((msk & m) == 0)) {
00487             USB_EndPointMask &= ~m;
00488             USB_EndPointHalt &= ~m;
00489             USB_DisableEP(n);
00490           }
00491         }
00492         break;
00493     }
00494     //(uint8_t *)pD += pD->bLength;
00495     tmp = (uint32_t)pD;
00496     tmp += pD->bLength;
00497     pD = (USB_COMMON_DESCRIPTOR *) tmp;
00498   }
00499   return (set);
00500 }
00501 
00502 
00503 /*
00504  *  USB Endpoint 0 Event Callback
00505  *    Parameter:       event
00506  */
00507 
00508 void USB_EndPoint0 (uint32_t event) {
00509 
00510   switch (event) {
00511 
00512     case USB_EVT_SETUP:
00513       USB_SetupStage();
00514       USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
00515       EP0Data.Count = SetupPacket.wLength;
00516       switch (SetupPacket.bmRequestType.BM.Type) {
00517 
00518         case REQUEST_STANDARD:
00519           switch (SetupPacket.bRequest) {
00520 
00521             case USB_REQUEST_GET_STATUS:
00522               if (!USB_GetStatus()) {
00523                 goto stall_i;
00524               }
00525               break;
00526 
00527             case USB_REQUEST_CLEAR_FEATURE:
00528               if (!USB_SetClrFeature(0)) {
00529                 goto stall_i;
00530               }
00531               USB_StatusInStage();
00532 #if USB_FEATURE_EVENT
00533               USB_Feature_Event();
00534 #endif
00535               break;
00536 
00537             case USB_REQUEST_SET_FEATURE:
00538               if (!USB_SetClrFeature(1)) {
00539                 goto stall_i;
00540               }
00541               USB_StatusInStage();
00542 #if USB_FEATURE_EVENT
00543               USB_Feature_Event();
00544 #endif
00545               break;
00546 
00547             case USB_REQUEST_SET_ADDRESS:
00548               switch (SetupPacket.bmRequestType.BM.Recipient) {
00549                 case REQUEST_TO_DEVICE:
00550                   USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
00551                   USB_StatusInStage();
00552                   break;
00553                 default:
00554                   goto stall_i;
00555               }
00556               break;
00557 
00558             case USB_REQUEST_GET_DESCRIPTOR:
00559               if (!USB_GetDescriptor()) {
00560                 goto stall_i;
00561               }
00562               break;
00563 
00564             case USB_REQUEST_SET_DESCRIPTOR:
00565 /*stall_o:*/  USB_SetStallEP(0x00);
00566               EP0Data.Count = 0;
00567               break;
00568 
00569             case USB_REQUEST_GET_CONFIGURATION:
00570               switch (SetupPacket.bmRequestType.BM.Recipient) {
00571                 case REQUEST_TO_DEVICE:
00572                   EP0Data.pData = &USB_Configuration;
00573                   USB_DataInStage();
00574                   break;
00575                 default:
00576                   goto stall_i;
00577               }
00578               break;
00579 
00580             case USB_REQUEST_SET_CONFIGURATION:
00581               switch (SetupPacket.bmRequestType.BM.Recipient) {
00582                 case REQUEST_TO_DEVICE:
00583                   if (!USB_SetConfiguration()) {
00584                     goto stall_i;
00585                   }
00586                   USB_StatusInStage();
00587 #if USB_CONFIGURE_EVENT
00588                   USB_Configure_Event();
00589 #endif
00590                   break;
00591                 default:
00592                   goto stall_i;
00593               }
00594               break;
00595 
00596             case USB_REQUEST_GET_INTERFACE:
00597               switch (SetupPacket.bmRequestType.BM.Recipient) {
00598                 case REQUEST_TO_INTERFACE:
00599                   if ((USB_Configuration != 0) &&
00600                       (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
00601                     EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
00602                     USB_DataInStage();
00603                   } else {
00604                     goto stall_i;
00605                   }
00606                   break;
00607                 default:
00608                   goto stall_i;
00609               }
00610               break;
00611 
00612             case USB_REQUEST_SET_INTERFACE:
00613               switch (SetupPacket.bmRequestType.BM.Recipient) {
00614                 case REQUEST_TO_INTERFACE:
00615                   if (!USB_SetInterface()) {
00616                     goto stall_i;
00617                   }
00618                   USB_StatusInStage();
00619 #if USB_INTERFACE_EVENT
00620                   USB_Interface_Event();
00621 #endif
00622                   break;
00623                 default:
00624                   goto stall_i;
00625               }
00626               break;
00627 
00628             default:
00629               goto stall_i;
00630 
00631           }
00632           break;
00633 
00634         case REQUEST_CLASS:
00635 #if USB_CLASS
00636           switch (SetupPacket.bmRequestType.BM.Recipient) {
00637             case REQUEST_TO_INTERFACE:
00638 #if USB_HID
00639               if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
00640                 switch (SetupPacket.bRequest) {
00641                   case HID_REQUEST_GET_REPORT:
00642                     if (HID_GetReport()) {
00643                       EP0Data.pData = EP0Buf;
00644                       USB_DataInStage();
00645                       goto class_ok;
00646                     }
00647                     break;
00648                   case HID_REQUEST_SET_REPORT:
00649                     EP0Data.pData = EP0Buf;
00650                     goto class_ok;
00651                   case HID_REQUEST_GET_IDLE:
00652                     if (HID_GetIdle()) {
00653                       EP0Data.pData = EP0Buf;
00654                       USB_DataInStage();
00655                       goto class_ok;
00656                     }
00657                     break;
00658                   case HID_REQUEST_SET_IDLE:
00659                     if (HID_SetIdle()) {
00660                       USB_StatusInStage();
00661                       goto class_ok;
00662                     }
00663                     break;
00664                   case HID_REQUEST_GET_PROTOCOL:
00665                     if (HID_GetProtocol()) {
00666                       EP0Data.pData = EP0Buf;
00667                       USB_DataInStage();
00668                       goto class_ok;
00669                     }
00670                     break;
00671                   case HID_REQUEST_SET_PROTOCOL:
00672                     if (HID_SetProtocol()) {
00673                       USB_StatusInStage();
00674                       goto class_ok;
00675                     }
00676                     break;
00677                 }
00678               }
00679 #endif  /* USB_HID */
00680 #if USB_MSC
00681               if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {
00682                 switch (SetupPacket.bRequest) {
00683                   case MSC_REQUEST_RESET:
00684                     if (MSC_Reset()) {
00685                       USB_StatusInStage();
00686                       goto class_ok;
00687                     }
00688                     break;
00689                   case MSC_REQUEST_GET_MAX_LUN:
00690                     if (MSC_GetMaxLUN()) {
00691                       EP0Data.pData = EP0Buf;
00692                       USB_DataInStage();
00693                       goto class_ok;
00694                     }
00695                     break;
00696                 }
00697               }
00698 #endif  /* USB_MSC */
00699 #if USB_AUDIO
00700               if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||
00701                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
00702                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
00703                 if (SetupPacket.bmRequestType.BM.Dir) {
00704                   if (ADC_IF_GetRequest()) {
00705                     EP0Data.pData = EP0Buf;
00706                     USB_DataInStage();
00707                     goto class_ok;
00708                   }
00709                 } else {
00710                   EP0Data.pData = EP0Buf;
00711                   goto class_ok;
00712                 }
00713               }
00714 #endif  /* USB_AUDIO */
00715               goto stall_i;
00716 #if USB_AUDIO
00717             case REQUEST_TO_ENDPOINT:
00718               if (SetupPacket.bmRequestType.BM.Dir) {
00719                 if (ADC_EP_GetRequest()) {
00720                   EP0Data.pData = EP0Buf;
00721                   USB_DataInStage();
00722                   goto class_ok;
00723                 }
00724               } else {
00725                 EP0Data.pData = EP0Buf;
00726                 goto class_ok;
00727               }
00728               goto stall_i;
00729 #endif  /* USB_AUDIO */
00730             default:
00731               goto stall_i;
00732           }
00733 class_ok: break;
00734 #else
00735           goto stall_i;
00736 #endif  /* USB_CLASS */
00737 
00738         case REQUEST_VENDOR:
00739           goto stall_i;
00740 
00741         default:
00742 stall_i:  USB_SetStallEP(0x80);
00743           EP0Data.Count = 0;
00744           break;
00745 
00746       }
00747       break;
00748 
00749     case USB_EVT_OUT:
00750       if (SetupPacket.bmRequestType.BM.Dir == 0) {
00751         if (EP0Data.Count) {
00752           USB_DataOutStage();
00753           if (EP0Data.Count == 0) {
00754             switch (SetupPacket.bmRequestType.BM.Type) {
00755               case REQUEST_STANDARD:
00756                 goto stall_i;
00757 #if (USB_CLASS)
00758               case REQUEST_CLASS:
00759                 switch (SetupPacket.bmRequestType.BM.Recipient) {
00760                   case REQUEST_TO_INTERFACE:
00761 #if USB_HID
00762                     if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
00763                       if (!HID_SetReport()) {
00764                         goto stall_i;
00765                       }
00766                       break;
00767                     }
00768 #endif
00769 #if USB_AUDIO
00770                     if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||
00771                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
00772                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
00773                       if (!ADC_IF_SetRequest()) {
00774                         goto stall_i;
00775                       }
00776                       break;
00777                     }
00778 #endif
00779                     goto stall_i;
00780                   case REQUEST_TO_ENDPOINT:
00781 #if USB_AUDIO
00782                     if (ADC_EP_SetRequest()) break;
00783 #endif
00784                     goto stall_i;
00785                   default:
00786                     goto stall_i;
00787                 }
00788 #endif
00789               default:
00790                 goto stall_i;
00791             }
00792 //           USB_StatusInStage();
00793           }
00794         }
00795       } else {
00796         USB_StatusOutStage();
00797       }
00798       break;
00799 
00800     case USB_EVT_IN:
00801       if (SetupPacket.bmRequestType.BM.Dir == 1) {
00802         USB_DataInStage();
00803       } else {
00804         if (USB_DeviceAddress & 0x80) {
00805           USB_DeviceAddress &= 0x7F;
00806           USB_SetAddress(USB_DeviceAddress);
00807         }
00808       }
00809       break;
00810 
00811     case USB_EVT_IN_STALL:
00812       USB_ClrStallEP(0x80);
00813       break;
00814 
00815     case USB_EVT_OUT_STALL:
00816       USB_ClrStallEP(0x00);
00817       break;
00818 
00819   }
00820 }