usbohci.c 53 KB

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