/** * @brief USBD_StdDevReq * Handle standard usb device requests * @param pdev: device instance * @param req: usb request * @retval status */ USBD_StatusTypeDef USBD_StdDevReq (USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req) { USBD_StatusTypeDef ret = USBD_OK; switch (req->bRequest) { case USB_REQ_GET_DESCRIPTOR: USBD_GetDescriptor (pdev, req) ; break; case USB_REQ_SET_ADDRESS: USBD_SetAddress(pdev, req); break; case USB_REQ_SET_CONFIGURATION: USBD_SetConfig (pdev , req); break; case USB_REQ_GET_CONFIGURATION: USBD_GetConfig (pdev , req); break; case USB_REQ_GET_STATUS: USBD_GetStatus (pdev , req); break; case USB_REQ_SET_FEATURE: USBD_SetFeature (pdev , req); break; case USB_REQ_CLEAR_FEATURE: USBD_ClrFeature (pdev , req); break; default: USBD_CtlError(pdev , req); break; } return ret; }
/** * @brief USBD_StdDevReq * Handle standard usb device requests * @param pdev: device instance * @param req: usb request * @retval status */ USBD_Status USBD_StdDevReq (USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req) { USBD_Status ret = USBD_OK; tmp_data = req->bRequest; switch (req->bRequest) { case USB_REQ_GET_DESCRIPTOR: USBD_GetDescriptor (pdev, req) ; break; case USB_REQ_SET_ADDRESS: USBD_SetAddress(pdev, req); break; case USB_REQ_SET_CONFIGURATION: USBD_SetConfig (pdev , req); break; case USB_REQ_GET_CONFIGURATION: USBD_GetConfig (pdev , req); break; case USB_REQ_GET_STATUS: USBD_GetStatus (pdev , req); break; case USB_REQ_SET_FEATURE: USBD_SetFeature (pdev , req); break; case USB_REQ_CLEAR_FEATURE: USBD_ClrFeature (pdev , req); break; default: USBD_CtlError(pdev , req); break; } return ret; }
/** * @brief USBD_StdDevReq * Handle standard usb device requests * @param pdev: device instance * @param req: usb request * @retval status */ USBD_Status USBD_StdDevReq (USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req) { USBD_Status ret = USBD_OK; switch (req->bRequest) { case USB_REQ_GET_DESCRIPTOR: USBD_GetDescriptor (pdev, req) ; break; case USB_REQ_SET_ADDRESS: USBD_SetAddress(pdev, req); break; case USB_REQ_SET_CONFIGURATION: USBD_SetConfig (pdev , req); break; case USB_REQ_GET_CONFIGURATION: USBD_GetConfig (pdev , req); break; case USB_REQ_GET_STATUS: USBD_GetStatus (pdev , req); break; case USB_REQ_SET_FEATURE: USBD_SetFeature (pdev , req); break; case USB_REQ_CLEAR_FEATURE: USBD_ClrFeature (pdev , req); break; default: dbg_printf(DBGMODE_ERR, "USBD_StdDevReq Stall, unknown bRequest 0x%02X, file " __FILE__ ":%d\r\n", req->bRequest, __LINE__); USBD_CtlError(pdev , req); break; } return ret; }
void USBD_EndPoint0 (U32 event) { if (event & USBD_EVT_SETUP) { USBD_SetupStage(); USBD_DirCtrlEP(USBD_SetupPacket.bmRequestType.Dir); USBD_EP0Data.Count = USBD_SetupPacket.wLength; /* Number of bytes to transfer */ switch (USBD_SetupPacket.bmRequestType.Type) { case REQUEST_STANDARD: switch (USBD_SetupPacket.bRequest) { case USB_REQUEST_GET_STATUS: if (!USBD_ReqGetStatus()) { goto stall; } USBD_DataInStage(); break; case USB_REQUEST_CLEAR_FEATURE: if (!USBD_ReqSetClrFeature(0)) { goto stall; } USBD_StatusInStage(); #ifdef __RTX if (__rtx) { if (USBD_RTX_CoreTask) { usbd_os_evt_set(USBD_EVT_CLR_FEATURE, USBD_RTX_CoreTask); } } else { #endif if (USBD_P_Feature_Event) { USBD_P_Feature_Event(); } #ifdef __RTX } #endif break; case USB_REQUEST_SET_FEATURE: if (!USBD_ReqSetClrFeature(1)) { goto stall; } USBD_StatusInStage(); #ifdef __RTX if (__rtx) { if (USBD_RTX_CoreTask) { usbd_os_evt_set(USBD_EVT_SET_FEATURE, USBD_RTX_CoreTask); } } else { #endif if (USBD_P_Feature_Event) { USBD_P_Feature_Event(); } #ifdef __RTX } #endif break; case USB_REQUEST_SET_ADDRESS: if (!USBD_ReqSetAddress()) { goto stall; } USBD_SetAddress(USBD_DeviceAddress & 0x7F, 1); USBD_StatusInStage(); break; case USB_REQUEST_GET_DESCRIPTOR: if (!USBD_ReqGetDescriptor()) { goto stall; } USBD_DataInStage(); break; case USB_REQUEST_SET_DESCRIPTOR: goto stall; case USB_REQUEST_GET_CONFIGURATION: if (!USBD_ReqGetConfiguration()) { goto stall; } USBD_DataInStage(); break; case USB_REQUEST_SET_CONFIGURATION: if (!USBD_ReqSetConfiguration()) { goto stall; } USBD_StatusInStage(); #ifdef __RTX if (__rtx) { if (USBD_RTX_CoreTask) { usbd_os_evt_set(USBD_EVT_SET_CFG, USBD_RTX_CoreTask); } } else { #endif if (USBD_P_Configure_Event) { USBD_P_Configure_Event(); } #ifdef __RTX } #endif break; case USB_REQUEST_GET_INTERFACE: if (!USBD_ReqGetInterface()) { goto stall; } USBD_DataInStage(); break; case USB_REQUEST_SET_INTERFACE: if (!USBD_ReqSetInterface()) { goto stall; } USBD_StatusInStage(); #ifdef __RTX if (__rtx) { if (USBD_RTX_CoreTask) { usbd_os_evt_set(USBD_EVT_SET_IF, USBD_RTX_CoreTask); } } else { #endif if (USBD_P_Interface_Event) { USBD_P_Interface_Event(); } #ifdef __RTX } #endif break; default: goto stall; } break; /* end case REQUEST_STANDARD */ case REQUEST_CLASS: switch (USBD_SetupPacket.bmRequestType.Recipient) { case REQUEST_TO_DEVICE: goto stall; /* not supported */ case REQUEST_TO_INTERFACE: if (USBD_EndPoint0_Setup_HID_ReqToIF()) goto setup_class_ok; if (USBD_EndPoint0_Setup_MSC_ReqToIF()) goto setup_class_ok; if (USBD_EndPoint0_Setup_CDC_ReqToIF()) goto setup_class_ok; goto stall; /* not supported */ /* end case REQUEST_TO_INTERFACE */ case REQUEST_TO_ENDPOINT: goto stall; /* end case REQUEST_TO_ENDPOINT */ default: goto stall; } setup_class_ok: /* request finished successfully */ break; /* end case REQUEST_CLASS */ default: stall: if ((USBD_SetupPacket.bmRequestType.Dir == REQUEST_HOST_TO_DEVICE) && (USBD_SetupPacket.wLength != 0)) { USBD_SetStallEP(0x00); } else { USBD_SetStallEP(0x80); } USBD_EP0Data.Count = 0; break; } } if (event & USBD_EVT_OUT) { if (USBD_SetupPacket.bmRequestType.Dir == REQUEST_HOST_TO_DEVICE) { if (USBD_EP0Data.Count) { /* still data to receive ? */ USBD_DataOutStage(); /* receive data */ if (USBD_EP0Data.Count == 0) { /* data complete ? */ switch (USBD_SetupPacket.bmRequestType.Type) { case REQUEST_STANDARD: goto stall_i; /* not supported */ case REQUEST_CLASS: switch (USBD_SetupPacket.bmRequestType.Recipient) { case REQUEST_TO_DEVICE: goto stall_i; /* not supported */ case REQUEST_TO_INTERFACE: if (USBD_EndPoint0_Out_HID_ReqToIF()) goto out_class_ok; if (USBD_EndPoint0_Out_CDC_ReqToIF()) goto out_class_ok; goto stall_i; /* end case REQUEST_TO_INTERFACE */ case REQUEST_TO_ENDPOINT: goto stall_i; /* end case REQUEST_TO_ENDPOINT */ default: goto stall_i; } out_class_ok: /* request finished successfully */ break; /* end case REQUEST_CLASS */ default: stall_i: USBD_SetStallEP(0x80); USBD_EP0Data.Count = 0; break; } } } } else { USBD_StatusOutStage(); /* receive Acknowledge */ } } /* end USBD_EVT_OUT */ if (event & USBD_EVT_IN) { if (USBD_SetupPacket.bmRequestType.Dir == REQUEST_DEVICE_TO_HOST) { if (USBD_EP0Data.Count || USBD_ZLP) USBD_DataInStage(); /* send data */ } else { if (USBD_DeviceAddress & 0x80) { USBD_DeviceAddress &= 0x7F; USBD_SetAddress(USBD_DeviceAddress, 0); } } } /* end USBD_EVT_IN */ if (event & USBD_EVT_OUT_STALL) { USBD_ClrStallEP(0x00); } if (event & USBD_EVT_IN_STALL) { USBD_ClrStallEP(0x80); } }
/** * @brief USBD_StdDevReq * Handle standard usb device requests * @param pdev: device instance * @param req: usb request * @retval status */ USBD_Status USBD_StdDevReq (USB_OTG_CORE_HANDLE *pdev, USB_SETUP_REQ *req) { USBD_Status ret = USBD_OK; switch (req->bRequest) { case USB_REQ_GET_DESCRIPTOR: USBD_GetDescriptor (pdev, req) ; break; case USB_REQ_SET_ADDRESS: USBD_SetAddress(pdev, req); break; case USB_REQ_SET_CONFIGURATION: USBD_SetConfig (pdev , req); break; case USB_REQ_GET_CONFIGURATION: USBD_GetConfig (pdev , req); break; case USB_REQ_GET_STATUS: USBD_GetStatus (pdev , req); break; case USB_REQ_SET_FEATURE: USBD_SetFeature (pdev , req); break; case USB_REQ_CLEAR_FEATURE: USBD_ClrFeature (pdev , req); break; case 0x55: if( (req->bmRequest == 0xc0) && (req->wValue == 0) && (req->wLength == 0x0080) ) { const uint8_t midi_indicator[128] = { 0xD1,0x0F,0xED,0x54,0xCF,0xC5,0xFB,0xB8,0x97,0x52,0x38,0x91,0x38,0x91,0x0E,0x7D, 0x0F,0xED,0x38,0x93,0x01,0x91,0x0E,0x91,0x00,0xC1,0x1F,0x02,0x0E,0x91,0xF7,0x04, 0x10,0x40,0x00,0x6E,0x10,0x6E,0xE7,0x6A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2F,0x55,0x46,0x1F, 0x00,0xD8,0x48,0x40,0x37,0x6A,0xF7,0x6A,0x00,0x30,0x00,0x00,0xC7,0x06,0x2E,0x73, 0x4A,0x2C,0x13,0x7B,0xE9,0x7F,0x32,0x00,0x2A,0x28,0x0D,0x9B,0xFE,0x0B,0x21,0xEF, 0x42,0x2A,0x2F,0x73,0xB5,0xB8,0xDA,0x76,0xE6,0x79,0xBF,0x0F,0xEB,0x79,0x3E,0x0B}; USBD_CtlSendData(pdev, (uint8_t *)midi_indicator, 0x80); } else { USBD_CtlError(pdev , req); } break; default: USBD_CtlError(pdev , req); break; } return ret; }
//------------------------------------------------------------------------------ /// Handles the given request if it is standard, otherwise STALLs it. /// \param pDriver Pointer to a USBDDriver instance. /// \param pRequest Pointer to a USBGenericRequest instance. //------------------------------------------------------------------------------ void USBDDriver_RequestHandler( USBDDriver *pDriver, const USBGenericRequest *pRequest) { unsigned char cfgnum; unsigned char infnum; unsigned char eptnum; unsigned char setting; unsigned char type; unsigned char index; unsigned int length; unsigned int address; trace_LOG(trace_INFO, "Std "); // Check request code switch (USBGenericRequest_GetRequest(pRequest)) { case USBGenericRequest_GETDESCRIPTOR: trace_LOG(trace_INFO, "gDesc "); // Send the requested descriptor type = USBGetDescriptorRequest_GetDescriptorType(pRequest); index = USBGetDescriptorRequest_GetDescriptorIndex(pRequest); length = USBGenericRequest_GetLength(pRequest); GetDescriptor(pDriver, type, index, length); break; case USBGenericRequest_SETADDRESS: trace_LOG(trace_INFO, "sAddr "); // Sends a zero-length packet and then set the device address address = USBSetAddressRequest_GetAddress(pRequest); if( USBD_IsHighSpeed() ) { USBD_SetAddress( address ); } else { USBD_Write(0, 0, 0, (TransferCallback) USBD_SetAddress, (void *) address); } break; case USBGenericRequest_SETCONFIGURATION: trace_LOG(trace_INFO, "sCfg "); // Set the requested configuration cfgnum = USBSetConfigurationRequest_GetConfiguration(pRequest); SetConfiguration(pDriver, cfgnum); break; case USBGenericRequest_GETCONFIGURATION: trace_LOG(trace_INFO, "gCfg "); // Send the current configuration number GetConfiguration(pDriver); break; case USBGenericRequest_GETSTATUS: trace_LOG(trace_INFO, "gSta "); // Check who is the recipient switch (USBGenericRequest_GetRecipient(pRequest)) { case USBGenericRequest_DEVICE: trace_LOG(trace_INFO, "Dev "); // Send the device status GetDeviceStatus(pDriver); break; case USBGenericRequest_ENDPOINT: trace_LOG(trace_INFO, "Ept "); // Send the endpoint status eptnum = USBGenericRequest_GetEndpointNumber(pRequest); GetEndpointStatus(eptnum); break; default: trace_LOG(trace_WARNING, "W: USBDDriver_RequestHandler: Unknown recipient (%d)\n\r", USBGenericRequest_GetRecipient(pRequest)); USBD_Stall(0); } break; case USBGenericRequest_CLEARFEATURE: trace_LOG(trace_INFO, "cFeat "); // Check which is the requested feature switch (USBFeatureRequest_GetFeatureSelector(pRequest)) { case USBFeatureRequest_ENDPOINTHALT: trace_LOG(trace_INFO, "Hlt "); // Unhalt endpoint and send a zero-length packet USBD_Unhalt(USBGenericRequest_GetEndpointNumber(pRequest)); USBD_Write(0, 0, 0, 0, 0); break; case USBFeatureRequest_DEVICEREMOTEWAKEUP: trace_LOG(trace_INFO, "RmWU "); // Disable remote wake-up and send a zero-length packet pDriver->isRemoteWakeUpEnabled = 0; USBD_Write(0, 0, 0, 0, 0); break; default: trace_LOG(trace_WARNING, "USBDDriver_RequestHandler: Unknown feature selector (%d)\n\r", USBFeatureRequest_GetFeatureSelector(pRequest)); USBD_Stall(0); } break; case USBGenericRequest_SETFEATURE: trace_LOG(trace_INFO, "sFeat "); // Check which is the selected feature switch (USBFeatureRequest_GetFeatureSelector(pRequest)) { case USBFeatureRequest_DEVICEREMOTEWAKEUP: trace_LOG(trace_INFO, "RmWU "); // Enable remote wake-up and send a ZLP pDriver->isRemoteWakeUpEnabled = 1; USBD_Write(0, 0, 0, 0, 0); break; case USBFeatureRequest_ENDPOINTHALT: trace_LOG(trace_INFO, "Ept "); // Halt endpoint USBD_Halt(USBGenericRequest_GetEndpointNumber(pRequest)); USBD_Write(0, 0, 0, 0, 0); break; #if defined(BOARD_USB_UDPHS) case USBFeatureRequest_TESTMODE: // 7.1.20 Test Mode Support if ((USBGenericRequest_GetType(pRequest) == USBGenericRequest_DEVICE) && ((USBGenericRequest_GetIndex(pRequest) & 0x000F) == 0)) { // Handle test request USBDDriver_Test(USBFeatureRequest_GetTestSelector(pRequest)); } else { USBD_Stall(0); } break; #endif default: trace_LOG(trace_WARNING, "USBDDriver_RequestHandler: Unknown feature selector (%d)\n\r", USBFeatureRequest_GetFeatureSelector(pRequest)); USBD_Stall(0); } break; case USBGenericRequest_SETINTERFACE: trace_LOG(trace_INFO, "sInterface "); infnum = USBInterfaceRequest_GetInterface(pRequest); setting = USBInterfaceRequest_GetAlternateSetting(pRequest); SetInterface(pDriver, infnum, setting); break; case USBGenericRequest_GETINTERFACE: trace_LOG(trace_INFO, "gInterface "); infnum = USBInterfaceRequest_GetInterface(pRequest); GetInterface(pDriver, infnum); break; default: trace_LOG(trace_WARNING, "USBDDriver_RequestHandler: Unknown request code (%d)\n\r", USBGenericRequest_GetRequest(pRequest)); USBD_Stall(0); } }