implement msc device class

usbd auto stall control for not supported return from class control request
usbd implement xfer isr callback mechanism
DCD
- implement dcd multiple qtd support
- dcd dcd_pipe_stall
- implement dcd_pipe_queue_xfer
- xfer_complete_isr
- flush control endpoint if received new setup while previous transfer is not complete
change msc_cmd_block_wrapper_t flags field to dir
force full speed for easy testing

NOTEs: somehow unable to get endpoint IN interrupt with ioc
This commit is contained in:
hathach
2013-11-01 12:11:26 +07:00
parent c760c69d51
commit 3a54ad4c0d
28 changed files with 821 additions and 243 deletions

View File

@@ -138,13 +138,10 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const *
uint8_t const desc_type = u16_high_u8(p_request->wValue);
uint8_t const desc_index = u16_low_u8 (p_request->wValue);
if ( p_request->bRequest == TUSB_REQUEST_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
{
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_hid->p_report_desc, p_hid->report_length);
}else
{
dcd_pipe_control_stall(coreid);
}
ASSERT ( p_request->bRequest == TUSB_REQUEST_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT,
TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT);
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, p_hid->p_report_desc, p_hid->report_length);
}
//------------- Class Specific Request -------------//
else if (p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_CLASS)
@@ -170,12 +167,11 @@ tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const *
case HID_REQUEST_CONTROL_GET_PROTOCOL:
case HID_REQUEST_CONTROL_SET_PROTOCOL:
default:
dcd_pipe_control_stall(coreid);
return TUSB_ERROR_NOT_SUPPORTED_YET;
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
}
}else
{
dcd_pipe_control_stall(coreid);
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
}
return TUSB_ERROR_NONE;
@@ -215,7 +211,7 @@ tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_int
p_hid->interface_number = p_interface_desc->bInterfaceNumber;
p_hid->report_length = p_desc_hid->wReportLength;
p_hid->ept_handle = dcd_pipe_open(coreid, p_desc_endpoint);
p_hid->ept_handle = dcd_pipe_open(coreid, p_desc_endpoint, p_interface_desc->bInterfaceClass);
ASSERT( endpointhandle_is_valid(p_hid->ept_handle), TUSB_ERROR_DCD_FAILED);
}
break;
@@ -233,6 +229,7 @@ tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_int
return TUSB_ERROR_NONE;
}
void hidd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
#if defined(CAP_DEVICE_ROMDRIVER) && TUSB_CFG_DEVICE_USE_ROM_DRIVER
#include "device/dcd_nxp_romdriver.h" // TODO remove rom driver dependency

View File

@@ -135,6 +135,7 @@ void tusbd_hid_mouse_isr(uint8_t coreid, tusb_event_t event);
tusb_error_t hidd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length);
tusb_error_t hidd_control_request(uint8_t coreid, tusb_control_request_t const * p_request);
void hidd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
#endif

View File

@@ -101,7 +101,7 @@ typedef ATTR_PACKED_STRUCT(struct) {
uint32_t signature ; ///< Signature that helps identify this data packet as a CBW. The signature field shall contain the value 43425355h (little endian), indicating a CBW.
uint32_t tag ; ///< Tag sent by the host. The device shall echo the contents of this field back to the host in the dCSWTagfield of the associated CSW. The dCSWTagpositively associates a CSW with the corresponding CBW.
uint32_t xfer_bytes ; ///< The number of bytes of data that the host expects to transfer on the Bulk-In or Bulk-Out endpoint (as indicated by the Directionbit) during the execution of this command. If this field is zero, the device and the host shall transfer no data between the CBW and the associated CSW, and the device shall ignore the value of the Directionbit in bmCBWFlags.
uint8_t flags ; ///< Bit 7 of this field define transfer direction \n - 0 : Data-Out from host to the device. \n - 1 : Data-In from the device to the host.
uint8_t dir ; ///< Bit 7 of this field define transfer direction \n - 0 : Data-Out from host to the device. \n - 1 : Data-In from the device to the host.
uint8_t lun ; ///< The device Logical Unit Number (LUN) to which the command block is being sent. For devices that support multiple LUNs, the host shall place into this field the LUN to which this command block is addressed. Otherwise, the host shall set this field to zero.
uint8_t cmd_len ; ///< The valid length of the CBWCBin bytes. This defines the valid length of the command block. The only legal values are 1 through 16
uint8_t command[16] ; ///< The command block to be executed by the device. The device shall interpret the first cmd_len bytes in this field as a command block

173
tinyusb/class/msc_device.c Normal file
View File

@@ -0,0 +1,173 @@
/**************************************************************************/
/*!
@file msc_device.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
#include "tusb_option.h"
#if (MODE_DEVICE_SUPPORTED && TUSB_CFG_DEVICE_MSC)
#define _TINY_USB_SOURCE_FILE_
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
#include "common/common.h"
#include "msc_device.h"
#include "tusb_descriptors.h"
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
typedef struct {
uint8_t interface_number;
endpoint_handle_t edpt_in, edpt_out;
// must be in USB ram
uint8_t max_lun;
msc_cmd_block_wrapper_t cbw;
msc_cmd_status_wrapper_t csw;
}mscd_interface_t;
STATIC_VAR mscd_interface_t mscd_data TUSB_CFG_ATTR_USBRAM;
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
//--------------------------------------------------------------------+
// USBD-CLASS API
//--------------------------------------------------------------------+
tusb_error_t mscd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length)
{
ASSERT( ( MSC_SUBCLASS_SCSI == p_interface_desc->bInterfaceSubClass &&
MSC_PROTOCOL_BOT == p_interface_desc->bInterfaceProtocol ), TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL );
mscd_interface_t * p_msc = &mscd_data;
//------------- Open Data Pipe -------------//
tusb_descriptor_endpoint_t const *p_endpoint = (tusb_descriptor_endpoint_t const *) descriptor_next( (uint8_t const*) p_interface_desc );
for(uint32_t i=0; i<2; i++)
{
ASSERT(TUSB_DESC_TYPE_ENDPOINT == p_endpoint->bDescriptorType &&
TUSB_XFER_BULK == p_endpoint->bmAttributes.xfer, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
endpoint_handle_t * p_edpt_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_DEV_TO_HOST_MASK ) ?
&p_msc->edpt_in : &p_msc->edpt_out;
(*p_edpt_hdl) = dcd_pipe_open(coreid, p_endpoint, p_interface_desc->bInterfaceClass);
ASSERT( endpointhandle_is_valid(*p_edpt_hdl), TUSB_ERROR_DCD_FAILED);
p_endpoint = (tusb_descriptor_endpoint_t const *) descriptor_next( (uint8_t const*) p_endpoint );
}
p_msc->interface_number = p_interface_desc->bInterfaceNumber;
(*p_length) += sizeof(tusb_descriptor_interface_t) + 2*sizeof(tusb_descriptor_endpoint_t);
//------------- Queue Endpoint OUT for Command Block Wrapper -------------//
ASSERT_STATUS( dcd_pipe_xfer(p_msc->edpt_out, &p_msc->cbw, sizeof(msc_cmd_block_wrapper_t), true) );
return TUSB_ERROR_NONE;
}
tusb_error_t mscd_control_request(uint8_t coreid, tusb_control_request_t const * p_request)
{
ASSERT(p_request->bmRequestType_bit.type == TUSB_REQUEST_TYPE_CLASS, TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT);
mscd_interface_t * p_msc = &mscd_data;
switch(p_request->bRequest)
{
case MSC_REQUEST_RESET:
dcd_pipe_control_xfer(coreid, TUSB_DIR_HOST_TO_DEV, NULL, 0);
break;
case MSC_REQUEST_GET_MAX_LUN:
dcd_pipe_control_xfer(coreid, TUSB_DIR_DEV_TO_HOST, &p_msc->max_lun, 1);
break;
default:
return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
}
return TUSB_ERROR_NONE;
}
void mscd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes)
{
// TODO failed --> STALL pipe, on clear STALL --> queue endpoint OUT
mscd_interface_t * p_msc = &mscd_data;
if ( endpointhandle_is_equal(p_msc->edpt_in, edpt_hdl) )
{
return; // currently no need to handle bulk in
}
ASSERT( endpointhandle_is_equal(p_msc->edpt_out, edpt_hdl) &&
xferred_bytes == sizeof(msc_cmd_block_wrapper_t) &&
event == TUSB_EVENT_XFER_COMPLETE &&
p_msc->cbw.signature == MSC_CBW_SIGNATURE, VOID_RETURN );
void *p_buffer = NULL;
p_msc->csw.signature = MSC_CSW_SIGNATURE;
p_msc->csw.tag = p_msc->cbw.tag;
p_msc->csw.status = tusbd_msc_scsi_received_isr(edpt_hdl.coreid, p_msc->cbw.lun, p_msc->cbw.command, &p_buffer, p_msc->cbw.xfer_bytes);
p_msc->csw.data_residue = 0; // TODO expected length, response length
//------------- Data Phase -------------//
if ( (p_msc->cbw.dir & BIT_(7)) && p_buffer == NULL )
{ // application does not provide data to response --> possibly unsupported SCSI command
ASSERT( TUSB_ERROR_NONE == dcd_pipe_stall(p_msc->edpt_in), VOID_RETURN );
}
if ( p_msc->cbw.dir & BIT_(7) )
{
ASSERT( dcd_pipe_queue_xfer(p_msc->edpt_in, p_buffer, p_msc->cbw.xfer_bytes) == TUSB_ERROR_NONE, VOID_RETURN);
} else
{
ASSERT( dcd_pipe_queue_xfer(p_msc->edpt_out, p_buffer, p_msc->cbw.xfer_bytes) == TUSB_ERROR_NONE, VOID_RETURN);
}
//------------- Status Phase -------------//
ASSERT( dcd_pipe_xfer( p_msc->edpt_in , &p_msc->csw, sizeof(msc_cmd_status_wrapper_t), true) == TUSB_ERROR_NONE, VOID_RETURN );
//------------- Queue the next CBW -------------//
// ASSERT( dcd_pipe_xfer( p_msc->edpt_out, &p_msc->cbw, sizeof(msc_cmd_block_wrapper_t), true) == TUSB_ERROR_NONE, VOID_RETURN );
}
#endif

View File

@@ -0,0 +1,72 @@
/**************************************************************************/
/*!
@file msc_device.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
#ifndef _TUSB_MSC_DEVICE_H_
#define _TUSB_MSC_DEVICE_H_
#include "common/common.h"
#include "device/usbd.h"
#include "msc.h"
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
msc_csw_status_t tusbd_msc_scsi_received_isr(uint8_t coreid, uint8_t lun, uint8_t scsi_cmd[16], void ** pp_buffer, uint16_t expected_length);
//--------------------------------------------------------------------+
// USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
#ifdef _TINY_USB_SOURCE_FILE_
tusb_error_t mscd_open(uint8_t coreid, tusb_descriptor_interface_t const * p_interface_desc, uint16_t *p_length);
tusb_error_t mscd_control_request(uint8_t coreid, tusb_control_request_t const * p_request);
void mscd_isr(endpoint_handle_t edpt_hdl, tusb_event_t event, uint32_t xferred_bytes);
#endif
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_MSC_DEVICE_H_ */
/** @} */

View File

@@ -58,7 +58,7 @@ STATIC_VAR msch_interface_t msch_data[TUSB_CFG_HOST_DEVICE_MAX] TUSB_CFG_ATTR_US
OSAL_SEM_DEF(msch_semaphore);
static osal_semaphore_handle_t msch_sem_hdl;
// buffer used to read scsi information when mounted, largest reponse data currently is inquiry
// buffer used to read scsi information when mounted, largest response data currently is inquiry
ATTR_ALIGNED(4) STATIC_VAR uint8_t msch_buffer[sizeof(scsi_inquiry_data_t)] TUSB_CFG_ATTR_USBRAM;
//--------------------------------------------------------------------+
@@ -125,7 +125,7 @@ static tusb_error_t msch_command_xfer(msch_interface_t * p_msch, void* p_buffer)
{
if ( NULL != p_buffer)
{ // there is data phase
if (p_msch->cbw.flags & TUSB_DIR_DEV_TO_HOST_MASK)
if (p_msch->cbw.dir & TUSB_DIR_DEV_TO_HOST_MASK)
{
ASSERT_STATUS( hcd_pipe_xfer(p_msch->bulk_out, (uint8_t*) &p_msch->cbw, sizeof(msc_cmd_block_wrapper_t), false) );
ASSERT_STATUS( hcd_pipe_queue_xfer(p_msch->bulk_in , p_buffer, p_msch->cbw.xfer_bytes) );
@@ -148,7 +148,7 @@ tusb_error_t tusbh_msc_inquiry(uint8_t dev_addr, uint8_t lun, uint8_t *p_data)
//------------- Command Block Wrapper -------------//
msc_cbw_add_signature(&p_msch->cbw, lun);
p_msch->cbw.xfer_bytes = sizeof(scsi_inquiry_data_t);
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.cmd_len = sizeof(scsi_inquiry_t);
//------------- SCSI command -------------//
@@ -172,7 +172,7 @@ tusb_error_t tusbh_msc_read_capacity10(uint8_t dev_addr, uint8_t lun, uint8_t *p
//------------- Command Block Wrapper -------------//
msc_cbw_add_signature(&p_msch->cbw, lun);
p_msch->cbw.xfer_bytes = sizeof(scsi_read_capacity10_data_t);
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.cmd_len = sizeof(scsi_read_capacity10_t);
//------------- SCSI command -------------//
@@ -196,7 +196,7 @@ tusb_error_t tusbh_msc_request_sense(uint8_t dev_addr, uint8_t lun, uint8_t *p_d
//------------- Command Block Wrapper -------------//
p_msch->cbw.xfer_bytes = 18;
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.cmd_len = sizeof(scsi_request_sense_t);
//------------- SCSI command -------------//
@@ -221,7 +221,7 @@ tusb_error_t tusbh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, msc_cmd_s
msc_cbw_add_signature(&p_msch->cbw, lun);
p_msch->cbw.xfer_bytes = 0; // Number of bytes
p_msch->cbw.flags = TUSB_DIR_HOST_TO_DEV;
p_msch->cbw.dir = TUSB_DIR_HOST_TO_DEV;
p_msch->cbw.cmd_len = sizeof(scsi_test_unit_ready_t);
//------------- SCSI command -------------//
@@ -248,7 +248,7 @@ tusb_error_t tusbh_msc_read10(uint8_t dev_addr, uint8_t lun, void * p_buffer, u
msc_cbw_add_signature(&p_msch->cbw, lun);
p_msch->cbw.xfer_bytes = p_msch->block_size*block_count; // Number of bytes
p_msch->cbw.flags = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.dir = TUSB_DIR_DEV_TO_HOST_MASK;
p_msch->cbw.cmd_len = sizeof(scsi_read10_t);
//------------- SCSI command -------------//
@@ -274,7 +274,7 @@ tusb_error_t tusbh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * p_buf
msc_cbw_add_signature(&p_msch->cbw, lun);
p_msch->cbw.xfer_bytes = p_msch->block_size*block_count; // Number of bytes
p_msch->cbw.flags = TUSB_DIR_HOST_TO_DEV;
p_msch->cbw.dir = TUSB_DIR_HOST_TO_DEV;
p_msch->cbw.cmd_len = sizeof(scsi_write10_t);
//------------- SCSI command -------------//
@@ -310,7 +310,7 @@ tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con
if (! ( MSC_SUBCLASS_SCSI == p_interface_desc->bInterfaceSubClass &&
MSC_PROTOCOL_BOT == p_interface_desc->bInterfaceProtocol ) )
{
return TUSB_ERROR_MSCH_UNSUPPORTED_PROTOCOL;
return TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL;
}
//------------- Open Data Pipe -------------//