chore(usbgadget): rebind usb after updating config

This commit is contained in:
Siyuan Miao 2025-05-20 00:31:18 +02:00
parent c4782f28dc
commit 5786d37ee0
3 changed files with 14 additions and 5 deletions

View File

@ -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
}
}
}

View File

@ -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
})
}

View File

@ -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,
})
}