void _USB_DEVICE_CDC_ACMSetUpPacketHandler ( SYS_MODULE_INDEX iCDC , USB_DEVICE_CDC_INSTANCE * thisCDCDevice, USB_SETUP_PACKET * setupRequest ) { /* Check the request */ switch (setupRequest->bRequest ) { case USB_CDC_REQUEST_SET_LINE_CODING: /* Send this event to application. The application * should issues a control receive request to receive * the data from the host. */ if(thisCDCDevice->appEventCallBack != NULL) { thisCDCDevice->appEventCallBack(iCDC, USB_DEVICE_CDC_EVENT_SET_LINE_CODING, NULL, thisCDCDevice->userData); } break; case USB_CDC_REQUEST_GET_LINE_CODING: /* Send this event to application. The application should * issue a control send request to send this request to * the host. */ if(thisCDCDevice->appEventCallBack != NULL) { thisCDCDevice->appEventCallBack(iCDC, USB_DEVICE_CDC_EVENT_GET_LINE_CODING, NULL, thisCDCDevice->userData); } break; case USB_CDC_REQUEST_SET_CONTROL_LINE_STATE: /* In this event, the data is available in the * setup packet. Send it to the application */ if(thisCDCDevice->appEventCallBack != NULL) { thisCDCDevice->appEventCallBack(iCDC, USB_DEVICE_CDC_EVENT_SET_CONTROL_LINE_STATE, (USB_CDC_CONTROL_LINE_STATE *)(&setupRequest->wValue), thisCDCDevice->userData); } break; /* AT commands */ case USB_CDC_REQUEST_SEND_ENCAPSULATED_COMMAND: case USB_CDC_REQUEST_GET_ENCAPSULATED_RESPONSE: /* AT commands are not supported */ USB_DEVICE_ControlStatus(thisCDCDevice->deviceHandle, USB_DEVICE_CONTROL_STATUS_ERROR); break; /* break request */ case USB_CDC_REQUEST_SEND_BREAK: /* In this event, the data is available in the * setup packet. Send it to the application */ if(thisCDCDevice->appEventCallBack != NULL) { thisCDCDevice->appEventCallBack(iCDC, USB_DEVICE_CDC_EVENT_SEND_BREAK, (uint16_t *)(&setupRequest->wValue), thisCDCDevice->userData); } break; /* requests that do not belog to ACM sub class */ case USB_CDC_REQUEST_SET_COMM_FEATURE: case USB_CDC_REQUEST_GET_COMM_FEATURE: case USB_CDC_REQUEST_CLEAR_COMM_FEATURE: case USB_CDC_REQUEST_SET_AUX_LINE_STATE: case USB_CDC_REQUEST_SET_HOOK_STATE: case USB_CDC_REQUEST_PULSE_SETUP: case USB_CDC_REQUEST_SEND_PULSE: case USB_CDC_REQUEST_SET_PULSE_TIME: case USB_CDC_REQUEST_RING_AUX_JACK: case USB_CDC_REQUEST_SET_RINGER_PARMS: case USB_CDC_REQUEST_GET_RINGER_PARMS: case USB_CDC_REQUEST_SET_OPERATIONAL_PARMS: case USB_CDC_REQUEST_GET_OPERATIONAL_PARMS: case USB_CDC_REQUEST_SET_LINE_PARMS: case USB_CDC_REQUEST_GET_LINE_PARMS: case USB_CDC_REQUEST_DIAL_DIGITS: case USB_CDC_REQUEST_SET_UNIT_PARAMETER: case USB_CDC_REQUEST_GET_UNIT_PARAMETER: case USB_CDC_REQUEST_CLEAR_UNIT_PARAMETER: case USB_CDC_REQUEST_GET_PROFILE: case USB_CDC_REQUEST_SET_ETHERNET_MULTICAST_FILTERS: case USB_CDC_REQUEST_SET_ETHERNET_POWER_MANAGEMENT_FILTER: case USB_CDC_REQUEST_GET_ETHERNET_POWER_MANAGEMENT_FILTER: case USB_CDC_REQUEST_SET_ETHERNET_PACKET_FILTER: case USB_CDC_REQUEST_GET_ETHERNET_STATISTIC: case USB_CDC_REQUEST_SET_ATM_DATA_FORMAT: case USB_CDC_REQUEST_GET_ATM_DEVICE_STATISTICS: case USB_CDC_REQUEST_SET_ATM_DEFAULT_VC: case USB_CDC_REQUEST_GET_ATM_VC_STATISTICS: default: /* These request are not supported */ USB_DEVICE_ControlStatus(thisCDCDevice->deviceHandle, USB_DEVICE_CONTROL_STATUS_ERROR); break; } }
void APP_USBDeviceHIDEventHandler(USB_DEVICE_HID_INDEX hidInstance, USB_DEVICE_HID_EVENT event, void * eventData, uintptr_t userData) { APP_DATA * appData = (APP_DATA *)userData; switch(event) { case USB_DEVICE_HID_EVENT_REPORT_SENT: /* This means the mouse report was sent. We are free to send another report */ appData->isMouseReportSendBusy = false; break; case USB_DEVICE_HID_EVENT_REPORT_RECEIVED: /* Dont care for other event in this demo */ break; case USB_DEVICE_HID_EVENT_SET_IDLE: /* Acknowledge the Control Write Transfer */ USB_DEVICE_ControlStatus(appData->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); /* save Idle rate received from Host */ appData->idleRate = ((USB_DEVICE_HID_EVENT_DATA_SET_IDLE*)eventData)->duration; break; case USB_DEVICE_HID_EVENT_GET_IDLE: /* Host is requesting for Idle rate. Now send the Idle rate */ USB_DEVICE_ControlSend(appData->deviceHandle, &(appData->idleRate),1); /* On successfully receiving Idle rate, the Host would acknowledge back with a Zero Length packet. The HID function driver returns an event USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT to the application upon receiving this Zero Length packet from Host. USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT event indicates this control transfer event is complete */ break; case USB_DEVICE_HID_EVENT_SET_PROTOCOL: /* Host is trying set protocol. Now receive the protocol and save */ appData->activeProtocol = *(USB_HID_PROTOCOL_CODE *)eventData; /* Acknowledge the Control Write Transfer */ USB_DEVICE_ControlStatus(appData->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_HID_EVENT_GET_PROTOCOL: /* Host is requesting for Current Protocol. Now send the Idle rate */ USB_DEVICE_ControlSend(appData->deviceHandle, &(appData->activeProtocol), 1); /* On successfully receiving Idle rate, the Host would acknowledge back with a Zero Length packet. The HID function driver returns an event USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT to the application upon receiving this Zero Length packet from Host. USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT event indicates this control transfer event is complete */ break; case USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT: break; default: break; } }
USB_DEVICE_CDC_EVENT_RESPONSE APP_USBDeviceCDCEventHandler ( USB_DEVICE_CDC_INDEX index , USB_DEVICE_CDC_EVENT event , void * pData, uintptr_t userData ) { APP2_DATA * app2DataObject; app2DataObject = (APP2_DATA *)userData; USB_CDC_CONTROL_LINE_STATE * controlLineStateData; switch ( event ) { case USB_DEVICE_CDC_EVENT_GET_LINE_CODING: /* This means the host wants to know the current line * coding. This is a control transfer request. Use the * USB_DEVICE_ControlSend() function to send the data to * host. */ USB_DEVICE_ControlSend(app2DataObject->deviceHandle, &app2DataObject->getLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_LINE_CODING: /* This means the host wants to set the line coding. * This is a control transfer request. Use the * USB_DEVICE_ControlReceive() function to receive the * data from the host */ USB_DEVICE_ControlReceive(app2DataObject->deviceHandle, &app2DataObject->setLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_CONTROL_LINE_STATE: /* This means the host is setting the control line state. * Read the control line state. We will accept this request * for now. */ controlLineStateData = (USB_CDC_CONTROL_LINE_STATE *)pData; app2DataObject->controlLineStateData.dtr = controlLineStateData->dtr; app2DataObject->controlLineStateData.carrier = controlLineStateData->carrier; USB_DEVICE_ControlStatus(app2DataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_SEND_BREAK: /* This means that the host is requesting that a break of the * specified duration be sent. Read the break duration */ app2DataObject->breakData = ((USB_DEVICE_CDC_EVENT_DATA_SEND_BREAK *)pData)->breakDuration; /* Complete the control transfer by sending a ZLP */ USB_DEVICE_ControlStatus(app2DataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_READ_COMPLETE: /* This means that the host has sent some data*/ //SYS_CONSOLE_MESSAGE("Read flag set\n"); app2DataObject->isReadComplete = true; break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: /* The data stage of the last control transfer is * complete. For now we accept all the data */ USB_DEVICE_ControlStatus(app2DataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_SENT: /* This means the GET LINE CODING function data is valid. We dont * do much with this data in this demo. */ break; case USB_DEVICE_CDC_EVENT_WRITE_COMPLETE: /* This means that the data write got completed. We can schedule * the next read. */ app2DataObject->isWriteComplete = true; break; default: break; } return USB_DEVICE_CDC_EVENT_RESPONSE_NONE; }
void _USB_DEVICE_CDC_ControlTransferHandler ( SYS_MODULE_INDEX iCDC , USB_DEVICE_EVENT controlTransferEvent, USB_SETUP_PACKET * setupRequest ) { USB_DEVICE_HANDLE deviceHandle; USB_DEVICE_CDC_INSTANCE * thisCDCDevice; /* Check the validity of the function driver index */ if (iCDC >= USB_DEVICE_CDC_INSTANCES_NUMBER) { /* invalid CDC index */ SYS_DEBUG(0, "USB Device CDC: Invalid CDC index" ); return; } /* Get a local reference */ thisCDCDevice = &gUSBDeviceCDCInstance[iCDC]; /* Get the Device Layer handle */ deviceHandle = thisCDCDevice->deviceHandle; switch (controlTransferEvent) { /* Setup packet received */ case USB_DEVICE_EVENT_CONTROL_TRANSFER_SETUP_REQUEST: /* This means we have a setup packet for this interface */ if(!(setupRequest->bmRequestType & USB_CDC_REQUEST_CLASS_SPECIFIC)) { /* This means this is not a class specific request. * We stall this request */ USB_DEVICE_ControlStatus(deviceHandle, USB_DEVICE_CONTROL_STATUS_ERROR); } else { /* Check if the requests belong to the ACM sub class */ switch(setupRequest->bRequest) { case USB_CDC_REQUEST_SET_LINE_CODING: case USB_CDC_REQUEST_GET_LINE_CODING: case USB_CDC_REQUEST_SET_CONTROL_LINE_STATE: case USB_CDC_REQUEST_SEND_BREAK: case USB_CDC_REQUEST_SEND_ENCAPSULATED_COMMAND: case USB_CDC_REQUEST_GET_ENCAPSULATED_RESPONSE: /* These are ACM requests */ _USB_DEVICE_CDC_ACMSetUpPacketHandler(iCDC, thisCDCDevice, setupRequest); break; default: /* This is an un-supported request */ USB_DEVICE_ControlStatus(deviceHandle, USB_DEVICE_CONTROL_STATUS_ERROR); break; } } break; case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: /* A control transfer data stage is complete. Send * this event to application */ if(thisCDCDevice->appEventCallBack != NULL) { thisCDCDevice->appEventCallBack(iCDC, USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_RECEIVED, NULL, thisCDCDevice->userData ); } break; case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_SENT: /* A control transfer data stage is complete. Send * this event to application */ if(thisCDCDevice->appEventCallBack != NULL) { thisCDCDevice->appEventCallBack(iCDC, USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_SENT, NULL, thisCDCDevice->userData ); } default: break; } }
USB_DEVICE_CDC_EVENT_RESPONSE APP_USBDeviceCDCEventHandler ( USB_DEVICE_CDC_INDEX index , USB_DEVICE_CDC_EVENT event , void* pData, uintptr_t userData ) { APP_DATA * appDataObject; appDataObject = (APP_DATA *)userData; USB_CDC_CONTROL_LINE_STATE * controlLineStateData; uint16_t * breakData; switch ( event ) { case USB_DEVICE_CDC_EVENT_GET_LINE_CODING: /* This means the host wants to know the current line * coding. This is a control transfer request. Use the * USB_DEVICE_ControlSend() function to send the data to * host. */ USB_DEVICE_ControlSend(appDataObject->deviceHandle, &appDataObject->getLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_LINE_CODING: /* This means the host wants to set the line coding. * This is a control transfer request. Use the * USB_DEVICE_ControlReceive() function to receive the * data from the host */ USB_DEVICE_ControlReceive(appDataObject->deviceHandle, &appDataObject->setLineCodingData, sizeof(USB_CDC_LINE_CODING)); break; case USB_DEVICE_CDC_EVENT_SET_CONTROL_LINE_STATE: /* This means the host is setting the control line state. * Read the control line state. We will accept this request * for now. */ controlLineStateData = (USB_CDC_CONTROL_LINE_STATE *)pData; appDataObject->controlLineStateData.dtr = controlLineStateData->dtr; appDataObject->controlLineStateData.carrier = controlLineStateData->carrier; USB_DEVICE_ControlStatus(appDataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_SEND_BREAK: /* This means that the host is requesting that a break of the * specified duration be sent. Read the break duration */ breakData = (uint16_t *)pData; appDataObject->breakData = *breakData; break; case USB_DEVICE_CDC_EVENT_READ_COMPLETE: /* This means that the host has sent some data*/ appDataObject->isReadComplete = true; appDataObject->readLength = ((USB_DEVICE_CDC_EVENT_DATA_READ_COMPLETE*)pData)->length; break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: /* The data stage of the last control transfer is * complete. We have received the line coding from the host. Call * the usart driver routine to change the baud. We can call this * function here as it is not blocking. */ DRV_USART_BaudSet(appDataObject->usartHandle, appDataObject->setLineCodingData.dwDTERate); USB_DEVICE_ControlStatus(appDataObject->deviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_DEVICE_CDC_EVENT_CONTROL_TRANSFER_DATA_SENT: /* This means the GET LINE CODING function data is valid. We dont * do much with this data in this demo. */ break; case USB_DEVICE_CDC_EVENT_WRITE_COMPLETE: /* This means that the data write got completed. We can schedule * the next read. */ break; default: break; } return USB_DEVICE_CDC_EVENT_RESPONSE_NONE; }
/* Function: void _USB_DEVICE_AUDIO_SetupPacketHandler ( USB_DEVICE_AUDIO_INDEX iAudio , USB_SETUP_PACKET * setupPkt ) Summary: Handles a fresh SETUP packet received from Host. Description: Handles a fresh SETUP packet received from Host. Returns: This is a local function and should not be called directly by the application. */ void _USB_DEVICE_AUDIO_SetupPacketHandler ( USB_DEVICE_AUDIO_INDEX iAudio , USB_SETUP_PACKET * setupPkt ) { uint8_t audioControlInterfaceId; uint8_t interfaceId; uint8_t curAlternateSetting; uint8_t prevAlternateSetting; uint8_t streamIntfcIndex; uint8_t noOfEndpoints; USB_ENDPOINT ep; uint16_t maxPacketSize; USB_DEVICE_HANDLE usbDeviceHandle; USB_DEVICE_AUDIO_EVENT event; USB_DEVICE_AUDIO_EVENT_DATA_INTERFACE_SETTING_CHANGED interfaceInfo; USB_ERROR endpointEnableResult; USB_DEVICE_AUDIO_STREAMING_INTERFACE_ALTERNATE_SETTING *pCurAlternateStng; USB_DEVICE_AUDIO_STREAMING_INTERFACE_ALTERNATE_SETTING *pPrevAlternateStng; USB_DEVICE_AUDIO_STREAMING_INTERFACE *pStreamingInterface; uint16_t adjustedMaxPacketSize =1; /* Obtain pointer to the Audio Instance that is being addressed*/ USB_DEVICE_AUDIO_INSTANCE* audioInstance = &gUsbDeviceAudioInstance[iAudio]; /* Obtain pointer to the Audio Interface collection*/ USB_DEVICE_AUDIO_INTERFACE_COLLECTION *curInfCollection = &(audioInstance->infCollection); /* Get the Device Layer handle */ usbDeviceHandle = gUsbDeviceAudioInstance[iAudio].devLayerHandle; /* Check if the request is a standard interface request*/ if ( (setupPkt->RequestType == USB_SETUP_REQUEST_TYPE_STANDARD) && (setupPkt->Recipient == USB_SETUP_REQUEST_RECIPIENT_INTERFACE)) { /* We have received Standard Set request */ /* retrieve interface number from the Setup packet */ interfaceId = setupPkt->bIntfID; /* retrieve audio Control interface number*/ audioControlInterfaceId = curInfCollection->bControlInterfaceNum; switch(setupPkt->bRequest) { case USB_REQUEST_SET_INTERFACE: curAlternateSetting = setupPkt->bAltID; if (interfaceId == audioControlInterfaceId) { /*SET INTERFACE command was received to Audio Control Interface */ curInfCollection->bControlAltSetng = curAlternateSetting; } else { /*An Audio Streaming interface has received SET INTERFACE command */ streamIntfcIndex = interfaceId - audioControlInterfaceId - 1; /* Get pointer to the current audio streaming interface */ pStreamingInterface = &(curInfCollection->streamInf[streamIntfcIndex]); /* Get pointer to the Interface Alternate setting. */ pCurAlternateStng = &(pStreamingInterface->alterntSetting[curAlternateSetting]); /* Find out how many endpoint are present for this Interface * Alternate setting */ noOfEndpoints = pCurAlternateStng->numEndPoints; if ((noOfEndpoints) && (curAlternateSetting)) { /* We have to enable the endpoint only if this alternate setting has at least one endpoint and the alternate setting is a non zero value */ /*Retrieve endpoint size */ ep = pCurAlternateStng->isoDataEp.epAddr; /* retrieve max packet size. */ maxPacketSize = pCurAlternateStng->isoDataEp.epMaxPacketSize; /* maxpacket size should be adjusted to upper power of two. As per the PIC32MZ USB module requirement.*/ if (maxPacketSize) { while(adjustedMaxPacketSize < maxPacketSize) adjustedMaxPacketSize <<= 1; } else adjustedMaxPacketSize = 0; /* Enable Isochronous Data Endpoint */ endpointEnableResult = USB_DEVICE_EndpointEnable ( usbDeviceHandle , 0, ep , USB_TRANSFER_TYPE_ISOCHRONOUS , adjustedMaxPacketSize ); if (endpointEnableResult != USB_ERROR_NONE) { SYS_ASSERT (false, "Endpoint not Enabled"); } if (noOfEndpoints == 2) { /* If number of Endpoints is Two, then it is sure * that this alternate setting reports a Isochronous * Sync Endpoint. Enable the Sync Endpoint. */ } //Change Audio Instance object state to Initialized. pStreamingInterface->state = USB_DEVICE_AUDIO_STRMNG_INTFC_INITIALIZED; } else //alternateSetting = 0 { if (pStreamingInterface->state == USB_DEVICE_AUDIO_STRMNG_INTFC_INITIALIZED) { //Disable the Endpoint in the previous Active Alternate setting. prevAlternateSetting = pStreamingInterface->activeSetting; //get a pointer to the previous alternate setting. pPrevAlternateStng = &(pStreamingInterface->alterntSetting[prevAlternateSetting]); //Get endpoint no of the previous alternate setting ep = pPrevAlternateStng->isoDataEp.epAddr; //Disable the Endpoint USB_DEVICE_EndpointDisable(usbDeviceHandle ,ep); if (noOfEndpoints == 2) { /* If number of Endpoints is Two, then it is sure * that this alternate setting reports a Isochronous * Sync Endpoint. Disable the Sync Endpoint. */ } curInfCollection->streamInf[streamIntfcIndex].state = USB_DEVICE_AUDIO_STRMNG_INTFC_NOT_INITIALIZED; USB_DEVICE_IRPCancelAll(usbDeviceHandle, ep); } } /* Remember the new alternate setting received */ curInfCollection->streamInf[streamIntfcIndex].activeSetting = curAlternateSetting; /* Notify application about the SET_INTERFACE request */ interfaceInfo.interfaceNumber = interfaceId; interfaceInfo.interfaceAlternateSetting = curAlternateSetting; audioInstance->appEventCallBack ( iAudio, USB_DEVICE_AUDIO_EVENT_INTERFACE_SETTING_CHANGED, &interfaceInfo, 0 ); } /* Send an Acknowledgement to the Host */ USB_DEVICE_ControlStatus( usbDeviceHandle, USB_DEVICE_CONTROL_STATUS_OK); break; case USB_REQUEST_GET_INTERFACE: if (interfaceId == audioControlInterfaceId) { curAlternateSetting = curInfCollection->bControlAltSetng; } else { streamIntfcIndex = interfaceId - audioControlInterfaceId - 1; curAlternateSetting = curInfCollection->streamInf[streamIntfcIndex].activeSetting; } USB_DEVICE_ControlSend( usbDeviceHandle, (void *)&curAlternateSetting, 1); break; default: break; } }//end of if((setupPkt->bmRequestType == else if ( (setupPkt->RequestType == USB_SETUP_REQUEST_TYPE_CLASS ) && (setupPkt->Recipient == USB_SETUP_REQUEST_RECIPIENT_INTERFACE)) { /* We have recived a Audio Class specific Interface request */ switch (setupPkt->bRequest) { case USB_AUDIO_CS_SET_CUR: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_CUR; break; case USB_AUDIO_CS_GET_CUR: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_CUR; break; case USB_AUDIO_CS_SET_MIN: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_MIN; break; case USB_AUDIO_CS_GET_MIN: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_MIN; break; case USB_AUDIO_CS_SET_MAX: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_MAX; break; case USB_AUDIO_CS_GET_MAX: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_MAX; break; case USB_AUDIO_CS_SET_RES: event = USB_DEVICE_AUDIO_EVENT_CONTROL_SET_RES; break; case USB_AUDIO_CS_GET_RES: event = USB_DEVICE_AUDIO_EVENT_CONTROL_GET_RES; break; case USB_AUDIO_CS_SET_MEM: event = USB_DEVICE_AUDIO_EVENT_ENTITY_SET_MEM; break; case USB_AUDIO_CS_GET_MEM: event = USB_DEVICE_AUDIO_EVENT_ENTITY_GET_MEM; break; case USB_AUDIO_CS_GET_STAT: event = USB_DEVICE_AUDIO_EVENT_ENTITY_GET_STAT; break; default: /* Unknown request. Stall the request */ event = 0; USB_DEVICE_ControlStatus(audioInstance->devLayerHandle, USB_DEVICE_CONTROL_STATUS_ERROR); } if (( audioInstance->appEventCallBack) && (event)) { /* inform the application about the request */ /* the application needs to handle both EP and entity specific requests */ audioInstance->appEventCallBack ( iAudio, event, setupPkt, 0); } }// end of else if }//end of function _USB_DEVICE_AUDIO_SetupPacketHandler
USB_DEVICE_HID_EVENT_RESPONSE APP_USBDeviceHIDEventHandler ( USB_DEVICE_HID_INDEX iHID, USB_DEVICE_HID_EVENT event, void * eventData, uintptr_t userData ) { USB_DEVICE_HID_EVENT_DATA_REPORT_SENT * reportSent; USB_DEVICE_HID_EVENT_DATA_REPORT_RECEIVED * reportReceived; /* Check type of event */ switch (event) { case USB_DEVICE_HID_EVENT_REPORT_SENT: /* The eventData parameter will be USB_DEVICE_HID_EVENT_REPORT_SENT * pointer type containing details about the report that was * sent. */ reportSent = (USB_DEVICE_HID_EVENT_DATA_REPORT_SENT *) eventData; if(reportSent->handle == appData.txTransferHandle ) { // Transfer progressed. appData.hidDataTransmitted = true; } break; case USB_DEVICE_HID_EVENT_REPORT_RECEIVED: /* The eventData parameter will be USB_DEVICE_HID_EVENT_REPORT_RECEIVED * pointer type containing details about the report that was * received. */ reportReceived = (USB_DEVICE_HID_EVENT_DATA_REPORT_RECEIVED *) eventData; if(reportReceived->handle == appData.rxTransferHandle ) { // Transfer progressed. appData.hidDataReceived = true; } break; case USB_DEVICE_HID_EVENT_SET_IDLE: /* For now we just accept this request as is. We acknowledge * this request using the USB_DEVICE_HID_ControlStatus() * function with a USB_DEVICE_CONTROL_STATUS_OK flag */ USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_OK); /* Save Idle rate recieved from Host */ appData.idleRate = ((USB_DEVICE_HID_EVENT_DATA_SET_IDLE*)eventData)->duration; break; case USB_DEVICE_HID_EVENT_GET_IDLE: /* Host is requesting for Idle rate. Now send the Idle rate */ USB_DEVICE_ControlSend(appData.usbDevHandle, & (appData.idleRate),1); /* On successfully reciveing Idle rate, the Host would acknowledge back with a Zero Length packet. The HID function drvier returns an event USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT to the application upon receiving this Zero Length packet from Host. USB_DEVICE_HID_EVENT_CONTROL_TRANSFER_DATA_SENT event indicates this control transfer event is complete */ break; default: // Nothing to do. break; } return USB_DEVICE_HID_EVENT_RESPONSE_NONE; }
void APP_USBDeviceEventHandler ( USB_DEVICE_EVENT event, void * eventData, uintptr_t context ) { USB_DEVICE_EVENT_DATA_CONFIGURED* configuredEventData; switch(event) { case USB_DEVICE_EVENT_RESET: U1TXREG = 'R'; //break; case USB_DEVICE_EVENT_DECONFIGURED: { appData.state = APP_STATE_USB_OPENED; appData.reading = 0; appData.audio_play = false; USB_DEVICE_EndpointDisable(appData.usbDevHandle, AUDIO_EP); if(appData.tunnel_write_handle != USB_DEVICE_TRANSFER_HANDLE_INVALID) USB_DEVICE_EndpointTransferCancel(appData.usbDevHandle, TUNNEL_EP_IN, appData.tunnel_write_handle); USB_DEVICE_EndpointDisable(appData.usbDevHandle, TUNNEL_EP_IN); USB_DEVICE_EndpointDisable(appData.usbDevHandle, TUNNEL_EP_OUT); break; } case USB_DEVICE_EVENT_CONFIGURED: U1TXREG = '+'; /* check the configuration */ configuredEventData = (USB_DEVICE_EVENT_DATA_CONFIGURED *)eventData; if(configuredEventData->configurationValue == 1) { } USB_DEVICE_EndpointEnable(appData.usbDevHandle, AUDIO_STREAMING_INTERFACE_ID, AUDIO_EP, USB_TRANSFER_TYPE_ISOCHRONOUS, PACKET_SIZE); USB_DEVICE_EndpointEnable(appData.usbDevHandle, TUNER_CONTROL_INTERFACE_ID, TUNNEL_EP_IN, USB_TRANSFER_TYPE_BULK, 32); USB_DEVICE_EndpointEnable(appData.usbDevHandle, TUNER_CONTROL_INTERFACE_ID, TUNNEL_EP_OUT, USB_TRANSFER_TYPE_BULK, 32); appData.state = APP_STATE_CONFIGURED; break; case USB_DEVICE_EVENT_SUSPENDED: U1TXREG = 'S'; //appData.suspended = true; break; case USB_DEVICE_EVENT_RESUMED: U1TXREG = 's'; appData.suspended = false; break; case USB_DEVICE_EVENT_POWER_DETECTED: #ifndef NO_USB_ATTACH USB_DEVICE_Attach (appData.usbDevHandle); #else #warning "USB ATTACH DISABLED" #endif break; case USB_DEVICE_EVENT_POWER_REMOVED: USB_DEVICE_Detach (appData.usbDevHandle); break; case USB_DEVICE_EVENT_ERROR: U1TXREG = 'E'; break; case USB_DEVICE_EVENT_CONTROL_TRANSFER_SETUP_REQUEST: { USB_SETUP_PACKET * sp = eventData; if(sp->bRequest == 0x0b && sp->wIndex == 0x0001) //set interace on if 1 { //USB_DEVICE_ControlReceive(appData.usbDevHandle, &controlData, sizeof(controlData)); if(sp->wValue == 0) { int i; appData.audio_play = false; USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_OK); U1TXREG = 'C'; } else if(sp->wValue == 1) { appData.audio_play = true; USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_OK); appData.noAudioData = 1; U1TXREG = 'p'; } else USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_ERROR); } else USB_DEVICE_ControlStatus(appData.usbDevHandle, USB_DEVICE_CONTROL_STATUS_ERROR); break; } case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_RECEIVED: case USB_DEVICE_EVENT_CONTROL_TRANSFER_ABORTED: case USB_DEVICE_EVENT_CONTROL_TRANSFER_DATA_SENT: //debughalt(); break; case USB_DEVICE_EVENT_ENDPOINT_READ_COMPLETE: { appData.tunnel_read_count = ((USB_DEVICE_EVENT_DATA_ENDPOINT_WRITE_COMPLETE*)eventData)->length; if(appData.tunnel_read_data[0]&TUNNEL_PING_MASK) appData.pingRequest = 1; else appData.tuner_request = 1; //appData.reading = 0; U1TXREG = 'I'; break; } case USB_DEVICE_EVENT_ENDPOINT_WRITE_COMPLETE: { USB_DEVICE_EVENT_DATA_ENDPOINT_WRITE_COMPLETE* ed = (USB_DEVICE_EVENT_DATA_ENDPOINT_WRITE_COMPLETE*)eventData; if(appData.tunnel_write_handle == ed->transferHandle) { appData.tunnel_write_handle = USB_DEVICE_TRANSFER_HANDLE_INVALID; U1TXREG = 'W'; } else { int i=0; for(;i<AUDIO_HANDLES;i++) if(ed->transferHandle == audio_handle[i]) audio_handle[i] = HANDLE_INVALID; } break; } default: debughalt(); break; } }