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