Fixed interrupt driven HID device demos not clearing the interrupt flags in all circu...
[pub/USBasp.git] / Demos / MassStorage / MassStorage.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * Header file for MassStorage.c.
34 */
35
36 #ifndef _MASS_STORAGE_H_
37 #define _MASS_STORAGE_H_
38
39 /* Includes: */
40 #include <avr/io.h>
41 #include <avr/wdt.h>
42
43 #include "Descriptors.h"
44 #include "SCSI.h"
45 #include "DataflashManager.h"
46
47 #include <LUFA/Version.h> // Library Version Information
48 #include <LUFA/Common/ButtLoadTag.h> // PROGMEM tags readable by the ButtLoad project
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
53
54 /* Macros: */
55 /** Mass Storage Class specific request to reset the Mass Storage interface, ready for the next command. */
56 #define REQ_MassStorageReset 0xFF
57
58 /** Mass Storage Class specific request to retrieve the total number of Logical Units (drives) in the SCSI device. */
59 #define REQ_GetMaxLUN 0xFE
60
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
63
64 /** Total number of Logical Units (drives) in the device. By default, the total device capacity is shared equally between
65 * each drive - this can be set to any positive non-zero amount. */
66 #define TOTAL_LUNS 2
67
68 /** Blocks in each LUN, calculated from the total capacity divided by the total number of Logical Units in the device. */
69 #define LUN_MEDIA_BLOCKS (VIRTUAL_MEMORY_BLOCKS / TOTAL_LUNS)
70
71 /** Magic signature for a Command Block Wrapper used in the Mass Storage Bulk-Only transport protocol. */
72 #define CBW_SIGNATURE 0x43425355UL
73
74 /** Magic signature for a Command Status Wrapper used in the Mass Storage Bulk-Only transport protocol. */
75 #define CSW_SIGNATURE 0x53425355UL
76
77 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from host-to-device. */
78 #define COMMAND_DIRECTION_DATA_OUT (0 << 7)
79
80 /** Mask for a Command Block Wrapper's flags attribute to specify a command with data sent from device-to-host. */
81 #define COMMAND_DIRECTION_DATA_IN (1 << 7)
82
83 /* Type defines: */
84 /** Type define for a Command Block Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
85 typedef struct
86 {
87 uint32_t Signature; /**< Command block signature, must be CBW_SIGNATURE to indicate a valid Command Block */
88 uint32_t Tag; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
89 uint32_t DataTransferLength; /** Length of the optional data portion of the issued command, in bytes */
90 uint8_t Flags; /**< Command block flags, indicating command data direction */
91 uint8_t LUN; /**< Logical Unit number this command is issued to */
92 uint8_t SCSICommandLength; /**< Length of the issued SCSI command within the SCSI command data array */
93 uint8_t SCSICommandData[MAX_SCSI_COMMAND_LENGTH]; /**< Issued SCSI command in the Command Block */
94 } CommandBlockWrapper_t;
95
96 /** Type define for a Command Status Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
97 typedef struct
98 {
99 uint32_t Signature; /**< Status block signature, must be CSW_SIGNATURE to indicate a valid Command Status */
100 uint32_t Tag; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
101 uint32_t DataTransferResidue; /**< Number of bytes of data not processed in the SCSI command */
102 uint8_t Status; /**< Status code of the issued command - a value from the MassStorage_CommandStatusCodes_t enum */
103 } CommandStatusWrapper_t;
104
105 /* Enums: */
106 /** Enum for the possible command status wrapper return status codes. */
107 enum MassStorage_CommandStatusCodes_t
108 {
109 Command_Pass = 0, /**< Command completed with no error */
110 Command_Fail = 1, /**< Command failed to complete - host may check the exact error via a SCSI REQUEST SENSE command */
111 Phase_Error = 2 /**< Command failed due to being invalid in the current phase */
112 };
113
114 /** Enum for the possible status codes for passing to the UpdateStatus() function. */
115 enum MassStorage_StatusCodes_t
116 {
117 Status_USBNotReady = 0, /**< USB is not ready (disconnected from a USB host) */
118 Status_USBEnumerating = 1, /**< USB interface is enumerating */
119 Status_USBReady = 2, /**< USB interface is connected and ready */
120 Status_CommandBlockError = 3, /**< Processing a SCSI command block from the host */
121 Status_ProcessingCommandBlock = 4, /**< Error during the processing of a SCSI command block from the host */
122 };
123
124 /* Global Variables: */
125 extern CommandBlockWrapper_t CommandBlock;
126 extern CommandStatusWrapper_t CommandStatus;
127 extern volatile bool IsMassStoreReset;
128
129 /* Task Definitions: */
130 TASK(USB_MassStorage);
131
132 /* Stream Callbacks: */
133 STREAM_CALLBACK(AbortOnMassStoreReset);
134
135 /* Event Handlers: */
136 /** Indicates that this module will catch the USB_Reset event when thrown by the library. */
137 HANDLES_EVENT(USB_Reset);
138
139 /** Indicates that this module will catch the USB_Connect event when thrown by the library. */
140 HANDLES_EVENT(USB_Connect);
141
142 /** Indicates that this module will catch the USB_Disconnect event when thrown by the library. */
143 HANDLES_EVENT(USB_Disconnect);
144
145 /** Indicates that this module will catch the USB_ConfigurationChanged event when thrown by the library. */
146 HANDLES_EVENT(USB_ConfigurationChanged);
147
148 /** Indicates that this module will catch the USB_UnhandledControlPacket event when thrown by the library. */
149 HANDLES_EVENT(USB_UnhandledControlPacket);
150
151 /* Function Prototypes: */
152 void UpdateStatus(uint8_t CurrentStatus);
153
154 #if defined(INCLUDE_FROM_MASSSTORAGE_C)
155 static bool ReadInCommandBlock(void);
156 static void ReturnCommandStatus(void);
157 #endif
158
159 #endif