IOKit or IOUSBHost for USB device acces from user space

USB-Devices without macOS support

Every so often I find usefull USB consumer hardware without any macOS support. At best accompanied with some sample code aimed at Linux using libUSB and/or a datasheet [1]. Using code snippets from long since archived IOKit documentation I can deal with that.

IOUSBHost

Using working IOKit based ObjC Foundation command-line code[2] as reference I am attempting to use theIOUSBHost framework (ObjC Foundation tool). IOUSBHost documentation[3] has all the ingredients but lacks a recipy.

Starting with a plugged-in USBDevice and using its idVendor and idProduct[4] I can

  • initialize and destroy base class IOUSBObject and get notified when I unplug the USBDevice.
  • initialize and destroy IOUSBHostDevice[5] and retrieve various descriptors [6] and configure with value: 0, and even do a vendor bmRequestType request without error.

But since I already beforehand know the content of my descriptors I can start with IOUSBHostInterface?[7] , needed for my 3 pipes, interrupt, bulk in and out. But

  • Error:Unable to open io_service_t object and create user client. with reason: Exclusive open of usb object failed, whether I asked for exclusive access or not.

So I could do with some guidance on how to use IOUSBHost[8].

Let Any Pointer bring joy.


Using: Apple M1 Max, MacOS 13.0.1 (22A400), Xcode 14.1 (14B47b)

Alternatives considered: Raspberry Pi using UDP (JSON;) over wifi.


[1] e.g. https://www.digchip.com/datasheets/parts/datasheet/280/DS2490-pdf.php

[2] depending on usage this might need a runloop

[3] including dec 2022 https://www.usb.org/sites/default/files/usb_32_202206.zip I failed to find the phrase 'Host-mode'. HINT start by inspecting chapter 9 to get a taste of the terms like function, class, address and value and number. Those wordings will also appear in Apple's documentation. NOTE Tables in chapter 9: Assuming D[n]: stands for bitNr[n] in a bitmap where D[0] is the least significant bit. USEFULL No.

[4] use the IORegistryExplorer.app and/or ioreg in Terminal

[5] IOUSBHostInterface.h tells me: CFMutableDictionaryRef to be used with IOService matching methods. To be released by caller. But as far a I know CFFoundation is now under ARC.

[6] IOUSBInterfaceDescriptor * next = IOUSBGetNextInterfaceDescriptor(configurationDescriptor, nil); while (next != nil) // needs cast (IOUSBDescriptorHeader *)next

[7] signed to run locally

[8] My next step would be using Swift. Off topic but related and disturbing in a type safe world https://forums.swift.org/t/nsdata-data-vs-data-withunsafebytes/50231/5 Please please please never use NSData.bytes in Swift code.

Is this why IOUSBHostObject ioDataWithCapacity uses NSMutableData?

Quote: Because the kernel backs the NSMutableData object, the length and capacity aren’t mutable. Any changes to the length or capacity throws an exception.

Effectively treating NSMutableData as NSData, only the compiler complains beforehand when an attempt is made to modify the length of NSData (data.bytes ARE mutable)

IOKit or IOUSBHost for USB device acces from user space
 
 
Q