Move to 1.0 libusb.

This commit is contained in:
Matthew Parlane 2012-12-24 08:31:50 +13:00
parent d6e46dd7e2
commit c626d5aae3
6 changed files with 139 additions and 101 deletions

Binary file not shown.

Binary file not shown.

View File

@ -20,24 +20,28 @@
#include "../HW/WII_IPC.h"
#include "WII_IPC_HLE.h"
#include "WII_IPC_HLE_Device_hid.h"
#include "lusb0_usb.h"
#include "libusb.h"
#include "errno.h"
#include <time.h>
CWII_IPC_HLE_Device_hid::CWII_IPC_HLE_Device_hid(u32 _DeviceID, const std::string& _rDeviceName)
: IWII_IPC_HLE_Device(_DeviceID, _rDeviceName)
{
usb_init(); /* initialize the library */
//usb_init(); /* initialize the library */
libusb_init(NULL);
}
CWII_IPC_HLE_Device_hid::~CWII_IPC_HLE_Device_hid()
{
for ( std::map<u32,usb_dev_handle*>::const_iterator iter = open_devices.begin(); iter != open_devices.end(); ++iter )
/*for ( std::map<u32,usb_dev_handle*>::const_iterator iter = open_devices.begin(); iter != open_devices.end(); ++iter )
{
usb_close(iter->second);
}
open_devices.clear();
*/
libusb_exit(NULL);
}
bool CWII_IPC_HLE_Device_hid::Open(u32 _CommandAddress, u32 _Mode)
@ -61,7 +65,11 @@ u32 CWII_IPC_HLE_Device_hid::Update()
{
u32 work_done = 0;
int ret = -4;
//timeval tv;
//libusb_handle_events_timeout_completed(NULL, &tv, NULL);
/*
std::list<_hidevent>::iterator ev = event_list.begin();
while (ev != event_list.end()) {
@ -89,7 +97,7 @@ u32 CWII_IPC_HLE_Device_hid::Update()
event_list.erase(ev++);
else
ev++;
}
}*/
return work_done;
}
@ -170,17 +178,17 @@ bool CWII_IPC_HLE_Device_hid::IOCtl(u32 _CommandAddress)
-4 Cant find device specified
*/
u32 dev_num = Memory::Read_U32(BufferIn+0x10);
u8 requesttype = Memory::Read_U8(BufferIn+0x14);
u8 requestType = Memory::Read_U8(BufferIn+0x14);
u8 request = Memory::Read_U8(BufferIn+0x15);
u16 value = Memory::Read_U16(BufferIn+0x16);
u16 index = Memory::Read_U16(BufferIn+0x18);
u16 size = Memory::Read_U16(BufferIn+0x1A);
u32 data = Memory::Read_U32(BufferIn+0x1C);
usb_find_busses(); /* find all busses */
usb_find_devices(); /* find all connected devices */
struct usb_dev_handle * dev_handle = GetDeviceByDevNum(dev_num);
DEBUG_LOG(WII_IPC_HID, "HID::IOCtl(Control)(%02X, %02X) = %d (BufferIn: (%08x, %i), BufferOut: (%08x, %i)",
requestType, request, ReturnValue, BufferIn, BufferInSize, BufferOut, BufferOutSize);
libusb_device_handle * dev_handle = GetDeviceByDevNum(dev_num);
if (dev_handle == NULL)
{
@ -188,76 +196,63 @@ bool CWII_IPC_HLE_Device_hid::IOCtl(u32 _CommandAddress)
break;
}
ReturnValue = usb_control_msg(dev_handle, requesttype, request,
/*ReturnValue = usb_control_msg(dev_handle, requesttype, request,
value, index, (char*)Memory::GetPointer(data), size,
0);
*/
ReturnValue = libusb_control_transfer (dev_handle, requestType, request, value, index, (unsigned char*)Memory::GetPointer(data), size, 0);
if(ReturnValue>=0)
{
ReturnValue += sizeof(usb_ctrl_setup);
ReturnValue += sizeof(libusb_control_setup);
}
DEBUG_LOG(WII_IPC_HID, "HID::IOCtl(Control)(%02X, %02X) = %d (BufferIn: (%08x, %i), BufferOut: (%08x, %i)",
requesttype, request, ReturnValue, BufferIn, BufferInSize, BufferOut, BufferOutSize);
requestType, request, ReturnValue, BufferIn, BufferInSize, BufferOut, BufferOutSize);
break;
}
case IOCTL_HID_INTERRUPT_OUT:
case IOCTL_HID_INTERRUPT_IN:
{
u32 dev_num = Memory::Read_U32(BufferIn+0x10);
u32 end_point = Memory::Read_U32(BufferIn+0x14);
u32 endpoint = Memory::Read_U32(BufferIn+0x14);
u32 length = Memory::Read_U32(BufferIn+0x18);
u32 data = Memory::Read_U32(BufferIn+0x1C);
DEBUG_LOG(WII_IPC_HID, "HID::IOCtl(Interrupt %s)(%d,%d,%p) = %d (BufferIn: (%08x, %i), BufferOut: (%08x, %i)",
Parameter == IOCTL_HID_INTERRUPT_IN ? "In" : "Out", endpoint, length, data, ReturnValue, BufferIn, BufferInSize, BufferOut, BufferOutSize);
int ret = 0;
void * context = NULL;
ReturnValue = -4;
struct usb_dev_handle * dev_handle = GetDeviceByDevNum(dev_num);
libusb_device_handle * dev_handle = GetDeviceByDevNum(dev_num);
if (dev_handle == NULL)
{
ReturnValue = -4;
goto int_in_end_print;
}
int transfered = 0;
usb_claim_interface(dev_handle,0);
ret = usb_interrupt_setup_async(dev_handle, &context, end_point);
if (ret< 0)
if(libusb_interrupt_transfer(dev_handle, endpoint, (unsigned char*)Memory::GetPointer(data), length, &transfered, 0 ) == 0)
{
ReturnValue = -4;
goto int_in_end_print;
}
ret = usb_submit_async(context, (char*)Memory::GetPointer(data), length);
if (ret< 0)
{
ReturnValue = -4;
goto int_in_end_print;
ReturnValue = transfered;
}
ret = usb_reap_async_nocancel(context, 0);
if (ret >= 0)
{
ReturnValue = ret;
}
else
{
/*
_hidevent ev;
ev.enq_address = _CommandAddress;
ev.type = Parameter;
ev.context = context;
event_list.push_back(ev);
return false;
}
*/
int_in_end_print:
DEBUG_LOG(WII_IPC_HID, "HID::IOCtl(Interrupt %s)(%d,%d,%p) = %d (BufferIn: (%08x, %i), BufferOut: (%08x, %i)",
Parameter == IOCTL_HID_INTERRUPT_IN ? "In" : "Out", end_point, length, data, ReturnValue, BufferIn, BufferInSize, BufferOut, BufferOutSize);
Parameter == IOCTL_HID_INTERRUPT_IN ? "In" : "Out", endpoint, length, data, ReturnValue, BufferIn, BufferInSize, BufferOut, BufferOutSize);
break;
}
@ -274,6 +269,7 @@ bool CWII_IPC_HLE_Device_hid::IOCtl(u32 _CommandAddress)
return true;
}
bool CWII_IPC_HLE_Device_hid::IOCtlV(u32 _CommandAddress)
{
@ -283,7 +279,8 @@ bool CWII_IPC_HLE_Device_hid::IOCtlV(u32 _CommandAddress)
switch (CommandBuffer.Parameter)
{
case 0x1337:
break;
default:
{
DEBUG_LOG(WII_IPC_HID, "%s - IOCtlV:", GetDeviceName().c_str());
@ -306,7 +303,7 @@ bool CWII_IPC_HLE_Device_hid::IOCtlV(u32 _CommandAddress)
void CWII_IPC_HLE_Device_hid::ConvertDeviceToWii(WiiHIDDeviceDescriptor *dest, struct usb_device_descriptor *src)
void CWII_IPC_HLE_Device_hid::ConvertDeviceToWii(WiiHIDDeviceDescriptor *dest, const struct libusb_device_descriptor *src)
{
memcpy(dest,src,sizeof(WiiHIDDeviceDescriptor));
dest->bcdUSB = Common::swap16(dest->bcdUSB);
@ -315,18 +312,18 @@ void CWII_IPC_HLE_Device_hid::ConvertDeviceToWii(WiiHIDDeviceDescriptor *dest, s
dest->bcdDevice = Common::swap16(dest->bcdDevice);
}
void CWII_IPC_HLE_Device_hid::ConvertConfigToWii(WiiHIDConfigDescriptor *dest, struct usb_config_descriptor *src)
void CWII_IPC_HLE_Device_hid::ConvertConfigToWii(WiiHIDConfigDescriptor *dest, const struct libusb_config_descriptor *src)
{
memcpy(dest,src,sizeof(WiiHIDConfigDescriptor));
dest->wTotalLength = Common::swap16(dest->wTotalLength);
}
void CWII_IPC_HLE_Device_hid::ConvertInterfaceToWii(WiiHIDInterfaceDescriptor *dest, struct usb_interface_descriptor *src)
void CWII_IPC_HLE_Device_hid::ConvertInterfaceToWii(WiiHIDInterfaceDescriptor *dest, const struct libusb_interface_descriptor *src)
{
memcpy(dest,src,sizeof(WiiHIDInterfaceDescriptor));
}
void CWII_IPC_HLE_Device_hid::ConvertEndpointToWii(WiiHIDEndpointDescriptor *dest, struct usb_endpoint_descriptor *src)
void CWII_IPC_HLE_Device_hid::ConvertEndpointToWii(WiiHIDEndpointDescriptor *dest, const struct libusb_endpoint_descriptor *src)
{
memcpy(dest,src,sizeof(WiiHIDEndpointDescriptor));
dest->wMaxPacketSize = Common::swap16(dest->wMaxPacketSize);
@ -334,35 +331,45 @@ void CWII_IPC_HLE_Device_hid::ConvertEndpointToWii(WiiHIDEndpointDescriptor *des
void CWII_IPC_HLE_Device_hid::FillOutDevices(u32 BufferOut, u32 BufferOutSize)
{
usb_find_busses(); /* find all busses */
usb_find_devices(); /* find all connected devices */
struct usb_bus *bus;
struct usb_device *dev;
int OffsetBuffer = BufferOut;
int OffsetStart = 0;
int c,ic,i,e; /* config, interface container, interface, endpoint */
for (bus = usb_get_busses(); bus; bus = bus->next)
int OffsetDevice = 0;
int d, c,ic,i,e; /* config, interface container, interface, endpoint */
libusb_device **list;
libusb_device *found = NULL;
ssize_t cnt = libusb_get_device_list(NULL, &list);
for (d = 0; d < cnt; d++)
{
for (dev = bus->devices; dev; dev = dev->next)
libusb_device *device = list[d];
struct libusb_device_descriptor desc;
int dRet = libusb_get_device_descriptor (device, &desc);
DEBUG_LOG(WII_IPC_HID, "Vendor: %d Product: %X Devnum: %X",desc.idVendor, desc.idProduct);
if (desc.idVendor != 0x21A4)
continue;
u32 devNum = 0;//(libusb_get_bus_number (device) << 8) | libusb_get_device_address (device);
DEBUG_LOG(WII_IPC_HID, "Found device with Vendor: %d Product: %d Devnum: %d, Error: %d",desc.idVendor, desc.idProduct, devNum, dRet);
OffsetStart = OffsetBuffer;
OffsetBuffer += 4; // skip length for now, fill at end
Memory::Write_U32(devNum, OffsetBuffer); //write device num
OffsetBuffer += 4;
WiiHIDDeviceDescriptor wii_device;
ConvertDeviceToWii(&wii_device, &desc);
Memory::WriteBigEData((const u8*)&wii_device, OffsetBuffer, Align(wii_device.bLength, 4));
OffsetBuffer += Align(wii_device.bLength, 4);
bool deviceValid = true;
for (c = 0; deviceValid && c < desc.bNumConfigurations; c++)
{
struct usb_device_descriptor *device = &dev->descriptor;
DEBUG_LOG(WII_IPC_HID, "Found device with Vendor: %d Product: %d Devnum: %d",device->idVendor, device->idProduct, dev->devnum);
OffsetStart = OffsetBuffer;
OffsetBuffer += 4; // skip length for now, fill at end
Memory::Write_U32(dev->devnum, OffsetBuffer); //write device num
OffsetBuffer += 4;
WiiHIDDeviceDescriptor wii_device;
ConvertDeviceToWii(&wii_device, device);
Memory::WriteBigEData((const u8*)&wii_device, OffsetBuffer, Align(wii_device.bLength, 4));
OffsetBuffer += Align(wii_device.bLength, 4);
for (c = 0; c < device->bNumConfigurations; c++)
struct libusb_config_descriptor *config = NULL;
int cRet = libusb_get_config_descriptor(device, c, &config);
if(cRet == 0)
{
struct usb_config_descriptor *config = &dev->config[c];
WiiHIDConfigDescriptor wii_config;
ConvertConfigToWii(&wii_config, config);
Memory::WriteBigEData((const u8*)&wii_config, OffsetBuffer, Align(wii_config.bLength, 4));
@ -370,10 +377,10 @@ void CWII_IPC_HLE_Device_hid::FillOutDevices(u32 BufferOut, u32 BufferOutSize)
for (ic = 0; ic < config->bNumInterfaces; ic++)
{
struct usb_interface *interfaceContainer = &config->interface[ic];
const struct libusb_interface *interfaceContainer = &config->interface[ic];
for (i = 0; i < interfaceContainer->num_altsetting; i++)
{
struct usb_interface_descriptor *interface = &interfaceContainer->altsetting[i];
const struct libusb_interface_descriptor *interface = &interfaceContainer->altsetting[i];
WiiHIDInterfaceDescriptor wii_interface;
ConvertInterfaceToWii(&wii_interface, interface);
@ -382,7 +389,7 @@ void CWII_IPC_HLE_Device_hid::FillOutDevices(u32 BufferOut, u32 BufferOutSize)
for (e = 0; e < interface->bNumEndpoints; e++)
{
struct usb_endpoint_descriptor *endpoint = &interface->endpoint[e];
const struct libusb_endpoint_descriptor *endpoint = &interface->endpoint[e];
WiiHIDEndpointDescriptor wii_endpoint;
ConvertEndpointToWii(&wii_endpoint, endpoint);
@ -392,42 +399,72 @@ void CWII_IPC_HLE_Device_hid::FillOutDevices(u32 BufferOut, u32 BufferOutSize)
} //endpoints
} // interfaces
} // interface containters
} // configs
libusb_free_config_descriptor(config);
config = NULL;
}
else
{
DEBUG_LOG(WII_IPC_HID, "Could not open the device %d", cRet);
deviceValid = false;
OffsetBuffer = OffsetStart;
}
} // configs
Memory::Write_U32(OffsetBuffer-OffsetStart, OffsetStart); // fill in length
Memory::Write_U32(OffsetBuffer-OffsetStart, OffsetStart); // fill in length
} // devices
} // buses
}
libusb_free_device_list(list, TRUE);
Memory::Write_U32(0xFFFFFFFF, OffsetBuffer); // no more devices
char file[0x50];
snprintf(file, 0x50, "device_list.bin");
FILE* test = fopen (file, "wb");
fwrite((char*)Memory::GetPointer(BufferOut), 1, (OffsetBuffer - BufferOut)+4, test);
fclose(test);
}
int CWII_IPC_HLE_Device_hid::Align(int num, int alignment)
{
return (num + (alignment-1)) & ~(alignment-1);
}
struct usb_dev_handle * CWII_IPC_HLE_Device_hid::GetDeviceByDevNum(u32 devNum)
libusb_device_handle * CWII_IPC_HLE_Device_hid::GetDeviceByDevNum(u32 devNum)
{
int i;
libusb_device **list;
libusb_device_handle *handle = NULL;
ssize_t cnt;
if (open_devices.find(devNum) != open_devices.end())
return open_devices[devNum];
cnt = libusb_get_device_list(NULL, &list);
if (cnt < 0)
return NULL;
for (i = 0; i < cnt; i++) {
libusb_device *device = list[i];
usb_dev_handle * device = NULL;
for (struct usb_bus *bus = usb_get_busses(); bus; bus = bus->next)
{
for (struct usb_device *dev = bus->devices; dev; dev = dev->next)
if (libusb_open(device, &handle))
break;
//struct libusb_device_descriptor desc;
u32 deviceID = (libusb_get_bus_number (device) << 8) | libusb_get_device_address (device);
if (deviceID == devNum)
open_devices[devNum] = handle;
else
{
if(dev->devnum == devNum){
open_devices[devNum] = device = usb_open(dev);
return device;
}
libusb_close(handle);
handle = NULL;
}
}
return device;
libusb_free_device_list(list, TRUE);
return handle;
}

View File

@ -115,15 +115,16 @@ private:
void CWII_IPC_HLE_Device_hid::FillOutDevices(u32 BufferOut, u32 BufferOutSize);
void ConvertDeviceToWii(WiiHIDDeviceDescriptor *dest, struct usb_device_descriptor *src);
void ConvertConfigToWii(WiiHIDConfigDescriptor *dest, struct usb_config_descriptor *src);
void ConvertInterfaceToWii(WiiHIDInterfaceDescriptor *dest, struct usb_interface_descriptor *src);
void ConvertEndpointToWii(WiiHIDEndpointDescriptor *dest, struct usb_endpoint_descriptor *src);
void ConvertDeviceToWii(WiiHIDDeviceDescriptor *dest, const struct libusb_device_descriptor *src);
void ConvertConfigToWii(WiiHIDConfigDescriptor *dest, const struct libusb_config_descriptor *src);
void ConvertInterfaceToWii(WiiHIDInterfaceDescriptor *dest, const struct libusb_interface_descriptor *src);
void ConvertEndpointToWii(WiiHIDEndpointDescriptor *dest, const struct libusb_endpoint_descriptor *src);
int Align(int num, int alignment);
struct usb_dev_handle * GetDeviceByDevNum(u32 devNum);
std::map<u32,usb_dev_handle*> open_devices;
struct libusb_device_handle * GetDeviceByDevNum(u32 devNum);
std::map<u32,libusb_device_handle*> open_devices;
std::map<std::string,int> device_identifiers;
typedef struct

View File

@ -8,7 +8,7 @@
<ItemDefinitionGroup>
<Link>
<AdditionalLibraryDirectories>..\..\..\Externals\SDL\$(PlatformName);..\..\..\Externals\GLew;..\..\..\Externals\Cg;..\..\..\Externals\portaudio\$(PlatformName)\$(ConfigurationName);..\..\..\Externals\libusb\$(PlatformName)</AdditionalLibraryDirectories>
<AdditionalDependencies>portaudio.lib;dsound.lib;dxerr.lib;iphlpapi.lib;winmm.lib;setupapi.lib;xinput.lib;vfw32.lib;cg.lib;cgGL.lib;opengl32.lib;glew32s.lib;glu32.lib;rpcrt4.lib;comctl32.lib;libusb.lib;hidapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>portaudio.lib;dsound.lib;dxerr.lib;iphlpapi.lib;winmm.lib;setupapi.lib;xinput.lib;vfw32.lib;cg.lib;cgGL.lib;opengl32.lib;glew32s.lib;glu32.lib;rpcrt4.lib;comctl32.lib;libusb.lib;libusb-1.0.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />

View File

@ -9,7 +9,7 @@
<ItemDefinitionGroup>
<Link>
<AdditionalLibraryDirectories>..\..\..\Externals\SDL\$(PlatformName);..\..\..\Externals\GLew;..\..\..\Externals\Cg64;..\..\..\Externals\portaudio\$(PlatformName)\$(ConfigurationName);..\..\..\Externals\libusb\$(PlatformName)</AdditionalLibraryDirectories>
<AdditionalDependencies>portaudio.lib;dsound.lib;dxerr.lib;iphlpapi.lib;winmm.lib;setupapi.lib;xinput.lib;vfw32.lib;cg.lib;cgGL.lib;opengl32.lib;glew64s.lib;glu32.lib;rpcrt4.lib;comctl32.lib;libusb.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>portaudio.lib;dsound.lib;dxerr.lib;iphlpapi.lib;winmm.lib;setupapi.lib;xinput.lib;vfw32.lib;cg.lib;cgGL.lib;opengl32.lib;glew64s.lib;glu32.lib;rpcrt4.lib;comctl32.lib;libusb.lib;libusb-1.0.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup />