pof_usb.c 15 KB

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