Almost identical to USBHostShell; reads all endpoint descriptors

Dependents:   myBlueUSB mbed_TANK_Kinect myBlueUSB_ros myBlueUSB_localfix

Committer:
networker
Date:
Mon Apr 04 16:42:56 2011 +0000
Revision:
0:d6bff1c2dab1
Child:
3:58c785c2b381
initial revision

Who changed what in which revision?

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