a0b23b979f11b96c0099e04a1e05108cf9b1827c
[pub/USBasp.git] / Demos / Device / LowLevel / MassStorage / MassStorage.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 #include <avr/interrupt.h>
44
45 #include "Descriptors.h"
46
47 #include "Lib/SCSI.h"
48 #include "Lib/DataflashManager.h"
49
50 #include <LUFA/Version.h>
51 #include <LUFA/Drivers/USB/USB.h>
52 #include <LUFA/Drivers/Board/LEDs.h>
53 #include <LUFA/Drivers/Board/Dataflash.h>
54
55 /* Macros: */
56 /** Mass Storage Class specific request to reset the Mass Storage interface, ready for the next command. */
57 #define REQ_MassStorageReset 0xFF
58
59 /** Mass Storage Class specific request to retrieve the total number of Logical Units (drives) in the SCSI device. */
60 #define REQ_GetMaxLUN 0xFE
61
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
64
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.
67 */
68 #define TOTAL_LUNS 1
69
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)
72
73 /** Magic signature for a Command Block Wrapper used in the Mass Storage Bulk-Only transport protocol. */
74 #define CBW_SIGNATURE 0x43425355UL
75
76 /** Magic signature for a Command Status Wrapper used in the Mass Storage Bulk-Only transport protocol. */
77 #define CSW_SIGNATURE 0x53425355UL
78
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)
81
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)
84
85 /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
86 #define LEDMASK_USB_NOTREADY LEDS_LED1
87
88 /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
89 #define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3)
90
91 /** LED mask for the library LED driver, to indicate that the USB interface is ready. */
92 #define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4)
93
94 /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
95 #define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3)
96
97 /** LED mask for the library LED driver, to indicate that the USB interface is busy. */
98 #define LEDMASK_USB_BUSY LEDS_LED2
99
100 /* Type defines: */
101 /** Type define for a Command Block Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
102 typedef struct
103 {
104 uint32_t Signature; /**< Command block signature, must be CBW_SIGNATURE to indicate a valid Command Block */
105 uint32_t Tag; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
106 uint32_t DataTransferLength; /** Length of the optional data portion of the issued command, in bytes */
107 uint8_t Flags; /**< Command block flags, indicating command data direction */
108 uint8_t LUN; /**< Logical Unit number this command is issued to */
109 uint8_t SCSICommandLength; /**< Length of the issued SCSI command within the SCSI command data array */
110 uint8_t SCSICommandData[MAX_SCSI_COMMAND_LENGTH]; /**< Issued SCSI command in the Command Block */
111 } CommandBlockWrapper_t;
112
113 /** Type define for a Command Status Wrapper, used in the Mass Storage Bulk-Only Transport protocol. */
114 typedef struct
115 {
116 uint32_t Signature; /**< Status block signature, must be CSW_SIGNATURE to indicate a valid Command Status */
117 uint32_t Tag; /**< Unique command ID value, to associate a command block wrapper with its command status wrapper */
118 uint32_t DataTransferResidue; /**< Number of bytes of data not processed in the SCSI command */
119 uint8_t Status; /**< Status code of the issued command - a value from the MassStorage_CommandStatusCodes_t enum */
120 } CommandStatusWrapper_t;
121
122 /* Enums: */
123 /** Enum for the possible command status wrapper return status codes. */
124 enum MassStorage_CommandStatusCodes_t
125 {
126 Command_Pass = 0, /**< Command completed with no error */
127 Command_Fail = 1, /**< Command failed to complete - host may check the exact error via a SCSI REQUEST SENSE command */
128 Phase_Error = 2 /**< Command failed due to being invalid in the current phase */
129 };
130
131 /* Global Variables: */
132 extern CommandBlockWrapper_t CommandBlock;
133 extern CommandStatusWrapper_t CommandStatus;
134 extern volatile bool IsMassStoreReset;
135
136 /* Function Prototypes: */
137 void SetupHardware(void);
138 void MassStorage_Task(void);
139
140 void EVENT_USB_Device_Connect(void);
141 void EVENT_USB_Device_Disconnect(void);
142 void EVENT_USB_Device_ConfigurationChanged(void);
143 void EVENT_USB_Device_UnhandledControlRequest(void);
144
145 #if defined(INCLUDE_FROM_MASSSTORAGE_C)
146 static bool ReadInCommandBlock(void);
147 static void ReturnCommandStatus(void);
148 #endif
149
150 uint8_t StreamCallback_AbortOnMassStoreReset(void);
151
152 #endif