pof_usb.c 27 KB

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