Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions docs/8bitdo-retro-r8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# 8BitDo Retro R8 Mouse (2dc8:5206 receiver)

Driver for the Retro R8 through its 2.4 GHz receiver: reads its settings and
firmware versions, and writes the polling rate, DPI stages and lift-off.
Verified on a Retro R8 N Edition on Linux. Battery is not available on this connection (see
below); over Bluetooth the mouse reports it through the standard Battery
Service.

## Hardware

- Receiver: VID `0x2dc8`, PID `0x5206`, bcdDevice `4.26`, product string
"8BitDo Retro R8 Mouse Adapter". Four HID interfaces:
- 0: mouse (report 1);
- 1: keyboard, consumer and system control;
- 2: vendor collection, usage page `0xFF00`, usage `0x01`, unnumbered
64-byte input and output reports. **This is the configuration channel.**
- 3: vendor usage page `0xFF0B` (reports `0x2A`–`0x2D`) and `0xFF02`
(feature `0x14`): the Realtek update channel. Not used.
- Over Bluetooth LE the mouse shows up as PID `0x5205` with GATT Device
Information, Battery (`0x180F`) and HID services.
- Receiver firmware identifies itself as Realtek SDK "RTK20250227D".

## Sources

- **8BitDo Ultimate Software V2, Windows V1.38**, read statically (it is a
.NET application plus a native helper DLL). Used for: which frames it sends
to PID `0x5206`, which reply bytes it checks, and the offsets it reads each
value from. No vendor code or binary is included in this repository.
- **Hardware**: every read below was sent to the receiver on Linux through
`/dev/hidraw` and the replies recorded. The DPI button was pressed while the
replies were polled, and the active stage followed it through all six
stages and back.
- **[mbs1337/r8-rs](https://github.com/mbs1337/r8-rs)** (GPL-3.0), an
independent Linux tool for the same receiver, uses the same frame and
`01 06 12 01` for the active DPI, which corroborates the layout above. No
code from it is used here.

## Frame

Unnumbered 64-byte output report, reply as a 64-byte input report.

| Target | Request | Reply |
|---|---|---|
| Mouse (over the air) | `01 06 <cmd> 01 [args]` | `01 06 <cmd> 11 <payload>` |
| Receiver | `06 <class> <id> 01` | `26 <class> <id> 11 <payload>` |

`op` `01` reads; the vendor tool writes with `04`.

For a mouse request the receiver first acknowledges at once with
`21 06 <cmd> 01 … 14` while it forwards the request over the air; the mouse's
reply `01 06 <cmd> 11 …` follows 5–210 ms later. Wait for the reply and skip
the acknowledgement. When the mouse is asleep or switched off, only the
acknowledgement arrives.

The mouse answers almost every id with `11`, including ids it does not use:
those get an empty payload. An empty `01 06 <cmd> 11` therefore does not prove
that a command exists or that it read anything, and the driver rejects replies
without the payload it expects.

## Verified reads

| Command | Payload | Example (N Edition) |
|---|---|---|
| Receiver `06 03 0A` | `[linked]` | `01` linked, `00` mouse asleep/off |
| `0x12` active DPI | `[stage count] [active stage, 0-based] [dpi u16 LE] [table…]` | `06 01 b0 04` → stage 2 of 6, 1200 |
| `0x28` DPI table, arg `00` X / `01` Y | `[axis] [enabled mask] [6 × dpi u16 LE]` | `00 3f` 800/1200/1600/2400/3200/6400 |
| `0x0D` polling | `[rate count] [active index, 0-based] 00 00 [6 × Hz u16 LE]` | `05 01` over 250/500/1000/2000/4000 → 500 Hz |
| `0x26` lift-off | `[level]` | `01` (the tool offers 1 and 2) |
| `0x25` edition | byte 3 | `01` N Edition; the tool also knows 0 Xbox, 3 C64, 7 Forest, 8 GoW |
| Mouse `01 04 04` / receiver `06 02 03` | firmware version: major in payload byte 8, minor in byte 14 | mouse `0b`/`03` → 11.03, receiver `0a`/`0e` → 10.14 |

The versions match the newest entries of 8BitDo's firmware catalog (mouse
11.03, receiver 10.14), and the vendor tool computes them the same way.

Also answered but not used by the driver: `0x27` left-handed mode, `0x23`
wheel direction, `0x24` wheel speed (all `0` on the test mouse).

## Verified writes

Op `04`, payload after `01 06 <cmd> 04`; the mouse confirms with
`01 06 <cmd> 14`. Layouts are the vendor tool's (`WriteLod`, `WriteDpiIndex`,
`WriteDpi`, `WriteRate`). The tool sends no separate save command, and none
was needed: the values were read back from the mouse.

