X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/bb23e55f11a42f6214757483d010ebd24377caeb..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 073111e64..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 @@ -34,9 +34,9 @@ #define INCLUDE_FROM_MS_CLASS_DEVICE_C #include "MassStorage.h" -static USB_ClassInfo_MS_Device_t* CallbackMSInterfaceInfo; +static volatile bool* CallbackIsResetSource; -void MS_Device_ProcessControlPacket(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) +void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { if (!(Endpoint_IsSETUPReceived())) return; @@ -53,8 +53,7 @@ void MS_Device_ProcessControlPacket(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) MSInterfaceInfo->State.IsMassStoreReset = true; - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -63,30 +62,30 @@ void MS_Device_ProcessControlPacket(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) { Endpoint_ClearSETUP(); - Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1); - + Endpoint_Write_Byte(MSInterfaceInfo->Config.TotalLUNs - 1); Endpoint_ClearIN(); - while (!(Endpoint_IsOUTReceived())); - Endpoint_ClearOUT(); + Endpoint_ClearStatusStage(); } break; } } -bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) +bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { + memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State)); + if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber, EP_TYPE_BULK, ENDPOINT_DIR_IN, MSInterfaceInfo->Config.DataINEndpointSize, - ENDPOINT_BANK_SINGLE))) + MSInterfaceInfo->Config.DataINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE))) { return false; } if (!(Endpoint_ConfigureEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber, EP_TYPE_BULK, ENDPOINT_DIR_OUT, MSInterfaceInfo->Config.DataOUTEndpointSize, - ENDPOINT_BANK_SINGLE))) + MSInterfaceInfo->Config.DataOUTEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : ENDPOINT_BANK_SINGLE))) { return false; } @@ -94,9 +93,9 @@ bool MS_Device_ConfigureEndpoints(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) return true; } -void MS_Device_USBTask(USB_ClassInfo_MS_Device_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->Config.DataOUTEndpointNumber); @@ -121,34 +120,41 @@ void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) } 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_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber); - Endpoint_ClearStall(); - } } } - MSInterfaceInfo->State.IsMassStoreReset = false; + 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 MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) +static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber); - CallbackMSInterfaceInfo = MSInterfaceInfo; - Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock, - (sizeof(CommandBlockWrapper_t) - 16), - StreamCallback_MS_Device_AbortOnMassStoreReset); - + 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(); @@ -158,26 +164,28 @@ static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* MSInterfaceI return false; } - CallbackMSInterfaceInfo = MSInterfaceInfo; - Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData, - MSInterfaceInfo->State.CommandBlock.SCSICommandLength, - StreamCallback_MS_Device_AbortOnMassStoreReset); - - Endpoint_ClearOUT(); - - if (MSInterfaceInfo->State.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 MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* MSInterfaceInfo) +static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo) { Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber); while (Endpoint_IsStalled()) { + #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); + #endif if (MSInterfaceInfo->State.IsMassStoreReset) return; @@ -187,27 +195,31 @@ static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* MSInterface while (Endpoint_IsStalled()) { + #if !defined(INTERRUPT_CONTROL_ENDPOINT) USB_USBTask(); - + #endif + if (MSInterfaceInfo->State.IsMassStoreReset) return; } - CallbackMSInterfaceInfo = MSInterfaceInfo; - Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(CommandStatusWrapper_t), - StreamCallback_MS_Device_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->State.IsMassStoreReset) - return; + Endpoint_ClearIN(); } static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void) { - MS_Device_USBTask(CallbackMSInterfaceInfo); + #if !defined(INTERRUPT_CONTROL_ENDPOINT) + USB_USBTask(); + #endif - if (CallbackMSInterfaceInfo->State.IsMassStoreReset) + if (*CallbackIsResetSource) return STREAMCALLBACK_Abort; else return STREAMCALLBACK_Continue;