I tried to port this to DriverKit

Author: noah-nueblingCreated Sep 2, 2023Updated Sep 27, 2024

Hey guys, I tried to port this driver to DriverKit, so it works under modern macOS.

Here's the repo for my port: https://github.com/noah-nuebling/mac-gamepad-driver

Unfortunately it doesn't quite work so far but I thought I'd share it in case somebody else wants to keep working on it.

Discussion

This attempts to port the driver for wired controllers in the original repo to DriverKit. In the original repo, the driver for wired controllers is implemented by the files _60Controller.cpp and Controller.cpp.

The port creates two clients for the gamepad in the IORegistry. gamepad_driver_user and Xbox360ControllerClass. Here's a screenshot. CleanShot 2023-09-02 at 23 06 51@2x

Here's more details on how it works:

I have an XboxOne wired controller. When I attach it to the computer, macOS creates a registry entry of class IOUSBHostDevice for it (it does this anyways even if this driver is not installed). Then, if this driver is installed, the system creates the gamepad_driver_user registry entry which is driven by the code in the cpp class gamepad_driver_user and attaches it to the IOUSBHostDevice as a client. When gamepad_driver_user starts, it starts finding USB interfaces and stuff of the device and it tries to determine what kind of gamepad is being used (XboxOne, Xbox360, XboxOriginal, and some other options). It tries to find the USB endpoints on the device for sending and receiving data. Then it writes some data to the device. It asks the system to create another registry entry which is driven by the code in the cpp class Xbox360ControllerClass and the system attaches this new entry to gamepad_driver_user as a client. Then gamepad_driver_user starts reading from the device in a loop. The data it reads from the device in a loop is sent to Xbox360ControllerClass→handleReport(), which then does some additional processing on the report before passing it on to userspace clients.

The Xbox360ControllerClass registry entry is a subclass of IOHIDDevice. Which means userspace programs can find it and start talking to it using the IOHIDManager apis. This architecture is the same as the original 360Controller driver. But the way it's implemented is pretty different because all the APIs changed a lot for DriverKit.

My current implementation doesn't work properly for my wired XboxOne controller because all of the reports that are being read from the device don't seem to be "valid" according to the logic I copied from the 360Controller driver. I don't know if my controller is weird or if there's a bug in my code.

Also see Notes.md in the repo for more info and steps to run and test the driver.

Another thing that's really annoying is that you need to request permissions from Apple to attach your driver directly to the IOUSBHostDevice. And you need to request separate permissions for each vendorID. This might make it infeasible to ever publish the DriverKit based driver. But currently I'm using Ad Hoc signing and some other hacks to get around this requirement and run the driver even without permission from Apple. I even requested the permissions from Apple but they declined :(

Another side note:

Here are the logs that the driver outputs on my system: CleanShot 2023-09-02 at 23 27 18@2x

I think next steps for further developing the driver would be:

  • Testing and debugging with a wired Xbox 360 controller and getting it work in the first place.
  • Implementing the proper support for XboxOne and XboxOriginal controllers (stubs for XboxOne controller support are already in the repo)
  • Maybe building a UI / updating the UI over from the original 360Controller driver. The old UI should work the same with the new driver without any meaningful updates I think. But it might be a good idea to port the UI over from being a prefpane to being a normal app, because prefpanes are really hard to develop under modern macOS.
  • Maybe add the remapping and deadzone customization features like the original 360Controller driver, so everything in the UI works. Shouldn't be too much work.
  • Implementing support for peripherals like the microphone if anyone wants to.
  • Fixing all the memory leaks and bad error handling in my horrible code

If you have any more questions, let me know.

Source: 360Controller/360Controller