vpelletier / python-functionfs

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

HID example does not work on Radxa Rockpi 4B+ (dwc3) #28

Closed herbertp closed 1 year ago

herbertp commented 1 year ago

When trying to run the HID example on a recent version of Armbian, everything seems to be fine from the Gadget/python side, but the Host side reports the following errors:

[11529.096555] usb 2-1: new SuperSpeed USB device number 18 using xhci_hcd
[11529.117158] usb 2-1: no configurations
[11529.117182] usb 2-1: can't read configurations, error -22
[11529.320528] usb 2-1: new SuperSpeed USB device number 19 using xhci_hcd
[11529.341146] usb 2-1: no configurations
[11529.341167] usb 2-1: can't read configurations, error -22
[11529.349908] usb usb2-port1: attempt power cycle
[11530.276543] usb 2-1: new SuperSpeed USB device number 20 using xhci_hcd
[11530.297214] usb 2-1: no configurations
[11530.297236] usb 2-1: can't read configurations, error -22
[11530.500545] usb 2-1: new SuperSpeed USB device number 21 using xhci_hcd
[11530.521191] usb 2-1: no configurations
[11530.521212] usb 2-1: can't read configurations, error -22
[11530.529882] usb usb2-port1: unable to enumerate USB device

Note that the kernel_ncm example works just fine and gives:

[537255.940114] usb 2-1: new SuperSpeed USB device number 22 using xhci_hcd
[537255.960961] usb 2-1: New USB device found, idVendor=1d6b, idProduct=0104, bcdDevice= 6.01
[537255.960990] usb 2-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[537256.028546] usbcore: registered new interface driver cdc_ether
[537256.061476] cdc_ncm 2-1:1.0: MAC-Address: 0a:fb:ea:89:33:bf
[537256.062605] cdc_ncm 2-1:1.0 usb0: register 'cdc_ncm' at usb-0000:01:00.0-1, CDC NCM (NO ZLP), 0a:fb:ea:89:33:bf
[537256.063443] usbcore: registered new interface driver cdc_ncm
[537256.071446] usbcore: registered new interface driver cdc_wdm
[537256.077169] usbcore: registered new interface driver cdc_mbim

System: Armbian 23.02.0-trunk Lunar with bleeding edge Linux 6.1.24-rockchip64. Rockpi 4Bplus with USB port configured to peripherial. usb_f_fs and libcomposite modules loaded. configfs mounted.

herbertp commented 1 year ago

Based on the suggestions in issue #29 I tried a few things and here are the results:

Note: on USB 2.0 the gadget reports "onEnable called" while on 3.x no message is issued.

My Setup is an RPi4 acting as Host (2.0 and 3.x) and the RockPi 4B+ as (3.x) peripheral. My conclusion so far is that the example does not work with USB 3.x host and gadget while it seems to work fine when limited to USB 2.0.

Would be great to get some confirmation that the example works on USB 3.x capable systems.

Thanks, Herbert

vpelletier commented 1 year ago

Thank you very much for these tests.

I do not have any USB3-capable gadget, so I cannot test the example on real hardware... But there is always dummy_hcd.

First, in USB2 mode:

$ sudo modprobe dummy_hcd
$ cat /sys/class/udc/dummy_udc.0/maximum_speed
high-speed
$ virtualenv py3
[...]
$ ./py3/bin/pip install .
[...]
$ sudo py3/bin/python examples/hid/device.py
Gadget ready, waiting for function to exit.
onEnable called
^CGadget exiting.

Now in USB3 mode:

$ sudo rmmod dummy_hcd
$ sudo modprobe dummy_hcd is_super_speed=1
$ cat /sys/class/udc/dummy_udc.0/maximum_speed
super-speed
$ sudo py3/bin/python examples/hid/device.py
Gadget ready, waiting for function to exit.
^CGadget exiting.

...and here it is. So, good news: this is not an obscure hardware-dependent UDC bug. Somehow the example code (...or even something in python-functionfs itself ?) indeed does not work in USB3 mode.

EDIT: quick syslog extract when in superspeed mode, showing this is an enumeration issue, the configuration descriptor cannot be read (so it must be missing in the device declaration):

kernel: [38133.328480] dummy_hcd dummy_hcd.0: Dummy host controller
kernel: [38133.328526] dummy_hcd dummy_hcd.0: new USB bus registered, assigned bus number 6
kernel: [38133.328553] usb usb6: We don't know the algorithms for LPM for this host, disabling LPM.
kernel: [38133.328596] usb usb6: New USB device found, idVendor=1d6b, idProduct=0003, bcdDevice= 6.01
kernel: [38133.328601] usb usb6: New USB device strings: Mfr=3, Product=2, SerialNumber=1
kernel: [38133.328604] usb usb6: Product: Dummy host controller
kernel: [38133.328607] usb usb6: Manufacturer: Linux 6.1.0-8-amd64 dummy_hcd
kernel: [38133.328609] usb usb6: SerialNumber: dummy_hcd.0
kernel: [38133.328838] hub 6-0:1.0: USB hub found
kernel: [38133.328853] hub 6-0:1.0: 1 port detected
systemd[1]: Reached target usb-gadget.target - Hardware activated USB gadget.
kernel: [38333.042307] file system registered
kernel: [38333.044768] read descriptors
kernel: [38333.044833] read strings
kernel: [38333.318348] usb 6-1: new SuperSpeed USB device number 2 using dummy_hcd
kernel: [38333.378359] usb 6-1: no configurations
kernel: [38333.378373] usb 6-1: can't read configurations, error -22
kernel: [38333.514138] usb 6-1: new SuperSpeed USB device number 3 using dummy_hcd
kernel: [38333.578373] usb 6-1: no configurations
kernel: [38333.578387] usb 6-1: can't read configurations, error -22
kernel: [38333.578428] usb usb6-port1: attempt power cycle
kernel: [38333.730355] usb 6-1: new SuperSpeed USB device number 4 using dummy_hcd
kernel: [38333.790196] usb 6-1: no configurations
kernel: [38333.790210] usb 6-1: can't read configurations, error -22
kernel: [38333.926359] usb 6-1: new SuperSpeed USB device number 5 using dummy_hcd
kernel: [38333.986377] usb 6-1: no configurations
kernel: [38333.986391] usb 6-1: can't read configurations, error -22
kernel: [38333.986434] usb usb6-port1: unable to enumerate USB device
kernel: [38347.937725] ffs_data_put(): freeing
kernel: [38347.938057] unloading
systemd[1]: tmp-ffs.usb0_y8ph75_2.mount: Deactivated successfully.
vpelletier commented 1 year ago

