nsyshid: Add Skylander Xbox 360 Portal support (#1550)

This commit is contained in:
gamerbross
2025-05-04 21:44:46 +02:00
committed by GitHub
parent 352a918494
commit 33d5c6d490
9 changed files with 864 additions and 5 deletions
+4
View File
@@ -476,6 +476,10 @@ add_library(CemuCafe
OS/libs/nsyshid/Infinity.h
OS/libs/nsyshid/Skylander.cpp
OS/libs/nsyshid/Skylander.h
OS/libs/nsyshid/SkylanderXbox360.cpp
OS/libs/nsyshid/SkylanderXbox360.h
OS/libs/nsyshid/g721/g721.cpp
OS/libs/nsyshid/g721/g721.h
OS/libs/nsyskbd/nsyskbd.cpp
OS/libs/nsyskbd/nsyskbd.h
OS/libs/nsysnet/nsysnet.cpp
+1 -1
View File
@@ -172,7 +172,7 @@ namespace nsyshid
std::shared_ptr<Device> FindDevice(std::function<bool(const std::shared_ptr<Device>&)> isWantedDevice);
bool FindDeviceById(uint16 vendorId, uint16 productId);
std::shared_ptr<Device> FindDeviceById(uint16 vendorId, uint16 productId);
bool IsDeviceWhitelisted(uint16 vendorId, uint16 productId);
@@ -4,6 +4,7 @@
#include "Infinity.h"
#include "Skylander.h"
#include "config/CemuConfig.h"
#include "SkylanderXbox360.h"
namespace nsyshid::backend::emulated
{
@@ -28,6 +29,13 @@ namespace nsyshid::backend::emulated
auto device = std::make_shared<SkylanderPortalDevice>();
AttachDevice(device);
}
else if (auto usb_portal = FindDeviceById(0x1430, 0x1F17))
{
cemuLog_logDebug(LogType::Force, "Attaching Xbox 360 Portal");
// Add Skylander Xbox 360 Portal
auto device = std::make_shared<SkylanderXbox360PortalLibusb>(usb_portal);
AttachDevice(device);
}
if (GetConfig().emulated_usb_devices.emulate_infinity_base && !FindDeviceById(0x0E6F, 0x0129))
{
cemuLog_logDebug(LogType::Force, "Attaching Emulated Base");
@@ -0,0 +1,160 @@
#include "SkylanderXbox360.h"
namespace nsyshid
{
SkylanderXbox360PortalLibusb::SkylanderXbox360PortalLibusb(std::shared_ptr<Device> usbPortal)
: Device(0x1430, 0x0150, 1, 2, 0)
{
m_IsOpened = false;
m_usbPortal = std::static_pointer_cast<backend::libusb::DeviceLibusb>(usbPortal);
}
bool SkylanderXbox360PortalLibusb::Open()
{
return m_usbPortal->Open();
}
void SkylanderXbox360PortalLibusb::Close()
{
return m_usbPortal->Close();
}
bool SkylanderXbox360PortalLibusb::IsOpened()
{
return m_usbPortal->IsOpened();
}
Device::ReadResult SkylanderXbox360PortalLibusb::Read(ReadMessage* message)
{
std::vector<uint8> xboxData(std::min<uint32>(32, message->length + sizeof(XBOX_DATA_HEADER)), 0);
memcpy(xboxData.data(), XBOX_DATA_HEADER, sizeof(XBOX_DATA_HEADER));
memcpy(xboxData.data() + sizeof(XBOX_DATA_HEADER), message->data, message->length - sizeof(XBOX_DATA_HEADER));
ReadMessage xboxMessage(xboxData.data(), xboxData.size(), 0);
auto result = m_usbPortal->Read(&xboxMessage);
memcpy(message->data, xboxData.data() + sizeof(XBOX_DATA_HEADER), message->length);
message->bytesRead = xboxMessage.bytesRead;
return result;
}
// Use InterruptTransfer instead of ControlTransfer
bool SkylanderXbox360PortalLibusb::SetReport(ReportMessage* message)
{
if (message->data[0] == 'M' && message->data[1] == 0x01) // Enables Speaker
g72x_init_state(&m_state);
std::vector<uint8> xboxData(message->length + sizeof(XBOX_DATA_HEADER), 0);
memcpy(xboxData.data(), XBOX_DATA_HEADER, sizeof(XBOX_DATA_HEADER));
memcpy(xboxData.data() + sizeof(XBOX_DATA_HEADER), message->data, message->length);
WriteMessage xboxMessage(xboxData.data(), xboxData.size(), 0);
auto result = m_usbPortal->Write(&xboxMessage);
memcpy(message->data, xboxData.data() + sizeof(XBOX_DATA_HEADER), message->length);
return result == WriteResult::Success;
}
Device::WriteResult SkylanderXbox360PortalLibusb::Write(WriteMessage* message)
{
std::vector<uint8> audioData(message->data, message->data + message->length);
std::vector<uint8_t> xboxAudioData;
for (size_t i = 0; i < audioData.size(); i += 4)
{
int16_t sample1 = (static_cast<int16_t>(audioData[i + 1]) << 8) | audioData[i];
int16_t sample2 = (static_cast<int16_t>(audioData[i + 3]) << 8) | audioData[i + 2];
uint8_t encoded1 = g721_encoder(sample1, &m_state) & 0x0F;
uint8_t encoded2 = g721_encoder(sample2, &m_state) & 0x0F;
xboxAudioData.push_back((encoded2 << 4) | encoded1);
}
std::vector<uint8> xboxData(xboxAudioData.size() + sizeof(XBOX_AUDIO_DATA_HEADER), 0);
memcpy(xboxData.data(), XBOX_AUDIO_DATA_HEADER, sizeof(XBOX_AUDIO_DATA_HEADER));
memcpy(xboxData.data() + sizeof(XBOX_AUDIO_DATA_HEADER), xboxAudioData.data(), xboxAudioData.size());
WriteMessage xboxMessage(xboxData.data(), xboxData.size(), 0);
auto result = m_usbPortal->Write(&xboxMessage);
memcpy(message->data, xboxData.data() + sizeof(XBOX_AUDIO_DATA_HEADER), xboxAudioData.size());
message->bytesWritten = xboxMessage.bytesWritten - sizeof(XBOX_AUDIO_DATA_HEADER);
return result;
}
bool SkylanderXbox360PortalLibusb::GetDescriptor(uint8 descType, uint8 descIndex, uint16 lang, uint8* output, uint32 outputMaxLength)
{
uint8 configurationDescriptor[0x29];
uint8* currentWritePtr;
// configuration descriptor
currentWritePtr = configurationDescriptor + 0;
*(uint8*)(currentWritePtr + 0) = 9; // bLength
*(uint8*)(currentWritePtr + 1) = 2; // bDescriptorType
*(uint16be*)(currentWritePtr + 2) = 0x0029; // wTotalLength
*(uint8*)(currentWritePtr + 4) = 1; // bNumInterfaces
*(uint8*)(currentWritePtr + 5) = 1; // bConfigurationValue
*(uint8*)(currentWritePtr + 6) = 0; // iConfiguration
*(uint8*)(currentWritePtr + 7) = 0x80; // bmAttributes
*(uint8*)(currentWritePtr + 8) = 0xFA; // MaxPower
currentWritePtr = currentWritePtr + 9;
// interface descriptor
*(uint8*)(currentWritePtr + 0) = 9; // bLength
*(uint8*)(currentWritePtr + 1) = 0x04; // bDescriptorType
*(uint8*)(currentWritePtr + 2) = 0; // bInterfaceNumber
*(uint8*)(currentWritePtr + 3) = 0; // bAlternateSetting
*(uint8*)(currentWritePtr + 4) = 2; // bNumEndpoints
*(uint8*)(currentWritePtr + 5) = 3; // bInterfaceClass
*(uint8*)(currentWritePtr + 6) = 0; // bInterfaceSubClass
*(uint8*)(currentWritePtr + 7) = 0; // bInterfaceProtocol
*(uint8*)(currentWritePtr + 8) = 0; // iInterface
currentWritePtr = currentWritePtr + 9;
// HID descriptor
*(uint8*)(currentWritePtr + 0) = 9; // bLength
*(uint8*)(currentWritePtr + 1) = 0x21; // bDescriptorType
*(uint16be*)(currentWritePtr + 2) = 0x0111; // bcdHID
*(uint8*)(currentWritePtr + 4) = 0x00; // bCountryCode
*(uint8*)(currentWritePtr + 5) = 0x01; // bNumDescriptors
*(uint8*)(currentWritePtr + 6) = 0x22; // bDescriptorType
*(uint16be*)(currentWritePtr + 7) = 0x001D; // wDescriptorLength
currentWritePtr = currentWritePtr + 9;
// endpoint descriptor 1
*(uint8*)(currentWritePtr + 0) = 7; // bLength
*(uint8*)(currentWritePtr + 1) = 0x05; // bDescriptorType
*(uint8*)(currentWritePtr + 2) = 0x81; // bEndpointAddress
*(uint8*)(currentWritePtr + 3) = 0x03; // bmAttributes
*(uint16be*)(currentWritePtr + 4) = 0x0040; // wMaxPacketSize
*(uint8*)(currentWritePtr + 6) = 0x01; // bInterval
currentWritePtr = currentWritePtr + 7;
// endpoint descriptor 2
*(uint8*)(currentWritePtr + 0) = 7; // bLength
*(uint8*)(currentWritePtr + 1) = 0x05; // bDescriptorType
*(uint8*)(currentWritePtr + 2) = 0x02; // bEndpointAddress
*(uint8*)(currentWritePtr + 3) = 0x03; // bmAttributes
*(uint16be*)(currentWritePtr + 4) = 0x0040; // wMaxPacketSize
*(uint8*)(currentWritePtr + 6) = 0x01; // bInterval
currentWritePtr = currentWritePtr + 7;
cemu_assert_debug((currentWritePtr - configurationDescriptor) == 0x29);
memcpy(output, configurationDescriptor,
std::min<uint32>(outputMaxLength, sizeof(configurationDescriptor)));
return true;
}
bool SkylanderXbox360PortalLibusb::SetIdle(uint8 ifIndex,
uint8 reportId,
uint8 duration)
{
return true;
}
bool SkylanderXbox360PortalLibusb::SetProtocol(uint8 ifIndex, uint8 protocol)
{
return true;
}
}
@@ -0,0 +1,46 @@
#pragma once
#include "nsyshid.h"
#include "BackendLibusb.h"
#include "g721/g721.h"
namespace nsyshid
{
class SkylanderXbox360PortalLibusb final : public Device {
public:
SkylanderXbox360PortalLibusb(std::shared_ptr<Device> usbPortal);
~SkylanderXbox360PortalLibusb() = default;
bool Open() override;
void Close() override;
bool IsOpened() override;
ReadResult Read(ReadMessage* message) override;
WriteResult Write(WriteMessage* message) override;
bool GetDescriptor(uint8 descType,
uint8 descIndex,
uint16 lang,
uint8* output,
uint32 outputMaxLength) override;
bool SetIdle(uint8 ifIndex,
uint8 reportId,
uint8 duration) override;
bool SetProtocol(uint8 ifIndex, uint8 protocol) override;
bool SetReport(ReportMessage* message) override;
private:
std::shared_ptr<backend::libusb::DeviceLibusb> m_usbPortal;
bool m_IsOpened;
struct g72x_state m_state;
};
constexpr uint8 XBOX_DATA_HEADER[] = { 0x0B, 0x14 };
constexpr uint8 XBOX_AUDIO_DATA_HEADER[] = { 0x0B, 0x17 };
} // namespace nsyshid
+2
View File
@@ -16,6 +16,8 @@ namespace nsyshid
m_devices.emplace_back(0x0e6f, 0x0241);
// skylanders portal
m_devices.emplace_back(0x1430, 0x0150);
// skylanders 360 portal
m_devices.emplace_back(0x1430, 0x1F17);
// disney infinity base
m_devices.emplace_back(0x0e6f, 0x0129);
}
File diff suppressed because it is too large Load Diff
+96
View File
@@ -0,0 +1,96 @@
/*
* This source code is a product of Sun Microsystems, Inc. and is provided
* for unrestricted use. Users may copy or modify this source code without
* charge.
*
* SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
* THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* Sun source code is provided with no support and without any obligation on
* the part of Sun Microsystems, Inc. to assist in its use, correction,
* modification or enhancement.
*
* SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
* OR ANY PART THEREOF.
*
* In no event will Sun Microsystems, Inc. be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Sun has been advised of the possibility of such damages.
*
* Sun Microsystems, Inc.
* 2550 Garcia Avenue
* Mountain View, California 94043
*/
/*
* g72x.h
*
* Header file for CCITT conversion routines.
*
*/
#ifndef _G72X_H
#define _G72X_H
/*
* The following is the definition of the state structure
* used by the G.721/G.723 encoder and decoder to preserve their internal
* state between successive calls. The meanings of the majority
* of the state structure fields are explained in detail in the
* CCITT Recommendation G.721. The field names are essentially identical
* to variable names in the bit level description of the coding algorithm
* included in this Recommendation.
*/
struct g72x_state {
long yl; /* Locked or steady state step size multiplier. */
short yu; /* Unlocked or non-steady state step size multiplier. */
short dms; /* Short term energy estimate. */
short dml; /* Long term energy estimate. */
short ap; /* Linear weighting coefficient of 'yl' and 'yu'. */
short a[2]; /* Coefficients of pole portion of prediction filter. */
short b[6]; /* Coefficients of zero portion of prediction filter. */
short pk[2]; /*
* Signs of previous two samples of a partially
* reconstructed signal.
*/
short dq[6]; /*
* Previous 6 samples of the quantized difference
* signal represented in an internal floating point
* format.
*/
short sr[2]; /*
* Previous 2 samples of the quantized difference
* signal represented in an internal floating point
* format.
*/
char td; /* delayed tone detect, new in 1988 version */
};
/* External function definitions. */
void g72x_init_state(struct g72x_state*);
int g721_encoder(int sample, struct g72x_state* state_ptr);
int g721_decoder(int code, struct g72x_state* state_ptr);
int quantize(int d, int y, short* table, int size);
int reconstruct(int, int, int);
void
update(int code_size,
int y,
int wi,
int fi,
int dq,
int sr,
int dqsez,
struct g72x_state* state_ptr);
int predictor_zero(struct g72x_state* state_ptr);
int predictor_pole(struct g72x_state* state_ptr);
int step_size(struct g72x_state* state_ptr);
#endif /* !_G72X_H */
+4 -4
View File
@@ -256,17 +256,17 @@ namespace nsyshid
device->m_productId);
}
bool FindDeviceById(uint16 vendorId, uint16 productId)
std::shared_ptr<Device> FindDeviceById(uint16 vendorId, uint16 productId)
{
std::lock_guard<std::recursive_mutex> lock(hidMutex);
for (const auto& device : deviceList)
{
if (device->m_vendorId == vendorId && device->m_productId == productId)
{
return true;
return device;
}
}
return false;
return nullptr;
}
void export_HIDAddClient(PPCInterpreter_t* hCPU)
@@ -876,7 +876,7 @@ namespace nsyshid
return nullptr;
}
bool Backend::FindDeviceById(uint16 vendorId, uint16 productId)
std::shared_ptr<Device> Backend::FindDeviceById(uint16 vendorId, uint16 productId)
{
return nsyshid::FindDeviceById(vendorId, productId);
}