pof_usb.c 27 KB

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