Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ehci.cpp 36KB

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