Trouble with USB OTG in device mode

Hello,

I would like to use the ROCK Pi 4A USB OTG port exclusively in device mode (presenting the ROCK Pi 4 as a peripheral to another host). I am running Armbian Buster on the ROCK Pi 4 and have configured a USB gadget via configfs, and have set the OTG hardware switch to the device setting. However, when I connect the ROCK Pi 4 to another host, the host does not immediately see the device.

If I toggle the OTG hardware switch from device → host → device again, the host will (often) see the device and I can start to use it without a problem. But this is inconvenient and not ideal for my application.

Is there some way to make the OTG port permanently visible as a device to the host without needing to toggle the switch?

You can toggle the switch to device side, and force set the otg to peripheral mode in the kernel device tree.

change the default otg mode:

&usbdrd_dwc3_0 {
    dr_mode = "otg";
    status = "okay";
};

to

&usbdrd_dwc3_0 {
    dr_mode = "peripheral";
    status = "okay";
};

and try.

Hi,
I use ROCK Pi 4B v1.3, with Debian9Desktop from Radxa download. Kernel 4.4.154-90.
I want to use it as external USB storage for my PC.
And I’d like to have default set of ROCK Pi 4 USB OTG port as a g_mass_storage device.

I observed that behavior of ROCK Pi 4 as g_mass_storage device depends on the connection to Host PC before I power the ROCK PI 4.
If I first power up the ROCK pi 4 and later connect to host PC, to start the OTG I need to toggle the switch in the way @antihacker do.
If I first connect USB port to host PC, set the DEVICE position of the switch and later power up the ROCK Pi 4. The OTG is working properly. I do not need to toggle the OTG switch.

Is there a way to start the ROCK Pi 4 always as the OTG device?

I checked your suggestion, but it doesn’t work in the way I need.
I changed rockpi-4b-linux.dts
dr_mode I found in line 473 (usb0 -> dwc3@fe800000 -> dr_mode)

I discovered that only one change between both settings (otg/peripheral) is the reaction for switching to the HOST position:

when dr_mode = “peripheral”;
the Host is not starting and the only log message is that rockchip-dwc3 usb0: USB unconnected.

[ 67.561442] rockchip-dwc3 usb0: Peripheral disconnect timeout
[ 67.561974] rockchip-dwc3 usb0: USB unconnected

when dr_mode = “otg” the Host is starting.

[10374.670799] xhci-hcd xhci-hcd.8.auto: xHCI Host Controller
[10374.671976] xhci-hcd xhci-hcd.8.auto: new USB bus registered, assigned bus number 7
[10374.672887] xhci-hcd xhci-hcd.8.auto: hcc params 0x0220fe64 hci version 0x110 quirks 0x02030010
[10374.673953] xhci-hcd xhci-hcd.8.auto: irq 231, io mem 0xfe800000
[10374.676352] usb usb7: New USB device found, idVendor=1d6b, idProduct=0002
[10374.676967] usb usb7: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[10374.677732] usb usb7: Product: xHCI Host Controller
[10374.678183] usb usb7: Manufacturer: Linux 4.4.154-90-rockchip-ga14f6502e045 xhci-hcd
[10374.678870] usb usb7: SerialNumber: xhci-hcd.8.auto
[10374.680579] hub 7-0:1.0: USB hub found
[10374.680982] hub 7-0:1.0: 1 port detected
[10374.682105] xhci-hcd xhci-hcd.8.auto: xHCI Host Controller
[10374.683056] xhci-hcd xhci-hcd.8.auto: new USB bus registered, assigned bus number 8
[10374.683862] usb usb8: We don’t know the algorithms for LPM for this host, disabling LPM.
[10374.684771] usb usb8: New USB device found, idVendor=1d6b, idProduct=0003
[10374.685488] usb usb8: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[10374.686143] usb usb8: Product: xHCI Host Controller
[10374.686598] usb usb8: Manufacturer: Linux 4.4.154-90-rockchip-ga14f6502e045 xhci-hcd
[10374.687286] usb usb8: SerialNumber: xhci-hcd.8.auto
[10374.693202] hub 8-0:1.0: USB hub found
[10374.693609] hub 8-0:1.0: 1 port detected
[10374.694566] rockchip-dwc3 usb0: USB HOST connected

Switching back to the DEVICE position, starts the g_mass_storage and RockPi is visible as a Mass storage Device from the Host PC.
dmesg of RockPi:

[ 154.074280] phy phy-ff770000.syscon:usb2-phy@e450.1: charger = USB_SDP_CHARGER
[ 154.074940] rockchip-dwc3 usb0: USB peripheral connected
[ 154.394231] g_mass_storage gadget: high-speed config #1: Linux File-Backed Storage

So, maybe there is other tip I can check ?