pof_usb.c 14 KB

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