Endpoint configuration is now refined to give better output when all configurations...
[pub/USBasp.git] / Demos / Device / 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 #include <avr/power.h>
43
44 #include "Descriptors.h"
45 #include "SCSI.h"
46 #include "DataflashManager.h"
47
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
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. The total device capacity is shared equally between
65 * each drive - this can be set to any positive non-zero amount.
66 */
67 #define TOTAL_LUNS 2
68
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)
71
72 /** Magic signature for a Command Block Wrapper used in the Mass Storage Bulk-Only transport protocol. */
73 #define CBW_SIGNATURE 0x43425355UL
74
75 /** Magic signature for a Command Status Wrapper used in the Mass Storage Bulk-Only transport protocol. */
76 #define CSW_SIGNATURE 0x53425355UL
77
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)
80
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)
83
84 /* Type defines: */
85 /** Type define for a Command Block Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
86 typedef struct
87 {
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;
96
97 /** Type define for a Command Status Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
98 typedef struct
99 {
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;
105
106 /* Enums: */
107 /** Enum for the possible command status wrapper return status codes. */
108 enum MassStorage_CommandStatusCodes_t
109 {
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 */
113 };
114
115 /** Enum for the possible status codes for passing to the UpdateStatus() function. */
116 enum MassStorage_StatusCodes_t
117 {
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 */
123 };
124
125 /* Global Variables: */
126 extern CommandBlockWrapper_t CommandBlock;
127 extern CommandStatusWrapper_t CommandStatus;
128 extern volatile bool IsMassStoreReset;
129
130 /* Task Definitions: */
131 TASK(USB_MassStorage);
132
133 /* Stream Callbacks: */
134 STREAM_CALLBACK(AbortOnMassStoreReset);
135
136 /* Event Handlers: */
137 /** Indicates that this module will catch the USB_Reset event when thrown by the library. */
138 HANDLES_EVENT(USB_Reset);
139
140 /** Indicates that this module will catch the USB_Connect event when thrown by the library. */
141 HANDLES_EVENT(USB_Connect);
142
143 /** Indicates that this module will catch the USB_Disconnect event when thrown by the library. */
144 HANDLES_EVENT(USB_Disconnect);
145
146 /** Indicates that this module will catch the USB_ConfigurationChanged event when thrown by the library. */
147 HANDLES_EVENT(USB_ConfigurationChanged);
148
149 /** Indicates that this module will catch the USB_UnhandledControlPacket event when thrown by the library. */
150 HANDLES_EVENT(USB_UnhandledControlPacket);
151
152 /* Function Prototypes: */
153 void UpdateStatus(uint8_t CurrentStatus);
154
155 #if defined(INCLUDE_FROM_MASSSTORAGE_C)
156 static bool ReadInCommandBlock(void);
157 static void ReturnCommandStatus(void);
158 #endif
159
160 #endif