⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xsusbhostapi.c

📁 优龙YLP270开发板 光盘自带的BIOS和实验例程源码 强烈推荐
💻 C
📖 第 1 页 / 共 5 页
字号:
		            USBD_VarOK,
#else
		            USBD_VarNotOK,
#endif
		            UsbTDIn,
		            length);
    if (rc != UsbdNoError)
    {
        DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Get device descriptor failed - %d!\n", *length);
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETDEVDESCR, 2, ERR_T_NOTRANSFER,
                                    rc, *length, 0);  
        freeBuffer((void*)setuP);
    	return UsbHost.loggedError;
    }
    
    dumpBuffer((char*)devdP, *length);

    // Copy the data back to the caller's buffer
    if (*length < len)
        len = *length;
    if (bufP)
        memcpy((char*)bufP,(char*)devdP,len);

    freeBuffer((void*)setuP);

    return rc;
}

/*----------------------------------------------------------------------
 * Get the port configuration descriptors
 */
static
UINT getConfiguration(USBD_Device_T * devP,
		     USB_ConfigurationDescriptor_T *bufP,
		     int index,
		     int len,
		     int * length)
{
    USB_EndpointDescriptor_T * edP = devP->Endpoints[0];
    USB_DeviceRequest_T * setuP;
    USB_ConfigurationDescriptor_T * confP;
    UINT rc = UsbdNoError;

    if ((setuP = (USB_DeviceRequest_T*)newBuffer()) == NULL)
    {
        DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Failed to allocate setup buffer!\n");
      	usbxStats.noBuffer++;
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETCONFIG, 1, ERR_T_NOBUFFER,
                                        usbxStats.noBuffer, UsbdNoBuffer, 0);  
  	    return UsbHost.loggedError;
    }
    
    confP = (USB_ConfigurationDescriptor_T *)(setuP + 1);

    memset((char*)setuP,0,sizeof(USB_DeviceRequest_T));
    setuP->bmRequestType.s.toHost = 1;
    setuP->bmRequestType.s.recipient = UsbReqTypeDevice;
    setuP->bmRequestType.s.type = UsbReqTypeStandard;
    setuP->bRequest = UsbGetDescriptor;
    setuP->wValue.desc.index = index;
    setuP->wValue.desc.type = UsbDescTypeConfiguration;
    setuP->wIndex.d = 0;
    setuP->wLength = sizeof(USB_ConfigurationDescriptor_T);

    rc = sendMessage(edP,
		            (void*)setuP,
		            (void*)confP,
		            setuP->wLength,
#if 1 /* Allow       short packets and cehck length */
		            USBD_VarOK,
#else
		            USBD_VarNotOK,
#endif
		            UsbTDIn,
		            length);

    if (rc != UsbdNoError)
    {
        DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Get configuration descriptor failed - %d!\n", *length);
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETCONFIG, 2, ERR_T_NOTRANSFER,
                   rc, *length, 0);  
  	    freeBuffer((void*)setuP);
  	    return UsbHost.loggedError;
    }
    
    dumpBuffer((char*)confP, *length);

    if (*length < confP->bLength)
    {
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETCONFIG, 3, ERR_T_UNEXPECTED,
                   *length, UsbdMissingData, 0);  
  	    return UsbHost.loggedError;
    }

    if (confP->wTotalLength > (USBD_BUFSIZ-sizeof(USB_DeviceRequest_T)))
    {
        DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Configuration descriptor is too large, %d bytes!\n",
	                    confP->wTotalLength);
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETCONFIG, 4, ERR_T_UNEXPECTED,
                   confP->wTotalLength, confP->wTotalLength, 0);  
  	    return UsbHost.loggedError;
    }

    // Now read the configuration descriptor with all the sub descriptors
    setuP->wLength = confP->wTotalLength;
    rc = sendMessage(edP,
		            (void*)setuP,
		            (void*)confP,
		            setuP->wLength,
#if 1 /* Allow       short packets and cehck length */
		            USBD_VarOK,
#else
		            USBD_VarNotOK,
#endif
		            UsbTDIn,
		            length);
    if (rc != UsbdNoError)
    {
        DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Get configuration descriptor failed - %d!\n",*length);
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETCONFIG, 5, ERR_T_NOTRANSFER,
                   rc, *length, 0);  
        freeBuffer((void*)setuP);
  	    return UsbHost.loggedError;
    }
    dumpBuffer((char*)confP, *length);

    // Make sure we read the whole descriptor
    if (*length < confP->wTotalLength)
    {
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETCONFIG, 6, ERR_T_UNEXPECTED,
                   *length, UsbdMissingData, 0);  
  	    return UsbHost.loggedError;
    }

    if (*length < len)
    len = *length;
    if (bufP)
      memcpy((char*)bufP, (char*)confP, len);

    freeBuffer((void*)setuP);

    return UsbHost.loggedError;
}

