mirror of https://github.com/jetkvm/kvm.git
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
package usbgadget
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
func getUdcs() []string {
|
|
var udcs []string
|
|
|
|
files, err := os.ReadDir("/sys/devices/platform/usbdrd")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
for _, file := range files {
|
|
if !file.IsDir() || !strings.HasSuffix(file.Name(), ".usb") {
|
|
continue
|
|
}
|
|
udcs = append(udcs, file.Name())
|
|
}
|
|
|
|
return udcs
|
|
}
|
|
|
|
func rebindUsb(udc string, ignoreUnbindError bool) error {
|
|
err := os.WriteFile(path.Join(dwc3Path, "unbind"), []byte(udc), 0644)
|
|
if err != nil && !ignoreUnbindError {
|
|
return err
|
|
}
|
|
err = os.WriteFile(path.Join(dwc3Path, "bind"), []byte(udc), 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (u *UsbGadget) rebindUsb(ignoreUnbindError bool) error {
|
|
u.log.Infof("rebinding USB gadget to UDC %s", u.udc)
|
|
return rebindUsb(u.udc, ignoreUnbindError)
|
|
}
|
|
|
|
// RebindUsb rebinds the USB gadget to the UDC.
|
|
func (u *UsbGadget) RebindUsb(ignoreUnbindError bool) error {
|
|
u.configLock.Lock()
|
|
defer u.configLock.Unlock()
|
|
|
|
return u.rebindUsb(ignoreUnbindError)
|
|
}
|
|
|
|
func (u *UsbGadget) writeUDC() error {
|
|
path := path.Join(u.kvmGadgetPath, "UDC")
|
|
|
|
u.log.Tracef("writing UDC %s to %s", u.udc, path)
|
|
err := u.writeIfDifferent(path, []byte(u.udc), 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to write UDC: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetUsbState returns the current state of the USB gadget
|
|
func (u *UsbGadget) GetUsbState() (state string) {
|
|
stateFile := path.Join("/sys/class/udc", u.udc, "state")
|
|
stateBytes, err := os.ReadFile(stateFile)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return "not attached"
|
|
} else {
|
|
u.log.Tracef("failed to read usb state: %v", err)
|
|
}
|
|
return "unknown"
|
|
}
|
|
return strings.TrimSpace(string(stateBytes))
|
|
}
|
|
|
|
// IsUDCBound checks if the UDC state is bound.
|
|
func (u *UsbGadget) IsUDCBound() (bool, error) {
|
|
udcFilePath := path.Join(dwc3Path, u.udc)
|
|
_, err := os.Stat(udcFilePath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, fmt.Errorf("error checking USB emulation state: %w", err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// BindUDC binds the gadget to the UDC.
|
|
func (u *UsbGadget) BindUDC() error {
|
|
err := os.WriteFile(path.Join(dwc3Path, "bind"), []byte(u.udc), 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("error binding UDC: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UnbindUDC unbinds the gadget from the UDC.
|
|
func (u *UsbGadget) UnbindUDC() error {
|
|
err := os.WriteFile(path.Join(dwc3Path, "unbind"), []byte(u.udc), 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("error unbinding UDC: %w", err)
|
|
}
|
|
return nil
|
|
}
|