usbehci.c 72 KB

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