122 lines
3.7 KiB
C++
122 lines
3.7 KiB
C++
#include "protocol/wire.h"
|
|
|
|
namespace nui::protocol::wire {
|
|
namespace {
|
|
|
|
constexpr uint16_t kVendorMicrosoft = 0x045e;
|
|
constexpr uint16_t kProductCamera = 0x02ae; // PID_NUI_CAMERA, freenect_internal.h:139
|
|
constexpr uint16_t kProductAudio = 0x02ad; // PID_NUI_AUDIO, freenect_internal.h:138
|
|
constexpr uint16_t kProductMotor = 0x02b0; // PID_NUI_MOTOR, freenect_internal.h:140
|
|
constexpr uint16_t kBcdDevice1414 = 0x010b; // 267, the 1414 discriminator, usb_libusb10.c:431
|
|
|
|
void append16(Bytes& bytes, uint16_t value) {
|
|
bytes.push_back(static_cast<uint8_t>(value));
|
|
bytes.push_back(static_cast<uint8_t>(value >> 8));
|
|
}
|
|
|
|
Bytes device_descriptor(uint16_t product, uint16_t bcd_device) {
|
|
Bytes bytes{18, 0x01};
|
|
append16(bytes, 0x0200);
|
|
bytes.insert(bytes.end(), {0xff, 0xff, 0xff, 64});
|
|
append16(bytes, kVendorMicrosoft);
|
|
append16(bytes, product);
|
|
append16(bytes, bcd_device);
|
|
bytes.insert(bytes.end(), {0, 0, 0, 1});
|
|
return bytes;
|
|
}
|
|
|
|
void append_interface(Bytes& bytes, uint8_t number, uint8_t endpoints) {
|
|
bytes.insert(bytes.end(),
|
|
{9, 0x04, number, 0, endpoints, 0xff, 0xff, 0xff, 0});
|
|
}
|
|
|
|
void append_iso_in_endpoint(Bytes& bytes, uint8_t address, uint16_t max_packet) {
|
|
bytes.insert(bytes.end(), {7, 0x05, address, 0x01});
|
|
append16(bytes, max_packet);
|
|
bytes.push_back(1);
|
|
}
|
|
|
|
Bytes config_descriptor(Bytes body, uint8_t interfaces) {
|
|
Bytes bytes{9, 0x02};
|
|
append16(bytes, static_cast<uint16_t>(9 + body.size()));
|
|
bytes.insert(bytes.end(), {interfaces, 1, 0, 0x80, 50});
|
|
bytes.insert(bytes.end(), body.begin(), body.end());
|
|
return bytes;
|
|
}
|
|
|
|
Bytes camera_config() {
|
|
Bytes body;
|
|
append_interface(body, 0, 2);
|
|
// Depth iso IN 0x82 and video iso IN 0x81 (cameras.c:871, :1021). The high-
|
|
// bandwidth iso packet size the host negotiates at runtime belongs to the
|
|
// attach shim (todo 9); the descriptor advertises the per-microframe buffer.
|
|
append_iso_in_endpoint(body, 0x82, 1760);
|
|
append_iso_in_endpoint(body, 0x81, 1920);
|
|
return config_descriptor(std::move(body), 1);
|
|
}
|
|
|
|
Bytes audio_config() {
|
|
// Two interfaces so the host reads the audio device as already firmware-
|
|
// loaded and skips the bootloader upload (usb_libusb10.c:554-555).
|
|
Bytes body;
|
|
append_interface(body, 0, 0);
|
|
append_interface(body, 1, 0);
|
|
return config_descriptor(std::move(body), 2);
|
|
}
|
|
|
|
Bytes motor_config() {
|
|
Bytes body;
|
|
append_interface(body, 0, 0);
|
|
return config_descriptor(std::move(body), 1);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::optional<Bytes> descriptor(UsbFunction function, uint16_t type_index) {
|
|
const uint8_t type = static_cast<uint8_t>(type_index >> 8);
|
|
const uint8_t index = static_cast<uint8_t>(type_index);
|
|
if (index != 0) {
|
|
return std::nullopt;
|
|
}
|
|
if (type == 0x01) {
|
|
switch (function) {
|
|
case UsbFunction::camera:
|
|
return device_descriptor(kProductCamera, kBcdDevice1414);
|
|
case UsbFunction::audio:
|
|
return device_descriptor(kProductAudio, kBcdDevice1414);
|
|
case UsbFunction::motor:
|
|
return device_descriptor(kProductMotor, kBcdDevice1414);
|
|
}
|
|
}
|
|
if (type == 0x02) {
|
|
switch (function) {
|
|
case UsbFunction::camera:
|
|
return camera_config();
|
|
case UsbFunction::audio:
|
|
return audio_config();
|
|
case UsbFunction::motor:
|
|
return motor_config();
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
bool endpoint_exists(UsbFunction function, uint16_t endpoint) {
|
|
return function == UsbFunction::camera &&
|
|
(endpoint == 0x81 || endpoint == 0x82);
|
|
}
|
|
|
|
uint8_t interface_count(UsbFunction function) {
|
|
switch (function) {
|
|
case UsbFunction::camera:
|
|
return 1;
|
|
case UsbFunction::audio:
|
|
return 2;
|
|
case UsbFunction::motor:
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
} // namespace nui::protocol::wire
|