ehcihc.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. ehcihc.c
  9. Abstract:
  10. This module implements support for the EHCI USB 2.0 Host Controller.
  11. Author:
  12. Evan Green 18-Mar-2013
  13. Environment:
  14. Kernel
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <minoca/kernel/driver.h>
  20. #include <minoca/fw/acpitabs.h>
  21. #include <minoca/usb/usbhost.h>
  22. #include "ehci.h"
  23. //
  24. // --------------------------------------------------------------------- Macros
  25. //
  26. //
  27. // These macros read from and write to a EHCI host controller register.
  28. //
  29. #define EHCI_READ_REGISTER(_Controller, _Register) \
  30. HlReadRegister32((_Controller)->RegisterBase + _Register)
  31. #define EHCI_WRITE_REGISTER(_Controller, _Register, _Value) \
  32. HlWriteRegister32((_Controller)->RegisterBase + _Register, _Value);
  33. #define EHCI_READ_PORT_REGISTER(_Controller, _PortIndex) \
  34. EHCI_READ_REGISTER( \
  35. _Controller, \
  36. EhciRegisterPortStatusBase + ((_PortIndex) * sizeof(ULONG)))
  37. #define EHCI_WRITE_PORT_REGISTER(_Controller, _PortIndex, _Value) \
  38. EHCI_WRITE_REGISTER( \
  39. _Controller, \
  40. EhciRegisterPortStatusBase + ((_PortIndex) * sizeof(ULONG)), \
  41. _Value)
  42. //
  43. // ---------------------------------------------------------------- Definitions
  44. //
  45. //
  46. // Define values to convert between frames and microframes.
  47. //
  48. #define EHCI_MICROFRAMES_PER_FRAME 8
  49. #define EHCI_MICROFRAMES_PER_FRAME_SHIFT 3
  50. //
  51. // Define EHCI debug flags.
  52. //
  53. #define EHCI_DEBUG_PORTS 0x00000001
  54. #define EHCI_DEBUG_TRANSFERS 0x00000002
  55. #define EHCI_DEBUG_ERRORS 0x00000004
  56. //
  57. // Define the timeout value for the endpoint flush operation.
  58. //
  59. #define EHCI_ENDPOINT_FLUSH_TIMEOUT 10
  60. //
  61. // Define the timeout value for the polled I/O operations.
  62. //
  63. #define EHCI_POLLED_TRANSFER_TIMEOUT 10
  64. //
  65. // ------------------------------------------------------ Data Type Definitions
  66. //
  67. //
  68. // ----------------------------------------------- Internal Function Prototypes
  69. //
  70. KSTATUS
  71. EhcipCreateEndpoint (
  72. PVOID HostControllerContext,
  73. PUSB_HOST_ENDPOINT_CREATION_REQUEST Endpoint,
  74. PVOID *EndpointContext
  75. );
  76. VOID
  77. EhcipResetEndpoint (
  78. PVOID HostControllerContext,
  79. PVOID EndpointContext,
  80. ULONG MaxPacketSize
  81. );
  82. KSTATUS
  83. EhcipFlushEndpoint (
  84. PVOID HostControllerContext,
  85. PVOID EndpointContext,
  86. PULONG TransferCount
  87. );
  88. VOID
  89. EhcipDestroyEndpoint (
  90. PVOID HostControllerContext,
  91. PVOID EndpointContext
  92. );
  93. KSTATUS
  94. EhcipCreateTransfer (
  95. PVOID HostControllerContext,
  96. PVOID EndpointContext,
  97. ULONG MaxBufferSize,
  98. ULONG Flags,
  99. PVOID *TransferContext
  100. );
  101. VOID
  102. EhcipDestroyTransfer (
  103. PVOID HostControllerContext,
  104. PVOID EndpointContext,
  105. PVOID TransferContext
  106. );
  107. KSTATUS
  108. EhcipSubmitTransfer (
  109. PVOID HostControllerContext,
  110. PVOID EndpointContext,
  111. PUSB_TRANSFER_INTERNAL Transfer,
  112. PVOID TransferContext
  113. );
  114. KSTATUS
  115. EhcipSubmitPolledTransfer (
  116. PVOID HostControllerContext,
  117. PVOID EndpointContext,
  118. PUSB_TRANSFER_INTERNAL Transfer,
  119. PVOID TransferContext
  120. );
  121. KSTATUS
  122. EhcipSubmitTransferSet (
  123. PEHCI_CONTROLLER Controller,
  124. PEHCI_ENDPOINT Endpoint,
  125. PEHCI_TRANSFER_SET TransferSet,
  126. PULONG SubmittedTransferCount,
  127. BOOL LockRequired
  128. );
  129. KSTATUS
  130. EhcipCancelTransfer (
  131. PVOID HostControllerContext,
  132. PVOID EndpointContext,
  133. PUSB_TRANSFER_INTERNAL Transfer,
  134. PVOID TransferContext
  135. );
  136. KSTATUS
  137. EhcipGetRootHubStatus (
  138. PVOID HostControllerContext,
  139. PUSB_HUB_STATUS HubStatus
  140. );
  141. KSTATUS
  142. EhcipSetRootHubStatus (
  143. PVOID HostControllerContext,
  144. PUSB_HUB_STATUS NewStatus
  145. );
  146. RUNLEVEL
  147. EhcipAcquireControllerLock (
  148. PEHCI_CONTROLLER Controller
  149. );
  150. VOID
  151. EhcipReleaseControllerLock (
  152. PEHCI_CONTROLLER Controller,
  153. RUNLEVEL OldRunLevel
  154. );
  155. VOID
  156. EhcipProcessInterrupt (
  157. PEHCI_CONTROLLER Controller,
  158. ULONG PendingStatusBits
  159. );
  160. VOID
  161. EhcipFillOutTransferDescriptor (
  162. PEHCI_CONTROLLER Controller,
  163. PEHCI_TRANSFER EhciTransfer,
  164. ULONG Offset,
  165. ULONG Length,
  166. BOOL LastTransfer,
  167. PBOOL DataToggle,
  168. PEHCI_TRANSFER AlternateNextTransfer
  169. );
  170. VOID
  171. EhcipLinkTransferSetInHardware (
  172. PEHCI_TRANSFER_SET TransferSet
  173. );
  174. BOOL
  175. EhcipProcessPotentiallyCompletedTransfer (
  176. PEHCI_TRANSFER Transfer
  177. );
  178. VOID
  179. EhcipRemoveCompletedTransferSet (
  180. PEHCI_CONTROLLER Controller,
  181. PEHCI_TRANSFER_SET TransferSet
  182. );
  183. VOID
  184. EhcipProcessAsyncOnAdvanceInterrupt (
  185. PEHCI_CONTROLLER Controller
  186. );
  187. VOID
  188. EhcipRemoveCancelledTransferSet (
  189. PEHCI_CONTROLLER Controller,
  190. PEHCI_TRANSFER_SET TransferSet
  191. );
  192. VOID
  193. EhcipDestroyQueuesWorkRoutine (
  194. PVOID Parameter
  195. );
  196. //
  197. // -------------------------------------------------------------------- Globals
  198. //
  199. //
  200. // Define a bitfield of debug flags that enable various print messages for
  201. // EHCI. See EHCI_DEBUG_* definitions.
  202. //
  203. ULONG EhciDebugFlags = 0x0;
  204. //
  205. // ------------------------------------------------------------------ Functions
  206. //
  207. PEHCI_CONTROLLER
  208. EhcipInitializeControllerState (
  209. PVOID OperationalRegisterBase,
  210. PHYSICAL_ADDRESS RegisterBasePhysical,
  211. ULONG PortCount,
  212. PDEBUG_USB_HANDOFF_DATA HandoffData
  213. )
  214. /*++
  215. Routine Description:
  216. This routine initializes the state and variables needed to start up an EHCI
  217. host controller.
  218. Arguments:
  219. OperationalRegisterBase - Supplies the virtual address of the base of the
  220. operational registers.
  221. RegisterBasePhysical - Supplies the physical address of the base of the
  222. EHCI registers (not the operational registers).
  223. PortCount - Supplies the number of ports on the EHCI controller.
  224. HandoffData - Supplies an optional pointer to the debug handoff data if the
  225. kernel debugger is using this controller.
  226. Return Value:
  227. Returns a pointer to the EHCI controller state object on success.
  228. NULL on failure.
  229. --*/
  230. {
  231. ULONG BlockSize;
  232. PEHCI_CONTROLLER Controller;
  233. PEHCI_DEBUG_HANDOFF_DATA EhciHandoff;
  234. ULONG Flags;
  235. ULONG Frame;
  236. ULONG FrameBits;
  237. ULONG IoBufferFlags;
  238. PEHCI_TRANSFER_QUEUE PreviousTransferQueue;
  239. PEHCI_QUEUE_HEAD QueueHead;
  240. PHYSICAL_ADDRESS QueueHeadPhysicalAddress;
  241. ULONG RemainingSize;
  242. KSTATUS Status;
  243. PEHCI_TRANSFER_QUEUE TransferQueue;
  244. ULONG TreeLevel;
  245. ASSERT(PortCount != 0);
  246. EhciHandoff = NULL;
  247. if (HandoffData != NULL) {
  248. ASSERT(HandoffData->HostDataSize == sizeof(EHCI_DEBUG_HANDOFF_DATA));
  249. EhciHandoff = HandoffData->HostData;
  250. }
  251. //
  252. // Allocate the controller structure itself.
  253. //
  254. Controller = MmAllocateNonPagedPool(sizeof(EHCI_CONTROLLER),
  255. EHCI_ALLOCATION_TAG);
  256. if (Controller == NULL) {
  257. Status = STATUS_INSUFFICIENT_RESOURCES;
  258. goto InitializeControllerStateEnd;
  259. }
  260. RtlZeroMemory(Controller, sizeof(EHCI_CONTROLLER));
  261. INITIALIZE_LIST_HEAD(&(Controller->IsochronousTransferListHead));
  262. INITIALIZE_LIST_HEAD(&(Controller->TransferListHead));
  263. INITIALIZE_LIST_HEAD(&(Controller->AsyncOnAdvanceReadyListHead));
  264. INITIALIZE_LIST_HEAD(&(Controller->AsyncOnAdvancePendingListHead));
  265. INITIALIZE_LIST_HEAD(&(Controller->QueuesToDestroyListHead));
  266. INITIALIZE_LIST_HEAD(&(Controller->EndpointListHead));
  267. Controller->RegisterBase = OperationalRegisterBase;
  268. Controller->PhysicalBase = RegisterBasePhysical;
  269. Controller->UsbCoreHandle = INVALID_HANDLE;
  270. Controller->InterruptHandle = INVALID_HANDLE;
  271. Controller->PortCount = PortCount;
  272. Controller->HandoffData = EhciHandoff;
  273. KeInitializeSpinLock(&(Controller->Lock));
  274. Controller->DestroyQueuesWorkItem = KeCreateWorkItem(
  275. NULL,
  276. WorkPriorityNormal,
  277. EhcipDestroyQueuesWorkRoutine,
  278. Controller,
  279. EHCI_ALLOCATION_TAG);
  280. if (Controller->DestroyQueuesWorkItem == NULL) {
  281. Status = STATUS_INSUFFICIENT_RESOURCES;
  282. goto InitializeControllerStateEnd;
  283. }
  284. //
  285. // Allocate and initialize the buffer used to hold the EHCI schedule. Since
  286. // the controller never writes to the periodic schedule memory, just map
  287. // it cached and manage it carefully (rather than mapping the whole schedule
  288. // uncached).
  289. //
  290. IoBufferFlags = IO_BUFFER_FLAG_PHYSICALLY_CONTIGUOUS;
  291. Controller->PeriodicScheduleIoBuffer = MmAllocateNonPagedIoBuffer(
  292. 0,
  293. MAX_ULONG,
  294. EHCI_FRAME_LIST_ALIGNMENT,
  295. sizeof(EHCI_PERIODIC_SCHEDULE),
  296. IoBufferFlags);
  297. if (Controller->PeriodicScheduleIoBuffer == NULL) {
  298. Status = STATUS_INSUFFICIENT_RESOURCES;
  299. goto InitializeControllerStateEnd;
  300. }
  301. ASSERT(Controller->PeriodicScheduleIoBuffer->FragmentCount == 1);
  302. ASSERT(Controller->PeriodicScheduleIoBuffer->Fragment[0].Size >=
  303. sizeof(EHCI_PERIODIC_SCHEDULE));
  304. Controller->PeriodicSchedule =
  305. Controller->PeriodicScheduleIoBuffer->Fragment[0].VirtualAddress;
  306. //
  307. // Create the block allocator used to allocate transfers and queues. The
  308. // block size is that of the larger structure.
  309. //
  310. ASSERT((EHCI_BLOCK_ALLOCATOR_ALIGNMENT & (~EHCI_LINK_ADDRESS_MASK)) == 0);
  311. if (sizeof(EHCI_TRANSFER_DESCRIPTOR) >= sizeof(EHCI_QUEUE_HEAD)) {
  312. BlockSize = sizeof(EHCI_TRANSFER_DESCRIPTOR);
  313. } else {
  314. BlockSize = sizeof(EHCI_QUEUE_HEAD);
  315. }
  316. Flags = BLOCK_ALLOCATOR_FLAG_NON_CACHED |
  317. BLOCK_ALLOCATOR_FLAG_PHYSICALLY_CONTIGUOUS;
  318. Controller->BlockAllocator = MmCreateBlockAllocator(
  319. BlockSize,
  320. EHCI_BLOCK_ALLOCATOR_ALIGNMENT,
  321. EHCI_BLOCK_ALLOCATOR_EXPANSION_COUNT,
  322. Flags,
  323. EHCI_BLOCK_ALLOCATION_TAG);
  324. if (Controller->BlockAllocator == NULL) {
  325. Status = STATUS_INSUFFICIENT_RESOURCES;
  326. goto InitializeControllerStateEnd;
  327. }
  328. //
  329. // Create the periodic schedule, which is a tree of empty queues. Interrupt
  330. // transfers can get different polling rates by inserting themselves at
  331. // different levels of the tree.
  332. //
  333. for (TreeLevel = 0;
  334. TreeLevel < EHCI_PERIODIC_SCHEDULE_TREE_DEPTH;
  335. TreeLevel += 1) {
  336. QueueHead = MmAllocateBlock(Controller->BlockAllocator,
  337. &QueueHeadPhysicalAddress);
  338. if (QueueHead == NULL) {
  339. Status = STATUS_INSUFFICIENT_RESOURCES;
  340. goto InitializeControllerStateEnd;
  341. }
  342. //
  343. // Initialize the transfer queue.
  344. //
  345. TransferQueue = &(Controller->InterruptTree[TreeLevel]);
  346. INITIALIZE_LIST_HEAD(&(TransferQueue->ListEntry));
  347. TransferQueue->HardwareQueueHead = QueueHead;
  348. TransferQueue->PhysicalAddress = QueueHeadPhysicalAddress;
  349. //
  350. // Initialize the queue head. This is non-cached memory, so don't
  351. // needlessly zero the structure. Be smart about it.
  352. //
  353. QueueHead->HorizontalLink = EHCI_LINK_TERMINATE;
  354. QueueHead->Destination = 0;
  355. QueueHead->SplitInformation = EHCI_QUEUE_1_TRANSACTION_PER_MICRO_FRAME;
  356. QueueHead->CurrentTransferDescriptorLink = 0;
  357. QueueHead->TransferOverlay.NextTransfer = EHCI_LINK_TERMINATE;
  358. QueueHead->TransferOverlay.AlternateNextTransfer = EHCI_LINK_TERMINATE;
  359. QueueHead->TransferOverlay.Token = EHCI_TRANSFER_STATUS_HALTED;
  360. RemainingSize = sizeof(EHCI_TRANSFER_DESCRIPTOR) -
  361. FIELD_OFFSET(EHCI_TRANSFER_DESCRIPTOR, BufferPointer);
  362. RtlZeroMemory(&(QueueHead->TransferOverlay.BufferPointer),
  363. RemainingSize);
  364. //
  365. // Unless this is the first (least often polled) queue, set the
  366. // previous queue to point at this more often polled queue.
  367. //
  368. if (TreeLevel != 0) {
  369. PreviousTransferQueue = &(Controller->InterruptTree[TreeLevel - 1]);
  370. ASSERT(((ULONG)QueueHeadPhysicalAddress &
  371. (~EHCI_LINK_ADDRESS_MASK)) == 0);
  372. PreviousTransferQueue->HardwareQueueHead->HorizontalLink =
  373. ((ULONG)QueueHeadPhysicalAddress &
  374. EHCI_LINK_ADDRESS_MASK) | EHCI_LINK_TYPE_QUEUE_HEAD;
  375. INSERT_AFTER(&(TransferQueue->ListEntry),
  376. &(PreviousTransferQueue->ListEntry));
  377. }
  378. }
  379. //
  380. // Initialize the array of frame list pointers for the periodic schedule
  381. // to point to the various levels of the tree with their respective
  382. // frequencies.
  383. //
  384. for (Frame = 0; Frame < EHCI_DEFAULT_FRAME_LIST_ENTRY_COUNT; Frame += 1) {
  385. FrameBits = Frame;
  386. TreeLevel = 0;
  387. //
  388. // Figure out how many zero bits are clear. For example, one in 8 times,
  389. // the first three bits are 0. Use that to determine which frequency
  390. // level to put for this frame.
  391. //
  392. while (((FrameBits & 0x1) == 0) &&
  393. (TreeLevel < EHCI_PERIODIC_SCHEDULE_TREE_DEPTH - 1)) {
  394. TreeLevel += 1;
  395. FrameBits = FrameBits >> 1;
  396. }
  397. //
  398. // The most common one is at the end, remember.
  399. //
  400. TreeLevel = EHCI_PERIODIC_SCHEDULE_TREE_DEPTH - 1 - TreeLevel;
  401. ASSERT(TreeLevel < EHCI_PERIODIC_SCHEDULE_TREE_DEPTH);
  402. TransferQueue = &(Controller->InterruptTree[TreeLevel]);
  403. Controller->PeriodicSchedule->FrameLink[Frame] =
  404. ((ULONG)TransferQueue->PhysicalAddress &
  405. EHCI_LINK_ADDRESS_MASK) |
  406. EHCI_LINK_TYPE_QUEUE_HEAD;
  407. }
  408. //
  409. // Clean the cache of the periodic schedule.
  410. //
  411. MmFlushBufferForDataOut(Controller->PeriodicSchedule,
  412. sizeof(EHCI_PERIODIC_SCHEDULE));
  413. //
  414. // Create an empty queue head for the asynchronous list.
  415. //
  416. QueueHead = MmAllocateBlock(Controller->BlockAllocator,
  417. &QueueHeadPhysicalAddress);
  418. if (QueueHead == NULL) {
  419. Status = STATUS_INSUFFICIENT_RESOURCES;
  420. goto InitializeControllerStateEnd;
  421. }
  422. //
  423. // Link the asynchronous schedule with this new queue head.
  424. //
  425. TransferQueue = &(Controller->AsynchronousSchedule);
  426. INITIALIZE_LIST_HEAD(&(TransferQueue->ListEntry));
  427. TransferQueue->HardwareQueueHead = QueueHead;
  428. TransferQueue->PhysicalAddress = QueueHeadPhysicalAddress;
  429. //
  430. // Initialize the queue head. Do not zero the whole thing, as every field
  431. // will be filled in below.
  432. //
  433. QueueHead->SplitInformation = EHCI_QUEUE_1_TRANSACTION_PER_MICRO_FRAME;
  434. QueueHead->CurrentTransferDescriptorLink = 0;
  435. QueueHead->TransferOverlay.NextTransfer = EHCI_LINK_TERMINATE;
  436. QueueHead->TransferOverlay.AlternateNextTransfer = EHCI_LINK_TERMINATE;
  437. QueueHead->TransferOverlay.Token = EHCI_TRANSFER_STATUS_HALTED;
  438. RemainingSize = sizeof(EHCI_TRANSFER_DESCRIPTOR) -
  439. FIELD_OFFSET(EHCI_TRANSFER_DESCRIPTOR, BufferPointer);
  440. RtlZeroMemory(&(QueueHead->TransferOverlay.BufferPointer),
  441. RemainingSize);
  442. //
  443. // Here's where things get interesting. If there's handoff data, then the
  444. // kernel debugger has set up two queue heads already, one is an empty
  445. // reclamation queue, and the other is an empty queue that's never used.
  446. // It inserts its own queue heads in between these two queues. The
  447. // handoff data contains the actual pointers to the queue heads the kernel
  448. // debugger uses, so they can be moved.
  449. //
  450. if (EhciHandoff != NULL) {
  451. //
  452. // Take down the kernel debug connection, as the controller's going to
  453. // be reset. The USB core driver will reconnect when it re-discovers
  454. // the debug device.
  455. //
  456. RtlDebugPrint("EHCI: Temporarily disconnecting kernel debugger "
  457. "while the controller is reinitialized.\n");
  458. KdDisconnect();
  459. //
  460. // Use the newly allocated queue to replace the end queue. This EHCI
  461. // driver will insert all its queue heads after this new end queue head,
  462. // as it appears to be the start of the asynchronous schedule. The
  463. // actual start of the schedule is the reclamation queue head in the
  464. // kernel debugger.
  465. //
  466. QueueHead->HorizontalLink = EhciHandoff->EndQueue->HorizontalLink;
  467. QueueHead->Destination = EhciHandoff->EndQueue->Destination;
  468. EhciHandoff->ReclamationQueue->HorizontalLink =
  469. QueueHeadPhysicalAddress | EHCI_LINK_TYPE_QUEUE_HEAD;
  470. //
  471. // Replace the end queue for the kernel debugger.
  472. //
  473. EhciHandoff->EndQueue = QueueHead;
  474. EhciHandoff->EndQueuePhysical = QueueHeadPhysicalAddress;
  475. //
  476. // There is no handoff data, so initialize the queue head to be the
  477. // reclamation queue head and the beginning of the asynchronous schedule.
  478. // Loop that queue to point back to itself.
  479. //
  480. } else {
  481. QueueHead->Destination = EHCI_QUEUE_RECLAMATION_HEAD;
  482. QueueHead->HorizontalLink = ((ULONG)QueueHeadPhysicalAddress &
  483. EHCI_LINK_ADDRESS_MASK) |
  484. EHCI_LINK_TYPE_QUEUE_HEAD;
  485. }
  486. Status = STATUS_SUCCESS;
  487. InitializeControllerStateEnd:
  488. if (!KSUCCESS(Status)) {
  489. if (Controller != NULL) {
  490. EhcipDestroyControllerState(Controller);
  491. Controller = NULL;
  492. }
  493. }
  494. return Controller;
  495. }
  496. VOID
  497. EhcipDestroyControllerState (
  498. PEHCI_CONTROLLER Controller
  499. )
  500. /*++
  501. Routine Description:
  502. This routine destroys the memory associated with an EHCI controller.
  503. Arguments:
  504. Controller - Supplies a pointer to the EHCI controller state to release.
  505. Return Value:
  506. None.
  507. --*/
  508. {
  509. ULONG TreeLevel;
  510. ASSERT(LIST_EMPTY(&(Controller->EndpointListHead)) != FALSE);
  511. if (Controller->DestroyQueuesWorkItem != NULL) {
  512. KeDestroyWorkItem(Controller->DestroyQueuesWorkItem);
  513. }
  514. if (Controller->PeriodicScheduleIoBuffer != NULL) {
  515. MmFreeIoBuffer(Controller->PeriodicScheduleIoBuffer);
  516. }
  517. for (TreeLevel = 0;
  518. TreeLevel < EHCI_PERIODIC_SCHEDULE_TREE_DEPTH;
  519. TreeLevel += 1) {
  520. if (Controller->InterruptTree[TreeLevel].HardwareQueueHead == NULL) {
  521. continue;
  522. }
  523. MmFreeBlock(Controller->BlockAllocator,
  524. Controller->InterruptTree[TreeLevel].HardwareQueueHead);
  525. Controller->InterruptTree[TreeLevel].HardwareQueueHead = NULL;
  526. }
  527. //
  528. // If there's handoff data, it's not great that the controller is going
  529. // down. Disconnect the debugger for safety.
  530. //
  531. if (Controller->HandoffData != NULL) {
  532. RtlDebugPrint("EHCI: Disconnecting kernel debugger as EHCI "
  533. "controller is being removed.\n");
  534. KdDisconnect();
  535. }
  536. if (Controller->AsynchronousSchedule.HardwareQueueHead != NULL) {
  537. MmFreeBlock(Controller->BlockAllocator,
  538. Controller->AsynchronousSchedule.HardwareQueueHead);
  539. Controller->AsynchronousSchedule.HardwareQueueHead = NULL;
  540. }
  541. if (Controller->BlockAllocator != NULL) {
  542. MmDestroyBlockAllocator(Controller->BlockAllocator);
  543. Controller->BlockAllocator = NULL;
  544. }
  545. ASSERT(LIST_EMPTY(&(Controller->IsochronousTransferListHead)) != FALSE);
  546. if (Controller->UsbCoreHandle != INVALID_HANDLE) {
  547. UsbHostDestroyControllerState(Controller->UsbCoreHandle);
  548. }
  549. MmFreeNonPagedPool(Controller);
  550. return;
  551. }
  552. KSTATUS
  553. EhcipRegisterController (
  554. PEHCI_CONTROLLER Controller,
  555. PDEVICE Device
  556. )
  557. /*++
  558. Routine Description:
  559. This routine registers the started EHCI controller with the core USB
  560. library.
  561. Arguments:
  562. Controller - Supplies a pointer to the EHCI controller state of the
  563. controller to register.
  564. Device - Supplies a pointer to the device object.
  565. Return Value:
  566. Status code.
  567. --*/
  568. {
  569. USB_HOST_CONTROLLER_INTERFACE Interface;
  570. KSTATUS Status;
  571. //
  572. // Fill out the functions that the USB core library will use to control
  573. // the EHCI controller.
  574. //
  575. RtlZeroMemory(&Interface, sizeof(USB_HOST_CONTROLLER_INTERFACE));
  576. Interface.Version = USB_HOST_CONTROLLER_INTERFACE_VERSION;
  577. Interface.DriverObject = EhciDriver;
  578. Interface.DeviceObject = Device;
  579. Interface.HostControllerContext = Controller;
  580. Interface.Speed = UsbDeviceSpeedHigh;
  581. Interface.Identifier = Controller->PhysicalBase;
  582. Interface.DebugPortSubType = DEBUG_PORT_USB_EHCI;
  583. Interface.RootHubPortCount = Controller->PortCount;
  584. Interface.CreateEndpoint = EhcipCreateEndpoint;
  585. Interface.ResetEndpoint = EhcipResetEndpoint;
  586. Interface.FlushEndpoint = EhcipFlushEndpoint;
  587. Interface.DestroyEndpoint = EhcipDestroyEndpoint;
  588. Interface.CreateTransfer = EhcipCreateTransfer;
  589. Interface.DestroyTransfer = EhcipDestroyTransfer;
  590. Interface.SubmitTransfer = EhcipSubmitTransfer;
  591. Interface.SubmitPolledTransfer = EhcipSubmitPolledTransfer;
  592. Interface.CancelTransfer = EhcipCancelTransfer;
  593. Interface.GetRootHubStatus = EhcipGetRootHubStatus;
  594. Interface.SetRootHubStatus = EhcipSetRootHubStatus;
  595. Status = UsbHostRegisterController(&Interface,
  596. &(Controller->UsbCoreHandle));
  597. if (!KSUCCESS(Status)) {
  598. goto RegisterControllerEnd;
  599. }
  600. RegisterControllerEnd:
  601. return Status;
  602. }
  603. VOID
  604. EhcipSetInterruptHandle (
  605. PEHCI_CONTROLLER Controller,
  606. HANDLE InterruptHandle
  607. )
  608. /*++
  609. Routine Description:
  610. This routine saves the handle of the connected interrupt in the EHCI
  611. controller.
  612. Arguments:
  613. Controller - Supplies a pointer to the EHCI controller state.
  614. InterruptHandle - Supplies the connected interrupt handle.
  615. Return Value:
  616. None.
  617. --*/
  618. {
  619. Controller->InterruptHandle = InterruptHandle;
  620. return;
  621. }
  622. KSTATUS
  623. EhcipResetController (
  624. PEHCI_CONTROLLER Controller
  625. )
  626. /*++
  627. Routine Description:
  628. This routine resets and starts the EHCI controller.
  629. Arguments:
  630. Controller - Supplies a pointer to the EHCI controller state of the
  631. controller to reset.
  632. Return Value:
  633. Status code.
  634. --*/
  635. {
  636. ULONG CommandRegister;
  637. ULONG InterruptRegister;
  638. PIO_BUFFER PeriodicIoBuffer;
  639. ULONG PhysicalAddress;
  640. ULONG PortIndex;
  641. ULONG PortStatusRegister;
  642. //
  643. // Reset the host controller and wait for the hardware to clear the bit,
  644. // which indicates that the reset is complete.
  645. //
  646. CommandRegister = EHCI_COMMAND_CONTROLLER_RESET;
  647. EHCI_WRITE_REGISTER(Controller, EhciRegisterUsbCommand, CommandRegister);
  648. do {
  649. //
  650. // AND in the hardware register to see if the bit has cleared.
  651. //
  652. CommandRegister &= EHCI_READ_REGISTER(Controller,
  653. EhciRegisterUsbCommand);
  654. } while (CommandRegister != 0);
  655. //
  656. // Clear the status register.
  657. //
  658. EHCI_WRITE_REGISTER(Controller, EhciRegisterUsbStatus, 0);
  659. //
  660. // Write the the segment selector to use the first 4GB of physical memory.
  661. //
  662. EHCI_WRITE_REGISTER(Controller, EhciRegisterSegmentSelector, 0);
  663. //
  664. // Enable all interrupts except the frame list rollover.
  665. //
  666. InterruptRegister = EHCI_INTERRUPT_ASYNC_ADVANCE |
  667. EHCI_INTERRUPT_HOST_SYSTEM_ERROR |
  668. EHCI_INTERRUPT_PORT_CHANGE |
  669. EHCI_INTERRUPT_USB_ERROR |
  670. EHCI_INTERRUPT_ENABLE;
  671. EHCI_WRITE_REGISTER(Controller,
  672. EhciRegisterUsbInterruptEnable,
  673. InterruptRegister);
  674. //
  675. // Set the periodic list base register to the physical address of the EHCI
  676. // periodic schedule.
  677. //
  678. PeriodicIoBuffer = Controller->PeriodicScheduleIoBuffer;
  679. PhysicalAddress = (ULONG)PeriodicIoBuffer->Fragment[0].PhysicalAddress;
  680. ASSERT(PhysicalAddress == PeriodicIoBuffer->Fragment[0].PhysicalAddress);
  681. EHCI_WRITE_REGISTER(Controller,
  682. EhciRegisterPeriodicListBase,
  683. PhysicalAddress);
  684. //
  685. // Write the asynchronous list base to the reclamation list head.
  686. //
  687. PhysicalAddress = (ULONG)Controller->AsynchronousSchedule.PhysicalAddress;
  688. ASSERT(PhysicalAddress == Controller->AsynchronousSchedule.PhysicalAddress);
  689. EHCI_WRITE_REGISTER(Controller,
  690. EhciRegisterAsynchronousListAddress,
  691. PhysicalAddress);
  692. //
  693. // Write to the command register to start the controller.
  694. //
  695. CommandRegister = EHCI_COMMAND_INTERRUPT_EVERY_8_UFRAMES |
  696. ECHI_COMMAND_ASYNC_PARK_ENABLE |
  697. (3 << EHCI_COMMAND_PARK_COUNT_SHIFT) |
  698. EHCI_COMMAND_ENABLE_ASYNC_SCHEDULE |
  699. EHCI_COMMAND_ENABLE_PERIODIC_SCHEDULE |
  700. EHCI_COMMAND_1024_FRAME_LIST_ENTRIES |
  701. EHCI_COMMAND_RUN;
  702. EHCI_WRITE_REGISTER(Controller, EhciRegisterUsbCommand, CommandRegister);
  703. Controller->CommandRegister = CommandRegister;
  704. //
  705. // Set the config flag, which switches all the ports to EHCI away from the
  706. // companion controllers.
  707. //
  708. EHCI_WRITE_REGISTER(Controller, EhciRegisterConfigured, 1);
  709. //
  710. // Fire up the ports.
  711. //
  712. for (PortIndex = 0; PortIndex < Controller->PortCount; PortIndex += 1) {
  713. PortStatusRegister = EHCI_READ_PORT_REGISTER(Controller, PortIndex);
  714. if ((PortStatusRegister & EHCI_PORT_POWER) == 0) {
  715. PortStatusRegister |= EHCI_PORT_POWER;
  716. EHCI_WRITE_PORT_REGISTER(Controller, PortIndex, PortStatusRegister);
  717. }
  718. }
  719. return STATUS_SUCCESS;
  720. }
  721. INTERRUPT_STATUS
  722. EhcipInterruptService (
  723. PVOID Context
  724. )
  725. /*++
  726. Routine Description:
  727. This routine implements the EHCI interrupt service routine.
  728. Arguments:
  729. Context - Supplies the context pointer given to the system when the
  730. interrupt was connected. In this case, this points to the EHCI
  731. controller.
  732. Return Value:
  733. Interrupt status.
  734. --*/
  735. {
  736. PEHCI_CONTROLLER Controller;
  737. INTERRUPT_STATUS InterruptStatus;
  738. USHORT UsbStatus;
  739. Controller = (PEHCI_CONTROLLER)Context;
  740. InterruptStatus = InterruptStatusNotClaimed;
  741. //
  742. // Read the status register. If it's non-zero, this is USB's interrupt.
  743. //
  744. UsbStatus = EHCI_READ_REGISTER(Controller, EhciRegisterUsbStatus) &
  745. EHCI_STATUS_INTERRUPT_MASK;
  746. if (UsbStatus != 0) {
  747. InterruptStatus = InterruptStatusClaimed;
  748. RtlAtomicOr32(&(Controller->PendingStatusBits), UsbStatus);
  749. //
  750. // Clear the bits in the status register to acknowledge the interrupt.
  751. //
  752. EHCI_WRITE_REGISTER(Controller, EhciRegisterUsbStatus, UsbStatus);
  753. }
  754. return InterruptStatus;
  755. }
  756. INTERRUPT_STATUS
  757. EhcipInterruptServiceDpc (
  758. PVOID Parameter
  759. )
  760. /*++
  761. Routine Description:
  762. This routine implements the EHCI dispatch level interrupt service.
  763. Arguments:
  764. Parameter - Supplies the context, in this case the EHCI controller
  765. structure.
  766. Return Value:
  767. None.
  768. --*/
  769. {
  770. PEHCI_CONTROLLER Controller;
  771. ULONG StatusBits;
  772. Controller = Parameter;
  773. StatusBits = RtlAtomicExchange32(&(Controller->PendingStatusBits), 0);
  774. if (StatusBits == 0) {
  775. return InterruptStatusNotClaimed;
  776. }
  777. EhcipProcessInterrupt(Controller, StatusBits);
  778. return InterruptStatusClaimed;
  779. }
  780. //
  781. // --------------------------------------------------------- Internal Functions
  782. //
  783. KSTATUS
  784. EhcipCreateEndpoint (
  785. PVOID HostControllerContext,
  786. PUSB_HOST_ENDPOINT_CREATION_REQUEST Endpoint,
  787. PVOID *EndpointContext
  788. )
  789. /*++
  790. Routine Description:
  791. This routine is called by the USB core when a new endpoint is being opened.
  792. It allows the host controller to create and store any context needed to
  793. support a new endpoint (such as a queue head).
  794. Arguments:
  795. HostControllerContext - Supplies the context pointer passed to the USB core
  796. when the controller was created. This is used to identify the USB host
  797. controller to the host controller driver.
  798. Endpoint - Supplies a pointer containing information about the endpoint
  799. being created. The host controller cannot count on this buffer sticking
  800. around after the function returns. If it needs this information it
  801. should make a copy of it.
  802. EndpointContext - Supplies a pointer where the host controller can store a
  803. context pointer identifying the endpoint created.
  804. Return Value:
  805. STATUS_SUCCESS if the endpoint can be successfully accommodated.
  806. Failing status code if the endpoint cannot be opened.
  807. --*/
  808. {
  809. ULONG ClosestRate;
  810. PEHCI_CONTROLLER Controller;
  811. ULONG Destination;
  812. PEHCI_TRANSFER DummyTransfer;
  813. ULONG EndMask;
  814. PEHCI_TRANSFER_DESCRIPTOR HardwareTransfer;
  815. PHYSICAL_ADDRESS HardwareTransferPhysicalAddress;
  816. ULONG InterruptTreeLevel;
  817. ULONG NakReloadCount;
  818. PEHCI_ENDPOINT NewEndpoint;
  819. PEHCI_TRANSFER_QUEUE NewQueue;
  820. RUNLEVEL OldRunLevel;
  821. PHYSICAL_ADDRESS PhysicalAddress;
  822. ULONG PollRate;
  823. PEHCI_TRANSFER_QUEUE QueueBefore;
  824. PEHCI_QUEUE_HEAD QueueHead;
  825. PHYSICAL_ADDRESS QueueHeadPhysicalAddress;
  826. ULONG RemainingSize;
  827. ULONG SplitInformation;
  828. ULONG StartMicroFrame;
  829. KSTATUS Status;
  830. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  831. NewEndpoint = MmAllocateNonPagedPool(sizeof(EHCI_ENDPOINT),
  832. EHCI_ALLOCATION_TAG);
  833. if (NewEndpoint == NULL) {
  834. Status = STATUS_INSUFFICIENT_RESOURCES;
  835. goto CreateEndpointEnd;
  836. }
  837. RtlZeroMemory(NewEndpoint, sizeof(EHCI_ENDPOINT));
  838. INITIALIZE_LIST_HEAD(&(NewEndpoint->TransferListHead));
  839. NewEndpoint->TransferType = Endpoint->Type;
  840. ASSERT((Endpoint->Speed == UsbDeviceSpeedLow) ||
  841. (Endpoint->Speed == UsbDeviceSpeedFull) ||
  842. (Endpoint->Speed == UsbDeviceSpeedHigh));
  843. NewEndpoint->Speed = Endpoint->Speed;
  844. ASSERT(Endpoint->MaxPacketSize != 0);
  845. NewEndpoint->MaxPacketSize = Endpoint->MaxPacketSize;
  846. NewEndpoint->EndpointNumber = Endpoint->EndpointNumber;
  847. NewEndpoint->PollRate = Endpoint->PollRate;
  848. //
  849. // If the endpoint is high speed, the units are in microframes. But EHCI
  850. // periodic schedules run in frames, so convert down (rounding up).
  851. //
  852. if (NewEndpoint->Speed == UsbDeviceSpeedHigh) {
  853. NewEndpoint->PollRate =
  854. ALIGN_RANGE_UP(NewEndpoint->PollRate, EHCI_MICROFRAMES_PER_FRAME) >>
  855. EHCI_MICROFRAMES_PER_FRAME_SHIFT;
  856. }
  857. //
  858. // For isochronous endpoints, that's all that is needed.
  859. //
  860. if (NewEndpoint->TransferType == UsbTransferTypeIsochronous) {
  861. Status = STATUS_SUCCESS;
  862. goto CreateEndpointEnd;
  863. }
  864. //
  865. // Create the hardware queue head.
  866. //
  867. QueueHead = MmAllocateBlock(Controller->BlockAllocator,
  868. &QueueHeadPhysicalAddress);
  869. if (QueueHead == NULL) {
  870. Status = STATUS_INSUFFICIENT_RESOURCES;
  871. goto CreateEndpointEnd;
  872. }
  873. RtlZeroMemory(QueueHead, sizeof(EHCI_QUEUE_HEAD));
  874. NewQueue = &(NewEndpoint->Queue);
  875. NewQueue->HardwareQueueHead = QueueHead;
  876. NewQueue->PhysicalAddress = QueueHeadPhysicalAddress;
  877. //
  878. // Set the NAK reload count to the maximum for control and bulk transfers.
  879. // Interrupt and isochronous transfers must have the NAK reload count set
  880. // to zero.
  881. //
  882. if ((NewEndpoint->TransferType == UsbTransferTypeControl) ||
  883. (NewEndpoint->TransferType == UsbTransferTypeBulk)) {
  884. NakReloadCount = EHCI_QUEUE_DEFAULT_NAK_RELOAD_COUNT;
  885. } else {
  886. NakReloadCount = 0;
  887. }
  888. //
  889. // Initialize the hardware queue entry. Notice one thing conspicuously
  890. // missing is the device address. This gets initialized to zero, and fixed
  891. // up during transfer submissions (when the device is potentially moved off
  892. // address zero).
  893. //
  894. Destination =
  895. (NakReloadCount << EHCI_QUEUE_NAK_RELOAD_COUNT_SHIFT) |
  896. ((NewEndpoint->MaxPacketSize << EHCI_QUEUE_MAX_PACKET_LENGTH_SHIFT) &
  897. EHCI_QUEUE_MAX_PACKET_LENGTH_MASK) |
  898. ((NewEndpoint->EndpointNumber & USB_ENDPOINT_ADDRESS_MASK) <<
  899. EHCI_QUEUE_ENDPOINT_SHIFT);
  900. switch (NewEndpoint->Speed) {
  901. case UsbDeviceSpeedLow:
  902. Destination |= EHCI_QUEUE_LOW_SPEED;
  903. break;
  904. case UsbDeviceSpeedFull:
  905. Destination |= EHCI_QUEUE_FULL_SPEED;
  906. break;
  907. case UsbDeviceSpeedHigh:
  908. Destination |= EHCI_QUEUE_HIGH_SPEED;
  909. break;
  910. default:
  911. ASSERT(FALSE);
  912. Status = STATUS_INVALID_PARAMETER;
  913. goto CreateEndpointEnd;
  914. }
  915. //
  916. // All control transfers handle the data toggle without hardware
  917. // assistance. Non-high speed control transfers must have the control
  918. // endpoint flag set. High speed control transfers should not have said
  919. // flag set.
  920. //
  921. if (NewEndpoint->TransferType == UsbTransferTypeControl) {
  922. Destination |= EHCI_QUEUE_USE_TRANSFER_DESCRIPTOR_DATA_TOGGLE;
  923. if (NewEndpoint->Speed != UsbDeviceSpeedHigh) {
  924. Destination |= EHCI_QUEUE_CONTROL_ENDPOINT;
  925. }
  926. }
  927. QueueHead->Destination = Destination;
  928. //
  929. // Set the split information in the hardware queue entry.
  930. //
  931. SplitInformation = EHCI_QUEUE_1_TRANSACTION_PER_MICRO_FRAME;
  932. if ((NewEndpoint->Speed == UsbDeviceSpeedLow) ||
  933. (NewEndpoint->Speed == UsbDeviceSpeedFull)) {
  934. ASSERT(Endpoint->HubAddress != 0);
  935. ASSERT(Endpoint->HubPortNumber != 0);
  936. SplitInformation |=
  937. ((Endpoint->HubPortNumber << EHCI_QUEUE_PORT_NUMBER_SHIFT) &
  938. EHCI_QUEUE_PORT_NUMBER_MASK) |
  939. ((Endpoint->HubAddress << EHCI_QUEUE_HUB_ADDRESS_SHIFT) &
  940. EHCI_QUEUE_HUB_ADDRESS_MASK);
  941. if (NewEndpoint->TransferType == UsbTransferTypeInterrupt) {
  942. //
  943. // Make a weak attempt at spreading out these transfers throughout
  944. // micro frames. Only start in 0-3, inclusive, to avoid dealing
  945. // with Frame Split Transaction Nodes.
  946. //
  947. // N.B. Interrupt transfer cancellation will need to change if the
  948. // above behavior is changed.
  949. //
  950. StartMicroFrame = Controller->EndpointCount & 0x3;
  951. //
  952. // Isochronous OUT endpoints don't use complete splits, but
  953. // interrupt and other endpoints usually skip a microframe and then
  954. // issue complete splits for the next three.
  955. //
  956. if ((Endpoint->Type == UsbTransferTypeIsochronous) &&
  957. (Endpoint->Direction == UsbTransferDirectionOut)) {
  958. EndMask = 0;
  959. } else {
  960. EndMask = (1 << (StartMicroFrame + 2)) |
  961. (1 << (StartMicroFrame + 3)) |
  962. (1 << (StartMicroFrame + 4));
  963. }
  964. SplitInformation |=
  965. ((EndMask << EHCI_QUEUE_SPLIT_COMPLETION_SHIFT) &
  966. EHCI_QUEUE_SPLIT_COMPLETION_MASK) |
  967. ((1 << StartMicroFrame) &
  968. EHCI_QUEUE_SPLIT_START_MASK);
  969. }
  970. } else {
  971. //
  972. // Make a weak attempt at spreading the transfers throughout micro-
  973. // frames.
  974. //
  975. if (NewEndpoint->TransferType == UsbTransferTypeInterrupt) {
  976. SplitInformation |= ((1 << (Controller->EndpointCount & 0x7)) &
  977. EHCI_QUEUE_SPLIT_START_MASK);
  978. }
  979. }
  980. QueueHead->SplitInformation = SplitInformation;
  981. //
  982. // Allocate an initial dummy transfer to point this queue at.
  983. //
  984. DummyTransfer = MmAllocateNonPagedPool(sizeof(EHCI_TRANSFER),
  985. EHCI_ALLOCATION_TAG);
  986. if (DummyTransfer == NULL) {
  987. Status = STATUS_INSUFFICIENT_RESOURCES;
  988. goto CreateEndpointEnd;
  989. }
  990. HardwareTransfer = MmAllocateBlock(Controller->BlockAllocator,
  991. &HardwareTransferPhysicalAddress);
  992. if (HardwareTransfer == NULL) {
  993. Status = STATUS_INSUFFICIENT_RESOURCES;
  994. goto CreateEndpointEnd;
  995. }
  996. RtlZeroMemory(DummyTransfer, sizeof(EHCI_TRANSFER));
  997. NewQueue->DummyTransfer = DummyTransfer;
  998. DummyTransfer->HardwareTransfer = HardwareTransfer;
  999. DummyTransfer->PhysicalAddress = HardwareTransferPhysicalAddress;
  1000. HardwareTransfer->NextTransfer = EHCI_LINK_TERMINATE;
  1001. HardwareTransfer->AlternateNextTransfer = EHCI_LINK_TERMINATE;
  1002. HardwareTransfer->Token = EHCI_TRANSFER_STATUS_HALTED;
  1003. RemainingSize = sizeof(EHCI_TRANSFER_DESCRIPTOR) -
  1004. FIELD_OFFSET(EHCI_TRANSFER_DESCRIPTOR, BufferPointer);
  1005. RtlZeroMemory(&(HardwareTransfer->BufferPointer),
  1006. RemainingSize);
  1007. //
  1008. // Point the queue at the dummy transfer.
  1009. //
  1010. QueueHead->TransferOverlay.NextTransfer =
  1011. (ULONG)HardwareTransferPhysicalAddress;
  1012. QueueHead->TransferOverlay.AlternateNextTransfer =
  1013. (ULONG)HardwareTransferPhysicalAddress;
  1014. //
  1015. // Figure out where to insert this queue. If it's an interrupt transfer,
  1016. // determine what level of the tree it belongs in based on the polling rate.
  1017. //
  1018. if (NewEndpoint->TransferType == UsbTransferTypeInterrupt) {
  1019. PollRate = NewEndpoint->PollRate;
  1020. if (PollRate > MAX_USHORT / 2) {
  1021. PollRate = MAX_USHORT / 2;
  1022. }
  1023. ClosestRate = 1;
  1024. while (ClosestRate < PollRate) {
  1025. ClosestRate <<= 1;
  1026. }
  1027. if (ClosestRate != PollRate) {
  1028. ClosestRate >>= 1;
  1029. }
  1030. ASSERT(ClosestRate != 0);
  1031. InterruptTreeLevel = EHCI_PERIODIC_SCHEDULE_TREE_DEPTH - 1;
  1032. while (((ClosestRate & 0x1) == 0) && (InterruptTreeLevel != 0)) {
  1033. ClosestRate >>= 1;
  1034. InterruptTreeLevel -= 1;
  1035. }
  1036. ASSERT(InterruptTreeLevel < EHCI_PERIODIC_SCHEDULE_TREE_DEPTH);
  1037. QueueBefore = &(Controller->InterruptTree[InterruptTreeLevel]);
  1038. NewEndpoint->PollRate = ClosestRate;
  1039. } else {
  1040. QueueBefore = &(Controller->AsynchronousSchedule);
  1041. }
  1042. //
  1043. // Insert the endpoint onto the global queue, both the software list and
  1044. // the hardware's singly linked list. Use register writes for memory that
  1045. // is potentially being actively observed by hardware.
  1046. //
  1047. OldRunLevel = EhcipAcquireControllerLock(Controller);
  1048. Controller->EndpointCount += 1;
  1049. INSERT_BEFORE(&(NewEndpoint->ListEntry), &(Controller->EndpointListHead));
  1050. INSERT_AFTER(&(NewQueue->ListEntry), &(QueueBefore->ListEntry));
  1051. QueueHead->HorizontalLink = QueueBefore->HardwareQueueHead->HorizontalLink;
  1052. ASSERT(((ULONG)NewQueue->PhysicalAddress & (~EHCI_LINK_ADDRESS_MASK)) == 0);
  1053. PhysicalAddress = NewQueue->PhysicalAddress | EHCI_LINK_TYPE_QUEUE_HEAD;
  1054. HlWriteRegister32(&(QueueBefore->HardwareQueueHead->HorizontalLink),
  1055. (ULONG)PhysicalAddress);
  1056. EhcipReleaseControllerLock(Controller, OldRunLevel);
  1057. Status = STATUS_SUCCESS;
  1058. CreateEndpointEnd:
  1059. if (!KSUCCESS(Status)) {
  1060. if (NewEndpoint != NULL) {
  1061. DummyTransfer = NewEndpoint->Queue.DummyTransfer;
  1062. if (DummyTransfer != NULL) {
  1063. if (DummyTransfer->HardwareTransfer != NULL) {
  1064. MmFreeBlock(Controller->BlockAllocator,
  1065. DummyTransfer->HardwareTransfer);
  1066. }
  1067. MmFreeNonPagedPool(DummyTransfer);
  1068. }
  1069. MmFreeNonPagedPool(NewEndpoint);
  1070. NewEndpoint = NULL;
  1071. }
  1072. }
  1073. *EndpointContext = NewEndpoint;
  1074. return Status;
  1075. }
  1076. VOID
  1077. EhcipResetEndpoint (
  1078. PVOID HostControllerContext,
  1079. PVOID EndpointContext,
  1080. ULONG MaxPacketSize
  1081. )
  1082. /*++
  1083. Routine Description:
  1084. This routine is called by the USB core when an endpoint needs to be reset.
  1085. Arguments:
  1086. HostControllerContext - Supplies the context pointer passed to the USB core
  1087. when the controller was created. This is used to identify the USB host
  1088. controller to the host controller driver.
  1089. EndpointContext - Supplies a pointer to the context returned by the host
  1090. controller when the endpoint was created.
  1091. MaxPacketSize - Supplies the maximum transfer size of the endpoint.
  1092. Return Value:
  1093. None.
  1094. --*/
  1095. {
  1096. PEHCI_ENDPOINT Endpoint;
  1097. PEHCI_QUEUE_HEAD HardwareQueueHead;
  1098. ULONG Token;
  1099. Endpoint = (PEHCI_ENDPOINT)EndpointContext;
  1100. //
  1101. // There better not be any active transfers running around during an
  1102. // endpoint reset.
  1103. //
  1104. ASSERT(LIST_EMPTY(&(Endpoint->TransferListHead)) != FALSE);
  1105. //
  1106. // If the max packet size changed, update the queue head.
  1107. //
  1108. HardwareQueueHead = Endpoint->Queue.HardwareQueueHead;
  1109. if (MaxPacketSize != Endpoint->MaxPacketSize) {
  1110. Endpoint->MaxPacketSize = MaxPacketSize;
  1111. HardwareQueueHead->Destination =
  1112. (HardwareQueueHead->Destination &
  1113. (~EHCI_QUEUE_MAX_PACKET_LENGTH_MASK)) |
  1114. ((MaxPacketSize << EHCI_QUEUE_MAX_PACKET_LENGTH_SHIFT) &
  1115. EHCI_QUEUE_MAX_PACKET_LENGTH_MASK);
  1116. }
  1117. //
  1118. // Reset the data toggle in the transfer overlay.
  1119. //
  1120. Token = HlReadRegister32(&(HardwareQueueHead->TransferOverlay.Token));
  1121. Token &= ~EHCI_TRANSFER_DATA_TOGGLE;
  1122. HlWriteRegister32(&(HardwareQueueHead->TransferOverlay.Token), Token);
  1123. return;
  1124. }
  1125. KSTATUS
  1126. EhcipFlushEndpoint (
  1127. PVOID HostControllerContext,
  1128. PVOID EndpointContext,
  1129. PULONG TransferCount
  1130. )
  1131. /*++
  1132. Routine Description:
  1133. This routine flushes all the active transfers from an endpoint. It does so
  1134. by polling for completion status and does not return until all transfers
  1135. are completed. This must be called at high run level.
  1136. Arguments:
  1137. HostControllerContext - Supplies the context pointer passed to the USB core
  1138. when the controller was created. This is used to identify the USB host
  1139. controller to the host controller driver.
  1140. EndpointContext - Supplies a pointer to the context returned by the host
  1141. controller when the endpoint was created.
  1142. TransferCount - Supplies a pointer to a boolean that receives the number
  1143. of transfers that were flushed.
  1144. Return Value:
  1145. Status code.
  1146. --*/
  1147. {
  1148. PEHCI_CONTROLLER Controller;
  1149. ULONG Count;
  1150. PLIST_ENTRY CurrentEntry;
  1151. PEHCI_ENDPOINT Endpoint;
  1152. PEHCI_TRANSFER NextTransfer;
  1153. BOOL RemoveSet;
  1154. KSTATUS Status;
  1155. ULONGLONG Timeout;
  1156. PEHCI_TRANSFER Transfer;
  1157. PEHCI_TRANSFER_SET TransferSet;
  1158. //
  1159. // This routine removes transfers without acquiring the controller lock. It
  1160. // is expected that the caller is using under special circumstances at high
  1161. // run level (e.g. to prepare for crash dump writes during system failure).
  1162. //
  1163. ASSERT(KeGetRunLevel() == RunLevelHigh);
  1164. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  1165. Endpoint = (PEHCI_ENDPOINT)EndpointContext;
  1166. if (Endpoint->TransferType == UsbTransferTypeIsochronous) {
  1167. //
  1168. // TODO: Implement support for isochronous transfers.
  1169. //
  1170. ASSERT(FALSE);
  1171. return STATUS_NOT_SUPPORTED;
  1172. }
  1173. //
  1174. // Let every transfer set in the endpoint complete. If the caller is about
  1175. // to use this endpoint for an operation during a system failure, then the
  1176. // endpoint better be alive enough to finish the rest of its current
  1177. // transfers.
  1178. //
  1179. Timeout = HlQueryTimeCounter() +
  1180. (HlQueryTimeCounterFrequency() * EHCI_ENDPOINT_FLUSH_TIMEOUT);
  1181. Count = 0;
  1182. while (LIST_EMPTY(&(Endpoint->TransferListHead)) == FALSE) {
  1183. if (HlQueryTimeCounter() > Timeout) {
  1184. Status = STATUS_TIMEOUT;
  1185. goto FlushEndpointEnd;
  1186. }
  1187. CurrentEntry = Endpoint->TransferListHead.Next;
  1188. while (CurrentEntry != &(Endpoint->TransferListHead)) {
  1189. ASSERT((CurrentEntry != NULL) && (CurrentEntry->Next != NULL));
  1190. Transfer = LIST_VALUE(CurrentEntry,
  1191. EHCI_TRANSFER,
  1192. EndpointListEntry);
  1193. CurrentEntry = CurrentEntry->Next;
  1194. RemoveSet = EhcipProcessPotentiallyCompletedTransfer(Transfer);
  1195. if (RemoveSet != FALSE) {
  1196. //
  1197. // Get the current entry off of this set, as several transfers
  1198. // may be removed here.
  1199. //
  1200. TransferSet = Transfer->Set;
  1201. if (CurrentEntry != &(Endpoint->TransferListHead)) {
  1202. NextTransfer = LIST_VALUE(CurrentEntry,
  1203. EHCI_TRANSFER,
  1204. EndpointListEntry);
  1205. while (NextTransfer->Set == TransferSet) {
  1206. CurrentEntry = CurrentEntry->Next;
  1207. if (CurrentEntry == &(Endpoint->TransferListHead)) {
  1208. break;
  1209. }
  1210. NextTransfer = LIST_VALUE(CurrentEntry,
  1211. EHCI_TRANSFER,
  1212. EndpointListEntry);
  1213. }
  1214. }
  1215. //
  1216. // Remove the transfer set from the owning endpoint's queue,
  1217. // but don't bother to call the completion routine. It's really
  1218. // just lights out for this transfer.
  1219. //
  1220. EhcipRemoveCompletedTransferSet(Controller, TransferSet);
  1221. Count += 1;
  1222. }
  1223. }
  1224. }
  1225. Status = STATUS_SUCCESS;
  1226. FlushEndpointEnd:
  1227. *TransferCount = Count;
  1228. return Status;
  1229. }
  1230. VOID
  1231. EhcipDestroyEndpoint (
  1232. PVOID HostControllerContext,
  1233. PVOID EndpointContext
  1234. )
  1235. /*++
  1236. Routine Description:
  1237. This routine tears down and destroys an endpoint created with the endpoint
  1238. creation routine.
  1239. Arguments:
  1240. HostControllerContext - Supplies the context pointer passed to the USB core
  1241. when the controller was created. This is used to identify the USB host
  1242. controller to the host controller driver.
  1243. EndpointContext - Supplies a pointer to the context returned by the host
  1244. controller when the endpoint was created.
  1245. Return Value:
  1246. None.
  1247. --*/
  1248. {
  1249. ULONG CommandRegister;
  1250. PEHCI_CONTROLLER Controller;
  1251. PEHCI_ENDPOINT Endpoint;
  1252. BOOL LockHeld;
  1253. RUNLEVEL OldRunLevel;
  1254. PEHCI_TRANSFER_QUEUE Queue;
  1255. PEHCI_TRANSFER_QUEUE QueueBefore;
  1256. BOOL ReleaseEndpoint;
  1257. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  1258. Endpoint = (PEHCI_ENDPOINT)EndpointContext;
  1259. LockHeld = FALSE;
  1260. ReleaseEndpoint = TRUE;
  1261. ASSERT(LIST_EMPTY(&(Endpoint->TransferListHead)) != FALSE);
  1262. //
  1263. // Remove the endpoint's queue from the hardware schedule.
  1264. //
  1265. if (Endpoint->Queue.HardwareQueueHead != NULL) {
  1266. OldRunLevel = EhcipAcquireControllerLock(Controller);
  1267. LockHeld = TRUE;
  1268. if (Endpoint->Queue.HardwareQueueHead == NULL) {
  1269. goto DestroyEndpointEnd;
  1270. }
  1271. Queue = &(Endpoint->Queue);
  1272. Controller->EndpointCount -= 1;
  1273. LIST_REMOVE(&(Endpoint->ListEntry));
  1274. //
  1275. // Isochronous transfers are handled differently.
  1276. //
  1277. if (Endpoint->TransferType == UsbTransferTypeIsochronous) {
  1278. ASSERT(FALSE);
  1279. goto DestroyEndpointEnd;
  1280. //
  1281. // Remove the interrupt endpoint's queue from the synchronous schedule.
  1282. //
  1283. } else if (Endpoint->TransferType == UsbTransferTypeInterrupt) {
  1284. ASSERT(Queue->ListEntry.Next != NULL);
  1285. QueueBefore = LIST_VALUE(Queue->ListEntry.Previous,
  1286. EHCI_TRANSFER_QUEUE,
  1287. ListEntry);
  1288. HlWriteRegister32(&(QueueBefore->HardwareQueueHead->HorizontalLink),
  1289. Queue->HardwareQueueHead->HorizontalLink);
  1290. LIST_REMOVE(&(Queue->ListEntry));
  1291. Queue->ListEntry.Next = NULL;
  1292. //
  1293. // Now release the lock and wait a full frame to make sure that the
  1294. // periodic schedule has moved beyond this queue head. This simple
  1295. // wait accounts for split transactions, but will need to be
  1296. // updated if Frame Split Transaction Nodes are supported.
  1297. //
  1298. EhcipReleaseControllerLock(Controller, OldRunLevel);
  1299. LockHeld = FALSE;
  1300. KeDelayExecution(FALSE, FALSE, 1 * MICROSECONDS_PER_MILLISECOND);
  1301. //
  1302. // The queue can be safely destroyed.
  1303. //
  1304. if (Queue->DummyTransfer != NULL) {
  1305. if (Queue->DummyTransfer->HardwareTransfer != NULL) {
  1306. MmFreeBlock(Controller->BlockAllocator,
  1307. Queue->DummyTransfer->HardwareTransfer);
  1308. }
  1309. MmFreeNonPagedPool(Queue->DummyTransfer);
  1310. }
  1311. MmFreeBlock(Controller->BlockAllocator, Queue->HardwareQueueHead);
  1312. Queue->HardwareQueueHead = NULL;
  1313. //
  1314. // Remove bulk and control endpoint's queue head from the asynchronous
  1315. // schedule. The transfer set will be fully removed from the queue head
  1316. // once the interrupt for async-on-advance has fired.
  1317. //
  1318. } else {
  1319. ASSERT(Queue->AsyncOnAdvanceCancel == FALSE);
  1320. ASSERT((Endpoint->TransferType == UsbTransferTypeControl) ||
  1321. (Endpoint->TransferType == UsbTransferTypeBulk));
  1322. QueueBefore = LIST_VALUE(Queue->ListEntry.Previous,
  1323. EHCI_TRANSFER_QUEUE,
  1324. ListEntry);
  1325. HlWriteRegister32(&(QueueBefore->HardwareQueueHead->HorizontalLink),
  1326. Queue->HardwareQueueHead->HorizontalLink);
  1327. LIST_REMOVE(&(Queue->ListEntry));
  1328. //
  1329. // If the asynchronous on advance ready list is empty, then add
  1330. // this queue head to the ready list and ring the doorbell.
  1331. //
  1332. if (LIST_EMPTY(&(Controller->AsyncOnAdvanceReadyListHead))) {
  1333. INSERT_BEFORE(&(Queue->ListEntry),
  1334. &(Controller->AsyncOnAdvanceReadyListHead));
  1335. CommandRegister = Controller->CommandRegister;
  1336. CommandRegister |= EHCI_COMMAND_INTERRUPT_ON_ASYNC_ADVANCE;
  1337. EHCI_WRITE_REGISTER(Controller,
  1338. EhciRegisterUsbCommand,
  1339. CommandRegister);
  1340. //
  1341. // Otherwise the doorbell has already been rung. This queue head
  1342. // will have to wait for the next chance to ring it. Put it on the
  1343. // pending list.
  1344. //
  1345. } else {
  1346. INSERT_BEFORE(&(Queue->ListEntry),
  1347. &(Controller->AsyncOnAdvancePendingListHead));
  1348. }
  1349. //
  1350. // Do not release the endpoint. It will get released along with the
  1351. // queue when the async-on-advance interrupt is handled.
  1352. //
  1353. ReleaseEndpoint = FALSE;
  1354. }
  1355. }
  1356. DestroyEndpointEnd:
  1357. if (LockHeld != FALSE) {
  1358. EhcipReleaseControllerLock(Controller, OldRunLevel);
  1359. }
  1360. if (ReleaseEndpoint != FALSE) {
  1361. MmFreeNonPagedPool(Endpoint);
  1362. }
  1363. return;
  1364. }
  1365. KSTATUS
  1366. EhcipCreateTransfer (
  1367. PVOID HostControllerContext,
  1368. PVOID EndpointContext,
  1369. ULONG MaxBufferSize,
  1370. ULONG Flags,
  1371. PVOID *TransferContext
  1372. )
  1373. /*++
  1374. Routine Description:
  1375. This routine allocates structures needed for the USB host controller to
  1376. support a transfer.
  1377. Arguments:
  1378. HostControllerContext - Supplies the context pointer passed to the USB core
  1379. when the controller was created. This is used to identify the USB host
  1380. controller to the host controller driver.
  1381. EndpointContext - Supplies a pointer to the host controller's context of
  1382. the endpoint that this transfer will eventually be submitted to.
  1383. MaxBufferSize - Supplies the maximum buffer length, in bytes, of the
  1384. transfer when it is submitted. It is assumed that the host controller
  1385. will set up as many transfer descriptors as are needed to support a
  1386. transfer of this size.
  1387. Flags - Supplies a bitfield of flags regarding the transaction. See
  1388. USB_TRANSFER_FLAG_* definitions.
  1389. TransferContext - Supplies a pointer where the host controller can store a
  1390. context pointer containing any needed structures for the transfer.
  1391. Return Value:
  1392. None.
  1393. --*/
  1394. {
  1395. ULONG AllocationSize;
  1396. PEHCI_CONTROLLER Controller;
  1397. PEHCI_ENDPOINT Endpoint;
  1398. BOOL ForceShortTransfer;
  1399. PEHCI_TRANSFER_DESCRIPTOR HardwareTransfer;
  1400. PHYSICAL_ADDRESS HardwareTransferPhysicalAddress;
  1401. KSTATUS Status;
  1402. PEHCI_TRANSFER Transfer;
  1403. PEHCI_TRANSFER *TransferArray;
  1404. ULONG TransferCount;
  1405. ULONG TransferIndex;
  1406. PEHCI_TRANSFER_SET TransferSet;
  1407. ASSERT(TransferContext != NULL);
  1408. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  1409. Endpoint = (PEHCI_ENDPOINT)EndpointContext;
  1410. ForceShortTransfer = FALSE;
  1411. if ((Flags & USB_TRANSFER_FLAG_FORCE_SHORT_TRANSFER) != 0) {
  1412. ForceShortTransfer = TRUE;
  1413. }
  1414. //
  1415. // Figure out the number of transfers needed. The first 8 bytes of a
  1416. // control transfer (the setup packet) are always on their own. Control
  1417. // transfers also have a status stage at the end.
  1418. //
  1419. TransferCount = 0;
  1420. if (Endpoint->TransferType == UsbTransferTypeControl) {
  1421. ASSERT(MaxBufferSize >= sizeof(USB_SETUP_PACKET));
  1422. MaxBufferSize -= sizeof(USB_SETUP_PACKET);
  1423. //
  1424. // Account for both the setup and status stage here.
  1425. //
  1426. TransferCount += 2;
  1427. }
  1428. //
  1429. // Create enough data transfers knowing that all submitted transfers will
  1430. // have virtually contiguous data. An extra page must be added to the max
  1431. // transfer size for the transfer calculation because a non page-aligned
  1432. // buffer could cause an EHCI max packet size aligned buffer to be split
  1433. // across two hardware transfers.
  1434. //
  1435. if (MaxBufferSize != 0) {
  1436. MaxBufferSize += (EHCI_PAGE_SIZE - 1);
  1437. TransferCount += MaxBufferSize / EHCI_TRANSFER_MAX_PACKET_SIZE;
  1438. if ((MaxBufferSize % EHCI_TRANSFER_MAX_PACKET_SIZE) != 0) {
  1439. TransferCount += 1;
  1440. }
  1441. //
  1442. // If a short transfer needs to be forced and the last packet might not
  1443. // be a short packet, then add another transfer to account for the
  1444. // forced zero length packet.
  1445. //
  1446. if ((ForceShortTransfer != FALSE) &&
  1447. (MaxBufferSize >= Endpoint->MaxPacketSize)) {
  1448. TransferCount += 1;
  1449. }
  1450. //
  1451. // Account for a USB transfer that will only send zero length packets and
  1452. // for control transfers that need to force a zero length packet in the
  1453. // data phase.
  1454. //
  1455. } else if ((ForceShortTransfer != FALSE) ||
  1456. (Endpoint->TransferType != UsbTransferTypeControl)) {
  1457. TransferCount += 1;
  1458. }
  1459. //
  1460. // Allocate the transfer set structure. Include space for all but the first
  1461. // EHCI_TRANSFER. The first transfer is swapped with the queue's dummy
  1462. // transfer and must be done with its own allocation.
  1463. //
  1464. AllocationSize = sizeof(EHCI_TRANSFER_SET);
  1465. if (TransferCount > 1) {
  1466. AllocationSize += sizeof(PEHCI_TRANSFER) * (TransferCount - 1);
  1467. AllocationSize += sizeof(EHCI_TRANSFER) * (TransferCount - 1);
  1468. }
  1469. TransferSet = MmAllocateNonPagedPool(AllocationSize, EHCI_ALLOCATION_TAG);
  1470. if (TransferSet == NULL) {
  1471. Status = STATUS_INSUFFICIENT_RESOURCES;
  1472. goto CreateTransferEnd;
  1473. }
  1474. RtlZeroMemory(TransferSet, AllocationSize);
  1475. TransferSet->TransferCount = TransferCount;
  1476. TransferSet->Endpoint = Endpoint;
  1477. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  1478. //
  1479. // Allocate the first transfer.
  1480. //
  1481. ASSERT(TransferCount >= 1);
  1482. Transfer = MmAllocateNonPagedPool(sizeof(EHCI_TRANSFER),
  1483. EHCI_ALLOCATION_TAG);
  1484. if (Transfer == NULL) {
  1485. Status = STATUS_INSUFFICIENT_RESOURCES;
  1486. goto CreateTransferEnd;
  1487. }
  1488. RtlZeroMemory(Transfer, sizeof(EHCI_TRANSFER));
  1489. Transfer->Set = TransferSet;
  1490. TransferArray[0] = Transfer;
  1491. //
  1492. // Create the new transfer's hardware descriptors while initializing the
  1493. // transfers that are included within the transfer set allocation.
  1494. //
  1495. Transfer = (PEHCI_TRANSFER)((PVOID)(TransferSet + 1) +
  1496. (sizeof(PEHCI_TRANSFER) * (TransferCount - 1)));
  1497. for (TransferIndex = 0; TransferIndex < TransferCount; TransferIndex += 1) {
  1498. HardwareTransfer = MmAllocateBlock(Controller->BlockAllocator,
  1499. &HardwareTransferPhysicalAddress);
  1500. if (HardwareTransfer == NULL) {
  1501. Status = STATUS_INSUFFICIENT_RESOURCES;
  1502. goto CreateTransferEnd;
  1503. }
  1504. if (TransferIndex != 0) {
  1505. TransferArray[TransferIndex] = Transfer;
  1506. Transfer->Set = TransferSet;
  1507. Transfer += 1;
  1508. }
  1509. TransferArray[TransferIndex]->HardwareTransfer = HardwareTransfer;
  1510. TransferArray[TransferIndex]->PhysicalAddress =
  1511. HardwareTransferPhysicalAddress;
  1512. ASSERT((HardwareTransferPhysicalAddress & EHCI_LINK_ADDRESS_MASK) ==
  1513. HardwareTransferPhysicalAddress);
  1514. }
  1515. Status = STATUS_SUCCESS;
  1516. CreateTransferEnd:
  1517. if (!KSUCCESS(Status)) {
  1518. if (TransferSet != NULL) {
  1519. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  1520. for (TransferIndex = 0;
  1521. TransferIndex < TransferSet->TransferCount;
  1522. TransferIndex += 1) {
  1523. Transfer = TransferArray[TransferIndex];
  1524. if (Transfer != NULL) {
  1525. if (Transfer->HardwareTransfer != NULL) {
  1526. MmFreeBlock(Controller->BlockAllocator,
  1527. Transfer->HardwareTransfer);
  1528. }
  1529. if (TransferIndex == 0) {
  1530. MmFreeNonPagedPool(Transfer);
  1531. }
  1532. }
  1533. }
  1534. MmFreeNonPagedPool(TransferSet);
  1535. TransferSet = NULL;
  1536. }
  1537. }
  1538. *TransferContext = TransferSet;
  1539. return Status;
  1540. }
  1541. VOID
  1542. EhcipDestroyTransfer (
  1543. PVOID HostControllerContext,
  1544. PVOID EndpointContext,
  1545. PVOID TransferContext
  1546. )
  1547. /*++
  1548. Routine Description:
  1549. This routine destroys host controller structures associated with a USB
  1550. transfer.
  1551. Arguments:
  1552. HostControllerContext - Supplies the context pointer passed to the USB core
  1553. when the controller was created. This is used to identify the USB host
  1554. controller to the host controller driver.
  1555. EndpointContext - Supplies a pointer to the host controller context for the
  1556. endpoint this transfer belonged to.
  1557. TransferContext - Supplies the pointer provided to the USB core by the host
  1558. controller when the transfer was created.
  1559. Return Value:
  1560. None.
  1561. --*/
  1562. {
  1563. PEHCI_CONTROLLER Controller;
  1564. PEHCI_TRANSFER Transfer;
  1565. PEHCI_TRANSFER *TransferArray;
  1566. ULONG TransferIndex;
  1567. PEHCI_TRANSFER_SET TransferSet;
  1568. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  1569. TransferSet = (PEHCI_TRANSFER_SET)TransferContext;
  1570. //
  1571. // Free all transfers that were allocated.
  1572. //
  1573. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  1574. for (TransferIndex = 0;
  1575. TransferIndex < TransferSet->TransferCount;
  1576. TransferIndex += 1) {
  1577. Transfer = TransferArray[TransferIndex];
  1578. ASSERT(Transfer != NULL);
  1579. ASSERT(Transfer->HardwareTransfer != NULL);
  1580. ASSERT(Transfer->EndpointListEntry.Next == NULL);
  1581. MmFreeBlock(Controller->BlockAllocator, Transfer->HardwareTransfer);
  1582. if (TransferIndex == 0) {
  1583. MmFreeNonPagedPool(Transfer);
  1584. }
  1585. TransferArray[TransferIndex] = NULL;
  1586. }
  1587. MmFreeNonPagedPool(TransferSet);
  1588. return;
  1589. }
  1590. KSTATUS
  1591. EhcipSubmitTransfer (
  1592. PVOID HostControllerContext,
  1593. PVOID EndpointContext,
  1594. PUSB_TRANSFER_INTERNAL Transfer,
  1595. PVOID TransferContext
  1596. )
  1597. /*++
  1598. Routine Description:
  1599. This routine submits a transfer to the USB host controller for execution.
  1600. Arguments:
  1601. HostControllerContext - Supplies the context pointer passed to the USB core
  1602. when the controller was created. This is used to identify the USB host
  1603. controller to the host controller driver.
  1604. EndpointContext - Supplies the context pointer provided to the USB core by
  1605. the host controller when the endpoint was created.
  1606. Transfer - Supplies a pointer to the USB transfer to execute.
  1607. TransferContext - Supplies the pointer provided to the USB core by the host
  1608. controller when the transfer was created.
  1609. Return Value:
  1610. STATUS_SUCCESS if the transfer was successfully added to the hardware queue.
  1611. Failure codes if the transfer could not be added.
  1612. --*/
  1613. {
  1614. PEHCI_CONTROLLER Controller;
  1615. PEHCI_ENDPOINT Endpoint;
  1616. UCHAR QueueDeviceAddress;
  1617. KSTATUS Status;
  1618. PEHCI_TRANSFER_SET TransferSet;
  1619. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  1620. Endpoint = (PEHCI_ENDPOINT)EndpointContext;
  1621. TransferSet = (PEHCI_TRANSFER_SET)TransferContext;
  1622. TransferSet->UsbTransfer = Transfer;
  1623. //
  1624. // Before filling out and inserting transfers, take a look to see if the
  1625. // device address has changed. If it has, then it should still be in the
  1626. // enumeration phase, meaning there are no pending transfers floating
  1627. // around.
  1628. //
  1629. QueueDeviceAddress = Endpoint->Queue.HardwareQueueHead->Destination &
  1630. EHCI_QUEUE_DEVICE_ADDRESS_MASK;
  1631. if (Transfer->DeviceAddress != QueueDeviceAddress) {
  1632. ASSERT((QueueDeviceAddress == 0) && (Transfer->DeviceAddress != 0));
  1633. ASSERT(LIST_EMPTY(&(Endpoint->TransferListHead)) != FALSE);
  1634. Endpoint->Queue.HardwareQueueHead->Destination |=
  1635. Transfer->DeviceAddress & EHCI_QUEUE_DEVICE_ADDRESS_MASK;
  1636. }
  1637. //
  1638. // Initialize and submit the EHCI transfer set.
  1639. //
  1640. Status = EhcipSubmitTransferSet(Controller,
  1641. Endpoint,
  1642. TransferSet,
  1643. NULL,
  1644. FALSE);
  1645. return Status;
  1646. }
  1647. KSTATUS
  1648. EhcipSubmitPolledTransfer (
  1649. PVOID HostControllerContext,
  1650. PVOID EndpointContext,
  1651. PUSB_TRANSFER_INTERNAL Transfer,
  1652. PVOID TransferContext
  1653. )
  1654. /*++
  1655. Routine Description:
  1656. This routine submits a transfer to the USB host controller for execution
  1657. and busy waits for it to complete. This routine is meant for crash dump
  1658. support to allow USB transfers when the system is fragile. As a result, it
  1659. forgoes acquiring the normal sequence of locks.
  1660. Arguments:
  1661. HostControllerContext - Supplies the context pointer passed to the USB core
  1662. when the controller was created. This is used to identify the USB host
  1663. controller to the host controller driver.
  1664. EndpointContext - Supplies the context pointer provided to the USB core by
  1665. the host controller when the endpoint was created.
  1666. Transfer - Supplies a pointer to the USB transfer to execute.
  1667. TransferContext - Supplies the pointer provided to the USB core by the host
  1668. controller when the transfer was created.
  1669. Return Value:
  1670. STATUS_SUCCESS if the transfer was successfully added to the hardware queue.
  1671. Failure codes if the transfer could not be added.
  1672. --*/
  1673. {
  1674. PEHCI_CONTROLLER Controller;
  1675. PEHCI_TRANSFER EhciTransfer;
  1676. PEHCI_ENDPOINT Endpoint;
  1677. volatile PULONG HardwareStatus;
  1678. PEHCI_TRANSFER_QUEUE Queue;
  1679. UCHAR QueueDeviceAddress;
  1680. BOOL RemoveSet;
  1681. KSTATUS Status;
  1682. ULONGLONG Timeout;
  1683. PEHCI_TRANSFER *TransferArray;
  1684. ULONG TransferCount;
  1685. ULONG TransferIndex;
  1686. PEHCI_TRANSFER_SET TransferSet;
  1687. ASSERT(KeGetRunLevel() == RunLevelHigh);
  1688. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  1689. Endpoint = (PEHCI_ENDPOINT)EndpointContext;
  1690. TransferSet = (PEHCI_TRANSFER_SET)TransferContext;
  1691. TransferSet->UsbTransfer = Transfer;
  1692. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  1693. //
  1694. // Then endpoint better not be in the middle of a transfer.
  1695. //
  1696. ASSERT(LIST_EMPTY(&(Endpoint->TransferListHead)) != FALSE);
  1697. //
  1698. // The queue head should be pointing at the dummy transfer and that dummy
  1699. // transfer should be the end of the line.
  1700. //
  1701. Queue = &(Endpoint->Queue);
  1702. ASSERT(Queue->HardwareQueueHead->TransferOverlay.NextTransfer ==
  1703. Queue->DummyTransfer->PhysicalAddress);
  1704. ASSERT(Queue->DummyTransfer->HardwareTransfer->NextTransfer ==
  1705. EHCI_LINK_TERMINATE);
  1706. ASSERT(Queue->DummyTransfer->HardwareTransfer->AlternateNextTransfer ==
  1707. EHCI_LINK_TERMINATE);
  1708. ASSERT(Queue->DummyTransfer->HardwareTransfer->Token ==
  1709. EHCI_TRANSFER_STATUS_HALTED);
  1710. //
  1711. // Before filling out and inserting transfers, assert that the device's
  1712. // address has not changed. Polled I/O should not be used during a device's
  1713. // enumeration phase.
  1714. //
  1715. QueueDeviceAddress = Queue->HardwareQueueHead->Destination &
  1716. EHCI_QUEUE_DEVICE_ADDRESS_MASK;
  1717. ASSERT(Transfer->DeviceAddress == QueueDeviceAddress);
  1718. //
  1719. // Initialize and submit the EHCI transfer set.
  1720. //
  1721. Status = EhcipSubmitTransferSet(Controller,
  1722. Endpoint,
  1723. TransferSet,
  1724. &TransferCount,
  1725. TRUE);
  1726. if (!KSUCCESS(Status)) {
  1727. return Status;
  1728. }
  1729. //
  1730. // The transfer is under way. Time to wait for it to complete. This
  1731. // requires a busy spin as threads cannot yield in the limited environment
  1732. // this routine is meant for.
  1733. //
  1734. Timeout = HlQueryTimeCounter() +
  1735. (HlQueryTimeCounterFrequency() * EHCI_POLLED_TRANSFER_TIMEOUT);
  1736. for (TransferIndex = 0; TransferIndex < TransferCount; TransferIndex += 1) {
  1737. EhciTransfer = TransferArray[TransferIndex];
  1738. HardwareStatus = &(EhciTransfer->HardwareTransfer->Token);
  1739. while ((*HardwareStatus & EHCI_TRANSFER_STATUS_ACTIVE) != 0) {
  1740. if (HlQueryTimeCounter() > Timeout) {
  1741. Transfer->Public.Status = STATUS_TIMEOUT;
  1742. goto SubmitPolledTransferEnd;
  1743. }
  1744. }
  1745. RemoveSet = EhcipProcessPotentiallyCompletedTransfer(EhciTransfer);
  1746. if (RemoveSet != FALSE) {
  1747. break;
  1748. }
  1749. }
  1750. EhcipRemoveCompletedTransferSet(Controller, TransferSet);
  1751. SubmitPolledTransferEnd:
  1752. return Transfer->Public.Status;
  1753. }
  1754. KSTATUS
  1755. EhcipSubmitTransferSet (
  1756. PEHCI_CONTROLLER Controller,
  1757. PEHCI_ENDPOINT Endpoint,
  1758. PEHCI_TRANSFER_SET TransferSet,
  1759. PULONG SubmittedTransferCount,
  1760. BOOL LockNotRequired
  1761. )
  1762. /*++
  1763. Routine Description:
  1764. This routine submits the given transfer set on the provided endpoint.
  1765. Arguments:
  1766. Controller - Supplies a pointer to the EHCI controller context.
  1767. Endpoint - Supplies a pointer to the endpoint that owns the transfer set.
  1768. TransferSet - Supplies a pointer to the transfer set to submit.
  1769. SubmittedTransferCount - Supplies an optional pointer to a boolean that
  1770. receives the total number of transfers submitted for the set.
  1771. LockNotRequired - Supplies a pointer indicating whether or not the
  1772. controllers lock is required when submitting. The default is FALSE.
  1773. Return Value:
  1774. Status code.
  1775. --*/
  1776. {
  1777. LIST_ENTRY ControllerList;
  1778. BOOL ControlTransfer;
  1779. BOOL DataToggle;
  1780. PEHCI_TRANSFER EhciTransfer;
  1781. LIST_ENTRY EndpointList;
  1782. PEHCI_TRANSFER FinalTransfer;
  1783. BOOL ForceShortTransfer;
  1784. BOOL LastTransfer;
  1785. ULONG Length;
  1786. ULONG Offset;
  1787. RUNLEVEL OldRunLevel;
  1788. ULONG PageOffset;
  1789. PEHCI_TRANSFER PreviousTransfer;
  1790. ULONG TotalLength;
  1791. PUSB_TRANSFER_INTERNAL Transfer;
  1792. PEHCI_TRANSFER *TransferArray;
  1793. ULONG TransferCount;
  1794. ULONG TransferIndex;
  1795. ControlTransfer = FALSE;
  1796. EhciTransfer = NULL;
  1797. FinalTransfer = NULL;
  1798. Transfer = TransferSet->UsbTransfer;
  1799. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  1800. //
  1801. // This queue had better be inserted.
  1802. //
  1803. ASSERT(Endpoint->Queue.ListEntry.Next != NULL);
  1804. //
  1805. // The transfer set had better not already be queued.
  1806. //
  1807. ASSERT((TransferSet->Flags & EHCI_TRANSFER_SET_FLAG_QUEUED) == 0);
  1808. //
  1809. // Initialize the state to queued. Old state from the last go-around should
  1810. // be wiped.
  1811. //
  1812. TransferSet->Flags = EHCI_TRANSFER_SET_FLAG_QUEUED;
  1813. //
  1814. // Assume that this is going to be a rousing success.
  1815. //
  1816. Transfer->Public.Status = STATUS_SUCCESS;
  1817. Transfer->Public.Error = UsbErrorNone;
  1818. //
  1819. // Determine the number of EHCI transfers needed for this USB transfer,
  1820. // and loop filling them out. This is necessary because the number of
  1821. // EHCI transfers per USB transfer is not constant; the system may re-use a
  1822. // transfer and change the length.
  1823. //
  1824. PageOffset = REMAINDER(Transfer->Public.BufferPhysicalAddress,
  1825. EHCI_PAGE_SIZE);
  1826. TransferCount = 0;
  1827. TotalLength = Transfer->Public.Length;
  1828. if (Endpoint->TransferType == UsbTransferTypeControl) {
  1829. ControlTransfer = TRUE;
  1830. ASSERT(TotalLength >= sizeof(USB_SETUP_PACKET));
  1831. TotalLength -= sizeof(USB_SETUP_PACKET);
  1832. //
  1833. // Account for both the setup and status transfers.
  1834. //
  1835. TransferCount += 2;
  1836. PageOffset += sizeof(USB_SETUP_PACKET);
  1837. PageOffset = REMAINDER(PageOffset, EHCI_PAGE_SIZE);
  1838. }
  1839. ForceShortTransfer = FALSE;
  1840. if ((Transfer->Public.Flags &
  1841. USB_TRANSFER_FLAG_FORCE_SHORT_TRANSFER) != 0) {
  1842. ForceShortTransfer = TRUE;
  1843. }
  1844. //
  1845. // If the USB transfer has data, the number of data transfers depends on
  1846. // the length of the data and the page offset for the start of the data.
  1847. //
  1848. if (TotalLength != 0) {
  1849. TotalLength += PageOffset;
  1850. TransferCount += TotalLength / EHCI_TRANSFER_MAX_PACKET_SIZE;
  1851. if ((TotalLength % EHCI_TRANSFER_MAX_PACKET_SIZE) != 0) {
  1852. TransferCount += 1;
  1853. }
  1854. //
  1855. // If a short transfer must be sent and the total length is a multiple,
  1856. // of the max packet size, then add an extra transfer to make sure a
  1857. // short transfer is sent.
  1858. //
  1859. if ((ForceShortTransfer != FALSE) &&
  1860. ((TotalLength % Endpoint->MaxPacketSize) == 0)) {
  1861. TransferCount += 1;
  1862. }
  1863. //
  1864. // Make sure at least one packet is set for zero-length packets. Unless a
  1865. // short transfer is being forced, exclude control transfers as there is
  1866. // just no data phase if this is the case.
  1867. //
  1868. } else if ((ForceShortTransfer != FALSE) ||
  1869. (Endpoint->TransferType != UsbTransferTypeControl)) {
  1870. TransferCount = 1;
  1871. }
  1872. ASSERT(TransferSet->TransferCount >= TransferCount);
  1873. //
  1874. // Now that the transfer count has been computed, save the ultimate
  1875. // transfer if it's a control request.
  1876. //
  1877. if (ControlTransfer != FALSE) {
  1878. FinalTransfer = TransferArray[TransferCount - 1];
  1879. }
  1880. PageOffset = REMAINDER(Transfer->Public.BufferPhysicalAddress,
  1881. EHCI_PAGE_SIZE);
  1882. DataToggle = FALSE;
  1883. Offset = 0;
  1884. LastTransfer = FALSE;
  1885. INITIALIZE_LIST_HEAD(&ControllerList);
  1886. INITIALIZE_LIST_HEAD(&EndpointList);
  1887. for (TransferIndex = 0; TransferIndex < TransferCount; TransferIndex += 1) {
  1888. //
  1889. // Calculate the length for this transfer descriptor.
  1890. //
  1891. Length = EHCI_TRANSFER_MAX_PACKET_SIZE - PageOffset;
  1892. if ((Offset + Length) > Transfer->Public.Length) {
  1893. Length = Transfer->Public.Length - Offset;
  1894. }
  1895. if (TransferIndex == (TransferCount - 1)) {
  1896. LastTransfer = TRUE;
  1897. }
  1898. if (ControlTransfer != FALSE) {
  1899. //
  1900. // The first part of a control transfer is the setup packet, which
  1901. // is always 8 bytes long.
  1902. //
  1903. if (Offset == 0) {
  1904. Length = sizeof(USB_SETUP_PACKET);
  1905. }
  1906. //
  1907. // The last part of a control transfer is the status phase and the
  1908. // length better be zero.
  1909. //
  1910. ASSERT((LastTransfer == FALSE) || (Length == 0));
  1911. }
  1912. ASSERT((Length != 0) ||
  1913. (LastTransfer != FALSE) ||
  1914. ((ForceShortTransfer != FALSE) && (ControlTransfer != FALSE)));
  1915. //
  1916. // Fill out this transfer descriptor.
  1917. //
  1918. EhciTransfer = TransferArray[TransferIndex];
  1919. EhcipFillOutTransferDescriptor(Controller,
  1920. EhciTransfer,
  1921. Offset,
  1922. Length,
  1923. LastTransfer,
  1924. &DataToggle,
  1925. FinalTransfer);
  1926. //
  1927. // Point the previous transfer to this transfer.
  1928. //
  1929. if (TransferIndex != 0) {
  1930. PreviousTransfer = TransferArray[TransferIndex - 1];
  1931. PreviousTransfer->HardwareTransfer->NextTransfer =
  1932. (ULONG)EhciTransfer->PhysicalAddress;
  1933. }
  1934. ASSERT(EhciTransfer->GlobalListEntry.Next == NULL);
  1935. INSERT_BEFORE(&(EhciTransfer->EndpointListEntry), &EndpointList);
  1936. INSERT_BEFORE(&(EhciTransfer->GlobalListEntry), &ControllerList);
  1937. //
  1938. // Advance the buffer position.
  1939. //
  1940. Offset += Length;
  1941. PageOffset += Length;
  1942. PageOffset = REMAINDER(PageOffset, EHCI_PAGE_SIZE);
  1943. }
  1944. //
  1945. // Acquire the lock, if requested. It did not need to be acquired for
  1946. // filling out the descriptors because no modifiable global or endpoint
  1947. // state was read or modified.
  1948. //
  1949. if (LockNotRequired == FALSE) {
  1950. OldRunLevel = EhcipAcquireControllerLock(Controller);
  1951. }
  1952. //
  1953. // Add the transfer to the endpoint and controller global lists by
  1954. // appending the locally created lists.
  1955. //
  1956. APPEND_LIST(&EndpointList, &(Endpoint->TransferListHead));
  1957. APPEND_LIST(&ControllerList, &(Controller->TransferListHead));
  1958. //
  1959. // The transfer is ready to go. Do the actual insertion.
  1960. //
  1961. if (Transfer->Type == UsbTransferTypeIsochronous) {
  1962. //
  1963. // TODO: Implement isochronous support.
  1964. //
  1965. ASSERT(FALSE);
  1966. return STATUS_NOT_IMPLEMENTED;
  1967. } else {
  1968. //
  1969. // Mark the last transfer, then submit the transfer array to the
  1970. // hardware.
  1971. //
  1972. ASSERT(TransferCount != 0);
  1973. TransferArray[TransferCount - 1]->LastTransfer = TRUE;
  1974. EhcipLinkTransferSetInHardware(TransferSet);
  1975. }
  1976. //
  1977. // All done. Release the lock, if necessary, and return.
  1978. //
  1979. if (LockNotRequired == FALSE) {
  1980. EhcipReleaseControllerLock(Controller, OldRunLevel);
  1981. }
  1982. if (SubmittedTransferCount != NULL) {
  1983. *SubmittedTransferCount = TransferCount;
  1984. }
  1985. return STATUS_SUCCESS;
  1986. }
  1987. KSTATUS
  1988. EhcipCancelTransfer (
  1989. PVOID HostControllerContext,
  1990. PVOID EndpointContext,
  1991. PUSB_TRANSFER_INTERNAL Transfer,
  1992. PVOID TransferContext
  1993. )
  1994. /*++
  1995. Routine Description:
  1996. This routine submits attempts to cancel a transfer that was previously
  1997. submitted for execution.
  1998. Arguments:
  1999. HostControllerContext - Supplies the context pointer passed to the USB core
  2000. when the controller was created. This is used to identify the USB host
  2001. controller to the host controller driver.
  2002. EndpointContext - Supplies the context pointer provided to the USB core by
  2003. the host controller when the endpoint was created.
  2004. Transfer - Supplies a pointer to the USB transfer to execute.
  2005. TransferContext - Supplies the pointer provided to the USB core by the host
  2006. controller when the transfer was created.
  2007. Return Value:
  2008. STATUS_SUCCESS if the transfer was successfully removed from the hardware
  2009. queue.
  2010. STATUS_TOO_LATE if the transfer had already completed.
  2011. Other failure codes if the transfer could not be cancelled but has not yet
  2012. completed.
  2013. --*/
  2014. {
  2015. ULONG CommandRegister;
  2016. PEHCI_CONTROLLER Controller;
  2017. ULONG HorizontalLink;
  2018. ULONG InterruptTreeLevel;
  2019. RUNLEVEL OldRunLevel;
  2020. ULONG PollRate;
  2021. PEHCI_TRANSFER_QUEUE Queue;
  2022. PEHCI_TRANSFER_QUEUE QueueBefore;
  2023. KSTATUS Status;
  2024. PEHCI_TRANSFER_SET TransferSet;
  2025. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  2026. Status = STATUS_SUCCESS;
  2027. TransferSet = (PEHCI_TRANSFER_SET)TransferContext;
  2028. ASSERT(TransferSet->UsbTransfer == Transfer);
  2029. //
  2030. // Lock the controller to manipulate lists.
  2031. //
  2032. OldRunLevel = EhcipAcquireControllerLock(Controller);
  2033. //
  2034. // If the transfer set is not currently queued, then there is nothing to be
  2035. // done.
  2036. //
  2037. if ((TransferSet->Flags & EHCI_TRANSFER_SET_FLAG_QUEUED) == 0) {
  2038. Status = STATUS_TOO_LATE;
  2039. goto CancelTransferEnd;
  2040. }
  2041. //
  2042. // Isochronous transfers are handled differently.
  2043. //
  2044. if (Transfer->Type == UsbTransferTypeIsochronous) {
  2045. ASSERT(FALSE);
  2046. Status = STATUS_NOT_IMPLEMENTED;
  2047. goto CancelTransferEnd;
  2048. //
  2049. // Remove the interrupt endpoint's queue head from the synchronous schedule.
  2050. //
  2051. } else if (Transfer->Type == UsbTransferTypeInterrupt) {
  2052. Queue = &(TransferSet->Endpoint->Queue);
  2053. //
  2054. // This code assumes that there is only one transfer on an interrupt
  2055. // endpoint.
  2056. //
  2057. ASSERT(Queue->ListEntry.Next != NULL);
  2058. QueueBefore = LIST_VALUE(Queue->ListEntry.Previous,
  2059. EHCI_TRANSFER_QUEUE,
  2060. ListEntry);
  2061. HlWriteRegister32(&(QueueBefore->HardwareQueueHead->HorizontalLink),
  2062. Queue->HardwareQueueHead->HorizontalLink);
  2063. LIST_REMOVE(&(Queue->ListEntry));
  2064. Queue->ListEntry.Next = NULL;
  2065. //
  2066. // Now release the lock and wait a full frame to make sure that the
  2067. // periodic schedule has moved beyond this queue head. This simple wait
  2068. // accounts for split transactions, but will need to be updated if
  2069. // Frame Split Transaction Nodes are supported.
  2070. //
  2071. EhcipReleaseControllerLock(Controller, OldRunLevel);
  2072. KeDelayExecution(FALSE, FALSE, 1 * MICROSECONDS_PER_MILLISECOND);
  2073. //
  2074. // Reacquire the lock to complete the cancellation.
  2075. //
  2076. OldRunLevel = EhcipAcquireControllerLock(Controller);
  2077. ASSERT(Queue->ListEntry.Next == NULL);
  2078. //
  2079. // If the interrupt was completed while the lock was released, then
  2080. // return that it was too late to cancel.
  2081. //
  2082. if ((TransferSet->Flags & EHCI_TRANSFER_SET_FLAG_QUEUED) == 0) {
  2083. Status = STATUS_TOO_LATE;
  2084. //
  2085. // Otherwise mark the transfer as cancelled, remove the transfer set
  2086. // and complete the callback.
  2087. //
  2088. } else {
  2089. Transfer->Public.Status = STATUS_OPERATION_CANCELLED;
  2090. Transfer->Public.Error = UsbErrorTransferCancelled;
  2091. EhcipRemoveCancelledTransferSet(Controller, TransferSet);
  2092. UsbHostProcessCompletedTransfer(TransferSet->UsbTransfer);
  2093. }
  2094. //
  2095. // Add the queue back into the periodic schedule.
  2096. //
  2097. PollRate = TransferSet->Endpoint->PollRate;
  2098. ASSERT(PollRate != 0);
  2099. InterruptTreeLevel = EHCI_PERIODIC_SCHEDULE_TREE_DEPTH - 1;
  2100. while (((PollRate & 0x1) == 0) && (InterruptTreeLevel != 0)) {
  2101. PollRate = PollRate >> 1;
  2102. InterruptTreeLevel -= 1;
  2103. }
  2104. ASSERT(InterruptTreeLevel < EHCI_PERIODIC_SCHEDULE_TREE_DEPTH);
  2105. QueueBefore = &(Controller->InterruptTree[InterruptTreeLevel]);
  2106. INSERT_AFTER(&(Queue->ListEntry), &(QueueBefore->ListEntry));
  2107. Queue->HardwareQueueHead->HorizontalLink =
  2108. QueueBefore->HardwareQueueHead->HorizontalLink;
  2109. HorizontalLink = (ULONG)Queue->PhysicalAddress;
  2110. ASSERT((HorizontalLink & (~EHCI_LINK_ADDRESS_MASK)) == 0);
  2111. HorizontalLink |= EHCI_LINK_TYPE_QUEUE_HEAD;
  2112. HlWriteRegister32(&(QueueBefore->HardwareQueueHead->HorizontalLink),
  2113. HorizontalLink);
  2114. //
  2115. // Remove bulk and control endpoint's queue head from the asynchronous
  2116. // schedule. The transfer set will be fully removed from the queue head
  2117. // once the interrupt for async-on-advance has fired.
  2118. //
  2119. } else {
  2120. ASSERT((Transfer->Type == UsbTransferTypeControl) ||
  2121. (Transfer->Type == UsbTransferTypeBulk));
  2122. //
  2123. // Mark that the transfer set is in the process of being cancelled.
  2124. //
  2125. TransferSet->Flags |= EHCI_TRANSFER_SET_FLAG_CANCELLING;
  2126. //
  2127. // If the queue's async on advance state is already set, that means it
  2128. // is already out of the hardware's queue head and on a list. This
  2129. // transfer will be handled by interrupt processing.
  2130. //
  2131. Queue = &(TransferSet->Endpoint->Queue);
  2132. if (Queue->AsyncOnAdvanceCancel != FALSE) {
  2133. goto CancelTransferEnd;
  2134. }
  2135. Queue->AsyncOnAdvanceCancel = TRUE;
  2136. //
  2137. // Otherwise the queue must be removed from the hardware list.
  2138. //
  2139. QueueBefore = LIST_VALUE(Queue->ListEntry.Previous,
  2140. EHCI_TRANSFER_QUEUE,
  2141. ListEntry);
  2142. HlWriteRegister32(&(QueueBefore->HardwareQueueHead->HorizontalLink),
  2143. Queue->HardwareQueueHead->HorizontalLink);
  2144. LIST_REMOVE(&(Queue->ListEntry));
  2145. //
  2146. // If the asynchronous on advance ready list is empty, then add this
  2147. // queue head to the ready list and ring the doorbell.
  2148. //
  2149. if (LIST_EMPTY(&(Controller->AsyncOnAdvanceReadyListHead)) != FALSE) {
  2150. INSERT_BEFORE(&(Queue->ListEntry),
  2151. &(Controller->AsyncOnAdvanceReadyListHead));
  2152. CommandRegister = Controller->CommandRegister;
  2153. CommandRegister |= EHCI_COMMAND_INTERRUPT_ON_ASYNC_ADVANCE;
  2154. EHCI_WRITE_REGISTER(Controller,
  2155. EhciRegisterUsbCommand,
  2156. CommandRegister);
  2157. //
  2158. // Otherwise the doorbell has already been rung. This queue head will
  2159. // have to wait for the next chance to ring it. Put it on the pending
  2160. // list.
  2161. //
  2162. } else {
  2163. INSERT_BEFORE(&(Queue->ListEntry),
  2164. &(Controller->AsyncOnAdvancePendingListHead));
  2165. }
  2166. }
  2167. CancelTransferEnd:
  2168. //
  2169. // Release the lock and return.
  2170. //
  2171. EhcipReleaseControllerLock(Controller, OldRunLevel);
  2172. return Status;
  2173. }
  2174. KSTATUS
  2175. EhcipGetRootHubStatus (
  2176. PVOID HostControllerContext,
  2177. PUSB_HUB_STATUS HubStatus
  2178. )
  2179. /*++
  2180. Routine Description:
  2181. This routine queries the host controller for the status of the root hub.
  2182. Arguments:
  2183. HostControllerContext - Supplies the context pointer passed to the USB core
  2184. when the controller was created. This is used to identify the USB host
  2185. controller to the host controller driver.
  2186. HubStatus - Supplies a pointer where the host controller should fill out
  2187. the root hub status.
  2188. Return Value:
  2189. STATUS_SUCCESS if the hub status was successfully queried.
  2190. Failure codes if the status could not be queried.
  2191. --*/
  2192. {
  2193. USHORT ChangeBits;
  2194. PEHCI_CONTROLLER Controller;
  2195. USHORT HardwareStatus;
  2196. ULONG PortIndex;
  2197. PUSB_PORT_STATUS PortStatus;
  2198. USHORT SoftwareStatus;
  2199. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  2200. ASSERT(Controller->PortCount != 0);
  2201. ASSERT(HubStatus->PortStatus != NULL);
  2202. for (PortIndex = 0; PortIndex < Controller->PortCount; PortIndex += 1) {
  2203. HardwareStatus = EHCI_READ_PORT_REGISTER(Controller, PortIndex);
  2204. //
  2205. // Set the corresponding software bits. If the owner bit is set,
  2206. // pretend like there's nothing here.
  2207. //
  2208. SoftwareStatus = 0;
  2209. if (((HardwareStatus & EHCI_PORT_CONNECT_STATUS) != 0) &&
  2210. ((HardwareStatus & EHCI_PORT_OWNER) == 0)) {
  2211. SoftwareStatus |= USB_PORT_STATUS_CONNECTED;
  2212. //
  2213. // If the port is presenting a K state, then it's a low speed.
  2214. // Otherwise, assume that if it hasn't yet been passed off to the
  2215. // companion controller that it's a high speed device. If it turns
  2216. // out to be a full speed device, it will eventually get
  2217. // disconnected from here and passed on to the companion controller.
  2218. //
  2219. if ((HardwareStatus & EHCI_PORT_LINE_STATE_MASK) ==
  2220. EHCI_PORT_LINE_STATE_K) {
  2221. HubStatus->PortDeviceSpeed[PortIndex] = UsbDeviceSpeedLow;
  2222. //
  2223. // Release ownership of this device.
  2224. //
  2225. HardwareStatus |= EHCI_PORT_OWNER;
  2226. EHCI_WRITE_PORT_REGISTER(Controller, PortIndex, HardwareStatus);
  2227. HardwareStatus = 0;
  2228. SoftwareStatus = 0;
  2229. } else {
  2230. HubStatus->PortDeviceSpeed[PortIndex] = UsbDeviceSpeedHigh;
  2231. }
  2232. }
  2233. if ((HardwareStatus & EHCI_PORT_ENABLE) != 0) {
  2234. SoftwareStatus |= USB_PORT_STATUS_ENABLED;
  2235. }
  2236. if ((HardwareStatus & EHCI_PORT_RESET) != 0) {
  2237. SoftwareStatus |= USB_PORT_STATUS_RESET;
  2238. }
  2239. if ((HardwareStatus & EHCI_PORT_OVER_CURRENT_ACTIVE) != 0) {
  2240. SoftwareStatus |= USB_PORT_STATUS_OVER_CURRENT;
  2241. }
  2242. //
  2243. // If the new software status is different from the current software
  2244. // status, record the change bits and set the new software status.
  2245. //
  2246. PortStatus = &(HubStatus->PortStatus[PortIndex]);
  2247. if (SoftwareStatus != PortStatus->Status) {
  2248. ChangeBits = SoftwareStatus ^ PortStatus->Status;
  2249. //
  2250. // Because the change bits correspond with the status bits 1-to-1,
  2251. // just OR in the change bits.
  2252. //
  2253. PortStatus->Change |= ChangeBits;
  2254. PortStatus->Status = SoftwareStatus;
  2255. }
  2256. //
  2257. // Acknowledge the over current change bit if it is set.
  2258. //
  2259. if ((HardwareStatus & EHCI_PORT_OVER_CURRENT_CHANGE) != 0) {
  2260. PortStatus->Change |= USB_PORT_STATUS_CHANGE_OVER_CURRENT;
  2261. EHCI_WRITE_PORT_REGISTER(Controller, PortIndex, HardwareStatus);
  2262. }
  2263. //
  2264. // Acknowledge the port connection status change in the hardware and
  2265. // set the bit in the software's port status change bits. It may be
  2266. // that the port transitioned from connected to connected and the above
  2267. // checks did not pick up the change.
  2268. //
  2269. if ((HardwareStatus & EHCI_PORT_CONNECT_STATUS_CHANGE) != 0) {
  2270. PortStatus->Change |= USB_PORT_STATUS_CHANGE_CONNECTED;
  2271. //
  2272. // If the port is not in the middle of a reset, clear the connect
  2273. // status change bit in the hardware by setting it to 1. Resets
  2274. // clear the connect status changed bit.
  2275. //
  2276. if ((HardwareStatus & EHCI_PORT_RESET) == 0) {
  2277. EHCI_WRITE_PORT_REGISTER(Controller, PortIndex, HardwareStatus);
  2278. }
  2279. }
  2280. if ((EhciDebugFlags & EHCI_DEBUG_PORTS) != 0) {
  2281. RtlDebugPrint(
  2282. "EHCI: Controller 0x%x Port %d Status 0x%x. "
  2283. "Connected %d, Owner %d, Enabled %d, Reset %d, "
  2284. "Changed %d.\n",
  2285. Controller,
  2286. PortIndex,
  2287. HardwareStatus,
  2288. (HardwareStatus & EHCI_PORT_CONNECT_STATUS) != 0,
  2289. (HardwareStatus & EHCI_PORT_OWNER) != 0,
  2290. (HardwareStatus & EHCI_PORT_ENABLE) != 0,
  2291. (HardwareStatus & EHCI_PORT_RESET) != 0,
  2292. (HardwareStatus & EHCI_PORT_CONNECT_STATUS_CHANGE) != 0);
  2293. }
  2294. }
  2295. return STATUS_SUCCESS;
  2296. }
  2297. KSTATUS
  2298. EhcipSetRootHubStatus (
  2299. PVOID HostControllerContext,
  2300. PUSB_HUB_STATUS HubStatus
  2301. )
  2302. /*++
  2303. Routine Description:
  2304. This routine sets the state of the root hub in the USB host controller. It
  2305. looks at the status change bits for each port in order to determine what
  2306. needs to be set.
  2307. Arguments:
  2308. HostControllerContext - Supplies the context pointer passed to the USB core
  2309. when the controller was created. This is used to identify the USB host
  2310. controller to the host controller driver.
  2311. HubStatus - Supplies a pointer to the status that should be set in the root
  2312. hub.
  2313. Return Value:
  2314. STATUS_SUCCESS if the hub state was successfully programmed into the device.
  2315. Failure codes if the status could not be set.
  2316. --*/
  2317. {
  2318. PEHCI_CONTROLLER Controller;
  2319. USHORT HardwareStatus;
  2320. USHORT OriginalHardwareStatus;
  2321. ULONG PortIndex;
  2322. PUSB_PORT_STATUS PortStatus;
  2323. Controller = (PEHCI_CONTROLLER)HostControllerContext;
  2324. ASSERT(Controller->PortCount != 0);
  2325. //
  2326. // The supplied hub status has change bits indicate what is to be newly set
  2327. // in each port's software status. This routine will clear any change bits
  2328. // it handles.
  2329. //
  2330. for (PortIndex = 0; PortIndex < Controller->PortCount; PortIndex += 1) {
  2331. //
  2332. // The caller is required to notify the routine about what needs to be
  2333. // set by updating the change bits. If there are not changed bits, then
  2334. // skip the port.
  2335. //
  2336. PortStatus = &(HubStatus->PortStatus[PortIndex]);
  2337. if (PortStatus->Change == 0) {
  2338. continue;
  2339. }
  2340. OriginalHardwareStatus = EHCI_READ_PORT_REGISTER(Controller, PortIndex);
  2341. HardwareStatus = OriginalHardwareStatus;
  2342. //
  2343. // Leave the port alone if it's not owned by EHCI and there isn't an
  2344. // active reset.
  2345. //
  2346. if (((HardwareStatus & EHCI_PORT_OWNER) != 0) &&
  2347. ((PortStatus->Status & USB_PORT_STATUS_RESET) == 0)) {
  2348. //
  2349. // Clear any change bits that this routine would otherwise handle.
  2350. // This acknowledges that they were dealt with (i.e. this port is
  2351. // dead and there is nothing anyone else should do with the change
  2352. // bits later).
  2353. //
  2354. PortStatus->Change &= ~(USB_PORT_STATUS_CHANGE_RESET |
  2355. USB_PORT_STATUS_CHANGE_ENABLED |
  2356. USB_PORT_STATUS_CHANGE_SUSPENDED);
  2357. continue;
  2358. }
  2359. //
  2360. // Clear out the bits that may potentially be adjusted.
  2361. //
  2362. HardwareStatus &= ~(EHCI_PORT_ENABLE | EHCI_PORT_RESET |
  2363. EHCI_PORT_SUSPEND | EHCI_PORT_INDICATOR_MASK |
  2364. EHCI_PORT_OWNER);
  2365. //
  2366. // Set the hardware bits according to what's passed in.
  2367. //
  2368. if ((PortStatus->Change & USB_PORT_STATUS_CHANGE_ENABLED) != 0) {
  2369. //
  2370. // If the port is being enabled, then set the enabled bits, power
  2371. // it on and turn on the green indicator.
  2372. //
  2373. if ((PortStatus->Status & USB_PORT_STATUS_ENABLED) != 0) {
  2374. HardwareStatus |= EHCI_PORT_ENABLE |
  2375. EHCI_PORT_INDICATOR_GREEN |
  2376. EHCI_PORT_POWER;
  2377. }
  2378. //
  2379. // Acknowledge that the enable bit was handled.
  2380. //
  2381. PortStatus->Change &= ~USB_PORT_STATUS_CHANGE_ENABLED;
  2382. }
  2383. //
  2384. // The EHCI spec says that whenever the reset bit is set, the enable
  2385. // bit must be cleared. If the port is high speed, the enable bit will
  2386. // be set automatically once the reset completes.
  2387. //
  2388. if ((PortStatus->Change & USB_PORT_STATUS_CHANGE_RESET) != 0) {
  2389. if ((PortStatus->Status & USB_PORT_STATUS_RESET) != 0) {
  2390. HardwareStatus |= EHCI_PORT_RESET;
  2391. HardwareStatus &= ~EHCI_PORT_ENABLE;
  2392. }
  2393. //
  2394. // Acknowledge that the reset bit was handled.
  2395. //
  2396. PortStatus->Change &= ~USB_PORT_STATUS_CHANGE_RESET;
  2397. }
  2398. //
  2399. // Suspend the port if requested.
  2400. //
  2401. if ((PortStatus->Change & USB_PORT_STATUS_CHANGE_SUSPENDED) != 0) {
  2402. if ((PortStatus->Status & USB_PORT_STATUS_SUSPENDED) != 0) {
  2403. HardwareStatus |= EHCI_PORT_SUSPEND;
  2404. }
  2405. PortStatus->Change &= ~USB_PORT_STATUS_CHANGE_SUSPENDED;
  2406. }
  2407. //
  2408. // Write out the new value if it is different than the old one.
  2409. //
  2410. if (HardwareStatus != OriginalHardwareStatus) {
  2411. EHCI_WRITE_PORT_REGISTER(Controller, PortIndex, HardwareStatus);
  2412. }
  2413. //
  2414. // If reset was set, wait the required amount of time and then clear
  2415. // the reset bit, as if this were a hub and it was cleared
  2416. // automatically.
  2417. //
  2418. if ((HardwareStatus & EHCI_PORT_RESET) != 0) {
  2419. HlBusySpin(20 * 1000);
  2420. HardwareStatus = EHCI_READ_PORT_REGISTER(Controller, PortIndex);
  2421. HardwareStatus &= ~EHCI_PORT_RESET;
  2422. EHCI_WRITE_PORT_REGISTER(Controller, PortIndex, HardwareStatus);
  2423. //
  2424. // Wait a further 5ms (the EHCI spec says the host controller has
  2425. // to have it done in 2ms), and if the port is not enabled, then
  2426. // it's a full speed device, and should be handed off to the
  2427. // companion controller.
  2428. //
  2429. HlBusySpin(5 * 1000);
  2430. HardwareStatus = EHCI_READ_PORT_REGISTER(Controller, PortIndex);
  2431. if ((HardwareStatus & EHCI_PORT_ENABLE) == 0) {
  2432. HardwareStatus |= EHCI_PORT_OWNER;
  2433. EHCI_WRITE_PORT_REGISTER(Controller, PortIndex, HardwareStatus);
  2434. }
  2435. }
  2436. }
  2437. return STATUS_SUCCESS;
  2438. }
  2439. RUNLEVEL
  2440. EhcipAcquireControllerLock (
  2441. PEHCI_CONTROLLER Controller
  2442. )
  2443. /*++
  2444. Routine Description:
  2445. This routine acquires the given EHCI controller's lock at dispatch level.
  2446. Arguments:
  2447. Controller - Supplies a pointer to the controller to lock.
  2448. Return Value:
  2449. Returns the previous run-level, which must be passed in when the controller
  2450. is unlocked.
  2451. --*/
  2452. {
  2453. RUNLEVEL OldRunLevel;
  2454. OldRunLevel = KeRaiseRunLevel(RunLevelDispatch);
  2455. KeAcquireSpinLock(&(Controller->Lock));
  2456. return OldRunLevel;
  2457. }
  2458. VOID
  2459. EhcipReleaseControllerLock (
  2460. PEHCI_CONTROLLER Controller,
  2461. RUNLEVEL OldRunLevel
  2462. )
  2463. /*++
  2464. Routine Description:
  2465. This routine releases the given EHCI controller's lock, and returns the
  2466. run-level to its previous value.
  2467. Arguments:
  2468. Controller - Supplies a pointer to the controller to unlock.
  2469. OldRunLevel - Supplies the original run level returned when the lock was
  2470. acquired.
  2471. Return Value:
  2472. None.
  2473. --*/
  2474. {
  2475. KeReleaseSpinLock(&(Controller->Lock));
  2476. KeLowerRunLevel(OldRunLevel);
  2477. return;
  2478. }
  2479. VOID
  2480. EhcipProcessInterrupt (
  2481. PEHCI_CONTROLLER Controller,
  2482. ULONG PendingStatusBits
  2483. )
  2484. /*++
  2485. Routine Description:
  2486. This routine performs the work associated with receiving an EHCI interrupt.
  2487. This routine runs at dispatch level.
  2488. Arguments:
  2489. Controller - Supplies a pointer to the controller.
  2490. PendingStatusBits - Supplies the pending status bits to service.
  2491. Return Value:
  2492. None.
  2493. --*/
  2494. {
  2495. PLIST_ENTRY CurrentEntry;
  2496. PEHCI_TRANSFER NextTransfer;
  2497. RUNLEVEL OldRunLevel;
  2498. BOOL RemoveSet;
  2499. PEHCI_TRANSFER Transfer;
  2500. PEHCI_TRANSFER_SET TransferSet;
  2501. //
  2502. // Lock the controller and loop until this routine has caught up with the
  2503. // interrupts.
  2504. //
  2505. OldRunLevel = EhcipAcquireControllerLock(Controller);
  2506. //
  2507. // If the interrupt was a device change interrupt, then notify the USB core
  2508. // that the root hub noticed a device change.
  2509. //
  2510. if ((PendingStatusBits & EHCI_STATUS_PORT_CHANGE_DETECT) != 0) {
  2511. UsbHostNotifyPortChange(Controller->UsbCoreHandle);
  2512. }
  2513. //
  2514. // TODO: Go through the isochronous transfers.
  2515. //
  2516. ASSERT(LIST_EMPTY(&(Controller->IsochronousTransferListHead)) != FALSE);
  2517. //
  2518. // Loop through every transfer in the schedule.
  2519. //
  2520. CurrentEntry = Controller->TransferListHead.Next;
  2521. while (CurrentEntry != &(Controller->TransferListHead)) {
  2522. ASSERT((CurrentEntry != NULL) && (CurrentEntry->Next != NULL));
  2523. Transfer = LIST_VALUE(CurrentEntry, EHCI_TRANSFER, GlobalListEntry);
  2524. CurrentEntry = CurrentEntry->Next;
  2525. RemoveSet = EhcipProcessPotentiallyCompletedTransfer(Transfer);
  2526. if (RemoveSet != FALSE) {
  2527. //
  2528. // Get the current entry off of this set, as several transfers
  2529. // may be removed here.
  2530. //
  2531. TransferSet = Transfer->Set;
  2532. if (CurrentEntry != &(Controller->TransferListHead)) {
  2533. NextTransfer = LIST_VALUE(CurrentEntry,
  2534. EHCI_TRANSFER,
  2535. GlobalListEntry);
  2536. while (NextTransfer->Set == TransferSet) {
  2537. CurrentEntry = CurrentEntry->Next;
  2538. if (CurrentEntry == &(Controller->TransferListHead)) {
  2539. break;
  2540. }
  2541. NextTransfer = LIST_VALUE(CurrentEntry,
  2542. EHCI_TRANSFER,
  2543. GlobalListEntry);
  2544. }
  2545. }
  2546. //
  2547. // Remove the transfer set from the owning endpoint's queue and
  2548. // call the completion routine.
  2549. //
  2550. EhcipRemoveCompletedTransferSet(Controller, TransferSet);
  2551. UsbHostProcessCompletedTransfer(TransferSet->UsbTransfer);
  2552. }
  2553. }
  2554. //
  2555. // If the interrupt was the "interrupt on asynchronous schedule advance"
  2556. // doorbell, then process the ready list, knowing that hardware is no
  2557. // longer using it. Run this after processing all the transfers in case a
  2558. // transfer finished before any of the queues were removed.
  2559. //
  2560. if ((PendingStatusBits & EHCI_STATUS_INTERRUPT_ON_ASYNC_ADVANCE) != 0) {
  2561. EhcipProcessAsyncOnAdvanceInterrupt(Controller);
  2562. }
  2563. //
  2564. // Release the lock.
  2565. //
  2566. EhcipReleaseControllerLock(Controller, OldRunLevel);
  2567. return;
  2568. }
  2569. VOID
  2570. EhcipFillOutTransferDescriptor (
  2571. PEHCI_CONTROLLER Controller,
  2572. PEHCI_TRANSFER EhciTransfer,
  2573. ULONG Offset,
  2574. ULONG Length,
  2575. BOOL LastTransfer,
  2576. PBOOL DataToggle,
  2577. PEHCI_TRANSFER AlternateNextTransfer
  2578. )
  2579. /*++
  2580. Routine Description:
  2581. This routine fills out an EHCI transfer descriptor.
  2582. Arguments:
  2583. Controller - Supplies a pointer to the EHCI controller.
  2584. EhciTransfer - Supplies a pointer to EHCI's transfer descriptor information.
  2585. Offset - Supplies the offset from the public transfer physical address that
  2586. this transfer descriptor should be initialize to.
  2587. Length - Supplies the length of the transfer, in bytes.
  2588. LastTransfer - Supplies a boolean indicating if this transfer descriptor
  2589. represents the last transfer in a set. For control transfers, this is
  2590. the status phase where the in/out is reversed and the length had better
  2591. be zero.
  2592. DataToggle - Supplies a pointer to a boolean that indicates the current
  2593. data toggle status for the overall transfer. This routine will update
  2594. the data toggle upon return to indicate what the data toggle should be
  2595. for the next transfer to be initialized.
  2596. AlternateNextTransfer - Supplies an optional pointer to a transfer to move
  2597. to if this transfer is an IN and comes up short of its max transfer
  2598. length.
  2599. Return Value:
  2600. None.
  2601. --*/
  2602. {
  2603. ULONG BufferIndex;
  2604. ULONG BufferPhysical;
  2605. ULONG EndAddress;
  2606. PEHCI_ENDPOINT Endpoint;
  2607. PEHCI_TRANSFER_DESCRIPTOR HardwareTransfer;
  2608. ULONG Token;
  2609. PUSB_TRANSFER_INTERNAL Transfer;
  2610. PEHCI_TRANSFER_SET TransferSet;
  2611. TransferSet = EhciTransfer->Set;
  2612. Endpoint = TransferSet->Endpoint;
  2613. Transfer = TransferSet->UsbTransfer;
  2614. EhciTransfer->LastTransfer = FALSE;
  2615. HardwareTransfer = EhciTransfer->HardwareTransfer;
  2616. //
  2617. // Set up the buffer pointers.
  2618. //
  2619. BufferPhysical = Transfer->Public.BufferPhysicalAddress + Offset;
  2620. EndAddress = BufferPhysical + Length;
  2621. ASSERT((REMAINDER(BufferPhysical, EHCI_PAGE_SIZE) + Length) <=
  2622. EHCI_TRANSFER_MAX_PACKET_SIZE);
  2623. for (BufferIndex = 0;
  2624. BufferIndex < EHCI_TRANSFER_POINTER_COUNT;
  2625. BufferIndex += 1) {
  2626. if (BufferPhysical < EndAddress) {
  2627. HardwareTransfer->BufferPointer[BufferIndex] = BufferPhysical;
  2628. BufferPhysical += EHCI_PAGE_SIZE;
  2629. BufferPhysical = ALIGN_RANGE_DOWN(BufferPhysical, EHCI_PAGE_SIZE);
  2630. } else {
  2631. HardwareTransfer->BufferPointer[BufferIndex] = 0;
  2632. }
  2633. HardwareTransfer->BufferAddressHigh[BufferIndex] = 0;
  2634. }
  2635. //
  2636. // Figure out the token value for this transfer descriptor.
  2637. //
  2638. EhciTransfer->TransferLength = Length;
  2639. Token = (Length << EHCI_TRANSFER_TOTAL_BYTES_SHIFT);
  2640. Token |= EHCI_TRANSFER_3_ERRORS_ALLOWED;
  2641. Token |= EHCI_TRANSFER_STATUS_ACTIVE;
  2642. //
  2643. // The first packet in a control transfer is always a setup packet. It does
  2644. // not have the data toggle set, but prepares for the next transfer to have
  2645. // the bit set by setting the data toggle to true.
  2646. //
  2647. if ((Endpoint->TransferType == UsbTransferTypeControl) && (Offset == 0)) {
  2648. Token |= EHCI_TRANSFER_PID_CODE_SETUP;
  2649. *DataToggle = TRUE;
  2650. //
  2651. // Do it backwards if this is the status phase. Status phases always have
  2652. // a data toggle of 1. The data toggle boolean does not need to be updated
  2653. // as this is always the last transfer.
  2654. //
  2655. } else if ((Endpoint->TransferType == UsbTransferTypeControl) &&
  2656. (LastTransfer != FALSE)) {
  2657. Token |= EHCI_TRANSFER_DATA_TOGGLE;
  2658. ASSERT((Length == 0) &&
  2659. (Endpoint->TransferType == UsbTransferTypeControl));
  2660. if (Transfer->Public.Direction == UsbTransferDirectionIn) {
  2661. Token |= EHCI_TRANSFER_PID_CODE_OUT;
  2662. } else {
  2663. ASSERT(Transfer->Public.Direction == UsbTransferDirectionOut);
  2664. Token |= EHCI_TRANSFER_PID_CODE_IN;
  2665. }
  2666. //
  2667. // Not setup and not status, fill this out like a normal descriptor.
  2668. //
  2669. } else {
  2670. if (Transfer->Public.Direction == UsbTransferDirectionIn) {
  2671. Token |= EHCI_TRANSFER_PID_CODE_IN;
  2672. } else {
  2673. ASSERT(Transfer->Public.Direction == UsbTransferDirectionOut);
  2674. Token |= EHCI_TRANSFER_PID_CODE_OUT;
  2675. }
  2676. //
  2677. // The host controller keeps track of the data toggle bits for control
  2678. // transfers (rather than the hardware), so set the data toggle bit
  2679. // accordingly and update the data toggle boolean for the next
  2680. // transfer.
  2681. //
  2682. if (Endpoint->TransferType == UsbTransferTypeControl) {
  2683. if (*DataToggle != FALSE) {
  2684. Token |= EHCI_TRANSFER_DATA_TOGGLE;
  2685. *DataToggle = FALSE;
  2686. } else {
  2687. *DataToggle = TRUE;
  2688. }
  2689. }
  2690. }
  2691. ASSERT((Endpoint->Speed == UsbDeviceSpeedLow) ||
  2692. (Endpoint->Speed == UsbDeviceSpeedFull) ||
  2693. (Endpoint->Speed == UsbDeviceSpeedHigh));
  2694. //
  2695. // Don't set the interrupt flag if 1) This is not the last descriptor or
  2696. // 2) The caller requested not to.
  2697. //
  2698. if ((LastTransfer != FALSE) &&
  2699. ((Transfer->Public.Flags &
  2700. USB_TRANSFER_FLAG_NO_INTERRUPT_ON_COMPLETION) == 0)) {
  2701. Token |= EHCI_TRANSFER_INTERRUPT_ON_COMPLETE;
  2702. }
  2703. HardwareTransfer->Token = Token;
  2704. if ((EhciDebugFlags & EHCI_DEBUG_TRANSFERS) != 0) {
  2705. RtlDebugPrint("EHCI: Adding transfer (0x%08x) PA 0x%I64x to endpoint "
  2706. "(0x%08x): Token 0x%08x.\n",
  2707. EhciTransfer,
  2708. EhciTransfer->PhysicalAddress,
  2709. Endpoint,
  2710. EhciTransfer->HardwareTransfer->Token);
  2711. }
  2712. //
  2713. // Set up the link pointers of the transfer descriptor. With the exception
  2714. // of isochronous transfers (which will get patched up later) transfer
  2715. // descriptors are always put at the end of the queue. They confusingly
  2716. // point back to the first transfer because the first transfer will
  2717. // eventually get swapped out to be a dummy last transfer. That fact is
  2718. // anticipated here so that now all transfers lead to the dummy at the end.
  2719. //
  2720. HardwareTransfer->NextTransfer =
  2721. (ULONG)(TransferSet->Transfer[0]->PhysicalAddress);
  2722. if ((AlternateNextTransfer != NULL) &&
  2723. (AlternateNextTransfer != EhciTransfer)) {
  2724. ASSERT((ULONG)AlternateNextTransfer->PhysicalAddress ==
  2725. AlternateNextTransfer->PhysicalAddress);
  2726. HardwareTransfer->AlternateNextTransfer =
  2727. (ULONG)AlternateNextTransfer->PhysicalAddress;
  2728. } else {
  2729. //
  2730. // Point the next transfer to what will become the end of this set, so
  2731. // that if a short packet comes in this transfer set will be done and
  2732. // the queue moves to the next set of transfers.
  2733. //
  2734. HardwareTransfer->AlternateNextTransfer =
  2735. (ULONG)(TransferSet->Transfer[0]->PhysicalAddress);
  2736. }
  2737. if (Transfer->Type == UsbTransferTypeIsochronous) {
  2738. //
  2739. // TODO: Implement isochronous transfers.
  2740. //
  2741. ASSERT(FALSE);
  2742. }
  2743. return;
  2744. }
  2745. VOID
  2746. EhcipLinkTransferSetInHardware (
  2747. PEHCI_TRANSFER_SET TransferSet
  2748. )
  2749. /*++
  2750. Routine Description:
  2751. This routine links a set of transfer descriptors up to their proper queue
  2752. head, making them visible to the hardware. This routine assumes the
  2753. controller lock is already held.
  2754. Arguments:
  2755. TransferSet - Supplies a pointer to the transfer set.
  2756. Return Value:
  2757. None.
  2758. --*/
  2759. {
  2760. PEHCI_ENDPOINT Endpoint;
  2761. PEHCI_TRANSFER OriginalDummyTransfer;
  2762. PEHCI_TRANSFER OriginalFirstTransfer;
  2763. PEHCI_QUEUE_HEAD QueueHead;
  2764. ULONG RemainingSize;
  2765. ULONG Token;
  2766. Endpoint = TransferSet->Endpoint;
  2767. //
  2768. // TODO: Implement support for isochronous.
  2769. //
  2770. ASSERT(Endpoint->TransferType != UsbTransferTypeIsochronous);
  2771. OriginalDummyTransfer = Endpoint->Queue.DummyTransfer;
  2772. OriginalFirstTransfer = TransferSet->Transfer[0];
  2773. ASSERT((OriginalDummyTransfer->HardwareTransfer->Token &
  2774. EHCI_TRANSFER_STATUS_HALTED) != 0);
  2775. //
  2776. // The way this is going to work is to not actually use the first transfer
  2777. // of the set, but to copy it into the dummy transfer that's already on the
  2778. // hardware list. That dummy transfer becomes the first transfer of the set,
  2779. // and the original first transfer becomes the new dummy. Begin by saving
  2780. // the original first transfer's token.
  2781. //
  2782. Token = OriginalFirstTransfer->HardwareTransfer->Token;
  2783. OriginalFirstTransfer->HardwareTransfer->Token =
  2784. OriginalDummyTransfer->HardwareTransfer->Token;
  2785. //
  2786. // Copy the remainder of the original first transfer over the dummy, but
  2787. // make sure it stays inactive so the hardware doesn't look at it.
  2788. //
  2789. OriginalDummyTransfer->HardwareTransfer->NextTransfer =
  2790. OriginalFirstTransfer->HardwareTransfer->NextTransfer;
  2791. OriginalDummyTransfer->HardwareTransfer->AlternateNextTransfer =
  2792. OriginalFirstTransfer->HardwareTransfer->AlternateNextTransfer;
  2793. RemainingSize = sizeof(EHCI_TRANSFER_DESCRIPTOR) -
  2794. FIELD_OFFSET(EHCI_TRANSFER_DESCRIPTOR, BufferPointer);
  2795. RtlCopyMemory(&(OriginalDummyTransfer->HardwareTransfer->BufferPointer),
  2796. &(OriginalFirstTransfer->HardwareTransfer->BufferPointer),
  2797. RemainingSize);
  2798. ASSERT((OriginalDummyTransfer->EndpointListEntry.Next == NULL) &&
  2799. (OriginalDummyTransfer->GlobalListEntry.Next == NULL) &&
  2800. (OriginalFirstTransfer->EndpointListEntry.Next != NULL) &&
  2801. (OriginalFirstTransfer->GlobalListEntry.Next != NULL));
  2802. //
  2803. // Add the dummy transfer to the software lists, and remove the original
  2804. // first transfer.
  2805. //
  2806. INSERT_BEFORE(&(OriginalDummyTransfer->EndpointListEntry),
  2807. &(OriginalFirstTransfer->EndpointListEntry));
  2808. INSERT_BEFORE(&(OriginalDummyTransfer->GlobalListEntry),
  2809. &(OriginalFirstTransfer->GlobalListEntry));
  2810. LIST_REMOVE(&(OriginalFirstTransfer->GlobalListEntry));
  2811. LIST_REMOVE(&(OriginalFirstTransfer->EndpointListEntry));
  2812. OriginalFirstTransfer->EndpointListEntry.Next = NULL;
  2813. OriginalFirstTransfer->GlobalListEntry.Next = NULL;
  2814. //
  2815. // Copy over any other aspects.
  2816. //
  2817. OriginalFirstTransfer->HardwareTransfer->NextTransfer = EHCI_LINK_TERMINATE;
  2818. OriginalFirstTransfer->HardwareTransfer->AlternateNextTransfer =
  2819. EHCI_LINK_TERMINATE;
  2820. OriginalDummyTransfer->TransferLength =
  2821. OriginalFirstTransfer->TransferLength;
  2822. OriginalFirstTransfer->TransferLength = 0;
  2823. OriginalDummyTransfer->LastTransfer = OriginalFirstTransfer->LastTransfer;
  2824. OriginalFirstTransfer->LastTransfer = FALSE;
  2825. //
  2826. // Switch their roles.
  2827. //
  2828. TransferSet->Transfer[0] = OriginalDummyTransfer;
  2829. OriginalFirstTransfer->Set = NULL;
  2830. OriginalDummyTransfer->Set = TransferSet;
  2831. Endpoint->Queue.DummyTransfer = OriginalFirstTransfer;
  2832. //
  2833. // Make everything live by setting the token in the new first transfer.
  2834. // Use the register write function to ensure the compiler does this in a
  2835. // single write (and not something goofy like byte by byte). This routine
  2836. // also serves as a full memory barrier.
  2837. //
  2838. QueueHead = Endpoint->Queue.HardwareQueueHead;
  2839. HlWriteRegister32(&(OriginalDummyTransfer->HardwareTransfer->Token), Token);
  2840. //
  2841. // If the queue head was halted, it needs to be restarted. Zero out the
  2842. // current descriptor so nothing gets written back, set the next link to
  2843. // the start of the list, and zero out the token. Avoid the very rare
  2844. // situation where the hardware got all the way through the transfers
  2845. // linked in the previous line (and has an errata where the halted
  2846. // descriptor is copied into the overlay).
  2847. //
  2848. if (((QueueHead->TransferOverlay.Token &
  2849. EHCI_TRANSFER_STATUS_HALTED) != 0) &&
  2850. (QueueHead->CurrentTransferDescriptorLink !=
  2851. (ULONG)(OriginalFirstTransfer->PhysicalAddress))) {
  2852. QueueHead->CurrentTransferDescriptorLink = 0;
  2853. QueueHead->TransferOverlay.NextTransfer =
  2854. OriginalDummyTransfer->PhysicalAddress;
  2855. HlWriteRegister32(
  2856. &(QueueHead->TransferOverlay.Token),
  2857. QueueHead->TransferOverlay.Token & EHCI_TRANSFER_DATA_TOGGLE);
  2858. }
  2859. return;
  2860. }
  2861. BOOL
  2862. EhcipProcessPotentiallyCompletedTransfer (
  2863. PEHCI_TRANSFER Transfer
  2864. )
  2865. /*++
  2866. Routine Description:
  2867. This routine processes a transfer descriptor, adjusting the USB transfer if
  2868. the transfer descriptor errored out.
  2869. Arguments:
  2870. Transfer - Supplies a pointer to the transfer to evaluate.
  2871. Return Value:
  2872. TRUE if the transfer set should be removed from the list because the
  2873. transfer has failed.
  2874. FALSE if the transfer set should not be removed from the list.
  2875. --*/
  2876. {
  2877. ULONG HardwareStatus;
  2878. ULONG LengthTransferred;
  2879. PEHCI_QUEUE_HEAD QueueHead;
  2880. BOOL RemoveSet;
  2881. PUSB_TRANSFER UsbTransfer;
  2882. RemoveSet = FALSE;
  2883. //
  2884. // Skip the transfer if it's already been dealt with.
  2885. //
  2886. if (Transfer->GlobalListEntry.Next == NULL) {
  2887. goto ProcessPotentiallyCompletedTransferEnd;
  2888. }
  2889. HardwareStatus = Transfer->HardwareTransfer->Token;
  2890. if ((HardwareStatus & EHCI_TRANSFER_STATUS_ACTIVE) == 0) {
  2891. if ((EhciDebugFlags & EHCI_DEBUG_TRANSFERS) != 0) {
  2892. RtlDebugPrint("EHCI: Transfer (0x%08x) PA 0x%I64x completed with "
  2893. "token 0x%08x\n",
  2894. Transfer,
  2895. Transfer->PhysicalAddress,
  2896. HardwareStatus);
  2897. }
  2898. LIST_REMOVE(&(Transfer->EndpointListEntry));
  2899. Transfer->EndpointListEntry.Next = NULL;
  2900. LIST_REMOVE(&(Transfer->GlobalListEntry));
  2901. Transfer->GlobalListEntry.Next = NULL;
  2902. LengthTransferred = Transfer->TransferLength -
  2903. ((HardwareStatus &
  2904. EHCI_TRANSFER_TOTAL_BYTES_MASK) >>
  2905. EHCI_TRANSFER_TOTAL_BYTES_SHIFT);
  2906. UsbTransfer = &(Transfer->Set->UsbTransfer->Public);
  2907. UsbTransfer->LengthTransferred += LengthTransferred;
  2908. //
  2909. // If error bits were set, it's curtains for this transfer. Figure out
  2910. // exactly what went wrong. A halted error is first in line even if
  2911. // another bit (e.g. Babble) is set, because the driver may want to
  2912. // clear the halted state.
  2913. //
  2914. if ((HardwareStatus & EHCI_TRANSFER_ERROR_MASK) != 0) {
  2915. if (((EhciDebugFlags & EHCI_DEBUG_ERRORS) != 0) &&
  2916. ((EhciDebugFlags & EHCI_DEBUG_TRANSFERS) == 0)) {
  2917. RtlDebugPrint("EHCI: Transfer (0x%08x) PA 0x%I64x completed "
  2918. "with token 0x%08x\n",
  2919. Transfer,
  2920. Transfer->PhysicalAddress,
  2921. HardwareStatus);
  2922. }
  2923. RemoveSet = TRUE;
  2924. UsbTransfer->Status = STATUS_DEVICE_IO_ERROR;
  2925. if ((HardwareStatus & EHCI_TRANSFER_STATUS_HALTED) != 0) {
  2926. UsbTransfer->Error = UsbErrorTransferStalled;
  2927. //
  2928. // Clear out the current link so that when the next transfer
  2929. // set is linked in it won't get confused if this transfer
  2930. // is reused.
  2931. //
  2932. QueueHead = Transfer->Set->Endpoint->Queue.HardwareQueueHead;
  2933. QueueHead->CurrentTransferDescriptorLink = 0;
  2934. } else if ((HardwareStatus &
  2935. EHCI_TRANSFER_MISSED_MICRO_FRAME_ERROR) != 0) {
  2936. UsbTransfer->Error = UsbErrorTransferMissedMicroFrame;
  2937. } else if ((HardwareStatus &
  2938. EHCI_TRANSFER_TRANSACTION_ERROR) != 0) {
  2939. UsbTransfer->Error = UsbErrorTransferCrcOrTimeoutError;
  2940. } else if ((HardwareStatus & EHCI_TRANSFER_BABBLE_ERROR) != 0) {
  2941. UsbTransfer->Error = UsbErrorTransferBabbleDetected;
  2942. } else if ((HardwareStatus &
  2943. EHCI_TRANSFER_STATUS_DATA_BUFFER_ERROR) != 0) {
  2944. UsbTransfer->Error = UsbErrorTransferDataBuffer;
  2945. }
  2946. //
  2947. // Also check for short packets.
  2948. //
  2949. } else if ((LengthTransferred != Transfer->TransferLength) &&
  2950. ((UsbTransfer->Flags &
  2951. USB_TRANSFER_FLAG_NO_SHORT_TRANSFERS) != 0)) {
  2952. UsbTransfer->Status = STATUS_DATA_LENGTH_MISMATCH;
  2953. UsbTransfer->Error = UsbErrorShortPacket;
  2954. }
  2955. //
  2956. // If this is the last transfer, then signal that processing on this
  2957. // set is complete.
  2958. //
  2959. if ((Transfer->LastTransfer != FALSE) ||
  2960. (LengthTransferred != Transfer->TransferLength)) {
  2961. RemoveSet = TRUE;
  2962. }
  2963. }
  2964. ProcessPotentiallyCompletedTransferEnd:
  2965. return RemoveSet;
  2966. }
  2967. VOID
  2968. EhcipRemoveCompletedTransferSet (
  2969. PEHCI_CONTROLLER Controller,
  2970. PEHCI_TRANSFER_SET TransferSet
  2971. )
  2972. /*++
  2973. Routine Description:
  2974. This routine removes a completed transfer set from the schedule. This
  2975. routine assumes that the controller lock is already held.
  2976. Arguments:
  2977. Controller - Supplies a pointer to the controller being operated on.
  2978. TransferSet - Supplies a pointer to the set of transfers to remove.
  2979. Return Value:
  2980. None.
  2981. --*/
  2982. {
  2983. ULONG BackwardsTransferIndex;
  2984. PEHCI_TRANSFER EhciTransfer;
  2985. PEHCI_ENDPOINT Endpoint;
  2986. PEHCI_TRANSFER *TransferArray;
  2987. ULONG TransferIndex;
  2988. Endpoint = TransferSet->Endpoint;
  2989. //
  2990. // Isochronous transfers are handled differently.
  2991. //
  2992. if (Endpoint->TransferType == UsbTransferTypeIsochronous) {
  2993. ASSERT(FALSE);
  2994. goto RemoveCompletedTransferSetEnd;
  2995. }
  2996. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  2997. for (TransferIndex = 0;
  2998. TransferIndex < TransferSet->TransferCount;
  2999. TransferIndex += 1) {
  3000. BackwardsTransferIndex = TransferSet->TransferCount - 1 - TransferIndex;
  3001. EhciTransfer = TransferArray[BackwardsTransferIndex];
  3002. //
  3003. // Skip this transfer if it's done or otherwise not currently queued.
  3004. //
  3005. if (EhciTransfer->EndpointListEntry.Next == NULL) {
  3006. continue;
  3007. }
  3008. //
  3009. // Since the transfer set completed, all of the transfers are already
  3010. // out of the hardware's queue. Just remove them from the software
  3011. // list.
  3012. //
  3013. LIST_REMOVE(&(EhciTransfer->EndpointListEntry));
  3014. EhciTransfer->EndpointListEntry.Next = NULL;
  3015. ASSERT(EhciTransfer->GlobalListEntry.Next != NULL);
  3016. LIST_REMOVE(&(EhciTransfer->GlobalListEntry));
  3017. EhciTransfer->GlobalListEntry.Next = NULL;
  3018. }
  3019. RemoveCompletedTransferSetEnd:
  3020. //
  3021. // Transfer set has been removed. Mark that it is no longer queued.
  3022. //
  3023. TransferSet->Flags &= ~EHCI_TRANSFER_SET_FLAG_QUEUED;
  3024. return;
  3025. }
  3026. VOID
  3027. EhcipProcessAsyncOnAdvanceInterrupt (
  3028. PEHCI_CONTROLLER Controller
  3029. )
  3030. /*++
  3031. Routine Description:
  3032. This routine processes the queue heads that were waiting for an advance of
  3033. the asynchronous schedule. This routine assumes that the controller lock is
  3034. held.
  3035. Arguments:
  3036. Controller - Supplies a pointer to the controller being operated on.
  3037. Return Value:
  3038. None.
  3039. --*/
  3040. {
  3041. ULONG CommandRegister;
  3042. PLIST_ENTRY CurrentEntry;
  3043. PEHCI_TRANSFER EhciTransfer;
  3044. PEHCI_ENDPOINT Endpoint;
  3045. ULONG Flags;
  3046. ULONG HorizontalLink;
  3047. PEHCI_TRANSFER LastTransfer;
  3048. PEHCI_TRANSFER_QUEUE Queue;
  3049. PEHCI_TRANSFER_QUEUE QueueBefore;
  3050. LIST_ENTRY QueueListHead;
  3051. BOOL QueueWorkItem;
  3052. PEHCI_TRANSFER *TransferArray;
  3053. ULONG TransferIndex;
  3054. PEHCI_TRANSFER_SET TransferSet;
  3055. PUSB_TRANSFER UsbTransfer;
  3056. ASSERT(KeIsSpinLockHeld(&(Controller->Lock)) != FALSE);
  3057. //
  3058. // First transfer the list of queue heads that are ready to be processed to
  3059. // a local list. Be a bit defensive against spurious async advance
  3060. // interrupts (potentially caused by KD USB).
  3061. //
  3062. if (LIST_EMPTY(&(Controller->AsyncOnAdvanceReadyListHead)) != FALSE) {
  3063. INITIALIZE_LIST_HEAD(&QueueListHead);
  3064. } else {
  3065. MOVE_LIST(&(Controller->AsyncOnAdvanceReadyListHead), &QueueListHead);
  3066. }
  3067. INITIALIZE_LIST_HEAD(&(Controller->AsyncOnAdvanceReadyListHead));
  3068. //
  3069. // If the pending list is not empty, transfer it to the ready list and ring
  3070. // the doorbell.
  3071. //
  3072. if (LIST_EMPTY(&(Controller->AsyncOnAdvancePendingListHead)) == FALSE) {
  3073. MOVE_LIST(&(Controller->AsyncOnAdvancePendingListHead),
  3074. &(Controller->AsyncOnAdvanceReadyListHead));
  3075. INITIALIZE_LIST_HEAD(&(Controller->AsyncOnAdvancePendingListHead));
  3076. CommandRegister = Controller->CommandRegister;
  3077. CommandRegister |= EHCI_COMMAND_INTERRUPT_ON_ASYNC_ADVANCE;
  3078. EHCI_WRITE_REGISTER(Controller,
  3079. EhciRegisterUsbCommand,
  3080. CommandRegister);
  3081. }
  3082. //
  3083. // Now that the next doorbell is all set up, process the list of queue
  3084. // heads that have been fully removed from the hardware's grasp. There are
  3085. // two reasons for which a queue head can be removed. The first is if the
  3086. // endpoint is being removed. The second is if a transfer set in the queue
  3087. // was cancelled.
  3088. //
  3089. QueueWorkItem = FALSE;
  3090. while (LIST_EMPTY(&QueueListHead) == FALSE) {
  3091. Queue = LIST_VALUE(QueueListHead.Next, EHCI_TRANSFER_QUEUE, ListEntry);
  3092. LIST_REMOVE(&(Queue->ListEntry));
  3093. //
  3094. // If the queue has no async on advance context, then it's on the list
  3095. // in order to be destroyed. Add it to the list of queue heads to
  3096. // destroy. Note that a work item needs to be scheduled if this is the
  3097. // first entry on the list.
  3098. //
  3099. if (Queue->AsyncOnAdvanceCancel == FALSE) {
  3100. if (LIST_EMPTY(&(Controller->QueuesToDestroyListHead)) != FALSE) {
  3101. QueueWorkItem = TRUE;
  3102. }
  3103. INSERT_BEFORE(&(Queue->ListEntry),
  3104. &(Controller->QueuesToDestroyListHead));
  3105. //
  3106. // Otherwise the queue is here to remove one or more transfer sets that
  3107. // were cancelled.
  3108. //
  3109. } else {
  3110. Endpoint = PARENT_STRUCTURE(Queue, EHCI_ENDPOINT, Queue);
  3111. Queue->AsyncOnAdvanceCancel = FALSE;
  3112. ASSERT((Endpoint->TransferType == UsbTransferTypeControl) ||
  3113. (Endpoint->TransferType == UsbTransferTypeBulk));
  3114. CurrentEntry = Endpoint->TransferListHead.Next;
  3115. while (CurrentEntry != &(Endpoint->TransferListHead)) {
  3116. EhciTransfer = LIST_VALUE(CurrentEntry,
  3117. EHCI_TRANSFER,
  3118. EndpointListEntry);
  3119. //
  3120. // If the transfer set was not marked for cancelling, skip it.
  3121. //
  3122. TransferSet = EhciTransfer->Set;
  3123. Flags = TransferSet->Flags;
  3124. ASSERT((Flags & EHCI_TRANSFER_SET_FLAG_QUEUED) != 0);
  3125. if ((Flags & EHCI_TRANSFER_SET_FLAG_CANCELLING) == 0) {
  3126. CurrentEntry = CurrentEntry->Next;
  3127. continue;
  3128. }
  3129. //
  3130. // The next transfer to process is the next transfer after this
  3131. // set.
  3132. //
  3133. CurrentEntry = NULL;
  3134. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  3135. for (TransferIndex = 0;
  3136. TransferIndex < TransferSet->TransferCount;
  3137. TransferIndex += 1) {
  3138. if (TransferArray[TransferIndex]->LastTransfer != FALSE) {
  3139. LastTransfer = TransferArray[TransferIndex];
  3140. CurrentEntry = LastTransfer->EndpointListEntry.Next;
  3141. break;
  3142. }
  3143. }
  3144. ASSERT(CurrentEntry != NULL);
  3145. //
  3146. // Officially mark the transfer as cancelled, remove the
  3147. // transfer set and call the completion routine.
  3148. //
  3149. UsbTransfer = &(TransferSet->UsbTransfer->Public);
  3150. UsbTransfer->Status = STATUS_OPERATION_CANCELLED;
  3151. UsbTransfer->Error = UsbErrorTransferCancelled;
  3152. EhcipRemoveCancelledTransferSet(Controller, TransferSet);
  3153. UsbHostProcessCompletedTransfer(TransferSet->UsbTransfer);
  3154. }
  3155. //
  3156. // Now that all of the queue's cancelled transfer sets have been
  3157. // processed add it back to the asynchronous schedule.
  3158. //
  3159. QueueBefore = &(Controller->AsynchronousSchedule);
  3160. INSERT_AFTER(&(Queue->ListEntry), &(QueueBefore->ListEntry));
  3161. Queue->HardwareQueueHead->HorizontalLink =
  3162. QueueBefore->HardwareQueueHead->HorizontalLink;
  3163. HorizontalLink = (ULONG)Queue->PhysicalAddress;
  3164. ASSERT((HorizontalLink & (~EHCI_LINK_ADDRESS_MASK)) == 0);
  3165. HorizontalLink |= EHCI_LINK_TYPE_QUEUE_HEAD;
  3166. HlWriteRegister32(&(QueueBefore->HardwareQueueHead->HorizontalLink),
  3167. HorizontalLink);
  3168. }
  3169. }
  3170. //
  3171. // Queue the work item now if there is work to do.
  3172. //
  3173. if (QueueWorkItem != FALSE) {
  3174. KeQueueWorkItem(Controller->DestroyQueuesWorkItem);
  3175. }
  3176. return;
  3177. }
  3178. VOID
  3179. EhcipRemoveCancelledTransferSet (
  3180. PEHCI_CONTROLLER Controller,
  3181. PEHCI_TRANSFER_SET TransferSet
  3182. )
  3183. /*++
  3184. Routine Description:
  3185. This routine removes a cancelled transfer set from the schedule. This
  3186. routine assumes that the controller lock is already held.
  3187. Arguments:
  3188. Controller - Supplies a pointer to the controller being operated on.
  3189. TransferSet - Supplies a pointer to the set of transfers to remove.
  3190. Return Value:
  3191. None.
  3192. --*/
  3193. {
  3194. ULONG BackwardsTransferIndex;
  3195. PEHCI_TRANSFER EhciTransfer;
  3196. PEHCI_ENDPOINT Endpoint;
  3197. PEHCI_QUEUE_HEAD HardwareQueueHead;
  3198. PEHCI_TRANSFER_DESCRIPTOR HardwareTransfer;
  3199. PLIST_ENTRY NextEntry;
  3200. PEHCI_TRANSFER NextTransfer;
  3201. PLIST_ENTRY PreviousEntry;
  3202. PEHCI_TRANSFER PreviousTransfer;
  3203. PEHCI_TRANSFER_QUEUE Queue;
  3204. PEHCI_TRANSFER *TransferArray;
  3205. ULONG TransferIndex;
  3206. ASSERT(KeIsSpinLockHeld(&(Controller->Lock)) != FALSE);
  3207. Endpoint = TransferSet->Endpoint;
  3208. Queue = &(Endpoint->Queue);
  3209. TransferArray = (PEHCI_TRANSFER *)&(TransferSet->Transfer);
  3210. //
  3211. // Isochronous transfers are handled differently.
  3212. //
  3213. if (Endpoint->TransferType == UsbTransferTypeIsochronous) {
  3214. ASSERT(FALSE);
  3215. goto RemoveCancelledTransferSetEnd;
  3216. }
  3217. //
  3218. // Loop over all transfers in the set, removing any incomplete transfers.
  3219. //
  3220. NextEntry = NULL;
  3221. PreviousEntry = NULL;
  3222. for (TransferIndex = 0;
  3223. TransferIndex < TransferSet->TransferCount;
  3224. TransferIndex += 1) {
  3225. BackwardsTransferIndex = TransferSet->TransferCount - 1 - TransferIndex;
  3226. EhciTransfer = TransferArray[BackwardsTransferIndex];
  3227. //
  3228. // Skip this transfer if it's done or otherwise not currently queued.
  3229. //
  3230. if (EhciTransfer->EndpointListEntry.Next == NULL) {
  3231. continue;
  3232. }
  3233. //
  3234. // Record the transfer directly following the set. This is the next
  3235. // entry of the last transfer in the set.
  3236. //
  3237. if (EhciTransfer->LastTransfer != FALSE) {
  3238. NextEntry = EhciTransfer->EndpointListEntry.Next;
  3239. }
  3240. //
  3241. // Record the previous entry of the first transfer in the set that is
  3242. // still queued. This loop iterates backwards, so just record it every
  3243. // time.
  3244. //
  3245. PreviousEntry = EhciTransfer->EndpointListEntry.Previous;
  3246. //
  3247. // Either the previous entry is valid or this transfer was previously
  3248. // the first transfer in the queue.
  3249. //
  3250. ASSERT((PreviousEntry != &(Endpoint->TransferListHead)) ||
  3251. (Queue->HardwareQueueHead->CurrentTransferDescriptorLink == 0) ||
  3252. (Queue->HardwareQueueHead->CurrentTransferDescriptorLink ==
  3253. EhciTransfer->PhysicalAddress));
  3254. //
  3255. // Remove the transfer from the software lists. The endpoint's queue
  3256. // head is not in the schedule so the hardware transfer does not need
  3257. // to be modified - that is handled below.
  3258. //
  3259. LIST_REMOVE(&(EhciTransfer->EndpointListEntry));
  3260. EhciTransfer->EndpointListEntry.Next = NULL;
  3261. ASSERT(EhciTransfer->GlobalListEntry.Next != NULL);
  3262. LIST_REMOVE(&(EhciTransfer->GlobalListEntry));
  3263. EhciTransfer->GlobalListEntry.Next = NULL;
  3264. }
  3265. //
  3266. // Determine the next transfer in the queue after the set being removed.
  3267. // It could be the dummy transfer.
  3268. //
  3269. if ((NextEntry == NULL) || (NextEntry == &(Endpoint->TransferListHead))) {
  3270. NextTransfer = Queue->DummyTransfer;
  3271. } else {
  3272. NextTransfer = LIST_VALUE(NextEntry, EHCI_TRANSFER, EndpointListEntry);
  3273. }
  3274. //
  3275. // If there was a previous transfer in the queue, then point that at the
  3276. // next transfer.
  3277. //
  3278. if ((PreviousEntry != NULL) &&
  3279. (PreviousEntry != &(Endpoint->TransferListHead))) {
  3280. PreviousTransfer = LIST_VALUE(PreviousEntry,
  3281. EHCI_TRANSFER,
  3282. EndpointListEntry);
  3283. HardwareTransfer = PreviousTransfer->HardwareTransfer;
  3284. HlWriteRegister32(&(HardwareTransfer->NextTransfer),
  3285. (ULONG)(NextTransfer->PhysicalAddress));
  3286. HlWriteRegister32(&(HardwareTransfer->AlternateNextTransfer),
  3287. (ULONG)(NextTransfer->PhysicalAddress));
  3288. //
  3289. // Otherwise the queue head needs to be updated to grab the next transfer
  3290. // the next time is runs in the schedule.
  3291. //
  3292. } else {
  3293. HardwareQueueHead = Queue->HardwareQueueHead;
  3294. HardwareQueueHead->CurrentTransferDescriptorLink = 0;
  3295. HardwareQueueHead->TransferOverlay.NextTransfer =
  3296. (ULONG)(NextTransfer->PhysicalAddress);
  3297. HardwareQueueHead->TransferOverlay.AlternateNextTransfer =
  3298. (ULONG)(NextTransfer->PhysicalAddress);
  3299. }
  3300. RemoveCancelledTransferSetEnd:
  3301. //
  3302. // Transfer set has been removed. Mark that it is no longer queued.
  3303. //
  3304. TransferSet->Flags &= ~EHCI_TRANSFER_SET_FLAG_QUEUED;
  3305. return;
  3306. }
  3307. VOID
  3308. EhcipDestroyQueuesWorkRoutine (
  3309. PVOID Parameter
  3310. )
  3311. /*++
  3312. Routine Description:
  3313. This routine implements the queue head destruction work routine.
  3314. Arguments:
  3315. Parameter - Supplies an optional parameter passed in by the creator of the
  3316. work item. The EHCI controller context is supplied in this case.
  3317. Return Value:
  3318. None.
  3319. --*/
  3320. {
  3321. PEHCI_CONTROLLER Controller;
  3322. PEHCI_ENDPOINT Endpoint;
  3323. RUNLEVEL OldRunLevel;
  3324. PEHCI_TRANSFER_QUEUE Queue;
  3325. LIST_ENTRY QueueListHead;
  3326. Controller = (PEHCI_CONTROLLER)Parameter;
  3327. ASSERT(LIST_EMPTY(&(Controller->QueuesToDestroyListHead)) == FALSE);
  3328. //
  3329. // Acquire the controller lock and move all the queue heads that are
  3330. // awaiting removal to a local list.
  3331. //
  3332. OldRunLevel = EhcipAcquireControllerLock(Controller);
  3333. MOVE_LIST(&(Controller->QueuesToDestroyListHead), &QueueListHead);
  3334. INITIALIZE_LIST_HEAD(&(Controller->QueuesToDestroyListHead));
  3335. EhcipReleaseControllerLock(Controller, OldRunLevel);
  3336. //
  3337. // Iterate over the local list, destroying each queue head.
  3338. //
  3339. while (LIST_EMPTY(&QueueListHead) == FALSE) {
  3340. Queue = LIST_VALUE(QueueListHead.Next, EHCI_TRANSFER_QUEUE, ListEntry);
  3341. Endpoint = PARENT_STRUCTURE(Queue, EHCI_ENDPOINT, Queue);
  3342. LIST_REMOVE(&(Queue->ListEntry));
  3343. if (Queue->DummyTransfer != NULL) {
  3344. if (Queue->DummyTransfer->HardwareTransfer != NULL) {
  3345. MmFreeBlock(Controller->BlockAllocator,
  3346. Queue->DummyTransfer->HardwareTransfer);
  3347. }
  3348. MmFreeNonPagedPool(Queue->DummyTransfer);
  3349. }
  3350. if (Queue->HardwareQueueHead != NULL) {
  3351. MmFreeBlock(Controller->BlockAllocator, Queue->HardwareQueueHead);
  3352. }
  3353. MmFreeNonPagedPool(Endpoint);
  3354. }
  3355. return;
  3356. }