Skip to content

armgit / gecko

Public Imported from GitHub
0

A GameCube/Wii emulator and debugger written in Rust.

satisfy bluetooth in sdk, reaching audio shit now

Layle86d1111c777c
diff --git a/crates/gecko/src/hollywood/ipc/usb.rs b/crates/gecko/src/hollywood/ipc/usb.rs
index 987fbd0..116f854 100644
--- a/crates/gecko/src/hollywood/ipc/usb.rs
+++ b/crates/gecko/src/hollywood/ipc/usb.rs
@@ -1,23 +1,138 @@
-use crate::hollywood::ipc::IosDevice;
-
-pub struct Usb;
-
-impl IosDevice for Usb {
- fn ioctlv(
- &mut self,
- _ctx: &mut super::DeviceContext<'_>,
- cmd: u32,
- in_count: u32,
- io_count: u32,
- vec_ptr: u32,
- ) -> i32 {
- tracing::warn!(
- cmd = format!("{cmd:#010X}"),
- in_count,
- io_count,
- vec_ptr = format!("{vec_ptr:#010X}"),
- "USB: unimplemented ioctlv"
- );
+use crate::hollywood::ipc::{DeviceContext, IosDevice};
+use std::collections::VecDeque;
+
+const USB_CTRL: u32 = 0;
+const USB_BULK: u32 = 1;
+const USB_INTR: u32 = 2;
+const EP_INT_IN: u8 = 0x81;
+
+const EV_COMMAND_COMPLETE: u8 = 0x0E;
+const OP_READ_LOCAL_VERSION: u16 = 0x1001;
+const OP_READ_LOCAL_FEATURES: u16 = 0x1003;
+const OP_READ_BUFFER_SIZE: u16 = 0x1005;
+const OP_READ_BD_ADDR: u16 = 0x1009;
+
+pub struct Bluetooth {
+ pending_hci: VecDeque<Vec<u8>>,
+}
+
+impl Bluetooth {
+ pub fn new() -> Self {
+ Self {
+ pending_hci: VecDeque::new(),
+ }
+ }
+}
+
+impl IosDevice for Bluetooth {
+ fn ioctlv(&mut self, ctx: &mut DeviceContext<'_>, cmd: u32, _in_count: u32, _io_count: u32, vec_ptr: u32) -> i32 {
+ match cmd {
+ USB_CTRL => self.handle_control(ctx, vec_ptr),
+ USB_INTR => self.handle_interrupt(ctx, vec_ptr),
+ USB_BULK => self.handle_bulk(ctx, vec_ptr),
+ _ => 0,
+ }
+ }
+}
+
+impl Bluetooth {
+ fn handle_control(&mut self, ctx: &mut DeviceContext<'_>, vec_ptr: u32) -> i32 {
+ let bm_request = ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 0));
+ let b_request = ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 1));
+ if bm_request != 0x20 || b_request != 0 {
+ return 0;
+ }
+
+ let w_length = {
+ let p = self::vec_data(ctx.mmio, vec_ptr, 4);
+ u16::from_le_bytes([ctx.mmio.phys_read_u8(p), ctx.mmio.phys_read_u8(p + 1)])
+ };
+ if w_length < 3 {
+ return 0;
+ }
+
+ let data_ptr = self::vec_data(ctx.mmio, vec_ptr, 6);
+ let opcode = u16::from_le_bytes([ctx.mmio.phys_read_u8(data_ptr), ctx.mmio.phys_read_u8(data_ptr + 1)]);
+
+ self.queue_command_complete(opcode);
+ 0
+ }
+
+ /// For bulk OUT (0x02) we currently drop whatever the SDK sends. We warn if
+ /// there's any payload, since Bulk IN (0x82) just gets a length 0 reply
+ /// since we don't have ACL frames queued.
+ fn handle_bulk(&mut self, ctx: &mut DeviceContext<'_>, vec_ptr: u32) -> i32 {
+ let endpoint = ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 0));
+
+ if endpoint == 0x02 {
+ let len_p = self::vec_data(ctx.mmio, vec_ptr, 1);
+ let w_length = u16::from_le_bytes([ctx.mmio.phys_read_u8(len_p), ctx.mmio.phys_read_u8(len_p + 1)]);
+ if w_length > 0 {
+ tracing::warn!(len = w_length, "no L2CAP yet");
+ }
+ }
+
0
}
+
+ /// HCI event receive (interrupt IN endpoint 0x81). Deliver the next
+ /// queued event, or reply length 0 (the SDK seems to tolerate that).
+ fn handle_interrupt(&mut self, ctx: &mut DeviceContext<'_>, vec_ptr: u32) -> i32 {
+ if ctx.mmio.phys_read_u8(self::vec_data(ctx.mmio, vec_ptr, 0)) != EP_INT_IN {
+ return 0;
+ }
+
+ let Some(event) = self.pending_hci.pop_front() else {
+ return 0;
+ };
+
+ let buf_ptr = self::vec_data(ctx.mmio, vec_ptr, 2);
+ for (i, b) in event.iter().enumerate() {
+ ctx.mmio.phys_write_u8(buf_ptr + i as u32, *b);
+ }
+
+ event.len() as i32
+ }
+
+ /// Build an HCI Command_Complete event for `opcode` and append it to the
+ /// outgoing queue. The four init Read_* commands need real payloads so
+ /// the SDK's BTM init state machine can advance to state 5; everything
+ /// else is generic status=0.
+ fn queue_command_complete(&mut self, opcode: u16) {
+ let payload: &[u8] = match opcode {
+ OP_READ_LOCAL_VERSION => &[
+ 0x00, // status
+ 0x03, // HCI v1.2
+ 0x40, 0x0E, // HCI revision
+ 0x03, // LMP v1.2
+ 0x0F, 0x00, // manufacturer = Broadcom
+ 0x40, 0x0E, // LMP subversion
+ ],
+ OP_READ_LOCAL_FEATURES => &[0x00, 0xBC, 0x02, 0x04, 0x38, 0x08, 0x08, 0x00, 0x00],
+ OP_READ_BUFFER_SIZE => &[
+ 0x00, 0x53, 0x01, // ACL packet length = 339
+ 0x40, // SCO packet length = 64
+ 0x08, 0x00, // num ACL packets
+ 0x08, 0x00, // num SCO packets
+ ],
+ OP_READ_BD_ADDR => &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66],
+ _ => &[0x00],
+ };
+
+ let mut event = Vec::with_capacity(5 + payload.len());
+ event.extend_from_slice(&[
+ EV_COMMAND_COMPLETE,
+ (3 + payload.len()) as u8,
+ 0x01, // num_hci_command_packets
+ opcode as u8,
+ (opcode >> 8) as u8,
+ ]);
+ event.extend_from_slice(payload);
+
+ self.pending_hci.push_back(event);
+ }
+}
+
+fn vec_data<const SYS: crate::system::SystemId>(mmio: &crate::mmio::Mmio<SYS>, vec_ptr: u32, idx: u32) -> u32 {
+ mmio.phys_read_u32(vec_ptr + (idx * 8))
}
diff --git a/crates/gecko/src/mmio/bus.rs b/crates/gecko/src/mmio/bus.rs
index 189850c..6d0c81d 100644
--- a/crates/gecko/src/mmio/bus.rs
+++ b/crates/gecko/src/mmio/bus.rs
@@ -771,8 +771,8 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
self.mmio.phys_write_u8(phys, val);
}
GX_FIFO_BASE..=GX_FIFO_END => {
- let wptr = self.pi.fifo_wptr as usize;
- self.mmio.ram[wptr] = val;
+ let wptr = self.pi.fifo_wptr;
+ self.mmio.phys_write_u8(wptr, val);
self.pi.advance_fifo_wptr(1);
if self.cp.control.gp_link_enable() {
self.gx.mmio_write_u8(&mut self.mmio, self.render_sink.as_mut(), val);
@@ -936,9 +936,8 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
self.mmio.phys_write_u16(phys, val);
}
GX_FIFO_BASE..=GX_FIFO_END => {
- let wptr = self.pi.fifo_wptr as usize;
- let bytes = val.to_be_bytes();
- self.mmio.ram[wptr..wptr + 2].copy_from_slice(&bytes);
+ let wptr = self.pi.fifo_wptr;
+ self.mmio.phys_write_u16(wptr, val);
self.pi.advance_fifo_wptr(2);
if self.cp.control.gp_link_enable() {
self.gx.mmio_write_u16(&mut self.mmio, self.render_sink.as_mut(), val);
@@ -1101,9 +1100,8 @@ impl<const SYSTEM: SystemId> System<SYSTEM> {
self.mmio.phys_write_u32(phys, val);
}
GX_FIFO_BASE..=GX_FIFO_END => {
- let wptr = self.pi.fifo_wptr as usize;
- let bytes = val.to_be_bytes();
- self.mmio.ram[wptr..wptr + 4].copy_from_slice(&bytes);
+ let wptr = self.pi.fifo_wptr;
+ self.mmio.phys_write_u32(wptr, val);
self.pi.advance_fifo_wptr(4);
if self.cp.control.gp_link_enable() {
self.gx.mmio_write_u32(&mut self.mmio, self.render_sink.as_mut(), val);
diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rs
index ebe1796..feac932 100644
--- a/crates/gecko/src/starlet.rs
+++ b/crates/gecko/src/starlet.rs
@@ -122,7 +122,8 @@ impl System<{ crate::WII }> {
self.starlet
.register("/dev/di", Box::new(ipc::di::DiskInterface::new()));
self.starlet.register("/dev/es", Box::new(ipc::es::ETicketServices));
- self.starlet.register("/dev/usb/oh1/57e/305", Box::new(ipc::usb::Usb));
+ self.starlet
+ .register("/dev/usb/oh1/57e/305", Box::new(ipc::usb::Bluetooth::new()));
}
pub fn create_device_context(&mut self) -> (&mut Starlet, DeviceContext<'_>) {
@@ -139,7 +140,6 @@ impl System<{ crate::WII }> {
pub fn dispatch_command<const SYSTEM: SystemId>(sys: &mut System<SYSTEM>, cmd_paddr: u32) {
let result = self::process_command(sys, cmd_paddr);
-
sys.starlet.pending.push_back(PendingResponse { cmd_paddr, result });
sys.scheduler
.schedule_in(FINALIZE_DELAY_CYCLES, self::deliver_pending::<SYSTEM>);