| Command | Payload |
|---|---|
| `0x26` lift-off | `[level]`, 1 or 2 |
| `0x12` active stage | `06 [slot] [that slot's dpi u16 LE]` |
| `0x28` DPI table, per axis | `[axis] [enabled mask] [6 × dpi u16 LE]`; the tool writes X then Y |
| `0x0D` polling | `[rate count] [active index] 00 00 00 00 [6 × Hz u16 LE]` (rates two bytes later than in the read) |

DPI is 50–26,000 in steps of 50, the vendor tool's limits for this mouse.

On hardware, each write was made, read back and restored: polling 500 → 1000
Hz, lift-off 1 → 2, active stage 2 → 1 (three rounds), and an inactive stage
6400 → 6350 DPI; the mouse ended in exactly its starting state. Once, the read
right after a stage change went unanswered, so the driver retries its
verification reads. `readStatus` retries the settings it reports for the same
reason: the panel reads the status right after each write. With that, the
OpenMouse hardware test's write round-trip passes (DPI, polling 1000 → 2000 →
1000 Hz, lift-off).

### Other mouse ids

A read-only sweep of `0x00`–`0x3F` (every known id above, `0x1E` and `0x20`
skipped; link and active DPI re-read after every id, all unchanged), waiting
for the mouse's own reply past the acknowledgement:

- `0x0E` and `0x13` return the active DPI and the stage table in two other
layouts (`03 06 60 09 …` and `06 03 20 03 …` with the mouse on stage 4 of 6);
- `0x17` returns one byte, `01`, of unknown meaning;
- `0x1B` and `0x2E` get only the receiver's acknowledgement;
- every other id, `0x1A` included, gets an empty `11`.

## Not verified, not implemented

- **Other writes.** The vendor tool also writes buttons, macros, the
profile, wheel settings and left-handed mode with op `04`. None of them has
been sent to hardware for this driver.
- **Stage count and mask.** The driver keeps the enabled mask and edits
values only; turning stages on or off has not been tried.
- **Lift-off levels.** Mapped to `Low` (1) and `High` (2) after the order of
the tool's two options; the distances are not documented.

## Battery

Not available through the receiver:

- the vendor tool never reads a battery value for PID `0x5206`;
- `0x1A`, the opcode of the vendor DLL's `ReadBAT` export (used for other
products) and the read r8-rs tries, answers with an empty `11`; r8-rs's
README also notes that the battery is not exposed over this link;
- the mouse-id sweep above returns nothing battery-like, nor do receiver
classes `06 02 xx` and `06 03 xx` (`xx` = `0x00`–`0x3F`), and charging on
the dock changes no reply.

## Do not send

- Mouse `0x1E` and `0x20` (with op `01`) drop the mouse off the receiver. It
comes back after moving it or switching it off and on, with its settings
intact.
- Receiver `06 04 …` erases flash and `06 07 …` resets the receiver (the
vendor tool's update path, opcodes `0x0004`/`0x0007`; `0x00C1`/`0x00C3`
start the update and read its CRC). Never sweep receiver classes blindly.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
"types": "./dist/kmart/index.d.ts",
"import": "./dist/kmart/index.js"
},
"./8bitdo": {
"types": "./dist/8bitdo/index.d.ts",
"import": "./dist/8bitdo/index.js"
},
"./vaxee": {
"types": "./dist/vaxee/index.d.ts",
"import": "./dist/vaxee/index.js"
Expand Down
163 changes: 163 additions & 0 deletions src/8bitdo/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
EIGHTBITDO_CMD,
EIGHTBITDO_RECEIVER_QUERY,
EIGHTBITDO_REPORT_SIZE,
eightBitDoDecodeReply,
eightBitDoEncodeActiveDpiWrite,
eightBitDoEncodeDpiTableWrite,
eightBitDoEncodeLiftOffWrite,
eightBitDoEncodePollingWrite,
eightBitDoEnabledStages,
eightBitDoEncodeMouseRead,
eightBitDoEncodeMouseVersionRead,
eightBitDoEncodeReceiverRead,
eightBitDoIsForwarded,
eightBitDoParseActiveDpi,
eightBitDoParseDpiTable,
eightBitDoParseEdition,
eightBitDoParseFirmwareVersion,
eightBitDoParseLiftOff,
eightBitDoParseLink,
eightBitDoParsePolling,
type EightBitDoMouseCommand,
} from "./index.ts";

