usbohci.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601
  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. }
  1603. if(c[Rtype] & Rd2h){
  1604. count = Rsetuplen;
  1605. cio->ndata = len;
  1606. cio->Qio.tok = Tdtokout;
  1607. }else{
  1608. if(len < 0)
  1609. count = -1;
  1610. else
  1611. count = Rsetuplen + len;
  1612. cio->Qio.tok = Tdtokin;
  1613. }
  1614. cio->Qio.toggle = Tddata1;
  1615. epio(ep, &cio->Qio, nil, 0, 0);
  1616. qunlock(&cio->Qio.ql);
  1617. poperror();
  1618. ddeprint("epctlio cio %#p return %ld\n", cio, count);
  1619. return count;
  1620. }
  1621. /*
  1622. * Put new samples in the dummy Td.
  1623. * BUG: This does only a transfer per Td. We could do up to 8.
  1624. */
  1625. static int32_t
  1626. putsamples(Ctlr *ctlr, Ep *ep, Isoio *iso, unsigned char *b, int32_t count)
  1627. {
  1628. Td *td;
  1629. uint32_t n;
  1630. td = pa2ptr(iso->Qio.ed->tail);
  1631. n = count;
  1632. if(n > td->nbytes - BLEN(td->bp))
  1633. n = td->nbytes - BLEN(td->bp);
  1634. assert(td->bp->wp + n <= td->bp->lim);
  1635. memmove(td->bp->wp, b, n);
  1636. td->bp->wp += n;
  1637. if(BLEN(td->bp) == td->nbytes){ /* full Td: activate it */
  1638. ilock(&ctlr->l);
  1639. isoadvance(ep, iso, td);
  1640. iunlock(&ctlr->l);
  1641. }
  1642. return n;
  1643. }
  1644. static int32_t
  1645. episowrite(Ep *ep, void *a, int32_t count)
  1646. {
  1647. Proc *up = externup();
  1648. int32_t tot, nw;
  1649. char *err;
  1650. unsigned char *b;
  1651. Ctlr *ctlr;
  1652. Isoio *iso;
  1653. ctlr = ep->hp->Hciimpl.aux;
  1654. iso = ep->aux;
  1655. iso->Qio.debug = ep->debug;
  1656. qlock(&iso->Qio.ql);
  1657. if(waserror()){
  1658. qunlock(&iso->Qio.ql);
  1659. nexterror();
  1660. }
  1661. diprint("ohci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  1662. ilock(&ctlr->l);
  1663. if(iso->Qio.state == Qclose){
  1664. iunlock(&ctlr->l);
  1665. error(iso->Qio.err ? iso->Qio.err : Eio);
  1666. }
  1667. iso->Qio.state = Qrun;
  1668. b = a;
  1669. for(tot = 0; tot < count; tot += nw){
  1670. while(isocanwrite(iso) == 0){
  1671. iunlock(&ctlr->l);
  1672. diprint("ohci: episowrite: %#p sleep\n", iso);
  1673. if(waserror()){
  1674. if(iso->Qio.err == nil)
  1675. iso->Qio.err = "I/O timed out";
  1676. ilock(&ctlr->l);
  1677. break;
  1678. }
  1679. tsleep(&iso->Qio.Rendez, isocanwrite, iso, ep->tmout);
  1680. poperror();
  1681. ilock(&ctlr->l);
  1682. }
  1683. err = iso->Qio.err;
  1684. iso->Qio.err = nil;
  1685. if(iso->Qio.state == Qclose || err != nil){
  1686. iunlock(&ctlr->l);
  1687. error(err ? err : Eio);
  1688. }
  1689. if(iso->Qio.state != Qrun)
  1690. panic("episowrite: iso not running");
  1691. iunlock(&ctlr->l); /* We could page fault here */
  1692. nw = putsamples(ctlr, ep, iso, b+tot, count-tot);
  1693. ilock(&ctlr->l);
  1694. }
  1695. if(iso->Qio.state != Qclose)
  1696. iso->Qio.state = Qdone;
  1697. iunlock(&ctlr->l);
  1698. err = iso->Qio.err; /* in case it failed early */
  1699. iso->Qio.err = nil;
  1700. qunlock(&iso->Qio.ql);
  1701. poperror();
  1702. if(err != nil)
  1703. error(err);
  1704. diprint("ohci: episowrite: %#p %ld bytes\n", iso, tot);
  1705. return tot;
  1706. }
  1707. static int32_t
  1708. epwrite(Ep *ep, void *a, int32_t count)
  1709. {
  1710. Proc *up = externup();
  1711. Qio *io;
  1712. Ctlio *cio;
  1713. uint32_t delta;
  1714. unsigned char *b;
  1715. int32_t tot, nw;
  1716. if(ep->aux == nil)
  1717. panic("ohci: epwrite: not open");
  1718. switch(ep->ttype){
  1719. case Tctl:
  1720. cio = ep->aux;
  1721. return epctlio(ep, cio, a, count);
  1722. case Tbulk:
  1723. io = ep->aux;
  1724. if(ep->clrhalt)
  1725. clrhalt(ep);
  1726. /*
  1727. * Put at most Tdatomic Tds (512 bytes) at a time.
  1728. * Otherwise some devices produce babble errors.
  1729. */
  1730. b = a;
  1731. assert(a != nil);
  1732. for(tot = 0; tot < count ; tot += nw){
  1733. nw = count - tot;
  1734. if(nw > Tdatomic * ep->maxpkt)
  1735. nw = Tdatomic * ep->maxpkt;
  1736. nw = epio(ep, &io[OWRITE], b+tot, nw, 1);
  1737. }
  1738. return tot;
  1739. case Tintr:
  1740. io = ep->aux;
  1741. delta = TK2MS(machp()->ticks) - io[OWRITE].iotime + 1;
  1742. if(delta < ep->pollival)
  1743. tsleep(&up->sleep, return0, 0, ep->pollival - delta);
  1744. if(ep->clrhalt)
  1745. clrhalt(ep);
  1746. return epio(ep, &io[OWRITE], a, count, 1);
  1747. case Tiso:
  1748. return episowrite(ep, a, count);
  1749. default:
  1750. panic("ohci: epwrite: bad ep ttype %d", ep->ttype);
  1751. }
  1752. return -1;
  1753. }
  1754. static Ed*
  1755. newed(Ctlr *ctlr, Ep *ep, Qio *io, char *c)
  1756. {
  1757. Proc *up = externup();
  1758. Ed *ed;
  1759. Td *td;
  1760. ed = io->ed = edalloc(); /* no errors raised here, really */
  1761. td = tdalloc();
  1762. td->ep = ep;
  1763. td->io = io;
  1764. ed->tail = ptr2pa(td);
  1765. ed->head = ptr2pa(td);
  1766. ed->tds = td;
  1767. ed->ep = ep;
  1768. ed->ctrl = (ep->maxpkt & Edmpsmask) << Edmpsshift;
  1769. if(ep->ttype == Tiso)
  1770. ed->ctrl |= Ediso;
  1771. if(waserror()){
  1772. edfree(ed);
  1773. io->ed = nil;
  1774. nexterror();
  1775. }
  1776. /* For setup endpoints we start with the config address */
  1777. if(ep->ttype != Tctl)
  1778. edsetaddr(io->ed, io->usbid);
  1779. if(ep->dev->speed == Lowspeed)
  1780. ed->ctrl |= Edlow;
  1781. switch(io->tok){
  1782. case Tdtokin:
  1783. ed->ctrl |= Edin;
  1784. break;
  1785. case Tdtokout:
  1786. ed->ctrl |= Edout;
  1787. break;
  1788. default:
  1789. ed->ctrl |= Edtddir; /* Td will say */
  1790. break;
  1791. }
  1792. switch(ep->ttype){
  1793. case Tctl:
  1794. ilock(&ctlr->l);
  1795. edlinked(ed, ctlhd(ctlr));
  1796. setctlhd(ctlr, ed);
  1797. iunlock(&ctlr->l);
  1798. break;
  1799. case Tbulk:
  1800. ilock(&ctlr->l);
  1801. edlinked(ed, bulkhd(ctlr));
  1802. setbulkhd(ctlr, ed);
  1803. iunlock(&ctlr->l);
  1804. break;
  1805. case Tintr:
  1806. case Tiso:
  1807. ilock(&ctlr->l);
  1808. schedq(ctlr, io, ep->pollival);
  1809. iunlock(&ctlr->l);
  1810. break;
  1811. default:
  1812. panic("ohci: newed: bad ttype");
  1813. }
  1814. poperror();
  1815. return ed;
  1816. }
  1817. static void
  1818. isoopen(Ctlr *ctlr, Ep *ep)
  1819. {
  1820. Td *td, *edtds;
  1821. Isoio *iso;
  1822. int i;
  1823. iso = ep->aux;
  1824. iso->Qio.usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
  1825. iso->Qio.bw = ep->hz * ep->samplesz; /* bytes/sec */
  1826. if(ep->mode != OWRITE){
  1827. print("ohci: bug: iso input streams not implemented\n");
  1828. error("ohci iso input streams not implemented");
  1829. }else
  1830. iso->Qio.tok = Tdtokout;
  1831. iso->left = 0;
  1832. iso->nerrs = 0;
  1833. iso->frno = TRUNC(ctlr->ohci->fmnumber + 10, Ntdframes);
  1834. iso->nframes = 1000 / ep->pollival;
  1835. if(iso->nframes < 10){
  1836. print("ohci: isoopen: less than 10 frames; using 10.\n");
  1837. iso->nframes = 10;
  1838. }
  1839. iso->navail = iso->nframes;
  1840. iso->atds = edtds = nil;
  1841. for(i = 0; i < iso->nframes-1; i++){ /* -1 for dummy */
  1842. td = tdalloc();
  1843. td->ep = ep;
  1844. td->io = &iso->Qio;
  1845. td->bp = allocb(ep->maxpkt);
  1846. td->anext = iso->atds; /* link as avail */
  1847. iso->atds = td;
  1848. td->next = edtds;
  1849. edtds = td;
  1850. }
  1851. newed(ctlr, ep, &iso->Qio, "iso"); /* allocates a dummy td */
  1852. iso->Qio.ed->tds->bp = allocb(ep->maxpkt); /* but not its block */
  1853. iso->Qio.ed->tds->next = edtds;
  1854. isodtdinit(ep, iso, iso->Qio.ed->tds);
  1855. }
  1856. /*
  1857. * Allocate the endpoint and set it up for I/O
  1858. * in the controller. This must follow what's said
  1859. * in Ep regarding configuration, including perhaps
  1860. * the saved toggles (saved on a previous close of
  1861. * the endpoint data file by epclose).
  1862. */
  1863. static void
  1864. epopen(Ep *ep)
  1865. {
  1866. Proc *up = externup();
  1867. Ctlr *ctlr;
  1868. Qio *io;
  1869. Ctlio *cio;
  1870. uint32_t usbid;
  1871. ctlr = ep->hp->Hciimpl.aux;
  1872. deprint("ohci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
  1873. if(ep->aux != nil)
  1874. panic("ohci: epopen called with open ep");
  1875. if(waserror()){
  1876. free(ep->aux);
  1877. ep->aux = nil;
  1878. nexterror();
  1879. }
  1880. switch(ep->ttype){
  1881. case Tnone:
  1882. error("endpoint not configured");
  1883. case Tiso:
  1884. ep->aux = smalloc(sizeof(Isoio));
  1885. isoopen(ctlr, ep);
  1886. break;
  1887. case Tctl:
  1888. cio = ep->aux = smalloc(sizeof(Ctlio));
  1889. cio->Qio.debug = ep->debug;
  1890. cio->ndata = -1;
  1891. cio->data = nil;
  1892. cio->Qio.tok = -1; /* invalid; Tds will say */
  1893. if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
  1894. break;
  1895. newed(ctlr, ep, &cio->Qio, "epc");
  1896. break;
  1897. case Tbulk:
  1898. ep->pollival = 1; /* assume this; doesn't really matter */
  1899. /* and fall... */
  1900. case Tintr:
  1901. io = ep->aux = smalloc(sizeof(Qio)*2);
  1902. io[OREAD].debug = io[OWRITE].debug = ep->debug;
  1903. usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
  1904. if(ep->mode != OREAD){
  1905. if(ep->toggle[OWRITE] != 0)
  1906. io[OWRITE].toggle = Tddata1;
  1907. else
  1908. io[OWRITE].toggle = Tddata0;
  1909. io[OWRITE].tok = Tdtokout;
  1910. io[OWRITE].usbid = usbid;
  1911. io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  1912. newed(ctlr, ep, io+OWRITE, "epw");
  1913. }
  1914. if(ep->mode != OWRITE){
  1915. if(ep->toggle[OREAD] != 0)
  1916. io[OREAD].toggle = Tddata1;
  1917. else
  1918. io[OREAD].toggle = Tddata0;
  1919. io[OREAD].tok = Tdtokin;
  1920. io[OREAD].usbid = usbid;
  1921. io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
  1922. newed(ctlr, ep, io+OREAD, "epr");
  1923. }
  1924. break;
  1925. }
  1926. deprint("ohci: epopen done:\n");
  1927. if(debug || ep->debug)
  1928. dump(ep->hp);
  1929. poperror();
  1930. }
  1931. static void
  1932. cancelio(Ep *ep, Qio *io)
  1933. {
  1934. Proc *up = externup();
  1935. Ed *ed;
  1936. Ctlr *ctlr;
  1937. ctlr = ep->hp->Hciimpl.aux;
  1938. ilock(&ctlr->l);
  1939. if(io == nil || io->state == Qclose){
  1940. assert(io == nil || io->ed == nil);
  1941. iunlock(&ctlr->l);
  1942. return;
  1943. }
  1944. ed = io->ed;
  1945. io->state = Qclose;
  1946. io->err = Eio;
  1947. aborttds(io);
  1948. iunlock(&ctlr->l);
  1949. if(!waserror()){
  1950. tsleep(&up->sleep, return0, 0, Abortdelay);
  1951. poperror();
  1952. }
  1953. wakeup(&io->Rendez);
  1954. qlock(&io->ql);
  1955. /* wait for epio if running */
  1956. qunlock(&io->ql);
  1957. ilock(&ctlr->l);
  1958. switch(ep->ttype){
  1959. case Tctl:
  1960. unlinkctl(ctlr, ed);
  1961. break;
  1962. case Tbulk:
  1963. unlinkbulk(ctlr, ed);
  1964. break;
  1965. case Tintr:
  1966. case Tiso:
  1967. unschedq(ctlr, io);
  1968. break;
  1969. default:
  1970. panic("ohci cancelio: bad ttype");
  1971. }
  1972. iunlock(&ctlr->l);
  1973. edfree(io->ed);
  1974. io->ed = nil;
  1975. }
  1976. static void
  1977. epclose(Ep *ep)
  1978. {
  1979. Ctlio *cio;
  1980. Isoio *iso;
  1981. Qio *io;
  1982. deprint("ohci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
  1983. if(ep->aux == nil)
  1984. panic("ohci: epclose called with closed ep");
  1985. switch(ep->ttype){
  1986. case Tctl:
  1987. cio = ep->aux;
  1988. cancelio(ep, &cio->Qio);
  1989. free(cio->data);
  1990. cio->data = nil;
  1991. break;
  1992. case Tbulk:
  1993. case Tintr:
  1994. io = ep->aux;
  1995. if(ep->mode != OWRITE){
  1996. cancelio(ep, &io[OREAD]);
  1997. if(io[OREAD].toggle == Tddata1)
  1998. ep->toggle[OREAD] = 1;
  1999. }
  2000. if(ep->mode != OREAD){
  2001. cancelio(ep, &io[OWRITE]);
  2002. if(io[OWRITE].toggle == Tddata1)
  2003. ep->toggle[OWRITE] = 1;
  2004. }
  2005. break;
  2006. case Tiso:
  2007. iso = ep->aux;
  2008. cancelio(ep, &iso->Qio);
  2009. break;
  2010. default:
  2011. panic("epclose: bad ttype %d", ep->ttype);
  2012. }
  2013. deprint("ohci: epclose ep%d.%d: done\n", ep->dev->nb, ep->nb);
  2014. free(ep->aux);
  2015. ep->aux = nil;
  2016. }
  2017. static int
  2018. portreset(Hci *hp, int port, int on)
  2019. {
  2020. Proc *up = externup();
  2021. Ctlr *ctlr;
  2022. Ohci *ohci;
  2023. if(on == 0)
  2024. return 0;
  2025. ctlr = hp->Hciimpl.aux;
  2026. qlock(&ctlr->resetl);
  2027. if(waserror()){
  2028. qunlock(&ctlr->resetl);
  2029. nexterror();
  2030. }
  2031. ilock(&ctlr->l);
  2032. ohci = ctlr->ohci;
  2033. ohci->rhportsts[port - 1] = Spp;
  2034. if((ohci->rhportsts[port - 1] & Ccs) == 0){
  2035. iunlock(&ctlr->l);
  2036. error("port not connected");
  2037. }
  2038. ohci->rhportsts[port - 1] = Spr;
  2039. while((ohci->rhportsts[port - 1] & Prsc) == 0){
  2040. iunlock(&ctlr->l);
  2041. dprint("ohci: portreset, wait for reset complete\n");
  2042. ilock(&ctlr->l);
  2043. }
  2044. ohci->rhportsts[port - 1] = Prsc;
  2045. iunlock(&ctlr->l);
  2046. poperror();
  2047. qunlock(&ctlr->resetl);
  2048. return 0;
  2049. }
  2050. static int
  2051. portenable(Hci *hp, int port, int on)
  2052. {
  2053. Proc *up = externup();
  2054. Ctlr *ctlr;
  2055. ctlr = hp->Hciimpl.aux;
  2056. dprint("ohci: %#p port %d enable=%d\n", ctlr->ohci, port, on);
  2057. qlock(&ctlr->resetl);
  2058. if(waserror()){
  2059. qunlock(&ctlr->resetl);
  2060. nexterror();
  2061. }
  2062. ilock(&ctlr->l);
  2063. if(on)
  2064. ctlr->ohci->rhportsts[port - 1] = Spe | Spp;
  2065. else
  2066. ctlr->ohci->rhportsts[port - 1] = Cpe;
  2067. iunlock(&ctlr->l);
  2068. tsleep(&up->sleep, return0, 0, Enabledelay);
  2069. poperror();
  2070. qunlock(&ctlr->resetl);
  2071. return 0;
  2072. }
  2073. static int
  2074. portstatus(Hci *hp, int port)
  2075. {
  2076. int v;
  2077. Ctlr *ub;
  2078. uint32_t ohcistatus;
  2079. /*
  2080. * We must return status bits as a
  2081. * get port status hub request would do.
  2082. */
  2083. ub = hp->Hciimpl.aux;
  2084. ohcistatus = ub->ohci->rhportsts[port - 1];
  2085. v = 0;
  2086. if(ohcistatus & Ccs)
  2087. v |= HPpresent;
  2088. if(ohcistatus & Pes)
  2089. v |= HPenable;
  2090. if(ohcistatus & Pss)
  2091. v |= HPsuspend;
  2092. if(ohcistatus & Prs)
  2093. v |= HPreset;
  2094. else {
  2095. /* port is not in reset; these potential writes are ok */
  2096. if(ohcistatus & Csc){
  2097. v |= HPstatuschg;
  2098. ub->ohci->rhportsts[port - 1] = Csc;
  2099. }
  2100. if(ohcistatus & Pesc){
  2101. v |= HPchange;
  2102. ub->ohci->rhportsts[port - 1] = Pesc;
  2103. }
  2104. }
  2105. if(ohcistatus & Lsda)
  2106. v |= HPslow;
  2107. if(v & (HPstatuschg|HPchange))
  2108. ddprint("ohci port %d sts %#lx hub sts %#x\n", port, ohcistatus, v);
  2109. return v;
  2110. }
  2111. static void
  2112. dumpohci(Ctlr *ctlr)
  2113. {
  2114. int i;
  2115. uint32_t *ohci;
  2116. ohci = &ctlr->ohci->revision;
  2117. print("ohci registers: \n");
  2118. for(i = 0; i < sizeof(Ohci)/sizeof(uint32_t); i++)
  2119. if(i < 3 || ohci[i] != 0)
  2120. print("\t[%#2.2x]\t%#8.8lx\n", i * 4, ohci[i]);
  2121. print("\n");
  2122. }
  2123. static void
  2124. init(Hci *hp)
  2125. {
  2126. Ctlr *ctlr;
  2127. Ohci *ohci;
  2128. int i;
  2129. uint32_t ival, ctrl, fmi;
  2130. ctlr = hp->Hciimpl.aux;
  2131. dprint("ohci %#p init\n", ctlr->ohci);
  2132. ohci = ctlr->ohci;
  2133. fmi = ctlr->ohci->fminterval;
  2134. ctlr->ohci->cmdsts = Shcr; /* reset the block */
  2135. while(ctlr->ohci->cmdsts & Shcr)
  2136. delay(1); /* wait till reset complete, Ohci says 10us max. */
  2137. ctlr->ohci->fminterval = fmi;
  2138. /*
  2139. * now that soft reset is done we are in suspend state.
  2140. * Setup registers which take in suspend state
  2141. * (will only be here for 2ms).
  2142. */
  2143. ctlr->ohci->hcca = ptr2pa(ctlr->hcca);
  2144. setctlhd(ctlr, nil);
  2145. ctlr->ohci->ctlcurred = 0;
  2146. setbulkhd(ctlr, nil);
  2147. ctlr->ohci->bulkcurred = 0;
  2148. ohci->intrenable = Mie | Wdh | Ue;
  2149. ohci->control |= Ccle | Cble | Cple | Cie | Cfsoper;
  2150. /* set frame after operational */
  2151. ohci->rhdesca = Nps; /* no power switching */
  2152. if(ohci->rhdesca & Nps){
  2153. dprint("ohci: ports are not power switched\n");
  2154. }else{
  2155. dprint("ohci: ports are power switched\n");
  2156. ohci->rhdesca &= ~Psm;
  2157. ohci->rhsts &= ~Lpsc;
  2158. }
  2159. for(i = 0; i < ctlr->nports; i++) /* paranoia */
  2160. ohci->rhportsts[i] = 0; /* this has no effect */
  2161. delay(50);
  2162. for(i = 0; i < ctlr->nports; i++){
  2163. ohci->rhportsts[i] = Spp;
  2164. if((ohci->rhportsts[i] & Ccs) != 0)
  2165. ohci->rhportsts[i] |= Spr;
  2166. }
  2167. delay(100);
  2168. ctrl = ohci->control;
  2169. if((ctrl & Cfsmask) != Cfsoper){
  2170. ctrl = (ctrl & ~Cfsmask) | Cfsoper;
  2171. ohci->control = ctrl;
  2172. ohci->rhsts = Lpsc;
  2173. }
  2174. ival = ohci->fminterval & ~(Fmaxpktmask << Fmaxpktshift);
  2175. ohci->fminterval = ival | (5120 << Fmaxpktshift);
  2176. if(debug > 1)
  2177. dumpohci(ctlr);
  2178. }
  2179. static void
  2180. scanpci(void)
  2181. {
  2182. uint32_t mem;
  2183. Ctlr *ctlr;
  2184. Pcidev *p;
  2185. int i;
  2186. static int already = 0;
  2187. if(already)
  2188. return;
  2189. already = 1;
  2190. p = nil;
  2191. while((p = pcimatch(p, 0, 0)) != nil){
  2192. /*
  2193. * Find Ohci controllers (Programming Interface = 0x10).
  2194. */
  2195. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb ||
  2196. p->ccrp != 0x10)
  2197. continue;
  2198. mem = p->mem[0].bar & ~0x0F;
  2199. dprint("ohci: %x/%x port 0x%lx size 0x%x irq %d\n",
  2200. p->vid, p->did, mem, p->mem[0].size, p->intl);
  2201. if(mem == 0){
  2202. print("ohci: failed to map registers\n");
  2203. continue;
  2204. }
  2205. if(p->intl == 0xFF || p->intl == 0) {
  2206. print("ohci: no irq assigned for port %#lx\n", mem);
  2207. continue;
  2208. }
  2209. ctlr = malloc(sizeof(Ctlr));
  2210. if (ctlr == nil)
  2211. panic("ohci: out of memory");
  2212. ctlr->pcidev = p;
  2213. ctlr->ohci = vmap(mem, p->mem[0].size);
  2214. dprint("scanpci: ctlr %#p, ohci %#p\n", ctlr, ctlr->ohci);
  2215. pcisetbme(p);
  2216. pcisetpms(p, 0);
  2217. for(i = 0; i < Nhcis; i++)
  2218. if(ctlrs[i] == nil){
  2219. ctlrs[i] = ctlr;
  2220. break;
  2221. }
  2222. if(i == Nhcis)
  2223. print("ohci: bug: no more controllers\n");
  2224. }
  2225. }
  2226. static void
  2227. usbdebug(Hci *hci, int d)
  2228. {
  2229. debug = d;
  2230. }
  2231. /*
  2232. * build the periodic scheduling tree:
  2233. * framesize must be a multiple of the tree size
  2234. */
  2235. static void
  2236. mkqhtree(Ctlr *ctlr)
  2237. {
  2238. int i, n, d, o, leaf0, depth;
  2239. Ed **tree;
  2240. Qtree *qt;
  2241. depth = flog2(32);
  2242. n = (1 << (depth+1)) - 1;
  2243. qt = mallocz(sizeof(*qt), 1);
  2244. if(qt == nil)
  2245. panic("usb: can't allocate scheduling tree");
  2246. qt->nel = n;
  2247. qt->depth = depth;
  2248. qt->bw = mallocz(n * sizeof(qt->bw), 1);
  2249. qt->root = tree = mallocz(n * sizeof(Ed *), 1);
  2250. if(qt->bw == nil || qt->root == nil)
  2251. panic("usb: can't allocate scheduling tree");
  2252. for(i = 0; i < n; i++){
  2253. if((tree[i] = edalloc()) == nil)
  2254. panic("mkqhtree");
  2255. tree[i]->ctrl = (8 << Edmpsshift); /* not needed */
  2256. tree[i]->ctrl |= Edskip;
  2257. if(i > 0)
  2258. edlinked(tree[i], tree[(i-1)/2]);
  2259. else
  2260. edlinked(tree[i], nil);
  2261. }
  2262. ctlr->ntree = i;
  2263. dprint("ohci: tree: %d endpoints allocated\n", i);
  2264. /* distribute leaves evenly round the frame list */
  2265. leaf0 = n / 2;
  2266. for(i = 0; i < 32; i++){
  2267. o = 0;
  2268. for(d = 0; d < depth; d++){
  2269. o <<= 1;
  2270. if(i & (1 << d))
  2271. o |= 1;
  2272. }
  2273. if(leaf0 + o >= n){
  2274. print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
  2275. break;
  2276. }
  2277. ctlr->hcca->intrtable[i] = ptr2pa(tree[leaf0 + o]);
  2278. }
  2279. ctlr->tree = qt;
  2280. }
  2281. static void
  2282. ohcimeminit(Ctlr *ctlr)
  2283. {
  2284. Hcca *hcca;
  2285. edfree(edalloc()); /* allocate pools now */
  2286. tdfree(tdalloc());
  2287. hcca = mallocalign(sizeof(Hcca), 256, 0, 0);
  2288. if(hcca == nil)
  2289. panic("usbhreset: no memory for Hcca");
  2290. memset(hcca, 0, sizeof(*hcca));
  2291. ctlr->hcca = hcca;
  2292. mkqhtree(ctlr);
  2293. }
  2294. static void
  2295. ohcireset(Ctlr *ctlr)
  2296. {
  2297. ilock(&ctlr->l);
  2298. dprint("ohci %#p reset\n", ctlr->ohci);
  2299. /*
  2300. * usually enter here in reset, wait till its through,
  2301. * then do our own so we are on known timing conditions.
  2302. * Is this needed?
  2303. */
  2304. delay(100);
  2305. ctlr->ohci->control = 0;
  2306. delay(100);
  2307. /* legacy support register: turn off lunacy mode */
  2308. pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
  2309. iunlock(&ctlr->l);
  2310. }
  2311. static void
  2312. shutdown(Hci *hp)
  2313. {
  2314. Ctlr *ctlr;
  2315. ctlr = hp->Hciimpl.aux;
  2316. ilock(&ctlr->l);
  2317. ctlr->ohci->intrdisable = Mie;
  2318. ctlr->ohci->control = 0;
  2319. coherence();
  2320. delay(100);
  2321. iunlock(&ctlr->l);
  2322. }
  2323. static int
  2324. reset(Hci *hp)
  2325. {
  2326. int i;
  2327. Ctlr *ctlr;
  2328. Pcidev *p;
  2329. static Lock resetlck;
  2330. ilock(&resetlck);
  2331. scanpci();
  2332. /*
  2333. * Any adapter matches if no hp->port is supplied,
  2334. * otherwise the ports must match.
  2335. */
  2336. ctlr = nil;
  2337. for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
  2338. ctlr = ctlrs[i];
  2339. if(ctlr->active == 0)
  2340. if(hp->ISAConf.port == 0 || hp->ISAConf.port == (uintptr)ctlr->ohci){
  2341. ctlr->active = 1;
  2342. break;
  2343. }
  2344. }
  2345. iunlock(&resetlck);
  2346. if(ctlrs[i] == nil || i == Nhcis)
  2347. return -1;
  2348. if(ctlr->ohci->control == ~0)
  2349. return -1;
  2350. p = ctlr->pcidev;
  2351. hp->Hciimpl.aux = ctlr;
  2352. hp->ISAConf.port = (uintptr)ctlr->ohci;
  2353. hp->ISAConf.irq = p->intl;
  2354. hp->tbdf = p->tbdf;
  2355. ctlr->nports = hp->nports = ctlr->ohci->rhdesca & 0xff;
  2356. ohcireset(ctlr);
  2357. ohcimeminit(ctlr);
  2358. /*
  2359. * Linkage to the generic HCI driver.
  2360. */
  2361. hp->Hciimpl.init = init;
  2362. hp->Hciimpl.dump = dump;
  2363. hp->Hciimpl.interrupt = interrupt;
  2364. hp->Hciimpl.epopen = epopen;
  2365. hp->Hciimpl.epclose = epclose;
  2366. hp->Hciimpl.epread = epread;
  2367. hp->Hciimpl.epwrite = epwrite;
  2368. hp->Hciimpl.seprintep = seprintep;
  2369. hp->Hciimpl.portenable = portenable;
  2370. hp->Hciimpl.portreset = portreset;
  2371. hp->Hciimpl.portstatus = portstatus;
  2372. hp->Hciimpl.shutdown = shutdown;
  2373. hp->Hciimpl.debug = usbdebug;
  2374. hp->ISAConf.type = "ohci";
  2375. return 0;
  2376. }
  2377. void
  2378. usbohcilink(void)
  2379. {
  2380. addhcitype("ohci", reset);
  2381. }