this is the Peter Barrett USBHostShell program, a bit modified for being a bit more talkactive, just to let u know how the USB protocol layer is going on, and all the data trasnfers. Also there is a small implementation of HID descriptors, but not functional... yet :S the aim is to at least implement the gamepad HID, and make an array of function pointer to each HID function

Dependencies:   mbed

Committer:
Sergio
Date:
Mon Sep 13 12:40:05 2010 +0000
Revision:
0:e1e03118b8fe

        

Who changed what in which revision?

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