0

I am writing a custom device driver for a USB device which is quite old. The driver is written on the basis on Sample code given by Apple for IOKit. I am able to do communication to the device and I have written the IOUserClient for application space as well.

I am having issue with retrieving data from the buffer to the IOUserClient from the Kernel space. The way this works is that I send a READ command to the device and in turn the device takes time to fill up the read buffer which is notified in the completionhandler. I am using IOCommandGate for the communication. The read function on the IOUserClient should ideally wait for the buffer to fill up from the device but it doesn't. I tried to add a IOLock so that IOUserClient side will wait for the buffer fill up but it waits infinitely without terminating. This might be because the commandgate only forks one thread.

Can Commandgate sleep/awake be used in this case? The Kernel panic happens when I do that. Is there another way? I am relatively new in this area and would appreciate some direction or pointers on the same.

4
  • 1
    1. You shouldn't generally be using a kext for driving USB devices on macOS from 10.15 onwards. What are the reasons you aren't using the user space USB APIs or DriverKit? 2. Assuming you have a good reason to use a kext: IOUserClient ExternalMethods can either be async or synchronous. Async external methods should return immediately, then be notified of completion via the Mach port argument. For a sync/blocking externalmethod, if you want to use an IOCommandGate, you need to suspend the method code with commandSleep(), and wake it up from the I/O completion using commandWakeup(). Commented Jan 28, 2024 at 14:11
  • @pmdj Thanks for the response. I had to write Kext because its a very old device. I tried doing the Dext dev before writing the Kext but I faced a few issues at the lower level. The Kext, as such, is working as I mentioned before. I tried doing commandSleep but as I said it is giving Kernel panic. Just passing commandSleep(void *event) with event just a boolean variable. One more issue which I found was that probably its not multithreaded by default as I thought it would be. So probably I will have to try out the Async method. Commented Jan 28, 2024 at 16:14
  • The device being old shouldn’t have much bearing on what driver tech to use. Note that macOS may refuse to load your kext with SIP enabled depending on what APIs it uses; this includes most types of USB kexts. Commented Jan 29, 2024 at 16:42
  • You can only call commandSleep from inside the gate job, perhaps the panic is related to it being called from outside? Commented Jan 29, 2024 at 16:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.