📄 usbcbiufidevlib.c
字号:
} /* Check the media type bit */ if (inquiry[1] & USB_UFI_INQUIRY_RMB_BIT) { pCbiUfiDev->blkDev.bd_removable = TRUE; } else { pCbiUfiDev->blkDev.bd_removable = FALSE; } usbCbiUfiReqSense (pCbiUfiDev); pCbiUfiDev->bulkInData = inquiry; /* read the block size and capacity of device (in terms of blocks) */ if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_READ_CAPACITY, NULL, NULL) != OK ) { OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } /* * Read Capacity command will usually return CHECK CONDITION status * very first time, just try again. */ if ( (s = usbCbiUfiCmdExecute (pCbiUfiDev)) == USB_COMMAND_FAILED ) { usbCbiUfiReqSense (pCbiUfiDev); pCbiUfiDev->bulkInData = inquiry; if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_READ_CAPACITY, NULL, NULL) != OK ) { OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } if ( usbCbiUfiCmdExecute (pCbiUfiDev) != USB_COMMAND_SUCCESS ) { OSS_MUTEX_RELEASE (cbiUfiDevMutex); USB_CBI_UFI_ERR ("usbCbiUfiBlkDevCreate: Error executing "\ "USB_UFI_READ_CAPACITY command\n", 0, 0, 0, 0, 0, 0); return (NULL); } usbCbiUfiReqSense (pCbiUfiDev); } else if ( s != USB_COMMAND_SUCCESS) { OSS_MUTEX_RELEASE (cbiUfiDevMutex); USB_CBI_UFI_ERR ("usbCbiUfiBlkDevCreate: Error executing "\ "USB_UFI_READ_CAPACITY command\n", 0, 0, 0, 0, 0, 0); return (NULL); } /* Response is in BIG endian format. Swap it to get correct value */ pCbiUfiDev->blkDev.bd_nBlocks = USB_SWAP_32(*((UINT32 *)inquiry)); pCbiUfiDev->blkDev.bd_bytesPerBlk = USB_SWAP_32(*((UINT32 *)(inquiry+4))); USB_CBI_UFI_DEBUG ("usbCbiUfiBlkDevCreate: Number of Blocks : %x, " "Block Size: %x \n", pCbiUfiDev->blkDev.bd_nBlocks, pCbiUfiDev->blkDev.bd_bytesPerBlk, 0, 0, 0, 0 ); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (&pCbiUfiDev->blkDev); } /***************************************************************************** usbCbiUfiDevBlkRd - routine to read one or more blocks from the device.** This routine reads the specified physical sector(s) from a specified* physical device. Typically called by file system when data is to be* read from a particular device.** RETURNS: OK on success, or ERROR if failed to read from device*/LOCAL STATUS usbCbiUfiDevBlkRd ( BLK_DEV * pBlkDev, /* pointer to UFI device */ UINT32 blkNum, /* logical block number */ UINT32 numBlks, /* number of blocks to read */ char * pBuf /* store for data */ ) { USB_COMMAND_STATUS s; pUSB_CBI_UFI_DEV pCbiUfiDev = (USB_CBI_UFI_DEV *)pBlkDev; USB_CBI_UFI_DEBUG ("usbCbiUfiDevBlkRd: Reading from block number: %x\n", blkNum, 0, 0, 0, 0, 0); OSS_MUTEX_TAKE (cbiUfiDevMutex, OSS_BLOCK); /* initialise the pointer to fetch bulk out data */ pCbiUfiDev->bulkInData = (UINT8 *)pBuf; if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_READ10, blkNum, numBlks) != OK ) { OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (ERROR); } s = usbCbiUfiCmdExecute (pCbiUfiDev); if ( s != USB_COMMAND_SUCCESS ) { if ( s == USB_COMMAND_FAILED) usbCbiUfiDevReset (pBlkDev); usbCbiUfiReqSense (pCbiUfiDev); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (ERROR); } OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (OK); }/***************************************************************************** usbCbiUfiDevBlkWrt - routine to write one or more blocks to the device.** This routine writes the specified physical sector(s) to a specified physical* device.** RETURNS: OK on success, or ERROR if failed to write to device*/LOCAL STATUS usbCbiUfiDevBlkWrt ( BLK_DEV * pBlkDev, /* pointer to UFI device */ UINT32 blkNum, /* logical block number */ UINT32 numBlks, /* number of blocks to write */ char * pBuf /* data to be written */ ) { USB_COMMAND_STATUS s; pUSB_CBI_UFI_DEV pCbiUfiDev = (USB_CBI_UFI_DEV *)pBlkDev; USB_CBI_UFI_DEBUG ("usbCbiUfiDevBlkWrt: Writing from block number: %x\n", blkNum, 0, 0, 0, 0, 0); OSS_MUTEX_TAKE (cbiUfiDevMutex, OSS_BLOCK); /* initialise the pointer to fetch bulk out data */ pCbiUfiDev->bulkOutData = (UINT8 *)pBuf; if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_WRITE10, blkNum, numBlks) != OK ) { OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (ERROR); } s = usbCbiUfiCmdExecute (pCbiUfiDev); /* * check for the status of the write operation. If failed while * transferring ADSC or any FATAL error, do a block reset. */ if ( s != USB_COMMAND_SUCCESS ) { if ( s == USB_COMMAND_FAILED) { usbCbiUfiDevReset (pBlkDev); } usbCbiUfiReqSense (pCbiUfiDev); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (ERROR); } OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (OK); }/***************************************************************************** usbCbiUfiIrpCallback - Invoked upon IRP completion** Examines the status of the IRP submitted. * ** RETURNS: N/A*/LOCAL VOID usbCbiUfiIrpCallback ( pVOID p /* pointer to the IRP submitted */ ) { pUSB_IRP pIrp = (pUSB_IRP) p; pUSB_CBI_UFI_DEV pCbiUfiDev = pIrp->userPtr; /* check whether the IRP was for bulk out/ bulk in / status transport */ if (pIrp == &(pCbiUfiDev->outIrp)) { if (pIrp->result == OK) /* check the result of IRP */ { USB_CBI_UFI_DEBUG ("usbCbiUfiIrpCallback: Num of Bytes transferred on "\ "out pipe %d\n", pIrp->bfrList[0].actLen, 0, 0, 0, 0, 0); } else { USB_CBI_UFI_ERR ("usbCbiUfiIrpCallback: Irp failed on Bulk Out %x \n", pIrp->result, 0, 0, 0, 0, 0); /* Clear HALT Feature on Bulk out Endpoint */ if ((usbdFeatureClear (usbdHandle, pCbiUfiDev->cbiUfiDevId, USB_RT_ENDPOINT, USB_FSEL_DEV_ENDPOINT_HALT, (pCbiUfiDev->outEpAddress & 0x0F))) != OK) { USB_CBI_UFI_ERR ("usbCbiUfiIrpCallback: Failed to clear HALT "\ "feature on bulk out Endpoint %x\n", 0, 0, 0, 0, 0, 0); } } } else if (pIrp == &(pCbiUfiDev->inIrp)) /* IRP for bulk_in data */ { if (pIrp->result == OK) { USB_CBI_UFI_DEBUG ("usbCbiUfiIrpCallback: Num of Bytes read from Bulk "\ "In =%d\n", pIrp->bfrList[0].actLen, 0, 0, 0, 0, 0); } else /* IRP on BULK IN failed */ { USB_CBI_UFI_ERR ("usbCbiUfiIrpCallback : Irp failed on Bulk in ,%x\n", pIrp->result, 0, 0, 0, 0, 0); /* Clear HALT Feature on Bulk in Endpoint */ if ((usbdFeatureClear (usbdHandle, pCbiUfiDev->cbiUfiDevId, USB_RT_ENDPOINT, USB_FSEL_DEV_ENDPOINT_HALT, (pCbiUfiDev->inEpAddress & 0x0F))) != OK) { USB_CBI_UFI_ERR ("usbCbiUfiIrpCallback: Failed to clear HALT "\ "feature on bulk in Endpoint %x\n", 0, 0, 0, 0, 0, 0); } } } else if (pIrp == &(pCbiUfiDev->statusIrp)) /* IRP for bulk_in data */ { if (pIrp->result == OK) { USB_CBI_UFI_DEBUG ("usbCbiUfiIrpCallback: Num of Bytes read from "\ "status pipe =%d, %d, %d\n", pIrp->bfrList[0].actLen, pCbiUfiDev->intrStatus[0], pCbiUfiDev->intrStatus[1], 0, 0, 0); } else /* IRP on BULK IN failed */ { USB_CBI_UFI_ERR ("usbCbiUfiIrpCallback : Irp failed on interrupt pipe ,%x\n", pIrp->result, 0, 0, 0, 0, 0); /* Clear HALT Feature on Bulk in Endpoint */ if ((usbdFeatureClear (usbdHandle, pCbiUfiDev->cbiUfiDevId, USB_RT_ENDPOINT, USB_FSEL_DEV_ENDPOINT_HALT, (pCbiUfiDev->intrEpAddress & 0x0F))) != OK) { USB_CBI_UFI_ERR ("usbCbiUfiIrpCallback: Failed to clear HALT "\ "feature on bulk in Endpoint %x\n", 0, 0, 0, 0, 0, 0); } } } OSS_SEM_GIVE (cbiUfiIrpSem); } /***************************************************************************** usbCbiUfiDevStChk - routine to check device status** Typically called by the file system before doing a read/write to the device.* The routine issues a TEST_UNIT_READY command. Also, if the device is not* ready, then the sense data is read from the device to check for presence/* change of media. For removable devices, the ready change flag is set to * indicate media change. File system checks this flag, and remounts the device** RETURNS: OK if the device is ready for IO, or ERROR if device is busy. */LOCAL STATUS usbCbiUfiDevStChk ( BLK_DEV * pBlkDev /* pointer to MSC/CBI/UFI device */ ) { UINT8 senseData[USB_UFI_MS_HEADER_LEN]; /* store for SENSE data */ USB_COMMAND_STATUS s ; STATUS rtnStatus = OK; pUSB_CBI_UFI_DEV pCbiUfiDev = (USB_CBI_UFI_DEV *)pBlkDev; OSS_MUTEX_TAKE (cbiUfiDevMutex, OSS_BLOCK); USB_CBI_UFI_DEBUG ("usbCbiUfiDevStChk: About to send TEST_UNIT_READY\n", 0, 0, 0, 0, 0, 0); /* Form a Test Unit Ready command and send it */ if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_TEST_UNIT_READY, NULL, NULL) != OK ) { OSS_MUTEX_RELEASE (cbiUfiDevMutex); USB_CBI_UFI_ERR ("usbCbiUfiDevStChk: Error forming command\n", 0, 0, 0, 0, 0, 0); return (ERROR); } if (usbCbiUfiCmdExecute (pCbiUfiDev) != USB_COMMAND_SUCCESS) { usbCbiUfiReqSense (pCbiUfiDev); OSS_MUTEX_RELEASE (cbiUfiDevMutex); USB_CBI_UFI_DEBUG ("usbCbiUfiDevStChk: device not ready\n", 0, 0, 0, 0, 0, 0); return (ERROR); } else if (pCbiUfiDev->intrStatus[0] != USB_UFI_NO_SENSE) { if (pCbiUfiDev->intrStatus[0] == USB_UFI_ASC_NO_MEDIA) { USB_CBI_UFI_DEBUG ("usbCbiUfiDevStChk: Media not present\n", 0, 0, 0, 0, 0, 0); /* declare that device has to be mounted on next IO operation */ if (pCbiUfiDev->blkDev.bd_removable == TRUE) pCbiUfiDev->blkDev.bd_readyChanged = TRUE; rtnStatus = ERROR; } /* check for media change */ else if (pCbiUfiDev->intrStatus[0] == USB_UFI_ASC_MEDIA_CHANGE) { USB_CBI_UFI_DEBUG ("usbCbiUfiDevStChk: Media changed\n", 0, 0, 0, 0, 0, 0); /* declare that
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -