From 5786d37ee0f7f9887ab2adb4a7d547e2b579ba8c Mon Sep 17 00:00:00 2001 From: Siyuan Miao Date: Tue, 20 May 2025 00:31:18 +0200 Subject: [PATCH] chore(usbgadget): rebind usb after updating config --- internal/usbgadget/changeset_resolver.go | 6 +++++- internal/usbgadget/config.go | 9 ++++++--- internal/usbgadget/config_tx.go | 4 +++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/internal/usbgadget/changeset_resolver.go b/internal/usbgadget/changeset_resolver.go index 3dfffa3..67812e0 100644 --- a/internal/usbgadget/changeset_resolver.go +++ b/internal/usbgadget/changeset_resolver.go @@ -128,7 +128,11 @@ func (c *ChangeSetResolver) applyChanges() error { err := c.changeset.applyChange(change) if err != nil { - return err + if change.IgnoreErrors { + c.l.Warn().Str("change", change.String()).Err(err).Msg("ignoring error") + } else { + return err + } } } diff --git a/internal/usbgadget/config.go b/internal/usbgadget/config.go index e2959f3..1c4f9c3 100644 --- a/internal/usbgadget/config.go +++ b/internal/usbgadget/config.go @@ -177,7 +177,7 @@ func (u *UsbGadget) Init() error { u.udc = udcs[0] - err := u.configureUsbGadget() + err := u.configureUsbGadget(false) if err != nil { return u.logError("unable to initialize USB stack", err) } @@ -191,7 +191,7 @@ func (u *UsbGadget) UpdateGadgetConfig() error { u.loadGadgetConfig() - err := u.configureUsbGadget() + err := u.configureUsbGadget(true) if err != nil { return u.logError("unable to update gadget config", err) } @@ -199,11 +199,14 @@ func (u *UsbGadget) UpdateGadgetConfig() error { return nil } -func (u *UsbGadget) configureUsbGadget() error { +func (u *UsbGadget) configureUsbGadget(resetUsb bool) error { return u.WithTransaction(func() error { u.tx.MountConfigFS() u.tx.CreateConfigPath() u.tx.WriteGadgetConfig() + if resetUsb { + u.tx.RebindUsb(true) + } return nil }) } diff --git a/internal/usbgadget/config_tx.go b/internal/usbgadget/config_tx.go index ce98dd1..df8a3d1 100644 --- a/internal/usbgadget/config_tx.go +++ b/internal/usbgadget/config_tx.go @@ -319,6 +319,7 @@ func (tx *UsbGadgetTransaction) WriteUDC() { // bound the gadget to a UDC (USB Device Controller) path := path.Join(tx.kvmGadgetPath, "UDC") tx.addFileChange("udc", RequestedFileChange{ + Key: "udc", Path: path, ExpectedState: FileStateFileContentMatch, ExpectedContent: []byte(tx.udc), @@ -334,6 +335,8 @@ func (tx *UsbGadgetTransaction) RebindUsb(ignoreUnbindError bool) { ExpectedState: FileStateFileWrite, ExpectedContent: []byte(tx.udc), Description: "unbind UDC", + DependsOn: []string{"udc"}, + IgnoreErrors: ignoreUnbindError, }) // bind the gadget to the UDC tx.addFileChange("udc", RequestedFileChange{ @@ -342,6 +345,5 @@ func (tx *UsbGadgetTransaction) RebindUsb(ignoreUnbindError bool) { ExpectedContent: []byte(tx.udc), Description: "bind UDC", DependsOn: []string{path.Join(tx.dwc3Path, "unbind")}, - IgnoreErrors: ignoreUnbindError, }) }