usbuhci.c 50 KB

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