Re-designing USB stack
Introduction
The pmOS USB stack consists mostly of: (i) initramfs to configure USB Ethernet gadget, (ii) undhcpd to provide a simple DHCP server for SSH over USB networking, (iii) NetworkManager after booting, taking over control over the USB port from initramfs and udhpcd.
This breaks netbooting because the USB connection is reset during the handover between initramfs and NetworkManager (#2290 (closed)). Moreover, other USB gadget modes are hard to add and implement such as USB mass storage, MTP, Android Auto, Tethering, etc.
Sailfish OS and UBPorts use usb-moded which owns the USB port and activates profiles upon it such as mass storage, MTP, USB ethernet, etc. depending on the user's setting. This daemon exists since the MeeGo era for this purpose.
Changes with this MR
- Initramfs keeps USB ethernet gadget for debugging and kernel development purposes.
-
usb-modedis introduced as the daemon for managing the USB connection and patched for postmarketOS and other mainline distros to disable rescue mode (we have this in initramfs already), support NCM USB gadget mode, and generalize FunctionFS daemon support from MTP to any type of daemon running in userspace. These daemons only run when the gadget is active in the specific FunctionFS mode. - NetworkManager only manages the actually IP stack, it doesn't need a handover anymore from the USB ethernet gadget of the initramfs and NetworkManager. This way, we can exclude
usb-modedfrom the installation when using netboot, which stops breaking the connection. - USB connections are shown in the UI by NetworkManager when they are active in USB ethernet gadget mode.
- Charging only mode is the default instead of providing an ethernet gadget. Users can switch to developer mode to have the same situation as now for accessing the device over SSH (no tethering).
- Tethering can be enabled as well, which is similar to developer mode but also provides internet access to the connected USB host (PC, etc.).
- Media Transfer Protocol support is added through uMTPResponder (
umtprd) to transfer files easily between the device and the connected host. Only/homeis exposed for security reasons and MTP mode must be activated by the user, similar to developer/tethering modes. - Future USB gadgets are now possible like Android Open Accessory to support Android Auto by adding a configuration file for
usb-moded. Each mode is a separate subpackage which allows the user to install whatever they like. If they only need developer mode, they can skip all other subpackages.
TODOs
-
Depend on usb-modedin postmarketos-base (or other base package), to discuss. -
Remove old NM approach -
Mark conflict with netboot package to avoid installing usb-moded, to fix breaking netboot mode (#2290 (closed)) -
Add UI to select USB mode? Currently, switching modes is possible with usb_mode_util -s $MODEvia the command line, runusb_mode_util -mto list all modes. Android shows a notification when the USB cable is plugged in and only allows charging. When the users taps the notification, an UI is shown where the USB mode can be selected (avoids that data is leaked without user interaction). Not sure this is the right UX, but covers the requirements though. To be discussed. -
Send usb-modedpatches upstream- will upgrade
usb-modedin Alpine once landed in a release, but may not happen before this is merged - Submitted upstream: https://github.com/sailfishos/usb-moded/pull/25, https://github.com/sailfishos/usb-moded/pull/26, and https://github.com/sailfishos/usb-moded/pull/27
- will upgrade
-
Send uMTP-Responder to Alpine: https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/84536
How to test
Once more integrated with other packages, this should be more straightforward as the service will be enabled automatically.
- This is only enabled for GNOME Mobile, Plasma Mobile, and Phosh. If you're running some other UI on your phone and feel like this should be enabled for that UI, leave a comment.
- Get the packages from this MR with
mrtest upgrade 6531 - Reboot, to make sure all relevant services are started up
- Plug in a USB cable, a notification should show up letting you switch modes. Play around with them, report issues. NOTE: The notification does not show up if the device is locked, this is by design since we don't to allow mode switching when the device is locked
Fixed issues
- Fixes #2290 (closed)
- Fixes pmcr#6 (closed)
- Fixes #646 (closed)
- Fixes #2541 (closed)