您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1119 行
36KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost.h"
  25. // Size of the periodic list, in milliseconds. This determines the
  26. // slowest rate we can poll interrupt endpoints. Each entry uses
  27. // 12 bytes (4 for a pointer, 8 for bandwidth management).
  28. // may be 8, 16, 32, 64, 128, 256, 512, 1024
  29. #define PERIODIC_LIST_SIZE 32
  30. static uint32_t periodictable[PERIODIC_LIST_SIZE] __attribute__ ((aligned(4096), used));
  31. static uint8_t uframe_bandwidth[PERIODIC_LIST_SIZE*8];
  32. static uint8_t port_state;
  33. #define PORT_STATE_DISCONNECTED 0
  34. #define PORT_STATE_DEBOUNCE 1
  35. #define PORT_STATE_RESET 2
  36. #define PORT_STATE_RECOVERY 3
  37. #define PORT_STATE_ACTIVE 4
  38. static Device_t *rootdev=NULL;
  39. static Transfer_t *async_followup_first=NULL;
  40. static Transfer_t *async_followup_last=NULL;
  41. static Transfer_t *periodic_followup_first=NULL;
  42. static Transfer_t *periodic_followup_last=NULL;
  43. static USBDriverTimer *active_timers=NULL;
  44. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  45. uint32_t pid, uint32_t data01, bool irq);
  46. static bool followup_Transfer(Transfer_t *transfer);
  47. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last);
  48. static void remove_from_async_followup_list(Transfer_t *transfer);
  49. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last);
  50. static void remove_from_periodic_followup_list(Transfer_t *transfer);
  51. void USBHost::begin()
  52. {
  53. // Teensy 3.6 has USB host power controlled by PTE6
  54. PORTE_PCR6 = PORT_PCR_MUX(1);
  55. GPIOE_PDDR |= (1<<6);
  56. GPIOE_PSOR = (1<<6); // turn on USB host power
  57. delay(10);
  58. println("sizeof Device = ", sizeof(Device_t));
  59. println("sizeof Pipe = ", sizeof(Pipe_t));
  60. println("sizeof Transfer = ", sizeof(Transfer_t));
  61. if ((sizeof(Pipe_t) & 0x1F) || (sizeof(Transfer_t) & 0x1F)) {
  62. println("ERROR: Pipe_t & Transfer_t must be multiples of 32 bytes!");
  63. while (1) ; // die here
  64. }
  65. // configure the MPU to allow USBHS DMA to access memory
  66. MPU_RGDAAC0 |= 0x30000000;
  67. //println("MPU_RGDAAC0 = ", MPU_RGDAAC0, HEX);
  68. // turn on clocks
  69. MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz
  70. OSC0_CR |= OSC_ERCLKEN;
  71. SIM_SOPT2 |= SIM_SOPT2_USBREGEN; // turn on USB regulator
  72. SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC; // use IRC for slow clock
  73. println("power up USBHS PHY");
  74. SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM; // disable USB current limit
  75. //SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237
  76. SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
  77. USBHSDCD_CLOCK = 33 << 2;
  78. //print("init USBHS PHY & PLL");
  79. // init process: page 1681-1682
  80. USBPHY_CTRL_CLR = (USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE); // // CTRL pg 1698
  81. USBPHY_CTRL_SET = USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3;
  82. //USBPHY_CTRL_SET = USBPHY_CTRL_FSDLL_RST_EN; // TODO: what does this do??
  83. USBPHY_TRIM_OVERRIDE_EN_SET = 1;
  84. USBPHY_PLL_SIC = USBPHY_PLL_SIC_PLL_POWER | USBPHY_PLL_SIC_PLL_ENABLE |
  85. USBPHY_PLL_SIC_PLL_DIV_SEL(1) | USBPHY_PLL_SIC_PLL_EN_USB_CLKS;
  86. // wait for the PLL to lock
  87. int count=0;
  88. while ((USBPHY_PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK) == 0) {
  89. count++;
  90. }
  91. //println("PLL locked, waited ", count);
  92. // turn on power to PHY
  93. USBPHY_PWD = 0;
  94. delay(10);
  95. // sanity check, connect 470K pullup & 100K pulldown and watch D+ voltage change
  96. //USBPHY_ANACTRL_CLR = (1<<10); // turn off both 15K pulldowns... works! :)
  97. // sanity check, output clocks on pin 9 for testing
  98. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(3); // LPO 1kHz
  99. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(2); // Flash
  100. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(6); // XTAL
  101. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(7); // IRC 48MHz
  102. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(4); // MCGIRCLK
  103. //CORE_PIN9_CONFIG = PORT_PCR_MUX(5); // CLKOUT on PTC3 Alt5 (Arduino pin 9)
  104. // now with the PHY up and running, start up USBHS
  105. //print("begin ehci reset");
  106. USBHS_USBCMD |= USBHS_USBCMD_RST;
  107. //count = 0;
  108. while (USBHS_USBCMD & USBHS_USBCMD_RST) {
  109. //count++;
  110. }
  111. //println(" reset waited ", count);
  112. init_Device_Pipe_Transfer_memory();
  113. for (int i=0; i < 32; i++) {
  114. periodictable[i] = 1;
  115. }
  116. memset(uframe_bandwidth, 0, sizeof(uframe_bandwidth));
  117. port_state = PORT_STATE_DISCONNECTED;
  118. USBHS_USB_SBUSCFG = 1; // System Bus Interface Configuration
  119. // turn on the USBHS controller
  120. //USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  121. USBHS_USBMODE = USBHS_USBMODE_CM(3); // host mode
  122. USBHS_USBINTR = 0;
  123. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  124. USBHS_FRINDEX = 0;
  125. USBHS_ASYNCLISTADDR = 0;
  126. USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
  127. USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE | USBHS_USBCMD_PSE |
  128. #if PERIODIC_LIST_SIZE == 8
  129. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(3);
  130. #elif PERIODIC_LIST_SIZE == 16
  131. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(2);
  132. #elif PERIODIC_LIST_SIZE == 32
  133. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1);
  134. #elif PERIODIC_LIST_SIZE == 64
  135. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0);
  136. #elif PERIODIC_LIST_SIZE == 128
  137. USBHS_USBCMD_FS(3);
  138. #elif PERIODIC_LIST_SIZE == 256
  139. USBHS_USBCMD_FS(2);
  140. #elif PERIODIC_LIST_SIZE == 512
  141. USBHS_USBCMD_FS(1);
  142. #elif PERIODIC_LIST_SIZE == 1024
  143. USBHS_USBCMD_FS(0);
  144. #else
  145. #error "Unsupported PERIODIC_LIST_SIZE"
  146. #endif
  147. // turn on the USB port
  148. //USBHS_PORTSC1 = USBHS_PORTSC_PP;
  149. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  150. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  151. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  152. //println("USBHS_ASYNCLISTADDR = ", USBHS_ASYNCLISTADDR, HEX);
  153. //println("USBHS_PERIODICLISTBASE = ", USBHS_PERIODICLISTBASE, HEX);
  154. //println("periodictable = ", (uint32_t)periodictable, HEX);
  155. // enable interrupts, after this point interruts to all the work
  156. attachInterruptVector(IRQ_USBHS, isr);
  157. NVIC_ENABLE_IRQ(IRQ_USBHS);
  158. USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0 | USBHS_USBINTR_TIE1;
  159. USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
  160. USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
  161. }
  162. // EHCI registers page default
  163. // -------------- ---- -------
  164. // USBHS_USBCMD 1599 00080000 USB Command
  165. // USBHS_USBSTS 1602 00000000 USB Status
  166. // USBHS_USBINTR 1606 00000000 USB Interrupt Enable
  167. // USBHS_FRINDEX 1609 00000000 Frame Index Register
  168. // USBHS_PERIODICLISTBASE 1610 undefine Periodic Frame List Base Address
  169. // USBHS_ASYNCLISTADDR 1612 undefine Asynchronous List Address
  170. // USBHS_PORTSC1 1619 00002000 Port Status and Control
  171. // USBHS_USBMODE 1629 00005000 USB Mode
  172. // USBHS_GPTIMERnCTL 1591 00000000 General Purpose Timer n Control
  173. // PORT_STATE_DISCONNECTED 0
  174. // PORT_STATE_DEBOUNCE 1
  175. // PORT_STATE_RESET 2
  176. // PORT_STATE_RECOVERY 3
  177. // PORT_STATE_ACTIVE 4
  178. void USBHost::isr()
  179. {
  180. uint32_t stat = USBHS_USBSTS;
  181. USBHS_USBSTS = stat; // clear pending interrupts
  182. //stat &= USBHS_USBINTR; // mask away unwanted interrupts
  183. #if 1
  184. println();
  185. println("ISR: ", stat, HEX);
  186. //if (stat & USBHS_USBSTS_UI) println(" USB Interrupt");
  187. if (stat & USBHS_USBSTS_UEI) println(" USB Error");
  188. if (stat & USBHS_USBSTS_PCI) println(" Port Change");
  189. //if (stat & USBHS_USBSTS_FRI) println(" Frame List Rollover");
  190. if (stat & USBHS_USBSTS_SEI) println(" System Error");
  191. //if (stat & USBHS_USBSTS_AAI) println(" Async Advance (doorbell)");
  192. if (stat & USBHS_USBSTS_URI) println(" Reset Recv");
  193. //if (stat & USBHS_USBSTS_SRI) println(" SOF");
  194. if (stat & USBHS_USBSTS_SLI) println(" Suspend");
  195. if (stat & USBHS_USBSTS_HCH) println(" Host Halted");
  196. //if (stat & USBHS_USBSTS_RCL) println(" Reclamation");
  197. //if (stat & USBHS_USBSTS_PS) println(" Periodic Sched En");
  198. //if (stat & USBHS_USBSTS_AS) println(" Async Sched En");
  199. if (stat & USBHS_USBSTS_NAKI) println(" NAK");
  200. if (stat & USBHS_USBSTS_UAI) println(" USB Async");
  201. if (stat & USBHS_USBSTS_UPI) println(" USB Periodic");
  202. if (stat & USBHS_USBSTS_TI0) println(" Timer0");
  203. if (stat & USBHS_USBSTS_TI1) println(" Timer1");
  204. #endif
  205. if (stat & USBHS_USBSTS_UAI) { // completed qTD(s) from the async schedule
  206. println("Async Followup");
  207. //print(async_followup_first, async_followup_last);
  208. Transfer_t *p = async_followup_first;
  209. while (p) {
  210. if (followup_Transfer(p)) {
  211. // transfer completed
  212. Transfer_t *next = p->next_followup;
  213. remove_from_async_followup_list(p);
  214. free_Transfer(p);
  215. p = next;
  216. } else {
  217. // transfer still pending
  218. p = p->next_followup;
  219. }
  220. }
  221. //print(async_followup_first, async_followup_last);
  222. }
  223. if (stat & USBHS_USBSTS_UPI) { // completed qTD(s) from the periodic schedule
  224. println("Periodic Followup");
  225. Transfer_t *p = periodic_followup_first;
  226. while (p) {
  227. if (followup_Transfer(p)) {
  228. // transfer completed
  229. Transfer_t *next = p->next_followup;
  230. remove_from_periodic_followup_list(p);
  231. free_Transfer(p);
  232. p = next;
  233. } else {
  234. // transfer still pending
  235. p = p->next_followup;
  236. }
  237. }
  238. }
  239. if (stat & USBHS_USBSTS_PCI) { // port change detected
  240. const uint32_t portstat = USBHS_PORTSC1;
  241. println("port change: ", portstat, HEX);
  242. USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
  243. if (portstat & USBHS_PORTSC_OCC) {
  244. println(" overcurrent change");
  245. }
  246. if (portstat & USBHS_PORTSC_CSC) {
  247. if (portstat & USBHS_PORTSC_CCS) {
  248. println(" connect");
  249. if (port_state == PORT_STATE_DISCONNECTED
  250. || port_state == PORT_STATE_DEBOUNCE) {
  251. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  252. port_state = PORT_STATE_DEBOUNCE;
  253. USBHS_GPTIMER0LD = 100000; // microseconds
  254. USBHS_GPTIMER0CTL =
  255. USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  256. stat &= ~USBHS_USBSTS_TI0;
  257. }
  258. } else {
  259. println(" disconnect");
  260. port_state = PORT_STATE_DISCONNECTED;
  261. USBPHY_CTRL_CLR = USBPHY_CTRL_ENHOSTDISCONDETECT;
  262. disconnect_Device(rootdev);
  263. rootdev = NULL;
  264. }
  265. }
  266. if (portstat & USBHS_PORTSC_PEC) {
  267. // PEC bit only detects disable
  268. println(" disable");
  269. } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
  270. println(" port enabled");
  271. port_state = PORT_STATE_RECOVERY;
  272. // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188)
  273. USBHS_GPTIMER0LD = 10000; // microseconds
  274. USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  275. if (USBHS_PORTSC1 & USBHS_PORTSC_HSP) {
  276. // turn on high-speed disconnect detector
  277. USBPHY_CTRL_SET = USBPHY_CTRL_ENHOSTDISCONDETECT;
  278. }
  279. }
  280. if (portstat & USBHS_PORTSC_FPR) {
  281. println(" force resume");
  282. }
  283. }
  284. if (stat & USBHS_USBSTS_TI0) { // timer 0 - used for built-in port events
  285. //println("timer0");
  286. if (port_state == PORT_STATE_DEBOUNCE) {
  287. port_state = PORT_STATE_RESET;
  288. // Since we have only 1 port, no other device can
  289. // be in reset or enumeration. If multiple ports
  290. // are ever supported, we would need to remain in
  291. // debounce if any other port was resetting or
  292. // enumerating a device.
  293. USBHS_PORTSC1 |= USBHS_PORTSC_PR; // begin reset sequence
  294. println(" begin reset");
  295. } else if (port_state == PORT_STATE_RECOVERY) {
  296. port_state = PORT_STATE_ACTIVE;
  297. println(" end recovery");
  298. // HCSPARAMS TTCTRL page 1671
  299. uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
  300. rootdev = new_Device(speed, 0, 0);
  301. }
  302. }
  303. if (stat & USBHS_USBSTS_TI1) { // timer 1 - used for USBDriverTimer
  304. //println("timer1");
  305. USBDriverTimer *timer = active_timers;
  306. if (timer) {
  307. USBDriverTimer *next = timer->next;
  308. active_timers = next;
  309. if (next) {
  310. // more timers scheduled
  311. next->prev = NULL;
  312. USBHS_GPTIMER1LD = next->usec - 1;
  313. USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  314. }
  315. // TODO: call multiple timers if 0 elapsed between them?
  316. timer->driver->timer_event(timer); // call driver's timer()
  317. }
  318. }
  319. }
  320. void USBDriverTimer::start(uint32_t microseconds)
  321. {
  322. #ifdef USBHOST_PRINT_DEBUG
  323. Serial.print("start_timer, us = ");
  324. Serial.print(microseconds);
  325. Serial.print(", driver = ");
  326. Serial.print((uint32_t)driver, HEX);
  327. Serial.print(", this = ");
  328. Serial.println((uint32_t)this, HEX);
  329. #endif
  330. if (!driver) return;
  331. if (microseconds < 100) return; // minimum timer duration
  332. started_micros = micros();
  333. if (active_timers == NULL) {
  334. // schedule is empty, just add this timer
  335. usec = microseconds;
  336. next = NULL;
  337. prev = NULL;
  338. active_timers = this;
  339. USBHS_GPTIMER1LD = microseconds - 1;
  340. USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  341. return;
  342. }
  343. uint32_t remain = USBHS_GPTIMER1CTL & 0xFFFFFF;
  344. //Serial.print("remain = ");
  345. //Serial.println(remain);
  346. if (microseconds < remain) {
  347. // this timer event is before any on the schedule
  348. __disable_irq();
  349. USBHS_GPTIMER1CTL = 0;
  350. USBHS_USBSTS = USBHS_USBSTS_TI1; // TODO: UPI & UAI safety?!
  351. usec = microseconds;
  352. next = active_timers;
  353. prev = NULL;
  354. active_timers->usec = remain - microseconds;
  355. active_timers->prev = this;
  356. active_timers = this;
  357. USBHS_GPTIMER1LD = microseconds - 1;
  358. USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  359. __enable_irq();
  360. return;
  361. }
  362. // add this timer to the schedule, somewhere after the first timer
  363. microseconds -= remain;
  364. USBDriverTimer *list = active_timers;
  365. while (list->next) {
  366. list = list->next;
  367. if (microseconds < list->usec) {
  368. // add timer into middle of list
  369. list->usec -= microseconds;
  370. usec = microseconds;
  371. next = list;
  372. prev = list->prev;
  373. list->prev = this;
  374. prev->next = this;
  375. return;
  376. }
  377. microseconds -= list->usec;
  378. }
  379. // add timer to the end of the schedule
  380. usec = microseconds;
  381. next = NULL;
  382. prev = list;
  383. list->next = this;
  384. }
  385. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  386. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  387. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  388. {
  389. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  390. (max_packet_length << 16) | (head_of_list << 15) |
  391. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  392. (inactivate << 7) | (address << 0) );
  393. }
  394. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  395. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  396. {
  397. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  398. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  399. }
  400. // Create a new pipe. It's QH is added to the async or periodic schedule,
  401. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  402. // dev: device owning this pipe/endpoint
  403. // type: 0=control, 2=bulk, 3=interrupt
  404. // endpoint: 0 for control, 1-15 for bulk or interrupt
  405. // direction: 0=OUT, 1=IN (unused for control)
  406. // maxlen: maximum packet size
  407. // interval: polling interval for interrupt, power of 2, unused if control or bulk
  408. //
  409. Pipe_t * USBHost::new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  410. uint32_t direction, uint32_t maxlen, uint32_t interval)
  411. {
  412. Pipe_t *pipe;
  413. Transfer_t *halt;
  414. uint32_t c=0, dtc=0;
  415. println("new_Pipe");
  416. pipe = allocate_Pipe();
  417. if (!pipe) return NULL;
  418. halt = allocate_Transfer();
  419. if (!halt) {
  420. free_Pipe(pipe);
  421. return NULL;
  422. }
  423. memset(pipe, 0, sizeof(Pipe_t));
  424. memset(halt, 0, sizeof(Transfer_t));
  425. halt->qtd.next = 1;
  426. halt->qtd.token = 0x40;
  427. pipe->device = dev;
  428. pipe->qh.next = (uint32_t)halt;
  429. pipe->qh.alt_next = 1;
  430. pipe->direction = direction;
  431. pipe->type = type;
  432. if (type == 3) {
  433. // interrupt transfers require bandwidth & microframe scheduling
  434. if (!allocate_interrupt_pipe_bandwidth(pipe, maxlen, interval)) {
  435. free_Transfer(halt);
  436. free_Pipe(pipe);
  437. return NULL;
  438. }
  439. }
  440. if (endpoint > 0) {
  441. // if non-control pipe, update dev->data_pipes list
  442. Pipe_t *p = dev->data_pipes;
  443. if (p == NULL) {
  444. dev->data_pipes = pipe;
  445. } else {
  446. while (p->next) p = p->next;
  447. p->next = pipe;
  448. }
  449. }
  450. if (type == 0) {
  451. // control
  452. if (dev->speed < 2) c = 1;
  453. dtc = 1;
  454. } else if (type == 2) {
  455. // bulk
  456. } else if (type == 3) {
  457. // interrupt
  458. //pipe->qh.token = 0x80000000; // TODO: OUT starts with DATA0 or DATA1?
  459. }
  460. pipe->qh.capabilities[0] = QH_capabilities1(15, c, maxlen, 0,
  461. dtc, dev->speed, endpoint, 0, dev->address);
  462. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  463. dev->hub_address, pipe->complete_mask, pipe->start_mask);
  464. if (type == 0 || type == 2) {
  465. // control or bulk: add to async queue
  466. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  467. if (list == NULL) {
  468. pipe->qh.capabilities[0] |= 0x8000; // H bit
  469. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  470. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  471. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  472. //println(" first in async list");
  473. } else {
  474. // EHCI 1.0: section 4.8.1, page 72
  475. pipe->qh.horizontal_link = list->qh.horizontal_link;
  476. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  477. //println(" added to async list");
  478. }
  479. } else if (type == 3) {
  480. // interrupt: add to periodic schedule
  481. add_qh_to_periodic_schedule(pipe);
  482. }
  483. return pipe;
  484. }
  485. // Fill in the qTD fields (token & data)
  486. // t the Transfer qTD to initialize
  487. // buf data to transfer
  488. // len length of data
  489. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  490. // data01 value of DATA0/DATA1 toggle on 1st packet
  491. // irq whether to generate an interrupt when transfer complete
  492. //
  493. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  494. uint32_t pid, uint32_t data01, bool irq)
  495. {
  496. t->qtd.alt_next = 1; // 1=terminate
  497. if (data01) data01 = 0x80000000;
  498. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  499. uint32_t addr = (uint32_t)buf;
  500. t->qtd.buffer[0] = addr;
  501. addr &= 0xFFFFF000;
  502. t->qtd.buffer[1] = addr + 0x1000;
  503. t->qtd.buffer[2] = addr + 0x2000;
  504. t->qtd.buffer[3] = addr + 0x3000;
  505. t->qtd.buffer[4] = addr + 0x4000;
  506. }
  507. // Create a Control Transfer and queue it
  508. //
  509. bool USBHost::queue_Control_Transfer(Device_t *dev, setup_t *setup, void *buf, USBDriver *driver)
  510. {
  511. Transfer_t *transfer, *data, *status;
  512. uint32_t status_direction;
  513. println("new_Control_Transfer");
  514. if (setup->wLength > 16384) return false; // max 16K data for control
  515. transfer = allocate_Transfer();
  516. if (!transfer) {
  517. println(" error allocating setup transfer");
  518. return false;
  519. }
  520. status = allocate_Transfer();
  521. if (!status) {
  522. println(" error allocating status transfer");
  523. free_Transfer(transfer);
  524. return false;
  525. }
  526. if (setup->wLength > 0) {
  527. data = allocate_Transfer();
  528. if (!data) {
  529. println(" error allocating data transfer");
  530. free_Transfer(transfer);
  531. free_Transfer(status);
  532. return false;
  533. }
  534. uint32_t pid = (setup->bmRequestType & 0x80) ? 1 : 0;
  535. init_qTD(data, buf, setup->wLength, pid, 1, false);
  536. transfer->qtd.next = (uint32_t)data;
  537. data->qtd.next = (uint32_t)status;
  538. status_direction = pid ^ 1;
  539. } else {
  540. transfer->qtd.next = (uint32_t)status;
  541. status_direction = 1; // always IN, USB 2.0 page 226
  542. }
  543. //println("setup address ", (uint32_t)setup, HEX);
  544. init_qTD(transfer, setup, 8, 2, 0, false);
  545. init_qTD(status, NULL, 0, status_direction, 1, true);
  546. status->pipe = dev->control_pipe;
  547. status->buffer = buf;
  548. status->length = setup->wLength;
  549. status->setup.word1 = setup->word1;
  550. status->setup.word2 = setup->word2;
  551. status->driver = driver;
  552. status->qtd.next = 1;
  553. return queue_Transfer(dev->control_pipe, transfer);
  554. }
  555. // Create a Bulk or Interrupt Transfer and queue it
  556. //
  557. bool USBHost::queue_Data_Transfer(Pipe_t *pipe, void *buffer, uint32_t len, USBDriver *driver)
  558. {
  559. Transfer_t *transfer, *data, *next;
  560. uint8_t *p = (uint8_t *)buffer;
  561. uint32_t count;
  562. bool last = false;
  563. // TODO: option for zero length packet? Maybe in Pipe_t fields?
  564. println("new_Data_Transfer");
  565. // allocate qTDs
  566. transfer = allocate_Transfer();
  567. if (!transfer) return false;
  568. data = transfer;
  569. for (count=(len >> 14); count; count--) {
  570. next = allocate_Transfer();
  571. if (!next) {
  572. // free already-allocated qTDs
  573. while (1) {
  574. next = (Transfer_t *)transfer->qtd.next;
  575. free_Transfer(transfer);
  576. if (transfer == data) break;
  577. transfer = next;
  578. }
  579. return false;
  580. }
  581. data->qtd.next = (uint32_t)next;
  582. data = next;
  583. }
  584. // last qTD needs info for followup
  585. data->qtd.next = 1;
  586. data->pipe = pipe;
  587. data->buffer = buffer;
  588. data->length = len;
  589. data->setup.word1 = 0;
  590. data->setup.word2 = 0;
  591. data->driver = driver;
  592. // initialize all qTDs
  593. data = transfer;
  594. while (1) {
  595. uint32_t count = len;
  596. if (count > 16384) {
  597. count = 16384;
  598. } else {
  599. last = true;
  600. }
  601. init_qTD(data, p, count, pipe->direction, 0, last);
  602. if (last) break;
  603. p += count;
  604. len -= count;
  605. data = (Transfer_t *)(data->qtd.next);
  606. }
  607. return queue_Transfer(pipe, transfer);
  608. }
  609. bool USBHost::queue_Transfer(Pipe_t *pipe, Transfer_t *transfer)
  610. {
  611. // find halt qTD
  612. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  613. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  614. // transfer's token
  615. uint32_t token = transfer->qtd.token;
  616. // transfer becomes new halt qTD
  617. transfer->qtd.token = 0x40;
  618. // copy transfer non-token fields to halt
  619. halt->qtd.next = transfer->qtd.next;
  620. halt->qtd.alt_next = transfer->qtd.alt_next;
  621. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize memcpy, all
  622. halt->qtd.buffer[1] = transfer->qtd.buffer[1]; // fields except token
  623. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  624. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  625. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  626. halt->pipe = pipe;
  627. halt->buffer = transfer->buffer;
  628. halt->length = transfer->length;
  629. halt->setup = transfer->setup;
  630. halt->driver = transfer->driver;
  631. // find the last qTD we're adding
  632. Transfer_t *last = halt;
  633. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  634. // last points to transfer (which becomes new halt)
  635. last->qtd.next = (uint32_t)transfer;
  636. transfer->qtd.next = 1;
  637. // link all the new qTD by next_followup & prev_followup
  638. Transfer_t *prev = NULL;
  639. Transfer_t *p = halt;
  640. while (p->qtd.next != (uint32_t)transfer) {
  641. Transfer_t *next = (Transfer_t *)p->qtd.next;
  642. p->prev_followup = prev;
  643. p->next_followup = next;
  644. prev = p;
  645. p = next;
  646. }
  647. p->prev_followup = prev;
  648. p->next_followup = NULL;
  649. //print(halt, p);
  650. // add them to a followup list
  651. if (pipe->type == 0 || pipe->type == 2) {
  652. // control or bulk
  653. add_to_async_followup_list(halt, p);
  654. } else {
  655. // interrupt
  656. add_to_periodic_followup_list(halt, p);
  657. }
  658. // old halt becomes new transfer, this commits all new qTDs to QH
  659. halt->qtd.token = token;
  660. return true;
  661. }
  662. static bool followup_Transfer(Transfer_t *transfer)
  663. {
  664. //println(" Followup ", (uint32_t)transfer, HEX);
  665. if (!(transfer->qtd.token & 0x80)) {
  666. // TODO: check error status
  667. if (transfer->qtd.token & 0x8000) {
  668. // this transfer caused an interrupt
  669. if (transfer->pipe->callback_function) {
  670. // do the callback
  671. (*(transfer->pipe->callback_function))(transfer);
  672. }
  673. }
  674. // do callback function...
  675. //println(" completed");
  676. return true;
  677. }
  678. return false;
  679. }
  680. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  681. {
  682. last->next_followup = NULL; // always add to end of list
  683. if (async_followup_last == NULL) {
  684. first->prev_followup = NULL;
  685. async_followup_first = first;
  686. } else {
  687. first->prev_followup = async_followup_last;
  688. async_followup_last->next_followup = first;
  689. }
  690. async_followup_last = last;
  691. }
  692. static void remove_from_async_followup_list(Transfer_t *transfer)
  693. {
  694. Transfer_t *next = transfer->next_followup;
  695. Transfer_t *prev = transfer->prev_followup;
  696. if (prev) {
  697. prev->next_followup = next;
  698. } else {
  699. async_followup_first = next;
  700. }
  701. if (next) {
  702. next->prev_followup = prev;
  703. } else {
  704. async_followup_last = prev;
  705. }
  706. }
  707. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  708. {
  709. last->next_followup = NULL; // always add to end of list
  710. if (periodic_followup_last == NULL) {
  711. first->prev_followup = NULL;
  712. periodic_followup_first = first;
  713. } else {
  714. first->prev_followup = periodic_followup_last;
  715. periodic_followup_last->next_followup = first;
  716. }
  717. periodic_followup_last = last;
  718. }
  719. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  720. {
  721. Transfer_t *next = transfer->next_followup;
  722. Transfer_t *prev = transfer->prev_followup;
  723. if (prev) {
  724. prev->next_followup = next;
  725. } else {
  726. periodic_followup_first = next;
  727. }
  728. if (next) {
  729. next->prev_followup = prev;
  730. } else {
  731. periodic_followup_last = prev;
  732. }
  733. }
  734. static uint32_t max4(uint32_t n1, uint32_t n2, uint32_t n3, uint32_t n4)
  735. {
  736. if (n1 > n2) {
  737. // can't be n2
  738. if (n1 > n3) {
  739. // can't be n3
  740. if (n1 > n4) return n1;
  741. } else {
  742. // can't be n1
  743. if (n3 > n4) return n3;
  744. }
  745. } else {
  746. // can't be n1
  747. if (n2 > n3) {
  748. // can't be n3
  749. if (n2 > n4) return n2;
  750. } else {
  751. // can't be n2
  752. if (n3 > n4) return n3;
  753. }
  754. }
  755. return n4;
  756. }
  757. static uint32_t round_to_power_of_two(uint32_t n, uint32_t maxnum)
  758. {
  759. for (uint32_t pow2num=1; pow2num < maxnum; pow2num <<= 1) {
  760. if (n <= (pow2num | (pow2num >> 1))) return pow2num;
  761. }
  762. return maxnum;
  763. }
  764. // Allocate bandwidth for an interrupt pipe. Given the packet size
  765. // and other parameters, find the best place to schedule this pipe.
  766. // Returns true if enough bandwidth is available, and the best
  767. // frame offset, smask and cmask. Or returns false if no group
  768. // of microframes has enough bandwidth available.
  769. //
  770. // pipe:
  771. // device->speed [in] 0=full speed, 1=low speed, 2=high speed
  772. // direction [in] 0=OUT, 1=IN
  773. // start_mask [out] uframes to start transfer
  774. // complete_mask [out] uframes to complete transfer (FS & LS only)
  775. // periodic_interval [out] fream repeat level: 1, 2, 4, 8... PERIODIC_LIST_SIZE
  776. // periodic_offset [out] frame repeat offset: 0 to periodic_interval-1
  777. // maxlen: [in] maximum packet length
  778. // interval: [in] polling interval: LS+FS: frames, HS: 2^(n-1) uframes
  779. //
  780. bool USBHost::allocate_interrupt_pipe_bandwidth(Pipe_t *pipe, uint32_t maxlen, uint32_t interval)
  781. {
  782. println("allocate_interrupt_pipe_bandwidth");
  783. if (interval == 0) interval = 1;
  784. maxlen = (maxlen * 76459) >> 16; // worst case bit stuffing
  785. if (pipe->device->speed == 2) {
  786. // high speed 480 Mbit/sec
  787. println(" ep interval = ", interval);
  788. if (interval > 15) interval = 15;
  789. interval = 1 << (interval - 1);
  790. if (interval > PERIODIC_LIST_SIZE*8) interval = PERIODIC_LIST_SIZE*8;
  791. println(" interval = ", interval);
  792. uint32_t pinterval = interval >> 3;
  793. pipe->periodic_interval = (pinterval > 0) ? pinterval : 1;
  794. uint32_t stime = (55 + 32 + maxlen) >> 5; // time units: 32 bytes or 533 ns
  795. uint32_t best_offset = 0xFFFFFFFF;
  796. uint32_t best_bandwidth = 0xFFFFFFFF;
  797. for (uint32_t offset=0; offset < interval; offset++) {
  798. // for each possible uframe offset, find the worst uframe bandwidth
  799. uint32_t max_bandwidth = 0;
  800. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
  801. uint32_t bandwidth = uframe_bandwidth[i] + stime;
  802. if (bandwidth > max_bandwidth) max_bandwidth = bandwidth;
  803. }
  804. // remember which uframe offset is the best
  805. if (max_bandwidth < best_bandwidth) {
  806. best_bandwidth = max_bandwidth;
  807. best_offset = offset;
  808. }
  809. }
  810. print(" best_bandwidth = ");
  811. print(best_bandwidth);
  812. print(", at offset = ");
  813. println(best_offset);
  814. // a 125 us micro frame can fit 7500 bytes, or 234 of our 32-byte units
  815. // fail if the best found needs more than 80% (234 * 0.8) in any uframe
  816. if (best_bandwidth > 187) return false;
  817. for (uint32_t i=best_offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
  818. uframe_bandwidth[i] += stime;
  819. }
  820. if (interval == 1) {
  821. pipe->start_mask = 0xFF;
  822. } else if (interval == 2) {
  823. pipe->start_mask = 0x55 << (best_offset & 1);
  824. } else if (interval <= 4) {
  825. pipe->start_mask = 0x11 << (best_offset & 3);
  826. } else {
  827. pipe->start_mask = 0x01 << (best_offset & 7);
  828. }
  829. pipe->periodic_offset = best_offset >> 3;
  830. pipe->complete_mask = 0;
  831. } else {
  832. // full speed 12 Mbit/sec or low speed 1.5 Mbit/sec
  833. interval = round_to_power_of_two(interval, PERIODIC_LIST_SIZE);
  834. pipe->periodic_interval = interval;
  835. uint32_t stime, ctime;
  836. if (pipe->direction == 0) {
  837. // for OUT direction, SSPLIT will carry the data payload
  838. // TODO: how much time to SSPLIT & CSPLIT actually take?
  839. // they're not documented in 5.7 or 5.11.3.
  840. stime = (100 + 32 + maxlen) >> 5;
  841. ctime = (55 + 32) >> 5;
  842. } else {
  843. // for IN direction, data payload in CSPLIT
  844. stime = (40 + 32) >> 5;
  845. ctime = (70 + 32 + maxlen) >> 5;
  846. }
  847. // TODO: should we take Single-TT hubs into account, avoid
  848. // scheduling overlapping SSPLIT & CSPLIT to the same hub?
  849. // TODO: even if Multi-TT, do we need to worry about packing
  850. // too many into the same uframe?
  851. uint32_t best_shift = 0;
  852. uint32_t best_offset = 0xFFFFFFFF;
  853. uint32_t best_bandwidth = 0xFFFFFFFF;
  854. for (uint32_t offset=0; offset < interval; offset++) {
  855. // for each 1ms frame offset, compute the worst uframe usage
  856. uint32_t max_bandwidth = 0;
  857. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
  858. for (uint32_t j=0; j <= 3; j++) { // max 3 without FSTN
  859. // at each location, find worst uframe usage
  860. // for SSPLIT+CSPLITs
  861. uint32_t n = (i << 3) + j;
  862. uint32_t bw1 = uframe_bandwidth[n+0] + stime;
  863. uint32_t bw2 = uframe_bandwidth[n+2] + ctime;
  864. uint32_t bw3 = uframe_bandwidth[n+3] + ctime;
  865. uint32_t bw4 = uframe_bandwidth[n+4] + ctime;
  866. max_bandwidth = max4(bw1, bw2, bw3, bw4);
  867. // remember the best usage found
  868. if (max_bandwidth < best_bandwidth) {
  869. best_bandwidth = max_bandwidth;
  870. best_offset = i;
  871. best_shift = j;
  872. }
  873. }
  874. }
  875. }
  876. print(" best_bandwidth = ");
  877. println(best_bandwidth);
  878. print(", at offset = ");
  879. print(best_offset);
  880. print(", shift= ");
  881. println(best_shift);
  882. // a 125 us micro frame can fit 7500 bytes, or 234 of our 32-byte units
  883. // fail if the best found needs more than 80% (234 * 0.8) in any uframe
  884. if (best_bandwidth > 187) return false;
  885. for (uint32_t i=best_offset; i < PERIODIC_LIST_SIZE; i += interval) {
  886. uint32_t n = (i << 3) + best_shift;
  887. uframe_bandwidth[n+0] += stime;
  888. uframe_bandwidth[n+2] += ctime;
  889. uframe_bandwidth[n+3] += ctime;
  890. uframe_bandwidth[n+4] += ctime;
  891. }
  892. pipe->start_mask = 0x01 << best_shift;
  893. pipe->complete_mask = 0x1C << best_shift;
  894. pipe->periodic_offset = best_offset;
  895. }
  896. return true;
  897. }
  898. // put a new pipe into the periodic schedule tree
  899. // according to periodic_interval and periodic_offset
  900. //
  901. void USBHost::add_qh_to_periodic_schedule(Pipe_t *pipe)
  902. {
  903. // quick hack for testing, just put it into the first table entry
  904. println("add_qh_to_periodic_schedule:");
  905. #if 0
  906. pipe->qh.horizontal_link = periodictable[0];
  907. periodictable[0] = (uint32_t)&(pipe->qh) | 2; // 2=QH
  908. println("init periodictable with ", periodictable[0], HEX);
  909. #else
  910. uint32_t interval = pipe->periodic_interval;
  911. uint32_t offset = pipe->periodic_offset;
  912. println(" interval = ", interval);
  913. println(" offset = ", offset);
  914. // TODO: does this really make an inverted tree like EHCI figure 4-18, page 93
  915. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
  916. uint32_t num = periodictable[i];
  917. Pipe_t *node = (Pipe_t *)(num & 0xFFFFFFE0);
  918. if ((num & 1) || ((num & 6) == 2 && node->periodic_interval < interval)) {
  919. println(" add to slot ", i);
  920. pipe->qh.horizontal_link = num;
  921. periodictable[i] = (uint32_t)&(pipe->qh) | 2; // 2=QH
  922. } else {
  923. println(" traverse list ", i);
  924. // TODO: skip past iTD, siTD when/if we support isochronous
  925. while (node->periodic_interval >= interval) {
  926. if (node->qh.horizontal_link & 1) break;
  927. num = node->qh.horizontal_link;
  928. node = (Pipe_t *)(num & 0xFFFFFFE0);
  929. }
  930. pipe->qh.horizontal_link = num;
  931. node->qh.horizontal_link = (uint32_t)pipe | 2; // 2=QH
  932. }
  933. }
  934. #endif
  935. #if 1
  936. println("Periodic Schedule:");
  937. for (uint32_t i=0; i < PERIODIC_LIST_SIZE; i++) {
  938. if (i < 10) print(" ");
  939. print(i);
  940. print(": ");
  941. print_qh_list((Pipe_t *)(periodictable[i] & 0xFFFFFFE0));
  942. }
  943. #endif
  944. }
  945. void USBHost::delete_Pipe(Pipe_t *pipe)
  946. {
  947. println("delete_Pipe ", (uint32_t)pipe, HEX);
  948. // halt pipe, find and free all Transfer_t
  949. // EHCI 1.0, 4.8.2 page 72: "Software should first deactivate
  950. // all active qTDs, wait for the queue head to go inactive"
  951. //
  952. // http://www.spinics.net/lists/linux-usb/msg131607.html
  953. // http://www.spinics.net/lists/linux-usb/msg131936.html
  954. //
  955. // In practice it's not feasible to wait for an active QH to become
  956. // inactive before removing it, for several reasons. For one, the QH may
  957. // _never_ become inactive (if the endpoint NAKs indefinitely). For
  958. // another, the procedure given in the spec (deactivate the qTDs on the
  959. // queue) is racy, since the controller can perform a new overlay or
  960. // writeback at any time.
  961. bool isasync = (pipe->type == 0 || pipe->type == 2);
  962. if (isasync) {
  963. // find the next QH in the async schedule loop
  964. Pipe_t *next = (Pipe_t *)(pipe->qh.horizontal_link & 0xFFFFFFE0);
  965. if (next == pipe) {
  966. // removing the only QH, so just shut down the async schedule
  967. println(" shut down async schedule");
  968. USBHS_USBCMD &= ~USBHS_USBCMD_ASE; // disable async schedule
  969. while (USBHS_USBSTS & USBHS_USBSTS_AS) ; // busy loop wait
  970. USBHS_ASYNCLISTADDR = 0;
  971. } else {
  972. // find the previous QH in the async schedule loop
  973. println(" remove QH from async schedule");
  974. Pipe_t *prev = next;
  975. while (1) {
  976. Pipe_t *n = (Pipe_t *)(prev->qh.horizontal_link & 0xFFFFFFE0);
  977. if (n == pipe) break;
  978. prev = n;
  979. }
  980. // if removing the one with H bit, set another
  981. if (pipe->qh.capabilities[0] & 0x8000) {
  982. prev->qh.capabilities[0] |= 0x8000; // set H bit
  983. }
  984. // link the previous QH, we're no longer in the loop
  985. prev->qh.horizontal_link = pipe->qh.horizontal_link;
  986. // do the Async Advance Doorbell handshake to wait to be
  987. // sure the EHCI no longer references the removed QH
  988. USBHS_USBCMD |= USBHS_USBCMD_IAA;
  989. while (!(USBHS_USBSTS & USBHS_USBSTS_AAI)) ; // busy loop wait
  990. USBHS_USBSTS = USBHS_USBSTS_AAI;
  991. // TODO: does this write interfere UPI & UAI (bits 18 & 19) ??
  992. }
  993. // find & free all the transfers which completed
  994. Transfer_t *t = async_followup_first;
  995. while (t) {
  996. Transfer_t *next = t->next_followup;
  997. if (t->pipe == pipe) {
  998. remove_from_async_followup_list(t);
  999. free_Transfer(t);
  1000. }
  1001. t = next;
  1002. }
  1003. } else {
  1004. // remove from the periodic schedule
  1005. for (uint32_t i=0; i < PERIODIC_LIST_SIZE; i++) {
  1006. uint32_t num = periodictable[i];
  1007. if (num & 1) continue;
  1008. Pipe_t *node = (Pipe_t *)(num & 0xFFFFFFE0);
  1009. if (node == pipe) {
  1010. periodictable[i] = pipe->qh.horizontal_link;
  1011. continue;
  1012. }
  1013. Pipe_t *prev = node;
  1014. while (1) {
  1015. num = node->qh.horizontal_link;
  1016. if (num & 1) break;
  1017. node = (Pipe_t *)(num & 0xFFFFFFE0);
  1018. if (node == pipe) {
  1019. prev->qh.horizontal_link = node->qh.horizontal_link;
  1020. break;
  1021. }
  1022. prev = node;
  1023. }
  1024. }
  1025. // TODO: subtract bandwidth from uframe_bandwidth array
  1026. // find & free all the transfers which completed
  1027. Transfer_t *t = periodic_followup_first;
  1028. while (t) {
  1029. Transfer_t *next = t->next_followup;
  1030. if (t->pipe == pipe) {
  1031. remove_from_periodic_followup_list(t);
  1032. free_Transfer(t);
  1033. }
  1034. t = next;
  1035. }
  1036. }
  1037. //
  1038. // TODO: do we need to look at pipe->qh.current ??
  1039. //
  1040. // free all the transfers still attached to the QH
  1041. Transfer_t *tr = (Transfer_t *)(pipe->qh.next);
  1042. while ((uint32_t)tr & 0xFFFFFFE0) {
  1043. Transfer_t *next = (Transfer_t *)(tr->qtd.next);
  1044. free_Transfer(tr);
  1045. tr = next;
  1046. }
  1047. // hopefully we found everything...
  1048. free_Pipe(pipe);
  1049. }