/*----------------------------------------------------------------------
 * Get a descriptor
 */
extern
UINT XsUsbHostGetDescriptor(USBD_Device_T * devP,
		                    int recipient,
		                    int reqType,
		                    int descType,
		                    int index,
		                    void *bufP,
		                    int len)
{
    USB_EndpointDescriptor_T * edP = devP->Endpoints[0];
    USB_DeviceRequest_T * setuP;
    void * lBufP;
    int length;
    UINT rc = UsbdNoError;

    if ((setuP = (USB_DeviceRequest_T*)newBuffer()) == NULL)
    {
        DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Failed to allocate setup buffer!\n");
      	usbxStats.noBuffer++;
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETDESCRIPTOR, 1, ERR_T_NOBUFFER,
                                        usbxStats.noBuffer, UsbdNoBuffer, 0);  
  	    return UsbHost.loggedError;
    }

    lBufP = (void *)(setuP + 1);

    memset((char*)setuP,0,sizeof(USB_DeviceRequest_T));
    setuP->bmRequestType.s.toHost = 1;
    setuP->bmRequestType.s.recipient = recipient;
    setuP->bmRequestType.s.type = reqType;
    setuP->bRequest = UsbGetDescriptor;
    setuP->wValue.desc.index = index;
    setuP->wValue.desc.type = descType;
    setuP->wIndex.d = 0;
    setuP->wLength = len;

    rc = sendMessage(edP,
		            (void*)setuP,
		            (void*)lBufP,
		            setuP->wLength,
#if 1 /* Allow short packets */
		            USBD_VarOK,
#else
		            USBD_VarNotOK,
#endif
		            UsbTDIn,
		            &length);
    if (rc != UsbdNoError)
    {
        DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Get descriptor failed - %d!\n", length);
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_GETDESCRIPTOR, 2, ERR_T_UNEXPECTED,
                   rc, length, 0);  
  	    freeBuffer((void*)setuP);
  	    return UsbHost.loggedError;
    }

    if (len < length)
      len = length;
    memcpy(bufP, lBufP, len);

    freeBuffer((void*)setuP);
    return UsbdNoError;
}

/*----------------------------------------------------------------------
 * Enable a port and wait for the status to update
 */
static
UINT enablePort(int portId)
{
    // Enable the port
    hcP->HcRhPortStatus[portId].d = USB_SetPortEnable_M;
    if (!hcP->HcRhPortStatus[portId].r.PortEnableStatus)
    {
        if (hcP->HcRhPortStatus[portId].r.ConnectStatusChange)
        {
            DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Attempt to enable disconnected device!\n");
        }
        else
        {
            DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Port failed to enable!\n");
        }
    
        LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_DISCONNECTED, 1, ERR_T_UNEXPECTED,
                                        0, UsbdDisconnected, 0);  
        return UsbHost.loggedError;
    }
    
    DM_CwDbgPrintf(DM_CW_USB_HOST_0,  "Port enabled OK\n");
    
    return UsbdNoError;
}

/*----------------------------------------------------------------------
 * Reset a port and wait for the port to reset
 */
static
UINT resetPort(int portId)
{
    int waitCount = 0;

    hcP->HcRhPortStatus[portId].d = USB_SetPortReset_M;
    while(!hcP->HcRhPortStatus[portId].r.PortResetStatus)
    {
        if (hcP->HcRhPortStatus[portId].r.ConnectStatusChange)
        {
            DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Attempt to reset disconnected device!\n");
            LOGERRORX(UsbHost.loggedError, ERR_L_USB, ERR_S_XSUSB_DISCONNECTED, 2, ERR_T_UNEXPECTED,
                                        0, UsbdDisconnected, 0);  
    	    return UsbHost.loggedError;
        }

//temp   if (loopbackMode)
//      USBR_CheckLoopback();

        DM_WaitUs(500);
        waitCount++;
    }
    while(hcP->HcRhPortStatus[portId].r.PortResetStatus)
    {

//temp   if (loopbackMode)
//      USBR_CheckLoopback();

        DM_WaitUs(500);
        waitCount++;
    }
    
    DM_CwDbgPrintf(DM_CW_USB_HOST_0, "Port reset ok, wait %d ms\n", waitCount/2);

     if (waitCount/2 < 10) /* Make sure we wait the full 10ms */
       DM_WaitMs(10-waitCount/2);

    return UsbdNoError;
}

