| 1 |
/** |
| 2 |
****************************************************************************** |
| 3 |
* @file usbh_msc_scsi.c |
| 4 |
* @author MCD Application Team |
| 5 |
* @version V3.0.0 |
| 6 |
* @date 18-February-2014 |
| 7 |
* @brief This file implements the SCSI commands |
| 8 |
****************************************************************************** |
| 9 |
* @attention |
| 10 |
* |
| 11 |
* <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> |
| 12 |
* |
| 13 |
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); |
| 14 |
* You may not use this file except in compliance with the License. |
| 15 |
* You may obtain a copy of the License at: |
| 16 |
* |
| 17 |
* http://www.st.com/software_license_agreement_liberty_v2 |
| 18 |
* |
| 19 |
* Unless required by applicable law or agreed to in writing, software |
| 20 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 21 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 |
* See the License for the specific language governing permissions and |
| 23 |
* limitations under the License. |
| 24 |
* |
| 25 |
****************************************************************************** |
| 26 |
*/ |
| 27 |
|
| 28 |
/* Includes ------------------------------------------------------------------*/ |
| 29 |
#include "usbh_msc.h" |
| 30 |
#include "usbh_msc_scsi.h" |
| 31 |
#include "usbh_msc_bot.h" |
| 32 |
|
| 33 |
|
| 34 |
/** @addtogroup USBH_LIB |
| 35 |
* @{ |
| 36 |
*/ |
| 37 |
|
| 38 |
/** @addtogroup USBH_CLASS |
| 39 |
* @{ |
| 40 |
*/ |
| 41 |
|
| 42 |
/** @addtogroup USBH_MSC_CLASS |
| 43 |
* @{ |
| 44 |
*/ |
| 45 |
|
| 46 |
/** @defgroup USBH_MSC_SCSI |
| 47 |
* @brief This file includes the mass storage related functions |
| 48 |
* @{ |
| 49 |
*/ |
| 50 |
|
| 51 |
|
| 52 |
/** @defgroup USBH_MSC_SCSI_Private_TypesDefinitions |
| 53 |
* @{ |
| 54 |
*/ |
| 55 |
|
| 56 |
/** |
| 57 |
* @} |
| 58 |
*/ |
| 59 |
|
| 60 |
/** @defgroup USBH_MSC_SCSI_Private_Defines |
| 61 |
* @{ |
| 62 |
*/ |
| 63 |
/** |
| 64 |
* @} |
| 65 |
*/ |
| 66 |
|
| 67 |
/** @defgroup USBH_MSC_SCSI_Private_Macros |
| 68 |
* @{ |
| 69 |
*/ |
| 70 |
/** |
| 71 |
* @} |
| 72 |
*/ |
| 73 |
|
| 74 |
|
| 75 |
/** @defgroup USBH_MSC_SCSI_Private_FunctionPrototypes |
| 76 |
* @{ |
| 77 |
*/ |
| 78 |
/** |
| 79 |
* @} |
| 80 |
*/ |
| 81 |
|
| 82 |
|
| 83 |
/** @defgroup USBH_MSC_SCSI_Exported_Variables |
| 84 |
* @{ |
| 85 |
*/ |
| 86 |
|
| 87 |
/** |
| 88 |
* @} |
| 89 |
*/ |
| 90 |
|
| 91 |
|
| 92 |
/** @defgroup USBH_MSC_SCSI_Private_Functions |
| 93 |
* @{ |
| 94 |
*/ |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* @brief USBH_MSC_SCSI_TestUnitReady |
| 99 |
* Issue TestUnitReady command. |
| 100 |
* @param phost: Host handle |
| 101 |
* @param lun: Logical Unit Number |
| 102 |
* @retval USBH Status |
| 103 |
*/ |
| 104 |
USBH_StatusTypeDef USBH_MSC_SCSI_TestUnitReady (USBH_HandleTypeDef *phost, |
| 105 |
uint8_t lun) |
| 106 |
{ |
| 107 |
USBH_StatusTypeDef error = USBH_FAIL ; |
| 108 |
MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData; |
| 109 |
|
| 110 |
switch(MSC_Handle->hbot.cmd_state) |
| 111 |
{ |
| 112 |
case BOT_CMD_SEND: |
| 113 |
|
| 114 |
/*Prepare the CBW and relevent field*/ |
| 115 |
MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_MODE_TEST_UNIT_READY; |
| 116 |
MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT; |
| 117 |
MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; |
| 118 |
|
| 119 |
USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); |
| 120 |
MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_TEST_UNIT_READY; |
| 121 |
|
| 122 |
MSC_Handle->hbot.state = BOT_SEND_CBW; |
| 123 |
MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; |
| 124 |
error = USBH_BUSY; |
| 125 |
break; |
| 126 |
|
| 127 |
case BOT_CMD_WAIT: |
| 128 |
error = USBH_MSC_BOT_Process(phost, lun); |
| 129 |
break; |
| 130 |
|
| 131 |
default: |
| 132 |
break; |
| 133 |
} |
| 134 |
|
| 135 |
return error; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @brief USBH_MSC_SCSI_ReadCapacity |
| 140 |
* Issue Read Capacity command. |
| 141 |
* @param phost: Host handle |
| 142 |
* @param lun: Logical Unit Number |
| 143 |
* @param capacity: pointer to the capacity structure |
| 144 |
* @retval USBH Status |
| 145 |
*/ |
| 146 |
USBH_StatusTypeDef USBH_MSC_SCSI_ReadCapacity (USBH_HandleTypeDef *phost, |
| 147 |
uint8_t lun, |
| 148 |
SCSI_CapacityTypeDef *capacity) |
| 149 |
{ |
| 150 |
USBH_StatusTypeDef error = USBH_BUSY ; |
| 151 |
MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData; |
| 152 |
|
| 153 |
switch(MSC_Handle->hbot.cmd_state) |
| 154 |
{ |
| 155 |
case BOT_CMD_SEND: |
| 156 |
|
| 157 |
/*Prepare the CBW and relevent field*/ |
| 158 |
MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_READ_CAPACITY10; |
| 159 |
MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; |
| 160 |
MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; |
| 161 |
|
| 162 |
USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); |
| 163 |
MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ_CAPACITY10; |
| 164 |
|
| 165 |
MSC_Handle->hbot.state = BOT_SEND_CBW; |
| 166 |
|
| 167 |
MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; |
| 168 |
MSC_Handle->hbot.pbuf = (uint8_t *)MSC_Handle->hbot.data; |
| 169 |
error = USBH_BUSY; |
| 170 |
break; |
| 171 |
|
| 172 |
case BOT_CMD_WAIT: |
| 173 |
|
| 174 |
error = USBH_MSC_BOT_Process(phost, lun); |
| 175 |
|
| 176 |
if(error == USBH_OK) |
| 177 |
{ |
| 178 |
/*assign the capacity*/ |
| 179 |
capacity->block_nbr = MSC_Handle->hbot.pbuf[3] | (MSC_Handle->hbot.pbuf[2] << 8) |\ |
| 180 |
(MSC_Handle->hbot.pbuf[1] << 16) | (MSC_Handle->hbot.pbuf[0] << 24); |
| 181 |
|
| 182 |
/*assign the page length*/ |
| 183 |
capacity->block_size = MSC_Handle->hbot.pbuf[7] | (MSC_Handle->hbot.pbuf[6] << 8); |
| 184 |
} |
| 185 |
break; |
| 186 |
|
| 187 |
default: |
| 188 |
break; |
| 189 |
} |
| 190 |
|
| 191 |
return error; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* @brief USBH_MSC_SCSI_Inquiry |
| 196 |
* Issue Inquiry command. |
| 197 |
* @param phost: Host handle |
| 198 |
* @param lun: Logical Unit Number |
| 199 |
* @param capacity: pointer to the inquiry structure |
| 200 |
* @retval USBH Status |
| 201 |
*/ |
| 202 |
USBH_StatusTypeDef USBH_MSC_SCSI_Inquiry (USBH_HandleTypeDef *phost, |
| 203 |
uint8_t lun, |
| 204 |
SCSI_StdInquiryDataTypeDef *inquiry) |
| 205 |
{ |
| 206 |
USBH_StatusTypeDef error = USBH_FAIL ; |
| 207 |
MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData; |
| 208 |
switch(MSC_Handle->hbot.cmd_state) |
| 209 |
{ |
| 210 |
case BOT_CMD_SEND: |
| 211 |
|
| 212 |
/*Prepare the CBW and relevent field*/ |
| 213 |
MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_INQUIRY; |
| 214 |
MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; |
| 215 |
MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; |
| 216 |
|
| 217 |
USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_LENGTH); |
| 218 |
MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_INQUIRY; |
| 219 |
MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5); |
| 220 |
MSC_Handle->hbot.cbw.field.CB[2] = 0; |
| 221 |
MSC_Handle->hbot.cbw.field.CB[3] = 0; |
| 222 |
MSC_Handle->hbot.cbw.field.CB[4] = 0x24; |
| 223 |
MSC_Handle->hbot.cbw.field.CB[5] = 0; |
| 224 |
|
| 225 |
MSC_Handle->hbot.state = BOT_SEND_CBW; |
| 226 |
|
| 227 |
MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; |
| 228 |
MSC_Handle->hbot.pbuf = (uint8_t *)MSC_Handle->hbot.data; |
| 229 |
error = USBH_BUSY; |
| 230 |
break; |
| 231 |
|
| 232 |
case BOT_CMD_WAIT: |
| 233 |
|
| 234 |
error = USBH_MSC_BOT_Process(phost, lun); |
| 235 |
|
| 236 |
if(error == USBH_OK) |
| 237 |
{ |
| 238 |
USBH_memset(inquiry, 0, sizeof(SCSI_StdInquiryDataTypeDef)); |
| 239 |
/*assign Inquiry Data */ |
| 240 |
inquiry->DeviceType = MSC_Handle->hbot.pbuf[0] & 0x1F; |
| 241 |
inquiry->PeripheralQualifier = MSC_Handle->hbot.pbuf[0] >> 5; |
| 242 |
inquiry->RemovableMedia = (MSC_Handle->hbot.pbuf[1] & 0x80)== 0x80; |
| 243 |
USBH_memcpy (inquiry->vendor_id, &MSC_Handle->hbot.pbuf[8], 8); |
| 244 |
USBH_memcpy (inquiry->product_id, &MSC_Handle->hbot.pbuf[16], 16); |
| 245 |
USBH_memcpy (inquiry->revision_id, &MSC_Handle->hbot.pbuf[32], 4); |
| 246 |
} |
| 247 |
break; |
| 248 |
|
| 249 |
default: |
| 250 |
break; |
| 251 |
} |
| 252 |
|
| 253 |
return error; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* @brief USBH_MSC_SCSI_RequestSense |
| 258 |
* Issue RequestSense command. |
| 259 |
* @param phost: Host handle |
| 260 |
* @param lun: Logical Unit Number |
| 261 |
* @param capacity: pointer to the sense data structure |
| 262 |
* @retval USBH Status |
| 263 |
*/ |
| 264 |
USBH_StatusTypeDef USBH_MSC_SCSI_RequestSense (USBH_HandleTypeDef *phost, |
| 265 |
uint8_t lun, |
| 266 |
SCSI_SenseTypeDef *sense_data) |
| 267 |
{ |
| 268 |
USBH_StatusTypeDef error = USBH_FAIL ; |
| 269 |
MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData; |
| 270 |
|
| 271 |
switch(MSC_Handle->hbot.cmd_state) |
| 272 |
{ |
| 273 |
case BOT_CMD_SEND: |
| 274 |
|
| 275 |
/*Prepare the CBW and relevent field*/ |
| 276 |
MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_REQUEST_SENSE; |
| 277 |
MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; |
| 278 |
MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; |
| 279 |
|
| 280 |
USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); |
| 281 |
MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_REQUEST_SENSE; |
| 282 |
MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5); |
| 283 |
MSC_Handle->hbot.cbw.field.CB[2] = 0; |
| 284 |
MSC_Handle->hbot.cbw.field.CB[3] = 0; |
| 285 |
MSC_Handle->hbot.cbw.field.CB[4] = DATA_LEN_REQUEST_SENSE; |
| 286 |
MSC_Handle->hbot.cbw.field.CB[5] = 0; |
| 287 |
|
| 288 |
MSC_Handle->hbot.state = BOT_SEND_CBW; |
| 289 |
MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; |
| 290 |
MSC_Handle->hbot.pbuf = (uint8_t *)MSC_Handle->hbot.data; |
| 291 |
error = USBH_BUSY; |
| 292 |
break; |
| 293 |
|
| 294 |
case BOT_CMD_WAIT: |
| 295 |
|
| 296 |
error = USBH_MSC_BOT_Process(phost, lun); |
| 297 |
|
| 298 |
if(error == USBH_OK) |
| 299 |
{ |
| 300 |
sense_data->key = MSC_Handle->hbot.pbuf[2] & 0x0F; |
| 301 |
sense_data->asc = MSC_Handle->hbot.pbuf[12]; |
| 302 |
sense_data->ascq = MSC_Handle->hbot.pbuf[13]; |
| 303 |
} |
| 304 |
break; |
| 305 |
|
| 306 |
default: |
| 307 |
break; |
| 308 |
} |
| 309 |
|
| 310 |
return error; |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* @brief USBH_MSC_SCSI_Write |
| 315 |
* Issue write10 command. |
| 316 |
* @param phost: Host handle |
| 317 |
* @param lun: Logical Unit Number |
| 318 |
* @param address: sector address |
| 319 |
* @param pbuf: pointer to data |
| 320 |
* @param length: number of sector to write |
| 321 |
* @retval USBH Status |
| 322 |
*/ |
| 323 |
USBH_StatusTypeDef USBH_MSC_SCSI_Write(USBH_HandleTypeDef *phost, |
| 324 |
uint8_t lun, |
| 325 |
uint32_t address, |
| 326 |
uint8_t *pbuf, |
| 327 |
uint32_t length) |
| 328 |
{ |
| 329 |
USBH_StatusTypeDef error = USBH_FAIL ; |
| 330 |
|
| 331 |
MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData; |
| 332 |
|
| 333 |
switch(MSC_Handle->hbot.cmd_state) |
| 334 |
{ |
| 335 |
case BOT_CMD_SEND: |
| 336 |
|
| 337 |
/*Prepare the CBW and relevent field*/ |
| 338 |
MSC_Handle->hbot.cbw.field.DataTransferLength = length * 512; |
| 339 |
MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT; |
| 340 |
MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; |
| 341 |
|
| 342 |
USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); |
| 343 |
MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_WRITE10; |
| 344 |
|
| 345 |
/*logical block address*/ |
| 346 |
MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)&address)[3]); |
| 347 |
MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)&address)[2]); |
| 348 |
MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)&address)[1]); |
| 349 |
MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)&address)[0]); |
| 350 |
|
| 351 |
|
| 352 |
/*Tranfer length */ |
| 353 |
MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)&length)[1]) ; |
| 354 |
MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)&length)[0]) ; |
| 355 |
|
| 356 |
|
| 357 |
MSC_Handle->hbot.state = BOT_SEND_CBW; |
| 358 |
MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; |
| 359 |
MSC_Handle->hbot.pbuf = pbuf; |
| 360 |
error = USBH_BUSY; |
| 361 |
break; |
| 362 |
|
| 363 |
case BOT_CMD_WAIT: |
| 364 |
error = USBH_MSC_BOT_Process(phost, lun); |
| 365 |
break; |
| 366 |
|
| 367 |
default: |
| 368 |
break; |
| 369 |
} |
| 370 |
|
| 371 |
return error; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* @brief USBH_MSC_SCSI_Read |
| 376 |
* Issue Read10 command. |
| 377 |
* @param phost: Host handle |
| 378 |
* @param lun: Logical Unit Number |
| 379 |
* @param address: sector address |
| 380 |
* @param pbuf: pointer to data |
| 381 |
* @param length: number of sector to read |
| 382 |
* @retval USBH Status |
| 383 |
*/ |
| 384 |
USBH_StatusTypeDef USBH_MSC_SCSI_Read(USBH_HandleTypeDef *phost, |
| 385 |
uint8_t lun, |
| 386 |
uint32_t address, |
| 387 |
uint8_t *pbuf, |
| 388 |
uint32_t length) |
| 389 |
{ |
| 390 |
USBH_StatusTypeDef error = USBH_FAIL ; |
| 391 |
MSC_HandleTypeDef *MSC_Handle = phost->pActiveClass->pData; |
| 392 |
|
| 393 |
switch(MSC_Handle->hbot.cmd_state) |
| 394 |
{ |
| 395 |
case BOT_CMD_SEND: |
| 396 |
|
| 397 |
/*Prepare the CBW and relevent field*/ |
| 398 |
MSC_Handle->hbot.cbw.field.DataTransferLength = length * 512; |
| 399 |
MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; |
| 400 |
MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; |
| 401 |
|
| 402 |
USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); |
| 403 |
MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ10; |
| 404 |
|
| 405 |
/*logical block address*/ |
| 406 |
MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)&address)[3]); |
| 407 |
MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)&address)[2]); |
| 408 |
MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)&address)[1]); |
| 409 |
MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)&address)[0]); |
| 410 |
|
| 411 |
|
| 412 |
/*Tranfer length */ |
| 413 |
MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)&length)[1]) ; |
| 414 |
MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)&length)[0]) ; |
| 415 |
|
| 416 |
|
| 417 |
MSC_Handle->hbot.state = BOT_SEND_CBW; |
| 418 |
MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; |
| 419 |
MSC_Handle->hbot.pbuf = pbuf; |
| 420 |
error = USBH_BUSY; |
| 421 |
break; |
| 422 |
|
| 423 |
case BOT_CMD_WAIT: |
| 424 |
error = USBH_MSC_BOT_Process(phost, lun); |
| 425 |
break; |
| 426 |
|
| 427 |
default: |
| 428 |
break; |
| 429 |
} |
| 430 |
|
| 431 |
return error; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
/** |
| 436 |
* @} |
| 437 |
*/ |
| 438 |
|
| 439 |
/** |
| 440 |
* @} |
| 441 |
*/ |
| 442 |
|
| 443 |
/** |
| 444 |
* @} |
| 445 |
*/ |
| 446 |
|
| 447 |
/** |
| 448 |
* @} |
| 449 |
*/ |
| 450 |
|
| 451 |
/** |
| 452 |
* @} |
| 453 |
*/ |
| 454 |
|
| 455 |
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
| 456 |
|
| 457 |
|
| 458 |
|