first
This commit is contained in:
1380
rt-thread/components/drivers/usb/cherryusb/class/video/usb_video.h
Normal file
1380
rt-thread/components/drivers/usb/cherryusb/class/video/usb_video.h
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,788 @@
|
||||
/*
|
||||
* Copyright (c) 2022, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_video.h"
|
||||
|
||||
struct video_entity_info {
|
||||
uint8_t bDescriptorSubtype;
|
||||
uint8_t bEntityId;
|
||||
uint16_t wTerminalType;
|
||||
};
|
||||
|
||||
struct usbd_video_priv {
|
||||
struct video_probe_and_commit_controls probe;
|
||||
struct video_probe_and_commit_controls commit;
|
||||
uint8_t power_mode;
|
||||
uint8_t error_code;
|
||||
struct video_entity_info info[3];
|
||||
} g_usbd_video[CONFIG_USBDEV_MAX_BUS];
|
||||
|
||||
static int usbd_video_control_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
||||
{
|
||||
uint8_t control_selector = (uint8_t)(setup->wValue >> 8);
|
||||
|
||||
switch (control_selector) {
|
||||
case VIDEO_VC_VIDEO_POWER_MODE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_SET_CUR:
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
case VIDEO_VC_REQUEST_ERROR_CODE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
(*data)[0] = 0x06;
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usbd_video_control_unit_terminal_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
||||
{
|
||||
uint8_t entity_id = (uint8_t)(setup->wIndex >> 8);
|
||||
uint8_t control_selector = (uint8_t)(setup->wValue >> 8);
|
||||
|
||||
for (uint8_t i = 0; i < 3; i++) {
|
||||
struct video_entity_info *entity_info = &g_usbd_video[busid].info[i];
|
||||
if (entity_info->bEntityId == entity_id) {
|
||||
switch (entity_info->bDescriptorSubtype) {
|
||||
case VIDEO_VC_HEADER_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
case VIDEO_VC_INPUT_TERMINAL_DESCRIPTOR_SUBTYPE:
|
||||
if (entity_info->wTerminalType == VIDEO_ITT_CAMERA) {
|
||||
switch (control_selector) {
|
||||
case VIDEO_CT_AE_MODE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
(*data)[0] = 0x08;
|
||||
*len = 1;
|
||||
break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint32_t dwExposureTimeAbsolute = 2500;
|
||||
memcpy(*data, (uint8_t *)&dwExposureTimeAbsolute, 4);
|
||||
*len = 4;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint32_t dwExposureTimeAbsolute = 5; //0.0005sec
|
||||
memcpy(*data, (uint8_t *)&dwExposureTimeAbsolute, 4);
|
||||
*len = 4;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint32_t dwExposureTimeAbsolute = 2500; //0.2500sec
|
||||
memcpy(*data, (uint8_t *)&dwExposureTimeAbsolute, 4);
|
||||
*len = 4;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint32_t dwExposureTimeAbsolute = 5; //0.0005sec
|
||||
memcpy(*data, (uint8_t *)&dwExposureTimeAbsolute, 4);
|
||||
*len = 4;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint32_t dwExposureTimeAbsolute = 2500; //0.2500sec
|
||||
memcpy(*data, (uint8_t *)&dwExposureTimeAbsolute, 4);
|
||||
*len = 4;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_CT_FOCUS_ABSOLUTE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wFocusAbsolute = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wFocusAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wFocusAbsolute = 0;
|
||||
memcpy(*data, (uint8_t *)&wFocusAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wFocusAbsolute = 0x00ff;
|
||||
memcpy(*data, (uint8_t *)&wFocusAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wFocusAbsolute = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wFocusAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wFocusAbsolute = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wFocusAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_CT_ZOOM_ABSOLUTE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wObjectiveFocalLength = 0x0064;
|
||||
memcpy(*data, (uint8_t *)&wObjectiveFocalLength, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wObjectiveFocalLength = 0x0064;
|
||||
memcpy(*data, (uint8_t *)&wObjectiveFocalLength, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wObjectiveFocalLength = 0x00c8;
|
||||
memcpy(*data, (uint8_t *)&wObjectiveFocalLength, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wObjectiveFocalLength = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wObjectiveFocalLength, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wObjectiveFocalLength = 0x0064;
|
||||
memcpy(*data, (uint8_t *)&wObjectiveFocalLength, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_CT_ROLL_ABSOLUTE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wRollAbsolute = 0x0000;
|
||||
memcpy(*data, (uint8_t *)&wRollAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wRollAbsolute = 0x0000;
|
||||
memcpy(*data, (uint8_t *)&wRollAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wRollAbsolute = 0x00ff;
|
||||
memcpy(*data, (uint8_t *)&wRollAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wRollAbsolute = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wRollAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wRollAbsolute = 0x0000;
|
||||
memcpy(*data, (uint8_t *)&wRollAbsolute, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_CT_FOCUS_AUTO_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wFocusAuto = 0x0000;
|
||||
memcpy(*data, (uint8_t *)&wFocusAuto, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class control selector 0x%02x\r\n", control_selector);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
USB_LOG_WRN("Unhandled Video Class wTerminalType 0x%02x\r\n", entity_info->wTerminalType);
|
||||
return -2;
|
||||
}
|
||||
break;
|
||||
case VIDEO_VC_OUTPUT_TERMINAL_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
case VIDEO_VC_SELECTOR_UNIT_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
case VIDEO_VC_PROCESSING_UNIT_DESCRIPTOR_SUBTYPE:
|
||||
switch (control_selector) {
|
||||
case VIDEO_PU_BACKLIGHT_COMPENSATION_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wBacklightCompensation = 0x0004;
|
||||
memcpy(*data, (uint8_t *)&wBacklightCompensation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wBacklightCompensation = 0;
|
||||
memcpy(*data, (uint8_t *)&wBacklightCompensation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wBacklightCompensation = 8;
|
||||
memcpy(*data, (uint8_t *)&wBacklightCompensation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wBacklightCompensation = 1;
|
||||
memcpy(*data, (uint8_t *)&wBacklightCompensation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wBacklightCompensation = 4;
|
||||
memcpy(*data, (uint8_t *)&wBacklightCompensation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_BRIGHTNESS_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_SET_CUR: {
|
||||
//uint16_t wBrightness = (uint16_t)(*data)[1] << 8 | (uint16_t)(*data)[0];
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wBrightness = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wBrightness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wBrightness = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wBrightness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wBrightness = 0x00ff;
|
||||
memcpy(*data, (uint8_t *)&wBrightness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wBrightness = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wBrightness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wBrightness = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wBrightness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_CONTRAST_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wContrast = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wContrast, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wContrast = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wContrast, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wContrast = 0x00ff;
|
||||
memcpy(*data, (uint8_t *)&wContrast, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wContrast = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wContrast, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wContrast = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wContrast, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_HUE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wHue = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wHue, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wHue = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wHue, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wHue = 0x00ff;
|
||||
memcpy(*data, (uint8_t *)&wHue, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wHue = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wHue, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wHue = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wHue, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_SATURATION_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wSaturation = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wSaturation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wSaturation = 0x00ff;
|
||||
memcpy(*data, (uint8_t *)&wSaturation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wSaturation = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wSaturation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wSaturation = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wSaturation, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_SHARPNESS_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wSharpness = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wSharpness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wSharpness = 0x00ff;
|
||||
memcpy(*data, (uint8_t *)&wSharpness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wSharpness = 0x0001;
|
||||
memcpy(*data, (uint8_t *)&wSharpness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wSharpness = 0x0080;
|
||||
memcpy(*data, (uint8_t *)&wSharpness, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_GAIN_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wGain = 0;
|
||||
memcpy(*data, (uint8_t *)&wGain, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wGain = 255;
|
||||
memcpy(*data, (uint8_t *)&wGain, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wGain = 1;
|
||||
memcpy(*data, (uint8_t *)&wGain, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wGain = 255;
|
||||
memcpy(*data, (uint8_t *)&wGain, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_WHITE_BALANCE_TEMPERATURE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wWhiteBalance_Temprature = 417;
|
||||
memcpy(*data, (uint8_t *)&wWhiteBalance_Temprature, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MIN: {
|
||||
uint16_t wWhiteBalance_Temprature = 300;
|
||||
memcpy(*data, (uint8_t *)&wWhiteBalance_Temprature, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_MAX: {
|
||||
uint16_t wWhiteBalance_Temprature = 600;
|
||||
memcpy(*data, (uint8_t *)&wWhiteBalance_Temprature, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_RES: {
|
||||
uint16_t wWhiteBalance_Temprature = 1;
|
||||
memcpy(*data, (uint8_t *)&wWhiteBalance_Temprature, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03; //struct video_camera_capabilities
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_DEF: {
|
||||
uint16_t wWhiteBalance_Temprature = 417;
|
||||
memcpy(*data, (uint8_t *)&wWhiteBalance_Temprature, 2);
|
||||
*len = 2;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR: {
|
||||
uint16_t wWhiteBalance_Temprature_Auto = 1;
|
||||
memcpy(*data, (uint8_t *)&wWhiteBalance_Temprature_Auto, 1);
|
||||
*len = 1;
|
||||
} break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
g_usbd_video[busid].error_code = 0x06;
|
||||
USB_LOG_WRN("Unhandled Video Class control selector 0x%02x\r\n", control_selector);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_VC_EXTENSION_UNIT_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
case VIDEO_VC_ENCODING_UNIT_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usbd_video_stream_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
||||
{
|
||||
uint8_t control_selector = (uint8_t)(setup->wValue >> 8);
|
||||
|
||||
switch (control_selector) {
|
||||
case VIDEO_VS_PROBE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_SET_CUR:
|
||||
//memcpy((uint8_t *)&g_usbd_video[busid].probe, *data, setup->wLength);
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
memcpy(*data, (uint8_t *)&g_usbd_video[busid].probe, setup->wLength);
|
||||
*len = sizeof(struct video_probe_and_commit_controls);
|
||||
break;
|
||||
|
||||
case VIDEO_REQUEST_GET_MIN:
|
||||
case VIDEO_REQUEST_GET_MAX:
|
||||
case VIDEO_REQUEST_GET_RES:
|
||||
case VIDEO_REQUEST_GET_DEF:
|
||||
memcpy(*data, (uint8_t *)&g_usbd_video[busid].probe, setup->wLength);
|
||||
*len = sizeof(struct video_probe_and_commit_controls);
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_LEN:
|
||||
(*data)[0] = sizeof(struct video_probe_and_commit_controls);
|
||||
*len = 1;
|
||||
break;
|
||||
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03;
|
||||
*len = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_VS_COMMIT_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_SET_CUR:
|
||||
//memcpy((uint8_t *)&g_usbd_video[busid].commit, *data, setup->wLength);
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
memcpy(*data, (uint8_t *)&g_usbd_video[busid].commit, setup->wLength);
|
||||
*len = sizeof(struct video_probe_and_commit_controls);
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_MIN:
|
||||
case VIDEO_REQUEST_GET_MAX:
|
||||
case VIDEO_REQUEST_GET_RES:
|
||||
case VIDEO_REQUEST_GET_DEF:
|
||||
memcpy(*data, (uint8_t *)&g_usbd_video[busid].commit, setup->wLength);
|
||||
*len = sizeof(struct video_probe_and_commit_controls);
|
||||
break;
|
||||
|
||||
case VIDEO_REQUEST_GET_LEN:
|
||||
(*data)[0] = sizeof(struct video_probe_and_commit_controls);
|
||||
*len = 1;
|
||||
break;
|
||||
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x03;
|
||||
*len = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case VIDEO_VS_STREAM_ERROR_CODE_CONTROL:
|
||||
switch (setup->bRequest) {
|
||||
case VIDEO_REQUEST_GET_CUR:
|
||||
(*data)[0] = g_usbd_video[busid].error_code;
|
||||
*len = 1;
|
||||
break;
|
||||
case VIDEO_REQUEST_GET_INFO:
|
||||
(*data)[0] = 0x01;
|
||||
*len = 1;
|
||||
break;
|
||||
default:
|
||||
USB_LOG_WRN("Unhandled Video Class bRequest 0x%02x\r\n", setup->bRequest);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int video_class_interface_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
|
||||
{
|
||||
USB_LOG_DBG("Video Class request: "
|
||||
"bRequest 0x%02x\r\n",
|
||||
setup->bRequest);
|
||||
|
||||
uint8_t intf_num = (uint8_t)setup->wIndex;
|
||||
uint8_t entity_id = (uint8_t)(setup->wIndex >> 8);
|
||||
|
||||
if (intf_num == 0) { /* Video Control Interface */
|
||||
if (entity_id == 0) {
|
||||
return usbd_video_control_request_handler(busid, setup, data, len); /* Interface Control Requests */
|
||||
} else {
|
||||
return usbd_video_control_unit_terminal_request_handler(busid, setup, data, len); /* Unit and Terminal Requests */
|
||||
}
|
||||
} else if (intf_num == 1) { /* Video Stream Inteface */
|
||||
return usbd_video_stream_request_handler(busid, setup, data, len); /* Interface Stream Requests */
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void video_notify_handler(uint8_t busid, uint8_t event, void *arg)
|
||||
{
|
||||
switch (event) {
|
||||
case USBD_EVENT_RESET:
|
||||
g_usbd_video[busid].error_code = 0;
|
||||
g_usbd_video[busid].power_mode = 0;
|
||||
break;
|
||||
|
||||
case USBD_EVENT_SET_INTERFACE: {
|
||||
struct usb_interface_descriptor *intf = (struct usb_interface_descriptor *)arg;
|
||||
if (intf->bAlternateSetting == 1) {
|
||||
usbd_video_open(busid, intf->bInterfaceNumber);
|
||||
} else {
|
||||
usbd_video_close(busid, intf->bInterfaceNumber);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void usbd_video_probe_and_commit_controls_init(uint8_t busid, uint32_t dwFrameInterval, uint32_t dwMaxVideoFrameSize, uint32_t dwMaxPayloadTransferSize)
|
||||
{
|
||||
g_usbd_video[busid].probe.hintUnion.bmHint = 0x01;
|
||||
g_usbd_video[busid].probe.hintUnion1.bmHint = 0;
|
||||
g_usbd_video[busid].probe.bFormatIndex = 1;
|
||||
g_usbd_video[busid].probe.bFrameIndex = 1;
|
||||
g_usbd_video[busid].probe.dwFrameInterval = dwFrameInterval;
|
||||
g_usbd_video[busid].probe.wKeyFrameRate = 0;
|
||||
g_usbd_video[busid].probe.wPFrameRate = 0;
|
||||
g_usbd_video[busid].probe.wCompQuality = 0;
|
||||
g_usbd_video[busid].probe.wCompWindowSize = 0;
|
||||
g_usbd_video[busid].probe.wDelay = 0;
|
||||
g_usbd_video[busid].probe.dwMaxVideoFrameSize = dwMaxVideoFrameSize;
|
||||
g_usbd_video[busid].probe.dwMaxPayloadTransferSize = dwMaxPayloadTransferSize;
|
||||
g_usbd_video[busid].probe.dwClockFrequency = 0;
|
||||
g_usbd_video[busid].probe.bmFramingInfo = 0;
|
||||
g_usbd_video[busid].probe.bPreferedVersion = 0;
|
||||
g_usbd_video[busid].probe.bMinVersion = 0;
|
||||
g_usbd_video[busid].probe.bMaxVersion = 0;
|
||||
|
||||
g_usbd_video[busid].commit.hintUnion.bmHint = 0x01;
|
||||
g_usbd_video[busid].commit.hintUnion1.bmHint = 0;
|
||||
g_usbd_video[busid].commit.bFormatIndex = 1;
|
||||
g_usbd_video[busid].commit.bFrameIndex = 1;
|
||||
g_usbd_video[busid].commit.dwFrameInterval = dwFrameInterval;
|
||||
g_usbd_video[busid].commit.wKeyFrameRate = 0;
|
||||
g_usbd_video[busid].commit.wPFrameRate = 0;
|
||||
g_usbd_video[busid].commit.wCompQuality = 0;
|
||||
g_usbd_video[busid].commit.wCompWindowSize = 0;
|
||||
g_usbd_video[busid].commit.wDelay = 0;
|
||||
g_usbd_video[busid].commit.dwMaxVideoFrameSize = dwMaxVideoFrameSize;
|
||||
g_usbd_video[busid].commit.dwMaxPayloadTransferSize = dwMaxPayloadTransferSize;
|
||||
g_usbd_video[busid].commit.dwClockFrequency = 0;
|
||||
g_usbd_video[busid].commit.bmFramingInfo = 0;
|
||||
g_usbd_video[busid].commit.bPreferedVersion = 0;
|
||||
g_usbd_video[busid].commit.bMinVersion = 0;
|
||||
g_usbd_video[busid].commit.bMaxVersion = 0;
|
||||
}
|
||||
|
||||
struct usbd_interface *usbd_video_init_intf(uint8_t busid, struct usbd_interface *intf,
|
||||
uint32_t dwFrameInterval,
|
||||
uint32_t dwMaxVideoFrameSize,
|
||||
uint32_t dwMaxPayloadTransferSize)
|
||||
{
|
||||
intf->class_interface_handler = video_class_interface_request_handler;
|
||||
intf->class_endpoint_handler = NULL;
|
||||
intf->vendor_handler = NULL;
|
||||
intf->notify_handler = video_notify_handler;
|
||||
|
||||
g_usbd_video[busid].info[0].bDescriptorSubtype = VIDEO_VC_INPUT_TERMINAL_DESCRIPTOR_SUBTYPE;
|
||||
g_usbd_video[busid].info[0].bEntityId = 0x01;
|
||||
g_usbd_video[busid].info[0].wTerminalType = VIDEO_ITT_CAMERA;
|
||||
g_usbd_video[busid].info[1].bDescriptorSubtype = VIDEO_VC_OUTPUT_TERMINAL_DESCRIPTOR_SUBTYPE;
|
||||
g_usbd_video[busid].info[1].bEntityId = 0x03;
|
||||
g_usbd_video[busid].info[1].wTerminalType = 0x00;
|
||||
g_usbd_video[busid].info[2].bDescriptorSubtype = VIDEO_VC_PROCESSING_UNIT_DESCRIPTOR_SUBTYPE;
|
||||
g_usbd_video[busid].info[2].bEntityId = 0x02;
|
||||
g_usbd_video[busid].info[2].wTerminalType = 0x00;
|
||||
|
||||
usbd_video_probe_and_commit_controls_init(busid, dwFrameInterval, dwMaxVideoFrameSize, dwMaxPayloadTransferSize);
|
||||
return intf;
|
||||
}
|
||||
|
||||
uint32_t usbd_video_payload_fill(uint8_t busid, uint8_t *input, uint32_t input_len, uint8_t *output, uint32_t *out_len)
|
||||
{
|
||||
uint32_t packets;
|
||||
uint32_t last_packet_size;
|
||||
uint32_t picture_pos = 0;
|
||||
static uint8_t uvc_header[2] = { 0x02, 0x80 };
|
||||
|
||||
packets = (input_len + (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2) ) / (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2);
|
||||
last_packet_size = input_len - ((packets - 1) * (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2));
|
||||
|
||||
for (size_t i = 0; i < packets; i++) {
|
||||
output[g_usbd_video[busid].probe.dwMaxPayloadTransferSize * i] = uvc_header[0];
|
||||
output[g_usbd_video[busid].probe.dwMaxPayloadTransferSize * i + 1] = uvc_header[1];
|
||||
if (i == (packets - 1)) {
|
||||
memcpy(&output[2 + g_usbd_video[busid].probe.dwMaxPayloadTransferSize * i], &input[picture_pos], last_packet_size);
|
||||
output[g_usbd_video[busid].probe.dwMaxPayloadTransferSize * i + 1] |= (1 << 1);
|
||||
} else {
|
||||
memcpy(&output[2 + g_usbd_video[busid].probe.dwMaxPayloadTransferSize * i], &input[picture_pos], g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2);
|
||||
picture_pos += g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2;
|
||||
}
|
||||
}
|
||||
uvc_header[1] ^= 1;
|
||||
*out_len = (input_len + 2 * packets);
|
||||
return packets;
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2022, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#ifndef USBD_VIDEO_H
|
||||
#define USBD_VIDEO_H
|
||||
|
||||
#include "usb_video.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Init video interface driver */
|
||||
struct usbd_interface *usbd_video_init_intf(uint8_t busid, struct usbd_interface *intf,
|
||||
uint32_t dwFrameInterval,
|
||||
uint32_t dwMaxVideoFrameSize,
|
||||
uint32_t dwMaxPayloadTransferSize);
|
||||
|
||||
void usbd_video_open(uint8_t busid, uint8_t intf);
|
||||
void usbd_video_close(uint8_t busid, uint8_t intf);
|
||||
uint32_t usbd_video_payload_fill(uint8_t busid, uint8_t *input, uint32_t input_len, uint8_t *output, uint32_t *out_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* USBD_VIDEO_H */
|
@@ -0,0 +1,542 @@
|
||||
/*
|
||||
* Copyright (c) 2022, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#include "usbh_core.h"
|
||||
#include "usbh_video.h"
|
||||
|
||||
#undef USB_DBG_TAG
|
||||
#define USB_DBG_TAG "usbh_video"
|
||||
#include "usb_log.h"
|
||||
|
||||
#define DEV_FORMAT "/dev/video%d"
|
||||
|
||||
/* general descriptor field offsets */
|
||||
#define DESC_bLength 0 /** Length offset */
|
||||
#define DESC_bDescriptorType 1 /** Descriptor type offset */
|
||||
#define DESC_bDescriptorSubType 2 /** Descriptor subtype offset */
|
||||
#define DESC_bNumFormats 3 /** Descriptor numformat offset */
|
||||
#define DESC_bNumFrameDescriptors 4 /** Descriptor numframe offset */
|
||||
#define DESC_bFormatIndex 3 /** Descriptor format index offset */
|
||||
#define DESC_bFrameIndex 3 /** Descriptor frame index offset */
|
||||
|
||||
/* interface descriptor field offsets */
|
||||
#define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
|
||||
#define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
|
||||
|
||||
USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_video_buf[128];
|
||||
|
||||
static const char *format_type[] = { "uncompressed", "mjpeg" };
|
||||
|
||||
static struct usbh_video g_video_class[CONFIG_USBHOST_MAX_VIDEO_CLASS];
|
||||
static uint32_t g_devinuse = 0;
|
||||
|
||||
static struct usbh_video *usbh_video_class_alloc(void)
|
||||
{
|
||||
int devno;
|
||||
|
||||
for (devno = 0; devno < CONFIG_USBHOST_MAX_VIDEO_CLASS; devno++) {
|
||||
if ((g_devinuse & (1 << devno)) == 0) {
|
||||
g_devinuse |= (1 << devno);
|
||||
memset(&g_video_class[devno], 0, sizeof(struct usbh_video));
|
||||
g_video_class[devno].minor = devno;
|
||||
return &g_video_class[devno];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void usbh_video_class_free(struct usbh_video *video_class)
|
||||
{
|
||||
int devno = video_class->minor;
|
||||
|
||||
if (devno >= 0 && devno < 32) {
|
||||
g_devinuse &= ~(1 << devno);
|
||||
}
|
||||
memset(video_class, 0, sizeof(struct usbh_video));
|
||||
}
|
||||
|
||||
int usbh_video_get(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
struct usb_setup_packet *setup;
|
||||
int ret;
|
||||
uint8_t retry;
|
||||
|
||||
if (!video_class || !video_class->hport) {
|
||||
return -USB_ERR_INVAL;
|
||||
}
|
||||
setup = video_class->hport->setup;
|
||||
|
||||
setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
|
||||
setup->bRequest = request;
|
||||
setup->wValue = cs << 8;
|
||||
setup->wIndex = (entity_id << 8) | intf;
|
||||
setup->wLength = len;
|
||||
|
||||
retry = 0;
|
||||
while (1) {
|
||||
ret = usbh_control_transfer(video_class->hport, setup, g_video_buf);
|
||||
if (ret > 0) {
|
||||
break;
|
||||
}
|
||||
retry++;
|
||||
|
||||
if (retry == 3) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if (buf) {
|
||||
memcpy(buf, g_video_buf, len);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int usbh_video_set(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
struct usb_setup_packet *setup;
|
||||
int ret;
|
||||
|
||||
if (!video_class || !video_class->hport) {
|
||||
return -USB_ERR_INVAL;
|
||||
}
|
||||
setup = video_class->hport->setup;
|
||||
|
||||
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
|
||||
setup->bRequest = request;
|
||||
setup->wValue = cs << 8;
|
||||
setup->wIndex = (entity_id << 8) | intf;
|
||||
setup->wLength = len;
|
||||
|
||||
memcpy(g_video_buf, buf, len);
|
||||
|
||||
ret = usbh_control_transfer(video_class->hport, setup, g_video_buf);
|
||||
usb_osal_msleep(50);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int usbh_videostreaming_get_cur_probe(struct usbh_video *video_class)
|
||||
{
|
||||
return usbh_video_get(video_class, VIDEO_REQUEST_GET_CUR, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, (uint8_t *)&video_class->probe, 26);
|
||||
}
|
||||
|
||||
int usbh_videostreaming_set_cur_probe(struct usbh_video *video_class, uint8_t formatindex, uint8_t frameindex)
|
||||
{
|
||||
video_class->probe.bFormatIndex = formatindex;
|
||||
video_class->probe.bFrameIndex = frameindex;
|
||||
video_class->probe.dwMaxPayloadTransferSize = 0;
|
||||
video_class->probe.dwFrameInterval = 333333;
|
||||
return usbh_video_set(video_class, VIDEO_REQUEST_SET_CUR, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, (uint8_t *)&video_class->probe, 26);
|
||||
}
|
||||
|
||||
int usbh_videostreaming_set_cur_commit(struct usbh_video *video_class, uint8_t formatindex, uint8_t frameindex)
|
||||
{
|
||||
memcpy(&video_class->commit, &video_class->probe, sizeof(struct video_probe_and_commit_controls));
|
||||
video_class->commit.bFormatIndex = formatindex;
|
||||
video_class->commit.bFrameIndex = frameindex;
|
||||
video_class->commit.dwFrameInterval = 333333;
|
||||
return usbh_video_set(video_class, VIDEO_REQUEST_SET_CUR, video_class->data_intf, 0x00, VIDEO_VS_COMMIT_CONTROL, (uint8_t *)&video_class->commit, 26);
|
||||
}
|
||||
|
||||
int usbh_video_open(struct usbh_video *video_class,
|
||||
uint8_t format_type,
|
||||
uint16_t wWidth,
|
||||
uint16_t wHeight,
|
||||
uint8_t altsetting)
|
||||
{
|
||||
struct usb_setup_packet *setup;
|
||||
struct usb_endpoint_descriptor *ep_desc;
|
||||
uint8_t mult;
|
||||
uint16_t mps;
|
||||
int ret;
|
||||
bool found = false;
|
||||
uint8_t formatidx = 0;
|
||||
uint8_t frameidx = 0;
|
||||
uint8_t step;
|
||||
|
||||
if (!video_class || !video_class->hport) {
|
||||
return -USB_ERR_INVAL;
|
||||
}
|
||||
setup = video_class->hport->setup;
|
||||
|
||||
if (video_class->is_opened) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < video_class->num_of_formats; i++) {
|
||||
if (format_type == video_class->format[i].format_type) {
|
||||
formatidx = i + 1;
|
||||
for (uint8_t j = 0; j < video_class->format[i].num_of_frames; j++) {
|
||||
if ((wWidth == video_class->format[i].frame[j].wWidth) &&
|
||||
(wHeight == video_class->format[i].frame[j].wHeight)) {
|
||||
frameidx = j + 1;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found == false) {
|
||||
return -USB_ERR_NODEV;
|
||||
}
|
||||
|
||||
if (altsetting > (video_class->num_of_intf_altsettings - 1)) {
|
||||
return -USB_ERR_INVAL;
|
||||
}
|
||||
|
||||
/* Open video step:
|
||||
* Get CUR request (probe)
|
||||
* Set CUR request (probe)
|
||||
* Get CUR request (probe)
|
||||
* Get MAX request (probe)
|
||||
* Get MIN request (probe)
|
||||
* Get CUR request (probe)
|
||||
* Set CUR request (commit)
|
||||
*
|
||||
*/
|
||||
step = 0;
|
||||
ret = usbh_videostreaming_get_cur_probe(video_class);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 1;
|
||||
ret = usbh_videostreaming_set_cur_probe(video_class, formatidx, frameidx);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 2;
|
||||
ret = usbh_videostreaming_get_cur_probe(video_class);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 3;
|
||||
ret = usbh_video_get(video_class, VIDEO_REQUEST_GET_MAX, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, NULL, 26);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 4;
|
||||
ret = usbh_video_get(video_class, VIDEO_REQUEST_GET_MIN, video_class->data_intf, 0x00, VIDEO_VS_PROBE_CONTROL, NULL, 26);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 5;
|
||||
ret = usbh_videostreaming_set_cur_probe(video_class, formatidx, frameidx);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 6;
|
||||
ret = usbh_videostreaming_get_cur_probe(video_class);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 7;
|
||||
ret = usbh_videostreaming_set_cur_commit(video_class, formatidx, frameidx);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
step = 8;
|
||||
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
|
||||
setup->bRequest = USB_REQUEST_SET_INTERFACE;
|
||||
setup->wValue = altsetting;
|
||||
setup->wIndex = video_class->data_intf;
|
||||
setup->wLength = 0;
|
||||
|
||||
ret = usbh_control_transfer(video_class->hport, setup, NULL);
|
||||
if (ret < 0) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
ep_desc = &video_class->hport->config.intf[video_class->data_intf].altsetting[altsetting].ep[0].ep_desc;
|
||||
mult = (ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK) >> USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT;
|
||||
mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
|
||||
if (ep_desc->bEndpointAddress & 0x80) {
|
||||
video_class->isoin_mps = mps * (mult + 1);
|
||||
USBH_EP_INIT(video_class->isoin, ep_desc);
|
||||
} else {
|
||||
video_class->isoout_mps = mps * (mult + 1);
|
||||
USBH_EP_INIT(video_class->isoout, ep_desc);
|
||||
}
|
||||
|
||||
USB_LOG_INFO("Open video and select formatidx:%u, frameidx:%u, altsetting:%u\r\n", formatidx, frameidx, altsetting);
|
||||
video_class->is_opened = true;
|
||||
video_class->current_format = format_type;
|
||||
return ret;
|
||||
|
||||
errout:
|
||||
USB_LOG_ERR("Fail to open video in step %u\r\n", step);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int usbh_video_close(struct usbh_video *video_class)
|
||||
{
|
||||
struct usb_setup_packet *setup;
|
||||
int ret = 0;
|
||||
|
||||
if (!video_class || !video_class->hport) {
|
||||
return -USB_ERR_INVAL;
|
||||
}
|
||||
setup = video_class->hport->setup;
|
||||
|
||||
USB_LOG_INFO("Close video device\r\n");
|
||||
|
||||
video_class->is_opened = false;
|
||||
|
||||
if (video_class->isoin) {
|
||||
video_class->isoin = NULL;
|
||||
}
|
||||
|
||||
if (video_class->isoout) {
|
||||
video_class->isoout = NULL;
|
||||
}
|
||||
|
||||
setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
|
||||
setup->bRequest = USB_REQUEST_SET_INTERFACE;
|
||||
setup->wValue = 0;
|
||||
setup->wIndex = video_class->data_intf;
|
||||
setup->wLength = 0;
|
||||
|
||||
ret = usbh_control_transfer(video_class->hport, setup, NULL);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void usbh_video_list_info(struct usbh_video *video_class)
|
||||
{
|
||||
struct usb_endpoint_descriptor *ep_desc;
|
||||
uint8_t mult;
|
||||
uint16_t mps;
|
||||
|
||||
USB_LOG_INFO("============= Video device information ===================\r\n");
|
||||
USB_LOG_RAW("bcdVDC:%04x\r\n", video_class->bcdVDC);
|
||||
USB_LOG_RAW("Num of altsettings:%u\r\n", video_class->num_of_intf_altsettings);
|
||||
|
||||
for (uint8_t i = 0; i < video_class->num_of_intf_altsettings; i++) {
|
||||
if (i == 0) {
|
||||
USB_LOG_RAW("Ingore altsetting 0\r\n");
|
||||
continue;
|
||||
}
|
||||
ep_desc = &video_class->hport->config.intf[video_class->data_intf].altsetting[i].ep[0].ep_desc;
|
||||
|
||||
mult = (ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK) >> USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT;
|
||||
mps = ep_desc->wMaxPacketSize & USB_MAXPACKETSIZE_MASK;
|
||||
|
||||
USB_LOG_RAW("Altsetting:%u, Ep=%02x Attr=%02u Mps=%d Interval=%02u Mult=%02u\r\n",
|
||||
i,
|
||||
ep_desc->bEndpointAddress,
|
||||
ep_desc->bmAttributes,
|
||||
mps,
|
||||
ep_desc->bInterval,
|
||||
mult);
|
||||
}
|
||||
|
||||
USB_LOG_RAW("bNumFormats:%u\r\n", video_class->num_of_formats);
|
||||
for (uint8_t i = 0; i < video_class->num_of_formats; i++) {
|
||||
USB_LOG_RAW(" FormatIndex:%u\r\n", i + 1);
|
||||
USB_LOG_RAW(" FormatType:%s\r\n", format_type[video_class->format[i].format_type]);
|
||||
USB_LOG_RAW(" bNumFrames:%u\r\n", video_class->format[i].num_of_frames);
|
||||
USB_LOG_RAW(" Resolution:\r\n");
|
||||
for (uint8_t j = 0; j < video_class->format[i].num_of_frames; j++) {
|
||||
USB_LOG_RAW(" FrameIndex:%u\r\n", j + 1);
|
||||
USB_LOG_RAW(" wWidth: %d, wHeight: %d\r\n",
|
||||
video_class->format[i].frame[j].wWidth,
|
||||
video_class->format[i].frame[j].wHeight);
|
||||
}
|
||||
}
|
||||
|
||||
USB_LOG_INFO("============= Video device information ===================\r\n");
|
||||
}
|
||||
|
||||
static int usbh_video_ctrl_connect(struct usbh_hubport *hport, uint8_t intf)
|
||||
{
|
||||
int ret;
|
||||
uint8_t cur_iface = 0xff;
|
||||
// uint8_t cur_alt_setting = 0xff;
|
||||
uint8_t frame_index = 0xff;
|
||||
uint8_t format_index = 0xff;
|
||||
uint8_t num_of_frames = 0xff;
|
||||
uint8_t *p;
|
||||
|
||||
struct usbh_video *video_class = usbh_video_class_alloc();
|
||||
if (video_class == NULL) {
|
||||
USB_LOG_ERR("Fail to alloc video_class\r\n");
|
||||
return -USB_ERR_NOMEM;
|
||||
}
|
||||
|
||||
video_class->hport = hport;
|
||||
video_class->ctrl_intf = intf;
|
||||
video_class->data_intf = intf + 1;
|
||||
video_class->num_of_intf_altsettings = hport->config.intf[intf + 1].altsetting_num;
|
||||
|
||||
hport->config.intf[intf].priv = video_class;
|
||||
|
||||
ret = usbh_video_close(video_class);
|
||||
if (ret < 0) {
|
||||
USB_LOG_ERR("Fail to close video device\r\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
p = hport->raw_config_desc;
|
||||
while (p[DESC_bLength]) {
|
||||
switch (p[DESC_bDescriptorType]) {
|
||||
case USB_DESCRIPTOR_TYPE_INTERFACE:
|
||||
cur_iface = p[INTF_DESC_bInterfaceNumber];
|
||||
//cur_alt_setting = p[INTF_DESC_bAlternateSetting];
|
||||
break;
|
||||
case USB_DESCRIPTOR_TYPE_ENDPOINT:
|
||||
//ep_desc = (struct usb_endpoint_descriptor *)p;
|
||||
break;
|
||||
case VIDEO_CS_INTERFACE_DESCRIPTOR_TYPE:
|
||||
if (cur_iface == video_class->ctrl_intf) {
|
||||
switch (p[DESC_bDescriptorSubType]) {
|
||||
case VIDEO_VC_HEADER_DESCRIPTOR_SUBTYPE:
|
||||
video_class->bcdVDC = ((uint16_t)p[4] << 8) | (uint16_t)p[3];
|
||||
break;
|
||||
case VIDEO_VC_INPUT_TERMINAL_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
case VIDEO_VC_OUTPUT_TERMINAL_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
case VIDEO_VC_PROCESSING_UNIT_DESCRIPTOR_SUBTYPE:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (cur_iface == video_class->data_intf) {
|
||||
switch (p[DESC_bDescriptorSubType]) {
|
||||
case VIDEO_VS_INPUT_HEADER_DESCRIPTOR_SUBTYPE:
|
||||
video_class->num_of_formats = p[DESC_bNumFormats];
|
||||
break;
|
||||
case VIDEO_VS_FORMAT_UNCOMPRESSED_DESCRIPTOR_SUBTYPE:
|
||||
format_index = p[DESC_bFormatIndex];
|
||||
num_of_frames = p[DESC_bNumFrameDescriptors];
|
||||
|
||||
video_class->format[format_index - 1].num_of_frames = num_of_frames;
|
||||
video_class->format[format_index - 1].format_type = USBH_VIDEO_FORMAT_UNCOMPRESSED;
|
||||
break;
|
||||
case VIDEO_VS_FORMAT_MJPEG_DESCRIPTOR_SUBTYPE:
|
||||
format_index = p[DESC_bFormatIndex];
|
||||
num_of_frames = p[DESC_bNumFrameDescriptors];
|
||||
|
||||
video_class->format[format_index - 1].num_of_frames = num_of_frames;
|
||||
video_class->format[format_index - 1].format_type = USBH_VIDEO_FORMAT_MJPEG;
|
||||
break;
|
||||
case VIDEO_VS_FRAME_UNCOMPRESSED_DESCRIPTOR_SUBTYPE:
|
||||
frame_index = p[DESC_bFrameIndex];
|
||||
|
||||
video_class->format[format_index - 1].frame[frame_index - 1].wWidth = ((struct video_cs_if_vs_frame_uncompressed_descriptor *)p)->wWidth;
|
||||
video_class->format[format_index - 1].frame[frame_index - 1].wHeight = ((struct video_cs_if_vs_frame_uncompressed_descriptor *)p)->wHeight;
|
||||
break;
|
||||
case VIDEO_VS_FRAME_MJPEG_DESCRIPTOR_SUBTYPE:
|
||||
frame_index = p[DESC_bFrameIndex];
|
||||
|
||||
video_class->format[format_index - 1].frame[frame_index - 1].wWidth = ((struct video_cs_if_vs_frame_mjpeg_descriptor *)p)->wWidth;
|
||||
video_class->format[format_index - 1].frame[frame_index - 1].wHeight = ((struct video_cs_if_vs_frame_mjpeg_descriptor *)p)->wHeight;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
/* skip to next descriptor */
|
||||
p += p[DESC_bLength];
|
||||
}
|
||||
|
||||
usbh_video_list_info(video_class);
|
||||
|
||||
snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, video_class->minor);
|
||||
|
||||
USB_LOG_INFO("Register Video Class:%s\r\n", hport->config.intf[intf].devname);
|
||||
|
||||
usbh_video_run(video_class);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int usbh_video_ctrl_disconnect(struct usbh_hubport *hport, uint8_t intf)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
struct usbh_video *video_class = (struct usbh_video *)hport->config.intf[intf].priv;
|
||||
|
||||
if (video_class) {
|
||||
if (video_class->isoin) {
|
||||
}
|
||||
|
||||
if (video_class->isoout) {
|
||||
}
|
||||
|
||||
if (hport->config.intf[intf].devname[0] != '\0') {
|
||||
USB_LOG_INFO("Unregister Video Class:%s\r\n", hport->config.intf[intf].devname);
|
||||
usbh_video_stop(video_class);
|
||||
}
|
||||
|
||||
usbh_video_class_free(video_class);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int usbh_video_streaming_connect(struct usbh_hubport *hport, uint8_t intf)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usbh_video_streaming_disconnect(struct usbh_hubport *hport, uint8_t intf)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
__WEAK void usbh_video_run(struct usbh_video *video_class)
|
||||
{
|
||||
}
|
||||
|
||||
__WEAK void usbh_video_stop(struct usbh_video *video_class)
|
||||
{
|
||||
}
|
||||
|
||||
const struct usbh_class_driver video_ctrl_class_driver = {
|
||||
.driver_name = "video_ctrl",
|
||||
.connect = usbh_video_ctrl_connect,
|
||||
.disconnect = usbh_video_ctrl_disconnect
|
||||
};
|
||||
|
||||
const struct usbh_class_driver video_streaming_class_driver = {
|
||||
.driver_name = "video_streaming",
|
||||
.connect = usbh_video_streaming_connect,
|
||||
.disconnect = usbh_video_streaming_disconnect
|
||||
};
|
||||
|
||||
CLASS_INFO_DEFINE const struct usbh_class_info video_ctrl_class_info = {
|
||||
.match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
|
||||
.class = USB_DEVICE_CLASS_VIDEO,
|
||||
.subclass = VIDEO_SC_VIDEOCONTROL,
|
||||
.protocol = VIDEO_PC_PROTOCOL_UNDEFINED,
|
||||
.id_table = NULL,
|
||||
.class_driver = &video_ctrl_class_driver
|
||||
};
|
||||
|
||||
CLASS_INFO_DEFINE const struct usbh_class_info video_streaming_class_info = {
|
||||
.match_flags = USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL,
|
||||
.class = USB_DEVICE_CLASS_VIDEO,
|
||||
.subclass = VIDEO_SC_VIDEOSTREAMING,
|
||||
.protocol = VIDEO_PC_PROTOCOL_UNDEFINED,
|
||||
.id_table = NULL,
|
||||
.class_driver = &video_streaming_class_driver
|
||||
};
|
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (c) 2022, sakumisu
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#ifndef USBH_VIDEO_H
|
||||
#define USBH_VIDEO_H
|
||||
|
||||
#include "usb_video.h"
|
||||
|
||||
#define USBH_VIDEO_FORMAT_UNCOMPRESSED 0
|
||||
#define USBH_VIDEO_FORMAT_MJPEG 1
|
||||
|
||||
struct usbh_video_resolution {
|
||||
uint16_t wWidth;
|
||||
uint16_t wHeight;
|
||||
};
|
||||
|
||||
struct usbh_video_format {
|
||||
struct usbh_video_resolution frame[12];
|
||||
uint8_t format_type;
|
||||
uint8_t num_of_frames;
|
||||
};
|
||||
|
||||
struct usbh_videoframe {
|
||||
uint8_t *frame_buf;
|
||||
uint32_t frame_bufsize;
|
||||
uint32_t frame_format;
|
||||
uint32_t frame_size;
|
||||
};
|
||||
|
||||
struct usbh_videostreaming {
|
||||
struct usbh_videoframe *frame;
|
||||
uint32_t frame_format;
|
||||
uint32_t bufoffset;
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
};
|
||||
|
||||
struct usbh_video {
|
||||
struct usbh_hubport *hport;
|
||||
struct usb_endpoint_descriptor *isoin; /* ISO IN endpoint */
|
||||
struct usb_endpoint_descriptor *isoout; /* ISO OUT endpoint */
|
||||
|
||||
uint8_t ctrl_intf; /* interface number */
|
||||
uint8_t data_intf; /* interface number */
|
||||
uint8_t minor;
|
||||
struct video_probe_and_commit_controls probe;
|
||||
struct video_probe_and_commit_controls commit;
|
||||
uint16_t isoin_mps;
|
||||
uint16_t isoout_mps;
|
||||
bool is_opened;
|
||||
uint8_t current_format;
|
||||
uint16_t bcdVDC;
|
||||
uint8_t num_of_intf_altsettings;
|
||||
uint8_t num_of_formats;
|
||||
struct usbh_video_format format[3];
|
||||
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int usbh_video_get(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len);
|
||||
int usbh_video_set(struct usbh_video *video_class, uint8_t request, uint8_t intf, uint8_t entity_id, uint8_t cs, uint8_t *buf, uint16_t len);
|
||||
|
||||
int usbh_video_open(struct usbh_video *video_class,
|
||||
uint8_t format_type,
|
||||
uint16_t wWidth,
|
||||
uint16_t wHeight,
|
||||
uint8_t altsetting);
|
||||
int usbh_video_close(struct usbh_video *video_class);
|
||||
|
||||
void usbh_video_list_info(struct usbh_video *video_class);
|
||||
|
||||
void usbh_video_run(struct usbh_video *video_class);
|
||||
void usbh_video_stop(struct usbh_video *video_class);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* USBH_VIDEO_H */
|
Reference in New Issue
Block a user