c3390a59ca505b0644aab0849f32f315e6474fd6
[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.fourwalledcubicle.com
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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
33
34 #define INCLUDE_FROM_MS_CLASS_HOST_C
35 #include "MassStorage.h"
36
37 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, uint16_t ConfigDescriptorSize,
38 void* DeviceConfigDescriptor)
39 {
40 uint8_t FoundEndpoints = 0;
41
42 memset(&MSInterfaceInfo->State, 0x00, sizeof(MSInterfaceInfo->State));
43
44 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor) != DTYPE_Configuration)
45 return MS_ENUMERROR_InvalidConfigDescriptor;
46
47 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
48 DComp_NextMSInterface) != DESCRIPTOR_SEARCH_COMP_Found)
49 {
50 return MS_ENUMERROR_NoMSInterfaceFound;
51 }
52
53 MSInterfaceInfo->State.InterfaceNumber = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Interface_t)->InterfaceNumber;
54
55 while (FoundEndpoints != (MS_FOUND_DATAPIPE_IN | MS_FOUND_DATAPIPE_OUT))
56 {
57 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &DeviceConfigDescriptor,
58 DComp_NextMSInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
59 {
60 return MS_ENUMERROR_EndpointsNotFound;
61 }
62
63 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(DeviceConfigDescriptor, USB_Descriptor_Endpoint_t);
64
65 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
66 {
67 Pipe_ConfigurePipe(MSInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_IN,
68 EndpointData->EndpointAddress, EndpointData->EndpointSize,
69 MSInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
70 MSInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
71
72 FoundEndpoints |= MS_FOUND_DATAPIPE_IN;
73 }
74 else
75 {
76 Pipe_ConfigurePipe(MSInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_BULK, PIPE_TOKEN_OUT,
77 EndpointData->EndpointAddress, EndpointData->EndpointSize,
78 MSInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
79 MSInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
80
81 FoundEndpoints |= MS_FOUND_DATAPIPE_OUT;
82 }
83 }
84
85 MSInterfaceInfo->State.IsActive = true;
86 return MS_ENUMERROR_NoError;
87 }
88
89 static uint8_t DComp_NextMSInterface(void* const CurrentDescriptor)
90 {
91 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
92 {
93 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
94 USB_Descriptor_Interface_t);
95
96 if ((CurrentInterface->Class == MASS_STORE_CLASS) &&
97 (CurrentInterface->SubClass == MASS_STORE_SUBCLASS) &&
98 (CurrentInterface->Protocol == MASS_STORE_PROTOCOL))
99 {
100 return DESCRIPTOR_SEARCH_Found;
101 }
102 }
103
104 return DESCRIPTOR_SEARCH_NotFound;
105 }
106
107 static uint8_t DComp_NextMSInterfaceEndpoint(void* const CurrentDescriptor)
108 {
109 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
110 {
111 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
112 USB_Descriptor_Endpoint_t);
113
114 uint8_t EndpointType = (CurrentEndpoint->Attributes & EP_TYPE_MASK);
115
116 if ((EndpointType == EP_TYPE_BULK) &&
117 (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress))))
118 {
119 return DESCRIPTOR_SEARCH_Found;
120 }
121 }
122 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
123 {
124 return DESCRIPTOR_SEARCH_Fail;
125 }
126
127 return DESCRIPTOR_SEARCH_NotFound;
128 }
129
130 void MS_Host_USBTask(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
131 {
132 (void)MSInterfaceInfo;
133 }
134
135 static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, MS_CommandBlockWrapper_t* const SCSICommandBlock,
136 void* BufferPtr)
137 {
138 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
139
140 SCSICommandBlock->Tag = ++MSInterfaceInfo->State.TransactionTag;
141
142 if (MSInterfaceInfo->State.TransactionTag == 0xFFFFFFFF)
143 MSInterfaceInfo->State.TransactionTag = 1;
144
145 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
146 Pipe_Unfreeze();
147
148 if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t),
149 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
150 return ErrorCode;
151
152 Pipe_ClearOUT();
153 Pipe_WaitUntilReady();
154
155 Pipe_Freeze();
156
157 if ((BufferPtr != NULL) &&
158 ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, SCSICommandBlock, BufferPtr)) != PIPE_RWSTREAM_NoError))
159 {
160 Pipe_Freeze();
161 return ErrorCode;
162 }
163
164 return ErrorCode;
165 }
166
167 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
168 {
169 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
170
171 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
172 Pipe_Unfreeze();
173
174 while (!(Pipe_IsINReceived()))
175 {
176 if (USB_INT_HasOccurred(USB_INT_HSOFI))
177 {
178 USB_INT_Clear(USB_INT_HSOFI);
179 TimeoutMSRem--;
180
181 if (!(TimeoutMSRem))
182 return PIPE_RWSTREAM_Timeout;
183 }
184
185 Pipe_Freeze();
186 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
187 Pipe_Unfreeze();
188
189 if (Pipe_IsStalled())
190 {
191 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataOUTPipeNumber);
192
193 return PIPE_RWSTREAM_PipeStalled;
194 }
195
196 Pipe_Freeze();
197 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
198 Pipe_Unfreeze();
199
200 if (Pipe_IsStalled())
201 {
202 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataINPipeNumber);
203
204 return PIPE_RWSTREAM_PipeStalled;
205 }
206
207 if (USB_HostState == HOST_STATE_Unattached)
208 return PIPE_RWSTREAM_DeviceDisconnected;
209 };
210
211 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
212 Pipe_Freeze();
213
214 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
215 Pipe_Freeze();
216
217 return PIPE_RWSTREAM_NoError;
218 }
219
220 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
221 MS_CommandBlockWrapper_t* const SCSICommandBlock, void* BufferPtr)
222 {
223 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
224 uint16_t BytesRem = SCSICommandBlock->DataTransferLength;
225
226 if (SCSICommandBlock->Flags & COMMAND_DIRECTION_DATA_IN)
227 {
228 if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
229 {
230 Pipe_Freeze();
231 return ErrorCode;
232 }
233
234 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
235 Pipe_Unfreeze();
236
237 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
238 return ErrorCode;
239
240 Pipe_ClearIN();
241 }
242 else
243 {
244 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
245 Pipe_Unfreeze();
246
247 if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
248 return ErrorCode;
249
250 Pipe_ClearOUT();
251
252 while (!(Pipe_IsOUTReady()))
253 {
254 if (USB_HostState == HOST_STATE_Unattached)
255 return PIPE_RWSTREAM_DeviceDisconnected;
256 }
257 }
258
259 Pipe_Freeze();
260
261 return ErrorCode;
262 }
263
264 static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
265 MS_CommandStatusWrapper_t* const SCSICommandStatus)
266 {
267 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
268
269 if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
270 return ErrorCode;
271
272 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
273 Pipe_Unfreeze();
274
275 if ((ErrorCode = Pipe_Read_Stream_LE(SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t),
276 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
277 {
278 return ErrorCode;
279 }
280
281 Pipe_ClearIN();
282 Pipe_Freeze();
283
284 if (SCSICommandStatus->Status != SCSI_Command_Pass)
285 ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED;
286
287 return ErrorCode;
288 }
289
290 uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
291 {
292 USB_ControlRequest = (USB_Request_Header_t)
293 {
294 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
295 .bRequest = REQ_MassStorageReset,
296 .wValue = 0,
297 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
298 .wLength = 0,
299 };
300
301 Pipe_SelectPipe(PIPE_CONTROLPIPE);
302
303 return USB_Host_SendControlRequest(NULL);
304 }
305
306 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, uint8_t* const MaxLUNIndex)
307 {
308 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
309
310 USB_ControlRequest = (USB_Request_Header_t)
311 {
312 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
313 .bRequest = REQ_GetMaxLUN,
314 .wValue = 0,
315 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
316 .wLength = 1,
317 };
318
319 Pipe_SelectPipe(PIPE_CONTROLPIPE);
320
321 if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) != HOST_SENDCONTROL_Successful)
322 {
323 *MaxLUNIndex = 0;
324 ErrorCode = HOST_SENDCONTROL_Successful;
325 }
326
327 return ErrorCode;
328 }
329
330 uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
331 SCSI_Inquiry_Response_t* const InquiryData)
332 {
333 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
334 return HOST_SENDCONTROL_DeviceDisconnected;
335
336 uint8_t ErrorCode;
337
338 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
339 {
340 .Signature = CBW_SIGNATURE,
341 .DataTransferLength = sizeof(SCSI_Inquiry_Response_t),
342 .Flags = COMMAND_DIRECTION_DATA_IN,
343 .LUN = LUNIndex,
344 .SCSICommandLength = 6,
345 .SCSICommandData =
346 {
347 SCSI_CMD_INQUIRY,
348 0x00, // Reserved
349 0x00, // Reserved
350 0x00, // Reserved
351 sizeof(SCSI_Inquiry_Response_t), // Allocation Length
352 0x00 // Unused (control)
353 }
354 };
355
356 MS_CommandStatusWrapper_t SCSICommandStatus;
357
358 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, InquiryData)) != PIPE_RWSTREAM_NoError)
359 return ErrorCode;
360
361 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
362 return ErrorCode;
363
364 return PIPE_RWSTREAM_NoError;
365 }
366
367 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex)
368 {
369 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
370 return HOST_SENDCONTROL_DeviceDisconnected;
371
372 uint8_t ErrorCode;
373
374 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
375 {
376 .Signature = CBW_SIGNATURE,
377 .DataTransferLength = 0,
378 .Flags = COMMAND_DIRECTION_DATA_IN,
379 .LUN = LUNIndex,
380 .SCSICommandLength = 6,
381 .SCSICommandData =
382 {
383 SCSI_CMD_TEST_UNIT_READY,
384 0x00, // Reserved
385 0x00, // Reserved
386 0x00, // Reserved
387 0x00, // Reserved
388 0x00 // Unused (control)
389 }
390 };
391
392 MS_CommandStatusWrapper_t SCSICommandStatus;
393
394 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
395 return ErrorCode;
396
397 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
398 return ErrorCode;
399
400 return PIPE_RWSTREAM_NoError;
401 }
402
403 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
404 SCSI_Capacity_t* const DeviceCapacity)
405 {
406 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
407 return HOST_SENDCONTROL_DeviceDisconnected;
408
409 uint8_t ErrorCode;
410
411 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
412 {
413 .Signature = CBW_SIGNATURE,
414 .DataTransferLength = sizeof(SCSI_Capacity_t),
415 .Flags = COMMAND_DIRECTION_DATA_IN,
416 .LUN = LUNIndex,
417 .SCSICommandLength = 10,
418 .SCSICommandData =
419 {
420 SCSI_CMD_READ_CAPACITY_10,
421 0x00, // Reserved
422 0x00, // MSB of Logical block address
423 0x00,
424 0x00,
425 0x00, // LSB of Logical block address
426 0x00, // Reserved
427 0x00, // Reserved
428 0x00, // Partial Medium Indicator
429 0x00 // Unused (control)
430 }
431 };
432
433 MS_CommandStatusWrapper_t SCSICommandStatus;
434
435 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError)
436 return ErrorCode;
437
438 DeviceCapacity->Blocks = SwapEndian_32(DeviceCapacity->Blocks);
439 DeviceCapacity->BlockSize = SwapEndian_32(DeviceCapacity->BlockSize);
440
441 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
442 return ErrorCode;
443
444 return PIPE_RWSTREAM_NoError;
445 }
446
447 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
448 SCSI_Request_Sense_Response_t* const SenseData)
449 {
450 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
451 return HOST_SENDCONTROL_DeviceDisconnected;
452
453 uint8_t ErrorCode;
454
455 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
456 {
457 .Signature = CBW_SIGNATURE,
458 .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),
459 .Flags = COMMAND_DIRECTION_DATA_IN,
460 .LUN = LUNIndex,
461 .SCSICommandLength = 6,
462 .SCSICommandData =
463 {
464 SCSI_CMD_REQUEST_SENSE,
465 0x00, // Reserved
466 0x00, // Reserved
467 0x00, // Reserved
468 sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
469 0x00 // Unused (control)
470 }
471 };
472
473 MS_CommandStatusWrapper_t SCSICommandStatus;
474
475 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, SenseData)) != PIPE_RWSTREAM_NoError)
476 return ErrorCode;
477
478 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
479 return ErrorCode;
480
481 return PIPE_RWSTREAM_NoError;
482 }
483
484 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
485 const bool PreventRemoval)
486 {
487 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
488 return HOST_SENDCONTROL_DeviceDisconnected;
489
490 uint8_t ErrorCode;
491
492 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
493 {
494 .Signature = CBW_SIGNATURE,
495 .DataTransferLength = 0,
496 .Flags = COMMAND_DIRECTION_DATA_OUT,
497 .LUN = LUNIndex,
498 .SCSICommandLength = 6,
499 .SCSICommandData =
500 {
501 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
502 0x00, // Reserved
503 0x00, // Reserved
504 PreventRemoval, // Prevent flag
505 0x00, // Reserved
506 0x00 // Unused (control)
507 }
508 };
509
510 MS_CommandStatusWrapper_t SCSICommandStatus;
511
512 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
513 return ErrorCode;
514
515 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
516 return ErrorCode;
517
518 return PIPE_RWSTREAM_NoError;
519 }
520
521 uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex, const uint32_t BlockAddress,
522 const uint8_t Blocks, const uint16_t BlockSize, void* BlockBuffer)
523 {
524 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
525 return HOST_SENDCONTROL_DeviceDisconnected;
526
527 uint8_t ErrorCode;
528
529 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
530 {
531 .Signature = CBW_SIGNATURE,
532 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
533 .Flags = COMMAND_DIRECTION_DATA_IN,
534 .LUN = LUNIndex,
535 .SCSICommandLength = 10,
536 .SCSICommandData =
537 {
538 SCSI_CMD_READ_10,
539 0x00, // Unused (control bits, all off)
540 (BlockAddress >> 24), // MSB of Block Address
541 (BlockAddress >> 16),
542 (BlockAddress >> 8),
543 (BlockAddress & 0xFF), // LSB of Block Address
544 0x00, // Unused (reserved)
545 0x00, // MSB of Total Blocks to Read
546 Blocks, // LSB of Total Blocks to Read
547 0x00 // Unused (control)
548 }
549 };
550
551 MS_CommandStatusWrapper_t SCSICommandStatus;
552
553 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
554 return ErrorCode;
555
556 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
557 return ErrorCode;
558
559 return PIPE_RWSTREAM_NoError;
560 }
561
562 uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex, const uint32_t BlockAddress,
563 const uint8_t Blocks, const uint16_t BlockSize, void* BlockBuffer)
564 {
565 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
566 return HOST_SENDCONTROL_DeviceDisconnected;
567
568 uint8_t ErrorCode;
569
570 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
571 {
572 .Signature = CBW_SIGNATURE,
573 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
574 .Flags = COMMAND_DIRECTION_DATA_OUT,
575 .LUN = LUNIndex,
576 .SCSICommandLength = 10,
577 .SCSICommandData =
578 {
579 SCSI_CMD_WRITE_10,
580 0x00, // Unused (control bits, all off)
581 (BlockAddress >> 24), // MSB of Block Address
582 (BlockAddress >> 16),
583 (BlockAddress >> 8),
584 (BlockAddress & 0xFF), // LSB of Block Address
585 0x00, // Unused (reserved)
586 0x00, // MSB of Total Blocks to Write
587 Blocks, // LSB of Total Blocks to Write
588 0x00 // Unused (control)
589 }
590 };
591
592 MS_CommandStatusWrapper_t SCSICommandStatus;
593
594 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
595 return ErrorCode;
596
597 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
598 return ErrorCode;
599
600 return PIPE_RWSTREAM_NoError;
601 }
602
603 #endif