mirror of https://github.com/jetkvm/kvm.git
Compare commits
15 Commits
391de747a6
...
03fd7508de
Author | SHA1 | Date |
---|---|---|
|
03fd7508de | |
|
e9096c36f7 | |
|
c6ba93c46f | |
|
afebc2dd1e | |
|
8108597740 | |
|
61477e1795 | |
|
ac377c4953 | |
|
e9b68f8131 | |
|
3be51a106f | |
|
30385f8ae3 | |
|
8c54eac167 | |
|
fd6e2fa7df | |
|
789f2bef65 | |
|
f8e668349b | |
|
8329137bae |
10
jsonrpc.go
10
jsonrpc.go
|
@ -479,17 +479,15 @@ func rpcSetUsbEmulationState(enabled bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func rpcSetUsbConfig(usbConfig UsbConfig) error {
|
func rpcSetUsbConfig(usbConfig UsbConfig) error {
|
||||||
log.Printf("[jsonrpc.go:rpcSetUsbConfig] called")
|
|
||||||
|
|
||||||
LoadConfig()
|
LoadConfig()
|
||||||
config.UsbConfig = usbConfig
|
config.UsbConfig = usbConfig
|
||||||
|
|
||||||
err2 := UpdateGadgetConfig()
|
err := UpdateGadgetConfig()
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write gadget config: %w", err2)
|
return fmt.Errorf("failed to write gadget config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := SaveConfig()
|
err = SaveConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to save usb config: %w", err)
|
return fmt.Errorf("failed to save usb config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,11 +28,11 @@ import { useRevalidator } from "react-router-dom";
|
||||||
import { ShieldCheckIcon } from "@heroicons/react/20/solid";
|
import { ShieldCheckIcon } from "@heroicons/react/20/solid";
|
||||||
|
|
||||||
export function SettingsItem({
|
export function SettingsItem({
|
||||||
title,
|
title,
|
||||||
description,
|
description,
|
||||||
children,
|
children,
|
||||||
className,
|
className,
|
||||||
}: {
|
}: {
|
||||||
title: string;
|
title: string;
|
||||||
description: string | React.ReactNode;
|
description: string | React.ReactNode;
|
||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
|
@ -103,6 +103,12 @@ export default function SettingsSidebar() {
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
|
|
||||||
const [usbEmulationEnabled, setUsbEmulationEnabled] = useState(false);
|
const [usbEmulationEnabled, setUsbEmulationEnabled] = useState(false);
|
||||||
|
const getUsbEmulationState = useCallback(() => {
|
||||||
|
send("getUsbEmulationState", {}, resp => {
|
||||||
|
if ("error" in resp) return;
|
||||||
|
setUsbEmulationEnabled(resp.result as boolean);
|
||||||
|
});
|
||||||
|
}, [send]);
|
||||||
|
|
||||||
const [usbConfig, setUsbConfig] = useState({
|
const [usbConfig, setUsbConfig] = useState({
|
||||||
usb_product_id: '',
|
usb_product_id: '',
|
||||||
|
@ -112,13 +118,6 @@ export default function SettingsSidebar() {
|
||||||
usb_name: '',
|
usb_name: '',
|
||||||
})
|
})
|
||||||
|
|
||||||
const getUsbEmulationState = useCallback(() => {
|
|
||||||
send("getUsbEmulationState", {}, resp => {
|
|
||||||
if ("error" in resp) return;
|
|
||||||
setUsbEmulationEnabled(resp.result as boolean);
|
|
||||||
});
|
|
||||||
}, [send]);
|
|
||||||
|
|
||||||
const handleUsbEmulationToggle = useCallback(
|
const handleUsbEmulationToggle = useCallback(
|
||||||
(enabled: boolean) => {
|
(enabled: boolean) => {
|
||||||
send("setUsbEmulationState", { enabled: enabled }, resp => {
|
send("setUsbEmulationState", { enabled: enabled }, resp => {
|
||||||
|
@ -225,7 +224,7 @@ export default function SettingsSidebar() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [send, usbConfig]);
|
}, [send]);
|
||||||
|
|
||||||
const handleSSHKeyChange = (newKey: string) => {
|
const handleSSHKeyChange = (newKey: string) => {
|
||||||
setSSHKey(newKey);
|
setSSHKey(newKey);
|
||||||
|
|
77
usb.go
77
usb.go
|
@ -3,7 +3,6 @@ package kvm
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
gadget "github.com/openstadia/go-usb-gadget"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -12,6 +11,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
gadget "github.com/openstadia/go-usb-gadget"
|
||||||
)
|
)
|
||||||
|
|
||||||
const configFSPath = "/sys/kernel/config"
|
const configFSPath = "/sys/kernel/config"
|
||||||
|
@ -19,6 +20,19 @@ const gadgetPath = "/sys/kernel/config/usb_gadget"
|
||||||
const kvmGadgetPath = "/sys/kernel/config/usb_gadget/jetkvm"
|
const kvmGadgetPath = "/sys/kernel/config/usb_gadget/jetkvm"
|
||||||
const configC1Path = "/sys/kernel/config/usb_gadget/jetkvm/configs/c.1"
|
const configC1Path = "/sys/kernel/config/usb_gadget/jetkvm/configs/c.1"
|
||||||
|
|
||||||
|
func mountConfigFS() error {
|
||||||
|
_, err := os.Stat(gadgetPath)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = exec.Command("mount", "-t", "configfs", "none", configFSPath).Run()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to mount configfs: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("unable to access usb gadget path: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
_ = os.MkdirAll(imagesFolder, 0755)
|
_ = os.MkdirAll(imagesFolder, 0755)
|
||||||
udcs := gadget.GetUdcs()
|
udcs := gadget.GetUdcs()
|
||||||
|
@ -72,7 +86,7 @@ func UpdateGadgetConfig() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Successfully updated string attributes: %s", strAttrs)
|
log.Printf("Successfully updated usb string attributes: %s", strAttrs)
|
||||||
|
|
||||||
err = rebindUsb()
|
err = rebindUsb()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -82,22 +96,6 @@ func UpdateGadgetConfig() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mountConfigFS() error {
|
|
||||||
logger.Infof("Checking for gadgetPath: %s ...", gadgetPath)
|
|
||||||
_, err := os.Stat(gadgetPath)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
logger.Infof("running mount command...")
|
|
||||||
err = exec.Command("mount", "-t", "configfs", "none", configFSPath).Run()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to mount configfs: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("unable to access usb gadget path: %w", err)
|
|
||||||
}
|
|
||||||
logger.Infof("Successfully mounted usb gadget at %s", gadgetPath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeGadgetAttrs(basePath string, attrs [][]string) error {
|
func writeGadgetAttrs(basePath string, attrs [][]string) error {
|
||||||
for _, item := range attrs {
|
for _, item := range attrs {
|
||||||
filePath := filepath.Join(basePath, item[0])
|
filePath := filepath.Join(basePath, item[0])
|
||||||
|
@ -119,35 +117,30 @@ func writeGadgetConfig() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
LoadConfig()
|
err = writeGadgetAttrs(kvmGadgetPath, [][]string{
|
||||||
gadgetAttrs := [][]string{
|
{"bcdUSB", "0x0200"}, //USB 2.0
|
||||||
{"bcdUSB", "0x0200"}, //USB 2.0
|
{"idVendor", "0x1d6b"}, //The Linux Foundation
|
||||||
{"idVendor", config.UsbConfig.UsbVendorId},
|
{"idProduct", "0104"}, //Multifunction Composite Gadget¬
|
||||||
{"idProduct", config.UsbConfig.UsbProductId},
|
|
||||||
{"bcdDevice", "0100"},
|
{"bcdDevice", "0100"},
|
||||||
}
|
})
|
||||||
err = writeGadgetAttrs(kvmGadgetPath, gadgetAttrs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Infof("Successfully wrote gadget attributes: %s", gadgetAttrs)
|
|
||||||
gadgetStringsPath := filepath.Join(kvmGadgetPath, "strings", "0x409")
|
gadgetStringsPath := filepath.Join(kvmGadgetPath, "strings", "0x409")
|
||||||
err = os.MkdirAll(gadgetStringsPath, 0755)
|
err = os.MkdirAll(gadgetStringsPath, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
strAttrs := [][]string{
|
err = writeGadgetAttrs(gadgetStringsPath, [][]string{
|
||||||
{"serialnumber", config.UsbConfig.UsbSerialNumber},
|
{"serialnumber", GetDeviceID()},
|
||||||
{"manufacturer", config.UsbConfig.UsbManufacturer},
|
{"manufacturer", "JetKVM"},
|
||||||
{"product", config.UsbConfig.UsbName},
|
{"product", "JetKVM USB Emulation Device"},
|
||||||
}
|
})
|
||||||
err = writeGadgetAttrs(gadgetStringsPath, strAttrs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.Infof("Successfully wrote string attributes: %s", strAttrs)
|
|
||||||
|
|
||||||
configC1StringsPath := path.Join(configC1Path, "strings", "0x409")
|
configC1StringsPath := path.Join(configC1Path, "strings", "0x409")
|
||||||
err = os.MkdirAll(configC1StringsPath, 0755)
|
err = os.MkdirAll(configC1StringsPath, 0755)
|
||||||
|
@ -261,27 +254,11 @@ func writeGadgetConfig() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func rebindUsb() error {
|
func rebindUsb() error {
|
||||||
unbindErr := unbindUsb()
|
|
||||||
if unbindErr != nil {
|
|
||||||
return unbindErr
|
|
||||||
}
|
|
||||||
bindErr := bindUsb()
|
|
||||||
if bindErr != nil {
|
|
||||||
return bindErr
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func unbindUsb() error {
|
|
||||||
err := os.WriteFile("/sys/bus/platform/drivers/dwc3/unbind", []byte(udc), 0644)
|
err := os.WriteFile("/sys/bus/platform/drivers/dwc3/unbind", []byte(udc), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
err = os.WriteFile("/sys/bus/platform/drivers/dwc3/bind", []byte(udc), 0644)
|
||||||
}
|
|
||||||
|
|
||||||
func bindUsb() error {
|
|
||||||
err := os.WriteFile("/sys/bus/platform/drivers/dwc3/bind", []byte(udc), 0644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue