usbohci.c 54 KB

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