![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
USB Host Lite downloaded from NXP web site. Untested! Compiled, dropped on MBED, booted up, and get the console message that it is initializing the stack. I however do not have the ability to connect a USB Memory device to test it.
MassStorage/usbhost_ms.c@0:0826fcc5d020, 2010-01-13 (annotated)
- Committer:
- mshoemaker
- Date:
- Wed Jan 13 01:29:30 2010 +0000
- Revision:
- 0:0826fcc5d020
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mshoemaker | 0:0826fcc5d020 | 1 | /* |
mshoemaker | 0:0826fcc5d020 | 2 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 3 | * NXP USB Host Stack |
mshoemaker | 0:0826fcc5d020 | 4 | * |
mshoemaker | 0:0826fcc5d020 | 5 | * (c) Copyright 2008, NXP SemiConductors |
mshoemaker | 0:0826fcc5d020 | 6 | * (c) Copyright 2008, OnChip Technologies LLC |
mshoemaker | 0:0826fcc5d020 | 7 | * All Rights Reserved |
mshoemaker | 0:0826fcc5d020 | 8 | * |
mshoemaker | 0:0826fcc5d020 | 9 | * www.nxp.com |
mshoemaker | 0:0826fcc5d020 | 10 | * www.onchiptech.com |
mshoemaker | 0:0826fcc5d020 | 11 | * |
mshoemaker | 0:0826fcc5d020 | 12 | * File : usbhost_ms.c |
mshoemaker | 0:0826fcc5d020 | 13 | * Programmer(s) : Ravikanth.P |
mshoemaker | 0:0826fcc5d020 | 14 | * Version : |
mshoemaker | 0:0826fcc5d020 | 15 | * |
mshoemaker | 0:0826fcc5d020 | 16 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 17 | */ |
mshoemaker | 0:0826fcc5d020 | 18 | |
mshoemaker | 0:0826fcc5d020 | 19 | /* |
mshoemaker | 0:0826fcc5d020 | 20 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 21 | * INCLUDE HEADER FILES |
mshoemaker | 0:0826fcc5d020 | 22 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 23 | */ |
mshoemaker | 0:0826fcc5d020 | 24 | |
mshoemaker | 0:0826fcc5d020 | 25 | #include "usbhost_ms.h" |
mshoemaker | 0:0826fcc5d020 | 26 | |
mshoemaker | 0:0826fcc5d020 | 27 | /* |
mshoemaker | 0:0826fcc5d020 | 28 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 29 | * GLOBAL VARIABLES |
mshoemaker | 0:0826fcc5d020 | 30 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 31 | */ |
mshoemaker | 0:0826fcc5d020 | 32 | |
mshoemaker | 0:0826fcc5d020 | 33 | USB_INT32U MS_BlkSize; |
mshoemaker | 0:0826fcc5d020 | 34 | |
mshoemaker | 0:0826fcc5d020 | 35 | /* |
mshoemaker | 0:0826fcc5d020 | 36 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 37 | * INITIALIZE MASS STORAGE INTERFACE |
mshoemaker | 0:0826fcc5d020 | 38 | * |
mshoemaker | 0:0826fcc5d020 | 39 | * Description: This function initializes the mass storage interface |
mshoemaker | 0:0826fcc5d020 | 40 | * |
mshoemaker | 0:0826fcc5d020 | 41 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 42 | * |
mshoemaker | 0:0826fcc5d020 | 43 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 44 | * ERR_INVALID_BOOTSIG if Failed |
mshoemaker | 0:0826fcc5d020 | 45 | * |
mshoemaker | 0:0826fcc5d020 | 46 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 47 | */ |
mshoemaker | 0:0826fcc5d020 | 48 | |
mshoemaker | 0:0826fcc5d020 | 49 | USB_INT32S MS_Init (USB_INT32U *blkSize, USB_INT32U *numBlks, USB_INT08U *inquiryResult) |
mshoemaker | 0:0826fcc5d020 | 50 | { |
mshoemaker | 0:0826fcc5d020 | 51 | USB_INT08U retry; |
mshoemaker | 0:0826fcc5d020 | 52 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 53 | |
mshoemaker | 0:0826fcc5d020 | 54 | MS_GetMaxLUN(); /* Get maximum logical unit number */ |
mshoemaker | 0:0826fcc5d020 | 55 | retry = 80; |
mshoemaker | 0:0826fcc5d020 | 56 | while(retry) { |
mshoemaker | 0:0826fcc5d020 | 57 | rc = MS_TestUnitReady(); /* Test whether the unit is ready */ |
mshoemaker | 0:0826fcc5d020 | 58 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 59 | break; |
mshoemaker | 0:0826fcc5d020 | 60 | } |
mshoemaker | 0:0826fcc5d020 | 61 | MS_GetSenseInfo(); /* Get sense information */ |
mshoemaker | 0:0826fcc5d020 | 62 | retry--; |
mshoemaker | 0:0826fcc5d020 | 63 | } |
mshoemaker | 0:0826fcc5d020 | 64 | if (rc != OK) { |
mshoemaker | 0:0826fcc5d020 | 65 | PRINT_Err(rc); |
mshoemaker | 0:0826fcc5d020 | 66 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 67 | } |
mshoemaker | 0:0826fcc5d020 | 68 | rc = MS_ReadCapacity(numBlks, blkSize); /* Read capacity of the disk */ |
mshoemaker | 0:0826fcc5d020 | 69 | MS_BlkSize = *blkSize; // Set global |
mshoemaker | 0:0826fcc5d020 | 70 | rc = MS_Inquire (inquiryResult); |
mshoemaker | 0:0826fcc5d020 | 71 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 72 | } |
mshoemaker | 0:0826fcc5d020 | 73 | /* |
mshoemaker | 0:0826fcc5d020 | 74 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 75 | * PARSE THE CONFIGURATION |
mshoemaker | 0:0826fcc5d020 | 76 | * |
mshoemaker | 0:0826fcc5d020 | 77 | * Description: This function is used to parse the configuration |
mshoemaker | 0:0826fcc5d020 | 78 | * |
mshoemaker | 0:0826fcc5d020 | 79 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 80 | * |
mshoemaker | 0:0826fcc5d020 | 81 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 82 | * ERR_INVALID_BOOTSIG if Failed |
mshoemaker | 0:0826fcc5d020 | 83 | * |
mshoemaker | 0:0826fcc5d020 | 84 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 85 | */ |
mshoemaker | 0:0826fcc5d020 | 86 | |
mshoemaker | 0:0826fcc5d020 | 87 | USB_INT32S MS_ParseConfiguration (void) |
mshoemaker | 0:0826fcc5d020 | 88 | { |
mshoemaker | 0:0826fcc5d020 | 89 | volatile USB_INT08U *desc_ptr; |
mshoemaker | 0:0826fcc5d020 | 90 | USB_INT08U ms_int_found; |
mshoemaker | 0:0826fcc5d020 | 91 | |
mshoemaker | 0:0826fcc5d020 | 92 | |
mshoemaker | 0:0826fcc5d020 | 93 | desc_ptr = TDBuffer; |
mshoemaker | 0:0826fcc5d020 | 94 | ms_int_found = 0; |
mshoemaker | 0:0826fcc5d020 | 95 | |
mshoemaker | 0:0826fcc5d020 | 96 | if (desc_ptr[1] != USB_DESCRIPTOR_TYPE_CONFIGURATION) { |
mshoemaker | 0:0826fcc5d020 | 97 | return (ERR_BAD_CONFIGURATION); |
mshoemaker | 0:0826fcc5d020 | 98 | } |
mshoemaker | 0:0826fcc5d020 | 99 | desc_ptr += desc_ptr[0]; |
mshoemaker | 0:0826fcc5d020 | 100 | |
mshoemaker | 0:0826fcc5d020 | 101 | while (desc_ptr != TDBuffer + ReadLE16U(&TDBuffer[2])) { |
mshoemaker | 0:0826fcc5d020 | 102 | // while (desc_ptr != TDBuffer + *((USB_INT16U *) &TDBuffer[2])) { |
mshoemaker | 0:0826fcc5d020 | 103 | |
mshoemaker | 0:0826fcc5d020 | 104 | switch (desc_ptr[1]) { |
mshoemaker | 0:0826fcc5d020 | 105 | |
mshoemaker | 0:0826fcc5d020 | 106 | case USB_DESCRIPTOR_TYPE_INTERFACE: /* If it is an interface descriptor */ |
mshoemaker | 0:0826fcc5d020 | 107 | if (desc_ptr[5] == MASS_STORAGE_CLASS && /* check if the class is mass storage */ |
mshoemaker | 0:0826fcc5d020 | 108 | desc_ptr[6] == MASS_STORAGE_SUBCLASS_SCSI && /* check if the subclass is SCSI */ |
mshoemaker | 0:0826fcc5d020 | 109 | desc_ptr[7] == MASS_STORAGE_PROTOCOL_BO) { /* check if the protocol is Bulk only */ |
mshoemaker | 0:0826fcc5d020 | 110 | ms_int_found = 1; |
mshoemaker | 0:0826fcc5d020 | 111 | desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ |
mshoemaker | 0:0826fcc5d020 | 112 | } |
mshoemaker | 0:0826fcc5d020 | 113 | break; |
mshoemaker | 0:0826fcc5d020 | 114 | |
mshoemaker | 0:0826fcc5d020 | 115 | case USB_DESCRIPTOR_TYPE_ENDPOINT: /* If it is an endpoint descriptor */ |
mshoemaker | 0:0826fcc5d020 | 116 | if ((desc_ptr[3] & 0x03) == 0x02) { /* If it is Bulk endpoint */ |
mshoemaker | 0:0826fcc5d020 | 117 | if (desc_ptr[2] & 0x80) { /* If it is In endpoint */ |
mshoemaker | 0:0826fcc5d020 | 118 | EDBulkIn->Control = 1 | /* USB address */ |
mshoemaker | 0:0826fcc5d020 | 119 | ((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */ |
mshoemaker | 0:0826fcc5d020 | 120 | (2 << 11) | /* direction */ |
mshoemaker | 0:0826fcc5d020 | 121 | (ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */ |
mshoemaker | 0:0826fcc5d020 | 122 | desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ |
mshoemaker | 0:0826fcc5d020 | 123 | } else { /* If it is Out endpoint */ |
mshoemaker | 0:0826fcc5d020 | 124 | EDBulkOut->Control = 1 | /* USB address */ |
mshoemaker | 0:0826fcc5d020 | 125 | ((desc_ptr[2] & 0x7F) << 7) | /* Endpoint address */ |
mshoemaker | 0:0826fcc5d020 | 126 | (1 << 11) | /* direction */ |
mshoemaker | 0:0826fcc5d020 | 127 | (ReadLE16U(&desc_ptr[4]) << 16); /* MaxPkt Size */ |
mshoemaker | 0:0826fcc5d020 | 128 | desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ |
mshoemaker | 0:0826fcc5d020 | 129 | } |
mshoemaker | 0:0826fcc5d020 | 130 | } else { /* If it is not bulk end point */ |
mshoemaker | 0:0826fcc5d020 | 131 | desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ |
mshoemaker | 0:0826fcc5d020 | 132 | } |
mshoemaker | 0:0826fcc5d020 | 133 | break; |
mshoemaker | 0:0826fcc5d020 | 134 | |
mshoemaker | 0:0826fcc5d020 | 135 | default: /* If the descriptor is neither interface nor endpoint */ |
mshoemaker | 0:0826fcc5d020 | 136 | desc_ptr += desc_ptr[0]; /* Move to next descriptor start */ |
mshoemaker | 0:0826fcc5d020 | 137 | break; |
mshoemaker | 0:0826fcc5d020 | 138 | } |
mshoemaker | 0:0826fcc5d020 | 139 | } |
mshoemaker | 0:0826fcc5d020 | 140 | if (ms_int_found) { |
mshoemaker | 0:0826fcc5d020 | 141 | PRINT_Log("Mass Storage device connected\n"); |
mshoemaker | 0:0826fcc5d020 | 142 | return (OK); |
mshoemaker | 0:0826fcc5d020 | 143 | } else { |
mshoemaker | 0:0826fcc5d020 | 144 | PRINT_Log("Not a Mass Storage device\n"); |
mshoemaker | 0:0826fcc5d020 | 145 | return (ERR_NO_MS_INTERFACE); |
mshoemaker | 0:0826fcc5d020 | 146 | } |
mshoemaker | 0:0826fcc5d020 | 147 | } |
mshoemaker | 0:0826fcc5d020 | 148 | |
mshoemaker | 0:0826fcc5d020 | 149 | /* |
mshoemaker | 0:0826fcc5d020 | 150 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 151 | * GET MAXIMUM LOGICAL UNIT |
mshoemaker | 0:0826fcc5d020 | 152 | * |
mshoemaker | 0:0826fcc5d020 | 153 | * Description: This function returns the maximum logical unit from the device |
mshoemaker | 0:0826fcc5d020 | 154 | * |
mshoemaker | 0:0826fcc5d020 | 155 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 156 | * |
mshoemaker | 0:0826fcc5d020 | 157 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 158 | * ERR_INVALID_BOOTSIG if Failed |
mshoemaker | 0:0826fcc5d020 | 159 | * |
mshoemaker | 0:0826fcc5d020 | 160 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 161 | */ |
mshoemaker | 0:0826fcc5d020 | 162 | |
mshoemaker | 0:0826fcc5d020 | 163 | USB_INT32S MS_GetMaxLUN (void) |
mshoemaker | 0:0826fcc5d020 | 164 | { |
mshoemaker | 0:0826fcc5d020 | 165 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 166 | |
mshoemaker | 0:0826fcc5d020 | 167 | |
mshoemaker | 0:0826fcc5d020 | 168 | rc = Host_CtrlRecv(USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE, |
mshoemaker | 0:0826fcc5d020 | 169 | MS_GET_MAX_LUN_REQ, |
mshoemaker | 0:0826fcc5d020 | 170 | 0, |
mshoemaker | 0:0826fcc5d020 | 171 | 0, |
mshoemaker | 0:0826fcc5d020 | 172 | 1, |
mshoemaker | 0:0826fcc5d020 | 173 | TDBuffer); |
mshoemaker | 0:0826fcc5d020 | 174 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 175 | } |
mshoemaker | 0:0826fcc5d020 | 176 | |
mshoemaker | 0:0826fcc5d020 | 177 | /* |
mshoemaker | 0:0826fcc5d020 | 178 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 179 | * GET SENSE INFORMATION |
mshoemaker | 0:0826fcc5d020 | 180 | * |
mshoemaker | 0:0826fcc5d020 | 181 | * Description: This function is used to get sense information from the device |
mshoemaker | 0:0826fcc5d020 | 182 | * |
mshoemaker | 0:0826fcc5d020 | 183 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 184 | * |
mshoemaker | 0:0826fcc5d020 | 185 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 186 | * ERROR if Failed |
mshoemaker | 0:0826fcc5d020 | 187 | * |
mshoemaker | 0:0826fcc5d020 | 188 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 189 | */ |
mshoemaker | 0:0826fcc5d020 | 190 | |
mshoemaker | 0:0826fcc5d020 | 191 | USB_INT32S MS_GetSenseInfo (void) |
mshoemaker | 0:0826fcc5d020 | 192 | { |
mshoemaker | 0:0826fcc5d020 | 193 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 194 | |
mshoemaker | 0:0826fcc5d020 | 195 | |
mshoemaker | 0:0826fcc5d020 | 196 | Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_REQUEST_SENSE, 6); |
mshoemaker | 0:0826fcc5d020 | 197 | rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 198 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 199 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, 18); |
mshoemaker | 0:0826fcc5d020 | 200 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 201 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 202 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 203 | if (TDBuffer[12] != 0) { |
mshoemaker | 0:0826fcc5d020 | 204 | rc = ERR_MS_CMD_FAILED; |
mshoemaker | 0:0826fcc5d020 | 205 | } |
mshoemaker | 0:0826fcc5d020 | 206 | } |
mshoemaker | 0:0826fcc5d020 | 207 | } |
mshoemaker | 0:0826fcc5d020 | 208 | } |
mshoemaker | 0:0826fcc5d020 | 209 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 210 | } |
mshoemaker | 0:0826fcc5d020 | 211 | |
mshoemaker | 0:0826fcc5d020 | 212 | /* |
mshoemaker | 0:0826fcc5d020 | 213 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 214 | * TEST UNIT READY |
mshoemaker | 0:0826fcc5d020 | 215 | * |
mshoemaker | 0:0826fcc5d020 | 216 | * Description: This function is used to test whether the unit is ready or not |
mshoemaker | 0:0826fcc5d020 | 217 | * |
mshoemaker | 0:0826fcc5d020 | 218 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 219 | * |
mshoemaker | 0:0826fcc5d020 | 220 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 221 | * ERROR if Failed |
mshoemaker | 0:0826fcc5d020 | 222 | * |
mshoemaker | 0:0826fcc5d020 | 223 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 224 | */ |
mshoemaker | 0:0826fcc5d020 | 225 | |
mshoemaker | 0:0826fcc5d020 | 226 | USB_INT32S MS_TestUnitReady (void) |
mshoemaker | 0:0826fcc5d020 | 227 | { |
mshoemaker | 0:0826fcc5d020 | 228 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 229 | |
mshoemaker | 0:0826fcc5d020 | 230 | |
mshoemaker | 0:0826fcc5d020 | 231 | Fill_MSCommand(0, 0, 0, MS_DATA_DIR_NONE, SCSI_CMD_TEST_UNIT_READY, 6); |
mshoemaker | 0:0826fcc5d020 | 232 | rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 233 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 234 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 235 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 236 | if (TDBuffer[12] != 0) { |
mshoemaker | 0:0826fcc5d020 | 237 | rc = ERR_MS_CMD_FAILED; |
mshoemaker | 0:0826fcc5d020 | 238 | } |
mshoemaker | 0:0826fcc5d020 | 239 | } |
mshoemaker | 0:0826fcc5d020 | 240 | } |
mshoemaker | 0:0826fcc5d020 | 241 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 242 | } |
mshoemaker | 0:0826fcc5d020 | 243 | |
mshoemaker | 0:0826fcc5d020 | 244 | /* |
mshoemaker | 0:0826fcc5d020 | 245 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 246 | * READ CAPACITY |
mshoemaker | 0:0826fcc5d020 | 247 | * |
mshoemaker | 0:0826fcc5d020 | 248 | * Description: This function is used to read the capacity of the mass storage device |
mshoemaker | 0:0826fcc5d020 | 249 | * |
mshoemaker | 0:0826fcc5d020 | 250 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 251 | * |
mshoemaker | 0:0826fcc5d020 | 252 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 253 | * ERROR if Failed |
mshoemaker | 0:0826fcc5d020 | 254 | * |
mshoemaker | 0:0826fcc5d020 | 255 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 256 | */ |
mshoemaker | 0:0826fcc5d020 | 257 | |
mshoemaker | 0:0826fcc5d020 | 258 | USB_INT32S MS_ReadCapacity (USB_INT32U *numBlks, USB_INT32U *blkSize) |
mshoemaker | 0:0826fcc5d020 | 259 | { |
mshoemaker | 0:0826fcc5d020 | 260 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 261 | |
mshoemaker | 0:0826fcc5d020 | 262 | |
mshoemaker | 0:0826fcc5d020 | 263 | Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_READ_CAPACITY, 10); |
mshoemaker | 0:0826fcc5d020 | 264 | rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 265 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 266 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, 8); |
mshoemaker | 0:0826fcc5d020 | 267 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 268 | if (numBlks) |
mshoemaker | 0:0826fcc5d020 | 269 | *numBlks = ReadBE32U(&TDBuffer[0]); |
mshoemaker | 0:0826fcc5d020 | 270 | if (blkSize) |
mshoemaker | 0:0826fcc5d020 | 271 | *blkSize = ReadBE32U(&TDBuffer[4]); |
mshoemaker | 0:0826fcc5d020 | 272 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 273 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 274 | if (TDBuffer[12] != 0) { |
mshoemaker | 0:0826fcc5d020 | 275 | rc = ERR_MS_CMD_FAILED; |
mshoemaker | 0:0826fcc5d020 | 276 | } |
mshoemaker | 0:0826fcc5d020 | 277 | } |
mshoemaker | 0:0826fcc5d020 | 278 | } |
mshoemaker | 0:0826fcc5d020 | 279 | } |
mshoemaker | 0:0826fcc5d020 | 280 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 281 | } |
mshoemaker | 0:0826fcc5d020 | 282 | |
mshoemaker | 0:0826fcc5d020 | 283 | |
mshoemaker | 0:0826fcc5d020 | 284 | |
mshoemaker | 0:0826fcc5d020 | 285 | USB_INT32S MS_Inquire (USB_INT08U *response) |
mshoemaker | 0:0826fcc5d020 | 286 | { |
mshoemaker | 0:0826fcc5d020 | 287 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 288 | USB_INT32U i; |
mshoemaker | 0:0826fcc5d020 | 289 | |
mshoemaker | 0:0826fcc5d020 | 290 | Fill_MSCommand(0, 0, 0, MS_DATA_DIR_IN, SCSI_CMD_INQUIRY, 6); |
mshoemaker | 0:0826fcc5d020 | 291 | rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 292 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 293 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, INQUIRY_LENGTH); |
mshoemaker | 0:0826fcc5d020 | 294 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 295 | if (response) { |
mshoemaker | 0:0826fcc5d020 | 296 | for ( i = 0; i < INQUIRY_LENGTH; i++ ) |
mshoemaker | 0:0826fcc5d020 | 297 | *response++ = *TDBuffer++; |
mshoemaker | 0:0826fcc5d020 | 298 | #if 0 |
mshoemaker | 0:0826fcc5d020 | 299 | MemCpy (response, TDBuffer, INQUIRY_LENGTH); |
mshoemaker | 0:0826fcc5d020 | 300 | StrNullTrailingSpace (response->vendorID, SCSI_INQUIRY_VENDORCHARS); |
mshoemaker | 0:0826fcc5d020 | 301 | StrNullTrailingSpace (response->productID, SCSI_INQUIRY_PRODUCTCHARS); |
mshoemaker | 0:0826fcc5d020 | 302 | StrNullTrailingSpace (response->productRev, SCSI_INQUIRY_REVCHARS); |
mshoemaker | 0:0826fcc5d020 | 303 | #endif |
mshoemaker | 0:0826fcc5d020 | 304 | } |
mshoemaker | 0:0826fcc5d020 | 305 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 306 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 307 | if (TDBuffer[12] != 0) { // bCSWStatus byte |
mshoemaker | 0:0826fcc5d020 | 308 | rc = ERR_MS_CMD_FAILED; |
mshoemaker | 0:0826fcc5d020 | 309 | } |
mshoemaker | 0:0826fcc5d020 | 310 | } |
mshoemaker | 0:0826fcc5d020 | 311 | } |
mshoemaker | 0:0826fcc5d020 | 312 | } |
mshoemaker | 0:0826fcc5d020 | 313 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 314 | } |
mshoemaker | 0:0826fcc5d020 | 315 | |
mshoemaker | 0:0826fcc5d020 | 316 | /* |
mshoemaker | 0:0826fcc5d020 | 317 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 318 | * RECEIVE THE BULK DATA |
mshoemaker | 0:0826fcc5d020 | 319 | * |
mshoemaker | 0:0826fcc5d020 | 320 | * Description: This function is used to receive the bulk data |
mshoemaker | 0:0826fcc5d020 | 321 | * |
mshoemaker | 0:0826fcc5d020 | 322 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 323 | * |
mshoemaker | 0:0826fcc5d020 | 324 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 325 | * ERR_INVALID_BOOTSIG if Failed |
mshoemaker | 0:0826fcc5d020 | 326 | * |
mshoemaker | 0:0826fcc5d020 | 327 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 328 | */ |
mshoemaker | 0:0826fcc5d020 | 329 | |
mshoemaker | 0:0826fcc5d020 | 330 | USB_INT32S MS_BulkRecv ( USB_INT32U block_number, |
mshoemaker | 0:0826fcc5d020 | 331 | USB_INT16U num_blocks, |
mshoemaker | 0:0826fcc5d020 | 332 | volatile USB_INT08U *user_buffer) |
mshoemaker | 0:0826fcc5d020 | 333 | { |
mshoemaker | 0:0826fcc5d020 | 334 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 335 | int i; |
mshoemaker | 0:0826fcc5d020 | 336 | volatile USB_INT08U *c = user_buffer; |
mshoemaker | 0:0826fcc5d020 | 337 | for (i=0;i<MS_BlkSize*num_blocks;i++) |
mshoemaker | 0:0826fcc5d020 | 338 | *c++ = 0; |
mshoemaker | 0:0826fcc5d020 | 339 | |
mshoemaker | 0:0826fcc5d020 | 340 | |
mshoemaker | 0:0826fcc5d020 | 341 | Fill_MSCommand(block_number, MS_BlkSize, num_blocks, MS_DATA_DIR_IN, SCSI_CMD_READ_10, 10); |
mshoemaker | 0:0826fcc5d020 | 342 | |
mshoemaker | 0:0826fcc5d020 | 343 | rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 344 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 345 | rc = Host_ProcessTD(EDBulkIn, TD_IN, user_buffer, MS_BlkSize * num_blocks); |
mshoemaker | 0:0826fcc5d020 | 346 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 347 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 348 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 349 | if (TDBuffer[12] != 0) { |
mshoemaker | 0:0826fcc5d020 | 350 | rc = ERR_MS_CMD_FAILED; |
mshoemaker | 0:0826fcc5d020 | 351 | } |
mshoemaker | 0:0826fcc5d020 | 352 | } |
mshoemaker | 0:0826fcc5d020 | 353 | } |
mshoemaker | 0:0826fcc5d020 | 354 | } |
mshoemaker | 0:0826fcc5d020 | 355 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 356 | } |
mshoemaker | 0:0826fcc5d020 | 357 | |
mshoemaker | 0:0826fcc5d020 | 358 | /* |
mshoemaker | 0:0826fcc5d020 | 359 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 360 | * SEND BULK DATA |
mshoemaker | 0:0826fcc5d020 | 361 | * |
mshoemaker | 0:0826fcc5d020 | 362 | * Description: This function is used to send the bulk data |
mshoemaker | 0:0826fcc5d020 | 363 | * |
mshoemaker | 0:0826fcc5d020 | 364 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 365 | * |
mshoemaker | 0:0826fcc5d020 | 366 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 367 | * ERR_INVALID_BOOTSIG if Failed |
mshoemaker | 0:0826fcc5d020 | 368 | * |
mshoemaker | 0:0826fcc5d020 | 369 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 370 | */ |
mshoemaker | 0:0826fcc5d020 | 371 | |
mshoemaker | 0:0826fcc5d020 | 372 | USB_INT32S MS_BulkSend ( USB_INT32U block_number, |
mshoemaker | 0:0826fcc5d020 | 373 | USB_INT16U num_blocks, |
mshoemaker | 0:0826fcc5d020 | 374 | volatile USB_INT08U *user_buffer) |
mshoemaker | 0:0826fcc5d020 | 375 | { |
mshoemaker | 0:0826fcc5d020 | 376 | USB_INT32S rc; |
mshoemaker | 0:0826fcc5d020 | 377 | |
mshoemaker | 0:0826fcc5d020 | 378 | |
mshoemaker | 0:0826fcc5d020 | 379 | Fill_MSCommand(block_number, MS_BlkSize, num_blocks, MS_DATA_DIR_OUT, SCSI_CMD_WRITE_10, 10); |
mshoemaker | 0:0826fcc5d020 | 380 | |
mshoemaker | 0:0826fcc5d020 | 381 | rc = Host_ProcessTD(EDBulkOut, TD_OUT, TDBuffer, CBW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 382 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 383 | rc = Host_ProcessTD(EDBulkOut, TD_OUT, user_buffer, MS_BlkSize * num_blocks); |
mshoemaker | 0:0826fcc5d020 | 384 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 385 | rc = Host_ProcessTD(EDBulkIn, TD_IN, TDBuffer, CSW_SIZE); |
mshoemaker | 0:0826fcc5d020 | 386 | if (rc == OK) { |
mshoemaker | 0:0826fcc5d020 | 387 | if (TDBuffer[12] != 0) { |
mshoemaker | 0:0826fcc5d020 | 388 | rc = ERR_MS_CMD_FAILED; |
mshoemaker | 0:0826fcc5d020 | 389 | } |
mshoemaker | 0:0826fcc5d020 | 390 | } |
mshoemaker | 0:0826fcc5d020 | 391 | } |
mshoemaker | 0:0826fcc5d020 | 392 | } |
mshoemaker | 0:0826fcc5d020 | 393 | return (rc); |
mshoemaker | 0:0826fcc5d020 | 394 | } |
mshoemaker | 0:0826fcc5d020 | 395 | |
mshoemaker | 0:0826fcc5d020 | 396 | /* |
mshoemaker | 0:0826fcc5d020 | 397 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 398 | * FILL MASS STORAGE COMMAND |
mshoemaker | 0:0826fcc5d020 | 399 | * |
mshoemaker | 0:0826fcc5d020 | 400 | * Description: This function is used to fill the mass storage command |
mshoemaker | 0:0826fcc5d020 | 401 | * |
mshoemaker | 0:0826fcc5d020 | 402 | * Arguments : None |
mshoemaker | 0:0826fcc5d020 | 403 | * |
mshoemaker | 0:0826fcc5d020 | 404 | * Returns : OK if Success |
mshoemaker | 0:0826fcc5d020 | 405 | * ERR_INVALID_BOOTSIG if Failed |
mshoemaker | 0:0826fcc5d020 | 406 | * |
mshoemaker | 0:0826fcc5d020 | 407 | ************************************************************************************************************** |
mshoemaker | 0:0826fcc5d020 | 408 | */ |
mshoemaker | 0:0826fcc5d020 | 409 | |
mshoemaker | 0:0826fcc5d020 | 410 | void Fill_MSCommand (USB_INT32U block_number, |
mshoemaker | 0:0826fcc5d020 | 411 | USB_INT32U block_size, |
mshoemaker | 0:0826fcc5d020 | 412 | USB_INT16U num_blocks, |
mshoemaker | 0:0826fcc5d020 | 413 | MS_DATA_DIR direction, |
mshoemaker | 0:0826fcc5d020 | 414 | USB_INT08U scsi_cmd, |
mshoemaker | 0:0826fcc5d020 | 415 | USB_INT08U scsi_cmd_len) |
mshoemaker | 0:0826fcc5d020 | 416 | { |
mshoemaker | 0:0826fcc5d020 | 417 | USB_INT32U data_len; |
mshoemaker | 0:0826fcc5d020 | 418 | static USB_INT32U tag_cnt = 0; |
mshoemaker | 0:0826fcc5d020 | 419 | USB_INT32U cnt; |
mshoemaker | 0:0826fcc5d020 | 420 | |
mshoemaker | 0:0826fcc5d020 | 421 | |
mshoemaker | 0:0826fcc5d020 | 422 | for (cnt = 0; cnt < CBW_SIZE; cnt++) { |
mshoemaker | 0:0826fcc5d020 | 423 | TDBuffer[cnt] = 0; |
mshoemaker | 0:0826fcc5d020 | 424 | } |
mshoemaker | 0:0826fcc5d020 | 425 | switch(scsi_cmd) { |
mshoemaker | 0:0826fcc5d020 | 426 | |
mshoemaker | 0:0826fcc5d020 | 427 | case SCSI_CMD_TEST_UNIT_READY: |
mshoemaker | 0:0826fcc5d020 | 428 | data_len = 0; |
mshoemaker | 0:0826fcc5d020 | 429 | break; |
mshoemaker | 0:0826fcc5d020 | 430 | case SCSI_CMD_READ_CAPACITY: |
mshoemaker | 0:0826fcc5d020 | 431 | data_len = 8; |
mshoemaker | 0:0826fcc5d020 | 432 | break; |
mshoemaker | 0:0826fcc5d020 | 433 | case SCSI_CMD_REQUEST_SENSE: |
mshoemaker | 0:0826fcc5d020 | 434 | data_len = 18; |
mshoemaker | 0:0826fcc5d020 | 435 | break; |
mshoemaker | 0:0826fcc5d020 | 436 | case SCSI_CMD_INQUIRY: |
mshoemaker | 0:0826fcc5d020 | 437 | data_len = 36; |
mshoemaker | 0:0826fcc5d020 | 438 | break; |
mshoemaker | 0:0826fcc5d020 | 439 | default: |
mshoemaker | 0:0826fcc5d020 | 440 | data_len = block_size * num_blocks; |
mshoemaker | 0:0826fcc5d020 | 441 | break; |
mshoemaker | 0:0826fcc5d020 | 442 | } |
mshoemaker | 0:0826fcc5d020 | 443 | WriteLE32U(TDBuffer, CBW_SIGNATURE); |
mshoemaker | 0:0826fcc5d020 | 444 | WriteLE32U(&TDBuffer[4], tag_cnt); |
mshoemaker | 0:0826fcc5d020 | 445 | WriteLE32U(&TDBuffer[8], data_len); |
mshoemaker | 0:0826fcc5d020 | 446 | TDBuffer[12] = (direction == MS_DATA_DIR_NONE) ? 0 : direction; |
mshoemaker | 0:0826fcc5d020 | 447 | TDBuffer[14] = scsi_cmd_len; /* Length of the CBW */ |
mshoemaker | 0:0826fcc5d020 | 448 | TDBuffer[15] = scsi_cmd; |
mshoemaker | 0:0826fcc5d020 | 449 | if ((scsi_cmd == SCSI_CMD_REQUEST_SENSE) |
mshoemaker | 0:0826fcc5d020 | 450 | || (scsi_cmd == SCSI_CMD_INQUIRY)) { |
mshoemaker | 0:0826fcc5d020 | 451 | TDBuffer[19] = (USB_INT08U)data_len; |
mshoemaker | 0:0826fcc5d020 | 452 | } else { |
mshoemaker | 0:0826fcc5d020 | 453 | WriteBE32U(&TDBuffer[17], block_number); |
mshoemaker | 0:0826fcc5d020 | 454 | } |
mshoemaker | 0:0826fcc5d020 | 455 | WriteBE16U(&TDBuffer[22], num_blocks); |
mshoemaker | 0:0826fcc5d020 | 456 | } |