I think I have a fix: 36bd59f15bd90bf2fddbdec369d2aa6e376ed082 .

Could you please try this change on real hardware ?

EDIT: on dummy_hcd with the fix above:

$ sudo py3/bin/python examples/hid/device.py
Gadget ready, waiting for function to exit.
onEnable called
^CGadget exiting.
kernel: [41017.006892] file system registered
kernel: [41017.009880] read descriptors
kernel: [41017.009929] read strings
kernel: [41017.278574] usb 6-1: new SuperSpeed USB device number 16 using dummy_hcd
kernel: [41017.390570] usb 6-1: New USB device found, idVendor=1d6b, idProduct=0104, bcdDevice= 6.01
kernel: [41017.390586] usb 6-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
kernel: [41017.390593] usb 6-1: Product: HID mouse demo
kernel: [41017.390599] usb 6-1: Manufacturer: python-functionfs
kernel: [41017.447040] input: python-functionfs HID mouse demo as /devices/platform/dummy_hcd.0/usb6/6-1/6-1:1.0/0003:1D6B:0104.0009/input/input24
kernel: [41017.447738] hid-generic 0003:1D6B:0104.0009: input,hidraw5: USB HID v1.11 Mouse [python-functionfs HID mouse demo] on usb-dummy_hcd.0-1/input0
kernel: [41019.405112] usb 6-1: USB disconnect, device number 16
kernel: [41019.449653] ffs_data_put(): freeing
kernel: [41019.449981] unloading
systemd[1]: tmp-ffs.usb0_s5lw1tg5.mount: Deactivated successfully.
herbertp commented 1 year ago

Unfortunately no luck on real hardware ...

RPi4 reports:

[4186901.631937] usb 2-1: new SuperSpeed USB device number 35 using xhci_hcd
[4186901.652502] usb 2-1: no configurations
[4186901.652529] usb 2-1: can't read configurations, error -22
[4186901.855913] usb 2-1: new SuperSpeed USB device number 36 using xhci_hcd
[4186901.876449] usb 2-1: no configurations
[4186901.876472] usb 2-1: can't read configurations, error -22
[4186901.885154] usb usb2-port1: attempt power cycle
[4186902.811914] usb 2-1: new SuperSpeed USB device number 37 using xhci_hcd
[4186902.832528] usb 2-1: no configurations
[4186902.832550] usb 2-1: can't read configurations, error -22
[4186903.035927] usb 2-1: new SuperSpeed USB device number 38 using xhci_hcd
[4186903.056526] usb 2-1: no configurations
[4186903.056547] usb 2-1: can't read configurations, error -22
[4186903.065184] usb usb2-port1: unable to enumerate USB device

... and the RockPi (Peripheral) doesn't get beyond ...

Gadget ready, waiting for function to exit.

Thanks, Herbert

herbertp commented 1 year ago

Small update here, I've built a 6.3.9 kernel (for the RockPi) which shows exactly the same behavior:

[4293194.881651] usb usb2-port1: attempt power cycle
[4293195.393110] usb 2-1: new SuperSpeed USB device number 49 using xhci_hcd
[4293195.413689] usb 2-1: no configurations
[4293195.413715] usb 2-1: can't read configurations, error -22
[4293195.617154] usb 2-1: new SuperSpeed USB device number 50 using xhci_hcd
[4293195.637702] usb 2-1: no configurations
[4293195.637727] usb 2-1: can't read configurations, error -22
[4293195.646453] usb usb2-port1: unable to enumerate USB device

... and again, it works fine if connected to an USB 2.0 only port.

Hope this helps, Herbert

herbertp commented 1 year ago

Actually I take it all back, turns out that I updated the git repo, but forgot to install python-functionfs ... With the updated version installed, it now actually works on real hardware as well!

Thanks again for the quick fix!

vpelletier commented 1 year ago

With the updated version installed, it now actually works on real hardware as well!

Great, I was wondering if there was another layer to this issue, with the UDC, and was about to suggest that you repeat the test I did with dummy_udc on your device (which, it turns out, would have caught the missed update).

I added a docstring-only fix on top of the code change and released the result as 0.9.2 .

Thank you very much for your tests !