pof_usb_xbox360.c 21 KB

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