usbd_gamepad.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2026, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_hid.h"
  8. #include "usbd_gamepad.h"
  9. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t gamepad_report_buffer[64];
  10. static int xinput_vendor_class_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  11. {
  12. struct xinput_in_report xinput_report;
  13. memset(&xinput_report, 0, sizeof(xinput_report));
  14. xinput_report.report_size = 20;
  15. memcpy(*data, &xinput_report, sizeof(xinput_report));
  16. *len = sizeof(xinput_report);
  17. return 0;
  18. }
  19. int usbd_gamepad_xinput_send_report(uint8_t ep, struct usb_gamepad_report *report)
  20. {
  21. struct xinput_in_report *xinput_report;
  22. xinput_report = (struct xinput_in_report *)gamepad_report_buffer;
  23. memset(xinput_report, 0, sizeof(xinput_report));
  24. xinput_report->report_size = 20;
  25. if (report->buttons & USB_GAMEPAD_BUTTON_DU)
  26. xinput_report->buttons |= XINPUT_BUTTON_MASK_UP;
  27. if (report->buttons & USB_GAMEPAD_BUTTON_DD)
  28. xinput_report->buttons |= XINPUT_BUTTON_MASK_DOWN;
  29. if (report->buttons & USB_GAMEPAD_BUTTON_DL)
  30. xinput_report->buttons |= XINPUT_BUTTON_MASK_LEFT;
  31. if (report->buttons & USB_GAMEPAD_BUTTON_DR)
  32. xinput_report->buttons |= XINPUT_BUTTON_MASK_RIGHT;
  33. if (report->buttons & USB_GAMEPAD_BUTTON_S2)
  34. xinput_report->buttons |= XINPUT_BUTTON_MASK_START;
  35. if (report->buttons & USB_GAMEPAD_BUTTON_S1)
  36. xinput_report->buttons |= XINPUT_BUTTON_MASK_BACK;
  37. if (report->buttons & USB_GAMEPAD_BUTTON_L3)
  38. xinput_report->buttons |= XINPUT_BUTTON_MASK_L3;
  39. if (report->buttons & USB_GAMEPAD_BUTTON_R3)
  40. xinput_report->buttons |= XINPUT_BUTTON_MASK_R3;
  41. if (report->buttons & USB_GAMEPAD_BUTTON_L1)
  42. xinput_report->buttons |= XINPUT_BUTTON_MASK_LB;
  43. if (report->buttons & USB_GAMEPAD_BUTTON_R1)
  44. xinput_report->buttons |= XINPUT_BUTTON_MASK_RB;
  45. if (report->buttons & USB_GAMEPAD_BUTTON_A1)
  46. xinput_report->buttons |= XINPUT_BUTTON_MASK_GUIDE;
  47. if (report->buttons & USB_GAMEPAD_BUTTON_B1)
  48. xinput_report->buttons |= XINPUT_BUTTON_MASK_A;
  49. if (report->buttons & USB_GAMEPAD_BUTTON_B2)
  50. xinput_report->buttons |= XINPUT_BUTTON_MASK_B;
  51. if (report->buttons & USB_GAMEPAD_BUTTON_B3)
  52. xinput_report->buttons |= XINPUT_BUTTON_MASK_X;
  53. if (report->buttons & USB_GAMEPAD_BUTTON_B4)
  54. xinput_report->buttons |= XINPUT_BUTTON_MASK_Y;
  55. // Analog triggers (0-255), fall back to digital if analog is 0 but button pressed
  56. xinput_report->lt = report->lt;
  57. xinput_report->rt = report->rt;
  58. if (xinput_report->lt == 0 && (report->buttons & USB_GAMEPAD_BUTTON_L2))
  59. xinput_report->lt = 0xFF;
  60. if (xinput_report->rt == 0 && (report->buttons & USB_GAMEPAD_BUTTON_R2))
  61. xinput_report->rt = 0xFF;
  62. return usbd_ep_start_write(0, ep, gamepad_report_buffer, sizeof(struct xinput_in_report));
  63. }
  64. // Convert gamepad dpad mask to switch hat value
  65. static uint8_t convert_dpad_to_switch_hat(uint32_t buttons)
  66. {
  67. // Joypad uses active-high (1 = pressed)
  68. uint8_t up = (buttons & USB_GAMEPAD_BUTTON_DU) ? 1 : 0;
  69. uint8_t down = (buttons & USB_GAMEPAD_BUTTON_DD) ? 1 : 0;
  70. uint8_t left = (buttons & USB_GAMEPAD_BUTTON_DL) ? 1 : 0;
  71. uint8_t right = (buttons & USB_GAMEPAD_BUTTON_DR) ? 1 : 0;
  72. if (up && right)
  73. return SWITCH_HAT_UP_RIGHT;
  74. if (up && left)
  75. return SWITCH_HAT_UP_LEFT;
  76. if (down && right)
  77. return SWITCH_HAT_DOWN_RIGHT;
  78. if (down && left)
  79. return SWITCH_HAT_DOWN_LEFT;
  80. if (up)
  81. return SWITCH_HAT_UP;
  82. if (down)
  83. return SWITCH_HAT_DOWN;
  84. if (left)
  85. return SWITCH_HAT_LEFT;
  86. if (right)
  87. return SWITCH_HAT_RIGHT;
  88. return SWITCH_HAT_CENTER;
  89. }
  90. int usbd_gamepad_switch_send_report(uint8_t ep, struct usb_gamepad_report *report)
  91. {
  92. struct switch_in_report *switch_report;
  93. switch_report = (struct switch_in_report *)gamepad_report_buffer;
  94. memset(switch_report, 0, sizeof(switch_report));
  95. if (report->buttons & USB_GAMEPAD_BUTTON_S1)
  96. switch_report->buttons |= SWITCH_MASK_MINUS;
  97. if (report->buttons & USB_GAMEPAD_BUTTON_S2)
  98. switch_report->buttons |= SWITCH_MASK_PLUS;
  99. if (report->buttons & USB_GAMEPAD_BUTTON_L1)
  100. switch_report->buttons |= SWITCH_MASK_L;
  101. if (report->buttons & USB_GAMEPAD_BUTTON_R1)
  102. switch_report->buttons |= SWITCH_MASK_R;
  103. if (report->buttons & USB_GAMEPAD_BUTTON_L2)
  104. switch_report->buttons |= SWITCH_MASK_ZL;
  105. if (report->buttons & USB_GAMEPAD_BUTTON_R2)
  106. switch_report->buttons |= SWITCH_MASK_ZR;
  107. if (report->buttons & USB_GAMEPAD_BUTTON_L3)
  108. switch_report->buttons |= SWITCH_MASK_L3;
  109. if (report->buttons & USB_GAMEPAD_BUTTON_R3)
  110. switch_report->buttons |= SWITCH_MASK_R3;
  111. if (report->buttons & USB_GAMEPAD_BUTTON_A1)
  112. switch_report->buttons |= SWITCH_MASK_HOME;
  113. if (report->buttons & USB_GAMEPAD_BUTTON_A2)
  114. switch_report->buttons |= SWITCH_MASK_CAPTURE;
  115. if (report->buttons & USB_GAMEPAD_BUTTON_B1)
  116. switch_report->buttons |= SWITCH_MASK_B;
  117. if (report->buttons & USB_GAMEPAD_BUTTON_B2)
  118. switch_report->buttons |= SWITCH_MASK_A;
  119. if (report->buttons & USB_GAMEPAD_BUTTON_B3)
  120. switch_report->buttons |= SWITCH_MASK_Y;
  121. if (report->buttons & USB_GAMEPAD_BUTTON_B4)
  122. switch_report->buttons |= SWITCH_MASK_X;
  123. switch_report->hat = convert_dpad_to_switch_hat(report->buttons);
  124. // Analog sticks (HID convention: 0=up, 255=down - no inversion needed)
  125. switch_report->lx = report->lx;
  126. switch_report->ly = report->ly;
  127. switch_report->rx = report->rx;
  128. switch_report->ry = report->ry;
  129. switch_report->vendor = 0;
  130. return usbd_ep_start_write(0, ep, gamepad_report_buffer, sizeof(struct switch_in_report));
  131. }
  132. struct usbd_interface *usbd_gamepad_xinput_init_intf(struct usbd_interface *intf)
  133. {
  134. intf->class_interface_handler = NULL;
  135. intf->class_endpoint_handler = NULL;
  136. intf->vendor_handler = xinput_vendor_class_request_handler;
  137. intf->notify_handler = NULL;
  138. return intf;
  139. }
  140. static const uint8_t hid_switch_report_desc[HID_SWITCH_REPORT_DESC_SIZE] = {
  141. 0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  142. 0x09, 0x05, // Usage (Game Pad)
  143. 0xA1, 0x01, // Collection (Application)
  144. 0x15, 0x00, // Logical Minimum (0)
  145. 0x25, 0x01, // Logical Maximum (1)
  146. 0x35, 0x00, // Physical Minimum (0)
  147. 0x45, 0x01, // Physical Maximum (1)
  148. 0x75, 0x01, // Report Size (1)
  149. 0x95, 0x10, // Report Count (16)
  150. 0x05, 0x09, // Usage Page (Button)
  151. 0x19, 0x01, // Usage Minimum (Button 1)
  152. 0x29, 0x10, // Usage Maximum (Button 16)
  153. 0x81, 0x02, // Input (Data,Var,Abs)
  154. 0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  155. 0x25, 0x07, // Logical Maximum (7)
  156. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  157. 0x75, 0x04, // Report Size (4)
  158. 0x95, 0x01, // Report Count (1)
  159. 0x65, 0x14, // Unit (Eng Rot:Angular Pos)
  160. 0x09, 0x39, // Usage (Hat switch)
  161. 0x81, 0x42, // Input (Data,Var,Abs,Null)
  162. 0x65, 0x00, // Unit (None)
  163. 0x95, 0x01, // Report Count (1)
  164. 0x81, 0x01, // Input (Const) - 4-bit padding
  165. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  166. 0x46, 0xFF, 0x00, // Physical Maximum (255)
  167. 0x09, 0x30, // Usage (X) - Left Stick X
  168. 0x09, 0x31, // Usage (Y) - Left Stick Y
  169. 0x09, 0x32, // Usage (Z) - Right Stick X
  170. 0x09, 0x35, // Usage (Rz) - Right Stick Y
  171. 0x75, 0x08, // Report Size (8)
  172. 0x95, 0x04, // Report Count (4)
  173. 0x81, 0x02, // Input (Data,Var,Abs)
  174. 0x06, 0x00, 0xFF, // Usage Page (Vendor Defined)
  175. 0x09, 0x20, // Usage (0x20)
  176. 0x95, 0x01, // Report Count (1)
  177. 0x81, 0x02, // Input (Data,Var,Abs) - Vendor byte
  178. 0x0A, 0x21, 0x26, // Usage (0x2621)
  179. 0x95, 0x08, // Report Count (8)
  180. 0x91, 0x02, // Output (Data,Var,Abs) - Rumble
  181. 0xC0, // End Collection
  182. };
  183. struct usbd_interface *usbd_gamepad_switch_init_intf(struct usbd_interface *intf)
  184. {
  185. return usbd_hid_init_intf(0, intf, hid_switch_report_desc, HID_SWITCH_REPORT_DESC_SIZE);
  186. }