1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: USB Core Module
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.
16 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17 *----------------------------------------------------------------------------
19 * V1.20 Added vendor specific requests
20 * Changed string descriptor handling
22 * V1.00 Initial Version
23 *----------------------------------------------------------------------------*/
24 #include "lpc_types.h"
62 #if defined ( __CC_ARM )
63 #pragma diag_suppress 111,1441
66 #if defined ( __GNUC__ )
67 #define __packed __attribute__((__packed__))
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];
79 uint8_t EP0Buf[USB_MAX_PACKET0];
84 USB_SETUP_PACKET SetupPacket;
93 void USB_ResetCore (void) {
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;
105 * USB Request - Setup Stage
106 * Parameters: None (global SetupPacket)
110 void USB_SetupStage (void) {
111 USB_ReadEP(0x00, (uint8_t *)&SetupPacket);
116 * USB Request - Data In Stage
117 * Parameters: None (global EP0Data)
121 void USB_DataInStage (void) {
124 if (EP0Data.Count > USB_MAX_PACKET0) {
125 cnt = USB_MAX_PACKET0;
129 cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
130 EP0Data.pData += cnt;
131 EP0Data.Count -= cnt;
136 * USB Request - Data Out Stage
137 * Parameters: None (global EP0Data)
141 void USB_DataOutStage (void) {
144 cnt = USB_ReadEP(0x00, EP0Data.pData);
145 EP0Data.pData += cnt;
146 EP0Data.Count -= cnt;
151 * USB Request - Status In Stage
156 void USB_StatusInStage (void) {
157 USB_WriteEP(0x80, NULL, 0);
162 * USB Request - Status Out Stage
167 void USB_StatusOutStage (void) {
168 USB_ReadEP(0x00, EP0Buf);
173 * Get Status USB Request
174 * Parameters: None (global SetupPacket)
175 * Return Value: TRUE - Success, FALSE - Error
178 #if defined ( __IAR_SYSTEMS_ICC__ )
179 inline uint32_t USB_ReqGetStatus (void) {
181 __inline uint32_t USB_ReqGetStatus (void) {
185 switch (SetupPacket.bmRequestType.BM.Recipient) {
186 case REQUEST_TO_DEVICE:
187 EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
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;
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;
217 * Set/Clear Feature USB Request
218 * Parameters: sc: 0 - Clear, 1 - Set
219 * (global SetupPacket)
220 * Return Value: TRUE - Success, FALSE - Error
223 #if defined ( __IAR_SYSTEMS_ICC__ )
224 inline uint32_t USB_ReqSetClrFeature (uint32_t sc) {
226 __inline uint32_t USB_ReqSetClrFeature (uint32_t sc) {
230 switch (SetupPacket.bmRequestType.BM.Recipient) {
231 case REQUEST_TO_DEVICE:
232 if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
235 USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP;
237 USB_WakeUpCfg(FALSE);
238 USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
244 case REQUEST_TO_INTERFACE:
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) {
253 USB_EndPointHalt |= m;
255 if ((USB_EndPointStall & m) != 0) {
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));
267 USB_EndPointHalt &= ~m;
284 * Set Address USB Request
285 * Parameters: None (global SetupPacket)
286 * Return Value: TRUE - Success, FALSE - Error
289 #if defined ( __IAR_SYSTEMS_ICC__ )
290 inline uint32_t USB_ReqSetAddress (void) {
292 __inline uint32_t USB_ReqSetAddress (void) {
294 switch (SetupPacket.bmRequestType.BM.Recipient) {
295 case REQUEST_TO_DEVICE:
296 USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
306 * Get Descriptor USB Request
307 * Parameters: None (global SetupPacket)
308 * Return Value: TRUE - Success, FALSE - Error
311 #if defined ( __IAR_SYSTEMS_ICC__ )
312 inline uint32_t USB_ReqGetDescriptor (void) {
314 __inline uint32_t USB_ReqGetDescriptor (void) {
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;
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;
333 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
337 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
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;
346 if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
350 len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
356 case REQUEST_TO_INTERFACE:
357 switch (SetupPacket.wValue.WB.H) {
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 */
363 EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
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 */
370 EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
371 len = HID_ReportDescSize;
373 case HID_PHYSICAL_DESCRIPTOR_TYPE:
374 return (FALSE); /* HID Physical Descriptor is not supported */
384 if (EP0Data.Count > len) {
393 * Get Configuration USB Request
394 * Parameters: None (global SetupPacket)
395 * Return Value: TRUE - Success, FALSE - Error
398 #if defined ( __IAR_SYSTEMS_ICC__ )
399 inline uint32_t USB_ReqGetConfiguration (void) {
401 __inline uint32_t USB_ReqGetConfiguration (void) {
403 switch (SetupPacket.bmRequestType.BM.Recipient) {
404 case REQUEST_TO_DEVICE:
405 EP0Data.pData = &USB_Configuration;
415 * Set Configuration USB Request
416 * Parameters: None (global SetupPacket)
417 * Return Value: TRUE - Success, FALSE - Error
420 #if defined ( __IAR_SYSTEMS_ICC__ )
421 inline uint32_t USB_ReqSetConfiguration (void) {
423 __inline uint32_t USB_ReqSetConfiguration (void) {
425 USB_COMMON_DESCRIPTOR *pD;
430 switch (SetupPacket.bmRequestType.BM.Recipient) {
431 case REQUEST_TO_DEVICE:
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;
444 for (n = 1; n < 16; n++) {
445 if (USB_EndPointMask & (1 << n)) {
448 if (USB_EndPointMask & ((1 << 16) << n)) {
449 USB_DisableEP(n | 0x80);
452 USB_EndPointMask = 0x00010001;
453 USB_EndPointHalt = 0x00000000;
454 USB_EndPointStall= 0x00000000;
456 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
457 USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED;
459 USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
462 // (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
464 tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
465 pD = (USB_COMMON_DESCRIPTOR *)tmp;
469 case USB_INTERFACE_DESCRIPTOR_TYPE:
470 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
472 case USB_ENDPOINT_DESCRIPTOR_TYPE:
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);
483 // (uint8_t *)pD += pD->bLength;
486 pD = (USB_COMMON_DESCRIPTOR *)tmp;
490 USB_Configuration = 0;
491 for (n = 1; n < 16; n++) {
492 if (USB_EndPointMask & (1 << n)) {
495 if (USB_EndPointMask & ((1 << 16) << n)) {
496 USB_DisableEP(n | 0x80);
499 USB_EndPointMask = 0x00010001;
500 USB_EndPointHalt = 0x00000000;
501 USB_EndPointStall = 0x00000000;
502 USB_Configure(FALSE);
505 if (USB_Configuration != SetupPacket.wValue.WB.L) {
517 * Get Interface USB Request
518 * Parameters: None (global SetupPacket)
519 * Return Value: TRUE - Success, FALSE - Error
522 #if defined ( __IAR_SYSTEMS_ICC__ )
523 inline uint32_t USB_ReqGetInterface (void) {
525 __inline uint32_t USB_ReqGetInterface (void) {
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;
543 * Set Interface USB Request
544 * Parameters: None (global SetupPacket)
545 * Return Value: TRUE - Success, FALSE - Error
547 #if defined ( __IAR_SYSTEMS_ICC__ )
548 inline uint32_t USB_ReqSetInterface (void) {
550 __inline uint32_t USB_ReqSetInterface (void) {
552 USB_COMMON_DESCRIPTOR *pD;
553 uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
558 switch (SetupPacket.bmRequestType.BM.Recipient) {
559 case REQUEST_TO_INTERFACE:
560 if (USB_Configuration == 0) return (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;
569 tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
570 pD = (USB_COMMON_DESCRIPTOR *)tmp;
575 case USB_INTERFACE_DESCRIPTOR_TYPE:
576 ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
577 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
579 if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
581 old = USB_AltSetting[ifn];
582 USB_AltSetting[ifn] = (uint8_t)alt;
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);
597 else if ((alt == old) && ((msk & m) == 0)) {
598 USB_EndPointMask &= ~m;
599 USB_EndPointHalt &= ~m;
605 // (uint8_t *)pD += pD->bLength;
608 pD = (USB_COMMON_DESCRIPTOR *)tmp;
620 * USB Endpoint 0 Event Callback
625 void USB_EndPoint0 (uint32_t event) {
630 USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
631 EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */
632 switch (SetupPacket.bmRequestType.BM.Type) {
634 case REQUEST_STANDARD:
635 switch (SetupPacket.bRequest) {
636 case USB_REQUEST_GET_STATUS:
637 if (!USB_ReqGetStatus()) {
643 case USB_REQUEST_CLEAR_FEATURE:
644 if (!USB_ReqSetClrFeature(0)) {
648 #if USB_FEATURE_EVENT
653 case USB_REQUEST_SET_FEATURE:
654 if (!USB_ReqSetClrFeature(1)) {
658 #if USB_FEATURE_EVENT
663 case USB_REQUEST_SET_ADDRESS:
664 if (!USB_ReqSetAddress()) {
670 case USB_REQUEST_GET_DESCRIPTOR:
671 if (!USB_ReqGetDescriptor()) {
677 case USB_REQUEST_SET_DESCRIPTOR:
678 /*stall_o:*/ USB_SetStallEP(0x00); /* not supported */
682 case USB_REQUEST_GET_CONFIGURATION:
683 if (!USB_ReqGetConfiguration()) {
689 case USB_REQUEST_SET_CONFIGURATION:
690 if (!USB_ReqSetConfiguration()) {
694 #if USB_CONFIGURE_EVENT
695 USB_Configure_Event();
699 case USB_REQUEST_GET_INTERFACE:
700 if (!USB_ReqGetInterface()) {
706 case USB_REQUEST_SET_INTERFACE:
707 if (!USB_ReqSetInterface()) {
711 #if USB_INTERFACE_EVENT
712 USB_Interface_Event();
719 break; /* end case REQUEST_STANDARD */
723 switch (SetupPacket.bmRequestType.BM.Recipient) {
725 case REQUEST_TO_DEVICE:
726 goto stall_i; /* not supported */
728 case REQUEST_TO_INTERFACE:
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 */
739 case HID_REQUEST_SET_REPORT:
740 EP0Data.pData = EP0Buf; /* data to be received */
742 case HID_REQUEST_GET_IDLE:
744 EP0Data.pData = EP0Buf; /* point to data to be sent */
745 USB_DataInStage(); /* send requested data */
749 case HID_REQUEST_SET_IDLE:
751 USB_StatusInStage(); /* send Acknowledge */
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 */
762 case HID_REQUEST_SET_PROTOCOL:
763 if (HID_SetProtocol()) {
764 USB_StatusInStage(); /* send Acknowledge */
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)) {
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;
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 */
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 */
819 #endif /* USB_AUDIO */
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 */
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 */
834 case CDC_SET_COMM_FEATURE:
835 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
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 */
844 case CDC_CLEAR_COMM_FEATURE:
845 if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
846 USB_StatusInStage(); /* send Acknowledge */
850 case CDC_SET_LINE_CODING:
851 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
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 */
860 case CDC_SET_CONTROL_LINE_STATE:
861 if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
862 USB_StatusInStage(); /* send Acknowledge */
867 if (CDC_SendBreak(SetupPacket.wValue.W)) {
868 USB_StatusInStage(); /* send Acknowledge */
875 goto stall_i; /* not supported */
876 /* end case REQUEST_TO_INTERFACE */
878 case REQUEST_TO_ENDPOINT:
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 */
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 */
898 #endif /* USB_AUDIO */
900 /* end case REQUEST_TO_ENDPOINT */
905 setup_class_ok: /* request finished successfully */
906 break; /* end case REQUEST_CLASS */
907 #endif /* USB_CLASS */
911 switch (SetupPacket.bmRequestType.BM.Recipient) {
913 case REQUEST_TO_DEVICE:
914 if (!USB_ReqVendorDev(TRUE)) {
915 goto stall_i; /* not supported */
919 case REQUEST_TO_INTERFACE:
920 if (!USB_ReqVendorIF(TRUE)) {
921 goto stall_i; /* not supported */
925 case REQUEST_TO_ENDPOINT:
926 if (!USB_ReqVendorEP(TRUE)) {
927 goto stall_i; /* not supported */
935 if (SetupPacket.wLength) {
936 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
943 break; /* end case REQUEST_VENDOR */
944 #endif /* USB_VENDOR */
947 stall_i: USB_SetStallEP(0x80);
951 break; /* end case USB_EVT_SETUP */
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) {
960 case REQUEST_STANDARD:
961 goto stall_i; /* not supported */
965 switch (SetupPacket.bmRequestType.BM.Recipient) {
966 case REQUEST_TO_DEVICE:
967 goto stall_i; /* not supported */
969 case REQUEST_TO_INTERFACE:
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 */
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 */
998 #endif /* USB_AUDIO */
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 */
1009 case CDC_SET_COMM_FEATURE:
1010 if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
1011 USB_StatusInStage(); /* send Acknowledge */
1015 case CDC_SET_LINE_CODING:
1016 if (CDC_SetLineCoding()) {
1017 USB_StatusInStage(); /* send Acknowledge */
1023 #endif /* USB_CDC */
1025 /* end case REQUEST_TO_INTERFACE */
1027 case REQUEST_TO_ENDPOINT:
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 */
1040 #endif /* USB_AUDIO */
1042 /* end case REQUEST_TO_ENDPOINT */
1047 out_class_ok: /* request finished successfully */
1048 break; /* end case REQUEST_CLASS */
1049 #endif /* USB_CLASS */
1052 case REQUEST_VENDOR:
1053 switch (SetupPacket.bmRequestType.BM.Recipient) {
1055 case REQUEST_TO_DEVICE:
1056 if (!USB_ReqVendorDev(FALSE)) {
1057 goto stall_i; /* not supported */
1061 case REQUEST_TO_INTERFACE:
1062 if (!USB_ReqVendorIF(FALSE)) {
1063 goto stall_i; /* not supported */
1067 case REQUEST_TO_ENDPOINT:
1068 if (!USB_ReqVendorEP(FALSE)) {
1069 goto stall_i; /* not supported */
1077 USB_StatusInStage();
1079 break; /* end case REQUEST_VENDOR */
1080 #endif /* USB_VENDOR */
1088 USB_StatusOutStage(); /* receive Acknowledge */
1090 break; /* end case USB_EVT_OUT */
1093 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
1094 USB_DataInStage(); /* send data */
1096 if (USB_DeviceAddress & 0x80) {
1097 USB_DeviceAddress &= 0x7F;
1098 USB_SetAddress(USB_DeviceAddress);
1101 break; /* end case USB_EVT_IN */
1103 case USB_EVT_OUT_STALL:
1104 USB_ClrStallEP(0x00);
1107 case USB_EVT_IN_STALL:
1108 USB_ClrStallEP(0x80);