USB: Fix buffer copies in EyeToy

This commit is contained in:
Stenzek 2024-01-28 12:47:35 +10:00 committed by Connor McLaughlin
parent 41f14a8cf8
commit 609165e412
1 changed files with 30 additions and 33 deletions

View File

@ -286,11 +286,10 @@ namespace usb_eyetoy
switch (p->pid) switch (p->pid)
{ {
case USB_TOKEN_IN: case USB_TOKEN_IN:
uint8_t data[max_ep_size]; u8 data[max_ep_size];
pxAssert(p->buffer_size <= max_ep_size);
if (devep == 1) if (devep == 1)
{ {
memset(data, 0xff, sizeof(data));
if (s->frame_step == 0) if (s->frame_step == 0)
{ {
s->mpeg_frame_size = s->videodev->GetImage(s->mpeg_frame_data.get(), 640 * 480 * 3); s->mpeg_frame_size = s->videodev->GetImage(s->mpeg_frame_data.get(), 640 * 480 * 3);
@ -300,40 +299,39 @@ namespace usb_eyetoy
break; break;
} }
uint8_t header[] = {0xFF, 0xFF, 0xFF, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; u8 header[] = {0xFF, 0xFF, 0xFF, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
header[0x0A] = s->regs[OV519_RA0_FORMAT] == OV519_RA0_FORMAT_JPEG ? 0x03 : 0x01; header[0x0A] = s->regs[OV519_RA0_FORMAT] == OV519_RA0_FORMAT_JPEG ? 0x03 : 0x01;
memcpy(data, header, sizeof(header)); std::memcpy(data, header, sizeof(header));
const u32 data_pk = std::min(p->buffer_size - static_cast<u32>(sizeof(header)), s->mpeg_frame_size);
std::memcpy(data + sizeof(header), s->mpeg_frame_data.get(), data_pk);
usb_packet_copy(p, data, sizeof(header) + data_pk);
int data_pk = max_ep_size - sizeof(header);
memcpy(data + sizeof(header), s->mpeg_frame_data.get(), data_pk);
s->mpeg_frame_offset = data_pk; s->mpeg_frame_offset = data_pk;
s->frame_step++; s->frame_step++;
} }
else if (s->mpeg_frame_offset < s->mpeg_frame_size) else if (s->mpeg_frame_offset < s->mpeg_frame_size)
{ {
int data_pk = s->mpeg_frame_size - s->mpeg_frame_offset; const u32 data_pk = std::min(s->mpeg_frame_size - s->mpeg_frame_offset, p->buffer_size);
if (data_pk > max_ep_size) usb_packet_copy(p, s->mpeg_frame_data.get() + s->mpeg_frame_offset, data_pk);
data_pk = max_ep_size;
memcpy(data, s->mpeg_frame_data.get() + s->mpeg_frame_offset, data_pk);
s->mpeg_frame_offset += data_pk; s->mpeg_frame_offset += data_pk;
s->frame_step++; s->frame_step++;
} }
else else
{ {
uint8_t footer[] = {0xFF, 0xFF, 0xFF, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; u8 footer[] = {0xFF, 0xFF, 0xFF, 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
footer[0x0A] = s->regs[OV519_RA0_FORMAT] == OV519_RA0_FORMAT_JPEG ? 0x03 : 0x01; footer[0x0A] = s->regs[OV519_RA0_FORMAT] == OV519_RA0_FORMAT_JPEG ? 0x03 : 0x01;
memcpy(data, footer, sizeof(footer)); usb_packet_copy(p, footer, sizeof(footer));
s->frame_step = 0; s->frame_step = 0;
} }
usb_packet_copy(p, data, std::min(max_ep_size, p->buffer_size));
} }
else if (devep == 2) else if (devep == 2)
{ {
// get audio // get audio
//Console.Warning("get audio %d\n", len); //Console.Warning("get audio %d\n", len);
memset(data, 0, p->buffer_size); memset(data, 0, std::min(p->buffer_size, max_ep_size));
usb_packet_copy(p, data, p->buffer_size); usb_packet_copy(p, data, std::min(p->buffer_size, max_ep_size));
} }
break; break;
case USB_TOKEN_OUT: case USB_TOKEN_OUT:
@ -346,7 +344,6 @@ namespace usb_eyetoy
static void webcam_handle_data_ov511p(USBDevice* dev, USBPacket* p) static void webcam_handle_data_ov511p(USBDevice* dev, USBPacket* p)
{ {
EYETOYState* s = USB_CONTAINER_OF(dev, EYETOYState, dev); EYETOYState* s = USB_CONTAINER_OF(dev, EYETOYState, dev);
static const unsigned int max_ep_size = 960; // 961
uint8_t devep = p->ep->nr; uint8_t devep = p->ep->nr;
if (!s->hw_camera_running) if (!s->hw_camera_running)
@ -361,9 +358,6 @@ namespace usb_eyetoy
case USB_TOKEN_IN: case USB_TOKEN_IN:
if (devep == 1) if (devep == 1)
{ {
uint8_t data[max_ep_size];
memset(data, 0x00, sizeof(data));
if (s->frame_step == 0) if (s->frame_step == 0)
{ {
s->mpeg_frame_size = s->videodev->GetImage(s->mpeg_frame_data.get(), 640 * 480 * 3); s->mpeg_frame_size = s->videodev->GetImage(s->mpeg_frame_data.get(), 640 * 480 * 3);
@ -373,31 +367,34 @@ namespace usb_eyetoy
break; break;
} }
uint8_t header[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28}; // 28 <> 29 static const unsigned int max_ep_size = 960; // 961
memcpy(data, header, sizeof(header)); u8 data[max_ep_size];
pxAssert(p->buffer_size <= max_ep_size);
static constexpr const u8 header[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28}; // 28 <> 29
std::memcpy(data, header, sizeof(header));
const u32 data_pk = std::min(p->buffer_size - static_cast<u32>(sizeof(header)), s->mpeg_frame_size);
std::memcpy(data + sizeof(header), s->mpeg_frame_data.get(), data_pk);
usb_packet_copy(p, data, sizeof(header) + data_pk);
int data_pk = max_ep_size - sizeof(header);
memcpy(data + sizeof(header), s->mpeg_frame_data.get(), data_pk);
s->mpeg_frame_offset = data_pk; s->mpeg_frame_offset = data_pk;
s->frame_step++; s->frame_step++;
} }
else if (s->mpeg_frame_offset < s->mpeg_frame_size) else if (s->mpeg_frame_offset < s->mpeg_frame_size)
{ {
int data_pk = s->mpeg_frame_size - s->mpeg_frame_offset; const u32 data_pk = std::min(s->mpeg_frame_size - s->mpeg_frame_offset, p->buffer_size);
if (data_pk > max_ep_size) usb_packet_copy(p, s->mpeg_frame_data.get() + s->mpeg_frame_offset, data_pk);
data_pk = max_ep_size;
memcpy(data, s->mpeg_frame_data.get() + s->mpeg_frame_offset, data_pk);
s->mpeg_frame_offset += data_pk; s->mpeg_frame_offset += data_pk;
s->frame_step++; s->frame_step++;
} }
else else
{ {
uint8_t footer[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA8, 0x09, 0x07}; static constexpr const u8 footer[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA8, 0x09, 0x07};
memcpy(data, footer, sizeof(footer)); usb_packet_copy(p, const_cast<u8*>(footer), sizeof(footer));
s->frame_step = 0; s->frame_step = 0;
} }
usb_packet_copy(p, data, std::min(max_ep_size, p->buffer_size));
} }
break; break;
case USB_TOKEN_OUT: case USB_TOKEN_OUT: