Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: Attempting to get joystick vendor and product IDs via ioctl, get EINVAL instead

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?

like image 265
NoobsArePeople2 Avatar asked Oct 27 '25 12:10

NoobsArePeople2


1 Answers

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.

like image 107
NoobsArePeople2 Avatar answered Oct 29 '25 06:10

NoobsArePeople2