mode_xbox_og.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. #include "mode_xbox_og.h"
  2. #include "triton.h"
  3. #include "gamepad_util.h"
  4. #include "config.h"
  5. #include "haptics.h"
  6. #include "bonds.h"
  7. #include "usb_tx.h"
  8. #include "usb_app_drivers.h"
  9. #include <Adafruit_TinyUSB.h>
  10. #include <Arduino.h>
  11. #include <string.h>
  12. #define XBOX_OG_DESC_LEN (9 + 7 + 7)
  13. #define XBOX_OG_PACKET_SIZE 32
  14. #define XBOX_OG_CONN_MS 1200u
  15. enum {
  16. XBOX_OG_DUP = 0x01,
  17. XBOX_OG_DDOWN = 0x02,
  18. XBOX_OG_DLEFT = 0x04,
  19. XBOX_OG_DRIGHT = 0x08,
  20. XBOX_OG_START = 0x10,
  21. XBOX_OG_BACK = 0x20,
  22. XBOX_OG_L3 = 0x40,
  23. XBOX_OG_R3 = 0x80,
  24. };
  25. #pragma pack(push, 1)
  26. struct XboxOgInputReport {
  27. uint8_t reserved;
  28. uint8_t length;
  29. uint8_t buttons;
  30. uint8_t reserved2;
  31. uint8_t a;
  32. uint8_t b;
  33. uint8_t x;
  34. uint8_t y;
  35. uint8_t black;
  36. uint8_t white;
  37. uint8_t left_trigger;
  38. uint8_t right_trigger;
  39. int16_t left_x;
  40. int16_t left_y;
  41. int16_t right_x;
  42. int16_t right_y;
  43. };
  44. struct XboxOgOutputReport {
  45. uint8_t reserved;
  46. uint8_t length;
  47. uint16_t left_motor;
  48. uint16_t right_motor;
  49. };
  50. #pragma pack(pop)
  51. static const uint8_t XID_DESCRIPTOR[] = {
  52. 0x10, 0x42, 0x00, 0x01, 0x01, 0x02, 0x14, 0x06,
  53. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  54. };
  55. static const uint8_t XID_CAPABILITIES_IN[] = {
  56. 0x00, 0x14, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  57. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  58. };
  59. static const uint8_t XID_CAPABILITIES_OUT[] = {
  60. 0x00, 0x06, 0xFF, 0xFF, 0xFF, 0xFF,
  61. };
  62. static_assert(sizeof(XboxOgInputReport) == 20,
  63. "Original Xbox input report must be 20 bytes");
  64. static_assert(sizeof(XboxOgOutputReport) == 6,
  65. "Original Xbox output report must be 6 bytes");
  66. static_assert(sizeof(XID_DESCRIPTOR) == 16, "XID descriptor must be 16 bytes");
  67. static_assert(sizeof(XID_CAPABILITIES_IN) == 20,
  68. "XID input capabilities must be 20 bytes");
  69. static_assert(sizeof(XID_CAPABILITIES_OUT) == 6,
  70. "XID output capabilities must be 6 bytes");
  71. struct XboxOgSlot {
  72. uint8_t interface_number;
  73. uint8_t endpoint_in;
  74. uint8_t endpoint_out;
  75. // The loop owns bond selection and is its only reader -- it snapshots
  76. // bond_slot when relaying rumble; lifecycle callbacks only clear it.
  77. volatile int8_t bond_slot;
  78. // TinyUSB callbacks publish endpoint readiness from the usbd task; the
  79. // loop owns pending input state.
  80. volatile bool in_use;
  81. volatile bool tx_pending;
  82. // TinyUSB ignores xfer_cb's return value, so the loop drain must remember
  83. // and recover a failed runtime OUT rearm.
  84. volatile bool output_rearm_pending;
  85. // The loop publishes complete reports under PRIMASK; USB only snapshots it.
  86. XboxOgInputReport latest_report;
  87. // These four are owned by the USB peripheral, not by a lock, so the
  88. // rules below hold only while the endpoints are live -- bus reset and
  89. // deinit clear them wholesale. See xboxOgClearSlot().
  90. // EP0 IN owns this snapshot until the control transfer completes.
  91. uint8_t control_in_buffer[sizeof(XboxOgInputReport)];
  92. // The loop may replace this only while the interrupt IN endpoint is idle.
  93. CFG_TUD_MEM_ALIGN uint8_t tx_buffer[XBOX_OG_PACKET_SIZE];
  94. // Interrupt OUT DMA owns this buffer between arm and completion.
  95. CFG_TUD_MEM_ALIGN uint8_t output_buffer[XBOX_OG_PACKET_SIZE];
  96. // EP0 OUT owns this buffer from SET_REPORT setup through its data stage.
  97. uint8_t control_out_buffer[sizeof(XboxOgOutputReport)];
  98. // USB output callbacks publish this mailbox; the loop consumes it and
  99. // relays to whichever slot is currently bonded when it does.
  100. volatile uint16_t rumble_low;
  101. volatile uint16_t rumble_high;
  102. volatile bool rumble_pending;
  103. };
  104. static XboxOgSlot g_xboxOgSlot;
  105. static volatile uint32_t g_xboxOgGeneration;
  106. static unsigned long g_xboxOgNeutralMs;
  107. static void xboxOgNeutralReport(XboxOgInputReport &report)
  108. {
  109. memset(&report, 0, sizeof report);
  110. report.length = sizeof report;
  111. }
  112. // Wholesale clear, DMA buffers included. Every caller runs at a point where
  113. // no transfer is armed against them: mode setup runs before attach, init /
  114. // deinit / bus reset arrive after the peripheral has dropped the endpoints,
  115. // and xboxOgOpen clears before it arms the OUT endpoint (or after closing
  116. // both on the failure path).
  117. static void xboxOgClearSlot(void)
  118. {
  119. uint32_t primask = __get_PRIMASK();
  120. __disable_irq();
  121. g_xboxOgGeneration++;
  122. memset(&g_xboxOgSlot, 0, sizeof g_xboxOgSlot);
  123. g_xboxOgSlot.bond_slot = -1;
  124. xboxOgNeutralReport(g_xboxOgSlot.latest_report);
  125. __set_PRIMASK(primask);
  126. }
  127. static void xboxOgSnapshotReport(uint8_t *destination)
  128. {
  129. uint32_t primask = __get_PRIMASK();
  130. __disable_irq();
  131. memcpy(destination, &g_xboxOgSlot.latest_report,
  132. sizeof g_xboxOgSlot.latest_report);
  133. __set_PRIMASK(primask);
  134. }
  135. static void xboxOgPublishReport(const XboxOgInputReport &report)
  136. {
  137. uint32_t primask = __get_PRIMASK();
  138. __disable_irq();
  139. memcpy(&g_xboxOgSlot.latest_report, &report, sizeof report);
  140. g_xboxOgSlot.tx_pending = true;
  141. __set_PRIMASK(primask);
  142. }
  143. static bool xboxOgPublishBondReport(const XboxOgInputReport &report, int slot,
  144. uint32_t generation)
  145. {
  146. uint32_t primask = __get_PRIMASK();
  147. bool published = false;
  148. __disable_irq();
  149. if (g_xboxOgGeneration == generation &&
  150. g_xboxOgSlot.bond_slot == slot) {
  151. memcpy(&g_xboxOgSlot.latest_report, &report, sizeof report);
  152. g_xboxOgSlot.tx_pending = true;
  153. published = true;
  154. }
  155. __set_PRIMASK(primask);
  156. return published;
  157. }
  158. static bool xboxOgApplyRumble(XboxOgSlot &slot, const uint8_t *data,
  159. uint16_t length)
  160. {
  161. if (!data || length < sizeof(XboxOgOutputReport) || data[0] != 0x00 ||
  162. data[1] != sizeof(XboxOgOutputReport))
  163. return false;
  164. uint16_t low = (uint16_t)data[2] | ((uint16_t)data[3] << 8);
  165. uint16_t high = (uint16_t)data[4] | ((uint16_t)data[5] << 8);
  166. uint32_t primask = __get_PRIMASK();
  167. __disable_irq();
  168. slot.rumble_low = low;
  169. slot.rumble_high = high;
  170. slot.rumble_pending = true;
  171. __set_PRIMASK(primask);
  172. return true;
  173. }
  174. static void xboxOgRetryOutputRearm(void)
  175. {
  176. XboxOgSlot &slot = g_xboxOgSlot;
  177. uint32_t generation = g_xboxOgGeneration;
  178. uint8_t endpoint = slot.endpoint_out;
  179. if (!tud_mounted() || !slot.in_use || !slot.output_rearm_pending ||
  180. endpoint == 0 || usbd_edpt_busy(0, endpoint) ||
  181. !usbd_edpt_claim(0, endpoint))
  182. return;
  183. if (usbd_edpt_xfer(0, endpoint, slot.output_buffer,
  184. sizeof slot.output_buffer)) {
  185. uint32_t primask = __get_PRIMASK();
  186. __disable_irq();
  187. if (g_xboxOgGeneration == generation && slot.in_use &&
  188. slot.endpoint_out == endpoint)
  189. slot.output_rearm_pending = false;
  190. __set_PRIMASK(primask);
  191. } else {
  192. usbd_edpt_release(0, endpoint);
  193. }
  194. }
  195. static void xboxOgTxDrain(void)
  196. {
  197. XboxOgSlot &slot = g_xboxOgSlot;
  198. xboxOgRetryOutputRearm();
  199. if (!tud_mounted() || !slot.in_use || !slot.tx_pending ||
  200. slot.endpoint_in == 0 || usbd_edpt_busy(0, slot.endpoint_in))
  201. return;
  202. if (!usbd_edpt_claim(0, slot.endpoint_in))
  203. return;
  204. xboxOgSnapshotReport(slot.tx_buffer);
  205. if (usbd_edpt_xfer(0, slot.endpoint_in, slot.tx_buffer,
  206. sizeof(XboxOgInputReport))) {
  207. slot.tx_pending = false;
  208. } else {
  209. usbd_edpt_release(0, slot.endpoint_in);
  210. }
  211. }
  212. // The console asks for these three before it accepts the controller: 0x06
  213. // with wValue 0x4200 is the XID descriptor, 0x01 with 0x0100/0x0200 the
  214. // input and output capabilities. Stalling them makes it refuse the pad.
  215. static bool xboxOgVendorRequest(const tusb_control_request_t *request)
  216. {
  217. if (!g_xboxOgSlot.in_use || request->bmRequestType != 0xC1 ||
  218. request->wIndex != g_xboxOgSlot.interface_number)
  219. return false;
  220. if (request->bRequest == 0x06 && request->wValue == 0x4200)
  221. return true;
  222. if (request->bRequest != 0x01)
  223. return false;
  224. return request->wValue == 0x0100 || request->wValue == 0x0200;
  225. }
  226. bool xboxOgVendorControlXfer(uint8_t rhport, uint8_t stage,
  227. const tusb_control_request_t *request)
  228. {
  229. if (!request || !xboxOgVendorRequest(request))
  230. return false;
  231. if (stage != CONTROL_STAGE_SETUP)
  232. return true;
  233. if (request->bRequest == 0x06)
  234. return tud_control_xfer(rhport, request,
  235. const_cast<uint8_t *>(XID_DESCRIPTOR),
  236. sizeof XID_DESCRIPTOR);
  237. if (request->wValue == 0x0100)
  238. return tud_control_xfer(
  239. rhport, request,
  240. const_cast<uint8_t *>(XID_CAPABILITIES_IN),
  241. sizeof XID_CAPABILITIES_IN);
  242. return tud_control_xfer(rhport, request,
  243. const_cast<uint8_t *>(XID_CAPABILITIES_OUT),
  244. sizeof XID_CAPABILITIES_OUT);
  245. }
  246. // Controller S also carries input and rumble over EP0, in parallel with the
  247. // interrupt endpoints: GET_REPORT (0xA1/0x01, wValue 0x0100) and SET_REPORT
  248. // (0x21/0x09, wValue 0x0200).
  249. static bool xboxOgClassControlXfer(uint8_t rhport, uint8_t stage,
  250. const tusb_control_request_t *request)
  251. {
  252. if (!request || !g_xboxOgSlot.in_use ||
  253. request->wIndex != g_xboxOgSlot.interface_number)
  254. return false;
  255. if (request->bmRequestType == 0xA1 && request->bRequest == 0x01 &&
  256. request->wValue == 0x0100) {
  257. if (stage == CONTROL_STAGE_SETUP) {
  258. xboxOgSnapshotReport(g_xboxOgSlot.control_in_buffer);
  259. return tud_control_xfer(
  260. rhport, request, g_xboxOgSlot.control_in_buffer,
  261. sizeof g_xboxOgSlot.control_in_buffer);
  262. }
  263. return true;
  264. }
  265. if (request->bmRequestType != 0x21 || request->bRequest != 0x09 ||
  266. request->wValue != 0x0200 ||
  267. request->wLength != sizeof(XboxOgOutputReport))
  268. return false;
  269. if (stage == CONTROL_STAGE_SETUP) {
  270. memset(g_xboxOgSlot.control_out_buffer, 0,
  271. sizeof g_xboxOgSlot.control_out_buffer);
  272. return tud_control_xfer(rhport, request,
  273. g_xboxOgSlot.control_out_buffer,
  274. sizeof g_xboxOgSlot.control_out_buffer);
  275. }
  276. if (stage == CONTROL_STAGE_DATA)
  277. return xboxOgApplyRumble(
  278. g_xboxOgSlot, g_xboxOgSlot.control_out_buffer,
  279. sizeof g_xboxOgSlot.control_out_buffer);
  280. return true;
  281. }
  282. static void xboxOgInit(void)
  283. {
  284. xboxOgClearSlot();
  285. }
  286. static bool xboxOgDeinit(void)
  287. {
  288. xboxOgClearSlot();
  289. return true;
  290. }
  291. static void xboxOgReset(uint8_t rhport)
  292. {
  293. (void)rhport;
  294. xboxOgClearSlot();
  295. }
  296. static bool xboxOgEndpointValid(const tusb_desc_endpoint_t *endpoint)
  297. {
  298. return endpoint->bLength == sizeof(tusb_desc_endpoint_t) &&
  299. endpoint->bDescriptorType == TUSB_DESC_ENDPOINT &&
  300. endpoint->bmAttributes.xfer == TUSB_XFER_INTERRUPT &&
  301. endpoint->bmAttributes.sync == 0 &&
  302. endpoint->bmAttributes.usage == 0 &&
  303. (endpoint->bEndpointAddress & 0x70) == 0 &&
  304. tu_edpt_number(endpoint->bEndpointAddress) != 0 &&
  305. tu_le16toh(endpoint->wMaxPacketSize) == XBOX_OG_PACKET_SIZE &&
  306. endpoint->bInterval == 4;
  307. }
  308. static uint16_t xboxOgOpen(uint8_t rhport,
  309. const tusb_desc_interface_t *interface,
  310. uint16_t max_length)
  311. {
  312. if (!interface || g_xboxOgSlot.in_use ||
  313. max_length < sizeof(tusb_desc_interface_t) ||
  314. interface->bLength != sizeof(tusb_desc_interface_t) ||
  315. interface->bDescriptorType != TUSB_DESC_INTERFACE ||
  316. interface->bAlternateSetting != 0 || interface->iInterface != 0 ||
  317. interface->bNumEndpoints != 2 ||
  318. interface->bInterfaceClass != 0x58 ||
  319. interface->bInterfaceSubClass != 0x42 ||
  320. interface->bInterfaceProtocol != 0x00)
  321. return 0;
  322. const uint8_t *start = reinterpret_cast<const uint8_t *>(interface);
  323. const uint8_t *cursor = start + interface->bLength;
  324. const uint8_t *end = start + max_length;
  325. const tusb_desc_endpoint_t *endpoint_in = nullptr;
  326. const tusb_desc_endpoint_t *endpoint_out = nullptr;
  327. uint8_t endpoint_count = 0;
  328. while (cursor < end) {
  329. uint16_t remaining = (uint16_t)(end - cursor);
  330. if (remaining < 2 || cursor[0] < 2 || cursor[0] > remaining)
  331. return 0;
  332. if (cursor[1] == TUSB_DESC_INTERFACE) {
  333. if (cursor[0] < sizeof(tusb_desc_interface_t))
  334. return 0;
  335. break;
  336. }
  337. if (cursor[1] == TUSB_DESC_INTERFACE_ASSOCIATION) {
  338. if (cursor[0] < sizeof(tusb_desc_interface_assoc_t))
  339. return 0;
  340. break;
  341. }
  342. if (cursor[1] == TUSB_DESC_ENDPOINT) {
  343. if (cursor[0] < sizeof(tusb_desc_endpoint_t))
  344. return 0;
  345. const tusb_desc_endpoint_t *endpoint =
  346. reinterpret_cast<const tusb_desc_endpoint_t *>(
  347. cursor);
  348. if (!xboxOgEndpointValid(endpoint) ||
  349. ++endpoint_count > interface->bNumEndpoints)
  350. return 0;
  351. if (tu_edpt_dir(endpoint->bEndpointAddress) ==
  352. TUSB_DIR_IN) {
  353. if (endpoint_in)
  354. return 0;
  355. endpoint_in = endpoint;
  356. } else {
  357. if (endpoint_out)
  358. return 0;
  359. endpoint_out = endpoint;
  360. }
  361. }
  362. cursor += cursor[0];
  363. }
  364. if (endpoint_count != 2 || !endpoint_in || !endpoint_out)
  365. return 0;
  366. if (!usbd_edpt_open(rhport, endpoint_in))
  367. return 0;
  368. if (!usbd_edpt_open(rhport, endpoint_out)) {
  369. usbd_edpt_close(rhport, endpoint_in->bEndpointAddress);
  370. return 0;
  371. }
  372. xboxOgClearSlot();
  373. g_xboxOgSlot.interface_number = interface->bInterfaceNumber;
  374. g_xboxOgSlot.endpoint_in = endpoint_in->bEndpointAddress;
  375. g_xboxOgSlot.endpoint_out = endpoint_out->bEndpointAddress;
  376. g_xboxOgSlot.in_use = true;
  377. if (!usbd_edpt_xfer(rhport, g_xboxOgSlot.endpoint_out,
  378. g_xboxOgSlot.output_buffer,
  379. sizeof g_xboxOgSlot.output_buffer)) {
  380. usbd_edpt_close(rhport, g_xboxOgSlot.endpoint_in);
  381. usbd_edpt_close(rhport, g_xboxOgSlot.endpoint_out);
  382. xboxOgClearSlot();
  383. return 0;
  384. }
  385. return (uint16_t)(cursor - start);
  386. }
  387. static bool xboxOgXfer(uint8_t rhport, uint8_t endpoint, xfer_result_t result,
  388. uint32_t transferred)
  389. {
  390. if (!g_xboxOgSlot.in_use)
  391. return false;
  392. if (endpoint == g_xboxOgSlot.endpoint_in)
  393. return true;
  394. if (endpoint != g_xboxOgSlot.endpoint_out)
  395. return false;
  396. if (result == XFER_RESULT_SUCCESS)
  397. xboxOgApplyRumble(g_xboxOgSlot, g_xboxOgSlot.output_buffer,
  398. (uint16_t)transferred);
  399. if (!g_xboxOgSlot.in_use)
  400. return true;
  401. if (!usbd_edpt_xfer(rhport, g_xboxOgSlot.endpoint_out,
  402. g_xboxOgSlot.output_buffer,
  403. sizeof g_xboxOgSlot.output_buffer)) {
  404. uint32_t primask = __get_PRIMASK();
  405. __disable_irq();
  406. if (g_xboxOgSlot.in_use &&
  407. endpoint == g_xboxOgSlot.endpoint_out)
  408. g_xboxOgSlot.output_rearm_pending = true;
  409. __set_PRIMASK(primask);
  410. }
  411. return true;
  412. }
  413. static const usbd_class_driver_t g_xboxOgDriver = {
  414. #if CFG_TUSB_DEBUG >= 2
  415. .name = "XID",
  416. #endif
  417. .init = xboxOgInit,
  418. .deinit = xboxOgDeinit,
  419. .reset = xboxOgReset,
  420. .open = xboxOgOpen,
  421. .control_xfer_cb = xboxOgClassControlXfer,
  422. .xfer_cb = xboxOgXfer,
  423. .sof = nullptr,
  424. };
  425. const usbd_class_driver_t *xboxOgClassDriver(void)
  426. {
  427. return &g_xboxOgDriver;
  428. }
  429. class Adafruit_USBD_XboxOg : public Adafruit_USBD_Interface {
  430. public:
  431. uint16_t getInterfaceDescriptor(uint8_t, uint8_t *buffer,
  432. uint16_t buffer_size) override
  433. {
  434. if (!buffer)
  435. return XBOX_OG_DESC_LEN;
  436. if (buffer_size < XBOX_OG_DESC_LEN)
  437. return 0;
  438. uint8_t interface_number = TinyUSBDevice.allocInterface(1);
  439. uint8_t endpoint_in = TinyUSBDevice.allocEndpoint(TUSB_DIR_IN);
  440. uint8_t endpoint_out =
  441. TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
  442. const uint8_t descriptor[XBOX_OG_DESC_LEN] = {
  443. 9,
  444. TUSB_DESC_INTERFACE,
  445. interface_number,
  446. 0,
  447. 2,
  448. 0x58,
  449. 0x42,
  450. 0x00,
  451. 0,
  452. 7,
  453. TUSB_DESC_ENDPOINT,
  454. endpoint_in,
  455. TUSB_XFER_INTERRUPT,
  456. U16_TO_U8S_LE(XBOX_OG_PACKET_SIZE),
  457. 4,
  458. 7,
  459. TUSB_DESC_ENDPOINT,
  460. endpoint_out,
  461. TUSB_XFER_INTERRUPT,
  462. U16_TO_U8S_LE(XBOX_OG_PACKET_SIZE),
  463. 4,
  464. };
  465. memcpy(buffer, descriptor, sizeof descriptor);
  466. return sizeof descriptor;
  467. }
  468. bool begin()
  469. {
  470. return TinyUSBDevice.addInterface(*this);
  471. }
  472. };
  473. static Adafruit_USBD_XboxOg g_xboxOgInterface;
  474. XboxOgController g_xboxOgCtl;
  475. enum XboxOgRemapTarget : uint8_t {
  476. XBOX_OG_TARGET_NONE,
  477. XBOX_OG_TARGET_A,
  478. XBOX_OG_TARGET_B,
  479. XBOX_OG_TARGET_X,
  480. XBOX_OG_TARGET_Y,
  481. XBOX_OG_TARGET_WHITE,
  482. XBOX_OG_TARGET_BLACK,
  483. XBOX_OG_TARGET_L3,
  484. XBOX_OG_TARGET_R3,
  485. XBOX_OG_TARGET_BACK,
  486. XBOX_OG_TARGET_START,
  487. XBOX_OG_TARGET_DUP,
  488. XBOX_OG_TARGET_DDOWN,
  489. XBOX_OG_TARGET_DLEFT,
  490. XBOX_OG_TARGET_DRIGHT,
  491. };
  492. // Remaps target Controller S outputs independently of the RF button masks.
  493. // Codes come from the shared remap code space: 5/6 (LB/RB) drive White/Black,
  494. // 9/10 are semantic Back/Start, and 11 (Guide) has no Controller S target.
  495. static constexpr XboxOgRemapTarget XBOX_OG_REMAP[] = {
  496. XBOX_OG_TARGET_NONE, XBOX_OG_TARGET_A, XBOX_OG_TARGET_B,
  497. XBOX_OG_TARGET_X, XBOX_OG_TARGET_Y, XBOX_OG_TARGET_WHITE,
  498. XBOX_OG_TARGET_BLACK, XBOX_OG_TARGET_L3, XBOX_OG_TARGET_R3,
  499. XBOX_OG_TARGET_BACK, XBOX_OG_TARGET_START, XBOX_OG_TARGET_NONE,
  500. XBOX_OG_TARGET_DUP, XBOX_OG_TARGET_DDOWN, XBOX_OG_TARGET_DLEFT,
  501. XBOX_OG_TARGET_DRIGHT,
  502. };
  503. static_assert(sizeof XBOX_OG_REMAP / sizeof XBOX_OG_REMAP[0] == 16,
  504. "Xbox remap table must cover codes 0 through 15");
  505. static void xboxOgApplyRemap(XboxOgInputReport &report, uint8_t code)
  506. {
  507. uint32_t mapped = tritonFromCode(code);
  508. // LT/RT are codes 19/20, outside the table -- these two must stay above
  509. // the bounds check or a paddle mapped to a trigger does nothing.
  510. if (mapped & TB_L2)
  511. report.left_trigger = 0xFF;
  512. if (mapped & TB_R2)
  513. report.right_trigger = 0xFF;
  514. if (code >= sizeof XBOX_OG_REMAP / sizeof XBOX_OG_REMAP[0])
  515. return;
  516. switch (XBOX_OG_REMAP[code]) {
  517. case XBOX_OG_TARGET_A:
  518. report.a = 0xFF;
  519. break;
  520. case XBOX_OG_TARGET_B:
  521. report.b = 0xFF;
  522. break;
  523. case XBOX_OG_TARGET_X:
  524. report.x = 0xFF;
  525. break;
  526. case XBOX_OG_TARGET_Y:
  527. report.y = 0xFF;
  528. break;
  529. case XBOX_OG_TARGET_WHITE:
  530. report.white = 0xFF;
  531. break;
  532. case XBOX_OG_TARGET_BLACK:
  533. report.black = 0xFF;
  534. break;
  535. case XBOX_OG_TARGET_L3:
  536. report.buttons |= XBOX_OG_L3;
  537. break;
  538. case XBOX_OG_TARGET_R3:
  539. report.buttons |= XBOX_OG_R3;
  540. break;
  541. case XBOX_OG_TARGET_BACK:
  542. report.buttons |= XBOX_OG_BACK;
  543. break;
  544. case XBOX_OG_TARGET_START:
  545. report.buttons |= XBOX_OG_START;
  546. break;
  547. case XBOX_OG_TARGET_DUP:
  548. report.buttons |= XBOX_OG_DUP;
  549. break;
  550. case XBOX_OG_TARGET_DDOWN:
  551. report.buttons |= XBOX_OG_DDOWN;
  552. break;
  553. case XBOX_OG_TARGET_DLEFT:
  554. report.buttons |= XBOX_OG_DLEFT;
  555. break;
  556. case XBOX_OG_TARGET_DRIGHT:
  557. report.buttons |= XBOX_OG_DRIGHT;
  558. break;
  559. case XBOX_OG_TARGET_NONE:
  560. default:
  561. break;
  562. }
  563. }
  564. static void xboxOgBuildReport(XboxOgInputReport &report, const uint8_t *raw)
  565. {
  566. uint32_t buttons = btnsOf(raw);
  567. xboxOgNeutralReport(report);
  568. if (buttons & TB_DUP)
  569. report.buttons |= XBOX_OG_DUP;
  570. if (buttons & TB_DDN)
  571. report.buttons |= XBOX_OG_DDOWN;
  572. if (buttons & TB_DLF)
  573. report.buttons |= XBOX_OG_DLEFT;
  574. if (buttons & TB_DRT)
  575. report.buttons |= XBOX_OG_DRIGHT;
  576. // The bit names are the reverse of the physical labels: the left
  577. // button (View) arrives as TB_MENU and the right one as TB_VIEW.
  578. // Back is the left button on an Original Xbox, so this is positional.
  579. if (buttons & TB_VIEW)
  580. report.buttons |= XBOX_OG_START;
  581. if (buttons & TB_MENU)
  582. report.buttons |= XBOX_OG_BACK;
  583. if (buttons & TB_L3)
  584. report.buttons |= XBOX_OG_L3;
  585. if (buttons & TB_R3)
  586. report.buttons |= XBOX_OG_R3;
  587. // A/B/X/Y and White/Black are analog pressure bytes, so a digital press
  588. // writes full scale; the shoulders have no button bit at all.
  589. if (buttons & TB_A)
  590. (g_abSwap ? report.b : report.a) = 0xFF;
  591. if (buttons & TB_B)
  592. (g_abSwap ? report.a : report.b) = 0xFF;
  593. if (buttons & TB_X)
  594. (g_abSwap ? report.y : report.x) = 0xFF;
  595. if (buttons & TB_Y)
  596. (g_abSwap ? report.x : report.y) = 0xFF;
  597. if (buttons & TB_LB)
  598. report.white = 0xFF;
  599. if (buttons & TB_RB)
  600. report.black = 0xFF;
  601. report.left_trigger = trigU8(u16off(raw, 4));
  602. report.right_trigger = trigU8(u16off(raw, 6));
  603. if (buttons & TB_L2)
  604. report.left_trigger = 0xFF;
  605. if (buttons & TB_R2)
  606. report.right_trigger = 0xFF;
  607. const uint32_t paddle_buttons[] = { TB_L4, TB_R4, TB_L5, TB_R5 };
  608. for (int i = 0; i < 4; i++)
  609. if (buttons & paddle_buttons[i])
  610. xboxOgApplyRemap(report, g_back[i]);
  611. if ((buttons & TB_QAM) && g_qamMap)
  612. xboxOgApplyRemap(report, g_qamMap);
  613. // Raw-report offsets, not slotSticks(): like mode_xinput this mode decodes 0x45 in place and never
  614. // touches g_in. 16/18 = left pad X/Y, 22/24 = right pad X/Y.
  615. int16_t lx = (int16_t)s16off(raw, 8), ly = (int16_t)s16off(raw, 10),
  616. rx = (int16_t)s16off(raw, 12), ry = (int16_t)s16off(raw, 14);
  617. padStickBlend(buttons, (int16_t)s16off(raw, 16),
  618. (int16_t)s16off(raw, 18), (int16_t)s16off(raw, 22),
  619. (int16_t)s16off(raw, 24), &lx, &ly, &rx, &ry);
  620. le16(reinterpret_cast<uint8_t *>(&report.left_x), lx);
  621. le16(reinterpret_cast<uint8_t *>(&report.left_y), ly);
  622. le16(reinterpret_cast<uint8_t *>(&report.right_x), rx);
  623. le16(reinterpret_cast<uint8_t *>(&report.right_y), ry);
  624. }
  625. static bool xboxOgBondRecent(int slot, unsigned long now)
  626. {
  627. return slot >= 0 && slot < NSLOT && g_slot[slot].used &&
  628. g_connReplyMs[slot] != 0 &&
  629. (unsigned long)(now - g_connReplyMs[slot]) < XBOX_OG_CONN_MS;
  630. }
  631. static void xboxOgUpdateBond(void)
  632. {
  633. XboxOgSlot &state = g_xboxOgSlot;
  634. unsigned long now = millis();
  635. int current = state.bond_slot;
  636. if (xboxOgBondRecent(current, now))
  637. return;
  638. if (current >= 0) {
  639. XboxOgInputReport neutral;
  640. state.bond_slot = -1;
  641. xboxOgNeutralReport(neutral);
  642. xboxOgPublishReport(neutral);
  643. g_xboxOgNeutralMs = now;
  644. }
  645. for (int slot = 0; slot < NSLOT; slot++)
  646. if (xboxOgBondRecent(slot, now)) {
  647. state.bond_slot = (int8_t)slot;
  648. break;
  649. }
  650. }
  651. // Fire-and-forget: no stop is tracked or sent here. The shared relay in
  652. // haptics.cpp (burst, reconnect scrub, per-slot watchdog) stops the motor.
  653. static void xboxOgHandleRumble(void)
  654. {
  655. XboxOgSlot &state = g_xboxOgSlot;
  656. uint16_t low, high;
  657. bool pending;
  658. int8_t target;
  659. uint32_t primask = __get_PRIMASK();
  660. __disable_irq();
  661. pending = state.rumble_pending;
  662. low = state.rumble_low;
  663. high = state.rumble_high;
  664. target = state.bond_slot;
  665. state.rumble_pending = false;
  666. __set_PRIMASK(primask);
  667. if (!pending || target < 0 || !g_rumble)
  668. return;
  669. hapticSteamRumble(low, high, (uint8_t)target);
  670. }
  671. void XboxOgController::begin()
  672. {
  673. xboxOgClearSlot();
  674. g_xboxOgNeutralMs = 0;
  675. usbIdentity();
  676. USBDevice.setConfigurationMaxPower(100);
  677. g_xboxOgInterface.begin();
  678. usbTxRegisterDrain(xboxOgTxDrain);
  679. }
  680. void XboxOgController::onReport45(int slot, const uint8_t *rep, bool fresh,
  681. uint8_t bodyTlen)
  682. {
  683. (void)fresh;
  684. if (!rep || slot < 0 || slot >= NSLOT || bodyTlen < 18 ||
  685. (rep[0] != 0x45 && rep[0] != 0x42) ||
  686. g_xboxOgSlot.bond_slot != slot)
  687. return;
  688. uint32_t generation = g_xboxOgGeneration;
  689. XboxOgInputReport report;
  690. xboxOgBuildReport(report, rep);
  691. xboxOgPublishBondReport(report, slot, generation);
  692. }
  693. void XboxOgController::task()
  694. {
  695. xboxOgUpdateBond();
  696. xboxOgHandleRumble();
  697. if (g_xboxOgSlot.bond_slot >= 0 ||
  698. (unsigned long)(millis() - g_xboxOgNeutralMs) < USB_STREAM_MS)
  699. return;
  700. XboxOgInputReport neutral;
  701. g_xboxOgNeutralMs = millis();
  702. xboxOgNeutralReport(neutral);
  703. xboxOgPublishReport(neutral);
  704. }
  705. void XboxOgController::usbIdentity()
  706. {
  707. USBDevice.setID(0x045E, 0x0289);
  708. USBDevice.setDeviceVersion(0x0121);
  709. // A real Controller S sends no string descriptors (iManufacturer /
  710. // iProduct / iSerial = 0); hosts name it from usb.ids. TinyUSB pins
  711. // those indices non-zero, so mirror what usb.ids shows for 045E:0289.
  712. USBDevice.setManufacturerDescriptor("Microsoft Corp.");
  713. USBDevice.setProductDescriptor("Xbox Controller S");
  714. }