pof_usb.c 14 KB

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