usbuhci.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  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 Universal Host Controller Interface (sic) driver.
  11. *
  12. * BUGS:
  13. * - Too many delays and ilocks.
  14. * - bandwidth admission control must be done per-frame.
  15. * - interrupt endpoints should go on a tree like [oe]hci.
  16. * - must warn of power overruns.
  17. */
  18. #include "u.h"
  19. #include "../port/lib.h"
  20. #include "mem.h"
  21. #include "dat.h"
  22. #include "fns.h"
  23. #include "io.h"
  24. #include "../port/error.h"
  25. #include "../port/usb.h"
  26. typedef struct Ctlio Ctlio;
  27. typedef struct Ctlr Ctlr;
  28. typedef struct Isoio Isoio;
  29. typedef struct Qh Qh;
  30. typedef struct Qhpool Qhpool;
  31. typedef struct Qio Qio;
  32. typedef struct Td Td;
  33. typedef struct Tdpool Tdpool;
  34. enum
  35. {
  36. Resetdelay = 100, /* delay after a controller reset (ms) */
  37. Enabledelay = 100, /* waiting for a port to enable */
  38. Abortdelay = 5, /* delay after cancelling Tds (ms) */
  39. Incr = 64, /* for Td and Qh pools */
  40. Tdatomic = 8, /* max nb. of Tds per bulk I/O op. */
  41. /* Queue states (software) */
  42. Qidle = 0,
  43. Qinstall,
  44. Qrun,
  45. Qdone,
  46. Qclose,
  47. Qfree,
  48. /*
  49. * HW constants
  50. */
  51. Nframes = 1024, /* 2ⁿ for mallocalign; max 1024 */
  52. Align = 16, /* for data structures */
  53. /* Size of small buffer kept within Tds. (software) */
  54. /* Keep as a multiple of Align to maintain alignment of Tds in pool */
  55. Tdndata = 1*Align,
  56. /* i/o space
  57. * Some ports are short, some are int32_t, some are byte.
  58. * We use ins[bsl] and not vmap.
  59. */
  60. Cmd = 0,
  61. Crun = 0x01,
  62. Chcreset = 0x02, /* host controller reset */
  63. Cgreset = 0x04, /* global reset */
  64. Cegsm = 0x08, /* enter global suspend */
  65. Cfgr = 0x10, /* forge global resume */
  66. Cdbg = 0x20, /* single step, debug */
  67. Cmaxp = 0x80, /* max packet */
  68. Status = 2,
  69. Susbintr = 0x01, /* interrupt */
  70. Seintr = 0x02, /* error interrupt */
  71. Sresume = 0x04, /* resume detect */
  72. Shserr = 0x08, /* host system error */
  73. Shcerr = 0x10, /* host controller error */
  74. Shalted = 0x20, /* controller halted */
  75. Sall = 0x3F,
  76. Usbintr = 4,
  77. Itmout = 0x01, /* timeout or crc */
  78. Iresume = 0x02, /* resume interrupt enable */
  79. Ioc = 0x04, /* interrupt on complete */
  80. Ishort = 0x08, /* short packet interrupt */
  81. Iall = 0x0F,
  82. Frnum = 6,
  83. Flbaseadd = 8,
  84. SOFmod = 0xC, /* start of frame modifier register */
  85. Portsc0 = 0x10,
  86. PSpresent = 0x0001, /* device present */
  87. PSstatuschg = 0x0002, /* PSpresent changed */
  88. PSenable = 0x0004, /* device enabled */
  89. PSchange = 0x0008, /* PSenable changed */
  90. PSresume = 0x0040, /* resume detected */
  91. PSreserved1 = 0x0080, /* always read as 1; reserved */
  92. PSslow = 0x0100, /* device has low speed */
  93. PSreset = 0x0200, /* port reset */
  94. PSsuspend = 0x1000, /* port suspended */
  95. /* Transfer descriptor link */
  96. Tdterm = 0x1, /* nil (terminate) */
  97. Tdlinkqh = 0x2, /* link refers to a QH */
  98. Tdvf = 0x4, /* run linked Tds first (depth-first)*/
  99. /* Transfer status bits */
  100. Tdbitstuff = 0x00020000, /* bit stuffing error */
  101. Tdcrcto = 0x00040000, /* crc or timeout error */
  102. Tdnak = 0x00080000, /* nak packet received */
  103. Tdbabble = 0x00100000, /* babble detected */
  104. Tddberr = 0x00200000, /* data buf. error */
  105. Tdstalled = 0x00400000, /* serious error to ep. */
  106. Tdactive = 0x00800000, /* enabled/in use by hw */
  107. /* Transfer control bits */
  108. Tdioc = 0x01000000, /* interrupt on complete */
  109. Tdiso = 0x02000000, /* isochronous select */
  110. Tdlow = 0x04000000, /* low speed device */
  111. Tderr1 = 0x08000000, /* bit 0 of error counter */
  112. Tderr2 = 0x10000000, /* bit 1 of error counter */
  113. Tdspd = 0x20000000, /* short packet detect */
  114. Tdlen = 0x000003FF, /* actual length field */
  115. Tdfatalerr = Tdnak|Tdbabble|Tdstalled, /* hw retries others */
  116. Tderrors = Tdfatalerr|Tdbitstuff|Tdcrcto|Tddberr,
  117. /* Transfer descriptor token bits */
  118. Tddata0 = 0,
  119. Tddata1 = 0x80000, /* data toggle (1==DATA1) */
  120. Tdtokin = 0x69,
  121. Tdtokout = 0xE1,
  122. Tdtoksetup = 0x2D,
  123. Tdmaxpkt = 0x800, /* max packet size */
  124. /* Queue head bits */
  125. QHterm = 1<<0, /* nil (terminate) */
  126. QHlinkqh = 1<<1, /* link refers to a QH */
  127. QHvf = 1<<2, /* vertical first (depth first) */
  128. };
  129. struct Ctlr
  130. {
  131. Lock l; /* for ilock. qh lists and basic ctlr I/O */
  132. QLock portlck; /* for port resets/enable... */
  133. Pcidev* pcidev;
  134. int active;
  135. int port; /* I/O address */
  136. Qh* qhs; /* list of Qhs for this controller */
  137. Qh* qh[Tmax]; /* Dummy Qhs to insert Qhs after */
  138. Isoio* iso; /* list of active iso I/O */
  139. uint32_t* frames; /* frame list (used by hw) */
  140. uint32_t load; /* max load for a single frame */
  141. uint32_t isoload; /* max iso load for a single frame */
  142. int nintr; /* number of interrupts attended */
  143. int ntdintr; /* number of intrs. with something to do */
  144. int nqhintr; /* number of intrs. for Qhs */
  145. int nisointr; /* number of intrs. for iso transfers */
  146. };
  147. struct Qio
  148. {
  149. QLock ql; /* for the entire I/O process */
  150. Rendez Rendez; /* wait for completion */
  151. Qh* qh; /* Td list (field const after init) */
  152. int usbid; /* usb address for endpoint/device */
  153. int toggle; /* Tddata0/Tddata1 */
  154. int tok; /* Tdtoksetup, Tdtokin, Tdtokout */
  155. uint32_t iotime; /* time of last I/O */
  156. int debug; /* debug flag from the endpoint */
  157. char* err; /* error string */
  158. };
  159. struct Ctlio
  160. {
  161. Qio Qio; /* a single Qio for each RPC */
  162. unsigned char* data; /* read from last ctl req. */
  163. int ndata; /* number of bytes read */
  164. };
  165. struct Isoio
  166. {
  167. QLock ql;
  168. Rendez Rendez; /* wait for space/completion/errors */
  169. int usbid; /* address used for device/endpoint */
  170. int tok; /* Tdtokin or Tdtokout */
  171. int state; /* Qrun -> Qdone -> Qrun... -> Qclose */
  172. int nframes; /* Nframes/ep->pollival */
  173. unsigned char* data; /* iso data buffers if not embedded */
  174. int td0frno; /* frame number for first Td */
  175. Td* tdu; /* next td for user I/O in tdps */
  176. Td* tdi; /* next td processed by interrupt */
  177. char* err; /* error string */
  178. int nerrs; /* nb of consecutive I/O errors */
  179. int32_t nleft; /* number of bytes left from last write */
  180. int debug; /* debug flag from the endpoint */
  181. Isoio* next; /* in list of active Isoios */
  182. Td* tdps[Nframes]; /* pointer to Td used for i-th frame or nil */
  183. };
  184. struct Tdpool
  185. {
  186. Lock l;
  187. Td* free;
  188. int nalloc;
  189. int ninuse;
  190. int nfree;
  191. };
  192. struct Qhpool
  193. {
  194. Lock l;
  195. Qh* free;
  196. int nalloc;
  197. int ninuse;
  198. int nfree;
  199. };
  200. /*
  201. * HW data structures
  202. */
  203. /*
  204. * Queue header (known by hw).
  205. * 16-byte aligned. first two words used by hw.
  206. * They are taken from the pool upon endpoint opening and
  207. * queued after the dummy queue header for the endpoint type
  208. * in the controller. Actual I/O happens as Tds are linked into it.
  209. * The driver does I/O in lock-step.
  210. * The user builds a list of Tds and links it into the Qh,
  211. * then the Qh goes from Qidle to Qrun and nobody touches it until
  212. * it becomes Qdone at interrupt time.
  213. * At that point the user collects the Tds and it goes Qidle.
  214. * A premature cancel may set the state to Qclose and abort I/O.
  215. * The Ctlr lock protects change of state for Qhs in use.
  216. */
  217. struct Qh
  218. {
  219. uint32_t link; /* link to next horiz. item (eg. Qh) */
  220. uint32_t elink; /* link to element (eg. Td; updated by hw) */
  221. uint32_t state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
  222. uint32_t _32;
  223. Qio* io; /* for this queue */
  224. Qh* next; /* in active or free list */
  225. Td* tds; /* Td list in this Qh (initially, elink) */
  226. char* tag; /* debug and align, mostly */
  227. };
  228. /*
  229. * Transfer descriptor.
  230. * 16-byte aligned. first two words used by hw. Next 4 by sw.
  231. * We keep an embedded buffer for small I/O transfers.
  232. * They are taken from the pool when buffers are needed for I/O
  233. * and linked at the Qh/Isoio for the endpoint and direction requiring it.
  234. * The block keeps actual data. They are protected from races by
  235. * the queue or the pool keeping it. The owner of the link to the Td
  236. * is free to use it and can be the only one using it.
  237. */
  238. struct Td
  239. {
  240. uint32_t link; /* Link to next Td or Qh */
  241. uint32_t csw; /* control and status word (updated by hw) */
  242. uint32_t token; /* endpt, device, pid */
  243. uint32_t buffer; /* buffer pointer */
  244. Td* next; /* in qh or Isoio or free list */
  245. uint32_t ndata; /* bytes available/used at data */
  246. unsigned char* data; /* pointer to actual data */
  247. void* buff; /* allocated data, for large transfers */
  248. unsigned char sbuff[Tdndata]; /* embedded buffer, for small transfers */
  249. };
  250. #define INB(x) inb(ctlr->port+(x))
  251. #define INS(x) ins(ctlr->port+(x))
  252. #define INL(x) inl(ctlr->port+(x))
  253. #define OUTB(x, v) outb(ctlr->port+(x), (v))
  254. #define OUTS(x, v) outs(ctlr->port+(x), (v))
  255. #define OUTL(x, v) outl(ctlr->port+(x), (v))
  256. #define TRUNC(x, sz) ((x) & ((sz)-1))
  257. #define PTR(q) ((void*)KADDR((uint32_t)(q) & ~ (0xF|PCIWINDOW)))
  258. #define QPTR(q) ((Qh*)PTR(q))
  259. #define TPTR(q) ((Td*)PTR(q))
  260. #define PORT(p) (Portsc0 + 2*(p))
  261. #define diprint if(debug || iso->debug)print
  262. #define ddiprint if(debug>1 || iso->debug>1)print
  263. #define dqprint if(debug || (qh->io && qh->io->debug))print
  264. #define ddqprint if(debug>1 || (qh->io && qh->io->debug>1))print
  265. static Ctlr* ctlrs[Nhcis];
  266. static Tdpool tdpool;
  267. static Qhpool qhpool;
  268. static int debug;
  269. static char* qhsname[] = { "idle", "install", "run", "done", "close", "FREE" };
  270. static void
  271. uhcicmd(Ctlr *ctlr, int c)
  272. {
  273. OUTS(Cmd, c);
  274. }
  275. static void
  276. uhcirun(Ctlr *ctlr, int on)
  277. {
  278. int i;
  279. ddprint("uhci %#x setting run to %d\n", ctlr->port, on);
  280. if(on)
  281. uhcicmd(ctlr, INS(Cmd)|Crun);
  282. else
  283. uhcicmd(ctlr, INS(Cmd) & ~Crun);
  284. for(i = 0; i < 100; i++)
  285. if(on == 0 && (INS(Status) & Shalted) != 0)
  286. break;
  287. else if(on != 0 && (INS(Status) & Shalted) == 0)
  288. break;
  289. else
  290. delay(1);
  291. if(i == 100)
  292. dprint("uhci %#x run cmd timed out\n", ctlr->port);
  293. ddprint("uhci %#x cmd %#x sts %#x\n",
  294. ctlr->port, INS(Cmd), INS(Status));
  295. }
  296. static int
  297. tdlen(Td *td)
  298. {
  299. return (td->csw+1) & Tdlen;
  300. }
  301. static int
  302. maxtdlen(Td *td)
  303. {
  304. return ((td->token>>21)+1) & (Tdmaxpkt-1);
  305. }
  306. static int
  307. tdtok(Td *td)
  308. {
  309. return td->token & 0xFF;
  310. }
  311. static char*
  312. seprinttd(char *s, char *se, Td *td)
  313. {
  314. s = seprint(s, se, "%#p link %#lx", td, td->link);
  315. if((td->link & Tdvf) != 0)
  316. s = seprint(s, se, "V");
  317. if((td->link & Tdterm) != 0)
  318. s = seprint(s, se, "T");
  319. if((td->link & Tdlinkqh) != 0)
  320. s = seprint(s, se, "Q");
  321. s = seprint(s, se, " csw %#lx ", td->csw);
  322. if(td->csw & Tdactive)
  323. s = seprint(s, se, "a");
  324. if(td->csw & Tdiso)
  325. s = seprint(s, se, "I");
  326. if(td->csw & Tdioc)
  327. s = seprint(s, se, "i");
  328. if(td->csw & Tdlow)
  329. s = seprint(s, se, "l");
  330. if((td->csw & (Tderr1|Tderr2)) == 0)
  331. s = seprint(s, se, "z");
  332. if(td->csw & Tderrors)
  333. s = seprint(s, se, " err %#lx", td->csw & Tderrors);
  334. if(td->csw & Tdstalled)
  335. s = seprint(s, se, "s");
  336. if(td->csw & Tddberr)
  337. s = seprint(s, se, "d");
  338. if(td->csw & Tdbabble)
  339. s = seprint(s, se, "b");
  340. if(td->csw & Tdnak)
  341. s = seprint(s, se, "n");
  342. if(td->csw & Tdcrcto)
  343. s = seprint(s, se, "c");
  344. if(td->csw & Tdbitstuff)
  345. s = seprint(s, se, "B");
  346. s = seprint(s, se, " stslen %d", tdlen(td));
  347. s = seprint(s, se, " token %#lx", td->token);
  348. if(td->token == 0) /* the BWS loopback Td, ignore rest */
  349. return s;
  350. s = seprint(s, se, " maxlen %d", maxtdlen(td));
  351. if(td->token & Tddata1)
  352. s = seprint(s, se, " d1");
  353. else
  354. s = seprint(s, se, " d0");
  355. s = seprint(s, se, " id %#lx:", (td->token>>15) & Epmax);
  356. s = seprint(s, se, "%#lx", (td->token>>8) & Devmax);
  357. switch(tdtok(td)){
  358. case Tdtokin:
  359. s = seprint(s, se, " in");
  360. break;
  361. case Tdtokout:
  362. s = seprint(s, se, " out");
  363. break;
  364. case Tdtoksetup:
  365. s = seprint(s, se, " setup");
  366. break;
  367. default:
  368. s = seprint(s, se, " BADPID");
  369. }
  370. s = seprint(s, se, "\n\t buffer %#lx data %#p", td->buffer, td->data);
  371. s = seprint(s, se, " ndata %lu sbuff %#p buff %#p",
  372. td->ndata, td->sbuff, td->buff);
  373. if(td->ndata > 0)
  374. s = seprintdata(s, se, td->data, td->ndata);
  375. return s;
  376. }
  377. static void
  378. isodump(Isoio *iso, int all)
  379. {
  380. char buf[256];
  381. Td *td;
  382. int i;
  383. print("iso %#p %s state %d nframes %d"
  384. " td0 %#p tdu %#p tdi %#p data %#p\n",
  385. iso, iso->tok == Tdtokin ? "in" : "out",
  386. iso->state, iso->nframes, iso->tdps[iso->td0frno],
  387. iso->tdu, iso->tdi, iso->data);
  388. if(iso->err != nil)
  389. print("\terr='%s'\n", iso->err);
  390. if(all == 0){
  391. seprinttd(buf, buf+sizeof(buf), iso->tdu);
  392. print("\ttdu %s\n", buf);
  393. seprinttd(buf, buf+sizeof(buf), iso->tdi);
  394. print("\ttdi %s\n", buf);
  395. }else{
  396. td = iso->tdps[iso->td0frno];
  397. for(i = 0; i < iso->nframes; i++){
  398. seprinttd(buf, buf+sizeof(buf), td);
  399. if(td == iso->tdi)
  400. print("i->");
  401. if(td == iso->tdu)
  402. print("u->");
  403. print("\t%s\n", buf);
  404. td = td->next;
  405. }
  406. }
  407. }
  408. static int
  409. sameptr(void *p, uint32_t l)
  410. {
  411. if(l & QHterm)
  412. return p == nil;
  413. return PTR(l) == p;
  414. }
  415. static void
  416. dumptd(Td *td, char *pref)
  417. {
  418. char buf[256];
  419. char *s;
  420. char *se;
  421. int i;
  422. i = 0;
  423. se = buf+sizeof(buf);
  424. for(; td != nil; td = td->next){
  425. s = seprinttd(buf, se, td);
  426. if(!sameptr(td->next, td->link))
  427. seprint(s, se, " next %#p != link %#lx %#p",
  428. td->next, td->link, TPTR(td->link));
  429. print("%std %s\n", pref, buf);
  430. if(i++ > 20){
  431. print("...more tds...\n");
  432. break;
  433. }
  434. }
  435. }
  436. static void
  437. qhdump(Qh *qh, char *pref)
  438. {
  439. char buf[256];
  440. char *s;
  441. char *se;
  442. uint32_t td;
  443. int i;
  444. s = buf;
  445. se = buf+sizeof(buf);
  446. s = seprint(s, se, "%sqh %s %#p state %s link %#lx", pref,
  447. qh->tag, qh, qhsname[qh->state], qh->link);
  448. if(!sameptr(qh->tds, qh->elink))
  449. s = seprint(s, se, " [tds %#p != elink %#lx %#p]",
  450. qh->tds, qh->elink, TPTR(qh->elink));
  451. if(!sameptr(qh->next, qh->link))
  452. s = seprint(s, se, " [next %#p != link %#lx %#p]",
  453. qh->next, qh->link, QPTR(qh->link));
  454. if((qh->link & Tdterm) != 0)
  455. s = seprint(s, se, "T");
  456. if((qh->link & Tdlinkqh) != 0)
  457. s = seprint(s, se, "Q");
  458. s = seprint(s, se, " elink %#lx", qh->elink);
  459. if((qh->elink & Tdterm) != 0)
  460. s = seprint(s, se, "T");
  461. if((qh->elink & Tdlinkqh) != 0)
  462. s = seprint(s, se, "Q");
  463. s = seprint(s, se, " io %#p", qh->io);
  464. if(qh->io != nil && qh->io->err != nil)
  465. seprint(s, se, " err='%s'", qh->io->err);
  466. print("%s\n", buf);
  467. dumptd(qh->tds, "\t");
  468. if((qh->elink & QHterm) == 0){
  469. print("\thw tds:");
  470. i = 0;
  471. for(td = qh->elink; (td & Tdterm) == 0; td = TPTR(td)->link){
  472. print(" %#lx", td);
  473. if(td == TPTR(td)->link) /* BWS Td */
  474. break;
  475. if(i++ > 40){
  476. print("...");
  477. break;
  478. }
  479. }
  480. print("\n");
  481. }
  482. }
  483. static void
  484. xdump(Ctlr *ctlr, int doilock)
  485. {
  486. Isoio *iso;
  487. Qh *qh;
  488. int i;
  489. if(doilock){
  490. if(ctlr == ctlrs[0]){
  491. lock(&tdpool.l);
  492. print("tds: alloc %d = inuse %d + free %d\n",
  493. tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
  494. unlock(&tdpool.l);
  495. lock(&qhpool.l);
  496. print("qhs: alloc %d = inuse %d + free %d\n",
  497. qhpool.nalloc, qhpool.ninuse, qhpool.nfree);
  498. unlock(&qhpool.l);
  499. }
  500. ilock(&ctlr->l);
  501. }
  502. print("uhci port %#x frames %#p nintr %d ntdintr %d",
  503. ctlr->port, ctlr->frames, ctlr->nintr, ctlr->ntdintr);
  504. print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
  505. print("cmd %#x sts %#x fl %#lx ps1 %#x ps2 %#x frames[0] %#lx\n",
  506. INS(Cmd), INS(Status),
  507. INL(Flbaseadd), INS(PORT(0)), INS(PORT(1)),
  508. ctlr->frames[0]);
  509. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  510. isodump(iso, 1);
  511. i = 0;
  512. for(qh = ctlr->qhs; qh != nil; qh = qh->next){
  513. qhdump(qh, "");
  514. if(i++ > 20){
  515. print("qhloop\n");
  516. break;
  517. }
  518. }
  519. print("\n");
  520. if(doilock)
  521. iunlock(&ctlr->l);
  522. }
  523. static void
  524. dump(Hci *hp)
  525. {
  526. xdump(hp->Hciimpl.aux, 1);
  527. }
  528. static Td*
  529. tdalloc(void)
  530. {
  531. int i, sz;
  532. Td *td;
  533. Td *pool;
  534. sz = ROUNDUP(sizeof *td, 16);
  535. lock(&tdpool.l);
  536. if(tdpool.free == nil){
  537. ddprint("uhci: tdalloc %d Tds\n", Incr);
  538. pool = mallocalign(Incr*sz, Align, 0, 0);
  539. if(pool == nil)
  540. panic("tdalloc");
  541. for(i=Incr; --i>=0;){
  542. pool[i].next = tdpool.free;
  543. tdpool.free = &pool[i];
  544. }
  545. tdpool.nalloc += Incr;
  546. tdpool.nfree += Incr;
  547. }
  548. td = tdpool.free;
  549. tdpool.free = td->next;
  550. tdpool.ninuse++;
  551. tdpool.nfree--;
  552. unlock(&tdpool.l);
  553. memset(td, 0, sizeof(Td));
  554. td->link = Tdterm;
  555. assert(((uint64_t)td & 0xF) == 0);
  556. return td;
  557. }
  558. static void
  559. tdfree(Td *td)
  560. {
  561. if(td == nil)
  562. return;
  563. free(td->buff);
  564. td->buff = nil;
  565. lock(&tdpool.l);
  566. td->next = tdpool.free;
  567. tdpool.free = td;
  568. tdpool.ninuse--;
  569. tdpool.nfree++;
  570. unlock(&tdpool.l);
  571. }
  572. static void
  573. qhlinkqh(Qh* qh, Qh* next)
  574. {
  575. if(next == nil)
  576. qh->link = QHterm;
  577. else{
  578. next->link = qh->link;
  579. next->next = qh->next;
  580. qh->link = PCIWADDR(next)|QHlinkqh;
  581. }
  582. qh->next = next;
  583. }
  584. static void
  585. qhlinktd(Qh *qh, Td *td)
  586. {
  587. qh->tds = td;
  588. if(td == nil)
  589. qh->elink = QHvf|QHterm;
  590. else
  591. qh->elink = PCIWADDR(td);
  592. }
  593. static void
  594. tdlinktd(Td *td, Td *next)
  595. {
  596. td->next = next;
  597. if(next == nil)
  598. td->link = Tdterm;
  599. else
  600. td->link = PCIWADDR(next)|Tdvf;
  601. }
  602. static Qh*
  603. qhalloc(Ctlr *ctlr, Qh *prev, Qio *io, char *tag)
  604. {
  605. int i, sz;
  606. Qh *qh;
  607. Qh *pool;
  608. sz = ROUNDUP(sizeof *qh, 16);
  609. lock(&qhpool.l);
  610. if(qhpool.free == nil){
  611. ddprint("uhci: qhalloc %d Qhs\n", Incr);
  612. pool = mallocalign(Incr*sz, Align, 0, 0);
  613. if(pool == nil)
  614. panic("qhalloc");
  615. for(i=Incr; --i>=0;){
  616. pool[i].next = qhpool.free;
  617. qhpool.free = &pool[i];
  618. }
  619. qhpool.nalloc += Incr;
  620. qhpool.nfree += Incr;
  621. }
  622. qh = qhpool.free;
  623. qhpool.free = qh->next;
  624. qh->next = nil;
  625. qh->link = QHterm;
  626. qhpool.ninuse++;
  627. qhpool.nfree--;
  628. unlock(&qhpool.l);
  629. qh->tds = nil;
  630. qh->elink = QHterm;
  631. qh->state = Qidle;
  632. qh->io = io;
  633. qh->tag = nil;
  634. kstrdup(&qh->tag, tag);
  635. if(prev != nil){
  636. coherence();
  637. ilock(&ctlr->l);
  638. qhlinkqh(prev, qh);
  639. iunlock(&ctlr->l);
  640. }
  641. if(((uint64_t)qh & 0xF) != 0)
  642. panic("usbuhci: qhalloc qh 0x%p (not 16-aligned)", qh);
  643. return qh;
  644. }
  645. static void
  646. qhfree(Ctlr *ctlr, Qh *qh)
  647. {
  648. Td *td;
  649. Td *ltd;
  650. Qh *q;
  651. if(qh == nil)
  652. return;
  653. ilock(&ctlr->l);
  654. for(q = ctlr->qhs; q != nil; q = q->next)
  655. if(q->next == qh)
  656. break;
  657. if(q == nil)
  658. panic("qhfree: nil q");
  659. q->next = qh->next;
  660. q->link = qh->link;
  661. iunlock(&ctlr->l);
  662. for(td = qh->tds; td != nil; td = ltd){
  663. ltd = td->next;
  664. tdfree(td);
  665. }
  666. lock(&qhpool.l);
  667. qh->state = Qfree; /* paranoia */
  668. qh->next = qhpool.free;
  669. qh->tag = nil;
  670. qh->io = nil;
  671. qhpool.free = qh;
  672. qhpool.ninuse--;
  673. qhpool.nfree++;
  674. unlock(&qhpool.l);
  675. ddprint("qhfree: qh %#p\n", qh);
  676. }
  677. static char*
  678. errmsg(int err)
  679. {
  680. if(err == 0)
  681. return "ok";
  682. if(err & Tdcrcto)
  683. return "crc/timeout error";
  684. if(err & Tdbabble)
  685. return "babble detected";
  686. if(err & Tddberr)
  687. return "db error";
  688. if(err & Tdbitstuff)
  689. return "bit stuffing error";
  690. if(err & Tdstalled)
  691. return Estalled;
  692. return Eio;
  693. }
  694. static int
  695. isocanread(void *a)
  696. {
  697. Isoio *iso;
  698. iso = a;
  699. return iso->state == Qclose ||
  700. (iso->state == Qrun &&
  701. iso->tok == Tdtokin && iso->tdi != iso->tdu);
  702. }
  703. static int
  704. isocanwrite(void *a)
  705. {
  706. Isoio *iso;
  707. iso = a;
  708. return iso->state == Qclose ||
  709. (iso->state == Qrun &&
  710. iso->tok == Tdtokout && iso->tdu->next != iso->tdi);
  711. }
  712. static void
  713. tdisoinit(Isoio *iso, Td *td, int32_t count)
  714. {
  715. td->ndata = count;
  716. td->token = ((count-1)<<21)| ((iso->usbid & 0x7FF)<<8) | iso->tok;
  717. td->csw = Tderr1|Tdiso|Tdactive|Tdioc;
  718. }
  719. /*
  720. * Process Iso i/o on interrupt. For writes update just error status.
  721. * For reads update tds to reflect data and also error status.
  722. * When tdi aproaches tdu, advance tdu; data may be lost.
  723. * (If nframes is << Nframes tdu might be far away but this avoids
  724. * races regarding frno.)
  725. * If we suffer errors for more than half the frames we stall.
  726. */
  727. static void
  728. isointerrupt(Ctlr *ctlr, Isoio* iso)
  729. {
  730. Td *tdi;
  731. int err;
  732. int i;
  733. int nframes;
  734. tdi = iso->tdi;
  735. if((tdi->csw & Tdactive) != 0) /* nothing new done */
  736. return;
  737. ctlr->nisointr++;
  738. ddiprint("isointr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
  739. if(iso->state != Qrun && iso->state != Qdone)
  740. panic("isointr: iso state");
  741. if(debug > 1 || iso->debug > 1)
  742. isodump(iso, 0);
  743. nframes = iso->nframes / 2; /* limit how many we look */
  744. if(nframes > 64)
  745. nframes = 64;
  746. for(i = 0; i < nframes && (tdi->csw & Tdactive) == 0; i++){
  747. tdi->csw &= ~Tdioc;
  748. err = tdi->csw & Tderrors;
  749. if(err == 0)
  750. iso->nerrs = 0;
  751. else if(iso->nerrs++ > iso->nframes/2)
  752. tdi->csw |= Tdstalled;
  753. if((tdi->csw & Tdstalled) != 0){
  754. if(iso->err == nil){
  755. iso->err = errmsg(err);
  756. diprint("isointerrupt: tdi %#p error %#x %s\n",
  757. tdi, err, iso->err);
  758. diprint("ctlr load %lu\n", ctlr->load);
  759. }
  760. tdi->ndata = 0;
  761. }else
  762. tdi->ndata = tdlen(tdi);
  763. if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
  764. memset(iso->tdu->data, 0, maxtdlen(iso->tdu));
  765. tdisoinit(iso, iso->tdu, maxtdlen(iso->tdu));
  766. iso->tdu = iso->tdu->next;
  767. iso->nleft = 0;
  768. }
  769. tdi = tdi->next;
  770. }
  771. ddiprint("isointr: %d frames processed\n", nframes);
  772. if(i == nframes)
  773. tdi->csw |= Tdioc;
  774. iso->tdi = tdi;
  775. if(isocanwrite(iso) || isocanread(iso)){
  776. diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
  777. iso->tdi, iso->tdu);
  778. wakeup(&iso->Rendez);
  779. }
  780. }
  781. /*
  782. * Process a Qh upon interrupt. There's one per ongoing user I/O.
  783. * User process releases resources later, that is not done here.
  784. * We may find in this order one or more Tds:
  785. * - none/many non active and completed Tds
  786. * - none/one (usually(!) not active) and failed Td
  787. * - none/many active Tds.
  788. * Upon errors the entire transfer is aborted and error reported.
  789. * Otherwise, the transfer is complete only when all Tds are done or
  790. * when a read with less than maxpkt is found.
  791. * Use the software list and not qh->elink to avoid races.
  792. * We could use qh->elink to see if there's something new or not.
  793. */
  794. static void
  795. qhinterrupt(Ctlr *ctlr, Qh *qh)
  796. {
  797. Td *td;
  798. int err;
  799. ctlr->nqhintr++;
  800. if(qh->state != Qrun)
  801. panic("qhinterrupt: qh state");
  802. if(qh->tds == nil)
  803. panic("qhinterrupt: no tds");
  804. if((qh->tds->csw & Tdactive) == 0)
  805. ddqprint("qhinterrupt port %#x qh %#p p0 %#x p1 %#x\n",
  806. ctlr->port, qh, INS(PORT(0)), INS(PORT(1)));
  807. for(td = qh->tds; td != nil; td = td->next){
  808. if(td->csw & Tdactive)
  809. return;
  810. td->csw &= ~Tdioc;
  811. if((td->csw & Tdstalled) != 0){
  812. err = td->csw & Tderrors;
  813. /* just stalled is end of xfer but not an error */
  814. if(err != Tdstalled && qh->io->err == nil){
  815. qh->io->err = errmsg(td->csw & Tderrors);
  816. dqprint("qhinterrupt: td %#p error %#x %s\n",
  817. td, err, qh->io->err);
  818. dqprint("ctlr load %lu\n", ctlr->load);
  819. }
  820. break;
  821. }
  822. if((td->csw & Tdnak) != 0){ /* retransmit; not serious */
  823. td->csw &= ~Tdnak;
  824. if(td->next == nil)
  825. td->csw |= Tdioc;
  826. }
  827. td->ndata = tdlen(td);
  828. if(td->ndata < maxtdlen(td)){ /* EOT */
  829. td = td->next;
  830. break;
  831. }
  832. }
  833. /*
  834. * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
  835. */
  836. qh->elink = QHterm;
  837. for(; td != nil; td = td->next)
  838. td->ndata = 0;
  839. qh->state = Qdone;
  840. wakeup(&qh->io->Rendez);
  841. }
  842. static void
  843. interrupt(Ureg *ureg, void *a)
  844. {
  845. Hci *hp;
  846. Ctlr *ctlr;
  847. int frptr;
  848. int frno;
  849. Qh *qh;
  850. Isoio *iso;
  851. int sts;
  852. int cmd;
  853. hp = a;
  854. ctlr = hp->Hciimpl.aux;
  855. ilock(&ctlr->l);
  856. ctlr->nintr++;
  857. sts = INS(Status);
  858. if((sts & Sall) == 0){ /* not for us; sharing irq */
  859. iunlock(&ctlr->l);
  860. return;
  861. }
  862. OUTS(Status, sts & Sall);
  863. cmd = INS(Cmd);
  864. if((cmd & Crun) == 0){
  865. print("uhci %#x: not running: uhci bug?\n", ctlr->port);
  866. /* BUG: should abort everything in this case */
  867. }
  868. if(debug > 1){
  869. frptr = INL(Flbaseadd);
  870. frno = INL(Frnum);
  871. frno = TRUNC(frno, Nframes);
  872. print("cmd %#x sts %#x frptr %#x frno %d\n",
  873. cmd, sts, frptr, frno);
  874. }
  875. ctlr->ntdintr++;
  876. /*
  877. * Will we know in USB 3.0 who the interrupt was for?.
  878. * Do they still teach indexing in CS?
  879. * This is Intel's doing.
  880. */
  881. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  882. if(iso->state == Qrun || iso->state == Qdone)
  883. isointerrupt(ctlr, iso);
  884. for(qh = ctlr->qhs; qh != nil; qh = qh->next)
  885. if(qh->state == Qrun)
  886. qhinterrupt(ctlr, qh);
  887. else if(qh->state == Qclose)
  888. qhlinktd(qh, nil);
  889. iunlock(&ctlr->l);
  890. }
  891. /*
  892. * iso->tdu is the next place to put data. When it gets full
  893. * it is activated and tdu advanced.
  894. */
  895. static int32_t
  896. putsamples(Isoio *iso, unsigned char *b, int32_t count)
  897. {
  898. int32_t tot;
  899. int32_t n;
  900. for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
  901. n = count-tot;
  902. if(n > maxtdlen(iso->tdu) - iso->nleft)
  903. n = maxtdlen(iso->tdu) - iso->nleft;
  904. memmove(iso->tdu->data+iso->nleft, b+tot, n);
  905. iso->nleft += n;
  906. if(iso->nleft == maxtdlen(iso->tdu)){
  907. tdisoinit(iso, iso->tdu, iso->nleft);
  908. iso->nleft = 0;
  909. iso->tdu = iso->tdu->next;
  910. }
  911. }
  912. return tot;
  913. }
  914. /*
  915. * Queue data for writing and return error status from
  916. * last writes done, to maintain buffered data.
  917. */
  918. static int32_t
  919. episowrite(Ep *ep, Isoio *iso, void *a, int32_t count)
  920. {
  921. Proc *up = externup();
  922. Ctlr *ctlr;
  923. unsigned char *b;
  924. int tot;
  925. int nw;
  926. char *err;
  927. iso->debug = ep->debug;
  928. diprint("uhci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  929. ctlr = ep->hp->Hciimpl.aux;
  930. qlock(&iso->ql);
  931. if(waserror()){
  932. qunlock(&iso->ql);
  933. nexterror();
  934. }
  935. ilock(&ctlr->l);
  936. if(iso->state == Qclose){
  937. iunlock(&ctlr->l);
  938. error(iso->err ? iso->err : Eio);
  939. }
  940. iso->state = Qrun;
  941. b = a;
  942. for(tot = 0; tot < count; tot += nw){
  943. while(isocanwrite(iso) == 0){
  944. iunlock(&ctlr->l);
  945. diprint("uhci: episowrite: %#p sleep\n", iso);
  946. if(waserror()){
  947. if(iso->err == nil)
  948. iso->err = "I/O timed out";
  949. ilock(&ctlr->l);
  950. break;
  951. }
  952. tsleep(&iso->Rendez, isocanwrite, iso, ep->tmout);
  953. poperror();
  954. ilock(&ctlr->l);
  955. }
  956. err = iso->err;
  957. iso->err = nil;
  958. if(iso->state == Qclose || err != nil){
  959. iunlock(&ctlr->l);
  960. error(err ? err : Eio);
  961. }
  962. if(iso->state != Qrun)
  963. panic("episowrite: iso not running");
  964. iunlock(&ctlr->l); /* We could page fault here */
  965. nw = putsamples(iso, b+tot, count-tot);
  966. ilock(&ctlr->l);
  967. }
  968. if(iso->state != Qclose)
  969. iso->state = Qdone;
  970. iunlock(&ctlr->l);
  971. err = iso->err; /* in case it failed early */
  972. iso->err = nil;
  973. qunlock(&iso->ql);
  974. poperror();
  975. if(err != nil)
  976. error(err);
  977. diprint("uhci: episowrite: %#p %d bytes\n", iso, tot);
  978. return tot;
  979. }
  980. /*
  981. * Available data is kept at tdu and following tds, up to tdi (excluded).
  982. */
  983. static int32_t
  984. episoread(Ep *ep, Isoio *iso, void *a, int count)
  985. {
  986. Proc *up = externup();
  987. Ctlr *ctlr;
  988. unsigned char *b;
  989. int nr;
  990. int tot;
  991. Td *tdu;
  992. iso->debug = ep->debug;
  993. diprint("uhci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  994. b = a;
  995. ctlr = ep->hp->Hciimpl.aux;
  996. qlock(&iso->ql);
  997. if(waserror()){
  998. qunlock(&iso->ql);
  999. nexterror();
  1000. }
  1001. iso->err = nil;
  1002. iso->nerrs = 0;
  1003. ilock(&ctlr->l);
  1004. if(iso->state == Qclose){
  1005. iunlock(&ctlr->l);
  1006. error(iso->err ? iso->err : Eio);
  1007. }
  1008. iso->state = Qrun;
  1009. while(isocanread(iso) == 0){
  1010. iunlock(&ctlr->l);
  1011. diprint("uhci: episoread: %#p sleep\n", iso);
  1012. if(waserror()){
  1013. if(iso->err == nil)
  1014. iso->err = "I/O timed out";
  1015. ilock(&ctlr->l);
  1016. break;
  1017. }
  1018. tsleep(&iso->Rendez, isocanread, iso, ep->tmout);
  1019. poperror();
  1020. ilock(&ctlr->l);
  1021. }
  1022. if(iso->state == Qclose){
  1023. iunlock(&ctlr->l);
  1024. error(iso->err ? iso->err : Eio);
  1025. }
  1026. iso->state = Qdone;
  1027. assert(iso->tdu != iso->tdi);
  1028. for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
  1029. tdu = iso->tdu;
  1030. if(tdu->csw & Tdactive){
  1031. diprint("uhci: episoread: %#p tdu active\n", iso);
  1032. break;
  1033. }
  1034. nr = tdu->ndata;
  1035. if(tot + nr > count)
  1036. nr = count - tot;
  1037. if(nr == 0)
  1038. print("uhci: ep%d.%d: too many polls\n",
  1039. ep->dev->nb, ep->nb);
  1040. else{
  1041. iunlock(&ctlr->l); /* We could page fault here */
  1042. memmove(b+tot, tdu->data, nr);
  1043. ilock(&ctlr->l);
  1044. if(nr < tdu->ndata)
  1045. memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
  1046. tdu->ndata -= nr;
  1047. }
  1048. if(tdu->ndata == 0){
  1049. tdisoinit(iso, tdu, ep->maxpkt);
  1050. iso->tdu = tdu->next;
  1051. }
  1052. }
  1053. iunlock(&ctlr->l);
  1054. qunlock(&iso->ql);
  1055. poperror();
  1056. diprint("uhci: episoread: %#p %d bytes err '%s'\n", iso, tot, iso->err);
  1057. if(iso->err != nil)
  1058. error(iso->err);
  1059. return tot;
  1060. }
  1061. static int
  1062. nexttoggle(int tog)
  1063. {
  1064. if(tog == Tddata0)
  1065. return Tddata1;
  1066. else
  1067. return Tddata0;
  1068. }
  1069. static Td*
  1070. epgettd(Ep *ep, Qio *io, int flags, void *a, int count)
  1071. {
  1072. Td *td;
  1073. int tok;
  1074. if(ep->maxpkt < count)
  1075. error("maxpkt too short");
  1076. td = tdalloc();
  1077. if(count <= Tdndata)
  1078. td->data = td->sbuff;
  1079. else
  1080. td->data = td->buff = smalloc(ep->maxpkt);
  1081. td->buffer = PCIWADDR(td->data);
  1082. td->ndata = count;
  1083. if(a != nil && count > 0)
  1084. memmove(td->data, a, count);
  1085. td->csw = Tderr2|Tderr1|flags;
  1086. if(ep->dev->speed == Lowspeed)
  1087. td->csw |= Tdlow;
  1088. tok = io->tok | io->toggle;
  1089. io->toggle = nexttoggle(io->toggle);
  1090. td->token = ((count-1)<<21) | ((io->usbid&0x7FF)<<8) | tok;
  1091. return td;
  1092. }
  1093. /*
  1094. * Try to get them idle
  1095. */
  1096. static void
  1097. aborttds(Qh *qh)
  1098. {
  1099. Td *td;
  1100. qh->state = Qdone;
  1101. qh->elink = QHterm;
  1102. for(td = qh->tds; td != nil; td = td->next){
  1103. if(td->csw & Tdactive)
  1104. td->ndata = 0;
  1105. td->csw &= ~(Tdactive|Tdioc);
  1106. }
  1107. }
  1108. static int
  1109. epiodone(void *a)
  1110. {
  1111. Qh *qh;
  1112. qh = a;
  1113. return qh->state != Qrun;
  1114. }
  1115. static void
  1116. epiowait(Ctlr *ctlr, Qio *io, int tmout, uint32_t load)
  1117. {
  1118. Proc *up = externup();
  1119. Qh *qh;
  1120. int timedout;
  1121. qh = io->qh;
  1122. ddqprint("uhci io %#p sleep on qh %#p state %lu\n", io, qh, qh->state);
  1123. timedout = 0;
  1124. if(waserror()){
  1125. dqprint("uhci io %#p qh %#p timed out\n", io, qh);
  1126. timedout++;
  1127. }else{
  1128. if(tmout == 0)
  1129. sleep(&io->Rendez, epiodone, qh);
  1130. else
  1131. tsleep(&io->Rendez, epiodone, qh, tmout);
  1132. poperror();
  1133. }
  1134. ilock(&ctlr->l);
  1135. if(qh->state == Qrun)
  1136. timedout = 1;
  1137. else if(qh->state != Qdone && qh->state != Qclose)
  1138. panic("epio: queue not done and not closed");
  1139. if(timedout){
  1140. aborttds(io->qh);
  1141. io->err = "request timed out";
  1142. iunlock(&ctlr->l);
  1143. if(!waserror()){
  1144. tsleep(&up->sleep, return0, 0, Abortdelay);
  1145. poperror();
  1146. }
  1147. ilock(&ctlr->l);
  1148. }
  1149. if(qh->state != Qclose)
  1150. qh->state = Qidle;
  1151. qhlinktd(qh, nil);
  1152. ctlr->load -= load;
  1153. iunlock(&ctlr->l);
  1154. }
  1155. /*
  1156. * Non iso I/O.
  1157. * To make it work for control transfers, the caller may
  1158. * lock the Qio for the entire control transfer.
  1159. */
  1160. static int32_t
  1161. epio(Ep *ep, Qio *io, void *a, int32_t count, int mustlock)
  1162. {
  1163. Proc *up = externup();
  1164. Td *td, *ltd, *td0, *ntd;
  1165. Ctlr *ctlr;
  1166. Qh* qh;
  1167. int32_t n, tot;
  1168. char buf[128];
  1169. unsigned char *c;
  1170. int saved, ntds, tmout;
  1171. uint32_t load;
  1172. char *err;
  1173. qh = io->qh;
  1174. ctlr = ep->hp->Hciimpl.aux;
  1175. io->debug = ep->debug;
  1176. tmout = ep->tmout;
  1177. ddeprint("epio: %s ep%d.%d io %#p count %ld load %lu\n",
  1178. io->tok == Tdtokin ? "in" : "out",
  1179. ep->dev->nb, ep->nb, io, count, ctlr->load);
  1180. if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
  1181. seprintdata(buf, buf+sizeof(buf), a, count);
  1182. print("uchi epio: user data: %s\n", buf);
  1183. }
  1184. if(mustlock){
  1185. qlock(&io->ql);
  1186. if(waserror()){
  1187. qunlock(&io->ql);
  1188. nexterror();
  1189. }
  1190. }
  1191. io->err = nil;
  1192. ilock(&ctlr->l);
  1193. if(qh->state == Qclose){ /* Tds released by cancelio */
  1194. iunlock(&ctlr->l);
  1195. error(io->err ? io->err : Eio);
  1196. }
  1197. if(qh->state != Qidle)
  1198. panic("epio: qh not idle");
  1199. qh->state = Qinstall;
  1200. iunlock(&ctlr->l);
  1201. c = a;
  1202. td0 = ltd = nil;
  1203. load = tot = 0;
  1204. do{
  1205. n = ep->maxpkt;
  1206. if(count-tot < n)
  1207. n = count-tot;
  1208. if(c != nil && io->tok != Tdtokin)
  1209. td = epgettd(ep, io, Tdactive, c+tot, n);
  1210. else
  1211. td = epgettd(ep, io, Tdactive|Tdspd, nil, n);
  1212. if(td0 == nil)
  1213. td0 = td;
  1214. else
  1215. tdlinktd(ltd, td);
  1216. ltd = td;
  1217. tot += n;
  1218. load += ep->load;
  1219. }while(tot < count);
  1220. if(td0 == nil || ltd == nil)
  1221. panic("epio: no td");
  1222. ltd->csw |= Tdioc; /* the last one interrupts */
  1223. ddeprint("uhci: load %lu ctlr load %lu\n", load, ctlr->load);
  1224. ilock(&ctlr->l);
  1225. if(qh->state != Qclose){
  1226. io->iotime = TK2MS(machp()->ticks);
  1227. qh->state = Qrun;
  1228. coherence();
  1229. qhlinktd(qh, td0);
  1230. ctlr->load += load;
  1231. }
  1232. iunlock(&ctlr->l);
  1233. epiowait(ctlr, io, tmout, load);
  1234. if(debug > 1 || ep->debug > 1)
  1235. dumptd(td0, "epio: got tds: ");
  1236. tot = 0;
  1237. c = a;
  1238. saved = 0;
  1239. ntds = 0;
  1240. for(td = td0; td != nil; td = ntd){
  1241. ntds++;
  1242. /*
  1243. * Use td tok, not io tok, because of setup packets.
  1244. * Also, if the Td was stalled or active (previous Td
  1245. * was a short packet), we must save the toggle as it is.
  1246. */
  1247. if(td->csw & (Tdstalled|Tdactive)){
  1248. if(saved++ == 0)
  1249. io->toggle = td->token & Tddata1;
  1250. }else{
  1251. tot += td->ndata;
  1252. if(c != nil && tdtok(td) == Tdtokin && td->ndata > 0){
  1253. memmove(c, td->data, td->ndata);
  1254. c += td->ndata;
  1255. }
  1256. }
  1257. ntd = td->next;
  1258. tdfree(td);
  1259. }
  1260. err = io->err;
  1261. if(mustlock){
  1262. qunlock(&io->ql);
  1263. poperror();
  1264. }
  1265. ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
  1266. io, ntds, tot, err);
  1267. if(err != nil)
  1268. error(err);
  1269. if(tot < 0)
  1270. error(Eio);
  1271. return tot;
  1272. }
  1273. /*
  1274. * halt condition was cleared on the endpoint. update our toggles.
  1275. */
  1276. static void
  1277. clrhalt(Ep *ep)
  1278. {
  1279. Qio *io;
  1280. ep->clrhalt = 0;
  1281. switch(ep->ttype){
  1282. case Tbulk:
  1283. case Tintr:
  1284. io = ep->aux;
  1285. if(ep->mode != OREAD){
  1286. qlock(&io[OWRITE].ql);
  1287. io[OWRITE].toggle = Tddata0;
  1288. deprint("ep clrhalt for io %#p\n", io+OWRITE);
  1289. qunlock(&io[OWRITE].ql);
  1290. }
  1291. if(ep->mode != OWRITE){
  1292. qlock(&io[OREAD].ql);
  1293. io[OREAD].toggle = Tddata0;
  1294. deprint("ep clrhalt for io %#p\n", io+OREAD);
  1295. qunlock(&io[OREAD].ql);
  1296. }
  1297. break;
  1298. }
  1299. }
  1300. static int32_t
  1301. epread(Ep *ep, void *a, int32_t count)
  1302. {
  1303. Proc *up = externup();
  1304. Ctlio *cio;
  1305. Qio *io;
  1306. Isoio *iso;
  1307. char buf[160];
  1308. uint32_t delta;
  1309. ddeprint("uhci: epread\n");
  1310. if(ep->aux == nil)
  1311. panic("epread: not open");
  1312. switch(ep->ttype){
  1313. case Tctl:
  1314. cio = ep->aux;
  1315. qlock(&cio->Qio.ql);
  1316. if(waserror()){
  1317. qunlock(&cio->Qio.ql);
  1318. nexterror();
  1319. }
  1320. ddeprint("epread ctl ndata %d\n", cio->ndata);
  1321. if(cio->ndata < 0)
  1322. error("request expected");
  1323. else if(cio->ndata == 0){
  1324. cio->ndata = -1;
  1325. count = 0;
  1326. }else{
  1327. if(count > cio->ndata)
  1328. count = cio->ndata;
  1329. if(count > 0)
  1330. memmove(a, cio->data, count);
  1331. /* BUG for big transfers */
  1332. free(cio->data);
  1333. cio->data = nil;
  1334. cio->ndata = 0; /* signal EOF next time */
  1335. }
  1336. qunlock(&cio->Qio.ql);
  1337. poperror();
  1338. if(debug>1 || ep->debug){
  1339. seprintdata(buf, buf+sizeof(buf), a, count);
  1340. print("epread: %s\n", buf);
  1341. }
  1342. return count;
  1343. case Tbulk:
  1344. io = ep->aux;
  1345. if(ep->clrhalt)
  1346. clrhalt(ep);
  1347. return epio(ep, &io[OREAD], a, count, 1);
  1348. case Tintr:
  1349. io = ep->aux;
  1350. delta = TK2MS(machp()->ticks) - io[OREAD].iotime + 1;
  1351. if(delta < ep->pollival / 2)
  1352. tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
  1353. if(ep->clrhalt)
  1354. clrhalt(ep);
  1355. return epio(ep, &io[OREAD], a, count, 1);
  1356. case Tiso:
  1357. iso = ep->aux;
  1358. return episoread(ep, iso, a, count);
  1359. default:
  1360. panic("epread: bad ep ttype %d", ep->ttype);
  1361. }
  1362. return -1;
  1363. }
  1364. /*
  1365. * Control transfers are one setup write (data0)
  1366. * plus zero or more reads/writes (data1, data0, ...)
  1367. * plus a final write/read with data1 to ack.
  1368. * For both host to device and device to host we perform
  1369. * the entire transfer when the user writes the request,
  1370. * and keep any data read from the device for a later read.
  1371. * We call epio three times instead of placing all Tds at
  1372. * the same time because doing so leads to crc/tmout errors
  1373. * for some devices.
  1374. * Upon errors on the data phase we must still run the status
  1375. * phase or the device may cease responding in the future.
  1376. */
  1377. static int32_t
  1378. epctlio(Ep *ep, Ctlio *cio, void *a, int32_t count)
  1379. {
  1380. Proc *up = externup();
  1381. unsigned char *c;
  1382. int32_t len;
  1383. ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
  1384. cio, ep->dev->nb, ep->nb, count);
  1385. if(count < Rsetuplen)
  1386. error("short usb comand");
  1387. qlock(&cio->Qio.ql);
  1388. free(cio->data);
  1389. cio->data = nil;
  1390. cio->ndata = 0;
  1391. if(waserror()){
  1392. qunlock(&cio->Qio.ql);
  1393. free(cio->data);
  1394. cio->data = nil;
  1395. cio->ndata = 0;
  1396. nexterror();
  1397. }
  1398. /* set the address if unset and out of configuration state */
  1399. if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
  1400. if(cio->Qio.usbid == 0)
  1401. cio->Qio.usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb&Devmax);
  1402. c = a;
  1403. cio->Qio.tok = Tdtoksetup;
  1404. cio->Qio.toggle = Tddata0;
  1405. if(epio(ep, &cio->Qio, a, Rsetuplen, 0) < Rsetuplen)
  1406. error(Eio);
  1407. a = c + Rsetuplen;
  1408. count -= Rsetuplen;
  1409. cio->Qio.toggle = Tddata1;
  1410. if(c[Rtype] & Rd2h){
  1411. cio->Qio.tok = Tdtokin;
  1412. len = GET2(c+Rcount);
  1413. if(len <= 0)
  1414. error("bad length in d2h request");
  1415. if(len > Maxctllen)
  1416. error("d2h data too large to fit in uhci");
  1417. a = cio->data = smalloc(len+1);
  1418. }else{
  1419. cio->Qio.tok = Tdtokout;
  1420. len = count;
  1421. }
  1422. if(len > 0) {
  1423. if(waserror())
  1424. len = -1;
  1425. else{
  1426. len = epio(ep, &cio->Qio, a, len, 0);
  1427. poperror();
  1428. }
  1429. }
  1430. if(c[Rtype] & Rd2h){
  1431. count = Rsetuplen;
  1432. cio->ndata = len;
  1433. cio->Qio.tok = Tdtokout;
  1434. }else{
  1435. if(len < 0)
  1436. count = -1;
  1437. else
  1438. count = Rsetuplen + len;
  1439. cio->Qio.tok = Tdtokin;
  1440. }
  1441. cio->Qio.toggle = Tddata1;
  1442. epio(ep, &cio->Qio, nil, 0, 0);
  1443. qunlock(&cio->Qio.ql);
  1444. poperror();
  1445. ddeprint("epctlio cio %#p return %ld\n", cio, count);
  1446. return count;
  1447. }
  1448. static int32_t
  1449. epwrite(Ep *ep, void *a, int32_t count)
  1450. {
  1451. Proc *up = externup();
  1452. Ctlio *cio;
  1453. Isoio *iso;
  1454. Qio *io;
  1455. uint32_t delta;
  1456. char *b;
  1457. int tot;
  1458. int nw;
  1459. ddeprint("uhci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
  1460. if(ep->aux == nil)
  1461. panic("uhci: epwrite: not open");
  1462. switch(ep->ttype){
  1463. case Tctl:
  1464. cio = ep->aux;
  1465. return epctlio(ep, cio, a, count);
  1466. case Tbulk:
  1467. io = ep->aux;
  1468. if(ep->clrhalt)
  1469. clrhalt(ep);
  1470. /*
  1471. * Put at most Tdatomic Tds (512 bytes) at a time.
  1472. * Otherwise some devices produce babble errors.
  1473. */
  1474. b = a;
  1475. for(tot = 0; tot < count ; tot += nw){
  1476. nw = count - tot;
  1477. if(nw > Tdatomic * ep->maxpkt)
  1478. nw = Tdatomic * ep->maxpkt;
  1479. nw = epio(ep, &io[OWRITE], b+tot, nw, 1);
  1480. }
  1481. return tot;
  1482. case Tintr:
  1483. io = ep->aux;
  1484. delta = TK2MS(machp()->ticks) - io[OWRITE].iotime + 1;
  1485. if(delta < ep->pollival)
  1486. tsleep(&up->sleep, return0, 0, ep->pollival - delta);
  1487. if(ep->clrhalt)
  1488. clrhalt(ep);
  1489. return epio(ep, &io[OWRITE], a, count, 1);
  1490. case Tiso:
  1491. iso = ep->aux;
  1492. return episowrite(ep, iso, a, count);
  1493. default:
  1494. panic("uhci: epwrite: bad ep ttype %d", ep->ttype);
  1495. }
  1496. return -1;
  1497. }
  1498. static void
  1499. isoopen(Ep *ep)
  1500. {
  1501. Ctlr *ctlr;
  1502. Isoio *iso;
  1503. int frno;
  1504. int i;
  1505. Td* td;
  1506. Td* ltd;
  1507. int size;
  1508. int left;
  1509. if(ep->mode == ORDWR)
  1510. error("iso i/o is half-duplex");
  1511. ctlr = ep->hp->Hciimpl.aux;
  1512. iso = ep->aux;
  1513. iso->debug = ep->debug;
  1514. iso->next = nil; /* paranoia */
  1515. if(ep->mode == OREAD)
  1516. iso->tok = Tdtokin;
  1517. else
  1518. iso->tok = Tdtokout;
  1519. iso->usbid = ((ep->nb & Epmax)<<7)|(ep->dev->nb & Devmax);
  1520. iso->state = Qidle;
  1521. iso->nframes = Nframes/ep->pollival;
  1522. if(iso->nframes < 3)
  1523. error("uhci isoopen bug"); /* we need at least 3 tds */
  1524. ilock(&ctlr->l);
  1525. if(ctlr->load + ep->load > 800)
  1526. print("usb: uhci: bandwidth may be exceeded\n");
  1527. ctlr->load += ep->load;
  1528. ctlr->isoload += ep->load;
  1529. dprint("uhci: load %lu isoload %lu\n", ctlr->load, ctlr->isoload);
  1530. iunlock(&ctlr->l);
  1531. /*
  1532. * From here on this cannot raise errors
  1533. * unless we catch them and release here all memory allocated.
  1534. */
  1535. if(ep->maxpkt > Tdndata)
  1536. iso->data = smalloc(iso->nframes*ep->maxpkt);
  1537. ilock(&ctlr->l);
  1538. frno = INS(Frnum) + 10; /* start 10ms ahead */
  1539. frno = TRUNC(frno, Nframes);
  1540. iunlock(&ctlr->l);
  1541. iso->td0frno = frno;
  1542. ltd = nil;
  1543. left = 0;
  1544. for(i = 0; i < iso->nframes; i++){
  1545. td = iso->tdps[frno] = tdalloc();
  1546. if(ep->mode == OREAD)
  1547. size = ep->maxpkt;
  1548. else{
  1549. size = (ep->hz+left) * ep->pollival / 1000;
  1550. size *= ep->samplesz;
  1551. left = (ep->hz+left) * ep->pollival % 1000;
  1552. if(size > ep->maxpkt){
  1553. print("uhci: ep%d.%d: size > maxpkt\n",
  1554. ep->dev->nb, ep->nb);
  1555. print("size = %d max = %ld\n", size, ep->maxpkt);
  1556. size = ep->maxpkt;
  1557. }
  1558. }
  1559. if(size > Tdndata)
  1560. td->data = iso->data + i * ep->maxpkt;
  1561. else
  1562. td->data = td->sbuff;
  1563. td->buffer = PCIWADDR(td->data);
  1564. tdisoinit(iso, td, size);
  1565. if(ltd != nil)
  1566. ltd->next = td;
  1567. ltd = td;
  1568. frno = TRUNC(frno+ep->pollival, Nframes);
  1569. }
  1570. ltd->next = iso->tdps[iso->td0frno];
  1571. iso->tdi = iso->tdps[iso->td0frno];
  1572. iso->tdu = iso->tdi; /* read: right now; write: 1s ahead */
  1573. ilock(&ctlr->l);
  1574. frno = iso->td0frno;
  1575. for(i = 0; i < iso->nframes; i++){
  1576. iso->tdps[frno]->link = ctlr->frames[frno];
  1577. frno = TRUNC(frno+ep->pollival, Nframes);
  1578. }
  1579. coherence();
  1580. frno = iso->td0frno;
  1581. for(i = 0; i < iso->nframes; i++){
  1582. ctlr->frames[frno] = PCIWADDR(iso->tdps[frno]);
  1583. frno = TRUNC(frno+ep->pollival, Nframes);
  1584. }
  1585. iso->next = ctlr->iso;
  1586. ctlr->iso = iso;
  1587. iso->state = Qdone;
  1588. iunlock(&ctlr->l);
  1589. if(debug > 1 || iso->debug >1)
  1590. isodump(iso, 0);
  1591. }
  1592. /*
  1593. * Allocate the endpoint and set it up for I/O
  1594. * in the controller. This must follow what's said
  1595. * in Ep regarding configuration, including perhaps
  1596. * the saved toggles (saved on a previous close of
  1597. * the endpoint data file by epclose).
  1598. */
  1599. static void
  1600. epopen(Ep *ep)
  1601. {
  1602. Proc *up = externup();
  1603. Ctlr *ctlr;
  1604. Qh *cqh;
  1605. Qio *io;
  1606. Ctlio *cio;
  1607. int usbid;
  1608. ctlr = ep->hp->Hciimpl.aux;
  1609. deprint("uhci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
  1610. if(ep->aux != nil)
  1611. panic("uhci: epopen called with open ep");
  1612. if(waserror()){
  1613. free(ep->aux);
  1614. ep->aux = nil;
  1615. nexterror();
  1616. }
  1617. if(ep->maxpkt > Tdmaxpkt){
  1618. print("uhci: maxkpkt too large: using %d\n", Tdmaxpkt);
  1619. ep->maxpkt = Tdmaxpkt;
  1620. }
  1621. cqh = ctlr->qh[ep->ttype];
  1622. switch(ep->ttype){
  1623. case Tnone:
  1624. error("endpoint not configured");
  1625. case Tiso:
  1626. ep->aux = smalloc(sizeof(Isoio));
  1627. isoopen(ep);
  1628. break;
  1629. case Tctl:
  1630. cio = ep->aux = smalloc(sizeof(Ctlio));
  1631. cio->Qio.debug = ep->debug;
  1632. cio->ndata = -1;
  1633. cio->data = nil;
  1634. if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
  1635. break;
  1636. cio->Qio.qh = qhalloc(ctlr, cqh, &cio->Qio, "epc");
  1637. break;
  1638. case Tbulk:
  1639. case Tintr:
  1640. io = ep->aux = smalloc(sizeof(Qio)*2);
  1641. io[OREAD].debug = io[OWRITE].debug = ep->debug;
  1642. usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb &Devmax);
  1643. if(ep->mode != OREAD){
  1644. if(ep->toggle[OWRITE] != 0)
  1645. io[OWRITE].toggle = Tddata1;
  1646. else
  1647. io[OWRITE].toggle = Tddata0;
  1648. io[OWRITE].tok = Tdtokout;
  1649. io[OWRITE].qh = qhalloc(ctlr, cqh, io+OWRITE, "epw");
  1650. io[OWRITE].usbid = usbid;
  1651. }
  1652. if(ep->mode != OWRITE){
  1653. if(ep->toggle[OREAD] != 0)
  1654. io[OREAD].toggle = Tddata1;
  1655. else
  1656. io[OREAD].toggle = Tddata0;
  1657. io[OREAD].tok = Tdtokin;
  1658. io[OREAD].qh = qhalloc(ctlr, cqh, io+OREAD, "epr");
  1659. io[OREAD].usbid = usbid;
  1660. }
  1661. break;
  1662. }
  1663. if(debug>1 || ep->debug)
  1664. dump(ep->hp);
  1665. deprint("uhci: epopen done\n");
  1666. poperror();
  1667. }
  1668. static void
  1669. cancelio(Ctlr *ctlr, Qio *io)
  1670. {
  1671. Proc *up = externup();
  1672. Qh *qh;
  1673. ilock(&ctlr->l);
  1674. qh = io->qh;
  1675. if(io == nil || io->qh == nil || io->qh->state == Qclose){
  1676. iunlock(&ctlr->l);
  1677. return;
  1678. }
  1679. dqprint("uhci: cancelio for qh %#p state %s\n",
  1680. qh, qhsname[qh->state]);
  1681. aborttds(qh);
  1682. qh->state = Qclose;
  1683. iunlock(&ctlr->l);
  1684. if(!waserror()){
  1685. tsleep(&up->sleep, return0, 0, Abortdelay);
  1686. poperror();
  1687. }
  1688. wakeup(&io->Rendez);
  1689. qlock(&io->ql);
  1690. /* wait for epio if running */
  1691. qunlock(&io->ql);
  1692. qhfree(ctlr, qh);
  1693. io->qh = nil;
  1694. }
  1695. static void
  1696. cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, uint32_t load)
  1697. {
  1698. Proc *up = externup();
  1699. Isoio **il;
  1700. uint32_t *lp;
  1701. int i;
  1702. int frno;
  1703. Td *td;
  1704. ilock(&ctlr->l);
  1705. if(iso->state == Qclose){
  1706. iunlock(&ctlr->l);
  1707. return;
  1708. }
  1709. if(iso->state != Qrun && iso->state != Qdone)
  1710. panic("bad iso state");
  1711. iso->state = Qclose;
  1712. if(ctlr->isoload < load)
  1713. panic("uhci: low isoload");
  1714. ctlr->isoload -= load;
  1715. ctlr->load -= load;
  1716. for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
  1717. if(*il == iso)
  1718. break;
  1719. if(*il == nil)
  1720. panic("isocancel: not found");
  1721. *il = iso->next;
  1722. frno = iso->td0frno;
  1723. for(i = 0; i < iso->nframes; i++){
  1724. td = iso->tdps[frno];
  1725. td->csw &= ~(Tdioc|Tdactive);
  1726. for(lp=&ctlr->frames[frno]; !(*lp & Tdterm);
  1727. lp = &TPTR(*lp)->link)
  1728. if(TPTR(*lp) == td)
  1729. break;
  1730. if(*lp & Tdterm)
  1731. panic("cancelisoio: td not found");
  1732. *lp = td->link;
  1733. frno = TRUNC(frno+pollival, Nframes);
  1734. }
  1735. iunlock(&ctlr->l);
  1736. /*
  1737. * wakeup anyone waiting for I/O and
  1738. * wait to be sure no I/O is in progress in the controller.
  1739. * and then wait to be sure episo-io is no int32_ter running.
  1740. */
  1741. wakeup(&iso->Rendez);
  1742. diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
  1743. tsleep(&up->sleep, return0, 0, 5);
  1744. qlock(&iso->ql);
  1745. qunlock(&iso->ql);
  1746. diprint("cancelisoio iso %#p releasing iso\n", iso);
  1747. frno = iso->td0frno;
  1748. for(i = 0; i < iso->nframes; i++){
  1749. tdfree(iso->tdps[frno]);
  1750. iso->tdps[frno] = nil;
  1751. frno = TRUNC(frno+pollival, Nframes);
  1752. }
  1753. free(iso->data);
  1754. iso->data = nil;
  1755. }
  1756. static void
  1757. epclose(Ep *ep)
  1758. {
  1759. Ctlr *ctlr;
  1760. Ctlio *cio;
  1761. Isoio *iso;
  1762. Qio *io;
  1763. ctlr = ep->hp->Hciimpl.aux;
  1764. deprint("uhci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
  1765. if(ep->aux == nil)
  1766. panic("uhci: epclose called with closed ep");
  1767. switch(ep->ttype){
  1768. case Tctl:
  1769. cio = ep->aux;
  1770. cancelio(ctlr, &cio->Qio);
  1771. free(cio->data);
  1772. cio->data = nil;
  1773. break;
  1774. case Tbulk:
  1775. case Tintr:
  1776. io = ep->aux;
  1777. ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
  1778. if(ep->mode != OWRITE){
  1779. cancelio(ctlr, &io[OREAD]);
  1780. if(io[OREAD].toggle == Tddata1)
  1781. ep->toggle[OREAD] = 1;
  1782. }
  1783. if(ep->mode != OREAD){
  1784. cancelio(ctlr, &io[OWRITE]);
  1785. if(io[OWRITE].toggle == Tddata1)
  1786. ep->toggle[OWRITE] = 1;
  1787. }
  1788. break;
  1789. case Tiso:
  1790. iso = ep->aux;
  1791. cancelisoio(ctlr, iso, ep->pollival, ep->load);
  1792. break;
  1793. default:
  1794. panic("epclose: bad ttype %d", ep->ttype);
  1795. }
  1796. free(ep->aux);
  1797. ep->aux = nil;
  1798. }
  1799. static char*
  1800. seprintep(char *s, char *e, Ep *ep)
  1801. {
  1802. Ctlio *cio;
  1803. Qio *io;
  1804. Isoio *iso;
  1805. Ctlr *ctlr;
  1806. ctlr = ep->hp->Hciimpl.aux;
  1807. ilock(&ctlr->l);
  1808. if(ep->aux == nil){
  1809. *s = 0;
  1810. iunlock(&ctlr->l);
  1811. return s;
  1812. }
  1813. switch(ep->ttype){
  1814. case Tctl:
  1815. cio = ep->aux;
  1816. s = seprint(s,e,"cio %#p qh %#p"
  1817. " id %#x tog %#x tok %#x err %s\n",
  1818. cio, cio->Qio.qh, cio->Qio.usbid, cio->Qio.toggle,
  1819. cio->Qio.tok, cio->Qio.err);
  1820. break;
  1821. case Tbulk:
  1822. case Tintr:
  1823. io = ep->aux;
  1824. if(ep->mode != OWRITE)
  1825. s = seprint(s,e,"r: qh %#p id %#x tog %#x tok %#x err %s\n",
  1826. io[OREAD].qh, io[OREAD].usbid, io[OREAD].toggle,
  1827. io[OREAD].tok, io[OREAD].err);
  1828. if(ep->mode != OREAD)
  1829. s = seprint(s,e,"w: qh %#p id %#x tog %#x tok %#x err %s\n",
  1830. io[OWRITE].qh, io[OWRITE].usbid, io[OWRITE].toggle,
  1831. io[OWRITE].tok, io[OWRITE].err);
  1832. break;
  1833. case Tiso:
  1834. iso = ep->aux;
  1835. s = seprint(s,e,"iso %#p id %#x tok %#x tdu %#p tdi %#p err %s\n",
  1836. iso, iso->usbid, iso->tok, iso->tdu, iso->tdi, iso->err);
  1837. break;
  1838. }
  1839. iunlock(&ctlr->l);
  1840. return s;
  1841. }
  1842. static int
  1843. portenable(Hci *hp, int port, int on)
  1844. {
  1845. Proc *up = externup();
  1846. int s;
  1847. int ioport;
  1848. Ctlr *ctlr;
  1849. ctlr = hp->Hciimpl.aux;
  1850. dprint("uhci: %#x port %d enable=%d\n", ctlr->port, port, on);
  1851. ioport = PORT(port-1);
  1852. qlock(&ctlr->portlck);
  1853. if(waserror()){
  1854. qunlock(&ctlr->portlck);
  1855. nexterror();
  1856. }
  1857. ilock(&ctlr->l);
  1858. s = INS(ioport);
  1859. if(on)
  1860. OUTS(ioport, s | PSenable);
  1861. else
  1862. OUTS(ioport, s & ~PSenable);
  1863. microdelay(64);
  1864. iunlock(&ctlr->l);
  1865. tsleep(&up->sleep, return0, 0, Enabledelay);
  1866. dprint("uhci %#x port %d enable=%d: sts %#x\n",
  1867. ctlr->port, port, on, INS(ioport));
  1868. qunlock(&ctlr->portlck);
  1869. poperror();
  1870. return 0;
  1871. }
  1872. static int
  1873. portreset(Hci *hp, int port, int on)
  1874. {
  1875. int i, p;
  1876. Ctlr *ctlr;
  1877. if(on == 0)
  1878. return 0;
  1879. ctlr = hp->Hciimpl.aux;
  1880. dprint("uhci: %#x port %d reset\n", ctlr->port, port);
  1881. p = PORT(port-1);
  1882. ilock(&ctlr->l);
  1883. OUTS(p, PSreset);
  1884. delay(50);
  1885. OUTS(p, INS(p) & ~PSreset);
  1886. OUTS(p, INS(p) | PSenable);
  1887. microdelay(64);
  1888. for(i=0; i<1000 && (INS(p) & PSenable) == 0; i++)
  1889. ;
  1890. OUTS(p, (INS(p) & ~PSreset)|PSenable);
  1891. iunlock(&ctlr->l);
  1892. dprint("uhci %#x after port %d reset: sts %#x\n",
  1893. ctlr->port, port, INS(p));
  1894. return 0;
  1895. }
  1896. static int
  1897. portstatus(Hci *hp, int port)
  1898. {
  1899. Proc *up = externup();
  1900. int s;
  1901. int r;
  1902. int ioport;
  1903. Ctlr *ctlr;
  1904. ctlr = hp->Hciimpl.aux;
  1905. ioport = PORT(port-1);
  1906. qlock(&ctlr->portlck);
  1907. if(waserror()){
  1908. iunlock(&ctlr->l);
  1909. qunlock(&ctlr->portlck);
  1910. nexterror();
  1911. }
  1912. ilock(&ctlr->l);
  1913. s = INS(ioport);
  1914. if(s & (PSstatuschg | PSchange)){
  1915. OUTS(ioport, s);
  1916. ddprint("uhci %#x port %d status %#x\n", ctlr->port, port, s);
  1917. }
  1918. iunlock(&ctlr->l);
  1919. qunlock(&ctlr->portlck);
  1920. poperror();
  1921. /*
  1922. * We must return status bits as a
  1923. * get port status hub request would do.
  1924. */
  1925. r = 0;
  1926. if(s & PSpresent)
  1927. r |= HPpresent;
  1928. if(s & PSenable)
  1929. r |= HPenable;
  1930. if(s & PSsuspend)
  1931. r |= HPsuspend;
  1932. if(s & PSreset)
  1933. r |= HPreset;
  1934. if(s & PSslow)
  1935. r |= HPslow;
  1936. if(s & PSstatuschg)
  1937. r |= HPstatuschg;
  1938. if(s & PSchange)
  1939. r |= HPchange;
  1940. return r;
  1941. }
  1942. static void
  1943. scanpci(void)
  1944. {
  1945. static int already = 0;
  1946. int io;
  1947. int i;
  1948. Ctlr *ctlr;
  1949. Pcidev *p;
  1950. if(already)
  1951. return;
  1952. already = 1;
  1953. p = nil;
  1954. while((p = pcimatch(p, 0, 0)) != nil){
  1955. /*
  1956. * Find UHCI controllers (Programming Interface = 0).
  1957. */
  1958. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb)
  1959. continue;
  1960. switch(p->ccrp){
  1961. case 0:
  1962. io = p->mem[4].bar & ~0x0F;
  1963. break;
  1964. default:
  1965. continue;
  1966. }
  1967. if(io == 0){
  1968. print("usbuhci: %#x %#x: failed to map registers\n",
  1969. p->vid, p->did);
  1970. continue;
  1971. }
  1972. if(ioalloc(io, p->mem[4].size, 0, "usbuhci") < 0){
  1973. print("usbuhci: port %#x in use\n", io);
  1974. continue;
  1975. }
  1976. if(p->intl == 0xFF || p->intl == 0){
  1977. print("usbuhci: no irq assigned for port %#x\n", io);
  1978. continue;
  1979. }
  1980. dprint("uhci: %#x %#x: port %#x size %#x irq %d\n",
  1981. p->vid, p->did, io, p->mem[4].size, p->intl);
  1982. ctlr = malloc(sizeof(Ctlr));
  1983. if (ctlr == nil)
  1984. panic("uhci: out of memory");
  1985. ctlr->pcidev = p;
  1986. ctlr->port = io;
  1987. for(i = 0; i < Nhcis; i++)
  1988. if(ctlrs[i] == nil){
  1989. ctlrs[i] = ctlr;
  1990. break;
  1991. }
  1992. if(i == Nhcis)
  1993. print("uhci: bug: no more controllers\n");
  1994. }
  1995. }
  1996. static void
  1997. uhcimeminit(Ctlr *ctlr)
  1998. {
  1999. Td* td;
  2000. Qh *qh;
  2001. int frsize;
  2002. int i;
  2003. ctlr->qhs = ctlr->qh[Tctl] = qhalloc(ctlr, nil, nil, "CTL");
  2004. ctlr->qh[Tintr] = qhalloc(ctlr, ctlr->qh[Tctl], nil, "INT");
  2005. ctlr->qh[Tbulk] = qhalloc(ctlr, ctlr->qh[Tintr], nil, "BLK");
  2006. /* idle Td from dummy Qh at the end. looped back to itself */
  2007. /* This is a workaround for PIIX4 errata 29773804.pdf */
  2008. qh = qhalloc(ctlr, ctlr->qh[Tbulk], nil, "BWS");
  2009. td = tdalloc();
  2010. td->link = PCIWADDR(td);
  2011. qhlinktd(qh, td);
  2012. /* loop (hw only) from the last qh back to control xfers.
  2013. * this may be done only for some of them. Disable until ehci comes.
  2014. */
  2015. if(0)
  2016. qh->link = PCIWADDR(ctlr->qhs);
  2017. frsize = Nframes*sizeof(uint32_t);
  2018. ctlr->frames = mallocalign(frsize, frsize, 0, 0);
  2019. if(ctlr->frames == nil)
  2020. panic("uhci reset: no memory");
  2021. ctlr->iso = nil;
  2022. for(i = 0; i < Nframes; i++)
  2023. ctlr->frames[i] = PCIWADDR(ctlr->qhs)|QHlinkqh;
  2024. OUTL(Flbaseadd, PCIWADDR(ctlr->frames));
  2025. OUTS(Frnum, 0);
  2026. dprint("uhci %#x flb %#lx frno %#x\n", ctlr->port,
  2027. INL(Flbaseadd), INS(Frnum));
  2028. }
  2029. static void
  2030. init(Hci *hp)
  2031. {
  2032. Ctlr *ctlr;
  2033. int sts;
  2034. int i;
  2035. ctlr = hp->Hciimpl.aux;
  2036. dprint("uhci %#x init\n", ctlr->port);
  2037. coherence();
  2038. ilock(&ctlr->l);
  2039. OUTS(Usbintr, Itmout|Iresume|Ioc|Ishort);
  2040. uhcirun(ctlr, 1);
  2041. dprint("uhci: init: cmd %#x sts %#x sof %#x",
  2042. INS(Cmd), INS(Status), INS(SOFmod));
  2043. dprint(" flb %#lx frno %#x psc0 %#x psc1 %#x",
  2044. INL(Flbaseadd), INS(Frnum), INS(PORT(0)), INS(PORT(1)));
  2045. /* guess other ports */
  2046. for(i = 2; i < 6; i++){
  2047. sts = INS(PORT(i));
  2048. if(sts != 0xFFFF && (sts & PSreserved1) == 1){
  2049. dprint(" psc%d %#x", i, sts);
  2050. hp->nports++;
  2051. }else
  2052. break;
  2053. }
  2054. for(i = 0; i < hp->nports; i++)
  2055. OUTS(PORT(i), 0);
  2056. iunlock(&ctlr->l);
  2057. }
  2058. static void
  2059. uhcireset(Ctlr *ctlr)
  2060. {
  2061. int i;
  2062. int sof;
  2063. ilock(&ctlr->l);
  2064. dprint("uhci %#x reset\n", ctlr->port);
  2065. /*
  2066. * Turn off legacy mode. Some controllers won't
  2067. * interrupt us as expected otherwise.
  2068. */
  2069. uhcirun(ctlr, 0);
  2070. pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
  2071. OUTS(Usbintr, 0);
  2072. sof = INB(SOFmod);
  2073. uhcicmd(ctlr, Cgreset); /* global reset */
  2074. delay(Resetdelay);
  2075. uhcicmd(ctlr, 0); /* all halt */
  2076. uhcicmd(ctlr, Chcreset); /* controller reset */
  2077. for(i = 0; i < 100; i++){
  2078. if((INS(Cmd) & Chcreset) == 0)
  2079. break;
  2080. delay(1);
  2081. }
  2082. if(i == 100)
  2083. print("uhci %#x controller reset timed out\n", ctlr->port);
  2084. OUTB(SOFmod, sof);
  2085. iunlock(&ctlr->l);
  2086. }
  2087. static void
  2088. setdebug(Hci *hci, int d)
  2089. {
  2090. debug = d;
  2091. }
  2092. static void
  2093. shutdown(Hci *hp)
  2094. {
  2095. Ctlr *ctlr;
  2096. ctlr = hp->Hciimpl.aux;
  2097. ilock(&ctlr->l);
  2098. uhcirun(ctlr, 0);
  2099. delay(100);
  2100. iunlock(&ctlr->l);
  2101. }
  2102. static int
  2103. reset(Hci *hp)
  2104. {
  2105. static Lock resetlck;
  2106. int i;
  2107. Ctlr *ctlr;
  2108. Pcidev *p;
  2109. ilock(&resetlck);
  2110. scanpci();
  2111. /*
  2112. * Any adapter matches if no hp->port is supplied,
  2113. * otherwise the ports must match.
  2114. */
  2115. ctlr = nil;
  2116. for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
  2117. ctlr = ctlrs[i];
  2118. if(ctlr->active == 0)
  2119. if(hp->ISAConf.port == 0 || hp->ISAConf.port == ctlr->port){
  2120. ctlr->active = 1;
  2121. break;
  2122. }
  2123. }
  2124. iunlock(&resetlck);
  2125. if(ctlrs[i] == nil || i == Nhcis)
  2126. return -1;
  2127. p = ctlr->pcidev;
  2128. hp->Hciimpl.aux = ctlr;
  2129. hp->ISAConf.port = ctlr->port;
  2130. hp->ISAConf.irq = p->intl;
  2131. hp->tbdf = p->tbdf;
  2132. hp->nports = 2; /* default */
  2133. uhcireset(ctlr);
  2134. uhcimeminit(ctlr);
  2135. /*
  2136. * Linkage to the generic HCI driver.
  2137. */
  2138. hp->Hciimpl.init = init;
  2139. hp->Hciimpl.dump = dump;
  2140. hp->Hciimpl.interrupt = interrupt;
  2141. hp->Hciimpl.epopen = epopen;
  2142. hp->Hciimpl.epclose = epclose;
  2143. hp->Hciimpl.epread = epread;
  2144. hp->Hciimpl.epwrite = epwrite;
  2145. hp->Hciimpl.seprintep = seprintep;
  2146. hp->Hciimpl.portenable = portenable;
  2147. hp->Hciimpl.portreset = portreset;
  2148. hp->Hciimpl.portstatus = portstatus;
  2149. hp->Hciimpl.shutdown = shutdown;
  2150. hp->Hciimpl.debug = setdebug;
  2151. hp->ISAConf.type = "uhci";
  2152. return 0;
  2153. }
  2154. void
  2155. usbuhcilink(void)
  2156. {
  2157. addhcitype("uhci", reset);
  2158. }