as

Dependencies:   C12832 CMPS03 FatFileSystemCpp mbed

Committer:
JWitherstone
Date:
Wed May 07 10:15:01 2014 +0000
Revision:
0:6cf24371ad4f
this is compass;

Who changed what in which revision?

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