diff --git a/emu_state.h b/emu_state.h index c4a4882..ab92db3 100644 --- a/emu_state.h +++ b/emu_state.h @@ -635,6 +635,8 @@ struct EmuState { static constexpr size_t N_UARTS = 5; std::string uart_tx_log[N_UARTS]; std::deque uart_rx_fifo[N_UARTS]; + // Bytes the emulated PC sends to a USB serial port (USBSER events). + std::deque usb_serial_rx; }; #endif // EMU_STATE_H diff --git a/input_script.cpp b/input_script.cpp index d6f93b9..6f47fae 100644 --- a/input_script.cpp +++ b/input_script.cpp @@ -17,6 +17,10 @@ // touches the screen at (x, y) in the 1280x720 landscape picture bdk's // touch driver reports (Nyx's coordinates), and lifts after hold_ms. // +// USBSER +// +// types text into the USB serial port the PC has open (escapes as UART). +// // SNAP // // writes the next frame the display shows to , raw like last_fb.rgba @@ -49,7 +53,7 @@ namespace { -enum class Key { Power, VolUp, VolDown, Uart, UartFile, Tap, Snap }; +enum class Key { Power, VolUp, VolDown, Uart, UartFile, UsbSer, Tap, Snap }; struct Event { uint64_t at_us = 0; // press time @@ -74,6 +78,7 @@ const char *key_name(Key k) { case Key::VolUp: return "VOL+"; case Key::Uart: return "UART"; case Key::UartFile: return "UARTFILE"; + case Key::UsbSer: return "USBSER"; case Key::Tap: return "TAP"; case Key::Snap: return "SNAP"; default: return "VOL-"; @@ -96,6 +101,7 @@ bool parse_key(const std::string &tok, Key *out) { // UARTFILE pushes a file's raw contents, for binary protocol work. if (s == "UART") { *out = Key::Uart; return true; } if (s == "UARTFILE") { *out = Key::UartFile; return true; } + if (s == "USBSER") { *out = Key::UsbSer; return true; } if (s == "TAP" || s == "T") { *out = Key::Tap; return true; } if (s == "SNAP") { *out = Key::Snap; return true; } return false; @@ -184,7 +190,8 @@ bool parse_text(const std::string &text) { return false; } - if (ev.key == Key::Uart || ev.key == Key::UartFile || ev.key == Key::Snap) { + if (ev.key == Key::Uart || ev.key == Key::UartFile || ev.key == Key::UsbSer || + ev.key == Key::Snap) { // Everything after the keyword is the payload; keep interior spacing. size_t kpos = raw.find(tok[1]); std::string rest = (kpos == std::string::npos) @@ -195,7 +202,7 @@ bool parse_text(const std::string &text) { while (!rest.empty() && (rest.back() == ' ' || rest.back() == '\t' || rest.back() == '\r')) rest.pop_back(); - ev.data = (ev.key == Key::Uart) ? unescape(rest) : rest; + ev.data = (ev.key == Key::Uart || ev.key == Key::UsbSer) ? unescape(rest) : rest; ev.until_us = ev.at_us; // instantaneous, nothing to release prev_release_us = ev.at_us; g_events.push_back(ev); @@ -290,6 +297,23 @@ void input_script_tick(EmuState &state) { // UART events are one-shot: push the bytes into UART_B's receive FIFO // (index 1 = UART_B, the port payloads use for their debug console) and // retire the event immediately. + if (ev.key == Key::UsbSer) { + if (now >= ev.at_us && !ev.pressed) { + for (unsigned char ch : ev.data) + state.usb_serial_rx.push_back(ch); + ev.pressed = ev.released = true; + printf("[input-script] USB serial <- %zu byte(s) @%llu us (%zu/%zu)\n", + ev.data.size(), (unsigned long long)now, i + 1, g_events.size()); + fflush(stdout); + if (i == g_done) { + g_done++; + if (g_done == g_events.size()) + printf("[input-script] sequence complete\n"); + } + continue; + } + break; + } if (ev.key == Key::Uart || ev.key == Key::UartFile) { if (now >= ev.at_us && !ev.pressed) { std::string bytes = ev.data; diff --git a/t210/usb.cpp b/t210/usb.cpp index 4754d8c..fd95b4a 100644 --- a/t210/usb.cpp +++ b/t210/usb.cpp @@ -22,6 +22,11 @@ // (START STOP UNIT, LoEj) and polls TEST UNIT READY until // the gadget lets go, as Linux does // HID the report descriptor, SET_IDLE, then interrupt IN reports +// CDC ACM SET_LINE_CODING (115200 8N1) and GET_LINE_CODING, +// SET_CONTROL_LINE_STATE (DTR, RTS), then bulk IN read +// continuously and printed as [usb-serial] lines, the +// notification endpoint polled every 16 ms, and what a +// USBSER script event types sent on bulk OUT // // With --usb-host nbd[:port] (or [usb] nbd_port) the mass storage session // instead locks the medium (PREVENT MEDIUM REMOVAL) and serves the disk over @@ -161,6 +166,7 @@ enum class Step { NbdPrevent, NbdRead, NbdWrite, NbdFlush, NbdTur, MscAllow, MscEject, MscAfterEject, MscEjectWait, NbdServe, HidReport, HidIdle, HidPoll, + CdcSetLine, CdcGetLine, CdcSetCtrl, CdcPoll, Idle, Failed, }; @@ -238,6 +244,11 @@ private: uint8_t iface_class_ = 0, iface_sub_ = 0, iface_proto_ = 0; uint16_t hid_report_len_ = 0; Endpoint bulk_in_, bulk_out_, intr_in_; + // A serial port: its data interface, the line being received, the next + // notification poll. + uint8_t data_iface_ = 0; + std::string cdc_line_; + uint64_t cdc_notif_us_ = 0; std::string vendor_, product_, serial_; // Mass storage. @@ -466,8 +477,15 @@ private: const uint8_t *d = &cfg_[i]; size_t n = std::min(d[0], cfg_.size() - i); if (d[1] == 4 && n >= 9) { // interface - if (!first_iface) + if (!first_iface) { + // A serial port's data interface, with its bulk endpoints, + // follows its communications interface. + if (iface_class_ == 0x02 && d[5] == 0x0A) { + data_iface_ = d[2]; + continue; + } break; // the first interface is enough + } first_iface = false; iface_ = d[2]; iface_class_ = d[5]; @@ -497,6 +515,8 @@ private: return "mass storage, bulk-only"; if (iface_class_ == 0x03) return "HID"; + if (iface_class_ == 0x02 && iface_sub_ == 0x02) + return "CDC ACM serial port"; return "a class this host does not drive"; } @@ -643,7 +663,8 @@ private: } case Step::HidPoll: - finish(); // polls the endpoint itself + case Step::CdcPoll: + finish(); // polls the endpoints itself return; case Step::NbdServe: @@ -742,6 +763,12 @@ private: control(0x81, 6, 0x2200, iface_, hid_report_len_ ? hid_report_len_ : 256); return go(Step::HidReport); } + if (iface_class_ == 0x02 && iface_sub_ == 0x02 && bulk_in_.addr && + bulk_out_.addr) { + // As a terminal opening the port: the line coding first. + control(0x21, 0x20, 0, iface_, 7, {0x00, 0xC2, 0x01, 0x00, 0, 0, 8}); + return go(Step::CdcSetLine); + } printf("[usb-host] configured; nothing more to do\n"); go(Step::Idle); } @@ -1064,6 +1091,80 @@ private: deadline_ = now_us() + kTimeoutUs; return go(Step::HidPoll); + // ---- CDC ACM ---- + + case Step::CdcSetLine: + printf("[usb-host] SET_LINE_CODING 115200 8N1 %s\n", + ctl_.stalled ? "stalled" : "done"); + control(0xA1, 0x21, 0, iface_, 7); // GET_LINE_CODING + return go(Step::CdcGetLine); + + case Step::CdcGetLine: { + const std::vector &d = ctl_.data; + if (ctl_.stalled || d.size() < 7) + printf("[usb-host] GET_LINE_CODING %s\n", + ctl_.stalled ? "stalled" : "came back short"); + else + printf("[usb-host] GET_LINE_CODING: %u baud, %u data bits, stop %u, " + "parity %u\n", d[0] | d[1] << 8 | d[2] << 16 | (uint32_t)d[3] << 24, + d[6], d[4], d[5]); + control(0x21, 0x22, 0x03, iface_, 0); // SET_CONTROL_LINE_STATE + return go(Step::CdcSetCtrl); + } + + case Step::CdcSetCtrl: + printf("[usb-host] SET_CONTROL_LINE_STATE DTR RTS %s; reading EP %02X " + "(data interface %u)\n", ctl_.stalled ? "stalled" : "done", + bulk_in_.addr, data_iface_); + cdc_line_.clear(); + cdc_notif_us_ = 0; + return go(Step::CdcPoll); + + case Step::CdcPoll: { + // What the device sends, a line at a time. + uint8_t buf[512]; + bool short_pkt = false; + int n = dc_->in(bulk_in_.addr & 0xF, buf, + std::min(bulk_in_.max_packet, sizeof(buf)), &short_pkt); + for (int i = 0; i < n; i++) { + if (buf[i] == '\n') { + printf("[usb-serial] %s\n", cdc_line_.c_str()); + cdc_line_.clear(); + } else if (buf[i] >= 0x20 && buf[i] < 0x7F) { + cdc_line_ += (char)buf[i]; + } + } + if (n > 0) + progressed_ = true; + // What the script types, once the device has a read queued. + std::deque &tx = g_state->usb_serial_rx; + if (!tx.empty()) { + uint8_t out[512]; + int len = (int)std::min(tx.size(), + std::min(bulk_out_.max_packet, sizeof(out))); + std::copy(tx.begin(), tx.begin() + len, out); + int m = dc_->out(bulk_out_.addr & 0xF, out, len); + if (m > 0) { + tx.erase(tx.begin(), tx.begin() + m); + printf("[usb-host] %d byte(s) to EP %02X\n", m, bulk_out_.addr); + progressed_ = true; + } + } + // The notification endpoint, polled as a PC polls it. This device + // never sends on it, so a NAK is the answer expected. + if (intr_in_.addr && now_us() >= cdc_notif_us_) { + cdc_notif_us_ = now_us() + 16000; + uint8_t nb[16]; + bool sp = false; + int k = dc_->in(intr_in_.addr & 0xF, nb, sizeof(nb), &sp); + if (k >= 0) + printf("[usb-host] serial state notification, %d byte(s)\n", k); + else if (k == XFER_STALL) + printf("[usb-host] the notification endpoint stalled\n"); + } + return; + } + case Step::HidPoll: { uint8_t buf[512]; bool short_pkt = false; @@ -1419,7 +1520,7 @@ Usb2d g_usb2d; // The device side of an xHCI controller, as bdk's xusbd.c drives it on // Mariko (register layout in usb_t210.h, TRB and context layouts in xusbd.c). // Endpoints are numbered as device context indexes: 0 is the control -// endpoint, 2 and 3 bulk OUT and IN. Each has a context (16 words at ECP + +// endpoint, 2n and 2n + 1 endpoint n's OUT and IN. Each has a context (16 words at ECP + // 64 * index: word 2 holds its transfer ring's dequeue pointer and DCS) and a // ring of 16-byte TRBs the driver fills - Data and Status TRBs on the control // ring, Normal TRBs on the bulk ones, a Link TRB closing each ring - ringing @@ -1483,7 +1584,7 @@ public: reg(off) &= ~val; return; case X_EP_RELOAD: - for (int i = 0; i < 4; i++) + for (int i = 0; i < 6; i++) if (val & (1u << i)) { load_ring(i); UTRACE("reload ep %d: ring %08X dcs %u\n", i, rings_[i].deq, @@ -1546,7 +1647,7 @@ public: bool setup_taken() override { return true; } int in(int ep, uint8_t *buf, int max, bool *short_pkt) override { - int dci = ep ? 3 : 0; + int dci = ep ? ep * 2 + 1 : 0; int n = transfer(dci, true, buf, max); if (n >= 0) { // As on the USB2 controller, a TRB that moved less than the host asked @@ -1562,7 +1663,7 @@ public: } int out(int ep, const uint8_t *buf, int len) override { - return transfer(ep ? 2 : 0, false, const_cast(buf), len); + return transfer(ep ? ep * 2 : 0, false, const_cast(buf), len); } uint32_t address() override { return (reg(X_CTRL) >> 24) & 0x7F; } @@ -1579,7 +1680,7 @@ private: uint32_t deq = 0; uint32_t dcs = 0; bool valid = false; - } rings_[4]; + } rings_[6]; // endpoints 0 to 2 uint32_t ®(uint32_t off) { return regs_[(off % kSize) / 4]; }