pof_usb_xbox360.c 21 KB

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