pof_usb.c 23 KB

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