📄 usbcbiufidevlib.c
字号:
} 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_FREE (pInquiry); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } /* Check the media type bit */ if (*(pInquiry + 1) & USB_UFI_INQUIRY_RMB_BIT) { pCbiUfiDev->blkDev.bd_removable = TRUE; } else { pCbiUfiDev->blkDev.bd_removable = FALSE; } /* * The CBI Spec states that request sense should be called after * every command whether successful or not. * * This request sense should return: * POWER ON RESET OR BUS DEVICE RESET OCCURED (0X29) */ usbCbiUfiReqSense (pCbiUfiDev); pCbiUfiDev->bulkInData = pInquiry; /* * Read the block size and capacity of device (in terms of blocks) * --------------------------------------------------------------- * * The first read capacity should fail. It will stall the pipe. After * the feature clear the following request sense should return: * * NOT READY TO READY TRANSITION - MEDIA CHANGED * or * NO MEDIA * if no disk is present. In the first case just try again, * how ever if there is no media, fake the disk size and return * the BLK_DEV. */ if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_READ_CAPACITY, 0, 0) != OK ) { OSS_FREE (pInquiry); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } if ( (s = usbCbiUfiCmdExecute (pCbiUfiDev)) == USB_COMMAND_FAILED ) { usbCbiUfiReqSense (pCbiUfiDev); /* * We ignore the sense data and instead look at the * interrupt data block which is the same. */ 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; /* fake the disk size */ pCbiUfiDev->blkDev.bd_nBlocks = (UINT32) 0xb40; pCbiUfiDev->blkDev.bd_bytesPerBlk = (UINT32) 0x200; printf ("No disk in drive\n"); OSS_FREE (pInquiry); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (&pCbiUfiDev->blkDev); } /* Here we go with the second try */ if (pCbiUfiDev->intrStatus[0] == USB_UFI_ASC_MEDIA_CHANGE) { pCbiUfiDev->bulkInData = pInquiry; if ( usbCbiUfiFormCmd (pCbiUfiDev, USB_UFI_READ_CAPACITY, 0, 0) != OK ) { OSS_FREE (pInquiry); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } if ( usbCbiUfiCmdExecute (pCbiUfiDev) != USB_COMMAND_SUCCESS ) { USB_CBI_UFI_ERR ("usbCbiUfiBlkDevCreate: Error executing "\ "USB_UFI_READ_CAPACITY command\n", 0, 0, 0, 0, 0, 0); OSS_FREE (pInquiry); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } /* This guy should return no sense data */ usbCbiUfiReqSense (pCbiUfiDev); } } else if ( s != USB_COMMAND_SUCCESS) { USB_CBI_UFI_ERR ("usbCbiUfiBlkDevCreate: Error executing "\ "USB_UFI_READ_CAPACITY command\n", 0, 0, 0, 0, 0, 0); OSS_FREE (pInquiry); OSS_MUTEX_RELEASE (cbiUfiDevMutex); return (NULL); } /* * Response is in BIG endian format. Swap it to get correct value. * Also, READ_CAPACITY returns the address of the last logical block, * NOT the number of blocks. Since the blkDev structure wants the * number of blocks, we add 1 (numBlocks = last address + 1). */ pCbiUfiDev->blkDev.bd_nBlocks = USB_SWAP_32 (*((UINT32 *) pInquiry)) + 1; pCbiUfiDev->blkDev.bd_bytesPerBlk = \ USB_SWAP_32 (*((UINT32 *) (pInquiry+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_FREE (pInquiry); 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 &
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -