diff options
| author | hathach <[email protected]> | 2013-09-24 18:23:34 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2013-09-24 18:23:34 +0700 |
| commit | bbfa85aa08d19f57f6e1c8bb0040839e38d9a01c (patch) | |
| tree | b79eac25722edca6eca467ae62b2506add73f7d7 /tinyusb | |
| parent | 63b776f7cf3bb7fe64c2f3b3e383a90b2c50683f (diff) | |
allow a limited model to deal with stalled pipe/transaction
added stall clear & able to mount the stupid toshiba thumb drive
add HCD pipe API
- bool hcd_pipe_is_busy(pipe_handle_t pipe_hdl);
- bool hcd_pipe_is_stalled(pipe_handle_t pipe_hdl);
- uint8_t hcd_pipe_get_endpoint_addr(pipe_handle_t pipe_hdl);
- tusb_error_t hcd_pipe_clear_stall(pipe_handle_t pipe_hdl);
remove tusbh_device_mount_failed_cb (not neccessary for user)
Diffstat (limited to 'tinyusb')
| -rw-r--r-- | tinyusb/class/cdc_host.c | 1 | ||||
| -rw-r--r-- | tinyusb/class/msc_host.c | 56 | ||||
| -rw-r--r-- | tinyusb/common/errors.h | 2 | ||||
| -rw-r--r-- | tinyusb/host/ehci/ehci.c | 41 | ||||
| -rw-r--r-- | tinyusb/host/hcd.h | 5 | ||||
| -rw-r--r-- | tinyusb/host/usbh.c | 10 | ||||
| -rw-r--r-- | tinyusb/osal/osal.h | 14 | ||||
| -rw-r--r-- | tinyusb/osal/osal_none.h | 3 |
8 files changed, 106 insertions, 26 deletions
diff --git a/tinyusb/class/cdc_host.c b/tinyusb/class/cdc_host.c index 15a232184..1fae8d66e 100644 --- a/tinyusb/class/cdc_host.c +++ b/tinyusb/class/cdc_host.c @@ -194,6 +194,7 @@ tusb_error_t cdch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con { tusb_descriptor_endpoint_t const *p_endpoint = (tusb_descriptor_endpoint_t const *) p_desc; ASSERT_INT(TUSB_DESC_TYPE_ENDPOINT, p_endpoint->bDescriptorType, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); + ASSERT_INT(TUSB_XFER_BULK, p_endpoint->bmAttributes.xfer, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_DEV_TO_HOST_MASK ) ? &p_cdc->pipe_in : &p_cdc->pipe_out; diff --git a/tinyusb/class/msc_host.c b/tinyusb/class/msc_host.c index 1e11a4b85..9d4c9a258 100644 --- a/tinyusb/class/msc_host.c +++ b/tinyusb/class/msc_host.c @@ -53,6 +53,10 @@ //--------------------------------------------------------------------+ STATIC_VAR msch_interface_t msch_data[TUSB_CFG_HOST_DEVICE_MAX] TUSB_CFG_ATTR_USBRAM; +//------------- Initalization Data -------------// +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 ATTR_ALIGNED(4) STATIC_VAR uint8_t msch_buffer[sizeof(scsi_inquiry_data_t)] TUSB_CFG_ATTR_USBRAM; @@ -166,6 +170,7 @@ static tusb_error_t scsi_command_send(msch_interface_t * p_msch, scsi_cmd_type_t void msch_init(void) { memclr_(msch_data, sizeof(msch_interface_t)*TUSB_CFG_HOST_DEVICE_MAX); + msch_sem_hdl = osal_semaphore_create( OSAL_SEM_REF(msch_semaphore) ); } tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t const *p_interface_desc, uint16_t *p_length) @@ -185,6 +190,7 @@ tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con for(uint32_t i=0; i<2; i++) { ASSERT_INT(TUSB_DESC_TYPE_ENDPOINT, p_endpoint->bDescriptorType, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); + ASSERT_INT(TUSB_XFER_BULK, p_endpoint->bmAttributes.xfer, TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED); pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_DEV_TO_HOST_MASK ) ? &msch_data[dev_addr-1].bulk_in : &msch_data[dev_addr-1].bulk_out; @@ -198,6 +204,7 @@ tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con msch_data[dev_addr-1].interface_number = p_interface_desc->bInterfaceNumber; (*p_length) += sizeof(tusb_descriptor_interface_t) + 2*sizeof(tusb_descriptor_endpoint_t); + //------------- Get Max Lun -------------// OSAL_SUBTASK_INVOKED_AND_WAIT( usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_DEV_TO_HOST, TUSB_REQUEST_TYPE_CLASS, TUSB_REQUEST_RECIPIENT_INTERFACE), @@ -219,26 +226,45 @@ tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con ); #endif - - // TODO timeout required, a proper synchronization -// while( !hcd_pipe_is_idle(msch_data[dev_addr-1].bulk_in) ) - + enum { SCSI_XFER_TIMEOUT = 1000 }; //------------- SCSI Inquiry -------------// scsi_command_send(&msch_data[dev_addr-1], SCSI_CMD_INQUIRY, 0, msch_buffer); - osal_task_delay(2); + osal_semaphore_wait(msch_sem_hdl, SCSI_XFER_TIMEOUT, &error); + SUBTASK_ASSERT_STATUS(error); memcpy(msch_data[dev_addr-1].vendor_id , ((scsi_inquiry_data_t*) msch_buffer)->vendor_id , 8); memcpy(msch_data[dev_addr-1].product_id, ((scsi_inquiry_data_t*) msch_buffer)->product_id, 16); -#if 0 - //------------- SCSI Request Sense -------------// - scsi_command_send(&msch_data[dev_addr-1], SCSI_CMD_REQUEST_SENSE, 0, msch_buffer); - osal_task_delay(2); -#endif - //------------- SCSI Read Capacity 10 -------------// scsi_command_send(&msch_data[dev_addr-1], SCSI_CMD_READ_CAPACITY_10, 0, msch_buffer); - osal_task_delay(2); + osal_semaphore_wait(msch_sem_hdl, SCSI_XFER_TIMEOUT, &error); + SUBTASK_ASSERT_STATUS(error); + + // NOTE: my toshiba thumbdrive stall the first Read Capacity and require the sequence + // Read Capacity --> Stalled --> Clear Stall --> Request Sense --> Read Capacity (2) to work + if ( hcd_pipe_is_stalled(msch_data[dev_addr-1].bulk_in) ) + { // clear stall TODO abtract clear stalll function + OSAL_SUBTASK_INVOKED_AND_WAIT( + usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_HOST_TO_DEV, TUSB_REQUEST_TYPE_STANDARD, TUSB_REQUEST_RECIPIENT_ENDPOINT), + TUSB_REQUEST_CLEAR_FEATURE, 0, hcd_pipe_get_endpoint_addr(msch_data[dev_addr-1].bulk_in), + 0, NULL ), + error + ); + + hcd_pipe_clear_stall(msch_data[dev_addr-1].bulk_in); + osal_semaphore_wait(msch_sem_hdl, SCSI_XFER_TIMEOUT, &error); + SUBTASK_ASSERT_STATUS(error); + + //------------- SCSI Request Sense -------------// + scsi_command_send(&msch_data[dev_addr-1], SCSI_CMD_REQUEST_SENSE, 0, msch_buffer); + osal_semaphore_wait(msch_sem_hdl, SCSI_XFER_TIMEOUT, &error); + SUBTASK_ASSERT_STATUS(error); + + //------------- Re-read SCSI Read Capactity -------------// + scsi_command_send(&msch_data[dev_addr-1], SCSI_CMD_READ_CAPACITY_10, 0, msch_buffer); + osal_semaphore_wait(msch_sem_hdl, SCSI_XFER_TIMEOUT, &error); + SUBTASK_ASSERT_STATUS(error); + } msch_data[dev_addr-1].last_lba = __be2le( ((scsi_read_capacity10_data_t*)msch_buffer)->last_lba ); msch_data[dev_addr-1].block_size = (uint16_t) __be2le( ((scsi_read_capacity10_data_t*)msch_buffer)->block_size ); @@ -252,7 +278,10 @@ tusb_error_t msch_open_subtask(uint8_t dev_addr, tusb_descriptor_interface_t con void msch_isr(pipe_handle_t pipe_hdl, tusb_event_t event, uint32_t xferred_bytes) { - + if ( pipehandle_is_equal(pipe_hdl, msch_data[pipe_hdl.dev_addr-1].bulk_in) ) + { + osal_semaphore_post(msch_sem_hdl); + } } void msch_close(uint8_t dev_addr) @@ -261,6 +290,7 @@ void msch_close(uint8_t dev_addr) (void) hcd_pipe_close(msch_data[dev_addr-1].bulk_out); memclr_(&msch_data[dev_addr-1], sizeof(msch_interface_t)); + osal_semaphore_reset(msch_sem_hdl); } //--------------------------------------------------------------------+ diff --git a/tinyusb/common/errors.h b/tinyusb/common/errors.h index 96681624c..77461f568 100644 --- a/tinyusb/common/errors.h +++ b/tinyusb/common/errors.h @@ -69,6 +69,8 @@ ENTRY(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND )\ ENTRY(TUSB_ERROR_USBH_MOUNT_CONFIG_DESC_TOO_LONG )\ ENTRY(TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED )\ + ENTRY(TUSB_ERROR_USBH_XFER_STALLED )\ + ENTRY(TUSB_ERROR_USBH_XFER_FAILED )\ ENTRY(TUSB_ERROR_OSAL_TIMEOUT )\ ENTRY(TUSB_ERROR_OSAL_WAITING ) /* only used by OSAL_NONE in the subtask */ \ ENTRY(TUSB_ERROR_OSAL_TASK_FAILED )\ diff --git a/tinyusb/host/ehci/ehci.c b/tinyusb/host/ehci/ehci.c index 40c59616e..684051917 100644 --- a/tinyusb/host/ehci/ehci.c +++ b/tinyusb/host/ehci/ehci.c @@ -100,6 +100,13 @@ static inline ehci_qhd_t* qhd_find_free (uint8_t dev_addr) ATTR_PURE ATTR_ALW static inline tusb_xfer_type_t qhd_get_xfer_type(ehci_qhd_t const * p_qhd) ATTR_ALWAYS_INLINE ATTR_PURE; STATIC_ INLINE_ ehci_qhd_t* qhd_get_from_pipe_handle(pipe_handle_t pipe_hdl) ATTR_PURE ATTR_ALWAYS_INLINE; static inline pipe_handle_t qhd_create_pipe_handle(ehci_qhd_t const * p_qhd, tusb_xfer_type_t xfer_type) ATTR_PURE ATTR_ALWAYS_INLINE; +// determine if a queue head has bus-related error +static inline bool qhd_has_xact_error(ehci_qhd_t * p_qhd) ATTR_ALWAYS_INLINE ATTR_PURE; +static inline bool qhd_has_xact_error(ehci_qhd_t * p_qhd) +{ + return ( p_qhd->qtd_overlay.buffer_err ||p_qhd->qtd_overlay.babble_err || p_qhd->qtd_overlay.xact_err ); + //p_qhd->qtd_overlay.non_hs_period_missed_uframe || p_qhd->qtd_overlay.pingstate_err TODO split transaction error +} static void qhd_init(ehci_qhd_t *p_qhd, uint8_t dev_addr, uint16_t max_packet_size, uint8_t endpoint_addr, uint8_t xfer_type, uint8_t interval); @@ -468,12 +475,38 @@ tusb_error_t hcd_pipe_close(pipe_handle_t pipe_hdl) return TUSB_ERROR_NONE; } +bool hcd_pipe_is_busy(pipe_handle_t pipe_hdl) +{ + ehci_qhd_t *p_qhd = qhd_get_from_pipe_handle( pipe_hdl ); + return !p_qhd->qtd_overlay.halted && (p_qhd->p_qtd_list_head != NULL); +} + bool hcd_pipe_is_idle(pipe_handle_t pipe_hdl) { ehci_qhd_t *p_qhd = qhd_get_from_pipe_handle( pipe_hdl ); return (p_qhd->p_qtd_list_head == NULL); } +bool hcd_pipe_is_stalled(pipe_handle_t pipe_hdl) +{ + ehci_qhd_t *p_qhd = qhd_get_from_pipe_handle( pipe_hdl ); + return p_qhd->qtd_overlay.halted && !qhd_has_xact_error(p_qhd); +} + +uint8_t hcd_pipe_get_endpoint_addr(pipe_handle_t pipe_hdl) +{ + ehci_qhd_t *p_qhd = qhd_get_from_pipe_handle( pipe_hdl ); + return p_qhd->endpoint_number + ( (p_qhd->pid_non_control == EHCI_PID_IN) ? 0x80 : 0); +} + +tusb_error_t hcd_pipe_clear_stall(pipe_handle_t pipe_hdl) +{ + ehci_qhd_t *p_qhd = qhd_get_from_pipe_handle( pipe_hdl ); + p_qhd->qtd_overlay.halted = 0; + + return TUSB_ERROR_NONE; +} + //--------------------------------------------------------------------+ // EHCI Interrupt Handler //--------------------------------------------------------------------+ @@ -617,16 +650,14 @@ static void period_list_xfer_complete_isr(uint8_t hostid, uint8_t interval_ms) static void qhd_xfer_error_isr(ehci_qhd_t * p_qhd) { - if ( (p_qhd->device_address != 0 && p_qhd->qtd_overlay.halted) || // addr0 cannot be protocol STALL - p_qhd->qtd_overlay.buffer_err ||p_qhd->qtd_overlay.babble_err || p_qhd->qtd_overlay.xact_err ) - //p_qhd->qtd_overlay.non_hs_period_missed_uframe || p_qhd->qtd_overlay.pingstate_err TODO split transaction error + if ( (p_qhd->device_address != 0 && p_qhd->qtd_overlay.halted) || // addr0 cannot be protocol STALL + qhd_has_xact_error(p_qhd) ) { // current qhd has error in transaction tusb_xfer_type_t const xfer_type = qhd_get_xfer_type(p_qhd); tusb_event_t error_event; // no error bits are set, endpoint is halted due to STALL - error_event = ( !(p_qhd->qtd_overlay.buffer_err || p_qhd->qtd_overlay.babble_err || - p_qhd->qtd_overlay.xact_err) ) ? TUSB_EVENT_XFER_STALLED : TUSB_EVENT_XFER_ERROR; + error_event = qhd_has_xact_error(p_qhd) ? TUSB_EVENT_XFER_ERROR : TUSB_EVENT_XFER_STALLED; p_qhd->total_xferred_bytes += p_qhd->p_qtd_list_head->expected_bytes - p_qhd->p_qtd_list_head->total_bytes; diff --git a/tinyusb/host/hcd.h b/tinyusb/host/hcd.h index 61642d95b..6383015a8 100644 --- a/tinyusb/host/hcd.h +++ b/tinyusb/host/hcd.h @@ -98,7 +98,12 @@ pipe_handle_t hcd_pipe_open(uint8_t dev_addr, tusb_descriptor_endpoint_t const * tusb_error_t hcd_pipe_queue_xfer(pipe_handle_t pipe_hdl, uint8_t buffer[], uint16_t total_bytes) ATTR_WARN_UNUSED_RESULT; // only queue, not transferring yet tusb_error_t hcd_pipe_xfer(pipe_handle_t pipe_hdl, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete) ATTR_WARN_UNUSED_RESULT; tusb_error_t hcd_pipe_close(pipe_handle_t pipe_hdl) /*ATTR_WARN_UNUSED_RESULT*/; + +bool hcd_pipe_is_busy(pipe_handle_t pipe_hdl); +bool hcd_pipe_is_stalled(pipe_handle_t pipe_hdl); bool hcd_pipe_is_idle(pipe_handle_t pipe_hdl); +uint8_t hcd_pipe_get_endpoint_addr(pipe_handle_t pipe_hdl); +tusb_error_t hcd_pipe_clear_stall(pipe_handle_t pipe_hdl); #if 0 tusb_error_t hcd_pipe_cancel()ATTR_WARN_UNUSED_RESULT; diff --git a/tinyusb/host/usbh.c b/tinyusb/host/usbh.c index 67cc8f96c..65efe8768 100644 --- a/tinyusb/host/usbh.c +++ b/tinyusb/host/usbh.c @@ -216,9 +216,13 @@ tusb_error_t usbh_control_xfer_subtask(uint8_t dev_addr, uint8_t bmRequestType, osal_mutex_release(usbh_devices[dev_addr].control.mutex_hdl); // TODO make handler for this function general purpose - SUBTASK_ASSERT_WITH_HANDLER(TUSB_ERROR_NONE == error && - TUSB_EVENT_XFER_COMPLETE == usbh_devices[dev_addr].control.pipe_status, - tusbh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL) ); + if (TUSB_ERROR_NONE != error) SUBTASK_EXIT(error); + if (TUSB_EVENT_XFER_STALLED == usbh_devices[dev_addr].control.pipe_status) SUBTASK_EXIT(TUSB_ERROR_USBH_XFER_STALLED); + if (TUSB_EVENT_XFER_ERROR == usbh_devices[dev_addr].control.pipe_status) SUBTASK_EXIT(TUSB_ERROR_USBH_XFER_FAILED); + +// SUBTASK_ASSERT_WITH_HANDLER(TUSB_ERROR_NONE == error && +// TUSB_EVENT_XFER_COMPLETE == usbh_devices[dev_addr].control.pipe_status, +// tusbh_device_mount_failed_cb(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND, NULL) ); OSAL_SUBTASK_END } diff --git a/tinyusb/osal/osal.h b/tinyusb/osal/osal.h index d2287b3a5..d23cb518c 100644 --- a/tinyusb/osal/osal.h +++ b/tinyusb/osal/osal.h @@ -68,16 +68,20 @@ #if TUSB_CFG_OS == TUSB_OS_NONE #include "osal_none.h" -#elif TUSB_CFG_OS == TUSB_OS_FREERTOS - #include "osal_freeRTOS.h" -#elif TUSB_CFG_OS == TUSB_OS_CMSIS_RTX - #include "osal_cmsis_rtx.h" #else - #error TUSB_CFG_OS is not defined or OS is not supported yet + #define OSAL_VAR + #if TUSB_CFG_OS == TUSB_OS_FREERTOS + #include "osal_freeRTOS.h" + #elif TUSB_CFG_OS == TUSB_OS_CMSIS_RTX + #include "osal_cmsis_rtx.h" + #else + #error TUSB_CFG_OS is not defined or OS is not supported yet + #endif #endif #else // OSAL API for cmock +#define OSAL_VAR #include "osal_common.h" //------------- Tick -------------// diff --git a/tinyusb/osal/osal_none.h b/tinyusb/osal/osal_none.h index 853f33e67..775febc3f 100644 --- a/tinyusb/osal/osal_none.h +++ b/tinyusb/osal/osal_none.h @@ -58,6 +58,9 @@ extern "C" { #endif +// variable that retains value after OSAL blocking service need to declare with OSAL_VAR +#define OSAL_VAR static + //--------------------------------------------------------------------+ // TICK API //--------------------------------------------------------------------+ |
