Skip to content

Native BSD backend using uhid #608

Closed

Description

I was curious about the pragmatics of supporting various BSDs. There seems to be a fairly common API for HID over USB on most common BSDs called usbhid which is intended to be used with the uhid kernel driver for generic HID devices. Support for this is likely a necessity because several BSD ports of libusb cannot be used with USB devices already reserved by a kernel driver.

I'm too unfamiliar with HID myself to work on this at present, but is there anyone else willing to put in the effort to add this as a hidapi backend?

Activity

  1. Youw commented on Aug 12, 2023

    @Youw
    Member

    Duplicate of #543 ?

  2. added
    enhancement
    bsd
    on Aug 12, 2023
  3. changed the title [-]BSD Support[/-] [+]Native BSD backend[/+] on Aug 12, 2023
  4. Youw commented on Aug 12, 2023

    @Youw
    Member

    For those who potentially want to start implementing this but don't know where to start (from HIDAPI POV) - here is an example me adding an experimental WinRT backend: https://github.com/libusb/hidapi/compare/winrt

  5. changed the title [-]Native BSD backend[/-] [+]Native BSD backend using uhid[/+] on Aug 13, 2023
  6. mcuee commented on Aug 13, 2023

    @mcuee
    Member

    I am not so sure if there is any interest to have the uhid backend for FreeBSD as the existing libusb backedn works fine (libusb is part of FreeBSD now, and it is using FreeBSD's own libusb implementation with BSD license). So you can arguably call it native for FreeBD as well. hidraw backend seems to be more interesting for FreeBSD.

    But I agree that it will be good to have uhid backend for NetBSD and OpenBSD, if it is possible. HIDAPI libusb backend for NetBSD/OpenBSD require rebuilding the kernel last time I checked, so it is not really that useful.

  7. Youw commented on Aug 13, 2023

    @Youw
    Member

    Instead of linking against uhid usbhid HIDAPI could load it dynamically - that way we avoid symbol conflict.
    Also, for HIDAPI v1 I think hid_init will be renamed to hidapi_initialize (and it will become context-specific initialisation, similar to what libusb uses).

  8. Youw commented on Aug 15, 2023

    @Youw
    Member

    What I was referring to it the optional high level library usbhid

    Excuse me for confusion, I really meant usbhid.

    Is there even an API in hidapi to handle this?
    In the case of a device with multiple report IDs being split across multiple devices, how should I approach this?

    This is an interesting problem.
    As per current HIDAPI design, each hid_device is only as unique as VID/PID/SerialNumber/UsagePage/Usage combination (and it is not even always true for the UsagePage/Usage part - but that's a different story).

    HIDAPI doesn't support "device-per-report" abstraction as it assumes that a single device may support multiple reports (that's why a report ID is explicitly passed as part of data payload).

    To support the uhid abstraction, the HIDAPI implementation would have to do it under the hood:

    • parse the report descriptor;
    • find a list of reports supported by the device;
    • first time a report is tried to read/write - open a corresponding per-report uhid device and use it to send/receive data.

    I don't see any better way.

  9. Youw commented on Aug 15, 2023

    @Youw
    Member

    I believe opening all devices at once would be overkill, if even possible (what about exclusive/shared access, read/write permissions, etc.?). Lookup table - yes, plus lazy device opening, only if/when needed.

  10. Youw commented on Aug 16, 2023

    @Youw
    Member

    Nevertheless thanks for great investigation.
    No one has check it this deep in context of HIDAPI.

    I'll keep this ticket open for now. Maybe there will be more findings/details.

  11. mcuee commented on Aug 27, 2023

    @mcuee
    Member

    @Youw Does the libusb backend on FreeBSD have to completely disable the HID kernel driver to work? I was considering that in some niche situations it may be good to have the option to use the native kernel interface since devices in some cases will expose multiple HID interfaces.

    No. That is the beauty of FreeBSD USB stack. It exposes ugen driver along with uhid for generic USB HID devices, so that you can use libusb backend out of box.

    Unfortunately Hans Petter Selasky passed away recently. If not I would ask him to explain how it works.

  12. mcuee commented on Aug 27, 2023

    @mcuee
    Member

    I see. How unfortunate. I think I will just work on OpenBSD backend and consider uhid work complete. I have an idea for how to make it work. It won't be ideal but it is still an improvement due to how limited libusb on OpenBSD is. Notable limitations would be that it would not be possible to enumerate uhid devices that are already in use.

    Having a uhid backend for OpenBSD will be great. Thanks. As you mentioned, it will still be much better than libusb backend.

    In fact I consider HIDAPI libusb backend not so useful (in fact kind of useless) for NetBSD/OpenBSD users.

  13. mcuee commented on Mar 28, 2025

    @mcuee
    Member
  14. Youw commented on Mar 28, 2025

    @Youw
    Member

    Right, closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bsdenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions