📄 usbcbiufidevlib.c
字号:
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; } } } /***************************************************************************** usbCbiUfiDevFind - Searches for a USB_CBI_UFI_DEV for indicated node ID** RETURNS: pointer to matching USB_CBI_UFI_DEV or NULL if not found*/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); }/***************************************************************************** findEndpoint - Searches for a BULK endpoint of the indicated direction.** RETURNS: pointer to matching endpoint descriptor or NULL if not found*/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); }/***************************************************************************** usbCbiUfiPhysDevCreate - create USB_CBI_UFI_DEV Structure for the device * * This function is invoked from the dynamic attach callback routine whenever * a USB_CBI_UFI_DEV device is attached. It allocates memory for the structure,* sets device configuration, and creates pipe for bulk-in,bulk-out and interrupt* endpoints. It also sets the device configuration so that it follows UFI* command set.* * RETURNS: pUSB_CBI_UFI_DEV on success, NULL if failed to create device.*/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 bfr[255]; /* store for descriptors */ UINT8 * pBfr = bfr; /* pointer to the above store */ UINT ifNo; pUSB_CBI_UFI_DEV pCbiUfiDev; pUSB_CONFIG_DESCR pCfgDescr; pUSB_INTERFACE_DESCR pIfDescr; pUSB_ENDPOINT_DESCR pOutEp; pUSB_ENDPOINT_DESCR pInEp; pUSB_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 ((pCbiUfiDev = OSS_CALLOC (sizeof (*pCbiUfiDev))) == NULL) { USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Unable to allocate memory\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, 255, bfr, &actLength) != OK) { USB_CBI_UFI_ERR ("usbCbiUfiPhysDevCreate: Unable to read configuration "\ "descriptor\n", 0, 0, 0, 0, 0, 0); goto errorExit; } if ((pCfgDescr = usbDescrParse (bfr, 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; while ((pIfDescr = usbDescrParseSkip (&pBfr, &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 (pBfr, actLength, USB_ATTR_BULK, USB_ENDPOINT_OUT)) == NULL) goto errorExit; if ((pInEp = findEndpoint (pBfr, actLength, USB_ATTR_BULK, USB_ENDPOINT_IN)) == NULL) goto errorExit; if ((pIntrEp = findEndpoint (pBfr, 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, FROM_LITTLEW(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); return (pCbiUfiDev);errorExit: /* Error in creating CBI UFI Device */ usbCbiUfiDevDestroy (pCbiUfiDev); return (NULL); }/***************************************************************************** usbCbiUfiBlkDevCreate - create a block device.** This routine initializes a BLK_DEV structure, which describes a * logical partition on a USB_CBI_UFI_DEV device. A logical partition is * an array of contiguously addressed blocks; it can be completely described * by the number of blocks and the address of the first block in the partition. ** RETURNS: A pointer to the BLK_DEV, or NULL if no CBI/UFI device exists.*/BLK_DEV * usbCbiUfiBlkDevCreate ( USBD_NODE_ID nodeId /* Node Id of the CBI_UFI device */ ) { UINT8 inquiry[UFI_STD_INQUIRY_LEN]; /* store for INQUIRY data */ USB_COMMAND_STATUS s; pUSB_CBI_UFI_DEV pCbiUfiDev = usbCbiUfiDevFind (nodeId);/* pUSB_CBI_UFI_DEV pCbiUfiDev = usbListFirst (&cbiUfiDevList);*/ if (pCbiUfiDev == NULL) { USB_CBI_UFI_ERR ("usbCbiUfiBlkDevCreate: No MSC/CBI/UFI found\n", 0, 0, 0, 0, 0, 0); return (NULL); } OSS_MUTEX_TAKE (cbiUfiDevMutex, OSS_BLOCK); /* * Initialise the standard block device structure for use * with file systems */ pCbiUfiDev->blkDev.bd_blkRd = (FUNCPTR) usbCbiUfiDevBlkRd; pCbiUfiDev->blkDev.bd_blkWrt = (FUNCPTR) usbCbiUfiDevBlkWrt; pCbiUfiDev->blkDev.bd_ioctl = (FUNCPTR) usbCbiUfiDevIoctl; pCbiUfiDev->blkDev.bd_reset = (FUNCPTR) NULL; pCbiUfiDev->blkDev.bd_statusChk = (FUNCPTR) usbCbiUfiDevStChk; pCbiUfiDev->blkDev.bd_retry = 1; pCbiUfiDev->blkDev.bd_mode = O_RDWR; pCbiUfiDev->blkDev.bd_readyChanged = TRUE; /* * Read out the standard INQUIRY information from the device, mainly to * check whether the media is removable or not. */ pCbiUfiDev->bulkInData = inquiry; if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_INQUIRY, NULL, NULL) != OK ) { USB_CBI_UFI_ERR ("usbCbiUfiBlkDevCreate: Error forming command\n", 0, 0, 0, 0, 0, 0); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } if ( usbCbiUfiCmdExecute (pCbiUfiDev) != USB_COMMAND_SUCCESS) { USB_CBI_UFI_ERR ("usbCbiUfiBlkDevCreate: Error executing "\ "USB_UFI_INQUIRY command\n", 0, 0, 0, 0, 0, 0); usbCbiUfiReqSense (pCbiUfiDev); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -