usb_config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #include "usb_config.h"
  2. #include "usb/scsi.h"
  3. #include "irq.h"
  4. #include "init.h"
  5. #include "gpio.h"
  6. #include "timer.h"
  7. #include "app_config.h"
  8. #include "lbuf.h"
  9. #ifdef CONFIG_ADAPTER_ENABLE
  10. #include "adapter_usb_hid.h"
  11. #endif//CONFIG_ADAPTER_ENABLE
  12. #define LOG_TAG_CONST USB
  13. #define LOG_TAG "[USB]"
  14. #define LOG_ERROR_ENABLE
  15. #define LOG_DEBUG_ENABLE
  16. #define LOG_INFO_ENABLE
  17. /* #define LOG_DUMP_ENABLE */
  18. #define LOG_CLI_ENABLE
  19. #include "debug.h"
  20. #define SET_INTERRUPT ___interrupt
  21. #define MAX_EP_TX 5
  22. #define MAX_EP_RX 5
  23. static usb_interrupt usb_interrupt_tx[USB_MAX_HW_NUM][MAX_EP_TX];// SEC(.usb_g_bss);
  24. static usb_interrupt usb_interrupt_rx[USB_MAX_HW_NUM][MAX_EP_RX];// SEC(.usb_h_bss);
  25. static u8 ep0_dma_buffer[EP0_SETUP_LEN] __attribute__((aligned(4))) SEC(.usb_ep0) ;
  26. #if TCFG_USB_SLAVE_MSD_ENABLE
  27. #define MSD_DMA_SIZE (64*2)
  28. #else
  29. #define MSD_DMA_SIZE 0
  30. #endif
  31. #if TCFG_USB_SLAVE_HID_ENABLE
  32. #define HID_DMA_SIZE 64
  33. #if CONFIG_APP_DONGLE
  34. #define HID_DMA_SIZE 64*CONFIG_BT_GATT_CLIENT_NUM
  35. #endif
  36. #else
  37. #define HID_DMA_SIZE 0
  38. #endif
  39. #if TCFG_USB_CUSTOM_HID_ENABLE
  40. #define CUSTOM_HID_DMA_SIZE 64 * 2
  41. #else
  42. #define CUSTOM_HID_DMA_SIZE 0
  43. #endif
  44. #if TCFG_USB_SLAVE_AUDIO_ENABLE
  45. #define AUDIO_DMA_SIZE 256+192
  46. #else
  47. #define AUDIO_DMA_SIZE 0
  48. #endif
  49. #if TCFG_USB_SLAVE_CDC_ENABLE
  50. #if CDC_INTR_EP_ENABLE
  51. #define CDC_DMA_SIZE (64*3)
  52. #else
  53. #define CDC_DMA_SIZE (64*2)
  54. #endif
  55. #else
  56. #define CDC_DMA_SIZE 0
  57. #endif
  58. struct usb_config_var_t {
  59. u8 usb_setup_buffer[USB_SETUP_SIZE];
  60. struct usb_ep_addr_t usb_ep_addr;
  61. struct usb_setup_t usb_setup;
  62. };
  63. static struct usb_config_var_t *usb_config_var = {NULL};
  64. #if USB_MALLOC_ENABLE
  65. #else
  66. static struct usb_config_var_t _usb_config_var SEC(.usb_config_var);
  67. #endif
  68. #define USB_DMA_BUF_ALIGN (8)
  69. #ifndef USB_DMA_BUF_MAX_SIZE
  70. #define USB_DMA_BUF_MAX_SIZE (HID_DMA_SIZE +USB_DMA_BUF_ALIGN+ AUDIO_DMA_SIZE +USB_DMA_BUF_ALIGN+ MSD_DMA_SIZE*2 + USB_DMA_BUF_ALIGN+CDC_DMA_SIZE + USB_DMA_BUF_ALIGN + CUSTOM_HID_DMA_SIZE + USB_DMA_BUF_ALIGN+ 100)
  71. #endif//USB_DMA_BUF_MAX_SIZE
  72. static u8 usb_dma_buf[USB_DMA_BUF_MAX_SIZE] SEC(.usb_msd_dma) __attribute__((aligned(8)));
  73. struct lbuff_head *usb_dma_lbuf = NULL;
  74. void usb_memory_init()
  75. {
  76. usb_dma_lbuf = lbuf_init(usb_dma_buf, sizeof(usb_dma_buf), USB_DMA_BUF_ALIGN, 0);
  77. log_info("%s() total dma size %x @%x", __func__, sizeof(usb_dma_buf), usb_dma_buf);
  78. }
  79. __attribute__((always_inline_when_const_args))
  80. void *usb_alloc_ep_dmabuffer(const usb_dev usb_id, u32 ep, u32 dma_size)
  81. {
  82. u8 *ep_buffer = NULL;
  83. u32 _ep = ep & 0xf;
  84. if (ep & USB_DIR_IN) {
  85. switch (_ep) {
  86. case 0:
  87. ep_buffer = ep0_dma_buffer;
  88. break;
  89. default :
  90. ep_buffer = lbuf_alloc(usb_dma_lbuf, dma_size);
  91. break;
  92. }
  93. } else {
  94. switch (_ep) {
  95. case 0:
  96. ep_buffer = ep0_dma_buffer;
  97. break;
  98. default :
  99. ep_buffer = lbuf_alloc(usb_dma_lbuf, dma_size);
  100. break;
  101. }
  102. }
  103. ASSERT(ep_buffer, "usb_alloc_ep_dmabuffer ep_buffer = NULL!!!, _ep = %x, dma_size = %d\n", ep, dma_size);
  104. log_info("ep_buffer = %x, ep = %x, dma_size = %d\n", ep_buffer, ep, dma_size);
  105. return ep_buffer;
  106. }
  107. static u8 usb_remote_wakeup_flag = USB_READY;//0:初始状态或suspend 1:从机已发送wakeup 2:主机已被唤醒
  108. static u32 usb_remote_wakeup_cnt = 0;
  109. static void usb_resume_sign(void *priv)
  110. {
  111. usb_remote_wakeup_flag = USB_RESUME_WAIT;
  112. usb_remote_wakeup_cnt = 0;
  113. log_info("slave remote_wakeup host signal has been sent");
  114. usb_dev usb_id = usb_device2id(priv);
  115. u32 reg = usb_read_power(usb_id);
  116. usb_write_power(usb_id, reg | BIT(2));//send resume
  117. os_time_dly(2);//10ms~20ms
  118. usb_write_power(usb_id, reg);//clean resume
  119. usb_sof_isr_reg(usb_id, 3, 0);
  120. }
  121. static u16 Wakeup_detect_timer;
  122. void usb_remote_wakeup_detect(void *priv)
  123. {
  124. if (usb_remote_wakeup_flag != USB_RESUME_WAIT) {
  125. return ;
  126. }
  127. if (usb_remote_wakeup_cnt == 0) {
  128. usb_remote_wakeup_flag = USB_SUSPEND;
  129. log_info("Wakeup fail!!! no SOF packet receive!\n");
  130. }
  131. if (usb_remote_wakeup_cnt > USB_REMOTE_WAKEUP_TIMEOUT_DETECT_TIMES - 200) {
  132. usb_remote_wakeup_flag = USB_READY;
  133. log_info("Receive %d SOF packet, USB ready!\n", usb_remote_wakeup_cnt);
  134. } else {
  135. usb_remote_wakeup_flag = USB_READY;
  136. log_info("Receive %d SOF packet, please increase USB_REMOTE_WAKEUP_TIMEOUT_DETECT_TIMES\n", usb_remote_wakeup_cnt);
  137. }
  138. }
  139. void usb_remote_wakeup(const usb_dev usb_id)
  140. {
  141. struct usb_device_t *usb_device = usb_id2device(usb_id);
  142. if (usb_device->bRemoteWakup) {
  143. sys_timeout_add(usb_device, usb_remote_wakeup_detect, USB_REMOTE_WAKEUP_TIMEOUT_DETECT_TIMES);
  144. sys_timeout_add(usb_device, usb_resume_sign, 1);
  145. } else {
  146. usb_device->bRemoteWakup = 1;
  147. }
  148. }
  149. void usb_phy_resume(const usb_dev usb_id)
  150. {
  151. usb_iomode(0);
  152. struct usb_device_t *usb_device = usb_id2device(usb_id);
  153. usb_write_faddr(usb_id, usb_device->baddr);
  154. if (usb_device->baddr == 0) {
  155. usb_device->bDeviceStates = USB_DEFAULT;
  156. } else {
  157. usb_device->bDeviceStates = USB_CONFIGURED;
  158. }
  159. usb_otg_resume(usb_id);
  160. }
  161. void usb_phy_suspend(const usb_dev usb_id)
  162. {
  163. gpio_set_pull_up(IO_PORT_DP, 1);
  164. gpio_set_pull_down(IO_PORT_DP, 0);
  165. gpio_set_direction(IO_PORT_DP, 1);
  166. usb_iomode(1);
  167. /* musb_read_usb(0, MUSB_INTRUSB); */
  168. usb_otg_suspend(usb_id, OTG_KEEP_STATE);
  169. }
  170. u32 usb_get_suspend_resume_status(const usb_dev usb_id)
  171. {
  172. switch (usb_remote_wakeup_flag) {
  173. case USB_READY:
  174. //log_info("USB READY\n");
  175. putchar('R');
  176. break;
  177. case USB_SUSPEND:
  178. log_info("USB SUSPEND\n");
  179. break;
  180. case USB_RESUME_WAIT:
  181. log_info("USB remote_wakeup send, RESUME WAIT\n");
  182. break;
  183. case USB_RESUME_OK://保留状态,未使用
  184. log_info("USB RESUME OK\n");
  185. break;
  186. default:
  187. break;
  188. }
  189. return usb_remote_wakeup_flag;
  190. }
  191. void usb_isr(const usb_dev usb_id)
  192. {
  193. u32 intr_usb, intr_usbe;
  194. u32 intr_tx, intr_txe;
  195. u32 intr_rx, intr_rxe;
  196. __asm__ volatile("ssync");
  197. usb_read_intr(usb_id, &intr_usb, &intr_tx, &intr_rx);
  198. usb_read_intre(usb_id, &intr_usbe, &intr_txe, &intr_rxe);
  199. struct usb_device_t *usb_device = usb_id2device(usb_id);
  200. intr_usb &= intr_usbe;
  201. intr_tx &= intr_txe;
  202. intr_rx &= intr_rxe;
  203. if (intr_usb & INTRUSB_SUSPEND) {
  204. log_error("usb suspend");
  205. usb_remote_wakeup_flag = USB_SUSPEND;
  206. #if USB_SUSPEND_RESUME
  207. usb_phy_suspend(usb_id);
  208. #endif
  209. #if USB_SUSPEND_RESUME_SYSTEM_NO_SLEEP
  210. printf("\n NULL \n");
  211. #endif
  212. }
  213. if (intr_usb & INTRUSB_RESET_BABBLE) {
  214. log_error("usb reset");
  215. usb_reset_interface(usb_device);
  216. #if USB_SUSPEND_RESUME || USB_SUSPEND_RESUME_SYSTEM_NO_SLEEP
  217. u32 reg = usb_read_power(usb_id);
  218. usb_write_power(usb_id, (reg | INTRUSB_SUSPEND | INTRUSB_RESUME));//enable suspend resume
  219. #endif
  220. }
  221. if (intr_usb & INTRUSB_RESUME) {
  222. log_error("usb resume");
  223. #if USB_SUSPEND_RESUME
  224. usb_phy_resume(usb_id);
  225. #endif
  226. }
  227. if (intr_tx & BIT(0)) {
  228. if (usb_interrupt_rx[usb_id][0]) {
  229. usb_interrupt_rx[usb_id][0](usb_device, 0);
  230. } else {
  231. #if USB_SUSPEND_RESUME || USB_SUSPEND_RESUME_SYSTEM_NO_SLEEP
  232. if (usb_remote_wakeup_flag == USB_RESUME_WAIT) {
  233. if (usb_device->bsetup_phase == USB_EP0_STAGE_SETUP) {
  234. usb_remote_wakeup_flag = USB_READY;
  235. log_info("receive setup packet");
  236. }
  237. }
  238. #endif
  239. usb_control_transfer(usb_device);
  240. }
  241. }
  242. for (int i = 1; i < MAX_EP_TX; i++) {
  243. if (intr_tx & BIT(i)) {
  244. if (usb_interrupt_tx[usb_id][i]) {
  245. usb_interrupt_tx[usb_id][i](usb_device, i);
  246. }
  247. }
  248. }
  249. for (int i = 1; i < MAX_EP_RX; i++) {
  250. if (intr_rx & BIT(i)) {
  251. if (usb_interrupt_rx[usb_id][i]) {
  252. usb_interrupt_rx[usb_id][i](usb_device, i);
  253. }
  254. }
  255. }
  256. __asm__ volatile("csync");
  257. }
  258. void usb_sof_isr(const usb_dev usb_id)
  259. {
  260. usb_sof_clr_pnd(usb_id);
  261. static u32 sof_count = 0;
  262. #if USB_SUSPEND_RESUME || USB_SUSPEND_RESUME_SYSTEM_NO_SLEEP
  263. usb_remote_wakeup_cnt++;
  264. #else
  265. if ((sof_count++ % 1000) == 0) {
  266. log_d("sof 1s isr frame:%d", usb_read_sofframe(usb_id));
  267. }
  268. #endif
  269. }
  270. void usb_suspend_check(void *p)
  271. {
  272. usb_dev usb_id = (usb_dev)p;
  273. static u16 sof_frame = 0;
  274. u16 frame = usb_read_sofframe(usb_id);// sof frame 不更新,则usb进入断开或者suspend状态
  275. if (frame == sof_frame) {
  276. usb_phy_suspend(usb_id);
  277. }
  278. sof_frame = frame;
  279. }
  280. SET_INTERRUPT
  281. void usb0_g_isr()
  282. {
  283. usb_isr(0);
  284. }
  285. SET_INTERRUPT
  286. void usb0_sof_isr()
  287. {
  288. usb_sof_isr(0);
  289. }
  290. #if USB_MAX_HW_NUM == 2
  291. SET_INTERRUPT
  292. void usb1_g_isr()
  293. {
  294. usb_isr(1);
  295. }
  296. SET_INTERRUPT
  297. void usb1_sof_isr()
  298. {
  299. usb_sof_isr(1);
  300. }
  301. #endif
  302. __attribute__((always_inline_when_const_args))
  303. u32 usb_g_set_intr_hander(const usb_dev usb_id, u32 ep, usb_interrupt hander)
  304. {
  305. if (ep & USB_DIR_IN) {
  306. usb_interrupt_tx[usb_id][ep & 0xf] = hander;
  307. } else {
  308. usb_interrupt_rx[usb_id][ep] = hander;
  309. }
  310. return 0;
  311. }
  312. void usb_g_isr_reg(const usb_dev usb_id, u8 priority, u8 cpu_id)
  313. {
  314. if (usb_id == 0) {
  315. request_irq(IRQ_USB_CTRL_IDX, priority, usb0_g_isr, cpu_id);
  316. } else {
  317. #if USB_MAX_HW_NUM == 2
  318. request_irq(IRQ_USB1_CTRL_IDX, priority, usb1_g_isr, cpu_id);
  319. #endif
  320. }
  321. }
  322. void usb_sof_isr_reg(const usb_dev usb_id, u8 priority, u8 cpu_id)
  323. {
  324. if (usb_id == 0) {
  325. request_irq(IRQ_USB_SOF_IDX, priority, usb0_sof_isr, cpu_id);
  326. } else {
  327. #if USB_MAX_HW_NUM == 2
  328. request_irq(IRQ_USB1_SOF_IDX, priority, usb1_sof_isr, cpu_id);
  329. #endif
  330. }
  331. usb_sofie_enable(usb_id);
  332. }
  333. u32 usb_config(const usb_dev usb_id)
  334. {
  335. memset(usb_interrupt_rx[usb_id], 0, sizeof(usb_interrupt_rx[usb_id]));
  336. memset(usb_interrupt_tx[usb_id], 0, sizeof(usb_interrupt_tx[usb_id]));
  337. if (!usb_config_var) {
  338. #if USB_MALLOC_ENABLE
  339. usb_config_var = (struct usb_config_var_t *)zalloc(sizeof(struct usb_config_var_t));
  340. if (!usb_config_var) {
  341. return -1;
  342. }
  343. #else
  344. memset(&_usb_config_var, 0, sizeof(_usb_config_var));
  345. usb_config_var = &_usb_config_var;
  346. #endif
  347. }
  348. log_debug("zalloc: usb_config_var = %x\n", usb_config_var);
  349. usb_var_init(usb_id, &(usb_config_var->usb_ep_addr));
  350. usb_setup_init(usb_id, &(usb_config_var->usb_setup), usb_config_var->usb_setup_buffer);
  351. return 0;
  352. }
  353. u32 usb_release(const usb_dev usb_id)
  354. {
  355. log_debug("free zalloc: usb_config_var = %x\n", usb_id, usb_config_var);
  356. usb_var_init(usb_id, NULL);
  357. usb_setup_init(usb_id, NULL, NULL);
  358. #if USB_MALLOC_ENABLE
  359. if (usb_config_var) {
  360. log_debug("free: usb_config_var = %x\n", usb_config_var);
  361. free(usb_config_var);
  362. }
  363. #endif
  364. usb_config_var = NULL;
  365. return 0;
  366. }