Skip to content

Commit

Permalink
mofeng-git#46 修复 CD-ROM 与 FLASH 模式网页无法切换
Browse files Browse the repository at this point in the history
挂载 MSD 时重启 UDC 确保模式生效
  • Loading branch information
mofeng-git committed Nov 5, 2024
1 parent de5cb73 commit 72dce4d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions kvmd/plugins/hid/otg/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ def __ensure_device(self) -> bool:
flags |= (os.O_RDWR if self.__read_size else os.O_WRONLY)
self.__fd = os.open(self.__device_path, flags)
except Exception as err:
logger.error(self.gettext("Can't open HID-%s device %s: %s"),
self.__name, self.__device_path, tools.efmt(err))
#logger.error(self.gettext("Can't open HID-%s device %s: %s"),self.__name, self.__device_path, tools.efmt(err))
pass

if self.__fd >= 0:
try:
Expand Down
11 changes: 11 additions & 0 deletions kvmd/plugins/msd/otg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dataclasses
import functools
import time
import os

from typing import AsyncGenerator

Expand Down Expand Up @@ -259,6 +260,7 @@ async def set_params(

@aiotools.atomic_fg
async def set_connected(self, connected: bool) -> None:
print(self.__drive)
async with self.__state.busy():
assert self.__state.vd
if connected:
Expand All @@ -274,6 +276,15 @@ async def set_connected(self, connected: bool) -> None:

self.__drive.set_rw_flag(self.__state.vd.rw)
self.__drive.set_cdrom_flag(self.__state.vd.cdrom)
#reboot UDC to fix otg cd-rom and flash switch
udc_path = self.__drive.get_udc_path()
with open(udc_path) as file:
enabled = bool(file.read().strip())
if enabled:
with open(udc_path, "w") as file:
file.write("\n")
with open(udc_path, "w") as file:
file.write(sorted(os.listdir("/sys/class/udc"))[0])
if self.__state.vd.rw:
await self.__state.vd.image.remount_rw(True)
self.__drive.set_image_path(self.__state.vd.image.path)
Expand Down
4 changes: 4 additions & 0 deletions kvmd/plugins/msd/otg/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self) -> None:
class Drive:
def __init__(self, gadget: str, instance: int, lun: int) -> None:
func = f"mass_storage.usb{instance}"
self.__udc_path = os.path.join(f"/sys/kernel/config/usb_gadget", gadget, usb.G_UDC)
self.__profile_func_path = usb.get_gadget_path(gadget, usb.G_PROFILE, func)
self.__profile_path = usb.get_gadget_path(gadget, usb.G_PROFILE)
self.__lun_path = usb.get_gadget_path(gadget, usb.G_FUNCTIONS, func, f"lun.{lun}")
Expand All @@ -49,6 +50,9 @@ def is_enabled(self) -> bool:

def get_watchable_paths(self) -> list[str]:
return [self.__lun_path, self.__profile_path]

def get_udc_path(self) -> str:
return self.__udc_path

# =====

Expand Down

0 comments on commit 72dce4d

Please sign in to comment.