Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/jetkvm_usb_hid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
idf_component_register(
SRCS "jetkvm_usb_hid.c" "keyboard_rollover.c" "cdrom_toc.c"
INCLUDE_DIRS "include"
REQUIRES jetkvm_virtual_media esp_timer jetkvm_audio)
REQUIRES jetkvm_virtual_media esp_timer jetkvm_audio jetkvm_identity)

target_compile_options(${COMPONENT_LIB} PRIVATE -Werror)
14 changes: 9 additions & 5 deletions components/jetkvm_usb_hid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This component owns the target-facing USB device on the ESP32-P4 USB 2.0 high-sp
- `jetkvm_usb_hid_keyboard_report()`, `jetkvm_usb_hid_keypress()`, `jetkvm_usb_hid_absolute_mouse_report()`, `jetkvm_usb_hid_relative_mouse_report()`, `jetkvm_usb_hid_wheel_report()` and `jetkvm_usb_hid_release_all()` send input.
- `jetkvm_usb_hid_remote_wakeup()` wakes a suspended host.
- `jetkvm_usb_hid_get_state()` and `jetkvm_usb_hid_get_configuration_snapshot()` report status.
- `jetkvm_usb_hid_cdrom_drive_takes_medium()` tells `jetkvm_usb_service_present_medium()` whether a mount needs re-enumeration.
- `jetkvm_usb_hid_drive_takes_medium()` tells `jetkvm_usb_service_present_medium()` whether a mount needs re-enumeration.

## HID functions

Expand All @@ -31,12 +31,16 @@ Interface and TinyUSB instance numbers are assigned densely in that order, and e

## Virtual-media drive

The MSC interface has one logical unit that exposes only the mounted image, never the whole TF card. Local disk images whose size is a multiple of 512 bytes are writable; CD-ROM and HTTP images are read-only.
The MSC interface has two logical units that the host has at all times: a CD-ROM drive and a removable disk drive. Some boot firmware reads the first unit only, so the first unit is the drive of the image that is mounted when the USB driver starts, and the CD-ROM drive when nothing is mounted. Only the order with the CD-ROM drive first names the MSC interface, so the two orders have different configuration descriptors and a Linux host that kept the device across a port reset probes it again. The drives expose only the mounted image, never the whole TF card. Local disk images whose size is a multiple of 512 bytes are writable; CD-ROM and HTTP images are read-only.

The host reads the drive type from `INQUIRY` at enumeration and never asks again. The type is therefore fixed at each driver start: the type of the mounted image, or CD-ROM if nothing is mounted. Unmounting leaves the drive present without a medium.
The host reads each unit's type from `INQUIRY` at enumeration and never asks again, so a unit never changes its type. A mounted image is the medium of the drive of its type, and the other drive is empty. Unmounting leaves both drives present without a medium.

A CD-ROM image can replace the medium of an enumerated CD-ROM drive without re-enumeration, but only if the host polls the drive: it must have sent `TEST UNIT READY` or `GET EVENT STATUS NOTIFICATION` within the last 3 s and since the drive was last emptied. macOS stops polling a drive once it has held a medium, and hosts in boot firmware or suspend do not poll. Every other mount re-enumerates the device, which briefly removes all interfaces and ends any OS Services session.
A mount is a medium change, as when a card goes into a card reader. The USB device stays as it is, so the keyboard, the mice, audio and an OS Services session continue. The host finds the new medium when it polls the drive. Linux and Windows poll every second or two, with and without a medium, and macOS polls the disk drive. macOS stops polling the CD-ROM drive once it has found a medium in it, also after the Mini unmounts that medium, until something on the Mac ejects it. Hosts in boot firmware or suspend do not poll. So a mount re-enumerates the device when the host has not sent `TEST UNIT READY` or `GET EVENT STATUS NOTIFICATION` to the image's drive within the last 3 s, or has not polled it again since it found the drive's last medium. A re-enumeration briefly removes all interfaces and ends any OS Services session.

The drive reports each medium change once with UNIT ATTENTION 28h. Each mount and unmount advances the medium generation in `jetkvm_virtual_media`. Until the host has seen the current generation, `READ(10)`, `WRITE(10)` and `READ CAPACITY` fail, so a command meant for a previous image never touches the new one.
The drives reject `PREVENT ALLOW MEDIUM REMOVAL` with the prevent bits set, because the user removes the medium on the Mini, whatever the host asks. macOS stops polling a drive whose medium it has locked, and keeps polling a drive that rejects the lock.

Windows makes a drive of the second unit only for a device with a USB serial number string. A device with mass storage whose USB configuration has no serial number therefore reports its device ID, in upper case, as its serial number; the configuration keeps the empty value.

The drive reports each medium change once with UNIT ATTENTION 28h, and the host reads the capacity again. Linux sends `TEST UNIT READY` to a CD-ROM drive only when the drive is opened; its poll sends `GET EVENT STATUS NOTIFICATION`, which the CD-ROM drive answers with the same UNIT ATTENTION once per change. Each mount and unmount advances the medium generation in `jetkvm_virtual_media`. Until the host has seen the current generation, `READ(10)`, `WRITE(10)` and `READ CAPACITY` fail, so a command meant for a previous image never touches the new one.

Writable media advertise a write cache through the Caching mode page. The host then sends `SYNCHRONIZE CACHE` at fsync and eject, which succeeds only after the image syncs. MSC telemetry appears in `/webrtc/stats` under `usbHid` (`mscWrite`, `mediaWrite`, `mediaRead`).
17 changes: 8 additions & 9 deletions components/jetkvm_usb_hid/include/jetkvm_usb_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,15 @@ esp_err_t jetkvm_usb_hid_set_descriptor_configuration(
esp_err_t jetkvm_usb_hid_init(void);

/*
* True when the host finds a CD-ROM image that has just been mounted as a
* new medium of the drive that it has. Two things must hold. The host has
* the virtual-media drive as a CD-ROM drive: the target-port driver runs
* with mass storage, and the enumeration it started with presented a CD-ROM
* drive (a CD-ROM image was mounted then, or nothing). And the host asks the
* drive for its medium: it has asked in the last 3 s, and since the drive
* was last emptied. Every other mount needs a re-enumeration, which presents
* the drive again.
* True when the host finds an image that has just been mounted as a new
* medium of the drive of its type, the CD-ROM drive or the disk drive. Two
* things must hold: the target-port driver runs with mass storage, and the
* host asks that drive for its medium: it has asked in the last 3 s, and it
* has found the new medium already or asked again after it found the drive's
* last medium. A host in its boot firmware or a suspended host does not ask;
* the mount then needs a re-enumeration.
*/
bool jetkvm_usb_hid_cdrom_drive_takes_medium(void);
bool jetkvm_usb_hid_drive_takes_medium(bool cdrom_image);

/* Best-effort release of all keys/buttons followed by driver teardown. */
esp_err_t jetkvm_usb_hid_deinit(void);
Expand Down
Loading
Loading