usbuhci.c 51 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 xspanalloc; 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; /* 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; /* for the entire I/O process */
  150. 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; /* 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;
  168. 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;
  187. Td* free;
  188. int nalloc;
  189. int ninuse;
  190. int nfree;
  191. };
  192. struct Qhpool
  193. {
  194. Lock;
  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. Qio* io; /* for this queue */
  223. Qh* next; /* in active or free list */
  224. Td* tds; /* Td list in this Qh (initially, elink) */
  225. char* tag; /* debug and align, mostly */
  226. uint32_t align;
  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 %#ux 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 %#ux cmd %#ux sts %#ux\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 %#ulx", 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 %#ulx ", 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 %#ulx", 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 %#ulx", 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 %#ulx:", (td->token>>15) & Epmax);
  356. s = seprint(s, se, "%#ulx", (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 %#ulx data %#p", td->buffer, td->data);
  371. s = seprint(s, se, " ndata %uld 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 %#ulx %#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 %#ulx", 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 %#ulx %#p]",
  450. qh->tds, qh->elink, TPTR(qh->elink));
  451. if(!sameptr(qh->next, qh->link))
  452. s = seprint(s, se, " [next %#p != link %#ulx %#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 %#ulx", 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(" %#ulx", 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);
  492. print("tds: alloc %d = inuse %d + free %d\n",
  493. tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
  494. unlock(&tdpool);
  495. lock(&qhpool);
  496. print("qhs: alloc %d = inuse %d + free %d\n",
  497. qhpool.nalloc, qhpool.ninuse, qhpool.nfree);
  498. unlock(&qhpool);
  499. }
  500. ilock(ctlr);
  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 %#ux sts %#ux fl %#ulx ps1 %#ux ps2 %#ux frames[0] %#ulx\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);
  522. }
  523. static void
  524. dump(Hci *hp)
  525. {
  526. xdump(hp->aux, 1);
  527. }
  528. static Td*
  529. tdalloc(void)
  530. {
  531. int i;
  532. Td *td;
  533. Td *pool;
  534. lock(&tdpool);
  535. if(tdpool.free == nil){
  536. ddprint("uhci: tdalloc %d Tds\n", Incr);
  537. pool = xspanalloc(Incr*sizeof(Td), Align, 0);
  538. if(pool == nil)
  539. panic("tdalloc");
  540. for(i=Incr; --i>=0;){
  541. pool[i].next = tdpool.free;
  542. tdpool.free = &pool[i];
  543. }
  544. tdpool.nalloc += Incr;
  545. tdpool.nfree += Incr;
  546. }
  547. td = tdpool.free;
  548. tdpool.free = td->next;
  549. tdpool.ninuse++;
  550. tdpool.nfree--;
  551. unlock(&tdpool);
  552. memset(td, 0, sizeof(Td));
  553. td->link = Tdterm;
  554. assert(((uint64_t)td & 0xF) == 0);
  555. return td;
  556. }
  557. static void
  558. tdfree(Td *td)
  559. {
  560. if(td == nil)
  561. return;
  562. free(td->buff);
  563. td->buff = nil;
  564. lock(&tdpool);
  565. td->next = tdpool.free;
  566. tdpool.free = td;
  567. tdpool.ninuse--;
  568. tdpool.nfree++;
  569. unlock(&tdpool);
  570. }
  571. static void
  572. qhlinkqh(Qh* qh, Qh* next)
  573. {
  574. if(next == nil)
  575. qh->link = QHterm;
  576. else{
  577. next->link = qh->link;
  578. next->next = qh->next;
  579. qh->link = PCIWADDR(next)|QHlinkqh;
  580. }
  581. qh->next = next;
  582. }
  583. static void
  584. qhlinktd(Qh *qh, Td *td)
  585. {
  586. qh->tds = td;
  587. if(td == nil)
  588. qh->elink = QHvf|QHterm;
  589. else
  590. qh->elink = PCIWADDR(td);
  591. }
  592. static void
  593. tdlinktd(Td *td, Td *next)
  594. {
  595. td->next = next;
  596. if(next == nil)
  597. td->link = Tdterm;
  598. else
  599. td->link = PCIWADDR(next)|Tdvf;
  600. }
  601. static Qh*
  602. qhalloc(Ctlr *ctlr, Qh *prev, Qio *io, char *tag)
  603. {
  604. int i;
  605. Qh *qh;
  606. Qh *pool;
  607. lock(&qhpool);
  608. if(qhpool.free == nil){
  609. ddprint("uhci: qhalloc %d Qhs\n", Incr);
  610. pool = xspanalloc(Incr*sizeof(Qh), Align, 0);
  611. if(pool == nil)
  612. panic("qhalloc");
  613. for(i=Incr; --i>=0;){
  614. pool[i].next = qhpool.free;
  615. qhpool.free = &pool[i];
  616. }
  617. qhpool.nalloc += Incr;
  618. qhpool.nfree += Incr;
  619. }
  620. qh = qhpool.free;
  621. qhpool.free = qh->next;
  622. qh->next = nil;
  623. qh->link = QHterm;
  624. qhpool.ninuse++;
  625. qhpool.nfree--;
  626. unlock(&qhpool);
  627. qh->tds = nil;
  628. qh->elink = QHterm;
  629. qh->state = Qidle;
  630. qh->io = io;
  631. qh->tag = nil;
  632. kstrdup(&qh->tag, tag);
  633. if(prev != nil){
  634. coherence();
  635. ilock(ctlr);
  636. qhlinkqh(prev, qh);
  637. iunlock(ctlr);
  638. }
  639. assert(((uint64_t)qh & 0xF) == 0);
  640. return qh;
  641. }
  642. static void
  643. qhfree(Ctlr *ctlr, Qh *qh)
  644. {
  645. Td *td;
  646. Td *ltd;
  647. Qh *q;
  648. if(qh == nil)
  649. return;
  650. ilock(ctlr);
  651. for(q = ctlr->qhs; q != nil; q = q->next)
  652. if(q->next == qh)
  653. break;
  654. if(q == nil)
  655. panic("qhfree: nil q");
  656. q->next = qh->next;
  657. q->link = qh->link;
  658. iunlock(ctlr);
  659. for(td = qh->tds; td != nil; td = ltd){
  660. ltd = td->next;
  661. tdfree(td);
  662. }
  663. lock(&qhpool);
  664. qh->state = Qfree; /* paranoia */
  665. qh->next = qhpool.free;
  666. qh->tag = nil;
  667. qh->io = nil;
  668. qhpool.free = qh;
  669. qhpool.ninuse--;
  670. qhpool.nfree++;
  671. unlock(&qhpool);
  672. ddprint("qhfree: qh %#p\n", qh);
  673. }
  674. static char*
  675. errmsg(int err)
  676. {
  677. if(err == 0)
  678. return "ok";
  679. if(err & Tdcrcto)
  680. return "crc/timeout error";
  681. if(err & Tdbabble)
  682. return "babble detected";
  683. if(err & Tddberr)
  684. return "db error";
  685. if(err & Tdbitstuff)
  686. return "bit stuffing error";
  687. if(err & Tdstalled)
  688. return Estalled;
  689. return Eio;
  690. }
  691. static int
  692. isocanread(void *a)
  693. {
  694. Isoio *iso;
  695. iso = a;
  696. return iso->state == Qclose ||
  697. (iso->state == Qrun &&
  698. iso->tok == Tdtokin && iso->tdi != iso->tdu);
  699. }
  700. static int
  701. isocanwrite(void *a)
  702. {
  703. Isoio *iso;
  704. iso = a;
  705. return iso->state == Qclose ||
  706. (iso->state == Qrun &&
  707. iso->tok == Tdtokout && iso->tdu->next != iso->tdi);
  708. }
  709. static void
  710. tdisoinit(Isoio *iso, Td *td, int32_t count)
  711. {
  712. td->ndata = count;
  713. td->token = ((count-1)<<21)| ((iso->usbid & 0x7FF)<<8) | iso->tok;
  714. td->csw = Tderr1|Tdiso|Tdactive|Tdioc;
  715. }
  716. /*
  717. * Process Iso i/o on interrupt. For writes update just error status.
  718. * For reads update tds to reflect data and also error status.
  719. * When tdi aproaches tdu, advance tdu; data may be lost.
  720. * (If nframes is << Nframes tdu might be far away but this avoids
  721. * races regarding frno.)
  722. * If we suffer errors for more than half the frames we stall.
  723. */
  724. static void
  725. isointerrupt(Ctlr *ctlr, Isoio* iso)
  726. {
  727. Td *tdi;
  728. int err;
  729. int i;
  730. int nframes;
  731. tdi = iso->tdi;
  732. if((tdi->csw & Tdactive) != 0) /* nothing new done */
  733. return;
  734. ctlr->nisointr++;
  735. ddiprint("isointr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
  736. if(iso->state != Qrun && iso->state != Qdone)
  737. panic("isointr: iso state");
  738. if(debug > 1 || iso->debug > 1)
  739. isodump(iso, 0);
  740. nframes = iso->nframes / 2; /* limit how many we look */
  741. if(nframes > 64)
  742. nframes = 64;
  743. for(i = 0; i < nframes && (tdi->csw & Tdactive) == 0; i++){
  744. tdi->csw &= ~Tdioc;
  745. err = tdi->csw & Tderrors;
  746. if(err == 0)
  747. iso->nerrs = 0;
  748. else if(iso->nerrs++ > iso->nframes/2)
  749. tdi->csw |= Tdstalled;
  750. if((tdi->csw & Tdstalled) != 0){
  751. if(iso->err == nil){
  752. iso->err = errmsg(err);
  753. diprint("isointerrupt: tdi %#p error %#ux %s\n",
  754. tdi, err, iso->err);
  755. diprint("ctlr load %uld\n", ctlr->load);
  756. }
  757. tdi->ndata = 0;
  758. }else
  759. tdi->ndata = tdlen(tdi);
  760. if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
  761. memset(iso->tdu->data, 0, maxtdlen(iso->tdu));
  762. tdisoinit(iso, iso->tdu, maxtdlen(iso->tdu));
  763. iso->tdu = iso->tdu->next;
  764. iso->nleft = 0;
  765. }
  766. tdi = tdi->next;
  767. }
  768. ddiprint("isointr: %d frames processed\n", nframes);
  769. if(i == nframes)
  770. tdi->csw |= Tdioc;
  771. iso->tdi = tdi;
  772. if(isocanwrite(iso) || isocanread(iso)){
  773. diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
  774. iso->tdi, iso->tdu);
  775. wakeup(iso);
  776. }
  777. }
  778. /*
  779. * Process a Qh upon interrupt. There's one per ongoing user I/O.
  780. * User process releases resources later, that is not done here.
  781. * We may find in this order one or more Tds:
  782. * - none/many non active and completed Tds
  783. * - none/one (usually(!) not active) and failed Td
  784. * - none/many active Tds.
  785. * Upon errors the entire transfer is aborted and error reported.
  786. * Otherwise, the transfer is complete only when all Tds are done or
  787. * when a read with less than maxpkt is found.
  788. * Use the software list and not qh->elink to avoid races.
  789. * We could use qh->elink to see if there's something new or not.
  790. */
  791. static void
  792. qhinterrupt(Ctlr *ctlr, Qh *qh)
  793. {
  794. Td *td;
  795. int err;
  796. ctlr->nqhintr++;
  797. if(qh->state != Qrun)
  798. panic("qhinterrupt: qh state");
  799. if(qh->tds == nil)
  800. panic("qhinterrupt: no tds");
  801. if((qh->tds->csw & Tdactive) == 0)
  802. ddqprint("qhinterrupt port %#ux qh %#p p0 %#x p1 %#x\n",
  803. ctlr->port, qh, INS(PORT(0)), INS(PORT(1)));
  804. for(td = qh->tds; td != nil; td = td->next){
  805. if(td->csw & Tdactive)
  806. return;
  807. td->csw &= ~Tdioc;
  808. if((td->csw & Tdstalled) != 0){
  809. err = td->csw & Tderrors;
  810. /* just stalled is end of xfer but not an error */
  811. if(err != Tdstalled && qh->io->err == nil){
  812. qh->io->err = errmsg(td->csw & Tderrors);
  813. dqprint("qhinterrupt: td %#p error %#ux %s\n",
  814. td, err, qh->io->err);
  815. dqprint("ctlr load %uld\n", ctlr->load);
  816. }
  817. break;
  818. }
  819. if((td->csw & Tdnak) != 0){ /* retransmit; not serious */
  820. td->csw &= ~Tdnak;
  821. if(td->next == nil)
  822. td->csw |= Tdioc;
  823. }
  824. td->ndata = tdlen(td);
  825. if(td->ndata < maxtdlen(td)){ /* EOT */
  826. td = td->next;
  827. break;
  828. }
  829. }
  830. /*
  831. * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
  832. */
  833. qh->elink = QHterm;
  834. for(; td != nil; td = td->next)
  835. td->ndata = 0;
  836. qh->state = Qdone;
  837. wakeup(qh->io);
  838. }
  839. static void
  840. interrupt(Ureg *ureg, void *a)
  841. {
  842. Hci *hp;
  843. Ctlr *ctlr;
  844. int frptr;
  845. int frno;
  846. Qh *qh;
  847. Isoio *iso;
  848. int sts;
  849. int cmd;
  850. hp = a;
  851. ctlr = hp->aux;
  852. ilock(ctlr);
  853. ctlr->nintr++;
  854. sts = INS(Status);
  855. if((sts & Sall) == 0){ /* not for us; sharing irq */
  856. iunlock(ctlr);
  857. return;
  858. }
  859. OUTS(Status, sts & Sall);
  860. cmd = INS(Cmd);
  861. if(cmd & Crun == 0){
  862. print("uhci %#ux: not running: uhci bug?\n", ctlr->port);
  863. /* BUG: should abort everything in this case */
  864. }
  865. if(debug > 1){
  866. frptr = INL(Flbaseadd);
  867. frno = INL(Frnum);
  868. frno = TRUNC(frno, Nframes);
  869. print("cmd %#ux sts %#ux frptr %#ux frno %d\n",
  870. cmd, sts, frptr, frno);
  871. }
  872. ctlr->ntdintr++;
  873. /*
  874. * Will we know in USB 3.0 who the interrupt was for?.
  875. * Do they still teach indexing in CS?
  876. * This is Intel's doing.
  877. */
  878. for(iso = ctlr->iso; iso != nil; iso = iso->next)
  879. if(iso->state == Qrun || iso->state == Qdone)
  880. isointerrupt(ctlr, iso);
  881. for(qh = ctlr->qhs; qh != nil; qh = qh->next)
  882. if(qh->state == Qrun)
  883. qhinterrupt(ctlr, qh);
  884. else if(qh->state == Qclose)
  885. qhlinktd(qh, nil);
  886. iunlock(ctlr);
  887. }
  888. /*
  889. * iso->tdu is the next place to put data. When it gets full
  890. * it is activated and tdu advanced.
  891. */
  892. static int32_t
  893. putsamples(Isoio *iso, unsigned char *b, int32_t count)
  894. {
  895. int32_t tot;
  896. int32_t n;
  897. for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
  898. n = count-tot;
  899. if(n > maxtdlen(iso->tdu) - iso->nleft)
  900. n = maxtdlen(iso->tdu) - iso->nleft;
  901. memmove(iso->tdu->data+iso->nleft, b+tot, n);
  902. iso->nleft += n;
  903. if(iso->nleft == maxtdlen(iso->tdu)){
  904. tdisoinit(iso, iso->tdu, iso->nleft);
  905. iso->nleft = 0;
  906. iso->tdu = iso->tdu->next;
  907. }
  908. }
  909. return tot;
  910. }
  911. /*
  912. * Queue data for writing and return error status from
  913. * last writes done, to maintain buffered data.
  914. */
  915. static int32_t
  916. episowrite(Ep *ep, Isoio *iso, void *a, int32_t count)
  917. {
  918. Proc *up = externup();
  919. Ctlr *ctlr;
  920. unsigned char *b;
  921. int tot;
  922. int nw;
  923. char *err;
  924. iso->debug = ep->debug;
  925. diprint("uhci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  926. ctlr = ep->hp->aux;
  927. qlock(iso);
  928. if(waserror()){
  929. qunlock(iso);
  930. nexterror();
  931. }
  932. ilock(ctlr);
  933. if(iso->state == Qclose){
  934. iunlock(ctlr);
  935. error(iso->err ? iso->err : Eio);
  936. }
  937. iso->state = Qrun;
  938. b = a;
  939. for(tot = 0; tot < count; tot += nw){
  940. while(isocanwrite(iso) == 0){
  941. iunlock(ctlr);
  942. diprint("uhci: episowrite: %#p sleep\n", iso);
  943. if(waserror()){
  944. if(iso->err == nil)
  945. iso->err = "I/O timed out";
  946. ilock(ctlr);
  947. break;
  948. }
  949. tsleep(iso, isocanwrite, iso, ep->tmout);
  950. poperror();
  951. ilock(ctlr);
  952. }
  953. err = iso->err;
  954. iso->err = nil;
  955. if(iso->state == Qclose || err != nil){
  956. iunlock(ctlr);
  957. error(err ? err : Eio);
  958. }
  959. if(iso->state != Qrun)
  960. panic("episowrite: iso not running");
  961. iunlock(ctlr); /* We could page fault here */
  962. nw = putsamples(iso, b+tot, count-tot);
  963. ilock(ctlr);
  964. }
  965. if(iso->state != Qclose)
  966. iso->state = Qdone;
  967. iunlock(ctlr);
  968. err = iso->err; /* in case it failed early */
  969. iso->err = nil;
  970. qunlock(iso);
  971. poperror();
  972. if(err != nil)
  973. error(err);
  974. diprint("uhci: episowrite: %#p %d bytes\n", iso, tot);
  975. return tot;
  976. }
  977. /*
  978. * Available data is kept at tdu and following tds, up to tdi (excluded).
  979. */
  980. static int32_t
  981. episoread(Ep *ep, Isoio *iso, void *a, int count)
  982. {
  983. Proc *up = externup();
  984. Ctlr *ctlr;
  985. unsigned char *b;
  986. int nr;
  987. int tot;
  988. Td *tdu;
  989. iso->debug = ep->debug;
  990. diprint("uhci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
  991. b = a;
  992. ctlr = ep->hp->aux;
  993. qlock(iso);
  994. if(waserror()){
  995. qunlock(iso);
  996. nexterror();
  997. }
  998. iso->err = nil;
  999. iso->nerrs = 0;
  1000. ilock(ctlr);
  1001. if(iso->state == Qclose){
  1002. iunlock(ctlr);
  1003. error(iso->err ? iso->err : Eio);
  1004. }
  1005. iso->state = Qrun;
  1006. while(isocanread(iso) == 0){
  1007. iunlock(ctlr);
  1008. diprint("uhci: episoread: %#p sleep\n", iso);
  1009. if(waserror()){
  1010. if(iso->err == nil)
  1011. iso->err = "I/O timed out";
  1012. ilock(ctlr);
  1013. break;
  1014. }
  1015. tsleep(iso, isocanread, iso, ep->tmout);
  1016. poperror();
  1017. ilock(ctlr);
  1018. }
  1019. if(iso->state == Qclose){
  1020. iunlock(ctlr);
  1021. error(iso->err ? iso->err : Eio);
  1022. }
  1023. iso->state = Qdone;
  1024. assert(iso->tdu != iso->tdi);
  1025. for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
  1026. tdu = iso->tdu;
  1027. if(tdu->csw & Tdactive){
  1028. diprint("uhci: episoread: %#p tdu active\n", iso);
  1029. break;
  1030. }
  1031. nr = tdu->ndata;
  1032. if(tot + nr > count)
  1033. nr = count - tot;
  1034. if(nr == 0)
  1035. print("uhci: ep%d.%d: too many polls\n",
  1036. ep->dev->nb, ep->nb);
  1037. else{
  1038. iunlock(ctlr); /* We could page fault here */
  1039. memmove(b+tot, tdu->data, nr);
  1040. ilock(ctlr);
  1041. if(nr < tdu->ndata)
  1042. memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
  1043. tdu->ndata -= nr;
  1044. }
  1045. if(tdu->ndata == 0){
  1046. tdisoinit(iso, tdu, ep->maxpkt);
  1047. iso->tdu = tdu->next;
  1048. }
  1049. }
  1050. iunlock(ctlr);
  1051. qunlock(iso);
  1052. poperror();
  1053. diprint("uhci: episoread: %#p %d bytes err '%s'\n", iso, tot, iso->err);
  1054. if(iso->err != nil)
  1055. error(iso->err);
  1056. return tot;
  1057. }
  1058. static int
  1059. nexttoggle(int tog)
  1060. {
  1061. if(tog == Tddata0)
  1062. return Tddata1;
  1063. else
  1064. return Tddata0;
  1065. }
  1066. static Td*
  1067. epgettd(Ep *ep, Qio *io, int flags, void *a, int count)
  1068. {
  1069. Td *td;
  1070. int tok;
  1071. if(ep->maxpkt < count)
  1072. error("maxpkt too short");
  1073. td = tdalloc();
  1074. if(count <= Tdndata)
  1075. td->data = td->sbuff;
  1076. else
  1077. td->data = td->buff = smalloc(ep->maxpkt);
  1078. td->buffer = PCIWADDR(td->data);
  1079. td->ndata = count;
  1080. if(a != nil && count > 0)
  1081. memmove(td->data, a, count);
  1082. td->csw = Tderr2|Tderr1|flags;
  1083. if(ep->dev->speed == Lowspeed)
  1084. td->csw |= Tdlow;
  1085. tok = io->tok | io->toggle;
  1086. io->toggle = nexttoggle(io->toggle);
  1087. td->token = ((count-1)<<21) | ((io->usbid&0x7FF)<<8) | tok;
  1088. return td;
  1089. }
  1090. /*
  1091. * Try to get them idle
  1092. */
  1093. static void
  1094. aborttds(Qh *qh)
  1095. {
  1096. Td *td;
  1097. qh->state = Qdone;
  1098. qh->elink = QHterm;
  1099. for(td = qh->tds; td != nil; td = td->next){
  1100. if(td->csw & Tdactive)
  1101. td->ndata = 0;
  1102. td->csw &= ~(Tdactive|Tdioc);
  1103. }
  1104. }
  1105. static int
  1106. epiodone(void *a)
  1107. {
  1108. Qh *qh;
  1109. qh = a;
  1110. return qh->state != Qrun;
  1111. }
  1112. static void
  1113. epiowait(Ctlr *ctlr, Qio *io, int tmout, uint32_t load)
  1114. {
  1115. Proc *up = externup();
  1116. Qh *qh;
  1117. int timedout;
  1118. qh = io->qh;
  1119. ddqprint("uhci io %#p sleep on qh %#p state %uld\n", io, qh, qh->state);
  1120. timedout = 0;
  1121. if(waserror()){
  1122. dqprint("uhci io %#p qh %#p timed out\n", io, qh);
  1123. timedout++;
  1124. }else{
  1125. if(tmout == 0)
  1126. sleep(io, epiodone, qh);
  1127. else
  1128. tsleep(io, epiodone, qh, tmout);
  1129. poperror();
  1130. }
  1131. ilock(ctlr);
  1132. if(qh->state == Qrun)
  1133. timedout = 1;
  1134. else if(qh->state != Qdone && qh->state != Qclose)
  1135. panic("epio: queue not done and not closed");
  1136. if(timedout){
  1137. aborttds(io->qh);
  1138. io->err = "request timed out";
  1139. iunlock(ctlr);
  1140. if(!waserror()){
  1141. tsleep(&up->sleep, return0, 0, Abortdelay);
  1142. poperror();
  1143. }
  1144. ilock(ctlr);
  1145. }
  1146. if(qh->state != Qclose)
  1147. qh->state = Qidle;
  1148. qhlinktd(qh, nil);
  1149. ctlr->load -= load;
  1150. iunlock(ctlr);
  1151. }
  1152. /*
  1153. * Non iso I/O.
  1154. * To make it work for control transfers, the caller may
  1155. * lock the Qio for the entire control transfer.
  1156. */
  1157. static int32_t
  1158. epio(Ep *ep, Qio *io, void *a, int32_t count, int mustlock)
  1159. {
  1160. Proc *up = externup();
  1161. Td *td, *ltd, *td0, *ntd;
  1162. Ctlr *ctlr;
  1163. Qh* qh;
  1164. int32_t n, tot;
  1165. char buf[128];
  1166. unsigned char *c;
  1167. int saved, ntds, tmout;
  1168. uint32_t load;
  1169. char *err;
  1170. qh = io->qh;
  1171. ctlr = ep->hp->aux;
  1172. io->debug = ep->debug;
  1173. tmout = ep->tmout;
  1174. ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
  1175. io->tok == Tdtokin ? "in" : "out",
  1176. ep->dev->nb, ep->nb, io, count, ctlr->load);
  1177. if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
  1178. seprintdata(buf, buf+sizeof(buf), a, count);
  1179. print("uchi epio: user data: %s\n", buf);
  1180. }
  1181. if(mustlock){
  1182. qlock(io);
  1183. if(waserror()){
  1184. qunlock(io);
  1185. nexterror();
  1186. }
  1187. }
  1188. io->err = nil;
  1189. ilock(ctlr);
  1190. if(qh->state == Qclose){ /* Tds released by cancelio */
  1191. iunlock(ctlr);
  1192. error(io->err ? io->err : Eio);
  1193. }
  1194. if(qh->state != Qidle)
  1195. panic("epio: qh not idle");
  1196. qh->state = Qinstall;
  1197. iunlock(ctlr);
  1198. c = a;
  1199. td0 = ltd = nil;
  1200. load = tot = 0;
  1201. do{
  1202. n = ep->maxpkt;
  1203. if(count-tot < n)
  1204. n = count-tot;
  1205. if(c != nil && io->tok != Tdtokin)
  1206. td = epgettd(ep, io, Tdactive, c+tot, n);
  1207. else
  1208. td = epgettd(ep, io, Tdactive|Tdspd, nil, n);
  1209. if(td0 == nil)
  1210. td0 = td;
  1211. else
  1212. tdlinktd(ltd, td);
  1213. ltd = td;
  1214. tot += n;
  1215. load += ep->load;
  1216. }while(tot < count);
  1217. if(td0 == nil || ltd == nil)
  1218. panic("epio: no td");
  1219. ltd->csw |= Tdioc; /* the last one interrupts */
  1220. ddeprint("uhci: load %uld ctlr load %uld\n", load, ctlr->load);
  1221. ilock(ctlr);
  1222. if(qh->state != Qclose){
  1223. io->iotime = TK2MS(machp()->ticks);
  1224. qh->state = Qrun;
  1225. coherence();
  1226. qhlinktd(qh, td0);
  1227. ctlr->load += load;
  1228. }
  1229. iunlock(ctlr);
  1230. epiowait(ctlr, io, tmout, load);
  1231. if(debug > 1 || ep->debug > 1)
  1232. dumptd(td0, "epio: got tds: ");
  1233. tot = 0;
  1234. c = a;
  1235. saved = 0;
  1236. ntds = 0;
  1237. for(td = td0; td != nil; td = ntd){
  1238. ntds++;
  1239. /*
  1240. * Use td tok, not io tok, because of setup packets.
  1241. * Also, if the Td was stalled or active (previous Td
  1242. * was a short packet), we must save the toggle as it is.
  1243. */
  1244. if(td->csw & (Tdstalled|Tdactive)){
  1245. if(saved++ == 0)
  1246. io->toggle = td->token & Tddata1;
  1247. }else{
  1248. tot += td->ndata;
  1249. if(c != nil && tdtok(td) == Tdtokin && td->ndata > 0){
  1250. memmove(c, td->data, td->ndata);
  1251. c += td->ndata;
  1252. }
  1253. }
  1254. ntd = td->next;
  1255. tdfree(td);
  1256. }
  1257. err = io->err;
  1258. if(mustlock){
  1259. qunlock(io);
  1260. poperror();
  1261. }
  1262. ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
  1263. io, ntds, tot, err);
  1264. if(err != nil)
  1265. error(err);
  1266. if(tot < 0)
  1267. error(Eio);
  1268. return tot;
  1269. }
  1270. /*
  1271. * halt condition was cleared on the endpoint. update our toggles.
  1272. */
  1273. static void
  1274. clrhalt(Ep *ep)
  1275. {
  1276. Qio *io;
  1277. ep->clrhalt = 0;
  1278. switch(ep->ttype){
  1279. case Tbulk:
  1280. case Tintr:
  1281. io = ep->aux;
  1282. if(ep->mode != OREAD){
  1283. qlock(&io[OWRITE]);
  1284. io[OWRITE].toggle = Tddata0;
  1285. deprint("ep clrhalt for io %#p\n", io+OWRITE);
  1286. qunlock(&io[OWRITE]);
  1287. }
  1288. if(ep->mode != OWRITE){
  1289. qlock(&io[OREAD]);
  1290. io[OREAD].toggle = Tddata0;
  1291. deprint("ep clrhalt for io %#p\n", io+OREAD);
  1292. qunlock(&io[OREAD]);
  1293. }
  1294. break;
  1295. }
  1296. }
  1297. static int32_t
  1298. epread(Ep *ep, void *a, int32_t count)
  1299. {
  1300. Proc *up = externup();
  1301. Ctlio *cio;
  1302. Qio *io;
  1303. Isoio *iso;
  1304. char buf[160];
  1305. uint32_t delta;
  1306. ddeprint("uhci: epread\n");
  1307. if(ep->aux == nil)
  1308. panic("epread: not open");
  1309. switch(ep->ttype){
  1310. case Tctl:
  1311. cio = ep->aux;
  1312. qlock(cio);
  1313. if(waserror()){
  1314. qunlock(cio);
  1315. nexterror();
  1316. }
  1317. ddeprint("epread ctl ndata %d\n", cio->ndata);
  1318. if(cio->ndata < 0)
  1319. error("request expected");
  1320. else if(cio->ndata == 0){
  1321. cio->ndata = -1;
  1322. count = 0;
  1323. }else{
  1324. if(count > cio->ndata)
  1325. count = cio->ndata;
  1326. if(count > 0)
  1327. memmove(a, cio->data, count);
  1328. /* BUG for big transfers */
  1329. free(cio->data);
  1330. cio->data = nil;
  1331. cio->ndata = 0; /* signal EOF next time */
  1332. }
  1333. qunlock(cio);
  1334. poperror();
  1335. if(debug>1 || ep->debug){
  1336. seprintdata(buf, buf+sizeof(buf), a, count);
  1337. print("epread: %s\n", buf);
  1338. }
  1339. return count;
  1340. case Tbulk:
  1341. io = ep->aux;
  1342. if(ep->clrhalt)
  1343. clrhalt(ep);
  1344. return epio(ep, &io[OREAD], a, count, 1);
  1345. case Tintr:
  1346. io = ep->aux;
  1347. delta = TK2MS(machp()->ticks) - io[OREAD].iotime + 1;
  1348. if(delta < ep->pollival / 2)
  1349. tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
  1350. if(ep->clrhalt)
  1351. clrhalt(ep);
  1352. return epio(ep, &io[OREAD], a, count, 1);
  1353. case Tiso:
  1354. iso = ep->aux;
  1355. return episoread(ep, iso, a, count);
  1356. default:
  1357. panic("epread: bad ep ttype %d", ep->ttype);
  1358. }
  1359. return -1;
  1360. }
  1361. /*
  1362. * Control transfers are one setup write (data0)
  1363. * plus zero or more reads/writes (data1, data0, ...)
  1364. * plus a final write/read with data1 to ack.
  1365. * For both host to device and device to host we perform
  1366. * the entire transfer when the user writes the request,
  1367. * and keep any data read from the device for a later read.
  1368. * We call epio three times instead of placing all Tds at
  1369. * the same time because doing so leads to crc/tmout errors
  1370. * for some devices.
  1371. * Upon errors on the data phase we must still run the status
  1372. * phase or the device may cease responding in the future.
  1373. */
  1374. static int32_t
  1375. epctlio(Ep *ep, Ctlio *cio, void *a, int32_t count)
  1376. {
  1377. Proc *up = externup();
  1378. unsigned char *c;
  1379. int32_t len;
  1380. ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
  1381. cio, ep->dev->nb, ep->nb, count);
  1382. if(count < Rsetuplen)
  1383. error("short usb comand");
  1384. qlock(cio);
  1385. free(cio->data);
  1386. cio->data = nil;
  1387. cio->ndata = 0;
  1388. if(waserror()){
  1389. qunlock(cio);
  1390. free(cio->data);
  1391. cio->data = nil;
  1392. cio->ndata = 0;
  1393. nexterror();
  1394. }
  1395. /* set the address if unset and out of configuration state */
  1396. if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
  1397. if(cio->usbid == 0)
  1398. cio->usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb&Devmax);
  1399. c = a;
  1400. cio->tok = Tdtoksetup;
  1401. cio->toggle = Tddata0;
  1402. if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
  1403. error(Eio);
  1404. a = c + Rsetuplen;
  1405. count -= Rsetuplen;
  1406. cio->toggle = Tddata1;
  1407. if(c[Rtype] & Rd2h){
  1408. cio->tok = Tdtokin;
  1409. len = GET2(c+Rcount);
  1410. if(len <= 0)
  1411. error("bad length in d2h request");
  1412. if(len > Maxctllen)
  1413. error("d2h data too large to fit in uhci");
  1414. a = cio->data = smalloc(len+1);
  1415. }else{
  1416. cio->tok = Tdtokout;
  1417. len = count;
  1418. }
  1419. if(len > 0)
  1420. if(waserror())
  1421. len = -1;
  1422. else{
  1423. len = epio(ep, cio, a, len, 0);
  1424. poperror();
  1425. }
  1426. if(c[Rtype] & Rd2h){
  1427. count = Rsetuplen;
  1428. cio->ndata = len;
  1429. cio->tok = Tdtokout;
  1430. }else{
  1431. if(len < 0)
  1432. count = -1;
  1433. else
  1434. count = Rsetuplen + len;
  1435. cio->tok = Tdtokin;
  1436. }
  1437. cio->toggle = Tddata1;
  1438. epio(ep, cio, nil, 0, 0);
  1439. qunlock(cio);
  1440. poperror();
  1441. ddeprint("epctlio cio %#p return %ld\n", cio, count);
  1442. return count;
  1443. }
  1444. static int32_t
  1445. epwrite(Ep *ep, void *a, int32_t count)
  1446. {
  1447. Proc *up = externup();
  1448. Ctlio *cio;
  1449. Isoio *iso;
  1450. Qio *io;
  1451. uint32_t delta;
  1452. char *b;
  1453. int tot;
  1454. int nw;
  1455. ddeprint("uhci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
  1456. if(ep->aux == nil)
  1457. panic("uhci: epwrite: not open");
  1458. switch(ep->ttype){
  1459. case Tctl:
  1460. cio = ep->aux;
  1461. return epctlio(ep, cio, a, count);
  1462. case Tbulk:
  1463. io = ep->aux;
  1464. if(ep->clrhalt)
  1465. clrhalt(ep);
  1466. /*
  1467. * Put at most Tdatomic Tds (512 bytes) at a time.
  1468. * Otherwise some devices produce babble errors.
  1469. */
  1470. b = a;
  1471. for(tot = 0; tot < count ; tot += nw){
  1472. nw = count - tot;
  1473. if(nw > Tdatomic * ep->maxpkt)
  1474. nw = Tdatomic * ep->maxpkt;
  1475. nw = epio(ep, &io[OWRITE], b+tot, nw, 1);
  1476. }
  1477. return tot;
  1478. case Tintr:
  1479. io = ep->aux;
  1480. delta = TK2MS(machp()->ticks) - io[OWRITE].iotime + 1;
  1481. if(delta < ep->pollival)
  1482. tsleep(&up->sleep, return0, 0, ep->pollival - delta);
  1483. if(ep->clrhalt)
  1484. clrhalt(ep);
  1485. return epio(ep, &io[OWRITE], a, count, 1);
  1486. case Tiso:
  1487. iso = ep->aux;
  1488. return episowrite(ep, iso, a, count);
  1489. default:
  1490. panic("uhci: epwrite: bad ep ttype %d", ep->ttype);
  1491. }
  1492. return -1;
  1493. }
  1494. static void
  1495. isoopen(Ep *ep)
  1496. {
  1497. Ctlr *ctlr;
  1498. Isoio *iso;
  1499. int frno;
  1500. int i;
  1501. Td* td;
  1502. Td* ltd;
  1503. int size;
  1504. int left;
  1505. if(ep->mode == ORDWR)
  1506. error("iso i/o is half-duplex");
  1507. ctlr = ep->hp->aux;
  1508. iso = ep->aux;
  1509. iso->debug = ep->debug;
  1510. iso->next = nil; /* paranoia */
  1511. if(ep->mode == OREAD)
  1512. iso->tok = Tdtokin;
  1513. else
  1514. iso->tok = Tdtokout;
  1515. iso->usbid = ((ep->nb & Epmax)<<7)|(ep->dev->nb & Devmax);
  1516. iso->state = Qidle;
  1517. iso->nframes = Nframes/ep->pollival;
  1518. if(iso->nframes < 3)
  1519. error("uhci isoopen bug"); /* we need at least 3 tds */
  1520. ilock(ctlr);
  1521. if(ctlr->load + ep->load > 800)
  1522. print("usb: uhci: bandwidth may be exceeded\n");
  1523. ctlr->load += ep->load;
  1524. ctlr->isoload += ep->load;
  1525. dprint("uhci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
  1526. iunlock(ctlr);
  1527. /*
  1528. * From here on this cannot raise errors
  1529. * unless we catch them and release here all memory allocated.
  1530. */
  1531. if(ep->maxpkt > Tdndata)
  1532. iso->data = smalloc(iso->nframes*ep->maxpkt);
  1533. ilock(ctlr);
  1534. frno = INS(Frnum) + 10; /* start 10ms ahead */
  1535. frno = TRUNC(frno, Nframes);
  1536. iunlock(ctlr);
  1537. iso->td0frno = frno;
  1538. ltd = nil;
  1539. left = 0;
  1540. for(i = 0; i < iso->nframes; i++){
  1541. td = iso->tdps[frno] = tdalloc();
  1542. if(ep->mode == OREAD)
  1543. size = ep->maxpkt;
  1544. else{
  1545. size = (ep->hz+left) * ep->pollival / 1000;
  1546. size *= ep->samplesz;
  1547. left = (ep->hz+left) * ep->pollival % 1000;
  1548. if(size > ep->maxpkt){
  1549. print("uhci: ep%d.%d: size > maxpkt\n",
  1550. ep->dev->nb, ep->nb);
  1551. print("size = %d max = %ld\n", size, ep->maxpkt);
  1552. size = ep->maxpkt;
  1553. }
  1554. }
  1555. if(size > Tdndata)
  1556. td->data = iso->data + i * ep->maxpkt;
  1557. else
  1558. td->data = td->sbuff;
  1559. td->buffer = PCIWADDR(td->data);
  1560. tdisoinit(iso, td, size);
  1561. if(ltd != nil)
  1562. ltd->next = td;
  1563. ltd = td;
  1564. frno = TRUNC(frno+ep->pollival, Nframes);
  1565. }
  1566. ltd->next = iso->tdps[iso->td0frno];
  1567. iso->tdi = iso->tdps[iso->td0frno];
  1568. iso->tdu = iso->tdi; /* read: right now; write: 1s ahead */
  1569. ilock(ctlr);
  1570. frno = iso->td0frno;
  1571. for(i = 0; i < iso->nframes; i++){
  1572. iso->tdps[frno]->link = ctlr->frames[frno];
  1573. frno = TRUNC(frno+ep->pollival, Nframes);
  1574. }
  1575. coherence();
  1576. frno = iso->td0frno;
  1577. for(i = 0; i < iso->nframes; i++){
  1578. ctlr->frames[frno] = PCIWADDR(iso->tdps[frno]);
  1579. frno = TRUNC(frno+ep->pollival, Nframes);
  1580. }
  1581. iso->next = ctlr->iso;
  1582. ctlr->iso = iso;
  1583. iso->state = Qdone;
  1584. iunlock(ctlr);
  1585. if(debug > 1 || iso->debug >1)
  1586. isodump(iso, 0);
  1587. }
  1588. /*
  1589. * Allocate the endpoint and set it up for I/O
  1590. * in the controller. This must follow what's said
  1591. * in Ep regarding configuration, including perhaps
  1592. * the saved toggles (saved on a previous close of
  1593. * the endpoint data file by epclose).
  1594. */
  1595. static void
  1596. epopen(Ep *ep)
  1597. {
  1598. Proc *up = externup();
  1599. Ctlr *ctlr;
  1600. Qh *cqh;
  1601. Qio *io;
  1602. Ctlio *cio;
  1603. int usbid;
  1604. ctlr = ep->hp->aux;
  1605. deprint("uhci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
  1606. if(ep->aux != nil)
  1607. panic("uhci: epopen called with open ep");
  1608. if(waserror()){
  1609. free(ep->aux);
  1610. ep->aux = nil;
  1611. nexterror();
  1612. }
  1613. if(ep->maxpkt > Tdmaxpkt){
  1614. print("uhci: maxkpkt too large: using %d\n", Tdmaxpkt);
  1615. ep->maxpkt = Tdmaxpkt;
  1616. }
  1617. cqh = ctlr->qh[ep->ttype];
  1618. switch(ep->ttype){
  1619. case Tnone:
  1620. error("endpoint not configured");
  1621. case Tiso:
  1622. ep->aux = smalloc(sizeof(Isoio));
  1623. isoopen(ep);
  1624. break;
  1625. case Tctl:
  1626. cio = ep->aux = smalloc(sizeof(Ctlio));
  1627. cio->debug = ep->debug;
  1628. cio->ndata = -1;
  1629. cio->data = nil;
  1630. if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
  1631. break;
  1632. cio->qh = qhalloc(ctlr, cqh, cio, "epc");
  1633. break;
  1634. case Tbulk:
  1635. case Tintr:
  1636. io = ep->aux = smalloc(sizeof(Qio)*2);
  1637. io[OREAD].debug = io[OWRITE].debug = ep->debug;
  1638. usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb &Devmax);
  1639. if(ep->mode != OREAD){
  1640. if(ep->toggle[OWRITE] != 0)
  1641. io[OWRITE].toggle = Tddata1;
  1642. else
  1643. io[OWRITE].toggle = Tddata0;
  1644. io[OWRITE].tok = Tdtokout;
  1645. io[OWRITE].qh = qhalloc(ctlr, cqh, io+OWRITE, "epw");
  1646. io[OWRITE].usbid = usbid;
  1647. }
  1648. if(ep->mode != OWRITE){
  1649. if(ep->toggle[OREAD] != 0)
  1650. io[OREAD].toggle = Tddata1;
  1651. else
  1652. io[OREAD].toggle = Tddata0;
  1653. io[OREAD].tok = Tdtokin;
  1654. io[OREAD].qh = qhalloc(ctlr, cqh, io+OREAD, "epr");
  1655. io[OREAD].usbid = usbid;
  1656. }
  1657. break;
  1658. }
  1659. if(debug>1 || ep->debug)
  1660. dump(ep->hp);
  1661. deprint("uhci: epopen done\n");
  1662. poperror();
  1663. }
  1664. static void
  1665. cancelio(Ctlr *ctlr, Qio *io)
  1666. {
  1667. Proc *up = externup();
  1668. Qh *qh;
  1669. ilock(ctlr);
  1670. qh = io->qh;
  1671. if(io == nil || io->qh == nil || io->qh->state == Qclose){
  1672. iunlock(ctlr);
  1673. return;
  1674. }
  1675. dqprint("uhci: cancelio for qh %#p state %s\n",
  1676. qh, qhsname[qh->state]);
  1677. aborttds(qh);
  1678. qh->state = Qclose;
  1679. iunlock(ctlr);
  1680. if(!waserror()){
  1681. tsleep(&up->sleep, return0, 0, Abortdelay);
  1682. poperror();
  1683. }
  1684. wakeup(io);
  1685. qlock(io);
  1686. /* wait for epio if running */
  1687. qunlock(io);
  1688. qhfree(ctlr, qh);
  1689. io->qh = nil;
  1690. }
  1691. static void
  1692. cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, uint32_t load)
  1693. {
  1694. Proc *up = externup();
  1695. Isoio **il;
  1696. uint32_t *lp;
  1697. int i;
  1698. int frno;
  1699. Td *td;
  1700. ilock(ctlr);
  1701. if(iso->state == Qclose){
  1702. iunlock(ctlr);
  1703. return;
  1704. }
  1705. if(iso->state != Qrun && iso->state != Qdone)
  1706. panic("bad iso state");
  1707. iso->state = Qclose;
  1708. if(ctlr->isoload < load)
  1709. panic("uhci: low isoload");
  1710. ctlr->isoload -= load;
  1711. ctlr->load -= load;
  1712. for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
  1713. if(*il == iso)
  1714. break;
  1715. if(*il == nil)
  1716. panic("isocancel: not found");
  1717. *il = iso->next;
  1718. frno = iso->td0frno;
  1719. for(i = 0; i < iso->nframes; i++){
  1720. td = iso->tdps[frno];
  1721. td->csw &= ~(Tdioc|Tdactive);
  1722. for(lp=&ctlr->frames[frno]; !(*lp & Tdterm);
  1723. lp = &TPTR(*lp)->link)
  1724. if(TPTR(*lp) == td)
  1725. break;
  1726. if(*lp & Tdterm)
  1727. panic("cancelisoio: td not found");
  1728. *lp = td->link;
  1729. frno = TRUNC(frno+pollival, Nframes);
  1730. }
  1731. iunlock(ctlr);
  1732. /*
  1733. * wakeup anyone waiting for I/O and
  1734. * wait to be sure no I/O is in progress in the controller.
  1735. * and then wait to be sure episo-io is no int32_ter running.
  1736. */
  1737. wakeup(iso);
  1738. diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
  1739. tsleep(&up->sleep, return0, 0, 5);
  1740. qlock(iso);
  1741. qunlock(iso);
  1742. diprint("cancelisoio iso %#p releasing iso\n", iso);
  1743. frno = iso->td0frno;
  1744. for(i = 0; i < iso->nframes; i++){
  1745. tdfree(iso->tdps[frno]);
  1746. iso->tdps[frno] = nil;
  1747. frno = TRUNC(frno+pollival, Nframes);
  1748. }
  1749. free(iso->data);
  1750. iso->data = nil;
  1751. }
  1752. static void
  1753. epclose(Ep *ep)
  1754. {
  1755. Ctlr *ctlr;
  1756. Ctlio *cio;
  1757. Isoio *iso;
  1758. Qio *io;
  1759. ctlr = ep->hp->aux;
  1760. deprint("uhci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
  1761. if(ep->aux == nil)
  1762. panic("uhci: epclose called with closed ep");
  1763. switch(ep->ttype){
  1764. case Tctl:
  1765. cio = ep->aux;
  1766. cancelio(ctlr, cio);
  1767. free(cio->data);
  1768. cio->data = nil;
  1769. break;
  1770. case Tbulk:
  1771. case Tintr:
  1772. io = ep->aux;
  1773. ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
  1774. if(ep->mode != OWRITE){
  1775. cancelio(ctlr, &io[OREAD]);
  1776. if(io[OREAD].toggle == Tddata1)
  1777. ep->toggle[OREAD] = 1;
  1778. }
  1779. if(ep->mode != OREAD){
  1780. cancelio(ctlr, &io[OWRITE]);
  1781. if(io[OWRITE].toggle == Tddata1)
  1782. ep->toggle[OWRITE] = 1;
  1783. }
  1784. break;
  1785. case Tiso:
  1786. iso = ep->aux;
  1787. cancelisoio(ctlr, iso, ep->pollival, ep->load);
  1788. break;
  1789. default:
  1790. panic("epclose: bad ttype %d", ep->ttype);
  1791. }
  1792. free(ep->aux);
  1793. ep->aux = nil;
  1794. }
  1795. static char*
  1796. seprintep(char *s, char *e, Ep *ep)
  1797. {
  1798. Ctlio *cio;
  1799. Qio *io;
  1800. Isoio *iso;
  1801. Ctlr *ctlr;
  1802. ctlr = ep->hp->aux;
  1803. ilock(ctlr);
  1804. if(ep->aux == nil){
  1805. *s = 0;
  1806. iunlock(ctlr);
  1807. return s;
  1808. }
  1809. switch(ep->ttype){
  1810. case Tctl:
  1811. cio = ep->aux;
  1812. s = seprint(s,e,"cio %#p qh %#p"
  1813. " id %#x tog %#x tok %#x err %s\n",
  1814. cio, cio->qh, cio->usbid, cio->toggle,
  1815. cio->tok, cio->err);
  1816. break;
  1817. case Tbulk:
  1818. case Tintr:
  1819. io = ep->aux;
  1820. if(ep->mode != OWRITE)
  1821. s = seprint(s,e,"r: qh %#p id %#x tog %#x tok %#x err %s\n",
  1822. io[OREAD].qh, io[OREAD].usbid, io[OREAD].toggle,
  1823. io[OREAD].tok, io[OREAD].err);
  1824. if(ep->mode != OREAD)
  1825. s = seprint(s,e,"w: qh %#p id %#x tog %#x tok %#x err %s\n",
  1826. io[OWRITE].qh, io[OWRITE].usbid, io[OWRITE].toggle,
  1827. io[OWRITE].tok, io[OWRITE].err);
  1828. break;
  1829. case Tiso:
  1830. iso = ep->aux;
  1831. s = seprint(s,e,"iso %#p id %#x tok %#x tdu %#p tdi %#p err %s\n",
  1832. iso, iso->usbid, iso->tok, iso->tdu, iso->tdi, iso->err);
  1833. break;
  1834. }
  1835. iunlock(ctlr);
  1836. return s;
  1837. }
  1838. static int
  1839. portenable(Hci *hp, int port, int on)
  1840. {
  1841. Proc *up = externup();
  1842. int s;
  1843. int ioport;
  1844. Ctlr *ctlr;
  1845. ctlr = hp->aux;
  1846. dprint("uhci: %#x port %d enable=%d\n", ctlr->port, port, on);
  1847. ioport = PORT(port-1);
  1848. qlock(&ctlr->portlck);
  1849. if(waserror()){
  1850. qunlock(&ctlr->portlck);
  1851. nexterror();
  1852. }
  1853. ilock(ctlr);
  1854. s = INS(ioport);
  1855. if(on)
  1856. OUTS(ioport, s | PSenable);
  1857. else
  1858. OUTS(ioport, s & ~PSenable);
  1859. microdelay(64);
  1860. iunlock(ctlr);
  1861. tsleep(&up->sleep, return0, 0, Enabledelay);
  1862. dprint("uhci %#ux port %d enable=%d: sts %#x\n",
  1863. ctlr->port, port, on, INS(ioport));
  1864. qunlock(&ctlr->portlck);
  1865. poperror();
  1866. return 0;
  1867. }
  1868. static int
  1869. portreset(Hci *hp, int port, int on)
  1870. {
  1871. int i, p;
  1872. Ctlr *ctlr;
  1873. if(on == 0)
  1874. return 0;
  1875. ctlr = hp->aux;
  1876. dprint("uhci: %#ux port %d reset\n", ctlr->port, port);
  1877. p = PORT(port-1);
  1878. ilock(ctlr);
  1879. OUTS(p, PSreset);
  1880. delay(50);
  1881. OUTS(p, INS(p) & ~PSreset);
  1882. OUTS(p, INS(p) | PSenable);
  1883. microdelay(64);
  1884. for(i=0; i<1000 && (INS(p) & PSenable) == 0; i++)
  1885. ;
  1886. OUTS(p, (INS(p) & ~PSreset)|PSenable);
  1887. iunlock(ctlr);
  1888. dprint("uhci %#ux after port %d reset: sts %#x\n",
  1889. ctlr->port, port, INS(p));
  1890. return 0;
  1891. }
  1892. static int
  1893. portstatus(Hci *hp, int port)
  1894. {
  1895. Proc *up = externup();
  1896. int s;
  1897. int r;
  1898. int ioport;
  1899. Ctlr *ctlr;
  1900. ctlr = hp->aux;
  1901. ioport = PORT(port-1);
  1902. qlock(&ctlr->portlck);
  1903. if(waserror()){
  1904. iunlock(ctlr);
  1905. qunlock(&ctlr->portlck);
  1906. nexterror();
  1907. }
  1908. ilock(ctlr);
  1909. s = INS(ioport);
  1910. if(s & (PSstatuschg | PSchange)){
  1911. OUTS(ioport, s);
  1912. ddprint("uhci %#ux port %d status %#x\n", ctlr->port, port, s);
  1913. }
  1914. iunlock(ctlr);
  1915. qunlock(&ctlr->portlck);
  1916. poperror();
  1917. /*
  1918. * We must return status bits as a
  1919. * get port status hub request would do.
  1920. */
  1921. r = 0;
  1922. if(s & PSpresent)
  1923. r |= HPpresent;
  1924. if(s & PSenable)
  1925. r |= HPenable;
  1926. if(s & PSsuspend)
  1927. r |= HPsuspend;
  1928. if(s & PSreset)
  1929. r |= HPreset;
  1930. if(s & PSslow)
  1931. r |= HPslow;
  1932. if(s & PSstatuschg)
  1933. r |= HPstatuschg;
  1934. if(s & PSchange)
  1935. r |= HPchange;
  1936. return r;
  1937. }
  1938. static void
  1939. scanpci(void)
  1940. {
  1941. static int already = 0;
  1942. int io;
  1943. int i;
  1944. Ctlr *ctlr;
  1945. Pcidev *p;
  1946. if(already)
  1947. return;
  1948. already = 1;
  1949. p = nil;
  1950. while(p = pcimatch(p, 0, 0)){
  1951. /*
  1952. * Find UHCI controllers (Programming Interface = 0).
  1953. */
  1954. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb)
  1955. continue;
  1956. switch(p->ccrp){
  1957. case 0:
  1958. io = p->mem[4].bar & ~0x0F;
  1959. break;
  1960. default:
  1961. continue;
  1962. }
  1963. if(io == 0){
  1964. print("usbuhci: %#x %#x: failed to map registers\n",
  1965. p->vid, p->did);
  1966. continue;
  1967. }
  1968. if(ioalloc(io, p->mem[4].size, 0, "usbuhci") < 0){
  1969. print("usbuhci: port %#ux in use\n", io);
  1970. continue;
  1971. }
  1972. if(p->intl == 0xFF || p->intl == 0){
  1973. print("usbuhci: no irq assigned for port %#ux\n", io);
  1974. continue;
  1975. }
  1976. dprint("uhci: %#x %#x: port %#ux size %#x irq %d\n",
  1977. p->vid, p->did, io, p->mem[4].size, p->intl);
  1978. ctlr = malloc(sizeof(Ctlr));
  1979. if (ctlr == nil)
  1980. panic("uhci: out of memory");
  1981. ctlr->pcidev = p;
  1982. ctlr->port = io;
  1983. for(i = 0; i < Nhcis; i++)
  1984. if(ctlrs[i] == nil){
  1985. ctlrs[i] = ctlr;
  1986. break;
  1987. }
  1988. if(i == Nhcis)
  1989. print("uhci: bug: no more controllers\n");
  1990. }
  1991. }
  1992. static void
  1993. uhcimeminit(Ctlr *ctlr)
  1994. {
  1995. Td* td;
  1996. Qh *qh;
  1997. int frsize;
  1998. int i;
  1999. ctlr->qhs = ctlr->qh[Tctl] = qhalloc(ctlr, nil, nil, "CTL");
  2000. ctlr->qh[Tintr] = qhalloc(ctlr, ctlr->qh[Tctl], nil, "INT");
  2001. ctlr->qh[Tbulk] = qhalloc(ctlr, ctlr->qh[Tintr], nil, "BLK");
  2002. /* idle Td from dummy Qh at the end. looped back to itself */
  2003. /* This is a workaround for PIIX4 errata 29773804.pdf */
  2004. qh = qhalloc(ctlr, ctlr->qh[Tbulk], nil, "BWS");
  2005. td = tdalloc();
  2006. td->link = PCIWADDR(td);
  2007. qhlinktd(qh, td);
  2008. /* loop (hw only) from the last qh back to control xfers.
  2009. * this may be done only for some of them. Disable until ehci comes.
  2010. */
  2011. if(0)
  2012. qh->link = PCIWADDR(ctlr->qhs);
  2013. frsize = Nframes*sizeof(uint32_t);
  2014. ctlr->frames = xspanalloc(frsize, frsize, 0);
  2015. if(ctlr->frames == nil)
  2016. panic("uhci reset: no memory");
  2017. ctlr->iso = nil;
  2018. for(i = 0; i < Nframes; i++)
  2019. ctlr->frames[i] = PCIWADDR(ctlr->qhs)|QHlinkqh;
  2020. OUTL(Flbaseadd, PCIWADDR(ctlr->frames));
  2021. OUTS(Frnum, 0);
  2022. dprint("uhci %#ux flb %#ulx frno %#ux\n", ctlr->port,
  2023. INL(Flbaseadd), INS(Frnum));
  2024. }
  2025. static void
  2026. init(Hci *hp)
  2027. {
  2028. Ctlr *ctlr;
  2029. int sts;
  2030. int i;
  2031. ctlr = hp->aux;
  2032. dprint("uhci %#ux init\n", ctlr->port);
  2033. coherence();
  2034. ilock(ctlr);
  2035. OUTS(Usbintr, Itmout|Iresume|Ioc|Ishort);
  2036. uhcirun(ctlr, 1);
  2037. dprint("uhci: init: cmd %#ux sts %#ux sof %#ux",
  2038. INS(Cmd), INS(Status), INS(SOFmod));
  2039. dprint(" flb %#ulx frno %#ux psc0 %#ux psc1 %#ux",
  2040. INL(Flbaseadd), INS(Frnum), INS(PORT(0)), INS(PORT(1)));
  2041. /* guess other ports */
  2042. for(i = 2; i < 6; i++){
  2043. sts = INS(PORT(i));
  2044. if(sts != 0xFFFF && (sts & PSreserved1) == 1){
  2045. dprint(" psc%d %#ux", i, sts);
  2046. hp->nports++;
  2047. }else
  2048. break;
  2049. }
  2050. for(i = 0; i < hp->nports; i++)
  2051. OUTS(PORT(i), 0);
  2052. iunlock(ctlr);
  2053. }
  2054. static void
  2055. uhcireset(Ctlr *ctlr)
  2056. {
  2057. int i;
  2058. int sof;
  2059. ilock(ctlr);
  2060. dprint("uhci %#ux reset\n", ctlr->port);
  2061. /*
  2062. * Turn off legacy mode. Some controllers won't
  2063. * interrupt us as expected otherwise.
  2064. */
  2065. uhcirun(ctlr, 0);
  2066. pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
  2067. OUTS(Usbintr, 0);
  2068. sof = INB(SOFmod);
  2069. uhcicmd(ctlr, Cgreset); /* global reset */
  2070. delay(Resetdelay);
  2071. uhcicmd(ctlr, 0); /* all halt */
  2072. uhcicmd(ctlr, Chcreset); /* controller reset */
  2073. for(i = 0; i < 100; i++){
  2074. if((INS(Cmd) & Chcreset) == 0)
  2075. break;
  2076. delay(1);
  2077. }
  2078. if(i == 100)
  2079. print("uhci %#x controller reset timed out\n", ctlr->port);
  2080. OUTB(SOFmod, sof);
  2081. iunlock(ctlr);
  2082. }
  2083. static void
  2084. setdebug(Hci *hci, int d)
  2085. {
  2086. debug = d;
  2087. }
  2088. static void
  2089. shutdown(Hci *hp)
  2090. {
  2091. Ctlr *ctlr;
  2092. ctlr = hp->aux;
  2093. ilock(ctlr);
  2094. uhcirun(ctlr, 0);
  2095. delay(100);
  2096. iunlock(ctlr);
  2097. }
  2098. static int
  2099. reset(Hci *hp)
  2100. {
  2101. static Lock resetlck;
  2102. int i;
  2103. Ctlr *ctlr;
  2104. Pcidev *p;
  2105. /*
  2106. if(getconf("*nousbuhci"))
  2107. return -1;
  2108. */
  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->port == 0 || hp->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->aux = ctlr;
  2129. hp->port = ctlr->port;
  2130. hp->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->init = init;
  2139. hp->dump = dump;
  2140. hp->interrupt = interrupt;
  2141. hp->epopen = epopen;
  2142. hp->epclose = epclose;
  2143. hp->epread = epread;
  2144. hp->epwrite = epwrite;
  2145. hp->seprintep = seprintep;
  2146. hp->portenable = portenable;
  2147. hp->portreset = portreset;
  2148. hp->portstatus = portstatus;
  2149. hp->shutdown = shutdown;
  2150. hp->debug = setdebug;
  2151. hp->type = "uhci";
  2152. return 0;
  2153. }
  2154. void
  2155. usbuhcilink(void)
  2156. {
  2157. addhcitype("uhci", reset);
  2158. }