diff options
| author | hathach <[email protected]> | 2018-11-20 11:06:16 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-11-20 11:06:16 +0700 |
| commit | aa71b8fd874901347dedf4046a74d686907cf147 (patch) | |
| tree | bcc958c04628507c72879ead57766d0bc732d588 /src/device | |
| parent | 5757918df4a3cc4561775db61d9f720df8b23c31 (diff) | |
| parent | 5e65886fdd2bc5bac4ee9335fff1726f9382c109 (diff) | |
Merge pull request #10 from hathach/devlocal
merge for local develop
Diffstat (limited to 'src/device')
| -rw-r--r-- | src/device/control.c | 264 | ||||
| -rw-r--r-- | src/device/control.h | 98 | ||||
| -rw-r--r-- | src/device/dcd.h | 4 | ||||
| -rw-r--r-- | src/device/usbd.c | 257 | ||||
| -rw-r--r-- | src/device/usbd_control.c | 170 | ||||
| -rw-r--r-- | src/device/usbd_pvt.h | 17 |
6 files changed, 382 insertions, 428 deletions
diff --git a/src/device/control.c b/src/device/control.c deleted file mode 100644 index 26dd8fb35..000000000 --- a/src/device/control.c +++ /dev/null @@ -1,264 +0,0 @@ -/**************************************************************************/
-/*!
- @file usbd.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 TUSB_OPT_DEVICE_ENABLED
-
-#define _TINY_USB_SOURCE_FILE_
-
-#include "tusb.h"
-#include "control.h"
-#include "device/usbd_pvt.h"
-
-control_t control_state;
-
-CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN uint8_t _shared_control_buffer[64];
-
-void controld_reset(uint8_t rhport) {
- control_state.current_stage = CONTROL_STAGE_SETUP;
-}
-
-void controld_init(void) {
-}
-
-// Helper to send STATUS (zero length) packet
-// Note dir is value of direction bit in setup packet (i.e DATA stage direction)
-static inline bool dcd_control_status(uint8_t rhport, uint8_t dir)
-{
- uint8_t ep_addr = 0;
- // Invert the direction.
- if (dir == TUSB_DIR_OUT) {
- ep_addr |= TUSB_DIR_IN_MASK;
- }
- // status direction is reversed to one in the setup packet
- return dcd_edpt_xfer(rhport, ep_addr, NULL, 0);
-}
-
-static inline void dcd_control_stall(uint8_t rhport)
-{
- dcd_edpt_stall(rhport, 0 | TUSB_DIR_IN_MASK);
-}
-
-
-// return len of descriptor and change pointer to descriptor's buffer
-static uint16_t get_descriptor(uint8_t rhport, tusb_control_request_t const * const p_request, uint8_t const ** pp_buffer)
-{
- (void) rhport;
-
- tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
- uint8_t const desc_index = tu_u16_low( p_request->wValue );
-
- uint8_t const * desc_data = NULL ;
- uint16_t len = 0;
-
- switch(desc_type)
- {
- case TUSB_DESC_DEVICE:
- desc_data = (uint8_t const *) usbd_desc_set->device;
- len = sizeof(tusb_desc_device_t);
- break;
-
- case TUSB_DESC_CONFIGURATION:
- desc_data = (uint8_t const *) usbd_desc_set->config;
- len = ((tusb_desc_configuration_t const*) desc_data)->wTotalLength;
- break;
-
- case TUSB_DESC_STRING:
- // String Descriptor always uses the desc set from user
- if ( desc_index < tud_desc_set.string_count )
- {
- desc_data = tud_desc_set.string_arr[desc_index];
- TU_VERIFY( desc_data != NULL, 0 );
-
- len = desc_data[0]; // first byte of descriptor is its size
- }else
- {
- // out of range
- /* The 0xee string is indeed a Microsoft USB extension.
- * It can be used to tell Windows what driver it should use for the device !!!
- */
- return 0;
- }
- break;
-
- case TUSB_DESC_DEVICE_QUALIFIER:
- // TODO If not highspeed capable stall this request otherwise
- // return the descriptor that could work in highspeed
- return 0;
- break;
-
- default: return 0;
- }
-
- TU_ASSERT( desc_data != NULL, 0);
-
- // up to Host's length
- len = tu_min16(p_request->wLength, len );
- (*pp_buffer) = desc_data;
-
- return len;
-}
-
-tusb_error_t controld_xfer_cb(uint8_t rhport, uint8_t edpt_addr, tusb_event_t event, uint32_t xferred_bytes) {
- if (control_state.current_stage == CONTROL_STAGE_STATUS && xferred_bytes == 0) {
- control_state.current_stage = CONTROL_STAGE_SETUP;
- return TUSB_ERROR_NONE;
- }
- tusb_error_t error = TUSB_ERROR_NONE;
- control_state.total_transferred += xferred_bytes;
- tusb_control_request_t const *p_request = &control_state.current_request;
-
- if (p_request->wLength == control_state.total_transferred || xferred_bytes < 64) {
- control_state.current_stage = CONTROL_STAGE_STATUS;
- dcd_control_status(rhport, p_request->bmRequestType_bit.direction);
-
- // Do the user callback after queueing the STATUS packet because the callback could be slow.
- if ( TUSB_REQ_RCPT_INTERFACE == p_request->bmRequestType_bit.recipient )
- {
- tud_control_interface_control_complete_cb(rhport, tu_u16_low(p_request->wIndex), p_request);
- }
- } else {
- if (TUSB_REQ_RCPT_INTERFACE == p_request->bmRequestType_bit.recipient) {
- error = tud_control_interface_control_cb(rhport, tu_u16_low(p_request->wIndex), p_request, control_state.total_transferred);
- } else {
- error = controld_process_control_request(rhport, p_request, control_state.total_transferred);
- }
- }
- return error;
-}
-
-// This tracks the state of a control request.
-tusb_error_t controld_process_setup_request(uint8_t rhport, tusb_control_request_t const * p_request) {
- tusb_error_t error = TUSB_ERROR_NONE;
- memcpy(&control_state.current_request, p_request, sizeof(tusb_control_request_t));
- if (p_request->wLength == 0) {
- control_state.current_stage = CONTROL_STAGE_STATUS;
- } else {
- control_state.current_stage = CONTROL_STAGE_DATA;
- control_state.total_transferred = 0;
- }
-
- if ( TUSB_REQ_RCPT_INTERFACE == p_request->bmRequestType_bit.recipient )
- {
- error = tud_control_interface_control_cb(rhport, tu_u16_low(p_request->wIndex), p_request, 0);
- } else {
- error = controld_process_control_request(rhport, p_request, 0);
- }
-
- if (error != TUSB_ERROR_NONE) {
- dcd_control_stall(rhport); // Stall errored requests
- } else if (control_state.current_stage == CONTROL_STAGE_STATUS) {
- dcd_control_status(rhport, p_request->bmRequestType_bit.direction);
- }
- return error;
-}
-
-// This handles the actual request and its response.
-tusb_error_t controld_process_control_request(uint8_t rhport, tusb_control_request_t const * p_request, uint16_t bytes_already_sent)
-{
- tusb_error_t error = TUSB_ERROR_NONE;
- uint8_t ep_addr = 0;
- if (p_request->bmRequestType_bit.direction == TUSB_DIR_IN) {
- ep_addr |= TUSB_DIR_IN_MASK;
- }
-
- //------------- Standard Request e.g in enumeration -------------//
- if( TUSB_REQ_RCPT_DEVICE == p_request->bmRequestType_bit.recipient &&
- TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type ) {
- switch (p_request->bRequest) {
- case TUSB_REQ_GET_DESCRIPTOR: {
- uint8_t const * buffer = NULL;
- uint16_t const len = get_descriptor(rhport, p_request, &buffer);
-
- if (len) {
- uint16_t remaining_bytes = len - bytes_already_sent;
- if (remaining_bytes > 64) {
- remaining_bytes = 64;
- }
- memcpy(_shared_control_buffer, buffer + bytes_already_sent, remaining_bytes);
- dcd_edpt_xfer(rhport, ep_addr, _shared_control_buffer, remaining_bytes);
- } else {
- return TUSB_ERROR_FAILED;
- }
- break;
- }
- case TUSB_REQ_GET_CONFIGURATION:
- memcpy(_shared_control_buffer, &control_state.config, 1);
- dcd_edpt_xfer(rhport, ep_addr, _shared_control_buffer, 1);
- break;
- case TUSB_REQ_SET_ADDRESS:
- dcd_set_address(rhport, (uint8_t) p_request->wValue);
- break;
- case TUSB_REQ_SET_CONFIGURATION:
- control_state.config = p_request->wValue;
- tud_control_set_config_cb (rhport, control_state.config);
- break;
- default:
- return TUSB_ERROR_FAILED;
- }
- } else if (p_request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_ENDPOINT &&
- p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) {
- //------------- Endpoint Request -------------//
- switch (p_request->bRequest) {
- case TUSB_REQ_GET_STATUS: {
- uint16_t status = dcd_edpt_stalled(rhport, tu_u16_low(p_request->wIndex)) ? 0x0001 : 0x0000;
- memcpy(_shared_control_buffer, &status, 2);
-
- dcd_edpt_xfer(rhport, ep_addr, _shared_control_buffer, 2);
- break;
- }
- case TUSB_REQ_CLEAR_FEATURE:
- // only endpoint feature is halted/stalled
- dcd_edpt_clear_stall(rhport, tu_u16_low(p_request->wIndex));
- break;
- case TUSB_REQ_SET_FEATURE:
- // only endpoint feature is halted/stalled
- dcd_edpt_stall(rhport, tu_u16_low(p_request->wIndex));
- break;
- default:
- return TUSB_ERROR_FAILED;
- }
- } else {
- //------------- Unsupported Request -------------//
- return TUSB_ERROR_FAILED;
- }
- return error;
-}
-
-#endif
diff --git a/src/device/control.h b/src/device/control.h deleted file mode 100644 index 633ccb60c..000000000 --- a/src/device/control.h +++ /dev/null @@ -1,98 +0,0 @@ -/**************************************************************************/
-/*!
- @file usbd.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.
-*/
-/**************************************************************************/
-
-/** \ingroup group_usbd
- * @{ */
-
-#ifndef _TUSB_CONTROL_H_
-#define _TUSB_CONTROL_H_
-
-#ifdef __cplusplus
- extern "C" {
-#endif
-
-#include "tusb.h"
-
-typedef enum {
- CONTROL_STAGE_SETUP, // Waiting for a setup token.
- CONTROL_STAGE_DATA, // In the process of sending or receiving data.
- CONTROL_STAGE_STATUS // In the process of transmitting the STATUS ZLP.
-} control_stage_t;
-
-typedef struct {
- control_stage_t current_stage;
- tusb_control_request_t current_request;
- uint16_t total_transferred;
- uint8_t config;
-} control_t;
-
-extern uint8_t _shared_control_buffer[64];
-
-tusb_error_t controld_process_setup_request(uint8_t rhport, tusb_control_request_t const * const p_request);
-
-// Callback when the configuration of the device is changed.
-tusb_error_t tud_control_set_config_cb(uint8_t rhport, uint8_t config_number);
-
-// Called when the DATA stage of a control transaction is complete.
-void tud_control_interface_control_complete_cb(uint8_t rhport, uint8_t interface, tusb_control_request_t const * const p_request);
-
-tusb_error_t tud_control_interface_control_cb(uint8_t rhport, uint8_t interface, tusb_control_request_t const * const p_request, uint16_t bytes_already_sent);
-
-//--------------------------------------------------------------------+
-// INTERNAL API
-//--------------------------------------------------------------------+
-
-void controld_init(void);
-tusb_error_t controld_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length);
-
-// This tracks the state of a control request.
-tusb_error_t controld_process_setup_request(uint8_t rhport, tusb_control_request_t const * p_request);
-
-// This handles the actual request and its response.
-tusb_error_t controld_process_control_request(uint8_t rhport, tusb_control_request_t const * p_request, uint16_t bytes_already_sent);
-
-tusb_error_t controld_xfer_cb(uint8_t rhport, uint8_t edpt_addr, tusb_event_t event, uint32_t xferred_bytes);
-void controld_reset(uint8_t rhport);
-
-#ifdef __cplusplus
- }
-#endif
-
-#endif /* _TUSB_CONTROL_H_ */
-
-/** @} */
diff --git a/src/device/dcd.h b/src/device/dcd.h index ff836f405..bd0ea852e 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -124,6 +124,10 @@ void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_ /*------------------------------------------------------------------*/
/* Endpoint API
+ * Note:
+ * - Address of control endpoint OUT is 0x00, In is 0x80
+ * - When stalling control endpoint both control OUT and IN must be stalled
+ * (according to USB spec, stalled control is only recovered with setup token)
*------------------------------------------------------------------*/
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
diff --git a/src/device/usbd.c b/src/device/usbd.c index 451494633..eac1a6a20 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -44,7 +44,6 @@ #define _TINY_USB_SOURCE_FILE_
-#include "control.h"
#include "tusb.h"
#include "usbd.h"
#include "device/usbd_pvt.h"
@@ -68,16 +67,13 @@ typedef struct {
uint8_t config_num;
- // map interface number to driver (0xff is invalid)
- uint8_t itf2drv[16];
+ uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
+ uint8_t ep2drv[2][8]; // map endpoint to driver ( 0xff is invalid )
- // map endpoint to driver ( 0xff is invalid )
- uint8_t ep2drv[2][8];
}usbd_device_t;
static usbd_device_t _usbd_dev;
-
// Auto descriptor is enabled, descriptor set point to auto generated one
#if CFG_TUD_DESC_AUTO
extern tud_desc_set_t const _usbd_auto_desc_set;
@@ -94,9 +90,8 @@ typedef struct { void (* init ) (void);
tusb_error_t (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length);
- // Control request is called one or more times for a request and can queue multiple data packets.
- tusb_error_t (* control_request ) (uint8_t rhport, tusb_control_request_t const *, uint16_t bytes_already_sent);
- void (* control_request_complete ) (uint8_t rhport, tusb_control_request_t const *);
+ bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request);
+ bool (* control_request_complete ) (uint8_t rhport, tusb_control_request_t const * request);
tusb_error_t (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, tusb_event_t, uint32_t);
void (* sof ) (uint8_t rhport);
void (* reset ) (uint8_t);
@@ -104,16 +99,6 @@ typedef struct { static usbd_class_driver_t const usbd_class_drivers[] =
{
- {
- .class_code = TUSB_CLASS_UNSPECIFIED,
- .init = controld_init,
- .open = NULL,
- .control_request = NULL,
- .control_request_complete = NULL,
- .xfer_cb = controld_xfer_cb,
- .sof = NULL,
- .reset = controld_reset
- },
#if CFG_TUD_CDC
{
.class_code = TUSB_CLASS_CDC,
@@ -181,9 +166,16 @@ OSAL_QUEUE_DEF(_usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t); static osal_queue_t _usbd_q;
//--------------------------------------------------------------------+
-// INTERNAL FUNCTION
+// Prototypes
//--------------------------------------------------------------------+
static void mark_interface_endpoint(uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
+static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
+static bool process_set_config(uint8_t rhport, uint8_t config_number);
+static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len);
+
+void usbd_control_reset (uint8_t rhport);
+tusb_error_t usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes);
+void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) );
//--------------------------------------------------------------------+
// APPLICATION API
@@ -194,7 +186,7 @@ bool tud_mounted(void) }
//--------------------------------------------------------------------+
-// IMPLEMENTATION
+// USBD Task
//--------------------------------------------------------------------+
tusb_error_t usbd_init (void)
{
@@ -223,9 +215,8 @@ static void usbd_reset(uint8_t rhport) tu_varclr(&_usbd_dev);
memset(_usbd_dev.itf2drv, 0xff, sizeof(_usbd_dev.itf2drv)); // invalid mapping
memset(_usbd_dev.ep2drv , 0xff, sizeof(_usbd_dev.ep2drv )); // invalid mapping
- // Always map the 0th endpoint to the control driver.
- _usbd_dev.ep2drv[TUSB_DIR_IN][0] = 0;
- _usbd_dev.ep2drv[TUSB_DIR_OUT][0] = 0;
+
+ usbd_control_reset(rhport);
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
{
@@ -233,30 +224,41 @@ static void usbd_reset(uint8_t rhport) }
}
+// Main device task implementation
static void usbd_task_body(void)
{
- dcd_event_t event;
-
// Loop until there is no more events in the queue
while (1)
{
+ dcd_event_t event;
+
if ( !osal_queue_receive(_usbd_q, &event) ) return;
switch ( event.event_id )
{
case DCD_EVENT_SETUP_RECEIVED:
- // Setup tokens are unique to the Control endpoint so we delegate to it directly.
- controld_process_setup_request(event.rhport, &event.setup_received);
+ // Process control request, if failed control endpoint is stalled
+ if ( !process_control_request(event.rhport, &event.setup_received) )
+ {
+ usbd_control_stall(event.rhport);
+ }
break;
case DCD_EVENT_XFER_COMPLETE:
{
// Invoke the class callback associated with the endpoint address
uint8_t const ep_addr = event.xfer_complete.ep_addr;
- uint8_t const drv_id = _usbd_dev.ep2drv[edpt_dir(ep_addr)][edpt_number(ep_addr)];
- if ( drv_id < USBD_CLASS_DRIVER_COUNT )
+ if ( 0 == edpt_number(ep_addr) )
{
+ // control transfer DATA stage callback
+ usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
+ }
+ else
+ {
+ uint8_t const drv_id = _usbd_dev.ep2drv[edpt_dir(ep_addr)][edpt_number(ep_addr)];
+ TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
+
usbd_class_drivers[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
}
}
@@ -316,66 +318,142 @@ void usbd_task( void* param) #endif
}
-void tud_control_interface_control_complete_cb(uint8_t rhport, uint8_t interface, tusb_control_request_t const * const p_request) {
- if (_usbd_dev.itf2drv[ interface ] < USBD_CLASS_DRIVER_COUNT)
+//--------------------------------------------------------------------+
+// Control Request Parser & Handling
+//--------------------------------------------------------------------+
+
+// This handles the actual request and its response.
+// return false will cause its caller to stall control endpoint
+static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request)
+{
+ usbd_control_set_complete_callback(NULL);
+
+ if ( TUSB_REQ_RCPT_DEVICE == p_request->bmRequestType_bit.recipient &&
+ TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type )
+ {
+ //------------- Standard Device Requests e.g in enumeration -------------//
+ void* data_buf = NULL;
+ uint16_t data_len = 0;
+
+ switch ( p_request->bRequest )
{
- const usbd_class_driver_t *driver = &usbd_class_drivers[_usbd_dev.itf2drv[interface]];
- if (driver->control_request_complete != NULL) {
- driver->control_request_complete(rhport, p_request);
+ case TUSB_REQ_SET_ADDRESS:
+ dcd_set_address(rhport, (uint8_t) p_request->wValue);
+ break;
+
+ case TUSB_REQ_GET_CONFIGURATION:
+ data_buf = &_usbd_dev.config_num;
+ data_len = 1;
+ break;
+
+ case TUSB_REQ_SET_CONFIGURATION:
+ {
+ uint8_t const config = (uint8_t) p_request->wValue;
+
+ dcd_set_config(rhport, config);
+ _usbd_dev.config_num = config;
+
+ TU_ASSERT( TUSB_ERROR_NONE == process_set_config(rhport, config) );
}
+ break;
+
+ case TUSB_REQ_GET_DESCRIPTOR:
+ data_buf = (void*) get_descriptor(p_request, &data_len);
+ if ( data_buf == NULL || data_len == 0 ) return false;
+ break;
+
+ default: return false;
}
-}
-tusb_error_t tud_control_interface_control_cb(uint8_t rhport, uint8_t interface, tusb_control_request_t const * const p_request, uint16_t bytes_already_sent) {
- if (_usbd_dev.itf2drv[ interface ] < USBD_CLASS_DRIVER_COUNT)
+ usbd_control_xfer(rhport, p_request, data_buf, data_len);
+ }
+ else if ( TUSB_REQ_RCPT_INTERFACE == p_request->bmRequestType_bit.recipient )
+ {
+ //------------- Class/Interface Specific Request -------------//
+ uint8_t const itf = tu_u16_low(p_request->wIndex);
+ uint8_t const drvid = _usbd_dev.itf2drv[ itf ];
+
+ TU_VERIFY (drvid < USBD_CLASS_DRIVER_COUNT );
+
+ usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_request_complete );
+
+ // control endpoint will be stalled if driver return false
+ return usbd_class_drivers[drvid].control_request(rhport, p_request);
+ }
+ else if ( p_request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_ENDPOINT &&
+ p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD )
+ {
+ //------------- Endpoint Request -------------//
+ switch ( p_request->bRequest )
{
- return usbd_class_drivers[_usbd_dev.itf2drv[interface]].control_request(rhport, p_request, bytes_already_sent);
+ case TUSB_REQ_GET_STATUS:
+ {
+ uint16_t status = dcd_edpt_stalled(rhport, tu_u16_low(p_request->wIndex)) ? 0x0001 : 0x0000;
+ usbd_control_xfer(rhport, p_request, &status, 2);
+ }
+ break;
+
+ case TUSB_REQ_CLEAR_FEATURE:
+ // only endpoint feature is halted/stalled
+ dcd_edpt_clear_stall(rhport, tu_u16_low(p_request->wIndex));
+ usbd_control_status(rhport, p_request);
+ break;
+
+ case TUSB_REQ_SET_FEATURE:
+ // only endpoint feature is halted/stalled
+ dcd_edpt_stall(rhport, tu_u16_low(p_request->wIndex));
+ usbd_control_status(rhport, p_request);
+ break;
+
+ default: return false;
}
- return TUSB_ERROR_FAILED;
+ }
+ else
+ {
+ //------------- Unsupported Request -------------//
+ return false;
+ }
+
+ return true;
}
// Process Set Configure Request
-// TODO Host (windows) can get HID report descriptor before set configured
-// may need to open interface before set configured
-tusb_error_t tud_control_set_config_cb(uint8_t rhport, uint8_t config_number)
+// This function parse configuration descriptor & open drivers accordingly
+static bool process_set_config(uint8_t rhport, uint8_t config_number)
{
- dcd_set_config(rhport, config_number);
-
- _usbd_dev.config_num = config_number;
-
- //------------- parse configuration & open drivers -------------//
uint8_t const * desc_cfg = (uint8_t const *) usbd_desc_set->config;
- TU_ASSERT(desc_cfg != NULL, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
+ TU_ASSERT(desc_cfg != NULL);
+
uint8_t const * p_desc = desc_cfg + sizeof(tusb_desc_configuration_t);
uint16_t const cfg_len = ((tusb_desc_configuration_t*)desc_cfg)->wTotalLength;
while( p_desc < desc_cfg + cfg_len )
{
+ // Each interface always starts with Interface or Association descriptor
if ( TUSB_DESC_INTERFACE_ASSOCIATION == descriptor_type(p_desc) )
{
p_desc = descriptor_next(p_desc); // ignore Interface Association
}else
{
- TU_ASSERT( TUSB_DESC_INTERFACE == descriptor_type(p_desc), TUSB_ERROR_NOT_SUPPORTED_YET );
+ TU_ASSERT( TUSB_DESC_INTERFACE == descriptor_type(p_desc) );
- tusb_desc_interface_t* p_desc_itf = (tusb_desc_interface_t*) p_desc;
- uint8_t const class_code = p_desc_itf->bInterfaceClass;
+ tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) p_desc;
// Check if class is supported
uint8_t drv_id;
for (drv_id = 0; drv_id < USBD_CLASS_DRIVER_COUNT; drv_id++)
{
- if ( usbd_class_drivers[drv_id].class_code == class_code ) break;
+ if ( usbd_class_drivers[drv_id].class_code == desc_itf->bInterfaceClass ) break;
}
- TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT, TUSB_ERROR_NOT_SUPPORTED_YET );
+ TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT ); // unsupported class
- // Interface number must not be used
- TU_ASSERT( 0xff == _usbd_dev.itf2drv[p_desc_itf->bInterfaceNumber], TUSB_ERROR_FAILED);
- _usbd_dev.itf2drv[p_desc_itf->bInterfaceNumber] = drv_id;
+ // Interface number must not be used already TODO alternate interface
+ TU_ASSERT( 0xff == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] );
+ _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
uint16_t len=0;
- TU_ASSERT_ERR( usbd_class_drivers[drv_id].open( rhport, p_desc_itf, &len ) );
- TU_ASSERT( len >= sizeof(tusb_desc_interface_t), TUSB_ERROR_FAILED );
+ TU_ASSERT_ERR( usbd_class_drivers[drv_id].open( rhport, desc_itf, &len ), false );
+ TU_ASSERT( len >= sizeof(tusb_desc_interface_t) );
mark_interface_endpoint(p_desc, len, drv_id);
@@ -408,8 +486,62 @@ static void mark_interface_endpoint(uint8_t const* p_desc, uint16_t desc_len, ui }
}
+// return descriptor's buffer and update desc_len
+static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len)
+{
+ tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
+ uint8_t const desc_index = tu_u16_low( p_request->wValue );
+
+ uint8_t const * desc_data = NULL;
+ uint16_t len = 0;
+
+ *desc_len = 0;
+
+ switch(desc_type)
+ {
+ case TUSB_DESC_DEVICE:
+ desc_data = (uint8_t const *) usbd_desc_set->device;
+ len = sizeof(tusb_desc_device_t);
+ break;
+
+ case TUSB_DESC_CONFIGURATION:
+ desc_data = (uint8_t const *) usbd_desc_set->config;
+ len = ((tusb_desc_configuration_t const*) desc_data)->wTotalLength;
+ break;
+
+ case TUSB_DESC_STRING:
+ // String Descriptor always uses the desc set from user
+ if ( desc_index < tud_desc_set.string_count )
+ {
+ desc_data = tud_desc_set.string_arr[desc_index];
+ TU_VERIFY( desc_data != NULL, NULL );
+
+ len = desc_data[0]; // first byte of descriptor is its size
+ }else
+ {
+ // out of range
+ /* The 0xEE index string is a Microsoft USB extension.
+ * It can be used to tell Windows what driver it should use for the device !!!
+ */
+ return NULL;
+ }
+ break;
+
+ case TUSB_DESC_DEVICE_QUALIFIER:
+ // TODO If not highspeed capable stall this request otherwise
+ // return the descriptor that could work in highspeed
+ return NULL;
+ break;
+
+ default: return NULL;
+ }
+
+ *desc_len = len;
+ return desc_data;
+}
+
//--------------------------------------------------------------------+
-// USBD-DCD Callback API
+// DCD Event Handler
//--------------------------------------------------------------------+
void dcd_event_handler(dcd_event_t const * event, bool in_isr)
{
@@ -434,6 +566,9 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) break;
case DCD_EVENT_XFER_COMPLETE:
+ // skip zero-length control status complete event, should dcd notifies us.
+ if ( 0 == edpt_number(event->xfer_complete.ep_addr) && event->xfer_complete.len == 0) break;
+
osal_queue_send(_usbd_q, event, in_isr);
TU_ASSERT(event->xfer_complete.result == DCD_XFER_SUCCESS,);
break;
@@ -442,8 +577,6 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) }
}
-void dcd_event_handler(dcd_event_t const * event, bool in_isr);
-
// helper to send bus signal event
void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr)
{
diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c new file mode 100644 index 000000000..a9687a3e6 --- /dev/null +++ b/src/device/usbd_control.c @@ -0,0 +1,170 @@ +/**************************************************************************/
+/*!
+ @file usbd_control.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 TUSB_OPT_DEVICE_ENABLED
+
+#define _TINY_USB_SOURCE_FILE_
+
+#include "tusb.h"
+#include "device/usbd_pvt.h"
+
+enum
+{
+ EDPT_CTRL_OUT = 0x00,
+ EDPT_CTRL_IN = 0x80
+};
+
+typedef struct
+{
+ tusb_control_request_t request;
+
+ void* buffer;
+ uint16_t total_len;
+ uint16_t total_transferred;
+
+ bool (*complete_cb) (uint8_t, tusb_control_request_t const *);
+} usbd_control_xfer_t;
+
+static usbd_control_xfer_t _control_state;
+
+CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN uint8_t _usbd_ctrl_buf[CFG_TUD_ENDOINT0_SIZE];
+
+void usbd_control_reset (uint8_t rhport)
+{
+ tu_varclr(&_control_state);
+}
+
+void usbd_control_stall(uint8_t rhport)
+{
+ dcd_edpt_stall(rhport, 0);
+}
+
+bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request)
+{
+ // status direction is reversed to one in the setup packet
+ return dcd_edpt_xfer(rhport, request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN, NULL, 0);
+}
+
+
+// Each transaction is up to endpoint0's max packet size
+static bool start_control_data_xact(uint8_t rhport)
+{
+ uint16_t const xact_len = tu_min16(_control_state.total_len - _control_state.total_transferred, CFG_TUD_ENDOINT0_SIZE);
+
+ uint8_t ep_addr = EDPT_CTRL_OUT;
+
+ if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_IN )
+ {
+ ep_addr = EDPT_CTRL_IN;
+ memcpy(_usbd_ctrl_buf, _control_state.buffer, xact_len);
+ }
+
+ return dcd_edpt_xfer(rhport, ep_addr, _usbd_ctrl_buf, xact_len);
+}
+
+// TODO may find a better way
+void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) )
+{
+ _control_state.complete_cb = fp;
+}
+
+bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len)
+{
+ _control_state.request = (*request);
+ _control_state.buffer = buffer;
+ _control_state.total_len = tu_min16(len, request->wLength);
+ _control_state.total_transferred = 0;
+
+ if ( buffer != NULL && len )
+ {
+ // Data stage
+ TU_ASSERT( start_control_data_xact(rhport) );
+ }else
+ {
+ // Status stage
+ TU_ASSERT( usbd_control_status(rhport, request) );
+ }
+
+ return true;
+}
+
+// callback when a transaction complete on DATA stage of control endpoint
+tusb_error_t usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes)
+{
+ if ( _control_state.request.bmRequestType_bit.direction == TUSB_DIR_OUT )
+ {
+ memcpy(_control_state.buffer, _usbd_ctrl_buf, xferred_bytes);
+ }
+
+ _control_state.total_transferred += xferred_bytes;
+ _control_state.buffer += xferred_bytes;
+
+ if ( _control_state.total_len == _control_state.total_transferred || xferred_bytes < CFG_TUD_ENDOINT0_SIZE )
+ {
+ // DATA stage is complete
+ bool is_ok = true;
+
+ // invoke complete callback if set
+ // callback can still stall control in status phase e.g out data does not make sense
+ if ( _control_state.complete_cb )
+ {
+ is_ok = _control_state.complete_cb(rhport, &_control_state.request);
+ }
+
+ if ( is_ok )
+ {
+ // Send status
+ TU_ASSERT( usbd_control_status(rhport, &_control_state.request), TUSB_ERROR_FAILED );
+ }else
+ {
+ // stall due to callback
+ usbd_control_stall(rhport);
+ }
+ }
+ else
+ {
+ // More data to transfer
+ TU_ASSERT(start_control_data_xact(rhport), TUSB_ERROR_FAILED);
+ }
+
+ return TUSB_ERROR_NONE;
+}
+
+#endif
diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h index bbbe0a604..30f010887 100644 --- a/src/device/usbd_pvt.h +++ b/src/device/usbd_pvt.h @@ -54,15 +54,24 @@ extern tud_desc_set_t const* usbd_desc_set; tusb_error_t usbd_init (void); void usbd_task (void* param); + +// Carry out Data and Status stage of control transfer +// - If len = 0, it is equivalent to sending status only +// - If len > wLength : it will be truncated +bool usbd_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len); + +// Send STATUS (zero length) packet +bool usbd_control_status(uint8_t rhport, tusb_control_request_t const * request); + +// Stall control endpoint until new setup packet arrived +void usbd_control_stall(uint8_t rhport); + /*------------------------------------------------------------------*/ -/* Endpoint helper +/* Helper *------------------------------------------------------------------*/ // helper to parse an pair of In and Out endpoint descriptors. They must be consecutive tusb_error_t usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* p_desc_ep, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in); -/*------------------------------------------------------------------*/ -/* Other Helpers - *------------------------------------------------------------------*/ void usbd_defer_func( osal_task_func_t func, void* param, bool in_isr ); |
