From ba7c6dab55df42a2bc356bd4c6020da23005b285 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Tue, 20 Jan 2009 08:42:07 +0100 Subject: [PATCH] Autodetect endpoints --- src/xbox360_controller.cpp | 69 +++++++++++++++++++++++++++++++++++++- src/xbox360_controller.hpp | 3 +- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/xbox360_controller.cpp b/src/xbox360_controller.cpp index 60bd8c6..d1680f1 100644 --- a/src/xbox360_controller.cpp +++ b/src/xbox360_controller.cpp @@ -27,11 +27,32 @@ #include "xbox360_controller.hpp" Xbox360Controller::Xbox360Controller(struct usb_device* dev, bool is_guitar) - : is_guitar(is_guitar), + : dev(dev), + is_guitar(is_guitar), endpoint_in(1), endpoint_out(2) { + find_endpoints(); + if (0) + { + std::cout << "EP(IN): " << endpoint_in << std::endl; + std::cout << "EP(OUT): " << endpoint_out << std::endl; + } + handle = usb_open(dev); + + if (0) + { + int err; + if ((err = usb_set_configuration(handle, 0)) < 0) + { + std::ostringstream out; + out << "Error set USB configuration: " << strerror(-err) << std::endl + << "Try to run 'rmmod xpad' and start xboxdrv again."; + throw std::runtime_error(out.str()); + } + } + if (!handle) { throw std::runtime_error("Error opening Xbox360 controller"); @@ -78,6 +99,52 @@ Xbox360Controller::~Xbox360Controller() usb_close(handle); } +void +Xbox360Controller::find_endpoints() +{ + bool print_debug = false; + + if (print_debug) + std::cout << "find_endpoints: numcfg: " << (int)dev->descriptor.bNumConfigurations << std::endl; + + for(int i = 0; i < dev->descriptor.bNumConfigurations; ++i) + { + if (print_debug) + std::cout << "Configuration: " << i << std::endl; + for(int j = 0; j < dev->config[i].bNumInterfaces; ++j) + { + if (print_debug) + std::cout << " Interface " << j << ":" << std::endl; + for(int k = 0; k < dev->config[i].interface[j].num_altsetting; ++k) + { + for(int l = 0; l < dev->config[i].interface[j].altsetting[k].bNumEndpoints; ++l) + { + if (dev->config[i].interface[j].altsetting[k].bInterfaceClass == USB_CLASS_VENDOR_SPEC && + dev->config[i].interface[j].altsetting[k].bInterfaceSubClass == 93 && + dev->config[i].interface[j].altsetting[k].bInterfaceProtocol == 1) + { + if (dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK) + { + endpoint_in = int(dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK); + } + else + { + endpoint_out = int(dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK); + } + } + + if (print_debug) + std::cout << " Endpoint: " + << int(dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_ADDRESS_MASK) + << ((dev->config[i].interface[j].altsetting[k].endpoint[l].bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? " (IN)" : " (OUT)") + << std::endl; + } + } + } + } + +} + void Xbox360Controller::set_rumble(uint8_t left, uint8_t right) { diff --git a/src/xbox360_controller.hpp b/src/xbox360_controller.hpp index d65faa4..0220565 100644 --- a/src/xbox360_controller.hpp +++ b/src/xbox360_controller.hpp @@ -28,14 +28,15 @@ struct XPadDevice; class Xbox360Controller : public XboxGenericController { private: - bool is_guitar; struct usb_device* dev; + bool is_guitar; XPadDevice* dev_type; struct usb_dev_handle* handle; int endpoint_in; int endpoint_out; + void find_endpoints(); public: Xbox360Controller(struct usb_device* dev, bool is_guitar); ~Xbox360Controller();