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>
50 #include <LUFA/Drivers/USB/USB.h>
51 #include <LUFA/Drivers/Board/LEDs.h>
52 #include <LUFA/Drivers/Board/Dataflash.h>
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)
84 /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
85 #define LEDMASK_USB_NOTREADY LEDS_LED1
87 /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
88 #define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3)
90 /** LED mask for the library LED driver, to indicate that the USB interface is ready. */
91 #define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4)
93 /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
94 #define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3)
96 /** LED mask for the library LED driver, to indicate that the USB interface is busy. */
97 #define LEDMASK_USB_BUSY (LEDS_LED2)
100 /** Type define for a Command Block Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
103 uint32_t Signature
; /**< Command block signature, must be CBW_SIGNATURE to indicate a valid Command Block */
104 uint32_t Tag
; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
105 uint32_t DataTransferLength
; /** Length of the optional data portion of the issued command, in bytes */
106 uint8_t Flags
; /**< Command block flags, indicating command data direction */
107 uint8_t LUN
; /**< Logical Unit number this command is issued to */
108 uint8_t SCSICommandLength
; /**< Length of the issued SCSI command within the SCSI command data array */
109 uint8_t SCSICommandData
[MAX_SCSI_COMMAND_LENGTH
]; /**< Issued SCSI command in the Command Block */
110 } CommandBlockWrapper_t
;
112 /** Type define for a Command Status Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
115 uint32_t Signature
; /**< Status block signature, must be CSW_SIGNATURE to indicate a valid Command Status */
116 uint32_t Tag
; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
117 uint32_t DataTransferResidue
; /**< Number of bytes of data not processed in the SCSI command */
118 uint8_t Status
; /**< Status code of the issued command - a value from the MassStorage_CommandStatusCodes_t enum */
119 } CommandStatusWrapper_t
;
122 /** Enum for the possible command status wrapper return status codes. */
123 enum MassStorage_CommandStatusCodes_t
125 Command_Pass
= 0, /**< Command completed with no error */
126 Command_Fail
= 1, /**< Command failed to complete - host may check the exact error via a SCSI REQUEST SENSE command */
127 Phase_Error
= 2 /**< Command failed due to being invalid in the current phase */
130 /* Global Variables: */
131 extern CommandBlockWrapper_t CommandBlock
;
132 extern CommandStatusWrapper_t CommandStatus
;
133 extern volatile bool IsMassStoreReset
;
135 /* Function Prototypes: */
136 void SetupHardware(void);
137 void MassStorage_Task(void);
139 void EVENT_USB_Device_Connect(void);
140 void EVENT_USB_Device_Disconnect(void);
141 void EVENT_USB_Device_ConfigurationChanged(void);
142 void EVENT_USB_Device_UnhandledControlRequest(void);
144 #if defined(INCLUDE_FROM_MASSSTORAGE_C)
145 static bool ReadInCommandBlock(void);
146 static void ReturnCommandStatus(void);
149 uint8_t StreamCallback_AbortOnMassStoreReset(void);