usbuhci.c 52 KB

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