usbuhci.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  1. /*
  2. * USB Universal Host Controller Interface (sic) driver.
  3. *
  4. * BUGS:
  5. * - Too many delays and ilocks.
  6. * - bandwidth admission control must be done per-frame.
  7. * - interrupt endpoints should go on a tree like [oe]hci.
  8. * - must warn of power overruns.
  9. */
  10. #include "u.h"
  11. #include "../port/lib.h"
  12. #include "mem.h"
  13. #include "dat.h"
  14. #include "fns.h"
  15. #include "io.h"
  16. #include "../port/error.h"
  17. #include "../port/usb.h"
  18. typedef struct Ctlio Ctlio;
  19. typedef struct Ctlr Ctlr;
  20. typedef struct Isoio Isoio;
  21. typedef struct Qh Qh;
  22. typedef struct Qhpool Qhpool;
  23. typedef struct Qio Qio;
  24. typedef struct Td Td;
  25. typedef struct Tdpool Tdpool;
  26. enum
  27. {
  28. Resetdelay = 100, /* delay after a controller reset (ms) */
  29. Enabledelay = 100, /* waiting for a port to enable */
  30. Abortdelay = 5, /* delay after cancelling Tds (ms) */
  31. Incr = 64, /* for Td and Qh pools */
  32. Tdatomic = 8, /* max nb. of Tds per bulk I/O op. */
  33. /* Queue states (software) */
  34. Qidle = 0,
  35. Qinstall,
  36. Qrun,
  37. Qdone,
  38. Qclose,
  39. Qfree,
  40. /*
  41. * HW constants
  42. */
  43. Nframes = 1024, /* 2ⁿ for xspanalloc; max 1024 */
  44. Align = 16, /* for data structures */
  45. /* Size of small buffer kept within Tds. (software) */
  46. /* Keep as a multiple of Align to maintain alignment of Tds in pool */
  47. Tdndata = 1*Align,
  48. /* i/o space
  49. * Some ports are short, some are long, some are byte.
  50. * We use ins[bsl] and not vmap.
  51. */
  52. Cmd = 0,
  53. Crun = 0x01,
  54. Chcreset = 0x02, /* host controller reset */
  55. Cgreset = 0x04, /* global reset */
  56. Cegsm = 0x08, /* enter global suspend */
  57. Cfgr = 0x10, /* forge global resume */
  58. Cdbg = 0x20, /* single step, debug */
  59. Cmaxp = 0x80, /* max packet */
  60. Status = 2,
  61. Susbintr = 0x01, /* interrupt */
  62. Seintr = 0x02, /* error interrupt */
  63. Sresume = 0x04, /* resume detect */
  64. Shserr = 0x08, /* host system error */
  65. Shcerr = 0x10, /* host controller error */
  66. Shalted = 0x20, /* controller halted */
  67. Sall = 0x3F,
  68. Usbintr = 4,
  69. Itmout = 0x01, /* timeout or crc */
  70. Iresume = 0x02, /* resume interrupt enable */
  71. Ioc = 0x04, /* interrupt on complete */
  72. Ishort = 0x08, /* short packet interrupt */
  73. Iall = 0x0F,
  74. Frnum = 6,
  75. Flbaseadd = 8,
  76. SOFmod = 0xC, /* start of frame modifier register */
  77. Portsc0 = 0x10,
  78. PSpresent = 0x0001, /* device present */
  79. PSstatuschg = 0x0002, /* PSpresent changed */
  80. PSenable = 0x0004, /* device enabled */
  81. PSchange = 0x0008, /* PSenable changed */
  82. PSresume = 0x0040, /* resume detected */
  83. PSreserved1 = 0x0080, /* always read as 1; reserved */
  84. PSslow = 0x0100, /* device has low speed */
  85. PSreset = 0x0200, /* port reset */
  86. PSsuspend = 0x1000, /* port suspended */
  87. /* Transfer descriptor link */
  88. Tdterm = 0x1, /* nil (terminate) */
  89. Tdlinkqh = 0x2, /* link refers to a QH */
  90. Tdvf = 0x4, /* run linked Tds first (depth-first)*/
  91. /* Transfer status bits */
  92. Tdbitstuff = 0x00020000, /* bit stuffing error */
  93. Tdcrcto = 0x00040000, /* crc or timeout error */
  94. Tdnak = 0x00080000, /* nak packet received */
  95. Tdbabble = 0x00100000, /* babble detected */
  96. Tddberr = 0x00200000, /* data buf. error */
  97. Tdstalled = 0x00400000, /* serious error to ep. */
  98. Tdactive = 0x00800000, /* enabled/in use by hw */
  99. /* Transfer control bits */
  100. Tdioc = 0x01000000, /* interrupt on complete */
  101. Tdiso = 0x02000000, /* isochronous select */
  102. Tdlow = 0x04000000, /* low speed device */
  103. Tderr1 = 0x08000000, /* bit 0 of error counter */
  104. Tderr2 = 0x10000000, /* bit 1 of error counter */
  105. Tdspd = 0x20000000, /* short packet detect */
  106. Tdlen = 0x000003FF, /* actual length field */
  107. Tdfatalerr = Tdnak|Tdbabble|Tdstalled, /* hw retries others */
  108. Tderrors = Tdfatalerr|Tdbitstuff|Tdcrcto|Tddberr,
  109. /* Transfer descriptor token bits */
  110. Tddata0 = 0,
  111. Tddata1 = 0x80000, /* data toggle (1==DATA1) */
  112. Tdtokin = 0x69,
  113. Tdtokout = 0xE1,
  114. Tdtoksetup = 0x2D,
  115. Tdmaxpkt = 0x800, /* max packet size */
  116. /* Queue head bits */
  117. QHterm = 1<<0, /* nil (terminate) */
  118. QHlinkqh = 1<<1, /* link refers to a QH */
  119. QHvf = 1<<2, /* vertical first (depth first) */
  120. };
  121. struct Ctlr
  122. {
  123. Lock; /* for ilock. qh lists and basic ctlr I/O */
  124. QLock portlck; /* for port resets/enable... */
  125. Pcidev* pcidev;
  126. int active;
  127. int port; /* I/O address */
  128. Qh* qhs; /* list of Qhs for this controller */
  129. Qh* qh[Tmax]; /* Dummy Qhs to insert Qhs after */
  130. Isoio* iso; /* list of active iso I/O */
  131. ulong* frames; /* frame list (used by hw) */
  132. ulong load; /* max load for a single frame */
  133. ulong isoload; /* max iso load for a single frame */
  134. int nintr; /* number of interrupts attended */
  135. int ntdintr; /* number of intrs. with something to do */
  136. int nqhintr; /* number of intrs. for Qhs */
  137. int nisointr; /* number of intrs. for iso transfers */
  138. };
  139. struct Qio
  140. {
  141. QLock; /* for the entire I/O process */
  142. Rendez; /* wait for completion */
  143. Qh* qh; /* Td list (field const after init) */
  144. int usbid; /* usb address for endpoint/device */
  145. int toggle; /* Tddata0/Tddata1 */
  146. int tok; /* Tdtoksetup, Tdtokin, Tdtokout */
  147. ulong iotime; /* time of last I/O */
  148. int debug; /* debug flag from the endpoint */
  149. char* err; /* error string */
  150. };
  151. struct Ctlio
  152. {
  153. Qio; /* a single Qio for each RPC */
  154. uchar* data; /* read from last ctl req. */
  155. int ndata; /* number of bytes read */
  156. };
  157. struct Isoio
  158. {
  159. QLock;
  160. Rendez; /* wait for space/completion/errors */
  161. int usbid; /* address used for device/endpoint */
  162. int tok; /* Tdtokin or Tdtokout */
  163. int state; /* Qrun -> Qdone -> Qrun... -> Qclose */
  164. int nframes; /* Nframes/ep->pollival */
  165. uchar* data; /* iso data buffers if not embedded */
  166. int td0frno; /* frame number for first Td */
  167. Td* tdu; /* next td for user I/O in tdps */
  168. Td* tdi; /* next td processed by interrupt */
  169. char* err; /* error string */
  170. int nerrs; /* nb of consecutive I/O errors */
  171. long nleft; /* number of bytes left from last write */
  172. int debug; /* debug flag from the endpoint */
  173. Isoio* next; /* in list of active Isoios */
  174. Td* tdps[Nframes]; /* pointer to Td used for i-th frame or nil */
  175. };
  176. struct Tdpool
  177. {
  178. Lock;
  179. Td* free;
  180. int nalloc;
  181. int ninuse;
  182. int nfree;
  183. };
  184. struct Qhpool
  185. {
  186. Lock;
  187. Qh* free;
  188. int nalloc;
  189. int ninuse;
  190. int nfree;
  191. };
  192. /*
  193. * HW data structures
  194. */
  195. /*
  196. * Queue header (known by hw).
  197. * 16-byte aligned. first two words used by hw.
  198. * They are taken from the pool upon endpoint opening and
  199. * queued after the dummy queue header for the endpoint type
  200. * in the controller. Actual I/O happens as Tds are linked into it.
  201. * The driver does I/O in lock-step.
  202. * The user builds a list of Tds and links it into the Qh,
  203. * then the Qh goes from Qidle to Qrun and nobody touches it until
  204. * it becomes Qdone at interrupt time.
  205. * At that point the user collects the Tds and it goes Qidle.
  206. * A premature cancel may set the state to Qclose and abort I/O.
  207. * The Ctlr lock protects change of state for Qhs in use.
  208. */
  209. struct Qh
  210. {
  211. ulong link; /* link to next horiz. item (eg. Qh) */
  212. ulong elink; /* link to element (eg. Td; updated by hw) */
  213. ulong state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
  214. Qio* io; /* for this queue */
  215. Qh* next; /* in active or free list */
  216. Td* tds; /* Td list in this Qh (initially, elink) */
  217. char* tag; /* debug and align, mostly */
  218. ulong align;
  219. };
  220. /*
  221. * Transfer descriptor.
  222. * 16-byte aligned. first two words used by hw. Next 4 by sw.
  223. * We keep an embedded buffer for small I/O transfers.
  224. * They are taken from the pool when buffers are needed for I/O
  225. * and linked at the Qh/Isoio for the endpoint and direction requiring it.
  226. * The block keeps actual data. They are protected from races by
  227. * the queue or the pool keeping it. The owner of the link to the Td
  228. * is free to use it and can be the only one using it.
  229. */
  230. struct Td
  231. {
  232. ulong link; /* Link to next Td or Qh */
  233. ulong csw; /* control and status word (updated by hw) */
  234. ulong token; /* endpt, device, pid */
  235. ulong buffer; /* buffer pointer */
  236. Td* next; /* in qh or Isoio or free list */
  237. ulong ndata; /* bytes available/used at data */
  238. uchar* data; /* pointer to actual data */
  239. void* buff; /* allocated data, for large transfers */
  240. uchar sbuff[Tdndata]; /* embedded buffer, for small transfers */
  241. };
  242. #define INB(x) inb(ctlr->port+(x))
  243. #define INS(x) ins(ctlr->port+(x))
  244. #define INL(x) inl(ctlr->port+(x))
  245. #define OUTB(x, v) outb(ctlr->port+(x), (v))
  246. #define OUTS(x, v) outs(ctlr->port+(x), (v))
  247. #define OUTL(x, v) outl(ctlr->port+(x), (v))
  248. #define TRUNC(x, sz) ((x) & ((sz)-1))
  249. #define PTR(q) ((void*)KADDR((ulong)(q) & ~ (0xF|PCIWINDOW)))
  250. #define QPTR(q) ((Qh*)PTR(q))
  251. #define TPTR(q) ((Td*)PTR(q))
  252. #define PORT(p) (Portsc0 + 2*(p))
  253. #define diprint if(debug || iso->debug)print
  254. #define ddiprint if(debug>1 || iso->debug>1)print
  255. #define dqprint if(debug || (qh->io && qh->io->debug))print
  256. #define ddqprint if(debug>1 || (qh->io && qh->io->debug>1))print
  257. static Ctlr* ctlrs[Nhcis];
  258. static Tdpool tdpool;
  259. static Qhpool qhpool;
  260. static int debug;
  261. static char* qhsname[] = { "idle", "install", "run", "done", "close", "FREE" };
  262. static void
  263. uhcicmd(Ctlr *ctlr, int c)
  264. {
  265. OUTS(Cmd, c);
  266. }
  267. static void
  268. uhcirun(Ctlr *ctlr, int on)
  269. {
  270. int i;
  271. ddprint("uhci %#ux setting run to %d\n", ctlr->port, on);
  272. if(on)
  273. uhcicmd(ctlr, INS(Cmd)|Crun);
  274. else
  275. uhcicmd(ctlr, INS(Cmd) & ~Crun);
  276. for(i = 0; i < 100; i++)
  277. if(on == 0 && (INS(Status) & Shalted) != 0)
  278. break;
  279. else if(on != 0 && (INS(Status) & Shalted) == 0)
  280. break;
  281. else
  282. delay(1);
  283. if(i == 100)
  284. dprint("uhci %#x run cmd timed out\n", ctlr->port);
  285. ddprint("uhci %#ux cmd %#ux sts %#ux\n",
  286. ctlr->port, INS(Cmd), INS(Status));
  287. }
  288. static int
  289. tdlen(Td *td)
  290. {
  291. return (td->csw+1) & Tdlen;
  292. }
  293. static int
  294. maxtdlen(Td *td)
  295. {
  296. return ((td->token>>21)+1) & (Tdmaxpkt-1);
  297. }
  298. static int
  299. tdtok(Td *td)
  300. {
  301. return td->token & 0xFF;
  302. }
  303. static char*
  304. seprinttd(char *s, char *se, Td *td)
  305. {
  306. s = seprint(s, se, "%#p link %#ulx", td, td->link);
  307. if((td->link & Tdvf) != 0)
  308. s = seprint(s, se, "V");
  309. if((td->link & Tdterm) != 0)
  310. s = seprint(s, se, "T");
  311. if((td->link & Tdlinkqh) != 0)
  312. s = seprint(s, se, "Q");
  313. s = seprint(s, se, " csw %#ulx ", td->csw);
  314. if(td->csw & Tdactive)
  315. s = seprint(s, se, "a");
  316. if(td->csw & Tdiso)
  317. s = seprint(s, se, "I");
  318. if(td->csw & Tdioc)
  319. s = seprint(s, se, "i");
  320. if(td->csw & Tdlow)
  321. s = seprint(s, se, "l");
  322. if((td->csw & (Tderr1|Tderr2)) == 0)
  323. s = seprint(s, se, "z");
  324. if(td->csw & Tderrors)
  325. s = seprint(s, se, " err %#ulx", td->csw & Tderrors);
  326. if(td->csw & Tdstalled)
  327. s = seprint(s, se, "s");
  328. if(td->csw & Tddberr)
  329. s = seprint(s, se, "d");
  330. if(td->csw & Tdbabble)
  331. s = seprint(s, se, "b");
  332. if(td->csw & Tdnak)
  333. s = seprint(s, se, "n");
  334. if(td->csw & Tdcrcto)
  335. s = seprint(s, se, "c");
  336. if(td->csw & Tdbitstuff)
  337. s = seprint(s, se, "B");
  338. s = seprint(s, se, " stslen %d", tdlen(td));
  339. s = seprint(s, se, " token %#ulx", td->token);
  340. if(td->token == 0) /* the BWS loopback Td, ignore rest */
  341. return s;
  342. s = seprint(s, se, " maxlen %d", maxtdlen(td));
  343. if(td->token & Tddata1)
  344. s = seprint(s, se, " d1");
  345. else
  346. s = seprint(s, se, " d0");
  347. s = seprint(s, se, " id %#ulx:", (td->token>>15) & Epmax);
  348. s = seprint(s, se, "%#ulx", (td->token>>8) & Devmax);
  349. switch(tdtok(td)){
  350. case Tdtokin:
  351. s = seprint(s, se, " in");
  352. break;
  353. case Tdtokout:
  354. s = seprint(s, se, " out");
  355. break;
  356. case Tdtoksetup:
  357. s = seprint(s, se, " setup");
  358. break;
  359. default:
  360. s = seprint(s, se, " BADPID");
  361. }
  362. s = seprint(s, se, "\n\t buffer %#ulx data %#p", td->buffer, td->data);
  363. s = seprint(s, se, " ndata %uld sbuff %#p buff %#p",
  364. td->ndata, td->sbuff, td->buff);
  365. if(td->ndata > 0)
  366. s = seprintdata(s, se, td->data, td->ndata);
  367. return s;
  368. }
  369. static void
  370. isodump(Isoio *iso, int all)
  371. {
  372. char buf[256];
  373. Td *td;
  374. int i;
  375. print("iso %#p %s state %d nframes %d"
  376. " td0 %#p tdu %#p tdi %#p data %#p\n",
  377. iso, iso->tok == Tdtokin ? "in" : "out",
  378. iso->state, iso->nframes, iso->tdps[iso->td0frno],
  379. iso->tdu, iso->tdi, iso->data);
  380. if(iso->err != nil)
  381. print("\terr='%s'\n", iso->err);
  382. if(all == 0){
  383. seprinttd(buf, buf+sizeof(buf), iso->tdu);
  384. print("\ttdu %s\n", buf);
  385. seprinttd(buf, buf+sizeof(buf), iso->tdi);
  386. print("\ttdi %s\n", buf);
  387. }else{
  388. td = iso->tdps[iso->td0frno];
  389. for(i = 0; i < iso->nframes; i++){
  390. seprinttd(buf, buf+sizeof(buf), td);
  391. if(td == iso->tdi)
  392. print("i->");
  393. if(td == iso->tdu)
  394. print("u->");
  395. print("\t%s\n", buf);
  396. td = td->next;
  397. }
  398. }
  399. }
  400. static int
  401. sameptr(void *p, ulong l)
  402. {
  403. if(l & QHterm)
  404. return p == nil;
  405. return PTR(l) == p;
  406. }
  407. static void
  408. dumptd(Td *td, char *pref)
  409. {
  410. char buf[256];
  411. char *s;
  412. char *se;
  413. int i;
  414. i = 0;
  415. se = buf+sizeof(buf);
  416. for(; td != nil; td = td->next){
  417. s = seprinttd(buf, se, td);
  418. if(!sameptr(td->next, td->link))
  419. seprint(s, se, " next %#p != link %#ulx %#p",
  420. td->next, td->link, TPTR(td->link));
  421. print("%std %s\n", pref, buf);
  422. if(i++ > 20){
  423. print("...more tds...\n");
  424. break;
  425. }
  426. }
  427. }
  428. static void
  429. qhdump(Qh *qh, char *pref)
  430. {
  431. char buf[256];
  432. char *s;
  433. char *se;
  434. ulong td;
  435. int i;
  436. s = buf;
  437. se = buf+sizeof(buf);
  438. s = seprint(s, se, "%sqh %s %#p state %s link %#ulx", pref,
  439. qh->tag, qh, qhsname[qh->state], qh->link);
  440. if(!sameptr(qh->tds, qh->elink))
  441. s = seprint(s, se, " [tds %#p != elink %#ulx %#p]",
  442. qh->tds, qh->elink, TPTR(qh->elink));
  443. if(!sameptr(qh->next, qh->link))
  444. s = seprint(s, se, " [next %#p != link %#ulx %#p]",
  445. qh->next, qh->link, QPTR(qh->link));
  446. if((qh->link & Tdterm) != 0)
  447. s = seprint(s, se, "T");
  448. if((qh->link & Tdlinkqh) != 0)
  449. s = seprint(s, se, "Q");
  450. s = seprint(s, se, " elink %#ulx", qh->elink);
  451. if((qh->elink & Tdterm) != 0)
  452. s = seprint(s, se, "T");
  453. if((qh->elink & Tdlinkqh) != 0)
  454. s = seprint(s, se, "Q");
  455. s = seprint(s, se, " io %#p", qh->io);
  456. if(qh->io != nil && qh->io->err != nil)
  457. seprint(s, se, " err='%s'", qh->io->err);
  458. print("%s\n", buf);
  459. dumptd(qh->tds, "\t");
  460. if((qh->elink & QHterm) == 0){
  461. print("\thw tds:");
  462. i = 0;
  463. for(td = qh->elink; (td & Tdterm) == 0; td = TPTR(td)->link){
  464. print(" %#ulx", td);
  465. if(td == TPTR(td)->link) /* BWS Td */
  466. break;
  467. if(i++ > 40){
  468. print("...");
  469. break;
  470. }
  471. }
  472. print("\n");
  473. }
  474. }
  475. static void
  476. xdump(Ctlr *ctlr, int doilock)
  477. {
  478. Isoio *iso;
  479. Qh *qh;
  480. int i;
  481. if(doilock){
  482. if(ctlr == ctlrs[0]){
  483. lock(&tdpool);
  484. print("tds: alloc %d = inuse %d + free %d\n",
  485. tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
  486. unlock(&tdpool);
  487. lock(&qhpool);
  488. print("qhs: alloc %d = inuse %d + free %d\n",
  489. qhpool.nalloc, qhpool.ninuse, qhpool.nfree);
  490. unlock(&qhpool);
  491. }
  492. ilock(ctlr);
  493. }
  494. print("uhci port %#x frames %#p nintr %d ntdintr %d",
  495. ctlr->port, ctlr->frames, ctlr->nintr, ctlr->ntdintr);
  496. print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
  497. print("cmd %#ux sts %#ux fl %#ulx ps1 %#ux ps2 %#ux frames[0] %#ulx\n",
  498. INS(Cmd), INS(Status),
  499. INL(Flbaseadd), INS(PORT(0)), INS(PORT(1)),
  500. ctlr->frames[0]);
  501. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  502. isodump(iso, 1);
  503. i = 0;
  504. for(qh = ctlr->qhs; qh != nil; qh = qh->next){
  505. qhdump(qh, "");
  506. if(i++ > 20){
  507. print("qhloop\n");
  508. break;
  509. }
  510. }
  511. print("\n");
  512. if(doilock)
  513. iunlock(ctlr);
  514. }
  515. static void
  516. dump(Hci *hp)
  517. {
  518. xdump(hp->aux, 1);
  519. }
  520. static Td*
  521. tdalloc(void)
  522. {
  523. int i;
  524. Td *td;
  525. Td *pool;
  526. lock(&tdpool);
  527. if(tdpool.free == nil){
  528. ddprint("uhci: tdalloc %d Tds\n", Incr);
  529. pool = xspanalloc(Incr*sizeof(Td), Align, 0);
  530. if(pool == nil)
  531. panic("tdalloc");
  532. for(i=Incr; --i>=0;){
  533. pool[i].next = tdpool.free;
  534. tdpool.free = &pool[i];
  535. }
  536. tdpool.nalloc += Incr;
  537. tdpool.nfree += Incr;
  538. }
  539. td = tdpool.free;
  540. tdpool.free = td->next;
  541. tdpool.ninuse++;
  542. tdpool.nfree--;
  543. unlock(&tdpool);
  544. memset(td, 0, sizeof(Td));
  545. td->link = Tdterm;
  546. assert(((ulong)td & 0xF) == 0);
  547. return td;
  548. }
  549. static void
  550. tdfree(Td *td)
  551. {
  552. if(td == nil)
  553. return;
  554. free(td->buff);
  555. td->buff = nil;
  556. lock(&tdpool);
  557. td->next = tdpool.free;
  558. tdpool.free = td;
  559. tdpool.ninuse--;
  560. tdpool.nfree++;
  561. unlock(&tdpool);
  562. }
  563. static void
  564. qhlinkqh(Qh* qh, Qh* next)
  565. {
  566. if(next == nil)
  567. qh->link = QHterm;
  568. else{
  569. next->link = qh->link;
  570. next->next = qh->next;
  571. qh->link = PCIWADDR(next)|QHlinkqh;
  572. }
  573. qh->next = next;
  574. }
  575. static void
  576. qhlinktd(Qh *qh, Td *td)
  577. {
  578. qh->tds = td;
  579. if(td == nil)
  580. qh->elink = QHvf|QHterm;
  581. else
  582. qh->elink = PCIWADDR(td);
  583. }
  584. static void
  585. tdlinktd(Td *td, Td *next)
  586. {
  587. td->next = next;
  588. if(next == nil)
  589. td->link = Tdterm;
  590. else
  591. td->link = PCIWADDR(next)|Tdvf;
  592. }
  593. static Qh*
  594. qhalloc(Ctlr *ctlr, Qh *prev, Qio *io, char *tag)
  595. {
  596. int i;
  597. Qh *qh;
  598. Qh *pool;
  599. lock(&qhpool);
  600. if(qhpool.free == nil){
  601. ddprint("uhci: qhalloc %d Qhs\n", Incr);
  602. pool = xspanalloc(Incr*sizeof(Qh), Align, 0);
  603. if(pool == nil)
  604. panic("qhalloc");
  605. for(i=Incr; --i>=0;){
  606. pool[i].next = qhpool.free;
  607. qhpool.free = &pool[i];
  608. }
  609. qhpool.nalloc += Incr;
  610. qhpool.nfree += Incr;
  611. }
  612. qh = qhpool.free;
  613. qhpool.free = qh->next;
  614. qh->next = nil;
  615. qh->link = QHterm;
  616. qhpool.ninuse++;
  617. qhpool.nfree--;
  618. unlock(&qhpool);
  619. qh->tds = nil;
  620. qh->elink = QHterm;
  621. qh->state = Qidle;
  622. qh->io = io;
  623. qh->tag = nil;
  624. kstrdup(&qh->tag, tag);
  625. if(prev != nil){
  626. coherence();
  627. ilock(ctlr);
  628. qhlinkqh(prev, qh);
  629. iunlock(ctlr);
  630. }
  631. assert(((ulong)qh & 0xF) == 0);
  632. return qh;
  633. }
  634. static void
  635. qhfree(Ctlr *ctlr, Qh *qh)
  636. {
  637. Td *td;
  638. Td *ltd;
  639. Qh *q;
  640. if(qh == nil)
  641. return;
  642. ilock(ctlr);
  643. for(q = ctlr->qhs; q != nil; q = q->next)
  644. if(q->next == qh)
  645. break;
  646. if(q == nil)
  647. panic("qhfree: nil q");
  648. q->next = qh->next;
  649. q->link = qh->link;
  650. iunlock(ctlr);
  651. for(td = qh->tds; td != nil; td = ltd){
  652. ltd = td->next;
  653. tdfree(td);
  654. }
  655. lock(&qhpool);
  656. qh->state = Qfree; /* paranoia */
  657. qh->next = qhpool.free;
  658. qh->tag = nil;
  659. qh->io = nil;
  660. qhpool.free = qh;
  661. qhpool.ninuse--;
  662. qhpool.nfree++;
  663. unlock(&qhpool);
  664. ddprint("qhfree: qh %#p\n", qh);
  665. }
  666. static char*
  667. errmsg(int err)
  668. {
  669. if(err == 0)
  670. return "ok";
  671. if(err & Tdcrcto)
  672. return "crc/timeout error";
  673. if(err & Tdbabble)
  674. return "babble detected";
  675. if(err & Tddberr)
  676. return "db error";
  677. if(err & Tdbitstuff)
  678. return "bit stuffing error";
  679. if(err & Tdstalled)
  680. return Estalled;
  681. return Eio;
  682. }
  683. static int
  684. isocanread(void *a)
  685. {
  686. Isoio *iso;
  687. iso = a;
  688. return iso->state == Qclose ||
  689. (iso->state == Qrun &&
  690. iso->tok == Tdtokin && iso->tdi != iso->tdu);
  691. }
  692. static int
  693. isocanwrite(void *a)
  694. {
  695. Isoio *iso;
  696. iso = a;
  697. return iso->state == Qclose ||
  698. (iso->state == Qrun &&
  699. iso->tok == Tdtokout && iso->tdu->next != iso->tdi);
  700. }
  701. static void
  702. tdisoinit(Isoio *iso, Td *td, long count)
  703. {
  704. td->ndata = count;
  705. td->token = ((count-1)<<21)| ((iso->usbid & 0x7FF)<<8) | iso->tok;
  706. td->csw = Tderr1|Tdiso|Tdactive|Tdioc;
  707. }
  708. /*
  709. * Process Iso i/o on interrupt. For writes update just error status.
  710. * For reads update tds to reflect data and also error status.
  711. * When tdi aproaches tdu, advance tdu; data may be lost.
  712. * (If nframes is << Nframes tdu might be far away but this avoids
  713. * races regarding frno.)
  714. * If we suffer errors for more than half the frames we stall.
  715. */
  716. static void
  717. isointerrupt(Ctlr *ctlr, Isoio* iso)
  718. {
  719. Td *tdi;
  720. int err;
  721. int i;
  722. int nframes;
  723. tdi = iso->tdi;
  724. if((tdi->csw & Tdactive) != 0) /* nothing new done */
  725. return;
  726. ctlr->nisointr++;
  727. ddiprint("isointr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
  728. if(iso->state != Qrun && iso->state != Qdone)
  729. panic("isointr: iso state");
  730. if(debug > 1 || iso->debug > 1)
  731. isodump(iso, 0);
  732. nframes = iso->nframes / 2; /* limit how many we look */
  733. if(nframes > 64)
  734. nframes = 64;
  735. for(i = 0; i < nframes && (tdi->csw & Tdactive) == 0; i++){
  736. tdi->csw &= ~Tdioc;
  737. err = tdi->csw & Tderrors;
  738. if(err == 0)
  739. iso->nerrs = 0;
  740. else if(iso->nerrs++ > iso->nframes/2)
  741. tdi->csw |= Tdstalled;
  742. if((tdi->csw & Tdstalled) != 0){
  743. if(iso->err == nil){
  744. iso->err = errmsg(err);
  745. diprint("isointerrupt: tdi %#p error %#ux %s\n",
  746. tdi, err, iso->err);
  747. diprint("ctlr load %uld\n", ctlr->load);
  748. }
  749. tdi->ndata = 0;
  750. }else
  751. tdi->ndata = tdlen(tdi);
  752. if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
  753. memset(iso->tdu->data, 0, maxtdlen(iso->tdu));
  754. tdisoinit(iso, iso->tdu, maxtdlen(iso->tdu));
  755. iso->tdu = iso->tdu->next;
  756. iso->nleft = 0;
  757. }
  758. tdi = tdi->next;
  759. }
  760. ddiprint("isointr: %d frames processed\n", nframes);
  761. if(i == nframes)
  762. tdi->csw |= Tdioc;
  763. iso->tdi = tdi;
  764. if(isocanwrite(iso) || isocanread(iso)){
  765. diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
  766. iso->tdi, iso->tdu);
  767. wakeup(iso);
  768. }
  769. }
  770. /*
  771. * Process a Qh upon interrupt. There's one per ongoing user I/O.
  772. * User process releases resources later, that is not done here.
  773. * We may find in this order one or more Tds:
  774. * - none/many non active and completed Tds
  775. * - none/one (usually(!) not active) and failed Td
  776. * - none/many active Tds.
  777. * Upon errors the entire transfer is aborted and error reported.
  778. * Otherwise, the transfer is complete only when all Tds are done or
  779. * when a read with less than maxpkt is found.
  780. * Use the software list and not qh->elink to avoid races.
  781. * We could use qh->elink to see if there's something new or not.
  782. */
  783. static void
  784. qhinterrupt(Ctlr *ctlr, Qh *qh)
  785. {
  786. Td *td;
  787. int err;
  788. ctlr->nqhintr++;
  789. if(qh->state != Qrun)
  790. panic("qhinterrupt: qh state");
  791. if(qh->tds == nil)
  792. panic("qhinterrupt: no tds");
  793. if((qh->tds->csw & Tdactive) == 0)
  794. ddqprint("qhinterrupt port %#ux qh %#p p0 %#x p1 %#x\n",
  795. ctlr->port, qh, INS(PORT(0)), INS(PORT(1)));
  796. for(td = qh->tds; td != nil; td = td->next){
  797. if(td->csw & Tdactive)
  798. return;
  799. td->csw &= ~Tdioc;
  800. if((td->csw & Tdstalled) != 0){
  801. err = td->csw & Tderrors;
  802. /* just stalled is end of xfer but not an error */
  803. if(err != Tdstalled && qh->io->err == nil){
  804. qh->io->err = errmsg(td->csw & Tderrors);
  805. dqprint("qhinterrupt: td %#p error %#ux %s\n",
  806. td, err, qh->io->err);
  807. dqprint("ctlr load %uld\n", ctlr->load);
  808. }
  809. break;
  810. }
  811. if((td->csw & Tdnak) != 0){ /* retransmit; not serious */
  812. td->csw &= ~Tdnak;
  813. if(td->next == nil)
  814. td->csw |= Tdioc;
  815. }
  816. td->ndata = tdlen(td);
  817. if(td->ndata < maxtdlen(td)){ /* EOT */
  818. td = td->next;
  819. break;
  820. }
  821. }
  822. /*
  823. * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
  824. */
  825. qh->elink = QHterm;
  826. for(; td != nil; td = td->next)
  827. td->ndata = 0;
  828. qh->state = Qdone;
  829. wakeup(qh->io);
  830. }
  831. static void
  832. interrupt(Ureg*, void *a)
  833. {
  834. Hci *hp;
  835. Ctlr *ctlr;
  836. int frptr;
  837. int frno;
  838. Qh *qh;
  839. Isoio *iso;
  840. int sts;
  841. int cmd;
  842. hp = a;
  843. ctlr = hp->aux;
  844. ilock(ctlr);
  845. ctlr->nintr++;
  846. sts = INS(Status);
  847. if((sts & Sall) == 0){ /* not for us; sharing irq */
  848. iunlock(ctlr);
  849. return;
  850. }
  851. OUTS(Status, sts & Sall);
  852. cmd = INS(Cmd);
  853. if(cmd & Crun == 0){
  854. print("uhci %#ux: not running: uhci bug?\n", ctlr->port);
  855. /* BUG: should abort everything in this case */
  856. }
  857. if(debug > 1){
  858. frptr = INL(Flbaseadd);
  859. frno = INL(Frnum);
  860. frno = TRUNC(frno, Nframes);
  861. print("cmd %#ux sts %#ux frptr %#ux frno %d\n",
  862. cmd, sts, frptr, frno);
  863. }
  864. ctlr->ntdintr++;
  865. /*
  866. * Will we know in USB 3.0 who the interrupt was for?.
  867. * Do they still teach indexing in CS?
  868. * This is Intel's doing.
  869. */
  870. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  871. if(iso->state == Qrun || iso->state == Qdone)
  872. isointerrupt(ctlr, iso);
  873. for(qh = ctlr->qhs; qh != nil; qh = qh->next)
  874. if(qh->state == Qrun)
  875. qhinterrupt(ctlr, qh);
  876. else if(qh->state == Qclose)
  877. qhlinktd(qh, nil);
  878. iunlock(ctlr);
  879. }
  880. /*
  881. * iso->tdu is the next place to put data. When it gets full
  882. * it is activated and tdu advanced.
  883. */
  884. static long
  885. putsamples(Isoio *iso, uchar *b, long count)
  886. {
  887. long tot;
  888. long n;
  889. for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
  890. n = count-tot;
  891. if(n > maxtdlen(iso->tdu) - iso->nleft)
  892. n = maxtdlen(iso->tdu) - iso->nleft;
  893. memmove(iso->tdu->data+iso->nleft, b+tot, n);
  894. iso->nleft += n;
  895. if(iso->nleft == maxtdlen(iso->tdu)){
  896. tdisoinit(iso, iso->tdu, iso->nleft);
  897. iso->nleft = 0;
  898. iso->tdu = iso->tdu->next;
  899. }
  900. }
  901. return tot;
  902. }
  903. /*
  904. * Queue data for writing and return error status from
  905. * last writes done, to maintain buffered data.
  906. */
  907. static long
  908. episowrite(Ep *ep, Isoio *iso, void *a, long count)
  909. {
  910. Ctlr *ctlr;
  911. uchar *b;
  912. int tot;
  913. int nw;
  914. char *err;
  915. iso->debug = ep->debug;
  916. diprint("uhci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  917. ctlr = ep->hp->aux;
  918. qlock(iso);
  919. if(waserror()){
  920. qunlock(iso);
  921. nexterror();
  922. }
  923. ilock(ctlr);
  924. if(iso->state == Qclose){
  925. iunlock(ctlr);
  926. error(iso->err ? iso->err : Eio);
  927. }
  928. iso->state = Qrun;
  929. b = a;
  930. for(tot = 0; tot < count; tot += nw){
  931. while(isocanwrite(iso) == 0){
  932. iunlock(ctlr);
  933. diprint("uhci: episowrite: %#p sleep\n", iso);
  934. if(waserror()){
  935. if(iso->err == nil)
  936. iso->err = "I/O timed out";
  937. ilock(ctlr);
  938. break;
  939. }
  940. tsleep(iso, isocanwrite, iso, ep->tmout);
  941. poperror();
  942. ilock(ctlr);
  943. }
  944. err = iso->err;
  945. iso->err = nil;
  946. if(iso->state == Qclose || err != nil){
  947. iunlock(ctlr);
  948. error(err ? err : Eio);
  949. }
  950. if(iso->state != Qrun)
  951. panic("episowrite: iso not running");
  952. iunlock(ctlr); /* We could page fault here */
  953. nw = putsamples(iso, b+tot, count-tot);
  954. ilock(ctlr);
  955. }
  956. if(iso->state != Qclose)
  957. iso->state = Qdone;
  958. iunlock(ctlr);
  959. err = iso->err; /* in case it failed early */
  960. iso->err = nil;
  961. qunlock(iso);
  962. poperror();
  963. if(err != nil)
  964. error(err);
  965. diprint("uhci: episowrite: %#p %d bytes\n", iso, tot);
  966. return tot;
  967. }
  968. /*
  969. * Available data is kept at tdu and following tds, up to tdi (excluded).
  970. */
  971. static long
  972. episoread(Ep *ep, Isoio *iso, void *a, int count)
  973. {
  974. Ctlr *ctlr;
  975. uchar *b;
  976. int nr;
  977. int tot;
  978. Td *tdu;
  979. iso->debug = ep->debug;
  980. diprint("uhci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  981. b = a;
  982. ctlr = ep->hp->aux;
  983. qlock(iso);
  984. if(waserror()){
  985. qunlock(iso);
  986. nexterror();
  987. }
  988. iso->err = nil;
  989. iso->nerrs = 0;
  990. ilock(ctlr);
  991. if(iso->state == Qclose){
  992. iunlock(ctlr);
  993. error(iso->err ? iso->err : Eio);
  994. }
  995. iso->state = Qrun;
  996. while(isocanread(iso) == 0){
  997. iunlock(ctlr);
  998. diprint("uhci: episoread: %#p sleep\n", iso);
  999. if(waserror()){
  1000. if(iso->err == nil)
  1001. iso->err = "I/O timed out";
  1002. ilock(ctlr);
  1003. break;
  1004. }
  1005. tsleep(iso, isocanread, iso, ep->tmout);
  1006. poperror();
  1007. ilock(ctlr);
  1008. }
  1009. if(iso->state == Qclose){
  1010. iunlock(ctlr);
  1011. error(iso->err ? iso->err : Eio);
  1012. }
  1013. iso->state = Qdone;
  1014. assert(iso->tdu != iso->tdi);
  1015. for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
  1016. tdu = iso->tdu;
  1017. if(tdu->csw & Tdactive){
  1018. diprint("uhci: episoread: %#p tdu active\n", iso);
  1019. break;
  1020. }
  1021. nr = tdu->ndata;
  1022. if(tot + nr > count)
  1023. nr = count - tot;
  1024. if(nr == 0)
  1025. print("uhci: ep%d.%d: too many polls\n",
  1026. ep->dev->nb, ep->nb);
  1027. else{
  1028. iunlock(ctlr); /* We could page fault here */
  1029. memmove(b+tot, tdu->data, nr);
  1030. ilock(ctlr);
  1031. if(nr < tdu->ndata)
  1032. memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
  1033. tdu->ndata -= nr;
  1034. }
  1035. if(tdu->ndata == 0){
  1036. tdisoinit(iso, tdu, ep->maxpkt);
  1037. iso->tdu = tdu->next;
  1038. }
  1039. }
  1040. iunlock(ctlr);
  1041. qunlock(iso);
  1042. poperror();
  1043. diprint("uhci: episoread: %#p %d bytes err '%s'\n", iso, tot, iso->err);
  1044. if(iso->err != nil)
  1045. error(iso->err);
  1046. return tot;
  1047. }
  1048. static int
  1049. nexttoggle(int tog)
  1050. {
  1051. if(tog == Tddata0)
  1052. return Tddata1;
  1053. else
  1054. return Tddata0;
  1055. }
  1056. static Td*
  1057. epgettd(Ep *ep, Qio *io, int flags, void *a, int count)
  1058. {
  1059. Td *td;
  1060. int tok;
  1061. if(ep->maxpkt < count)
  1062. error("maxpkt too short");
  1063. td = tdalloc();
  1064. if(count <= Tdndata)
  1065. td->data = td->sbuff;
  1066. else
  1067. td->data = td->buff = smalloc(ep->maxpkt);
  1068. td->buffer = PCIWADDR(td->data);
  1069. td->ndata = count;
  1070. if(a != nil && count > 0)
  1071. memmove(td->data, a, count);
  1072. td->csw = Tderr2|Tderr1|flags;
  1073. if(ep->dev->speed == Lowspeed)
  1074. td->csw |= Tdlow;
  1075. tok = io->tok | io->toggle;
  1076. io->toggle = nexttoggle(io->toggle);
  1077. td->token = ((count-1)<<21) | ((io->usbid&0x7FF)<<8) | tok;
  1078. return td;
  1079. }
  1080. /*
  1081. * Try to get them idle
  1082. */
  1083. static void
  1084. aborttds(Qh *qh)
  1085. {
  1086. Td *td;
  1087. qh->state = Qdone;
  1088. qh->elink = QHterm;
  1089. for(td = qh->tds; td != nil; td = td->next){
  1090. if(td->csw & Tdactive)
  1091. td->ndata = 0;
  1092. td->csw &= ~(Tdactive|Tdioc);
  1093. }
  1094. }
  1095. static int
  1096. epiodone(void *a)
  1097. {
  1098. Qh *qh;
  1099. qh = a;
  1100. return qh->state != Qrun;
  1101. }
  1102. static void
  1103. epiowait(Ctlr *ctlr, Qio *io, int tmout, ulong load)
  1104. {
  1105. Qh *qh;
  1106. int timedout;
  1107. qh = io->qh;
  1108. ddqprint("uhci io %#p sleep on qh %#p state %uld\n", io, qh, qh->state);
  1109. timedout = 0;
  1110. if(waserror()){
  1111. dqprint("uhci io %#p qh %#p timed out\n", io, qh);
  1112. timedout++;
  1113. }else{
  1114. if(tmout == 0)
  1115. sleep(io, epiodone, qh);
  1116. else
  1117. tsleep(io, epiodone, qh, tmout);
  1118. poperror();
  1119. }
  1120. ilock(ctlr);
  1121. if(qh->state == Qrun)
  1122. timedout = 1;
  1123. else if(qh->state != Qdone && qh->state != Qclose)
  1124. panic("epio: queue not done and not closed");
  1125. if(timedout){
  1126. aborttds(io->qh);
  1127. io->err = "request timed out";
  1128. iunlock(ctlr);
  1129. if(!waserror()){
  1130. tsleep(&up->sleep, return0, 0, Abortdelay);
  1131. poperror();
  1132. }
  1133. ilock(ctlr);
  1134. }
  1135. if(qh->state != Qclose)
  1136. qh->state = Qidle;
  1137. qhlinktd(qh, nil);
  1138. ctlr->load -= load;
  1139. iunlock(ctlr);
  1140. }
  1141. /*
  1142. * Non iso I/O.
  1143. * To make it work for control transfers, the caller may
  1144. * lock the Qio for the entire control transfer.
  1145. */
  1146. static long
  1147. epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
  1148. {
  1149. Td *td, *ltd, *td0, *ntd;
  1150. Ctlr *ctlr;
  1151. Qh* qh;
  1152. long n, tot;
  1153. char buf[128];
  1154. uchar *c;
  1155. int saved, ntds, tmout;
  1156. ulong load;
  1157. char *err;
  1158. qh = io->qh;
  1159. ctlr = ep->hp->aux;
  1160. io->debug = ep->debug;
  1161. tmout = ep->tmout;
  1162. ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
  1163. io->tok == Tdtokin ? "in" : "out",
  1164. ep->dev->nb, ep->nb, io, count, ctlr->load);
  1165. if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
  1166. seprintdata(buf, buf+sizeof(buf), a, count);
  1167. print("uchi epio: user data: %s\n", buf);
  1168. }
  1169. if(mustlock){
  1170. qlock(io);
  1171. if(waserror()){
  1172. qunlock(io);
  1173. nexterror();
  1174. }
  1175. }
  1176. io->err = nil;
  1177. ilock(ctlr);
  1178. if(qh->state == Qclose){ /* Tds released by cancelio */
  1179. iunlock(ctlr);
  1180. error(io->err ? io->err : Eio);
  1181. }
  1182. if(qh->state != Qidle)
  1183. panic("epio: qh not idle");
  1184. qh->state = Qinstall;
  1185. iunlock(ctlr);
  1186. c = a;
  1187. td0 = ltd = nil;
  1188. load = tot = 0;
  1189. do{
  1190. n = ep->maxpkt;
  1191. if(count-tot < n)
  1192. n = count-tot;
  1193. if(c != nil && io->tok != Tdtokin)
  1194. td = epgettd(ep, io, Tdactive, c+tot, n);
  1195. else
  1196. td = epgettd(ep, io, Tdactive|Tdspd, nil, n);
  1197. if(td0 == nil)
  1198. td0 = td;
  1199. else
  1200. tdlinktd(ltd, td);
  1201. ltd = td;
  1202. tot += n;
  1203. load += ep->load;
  1204. }while(tot < count);
  1205. if(td0 == nil || ltd == nil)
  1206. panic("epio: no td");
  1207. ltd->csw |= Tdioc; /* the last one interrupts */
  1208. ddeprint("uhci: load %uld ctlr load %uld\n", load, ctlr->load);
  1209. ilock(ctlr);
  1210. if(qh->state != Qclose){
  1211. io->iotime = TK2MS(MACHP(0)->ticks);
  1212. qh->state = Qrun;
  1213. coherence();
  1214. qhlinktd(qh, td0);
  1215. ctlr->load += load;
  1216. }
  1217. iunlock(ctlr);
  1218. epiowait(ctlr, io, tmout, load);
  1219. if(debug > 1 || ep->debug > 1)
  1220. dumptd(td0, "epio: got tds: ");
  1221. tot = 0;
  1222. c = a;
  1223. saved = 0;
  1224. ntds = 0;
  1225. for(td = td0; td != nil; td = ntd){
  1226. ntds++;
  1227. /*
  1228. * Use td tok, not io tok, because of setup packets.
  1229. * Also, if the Td was stalled or active (previous Td
  1230. * was a short packet), we must save the toggle as it is.
  1231. */
  1232. if(td->csw & (Tdstalled|Tdactive)){
  1233. if(saved++ == 0)
  1234. io->toggle = td->token & Tddata1;
  1235. }else{
  1236. tot += td->ndata;
  1237. if(c != nil && tdtok(td) == Tdtokin && td->ndata > 0){
  1238. memmove(c, td->data, td->ndata);
  1239. c += td->ndata;
  1240. }
  1241. }
  1242. ntd = td->next;
  1243. tdfree(td);
  1244. }
  1245. err = io->err;
  1246. if(mustlock){
  1247. qunlock(io);
  1248. poperror();
  1249. }
  1250. ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
  1251. io, ntds, tot, err);
  1252. if(err != nil)
  1253. error(err);
  1254. if(tot < 0)
  1255. error(Eio);
  1256. return tot;
  1257. }
  1258. /*
  1259. * halt condition was cleared on the endpoint. update our toggles.
  1260. */
  1261. static void
  1262. clrhalt(Ep *ep)
  1263. {
  1264. Qio *io;
  1265. ep->clrhalt = 0;
  1266. switch(ep->ttype){
  1267. case Tbulk:
  1268. case Tintr:
  1269. io = ep->aux;
  1270. if(ep->mode != OREAD){
  1271. qlock(&io[OWRITE]);
  1272. io[OWRITE].toggle = Tddata0;
  1273. deprint("ep clrhalt for io %#p\n", io+OWRITE);
  1274. qunlock(&io[OWRITE]);
  1275. }
  1276. if(ep->mode != OWRITE){
  1277. qlock(&io[OREAD]);
  1278. io[OREAD].toggle = Tddata0;
  1279. deprint("ep clrhalt for io %#p\n", io+OREAD);
  1280. qunlock(&io[OREAD]);
  1281. }
  1282. break;
  1283. }
  1284. }
  1285. static long
  1286. epread(Ep *ep, void *a, long count)
  1287. {
  1288. Ctlio *cio;
  1289. Qio *io;
  1290. Isoio *iso;
  1291. char buf[160];
  1292. ulong delta;
  1293. ddeprint("uhci: epread\n");
  1294. if(ep->aux == nil)
  1295. panic("epread: not open");
  1296. switch(ep->ttype){
  1297. case Tctl:
  1298. cio = ep->aux;
  1299. qlock(cio);
  1300. if(waserror()){
  1301. qunlock(cio);
  1302. nexterror();
  1303. }
  1304. ddeprint("epread ctl ndata %d\n", cio->ndata);
  1305. if(cio->ndata < 0)
  1306. error("request expected");
  1307. else if(cio->ndata == 0){
  1308. cio->ndata = -1;
  1309. count = 0;
  1310. }else{
  1311. if(count > cio->ndata)
  1312. count = cio->ndata;
  1313. if(count > 0)
  1314. memmove(a, cio->data, count);
  1315. /* BUG for big transfers */
  1316. free(cio->data);
  1317. cio->data = nil;
  1318. cio->ndata = 0; /* signal EOF next time */
  1319. }
  1320. qunlock(cio);
  1321. poperror();
  1322. if(debug>1 || ep->debug){
  1323. seprintdata(buf, buf+sizeof(buf), a, count);
  1324. print("epread: %s\n", buf);
  1325. }
  1326. return count;
  1327. case Tbulk:
  1328. io = ep->aux;
  1329. if(ep->clrhalt)
  1330. clrhalt(ep);
  1331. return epio(ep, &io[OREAD], a, count, 1);
  1332. case Tintr:
  1333. io = ep->aux;
  1334. delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
  1335. if(delta < ep->pollival / 2)
  1336. tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
  1337. if(ep->clrhalt)
  1338. clrhalt(ep);
  1339. return epio(ep, &io[OREAD], a, count, 1);
  1340. case Tiso:
  1341. iso = ep->aux;
  1342. return episoread(ep, iso, a, count);
  1343. default:
  1344. panic("epread: bad ep ttype %d", ep->ttype);
  1345. }
  1346. return -1;
  1347. }
  1348. /*
  1349. * Control transfers are one setup write (data0)
  1350. * plus zero or more reads/writes (data1, data0, ...)
  1351. * plus a final write/read with data1 to ack.
  1352. * For both host to device and device to host we perform
  1353. * the entire transfer when the user writes the request,
  1354. * and keep any data read from the device for a later read.
  1355. * We call epio three times instead of placing all Tds at
  1356. * the same time because doing so leads to crc/tmout errors
  1357. * for some devices.
  1358. * Upon errors on the data phase we must still run the status
  1359. * phase or the device may cease responding in the future.
  1360. */
  1361. static long
  1362. epctlio(Ep *ep, Ctlio *cio, void *a, long count)
  1363. {
  1364. uchar *c;
  1365. long len;
  1366. ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
  1367. cio, ep->dev->nb, ep->nb, count);
  1368. if(count < Rsetuplen)
  1369. error("short usb comand");
  1370. qlock(cio);
  1371. free(cio->data);
  1372. cio->data = nil;
  1373. cio->ndata = 0;
  1374. if(waserror()){
  1375. qunlock(cio);
  1376. free(cio->data);
  1377. cio->data = nil;
  1378. cio->ndata = 0;
  1379. nexterror();
  1380. }
  1381. /* set the address if unset and out of configuration state */
  1382. if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
  1383. if(cio->usbid == 0)
  1384. cio->usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb&Devmax);
  1385. c = a;
  1386. cio->tok = Tdtoksetup;
  1387. cio->toggle = Tddata0;
  1388. if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
  1389. error(Eio);
  1390. a = c + Rsetuplen;
  1391. count -= Rsetuplen;
  1392. cio->toggle = Tddata1;
  1393. if(c[Rtype] & Rd2h){
  1394. cio->tok = Tdtokin;
  1395. len = GET2(c+Rcount);
  1396. if(len <= 0)
  1397. error("bad length in d2h request");
  1398. if(len > Maxctllen)
  1399. error("d2h data too large to fit in uhci");
  1400. a = cio->data = smalloc(len+1);
  1401. }else{
  1402. cio->tok = Tdtokout;
  1403. len = count;
  1404. }
  1405. if(len > 0)
  1406. if(waserror())
  1407. len = -1;
  1408. else{
  1409. len = epio(ep, cio, a, len, 0);
  1410. poperror();
  1411. }
  1412. if(c[Rtype] & Rd2h){
  1413. count = Rsetuplen;
  1414. cio->ndata = len;
  1415. cio->tok = Tdtokout;
  1416. }else{
  1417. if(len < 0)
  1418. count = -1;
  1419. else
  1420. count = Rsetuplen + len;
  1421. cio->tok = Tdtokin;
  1422. }
  1423. cio->toggle = Tddata1;
  1424. epio(ep, cio, nil, 0, 0);
  1425. qunlock(cio);
  1426. poperror();
  1427. ddeprint("epctlio cio %#p return %ld\n", cio, count);
  1428. return count;
  1429. }
  1430. static long
  1431. epwrite(Ep *ep, void *a, long count)
  1432. {
  1433. Ctlio *cio;
  1434. Isoio *iso;
  1435. Qio *io;
  1436. ulong delta;
  1437. char *b;
  1438. int tot;
  1439. int nw;
  1440. ddeprint("uhci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
  1441. if(ep->aux == nil)
  1442. panic("uhci: epwrite: not open");
  1443. switch(ep->ttype){
  1444. case Tctl:
  1445. cio = ep->aux;
  1446. return epctlio(ep, cio, a, count);
  1447. case Tbulk:
  1448. io = ep->aux;
  1449. if(ep->clrhalt)
  1450. clrhalt(ep);
  1451. /*
  1452. * Put at most Tdatomic Tds (512 bytes) at a time.
  1453. * Otherwise some devices produce babble errors.
  1454. */
  1455. b = a;
  1456. for(tot = 0; tot < count ; tot += nw){
  1457. nw = count - tot;
  1458. if(nw > Tdatomic * ep->maxpkt)
  1459. nw = Tdatomic * ep->maxpkt;
  1460. nw = epio(ep, &io[OWRITE], b+tot, nw, 1);
  1461. }
  1462. return tot;
  1463. case Tintr:
  1464. io = ep->aux;
  1465. delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
  1466. if(delta < ep->pollival)
  1467. tsleep(&up->sleep, return0, 0, ep->pollival - delta);
  1468. if(ep->clrhalt)
  1469. clrhalt(ep);
  1470. return epio(ep, &io[OWRITE], a, count, 1);
  1471. case Tiso:
  1472. iso = ep->aux;
  1473. return episowrite(ep, iso, a, count);
  1474. default:
  1475. panic("uhci: epwrite: bad ep ttype %d", ep->ttype);
  1476. }
  1477. return -1;
  1478. }
  1479. static void
  1480. isoopen(Ep *ep)
  1481. {
  1482. Ctlr *ctlr;
  1483. Isoio *iso;
  1484. int frno;
  1485. int i;
  1486. Td* td;
  1487. Td* ltd;
  1488. int size;
  1489. int left;
  1490. if(ep->mode == ORDWR)
  1491. error("iso i/o is half-duplex");
  1492. ctlr = ep->hp->aux;
  1493. iso = ep->aux;
  1494. iso->debug = ep->debug;
  1495. iso->next = nil; /* paranoia */
  1496. if(ep->mode == OREAD)
  1497. iso->tok = Tdtokin;
  1498. else
  1499. iso->tok = Tdtokout;
  1500. iso->usbid = ((ep->nb & Epmax)<<7)|(ep->dev->nb & Devmax);
  1501. iso->state = Qidle;
  1502. iso->nframes = Nframes/ep->pollival;
  1503. if(iso->nframes < 3)
  1504. error("uhci isoopen bug"); /* we need at least 3 tds */
  1505. ilock(ctlr);
  1506. if(ctlr->load + ep->load > 800)
  1507. print("usb: uhci: bandwidth may be exceeded\n");
  1508. ctlr->load += ep->load;
  1509. ctlr->isoload += ep->load;
  1510. dprint("uhci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
  1511. iunlock(ctlr);
  1512. /*
  1513. * From here on this cannot raise errors
  1514. * unless we catch them and release here all memory allocated.
  1515. */
  1516. if(ep->maxpkt > Tdndata)
  1517. iso->data = smalloc(iso->nframes*ep->maxpkt);
  1518. ilock(ctlr);
  1519. frno = INS(Frnum) + 10; /* start 10ms ahead */
  1520. frno = TRUNC(frno, Nframes);
  1521. iunlock(ctlr);
  1522. iso->td0frno = frno;
  1523. ltd = nil;
  1524. left = 0;
  1525. for(i = 0; i < iso->nframes; i++){
  1526. td = iso->tdps[frno] = tdalloc();
  1527. if(ep->mode == OREAD)
  1528. size = ep->maxpkt;
  1529. else{
  1530. size = (ep->hz+left) * ep->pollival / 1000;
  1531. size *= ep->samplesz;
  1532. left = (ep->hz+left) * ep->pollival % 1000;
  1533. if(size > ep->maxpkt){
  1534. print("uhci: ep%d.%d: size > maxpkt\n",
  1535. ep->dev->nb, ep->nb);
  1536. print("size = %d max = %ld\n", size, ep->maxpkt);
  1537. size = ep->maxpkt;
  1538. }
  1539. }
  1540. if(size > Tdndata)
  1541. td->data = iso->data + i * ep->maxpkt;
  1542. else
  1543. td->data = td->sbuff;
  1544. td->buffer = PCIWADDR(td->data);
  1545. tdisoinit(iso, td, size);
  1546. if(ltd != nil)
  1547. ltd->next = td;
  1548. ltd = td;
  1549. frno = TRUNC(frno+ep->pollival, Nframes);
  1550. }
  1551. ltd->next = iso->tdps[iso->td0frno];
  1552. iso->tdi = iso->tdps[iso->td0frno];
  1553. iso->tdu = iso->tdi; /* read: right now; write: 1s ahead */
  1554. ilock(ctlr);
  1555. frno = iso->td0frno;
  1556. for(i = 0; i < iso->nframes; i++){
  1557. iso->tdps[frno]->link = ctlr->frames[frno];
  1558. frno = TRUNC(frno+ep->pollival, Nframes);
  1559. }
  1560. coherence();
  1561. frno = iso->td0frno;
  1562. for(i = 0; i < iso->nframes; i++){
  1563. ctlr->frames[frno] = PCIWADDR(iso->tdps[frno]);
  1564. frno = TRUNC(frno+ep->pollival, Nframes);
  1565. }
  1566. iso->next = ctlr->iso;
  1567. ctlr->iso = iso;
  1568. iso->state = Qdone;
  1569. iunlock(ctlr);
  1570. if(debug > 1 || iso->debug >1)
  1571. isodump(iso, 0);
  1572. }
  1573. /*
  1574. * Allocate the endpoint and set it up for I/O
  1575. * in the controller. This must follow what's said
  1576. * in Ep regarding configuration, including perhaps
  1577. * the saved toggles (saved on a previous close of
  1578. * the endpoint data file by epclose).
  1579. */
  1580. static void
  1581. epopen(Ep *ep)
  1582. {
  1583. Ctlr *ctlr;
  1584. Qh *cqh;
  1585. Qio *io;
  1586. Ctlio *cio;
  1587. int usbid;
  1588. ctlr = ep->hp->aux;
  1589. deprint("uhci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
  1590. if(ep->aux != nil)
  1591. panic("uhci: epopen called with open ep");
  1592. if(waserror()){
  1593. free(ep->aux);
  1594. ep->aux = nil;
  1595. nexterror();
  1596. }
  1597. if(ep->maxpkt > Tdmaxpkt){
  1598. print("uhci: maxkpkt too large: using %d\n", Tdmaxpkt);
  1599. ep->maxpkt = Tdmaxpkt;
  1600. }
  1601. cqh = ctlr->qh[ep->ttype];
  1602. switch(ep->ttype){
  1603. case Tnone:
  1604. error("endpoint not configured");
  1605. case Tiso:
  1606. ep->aux = smalloc(sizeof(Isoio));
  1607. isoopen(ep);
  1608. break;
  1609. case Tctl:
  1610. cio = ep->aux = smalloc(sizeof(Ctlio));
  1611. cio->debug = ep->debug;
  1612. cio->ndata = -1;
  1613. cio->data = nil;
  1614. if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
  1615. break;
  1616. cio->qh = qhalloc(ctlr, cqh, cio, "epc");
  1617. break;
  1618. case Tbulk:
  1619. case Tintr:
  1620. io = ep->aux = smalloc(sizeof(Qio)*2);
  1621. io[OREAD].debug = io[OWRITE].debug = ep->debug;
  1622. usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb &Devmax);
  1623. if(ep->mode != OREAD){
  1624. if(ep->toggle[OWRITE] != 0)
  1625. io[OWRITE].toggle = Tddata1;
  1626. else
  1627. io[OWRITE].toggle = Tddata0;
  1628. io[OWRITE].tok = Tdtokout;
  1629. io[OWRITE].qh = qhalloc(ctlr, cqh, io+OWRITE, "epw");
  1630. io[OWRITE].usbid = usbid;
  1631. }
  1632. if(ep->mode != OWRITE){
  1633. if(ep->toggle[OREAD] != 0)
  1634. io[OREAD].toggle = Tddata1;
  1635. else
  1636. io[OREAD].toggle = Tddata0;
  1637. io[OREAD].tok = Tdtokin;
  1638. io[OREAD].qh = qhalloc(ctlr, cqh, io+OREAD, "epr");
  1639. io[OREAD].usbid = usbid;
  1640. }
  1641. break;
  1642. }
  1643. if(debug>1 || ep->debug)
  1644. dump(ep->hp);
  1645. deprint("uhci: epopen done\n");
  1646. poperror();
  1647. }
  1648. static void
  1649. cancelio(Ctlr *ctlr, Qio *io)
  1650. {
  1651. Qh *qh;
  1652. ilock(ctlr);
  1653. qh = io->qh;
  1654. if(io == nil || io->qh == nil || io->qh->state == Qclose){
  1655. iunlock(ctlr);
  1656. return;
  1657. }
  1658. dqprint("uhci: cancelio for qh %#p state %s\n",
  1659. qh, qhsname[qh->state]);
  1660. aborttds(qh);
  1661. qh->state = Qclose;
  1662. iunlock(ctlr);
  1663. if(!waserror()){
  1664. tsleep(&up->sleep, return0, 0, Abortdelay);
  1665. poperror();
  1666. }
  1667. wakeup(io);
  1668. qlock(io);
  1669. /* wait for epio if running */
  1670. qunlock(io);
  1671. qhfree(ctlr, qh);
  1672. io->qh = nil;
  1673. }
  1674. static void
  1675. cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, ulong load)
  1676. {
  1677. Isoio **il;
  1678. ulong *lp;
  1679. int i;
  1680. int frno;
  1681. Td *td;
  1682. ilock(ctlr);
  1683. if(iso->state == Qclose){
  1684. iunlock(ctlr);
  1685. return;
  1686. }
  1687. if(iso->state != Qrun && iso->state != Qdone)
  1688. panic("bad iso state");
  1689. iso->state = Qclose;
  1690. if(ctlr->isoload < load)
  1691. panic("uhci: low isoload");
  1692. ctlr->isoload -= load;
  1693. ctlr->load -= load;
  1694. for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
  1695. if(*il == iso)
  1696. break;
  1697. if(*il == nil)
  1698. panic("isocancel: not found");
  1699. *il = iso->next;
  1700. frno = iso->td0frno;
  1701. for(i = 0; i < iso->nframes; i++){
  1702. td = iso->tdps[frno];
  1703. td->csw &= ~(Tdioc|Tdactive);
  1704. for(lp=&ctlr->frames[frno]; !(*lp & Tdterm);
  1705. lp = &TPTR(*lp)->link)
  1706. if(TPTR(*lp) == td)
  1707. break;
  1708. if(*lp & Tdterm)
  1709. panic("cancelisoio: td not found");
  1710. *lp = td->link;
  1711. frno = TRUNC(frno+pollival, Nframes);
  1712. }
  1713. iunlock(ctlr);
  1714. /*
  1715. * wakeup anyone waiting for I/O and
  1716. * wait to be sure no I/O is in progress in the controller.
  1717. * and then wait to be sure episo-io is no longer running.
  1718. */
  1719. wakeup(iso);
  1720. diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
  1721. tsleep(&up->sleep, return0, 0, 5);
  1722. qlock(iso);
  1723. qunlock(iso);
  1724. diprint("cancelisoio iso %#p releasing iso\n", iso);
  1725. frno = iso->td0frno;
  1726. for(i = 0; i < iso->nframes; i++){
  1727. tdfree(iso->tdps[frno]);
  1728. iso->tdps[frno] = nil;
  1729. frno = TRUNC(frno+pollival, Nframes);
  1730. }
  1731. free(iso->data);
  1732. iso->data = nil;
  1733. }
  1734. static void
  1735. epclose(Ep *ep)
  1736. {
  1737. Ctlr *ctlr;
  1738. Ctlio *cio;
  1739. Isoio *iso;
  1740. Qio *io;
  1741. ctlr = ep->hp->aux;
  1742. deprint("uhci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
  1743. if(ep->aux == nil)
  1744. panic("uhci: epclose called with closed ep");
  1745. switch(ep->ttype){
  1746. case Tctl:
  1747. cio = ep->aux;
  1748. cancelio(ctlr, cio);
  1749. free(cio->data);
  1750. cio->data = nil;
  1751. break;
  1752. case Tbulk:
  1753. case Tintr:
  1754. io = ep->aux;
  1755. ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
  1756. if(ep->mode != OWRITE){
  1757. cancelio(ctlr, &io[OREAD]);
  1758. if(io[OREAD].toggle == Tddata1)
  1759. ep->toggle[OREAD] = 1;
  1760. }
  1761. if(ep->mode != OREAD){
  1762. cancelio(ctlr, &io[OWRITE]);
  1763. if(io[OWRITE].toggle == Tddata1)
  1764. ep->toggle[OWRITE] = 1;
  1765. }
  1766. break;
  1767. case Tiso:
  1768. iso = ep->aux;
  1769. cancelisoio(ctlr, iso, ep->pollival, ep->load);
  1770. break;
  1771. default:
  1772. panic("epclose: bad ttype %d", ep->ttype);
  1773. }
  1774. free(ep->aux);
  1775. ep->aux = nil;
  1776. }
  1777. static char*
  1778. seprintep(char *s, char *e, Ep *ep)
  1779. {
  1780. Ctlio *cio;
  1781. Qio *io;
  1782. Isoio *iso;
  1783. Ctlr *ctlr;
  1784. ctlr = ep->hp->aux;
  1785. ilock(ctlr);
  1786. if(ep->aux == nil){
  1787. *s = 0;
  1788. iunlock(ctlr);
  1789. return s;
  1790. }
  1791. switch(ep->ttype){
  1792. case Tctl:
  1793. cio = ep->aux;
  1794. s = seprint(s,e,"cio %#p qh %#p"
  1795. " id %#x tog %#x tok %#x err %s\n",
  1796. cio, cio->qh, cio->usbid, cio->toggle,
  1797. cio->tok, cio->err);
  1798. break;
  1799. case Tbulk:
  1800. case Tintr:
  1801. io = ep->aux;
  1802. if(ep->mode != OWRITE)
  1803. s = seprint(s,e,"r: qh %#p id %#x tog %#x tok %#x err %s\n",
  1804. io[OREAD].qh, io[OREAD].usbid, io[OREAD].toggle,
  1805. io[OREAD].tok, io[OREAD].err);
  1806. if(ep->mode != OREAD)
  1807. s = seprint(s,e,"w: qh %#p id %#x tog %#x tok %#x err %s\n",
  1808. io[OWRITE].qh, io[OWRITE].usbid, io[OWRITE].toggle,
  1809. io[OWRITE].tok, io[OWRITE].err);
  1810. break;
  1811. case Tiso:
  1812. iso = ep->aux;
  1813. s = seprint(s,e,"iso %#p id %#x tok %#x tdu %#p tdi %#p err %s\n",
  1814. iso, iso->usbid, iso->tok, iso->tdu, iso->tdi, iso->err);
  1815. break;
  1816. }
  1817. iunlock(ctlr);
  1818. return s;
  1819. }
  1820. static int
  1821. portenable(Hci *hp, int port, int on)
  1822. {
  1823. int s;
  1824. int ioport;
  1825. Ctlr *ctlr;
  1826. ctlr = hp->aux;
  1827. dprint("uhci: %#x port %d enable=%d\n", ctlr->port, port, on);
  1828. ioport = PORT(port-1);
  1829. qlock(&ctlr->portlck);
  1830. if(waserror()){
  1831. qunlock(&ctlr->portlck);
  1832. nexterror();
  1833. }
  1834. ilock(ctlr);
  1835. s = INS(ioport);
  1836. if(on)
  1837. OUTS(ioport, s | PSenable);
  1838. else
  1839. OUTS(ioport, s & ~PSenable);
  1840. microdelay(64);
  1841. iunlock(ctlr);
  1842. tsleep(&up->sleep, return0, 0, Enabledelay);
  1843. dprint("uhci %#ux port %d enable=%d: sts %#x\n",
  1844. ctlr->port, port, on, INS(ioport));
  1845. qunlock(&ctlr->portlck);
  1846. poperror();
  1847. return 0;
  1848. }
  1849. static int
  1850. portreset(Hci *hp, int port, int on)
  1851. {
  1852. int i, p;
  1853. Ctlr *ctlr;
  1854. if(on == 0)
  1855. return 0;
  1856. ctlr = hp->aux;
  1857. dprint("uhci: %#ux port %d reset\n", ctlr->port, port);
  1858. p = PORT(port-1);
  1859. ilock(ctlr);
  1860. OUTS(p, PSreset);
  1861. delay(50);
  1862. OUTS(p, INS(p) & ~PSreset);
  1863. OUTS(p, INS(p) | PSenable);
  1864. microdelay(64);
  1865. for(i=0; i<1000 && (INS(p) & PSenable) == 0; i++)
  1866. ;
  1867. OUTS(p, (INS(p) & ~PSreset)|PSenable);
  1868. iunlock(ctlr);
  1869. dprint("uhci %#ux after port %d reset: sts %#x\n",
  1870. ctlr->port, port, INS(p));
  1871. return 0;
  1872. }
  1873. static int
  1874. portstatus(Hci *hp, int port)
  1875. {
  1876. int s;
  1877. int r;
  1878. int ioport;
  1879. Ctlr *ctlr;
  1880. ctlr = hp->aux;
  1881. ioport = PORT(port-1);
  1882. qlock(&ctlr->portlck);
  1883. if(waserror()){
  1884. iunlock(ctlr);
  1885. qunlock(&ctlr->portlck);
  1886. nexterror();
  1887. }
  1888. ilock(ctlr);
  1889. s = INS(ioport);
  1890. if(s & (PSstatuschg | PSchange)){
  1891. OUTS(ioport, s);
  1892. ddprint("uhci %#ux port %d status %#x\n", ctlr->port, port, s);
  1893. }
  1894. iunlock(ctlr);
  1895. qunlock(&ctlr->portlck);
  1896. poperror();
  1897. /*
  1898. * We must return status bits as a
  1899. * get port status hub request would do.
  1900. */
  1901. r = 0;
  1902. if(s & PSpresent)
  1903. r |= HPpresent;
  1904. if(s & PSenable)
  1905. r |= HPenable;
  1906. if(s & PSsuspend)
  1907. r |= HPsuspend;
  1908. if(s & PSreset)
  1909. r |= HPreset;
  1910. if(s & PSslow)
  1911. r |= HPslow;
  1912. if(s & PSstatuschg)
  1913. r |= HPstatuschg;
  1914. if(s & PSchange)
  1915. r |= HPchange;
  1916. return r;
  1917. }
  1918. static void
  1919. scanpci(void)
  1920. {
  1921. static int already = 0;
  1922. int io;
  1923. int i;
  1924. Ctlr *ctlr;
  1925. Pcidev *p;
  1926. if(already)
  1927. return;
  1928. already = 1;
  1929. p = nil;
  1930. while(p = pcimatch(p, 0, 0)){
  1931. /*
  1932. * Find UHCI controllers (Programming Interface = 0).
  1933. */
  1934. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb)
  1935. continue;
  1936. switch(p->ccrp){
  1937. case 0:
  1938. io = p->mem[4].bar & ~0x0F;
  1939. break;
  1940. default:
  1941. continue;
  1942. }
  1943. if(io == 0){
  1944. print("usbuhci: %#x %#x: failed to map registers\n",
  1945. p->vid, p->did);
  1946. continue;
  1947. }
  1948. if(ioalloc(io, p->mem[4].size, 0, "usbuhci") < 0){
  1949. print("usbuhci: port %#ux in use\n", io);
  1950. continue;
  1951. }
  1952. if(p->intl == 0xFF || p->intl == 0){
  1953. print("usbuhci: no irq assigned for port %#ux\n", io);
  1954. continue;
  1955. }
  1956. dprint("uhci: %#x %#x: port %#ux size %#x irq %d\n",
  1957. p->vid, p->did, io, p->mem[4].size, p->intl);
  1958. ctlr = smalloc(sizeof(Ctlr));
  1959. ctlr->pcidev = p;
  1960. ctlr->port = io;
  1961. for(i = 0; i < Nhcis; i++)
  1962. if(ctlrs[i] == nil){
  1963. ctlrs[i] = ctlr;
  1964. break;
  1965. }
  1966. if(i == Nhcis)
  1967. print("uhci: bug: no more controllers\n");
  1968. }
  1969. }
  1970. static void
  1971. uhcimeminit(Ctlr *ctlr)
  1972. {
  1973. Td* td;
  1974. Qh *qh;
  1975. int frsize;
  1976. int i;
  1977. ctlr->qhs = ctlr->qh[Tctl] = qhalloc(ctlr, nil, nil, "CTL");
  1978. ctlr->qh[Tintr] = qhalloc(ctlr, ctlr->qh[Tctl], nil, "INT");
  1979. ctlr->qh[Tbulk] = qhalloc(ctlr, ctlr->qh[Tintr], nil, "BLK");
  1980. /* idle Td from dummy Qh at the end. looped back to itself */
  1981. /* This is a workaround for PIIX4 errata 29773804.pdf */
  1982. qh = qhalloc(ctlr, ctlr->qh[Tbulk], nil, "BWS");
  1983. td = tdalloc();
  1984. td->link = PCIWADDR(td);
  1985. qhlinktd(qh, td);
  1986. /* loop (hw only) from the last qh back to control xfers.
  1987. * this may be done only for some of them. Disable until ehci comes.
  1988. */
  1989. if(0)
  1990. qh->link = PCIWADDR(ctlr->qhs);
  1991. frsize = Nframes*sizeof(ulong);
  1992. ctlr->frames = xspanalloc(frsize, frsize, 0);
  1993. if(ctlr->frames == nil)
  1994. panic("uhci reset: no memory");
  1995. ctlr->iso = nil;
  1996. for(i = 0; i < Nframes; i++)
  1997. ctlr->frames[i] = PCIWADDR(ctlr->qhs)|QHlinkqh;
  1998. OUTL(Flbaseadd, PCIWADDR(ctlr->frames));
  1999. OUTS(Frnum, 0);
  2000. dprint("uhci %#ux flb %#ulx frno %#ux\n", ctlr->port,
  2001. INL(Flbaseadd), INS(Frnum));
  2002. }
  2003. static void
  2004. init(Hci *hp)
  2005. {
  2006. Ctlr *ctlr;
  2007. int sts;
  2008. int i;
  2009. ctlr = hp->aux;
  2010. dprint("uhci %#ux init\n", ctlr->port);
  2011. coherence();
  2012. ilock(ctlr);
  2013. OUTS(Usbintr, Itmout|Iresume|Ioc|Ishort);
  2014. uhcirun(ctlr, 1);
  2015. dprint("uhci: init: cmd %#ux sts %#ux sof %#ux",
  2016. INS(Cmd), INS(Status), INS(SOFmod));
  2017. dprint(" flb %#ulx frno %#ux psc0 %#ux psc1 %#ux",
  2018. INL(Flbaseadd), INS(Frnum), INS(PORT(0)), INS(PORT(1)));
  2019. /* guess other ports */
  2020. for(i = 2; i < 6; i++){
  2021. sts = INS(PORT(i));
  2022. if(sts != 0xFFFF && (sts & PSreserved1) == 1){
  2023. dprint(" psc%d %#ux", i, sts);
  2024. hp->nports++;
  2025. }else
  2026. break;
  2027. }
  2028. for(i = 0; i < hp->nports; i++)
  2029. OUTS(PORT(i), 0);
  2030. iunlock(ctlr);
  2031. }
  2032. static void
  2033. uhcireset(Ctlr *ctlr)
  2034. {
  2035. int i;
  2036. int sof;
  2037. ilock(ctlr);
  2038. dprint("uhci %#ux reset\n", ctlr->port);
  2039. /*
  2040. * Turn off legacy mode. Some controllers won't
  2041. * interrupt us as expected otherwise.
  2042. */
  2043. uhcirun(ctlr, 0);
  2044. pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
  2045. OUTS(Usbintr, 0);
  2046. sof = INB(SOFmod);
  2047. uhcicmd(ctlr, Cgreset); /* global reset */
  2048. delay(Resetdelay);
  2049. uhcicmd(ctlr, 0); /* all halt */
  2050. uhcicmd(ctlr, Chcreset); /* controller reset */
  2051. for(i = 0; i < 100; i++){
  2052. if((INS(Cmd) & Chcreset) == 0)
  2053. break;
  2054. delay(1);
  2055. }
  2056. if(i == 100)
  2057. print("uhci %#x controller reset timed out\n", ctlr->port);
  2058. OUTB(SOFmod, sof);
  2059. iunlock(ctlr);
  2060. }
  2061. static void
  2062. setdebug(Hci*, int d)
  2063. {
  2064. debug = d;
  2065. }
  2066. static void
  2067. shutdown(Hci *hp)
  2068. {
  2069. Ctlr *ctlr;
  2070. ctlr = hp->aux;
  2071. ilock(ctlr);
  2072. uhcirun(ctlr, 0);
  2073. delay(100);
  2074. iunlock(ctlr);
  2075. }
  2076. static int
  2077. reset(Hci *hp)
  2078. {
  2079. static Lock resetlck;
  2080. int i;
  2081. Ctlr *ctlr;
  2082. Pcidev *p;
  2083. if(getconf("*nousbuhci"))
  2084. return -1;
  2085. ilock(&resetlck);
  2086. scanpci();
  2087. /*
  2088. * Any adapter matches if no hp->port is supplied,
  2089. * otherwise the ports must match.
  2090. */
  2091. ctlr = nil;
  2092. for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
  2093. ctlr = ctlrs[i];
  2094. if(ctlr->active == 0)
  2095. if(hp->port == 0 || hp->port == ctlr->port){
  2096. ctlr->active = 1;
  2097. break;
  2098. }
  2099. }
  2100. iunlock(&resetlck);
  2101. if(ctlrs[i] == nil || i == Nhcis)
  2102. return -1;
  2103. p = ctlr->pcidev;
  2104. hp->aux = ctlr;
  2105. hp->port = ctlr->port;
  2106. hp->irq = p->intl;
  2107. hp->tbdf = p->tbdf;
  2108. hp->nports = 2; /* default */
  2109. uhcireset(ctlr);
  2110. uhcimeminit(ctlr);
  2111. /*
  2112. * Linkage to the generic HCI driver.
  2113. */
  2114. hp->init = init;
  2115. hp->dump = dump;
  2116. hp->interrupt = interrupt;
  2117. hp->epopen = epopen;
  2118. hp->epclose = epclose;
  2119. hp->epread = epread;
  2120. hp->epwrite = epwrite;
  2121. hp->seprintep = seprintep;
  2122. hp->portenable = portenable;
  2123. hp->portreset = portreset;
  2124. hp->portstatus = portstatus;
  2125. hp->shutdown = shutdown;
  2126. hp->debug = setdebug;
  2127. hp->type = "uhci";
  2128. return 0;
  2129. }
  2130. void
  2131. usbuhcilink(void)
  2132. {
  2133. addhcitype("uhci", reset);
  2134. }