3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
34 #define INCLUDE_FROM_MS_CLASS_HOST_C
35 #include "MassStorage.h"
37 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint16_t ConfigDescriptorLength
,
38 uint8_t* DeviceConfigDescriptor
)
40 uint8_t FoundEndpoints
= 0;
42 memset(&MSInterfaceInfo
->State
, 0x00, sizeof(MSInterfaceInfo
->State
));
44 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor
) != DTYPE_Configuration
)
45 return MS_ENUMERROR_InvalidConfigDescriptor
;
47 if (USB_GetNextDescriptorComp(&ConfigDescriptorLength
, &DeviceConfigDescriptor
,
48 DComp_NextMassStorageInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
50 return MS_ENUMERROR_NoMSInterfaceFound
;
53 MSInterfaceInfo
->State
.InterfaceNumber
=
54 #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES)
55 DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Interface_t
)->InterfaceNumber
;
57 DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Interface_t
)->bInterfaceNumber
;
60 while (FoundEndpoints
!= (MS_FOUND_DATAPIPE_IN
| MS_FOUND_DATAPIPE_OUT
))
62 if (USB_GetNextDescriptorComp(&ConfigDescriptorLength
, &DeviceConfigDescriptor
,
63 DComp_NextInterfaceBulkDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
65 return MS_ENUMERROR_EndpointsNotFound
;
68 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Endpoint_t
);
70 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
72 Pipe_ConfigurePipe(MSInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
73 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
75 MSInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
77 FoundEndpoints
|= MS_FOUND_DATAPIPE_IN
;
81 Pipe_ConfigurePipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
82 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
84 MSInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
86 FoundEndpoints
|= MS_FOUND_DATAPIPE_OUT
;
90 MSInterfaceInfo
->State
.Active
= true;
91 return MS_ENUMERROR_NoError
;
94 static uint8_t DComp_NextMassStorageInterface(void* CurrentDescriptor
)
96 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
98 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class
== MASS_STORE_CLASS
) &&
99 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass
== MASS_STORE_SUBCLASS
) &&
100 (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol
== MASS_STORE_PROTOCOL
))
102 return DESCRIPTOR_SEARCH_Found
;
106 return DESCRIPTOR_SEARCH_NotFound
;
109 static uint8_t DComp_NextInterfaceBulkDataEndpoint(void* CurrentDescriptor
)
111 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
113 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
114 USB_Descriptor_Endpoint_t
);
116 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
118 if ((EndpointType
== EP_TYPE_BULK
) &&
119 (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
))))
121 return DESCRIPTOR_SEARCH_Found
;
124 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
126 return DESCRIPTOR_SEARCH_Fail
;
129 return DESCRIPTOR_SEARCH_NotFound
;
132 void MS_Host_USBTask(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
)
137 static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, MS_CommandBlockWrapper_t
* SCSICommandBlock
,
140 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
142 SCSICommandBlock
->Tag
= ++MSInterfaceInfo
->State
.TransactionTag
;
144 if (MSInterfaceInfo
->State
.TransactionTag
== 0xFFFFFFFF)
145 MSInterfaceInfo
->State
.TransactionTag
= 1;
147 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
150 if ((ErrorCode
= Pipe_Write_Stream_LE(SCSICommandBlock
, sizeof(MS_CommandBlockWrapper_t
))) != PIPE_RWSTREAM_NoError
)
154 Pipe_WaitUntilReady();
158 if ((BufferPtr
!= NULL
) &&
159 ((ErrorCode
= MS_Host_SendReceiveData(MSInterfaceInfo
, SCSICommandBlock
, BufferPtr
)) != PIPE_RWSTREAM_NoError
))
168 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
)
170 uint16_t TimeoutMSRem
= COMMAND_DATA_TIMEOUT_MS
;
172 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
175 while (!(Pipe_IsINReceived()))
177 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
179 USB_INT_Clear(USB_INT_HSOFI
);
183 return PIPE_RWSTREAM_Timeout
;
187 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
190 if (Pipe_IsStalled())
192 USB_Host_ClearPipeStall(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
194 return PIPE_RWSTREAM_PipeStalled
;
198 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
201 if (Pipe_IsStalled())
203 USB_Host_ClearPipeStall(MSInterfaceInfo
->Config
.DataINPipeNumber
);
205 return PIPE_RWSTREAM_PipeStalled
;
208 if (USB_HostState
== HOST_STATE_Unattached
)
209 return PIPE_RWSTREAM_DeviceDisconnected
;
212 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
215 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
218 return PIPE_RWSTREAM_NoError
;
221 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
,
222 MS_CommandBlockWrapper_t
* SCSICommandBlock
, void* BufferPtr
)
224 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
225 uint16_t BytesRem
= SCSICommandBlock
->DataTransferLength
;
227 if (SCSICommandBlock
->Flags
& COMMAND_DIRECTION_DATA_IN
)
229 if ((ErrorCode
= MS_Host_WaitForDataReceived(MSInterfaceInfo
)) != PIPE_RWSTREAM_NoError
)
235 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
238 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPtr
, BytesRem
)) != PIPE_RWSTREAM_NoError
)
245 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
248 if ((ErrorCode
= Pipe_Write_Stream_LE(BufferPtr
, BytesRem
)) != PIPE_RWSTREAM_NoError
)
253 while (!(Pipe_IsOUTReady()))
255 if (USB_HostState
== HOST_STATE_Unattached
)
256 return PIPE_RWSTREAM_DeviceDisconnected
;
265 static uint8_t MS_Host_GetReturnedStatus(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
,
266 MS_CommandStatusWrapper_t
* SCSICommandStatus
)
268 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
270 if ((ErrorCode
= MS_Host_WaitForDataReceived(MSInterfaceInfo
)) != PIPE_RWSTREAM_NoError
)
273 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
276 if ((ErrorCode
= Pipe_Read_Stream_LE(SCSICommandStatus
, sizeof(MS_CommandStatusWrapper_t
))) != PIPE_RWSTREAM_NoError
)
282 if (SCSICommandStatus
->Status
!= SCSI_Command_Pass
)
283 ErrorCode
= MS_ERROR_LOGICAL_CMD_FAILED
;
288 uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
)
290 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
291 return HOST_SENDCONTROL_DeviceDisconnect
;
293 USB_ControlRequest
= (USB_Request_Header_t
)
295 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
296 .bRequest
= REQ_MassStorageReset
,
298 .wIndex
= MSInterfaceInfo
->State
.InterfaceNumber
,
302 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
304 return USB_Host_SendControlRequest(NULL
);
307 uint8_t MS_Host_GetMaxLUN(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t* MaxLUNIndex
)
309 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
310 return HOST_SENDCONTROL_DeviceDisconnect
;
314 USB_ControlRequest
= (USB_Request_Header_t
)
316 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
317 .bRequest
= REQ_GetMaxLUN
,
319 .wIndex
= MSInterfaceInfo
->State
.InterfaceNumber
,
323 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
325 if ((ErrorCode
= USB_Host_SendControlRequest(MaxLUNIndex
)) == HOST_SENDCONTROL_SetupStalled
)
335 uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t LUNIndex
, SCSI_Inquiry_Response_t
* InquiryData
)
337 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
338 return HOST_SENDCONTROL_DeviceDisconnect
;
340 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
342 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
344 .Signature
= CBW_SIGNATURE
,
345 .DataTransferLength
= sizeof(SCSI_Inquiry_Response_t
),
346 .Flags
= COMMAND_DIRECTION_DATA_IN
,
348 .SCSICommandLength
= 6,
355 sizeof(SCSI_Inquiry_Response_t
), // Allocation Length
356 0x00 // Unused (control)
360 MS_CommandStatusWrapper_t SCSICommandStatus
;
362 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, InquiryData
)) != PIPE_RWSTREAM_NoError
)
368 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
377 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t LUNIndex
)
379 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
380 return HOST_SENDCONTROL_DeviceDisconnect
;
382 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
384 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
386 .Signature
= CBW_SIGNATURE
,
387 .DataTransferLength
= 0,
388 .Flags
= COMMAND_DIRECTION_DATA_IN
,
390 .SCSICommandLength
= 6,
393 SCSI_CMD_TEST_UNIT_READY
,
398 0x00 // Unused (control)
402 MS_CommandStatusWrapper_t SCSICommandStatus
;
404 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
)
410 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
419 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t LUNIndex
,
420 SCSI_Capacity_t
* DeviceCapacity
)
422 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
423 return HOST_SENDCONTROL_DeviceDisconnect
;
425 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
427 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
429 .Signature
= CBW_SIGNATURE
,
430 .DataTransferLength
= sizeof(SCSI_Capacity_t
),
431 .Flags
= COMMAND_DIRECTION_DATA_IN
,
433 .SCSICommandLength
= 10,
436 SCSI_CMD_READ_CAPACITY_10
,
438 0x00, // MSB of Logical block address
441 0x00, // LSB of Logical block address
444 0x00, // Partial Medium Indicator
445 0x00 // Unused (control)
449 MS_CommandStatusWrapper_t SCSICommandStatus
;
451 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, DeviceCapacity
)) != PIPE_RWSTREAM_NoError
)
457 DeviceCapacity
->Blocks
= SwapEndian_32(DeviceCapacity
->Blocks
);
458 DeviceCapacity
->BlockSize
= SwapEndian_32(DeviceCapacity
->BlockSize
);
460 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
469 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t LUNIndex
,
470 SCSI_Request_Sense_Response_t
* SenseData
)
472 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
473 return HOST_SENDCONTROL_DeviceDisconnect
;
475 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
477 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
479 .Signature
= CBW_SIGNATURE
,
480 .DataTransferLength
= sizeof(SCSI_Request_Sense_Response_t
),
481 .Flags
= COMMAND_DIRECTION_DATA_IN
,
483 .SCSICommandLength
= 6,
486 SCSI_CMD_REQUEST_SENSE
,
490 sizeof(SCSI_Request_Sense_Response_t
), // Allocation Length
491 0x00 // Unused (control)
495 MS_CommandStatusWrapper_t SCSICommandStatus
;
497 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, SenseData
)) != PIPE_RWSTREAM_NoError
)
503 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
512 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t LUNIndex
, bool PreventRemoval
)
514 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
515 return HOST_SENDCONTROL_DeviceDisconnect
;
517 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
519 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
521 .Signature
= CBW_SIGNATURE
,
522 .DataTransferLength
= 0,
523 .Flags
= COMMAND_DIRECTION_DATA_OUT
,
525 .SCSICommandLength
= 6,
528 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
,
531 PreventRemoval
, // Prevent flag
533 0x00 // Unused (control)
537 MS_CommandStatusWrapper_t SCSICommandStatus
;
539 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
)
545 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
554 uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t LUNIndex
, uint32_t BlockAddress
,
555 uint8_t Blocks
, uint16_t BlockSize
, void* BlockBuffer
)
557 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
558 return HOST_SENDCONTROL_DeviceDisconnect
;
560 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
562 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
564 .Signature
= CBW_SIGNATURE
,
565 .DataTransferLength
= ((uint32_t)Blocks
* BlockSize
),
566 .Flags
= COMMAND_DIRECTION_DATA_IN
,
568 .SCSICommandLength
= 10,
572 0x00, // Unused (control bits, all off)
573 (BlockAddress
>> 24), // MSB of Block Address
574 (BlockAddress
>> 16),
576 (BlockAddress
& 0xFF), // LSB of Block Address
577 0x00, // Unused (reserved)
578 0x00, // MSB of Total Blocks to Read
579 Blocks
, // LSB of Total Blocks to Read
580 0x00 // Unused (control)
584 MS_CommandStatusWrapper_t SCSICommandStatus
;
586 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, BlockBuffer
)) != PIPE_RWSTREAM_NoError
)
592 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
601 uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t
* MSInterfaceInfo
, uint8_t LUNIndex
, uint32_t BlockAddress
,
602 uint8_t Blocks
, uint16_t BlockSize
, void* BlockBuffer
)
604 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.Active
))
605 return HOST_SENDCONTROL_DeviceDisconnect
;
607 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
609 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
611 .Signature
= CBW_SIGNATURE
,
612 .DataTransferLength
= ((uint32_t)Blocks
* BlockSize
),
613 .Flags
= COMMAND_DIRECTION_DATA_OUT
,
615 .SCSICommandLength
= 10,
619 0x00, // Unused (control bits, all off)
620 (BlockAddress
>> 24), // MSB of Block Address
621 (BlockAddress
>> 16),
623 (BlockAddress
& 0xFF), // LSB of Block Address
624 0x00, // Unused (reserved)
625 0x00, // MSB of Total Blocks to Write
626 Blocks
, // LSB of Total Blocks to Write
627 0x00 // Unused (control)
631 MS_CommandStatusWrapper_t SCSICommandStatus
;
633 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, BlockBuffer
)) != PIPE_RWSTREAM_NoError
)
639 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)