pof_usb.c 28 KB

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