3

I'm attempting to read the name, vendor ID and product ID for a USB joystick on Ubuntu (specifically I'm working with a wired Xbox 360 pad on Ubuntu 13.10 x64). I can read the name but when attempting to read the vendor and product IDs I get an EINVAL error. The code is as follows:

if (plugged[index])
{
    char name[32];
    std::snprintf(name, sizeof(name), "/dev/input/js%u", index);

    // Open the joystick's file descriptor (read-only and non-blocking)
    m_file = ::open(name, O_RDONLY | O_NONBLOCK);
    if (m_file >= 0)
    {
        // Retrieve the axes mapping
        ioctl(m_file, JSIOCGAXMAP, m_mapping);

        // Get the name
        char joyname[128];
        if (ioctl(m_file, JSIOCGNAME(128), joyname) < 0) {
            m_name = "Unknown Joystick";
        } else {
            m_name = joyname;
        }

        // Get vendor and product IDs
        input_id inpid;
        if (ioctl(m_file, EVIOCGID, &inpid) < 0) {
            if (errno == EBADF) printf("EBADF\n");
            if (errno == EFAULT) printf("EFAULT\n");
            if (errno == ENOTTY) printf("ENOTTY\n");
            if (errno == EINVAL) printf("EINVAL\n");
            m_manufacturerID = 0;
            m_productID = 0;
        } else {
            m_manufacturerID = inpid.vendor;
            m_productID = inpid.product;
        }

        // Reset the joystick state
        m_state = JoystickState();

        return true;
    }
    else
    {
        return false;
    }
}
else
{
    return false;
}

The snippet that reads vendor and product IDs is:

ioctl(m_file, EVIOCGID, &inpid)

According the man page for ioctl, EINVAL the request (EVIOCGID) or argp (inpid) is invalid.

How can I determine which is invalid?

2 Answers 2

9

After doing some more digging I found that the reason ioctl(m_file, EVIOCGID, &inpid) fails is that the device I'm opening is a joystick (/dev/input/js) and the EVIOCGID ioctl is for event devices (/dev/input/event) hence the failure. Unfortunately there is not JSIOCGID ioctl so I had to change tactics. Instead I'm using udev to access the joystick's vendor and product IDs. Here's the code I'm using:

// Use udev to look up the product and manufacturer IDs
struct udev *udev = udev_new();
if (udev)
{
    char sysname[32];
    std::snprintf(sysname, sizeof(sysname), "js%u", index);
    struct udev_device *dev = udev_device_new_from_subsystem_sysname(udev, "input", sysname);
    dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
    if (!dev)
    {
        err() << "Unable to find parent USB device" << std::endl;
    }

    std::stringstream ss;
    ss << std::hex << udev_device_get_sysattr_value(dev, "idVendor");
    ss >> m_manufacturerID;

    ss.clear();
    ss.str("");
    ss << std::hex << udev_device_get_sysattr_value(dev, "idProduct");
    ss >> m_productID;

    udev_device_unref(dev);
    udev_unref(udev);
}
else
{
    err() << "Cannot create udev" << std::endl;
}

With udev I've been able to consistently retrieve vendor and product IDs for USB joysticks on Ubuntu 13.10 x64.

UPDATE

I've tested this further with:

  • Linux Mint 16 x64
  • Manjaro x64
  • Fedora 20 x64

In all instances this code worked great.

0

Just a bit more cleaned up example with minor bug fix:

#include <iostream>
#include <libudev.h>
#include <sstream>

bool getJoystickInfo(int index, std::string& manufacturerID, std::string& productID, std::string& message)
{
    // Use udev to look up the product and manufacturer IDs
    struct udev *udev = udev_new();
    if (udev) {
        char sysname[32];
        std::snprintf(sysname, sizeof(sysname), "js%u", index);
        struct udev_device *dev = udev_device_new_from_subsystem_sysname(udev, "input", sysname);
        dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
        if (!dev)
        {
            message = "Unable to find parent USB device";
            return false;
        }

        std::stringstream ss;
        ss << std::hex << udev_device_get_sysattr_value(dev, "idVendor");
        ss >> manufacturerID;

        ss.clear();
        ss.str("");
        ss << std::hex << udev_device_get_sysattr_value(dev, "idProduct");
        ss >> productID;

        udev_device_unref(dev);
    }
    else
    {
        message = "Cannot create udev";
        return false;
    }
    udev_unref(udev);
    return true;
}

int main() {
    std::string manufacturerID, productID, message;
    if (getJoystickInfo(1, manufacturerID, productID, message))
        std::cout << manufacturerID << "\t" << productID << std::endl;
    else
        std::cerr << message << std::endl;
    return 0;
}

To compile it either pass -ludev option to gcc or use following in CMakeLists.txt:

target_link_libraries(MyExecutable udev)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.