Fix documentation reference error.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / MassStorage.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
34
35 #define __INCLUDE_FROM_MS_DRIVER
36 #define __INCLUDE_FROM_MASSSTORAGE_HOST_C
37 #include "MassStorage.h"
38
39 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
40 uint16_t ConfigDescriptorSize,
41 void* ConfigDescriptorData)
42 {
43 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
44 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
45 USB_Descriptor_Interface_t* MassStorageInterface = NULL;
46
47 memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
48
49 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
50 return MS_ENUMERROR_InvalidConfigDescriptor;
51
52 while (!(DataINEndpoint) || !(DataOUTEndpoint))
53 {
54 if (!(MassStorageInterface) ||
55 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
56 DCOMP_MS_Host_NextMSInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
57 {
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
59 DCOMP_MS_Host_NextMSInterface) != DESCRIPTOR_SEARCH_COMP_Found)
60 {
61 return MS_ENUMERROR_NoCompatibleInterfaceFound;
62 }
63
64 MassStorageInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
65
66 DataINEndpoint = NULL;
67 DataOUTEndpoint = NULL;
68
69 continue;
70 }
71
72 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
73
74 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
75 DataINEndpoint = EndpointData;
76 else
77 DataOUTEndpoint = EndpointData;
78 }
79
80 for (uint8_t PipeNum = 1; PipeNum < PIPE_TOTAL_PIPES; PipeNum++)
81 {
82 if (PipeNum == MSInterfaceInfo->Config.DataINPipeNumber)
83 {
84 Pipe_ConfigurePipe(PipeNum, EP_TYPE_BULK, PIPE_TOKEN_IN,
85 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize,
86 MSInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
87
88 MSInterfaceInfo->State.DataINPipeSize = DataINEndpoint->EndpointSize;
89 }
90 else if (PipeNum == MSInterfaceInfo->Config.DataOUTPipeNumber)
91 {
92 Pipe_ConfigurePipe(PipeNum, EP_TYPE_BULK, PIPE_TOKEN_OUT,
93 DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize,
94 MSInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
95
96 MSInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
97 }
98 }
99
100 MSInterfaceInfo->State.InterfaceNumber = MassStorageInterface->InterfaceNumber;
101 MSInterfaceInfo->State.IsActive = true;
102
103 return MS_ENUMERROR_NoError;
104 }
105
106 static uint8_t DCOMP_MS_Host_NextMSInterface(void* const CurrentDescriptor)
107 {
108 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
109
110 if (Header->Type == DTYPE_Interface)
111 {
112 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
113
114 if ((Interface->Class == MS_CSCP_MassStorageClass) &&
115 (Interface->SubClass == MS_CSCP_SCSITransparentSubclass) &&
116 (Interface->Protocol == MS_CSCP_BulkOnlyTransportProtocol))
117 {
118 return DESCRIPTOR_SEARCH_Found;
119 }
120 }
121
122 return DESCRIPTOR_SEARCH_NotFound;
123 }
124
125 static uint8_t DCOMP_MS_Host_NextMSInterfaceEndpoint(void* const CurrentDescriptor)
126 {
127 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
128
129 if (Header->Type == DTYPE_Endpoint)
130 {
131 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
132
133 uint8_t EndpointType = (Endpoint->Attributes & EP_TYPE_MASK);
134
135 if ((EndpointType == EP_TYPE_BULK) && (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress))))
136 {
137 return DESCRIPTOR_SEARCH_Found;
138 }
139 }
140 else if (Header->Type == DTYPE_Interface)
141 {
142 return DESCRIPTOR_SEARCH_Fail;
143 }
144
145 return DESCRIPTOR_SEARCH_NotFound;
146 }
147
148 static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
149 MS_CommandBlockWrapper_t* const SCSICommandBlock,
150 const void* const BufferPtr)
151 {
152 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
153
154 SCSICommandBlock->Signature = MS_CBW_SIGNATURE;
155 SCSICommandBlock->Tag = ++MSInterfaceInfo->State.TransactionTag;
156
157 if (MSInterfaceInfo->State.TransactionTag == 0xFFFFFFFF)
158 MSInterfaceInfo->State.TransactionTag = 1;
159
160 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
161 Pipe_Unfreeze();
162
163 if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t),
164 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
165 return ErrorCode;
166
167 Pipe_ClearOUT();
168 Pipe_WaitUntilReady();
169
170 Pipe_Freeze();
171
172 if ((BufferPtr != NULL) &&
173 ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, SCSICommandBlock, (void*)BufferPtr)) != PIPE_RWSTREAM_NoError))
174 {
175 Pipe_Freeze();
176 return ErrorCode;
177 }
178
179 return ErrorCode;
180 }
181
182 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
183 {
184 uint16_t TimeoutMSRem = MS_COMMAND_DATA_TIMEOUT_MS;
185 uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
186
187 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
188 Pipe_Unfreeze();
189
190 while (!(Pipe_IsINReceived()))
191 {
192 uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
193
194 if (CurrentFrameNumber != PreviousFrameNumber)
195 {
196 PreviousFrameNumber = CurrentFrameNumber;
197
198 if (!(TimeoutMSRem--))
199 return PIPE_RWSTREAM_Timeout;
200 }
201
202 Pipe_Freeze();
203 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
204 Pipe_Unfreeze();
205
206 if (Pipe_IsStalled())
207 {
208 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataOUTPipeNumber);
209
210 return PIPE_RWSTREAM_PipeStalled;
211 }
212
213 Pipe_Freeze();
214 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
215 Pipe_Unfreeze();
216
217 if (Pipe_IsStalled())
218 {
219 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataINPipeNumber);
220
221 return PIPE_RWSTREAM_PipeStalled;
222 }
223
224 if (USB_HostState == HOST_STATE_Unattached)
225 return PIPE_RWSTREAM_DeviceDisconnected;
226 };
227
228 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
229 Pipe_Freeze();
230
231 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
232 Pipe_Freeze();
233
234 return PIPE_RWSTREAM_NoError;
235 }
236
237 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
238 MS_CommandBlockWrapper_t* const SCSICommandBlock,
239 void* BufferPtr)
240 {
241 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
242 uint16_t BytesRem = SCSICommandBlock->DataTransferLength;
243
244 if (SCSICommandBlock->Flags & MS_COMMAND_DIR_DATA_IN)
245 {
246 if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
247 {
248 Pipe_Freeze();
249 return ErrorCode;
250 }
251
252 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
253 Pipe_Unfreeze();
254
255 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
256 return ErrorCode;
257
258 Pipe_ClearIN();
259 }
260 else
261 {
262 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
263 Pipe_Unfreeze();
264
265 if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
266 return ErrorCode;
267
268 Pipe_ClearOUT();
269
270 while (!(Pipe_IsOUTReady()))
271 {
272 if (USB_HostState == HOST_STATE_Unattached)
273 return PIPE_RWSTREAM_DeviceDisconnected;
274 }
275 }
276
277 Pipe_Freeze();
278
279 return ErrorCode;
280 }
281
282 static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
283 MS_CommandStatusWrapper_t* const SCSICommandStatus)
284 {
285 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
286
287 if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
288 return ErrorCode;
289
290 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
291 Pipe_Unfreeze();
292
293 if ((ErrorCode = Pipe_Read_Stream_LE(SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t),
294 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
295 {
296 return ErrorCode;
297 }
298
299 Pipe_ClearIN();
300 Pipe_Freeze();
301
302 if (SCSICommandStatus->Status != MS_SCSI_COMMAND_Pass)
303 ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED;
304
305 return ErrorCode;
306 }
307
308 uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
309 {
310 USB_ControlRequest = (USB_Request_Header_t)
311 {
312 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
313 .bRequest = MS_REQ_MassStorageReset,
314 .wValue = 0,
315 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
316 .wLength = 0,
317 };
318
319 Pipe_SelectPipe(PIPE_CONTROLPIPE);
320
321 return USB_Host_SendControlRequest(NULL);
322 }
323
324 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
325 uint8_t* const MaxLUNIndex)
326 {
327 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
328
329 USB_ControlRequest = (USB_Request_Header_t)
330 {
331 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
332 .bRequest = MS_REQ_GetMaxLUN,
333 .wValue = 0,
334 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
335 .wLength = 1,
336 };
337
338 Pipe_SelectPipe(PIPE_CONTROLPIPE);
339
340 if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) != HOST_SENDCONTROL_Successful)
341 {
342 *MaxLUNIndex = 0;
343 ErrorCode = HOST_SENDCONTROL_Successful;
344 }
345
346 return ErrorCode;
347 }
348
349 uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
350 const uint8_t LUNIndex,
351 SCSI_Inquiry_Response_t* const InquiryData)
352 {
353 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
354 return HOST_SENDCONTROL_DeviceDisconnected;
355
356 uint8_t ErrorCode;
357
358 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
359 {
360 .DataTransferLength = sizeof(SCSI_Inquiry_Response_t),
361 .Flags = MS_COMMAND_DIR_DATA_IN,
362 .LUN = LUNIndex,
363 .SCSICommandLength = 6,
364 .SCSICommandData =
365 {
366 SCSI_CMD_INQUIRY,
367 0x00, // Reserved
368 0x00, // Reserved
369 0x00, // Reserved
370 sizeof(SCSI_Inquiry_Response_t), // Allocation Length
371 0x00 // Unused (control)
372 }
373 };
374
375 MS_CommandStatusWrapper_t SCSICommandStatus;
376
377 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, InquiryData)) != PIPE_RWSTREAM_NoError)
378 return ErrorCode;
379
380 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
381 return ErrorCode;
382
383 return PIPE_RWSTREAM_NoError;
384 }
385
386 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
387 const uint8_t LUNIndex)
388 {
389 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
390 return HOST_SENDCONTROL_DeviceDisconnected;
391
392 uint8_t ErrorCode;
393
394 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
395 {
396 .DataTransferLength = 0,
397 .Flags = MS_COMMAND_DIR_DATA_IN,
398 .LUN = LUNIndex,
399 .SCSICommandLength = 6,
400 .SCSICommandData =
401 {
402 SCSI_CMD_TEST_UNIT_READY,
403 0x00, // Reserved
404 0x00, // Reserved
405 0x00, // Reserved
406 0x00, // Reserved
407 0x00 // Unused (control)
408 }
409 };
410
411 MS_CommandStatusWrapper_t SCSICommandStatus;
412
413 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
414 return ErrorCode;
415
416 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
417 return ErrorCode;
418
419 return PIPE_RWSTREAM_NoError;
420 }
421
422 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
423 const uint8_t LUNIndex,
424 SCSI_Capacity_t* const DeviceCapacity)
425 {
426 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
427 return HOST_SENDCONTROL_DeviceDisconnected;
428
429 uint8_t ErrorCode;
430
431 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
432 {
433 .DataTransferLength = sizeof(SCSI_Capacity_t),
434 .Flags = MS_COMMAND_DIR_DATA_IN,
435 .LUN = LUNIndex,
436 .SCSICommandLength = 10,
437 .SCSICommandData =
438 {
439 SCSI_CMD_READ_CAPACITY_10,
440 0x00, // Reserved
441 0x00, // MSB of Logical block address
442 0x00,
443 0x00,
444 0x00, // LSB of Logical block address
445 0x00, // Reserved
446 0x00, // Reserved
447 0x00, // Partial Medium Indicator
448 0x00 // Unused (control)
449 }
450 };
451
452 MS_CommandStatusWrapper_t SCSICommandStatus;
453
454 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError)
455 return ErrorCode;
456
457 SwapEndian_n(&DeviceCapacity->Blocks, sizeof(DeviceCapacity->Blocks));
458 SwapEndian_n(&DeviceCapacity->BlockSize, sizeof(DeviceCapacity->BlockSize));
459
460 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
461 return ErrorCode;
462
463 return PIPE_RWSTREAM_NoError;
464 }
465
466 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
467 const uint8_t LUNIndex,
468 SCSI_Request_Sense_Response_t* const SenseData)
469 {
470 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
471 return HOST_SENDCONTROL_DeviceDisconnected;
472
473 uint8_t ErrorCode;
474
475 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
476 {
477 .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),
478 .Flags = MS_COMMAND_DIR_DATA_IN,
479 .LUN = LUNIndex,
480 .SCSICommandLength = 6,
481 .SCSICommandData =
482 {
483 SCSI_CMD_REQUEST_SENSE,
484 0x00, // Reserved
485 0x00, // Reserved
486 0x00, // Reserved
487 sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
488 0x00 // Unused (control)
489 }
490 };
491
492 MS_CommandStatusWrapper_t SCSICommandStatus;
493
494 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, SenseData)) != PIPE_RWSTREAM_NoError)
495 return ErrorCode;
496
497 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
498 return ErrorCode;
499
500 return PIPE_RWSTREAM_NoError;
501 }
502
503 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
504 const uint8_t LUNIndex,
505 const bool PreventRemoval)
506 {
507 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
508 return HOST_SENDCONTROL_DeviceDisconnected;
509
510 uint8_t ErrorCode;
511
512 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
513 {
514 .DataTransferLength = 0,
515 .Flags = MS_COMMAND_DIR_DATA_OUT,
516 .LUN = LUNIndex,
517 .SCSICommandLength = 6,
518 .SCSICommandData =
519 {
520 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
521 0x00, // Reserved
522 0x00, // Reserved
523 PreventRemoval, // Prevent flag
524 0x00, // Reserved
525 0x00 // Unused (control)
526 }
527 };
528
529 MS_CommandStatusWrapper_t SCSICommandStatus;
530
531 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
532 return ErrorCode;
533
534 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
535 return ErrorCode;
536
537 return PIPE_RWSTREAM_NoError;
538 }
539
540 uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
541 const uint8_t LUNIndex,
542 const uint32_t BlockAddress,
543 const uint8_t Blocks,
544 const uint16_t BlockSize,
545 void* BlockBuffer)
546 {
547 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
548 return HOST_SENDCONTROL_DeviceDisconnected;
549
550 uint8_t ErrorCode;
551
552 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
553 {
554 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
555 .Flags = MS_COMMAND_DIR_DATA_IN,
556 .LUN = LUNIndex,
557 .SCSICommandLength = 10,
558 .SCSICommandData =
559 {
560 SCSI_CMD_READ_10,
561 0x00, // Unused (control bits, all off)
562 (BlockAddress >> 24), // MSB of Block Address
563 (BlockAddress >> 16),
564 (BlockAddress >> 8),
565 (BlockAddress & 0xFF), // LSB of Block Address
566 0x00, // Reserved
567 0x00, // MSB of Total Blocks to Read
568 Blocks, // LSB of Total Blocks to Read
569 0x00 // Unused (control)
570 }
571 };
572
573 MS_CommandStatusWrapper_t SCSICommandStatus;
574
575 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
576 return ErrorCode;
577
578 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
579 return ErrorCode;
580
581 return PIPE_RWSTREAM_NoError;
582 }
583
584 uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
585 const uint8_t LUNIndex,
586 const uint32_t BlockAddress,
587 const uint8_t Blocks,
588 const uint16_t BlockSize,
589 const void* BlockBuffer)
590 {
591 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
592 return HOST_SENDCONTROL_DeviceDisconnected;
593
594 uint8_t ErrorCode;
595
596 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
597 {
598 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
599 .Flags = MS_COMMAND_DIR_DATA_OUT,
600 .LUN = LUNIndex,
601 .SCSICommandLength = 10,
602 .SCSICommandData =
603 {
604 SCSI_CMD_WRITE_10,
605 0x00, // Unused (control bits, all off)
606 (BlockAddress >> 24), // MSB of Block Address
607 (BlockAddress >> 16),
608 (BlockAddress >> 8),
609 (BlockAddress & 0xFF), // LSB of Block Address
610 0x00, // Reserved
611 0x00, // MSB of Total Blocks to Write
612 Blocks, // LSB of Total Blocks to Write
613 0x00 // Unused (control)
614 }
615 };
616
617 MS_CommandStatusWrapper_t SCSICommandStatus;
618
619 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
620 return ErrorCode;
621
622 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
623 return ErrorCode;
624
625 return PIPE_RWSTREAM_NoError;
626 }
627
628 #endif
629