makoto abe
/
ADK_BlueUSBwithlog
MassStorage.cpp@1:237cfff63ef8, 2011-05-26 (annotated)
- Committer:
- abe00makoto
- Date:
- Thu May 26 19:39:37 2011 +0000
- Revision:
- 1:237cfff63ef8
- Parent:
- 0:e939856c1939
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abe00makoto | 0:e939856c1939 | 1 | |
abe00makoto | 0:e939856c1939 | 2 | /* |
abe00makoto | 0:e939856c1939 | 3 | Copyright (c) 2010 Peter Barrett |
abe00makoto | 0:e939856c1939 | 4 | |
abe00makoto | 0:e939856c1939 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
abe00makoto | 0:e939856c1939 | 6 | of this software and associated documentation files (the "Software"), to deal |
abe00makoto | 0:e939856c1939 | 7 | in the Software without restriction, including without limitation the rights |
abe00makoto | 0:e939856c1939 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
abe00makoto | 0:e939856c1939 | 9 | copies of the Software, and to permit persons to whom the Software is |
abe00makoto | 0:e939856c1939 | 10 | furnished to do so, subject to the following conditions: |
abe00makoto | 0:e939856c1939 | 11 | |
abe00makoto | 0:e939856c1939 | 12 | The above copyright notice and this permission notice shall be included in |
abe00makoto | 0:e939856c1939 | 13 | all copies or substantial portions of the Software. |
abe00makoto | 0:e939856c1939 | 14 | |
abe00makoto | 0:e939856c1939 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
abe00makoto | 0:e939856c1939 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
abe00makoto | 0:e939856c1939 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
abe00makoto | 0:e939856c1939 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
abe00makoto | 0:e939856c1939 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
abe00makoto | 0:e939856c1939 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
abe00makoto | 0:e939856c1939 | 21 | THE SOFTWARE. |
abe00makoto | 0:e939856c1939 | 22 | */ |
abe00makoto | 0:e939856c1939 | 23 | |
abe00makoto | 0:e939856c1939 | 24 | #include "stdlib.h" |
abe00makoto | 0:e939856c1939 | 25 | #include "stdio.h" |
abe00makoto | 0:e939856c1939 | 26 | #include "string.h" |
abe00makoto | 0:e939856c1939 | 27 | |
abe00makoto | 0:e939856c1939 | 28 | #include "Utils.h" |
abe00makoto | 0:e939856c1939 | 29 | #include "USBHost.h" |
abe00makoto | 0:e939856c1939 | 30 | |
abe00makoto | 0:e939856c1939 | 31 | |
abe00makoto | 0:e939856c1939 | 32 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize); |
abe00makoto | 0:e939856c1939 | 33 | int MassStorage_ReadBlock(int device, u32 block, u8* dst); |
abe00makoto | 0:e939856c1939 | 34 | int MassStorage_WriteBlock(int device, u32 block, const u8* dst); |
abe00makoto | 0:e939856c1939 | 35 | |
abe00makoto | 0:e939856c1939 | 36 | |
abe00makoto | 0:e939856c1939 | 37 | #define ERR_BAD_CSW_SIGNATURE -200 |
abe00makoto | 0:e939856c1939 | 38 | |
abe00makoto | 0:e939856c1939 | 39 | #define CBW_SIGNATURE 0x43425355 |
abe00makoto | 0:e939856c1939 | 40 | #define CSW_SIGNATURE 0x53425355 |
abe00makoto | 0:e939856c1939 | 41 | |
abe00makoto | 0:e939856c1939 | 42 | // Command Block |
abe00makoto | 0:e939856c1939 | 43 | typedef struct |
abe00makoto | 0:e939856c1939 | 44 | { |
abe00makoto | 0:e939856c1939 | 45 | u32 Signature; |
abe00makoto | 0:e939856c1939 | 46 | u32 Tag; |
abe00makoto | 0:e939856c1939 | 47 | u32 TransferLength; |
abe00makoto | 0:e939856c1939 | 48 | u8 Flags; |
abe00makoto | 0:e939856c1939 | 49 | u8 LUN; |
abe00makoto | 0:e939856c1939 | 50 | u8 CBLength; |
abe00makoto | 0:e939856c1939 | 51 | u8 CB[16]; // only 6 really |
abe00makoto | 0:e939856c1939 | 52 | } CBW; |
abe00makoto | 0:e939856c1939 | 53 | |
abe00makoto | 0:e939856c1939 | 54 | // Status block |
abe00makoto | 0:e939856c1939 | 55 | typedef struct |
abe00makoto | 0:e939856c1939 | 56 | { |
abe00makoto | 0:e939856c1939 | 57 | u32 Signature; |
abe00makoto | 0:e939856c1939 | 58 | u32 Tag; |
abe00makoto | 0:e939856c1939 | 59 | u32 DataResidue; |
abe00makoto | 0:e939856c1939 | 60 | u8 Status; |
abe00makoto | 0:e939856c1939 | 61 | } CSW; |
abe00makoto | 0:e939856c1939 | 62 | |
abe00makoto | 0:e939856c1939 | 63 | int SCSIRequestSense(int device); |
abe00makoto | 0:e939856c1939 | 64 | |
abe00makoto | 0:e939856c1939 | 65 | int DoSCSI(int device, const u8* cmd, int cmdLen, int flags, u8* data, u32 transferLen) |
abe00makoto | 0:e939856c1939 | 66 | { |
abe00makoto | 0:e939856c1939 | 67 | CBW cbw; |
abe00makoto | 0:e939856c1939 | 68 | cbw.Signature = CBW_SIGNATURE; |
abe00makoto | 0:e939856c1939 | 69 | cbw.Tag = 0; |
abe00makoto | 0:e939856c1939 | 70 | cbw.TransferLength = transferLen; |
abe00makoto | 0:e939856c1939 | 71 | cbw.Flags = flags; |
abe00makoto | 0:e939856c1939 | 72 | cbw.LUN = 0; |
abe00makoto | 0:e939856c1939 | 73 | cbw.CBLength = cmdLen; |
abe00makoto | 0:e939856c1939 | 74 | memset(cbw.CB,0,sizeof(cbw.CB)); |
abe00makoto | 0:e939856c1939 | 75 | memcpy(cbw.CB,cmd,cmdLen); |
abe00makoto | 0:e939856c1939 | 76 | |
abe00makoto | 0:e939856c1939 | 77 | int r; |
abe00makoto | 0:e939856c1939 | 78 | r = USBBulkTransfer(device,0x01,(u8*)&cbw,31); // Send the command |
abe00makoto | 0:e939856c1939 | 79 | if (r < 0) |
abe00makoto | 0:e939856c1939 | 80 | return r; |
abe00makoto | 0:e939856c1939 | 81 | |
abe00makoto | 0:e939856c1939 | 82 | if (data) |
abe00makoto | 0:e939856c1939 | 83 | { |
abe00makoto | 0:e939856c1939 | 84 | r = USBBulkTransfer(device,flags | 1,data,transferLen); |
abe00makoto | 0:e939856c1939 | 85 | if (r < 0) |
abe00makoto | 0:e939856c1939 | 86 | return r; |
abe00makoto | 0:e939856c1939 | 87 | } |
abe00makoto | 0:e939856c1939 | 88 | |
abe00makoto | 0:e939856c1939 | 89 | CSW csw; |
abe00makoto | 0:e939856c1939 | 90 | csw.Signature = 0; |
abe00makoto | 0:e939856c1939 | 91 | r = USBBulkTransfer(device,0x81,(u8*)&csw,13); |
abe00makoto | 0:e939856c1939 | 92 | if (r < 0) |
abe00makoto | 0:e939856c1939 | 93 | return r; |
abe00makoto | 0:e939856c1939 | 94 | |
abe00makoto | 0:e939856c1939 | 95 | if (csw.Signature != CSW_SIGNATURE) |
abe00makoto | 0:e939856c1939 | 96 | return ERR_BAD_CSW_SIGNATURE; |
abe00makoto | 0:e939856c1939 | 97 | |
abe00makoto | 0:e939856c1939 | 98 | // ModeSense? |
abe00makoto | 0:e939856c1939 | 99 | if (csw.Status == 1 && cmd[0] != 3) |
abe00makoto | 0:e939856c1939 | 100 | return SCSIRequestSense(device); |
abe00makoto | 0:e939856c1939 | 101 | |
abe00makoto | 0:e939856c1939 | 102 | return csw.Status; |
abe00makoto | 0:e939856c1939 | 103 | } |
abe00makoto | 0:e939856c1939 | 104 | |
abe00makoto | 0:e939856c1939 | 105 | int SCSITestUnitReady(int device) |
abe00makoto | 0:e939856c1939 | 106 | { |
abe00makoto | 0:e939856c1939 | 107 | u8 cmd[6]; |
abe00makoto | 0:e939856c1939 | 108 | memset(cmd,0,6); |
abe00makoto | 0:e939856c1939 | 109 | return DoSCSI(device,cmd,6,DEVICE_TO_HOST,0,0); |
abe00makoto | 0:e939856c1939 | 110 | } |
abe00makoto | 0:e939856c1939 | 111 | |
abe00makoto | 0:e939856c1939 | 112 | int SCSIRequestSense(int device) |
abe00makoto | 0:e939856c1939 | 113 | { |
abe00makoto | 0:e939856c1939 | 114 | u8 cmd[6] = {0x03,0,0,0,18,0}; |
abe00makoto | 0:e939856c1939 | 115 | u8 result[18]; |
abe00makoto | 0:e939856c1939 | 116 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,18); |
abe00makoto | 0:e939856c1939 | 117 | return r; |
abe00makoto | 0:e939856c1939 | 118 | } |
abe00makoto | 0:e939856c1939 | 119 | |
abe00makoto | 0:e939856c1939 | 120 | int SCSIInquiry(int device) |
abe00makoto | 0:e939856c1939 | 121 | { |
abe00makoto | 0:e939856c1939 | 122 | u8 cmd[6] = {0x12,0,0,0,36,0}; |
abe00makoto | 0:e939856c1939 | 123 | u8 result[36+2]; |
abe00makoto | 0:e939856c1939 | 124 | result[36] = '\n'; |
abe00makoto | 0:e939856c1939 | 125 | result[37] = 0; |
abe00makoto | 0:e939856c1939 | 126 | int r = DoSCSI(device,cmd,6,DEVICE_TO_HOST,result,36); |
abe00makoto | 0:e939856c1939 | 127 | if (r == 0) |
abe00makoto | 0:e939856c1939 | 128 | printf((const char*)result + 8); |
abe00makoto | 0:e939856c1939 | 129 | return r; |
abe00makoto | 0:e939856c1939 | 130 | } |
abe00makoto | 0:e939856c1939 | 131 | |
abe00makoto | 0:e939856c1939 | 132 | int SCSIReadCapacity(int device, u32* blockCount, u32* blockSize) |
abe00makoto | 0:e939856c1939 | 133 | { |
abe00makoto | 0:e939856c1939 | 134 | u8 cmd[10] = {0x25,0,0,0,8,0,0,0,0,0}; |
abe00makoto | 0:e939856c1939 | 135 | u8 result[8]; |
abe00makoto | 0:e939856c1939 | 136 | *blockSize = 0; |
abe00makoto | 0:e939856c1939 | 137 | *blockCount = 0; |
abe00makoto | 0:e939856c1939 | 138 | int r = DoSCSI(device,cmd,10,DEVICE_TO_HOST,result,8); |
abe00makoto | 0:e939856c1939 | 139 | if (r == 0) |
abe00makoto | 0:e939856c1939 | 140 | { |
abe00makoto | 0:e939856c1939 | 141 | *blockCount = BE32(result); |
abe00makoto | 0:e939856c1939 | 142 | *blockSize = BE32(result+4); |
abe00makoto | 0:e939856c1939 | 143 | } |
abe00makoto | 0:e939856c1939 | 144 | return r; |
abe00makoto | 0:e939856c1939 | 145 | } |
abe00makoto | 0:e939856c1939 | 146 | |
abe00makoto | 0:e939856c1939 | 147 | int SCSITransfer(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize, int direction) |
abe00makoto | 0:e939856c1939 | 148 | { |
abe00makoto | 0:e939856c1939 | 149 | // USB hardware will only do 4k per transfer |
abe00makoto | 0:e939856c1939 | 150 | while (blockCount*blockSize > 4096) |
abe00makoto | 0:e939856c1939 | 151 | { |
abe00makoto | 0:e939856c1939 | 152 | int count = 4096/blockSize; |
abe00makoto | 0:e939856c1939 | 153 | int r = SCSITransfer(device,blockAddr,count,dst,blockSize,direction); |
abe00makoto | 0:e939856c1939 | 154 | dst += count*blockSize; |
abe00makoto | 0:e939856c1939 | 155 | blockAddr += count; |
abe00makoto | 0:e939856c1939 | 156 | blockCount -= count; |
abe00makoto | 0:e939856c1939 | 157 | } |
abe00makoto | 0:e939856c1939 | 158 | |
abe00makoto | 0:e939856c1939 | 159 | u8 cmd[10]; |
abe00makoto | 0:e939856c1939 | 160 | memset(cmd,0,10); |
abe00makoto | 0:e939856c1939 | 161 | cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A; |
abe00makoto | 0:e939856c1939 | 162 | BE32(blockAddr,cmd+2); |
abe00makoto | 0:e939856c1939 | 163 | BE16(blockCount,cmd+7); |
abe00makoto | 0:e939856c1939 | 164 | return DoSCSI(device,cmd,10,direction,dst,blockSize*blockCount); |
abe00makoto | 0:e939856c1939 | 165 | } |
abe00makoto | 0:e939856c1939 | 166 | |
abe00makoto | 0:e939856c1939 | 167 | int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize) |
abe00makoto | 0:e939856c1939 | 168 | { |
abe00makoto | 0:e939856c1939 | 169 | return SCSIReadCapacity(device,blockCount,blockSize); |
abe00makoto | 0:e939856c1939 | 170 | } |
abe00makoto | 0:e939856c1939 | 171 | |
abe00makoto | 0:e939856c1939 | 172 | int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
abe00makoto | 0:e939856c1939 | 173 | { |
abe00makoto | 0:e939856c1939 | 174 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,DEVICE_TO_HOST); |
abe00makoto | 0:e939856c1939 | 175 | } |
abe00makoto | 0:e939856c1939 | 176 | |
abe00makoto | 0:e939856c1939 | 177 | int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize = 512) |
abe00makoto | 0:e939856c1939 | 178 | { |
abe00makoto | 0:e939856c1939 | 179 | return SCSITransfer(device,blockAddr,blockCount,dst,blockSize,HOST_TO_DEVICE); |
abe00makoto | 0:e939856c1939 | 180 | } |