pof_usb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include "pof_usb.h"
  2. #define TAG "POF USB"
  3. #define HID_INTERVAL 1
  4. #define USB_EP0_SIZE 8
  5. #define POF_USB_VID (0x1430)
  6. #define POF_USB_PID (0x0150)
  7. #define POF_USB_PID_X360 (0x1F17)
  8. #define POF_USB_EP_IN (0x81)
  9. #define POF_USB_EP_OUT (0x02)
  10. #define POF_USB_X360_AUDIO_EP_IN1 (0x83)
  11. #define POF_USB_X360_AUDIO_EP_OUT1 (0x04)
  12. #define POF_USB_X360_AUDIO_EP_IN2 (0x85)
  13. #define POF_USB_X360_AUDIO_EP_OUT2 (0x06)
  14. #define POF_USB_X360_PLUGIN_MODULE_EP_IN (0x87)
  15. #define POF_USB_EP_IN_SIZE (64UL)
  16. #define POF_USB_EP_OUT_SIZE (64UL)
  17. #define POF_USB_RX_MAX_SIZE (POF_USB_EP_OUT_SIZE)
  18. #define POF_USB_TX_MAX_SIZE (POF_USB_EP_IN_SIZE)
  19. #define POF_USB_ACTUAL_OUTPUT_SIZE 0x20
  20. static const struct usb_string_descriptor dev_manuf_desc =
  21. USB_ARRAY_DESC(0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x00);
  22. static const struct usb_string_descriptor dev_product_desc =
  23. USB_ARRAY_DESC(0x53, 0x70, 0x79, 0x72, 0x6f, 0x20, 0x50, 0x6f, 0x72, 0x74, 0x61, 0x00);
  24. static const struct usb_string_descriptor dev_security_desc =
  25. USB_STRING_DESC("Xbox Security Method 3, Version 1.00, © 2005 Microsoft Corporation. All rights reserved.");
  26. static const uint8_t hid_report_desc[] = {0x06, 0x00, 0xFF, 0x09, 0x01, 0xA1, 0x01, 0x19,
  27. 0x01, 0x29, 0x40, 0x15, 0x00, 0x26, 0xFF, 0x00,
  28. 0x75, 0x08, 0x95, 0x20, 0x81, 0x00, 0x19, 0x01,
  29. 0x29, 0x40, 0x91, 0x00, 0xC0};
  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 usbd_respond
  34. pof_xbox_360_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. UNUSED(pof_usb_receive);
  77. if(virtual_portal->speaker) {
  78. uint8_t buf[POF_USB_RX_MAX_SIZE];
  79. len_data = pof_usb_receive(dev, buf, POF_USB_RX_MAX_SIZE);
  80. // https://github.com/xMasterX/all-the-plugins/blob/dev/base_pack/wav_player/wav_player_hal.c
  81. if(len_data > 0) {
  82. /*
  83. FURI_LOG_RAW_I("pof_usb_receive: ");
  84. for(uint32_t i = 0; i < len_data; i++) {
  85. FURI_LOG_RAW_I("%02x", buf[i]);
  86. }
  87. FURI_LOG_RAW_I("\r\n");
  88. */
  89. }
  90. }
  91. if(pof_usb->dataAvailable > 0) {
  92. memset(tx_data, 0, sizeof(tx_data));
  93. int send_len =
  94. virtual_portal_process_message(virtual_portal, pof_usb->data + 2, tx_data + 2);
  95. if(send_len > 0) {
  96. tx_data[0] = 0x1b;
  97. tx_data[1] = send_len;
  98. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  99. }
  100. pof_usb->dataAvailable = 0;
  101. }
  102. // Check next status time since the timeout based one might be starved by incoming packets.
  103. if(now > lastStatus + timeout) {
  104. lastStatus = now;
  105. memset(tx_data, 0, sizeof(tx_data));
  106. len_data = virtual_portal_send_status(virtual_portal, tx_data);
  107. if(len_data > 0) {
  108. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  109. }
  110. }
  111. flags &= ~EventRx; // clear flag
  112. }
  113. if(flags) {
  114. if(flags & EventResetSio) {
  115. }
  116. if(flags & EventTxComplete) {
  117. pof_usb->tx_complete = true;
  118. }
  119. if(flags & EventTxImmediate) {
  120. pof_usb->tx_immediate = true;
  121. if(pof_usb->tx_complete) {
  122. flags |= EventTx;
  123. }
  124. }
  125. if(flags & EventTx) {
  126. pof_usb->tx_complete = false;
  127. pof_usb->tx_immediate = false;
  128. }
  129. if(flags & EventExit) {
  130. FURI_LOG_I(TAG, "exit");
  131. break;
  132. }
  133. }
  134. if(flags == (uint32_t)FuriFlagErrorISR) { // timeout
  135. memset(tx_data, 0, sizeof(tx_data));
  136. len_data = virtual_portal_send_status(virtual_portal, tx_data);
  137. if(len_data > 0) {
  138. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  139. }
  140. lastStatus = now;
  141. }
  142. }
  143. return 0;
  144. }
  145. static void pof_usb_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
  146. UNUSED(intf);
  147. PoFUsb* pof_usb = ctx;
  148. pof_cur = pof_usb;
  149. pof_usb->dev = dev;
  150. usbd_reg_config(dev, pof_usb_ep_config);
  151. usbd_reg_control(dev, pof_hid_control);
  152. UNUSED(pof_hid_control);
  153. usbd_connect(dev, true);
  154. pof_usb->thread = furi_thread_alloc();
  155. furi_thread_set_name(pof_usb->thread, "PoFUsb");
  156. furi_thread_set_stack_size(pof_usb->thread, 2 * 1024);
  157. furi_thread_set_context(pof_usb->thread, ctx);
  158. furi_thread_set_callback(pof_usb->thread, pof_thread_worker);
  159. furi_thread_start(pof_usb->thread);
  160. }
  161. static void pof_usb_init_xbox_360(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
  162. UNUSED(intf);
  163. PoFUsb* pof_usb = ctx;
  164. pof_cur = pof_usb;
  165. pof_usb->dev = dev;
  166. usbd_reg_config(dev, pof_usb_ep_config);
  167. usbd_reg_control(dev, pof_xbox_360_control);
  168. UNUSED(pof_xbox_360_control);
  169. usbd_connect(dev, true);
  170. pof_usb->thread = furi_thread_alloc();
  171. furi_thread_set_name(pof_usb->thread, "PoFUsb");
  172. furi_thread_set_stack_size(pof_usb->thread, 2 * 1024);
  173. furi_thread_set_context(pof_usb->thread, ctx);
  174. furi_thread_set_callback(pof_usb->thread, pof_thread_worker);
  175. furi_thread_start(pof_usb->thread);
  176. }
  177. static void pof_usb_deinit(usbd_device* dev) {
  178. usbd_reg_config(dev, NULL);
  179. usbd_reg_control(dev, NULL);
  180. PoFUsb* pof_usb = pof_cur;
  181. if(!pof_usb || pof_usb->dev != dev) {
  182. return;
  183. }
  184. pof_cur = NULL;
  185. furi_assert(pof_usb->thread);
  186. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventExit);
  187. furi_thread_join(pof_usb->thread);
  188. furi_thread_free(pof_usb->thread);
  189. pof_usb->thread = NULL;
  190. free(pof_usb->usb.str_prod_descr);
  191. pof_usb->usb.str_prod_descr = NULL;
  192. free(pof_usb->usb.str_serial_descr);
  193. pof_usb->usb.str_serial_descr = NULL;
  194. free(pof_usb);
  195. }
  196. static void pof_usb_send(usbd_device* dev, uint8_t* buf, uint16_t len) {
  197. // Hide frequent responses
  198. /*
  199. if(buf[0] != 'S' && buf[0] != 'J') {
  200. FURI_LOG_RAW_D("> ");
  201. for(size_t i = 0; i < len; i++) {
  202. FURI_LOG_RAW_D("%02x", buf[i]);
  203. }
  204. FURI_LOG_RAW_D("\r\n");
  205. }
  206. */
  207. usbd_ep_write(dev, POF_USB_EP_IN, buf, len);
  208. }
  209. static int32_t pof_usb_receive(usbd_device* dev, uint8_t* buf, uint16_t max_len) {
  210. int32_t len = usbd_ep_read(dev, POF_USB_EP_OUT, buf, max_len);
  211. return ((len < 0) ? 0 : len);
  212. }
  213. static void pof_usb_wakeup(usbd_device* dev) {
  214. UNUSED(dev);
  215. }
  216. static void pof_usb_suspend(usbd_device* dev) {
  217. PoFUsb* pof_usb = pof_cur;
  218. if(!pof_usb || pof_usb->dev != dev) return;
  219. }
  220. static void pof_usb_rx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  221. UNUSED(dev);
  222. UNUSED(event);
  223. UNUSED(ep);
  224. PoFUsb* pof_usb = pof_cur;
  225. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventRx);
  226. }
  227. static void pof_usb_tx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  228. UNUSED(dev);
  229. UNUSED(event);
  230. UNUSED(ep);
  231. PoFUsb* pof_usb = pof_cur;
  232. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventTxComplete);
  233. }
  234. static usbd_respond pof_usb_ep_config(usbd_device* dev, uint8_t cfg) {
  235. switch(cfg) {
  236. case 0: // deconfig
  237. usbd_ep_deconfig(dev, POF_USB_EP_OUT);
  238. usbd_ep_deconfig(dev, POF_USB_EP_IN);
  239. usbd_reg_endpoint(dev, POF_USB_EP_OUT, NULL);
  240. usbd_reg_endpoint(dev, POF_USB_EP_IN, NULL);
  241. return usbd_ack;
  242. case 1: // config
  243. usbd_ep_config(dev, POF_USB_EP_IN, USB_EPTYPE_INTERRUPT, POF_USB_EP_IN_SIZE);
  244. usbd_ep_config(dev, POF_USB_EP_OUT, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
  245. usbd_reg_endpoint(dev, POF_USB_EP_IN, pof_usb_tx_ep_callback);
  246. usbd_reg_endpoint(dev, POF_USB_EP_OUT, pof_usb_rx_ep_callback);
  247. return usbd_ack;
  248. }
  249. return usbd_fail;
  250. }
  251. struct PoFUsbDescriptor {
  252. struct usb_config_descriptor config;
  253. struct usb_interface_descriptor intf;
  254. struct usb_hid_descriptor hid_desc;
  255. struct usb_endpoint_descriptor ep_in;
  256. struct usb_endpoint_descriptor ep_out;
  257. } __attribute__((packed));
  258. struct usb_xbox_intf_descriptor {
  259. uint8_t bLength;
  260. uint8_t bDescriptorType;
  261. uint8_t reserved[2];
  262. uint8_t subtype;
  263. uint8_t reserved2;
  264. uint8_t bEndpointAddressIn;
  265. uint8_t bMaxDataSizeIn;
  266. uint8_t reserved3[5];
  267. uint8_t bEndpointAddressOut;
  268. uint8_t bMaxDataSizeOut;
  269. uint8_t reserved4[2];
  270. } __attribute__((packed));
  271. struct PoFUsbDescriptorXbox360 {
  272. struct usb_config_descriptor config;
  273. struct usb_interface_descriptor intf;
  274. struct usb_xbox_intf_descriptor xbox_desc;
  275. struct usb_endpoint_descriptor ep_in;
  276. struct usb_endpoint_descriptor ep_out;
  277. struct usb_interface_descriptor intfAudio;
  278. uint8_t audio_desc[0x1B];
  279. struct usb_endpoint_descriptor ep_in_audio1;
  280. struct usb_endpoint_descriptor ep_out_audio1;
  281. struct usb_endpoint_descriptor ep_in_audio2;
  282. struct usb_endpoint_descriptor ep_out_audio2;
  283. struct usb_interface_descriptor intfPluginModule;
  284. uint8_t plugin_module_desc[0x09];
  285. struct usb_endpoint_descriptor ep_in_plugin_module;
  286. struct usb_interface_descriptor intfSecurity;
  287. uint8_t security_desc[0x06];
  288. } __attribute__((packed));
  289. static const struct usb_device_descriptor usb_pof_dev_descr = {
  290. .bLength = sizeof(struct usb_device_descriptor),
  291. .bDescriptorType = USB_DTYPE_DEVICE,
  292. .bcdUSB = VERSION_BCD(2, 0, 0),
  293. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  294. .bDeviceSubClass = USB_SUBCLASS_NONE,
  295. .bDeviceProtocol = USB_PROTO_NONE,
  296. .bMaxPacketSize0 = USB_EP0_SIZE,
  297. .idVendor = POF_USB_VID,
  298. .idProduct = POF_USB_PID,
  299. .bcdDevice = VERSION_BCD(1, 0, 0),
  300. .iManufacturer = 1, // UsbDevManuf
  301. .iProduct = 2, // UsbDevProduct
  302. .iSerialNumber = 0,
  303. .bNumConfigurations = 1,
  304. };
  305. static const struct usb_device_descriptor usb_pof_dev_descr_xbox_360 = {
  306. .bLength = sizeof(struct usb_device_descriptor),
  307. .bDescriptorType = USB_DTYPE_DEVICE,
  308. .bcdUSB = VERSION_BCD(2, 0, 0),
  309. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  310. .bDeviceSubClass = USB_SUBCLASS_NONE,
  311. .bDeviceProtocol = USB_PROTO_NONE,
  312. .bMaxPacketSize0 = USB_EP0_SIZE,
  313. .idVendor = POF_USB_VID,
  314. .idProduct = POF_USB_PID_X360,
  315. .bcdDevice = VERSION_BCD(1, 0, 0),
  316. .iManufacturer = 1, // UsbDevManuf
  317. .iProduct = 2, // UsbDevProduct
  318. .iSerialNumber = 0,
  319. .bNumConfigurations = 1,
  320. };
  321. static const struct PoFUsbDescriptor usb_pof_cfg_descr = {
  322. .config =
  323. {
  324. .bLength = sizeof(struct usb_config_descriptor),
  325. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  326. .wTotalLength = sizeof(struct PoFUsbDescriptor),
  327. .bNumInterfaces = 1,
  328. .bConfigurationValue = 1,
  329. .iConfiguration = NO_DESCRIPTOR,
  330. .bmAttributes = USB_CFG_ATTR_RESERVED,
  331. .bMaxPower = USB_CFG_POWER_MA(500),
  332. },
  333. .intf =
  334. {
  335. .bLength = sizeof(struct usb_interface_descriptor),
  336. .bDescriptorType = USB_DTYPE_INTERFACE,
  337. .bInterfaceNumber = 0,
  338. .bAlternateSetting = 0,
  339. .bNumEndpoints = 2,
  340. .bInterfaceClass = USB_CLASS_HID,
  341. .bInterfaceSubClass = USB_HID_SUBCLASS_NONBOOT,
  342. .bInterfaceProtocol = USB_HID_PROTO_NONBOOT,
  343. .iInterface = NO_DESCRIPTOR,
  344. },
  345. .hid_desc =
  346. {
  347. .bLength = sizeof(struct usb_hid_descriptor),
  348. .bDescriptorType = USB_DTYPE_HID,
  349. .bcdHID = VERSION_BCD(1, 1, 1),
  350. .bCountryCode = USB_HID_COUNTRY_NONE,
  351. .bNumDescriptors = 1,
  352. .bDescriptorType0 = USB_DTYPE_HID_REPORT,
  353. .wDescriptorLength0 = sizeof(hid_report_desc),
  354. },
  355. .ep_in =
  356. {
  357. .bLength = sizeof(struct usb_endpoint_descriptor),
  358. .bDescriptorType = USB_DTYPE_ENDPOINT,
  359. .bEndpointAddress = POF_USB_EP_IN,
  360. .bmAttributes = USB_EPTYPE_INTERRUPT,
  361. .wMaxPacketSize = 0x40,
  362. .bInterval = HID_INTERVAL,
  363. },
  364. .ep_out =
  365. {
  366. .bLength = sizeof(struct usb_endpoint_descriptor),
  367. .bDescriptorType = USB_DTYPE_ENDPOINT,
  368. .bEndpointAddress = POF_USB_EP_OUT,
  369. .bmAttributes = USB_EPTYPE_INTERRUPT,
  370. .wMaxPacketSize = 0x40,
  371. .bInterval = HID_INTERVAL,
  372. },
  373. };
  374. static const struct PoFUsbDescriptorXbox360 usb_pof_cfg_descr_x360 = {
  375. .config =
  376. {
  377. .bLength = sizeof(struct usb_config_descriptor),
  378. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  379. .wTotalLength = sizeof(struct PoFUsbDescriptorXbox360),
  380. .bNumInterfaces = 4,
  381. .bConfigurationValue = 1,
  382. .iConfiguration = NO_DESCRIPTOR,
  383. .bmAttributes = USB_CFG_ATTR_RESERVED,
  384. .bMaxPower = USB_CFG_POWER_MA(500),
  385. },
  386. .intf =
  387. {
  388. .bLength = sizeof(struct usb_interface_descriptor),
  389. .bDescriptorType = USB_DTYPE_INTERFACE,
  390. .bInterfaceNumber = 0,
  391. .bAlternateSetting = 0,
  392. .bNumEndpoints = 2,
  393. .bInterfaceClass = 0xFF,
  394. .bInterfaceSubClass = 0x5D,
  395. .bInterfaceProtocol = 0x01,
  396. .iInterface = NO_DESCRIPTOR,
  397. },
  398. .xbox_desc =
  399. {
  400. .bLength = sizeof(struct usb_xbox_intf_descriptor),
  401. .bDescriptorType = 0x21,
  402. .reserved = {0x10, 0x01},
  403. .subtype = 0x24,
  404. .reserved2 = 0x25,
  405. .bEndpointAddressIn = POF_USB_EP_IN,
  406. .bMaxDataSizeIn = 0x14,
  407. .reserved3 = {0x03, 0x03, 0x03, 0x04, 0x13},
  408. .bEndpointAddressOut = POF_USB_EP_OUT,
  409. .bMaxDataSizeOut = 0x08,
  410. .reserved4 = {0x03, 0x03},
  411. },
  412. .ep_in =
  413. {
  414. .bLength = sizeof(struct usb_endpoint_descriptor),
  415. .bDescriptorType = USB_DTYPE_ENDPOINT,
  416. .bEndpointAddress = POF_USB_EP_IN,
  417. .bmAttributes = USB_EPTYPE_INTERRUPT,
  418. .wMaxPacketSize = 0x40,
  419. .bInterval = HID_INTERVAL,
  420. },
  421. .ep_out =
  422. {
  423. .bLength = sizeof(struct usb_endpoint_descriptor),
  424. .bDescriptorType = USB_DTYPE_ENDPOINT,
  425. .bEndpointAddress = POF_USB_EP_OUT,
  426. .bmAttributes = USB_EPTYPE_INTERRUPT,
  427. .wMaxPacketSize = 0x40,
  428. .bInterval = HID_INTERVAL,
  429. },
  430. .intfAudio =
  431. {
  432. .bLength = sizeof(struct usb_interface_descriptor),
  433. .bDescriptorType = USB_DTYPE_INTERFACE,
  434. .bInterfaceNumber = 1,
  435. .bAlternateSetting = 0,
  436. .bNumEndpoints = 4,
  437. .bInterfaceClass = 0xFF,
  438. .bInterfaceSubClass = 0x5D,
  439. .bInterfaceProtocol = 0x03,
  440. .iInterface = NO_DESCRIPTOR,
  441. },
  442. .audio_desc =
  443. {0x1B, 0x21, 0x00, 0x01, 0x01, 0x01, POF_USB_X360_AUDIO_EP_IN1, 0x40, 0x01, POF_USB_X360_AUDIO_EP_OUT1,
  444. 0x20, 0x16, POF_USB_X360_AUDIO_EP_IN2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16,
  445. POF_USB_X360_AUDIO_EP_OUT2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  446. .ep_in_audio1 =
  447. {
  448. .bLength = sizeof(struct usb_endpoint_descriptor),
  449. .bDescriptorType = USB_DTYPE_ENDPOINT,
  450. .bEndpointAddress = POF_USB_X360_AUDIO_EP_IN1,
  451. .bmAttributes = USB_EPTYPE_INTERRUPT,
  452. .wMaxPacketSize = 0x20,
  453. .bInterval = 2,
  454. },
  455. .ep_out_audio1 =
  456. {
  457. .bLength = sizeof(struct usb_endpoint_descriptor),
  458. .bDescriptorType = USB_DTYPE_ENDPOINT,
  459. .bEndpointAddress = POF_USB_X360_AUDIO_EP_OUT1,
  460. .bmAttributes = USB_EPTYPE_INTERRUPT,
  461. .wMaxPacketSize = 0x20,
  462. .bInterval = 4,
  463. },
  464. .ep_in_audio2 =
  465. {
  466. .bLength = sizeof(struct usb_endpoint_descriptor),
  467. .bDescriptorType = USB_DTYPE_ENDPOINT,
  468. .bEndpointAddress = POF_USB_X360_AUDIO_EP_IN2,
  469. .bmAttributes = USB_EPTYPE_INTERRUPT,
  470. .wMaxPacketSize = 0x20,
  471. .bInterval = 0x40,
  472. },
  473. .ep_out_audio2 =
  474. {
  475. .bLength = sizeof(struct usb_endpoint_descriptor),
  476. .bDescriptorType = USB_DTYPE_ENDPOINT,
  477. .bEndpointAddress = POF_USB_X360_AUDIO_EP_OUT2,
  478. .bmAttributes = USB_EPTYPE_INTERRUPT,
  479. .wMaxPacketSize = 0x10,
  480. .bInterval = 0x10,
  481. },
  482. .intfPluginModule =
  483. {
  484. .bLength = sizeof(struct usb_interface_descriptor),
  485. .bDescriptorType = USB_DTYPE_INTERFACE,
  486. .bInterfaceNumber = 1,
  487. .bAlternateSetting = 0,
  488. .bNumEndpoints = 1,
  489. .bInterfaceClass = 0xFF,
  490. .bInterfaceSubClass = 0x5D,
  491. .bInterfaceProtocol = 0x02,
  492. .iInterface = NO_DESCRIPTOR,
  493. },
  494. .plugin_module_desc =
  495. {0x09, 0x21, 0x00, 0x01, 0x01, 0x22, POF_USB_X360_PLUGIN_MODULE_EP_IN, 0x07, 0x00},
  496. .ep_in_plugin_module =
  497. {
  498. .bLength = sizeof(struct usb_endpoint_descriptor),
  499. .bDescriptorType = USB_DTYPE_ENDPOINT,
  500. .bEndpointAddress = POF_USB_X360_PLUGIN_MODULE_EP_IN,
  501. .bmAttributes = USB_EPTYPE_INTERRUPT,
  502. .wMaxPacketSize = 0x20,
  503. .bInterval = 16,
  504. },
  505. .intfSecurity =
  506. {
  507. .bLength = sizeof(struct usb_interface_descriptor),
  508. .bDescriptorType = USB_DTYPE_INTERFACE,
  509. .bInterfaceNumber = 2,
  510. .bAlternateSetting = 0,
  511. .bNumEndpoints = 0,
  512. .bInterfaceClass = 0xFF,
  513. .bInterfaceSubClass = 0xFD,
  514. .bInterfaceProtocol = 0x13,
  515. .iInterface = 4,
  516. },
  517. .security_desc =
  518. {0x06, 0x41, 0x00, 0x01, 0x01, 0x03},
  519. };
  520. /* Control requests handler */
  521. static usbd_respond
  522. pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  523. UNUSED(callback);
  524. uint8_t wValueH = req->wValue >> 8;
  525. uint8_t wValueL = req->wValue & 0xFF;
  526. uint16_t length = req->wLength;
  527. PoFUsb* pof_usb = pof_cur;
  528. /* HID control requests */
  529. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  530. (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
  531. req->wIndex == 0) {
  532. switch(req->bRequest) {
  533. case USB_HID_SETIDLE:
  534. return usbd_ack;
  535. case USB_HID_SETPROTOCOL:
  536. return usbd_ack;
  537. case USB_HID_GETREPORT:
  538. dev->status.data_ptr = pof_usb->tx_data;
  539. dev->status.data_count = sizeof(pof_usb->tx_data);
  540. return usbd_ack;
  541. case USB_HID_SETREPORT:
  542. if(wValueH == HID_REPORT_TYPE_INPUT) {
  543. if(length == POF_USB_RX_MAX_SIZE) {
  544. return usbd_ack;
  545. }
  546. } else if(wValueH == HID_REPORT_TYPE_OUTPUT) {
  547. memcpy(pof_usb->data, req->data, req->wLength);
  548. pof_usb->dataAvailable += req->wLength;
  549. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventRx);
  550. return usbd_ack;
  551. } else if(wValueH == HID_REPORT_TYPE_FEATURE) {
  552. return usbd_ack;
  553. }
  554. return usbd_fail;
  555. default:
  556. return usbd_fail;
  557. }
  558. }
  559. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  560. (USB_REQ_INTERFACE | USB_REQ_STANDARD) &&
  561. req->wIndex == 0 && req->bRequest == USB_STD_GET_DESCRIPTOR) {
  562. switch(wValueH) {
  563. case USB_DTYPE_HID:
  564. dev->status.data_ptr = (uint8_t*)&(usb_pof_cfg_descr.hid_desc);
  565. dev->status.data_count = sizeof(usb_pof_cfg_descr.hid_desc);
  566. return usbd_ack;
  567. case USB_DTYPE_HID_REPORT:
  568. dev->status.data_ptr = (uint8_t*)hid_report_desc;
  569. dev->status.data_count = sizeof(hid_report_desc);
  570. return usbd_ack;
  571. case USB_DTYPE_STRING:
  572. if (wValueL == 4) {
  573. dev->status.data_ptr = (uint8_t*)&dev_security_desc;
  574. dev->status.data_count = sizeof(dev_security_desc);
  575. return usbd_ack;
  576. }
  577. return usbd_fail;
  578. default:
  579. return usbd_fail;
  580. }
  581. }
  582. return usbd_fail;
  583. }
  584. static usbd_respond
  585. pof_xbox_360_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  586. UNUSED(callback);
  587. uint8_t wValueH = req->wValue >> 8;
  588. uint8_t wValueL = req->wValue & 0xFF;
  589. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  590. (USB_REQ_INTERFACE | USB_REQ_STANDARD) &&
  591. req->wIndex == 0 && req->bRequest == USB_STD_GET_DESCRIPTOR) {
  592. switch(wValueH) {
  593. case USB_DTYPE_STRING:
  594. if (wValueL == 4) {
  595. dev->status.data_ptr = (uint8_t*)&dev_security_desc;
  596. dev->status.data_count = sizeof(dev_security_desc);
  597. return usbd_ack;
  598. }
  599. return usbd_fail;
  600. default:
  601. return usbd_fail;
  602. }
  603. }
  604. return usbd_fail;
  605. }
  606. PoFUsb* pof_usb_start_xbox_360(VirtualPortal* virtual_portal) {
  607. PoFUsb* pof_usb = malloc(sizeof(PoFUsb));
  608. pof_usb->virtual_portal = virtual_portal;
  609. pof_usb->dataAvailable = 0;
  610. pof_usb->usb_prev = furi_hal_usb_get_config();
  611. pof_usb->usb.init = pof_usb_init_xbox_360;
  612. pof_usb->usb.deinit = pof_usb_deinit;
  613. pof_usb->usb.wakeup = pof_usb_wakeup;
  614. pof_usb->usb.suspend = pof_usb_suspend;
  615. pof_usb->usb.dev_descr = (struct usb_device_descriptor*)&usb_pof_dev_descr_xbox_360;
  616. pof_usb->usb.str_manuf_descr = (void*)&dev_manuf_desc;
  617. pof_usb->usb.str_prod_descr = (void*)&dev_product_desc;
  618. pof_usb->usb.str_serial_descr = NULL;
  619. pof_usb->usb.cfg_descr = (void*)&usb_pof_cfg_descr_x360;
  620. if(!furi_hal_usb_set_config(&pof_usb->usb, pof_usb)) {
  621. FURI_LOG_E(TAG, "USB locked, can not start");
  622. if(pof_usb->usb.str_manuf_descr) {
  623. free(pof_usb->usb.str_manuf_descr);
  624. }
  625. if(pof_usb->usb.str_prod_descr) {
  626. free(pof_usb->usb.str_prod_descr);
  627. }
  628. if(pof_usb->usb.str_serial_descr) {
  629. free(pof_usb->usb.str_serial_descr);
  630. }
  631. free(pof_usb);
  632. return NULL;
  633. }
  634. return pof_usb;
  635. }
  636. PoFUsb* pof_usb_start(VirtualPortal* virtual_portal) {
  637. PoFUsb* pof_usb = malloc(sizeof(PoFUsb));
  638. pof_usb->virtual_portal = virtual_portal;
  639. pof_usb->dataAvailable = 0;
  640. pof_usb->usb_prev = furi_hal_usb_get_config();
  641. pof_usb->usb.init = pof_usb_init;
  642. pof_usb->usb.deinit = pof_usb_deinit;
  643. pof_usb->usb.wakeup = pof_usb_wakeup;
  644. pof_usb->usb.suspend = pof_usb_suspend;
  645. pof_usb->usb.dev_descr = (struct usb_device_descriptor*)&usb_pof_dev_descr;
  646. pof_usb->usb.str_manuf_descr = (void*)&dev_manuf_desc;
  647. pof_usb->usb.str_prod_descr = (void*)&dev_product_desc;
  648. pof_usb->usb.str_serial_descr = NULL;
  649. pof_usb->usb.cfg_descr = (void*)&usb_pof_cfg_descr;
  650. if(!furi_hal_usb_set_config(&pof_usb->usb, pof_usb)) {
  651. FURI_LOG_E(TAG, "USB locked, can not start");
  652. if(pof_usb->usb.str_manuf_descr) {
  653. free(pof_usb->usb.str_manuf_descr);
  654. }
  655. if(pof_usb->usb.str_prod_descr) {
  656. free(pof_usb->usb.str_prod_descr);
  657. }
  658. if(pof_usb->usb.str_serial_descr) {
  659. free(pof_usb->usb.str_serial_descr);
  660. }
  661. free(pof_usb);
  662. pof_usb = NULL;
  663. return NULL;
  664. }
  665. return pof_usb;
  666. }
  667. void pof_usb_stop(PoFUsb* pof_usb) {
  668. if(pof_usb) {
  669. furi_hal_usb_set_config(pof_usb->usb_prev, NULL);
  670. }
  671. }