Based on PS3_BlueUSB with reference to http://blog.goo.ne.jp/roboz80/e/10e7bf38d3a63b996ca2894e9fb5e3b6

Dependencies:   TextLCD mbed

Committer:
kenbumono
Date:
Tue Jul 05 08:25:40 2011 +0000
Revision:
0:44619612f575

        

Who changed what in which revision?

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