pof_usb_xbox360.c 21 KB

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