usbehci.c 72 KB

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