usbohci.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  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 Open Host Controller Interface (Ohci) driver
  11. *
  12. * BUGS:
  13. * - Missing isochronous input streams.
  14. * - Too many delays and ilocks.
  15. * - bandwidth admission control must be done per-frame.
  16. * - Buffering could be handled like in uhci, to avoid
  17. * needed block allocation and avoid allocs for small Tds.
  18. * - must warn of power overruns.
  19. */
  20. #include "u.h"
  21. #include "../port/lib.h"
  22. #include "mem.h"
  23. #include "dat.h"
  24. #include "fns.h"
  25. #include "io.h"
  26. #include "../port/error.h"
  27. #include "../port/usb.h"
  28. typedef struct Ctlio Ctlio;
  29. typedef struct Ctlr Ctlr;
  30. typedef struct Ed Ed;
  31. typedef struct Edpool Edpool;
  32. typedef struct Epx Epx;
  33. typedef struct Hcca Hcca;
  34. typedef struct Isoio Isoio;
  35. typedef struct Ohci Ohci;
  36. typedef struct Qio Qio;
  37. typedef struct Qtree Qtree;
  38. typedef struct Td Td;
  39. typedef struct Tdpool Tdpool;
  40. enum
  41. {
  42. Incr = 64, /* for Td and Ed pools */
  43. Align = 0x20, /* OHCI only requires 0x10 */
  44. /* use always a power of 2 */
  45. Abortdelay = 1, /* delay after cancelling Tds (ms) */
  46. Tdatomic = 8, /* max nb. of Tds per bulk I/O op. */
  47. Enabledelay = 100, /* waiting for a port to enable */
  48. /* Queue states (software) */
  49. Qidle = 0,
  50. Qinstall,
  51. Qrun,
  52. Qdone,
  53. Qclose,
  54. Qfree,
  55. /* Ed control bits */
  56. Edmpsmask = 0x7ff, /* max packet size */
  57. Edmpsshift = 16,
  58. Edlow = 1 << 13, /* low speed */
  59. Edskip = 1 << 14, /* skip this ed */
  60. Ediso = 1 << 15, /* iso Tds used */
  61. Edtddir = 0, /* get dir from td */
  62. Edin = 2 << 11, /* direction in */
  63. Edout = 1 << 11, /* direction out */
  64. Eddirmask = 3 << 11, /* direction bits */
  65. Edhalt = 1, /* halted (in head ptr) */
  66. Edtoggle = 2, /* toggle (in head ptr) 1 == data1 */
  67. /* Td control bits */
  68. Tdround = 1<<18, /* (rounding) short packets ok */
  69. Tdtoksetup = 0<<19, /* setup packet */
  70. Tdtokin = 2<<19, /* in packet */
  71. Tdtokout = 1<<19, /* out packet */
  72. Tdtokmask = 3<<19, /* in/out/setup bits */
  73. Tdnoioc = 7<<21, /* intr. cnt. value for no interrupt */
  74. Tdusetog = 1<<25, /* use toggle from Td (1) or Ed (0) */
  75. Tddata1 = 1<<24, /* data toggle (1 == data1) */
  76. Tddata0 = 0<<24,
  77. Tdfcmask = 7, /* frame count (iso) */
  78. Tdfcshift = 24,
  79. Tdsfmask = 0xFFFF, /* starting frame (iso) */
  80. Tderrmask = 3, /* error counter */
  81. Tderrshift = 26,
  82. Tdccmask = 0xf, /* condition code (status) */
  83. Tdccshift = 28,
  84. Tdiccmask = 0xf, /* condition code (iso, offsets) */
  85. Tdiccshift = 12,
  86. Ntdframes = 0x10000, /* # of different iso frame numbers */
  87. /* Td errors (condition code) */
  88. Tdok = 0,
  89. Tdcrc = 1,
  90. Tdbitstuff = 2,
  91. Tdbadtog = 3,
  92. Tdstalled = 4,
  93. Tdtmout = 5,
  94. Tdpidchk = 6,
  95. Tdbadpid = 7,
  96. Tddataovr = 8,
  97. Tddataund = 9,
  98. Tdbufovr = 0xC,
  99. Tdbufund = 0xD,
  100. Tdnotacc = 0xE,
  101. /* control register */
  102. Cple = 0x04, /* periodic list enable */
  103. Cie = 0x08, /* iso. list enable */
  104. Ccle = 0x10, /* ctl list enable */
  105. Cble = 0x20, /* bulk list enable */
  106. Cfsmask = 3 << 6, /* functional state... */
  107. Cfsreset = 0 << 6,
  108. Cfsresume = 1 << 6,
  109. Cfsoper = 2 << 6,
  110. Cfssuspend = 3 << 6,
  111. /* command status */
  112. Sblf = 1 << 2, /* bulk list (load) flag */
  113. Sclf = 1 << 1, /* control list (load) flag */
  114. Shcr = 1 << 0, /* host controller reset */
  115. /* intr enable */
  116. Mie = 1 << 31,
  117. Oc = 1 << 30,
  118. Rhsc = 1 << 6,
  119. Fno = 1 << 5,
  120. Ue = 1 << 4,
  121. Rd = 1 << 3,
  122. Sf = 1 << 2,
  123. Wdh = 1 << 1,
  124. So = 1 << 0,
  125. Fmaxpktmask = 0x7fff,
  126. Fmaxpktshift = 16,
  127. HcRhDescA_POTPGT_MASK = 0xff << 24,
  128. HcRhDescA_POTPGT_SHIFT = 24,
  129. /* Rh status */
  130. Lps = 1 << 0,
  131. Cgp = 1 << 0,
  132. Oci = 1 << 1,
  133. Psm = 1 << 8,
  134. Nps = 1 << 9,
  135. Drwe = 1 << 15,
  136. Srwe = 1 << 15,
  137. Lpsc = 1 << 16,
  138. Ccic = 1 << 17,
  139. Crwe = 1 << 31,
  140. /* port status */
  141. Ccs = 0x00001, /* current connect status */
  142. Pes = 0x00002, /* port enable status */
  143. Pss = 0x00004, /* port suspend status */
  144. Poci = 0x00008, /* over current indicator */
  145. Prs = 0x00010, /* port reset status */
  146. Pps = 0x00100, /* port power status */
  147. Lsda = 0x00200, /* low speed device attached */
  148. Csc = 0x10000, /* connect status change */
  149. Pesc = 0x20000, /* enable status change */
  150. Pssc = 0x40000, /* suspend status change */
  151. Ocic = 0x80000, /* over current ind. change */
  152. Prsc = 0x100000, /* reset status change */
  153. /* port status write bits */
  154. Cpe = 0x001, /* clear port enable */
  155. Spe = 0x002, /* set port enable */
  156. Spr = 0x010, /* set port reset */
  157. Spp = 0x100, /* set port power */
  158. Cpp = 0x200, /* clear port power */
  159. };
  160. /*
  161. * Endpoint descriptor. (first 4 words used by hardware)
  162. */
  163. struct Ed {
  164. uint32_t ctrl;
  165. uint32_t tail; /* transfer descriptor */
  166. uint32_t head;
  167. uint32_t nexted;
  168. Ed* next; /* sw; in free list or next in list */
  169. Td* tds; /* in use by current xfer; all for iso */
  170. Ep* ep; /* debug/align */
  171. Ed* inext; /* debug/align (dump interrupt eds). */
  172. };
  173. /*
  174. * Endpoint I/O state (software), per direction.
  175. */
  176. struct Qio
  177. {
  178. QLock; /* for the entire I/O process */
  179. Rendez; /* wait for completion */
  180. Ed* ed; /* to place Tds on it */
  181. int sched; /* queue number (intr/iso) */
  182. int toggle; /* Tddata0/Tddata1 */
  183. uint32_t usbid; /* device/endpoint address */
  184. int tok; /* Tdsetup, Tdtokin, Tdtokout */
  185. int32_t iotime; /* last I/O time; to hold interrupt polls */
  186. int debug; /* for the endpoint */
  187. char* err; /* error status */
  188. int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
  189. int32_t bw; /* load (intr/iso) */
  190. };
  191. struct Ctlio
  192. {
  193. Qio; /* single Ed for all transfers */
  194. unsigned char* data; /* read from last ctl req. */
  195. int ndata; /* number of bytes read */
  196. };
  197. struct Isoio
  198. {
  199. Qio;
  200. int nframes; /* number of frames for a full second */
  201. Td* atds; /* Tds avail for further I/O */
  202. int navail; /* number of avail Tds */
  203. uint32_t frno; /* next frame number avail for I/O */
  204. uint32_t left; /* remainder after rounding Hz to samples/ms */
  205. int nerrs; /* consecutive errors on iso I/O */
  206. };
  207. /*
  208. * Transfer descriptor. Size must be multiple of 32
  209. * First block is used by hardware (aligned to 32).
  210. */
  211. struct Td
  212. {
  213. uint32_t ctrl;
  214. uint32_t cbp; /* current buffer pointer */
  215. uint32_t nexttd;
  216. uint32_t be;
  217. uint16_t offsets[8]; /* used by Iso Tds only */
  218. Td* next; /* in free or Ed tds list */
  219. Td* anext; /* in avail td list (iso) */
  220. Ep* ep; /* using this Td for I/O */
  221. Qio* io; /* using this Td for I/O */
  222. Block* bp; /* data for this Td */
  223. uint32_t nbytes; /* bytes in this Td */
  224. uint32_t cbp0; /* initial value for cbp */
  225. uint32_t last; /* true for last Td in Qio */
  226. };
  227. /*
  228. * Host controller communication area (hardware)
  229. */
  230. struct Hcca
  231. {
  232. uint32_t intrtable[32];
  233. uint16_t framenumber;
  234. uint16_t pad1;
  235. uint32_t donehead;
  236. unsigned char reserved[116];
  237. };
  238. /*
  239. * I/O registers
  240. */
  241. struct Ohci
  242. {
  243. /* control and status group */
  244. uint32_t revision; /*00*/
  245. uint32_t control; /*04*/
  246. uint32_t cmdsts; /*08*/
  247. uint32_t intrsts; /*0c*/
  248. uint32_t intrenable; /*10*/
  249. uint32_t intrdisable; /*14*/
  250. /* memory pointer group */
  251. uint32_t hcca; /*18*/
  252. uint32_t periodcurred; /*1c*/
  253. uint32_t ctlheaded; /*20*/
  254. uint32_t ctlcurred; /*24*/
  255. uint32_t bulkheaded; /*28*/
  256. uint32_t bulkcurred; /*2c*/
  257. uint32_t donehead; /*30*/
  258. /* frame counter group */
  259. uint32_t fminterval; /*34*/
  260. uint32_t fmremaining; /*38*/
  261. uint32_t fmnumber; /*3c*/
  262. uint32_t periodicstart; /*40*/
  263. uint32_t lsthreshold; /*44*/
  264. /* root hub group */
  265. uint32_t rhdesca; /*48*/
  266. uint32_t rhdescb; /*4c*/
  267. uint32_t rhsts; /*50*/
  268. uint32_t rhportsts[15]; /*54*/
  269. uint32_t pad25[20]; /*90*/
  270. /* unknown */
  271. uint32_t hostueaddr; /*e0*/
  272. uint32_t hostuests; /*e4*/
  273. uint32_t hosttimeoutctrl; /*e8*/
  274. uint32_t pad59; /*ec*/
  275. uint32_t pad60; /*f0*/
  276. uint32_t hostrevision; /*f4*/
  277. uint32_t pad62[2];
  278. /*100*/
  279. };
  280. /*
  281. * Endpoint tree (software)
  282. */
  283. struct Qtree
  284. {
  285. int nel;
  286. int depth;
  287. uint32_t* bw;
  288. Ed** root;
  289. };
  290. struct Tdpool
  291. {
  292. Lock;
  293. Td* free;
  294. int nalloc;
  295. int ninuse;
  296. int nfree;
  297. };
  298. struct Edpool
  299. {
  300. Lock;
  301. Ed* free;
  302. int nalloc;
  303. int ninuse;
  304. int nfree;
  305. };
  306. struct Ctlr
  307. {
  308. Lock; /* for ilock; lists and basic ctlr I/O */
  309. QLock resetl; /* lock controller during USB reset */
  310. int active;
  311. Ctlr* next;
  312. int nports;
  313. Ohci* ohci; /* base I/O address */
  314. Hcca* hcca; /* intr/done Td lists (used by hardware) */
  315. int overrun; /* sched. overrun */
  316. Ed* intrhd; /* list of intr. eds in tree */
  317. Qtree* tree; /* tree for t Ep i/o */
  318. int ntree; /* number of dummy Eds in tree */
  319. Pcidev* pcidev;
  320. };
  321. #define dqprint if(debug || io && io->debug)print
  322. #define ddqprint if(debug>1 || (io && io->debug>1))print
  323. #define diprint if(debug || iso && iso->debug)print
  324. #define ddiprint if(debug>1 || (iso && iso->debug>1))print
  325. #define TRUNC(x, sz) ((x) & ((sz)-1))
  326. static int ohciinterrupts[Nttypes];
  327. static char* iosname[] = { "idle", "install", "run", "done", "close", "FREE" };
  328. static int debug;
  329. static Edpool edpool;
  330. static Tdpool tdpool;
  331. static Ctlr* ctlrs[Nhcis];
  332. /* Never used
  333. static char EnotWritten[] = "usb write unfinished";
  334. static char EnotRead[] = "usb read unfinished";
  335. static char Eunderrun[] = "usb endpoint underrun";
  336. static QLock usbhstate; / * protects name space state * /
  337. static int schedendpt(Ctlr *ub, Ep *ep);
  338. static void unschedendpt(Ctlr *ub, Ep *ep);
  339. static int32_t qtd(Ctlr*, Ep*, int, Block*, unsigned char*, unsigned char*, int, uint32_t);
  340. */
  341. static char* errmsgs[] =
  342. {
  343. [Tdcrc] "crc error",
  344. [Tdbitstuff] "bit stuffing error",
  345. [Tdbadtog] "bad toggle",
  346. [Tdstalled] Estalled,
  347. [Tdtmout] "timeout error",
  348. [Tdpidchk] "pid check error",
  349. [Tdbadpid] "bad pid",
  350. [Tddataovr] "data overrun",
  351. [Tddataund] "data underrun",
  352. [Tdbufovr] "buffer overrun",
  353. [Tdbufund] "buffer underrun",
  354. [Tdnotacc] "not accessed"
  355. };
  356. static void*
  357. pa2ptr(uint32_t pa)
  358. {
  359. if(pa == 0)
  360. return nil;
  361. else
  362. return KADDR(pa);
  363. }
  364. static uint32_t
  365. ptr2pa(void *p)
  366. {
  367. if(p == nil)
  368. return 0;
  369. else
  370. return PADDR(p);
  371. }
  372. static void
  373. waitSOF(Ctlr *ub)
  374. {
  375. int frame = ub->hcca->framenumber & 0x3f;
  376. do {
  377. delay(2);
  378. } while(frame == (ub->hcca->framenumber & 0x3f));
  379. }
  380. static char*
  381. errmsg(int err)
  382. {
  383. if(err < nelem(errmsgs))
  384. return errmsgs[err];
  385. return nil;
  386. }
  387. static Ed*
  388. ctlhd(Ctlr *ctlr)
  389. {
  390. return pa2ptr(ctlr->ohci->ctlheaded);
  391. }
  392. static Ed*
  393. bulkhd(Ctlr *ctlr)
  394. {
  395. return pa2ptr(ctlr->ohci->bulkheaded);
  396. }
  397. static void
  398. edlinked(Ed *ed, Ed *next)
  399. {
  400. if(ed == nil)
  401. print("edlinked: nil ed: pc %#p\n", getcallerpc(&ed));
  402. ed->nexted = ptr2pa(next);
  403. ed->next = next;
  404. }
  405. static void
  406. setctlhd(Ctlr *ctlr, Ed *ed)
  407. {
  408. ctlr->ohci->ctlheaded = ptr2pa(ed);
  409. if(ed != nil)
  410. ctlr->ohci->cmdsts |= Sclf; /* reload it on next pass */
  411. }
  412. static void
  413. setbulkhd(Ctlr *ctlr, Ed *ed)
  414. {
  415. ctlr->ohci->bulkheaded = ptr2pa(ed);
  416. if(ed != nil)
  417. ctlr->ohci->cmdsts |= Sblf; /* reload it on next pass */
  418. }
  419. static void
  420. unlinkctl(Ctlr *ctlr, Ed *ed)
  421. {
  422. Ed *this, *prev, *next;
  423. ctlr->ohci->control &= ~Ccle;
  424. waitSOF(ctlr);
  425. this = ctlhd(ctlr);
  426. ctlr->ohci->ctlcurred = 0;
  427. prev = nil;
  428. while(this != nil && this != ed){
  429. prev = this;
  430. this = this->next;
  431. }
  432. if(this == nil){
  433. print("unlinkctl: not found\n");
  434. return;
  435. }
  436. next = this->next;
  437. if(prev == nil)
  438. setctlhd(ctlr, next);
  439. else
  440. edlinked(prev, next);
  441. ctlr->ohci->control |= Ccle;
  442. edlinked(ed, nil); /* wipe out next field */
  443. }
  444. static void
  445. unlinkbulk(Ctlr *ctlr, Ed *ed)
  446. {
  447. Ed *this, *prev, *next;
  448. ctlr->ohci->control &= ~Cble;
  449. waitSOF(ctlr);
  450. this = bulkhd(ctlr);
  451. ctlr->ohci->bulkcurred = 0;
  452. prev = nil;
  453. while(this != nil && this != ed){
  454. prev = this;
  455. this = this->next;
  456. }
  457. if(this == nil){
  458. print("unlinkbulk: not found\n");
  459. return;
  460. }
  461. next = this->next;
  462. if(prev == nil)
  463. setbulkhd(ctlr, next);
  464. else
  465. edlinked(prev, next);
  466. ctlr->ohci->control |= Cble;
  467. edlinked(ed, nil); /* wipe out next field */
  468. }
  469. static void
  470. edsetaddr(Ed *ed, uint32_t addr)
  471. {
  472. uint32_t ctrl;
  473. ctrl = ed->ctrl & ~((Epmax<<7)|Devmax);
  474. ctrl |= (addr & ((Epmax<<7)|Devmax));
  475. ed->ctrl = ctrl;
  476. }
  477. /*
  478. static void
  479. edsettog(Ed *ed, int c)
  480. {
  481. if(c != 0)
  482. ed->head |= Edtoggle;
  483. else
  484. ed->head &= ~Edtoggle;
  485. }
  486. */
  487. static int
  488. edtoggle(Ed *ed)
  489. {
  490. return ed->head & Edtoggle;
  491. }
  492. static int
  493. edhalted(Ed *ed)
  494. {
  495. return ed->head & Edhalt;
  496. }
  497. static int
  498. edmaxpkt(Ed *ed)
  499. {
  500. return (ed->ctrl >> Edmpsshift) & Edmpsmask;
  501. }
  502. static void
  503. edsetmaxpkt(Ed *ed, int m)
  504. {
  505. uint32_t c;
  506. c = ed->ctrl & ~(Edmpsmask << Edmpsshift);
  507. ed->ctrl = c | ((m&Edmpsmask) << Edmpsshift);
  508. }
  509. static int
  510. tderrs(Td *td)
  511. {
  512. return (td->ctrl >> Tdccshift) & Tdccmask;
  513. }
  514. static int
  515. tdtok(Td *td)
  516. {
  517. return (td->ctrl & Tdtokmask);
  518. }
  519. static Td*
  520. tdalloc(void)
  521. {
  522. Td *td;
  523. Td *pool;
  524. int i, sz;
  525. sz = ROUNDUP(sizeof *td, 16);
  526. lock(&tdpool);
  527. if(tdpool.free == nil){
  528. ddprint("ohci: tdalloc %d Tds\n", Incr);
  529. pool = mallocalign(Incr*sz, Align, 0, 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. tdpool.ninuse++;
  540. tdpool.nfree--;
  541. td = tdpool.free;
  542. tdpool.free = td->next;
  543. memset(td, 0, sizeof(Td));
  544. unlock(&tdpool);
  545. assert(((uintptr)td & 0xF) == 0);
  546. return td;
  547. }
  548. static void
  549. tdfree(Td *td)
  550. {
  551. if(td == 0)
  552. return;
  553. freeb(td->bp);
  554. td->bp = nil;
  555. lock(&tdpool);
  556. if(td->nexttd == 0x77777777)
  557. panic("ohci: tdfree: double free");
  558. memset(td, 7, sizeof(Td)); /* poison */
  559. td->next = tdpool.free;
  560. tdpool.free = td;
  561. tdpool.ninuse--;
  562. tdpool.nfree++;
  563. unlock(&tdpool);
  564. }
  565. static Ed*
  566. edalloc(void)
  567. {
  568. Ed *ed, *pool;
  569. int i, sz;
  570. sz = ROUNDUP(sizeof *ed, 16);
  571. lock(&edpool);
  572. if(edpool.free == nil){
  573. ddprint("ohci: edalloc %d Eds\n", Incr);
  574. pool = mallocalign(Incr*sz, Align, 0, 0);
  575. if(pool == nil)
  576. panic("edalloc");
  577. for(i=Incr; --i>=0;){
  578. pool[i].next = edpool.free;
  579. edpool.free = &pool[i];
  580. }
  581. edpool.nalloc += Incr;
  582. edpool.nfree += Incr;
  583. }
  584. edpool.ninuse++;
  585. edpool.nfree--;
  586. ed = edpool.free;
  587. edpool.free = ed->next;
  588. memset(ed, 0, sizeof(Ed));
  589. unlock(&edpool);
  590. return ed;
  591. }
  592. static void
  593. edfree(Ed *ed)
  594. {
  595. Td *td, *next;
  596. int i;
  597. if(ed == 0)
  598. return;
  599. i = 0;
  600. for(td = ed->tds; td != nil; td = next){
  601. next = td->next;
  602. tdfree(td);
  603. if(i++ > 2000){
  604. print("ohci: bug: ed with more than 2000 tds\n");
  605. break;
  606. }
  607. }
  608. lock(&edpool);
  609. if(ed->nexted == 0x99999999)
  610. panic("ohci: edfree: double free");
  611. memset(ed, 9, sizeof(Ed)); /* poison */
  612. ed->next = edpool.free;
  613. edpool.free = ed;
  614. edpool.ninuse--;
  615. edpool.nfree++;
  616. unlock(&edpool);
  617. ddprint("edfree: ed %#p\n", ed);
  618. }
  619. /*
  620. * return smallest power of 2 >= n
  621. */
  622. static int
  623. flog2(int n)
  624. {
  625. int i;
  626. for(i = 0; (1 << i) < n; i++)
  627. ;
  628. return i;
  629. }
  630. /*
  631. * return smallest power of 2 <= n
  632. */
  633. static int
  634. flog2lower(int n)
  635. {
  636. int i;
  637. for(i = 0; (1 << (i + 1)) <= n; i++)
  638. ;
  639. return i;
  640. }
  641. static int
  642. pickschedq(Qtree *qt, int pollival, uint32_t bw, uint32_t limit)
  643. {
  644. int i, j, d, upperb, q;
  645. uint32_t best, worst, total;
  646. d = flog2lower(pollival);
  647. if(d > qt->depth)
  648. d = qt->depth;
  649. q = -1;
  650. worst = 0;
  651. best = ~0;
  652. upperb = (1 << (d+1)) - 1;
  653. for(i = (1 << d) - 1; i < upperb; i++){
  654. total = qt->bw[0];
  655. for(j = i; j > 0; j = (j - 1) / 2)
  656. total += qt->bw[j];
  657. if(total < best){
  658. best = total;
  659. q = i;
  660. }
  661. if(total > worst)
  662. worst = total;
  663. }
  664. if(worst + bw >= limit)
  665. return -1;
  666. return q;
  667. }
  668. static int
  669. schedq(Ctlr *ctlr, Qio *io, int pollival)
  670. {
  671. int q;
  672. Ed *ted;
  673. q = pickschedq(ctlr->tree, pollival, io->bw, ~0);
  674. ddqprint("ohci: sched %#p q %d, ival %d, bw %ld\n", io, q, pollival, io->bw);
  675. if(q < 0){
  676. print("ohci: no room for ed\n");
  677. return -1;
  678. }
  679. ctlr->tree->bw[q] += io->bw;
  680. ted = ctlr->tree->root[q];
  681. io->sched = q;
  682. edlinked(io->ed, ted->next);
  683. edlinked(ted, io->ed);
  684. io->ed->inext = ctlr->intrhd;
  685. ctlr->intrhd = io->ed;
  686. return 0;
  687. }
  688. static void
  689. unschedq(Ctlr *ctlr, Qio *qio)
  690. {
  691. int q;
  692. Ed *prev, *this, *next;
  693. Ed **l;
  694. q = qio->sched;
  695. if(q < 0)
  696. return;
  697. ctlr->tree->bw[q] -= qio->bw;
  698. prev = ctlr->tree->root[q];
  699. this = prev->next;
  700. while(this != nil && this != qio->ed){
  701. prev = this;
  702. this = this->next;
  703. }
  704. if(this == nil)
  705. print("ohci: unschedq %d: not found\n", q);
  706. else{
  707. next = this->next;
  708. edlinked(prev, next);
  709. }
  710. waitSOF(ctlr);
  711. for(l = &ctlr->intrhd; *l != nil; l = &(*l)->inext)
  712. if(*l == qio->ed){
  713. *l = (*l)->inext;
  714. return;
  715. }
  716. print("ohci: unschedq: ed %#p not found\n", qio->ed);
  717. }
  718. static char*
  719. seprinttdtok(char *s, char *e, int tok)
  720. {
  721. switch(tok){
  722. case Tdtoksetup:
  723. s = seprint(s, e, " setup");
  724. break;
  725. case Tdtokin:
  726. s = seprint(s, e, " in");
  727. break;
  728. case Tdtokout:
  729. s = seprint(s, e, " out");
  730. break;
  731. }
  732. return s;
  733. }
  734. static char*
  735. seprinttd(char *s, char *e, Td *td, int iso)
  736. {
  737. int i;
  738. Block *bp;
  739. if(td == nil)
  740. return seprint(s, e, "<nil td>\n");
  741. s = seprint(s, e, "%#p ep %#p ctrl %#p", td, td->ep, td->ctrl);
  742. s = seprint(s, e, " cc=%#ulx", (td->ctrl >> Tdccshift) & Tdccmask);
  743. if(iso == 0){
  744. if((td->ctrl & Tdround) != 0)
  745. s = seprint(s, e, " rnd");
  746. s = seprinttdtok(s, e, td->ctrl & Tdtokmask);
  747. if((td->ctrl & Tdusetog) != 0)
  748. s = seprint(s, e, " d%d", (td->ctrl & Tddata1) ? 1 : 0);
  749. else
  750. s = seprint(s, e, " d-");
  751. s = seprint(s, e, " ec=%uld", (td->ctrl >> Tderrshift) & Tderrmask);
  752. }else{
  753. s = seprint(s, e, " fc=%uld", (td->ctrl >> Tdfcshift) & Tdfcmask);
  754. s = seprint(s, e, " sf=%uld", td->ctrl & Tdsfmask);
  755. }
  756. s = seprint(s, e, " cbp0 %#p cbp %#p next %#p be %#p %s",
  757. td->cbp0, td->cbp, td->nexttd, td->be, td->last ? "last" : "");
  758. s = seprint(s, e, "\n\t\t%ld bytes", td->nbytes);
  759. if((bp = td->bp) != nil){
  760. s = seprint(s, e, " rp %#p wp %#p ", bp->rp, bp->wp);
  761. if(BLEN(bp) > 0)
  762. s = seprintdata(s, e, bp->rp, bp->wp - bp->rp);
  763. }
  764. if(iso == 0)
  765. return seprint(s, e, "\n");
  766. s = seprint(s, e, "\n\t\t");
  767. /* we use only offsets[0] */
  768. i = 0;
  769. s = seprint(s, e, "[%d] %#ux cc=%#ux sz=%ud\n", i, td->offsets[i],
  770. (td->offsets[i] >> Tdiccshift) & Tdiccmask,
  771. td->offsets[i] & 0x7FF);
  772. return s;
  773. }
  774. static void
  775. dumptd(Td *td, char *p, int iso)
  776. {
  777. static char buf[512]; /* Too much */
  778. char *s;
  779. s = seprint(buf, buf+sizeof(buf), "%s: ", p);
  780. s = seprinttd(s, buf+sizeof(buf), td, iso);
  781. if(s > buf && s[-1] != '\n')
  782. s[-1] = '\n';
  783. print("\t%s", buf);
  784. }
  785. static void
  786. dumptds(Td *td, char *p, int iso)
  787. {
  788. int i;
  789. for(i = 0; td != nil; td = td->next){
  790. dumptd(td, p, iso);
  791. if(td->last)
  792. break;
  793. if(tdtok(td) == Tdtokin && ++i > 2){
  794. print("\t\t...\n");
  795. break;
  796. }
  797. }
  798. }
  799. static void
  800. dumped(Ed *ed)
  801. {
  802. char *buf, *s, *e;
  803. if(ed == nil){
  804. print("<null ed>\n");
  805. return;
  806. }
  807. buf = malloc(512);
  808. /* no waserror; may want to use from interrupt context */
  809. if(buf == nil)
  810. return;
  811. e = buf+512;
  812. s = seprint(buf, e, "\ted %#p: ctrl %#p", ed, ed->ctrl);
  813. if((ed->ctrl & Edskip) != 0)
  814. s = seprint(s, e, " skip");
  815. if((ed->ctrl & Ediso) != 0)
  816. s = seprint(s, e, " iso");
  817. if((ed->ctrl & Edlow) != 0)
  818. s = seprint(s, e, " low");
  819. s = seprint(s, e, " d%d", (ed->head & Edtoggle) ? 1 : 0);
  820. if((ed->ctrl & Eddirmask) == Edin)
  821. s = seprint(s, e, " in");
  822. if((ed->ctrl & Eddirmask) == Edout)
  823. s = seprint(s, e, " out");
  824. if(edhalted(ed))
  825. s = seprint(s, e, " hlt");
  826. s = seprint(s, e, " ep%uld.%uld", (ed->ctrl>>7)&Epmax, ed->ctrl&0x7f);
  827. s = seprint(s, e, " maxpkt %uld", (ed->ctrl>>Edmpsshift)&Edmpsmask);
  828. seprint(s, e, " tail %#p head %#p next %#p\n",ed->tail,ed->head,ed->nexted);
  829. print("%s", buf);
  830. free(buf);
  831. if(ed->tds != nil && (ed->ctrl & Ediso) == 0)
  832. dumptds(ed->tds, "td", 0);
  833. }
  834. static char*
  835. seprintio(char *s, char *e, Qio *io, char *pref)
  836. {
  837. s = seprint(s, e, "%s qio %#p ed %#p", pref, io, io->ed);
  838. s = seprint(s, e, " tog %d iot %ld err %s id %#ulx",
  839. io->toggle, io->iotime, io->err, io->usbid);
  840. s = seprinttdtok(s, e, io->tok);
  841. s = seprint(s, e, " %s\n", iosname[io->state]);
  842. return s;
  843. }
  844. static char*
  845. seprintep(char* s, char* e, Ep *ep)
  846. {
  847. Isoio *iso;
  848. Qio *io;
  849. Ctlio *cio;
  850. if(ep == nil)
  851. return seprint(s, e, "<nil ep>\n");
  852. if(ep->aux == nil)
  853. return seprint(s, e, "no mdep\n");
  854. switch(ep->ttype){
  855. case Tctl:
  856. cio = ep->aux;
  857. s = seprintio(s, e, cio, "c");
  858. s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
  859. break;
  860. case Tbulk:
  861. case Tintr:
  862. io = ep->aux;
  863. if(ep->mode != OWRITE)
  864. s = seprintio(s, e, &io[OREAD], "r");
  865. if(ep->mode != OREAD)
  866. s = seprintio(s, e, &io[OWRITE], "w");
  867. break;
  868. case Tiso:
  869. iso = ep->aux;
  870. s = seprintio(s, e, iso, "w");
  871. s = seprint(s, e, "\tntds %d avail %d frno %uld left %uld next avail %#p\n",
  872. iso->nframes, iso->navail, iso->frno, iso->left, iso->atds);
  873. break;
  874. }
  875. return s;
  876. }
  877. static char*
  878. seprintctl(char *s, char *se, uint32_t ctl)
  879. {
  880. s = seprint(s, se, "en=");
  881. if((ctl&Cple) != 0)
  882. s = seprint(s, se, "p");
  883. if((ctl&Cie) != 0)
  884. s = seprint(s, se, "i");
  885. if((ctl&Ccle) != 0)
  886. s = seprint(s, se, "c");
  887. if((ctl&Cble) != 0)
  888. s = seprint(s, se, "b");
  889. switch(ctl & Cfsmask){
  890. case Cfsreset:
  891. return seprint(s, se, " reset");
  892. case Cfsresume:
  893. return seprint(s, se, " resume");
  894. case Cfsoper:
  895. return seprint(s, se, " run");
  896. case Cfssuspend:
  897. return seprint(s, se, " suspend");
  898. default:
  899. return seprint(s, se, " ???");
  900. }
  901. }
  902. static void
  903. dump(Hci *hp)
  904. {
  905. Ctlr *ctlr;
  906. Ed *ed;
  907. char cs[20];
  908. ctlr = hp->aux;
  909. ilock(ctlr);
  910. seprintctl(cs, cs+sizeof(cs), ctlr->ohci->control);
  911. print("ohci ctlr %#p: frno %#ux ctl %#lux %s sts %#lux intr %#lux\n",
  912. ctlr, ctlr->hcca->framenumber, ctlr->ohci->control, cs,
  913. ctlr->ohci->cmdsts, ctlr->ohci->intrsts);
  914. print("ctlhd %#ulx cur %#ulx bulkhd %#ulx cur %#ulx done %#ulx\n",
  915. ctlr->ohci->ctlheaded, ctlr->ohci->ctlcurred,
  916. ctlr->ohci->bulkheaded, ctlr->ohci->bulkcurred,
  917. ctlr->ohci->donehead);
  918. if(ctlhd(ctlr) != nil)
  919. print("[ctl]\n");
  920. for(ed = ctlhd(ctlr); ed != nil; ed = ed->next)
  921. dumped(ed);
  922. if(bulkhd(ctlr) != nil)
  923. print("[bulk]\n");
  924. for(ed = bulkhd(ctlr); ed != nil; ed = ed->next)
  925. dumped(ed);
  926. if(ctlr->intrhd != nil)
  927. print("[intr]\n");
  928. for(ed = ctlr->intrhd; ed != nil; ed = ed->inext)
  929. dumped(ed);
  930. if(ctlr->tree->root[0]->next != nil)
  931. print("[iso]");
  932. for(ed = ctlr->tree->root[0]->next; ed != nil; ed = ed->next)
  933. dumped(ed);
  934. print("%d eds in tree\n", ctlr->ntree);
  935. iunlock(ctlr);
  936. lock(&tdpool);
  937. print("%d tds allocated = %d in use + %d free\n",
  938. tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
  939. unlock(&tdpool);
  940. lock(&edpool);
  941. print("%d eds allocated = %d in use + %d free\n",
  942. edpool.nalloc, edpool.ninuse, edpool.nfree);
  943. unlock(&edpool);
  944. }
  945. /*
  946. * Compute size for the next iso Td and setup its
  947. * descriptor for I/O according to the buffer size.
  948. */
  949. static void
  950. isodtdinit(Ep *ep, Isoio *iso, Td *td)
  951. {
  952. Block *bp;
  953. int32_t size;
  954. int i;
  955. bp = td->bp;
  956. assert(bp != nil && BLEN(bp) == 0);
  957. size = (ep->hz+iso->left) * ep->pollival / 1000;
  958. iso->left = (ep->hz+iso->left) * ep->pollival % 1000;
  959. size *= ep->samplesz;
  960. if(size > ep->maxpkt){
  961. print("ohci: ep%d.%d: size > maxpkt\n",
  962. ep->dev->nb, ep->nb);
  963. print("size = %uld max = %ld\n", size, ep->maxpkt);
  964. size = ep->maxpkt;
  965. }
  966. td->nbytes = size;
  967. memset(bp->wp, 0, size); /* in case we don't fill it on time */
  968. td->cbp0 = td->cbp = ptr2pa(bp->rp) & ~0xFFF;
  969. td->ctrl = TRUNC(iso->frno, Ntdframes);
  970. td->offsets[0] = (ptr2pa(bp->rp) & 0xFFF);
  971. td->offsets[0] |= (Tdnotacc << Tdiccshift);
  972. /* in case the controller checks out the offests... */
  973. for(i = 1; i < nelem(td->offsets); i++)
  974. td->offsets[i] = td->offsets[0];
  975. td->be = ptr2pa(bp->rp + size - 1);
  976. td->ctrl |= (0 << Tdfcshift); /* frame count is 1 */
  977. iso->frno = TRUNC(iso->frno + ep->pollival, Ntdframes);
  978. }
  979. /*
  980. * start I/O on the dummy td and setup a new dummy to fill up.
  981. */
  982. static void
  983. isoadvance(Ep *ep, Isoio *iso, Td *td)
  984. {
  985. Td *dtd;
  986. dtd = iso->atds;
  987. iso->atds = dtd->anext;
  988. iso->navail--;
  989. dtd->anext = nil;
  990. dtd->bp->wp = dtd->bp->rp;
  991. dtd->nexttd = 0;
  992. td->nexttd = ptr2pa(dtd);
  993. isodtdinit(ep, iso, dtd);
  994. iso->ed->tail = ptr2pa(dtd);
  995. }
  996. static int
  997. isocanwrite(void *a)
  998. {
  999. Isoio *iso;
  1000. iso = a;
  1001. return iso->state == Qclose || iso->err != nil ||
  1002. iso->navail > iso->nframes / 2;
  1003. }
  1004. /*
  1005. * Service a completed/failed Td from the done queue.
  1006. * It may be of any transfer type.
  1007. * The queue is not in completion order.
  1008. * (It's actually in reverse completion order).
  1009. *
  1010. * When an error, a short packet, or a last Td is found
  1011. * we awake the process waiting for the transfer.
  1012. * Although later we will process other Tds completed
  1013. * before, epio won't be able to touch the current Td
  1014. * until interrupt returns and releases the lock on the
  1015. * controller.
  1016. */
  1017. static void
  1018. qhinterrupt(Ctlr *ctrl, Ep *ep, Qio *io, Td *td, int n)
  1019. {
  1020. Block *bp;
  1021. int mode, err;
  1022. Ed *ed;
  1023. ed = io->ed;
  1024. if(io->state != Qrun)
  1025. return;
  1026. if(tdtok(td) == Tdtokin)
  1027. mode = OREAD;
  1028. else
  1029. mode = OWRITE;
  1030. bp = td->bp;
  1031. err = tderrs(td);
  1032. switch(err){
  1033. case Tddataovr: /* Overrun is not an error */
  1034. break;
  1035. case Tdok:
  1036. /* virtualbox doesn't always report underflow on short packets */
  1037. if(td->cbp == 0)
  1038. break;
  1039. /* fall through */
  1040. case Tddataund:
  1041. /* short input packets are ok */
  1042. if(mode == OREAD){
  1043. if(td->cbp == 0)
  1044. panic("ohci: short packet but cbp == 0");
  1045. /*
  1046. * td->cbp and td->cbp0 are the real addresses
  1047. * corresponding to virtual addresses bp->wp and
  1048. * bp->rp respectively.
  1049. */
  1050. bp->wp = bp->rp + (td->cbp - td->cbp0);
  1051. if(bp->wp < bp->rp)
  1052. panic("ohci: wp < rp");
  1053. /*
  1054. * It's ok. clear error and flag as last in xfer.
  1055. * epio must ignore following Tds.
  1056. */
  1057. td->last = 1;
  1058. td->ctrl &= ~(Tdccmask << Tdccshift);
  1059. break;
  1060. }
  1061. /* else fall; it's an error */
  1062. case Tdcrc:
  1063. case Tdbitstuff:
  1064. case Tdbadtog:
  1065. case Tdstalled:
  1066. case Tdtmout:
  1067. case Tdpidchk:
  1068. case Tdbadpid:
  1069. bp->wp = bp->rp; /* no bytes in xfer. */
  1070. io->err = errmsg(err);
  1071. if(debug || ep->debug){
  1072. print("tdinterrupt: failed err %d (%s)\n", err, io->err);
  1073. dumptd(td, "failed", ed->ctrl & Ediso);
  1074. }
  1075. td->last = 1;
  1076. break;
  1077. default:
  1078. panic("ohci: td cc %ud unknown", err);
  1079. }
  1080. if(td->last != 0){
  1081. /*
  1082. * clear td list and halt flag.
  1083. */
  1084. ed->head = (ed->head & Edtoggle) | ed->tail;
  1085. ed->tds = pa2ptr(ed->tail);
  1086. io->state = Qdone;
  1087. wakeup(io);
  1088. }
  1089. }
  1090. /*
  1091. * BUG: Iso input streams are not implemented.
  1092. */
  1093. static void
  1094. isointerrupt(Ctlr *ctlr, Ep *ep, Qio *io, Td *td, int n)
  1095. {
  1096. Isoio *iso;
  1097. Block *bp;
  1098. Ed *ed;
  1099. int err, isoerr;
  1100. iso = ep->aux;
  1101. ed = io->ed;
  1102. if(io->state == Qclose)
  1103. return;
  1104. bp = td->bp;
  1105. /*
  1106. * When we get more than half the frames consecutive errors
  1107. * we signal an actual error. Errors in the entire Td are
  1108. * more serious and are always singaled.
  1109. * Errors like overrun are not really errors. In fact, for
  1110. * output, errors cannot be really detected. The driver will
  1111. * hopefully notice I/O errors on input endpoints and detach the device.
  1112. */
  1113. err = tderrs(td);
  1114. isoerr = (td->offsets[0] >> Tdiccshift) & Tdiccmask;
  1115. if(isoerr == Tdok || isoerr == Tdnotacc)
  1116. iso->nerrs = 0;
  1117. else if(iso->nerrs++ > iso->nframes/2)
  1118. err = Tdstalled;
  1119. if(err != Tdok && err != Tddataovr){
  1120. bp->wp = bp->rp;
  1121. io->err = errmsg(err);
  1122. if(debug || ep->debug){
  1123. print("ohci: isointerrupt: ep%d.%d: err %d (%s) frnum 0x%lux\n",
  1124. ep->dev->nb, ep->nb,
  1125. err, errmsg(err), ctlr->ohci->fmnumber);
  1126. dumptd(td, "failed", ed->ctrl & Ediso);
  1127. }
  1128. }
  1129. td->bp->wp = td->bp->rp;
  1130. td->nbytes = 0;
  1131. td->anext = iso->atds;
  1132. iso->atds = td;
  1133. iso->navail++;
  1134. /*
  1135. * If almost all Tds are avail the user is not doing I/O at the
  1136. * required rate. We put another Td in place to keep the polling rate.
  1137. */
  1138. if(iso->err == nil && iso->navail > iso->nframes - 10)
  1139. isoadvance(ep, iso, pa2ptr(iso->ed->tail));
  1140. /*
  1141. * If there's enough buffering futher I/O can be done.
  1142. */
  1143. if(isocanwrite(iso))
  1144. wakeup(iso);
  1145. }
  1146. static void
  1147. interrupt(Ureg *ureg, void *arg)
  1148. {
  1149. Td *td, *ntd;
  1150. Hci *hp;
  1151. Ctlr *ctlr;
  1152. uint32_t status, curred;
  1153. int i, frno;
  1154. hp = arg;
  1155. ctlr = hp->aux;
  1156. ilock(ctlr);
  1157. ctlr->ohci->intrdisable = Mie;
  1158. coherence();
  1159. status = ctlr->ohci->intrsts & ctlr->ohci->intrenable;
  1160. status &= Oc|Rhsc|Fno|Ue|Rd|Sf|Wdh|So;
  1161. frno = TRUNC(ctlr->ohci->fmnumber, Ntdframes);
  1162. if(status & Wdh){
  1163. /* lsb of donehead has bit to flag other intrs. */
  1164. td = pa2ptr(ctlr->hcca->donehead & ~0xF);
  1165. for(i = 0; td != nil && i < 1024; i++){
  1166. if(0)ddprint("ohci tdinterrupt: td %#p\n", td);
  1167. ntd = pa2ptr(td->nexttd & ~0xF);
  1168. td->nexttd = 0;
  1169. if(td->ep == nil || td->io == nil)
  1170. panic("ohci: interrupt: ep %#p io %#p",
  1171. td->ep, td->io);
  1172. ohciinterrupts[td->ep->ttype]++;
  1173. if(td->ep->ttype == Tiso)
  1174. isointerrupt(ctlr, td->ep, td->io, td, frno);
  1175. else
  1176. qhinterrupt(ctlr, td->ep, td->io, td, frno);
  1177. td = ntd;
  1178. }
  1179. if(i >= 1024)
  1180. print("ohci: bug: more than 1024 done Tds?\n");
  1181. ctlr->hcca->donehead = 0;
  1182. }
  1183. ctlr->ohci->intrsts = status;
  1184. status &= ~Wdh;
  1185. status &= ~Sf;
  1186. if(status & So){
  1187. print("ohci: sched overrun: too much load\n");
  1188. ctlr->overrun++;
  1189. status &= ~So;
  1190. }
  1191. if((status & Ue) != 0){
  1192. curred = ctlr->ohci->periodcurred;
  1193. print("ohci: unrecoverable error frame 0x%.8lux ed 0x%.8lux, "
  1194. "ints %d %d %d %d\n",
  1195. ctlr->ohci->fmnumber, curred,
  1196. ohciinterrupts[Tctl], ohciinterrupts[Tintr],
  1197. ohciinterrupts[Tbulk], ohciinterrupts[Tiso]);
  1198. if(curred != 0)
  1199. dumped(pa2ptr(curred));
  1200. status &= ~Ue;
  1201. }
  1202. if(status != 0)
  1203. print("ohci interrupt: unhandled sts 0x%.8lux\n", status);
  1204. ctlr->ohci->intrenable = Mie | Wdh | Ue;
  1205. iunlock(ctlr);
  1206. }
  1207. /*
  1208. * The old dummy Td is used to implement the new Td.
  1209. * A new dummy is linked at the end of the old one and
  1210. * returned, to link further Tds if needed.
  1211. */
  1212. static Td*
  1213. epgettd(Ep *ep, Qio *io, Td **dtdp, int flags, void *a, int count)
  1214. {
  1215. Td *td, *dtd;
  1216. Block *bp;
  1217. if(count <= BIGPGSZ)
  1218. bp = allocb(count);
  1219. else{
  1220. if(count > 2*BIGPGSZ)
  1221. panic("ohci: transfer > two pages");
  1222. /* maximum of one physical page crossing allowed */
  1223. bp = allocb(count+BIGPGSZ);
  1224. bp->rp = (unsigned char*)BIGPGROUND((uintptr)bp->rp);
  1225. bp->wp = bp->rp;
  1226. }
  1227. dtd = *dtdp;
  1228. td = dtd;
  1229. td->bp = bp;
  1230. if(count > 0){
  1231. td->cbp0 = td->cbp = ptr2pa(bp->wp);
  1232. td->be = ptr2pa(bp->wp + count - 1);
  1233. if(a != nil){
  1234. /* validaddr((uintptr)a, count, 0); DEBUG */
  1235. memmove(bp->wp, a, count);
  1236. }
  1237. bp->wp += count;
  1238. }
  1239. td->nbytes = count;
  1240. td->ctrl = io->tok|Tdusetog|io->toggle|flags;
  1241. if(io->toggle == Tddata0)
  1242. io->toggle = Tddata1;
  1243. else
  1244. io->toggle = Tddata0;
  1245. assert(td->ep == ep);
  1246. td->io = io;
  1247. dtd = tdalloc(); /* new dummy */
  1248. dtd->ep = ep;
  1249. td->nexttd = ptr2pa(dtd);
  1250. td->next = dtd;
  1251. *dtdp = dtd;
  1252. return td;
  1253. }
  1254. /*
  1255. * Try to get them idle
  1256. */
  1257. static void
  1258. aborttds(Qio *io)
  1259. {
  1260. Ed *ed;
  1261. Td *td;
  1262. ed = io->ed;
  1263. if(ed == nil)
  1264. return;
  1265. ed->ctrl |= Edskip;
  1266. for(td = ed->tds; td != nil; td = td->next)
  1267. if(td->bp != nil)
  1268. td->bp->wp = td->bp->rp;
  1269. ed->head = (ed->head&0xF) | ed->tail;
  1270. if((ed->ctrl & Ediso) == 0)
  1271. ed->tds = pa2ptr(ed->tail);
  1272. }
  1273. static int
  1274. epiodone(void *a)
  1275. {
  1276. Qio *io;
  1277. io = a;
  1278. return io->state != Qrun;
  1279. }
  1280. static void
  1281. epiowait(Ctlr *ctlr, Qio *io, int tmout, uint32_t n)
  1282. {
  1283. Proc *up = externup();
  1284. Ed *ed;
  1285. int timedout;
  1286. ed = io->ed;
  1287. if(0)ddqprint("ohci io %#p sleep on ed %#p state %s\n",
  1288. io, ed, iosname[io->state]);
  1289. timedout = 0;
  1290. if(waserror()){
  1291. dqprint("ohci io %#p ed %#p timed out\n", io, ed);
  1292. timedout++;
  1293. }else{
  1294. if(tmout == 0)
  1295. sleep(io, epiodone, io);
  1296. else
  1297. tsleep(io, epiodone, io, tmout);
  1298. poperror();
  1299. }
  1300. ilock(ctlr);
  1301. if(io->state == Qrun)
  1302. timedout = 1;
  1303. else if(io->state != Qdone && io->state != Qclose)
  1304. panic("epio: ed not done and not closed");
  1305. if(timedout){
  1306. aborttds(io);
  1307. io->err = "request timed out";
  1308. iunlock(ctlr);
  1309. if(!waserror()){
  1310. tsleep(&up->sleep, return0, 0, Abortdelay);
  1311. poperror();
  1312. }
  1313. ilock(ctlr);
  1314. }
  1315. if(io->state != Qclose)
  1316. io->state = Qidle;
  1317. iunlock(ctlr);
  1318. }
  1319. /*
  1320. * Non iso I/O.
  1321. * To make it work for control transfers, the caller may
  1322. * lock the Qio for the entire control transfer.
  1323. */
  1324. static int32_t
  1325. epio(Ep *ep, Qio *io, void *a, int32_t count, int mustlock)
  1326. {
  1327. Proc *up = externup();
  1328. Ed *ed;
  1329. Ctlr *ctlr;
  1330. char buf[80];
  1331. char *err;
  1332. unsigned char *c;
  1333. Td *td, *ltd, *ntd, *td0;
  1334. int last, ntds, tmout;
  1335. int32_t tot, n;
  1336. uint32_t load;
  1337. ed = io->ed;
  1338. ctlr = ep->hp->aux;
  1339. io->debug = ep->debug;
  1340. tmout = ep->tmout;
  1341. ddeprint("ohci: %s ep%d.%d io %#p count %ld\n",
  1342. io->tok == Tdtokin ? "in" : "out",
  1343. ep->dev->nb, ep->nb, io, count);
  1344. if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
  1345. seprintdata(buf, buf+sizeof(buf), a, count);
  1346. print("\t%s\n", buf);
  1347. }
  1348. if(mustlock){
  1349. qlock(io);
  1350. if(waserror()){
  1351. qunlock(io);
  1352. nexterror();
  1353. }
  1354. }
  1355. io->err = nil;
  1356. ilock(ctlr);
  1357. if(io->state == Qclose){ /* Tds released by cancelio */
  1358. iunlock(ctlr);
  1359. error(io->err ? io->err : Eio);
  1360. }
  1361. if(io->state != Qidle)
  1362. panic("epio: qio not idle");
  1363. io->state = Qinstall;
  1364. c = a;
  1365. ltd = td0 = ed->tds;
  1366. load = tot = 0;
  1367. do{
  1368. n = 2*BIGPGSZ;
  1369. if(count-tot < n)
  1370. n = count-tot;
  1371. if(c != nil && io->tok != Tdtokin)
  1372. td = epgettd(ep, io, &ltd, 0, c+tot, n);
  1373. else
  1374. td = epgettd(ep, io, &ltd, 0, nil, n);
  1375. tot += n;
  1376. load += ep->load;
  1377. }while(tot < count);
  1378. if(td0 == nil || ltd == nil || td0 == ltd)
  1379. panic("epio: no td");
  1380. td->last = 1;
  1381. if(debug > 2 || ep->debug > 2)
  1382. dumptds(td0, "put td", ep->ttype == Tiso);
  1383. iunlock(ctlr);
  1384. ilock(ctlr);
  1385. if(io->state != Qclose){
  1386. io->iotime = TK2MS(machp()->ticks);
  1387. io->state = Qrun;
  1388. ed->tail = ptr2pa(ltd);
  1389. if(ep->ttype == Tctl)
  1390. ctlr->ohci->cmdsts |= Sclf;
  1391. else if(ep->ttype == Tbulk)
  1392. ctlr->ohci->cmdsts |= Sblf;
  1393. }
  1394. iunlock(ctlr);
  1395. epiowait(ctlr, io, tmout, load);
  1396. ilock(ctlr);
  1397. if(debug > 1 || ep->debug > 1)
  1398. dumptds(td0, "got td", 0);
  1399. iunlock(ctlr);
  1400. tot = 0;
  1401. c = a;
  1402. ntds = last = 0;
  1403. for(td = td0; td != ltd; td = ntd){
  1404. ntds++;
  1405. /*
  1406. * If the Td is flagged as last we must
  1407. * ignore any following Td. The block may
  1408. * seem to have bytes but interrupt has not seen
  1409. * those Tds through the done queue, and they are void.
  1410. */
  1411. if(last == 0 && tderrs(td) == Tdok){
  1412. n = BLEN(td->bp);
  1413. tot += n;
  1414. if(c != nil && tdtok(td) == Tdtokin && n > 0){
  1415. memmove(c, td->bp->rp, n);
  1416. c += n;
  1417. }
  1418. }
  1419. last |= td->last;
  1420. ntd = td->next;
  1421. tdfree(td);
  1422. }
  1423. if(edtoggle(ed) == 0)
  1424. io->toggle = Tddata0;
  1425. else
  1426. io->toggle = Tddata1;
  1427. err = io->err;
  1428. if(mustlock){
  1429. qunlock(io);
  1430. poperror();
  1431. }
  1432. ddeprint("ohci: io %#p: %d tds: return %ld err '%s'\n\n",
  1433. io, ntds, tot, err);
  1434. if(err != nil)
  1435. error(err);
  1436. if(tot < 0)
  1437. error(Eio);
  1438. return tot;
  1439. }
  1440. /*
  1441. * halt condition was cleared on the endpoint. update our toggles.
  1442. */
  1443. static void
  1444. clrhalt(Ep *ep)
  1445. {
  1446. Qio *io;
  1447. ep->clrhalt = 0;
  1448. switch(ep->ttype){
  1449. case Tbulk:
  1450. case Tintr:
  1451. io = ep->aux;
  1452. if(ep->mode != OREAD){
  1453. qlock(&io[OWRITE]);
  1454. io[OWRITE].toggle = Tddata0;
  1455. deprint("ep clrhalt for io %#p\n", io+OWRITE);
  1456. qunlock(&io[OWRITE]);
  1457. }
  1458. if(ep->mode != OWRITE){
  1459. qlock(&io[OREAD]);
  1460. io[OREAD].toggle = Tddata0;
  1461. deprint("ep clrhalt for io %#p\n", io+OREAD);
  1462. qunlock(&io[OREAD]);
  1463. }
  1464. break;
  1465. }
  1466. }
  1467. static int32_t
  1468. epread(Ep *ep, void *a, int32_t count)
  1469. {
  1470. Proc *up = externup();
  1471. Ctlio *cio;
  1472. Qio *io;
  1473. char buf[80];
  1474. uint32_t delta;
  1475. if(ep->aux == nil)
  1476. panic("epread: not open");
  1477. switch(ep->ttype){
  1478. case Tctl:
  1479. cio = ep->aux;
  1480. qlock(cio);
  1481. if(waserror()){
  1482. qunlock(cio);
  1483. nexterror();
  1484. }
  1485. ddeprint("epread ctl ndata %d\n", cio->ndata);
  1486. if(cio->ndata < 0)
  1487. error("request expected");
  1488. else if(cio->ndata == 0){
  1489. cio->ndata = -1;
  1490. count = 0;
  1491. }else{
  1492. if(count > cio->ndata)
  1493. count = cio->ndata;
  1494. if(count > 0)
  1495. memmove(a, cio->data, count);
  1496. /* BUG for big transfers */
  1497. free(cio->data);
  1498. cio->data = nil;
  1499. cio->ndata = 0; /* signal EOF next time */
  1500. }
  1501. qunlock(cio);
  1502. poperror();
  1503. if(debug>1 || ep->debug){
  1504. seprintdata(buf, buf+sizeof(buf), a, count);
  1505. print("epread: %s\n", buf);
  1506. }
  1507. return count;
  1508. case Tbulk:
  1509. io = ep->aux;
  1510. if(ep->clrhalt)
  1511. clrhalt(ep);
  1512. return epio(ep, &io[OREAD], a, count, 1);
  1513. case Tintr:
  1514. io = ep->aux;
  1515. delta = TK2MS(machp()->ticks) - io[OREAD].iotime + 1;
  1516. if(delta < ep->pollival / 2)
  1517. tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
  1518. if(ep->clrhalt)
  1519. clrhalt(ep);
  1520. return epio(ep, &io[OREAD], a, count, 1);
  1521. case Tiso:
  1522. panic("ohci: iso read not implemented");
  1523. break;
  1524. default:
  1525. panic("epread: bad ep ttype %d", ep->ttype);
  1526. }
  1527. return -1;
  1528. }
  1529. /*
  1530. * Control transfers are one setup write (data0)
  1531. * plus zero or more reads/writes (data1, data0, ...)
  1532. * plus a final write/read with data1 to ack.
  1533. * For both host to device and device to host we perform
  1534. * the entire transfer when the user writes the request,
  1535. * and keep any data read from the device for a later read.
  1536. * We call epio three times instead of placing all Tds at
  1537. * the same time because doing so leads to crc/tmout errors
  1538. * for some devices.
  1539. * Upon errors on the data phase we must still run the status
  1540. * phase or the device may cease responding in the future.
  1541. */
  1542. static int32_t
  1543. epctlio(Ep *ep, Ctlio *cio, void *a, int32_t count)
  1544. {
  1545. Proc *up = externup();
  1546. unsigned char *c;
  1547. int32_t len;
  1548. ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
  1549. cio, ep->dev->nb, ep->nb, count);
  1550. if(count < Rsetuplen)
  1551. error("short usb command");
  1552. qlock(cio);
  1553. free(cio->data);
  1554. cio->data = nil;
  1555. cio->ndata = 0;
  1556. if(waserror()){
  1557. qunlock(cio);
  1558. free(cio->data);
  1559. cio->data = nil;
  1560. cio->ndata = 0;
  1561. nexterror();
  1562. }
  1563. /* set the address if unset and out of configuration state */
  1564. if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
  1565. if(cio->usbid == 0){
  1566. cio->usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
  1567. edsetaddr(cio->ed, cio->usbid);
  1568. }
  1569. /* adjust maxpkt if the user has learned a different one */
  1570. if(edmaxpkt(cio->ed) != ep->maxpkt)
  1571. edsetmaxpkt(cio->ed, ep->maxpkt);
  1572. c = a;
  1573. cio->tok = Tdtoksetup;
  1574. cio->toggle = Tddata0;
  1575. if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
  1576. error(Eio);
  1577. a = c + Rsetuplen;
  1578. count -= Rsetuplen;
  1579. cio->toggle = Tddata1;
  1580. if(c[Rtype] & Rd2h){
  1581. cio->tok = Tdtokin;
  1582. len = GET2(c+Rcount);
  1583. if(len <= 0)
  1584. error("bad length in d2h request");
  1585. if(len > Maxctllen)
  1586. error("d2h data too large to fit in ohci");
  1587. a = cio->data = smalloc(len+1);
  1588. }else{
  1589. cio->tok = Tdtokout;
  1590. len = count;
  1591. }
  1592. if(len > 0)
  1593. if(waserror())
  1594. len = -1;
  1595. else{
  1596. len = epio(ep, cio, a, len, 0);
  1597. poperror();
  1598. }
  1599. if(c[Rtype] & Rd2h){
  1600. count = Rsetuplen;
  1601. cio->ndata = len;
  1602. cio->tok = Tdtokout;
  1603. }else{
  1604. if(len < 0)
  1605. count = -1;
  1606. else
  1607. count = Rsetuplen + len;
  1608. cio->tok = Tdtokin;
  1609. }
  1610. cio->toggle = Tddata1;
  1611. epio(ep, cio, nil, 0, 0);
  1612. qunlock(cio);
  1613. poperror();
  1614. ddeprint("epctlio cio %#p return %ld\n", cio, count);
  1615. return count;
  1616. }
  1617. /*
  1618. * Put new samples in the dummy Td.
  1619. * BUG: This does only a transfer per Td. We could do up to 8.
  1620. */
  1621. static int32_t
  1622. putsamples(Ctlr *ctlr, Ep *ep, Isoio *iso, unsigned char *b, int32_t count)
  1623. {
  1624. Td *td;
  1625. uint32_t n;
  1626. td = pa2ptr(iso->ed->tail);
  1627. n = count;
  1628. if(n > td->nbytes - BLEN(td->bp))
  1629. n = td->nbytes - BLEN(td->bp);
  1630. assert(td->bp->wp + n <= td->bp->lim);
  1631. memmove(td->bp->wp, b, n);
  1632. td->bp->wp += n;
  1633. if(BLEN(td->bp) == td->nbytes){ /* full Td: activate it */
  1634. ilock(ctlr);
  1635. isoadvance(ep, iso, td);
  1636. iunlock(ctlr);
  1637. }
  1638. return n;
  1639. }
  1640. static int32_t
  1641. episowrite(Ep *ep, void *a, int32_t count)
  1642. {
  1643. Proc *up = externup();
  1644. int32_t tot, nw;
  1645. char *err;
  1646. unsigned char *b;
  1647. Ctlr *ctlr;
  1648. Isoio *iso;
  1649. ctlr = ep->hp->aux;
  1650. iso = ep->aux;
  1651. iso->debug = ep->debug;
  1652. qlock(iso);
  1653. if(waserror()){
  1654. qunlock(iso);
  1655. nexterror();
  1656. }
  1657. diprint("ohci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  1658. ilock(ctlr);
  1659. if(iso->state == Qclose){
  1660. iunlock(ctlr);
  1661. error(iso->err ? iso->err : Eio);
  1662. }
  1663. iso->state = Qrun;
  1664. b = a;
  1665. for(tot = 0; tot < count; tot += nw){
  1666. while(isocanwrite(iso) == 0){
  1667. iunlock(ctlr);
  1668. diprint("ohci: episowrite: %#p sleep\n", iso);
  1669. if(waserror()){
  1670. if(iso->err == nil)
  1671. iso->err = "I/O timed out";
  1672. ilock(ctlr);
  1673. break;
  1674. }
  1675. tsleep(iso, isocanwrite, iso, ep->tmout);
  1676. poperror();
  1677. ilock(ctlr);
  1678. }
  1679. err = iso->err;
  1680. iso->err = nil;
  1681. if(iso->state == Qclose || err != nil){
  1682. iunlock(ctlr);
  1683. error(err ? err : Eio);
  1684. }
  1685. if(iso->state != Qrun)
  1686. panic("episowrite: iso not running");
  1687. iunlock(ctlr); /* We could page fault here */
  1688. nw = putsamples(ctlr, ep, iso, b+tot, count-tot);
  1689. ilock(ctlr);
  1690. }
  1691. if(iso->state != Qclose)
  1692. iso->state = Qdone;
  1693. iunlock(ctlr);
  1694. err = iso->err; /* in case it failed early */
  1695. iso->err = nil;
  1696. qunlock(iso);
  1697. poperror();
  1698. if(err != nil)
  1699. error(err);
  1700. diprint("ohci: episowrite: %#p %ld bytes\n", iso, tot);
  1701. return tot;
  1702. }
  1703. static int32_t
  1704. epwrite(Ep *ep, void *a, int32_t count)
  1705. {
  1706. Proc *up = externup();
  1707. Qio *io;
  1708. Ctlio *cio;
  1709. uint32_t delta;
  1710. unsigned char *b;
  1711. int32_t tot, nw;
  1712. if(ep->aux == nil)
  1713. panic("ohci: epwrite: not open");
  1714. switch(ep->ttype){
  1715. case Tctl:
  1716. cio = ep->aux;
  1717. return epctlio(ep, cio, a, count);
  1718. case Tbulk:
  1719. io = ep->aux;
  1720. if(ep->clrhalt)
  1721. clrhalt(ep);
  1722. /*
  1723. * Put at most Tdatomic Tds (512 bytes) at a time.
  1724. * Otherwise some devices produce babble errors.
  1725. */
  1726. b = a;
  1727. assert(a != nil);
  1728. for(tot = 0; tot < count ; tot += nw){
  1729. nw = count - tot;
  1730. if(nw > Tdatomic * ep->maxpkt)
  1731. nw = Tdatomic * ep->maxpkt;
  1732. nw = epio(ep, &io[OWRITE], b+tot, nw, 1);
  1733. }
  1734. return tot;
  1735. case Tintr:
  1736. io = ep->aux;
  1737. delta = TK2MS(machp()->ticks) - io[OWRITE].iotime + 1;
  1738. if(delta < ep->pollival)
  1739. tsleep(&up->sleep, return0, 0, ep->pollival - delta);
  1740. if(ep->clrhalt)
  1741. clrhalt(ep);
  1742. return epio(ep, &io[OWRITE], a, count, 1);
  1743. case Tiso:
  1744. return episowrite(ep, a, count);
  1745. default:
  1746. panic("ohci: epwrite: bad ep ttype %d", ep->ttype);
  1747. }
  1748. return -1;
  1749. }
  1750. static Ed*
  1751. newed(Ctlr *ctlr, Ep *ep, Qio *io, char *c)
  1752. {
  1753. Proc *up = externup();
  1754. Ed *ed;
  1755. Td *td;
  1756. ed = io->ed = edalloc(); /* no errors raised here, really */
  1757. td = tdalloc();
  1758. td->ep = ep;
  1759. td->io = io;
  1760. ed->tail = ptr2pa(td);
  1761. ed->head = ptr2pa(td);
  1762. ed->tds = td;
  1763. ed->ep = ep;
  1764. ed->ctrl = (ep->maxpkt & Edmpsmask) << Edmpsshift;
  1765. if(ep->ttype == Tiso)
  1766. ed->ctrl |= Ediso;
  1767. if(waserror()){
  1768. edfree(ed);
  1769. io->ed = nil;
  1770. nexterror();
  1771. }
  1772. /* For setup endpoints we start with the config address */
  1773. if(ep->ttype != Tctl)
  1774. edsetaddr(io->ed, io->usbid);
  1775. if(ep->dev->speed == Lowspeed)
  1776. ed->ctrl |= Edlow;
  1777. switch(io->tok){
  1778. case Tdtokin:
  1779. ed->ctrl |= Edin;
  1780. break;
  1781. case Tdtokout:
  1782. ed->ctrl |= Edout;
  1783. break;
  1784. default:
  1785. ed->ctrl |= Edtddir; /* Td will say */
  1786. break;
  1787. }
  1788. switch(ep->ttype){
  1789. case Tctl:
  1790. ilock(ctlr);
  1791. edlinked(ed, ctlhd(ctlr));
  1792. setctlhd(ctlr, ed);
  1793. iunlock(ctlr);
  1794. break;
  1795. case Tbulk:
  1796. ilock(ctlr);
  1797. edlinked(ed, bulkhd(ctlr));
  1798. setbulkhd(ctlr, ed);
  1799. iunlock(ctlr);
  1800. break;
  1801. case Tintr:
  1802. case Tiso:
  1803. ilock(ctlr);
  1804. schedq(ctlr, io, ep->pollival);
  1805. iunlock(ctlr);
  1806. break;
  1807. default:
  1808. panic("ohci: newed: bad ttype");
  1809. }
  1810. poperror();
  1811. return ed;
  1812. }
  1813. static void
  1814. isoopen(Ctlr *ctlr, Ep *ep)
  1815. {
  1816. Td *td, *edtds;
  1817. Isoio *iso;
  1818. int i;
  1819. iso = ep->aux;
  1820. iso->usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
  1821. iso->bw = ep->hz * ep->samplesz; /* bytes/sec */
  1822. if(ep->mode != OWRITE){
  1823. print("ohci: bug: iso input streams not implemented\n");
  1824. error("ohci iso input streams not implemented");
  1825. }else
  1826. iso->tok = Tdtokout;
  1827. iso->left = 0;
  1828. iso->nerrs = 0;
  1829. iso->frno = TRUNC(ctlr->ohci->fmnumber + 10, Ntdframes);
  1830. iso->nframes = 1000 / ep->pollival;
  1831. if(iso->nframes < 10){
  1832. print("ohci: isoopen: less than 10 frames; using 10.\n");
  1833. iso->nframes = 10;
  1834. }
  1835. iso->navail = iso->nframes;
  1836. iso->atds = edtds = nil;
  1837. for(i = 0; i < iso->nframes-1; i++){ /* -1 for dummy */
  1838. td = tdalloc();
  1839. td->ep = ep;
  1840. td->io = iso;
  1841. td->bp = allocb(ep->maxpkt);
  1842. td->anext = iso->atds; /* link as avail */
  1843. iso->atds = td;
  1844. td->next = edtds;
  1845. edtds = td;
  1846. }
  1847. newed(ctlr, ep, iso, "iso"); /* allocates a dummy td */
  1848. iso->ed->tds->bp = allocb(ep->maxpkt); /* but not its block */
  1849. iso->ed->tds->next = edtds;
  1850. isodtdinit(ep, iso, iso->ed->tds);
  1851. }
  1852. /*
  1853. * Allocate the endpoint and set it up for I/O
  1854. * in the controller. This must follow what's said
  1855. * in Ep regarding configuration, including perhaps
  1856. * the saved toggles (saved on a previous close of
  1857. * the endpoint data file by epclose).
  1858. */
  1859. static void
  1860. epopen(Ep *ep)
  1861. {
  1862. Proc *up = externup();
  1863. Ctlr *ctlr;
  1864. Qio *io;
  1865. Ctlio *cio;
  1866. uint32_t usbid;
  1867. ctlr = ep->hp->aux;
  1868. deprint("ohci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
  1869. if(ep->aux != nil)
  1870. panic("ohci: epopen called with open ep");
  1871. if(waserror()){
  1872. free(ep->aux);
  1873. ep->aux = nil;
  1874. nexterror();
  1875. }
  1876. switch(ep->ttype){
  1877. case Tnone:
  1878. error("endpoint not configured");
  1879. case Tiso:
  1880. ep->aux = smalloc(sizeof(Isoio));
  1881. isoopen(ctlr, ep);
  1882. break;
  1883. case Tctl:
  1884. cio = ep->aux = smalloc(sizeof(Ctlio));
  1885. cio->debug = ep->debug;
  1886. cio->ndata = -1;
  1887. cio->data = nil;
  1888. cio->tok = -1; /* invalid; Tds will say */
  1889. if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
  1890. break;
  1891. newed(ctlr, ep, cio, "epc");
  1892. break;
  1893. case Tbulk:
  1894. ep->pollival = 1; /* assume this; doesn't really matter */
  1895. /* and fall... */
  1896. case Tintr:
  1897. io = ep->aux = smalloc(sizeof(Qio)*2);
  1898. io[OREAD].debug = io[OWRITE].debug = ep->debug;
  1899. usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
  1900. if(ep->mode != OREAD){
  1901. if(ep->toggle[OWRITE] != 0)
  1902. io[OWRITE].toggle = Tddata1;
  1903. else
  1904. io[OWRITE].toggle = Tddata0;
  1905. io[OWRITE].tok = Tdtokout;
  1906. io[OWRITE].usbid = usbid;
  1907. io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  1908. newed(ctlr, ep, io+OWRITE, "epw");
  1909. }
  1910. if(ep->mode != OWRITE){
  1911. if(ep->toggle[OREAD] != 0)
  1912. io[OREAD].toggle = Tddata1;
  1913. else
  1914. io[OREAD].toggle = Tddata0;
  1915. io[OREAD].tok = Tdtokin;
  1916. io[OREAD].usbid = usbid;
  1917. io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  1918. newed(ctlr, ep, io+OREAD, "epr");
  1919. }
  1920. break;
  1921. }
  1922. deprint("ohci: epopen done:\n");
  1923. if(debug || ep->debug)
  1924. dump(ep->hp);
  1925. poperror();
  1926. }
  1927. static void
  1928. cancelio(Ep *ep, Qio *io)
  1929. {
  1930. Proc *up = externup();
  1931. Ed *ed;
  1932. Ctlr *ctlr;
  1933. ctlr = ep->hp->aux;
  1934. ilock(ctlr);
  1935. if(io == nil || io->state == Qclose){
  1936. assert(io == nil || io->ed == nil);
  1937. iunlock(ctlr);
  1938. return;
  1939. }
  1940. ed = io->ed;
  1941. io->state = Qclose;
  1942. io->err = Eio;
  1943. aborttds(io);
  1944. iunlock(ctlr);
  1945. if(!waserror()){
  1946. tsleep(&up->sleep, return0, 0, Abortdelay);
  1947. poperror();
  1948. }
  1949. wakeup(io);
  1950. qlock(io);
  1951. /* wait for epio if running */
  1952. qunlock(io);
  1953. ilock(ctlr);
  1954. switch(ep->ttype){
  1955. case Tctl:
  1956. unlinkctl(ctlr, ed);
  1957. break;
  1958. case Tbulk:
  1959. unlinkbulk(ctlr, ed);
  1960. break;
  1961. case Tintr:
  1962. case Tiso:
  1963. unschedq(ctlr, io);
  1964. break;
  1965. default:
  1966. panic("ohci cancelio: bad ttype");
  1967. }
  1968. iunlock(ctlr);
  1969. edfree(io->ed);
  1970. io->ed = nil;
  1971. }
  1972. static void
  1973. epclose(Ep *ep)
  1974. {
  1975. Ctlio *cio;
  1976. Isoio *iso;
  1977. Qio *io;
  1978. deprint("ohci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
  1979. if(ep->aux == nil)
  1980. panic("ohci: epclose called with closed ep");
  1981. switch(ep->ttype){
  1982. case Tctl:
  1983. cio = ep->aux;
  1984. cancelio(ep, cio);
  1985. free(cio->data);
  1986. cio->data = nil;
  1987. break;
  1988. case Tbulk:
  1989. case Tintr:
  1990. io = ep->aux;
  1991. if(ep->mode != OWRITE){
  1992. cancelio(ep, &io[OREAD]);
  1993. if(io[OREAD].toggle == Tddata1)
  1994. ep->toggle[OREAD] = 1;
  1995. }
  1996. if(ep->mode != OREAD){
  1997. cancelio(ep, &io[OWRITE]);
  1998. if(io[OWRITE].toggle == Tddata1)
  1999. ep->toggle[OWRITE] = 1;
  2000. }
  2001. break;
  2002. case Tiso:
  2003. iso = ep->aux;
  2004. cancelio(ep, iso);
  2005. break;
  2006. default:
  2007. panic("epclose: bad ttype %d", ep->ttype);
  2008. }
  2009. deprint("ohci: epclose ep%d.%d: done\n", ep->dev->nb, ep->nb);
  2010. free(ep->aux);
  2011. ep->aux = nil;
  2012. }
  2013. static int
  2014. portreset(Hci *hp, int port, int on)
  2015. {
  2016. Proc *up = externup();
  2017. Ctlr *ctlr;
  2018. Ohci *ohci;
  2019. if(on == 0)
  2020. return 0;
  2021. ctlr = hp->aux;
  2022. qlock(&ctlr->resetl);
  2023. if(waserror()){
  2024. qunlock(&ctlr->resetl);
  2025. nexterror();
  2026. }
  2027. ilock(ctlr);
  2028. ohci = ctlr->ohci;
  2029. ohci->rhportsts[port - 1] = Spp;
  2030. if((ohci->rhportsts[port - 1] & Ccs) == 0){
  2031. iunlock(ctlr);
  2032. error("port not connected");
  2033. }
  2034. ohci->rhportsts[port - 1] = Spr;
  2035. while((ohci->rhportsts[port - 1] & Prsc) == 0){
  2036. iunlock(ctlr);
  2037. dprint("ohci: portreset, wait for reset complete\n");
  2038. ilock(ctlr);
  2039. }
  2040. ohci->rhportsts[port - 1] = Prsc;
  2041. iunlock(ctlr);
  2042. poperror();
  2043. qunlock(&ctlr->resetl);
  2044. return 0;
  2045. }
  2046. static int
  2047. portenable(Hci *hp, int port, int on)
  2048. {
  2049. Proc *up = externup();
  2050. Ctlr *ctlr;
  2051. ctlr = hp->aux;
  2052. dprint("ohci: %#p port %d enable=%d\n", ctlr->ohci, port, on);
  2053. qlock(&ctlr->resetl);
  2054. if(waserror()){
  2055. qunlock(&ctlr->resetl);
  2056. nexterror();
  2057. }
  2058. ilock(ctlr);
  2059. if(on)
  2060. ctlr->ohci->rhportsts[port - 1] = Spe | Spp;
  2061. else
  2062. ctlr->ohci->rhportsts[port - 1] = Cpe;
  2063. iunlock(ctlr);
  2064. tsleep(&up->sleep, return0, 0, Enabledelay);
  2065. poperror();
  2066. qunlock(&ctlr->resetl);
  2067. return 0;
  2068. }
  2069. static int
  2070. portstatus(Hci *hp, int port)
  2071. {
  2072. int v;
  2073. Ctlr *ub;
  2074. uint32_t ohcistatus;
  2075. /*
  2076. * We must return status bits as a
  2077. * get port status hub request would do.
  2078. */
  2079. ub = hp->aux;
  2080. ohcistatus = ub->ohci->rhportsts[port - 1];
  2081. v = 0;
  2082. if(ohcistatus & Ccs)
  2083. v |= HPpresent;
  2084. if(ohcistatus & Pes)
  2085. v |= HPenable;
  2086. if(ohcistatus & Pss)
  2087. v |= HPsuspend;
  2088. if(ohcistatus & Prs)
  2089. v |= HPreset;
  2090. else {
  2091. /* port is not in reset; these potential writes are ok */
  2092. if(ohcistatus & Csc){
  2093. v |= HPstatuschg;
  2094. ub->ohci->rhportsts[port - 1] = Csc;
  2095. }
  2096. if(ohcistatus & Pesc){
  2097. v |= HPchange;
  2098. ub->ohci->rhportsts[port - 1] = Pesc;
  2099. }
  2100. }
  2101. if(ohcistatus & Lsda)
  2102. v |= HPslow;
  2103. if(v & (HPstatuschg|HPchange))
  2104. ddprint("ohci port %d sts %#ulx hub sts %#x\n", port, ohcistatus, v);
  2105. return v;
  2106. }
  2107. static void
  2108. dumpohci(Ctlr *ctlr)
  2109. {
  2110. int i;
  2111. uint32_t *ohci;
  2112. ohci = &ctlr->ohci->revision;
  2113. print("ohci registers: \n");
  2114. for(i = 0; i < sizeof(Ohci)/sizeof(uint32_t); i++)
  2115. if(i < 3 || ohci[i] != 0)
  2116. print("\t[%#2.2x]\t%#8.8ulx\n", i * 4, ohci[i]);
  2117. print("\n");
  2118. }
  2119. static void
  2120. init(Hci *hp)
  2121. {
  2122. Ctlr *ctlr;
  2123. Ohci *ohci;
  2124. int i;
  2125. uint32_t ival, ctrl, fmi;
  2126. ctlr = hp->aux;
  2127. dprint("ohci %#p init\n", ctlr->ohci);
  2128. ohci = ctlr->ohci;
  2129. fmi = ctlr->ohci->fminterval;
  2130. ctlr->ohci->cmdsts = Shcr; /* reset the block */
  2131. while(ctlr->ohci->cmdsts & Shcr)
  2132. delay(1); /* wait till reset complete, Ohci says 10us max. */
  2133. ctlr->ohci->fminterval = fmi;
  2134. /*
  2135. * now that soft reset is done we are in suspend state.
  2136. * Setup registers which take in suspend state
  2137. * (will only be here for 2ms).
  2138. */
  2139. ctlr->ohci->hcca = ptr2pa(ctlr->hcca);
  2140. setctlhd(ctlr, nil);
  2141. ctlr->ohci->ctlcurred = 0;
  2142. setbulkhd(ctlr, nil);
  2143. ctlr->ohci->bulkcurred = 0;
  2144. ohci->intrenable = Mie | Wdh | Ue;
  2145. ohci->control |= Ccle | Cble | Cple | Cie | Cfsoper;
  2146. /* set frame after operational */
  2147. ohci->rhdesca = Nps; /* no power switching */
  2148. if(ohci->rhdesca & Nps){
  2149. dprint("ohci: ports are not power switched\n");
  2150. }else{
  2151. dprint("ohci: ports are power switched\n");
  2152. ohci->rhdesca &= ~Psm;
  2153. ohci->rhsts &= ~Lpsc;
  2154. }
  2155. for(i = 0; i < ctlr->nports; i++) /* paranoia */
  2156. ohci->rhportsts[i] = 0; /* this has no effect */
  2157. delay(50);
  2158. for(i = 0; i < ctlr->nports; i++){
  2159. ohci->rhportsts[i] = Spp;
  2160. if((ohci->rhportsts[i] & Ccs) != 0)
  2161. ohci->rhportsts[i] |= Spr;
  2162. }
  2163. delay(100);
  2164. ctrl = ohci->control;
  2165. if((ctrl & Cfsmask) != Cfsoper){
  2166. ctrl = (ctrl & ~Cfsmask) | Cfsoper;
  2167. ohci->control = ctrl;
  2168. ohci->rhsts = Lpsc;
  2169. }
  2170. ival = ohci->fminterval & ~(Fmaxpktmask << Fmaxpktshift);
  2171. ohci->fminterval = ival | (5120 << Fmaxpktshift);
  2172. if(debug > 1)
  2173. dumpohci(ctlr);
  2174. }
  2175. static void
  2176. scanpci(void)
  2177. {
  2178. uint32_t mem;
  2179. Ctlr *ctlr;
  2180. Pcidev *p;
  2181. int i;
  2182. static int already = 0;
  2183. if(already)
  2184. return;
  2185. already = 1;
  2186. p = nil;
  2187. while(p = pcimatch(p, 0, 0)) {
  2188. /*
  2189. * Find Ohci controllers (Programming Interface = 0x10).
  2190. */
  2191. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb ||
  2192. p->ccrp != 0x10)
  2193. continue;
  2194. mem = p->mem[0].bar & ~0x0F;
  2195. dprint("ohci: %x/%x port 0x%lux size 0x%x irq %d\n",
  2196. p->vid, p->did, mem, p->mem[0].size, p->intl);
  2197. if(mem == 0){
  2198. print("ohci: failed to map registers\n");
  2199. continue;
  2200. }
  2201. if(p->intl == 0xFF || p->intl == 0) {
  2202. print("ohci: no irq assigned for port %#lux\n", mem);
  2203. continue;
  2204. }
  2205. ctlr = malloc(sizeof(Ctlr));
  2206. if (ctlr == nil)
  2207. panic("ohci: out of memory");
  2208. ctlr->pcidev = p;
  2209. ctlr->ohci = vmap(mem, p->mem[0].size);
  2210. dprint("scanpci: ctlr %#p, ohci %#p\n", ctlr, ctlr->ohci);
  2211. pcisetbme(p);
  2212. pcisetpms(p, 0);
  2213. for(i = 0; i < Nhcis; i++)
  2214. if(ctlrs[i] == nil){
  2215. ctlrs[i] = ctlr;
  2216. break;
  2217. }
  2218. if(i == Nhcis)
  2219. print("ohci: bug: no more controllers\n");
  2220. }
  2221. }
  2222. static void
  2223. usbdebug(Hci *hci, int d)
  2224. {
  2225. debug = d;
  2226. }
  2227. /*
  2228. * build the periodic scheduling tree:
  2229. * framesize must be a multiple of the tree size
  2230. */
  2231. static void
  2232. mkqhtree(Ctlr *ctlr)
  2233. {
  2234. int i, n, d, o, leaf0, depth;
  2235. Ed **tree;
  2236. Qtree *qt;
  2237. depth = flog2(32);
  2238. n = (1 << (depth+1)) - 1;
  2239. qt = mallocz(sizeof(*qt), 1);
  2240. if(qt == nil)
  2241. panic("usb: can't allocate scheduling tree");
  2242. qt->nel = n;
  2243. qt->depth = depth;
  2244. qt->bw = mallocz(n * sizeof(qt->bw), 1);
  2245. qt->root = tree = mallocz(n * sizeof(Ed *), 1);
  2246. if(qt->bw == nil || qt->root == nil)
  2247. panic("usb: can't allocate scheduling tree");
  2248. for(i = 0; i < n; i++){
  2249. if((tree[i] = edalloc()) == nil)
  2250. panic("mkqhtree");
  2251. tree[i]->ctrl = (8 << Edmpsshift); /* not needed */
  2252. tree[i]->ctrl |= Edskip;
  2253. if(i > 0)
  2254. edlinked(tree[i], tree[(i-1)/2]);
  2255. else
  2256. edlinked(tree[i], nil);
  2257. }
  2258. ctlr->ntree = i;
  2259. dprint("ohci: tree: %d endpoints allocated\n", i);
  2260. /* distribute leaves evenly round the frame list */
  2261. leaf0 = n / 2;
  2262. for(i = 0; i < 32; i++){
  2263. o = 0;
  2264. for(d = 0; d < depth; d++){
  2265. o <<= 1;
  2266. if(i & (1 << d))
  2267. o |= 1;
  2268. }
  2269. if(leaf0 + o >= n){
  2270. print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
  2271. break;
  2272. }
  2273. ctlr->hcca->intrtable[i] = ptr2pa(tree[leaf0 + o]);
  2274. }
  2275. ctlr->tree = qt;
  2276. }
  2277. static void
  2278. ohcimeminit(Ctlr *ctlr)
  2279. {
  2280. Hcca *hcca;
  2281. edfree(edalloc()); /* allocate pools now */
  2282. tdfree(tdalloc());
  2283. hcca = mallocalign(sizeof(Hcca), 256, 0, 0);
  2284. if(hcca == nil)
  2285. panic("usbhreset: no memory for Hcca");
  2286. memset(hcca, 0, sizeof(*hcca));
  2287. ctlr->hcca = hcca;
  2288. mkqhtree(ctlr);
  2289. }
  2290. static void
  2291. ohcireset(Ctlr *ctlr)
  2292. {
  2293. ilock(ctlr);
  2294. dprint("ohci %#p reset\n", ctlr->ohci);
  2295. /*
  2296. * usually enter here in reset, wait till its through,
  2297. * then do our own so we are on known timing conditions.
  2298. * Is this needed?
  2299. */
  2300. delay(100);
  2301. ctlr->ohci->control = 0;
  2302. delay(100);
  2303. /* legacy support register: turn off lunacy mode */
  2304. pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
  2305. iunlock(ctlr);
  2306. }
  2307. static void
  2308. shutdown(Hci *hp)
  2309. {
  2310. Ctlr *ctlr;
  2311. ctlr = hp->aux;
  2312. ilock(ctlr);
  2313. ctlr->ohci->intrdisable = Mie;
  2314. ctlr->ohci->control = 0;
  2315. coherence();
  2316. delay(100);
  2317. iunlock(ctlr);
  2318. }
  2319. static int
  2320. reset(Hci *hp)
  2321. {
  2322. int i;
  2323. Ctlr *ctlr;
  2324. Pcidev *p;
  2325. static Lock resetlck;
  2326. /*
  2327. if(getconf("*nousbohci"))
  2328. return -1;
  2329. */
  2330. ilock(&resetlck);
  2331. scanpci();
  2332. /*
  2333. * Any adapter matches if no hp->port is supplied,
  2334. * otherwise the ports must match.
  2335. */
  2336. ctlr = nil;
  2337. for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
  2338. ctlr = ctlrs[i];
  2339. if(ctlr->active == 0)
  2340. if(hp->port == 0 || hp->port == (uintptr)ctlr->ohci){
  2341. ctlr->active = 1;
  2342. break;
  2343. }
  2344. }
  2345. iunlock(&resetlck);
  2346. if(ctlrs[i] == nil || i == Nhcis)
  2347. return -1;
  2348. if(ctlr->ohci->control == ~0)
  2349. return -1;
  2350. p = ctlr->pcidev;
  2351. hp->aux = ctlr;
  2352. hp->port = (uintptr)ctlr->ohci;
  2353. hp->irq = p->intl;
  2354. hp->tbdf = p->tbdf;
  2355. ctlr->nports = hp->nports = ctlr->ohci->rhdesca & 0xff;
  2356. ohcireset(ctlr);
  2357. ohcimeminit(ctlr);
  2358. /*
  2359. * Linkage to the generic HCI driver.
  2360. */
  2361. hp->init = init;
  2362. hp->dump = dump;
  2363. hp->interrupt = interrupt;
  2364. hp->epopen = epopen;
  2365. hp->epclose = epclose;
  2366. hp->epread = epread;
  2367. hp->epwrite = epwrite;
  2368. hp->seprintep = seprintep;
  2369. hp->portenable = portenable;
  2370. hp->portreset = portreset;
  2371. hp->portstatus = portstatus;
  2372. hp->shutdown = shutdown;
  2373. hp->debug = usbdebug;
  2374. hp->type = "ohci";
  2375. return 0;
  2376. }
  2377. void
  2378. usbohcilink(void)
  2379. {
  2380. addhcitype("ohci", reset);
  2381. }