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