Add fake Bluetooth controllers and USBSERFILE

This commit is contained in:
Baptiste MOINE committed 2026-11-08 00:16:48 +01:00
1 parent e17c315f87
commit 41245dc22b
4 files changed
+608 -11

No files matched your search

+28 -6
View File
@@ -14,6 +14,7 @@
#include "regcache.h"
#include "se_engine.h"
#include "tegra_bl.h"
#include "btpeer.h"
#include <algorithm>
#include <cmath>
@@ -253,7 +254,7 @@ struct BtChip {
BtPhase phase = BT_PHASE_OFF;
bool reg_on = false; // last sampled BT_REG_ON level
uint64_t por_us = 0; // emu_usec at the rising edge
uint8_t cmd[4 + 255]; // H4 command being assembled, host -> chip
uint8_t cmd[5 + 1024]; // H4 command or ACL packet, host -> chip
uint32_t cmd_n = 0;
};
static BtChip bt_chip;
@@ -283,6 +284,8 @@ static void bt_uart_sync(EmuState *state) {
bt_chip_tick(state);
UartPort &up = uart_ports[UART_D];
bool cts = bt_radio_alive(state) && bt_chip.phase == BT_PHASE_READY;
if (cts)
btpeer_tick(state); // the fake controllers' traffic, in guest time
if (cts != up.cts) {
up.cts = cts;
up.msr_delta |= 0x01; // DCTS
@@ -319,7 +322,8 @@ static void bt_queue(EmuState *state, const uint8_t *ev, size_t n) {
// One complete HCI command has arrived. Broadcom parts run their lower-layer
// stack out of on-die ROM, so a bare HCI_Reset is answered long before any
// firmware download -- which is the whole reason this probe works at all.
static void bt_chip_command(EmuState *state, uint16_t opcode) {
static void bt_chip_command(EmuState *state, uint16_t opcode,
const uint8_t *p, uint32_t n) {
switch (opcode) {
case 0x0C03: { // HCI_Reset
static const uint8_t cc[] = {0x04, 0x0E, 0x04, 0x01, 0x03, 0x0C, 0x00};
@@ -343,7 +347,9 @@ static void bt_chip_command(EmuState *state, uint16_t opcode) {
bt_queue(state, cc, sizeof(cc));
break;
}
default: { // Unknown Command, so the H4 stream stays framed either way.
default: { // A host's command (btpeer.cpp), or Unknown Command.
if (btpeer_command(state, opcode, p, n))
break;
const uint8_t cc[] = {0x04, 0x0E, 0x04, 0x01, (uint8_t)opcode,
(uint8_t)(opcode >> 8), 0x01};
bt_queue(state, cc, sizeof(cc));
@@ -362,17 +368,32 @@ static void bt_chip_rx(EmuState *state, uint8_t b) {
bt_chip.cmd_n = 0;
return;
}
if (bt_chip.cmd_n == 0 && b != 0x01)
return; // resync on the H4 command indicator
if (bt_chip.cmd_n == 0 && b != 0x01 && b != 0x02)
return; // resync on the H4 command or ACL indicator
if (bt_chip.cmd_n < sizeof(bt_chip.cmd))
bt_chip.cmd[bt_chip.cmd_n++] = b;
if (bt_chip.cmd[0] == 0x02) {
// ACL: 02 <handle/flags 2> <length 2> <data>, for the fake controllers.
if (bt_chip.cmd_n < 5)
return;
uint32_t len = bt_chip.cmd[3] | (uint32_t)bt_chip.cmd[4] << 8;
if (5 + len > sizeof(bt_chip.cmd)) {
bt_chip.cmd_n = 0;
return;
}
if (bt_chip.cmd_n < 5 + len)
return;
bt_chip.cmd_n = 0;
btpeer_acl(state, bt_chip.cmd + 1, 4 + len);
return;
}
if (bt_chip.cmd_n < 4)
return; // 01 <opcode lo> <opcode hi> <plen>
if (bt_chip.cmd_n < 4u + bt_chip.cmd[3])
return;
uint16_t opcode = (uint16_t)bt_chip.cmd[1] | ((uint16_t)bt_chip.cmd[2] << 8);
bt_chip.cmd_n = 0;
bt_chip_command(state, opcode);
bt_chip_command(state, opcode, bt_chip.cmd + 4, bt_chip.cmd[3]);
}
// BT_REG_ON (PH4) moved. The rising edge is the chip's power-on reset; the
@@ -391,6 +412,7 @@ static void bt_reg_on_set(EmuState *state, bool level) {
}
bt_chip.cmd_n = 0;
state->uart_rx_fifo[UART_D].clear();
btpeer_reset(state);
bt_uart_sync(state);
printf("[bt] BT_REG_ON %s (%s radio)\n", level ? "HIGH - POR" : "LOW - off",
bt_radio_name(state->bt_radio.load()));