3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Header file for MassStorage.c. 
  36 #ifndef _MASS_STORAGE_H_ 
  37 #define _MASS_STORAGE_H_ 
  42                 #include <avr/power.h> 
  44                 #include "Descriptors.h" 
  46                 #include "DataflashManager.h" 
  48                 #include <LUFA/Version.h>                    // Library Version Information 
  49                 #include <LUFA/Drivers/USB/USB.h>            // USB Functionality 
  50                 #include <LUFA/Drivers/Board/LEDs.h>         // LEDs driver 
  51                 #include <LUFA/Drivers/Board/Dataflash.h>    // Dataflash chip driver 
  52                 #include <LUFA/Scheduler/Scheduler.h>        // Simple scheduler for task management 
  55                 /** Mass Storage Class specific request to reset the Mass Storage interface, ready for the next command. */ 
  56                 #define REQ_MassStorageReset       0xFF 
  58                 /** Mass Storage Class specific request to retrieve the total number of Logical Units (drives) in the SCSI device. */ 
  59                 #define REQ_GetMaxLUN              0xFE 
  61                 /** Maximum length of a SCSI command which can be issued by the device or host in a Mass Storage bulk wrapper. */ 
  62                 #define MAX_SCSI_COMMAND_LENGTH    16 
  64                 /** Total number of Logical Units (drives) in the device. The total device capacity is shared equally between 
  65                  *  each drive - this can be set to any positive non-zero amount. 
  69                 /** Blocks in each LUN, calculated from the total capacity divided by the total number of Logical Units in the device. */ 
  70                 #define LUN_MEDIA_BLOCKS           (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS)     
  72                 /** Magic signature for a Command Block Wrapper used in the Mass Storage Bulk-Only transport protocol. */ 
  73                 #define CBW_SIGNATURE              0x43425355UL 
  75                 /** Magic signature for a Command Status Wrapper used in the Mass Storage Bulk-Only transport protocol. */ 
  76                 #define CSW_SIGNATURE              0x53425355UL 
  78                 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from host-to-device. */ 
  79                 #define COMMAND_DIRECTION_DATA_OUT (0 << 7) 
  81                 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from device-to-host. */ 
  82                 #define COMMAND_DIRECTION_DATA_IN  (1 << 7) 
  85                 /** Type define for a Command Block Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */ 
  88                         uint32_t Signature
; /**< Command block signature, must be CBW_SIGNATURE to indicate a valid Command Block */ 
  89                         uint32_t Tag
; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */ 
  90                         uint32_t DataTransferLength
; /** Length of the optional data portion of the issued command, in bytes */ 
  91                         uint8_t  Flags
; /**< Command block flags, indicating command data direction */ 
  92                         uint8_t  LUN
; /**< Logical Unit number this command is issued to */ 
  93                         uint8_t  SCSICommandLength
; /**< Length of the issued SCSI command within the SCSI command data array */ 
  94                         uint8_t  SCSICommandData
[MAX_SCSI_COMMAND_LENGTH
]; /**< Issued SCSI command in the Command Block */ 
  95                 } CommandBlockWrapper_t
; 
  97                 /** Type define for a Command Status Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */ 
 100                         uint32_t Signature
; /**< Status block signature, must be CSW_SIGNATURE to indicate a valid Command Status */ 
 101                         uint32_t Tag
; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */ 
 102                         uint32_t DataTransferResidue
; /**< Number of bytes of data not processed in the SCSI command */ 
 103                         uint8_t  Status
; /**< Status code of the issued command - a value from the MassStorage_CommandStatusCodes_t enum */ 
 104                 } CommandStatusWrapper_t
; 
 107                 /** Enum for the possible command status wrapper return status codes. */ 
 108                 enum MassStorage_CommandStatusCodes_t
 
 110                         Command_Pass 
= 0, /**< Command completed with no error */ 
 111                         Command_Fail 
= 1, /**< Command failed to complete - host may check the exact error via a SCSI REQUEST SENSE command */ 
 112                         Phase_Error  
= 2  /**< Command failed due to being invalid in the current phase */ 
 115                 /** Enum for the possible status codes for passing to the UpdateStatus() function. */ 
 116                 enum MassStorage_StatusCodes_t
 
 118                         Status_USBNotReady            
= 0, /**< USB is not ready (disconnected from a USB host) */ 
 119                         Status_USBEnumerating         
= 1, /**< USB interface is enumerating */ 
 120                         Status_USBReady               
= 2, /**< USB interface is connected and ready */ 
 121                         Status_CommandBlockError      
= 3, /**< Processing a SCSI command block from the host */ 
 122                         Status_ProcessingCommandBlock 
= 4, /**< Error during the processing of a SCSI command block from the host */ 
 125         /* Global Variables: */ 
 126                 extern CommandBlockWrapper_t  CommandBlock
; 
 127                 extern CommandStatusWrapper_t CommandStatus
; 
 128                 extern volatile bool          IsMassStoreReset
; 
 130         /* Task Definitions: */ 
 131                 TASK(USB_MassStorage
); 
 133         /* Stream Callbacks: */ 
 134                 STREAM_CALLBACK(AbortOnMassStoreReset
); 
 136         /* Event Handlers: */ 
 137                 /** Indicates that this module will catch the USB_Reset event when thrown by the library. */ 
 138                 HANDLES_EVENT(USB_Reset
); 
 140                 /** Indicates that this module will catch the USB_Connect event when thrown by the library. */ 
 141                 HANDLES_EVENT(USB_Connect
); 
 143                 /** Indicates that this module will catch the USB_Disconnect event when thrown by the library. */ 
 144                 HANDLES_EVENT(USB_Disconnect
); 
 146                 /** Indicates that this module will catch the USB_ConfigurationChanged event when thrown by the library. */ 
 147                 HANDLES_EVENT(USB_ConfigurationChanged
); 
 149                 /** Indicates that this module will catch the USB_UnhandledControlPacket event when thrown by the library. */ 
 150                 HANDLES_EVENT(USB_UnhandledControlPacket
); 
 152         /* Function Prototypes: */ 
 153                 void UpdateStatus(uint8_t CurrentStatus
); 
 155                 #if defined(INCLUDE_FROM_MASSSTORAGE_C) 
 156                         static bool ReadInCommandBlock(void); 
 157                         static void ReturnCommandStatus(void);