Enable serial console over USB on Nanopi R4S

Hello,

I have installed Openwrt 22.03.0 on a Nanopi R4S. It is a dual ethernet device configured as a transparent bridge. For that reason, I want to be able to connect to the device without using the ethernet ports.

Currently I am using a UART adapter connected to the debug pins, it works out of the box but it is not super convenient because I have to leave the enclosure open. Instead I would like to use the USB-C port as a serial console aka Serial Device Gadget (the Nanopi already gets its power supply from my PC through that USB cable).

I would also like to do the same on other devices like the Nanopi R1 or R2S. (The USB serial console works on Nanopi R1 using Armbian (as ttyACM0) so that device at least is definitely capable).

I have installed packages kmod-usb-gadget-serial and kmod-usb-serial
and lsmod then looks like this:

root@OpenWrt:~# lsmod  | egrep -i 'serial|tty|dwc|usb|console'
libcomposite           36864  1 usb_f_acm
u_serial               16384  1 usb_f_acm
udc_core               16384  3 usb_f_acm,u_serial,libcomposite
usb_f_acm              12288  0

I am wondering if I also need to have g_serial loaded? If I run:

modprobe g_serial

lsmod looks like this:

root@OpenWrt:~# lsmod  | egrep -i 'serial|tty|dwc|usb|console'
g_serial               12288  0 
libcomposite           36864  2 g_serial,usb_f_acm
u_serial               16384  1 usb_f_acm
udc_core               16384  3 usb_f_acm,u_serial,libcomposite
usb_f_acm              12288  0

But this entry shows up in dmesg:
[ 2875.597012] udc-core: couldn't find an available UDC - added [g_serial] to list of pending drivers

Which perhaps is a clue. A post on Stack Overflow mentions that dummy_hcd dummy (loopback) host controller device is needed for UDC.

I also notice that there are many available packages prefixed kmod-usb-serial* eg: kmod-usb-serial-ch341 and this is somewhat confusing. I am wondering if I need some additional package to make it work.

Based on my research, I understand that if all goes well /dev/ttyGS0 should appear on the Openwrt device, and I might need to add an entry into /etc/inittab as described here: https://openwrt.org/docs/techref/hardware/port.serial

I have also seen mentions of a device tree, but not sure if this is relevant here.

My references:

root@OpenWrt:~# cat /proc/cmdline
console=ttyS2,1500000 earlycon=uart8250,mmio32,0xff1a0000 root=PARTUUID=5452574f-02 rw rootwait

Dmesg info:

root@OpenWrt:~# dmesg | egrep -i 'serial|tty|dwc|usb|console'
[    0.000000] printk: bootconsole [uart8250] enabled
[    0.000000] Kernel command line: console=ttyS2,1500000 earlycon=uart8250,mmio32,0xff1a0000 root=PARTUUID=5452574f-02 rw rootwait
[    0.001765] Console: colour dummy device 80x25
[    0.061578] Serial: AMBA PL011 UART driver
[    0.114392] usbcore: registered new interface driver usbfs
[    0.114962] usbcore: registered new interface driver hub
[    0.115512] usbcore: registered new device driver usb
[    0.168095] Serial: 8250/16550 driver, 16 ports, IRQ sharing enabled
[    0.171178] printk: console [ttyS2] disabled
[    0.171656] ff1a0000.serial: ttyS2 at MMIO 0xff1a0000 (irq = 45, base_baud = 1500000) is a 16550A
[    0.172700] printk: console [ttyS2] enabled
[    0.173477] printk: bootconsole [uart8250] disabled
[    0.203440] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[    0.207169] ehci-platform fe380000.usb: EHCI Host Controller
[    0.207711] ehci-platform fe380000.usb: new USB bus registered, assigned bus number 1
[    0.208500] ehci-platform fe380000.usb: irq 37, io mem 0xfe380000
[    0.237236] ehci-platform fe380000.usb: USB 2.0 started, EHCI 1.00
[    0.238187] hub 1-0:1.0: USB hub found
[    0.239385] rockchip-usb2phy ff770000.syscon:usb2-phy@e460: Requested PHY is disabled
[    0.240077] ehci-platform fe3c0000.usb: EHCI Host Controller
[    0.240588] ehci-platform fe3c0000.usb: new USB bus registered, assigned bus number 2
[    0.241354] ehci-platform fe3c0000.usb: irq 39, io mem 0xfe3c0000
[    0.267233] ehci-platform fe3c0000.usb: USB 2.0 started, EHCI 1.00
[    0.268153] hub 2-0:1.0: USB hub found
[    0.269377] ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
[    0.270613] ohci-platform fe3a0000.usb: Generic Platform OHCI controller
[    0.271218] ohci-platform fe3a0000.usb: new USB bus registered, assigned bus number 3
[    0.271991] ohci-platform fe3a0000.usb: irq 38, io mem 0xfe3a0000
[    0.341603] hub 3-0:1.0: USB hub found
[    0.342770] rockchip-usb2phy ff770000.syscon:usb2-phy@e460: Requested PHY is disabled
[    0.343461] ohci-platform fe3e0000.usb: Generic Platform OHCI controller
[    0.344062] ohci-platform fe3e0000.usb: new USB bus registered, assigned bus number 4
[    0.344832] ohci-platform fe3e0000.usb: irq 40, io mem 0xfe3e0000
[    0.411603] hub 4-0:1.0: USB hub found
[    0.413806] xhci-hcd xhci-hcd.0.auto: new USB bus registered, assigned bus number 5
[    0.416529] hub 5-0:1.0: USB hub found
[    0.418021] xhci-hcd xhci-hcd.0.auto: new USB bus registered, assigned bus number 6
[    0.418701] xhci-hcd xhci-hcd.0.auto: Host supports USB 3.0 SuperSpeed
[    0.419324] usb usb6: We don't know the algorithms for LPM for this host, disabling LPM.
[    0.420385] hub 6-0:1.0: USB hub found
[    0.421957] xhci-hcd xhci-hcd.1.auto: new USB bus registered, assigned bus number 7
[    0.424657] hub 7-0:1.0: USB hub found
[    0.426090] xhci-hcd xhci-hcd.1.auto: new USB bus registered, assigned bus number 8
[    0.426768] xhci-hcd xhci-hcd.1.auto: Host supports USB 3.0 SuperSpeed
[    0.427422] usb usb8: We don't know the algorithms for LPM for this host, disabling LPM.
[    0.428483] hub 8-0:1.0: USB hub found
[    0.429567] usbcore: registered new interface driver usb-storage
[    0.465159] usbcore: registered new interface driver usbhid
[    0.465651] usbhid: USB HID core driver
[    0.813772] dw-apb-uart ff1a0000.serial: forbid DMA for kernel console
[    1.075637] init: Console is alive

