diff options
Diffstat (limited to 'src/device')
| -rw-r--r-- | src/device/dcd.h | 143 | ||||
| -rw-r--r-- | src/device/usbd.c | 665 | ||||
| -rw-r--r-- | src/device/usbd.h | 103 | ||||
| -rw-r--r-- | src/device/usbd_auto_desc.c | 660 | ||||
| -rw-r--r-- | src/device/usbd_control.c | 173 | ||||
| -rw-r--r-- | src/device/usbd_pvt.h | 82 |
6 files changed, 1826 insertions, 0 deletions
diff --git a/src/device/dcd.h b/src/device/dcd.h new file mode 100644 index 000000000..6b1356cbf --- /dev/null +++ b/src/device/dcd.h @@ -0,0 +1,143 @@ +/**************************************************************************/
+/*!
+ @file dcd.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
+ * \defgroup group_dcd Device Controller Driver (DCD)
+ * @{ */
+
+#ifndef _TUSB_DCD_H_
+#define _TUSB_DCD_H_
+
+#include "common/tusb_common.h"
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+typedef enum
+{
+ DCD_EVENT_BUS_RESET = 1,
+ DCD_EVENT_UNPLUGGED,
+ DCD_EVENT_SOF,
+ DCD_EVENT_SUSPENDED,
+ DCD_EVENT_RESUME,
+
+ DCD_EVENT_SETUP_RECEIVED,
+ DCD_EVENT_XFER_COMPLETE,
+
+ USBD_EVT_FUNC_CALL
+} dcd_eventid_t;
+
+typedef struct ATTR_ALIGNED(4)
+{
+ uint8_t rhport;
+ uint8_t event_id;
+
+ union {
+ // USBD_EVT_SETUP_RECEIVED
+ tusb_control_request_t setup_received;
+
+ // USBD_EVT_XFER_COMPLETE
+ struct {
+ uint8_t ep_addr;
+ uint8_t result;
+ uint32_t len;
+ }xfer_complete;
+
+ // USBD_EVT_FUNC_CALL
+ struct {
+ void (*func) (void*);
+ void* param;
+ }func_call;
+ };
+} dcd_event_t;
+
+TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct");
+
+/*------------------------------------------------------------------*/
+/* Device API
+ *------------------------------------------------------------------*/
+bool dcd_init (uint8_t rhport);
+void dcd_int_enable (uint8_t rhport);
+void dcd_int_disable(uint8_t rhport);
+
+void dcd_set_address(uint8_t rhport, uint8_t dev_addr);
+void dcd_set_config (uint8_t rhport, uint8_t config_num);
+
+/*------------------------------------------------------------------*/
+/* Event Function
+ * Called by DCD to notify USBD
+ *------------------------------------------------------------------*/
+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);
+
+// helper to send setup received
+void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr);
+
+// helper to send transfer complete event
+void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr);
+
+
+/*------------------------------------------------------------------*/
+/* Endpoint API
+ * - open : Configure endpoint's registers
+ * - xfer : Submit a transfer. When complete dcd_event_xfer_complete
+ * must be called to notify the stack
+ * - busy : Check if endpoint transferring is complete (TODO remove)
+ * - stall : stall ep. When control endpoint (addr = 0) is stalled,
+ * both direction (IN & OUT) of control ep must be stalled.
+ * - clear_stall : clear stall
+ * - stalled : check if stalled ( TODO remove )
+ *------------------------------------------------------------------*/
+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);
+bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr);
+
+void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
+void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
+bool dcd_edpt_stalled (uint8_t rhport, uint8_t ep_addr);
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* _TUSB_DCD_H_ */
+
+/// @}
diff --git a/src/device/usbd.c b/src/device/usbd.c new file mode 100644 index 000000000..8c685d5b3 --- /dev/null +++ b/src/device/usbd.c @@ -0,0 +1,665 @@ +/**************************************************************************/
+/*!
+ @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.
+*/
+/**************************************************************************/
+
+// This top level class manages the bus state and delegates events to class-specific drivers.
+
+#include "tusb_option.h"
+
+#if TUSB_OPT_DEVICE_ENABLED
+
+#define _TINY_USB_SOURCE_FILE_
+
+#include "tusb.h"
+#include "usbd.h"
+#include "device/usbd_pvt.h"
+
+#ifndef CFG_TUD_TASK_QUEUE_SZ
+#define CFG_TUD_TASK_QUEUE_SZ 16
+#endif
+
+#ifndef CFG_TUD_TASK_STACK_SZ
+#define CFG_TUD_TASK_STACK_SZ 150
+#endif
+
+#ifndef CFG_TUD_TASK_PRIO
+#define CFG_TUD_TASK_PRIO 0
+#endif
+
+
+//--------------------------------------------------------------------+
+// Device Data
+//--------------------------------------------------------------------+
+typedef struct {
+ uint8_t config_num;
+
+ uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
+ uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid )
+
+}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;
+tud_desc_set_t const* usbd_desc_set = &_usbd_auto_desc_set;
+#else
+tud_desc_set_t const* usbd_desc_set = &tud_desc_set;
+#endif
+
+//--------------------------------------------------------------------+
+// Class Driver
+//--------------------------------------------------------------------+
+typedef struct {
+ uint8_t class_code;
+
+ void (* init ) (void);
+ bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length);
+ 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);
+ bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t, uint32_t);
+ void (* sof ) (uint8_t rhport);
+ void (* reset ) (uint8_t);
+} usbd_class_driver_t;
+
+static usbd_class_driver_t const usbd_class_drivers[] =
+{
+ #if CFG_TUD_CDC
+ {
+ .class_code = TUSB_CLASS_CDC,
+ .init = cdcd_init,
+ .open = cdcd_open,
+ .control_request = cdcd_control_request,
+ .control_request_complete = cdcd_control_request_complete,
+ .xfer_cb = cdcd_xfer_cb,
+ .sof = NULL,
+ .reset = cdcd_reset
+ },
+ #endif
+
+ #if CFG_TUD_MSC
+ {
+ .class_code = TUSB_CLASS_MSC,
+ .init = mscd_init,
+ .open = mscd_open,
+ .control_request = mscd_control_request,
+ .control_request_complete = mscd_control_request_complete,
+ .xfer_cb = mscd_xfer_cb,
+ .sof = NULL,
+ .reset = mscd_reset
+ },
+ #endif
+
+
+ #if CFG_TUD_HID
+ {
+ .class_code = TUSB_CLASS_HID,
+ .init = hidd_init,
+ .open = hidd_open,
+ .control_request = hidd_control_request,
+ .control_request_complete = hidd_control_request_complete,
+ .xfer_cb = hidd_xfer_cb,
+ .sof = NULL,
+ .reset = hidd_reset
+ },
+ #endif
+
+ #if CFG_TUD_CUSTOM_CLASS
+ {
+ .class_code = TUSB_CLASS_VENDOR_SPECIFIC,
+ .init = cusd_init,
+ .open = cusd_open,
+ .control_request = cusd_control_request,
+ .control_request_complete = cusd_control_request_complete,
+ .xfer_cb = cusd_xfer_cb,
+ .sof = NULL,
+ .reset = cusd_reset
+ },
+ #endif
+};
+
+enum { USBD_CLASS_DRIVER_COUNT = sizeof(usbd_class_drivers) / sizeof(usbd_class_driver_t) };
+
+
+//--------------------------------------------------------------------+
+// DCD Event
+//--------------------------------------------------------------------+
+OSAL_TASK_DEF(_usbd_task_def, "usbd", usbd_task, CFG_TUD_TASK_PRIO, CFG_TUD_TASK_STACK_SZ);
+
+// Event queue
+// role device/host is used by OS NONE for mutex (disable usb isr) only
+OSAL_QUEUE_DEF(OPT_MODE_DEVICE, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t);
+static osal_queue_t _usbd_q;
+
+//--------------------------------------------------------------------+
+// Prototypes
+//--------------------------------------------------------------------+
+static void mark_interface_endpoint(uint8_t ep2drv[8][2], 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);
+static void const* get_descriptor(tusb_control_request_t const * p_request, uint16_t* desc_len);
+
+void usbd_control_reset (uint8_t rhport);
+bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
+void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) );
+
+//--------------------------------------------------------------------+
+// APPLICATION API
+//--------------------------------------------------------------------+
+bool tud_mounted(void)
+{
+ return _usbd_dev.config_num > 0;
+}
+
+//--------------------------------------------------------------------+
+// USBD Task
+//--------------------------------------------------------------------+
+bool usbd_init (void)
+{
+ // Init device queue & task
+ _usbd_q = osal_queue_create(&_usbd_qdef);
+ TU_ASSERT(_usbd_q != NULL);
+
+ osal_task_create(&_usbd_task_def);
+
+ // Init class drivers
+ for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++) usbd_class_drivers[i].init();
+
+ // Init device controller driver
+ TU_ASSERT(dcd_init(TUD_OPT_RHPORT));
+ dcd_int_enable(TUD_OPT_RHPORT);
+
+ return true;
+}
+
+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
+
+ usbd_control_reset(rhport);
+
+ for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
+ {
+ if ( usbd_class_drivers[i].reset ) usbd_class_drivers[i].reset( rhport );
+ }
+}
+
+// Main device task implementation
+static void usbd_task_body(void)
+{
+ // 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:
+ // 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;
+
+ if ( 0 == tu_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[tu_edpt_number(ep_addr)][tu_edpt_dir(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);
+ }
+ }
+ break;
+
+ case DCD_EVENT_BUS_RESET:
+ usbd_reset(event.rhport);
+ // TODO remove since if task is too slow, we could clear the event of the new attached
+ osal_queue_reset(_usbd_q);
+ break;
+
+ case DCD_EVENT_UNPLUGGED:
+ usbd_reset(event.rhport);
+ // TODO remove since if task is too slow, we could clear the event of the new attached
+ osal_queue_reset(_usbd_q);
+
+ // invoke callback
+ if (tud_umount_cb) tud_umount_cb();
+ break;
+
+ case DCD_EVENT_SOF:
+ for ( uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++ )
+ {
+ if ( usbd_class_drivers[i].sof )
+ {
+ usbd_class_drivers[i].sof(event.rhport);
+ }
+ }
+ break;
+
+ case USBD_EVT_FUNC_CALL:
+ if ( event.func_call.func ) event.func_call.func(event.func_call.param);
+ break;
+
+ default:
+ TU_BREAKPOINT();
+ break;
+ }
+ }
+}
+
+/* USB device task
+ * Thread that handles all device events. With an real RTOS, the task must be a forever loop and never return.
+ * For coding convenience with no RTOS, we use wrapped sub-function for processing to easily return at any time.
+ */
+void usbd_task( void* param)
+{
+ (void) param;
+
+#if CFG_TUSB_OS != OPT_OS_NONE
+ while (1) {
+#endif
+
+ usbd_task_body();
+
+#if CFG_TUSB_OS != OPT_OS_NONE
+ }
+#endif
+}
+
+//--------------------------------------------------------------------+
+// 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 )
+ {
+ case TUSB_REQ_SET_ADDRESS:
+ // response with status first before changing device address
+ usbd_control_status(rhport, p_request);
+ dcd_set_address(rhport, (uint8_t) p_request->wValue);
+ return true; // skip the rest
+ 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) );
+ }
+ 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:
+ TU_BREAKPOINT();
+ return false;
+ }
+
+ 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 )
+ {
+ 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:
+ TU_BREAKPOINT();
+ return false;
+ }
+ }
+ else
+ {
+ //------------- Unsupported Request -------------//
+ TU_BREAKPOINT();
+ return false;
+ }
+
+ return true;
+}
+
+// Process Set Configure Request
+// This function parse configuration descriptor & open drivers accordingly
+static bool process_set_config(uint8_t rhport)
+{
+ uint8_t const * desc_cfg = (uint8_t const *) usbd_desc_set->config;
+ 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 == tu_desc_type(p_desc) )
+ {
+ p_desc = tu_desc_next(p_desc); // ignore Interface Association
+ }else
+ {
+ TU_ASSERT( TUSB_DESC_INTERFACE == tu_desc_type(p_desc) );
+
+ 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 == desc_itf->bInterfaceClass ) break;
+ }
+ TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT );
+
+ // 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 itf_len=0;
+ TU_ASSERT( usbd_class_drivers[drv_id].open( rhport, desc_itf, &itf_len ) );
+ TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) );
+
+ mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, itf_len, drv_id);
+
+ p_desc += itf_len; // next interface
+ }
+ }
+
+ // invoke callback
+ if (tud_mount_cb) tud_mount_cb();
+
+ return TUSB_ERROR_NONE;
+}
+
+// Helper marking endpoint of interface belongs to class driver
+static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id)
+{
+ uint16_t len = 0;
+
+ while( len < desc_len )
+ {
+ if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
+ {
+ uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress;
+
+ ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = driver_id;
+ }
+
+ len += tu_desc_len(p_desc);
+ p_desc = tu_desc_next(p_desc);
+ }
+}
+
+// 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;
+}
+
+//--------------------------------------------------------------------+
+// DCD Event Handler
+//--------------------------------------------------------------------+
+void dcd_event_handler(dcd_event_t const * event, bool in_isr)
+{
+ switch (event->event_id)
+ {
+ case DCD_EVENT_BUS_RESET:
+ case DCD_EVENT_UNPLUGGED:
+ osal_queue_send(_usbd_q, event, in_isr);
+ break;
+
+ case DCD_EVENT_SOF:
+ // nothing to do now
+ break;
+
+ case DCD_EVENT_SUSPENDED:
+ // TODO support suspended
+ break;
+
+ case DCD_EVENT_RESUME:
+ // TODO support resume
+ break;
+
+ case DCD_EVENT_SETUP_RECEIVED:
+ osal_queue_send(_usbd_q, event, in_isr);
+ break;
+
+ case DCD_EVENT_XFER_COMPLETE:
+ // skip zero-length control status complete event, should dcd notifies us.
+ if ( 0 == tu_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 == XFER_RESULT_SUCCESS,);
+ break;
+
+ // Not an DCD event, just a convenient way to defer ISR function should we need
+ case USBD_EVT_FUNC_CALL:
+ osal_queue_send(_usbd_q, event, in_isr);
+ break;
+
+ default: break;
+ }
+}
+
+// helper to send bus signal event
+void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr)
+{
+ dcd_event_t event = { .rhport = rhport, .event_id = eid, };
+ dcd_event_handler(&event, in_isr);
+}
+
+// helper to send setup received
+void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr)
+{
+ dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED };
+ memcpy(&event.setup_received, setup, 8);
+
+ dcd_event_handler(&event, in_isr);
+}
+
+// helper to send transfer complete event
+void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr)
+{
+ dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE };
+
+ event.xfer_complete.ep_addr = ep_addr;
+ event.xfer_complete.len = xferred_bytes;
+ event.xfer_complete.result = result;
+
+ dcd_event_handler(&event, in_isr);
+}
+
+//--------------------------------------------------------------------+
+// Helper
+//--------------------------------------------------------------------+
+
+// Helper to parse an pair of endpoint descriptors (IN & OUT)
+bool usbd_open_edpt_pair(uint8_t rhport, tusb_desc_endpoint_t const* ep_desc, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in)
+{
+ for(int i=0; i<2; i++)
+ {
+ TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType &&
+ xfer_type == ep_desc->bmAttributes.xfer );
+
+ TU_ASSERT(dcd_edpt_open(rhport, ep_desc));
+
+ if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN )
+ {
+ (*ep_in) = ep_desc->bEndpointAddress;
+ }else
+ {
+ (*ep_out) = ep_desc->bEndpointAddress;
+ }
+
+ ep_desc = (tusb_desc_endpoint_t const *) tu_desc_next(ep_desc);
+ }
+
+ return true;
+}
+
+// Helper to defer an isr function
+void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr)
+{
+ dcd_event_t event =
+ {
+ .rhport = 0,
+ .event_id = USBD_EVT_FUNC_CALL,
+ };
+
+ event.func_call.func = func;
+ event.func_call.param = param;
+
+ dcd_event_handler(&event, in_isr);
+}
+
+#endif
diff --git a/src/device/usbd.h b/src/device/usbd.h new file mode 100644 index 000000000..f9e0d8f4d --- /dev/null +++ b/src/device/usbd.h @@ -0,0 +1,103 @@ +/**************************************************************************/
+/*!
+ @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_USBD_H_
+#define _TUSB_USBD_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+//--------------------------------------------------------------------+
+// INCLUDE
+//--------------------------------------------------------------------+
+#include <common/tusb_common.h>
+#include "osal/osal.h"
+#include "device/dcd.h"
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF
+//--------------------------------------------------------------------+
+
+/// \brief Descriptor pointer collector to all the needed.
+typedef struct {
+ void const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
+ void const * config; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
+
+ uint8_t const** string_arr; ///< a array of pointers to string descriptors
+ uint16_t string_count;
+
+ struct {
+ uint8_t const* generic;
+ uint8_t const* boot_keyboard;
+ uint8_t const* boot_mouse;
+ } hid_report;
+
+}tud_desc_set_t;
+
+
+// Must be defined by application
+extern tud_desc_set_t tud_desc_set;
+
+//--------------------------------------------------------------------+
+// APPLICATION API
+//--------------------------------------------------------------------+
+bool tud_mounted(void);
+
+//--------------------------------------------------------------------+
+// APPLICATION CALLBACK (WEAK is optional)
+//--------------------------------------------------------------------+
+
+/** Callback invoked when device is mounted (configured) */
+ATTR_WEAK void tud_mount_cb(void);
+
+/** Callback invoked when device is unmounted (bus reset/unplugged) */
+ATTR_WEAK void tud_umount_cb(void);
+
+//void tud_device_suspended_cb(void);
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* _TUSB_USBD_H_ */
+
+/** @} */
diff --git a/src/device/usbd_auto_desc.c b/src/device/usbd_auto_desc.c new file mode 100644 index 000000000..028d1b433 --- /dev/null +++ b/src/device/usbd_auto_desc.c @@ -0,0 +1,660 @@ +/**************************************************************************/ +/*! + @file usbd_auto_desc.c + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, 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 && CFG_TUD_DESC_AUTO + +#include "tusb.h" + +//--------------------------------------------------------------------+ +// Auto Description Default Configure & Validation +//--------------------------------------------------------------------+ + +// If HID Generic interface is generated +#define AUTO_DESC_HID_GENERIC (CFG_TUD_HID && ((CFG_TUD_HID_KEYBOARD && !CFG_TUD_HID_KEYBOARD_BOOT) || \ + (CFG_TUD_HID_MOUSE && !CFG_TUD_HID_MOUSE_BOOT)) ) +/*------------- VID/PID -------------*/ +#ifndef CFG_TUD_DESC_VID +#define CFG_TUD_DESC_VID 0xCAFE +#endif + +#ifndef CFG_TUD_DESC_PID + +/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. + * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. + * + * Auto ProductID layout's Bitmap: + * [MSB] HID Generic | Boot Mouse | Boot Keyboard | MSC | CDC [LSB] + */ +#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) +#define CFG_TUD_DESC_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(HID_KEYBOARD, 2) | _PID_MAP(HID_MOUSE, 3) | (AUTO_DESC_HID_GENERIC << 4) ) +#endif + +//--------------------------------------------------------------------+ +// Interface & Endpoint mapping +//--------------------------------------------------------------------+ + +/*------------- Interface Numbering -------------*/ +/* The order as follows: CDC, MSC, Boot Keyboard, Boot Mouse, HID Generic + * If an interface is not enabled, the later will take its place */ + +enum +{ +#if CFG_TUD_CDC + ITF_NUM_CDC, + ITF_NUM_CDC_DATA, +#endif + +#if CFG_TUD_MSC + ITF_NUM_MSC, +#endif + +#if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT + ITF_NUM_HID_BOOT_KBD, +#endif + +#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT + ITF_NUM_HID_BOOT_MSE, +#endif + +#if AUTO_DESC_HID_GENERIC + ITF_NUM_HID_GEN, +#endif + + ITF_NUM_TOTAL +}; + +enum +{ + ITF_STR_LANGUAGE = 0 , + ITF_STR_MANUFACTURER , + ITF_STR_PRODUCT , + ITF_STR_SERIAL , + +#if CFG_TUD_CDC + ITF_STR_CDC , +#endif + +#if CFG_TUD_MSC + ITF_STR_MSC , +#endif + +#if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT + ITF_STR_HID_BOOT_KBD, +#endif + +#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT + ITF_STR_HID_BOOT_MSE, +#endif + +#if AUTO_DESC_HID_GENERIC + ITF_STR_HID_GEN, +#endif +}; + +/*------------- Endpoint Numbering & Size -------------*/ +#define _EP_IN(x) (0x80 | (x)) +#define _EP_OUT(x) (x) + +// CDC +#ifdef CFG_TUD_DESC_CDC_EPNUM_NOTIF + #define EP_CDC_NOTIF _EP_IN (CFG_TUD_DESC_CDC_EPNUM_NOTIF) +#else + #define EP_CDC_NOTIF _EP_IN ( ITF_NUM_CDC+1 ) +#endif +#define EP_CDC_NOTIF_SIZE 8 + +#ifdef CFG_TUD_DESC_CDC_EPNUM + #define EP_CDC_OUT _EP_OUT( CFG_TUD_DESC_CDC_EPNUM ) + #define EP_CDC_IN _EP_IN ( CFG_TUD_DESC_CDC_EPNUM ) +#else + #define EP_CDC_OUT _EP_OUT( ITF_NUM_CDC+2 ) + #define EP_CDC_IN _EP_IN ( ITF_NUM_CDC+2 ) +#endif + +// Mass Storage +#ifdef CFG_TUD_DESC_MSC_EPNUM + #define EP_MSC_OUT _EP_OUT( CFG_TUD_DESC_MSC_EPNUM ) + #define EP_MSC_IN _EP_IN ( CFG_TUD_DESC_MSC_EPNUM ) +#else + #define EP_MSC_OUT _EP_OUT( ITF_NUM_MSC+1 ) + #define EP_MSC_IN _EP_IN ( ITF_NUM_MSC+1 ) +#endif + +#if TUD_OPT_HIGH_SPEED +#define EP_MSC_SIZE 512 +#else +#define EP_MSC_SIZE 64 +#endif + + +// HID Keyboard with boot protocol +#ifdef CFG_TUD_DESC_HID_KEYBOARD_EPNUM + #define EP_HID_KBD_BOOT _EP_IN ( CFG_TUD_DESC_HID_KEYBOARD_EPNUM ) +#else + #define EP_HID_KBD_BOOT _EP_IN ( ITF_NUM_HID_BOOT_KBD+1 ) +#endif +#define EP_HID_KBD_BOOT_SZ 8 + +// HID Mouse with boot protocol +#ifdef CFG_TUD_DESC_HID_MOUSE_EPNUM + #define EP_HID_MSE_BOOT _EP_IN ( CFG_TUD_DESC_HID_MOUSE_EPNUM ) +#else + #define EP_HID_MSE_BOOT _EP_IN ( ITF_NUM_HID_BOOT_MSE+1 ) +#endif +#define EP_HID_MSE_BOOT_SZ 8 + +// HID composite = keyboard + mouse + gamepad + etc ... +#define EP_HID_GEN _EP_IN ( ITF_NUM_HID_GEN+1 ) +#define EP_HID_GEN_SIZE 16 + + +//--------------------------------------------------------------------+ +// Auto generated HID Report Descriptors +//--------------------------------------------------------------------+ + + +/*------------- Boot Protocol Report Descriptor -------------*/ +#if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT +uint8_t const _desc_auto_hid_boot_kbd_report[] = { HID_REPORT_DESC_KEYBOARD() }; +#endif + +#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT +uint8_t const _desc_auto_hid_boot_mse_report[] = { HID_REPORT_DESC_MOUSE() }; +#endif + + +/*------------- Generic (composite) Descriptor -------------*/ +#if AUTO_DESC_HID_GENERIC + +// Report ID: 0 if there is only 1 report +// starting from 1 if there is multiple reports +#define _REPORT_ID_KBD + +// TODO report ID +uint8_t const _desc_auto_hid_generic_report[] = +{ +#if CFG_TUD_HID_KEYBOARD && !CFG_TUD_HID_KEYBOARD_BOOT + HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(1), ), +#endif + +#if CFG_TUD_HID_MOUSE && !CFG_TUD_HID_MOUSE_BOOT + HID_REPORT_DESC_MOUSE( HID_REPORT_ID(2), ) +#endif + +}; + +#endif // hid generic + + +/*------------------------------------------------------------------*/ +/* Auto generated Device & Configuration descriptor + *------------------------------------------------------------------*/ + +// For highspeed device but currently in full speed mode +//tusb_desc_device_qualifier_t _device_qual = +//{ +// .bLength = sizeof(tusb_desc_device_qualifier_t), +// .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER, +// .bcdUSB = 0x0200, +// .bDeviceClass = +//}; + +/*------------- Device Descriptor -------------*/ +tusb_desc_device_t const _desc_auto_device = +{ + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = 0x0200, + + #if CFG_TUD_CDC + // Use Interface Association Descriptor (IAD) for CDC + // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) + .bDeviceClass = TUSB_CLASS_MISC, + .bDeviceSubClass = MISC_SUBCLASS_COMMON, + .bDeviceProtocol = MISC_PROTOCOL_IAD, + #else + .bDeviceClass = 0x00, + .bDeviceSubClass = 0x00, + .bDeviceProtocol = 0x00, + #endif + + .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE, + + .idVendor = CFG_TUD_DESC_VID, + .idProduct = CFG_TUD_DESC_PID, + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01 // TODO multiple configurations +}; + + +/*------------- Configuration Descriptor -------------*/ +typedef struct ATTR_PACKED +{ + tusb_desc_configuration_t config; + + //------------- CDC -------------// +#if CFG_TUD_CDC + struct ATTR_PACKED + { + tusb_desc_interface_assoc_t iad; + + //CDC Control Interface + tusb_desc_interface_t comm_itf; + cdc_desc_func_header_t header; + cdc_desc_func_call_management_t call; + cdc_desc_func_acm_t acm; + cdc_desc_func_union_t union_func; + tusb_desc_endpoint_t ep_notif; + + //CDC Data Interface + tusb_desc_interface_t data_itf; + tusb_desc_endpoint_t ep_out; + tusb_desc_endpoint_t ep_in; + }cdc; +#endif + + //------------- Mass Storage -------------// +#if CFG_TUD_MSC + struct ATTR_PACKED + { + tusb_desc_interface_t itf; + tusb_desc_endpoint_t ep_out; + tusb_desc_endpoint_t ep_in; + } msc; +#endif + + //------------- HID -------------// +#if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT + struct ATTR_PACKED + { + tusb_desc_interface_t itf; + tusb_hid_descriptor_hid_t hid_desc; + tusb_desc_endpoint_t ep_in; + } hid_kbd_boot; +#endif + +#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT + struct ATTR_PACKED + { + tusb_desc_interface_t itf; + tusb_hid_descriptor_hid_t hid_desc; + tusb_desc_endpoint_t ep_in; + } hid_mse_boot; +#endif + +#if AUTO_DESC_HID_GENERIC + + struct ATTR_PACKED + { + tusb_desc_interface_t itf; + tusb_hid_descriptor_hid_t hid_desc; + tusb_desc_endpoint_t ep_in; + + #if 0 // CFG_TUD_HID_KEYBOARD + tusb_desc_endpoint_t ep_out; + #endif + } hid_generic; + +#endif + +} desc_auto_cfg_t; + +desc_auto_cfg_t const _desc_auto_config_struct = +{ + .config = + { + .bLength = sizeof(tusb_desc_configuration_t), + .bDescriptorType = TUSB_DESC_CONFIGURATION, + + .wTotalLength = sizeof(desc_auto_cfg_t), + .bNumInterfaces = ITF_NUM_TOTAL, + + .bConfigurationValue = 1, + .iConfiguration = 0x00, + .bmAttributes = TUSB_DESC_CONFIG_ATT_BUS_POWER, + .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(100) + }, + +#if CFG_TUD_CDC + // IAD points to CDC Interfaces + .cdc = + { + .iad = + { + .bLength = sizeof(tusb_desc_interface_assoc_t), + .bDescriptorType = TUSB_DESC_INTERFACE_ASSOCIATION, + + .bFirstInterface = ITF_NUM_CDC, + .bInterfaceCount = 2, + + .bFunctionClass = TUSB_CLASS_CDC, + .bFunctionSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, + .bFunctionProtocol = CDC_COMM_PROTOCOL_ATCOMMAND, + .iFunction = 0 + }, + + //------------- CDC Communication Interface -------------// + .comm_itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_CDC, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = TUSB_CLASS_CDC, + .bInterfaceSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, + .bInterfaceProtocol = CDC_COMM_PROTOCOL_ATCOMMAND, + .iInterface = 4 + }, + + .header = + { + .bLength = sizeof(cdc_desc_func_header_t), + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_HEADER, + .bcdCDC = 0x0120 + }, + + .call = + { + .bLength = sizeof(cdc_desc_func_call_management_t), + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_CALL_MANAGEMENT, + .bmCapabilities = { 0 }, + .bDataInterface = ITF_NUM_CDC+1, + }, + + .acm = + { + .bLength = sizeof(cdc_desc_func_acm_t), + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, + .bmCapabilities = { // 0x02 + .support_line_request = 1, + } + }, + + .union_func = + { + .bLength = sizeof(cdc_desc_func_union_t), // plus number of + .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC, + .bDescriptorSubType = CDC_FUNC_DESC_UNION, + .bControlInterface = ITF_NUM_CDC, + .bSubordinateInterface = ITF_NUM_CDC+1, + }, + + .ep_notif = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_CDC_NOTIF, + .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, + .wMaxPacketSize = { .size = EP_CDC_NOTIF_SIZE }, + .bInterval = 0x10 + }, + + //------------- CDC Data Interface -------------// + .data_itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_CDC+1, + .bAlternateSetting = 0x00, + .bNumEndpoints = 2, + .bInterfaceClass = TUSB_CLASS_CDC_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0x00 + }, + + .ep_out = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_CDC_OUT, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = CFG_TUD_CDC_EPSIZE }, + .bInterval = 0 + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_CDC_IN, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = CFG_TUD_CDC_EPSIZE }, + .bInterval = 0 + }, + }, +#endif // cdc + +#if CFG_TUD_MSC + //------------- Mass Storage-------------// + .msc = + { + .itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_MSC, + .bAlternateSetting = 0x00, + .bNumEndpoints = 2, + .bInterfaceClass = TUSB_CLASS_MSC, + .bInterfaceSubClass = MSC_SUBCLASS_SCSI, + .bInterfaceProtocol = MSC_PROTOCOL_BOT, + .iInterface = 4 + CFG_TUD_CDC + }, + + .ep_out = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_MSC_OUT, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = EP_MSC_SIZE}, + .bInterval = 1 + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_MSC_IN, + .bmAttributes = { .xfer = TUSB_XFER_BULK }, + .wMaxPacketSize = { .size = EP_MSC_SIZE}, + .bInterval = 1 + } + }, +#endif // msc + +#if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT + .hid_kbd_boot = + { + .itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_HID_BOOT_KBD, + .bAlternateSetting = 0x00, + .bNumEndpoints = 1, + .bInterfaceClass = TUSB_CLASS_HID, + .bInterfaceSubClass = HID_SUBCLASS_BOOT, + .bInterfaceProtocol = HID_PROTOCOL_KEYBOARD, + .iInterface = 0 //4 + CFG_TUD_CDC + CFG_TUD_MSC + }, + + .hid_desc = + { + .bLength = sizeof(tusb_hid_descriptor_hid_t), + .bDescriptorType = HID_DESC_TYPE_HID, + .bcdHID = 0x0111, + .bCountryCode = HID_Local_NotSupported, + .bNumDescriptors = 1, + .bReportType = HID_DESC_TYPE_REPORT, + .wReportLength = sizeof(_desc_auto_hid_boot_kbd_report) + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_HID_KBD_BOOT, + .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, + .wMaxPacketSize = { .size = EP_HID_KBD_BOOT_SZ }, + .bInterval = 0x0A + } + }, +#endif // boot keyboard + + //------------- HID Mouse -------------// +#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT + .hid_mse_boot = + { + .itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_HID_BOOT_MSE, + .bAlternateSetting = 0x00, + .bNumEndpoints = 1, + .bInterfaceClass = TUSB_CLASS_HID, + .bInterfaceSubClass = HID_SUBCLASS_BOOT, + .bInterfaceProtocol = HID_PROTOCOL_MOUSE, + .iInterface = 0 // 4 + CFG_TUD_CDC + CFG_TUD_MSC + CFG_TUD_HID_KEYBOARD + }, + + .hid_desc = + { + .bLength = sizeof(tusb_hid_descriptor_hid_t), + .bDescriptorType = HID_DESC_TYPE_HID, + .bcdHID = 0x0111, + .bCountryCode = HID_Local_NotSupported, + .bNumDescriptors = 1, + .bReportType = HID_DESC_TYPE_REPORT, + .wReportLength = sizeof(_desc_auto_hid_boot_mse_report) + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_HID_MSE_BOOT, + .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, + .wMaxPacketSize = { .size = EP_HID_MSE_BOOT_SZ }, + .bInterval = 0x0A + }, + }, + +#endif // boot mouse + +#if AUTO_DESC_HID_GENERIC + //------------- HID Generic Multiple report -------------// + .hid_generic = + { + .itf = + { + .bLength = sizeof(tusb_desc_interface_t), + .bDescriptorType = TUSB_DESC_INTERFACE, + .bInterfaceNumber = ITF_NUM_HID_GEN, + .bAlternateSetting = 0x00, + .bNumEndpoints = 1, + .bInterfaceClass = TUSB_CLASS_HID, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, // 4 + CFG_TUD_CDC + CFG_TUD_MSC, + }, + + .hid_desc = + { + .bLength = sizeof(tusb_hid_descriptor_hid_t), + .bDescriptorType = HID_DESC_TYPE_HID, + .bcdHID = 0x0111, + .bCountryCode = HID_Local_NotSupported, + .bNumDescriptors = 1, + .bReportType = HID_DESC_TYPE_REPORT, + .wReportLength = sizeof(_desc_auto_hid_generic_report) + }, + + .ep_in = + { + .bLength = sizeof(tusb_desc_endpoint_t), + .bDescriptorType = TUSB_DESC_ENDPOINT, + .bEndpointAddress = EP_HID_GEN, + .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT }, + .wMaxPacketSize = { .size = EP_HID_GEN_SIZE }, + .bInterval = 0x0A + } + } +#endif // hid generic +}; + +uint8_t const * const _desc_auto_config = (uint8_t const*) &_desc_auto_config_struct; + +tud_desc_set_t const _usbd_auto_desc_set = +{ + .device = &_desc_auto_device, + .config = &_desc_auto_config_struct, + + .hid_report = + { +#if AUTO_DESC_HID_GENERIC + .generic = _desc_auto_hid_generic_report, +#else + .generic = NULL, +#endif + +#if CFG_TUD_HID_KEYBOARD && CFG_TUD_HID_KEYBOARD_BOOT + .boot_keyboard = _desc_auto_hid_boot_kbd_report, +#endif + +#if CFG_TUD_HID_MOUSE && CFG_TUD_HID_MOUSE_BOOT + .boot_mouse = _desc_auto_hid_boot_mse_report +#endif + } +}; + +#endif diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c new file mode 100644 index 000000000..ae7e894b7 --- /dev/null +++ b/src/device/usbd_control.c @@ -0,0 +1,173 @@ +/**************************************************************************/
+/*!
+ @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_MEM_SECTION CFG_TUSB_MEM_ALIGN uint8_t _usbd_ctrl_buf[CFG_TUD_ENDOINT0_SIZE];
+
+void usbd_control_reset (uint8_t rhport)
+{
+ (void) 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
+bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
+{
+ (void) result;
+ (void) ep_addr;
+
+ 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) );
+ }else
+ {
+ // stall due to callback
+ usbd_control_stall(rhport);
+ }
+ }
+ else
+ {
+ // More data to transfer
+ TU_ASSERT( start_control_data_xact(rhport) );
+ }
+
+ return true;
+}
+
+#endif
diff --git a/src/device/usbd_pvt.h b/src/device/usbd_pvt.h new file mode 100644 index 000000000..798a871b1 --- /dev/null +++ b/src/device/usbd_pvt.h @@ -0,0 +1,82 @@ +/**************************************************************************/ +/*! + @file usbd_pvt.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, 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 USBD_PVT_H_ +#define USBD_PVT_H_ + +#include "osal/osal.h" +#include "common/tusb_fifo.h" + +#ifdef __cplusplus + extern "C" { +#endif + +// Either point to tud_desc_set or usbd_auto_desc_set depending on CFG_TUD_DESC_AUTO +extern tud_desc_set_t const* usbd_desc_set; + +//--------------------------------------------------------------------+ +// INTERNAL API for stack management +//--------------------------------------------------------------------+ +bool 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); + +/*------------------------------------------------------------------*/ +/* Helper + *------------------------------------------------------------------*/ +// helper to parse an pair of In and Out endpoint descriptors. They must be consecutive +bool 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); + +void usbd_defer_func( osal_task_func_t func, void* param, bool in_isr ); + + +#ifdef __cplusplus + } +#endif + +#endif /* USBD_PVT_H_ */ |
