pof_usb_xbox360.c 21 KB

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