summaryrefslogtreecommitdiff
path: root/src/class
diff options
context:
space:
mode:
authorGordon McNab <[email protected]>2021-12-08 08:36:43 +0000
committerGordon McNab <[email protected]>2021-12-08 08:36:43 +0000
commit9a7db985931ae215e808d40e7bacd52535d45305 (patch)
treea4d75e2c3823766da53a006280c0d805630d996e /src/class
parentedf3f3501656b010ad4a9e1d0a0d62573dd0b3fb (diff)
parentcde824f17f73b5ce4fcec8c176827297854a76c2 (diff)
Merge branch 'master' into port-ft90x
Diffstat (limited to 'src/class')
-rw-r--r--src/class/audio/audio.h12
-rw-r--r--src/class/audio/audio_device.c36
-rwxr-xr-xsrc/class/bth/bth_device.c120
-rw-r--r--src/class/cdc/cdc.h71
-rw-r--r--src/class/cdc/cdc_device.c12
-rw-r--r--src/class/cdc/cdc_device.h8
-rw-r--r--src/class/cdc/cdc_host.c2
-rw-r--r--src/class/dfu/dfu_device.c2
-rw-r--r--src/class/hid/hid_device.c16
-rw-r--r--src/class/hid/hid_device.h88
-rw-r--r--src/class/hid/hid_host.c10
-rw-r--r--src/class/midi/midi_device.c7
-rw-r--r--src/class/msc/msc_device.c566
-rw-r--r--src/class/msc/msc_device.h2
-rw-r--r--src/class/msc/msc_host.c2
-rw-r--r--src/class/net/ecm_rndis_device.c (renamed from src/class/net/net_device.c)8
-rw-r--r--src/class/net/ncm.h69
-rw-r--r--src/class/net/ncm_device.c510
-rw-r--r--src/class/net/net_device.h53
-rw-r--r--src/class/usbtmc/usbtmc_device.c44
-rw-r--r--src/class/usbtmc/usbtmc_device.h6
-rw-r--r--src/class/vendor/vendor_device.c39
-rw-r--r--src/class/vendor/vendor_device.h8
-rw-r--r--src/class/video/video.h480
-rw-r--r--src/class/video/video_device.c1149
-rw-r--r--src/class/video/video_device.h97
26 files changed, 3009 insertions, 408 deletions
diff --git a/src/class/audio/audio.h b/src/class/audio/audio.h
index f99061eae..6f9c1a6b5 100644
--- a/src/class/audio/audio.h
+++ b/src/class/audio/audio.h
@@ -494,18 +494,6 @@ typedef enum
/// All remaining definitions are taken from the descriptor descriptions in the UAC2 main specification
-/// Isochronous End Point Attributes
-typedef enum
-{
- TUSB_ISO_EP_ATT_NO_SYNC = 0x00,
- TUSB_ISO_EP_ATT_ASYNCHRONOUS = 0x04,
- TUSB_ISO_EP_ATT_ADAPTIVE = 0x08,
- TUSB_ISO_EP_ATT_SYNCHRONOUS = 0x0C,
- TUSB_ISO_EP_ATT_DATA = 0x00, ///< Data End Point
- TUSB_ISO_EP_ATT_EXPLICIT_FB = 0x10, ///< Feedback End Point
- TUSB_ISO_EP_ATT_IMPLICIT_FB = 0x20, ///< Data endpoint that also serves as an implicit feedback
-} tusb_iso_ep_attribute_t;
-
/// Audio Class-Control Values UAC2
typedef enum
{
diff --git a/src/class/audio/audio_device.c b/src/class/audio/audio_device.c
index ff4e312a7..d9f2e284e 100644
--- a/src/class/audio/audio_device.c
+++ b/src/class/audio/audio_device.c
@@ -64,10 +64,19 @@
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
+// Use ring buffer if it's available, some MCUs need extra RAM requirements
+#ifndef TUD_AUDIO_PREFER_RING_BUFFER
+#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX
+#define TUD_AUDIO_PREFER_RING_BUFFER 0
+#else
+#define TUD_AUDIO_PREFER_RING_BUFFER 1
+#endif
+#endif
+
// Linear buffer in case target MCU is not capable of handling a ring buffer FIFO e.g. no hardware buffer
// is available or driver is would need to be changed dramatically
-// Only STM32 synopsys use non-linear buffer for now
+// Only STM32 synopsys and dcd_transdimension use non-linear buffer for now
// Synopsys detection copied from dcd_synopsys.c (refactor later on)
#if defined (STM32F105x8) || defined (STM32F105xB) || defined (STM32F105xC) || \
defined (STM32F107xB) || defined (STM32F107xC)
@@ -90,11 +99,19 @@
CFG_TUSB_MCU == OPT_MCU_RX63X || \
CFG_TUSB_MCU == OPT_MCU_RX65X || \
CFG_TUSB_MCU == OPT_MCU_RX72N || \
- CFG_TUSB_MCU == OPT_MCU_GD32VF103
+ CFG_TUSB_MCU == OPT_MCU_GD32VF103 || \
+ CFG_TUSB_MCU == OPT_MCU_LPC18XX || \
+ CFG_TUSB_MCU == OPT_MCU_LPC43XX || \
+ CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
+ CFG_TUSB_MCU == OPT_MCU_MSP432E4
+#if TUD_AUDIO_PREFER_RING_BUFFER
#define USE_LINEAR_BUFFER 0
#else
#define USE_LINEAR_BUFFER 1
#endif
+#else
+#define USE_LINEAR_BUFFER 1
+#endif
// Declaration of buffers
@@ -1553,20 +1570,21 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
{
if (tu_desc_type(p_desc) == TUSB_DESC_ENDPOINT)
{
- TU_ASSERT(usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *)p_desc));
+ tusb_desc_endpoint_t const* desc_ep = (tusb_desc_endpoint_t const *) p_desc;
+ TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
- uint8_t ep_addr = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
+ uint8_t const ep_addr = desc_ep->bEndpointAddress;
//TODO: We need to set EP non busy since this is not taken care of right now in ep_close() - THIS IS A WORKAROUND!
usbd_edpt_clear_stall(rhport, ep_addr);
#if CFG_TUD_AUDIO_ENABLE_EP_IN
- if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN && ((tusb_desc_endpoint_t const *) p_desc)->bmAttributes.usage == 0x00) // Check if usage is data EP
+ if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN && desc_ep->bmAttributes.usage == 0x00) // Check if usage is data EP
{
// Save address
audio->ep_in = ep_addr;
audio->ep_in_as_intf_num = itf;
- audio->ep_in_sz = ((tusb_desc_endpoint_t const *) p_desc)->wMaxPacketSize.size;
+ audio->ep_in_sz = tu_edpt_packet_size(desc_ep);
// If software encoding is enabled, parse for the corresponding parameters - doing this here means only AS interfaces with EPs get scanned for parameters
#if CFG_TUD_AUDIO_ENABLE_ENCODING
@@ -1600,7 +1618,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
// Save address
audio->ep_out = ep_addr;
audio->ep_out_as_intf_num = itf;
- audio->ep_out_sz = ((tusb_desc_endpoint_t const *) p_desc)->wMaxPacketSize.size;
+ audio->ep_out_sz = tu_edpt_packet_size(desc_ep);
#if CFG_TUD_AUDIO_ENABLE_DECODING
audiod_parse_for_AS_params(audio, p_desc_parse_for_params, p_desc_end, itf);
@@ -1619,7 +1637,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
// In case of asynchronous EP, call Cb after ep_fb is set
- if (!(((tusb_desc_endpoint_t const *) p_desc)->bmAttributes.sync == 0x01 && audio->ep_fb == 0))
+ if ( !(desc_ep->bmAttributes.sync == 0x01 && audio->ep_fb == 0) )
{
if (tud_audio_set_itf_cb) TU_VERIFY(tud_audio_set_itf_cb(rhport, p_request));
}
@@ -1636,7 +1654,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
}
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
- if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN && ((tusb_desc_endpoint_t const *) p_desc)->bmAttributes.usage == 1) // Check if usage is explicit data feedback
+ if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN && desc_ep->bmAttributes.usage == 1) // Check if usage is explicit data feedback
{
audio->ep_fb = ep_addr;
diff --git a/src/class/bth/bth_device.c b/src/class/bth/bth_device.c
index 1d27ae7c5..8ef609622 100755
--- a/src/class/bth/bth_device.c
+++ b/src/class/bth/bth_device.c
@@ -112,74 +112,78 @@ uint16_t btd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_
TUD_BT_APP_SUBCLASS == itf_desc->bInterfaceSubClass &&
TUD_BT_PROTOCOL_PRIMARY_CONTROLLER == itf_desc->bInterfaceProtocol, 0);
- // Distinguish interface by number of endpoints, as both interface have same class, subclass and protocol
- if (itf_desc->bNumEndpoints == 3 && max_len >= hci_itf_size)
- {
- _btd_itf.itf_num = itf_desc->bInterfaceNumber;
+ TU_ASSERT(itf_desc->bNumEndpoints == 3 && max_len >= hci_itf_size);
- desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc);
+ _btd_itf.itf_num = itf_desc->bInterfaceNumber;
- TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0);
- TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
- _btd_itf.ep_ev = desc_ep->bEndpointAddress;
+ desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc);
- // Open endpoint pair
- TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(desc_ep), 2, TUSB_XFER_BULK, &_btd_itf.ep_acl_out,
- &_btd_itf.ep_acl_in), 0);
+ TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0);
+ TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
+ _btd_itf.ep_ev = desc_ep->bEndpointAddress;
- // Prepare for incoming data from host
- TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_itf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE), 0);
+ // Open endpoint pair
+ TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(desc_ep), 2, TUSB_XFER_BULK, &_btd_itf.ep_acl_out,
+ &_btd_itf.ep_acl_in), 0);
- drv_len = hci_itf_size;
- }
- else if (itf_desc->bNumEndpoints == 2 && max_len >= iso_alt_itf_size)
- {
- uint8_t dir;
+ itf_desc = (tusb_desc_interface_t const *)tu_desc_next(tu_desc_next(tu_desc_next(desc_ep)));
- desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(itf_desc);
+ // Prepare for incoming data from host
+ TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_itf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE), 0);
+
+ drv_len = hci_itf_size;
+
+ // Ensure this is still BT Primary Controller
+ TU_ASSERT(TUSB_CLASS_WIRELESS_CONTROLLER == itf_desc->bInterfaceClass &&
+ TUD_BT_APP_SUBCLASS == itf_desc->bInterfaceSubClass &&
+ TUD_BT_PROTOCOL_PRIMARY_CONTROLLER == itf_desc->bInterfaceProtocol, 0);
+ TU_ASSERT(itf_desc->bNumEndpoints == 2 && max_len >= iso_alt_itf_size + drv_len);
+
+ uint8_t dir;
+
+ desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(itf_desc);
+ TU_ASSERT(itf_desc->bAlternateSetting < CFG_TUD_BTH_ISO_ALT_COUNT, 0);
+ TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT, 0);
+ dir = tu_edpt_dir(desc_ep->bEndpointAddress);
+ _btd_itf.ep_voice[dir] = desc_ep->bEndpointAddress;
+ // Store endpoint size for alternative
+ _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t) tu_edpt_packet_size(desc_ep);
+
+ desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(desc_ep);
+ TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT, 0);
+ dir = tu_edpt_dir(desc_ep->bEndpointAddress);
+ _btd_itf.ep_voice[dir] = desc_ep->bEndpointAddress;
+ // Store endpoint size for alternative
+ _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t) tu_edpt_packet_size(desc_ep);
+ drv_len += iso_alt_itf_size;
+
+ for (int i = 1; i < CFG_TUD_BTH_ISO_ALT_COUNT && drv_len + iso_alt_itf_size <= max_len; ++i) {
+ // Make sure rest of alternatives matches
+ itf_desc = (tusb_desc_interface_t const *)tu_desc_next(desc_ep);
+ if (itf_desc->bDescriptorType != TUSB_DESC_INTERFACE ||
+ TUSB_CLASS_WIRELESS_CONTROLLER != itf_desc->bInterfaceClass ||
+ TUD_BT_APP_SUBCLASS != itf_desc->bInterfaceSubClass ||
+ TUD_BT_PROTOCOL_PRIMARY_CONTROLLER != itf_desc->bInterfaceProtocol)
+ {
+ // Not an Iso interface instance
+ break;
+ }
TU_ASSERT(itf_desc->bAlternateSetting < CFG_TUD_BTH_ISO_ALT_COUNT, 0);
- TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT, 0);
+
+ desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(itf_desc);
dir = tu_edpt_dir(desc_ep->bEndpointAddress);
- _btd_itf.ep_voice[dir] = desc_ep->bEndpointAddress;
- // Store endpoint size for alternative
- _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t)desc_ep->wMaxPacketSize.size;
+ // Verify that alternative endpoint are same as first ones
+ TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT &&
+ _btd_itf.ep_voice[dir] == desc_ep->bEndpointAddress, 0);
+ _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t) tu_edpt_packet_size(desc_ep);
desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(desc_ep);
- TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT, 0);
dir = tu_edpt_dir(desc_ep->bEndpointAddress);
- _btd_itf.ep_voice[dir] = desc_ep->bEndpointAddress;
- // Store endpoint size for alternative
- _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t)desc_ep->wMaxPacketSize.size;
+ // Verify that alternative endpoint are same as first ones
+ TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT &&
+ _btd_itf.ep_voice[dir] == desc_ep->bEndpointAddress, 0);
+ _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t) tu_edpt_packet_size(desc_ep);
drv_len += iso_alt_itf_size;
-
- for (int i = 1; i < CFG_TUD_BTH_ISO_ALT_COUNT && drv_len + iso_alt_itf_size <= max_len; ++i) {
- // Make sure rest of alternatives matches
- itf_desc = (tusb_desc_interface_t const *)tu_desc_next(desc_ep);
- if (itf_desc->bDescriptorType != TUSB_DESC_INTERFACE ||
- TUSB_CLASS_WIRELESS_CONTROLLER != itf_desc->bInterfaceClass ||
- TUD_BT_APP_SUBCLASS != itf_desc->bInterfaceSubClass ||
- TUD_BT_PROTOCOL_PRIMARY_CONTROLLER != itf_desc->bInterfaceProtocol)
- {
- // Not an Iso interface instance
- break;
- }
- TU_ASSERT(itf_desc->bAlternateSetting < CFG_TUD_BTH_ISO_ALT_COUNT, 0);
-
- desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(itf_desc);
- dir = tu_edpt_dir(desc_ep->bEndpointAddress);
- // Verify that alternative endpoint are same as first ones
- TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT &&
- _btd_itf.ep_voice[dir] == desc_ep->bEndpointAddress, 0);
- _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t)desc_ep->wMaxPacketSize.size;
-
- desc_ep = (tusb_desc_endpoint_t const *)tu_desc_next(desc_ep);
- dir = tu_edpt_dir(desc_ep->bEndpointAddress);
- // Verify that alternative endpoint are same as first ones
- TU_ASSERT(desc_ep->bDescriptorType == TUSB_DESC_ENDPOINT &&
- _btd_itf.ep_voice[dir] == desc_ep->bEndpointAddress, 0);
- _btd_itf.ep_voice_size[dir][itf_desc->bAlternateSetting] = (uint8_t)desc_ep->wMaxPacketSize.size;
- drv_len += iso_alt_itf_size;
- }
}
return drv_len;
@@ -214,14 +218,14 @@ bool btd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t c
}
else return false;
- return tud_control_xfer(rhport, request, &_btd_itf.hci_cmd, request->wLength);
+ return tud_control_xfer(rhport, request, &_btd_itf.hci_cmd, sizeof(_btd_itf.hci_cmd));
}
else if ( stage == CONTROL_STAGE_DATA )
{
// Handle class request only
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
- if (tud_bt_hci_cmd_cb) tud_bt_hci_cmd_cb(&_btd_itf.hci_cmd, request->wLength);
+ if (tud_bt_hci_cmd_cb) tud_bt_hci_cmd_cb(&_btd_itf.hci_cmd, tu_min16(request->wLength, sizeof(_btd_itf.hci_cmd)));
}
return true;
diff --git a/src/class/cdc/cdc.h b/src/class/cdc/cdc.h
index 5df47f70b..e345139ea 100644
--- a/src/class/cdc/cdc.h
+++ b/src/class/cdc/cdc.h
@@ -58,31 +58,32 @@ typedef enum
/// Communication Interface Subclass Codes
typedef enum
{
- CDC_COMM_SUBCLASS_DIRECT_LINE_CONTROL_MODEL = 0x01 , ///< Direct Line Control Model [USBPSTN1.2]
- CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL , ///< Abstract Control Model [USBPSTN1.2]
- CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL , ///< Telephone Control Model [USBPSTN1.2]
- CDC_COMM_SUBCLASS_MULTICHANNEL_CONTROL_MODEL , ///< Multi-Channel Control Model [USBISDN1.2]
- CDC_COMM_SUBCLASS_CAPI_CONTROL_MODEL , ///< CAPI Control Model [USBISDN1.2]
- CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL , ///< Ethernet Networking Control Model [USBECM1.2]
- CDC_COMM_SUBCLASS_ATM_NETWORKING_CONTROL_MODEL , ///< ATM Networking Control Model [USBATM1.2]
- CDC_COMM_SUBCLASS_WIRELESS_HANDSET_CONTROL_MODEL , ///< Wireless Handset Control Model [USBWMC1.1]
- CDC_COMM_SUBCLASS_DEVICE_MANAGEMENT , ///< Device Management [USBWMC1.1]
- CDC_COMM_SUBCLASS_MOBILE_DIRECT_LINE_MODEL , ///< Mobile Direct Line Model [USBWMC1.1]
- CDC_COMM_SUBCLASS_OBEX , ///< OBEX [USBWMC1.1]
- CDC_COMM_SUBCLASS_ETHERNET_EMULATION_MODEL ///< Ethernet Emulation Model [USBEEM1.0]
+ CDC_COMM_SUBCLASS_DIRECT_LINE_CONTROL_MODEL = 0x01 , ///< Direct Line Control Model [USBPSTN1.2]
+ CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL = 0x02 , ///< Abstract Control Model [USBPSTN1.2]
+ CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL = 0x03 , ///< Telephone Control Model [USBPSTN1.2]
+ CDC_COMM_SUBCLASS_MULTICHANNEL_CONTROL_MODEL = 0x04 , ///< Multi-Channel Control Model [USBISDN1.2]
+ CDC_COMM_SUBCLASS_CAPI_CONTROL_MODEL = 0x05 , ///< CAPI Control Model [USBISDN1.2]
+ CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL = 0x06 , ///< Ethernet Networking Control Model [USBECM1.2]
+ CDC_COMM_SUBCLASS_ATM_NETWORKING_CONTROL_MODEL = 0x07 , ///< ATM Networking Control Model [USBATM1.2]
+ CDC_COMM_SUBCLASS_WIRELESS_HANDSET_CONTROL_MODEL = 0x08 , ///< Wireless Handset Control Model [USBWMC1.1]
+ CDC_COMM_SUBCLASS_DEVICE_MANAGEMENT = 0x09 , ///< Device Management [USBWMC1.1]
+ CDC_COMM_SUBCLASS_MOBILE_DIRECT_LINE_MODEL = 0x0A , ///< Mobile Direct Line Model [USBWMC1.1]
+ CDC_COMM_SUBCLASS_OBEX = 0x0B , ///< OBEX [USBWMC1.1]
+ CDC_COMM_SUBCLASS_ETHERNET_EMULATION_MODEL = 0x0C , ///< Ethernet Emulation Model [USBEEM1.0]
+ CDC_COMM_SUBCLASS_NETWORK_CONTROL_MODEL = 0x0D ///< Network Control Model [USBNCM1.0]
} cdc_comm_sublcass_type_t;
/// Communication Interface Protocol Codes
typedef enum
{
- CDC_COMM_PROTOCOL_NONE = 0x00 , ///< No specific protocol
- CDC_COMM_PROTOCOL_ATCOMMAND , ///< AT Commands: V.250 etc
- CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101 , ///< AT Commands defined by PCCA-101
- CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101_AND_ANNEXO , ///< AT Commands defined by PCCA-101 & Annex O
- CDC_COMM_PROTOCOL_ATCOMMAND_GSM_707 , ///< AT Commands defined by GSM 07.07
- CDC_COMM_PROTOCOL_ATCOMMAND_3GPP_27007 , ///< AT Commands defined by 3GPP 27.007
- CDC_COMM_PROTOCOL_ATCOMMAND_CDMA , ///< AT Commands defined by TIA for CDMA
- CDC_COMM_PROTOCOL_ETHERNET_EMULATION_MODEL ///< Ethernet Emulation Model
+ CDC_COMM_PROTOCOL_NONE = 0x00 , ///< No specific protocol
+ CDC_COMM_PROTOCOL_ATCOMMAND = 0x01 , ///< AT Commands: V.250 etc
+ CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101 = 0x02 , ///< AT Commands defined by PCCA-101
+ CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101_AND_ANNEXO = 0x03 , ///< AT Commands defined by PCCA-101 & Annex O
+ CDC_COMM_PROTOCOL_ATCOMMAND_GSM_707 = 0x04 , ///< AT Commands defined by GSM 07.07
+ CDC_COMM_PROTOCOL_ATCOMMAND_3GPP_27007 = 0x05 , ///< AT Commands defined by 3GPP 27.007
+ CDC_COMM_PROTOCOL_ATCOMMAND_CDMA = 0x06 , ///< AT Commands defined by TIA for CDMA
+ CDC_COMM_PROTOCOL_ETHERNET_EMULATION_MODEL = 0x07 ///< Ethernet Emulation Model
} cdc_comm_protocol_type_t;
//------------- SubType Descriptor in COMM Functional Descriptor -------------//
@@ -114,7 +115,8 @@ typedef enum
CDC_FUNC_DESC_COMMAND_SET = 0x16 , ///< Command Set Functional Descriptor
CDC_FUNC_DESC_COMMAND_SET_DETAIL = 0x17 , ///< Command Set Detail Functional Descriptor
CDC_FUNC_DESC_TELEPHONE_CONTROL_MODEL = 0x18 , ///< Telephone Control Model Functional Descriptor
- CDC_FUNC_DESC_OBEX_SERVICE_IDENTIFIER = 0x19 ///< OBEX Service Identifier Functional Descriptor
+ CDC_FUNC_DESC_OBEX_SERVICE_IDENTIFIER = 0x19 , ///< OBEX Service Identifier Functional Descriptor
+ CDC_FUNC_DESC_NCM = 0x1A , ///< NCM Functional Descriptor
}cdc_func_desc_type_t;
//--------------------------------------------------------------------+
@@ -122,7 +124,8 @@ typedef enum
//--------------------------------------------------------------------+
// SUBCLASS code of Data Interface is not used and should/must be zero
-/// Data Interface Protocol Codes
+
+// Data Interface Protocol Codes
typedef enum{
CDC_DATA_PROTOCOL_ISDN_BRI = 0x30, ///< Physical interface protocol for ISDN BRI
CDC_DATA_PROTOCOL_HDLC = 0x31, ///< HDLC
@@ -147,7 +150,6 @@ typedef enum
{
CDC_REQUEST_SEND_ENCAPSULATED_COMMAND = 0x00, ///< is used to issue a command in the format of the supported control protocol of the Communications Class interface
CDC_REQUEST_GET_ENCAPSULATED_RESPONSE = 0x01, ///< is used to request a response in the format of the supported control protocol of the Communications Class interface.
-
CDC_REQUEST_SET_COMM_FEATURE = 0x02,
CDC_REQUEST_GET_COMM_FEATURE = 0x03,
CDC_REQUEST_CLEAR_COMM_FEATURE = 0x04,
@@ -194,21 +196,18 @@ typedef enum
// Management Elemenent Notification (Notification Endpoint)
//--------------------------------------------------------------------+
-/// Communication Interface Management Element Notification Codes
+/// 6.3 Notification Codes
typedef enum
{
- NETWORK_CONNECTION = 0x00, ///< This notification allows the device to notify the host about network connection status.
- RESPONSE_AVAILABLE = 0x01, ///< This notification allows the device to notify the hostthat a response is available. This response can be retrieved with a subsequent \ref CDC_REQUEST_GET_ENCAPSULATED_RESPONSE request.
-
- AUX_JACK_HOOK_STATE = 0x08,
- RING_DETECT = 0x09,
-
- SERIAL_STATE = 0x20,
-
- CALL_STATE_CHANGE = 0x28,
- LINE_STATE_CHANGE = 0x29,
- CONNECTION_SPEED_CHANGE = 0x2A, ///< This notification allows the device to inform the host-networking driver that a change in either the upstream or the downstream bit rate of the connection has occurred
- MDLM_SEMANTIC_MODEL_NOTIFICATION = 0x40,
+ CDC_NOTIF_NETWORK_CONNECTION = 0x00, ///< This notification allows the device to notify the host about network connection status.
+ CDC_NOTIF_RESPONSE_AVAILABLE = 0x01, ///< This notification allows the device to notify the hostthat a response is available. This response can be retrieved with a subsequent \ref CDC_REQUEST_GET_ENCAPSULATED_RESPONSE request.
+ CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,
+ CDC_NOTIF_RING_DETECT = 0x09,
+ CDC_NOTIF_SERIAL_STATE = 0x20,
+ CDC_NOTIF_CALL_STATE_CHANGE = 0x28,
+ CDC_NOTIF_LINE_STATE_CHANGE = 0x29,
+ CDC_NOTIF_CONNECTION_SPEED_CHANGE = 0x2A, ///< This notification allows the device to inform the host-networking driver that a change in either the upstream or the downstream bit rate of the connection has occurred
+ CDC_NOTIF_MDLM_SEMANTIC_MODEL_NOTIFICATION = 0x40,
}cdc_notification_request_t;
//--------------------------------------------------------------------+
diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c
index e622bd616..08f2af253 100644
--- a/src/class/cdc/cdc_device.c
+++ b/src/class/cdc/cdc_device.c
@@ -80,7 +80,7 @@ typedef struct
//--------------------------------------------------------------------+
CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
-static void _prep_out_transaction (cdcd_interface_t* p_cdc)
+static bool _prep_out_transaction (cdcd_interface_t* p_cdc)
{
uint8_t const rhport = TUD_OPT_RHPORT;
uint16_t available = tu_fifo_remaining(&p_cdc->rx_ff);
@@ -89,21 +89,23 @@ static void _prep_out_transaction (cdcd_interface_t* p_cdc)
// TODO Actually we can still carry out the transfer, keeping count of received bytes
// and slowly move it to the FIFO when read().
// This pre-check reduces endpoint claiming
- TU_VERIFY(available >= sizeof(p_cdc->epout_buf), );
+ TU_VERIFY(available >= sizeof(p_cdc->epout_buf));
// claim endpoint
- TU_VERIFY(usbd_edpt_claim(rhport, p_cdc->ep_out), );
+ TU_VERIFY(usbd_edpt_claim(rhport, p_cdc->ep_out));
// fifo can be changed before endpoint is claimed
available = tu_fifo_remaining(&p_cdc->rx_ff);
if ( available >= sizeof(p_cdc->epout_buf) )
{
- usbd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, sizeof(p_cdc->epout_buf));
+ return usbd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, sizeof(p_cdc->epout_buf));
}else
{
// Release endpoint since we don't make any transfer
usbd_edpt_release(rhport, p_cdc->ep_out);
+
+ return false;
}
}
@@ -229,7 +231,7 @@ void cdcd_init(void)
{
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
- p_cdc->wanted_char = -1;
+ p_cdc->wanted_char = (char) -1;
// default line coding is : stop bit = 1, parity = none, data bits = 8
p_cdc->line_coding.bit_rate = 115200;
diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h
index 7ff757add..fbc7162a3 100644
--- a/src/class/cdc/cdc_device.h
+++ b/src/class/cdc/cdc_device.h
@@ -82,7 +82,7 @@ int32_t tud_cdc_n_read_char (uint8_t itf);
void tud_cdc_n_read_flush (uint8_t itf);
// Get a byte from FIFO at the specified position without removing it
-bool tud_cdc_n_peek (uint8_t itf, uint8_t* u8);
+bool tud_cdc_n_peek (uint8_t itf, uint8_t* ui8);
// Write bytes to TX FIFO, data may remain in the FIFO for a while
uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
@@ -116,7 +116,7 @@ static inline uint32_t tud_cdc_available (void);
static inline int32_t tud_cdc_read_char (void);
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize);
static inline void tud_cdc_read_flush (void);
-static inline bool tud_cdc_peek (uint8_t* u8);
+static inline bool tud_cdc_peek (uint8_t* ui8);
static inline uint32_t tud_cdc_write_char (char ch);
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize);
@@ -206,9 +206,9 @@ static inline void tud_cdc_read_flush (void)
tud_cdc_n_read_flush(0);
}
-static inline bool tud_cdc_peek (uint8_t* u8)
+static inline bool tud_cdc_peek (uint8_t* ui8)
{
- return tud_cdc_n_peek(0, u8);
+ return tud_cdc_n_peek(0, ui8);
}
static inline uint32_t tud_cdc_write_char (char ch)
diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c
index e7eaf4d05..f4fb6c1d6 100644
--- a/src/class/cdc/cdc_host.c
+++ b/src/class/cdc/cdc_host.c
@@ -105,7 +105,7 @@ bool tuh_cdc_send(uint8_t dev_addr, void const * p_data, uint32_t length, bool i
uint8_t const ep_out = cdch_data[dev_addr-1].ep_out;
if ( usbh_edpt_busy(dev_addr, ep_out) ) return false;
- return usbh_edpt_xfer(dev_addr, ep_out, (void *) p_data, length);
+ return usbh_edpt_xfer(dev_addr, ep_out, (void*)(uintptr_t) p_data, length);
}
bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is_notify)
diff --git a/src/class/dfu/dfu_device.c b/src/class/dfu/dfu_device.c
index 28abf6e83..ddfa608e4 100644
--- a/src/class/dfu/dfu_device.c
+++ b/src/class/dfu/dfu_device.c
@@ -190,7 +190,7 @@ uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
_dfu_ctx.attrs = func_desc->bAttributes;
// CFG_TUD_DFU_XFER_BUFSIZE has to be set to the buffer size used in TUD_DFU_DESCRIPTOR
- uint16_t const transfer_size = tu_le16toh( tu_unaligned_read16((uint8_t*) func_desc + offsetof(tusb_desc_dfu_functional_t, wTransferSize)) );
+ uint16_t const transfer_size = tu_le16toh( tu_unaligned_read16((uint8_t const*) func_desc + offsetof(tusb_desc_dfu_functional_t, wTransferSize)) );
TU_ASSERT(transfer_size <= CFG_TUD_DFU_XFER_BUFSIZE, drv_len);
return drv_len;
diff --git a/src/class/hid/hid_device.c b/src/class/hid/hid_device.c
index 2ee80750a..588b61254 100644
--- a/src/class/hid/hid_device.c
+++ b/src/class/hid/hid_device.c
@@ -206,8 +206,8 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint1
//------------- HID descriptor -------------//
p_desc = tu_desc_next(p_desc);
+ TU_ASSERT(HID_DESC_TYPE_HID == tu_desc_type(p_desc), 0);
p_hid->hid_descriptor = (tusb_hid_descriptor_hid_t const *) p_desc;
- TU_ASSERT(HID_DESC_TYPE_HID == p_hid->hid_descriptor->bDescriptorType, 0);
//------------- Endpoint Descriptor -------------//
p_desc = tu_desc_next(p_desc);
@@ -216,10 +216,10 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint1
if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->itf_protocol = desc_itf->bInterfaceProtocol;
p_hid->protocol_mode = HID_PROTOCOL_REPORT; // Per Specs: default is report mode
- p_hid->itf_num = desc_itf->bInterfaceNumber;
+ p_hid->itf_num = desc_itf->bInterfaceNumber;
// Use offsetof to avoid pointer to the odd/misaligned address
- memcpy(&p_hid->report_desc_len, (uint8_t*) p_hid->hid_descriptor + offsetof(tusb_hid_descriptor_hid_t, wReportLength), 2);
+ p_hid->report_desc_len = tu_unaligned_read16((uint8_t const*) p_hid->hid_descriptor + offsetof(tusb_hid_descriptor_hid_t, wReportLength));
// Prepare for output endpoint
if (p_hid->ep_out)
@@ -256,13 +256,13 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_HID)
{
- TU_VERIFY(p_hid->hid_descriptor != NULL);
- TU_VERIFY(tud_control_xfer(rhport, request, (void*) p_hid->hid_descriptor, p_hid->hid_descriptor->bLength));
+ TU_VERIFY(p_hid->hid_descriptor);
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)(uintptr_t) p_hid->hid_descriptor, p_hid->hid_descriptor->bLength));
}
else if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT)
{
uint8_t const * desc_report = tud_hid_descriptor_report_cb(hid_itf);
- tud_control_xfer(rhport, request, (void*) desc_report, p_hid->report_desc_len);
+ tud_control_xfer(rhport, request, (void*)(uintptr_t) desc_report, p_hid->report_desc_len);
}
else
{
@@ -282,7 +282,7 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
uint8_t const report_id = tu_u16_low(request->wValue);
uint8_t* report_buf = p_hid->epin_buf;
- uint16_t req_len = request->wLength;
+ uint16_t req_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE);
uint16_t xferlen = 0;
@@ -314,7 +314,7 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
uint8_t const report_id = tu_u16_low(request->wValue);
uint8_t const* report_buf = p_hid->epout_buf;
- uint16_t report_len = request->wLength;
+ uint16_t report_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE);
// If host request a specific Report ID, extract report ID in buffer before invoking callback
if ( (report_id != HID_REPORT_TYPE_INVALID) && (report_len > 1) && (report_id == report_buf[0]) )
diff --git a/src/class/hid/hid_device.h b/src/class/hid/hid_device.h
index e2c950dd1..078b67349 100644
--- a/src/class/hid/hid_device.h
+++ b/src/class/hid/hid_device.h
@@ -195,16 +195,7 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y
HID_REPORT_COUNT ( 1 ) ,\
HID_REPORT_SIZE ( 8 ) ,\
HID_INPUT ( HID_CONSTANT ) ,\
- /* 6-byte Keycodes */ \
- HID_USAGE_PAGE ( HID_USAGE_PAGE_KEYBOARD ) ,\
- HID_USAGE_MIN ( 0 ) ,\
- HID_USAGE_MAX_N ( 255, 2 ) ,\
- HID_LOGICAL_MIN ( 0 ) ,\
- HID_LOGICAL_MAX_N( 255, 2 ) ,\
- HID_REPORT_COUNT ( 6 ) ,\
- HID_REPORT_SIZE ( 8 ) ,\
- HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\
- /* 5-bit LED Indicator Kana | Compose | ScrollLock | CapsLock | NumLock */ \
+ /* Output 5-bit LED Indicator Kana | Compose | ScrollLock | CapsLock | NumLock */ \
HID_USAGE_PAGE ( HID_USAGE_PAGE_LED ) ,\
HID_USAGE_MIN ( 1 ) ,\
HID_USAGE_MAX ( 5 ) ,\
@@ -215,6 +206,15 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y
HID_REPORT_COUNT ( 1 ) ,\
HID_REPORT_SIZE ( 3 ) ,\
HID_OUTPUT ( HID_CONSTANT ) ,\
+ /* 6-byte Keycodes */ \
+ HID_USAGE_PAGE ( HID_USAGE_PAGE_KEYBOARD ) ,\
+ HID_USAGE_MIN ( 0 ) ,\
+ HID_USAGE_MAX_N ( 255, 2 ) ,\
+ HID_LOGICAL_MIN ( 0 ) ,\
+ HID_LOGICAL_MAX_N( 255, 2 ) ,\
+ HID_REPORT_COUNT ( 6 ) ,\
+ HID_REPORT_SIZE ( 8 ) ,\
+ HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\
HID_COLLECTION_END \
// Mouse Report Descriptor Template
@@ -310,8 +310,8 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y
HID_COLLECTION_END \
// Gamepad Report Descriptor Template
-// with 16 buttons, 2 joysticks and 1 hat/dpad with following layout
-// | X | Y | Z | Rz | Rx | Ry (1 byte each) | hat/DPAD (1 byte) | Button Map (2 bytes) |
+// with 32 buttons, 2 joysticks and 1 hat/dpad with following layout
+// | X | Y | Z | Rz | Rx | Ry (1 byte each) | hat/DPAD (1 byte) | Button Map (4 bytes) |
#define TUD_HID_REPORT_DESC_GAMEPAD(...) \
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
HID_USAGE ( HID_USAGE_DESKTOP_GAMEPAD ) ,\
@@ -319,37 +319,37 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y
/* Report ID if any */\
__VA_ARGS__ \
/* 8 bit X, Y, Z, Rz, Rx, Ry (min -127, max 127 ) */ \
- HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
- HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
- HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
- HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
- HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
- HID_USAGE ( HID_USAGE_DESKTOP_RX ) ,\
- HID_USAGE ( HID_USAGE_DESKTOP_RY ) ,\
- HID_LOGICAL_MIN ( 0x81 ) ,\
- HID_LOGICAL_MAX ( 0x7f ) ,\
- HID_REPORT_COUNT ( 6 ) ,\
- HID_REPORT_SIZE ( 8 ) ,\
- HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
+ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
+ HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
+ HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
+ HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
+ HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
+ HID_USAGE ( HID_USAGE_DESKTOP_RX ) ,\
+ HID_USAGE ( HID_USAGE_DESKTOP_RY ) ,\
+ HID_LOGICAL_MIN ( 0x81 ) ,\
+ HID_LOGICAL_MAX ( 0x7f ) ,\
+ HID_REPORT_COUNT ( 6 ) ,\
+ HID_REPORT_SIZE ( 8 ) ,\
+ HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
/* 8 bit DPad/Hat Button Map */ \
- HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
- HID_USAGE ( HID_USAGE_DESKTOP_HAT_SWITCH ) ,\
- HID_LOGICAL_MIN ( 1 ) ,\
- HID_LOGICAL_MAX ( 8 ) ,\
- HID_PHYSICAL_MIN ( 0 ) ,\
- HID_PHYSICAL_MAX_N ( 315, 2 ) ,\
- HID_REPORT_COUNT ( 1 ) ,\
- HID_REPORT_SIZE ( 8 ) ,\
- HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
- /* 16 bit Button Map */ \
- HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
- HID_USAGE_MIN ( 1 ) ,\
- HID_USAGE_MAX ( 32 ) ,\
- HID_LOGICAL_MIN ( 0 ) ,\
- HID_LOGICAL_MAX ( 1 ) ,\
- HID_REPORT_COUNT ( 32 ) ,\
- HID_REPORT_SIZE ( 1 ) ,\
- HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
+ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
+ HID_USAGE ( HID_USAGE_DESKTOP_HAT_SWITCH ) ,\
+ HID_LOGICAL_MIN ( 1 ) ,\
+ HID_LOGICAL_MAX ( 8 ) ,\
+ HID_PHYSICAL_MIN ( 0 ) ,\
+ HID_PHYSICAL_MAX_N ( 315, 2 ) ,\
+ HID_REPORT_COUNT ( 1 ) ,\
+ HID_REPORT_SIZE ( 8 ) ,\
+ HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
+ /* 32 bit Button Map */ \
+ HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
+ HID_USAGE_MIN ( 1 ) ,\
+ HID_USAGE_MAX ( 32 ) ,\
+ HID_LOGICAL_MIN ( 0 ) ,\
+ HID_LOGICAL_MAX ( 1 ) ,\
+ HID_REPORT_COUNT ( 32 ) ,\
+ HID_REPORT_SIZE ( 1 ) ,\
+ HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
HID_COLLECTION_END \
// HID Generic Input & Output
@@ -364,14 +364,14 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y
/* Input */ \
HID_USAGE ( 0x02 ),\
HID_LOGICAL_MIN ( 0x00 ),\
- HID_LOGICAL_MAX ( 0xff ),\
+ HID_LOGICAL_MAX_N ( 0xff, 2 ),\
HID_REPORT_SIZE ( 8 ),\
HID_REPORT_COUNT( report_size ),\
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\
/* Output */ \
HID_USAGE ( 0x03 ),\
HID_LOGICAL_MIN ( 0x00 ),\
- HID_LOGICAL_MAX ( 0xff ),\
+ HID_LOGICAL_MAX_N ( 0xff, 2 ),\
HID_REPORT_SIZE ( 8 ),\
HID_REPORT_COUNT( report_size ),\
HID_OUTPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\
diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c
index 8c66477b3..f19f1ba81 100644
--- a/src/class/hid/hid_host.c
+++ b/src/class/hid/hid_host.c
@@ -291,7 +291,15 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType);
+ // first endpoint may be OUT, skip to IN endpoint
// TODO also open endpoint OUT
+ if(tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_OUT)
+ {
+ p_desc = tu_desc_next(p_desc);
+ desc_ep = (tusb_desc_endpoint_t const *) p_desc;
+ TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType);
+ }
+
TU_ASSERT( usbh_edpt_open(rhport, dev_addr, desc_ep) );
hidh_interface_t* hid_itf = get_instance(dev_addr, hid_dev->inst_count);
@@ -299,7 +307,7 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
hid_itf->itf_num = desc_itf->bInterfaceNumber;
hid_itf->ep_in = desc_ep->bEndpointAddress;
- hid_itf->epin_size = desc_ep->wMaxPacketSize.size;
+ hid_itf->epin_size = tu_edpt_packet_size(desc_ep);
// Assume bNumDescriptors = 1
hid_itf->report_desc_type = desc_hid->bReportType;
diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c
index eb85b668c..b08b362f9 100644
--- a/src/class/midi/midi_device.c
+++ b/src/class/midi/midi_device.c
@@ -279,6 +279,12 @@ uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, uint8_t const*
stream->buffer[0] = (cable_num << 4) | msg;
stream->total = 4;
}
+ else if ( msg == 0xC || msg == 0xD)
+ {
+ // Channel Voice Messages, two-byte variants (Program Change and Channel Pressure)
+ stream->buffer[0] = (cable_num << 4) | msg;
+ stream->total = 3;
+ }
else if ( msg == 0xf )
{
// System message
@@ -433,6 +439,7 @@ uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint
break;
}
}
+ TU_ASSERT(p_midi);
p_midi->itf_num = desc_midi->bInterfaceNumber;
(void) p_midi->itf_num;
diff --git a/src/class/msc/msc_device.c b/src/class/msc/msc_device.c
index 014e8b69e..97837b114 100644
--- a/src/class/msc/msc_device.c
+++ b/src/class/msc/msc_device.c
@@ -1,4 +1,4 @@
-/*
+/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
@@ -46,7 +46,8 @@ enum
MSC_STAGE_CMD = 0,
MSC_STAGE_DATA,
MSC_STAGE_STATUS,
- MSC_STAGE_STATUS_SENT
+ MSC_STAGE_STATUS_SENT,
+ MSC_STAGE_NEED_RESET,
};
typedef struct
@@ -61,7 +62,7 @@ typedef struct
// Bulk Only Transfer (BOT) Protocol
uint8_t stage;
- uint32_t total_len;
+ uint32_t total_len; // byte to be transferred, can be smaller than total_bytes in cbw
uint32_t xferred_len; // numbered of bytes transferred so far in the Data Stage
// Sense Response Data
@@ -78,7 +79,55 @@ CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static uint8_t _mscd_buf[CFG_TUD_MSC_EP_
//--------------------------------------------------------------------+
static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize);
static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc);
+
static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc);
+static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint32_t xferred_bytes);
+
+TU_ATTR_ALWAYS_INLINE static inline bool is_data_in(uint8_t dir)
+{
+ return tu_bit_test(dir, 7);
+}
+
+static inline bool send_csw(uint8_t rhport, mscd_interface_t* p_msc)
+{
+ // Data residue is always = host expect - actual transferred
+ p_msc->csw.data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len;
+
+ p_msc->stage = MSC_STAGE_STATUS_SENT;
+ return usbd_edpt_xfer(rhport, p_msc->ep_in , (uint8_t*) &p_msc->csw, sizeof(msc_csw_t));
+}
+
+static inline bool prepare_cbw(uint8_t rhport, mscd_interface_t* p_msc)
+{
+ p_msc->stage = MSC_STAGE_CMD;
+ return usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t));
+}
+
+static void fail_scsi_op(uint8_t rhport, mscd_interface_t* p_msc, uint8_t status)
+{
+ msc_cbw_t const * p_cbw = &p_msc->cbw;
+ msc_csw_t * p_csw = &p_msc->csw;
+
+ p_csw->status = status;
+ p_csw->data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len;
+ p_msc->stage = MSC_STAGE_STATUS;
+
+ // failed but sense key is not set: default to Illegal Request
+ if ( p_msc->sense_key == 0 ) tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
+
+ // If there is data stage and not yet complete, stall it
+ if ( p_cbw->total_bytes && p_csw->data_residue )
+ {
+ if ( is_data_in(p_cbw->dir) )
+ {
+ usbd_edpt_stall(rhport, p_msc->ep_in);
+ }
+ else
+ {
+ usbd_edpt_stall(rhport, p_msc->ep_out);
+ }
+ }
+}
static inline uint32_t rdwr10_get_lba(uint8_t const command[])
{
@@ -89,15 +138,65 @@ static inline uint32_t rdwr10_get_lba(uint8_t const command[])
return tu_ntohl(lba);
}
-static inline uint16_t rdwr10_get_blockcount(uint8_t const command[])
+static inline uint16_t rdwr10_get_blockcount(msc_cbw_t const* cbw)
{
- // use offsetof to avoid pointer to the odd/misaligned address
- uint16_t const block_count = tu_unaligned_read16(command + offsetof(scsi_write10_t, block_count));
-
- // block count is in Big Endian
+ uint16_t const block_count = tu_unaligned_read16(cbw->command + offsetof(scsi_write10_t, block_count));
return tu_ntohs(block_count);
}
+static inline uint16_t rdwr10_get_blocksize(msc_cbw_t const* cbw)
+{
+ // first extract block count in the command
+ uint16_t const block_count = rdwr10_get_blockcount(cbw);
+
+ // invalid block count
+ if (block_count == 0) return 0;
+
+ return (uint16_t) (cbw->total_bytes / block_count);
+}
+
+uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw)
+{
+ uint8_t status = MSC_CSW_STATUS_PASSED;
+ uint16_t const block_count = rdwr10_get_blockcount(cbw);
+
+ if ( cbw->total_bytes == 0 )
+ {
+ if ( block_count )
+ {
+ TU_LOG(MSC_DEBUG, " SCSI case 2 (Hn < Di) or case 3 (Hn < Do) \r\n");
+ status = MSC_CSW_STATUS_PHASE_ERROR;
+ }else
+ {
+ // no data transfer, only exist in complaint test suite
+ }
+ }else
+ {
+ if ( SCSI_CMD_READ_10 == cbw->command[0] && !is_data_in(cbw->dir) )
+ {
+ TU_LOG(MSC_DEBUG, " SCSI case 10 (Ho <> Di)\r\n");
+ status = MSC_CSW_STATUS_PHASE_ERROR;
+ }
+ else if ( SCSI_CMD_WRITE_10 == cbw->command[0] && is_data_in(cbw->dir) )
+ {
+ TU_LOG(MSC_DEBUG, " SCSI case 8 (Hi <> Do)\r\n");
+ status = MSC_CSW_STATUS_PHASE_ERROR;
+ }
+ else if ( 0 == block_count )
+ {
+ TU_LOG(MSC_DEBUG, " SCSI case 4 Hi > Dn (READ10) or case 9 Ho > Dn (WRITE10) \r\n");
+ status = MSC_CSW_STATUS_FAILED;
+ }
+ else if ( cbw->total_bytes / block_count == 0 )
+ {
+ TU_LOG(MSC_DEBUG, " Computed block size = 0. SCSI case 7 Hi < Di (READ10) or case 13 Ho < Do (WRIT10)\r\n");
+ status = MSC_CSW_STATUS_PHASE_ERROR;
+ }
+ }
+
+ return status;
+}
+
//--------------------------------------------------------------------+
// Debug
//--------------------------------------------------------------------+
@@ -164,7 +263,7 @@ uint16_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
// msc driver length is fixed
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
- // Max length mus be at least 1 interface + 2 endpoints
+ // Max length must be at least 1 interface + 2 endpoints
TU_ASSERT(max_len >= drv_len, 0);
mscd_interface_t * p_msc = &_mscd_itf;
@@ -174,35 +273,93 @@ uint16_t mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
TU_ASSERT( usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_msc->ep_out, &p_msc->ep_in), 0 );
// Prepare for Command Block Wrapper
- if ( !usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) )
- {
- TU_LOG_FAILED();
- TU_BREAKPOINT();
- }
+ TU_ASSERT( prepare_cbw(rhport, p_msc), drv_len);
return drv_len;
}
+static void proc_bot_reset(mscd_interface_t* p_msc)
+{
+ p_msc->stage = MSC_STAGE_CMD;
+ p_msc->total_len = 0;
+ p_msc->xferred_len = 0;
+
+ p_msc->sense_key = 0;
+ p_msc->add_sense_code = 0;
+ p_msc->add_sense_qualifier = 0;
+}
+
// Invoked when a control transfer occurred on an interface of this class
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
// return false to stall control endpoint (e.g unsupported request)
-bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * p_request)
+bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
{
// nothing to do with DATA & ACK stage
if (stage != CONTROL_STAGE_SETUP) return true;
- // Handle class request only
- TU_VERIFY(p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
+ mscd_interface_t* p_msc = &_mscd_itf;
+
+ // Clear Endpoint Feature (stall) for recovery
+ if ( TUSB_REQ_TYPE_STANDARD == request->bmRequestType_bit.type &&
+ TUSB_REQ_RCPT_ENDPOINT == request->bmRequestType_bit.recipient &&
+ TUSB_REQ_CLEAR_FEATURE == request->bRequest &&
+ TUSB_REQ_FEATURE_EDPT_HALT == request->wValue )
+ {
+ uint8_t const ep_addr = tu_u16_low(request->wIndex);
+
+ if ( p_msc->stage == MSC_STAGE_NEED_RESET )
+ {
+ // reset recovery is required to recover from this stage
+ // Clear Stall request cannot resolve this -> continue to stall endpoint
+ usbd_edpt_stall(rhport, ep_addr);
+ }
+ else
+ {
+ if ( ep_addr == p_msc->ep_in )
+ {
+ if ( p_msc->stage == MSC_STAGE_STATUS )
+ {
+ // resume sending SCSI status if we are in this stage previously before stalled
+ TU_ASSERT( send_csw(rhport, p_msc) );
+ }
+ }
+ else if ( ep_addr == p_msc->ep_out )
+ {
+ if ( p_msc->stage == MSC_STAGE_CMD )
+ {
+ // part of reset recovery (probably due to invalid CBW) -> prepare for new command
+ // Note: skip if already queued previously
+ if ( usbd_edpt_ready(rhport, p_msc->ep_out) )
+ {
+ TU_ASSERT( prepare_cbw(rhport, p_msc) );
+ }
+ }
+ }
+ }
+
+ return true;
+ }
+
+ // From this point only handle class request only
+ TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
- switch ( p_request->bRequest )
+ switch ( request->bRequest )
{
case MSC_REQ_RESET:
- // TODO: Actually reset interface.
- tud_control_status(rhport, p_request);
+ TU_LOG(MSC_DEBUG, " MSC BOT Reset\r\n");
+ TU_VERIFY(request->wValue == 0 && request->wLength == 0);
+
+ // driver state reset
+ proc_bot_reset(p_msc);
+
+ tud_control_status(rhport, request);
break;
case MSC_REQ_GET_MAX_LUN:
{
+ TU_LOG(MSC_DEBUG, " MSC Get Max Lun\r\n");
+ TU_VERIFY(request->wValue == 0 && request->wLength == 1);
+
uint8_t maxlun = 1;
if (tud_msc_get_maxlun_cb) maxlun = tud_msc_get_maxlun_cb();
TU_VERIFY(maxlun);
@@ -210,7 +367,7 @@ bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
// MAX LUN is minus 1 by specs
maxlun--;
- tud_control_xfer(rhport, p_request, &maxlun, 1);
+ tud_control_xfer(rhport, request, &maxlun, 1);
}
break;
@@ -222,6 +379,8 @@ bool mscd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
{
+ (void) event;
+
mscd_interface_t* p_msc = &_mscd_itf;
msc_cbw_t const * p_cbw = &p_msc->cbw;
msc_csw_t * p_csw = &p_msc->csw;
@@ -233,46 +392,79 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
// Complete IN while waiting for CMD is usually Status of previous SCSI op, ignore it
if(ep_addr != p_msc->ep_out) return true;
- TU_ASSERT( event == XFER_RESULT_SUCCESS &&
- xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE );
+ if ( !(xferred_bytes == sizeof(msc_cbw_t) && p_cbw->signature == MSC_CBW_SIGNATURE) )
+ {
+ TU_LOG(MSC_DEBUG, " SCSI CBW is not valid\r\n");
+
+ // BOT 6.6.1 If CBW is not valid stall both endpoints until reset recovery
+ p_msc->stage = MSC_STAGE_NEED_RESET;
+
+ // invalid CBW stall both endpoints
+ usbd_edpt_stall(rhport, p_msc->ep_in);
+ usbd_edpt_stall(rhport, p_msc->ep_out);
+
+ return false;
+ }
TU_LOG(MSC_DEBUG, " SCSI Command: %s\r\n", tu_lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0]));
- // TU_LOG_MEM(MSC_DEBUG, p_cbw, xferred_bytes, 2);
+ //TU_LOG_MEM(MSC_DEBUG, p_cbw, xferred_bytes, 2);
p_csw->signature = MSC_CSW_SIGNATURE;
p_csw->tag = p_cbw->tag;
p_csw->data_residue = 0;
+ p_csw->status = MSC_CSW_STATUS_PASSED;
/*------------- Parse command and prepare DATA -------------*/
p_msc->stage = MSC_STAGE_DATA;
p_msc->total_len = p_cbw->total_bytes;
p_msc->xferred_len = 0;
- if (SCSI_CMD_READ_10 == p_cbw->command[0])
- {
- proc_read10_cmd(rhport, p_msc);
- }
- else if (SCSI_CMD_WRITE_10 == p_cbw->command[0])
+ // Read10 or Write10
+ if ( (SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0]) )
{
- proc_write10_cmd(rhport, p_msc);
+ uint8_t const status = rdwr10_validate_cmd(p_cbw);
+
+ if ( status != MSC_CSW_STATUS_PASSED)
+ {
+ fail_scsi_op(rhport, p_msc, status);
+ }else if ( p_cbw->total_bytes )
+ {
+ if (SCSI_CMD_READ_10 == p_cbw->command[0])
+ {
+ proc_read10_cmd(rhport, p_msc);
+ }else
+ {
+ proc_write10_cmd(rhport, p_msc);
+ }
+ }else
+ {
+ // no data transfer, only exist in complaint test suite
+ p_msc->stage = MSC_STAGE_STATUS;
+ }
}
else
{
// For other SCSI commands
// 1. OUT : queue transfer (invoke app callback after done)
// 2. IN & Zero: Process if is built-in, else Invoke app callback. Skip DATA if zero length
- if ( (p_cbw->total_bytes > 0 ) && !tu_bit_test(p_cbw->dir, 7) )
+ if ( (p_cbw->total_bytes > 0 ) && !is_data_in(p_cbw->dir) )
{
- // queue transfer
- TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len) );
+ if (p_cbw->total_bytes > sizeof(_mscd_buf))
+ {
+ TU_LOG(MSC_DEBUG, " SCSI reject non READ10/WRITE10 with large data\r\n");
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
+ }else
+ {
+ // Didn't check for case 9 (Ho > Dn), which requires examining scsi command first
+ // but it is OK to just receive data then responded with failed status
+ TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len) );
+ }
}else
{
- int32_t resplen;
-
// First process if it is a built-in commands
- resplen = proc_builtin_scsi(p_cbw->lun, p_cbw->command, _mscd_buf, sizeof(_mscd_buf));
+ int32_t resplen = proc_builtin_scsi(p_cbw->lun, p_cbw->command, _mscd_buf, sizeof(_mscd_buf));
- // Not built-in, invoke user callback
+ // Invoke user callback if not built-in
if ( (resplen < 0) && (p_msc->sense_key == 0) )
{
resplen = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->total_len);
@@ -280,30 +472,37 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
if ( resplen < 0 )
{
- p_msc->total_len = 0;
- p_csw->status = MSC_CSW_STATUS_FAILED;
- p_msc->stage = MSC_STAGE_STATUS;
-
- // failed but senskey is not set: default to Illegal Request
- if ( p_msc->sense_key == 0 ) tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
-
- // Stall bulk In if needed
- if (p_cbw->total_bytes) usbd_edpt_stall(rhport, p_msc->ep_in);
+ // unsupported command
+ TU_LOG(MSC_DEBUG, " SCSI unsupported command\r\n");
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}
- else
+ else if (resplen == 0)
{
- p_msc->total_len = (uint32_t) resplen;
- p_csw->status = MSC_CSW_STATUS_PASSED;
-
- if (p_msc->total_len)
+ if (p_cbw->total_bytes)
{
- TU_ASSERT( p_cbw->total_bytes >= p_msc->total_len ); // cannot return more than host expect
- TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, p_msc->total_len) );
+ // 6.7 The 13 Cases: case 4 (Hi > Dn)
+ // TU_LOG(MSC_DEBUG, " SCSI case 4 (Hi > Dn): %lu\r\n", p_cbw->total_bytes);
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}else
{
+ // case 1 Hn = Dn: all good
p_msc->stage = MSC_STAGE_STATUS;
}
}
+ else
+ {
+ if ( p_cbw->total_bytes == 0 )
+ {
+ // 6.7 The 13 Cases: case 2 (Hn < Di)
+ // TU_LOG(MSC_DEBUG, " SCSI case 2 (Hn < Di): %lu\r\n", p_cbw->total_bytes);
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
+ }else
+ {
+ // cannot return more than host expect
+ p_msc->total_len = tu_min32((uint32_t) resplen, p_cbw->total_bytes);
+ TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, p_msc->total_len) );
+ }
+ }
}
}
break;
@@ -312,87 +511,51 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
TU_LOG(MSC_DEBUG, " SCSI Data\r\n");
//TU_LOG_MEM(MSC_DEBUG, _mscd_buf, xferred_bytes, 2);
- // OUT transfer, invoke callback if needed
- if ( !tu_bit_test(p_cbw->dir, 7) )
+ if (SCSI_CMD_READ_10 == p_cbw->command[0])
{
- if ( SCSI_CMD_WRITE_10 != p_cbw->command[0] )
- {
- int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->total_len);
+ p_msc->xferred_len += xferred_bytes;
- if ( cb_result < 0 )
- {
- p_csw->status = MSC_CSW_STATUS_FAILED;
- tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation
- }else
- {
- p_csw->status = MSC_CSW_STATUS_PASSED;
- }
- }
- else
+ if ( p_msc->xferred_len >= p_msc->total_len )
{
- uint16_t const block_sz = p_cbw->total_bytes / rdwr10_get_blockcount(p_cbw->command);
-
- // Adjust lba with transferred bytes
- uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
+ // Data Stage is complete
+ p_msc->stage = MSC_STAGE_STATUS;
+ }else
+ {
+ proc_read10_cmd(rhport, p_msc);
+ }
+ }
+ else if (SCSI_CMD_WRITE_10 == p_cbw->command[0])
+ {
+ proc_write10_new_data(rhport, p_msc, xferred_bytes);
+ }
+ else
+ {
+ p_msc->xferred_len += xferred_bytes;
- // Application can consume smaller bytes
- int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, p_msc->xferred_len % block_sz, _mscd_buf, xferred_bytes);
+ // OUT transfer, invoke callback if needed
+ if ( !is_data_in(p_cbw->dir) )
+ {
+ int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_buf, p_msc->total_len);
- if ( nbytes < 0 )
+ if ( cb_result < 0 )
{
- // negative means error -> skip to status phase, status in CSW set to failed
- p_csw->data_residue = p_cbw->total_bytes - p_msc->xferred_len;
- p_csw->status = MSC_CSW_STATUS_FAILED;
- p_msc->stage = MSC_STAGE_STATUS;
-
- tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation
- break;
+ // unsupported command
+ TU_LOG(MSC_DEBUG, " SCSI unsupported command\r\n");
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}else
{
- // Application consume less than what we got (including zero)
- if ( nbytes < (int32_t) xferred_bytes )
- {
- if ( nbytes > 0 )
- {
- p_msc->xferred_len += nbytes;
- memmove(_mscd_buf, _mscd_buf+nbytes, xferred_bytes-nbytes);
- }
-
- // simulate an transfer complete with adjusted parameters --> this driver callback will fired again
- dcd_event_xfer_complete(rhport, p_msc->ep_out, xferred_bytes-nbytes, XFER_RESULT_SUCCESS, false);
-
- return true; // skip the rest
- }
- else
- {
- // Application consume all bytes in our buffer. Nothing to do, process with normal flow
- }
+ // TODO haven't implement this scenario any further yet
}
}
- }
- // Accumulate data so far
- p_msc->xferred_len += xferred_bytes;
-
- if ( p_msc->xferred_len >= p_msc->total_len )
- {
- // Data Stage is complete
- p_msc->stage = MSC_STAGE_STATUS;
- }
- else
- {
- // READ10 & WRITE10 Can be executed with large bulk of data e.g write 8K bytes (several flash write)
- // We break it into multiple smaller command whose data size is up to CFG_TUD_MSC_EP_BUFSIZE
- if (SCSI_CMD_READ_10 == p_cbw->command[0])
+ if ( p_msc->xferred_len >= p_msc->total_len )
{
- proc_read10_cmd(rhport, p_msc);
+ // Data Stage is complete
+ p_msc->stage = MSC_STAGE_STATUS;
}
- else if (SCSI_CMD_WRITE_10 == p_cbw->command[0])
- {
- proc_write10_cmd(rhport, p_msc);
- }else
+ else
{
- // No other command take more than one transfer yet -> unlikely error
+ // This scenario with command that take more than one transfer is already rejected at Command stage
TU_BREAKPOINT();
}
}
@@ -406,7 +569,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
// Wait for the Status phase to complete
if( (ep_addr == p_msc->ep_in) && (xferred_bytes == sizeof(msc_csw_t)) )
{
- TU_LOG(MSC_DEBUG, " SCSI Status: %u\r\n", p_csw->status);
+ TU_LOG(MSC_DEBUG, " SCSI Status = %u\r\n", p_csw->status);
// TU_LOG_MEM(MSC_DEBUG, p_csw, xferred_bytes, 2);
// Invoke complete callback if defined
@@ -427,11 +590,11 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
break;
}
- // Move to default CMD stage
- p_msc->stage = MSC_STAGE_CMD;
-
- // Queue for the next CBW
- TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) );
+ TU_ASSERT( prepare_cbw(rhport, p_msc) );
+ }else
+ {
+ // Any xfer ended here is consider unknown error, ignore it
+ TU_LOG1(" Warning expect SCSI Status but received unknown data\r\n");
}
break;
@@ -440,22 +603,30 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
if ( p_msc->stage == MSC_STAGE_STATUS )
{
- // Either endpoints is stalled, need to wait until it is cleared by host
- if ( usbd_edpt_stalled(rhport, p_msc->ep_in) || usbd_edpt_stalled(rhport, p_msc->ep_out) )
+ // skip status if epin is currently stalled, will do it when received Clear Stall request
+ if ( !usbd_edpt_stalled(rhport, p_msc->ep_in) )
{
- // simulate an transfer complete with adjusted parameters --> this driver callback will fired again
- // and response with status phase after halted endpoints are cleared.
- // note: use ep_out to prevent confusing with STATUS complete
- dcd_event_xfer_complete(rhport, p_msc->ep_out, 0, XFER_RESULT_SUCCESS, false);
+ if ( (p_cbw->total_bytes > p_msc->xferred_len) && is_data_in(p_cbw->dir) )
+ {
+ // 6.7 The 13 Cases: case 5 (Hi > Di): STALL before status
+ // TU_LOG(MSC_DEBUG, " SCSI case 5 (Hi > Di): %lu > %lu\r\n", p_cbw->total_bytes, p_msc->xferred_len);
+ usbd_edpt_stall(rhport, p_msc->ep_in);
+ }else
+ {
+ TU_ASSERT( send_csw(rhport, p_msc) );
+ }
}
- else
- {
- // Move to Status Sent stage
- p_msc->stage = MSC_STAGE_STATUS_SENT;
- // Send SCSI Status
- TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_in , (uint8_t*) &p_msc->csw, sizeof(msc_csw_t)));
+ #if TU_CHECK_MCU(OPT_MCU_CXD56)
+ // WORKAROUND: cxd56 has its own nuttx usb stack which does not forward Set/ClearFeature(Endpoint) to DCD.
+ // There is no way for us to know when EP is un-stall, therefore we will unconditionally un-stall here and
+ // hope everything will work
+ if ( usbd_edpt_stalled(rhport, p_msc->ep_in) )
+ {
+ usbd_edpt_clear_stall(rhport, p_msc->ep_in);
+ send_csw(rhport, p_msc);
}
+ #endif
}
return true;
@@ -472,6 +643,8 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
(void) bufsize; // TODO refractor later
int32_t resplen;
+ mscd_interface_t* p_msc = &_mscd_itf;
+
switch ( scsi_cmd[0] )
{
case SCSI_CMD_TEST_UNIT_READY:
@@ -482,7 +655,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
resplen = - 1;
// If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
- if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
+ if ( p_msc->sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
}
break;
@@ -498,7 +671,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
resplen = - 1;
// If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
- if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
+ if ( p_msc->sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
}
}
break;
@@ -519,7 +692,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
resplen = -1;
// If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
- if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
+ if ( p_msc->sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
}else
{
scsi_read_capacity10_resp_t read_capa10;
@@ -555,7 +728,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
resplen = -1;
// If sense key is not set by callback, default to Logical Unit Not Ready, Cause Not Reportable
- if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
+ if ( p_msc->sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
}else
{
read_fmt_capa.block_num = tu_htonl(block_count);
@@ -600,9 +773,11 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
};
bool writable = true;
- if (tud_msc_is_writable_cb) {
- writable = tud_msc_is_writable_cb(lun);
+ if ( tud_msc_is_writable_cb )
+ {
+ writable = tud_msc_is_writable_cb(lun);
}
+
mode_resp.write_protected = !writable;
resplen = sizeof(mode_resp);
@@ -620,9 +795,9 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
sense_rsp.add_sense_len = sizeof(scsi_sense_fixed_resp_t) - 8;
- sense_rsp.sense_key = _mscd_itf.sense_key;
- sense_rsp.add_sense_code = _mscd_itf.add_sense_code;
- sense_rsp.add_sense_qualifier = _mscd_itf.add_sense_qualifier;
+ sense_rsp.sense_key = p_msc->sense_key;
+ sense_rsp.add_sense_code = p_msc->add_sense_code;
+ sense_rsp.add_sense_qualifier = p_msc->add_sense_qualifier;
resplen = sizeof(sense_rsp);
memcpy(buffer, &sense_rsp, resplen);
@@ -641,13 +816,9 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
{
msc_cbw_t const * p_cbw = &p_msc->cbw;
- msc_csw_t * p_csw = &p_msc->csw;
-
- uint16_t const block_cnt = rdwr10_get_blockcount(p_cbw->command);
- TU_ASSERT(block_cnt, ); // prevent div by zero
- uint16_t const block_sz = p_cbw->total_bytes / block_cnt;
- TU_ASSERT(block_sz, ); // prevent div by zero
+ // block size already verified not zero
+ uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
// Adjust lba with transferred bytes
uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
@@ -656,16 +827,18 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len);
// Application can consume smaller bytes
- nbytes = tud_msc_read10_cb(p_cbw->lun, lba, p_msc->xferred_len % block_sz, _mscd_buf, (uint32_t) nbytes);
+ uint32_t const offset = p_msc->xferred_len % block_sz;
+ nbytes = tud_msc_read10_cb(p_cbw->lun, lba, offset, _mscd_buf, (uint32_t) nbytes);
if ( nbytes < 0 )
{
- // negative means error -> pipe is stalled & status in CSW set to failed
- p_csw->data_residue = p_cbw->total_bytes - p_msc->xferred_len;
- p_csw->status = MSC_CSW_STATUS_FAILED;
+ // negative means error -> endpoint is stalled & status in CSW set to failed
+ TU_LOG(MSC_DEBUG, " tud_msc_read10_cb() return -1\r\n");
- tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); // Sense = Invalid Command Operation
- usbd_edpt_stall(rhport, p_msc->ep_in);
+ // Sense = Flash not ready for access
+ tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_MEDIUM_ERROR, 0x33, 0x00);
+
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
}
else if ( nbytes == 0 )
{
@@ -682,16 +855,18 @@ static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
{
msc_cbw_t const * p_cbw = &p_msc->cbw;
bool writable = true;
- if (tud_msc_is_writable_cb) {
+
+ if ( tud_msc_is_writable_cb )
+ {
writable = tud_msc_is_writable_cb(p_cbw->lun);
}
- if (!writable) {
- msc_csw_t* p_csw = &p_msc->csw;
- p_csw->data_residue = p_cbw->total_bytes;
- p_csw->status = MSC_CSW_STATUS_FAILED;
- tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_DATA_PROTECT, 0x27, 0x00); // Sense = Write protected
- usbd_edpt_stall(rhport, p_msc->ep_out);
+ if ( !writable )
+ {
+ // Not writable, complete this SCSI op with error
+ // Sense = Write protected
+ tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_DATA_PROTECT, 0x27, 0x00);
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
return;
}
@@ -702,4 +877,63 @@ static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, nbytes), );
}
+// process new data arrived from WRITE10
+static void proc_write10_new_data(uint8_t rhport, mscd_interface_t* p_msc, uint32_t xferred_bytes)
+{
+ msc_cbw_t const * p_cbw = &p_msc->cbw;
+
+ // block size already verified not zero
+ uint16_t const block_sz = rdwr10_get_blocksize(p_cbw);
+
+ // Adjust lba with transferred bytes
+ uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
+
+ // Invoke callback to consume new data
+ uint32_t const offset = p_msc->xferred_len % block_sz;
+ int32_t nbytes = tud_msc_write10_cb(p_cbw->lun, lba, offset, _mscd_buf, xferred_bytes);
+
+ if ( nbytes < 0 )
+ {
+ // negative means error -> failed this scsi op
+ TU_LOG(MSC_DEBUG, " tud_msc_write10_cb() return -1\r\n");
+
+ // update actual byte before failed
+ p_msc->xferred_len += xferred_bytes;
+
+ // Sense = Flash not ready for access
+ tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_MEDIUM_ERROR, 0x33, 0x00);
+
+ fail_scsi_op(rhport, p_msc, MSC_CSW_STATUS_FAILED);
+ }else
+ {
+ // Application consume less than what we got (including zero)
+ if ( (uint32_t) nbytes < xferred_bytes )
+ {
+ if ( nbytes > 0 )
+ {
+ p_msc->xferred_len += nbytes;
+ memmove(_mscd_buf, _mscd_buf+nbytes, xferred_bytes-nbytes);
+ }
+
+ // simulate an transfer complete with adjusted parameters --> callback will be invoked with adjusted parameter
+ dcd_event_xfer_complete(rhport, p_msc->ep_out, xferred_bytes-nbytes, XFER_RESULT_SUCCESS, false);
+ }
+ else
+ {
+ // Application consume all bytes in our buffer
+ p_msc->xferred_len += xferred_bytes;
+
+ if ( p_msc->xferred_len >= p_msc->total_len )
+ {
+ // Data Stage is complete
+ p_msc->stage = MSC_STAGE_STATUS;
+ }else
+ {
+ // prepare to receive more data from host
+ proc_write10_cmd(rhport, p_msc);
+ }
+ }
+ }
+}
+
#endif
diff --git a/src/class/msc/msc_device.h b/src/class/msc/msc_device.h
index 8f90ef4ad..d32694340 100644
--- a/src/class/msc/msc_device.h
+++ b/src/class/msc/msc_device.h
@@ -140,7 +140,7 @@ TU_ATTR_WEAK void tud_msc_write10_complete_cb(uint8_t lun);
// Invoked when command in tud_msc_scsi_cb is complete
TU_ATTR_WEAK void tud_msc_scsi_complete_cb(uint8_t lun, uint8_t const scsi_cmd[16]);
-// Hook to make a mass storage device read-only. TODO remove
+// Invoked to check if device is writable as part of SCSI WRITE10
TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun);
//--------------------------------------------------------------------+
diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c
index 8069353cd..fa6519956 100644
--- a/src/class/msc/msc_host.c
+++ b/src/class/msc/msc_host.c
@@ -263,7 +263,7 @@ bool tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * buffer, uint32_
memcpy(cbw.command, &cmd_write10, cbw.cmd_len);
- return tuh_msc_scsi_command(dev_addr, &cbw, (void*) buffer, complete_cb);
+ return tuh_msc_scsi_command(dev_addr, &cbw, (void*)(uintptr_t) buffer, complete_cb);
}
#if 0
diff --git a/src/class/net/net_device.c b/src/class/net/ecm_rndis_device.c
index 9d9719ce4..c6cd388e3 100644
--- a/src/class/net/net_device.c
+++ b/src/class/net/ecm_rndis_device.c
@@ -27,7 +27,7 @@
#include "tusb_option.h"
-#if ( TUSB_OPT_DEVICE_ENABLED && CFG_TUD_NET )
+#if ( TUSB_OPT_DEVICE_ENABLED && CFG_TUD_ECM_RNDIS )
#include "device/usbd.h"
#include "device/usbd_pvt.h"
@@ -119,6 +119,8 @@ static void do_in_xfer(uint8_t *buf, uint16_t len)
void netd_report(uint8_t *buf, uint16_t len)
{
+ // skip if previous report not yet acknowledged by host
+ if ( usbd_edpt_busy(TUD_OPT_RHPORT, _netd_itf.ep_notif) ) return;
usbd_edpt_xfer(TUD_OPT_RHPORT, _netd_itf.ep_notif, buf, len);
}
@@ -407,8 +409,10 @@ bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
return true;
}
-bool tud_network_can_xmit(void)
+bool tud_network_can_xmit(uint16_t size)
{
+ (void)size;
+
return can_xmit;
}
diff --git a/src/class/net/ncm.h b/src/class/net/ncm.h
new file mode 100644
index 000000000..96ba11fbc
--- /dev/null
+++ b/src/class/net/ncm.h
@@ -0,0 +1,69 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2021, Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+
+#ifndef _TUSB_NCM_H_
+#define _TUSB_NCM_H_
+
+#include "common/tusb_common.h"
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+// Table 4.3 Data Class Interface Protocol Codes
+typedef enum
+{
+ NCM_DATA_PROTOCOL_NETWORK_TRANSFER_BLOCK = 0x01
+} ncm_data_interface_protocol_code_t;
+
+
+// Table 6.2 Class-Specific Request Codes for Network Control Model subclass
+typedef enum
+{
+ NCM_SET_ETHERNET_MULTICAST_FILTERS = 0x40,
+ NCM_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x41,
+ NCM_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x42,
+ NCM_SET_ETHERNET_PACKET_FILTER = 0x43,
+ NCM_GET_ETHERNET_STATISTIC = 0x44,
+ NCM_GET_NTB_PARAMETERS = 0x80,
+ NCM_GET_NET_ADDRESS = 0x81,
+ NCM_SET_NET_ADDRESS = 0x82,
+ NCM_GET_NTB_FORMAT = 0x83,
+ NCM_SET_NTB_FORMAT = 0x84,
+ NCM_GET_NTB_INPUT_SIZE = 0x85,
+ NCM_SET_NTB_INPUT_SIZE = 0x86,
+ NCM_GET_MAX_DATAGRAM_SIZE = 0x87,
+ NCM_SET_MAX_DATAGRAM_SIZE = 0x88,
+ NCM_GET_CRC_MODE = 0x89,
+ NCM_SET_CRC_MODE = 0x8A,
+} ncm_request_code_t;
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif
diff --git a/src/class/net/ncm_device.c b/src/class/net/ncm_device.c
new file mode 100644
index 000000000..3e131a85c
--- /dev/null
+++ b/src/class/net/ncm_device.c
@@ -0,0 +1,510 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jacob Berg Potter
+ * Copyright (c) 2020 Peter Lawrence
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#include "tusb_option.h"
+
+#if ( TUSB_OPT_DEVICE_ENABLED && CFG_TUD_NCM )
+
+#include "device/usbd.h"
+#include "device/usbd_pvt.h"
+#include "net_device.h"
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF
+//--------------------------------------------------------------------+
+
+#define NTH16_SIGNATURE 0x484D434E
+#define NDP16_SIGNATURE_NCM0 0x304D434E
+#define NDP16_SIGNATURE_NCM1 0x314D434E
+
+typedef struct TU_ATTR_PACKED
+{
+ uint16_t wLength;
+ uint16_t bmNtbFormatsSupported;
+ uint32_t dwNtbInMaxSize;
+ uint16_t wNdbInDivisor;
+ uint16_t wNdbInPayloadRemainder;
+ uint16_t wNdbInAlignment;
+ uint16_t wReserved;
+ uint32_t dwNtbOutMaxSize;
+ uint16_t wNdbOutDivisor;
+ uint16_t wNdbOutPayloadRemainder;
+ uint16_t wNdbOutAlignment;
+ uint16_t wNtbOutMaxDatagrams;
+} ntb_parameters_t;
+
+typedef struct TU_ATTR_PACKED
+{
+ uint32_t dwSignature;
+ uint16_t wHeaderLength;
+ uint16_t wSequence;
+ uint16_t wBlockLength;
+ uint16_t wNdpIndex;
+} nth16_t;
+
+typedef struct TU_ATTR_PACKED
+{
+ uint16_t wDatagramIndex;
+ uint16_t wDatagramLength;
+} ndp16_datagram_t;
+
+typedef struct TU_ATTR_PACKED
+{
+ uint32_t dwSignature;
+ uint16_t wLength;
+ uint16_t wNextNdpIndex;
+ ndp16_datagram_t datagram[];
+} ndp16_t;
+
+typedef union TU_ATTR_PACKED {
+ struct {
+ nth16_t nth;
+ ndp16_t ndp;
+ };
+ uint8_t data[CFG_TUD_NCM_IN_NTB_MAX_SIZE];
+} transmit_ntb_t;
+
+struct ecm_notify_struct
+{
+ tusb_control_request_t header;
+ uint32_t downlink, uplink;
+};
+
+typedef struct
+{
+ uint8_t itf_num; // Index number of Management Interface, +1 for Data Interface
+ uint8_t itf_data_alt; // Alternate setting of Data Interface. 0 : inactive, 1 : active
+
+ uint8_t ep_notif;
+ uint8_t ep_in;
+ uint8_t ep_out;
+
+ const ndp16_t *ndp;
+ uint8_t num_datagrams, current_datagram_index;
+
+ enum {
+ REPORT_SPEED,
+ REPORT_CONNECTED,
+ REPORT_DONE
+ } report_state;
+ bool report_pending;
+
+ uint8_t current_ntb; // Index in transmit_ntb[] that is currently being filled with datagrams
+ uint8_t datagram_count; // Number of datagrams in transmit_ntb[current_ntb]
+ uint16_t next_datagram_offset; // Offset in transmit_ntb[current_ntb].data to place the next datagram
+ uint16_t ntb_in_size; // Maximum size of transmitted (IN to host) NTBs; initially CFG_TUD_NCM_IN_NTB_MAX_SIZE
+ uint8_t max_datagrams_per_ntb; // Maximum number of datagrams per NTB; initially CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB
+
+ uint16_t nth_sequence; // Sequence number counter for transmitted NTBs
+
+ bool transferring;
+
+} ncm_interface_t;
+
+//--------------------------------------------------------------------+
+// INTERNAL OBJECT & FUNCTION DECLARATION
+//--------------------------------------------------------------------+
+
+CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static const ntb_parameters_t ntb_parameters = {
+ .wLength = sizeof(ntb_parameters_t),
+ .bmNtbFormatsSupported = 0x01,
+ .dwNtbInMaxSize = CFG_TUD_NCM_IN_NTB_MAX_SIZE,
+ .wNdbInDivisor = 4,
+ .wNdbInPayloadRemainder = 0,
+ .wNdbInAlignment = CFG_TUD_NCM_ALIGNMENT,
+ .wReserved = 0,
+ .dwNtbOutMaxSize = CFG_TUD_NCM_OUT_NTB_MAX_SIZE,
+ .wNdbOutDivisor = 4,
+ .wNdbOutPayloadRemainder = 0,
+ .wNdbOutAlignment = CFG_TUD_NCM_ALIGNMENT,
+ .wNtbOutMaxDatagrams = 0
+};
+
+CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static transmit_ntb_t transmit_ntb[2];
+
+CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN static uint8_t receive_ntb[CFG_TUD_NCM_OUT_NTB_MAX_SIZE];
+
+static ncm_interface_t ncm_interface;
+
+/*
+ * Set up the NTB state in ncm_interface to be ready to add datagrams.
+ */
+static void ncm_prepare_for_tx(void) {
+ ncm_interface.datagram_count = 0;
+ // datagrams start after all the headers
+ ncm_interface.next_datagram_offset = sizeof(nth16_t) + sizeof(ndp16_t)
+ + ((CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB + 1) * sizeof(ndp16_datagram_t));
+}
+
+/*
+ * If not already transmitting, start sending the current NTB to the host and swap buffers
+ * to start filling the other one with datagrams.
+ */
+static void ncm_start_tx(void) {
+ if (ncm_interface.transferring) {
+ return;
+ }
+
+ transmit_ntb_t *ntb = &transmit_ntb[ncm_interface.current_ntb];
+ size_t ntb_length = ncm_interface.next_datagram_offset;
+
+ // Fill in NTB header
+ ntb->nth.dwSignature = NTH16_SIGNATURE;
+ ntb->nth.wHeaderLength = sizeof(nth16_t);
+ ntb->nth.wSequence = ncm_interface.nth_sequence++;
+ ntb->nth.wBlockLength = ntb_length;
+ ntb->nth.wNdpIndex = sizeof(nth16_t);
+
+ // Fill in NDP16 header and terminator
+ ntb->ndp.dwSignature = NDP16_SIGNATURE_NCM0;
+ ntb->ndp.wLength = sizeof(ndp16_t) + (ncm_interface.datagram_count + 1) * sizeof(ndp16_datagram_t);
+ ntb->ndp.wNextNdpIndex = 0;
+ ntb->ndp.datagram[ncm_interface.datagram_count].wDatagramIndex = 0;
+ ntb->ndp.datagram[ncm_interface.datagram_count].wDatagramLength = 0;
+
+ // Kick off an endpoint transfer
+ usbd_edpt_xfer(TUD_OPT_RHPORT, ncm_interface.ep_in, ntb->data, ntb_length);
+ ncm_interface.transferring = true;
+
+ // Swap to the other NTB and clear it out
+ ncm_interface.current_ntb = 1 - ncm_interface.current_ntb;
+ ncm_prepare_for_tx();
+}
+
+static struct ecm_notify_struct ncm_notify_connected =
+{
+ .header = {
+ .bmRequestType_bit = {
+ .recipient = TUSB_REQ_RCPT_INTERFACE,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_IN
+ },
+ .bRequest = CDC_NOTIF_NETWORK_CONNECTION,
+ .wValue = 1 /* Connected */,
+ .wLength = 0,
+ },
+};
+
+static struct ecm_notify_struct ncm_notify_speed_change =
+{
+ .header = {
+ .bmRequestType_bit = {
+ .recipient = TUSB_REQ_RCPT_INTERFACE,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_IN
+ },
+ .bRequest = CDC_NOTIF_CONNECTION_SPEED_CHANGE,
+ .wLength = 8,
+ },
+ .downlink = 10000000,
+ .uplink = 10000000,
+};
+
+void tud_network_recv_renew(void)
+{
+ if (!ncm_interface.num_datagrams)
+ {
+ usbd_edpt_xfer(TUD_OPT_RHPORT, ncm_interface.ep_out, receive_ntb, sizeof(receive_ntb));
+ return;
+ }
+
+ const ndp16_t *ndp = ncm_interface.ndp;
+ const int i = ncm_interface.current_datagram_index;
+ ncm_interface.current_datagram_index++;
+ ncm_interface.num_datagrams--;
+
+ tud_network_recv_cb(receive_ntb + ndp->datagram[i].wDatagramIndex, ndp->datagram[i].wDatagramLength);
+}
+
+//--------------------------------------------------------------------+
+// USBD Driver API
+//--------------------------------------------------------------------+
+
+void netd_init(void)
+{
+ tu_memclr(&ncm_interface, sizeof(ncm_interface));
+ ncm_interface.ntb_in_size = CFG_TUD_NCM_IN_NTB_MAX_SIZE;
+ ncm_interface.max_datagrams_per_ntb = CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB;
+ ncm_prepare_for_tx();
+}
+
+void netd_reset(uint8_t rhport)
+{
+ (void) rhport;
+
+ netd_init();
+}
+
+uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
+{
+ // confirm interface hasn't already been allocated
+ TU_ASSERT(0 == ncm_interface.ep_notif, 0);
+
+ //------------- Management Interface -------------//
+ ncm_interface.itf_num = itf_desc->bInterfaceNumber;
+
+ uint16_t drv_len = sizeof(tusb_desc_interface_t);
+ uint8_t const * p_desc = tu_desc_next( itf_desc );
+
+ // Communication Functional Descriptors
+ while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
+ {
+ drv_len += tu_desc_len(p_desc);
+ p_desc = tu_desc_next(p_desc);
+ }
+
+ // notification endpoint (if any)
+ if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
+ {
+ TU_ASSERT( usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0 );
+
+ ncm_interface.ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
+
+ drv_len += tu_desc_len(p_desc);
+ p_desc = tu_desc_next(p_desc);
+ }
+
+ //------------- Data Interface -------------//
+ // - CDC-NCM data interface has 2 alternate settings
+ // - 0 : zero endpoints for inactive (default)
+ // - 1 : IN & OUT endpoints for transfer of NTBs
+ TU_ASSERT(TUSB_DESC_INTERFACE == tu_desc_type(p_desc), 0);
+
+ do
+ {
+ tusb_desc_interface_t const * data_itf_desc = (tusb_desc_interface_t const *) p_desc;
+ TU_ASSERT(TUSB_CLASS_CDC_DATA == data_itf_desc->bInterfaceClass, 0);
+
+ drv_len += tu_desc_len(p_desc);
+ p_desc = tu_desc_next(p_desc);
+ } while((TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && (drv_len <= max_len));
+
+ // Pair of endpoints
+ TU_ASSERT(TUSB_DESC_ENDPOINT == tu_desc_type(p_desc), 0);
+
+ TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &ncm_interface.ep_out, &ncm_interface.ep_in) );
+
+ drv_len += 2*sizeof(tusb_desc_endpoint_t);
+
+ return drv_len;
+}
+
+static void ncm_report(void)
+{
+ if (ncm_interface.report_state == REPORT_SPEED) {
+ ncm_notify_speed_change.header.wIndex = ncm_interface.itf_num;
+ usbd_edpt_xfer(TUD_OPT_RHPORT, ncm_interface.ep_notif, (uint8_t *) &ncm_notify_speed_change, sizeof(ncm_notify_speed_change));
+ ncm_interface.report_state = REPORT_CONNECTED;
+ ncm_interface.report_pending = true;
+ } else if (ncm_interface.report_state == REPORT_CONNECTED) {
+ ncm_notify_connected.header.wIndex = ncm_interface.itf_num;
+ usbd_edpt_xfer(TUD_OPT_RHPORT, ncm_interface.ep_notif, (uint8_t *) &ncm_notify_connected, sizeof(ncm_notify_connected));
+ ncm_interface.report_state = REPORT_DONE;
+ ncm_interface.report_pending = true;
+ }
+}
+
+TU_ATTR_WEAK void tud_network_link_state_cb(bool state)
+{
+ (void)state;
+}
+
+// Handle class control request
+// return false to stall control endpoint (e.g unsupported request)
+bool netd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
+{
+ if ( stage != CONTROL_STAGE_SETUP ) return true;
+
+ switch ( request->bmRequestType_bit.type )
+ {
+ case TUSB_REQ_TYPE_STANDARD:
+ switch ( request->bRequest )
+ {
+ case TUSB_REQ_GET_INTERFACE:
+ {
+ uint8_t const req_itfnum = (uint8_t) request->wIndex;
+ TU_VERIFY(ncm_interface.itf_num + 1 == req_itfnum);
+
+ tud_control_xfer(rhport, request, &ncm_interface.itf_data_alt, 1);
+ }
+ break;
+
+ case TUSB_REQ_SET_INTERFACE:
+ {
+ uint8_t const req_itfnum = (uint8_t) request->wIndex;
+ uint8_t const req_alt = (uint8_t) request->wValue;
+
+ // Only valid for Data Interface with Alternate is either 0 or 1
+ TU_VERIFY(ncm_interface.itf_num + 1 == req_itfnum && req_alt < 2);
+
+ if (req_alt != ncm_interface.itf_data_alt) {
+ ncm_interface.itf_data_alt = req_alt;
+
+ if (ncm_interface.itf_data_alt) {
+ if (!usbd_edpt_busy(rhport, ncm_interface.ep_out)) {
+ tud_network_recv_renew(); // prepare for incoming datagrams
+ }
+ if (!ncm_interface.report_pending) {
+ ncm_report();
+ }
+ }
+
+ tud_network_link_state_cb(ncm_interface.itf_data_alt);
+ }
+
+ tud_control_status(rhport, request);
+ }
+ break;
+
+ // unsupported request
+ default: return false;
+ }
+ break;
+
+ case TUSB_REQ_TYPE_CLASS:
+ TU_VERIFY (ncm_interface.itf_num == request->wIndex);
+
+ if (NCM_GET_NTB_PARAMETERS == request->bRequest)
+ {
+ tud_control_xfer(rhport, request, (void*)&ntb_parameters, sizeof(ntb_parameters));
+ }
+
+ break;
+
+ // unsupported request
+ default: return false;
+ }
+
+ return true;
+}
+
+static void handle_incoming_datagram(uint32_t len)
+{
+ uint32_t size = len;
+
+ if (len == 0) {
+ return;
+ }
+
+ TU_ASSERT(size >= sizeof(nth16_t), );
+
+ const nth16_t *hdr = (const nth16_t *)receive_ntb;
+ TU_ASSERT(hdr->dwSignature == NTH16_SIGNATURE, );
+ TU_ASSERT(hdr->wNdpIndex >= sizeof(nth16_t) && (hdr->wNdpIndex + sizeof(ndp16_t)) <= len, );
+
+ const ndp16_t *ndp = (const ndp16_t *)(receive_ntb + hdr->wNdpIndex);
+ TU_ASSERT(ndp->dwSignature == NDP16_SIGNATURE_NCM0 || ndp->dwSignature == NDP16_SIGNATURE_NCM1, );
+ TU_ASSERT(hdr->wNdpIndex + ndp->wLength <= len, );
+
+ int num_datagrams = (ndp->wLength - 12) / 4;
+ ncm_interface.current_datagram_index = 0;
+ ncm_interface.num_datagrams = 0;
+ ncm_interface.ndp = ndp;
+ for (int i = 0; i < num_datagrams && ndp->datagram[i].wDatagramIndex && ndp->datagram[i].wDatagramLength; i++)
+ {
+ ncm_interface.num_datagrams++;
+ }
+
+ tud_network_recv_renew();
+}
+
+bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
+{
+ (void) rhport;
+ (void) result;
+
+ /* new datagram receive_ntb */
+ if (ep_addr == ncm_interface.ep_out )
+ {
+ handle_incoming_datagram(xferred_bytes);
+ }
+
+ /* data transmission finished */
+ if (ep_addr == ncm_interface.ep_in )
+ {
+ if (ncm_interface.transferring) {
+ ncm_interface.transferring = false;
+ }
+
+ // If there are datagrams queued up that we tried to send while this NTB was being emitted, send them now
+ if (ncm_interface.datagram_count && ncm_interface.itf_data_alt == 1) {
+ ncm_start_tx();
+ }
+ }
+
+ if (ep_addr == ncm_interface.ep_notif )
+ {
+ ncm_interface.report_pending = false;
+ ncm_report();
+ }
+
+ return true;
+}
+
+// poll network driver for its ability to accept another packet to transmit
+bool tud_network_can_xmit(uint16_t size)
+{
+ TU_VERIFY(ncm_interface.itf_data_alt == 1);
+
+ if (ncm_interface.datagram_count >= ncm_interface.max_datagrams_per_ntb) {
+ TU_LOG2("NTB full [by count]\r\n");
+ return false;
+ }
+
+ size_t next_datagram_offset = ncm_interface.next_datagram_offset;
+ if (next_datagram_offset + size > ncm_interface.ntb_in_size) {
+ TU_LOG2("ntb full [by size]\r\n");
+ return false;
+ }
+
+ return true;
+}
+
+void tud_network_xmit(void *ref, uint16_t arg)
+{
+ transmit_ntb_t *ntb = &transmit_ntb[ncm_interface.current_ntb];
+ size_t next_datagram_offset = ncm_interface.next_datagram_offset;
+
+ uint16_t size = tud_network_xmit_cb(ntb->data + next_datagram_offset, ref, arg);
+
+ ntb->ndp.datagram[ncm_interface.datagram_count].wDatagramIndex = ncm_interface.next_datagram_offset;
+ ntb->ndp.datagram[ncm_interface.datagram_count].wDatagramLength = size;
+
+ ncm_interface.datagram_count++;
+ next_datagram_offset += size;
+
+ // round up so the next datagram is aligned correctly
+ next_datagram_offset += (CFG_TUD_NCM_ALIGNMENT - 1);
+ next_datagram_offset -= (next_datagram_offset % CFG_TUD_NCM_ALIGNMENT);
+
+ ncm_interface.next_datagram_offset = next_datagram_offset;
+
+ ncm_start_tx();
+}
+
+#endif
diff --git a/src/class/net/net_device.h b/src/class/net/net_device.h
index f030f3077..6e294465b 100644
--- a/src/class/net/net_device.h
+++ b/src/class/net/net_device.h
@@ -30,14 +30,36 @@
#include "class/cdc/cdc.h"
+#if CFG_TUD_ECM_RNDIS && CFG_TUD_NCM
+#error "Cannot enable both ECM_RNDIS and NCM network drivers"
+#endif
+
+#include "ncm.h"
+
/* declared here, NOT in usb_descriptors.c, so that the driver can intelligently ZLP as needed */
#define CFG_TUD_NET_ENDPOINT_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
-/* Maximum Tranmission Unit (in bytes) of the network, including Ethernet header */
+/* Maximum Transmission Unit (in bytes) of the network, including Ethernet header */
#ifndef CFG_TUD_NET_MTU
#define CFG_TUD_NET_MTU 1514
#endif
+#ifndef CFG_TUD_NCM_IN_NTB_MAX_SIZE
+#define CFG_TUD_NCM_IN_NTB_MAX_SIZE 3200
+#endif
+
+#ifndef CFG_TUD_NCM_OUT_NTB_MAX_SIZE
+#define CFG_TUD_NCM_OUT_NTB_MAX_SIZE 3200
+#endif
+
+#ifndef CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB
+#define CFG_TUD_NCM_MAX_DATAGRAMS_PER_NTB 8
+#endif
+
+#ifndef CFG_TUD_NCM_ALIGNMENT
+#define CFG_TUD_NCM_ALIGNMENT 4
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -46,8 +68,18 @@
// Application API
//--------------------------------------------------------------------+
-// client must provide this: initialize any network state back to the beginning
-void tud_network_init_cb(void);
+// indicate to network driver that client has finished with the packet provided to network_recv_cb()
+void tud_network_recv_renew(void);
+
+// poll network driver for its ability to accept another packet to transmit
+bool tud_network_can_xmit(uint16_t size);
+
+// if network_can_xmit() returns true, network_xmit() can be called once
+void tud_network_xmit(void *ref, uint16_t arg);
+
+//--------------------------------------------------------------------+
+// Application Callbacks (WEAK is optional)
+//--------------------------------------------------------------------+
// client must provide this: return false if the packet buffer was not accepted
bool tud_network_recv_cb(const uint8_t *src, uint16_t size);
@@ -55,18 +87,19 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t size);
// client must provide this: copy from network stack packet pointer to dst
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg);
+//------------- ECM/RNDIS -------------//
+
+// client must provide this: initialize any network state back to the beginning
+void tud_network_init_cb(void);
+
// client must provide this: 48-bit MAC address
// TODO removed later since it is not part of tinyusb stack
extern const uint8_t tud_network_mac_address[6];
-// indicate to network driver that client has finished with the packet provided to network_recv_cb()
-void tud_network_recv_renew(void);
-
-// poll network driver for its ability to accept another packet to transmit
-bool tud_network_can_xmit(void);
+//------------- NCM -------------//
-// if network_can_xmit() returns true, network_xmit() can be called once
-void tud_network_xmit(void *ref, uint16_t arg);
+// callback to client providing optional indication of internal state of network driver
+void tud_network_link_state_cb(bool state);
//--------------------------------------------------------------------+
// INTERNAL USBD-CLASS DRIVER API
diff --git a/src/class/usbtmc/usbtmc_device.c b/src/class/usbtmc/usbtmc_device.c
index c1ee49f45..4bd1edf12 100644
--- a/src/class/usbtmc/usbtmc_device.c
+++ b/src/class/usbtmc/usbtmc_device.c
@@ -1,11 +1,4 @@
/*
- * usbtmc.c
- *
- * Created on: Sep 9, 2019
- * Author: nconrad
- */
-
-/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Nathan Conrad
@@ -205,7 +198,7 @@ bool tud_usbtmc_transmit_dev_msg_data(
{
TU_ASSERT(usbtmc_state.capabilities->bmDevCapabilities.canEndBulkInOnTermChar);
TU_ASSERT(termCharRequested);
- TU_ASSERT(((uint8_t*)data)[len-1u] == termChar);
+ TU_ASSERT(((uint8_t const*)data)[len-1u] == termChar);
}
#endif
@@ -228,7 +221,7 @@ bool tud_usbtmc_transmit_dev_msg_data(
memcpy((uint8_t*)(usbtmc_state.ep_bulk_in_buf) + headerLen, data, dataLen);
usbtmc_state.transfer_size_remaining = len - dataLen;
usbtmc_state.transfer_size_sent = dataLen;
- usbtmc_state.devInBuffer = (uint8_t*)data + (dataLen);
+ usbtmc_state.devInBuffer = (uint8_t const*) data + (dataLen);
bool stateChanged =
atomicChangeState(STATE_TX_REQUESTED, (packetLen >= txBufLen) ? STATE_TX_INITIATED : STATE_TX_SHORTED);
@@ -287,7 +280,7 @@ uint16_t usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
tusb_desc_endpoint_t const *ep_desc = (tusb_desc_endpoint_t const *)p_desc;
switch(ep_desc->bmAttributes.xfer) {
case TUSB_XFER_BULK:
- TU_ASSERT(ep_desc->wMaxPacketSize.size == USBTMCD_MAX_PACKET_SIZE, 0);
+ TU_ASSERT(tu_edpt_packet_size(ep_desc) == USBTMCD_MAX_PACKET_SIZE, 0);
if (tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN)
{
usbtmc_state.ep_bulk_in = ep_desc->bEndpointAddress;
@@ -508,13 +501,13 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
case STATE_ABORTING_BULK_OUT:
TU_VERIFY(false);
return false; // Should be stalled by now, shouldn't have received a packet.
+
case STATE_TX_REQUESTED:
case STATE_TX_INITIATED:
case STATE_ABORTING_BULK_IN:
case STATE_ABORTING_BULK_IN_SHORTED:
case STATE_ABORTING_BULK_IN_ABORTED:
default:
-
TU_VERIFY(false);
}
}
@@ -528,37 +521,40 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
case STATE_TX_INITIATED:
if(usbtmc_state.transfer_size_remaining >=sizeof(usbtmc_state.ep_bulk_in_buf))
- {
+ {
// FIXME! This removes const below!
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in,
- (void*)usbtmc_state.devInBuffer,sizeof(usbtmc_state.ep_bulk_in_buf)));
+ (void*)(uintptr_t) usbtmc_state.devInBuffer, sizeof(usbtmc_state.ep_bulk_in_buf)));
usbtmc_state.devInBuffer += sizeof(usbtmc_state.ep_bulk_in_buf);
usbtmc_state.transfer_size_remaining -= sizeof(usbtmc_state.ep_bulk_in_buf);
usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.ep_bulk_in_buf);
- }
- else // last packet
- {
- size_t packetLen = usbtmc_state.transfer_size_remaining;
- memcpy(usbtmc_state.ep_bulk_in_buf, usbtmc_state.devInBuffer, usbtmc_state.transfer_size_remaining);
- usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.transfer_size_remaining);
- usbtmc_state.transfer_size_remaining = 0;
- usbtmc_state.devInBuffer = NULL;
- TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf,(uint16_t)packetLen));
+ }
+ else // last packet
+ {
+ size_t packetLen = usbtmc_state.transfer_size_remaining;
+ memcpy(usbtmc_state.ep_bulk_in_buf, usbtmc_state.devInBuffer, usbtmc_state.transfer_size_remaining);
+ usbtmc_state.transfer_size_sent += sizeof(usbtmc_state.transfer_size_remaining);
+ usbtmc_state.transfer_size_remaining = 0;
+ usbtmc_state.devInBuffer = NULL;
+ TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf, (uint16_t)packetLen) );
if(((packetLen % USBTMCD_MAX_PACKET_SIZE) != 0) || (packetLen == 0 ))
{
usbtmc_state.state = STATE_TX_SHORTED;
}
}
return true;
+
case STATE_ABORTING_BULK_IN:
// need to send short packet (ZLP?)
TU_VERIFY( usbd_edpt_xfer(rhport, usbtmc_state.ep_bulk_in, usbtmc_state.ep_bulk_in_buf,(uint16_t)0u));
usbtmc_state.state = STATE_ABORTING_BULK_IN_SHORTED;
return true;
+
case STATE_ABORTING_BULK_IN_SHORTED:
/* Done. :)*/
usbtmc_state.state = STATE_ABORTING_BULK_IN_ABORTED;
- return true;
+ return true;
+
default:
TU_ASSERT(false);
return false;
@@ -787,7 +783,7 @@ bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
{
TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
TU_VERIFY(request->wLength == sizeof(*(usbtmc_state.capabilities)));
- TU_VERIFY(tud_control_xfer(rhport, request, (void*)usbtmc_state.capabilities, sizeof(*usbtmc_state.capabilities)));
+ TU_VERIFY(tud_control_xfer(rhport, request, (void*)(uintptr_t) usbtmc_state.capabilities, sizeof(*usbtmc_state.capabilities)));
return true;
}
// USBTMC Optional Requests
diff --git a/src/class/usbtmc/usbtmc_device.h b/src/class/usbtmc/usbtmc_device.h
index 622800315..0549a1569 100644
--- a/src/class/usbtmc/usbtmc_device.h
+++ b/src/class/usbtmc/usbtmc_device.h
@@ -1,10 +1,4 @@
/*
- * usbtmc_device.h
- *
- * Created on: Sep 10, 2019
- * Author: nconrad
- */
-/*
* The MIT License (MIT)
*
* Copyright (c) 2019 N Conrad
diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c
index 8c59b4ea7..8a4ca1d2e 100644
--- a/src/class/vendor/vendor_device.c
+++ b/src/class/vendor/vendor_device.c
@@ -175,12 +175,12 @@ void vendord_reset(uint8_t rhport)
}
}
-uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
+uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len)
{
- TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == itf_desc->bInterfaceClass, 0);
+ TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == desc_itf->bInterfaceClass, 0);
- uint16_t const drv_len = sizeof(tusb_desc_interface_t) + itf_desc->bNumEndpoints*sizeof(tusb_desc_endpoint_t);
- TU_VERIFY(max_len >= drv_len, 0);
+ uint8_t const * p_desc = tu_desc_next(desc_itf);
+ uint8_t const * desc_end = p_desc + max_len;
// Find available interface
vendord_interface_t* p_vendor = NULL;
@@ -194,21 +194,30 @@ uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, ui
}
TU_VERIFY(p_vendor, 0);
- // Open endpoint pair with usbd helper
- TU_ASSERT(usbd_open_edpt_pair(rhport, tu_desc_next(itf_desc), 2, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in), 0);
+ p_vendor->itf_num = desc_itf->bInterfaceNumber;
+ if (desc_itf->bNumEndpoints)
+ {
+ // skip non-endpoint descriptors
+ while ( (TUSB_DESC_ENDPOINT != tu_desc_type(p_desc)) && (p_desc < desc_end) )
+ {
+ p_desc = tu_desc_next(p_desc);
+ }
- p_vendor->itf_num = itf_desc->bInterfaceNumber;
+ // Open endpoint pair with usbd helper
+ TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in), 0);
- // Prepare for incoming data
- if ( !usbd_edpt_xfer(rhport, p_vendor->ep_out, p_vendor->epout_buf, sizeof(p_vendor->epout_buf)) )
- {
- TU_LOG_FAILED();
- TU_BREAKPOINT();
- }
+ p_desc += desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t);
- maybe_transmit(p_vendor);
+ // Prepare for incoming data
+ if ( p_vendor->ep_out )
+ {
+ TU_ASSERT(usbd_edpt_xfer(rhport, p_vendor->ep_out, p_vendor->epout_buf, sizeof(p_vendor->epout_buf)), 0);
+ }
+
+ if ( p_vendor->ep_in ) maybe_transmit(p_vendor);
+ }
- return drv_len;
+ return (uintptr_t) p_desc - (uintptr_t) desc_itf;
}
bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
diff --git a/src/class/vendor/vendor_device.h b/src/class/vendor/vendor_device.h
index 844693c68..d71c2a3e9 100644
--- a/src/class/vendor/vendor_device.h
+++ b/src/class/vendor/vendor_device.h
@@ -44,7 +44,7 @@ bool tud_vendor_n_mounted (uint8_t itf);
uint32_t tud_vendor_n_available (uint8_t itf);
uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
-bool tud_vendor_n_peek (uint8_t itf, uint8_t* u8);
+bool tud_vendor_n_peek (uint8_t itf, uint8_t* ui8);
void tud_vendor_n_read_flush (uint8_t itf);
uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
@@ -59,7 +59,7 @@ uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str);
static inline bool tud_vendor_mounted (void);
static inline uint32_t tud_vendor_available (void);
static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize);
-static inline bool tud_vendor_peek (uint8_t* u8);
+static inline bool tud_vendor_peek (uint8_t* ui8);
static inline void tud_vendor_read_flush (void);
static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize);
static inline uint32_t tud_vendor_write_str (char const* str);
@@ -96,9 +96,9 @@ static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize)
return tud_vendor_n_read(0, buffer, bufsize);
}
-static inline bool tud_vendor_peek (uint8_t* u8)
+static inline bool tud_vendor_peek (uint8_t* ui8)
{
- return tud_vendor_n_peek(0, u8);
+ return tud_vendor_n_peek(0, ui8);
}
static inline void tud_vendor_read_flush(void)
diff --git a/src/class/video/video.h b/src/class/video/video.h
new file mode 100644
index 000000000..844746546
--- /dev/null
+++ b/src/class/video/video.h
@@ -0,0 +1,480 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2021 Koji KITAYAMA
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#ifndef TUSB_VIDEO_H_
+#define TUSB_VIDEO_H_
+
+#include "common/tusb_common.h"
+
+// Table 3-19 Color Matching Descriptor
+typedef enum {
+ VIDEO_COLOR_PRIMARIES_UNDEFINED = 0x00,
+ VIDEO_COLOR_PRIMARIES_BT709, // sRGB (default)
+ VIDEO_COLOR_PRIMARIES_BT470_2M,
+ VIDEO_COLOR_PRIMARIES_BT470_2BG,
+ VIDEO_COLOR_PRIMARIES_SMPTE170M,
+ VIDEO_COLOR_PRIMARIES_SMPTE240M,
+} video_color_primaries_t;
+
+// Table 3-19 Color Matching Descriptor
+typedef enum {
+ VIDEO_COLOR_XFER_CH_UNDEFINED = 0x00,
+ VIDEO_COLOR_XFER_CH_BT709, // default
+ VIDEO_COLOR_XFER_CH_BT470_2M,
+ VIDEO_COLOR_XFER_CH_BT470_2BG,
+ VIDEO_COLOR_XFER_CH_SMPTE170M,
+ VIDEO_COLOR_XFER_CH_SMPTE240M,
+ VIDEO_COLOR_XFER_CH_LINEAR,
+ VIDEO_COLOR_XFER_CH_SRGB,
+} video_color_transfer_characteristics_t;
+
+// Table 3-19 Color Matching Descriptor
+typedef enum {
+ VIDEO_COLOR_COEF_UNDEFINED = 0x00,
+ VIDEO_COLOR_COEF_BT709,
+ VIDEO_COLOR_COEF_FCC,
+ VIDEO_COLOR_COEF_BT470_2BG,
+ VIDEO_COLOR_COEF_SMPTE170M, // BT.601 default
+ VIDEO_COLOR_COEF_SMPTE240M,
+} video_color_matrix_coefficients_t;
+
+/* 4.2.1.2 Request Error Code Control */
+typedef enum {
+ VIDEO_ERROR_NONE = 0, /* The request succeeded. */
+ VIDEO_ERROR_NOT_READY,
+ VIDEO_ERROR_WRONG_STATE,
+ VIDEO_ERROR_POWER,
+ VIDEO_ERROR_OUT_OF_RANGE,
+ VIDEO_ERROR_INVALID_UNIT,
+ VIDEO_ERROR_INVALID_CONTROL,
+ VIDEO_ERROR_INVALID_REQUEST,
+ VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE,
+ VIDEO_ERROR_UNKNOWN = 0xFF,
+} video_error_code_t;
+
+/* A.2 Interface Subclass */
+typedef enum {
+ VIDEO_SUBCLASS_UNDEFINED = 0x00,
+ VIDEO_SUBCLASS_CONTROL,
+ VIDEO_SUBCLASS_STREAMING,
+ VIDEO_SUBCLASS_INTERFACE_COLLECTION,
+} video_subclass_type_t;
+
+/* A.3 Interface Protocol */
+typedef enum {
+ VIDEO_ITF_PROTOCOL_UNDEFINED = 0x00,
+ VIDEO_ITF_PROTOCOL_15,
+} video_interface_protocol_code_t;
+
+/* A.5 Class-Specific VideoControl Interface Descriptor Subtypes */
+typedef enum {
+ VIDEO_CS_ITF_VC_UNDEFINED = 0x00,
+ VIDEO_CS_ITF_VC_HEADER,
+ VIDEO_CS_ITF_VC_INPUT_TERMINAL,
+ VIDEO_CS_ITF_VC_OUTPUT_TERMINAL,
+ VIDEO_CS_ITF_VC_SELECTOR_UNIT,
+ VIDEO_CS_ITF_VC_PROCESSING_UNIT,
+ VIDEO_CS_ITF_VC_EXTENSION_UNIT,
+ VIDEO_CS_ITF_VC_ENCODING_UNIT,
+ VIDEO_CS_ITF_VC_MAX,
+} video_cs_vc_interface_subtype_t;
+
+/* A.6 Class-Specific VideoStreaming Interface Descriptor Subtypes */
+typedef enum {
+ VIDEO_CS_ITF_VS_UNDEFINED = 0x00,
+ VIDEO_CS_ITF_VS_INPUT_HEADER = 0x01,
+ VIDEO_CS_ITF_VS_OUTPUT_HEADER = 0x02,
+ VIDEO_CS_ITF_VS_STILL_IMAGE_FRAME = 0x03,
+ VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED = 0x04,
+ VIDEO_CS_ITF_VS_FRAME_UNCOMPRESSED = 0x05,
+ VIDEO_CS_ITF_VS_FORMAT_MJPEG = 0x06,
+ VIDEO_CS_ITF_VS_FRAME_MJPEG = 0x07,
+ VIDEO_CS_ITF_VS_FORMAT_MPEG2TS = 0x0A,
+ VIDEO_CS_ITF_VS_FORMAT_DV = 0x0C,
+ VIDEO_CS_ITF_VS_COLORFORMAT = 0x0D,
+ VIDEO_CS_ITF_VS_FORMAT_FRAME_BASED = 0x10,
+ VIDEO_CS_ITF_VS_FRAME_FRAME_BASED = 0x11,
+ VIDEO_CS_ITF_VS_FORMAT_STREAM_BASED = 0x12,
+ VIDEO_CS_ITF_VS_FORMAT_H264 = 0x13,
+ VIDEO_CS_ITF_VS_FRAME_H264 = 0x14,
+ VIDEO_CS_ITF_VS_FORMAT_H264_SIMULCAST = 0x15,
+ VIDEO_CS_ITF_VS_FORMAT_VP8 = 0x16,
+ VIDEO_CS_ITF_VS_FRAME_VP8 = 0x17,
+ VIDEO_CS_ITF_VS_FORMAT_VP8_SIMULCAST = 0x18,
+} video_cs_vs_interface_subtype_t;
+
+/* A.7. Class-Specific Endpoint Descriptor Subtypes */
+typedef enum {
+ VIDEO_CS_EP_UNDEFINED = 0x00,
+ VIDEO_CS_EP_GENERAL,
+ VIDEO_CS_EP_ENDPOINT,
+ VIDEO_CS_EP_INTERRUPT
+} video_cs_ep_subtype_t;
+
+/* A.8 Class-Specific Request Codes */
+typedef enum {
+ VIDEO_REQUEST_UNDEFINED = 0x00,
+ VIDEO_REQUEST_SET_CUR = 0x01,
+ VIDEO_REQUEST_SET_CUR_ALL = 0x11,
+ VIDEO_REQUEST_GET_CUR = 0x81,
+ VIDEO_REQUEST_GET_MIN = 0x82,
+ VIDEO_REQUEST_GET_MAX = 0x83,
+ VIDEO_REQUEST_GET_RES = 0x84,
+ VIDEO_REQUEST_GET_LEN = 0x85,
+ VIDEO_REQUEST_GET_INFO = 0x86,
+ VIDEO_REQUEST_GET_DEF = 0x87,
+ VIDEO_REQUEST_GET_CUR_ALL = 0x91,
+ VIDEO_REQUEST_GET_MIN_ALL = 0x92,
+ VIDEO_REQUEST_GET_MAX_ALL = 0x93,
+ VIDEO_REQUEST_GET_RES_ALL = 0x94,
+ VIDEO_REQUEST_GET_DEF_ALL = 0x97
+} video_control_request_t;
+
+/* A.9.1 VideoControl Interface Control Selectors */
+typedef enum {
+ VIDEO_VC_CTL_UNDEFINED = 0x00,
+ VIDEO_VC_CTL_VIDEO_POWER_MODE,
+ VIDEO_VC_CTL_REQUEST_ERROR_CODE,
+} video_interface_control_selector_t;
+
+/* A.9.8 VideoStreaming Interface Control Selectors */
+typedef enum {
+ VIDEO_VS_CTL_UNDEFINED = 0x00,
+ VIDEO_VS_CTL_PROBE,
+ VIDEO_VS_CTL_COMMIT,
+ VIDEO_VS_CTL_STILL_PROBE,
+ VIDEO_VS_CTL_STILL_COMMIT,
+ VIDEO_VS_CTL_STILL_IMAGE_TRIGGER,
+ VIDEO_VS_CTL_STREAM_ERROR_CODE,
+ VIDEO_VS_CTL_GENERATE_KEY_FRAME,
+ VIDEO_VS_CTL_UPDATE_FRAME_SEGMENT,
+ VIDEO_VS_CTL_SYNCH_DELAY_CONTROL,
+} video_interface_streaming_selector_t;
+
+/* B. Terminal Types */
+typedef enum {
+ // Terminal
+ VIDEO_TT_VENDOR_SPECIFIC = 0x0100,
+ VIDEO_TT_STREAMING = 0x0101,
+
+ // Input
+ VIDEO_ITT_VENDOR_SPECIFIC = 0x0200,
+ VIDEO_ITT_CAMERA = 0x0201,
+ VIDEO_ITT_MEDIA_TRANSPORT_INPUT = 0x0202,
+
+ // Output
+ VIDEO_OTT_VENDOR_SPECIFIC = 0x0300,
+ VIDEO_OTT_DISPLAY = 0x0301,
+ VIDEO_OTT_MEDIA_TRANSPORT_OUTPUT = 0x0302,
+
+ // External
+ VIDEO_ETT_VENDOR_SPEIFIC = 0x0400,
+ VIDEO_ETT_COMPOSITE_CONNECTOR = 0x0401,
+ VIDEO_ETT_SVIDEO_CONNECTOR = 0x0402,
+ VIDEO_ETT_COMPONENT_CONNECTOR = 0x0403,
+} video_terminal_type_t;
+
+//--------------------------------------------------------------------+
+// Descriptors
+//--------------------------------------------------------------------+
+
+/* 2.3.4.2 */
+typedef struct TU_ATTR_PACKED {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubType;
+ uint16_t bcdUVC;
+ uint16_t wTotalLength;
+ uint32_t dwClockFrequency;
+ uint8_t bInCollection;
+ uint8_t baInterfaceNr[];
+} tusb_desc_cs_video_ctl_itf_hdr_t;
+
+/* 2.4.3.3 */
+typedef struct TU_ATTR_PACKED {
+ uint8_t bHeaderLength;
+ union {
+ uint8_t bmHeaderInfo;
+ struct {
+ uint8_t FrameID: 1;
+ uint8_t EndOfFrame: 1;
+ uint8_t PresentationTime: 1;
+ uint8_t SourceClockReference: 1;
+ uint8_t PayloadSpecific: 1;
+ uint8_t StillImage: 1;
+ uint8_t Error: 1;
+ uint8_t EndOfHeader: 1;
+ };
+ };
+} tusb_video_payload_header_t;
+
+/* 3.9.2.1 */
+typedef struct TU_ATTR_PACKED {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubType;
+ uint8_t bNumFormats;
+ uint16_t wTotalLength;
+ uint8_t bEndpointAddress;
+ uint8_t bmInfo;
+ uint8_t bTerminalLink;
+ uint8_t bStillCaptureMethod;
+ uint8_t bTriggerSupport;
+ uint8_t bTriggerUsage;
+ uint8_t bControlSize;
+ uint8_t bmaControls[];
+} tusb_desc_cs_video_stm_itf_in_hdr_t;
+
+/* 3.9.2.2 */
+typedef struct TU_ATTR_PACKED {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubType;
+ uint8_t bNumFormats;
+ uint16_t wTotalLength;
+ uint8_t bEndpointAddress;
+ uint8_t bTerminalLink;
+ uint8_t bControlSize;
+ uint8_t bmaControls[];
+} tusb_desc_cs_video_stm_itf_out_hdr_t;
+
+typedef struct TU_ATTR_PACKED {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubType;
+ uint8_t bNumFormats;
+ uint16_t wTotalLength;
+ uint8_t bEndpointAddress;
+ union {
+ struct {
+ uint8_t bmInfo;
+ uint8_t bTerminalLink;
+ uint8_t bStillCaptureMethod;
+ uint8_t bTriggerSupport;
+ uint8_t bTriggerUsage;
+ uint8_t bControlSize;
+ uint8_t bmaControls[];
+ } input;
+ struct {
+ uint8_t bEndpointAddress;
+ uint8_t bTerminalLink;
+ uint8_t bControlSize;
+ uint8_t bmaControls[];
+ } output;
+ };
+} tusb_desc_cs_video_stm_itf_hdr_t;
+
+typedef struct TU_ATTR_PACKED {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubType;
+ uint8_t bFormatIndex;
+ uint8_t bNumFrameDescriptors;
+ uint8_t guidFormat[16];
+ uint8_t bBitsPerPixel;
+ uint8_t bDefaultFrameIndex;
+ uint8_t bAspectRatioX;
+ uint8_t bAspectRatioY;
+ uint8_t bmInterlaceFlags;
+ uint8_t bCopyProtect;
+} tusb_desc_cs_video_fmt_uncompressed_t;
+
+typedef struct TU_ATTR_PACKED {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubType;
+ uint8_t bFrameIndex;
+ uint8_t bmCapabilities;
+ uint16_t wWidth;
+ uint16_t wHeight;
+ uint32_t dwMinBitRate;
+ uint32_t dwMaxBitRate;
+ uint32_t dwMaxVideoFrameBufferSize; /* deprecated */
+ uint32_t dwDefaultFrameInterval;
+ uint8_t bFrameIntervalType;
+ uint32_t dwFrameInterval[];
+} tusb_desc_cs_video_frm_uncompressed_t;
+
+//--------------------------------------------------------------------+
+// Requests
+//--------------------------------------------------------------------+
+
+/* 4.3.1.1 */
+typedef struct TU_ATTR_PACKED {
+ union {
+ uint8_t bmHint;
+ struct TU_ATTR_PACKED {
+ uint16_t dwFrameInterval: 1;
+ uint16_t wKeyFrameRatel : 1;
+ uint16_t wPFrameRate : 1;
+ uint16_t wCompQuality : 1;
+ uint16_t wCompWindowSize: 1;
+ uint16_t : 0;
+ } Hint;
+ };
+ uint8_t bFormatIndex;
+ uint8_t bFrameIndex;
+ uint32_t dwFrameInterval;
+ uint16_t wKeyFrameRate;
+ uint16_t wPFrameRate;
+ uint16_t wCompQuality;
+ uint16_t wCompWindowSize;
+ uint16_t wDelay;
+ uint32_t dwMaxVideoFrameSize;
+ uint32_t dwMaxPayloadTransferSize;
+ uint32_t dwClockFrequency;
+ union {
+ uint8_t bmFramingInfo;
+ struct TU_ATTR_PACKED {
+ uint8_t FrameID : 1;
+ uint8_t EndOfFrame: 1;
+ uint8_t EndOfSlice: 1;
+ uint8_t : 0;
+ } FramingInfo;
+ };
+ uint8_t bPreferedVersion;
+ uint8_t bMinVersion;
+ uint8_t bMaxVersion;
+ uint8_t bUsage;
+ uint8_t bBitDepthLuma;
+ uint8_t bmSettings;
+ uint8_t bMaxNumberOfRefFramesPlus1;
+ uint16_t bmRateControlModes;
+ uint64_t bmLayoutPerStream;
+} video_probe_and_commit_control_t;
+
+TU_VERIFY_STATIC( sizeof(video_probe_and_commit_control_t) == 48, "size is not correct");
+
+#define TUD_VIDEO_DESC_IAD_LEN 8
+#define TUD_VIDEO_DESC_STD_VC_LEN 9
+#define TUD_VIDEO_DESC_CS_VC_LEN 12
+#define TUD_VIDEO_DESC_INPUT_TERM_LEN 8
+#define TUD_VIDEO_DESC_OUTPUT_TERM_LEN 9
+#define TUD_VIDEO_DESC_CAMERA_TERM_LEN 18
+#define TUD_VIDEO_DESC_STD_VS_LEN 9
+#define TUD_VIDEO_DESC_CS_VS_IN_LEN 13
+#define TUD_VIDEO_DESC_CS_VS_OUT_LEN 9
+#define TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN 27
+#define TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN 38
+#define TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_DISC_LEN 26
+#define TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN 6
+
+/* 2.2 compression formats */
+#define TUD_VIDEO_GUID_YUY2 0x59,0x55,0x59,0x32,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71
+#define TUD_VIDEO_GUID_NV12 0x4E,0x56,0x31,0x32,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71
+#define TUD_VIDEO_GUID_M420 0x4D,0x34,0x32,0x30,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71
+#define TUD_VIDEO_GUID_I420 0x49,0x34,0x32,0x30,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71
+
+#define TUD_VIDEO_DESC_IAD(_firstitfs, _nitfs, _stridx) \
+ TUD_VIDEO_DESC_IAD_LEN, TUSB_DESC_INTERFACE_ASSOCIATION, \
+ _firstitfs, _nitfs, TUSB_CLASS_VIDEO, VIDEO_SUBCLASS_INTERFACE_COLLECTION, \
+ VIDEO_ITF_PROTOCOL_UNDEFINED, _stridx
+
+#define TUD_VIDEO_DESC_STD_VC(_itfnum, _nEPs, _stridx) \
+ TUD_VIDEO_DESC_STD_VC_LEN, TUSB_DESC_INTERFACE, _itfnum, /* fixed to zero */ 0x00, \
+ _nEPs, TUSB_CLASS_VIDEO, VIDEO_SUBCLASS_CONTROL, VIDEO_ITF_PROTOCOL_15, _stridx
+
+/* 3.7.2 */
+#define TUD_VIDEO_DESC_CS_VC(_bcdUVC, _totallen, _clkfreq, ...) \
+ TUD_VIDEO_DESC_CS_VC_LEN + (TU_ARGS_NUM(__VA_ARGS__)), TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VC_HEADER, \
+ U16_TO_U8S_LE(_bcdUVC), U16_TO_U8S_LE((_totallen) + TUD_VIDEO_DESC_CS_VC_LEN + (TU_ARGS_NUM(__VA_ARGS__))), \
+ U32_TO_U8S_LE(_clkfreq), TU_ARGS_NUM(__VA_ARGS__), __VA_ARGS__
+
+/* 3.7.2.1 */
+#define TUD_VIDEO_DESC_INPUT_TERM(_tid, _tt, _at, _stridx) \
+ TUD_VIDEO_DESC_INPUT_TERM_LEN, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VC_INPUT_TERMINAL, \
+ _tid, U16_TO_U8S_LE(_tt), _at, _stridx
+
+/* 3.7.2.2 */
+#define TUD_VIDEO_DESC_OUTPUT_TERM(_tid, _tt, _at, _srcid, _stridx) \
+ TUD_VIDEO_DESC_OUTPUT_TERM_LEN, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VC_OUTPUT_TERMINAL, \
+ _tid, U16_TO_U8S_LE(_tt), _at, _srcid, _stridx
+
+/* 3.7.2.3 */
+#define TUD_VIDEO_DESC_CAMERA_TERM(_tid, _at, _stridx, _focal_min, _focal_max, _focal, _ctls) \
+ TUD_VIDEO_DESC_CAMERA_TERM_LEN, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VC_INPUT_TERMINAL, \
+ _tid, U16_TO_U8S_LE(VIDEO_ITT_CAMERA), _at, _stridx, \
+ U16_TO_U8S_LE(_focal_min), U16_TO_U8S_LE(_focal_max), U16_TO_U8S_LE(_focal), 3, \
+ TU_U32_BYTE0(_ctls), TU_U32_BYTE1(_ctls), TU_U32_BYTE2(_ctls)
+
+/* 3.9.1 */
+#define TUD_VIDEO_DESC_STD_VS(_itfnum, _alt, _epn, _stridx) \
+ TUD_VIDEO_DESC_STD_VS_LEN, TUSB_DESC_INTERFACE, _itfnum, _alt, \
+ _epn, TUSB_CLASS_VIDEO, VIDEO_SUBCLASS_STREAMING, VIDEO_ITF_PROTOCOL_15, _stridx
+
+/* 3.9.2.1 */
+#define TUD_VIDEO_DESC_CS_VS_INPUT(_numfmt, _totallen, _ep, _inf, _termlnk, _sticaptmeth, _trgspt, _trgusg, ...) \
+ TUD_VIDEO_DESC_CS_VS_IN_LEN + (_numfmt) * (TU_ARGS_NUM(__VA_ARGS__)), TUSB_DESC_CS_INTERFACE, \
+ VIDEO_CS_ITF_VS_INPUT_HEADER, _numfmt, \
+ U16_TO_U8S_LE((_totallen) + TUD_VIDEO_DESC_CS_VS_IN_LEN + (_numfmt) * (TU_ARGS_NUM(__VA_ARGS__))), \
+ _ep, _inf, _termlnk, _sticaptmeth, _trgspt, _trgusg, (TU_ARGS_NUM(__VA_ARGS__)), __VA_ARGS__
+
+/* 3.9.2.2 */
+#define TUD_VIDEO_DESC_CS_VS_OUTPUT(_numfmt, _totallen, _ep, _inf, _termlnk, ...) \
+ TUD_VIDEO_DESC_CS_VS_OUT_LEN + (_numfmt) * (TU_ARGS_NUM(__VA_ARGS__)), TUSB_DESC_CS_INTERFACE, \
+ VIDEO_CS_ITF_VS_OUTPUT_HEADER, _numfmt, \
+ U16_TO_U8S_LE((_totallen) + TUD_VIDEO_DESC_CS_VS_OUT_LEN + (_numfmt) * (TU_ARGS_NUM(__VA_ARGS__))), \
+ _ep, _inf, _termlnk, (TU_ARGS_NUM(__VA_ARGS__)), __VA_ARGS__
+
+/* Uncompressed 3.1.1 */
+#define TUD_VIDEO_GUID(_g0,_g1,_g2,_g3,_g4,_g5,_g6,_g7,_g8,_g9,_g10,_g11,_g12,_g13,_g14,_g15) _g0,_g1,_g2,_g3,_g4,_g5,_g6,_g7,_g8,_g9,_g10,_g11,_g12,_g13,_g14,_g15
+
+#define TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR(_fmtidx, _numfrmdesc, \
+ _guid, _bitsperpix, _frmidx, _asrx, _asry, _interlace, _cp) \
+ TUD_VIDEO_DESC_CS_VS_FMT_UNCOMPR_LEN, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED, \
+ _fmtidx, _numfrmdesc, TUD_VIDEO_GUID(_guid), \
+ _bitsperpix, _frmidx, _asrx, _asry, _interlace, _cp
+
+/* Uncompressed 3.1.2 Table 3-3 */
+#define TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT(_frmidx, _cap, _width, _height, _minbr, _maxbr, _maxfrmbufsz, _frminterval, _minfrminterval, _maxfrminterval, _frmintervalstep) \
+ TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_CONT_LEN, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_FRAME_UNCOMPRESSED, \
+ _frmidx, _cap, U16_TO_U8S_LE(_width), U16_TO_U8S_LE(_height), U32_TO_U8S_LE(_minbr), U32_TO_U8S_LE(_maxbr), \
+ U32_TO_U8S_LE(_maxfrmbufsz), U32_TO_U8S_LE(_frminterval), 0, \
+ U32_TO_U8S_LE(_minfrminterval), U32_TO_U8S_LE(_maxfrminterval), U32_TO_U8S_LE(_frmintervalstep)
+
+/* Uncompressed 3.1.2 Table 3-4 */
+#define TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_DISC(_frmidx, _cap, _width, _height, _minbr, _maxbr, _maxfrmbufsz, _frminterval, ...) \
+ TUD_VIDEO_DESC_CS_VS_FRM_UNCOMPR_DISC_LEN + (TU_ARGS_NUM(__VA_ARGS__)) * 4, \
+ TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_FRAME_UNCOMPRESSED, \
+ _frmidx, _cap, U16_TO_U8S_LE(_width), U16_TO_U8S_LE(_height), U32_TO_U8S_LE(_minbr), U32_TO_U8S_LE(_maxbr), \
+ U32_TO_U8S_LE(_maxfrmbufsz), U32_TO_U8S_LE(_frminterval), (TU_ARGS_NUM(__VA_ARGS__)), __VA_ARGS__
+
+/* 3.9.2.6 */
+#define TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING(_color, _trns, _mat) \
+ TUD_VIDEO_DESC_CS_VS_COLOR_MATCHING_LEN, \
+ TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_COLORFORMAT, \
+ _color, _trns, _mat
+
+/* 3.10.1.1 */
+#define TUD_VIDEO_DESC_EP_ISO(_ep, _epsize, _ep_interval) \
+ 7, TUSB_DESC_ENDPOINT, _ep, TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS,\
+ U16_TO_U8S_LE(_epsize), _ep_interval
+
+/* 3.10.1.2 */
+#define TUD_VIDEO_DESC_EP_BULK(_ep, _epsize, _ep_interval) \
+ 7, TUSB_DESC_ENDPOINT, _ep, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), _ep_interval
+
+#endif
diff --git a/src/class/video/video_device.c b/src/class/video/video_device.c
new file mode 100644
index 000000000..eeb068197
--- /dev/null
+++ b/src/class/video/video_device.c
@@ -0,0 +1,1149 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2021 Koji KITAYAMA
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#include "tusb_option.h"
+
+#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_VIDEO && CFG_TUD_VIDEO_STREAMING)
+
+#include "device/usbd.h"
+#include "device/usbd_pvt.h"
+
+#include "video_device.h"
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF
+//--------------------------------------------------------------------+
+typedef struct {
+ tusb_desc_interface_t std;
+ tusb_desc_cs_video_ctl_itf_hdr_t ctl;
+} tusb_desc_vc_itf_t;
+
+typedef struct {
+ tusb_desc_interface_t std;
+ tusb_desc_cs_video_stm_itf_hdr_t stm;
+} tusb_desc_vs_itf_t;
+
+typedef union {
+ tusb_desc_cs_video_ctl_itf_hdr_t ctl;
+ tusb_desc_cs_video_stm_itf_hdr_t stm;
+} tusb_desc_video_itf_hdr_t;
+
+typedef struct TU_ATTR_PACKED {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubtype;
+ uint8_t bEntityId;
+} tusb_desc_cs_video_entity_itf_t;
+
+/* video streaming interface */
+typedef struct TU_ATTR_PACKED {
+ uint8_t index_vc; /* index of bound video control interface */
+ uint8_t index_vs; /* index from the video control interface */
+ struct {
+ uint16_t beg; /* Offset of the begging of video streaming interface descriptor */
+ uint16_t end; /* Offset of the end of video streaming interface descriptor */
+ uint16_t cur; /* Offset of the current settings */
+ uint16_t ep[2]; /* Offset of endpoint descriptors. 0: streaming, 1: still capture */
+ } desc;
+ uint8_t *buffer; /* frame buffer. assume linear buffer. no support for stride access */
+ uint32_t bufsize; /* frame buffer size */
+ uint32_t offset; /* offset for the next payload transfer */
+ uint32_t max_payload_transfer_size;
+ uint8_t error_code;/* error code */
+ /*------------- From this point, data is not cleared by bus reset -------------*/
+ CFG_TUSB_MEM_ALIGN uint8_t ep_buf[CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE]; /* EP transfer buffer for streaming */
+} videod_streaming_interface_t;
+
+/* video control interface */
+typedef struct TU_ATTR_PACKED {
+ void const *beg; /* The head of the first video control interface descriptor */
+ uint16_t len; /* Byte length of the descriptors */
+ uint16_t cur; /* offset for current video control interface */
+ uint8_t stm[CFG_TUD_VIDEO_STREAMING]; /* Indices of streaming interface */
+ uint8_t error_code; /* error code */
+ uint8_t power_mode;
+
+ /*------------- From this point, data is not cleared by bus reset -------------*/
+ // CFG_TUSB_MEM_ALIGN uint8_t ctl_buf[64]; /* EP transfer buffer for interrupt transfer */
+
+} videod_interface_t;
+
+#define ITF_STM_MEM_RESET_SIZE offsetof(videod_streaming_interface_t, ep_buf)
+
+//--------------------------------------------------------------------+
+// INTERNAL OBJECT & FUNCTION DECLARATION
+//--------------------------------------------------------------------+
+CFG_TUSB_MEM_SECTION static videod_interface_t _videod_itf[CFG_TUD_VIDEO];
+CFG_TUSB_MEM_SECTION static videod_streaming_interface_t _videod_streaming_itf[CFG_TUD_VIDEO_STREAMING];
+
+static uint8_t const _cap_get = 0x1u; /* support for GET */
+static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
+
+/** Get interface number from the interface descriptor
+ *
+ * @param[in] desc interface descriptor
+ *
+ * @return bInterfaceNumber */
+static inline uint8_t _desc_itfnum(void const *desc)
+{
+ return ((uint8_t const*)desc)[2];
+}
+
+/** Get endpoint address from the endpoint descriptor
+ *
+ * @param[in] desc endpoint descriptor
+ *
+ * @return bEndpointAddress */
+static inline uint8_t _desc_ep_addr(void const *desc)
+{
+ return ((uint8_t const*)desc)[2];
+}
+
+/** Get instance of streaming interface
+ *
+ * @param[in] ctl_idx instance number of video control
+ * @param[in] stm_idx index number of streaming interface
+ *
+ * @return instance */
+static videod_streaming_interface_t* _get_instance_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
+{
+ videod_interface_t *ctl = &_videod_itf[ctl_idx];
+ if (!ctl->beg) return NULL;
+ videod_streaming_interface_t *stm = &_videod_streaming_itf[ctl->stm[stm_idx]];
+ if (!stm->desc.beg) return NULL;
+ return stm;
+}
+
+static tusb_desc_vc_itf_t const* _get_desc_vc(videod_interface_t const *self)
+{
+ return (tusb_desc_vc_itf_t const *)(self->beg + self->cur);
+}
+
+static tusb_desc_vs_itf_t const* _get_desc_vs(videod_streaming_interface_t const *self)
+{
+ if (!self->desc.cur) return NULL;
+ void const *desc = _videod_itf[self->index_vc].beg;
+ return (tusb_desc_vs_itf_t const*)(desc + self->desc.cur);
+}
+
+/** Find the first descriptor of a given type
+ *
+ * @param[in] beg The head of descriptor byte array.
+ * @param[in] end The tail of descriptor byte array.
+ * @param[in] desc_type The target descriptor type.
+ *
+ * @return The pointer for interface descriptor.
+ * @retval end did not found interface descriptor */
+static void const* _find_desc(void const *beg, void const *end, uint_fast8_t desc_type)
+{
+ void const *cur = beg;
+ while ((cur < end) && (desc_type != tu_desc_type(cur))) {
+ cur = tu_desc_next(cur);
+ }
+ return cur;
+}
+
+/** Find the first descriptor specified by the arguments
+ *
+ * @param[in] beg The head of descriptor byte array.
+ * @param[in] end The tail of descriptor byte array.
+ * @param[in] desc_type The target descriptor type
+ * @param[in] element_0 The target element following the desc_type
+ * @param[in] element_1 The target element following the element_0
+ *
+ * @return The pointer for interface descriptor.
+ * @retval end did not found interface descriptor */
+static void const* _find_desc_3(void const *beg, void const *end,
+ uint_fast8_t desc_type,
+ uint_fast8_t element_0,
+ uint_fast8_t element_1)
+{
+ for (void const *cur = beg; cur < end; cur = _find_desc(cur, end, desc_type)) {
+ uint8_t const *p = (uint8_t const *)cur;
+ if ((p[2] == element_0) && (p[3] == element_1)) {
+ return cur;
+ }
+ cur = tu_desc_next(cur);
+ }
+ return end;
+}
+
+/** Return the next interface descriptor which has another interface number.
+ *
+ * @param[in] beg The head of descriptor byte array.
+ * @param[in] end The tail of descriptor byte array.
+ *
+ * @return The pointer for interface descriptor.
+ * @retval end did not found interface descriptor */
+static void const* _next_desc_itf(void const *beg, void const *end)
+{
+ void const *cur = beg;
+ uint_fast8_t itfnum = ((tusb_desc_interface_t const*)cur)->bInterfaceNumber;
+ while ((cur < end) &&
+ (itfnum == ((tusb_desc_interface_t const*)cur)->bInterfaceNumber)) {
+ cur = _find_desc(tu_desc_next(cur), end, TUSB_DESC_INTERFACE);
+ }
+ return cur;
+}
+
+/** Find the first interface descriptor with the specified interface number and alternate setting number.
+ *
+ * @param[in] beg The head of descriptor byte array.
+ * @param[in] end The tail of descriptor byte array.
+ * @param[in] itfnum The target interface number.
+ * @param[in] altnum The target alternate setting number.
+ *
+ * @return The pointer for interface descriptor.
+ * @retval end did not found interface descriptor */
+static inline void const* _find_desc_itf(void const *beg, void const *end, uint_fast8_t itfnum, uint_fast8_t altnum)
+{
+ return _find_desc_3(beg, end, TUSB_DESC_INTERFACE, itfnum, altnum);
+}
+
+/** Find the first endpoint descriptor belonging to the current interface descriptor.
+ *
+ * The search range is from `beg` to `end` or the next interface descriptor.
+ *
+ * @param[in] beg The head of descriptor byte array.
+ * @param[in] end The tail of descriptor byte array.
+ *
+ * @return The pointer for endpoint descriptor.
+ * @retval end did not found endpoint descriptor */
+static void const* _find_desc_ep(void const *beg, void const *end)
+{
+ for (void const *cur = beg; cur < end; cur = tu_desc_next(cur)) {
+ uint_fast8_t desc_type = tu_desc_type(cur);
+ if (TUSB_DESC_ENDPOINT == desc_type) return cur;
+ if (TUSB_DESC_INTERFACE == desc_type) break;
+ }
+ return end;
+}
+
+/** Find the first entity descriptor with the entity ID
+ * specified by the argument belonging to the current video control descriptor.
+ *
+ * @param[in] desc The video control interface descriptor.
+ * @param[in] entityid The target entity id.
+ *
+ * @return The pointer for interface descriptor.
+ * @retval end did not found interface descriptor */
+static void const* _find_desc_entity(void const *desc, uint_fast8_t entityid)
+{
+ tusb_desc_vc_itf_t const *vc = (tusb_desc_vc_itf_t const*)desc;
+ void const *beg = vc;
+ void const *end = beg + vc->std.bLength + vc->ctl.wTotalLength;
+ for (void const *cur = beg; cur < end; cur = _find_desc(cur, end, TUSB_DESC_CS_INTERFACE)) {
+ tusb_desc_cs_video_entity_itf_t const *itf = (tusb_desc_cs_video_entity_itf_t const *)cur;
+ if ((VIDEO_CS_ITF_VC_INPUT_TERMINAL <= itf->bDescriptorSubtype
+ && itf->bDescriptorSubtype < VIDEO_CS_ITF_VC_MAX)
+ && itf->bEntityId == entityid) {
+ return itf;
+ }
+ cur = tu_desc_next(cur);
+ }
+ return end;
+}
+
+/** Return the end of the video streaming descriptor. */
+static inline void const* _end_of_streaming_descriptor(void const *desc)
+{
+ tusb_desc_vs_itf_t const *vs = (tusb_desc_vs_itf_t const *)desc;
+ return desc + vs->std.bLength + vs->stm.wTotalLength;
+}
+
+/** Find the first format descriptor with the specified format number. */
+static inline tusb_desc_cs_video_fmt_uncompressed_t const *_find_desc_format(void const *beg, void const *end, uint_fast8_t fmtnum)
+{
+ return (tusb_desc_cs_video_fmt_uncompressed_t const*)
+ _find_desc_3(beg, end, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_FORMAT_UNCOMPRESSED, fmtnum);
+}
+
+/** Find the first frame descriptor with the specified format number. */
+static inline tusb_desc_cs_video_frm_uncompressed_t const *_find_desc_frame(void const *beg, void const *end, uint_fast8_t frmnum)
+{
+ return (tusb_desc_cs_video_frm_uncompressed_t const*)
+ _find_desc_3(beg, end, TUSB_DESC_CS_INTERFACE, VIDEO_CS_ITF_VS_FRAME_UNCOMPRESSED, frmnum);
+}
+
+/** Set uniquely determined values to variables that have not been set
+ *
+ * @param[in,out] param Target */
+static bool _update_streaming_parameters(videod_streaming_interface_t const *stm,
+ video_probe_and_commit_control_t *param)
+{
+ tusb_desc_vs_itf_t const *vs = _get_desc_vs(stm);
+ uint_fast8_t fmtnum = param->bFormatIndex;
+ TU_ASSERT(fmtnum <= vs->stm.bNumFormats);
+ if (!fmtnum) {
+ if (1 < vs->stm.bNumFormats) return true; /* Need to negotiate all variables. */
+ fmtnum = 1;
+ param->bFormatIndex = 1;
+ }
+
+ /* Set the parameters determined by the format */
+ param->wKeyFrameRate = 1;
+ param->wPFrameRate = 0;
+ param->wCompQuality = 1; /* 1 to 10000 */
+ param->wCompWindowSize = 1; /* GOP size? */
+ param->wDelay = 0; /* milliseconds */
+ param->dwClockFrequency = 27000000; /* same as MPEG-2 system time clock */
+ param->bmFramingInfo = 0x3; /* enables FrameID and EndOfFrame */
+ param->bPreferedVersion = 1;
+ param->bMinVersion = 1;
+ param->bMaxVersion = 1;
+ param->bUsage = 0;
+ param->bBitDepthLuma = 8;
+
+ void const *end = _end_of_streaming_descriptor(vs);
+ tusb_desc_cs_video_fmt_uncompressed_t const *fmt = _find_desc_format(tu_desc_next(vs), end, fmtnum);
+ TU_ASSERT(fmt != end);
+ uint_fast8_t frmnum = param->bFrameIndex;
+ TU_ASSERT(frmnum <= fmt->bNumFrameDescriptors);
+ if (!frmnum) {
+ if (1 < fmt->bNumFrameDescriptors) return true;
+ frmnum = 1;
+ param->bFrameIndex = 1;
+ }
+ tusb_desc_cs_video_frm_uncompressed_t const *frm = _find_desc_frame(tu_desc_next(fmt), end, frmnum);
+ TU_ASSERT(frm != end);
+
+ /* Set the parameters determined by the frame */
+ uint_fast32_t frame_size = param->dwMaxVideoFrameSize;
+ if (!frame_size) {
+ frame_size = (uint_fast32_t)frm->wWidth * frm->wHeight * fmt->bBitsPerPixel / 8;
+ param->dwMaxVideoFrameSize = frame_size;
+ }
+
+ uint_fast32_t interval = param->dwFrameInterval;
+ if (!interval) {
+ if ((1 < frm->bFrameIntervalType) ||
+ ((0 == frm->bFrameIntervalType) && (frm->dwFrameInterval[1] != frm->dwFrameInterval[0]))) {
+ return true;
+ }
+ interval = frm->dwFrameInterval[0];
+ param->dwFrameInterval = interval;
+ }
+ uint_fast32_t interval_ms = interval / 10000;
+ TU_ASSERT(interval_ms);
+ uint_fast32_t payload_size = (frame_size + interval_ms - 1) / interval_ms + 2;
+ param->dwMaxPayloadTransferSize = payload_size;
+ return true;
+}
+
+/** Set the minimum, maximum, default values or resolutions to variables which need to negotiate with the host
+ *
+ * @param[in] request GET_MAX, GET_MIN, GET_RES or GET_DEF
+ * @param[in,out] param Target
+ */
+static bool _negotiate_streaming_parameters(videod_streaming_interface_t const *stm, uint_fast8_t request,
+ video_probe_and_commit_control_t *param)
+{
+ uint_fast8_t const fmtnum = param->bFormatIndex;
+ if (!fmtnum) {
+ switch (request) {
+ case VIDEO_REQUEST_GET_MAX:
+ param->bFormatIndex = _get_desc_vs(stm)->stm.bNumFormats;
+ break;
+ case VIDEO_REQUEST_GET_MIN:
+ case VIDEO_REQUEST_GET_DEF:
+ param->bFormatIndex = 1;
+ break;
+ default: return false;
+ }
+ /* Set the parameters determined by the format */
+ param->wKeyFrameRate = 1;
+ param->wPFrameRate = 0;
+ param->wCompQuality = 1; /* 1 to 10000 */
+ param->wCompWindowSize = 1; /* GOP size? */
+ param->wDelay = 0; /* milliseconds */
+ param->dwClockFrequency = 27000000; /* same as MPEG-2 system time clock */
+ param->bmFramingInfo = 0x3; /* enables FrameID and EndOfFrame */
+ param->bPreferedVersion = 1;
+ param->bMinVersion = 1;
+ param->bMaxVersion = 1;
+ param->bUsage = 0;
+ param->bBitDepthLuma = 8;
+ return true;
+ }
+
+ uint_fast8_t frmnum = param->bFrameIndex;
+ if (!frmnum) {
+ tusb_desc_vs_itf_t const *vs = _get_desc_vs(stm);
+ void const *end = _end_of_streaming_descriptor(vs);
+ tusb_desc_cs_video_fmt_uncompressed_t const *fmt = _find_desc_format(tu_desc_next(vs), end, fmtnum);
+ switch (request) {
+ case VIDEO_REQUEST_GET_MAX:
+ frmnum = fmt->bNumFrameDescriptors;
+ break;
+ case VIDEO_REQUEST_GET_MIN:
+ frmnum = 1;
+ break;
+ case VIDEO_REQUEST_GET_DEF:
+ frmnum = fmt->bDefaultFrameIndex;
+ break;
+ default: return false;
+ }
+ param->bFrameIndex = frmnum;
+ /* Set the parameters determined by the frame */
+ tusb_desc_cs_video_frm_uncompressed_t const *frm = _find_desc_frame(tu_desc_next(fmt), end, frmnum);
+ param->dwMaxVideoFrameSize = frm->wWidth * frm->wHeight * fmt->bBitsPerPixel / 8;
+ return true;
+ }
+
+ if (!param->dwFrameInterval) {
+ tusb_desc_vs_itf_t const *vs = _get_desc_vs(stm);
+ void const *end = _end_of_streaming_descriptor(vs);
+ tusb_desc_cs_video_fmt_uncompressed_t const *fmt = _find_desc_format(tu_desc_next(vs), end, fmtnum);
+ tusb_desc_cs_video_frm_uncompressed_t const *frm = _find_desc_frame(tu_desc_next(fmt), end, frmnum);
+
+ uint_fast32_t interval, interval_ms;
+ switch (request) {
+ case VIDEO_REQUEST_GET_MAX:
+ {
+ uint_fast32_t min_interval, max_interval;
+ uint_fast8_t num_intervals = frm->bFrameIntervalType;
+ max_interval = num_intervals ? frm->dwFrameInterval[num_intervals - 1]: frm->dwFrameInterval[1];
+ min_interval = frm->dwFrameInterval[0];
+ interval = max_interval;
+ interval_ms = min_interval / 10000;
+ }
+ break;
+ case VIDEO_REQUEST_GET_MIN:
+ {
+ uint_fast32_t min_interval, max_interval;
+ uint_fast8_t num_intervals = frm->bFrameIntervalType;
+ max_interval = num_intervals ? frm->dwFrameInterval[num_intervals - 1]: frm->dwFrameInterval[1];
+ min_interval = frm->dwFrameInterval[0];
+ interval = min_interval;
+ interval_ms = max_interval / 10000;
+ }
+ break;
+ case VIDEO_REQUEST_GET_DEF:
+ interval = frm->dwDefaultFrameInterval;
+ interval_ms = interval / 10000;
+ break;
+ case VIDEO_REQUEST_GET_RES:
+ {
+ uint_fast8_t num_intervals = frm->bFrameIntervalType;
+ if (num_intervals) {
+ interval = 0;
+ } else {
+ interval = frm->dwFrameInterval[2];
+ interval_ms = interval / 10000;
+ }
+ }
+ break;
+ default: return false;
+ }
+ param->dwFrameInterval = interval;
+ if (!interval) {
+ param->dwMaxPayloadTransferSize = 0;
+ } else {
+ uint_fast32_t frame_size = param->dwMaxVideoFrameSize;
+ if (!interval_ms) {
+ param->dwMaxPayloadTransferSize = frame_size + 2;
+ } else {
+ param->dwMaxPayloadTransferSize = (frame_size + interval_ms - 1) / interval_ms + 2;
+ }
+ }
+ return true;
+ }
+ return true;
+}
+
+/** Close current video control interface.
+ *
+ * @param[in,out] self Video control interface context.
+ * @param[in] altnum The target alternate setting number. */
+static bool _close_vc_itf(uint8_t rhport, videod_interface_t *self)
+{
+ tusb_desc_vc_itf_t const *vc = _get_desc_vc(self);
+ /* The next descriptor after the class-specific VC interface header descriptor. */
+ void const *cur = (void const*)vc + vc->std.bLength + vc->ctl.bLength;
+ /* The end of the video control interface descriptor. */
+ void const *end = (void const*)vc + vc->std.bLength + vc->ctl.wTotalLength;
+ if (vc->std.bNumEndpoints) {
+ /* Find the notification endpoint descriptor. */
+ cur = _find_desc(cur, end, TUSB_DESC_ENDPOINT);
+ TU_ASSERT(cur < end);
+ tusb_desc_endpoint_t const *notif = (tusb_desc_endpoint_t const *)cur;
+ usbd_edpt_close(rhport, notif->bEndpointAddress);
+ }
+ self->cur = 0;
+ return true;
+}
+
+/** Set the alternate setting to own video control interface.
+ *
+ * @param[in,out] self Video control interface context.
+ * @param[in] altnum The target alternate setting number. */
+static bool _open_vc_itf(uint8_t rhport, videod_interface_t *self, uint_fast8_t altnum)
+{
+ TU_LOG2(" open VC %d\n", altnum);
+ void const *beg = self->beg;
+ void const *end = beg + self->len;
+ /* The first descriptor is a video control interface descriptor. */
+ void const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum);
+ TU_LOG2(" cur %d\n", cur - beg);
+ TU_VERIFY(cur < end);
+
+ tusb_desc_vc_itf_t const *vc = (tusb_desc_vc_itf_t const *)cur;
+ TU_LOG2(" bInCollection %d\n", vc->ctl.bInCollection);
+ /* Support for up to 2 streaming interfaces only. */
+ TU_ASSERT(vc->ctl.bInCollection <= CFG_TUD_VIDEO_STREAMING);
+
+ /* Update to point the end of the video control interface descriptor. */
+ end = cur + vc->std.bLength + vc->ctl.wTotalLength;
+ /* Advance to the next descriptor after the class-specific VC interface header descriptor. */
+ cur += vc->std.bLength + vc->ctl.bLength;
+ TU_LOG2(" bNumEndpoints %d\n", vc->std.bNumEndpoints);
+ /* Open the notification endpoint if it exist. */
+ if (vc->std.bNumEndpoints) {
+ /* Support for 1 endpoint only. */
+ TU_VERIFY(1 == vc->std.bNumEndpoints);
+ /* Find the notification endpoint descriptor. */
+ cur = _find_desc(cur, end, TUSB_DESC_ENDPOINT);
+ TU_VERIFY(cur < end);
+ tusb_desc_endpoint_t const *notif = (tusb_desc_endpoint_t const *)cur;
+ /* Open the notification endpoint */
+ TU_ASSERT(usbd_edpt_open(rhport, notif));
+ }
+ self->cur = (void const*)vc - beg;
+ return true;
+}
+
+/** Set the alternate setting to own video streaming interface.
+ *
+ * @param[in,out] stm Streaming interface context.
+ * @param[in] altnum The target alternate setting number. */
+static bool _open_vs_itf(uint8_t rhport, videod_streaming_interface_t *stm, uint_fast8_t altnum)
+{
+ uint_fast8_t i;
+ TU_LOG2(" reopen VS %d\n", altnum);
+ void const *desc = _videod_itf[stm->index_vc].beg;
+
+ /* Close endpoints of previous settings. */
+ for (i = 0; i < TU_ARRAY_SIZE(stm->desc.ep); ++i) {
+ uint_fast16_t ofs_ep = stm->desc.ep[i];
+ if (!ofs_ep) break;
+ uint_fast8_t ep_adr = _desc_ep_addr(desc + ofs_ep);
+ usbd_edpt_close(rhport, ep_adr);
+ stm->desc.ep[i] = 0;
+ TU_LOG2(" close EP%02x\n", ep_adr);
+ }
+ /* clear transfer management information */
+ stm->buffer = NULL;
+ stm->bufsize = 0;
+ stm->offset = 0;
+
+ /* Find a alternate interface */
+ void const *beg = desc + stm->desc.beg;
+ void const *end = desc + stm->desc.end;
+ void const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum);
+ TU_VERIFY(cur < end);
+ uint_fast8_t numeps = ((tusb_desc_interface_t const *)cur)->bNumEndpoints;
+ TU_ASSERT(numeps <= TU_ARRAY_SIZE(stm->desc.ep));
+ stm->desc.cur = cur - desc; /* Save the offset of the new settings */
+ if (!altnum) {
+ /* initialize streaming settings */
+ stm->max_payload_transfer_size = 0;
+ video_probe_and_commit_control_t *param =
+ (video_probe_and_commit_control_t *)&stm->ep_buf;
+ tu_memclr(param, sizeof(*param));
+ return _update_streaming_parameters(stm, param);
+ }
+ /* Open endpoints of the new settings. */
+ for (i = 0, cur = tu_desc_next(cur); i < numeps; ++i, cur = tu_desc_next(cur)) {
+ cur = _find_desc_ep(cur, end);
+ TU_ASSERT(cur < end);
+ tusb_desc_endpoint_t const *ep = (tusb_desc_endpoint_t const*)cur;
+ if (!stm->max_payload_transfer_size) {
+ video_probe_and_commit_control_t const *param = (video_probe_and_commit_control_t const*)&stm->ep_buf;
+ uint_fast32_t max_size = param->dwMaxPayloadTransferSize;
+ if ((TUSB_XFER_ISOCHRONOUS == ep->bmAttributes.xfer) &&
+ (tu_edpt_packet_size(ep) < max_size))
+ {
+ /* FS must be less than or equal to max packet size */
+ return false;
+ }
+ /* Set the negotiated value */
+ stm->max_payload_transfer_size = max_size;
+ }
+ TU_ASSERT(usbd_edpt_open(rhport, ep));
+ stm->desc.ep[i] = cur - desc;
+ TU_LOG2(" open EP%02x\n", _desc_ep_addr(cur));
+ }
+ /* initialize payload header */
+ tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
+ hdr->bHeaderLength = sizeof(*hdr);
+ hdr->bmHeaderInfo = 0;
+
+ return true;
+}
+
+/** Prepare the next packet payload. */
+static uint_fast16_t _prepare_in_payload(videod_streaming_interface_t *stm)
+{
+ uint_fast16_t remaining = stm->bufsize - stm->offset;
+ uint_fast16_t hdr_len = stm->ep_buf[0];
+ uint_fast16_t pkt_len = stm->max_payload_transfer_size;
+ if (hdr_len + remaining < pkt_len) {
+ pkt_len = hdr_len + remaining;
+ }
+ uint_fast16_t data_len = pkt_len - hdr_len;
+ memcpy(&stm->ep_buf[hdr_len], stm->buffer + stm->offset, data_len);
+ stm->offset += data_len;
+ remaining -= data_len;
+ if (!remaining) {
+ tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
+ hdr->EndOfFrame = 1;
+ }
+ return hdr_len + data_len;
+}
+
+/** Handle a standard request to the video control interface. */
+static int handle_video_ctl_std_req(uint8_t rhport, uint8_t stage,
+ tusb_control_request_t const *request,
+ uint_fast8_t ctl_idx)
+{
+ switch (request->bRequest) {
+ case TUSB_REQ_GET_INTERFACE:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ tusb_desc_vc_itf_t const *vc = _get_desc_vc(&_videod_itf[ctl_idx]);
+ TU_VERIFY(vc, VIDEO_ERROR_UNKNOWN);
+
+ uint8_t alt_num = vc->std.bAlternateSetting;
+
+ TU_VERIFY(tud_control_xfer(rhport, request, &alt_num, sizeof(alt_num)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case TUSB_REQ_SET_INTERFACE:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(0 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(_close_vc_itf(rhport, &_videod_itf[ctl_idx]), VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(_open_vc_itf(rhport, &_videod_itf[ctl_idx], request->wValue), VIDEO_ERROR_UNKNOWN);
+ tud_control_status(rhport, request);
+ }
+ return VIDEO_ERROR_NONE;
+
+ default: /* Unknown/Unsupported request */
+ TU_BREAKPOINT();
+ return VIDEO_ERROR_INVALID_REQUEST;
+ }
+}
+
+static int handle_video_ctl_cs_req(uint8_t rhport, uint8_t stage,
+ tusb_control_request_t const *request,
+ uint_fast8_t ctl_idx)
+{
+ videod_interface_t *self = &_videod_itf[ctl_idx];
+
+ /* 4.2.1 Interface Control Request */
+ switch (TU_U16_HIGH(request->wValue)) {
+ case VIDEO_VC_CTL_VIDEO_POWER_MODE:
+ switch (request->bRequest) {
+ case VIDEO_REQUEST_SET_CUR:
+ if (stage == CONTROL_STAGE_SETUP) {
+ TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, &self->power_mode, sizeof(self->power_mode)), VIDEO_ERROR_UNKNOWN);
+ } else if (stage == CONTROL_STAGE_ACK) {
+ if (tud_video_power_mode_cb) return tud_video_power_mode_cb(ctl_idx, self->power_mode);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_CUR:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, &self->power_mode, sizeof(self->power_mode)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_INFO:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ default: break;
+ }
+ break;
+
+ case VIDEO_VC_CTL_REQUEST_ERROR_CODE:
+ switch (request->bRequest) {
+ case VIDEO_REQUEST_GET_CUR:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_INFO:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get, sizeof(_cap_get)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ default: break;
+ }
+ break;
+
+ default: break;
+ }
+
+ /* Unknown/Unsupported request */
+ TU_BREAKPOINT();
+ return VIDEO_ERROR_INVALID_REQUEST;
+}
+
+static int handle_video_ctl_req(uint8_t rhport, uint8_t stage,
+ tusb_control_request_t const *request,
+ uint_fast8_t ctl_idx)
+{
+ uint_fast8_t entity_id;
+ switch (request->bmRequestType_bit.type) {
+ case TUSB_REQ_TYPE_STANDARD:
+ return handle_video_ctl_std_req(rhport, stage, request, ctl_idx);
+
+ case TUSB_REQ_TYPE_CLASS:
+ entity_id = TU_U16_HIGH(request->wIndex);
+ if (!entity_id) {
+ return handle_video_ctl_cs_req(rhport, stage, request, ctl_idx);
+ } else {
+ TU_VERIFY(_find_desc_entity(_get_desc_vc(&_videod_itf[ctl_idx]), entity_id), VIDEO_ERROR_INVALID_REQUEST);
+ return VIDEO_ERROR_NONE;
+ }
+
+ default:
+ return VIDEO_ERROR_INVALID_REQUEST;
+ }
+}
+
+static int handle_video_stm_std_req(uint8_t rhport, uint8_t stage,
+ tusb_control_request_t const *request,
+ uint_fast8_t stm_idx)
+{
+ videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
+ switch (request->bRequest) {
+ case TUSB_REQ_GET_INTERFACE:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ tusb_desc_vs_itf_t const *vs = _get_desc_vs(self);
+ TU_VERIFY(vs, VIDEO_ERROR_UNKNOWN);
+ uint8_t alt_num = vs->std.bAlternateSetting;
+
+ TU_VERIFY(tud_control_xfer(rhport, request, &alt_num, sizeof(alt_num)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case TUSB_REQ_SET_INTERFACE:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(_open_vs_itf(rhport, self, request->wValue), VIDEO_ERROR_UNKNOWN);
+ tud_control_status(rhport, request);
+ }
+ return VIDEO_ERROR_NONE;
+
+ default: /* Unknown/Unsupported request */
+ TU_BREAKPOINT();
+ return VIDEO_ERROR_INVALID_REQUEST;
+ }
+}
+
+static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
+ tusb_control_request_t const *request,
+ uint_fast8_t stm_idx)
+{
+ (void)rhport;
+ videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
+
+ /* 4.2.1 Interface Control Request */
+ switch (TU_U16_HIGH(request->wValue)) {
+ case VIDEO_VS_CTL_STREAM_ERROR_CODE:
+ switch (request->bRequest) {
+ case VIDEO_REQUEST_GET_CUR:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ /* TODO */
+ TU_VERIFY(tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_INFO:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get, sizeof(_cap_get)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ default: break;
+ }
+ break;
+
+ case VIDEO_VS_CTL_PROBE:
+ switch (request->bRequest) {
+ case VIDEO_REQUEST_SET_CUR:
+ if (stage == CONTROL_STAGE_SETUP) {
+ TU_VERIFY(sizeof(video_probe_and_commit_control_t) == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)),
+ VIDEO_ERROR_UNKNOWN);
+ } else if (stage == CONTROL_STAGE_ACK) {
+ TU_VERIFY(_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf),
+ VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_CUR:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_MIN:
+ case VIDEO_REQUEST_GET_MAX:
+ case VIDEO_REQUEST_GET_RES:
+ case VIDEO_REQUEST_GET_DEF:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
+ video_probe_and_commit_control_t tmp;
+ tmp = *(video_probe_and_commit_control_t*)&self->ep_buf;
+ TU_VERIFY(_negotiate_streaming_parameters(self, request->bRequest, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
+ TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_LEN:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(2 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ uint16_t len = sizeof(video_probe_and_commit_control_t);
+ TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)&len, sizeof(len)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_INFO:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ default: break;
+ }
+ break;
+
+ case VIDEO_VS_CTL_COMMIT:
+ switch (request->bRequest) {
+ case VIDEO_REQUEST_SET_CUR:
+ if (stage == CONTROL_STAGE_SETUP) {
+ TU_VERIFY(sizeof(video_probe_and_commit_control_t) == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
+ } else if (stage == CONTROL_STAGE_ACK) {
+ TU_VERIFY(_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
+ if (tud_video_commit_cb) {
+ return tud_video_commit_cb(self->index_vc, self->index_vs, (video_probe_and_commit_control_t*)self->ep_buf);
+ }
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_CUR:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_LEN:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(2 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ uint16_t len = sizeof(video_probe_and_commit_control_t);
+ TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)&len, sizeof(len)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ case VIDEO_REQUEST_GET_INFO:
+ if (stage == CONTROL_STAGE_SETUP)
+ {
+ TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)(uintptr_t) &_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN);
+ }
+ return VIDEO_ERROR_NONE;
+
+ default: break;
+ }
+ break;
+
+ case VIDEO_VS_CTL_STILL_PROBE:
+ case VIDEO_VS_CTL_STILL_COMMIT:
+ case VIDEO_VS_CTL_STILL_IMAGE_TRIGGER:
+ case VIDEO_VS_CTL_GENERATE_KEY_FRAME:
+ case VIDEO_VS_CTL_UPDATE_FRAME_SEGMENT:
+ case VIDEO_VS_CTL_SYNCH_DELAY_CONTROL:
+ /* TODO */
+ break;
+
+ default: break;
+ }
+
+ /* Unknown/Unsupported request */
+ TU_BREAKPOINT();
+ return VIDEO_ERROR_INVALID_REQUEST;
+}
+
+static int handle_video_stm_req(uint8_t rhport, uint8_t stage,
+ tusb_control_request_t const *request,
+ uint_fast8_t stm_idx)
+{
+ switch (request->bmRequestType_bit.type) {
+ case TUSB_REQ_TYPE_STANDARD:
+ return handle_video_stm_std_req(rhport, stage, request, stm_idx);
+
+ case TUSB_REQ_TYPE_CLASS:
+ if (TU_U16_HIGH(request->wIndex)) return VIDEO_ERROR_INVALID_REQUEST;
+ return handle_video_stm_cs_req(rhport, stage, request, stm_idx);
+
+ default: return VIDEO_ERROR_INVALID_REQUEST;
+ }
+ return VIDEO_ERROR_UNKNOWN;
+}
+
+//--------------------------------------------------------------------+
+// APPLICATION API
+//--------------------------------------------------------------------+
+
+bool tud_video_n_connected(uint_fast8_t ctl_idx)
+{
+ TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
+ videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, 0);
+ if (stm) return true;
+ return false;
+}
+
+bool tud_video_n_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
+{
+ TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
+ TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING);
+ videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx);
+ if (!stm || !stm->desc.ep[0]) return false;
+ return true;
+}
+
+bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *buffer, size_t bufsize)
+{
+ TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
+ TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING);
+ if (!buffer || !bufsize) return false;
+ videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx);
+ if (!stm || !stm->desc.ep[0] || stm->buffer) return false;
+
+ /* Find EP address */
+ void const *desc = _videod_itf[stm->index_vc].beg;
+ uint_fast8_t ep_addr = 0;
+ for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
+ uint_fast16_t ofs_ep = stm->desc.ep[i];
+ if (!ofs_ep) continue;
+ ep_addr = _desc_ep_addr(desc + ofs_ep);
+ break;
+ }
+ if (!ep_addr) return false;
+
+ TU_VERIFY( usbd_edpt_claim(0, ep_addr));
+ /* update the packet header */
+ tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
+ hdr->FrameID ^= 1;
+ hdr->EndOfFrame = 0;
+ /* update the packet data */
+ stm->buffer = (uint8_t*)buffer;
+ stm->bufsize = bufsize;
+ uint_fast16_t pkt_len = _prepare_in_payload(stm);
+ TU_ASSERT( usbd_edpt_xfer(0, ep_addr, stm->ep_buf, pkt_len), 0);
+ return true;
+}
+
+//--------------------------------------------------------------------+
+// USBD Driver API
+//--------------------------------------------------------------------+
+void videod_init(void)
+{
+ for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO; ++i) {
+ videod_interface_t* ctl = &_videod_itf[i];
+ tu_memclr(ctl, sizeof(*ctl));
+ }
+ for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
+ videod_streaming_interface_t *stm = &_videod_streaming_itf[i];
+ tu_memclr(stm, ITF_STM_MEM_RESET_SIZE);
+ }
+}
+
+void videod_reset(uint8_t rhport)
+{
+ (void) rhport;
+ for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO; ++i) {
+ videod_interface_t* ctl = &_videod_itf[i];
+ tu_memclr(ctl, sizeof(*ctl));
+ }
+ for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
+ videod_streaming_interface_t *stm = &_videod_streaming_itf[i];
+ tu_memclr(stm, ITF_STM_MEM_RESET_SIZE);
+ }
+}
+
+uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
+{
+ TU_VERIFY((TUSB_CLASS_VIDEO == itf_desc->bInterfaceClass) &&
+ (VIDEO_SUBCLASS_CONTROL == itf_desc->bInterfaceSubClass) &&
+ (VIDEO_ITF_PROTOCOL_15 == itf_desc->bInterfaceProtocol), 0);
+
+ /* Find available interface */
+ videod_interface_t *self = NULL;
+ uint_fast8_t ctl_idx;
+ for (ctl_idx = 0; ctl_idx < CFG_TUD_VIDEO; ++ctl_idx) {
+ if (_videod_itf[ctl_idx].beg) continue;
+ self = &_videod_itf[ctl_idx];
+ break;
+ }
+ TU_ASSERT(ctl_idx < CFG_TUD_VIDEO, 0);
+
+ void const *end = (void const*)itf_desc + max_len;
+ self->beg = itf_desc;
+ self->len = max_len;
+ /*------------- Video Control Interface -------------*/
+ TU_VERIFY(_open_vc_itf(rhport, self, 0), 0);
+ tusb_desc_vc_itf_t const *vc = _get_desc_vc(self);
+ uint_fast8_t bInCollection = vc->ctl.bInCollection;
+ /* Find the end of the video interface descriptor */
+ void const *cur = _next_desc_itf(itf_desc, end);
+ for (uint_fast8_t stm_idx = 0; stm_idx < bInCollection; ++stm_idx) {
+ videod_streaming_interface_t *stm = NULL;
+ /* find free streaming interface handle */
+ for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
+ if (_videod_streaming_itf[i].desc.beg) continue;
+ stm = &_videod_streaming_itf[i];
+ self->stm[stm_idx] = i;
+ break;
+ }
+ TU_ASSERT(stm, 0);
+ stm->index_vc = ctl_idx;
+ stm->index_vs = stm_idx;
+ stm->desc.beg = (uintptr_t)cur - (uintptr_t)itf_desc;
+ cur = _next_desc_itf(cur, end);
+ stm->desc.end = (uintptr_t)cur - (uintptr_t)itf_desc;
+ }
+ self->len = (uintptr_t)cur - (uintptr_t)itf_desc;
+ return (uintptr_t)cur - (uintptr_t)itf_desc;
+}
+
+// Invoked when a control transfer occurred on an interface of this class
+// Driver response accordingly to the request and the transfer stage (setup/data/ack)
+// return false to stall control endpoint (e.g unsupported request)
+bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
+{
+ int err;
+ TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
+ uint_fast8_t itfnum = tu_u16_low(request->wIndex);
+
+ /* Identify which control interface to use */
+ uint_fast8_t itf;
+ for (itf = 0; itf < CFG_TUD_VIDEO; ++itf) {
+ void const *desc = _videod_itf[itf].beg;
+ if (!desc) continue;
+ if (itfnum == _desc_itfnum(desc)) break;
+ }
+
+ if (itf < CFG_TUD_VIDEO) {
+ err = handle_video_ctl_req(rhport, stage, request, itf);
+ _videod_itf[itf].error_code = (uint8_t)err;
+ if (err) return false;
+ return true;
+ }
+
+ /* Identify which streaming interface to use */
+ for (itf = 0; itf < CFG_TUD_VIDEO_STREAMING; ++itf) {
+ videod_streaming_interface_t *stm = &_videod_streaming_itf[itf];
+ if (!stm->desc.beg) continue;
+ void const *desc = _videod_itf[stm->index_vc].beg;
+ if (itfnum == _desc_itfnum(desc + stm->desc.beg)) break;
+ }
+
+ if (itf < CFG_TUD_VIDEO_STREAMING) {
+ err = handle_video_stm_req(rhport, stage, request, itf);
+ _videod_streaming_itf[itf].error_code = (uint8_t)err;
+ if (err) return false;
+ return true;
+ }
+ return false;
+}
+
+bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
+{
+ (void)result; (void)xferred_bytes;
+
+ /* find streaming handle */
+ uint_fast8_t itf;
+ videod_interface_t *ctl;
+ videod_streaming_interface_t *stm;
+ for (itf = 0; itf < CFG_TUD_VIDEO_STREAMING; ++itf) {
+ stm = &_videod_streaming_itf[itf];
+ uint_fast16_t const ep_ofs = stm->desc.ep[0];
+ if (!ep_ofs) continue;
+ ctl = &_videod_itf[stm->index_vc];
+ void const *desc = ctl->beg;
+ if (ep_addr == _desc_ep_addr(desc + ep_ofs)) break;
+ }
+
+ TU_ASSERT(itf < CFG_TUD_VIDEO_STREAMING);
+ if (stm->offset < stm->bufsize) {
+ /* Claim the endpoint */
+ TU_VERIFY( usbd_edpt_claim(rhport, ep_addr), 0);
+ uint_fast16_t pkt_len = _prepare_in_payload(stm);
+ TU_ASSERT( usbd_edpt_xfer(rhport, ep_addr, stm->ep_buf, pkt_len), 0);
+ } else {
+ stm->buffer = NULL;
+ stm->bufsize = 0;
+ stm->offset = 0;
+ if (tud_video_frame_xfer_complete_cb) {
+ tud_video_frame_xfer_complete_cb(stm->index_vc, stm->index_vs);
+ }
+ }
+ return true;
+}
+
+#endif
diff --git a/src/class/video/video_device.h b/src/class/video/video_device.h
new file mode 100644
index 000000000..ee2fcb9d5
--- /dev/null
+++ b/src/class/video/video_device.h
@@ -0,0 +1,97 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ * Copyright (c) 2021 Koji KITAYAMA
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#ifndef TUSB_VIDEO_DEVICE_H_
+#define TUSB_VIDEO_DEVICE_H_
+
+#include "common/tusb_common.h"
+#include "video.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//--------------------------------------------------------------------+
+// Application API (Multiple Ports)
+// CFG_TUD_VIDEO > 1
+//--------------------------------------------------------------------+
+
+/** Return true if streaming
+ *
+ * @param[in] ctl_idx Destination control interface index
+ * @param[in] stm_idx Destination streaming interface index */
+bool tud_video_n_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx);
+
+/** Transfer a frame
+ *
+ * @param[in] ctl_idx Destination control interface index
+ * @param[in] stm_idx Destination streaming interface index
+ * @param[in] buffer Frame buffer. The caller must not use this buffer until the operation is completed.
+ * @param[in] bufsize Byte size of the frame buffer */
+bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *buffer, size_t bufsize);
+
+/*------------- Optional callbacks -------------*/
+/** Invoked when compeletion of a frame transfer
+ *
+ * @param[in] ctl_idx Destination control interface index
+ * @param[in] stm_idx Destination streaming interface index */
+TU_ATTR_WEAK void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx);
+
+//--------------------------------------------------------------------+
+// Application Callback API (weak is optional)
+//--------------------------------------------------------------------+
+
+/** Invoked when SET_POWER_MODE request received
+ *
+ * @param[in] ctl_idx Destination control interface index
+ * @param[in] stm_idx Destination streaming interface index
+ * @return video_error_code_t */
+TU_ATTR_WEAK int tud_video_power_mode_cb(uint_fast8_t ctl_idx, uint8_t power_mod);
+
+/** Invoked when VS_COMMIT_CONTROL(SET_CUR) request received
+ *
+ * @param[in] ctl_idx Destination control interface index
+ * @param[in] stm_idx Destination streaming interface index
+ * @param[in] parameters Video streaming parameters
+ * @return video_error_code_t */
+TU_ATTR_WEAK int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx,
+ video_probe_and_commit_control_t const *parameters);
+
+//--------------------------------------------------------------------+
+// INTERNAL USBD-CLASS DRIVER API
+//--------------------------------------------------------------------+
+void videod_init (void);
+void videod_reset (uint8_t rhport);
+uint16_t videod_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len);
+bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request);
+bool videod_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif