pof_usb.c 22 KB

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