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