Fix nasty bug in USBInterrupt.c which would cause a lockup if the control endpoint...
[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_MS_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_MS_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_MS_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_MS_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->Signature = CBW_SIGNATURE;
138 SCSICommandBlock->Tag = ++MSInterfaceInfo->State.TransactionTag;
139
140 if (MSInterfaceInfo->State.TransactionTag == 0xFFFFFFFF)
141 MSInterfaceInfo->State.TransactionTag = 1;
142
143 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
144 Pipe_Unfreeze();
145
146 if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t),
147 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
148 return ErrorCode;
149
150 Pipe_ClearOUT();
151 Pipe_WaitUntilReady();
152
153 Pipe_Freeze();
154
155 if ((BufferPtr != NULL) &&
156 ((ErrorCode = MS_Host_SendReceiveData(MSInterfaceInfo, SCSICommandBlock, (void*)BufferPtr)) != PIPE_RWSTREAM_NoError))
157 {
158 Pipe_Freeze();
159 return ErrorCode;
160 }
161
162 return ErrorCode;
163 }
164
165 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
166 {
167 uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
168
169 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
170 Pipe_Unfreeze();
171
172 while (!(Pipe_IsINReceived()))
173 {
174 if (USB_INT_HasOccurred(USB_INT_HSOFI))
175 {
176 USB_INT_Clear(USB_INT_HSOFI);
177 TimeoutMSRem--;
178
179 if (!(TimeoutMSRem))
180 return PIPE_RWSTREAM_Timeout;
181 }
182
183 Pipe_Freeze();
184 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
185 Pipe_Unfreeze();
186
187 if (Pipe_IsStalled())
188 {
189 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataOUTPipeNumber);
190
191 return PIPE_RWSTREAM_PipeStalled;
192 }
193
194 Pipe_Freeze();
195 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
196 Pipe_Unfreeze();
197
198 if (Pipe_IsStalled())
199 {
200 USB_Host_ClearPipeStall(MSInterfaceInfo->Config.DataINPipeNumber);
201
202 return PIPE_RWSTREAM_PipeStalled;
203 }
204
205 if (USB_HostState == HOST_STATE_Unattached)
206 return PIPE_RWSTREAM_DeviceDisconnected;
207 };
208
209 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
210 Pipe_Freeze();
211
212 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
213 Pipe_Freeze();
214
215 return PIPE_RWSTREAM_NoError;
216 }
217
218 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
219 MS_CommandBlockWrapper_t* const SCSICommandBlock, void* BufferPtr)
220 {
221 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
222 uint16_t BytesRem = SCSICommandBlock->DataTransferLength;
223
224 if (SCSICommandBlock->Flags & COMMAND_DIRECTION_DATA_IN)
225 {
226 if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
227 {
228 Pipe_Freeze();
229 return ErrorCode;
230 }
231
232 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
233 Pipe_Unfreeze();
234
235 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
236 return ErrorCode;
237
238 Pipe_ClearIN();
239 }
240 else
241 {
242 Pipe_SelectPipe(MSInterfaceInfo->Config.DataOUTPipeNumber);
243 Pipe_Unfreeze();
244
245 if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
246 return ErrorCode;
247
248 Pipe_ClearOUT();
249
250 while (!(Pipe_IsOUTReady()))
251 {
252 if (USB_HostState == HOST_STATE_Unattached)
253 return PIPE_RWSTREAM_DeviceDisconnected;
254 }
255 }
256
257 Pipe_Freeze();
258
259 return ErrorCode;
260 }
261
262 static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo,
263 MS_CommandStatusWrapper_t* const SCSICommandStatus)
264 {
265 uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
266
267 if ((ErrorCode = MS_Host_WaitForDataReceived(MSInterfaceInfo)) != PIPE_RWSTREAM_NoError)
268 return ErrorCode;
269
270 Pipe_SelectPipe(MSInterfaceInfo->Config.DataINPipeNumber);
271 Pipe_Unfreeze();
272
273 if ((ErrorCode = Pipe_Read_Stream_LE(SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t),
274 NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
275 {
276 return ErrorCode;
277 }
278
279 Pipe_ClearIN();
280 Pipe_Freeze();
281
282 if (SCSICommandStatus->Status != SCSI_Command_Pass)
283 ErrorCode = MS_ERROR_LOGICAL_CMD_FAILED;
284
285 return ErrorCode;
286 }
287
288 uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo)
289 {
290 USB_ControlRequest = (USB_Request_Header_t)
291 {
292 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
293 .bRequest = REQ_MassStorageReset,
294 .wValue = 0,
295 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
296 .wLength = 0,
297 };
298
299 Pipe_SelectPipe(PIPE_CONTROLPIPE);
300
301 return USB_Host_SendControlRequest(NULL);
302 }
303
304 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, uint8_t* const MaxLUNIndex)
305 {
306 uint8_t ErrorCode = HOST_SENDCONTROL_Successful;
307
308 USB_ControlRequest = (USB_Request_Header_t)
309 {
310 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
311 .bRequest = REQ_GetMaxLUN,
312 .wValue = 0,
313 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
314 .wLength = 1,
315 };
316
317 Pipe_SelectPipe(PIPE_CONTROLPIPE);
318
319 if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) != HOST_SENDCONTROL_Successful)
320 {
321 *MaxLUNIndex = 0;
322 ErrorCode = HOST_SENDCONTROL_Successful;
323 }
324
325 return ErrorCode;
326 }
327
328 uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
329 SCSI_Inquiry_Response_t* const InquiryData)
330 {
331 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
332 return HOST_SENDCONTROL_DeviceDisconnected;
333
334 uint8_t ErrorCode;
335
336 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
337 {
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 .DataTransferLength = 0,
374 .Flags = COMMAND_DIRECTION_DATA_IN,
375 .LUN = LUNIndex,
376 .SCSICommandLength = 6,
377 .SCSICommandData =
378 {
379 SCSI_CMD_TEST_UNIT_READY,
380 0x00, // Reserved
381 0x00, // Reserved
382 0x00, // Reserved
383 0x00, // Reserved
384 0x00 // Unused (control)
385 }
386 };
387
388 MS_CommandStatusWrapper_t SCSICommandStatus;
389
390 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
391 return ErrorCode;
392
393 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
394 return ErrorCode;
395
396 return PIPE_RWSTREAM_NoError;
397 }
398
399 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
400 SCSI_Capacity_t* const DeviceCapacity)
401 {
402 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
403 return HOST_SENDCONTROL_DeviceDisconnected;
404
405 uint8_t ErrorCode;
406
407 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
408 {
409 .DataTransferLength = sizeof(SCSI_Capacity_t),
410 .Flags = COMMAND_DIRECTION_DATA_IN,
411 .LUN = LUNIndex,
412 .SCSICommandLength = 10,
413 .SCSICommandData =
414 {
415 SCSI_CMD_READ_CAPACITY_10,
416 0x00, // Reserved
417 0x00, // MSB of Logical block address
418 0x00,
419 0x00,
420 0x00, // LSB of Logical block address
421 0x00, // Reserved
422 0x00, // Reserved
423 0x00, // Partial Medium Indicator
424 0x00 // Unused (control)
425 }
426 };
427
428 MS_CommandStatusWrapper_t SCSICommandStatus;
429
430 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError)
431 return ErrorCode;
432
433 SwapEndian_n(&DeviceCapacity->Blocks, sizeof(DeviceCapacity->Blocks));
434 SwapEndian_n(&DeviceCapacity->BlockSize, sizeof(DeviceCapacity->BlockSize));
435
436 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
437 return ErrorCode;
438
439 return PIPE_RWSTREAM_NoError;
440 }
441
442 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
443 SCSI_Request_Sense_Response_t* const SenseData)
444 {
445 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
446 return HOST_SENDCONTROL_DeviceDisconnected;
447
448 uint8_t ErrorCode;
449
450 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
451 {
452 .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),
453 .Flags = COMMAND_DIRECTION_DATA_IN,
454 .LUN = LUNIndex,
455 .SCSICommandLength = 6,
456 .SCSICommandData =
457 {
458 SCSI_CMD_REQUEST_SENSE,
459 0x00, // Reserved
460 0x00, // Reserved
461 0x00, // Reserved
462 sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
463 0x00 // Unused (control)
464 }
465 };
466
467 MS_CommandStatusWrapper_t SCSICommandStatus;
468
469 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, SenseData)) != PIPE_RWSTREAM_NoError)
470 return ErrorCode;
471
472 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
473 return ErrorCode;
474
475 return PIPE_RWSTREAM_NoError;
476 }
477
478 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex,
479 const bool PreventRemoval)
480 {
481 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
482 return HOST_SENDCONTROL_DeviceDisconnected;
483
484 uint8_t ErrorCode;
485
486 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
487 {
488 .DataTransferLength = 0,
489 .Flags = COMMAND_DIRECTION_DATA_OUT,
490 .LUN = LUNIndex,
491 .SCSICommandLength = 6,
492 .SCSICommandData =
493 {
494 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
495 0x00, // Reserved
496 0x00, // Reserved
497 PreventRemoval, // Prevent flag
498 0x00, // Reserved
499 0x00 // Unused (control)
500 }
501 };
502
503 MS_CommandStatusWrapper_t SCSICommandStatus;
504
505 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
506 return ErrorCode;
507
508 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
509 return ErrorCode;
510
511 return PIPE_RWSTREAM_NoError;
512 }
513
514 uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex, const uint32_t BlockAddress,
515 const uint8_t Blocks, const uint16_t BlockSize, void* BlockBuffer)
516 {
517 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
518 return HOST_SENDCONTROL_DeviceDisconnected;
519
520 uint8_t ErrorCode;
521
522 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
523 {
524 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
525 .Flags = COMMAND_DIRECTION_DATA_IN,
526 .LUN = LUNIndex,
527 .SCSICommandLength = 10,
528 .SCSICommandData =
529 {
530 SCSI_CMD_READ_10,
531 0x00, // Unused (control bits, all off)
532 (BlockAddress >> 24), // MSB of Block Address
533 (BlockAddress >> 16),
534 (BlockAddress >> 8),
535 (BlockAddress & 0xFF), // LSB of Block Address
536 0x00, // Unused (reserved)
537 0x00, // MSB of Total Blocks to Read
538 Blocks, // LSB of Total Blocks to Read
539 0x00 // Unused (control)
540 }
541 };
542
543 MS_CommandStatusWrapper_t SCSICommandStatus;
544
545 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
546 return ErrorCode;
547
548 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
549 return ErrorCode;
550
551 return PIPE_RWSTREAM_NoError;
552 }
553
554 uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* const MSInterfaceInfo, const uint8_t LUNIndex, const uint32_t BlockAddress,
555 const uint8_t Blocks, const uint16_t BlockSize, const void* BlockBuffer)
556 {
557 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
558 return HOST_SENDCONTROL_DeviceDisconnected;
559
560 uint8_t ErrorCode;
561
562 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
563 {
564 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
565 .Flags = COMMAND_DIRECTION_DATA_OUT,
566 .LUN = LUNIndex,
567 .SCSICommandLength = 10,
568 .SCSICommandData =
569 {
570 SCSI_CMD_WRITE_10,
571 0x00, // Unused (control bits, all off)
572 (BlockAddress >> 24), // MSB of Block Address
573 (BlockAddress >> 16),
574 (BlockAddress >> 8),
575 (BlockAddress & 0xFF), // LSB of Block Address
576 0x00, // Unused (reserved)
577 0x00, // MSB of Total Blocks to Write
578 Blocks, // LSB of Total Blocks to Write
579 0x00 // Unused (control)
580 }
581 };
582
583 MS_CommandStatusWrapper_t SCSICommandStatus;
584
585 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
586 return ErrorCode;
587
588 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
589 return ErrorCode;
590
591 return PIPE_RWSTREAM_NoError;
592 }
593
594 #endif