Lots of bug fixing, and 4.7 merges
[SCSI2SD-V6.git] / include / scsi2sd.h
1 //      Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
2 //
3 //      This file is part of SCSI2SD.
4 //
5 //      SCSI2SD is free software: you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation, either version 3 of the License, or
8 //      (at your option) any later version.
9 //
10 //      SCSI2SD is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //
15 //      You should have received a copy of the GNU General Public License
16 //      along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.
17 #ifndef scsi2sd_h
18 #define scsi2sd_h
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /* Common type definitions shared between the firmware and config tools
25
26         The configuration data is now stored on the SD card, occupying the
27         last 2 sectors.
28
29         BoardConfig
30         TargetConfig (disk 0)
31         TargetConfig (disk 1)
32         TargetConfig (disk 2)
33         TargetConfig (disk 3)
34         TargetConfig (disk 4)
35         TargetConfig (disk 5)
36         TargetConfig (disk 6)
37
38 */
39
40 #include "stdint.h"
41
42 #define S2S_MAX_TARGETS 7
43 #define S2S_CFG_SIZE (S2S_MAX_TARGETS * sizeof(S2S_TargetCfg) + sizeof(S2S_BoardCfg))
44
45 typedef enum
46 {
47         S2S_CFG_TARGET_ID_BITS = 0x07,
48         S2S_CFG_TARGET_ENABLED = 0x80
49 } S2S_CFG_TARGET_FLAGS;
50
51 typedef enum
52 {
53         S2S_CFG_ENABLE_UNIT_ATTENTION = 1,
54         S2S_CFG_ENABLE_PARITY = 2,
55         S2S_CFG_ENABLE_SCSI2 = 4,
56         S2S_CFG_DISABLE_GLITCH = 8,
57         S2S_CFG_ENABLE_CACHE = 16,
58         S2S_CFG_ENABLE_DISCONNECT = 32,
59         S2S_CFG_ENABLE_SEL_LATCH = 64,
60         S2S_CFG_MAP_LUNS_TO_IDS = 128
61 } S2S_CFG_FLAGS;
62
63 typedef enum
64 {
65         S2S_CFG_ENABLE_TERMINATOR = 1
66 } S2S_CFG_FLAGS6;
67
68 typedef enum
69 {
70         S2S_CFG_FIXED,
71         S2S_CFG_REMOVEABLE,
72         S2S_CFG_OPTICAL,
73         S2S_CFG_FLOPPY_14MB,
74         S2S_CFG_MO,
75         S2S_CFG_SEQUENTIAL
76
77 } S2S_CFG_TYPE;
78
79 typedef enum
80 {
81         S2S_CFG_QUIRKS_NONE,
82         S2S_CFG_QUIRKS_APPLE,
83         S2S_CFG_QUIRKS_OMTI
84 } S2S_CFG_QUIRKS;
85
86 typedef enum
87 {
88         S2S_CFG_SPEED_NoLimit,
89         S2S_CFG_SPEED_ASYNC_15,
90         S2S_CFG_SPEED_ASYNC_33,
91         S2S_CFG_SPEED_ASYNC_50,
92         S2S_CFG_SPEED_SYNC_5,
93         S2S_CFG_SPEED_SYNC_10
94 } S2S_CFG_SPEED;
95
96 typedef struct __attribute__((packed))
97 {
98         // bits 7 -> 3 = S2S_CFG_TARGET_FLAGS
99         // bits 2 -> 0 = target SCSI ID.
100         uint8_t scsiId;
101
102         uint8_t deviceType; // S2S_CFG_TYPE
103         uint8_t flagsDEPRECATED; // S2S_CFG_FLAGS, removed in v4.5
104         uint8_t deviceTypeModifier; // Used in INQUIRY response.
105
106         uint32_t sdSectorStart;
107         uint32_t scsiSectors;
108
109         uint16_t bytesPerSector;
110
111         // Max allowed by legacy IBM-PC bios is 6 bits (63)
112         uint16_t sectorsPerTrack;
113
114         // MS-Dos up to 7.10 will crash on >= 256 heads.
115         uint16_t headsPerCylinder;
116
117
118         char vendor[8];
119         char prodId[16];
120         char revision[4];
121         char serial[16];
122
123         uint16_t quirks; // S2S_CFG_QUIRKS
124
125         uint8_t reserved[64]; // Pad out to 128 bytes for main section.
126 } S2S_TargetCfg;
127
128 typedef struct __attribute__((packed))
129 {
130         char magic[4]; // 'BCFG'
131         uint8_t flags; // S2S_CFG_FLAGS
132         uint8_t startupDelay; // Seconds.
133         uint8_t selectionDelay; // milliseconds. 255 = auto
134         uint8_t flags6; // S2S_CFG_FLAGS6
135
136         uint8_t scsiSpeed;
137
138         uint8_t reserved[119]; // Pad out to 128 bytes
139 } S2S_BoardCfg;
140
141 typedef enum
142 {
143         S2S_CMD_NONE, // Invalid
144
145         // Command content:
146         // uint8_t S2S_CFG_PING
147         // Response:
148         // S2S_CFG_STATUS
149         S2S_CMD_PING,
150
151         // Command content:
152         // uint8_t S2S_CFG_WRITEFLASH
153         // uint8_t[256] flashData
154         // uint8_t flashArray
155         // uint8_t flashRow
156         // Response:
157         // S2S_CFG_STATUS
158         S2S_CMD_WRITEFLASH,
159
160         // Command content:
161         // uint8_t S2S_CFG_READFLASH
162         // uint8_t flashArray
163         // uint8_t flashRow
164         // Response:
165         // 256 bytes of flash
166         S2S_CMD_READFLASH,
167
168         // Command content:
169         // uint8_t S2S_CFG_REBOOT
170         // Response: None.
171         S2S_CMD_REBOOT,
172
173         // Command content:
174         // uint8_t S2S_CFG_INFO
175         // Response:
176         // uint8_t[16] CSD
177         // uint8_t[16] CID
178         S2S_CMD_SDINFO,
179
180         // Command content:
181         // uint8_t S2S_CFG_SCSITEST
182         // Response:
183         // S2S_CFG_STATUS
184         // uint8_t result code (0 = passed)
185         S2S_CMD_SCSITEST,
186
187         // Command content:
188         // uint8_t S2S_CFG_DEVINFO
189         // Response:
190         // uint16_t protocol version (MSB)
191         // uint16_t firmware version (MSB)
192         // uint32_t SD capacity(MSB)
193         S2S_CMD_DEVINFO,
194
195         // Command content:
196         // uint8_t S2S_CFG_SD_WRITE
197         // uint32_t Sector Number (MSB)
198         // uint8_t[512] data
199         // Response:
200         // S2S_CFG_STATUS
201         S2S_CMD_SD_WRITE,
202
203         // Command content:
204         // uint8_t S2S_CFG_SD_READ
205         // uint32_t Sector Number (MSB)
206         // Response:
207         // 512 bytes of data
208         S2S_CMD_SD_READ,
209
210         // Command content:
211         // uint8_t S2S_CFG_DEBUG
212         // Response:
213         S2S_CMD_DEBUG,
214 } S2S_COMMAND;
215
216 typedef enum
217 {
218         S2S_CFG_STATUS_GOOD,
219         S2S_CFG_STATUS_ERR,
220         S2S_CFG_STATUS_BUSY
221 } S2S_CFG_STATUS;
222
223
224
225
226 #ifdef __cplusplus
227 } // extern "C"
228
229         #include <type_traits>
230         static_assert(
231                 std::is_pod<S2S_TargetCfg>::value, "Misuse of TargetConfig struct"
232                 );
233         static_assert(
234                 sizeof(S2S_TargetCfg) == 128,
235                 "TargetConfig struct size mismatch"
236                 );
237
238         static_assert(
239                 std::is_pod<S2S_BoardCfg>::value, "Misuse of BoardConfig struct"
240                 );
241         static_assert(
242                 sizeof(S2S_BoardCfg) == 128,
243                 "BoardConfig struct size mismatch"
244                 );
245
246 #endif
247
248 #endif