text stringlengths 0 1.47k |
|---|
# ===== file: cmd/btbridge/brcm.go ===== |
//go:build linux |
package main |
// A Broadcom controller on a UART (the Echo Spot's BCM43569A2 on /dev/ttyMT1) comes up at 115200 |
// baud running its ROM, without the RAM patch and with no address. Before the kernel stack gets it, |
// btbridge does what Android's libbt-vendor (Broadcom) does at power-on: |
// |
// 1. power the radio through its rfkill switch |
// 2. HCI_Reset at 115200 |
// 3. Download_Minidriver (0xFC2E), then every record of the .hcd patch file (each one a whole HCI |
// command, Write_RAM 0xFC4C, the last Launch_RAM 0xFC4E), a Command Complete for each |
// 4. the controller restarts into the patched firmware, back at 115200: HCI_Reset again |
// 5. Update_UART_Baud_Rate (0xFC18) and the tty to the same speed |
// 6. Write_BD_ADDR (0xFC01) with the factory address |
// |
// After that the tty is an H4 stream like any other and the bridge copies it to /dev/vhci. |
import ( |
"encoding/binary" |
"fmt" |
"os" |
"path/filepath" |
"strings" |
"syscall" |
"time" |
"unsafe" |
) |
const ( |
opReset = 0x0c03 |
opMinidriver = 0xfc2e |
opLaunchRAM = 0xfc4e |
opUpdateBaud = 0xfc18 |
opWriteBdaddr = 0xfc01 |
) |
// openUART opens a tty raw, 8N1 with RTS/CTS, at baud. |
func openUART(path string, baud int) (int, error) { |
fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_NOCTTY, 0) |
if err != nil { |
return -1, err |
} |
if err := setBaud(fd, baud); err != nil { |
syscall.Close(fd) |
return -1, err |
} |
return fd, nil |
} |
var bauds = map[int]uint32{ |
115200: syscall.B115200, |
230400: syscall.B230400, |
460800: syscall.B460800, |
921600: syscall.B921600, |
1000000: syscall.B1000000, |
1500000: syscall.B1500000, |
2000000: syscall.B2000000, |
3000000: syscall.B3000000, |
4000000: syscall.B4000000, |
} |
// setBaud puts the tty in raw mode at baud, flushing what is queued. |
func setBaud(fd, baud int) error { |
b, ok := bauds[baud] |
if !ok { |
return fmt.Errorf("unsupported baud rate %d", baud) |
} |
var t syscall.Termios |
if err := ioctl(fd, syscall.TCGETS, unsafe.Pointer(&t)); err != nil { |
return fmt.Errorf("TCGETS: %w", err) |
} |
t.Iflag = 0 |
t.Oflag = 0 |
t.Lflag = 0 |
t.Cflag = b | syscall.CS8 | syscall.CLOCAL | syscall.CREAD | 0x80000000 // CRTSCTS |
t.Ispeed, t.Ospeed = b, b |
t.Cc[syscall.VMIN] = 1 |
t.Cc[syscall.VTIME] = 0 |
if err := ioctl(fd, syscall.TCSETS, unsafe.Pointer(&t)); err != nil { |
return fmt.Errorf("TCSETS: %w", err) |
} |
// TCFLSH (0x540B) with TCIOFLUSH (2): drop what either direction has queued. |
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), 0x540b, 2); e != 0 { |
return e |
} |
return nil |
} |
func ioctl(fd int, req uintptr, arg unsafe.Pointer) error { |
if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), req, uintptr(arg)); e != 0 { |
return e |
} |
return nil |
} |
// rfkillPower switches every rfkill device of type bluetooth on (state 1), and reports whether |
// there was one. |
func rfkillPower() bool { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.