pof_usb.c 25 KB

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