1

Topic: Works on Linux, but only as root?

Hi, I just downloaded HidSharp and compiled the solution on Linux. I had to change the Target to ".NET 3.5" for all projects, but it compiled and ran.

However, it fails unless I run as root. It appears that something requires permissions, but I can't tell what. Here are some more details.

Any advice or hints would be appreciated!

-Doug

$ ls -al /dev/bus/usb/002/009 crw-rwxr-- 1 root plugdev 189, 136 Jun  8 14:20 /dev/bus/usb/002/009

$ mono HidSharp.Test.exe
Complete device list (took 73 ms to get 0 devices):

Opening HID class device...
Failed to open device.

$ sudo mono HidSharp.Test.exe
Complete device list (took 90 ms to get 2 devices):
BirdBrain Tools Finch (VID 9044, PID 4369, version 266)
(unnamed) (VID 16700, PID 33121, version 512)

Opening HID class device...
BirdBrain Tools Finch (VID 9044, PID 4369, version 266)
(unnamed) (VID 16700, PID 33121, version 512)

Max Lengths:
  Input:   9
  Output:  2
  Feature: 0

The operating system name for this device is:
  /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.6/2-1.6.1/2-1.6.1:1.0/0003:413C:8161.0001/hidraw/hidraw0

2

Re: Works on Linux, but only as root?

I have found that if I change TryParseReportDescriptor in NativeMethods.cs from:

int handle = NativeMethods.retry(() => NativeMethods.open
                                             (devnode, NativeMethods.oflag.NONBLOCK));

to:

int handle = NativeMethods.retry(() => NativeMethods.open
                                             (devnode, NativeMethods.oflag.RDONLY));

Then it works. Is this a valid solution?

3

Re: Works on Linux, but only as root?

Actually, the solution ended up to be commenting out the following bits:

protected override bool TryCreateDevice(object key, out HidDevice device, out object creationState)
        {
            creationState = null;
            string syspath = (string)key; var hidDevice = new LinuxHidDevice(syspath);
            bool info = hidDevice.GetInfo();
            //if (info) {
            //    device = null;
            //    return false;
            //}
            device = hidDevice;
            return true;
        }

and then it works for root and regular users.

4

Re: Works on Linux, but only as root?

Sorry for the delayed response.

Hmm. I will investigate this weekend. Read-only, I assume you are only able to read HID packets but not write them?

See what your permissions on /dev/hidraw0 are. By default, yes, it requires root. This is one way Linux differs rather drastically from (say) Windows where anyone can access any HID device any time. See http://askubuntu.com/questions/15570/co … hid-device for how to configure udev otherwise.

5

Re: Works on Linux, but only as root?

Yes, I did everything necessary to be able to read on the Linux side. But still, I had to make the above change.

6

Re: Works on Linux, but only as root?

Ah, come to think of it, it may need to be subsystem hidraw not usb in udev.

I'll investigate the permissions this weekend. All of my tests on Linux (32-bit Ubuntu) have been as root. Some folks use the library on Raspberry Pi as well, but that's always root.

GetInfo... GetInfo queries both udev as well as trying to open the hidraw device to get the report descriptor (for the Input, Output, and Feature report lengths, mainly). So if it has no permission to open the hidraw device, it can't enumerate it successfully. You are sure you have permissions to /dev/hidraw0?

On a side note, the TryCreateDevice code you have there has the GetInfo condition reversed from my code -- it's supposed to fail on false not true. What version are you using?

7

Re: Works on Linux, but only as root?

"On a side note" was the issue. It looks like I might have introduced this error when trying to debug. I apologize  for wasting your time.

Just for future generations, here are my udev rules, which I needed to access as non-root:

SUBSYSTEM=="usb", ATTR{idVendor}=="1234", ATTR{idProduct}=="1111", MODE="0660", GROUP="plugdev"
KERNEL=="hidraw*", ATTRS{idVendor}=="1234", ATTRS{idProduct}=="1111", MODE="0666", GROUP="plugdev"

Thanks for the great code!