pof_usb_xbox360.c 21 KB

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