3 * Amiga ZZ9000 USB Storage Driver (ZZ9000USBStorage.device)
4 * Copyright (C) 2016-2020, Lukas F. Hartmann <lukas@mntre.com>
5 * Based on code Copyright (C) 2016, Jason S. McMullan <jason.mcmullan@gmail.com>
8 * Licensed under the MIT License:
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * a copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
29 #include <exec/resident.h>
30 #include <exec/errors.h>
31 #include <exec/memory.h>
32 #include <exec/lists.h>
33 #include <exec/alerts.h>
34 #include <exec/tasks.h>
36 #include <exec/execbase.h>
38 #include <libraries/expansion.h>
40 #include <devices/trackdisk.h>
41 #include <devices/timer.h>
42 #include <devices/scsidisk.h>
44 #include <dos/filehandler.h>
46 #include <proto/exec.h>
47 #include <proto/disk.h>
48 #include <proto/expansion.h>
50 #include <clib/debug_protos.h>
53 #include "../piscsi-enums.h"
55 #define WRITESHORT(cmd, val) *(unsigned short *)((unsigned long)(PISCSI_OFFSET+cmd)) = val;
56 #define WRITELONG(cmd, val) *(unsigned long *)((unsigned long)(PISCSI_OFFSET+cmd)) = val;
57 #define WRITEBYTE(cmd, val) *(unsigned char *)((unsigned long)(PISCSI_OFFSET+cmd)) = val;
59 #define READSHORT(cmd, var) var = *(volatile unsigned short *)(PISCSI_OFFSET + cmd);
60 #define READLONG(cmd, var) var = *(volatile unsigned long *)(PISCSI_OFFSET + cmd);
64 struct Device* pi_dev;
82 struct ExecBase* SysBase = NULL;
85 const char DevName[] = "scsi.device";
87 const char DevName[] = "2nd.scsi.device";
89 const char DevName[] = "pi-scsi.device";
91 const char DevIdString[] = "Pi-SCSI 0.1";
93 const UWORD DevVersion = 43;
94 const UWORD DevRevision = 10;
98 struct piscsi_base *dev_base = NULL;
100 struct WBStartup *_WBenchMsg = NULL;
106 //#define bug(x,args...) KPrintF(x ,##args);
107 //#define debug(x,args...) bug("%s:%ld " x "\n", __func__, (unsigned long)__LINE__ ,##args)
109 uint8_t piscsi_perform_io(struct piscsi_unit *u, struct IORequest *io);
110 uint8_t piscsi_rw(struct piscsi_unit *u, struct IORequest *io, uint32_t offset, uint8_t write);
111 uint8_t piscsi_scsi(struct piscsi_unit *u, struct IORequest *io);
113 extern void* DOSBase[2];
115 uint32_t __UserDevInit(struct Device* dev) {
116 //uint8_t* registers = NULL;
117 SysBase = *(struct ExecBase **)4L;
119 KPrintF("Initializing devices.\n");
121 dev_base = AllocMem(sizeof(struct piscsi_base), MEMF_PUBLIC | MEMF_CLEAR);
122 dev_base->pi_dev = dev;
124 for (int i = 0; i < NUM_UNITS; i++) {
126 WRITESHORT(PISCSI_CMD_DRVNUM, i);
127 dev_base->units[i].regs_ptr = PISCSI_OFFSET;
128 READSHORT(PISCSI_CMD_DRVTYPE, r);
129 dev_base->units[i].enabled = r;
130 dev_base->units[i].present = r;
131 dev_base->units[i].valid = r;
132 dev_base->units[i].unit_num = i;
133 if (dev_base->units[i].present) {
134 READLONG(PISCSI_CMD_CYLS, dev_base->units[i].c);
135 READSHORT(PISCSI_CMD_HEADS, dev_base->units[i].h);
136 READSHORT(PISCSI_CMD_SECS, dev_base->units[i].s);
137 KPrintF("C/H/S: %ld / %ld / %ld\n", dev_base->units[i].c, dev_base->units[i].h, dev_base->units[i].s);
139 dev_base->units[i].change_num++;
140 // Send any reset signal to the "SCSI" device here.
146 uint32_t __UserDevCleanup(void) {
147 KPrintF("Cleaning up.\n");
148 FreeMem(dev_base, sizeof(struct piscsi_base));
152 uint32_t __UserDevOpen(struct IOExtTD *iotd, uint32_t num, uint32_t flags) {
153 struct Node* node = (struct Node*)iotd;
154 int io_err = IOERR_OPENFAIL;
156 //WRITESHORT(PISCSI_CMD_DEBUGME, 1);
159 WRITELONG(PISCSI_CMD_DRVNUM, num);
160 READLONG(PISCSI_CMD_DRVNUM, unit_num);
162 KPrintF("Opening device %ld Flags: %ld (%lx)\n", unit_num, flags, flags);
164 if (iotd && unit_num < NUM_UNITS) {
165 if (dev_base->units[unit_num].enabled && dev_base->units[unit_num].present) {
167 iotd->iotd_Req.io_Unit = (struct Unit*)&dev_base->units[unit_num].unit;
168 iotd->iotd_Req.io_Unit->unit_flags = UNITF_ACTIVE;
169 iotd->iotd_Req.io_Unit->unit_OpenCnt = 1;
174 iotd->iotd_Req.io_Error = io_err;
179 uint32_t __UserDevClose(struct IOExtTD *iotd) {
183 void exit(int status) { }
185 ADDTABL_1(__BeginIO,a1);
186 void __BeginIO(struct IORequest *io) {
187 if (dev_base == NULL || io == NULL)
190 struct piscsi_unit *u;
191 struct Node* node = (struct Node*)io;
192 u = (struct piscsi_unit *)io->io_Unit;
194 if (node == NULL || u == NULL)
197 KPrintF("io_Command = %ld, io_Flags = 0x%lx quick = %lx\n", io->io_Command, io->io_Flags, (io->io_Flags & IOF_QUICK));
198 io->io_Error = piscsi_perform_io(u, io);
200 if (!(io->io_Flags & IOF_QUICK)) {
201 ReplyMsg(&io->io_Message);
205 ADDTABL_1(__AbortIO,a1);
206 void __AbortIO(struct IORequest* io) {
207 KPrintF("AbortIO!\n");
209 io->io_Error = IOERR_ABORTED;
212 uint8_t piscsi_rw(struct piscsi_unit *u, struct IORequest *io, uint32_t offset, uint8_t write) {
213 struct IOStdReq *iostd = (struct IOStdReq *)io;
214 struct IOExtTD *iotd = (struct IOExtTD *)io;
217 uint32_t len, num_blocks;
218 uint32_t block, max_addr;
221 data = iotd->iotd_Req.io_Data;
222 len = iotd->iotd_Req.io_Length;
223 //uint32_t offset2 = iostd->io_Offset;
225 max_addr = 0xffffffff;
227 // well... if we had 64 bits this would make sense
228 if ((offset > max_addr) || (offset+len > max_addr))
229 return IOERR_BADADDRESS;
231 return IOERR_BADADDRESS;
232 if (len < PISCSI_BLOCK_SIZE) {
233 iostd->io_Actual = 0;
234 return IOERR_BADLENGTH;
237 //block = offset;// >> SD_SECTOR_SHIFT;
238 //num_blocks = len;// >> SD_SECTOR_SHIFT;
242 //uint32_t retries = 10;
243 //KPrintF("Write %lx -> %lx %lx\n", (uint32_t)data, offset, len);
244 WRITELONG(PISCSI_CMD_ADDR1, (offset >> 9));
245 WRITELONG(PISCSI_CMD_ADDR2, len);
246 WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
247 WRITESHORT(PISCSI_CMD_WRITE, 1);
249 //KPrintF("read %lx %lx -> %lx\n", offset, len, (uint32_t)data);
250 WRITELONG(PISCSI_CMD_ADDR1, (offset >> 9));
251 WRITELONG(PISCSI_CMD_ADDR2, len);
252 WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
253 WRITESHORT(PISCSI_CMD_READ, 1);
257 iostd->io_Actual = 0;
259 if (sderr & SCSIERR_TIMEOUT)
260 return TDERR_DiskChanged;
261 if (sderr & SCSIERR_PARAM)
262 return TDERR_SeekError;
263 if (sderr & SCSIERR_ADDRESS)
264 return TDERR_SeekError;
265 if (sderr & (SCSIERR_ERASESEQ | SCSIERR_ERASERES))
266 return TDERR_BadSecPreamble;
267 if (sderr & SCSIERR_CRC)
268 return TDERR_BadSecSum;
269 if (sderr & SCSIERR_ILLEGAL)
270 return TDERR_TooFewSecs;
271 if (sderr & SCSIERR_IDLE)
272 return TDERR_PostReset;
274 return TDERR_SeekError;
276 iostd->io_Actual = len;
282 #define PISCSI_ID_STRING "PISTORM Fake SCSI Disk 0.1 1111111111111111"
284 uint8_t piscsi_scsi(struct piscsi_unit *u, struct IORequest *io)
286 struct IOStdReq *iostd = (struct IOStdReq *)io;
287 struct SCSICmd *scsi = iostd->io_Data;
288 //uint8_t* registers = sdu->sdu_Registers;
289 uint8_t *data = (uint8_t *)scsi->scsi_Data;
290 uint32_t i, block, blocks, maxblocks;
293 KPrintF("SCSI len=%ld, cmd = %02lx %02lx %02lx ... (%ld)\n",
294 iostd->io_Length, scsi->scsi_Command[0],
295 scsi->scsi_Command[1], scsi->scsi_Command[2],
296 scsi->scsi_CmdLength);
298 //maxblocks = u->s * u->c * u->h;
300 if (scsi->scsi_CmdLength < 6) {
301 //KPrintF("SCSICMD BADLENGTH2");
302 return IOERR_BADLENGTH;
305 if (scsi->scsi_Command == NULL) {
306 //KPrintF("SCSICMD IOERR_BADADDRESS1");
307 return IOERR_BADADDRESS;
310 scsi->scsi_Actual = 0;
311 //iostd->io_Actual = sizeof(*scsi);
313 switch (scsi->scsi_Command[0]) {
314 case 0x00: // TEST_UNIT_READY
315 KPrintF("SCSI command: Test Unit Ready.\n");
319 case 0x12: // INQUIRY
320 KPrintF("SCSI command: Inquiry.\n");
321 for (i = 0; i < scsi->scsi_Length; i++) {
325 case 0: // SCSI device type: direct-access device
331 case 2: // VERSION = 0
334 case 3: // NORMACA=0, HISUP = 0, RESPONSE_DATA_FORMAT = 2
335 val = (0 << 5) | (0 << 4) | 2;
337 case 4: // ADDITIONAL_LENGTH = 44 - 4
341 if (i >= 8 && i < 44)
342 val = PISCSI_ID_STRING[i - 8];
349 scsi->scsi_Actual = i;
353 case 0x08: // READ (6)
354 case 0x0a: // WRITE (6)
355 block = scsi->scsi_Command[1] & 0x1f;
356 block = (block << 8) | scsi->scsi_Command[2];
357 block = (block << 8) | scsi->scsi_Command[3];
358 blocks = scsi->scsi_Command[4];
360 READLONG(PISCSI_CMD_BLOCKS, maxblocks);
361 if (block + blocks > maxblocks) {
362 err = IOERR_BADADDRESS;
365 /*if (scsi->scsi_Length < (blocks << SD_SECTOR_SHIFT)) {
366 err = IOERR_BADLENGTH;
370 err = IOERR_BADADDRESS;
374 if (scsi->scsi_Command[0] == 0x08) {
375 //KPrintF("scsi_read %lx %lx\n",block,blocks);
376 KPrintF("SCSI read %lx %lx -> %lx\n", block, blocks, (uint32_t)data);
377 WRITELONG(PISCSI_CMD_ADDR2, block);
378 WRITELONG(PISCSI_CMD_ADDR2, (blocks << 9));
379 WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
380 WRITESHORT(PISCSI_CMD_READ, 1);
383 //KPrintF("scsi_write %lx %lx\n",block,blocks);
384 KPrintF("SCSI write %lx -> %lx %lx\n", (uint32_t)data, block, blocks);
385 WRITELONG(PISCSI_CMD_ADDR2, block);
386 WRITELONG(PISCSI_CMD_ADDR2, (blocks << 9));
387 WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
388 WRITESHORT(PISCSI_CMD_WRITE, 1);
391 scsi->scsi_Actual = scsi->scsi_Length;
395 case 0x25: // READ CAPACITY (10)
396 KPrintF("SCSI command: Read Capacity.\n");
397 if (scsi->scsi_CmdLength < 10) {
398 err = HFERR_BadStatus;
402 block = *((uint32_t*)&scsi->scsi_Command[2]);
404 /*if ((scsi->scsi_Command[8] & 1) || block != 0) {
406 KPrintF("PMI not supported.\n");
407 err = HFERR_BadStatus;
411 if (scsi->scsi_Length < 8) {
412 err = IOERR_BADLENGTH;
416 READLONG(PISCSI_CMD_BLOCKS, blocks);
417 ((uint32_t*)data)[0] = blocks - 1;
418 ((uint32_t*)data)[1] = PISCSI_BLOCK_SIZE;
420 scsi->scsi_Actual = 8;
424 case 0x1a: // MODE SENSE (6)
425 KPrintF("SCSI command: Mode Sense.\n");
426 data[0] = 3 + 8 + 0x16;
427 data[1] = 0; // MEDIUM TYPE
431 READLONG(PISCSI_CMD_BLOCKS, maxblocks);
432 (blocks = (maxblocks - 1) & 0xFFFFFF);
434 *((uint32_t *)&data[4]) = blocks;
435 *((uint32_t *)&data[8]) = PISCSI_BLOCK_SIZE;
437 switch (((UWORD)scsi->scsi_Command[2] << 8) | scsi->scsi_Command[3]) {
438 case 0x0300: { // Format Device Mode
439 KPrintF("Grabbing SCSI format device mode data.\n");
440 uint8_t *datext = data + 12;
445 *((uint32_t *)&datext[4]) = 0;
446 *((uint32_t *)&datext[8]) = 0;
447 *((uint16_t *)&datext[10]) = u->s;
448 *((uint16_t *)&datext[12]) = PISCSI_BLOCK_SIZE;
451 *((uint32_t *)&datext[16]) = 0;
454 scsi->scsi_Actual = data[0] + 1;
458 case 0x0400: // Rigid Drive Geometry
459 KPrintF("Grabbing SCSI rigid drive geometry.\n");
460 uint8_t *datext = data + 12;
462 *((uint32_t *)&datext[1]) = u->c;
466 *((uint32_t *)&datext[6]) = 0;
467 *((uint32_t *)&datext[10]) = 0;
468 *((uint32_t *)&datext[13]) = u->c;
470 *((uint32_t *)&datext[18]) = 0;
471 *((uint16_t *)&datext[20]) = 5400;
473 scsi->scsi_Actual = data[0] + 1;
478 KPrintF("[WARN] Unhandled mode sense thing: %lx\n", ((UWORD)scsi->scsi_Command[2] << 8) | scsi->scsi_Command[3]);
479 err = HFERR_BadStatus;
484 case 0x37: // READ DEFECT DATA (10)
489 KPrintF("Unknown/unhandled SCSI command %lx.\n", scsi->scsi_Command[0]);
490 err = HFERR_BadStatus;
495 KPrintF("Some SCSI error occured: %ld\n", err);
496 scsi->scsi_Actual = 0;
502 #define DUMMYCMD iostd->io_Actual = 0; break;
503 uint8_t piscsi_perform_io(struct piscsi_unit *u, struct IORequest *io) {
504 struct IOStdReq *iostd = (struct IOStdReq *)io;
505 struct IOExtTD *iotd = (struct IOExtTD *)io;
510 //struct DriveGeometry *geom;
514 return IOERR_OPENFAIL;
517 data = iotd->iotd_Req.io_Data;
518 len = iotd->iotd_Req.io_Length;
520 if (io->io_Error == IOERR_ABORTED) {
524 //KPrintF("cmd: %s\n",cmd_name(io->io_Command));
525 //KPrintF("IO %lx Start, io_Flags = %ld, io_Command = %ld\n", io, io->io_Flags, io->io_Command);
527 switch (io->io_Command) {
529 /* Invalidate read buffer */
532 /* Flush write buffer */
537 iostd->io_Actual = u->change_num;
543 case TD_GETDRIVETYPE:
544 iostd->io_Actual = DG_DIRECT_ACCESS;
547 iostd->io_Actual = u->motor;
548 u->motor = iostd->io_Length ? 1 : 0;
552 offset = iotd->iotd_Req.io_Offset;
554 err = piscsi_rw(u, io, offset, 1);
557 offset = iotd->iotd_Req.io_Offset;
559 err = piscsi_rw(u, io, offset, 1);
562 offset = iotd->iotd_Req.io_Offset;
564 err = piscsi_rw(u, io, offset, 0);
568 err = piscsi_scsi(u, io);
571 int cmd = io->io_Command;
572 KPrintF("Unknown IO command: %ld\n", cmd);