Prevent the CDC Device Class driver from sending empty IN packets on every service...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / MassStorage.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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* MSInterfaceInfo, uint16_t ConfigDescriptorLength,
38 uint8_t* 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(&ConfigDescriptorLength, &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(&ConfigDescriptorLength, &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 PIPE_BANK_DOUBLE);
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 PIPE_BANK_DOUBLE);
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* 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* 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* MSInterfaceInfo)
131 {
132
133 }
134
135 static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, MS_CommandBlockWrapper_t* 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* 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* MSInterfaceInfo,
221 MS_CommandBlockWrapper_t* 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* MSInterfaceInfo,
265 MS_CommandStatusWrapper_t* 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 return ErrorCode;
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* MSInterfaceInfo)
289 {
290 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
291 return HOST_SENDCONTROL_DeviceDisconnect;
292
293 USB_ControlRequest = (USB_Request_Header_t)
294 {
295 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
296 .bRequest = REQ_MassStorageReset,
297 .wValue = 0,
298 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
299 .wLength = 0,
300 };
301
302 Pipe_SelectPipe(PIPE_CONTROLPIPE);
303
304 return USB_Host_SendControlRequest(NULL);
305 }
306
307 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t* MaxLUNIndex)
308 {
309 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
310 return HOST_SENDCONTROL_DeviceDisconnect;
311
312 uint8_t ErrorCode;
313
314 USB_ControlRequest = (USB_Request_Header_t)
315 {
316 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
317 .bRequest = REQ_GetMaxLUN,
318 .wValue = 0,
319 .wIndex = MSInterfaceInfo->State.InterfaceNumber,
320 .wLength = 1,
321 };
322
323 Pipe_SelectPipe(PIPE_CONTROLPIPE);
324
325 if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)
326 {
327 Pipe_ClearStall();
328
329 *MaxLUNIndex = 0;
330 }
331
332 return HOST_SENDCONTROL_SetupStalled;
333 }
334
335 uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, SCSI_Inquiry_Response_t* InquiryData)
336 {
337 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
338 return HOST_SENDCONTROL_DeviceDisconnect;
339
340 uint8_t ErrorCode;
341
342 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
343 {
344 .Signature = CBW_SIGNATURE,
345 .DataTransferLength = sizeof(SCSI_Inquiry_Response_t),
346 .Flags = COMMAND_DIRECTION_DATA_IN,
347 .LUN = LUNIndex,
348 .SCSICommandLength = 6,
349 .SCSICommandData =
350 {
351 SCSI_CMD_INQUIRY,
352 0x00, // Reserved
353 0x00, // Reserved
354 0x00, // Reserved
355 sizeof(SCSI_Inquiry_Response_t), // Allocation Length
356 0x00 // Unused (control)
357 }
358 };
359
360 MS_CommandStatusWrapper_t SCSICommandStatus;
361
362 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, InquiryData)) != PIPE_RWSTREAM_NoError)
363 return ErrorCode;
364
365 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
366 return ErrorCode;
367
368 return PIPE_RWSTREAM_NoError;
369 }
370
371 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex)
372 {
373 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
374 return HOST_SENDCONTROL_DeviceDisconnect;
375
376 uint8_t ErrorCode;
377
378 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
379 {
380 .Signature = CBW_SIGNATURE,
381 .DataTransferLength = 0,
382 .Flags = COMMAND_DIRECTION_DATA_IN,
383 .LUN = LUNIndex,
384 .SCSICommandLength = 6,
385 .SCSICommandData =
386 {
387 SCSI_CMD_TEST_UNIT_READY,
388 0x00, // Reserved
389 0x00, // Reserved
390 0x00, // Reserved
391 0x00, // Reserved
392 0x00 // Unused (control)
393 }
394 };
395
396 MS_CommandStatusWrapper_t SCSICommandStatus;
397
398 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
399 return ErrorCode;
400
401 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
402 return ErrorCode;
403
404 return PIPE_RWSTREAM_NoError;
405 }
406
407 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,
408 SCSI_Capacity_t* DeviceCapacity)
409 {
410 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
411 return HOST_SENDCONTROL_DeviceDisconnect;
412
413 uint8_t ErrorCode;
414
415 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
416 {
417 .Signature = CBW_SIGNATURE,
418 .DataTransferLength = sizeof(SCSI_Capacity_t),
419 .Flags = COMMAND_DIRECTION_DATA_IN,
420 .LUN = LUNIndex,
421 .SCSICommandLength = 10,
422 .SCSICommandData =
423 {
424 SCSI_CMD_READ_CAPACITY_10,
425 0x00, // Reserved
426 0x00, // MSB of Logical block address
427 0x00,
428 0x00,
429 0x00, // LSB of Logical block address
430 0x00, // Reserved
431 0x00, // Reserved
432 0x00, // Partial Medium Indicator
433 0x00 // Unused (control)
434 }
435 };
436
437 MS_CommandStatusWrapper_t SCSICommandStatus;
438
439 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, DeviceCapacity)) != PIPE_RWSTREAM_NoError)
440 return ErrorCode;
441
442 DeviceCapacity->Blocks = SwapEndian_32(DeviceCapacity->Blocks);
443 DeviceCapacity->BlockSize = SwapEndian_32(DeviceCapacity->BlockSize);
444
445 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
446 return ErrorCode;
447
448 return PIPE_RWSTREAM_NoError;
449 }
450
451 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex,
452 SCSI_Request_Sense_Response_t* SenseData)
453 {
454 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
455 return HOST_SENDCONTROL_DeviceDisconnect;
456
457 uint8_t ErrorCode;
458
459 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
460 {
461 .Signature = CBW_SIGNATURE,
462 .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),
463 .Flags = COMMAND_DIRECTION_DATA_IN,
464 .LUN = LUNIndex,
465 .SCSICommandLength = 6,
466 .SCSICommandData =
467 {
468 SCSI_CMD_REQUEST_SENSE,
469 0x00, // Reserved
470 0x00, // Reserved
471 0x00, // Reserved
472 sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
473 0x00 // Unused (control)
474 }
475 };
476
477 MS_CommandStatusWrapper_t SCSICommandStatus;
478
479 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, SenseData)) != PIPE_RWSTREAM_NoError)
480 return ErrorCode;
481
482 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
483 return ErrorCode;
484
485 return PIPE_RWSTREAM_NoError;
486 }
487
488 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, bool PreventRemoval)
489 {
490 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
491 return HOST_SENDCONTROL_DeviceDisconnect;
492
493 uint8_t ErrorCode;
494
495 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
496 {
497 .Signature = CBW_SIGNATURE,
498 .DataTransferLength = 0,
499 .Flags = COMMAND_DIRECTION_DATA_OUT,
500 .LUN = LUNIndex,
501 .SCSICommandLength = 6,
502 .SCSICommandData =
503 {
504 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
505 0x00, // Reserved
506 0x00, // Reserved
507 PreventRemoval, // Prevent flag
508 0x00, // Reserved
509 0x00 // Unused (control)
510 }
511 };
512
513 MS_CommandStatusWrapper_t SCSICommandStatus;
514
515 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, NULL)) != PIPE_RWSTREAM_NoError)
516 return ErrorCode;
517
518 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
519 return ErrorCode;
520
521 return PIPE_RWSTREAM_NoError;
522 }
523
524 uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddress,
525 uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer)
526 {
527 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
528 return HOST_SENDCONTROL_DeviceDisconnect;
529
530 uint8_t ErrorCode;
531
532 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
533 {
534 .Signature = CBW_SIGNATURE,
535 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
536 .Flags = COMMAND_DIRECTION_DATA_IN,
537 .LUN = LUNIndex,
538 .SCSICommandLength = 10,
539 .SCSICommandData =
540 {
541 SCSI_CMD_READ_10,
542 0x00, // Unused (control bits, all off)
543 (BlockAddress >> 24), // MSB of Block Address
544 (BlockAddress >> 16),
545 (BlockAddress >> 8),
546 (BlockAddress & 0xFF), // LSB of Block Address
547 0x00, // Unused (reserved)
548 0x00, // MSB of Total Blocks to Read
549 Blocks, // LSB of Total Blocks to Read
550 0x00 // Unused (control)
551 }
552 };
553
554 MS_CommandStatusWrapper_t SCSICommandStatus;
555
556 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
557 return ErrorCode;
558
559 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
560 return ErrorCode;
561
562 return PIPE_RWSTREAM_NoError;
563 }
564
565 uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t* MSInterfaceInfo, uint8_t LUNIndex, uint32_t BlockAddress,
566 uint8_t Blocks, uint16_t BlockSize, void* BlockBuffer)
567 {
568 if ((USB_HostState != HOST_STATE_Configured) || !(MSInterfaceInfo->State.IsActive))
569 return HOST_SENDCONTROL_DeviceDisconnect;
570
571 uint8_t ErrorCode;
572
573 MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
574 {
575 .Signature = CBW_SIGNATURE,
576 .DataTransferLength = ((uint32_t)Blocks * BlockSize),
577 .Flags = COMMAND_DIRECTION_DATA_OUT,
578 .LUN = LUNIndex,
579 .SCSICommandLength = 10,
580 .SCSICommandData =
581 {
582 SCSI_CMD_WRITE_10,
583 0x00, // Unused (control bits, all off)
584 (BlockAddress >> 24), // MSB of Block Address
585 (BlockAddress >> 16),
586 (BlockAddress >> 8),
587 (BlockAddress & 0xFF), // LSB of Block Address
588 0x00, // Unused (reserved)
589 0x00, // MSB of Total Blocks to Write
590 Blocks, // LSB of Total Blocks to Write
591 0x00 // Unused (control)
592 }
593 };
594
595 MS_CommandStatusWrapper_t SCSICommandStatus;
596
597 if ((ErrorCode = MS_Host_SendCommand(MSInterfaceInfo, &SCSICommandBlock, BlockBuffer)) != PIPE_RWSTREAM_NoError)
598 return ErrorCode;
599
600 if ((ErrorCode = MS_Host_GetReturnedStatus(MSInterfaceInfo, &SCSICommandStatus)) != PIPE_RWSTREAM_NoError)
601 return ErrorCode;
602
603 return PIPE_RWSTREAM_NoError;
604 }
605
606 #endif