MassStorageClassHost.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2017.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #define __INCLUDE_FROM_USB_DRIVER
  27. #include "../../Core/USBMode.h"
  28. #if defined(USB_CAN_BE_HOST)
  29. #define __INCLUDE_FROM_MS_DRIVER
  30. #define __INCLUDE_FROM_MASSSTORAGE_HOST_C
  31. #include "MassStorageClassHost.h"
  32. uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  33. uint16_t ConfigDescriptorSize,
  34. void* ConfigDescriptorData)
  35. {
  36. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  37. USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
  38. USB_Descriptor_Interface_t* MassStorageInterface = NULL;
  39. memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
  40. if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
  41. return MS_ENUMERROR_InvalidConfigDescriptor;
  42. while (!(DataINEndpoint) || !(DataOUTEndpoint))
  43. {
  44. if (!(MassStorageInterface) ||
  45. USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  46. DCOMP_MS_Host_NextMSInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  47. {
  48. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  49. DCOMP_MS_Host_NextMSInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  50. {
  51. return MS_ENUMERROR_NoCompatibleInterfaceFound;
  52. }
  53. MassStorageInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
  54. DataINEndpoint = NULL;
  55. DataOUTEndpoint = NULL;
  56. continue;
  57. }
  58. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
  59. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  60. DataINEndpoint = EndpointData;
  61. else
  62. DataOUTEndpoint = EndpointData;
  63. }
  64. MSInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
  65. MSInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
  66. MSInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_BULK;
  67. MSInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
  68. MSInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
  69. MSInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_BULK;
  70. if (!(Pipe_ConfigurePipeTable(&MSInterfaceInfo->Config.DataINPipe, 1)))
  71. return MS_ENUMERROR_PipeConfigurationFailed;
  72. if (!(Pipe_ConfigurePipeTable(&MSInterfaceInfo->Config.DataOUTPipe, 1)))
  73. return MS_ENUMERROR_PipeConfigurationFailed;
  74. MSInterfaceInfo->State.InterfaceNumber = MassStorageInterface->InterfaceNumber;
  75. MSInterfaceInfo->State.IsActive = true;
  76. return MS_ENUMERROR_NoError;
  77. }
  78. static uint8_t DCOMP_MS_Host_NextMSInterface(void* const CurrentDescriptor)
  79. {
  80. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  81. if (Header->Type == DTYPE_Interface)
  82. {
  83. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  84. if ((Interface->Class == MS_CSCP_MassStorageClass) &&
  85. (Interface->SubClass == MS_CSCP_SCSITransparentSubclass) &&
  86. (Interface->Protocol == MS_CSCP_BulkOnlyTransportProtocol))
  87. {
  88. return DESCRIPTOR_SEARCH_Found;
  89. }
  90. }
  91. return DESCRIPTOR_SEARCH_NotFound;
  92. }
  93. static uint8_t DCOMP_MS_Host_NextMSInterfaceEndpoint(void* const CurrentDescriptor)
  94. {
  95. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  96. if (Header->Type == DTYPE_Endpoint)
  97. {
  98. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  99. uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
  100. if ((EndpointType == EP_TYPE_BULK) && (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress))))
  101. {
  102. return DESCRIPTOR_SEARCH_Found;
  103. }
  104. }
  105. else if (Header->Type == DTYPE_Interface)
  106. {
  107. return DESCRIPTOR_SEARCH_Fail;
  108. }
  109. return DESCRIPTOR_SEARCH_NotFound;
  110. }
  111. static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  112. MS_CommandBlockWrapper_t* const SCSICommandBlock,
  113. const void* const BufferPtr)
  114. {
  115. uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
  116. if (++MSInterfaceInfo->State.TransactionTag == 0xFFFFFFFF)
  117. MSInterfaceInfo->State.TransactionTag = 1;
  118. SCSICommandBlock->Signature = CPU_TO_LE32(MS_CBW_SIGNATURE);
  119. SCSICommandBlock->Tag = cpu_to_le32(MSInterfaceInfo->State.TransactionTag);
  120. Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipe.Address);
  121. Pipe_Unfreeze();
  122. if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t),
  123. NULL)) != PIPE_RWSTREAM_NoError)
  124. {
  125. return ErrorCode;
  126. }
  127. Pipe_ClearOUT();
  128. Pipe_WaitUntilReady();
  129. Pipe_Freeze();
  130. if (BufferPtr != NULL)
  131. {
  132. ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, SCSICommandBlock, (void*)BufferPtr);
  133. if ((ErrorCode != PIPE_RWSTREAM_NoError) && (ErrorCode != PIPE_RWSTREAM_PipeStalled))
  134. {
  135. Pipe_Freeze();
  136. return ErrorCode;
  137. }
  138. }
  139. MS_CommandStatusWrapper_t SCSIStatusBlock;
  140. return MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSIStatusBlock);
  141. }
  142. static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
  143. {
  144. uint16_t TimeoutMSRem = MS_COMMAND_DATA_TIMEOUT_MS;
  145. uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
  146. Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipe.Address);
  147. Pipe_Unfreeze();
  148. while (!(Pipe_IsINReceived()))
  149. {
  150. uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
  151. if (CurrentFrameNumber != PreviousFrameNumber)
  152. {
  153. PreviousFrameNumber = CurrentFrameNumber;
  154. if (!(TimeoutMSRem--))
  155. return PIPE_RWSTREAM_Timeout;
  156. }
  157. Pipe_Freeze();
  158. Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipe.Address);
  159. Pipe_Unfreeze();
  160. if (Pipe_IsStalled())
  161. {
  162. USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
  163. return PIPE_RWSTREAM_PipeStalled;
  164. }
  165. Pipe_Freeze();
  166. Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipe.Address);
  167. Pipe_Unfreeze();
  168. if (Pipe_IsStalled())
  169. {
  170. USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
  171. return PIPE_RWSTREAM_PipeStalled;
  172. }
  173. if (USB_HostState == HOST_STATE_Unattached)
  174. return PIPE_RWSTREAM_DeviceDisconnected;
  175. };
  176. Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipe.Address);
  177. Pipe_Freeze();
  178. Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipe.Address);
  179. Pipe_Freeze();
  180. return PIPE_RWSTREAM_NoError;
  181. }
  182. static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  183. MS_CommandBlockWrapper_t* const SCSICommandBlock,
  184. void* BufferPtr)
  185. {
  186. uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
  187. uint16_t BytesRem = le32_to_cpu(SCSICommandBlock->DataTransferLength);
  188. if (SCSICommandBlock->Flags & MS_COMMAND_DIR_DATA_IN)
  189. {
  190. if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
  191. {
  192. Pipe_Freeze();
  193. return ErrorCode;
  194. }
  195. Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipe.Address);
  196. Pipe_Unfreeze();
  197. if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem, NULL)) != PIPE_RWSTREAM_NoError)
  198. return ErrorCode;
  199. Pipe_ClearIN();
  200. }
  201. else
  202. {
  203. Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipe.Address);
  204. Pipe_Unfreeze();
  205. if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem, NULL)) != PIPE_RWSTREAM_NoError)
  206. return ErrorCode;
  207. Pipe_ClearOUT();
  208. while (!(Pipe_IsOUTReady()))
  209. {
  210. if (USB_HostState == HOST_STATE_Unattached)
  211. return PIPE_RWSTREAM_DeviceDisconnected;
  212. }
  213. }
  214. Pipe_Freeze();
  215. return ErrorCode;
  216. }
  217. static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  218. MS_CommandStatusWrapper_t* const SCSICommandStatus)
  219. {
  220. uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
  221. if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
  222. return ErrorCode;
  223. Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipe.Address);
  224. Pipe_Unfreeze();
  225. if ((ErrorCode = Pipe_Read_Stream_LE(SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t),
  226. NULL)) != PIPE_RWSTREAM_NoError)
  227. {
  228. return ErrorCode;
  229. }
  230. Pipe_ClearIN();
  231. Pipe_Freeze();
  232. if (SCSICommandStatus->Status != MS_SCSI_COMMAND_Pass)
  233. ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED;
  234. return ErrorCode;
  235. }
  236. uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
  237. {
  238. uint8_t ErrorCode;
  239. USB_ControlRequest = (USB_Request_Header_t)
  240. {
  241. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  242. .bRequest = MS_REQ_MassStorageReset,
  243. .wValue = 0,
  244. .wIndex = MSInterfaceInfo->State.InterfaceNumber,
  245. .wLength = 0,
  246. };
  247. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  248. if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
  249. return ErrorCode;
  250. Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipe.Address);
  251. if ((ErrorCode = USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress())) != HOST_SENDCONTROL_Successful)
  252. return ErrorCode;
  253. Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipe.Address);
  254. if ((ErrorCode = USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress())) != HOST_SENDCONTROL_Successful)
  255. return ErrorCode;
  256. return HOST_SENDCONTROL_Successful;
  257. }
  258. uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  259. uint8_t* const MaxLUNIndex)
  260. {
  261. uint8_t ErrorCode;
  262. USB_ControlRequest = (USB_Request_Header_t)
  263. {
  264. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
  265. .bRequest = MS_REQ_GetMaxLUN,
  266. .wValue = 0,
  267. .wIndex = MSInterfaceInfo->State.InterfaceNumber,
  268. .wLength = 1,
  269. };
  270. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  271. if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)
  272. {
  273. *MaxLUNIndex = 0;
  274. ErrorCode = HOST_SENDCONTROL_Successful;
  275. }
  276. return ErrorCode;
  277. }
  278. uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  279. const uint8_t LUNIndex,
  280. SCSI_Inquiry_Response_t* const InquiryData)
  281. {
  282. if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
  283. return HOST_SENDCONTROL_DeviceDisconnected;
  284. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  285. {
  286. .DataTransferLength = CPU_TO_LE32(sizeof(SCSI_Inquiry_Response_t)),
  287. .Flags = MS_COMMAND_DIR_DATA_IN,
  288. .LUN = LUNIndex,
  289. .SCSICommandLength = 6,
  290. .SCSICommandData =
  291. {
  292. SCSI_CMD_INQUIRY,
  293. 0x00, // Reserved
  294. 0x00, // Reserved
  295. 0x00, // Reserved
  296. sizeof(SCSI_Inquiry_Response_t), // Allocation Length
  297. 0x00 // Unused (control)
  298. }
  299. };
  300. return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, InquiryData);
  301. }
  302. uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  303. const uint8_t LUNIndex)
  304. {
  305. if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
  306. return HOST_SENDCONTROL_DeviceDisconnected;
  307. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  308. {
  309. .DataTransferLength = CPU_TO_LE32(0),
  310. .Flags = MS_COMMAND_DIR_DATA_IN,
  311. .LUN = LUNIndex,
  312. .SCSICommandLength = 6,
  313. .SCSICommandData =
  314. {
  315. SCSI_CMD_TEST_UNIT_READY,
  316. 0x00, // Reserved
  317. 0x00, // Reserved
  318. 0x00, // Reserved
  319. 0x00, // Reserved
  320. 0x00 // Unused (control)
  321. }
  322. };
  323. return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL);
  324. }
  325. uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  326. const uint8_t LUNIndex,
  327. SCSI_Capacity_t* const DeviceCapacity)
  328. {
  329. if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
  330. return HOST_SENDCONTROL_DeviceDisconnected;
  331. uint8_t ErrorCode;
  332. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  333. {
  334. .DataTransferLength = CPU_TO_LE32(sizeof(SCSI_Capacity_t)),
  335. .Flags = MS_COMMAND_DIR_DATA_IN,
  336. .LUN = LUNIndex,
  337. .SCSICommandLength = 10,
  338. .SCSICommandData =
  339. {
  340. SCSI_CMD_READ_CAPACITY_10,
  341. 0x00, // Reserved
  342. 0x00, // MSB of Logical block address
  343. 0x00,
  344. 0x00,
  345. 0x00, // LSB of Logical block address
  346. 0x00, // Reserved
  347. 0x00, // Reserved
  348. 0x00, // Partial Medium Indicator
  349. 0x00 // Unused (control)
  350. }
  351. };
  352. if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError)
  353. return ErrorCode;
  354. DeviceCapacity->Blocks = BE32_TO_CPU(DeviceCapacity->Blocks);
  355. DeviceCapacity->BlockSize = BE32_TO_CPU(DeviceCapacity->BlockSize);
  356. return PIPE_RWSTREAM_NoError;
  357. }
  358. uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  359. const uint8_t LUNIndex,
  360. SCSI_Request_Sense_Response_t* const SenseData)
  361. {
  362. if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
  363. return HOST_SENDCONTROL_DeviceDisconnected;
  364. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  365. {
  366. .DataTransferLength = CPU_TO_LE32(sizeof(SCSI_Request_Sense_Response_t)),
  367. .Flags = MS_COMMAND_DIR_DATA_IN,
  368. .LUN = LUNIndex,
  369. .SCSICommandLength = 6,
  370. .SCSICommandData =
  371. {
  372. SCSI_CMD_REQUEST_SENSE,
  373. 0x00, // Reserved
  374. 0x00, // Reserved
  375. 0x00, // Reserved
  376. sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
  377. 0x00 // Unused (control)
  378. }
  379. };
  380. return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, SenseData);
  381. }
  382. uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  383. const uint8_t LUNIndex,
  384. const bool PreventRemoval)
  385. {
  386. if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
  387. return HOST_SENDCONTROL_DeviceDisconnected;
  388. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  389. {
  390. .DataTransferLength = CPU_TO_LE32(0),
  391. .Flags = MS_COMMAND_DIR_DATA_OUT,
  392. .LUN = LUNIndex,
  393. .SCSICommandLength = 6,
  394. .SCSICommandData =
  395. {
  396. SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
  397. 0x00, // Reserved
  398. 0x00, // Reserved
  399. PreventRemoval, // Prevent flag
  400. 0x00, // Reserved
  401. 0x00 // Unused (control)
  402. }
  403. };
  404. return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL);
  405. }
  406. uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  407. const uint8_t LUNIndex,
  408. const uint32_t BlockAddress,
  409. const uint8_t Blocks,
  410. const uint16_t BlockSize,
  411. void* BlockBuffer)
  412. {
  413. if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
  414. return HOST_SENDCONTROL_DeviceDisconnected;
  415. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  416. {
  417. .DataTransferLength = cpu_to_le32((uint32_t)Blocks * BlockSize),
  418. .Flags = MS_COMMAND_DIR_DATA_IN,
  419. .LUN = LUNIndex,
  420. .SCSICommandLength = 10,
  421. .SCSICommandData =
  422. {
  423. SCSI_CMD_READ_10,
  424. 0x00, // Unused (control bits, all off)
  425. (BlockAddress >> 24), // MSB of Block Address
  426. (BlockAddress >> 16),
  427. (BlockAddress >> 8),
  428. (BlockAddress & 0xFF), // LSB of Block Address
  429. 0x00, // Reserved
  430. 0x00, // MSB of Total Blocks to Read
  431. Blocks, // LSB of Total Blocks to Read
  432. 0x00 // Unused (control)
  433. }
  434. };
  435. return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer);
  436. }
  437. uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
  438. const uint8_t LUNIndex,
  439. const uint32_t BlockAddress,
  440. const uint8_t Blocks,
  441. const uint16_t BlockSize,
  442. const void* BlockBuffer)
  443. {
  444. if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
  445. return HOST_SENDCONTROL_DeviceDisconnected;
  446. MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
  447. {
  448. .DataTransferLength = cpu_to_le32((uint32_t)Blocks * BlockSize),
  449. .Flags = MS_COMMAND_DIR_DATA_OUT,
  450. .LUN = LUNIndex,
  451. .SCSICommandLength = 10,
  452. .SCSICommandData =
  453. {
  454. SCSI_CMD_WRITE_10,
  455. 0x00, // Unused (control bits, all off)
  456. (BlockAddress >> 24), // MSB of Block Address
  457. (BlockAddress >> 16),
  458. (BlockAddress >> 8),
  459. (BlockAddress & 0xFF), // LSB of Block Address
  460. 0x00, // Reserved
  461. 0x00, // MSB of Total Blocks to Write
  462. Blocks, // LSB of Total Blocks to Write
  463. 0x00 // Unused (control)
  464. }
  465. };
  466. return MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer);
  467. }
  468. #endif