const bytes = (hex: string) => {
const out = new Uint8Array(EIGHTBITDO_REPORT_SIZE);
out.set(hex.trim().split(/\s+/).map((b) => Number.parseInt(b, 16)));
return out;
};
const hex = (data: Uint8Array, length = data.length) =>
[...data.subarray(0, length)].map((b) => b.toString(16).padStart(2, "0")).join(" ");

// Replies read from a Retro R8 N Edition through its receiver (trailing zeros trimmed).
const REPLY = {
linked: "26 03 0a 11 01",
unlinked: "26 03 0a 11",
activeDpi: "01 06 12 11 06 01 b0 04 20 03 b0 04 40 06 60 09 80 0c 00 19",
activeDpiStep: "01 06 12 11 06 03 60 09 20 03 b0 04 40 06 60 09 80 0c 00 19",
dpiTableX: "01 06 28 11 00 3f 20 03 b0 04 40 06 60 09 80 0c 00 19",
polling: "01 06 0d 11 05 01 00 00 fa 00 f4 01 e8 03 d0 07 a0 0f",
liftOff: "01 06 26 11 01",
edition: "01 06 25 11 00 00 00 01",
mouseVersion: "01 04 04 11 50 00 10 03 01 50 01 00 0b 25 12 30 10 50 03",
receiverVersion: "26 02 03 11 50 00 10 03 02 50 01 00 0a 25 02 27 17 20 0e 01",
// The receiver's immediate acknowledgement; the mouse's reply follows it.
forwarded: "21 06 12 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14",
};

const reply = (request: Uint8Array, text: string) => eightBitDoDecodeReply(bytes(text), request);

test("encodes 64-byte read frames for the mouse and the receiver", () => {
const dpi = eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.activeDpi);
assert.equal(dpi.length, 64);
assert.equal(hex(dpi, 5), "01 06 12 01 00");
assert.equal(hex(eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.dpiTable, [1]), 5), "01 06 28 01 01");
assert.equal(hex(eightBitDoEncodeReceiverRead(EIGHTBITDO_RECEIVER_QUERY.link), 4), "06 03 0a 01");
});

test("refuses to encode anything but the known reads", () => {
// 0x1E drops the R8 off its receiver; 0x33 is the vendor tool's profile wipe.
for (const command of [0x1e, 0x20, 0x33]) {
assert.throws(() => eightBitDoEncodeMouseRead(command as EightBitDoMouseCommand), RangeError);
}
});

test("decodes replies only for the request they answer", () => {
const link = eightBitDoEncodeReceiverRead(EIGHTBITDO_RECEIVER_QUERY.link);
const dpi = eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.activeDpi);
assert.equal(eightBitDoParseLink(reply(link, REPLY.linked)!), true);
assert.equal(eightBitDoParseLink(reply(link, REPLY.unlinked)!), false);
assert.equal(reply(dpi, REPLY.linked), null);
assert.equal(reply(link, REPLY.activeDpi), null);
assert.equal(reply(dpi, REPLY.forwarded), null);
assert.equal(eightBitDoIsForwarded(bytes(REPLY.forwarded), dpi), true);
assert.equal(eightBitDoDecodeReply(new Uint8Array([1, 6]), dpi), null);
});

test("parses the active DPI and follows the DPI button", () => {
const dpi = eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.activeDpi);
assert.deepEqual(eightBitDoParseActiveDpi(reply(dpi, REPLY.activeDpi)!), { stageCount: 6, activeStage: 1, dpi: 1200 });
assert.deepEqual(eightBitDoParseActiveDpi(reply(dpi, REPLY.activeDpiStep)!), { stageCount: 6, activeStage: 3, dpi: 2400 });
assert.equal(eightBitDoParseActiveDpi(new Uint8Array([6, 6, 0x20, 0x03])), null);
});

test("parses the DPI stage table and its enabled mask", () => {
const table = eightBitDoParseDpiTable(reply(eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.dpiTable, [0]), REPLY.dpiTableX)!)!;
assert.deepEqual(table, { axis: 0, enabledMask: 0x3f, stages: [800, 1200, 1600, 2400, 3200, 6400] });
assert.deepEqual(eightBitDoEnabledStages({ ...table, enabledMask: 0b000101 }), [800, 1600]);
});

test("parses polling as an index into the mouse's own rate list", () => {
const polling = eightBitDoParsePolling(reply(eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.pollingRate), REPLY.polling)!);
assert.deepEqual(polling, {
rates: [250, 500, 1000, 2000, 4000],
slots: [250, 500, 1000, 2000, 4000, 0],
count: 5,
activeIndex: 1,
rateHz: 500,
});
assert.equal(eightBitDoParsePolling(new Uint8Array([5, 7, 0, 0, 0xfa, 0])), null);
});

