vpelletier / python-functionfs

Pythonic API for linux's functionfs
GNU General Public License v3.0
40 stars 13 forks source link

Respond to usb_control_msg #25

Closed meeuw closed 1 year ago

meeuw commented 1 year ago

Hi, I'd like to emulate Razer HID devices (keyboards, mice) to test OpenRazer (an open source linux driver).

I've adapted the hid example (changed the report descriptor and added 5 extra functionfs.HIDFunctions) so it matches the real hardware Razer keyboard I have. After that the kernel module want to communicate with it (through dummy_hcd) 🥳 .

Now it immediately fails on the call of usb_control_msg: https://github.com/openrazer/openrazer/blob/fe62bdadcfdb05fc9768cf3a38e954ae7cbb4991/driver/razercommon.c#L35

Can you point me in the right direction how to be able to handle/respond to an usb_control_msg call in a Python script using python-functionfs? Some pseudocode or related example would be great.

vpelletier commented 1 year ago

You are going to want to override the onSetup method.

In the code you linked, I see bmRequestType contains USB_RECIP_INTERFACE, which is good news: you can implement control requests targetted at this recipient at the function level. Requests targetting USB_RECIP_ENDPOINT are also possible (please check the onSetup implementation linked above). I am less sure about other recipients (just because I have not played with setup handling recently), especially USB_RECIP_DEVICE will likely be reserved by the kernel for its own handling, and it will only understand standard requests, and Microsoft's magic string descriptor stuff.

Here is a realistic implementation I did that may help explain what I mean in the docstring linked above. including the super call at the end.

meeuw commented 1 year ago

Thank you very much, for your answer and this great Python package.

I think I can just override setHIDReport in this case.

I wish I understood FunctionFS (and USB) a bit better, do you know of any more elaborate documentation than https://docs.kernel.org/usb/functionfs.html ?

vpelletier commented 1 year ago

I think I can just override setHIDReport in this case.

Oh, yes, this is even better.

About documentation on USB, I remember reading a nice simplification of the (very long, but thankfully publicly available) USB specification, and I belive it was BeyondLogic's USB in a NutShell. USB Protocols and USB Descriptors should be the most relevant (probably the latter more than the former).

About documentation on Linux's FunctionFS, I do not clearly remember what I read in order to make sense of the userland API the kernel exposes. I remember reading the ffs examples from the kernel's source, especially aio_simple.

meeuw commented 1 year ago

Thanks a lot, I guess that will do, I hope I can contribute soon to this project!