2021-07-17 18:51:33 +09:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
|
|
|
* Copyright (c) 2020 Reinhard Panhuber, Jerzy Kasenberg
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tusb_option.h"
|
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_VIDEO && CFG_TUD_VIDEO_STREAMING)
|
2021-07-17 18:51:33 +09:00
|
|
|
|
|
|
|
#include "device/usbd.h"
|
|
|
|
#include "device/usbd_pvt.h"
|
|
|
|
|
|
|
|
#include "video_device.h"
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// MACRO CONSTANT TYPEDEF
|
|
|
|
//--------------------------------------------------------------------+
|
2021-07-19 21:28:05 +09:00
|
|
|
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;
|
|
|
|
|
2021-07-25 14:01:35 +09:00
|
|
|
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;
|
|
|
|
|
2021-07-19 21:28:05 +09:00
|
|
|
typedef struct TU_ATTR_PACKED {
|
|
|
|
uint8_t bLength;
|
|
|
|
uint8_t bDescriptorType;
|
|
|
|
uint8_t bDescriptorSubtype;
|
2021-07-25 14:01:35 +09:00
|
|
|
uint8_t bEntityId;
|
2021-07-19 21:28:05 +09:00
|
|
|
} tusb_desc_cs_video_entity_itf_t;
|
|
|
|
|
2021-09-26 17:15:00 +09:00
|
|
|
/* video streaming interface */
|
2021-09-20 14:55:56 +09:00
|
|
|
typedef struct TU_ATTR_PACKED {
|
2021-09-26 17:15:00 +09:00
|
|
|
uint8_t index_vc; /* index of bound video control interface */
|
|
|
|
uint8_t index_vs; /* index from the video control interface */
|
2021-09-20 14:55:56 +09:00
|
|
|
struct {
|
2021-09-26 21:52:56 +09:00
|
|
|
uint16_t beg; /* Offset of the begging of video streaming interface descriptor */
|
2021-09-20 14:55:56 +09:00
|
|
|
uint16_t end; /* Offset of the end of video streaming interface descriptor */
|
|
|
|
uint16_t cur; /* Offset of the current settings */
|
2021-09-26 17:15:00 +09:00
|
|
|
uint16_t ep[2]; /* Offset of endpoint descriptors. 0: streaming, 1: still capture */
|
2021-09-20 14:55:56 +09:00
|
|
|
} desc;
|
2021-09-26 21:52:56 +09:00
|
|
|
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 */
|
2021-09-20 14:55:56 +09:00
|
|
|
uint32_t max_payload_transfer_size;
|
2021-09-29 01:18:33 +09:00
|
|
|
uint8_t error_code;/* error code */
|
2021-09-26 17:15:00 +09:00
|
|
|
/*------------- From this point, data is not cleared by bus reset -------------*/
|
2021-09-26 21:52:56 +09:00
|
|
|
CFG_TUSB_MEM_ALIGN uint8_t ep_buf[CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE]; /* EP transfer buffer for streaming */
|
2021-08-15 18:07:53 +09:00
|
|
|
} videod_streaming_interface_t;
|
|
|
|
|
2021-09-26 17:15:00 +09:00
|
|
|
/* video control interface */
|
2021-09-20 14:55:56 +09:00
|
|
|
typedef struct TU_ATTR_PACKED {
|
2021-07-25 14:01:35 +09:00
|
|
|
void const *beg; /* The head of the first video control interface descriptor */
|
|
|
|
uint16_t len; /* Byte length of the descriptors */
|
2021-09-20 14:55:56 +09:00
|
|
|
uint16_t cur; /* offset for current video control interface */
|
|
|
|
uint8_t stm[CFG_TUD_VIDEO_STREAMING]; /* Indices of streaming interface */
|
2021-09-26 17:15:00 +09:00
|
|
|
uint8_t error_code; /* error code */
|
2021-07-25 14:01:35 +09:00
|
|
|
uint8_t power_mode;
|
2021-07-17 18:51:33 +09:00
|
|
|
|
|
|
|
/*------------- From this point, data is not cleared by bus reset -------------*/
|
2021-09-26 21:52:56 +09:00
|
|
|
// CFG_TUSB_MEM_ALIGN uint8_t ctl_buf[64]; /* EP transfer buffer for interrupt transfer */
|
2021-07-17 18:51:33 +09:00
|
|
|
|
|
|
|
} videod_interface_t;
|
|
|
|
|
2021-09-26 17:15:00 +09:00
|
|
|
#define ITF_STM_MEM_RESET_SIZE offsetof(videod_streaming_interface_t, ep_buf)
|
2021-07-17 18:51:33 +09:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// INTERNAL OBJECT & FUNCTION DECLARATION
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
CFG_TUSB_MEM_SECTION static videod_interface_t _videod_itf[CFG_TUD_VIDEO];
|
2021-09-20 14:55:56 +09:00
|
|
|
CFG_TUSB_MEM_SECTION static videod_streaming_interface_t _videod_streaming_itf[CFG_TUD_VIDEO_STREAMING];
|
2021-07-17 18:51:33 +09:00
|
|
|
|
2021-07-25 14:01:35 +09:00
|
|
|
static uint8_t const _cap_get = 0x1u; /* support for GET */
|
|
|
|
static uint8_t const _cap_get_set = 0x3u; /* support for GET and SET */
|
|
|
|
|
2021-09-26 17:44:36 +09:00
|
|
|
/** Get interface number from the interface descriptor
|
|
|
|
*
|
|
|
|
* @param[in] desc interface descriptor
|
|
|
|
*
|
|
|
|
* @return bInterfaceNumber */
|
2021-09-20 14:55:56 +09:00
|
|
|
static inline uint8_t _desc_itfnum(void const *desc)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-09-20 14:55:56 +09:00
|
|
|
return ((uint8_t const*)desc)[2];
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
|
|
|
|
2021-09-26 17:44:36 +09:00
|
|
|
/** Get endpoint address from the endpoint descriptor
|
|
|
|
*
|
|
|
|
* @param[in] desc endpoint descriptor
|
|
|
|
*
|
|
|
|
* @return bEndpointAddress */
|
2021-09-20 14:55:56 +09:00
|
|
|
static inline uint8_t _desc_ep_addr(void const *desc)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-09-20 14:55:56 +09:00
|
|
|
return ((uint8_t const*)desc)[2];
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
|
|
|
|
2021-09-26 17:44:36 +09:00
|
|
|
/** 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 */
|
2021-09-26 17:15:00 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-20 14:55:56 +09:00
|
|
|
static tusb_desc_vc_itf_t const* _get_desc_vc(videod_interface_t const *self)
|
2021-07-31 19:35:22 +09:00
|
|
|
{
|
2021-09-20 14:55:56 +09:00
|
|
|
return (tusb_desc_vc_itf_t const *)(self->beg + self->cur);
|
|
|
|
}
|
|
|
|
|
2021-09-26 17:15:00 +09:00
|
|
|
static tusb_desc_vs_itf_t const* _get_desc_vs(videod_streaming_interface_t const *self)
|
2021-09-20 14:55:56 +09:00
|
|
|
{
|
|
|
|
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);
|
2021-07-31 19:35:22 +09:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:18:33 +09:00
|
|
|
/** Find the first descriptor of a given type
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
2021-09-29 01:18:33 +09:00
|
|
|
* @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.
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
|
|
|
* @return The pointer for interface descriptor.
|
|
|
|
* @retval end did not found interface descriptor */
|
2021-09-29 01:18:33 +09:00
|
|
|
static void const* _find_desc(void const *beg, void const *end, uint_fast8_t desc_type)
|
2021-07-17 18:51:33 +09:00
|
|
|
{
|
2021-07-25 14:01:35 +09:00
|
|
|
void const *cur = beg;
|
2021-09-29 01:18:33 +09:00
|
|
|
while ((cur < end) && (desc_type != tu_desc_type(cur))) {
|
2021-07-25 14:01:35 +09:00
|
|
|
cur = tu_desc_next(cur);
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
return cur;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:18:33 +09:00
|
|
|
/** 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.
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
|
|
|
* @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 */
|
2021-07-25 14:01:35 +09:00
|
|
|
static void const* _next_desc_itf(void const *beg, void const *end)
|
2021-07-19 21:28:05 +09:00
|
|
|
{
|
2021-07-25 14:01:35 +09:00
|
|
|
void const *cur = beg;
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast8_t itfnum = ((tusb_desc_interface_t const*)cur)->bInterfaceNumber;
|
2021-07-25 14:01:35 +09:00
|
|
|
while ((cur < end) &&
|
2021-08-15 18:07:53 +09:00
|
|
|
(itfnum == ((tusb_desc_interface_t const*)cur)->bInterfaceNumber)) {
|
2021-07-25 14:01:35 +09:00
|
|
|
cur = _find_desc(tu_desc_next(cur), end, TUSB_DESC_INTERFACE);
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
return cur;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
|
|
|
|
2021-07-25 14:01:35 +09:00
|
|
|
/** Find the first interface descriptor with the specified interface number and alternate setting number.
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
|
|
|
* @param[in] beg The head of descriptor byte array.
|
|
|
|
* @param[in] end The tail of descriptor byte array.
|
2021-07-25 14:01:35 +09:00
|
|
|
* @param[in] itfnum The target interface number.
|
|
|
|
* @param[in] altnum The target alternate setting number.
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
|
|
|
* @return The pointer for interface descriptor.
|
|
|
|
* @retval end did not found interface descriptor */
|
2021-09-29 01:18:33 +09:00
|
|
|
static inline void const* _find_desc_itf(void const *beg, void const *end, uint_fast8_t itfnum, uint_fast8_t altnum)
|
2021-07-19 21:28:05 +09:00
|
|
|
{
|
2021-09-29 01:18:33 +09:00
|
|
|
return _find_desc_3(beg, end, TUSB_DESC_INTERFACE, itfnum, altnum);
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
|
|
|
|
2021-09-29 01:18:33 +09:00
|
|
|
/** Find the first endpoint descriptor belonging to the current interface descriptor.
|
|
|
|
*
|
|
|
|
* The search range is from `beg` to `end` or the next interface descriptor.
|
2021-09-20 14:55:56 +09:00
|
|
|
*
|
|
|
|
* @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)) {
|
2021-09-26 17:15:00 +09:00
|
|
|
uint_fast8_t desc_type = tu_desc_type(cur);
|
2021-09-20 14:55:56 +09:00
|
|
|
if (TUSB_DESC_ENDPOINT == desc_type) return cur;
|
|
|
|
if (TUSB_DESC_INTERFACE == desc_type) break;
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:18:33 +09:00
|
|
|
/** Find the first entity descriptor with the entity ID
|
|
|
|
* specified by the argument belonging to the current video control descriptor.
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
2021-09-29 01:18:33 +09:00
|
|
|
* @param[in] desc The video control interface descriptor.
|
2021-07-25 14:01:35 +09:00
|
|
|
* @param[in] entityid The target entity id.
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
|
|
|
* @return The pointer for interface descriptor.
|
|
|
|
* @retval end did not found interface descriptor */
|
2021-09-29 01:18:33 +09:00
|
|
|
static void const* _find_desc_entity(void const *desc, uint_fast8_t entityid)
|
2021-07-19 21:28:05 +09:00
|
|
|
{
|
2021-09-29 01:18:33 +09:00
|
|
|
tusb_desc_vc_itf_t const *vc = (tusb_desc_vc_itf_t const*)desc;
|
|
|
|
void const *beg = vc;
|
2021-08-01 17:11:43 +09:00
|
|
|
void const *end = beg + vc->std.bLength + vc->ctl.wTotalLength;
|
2021-07-25 14:01:35 +09:00
|
|
|
for (void const *cur = beg; cur < end; cur = _find_desc(cur, end, TUSB_DESC_CS_INTERFACE)) {
|
2021-07-19 21:28:05 +09:00
|
|
|
tusb_desc_cs_video_entity_itf_t const *itf = (tusb_desc_cs_video_entity_itf_t const *)cur;
|
2021-08-15 18:07:53 +09:00
|
|
|
if ((VIDEO_CS_VC_INTERFACE_INPUT_TERMINAL <= itf->bDescriptorSubtype
|
|
|
|
&& itf->bDescriptorSubtype < VIDEO_CS_VC_INTERFACE_MAX)
|
|
|
|
&& itf->bEntityId == entityid) {
|
2021-07-19 21:28:05 +09:00
|
|
|
return itf;
|
|
|
|
}
|
|
|
|
cur = tu_desc_next(cur);
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
2021-09-29 01:18:33 +09:00
|
|
|
/** 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_VS_INTERFACE_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_VS_INTERFACE_FRAME_UNCOMPRESSED, frmnum);
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:53:26 +09:00
|
|
|
/** Set uniquely determined values to variables that have not been set
|
|
|
|
*
|
|
|
|
* @param[in,out] param Target */
|
2021-09-29 01:18:33 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-29 22:53:26 +09:00
|
|
|
/** Set the minimum or the maximum values to variables which need to negotiate with the host
|
|
|
|
*
|
|
|
|
* @param[in] set_max If true, the maximum values is set, otherwise the minimum value is set.
|
|
|
|
* @param[in,out] param Target
|
|
|
|
*/
|
2021-09-29 01:18:33 +09:00
|
|
|
static bool _negotiate_streaming_parameters(videod_streaming_interface_t const *stm, bool set_max,
|
|
|
|
video_probe_and_commit_control_t *param)
|
|
|
|
{
|
|
|
|
uint_fast8_t const fmtnum = param->bFormatIndex;
|
|
|
|
if (!fmtnum) {
|
|
|
|
if (set_max) {
|
|
|
|
tusb_desc_vs_itf_t const *vs = _get_desc_vs(stm);
|
|
|
|
param->bFormatIndex = vs->stm.bNumFormats;
|
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
frmnum = set_max ? fmt->bNumFrameDescriptors: 1;
|
|
|
|
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;
|
|
|
|
uint_fast8_t num_intervals = frm->bFrameIntervalType;
|
|
|
|
if (!num_intervals) {
|
|
|
|
interval = set_max ? frm->dwFrameInterval[1]: frm->dwFrameInterval[0];
|
|
|
|
} else {
|
|
|
|
interval = set_max ? frm->dwFrameInterval[num_intervals - 1]: frm->dwFrameInterval[0];
|
|
|
|
}
|
|
|
|
param->dwFrameInterval = interval;
|
|
|
|
uint_fast32_t interval_ms = interval / 10000;
|
|
|
|
TU_ASSERT(interval_ms);
|
|
|
|
uint_fast32_t frame_size = param->dwMaxVideoFrameSize;
|
|
|
|
param->dwMaxPayloadTransferSize = (frame_size + interval_ms - 1) / interval_ms + 2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:35:22 +09:00
|
|
|
/** Close current video control interface.
|
|
|
|
*
|
2021-09-26 21:52:56 +09:00
|
|
|
* @param[in,out] self Video control interface context.
|
2021-07-31 19:35:22 +09:00
|
|
|
* @param[in] altnum The target alternate setting number. */
|
2021-07-25 14:01:35 +09:00
|
|
|
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. */
|
2021-08-01 17:11:43 +09:00
|
|
|
void const *end = (void const*)vc + vc->std.bLength + vc->ctl.wTotalLength;
|
2021-07-25 14:01:35 +09:00
|
|
|
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);
|
|
|
|
}
|
2021-09-20 14:55:56 +09:00
|
|
|
self->cur = 0;
|
2021-07-25 14:01:35 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
/** Set the alternate setting to own video control interface.
|
2021-07-19 21:28:05 +09:00
|
|
|
*
|
2021-09-26 21:52:56 +09:00
|
|
|
* @param[in,out] self Video control interface context.
|
2021-07-25 14:01:35 +09:00
|
|
|
* @param[in] altnum The target alternate setting number. */
|
2021-09-25 16:59:17 +09:00
|
|
|
static bool _open_vc_itf(uint8_t rhport, videod_interface_t *self, uint_fast8_t altnum)
|
2021-07-19 21:28:05 +09:00
|
|
|
{
|
2021-09-26 17:44:36 +09:00
|
|
|
TU_LOG2(" open VC %d\n", altnum);
|
2021-07-19 21:28:05 +09:00
|
|
|
void const *beg = self->beg;
|
2021-07-25 14:01:35 +09:00
|
|
|
void const *end = beg + self->len;
|
2021-07-31 19:35:22 +09:00
|
|
|
/* The first descriptor is a video control interface descriptor. */
|
2021-09-20 14:55:56 +09:00
|
|
|
void const *cur = _find_desc_itf(beg, end, _desc_itfnum(beg), altnum);
|
2021-09-26 17:44:36 +09:00
|
|
|
TU_LOG2(" cur %ld\n", cur - beg);
|
2021-07-25 14:01:35 +09:00
|
|
|
TU_VERIFY(cur < end);
|
2021-07-19 21:28:05 +09:00
|
|
|
|
|
|
|
tusb_desc_vc_itf_t const *vc = (tusb_desc_vc_itf_t const *)cur;
|
2021-09-26 17:44:36 +09:00
|
|
|
TU_LOG2(" bInCollection %d\n", vc->ctl.bInCollection);
|
2021-07-19 21:28:05 +09:00
|
|
|
/* Support for up to 2 streaming interfaces only. */
|
2021-09-20 14:55:56 +09:00
|
|
|
TU_ASSERT(vc->ctl.bInCollection <= CFG_TUD_VIDEO_STREAMING);
|
2021-07-19 21:28:05 +09:00
|
|
|
|
2021-08-01 17:11:43 +09:00
|
|
|
/* Update to point the end of the video control interface descriptor. */
|
|
|
|
end = cur + vc->std.bLength + vc->ctl.wTotalLength;
|
2021-07-19 21:28:05 +09:00
|
|
|
/* Advance to the next descriptor after the class-specific VC interface header descriptor. */
|
|
|
|
cur += vc->std.bLength + vc->ctl.bLength;
|
2021-09-26 17:44:36 +09:00
|
|
|
TU_LOG2(" bNumEndpoints %d\n", vc->std.bNumEndpoints);
|
2021-07-19 21:28:05 +09:00
|
|
|
/* Open the notification endpoint if it exist. */
|
|
|
|
if (vc->std.bNumEndpoints) {
|
|
|
|
/* Support for 1 endpoint only. */
|
2021-07-25 14:01:35 +09:00
|
|
|
TU_VERIFY(1 == vc->std.bNumEndpoints);
|
2021-07-19 21:28:05 +09:00
|
|
|
/* Find the notification endpoint descriptor. */
|
2021-07-25 14:01:35 +09:00
|
|
|
cur = _find_desc(cur, end, TUSB_DESC_ENDPOINT);
|
|
|
|
TU_VERIFY(cur < end);
|
2021-07-19 21:28:05 +09:00
|
|
|
tusb_desc_endpoint_t const *notif = (tusb_desc_endpoint_t const *)cur;
|
|
|
|
/* Open the notification endpoint */
|
2021-07-25 14:01:35 +09:00
|
|
|
TU_ASSERT(usbd_edpt_open(rhport, notif));
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-09-20 14:55:56 +09:00
|
|
|
self->cur = (void const*)vc - beg;
|
2021-07-25 14:01:35 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
/** Set the alternate setting to own video streaming interface.
|
2021-07-25 14:01:35 +09:00
|
|
|
*
|
2021-09-26 21:52:56 +09:00
|
|
|
* @param[in,out] stm Streaming interface context.
|
2021-09-20 14:55:56 +09:00
|
|
|
* @param[in] altnum The target alternate setting number. */
|
2021-09-25 16:59:17 +09:00
|
|
|
static bool _open_vs_itf(uint8_t rhport, videod_streaming_interface_t *stm, uint_fast8_t altnum)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast8_t i;
|
2021-09-26 17:44:36 +09:00
|
|
|
TU_LOG2(" reopen VS %d\n", altnum);
|
2021-09-20 14:55:56 +09:00
|
|
|
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) {
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast16_t ofs_ep = stm->desc.ep[i];
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!ofs_ep) break;
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast8_t ep_adr = _desc_ep_addr(desc + ofs_ep);
|
2021-09-20 14:55:56 +09:00
|
|
|
usbd_edpt_close(rhport, ep_adr);
|
|
|
|
stm->desc.ep[i] = 0;
|
2021-09-26 17:44:36 +09:00
|
|
|
TU_LOG2(" close EP%02x\n", ep_adr);
|
2021-09-20 14:55:56 +09:00
|
|
|
}
|
2021-09-29 01:18:33 +09:00
|
|
|
/* clear transfer management information */
|
|
|
|
stm->buffer = NULL;
|
|
|
|
stm->bufsize = 0;
|
|
|
|
stm->offset = 0;
|
2021-09-20 14:55:56 +09:00
|
|
|
|
|
|
|
/* 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);
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast8_t numeps = ((tusb_desc_interface_t const *)cur)->bNumEndpoints;
|
2021-09-20 14:55:56 +09:00
|
|
|
TU_ASSERT(numeps <= TU_ARRAY_SIZE(stm->desc.ep));
|
|
|
|
stm->desc.cur = cur - desc; /* Save the offset of the new settings */
|
2021-09-29 01:18:33 +09:00
|
|
|
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;
|
2021-09-29 22:53:26 +09:00
|
|
|
tu_memclr(param, sizeof(*param));
|
2021-09-29 01:18:33 +09:00
|
|
|
return _update_streaming_parameters(stm, param);
|
|
|
|
}
|
2021-09-20 14:55:56 +09:00
|
|
|
/* 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);
|
2021-07-25 14:01:35 +09:00
|
|
|
TU_ASSERT(cur < end);
|
2021-09-29 01:18:33 +09:00
|
|
|
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) &&
|
|
|
|
(ep->wMaxPacketSize.size < 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));
|
2021-09-20 14:55:56 +09:00
|
|
|
stm->desc.ep[i] = cur - desc;
|
2021-09-26 17:44:36 +09:00
|
|
|
TU_LOG2(" open EP%02x\n", _desc_ep_addr(cur));
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
2021-09-25 13:08:00 +09:00
|
|
|
/* initialize payload header */
|
|
|
|
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
|
|
|
|
hdr->bHeaderLength = sizeof(*hdr);
|
|
|
|
hdr->bmHeaderInfo = 0;
|
|
|
|
|
2021-07-25 14:01:35 +09:00
|
|
|
return true;
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
|
|
|
|
2021-09-29 22:59:46 +09:00
|
|
|
/** Prepare the next packet payload. */
|
2021-09-26 17:44:36 +09:00
|
|
|
static uint_fast16_t _prepare_in_payload(videod_streaming_interface_t *stm)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-09-25 16:59:17 +09:00
|
|
|
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;
|
2021-09-25 13:08:00 +09:00
|
|
|
if (hdr_len + remaining < pkt_len) {
|
|
|
|
pkt_len = hdr_len + remaining;
|
|
|
|
}
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast16_t data_len = pkt_len - hdr_len;
|
2021-09-25 13:08:00 +09:00
|
|
|
memcpy(&stm->ep_buf[hdr_len], stm->buffer + stm->offset, data_len);
|
2021-09-20 14:55:56 +09:00
|
|
|
stm->offset += data_len;
|
|
|
|
remaining -= data_len;
|
|
|
|
if (!remaining) {
|
2021-09-25 13:08:00 +09:00
|
|
|
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
|
|
|
|
hdr->EndOfFrame = 1;
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
2021-09-25 16:59:17 +09:00
|
|
|
return hdr_len + data_len;
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Handle a standard request to the video control interface. */
|
2021-09-26 21:52:56 +09:00
|
|
|
static int handle_video_ctl_std_req(uint8_t rhport, uint8_t stage,
|
|
|
|
tusb_control_request_t const *request,
|
|
|
|
uint_fast8_t ctl_idx)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bRequest) {
|
2021-07-25 14:01:35 +09:00
|
|
|
case TUSB_REQ_GET_INTERFACE:
|
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
TU_VERIFY(1 == request->wLength, VIDEO_UNKNOWN);
|
2021-09-26 21:52:56 +09:00
|
|
|
tusb_desc_vc_itf_t const *vc = _get_desc_vc(&_videod_itf[ctl_idx]);
|
2021-07-31 19:35:22 +09:00
|
|
|
if (!vc) return VIDEO_UNKNOWN;
|
|
|
|
if (tud_control_xfer(rhport, request,
|
2021-08-15 18:07:53 +09:00
|
|
|
(void*)&vc->std.bAlternateSetting,
|
|
|
|
sizeof(vc->std.bAlternateSetting)))
|
2021-07-25 14:01:35 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-31 19:35:22 +09:00
|
|
|
case TUSB_REQ_SET_INTERFACE:
|
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(0 == request->wLength, VIDEO_UNKNOWN);
|
2021-09-26 21:52:56 +09:00
|
|
|
if (!_close_vc_itf(rhport, &_videod_itf[ctl_idx]))
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-09-26 21:52:56 +09:00
|
|
|
if (!_open_vc_itf(rhport, &_videod_itf[ctl_idx], request->wValue))
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-08-09 17:11:05 +09:00
|
|
|
tud_control_status(rhport, request);
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-25 14:01:35 +09:00
|
|
|
default: /* Unknown/Unsupported request */
|
|
|
|
TU_BREAKPOINT();
|
|
|
|
return VIDEO_INVALID_REQUEST;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
static int handle_video_ctl_cs_req(uint8_t rhport, uint8_t stage,
|
|
|
|
tusb_control_request_t const *request,
|
|
|
|
uint_fast8_t ctl_idx)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-09-26 21:52:56 +09:00
|
|
|
videod_interface_t *self = &_videod_itf[ctl_idx];
|
2021-07-25 14:01:35 +09:00
|
|
|
/* 4.2.1 Interface Control Request */
|
|
|
|
switch (TU_U16_HIGH(request->wValue)) {
|
|
|
|
case VIDEO_VC_CTL_VIDEO_POWER_MODE:
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bRequest) {
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_REQUEST_SET_CUR:
|
|
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
|
|
TU_VERIFY(1 == request->wLength, VIDEO_UNKNOWN);
|
|
|
|
if (!tud_control_xfer(rhport, request, &self->power_mode, sizeof(self->power_mode)))
|
|
|
|
return VIDEO_UNKNOWN;
|
|
|
|
} else if (stage == CONTROL_STAGE_ACK) {
|
2021-09-26 17:15:00 +09:00
|
|
|
if (tud_video_power_mode_cb)
|
2021-09-26 21:52:56 +09:00
|
|
|
return tud_video_power_mode_cb(ctl_idx, self->power_mode);
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
case VIDEO_REQUEST_GET_CUR:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(1 == request->wLength, VIDEO_UNKNOWN);
|
|
|
|
if (!tud_control_xfer(rhport, request, &self->power_mode, sizeof(self->power_mode)))
|
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
case VIDEO_REQUEST_GET_INFO:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(1 == request->wLength, VIDEO_UNKNOWN);
|
|
|
|
if (!tud_control_xfer(rhport, request, (uint8_t*)&_cap_get_set, sizeof(_cap_get_set)))
|
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
default: break;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
break;
|
|
|
|
case VIDEO_VC_CTL_REQUEST_ERROR_CODE:
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bRequest) {
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_REQUEST_GET_CUR:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)))
|
2021-08-15 18:07:53 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
case VIDEO_REQUEST_GET_INFO:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get, sizeof(_cap_get)))
|
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: break;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
/* Unknown/Unsupported request */
|
|
|
|
TU_BREAKPOINT();
|
|
|
|
return VIDEO_INVALID_REQUEST;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
static int handle_video_ctl_req(uint8_t rhport, uint8_t stage,
|
|
|
|
tusb_control_request_t const *request,
|
|
|
|
uint_fast8_t ctl_idx)
|
2021-07-19 21:28:05 +09:00
|
|
|
{
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast8_t entity_id;
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bmRequestType_bit.type) {
|
2021-07-25 14:01:35 +09:00
|
|
|
case TUSB_REQ_TYPE_STANDARD:
|
2021-09-26 21:52:56 +09:00
|
|
|
return handle_video_ctl_std_req(rhport, stage, request, ctl_idx);
|
2021-07-25 14:01:35 +09:00
|
|
|
case TUSB_REQ_TYPE_CLASS:
|
2021-07-31 19:35:22 +09:00
|
|
|
entity_id = TU_U16_HIGH(request->wIndex);
|
|
|
|
if (!entity_id) {
|
2021-09-26 21:52:56 +09:00
|
|
|
return handle_video_ctl_cs_req(rhport, stage, request, ctl_idx);
|
2021-07-25 14:01:35 +09:00
|
|
|
} else {
|
2021-09-26 21:52:56 +09:00
|
|
|
if (!_find_desc_entity(_get_desc_vc(&_videod_itf[ctl_idx]), entity_id))
|
2021-08-15 18:07:53 +09:00
|
|
|
return VIDEO_INVALID_REQUEST;
|
2021-07-25 14:01:35 +09:00
|
|
|
return VIDEO_INVALID_REQUEST;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return VIDEO_INVALID_REQUEST;
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 21:28:05 +09:00
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
static int handle_video_stm_std_req(uint8_t rhport, uint8_t stage,
|
|
|
|
tusb_control_request_t const *request,
|
|
|
|
uint_fast8_t stm_idx)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-09-26 21:52:56 +09:00
|
|
|
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bRequest) {
|
2021-07-25 14:01:35 +09:00
|
|
|
case TUSB_REQ_GET_INTERFACE:
|
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
TU_VERIFY(1 == request->wLength, VIDEO_UNKNOWN);
|
2021-09-20 14:55:56 +09:00
|
|
|
tusb_desc_vs_itf_t const *vs = _get_desc_vs(self);
|
2021-07-31 19:35:22 +09:00
|
|
|
if (!vs) return VIDEO_UNKNOWN;
|
|
|
|
if (tud_control_xfer(rhport, request,
|
2021-08-15 18:07:53 +09:00
|
|
|
(void*)&vs->std.bAlternateSetting,
|
|
|
|
sizeof(vs->std.bAlternateSetting)))
|
2021-07-25 14:01:35 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-31 19:35:22 +09:00
|
|
|
case TUSB_REQ_SET_INTERFACE:
|
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!_open_vs_itf(rhport, self, request->wValue))
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-08-09 17:11:05 +09:00
|
|
|
tud_control_status(rhport, request);
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-25 14:01:35 +09:00
|
|
|
default: /* Unknown/Unsupported request */
|
|
|
|
TU_BREAKPOINT();
|
|
|
|
return VIDEO_INVALID_REQUEST;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
|
|
|
|
tusb_control_request_t const *request,
|
|
|
|
uint_fast8_t stm_idx)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-08-15 18:07:53 +09:00
|
|
|
(void)rhport;
|
2021-09-26 21:52:56 +09:00
|
|
|
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
|
2021-07-25 14:01:35 +09:00
|
|
|
/* 4.2.1 Interface Control Request */
|
|
|
|
switch (TU_U16_HIGH(request->wValue)) {
|
2021-08-15 18:07:53 +09:00
|
|
|
case VIDEO_VS_CTL_STREAM_ERROR_CODE:
|
|
|
|
switch (request->bRequest) {
|
|
|
|
case VIDEO_REQUEST_GET_CUR:
|
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
/* TODO */
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)))
|
2021-08-15 18:07:53 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
case VIDEO_REQUEST_GET_INFO:
|
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get, sizeof(_cap_get)))
|
|
|
|
return VIDEO_UNKNOWN;
|
|
|
|
return VIDEO_NO_ERROR;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_VS_CTL_PROBE:
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bRequest) {
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_REQUEST_SET_CUR:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
|
|
TU_VERIFY(sizeof(video_probe_and_commit_control_t) == request->wLength, VIDEO_UNKNOWN);
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
|
2021-08-15 18:07:53 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
|
|
|
} else if (stage == CONTROL_STAGE_ACK) {
|
2021-09-29 01:18:33 +09:00
|
|
|
if (!_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf))
|
|
|
|
return VIDEO_INVALID_VALUE_WITHIN_RANGE;
|
2021-08-15 18:07:53 +09:00
|
|
|
}
|
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_REQUEST_GET_CUR:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(request->wLength, VIDEO_UNKNOWN);
|
2021-09-29 01:18:33 +09:00
|
|
|
if (tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
|
2021-08-15 18:07:53 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-09-29 01:18:33 +09:00
|
|
|
case VIDEO_REQUEST_GET_MIN: {
|
|
|
|
video_probe_and_commit_control_t tmp;
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(request->wLength, VIDEO_UNKNOWN);
|
2021-09-29 01:18:33 +09:00
|
|
|
tmp = *(video_probe_and_commit_control_t*)&self->ep_buf;
|
|
|
|
if (!_negotiate_streaming_parameters(self, false, &tmp))
|
|
|
|
return VIDEO_INVALID_VALUE_WITHIN_RANGE;
|
|
|
|
if (tud_control_xfer(rhport, request, &tmp,
|
2021-08-15 18:07:53 +09:00
|
|
|
sizeof(video_probe_and_commit_control_t)))
|
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-09-29 01:18:33 +09:00
|
|
|
}
|
|
|
|
case VIDEO_REQUEST_GET_MAX: {
|
|
|
|
video_probe_and_commit_control_t tmp;
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(request->wLength, VIDEO_UNKNOWN);
|
2021-09-29 01:18:33 +09:00
|
|
|
tmp = *(video_probe_and_commit_control_t*)&self->ep_buf;
|
|
|
|
if (!_negotiate_streaming_parameters(self, true, &tmp))
|
|
|
|
return VIDEO_INVALID_VALUE_WITHIN_RANGE;
|
|
|
|
if (tud_control_xfer(rhport, request, self->ep_buf,
|
2021-08-15 18:07:53 +09:00
|
|
|
sizeof(video_probe_and_commit_control_t)))
|
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-09-29 01:18:33 +09:00
|
|
|
}
|
2021-07-31 19:35:22 +09:00
|
|
|
case VIDEO_REQUEST_GET_RES:
|
|
|
|
return VIDEO_UNKNOWN;
|
|
|
|
case VIDEO_REQUEST_GET_DEF:
|
|
|
|
return VIDEO_UNKNOWN;
|
|
|
|
case VIDEO_REQUEST_GET_LEN:
|
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_REQUEST_GET_INFO:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(1 == request->wLength, VIDEO_UNKNOWN);
|
|
|
|
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get_set, sizeof(_cap_get_set)))
|
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
default: break;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
break;
|
|
|
|
case VIDEO_VS_CTL_COMMIT:
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bRequest) {
|
|
|
|
case VIDEO_REQUEST_SET_CUR:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage == CONTROL_STAGE_SETUP) {
|
|
|
|
TU_VERIFY(sizeof(video_probe_and_commit_control_t) == request->wLength, VIDEO_UNKNOWN);
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
|
2021-08-15 18:07:53 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
|
|
|
} else if (stage == CONTROL_STAGE_ACK) {
|
2021-09-29 01:18:33 +09:00
|
|
|
if (!_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf))
|
|
|
|
return VIDEO_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);
|
2021-08-15 18:07:53 +09:00
|
|
|
}
|
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_REQUEST_GET_CUR:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(request->wLength, VIDEO_UNKNOWN);
|
2021-09-29 01:18:33 +09:00
|
|
|
if (tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
|
2021-08-15 18:07:53 +09:00
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
case VIDEO_REQUEST_GET_INFO:
|
2021-08-15 18:07:53 +09:00
|
|
|
if (stage != CONTROL_STAGE_SETUP) return VIDEO_NO_ERROR;
|
|
|
|
TU_VERIFY(1 == request->wLength, VIDEO_UNKNOWN);
|
|
|
|
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get_set, sizeof(_cap_get_set)))
|
|
|
|
return VIDEO_NO_ERROR;
|
2021-07-31 19:35:22 +09:00
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-25 14:01:35 +09:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2021-08-15 18:07:53 +09:00
|
|
|
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;
|
2021-07-25 14:01:35 +09:00
|
|
|
default: break;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
2021-07-25 14:01:35 +09:00
|
|
|
/* Unknown/Unsupported request */
|
|
|
|
TU_BREAKPOINT();
|
|
|
|
return VIDEO_INVALID_REQUEST;
|
|
|
|
}
|
|
|
|
|
2021-09-26 21:52:56 +09:00
|
|
|
static int handle_video_stm_req(uint8_t rhport, uint8_t stage,
|
|
|
|
tusb_control_request_t const *request,
|
|
|
|
uint_fast8_t stm_idx)
|
2021-07-25 14:01:35 +09:00
|
|
|
{
|
2021-07-31 19:35:22 +09:00
|
|
|
switch (request->bmRequestType_bit.type) {
|
2021-07-25 14:01:35 +09:00
|
|
|
case TUSB_REQ_TYPE_STANDARD:
|
2021-09-26 21:52:56 +09:00
|
|
|
return handle_video_stm_std_req(rhport, stage, request, stm_idx);
|
2021-07-25 14:01:35 +09:00
|
|
|
case TUSB_REQ_TYPE_CLASS:
|
|
|
|
if (TU_U16_HIGH(request->wIndex))
|
|
|
|
return VIDEO_INVALID_REQUEST;
|
2021-09-26 21:52:56 +09:00
|
|
|
return handle_video_stm_cs_req(rhport, stage, request, stm_idx);
|
2021-07-25 14:01:35 +09:00
|
|
|
default:
|
|
|
|
return VIDEO_INVALID_REQUEST;
|
|
|
|
}
|
|
|
|
return VIDEO_UNKNOWN;
|
2021-07-19 21:28:05 +09:00
|
|
|
}
|
|
|
|
|
2021-07-17 18:51:33 +09:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// APPLICATION API
|
|
|
|
//--------------------------------------------------------------------+
|
2021-09-25 16:59:17 +09:00
|
|
|
|
2021-09-26 17:15:00 +09:00
|
|
|
bool tud_video_n_connected(uint_fast8_t ctl_idx)
|
2021-09-25 16:59:17 +09:00
|
|
|
{
|
2021-09-26 17:15:00 +09:00
|
|
|
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
|
|
|
|
videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, 0);
|
|
|
|
if (stm)
|
2021-09-20 14:55:56 +09:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-26 17:15:00 +09:00
|
|
|
bool tud_video_n_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
|
2021-09-20 14:55:56 +09:00
|
|
|
{
|
2021-09-26 17:15:00 +09:00
|
|
|
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;
|
|
|
|
}
|
2021-09-20 14:55:56 +09:00
|
|
|
|
2021-09-26 17:15:00 +09:00
|
|
|
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);
|
2021-09-25 16:59:17 +09:00
|
|
|
if (!buffer || !buffer)
|
|
|
|
return false;
|
2021-09-26 17:15:00 +09:00
|
|
|
videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx);
|
|
|
|
if (!stm || !stm->desc.ep[0] || stm->buffer)
|
2021-09-25 16:59:17 +09:00
|
|
|
return false;
|
2021-09-20 14:55:56 +09:00
|
|
|
|
2021-09-29 22:53:26 +09:00
|
|
|
/* Find EP address */
|
2021-09-25 16:59:17 +09:00
|
|
|
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));
|
2021-09-25 13:08:00 +09:00
|
|
|
/* update the packet header */
|
|
|
|
tusb_video_payload_header_t *hdr = (tusb_video_payload_header_t*)stm->ep_buf;
|
|
|
|
hdr->FrameID ^= 1;
|
|
|
|
hdr->EndOfFrame = 0;
|
2021-09-25 16:59:17 +09:00
|
|
|
/* update the packet data */
|
|
|
|
stm->buffer = (uint8_t*)buffer;
|
|
|
|
stm->bufsize = bufsize;
|
2021-09-26 17:44:36 +09:00
|
|
|
uint_fast16_t pkt_len = _prepare_in_payload(stm);
|
2021-09-25 16:59:17 +09:00
|
|
|
TU_ASSERT( usbd_edpt_xfer(0, ep_addr, stm->ep_buf, pkt_len), 0);
|
|
|
|
return true;
|
2021-09-20 14:55:56 +09:00
|
|
|
}
|
|
|
|
|
2021-07-17 18:51:33 +09:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// USBD Driver API
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
void videod_init(void)
|
|
|
|
{
|
2021-09-26 17:15:00 +09:00
|
|
|
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);
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void videod_reset(uint8_t rhport)
|
|
|
|
{
|
|
|
|
(void) rhport;
|
2021-09-26 17:15:00 +09:00
|
|
|
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);
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
|
|
|
|
{
|
2021-07-31 19:35:22 +09:00
|
|
|
TU_VERIFY((TUSB_CLASS_VIDEO == itf_desc->bInterfaceClass) &&
|
2021-08-15 18:07:53 +09:00
|
|
|
(VIDEO_SUBCLASS_CONTROL == itf_desc->bInterfaceSubClass) &&
|
|
|
|
(VIDEO_INT_PROTOCOL_CODE_15 == itf_desc->bInterfaceProtocol), 0);
|
2021-07-17 18:51:33 +09:00
|
|
|
|
|
|
|
/* Find available interface */
|
2021-07-19 21:28:05 +09:00
|
|
|
videod_interface_t *self = NULL;
|
2021-09-26 17:15:00 +09:00
|
|
|
uint_fast8_t ctl_idx;
|
|
|
|
for (ctl_idx = 0; ctl_idx < CFG_TUD_VIDEO; ++ctl_idx) {
|
|
|
|
if (_videod_itf[ctl_idx].beg)
|
2021-09-20 14:55:56 +09:00
|
|
|
continue;
|
2021-09-26 17:15:00 +09:00
|
|
|
self = &_videod_itf[ctl_idx];
|
2021-09-20 14:55:56 +09:00
|
|
|
break;
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
2021-09-26 17:15:00 +09:00
|
|
|
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO, 0);
|
2021-07-17 18:51:33 +09:00
|
|
|
|
2021-07-19 21:28:05 +09:00
|
|
|
void const *end = (void const*)itf_desc + max_len;
|
|
|
|
self->beg = itf_desc;
|
2021-07-25 14:01:35 +09:00
|
|
|
self->len = max_len;
|
2021-07-17 18:51:33 +09:00
|
|
|
/*------------- Video Control Interface -------------*/
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!_open_vc_itf(rhport, self, 0))
|
|
|
|
return 0;
|
2021-07-25 14:01:35 +09:00
|
|
|
tusb_desc_vc_itf_t const *vc = _get_desc_vc(self);
|
2021-09-26 17:15:00 +09:00
|
|
|
uint_fast8_t bInCollection = vc->ctl.bInCollection;
|
2021-08-15 18:07:53 +09:00
|
|
|
/* Find the end of the video interface descriptor */
|
2021-07-25 14:01:35 +09:00
|
|
|
void const *cur = _next_desc_itf(itf_desc, end);
|
2021-09-26 17:15:00 +09:00
|
|
|
for (uint_fast8_t stm_idx = 0; stm_idx < bInCollection; ++stm_idx) {
|
2021-09-20 14:55:56 +09:00
|
|
|
videod_streaming_interface_t *stm = NULL;
|
|
|
|
/* find free streaming interface handle */
|
2021-09-26 17:15:00 +09:00
|
|
|
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
|
2021-09-20 14:55:56 +09:00
|
|
|
if (_videod_streaming_itf[i].desc.beg)
|
|
|
|
continue;
|
|
|
|
stm = &_videod_streaming_itf[i];
|
2021-09-26 17:15:00 +09:00
|
|
|
self->stm[stm_idx] = i;
|
2021-09-20 14:55:56 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
TU_ASSERT(stm, 0);
|
2021-09-26 17:15:00 +09:00
|
|
|
stm->index_vc = ctl_idx;
|
|
|
|
stm->index_vs = stm_idx;
|
2021-09-20 14:55:56 +09:00
|
|
|
stm->desc.beg = (uintptr_t)cur - (uintptr_t)itf_desc;
|
2021-07-25 14:01:35 +09:00
|
|
|
cur = _next_desc_itf(cur, end);
|
2021-09-20 14:55:56 +09:00
|
|
|
stm->desc.end = (uintptr_t)cur - (uintptr_t)itf_desc;
|
2021-07-25 14:01:35 +09:00
|
|
|
}
|
|
|
|
self->len = (uintptr_t)cur - (uintptr_t)itf_desc;
|
2021-08-01 17:11:43 +09:00
|
|
|
return (uintptr_t)cur - (uintptr_t)itf_desc;
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2021-07-25 14:01:35 +09:00
|
|
|
int err;
|
2021-07-31 19:35:22 +09:00
|
|
|
if (request->bmRequestType_bit.recipient != TUSB_REQ_RCPT_INTERFACE) {
|
2021-07-19 21:28:05 +09:00
|
|
|
return false;
|
|
|
|
}
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast8_t itfnum = tu_u16_low(request->wIndex);
|
2021-09-20 14:55:56 +09:00
|
|
|
|
|
|
|
/* Identify which control interface to use */
|
2021-09-25 16:59:17 +09:00
|
|
|
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))
|
2021-07-19 21:28:05 +09:00
|
|
|
break;
|
2021-09-20 14:55:56 +09:00
|
|
|
}
|
2021-09-25 16:59:17 +09:00
|
|
|
if (itf < CFG_TUD_VIDEO) {
|
2021-09-20 14:55:56 +09:00
|
|
|
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 */
|
2021-09-25 16:59:17 +09:00
|
|
|
for (itf = 0; itf < CFG_TUD_VIDEO_STREAMING; ++itf) {
|
|
|
|
videod_streaming_interface_t *stm = &_videod_streaming_itf[itf];
|
|
|
|
if (!stm->desc.beg) continue;
|
2021-09-20 14:55:56 +09:00
|
|
|
void const *desc = _videod_itf[stm->index_vc].beg;
|
2021-09-25 16:59:17 +09:00
|
|
|
if (itfnum == _desc_itfnum(desc + stm->desc.beg))
|
2021-08-04 00:32:16 +09:00
|
|
|
break;
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
2021-09-25 16:59:17 +09:00
|
|
|
if (itf < CFG_TUD_VIDEO_STREAMING) {
|
2021-09-20 14:55:56 +09:00
|
|
|
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;
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
|
|
|
|
{
|
2021-09-25 16:59:17 +09:00
|
|
|
(void)result; (void)xferred_bytes;
|
2021-09-20 14:55:56 +09:00
|
|
|
|
|
|
|
/* find streaming handle */
|
2021-09-25 16:59:17 +09:00
|
|
|
uint_fast8_t itf;
|
2021-09-26 17:15:00 +09:00
|
|
|
videod_interface_t *ctl;
|
2021-09-25 13:08:00 +09:00
|
|
|
videod_streaming_interface_t *stm;
|
2021-09-25 16:59:17 +09:00
|
|
|
for (itf = 0; itf < CFG_TUD_VIDEO_STREAMING; ++itf) {
|
|
|
|
stm = &_videod_streaming_itf[itf];
|
|
|
|
uint_fast16_t const ep_ofs = stm->desc.ep[0];
|
2021-09-20 14:55:56 +09:00
|
|
|
if (!ep_ofs) continue;
|
2021-09-26 17:15:00 +09:00
|
|
|
ctl = &_videod_itf[stm->index_vc];
|
|
|
|
void const *desc = ctl->beg;
|
2021-09-20 14:55:56 +09:00
|
|
|
if (ep_addr == _desc_ep_addr(desc + ep_ofs))
|
|
|
|
break;
|
|
|
|
}
|
2021-09-25 16:59:17 +09:00
|
|
|
TU_ASSERT(itf < CFG_TUD_VIDEO_STREAMING);
|
2021-09-25 13:08:00 +09:00
|
|
|
if (stm->offset < stm->bufsize) {
|
2021-09-25 16:59:17 +09:00
|
|
|
/* Claim the endpoint */
|
|
|
|
TU_VERIFY( usbd_edpt_claim(rhport, ep_addr), 0);
|
2021-09-26 17:44:36 +09:00
|
|
|
uint_fast16_t pkt_len = _prepare_in_payload(stm);
|
2021-09-25 16:59:17 +09:00
|
|
|
TU_ASSERT( usbd_edpt_xfer(rhport, ep_addr, stm->ep_buf, pkt_len), 0);
|
2021-09-25 13:08:00 +09:00
|
|
|
} else {
|
|
|
|
stm->buffer = NULL;
|
|
|
|
stm->bufsize = 0;
|
|
|
|
stm->offset = 0;
|
|
|
|
if (tud_video_frame_xfer_complete_cb)
|
2021-09-26 17:15:00 +09:00
|
|
|
tud_video_frame_xfer_complete_cb(stm->index_vc, stm->index_vs);
|
2021-09-25 13:08:00 +09:00
|
|
|
}
|
2021-09-20 14:55:56 +09:00
|
|
|
return true;
|
2021-07-17 18:51:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|