Port of Keils USBCDC example, compiles ok. Gets stuck at init

Dependencies:   mbed

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.20
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) 2009 Keil - An ARM Company. All rights reserved.
00017  *----------------------------------------------------------------------------
00018  * History:
00019  *          V1.20 Added vendor specific requests
00020  *                Changed string descriptor handling
00021  *                Reworked Endpoint0
00022  *          V1.00 Initial Version
00023  *----------------------------------------------------------------------------*/
00024 #include "type.h"
00025 
00026 #include "usb.h"
00027 #include "usbcfg.h"
00028 #include "usbhw.h"
00029 #include "usbcore.h"
00030 #include "usbdesc.h"
00031 #include "usbuser.h"
00032 
00033 #if (USB_CLASS)
00034 
00035 #if (USB_AUDIO)
00036 #include "audio.h"
00037 #include "adcuser.h"
00038 #endif
00039 
00040 #if (USB_HID)
00041 #include "hid.h"
00042 #include "hiduser.h"
00043 #endif
00044 
00045 #if (USB_MSC)
00046 #include "msc.h"
00047 #include "mscuser.h"
00048 extern MSC_CSW CSW;
00049 #endif
00050 
00051 #if (USB_CDC)
00052 #include "cdc.h"
00053 #include "cdcuser.h"
00054 #endif
00055 
00056 #endif
00057 
00058 #if (USB_VENDOR)
00059 #include "vendor.h"
00060 #endif
00061 
00062 #pragma diag_suppress 111,1441
00063 
00064 
00065 uint16_t  USB_DeviceStatus;
00066 uint8_t  USB_DeviceAddress;
00067 uint8_t  USB_Configuration;
00068 uint32_t USB_EndPointMask;
00069 uint32_t USB_EndPointHalt;
00070 uint32_t USB_EndPointStall;                         /* EP must stay stalled */
00071 uint8_t  USB_NumInterfaces;
00072 uint8_t  USB_AltSetting[USB_IF_NUM];
00073 
00074 uint8_t  EP0Buf[USB_MAX_PACKET0];
00075 
00076 
00077 USB_EP_DATA EP0Data;
00078 
00079 USB_SETUP_PACKET SetupPacket;
00080 
00081 
00082 /*
00083  *  Reset USB Core
00084  *    Parameters:      None
00085  *    Return Value:    None
00086  */
00087 
00088 void USB_ResetCore (void) {
00089 
00090   USB_DeviceStatus  = USB_POWER;
00091   USB_DeviceAddress = 0;
00092   USB_Configuration = 0;
00093   USB_EndPointMask  = 0x00010001;
00094   USB_EndPointHalt  = 0x00000000;
00095   USB_EndPointStall = 0x00000000;
00096 }
00097 
00098 
00099 /*
00100  *  USB Request - Setup Stage
00101  *    Parameters:      None (global SetupPacket)
00102  *    Return Value:    None
00103  */
00104 
00105 void USB_SetupStage (void) {
00106   USB_ReadEP(0x00, (uint8_t *)&SetupPacket);
00107 }
00108 
00109 
00110 /*
00111  *  USB Request - Data In Stage
00112  *    Parameters:      None (global EP0Data)
00113  *    Return Value:    None
00114  */
00115 
00116 void USB_DataInStage (void) {
00117   uint32_t cnt;
00118 
00119   if (EP0Data.Count > USB_MAX_PACKET0) {
00120     cnt = USB_MAX_PACKET0;
00121   } else {
00122     cnt = EP0Data.Count;
00123   }
00124   cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
00125   EP0Data.pData += cnt;
00126   EP0Data.Count -= cnt;
00127 }
00128 
00129 
00130 /*
00131  *  USB Request - Data Out Stage
00132  *    Parameters:      None (global EP0Data)
00133  *    Return Value:    None
00134  */
00135 
00136 void USB_DataOutStage (void) {
00137   uint32_t cnt;
00138 
00139   cnt = USB_ReadEP(0x00, EP0Data.pData);
00140   EP0Data.pData += cnt;
00141   EP0Data.Count -= cnt;
00142 }
00143 
00144 
00145 /*
00146  *  USB Request - Status In Stage
00147  *    Parameters:      None
00148  *    Return Value:    None
00149  */
00150 
00151 void USB_StatusInStage (void) {
00152   USB_WriteEP(0x80, 0, 0);
00153 }
00154 
00155 
00156 /*
00157  *  USB Request - Status Out Stage
00158  *    Parameters:      None
00159  *    Return Value:    None
00160  */
00161 
00162 void USB_StatusOutStage (void) {
00163   USB_ReadEP(0x00, EP0Buf);
00164 }
00165 
00166 
00167 /*
00168  *  Get Status USB Request
00169  *    Parameters:      None (global SetupPacket)
00170  *    Return Value:    TRUE - Success, FALSE - Error
00171  */
00172 
00173 __inline uint32_t USB_ReqGetStatus (void) {
00174   uint32_t n, m;
00175 
00176   switch (SetupPacket.bmRequestType.BM.Recipient) {
00177     case REQUEST_TO_DEVICE:
00178       EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
00179       break;
00180     case REQUEST_TO_INTERFACE:
00181       if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
00182         *((__packed uint16_t *)EP0Buf) = 0;
00183         EP0Data.pData = EP0Buf;
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       } else {
00195         return (FALSE);
00196       }
00197       break;
00198     default:
00199       return (FALSE);
00200   }
00201   return (TRUE);
00202 }
00203 
00204 
00205 /*
00206  *  Set/Clear Feature USB Request
00207  *    Parameters:      sc:    0 - Clear, 1 - Set
00208  *                            (global SetupPacket)
00209  *    Return Value:    TRUE - Success, FALSE - Error
00210  */
00211 
00212 __inline uint32_t USB_ReqSetClrFeature (uint32_t sc) {
00213   uint32_t n, m;
00214 
00215   switch (SetupPacket.bmRequestType.BM.Recipient) {
00216     case REQUEST_TO_DEVICE:
00217       if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
00218         if (sc) {
00219           USB_WakeUpCfg(TRUE);
00220           USB_DeviceStatus |=  USB_GETSTATUS_REMOTE_WAKEUP;
00221         } else {
00222           USB_WakeUpCfg(FALSE);
00223           USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
00224         }
00225       } else {
00226         return (FALSE);
00227       }
00228       break;
00229     case REQUEST_TO_INTERFACE:
00230       return (FALSE);
00231     case REQUEST_TO_ENDPOINT:
00232       n = SetupPacket.wIndex.WB.L & 0x8F;
00233       m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00234       if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
00235         if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
00236           if (sc) {
00237             USB_SetStallEP(n);
00238             USB_EndPointHalt |=  m;
00239           } else {
00240             if ((USB_EndPointStall & m) != 0) {
00241               return (TRUE);
00242             }
00243             USB_ClrStallEP(n);
00244 #if (USB_MSC)
00245             if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
00246               /* Compliance Test: rewrite CSW after unstall */
00247               if (CSW.dSignature == MSC_CSW_Signature) {
00248                 USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
00249               }
00250             }
00251 #endif
00252             USB_EndPointHalt &= ~m;
00253           }
00254         } else {
00255           return (FALSE);
00256         }
00257       } else {
00258         return (FALSE);
00259       }
00260       break;
00261     default:
00262       return (FALSE);
00263   }
00264   return (TRUE);
00265 }
00266 
00267 
00268 /*
00269  *  Set Address USB Request
00270  *    Parameters:      None (global SetupPacket)
00271  *    Return Value:    TRUE - Success, FALSE - Error
00272  */
00273 
00274 __inline uint32_t USB_ReqSetAddress (void) {
00275 
00276   switch (SetupPacket.bmRequestType.BM.Recipient) {
00277     case REQUEST_TO_DEVICE:
00278       USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
00279       break;
00280     default:
00281       return (FALSE);
00282   }
00283   return (TRUE);
00284 }
00285 
00286 
00287 /*
00288  *  Get Descriptor USB Request
00289  *    Parameters:      None (global SetupPacket)
00290  *    Return Value:    TRUE - Success, FALSE - Error
00291  */
00292 
00293 __inline uint32_t USB_ReqGetDescriptor (void) {
00294   uint8_t  *pD;
00295   uint32_t len, n;
00296 
00297   switch (SetupPacket.bmRequestType.BM.Recipient) {
00298     case REQUEST_TO_DEVICE:
00299       switch (SetupPacket.wValue.WB.H) {
00300         case USB_DEVICE_DESCRIPTOR_TYPE:
00301           EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
00302           len = USB_DEVICE_DESC_SIZE;
00303           break;
00304         case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00305           pD = (uint8_t *)USB_ConfigDescriptor;
00306           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
00307             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
00308               pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00309             }
00310           }
00311           if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
00312             return (FALSE);
00313           }
00314           EP0Data.pData = pD;
00315           len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00316           break;
00317         case USB_STRING_DESCRIPTOR_TYPE:
00318           pD = (uint8_t *)USB_StringDescriptor;
00319           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
00320             if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
00321               pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
00322             }
00323           }
00324           if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
00325             return (FALSE);
00326           }
00327           EP0Data.pData = pD;
00328           len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
00329           break;
00330         default:
00331           return (FALSE);
00332       }
00333       break;
00334     case REQUEST_TO_INTERFACE:
00335       switch (SetupPacket.wValue.WB.H) {
00336 #if USB_HID
00337         case HID_HID_DESCRIPTOR_TYPE:
00338           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
00339             return (FALSE);    /* Only Single HID Interface is supported */
00340           }
00341           EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
00342           len = HID_DESC_SIZE;
00343           break;
00344         case HID_REPORT_DESCRIPTOR_TYPE:
00345           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
00346             return (FALSE);    /* Only Single HID Interface is supported */
00347           }
00348           EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
00349           len = HID_ReportDescSize;
00350           break;
00351         case HID_PHYSICAL_DESCRIPTOR_TYPE:
00352           return (FALSE);      /* HID Physical Descriptor is not supported */
00353 #endif
00354         default:
00355           return (FALSE);
00356       }
00357       break;
00358     default:
00359       return (FALSE);
00360   }
00361 
00362   if (EP0Data.Count > len) {
00363     EP0Data.Count = len;
00364   }
00365 
00366   return (TRUE);
00367 }
00368 
00369 
00370 /*
00371  *  Get Configuration USB Request
00372  *    Parameters:      None (global SetupPacket)
00373  *    Return Value:    TRUE - Success, FALSE - Error
00374  */
00375 
00376 __inline uint32_t USB_ReqGetConfiguration (void) {
00377 
00378   switch (SetupPacket.bmRequestType.BM.Recipient) {
00379     case REQUEST_TO_DEVICE:
00380       EP0Data.pData = &USB_Configuration;
00381       break;
00382     default:
00383       return (FALSE);
00384   }
00385   return (TRUE);
00386 }
00387 
00388 
00389 /*
00390  *  Set Configuration USB Request
00391  *    Parameters:      None (global SetupPacket)
00392  *    Return Value:    TRUE - Success, FALSE - Error
00393  */
00394 
00395 __inline uint32_t USB_ReqSetConfiguration (void) {
00396   USB_COMMON_DESCRIPTOR *pD;
00397   uint32_t alt = 0;
00398   uint32_t n, m;
00399 
00400   switch (SetupPacket.bmRequestType.BM.Recipient) {
00401     case REQUEST_TO_DEVICE:
00402 
00403       if (SetupPacket.wValue.WB.L) {
00404         pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
00405         while (pD->bLength) {
00406           switch (pD->bDescriptorType) {
00407             case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00408               if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
00409                 USB_Configuration = SetupPacket.wValue.WB.L;
00410                 USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
00411                 for (n = 0; n < USB_IF_NUM; n++) {
00412                   USB_AltSetting[n] = 0;
00413                 }
00414                 for (n = 1; n < 16; n++) {
00415                   if (USB_EndPointMask & (1 << n)) {
00416                     USB_DisableEP(n);
00417                   }
00418                   if (USB_EndPointMask & ((1 << 16) << n)) {
00419                     USB_DisableEP(n | 0x80);
00420                   }
00421                 }
00422                 USB_EndPointMask = 0x00010001;
00423                 USB_EndPointHalt = 0x00000000;
00424                 USB_EndPointStall= 0x00000000;
00425                 USB_Configure(TRUE);
00426                 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
00427                   USB_DeviceStatus |=  USB_GETSTATUS_SELF_POWERED;
00428                 } else {
00429                   USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
00430                 }
00431               } else {
00432                 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; // (uint8_t *)
00433                 continue;
00434               }
00435               break;
00436             case USB_INTERFACE_DESCRIPTOR_TYPE:
00437               alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
00438               break;
00439             case USB_ENDPOINT_DESCRIPTOR_TYPE:
00440               if (alt == 0) {
00441                 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
00442                 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00443                 USB_EndPointMask |= m;
00444                 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
00445                 USB_EnableEP(n);
00446                 USB_ResetEP(n);
00447               }
00448               break;
00449           }
00450           pD += pD->bLength; // (uint8_t *)
00451         }
00452       }
00453       else {
00454         USB_Configuration = 0;
00455         for (n = 1; n < 16; n++) {
00456           if (USB_EndPointMask & (1 << n)) {
00457             USB_DisableEP(n);
00458           }
00459           if (USB_EndPointMask & ((1 << 16) << n)) {
00460             USB_DisableEP(n | 0x80);
00461           }
00462         }
00463         USB_EndPointMask  = 0x00010001;
00464         USB_EndPointHalt  = 0x00000000;
00465         USB_EndPointStall = 0x00000000;
00466         USB_Configure(FALSE);
00467       }
00468 
00469       if (USB_Configuration != SetupPacket.wValue.WB.L) {
00470         return (FALSE);
00471       }
00472       break;
00473     default:
00474       return (FALSE);
00475   }
00476   return (TRUE);
00477 }
00478 
00479 
00480 /*
00481  *  Get Interface USB Request
00482  *    Parameters:      None (global SetupPacket)
00483  *    Return Value:    TRUE - Success, FALSE - Error
00484  */
00485 
00486 __inline uint32_t USB_ReqGetInterface (void) {
00487 
00488   switch (SetupPacket.bmRequestType.BM.Recipient) {
00489     case REQUEST_TO_INTERFACE:
00490       if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
00491         EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
00492       } else {
00493         return (FALSE);
00494       }
00495       break;
00496     default:
00497       return (FALSE);
00498   }
00499   return (TRUE);
00500 }
00501 
00502 
00503 /*
00504  *  Set Interface USB Request
00505  *    Parameters:      None (global SetupPacket)
00506  *    Return Value:    TRUE - Success, FALSE - Error
00507  */
00508 
00509 __inline uint32_t USB_ReqSetInterface (void) {
00510   USB_COMMON_DESCRIPTOR *pD;
00511   uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
00512   uint32_t n, m;
00513   uint32_t set;
00514 
00515   switch (SetupPacket.bmRequestType.BM.Recipient) {
00516     case REQUEST_TO_INTERFACE:
00517       if (USB_Configuration == 0) return (FALSE);
00518       set = FALSE;
00519       pD  = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
00520       while (pD->bLength) {
00521         switch (pD->bDescriptorType) {
00522           case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00523             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
00524              pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength; // (uint8_t *)
00525               continue;
00526             }
00527             break;
00528           case USB_INTERFACE_DESCRIPTOR_TYPE:
00529             ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
00530             alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
00531             msk = 0;
00532             if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
00533               set = TRUE;
00534               old = USB_AltSetting[ifn];
00535               USB_AltSetting[ifn] = (uint8_t)alt;
00536             }
00537             break;
00538           case USB_ENDPOINT_DESCRIPTOR_TYPE:
00539             if (ifn == SetupPacket.wIndex.WB.L) {
00540               n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
00541               m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00542               if (alt == SetupPacket.wValue.WB.L) {
00543                 USB_EndPointMask |=  m;
00544                 USB_EndPointHalt &= ~m;
00545                 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
00546                 USB_EnableEP(n);
00547                 USB_ResetEP(n);
00548                 msk |= m;
00549               }
00550               else if ((alt == old) && ((msk & m) == 0)) {
00551                 USB_EndPointMask &= ~m;
00552                 USB_EndPointHalt &= ~m;
00553                 USB_DisableEP(n);
00554               }
00555             }
00556            break;
00557         }
00558       pD += pD->bLength;
00559       }
00560       break;
00561     default:
00562       return (FALSE);
00563   }
00564 
00565   return (set);
00566 }
00567 
00568 
00569 /*
00570  *  USB Endpoint 0 Event Callback
00571  *    Parameters:      event
00572  *    Return Value:    none
00573  */
00574  
00575 void USB_EndPoint0 (uint32_t event) {
00576 
00577   switch (event) {
00578     case USB_EVT_SETUP:
00579       USB_SetupStage();
00580       USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
00581       EP0Data.Count = SetupPacket.wLength;     /* Number of bytes to transfer */
00582       switch (SetupPacket.bmRequestType.BM.Type) {
00583 
00584         case REQUEST_STANDARD:
00585           switch (SetupPacket.bRequest) {
00586             case USB_REQUEST_GET_STATUS:
00587               if (!USB_ReqGetStatus()) {
00588                 goto stall_i;
00589               }
00590               USB_DataInStage();
00591               break;
00592 
00593             case USB_REQUEST_CLEAR_FEATURE:
00594               if (!USB_ReqSetClrFeature(0)) {
00595                 goto stall_i;
00596               }
00597               USB_StatusInStage();
00598 #if USB_FEATURE_EVENT
00599               USB_Feature_Event();
00600 #endif
00601               break;
00602 
00603             case USB_REQUEST_SET_FEATURE:
00604               if (!USB_ReqSetClrFeature(1)) {
00605                 goto stall_i;
00606               }
00607               USB_StatusInStage();
00608 #if USB_FEATURE_EVENT
00609               USB_Feature_Event();
00610 #endif
00611               break;
00612 
00613             case USB_REQUEST_SET_ADDRESS:
00614               if (!USB_ReqSetAddress()) {
00615                 goto stall_i;
00616               }
00617               USB_StatusInStage();
00618               break;
00619 
00620             case USB_REQUEST_GET_DESCRIPTOR:
00621               if (!USB_ReqGetDescriptor()) {
00622                 goto stall_i;
00623               }
00624               USB_DataInStage();
00625               break;
00626 
00627             case USB_REQUEST_SET_DESCRIPTOR:
00628 /*stall_o:*/  USB_SetStallEP(0x00);            /* not supported */
00629               EP0Data.Count = 0;
00630               break;
00631 
00632             case USB_REQUEST_GET_CONFIGURATION:
00633               if (!USB_ReqGetConfiguration()) {
00634                 goto stall_i;
00635               }
00636               USB_DataInStage();
00637               break;
00638 
00639             case USB_REQUEST_SET_CONFIGURATION:
00640               if (!USB_ReqSetConfiguration()) {
00641                 goto stall_i;
00642               }
00643               USB_StatusInStage();
00644 #if USB_CONFIGURE_EVENT
00645               USB_Configure_Event();
00646 #endif
00647               break;
00648 
00649             case USB_REQUEST_GET_INTERFACE:
00650               if (!USB_ReqGetInterface()) {
00651                 goto stall_i;
00652               }
00653               USB_DataInStage();
00654               break;
00655 
00656             case USB_REQUEST_SET_INTERFACE:
00657               if (!USB_ReqSetInterface()) {
00658                 goto stall_i;
00659               }
00660               USB_StatusInStage();
00661 #if USB_INTERFACE_EVENT
00662               USB_Interface_Event();
00663 #endif
00664               break;
00665 
00666             default:
00667               goto stall_i;
00668           }
00669           break;  /* end case REQUEST_STANDARD */
00670 
00671 #if USB_CLASS
00672         case REQUEST_CLASS:
00673           switch (SetupPacket.bmRequestType.BM.Recipient) {
00674 
00675             case REQUEST_TO_DEVICE:
00676               goto stall_i;                                              /* not supported */
00677 
00678             case REQUEST_TO_INTERFACE:
00679 #if USB_HID
00680               if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {           /* IF number correct? */
00681                 switch (SetupPacket.bRequest) {
00682                   case HID_REQUEST_GET_REPORT:
00683                     if (HID_GetReport()) {
00684                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00685                       USB_DataInStage();                                 /* send requested data */
00686                       goto setup_class_ok;
00687                     }
00688                     break;
00689                   case HID_REQUEST_SET_REPORT:
00690                     EP0Data.pData = EP0Buf;                              /* data to be received */ 
00691                     goto setup_class_ok;
00692                   case HID_REQUEST_GET_IDLE:
00693                     if (HID_GetIdle()) {
00694                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00695                       USB_DataInStage();                                 /* send requested data */
00696                       goto setup_class_ok;
00697                     }
00698                     break;
00699                   case HID_REQUEST_SET_IDLE:
00700                     if (HID_SetIdle()) {
00701                       USB_StatusInStage();                               /* send Acknowledge */
00702                       goto setup_class_ok;
00703                     }
00704                     break;
00705                   case HID_REQUEST_GET_PROTOCOL:
00706                     if (HID_GetProtocol()) {
00707                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00708                       USB_DataInStage();                                 /* send requested data */
00709                       goto setup_class_ok;
00710                     }
00711                     break;
00712                   case HID_REQUEST_SET_PROTOCOL:
00713                     if (HID_SetProtocol()) {
00714                       USB_StatusInStage();                               /* send Acknowledge */
00715                       goto setup_class_ok;
00716                     }
00717                     break;
00718                 }
00719               }
00720 #endif  /* USB_HID */
00721 #if USB_MSC
00722               if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {           /* IF number correct? */
00723                 switch (SetupPacket.bRequest) {
00724                   case MSC_REQUEST_RESET:
00725                     if ((SetupPacket.wValue.W == 0) &&                     /* RESET with invalid parameters -> STALL */
00726                         (SetupPacket.wLength  == 0)) {
00727                       if (MSC_Reset()) {
00728                         USB_StatusInStage();
00729                         goto setup_class_ok;
00730                       }
00731                     }
00732                     break;
00733                   case MSC_REQUEST_GET_MAX_LUN:
00734                     if ((SetupPacket.wValue.W == 0) &&                     /* GET_MAX_LUN with invalid parameters -> STALL */
00735                         (SetupPacket.wLength  == 1)) { 
00736                       if (MSC_GetMaxLUN()) {
00737                         EP0Data.pData = EP0Buf;
00738                         USB_DataInStage();
00739                         goto setup_class_ok;
00740                       }
00741                     }
00742                     break;
00743                 }
00744               }
00745 #endif  /* USB_MSC */
00746 #if USB_AUDIO
00747               if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||       /* IF number correct? */
00748                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
00749                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
00750                 switch (SetupPacket.bRequest) {
00751                   case AUDIO_REQUEST_GET_CUR:
00752                   case AUDIO_REQUEST_GET_MIN:
00753                   case AUDIO_REQUEST_GET_MAX:
00754                   case AUDIO_REQUEST_GET_RES:
00755                     if (ADC_IF_GetRequest()) {
00756                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00757                       USB_DataInStage();                                 /* send requested data */
00758                       goto setup_class_ok;
00759                     }
00760                     break;
00761                   case AUDIO_REQUEST_SET_CUR:
00762 //                case AUDIO_REQUEST_SET_MIN:
00763 //                case AUDIO_REQUEST_SET_MAX:
00764 //                case AUDIO_REQUEST_SET_RES:
00765                     EP0Data.pData = EP0Buf;                              /* data to be received */ 
00766                     goto setup_class_ok;
00767                 }
00768               }
00769 #endif  /* USB_AUDIO */
00770 #if USB_CDC
00771               if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  ||       /* IF number correct? */
00772                   (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
00773                 switch (SetupPacket.bRequest) {
00774                   case CDC_SEND_ENCAPSULATED_COMMAND:
00775                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
00776                     goto setup_class_ok;
00777                   case CDC_GET_ENCAPSULATED_RESPONSE:
00778                     if (CDC_GetEncapsulatedResponse()) {
00779                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00780                       USB_DataInStage();                                 /* send requested data */
00781                       goto setup_class_ok;
00782                     }
00783                     break;
00784                   case CDC_SET_COMM_FEATURE:
00785                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
00786                     goto setup_class_ok;
00787                   case CDC_GET_COMM_FEATURE:
00788                     if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
00789                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00790                       USB_DataInStage();                                 /* send requested data */
00791                       goto setup_class_ok;
00792                     }
00793                     break;
00794                   case CDC_CLEAR_COMM_FEATURE:
00795                     if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
00796                       USB_StatusInStage();                               /* send Acknowledge */
00797                       goto setup_class_ok;
00798                     }
00799                     break;
00800                   case CDC_SET_LINE_CODING:
00801                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
00802                     goto setup_class_ok;
00803                   case CDC_GET_LINE_CODING:
00804                     if (CDC_GetLineCoding()) {
00805                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00806                       USB_DataInStage();                                 /* send requested data */
00807                       goto setup_class_ok;
00808                     }
00809                     break;
00810                   case CDC_SET_CONTROL_LINE_STATE:
00811                     if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
00812                       USB_StatusInStage();                               /* send Acknowledge */
00813                       goto setup_class_ok;
00814                     }
00815                     break;
00816                   case CDC_SEND_BREAK:
00817                     if (CDC_SendBreak(SetupPacket.wValue.W)) {
00818                       USB_StatusInStage();                               /* send Acknowledge */
00819                       goto setup_class_ok;
00820                     }
00821                     break;
00822                 }
00823               }
00824 #endif  /* USB_CDC */
00825               goto stall_i;                                              /* not supported */
00826               /* end case REQUEST_TO_INTERFACE */
00827 
00828             case REQUEST_TO_ENDPOINT:
00829 #if USB_AUDIO
00830               switch (SetupPacket.bRequest) {
00831                 case AUDIO_REQUEST_GET_CUR:
00832                 case AUDIO_REQUEST_GET_MIN:
00833                 case AUDIO_REQUEST_GET_MAX:
00834                 case AUDIO_REQUEST_GET_RES:
00835                   if (ADC_EP_GetRequest()) {
00836                     EP0Data.pData = EP0Buf;                              /* point to data to be sent */
00837                     USB_DataInStage();                                   /* send requested data */
00838                     goto setup_class_ok;
00839                   }
00840                   break;
00841                 case AUDIO_REQUEST_SET_CUR:
00842 //              case AUDIO_REQUEST_SET_MIN:
00843 //              case AUDIO_REQUEST_SET_MAX:
00844 //              case AUDIO_REQUEST_SET_RES:
00845                   EP0Data.pData = EP0Buf;                                /* data to be received */ 
00846                   goto setup_class_ok;
00847               }
00848 #endif  /* USB_AUDIO */
00849               goto stall_i;
00850               /* end case REQUEST_TO_ENDPOINT */
00851 
00852             default:
00853               goto stall_i;
00854           }
00855 setup_class_ok:                                                          /* request finished successfully */
00856           break;  /* end case REQUEST_CLASS */
00857 #endif  /* USB_CLASS */
00858 
00859 #if USB_VENDOR
00860         case REQUEST_VENDOR:
00861           switch (SetupPacket.bmRequestType.BM.Recipient) {
00862 
00863             case REQUEST_TO_DEVICE:
00864               if (!USB_ReqVendorDev(TRUE)) {
00865                 goto stall_i;                                            /* not supported */               
00866               }
00867               break;
00868 
00869             case REQUEST_TO_INTERFACE:
00870               if (!USB_ReqVendorIF(TRUE)) {
00871                 goto stall_i;                                            /* not supported */               
00872               }
00873               break;
00874 
00875             case REQUEST_TO_ENDPOINT:
00876               if (!USB_ReqVendorEP(TRUE)) {
00877                 goto stall_i;                                            /* not supported */               
00878               }
00879               break;
00880 
00881             default:
00882               goto stall_i;
00883           }
00884 
00885           if (SetupPacket.wLength) {
00886             if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
00887               USB_DataInStage();
00888             }
00889           } else {
00890             USB_StatusInStage();
00891           }
00892 
00893           break;  /* end case REQUEST_VENDOR */ 
00894 #endif  /* USB_VENDOR */
00895 
00896         default:
00897 stall_i:  USB_SetStallEP(0x80);
00898           EP0Data.Count = 0;
00899           break;
00900       }
00901       break;  /* end case USB_EVT_SETUP */
00902 
00903     case USB_EVT_OUT:
00904       if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
00905         if (EP0Data.Count) {                                             /* still data to receive ? */
00906           USB_DataOutStage();                                            /* receive data */
00907           if (EP0Data.Count == 0) {                                      /* data complete ? */
00908             switch (SetupPacket.bmRequestType.BM.Type) {
00909 
00910               case REQUEST_STANDARD:
00911                 goto stall_i;                                            /* not supported */
00912 
00913 #if (USB_CLASS) 
00914               case REQUEST_CLASS:
00915                 switch (SetupPacket.bmRequestType.BM.Recipient) {
00916                   case REQUEST_TO_DEVICE:
00917                     goto stall_i;                                        /* not supported */
00918 
00919                   case REQUEST_TO_INTERFACE:
00920 #if USB_HID
00921                     if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {     /* IF number correct? */
00922                       switch (SetupPacket.bRequest) {
00923                         case HID_REQUEST_SET_REPORT:
00924                           if (HID_SetReport()) {
00925                             USB_StatusInStage();                         /* send Acknowledge */
00926                             goto out_class_ok;
00927                           }
00928                           break;
00929                       }
00930                     }
00931 #endif  /* USB_HID */  
00932 #if USB_AUDIO
00933                     if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  || /* IF number correct? */
00934                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
00935                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
00936                       switch (SetupPacket.bRequest) {
00937                         case AUDIO_REQUEST_SET_CUR:
00938 //                      case AUDIO_REQUEST_SET_MIN:
00939 //                      case AUDIO_REQUEST_SET_MAX:
00940 //                      case AUDIO_REQUEST_SET_RES:
00941                           if (ADC_IF_SetRequest()) {
00942                             USB_StatusInStage();                         /* send Acknowledge */
00943                             goto out_class_ok;
00944                           }
00945                           break;
00946                       }
00947                     }
00948 #endif  /* USB_AUDIO */
00949 #if USB_CDC
00950                     if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  || /* IF number correct? */
00951                         (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
00952                       switch (SetupPacket.bRequest) {
00953                         case CDC_SEND_ENCAPSULATED_COMMAND:
00954                           if (CDC_SendEncapsulatedCommand()) {
00955                             USB_StatusInStage();                         /* send Acknowledge */
00956                             goto out_class_ok;
00957                           }
00958                           break;
00959                         case CDC_SET_COMM_FEATURE:
00960                           if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
00961                             USB_StatusInStage();                         /* send Acknowledge */
00962                             goto out_class_ok;
00963                           }
00964                           break;
00965                         case CDC_SET_LINE_CODING:
00966                           if (CDC_SetLineCoding()) {
00967                             USB_StatusInStage();                         /* send Acknowledge */
00968                             goto out_class_ok;
00969                           }
00970                           break;
00971                       }
00972                     } 
00973 #endif  /* USB_CDC */
00974                     goto stall_i;
00975                     /* end case REQUEST_TO_INTERFACE */
00976 
00977                   case REQUEST_TO_ENDPOINT:
00978 #if USB_AUDIO
00979                     switch (SetupPacket.bRequest) {
00980                       case AUDIO_REQUEST_SET_CUR:
00981 //                    case AUDIO_REQUEST_SET_MIN:
00982 //                    case AUDIO_REQUEST_SET_MAX:
00983 //                    case AUDIO_REQUEST_SET_RES:
00984                         if (ADC_EP_SetRequest()) {
00985                           USB_StatusInStage();                           /* send Acknowledge */
00986                           goto out_class_ok;
00987                         }
00988                         break;
00989                     }
00990 #endif  /* USB_AUDIO */
00991                     goto stall_i;
00992                     /* end case REQUEST_TO_ENDPOINT */
00993 
00994                   default:
00995                     goto stall_i;
00996                 }
00997 out_class_ok:                                                            /* request finished successfully */
00998                 break; /* end case REQUEST_CLASS */
00999 #endif  /* USB_CLASS */
01000 
01001 #if USB_VENDOR
01002               case REQUEST_VENDOR:
01003                 switch (SetupPacket.bmRequestType.BM.Recipient) {
01004       
01005                   case REQUEST_TO_DEVICE:
01006                     if (!USB_ReqVendorDev(FALSE)) {
01007                       goto stall_i;                                      /* not supported */               
01008                     }
01009                     break;
01010       
01011                   case REQUEST_TO_INTERFACE:
01012                     if (!USB_ReqVendorIF(FALSE)) {
01013                       goto stall_i;                                      /* not supported */               
01014                     }
01015                     break;
01016       
01017                   case REQUEST_TO_ENDPOINT:
01018                     if (!USB_ReqVendorEP(FALSE)) {
01019                       goto stall_i;                                      /* not supported */               
01020                     }
01021                     break;
01022       
01023                   default:
01024                     goto stall_i;
01025                 }
01026       
01027                 USB_StatusInStage();
01028       
01029                 break;  /* end case REQUEST_VENDOR */ 
01030 #endif  /* USB_VENDOR */
01031 
01032               default:
01033                 goto stall_i;
01034             }
01035           }
01036         }
01037       } else {
01038         USB_StatusOutStage();                                            /* receive Acknowledge */
01039       }
01040       break;  /* end case USB_EVT_OUT */
01041 
01042     case USB_EVT_IN :
01043       if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
01044         USB_DataInStage();                                               /* send data */
01045       } else {
01046         if (USB_DeviceAddress & 0x80) {
01047           USB_DeviceAddress &= 0x7F;
01048           USB_SetAddress(USB_DeviceAddress);
01049         }
01050       }
01051       break;  /* end case USB_EVT_IN */
01052 
01053     case USB_EVT_OUT_STALL:
01054       USB_ClrStallEP(0x00);
01055       break;
01056 
01057     case USB_EVT_IN_STALL:
01058       USB_ClrStallEP(0x80);
01059       break;
01060 
01061   }
01062 }