pof_usb_xbox360.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #include "pof_usb.h"
  2. #include "xsm3/xsm3.h"
  3. #include "furi_hal_random.h"
  4. #define TAG "POF USB XBOX360"
  5. #define HID_INTERVAL 1
  6. #define USB_EP0_SIZE 8
  7. #define POF_USB_VID (0x1430)
  8. #define POF_USB_PID (0x1F17)
  9. #define POF_USB_EP_IN (0x81)
  10. #define POF_USB_EP_OUT (0x02)
  11. #define POF_USB_X360_AUDIO_EP_IN1 (0x83)
  12. #define POF_USB_X360_AUDIO_EP_OUT1 (0x04)
  13. #define POF_USB_X360_AUDIO_EP_IN2 (0x85)
  14. #define POF_USB_X360_AUDIO_EP_OUT2 (0x06)
  15. #define POF_USB_X360_PLUGIN_MODULE_EP_IN (0x87)
  16. #define POF_USB_ACTUAL_OUTPUT_SIZE 0x20
  17. static const struct usb_string_descriptor dev_manuf_desc =
  18. USB_ARRAY_DESC(0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x00);
  19. static const struct usb_string_descriptor dev_product_desc =
  20. USB_ARRAY_DESC(0x53, 0x70, 0x79, 0x72, 0x6f, 0x20, 0x50, 0x6f, 0x72, 0x74, 0x61, 0x00);
  21. static const struct usb_string_descriptor dev_security_desc =
  22. USB_ARRAY_DESC(0x58, 0x62, 0x6f, 0x78, 0x20, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74,
  23. 0x79, 0x20, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x20, 0x33, 0x2c, 0x20,
  24. 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x31, 0x2e, 0x30, 0x30,
  25. 0x2c, 0x20, 0xa9, 0x20, 0x32, 0x30, 0x30, 0x35, 0x20, 0x4d, 0x69, 0x63,
  26. 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x43, 0x6f, 0x72, 0x70, 0x6f,
  27. 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x20, 0x41, 0x6c, 0x6c, 0x20,
  28. 0x72, 0x69, 0x67, 0x68, 0x74, 0x73, 0x20, 0x72, 0x65, 0x73, 0x65, 0x72,
  29. 0x76, 0x65, 0x64, 0x2e);
  30. static usbd_respond pof_usb_ep_config(usbd_device* dev, uint8_t cfg);
  31. static usbd_respond
  32. pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
  33. static void pof_usb_send(usbd_device* dev, uint8_t* buf, uint16_t len);
  34. static int32_t pof_usb_receive(usbd_device* dev, uint8_t* buf, uint16_t max_len);
  35. static PoFUsb* pof_cur = NULL;
  36. static int32_t pof_thread_worker(void* context) {
  37. PoFUsb* pof_usb = context;
  38. usbd_device* dev = pof_usb->dev;
  39. VirtualPortal* virtual_portal = pof_usb->virtual_portal;
  40. UNUSED(dev);
  41. uint32_t len_data = 0;
  42. uint8_t tx_data[POF_USB_TX_MAX_SIZE] = {0};
  43. uint32_t timeout = TIMEOUT_NORMAL; // FuriWaitForever; //ms
  44. uint32_t last = 0;
  45. while (true) {
  46. uint32_t now = furi_get_tick();
  47. uint32_t flags = furi_thread_flags_wait(EventAll, FuriFlagWaitAny, timeout);
  48. if (flags & EventRx) { // fast flag
  49. uint8_t buf[POF_USB_RX_MAX_SIZE];
  50. len_data = pof_usb_receive(dev, buf, POF_USB_RX_MAX_SIZE);
  51. // 360 controller packets have a header of 0x0b 0x14
  52. if (len_data > 0 && buf[0] == 0x0b && buf[1] == 0x14) {
  53. memset(tx_data, 0, sizeof(tx_data));
  54. // prepend packet with xinput header
  55. int send_len =
  56. virtual_portal_process_message(virtual_portal, buf + 2, tx_data + 2);
  57. if (send_len > 0) {
  58. tx_data[0] = 0x0b;
  59. tx_data[1] = 0x14;
  60. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  61. timeout = TIMEOUT_AFTER_RESPONSE;
  62. last = now;
  63. if (virtual_portal->speaker) {
  64. timeout = TIMEOUT_AFTER_MUSIC;
  65. }
  66. }
  67. } else if (len_data > 0 && buf[0] == 0x0b && buf[1] == 0x17) {
  68. // 360 audio packets start with 0b 17, samples start after the two byte header
  69. /*
  70. FURI_LOG_RAW_I("pof_usb_receive: ");
  71. for(uint32_t i = 2; i < len_data; i++) {
  72. FURI_LOG_RAW_I("%02x", buf[i]);
  73. }
  74. FURI_LOG_RAW_I("\r\n");
  75. */
  76. virtual_portal_process_audio_360(virtual_portal, buf + 2, len_data - 2);
  77. }
  78. // Check next status time since the timeout based one might be starved by incoming packets.
  79. if (now > last + timeout) {
  80. memset(tx_data, 0, sizeof(tx_data));
  81. len_data = virtual_portal_send_status(virtual_portal, tx_data + 2);
  82. if (len_data > 0) {
  83. tx_data[0] = 0x0b;
  84. tx_data[1] = 0x14;
  85. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  86. }
  87. last = now;
  88. timeout = TIMEOUT_NORMAL;
  89. if (virtual_portal->speaker) {
  90. timeout = TIMEOUT_AFTER_MUSIC;
  91. }
  92. }
  93. flags &= ~EventRx; // clear flag
  94. }
  95. if (flags) {
  96. if (flags & EventResetSio) {
  97. }
  98. if (flags & EventTxComplete) {
  99. pof_usb->tx_complete = true;
  100. }
  101. if (flags & EventTxImmediate) {
  102. pof_usb->tx_immediate = true;
  103. if (pof_usb->tx_complete) {
  104. flags |= EventTx;
  105. }
  106. }
  107. if (flags & EventTx) {
  108. pof_usb->tx_complete = false;
  109. pof_usb->tx_immediate = false;
  110. }
  111. if (flags & EventExit) {
  112. FURI_LOG_I(TAG, "exit");
  113. break;
  114. }
  115. }
  116. if (flags == (uint32_t)FuriFlagErrorISR) { // timeout
  117. memset(tx_data, 0, sizeof(tx_data));
  118. len_data = virtual_portal_send_status(virtual_portal, tx_data + 2);
  119. if (len_data > 0) {
  120. tx_data[0] = 0x0b;
  121. tx_data[1] = 0x14;
  122. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  123. }
  124. last = now;
  125. timeout = TIMEOUT_NORMAL;
  126. if (virtual_portal->speaker) {
  127. timeout = TIMEOUT_AFTER_MUSIC;
  128. }
  129. }
  130. }
  131. return 0;
  132. }
  133. static void pof_usb_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
  134. UNUSED(intf);
  135. PoFUsb* pof_usb = ctx;
  136. pof_cur = pof_usb;
  137. pof_usb->dev = dev;
  138. usbd_reg_config(dev, pof_usb_ep_config);
  139. usbd_reg_control(dev, pof_hid_control);
  140. UNUSED(pof_hid_control);
  141. usbd_connect(dev, true);
  142. pof_usb->thread = furi_thread_alloc();
  143. furi_thread_set_name(pof_usb->thread, "PoFUsb");
  144. furi_thread_set_stack_size(pof_usb->thread, 2 * 1024);
  145. furi_thread_set_context(pof_usb->thread, ctx);
  146. furi_thread_set_callback(pof_usb->thread, pof_thread_worker);
  147. furi_thread_start(pof_usb->thread);
  148. }
  149. static void pof_usb_deinit(usbd_device* dev) {
  150. usbd_reg_config(dev, NULL);
  151. usbd_reg_control(dev, NULL);
  152. PoFUsb* pof_usb = pof_cur;
  153. if (!pof_usb || pof_usb->dev != dev) {
  154. return;
  155. }
  156. pof_cur = NULL;
  157. furi_assert(pof_usb->thread);
  158. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventExit);
  159. furi_thread_join(pof_usb->thread);
  160. furi_thread_free(pof_usb->thread);
  161. pof_usb->thread = NULL;
  162. free(pof_usb->usb.str_prod_descr);
  163. pof_usb->usb.str_prod_descr = NULL;
  164. free(pof_usb->usb.str_serial_descr);
  165. pof_usb->usb.str_serial_descr = NULL;
  166. free(pof_usb);
  167. }
  168. static void pof_usb_send(usbd_device* dev, uint8_t* buf, uint16_t len) {
  169. // Hide frequent responses
  170. /*
  171. if(buf[0] != 'S' && buf[0] != 'J') {
  172. FURI_LOG_RAW_D("> ");
  173. for(size_t i = 0; i < len; i++) {
  174. FURI_LOG_RAW_D("%02x", buf[i]);
  175. }
  176. FURI_LOG_RAW_D("\r\n");
  177. }
  178. */
  179. usbd_ep_write(dev, POF_USB_EP_IN, buf, len);
  180. }
  181. static int32_t pof_usb_receive(usbd_device* dev, uint8_t* buf, uint16_t max_len) {
  182. int32_t len = usbd_ep_read(dev, POF_USB_EP_OUT, buf, max_len);
  183. return ((len < 0) ? 0 : len);
  184. }
  185. static void pof_usb_wakeup(usbd_device* dev) {
  186. UNUSED(dev);
  187. }
  188. static void pof_usb_suspend(usbd_device* dev) {
  189. PoFUsb* pof_usb = pof_cur;
  190. if (!pof_usb || pof_usb->dev != dev) return;
  191. }
  192. static void pof_usb_rx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  193. UNUSED(dev);
  194. UNUSED(event);
  195. UNUSED(ep);
  196. PoFUsb* pof_usb = pof_cur;
  197. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventRx);
  198. }
  199. static void pof_usb_tx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  200. UNUSED(dev);
  201. UNUSED(event);
  202. UNUSED(ep);
  203. PoFUsb* pof_usb = pof_cur;
  204. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventTxComplete);
  205. }
  206. static usbd_respond pof_usb_ep_config(usbd_device* dev, uint8_t cfg) {
  207. switch (cfg) {
  208. case 0: // deconfig
  209. usbd_ep_deconfig(dev, POF_USB_EP_OUT);
  210. usbd_ep_deconfig(dev, POF_USB_EP_IN);
  211. usbd_reg_endpoint(dev, POF_USB_EP_OUT, NULL);
  212. usbd_reg_endpoint(dev, POF_USB_EP_IN, NULL);
  213. usbd_reg_endpoint(dev, POF_USB_X360_AUDIO_EP_IN1, NULL);
  214. usbd_reg_endpoint(dev, POF_USB_X360_AUDIO_EP_IN2, NULL);
  215. usbd_reg_endpoint(dev, POF_USB_X360_AUDIO_EP_OUT1, NULL);
  216. usbd_reg_endpoint(dev, POF_USB_X360_AUDIO_EP_OUT2, NULL);
  217. usbd_reg_endpoint(dev, POF_USB_X360_PLUGIN_MODULE_EP_IN, NULL);
  218. usbd_ep_deconfig(dev, POF_USB_X360_AUDIO_EP_IN1);
  219. usbd_ep_deconfig(dev, POF_USB_X360_AUDIO_EP_IN2);
  220. usbd_ep_deconfig(dev, POF_USB_X360_AUDIO_EP_OUT1);
  221. usbd_ep_deconfig(dev, POF_USB_X360_AUDIO_EP_OUT2);
  222. usbd_ep_deconfig(dev, POF_USB_X360_PLUGIN_MODULE_EP_IN);
  223. return usbd_ack;
  224. case 1: // config
  225. usbd_ep_config(dev, POF_USB_EP_IN, USB_EPTYPE_INTERRUPT, POF_USB_EP_IN_SIZE);
  226. usbd_ep_config(dev, POF_USB_EP_OUT, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
  227. usbd_reg_endpoint(dev, POF_USB_EP_IN, pof_usb_tx_ep_callback);
  228. usbd_reg_endpoint(dev, POF_USB_EP_OUT, pof_usb_rx_ep_callback);
  229. usbd_ep_config(dev, POF_USB_X360_AUDIO_EP_IN1, USB_EPTYPE_INTERRUPT, POF_USB_EP_IN_SIZE);
  230. usbd_ep_config(dev, POF_USB_X360_AUDIO_EP_IN2, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
  231. usbd_ep_config(dev, POF_USB_X360_AUDIO_EP_OUT1, USB_EPTYPE_INTERRUPT, POF_USB_EP_IN_SIZE);
  232. usbd_ep_config(dev, POF_USB_X360_AUDIO_EP_OUT2, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
  233. usbd_ep_config(dev, POF_USB_X360_PLUGIN_MODULE_EP_IN, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
  234. return usbd_ack;
  235. }
  236. return usbd_fail;
  237. }
  238. struct usb_xbox_intf_descriptor {
  239. uint8_t bLength;
  240. uint8_t bDescriptorType;
  241. uint8_t reserved[2];
  242. uint8_t subtype;
  243. uint8_t reserved2;
  244. uint8_t bEndpointAddressIn;
  245. uint8_t bMaxDataSizeIn;
  246. uint8_t reserved3[5];
  247. uint8_t bEndpointAddressOut;
  248. uint8_t bMaxDataSizeOut;
  249. uint8_t reserved4[2];
  250. } __attribute__((packed));
  251. struct PoFUsbDescriptorXbox360 {
  252. struct usb_config_descriptor config;
  253. struct usb_interface_descriptor intf;
  254. struct usb_xbox_intf_descriptor xbox_desc;
  255. struct usb_endpoint_descriptor ep_in;
  256. struct usb_endpoint_descriptor ep_out;
  257. struct usb_interface_descriptor intfAudio;
  258. uint8_t audio_desc[0x1B];
  259. struct usb_endpoint_descriptor ep_in_audio1;
  260. struct usb_endpoint_descriptor ep_out_audio1;
  261. struct usb_endpoint_descriptor ep_in_audio2;
  262. struct usb_endpoint_descriptor ep_out_audio2;
  263. struct usb_interface_descriptor intfPluginModule;
  264. uint8_t plugin_module_desc[0x09];
  265. struct usb_endpoint_descriptor ep_in_plugin_module;
  266. struct usb_interface_descriptor intfSecurity;
  267. uint8_t security_desc[0x06];
  268. } __attribute__((packed));
  269. struct XInputVibrationCapabilities_t {
  270. uint8_t rid;
  271. uint8_t rsize;
  272. uint8_t padding;
  273. uint8_t left_motor;
  274. uint8_t right_motor;
  275. uint8_t padding_2[3];
  276. } __attribute__((packed));
  277. struct XInputInputCapabilities_t {
  278. uint8_t rid;
  279. uint8_t rsize;
  280. uint16_t buttons;
  281. uint8_t leftTrigger;
  282. uint8_t rightTrigger;
  283. uint16_t leftThumbX;
  284. uint16_t leftThumbY;
  285. uint16_t rightThumbX;
  286. uint16_t rightThumbY;
  287. uint8_t reserved[4];
  288. uint16_t flags;
  289. } __attribute__((packed));
  290. static const struct usb_device_descriptor usb_pof_dev_descr_xbox_360 = {
  291. .bLength = sizeof(struct usb_device_descriptor),
  292. .bDescriptorType = USB_DTYPE_DEVICE,
  293. .bcdUSB = VERSION_BCD(2, 0, 0),
  294. .bDeviceClass = 0xFF,
  295. .bDeviceSubClass = 0xFF,
  296. .bDeviceProtocol = 0xFF,
  297. .bMaxPacketSize0 = USB_EP0_SIZE,
  298. .idVendor = POF_USB_VID,
  299. .idProduct = POF_USB_PID,
  300. .bcdDevice = VERSION_BCD(1, 0, 0),
  301. .iManufacturer = 1, // UsbDevManuf
  302. .iProduct = 2, // UsbDevProduct
  303. .iSerialNumber = 0,
  304. .bNumConfigurations = 1,
  305. };
  306. static const uint8_t xbox_serial[] = {0x12, 0x14, 0x32, 0xEF};
  307. static const struct XInputVibrationCapabilities_t XInputVibrationCapabilities = {
  308. rid : 0x00,
  309. rsize : sizeof(struct XInputVibrationCapabilities_t),
  310. padding : 0x00,
  311. left_motor : 0x00,
  312. right_motor : 0x00,
  313. padding_2 : {0x00, 0x00, 0x00}
  314. };
  315. static const struct XInputInputCapabilities_t XInputInputCapabilities = {
  316. rid : 0x00,
  317. rsize : sizeof(struct XInputInputCapabilities_t),
  318. buttons : 0x0000,
  319. leftTrigger : 0x00,
  320. rightTrigger : 0x00,
  321. leftThumbX : 0x0000,
  322. leftThumbY : 0x0000,
  323. rightThumbX : 0x0000,
  324. rightThumbY : 0x0000,
  325. reserved : {0x00, 0x00, 0x00, 0x00},
  326. flags : 0x00
  327. };
  328. static const struct PoFUsbDescriptorXbox360 usb_pof_cfg_descr_x360 = {
  329. .config =
  330. {
  331. .bLength = sizeof(struct usb_config_descriptor),
  332. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  333. .wTotalLength = sizeof(struct PoFUsbDescriptorXbox360),
  334. .bNumInterfaces = 4,
  335. .bConfigurationValue = 1,
  336. .iConfiguration = NO_DESCRIPTOR,
  337. .bmAttributes = USB_CFG_ATTR_RESERVED,
  338. .bMaxPower = USB_CFG_POWER_MA(500),
  339. },
  340. .intf =
  341. {
  342. .bLength = sizeof(struct usb_interface_descriptor),
  343. .bDescriptorType = USB_DTYPE_INTERFACE,
  344. .bInterfaceNumber = 0,
  345. .bAlternateSetting = 0,
  346. .bNumEndpoints = 2,
  347. .bInterfaceClass = 0xFF,
  348. .bInterfaceSubClass = 0x5D,
  349. .bInterfaceProtocol = 0x01,
  350. .iInterface = NO_DESCRIPTOR,
  351. },
  352. .xbox_desc =
  353. {
  354. .bLength = sizeof(struct usb_xbox_intf_descriptor),
  355. .bDescriptorType = 0x21,
  356. .reserved = {0x10, 0x01},
  357. .subtype = 0x24,
  358. .reserved2 = 0x25,
  359. .bEndpointAddressIn = POF_USB_EP_IN,
  360. .bMaxDataSizeIn = 0x14,
  361. .reserved3 = {0x03, 0x03, 0x03, 0x04, 0x13},
  362. .bEndpointAddressOut = POF_USB_EP_OUT,
  363. .bMaxDataSizeOut = 0x08,
  364. .reserved4 = {0x03, 0x03},
  365. },
  366. .ep_in =
  367. {
  368. .bLength = sizeof(struct usb_endpoint_descriptor),
  369. .bDescriptorType = USB_DTYPE_ENDPOINT,
  370. .bEndpointAddress = POF_USB_EP_IN,
  371. .bmAttributes = USB_EPTYPE_INTERRUPT,
  372. .wMaxPacketSize = 0x20,
  373. .bInterval = HID_INTERVAL,
  374. },
  375. .ep_out =
  376. {
  377. .bLength = sizeof(struct usb_endpoint_descriptor),
  378. .bDescriptorType = USB_DTYPE_ENDPOINT,
  379. .bEndpointAddress = POF_USB_EP_OUT,
  380. .bmAttributes = USB_EPTYPE_INTERRUPT,
  381. .wMaxPacketSize = 0x40,
  382. .bInterval = HID_INTERVAL,
  383. },
  384. .intfAudio =
  385. {
  386. .bLength = sizeof(struct usb_interface_descriptor),
  387. .bDescriptorType = USB_DTYPE_INTERFACE,
  388. .bInterfaceNumber = 1,
  389. .bAlternateSetting = 0,
  390. .bNumEndpoints = 4,
  391. .bInterfaceClass = 0xFF,
  392. .bInterfaceSubClass = 0x5D,
  393. .bInterfaceProtocol = 0x03,
  394. .iInterface = NO_DESCRIPTOR,
  395. },
  396. .audio_desc =
  397. {0x1B, 0x21, 0x00, 0x01, 0x01, 0x01, POF_USB_X360_AUDIO_EP_IN1, 0x40, 0x01, POF_USB_X360_AUDIO_EP_OUT1,
  398. 0x20, 0x16, POF_USB_X360_AUDIO_EP_IN2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16,
  399. POF_USB_X360_AUDIO_EP_OUT2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  400. .ep_in_audio1 =
  401. {
  402. .bLength = sizeof(struct usb_endpoint_descriptor),
  403. .bDescriptorType = USB_DTYPE_ENDPOINT,
  404. .bEndpointAddress = POF_USB_X360_AUDIO_EP_IN1,
  405. .bmAttributes = USB_EPTYPE_INTERRUPT,
  406. .wMaxPacketSize = 0x20,
  407. .bInterval = 2,
  408. },
  409. .ep_out_audio1 =
  410. {
  411. .bLength = sizeof(struct usb_endpoint_descriptor),
  412. .bDescriptorType = USB_DTYPE_ENDPOINT,
  413. .bEndpointAddress = POF_USB_X360_AUDIO_EP_OUT1,
  414. .bmAttributes = USB_EPTYPE_INTERRUPT,
  415. .wMaxPacketSize = 0x20,
  416. .bInterval = 4,
  417. },
  418. .ep_in_audio2 =
  419. {
  420. .bLength = sizeof(struct usb_endpoint_descriptor),
  421. .bDescriptorType = USB_DTYPE_ENDPOINT,
  422. .bEndpointAddress = POF_USB_X360_AUDIO_EP_IN2,
  423. .bmAttributes = USB_EPTYPE_INTERRUPT,
  424. .wMaxPacketSize = 0x20,
  425. .bInterval = 0x40,
  426. },
  427. .ep_out_audio2 =
  428. {
  429. .bLength = sizeof(struct usb_endpoint_descriptor),
  430. .bDescriptorType = USB_DTYPE_ENDPOINT,
  431. .bEndpointAddress = POF_USB_X360_AUDIO_EP_OUT2,
  432. .bmAttributes = USB_EPTYPE_INTERRUPT,
  433. .wMaxPacketSize = 0x20,
  434. .bInterval = 0x10,
  435. },
  436. .intfPluginModule =
  437. {
  438. .bLength = sizeof(struct usb_interface_descriptor),
  439. .bDescriptorType = USB_DTYPE_INTERFACE,
  440. .bInterfaceNumber = 2,
  441. .bAlternateSetting = 0,
  442. .bNumEndpoints = 1,
  443. .bInterfaceClass = 0xFF,
  444. .bInterfaceSubClass = 0x5D,
  445. .bInterfaceProtocol = 0x02,
  446. .iInterface = NO_DESCRIPTOR,
  447. },
  448. .plugin_module_desc =
  449. {0x09, 0x21, 0x00, 0x01, 0x01, 0x22, POF_USB_X360_PLUGIN_MODULE_EP_IN, 0x07, 0x00},
  450. .ep_in_plugin_module =
  451. {
  452. .bLength = sizeof(struct usb_endpoint_descriptor),
  453. .bDescriptorType = USB_DTYPE_ENDPOINT,
  454. .bEndpointAddress = POF_USB_X360_PLUGIN_MODULE_EP_IN,
  455. .bmAttributes = USB_EPTYPE_INTERRUPT,
  456. .wMaxPacketSize = 0x20,
  457. .bInterval = 16,
  458. },
  459. .intfSecurity =
  460. {
  461. .bLength = sizeof(struct usb_interface_descriptor),
  462. .bDescriptorType = USB_DTYPE_INTERFACE,
  463. .bInterfaceNumber = 3,
  464. .bAlternateSetting = 0,
  465. .bNumEndpoints = 0,
  466. .bInterfaceClass = 0xFF,
  467. .bInterfaceSubClass = 0xFD,
  468. .bInterfaceProtocol = 0x13,
  469. .iInterface = 4,
  470. },
  471. .security_desc =
  472. {0x06, 0x41, 0x00, 0x01, 0x01, 0x03},
  473. };
  474. uint8_t serial[0x0C];
  475. short state = 2; // 1 = in-progress, 2 = complete
  476. /* Control requests handler */
  477. static usbd_respond
  478. pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  479. UNUSED(callback);
  480. uint8_t wValueH = req->wValue >> 8;
  481. uint8_t wValueL = req->wValue & 0xFF;
  482. if (req->bmRequestType == 0xC0 && req->bRequest == USB_HID_GETREPORT && req->wValue == 0x0000) {
  483. dev->status.data_ptr = (uint8_t*)xbox_serial;
  484. dev->status.data_count = sizeof(xbox_serial);
  485. return usbd_ack;
  486. }
  487. if (req->bmRequestType == 0xC1 && req->bRequest == USB_HID_GETREPORT && req->wValue == 0x0100) {
  488. dev->status.data_ptr = (uint8_t*)&(XInputInputCapabilities);
  489. dev->status.data_count = sizeof(XInputInputCapabilities);
  490. return usbd_ack;
  491. }
  492. if (req->bmRequestType == 0xC1 && req->bRequest == USB_HID_GETREPORT && req->wValue == 0x0000) {
  493. dev->status.data_ptr = (uint8_t*)&(XInputVibrationCapabilities);
  494. dev->status.data_count = sizeof(XInputVibrationCapabilities);
  495. return usbd_ack;
  496. }
  497. if (req->bmRequestType == 0x41 && req->bRequest == 00 && (req->wValue == 0x1F || req->wValue == 0x1E)) {
  498. return usbd_ack;
  499. }
  500. switch (req->bRequest) {
  501. case 0x81:
  502. uint8_t serial[0x0C];
  503. for (size_t i = 0; i < sizeof(serial); i++) {
  504. serial[i] = furi_hal_random_get() & 0xFF;
  505. }
  506. xsm3_set_vid_pid(serial, POF_USB_VID, POF_USB_PID);
  507. xsm3_initialise_state();
  508. xsm3_set_identification_data(xsm3_id_data_ms_controller);
  509. dev->status.data_ptr = (uint8_t*)(xsm3_id_data_ms_controller);
  510. dev->status.data_count = sizeof(xsm3_id_data_ms_controller);
  511. return usbd_ack;
  512. case 0x82:
  513. xsm3_do_challenge_init((uint8_t *)dev->status.data_ptr);
  514. return usbd_ack;
  515. case 0x87:
  516. xsm3_do_challenge_verify((uint8_t *)dev->status.data_ptr);
  517. return usbd_ack;
  518. case 0x84:
  519. return usbd_ack;
  520. case 0x83:
  521. dev->status.data_ptr = (uint8_t*)(xsm3_challenge_response);
  522. dev->status.data_count = req->wLength;
  523. return usbd_ack;
  524. case 0x86:
  525. dev->status.data_ptr = (uint8_t*)&(state);
  526. dev->status.data_count = sizeof(state);
  527. return usbd_ack;
  528. }
  529. if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  530. (USB_REQ_DEVICE | USB_REQ_STANDARD) &&
  531. req->bRequest == USB_STD_GET_DESCRIPTOR) {
  532. switch (wValueH) {
  533. case USB_DTYPE_STRING:
  534. if (wValueL == 4) {
  535. dev->status.data_ptr = (uint8_t*)&dev_security_desc;
  536. dev->status.data_count = dev_security_desc.bLength;
  537. return usbd_ack;
  538. }
  539. return usbd_fail;
  540. default:
  541. return usbd_fail;
  542. }
  543. }
  544. return usbd_fail;
  545. }
  546. PoFUsb* pof_usb_start_xbox360(VirtualPortal* virtual_portal) {
  547. PoFUsb* pof_usb = malloc(sizeof(PoFUsb));
  548. pof_usb->virtual_portal = virtual_portal;
  549. pof_usb->dataAvailable = 0;
  550. furi_hal_usb_unlock();
  551. pof_usb->usb_prev = furi_hal_usb_get_config();
  552. pof_usb->usb.init = pof_usb_init;
  553. pof_usb->usb.deinit = pof_usb_deinit;
  554. pof_usb->usb.wakeup = pof_usb_wakeup;
  555. pof_usb->usb.suspend = pof_usb_suspend;
  556. pof_usb->usb.dev_descr = (struct usb_device_descriptor*)&usb_pof_dev_descr_xbox_360;
  557. pof_usb->usb.cfg_descr = (void*)&usb_pof_cfg_descr_x360;
  558. pof_usb->usb.str_manuf_descr = (void*)&dev_manuf_desc;
  559. pof_usb->usb.str_prod_descr = (void*)&dev_product_desc;
  560. pof_usb->usb.str_serial_descr = NULL;
  561. if (!furi_hal_usb_set_config(&pof_usb->usb, pof_usb)) {
  562. FURI_LOG_E(TAG, "USB locked, can not start");
  563. if (pof_usb->usb.str_manuf_descr) {
  564. free(pof_usb->usb.str_manuf_descr);
  565. }
  566. if (pof_usb->usb.str_prod_descr) {
  567. free(pof_usb->usb.str_prod_descr);
  568. }
  569. if (pof_usb->usb.str_serial_descr) {
  570. free(pof_usb->usb.str_serial_descr);
  571. }
  572. free(pof_usb);
  573. pof_usb = NULL;
  574. return NULL;
  575. }
  576. return pof_usb;
  577. }
  578. void pof_usb_stop_xbox360(PoFUsb* pof_usb) {
  579. if (pof_usb) {
  580. furi_hal_usb_set_config(pof_usb->usb_prev, NULL);
  581. }
  582. }