HID Device Interface in macOS

10.7k views Asked by At

I have to create a software/driver for OSX that interact with HID device connected through USB. The purposes of the application are as follows,

  1. Detect when HID device is plugged in or Removed. The PID and VID of the device is known.
  2. Read HID device serial number, firmware version and other details about the connected device that matches with the VID and PID
  3. Get Reports and set reports to the HID device when it is connected.
  4. Communicate with another application through which the user can alter the HID device setting

I am not a driver developer, but have experience in C, C++, Obj C etc. I am wondering what will be the best way to achieve the mentioned objectives. That is should I develop kernel extension (driver) or is there a better alternative?.

The challenges I face are,

  • Starting with driver development
  • Mac application communicating with the driver.

Either way, this is a new field for me. Can you please suggest some documents or sample codes, from with I can start.

Also kindly mention forums, or communities which might be helpful in developing device driver for mac.

Thankyou you all for the help. :)

3

There are 3 answers

0
ramzes2 On

MacOS X already has drivers for HID devices. So you don't need to develop a special driver/kernel extension for it.

You can use cross-platform opensource library hidapi for communication with HID devices.

7
pmdj On

(Note: Updated 12/2023 with information about DriverKit; original question and answer predate the introduction of DriverKit.)

The other answer (hidapi) to this question certainly holds true for a bunch of different use cases, and I advise going down that route where possible. Rather than using the hidapi wrapper, you can also use the IOHIDManager system API directly.

There are certainly cases where you'll need to dig into driver development proper, however. In particular, if the HID device needs to act as the system keyboard or pointing device, but isn't picked up as you want it to by the built-in HID stack, you might need to (or want to) develop a DriverKit (pre-10.15: kernel-space) driver. (You do have the option of injecting events into the windowing system from userspace too, though, so I'd prototype your use case with that first, if in doubt.)

You haven't quite given us enough detail about what you're trying to do, or the nature of the device to really say for sure what approach might be best.

For HIDDriverKit sample code, Apple publishes some here. For kernel level, check out Apple's own Open Source code. (IOHIDFamily, IOUSBFamily, etc.) Searching the web for the classes you're working with will also turn up some third-party OSS code on Github etc.

Stack Overflow and the relevant Apple developer mailing lists (Darwin-drivers, Darwin-kernel, or Usb, depending on the question), and maybe the developer forums (I find them hard to follow/monitor consistently) are probably the best communities for this kind of thing.

0
thanhthuc nguyen On

You have three options to develop your driver:

  1. Build from scratch with c lib: https://github.com/libusb/hidapi

  2. Follow HID Driver documentation from Apple: https://developer.apple.com/documentation/hiddriverkit, for example:

  3. Reference to some of GitHub resource like: https://github.com/astarasikov/osxhidtouch

As I aware, it seems that number 2 is the best way to develop your driver. But number 3 also a great place to start your development. Overall, my suggestion is number 2 or number 3.