X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/a67bd74e3e8aad87dcee8cf0c0eaaccbe7d00552..03ee87b35abdb8b92e8b55ec040fa943f9a6786c:/LUFA/Drivers/USB/Class/Device/MassStorage.c diff --git a/LUFA/Drivers/USB/Class/Device/MassStorage.c b/LUFA/Drivers/USB/Class/Device/MassStorage.c index 62c5f0407..941db6866 100644 --- a/LUFA/Drivers/USB/Class/Device/MassStorage.c +++ b/LUFA/Drivers/USB/Class/Device/MassStorage.c @@ -1,21 +1,21 @@ /* LUFA Library - Copyright (C) Dean Camera, 2009. + Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com www.fourwalledcubicle.com */ /* - Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, and distribute this software - and its documentation for any purpose and without fee is hereby - granted, provided that the above copyright notice appear in all - copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -28,17 +28,20 @@ this software. */ -#define INCLUDE_FROM_MS_CLASS_C +#include "../../HighLevel/USBMode.h" +#if defined(USB_CAN_BE_DEVICE) + +#define INCLUDE_FROM_MS_CLASS_DEVICE_C #include "MassStorage.h" -static USB_ClassInfo_MS_t* CallbackMSInterfaceInfo; +static volatile bool* CallbackIsResetSource; -void USB_MS_ProcessControlPacket(USB_ClassInfo_MS_t* MSInterfaceInfo) +void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { if (!(Endpoint_IsSETUPReceived())) return; - if (USB_ControlRequest.wIndex != MSInterfaceInfo->InterfaceNumber) + if (USB_ControlRequest.wIndex != MSInterfaceInfo->Config.InterfaceNumber) return; switch (USB_ControlRequest.bRequest) @@ -48,10 +51,9 @@ void USB_MS_ProcessControlPacket(USB_ClassInfo_MS_t* MSInterfaceInfo) { Endpoint_ClearSETUP(); - MSInterfaceInfo->IsMassStoreReset = true; + MSInterfaceInfo->State.IsMassStoreReset = true; - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -60,30 +62,30 @@ void USB_MS_ProcessControlPacket(USB_ClassInfo_MS_t* MSInterfaceInfo) { Endpoint_ClearSETUP(); - Endpoint_Write_Byte(MSInterfaceInfo->TotalLUNs - 1); - + Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1); Endpoint_ClearIN(); - while (!(Endpoint_IsOUTReceived())); - Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); } break; } } -bool USB_MS_ConfigureEndpoints(USB_ClassInfo_MS_t* MSInterfaceInfo) +bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { - if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->DataINEndpointNumber, EP_TYPE_BULK, - ENDPOINT_DIR_IN, MSInterfaceInfo->DataINEndpointSize, - ENDPOINT_BANK_SINGLE))) + memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State)); + + if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK, + ENDPOINT_DIR_IN, MSInterfaceInfo->Config.DataINEndpointSize, + MSInterfaceInfo->Config.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE))) { return false; } - if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->DataOUTEndpointNumber, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, MSInterfaceInfo->DataOUTEndpointSize, - ENDPOINT_BANK_SINGLE))) + if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK, + ENDPOINT_DIR_OUT, MSInterfaceInfo->Config.DataOUTEndpointSize, + MSInterfaceInfo->Config.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE))) { return false; } @@ -91,118 +93,136 @@ bool USB_MS_ConfigureEndpoints(USB_ClassInfo_MS_t* MSInterfaceInfo) return true; } -void USB_MS_USBTask(USB_ClassInfo_MS_t* MSInterfaceInfo) +void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { - if (!(USB_IsConnected)) + if (USB_DeviceState != DEVICE_STATE_Configured) return; - Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber); + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber); if (Endpoint_IsReadWriteAllowed()) { - if (USB_MS_ReadInCommandBlock(MSInterfaceInfo)) + if (MS_Device_ReadInCommandBlock(MSInterfaceInfo)) { - if (MSInterfaceInfo->CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN) - Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber); + if (MSInterfaceInfo->State.CommandBlock.Flags & MS_COMMAND_DIR_DATA_IN) + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber); - MSInterfaceInfo->CommandStatus.Status = CALLBACK_USB_MS_SCSICommandReceived(MSInterfaceInfo) ? - SCSI_Command_Pass : SCSI_Command_Fail; - MSInterfaceInfo->CommandStatus.Signature = MS_CSW_SIGNATURE; - MSInterfaceInfo->CommandStatus.Tag = MSInterfaceInfo->CommandBlock.Tag; - MSInterfaceInfo->CommandStatus.DataTransferResidue = MSInterfaceInfo->CommandBlock.DataTransferLength; - - if ((MSInterfaceInfo->CommandStatus.Status == SCSI_Command_Fail) && (MSInterfaceInfo->CommandStatus.DataTransferResidue)) - Endpoint_StallTransaction(); - - USB_MS_ReturnCommandStatus(MSInterfaceInfo); - - if (MSInterfaceInfo->IsMassStoreReset) + MSInterfaceInfo->State.CommandStatus.Status = CALLBACK_MS_Device_SCSICommandReceived(MSInterfaceInfo) ? + SCSI_Command_Pass : SCSI_Command_Fail; + MSInterfaceInfo->State.CommandStatus.Signature = MS_CSW_SIGNATURE; + MSInterfaceInfo->State.CommandStatus.Tag = MSInterfaceInfo->State.CommandBlock.Tag; + MSInterfaceInfo->State.CommandStatus.DataTransferResidue = MSInterfaceInfo->State.CommandBlock.DataTransferLength; + + if ((MSInterfaceInfo->State.CommandStatus.Status == SCSI_Command_Fail) && + (MSInterfaceInfo->State.CommandStatus.DataTransferResidue)) { - Endpoint_ResetFIFO(MSInterfaceInfo->DataOUTEndpointNumber); - Endpoint_ResetFIFO(MSInterfaceInfo->DataINEndpointNumber); - - Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber); - Endpoint_ClearStall(); - Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber); - Endpoint_ClearStall(); - - MSInterfaceInfo->IsMassStoreReset = false; + Endpoint_StallTransaction(); } + + MS_Device_ReturnCommandStatus(MSInterfaceInfo); } } + + if (MSInterfaceInfo->State.IsMassStoreReset) + { + Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber); + Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataINEndpointNumber); + + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber); + Endpoint_ClearStall(); + Endpoint_ResetDataToggle(); + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber); + Endpoint_ClearStall(); + Endpoint_ResetDataToggle(); + + MSInterfaceInfo->State.IsMassStoreReset = false; + } } -static bool USB_MS_ReadInCommandBlock(USB_ClassInfo_MS_t* MSInterfaceInfo) +static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { - Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber); + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber); - CallbackMSInterfaceInfo = MSInterfaceInfo; - Endpoint_Read_Stream_LE(&MSInterfaceInfo->CommandBlock, - (sizeof(CommandBlockWrapper_t) - 16), - StreamCallback_AbortOnMassStoreReset); - - if ((MSInterfaceInfo->CommandBlock.Signature != MS_CBW_SIGNATURE) || - (MSInterfaceInfo->CommandBlock.LUN >= MSInterfaceInfo->TotalLUNs) || - (MSInterfaceInfo->CommandBlock.SCSICommandLength > 16)) + CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset; + if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock, + (sizeof(MS_CommandBlockWrapper_t) - 16), + StreamCallback_MS_Device_AbortOnMassStoreReset)) + { + return false; + } + + if ((MSInterfaceInfo->State.CommandBlock.Signature != MS_CBW_SIGNATURE) || + (MSInterfaceInfo->State.CommandBlock.LUN >= MSInterfaceInfo->Config.TotalLUNs) || + (MSInterfaceInfo->State.CommandBlock.Flags & 0x1F) || + (MSInterfaceInfo->State.CommandBlock.SCSICommandLength == 0) || + (MSInterfaceInfo->State.CommandBlock.SCSICommandLength > 16)) { Endpoint_StallTransaction(); - Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber); + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber); Endpoint_StallTransaction(); return false; } - CallbackMSInterfaceInfo = MSInterfaceInfo; - Endpoint_Read_Stream_LE(&MSInterfaceInfo->CommandBlock.SCSICommandData, - MSInterfaceInfo->CommandBlock.SCSICommandLength, - StreamCallback_AbortOnMassStoreReset); - - Endpoint_ClearOUT(); - - if (MSInterfaceInfo->IsMassStoreReset) - return false; + CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset; + if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData, + MSInterfaceInfo->State.CommandBlock.SCSICommandLength, + StreamCallback_MS_Device_AbortOnMassStoreReset)) + { + return false; + } + Endpoint_ClearOUT(); + return true; } -static void USB_MS_ReturnCommandStatus(USB_ClassInfo_MS_t* MSInterfaceInfo) +static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { - Endpoint_SelectEndpoint(MSInterfaceInfo->DataOUTEndpointNumber); + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber); while (Endpoint_IsStalled()) { + #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); + #endif - if (MSInterfaceInfo->IsMassStoreReset) + if (MSInterfaceInfo->State.IsMassStoreReset) return; } - Endpoint_SelectEndpoint(MSInterfaceInfo->DataINEndpointNumber); + Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber); while (Endpoint_IsStalled()) { + #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); - - if (MSInterfaceInfo->IsMassStoreReset) + #endif + + if (MSInterfaceInfo->State.IsMassStoreReset) return; } - CallbackMSInterfaceInfo = MSInterfaceInfo; - Endpoint_Write_Stream_LE(&MSInterfaceInfo->CommandStatus, sizeof(CommandStatusWrapper_t), - StreamCallback_AbortOnMassStoreReset); - - Endpoint_ClearIN(); + CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset; + if (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t), + StreamCallback_MS_Device_AbortOnMassStoreReset)) + { + return; + } - if (MSInterfaceInfo->IsMassStoreReset) - return; + Endpoint_ClearIN(); } -static uint8_t StreamCallback_AbortOnMassStoreReset(void) +static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void) { - USB_MS_USBTask(CallbackMSInterfaceInfo); + #if !defined(INTERRUPT_CONTROL_ENDPOINT) + USB_USBTask(); + #endif - if (CallbackMSInterfaceInfo->IsMassStoreReset) + if (*CallbackIsResetSource) return STREAMCALLBACK_Abort; else return STREAMCALLBACK_Continue; } + +#endif