You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1117 lines
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. Serial.print("start_timer, us = ");
  323. Serial.print(microseconds);
  324. Serial.print(", driver = ");
  325. Serial.print((uint32_t)driver, HEX);
  326. Serial.print(", this = ");
  327. Serial.println((uint32_t)this, HEX);
  328. if (!driver) return;
  329. if (microseconds < 100) return; // minimum timer duration
  330. started_micros = micros();
  331. if (active_timers == NULL) {
  332. // schedule is empty, just add this timer
  333. usec = microseconds;
  334. next = NULL;
  335. prev = NULL;
  336. active_timers = this;
  337. USBHS_GPTIMER1LD = microseconds - 1;
  338. USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  339. return;
  340. }
  341. uint32_t remain = USBHS_GPTIMER1CTL & 0xFFFFFF;
  342. //Serial.print("remain = ");
  343. //Serial.println(remain);
  344. if (microseconds < remain) {
  345. // this timer event is before any on the schedule
  346. __disable_irq();
  347. USBHS_GPTIMER1CTL = 0;
  348. USBHS_USBSTS = USBHS_USBSTS_TI1; // TODO: UPI & UAI safety?!
  349. usec = microseconds;
  350. next = active_timers;
  351. prev = NULL;
  352. active_timers->usec = remain - microseconds;
  353. active_timers->prev = this;
  354. active_timers = this;
  355. USBHS_GPTIMER1LD = microseconds - 1;
  356. USBHS_GPTIMER1CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  357. __enable_irq();
  358. return;
  359. }
  360. // add this timer to the schedule, somewhere after the first timer
  361. microseconds -= remain;
  362. USBDriverTimer *list = active_timers;
  363. while (list->next) {
  364. list = list->next;
  365. if (microseconds < list->usec) {
  366. // add timer into middle of list
  367. list->usec -= microseconds;
  368. usec = microseconds;
  369. next = list;
  370. prev = list->prev;
  371. list->prev = this;
  372. prev->next = this;
  373. return;
  374. }
  375. microseconds -= list->usec;
  376. }
  377. // add timer to the end of the schedule
  378. usec = microseconds;
  379. next = NULL;
  380. prev = list;
  381. list->next = this;
  382. }
  383. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  384. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  385. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  386. {
  387. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  388. (max_packet_length << 16) | (head_of_list << 15) |
  389. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  390. (inactivate << 7) | (address << 0) );
  391. }
  392. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  393. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  394. {
  395. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  396. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  397. }
  398. // Create a new pipe. It's QH is added to the async or periodic schedule,
  399. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  400. // dev: device owning this pipe/endpoint
  401. // type: 0=control, 2=bulk, 3=interrupt
  402. // endpoint: 0 for control, 1-15 for bulk or interrupt
  403. // direction: 0=OUT, 1=IN (unused for control)
  404. // maxlen: maximum packet size
  405. // interval: polling interval for interrupt, power of 2, unused if control or bulk
  406. //
  407. Pipe_t * USBHost::new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  408. uint32_t direction, uint32_t maxlen, uint32_t interval)
  409. {
  410. Pipe_t *pipe;
  411. Transfer_t *halt;
  412. uint32_t c=0, dtc=0;
  413. println("new_Pipe");
  414. pipe = allocate_Pipe();
  415. if (!pipe) return NULL;
  416. halt = allocate_Transfer();
  417. if (!halt) {
  418. free_Pipe(pipe);
  419. return NULL;
  420. }
  421. memset(pipe, 0, sizeof(Pipe_t));
  422. memset(halt, 0, sizeof(Transfer_t));
  423. halt->qtd.next = 1;
  424. halt->qtd.token = 0x40;
  425. pipe->device = dev;
  426. pipe->qh.next = (uint32_t)halt;
  427. pipe->qh.alt_next = 1;
  428. pipe->direction = direction;
  429. pipe->type = type;
  430. if (type == 3) {
  431. // interrupt transfers require bandwidth & microframe scheduling
  432. if (!allocate_interrupt_pipe_bandwidth(pipe, maxlen, interval)) {
  433. free_Transfer(halt);
  434. free_Pipe(pipe);
  435. return NULL;
  436. }
  437. }
  438. if (endpoint > 0) {
  439. // if non-control pipe, update dev->data_pipes list
  440. Pipe_t *p = dev->data_pipes;
  441. if (p == NULL) {
  442. dev->data_pipes = pipe;
  443. } else {
  444. while (p->next) p = p->next;
  445. p->next = pipe;
  446. }
  447. }
  448. if (type == 0) {
  449. // control
  450. if (dev->speed < 2) c = 1;
  451. dtc = 1;
  452. } else if (type == 2) {
  453. // bulk
  454. } else if (type == 3) {
  455. // interrupt
  456. //pipe->qh.token = 0x80000000; // TODO: OUT starts with DATA0 or DATA1?
  457. }
  458. pipe->qh.capabilities[0] = QH_capabilities1(15, c, maxlen, 0,
  459. dtc, dev->speed, endpoint, 0, dev->address);
  460. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  461. dev->hub_address, pipe->complete_mask, pipe->start_mask);
  462. if (type == 0 || type == 2) {
  463. // control or bulk: add to async queue
  464. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  465. if (list == NULL) {
  466. pipe->qh.capabilities[0] |= 0x8000; // H bit
  467. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  468. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  469. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  470. //println(" first in async list");
  471. } else {
  472. // EHCI 1.0: section 4.8.1, page 72
  473. pipe->qh.horizontal_link = list->qh.horizontal_link;
  474. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  475. //println(" added to async list");
  476. }
  477. } else if (type == 3) {
  478. // interrupt: add to periodic schedule
  479. add_qh_to_periodic_schedule(pipe);
  480. }
  481. return pipe;
  482. }
  483. // Fill in the qTD fields (token & data)
  484. // t the Transfer qTD to initialize
  485. // buf data to transfer
  486. // len length of data
  487. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  488. // data01 value of DATA0/DATA1 toggle on 1st packet
  489. // irq whether to generate an interrupt when transfer complete
  490. //
  491. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  492. uint32_t pid, uint32_t data01, bool irq)
  493. {
  494. t->qtd.alt_next = 1; // 1=terminate
  495. if (data01) data01 = 0x80000000;
  496. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  497. uint32_t addr = (uint32_t)buf;
  498. t->qtd.buffer[0] = addr;
  499. addr &= 0xFFFFF000;
  500. t->qtd.buffer[1] = addr + 0x1000;
  501. t->qtd.buffer[2] = addr + 0x2000;
  502. t->qtd.buffer[3] = addr + 0x3000;
  503. t->qtd.buffer[4] = addr + 0x4000;
  504. }
  505. // Create a Control Transfer and queue it
  506. //
  507. bool USBHost::queue_Control_Transfer(Device_t *dev, setup_t *setup, void *buf, USBDriver *driver)
  508. {
  509. Transfer_t *transfer, *data, *status;
  510. uint32_t status_direction;
  511. println("new_Control_Transfer");
  512. if (setup->wLength > 16384) return false; // max 16K data for control
  513. transfer = allocate_Transfer();
  514. if (!transfer) {
  515. println(" error allocating setup transfer");
  516. return false;
  517. }
  518. status = allocate_Transfer();
  519. if (!status) {
  520. println(" error allocating status transfer");
  521. free_Transfer(transfer);
  522. return false;
  523. }
  524. if (setup->wLength > 0) {
  525. data = allocate_Transfer();
  526. if (!data) {
  527. println(" error allocating data transfer");
  528. free_Transfer(transfer);
  529. free_Transfer(status);
  530. return false;
  531. }
  532. uint32_t pid = (setup->bmRequestType & 0x80) ? 1 : 0;
  533. init_qTD(data, buf, setup->wLength, pid, 1, false);
  534. transfer->qtd.next = (uint32_t)data;
  535. data->qtd.next = (uint32_t)status;
  536. status_direction = pid ^ 1;
  537. } else {
  538. transfer->qtd.next = (uint32_t)status;
  539. status_direction = 1; // always IN, USB 2.0 page 226
  540. }
  541. //println("setup address ", (uint32_t)setup, HEX);
  542. init_qTD(transfer, setup, 8, 2, 0, false);
  543. init_qTD(status, NULL, 0, status_direction, 1, true);
  544. status->pipe = dev->control_pipe;
  545. status->buffer = buf;
  546. status->length = setup->wLength;
  547. status->setup.word1 = setup->word1;
  548. status->setup.word2 = setup->word2;
  549. status->driver = driver;
  550. status->qtd.next = 1;
  551. return queue_Transfer(dev->control_pipe, transfer);
  552. }
  553. // Create a Bulk or Interrupt Transfer and queue it
  554. //
  555. bool USBHost::queue_Data_Transfer(Pipe_t *pipe, void *buffer, uint32_t len, USBDriver *driver)
  556. {
  557. Transfer_t *transfer, *data, *next;
  558. uint8_t *p = (uint8_t *)buffer;
  559. uint32_t count;
  560. bool last = false;
  561. // TODO: option for zero length packet? Maybe in Pipe_t fields?
  562. println("new_Data_Transfer");
  563. // allocate qTDs
  564. transfer = allocate_Transfer();
  565. if (!transfer) return false;
  566. data = transfer;
  567. for (count=(len >> 14); count; count--) {
  568. next = allocate_Transfer();
  569. if (!next) {
  570. // free already-allocated qTDs
  571. while (1) {
  572. next = (Transfer_t *)transfer->qtd.next;
  573. free_Transfer(transfer);
  574. if (transfer == data) break;
  575. transfer = next;
  576. }
  577. return false;
  578. }
  579. data->qtd.next = (uint32_t)next;
  580. data = next;
  581. }
  582. // last qTD needs info for followup
  583. data->qtd.next = 1;
  584. data->pipe = pipe;
  585. data->buffer = buffer;
  586. data->length = len;
  587. data->setup.word1 = 0;
  588. data->setup.word2 = 0;
  589. data->driver = driver;
  590. // initialize all qTDs
  591. data = transfer;
  592. while (1) {
  593. uint32_t count = len;
  594. if (count > 16384) {
  595. count = 16384;
  596. } else {
  597. last = true;
  598. }
  599. init_qTD(data, p, count, pipe->direction, 0, last);
  600. if (last) break;
  601. p += count;
  602. len -= count;
  603. data = (Transfer_t *)(data->qtd.next);
  604. }
  605. return queue_Transfer(pipe, transfer);
  606. }
  607. bool USBHost::queue_Transfer(Pipe_t *pipe, Transfer_t *transfer)
  608. {
  609. // find halt qTD
  610. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  611. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  612. // transfer's token
  613. uint32_t token = transfer->qtd.token;
  614. // transfer becomes new halt qTD
  615. transfer->qtd.token = 0x40;
  616. // copy transfer non-token fields to halt
  617. halt->qtd.next = transfer->qtd.next;
  618. halt->qtd.alt_next = transfer->qtd.alt_next;
  619. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize memcpy, all
  620. halt->qtd.buffer[1] = transfer->qtd.buffer[1]; // fields except token
  621. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  622. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  623. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  624. halt->pipe = pipe;
  625. halt->buffer = transfer->buffer;
  626. halt->length = transfer->length;
  627. halt->setup = transfer->setup;
  628. halt->driver = transfer->driver;
  629. // find the last qTD we're adding
  630. Transfer_t *last = halt;
  631. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  632. // last points to transfer (which becomes new halt)
  633. last->qtd.next = (uint32_t)transfer;
  634. transfer->qtd.next = 1;
  635. // link all the new qTD by next_followup & prev_followup
  636. Transfer_t *prev = NULL;
  637. Transfer_t *p = halt;
  638. while (p->qtd.next != (uint32_t)transfer) {
  639. Transfer_t *next = (Transfer_t *)p->qtd.next;
  640. p->prev_followup = prev;
  641. p->next_followup = next;
  642. prev = p;
  643. p = next;
  644. }
  645. p->prev_followup = prev;
  646. p->next_followup = NULL;
  647. //print(halt, p);
  648. // add them to a followup list
  649. if (pipe->type == 0 || pipe->type == 2) {
  650. // control or bulk
  651. add_to_async_followup_list(halt, p);
  652. } else {
  653. // interrupt
  654. add_to_periodic_followup_list(halt, p);
  655. }
  656. // old halt becomes new transfer, this commits all new qTDs to QH
  657. halt->qtd.token = token;
  658. return true;
  659. }
  660. static bool followup_Transfer(Transfer_t *transfer)
  661. {
  662. //println(" Followup ", (uint32_t)transfer, HEX);
  663. if (!(transfer->qtd.token & 0x80)) {
  664. // TODO: check error status
  665. if (transfer->qtd.token & 0x8000) {
  666. // this transfer caused an interrupt
  667. if (transfer->pipe->callback_function) {
  668. // do the callback
  669. (*(transfer->pipe->callback_function))(transfer);
  670. }
  671. }
  672. // do callback function...
  673. //println(" completed");
  674. return true;
  675. }
  676. return false;
  677. }
  678. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  679. {
  680. last->next_followup = NULL; // always add to end of list
  681. if (async_followup_last == NULL) {
  682. first->prev_followup = NULL;
  683. async_followup_first = first;
  684. } else {
  685. first->prev_followup = async_followup_last;
  686. async_followup_last->next_followup = first;
  687. }
  688. async_followup_last = last;
  689. }
  690. static void remove_from_async_followup_list(Transfer_t *transfer)
  691. {
  692. Transfer_t *next = transfer->next_followup;
  693. Transfer_t *prev = transfer->prev_followup;
  694. if (prev) {
  695. prev->next_followup = next;
  696. } else {
  697. async_followup_first = next;
  698. }
  699. if (next) {
  700. next->prev_followup = prev;
  701. } else {
  702. async_followup_last = prev;
  703. }
  704. }
  705. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  706. {
  707. last->next_followup = NULL; // always add to end of list
  708. if (periodic_followup_last == NULL) {
  709. first->prev_followup = NULL;
  710. periodic_followup_first = first;
  711. } else {
  712. first->prev_followup = periodic_followup_last;
  713. periodic_followup_last->next_followup = first;
  714. }
  715. periodic_followup_last = last;
  716. }
  717. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  718. {
  719. Transfer_t *next = transfer->next_followup;
  720. Transfer_t *prev = transfer->prev_followup;
  721. if (prev) {
  722. prev->next_followup = next;
  723. } else {
  724. periodic_followup_first = next;
  725. }
  726. if (next) {
  727. next->prev_followup = prev;
  728. } else {
  729. periodic_followup_last = prev;
  730. }
  731. }
  732. static uint32_t max4(uint32_t n1, uint32_t n2, uint32_t n3, uint32_t n4)
  733. {
  734. if (n1 > n2) {
  735. // can't be n2
  736. if (n1 > n3) {
  737. // can't be n3
  738. if (n1 > n4) return n1;
  739. } else {
  740. // can't be n1
  741. if (n3 > n4) return n3;
  742. }
  743. } else {
  744. // can't be n1
  745. if (n2 > n3) {
  746. // can't be n3
  747. if (n2 > n4) return n2;
  748. } else {
  749. // can't be n2
  750. if (n3 > n4) return n3;
  751. }
  752. }
  753. return n4;
  754. }
  755. static uint32_t round_to_power_of_two(uint32_t n, uint32_t maxnum)
  756. {
  757. for (uint32_t pow2num=1; pow2num < maxnum; pow2num <<= 1) {
  758. if (n <= (pow2num | (pow2num >> 1))) return pow2num;
  759. }
  760. return maxnum;
  761. }
  762. // Allocate bandwidth for an interrupt pipe. Given the packet size
  763. // and other parameters, find the best place to schedule this pipe.
  764. // Returns true if enough bandwidth is available, and the best
  765. // frame offset, smask and cmask. Or returns false if no group
  766. // of microframes has enough bandwidth available.
  767. //
  768. // pipe:
  769. // device->speed [in] 0=full speed, 1=low speed, 2=high speed
  770. // direction [in] 0=OUT, 1=IN
  771. // start_mask [out] uframes to start transfer
  772. // complete_mask [out] uframes to complete transfer (FS & LS only)
  773. // periodic_interval [out] fream repeat level: 1, 2, 4, 8... PERIODIC_LIST_SIZE
  774. // periodic_offset [out] frame repeat offset: 0 to periodic_interval-1
  775. // maxlen: [in] maximum packet length
  776. // interval: [in] polling interval: LS+FS: frames, HS: 2^(n-1) uframes
  777. //
  778. bool USBHost::allocate_interrupt_pipe_bandwidth(Pipe_t *pipe, uint32_t maxlen, uint32_t interval)
  779. {
  780. println("allocate_interrupt_pipe_bandwidth");
  781. if (interval == 0) interval = 1;
  782. maxlen = (maxlen * 76459) >> 16; // worst case bit stuffing
  783. if (pipe->device->speed == 2) {
  784. // high speed 480 Mbit/sec
  785. println(" ep interval = ", interval);
  786. if (interval > 15) interval = 15;
  787. interval = 1 << (interval - 1);
  788. if (interval > PERIODIC_LIST_SIZE*8) interval = PERIODIC_LIST_SIZE*8;
  789. println(" interval = ", interval);
  790. uint32_t pinterval = interval >> 3;
  791. pipe->periodic_interval = (pinterval > 0) ? pinterval : 1;
  792. uint32_t stime = (55 + 32 + maxlen) >> 5; // time units: 32 bytes or 533 ns
  793. uint32_t best_offset = 0xFFFFFFFF;
  794. uint32_t best_bandwidth = 0xFFFFFFFF;
  795. for (uint32_t offset=0; offset < interval; offset++) {
  796. // for each possible uframe offset, find the worst uframe bandwidth
  797. uint32_t max_bandwidth = 0;
  798. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
  799. uint32_t bandwidth = uframe_bandwidth[i] + stime;
  800. if (bandwidth > max_bandwidth) max_bandwidth = bandwidth;
  801. }
  802. // remember which uframe offset is the best
  803. if (max_bandwidth < best_bandwidth) {
  804. best_bandwidth = max_bandwidth;
  805. best_offset = offset;
  806. }
  807. }
  808. print(" best_bandwidth = ");
  809. print(best_bandwidth);
  810. print(", at offset = ");
  811. println(best_offset);
  812. // a 125 us micro frame can fit 7500 bytes, or 234 of our 32-byte units
  813. // fail if the best found needs more than 80% (234 * 0.8) in any uframe
  814. if (best_bandwidth > 187) return false;
  815. for (uint32_t i=best_offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
  816. uframe_bandwidth[i] += stime;
  817. }
  818. if (interval == 1) {
  819. pipe->start_mask = 0xFF;
  820. } else if (interval == 2) {
  821. pipe->start_mask = 0x55 << (best_offset & 1);
  822. } else if (interval <= 4) {
  823. pipe->start_mask = 0x11 << (best_offset & 3);
  824. } else {
  825. pipe->start_mask = 0x01 << (best_offset & 7);
  826. }
  827. pipe->periodic_offset = best_offset >> 3;
  828. pipe->complete_mask = 0;
  829. } else {
  830. // full speed 12 Mbit/sec or low speed 1.5 Mbit/sec
  831. interval = round_to_power_of_two(interval, PERIODIC_LIST_SIZE);
  832. pipe->periodic_interval = interval;
  833. uint32_t stime, ctime;
  834. if (pipe->direction == 0) {
  835. // for OUT direction, SSPLIT will carry the data payload
  836. // TODO: how much time to SSPLIT & CSPLIT actually take?
  837. // they're not documented in 5.7 or 5.11.3.
  838. stime = (100 + 32 + maxlen) >> 5;
  839. ctime = (55 + 32) >> 5;
  840. } else {
  841. // for IN direction, data payload in CSPLIT
  842. stime = (40 + 32) >> 5;
  843. ctime = (70 + 32 + maxlen) >> 5;
  844. }
  845. // TODO: should we take Single-TT hubs into account, avoid
  846. // scheduling overlapping SSPLIT & CSPLIT to the same hub?
  847. // TODO: even if Multi-TT, do we need to worry about packing
  848. // too many into the same uframe?
  849. uint32_t best_shift = 0;
  850. uint32_t best_offset = 0xFFFFFFFF;
  851. uint32_t best_bandwidth = 0xFFFFFFFF;
  852. for (uint32_t offset=0; offset < interval; offset++) {
  853. // for each 1ms frame offset, compute the worst uframe usage
  854. uint32_t max_bandwidth = 0;
  855. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
  856. for (uint32_t j=0; j <= 3; j++) { // max 3 without FSTN
  857. // at each location, find worst uframe usage
  858. // for SSPLIT+CSPLITs
  859. uint32_t n = (i << 3) + j;
  860. uint32_t bw1 = uframe_bandwidth[n+0] + stime;
  861. uint32_t bw2 = uframe_bandwidth[n+2] + ctime;
  862. uint32_t bw3 = uframe_bandwidth[n+3] + ctime;
  863. uint32_t bw4 = uframe_bandwidth[n+4] + ctime;
  864. max_bandwidth = max4(bw1, bw2, bw3, bw4);
  865. // remember the best usage found
  866. if (max_bandwidth < best_bandwidth) {
  867. best_bandwidth = max_bandwidth;
  868. best_offset = i;
  869. best_shift = j;
  870. }
  871. }
  872. }
  873. }
  874. print(" best_bandwidth = ");
  875. println(best_bandwidth);
  876. print(", at offset = ");
  877. print(best_offset);
  878. print(", shift= ");
  879. println(best_shift);
  880. // a 125 us micro frame can fit 7500 bytes, or 234 of our 32-byte units
  881. // fail if the best found needs more than 80% (234 * 0.8) in any uframe
  882. if (best_bandwidth > 187) return false;
  883. for (uint32_t i=best_offset; i < PERIODIC_LIST_SIZE; i += interval) {
  884. uint32_t n = (i << 3) + best_shift;
  885. uframe_bandwidth[n+0] += stime;
  886. uframe_bandwidth[n+2] += ctime;
  887. uframe_bandwidth[n+3] += ctime;
  888. uframe_bandwidth[n+4] += ctime;
  889. }
  890. pipe->start_mask = 0x01 << best_shift;
  891. pipe->complete_mask = 0x1C << best_shift;
  892. pipe->periodic_offset = best_offset;
  893. }
  894. return true;
  895. }
  896. // put a new pipe into the periodic schedule tree
  897. // according to periodic_interval and periodic_offset
  898. //
  899. void USBHost::add_qh_to_periodic_schedule(Pipe_t *pipe)
  900. {
  901. // quick hack for testing, just put it into the first table entry
  902. println("add_qh_to_periodic_schedule:");
  903. #if 0
  904. pipe->qh.horizontal_link = periodictable[0];
  905. periodictable[0] = (uint32_t)&(pipe->qh) | 2; // 2=QH
  906. println("init periodictable with ", periodictable[0], HEX);
  907. #else
  908. uint32_t interval = pipe->periodic_interval;
  909. uint32_t offset = pipe->periodic_offset;
  910. println(" interval = ", interval);
  911. println(" offset = ", offset);
  912. // TODO: does this really make an inverted tree like EHCI figure 4-18, page 93
  913. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
  914. uint32_t num = periodictable[i];
  915. Pipe_t *node = (Pipe_t *)(num & 0xFFFFFFE0);
  916. if ((num & 1) || ((num & 6) == 2 && node->periodic_interval < interval)) {
  917. println(" add to slot ", i);
  918. pipe->qh.horizontal_link = num;
  919. periodictable[i] = (uint32_t)&(pipe->qh) | 2; // 2=QH
  920. } else {
  921. println(" traverse list ", i);
  922. // TODO: skip past iTD, siTD when/if we support isochronous
  923. while (node->periodic_interval >= interval) {
  924. if (node->qh.horizontal_link & 1) break;
  925. num = node->qh.horizontal_link;
  926. node = (Pipe_t *)(num & 0xFFFFFFE0);
  927. }
  928. pipe->qh.horizontal_link = num;
  929. node->qh.horizontal_link = (uint32_t)pipe | 2; // 2=QH
  930. }
  931. }
  932. #endif
  933. #if 1
  934. println("Periodic Schedule:");
  935. for (uint32_t i=0; i < PERIODIC_LIST_SIZE; i++) {
  936. if (i < 10) print(" ");
  937. print(i);
  938. print(": ");
  939. print_qh_list((Pipe_t *)(periodictable[i] & 0xFFFFFFE0));
  940. }
  941. #endif
  942. }
  943. void USBHost::delete_Pipe(Pipe_t *pipe)
  944. {
  945. println("delete_Pipe ", (uint32_t)pipe, HEX);
  946. // halt pipe, find and free all Transfer_t
  947. // EHCI 1.0, 4.8.2 page 72: "Software should first deactivate
  948. // all active qTDs, wait for the queue head to go inactive"
  949. //
  950. // http://www.spinics.net/lists/linux-usb/msg131607.html
  951. // http://www.spinics.net/lists/linux-usb/msg131936.html
  952. //
  953. // In practice it's not feasible to wait for an active QH to become
  954. // inactive before removing it, for several reasons. For one, the QH may
  955. // _never_ become inactive (if the endpoint NAKs indefinitely). For
  956. // another, the procedure given in the spec (deactivate the qTDs on the
  957. // queue) is racy, since the controller can perform a new overlay or
  958. // writeback at any time.
  959. bool isasync = (pipe->type == 0 || pipe->type == 2);
  960. if (isasync) {
  961. // find the next QH in the async schedule loop
  962. Pipe_t *next = (Pipe_t *)(pipe->qh.horizontal_link & 0xFFFFFFE0);
  963. if (next == pipe) {
  964. // removing the only QH, so just shut down the async schedule
  965. println(" shut down async schedule");
  966. USBHS_USBCMD &= ~USBHS_USBCMD_ASE; // disable async schedule
  967. while (USBHS_USBSTS & USBHS_USBSTS_AS) ; // busy loop wait
  968. USBHS_ASYNCLISTADDR = 0;
  969. } else {
  970. // find the previous QH in the async schedule loop
  971. println(" remove QH from async schedule");
  972. Pipe_t *prev = next;
  973. while (1) {
  974. Pipe_t *n = (Pipe_t *)(prev->qh.horizontal_link & 0xFFFFFFE0);
  975. if (n == pipe) break;
  976. prev = n;
  977. }
  978. // if removing the one with H bit, set another
  979. if (pipe->qh.capabilities[0] & 0x8000) {
  980. prev->qh.capabilities[0] |= 0x8000; // set H bit
  981. }
  982. // link the previous QH, we're no longer in the loop
  983. prev->qh.horizontal_link = pipe->qh.horizontal_link;
  984. // do the Async Advance Doorbell handshake to wait to be
  985. // sure the EHCI no longer references the removed QH
  986. USBHS_USBCMD |= USBHS_USBCMD_IAA;
  987. while (!(USBHS_USBSTS & USBHS_USBSTS_AAI)) ; // busy loop wait
  988. USBHS_USBSTS = USBHS_USBSTS_AAI;
  989. // TODO: does this write interfere UPI & UAI (bits 18 & 19) ??
  990. }
  991. // find & free all the transfers which completed
  992. Transfer_t *t = async_followup_first;
  993. while (t) {
  994. Transfer_t *next = t->next_followup;
  995. if (t->pipe == pipe) {
  996. remove_from_async_followup_list(t);
  997. free_Transfer(t);
  998. }
  999. t = next;
  1000. }
  1001. } else {
  1002. // remove from the periodic schedule
  1003. for (uint32_t i=0; i < PERIODIC_LIST_SIZE; i++) {
  1004. uint32_t num = periodictable[i];
  1005. if (num & 1) continue;
  1006. Pipe_t *node = (Pipe_t *)(num & 0xFFFFFFE0);
  1007. if (node == pipe) {
  1008. periodictable[i] = pipe->qh.horizontal_link;
  1009. continue;
  1010. }
  1011. Pipe_t *prev = node;
  1012. while (1) {
  1013. num = node->qh.horizontal_link;
  1014. if (num & 1) break;
  1015. node = (Pipe_t *)(num & 0xFFFFFFE0);
  1016. if (node == pipe) {
  1017. prev->qh.horizontal_link = node->qh.horizontal_link;
  1018. break;
  1019. }
  1020. prev = node;
  1021. }
  1022. }
  1023. // TODO: subtract bandwidth from uframe_bandwidth array
  1024. // find & free all the transfers which completed
  1025. Transfer_t *t = periodic_followup_first;
  1026. while (t) {
  1027. Transfer_t *next = t->next_followup;
  1028. if (t->pipe == pipe) {
  1029. remove_from_periodic_followup_list(t);
  1030. free_Transfer(t);
  1031. }
  1032. t = next;
  1033. }
  1034. }
  1035. //
  1036. // TODO: do we need to look at pipe->qh.current ??
  1037. //
  1038. // free all the transfers still attached to the QH
  1039. Transfer_t *tr = (Transfer_t *)(pipe->qh.next);
  1040. while ((uint32_t)tr & 0xFFFFFFE0) {
  1041. Transfer_t *next = (Transfer_t *)(tr->qtd.next);
  1042. free_Transfer(tr);
  1043. tr = next;
  1044. }
  1045. // hopefully we found everything...
  1046. free_Pipe(pipe);
  1047. }