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