//***************************************************************************** // //! This function is used to open an instance of the HID driver. //! //! \param psDevice is a pointer to the device information structure. //! //! This function will attempt to open an instance of the HID driver based on //! the information contained in the psDevice structure. This call can fail if //! there are not sufficient resources to open the device. The function will //! return a value that should be passed back into USBHIDClose() when the //! driver is no longer needed. //! //! \return The function will return a pointer to a HID driver instance. // //***************************************************************************** static void * HIDDriverOpen(tUSBHostDevice *psDevice) { int32_t i32Idx, i32Dev; tEndpointDescriptor *psEndpointDescriptor; tInterfaceDescriptor *psInterface; // // Get the interface descriptor. // psInterface = USBDescGetInterface(psDevice->psConfigDescriptor, 0, 0); // // Search the currently open instances for one that supports the protocol // of this device. // for(i32Dev = 0; i32Dev < MAX_HID_DEVICES; i32Dev++) { if(g_psHIDDevice[i32Dev].iDeviceType == psInterface->bInterfaceProtocol) { // // Save the device pointer. // g_psHIDDevice[i32Dev].psDevice = psDevice; for(i32Idx = 0; i32Idx < 3; i32Idx++) { // // Get the first endpoint descriptor. // psEndpointDescriptor = USBDescGetInterfaceEndpoint(psInterface, i32Idx, 256); // // If no more endpoints then break out. // if(psEndpointDescriptor == 0) { break; } // // Interrupt // if((psEndpointDescriptor->bmAttributes & USB_EP_ATTR_TYPE_M) == USB_EP_ATTR_INT) { // // Interrupt IN. // if(psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_IN) { g_psHIDDevice[i32Dev].ui32IntInPipe = USBHCDPipeAlloc(0, USBHCD_PIPE_INTR_IN, psDevice, HIDIntINCallback); USBHCDPipeConfig(g_psHIDDevice[i32Dev].ui32IntInPipe, psEndpointDescriptor->wMaxPacketSize, psEndpointDescriptor->bInterval, (psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M)); } } } // // If there is a callback function call it to inform the application that // the device has been enumerated. // if(g_psHIDDevice[i32Dev].pfnCallback != 0) { g_psHIDDevice[i32Dev].pfnCallback( g_psHIDDevice[i32Dev].pvCBData, USB_EVENT_CONNECTED, (uint32_t)&g_psHIDDevice[i32Dev], 0); } // // Save the device pointer. // g_psHIDDevice[i32Dev].psDevice = psDevice; return (&g_psHIDDevice[i32Dev]); } } // // If we get here, no user has registered an interest in this particular // HID device so we return an error. // return(0); }
//***************************************************************************** // //! This function is used to open an instance of the HID driver. //! //! \param pDevice is a pointer to the device information structure. //! //! This function will attempt to open an instance of the HID driver based on //! the information contained in the pDevice structure. This call can fail if //! there are not sufficient resources to open the device. The function will //! return a value that should be passed back into USBHIDClose() when the //! driver is no longer needed. //! //! \return The function will return a pointer to a HID driver instance. // //***************************************************************************** static void * HIDDriverOpen(tUSBHostDevice *pDevice) { int iIdx; tEndpointDescriptor *pEndpointDescriptor; tInterfaceDescriptor *pInterface; // // Don't allow the device to be opened without closing first. // if(g_HIDDevice.pDevice) { return (0); } // // Get the interface descriptor. // pInterface = USBDescGetInterface(pDevice->pConfigDescriptor, 0, 0); if((pInterface->bInterfaceSubClass != USB_HID_SCLASS_BOOT) || (pInterface->bInterfaceProtocol != g_HIDDevice.eDeviceType)) { return(0); } // // Save the device pointer. // g_HIDDevice.pDevice = pDevice; for(iIdx = 0; iIdx < 3; iIdx++) { // // Get the first endpoint descriptor. // pEndpointDescriptor = USBDescGetInterfaceEndpoint(pInterface, iIdx, 256); // // If no more endpoints then break out. // if(pEndpointDescriptor == 0) { break; } // // Interrupt // if((pEndpointDescriptor->bmAttributes & USB_EP_ATTR_TYPE_M) == USB_EP_ATTR_INT) { // // Interrupt IN. // if(pEndpointDescriptor->bEndpointAddress & USB_EP_DESC_IN) { g_HIDDevice.ulIntInPipe = USBHCDPipeAlloc(0, USBHCD_PIPE_INTR_IN, pDevice->ulAddress, HIDIntINCallback); USBHCDPipeConfig(g_HIDDevice.ulIntInPipe, pEndpointDescriptor->wMaxPacketSize, pEndpointDescriptor->bInterval, (pEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M)); } } } // // If there is a callback function call it to inform the application that // the device has been enumerated. // if(g_HIDDevice.pfnCallback != 0) { g_HIDDevice.pfnCallback((void *)g_HIDDevice.ulCBData, USB_EVENT_CONNECTED, (unsigned long)&g_HIDDevice, 0); } // // Save the device pointer. // g_HIDDevice.pDevice = pDevice; return (&g_HIDDevice); }
//***************************************************************************** // //! This function is used to open an instance of the CDC driver. //! //! \param pDevice is a pointer to the device information structure. //! //! This function will attempt to open an instance of the CDC driver based on //! the information contained in the pDevice structure. This call can fail if //! there are not sufficient resources to open the device. The function will //! return a value that should be passed back into USBCDCClose() when the //! driver is no longer needed. //! //! \return The function will return a pointer to a CDC driver instance. // //***************************************************************************** static void * USBHCDCOpen(tUSBHostDevice *psDevice) { //printf("USBHCDCOpen: Start\n"); int32_t i32Idx, i32Idz; tEndpointDescriptor *psEndpointDescriptor; tInterfaceDescriptor *psInterface; // // Don't allow the device to be opened without closing first. // if(g_USBHCDCDevice.psDevice) { //printf("USBHCDCOpen: For Loop: i32Idx, %d\n", i32Idx); return(0); } // // Save the device pointer. // g_USBHCDCDevice.psDevice = psDevice; // // Loop Through the first 3 Interfaces Searching for Endpoints // for(i32Idz = 0; i32Idz < 3; i32Idz++) { // // Get the interface descriptor. // psInterface = USBDescGetInterface(psDevice->psConfigDescriptor, i32Idz, 0); // // Loop through the endpoints of the device. // bNumEndPoints returning 1, should be 3 - will set manually, as there is always 3...then it works!!! // for(i32Idx = 0; i32Idx < 3; i32Idx++) //psInterface->bNumEndpoints; i32Idx++) { // // Get the first endpoint descriptor. // psEndpointDescriptor = USBDescGetInterfaceEndpoint(psInterface, i32Idx, psDevice->ui32ConfigDescriptorSize); //System_printf("USBHCDCOpen: psEndPointDecriptor: %d bmAttribute: %d\n", psEndpointDescriptor, psEndpointDescriptor->bmAttributes); //System_flush(); // // If no more endpoints then break out. // if(psEndpointDescriptor == 0) { break; } // // See if this is an Interrupt endpoint. // if((psEndpointDescriptor->bmAttributes & USB_EP_ATTR_TYPE_M) == USB_EP_ATTR_INT) { // // Interrupt IN. // if(psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_IN) { g_USBHCDCDevice.ui32IntInPipe = USBHCDPipeAlloc(0, USBHCD_PIPE_INTR_IN, psDevice, USBHCDCInterruptCallback); USBHCDPipeConfig(g_USBHCDCDevice.ui32IntInPipe, psEndpointDescriptor->wMaxPacketSize, psEndpointDescriptor->bInterval, (psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M)); //System_printf("USBHCDCOpen: Allocated and Configured Interrupt In Endpoint: Interface Index: %d bEndPointAddress: %d ui32BulkOutPipe: %d\n", i32Idz, psEndpointDescriptor->bEndpointAddress, g_USBHCDCDevice.ui32IntInPipe); //System_flush(); } } // // See if this is a bulk endpoint. // if((psEndpointDescriptor->bmAttributes & USB_EP_ATTR_TYPE_M) == USB_EP_ATTR_BULK) { // // See if this is bulk IN or bulk OUT or Interrupt // //if((psEndpointDescriptor->bmAttributes & USB_EP_ATTR_INT) == USB_EP_ATTR_INT) //{ // // // // Allocate the USB Pipe for this Bulk IN Interrupt endpoint. // // // g_USBHCDCDevice.ui32BulkInPipe = // USBHCDPipeAllocSize(0, USBHCD_PIPE_BULK_IN, // psDevice, // psEndpointDescriptor->wMaxPacketSize, // USBHCDCInterruptCallback); // // // // Configure the USB pipe as a Bulk IN endpoint. // // // USBHCDPipeConfig(g_USBHCDCDevice.ui32BulkInPipe, // psEndpointDescriptor->wMaxPacketSize, // 1, // (psEndpointDescriptor->bEndpointAddress & // USB_EP_DESC_NUM_M)); // // System_printf("USBHCDCOpen: Allocated and Configured Interrupt EndPoint**: Interface Index: %d bEndPointAddress: %d ui32BulkOutPipe: %d\n", i32Idz, psEndpointDescriptor->bEndpointAddress, g_USBHCDCDevice.ui32BulkOutPipe); // System_flush(); // // } if(psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_IN) { // // Allocate the USB Pipe for this Bulk IN endpoint. // g_USBHCDCDevice.ui32BulkInPipe = USBHCDPipeAllocSize(0, USBHCD_PIPE_BULK_IN, psDevice, psEndpointDescriptor->wMaxPacketSize, USBHCDCINCallback); // // Configure the USB pipe as a Bulk IN endpoint. // USBHCDPipeConfig(g_USBHCDCDevice.ui32BulkInPipe, psEndpointDescriptor->wMaxPacketSize, 64, (psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M)); //System_printf("USBHCDCOpen: Allocated and Configured Bulk In EndPoint: Interface Index: %d bEndPointAddress: %d ui32BulkOutPipe: %d\n", i32Idz, psEndpointDescriptor->bEndpointAddress, g_USBHCDCDevice.ui32BulkOutPipe); //System_flush(); } else { // // Allocate the USB Pipe for this Bulk OUT endpoint. // g_USBHCDCDevice.ui32BulkOutPipe = USBHCDPipeAllocSize(0, USBHCD_PIPE_BULK_OUT, psDevice, psEndpointDescriptor->wMaxPacketSize, 0); // // Configure the USB pipe as a Bulk OUT endpoint. // USBHCDPipeConfig(g_USBHCDCDevice.ui32BulkOutPipe, psEndpointDescriptor->wMaxPacketSize, 0, (psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M)); //System_printf("USBHCDCOpen: Allocated and Configured Bulk Out EndPoint: Interface Index: %d bEndPointAddress: %d ui32BulkOutPipe: %d\n", i32Idz, psEndpointDescriptor->bEndpointAddress, g_USBHCDCDevice.ui32BulkOutPipe); //System_flush(); } } } } // // If there is a callback function call it to inform the application that // the device has been enumerated. // // To be used with the RTOS callback handler example. // // if(g_USBCDCDevice.pfnCallback != 0) // { // g_psHIDDevice[i32Dev].pfnCallback( // g_psHIDDevice[i32Dev].pvCBData, // USB_EVENT_CONNECTED, // (uint32_t)&g_psHIDDevice[i32Dev], 0); // } // // If the callback exists, call it with an Open event. // if(g_USBHCDCDevice.pfnCallback != 0) { // printf("USBHCDCOpen: Callback Exists: Calling\n"); // System_flush(); g_USBHCDCDevice.pfnCallback(&g_USBHCDCDevice, CDC_EVENT_OPEN, 0, 0); } // // Return the only instance of this device. // return(&g_USBHCDCDevice); }
//***************************************************************************** // // Open an instance of the hub driver. This is called when the USB host // has enumerated a new hub device. // //***************************************************************************** static void * HubDriverOpen(tUSBHostDevice *psDevice) { tEndpointDescriptor *psEndpointDescriptor; tInterfaceDescriptor *psInterface; tUsbHubDescriptor sHubDesc; bool bRetcode; uint32_t ui32Loop; // // If we are already talking to a hub, fail the call. We only support // a single hub. // if(g_sRootHub.ui32Flags & USBLIB_HUB_ACTIVE) { return(0); } // // Get pointers to the device descriptors we need to look at. // psInterface = USBDescGetInterface(psDevice->psConfigDescriptor, 0, 0); psEndpointDescriptor = USBDescGetInterfaceEndpoint(psInterface, 0, psDevice->ui32ConfigDescriptorSize); // // If there are no endpoints, something is wrong since a hub must have // a single INT endpoint for signaling. // if(psEndpointDescriptor == 0) { return 0; } // // Make sure we really are talking to a hub. // if((psInterface->bInterfaceClass != USB_CLASS_HUB) || (psInterface->bInterfaceSubClass != 0)) { // // Something is wrong - this isn't a hub or, if it is, we don't // understand the protocol it is using. // return(0); } // // Remember that this is a high speed hub with either single or multiple // transaction translators. // if(psInterface->bInterfaceProtocol == USB_HUB_PROTOCOL_SINGLE) { g_sRootHub.ui32Flags |= USBLIB_HUB_HS; } else if(psInterface->bInterfaceProtocol == USB_HUB_PROTOCOL_MULTI) { g_sRootHub.ui32Flags |= USBLIB_HUB_HS | USBLIB_HUB_MULTI_TT; } // // Remember the device information for later. // g_sRootHub.psDevice = psDevice; // // A hub must support an interrupt endpoint so check this. // if((psEndpointDescriptor->bmAttributes & USB_EP_ATTR_TYPE_M) == USB_EP_ATTR_INT) { // // The endpoint is the correct type. Is it an IN endpoint? // if(psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_IN) { // // Yes - all is well with the hub endpoint so allocate a pipe to // handle traffic from the hub. // g_sRootHub.ui32IntInPipe = USBHCDPipeAlloc(0, USBHCD_PIPE_INTR_IN, psDevice, HubIntINCallback); USBHCDPipeConfig(g_sRootHub.ui32IntInPipe, psEndpointDescriptor->wMaxPacketSize, psEndpointDescriptor->bInterval, psEndpointDescriptor->bEndpointAddress & USB_EP_DESC_NUM_M); } } // // Did we allocate the endpoint successfully? // if(!g_sRootHub.ui32IntInPipe) { // // No - return an error. // return 0; } // // Assuming we have a callback, call it to tell the owner that a hub is // now connected. // if(g_sRootHub.pfnCallback != 0) { g_sRootHub.pfnCallback((void *)g_sRootHub.ui32CBData, USB_EVENT_CONNECTED, (uint32_t)&g_sRootHub, 0); } // // Get the hub descriptor and store information we'll need for later. // bRetcode = GetHubDescriptor(&sHubDesc); if(bRetcode) { // // We read the descriptor successfully so extract the parts we need. // g_sRootHub.ui8NumPorts = sHubDesc.bNbrPorts; g_sRootHub.ui16HubCharacteristics = sHubDesc.wHubCharacteristics; g_sRootHub.ui8NumPortsInUse = (sHubDesc.bNbrPorts > MAX_USB_DEVICES) ? MAX_USB_DEVICES : sHubDesc.bNbrPorts; // // The size of the status change report that the hub sends is dependent // upon the number of ports that the hub supports. Calculate this by // adding 1 to the number of ports (bit 0 of the report is the hub // status, higher bits are one per port) then dividing by 8 (bits per // byte) and rounding up. // g_sRootHub.ui8ReportSize = ((sHubDesc.bNbrPorts + 1) + 7) / 8; // // Enable power to all ports on the hub. // for(ui32Loop = 1; ui32Loop <= sHubDesc.bNbrPorts; ui32Loop++) { // // Turn on power to this port. // HubSetPortFeature(&g_sRootHub, ui32Loop, HUB_FEATURE_PORT_POWER); } // // Clear out our port state structures. // for(ui32Loop = 0; ui32Loop < MAX_USB_DEVICES; ui32Loop++) { g_sRootHub.psPorts[ui32Loop].bChanged = false; g_sRootHub.psPorts[ui32Loop].iState = ePortIdle; } } else { // // Oops - we can't read the hub descriptor! Tidy up and return // an error. // USBHCDPipeFree(g_sRootHub.ui32IntInPipe); g_sRootHub.pfnCallback = 0; g_sRootHub.ui32Flags &= ~USBLIB_HUB_ACTIVE; return(0); } // // If we get here, all is well so remember that the hub is connected and // active. // g_sRootHub.ui32Flags |= USBLIB_HUB_ACTIVE; // // Return our instance data pointer to the caller to use as a handle. // return((void *)&g_sRootHub); }