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"
47 #include "Lib/DataflashManager.h"
49 #include <LUFA/Version.h> // Library Version Information
50 #include <LUFA/Drivers/USB/USB.h> // USB Functionality
51 #include <LUFA/Drivers/Board/LEDs.h> // LEDs driver
52 #include <LUFA/Drivers/Board/Dataflash.h> // Dataflash chip driver
53 #include <LUFA/Scheduler/Scheduler.h> // Simple scheduler for task management
56 /** Mass Storage Class specific request to reset the Mass Storage interface, ready for the next command. */
57 #define REQ_MassStorageReset 0xFF
59 /** Mass Storage Class specific request to retrieve the total number of Logical Units (drives) in the SCSI device. */
60 #define REQ_GetMaxLUN 0xFE
62 /** Maximum length of a SCSI command which can be issued by the device or host in a Mass Storage bulk wrapper. */
63 #define MAX_SCSI_COMMAND_LENGTH 16
65 /** Total number of Logical Units (drives) in the device. The total device capacity is shared equally between
66 * each drive - this can be set to any positive non-zero amount.
70 /** Blocks in each LUN, calculated from the total capacity divided by the total number of Logical Units in the device. */
71 #define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS)
73 /** Magic signature for a Command Block Wrapper used in the Mass Storage Bulk-Only transport protocol. */
74 #define CBW_SIGNATURE 0x43425355UL
76 /** Magic signature for a Command Status Wrapper used in the Mass Storage Bulk-Only transport protocol. */
77 #define CSW_SIGNATURE 0x53425355UL
79 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from host-to-device. */
80 #define COMMAND_DIRECTION_DATA_OUT (0 << 7)
82 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from device-to-host. */
83 #define COMMAND_DIRECTION_DATA_IN (1 << 7)
86 /** Type define for a Command Block Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
89 uint32_t Signature
; /**< Command block signature, must be CBW_SIGNATURE to indicate a valid Command Block */
90 uint32_t Tag
; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
91 uint32_t DataTransferLength
; /** Length of the optional data portion of the issued command, in bytes */
92 uint8_t Flags
; /**< Command block flags, indicating command data direction */
93 uint8_t LUN
; /**< Logical Unit number this command is issued to */
94 uint8_t SCSICommandLength
; /**< Length of the issued SCSI command within the SCSI command data array */
95 uint8_t SCSICommandData
[MAX_SCSI_COMMAND_LENGTH
]; /**< Issued SCSI command in the Command Block */
96 } CommandBlockWrapper_t
;
98 /** Type define for a Command Status Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
101 uint32_t Signature
; /**< Status block signature, must be CSW_SIGNATURE to indicate a valid Command Status */
102 uint32_t Tag
; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
103 uint32_t DataTransferResidue
; /**< Number of bytes of data not processed in the SCSI command */
104 uint8_t Status
; /**< Status code of the issued command - a value from the MassStorage_CommandStatusCodes_t enum */
105 } CommandStatusWrapper_t
;
108 /** Enum for the possible command status wrapper return status codes. */
109 enum MassStorage_CommandStatusCodes_t
111 Command_Pass
= 0, /**< Command completed with no error */
112 Command_Fail
= 1, /**< Command failed to complete - host may check the exact error via a SCSI REQUEST SENSE command */
113 Phase_Error
= 2 /**< Command failed due to being invalid in the current phase */
116 /** Enum for the possible status codes for passing to the UpdateStatus() function. */
117 enum MassStorage_StatusCodes_t
119 Status_USBNotReady
= 0, /**< USB is not ready (disconnected from a USB host) */
120 Status_USBEnumerating
= 1, /**< USB interface is enumerating */
121 Status_USBReady
= 2, /**< USB interface is connected and ready */
122 Status_CommandBlockError
= 3, /**< Processing a SCSI command block from the host */
123 Status_ProcessingCommandBlock
= 4, /**< Error during the processing of a SCSI command block from the host */
126 /* Global Variables: */
127 extern CommandBlockWrapper_t CommandBlock
;
128 extern CommandStatusWrapper_t CommandStatus
;
129 extern volatile bool IsMassStoreReset
;
131 /* Task Definitions: */
132 TASK(USB_MassStorage
);
134 /* Function Prototypes: */
135 void EVENT_USB_Connect(void);
136 void EVENT_USB_Disconnect(void);
137 void EVENT_USB_ConfigurationChanged(void);
138 void EVENT_USB_UnhandledControlPacket(void);
140 void UpdateStatus(uint8_t CurrentStatus
);
142 #if defined(INCLUDE_FROM_MASSSTORAGE_C)
143 static bool ReadInCommandBlock(void);
144 static void ReturnCommandStatus(void);
147 uint8_t StreamCallback_AbortOnMassStoreReset(void);