test("parses lift-off and edition", () => {
assert.equal(eightBitDoParseLiftOff(reply(eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.liftOff), REPLY.liftOff)!), 1);
assert.equal(eightBitDoParseLiftOff(new Uint8Array([3])), null);
assert.equal(eightBitDoParseEdition(reply(eightBitDoEncodeMouseRead(EIGHTBITDO_CMD.edition), REPLY.edition)!), "N Edition");
assert.equal(eightBitDoParseEdition(new Uint8Array([0, 0, 0, 0x42])), null);
});

test("parses firmware versions the way 8BitDo's catalog writes them", () => {
// The catalog lists the newest mouse firmware as 11.03 and receiver firmware as 10.14.
const mouse = eightBitDoEncodeMouseVersionRead();
const receiver = eightBitDoEncodeReceiverRead(EIGHTBITDO_RECEIVER_QUERY.version);
assert.equal(hex(mouse, 4), "01 04 04 01");
assert.equal(hex(receiver, 4), "06 02 03 01");
assert.equal(eightBitDoParseFirmwareVersion(reply(mouse, REPLY.mouseVersion)!), "11.03");
assert.equal(eightBitDoParseFirmwareVersion(reply(receiver, REPLY.receiverVersion)!), "10.14");
assert.equal(eightBitDoParseFirmwareVersion(new Uint8Array(15)), null);
assert.equal(eightBitDoParseFirmwareVersion(new Uint8Array(8)), null);
});

test("encodes the vendor tool's write frames", () => {
assert.equal(hex(eightBitDoEncodeLiftOffWrite(2), 5), "01 06 26 04 02");
// Stage 2 (slot 1) at 1200 DPI.
assert.equal(hex(eightBitDoEncodeActiveDpiWrite(1, 1200), 8), "01 06 12 04 06 01 b0 04");
assert.equal(
hex(eightBitDoEncodeDpiTableWrite({ axis: 1, enabledMask: 0x3f, stages: [800, 1200, 1600, 2400, 3200, 6400] }), 18),
"01 06 28 04 01 3f 20 03 b0 04 40 06 60 09 80 0c 00 19",
);
// 1000 Hz: the rate list is written back two bytes later than it is read.
const polling = eightBitDoParsePolling(bytes("05 01 00 00 fa 00 f4 01 e8 03 d0 07 a0 0f"))!;
assert.equal(
hex(eightBitDoEncodePollingWrite(polling, 2), 22),
"01 06 0d 04 05 02 00 00 00 00 fa 00 f4 01 e8 03 d0 07 a0 0f 00 00",
);
});

test("rejects writes the mouse cannot take", () => {
assert.throws(() => eightBitDoEncodeLiftOffWrite(3 as 1), RangeError);
assert.throws(() => eightBitDoEncodeActiveDpiWrite(6, 800), RangeError);
assert.throws(() => eightBitDoEncodeActiveDpiWrite(0, 825), RangeError);
assert.throws(() => eightBitDoEncodeActiveDpiWrite(0, 26050), RangeError);
assert.throws(() => eightBitDoEncodeDpiTableWrite({ axis: 0, enabledMask: 0x3f, stages: [800, 0, 1600, 2400, 3200, 6400] }), RangeError);
// A disabled stage may hold 0.
assert.doesNotThrow(() => eightBitDoEncodeDpiTableWrite({ axis: 0, enabledMask: 0x3d, stages: [800, 0, 1600, 2400, 3200, 6400] }));
const polling = eightBitDoParsePolling(bytes("05 01 00 00 fa 00 f4 01 e8 03 d0 07 a0 0f"))!;
assert.throws(() => eightBitDoEncodePollingWrite(polling, 5), RangeError);
});

test("accepts a write reply only with the write bit", () => {
const write = eightBitDoEncodeLiftOffWrite(1);
assert.notEqual(eightBitDoDecodeReply(bytes("01 06 26 14"), write), null);
// The read reply to the same command does not confirm a write.
assert.equal(eightBitDoDecodeReply(bytes("01 06 26 11 01"), write), null);
});

test("refuses receiver queries outside the known reads", () => {
assert.throws(() => eightBitDoEncodeReceiverRead([0x04, 0x00] as never), RangeError);
assert.throws(() => eightBitDoEncodeReceiverRead([0x07, 0x00] as never), RangeError);
assert.equal(eightBitDoEncodeReceiverRead(EIGHTBITDO_RECEIVER_QUERY.version)[1], 0x02);
});
Loading