3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define __INCLUDE_FROM_MS_DRIVER
36 #define __INCLUDE_FROM_MASSSTORAGE_HOST_C
37 #include "MassStorage.h"
39 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* ConfigDescriptorData
)
43 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
44 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
45 USB_Descriptor_Interface_t
* MassStorageInterface
= NULL
;
47 memset(&MSInterfaceInfo
->State
, 0x00, sizeof(MSInterfaceInfo
->State
));
49 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
50 return MS_ENUMERROR_InvalidConfigDescriptor
;
52 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
54 if (!(MassStorageInterface
) ||
55 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
56 DCOMP_MS_Host_NextMSInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
58 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
59 DCOMP_MS_Host_NextMSInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
61 return MS_ENUMERROR_NoCompatibleInterfaceFound
;
64 MassStorageInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
66 DataINEndpoint
= NULL
;
67 DataOUTEndpoint
= NULL
;
72 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
74 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
75 DataINEndpoint
= EndpointData
;
77 DataOUTEndpoint
= EndpointData
;
80 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
82 if (PipeNum
== MSInterfaceInfo
->Config
.DataINPipeNumber
)
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
);
88 MSInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
90 else if (PipeNum
== MSInterfaceInfo
->Config
.DataOUTPipeNumber
)
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
);
96 MSInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
100 MSInterfaceInfo
->State
.InterfaceNumber
= MassStorageInterface
->InterfaceNumber
;
101 MSInterfaceInfo
->State
.IsActive
= true;
103 return MS_ENUMERROR_NoError
;
106 static uint8_t DCOMP_MS_Host_NextMSInterface(void* const CurrentDescriptor
)
108 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
110 if (Header
->Type
== DTYPE_Interface
)
112 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
114 if ((Interface
->Class
== MS_CSCP_MassStorageClass
) &&
115 (Interface
->SubClass
== MS_CSCP_SCSITransparentSubclass
) &&
116 (Interface
->Protocol
== MS_CSCP_BulkOnlyTransportProtocol
))
118 return DESCRIPTOR_SEARCH_Found
;
122 return DESCRIPTOR_SEARCH_NotFound
;
125 static uint8_t DCOMP_MS_Host_NextMSInterfaceEndpoint(void* const CurrentDescriptor
)
127 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
129 if (Header
->Type
== DTYPE_Endpoint
)
131 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
133 uint8_t EndpointType
= (Endpoint
->Attributes
& EP_TYPE_MASK
);
135 if ((EndpointType
== EP_TYPE_BULK
) && (!(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
))))
137 return DESCRIPTOR_SEARCH_Found
;
140 else if (Header
->Type
== DTYPE_Interface
)
142 return DESCRIPTOR_SEARCH_Fail
;
145 return DESCRIPTOR_SEARCH_NotFound
;
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
)
152 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
154 SCSICommandBlock
->Signature
= MS_CBW_SIGNATURE
;
155 SCSICommandBlock
->Tag
= ++MSInterfaceInfo
->State
.TransactionTag
;
157 if (MSInterfaceInfo
->State
.TransactionTag
== 0xFFFFFFFF)
158 MSInterfaceInfo
->State
.TransactionTag
= 1;
160 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
163 if ((ErrorCode
= Pipe_Write_Stream_LE(SCSICommandBlock
, sizeof(MS_CommandBlockWrapper_t
),
164 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
168 Pipe_WaitUntilReady();
172 if ((BufferPtr
!= NULL
) &&
173 ((ErrorCode
= MS_Host_SendReceiveData(MSInterfaceInfo
, SCSICommandBlock
, (void*)BufferPtr
)) != PIPE_RWSTREAM_NoError
))
182 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
)
184 uint16_t TimeoutMSRem
= MS_COMMAND_DATA_TIMEOUT_MS
;
185 uint16_t PreviousFrameNumber
= USB_Host_GetFrameNumber();
187 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
190 while (!(Pipe_IsINReceived()))
192 uint16_t CurrentFrameNumber
= USB_Host_GetFrameNumber();
194 if (CurrentFrameNumber
!= PreviousFrameNumber
)
196 PreviousFrameNumber
= CurrentFrameNumber
;
198 if (!(TimeoutMSRem
--))
199 return PIPE_RWSTREAM_Timeout
;
203 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
206 if (Pipe_IsStalled())
208 USB_Host_ClearPipeStall(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
210 return PIPE_RWSTREAM_PipeStalled
;
214 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
217 if (Pipe_IsStalled())
219 USB_Host_ClearPipeStall(MSInterfaceInfo
->Config
.DataINPipeNumber
);
221 return PIPE_RWSTREAM_PipeStalled
;
224 if (USB_HostState
== HOST_STATE_Unattached
)
225 return PIPE_RWSTREAM_DeviceDisconnected
;
228 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
231 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
234 return PIPE_RWSTREAM_NoError
;
237 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
238 MS_CommandBlockWrapper_t
* const SCSICommandBlock
,
241 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
242 uint16_t BytesRem
= SCSICommandBlock
->DataTransferLength
;
244 if (SCSICommandBlock
->Flags
& MS_COMMAND_DIR_DATA_IN
)
246 if ((ErrorCode
= MS_Host_WaitForDataReceived(MSInterfaceInfo
)) != PIPE_RWSTREAM_NoError
)
252 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
255 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPtr
, BytesRem
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
262 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
265 if ((ErrorCode
= Pipe_Write_Stream_LE(BufferPtr
, BytesRem
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
270 while (!(Pipe_IsOUTReady()))
272 if (USB_HostState
== HOST_STATE_Unattached
)
273 return PIPE_RWSTREAM_DeviceDisconnected
;
282 static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
283 MS_CommandStatusWrapper_t
* const SCSICommandStatus
)
285 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
287 if ((ErrorCode
= MS_Host_WaitForDataReceived(MSInterfaceInfo
)) != PIPE_RWSTREAM_NoError
)
290 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
293 if ((ErrorCode
= Pipe_Read_Stream_LE(SCSICommandStatus
, sizeof(MS_CommandStatusWrapper_t
),
294 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
302 if (SCSICommandStatus
->Status
!= MS_SCSI_COMMAND_Pass
)
303 ErrorCode
= MS_ERROR_LOGICAL_CMD_FAILED
;
308 uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
)
310 USB_ControlRequest
= (USB_Request_Header_t
)
312 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
313 .bRequest
= MS_REQ_MassStorageReset
,
315 .wIndex
= MSInterfaceInfo
->State
.InterfaceNumber
,
319 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
321 return USB_Host_SendControlRequest(NULL
);
324 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
325 uint8_t* const MaxLUNIndex
)
327 uint8_t ErrorCode
= HOST_SENDCONTROL_Successful
;
329 USB_ControlRequest
= (USB_Request_Header_t
)
331 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
332 .bRequest
= MS_REQ_GetMaxLUN
,
334 .wIndex
= MSInterfaceInfo
->State
.InterfaceNumber
,
338 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
340 if ((ErrorCode
= USB_Host_SendControlRequest(MaxLUNIndex
)) != HOST_SENDCONTROL_Successful
)
343 ErrorCode
= HOST_SENDCONTROL_Successful
;
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
)
353 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
354 return HOST_SENDCONTROL_DeviceDisconnected
;
358 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
360 .DataTransferLength
= sizeof(SCSI_Inquiry_Response_t
),
361 .Flags
= MS_COMMAND_DIR_DATA_IN
,
363 .SCSICommandLength
= 6,
370 sizeof(SCSI_Inquiry_Response_t
), // Allocation Length
371 0x00 // Unused (control)
375 MS_CommandStatusWrapper_t SCSICommandStatus
;
377 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, InquiryData
)) != PIPE_RWSTREAM_NoError
)
380 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
383 return PIPE_RWSTREAM_NoError
;
386 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
387 const uint8_t LUNIndex
)
389 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
390 return HOST_SENDCONTROL_DeviceDisconnected
;
394 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
396 .DataTransferLength
= 0,
397 .Flags
= MS_COMMAND_DIR_DATA_IN
,
399 .SCSICommandLength
= 6,
402 SCSI_CMD_TEST_UNIT_READY
,
407 0x00 // Unused (control)
411 MS_CommandStatusWrapper_t SCSICommandStatus
;
413 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
)
416 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
419 return PIPE_RWSTREAM_NoError
;
422 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
423 const uint8_t LUNIndex
,
424 SCSI_Capacity_t
* const DeviceCapacity
)
426 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
427 return HOST_SENDCONTROL_DeviceDisconnected
;
431 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
433 .DataTransferLength
= sizeof(SCSI_Capacity_t
),
434 .Flags
= MS_COMMAND_DIR_DATA_IN
,
436 .SCSICommandLength
= 10,
439 SCSI_CMD_READ_CAPACITY_10
,
441 0x00, // MSB of Logical block address
444 0x00, // LSB of Logical block address
447 0x00, // Partial Medium Indicator
448 0x00 // Unused (control)
452 MS_CommandStatusWrapper_t SCSICommandStatus
;
454 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, DeviceCapacity
)) != PIPE_RWSTREAM_NoError
)
457 SwapEndian_n(&DeviceCapacity
->Blocks
, sizeof(DeviceCapacity
->Blocks
));
458 SwapEndian_n(&DeviceCapacity
->BlockSize
, sizeof(DeviceCapacity
->BlockSize
));
460 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
463 return PIPE_RWSTREAM_NoError
;
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
)
470 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
471 return HOST_SENDCONTROL_DeviceDisconnected
;
475 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
477 .DataTransferLength
= sizeof(SCSI_Request_Sense_Response_t
),
478 .Flags
= MS_COMMAND_DIR_DATA_IN
,
480 .SCSICommandLength
= 6,
483 SCSI_CMD_REQUEST_SENSE
,
487 sizeof(SCSI_Request_Sense_Response_t
), // Allocation Length
488 0x00 // Unused (control)
492 MS_CommandStatusWrapper_t SCSICommandStatus
;
494 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, SenseData
)) != PIPE_RWSTREAM_NoError
)
497 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
500 return PIPE_RWSTREAM_NoError
;
503 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
504 const uint8_t LUNIndex
,
505 const bool PreventRemoval
)
507 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
508 return HOST_SENDCONTROL_DeviceDisconnected
;
512 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
514 .DataTransferLength
= 0,
515 .Flags
= MS_COMMAND_DIR_DATA_OUT
,
517 .SCSICommandLength
= 6,
520 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
,
523 PreventRemoval
, // Prevent flag
525 0x00 // Unused (control)
529 MS_CommandStatusWrapper_t SCSICommandStatus
;
531 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
)
534 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
537 return PIPE_RWSTREAM_NoError
;
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
,
547 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
548 return HOST_SENDCONTROL_DeviceDisconnected
;
552 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
554 .DataTransferLength
= ((uint32_t)Blocks
* BlockSize
),
555 .Flags
= MS_COMMAND_DIR_DATA_IN
,
557 .SCSICommandLength
= 10,
561 0x00, // Unused (control bits, all off)
562 (BlockAddress
>> 24), // MSB of Block Address
563 (BlockAddress
>> 16),
565 (BlockAddress
& 0xFF), // LSB of Block Address
567 0x00, // MSB of Total Blocks to Read
568 Blocks
, // LSB of Total Blocks to Read
569 0x00 // Unused (control)
573 MS_CommandStatusWrapper_t SCSICommandStatus
;
575 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, BlockBuffer
)) != PIPE_RWSTREAM_NoError
)
578 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
581 return PIPE_RWSTREAM_NoError
;
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
)
591 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
592 return HOST_SENDCONTROL_DeviceDisconnected
;
596 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
598 .DataTransferLength
= ((uint32_t)Blocks
* BlockSize
),
599 .Flags
= MS_COMMAND_DIR_DATA_OUT
,
601 .SCSICommandLength
= 10,
605 0x00, // Unused (control bits, all off)
606 (BlockAddress
>> 24), // MSB of Block Address
607 (BlockAddress
>> 16),
609 (BlockAddress
& 0xFF), // LSB of Block Address
611 0x00, // MSB of Total Blocks to Write
612 Blocks
, // LSB of Total Blocks to Write
613 0x00 // Unused (control)
617 MS_CommandStatusWrapper_t SCSICommandStatus
;
619 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, BlockBuffer
)) != PIPE_RWSTREAM_NoError
)
622 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
625 return PIPE_RWSTREAM_NoError
;