linux-kernelembedded-linuxu-bootusb-otg

Can USB-OTG be used for U-Boot and Linux consoles?


I have a custom i.MX6Q-based board with working U-Boot and Linux (Ubuntu) setups. The micro and board have support for USB-OTG and one serial port; currently, the serial port serves the console for both U-Boot and Linux. However, we may need to use the serial port for another purpose, but we don't want to lose the console for U-Boot and Linux. Is it possible to use the USB-OTG port for the system console for both U-Boot and Linux?

I've done some research and found a couple of promising articles here and here, though the second article says this tidbit:

Unfortunately it won't work as system console as the gadget driver is loaded as a module, but we can use it for serial console.

I'm not sure I understand this, but it sounds like the method won't meet my needs, which is to use USB-OTG for both U-Boot and Linux system consoles. I did try these methods, but without luck, which may mean that U-Boot and Linux aren't built properly for the desired functionality.

So here are my questions:

  1. Can this work for U-Boot?
  2. Can this work for Linux?
  3. Am I insane for contemplating this path?

For either, any guidance (e.g. tutorials, examples, etc.) would be greatly appreciated.

Thanks!


Solution

    1. Can this work for U-Boot?

    Yes, at least since U-Boot version 2008.10, the README file has stated:

        Define the below if you wish to use the USB console.
            CONFIG_USB_DEVICE
            Define this to build a UDC device
    
            CONFIG_USB_TTY
            Define this to have a tty type of device available to
            talk to the UDC device
    
            CFG_CONSOLE_IS_IN_ENV
            Define this if you want stdin, stdout &/or stderr to
            be set to usbtty.
    

    Note that these configuration symbols are not accessible using the menuconfig, and must be enabled in a configuration file.
    Currently at least five boards use this U-Boot capability, based on the occurrence of CONFIG_USB_TTY in files in include/configs/, for example include/configs/ti_omap4_common.h.

    This USB configuration requires non-default definitions for the stdin and stdout environment variables. Refer to the README documentation for the details.


    1. Can this work for Linux?

    Yes, Linux (at least since version 4.5) can have a serial console on a USB connection, either a USB-to-serial adapter on a host port or a USB serial gadget on a device port (using CDC/ACM).

    For instance, in drivers/usb/gadget/Kconfig there's the selection:

    config U_SERIAL_CONSOLE
        bool "Serial gadget console support"
        depends on USB_G_SERIAL
        help
           It supports the serial gadget can be used as a console.
    

    In the Linux 5.7.8 kernel only two boards have default configurations that use this capability, for example see arch/arm/configs/aspeed_g4_defconfig.

    Besides a proper configuration to build the necessary drivers, a serial-gadget console requires (1) the kernel parameter specification (e.g. console=ttyGS0,...), and (2) a login session initiated by a getty command (e.g. in the inittab file).


    1. Am I insane for contemplating this path?

    No comment.

    Beware that should you encounter a kernel boot issue, the Linux serial-gadget console does not support earlycon nor earlyprintk capability.

    Personally I prefer to use a serial link that is persistent regardless of the target board's state. That ensures the terminal emulator program does not complain about lost connections.


    Addendum

    Unfortunately this Linux console on a USB serial gadget does not display boot messages generated by the kernel (before the login prompt), even if all drivers are statically linked in to the kernel image.
    Although the syslog has messages like

    console [ttyGS0] enabled
    g_serial gadget: g_serial ready
    ... 
    gs_open: ttyGS0 ((ptrval),(ptrval))
    

    before the salient Freeing unused kernel memory message, the host side does not receive any console messages until userspace is active.

    This shortcoming is also reported in this guide: https://linux-sunxi.org/USB_Gadget/Serial