usbehci.c 69 KB

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