3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.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_CLASS_HOST_C
36 #define __INCLUDE_FROM_MS_DRIVER
37 #include "MassStorage.h"
39 uint8_t MS_Host_ConfigurePipes(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* DeviceConfigDescriptor
)
43 uint8_t FoundEndpoints
= 0;
45 memset(&MSInterfaceInfo
->State
, 0x00, sizeof(MSInterfaceInfo
->State
));
47 if (DESCRIPTOR_TYPE(DeviceConfigDescriptor
) != DTYPE_Configuration
)
48 return MS_ENUMERROR_InvalidConfigDescriptor
;
50 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
51 DCOMP_MS_NextMSInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
53 return MS_ENUMERROR_NoMSInterfaceFound
;
56 MSInterfaceInfo
->State
.InterfaceNumber
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Interface_t
)->InterfaceNumber
;
58 while (FoundEndpoints
!= (MS_FOUND_DATAPIPE_IN
| MS_FOUND_DATAPIPE_OUT
))
60 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &DeviceConfigDescriptor
,
61 DCOMP_MS_NextMSInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
63 return MS_ENUMERROR_EndpointsNotFound
;
66 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(DeviceConfigDescriptor
, USB_Descriptor_Endpoint_t
);
68 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
70 Pipe_ConfigurePipe(MSInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_IN
,
71 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
72 MSInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
73 MSInterfaceInfo
->State
.DataINPipeSize
= EndpointData
->EndpointSize
;
75 FoundEndpoints
|= MS_FOUND_DATAPIPE_IN
;
79 Pipe_ConfigurePipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
,
80 EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
,
81 MSInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
);
82 MSInterfaceInfo
->State
.DataOUTPipeSize
= EndpointData
->EndpointSize
;
84 FoundEndpoints
|= MS_FOUND_DATAPIPE_OUT
;
88 MSInterfaceInfo
->State
.IsActive
= true;
89 return MS_ENUMERROR_NoError
;
92 static uint8_t DCOMP_MS_NextMSInterface(void* const CurrentDescriptor
)
94 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
96 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
97 USB_Descriptor_Interface_t
);
99 if ((CurrentInterface
->Class
== MASS_STORE_CLASS
) &&
100 (CurrentInterface
->SubClass
== MASS_STORE_SUBCLASS
) &&
101 (CurrentInterface
->Protocol
== MASS_STORE_PROTOCOL
))
103 return DESCRIPTOR_SEARCH_Found
;
107 return DESCRIPTOR_SEARCH_NotFound
;
110 static uint8_t DCOMP_MS_NextMSInterfaceEndpoint(void* const CurrentDescriptor
)
112 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
114 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
115 USB_Descriptor_Endpoint_t
);
117 uint8_t EndpointType
= (CurrentEndpoint
->Attributes
& EP_TYPE_MASK
);
119 if ((EndpointType
== EP_TYPE_BULK
) &&
120 (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
))))
122 return DESCRIPTOR_SEARCH_Found
;
125 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
127 return DESCRIPTOR_SEARCH_Fail
;
130 return DESCRIPTOR_SEARCH_NotFound
;
133 static uint8_t MS_Host_SendCommand(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
134 MS_CommandBlockWrapper_t
* const SCSICommandBlock
,
135 const void* const BufferPtr
)
137 uint8_t ErrorCode
= PIPE_RWSTREAM_NoError
;
139 SCSICommandBlock
->Signature
= CBW_SIGNATURE
;
140 SCSICommandBlock
->Tag
= ++MSInterfaceInfo
->State
.TransactionTag
;
142 if (MSInterfaceInfo
->State
.TransactionTag
== 0xFFFFFFFF)
143 MSInterfaceInfo
->State
.TransactionTag
= 1;
145 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
148 if ((ErrorCode
= Pipe_Write_Stream_LE(SCSICommandBlock
, sizeof(MS_CommandBlockWrapper_t
),
149 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
153 Pipe_WaitUntilReady();
157 if ((BufferPtr
!= NULL
) &&
158 ((ErrorCode
= MS_Host_SendReceiveData(MSInterfaceInfo
, SCSICommandBlock
, (void*)BufferPtr
)) != PIPE_RWSTREAM_NoError
))
167 static uint8_t MS_Host_WaitForDataReceived(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
)
169 uint16_t TimeoutMSRem
= COMMAND_DATA_TIMEOUT_MS
;
171 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
174 while (!(Pipe_IsINReceived()))
176 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
178 USB_INT_Clear(USB_INT_HSOFI
);
182 return PIPE_RWSTREAM_Timeout
;
186 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
189 if (Pipe_IsStalled())
191 USB_Host_ClearPipeStall(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
193 return PIPE_RWSTREAM_PipeStalled
;
197 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
200 if (Pipe_IsStalled())
202 USB_Host_ClearPipeStall(MSInterfaceInfo
->Config
.DataINPipeNumber
);
204 return PIPE_RWSTREAM_PipeStalled
;
207 if (USB_HostState
== HOST_STATE_Unattached
)
208 return PIPE_RWSTREAM_DeviceDisconnected
;
211 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataINPipeNumber
);
214 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
217 return PIPE_RWSTREAM_NoError
;
220 static uint8_t MS_Host_SendReceiveData(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
221 MS_CommandBlockWrapper_t
* const SCSICommandBlock
,
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
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
245 Pipe_SelectPipe(MSInterfaceInfo
->Config
.DataOUTPipeNumber
);
248 if ((ErrorCode
= Pipe_Write_Stream_LE(BufferPtr
, BytesRem
, NO_STREAM_CALLBACK
)) != 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
* const MSInterfaceInfo
,
266 MS_CommandStatusWrapper_t
* const 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
),
277 NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
285 if (SCSICommandStatus
->Status
!= SCSI_Command_Pass
)
286 ErrorCode
= MS_ERROR_LOGICAL_CMD_FAILED
;
291 uint8_t MS_Host_ResetMSInterface(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
)
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
* const MSInterfaceInfo
,
308 uint8_t* const MaxLUNIndex
)
310 uint8_t ErrorCode
= HOST_SENDCONTROL_Successful
;
312 USB_ControlRequest
= (USB_Request_Header_t
)
314 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
315 .bRequest
= REQ_GetMaxLUN
,
317 .wIndex
= MSInterfaceInfo
->State
.InterfaceNumber
,
321 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
323 if ((ErrorCode
= USB_Host_SendControlRequest(MaxLUNIndex
)) != HOST_SENDCONTROL_Successful
)
326 ErrorCode
= HOST_SENDCONTROL_Successful
;
332 uint8_t MS_Host_GetInquiryData(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
333 const uint8_t LUNIndex
,
334 SCSI_Inquiry_Response_t
* const InquiryData
)
336 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
337 return HOST_SENDCONTROL_DeviceDisconnected
;
341 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
343 .DataTransferLength
= sizeof(SCSI_Inquiry_Response_t
),
344 .Flags
= COMMAND_DIRECTION_DATA_IN
,
346 .SCSICommandLength
= 6,
353 sizeof(SCSI_Inquiry_Response_t
), // Allocation Length
354 0x00 // Unused (control)
358 MS_CommandStatusWrapper_t SCSICommandStatus
;
360 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, InquiryData
)) != PIPE_RWSTREAM_NoError
)
363 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
366 return PIPE_RWSTREAM_NoError
;
369 uint8_t MS_Host_TestUnitReady(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
370 const uint8_t LUNIndex
)
372 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
373 return HOST_SENDCONTROL_DeviceDisconnected
;
377 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
379 .DataTransferLength
= 0,
380 .Flags
= COMMAND_DIRECTION_DATA_IN
,
382 .SCSICommandLength
= 6,
385 SCSI_CMD_TEST_UNIT_READY
,
390 0x00 // Unused (control)
394 MS_CommandStatusWrapper_t SCSICommandStatus
;
396 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
)
399 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
402 return PIPE_RWSTREAM_NoError
;
405 uint8_t MS_Host_ReadDeviceCapacity(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
406 const uint8_t LUNIndex
,
407 SCSI_Capacity_t
* const DeviceCapacity
)
409 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
410 return HOST_SENDCONTROL_DeviceDisconnected
;
414 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
416 .DataTransferLength
= sizeof(SCSI_Capacity_t
),
417 .Flags
= COMMAND_DIRECTION_DATA_IN
,
419 .SCSICommandLength
= 10,
422 SCSI_CMD_READ_CAPACITY_10
,
424 0x00, // MSB of Logical block address
427 0x00, // LSB of Logical block address
430 0x00, // Partial Medium Indicator
431 0x00 // Unused (control)
435 MS_CommandStatusWrapper_t SCSICommandStatus
;
437 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, DeviceCapacity
)) != PIPE_RWSTREAM_NoError
)
440 SwapEndian_n(&DeviceCapacity
->Blocks
, sizeof(DeviceCapacity
->Blocks
));
441 SwapEndian_n(&DeviceCapacity
->BlockSize
, sizeof(DeviceCapacity
->BlockSize
));
443 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
446 return PIPE_RWSTREAM_NoError
;
449 uint8_t MS_Host_RequestSense(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
450 const uint8_t LUNIndex
,
451 SCSI_Request_Sense_Response_t
* const SenseData
)
453 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
454 return HOST_SENDCONTROL_DeviceDisconnected
;
458 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
460 .DataTransferLength
= sizeof(SCSI_Request_Sense_Response_t
),
461 .Flags
= COMMAND_DIRECTION_DATA_IN
,
463 .SCSICommandLength
= 6,
466 SCSI_CMD_REQUEST_SENSE
,
470 sizeof(SCSI_Request_Sense_Response_t
), // Allocation Length
471 0x00 // Unused (control)
475 MS_CommandStatusWrapper_t SCSICommandStatus
;
477 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, SenseData
)) != PIPE_RWSTREAM_NoError
)
480 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
483 return PIPE_RWSTREAM_NoError
;
486 uint8_t MS_Host_PreventAllowMediumRemoval(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
487 const uint8_t LUNIndex
,
488 const bool PreventRemoval
)
490 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
491 return HOST_SENDCONTROL_DeviceDisconnected
;
495 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
497 .DataTransferLength
= 0,
498 .Flags
= COMMAND_DIRECTION_DATA_OUT
,
500 .SCSICommandLength
= 6,
503 SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL
,
506 PreventRemoval
, // Prevent flag
508 0x00 // Unused (control)
512 MS_CommandStatusWrapper_t SCSICommandStatus
;
514 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, NULL
)) != PIPE_RWSTREAM_NoError
)
517 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
520 return PIPE_RWSTREAM_NoError
;
523 uint8_t MS_Host_ReadDeviceBlocks(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
524 const uint8_t LUNIndex
,
525 const uint32_t BlockAddress
,
526 const uint8_t Blocks
,
527 const uint16_t BlockSize
,
530 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
531 return HOST_SENDCONTROL_DeviceDisconnected
;
535 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
537 .DataTransferLength
= ((uint32_t)Blocks
* BlockSize
),
538 .Flags
= COMMAND_DIRECTION_DATA_IN
,
540 .SCSICommandLength
= 10,
544 0x00, // Unused (control bits, all off)
545 (BlockAddress
>> 24), // MSB of Block Address
546 (BlockAddress
>> 16),
548 (BlockAddress
& 0xFF), // LSB of Block Address
550 0x00, // MSB of Total Blocks to Read
551 Blocks
, // LSB of Total Blocks to Read
552 0x00 // Unused (control)
556 MS_CommandStatusWrapper_t SCSICommandStatus
;
558 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, BlockBuffer
)) != PIPE_RWSTREAM_NoError
)
561 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
564 return PIPE_RWSTREAM_NoError
;
567 uint8_t MS_Host_WriteDeviceBlocks(USB_ClassInfo_MS_Host_t
* const MSInterfaceInfo
,
568 const uint8_t LUNIndex
,
569 const uint32_t BlockAddress
,
570 const uint8_t Blocks
,
571 const uint16_t BlockSize
,
572 const void* BlockBuffer
)
574 if ((USB_HostState
!= HOST_STATE_Configured
) || !(MSInterfaceInfo
->State
.IsActive
))
575 return HOST_SENDCONTROL_DeviceDisconnected
;
579 MS_CommandBlockWrapper_t SCSICommandBlock
= (MS_CommandBlockWrapper_t
)
581 .DataTransferLength
= ((uint32_t)Blocks
* BlockSize
),
582 .Flags
= COMMAND_DIRECTION_DATA_OUT
,
584 .SCSICommandLength
= 10,
588 0x00, // Unused (control bits, all off)
589 (BlockAddress
>> 24), // MSB of Block Address
590 (BlockAddress
>> 16),
592 (BlockAddress
& 0xFF), // LSB of Block Address
594 0x00, // MSB of Total Blocks to Write
595 Blocks
, // LSB of Total Blocks to Write
596 0x00 // Unused (control)
600 MS_CommandStatusWrapper_t SCSICommandStatus
;
602 if ((ErrorCode
= MS_Host_SendCommand(MSInterfaceInfo
, &SCSICommandBlock
, BlockBuffer
)) != PIPE_RWSTREAM_NoError
)
605 if ((ErrorCode
= MS_Host_GetReturnedStatus(MSInterfaceInfo
, &SCSICommandStatus
)) != PIPE_RWSTREAM_NoError
)
608 return PIPE_RWSTREAM_NoError
;