/*
*******************************************************************************
*
* FUNCTION:			addEndpoint
*
* DESCRIPTION:		This procedure adds a new edpoint for the device
*
* INPUT PARAMETERS:	INT type
*					INT functionAddr
*					INT endpointNumber
*					INT dir
*					INT speed
*					INT format
*					INT maxPacketSize
*
* RETURNS:			USB_EndpointDescriptor_T * is a pointer to endpoint descriptor
*
* GLOBAL EFFECTS:	none.
*
* ASSUMPTIONS:		none.
*
* CALLS:			newEndpoint
*
* CALLED BY:		addDevice
*
* PROTOTYPE:		USB_EndpointDescriptor_T * addEndpoint (int type,
*				       					int functionAddr,
*				       					int endpointNumber,
*				       					int dir,
*				       					int speed,
*				       					int format,
*				       					int maxPacketSize)
*
*******************************************************************************
*/
static
USB_EndpointDescriptor_T * addEndpoint (int type,
				       					int functionAddr,
				       					int endpointNumber,
				       					int dir,
				       					int speed,
				       					int format,
				       					int maxPacketSize)
{
  	USB_EndpointDescriptor_T * edP;
  	USB_EndpointDescriptor_T * listP;

  	// Get an endpoint descriptor for the default control endpoint and configure it
  	if ((edP = newEndpoint(format)) == NULL) 
  	{
    	DM_CwDbgPrintf(DM_CW_USB_HOST_1, "Failed to allocate endpoint!\n");
    	return NULL;
  	}
  
  	edP->Control.s.FunctionAddress = functionAddr;
  	edP->Control.s.EndpointNumber = endpointNumber;
  	edP->Control.s.Direction = dir;
  	edP->Control.s.Speed = speed;
  	edP->Control.s.Format = format;
  	edP->Control.s.MaximumPacketSize = maxPacketSize;

  	// Handle endpoint specific initialization
  	switch (type)
  	{
  		case UsbControl:
    			edP->NextED.p = (USB_EndpointDescriptor_T*)hcP->HcControlHeadED.p;
    			hcP->HcControlHeadED.p = edP;
    			hcP->HcControl.rw.ControlListEnable = 1;
    			break;

  		case UsbBulk:
    			edP->NextED.p = (USB_EndpointDescriptor_T*)hcP->HcBulkHeadED.p;
   				hcP->HcBulkHeadED.p = edP;
    			hcP->HcControl.rw.BulkListEnable = 1;
    			break;

  		case UsbInterrupt:
    			listP = (USB_EndpointDescriptor_T *)&periodicTableP[USBD_8MS_BASE];
    			edP->NextED.p = listP->NextED.p;
    			listP->NextED.p = edP;
    			hcP->HcControl.rw.PeriodicListEnable = 1;
    			break;

  		case UsbIsochronous:
    			listP = (USB_EndpointDescriptor_T *)&periodicTableP[USBD_1MS_BASE];
    			edP->NextED.p = listP->NextED.p;
    			listP->NextED.p = edP;
                hcP->HcControl.rw.PeriodicListEnable = 1;
    			hcP->HcControl.rw.IsochronousEnable = 1;
    			break;
  	}

  	return edP;
}

/*----------------------------------------------------------------------
 * Add a new endpoint (user level)
 */
void XsUsbHostAddEndpoint(USBD_Device_T * devP,
		                int endpointIndex,
		                int type,
		                int functionAddr,
		                int endpointNumber,
		                int dir,
		                int speed,
		                int format,
		                int maxPacketSize)
{
  devP->Endpoints[endpointIndex] = addEndpoint(type,
					       functionAddr,
					       endpointNumber,
					       dir,
					       speed,
					       format,
					       maxPacketSize);
}

/*----------------------------------------------------------------------
 * Remove an endpoint
 */
static
int removeEndpoint(USB_EndpointDescriptor_T * edP)
{
  USB_EndpointDescriptor_T * lastP, * thisP, * nextP;
  int controlListEnable = hcP->HcControl.rw.ControlListEnable;
  int bulkListEnable = hcP->HcControl.rw.BulkListEnable;

  DM_CwDbgPrintf(DM_CW_USB_HOST_0, "Remove endpoint %x\n", edP);

  if (edP != NULL) {
    /* Stop the host control list access while we remove the endpoint.
     */
    hcP->HcControl.rw.ControlListEnable = 0;
    hcP->HcControl.rw.BulkListEnable = 0;
    while(!hcP->HcInterruptStatus.rw.Startof

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -