diff --git a/components/jetkvm_usb_hid/CMakeLists.txt b/components/jetkvm_usb_hid/CMakeLists.txt index fde3aeb6..f1b0f2f4 100644 --- a/components/jetkvm_usb_hid/CMakeLists.txt +++ b/components/jetkvm_usb_hid/CMakeLists.txt @@ -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) diff --git a/components/jetkvm_usb_hid/README.md b/components/jetkvm_usb_hid/README.md index 5fbfe39e..58c67dd1 100644 --- a/components/jetkvm_usb_hid/README.md +++ b/components/jetkvm_usb_hid/README.md @@ -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 @@ -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`). diff --git a/components/jetkvm_usb_hid/include/jetkvm_usb_hid.h b/components/jetkvm_usb_hid/include/jetkvm_usb_hid.h index 2e4a1faf..47bc3734 100644 --- a/components/jetkvm_usb_hid/include/jetkvm_usb_hid.h +++ b/components/jetkvm_usb_hid/include/jetkvm_usb_hid.h @@ -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); diff --git a/components/jetkvm_usb_hid/jetkvm_usb_hid.c b/components/jetkvm_usb_hid/jetkvm_usb_hid.c index 7e02701d..319d6120 100644 --- a/components/jetkvm_usb_hid/jetkvm_usb_hid.c +++ b/components/jetkvm_usb_hid/jetkvm_usb_hid.c @@ -9,7 +9,9 @@ #include "jetkvm_virtual_media.h" #include "cdrom_toc.h" #include "keyboard_rollover.h" +#include "jetkvm_identity.h" +#include #include #include @@ -495,6 +497,8 @@ static void msc_retain_block_count(uint32_t generation, uint32_t block_count) portEXIT_CRITICAL(&s_state_lock); } +static void msc_forget_host_asks_locked(void); + /* Called with s_state_lock held. Keep cumulative counters, but never carry a * partially observed BOT command across a detach/re-enumeration boundary. */ static void reset_msc_read_timing_locked(void) @@ -818,6 +822,24 @@ compiled_default_descriptor_configuration(void) }; } +/* The device ID in upper case, or empty when it cannot be read. */ +static const char *default_serial_number(void) +{ + static char serial_number[JETKVM_DEVICE_ID_CAPACITY]; + static bool read; + if (!read) { + if (jetkvm_device_id_get(serial_number, sizeof(serial_number)) != + ESP_OK) { + serial_number[0] = '\0'; + } + for (size_t i = 0U; serial_number[i] != '\0'; ++i) { + serial_number[i] = (char)toupper((unsigned char)serial_number[i]); + } + read = true; + } + return serial_number; +} + static esp_err_t copy_usb_string( char destination[USB_STRING_STORAGE_CAPACITY], const char *source) { @@ -1028,6 +1050,17 @@ static esp_err_t build_descriptor_bank( error = copy_usb_string(bank->string_storage[STRING_SERIAL], configuration->serial_number); } + /* With mass storage and without a configured serial number, the device + * reports the device ID as its serial number: Windows makes a drive of + * the second logical unit only for a device with one, and fails to + * enumerate a 2.1 device whose serial number string is empty. The + * configuration keeps it empty. */ + const char *serial_number = bank->string_storage[STRING_SERIAL]; + if (error == ESP_OK && configuration->mass_storage && + serial_number[0] == '\0' && default_serial_number()[0] != '\0') { + serial_number = default_serial_number(); + bank->string_descriptors[STRING_SERIAL] = serial_number; + } if (error == ESP_OK) { error = copy_usb_string(bank->string_storage[STRING_KEYBOARD], "JetKVM Keyboard"); @@ -1042,7 +1075,7 @@ static esp_err_t build_descriptor_bank( } if (error == ESP_OK) { error = copy_usb_string(bank->string_storage[STRING_MSC_CDROM], - "JetKVM Virtual CD-ROM"); + "JetKVM Virtual Media"); } if (error == ESP_OK) { error = copy_usb_string( @@ -1102,10 +1135,7 @@ static esp_err_t build_descriptor_bank( .bcdDevice = CONFIG_TINYUSB_DESC_BCD_DEVICE, .iManufacturer = STRING_MANUFACTURER, .iProduct = STRING_PRODUCT, - /* Windows fails to enumerate a 2.1 device whose serial number - * string is empty, so a device without one reports none. */ - .iSerialNumber = configuration->serial_number[0] != '\0' ? - STRING_SERIAL : 0, + .iSerialNumber = serial_number[0] != '\0' ? STRING_SERIAL : 0, .bNumConfigurations = 1, }; @@ -1229,6 +1259,7 @@ static void tinyusb_event_callback(tinyusb_event_t *event, void *arg) switch (event->id) { case TINYUSB_EVENT_ATTACHED: reset_msc_read_timing_locked(); + msc_forget_host_asks_locked(); s_ctx.state.connected = tud_connected(); s_ctx.state.mounted = true; s_ctx.state.suspended = false; @@ -1238,6 +1269,7 @@ static void tinyusb_event_callback(tinyusb_event_t *event, void *arg) break; case TINYUSB_EVENT_DETACHED: reset_msc_read_timing_locked(); + msc_forget_host_asks_locked(); s_ctx.state.connected = tud_connected(); s_ctx.state.mounted = false; s_ctx.state.suspended = false; @@ -1304,61 +1336,92 @@ uint8_t const *tud_hid_descriptor_report_cb(uint8_t instance) } } -/* The type of the virtual-media drive as the host has it (sr or sd). The - * host learns it from INQUIRY when it enumerates the device and does not ask - * again, so this changes only before a TinyUSB start: it is then the type of - * the mounted image, and CD-ROM with nothing mounted. An unmount leaves the - * drive on the host with its type and without a medium. A medium of the other - * type is not in the drive: its mount re-enumerates USB, and until then the - * drive is empty. */ -static jetkvm_virtual_media_mode_t s_msc_enumerated_mode; +/* The virtual-media interface has two drives that the host has all the time: + * a CD-ROM drive and a removable disk drive. The host learns a drive's type + * from INQUIRY when it enumerates the device and does not ask again, so a + * drive never changes its type. A mounted image is the medium of the drive of + * its type, and the other drive is empty. A mount or an unmount is a change + * of the medium, as in a card reader: the drive reports it, and the USB + * device stays as it is. + * + * Some boot firmware reads the first logical unit only, so the first unit is + * the drive of the image that is mounted when the driver starts, and the + * CD-ROM drive when nothing is mounted. msc_drive_start() sets it before + * TinyUSB runs; it stays for that enumeration. */ +#define MSC_LUN_COUNT 2U +static uint8_t s_msc_cdrom_lun; + +static jetkvm_virtual_media_mode_t msc_lun_mode(uint8_t lun) +{ + return lun == s_msc_cdrom_lun ? JETKVM_VIRTUAL_MEDIA_MODE_CDROM : + JETKVM_VIRTUAL_MEDIA_MODE_DISK; +} -/* The medium generation that the host has been told of, or 0. The host can - * read, write and size the medium only while this is the mounted medium. - * After a mount it is not: the host first gets "medium may have changed" and - * reads the new capacity. A command that the host sends for the previous - * image then fails and never reaches the new one. */ -static uint32_t s_msc_host_generation; +/* The medium generation that the host has been told of in each drive, or 0. + * The host can read, write and size the medium only while this is the + * mounted medium. After a mount it is not: the host first gets "medium may + * have changed" and reads the new capacity. A command that the host sends for + * the previous image then fails and never reaches the new one. */ +static uint32_t s_msc_host_generation[MSC_LUN_COUNT]; -/* The medium generation in the drive, or 0 for an empty drive, at the last - * GET EVENT STATUS NOTIFICATION that reported a change. */ +/* The medium generation in the CD-ROM drive, or 0 for an empty drive, at the + * last GET EVENT STATUS NOTIFICATION that reported a change. */ static uint32_t s_msc_event_generation; -/* When the host last asked the drive for its medium, with TEST UNIT READY or +/* When the host last asked each drive for its medium, with TEST UNIT READY or * GET EVENT STATUS NOTIFICATION, and the medium generation at that time. * Linux and Windows ask a drive every second or two, with and without a - * medium. macOS asks an empty drive twice per second and stops once the drive - * has a medium, also after that medium is gone; a computer in its boot - * firmware and a suspended host do not ask either. Only a host that asks - * finds a new medium in a drive that it keeps. Under s_state_lock; not asked - * since the last TinyUSB start. */ + * medium, and so does macOS for the disk drive. macOS stops asking the + * CD-ROM drive once it has found a medium in it, also after that medium is + * gone, and a computer in its boot firmware and a suspended host do not ask. + * Only a host that asks finds a new medium in a drive that it keeps. + * s_msc_found[lun] is the last medium that the host found in the drive, and + * s_msc_found_ms[lun] when it found it. Under s_state_lock; not asked since + * the last TinyUSB start or since the host attached. */ #define MSC_HOST_ASKS_WITHIN_MS 3000U -static bool s_msc_asked; -static uint32_t s_msc_asked_ms; -static uint32_t s_msc_asked_generation; +#define MSC_HOST_KEEPS_ASKING_MS 1000U +static bool s_msc_asked[MSC_LUN_COUNT]; +static uint32_t s_msc_asked_ms[MSC_LUN_COUNT]; +static uint32_t s_msc_asked_generation[MSC_LUN_COUNT]; +static uint32_t s_msc_found[MSC_LUN_COUNT]; +static uint32_t s_msc_found_ms[MSC_LUN_COUNT]; + +/* Called with s_state_lock held, at a TinyUSB start and when a host attaches + * or detaches: what one host asked says nothing about the next one. */ +static void msc_forget_host_asks_locked(void) +{ + memset(s_msc_asked, 0, sizeof(s_msc_asked)); + memset(s_msc_found, 0, sizeof(s_msc_found)); +} -static void msc_host_asked(void) +/* drive_generation: the generation of the medium in the drive, or 0. */ +static void msc_host_asked(uint8_t lun, uint32_t drive_generation) { const uint32_t generation = jetkvm_virtual_media_medium_generation(); const uint32_t now_ms = (uint32_t)(esp_timer_get_time() / 1000); portENTER_CRITICAL(&s_state_lock); - s_msc_asked = true; - s_msc_asked_ms = now_ms; - s_msc_asked_generation = generation; + s_msc_asked[lun] = true; + s_msc_asked_ms[lun] = now_ms; + s_msc_asked_generation[lun] = generation; + if (drive_generation != 0U && drive_generation != s_msc_found[lun]) { + s_msc_found[lun] = drive_generation; + s_msc_found_ms[lun] = now_ms; + } portEXIT_CRITICAL(&s_state_lock); } -/* What the drive holds, as of medium generation s_msc_drive_looked: the - * generation of its medium, or 0 when it is empty. */ +/* The mounted medium as of medium generation s_msc_drive_looked: its + * generation, or 0 when nothing is mounted, and its type. */ static uint32_t s_msc_drive_looked; static uint32_t s_msc_drive_found; +static jetkvm_virtual_media_mode_t s_msc_drive_found_mode; /* The generation of the medium in the drive, or 0 for an empty drive. A host - * asks the drive every second or two, so this takes the medium lock only - * when the generation has advanced since it last looked: what the drive - * holds changes with the generation only. For TinyUSB's task, and for the - * USB owner before it starts TinyUSB. */ -static uint32_t msc_drive_generation(void) + * asks each drive every second or two, so this takes the medium lock only + * when the generation has advanced since it last looked: what the drives + * hold changes with the generation only. For TinyUSB's task, and for the USB + * owner before it starts TinyUSB. */ +static uint32_t msc_drive_generation(uint8_t lun) { const uint32_t generation = jetkvm_virtual_media_medium_generation(); if (generation != s_msc_drive_looked) { @@ -1367,52 +1430,54 @@ static uint32_t msc_drive_generation(void) return 0U; } s_msc_drive_found = - state.image_mounted && state.mode == s_msc_enumerated_mode ? - state.medium_generation : 0U; + state.image_mounted ? state.medium_generation : 0U; + s_msc_drive_found_mode = state.mode; /* A state newer than this generation is looked up once more. */ s_msc_drive_looked = generation; } - return s_msc_drive_found; + return s_msc_drive_found_mode == msc_lun_mode(lun) ? s_msc_drive_found : + 0U; } /* The block size of every medium of the drive, as * jetkvm_virtual_media_capacity() reports it for the drive's type. */ -static uint16_t msc_drive_block_size(void) +static uint16_t msc_drive_block_size(uint8_t lun) { - return s_msc_enumerated_mode == JETKVM_VIRTUAL_MEDIA_MODE_CDROM ? - 2048U : JETKVM_VIRTUAL_MEDIA_BLOCK_SIZE; + return lun == s_msc_cdrom_lun ? 2048U : JETKVM_VIRTUAL_MEDIA_BLOCK_SIZE; } /* The generation of the medium in the drive when the host has been told of * it, else 0. Takes no lock: READ(10) asks for every endpoint fragment. */ static uint32_t msc_host_medium(uint8_t lun) { - if (lun != 0U) { + if (lun >= MSC_LUN_COUNT) { return 0U; } const uint32_t generation = jetkvm_virtual_media_medium_generation(); - return generation == __atomic_load_n(&s_msc_host_generation, + return generation == __atomic_load_n(&s_msc_host_generation[lun], __ATOMIC_ACQUIRE) ? generation : 0U; } -/* The number of logical units: one. TinyUSB answers GET MAX LUN with this - * number minus one, and stalls the request when it is 0. Windows clears that - * stall with CLEAR_FEATURE(ENDPOINT_HALT) for endpoint 0, which TinyUSB does - * not answer, and waits 5 s for it three times before it starts the drive. */ +/* The number of logical units. TinyUSB answers GET MAX LUN with this number + * minus one; with 0 it would stall the request, which Windows clears with a + * CLEAR_FEATURE for endpoint 0 that TinyUSB does not answer. Windows makes a + * drive of a unit other than the first only for a device with a serial + * number string. */ uint8_t tud_msc_get_maxlun_cb(void) { - return 1U; + return MSC_LUN_COUNT; } uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *response, uint32_t buffer_size) { - if (lun != 0U || response == NULL || buffer_size < sizeof(*response)) { + if (lun >= MSC_LUN_COUNT || response == NULL || + buffer_size < sizeof(*response)) { return 0U; } response->peripheral_device_type = - s_msc_enumerated_mode == JETKVM_VIRTUAL_MEDIA_MODE_CDROM ? 0x05U : 0x00U; + lun == s_msc_cdrom_lun ? 0x05U : 0x00U; response->is_removable = 1U; memcpy(response->vendor_id, "JetKVM ", sizeof(response->vendor_id)); memcpy(response->product_id, "Virtual Media ", @@ -1430,32 +1495,34 @@ void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], memcpy(product_rev, "1.0 ", 4U); } -/* Set before each TinyUSB start; the TinyUSB task clears it. A host that - * kept the device across a re-enumeration can still have a previous - * capacity. */ -static bool s_msc_started; +/* Set for each drive before each TinyUSB start; the TinyUSB task clears it. + * A host that kept the device across a re-enumeration can still have a + * previous capacity. */ +static bool s_msc_started[MSC_LUN_COUNT]; bool tud_msc_test_unit_ready_cb(uint8_t lun) { - if (lun == 0U) { - msc_host_asked(); + if (lun >= MSC_LUN_COUNT) { + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x25U, 0x00U); + return false; } - const uint32_t generation = lun == 0U ? msc_drive_generation() : 0U; + const uint32_t generation = msc_drive_generation(lun); + msc_host_asked(lun, generation); const bool ready = generation != 0U; - const bool changed = ready && generation != s_msc_host_generation; + const bool changed = ready && generation != s_msc_host_generation[lun]; if (changed) { - __atomic_store_n(&s_msc_host_generation, generation, + __atomic_store_n(&s_msc_host_generation[lun], generation, __ATOMIC_RELEASE); } if (!ready) { tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3aU, 0x00U); return false; } - if (changed || s_msc_started) { + if (changed || s_msc_started[lun]) { /* "Medium may have changed" makes the host read the capacity again * and drop what it kept of the previous medium, as after a card * swap. */ - s_msc_started = false; + s_msc_started[lun] = false; tud_msc_set_sense(lun, SCSI_SENSE_UNIT_ATTENTION, 0x28U, 0x00U); return false; } @@ -1502,7 +1569,7 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) { (void)power_condition; - if (lun != 0U) { + if (lun >= MSC_LUN_COUNT) { return false; } /* An empty drive has nothing to eject or to flush, and neither has a @@ -1525,6 +1592,24 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, return true; } +/* The drives cannot lock their medium: the user mounts and unmounts images + * on the Mini, whatever the host asks, as a card is taken out of a card + * reader. So a drive rejects the host's request to lock it. macOS stops + * asking a drive whose lock it holds, and would not find the medium gone or + * the next one; a drive that rejects the lock it keeps asking, as it asks a + * card reader. Linux and Windows go on without the lock. */ +bool tud_msc_prevent_allow_medium_removal_cb(uint8_t lun, + uint8_t prohibit_removal, + uint8_t control) +{ + (void)control; + if (lun < MSC_LUN_COUNT && prohibit_removal == 0U) { + return true; + } + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x24U, 0x00U); + return false; +} + static int32_t msc_read(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t buffer_size) { @@ -1559,7 +1644,7 @@ static int32_t msc_read(uint8_t lun, uint32_t lba, uint32_t offset, portEXIT_CRITICAL(&s_state_lock); const uint32_t generation = msc_host_medium(lun); - const uint16_t block_size = msc_drive_block_size(); + const uint16_t block_size = msc_drive_block_size(lun); uint32_t block_count = msc_block_count(generation); if (generation != 0U && block_count == 0U) { uint32_t refreshed_count = 0U; @@ -1756,7 +1841,7 @@ void tud_msc_read10_complete_cb(uint8_t lun) { int64_t completed_us = esp_timer_get_time(); portENTER_CRITICAL(&s_state_lock); - if (lun == 0U && s_msc_read_command_active && + if (s_msc_read_command_active && completed_us >= s_msc_command_started_us) { uint32_t elapsed_us = (uint32_t)(completed_us - s_msc_command_started_us); @@ -1780,7 +1865,7 @@ void tud_msc_read10_complete_cb(uint8_t lun) void tud_msc_write10_complete_cb(uint8_t lun) { - if (lun != 0U) { + if (lun >= MSC_LUN_COUNT) { return; } int64_t completed_us = esp_timer_get_time(); @@ -1804,7 +1889,7 @@ void tud_msc_scsi_complete_cb(uint8_t lun, const uint8_t scsi_command[16]) { (void)scsi_command; - if (lun != 0U) { + if (lun >= MSC_LUN_COUNT) { return; } portENTER_CRITICAL(&s_state_lock); @@ -1832,7 +1917,7 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, portEXIT_CRITICAL(&s_state_lock); const uint32_t generation = msc_host_medium(lun); - const uint16_t block_size = msc_drive_block_size(); + const uint16_t block_size = msc_drive_block_size(lun); uint32_t block_count = msc_block_count(generation); if (generation != 0U && block_count == 0U) { uint32_t refreshed_count = 0U; @@ -1889,7 +1974,7 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_command[16], void *buffer, uint16_t buffer_size) { - if (lun == 0U && scsi_command != NULL) { + if (lun < MSC_LUN_COUNT && scsi_command != NULL) { /* Linux normally probes MODE SENSE(6), which TinyUSB handles. Some * hosts prefer MODE SENSE(10), so report the same caching page here. */ if (scsi_command[0] == 0x5aU && buffer != NULL && buffer_size > 0U) { @@ -1929,18 +2014,14 @@ int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_command[16], tud_msc_set_sense(lun, SCSI_SENSE_MEDIUM_ERROR, 0x0cU, 0x00U); return -1; } - if (scsi_command[0] == SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL) { - return 0; - } /* Linux sends TEST UNIT READY to a CD-ROM drive only when the drive * is opened. Its poll, every 2 s, sends GET EVENT STATUS * NOTIFICATION, and takes UNIT ATTENTION as a changed medium, on * which it opens the drive. The CD-ROM drive gives that answer once * for each medium that came or went, and else rejects the command. */ - if (scsi_command[0] == 0x4aU && - s_msc_enumerated_mode == JETKVM_VIRTUAL_MEDIA_MODE_CDROM) { - msc_host_asked(); - const uint32_t generation = msc_drive_generation(); + if (scsi_command[0] == 0x4aU && lun == s_msc_cdrom_lun) { + const uint32_t generation = msc_drive_generation(lun); + msc_host_asked(lun, generation); if (generation != s_msc_event_generation) { s_msc_event_generation = generation; tud_msc_set_sense(lun, SCSI_SENSE_UNIT_ATTENTION, 0x28U, @@ -1951,7 +2032,7 @@ int32_t tud_msc_scsi_cb(uint8_t lun, const uint8_t scsi_command[16], /* macOS reads the TOC to find the data track; without it, the disc * has no partition to mount. */ if (scsi_command[0] == 0x43U && buffer != NULL && - s_msc_enumerated_mode == JETKVM_VIRTUAL_MEDIA_MODE_CDROM) { + lun == s_msc_cdrom_lun) { uint32_t block_count = 0U; uint16_t block_size = 0U; tud_msc_capacity_cb(lun, &block_count, &block_size); @@ -2149,48 +2230,54 @@ esp_err_t jetkvm_usb_hid_set_descriptor_configuration( return select_descriptor_configuration(configuration); } -/* Before TinyUSB starts: record the drive type that this enumeration - * presents, and return the name of the MSC interface, which only a CD-ROM - * drive has. A host that enumerates the device learns the drive's medium from - * the first commands, so it has the mounted medium from the start. - * Linux keeps a device across a port reset when its descriptors are - * unchanged. usb-storage resets the port when a command fails during the - * re-enumeration detach; that reset can consume the connect change, and the - * host then keeps the previous SCSI device type (sd or sr). A different - * configuration descriptor makes Linux drop and re-probe the device. */ -static uint8_t msc_drive_start(void) +/* Before TinyUSB starts: a host that enumerates the device learns each + * drive's medium from the first commands, so it has the mounted medium from + * the start. */ +static void msc_drive_start(void) { - s_msc_started = true; + for (uint8_t lun = 0U; lun < MSC_LUN_COUNT; ++lun) { + s_msc_started[lun] = true; + } const uint32_t generation = jetkvm_virtual_media_medium_generation(); jetkvm_virtual_media_state_t state = {0}; (void)jetkvm_virtual_media_get_state(&state); + s_msc_cdrom_lun = + state.image_mounted && state.mode == JETKVM_VIRTUAL_MEDIA_MODE_DISK ? + 1U : 0U; portENTER_CRITICAL(&s_state_lock); - s_msc_enumerated_mode = state.mode; - s_msc_asked = false; + msc_forget_host_asks_locked(); portEXIT_CRITICAL(&s_state_lock); s_msc_drive_found = state.image_mounted ? state.medium_generation : 0U; + s_msc_drive_found_mode = state.mode; s_msc_drive_looked = generation; - __atomic_store_n(&s_msc_host_generation, s_msc_drive_found, - __ATOMIC_RELEASE); - s_msc_event_generation = s_msc_drive_found; - return state.mode == JETKVM_VIRTUAL_MEDIA_MODE_CDROM ? - STRING_MSC_CDROM : 0U; + for (uint8_t lun = 0U; lun < MSC_LUN_COUNT; ++lun) { + __atomic_store_n(&s_msc_host_generation[lun], + msc_drive_generation(lun), __ATOMIC_RELEASE); + } + s_msc_event_generation = msc_drive_generation(s_msc_cdrom_lun); } -bool jetkvm_usb_hid_cdrom_drive_takes_medium(void) +bool jetkvm_usb_hid_drive_takes_medium(bool cdrom_image) { - /* A mount needs an empty drive, so the generation before the one of a - * mounted image is the one of the empty drive. A host that asked then, or - * later, asks a drive in the state that this one was in. */ + /* The host is not suspended, it asked the drive in the last 3 s, and it + * has found the new medium already or asks a drive with a medium too: it + * asked again at least 1 s after it found its last medium there, or after + * that medium was gone (a mount needs an empty drive, so that medium's + * unmount was the next generation). */ + const uint8_t lun = cdrom_image ? s_msc_cdrom_lun : 1U - s_msc_cdrom_lun; const uint32_t generation = jetkvm_virtual_media_medium_generation(); const uint32_t now_ms = (uint32_t)(esp_timer_get_time() / 1000); portENTER_CRITICAL(&s_state_lock); + const bool keeps_asking = + s_msc_found[lun] == 0U || s_msc_found[lun] == generation || + (int32_t)(s_msc_asked_generation[lun] - s_msc_found[lun]) > 0 || + s_msc_asked_ms[lun] - s_msc_found_ms[lun] >= MSC_HOST_KEEPS_ASKING_MS; const bool takes = s_ctx.state.initialized && !s_ctx.stopping && + !s_ctx.state.suspended && s_descriptor_banks[s_active_descriptor_bank].mass_storage && - s_msc_enumerated_mode == JETKVM_VIRTUAL_MEDIA_MODE_CDROM && - s_msc_asked && generation - s_msc_asked_generation <= 1U && - now_ms - s_msc_asked_ms <= MSC_HOST_ASKS_WITHIN_MS; + s_msc_asked[lun] && keeps_asking && + now_ms - s_msc_asked_ms[lun] <= MSC_HOST_ASKS_WITHIN_MS; portEXIT_CRITICAL(&s_state_lock); return takes; } @@ -2259,9 +2346,18 @@ esp_err_t jetkvm_usb_hid_init(void) hid_descriptor_bank_t *descriptor_bank = &s_descriptor_banks[descriptor_bank_index]; if (descriptor_bank->mass_storage) { - /* The initializing flag keeps select_descriptor_configuration() away - * from this bank while the descriptors are rebuilt. */ - descriptor_bank->mass_storage_string = msc_drive_start(); + msc_drive_start(); + /* Linux keeps a device across a port reset when its descriptors are + * unchanged, and with it the SCSI type of each logical unit. + * usb-storage resets the port when a command fails during the + * re-enumeration detach, and that reset can consume the connect + * change. The MSC interface has its name only when the CD-ROM drive + * is the first unit, so the other order has another configuration + * descriptor and Linux probes the device again. The initializing + * flag keeps select_descriptor_configuration() away from this bank + * while the descriptors are rebuilt. */ + descriptor_bank->mass_storage_string = + s_msc_cdrom_lun == 0U ? STRING_MSC_CDROM : 0U; error = build_configuration_descriptor( descriptor_bank, HID_FS_POLL_INTERVAL, descriptor_bank->full_speed_configuration); diff --git a/components/jetkvm_usb_hid/tests/test_dynamic_descriptors_static.py b/components/jetkvm_usb_hid/tests/test_dynamic_descriptors_static.py index 6b357a12..b5cb525a 100644 --- a/components/jetkvm_usb_hid/tests/test_dynamic_descriptors_static.py +++ b/components/jetkvm_usb_hid/tests/test_dynamic_descriptors_static.py @@ -153,12 +153,16 @@ def body(symbol: str) -> str: assert forbidden not in SOURCE, f"target HID component touches forbidden {forbidden}" # Windows fails to enumerate a USB 2.1 device (OS Services) whose serial -# number string is empty, so an empty serial number reports no string. +# number string is empty, and makes a drive of the second logical unit only +# for a device with a serial number. Without a configured one a device with +# mass storage reports its device ID, and never an empty string. bank_builder = body("build_descriptor_bank") +assert "default_serial_number()" in bank_builder, \ + "an empty serial number must fall back to the device ID" assert re.search( - r"\.iSerialNumber\s*=\s*configuration->serial_number\[0\]\s*!=\s*'\\0'\s*\?\s*" + r"\.iSerialNumber\s*=\s*serial_number\[0\]\s*!=\s*'\\0'\s*\?\s*" r"STRING_SERIAL\s*:\s*0", bank_builder -), "an empty serial number must give iSerialNumber 0" +), "an empty serial number string must give iSerialNumber 0" # The configuration descriptor names the configuration string, and an empty # configuration string gives iConfiguration 0, as for the serial number. diff --git a/components/jetkvm_usb_service/include/jetkvm_usb_service.h b/components/jetkvm_usb_service/include/jetkvm_usb_service.h index 731b0027..f42ae7d2 100644 --- a/components/jetkvm_usb_service/include/jetkvm_usb_service.h +++ b/components/jetkvm_usb_service/include/jetkvm_usb_service.h @@ -112,13 +112,12 @@ jetkvm_usb_service_configuration_result_t jetkvm_usb_service_reenumerate(void); /** - * Make the host find the image that has just been mounted. A CD-ROM image is - * a new medium of a drive that the host has as a CD-ROM drive and asks for - * its medium: the drive reports the change, and the USB device stays as it - * is. Every other mount re-enumerates as jetkvm_usb_service_reenumerate() - * does: a change of the drive's type needs it on every host, and macOS stops - * asking a drive that has had a medium, so it finds the next image only in a - * drive that it has enumerated again. + * Make the host find the image that has just been mounted. The image is a new + * medium of the drive of its type, the CD-ROM drive or the disk drive, which + * the host has all the time: when the host asks that drive for its medium, + * the drive reports the change and the USB device stays as it is. A host + * that does not ask, such as a computer in its boot firmware, finds the image + * only after a re-enumeration as jetkvm_usb_service_reenumerate() does. */ jetkvm_usb_service_configuration_result_t jetkvm_usb_service_present_medium(bool cdrom_image); diff --git a/components/jetkvm_usb_service/jetkvm_usb_service.c b/components/jetkvm_usb_service/jetkvm_usb_service.c index 619283a6..c434229e 100644 --- a/components/jetkvm_usb_service/jetkvm_usb_service.c +++ b/components/jetkvm_usb_service/jetkvm_usb_service.c @@ -843,7 +843,7 @@ jetkvm_usb_service_reenumerate(void) jetkvm_usb_service_configuration_result_t jetkvm_usb_service_present_medium(bool cdrom_image) { - if (cdrom_image && jetkvm_usb_hid_cdrom_drive_takes_medium()) { + if (jetkvm_usb_hid_drive_takes_medium(cdrom_image)) { return JETKVM_USB_SERVICE_CONFIGURATION_COMPLETED; } return jetkvm_usb_service_reenumerate(); diff --git a/components/webrtc_transport/webrtc_transport_rpc_adapters.c b/components/webrtc_transport/webrtc_transport_rpc_adapters.c index 9ec628aa..ee078826 100644 --- a/components/webrtc_transport/webrtc_transport_rpc_adapters.c +++ b/components/webrtc_transport/webrtc_transport_rpc_adapters.c @@ -351,9 +351,9 @@ rpc_format_virtual_media_storage_adapter(jetkvm_control_storage_space_t *space, } } -/* The host gets the mounted image as a new medium of its CD-ROM drive, or - * as a drive that it enumerates again. A mount that the host cannot be given - * is taken back. */ +/* The host gets the mounted image as a new medium of the drive of its type, + * or after it enumerates the device again. A mount that the host cannot be + * given is taken back. */ static jetkvm_virtual_media_change_result_t present_mounted_medium(jetkvm_control_virtual_media_mode_t mode) { diff --git a/frontend/jetkvm-ui/e2e/mini/https-image-read.ts b/frontend/jetkvm-ui/e2e/mini/https-image-read.ts index f0440b26..1a7ad147 100644 --- a/frontend/jetkvm-ui/e2e/mini/https-image-read.ts +++ b/frontend/jetkvm-ui/e2e/mini/https-image-read.ts @@ -33,9 +33,8 @@ function quote(value: string): string { } /** - * Mount the image as the Mini's CD-ROM and wait for the host to see it; - * returns the host's block device. A mount re-enumerates the Mini's USB - * device on the host. + * Mount the image as the Mini's CD-ROM and wait for the host to have the + * CD-ROM drive; returns the host's block device. */ export async function mountHttpsImage(page: Page): Promise { await callJsonRpc(page, "unmountImage").catch(() => undefined); diff --git a/tools/combined_load/KNOWN_FAILURES.md b/tools/combined_load/KNOWN_FAILURES.md index e702bfd9..52387486 100644 --- a/tools/combined_load/KNOWN_FAILURES.md +++ b/tools/combined_load/KNOWN_FAILURES.md @@ -34,27 +34,30 @@ To reproduce, run `tools/combined_load/run.sh --list=full` and look at t -## kf-268: A mount that re-enumerates USB ends the OS Services session and stops service video for 6 s +## kf-268: A mount for a host that does not poll the drive ends the OS Services session and stops service video for 6 s ### Problem -When a user mounts an image while the browser shows service video, video stops for about 6 s and the OS Services session ends and comes back. The terminal and file transfer pause, a file in transfer and the clipboard round fail, and about 0.8 s of keys and mouse moves are lost. Then video has a few freezes of about 0.3 s. With HDMI capture as the source, a mount does not stop video. An unmount never does. +The Mini gives the host a CD-ROM drive and a disk drive at all times, and a mount is a medium change of the drive of the image's type. A host that polls the drive finds the new medium, and USB and the OS Services session stay as they are. -To reproduce, run `tools/combined_load/run.sh --list=full` and look at mount changes (`tfMountCdrom`, `tfMountDisk`, `httpMountDisk`, `httpsMountDisk`) while `osVideo` is on. +When the host has not polled the drive in the last 3 s, the Mini enumerates again so that the host finds the image. The host then removes all of the Mini's USB interfaces, OS Services included. Service video stops for about 6 s, the terminal and file transfer pause, a file in transfer and the clipboard round fail, and about 0.8 s of keys and mouse moves are lost. -### Cause +Known cases: -The Mini is one USB device with eight interfaces, one of them mass storage with a single drive. The host learns from `INQUIRY` whether the drive is a CD-ROM drive or a disk, and learns a new type only by enumerating again. So a mount that changes the type calls `jetkvm_usb_service_reenumerate()`, and the host removes all eight interfaces, OS Services included. A disk mount re-enumerates even with the same type, because macOS stops asking a disk drive for its medium after it has had one. Only a CD-ROM image into a CD-ROM drive that the host polls is a medium change without re-enumeration. +- A computer in its boot firmware, and a suspended host. +- macOS: after the Mini unmounts a CD-ROM that the Mac did not eject, the next CD-ROM image needs a re-enumeration. -With a Linux PC as host, the USB outage is about 0.8 s. The sender takes the rest: about 2 s until systemd restarts it, and about 4 s until it registers video again. With a Windows PC as host, a re-enumerating mount takes about 3 s. +To reproduce, run `tools/combined_load/run.sh --list=full` and look at mount changes (`tfMountCdrom`, `tfMountDisk`, `httpMountDisk`, `httpsMountDisk`) while `osVideo` is on. ### What is known -A proposed change gives the host a CD-ROM drive and a disk drive at all times and rejects the host's medium lock. With it, the session stayed up for every mount of the walk on Linux, macOS and Windows. One macOS case remains: after the Mini unmounts a CD-ROM that the Mac did not eject, the next CD-ROM image needs a re-enumeration. +With a Linux PC as host, a full walk on each board had no breach that these entries tolerate: every mount kept the session. + +When the Mini does enumerate again, the USB outage on a Linux PC is about 0.8 s, and the sender takes the rest: about 2 s until systemd restarts it, and about 4 s until it registers video again. With a Windows PC as host, a re-enumerating mount takes about 3 s. ### Next steps -- Review and measure that change on all three host systems. +- Run the full walk with a macOS host and with a Windows host, and remove the entries that no longer occur. - Make the sender reconnect and register video faster after a re-enumeration. @@ -434,7 +437,7 @@ To reproduce, run `tools/combined_load/run.sh --list=full` and look at ` ### Problem -A mount that re-enumerates USB ends the OS Services session ([kf-268](#kf-268)). The session usually comes back in 6 to 10 s. In 2 of 39 full walks it did not come back: service video stayed off, the terminal and the file transfer sent nothing, and the clipboard round stayed wrong. It happened once on each board, after `tfMountCdrom on` and after `httpMountDisk on`, with a Linux PC as host. +A mount that re-enumerates USB ends the OS Services session ([kf-268](#kf-268)). The session usually comes back in 6 to 10 s. In 2 of 39 full walks, from before the Mini had two drives, it did not come back: service video stayed off, the terminal and the file transfer sent nothing, and the clipboard round stayed wrong. It happened once on each board, after `tfMountCdrom on` and after `httpMountDisk on`, with a Linux PC as host. In both walks the host's kernel log has the USB disconnect of the Mini at the mount and a new enumeration 2 s later. Which side then fails to start the session again, the sender or the Mini, is not known. @@ -445,4 +448,4 @@ To reproduce, run `tools/combined_load/run.sh --list=full` several times ### Next steps - Keep the sender's log and the Mini's console of such a walk, and find which side does not reconnect. -- Check whether the change proposed in [kf-268](#kf-268), which removes the re-enumeration, removes this fault too. +- Check whether the fault still occurs now that a mount keeps USB for a host that polls the drive ([kf-268](#kf-268)). diff --git a/tools/combined_load/runner/hosts.mjs b/tools/combined_load/runner/hosts.mjs index 529da51c..56f3ae34 100644 --- a/tools/combined_load/runner/hosts.mjs +++ b/tools/combined_load/runner/hosts.mjs @@ -103,8 +103,10 @@ export const HID_KEYS = [ ]; const usageByCode = code => `{${HID_KEYS.map(key => `${key[code]}: ${key.usage}`).join(', ')}}`; +// The evdev names have the Mini's USB serial number, when it has one, after +// the product's name. const LINUX_KEY_WATCHER = ` -import select, struct, time +import glob, select, struct, time keys = ${usageByCode('linux')} base = "/dev/input/by-id/usb-JetKVM_USB_Emulation_Device-" names = ["event-kbd", "if01-event-mouse", "if02-event-mouse"] @@ -117,7 +119,8 @@ while True: for name in names: if name not in devices: try: - devices[name] = open(base + name, "rb", buffering=0) + found = glob.glob(base[:-1] + "_*-" + name) or [base + name] + devices[name] = open(found[0], "rb", buffering=0) except OSError: pass if not devices: diff --git a/tools/integration/tests/mounted_image.test.mjs b/tools/integration/tests/mounted_image.test.mjs index 4af27994..824752aa 100644 --- a/tools/integration/tests/mounted_image.test.mjs +++ b/tools/integration/tests/mounted_image.test.mjs @@ -2,7 +2,6 @@ import assert from 'node:assert/strict'; import { randomBytes } from 'node:crypto'; import http from 'node:http'; import { test } from 'node:test'; -import { setTimeout as sleep } from 'node:timers/promises'; import { sha256 } from '../../combined_load/runner/content.mjs'; import { addressTowards } from '../../combined_load/runner/switches.mjs'; import { hostAgent } from '../lib/host.mjs'; @@ -170,9 +169,42 @@ test('a disk image whose size is not a whole number of 512-byte blocks mounts, a assert.ok(Date.now() - startedAt <= 60_000, 'the test takes 60 seconds at most'); }); +// The Mini's drives on the host as "@": type 0 is a +// disk drive, type 5 a CD-ROM drive. +const UNITS = 'for d in /sys/bus/scsi/devices/*:*:*:*; do [ "$(tr -d " " < $d/vendor 2>/dev/null)" = JetKVM ] && ' + + 'echo "$(cat $d/type)@${d##*:}"; done | sort | tr "\\n" " "; true'; + +test('the first logical unit is the drive of the image that is mounted when USB turns on', { timeout: 300_000 }, async t => { + const filename = `integration-first-unit-${Date.now()}.img`; + const found = await bench(t, [filename]); + if (!found) return; + const { host, mini } = found; + const data = randomBytes(MIB); + await upload(mini, filename, data); + const hostHas = (units, what) => until(async () => (await host.exec(UNITS)).trim(), have => have === units, 45) + .catch(e => assert.fail(`${what} within 45 seconds: ${e.message}`)); + const usbOffAndOn = async () => { + await mini.rpc('setUsbEmulationState', { enabled: false }); + await mini.rpc('setUsbEmulationState', { enabled: true }); + await host.inputDevices(INPUT_DEVICES, 30); + }; + + // The order is from the last time that USB turned on. + await usbOffAndOn(); + await hostHas('0@1 5@0', 'with nothing mounted, the CD-ROM drive is unit 0'); + await mini.rpc('mountWithStorage', { filename, mode: 'Disk' }, 60_000); + await hostReads(host, data, 'the host reads the image after the mount'); + await usbOffAndOn(); + await hostHas('0@0 5@1', 'with a disk image mounted, the disk drive is unit 0'); + await hostReads(host, data, 'the host reads the image from unit 0'); + await mini.rpc('unmountImage', {}, 60_000); + await usbOffAndOn(); + await hostHas('0@1 5@0', 'after the unmount, the CD-ROM drive is unit 0 again'); +}); + // PREVENT MEDIUM REMOVAL (eject -i on) stands in for a disc mounted on the -// host. Must stay the last test: it leaves the drive in CD-ROM mode. -test('an unmount succeeds while the host holds the CD-ROM drive locked', { timeout: 300_000 }, async t => { +// host. The drive rejects the lock. +test('an unmount succeeds after the host has asked to lock the CD-ROM drive', { timeout: 300_000 }, async t => { const found = await bench(t); if (!found) return; const { host, mini, undo } = found; @@ -185,13 +217,12 @@ test('an unmount succeeds while the host holds the CD-ROM drive locked', { timeo await hostGetsKey('the host gets a key before the mount'); await mini.rpc('mountWithHTTP', { url: server.url, mode: 'CDROM' }, 60_000); assert.equal((await mini.rpc('getVirtualMediaState'))?.mode, 'CDROM'); - // The host enumerates the Mini again after the mount. - await sleep(5000); const drive = (await until( () => host.exec(`for d in $(${drives('rom')}); do s=$(sudo -n blockdev --getsize64 "$d" 2>/dev/null) && [ "\${s:-0}" -gt 0 ] && echo "$d"; done; true`), out => out.trim() !== '', 45).catch(e => assert.fail(`the host has the Mini's CD-ROM drive with a medium within 45 seconds: ${e.message}`))) .trim().split('\n')[0]; - await host.exec(`sudo -n eject -i on ${drive}`); + // eject can report the rejected lock as a failure. + await host.exec(`sudo -n eject -i on ${drive} 2>/dev/null; true`); // The drive can be gone, or another one, after the unmount. undo.push(() => host.exec(`sudo -n eject -i off ${drive} 2>/dev/null; true`)); @@ -199,8 +230,8 @@ test('an unmount succeeds while the host holds the CD-ROM drive locked', { timeo assert.equal(await mini.rpc('getVirtualMediaState'), null, 'nothing is mounted after the unmount'); await host.inputDevices(INPUT_DEVICES, 15); await hostGetsKey('the host gets a key after the unmount'); - // The keyboard alone does not show it: the drive's first SCSI command can - // get no answer after a new enumeration, and the host then has no drive. + // The keyboard alone does not show it: a drive whose command got no + // answer is gone from the host. await until(() => host.exec(drives('rom')), out => out.trim() !== '', 15) .catch(e => assert.fail(`the host has the Mini's drive again within 15 seconds: ${e.message}`)); diff --git a/tools/integration/tests/os_services_mount.test.mjs b/tools/integration/tests/os_services_mount.test.mjs index 05504ca3..fb7aff70 100644 --- a/tools/integration/tests/os_services_mount.test.mjs +++ b/tools/integration/tests/os_services_mount.test.mjs @@ -11,8 +11,9 @@ const WIDTH = 1920; const HEIGHT = 1080; const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); -// A mount that changes the drive type re-enumerates USB, ending the OS -// Services session; a CD-ROM image on a CD-ROM drive does not. +// The host has the Mini's CD-ROM drive and disk drive at all times, so a +// mount of either type is a new medium of a drive, and USB and the OS +// Services session stay as they are. describe('OS Services service video and a mount', () => { let mini; let host; @@ -113,27 +114,26 @@ describe('OS Services service video and a mount', () => { }; after(() => cleanup(), { timeout: 300_000 }); - // Four mounts are more than the Mini's limit of three switches to service - // video in two minutes, which is for switches that fail and not for the - // Mini's own returns to USB. - step('mounts that change the type of the drive take the Mini off USB and back, and service video returns after each', { timeout: 600_000 }, async () => { + step('mounts of either type keep USB and service video', { timeout: 600_000 }, async () => { await videoOn(); try { - for (const mode of ['Disk', 'CDROM', 'Disk', 'CDROM']) { - const { switches } = await serviceVideo(); - const { mountCount } = await hostUsb(); - if (mode === 'CDROM') { - await mountCdrom(); - } else { - await mini.rpc('unmountImage'); - await mini.rpc('mountWithHTTP', { url: server.url, mode }, 60_000); - } - await until(hostUsb, now => now.mountCount > mountCount, 60); - const back = await until(async () => ({ error: (await videoState(mini)).osServices.videoError, switches: (await serviceVideo()).switches }), - now => now.error !== undefined || now.switches > switches, 60); - assert.equal(back.error, undefined, `the Mini's service video error after the mount as ${mode}`); - await showsServiceVideo(); + const before = { usb: await hostUsb(), switches: (await serviceVideo()).switches }; + for (const mode of ['Disk', 'CDROM', 'CDROM', 'Disk', 'Disk', 'CDROM']) { + await mini.rpc('unmountImage').catch(() => undefined); + await mini.rpc('mountWithHTTP', { url: server.url, mode }, 60_000); + await until(medium, now => now?.type === (mode === 'CDROM' ? 'rom' : 'disk'), 45) + .catch(error => assert.fail(`the host has the image as ${mode} within 45 s: ${error.message}`)); + const usb = await hostUsb(); + assert.equal(usb.configured, true, `the host keeps the Mini configured after the mount as ${mode}`); + assert.equal(usb.mountCount, before.usb.mountCount, `the host does not configure the Mini again for the mount as ${mode}`); + const video = await serviceVideo(); + assert.equal(video.source, 'guest', `the video source after the mount as ${mode}`); + assert.equal(video.switches, before.switches, `switches of the video source after the mount as ${mode}`); + // The Mini keeps USB for the next image only when the host has asked + // the drive again after it found this one; a polling host does within 2 s. + await sleep(3000); } + assert.equal((await videoState(mini)).osServices.videoError, undefined, 'the Mini\'s service video error'); } finally { await mini.rpc('unmountImage', {}, 30_000).catch(() => undefined); } @@ -148,7 +148,6 @@ describe('OS Services service video and a mount', () => { const dd = (await host.exec('sudo -n true 2>/dev/null && for dd in gnudd dd; do ' + '"$dd" --version 2>/dev/null | head -n 1 | grep -v uutils | grep -q coreutils && { echo "$dd"; break; }; done; true')).trim(); if (!dd) return t.skip(`${host.login} needs sudo without a password and GNU dd to read the CD-ROM`); - // The drive is a CD-ROM drive once a CD-ROM image has been mounted. await mountCdrom(); await videoOn(); await mini.rpc('unmountImage');