minor code clean up without functionality changes

This commit is contained in:
hathach 2021-10-12 00:56:20 +07:00
parent 4d170614dc
commit 7afcb1e5d5
1 changed files with 264 additions and 243 deletions

View File

@ -1,9 +1,8 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2019 Ha Thach (tinyusb.org)
* Copyright (c) 2020 Reinhard Panhuber, Jerzy Kasenberg
* Copyright (c) 2021 Koji KITAYAMA * 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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -300,8 +299,7 @@ static bool _update_streaming_parameters(videod_streaming_interface_t const *stm
uint_fast8_t fmtnum = param->bFormatIndex; uint_fast8_t fmtnum = param->bFormatIndex;
TU_ASSERT(fmtnum <= vs->stm.bNumFormats); TU_ASSERT(fmtnum <= vs->stm.bNumFormats);
if (!fmtnum) { if (!fmtnum) {
if (1 < vs->stm.bNumFormats) if (1 < vs->stm.bNumFormats) return true; /* Need to negotiate all variables. */
return true; /* Need to negotiate all variables. */
fmtnum = 1; fmtnum = 1;
param->bFormatIndex = 1; param->bFormatIndex = 1;
} }
@ -326,8 +324,7 @@ static bool _update_streaming_parameters(videod_streaming_interface_t const *stm
uint_fast8_t frmnum = param->bFrameIndex; uint_fast8_t frmnum = param->bFrameIndex;
TU_ASSERT(frmnum <= fmt->bNumFrameDescriptors); TU_ASSERT(frmnum <= fmt->bNumFrameDescriptors);
if (!frmnum) { if (!frmnum) {
if (1 < fmt->bNumFrameDescriptors) if (1 < fmt->bNumFrameDescriptors) return true;
return true;
frmnum = 1; frmnum = 1;
param->bFrameIndex = 1; param->bFrameIndex = 1;
} }
@ -344,8 +341,9 @@ static bool _update_streaming_parameters(videod_streaming_interface_t const *stm
uint_fast32_t interval = param->dwFrameInterval; uint_fast32_t interval = param->dwFrameInterval;
if (!interval) { if (!interval) {
if ((1 < frm->bFrameIntervalType) || if ((1 < frm->bFrameIntervalType) ||
((0 == frm->bFrameIntervalType) && (frm->dwFrameInterval[1] != frm->dwFrameInterval[0]))) ((0 == frm->bFrameIntervalType) && (frm->dwFrameInterval[1] != frm->dwFrameInterval[0]))) {
return true; return true;
}
interval = frm->dwFrameInterval[0]; interval = frm->dwFrameInterval[0];
param->dwFrameInterval = interval; param->dwFrameInterval = interval;
} }
@ -580,28 +578,31 @@ static int handle_video_ctl_std_req(uint8_t rhport, uint8_t stage,
uint_fast8_t ctl_idx) uint_fast8_t ctl_idx)
{ {
switch (request->bRequest) { switch (request->bRequest) {
case TUSB_REQ_GET_INTERFACE: case TUSB_REQ_GET_INTERFACE:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE; 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(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
if (!vc) return VIDEO_ERROR_UNKNOWN; tusb_desc_vc_itf_t const *vc = _get_desc_vc(&_videod_itf[ctl_idx]);
if (tud_control_xfer(rhport, request, TU_VERIFY(vc, VIDEO_ERROR_UNKNOWN);
(void*)&vc->std.bAlternateSetting,
sizeof(vc->std.bAlternateSetting))) TU_VERIFY(tud_control_xfer(rhport, request, (void*)&vc->std.bAlternateSetting, sizeof(vc->std.bAlternateSetting)),
VIDEO_ERROR_UNKNOWN);
}
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
case TUSB_REQ_SET_INTERFACE: case TUSB_REQ_SET_INTERFACE:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE; if (stage == CONTROL_STAGE_SETUP)
TU_VERIFY(0 == request->wLength, VIDEO_ERROR_UNKNOWN); {
if (!_close_vc_itf(rhport, &_videod_itf[ctl_idx])) TU_VERIFY(0 == request->wLength, VIDEO_ERROR_UNKNOWN);
return VIDEO_ERROR_UNKNOWN; TU_VERIFY(_close_vc_itf(rhport, &_videod_itf[ctl_idx]), VIDEO_ERROR_UNKNOWN);
if (!_open_vc_itf(rhport, &_videod_itf[ctl_idx], request->wValue)) TU_VERIFY(_open_vc_itf(rhport, &_videod_itf[ctl_idx], request->wValue), VIDEO_ERROR_UNKNOWN);
return VIDEO_ERROR_UNKNOWN; tud_control_status(rhport, request);
tud_control_status(rhport, request); }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
default: /* Unknown/Unsupported request */
TU_BREAKPOINT(); default: /* Unknown/Unsupported request */
return VIDEO_ERROR_INVALID_REQUEST; TU_BREAKPOINT();
return VIDEO_ERROR_INVALID_REQUEST;
} }
} }
@ -610,52 +611,63 @@ static int handle_video_ctl_cs_req(uint8_t rhport, uint8_t stage,
uint_fast8_t ctl_idx) uint_fast8_t ctl_idx)
{ {
videod_interface_t *self = &_videod_itf[ctl_idx]; videod_interface_t *self = &_videod_itf[ctl_idx];
/* 4.2.1 Interface Control Request */ /* 4.2.1 Interface Control Request */
switch (TU_U16_HIGH(request->wValue)) { switch (TU_U16_HIGH(request->wValue)) {
case VIDEO_VC_CTL_VIDEO_POWER_MODE: case VIDEO_VC_CTL_VIDEO_POWER_MODE:
switch (request->bRequest) { switch (request->bRequest) {
case VIDEO_REQUEST_SET_CUR: case VIDEO_REQUEST_SET_CUR:
if (stage == CONTROL_STAGE_SETUP) { if (stage == CONTROL_STAGE_SETUP) {
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN); TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
if (!tud_control_xfer(rhport, request, &self->power_mode, sizeof(self->power_mode))) TU_VERIFY(tud_control_xfer(rhport, request, &self->power_mode, sizeof(self->power_mode)), VIDEO_ERROR_UNKNOWN);
return VIDEO_ERROR_UNKNOWN; } else if (stage == CONTROL_STAGE_ACK) {
} else if (stage == CONTROL_STAGE_ACK) { if (tud_video_power_mode_cb) return tud_video_power_mode_cb(ctl_idx, self->power_mode);
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*)&_cap_get_set, sizeof(_cap_get_set)), VIDEO_ERROR_UNKNOWN);
}
return VIDEO_ERROR_NONE;
default: break;
} }
return VIDEO_ERROR_NONE; break;
case VIDEO_REQUEST_GET_CUR:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE; case VIDEO_VC_CTL_REQUEST_ERROR_CODE:
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN); switch (request->bRequest) {
if (!tud_control_xfer(rhport, request, &self->power_mode, sizeof(self->power_mode))) case VIDEO_REQUEST_GET_CUR:
return VIDEO_ERROR_UNKNOWN; if (stage == CONTROL_STAGE_SETUP)
return VIDEO_ERROR_NONE; {
case VIDEO_REQUEST_GET_INFO: TU_VERIFY(tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)), VIDEO_ERROR_UNKNOWN);
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE; }
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN); return VIDEO_ERROR_NONE;
if (!tud_control_xfer(rhport, request, (uint8_t*)&_cap_get_set, sizeof(_cap_get_set)))
return VIDEO_ERROR_UNKNOWN; case VIDEO_REQUEST_GET_INFO:
return VIDEO_ERROR_NONE; if (stage == CONTROL_STAGE_SETUP)
{
TU_VERIFY(tud_control_xfer(rhport, request, (uint8_t*)&_cap_get, sizeof(_cap_get)), VIDEO_ERROR_UNKNOWN);
}
return VIDEO_ERROR_NONE;
default: break;
}
break;
default: break; default: break;
}
break;
case VIDEO_VC_CTL_REQUEST_ERROR_CODE:
switch (request->bRequest) {
case VIDEO_REQUEST_GET_CUR:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
if (!tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t)))
return VIDEO_ERROR_UNKNOWN;
return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_INFO:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get, sizeof(_cap_get)))
return VIDEO_ERROR_UNKNOWN;
return VIDEO_ERROR_NONE;
default: break;
}
break;
default: break;
} }
/* Unknown/Unsupported request */ /* Unknown/Unsupported request */
TU_BREAKPOINT(); TU_BREAKPOINT();
return VIDEO_ERROR_INVALID_REQUEST; return VIDEO_ERROR_INVALID_REQUEST;
@ -667,19 +679,20 @@ static int handle_video_ctl_req(uint8_t rhport, uint8_t stage,
{ {
uint_fast8_t entity_id; uint_fast8_t entity_id;
switch (request->bmRequestType_bit.type) { switch (request->bmRequestType_bit.type) {
case TUSB_REQ_TYPE_STANDARD: case TUSB_REQ_TYPE_STANDARD:
return handle_video_ctl_std_req(rhport, stage, request, ctl_idx); return handle_video_ctl_std_req(rhport, stage, request, ctl_idx);
case TUSB_REQ_TYPE_CLASS:
entity_id = TU_U16_HIGH(request->wIndex); case TUSB_REQ_TYPE_CLASS:
if (!entity_id) { entity_id = TU_U16_HIGH(request->wIndex);
return handle_video_ctl_cs_req(rhport, stage, request, ctl_idx); if (!entity_id) {
} else { return handle_video_ctl_cs_req(rhport, stage, request, ctl_idx);
if (!_find_desc_entity(_get_desc_vc(&_videod_itf[ctl_idx]), entity_id)) } else {
return VIDEO_ERROR_INVALID_REQUEST; 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; return VIDEO_ERROR_INVALID_REQUEST;
}
default:
return VIDEO_ERROR_INVALID_REQUEST;
} }
} }
@ -689,25 +702,27 @@ static int handle_video_stm_std_req(uint8_t rhport, uint8_t stage,
{ {
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx]; videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
switch (request->bRequest) { switch (request->bRequest) {
case TUSB_REQ_GET_INTERFACE: case TUSB_REQ_GET_INTERFACE:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE; 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(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
if (!vs) return VIDEO_ERROR_UNKNOWN; tusb_desc_vs_itf_t const *vs = _get_desc_vs(self);
if (tud_control_xfer(rhport, request, TU_VERIFY(vs, VIDEO_ERROR_UNKNOWN);
(void*)&vs->std.bAlternateSetting, TU_VERIFY(tud_control_xfer(rhport, request, (void*)&vs->std.bAlternateSetting, sizeof(vs->std.bAlternateSetting)), VIDEO_ERROR_UNKNOWN);
sizeof(vs->std.bAlternateSetting))) }
return VIDEO_ERROR_NONE; return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
case TUSB_REQ_SET_INTERFACE: case TUSB_REQ_SET_INTERFACE:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE; if (stage == CONTROL_STAGE_SETUP)
if (!_open_vs_itf(rhport, self, request->wValue)) {
return VIDEO_ERROR_UNKNOWN; TU_VERIFY(_open_vs_itf(rhport, self, request->wValue), VIDEO_ERROR_UNKNOWN);
tud_control_status(rhport, request); tud_control_status(rhport, request);
return VIDEO_ERROR_NONE; }
default: /* Unknown/Unsupported request */ return VIDEO_ERROR_NONE;
TU_BREAKPOINT();
return VIDEO_ERROR_INVALID_REQUEST; default: /* Unknown/Unsupported request */
TU_BREAKPOINT();
return VIDEO_ERROR_INVALID_REQUEST;
} }
} }
@ -717,121 +732,136 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
{ {
(void)rhport; (void)rhport;
videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx]; videod_streaming_interface_t *self = &_videod_streaming_itf[stm_idx];
/* 4.2.1 Interface Control Request */ /* 4.2.1 Interface Control Request */
switch (TU_U16_HIGH(request->wValue)) { switch (TU_U16_HIGH(request->wValue)) {
case VIDEO_VS_CTL_STREAM_ERROR_CODE: case VIDEO_VS_CTL_STREAM_ERROR_CODE:
switch (request->bRequest) { switch (request->bRequest) {
case VIDEO_REQUEST_GET_CUR: case VIDEO_REQUEST_GET_CUR:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE; 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*)&_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:
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, false, &tmp), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
TU_VERIFY(tud_control_xfer(rhport, request, &tmp, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
}
return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_MAX:
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, true, &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_RES: return VIDEO_ERROR_UNKNOWN;
case VIDEO_REQUEST_GET_DEF: return VIDEO_ERROR_UNKNOWN;
case VIDEO_REQUEST_GET_LEN: return VIDEO_ERROR_UNKNOWN;
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*)&_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_INFO:
if (stage == CONTROL_STAGE_SETUP)
{
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, (uint8_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 */ /* TODO */
if (!tud_control_xfer(rhport, request, &self->error_code, sizeof(uint8_t))) break;
return VIDEO_ERROR_UNKNOWN;
return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_INFO:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get, sizeof(_cap_get)))
return VIDEO_ERROR_UNKNOWN;
return VIDEO_ERROR_NONE;
default: break; 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);
if (!tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
return VIDEO_ERROR_UNKNOWN;
} else if (stage == CONTROL_STAGE_ACK) {
if (!_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf))
return VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE;
}
return VIDEO_ERROR_NONE;
case VIDEO_REQUEST_GET_CUR:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
if (tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
case VIDEO_REQUEST_GET_MIN: {
video_probe_and_commit_control_t tmp;
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
tmp = *(video_probe_and_commit_control_t*)&self->ep_buf;
if (!_negotiate_streaming_parameters(self, false, &tmp))
return VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE;
if (tud_control_xfer(rhport, request, &tmp,
sizeof(video_probe_and_commit_control_t)))
return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
}
case VIDEO_REQUEST_GET_MAX: {
video_probe_and_commit_control_t tmp;
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
tmp = *(video_probe_and_commit_control_t*)&self->ep_buf;
if (!_negotiate_streaming_parameters(self, true, &tmp))
return VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE;
if (tud_control_xfer(rhport, request, self->ep_buf,
sizeof(video_probe_and_commit_control_t)))
return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
}
case VIDEO_REQUEST_GET_RES:
return VIDEO_ERROR_UNKNOWN;
case VIDEO_REQUEST_GET_DEF:
return VIDEO_ERROR_UNKNOWN;
case VIDEO_REQUEST_GET_LEN:
return VIDEO_ERROR_UNKNOWN;
case VIDEO_REQUEST_GET_INFO:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get_set, sizeof(_cap_get_set)))
return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
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);
if (!tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
return VIDEO_ERROR_UNKNOWN;
} else if (stage == CONTROL_STAGE_ACK) {
if (!_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf))
return 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) return VIDEO_ERROR_NONE;
TU_VERIFY(request->wLength, VIDEO_ERROR_UNKNOWN);
if (tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)))
return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
case VIDEO_REQUEST_GET_INFO:
if (stage != CONTROL_STAGE_SETUP) return VIDEO_ERROR_NONE;
TU_VERIFY(1 == request->wLength, VIDEO_ERROR_UNKNOWN);
if (tud_control_xfer(rhport, request, (uint8_t*)&_cap_get_set, sizeof(_cap_get_set)))
return VIDEO_ERROR_NONE;
return VIDEO_ERROR_UNKNOWN;
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 */ /* Unknown/Unsupported request */
TU_BREAKPOINT(); TU_BREAKPOINT();
return VIDEO_ERROR_INVALID_REQUEST; return VIDEO_ERROR_INVALID_REQUEST;
@ -842,14 +872,14 @@ static int handle_video_stm_req(uint8_t rhport, uint8_t stage,
uint_fast8_t stm_idx) uint_fast8_t stm_idx)
{ {
switch (request->bmRequestType_bit.type) { switch (request->bmRequestType_bit.type) {
case TUSB_REQ_TYPE_STANDARD: case TUSB_REQ_TYPE_STANDARD:
return handle_video_stm_std_req(rhport, stage, request, stm_idx); return handle_video_stm_std_req(rhport, stage, request, stm_idx);
case TUSB_REQ_TYPE_CLASS:
if (TU_U16_HIGH(request->wIndex)) case TUSB_REQ_TYPE_CLASS:
return VIDEO_ERROR_INVALID_REQUEST; if (TU_U16_HIGH(request->wIndex)) return VIDEO_ERROR_INVALID_REQUEST;
return handle_video_stm_cs_req(rhport, stage, request, stm_idx); return handle_video_stm_cs_req(rhport, stage, request, stm_idx);
default:
return VIDEO_ERROR_INVALID_REQUEST; default: return VIDEO_ERROR_INVALID_REQUEST;
} }
return VIDEO_ERROR_UNKNOWN; return VIDEO_ERROR_UNKNOWN;
} }
@ -862,8 +892,7 @@ bool tud_video_n_connected(uint_fast8_t ctl_idx)
{ {
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO); TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, 0); videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, 0);
if (stm) if (stm) return true;
return true;
return false; return false;
} }
@ -872,8 +901,7 @@ bool tud_video_n_streaming(uint_fast8_t ctl_idx, uint_fast8_t stm_idx)
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO); TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING); TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING);
videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx); videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx);
if (!stm || !stm->desc.ep[0]) if (!stm || !stm->desc.ep[0]) return false;
return false;
return true; return true;
} }
@ -881,11 +909,9 @@ bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *bu
{ {
TU_ASSERT(ctl_idx < CFG_TUD_VIDEO); TU_ASSERT(ctl_idx < CFG_TUD_VIDEO);
TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING); TU_ASSERT(stm_idx < CFG_TUD_VIDEO_STREAMING);
if (!buffer || !buffer) if (!buffer || !buffer) return false;
return false;
videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx); videod_streaming_interface_t *stm = _get_instance_streaming(ctl_idx, stm_idx);
if (!stm || !stm->desc.ep[0] || stm->buffer) if (!stm || !stm->desc.ep[0] || stm->buffer) return false;
return false;
/* Find EP address */ /* Find EP address */
void const *desc = _videod_itf[stm->index_vc].beg; void const *desc = _videod_itf[stm->index_vc].beg;
@ -896,8 +922,7 @@ bool tud_video_n_frame_xfer(uint_fast8_t ctl_idx, uint_fast8_t stm_idx, void *bu
ep_addr = _desc_ep_addr(desc + ofs_ep); ep_addr = _desc_ep_addr(desc + ofs_ep);
break; break;
} }
if (!ep_addr) if (!ep_addr) return false;
return false;
TU_VERIFY( usbd_edpt_claim(0, ep_addr)); TU_VERIFY( usbd_edpt_claim(0, ep_addr));
/* update the packet header */ /* update the packet header */
@ -950,8 +975,7 @@ uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
videod_interface_t *self = NULL; videod_interface_t *self = NULL;
uint_fast8_t ctl_idx; uint_fast8_t ctl_idx;
for (ctl_idx = 0; ctl_idx < CFG_TUD_VIDEO; ++ctl_idx) { for (ctl_idx = 0; ctl_idx < CFG_TUD_VIDEO; ++ctl_idx) {
if (_videod_itf[ctl_idx].beg) if (_videod_itf[ctl_idx].beg) continue;
continue;
self = &_videod_itf[ctl_idx]; self = &_videod_itf[ctl_idx];
break; break;
} }
@ -961,8 +985,7 @@ uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
self->beg = itf_desc; self->beg = itf_desc;
self->len = max_len; self->len = max_len;
/*------------- Video Control Interface -------------*/ /*------------- Video Control Interface -------------*/
if (!_open_vc_itf(rhport, self, 0)) TU_VERIFY(_open_vc_itf(rhport, self, 0), 0);
return 0;
tusb_desc_vc_itf_t const *vc = _get_desc_vc(self); tusb_desc_vc_itf_t const *vc = _get_desc_vc(self);
uint_fast8_t bInCollection = vc->ctl.bInCollection; uint_fast8_t bInCollection = vc->ctl.bInCollection;
/* Find the end of the video interface descriptor */ /* Find the end of the video interface descriptor */
@ -971,8 +994,7 @@ uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
videod_streaming_interface_t *stm = NULL; videod_streaming_interface_t *stm = NULL;
/* find free streaming interface handle */ /* find free streaming interface handle */
for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) { for (uint_fast8_t i = 0; i < CFG_TUD_VIDEO_STREAMING; ++i) {
if (_videod_streaming_itf[i].desc.beg) if (_videod_streaming_itf[i].desc.beg) continue;
continue;
stm = &_videod_streaming_itf[i]; stm = &_videod_streaming_itf[i];
self->stm[stm_idx] = i; self->stm[stm_idx] = i;
break; break;
@ -994,9 +1016,7 @@ uint16_t videod_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uin
bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
{ {
int err; int err;
if (request->bmRequestType_bit.recipient != TUSB_REQ_RCPT_INTERFACE) { TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE);
return false;
}
uint_fast8_t itfnum = tu_u16_low(request->wIndex); uint_fast8_t itfnum = tu_u16_low(request->wIndex);
/* Identify which control interface to use */ /* Identify which control interface to use */
@ -1004,9 +1024,9 @@ bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_
for (itf = 0; itf < CFG_TUD_VIDEO; ++itf) { for (itf = 0; itf < CFG_TUD_VIDEO; ++itf) {
void const *desc = _videod_itf[itf].beg; void const *desc = _videod_itf[itf].beg;
if (!desc) continue; if (!desc) continue;
if (itfnum == _desc_itfnum(desc)) if (itfnum == _desc_itfnum(desc)) break;
break;
} }
if (itf < CFG_TUD_VIDEO) { if (itf < CFG_TUD_VIDEO) {
err = handle_video_ctl_req(rhport, stage, request, itf); err = handle_video_ctl_req(rhport, stage, request, itf);
_videod_itf[itf].error_code = (uint8_t)err; _videod_itf[itf].error_code = (uint8_t)err;
@ -1019,9 +1039,9 @@ bool videod_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_
videod_streaming_interface_t *stm = &_videod_streaming_itf[itf]; videod_streaming_interface_t *stm = &_videod_streaming_itf[itf];
if (!stm->desc.beg) continue; if (!stm->desc.beg) continue;
void const *desc = _videod_itf[stm->index_vc].beg; void const *desc = _videod_itf[stm->index_vc].beg;
if (itfnum == _desc_itfnum(desc + stm->desc.beg)) if (itfnum == _desc_itfnum(desc + stm->desc.beg)) break;
break;
} }
if (itf < CFG_TUD_VIDEO_STREAMING) { if (itf < CFG_TUD_VIDEO_STREAMING) {
err = handle_video_stm_req(rhport, stage, request, itf); err = handle_video_stm_req(rhport, stage, request, itf);
_videod_streaming_itf[itf].error_code = (uint8_t)err; _videod_streaming_itf[itf].error_code = (uint8_t)err;
@ -1045,9 +1065,9 @@ bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
if (!ep_ofs) continue; if (!ep_ofs) continue;
ctl = &_videod_itf[stm->index_vc]; ctl = &_videod_itf[stm->index_vc];
void const *desc = ctl->beg; void const *desc = ctl->beg;
if (ep_addr == _desc_ep_addr(desc + ep_ofs)) if (ep_addr == _desc_ep_addr(desc + ep_ofs)) break;
break;
} }
TU_ASSERT(itf < CFG_TUD_VIDEO_STREAMING); TU_ASSERT(itf < CFG_TUD_VIDEO_STREAMING);
if (stm->offset < stm->bufsize) { if (stm->offset < stm->bufsize) {
/* Claim the endpoint */ /* Claim the endpoint */
@ -1058,8 +1078,9 @@ bool videod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
stm->buffer = NULL; stm->buffer = NULL;
stm->bufsize = 0; stm->bufsize = 0;
stm->offset = 0; stm->offset = 0;
if (tud_video_frame_xfer_complete_cb) if (tud_video_frame_xfer_complete_cb) {
tud_video_frame_xfer_complete_cb(stm->index_vc, stm->index_vs); tud_video_frame_xfer_complete_cb(stm->index_vc, stm->index_vs);
}
} }
return true; return true;
} }