usbcbiufidevlib.c

来自「MPC5200 BSP 支持ATA,USB, I2C,扩展网口」· C语言 代码 · 共 2,132 行 · 第 1/5 页

C
2,132
字号
        }     if ( usbCbiUfiCmdExecute (pCbiUfiDev) != USB_COMMAND_SUCCESS )        {        USB_CBI_UFI_ERR ("usbCbiUfiReqSense: Error executing "\                         "USB_UFI_REQUEST_SENSE command\n", 0, 0, 0, 0, 0, 0);                OSS_MUTEX_RELEASE (cbiUfiDevMutex);        }    else         {                 /* Request Sense command success. Check the sense key & qualifiers. */        switch (*(pReqSense + USB_UFI_SENSE_KEY_OFFSET))            {            case USB_UFI_NO_SENSE:                USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: No Sense key, %d, %d\n",                                   *(pReqSense + USB_UFI_SENSE_ASC),                                    *(pReqSense + USB_UFI_SENSE_ASCQ), 0, 0, 0, 0);                break;            case USB_UFI_RECOVERED_ERROR:                USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Recovered Error, %d, %d\n",                                   *(pReqSense + USB_UFI_SENSE_ASC),                                    *(pReqSense + USB_UFI_SENSE_ASCQ), 0, 0, 0, 0);                break;            case USB_UFI_NOT_READY:                USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Device not ready, %d, %d \n",                                   *(pReqSense + USB_UFI_SENSE_ASC),                                    *(pReqSense + USB_UFI_SENSE_ASCQ), 0, 0, 0, 0);                break;            case USB_UFI_MEDIUM_ERROR:                USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Medium Error, %d, %d\n",                                   *(pReqSense + USB_UFI_SENSE_ASC),                                    *(pReqSense + USB_UFI_SENSE_ASCQ), 0, 0, 0, 0);                break;            case USB_UFI_HARDWARE_ERROR:                USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Hardware Error, %d, %d\n",				    *(pReqSense + USB_UFI_SENSE_ASC),                                    *(pReqSense + USB_UFI_SENSE_ASCQ), 0, 0, 0, 0);                break;            case USB_UFI_ILL_REQUEST:                USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Illegal Request, %d, %d\n",                                   *(pReqSense + USB_UFI_SENSE_ASC),                                    *(pReqSense + USB_UFI_SENSE_ASCQ), 0, 0, 0, 0);                break;            case USB_UFI_UNIT_ATTN:                USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Unit Attention, %d, %d\n",                                   *(pReqSense + USB_UFI_SENSE_ASC),                                    *(pReqSense + USB_UFI_SENSE_ASCQ), 0, 0, 0, 0);                /* If Unit attention condition, check the ASC and ASCQ */                  status = (*(pReqSense + 12)  << 8) & 0xFF00;                status = status | *(pReqSense + 13);                switch (status)                    {                    case USB_UFI_WRITE_PROTECT :                        USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Media write "\                                           "protected\n", 0, 0, 0, 0, 0 ,0);                        break;                     case USB_UFI_MEDIA_CHANGE :                          USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Media changed\n",                                           0, 0, 0, 0, 0 ,0);                        break;                    case USB_UFI_POWER_ON_RESET :                          USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Power on reset"\                                           " Bus device reset\n",0, 0, 0, 0, 0 ,0);                        break;                    case USB_UFI_COMMAND_SUCCESS :                          USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: No sense"\                                           "Bus device reset\n",0, 0, 0, 0, 0 ,0);                        break;                    }                break;            case USB_UFI_DATA_PROTECT:                  USB_CBI_UFI_DEBUG ("usbCbiUfiReqSense: Write Protect\n",                                   0, 0, 0, 0, 0, 0);                break;            default :                break;            }        }    OSS_FREE (pReqSense);    }        LOCAL pUSB_CBI_UFI_DEV usbCbiUfiDevFind    (    USBD_NODE_ID nodeId          /* node ID to be looked for */    )    {    pUSB_CBI_UFI_DEV pCbiUfiDev = usbListFirst (&cbiUfiDevList);    /* browse through the list */    while (pCbiUfiDev != NULL)        {        if (pCbiUfiDev->cbiUfiDevId == nodeId)            break;        pCbiUfiDev = usbListNext (&pCbiUfiDev->cbiUfiDevLink);        }    return (pCbiUfiDev);    }LOCAL pUSB_ENDPOINT_DESCR findEndpoint    (    pUINT8 pBfr,    UINT16 bfrLen,    UINT8  attribute,     UINT16 direction    )    {    pUSB_ENDPOINT_DESCR pEp;    while ((pEp = usbDescrParseSkip (&pBfr, &bfrLen, USB_DESCR_ENDPOINT))            != NULL)        {        if ((pEp->attributes & USB_ATTR_EPTYPE_MASK) == attribute &&            (pEp->endpointAddress & USB_ENDPOINT_DIR_MASK) == direction)            break;        }    return (pEp);    }LOCAL pUSB_CBI_UFI_DEV usbCbiUfiPhysDevCreate    (    USBD_NODE_ID nodeId,         /* USBD Node Id ofthe device  */         UINT16       configuration,  /* Configuration value        */     UINT16       interface       /* Interface Number           */     )    {    UINT16   actLength;    UINT8 * pBfr;           /* store for descriptors      */     UINT8 * pScratchBfr;    /* pointer to the above store */     UINT     ifNo;      USB_CBI_UFI_DEV * pCbiUfiDev;    USB_CONFIG_DESCR * pCfgDescr;    USB_INTERFACE_DESCR * pIfDescr;    USB_ENDPOINT_DESCR * pOutEp;    USB_ENDPOINT_DESCR * pInEp;    USB_ENDPOINT_DESCR * pIntrEp;    /*     * A new device is being attached.	Check if we already      * have a structure for this device.     */    if ((pCbiUfiDev = usbCbiUfiDevFind (nodeId)) != NULL)        return (pCbiUfiDev);    /* Allocate memory for a new structure to represent this device */    if ((pBfr = OSS_MALLOC (USB_MAX_DESCR_LEN)) == NULL)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Unable to allocate \				memory:pBfr\n", 0, 0, 0, 0, 0, 0);        goto errorExit;        }    if ((pCbiUfiDev = OSS_CALLOC (sizeof (*pCbiUfiDev))) == NULL)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Unable to allocate \				memory:pCbiUfiDev\n", 0, 0, 0, 0, 0, 0);        goto errorExit;        }    pCbiUfiDev->cbiUfiDevId     = nodeId;     pCbiUfiDev->configuration   = configuration;    pCbiUfiDev->interface       = interface;    pCbiUfiDev->connected       = TRUE;    /* Check out the device configuration */    /* Configuration index is assumed to be one less than config'n value */    if (usbdDescriptorGet (usbdHandle, 			   pCbiUfiDev->cbiUfiDevId, 			   USB_RT_STANDARD | USB_RT_DEVICE, 			   USB_DESCR_CONFIGURATION, 			   (configuration - 1), 			   0, 			   USB_MAX_DESCR_LEN, 			   pBfr, 			   &actLength) 			!= OK)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Unable to read configuration "\                         "descriptor\n", 0, 0, 0, 0, 0, 0);        goto errorExit;        }    if ((pCfgDescr = usbDescrParse (pBfr, 				    actLength, 				    USB_DESCR_CONFIGURATION)) 				  == NULL)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Unable to find configuration "\                         "descriptor\n", 0, 0, 0, 0, 0, 0);        goto errorExit;        }       /* Look for the interface representing the USB_CBI_UFI Device. */    ifNo = 0;    /*     * usbDescrParseSkip() modifies the value of the pointer it recieves     * so we pass it a copy of our buffer pointer     */    pScratchBfr = pBfr;    while ((pIfDescr = usbDescrParseSkip (&pScratchBfr, 					  &actLength, 					  USB_DESCR_INTERFACE)) 					!= NULL)        {        if (ifNo == pCbiUfiDev->interface)            break;        ifNo++;        }    if (pIfDescr == NULL)        goto errorExit;    pCbiUfiDev->altSetting = pIfDescr->alternateSetting;    /*      * Retrieve the endpoint descriptor(s) following the identified interface     * descriptor.     */    if ((pOutEp = findEndpoint (pScratchBfr, 				actLength, 				USB_ATTR_BULK, 				USB_ENDPOINT_OUT)) 			    == NULL)        goto errorExit;    if ((pInEp = findEndpoint (pScratchBfr, 				       actLength, 				       USB_ATTR_BULK, 				       USB_ENDPOINT_IN)) 			   == NULL)        goto errorExit;    if ((pIntrEp = findEndpoint (pScratchBfr, 				 actLength, 				 USB_ATTR_INTERRUPT, 				 USB_ENDPOINT_IN)) 			      == NULL)        goto errorExit;     pCbiUfiDev->outEpAddress = pOutEp->endpointAddress;    pCbiUfiDev->inEpAddress  = pInEp->endpointAddress;    pCbiUfiDev->intrEpAddress  = pIntrEp->endpointAddress;    /* Set the device configuration corresponding to USB_CBI_UFI_DEV */    if ((usbdConfigurationSet (usbdHandle, pCbiUfiDev->cbiUfiDevId,                                pCbiUfiDev->configuration,                                pCfgDescr->maxPower * USB_POWER_MA_PER_UNIT)) 			      != OK )        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Unable to set device "\                         "configuration \n", 0, 0, 0, 0, 0, 0);        goto errorExit;        }    else        {        USB_CBI_UFI_DEBUG ("usbCbiUfiPhysDevCreate: Configuration set to 0x%x \n",                           pCbiUfiDev->configuration, 0, 0, 0, 0, 0);        }       /* Select interface     *     * NOTE: Some devices may reject this command, and this does not represent    * a fatal error.  Therefore, we ignore the return status.    */    usbdInterfaceSet (usbdHandle, 		      pCbiUfiDev->cbiUfiDevId, 		      pCbiUfiDev->interface, 		      pCbiUfiDev->altSetting);    /* Create a Bulk-out pipe for the USB_CBI_UFI_DEV device */    if (usbdPipeCreate (usbdHandle, 			pCbiUfiDev->cbiUfiDevId, 			pOutEp->endpointAddress, 			pCbiUfiDev->configuration, 			pCbiUfiDev->interface, 			USB_XFRTYPE_BULK, 			USB_DIR_OUT, 			FROM_LITTLEW(pOutEp->maxPacketSize),			0, 			0, 			&(pCbiUfiDev->outPipeHandle)) 		!= OK)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Error creating bulk out pipe\n",                         0, 0, 0, 0, 0, 0);        goto errorExit;        }     /* Create a Bulk-in pipe for the USB_CBI_UFI_DEV device */           if (usbdPipeCreate (usbdHandle, pCbiUfiDev->cbiUfiDevId,                         pInEp->endpointAddress, pCbiUfiDev->configuration,                         pCbiUfiDev->interface, USB_XFRTYPE_BULK, USB_DIR_IN,                         FROM_LITTLEW(pInEp->maxPacketSize), 0, 0,                         &(pCbiUfiDev->inPipeHandle)) != OK)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Error creating bulk in pipe\n",                         0, 0, 0, 0, 0, 0);        goto errorExit;        }     /* Create a Interrupt pipe for the USB_CBI_UFI_DEV device */    if ((usbdPipeCreate (usbdHandle, pCbiUfiDev->cbiUfiDevId,                         (pIntrEp->endpointAddress & 0xf), 			pCbiUfiDev->configuration,                         pCbiUfiDev->interface, 			USB_XFRTYPE_INTERRUPT, 			USB_DIR_IN,                         2, 			2, 			0x10,                         &(pCbiUfiDev->intrPipeHandle))) 			!= OK)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Error creating interrupt pipe %x\n",                         0, 0, 0, 0, 0, 0);        goto errorExit;        }     /* Clear HALT feauture on the endpoints */    if ((usbdFeatureClear (usbdHandle, 			   pCbiUfiDev->cbiUfiDevId, 			   USB_RT_ENDPOINT,                            USB_FSEL_DEV_ENDPOINT_HALT,                            (pOutEp->endpointAddress & 0x0F))) 			   != OK)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Failed to clear HALT feauture "\                         "on bulk out Endpoint %x\n", 0, 0, 0, 0, 0, 0);        }      if ((usbdFeatureClear (usbdHandle, 			   pCbiUfiDev->cbiUfiDevId, 			   USB_RT_ENDPOINT,                            USB_FSEL_DEV_ENDPOINT_HALT,                            (pOutEp->endpointAddress & 0x0F))) 			   != OK)        {        USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Failed to clear HALT feauture "\                         "on bulk in Endpoint %x\n", 0, 0, 0, 0, 0, 0);        }     /* Link the newly created structure. */    usbListLink (&cbiUfiDevList, pCbiUfiDev,                  &pCbiUfiDev->cbiUfiDevLink, LINK_TAIL);    OSS_FREE (pBfr);    return (pCbiUfiDev);errorExit:    /* Error in creating CBI UFI Device */    usbCbiUfiDevDestroy (pCbiUfiDev);    OSS_FREE (pBfr);    return (NULL);    }BLK_DEV * usbCbiUfiBlkDevCreate     (    USBD_NODE_ID nodeId        /* Node Id of the CBI_UFI device */     )    {    UINT8 * pInquiry;    /* store for INQUIRY data  */        USB_COMMAND_STATUS  s;     pUSB_CBI_UFI_DEV pCbiUfiDev = usbCbiUfiDevFind (nodeId);

⌨️ 快捷键说明

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