Output of lsusb:

root@OpenWrt:~# lsusb 
Bus 007 Device 001: ID 1d6b:0002 Linux 5.10.138 xhci-hcd xHCI Host Controller
Bus 002 Device 001: ID 1d6b:0002 Linux 5.10.138 ehci_hcd EHCI Host Controller
Bus 004 Device 001: ID 1d6b:0001 Linux 5.10.138 ohci_hcd Generic Platform OHCI controller
Bus 006 Device 001: ID 1d6b:0003 Linux 5.10.138 xhci-hcd xHCI Host Controller
Bus 008 Device 001: ID 1d6b:0003 Linux 5.10.138 xhci-hcd xHCI Host Controller
Bus 001 Device 001: ID 1d6b:0002 Linux 5.10.138 ehci_hcd EHCI Host Controller
Bus 003 Device 001: ID 1d6b:0001 Linux 5.10.138 ohci_hcd Generic Platform OHCI controller
Bus 005 Device 001: ID 1d6b:0002 Linux 5.10.138 xhci-hcd xHCI Host Controller

Thanks for your help in advance!

I do not think, that it is possible, waiting another answers.

Yeah I think the usb port needs special functionality like gadget mode - done it for rpi0 but I'm unsure if it's available on r4s
The wiki doesn't mention anything on this so I think you're out of luck

I have another question concerning FriendlyWRT - 'clone' of OpenWRT. Is it possible to 'sysupgrade' system not removing SD-card?

I remember doing it on my r1s when started with friendlyelec as only the kernel is custom build - most of the packages are installed from official feeds

The question is, how to perform 'sysupgrade' from one version FriendlyWRT to next one.

oh very sorry I've made a mistake - I didn;t do any sysupgrade but opkg upgrade

OK I am hopefully making some progress on this issue.

Another thread that touches on a related issue mentions the package kmod-usb-dwc2. After installing this package it seems that this message:

udc-core: couldn't find an available UDC - added [g_serial] to list of pending drivers

no longer shows up in dmesg. Which may be a positive development.

I think the one remaining issue has to do with USB OTG/host configuration and involves the dr_mode setting in particular.
Possible values should be:

  • host
  • peripheral
  • otg (default?)

I am not sure there is a way to change the USB configuration live from userspace without building a new device tree, or building a custom Openwrt image. I have never done this kind of stuff before, so I am not yet comfortable with it.

But it means that in theory, the USB port could be used not only as serial gadget (g_serial) but as a USB ethernet (g_ether) interface too.

I will update this thread if further progress is made. Surely, there are people interested in this functionality :slight_smile:

1 Like

Update: after consulting with a more knowledgeable person it turns out that the USB-C port on the R4S does not have data pins, so it's simply unsuitable for serving a serial console.

However there is another way. Simply use a crossover USB cable like this one. Then it's possible to establish a serial console over regular USB-A ports.

Steps to follow:

  1. plug the cable to the Nanopi and check the output of dmesg (or use readlog -f).
    You should see output like this:
[  266.547389] usb 5-1: new full-speed USB device number 2 using xhci-hcd
[  266.739938] ftdi_sio 5-1:1.0: FTDI USB Serial Device converter detected
[  266.740767] usb 5-1: Detected FT232RL
[  266.742783] usb 5-1: FTDI USB Serial Device converter now attached to ttyUSB0

As you can see a serial console ttyUSB0 is instantiated on the Nanopi. If you don't see that line then you might need to install kmod-usb-serial and/or kmod-usb-serial-ftdi but I am not too sure they are necessary.
2. install agetty and run this command to test: agetty 1500000 ttyUSB0. This will set up a listener on the serial console.
3. plug the other end of the cable to your PC. On a Linux computer, you should also see a similar output and probably the serial console will be named ttyUSB0 too - you can check the output of dmesg to confirm. If you have GNU screen installed you can run this command:
screen /dev/ttyUSB0 1500000
That should provide you with the Openwrt prompt. Another program like Minicom can be used instead.
4. To make it permanent you can add an entry like this in the /etc/inittab file on the Nanopi:
ttyUSB0::respawn:/usr/sbin/agetty ttyUSB0 1500000 vt102
NB: the cable has to be plugged in before the kernel boots. The inittab is read only on device startup.

Bonus: to use full screen width install the package setterm on the Openwrt device, then run this command: setterm --resize.

1 Like

nice hack - but a bit too expensive for my budget :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.