X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/ed031c1df2f5b053b9cd9f48c63e66a42b7c049e..e21b620bf6cc9fb1bb9e516efb57b1cb12c00085:/Demos/Host/MassStorageHost/MassStoreCommands.c?ds=inline diff --git a/Demos/Host/MassStorageHost/MassStoreCommands.c b/Demos/Host/MassStorageHost/MassStoreCommands.c index f4143adcf..4ebc67a77 100644 --- a/Demos/Host/MassStorageHost/MassStoreCommands.c +++ b/Demos/Host/MassStorageHost/MassStoreCommands.c @@ -37,8 +37,8 @@ * to a FAT library to give file-level access to an attached device's contents. * * \note Many Mass Storage devices on the market are non-compliant to the - * specifications and thus can proove difficult to interface with. It - * may be neccesary to retry the functions in the module several times + * specifications and thus can prove difficult to interface with. It + * may be necessary to retry the functions in the module several times * after they have returned and error to successfully send the command * to the device. Some devices may also need to have the stream function * timeout period extended beyond 100ms (some badly designed devices exceeding @@ -52,17 +52,17 @@ /* Globals: */ /** Current CBW to send to the device. This is automatically filled by the routines - * in this file and is not externally accessable. + * in this file and is not externally accessible. */ static CommandBlockWrapper_t SCSICommandBlock; /** Current CSW received from the device. This is automatically filled by the routines - * in this file and is externally accessable so that the return codes may be checked. + * in this file and is externally accessible so that the return codes may be checked. */ CommandStatusWrapper_t SCSICommandStatus; /** Current Tag value used in issued CBWs to the device. This is automatically incremented - * by the routines in this file, and is not externally accessable. + * by the routines in this file, and is not externally accessible. */ static uint32_t MassStore_Tag = 1; @@ -75,7 +75,7 @@ static uint8_t MassStore_SendCommand(void) { uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError; - /* Each transmission should have a unique tag value, excluding valued 0 and 0xFFFFFFFF */ + /* Each transmission should have a unique tag value, excluding values 0 and 0xFFFFFFFF */ if (++MassStore_Tag == 0xFFFFFFFF) MassStore_Tag = 1; @@ -88,7 +88,7 @@ static uint8_t MassStore_SendCommand(void) return ErrorCode; /* Send the data in the OUT pipe to the attached device */ - Pipe_ClearCurrentBank(); + Pipe_ClearOUT(); /* Some buggy devices require a delay here before the pipe freezing or they will lock up */ USB_Host_WaitMS(1); @@ -117,12 +117,12 @@ static uint8_t MassStore_WaitForDataReceived(void) Pipe_Unfreeze(); /* Wait until data received in the IN pipe */ - while (!(Pipe_ReadWriteAllowed())) + while (!(Pipe_IsINReceived())) { /* Check to see if a new frame has been issued (1ms elapsed) */ if (USB_INT_HasOccurred(USB_INT_HSOFI)) { - /* Clear the flag and decrement the timout period counter */ + /* Clear the flag and decrement the timeout period counter */ USB_INT_Clear(USB_INT_HSOFI); TimeoutMSRem--; @@ -183,6 +183,9 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr) /* Read in the block data from the pipe */ if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError) return ErrorCode; + + /* Acknowledge the packet */ + Pipe_ClearIN(); } else { @@ -193,10 +196,10 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr) /* Write the block data to the pipe */ if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError) return ErrorCode; + + /* Acknowledge the packet */ + Pipe_ClearOUT(); } - - /* Acknowledge the packet */ - Pipe_ClearCurrentBank(); /* Some buggy devices require a delay here before the pipe freezing or they will lock up */ USB_Host_WaitMS(1); @@ -215,6 +218,10 @@ static uint8_t MassStore_GetReturnedStatus(void) { uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError; + /* If an error in the command ocurred, abort */ + if ((ErrorCode == MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_ERROR_NoError) + return ErrorCode; + /* Select the IN data pipe for data reception */ Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE); Pipe_Unfreeze(); @@ -224,7 +231,7 @@ static uint8_t MassStore_GetReturnedStatus(void) return ErrorCode; /* Clear the data ready for next reception */ - Pipe_ClearCurrentBank(); + Pipe_ClearIN(); /* Some buggy devices require a delay here before the pipe freezing or they will lock up */ USB_Host_WaitMS(1); @@ -243,15 +250,18 @@ static uint8_t MassStore_GetReturnedStatus(void) */ uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum) { - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT), - bRequest: REQ_ClearFeature, - wValue: FEATURE_ENDPOINT_HALT, - wIndex: EndpointNum, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT), + .bRequest = REQ_ClearFeature, + .wValue = FEATURE_ENDPOINT_HALT, + .wIndex = EndpointNum, + .wLength = 0, }; + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + return USB_Host_SendControlRequest(NULL); } @@ -262,15 +272,18 @@ uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum) */ uint8_t MassStore_MassStorageReset(void) { - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), - bRequest: REQ_MassStorageReset, - wValue: 0, - wIndex: 0, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_MassStorageReset, + .wValue = 0, + .wIndex = 0, + .wLength = 0, }; + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + return USB_Host_SendControlRequest(NULL); } @@ -285,15 +298,18 @@ uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex) { uint8_t ErrorCode; - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE), - bRequest: REQ_GetMaxLUN, - wValue: 0, - wIndex: 0, - wLength: 1, + .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_GetMaxLUN, + .wValue = 0, + .wIndex = 0, + .wLength = 1, }; + /* Select the control pipe for the request transfer */ + Pipe_SelectPipe(PIPE_CONTROLPIPE); + if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled) { /* Clear the pipe stall */ @@ -321,17 +337,17 @@ uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_ /* Create a CBW with a SCSI command to issue REQUEST SENSE command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: sizeof(SCSI_Request_Sense_Response_t), - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 6 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t), + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 6 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_REQUEST_SENSE, 0x00, // Reserved @@ -388,17 +404,17 @@ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAd /* Create a CBW with a SCSI command to read in the given blocks from the device */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: ((uint32_t)Blocks * BlockSize), - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 10 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = ((uint32_t)Blocks * BlockSize), + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 10 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_READ_10, 0x00, // Unused (control bits, all off) @@ -459,17 +475,17 @@ uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockA /* Create a CBW with a SCSI command to write the given blocks to the device */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: ((uint32_t)Blocks * BlockSize), - Flags: COMMAND_DIRECTION_DATA_OUT, - LUN: LUNIndex, - SCSICommandLength: 10 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = ((uint32_t)Blocks * BlockSize), + .Flags = COMMAND_DIRECTION_DATA_OUT, + .LUN = LUNIndex, + .SCSICommandLength = 10 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_WRITE_10, 0x00, // Unused (control bits, all off) @@ -518,17 +534,17 @@ uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex) /* Create a CBW with a SCSI command to issue TEST UNIT READY command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: 0, - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 6 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = 0, + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 6 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_TEST_UNIT_READY, 0x00, // Reserved @@ -567,17 +583,17 @@ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const Ca /* Create a CBW with a SCSI command to issue READ CAPACITY command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: 8, - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 10 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = 8, + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 10 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_READ_CAPACITY_10, 0x00, // Reserved @@ -639,17 +655,17 @@ uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool P /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: 0, - Flags: COMMAND_DIRECTION_DATA_OUT, - LUN: LUNIndex, - SCSICommandLength: 6 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = 0, + .Flags = COMMAND_DIRECTION_DATA_OUT, + .LUN = LUNIndex, + .SCSICommandLength = 6 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL, 0x00, // Reserved