pof_usb.c 26 KB

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