pof_usb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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_EP_IN (0x81)
  8. #define POF_USB_EP_OUT (0x02)
  9. #define POF_USB_ACTUAL_OUTPUT_SIZE 0x20
  10. static const struct usb_string_descriptor dev_manuf_desc =
  11. USB_ARRAY_DESC(0x41, 0x63, 0x74, 0x69, 0x76, 0x69, 0x73, 0x69, 0x6f, 0x6e, 0x00);
  12. static const struct usb_string_descriptor dev_product_desc =
  13. USB_ARRAY_DESC(0x53, 0x70, 0x79, 0x72, 0x6f, 0x20, 0x50, 0x6f, 0x72, 0x74, 0x61, 0x00);
  14. static const uint8_t hid_report_desc[] = {0x06, 0x00, 0xFF, 0x09, 0x01, 0xA1, 0x01, 0x19,
  15. 0x01, 0x29, 0x40, 0x15, 0x00, 0x26, 0xFF, 0x00,
  16. 0x75, 0x08, 0x95, 0x20, 0x81, 0x00, 0x19, 0x01,
  17. 0x29, 0x40, 0x91, 0x00, 0xC0};
  18. static usbd_respond pof_usb_ep_config(usbd_device* dev, uint8_t cfg);
  19. static usbd_respond
  20. pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
  21. static void pof_usb_send(usbd_device* dev, uint8_t* buf, uint16_t len);
  22. static int32_t pof_usb_receive(usbd_device* dev, uint8_t* buf, uint16_t max_len);
  23. static PoFUsb* pof_cur = NULL;
  24. static int32_t pof_thread_worker(void* context) {
  25. PoFUsb* pof_usb = context;
  26. usbd_device* dev = pof_usb->dev;
  27. VirtualPortal* virtual_portal = pof_usb->virtual_portal;
  28. UNUSED(dev);
  29. uint32_t len_data = 0;
  30. uint8_t tx_data[POF_USB_TX_MAX_SIZE] = {0};
  31. uint32_t timeout = TIMEOUT_NORMAL; // FuriWaitForever; //ms
  32. uint32_t last = 0;
  33. while(true) {
  34. uint32_t now = furi_get_tick();
  35. uint32_t flags = furi_thread_flags_wait(EventAll, FuriFlagWaitAny, timeout);
  36. if(flags & EventRx) { // fast flag
  37. if(virtual_portal->speaker) {
  38. uint8_t buf[POF_USB_RX_MAX_SIZE];
  39. len_data = pof_usb_receive(dev, buf, POF_USB_RX_MAX_SIZE);
  40. // https://github.com/xMasterX/all-the-plugins/blob/dev/base_pack/wav_player/wav_player_hal.c
  41. if(len_data > 0) {
  42. /*
  43. FURI_LOG_RAW_I("pof_usb_receive: ");
  44. for(uint32_t i = 0; i < len_data; i++) {
  45. FURI_LOG_RAW_I("%02x", buf[i]);
  46. }
  47. FURI_LOG_RAW_I("\r\n");
  48. */
  49. virtual_portal_process_audio(virtual_portal, buf, len_data);
  50. }
  51. }
  52. if(pof_usb->dataAvailable > 0) {
  53. memset(tx_data, 0, sizeof(tx_data));
  54. int send_len =
  55. virtual_portal_process_message(virtual_portal, pof_usb->data, tx_data);
  56. if(send_len > 0) {
  57. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  58. timeout = TIMEOUT_AFTER_RESPONSE;
  59. if(virtual_portal->speaker) {
  60. timeout = TIMEOUT_AFTER_MUSIC;
  61. }
  62. last = now;
  63. }
  64. pof_usb->dataAvailable = 0;
  65. }
  66. // Check next status time since the timeout based one might be starved by incoming packets.
  67. if(now > last + timeout) {
  68. memset(tx_data, 0, sizeof(tx_data));
  69. len_data = virtual_portal_send_status(virtual_portal, tx_data);
  70. if(len_data > 0) {
  71. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  72. }
  73. last = now;
  74. timeout = TIMEOUT_NORMAL;
  75. if(virtual_portal->speaker) {
  76. timeout = TIMEOUT_AFTER_MUSIC;
  77. }
  78. }
  79. flags &= ~EventRx; // clear flag
  80. }
  81. if(flags) {
  82. if(flags & EventResetSio) {
  83. }
  84. if(flags & EventTxComplete) {
  85. pof_usb->tx_complete = true;
  86. }
  87. if(flags & EventTxImmediate) {
  88. pof_usb->tx_immediate = true;
  89. if(pof_usb->tx_complete) {
  90. flags |= EventTx;
  91. }
  92. }
  93. if(flags & EventTx) {
  94. pof_usb->tx_complete = false;
  95. pof_usb->tx_immediate = false;
  96. }
  97. if(flags & EventExit) {
  98. FURI_LOG_I(TAG, "exit");
  99. break;
  100. }
  101. }
  102. if(flags == (uint32_t)FuriFlagErrorISR) { // timeout
  103. memset(tx_data, 0, sizeof(tx_data));
  104. len_data = virtual_portal_send_status(virtual_portal, tx_data);
  105. if(len_data > 0) {
  106. pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
  107. }
  108. last = now;
  109. timeout = TIMEOUT_NORMAL;
  110. if(virtual_portal->speaker) {
  111. timeout = TIMEOUT_AFTER_MUSIC;
  112. }
  113. }
  114. }
  115. return 0;
  116. }
  117. static void pof_usb_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
  118. UNUSED(intf);
  119. PoFUsb* pof_usb = ctx;
  120. pof_cur = pof_usb;
  121. pof_usb->dev = dev;
  122. usbd_reg_config(dev, pof_usb_ep_config);
  123. usbd_reg_control(dev, pof_hid_control);
  124. UNUSED(pof_hid_control);
  125. usbd_connect(dev, true);
  126. pof_usb->thread = furi_thread_alloc();
  127. furi_thread_set_name(pof_usb->thread, "PoFUsb");
  128. furi_thread_set_stack_size(pof_usb->thread, 2 * 1024);
  129. furi_thread_set_context(pof_usb->thread, ctx);
  130. furi_thread_set_callback(pof_usb->thread, pof_thread_worker);
  131. furi_thread_start(pof_usb->thread);
  132. }
  133. static void pof_usb_deinit(usbd_device* dev) {
  134. usbd_reg_config(dev, NULL);
  135. usbd_reg_control(dev, NULL);
  136. PoFUsb* pof_usb = pof_cur;
  137. if(!pof_usb || pof_usb->dev != dev) {
  138. return;
  139. }
  140. pof_cur = NULL;
  141. furi_assert(pof_usb->thread);
  142. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventExit);
  143. furi_thread_join(pof_usb->thread);
  144. furi_thread_free(pof_usb->thread);
  145. pof_usb->thread = NULL;
  146. free(pof_usb->usb.str_prod_descr);
  147. pof_usb->usb.str_prod_descr = NULL;
  148. free(pof_usb->usb.str_serial_descr);
  149. pof_usb->usb.str_serial_descr = NULL;
  150. free(pof_usb);
  151. }
  152. static void pof_usb_send(usbd_device* dev, uint8_t* buf, uint16_t len) {
  153. // Hide frequent responses
  154. /*
  155. if(buf[0] != 'S' && buf[0] != 'J') {
  156. FURI_LOG_RAW_D("> ");
  157. for(size_t i = 0; i < len; i++) {
  158. FURI_LOG_RAW_D("%02x", buf[i]);
  159. }
  160. FURI_LOG_RAW_D("\r\n");
  161. }
  162. */
  163. usbd_ep_write(dev, POF_USB_EP_IN, buf, len);
  164. }
  165. static int32_t pof_usb_receive(usbd_device* dev, uint8_t* buf, uint16_t max_len) {
  166. int32_t len = usbd_ep_read(dev, POF_USB_EP_OUT, buf, max_len);
  167. return ((len < 0) ? 0 : len);
  168. }
  169. static void pof_usb_wakeup(usbd_device* dev) {
  170. UNUSED(dev);
  171. }
  172. static void pof_usb_suspend(usbd_device* dev) {
  173. PoFUsb* pof_usb = pof_cur;
  174. if(!pof_usb || pof_usb->dev != dev) return;
  175. }
  176. static void pof_usb_rx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  177. UNUSED(dev);
  178. UNUSED(event);
  179. UNUSED(ep);
  180. PoFUsb* pof_usb = pof_cur;
  181. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventRx);
  182. }
  183. static void pof_usb_tx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  184. UNUSED(dev);
  185. UNUSED(event);
  186. UNUSED(ep);
  187. PoFUsb* pof_usb = pof_cur;
  188. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventTxComplete);
  189. }
  190. static usbd_respond pof_usb_ep_config(usbd_device* dev, uint8_t cfg) {
  191. switch(cfg) {
  192. case 0: // deconfig
  193. usbd_ep_deconfig(dev, POF_USB_EP_OUT);
  194. usbd_ep_deconfig(dev, POF_USB_EP_IN);
  195. usbd_reg_endpoint(dev, POF_USB_EP_OUT, NULL);
  196. usbd_reg_endpoint(dev, POF_USB_EP_IN, NULL);
  197. return usbd_ack;
  198. case 1: // config
  199. usbd_ep_config(dev, POF_USB_EP_IN, USB_EPTYPE_INTERRUPT, POF_USB_EP_IN_SIZE);
  200. usbd_ep_config(dev, POF_USB_EP_OUT, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
  201. usbd_reg_endpoint(dev, POF_USB_EP_IN, pof_usb_tx_ep_callback);
  202. usbd_reg_endpoint(dev, POF_USB_EP_OUT, pof_usb_rx_ep_callback);
  203. return usbd_ack;
  204. }
  205. return usbd_fail;
  206. }
  207. struct PoFUsbDescriptor {
  208. struct usb_config_descriptor config;
  209. struct usb_interface_descriptor intf;
  210. struct usb_hid_descriptor hid_desc;
  211. struct usb_endpoint_descriptor ep_in;
  212. struct usb_endpoint_descriptor ep_out;
  213. } __attribute__((packed));
  214. static const struct usb_device_descriptor usb_pof_dev_descr = {
  215. .bLength = sizeof(struct usb_device_descriptor),
  216. .bDescriptorType = USB_DTYPE_DEVICE,
  217. .bcdUSB = VERSION_BCD(2, 0, 0),
  218. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  219. .bDeviceSubClass = USB_SUBCLASS_NONE,
  220. .bDeviceProtocol = USB_PROTO_NONE,
  221. .bMaxPacketSize0 = USB_EP0_SIZE,
  222. .idVendor = POF_USB_VID,
  223. .idProduct = POF_USB_PID,
  224. .bcdDevice = VERSION_BCD(1, 0, 0),
  225. .iManufacturer = 1, // UsbDevManuf
  226. .iProduct = 2, // UsbDevProduct
  227. .iSerialNumber = 0,
  228. .bNumConfigurations = 1,
  229. };
  230. static const struct PoFUsbDescriptor usb_pof_cfg_descr = {
  231. .config =
  232. {
  233. .bLength = sizeof(struct usb_config_descriptor),
  234. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  235. .wTotalLength = sizeof(struct PoFUsbDescriptor),
  236. .bNumInterfaces = 1,
  237. .bConfigurationValue = 1,
  238. .iConfiguration = NO_DESCRIPTOR,
  239. .bmAttributes = USB_CFG_ATTR_RESERVED,
  240. .bMaxPower = USB_CFG_POWER_MA(500),
  241. },
  242. .intf =
  243. {
  244. .bLength = sizeof(struct usb_interface_descriptor),
  245. .bDescriptorType = USB_DTYPE_INTERFACE,
  246. .bInterfaceNumber = 0,
  247. .bAlternateSetting = 0,
  248. .bNumEndpoints = 2,
  249. .bInterfaceClass = USB_CLASS_HID,
  250. .bInterfaceSubClass = USB_HID_SUBCLASS_NONBOOT,
  251. .bInterfaceProtocol = USB_HID_PROTO_NONBOOT,
  252. .iInterface = NO_DESCRIPTOR,
  253. },
  254. .hid_desc =
  255. {
  256. .bLength = sizeof(struct usb_hid_descriptor),
  257. .bDescriptorType = USB_DTYPE_HID,
  258. .bcdHID = VERSION_BCD(1, 1, 1),
  259. .bCountryCode = USB_HID_COUNTRY_NONE,
  260. .bNumDescriptors = 1,
  261. .bDescriptorType0 = USB_DTYPE_HID_REPORT,
  262. .wDescriptorLength0 = sizeof(hid_report_desc),
  263. },
  264. .ep_in =
  265. {
  266. .bLength = sizeof(struct usb_endpoint_descriptor),
  267. .bDescriptorType = USB_DTYPE_ENDPOINT,
  268. .bEndpointAddress = POF_USB_EP_IN,
  269. .bmAttributes = USB_EPTYPE_INTERRUPT,
  270. .wMaxPacketSize = 0x40,
  271. .bInterval = HID_INTERVAL,
  272. },
  273. .ep_out =
  274. {
  275. .bLength = sizeof(struct usb_endpoint_descriptor),
  276. .bDescriptorType = USB_DTYPE_ENDPOINT,
  277. .bEndpointAddress = POF_USB_EP_OUT,
  278. .bmAttributes = USB_EPTYPE_INTERRUPT,
  279. .wMaxPacketSize = 0x40,
  280. .bInterval = HID_INTERVAL,
  281. },
  282. };
  283. /* Control requests handler */
  284. static usbd_respond
  285. pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  286. UNUSED(callback);
  287. uint8_t wValueH = req->wValue >> 8;
  288. uint16_t length = req->wLength;
  289. PoFUsb* pof_usb = pof_cur;
  290. /* HID control requests */
  291. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  292. (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
  293. req->wIndex == 0) {
  294. switch(req->bRequest) {
  295. case USB_HID_SETIDLE:
  296. return usbd_ack;
  297. case USB_HID_SETPROTOCOL:
  298. return usbd_ack;
  299. case USB_HID_GETREPORT:
  300. dev->status.data_ptr = pof_usb->tx_data;
  301. dev->status.data_count = sizeof(pof_usb->tx_data);
  302. return usbd_ack;
  303. case USB_HID_SETREPORT:
  304. if(wValueH == HID_REPORT_TYPE_INPUT) {
  305. if(length == POF_USB_RX_MAX_SIZE) {
  306. return usbd_ack;
  307. }
  308. } else if(wValueH == HID_REPORT_TYPE_OUTPUT) {
  309. memcpy(pof_usb->data, req->data, req->wLength);
  310. pof_usb->dataAvailable += req->wLength;
  311. furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventRx);
  312. return usbd_ack;
  313. } else if(wValueH == HID_REPORT_TYPE_FEATURE) {
  314. return usbd_ack;
  315. }
  316. return usbd_fail;
  317. default:
  318. return usbd_fail;
  319. }
  320. }
  321. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  322. (USB_REQ_INTERFACE | USB_REQ_STANDARD) &&
  323. req->wIndex == 0 && req->bRequest == USB_STD_GET_DESCRIPTOR) {
  324. switch(wValueH) {
  325. case USB_DTYPE_HID:
  326. dev->status.data_ptr = (uint8_t*)&(usb_pof_cfg_descr.hid_desc);
  327. dev->status.data_count = sizeof(usb_pof_cfg_descr.hid_desc);
  328. return usbd_ack;
  329. case USB_DTYPE_HID_REPORT:
  330. dev->status.data_ptr = (uint8_t*)hid_report_desc;
  331. dev->status.data_count = sizeof(hid_report_desc);
  332. return usbd_ack;
  333. default:
  334. return usbd_fail;
  335. }
  336. }
  337. return usbd_fail;
  338. }
  339. PoFUsb* pof_usb_start(VirtualPortal* virtual_portal) {
  340. PoFUsb* pof_usb = malloc(sizeof(PoFUsb));
  341. pof_usb->virtual_portal = virtual_portal;
  342. pof_usb->dataAvailable = 0;
  343. furi_hal_usb_unlock();
  344. pof_usb->usb_prev = furi_hal_usb_get_config();
  345. pof_usb->usb.init = pof_usb_init;
  346. pof_usb->usb.deinit = pof_usb_deinit;
  347. pof_usb->usb.wakeup = pof_usb_wakeup;
  348. pof_usb->usb.suspend = pof_usb_suspend;
  349. pof_usb->usb.dev_descr = (struct usb_device_descriptor*)&usb_pof_dev_descr;
  350. pof_usb->usb.str_manuf_descr = (void*)&dev_manuf_desc;
  351. pof_usb->usb.str_prod_descr = (void*)&dev_product_desc;
  352. pof_usb->usb.str_serial_descr = NULL;
  353. pof_usb->usb.cfg_descr = (void*)&usb_pof_cfg_descr;
  354. if(!furi_hal_usb_set_config(&pof_usb->usb, pof_usb)) {
  355. FURI_LOG_E(TAG, "USB locked, can not start");
  356. if(pof_usb->usb.str_manuf_descr) {
  357. free(pof_usb->usb.str_manuf_descr);
  358. }
  359. if(pof_usb->usb.str_prod_descr) {
  360. free(pof_usb->usb.str_prod_descr);
  361. }
  362. if(pof_usb->usb.str_serial_descr) {
  363. free(pof_usb->usb.str_serial_descr);
  364. }
  365. free(pof_usb);
  366. pof_usb = NULL;
  367. return NULL;
  368. }
  369. return pof_usb;
  370. }
  371. void pof_usb_stop(PoFUsb* pof_usb) {
  372. if(pof_usb) {
  373. furi_hal_usb_set_config(pof_usb->usb_prev, NULL);
  374. }
  375. }