pof_usb_xbox360.c 22 KB

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