usbehci.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398
  1. /*
  2. * USB Enhanced Host Controller Interface (EHCI) driver
  3. * High speed USB 2.0.
  4. *
  5. * BUGS:
  6. * - Too many delays and ilocks.
  7. * - bandwidth admission control must be done per-frame.
  8. * - requires polling (some controllers miss interrupts).
  9. * - must warn of power overruns.
  10. */
  11. #include "u.h"
  12. #include "../port/lib.h"
  13. #include "mem.h"
  14. #include "dat.h"
  15. #include "fns.h"
  16. #include "io.h"
  17. #include "../port/error.h"
  18. #include "usb.h"
  19. #include "usbehci.h"
  20. typedef struct Ctlio Ctlio;
  21. typedef struct Ctlr Ctlr;
  22. typedef struct Itd Itd;
  23. typedef struct Sitd Sitd;
  24. typedef struct Qtd Qtd;
  25. typedef struct Td Td;
  26. typedef struct Qh Qh;
  27. typedef struct Fstn Fstn;
  28. typedef union Ed Ed;
  29. typedef struct Edpool Edpool;
  30. typedef struct Qio Qio;
  31. typedef struct Qtree Qtree;
  32. typedef struct Isoio Isoio;
  33. typedef struct Poll Poll;
  34. /*
  35. * EHCI interface registers and bits
  36. */
  37. enum {
  38. /* Queue states (software) */
  39. Qidle = 0,
  40. Qinstall,
  41. Qrun,
  42. Qdone,
  43. Qclose,
  44. Qfree,
  45. Enabledelay = 100, /* waiting for a port to enable */
  46. Abortdelay = 5, /* delay after cancelling Tds (ms) */
  47. Incr = 64, /* for pools of Tds, Qhs, etc. */
  48. Align = 128, /* in bytes for all those descriptors */
  49. /* Keep them as a power of 2, lower than ctlr->nframes */
  50. /* Also, keep Nisoframes >= Nintrleafs */
  51. Nintrleafs = 32, /* nb. of leaf frames in intr. tree */
  52. Nisoframes = 64, /* nb. of iso frames (in window) */
  53. /*
  54. * HW constants
  55. */
  56. /* Itd bits (csw[]) */
  57. Itdactive = 0x80000000, /* execution enabled */
  58. Itddberr = 0x40000000, /* data buffer error */
  59. Itdbabble = 0x20000000, /* babble error */
  60. Itdtrerr = 0x10000000, /* transaction error */
  61. Itdlenshift = 16, /* transaction length */
  62. Itdlenmask = 0xFFF,
  63. Itdioc = 0x00008000, /* interrupt on complete */
  64. Itdpgshift = 12, /* page select field */
  65. Itdoffshift = 0, /* transaction offset */
  66. /* Itd bits, buffer[] */
  67. Itdepshift = 8, /* endpoint address (buffer[0]) */
  68. Itddevshift = 0, /* device address (buffer[0]) */
  69. Itdin = 0x800, /* is input (buffer[1]) */
  70. Itdout = 0,
  71. Itdmaxpktshift = 0, /* max packet (buffer[1]) */
  72. Itdntdsshift = 0, /* nb. of tds per µframe (buffer[2]) */
  73. Itderrors = Itddberr|Itdbabble|Itdtrerr,
  74. /* Sitd bits (epc) */
  75. Stdin = 0x80000000, /* input direction */
  76. Stdportshift = 24, /* hub port number */
  77. Stdhubshift = 16, /* hub address */
  78. Stdepshift = 8, /* endpoint address */
  79. Stddevshift = 0, /* device address */
  80. /* Sitd bits (mfs) */
  81. Stdssmshift = 0, /* split start mask */
  82. Stdscmshift = 8, /* split complete mask */
  83. /* Sitd bits (csw) */
  84. Stdioc = 0x80000000, /* interrupt on complete */
  85. Stdpg = 0x40000000, /* page select */
  86. Stdlenshift = 16, /* total bytes to transfer */
  87. Stdlenmask = 0x3FF,
  88. Stdactive = 0x00000080, /* active */
  89. Stderr = 0x00000040, /* tr. translator error */
  90. Stddberr = 0x00000020, /* data buffer error */
  91. Stdbabble = 0x00000010, /* babble error */
  92. Stdtrerr = 0x00000008, /* transaction error */
  93. Stdmmf = 0x00000004, /* missed µframe */
  94. Stddcs = 0x00000002, /* do complete split */
  95. Stderrors = Stderr|Stddberr|Stdbabble|Stdtrerr|Stdmmf,
  96. /* Sitd bits buffer[1] */
  97. Stdtpall = 0x00000000, /* all payload here (188 bytes) */
  98. Stdtpbegin = 0x00000008, /* first payload for fs trans. */
  99. Stdtcntmask = 0x00000007, /* T-count */
  100. /* Td bits (csw) */
  101. Tddata1 = 0x80000000, /* data toggle 1 */
  102. Tddata0 = 0x00000000, /* data toggle 0 */
  103. Tdlenshift = 16, /* total bytes to transfer */
  104. Tdlenmask = 0x7FFF,
  105. Tdmaxpkt = 0x5000, /* max buffer for a Td */
  106. Tdioc = 0x00008000, /* interrupt on complete */
  107. Tdpgshift = 12, /* current page */
  108. Tdpgmask = 7,
  109. Tderr1 = 0x00000400, /* bit 0 of error counter */
  110. Tderr2 = 0x00000800, /* bit 1 of error counter */
  111. Tdtokout = 0x00000000, /* direction out */
  112. Tdtokin = 0x00000100, /* direction in */
  113. Tdtoksetup = 0x00000200, /* setup packet */
  114. Tdtok = 0x00000300, /* token bits */
  115. Tdactive = 0x00000080, /* active */
  116. Tdhalt = 0x00000040, /* halted */
  117. Tddberr = 0x00000020, /* data buffer error */
  118. Tdbabble = 0x00000010, /* babble error */
  119. Tdtrerr = 0x00000008, /* transaction error */
  120. Tdmmf = 0x00000004, /* missed µframe */
  121. Tddcs = 0x00000002, /* do complete split */
  122. Tdping = 0x00000001, /* do ping */
  123. Tderrors = Tdhalt|Tddberr|Tdbabble|Tdtrerr|Tdmmf,
  124. /* Qh bits (eps0) */
  125. Qhrlcmask = 0xF, /* nak reload count */
  126. Qhrlcshift = 28, /* nak reload count */
  127. Qhnhctl = 0x08000000, /* not-high speed ctl */
  128. Qhmplmask = 0x7FF, /* max packet */
  129. Qhmplshift = 16,
  130. Qhhrl = 0x00008000, /* head of reclamation list */
  131. Qhdtc = 0x00004000, /* data toggle ctl. */
  132. Qhint = 0x00000080, /* inactivate on next transition */
  133. Qhspeedmask = 0x00003000, /* speed bits */
  134. Qhfull = 0x00000000, /* full speed */
  135. Qhlow = 0x00001000, /* low speed */
  136. Qhhigh = 0x00002000, /* high speed */
  137. /* Qh bits (eps1) */
  138. Qhmultshift = 30, /* multiple tds per µframe */
  139. Qhmultmask = 3,
  140. Qhportshift = 23, /* hub port number */
  141. Qhhubshift = 16, /* hub address */
  142. Qhscmshift = 8, /* split completion mask bits */
  143. Qhismshift = 0, /* interrupt sched. mask bits */
  144. };
  145. /*
  146. * Endpoint tree (software)
  147. */
  148. struct Qtree {
  149. int nel;
  150. int depth;
  151. ulong* bw;
  152. Qh** root;
  153. };
  154. /*
  155. * One per endpoint per direction, to control I/O.
  156. */
  157. struct Qio {
  158. QLock; /* for the entire I/O process */
  159. Rendez; /* wait for completion */
  160. Qh* qh; /* Td list (field const after init) */
  161. int usbid; /* usb address for endpoint/device */
  162. int toggle; /* Tddata0/Tddata1 */
  163. int tok; /* Tdtoksetup, Tdtokin, Tdtokout */
  164. ulong iotime; /* last I/O time; to hold interrupt polls */
  165. int debug; /* debug flag from the endpoint */
  166. char* err; /* error string */
  167. char* tag; /* debug (no room in Qh for this) */
  168. ulong bw;
  169. };
  170. struct Ctlio {
  171. Qio; /* a single Qio for each RPC */
  172. uchar* data; /* read from last ctl req. */
  173. int ndata; /* number of bytes read */
  174. };
  175. struct Isoio {
  176. QLock;
  177. Rendez; /* wait for space/completion/errors */
  178. int usbid; /* address used for device/endpoint */
  179. int tok; /* Tdtokin or Tdtokout */
  180. int state; /* Qrun -> Qdone -> Qrun... -> Qclose */
  181. int nframes; /* number of frames ([S]Itds) used */
  182. uchar* data; /* iso data buffers if not embedded */
  183. char* err; /* error string */
  184. int nerrs; /* nb of consecutive I/O errors */
  185. ulong maxsize; /* ntds * ep->maxpkt */
  186. long nleft; /* number of bytes left from last write */
  187. int debug; /* debug flag from the endpoint */
  188. int hs; /* is high speed? */
  189. Isoio* next; /* in list of active Isoios */
  190. ulong td0frno; /* first frame used in ctlr */
  191. union{
  192. Itd* tdi; /* next td processed by interrupt */
  193. Sitd* stdi;
  194. };
  195. union{
  196. Itd* tdu; /* next td for user I/O in tdps */
  197. Sitd* stdu;
  198. };
  199. union{
  200. Itd** itdps; /* itdps[i]: ptr to Itd for i-th frame or nil */
  201. Sitd** sitdps; /* sitdps[i]: ptr to Sitd for i-th frame or nil */
  202. ulong** tdps; /* same thing, as seen by hw */
  203. };
  204. };
  205. struct Poll {
  206. Lock;
  207. Rendez;
  208. int must;
  209. int does;
  210. };
  211. struct Ctlr {
  212. Rendez; /* for waiting to async advance doorbell */
  213. Lock; /* for ilock. qh lists and basic ctlr I/O */
  214. QLock portlck; /* for port resets/enable... (and doorbell) */
  215. int active; /* in use or not */
  216. Pcidev* pcidev;
  217. Ecapio* capio; /* Capability i/o regs */
  218. Eopio* opio; /* Operational i/o regs */
  219. int nframes; /* 1024, 512, or 256 frames in the list */
  220. ulong* frames; /* periodic frame list (hw) */
  221. Qh* qhs; /* async Qh circular list for bulk/ctl */
  222. Qtree* tree; /* tree of Qhs for the periodic list */
  223. int ntree; /* number of dummy qhs in tree */
  224. Qh* intrqhs; /* list of (not dummy) qhs in tree */
  225. Isoio* iso; /* list of active Iso I/O */
  226. ulong load;
  227. ulong isoload;
  228. int nintr; /* number of interrupts attended */
  229. int ntdintr; /* number of intrs. with something to do */
  230. int nqhintr; /* number of async td intrs. */
  231. int nisointr; /* number of periodic td intrs. */
  232. int nreqs;
  233. Poll poll;
  234. };
  235. struct Edpool {
  236. Lock;
  237. Ed* free;
  238. int nalloc;
  239. int ninuse;
  240. int nfree;
  241. };
  242. /*
  243. * We use the 64-bit version for Itd, Sitd, Td, and Qh.
  244. * If the ehci is 64-bit capable it assumes we are using those
  245. * structures even when the system is 32 bits.
  246. */
  247. /*
  248. * Iso transfer descriptor. hw. 92 bytes, 104 bytes total
  249. * aligned to 32.
  250. */
  251. struct Itd {
  252. ulong link; /* to next hw struct */
  253. ulong csw[8]; /* sts/length/pg/off. updated by hw */
  254. ulong buffer[7]; /* buffer pointers, addrs, maxsz */
  255. ulong xbuffer[7]; /* high 32 bits of buffer for 64-bits */
  256. /* software */
  257. Itd* next;
  258. ulong ndata; /* number of bytes in data */
  259. ulong mdata; /* max number of bytes in data */
  260. uchar* data;
  261. };
  262. /*
  263. * Split transaction iso transfer descriptor.
  264. * hw: 36 bytes, 52 bytes total. aligned to 32.
  265. */
  266. struct Sitd {
  267. ulong link; /* to next hw struct */
  268. ulong epc; /* static endpoint state. addrs */
  269. ulong mfs; /* static endpoint state. µ-frame sched. */
  270. ulong csw; /* transfer state. updated by hw */
  271. ulong buffer[2]; /* buf. ptr/offset. offset updated by hw */
  272. /* buf ptr/TP/Tcnt. TP/Tcnt updated by hw */
  273. ulong blink; /* back pointer */
  274. ulong xbuffer[2]; /* high 32 bits of buffer for 64-bits */
  275. /* software */
  276. Sitd* next;
  277. ulong ndata; /* number of bytes in data */
  278. ulong mdata; /* max number of bytes in data */
  279. uchar* data;
  280. };
  281. /*
  282. * Queue element transfer descriptor.
  283. * hw: first 52 bytes; total 68+sbuff bytes aligned to 32 bytes.
  284. */
  285. struct Td {
  286. ulong nlink; /* to next Td */
  287. ulong alink; /* alternate link to next Td */
  288. ulong csw; /* cmd/sts. updated by hw */
  289. ulong buffer[5]; /* buf ptrs. offset updated by hw */
  290. ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
  291. Td* next; /* in qh or Isoio or free list */
  292. ulong ndata; /* bytes available/used at data */
  293. uchar* data; /* pointer to actual data */
  294. uchar* buff; /* allocated data buffer or nil */
  295. uchar sbuff[1]; /* first byte of embedded buffer */
  296. };
  297. /*
  298. * Queue head. Aligned to 32 bytes.
  299. * hw uses the first 68 bytes, 92 total.
  300. */
  301. struct Qh {
  302. ulong link; /* to next Qh in round robin */
  303. ulong eps0; /* static endpoint state. addrs */
  304. ulong eps1; /* static endpoint state. µ-frame sched. */
  305. /* updated by hw */
  306. ulong tclink; /* current Td (No Term bit here!) */
  307. ulong nlink; /* to next Td */
  308. ulong alink; /* alternate link to next Td */
  309. ulong csw; /* cmd/sts. updated by hw */
  310. ulong buffer[5]; /* buf ptrs. offset updated by hw */
  311. ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
  312. /* software */
  313. Qh* next; /* in controller list/tree of Qhs */
  314. int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
  315. Qio* io; /* for this queue */
  316. Td* tds; /* for this queue */
  317. int sched; /* slot for for intr. Qhs */
  318. Qh* inext; /* next in list of intr. qhs */
  319. };
  320. /*
  321. * We can avoid frame span traversal nodes if we don't span frames.
  322. * Just schedule transfer that can fit on the current frame and
  323. * wait a little bit otherwise.
  324. */
  325. /*
  326. * Software. Ehci descriptors provided by pool.
  327. * There are soo few because we avoid using Fstn.
  328. */
  329. union Ed {
  330. Ed* next; /* in free list */
  331. Qh qh;
  332. Td td;
  333. Itd itd;
  334. Sitd sitd;
  335. uchar align[Align];
  336. };
  337. #define diprint if(debug || iso->debug)print
  338. #define ddiprint if(debug>1 || iso->debug>1)print
  339. #define dqprint if(debug || (qh->io && qh->io->debug))print
  340. #define ddqprint if(debug>1 || (qh->io && qh->io->debug>1))print
  341. #define TRUNC(x, sz) ((x) & ((sz)-1))
  342. #define LPTR(q) ((ulong*)KADDR((q) & ~0x1F))
  343. static int debug;
  344. static Edpool edpool;
  345. static Ctlr* ctlrs[Nhcis];
  346. static char Ebug[] = "not yet implemented";
  347. static char* qhsname[] = { "idle", "install", "run", "done", "close", "FREE" };
  348. Ecapio* ehcidebugcapio;
  349. int ehcidebugport;
  350. static void
  351. ehcirun(Ctlr *ctlr, int on)
  352. {
  353. int i;
  354. Eopio *opio;
  355. ddprint("ehci %#p %s\n", ctlr->capio, on ? "starting" : "halting");
  356. opio = ctlr->opio;
  357. if(on)
  358. opio->cmd |= Crun;
  359. else
  360. opio->cmd = Cstop;
  361. for(i = 0; i < 100; i++)
  362. if(on == 0 && (opio->sts & Shalted) != 0)
  363. break;
  364. else if(on != 0 && (opio->sts & Shalted) == 0)
  365. break;
  366. else
  367. delay(1);
  368. if(i == 100)
  369. print("ehci %#p %s cmd timed out\n",
  370. ctlr->capio, on ? "run" : "halt");
  371. ddprint("ehci %#p cmd %#lux sts %#lux\n", ctlr->capio, opio->cmd, opio->sts);
  372. }
  373. static void*
  374. edalloc(void)
  375. {
  376. Ed *ed, *pool;
  377. int i;
  378. lock(&edpool);
  379. if(edpool.free == nil){
  380. pool = xspanalloc(Incr*sizeof(Ed), Align, 0);
  381. if(pool == nil)
  382. panic("edalloc");
  383. for(i=Incr; --i>=0;){
  384. pool[i].next = edpool.free;
  385. edpool.free = &pool[i];
  386. }
  387. edpool.nalloc += Incr;
  388. edpool.nfree += Incr;
  389. dprint("ehci: edalloc: %d eds\n", edpool.nalloc);
  390. }
  391. ed = edpool.free;
  392. edpool.free = ed->next;
  393. edpool.ninuse++;
  394. edpool.nfree--;
  395. unlock(&edpool);
  396. memset(ed, 0, sizeof(Ed)); /* safety */
  397. assert(((ulong)ed & 0xF) == 0);
  398. return ed;
  399. }
  400. static void
  401. edfree(void *a)
  402. {
  403. Ed *ed;
  404. ed = a;
  405. lock(&edpool);
  406. ed->next = edpool.free;
  407. edpool.free = ed;
  408. edpool.ninuse--;
  409. edpool.nfree++;
  410. unlock(&edpool);
  411. }
  412. /*
  413. * Allocate and so same initialization.
  414. * Free after releasing buffers used.
  415. */
  416. static Itd*
  417. itdalloc(void)
  418. {
  419. Itd *td;
  420. td = edalloc();
  421. td->link = Lterm;
  422. return td;
  423. }
  424. static void
  425. itdfree(Itd *td)
  426. {
  427. edfree(td);
  428. }
  429. static Sitd*
  430. sitdalloc(void)
  431. {
  432. Sitd *td;
  433. td = edalloc();
  434. td->link = td->blink = Lterm;
  435. return td;
  436. }
  437. static void
  438. sitdfree(Sitd *td)
  439. {
  440. edfree(td);
  441. }
  442. static Td*
  443. tdalloc(void)
  444. {
  445. Td *td;
  446. td = edalloc();
  447. td->nlink = td->alink = Lterm;
  448. return td;
  449. }
  450. static void
  451. tdfree(Td *td)
  452. {
  453. if(td == nil)
  454. return;
  455. free(td->buff);
  456. edfree(td);
  457. }
  458. static void
  459. tdlinktd(Td *td, Td *next)
  460. {
  461. td->next = next;
  462. td->alink = Lterm;
  463. if(next == nil)
  464. td->nlink = Lterm;
  465. else
  466. td->nlink = PADDR(next);
  467. }
  468. static Qh*
  469. qhlinkqh(Qh *qh, Qh *next)
  470. {
  471. qh->next = next;
  472. qh->link = PADDR(next)|Lqh;
  473. return qh;
  474. }
  475. static void
  476. qhsetaddr(Qh *qh, ulong addr)
  477. {
  478. ulong eps0;
  479. ulong ep;
  480. ulong dev;
  481. eps0 = qh->eps0 & ~((Epmax<<8)|Devmax);
  482. ep = (addr >> 7) & Epmax;
  483. dev = addr & Devmax;
  484. eps0 |= ep << 8;
  485. eps0 |= dev;
  486. qh->eps0 = eps0;
  487. }
  488. /*
  489. * return smallest power of 2 <= n
  490. */
  491. static int
  492. flog2lower(int n)
  493. {
  494. int i;
  495. for(i = 0; (1 << (i + 1)) <= n; i++)
  496. ;
  497. return i;
  498. }
  499. static int
  500. pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
  501. {
  502. int i, j, d, upperb, q;
  503. ulong best, worst, total;
  504. d = flog2lower(pollival);
  505. if(d > qt->depth)
  506. d = qt->depth;
  507. q = -1;
  508. worst = 0;
  509. best = ~0;
  510. upperb = (1 << (d+1)) - 1;
  511. for(i = (1 << d) - 1; i < upperb; i++){
  512. total = qt->bw[0];
  513. for(j = i; j > 0; j = (j - 1) / 2)
  514. total += qt->bw[j];
  515. if(total < best){
  516. best = total;
  517. q = i;
  518. }
  519. if(total > worst)
  520. worst = total;
  521. }
  522. if(worst + bw >= limit)
  523. return -1;
  524. return q;
  525. }
  526. static int
  527. schedq(Ctlr *ctlr, Qh *qh, int pollival)
  528. {
  529. int q;
  530. Qh *tqh;
  531. ulong bw;
  532. bw = qh->io->bw;
  533. q = pickschedq(ctlr->tree, pollival, 0, ~0);
  534. ddqprint("ehci: sched %#p q %d, ival %d, bw %uld\n",
  535. qh->io, q, pollival, bw);
  536. if(q < 0){
  537. print("ehci: no room for ed\n");
  538. return -1;
  539. }
  540. ctlr->tree->bw[q] += bw;
  541. tqh = ctlr->tree->root[q];
  542. qh->sched = q;
  543. qhlinkqh(qh, tqh->next);
  544. qhlinkqh(tqh, qh);
  545. qh->inext = ctlr->intrqhs;
  546. ctlr->intrqhs = qh;
  547. return 0;
  548. }
  549. static void
  550. unschedq(Ctlr *ctlr, Qh *qh)
  551. {
  552. int q;
  553. Qh *prev, *this, *next;
  554. Qh **l;
  555. ulong bw;
  556. bw = qh->io->bw;
  557. q = qh->sched;
  558. if(q < 0)
  559. return;
  560. ctlr->tree->bw[q] -= bw;
  561. prev = ctlr->tree->root[q];
  562. this = prev->next;
  563. while(this != nil && this != qh){
  564. prev = this;
  565. this = this->next;
  566. }
  567. if(this == nil)
  568. print("ehci: unschedq %d: not found\n", q);
  569. else{
  570. next = this->next;
  571. qhlinkqh(prev, next);
  572. }
  573. for(l = &ctlr->intrqhs; *l != nil; l = &(*l)->inext)
  574. if(*l == qh){
  575. *l = (*l)->inext;
  576. return;
  577. }
  578. print("ehci: unschedq: qh %#p not found\n", qh);
  579. }
  580. static ulong
  581. qhmaxpkt(Qh *qh)
  582. {
  583. return (qh->eps0 >> Qhmplshift) & Qhmplmask;
  584. }
  585. static void
  586. qhsetmaxpkt(Qh *qh, int maxpkt)
  587. {
  588. ulong eps0;
  589. eps0 = qh->eps0 & ~(Qhmplmask << Qhmplshift);
  590. eps0 |= (maxpkt & Qhmplmask) << Qhmplshift;
  591. qh->eps0 = eps0;
  592. }
  593. /*
  594. * Initialize the round-robin circular list of ctl/bulk Qhs
  595. * if ep is nil. Otherwise, allocate and link a new Qh in the ctlr.
  596. */
  597. static Qh*
  598. qhalloc(Ctlr *ctlr, Ep *ep, Qio *io, char* tag)
  599. {
  600. Qh *qh;
  601. int ttype;
  602. qh = edalloc();
  603. qh->nlink = Lterm;
  604. qh->alink = Lterm;
  605. qh->csw = Tdhalt;
  606. qh->state = Qidle;
  607. qh->sched = -1;
  608. qh->io = io;
  609. if(ep != nil){
  610. qh->eps0 = 0;
  611. qhsetmaxpkt(qh, ep->maxpkt);
  612. if(ep->dev->speed == Lowspeed)
  613. qh->eps0 |= Qhlow;
  614. if(ep->dev->speed == Highspeed)
  615. qh->eps0 |= Qhhigh;
  616. else if(ep->ttype == Tctl)
  617. qh->eps0 |= Qhnhctl;
  618. qh->eps0 |= Qhdtc;
  619. qh->eps0 |= (8 << Qhrlcshift); /* 8 naks max */
  620. qhsetaddr(qh, io->usbid);
  621. qh->eps1 = (ep->ntds & Qhmultmask) << Qhmultshift;
  622. qh->eps1 |= ep->dev->port << Qhportshift;
  623. qh->eps1 |= ep->dev->hub << Qhhubshift;
  624. qh->eps1 |= 034 << Qhscmshift;
  625. if(ep->ttype == Tintr)
  626. qh->eps1 |= (1 << Qhismshift); /* intr. start µf. */
  627. if(io != nil)
  628. io->tag = tag;
  629. }
  630. ilock(ctlr);
  631. ttype = Tctl;
  632. if(ep != nil)
  633. ttype = ep->ttype;
  634. switch(ttype){
  635. case Tctl:
  636. case Tbulk:
  637. if(ctlr->qhs == nil){
  638. ctlr->qhs = qhlinkqh(qh, qh);
  639. ctlr->opio->link = PADDR(qh)|Lqh;
  640. qh->eps0 |= Qhhigh | Qhhrl;
  641. }else{
  642. qhlinkqh(qh, ctlr->qhs->next);
  643. qhlinkqh(ctlr->qhs, qh);
  644. }
  645. break;
  646. case Tintr:
  647. schedq(ctlr, qh, ep->pollival);
  648. break;
  649. default:
  650. print("ehci: qhalloc called for ttype != ctl/bulk\n");
  651. }
  652. iunlock(ctlr);
  653. return qh;
  654. }
  655. static int
  656. qhadvanced(void *a)
  657. {
  658. Ctlr *ctlr;
  659. ctlr = a;
  660. return (ctlr->opio->cmd & Ciasync) == 0;
  661. }
  662. /*
  663. * called when a qh is removed, to be sure the hw is not
  664. * keeping pointers into it.
  665. */
  666. static void
  667. qhcoherency(Ctlr *ctlr)
  668. {
  669. int i;
  670. qlock(&ctlr->portlck);
  671. ctlr->opio->cmd |= Ciasync; /* ask for intr. on async advance */
  672. for(i = 0; i < 3 && qhadvanced(ctlr) == 0; i++)
  673. if(!waserror()){
  674. tsleep(ctlr, qhadvanced, ctlr, Abortdelay);
  675. poperror();
  676. }
  677. dprint("ehci: qhcoherency: doorbell %d\n", qhadvanced(ctlr));
  678. if(i == 3)
  679. print("ehci: async advance doorbell did not ring\n");
  680. ctlr->opio->cmd &= ~Ciasync; /* try to clean */
  681. qunlock(&ctlr->portlck);
  682. }
  683. static void
  684. qhfree(Ctlr *ctlr, Qh *qh)
  685. {
  686. Td *td;
  687. Td *ltd;
  688. Qh *q;
  689. if(qh == nil)
  690. return;
  691. ilock(ctlr);
  692. if(qh->sched < 0){
  693. for(q = ctlr->qhs; q != nil; q = q->next)
  694. if(q->next == qh)
  695. break;
  696. if(q == nil)
  697. panic("qhfree: nil q");
  698. q->next = qh->next;
  699. q->link = qh->link;
  700. }else
  701. unschedq(ctlr, qh);
  702. iunlock(ctlr);
  703. qhcoherency(ctlr);
  704. for(td = qh->tds; td != nil; td = ltd){
  705. ltd = td->next;
  706. tdfree(td);
  707. }
  708. edfree(qh);
  709. }
  710. static void
  711. qhlinktd(Qh *qh, Td *td)
  712. {
  713. ulong csw;
  714. int i;
  715. if(td == nil){
  716. qh->tds = nil;
  717. qh->csw |= Tdhalt;
  718. qh->csw &= ~Tdactive;
  719. }else{
  720. qh->tds = td;
  721. csw = qh->csw & (Tddata1|Tdping); /* save */
  722. qh->csw = Tdhalt;
  723. qh->tclink = 0;
  724. qh->alink = Lterm;
  725. qh->nlink = PADDR(td);
  726. for(i = 0; i < nelem(qh->buffer); i++)
  727. qh->buffer[i] = 0;
  728. qh->csw = csw & ~(Tdhalt|Tdactive); /* activate next */
  729. }
  730. }
  731. static char*
  732. seprintlink(char *s, char *se, char *name, ulong l, int typed)
  733. {
  734. s = seprint(s, se, "%s %ulx", name, l);
  735. if((l & Lterm) != 0)
  736. return seprint(s, se, "T");
  737. if(typed == 0)
  738. return s;
  739. switch(l & (3<<1)){
  740. case Litd:
  741. return seprint(s, se, "I");
  742. case Lqh:
  743. return seprint(s, se, "Q");
  744. case Lsitd:
  745. return seprint(s, se, "S");
  746. default:
  747. return seprint(s, se, "F");
  748. }
  749. }
  750. static char*
  751. seprintitd(char *s, char *se, Itd *td)
  752. {
  753. int i;
  754. char flags[6];
  755. ulong b0;
  756. ulong b1;
  757. char *rw;
  758. if(td == nil)
  759. return seprint(s, se, "<nil itd>\n");
  760. b0 = td->buffer[0];
  761. b1 = td->buffer[1];
  762. s = seprint(s, se, "itd %#p", td);
  763. rw = (b1 & Itdin) ? "in" : "out";
  764. s = seprint(s, se, " %s ep %uld dev %uld max %uld mult %uld",
  765. rw, (b0>>8)&Epmax, (b0&Devmax),
  766. td->buffer[1] & 0x7ff, b1 & 3);
  767. s = seprintlink(s, se, " link", td->link, 1);
  768. s = seprint(s, se, "\n");
  769. for(i = 0; i < nelem(td->csw); i++){
  770. memset(flags, '-', 5);
  771. if((td->csw[i] & Itdactive) != 0)
  772. flags[0] = 'a';
  773. if((td->csw[i] & Itdioc) != 0)
  774. flags[1] = 'i';
  775. if((td->csw[i] & Itddberr) != 0)
  776. flags[2] = 'd';
  777. if((td->csw[i] & Itdbabble) != 0)
  778. flags[3] = 'b';
  779. if((td->csw[i] & Itdtrerr) != 0)
  780. flags[4] = 't';
  781. flags[5] = 0;
  782. s = seprint(s, se, "\ttd%d %s", i, flags);
  783. s = seprint(s, se, " len %uld", (td->csw[i] >> 16) & 0x7ff);
  784. s = seprint(s, se, " pg %uld", (td->csw[i] >> 12) & 0x7);
  785. s = seprint(s, se, " off %uld\n", td->csw[i] & 0xfff);
  786. }
  787. s = seprint(s, se, "\tbuffs:");
  788. for(i = 0; i < nelem(td->buffer); i++)
  789. s = seprint(s, se, " %#lux", td->buffer[i] >> 12);
  790. return seprint(s, se, "\n");
  791. }
  792. static char*
  793. seprintsitd(char *s, char *se, Sitd *td)
  794. {
  795. static char pc[4] = { 'a', 'b', 'm', 'e' };
  796. char rw;
  797. char pg;
  798. char ss;
  799. char flags[8];
  800. if(td == nil)
  801. return seprint(s, se, "<nil sitd>\n");
  802. s = seprint(s, se, "sitd %#p", td);
  803. rw = (td->epc & Stdin) ? 'r' : 'w';
  804. s = seprint(s, se, " %c ep %uld dev %uld",
  805. rw, (td->epc>>8)&0xf, td->epc&0x7f);
  806. s = seprint(s, se, " max %uld", (td->csw >> 16) & 0x3ff);
  807. s = seprint(s, se, " hub %uld", (td->epc >> 16) & 0x7f);
  808. s = seprint(s, se, " port %uld\n", (td->epc >> 24) & 0x7f);
  809. memset(flags, '-', 7);
  810. if((td->csw & Stdactive) != 0)
  811. flags[0] = 'a';
  812. if((td->csw & Stdioc) != 0)
  813. flags[1] = 'i';
  814. if((td->csw & Stderr) != 0)
  815. flags[2] = 'e';
  816. if((td->csw & Stddberr) != 0)
  817. flags[3] = 'd';
  818. if((td->csw & Stdbabble) != 0)
  819. flags[4] = 'b';
  820. if((td->csw & Stdtrerr) != 0)
  821. flags[5] = 't';
  822. if((td->csw & Stdmmf) != 0)
  823. flags[6] = 'n';
  824. flags[7] = 0;
  825. ss = (td->csw & Stddcs) ? 'c' : 's';
  826. pg = (td->csw & Stdpg) ? '1' : '0';
  827. s = seprint(s, se, "\t%s %cs pg%c", flags, ss, pg);
  828. s = seprint(s, se, " b0 %#lux b1 %#lux off %uld\n",
  829. td->buffer[0] >> 12, td->buffer[1] >> 12, td->buffer[0] & 0xfff);
  830. s = seprint(s, se, "\ttpos %c tcnt %uld",
  831. pc[(td->buffer[0]>>3)&3], td->buffer[1] & 7);
  832. s = seprint(s, se, " ssm %#lux csm %#lux cspm %#lux",
  833. td->mfs & 0xff, (td->mfs>>8) & 0xff, (td->csw>>8) & 0xff);
  834. s = seprintlink(s, se, " link", td->link, 1);
  835. s = seprintlink(s, se, " blink", td->blink, 0);
  836. return seprint(s, se, "\n");
  837. }
  838. static long
  839. maxtdlen(Td *td)
  840. {
  841. return (td->csw >> Tdlenshift) & Tdlenmask;
  842. }
  843. static long
  844. tdlen(Td *td)
  845. {
  846. if(td->data == nil)
  847. return 0;
  848. return td->ndata - maxtdlen(td);
  849. }
  850. static char*
  851. seprinttd(char *s, char *se, Td *td, char *tag)
  852. {
  853. static char *tok[4] = { "out", "in", "setup", "BUG" };
  854. char flags[9];
  855. char t;
  856. char ss;
  857. int i;
  858. s = seprint(s, se, "%s %#p", tag, td);
  859. s = seprintlink(s, se, " nlink", td->nlink, 0);
  860. s = seprintlink(s, se, " alink", td->alink, 0);
  861. s = seprint(s, se, " %s", tok[(td->csw & Tdtok) >> 8]);
  862. if((td->csw & Tdping) != 0)
  863. s = seprint(s, se, " png");
  864. memset(flags, '-', 8);
  865. if((td->csw & Tdactive) != 0)
  866. flags[0] = 'a';
  867. if((td->csw & Tdioc) != 0)
  868. flags[1] = 'i';
  869. if((td->csw & Tdhalt) != 0)
  870. flags[2] = 'h';
  871. if((td->csw & Tddberr) != 0)
  872. flags[3] = 'd';
  873. if((td->csw & Tdbabble) != 0)
  874. flags[4] = 'b';
  875. if((td->csw & Tdtrerr) != 0)
  876. flags[5] = 't';
  877. if((td->csw & Tdmmf) != 0)
  878. flags[6] = 'n';
  879. if((td->csw & (Tderr2|Tderr1)) == 0)
  880. flags[7] = 'z';
  881. flags[8] = 0;
  882. t = (td->csw & Tddata1) ? '1' : '0';
  883. ss = (td->csw & Tddcs) ? 'c' : 's';
  884. s = seprint(s, se, "\n\td%c %s %cs", t, flags, ss);
  885. s = seprint(s, se, " max %uld", maxtdlen(td));
  886. s = seprint(s, se, " pg %uld off %#lux\n",
  887. (td->csw >> Tdpgshift) & Tdpgmask, td->buffer[0] & 0xFFF);
  888. s = seprint(s, se, "\tbuffs:");
  889. for(i = 0; i < nelem(td->buffer); i++)
  890. s = seprint(s, se, " %#lux", td->buffer[i]>>12);
  891. if(td->data != nil)
  892. s = seprintdata(s, se, td->data, td->ndata);
  893. return seprint(s, se, "\n");
  894. }
  895. static void
  896. dumptd(Td *td, char *pref)
  897. {
  898. char buf[256];
  899. char *se;
  900. int i;
  901. i = 0;
  902. se = buf+sizeof(buf);
  903. for(; td != nil; td = td->next){
  904. seprinttd(buf, se, td, pref);
  905. print("%s", buf);
  906. if(i++ > 20){
  907. print("...more tds...\n");
  908. break;
  909. }
  910. }
  911. }
  912. static void
  913. qhdump(Qh *qh)
  914. {
  915. static char *speed[] = {"full", "low", "high", "BUG"};
  916. char buf[256];
  917. char *s;
  918. char *se;
  919. char *tag;
  920. Td td;
  921. if(qh == nil){
  922. print("<nil qh>\n");
  923. return;
  924. }
  925. if(qh->io == nil)
  926. tag = "qh";
  927. else
  928. tag = qh->io->tag;
  929. se = buf+sizeof(buf);
  930. s = seprint(buf, se, "%s %#p", tag, qh);
  931. s = seprint(s, se, " ep %uld dev %uld",
  932. (qh->eps0>>8)&0xf, qh->eps0&0x7f);
  933. s = seprint(s, se, " hub %uld", (qh->eps1 >> 16) & 0x7f);
  934. s = seprint(s, se, " port %uld", (qh->eps1 >> 23) & 0x7f);
  935. s = seprintlink(s, se, " link", qh->link, 1);
  936. seprint(s, se, " clink %#lux", qh->tclink);
  937. print("%s\n", buf);
  938. s = seprint(buf, se, "\tnrld %uld", (qh->eps0 >> Qhrlcshift) & Qhrlcmask);
  939. s = seprint(s, se, " nak %uld", (qh->alink >> 1) & 0xf);
  940. s = seprint(s, se, " max %uld ", qhmaxpkt(qh));
  941. if((qh->eps0 & Qhnhctl) != 0)
  942. s = seprint(s, se, "c");
  943. if((qh->eps0 & Qhhrl) != 0)
  944. s = seprint(s, se, "h");
  945. if((qh->eps0 & Qhdtc) != 0)
  946. s = seprint(s, se, "d");
  947. if((qh->eps0 & Qhint) != 0)
  948. s = seprint(s, se, "i");
  949. s = seprint(s, se, " %s", speed[(qh->eps0 >> 12) & 3]);
  950. s = seprint(s, se, " mult %uld", (qh->eps1 >> Qhmultshift) & Qhmultmask);
  951. seprint(s, se, " scm %#lux ism %#lux\n",
  952. (qh->eps1 >> 8 & 0xff), qh->eps1 & 0xff);
  953. print("%s\n", buf);
  954. memset(&td, 0, sizeof(td));
  955. memmove(&td, &qh->nlink, 32); /* overlay area */
  956. seprinttd(buf, se, &td, "\tovl");
  957. print("%s", buf);
  958. }
  959. static void
  960. isodump(Isoio* iso, int all)
  961. {
  962. Itd *td, *tdi, *tdu;
  963. Sitd *std, *stdi, *stdu;
  964. char buf[256];
  965. int i;
  966. if(iso == nil){
  967. print("<nil iso>\n");
  968. return;
  969. }
  970. print("iso %#p %s %s speed state %d nframes %d maxsz %uld",
  971. iso, iso->tok == Tdtokin ? "in" : "out",
  972. iso->hs ? "high" : "full",
  973. iso->state, iso->nframes, iso->maxsize);
  974. print(" td0 %uld tdi %#p tdu %#p data %#p\n",
  975. iso->td0frno, iso->tdi, iso->tdu, iso->data);
  976. if(iso->err != nil)
  977. print("\terr %s\n", iso->err);
  978. if(iso->err != nil)
  979. print("\terr='%s'\n", iso->err);
  980. if(all == 0)
  981. if(iso->hs != 0){
  982. tdi = iso->tdi;
  983. seprintitd(buf, buf+sizeof(buf), tdi);
  984. print("\ttdi %s\n", buf);
  985. tdu = iso->tdu;
  986. seprintitd(buf, buf+sizeof(buf), tdu);
  987. print("\ttdu %s\n", buf);
  988. }else{
  989. stdi = iso->stdi;
  990. seprintsitd(buf, buf+sizeof(buf), stdi);
  991. print("\tstdi %s\n", buf);
  992. stdu = iso->stdu;
  993. seprintsitd(buf, buf+sizeof(buf), stdu);
  994. print("\tstdu %s\n", buf);
  995. }
  996. else{
  997. for(i = 0; i < Nisoframes; i++)
  998. if(iso->tdps[i] != nil)
  999. if(iso->hs != 0){
  1000. td = iso->itdps[i];
  1001. seprintitd(buf, buf+sizeof(buf), td);
  1002. if(td == iso->tdi)
  1003. print("i->");
  1004. if(td == iso->tdu)
  1005. print("i->");
  1006. print("[%d]\t%s", i, buf);
  1007. }else{
  1008. std = iso->sitdps[i];
  1009. seprintsitd(buf, buf+sizeof(buf), std);
  1010. if(std == iso->stdi)
  1011. print("i->");
  1012. if(std == iso->stdu)
  1013. print("u->");
  1014. print("[%d]\t%s", i, buf);
  1015. }
  1016. }
  1017. }
  1018. static void
  1019. dump(Hci *hp)
  1020. {
  1021. Ctlr *ctlr;
  1022. Isoio *iso;
  1023. Eopio *opio;
  1024. int i;
  1025. char buf[128];
  1026. char *s;
  1027. char *se;
  1028. Qh *qh;
  1029. ctlr = hp->aux;
  1030. opio = ctlr->opio;
  1031. ilock(ctlr);
  1032. print("ehci port %#p frames %#p (%d fr.) nintr %d ntdintr %d",
  1033. ctlr->capio, ctlr->frames, ctlr->nframes,
  1034. ctlr->nintr, ctlr->ntdintr);
  1035. print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
  1036. print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
  1037. opio->cmd, opio->sts, opio->intr, opio->frno);
  1038. print(" base %#lux link %#lux fr0 %#lux\n",
  1039. opio->frbase, opio->link, ctlr->frames[0]);
  1040. se = buf+sizeof(buf);
  1041. s = seprint(buf, se, "\t");
  1042. for(i = 0; i < hp->nports; i++){
  1043. s = seprint(s, se, "p%d %#lux ", i, opio->portsc[i]);
  1044. if(hp->nports > 4 && i == hp->nports/2 - 1)
  1045. s = seprint(s, se, "\n\t");
  1046. }
  1047. print("%s\n", buf);
  1048. qh = ctlr->qhs;
  1049. i = 0;
  1050. do{
  1051. qhdump(qh);
  1052. qh = qh->next;
  1053. }while(qh != ctlr->qhs && i++ < 100);
  1054. if(i > 100)
  1055. print("...too many Qhs...\n");
  1056. if(ctlr->intrqhs != nil)
  1057. print("intr qhs:\n");
  1058. for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
  1059. qhdump(qh);
  1060. if(ctlr->iso != nil)
  1061. print("iso:\n");
  1062. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  1063. isodump(ctlr->iso, 0);
  1064. print("%d eds in tree\n", ctlr->ntree);
  1065. iunlock(ctlr);
  1066. lock(&edpool);
  1067. print("%d eds allocated = %d in use + %d free\n",
  1068. edpool.nalloc, edpool.ninuse, edpool.nfree);
  1069. unlock(&edpool);
  1070. }
  1071. static char*
  1072. errmsg(int err)
  1073. {
  1074. if(err == 0)
  1075. return "ok";
  1076. if(err & Tddberr)
  1077. return "data buffer error";
  1078. if(err & Tdbabble)
  1079. return "babble detected";
  1080. if(err & Tdtrerr)
  1081. return "transaction error";
  1082. if(err & Tdmmf)
  1083. return "missed µframe";
  1084. if(err & Tdhalt)
  1085. return Estalled; /* [uo]hci report this error */
  1086. return Eio;
  1087. }
  1088. static char*
  1089. ierrmsg(int err)
  1090. {
  1091. if(err == 0)
  1092. return "ok";
  1093. if(err & Itddberr)
  1094. return "data buffer error";
  1095. if(err & Itdbabble)
  1096. return "babble detected";
  1097. if(err & Itdtrerr)
  1098. return "transaction error";
  1099. return Eio;
  1100. }
  1101. static char*
  1102. serrmsg(int err)
  1103. {
  1104. if(err & Stderr)
  1105. return "translation translator error";
  1106. /* other errors have same numbers than Td errors */
  1107. return errmsg(err);
  1108. }
  1109. static int
  1110. isocanread(void *a)
  1111. {
  1112. Isoio *iso;
  1113. iso = a;
  1114. if(iso->state == Qclose)
  1115. return 1;
  1116. if(iso->state == Qrun && iso->tok == Tdtokin){
  1117. if(iso->hs != 0 && iso->tdi != iso->tdu)
  1118. return 1;
  1119. if(iso->hs == 0 && iso->stdi != iso->stdu)
  1120. return 1;
  1121. }
  1122. return 0;
  1123. }
  1124. static int
  1125. isocanwrite(void *a)
  1126. {
  1127. Isoio *iso;
  1128. iso = a;
  1129. if(iso->state == Qclose)
  1130. return 1;
  1131. if(iso->state == Qrun && iso->tok == Tdtokout){
  1132. if(iso->hs != 0 && iso->tdu->next != iso->tdi)
  1133. return 1;
  1134. if(iso->hs == 0 && iso->stdu->next != iso->stdi)
  1135. return 1;
  1136. }
  1137. return 0;
  1138. }
  1139. static void
  1140. itdinit(Isoio *iso, Itd *td)
  1141. {
  1142. ulong pa;
  1143. int p;
  1144. int t;
  1145. ulong tsize;
  1146. ulong size;
  1147. /*
  1148. * BUG: This does not put an integral number of samples
  1149. * on each µframe unless samples per packet % 8 == 0
  1150. * Also, all samples are packed early on each frame.
  1151. */
  1152. p = 0;
  1153. size = td->ndata = td->mdata;
  1154. pa = PADDR(td->data);
  1155. for(t = 0; size > 0 && t < 8; t++){
  1156. tsize = size;
  1157. if(tsize > iso->maxsize)
  1158. tsize = iso->maxsize;
  1159. size -= tsize;
  1160. td->csw[t] = tsize << Itdlenshift;
  1161. assert(p < nelem(td->buffer));
  1162. td->csw[t] |= p << Itdpgshift;
  1163. td->csw[t] |= (pa & 0xFFF) << Itdoffshift;
  1164. td->csw[t] |= Itdactive|Itdioc;
  1165. if(((pa+tsize) & ~0xFFF) != (pa & ~0xFFF))
  1166. p++;
  1167. pa += tsize;
  1168. }
  1169. }
  1170. static void
  1171. sitdinit(Isoio *iso, Sitd *td)
  1172. {
  1173. td->ndata = td->mdata & Stdlenmask;
  1174. td->csw = (td->ndata << Stdlenshift) | Stdactive | Stdioc;
  1175. td->buffer[0] = PADDR(td->data);
  1176. td->buffer[1] = (td->buffer[0] & ~0xFFF) + 0x1000;
  1177. if(iso->tok == Tdtokin || td->ndata <= 188)
  1178. td->buffer[1] |= Stdtpall;
  1179. else
  1180. td->buffer[1] |= Stdtpbegin;
  1181. if(iso->tok == Tdtokin)
  1182. td->buffer[1] |= 1;
  1183. else
  1184. td->buffer[1] |= ((td->ndata + 187 ) / 188) & Stdtcntmask;
  1185. }
  1186. static int
  1187. itdactive(Itd *td)
  1188. {
  1189. int i;
  1190. for(i = 0; i < nelem(td->csw); i++)
  1191. if((td->csw[i] & Itdactive) != 0)
  1192. return 1;
  1193. return 0;
  1194. }
  1195. static int
  1196. isohsinterrupt(Ctlr *ctlr, Isoio *iso)
  1197. {
  1198. Itd *tdi;
  1199. int err;
  1200. int i;
  1201. int t;
  1202. int nframes;
  1203. tdi = iso->tdi;
  1204. assert(tdi != nil);
  1205. if(itdactive(tdi)) /* not all tds are done */
  1206. return 0;
  1207. ctlr->nisointr++;
  1208. ddiprint("isohsintr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
  1209. if(iso->state != Qrun && iso->state != Qdone)
  1210. panic("isofsintr: iso state");
  1211. if(debug > 1 || iso->debug > 1)
  1212. isodump(iso, 0);
  1213. nframes = iso->nframes / 2; /* limit how many we look */
  1214. if(nframes > Nisoframes)
  1215. nframes = Nisoframes;
  1216. if(iso->tok == Tdtokin)
  1217. tdi->ndata = 0;
  1218. /* else, it has the number of bytes transferred */
  1219. for(i = 0; i < nframes && itdactive(tdi) == 0; i++){
  1220. err = 0;
  1221. if(iso->tok == Tdtokin)
  1222. tdi->ndata += (tdi->csw[i] >> Itdlenshift)&Itdlenmask;
  1223. for(t = 0; t < nelem(tdi->csw); t++){
  1224. tdi->csw[i] &= ~Itdioc;
  1225. err |= tdi->csw[i] & Itderrors;
  1226. }
  1227. if(err == 0)
  1228. iso->nerrs = 0;
  1229. else if(iso->nerrs++ > iso->nframes/2){
  1230. if(iso->err == nil){
  1231. iso->err = ierrmsg(err);
  1232. diprint("isohsintr: tdi %#p error %#ux %s\n",
  1233. tdi, err, iso->err);
  1234. diprint("ctlr load %uld\n", ctlr->load);
  1235. }
  1236. tdi->ndata = 0;
  1237. }else
  1238. tdi->ndata = 0;
  1239. if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
  1240. memset(iso->tdu->data, 0, iso->tdu->mdata);
  1241. itdinit(iso, iso->tdu);
  1242. iso->tdu = iso->tdu->next;
  1243. iso->nleft = 0;
  1244. }
  1245. tdi = tdi->next;
  1246. }
  1247. ddiprint("isohsintr: %d frames processed\n", nframes);
  1248. if(i == nframes)
  1249. tdi->csw[0] |= Itdioc;
  1250. iso->tdi = tdi;
  1251. if(isocanwrite(iso) || isocanread(iso)){
  1252. diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
  1253. iso->tdi, iso->tdu);
  1254. wakeup(iso);
  1255. }
  1256. return 1;
  1257. }
  1258. static int
  1259. isofsinterrupt(Ctlr *ctlr, Isoio *iso)
  1260. {
  1261. Sitd *stdi;
  1262. int err;
  1263. int i;
  1264. int nframes;
  1265. stdi = iso->stdi;
  1266. assert(stdi != nil);
  1267. if((stdi->csw & Stdactive) != 0) /* nothing new done */
  1268. return 0;
  1269. ctlr->nisointr++;
  1270. ddiprint("isofsintr: iso %#p: tdi %#p tdu %#p\n", iso, stdi, iso->stdu);
  1271. if(iso->state != Qrun && iso->state != Qdone)
  1272. panic("isofsintr: iso state");
  1273. if(debug > 1 || iso->debug > 1)
  1274. isodump(iso, 0);
  1275. nframes = iso->nframes / 2; /* limit how many we look */
  1276. if(nframes > Nisoframes)
  1277. nframes = Nisoframes;
  1278. for(i = 0; i < nframes && (stdi->csw & Stdactive) == 0; i++){
  1279. stdi->csw &= ~Stdioc;
  1280. err = stdi->csw & Stderrors;
  1281. if(err == 0){
  1282. iso->nerrs = 0;
  1283. if(iso->tok == Tdtokin)
  1284. stdi->ndata = (stdi->csw>>Stdlenshift)&Stdlenmask;
  1285. /* else len is assumed correct */
  1286. }else if(iso->nerrs++ > iso->nframes/2){
  1287. if(iso->err == nil){
  1288. iso->err = serrmsg(err);
  1289. diprint("isofsintr: tdi %#p error %#ux %s\n",
  1290. stdi, err, iso->err);
  1291. diprint("ctlr load %uld\n", ctlr->load);
  1292. }
  1293. stdi->ndata = 0;
  1294. }else
  1295. stdi->ndata = 0;
  1296. if(stdi->next == iso->stdu || stdi->next->next == iso->stdu){
  1297. memset(iso->stdu->data, 0, iso->stdu->mdata);
  1298. sitdinit(iso, iso->stdu);
  1299. iso->stdu = iso->stdu->next;
  1300. iso->nleft = 0;
  1301. }
  1302. stdi = stdi->next;
  1303. }
  1304. ddiprint("isofsintr: %d frames processed\n", nframes);
  1305. if(i == nframes)
  1306. stdi->csw |= Stdioc;
  1307. iso->stdi = stdi;
  1308. if(isocanwrite(iso) || isocanread(iso)){
  1309. diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
  1310. iso->stdi, iso->stdu);
  1311. wakeup(iso);
  1312. }
  1313. return 1;
  1314. }
  1315. static int
  1316. qhinterrupt(Ctlr *ctlr, Qh *qh)
  1317. {
  1318. Td *td;
  1319. int err;
  1320. if(qh->state != Qrun)
  1321. panic("qhinterrupt: qh state");
  1322. if(qh->tds == nil)
  1323. panic("qhinterrupt: no tds");
  1324. if((qh->tds->csw & Tdactive) == 0)
  1325. ddqprint("qhinterrupt port %#p qh %#p\n",ctlr->capio, qh);
  1326. for(td = qh->tds; td != nil; td = td->next){
  1327. if(td->csw & Tdactive)
  1328. return 0;
  1329. if((td->csw & Tderrors) != 0){
  1330. err = td->csw & Tderrors;
  1331. if(qh->io->err == nil){
  1332. qh->io->err = errmsg(td->csw & Tderrors);
  1333. dqprint("qhintr: td %#p csw %#lux error %#ux %s\n",
  1334. td, td->csw, err, qh->io->err);
  1335. }
  1336. break;
  1337. }
  1338. td->ndata = tdlen(td);
  1339. if(td->ndata < maxtdlen(td)){ /* EOT */
  1340. td = td->next;
  1341. break;
  1342. }
  1343. }
  1344. /*
  1345. * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
  1346. */
  1347. for(; td != nil; td = td->next)
  1348. td->ndata = 0;
  1349. qh->state = Qdone;
  1350. wakeup(qh->io);
  1351. return 1;
  1352. }
  1353. static int
  1354. ehciintr(Hci *hp)
  1355. {
  1356. Ctlr *ctlr;
  1357. Eopio *opio;
  1358. Isoio *iso;
  1359. ulong sts;
  1360. Qh *qh;
  1361. int i;
  1362. int some;
  1363. ctlr = hp->aux;
  1364. opio = ctlr->opio;
  1365. /*
  1366. * Will we know in USB 3.0 who the interrupt was for?.
  1367. * Do they still teach indexing in CS?
  1368. * This is Intel's doing.
  1369. */
  1370. ilock(ctlr);
  1371. ctlr->nintr++;
  1372. sts = opio->sts & Sintrs;
  1373. if(sts == 0){ /* not ours; shared intr. */
  1374. iunlock(ctlr);
  1375. return 0;
  1376. }
  1377. opio->sts = sts;
  1378. if((sts & Sherr) != 0)
  1379. print("ehci: port %#p fatal host system error\n", ctlr->capio);
  1380. if((sts & Shalted) != 0)
  1381. print("ehci: port %#p: halted\n", ctlr->capio);
  1382. if((sts & Sasync) != 0){
  1383. dprint("ehci: doorbell\n");
  1384. wakeup(ctlr);
  1385. }
  1386. /*
  1387. * We enter always this if, even if it seems the
  1388. * interrupt does not report anything done/failed.
  1389. * Some controllers don't post interrupts right.
  1390. */
  1391. some = 0;
  1392. if((sts & (Serrintr|Sintr)) != 0){
  1393. ctlr->ntdintr++;
  1394. if(debug > 1){
  1395. print("ehci port %#p frames %#p nintr %d ntdintr %d",
  1396. ctlr->capio, ctlr->frames,
  1397. ctlr->nintr, ctlr->ntdintr);
  1398. print(" nqhintr %d nisointr %d\n",
  1399. ctlr->nqhintr, ctlr->nisointr);
  1400. print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
  1401. opio->cmd, opio->sts, opio->intr, opio->frno);
  1402. }
  1403. /* process the Iso transfers */
  1404. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  1405. if(iso->state == Qrun || iso->state == Qdone)
  1406. if(iso->hs != 0)
  1407. some += isohsinterrupt(ctlr, iso);
  1408. else
  1409. some += isofsinterrupt(ctlr, iso);
  1410. /* process the qhs in the periodic tree */
  1411. for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
  1412. if(qh->state == Qrun)
  1413. some += qhinterrupt(ctlr, qh);
  1414. /* process the async Qh circular list */
  1415. qh = ctlr->qhs;
  1416. i = 0;
  1417. do{
  1418. if(qh->state == Qrun)
  1419. some += qhinterrupt(ctlr, qh);
  1420. qh = qh->next;
  1421. }while(qh != ctlr->qhs && i++ < 100);
  1422. if(i > 100)
  1423. print("echi: interrupt: qh loop?\n");
  1424. }
  1425. iunlock(ctlr);
  1426. return some;
  1427. }
  1428. static void
  1429. interrupt(Ureg*, void* a)
  1430. {
  1431. ehciintr(a);
  1432. }
  1433. static int
  1434. portenable(Hci *hp, int port, int on)
  1435. {
  1436. Ctlr *ctlr;
  1437. Eopio *opio;
  1438. int s;
  1439. ctlr = hp->aux;
  1440. opio = ctlr->opio;
  1441. s = opio->portsc[port-1];
  1442. qlock(&ctlr->portlck);
  1443. if(waserror()){
  1444. qunlock(&ctlr->portlck);
  1445. nexterror();
  1446. }
  1447. dprint("ehci %#p port %d enable=%d; sts %#x\n",
  1448. ctlr->capio, port, on, s);
  1449. ilock(ctlr);
  1450. if(s & (Psstatuschg | Pschange))
  1451. opio->portsc[port-1] = s;
  1452. if(on)
  1453. opio->portsc[port-1] |= Psenable;
  1454. else
  1455. opio->portsc[port-1] &= ~Psenable;
  1456. microdelay(64);
  1457. iunlock(ctlr);
  1458. tsleep(&up->sleep, return0, 0, Enabledelay);
  1459. dprint("ehci %#p port %d enable=%d: sts %#lux\n",
  1460. ctlr->capio, port, on, opio->portsc[port-1]);
  1461. qunlock(&ctlr->portlck);
  1462. poperror();
  1463. return 0;
  1464. }
  1465. /*
  1466. * If we detect during status that the port is low-speed or
  1467. * during reset that it's full-speed, the device is not for
  1468. * ourselves. The companion controller will take care.
  1469. * Low-speed devices will not be seen by usbd. Full-speed
  1470. * ones are seen because it's only after reset that we know what
  1471. * they are (usbd may notice a device not enabled in this case).
  1472. */
  1473. static void
  1474. portlend(Ctlr *ctlr, int port, char *ss)
  1475. {
  1476. Eopio *opio;
  1477. ulong s;
  1478. opio = ctlr->opio;
  1479. dprint("ehci %#p port %d: %s speed device: no longer owned\n",
  1480. ctlr->capio, port, ss);
  1481. s = opio->portsc[port-1];
  1482. s &= ~(Pschange|Psstatuschg);
  1483. s |= Psowner;
  1484. opio->portsc[port-1] = s;
  1485. }
  1486. static int
  1487. portreset(Hci *hp, int port, int on)
  1488. {
  1489. ulong s;
  1490. Eopio *opio;
  1491. Ctlr *ctlr;
  1492. int i;
  1493. if(on == 0)
  1494. return 0;
  1495. ctlr = hp->aux;
  1496. opio = ctlr->opio;
  1497. qlock(&ctlr->portlck);
  1498. if(waserror()){
  1499. iunlock(ctlr);
  1500. qunlock(&ctlr->portlck);
  1501. nexterror();
  1502. }
  1503. s = opio->portsc[port-1];
  1504. dprint("ehci %#p port %d reset; sts %#lux\n", ctlr->capio, port, s);
  1505. ilock(ctlr);
  1506. s &= ~(Psenable|Psreset);
  1507. opio->portsc[port-1] = s|Psreset;
  1508. for(i = 0; i < 10; i++){
  1509. delay(10);
  1510. if((opio->portsc[port-1] & Psreset) == 0)
  1511. break;
  1512. }
  1513. opio->portsc[port-1] &= ~Psreset;
  1514. delay(10);
  1515. if((opio->portsc[port-1] & Psenable) == 0)
  1516. portlend(ctlr, port, "full");
  1517. iunlock(ctlr);
  1518. dprint("ehci %#p after port %d reset; sts %#lux\n",
  1519. ctlr->capio, port, opio->portsc[port-1]);
  1520. qunlock(&ctlr->portlck);
  1521. poperror();
  1522. return 0;
  1523. }
  1524. static int
  1525. portstatus(Hci *hp, int port)
  1526. {
  1527. int s;
  1528. int r;
  1529. Eopio *opio;
  1530. Ctlr *ctlr;
  1531. ctlr = hp->aux;
  1532. opio = ctlr->opio;
  1533. qlock(&ctlr->portlck);
  1534. if(waserror()){
  1535. iunlock(ctlr);
  1536. qunlock(&ctlr->portlck);
  1537. nexterror();
  1538. }
  1539. ilock(ctlr);
  1540. s = opio->portsc[port-1];
  1541. if(s & (Psstatuschg | Pschange)){
  1542. opio->portsc[port-1] = s;
  1543. ddprint("ehci %#p port %d status %#x\n", ctlr->capio, port, s);
  1544. }
  1545. /*
  1546. * If the port is a low speed port we yield ownership now
  1547. * to the [uo]hci companion controller and pretend it's not here.
  1548. */
  1549. if((s & Pspresent) != 0 && (s & Pslinemask) == Pslow){
  1550. portlend(ctlr, port, "low");
  1551. s &= ~Pspresent; /* not for us this time */
  1552. }
  1553. iunlock(ctlr);
  1554. qunlock(&ctlr->portlck);
  1555. poperror();
  1556. /*
  1557. * We must return status bits as a
  1558. * get port status hub request would do.
  1559. */
  1560. r = 0;
  1561. if(s & Pspresent)
  1562. r |= HPpresent|HPhigh;
  1563. if(s & Psenable)
  1564. r |= HPenable;
  1565. if(s & Pssuspend)
  1566. r |= HPsuspend;
  1567. if(s & Psreset)
  1568. r |= HPreset;
  1569. if(s & Psstatuschg)
  1570. r |= HPstatuschg;
  1571. if(s & Pschange)
  1572. r |= HPchange;
  1573. return r;
  1574. }
  1575. static char*
  1576. seprintio(char *s, char *e, Qio *io, char *pref)
  1577. {
  1578. s = seprint(s,e,"%s io %#p qh %#p id %#x", pref, io, io->qh, io->usbid);
  1579. s = seprint(s,e," iot %ld", io->iotime);
  1580. s = seprint(s,e," tog %#x tok %#x err %s", io->toggle, io->tok, io->err);
  1581. return s;
  1582. }
  1583. static char*
  1584. seprintep(char *s, char *e, Ep *ep)
  1585. {
  1586. Qio *io;
  1587. Ctlio *cio;
  1588. Ctlr *ctlr;
  1589. ctlr = ep->hp->aux;
  1590. ilock(ctlr);
  1591. if(ep->aux == nil){
  1592. *s = 0;
  1593. iunlock(ctlr);
  1594. return s;
  1595. }
  1596. switch(ep->ttype){
  1597. case Tctl:
  1598. cio = ep->aux;
  1599. s = seprintio(s, e, cio, "c");
  1600. s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
  1601. break;
  1602. case Tbulk:
  1603. case Tintr:
  1604. io = ep->aux;
  1605. if(ep->mode != OWRITE)
  1606. s = seprintio(s, e, &io[OREAD], "r");
  1607. if(ep->mode != OREAD)
  1608. s = seprintio(s, e, &io[OWRITE], "w");
  1609. break;
  1610. case Tiso:
  1611. *s = 0;
  1612. break;
  1613. }
  1614. iunlock(ctlr);
  1615. return s;
  1616. }
  1617. /*
  1618. * halt condition was cleared on the endpoint. update our toggles.
  1619. */
  1620. static void
  1621. clrhalt(Ep *ep)
  1622. {
  1623. Qio *io;
  1624. ep->clrhalt = 0;
  1625. switch(ep->ttype){
  1626. case Tintr:
  1627. case Tbulk:
  1628. io = ep->aux;
  1629. if(ep->mode != OREAD){
  1630. qlock(&io[OWRITE]);
  1631. io[OWRITE].toggle = Tddata0;
  1632. deprint("ep clrhalt for io %#p\n", io+OWRITE);
  1633. qunlock(&io[OWRITE]);
  1634. }
  1635. if(ep->mode != OWRITE){
  1636. qlock(&io[OREAD]);
  1637. io[OREAD].toggle = Tddata0;
  1638. deprint("ep clrhalt for io %#p\n", io+OREAD);
  1639. qunlock(&io[OREAD]);
  1640. }
  1641. break;
  1642. }
  1643. }
  1644. static void
  1645. xdump(char* pref, void *qh)
  1646. {
  1647. int i;
  1648. ulong *u;
  1649. u = qh;
  1650. print("%s %#p:", pref, u);
  1651. for(i = 0; i < 16; i++)
  1652. if((i%4) == 0)
  1653. print("\n %#8.8ulx", u[i]);
  1654. else
  1655. print(" %#8.8ulx", u[i]);
  1656. print("\n");
  1657. }
  1658. static long
  1659. episohscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
  1660. {
  1661. int nr;
  1662. long tot;
  1663. Itd *tdu;
  1664. for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
  1665. tdu = iso->tdu;
  1666. if(itdactive(tdu))
  1667. break;
  1668. nr = tdu->ndata;
  1669. if(tot + nr > count)
  1670. nr = count - tot;
  1671. if(nr == 0)
  1672. print("ehci: ep%d.%d: too many polls\n",
  1673. ep->dev->nb, ep->nb);
  1674. else{
  1675. iunlock(ctlr); /* We could page fault here */
  1676. memmove(b+tot, tdu->data, nr);
  1677. ilock(ctlr);
  1678. if(nr < tdu->ndata)
  1679. memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
  1680. tdu->ndata -= nr;
  1681. }
  1682. if(tdu->ndata == 0){
  1683. itdinit(iso, tdu);
  1684. iso->tdu = tdu->next;
  1685. }
  1686. }
  1687. return tot;
  1688. }
  1689. static long
  1690. episofscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
  1691. {
  1692. int nr;
  1693. long tot;
  1694. Sitd *stdu;
  1695. for(tot = 0; iso->stdi != iso->stdu && tot < count; tot += nr){
  1696. stdu = iso->stdu;
  1697. if(stdu->csw & Stdactive){
  1698. diprint("ehci: episoread: %#p tdu active\n", iso);
  1699. break;
  1700. }
  1701. nr = stdu->ndata;
  1702. if(tot + nr > count)
  1703. nr = count - tot;
  1704. if(nr == 0)
  1705. print("ehci: ep%d.%d: too many polls\n",
  1706. ep->dev->nb, ep->nb);
  1707. else{
  1708. iunlock(ctlr); /* We could page fault here */
  1709. memmove(b+tot, stdu->data, nr);
  1710. ilock(ctlr);
  1711. if(nr < stdu->ndata)
  1712. memmove(stdu->data,stdu->data+nr,stdu->ndata - nr);
  1713. stdu->ndata -= nr;
  1714. }
  1715. if(stdu->ndata == 0){
  1716. sitdinit(iso, stdu);
  1717. iso->stdu = stdu->next;
  1718. }
  1719. }
  1720. return tot;
  1721. }
  1722. static long
  1723. episoread(Ep *ep, Isoio *iso, void *a, long count)
  1724. {
  1725. Ctlr *ctlr;
  1726. uchar *b;
  1727. long tot;
  1728. iso->debug = ep->debug;
  1729. diprint("ehci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  1730. b = a;
  1731. ctlr = ep->hp->aux;
  1732. qlock(iso);
  1733. if(waserror()){
  1734. qunlock(iso);
  1735. nexterror();
  1736. }
  1737. iso->err = nil;
  1738. iso->nerrs = 0;
  1739. ilock(ctlr);
  1740. if(iso->state == Qclose){
  1741. iunlock(ctlr);
  1742. error(iso->err ? iso->err : Eio);
  1743. }
  1744. iso->state = Qrun;
  1745. while(isocanread(iso) == 0){
  1746. iunlock(ctlr);
  1747. diprint("ehci: episoread: %#p sleep\n", iso);
  1748. if(waserror()){
  1749. if(iso->err == nil)
  1750. iso->err = "I/O timed out";
  1751. ilock(ctlr);
  1752. break;
  1753. }
  1754. tsleep(iso, isocanread, iso, ep->tmout);
  1755. poperror();
  1756. ilock(ctlr);
  1757. }
  1758. if(iso->state == Qclose){
  1759. iunlock(ctlr);
  1760. error(iso->err ? iso->err : Eio);
  1761. }
  1762. iso->state = Qdone;
  1763. assert(iso->tdu != iso->tdi);
  1764. if(iso->hs != 0)
  1765. tot = episohscpy(ctlr, ep, iso, b, count);
  1766. else
  1767. tot = episofscpy(ctlr, ep, iso, b, count);
  1768. iunlock(ctlr);
  1769. qunlock(iso);
  1770. poperror();
  1771. diprint("uhci: episoread: %#p %uld bytes err '%s'\n", iso, tot, iso->err);
  1772. if(iso->err != nil)
  1773. error(iso->err);
  1774. return tot;
  1775. }
  1776. /*
  1777. * iso->tdu is the next place to put data. When it gets full
  1778. * it is activated and tdu advanced.
  1779. */
  1780. static long
  1781. putsamples(Isoio *iso, uchar *b, long count)
  1782. {
  1783. long tot;
  1784. long n;
  1785. for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
  1786. n = count-tot;
  1787. if(iso->hs != 0){
  1788. if(n > iso->tdu->mdata - iso->nleft)
  1789. n = iso->tdu->mdata - iso->nleft;
  1790. memmove(iso->tdu->data+iso->nleft, b+tot, n);
  1791. iso->nleft += n;
  1792. if(iso->nleft == iso->tdu->mdata){
  1793. itdinit(iso, iso->tdu);
  1794. iso->nleft = 0;
  1795. iso->tdu = iso->tdu->next;
  1796. }
  1797. }else{
  1798. if(n > iso->stdu->mdata - iso->nleft)
  1799. n = iso->stdu->mdata - iso->nleft;
  1800. memmove(iso->stdu->data+iso->nleft, b+tot, n);
  1801. iso->nleft += n;
  1802. if(iso->nleft == iso->stdu->mdata){
  1803. sitdinit(iso, iso->stdu);
  1804. iso->nleft = 0;
  1805. iso->stdu = iso->stdu->next;
  1806. }
  1807. }
  1808. }
  1809. return tot;
  1810. }
  1811. /*
  1812. * Queue data for writing and return error status from
  1813. * last writes done, to maintain buffered data.
  1814. */
  1815. static long
  1816. episowrite(Ep *ep, Isoio *iso, void *a, long count)
  1817. {
  1818. Ctlr *ctlr;
  1819. uchar *b;
  1820. int tot;
  1821. int nw;
  1822. char *err;
  1823. iso->debug = ep->debug;
  1824. diprint("ehci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  1825. ctlr = ep->hp->aux;
  1826. qlock(iso);
  1827. if(waserror()){
  1828. qunlock(iso);
  1829. nexterror();
  1830. }
  1831. ilock(ctlr);
  1832. if(iso->state == Qclose){
  1833. iunlock(ctlr);
  1834. error(iso->err ? iso->err : Eio);
  1835. }
  1836. iso->state = Qrun;
  1837. b = a;
  1838. for(tot = 0; tot < count; tot += nw){
  1839. while(isocanwrite(iso) == 0){
  1840. iunlock(ctlr);
  1841. diprint("ehci: episowrite: %#p sleep\n", iso);
  1842. if(waserror()){
  1843. if(iso->err == nil)
  1844. iso->err = "I/O timed out";
  1845. ilock(ctlr);
  1846. break;
  1847. }
  1848. tsleep(iso, isocanwrite, iso, ep->tmout);
  1849. poperror();
  1850. ilock(ctlr);
  1851. }
  1852. err = iso->err;
  1853. iso->err = nil;
  1854. if(iso->state == Qclose || err != nil){
  1855. iunlock(ctlr);
  1856. error(err ? err : Eio);
  1857. }
  1858. if(iso->state != Qrun)
  1859. panic("episowrite: iso not running");
  1860. iunlock(ctlr); /* We could page fault here */
  1861. nw = putsamples(iso, b+tot, count-tot);
  1862. ilock(ctlr);
  1863. }
  1864. if(iso->state != Qclose)
  1865. iso->state = Qdone;
  1866. iunlock(ctlr);
  1867. err = iso->err; /* in case it failed early */
  1868. iso->err = nil;
  1869. qunlock(iso);
  1870. poperror();
  1871. if(err != nil)
  1872. error(err);
  1873. diprint("ehci: episowrite: %#p %d bytes\n", iso, tot);
  1874. return tot;
  1875. }
  1876. static int
  1877. nexttoggle(int toggle, int count, int maxpkt)
  1878. {
  1879. int np;
  1880. np = count / maxpkt;
  1881. if(np == 0)
  1882. np = 1;
  1883. if((np % 2) == 0)
  1884. return toggle;
  1885. if(toggle == Tddata1)
  1886. return Tddata0;
  1887. else
  1888. return Tddata1;
  1889. }
  1890. static Td*
  1891. epgettd(Qio *io, int flags, void *a, int count, int maxpkt)
  1892. {
  1893. Td *td;
  1894. ulong pa;
  1895. int i;
  1896. if(count > Tdmaxpkt)
  1897. panic("ehci: epgettd: too many bytes");
  1898. td = tdalloc();
  1899. td->csw = flags;
  1900. td->csw |= io->toggle | io->tok | (count << Tdlenshift);
  1901. td->csw |= Tderr2|Tderr1;
  1902. /*
  1903. * use the space wasted by alignment as an
  1904. * embedded buffer if count bytes fit in there.
  1905. */
  1906. assert(Align > sizeof(Td));
  1907. if(count <= Align - sizeof(Td)){
  1908. td->data = td->sbuff;
  1909. td->buff = nil;
  1910. }else
  1911. td->data = td->buff = smalloc(Tdmaxpkt);
  1912. pa = PADDR(td->data);
  1913. for(i = 0; i < nelem(td->buffer); i++){
  1914. td->buffer[i] = pa;
  1915. if(i > 0)
  1916. td->buffer[i] &= ~0xFFF;
  1917. pa += 0x1000;
  1918. }
  1919. td->ndata = count;
  1920. if(a != nil && count > 0)
  1921. memmove(td->data, a, count);
  1922. io->toggle = nexttoggle(io->toggle, count, maxpkt);
  1923. return td;
  1924. }
  1925. /*
  1926. * Try to get them idle
  1927. */
  1928. static void
  1929. aborttds(Qh *qh)
  1930. {
  1931. Td *td;
  1932. qh->state = Qdone;
  1933. if(qh->sched >= 0 && (qh->eps0&Qhspeedmask) != Qhhigh)
  1934. qh->eps0 |= Qhint; /* inactivate on next pass */
  1935. for(td = qh->tds; td != nil; td = td->next){
  1936. if(td->csw & Tdactive)
  1937. td->ndata = 0;
  1938. td->csw |= Tdhalt;
  1939. }
  1940. }
  1941. /*
  1942. * Some controllers do not post the usb/error interrupt after
  1943. * the work has been done. It seems that we must poll for them.
  1944. */
  1945. static int
  1946. workpending(void *a)
  1947. {
  1948. Ctlr *ctlr;
  1949. ctlr = a;
  1950. return ctlr->nreqs > 0;
  1951. }
  1952. static void
  1953. ehcipoll(void* a)
  1954. {
  1955. Hci *hp;
  1956. Ctlr *ctlr;
  1957. Poll *poll;
  1958. int i;
  1959. hp = a;
  1960. ctlr = hp->aux;
  1961. poll = &ctlr->poll;
  1962. for(;;){
  1963. if(ctlr->nreqs == 0){
  1964. if(0)ddprint("ehcipoll %#p sleep\n", ctlr->capio);
  1965. sleep(poll, workpending, ctlr);
  1966. if(0)ddprint("ehcipoll %#p awaken\n", ctlr->capio);
  1967. }
  1968. for(i = 0; i < 16 && ctlr->nreqs > 0; i++)
  1969. if(ehciintr(hp) == 0)
  1970. break;
  1971. do{
  1972. tsleep(&up->sleep, return0, 0, 1);
  1973. ehciintr(hp);
  1974. }while(ctlr->nreqs > 0);
  1975. }
  1976. }
  1977. static void
  1978. pollcheck(Hci *hp)
  1979. {
  1980. Ctlr *ctlr;
  1981. Poll *poll;
  1982. ctlr = hp->aux;
  1983. poll = &ctlr->poll;
  1984. if(poll->must != 0 && poll->does == 0){
  1985. lock(poll);
  1986. if(poll->must != 0 && poll->does == 0){
  1987. poll->does++;
  1988. print("ehci %#p: polling\n", ctlr->capio);
  1989. kproc("ehcipoll", ehcipoll, hp);
  1990. }
  1991. unlock(poll);
  1992. }
  1993. }
  1994. static int
  1995. epiodone(void *a)
  1996. {
  1997. Qh *qh;
  1998. qh = a;
  1999. return qh->state != Qrun;
  2000. }
  2001. static void
  2002. epiowait(Hci *hp, Qio *io, int tmout, ulong load)
  2003. {
  2004. Qh *qh;
  2005. int timedout;
  2006. Ctlr *ctlr;
  2007. ctlr = hp->aux;
  2008. qh = io->qh;
  2009. ddqprint("ehci io %#p sleep on qh %#p state %s\n",
  2010. io, qh, qhsname[qh->state]);
  2011. timedout = 0;
  2012. if(waserror()){
  2013. dqprint("ehci io %#p qh %#p timed out\n", io, qh);
  2014. timedout++;
  2015. }else{
  2016. if(tmout == 0)
  2017. sleep(io, epiodone, qh);
  2018. else
  2019. tsleep(io, epiodone, qh, tmout);
  2020. poperror();
  2021. }
  2022. ilock(ctlr);
  2023. /* Are we missing interrupts? */
  2024. if(qh->state == Qrun){
  2025. iunlock(ctlr);
  2026. ehciintr(hp);
  2027. ilock(ctlr);
  2028. if(qh->state == Qdone){
  2029. dqprint("ehci %#p: polling required\n", ctlr->capio);
  2030. ctlr->poll.must = 1;
  2031. pollcheck(hp);
  2032. }
  2033. }
  2034. if(qh->state == Qrun){
  2035. dqprint("ehci io %#p qh %#p timed out (no intr?)\n", io, qh);
  2036. timedout = 1;
  2037. }else if(qh->state != Qdone && qh->state != Qclose)
  2038. panic("ehci: epio: queue state %d", qh->state);
  2039. if(timedout){
  2040. aborttds(io->qh);
  2041. io->err = "request timed out";
  2042. iunlock(ctlr);
  2043. if(!waserror()){
  2044. tsleep(&up->sleep, return0, 0, Abortdelay);
  2045. poperror();
  2046. }
  2047. ilock(ctlr);
  2048. }
  2049. if(qh->state != Qclose)
  2050. qh->state = Qidle;
  2051. qhlinktd(qh, nil);
  2052. ctlr->load -= load;
  2053. ctlr->nreqs--;
  2054. iunlock(ctlr);
  2055. }
  2056. /*
  2057. * Non iso I/O.
  2058. * To make it work for control transfers, the caller may
  2059. * lock the Qio for the entire control transfer.
  2060. */
  2061. static long
  2062. epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
  2063. {
  2064. Td *td, *ltd, *td0, *ntd;
  2065. Ctlr *ctlr;
  2066. Qh* qh;
  2067. long n, tot;
  2068. char buf[128];
  2069. uchar *c;
  2070. int saved, ntds, tmout;
  2071. ulong load;
  2072. char *err;
  2073. qh = io->qh;
  2074. ctlr = ep->hp->aux;
  2075. io->debug = ep->debug;
  2076. tmout = ep->tmout;
  2077. ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
  2078. io->tok == Tdtokin ? "in" : "out",
  2079. ep->dev->nb, ep->nb, io, count, ctlr->load);
  2080. if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
  2081. seprintdata(buf, buf+sizeof(buf), a, count);
  2082. print("echi epio: user data: %s\n", buf);
  2083. }
  2084. if(mustlock){
  2085. qlock(io);
  2086. if(waserror()){
  2087. qunlock(io);
  2088. nexterror();
  2089. }
  2090. }
  2091. io->err = nil;
  2092. ilock(ctlr);
  2093. if(qh->state == Qclose){ /* Tds released by cancelio */
  2094. iunlock(ctlr);
  2095. error(io->err ? io->err : Eio);
  2096. }
  2097. if(qh->state != Qidle)
  2098. panic("epio: qh not idle");
  2099. qh->state = Qinstall;
  2100. iunlock(ctlr);
  2101. c = a;
  2102. td0 = ltd = nil;
  2103. load = tot = 0;
  2104. do{
  2105. n = (Tdmaxpkt / ep->maxpkt) * ep->maxpkt;
  2106. if(count-tot < n)
  2107. n = count-tot;
  2108. if(io->tok != Tdtokin)
  2109. td = epgettd(io, Tdactive, c+tot, n, ep->maxpkt);
  2110. else
  2111. td = epgettd(io, Tdactive, nil, n, ep->maxpkt);
  2112. if(td0 == nil)
  2113. td0 = td;
  2114. else
  2115. tdlinktd(ltd, td);
  2116. ltd = td;
  2117. tot += n;
  2118. load += ep->load;
  2119. }while(tot < count);
  2120. if(td0 == nil || ltd == nil)
  2121. panic("epio: no td");
  2122. ltd->csw |= Tdioc; /* the last one interrupts */
  2123. ddeprint("ehci: load %uld ctlr load %uld\n", load, ctlr->load);
  2124. if(debug > 1 || ep->debug > 1)
  2125. dumptd(td0, "epio: put: ");
  2126. ilock(ctlr);
  2127. if(qh->state != Qclose){
  2128. io->iotime = TK2MS(MACHP(0)->ticks);
  2129. qh->state = Qrun;
  2130. qhlinktd(qh, td0);
  2131. ctlr->nreqs++;
  2132. ctlr->load += load;
  2133. }
  2134. iunlock(ctlr);
  2135. if(ctlr->poll.does)
  2136. wakeup(&ctlr->poll);
  2137. epiowait(ep->hp, io, tmout, load);
  2138. if(debug > 1 || ep->debug > 1){
  2139. dumptd(td0, "epio: got: ");
  2140. qhdump(qh);
  2141. }
  2142. tot = 0;
  2143. c = a;
  2144. saved = 0;
  2145. ntds = 0;
  2146. for(td = td0; td != nil; td = ntd){
  2147. ntds++;
  2148. /*
  2149. * Use td tok, not io tok, because of setup packets.
  2150. * Also, if the Td was stalled or active (previous Td
  2151. * was a short packet), we must save the toggle as it is.
  2152. */
  2153. if(td->csw & (Tdhalt|Tdactive)){
  2154. if(saved++ == 0)
  2155. io->toggle = td->csw & Tddata1;
  2156. }else{
  2157. tot += td->ndata;
  2158. if((td->csw & Tdtok) == Tdtokin && td->ndata > 0){
  2159. memmove(c, td->data, td->ndata);
  2160. c += td->ndata;
  2161. }
  2162. }
  2163. ntd = td->next;
  2164. tdfree(td);
  2165. }
  2166. err = io->err;
  2167. if(mustlock){
  2168. qunlock(io);
  2169. poperror();
  2170. }
  2171. ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
  2172. io, ntds, tot, err);
  2173. if(err == Estalled)
  2174. return 0; /* that's our convention */
  2175. if(err != nil)
  2176. error(err);
  2177. if(tot < 0)
  2178. error(Eio);
  2179. return tot;
  2180. }
  2181. static long
  2182. epread(Ep *ep, void *a, long count)
  2183. {
  2184. Ctlio *cio;
  2185. Qio *io;
  2186. Isoio *iso;
  2187. char buf[160];
  2188. ulong delta;
  2189. ddeprint("ehci: epread\n");
  2190. if(ep->aux == nil)
  2191. panic("epread: not open");
  2192. pollcheck(ep->hp);
  2193. switch(ep->ttype){
  2194. case Tctl:
  2195. cio = ep->aux;
  2196. qlock(cio);
  2197. if(waserror()){
  2198. qunlock(cio);
  2199. nexterror();
  2200. }
  2201. ddeprint("epread ctl ndata %d\n", cio->ndata);
  2202. if(cio->ndata < 0)
  2203. error("request expected");
  2204. else if(cio->ndata == 0){
  2205. cio->ndata = -1;
  2206. count = 0;
  2207. }else{
  2208. if(count > cio->ndata)
  2209. count = cio->ndata;
  2210. if(count > 0)
  2211. memmove(a, cio->data, count);
  2212. /* BUG for big transfers */
  2213. free(cio->data);
  2214. cio->data = nil;
  2215. cio->ndata = 0; /* signal EOF next time */
  2216. }
  2217. qunlock(cio);
  2218. poperror();
  2219. if(debug>1 || ep->debug){
  2220. seprintdata(buf, buf+sizeof(buf), a, count);
  2221. print("epread: %s\n", buf);
  2222. }
  2223. return count;
  2224. case Tbulk:
  2225. io = ep->aux;
  2226. if(ep->clrhalt)
  2227. clrhalt(ep);
  2228. return epio(ep, &io[OREAD], a, count, 1);
  2229. case Tintr:
  2230. io = ep->aux;
  2231. delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
  2232. if(delta < ep->pollival / 2)
  2233. tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
  2234. if(ep->clrhalt)
  2235. clrhalt(ep);
  2236. return epio(ep, &io[OREAD], a, count, 1);
  2237. case Tiso:
  2238. iso = ep->aux;
  2239. return episoread(ep, iso, a, count);
  2240. }
  2241. return -1;
  2242. }
  2243. /*
  2244. * Control transfers are one setup write (data0)
  2245. * plus zero or more reads/writes (data1, data0, ...)
  2246. * plus a final write/read with data1 to ack.
  2247. * For both host to device and device to host we perform
  2248. * the entire transfer when the user writes the request,
  2249. * and keep any data read from the device for a later read.
  2250. * We call epio three times instead of placing all Tds at
  2251. * the same time because doing so leads to crc/tmout errors
  2252. * for some devices.
  2253. * Upon errors on the data phase we must still run the status
  2254. * phase or the device may cease responding in the future.
  2255. */
  2256. static long
  2257. epctlio(Ep *ep, Ctlio *cio, void *a, long count)
  2258. {
  2259. uchar *c;
  2260. long len;
  2261. ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
  2262. cio, ep->dev->nb, ep->nb, count);
  2263. if(count < Rsetuplen)
  2264. error("short usb comand");
  2265. qlock(cio);
  2266. free(cio->data);
  2267. cio->data = nil;
  2268. cio->ndata = 0;
  2269. if(waserror()){
  2270. qunlock(cio);
  2271. free(cio->data);
  2272. cio->data = nil;
  2273. cio->ndata = 0;
  2274. nexterror();
  2275. }
  2276. /* set the address if unset and out of configuration state */
  2277. if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
  2278. if(cio->usbid == 0){
  2279. cio->usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb&Devmax);
  2280. qhsetaddr(cio->qh, cio->usbid);
  2281. }
  2282. /* adjust maxpkt if the user has learned a different one */
  2283. if(qhmaxpkt(cio->qh) != ep->maxpkt)
  2284. qhsetmaxpkt(cio->qh, ep->maxpkt);
  2285. c = a;
  2286. cio->tok = Tdtoksetup;
  2287. cio->toggle = Tddata0;
  2288. if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
  2289. error(Eio);
  2290. a = c + Rsetuplen;
  2291. count -= Rsetuplen;
  2292. cio->toggle = Tddata1;
  2293. if(c[Rtype] & Rd2h){
  2294. cio->tok = Tdtokin;
  2295. len = GET2(c+Rcount);
  2296. if(len <= 0)
  2297. error("bad length in d2h request");
  2298. if(len > Maxctllen)
  2299. error("d2h data too large to fit in ehci");
  2300. a = cio->data = smalloc(len+1);
  2301. }else{
  2302. cio->tok = Tdtokout;
  2303. len = count;
  2304. }
  2305. if(len > 0)
  2306. if(waserror())
  2307. len = -1;
  2308. else{
  2309. len = epio(ep, cio, a, len, 0);
  2310. poperror();
  2311. }
  2312. if(c[Rtype] & Rd2h){
  2313. count = Rsetuplen;
  2314. cio->ndata = len;
  2315. cio->tok = Tdtokout;
  2316. }else{
  2317. if(len < 0)
  2318. count = -1;
  2319. else
  2320. count = Rsetuplen + len;
  2321. cio->tok = Tdtokin;
  2322. }
  2323. cio->toggle = Tddata1;
  2324. epio(ep, cio, nil, 0, 0);
  2325. qunlock(cio);
  2326. poperror();
  2327. ddeprint("epctlio cio %#p return %ld\n", cio, count);
  2328. return count;
  2329. }
  2330. static long
  2331. epwrite(Ep *ep, void *a, long count)
  2332. {
  2333. Qio *io;
  2334. Ctlio *cio;
  2335. Isoio *iso;
  2336. ulong delta;
  2337. pollcheck(ep->hp);
  2338. ddeprint("ehci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
  2339. if(ep->aux == nil)
  2340. panic("ehci: epwrite: not open");
  2341. switch(ep->ttype){
  2342. case Tctl:
  2343. cio = ep->aux;
  2344. return epctlio(ep, cio, a, count);
  2345. case Tbulk:
  2346. io = ep->aux;
  2347. if(ep->clrhalt)
  2348. clrhalt(ep);
  2349. return epio(ep, &io[OWRITE], a, count, 1);
  2350. case Tintr:
  2351. io = ep->aux;
  2352. delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
  2353. if(delta < ep->pollival)
  2354. tsleep(&up->sleep, return0, 0, ep->pollival - delta);
  2355. if(ep->clrhalt)
  2356. clrhalt(ep);
  2357. return epio(ep, &io[OWRITE], a, count, 1);
  2358. case Tiso:
  2359. iso = ep->aux;
  2360. return episowrite(ep, iso, a, count);
  2361. }
  2362. return -1;
  2363. }
  2364. static void
  2365. isofsinit(Ep *ep, Isoio *iso)
  2366. {
  2367. long left;
  2368. Sitd *td;
  2369. Sitd *ltd;
  2370. int i;
  2371. ulong frno;
  2372. left = 0;
  2373. ltd = nil;
  2374. frno = iso->td0frno;
  2375. for(i = 0; i < iso->nframes; i++){
  2376. td = iso->sitdps[frno] = sitdalloc();
  2377. td->data = iso->data + i * ep->maxpkt;
  2378. td->epc = ep->dev->port << Stdportshift;
  2379. td->epc |= ep->dev->hub << Stdhubshift;
  2380. td->epc |= ep->nb << Stdepshift;
  2381. td->epc |= ep->dev->nb << Stddevshift;
  2382. td->mfs = (034 << Stdscmshift) | (1 << Stdssmshift);
  2383. if(ep->mode == OREAD){
  2384. td->epc |= Stdin;
  2385. td->mdata = ep->maxpkt;
  2386. }else{
  2387. td->mdata = (ep->hz+left) * ep->pollival / 1000;
  2388. td->mdata *= ep->samplesz;
  2389. left = (ep->hz+left) * ep->pollival % 1000;
  2390. if(td->mdata > ep->maxpkt){
  2391. print("ehci: ep%d.%d: size > maxpkt\n",
  2392. ep->dev->nb, ep->nb);
  2393. print("size = %ld max = %ld\n",
  2394. td->mdata,ep->maxpkt);
  2395. td->mdata = ep->maxpkt;
  2396. }
  2397. }
  2398. sitdinit(iso, td);
  2399. if(ltd != nil)
  2400. ltd->next = td;
  2401. ltd = td;
  2402. frno = TRUNC(frno+ep->pollival, Nisoframes);
  2403. }
  2404. ltd->next = iso->sitdps[iso->td0frno];
  2405. }
  2406. static void
  2407. isohsinit(Ep *ep, Isoio *iso)
  2408. {
  2409. long left;
  2410. Itd *td;
  2411. Itd *ltd;
  2412. ulong i;
  2413. ulong pa;
  2414. int p;
  2415. ulong frno;
  2416. int ival;
  2417. iso->hs = 1;
  2418. ival = 1;
  2419. if(ep->pollival > 8)
  2420. ival = ep->pollival/8;
  2421. left = 0;
  2422. ltd = nil;
  2423. frno = iso->td0frno;
  2424. for(i = 0; i < iso->nframes; i++){
  2425. td = iso->itdps[frno] = itdalloc();
  2426. td->data = iso->data + i * 8 * iso->maxsize;
  2427. pa = PADDR(td->data) & ~0xFFF;
  2428. for(p = 0; p < 8; p++)
  2429. td->buffer[i] = pa + p * 0x1000;
  2430. td->buffer[0] = PADDR(iso->data) & ~0xFFF;
  2431. td->buffer[0] |= ep->nb << Itdepshift;
  2432. td->buffer[0] |= ep->dev->nb << Itddevshift;
  2433. if(ep->mode == OREAD)
  2434. td->buffer[1] |= Itdin;
  2435. else
  2436. td->buffer[1] |= Itdout;
  2437. td->buffer[1] |= ep->maxpkt << Itdmaxpktshift;
  2438. td->buffer[2] |= ep->ntds << Itdntdsshift;
  2439. if(ep->mode == OREAD)
  2440. td->mdata = 8 * iso->maxsize;
  2441. else{
  2442. td->mdata = (ep->hz + left) * ep->pollival / 1000;
  2443. td->mdata *= ep->samplesz;
  2444. left = (ep->hz + left) * ep->pollival % 1000;
  2445. }
  2446. itdinit(iso, td);
  2447. if(ltd != nil)
  2448. ltd->next = td;
  2449. ltd = td;
  2450. frno = TRUNC(frno + ival, Nisoframes);
  2451. }
  2452. }
  2453. static void
  2454. isoopen(Ctlr *ctlr, Ep *ep)
  2455. {
  2456. Isoio *iso;
  2457. int ival; /* pollival in ms */
  2458. int n;
  2459. ulong frno;
  2460. int i;
  2461. int w;
  2462. int woff;
  2463. int tpf; /* tds per frame */
  2464. iso = ep->aux;
  2465. switch(ep->mode){
  2466. case OREAD:
  2467. iso->tok = Tdtokin;
  2468. break;
  2469. case OWRITE:
  2470. iso->tok = Tdtokout;
  2471. break;
  2472. default:
  2473. error("iso i/o is half-duplex");
  2474. }
  2475. iso->usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
  2476. iso->state = Qidle;
  2477. iso->debug = ep->debug;
  2478. ival = ep->pollival;
  2479. tpf = 1;
  2480. if(ep->dev->speed == Highspeed){
  2481. tpf = 8;
  2482. if(ival <= 8)
  2483. ival = 1;
  2484. else
  2485. ival /= 8;
  2486. }
  2487. iso->nframes = Nisoframes / ival;
  2488. if(iso->nframes < 3)
  2489. error("uhci isoopen bug"); /* we need at least 3 tds */
  2490. iso->maxsize = ep->ntds * ep->maxpkt;
  2491. if(ctlr->load + ep->load > 800)
  2492. print("usb: ehci: bandwidth may be exceeded\n");
  2493. ilock(ctlr);
  2494. ctlr->load += ep->load;
  2495. ctlr->isoload += ep->load;
  2496. ctlr->nreqs++;
  2497. dprint("ehci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
  2498. diprint("iso nframes %d pollival %uld ival %d maxpkt %uld ntds %d\n",
  2499. iso->nframes, ep->pollival, ival, ep->maxpkt, ep->ntds);
  2500. iunlock(ctlr);
  2501. if(ctlr->poll.does)
  2502. wakeup(&ctlr->poll);
  2503. /*
  2504. * From here on this cannot raise errors
  2505. * unless we catch them and release here all memory allocated.
  2506. */
  2507. assert(ep->maxpkt > 0 && ep->ntds > 0 && ep->ntds < 4);
  2508. assert(ep->maxpkt <= 1024);
  2509. iso->tdps = smalloc(sizeof(uintptr) * Nisoframes);
  2510. iso->data = smalloc(iso->nframes * tpf * ep->ntds * ep->maxpkt);
  2511. iso->td0frno = TRUNC(ctlr->opio->frno + 10, Nisoframes);
  2512. /* read: now; write: 1s ahead */
  2513. if(ep->dev->speed == Highspeed)
  2514. isohsinit(ep, iso);
  2515. else
  2516. isofsinit(ep, iso);
  2517. iso->tdu = iso->tdi = iso->itdps[iso->td0frno];
  2518. iso->stdu = iso->stdi = iso->sitdps[iso->td0frno];
  2519. ilock(ctlr);
  2520. frno = iso->td0frno;
  2521. for(i = 0; i < iso->nframes; i++){
  2522. *iso->tdps[frno] = ctlr->frames[frno];
  2523. frno = TRUNC(frno+ival, Nisoframes);
  2524. }
  2525. /*
  2526. * Iso uses a virtual frame window of Nisoframes, and we must
  2527. * fill the actual ctlr frame array by placing ctlr->nframes/Nisoframes
  2528. * copies of the window in the frame array.
  2529. */
  2530. assert(ctlr->nframes >= Nisoframes && Nisoframes >= iso->nframes);
  2531. assert(Nisoframes >= Nintrleafs);
  2532. n = ctlr->nframes / Nisoframes;
  2533. for(w = 0; w < n; w++){
  2534. frno = iso->td0frno;
  2535. woff = w * Nisoframes;
  2536. for(i = 0; i < iso->nframes ; i++){
  2537. assert(woff+frno < ctlr->nframes);
  2538. assert(iso->tdps[frno] != nil);
  2539. if(ep->dev->speed == Highspeed)
  2540. ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])|Litd;
  2541. else
  2542. ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])|Lsitd;
  2543. frno = TRUNC(frno+ep->pollival, Nisoframes);
  2544. }
  2545. }
  2546. iso->next = ctlr->iso;
  2547. ctlr->iso = iso;
  2548. iso->state = Qdone;
  2549. iunlock(ctlr);
  2550. if(debug > 1 || iso->debug >1)
  2551. isodump(iso, 0);
  2552. }
  2553. /*
  2554. * Allocate the endpoint and set it up for I/O
  2555. * in the controller. This must follow what's said
  2556. * in Ep regarding configuration, including perhaps
  2557. * the saved toggles (saved on a previous close of
  2558. * the endpoint data file by epclose).
  2559. */
  2560. static void
  2561. epopen(Ep *ep)
  2562. {
  2563. Ctlr *ctlr;
  2564. Ctlio *cio;
  2565. Qio *io;
  2566. int usbid;
  2567. ctlr = ep->hp->aux;
  2568. deprint("ehci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
  2569. if(ep->aux != nil)
  2570. panic("ehci: epopen called with open ep");
  2571. if(waserror()){
  2572. free(ep->aux);
  2573. ep->aux = nil;
  2574. nexterror();
  2575. }
  2576. switch(ep->ttype){
  2577. case Tnone:
  2578. error("endpoint not configured");
  2579. case Tiso:
  2580. ep->aux = smalloc(sizeof(Isoio));
  2581. isoopen(ctlr, ep);
  2582. break;
  2583. case Tctl:
  2584. cio = ep->aux = smalloc(sizeof(Ctlio));
  2585. cio->debug = ep->debug;
  2586. cio->ndata = -1;
  2587. cio->data = nil;
  2588. if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
  2589. break;
  2590. cio->qh = qhalloc(ctlr, ep, cio, "epc");
  2591. break;
  2592. case Tbulk:
  2593. ep->pollival = 1; /* assume this; doesn't really matter */
  2594. /* and fall... */
  2595. case Tintr:
  2596. io = ep->aux = smalloc(sizeof(Qio)*2);
  2597. io[OREAD].debug = io[OWRITE].debug = ep->debug;
  2598. usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb &Devmax);
  2599. if(ep->mode != OREAD){
  2600. if(ep->toggle[OWRITE] != 0)
  2601. io[OWRITE].toggle = Tddata1;
  2602. else
  2603. io[OWRITE].toggle = Tddata0;
  2604. io[OWRITE].tok = Tdtokout;
  2605. io[OWRITE].usbid = usbid;
  2606. io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  2607. io[OWRITE].qh = qhalloc(ctlr, ep, io+OWRITE, "epw");
  2608. }
  2609. if(ep->mode != OWRITE){
  2610. if(ep->toggle[OREAD] != 0)
  2611. io[OREAD].toggle = Tddata1;
  2612. else
  2613. io[OREAD].toggle = Tddata0;
  2614. io[OREAD].tok = Tdtokin;
  2615. io[OREAD].usbid = usbid;
  2616. io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  2617. io[OREAD].qh = qhalloc(ctlr, ep, io+OREAD, "epr");
  2618. }
  2619. break;
  2620. }
  2621. if(debug>1 || ep->debug)
  2622. dump(ep->hp);
  2623. deprint("ehci: epopen done\n");
  2624. poperror();
  2625. }
  2626. static void
  2627. cancelio(Ctlr *ctlr, Qio *io)
  2628. {
  2629. Qh *qh;
  2630. ilock(ctlr);
  2631. qh = io->qh;
  2632. if(io == nil || io->qh == nil || io->qh->state == Qclose){
  2633. iunlock(ctlr);
  2634. return;
  2635. }
  2636. dqprint("ehci: cancelio for qh %#p state %s\n",
  2637. qh, qhsname[qh->state]);
  2638. aborttds(qh);
  2639. qh->state = Qclose;
  2640. iunlock(ctlr);
  2641. if(!waserror()){
  2642. tsleep(&up->sleep, return0, 0, Abortdelay);
  2643. poperror();
  2644. }
  2645. wakeup(io);
  2646. qlock(io);
  2647. /* wait for epio if running */
  2648. qunlock(io);
  2649. qhfree(ctlr, qh);
  2650. io->qh = nil;
  2651. }
  2652. static void
  2653. cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, ulong load)
  2654. {
  2655. Isoio **il;
  2656. ulong *lp;
  2657. int i;
  2658. int frno;
  2659. int w;
  2660. int n;
  2661. int woff;
  2662. ulong *tp;
  2663. Itd *td;
  2664. Sitd *std;
  2665. int t;
  2666. ilock(ctlr);
  2667. if(iso->state == Qclose){
  2668. iunlock(ctlr);
  2669. return;
  2670. }
  2671. ctlr->nreqs--;
  2672. if(iso->state != Qrun && iso->state != Qdone)
  2673. panic("bad iso state");
  2674. iso->state = Qclose;
  2675. if(ctlr->isoload < load)
  2676. panic("ehci: low isoload");
  2677. ctlr->isoload -= load;
  2678. ctlr->load -= load;
  2679. for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
  2680. if(*il == iso)
  2681. break;
  2682. if(*il == nil)
  2683. panic("cancleiso: not found");
  2684. *il = iso->next;
  2685. frno = iso->td0frno;
  2686. for(i = 0; i < iso->nframes; i++){
  2687. tp = iso->tdps[frno];
  2688. if(iso->hs != 0){
  2689. td = iso->itdps[frno];
  2690. for(t = 0; t < nelem(td->csw); t++)
  2691. td->csw[1] &= ~(Itdioc|Itdactive);
  2692. }else{
  2693. std = iso->sitdps[frno];
  2694. std->csw &= ~(Stdioc|Stdactive);
  2695. }
  2696. for(lp=&ctlr->frames[frno]; !(*lp & Lterm); lp = &LPTR(*lp)[0])
  2697. if(LPTR(*lp) == tp)
  2698. break;
  2699. if(*lp & Lterm)
  2700. panic("cancelisoio: td not found");
  2701. *lp = tp[0];
  2702. /*
  2703. * Iso uses a virtual frame window of Nisoframes, and we must
  2704. * restore pointers in copies of the window kept at ctlr->frames.
  2705. */
  2706. if(lp == &ctlr->frames[frno]){
  2707. n = ctlr->nframes / Nisoframes;
  2708. for(w = 1; w < n; w++){
  2709. woff = w * Nisoframes;
  2710. ctlr->frames[woff+frno] = *lp;
  2711. }
  2712. }
  2713. frno = TRUNC(frno+pollival, Nisoframes);
  2714. }
  2715. iunlock(ctlr);
  2716. /*
  2717. * wakeup anyone waiting for I/O and
  2718. * wait to be sure no I/O is in progress in the controller.
  2719. * and then wait to be sure episo* is no longer running.
  2720. */
  2721. wakeup(iso);
  2722. diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
  2723. tsleep(&up->sleep, return0, 0, 5);
  2724. qlock(iso);
  2725. qunlock(iso);
  2726. diprint("cancelisoio iso %#p releasing iso\n", iso);
  2727. frno = iso->td0frno;
  2728. for(i = 0; i < iso->nframes; i++){
  2729. if(iso->hs != 0)
  2730. itdfree(iso->itdps[frno]);
  2731. else
  2732. sitdfree(iso->sitdps[frno]);
  2733. iso->tdps[frno] = nil;
  2734. frno = TRUNC(frno+pollival, Nisoframes);
  2735. }
  2736. free(iso->tdps);
  2737. iso->tdps = nil;
  2738. free(iso->data);
  2739. iso->data = nil;
  2740. }
  2741. static void
  2742. epclose(Ep *ep)
  2743. {
  2744. Qio *io;
  2745. Ctlio *cio;
  2746. Isoio *iso;
  2747. Ctlr *ctlr;
  2748. ctlr = ep->hp->aux;
  2749. deprint("ehci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
  2750. if(ep->aux == nil)
  2751. panic("ehci: epclose called with closed ep");
  2752. switch(ep->ttype){
  2753. case Tctl:
  2754. cio = ep->aux;
  2755. cancelio(ctlr, cio);
  2756. free(cio->data);
  2757. cio->data = nil;
  2758. break;
  2759. case Tintr:
  2760. case Tbulk:
  2761. io = ep->aux;
  2762. ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
  2763. if(ep->mode != OWRITE){
  2764. cancelio(ctlr, &io[OREAD]);
  2765. if(io[OREAD].toggle == Tddata1)
  2766. ep->toggle[OREAD] = 1;
  2767. }
  2768. if(ep->mode != OREAD){
  2769. cancelio(ctlr, &io[OWRITE]);
  2770. if(io[OWRITE].toggle == Tddata1)
  2771. ep->toggle[OWRITE] = 1;
  2772. }
  2773. break;
  2774. case Tiso:
  2775. iso = ep->aux;
  2776. cancelisoio(ctlr, iso, ep->pollival, ep->load);
  2777. break;
  2778. break;
  2779. default:
  2780. panic("epclose: bad ttype");
  2781. }
  2782. free(ep->aux);
  2783. ep->aux = nil;
  2784. }
  2785. static void
  2786. scanpci(void)
  2787. {
  2788. static int already = 0;
  2789. int i;
  2790. ulong io;
  2791. Ctlr *ctlr;
  2792. Pcidev *p;
  2793. Ecapio *capio;
  2794. if(already)
  2795. return;
  2796. already = 1;
  2797. p = nil;
  2798. while ((p = pcimatch(p, 0, 0)) != nil) {
  2799. /*
  2800. * Find EHCI controllers (Programming Interface = 0x20).
  2801. */
  2802. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb)
  2803. continue;
  2804. switch(p->ccrp){
  2805. case 0x20:
  2806. io = p->mem[0].bar & ~0x0f;
  2807. break;
  2808. default:
  2809. continue;
  2810. }
  2811. if(io == 0){
  2812. print("usbehci: %x %x: failed to map registers\n",
  2813. p->vid, p->did);
  2814. continue;
  2815. }
  2816. if(p->intl == 0xff || p->intl == 0) {
  2817. print("usbehci: no irq assigned for port %#lux\n", io);
  2818. continue;
  2819. }
  2820. dprint("usbehci: %#x %#x: port %#lux size %#x irq %d\n",
  2821. p->vid, p->did, io, p->mem[0].size, p->intl);
  2822. ctlr = mallocz(sizeof(Ctlr), 1);
  2823. ctlr->pcidev = p;
  2824. capio = ctlr->capio = vmap(io, p->mem[0].size);
  2825. ctlr->opio = (Eopio*)((uintptr)capio + (capio->cap & 0xff));
  2826. pcisetbme(p);
  2827. pcisetpms(p, 0);
  2828. for(i = 0; i < Nhcis; i++)
  2829. if(ctlrs[i] == nil){
  2830. ctlrs[i] = ctlr;
  2831. break;
  2832. }
  2833. if(i == Nhcis)
  2834. print("ehci: bug: no more controllers\n");
  2835. }
  2836. }
  2837. /*
  2838. * return smallest power of 2 >= n
  2839. */
  2840. static int
  2841. flog2(int n)
  2842. {
  2843. int i;
  2844. for(i = 0; (1 << i) < n; i++)
  2845. ;
  2846. return i;
  2847. }
  2848. /*
  2849. * build the periodic scheduling tree:
  2850. * framesize must be a multiple of the tree size
  2851. */
  2852. static void
  2853. mkqhtree(Ctlr *ctlr)
  2854. {
  2855. int i, n, d, o, leaf0, depth;
  2856. Qh **tree;
  2857. Qtree *qt;
  2858. Qh *qh;
  2859. ulong leafs[Nintrleafs];
  2860. depth = flog2(Nintrleafs);
  2861. n = (1 << (depth+1)) - 1;
  2862. qt = mallocz(sizeof(*qt), 1);
  2863. if(qt == nil)
  2864. panic("ehci: mkqhtree: no memory");
  2865. qt->nel = n;
  2866. qt->depth = depth;
  2867. qt->bw = mallocz(n * sizeof(qt->bw), 1);
  2868. qt->root = tree = mallocz(n * sizeof(Qh *), 1);
  2869. if(qt->bw == nil || tree == nil)
  2870. panic("ehci: mkqhtree: no memory");
  2871. for(i = 0; i < n; i++){
  2872. qh = tree[i] = edalloc();
  2873. if(qh == nil)
  2874. panic("ehci: mkqhtree: no memory");
  2875. qh->nlink = qh->alink = qh->link = Lterm;
  2876. qh->csw = Tdhalt;
  2877. qh->state = Qidle;
  2878. if(i > 0)
  2879. qhlinkqh(tree[i], tree[(i-1)/2]);
  2880. }
  2881. ctlr->ntree = i;
  2882. dprint("ehci: tree: %d endpoints allocated\n", i);
  2883. /* distribute leaves evenly round the frame list */
  2884. leaf0 = n / 2;
  2885. for(i = 0; i < Nintrleafs; i++){
  2886. o = 0;
  2887. for(d = 0; d < depth; d++){
  2888. o <<= 1;
  2889. if(i & (1 << d))
  2890. o |= 1;
  2891. }
  2892. if(leaf0 + o >= n){
  2893. print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
  2894. break;
  2895. }
  2896. leafs[i] = PADDR(tree[leaf0 + o]) | Lqh;
  2897. }
  2898. assert((ctlr->nframes % Nintrleafs) == 0);
  2899. for(i = 0; i < ctlr->nframes; i += Nintrleafs)
  2900. memmove(ctlr->frames + i, leafs, sizeof(leafs));
  2901. ctlr->tree = qt;
  2902. }
  2903. static void
  2904. ehcimeminit(Ctlr *ctlr)
  2905. {
  2906. int frsize;
  2907. Eopio *opio;
  2908. int i;
  2909. opio = ctlr->opio;
  2910. frsize = ctlr->nframes*sizeof(ulong);
  2911. assert((frsize & 0xFFF) == 0); /* must be 4k aligned */
  2912. ctlr->frames = xspanalloc(frsize, frsize, 0);
  2913. if(ctlr->frames == nil)
  2914. panic("ehci reset: no memory");
  2915. for (i = 0; i < ctlr->nframes; i++)
  2916. ctlr->frames[i] = Lterm;
  2917. opio->frbase = PADDR(ctlr->frames);
  2918. opio->frno = 0;
  2919. qhalloc(ctlr, nil, nil, nil); /* init async list */
  2920. mkqhtree(ctlr); /* init sync list */
  2921. edfree(edalloc()); /* try to get some ones pre-allocated */
  2922. dprint("ehci %#p flb %#lux frno %#lux\n",
  2923. ctlr->capio, opio->frbase, opio->frno);
  2924. }
  2925. static void
  2926. init(Hci *hp)
  2927. {
  2928. Ctlr *ctlr;
  2929. Eopio *opio;
  2930. int i;
  2931. hp->highspeed = 1;
  2932. ctlr = hp->aux;
  2933. opio = ctlr->opio;
  2934. dprint("ehci %#p init\n", ctlr->capio);
  2935. ilock(ctlr);
  2936. /*
  2937. * Unless we activate frroll interrupt
  2938. * some machines won't post other interrupts.
  2939. */
  2940. opio->intr = Iusb|Ierr|Iportchg|Ihcerr|Iasync;
  2941. opio->cmd |= Cpse;
  2942. opio->cmd |= Case;
  2943. ehcirun(ctlr, 1);
  2944. opio->config = Callmine; /* reclaim all ports */
  2945. for (i = 0; i < hp->nports; i++)
  2946. opio->portsc[i] = Pspower;
  2947. iunlock(ctlr);
  2948. if(debug > 1)
  2949. dump(hp);
  2950. }
  2951. /* Isn't this cap list search in a helper function? */
  2952. static void
  2953. getehci(Ctlr* ctlr)
  2954. {
  2955. int i, ptr, cap, sem;
  2956. ptr = ctlr->capio->capparms>>Ceecpshift & Ceecpmask;
  2957. for(; ptr != 0; ptr = pcicfgr8(ctlr->pcidev, ptr+1)){
  2958. if(ptr < 0x40 || (ptr & ~0xFC))
  2959. break;
  2960. cap = pcicfgr8(ctlr->pcidev, ptr);
  2961. if(cap != Clegacy)
  2962. continue;
  2963. sem = pcicfgr8(ctlr->pcidev, ptr+CLbiossem);
  2964. if(sem == 0)
  2965. continue;
  2966. pcicfgw8(ctlr->pcidev, ptr+CLossem, 1);
  2967. for(i = 0; i < 100; i++){
  2968. if(pcicfgr8(ctlr->pcidev, ptr+CLbiossem) == 0)
  2969. break;
  2970. delay(10);
  2971. }
  2972. if(i == 100)
  2973. dprint("ehci %#p: bios timed out\n", ctlr->capio);
  2974. pcicfgw32(ctlr->pcidev, ptr+CLcontrol, 0); /* no SMIs */
  2975. ctlr->opio->config = 0;
  2976. return;
  2977. }
  2978. }
  2979. static void
  2980. ehcireset(Ctlr *ctlr)
  2981. {
  2982. Eopio *opio;
  2983. int i;
  2984. ilock(ctlr);
  2985. dprint("ehci %#p reset\n", ctlr->capio);
  2986. opio = ctlr->opio;
  2987. /*
  2988. * Turn off legacy mode. Some controllers won't
  2989. * interrupt us as expected otherwise.
  2990. */
  2991. ehcirun(ctlr, 0);
  2992. pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
  2993. /*
  2994. * reclaim from bios
  2995. */
  2996. getehci(ctlr);
  2997. /* clear high 32 bits of address signals if it's 64 bits capable.
  2998. * This is probably not needed but it does not hurt and others do it.
  2999. */
  3000. if((ctlr->capio->capparms & C64) != 0){
  3001. dprint("ehci: 64 bits\n");
  3002. opio->seg = 0;
  3003. }
  3004. if(ehcidebugcapio != ctlr->capio){
  3005. opio->cmd |= Chcreset; /* controller reset */
  3006. for(i = 0; i < 100; i++){
  3007. if((opio->cmd & Chcreset) == 0)
  3008. break;
  3009. delay(1);
  3010. }
  3011. if(i == 100)
  3012. print("ehci %#p controller reset timed out\n", ctlr->capio);
  3013. }
  3014. /* requesting more interrupts per µframe may miss interrupts */
  3015. opio->cmd |= Citc8; /* 1 intr. per ms */
  3016. switch(opio->cmd & Cflsmask){
  3017. case Cfls1024:
  3018. ctlr->nframes = 1024;
  3019. break;
  3020. case Cfls512:
  3021. ctlr->nframes = 512;
  3022. break;
  3023. case Cfls256:
  3024. ctlr->nframes = 256;
  3025. break;
  3026. default:
  3027. panic("ehci: unknown fls %ld", opio->cmd & Cflsmask);
  3028. }
  3029. dprint("ehci: %d frames\n", ctlr->nframes);
  3030. iunlock(ctlr);
  3031. }
  3032. static void
  3033. setdebug(Hci*, int d)
  3034. {
  3035. debug = d;
  3036. }
  3037. static void
  3038. shutdown(Hci *hp)
  3039. {
  3040. int i;
  3041. Ctlr *ctlr;
  3042. Eopio *opio;
  3043. ctlr = hp->aux;
  3044. ilock(ctlr);
  3045. opio = ctlr->opio;
  3046. opio->cmd |= Chcreset; /* controller reset */
  3047. for(i = 0; i < 100; i++){
  3048. if((opio->cmd & Chcreset) == 0)
  3049. break;
  3050. delay(1);
  3051. }
  3052. if(i >= 100)
  3053. print("ehci %#p controller reset timed out\n", ctlr->capio);
  3054. delay(100);
  3055. ehcirun(ctlr, 0);
  3056. opio->frbase = 0;
  3057. iunlock(ctlr);
  3058. }
  3059. static int
  3060. reset(Hci *hp)
  3061. {
  3062. int i;
  3063. Ctlr *ctlr;
  3064. Ecapio *capio;
  3065. Pcidev *p;
  3066. static Lock resetlck;
  3067. if(getconf("*nousbehci"))
  3068. return -1;
  3069. ilock(&resetlck);
  3070. scanpci();
  3071. /*
  3072. * Any adapter matches if no hp->port is supplied,
  3073. * otherwise the ports must match.
  3074. */
  3075. ctlr = nil;
  3076. for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
  3077. ctlr = ctlrs[i];
  3078. if(ctlr->active == 0)
  3079. if(hp->port == 0 || hp->port == (uintptr)ctlr->capio){
  3080. ctlr->active = 1;
  3081. break;
  3082. }
  3083. }
  3084. iunlock(&resetlck);
  3085. if(ctlrs[i] == nil || i == Nhcis)
  3086. return -1;
  3087. p = ctlr->pcidev;
  3088. hp->aux = ctlr;
  3089. hp->port = (uintptr)ctlr->capio;
  3090. hp->irq = p->intl;
  3091. hp->tbdf = p->tbdf;
  3092. capio = ctlr->capio;
  3093. hp->nports = capio->parms & Cnports;
  3094. ddprint("echi: %s, ncc %lud npcc %lud\n",
  3095. capio->parms & 0x10000 ? "leds" : "no leds",
  3096. (capio->parms >> 12) & 0xf, (capio->parms >> 8) & 0xf);
  3097. ddprint("ehci: routing %s, %sport power ctl, %d ports\n",
  3098. capio->parms & 0x40 ? "explicit" : "automatic",
  3099. capio->parms & 0x10 ? "" : "no ", hp->nports);
  3100. ehcireset(ctlr);
  3101. ehcimeminit(ctlr);
  3102. /*
  3103. * Linkage to the generic HCI driver.
  3104. */
  3105. hp->init = init;
  3106. hp->dump = dump;
  3107. hp->interrupt = interrupt;
  3108. hp->epopen = epopen;
  3109. hp->epclose = epclose;
  3110. hp->epread = epread;
  3111. hp->epwrite = epwrite;
  3112. hp->seprintep = seprintep;
  3113. hp->portenable = portenable;
  3114. hp->portreset = portreset;
  3115. hp->portstatus = portstatus;
  3116. hp->shutdown = shutdown;
  3117. hp->debug = setdebug;
  3118. hp->type = "ehci";
  3119. return 0;
  3120. }
  3121. void
  3122. usbehcilink(void)
  3123. {
  3124. addhcitype("ehci", reset);
  3125. }