pof_usb_xbox360.c 21 KB

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