Improved reliability of USB config interface (port from V5 branch)
[SCSI2SD-V6.git] / src / firmware / config.c
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
18 #include "config.h"
19 #include "led.h"
20 #include "bsp.h"
21 #include "scsi.h"
22 #include "scsiPhy.h"
23 #include "sd.h"
24 #include "disk.h"
25 #include "bootloader.h"
26 #include "spinlock.h"
27
28 #include "../../include/scsi2sd.h"
29 #include "../../include/hidpacket.h"
30
31 #include "usb_device/usb_device.h"
32 #include "usb_device/usbd_hid.h"
33 #include "usb_device/usbd_composite.h"
34 #include "bsp_driver_sd.h"
35
36
37 #include <string.h>
38
39 static const uint16_t FIRMWARE_VERSION = 0x0632;
40
41 // Optional static config
42 extern uint8_t* __fixed_config;
43
44 #ifdef S2S_USB_HS
45 #define configUsbDev hUsbDeviceHS
46 #else
47 #define configUsbDev hUsbDeviceFS
48 #endif
49
50 // 1 flash row
51 static const uint8_t DEFAULT_CONFIG[128] =
52 {
53 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x3F, 0x00,
54 0x00, 0x02, 0x3F, 0x00, 0xFF, 0x00, 0x20, 0x63, 0x6F, 0x64, 0x65, 0x73,
55 0x72, 0x63, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x53,
56 0x43, 0x53, 0x49, 0x32, 0x53, 0x44, 0x20, 0x31, 0x2E, 0x30, 0x31, 0x32,
57 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
58 0x37, 0x38, 0x00, 0x00
59 };
60
61
62 static uint8_t s2s_cfg[S2S_CFG_SIZE] S2S_DMA_ALIGN;
63 static uint8_t configDmaBuf[512] S2S_DMA_ALIGN; // For SD card writes.
64
65
66 enum USB_STATE
67 {
68 USB_IDLE,
69 USB_DATA_SENT
70 };
71
72
73 static int usbInEpState;
74
75 static void s2s_debugTimer();
76
77 // Debug timer to log via USB.
78 // Timer 6 & 7 is a simple counter with no external IO supported.
79 static s2s_lock_t usbDevLock = s2s_lock_init;
80 TIM_HandleTypeDef htim7;
81 static int debugTimerStarted = 0;
82 void TIM7_IRQHandler()
83 {
84 HAL_TIM_IRQHandler(&htim7);
85 }
86 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
87 {
88 if (s2s_spin_trylock(&usbDevLock)) {
89 s2s_debugTimer();
90 s2s_spin_unlock(&usbDevLock);
91 }
92 }
93
94 void s2s_configInit(S2S_BoardCfg* config)
95 {
96 usbInEpState = USB_IDLE;
97
98 if (memcmp(__fixed_config, "BCFG", 4) == 0)
99 {
100 // Use hardcoded config
101 memcpy(s2s_cfg, __fixed_config, S2S_CFG_SIZE);
102 memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
103 }
104
105 else if ((blockDev.state & DISK_PRESENT) && sdDev.capacity)
106 {
107 int cfgSectors = (S2S_CFG_SIZE + 511) / 512;
108 BSP_SD_ReadBlocks_DMA(
109 &s2s_cfg[0],
110 sdDev.capacity - cfgSectors,
111 cfgSectors);
112
113 memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
114
115 if (memcmp(config->magic, "BCFG", 4))
116 {
117 // Invalid SD card config, use default.
118 memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);
119 memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
120 memcpy(config->magic, "BCFG", 4);
121 config->selectionDelay = 255; // auto
122 config->flags6 = S2S_CFG_ENABLE_TERMINATOR;
123
124 memcpy(
125 &s2s_cfg[0] + sizeof(S2S_BoardCfg),
126 DEFAULT_CONFIG,
127 sizeof(S2S_TargetCfg));
128 }
129 }
130 else
131 {
132 // No SD card, use existing config if valid
133 if (memcmp(config->magic, "BCFG", 4))
134 {
135 // Not valid, use empty config with no disks.
136 memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);
137 memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
138 config->selectionDelay = 255; // auto
139 config->flags6 = S2S_CFG_ENABLE_TERMINATOR;
140 }
141 }
142 }
143
144 static void debugInit(void)
145 {
146 if (debugTimerStarted == 1) return;
147
148 debugTimerStarted = 1;
149 // 10ms debug timer to capture logs over USB
150 __TIM7_CLK_ENABLE();
151 htim7.Instance = TIM7;
152 htim7.Init.Prescaler = 10800 - 1; // 16bit. 108MHz down to 10KHz
153 htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
154 htim7.Init.Period = 100 - 1; // 16bit. 10KHz down to 10ms.
155 htim7.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
156 HAL_TIM_Base_Init(&htim7);
157 HAL_TIM_Base_Start_IT(&htim7);
158
159 HAL_NVIC_SetPriority(TIM7_IRQn, 10, 0);
160 HAL_NVIC_EnableIRQ(TIM7_IRQn);
161 }
162
163
164 static void
165 pingCommand()
166 {
167 uint8_t response[] =
168 {
169 S2S_CFG_STATUS_GOOD
170 };
171 hidPacket_send(response, sizeof(response));
172 }
173
174 static void
175 sdInfoCommand()
176 {
177 uint8_t response[sizeof(sdDev.csd) + sizeof(sdDev.cid)];
178 memcpy(response, sdDev.csd, sizeof(sdDev.csd));
179 memcpy(response + sizeof(sdDev.csd), sdDev.cid, sizeof(sdDev.cid));
180
181 hidPacket_send(response, sizeof(response));
182 }
183
184
185 static void
186 scsiTestCommand()
187 {
188 int resultCode = scsiSelfTest();
189 uint8_t response[] =
190 {
191 resultCode == 0 ? S2S_CFG_STATUS_GOOD : S2S_CFG_STATUS_ERR,
192 resultCode
193 };
194 hidPacket_send(response, sizeof(response));
195 }
196
197 static void
198 scsiDevInfoCommand()
199 {
200 uint8_t response[] =
201 {
202 FIRMWARE_VERSION >> 8,
203 FIRMWARE_VERSION & 0xff,
204 sdDev.capacity >> 24,
205 sdDev.capacity >> 16,
206 sdDev.capacity >> 8,
207 sdDev.capacity,
208 1 // useSdConfig, always true for V6.
209 };
210 hidPacket_send(response, sizeof(response));
211 }
212
213 static void
214 debugCommand()
215 {
216 uint8_t response[32];
217 memcpy(&response, &scsiDev.cdb, 12);
218 response[12] = scsiDev.msgIn;
219 response[13] = scsiDev.msgOut;
220 response[14] = scsiDev.lastStatus;
221 response[15] = scsiDev.lastSense;
222 response[16] = scsiDev.phase;
223 response[17] = *SCSI_STS_SCSI;
224 response[18] = scsiDev.target != NULL ? scsiDev.target->syncOffset : 0;
225 response[19] = scsiDev.target != NULL ? scsiDev.target->syncPeriod : 0;
226 response[20] = scsiDev.minSyncPeriod;
227 response[21] = scsiDev.rstCount;
228 response[22] = scsiDev.selCount;
229 response[23] = scsiDev.msgCount;
230 response[24] = scsiDev.cmdCount;
231 response[25] = scsiDev.watchdogTick;
232 response[26] = blockDev.state;
233 response[27] = scsiDev.lastSenseASC >> 8;
234 response[28] = scsiDev.lastSenseASC;
235 response[29] = *SCSI_STS_DBX & 0xff; // What we've read
236 response[30] = *SCSI_STS_SELECTED;
237 response[31] = *SCSI_STS_DBX >> 8; // What we're writing
238 hidPacket_send(response, sizeof(response));
239 }
240
241 static void
242 sdWriteCommand(const uint8_t* cmd, size_t cmdSize)
243 {
244 if (cmdSize < 517)
245 {
246 return; // ignore.
247 }
248 uint32_t lba =
249 (((uint32_t)cmd[1]) << 24) |
250 (((uint32_t)cmd[2]) << 16) |
251 (((uint32_t)cmd[3]) << 8) |
252 ((uint32_t)cmd[4]);
253
254 memcpy(configDmaBuf, &cmd[5], 512);
255 BSP_SD_WriteBlocks_DMA(configDmaBuf, lba, 1);
256
257 uint8_t response[] =
258 {
259 S2S_CFG_STATUS_GOOD
260 };
261 hidPacket_send(response, sizeof(response));
262 }
263
264 static void
265 sdReadCommand(const uint8_t* cmd, size_t cmdSize)
266 {
267 if (cmdSize < 5)
268 {
269 return; // ignore.
270 }
271 uint32_t lba =
272 (((uint32_t)cmd[1]) << 24) |
273 (((uint32_t)cmd[2]) << 16) |
274 (((uint32_t)cmd[3]) << 8) |
275 ((uint32_t)cmd[4]);
276
277 BSP_SD_ReadBlocks_DMA(configDmaBuf, lba, 1);
278 hidPacket_send(configDmaBuf, 512);
279 }
280
281 static void
282 processCommand(const uint8_t* cmd, size_t cmdSize)
283 {
284 switch (cmd[0])
285 {
286 case S2S_CMD_PING:
287 pingCommand();
288 break;
289
290 case S2S_CMD_REBOOT:
291 s2s_enterBootloader();
292 break;
293
294 case S2S_CMD_SDINFO:
295 sdInfoCommand();
296 break;
297
298 case S2S_CMD_SCSITEST:
299 scsiTestCommand();
300 break;
301
302 case S2S_CMD_DEVINFO:
303 scsiDevInfoCommand();
304 break;
305
306 case S2S_CMD_SD_WRITE:
307 sdWriteCommand(cmd, cmdSize);
308 break;
309
310 case S2S_CMD_SD_READ:
311 sdReadCommand(cmd, cmdSize);
312 break;
313
314 case S2S_CMD_DEBUG:
315 if (debugTimerStarted == 0) {
316 debugInit();
317 }
318 debugCommand();
319 break;
320
321 case S2S_CMD_NONE: // invalid
322 default:
323 break;
324 }
325 }
326
327 void s2s_configPoll()
328 {
329 s2s_spin_lock(&usbDevLock);
330
331 if (!USBD_Composite_IsConfigured(&configUsbDev))
332 {
333 usbInEpState = USB_IDLE;
334 hidPacket_reset();
335 goto out;
336 }
337
338 if (USBD_HID_IsReportReady(&configUsbDev))
339 {
340 // Check if we have a previous command outstanding
341 // before accepting another
342 size_t cmdSize;
343 if (hidPacket_peekPacket(&cmdSize) == NULL)
344 {
345 uint8_t hidBuffer[USBHID_LEN];
346 int byteCount = USBD_HID_GetReport(&configUsbDev, hidBuffer, sizeof(hidBuffer));
347 hidPacket_recv(hidBuffer, byteCount);
348 }
349 }
350
351 if (hidPacket_getHIDBytesReady() == 0) // Nothing queued to send
352 {
353 size_t cmdSize;
354 const uint8_t* cmd = hidPacket_getPacket(&cmdSize);
355 if (cmd && (cmdSize > 0))
356 {
357 s2s_ledOn();
358 processCommand(cmd, cmdSize);
359 s2s_ledOff();
360 }
361 }
362
363 switch (usbInEpState)
364 {
365 case USB_IDLE:
366 {
367 uint8_t hidBuffer[USBHID_LEN];
368 const uint8_t* nextChunk = hidPacket_getHIDBytes(hidBuffer);
369
370 if (nextChunk)
371 {
372 USBD_HID_SendReport (&configUsbDev, nextChunk, sizeof(hidBuffer));
373 usbInEpState = USB_DATA_SENT;
374 }
375 }
376 break;
377
378 case USB_DATA_SENT:
379 if (!USBD_HID_IsBusy(&configUsbDev))
380 {
381 // Data accepted.
382 usbInEpState = USB_IDLE;
383 }
384 break;
385 }
386
387 out:
388 s2s_spin_unlock(&usbDevLock);
389 }
390
391 void s2s_debugTimer()
392 {
393 if (!USBD_Composite_IsConfigured(&configUsbDev))
394 {
395 usbInEpState = USB_IDLE;
396 return;
397 }
398
399 if (USBD_HID_IsReportReady(&configUsbDev))
400 {
401 // Check if we have a previous command outstanding
402 // before accepting another
403 size_t cmdSize;
404 if (hidPacket_peekPacket(&cmdSize) == NULL)
405 {
406 uint8_t hidBuffer[USBHID_LEN];
407 int byteCount = USBD_HID_GetReport(&configUsbDev, hidBuffer, sizeof(hidBuffer));
408 hidPacket_recv(hidBuffer, byteCount);
409 }
410 }
411
412 if (hidPacket_getHIDBytesReady() == 0) // Nothing queued to send
413 {
414 size_t cmdSize;
415 const uint8_t* cmd = hidPacket_peekPacket(&cmdSize);
416 // This is called from an ISR, only process simple commands.
417 if (cmd && (cmdSize > 0))
418 {
419 if (cmd[0] == S2S_CMD_DEBUG)
420 {
421 hidPacket_getPacket(&cmdSize);
422 debugCommand();
423 }
424 else if (cmd[0] == S2S_CMD_PING)
425 {
426 hidPacket_getPacket(&cmdSize);
427 pingCommand();
428 }
429 }
430 }
431
432 switch (usbInEpState)
433 {
434 case USB_IDLE:
435 {
436 uint8_t hidBuffer[USBHID_LEN];
437 const uint8_t* nextChunk = hidPacket_getHIDBytes(hidBuffer);
438
439 if (nextChunk)
440 {
441 USBD_HID_SendReport (&configUsbDev, nextChunk, sizeof(hidBuffer));
442 usbInEpState = USB_DATA_SENT;
443 }
444 }
445 break;
446
447 case USB_DATA_SENT:
448 if (!USBD_HID_IsBusy(&configUsbDev))
449 {
450 // Data accepted.
451 usbInEpState = USB_IDLE;
452 }
453 break;
454 }
455 }
456
457
458
459 // Public method for storing MODE SELECT results.
460 void s2s_configSave(int scsiId, uint16_t bytesPerSector)
461 {
462 S2S_TargetCfg* cfg = (S2S_TargetCfg*) s2s_getConfigById(scsiId);
463 cfg->bytesPerSector = bytesPerSector;
464
465 BSP_SD_WriteBlocks_DMA(
466 &s2s_cfg[0],
467 sdDev.capacity - S2S_CFG_SIZE,
468 (S2S_CFG_SIZE + 511) / 512);
469 }
470
471
472 const S2S_TargetCfg* s2s_getConfigByIndex(int i)
473 {
474 return (const S2S_TargetCfg*)
475 (s2s_cfg + sizeof(S2S_BoardCfg) + (i * sizeof(S2S_TargetCfg)));
476 }
477
478 const S2S_TargetCfg* s2s_getConfigById(int scsiId)
479 {
480 int i;
481 for (i = 0; i < S2S_MAX_TARGETS; ++i)
482 {
483 const S2S_TargetCfg* tgt = s2s_getConfigByIndex(i);
484 if ((tgt->scsiId & S2S_CFG_TARGET_ID_BITS) == scsiId)
485 {
486 return tgt;
487 }
488 }
489 return NULL;
490
491 }
492