usbohci.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  1. /*
  2. * USB Open Host Controller Interface (OHCI) driver
  3. * from devohci.c provided by Charles Forsyth, 5 Aug 2006.
  4. */
  5. #include "u.h"
  6. #include "../port/lib.h"
  7. #include "mem.h"
  8. #include "dat.h"
  9. #include "fns.h"
  10. #include "io.h"
  11. #include "../port/error.h"
  12. #include "usb.h"
  13. #define XPRINT if(usbhdebug) print
  14. #define XIPRINT if(usbhdebug) iprint
  15. #define XEPRINT if(usbhdebug || ep->debug) print
  16. #define XEIPRINT if(usbhdebug || ep->debug) iprint
  17. #define IPRINT(x) iprint x
  18. static int usbhdebug = 0;
  19. static int dcls;
  20. enum {
  21. Ned = 63 + 32,
  22. Ntd = 256,
  23. };
  24. /*
  25. * USB packet definitions
  26. */
  27. enum {
  28. Otoksetup = 0,
  29. Otokout = 1,
  30. Otokin = 2,
  31. /* port status - UHCI style */
  32. Suspend = 1<<12,
  33. PortReset = 1<<9,
  34. SlowDevice = 1<<8,
  35. ResumeDetect = 1<<6,
  36. PortEnableChange = 1<<3, /* write 1 to clear */
  37. PortEnable = 1<<2,
  38. ConnectStatusChange = 1<<1, /* write 1 to clear */
  39. DevicePresent = 1<<0,
  40. };
  41. typedef struct Ctlr Ctlr;
  42. typedef struct QTree QTree;
  43. enum {
  44. ED_MPS_MASK = 0x7ff,
  45. ED_MPS_SHIFT = 16,
  46. ED_C_MASK = 1,
  47. ED_C_SHIFT = 1,
  48. ED_F_BIT = 1 << 15,
  49. ED_S_MASK = 1,
  50. ED_S_SHIFT = 13,
  51. ED_D_MASK = 3,
  52. ED_D_SHIFT = 11,
  53. ED_H_MASK = 1,
  54. ED_H_SHIFT = 0,
  55. };
  56. typedef struct Endptx Endptx;
  57. typedef struct TD TD;
  58. struct Endptx
  59. {
  60. Lock; /* for manipulating ed */
  61. ED *ed; /* Single endpoint descriptor */
  62. int ntd; /* Number of TDs in use */
  63. int overruns;
  64. };
  65. struct TD {
  66. ulong ctrl;
  67. ulong cbp;
  68. ulong nexttd;
  69. ulong be;
  70. ushort offsets[8]; /* Iso TDs only */
  71. /* driver specific; pad to multiple of 32 */
  72. TD* next;
  73. Endpt *ep;
  74. Block *bp;
  75. ulong flags;
  76. ulong offset; /* offset associated with end of data */
  77. ulong bytes; /* bytes in this TD */
  78. ulong pad[2];
  79. };
  80. enum {
  81. TD_R_SHIFT = 18,
  82. TD_DP_MASK = 3,
  83. TD_DP_SHIFT = 19,
  84. TD_CC_MASK = 0xf,
  85. TD_CC_SHIFT = 28,
  86. TD_EC_MASK = 3,
  87. TD_EC_SHIFT = 26,
  88. TD_FLAGS_LAST = 1 << 0,
  89. };
  90. typedef struct HCCA HCCA;
  91. struct HCCA {
  92. ulong intrtable[32];
  93. ushort framenumber;
  94. ushort pad1;
  95. ulong donehead;
  96. uchar reserved[116];
  97. };
  98. /* OHCI registers */
  99. typedef struct OHCI OHCI;
  100. struct OHCI {
  101. /* control and status group */
  102. /*00*/ ulong revision;
  103. ulong control;
  104. ulong cmdsts;
  105. ulong intrsts;
  106. /*10*/ ulong intrenable;
  107. ulong intrdisable;
  108. /* memory pointer group */
  109. ulong hcca;
  110. ulong periodcurred;
  111. /*20*/ ulong ctlheaded;
  112. ulong ctlcurred;
  113. ulong bulkheaded;
  114. ulong bulkcurred;
  115. /*30*/ ulong donehead;
  116. /* frame counter group */
  117. ulong fminterval;
  118. ulong fmremaining;
  119. ulong fmnumber;
  120. /*40*/ ulong periodicstart;
  121. ulong lsthreshold;
  122. /* root hub group */
  123. ulong rhdesca;
  124. ulong rhdescb;
  125. /*50*/ ulong rhsts;
  126. ulong rhportsts[15];
  127. /*90*/ ulong pad25[20];
  128. /* unknown */
  129. /*e0*/ ulong hostueaddr;
  130. ulong hostuests;
  131. ulong hosttimeoutctrl;
  132. ulong pad59;
  133. /*f0*/ ulong pad60;
  134. ulong hostrevision;
  135. ulong pad62[2];
  136. /*100*/
  137. };
  138. /*
  139. * software structures
  140. */
  141. static struct {
  142. int bit;
  143. char *name;
  144. } portstatus[] = {
  145. { Suspend, "suspend", },
  146. { PortReset, "reset", },
  147. { SlowDevice, "lowspeed", },
  148. { ResumeDetect, "resume", },
  149. { PortEnableChange, "portchange", },
  150. { PortEnable, "enable", },
  151. { ConnectStatusChange, "statuschange", },
  152. { DevicePresent, "present", },
  153. };
  154. struct QTree {
  155. QLock;
  156. int nel;
  157. int depth;
  158. ulong* bw;
  159. ED **root;
  160. };
  161. /* device parameters */
  162. static char *devstates[] = {
  163. [Disabled] "Disabled",
  164. [Attached] "Attached",
  165. [Enabled] "Enabled",
  166. };
  167. struct Ctlr {
  168. Lock; /* protects state shared with interrupt (eg, free list) */
  169. int active;
  170. Pcidev* pcidev;
  171. int irq;
  172. ulong tbdf;
  173. Ctlr* next;
  174. int nports;
  175. OHCI *base; /* equiv to io in uhci */
  176. HCCA *uchcca;
  177. int idgen; /* version # to distinguish new connections */
  178. QLock resetl; /* lock controller during USB reset */
  179. struct {
  180. Lock;
  181. TD* pool;
  182. TD* free;
  183. int alloced;
  184. } td;
  185. struct {
  186. QLock;
  187. ED* pool;
  188. ED* free;
  189. int alloced;
  190. } ed;
  191. /* TODO: what happened to ctlq, etc. from uhci? */
  192. QTree* tree; /* tree for t Endpt i/o */
  193. struct {
  194. QLock;
  195. Endpt* f;
  196. } activends;
  197. };
  198. enum {
  199. HcRevision = 0x00,
  200. HcControl = 0x01,
  201. HcfsMask = 3 << 6,
  202. HcfsReset = 0 << 6,
  203. HcfsResume = 1 << 6,
  204. HcfsOperational=2 << 6,
  205. HcfsSuspend = 3 << 6,
  206. Ble = 1 << 5,
  207. Cle = 1 << 4,
  208. Ie = 1 << 3,
  209. Ple = 1 << 2,
  210. Cbsr_MASK = 3,
  211. HcCommandStatus = 0x02,
  212. Ocr = 1 << 3,
  213. Blf = 1 << 2,
  214. Clf = 1 << 1,
  215. Hcr = 1 << 0,
  216. HcIntrStatus = 0x03,
  217. HcIntrEnable = 0x04,
  218. Mie = 1 << 31,
  219. Oc = 1 << 30,
  220. Rhsc = 1 << 6,
  221. Fno = 1 << 5,
  222. Ue = 1 << 4,
  223. Rd = 1 << 3,
  224. Sf = 1 << 2,
  225. Wdh = 1 << 1,
  226. So = 1 << 0,
  227. HcIntrDisable = 0x05,
  228. HcFmIntvl = 0x0d,
  229. HcFmIntvl_FSMaxpack_MASK = 0x7fff,
  230. HcFmIntvl_FSMaxpack_SHIFT = 16,
  231. HcFmRemaining = 0x0e,
  232. HcFmNumber = 0x0f,
  233. HcLSThreshold = 0x11,
  234. HcRhDescA = 0x12,
  235. HcRhDescA_POTPGT_MASK = 0xff << 24,
  236. HcRhDescA_POTPGT_SHIFT = 24,
  237. HcRhDescB = 0x13,
  238. HcRhStatus = 0x14,
  239. Lps = 1 << 0,
  240. Cgp = 1 << 0,
  241. Oci = 1 << 1,
  242. Drwe = 1 << 15,
  243. Srwe = 1 << 15,
  244. LpsC = 1 << 16,
  245. Sgp = 1 << 16,
  246. Ccic = 1 << 17,
  247. Crwe = 1 << 31,
  248. HcRhPortStatus1 = 0x15,
  249. Ccs = 1 << 0,
  250. Cpe = 1 << 0,
  251. Pes = 1 << 1,
  252. Spe = 1 << 1,
  253. Pss = 1 << 2,
  254. Poci = 1 << 3,
  255. Prs = 1 << 4,
  256. Spr = 1 << 4,
  257. Pps = 1 << 8,
  258. Spp= 1 << 8,
  259. Lsda = 1 << 9,
  260. Cpp = 1 << 9,
  261. Csc = 1 << 16,
  262. Pesc = 1 << 17,
  263. Pssc = 1 << 18,
  264. Ocic = 1 << 19,
  265. Prsc = 1 << 20,
  266. HcRhPortStatus2 = 0x16,
  267. L2NFRAME = 5,
  268. NFRAME = 1 << L2NFRAME,
  269. /* TODO: from UHCI; correct for OHCI? */
  270. FRAMESIZE = NFRAME*sizeof(ulong), /* fixed by hardware; aligned to same */
  271. };
  272. char *usbmode[] = {
  273. [Ctlmode]= "Ctl",
  274. [Bulkmode] = "Bulk",
  275. [Intrmode] = "Intr",
  276. [Isomode] = "Iso",
  277. };
  278. static char *ousbmode[] = {
  279. [OREAD] = "r",
  280. [OWRITE] = "w",
  281. [ORDWR] = "rw",
  282. };
  283. int ohciinterrupts[Nmodes];
  284. static Ctlr* ctlrhead;
  285. static Ctlr* ctlrtail;
  286. static char Estalled[] = "usb endpoint stalled";
  287. static char EnotWritten[] = "usb write unfinished";
  288. static char EnotRead[] = "usb read unfinished";
  289. static char Eunderrun[] = "usb endpoint underrun";
  290. static QLock usbhstate; /* protects name space state */
  291. static void eptactivate(Ctlr *ub, Endpt *ep);
  292. static void eptdeactivate(Ctlr *ub, Endpt *e);
  293. static long read (Usbhost *, Endpt*, void*, long, vlong);
  294. static void scanpci(void);
  295. static int schedendpt(Ctlr *ub, Endpt *ep, int direction);
  296. static void unschedendpt(Ctlr *ub, Endpt *ep, int);
  297. static long write(Usbhost *, Endpt*, void*, long, vlong, int);
  298. static long qtd(Ctlr*, Endpt*, int, Block*, uchar*, uchar*, int, ulong);
  299. static short
  300. refcnt(Block *b, int i)
  301. {
  302. short v;
  303. static Lock l;
  304. ilock(&l);
  305. v = (b->flag += i);
  306. iunlock(&l);
  307. if(v < 0)
  308. iprint("refcnt 0x%lux %d\n", b, v);
  309. return v;
  310. }
  311. static void
  312. freewb(Block *b)
  313. {
  314. if(b == nil || refcnt(b, -1) > 0)
  315. return;
  316. if(b->base > b->rp || b->rp > b->wp || b->wp > b->lim)
  317. iprint("freebw: %lux %lux %lux %lux\n",
  318. b->base, b->rp, b->wp, b->lim);
  319. /* poison the block in case someone is still holding onto it */
  320. b->next = (Block*)0xdeadcafe;
  321. b->rp = (uchar*)0xdeadcafe;
  322. b->wp = (uchar*)0xdeadcafe;
  323. b->lim = (uchar*)0xdeadcafe;
  324. b->base = (uchar*)0xdeadcafe;
  325. free(b);
  326. }
  327. Block *
  328. allocwb(long size)
  329. {
  330. Block *b;
  331. b = allocb(size);
  332. b->flag = 1;
  333. b->free = freewb;
  334. return b;
  335. }
  336. void
  337. printdata(void *pdata, int itemsize, int nitems)
  338. {
  339. int i;
  340. uchar *p1;
  341. ushort *p2;
  342. ulong *p4;
  343. if(!usbhdebug)
  344. return;
  345. p1 = pdata;
  346. p2 = pdata;
  347. p4 = pdata;
  348. i = 0;
  349. for(;;){
  350. switch(itemsize){
  351. default:
  352. assert(0);
  353. case 1:
  354. print("%2.2ux ", *p1++);
  355. break;
  356. case 2:
  357. print("%4.4ux ", *p2++);
  358. break;
  359. case 4:
  360. print("%8.8lux ", *p4++);
  361. break;
  362. }
  363. if(++i >= nitems || (i & ((0x40 >> itemsize) - 1)) == 0){
  364. print("\n");
  365. if(i >= nitems)
  366. break;
  367. }
  368. }
  369. }
  370. /*
  371. * i left these in so that we could use the same
  372. * driver on several other platforms (in principle).
  373. * the processor on which it was originally developed
  374. * had an IO MMU and thus another address space.
  375. * it's nothing to do with USB as such.
  376. */
  377. ulong
  378. va2hcva(void *va)
  379. {
  380. if(va == nil)
  381. return 0;
  382. return PADDR(va);
  383. }
  384. void *
  385. hcva2va(ulong hcva)
  386. {
  387. if(hcva == 0)
  388. return nil;
  389. return KADDR(hcva);
  390. }
  391. void *
  392. va2ucva(void *va)
  393. {
  394. return va;
  395. }
  396. void *
  397. hcva2ucva(ulong hcva)
  398. {
  399. if(hcva == 0)
  400. return nil;
  401. if(hcva & 0xf0000000){
  402. iprint("hcva2ucva: bad 0x%lux, called from 0x%lux\n",
  403. hcva, getcallerpc(&hcva));
  404. return nil;
  405. }
  406. return KADDR(hcva);
  407. }
  408. #define IOCACHED 0
  409. #define invalidatedcacheva(va)
  410. #define dcclean(p, n)
  411. static void
  412. EDinit(ED *ed, int mps, int f, int k, int s, int d, int en, int fa,
  413. TD *tail, TD *head, int c, int h, ED *next)
  414. {
  415. /* check nothing is running? */
  416. ed->ctrl = (mps & ED_MPS_MASK) << ED_MPS_SHIFT
  417. | (f & 1) << 15
  418. | (k & 1) << 14
  419. | (s & ED_S_MASK) << ED_S_SHIFT
  420. | (d & 3) << 11 /* 00 is obtained from TD (used here) */
  421. | (en & 0xf) << 7
  422. | (fa & 0x7f);
  423. ed->tail = va2hcva(tail) & ~0xF;
  424. ed->head = (va2hcva(head) & ~0xF)
  425. | (c & ED_C_MASK) << ED_C_SHIFT
  426. | (h & ED_H_MASK) << ED_H_SHIFT;
  427. ed->next = va2hcva(next) & ~0xF;
  428. }
  429. static void
  430. EDsetS(ED *ed, int s)
  431. {
  432. XIPRINT("EDsetS: %s speed\n", s == Lowspeed ? "low" : "high");
  433. if(s == Lowspeed)
  434. ed->ctrl |= 1 << ED_S_SHIFT;
  435. else
  436. ed->ctrl &= ~(1 << ED_S_SHIFT);
  437. }
  438. static void
  439. EDsetMPS(ED *ed, int mps)
  440. {
  441. ed->ctrl = (ed->ctrl & ~(ED_MPS_MASK << ED_MPS_SHIFT)) |
  442. (mps & ED_MPS_MASK) << ED_MPS_SHIFT;
  443. }
  444. static void
  445. EDsetC(ED *ed, int c)
  446. {
  447. ed->head = (ed->head & ~(ED_C_MASK << ED_C_SHIFT)) |
  448. (c & ED_C_MASK) << ED_C_SHIFT;
  449. }
  450. static void
  451. EDsetH(ED *ed, int h)
  452. {
  453. ed->head = (ed->head & ~(ED_H_MASK << ED_H_SHIFT)) |
  454. (h & ED_H_MASK) << ED_H_SHIFT;
  455. }
  456. static int
  457. EDgetH(ED *ed)
  458. {
  459. return (ed->head >> ED_H_SHIFT) & ED_H_MASK;
  460. }
  461. static int
  462. EDgetC(ED *ed)
  463. {
  464. return (ed->head >> ED_C_SHIFT) & ED_C_MASK;
  465. }
  466. static void
  467. EDsetnext(ED *ed, void *va)
  468. {
  469. ed->next = va2hcva(va) & ~0xF;
  470. }
  471. static ED *
  472. EDgetnext(ED *ed)
  473. {
  474. return hcva2ucva(ed->next & ~0xF);
  475. }
  476. static void
  477. EDsettail(ED *ed, void *va)
  478. {
  479. ed->tail = va2hcva(va) & ~0xF;
  480. }
  481. static TD *
  482. EDgettail(ED *ed)
  483. {
  484. return hcva2ucva(ed->tail & ~0xF);
  485. }
  486. static void
  487. EDsethead(ED *ed, void *va)
  488. {
  489. ed->head = (ed->head & 0xf) | (va2hcva(va) & ~0xF);
  490. }
  491. static TD *
  492. EDgethead(ED *ed)
  493. {
  494. return hcva2ucva(ed->head & ~0xF);
  495. }
  496. static ED *
  497. EDalloc(Ctlr *ub)
  498. {
  499. ED *t;
  500. qlock(&ub->ed);
  501. t = ub->ed.free;
  502. if(t == nil){
  503. qunlock(&ub->ed);
  504. return nil;
  505. }
  506. ub->ed.free = (ED *)t->next;
  507. ub->ed.alloced++;
  508. if (0)
  509. print("%d endpoints allocated\n", ub->ed.alloced);
  510. qunlock(&ub->ed);
  511. t->next = 0;
  512. return t;
  513. }
  514. void
  515. TDsetnexttd(TD *td, TD *va)
  516. {
  517. td->nexttd = va2hcva(va) & ~0xF;
  518. }
  519. TD *
  520. TDgetnexttd(TD *td)
  521. {
  522. return hcva2ucva(td->nexttd & ~0xF);
  523. }
  524. void
  525. OHCIsetControlHeadED(OHCI *ohci, ED *va)
  526. {
  527. ohci->ctlheaded = va2hcva(va) & ~0xF;
  528. }
  529. ED *
  530. OHCIgetControlHeadED(OHCI *ohci)
  531. {
  532. return hcva2ucva(ohci->ctlheaded);
  533. }
  534. void
  535. OHCIsetBulkHeadED(OHCI *ohci, ED *va)
  536. {
  537. ohci->bulkheaded = va2hcva(va) & ~0xF;
  538. }
  539. ED *
  540. OHCIgetBulkHeadED(OHCI *ohci)
  541. {
  542. return hcva2ucva(ohci->bulkheaded);
  543. }
  544. static TD *
  545. TDalloc(Ctlr *ub, Endpt *ep, int musthave) /* alloctd */
  546. {
  547. TD *t;
  548. Endptx *epx;
  549. for(;;){
  550. ilock(ub);
  551. t = ub->td.free;
  552. if(t)
  553. break;
  554. iunlock(ub);
  555. if(up == nil){
  556. if(musthave)
  557. panic("TDalloc: out of descs");
  558. return nil;
  559. }
  560. tsleep(&up->sleep, return0, 0, 100);
  561. }
  562. ub->td.free = t->next;
  563. epx = ep->private;
  564. epx->ntd++;
  565. ub->td.alloced++;
  566. iunlock(ub);
  567. memset(t, 0, sizeof(TD));
  568. t->ep = ep;
  569. return t;
  570. }
  571. /* call under ilock */
  572. static void
  573. TDfree(Ctlr *ub, TD *t) /* freetd */
  574. {
  575. Endptx *epx;
  576. if(t == 0)
  577. return;
  578. if(t->ep){
  579. epx = t->ep->private;
  580. epx->ntd--;
  581. } else
  582. t->ep = nil; /* redundant? */
  583. t->bp = nil;
  584. t->next = ub->td.free;
  585. ub->td.free = t;
  586. ub->td.alloced--;
  587. }
  588. static void
  589. EDfree(Ctlr *ub, ED *t)
  590. {
  591. TD *td, *next;
  592. if(t == 0)
  593. return;
  594. qlock(&ub->ed);
  595. t->next = (ulong)ub->ed.free;
  596. ub->ed.free = t;
  597. ub->ed.alloced--;
  598. if (0)
  599. print("%d endpoints allocated\n", ub->ed.alloced);
  600. ilock(ub);
  601. for(td = EDgethead(t); td; td = next){
  602. next = TDgetnexttd(td);
  603. TDfree(ub, td);
  604. }
  605. iunlock(ub);
  606. EDsethead(t, 0);
  607. EDsettail(t, 0);
  608. qunlock(&ub->ed);
  609. }
  610. static void
  611. waitSOF(Ctlr *ub)
  612. {
  613. /*
  614. * wait for SOF - interlock with interrupt handler so
  615. * done queue processed first.
  616. */
  617. int frame = ub->uchcca->framenumber & 0x3f;
  618. do {
  619. delay(2);
  620. } while(frame == (ub->uchcca->framenumber & 0x3f));
  621. }
  622. static void
  623. dumptd(TD *td, char *s)
  624. {
  625. int i;
  626. Endpt *ep;
  627. ep = td->ep;
  628. print("\t%s: 0x%.8lux ctrl 0x%.8lux cbp 0x%.8lux "
  629. "nexttd 0x%.8lux be 0x%.8lux, flags %lux\n",
  630. s, td, td->ctrl, td->cbp, td->nexttd, td->be, td->flags);
  631. if(ep->epmode != Isomode){
  632. print("\t\tbytes: %ld\n", td->be + 1 - td->cbp);
  633. return;
  634. }
  635. print("\t\t0x%ux 0x%ux 0x%ux 0x%ux 0x%ux 0x%ux 0x%ux 0x%ux\n",
  636. td->offsets[0], td->offsets[1], td->offsets[2], td->offsets[3],
  637. td->offsets[4], td->offsets[5], td->offsets[6], td->offsets[7]);
  638. print("\t\tbytes:");
  639. for(i = 0; i < td->ctrl >> 24 & 0x7; i++)
  640. print(" %d", (td->offsets[i+1]-td->offsets[i])&0xfff);
  641. print(" %ld\n", (td->be + 1 - td->offsets[i]) & 0xfff);
  642. }
  643. static void
  644. dumped(ED *ed)
  645. {
  646. TD *tailp, *td;
  647. tailp = EDgettail(ed);
  648. td = EDgethead(ed);
  649. print("dumpED 0x%lux: ctrl 0x%lux tail 0x%lux head 0x%lux next 0x%lux\n",
  650. ed, ed->ctrl, ed->tail, ed->head, ed->next);
  651. if(tailp == td)
  652. return;
  653. do {
  654. dumptd(td, "td");
  655. } while((td = TDgetnexttd(td)) != tailp);
  656. }
  657. static void
  658. dumpstatus(Ctlr *ub)
  659. {
  660. ED *ed;
  661. print("dumpstatus 0x%lux, frame 0x%ux:\n", ub, ub->uchcca->framenumber);
  662. print("control 0x%lux, cmdstat 0x%lux, intrsts 0x%lux\n",
  663. ub->base->control, ub->base->cmdsts, ub->base->intrsts);
  664. print("Control:\n");
  665. for(ed = OHCIgetControlHeadED(ub->base); ed; ed = EDgetnext(ed))
  666. dumped(ed);
  667. print("Bulk:\n");
  668. for(ed = OHCIgetBulkHeadED(ub->base); ed; ed = EDgetnext(ed))
  669. dumped(ed);
  670. print("Iso:\n");
  671. for(ed = ub->tree->root[0]; ed; ed = EDgetnext(ed))
  672. dumped(ed);
  673. print("frame 0x%ux:\n", ub->uchcca->framenumber);
  674. }
  675. /*
  676. * halt the ED and free input or output transfer descs
  677. * called when the relevant lock in the enclosing Endpt is held
  678. */
  679. static void
  680. EDcancel(Ctlr *ub, ED *ed, int dirin)
  681. {
  682. int tddir, iso, n;
  683. TD *tailp, *headp, *td, *prev, *next;
  684. Endpt *ep;
  685. if(ed == nil)
  686. return;
  687. /* halt ED if not already halted */
  688. if(EDgetH(ed) != 1){
  689. EDsetH(ed, 1);
  690. waitSOF(ub);
  691. }
  692. SET(tddir);
  693. if((iso = ed->ctrl & ED_F_BIT) != 0)
  694. switch((ed->ctrl >> 11) & 0x3){
  695. default:
  696. panic("ED iso direction unset");
  697. case Otokin: tddir = Dirin; break;
  698. case Otokout: tddir = Dirout; break;
  699. }
  700. /* can now clean up TD list of ED */
  701. tailp = EDgettail(ed);
  702. headp = EDgethead(ed);
  703. n = 0;
  704. prev = nil;
  705. td = headp;
  706. while(td != tailp){
  707. ep = td->ep;
  708. if(iso == 0)
  709. switch((td->ctrl >> TD_DP_SHIFT) & TD_DP_MASK){
  710. default:
  711. panic("TD direction unset");
  712. case Otoksetup: tddir = Dirout; break;
  713. case Otokin: tddir = Dirin; break;
  714. case Otokout: tddir = Dirout; break;
  715. }
  716. else if(usbhdebug || ep->debug)
  717. print("EDcancel: buffered: %d, bytes %ld\n",
  718. ep->buffered, td->bytes);
  719. next = TDgetnexttd(td);
  720. if(dirin == 2 || dirin == tddir){
  721. XEPRINT("%d/%d: EDcancel %d\n", ep->dev->x, ep->x, tddir);
  722. /* Remove this sucker */
  723. ep->buffered -= td->bytes;
  724. if(ep->buffered < 0)
  725. ep->buffered = 0;
  726. ilock(ub);
  727. ep->dir[tddir].queued--;
  728. if(tddir == Dirout){
  729. freeb(td->bp);
  730. td->bp = nil;
  731. }
  732. if(prev)
  733. TDsetnexttd(prev, next);
  734. else
  735. EDsethead(ed, next);
  736. TDfree(ub, td);
  737. n++;
  738. iunlock(ub);
  739. }else{
  740. XEPRINT("%d/%d: EDcancel skip %d\n", ep->dev->x, ep->x,
  741. tddir);
  742. prev = td;
  743. }
  744. td = next;
  745. }
  746. XPRINT("EDcancel: %d\n", n);
  747. }
  748. static void
  749. eptactivate(Ctlr *ub, Endpt *ep)
  750. {
  751. Endptx *epx;
  752. qlock(&ub->activends);
  753. if(ep->active == 0){
  754. epx = ep->private;
  755. XEPRINT("%d/%d: activate\n", ep->dev->x, ep->x);
  756. ep->active = 1;
  757. /*
  758. * set the right speed
  759. */
  760. EDsetS(epx->ed, ep->dev->speed);
  761. switch(ep->epmode){
  762. case Ctlmode:
  763. /*
  764. * chain the two descs together, and
  765. * bind to beginning of control queue
  766. */
  767. EDsetnext(epx->ed, OHCIgetControlHeadED(ub->base));
  768. OHCIsetControlHeadED(ub->base, epx->ed);
  769. /*
  770. * prompt controller to absorb new queue on next pass
  771. */
  772. ub->base->cmdsts |= Clf;
  773. XEPRINT("%d/%d: activated in control queue\n",
  774. ep->dev->x, ep->x);
  775. break;
  776. case Bulkmode:
  777. EDsetnext(epx->ed, OHCIgetBulkHeadED(ub->base));
  778. OHCIsetBulkHeadED(ub->base, epx->ed);
  779. ub->base->cmdsts |= Blf;
  780. XEPRINT("%d/%d: activated %s in bulk input queue\n",
  781. ep->dev->x, ep->x, ousbmode[ep->mode]);
  782. break;
  783. case Isomode:
  784. if(ep->mode != OWRITE)
  785. schedendpt(ub, ep, Dirin);
  786. if(ep->mode != OREAD)
  787. schedendpt(ub, ep, Dirout);
  788. ep->buffered = 0;
  789. ep->partial = 0;
  790. break;
  791. case Intrmode:
  792. if(ep->mode != OWRITE)
  793. schedendpt(ub, ep, Dirin);
  794. if(ep->mode != OREAD)
  795. schedendpt(ub, ep, Dirout);
  796. break;
  797. case Nomode:
  798. break;
  799. default:
  800. panic("eptactivate: wierd epmode %d\n", ep->epmode);
  801. }
  802. ep->dir[Dirin].xdone = ep->dir[Dirin].xstarted = 0;
  803. ep->dir[Dirout].xdone = ep->dir[Dirout].xstarted = 0;
  804. ep->activef = ub->activends.f;
  805. ub->activends.f = ep;
  806. }
  807. qunlock(&ub->activends);
  808. }
  809. static void
  810. EDpullfrombulk(Ctlr *ub, ED *ed)
  811. {
  812. ED *this, *prev, *next;
  813. this = OHCIgetBulkHeadED(ub->base);
  814. ub->base->bulkcurred = 0;
  815. prev = nil;
  816. while(this != nil && this != ed){
  817. prev = this;
  818. this = EDgetnext(this);
  819. }
  820. if(this == nil){
  821. print("EDpullfrombulk: not found\n");
  822. return;
  823. }
  824. next = EDgetnext(this);
  825. if(prev == nil)
  826. OHCIsetBulkHeadED(ub->base, next);
  827. else
  828. EDsetnext(prev, next);
  829. EDsetnext(ed, nil); /* wipe out next field */
  830. }
  831. static void
  832. EDpullfromctl(Ctlr *ub, ED *ed)
  833. {
  834. ED *this, *prev, *next;
  835. this = OHCIgetControlHeadED(ub->base);
  836. ub->base->ctlcurred = 0;
  837. prev = nil;
  838. while(this != nil && this != ed){
  839. prev = this;
  840. this = EDgetnext(this);
  841. }
  842. if(this == nil)
  843. panic("EDpullfromctl: not found\n");
  844. next = EDgetnext(this);
  845. if(prev == nil)
  846. OHCIsetControlHeadED(ub->base, next);
  847. else
  848. EDsetnext(prev, next);
  849. EDsetnext(ed, nil); /* wipe out next field */
  850. }
  851. static void
  852. eptdeactivate(Ctlr *ub, Endpt *ep)
  853. {
  854. ulong ctrl;
  855. Endpt **l;
  856. Endptx *epx;
  857. /* could be O(1) but not worth it yet */
  858. qlock(&ub->activends);
  859. if(ep->active){
  860. epx = ep->private;
  861. XEPRINT("ohci: eptdeactivate %d/%d\n", ep->dev->x, ep->x);
  862. ep->active = 0;
  863. for(l = &ub->activends.f; *l != ep; l = &(*l)->activef)
  864. if(*l == nil){
  865. qunlock(&ub->activends);
  866. panic("usb eptdeactivate");
  867. }
  868. *l = ep->activef;
  869. /* pull it from the appropriate queue */
  870. ctrl = ub->base->control;
  871. switch(ep->epmode){
  872. case Ctlmode:
  873. if(ctrl & Cle){
  874. ub->base->control &= ~Cle;
  875. waitSOF(ub);
  876. }
  877. EDpullfromctl(ub, epx->ed);
  878. if(ctrl & Cle){
  879. ub->base->control |= Cle;
  880. /*
  881. * don't fill it if there is nothing in it -
  882. * shouldn't be necessary according to the
  883. * spec., but practice is different
  884. */
  885. if(OHCIgetControlHeadED(ub->base))
  886. ub->base->cmdsts |= Clf;
  887. }
  888. break;
  889. case Bulkmode:
  890. if(ctrl & Ble){
  891. ub->base->control &= ~Ble;
  892. waitSOF(ub);
  893. }
  894. EDpullfrombulk(ub, epx->ed);
  895. if(ctrl & Ble){
  896. ub->base->control |= Ble;
  897. /*
  898. * don't fill it if there is nothing in it -
  899. * shouldn't be necessary according to the
  900. * spec., but practice is different
  901. */
  902. if(OHCIgetBulkHeadED(ub->base))
  903. ub->base->cmdsts |= Blf;
  904. }
  905. break;
  906. case Intrmode:
  907. case Isomode:
  908. if(ep->mode != OWRITE)
  909. unschedendpt(ub, ep, Dirin);
  910. if(ep->mode != OREAD)
  911. unschedendpt(ub, ep, Dirout);
  912. waitSOF(ub);
  913. break;
  914. case Nomode:
  915. break;
  916. default:
  917. panic("eptdeactivate: wierd in.epmode %d\n",
  918. ep->epmode);
  919. }
  920. }
  921. qunlock(&ub->activends);
  922. }
  923. static void
  924. kickappropriatequeue(Ctlr *ub, Endpt *ep, int)
  925. {
  926. switch(ep->epmode){
  927. case Nomode:
  928. break;
  929. case Ctlmode:
  930. ub->base->cmdsts |= Clf;
  931. break;
  932. case Bulkmode:
  933. ub->base->cmdsts |= Blf;
  934. break;
  935. case Intrmode:
  936. case Isomode:
  937. /* no kicking required */
  938. break;
  939. default:
  940. panic("wierd epmode %d\n", ep->epmode);
  941. }
  942. }
  943. static void
  944. eptenable(Ctlr *ub, Endpt *ep, int dirin)
  945. {
  946. ED *ed;
  947. Endptx *epx;
  948. epx = ep->private;
  949. ed = epx->ed;
  950. if(EDgetH(ed) == 1){
  951. EDsetH(ed, 0);
  952. kickappropriatequeue(ub, ep, dirin);
  953. if(ep->epmode == Isomode || ep->epmode == Intrmode)
  954. waitSOF(ub);
  955. }
  956. }
  957. /*
  958. * return smallest power of 2 >= n
  959. */
  960. static int
  961. flog2(int n)
  962. {
  963. int i;
  964. for(i = 0; (1 << i) < n; i++)
  965. ;
  966. return i;
  967. }
  968. /*
  969. * return smallest power of 2 <= n
  970. */
  971. static int
  972. flog2lower(int n)
  973. {
  974. int i;
  975. for(i = 0; (1 << (i + 1)) <= n; i++)
  976. ;
  977. return i;
  978. }
  979. static int
  980. pickschedq(QTree *qt, int pollms, ulong bw, ulong limit)
  981. {
  982. int i, j, d, upperb, q;
  983. ulong best, worst, total;
  984. d = flog2lower(pollms);
  985. if(d > qt->depth)
  986. d = qt->depth;
  987. q = -1;
  988. worst = 0;
  989. best = ~0;
  990. upperb = (1 << (d+1)) - 1;
  991. for(i = (1 << d) - 1; i < upperb; i++){
  992. total = qt->bw[0];
  993. for(j = i; j > 0; j = (j - 1) / 2)
  994. total += qt->bw[j];
  995. if(total < best){
  996. best = total;
  997. q = i;
  998. }
  999. if(total > worst)
  1000. worst = total;
  1001. }
  1002. if(worst + bw >= limit)
  1003. return -1;
  1004. return q;
  1005. }
  1006. static int
  1007. schedendpt(Ctlr *ub, Endpt *ep, int dirin)
  1008. {
  1009. int q;
  1010. ED *ed;
  1011. Endptx *epx;
  1012. epx = ep->private;
  1013. qlock(ub->tree);
  1014. /* TO DO: bus bandwidth limit */
  1015. q = pickschedq(ub->tree, ep->pollms, ep->bw, ~0);
  1016. XEPRINT("schedendpt, dir %d Q index %d, ms %d, bw %ld\n",
  1017. dirin, q, ep->pollms, ep->bw);
  1018. if(q < 0){
  1019. qunlock(ub->tree);
  1020. return -1;
  1021. }
  1022. ub->tree->bw[q] += ep->bw;
  1023. ed = ub->tree->root[q];
  1024. ep->sched = q;
  1025. EDsetnext(epx->ed, EDgetnext(ed));
  1026. EDsetnext(ed, epx->ed);
  1027. XEPRINT("%d/%d: sched on q %d pollms %d\n",
  1028. ep->dev->x, ep->x, q, ep->pollms);
  1029. qunlock(ub->tree);
  1030. return 0;
  1031. }
  1032. static void
  1033. unschedendpt(Ctlr *ub, Endpt *ep, int dirin)
  1034. {
  1035. int q;
  1036. ED *prev, *this, *next;
  1037. Endptx *epx;
  1038. epx = ep->private;
  1039. if((q = ep->sched) < 0)
  1040. return;
  1041. qlock(ub->tree);
  1042. ub->tree->bw[q] -= ep->bw;
  1043. prev = ub->tree->root[q];
  1044. this = EDgetnext(prev);
  1045. while(this != nil && this != epx->ed){
  1046. prev = this;
  1047. this = EDgetnext(this);
  1048. }
  1049. if(this == nil)
  1050. print("unschedendpt %d %d: not found\n", dirin, q);
  1051. else{
  1052. next = EDgetnext(this);
  1053. EDsetnext(prev, next);
  1054. }
  1055. qunlock(ub->tree);
  1056. }
  1057. /* at entry, *e is partly populated */
  1058. static void
  1059. epalloc(Usbhost *uh, Endpt *ep)
  1060. {
  1061. int id;
  1062. Endptx *epx;
  1063. Ctlr *ctlr;
  1064. Udev *d;
  1065. TD *dtd;
  1066. XEPRINT("ohci: epalloc from devusb\n");
  1067. ctlr = uh->ctlr;
  1068. id = ep->id;
  1069. d = ep->dev;
  1070. epx = malloc(sizeof(Endptx));
  1071. memset(epx, 0, sizeof(Endptx));
  1072. ep->private = epx;
  1073. dtd = nil;
  1074. if(waserror()){
  1075. XEPRINT("ohci: epalloc error\n");
  1076. EDfree(ctlr, epx->ed);
  1077. epx->ed = nil;
  1078. TDfree(ctlr, dtd);
  1079. nexterror();
  1080. }
  1081. if(epx->ed)
  1082. error("usb: already allocated");
  1083. if((epx->ed = EDalloc(ctlr)) == nil)
  1084. error(Enomem);
  1085. ep->bw = 1; /* all looks the same currently */
  1086. if((dtd = TDalloc(ctlr, ep, 0)) == nil)
  1087. error(Enomem);
  1088. EDinit(epx->ed, ep->maxpkt, 0, 0, 0, 0, id, d->id, dtd, dtd, 0, 0, 0);
  1089. XEPRINT("ohci: epalloc done\n");
  1090. poperror();
  1091. }
  1092. static void
  1093. epfree(Usbhost *uh, Endpt *ep)
  1094. {
  1095. Endptx *epx;
  1096. Ctlr *ctlr;
  1097. epx = ep->private;
  1098. ctlr = uh->ctlr;
  1099. XEPRINT("ohci: epfree %d/%d from devusb\n", ep->dev->x, ep->x);
  1100. if(ep->active)
  1101. panic("epfree: active");
  1102. EDfree(ctlr, epx->ed);
  1103. epx->ed = nil;
  1104. free(epx);
  1105. ep->private = nil;
  1106. }
  1107. static void
  1108. epopen(Usbhost *uh, Endpt *ep)
  1109. {
  1110. Ctlr *ctlr;
  1111. XEPRINT("ohci: epopen %d/%d from devusb\n", ep->dev->x, ep->x);
  1112. ctlr = uh->ctlr;
  1113. if((ep->epmode == Isomode || ep->epmode == Intrmode) && ep->active)
  1114. error("already open");
  1115. eptactivate(ctlr, ep);
  1116. }
  1117. static int
  1118. setfrnum(Ctlr *ub, Endpt *ep)
  1119. {
  1120. short frnum, d;
  1121. static int adj;
  1122. /* adjust frnum as necessary... */
  1123. frnum = ub->base->fmnumber + (ep->buffered*1000)/ep->bw;
  1124. d = frnum - ep->frnum;
  1125. if(d < -100 || d > 100){
  1126. /* We'd play in the past */
  1127. if(0 && d > 1000)
  1128. /* We're more than a second off: */
  1129. print("d %d, done %d, started %d, buffered %d\n", d,
  1130. ep->dir[Dirout].xdone, ep->dir[Dirout].xstarted,
  1131. ep->buffered);
  1132. if(ep->dir[Dirout].xdone == ep->dir[Dirout].xstarted)
  1133. ep->buffered = adj = 0;
  1134. if(0 && (adj++ & 0xff) == 0)
  1135. print("adj %d %d\n", d, ep->buffered);
  1136. ep->frnum = ub->base->fmnumber + 10 + (ep->buffered*1000)/ep->bw;
  1137. ep->partial = 0;
  1138. return 1;
  1139. }
  1140. return 0;
  1141. }
  1142. static int
  1143. ceptdone(void *arg)
  1144. {
  1145. Endpt *ep;
  1146. ep = arg;
  1147. return ep->dir[Dirout].xdone - ep->dir[Dirout].xstarted >= 0
  1148. || ep->dir[Dirout].err;
  1149. }
  1150. static void
  1151. epclose(Usbhost *uh, Endpt *ep)
  1152. {
  1153. Ctlr *ctlr;
  1154. int xdone, part;
  1155. Endptx *epx;
  1156. XEPRINT("ohci: epclose %d/%d from devusb, %d buffered\n",
  1157. ep->dev->x, ep->x, ep->buffered);
  1158. ctlr = uh->ctlr;
  1159. epx = ep->private;
  1160. if(ep->epmode == Isomode && ep->active){
  1161. qlock(&ep->wlock);
  1162. if(ep->partial && setfrnum(ctlr, ep) == 0){
  1163. part = ep->partial;
  1164. memset(ep->bpartial->wp, 0, ep->maxpkt - ep->partial);
  1165. ep->bpartial->wp = ep->bpartial->rp + ep->maxpkt;
  1166. qtd(uh->ctlr, ep, Dirout, nil, ep->bpartial->rp,
  1167. ep->bpartial->wp, Otokout, TD_FLAGS_LAST);
  1168. XEPRINT("epclose: wrote partial block %d\n", part);
  1169. ep->partial = 0;
  1170. }
  1171. qunlock(&ep->wlock);
  1172. XEPRINT("epclose: wait for outstanding TDs, xdone %d"
  1173. ", xstarted %d, buffered %d, queued %d\n",
  1174. ep->dir[Dirout].xdone, ep->dir[Dirout].xstarted,
  1175. ep->buffered, ep->dir[Dirout].queued);
  1176. while(ep->dir[Dirout].err == nil
  1177. && (xdone = ep->dir[Dirout].xdone) - ep->dir[Dirout].xstarted < 0){
  1178. tsleep(&ep->dir[Dirout].rend, ceptdone, ep, 500);
  1179. if(xdone == ep->dir[Dirout].xdone){
  1180. print("no progress\n");
  1181. break;
  1182. }
  1183. }
  1184. if(ep->dir[Dirout].err)
  1185. XEPRINT("error: %s\n", ep->dir[Dirout].err);
  1186. if(ep->buffered)
  1187. XEPRINT("epclose: done waiting, xdone %d, xstarted %d, "
  1188. "buffered %d, queued %d\n",
  1189. ep->dir[Dirout].xdone, ep->dir[Dirout].xstarted,
  1190. ep->buffered, ep->dir[Dirout].queued);
  1191. }
  1192. lock(epx);
  1193. EDcancel(ctlr, epx->ed, 2);
  1194. unlock(epx);
  1195. if(ep->epmode == Isomode && ep->buffered)
  1196. XEPRINT("epclose: after cancelling, xdone %d, xstarted %d"
  1197. ", buffered %d, queued %d\n",
  1198. ep->dir[Dirout].xdone, ep->dir[Dirout].xstarted,
  1199. ep->buffered, ep->dir[Dirout].queued);
  1200. eptdeactivate(ctlr, ep);
  1201. }
  1202. static void
  1203. epmaxpkt(Usbhost *, Endpt *ep)
  1204. {
  1205. Endptx *epx;
  1206. epx = ep->private;
  1207. XEPRINT("ohci: epmaxpkt %d/%d: %d\n",
  1208. ep->dev->x, ep->x, ep->maxpkt);
  1209. EDsetMPS(epx->ed, ep->maxpkt);
  1210. }
  1211. static void
  1212. epmode(Usbhost *uh, Endpt *ep)
  1213. {
  1214. int tok, reactivate;
  1215. Ctlr *ctlr;
  1216. Endptx *epx;
  1217. epx = ep->private;
  1218. ctlr = uh->ctlr;
  1219. XEPRINT("ohci: epmode %d/%d %s → %s\n",
  1220. ep->dev->x, ep->x, usbmode[ep->epmode], usbmode[ep->epnewmode]);
  1221. reactivate = 0;
  1222. if(ep->epnewmode != ep->epmode)
  1223. if(reactivate = ep->active){
  1224. XEPRINT("ohci: epmode %d/%d: already open\n",
  1225. ep->dev->x, ep->x);
  1226. eptdeactivate(ctlr, ep);
  1227. }
  1228. EDsetS(epx->ed, ep->dev->speed);
  1229. switch(ep->epnewmode){
  1230. default:
  1231. panic("devusb is sick");
  1232. case Intrmode:
  1233. // ep->debug++;
  1234. ep->bw = ep->maxpkt*1000/ep->pollms; /* bytes/sec */
  1235. XEPRINT("ohci: epmode %d/%d %s, intr: maxpkt %d, pollms %d, bw %ld\n",
  1236. ep->dev->x, ep->x, ousbmode[ep->mode],
  1237. ep->maxpkt, ep->pollms, ep->bw);
  1238. break;
  1239. case Isomode:
  1240. // ep->debug++;
  1241. ep->rem = 999;
  1242. switch(ep->mode){
  1243. default:
  1244. panic("ep mode");
  1245. case ORDWR:
  1246. error("iso unidirectional only");
  1247. case OREAD:
  1248. tok = Otokin;
  1249. error("iso read not implemented");
  1250. break;
  1251. case OWRITE:
  1252. tok = Otokout;
  1253. break;
  1254. }
  1255. XEPRINT("ohci: epmode %d/%d %s, iso: maxpkt %d, pollms %d, hz %d, samp %d\n",
  1256. ep->dev->x, ep->x, ousbmode[ep->mode],
  1257. ep->maxpkt, ep->pollms, ep->hz, ep->samplesz);
  1258. ep->bw = ep->hz * ep->samplesz; /* bytes/sec */
  1259. /* Use Iso TDs: */
  1260. epx->ed->ctrl |= ED_F_BIT;
  1261. /* Set direction in ED, no room in an Iso TD for this */
  1262. epx->ed->ctrl &= ~(ED_D_MASK << ED_D_SHIFT);
  1263. epx->ed->ctrl |= tok << ED_D_SHIFT;
  1264. break;
  1265. case Bulkmode:
  1266. // ep->debug++;
  1267. /*
  1268. * Each Bulk device gets a queue head hanging off the
  1269. * bulk queue head
  1270. */
  1271. break;
  1272. case Ctlmode:
  1273. break;
  1274. }
  1275. ep->epmode = ep->epnewmode;
  1276. epmaxpkt(uh, ep);
  1277. if(reactivate){
  1278. XEPRINT("ohci: epmode %d/%d: reactivate\n", ep->dev->x, ep->x);
  1279. eptactivate(ctlr, ep);
  1280. }
  1281. }
  1282. static long
  1283. qtd(Ctlr *ub, Endpt *ep, int dirin , Block *bp, uchar *base, uchar *limit,
  1284. int pid, ulong flags)
  1285. {
  1286. int fc, mps;
  1287. ulong x;
  1288. uchar *p;
  1289. ED *ed;
  1290. Endptx *epx;
  1291. TD *dummytd, *td;
  1292. epx = ep->private;
  1293. ed = epx->ed;
  1294. td = hcva2ucva(ed->tail);
  1295. td->flags = flags;
  1296. if(ep->epmode == Isomode){
  1297. x = va2hcva(base);
  1298. td->cbp = x & ~0xfff;
  1299. x &= 0xfff;
  1300. p = base;
  1301. setfrnum(ub, ep);
  1302. td->ctrl = ep->frnum & 0xffff;
  1303. fc = 0;
  1304. for(;;){
  1305. /* Calculate number of samples in next packet */
  1306. mps = (ep->hz + ep->rem)/1000;
  1307. /* rem is the partial sample left over */
  1308. ep->rem += ep->hz - 1000*mps;
  1309. mps *= ep->samplesz;
  1310. if(mps > ep->maxpkt)
  1311. panic("Packet size");
  1312. if(ep->partial == 0 && mps > limit - p){
  1313. /* Save this data for later ... */
  1314. ep->partial = limit - p;
  1315. if(fc-- == 0)
  1316. return p - base; /* No TD */
  1317. /* We do have a TD, send this one off normally */
  1318. td->flags |= TD_FLAGS_LAST;
  1319. break;
  1320. }else if(mps >= limit - p){
  1321. td->flags |= TD_FLAGS_LAST;
  1322. mps = limit - p;
  1323. ep->partial = 0;
  1324. }
  1325. td->offsets[fc] = 0xe000 | x;
  1326. x += mps;
  1327. p += mps;
  1328. ep->frnum++;
  1329. if(fc == 7 || limit - p == 0)
  1330. break;
  1331. fc++;
  1332. }
  1333. td->ctrl |= fc << 24;
  1334. }else{
  1335. td->cbp = va2hcva(base);
  1336. td->ctrl = (pid & TD_DP_MASK) << TD_DP_SHIFT;
  1337. p = base;
  1338. mps = 0x2000 - ((ulong)p & 0xfff);
  1339. if(mps > ep->maxpkt)
  1340. mps = ep->maxpkt;
  1341. if(mps >= limit - p){
  1342. mps = limit - base;
  1343. td->flags |= TD_FLAGS_LAST;
  1344. }
  1345. p += mps;
  1346. }
  1347. td->be = va2hcva(p == nil? nil: p - 1);
  1348. td->ep = ep;
  1349. td->bytes = p - base;
  1350. ep->buffered += td->bytes;
  1351. td->bp = bp;
  1352. if(td->flags & TD_FLAGS_LAST)
  1353. ep->dir[dirin].xstarted++;
  1354. if(dirin == Dirout && bp)
  1355. refcnt(bp, 1);
  1356. dummytd = TDalloc(ub, ep, 1);
  1357. TDsetnexttd(td, dummytd);
  1358. ep->dir[dirin].queued++;
  1359. EDsettail(ed, dummytd);
  1360. if(usbhdebug || ep->debug)
  1361. dumptd(td, "qtd: before");
  1362. kickappropriatequeue(ub, ep, dirin);
  1363. return p - base;
  1364. }
  1365. Block*
  1366. allocrcvb(long size)
  1367. {
  1368. Block *b;
  1369. int asize;
  1370. asize = ROUND(size, dcls) + dcls - 1;
  1371. /*
  1372. * allocate enough to align rp to dcls, and have an integral number
  1373. * of cache lines in the buffer
  1374. */
  1375. while(waserror())
  1376. tsleep(&up->sleep, return0, 0, 100);
  1377. b = allocb(asize);
  1378. poperror();
  1379. /*
  1380. * align the rp and wp
  1381. */
  1382. b->rp = b->wp = (uchar *)ROUND((ulong)b->rp, dcls);
  1383. /*
  1384. * invalidate the cache lines which enclose the buffer
  1385. */
  1386. if(IOCACHED){
  1387. uchar *p;
  1388. p = b->rp;
  1389. while(size > 0){
  1390. invalidatedcacheva((ulong)p);
  1391. p += dcls;
  1392. size -= dcls;
  1393. }
  1394. }
  1395. return b;
  1396. }
  1397. /*
  1398. * build the periodic scheduling tree:
  1399. * framesize must be a multiple of the tree size
  1400. */
  1401. static QTree *
  1402. mkqhtree(Ctlr *ub)
  1403. {
  1404. int i, n, d, o, leaf0, depth;
  1405. ED **tree;
  1406. QTree *qt;
  1407. depth = flog2(32);
  1408. n = (1 << (depth+1)) - 1;
  1409. qt = mallocz(sizeof(*qt), 1);
  1410. if(qt == nil)
  1411. return nil;
  1412. qt->nel = n;
  1413. qt->depth = depth;
  1414. qt->bw = mallocz(n * sizeof(qt->bw), 1);
  1415. if(qt->bw == nil){
  1416. free(qt);
  1417. return nil;
  1418. }
  1419. tree = mallocz(n * sizeof(ED *), 1);
  1420. if(tree == nil){
  1421. free(qt->bw);
  1422. free(qt);
  1423. return nil;
  1424. }
  1425. for(i = 0; i < n; i++)
  1426. if((tree[i] = EDalloc(ub)) == nil)
  1427. break;
  1428. if(i < n){
  1429. int j;
  1430. for(j = 0; j < i; j++)
  1431. EDfree(ub, tree[j]);
  1432. free(tree);
  1433. free(qt->bw);
  1434. free(qt);
  1435. return nil;
  1436. }
  1437. qt->root = tree;
  1438. EDinit(qt->root[0], 8, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1439. for(i = 1; i < n; i++)
  1440. EDinit(tree[i], 8, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, tree[(i-1)/2]);
  1441. /* distribute leaves evenly round the frame list */
  1442. leaf0 = n / 2;
  1443. for(i = 0; i < 32; i++){
  1444. o = 0;
  1445. for(d = 0; d < depth; d++){
  1446. o <<= 1;
  1447. if(i & (1 << d))
  1448. o |= 1;
  1449. }
  1450. if(leaf0 + o >= n){
  1451. print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
  1452. break;
  1453. }
  1454. ub->uchcca->intrtable[i] = va2hcva(tree[leaf0 + o]);
  1455. }
  1456. return qt;
  1457. }
  1458. static void
  1459. portreset(Usbhost *uh, int port)
  1460. {
  1461. Ctlr *ctlr;
  1462. XIPRINT("ohci: portreset(port %d) from devusb\n", port);
  1463. ctlr = uh->ctlr;
  1464. /* should check that device not being configured on other port? */
  1465. qlock(&ctlr->resetl);
  1466. if(waserror()){
  1467. qunlock(&ctlr->resetl);
  1468. nexterror();
  1469. }
  1470. ilock(ctlr);
  1471. ctlr->base->rhportsts[port - 1] = Spp | Spr;
  1472. while((ctlr->base->rhportsts[port - 1] & Prsc) == 0){
  1473. iunlock(ctlr);
  1474. XIPRINT("ohci: portreset, wait for reset complete\n");
  1475. ilock(ctlr);
  1476. }
  1477. ctlr->base->rhportsts[port - 1] = Prsc;
  1478. iunlock(ctlr);
  1479. poperror();
  1480. qunlock(&ctlr->resetl);
  1481. }
  1482. static void
  1483. portenable(Usbhost *uh, int port, int on)
  1484. {
  1485. Ctlr *ctlr;
  1486. XIPRINT("ohci: portenable(port %d, on %d) from devusb\n", port, on);
  1487. ctlr = uh->ctlr;
  1488. /* should check that device not being configured on other port? */
  1489. qlock(&ctlr->resetl);
  1490. if(waserror()){
  1491. qunlock(&ctlr->resetl);
  1492. nexterror();
  1493. }
  1494. ilock(ctlr);
  1495. if(on)
  1496. ctlr->base->rhportsts[port - 1] = Spe | Spp;
  1497. else
  1498. ctlr->base->rhportsts[port - 1] = Cpe;
  1499. iunlock(ctlr);
  1500. poperror();
  1501. qunlock(&ctlr->resetl);
  1502. }
  1503. static int
  1504. getportstatus(Ctlr *ub, int port)
  1505. {
  1506. int v;
  1507. ulong ohcistatus;
  1508. ohcistatus = ub->base->rhportsts[port - 1];
  1509. v = 0;
  1510. if(ohcistatus & Ccs)
  1511. v |= DevicePresent;
  1512. if(ohcistatus & Pes)
  1513. v |= PortEnable;
  1514. if(ohcistatus & Pss)
  1515. v |= Suspend;
  1516. if(ohcistatus & Prs)
  1517. v |= PortReset;
  1518. else {
  1519. /* port is not in reset; these potential writes are ok */
  1520. if(ohcistatus & Csc){
  1521. /* TODO: could notify usbd equivalent here */
  1522. v |= ConnectStatusChange;
  1523. ub->base->rhportsts[port - 1] = Csc;
  1524. }
  1525. if(ohcistatus & Pesc){
  1526. /* TODO: could notify usbd equivalent here */
  1527. v |= PortEnableChange;
  1528. ub->base->rhportsts[port - 1] = Pesc;
  1529. }
  1530. }
  1531. if(ohcistatus & Lsda)
  1532. v |= SlowDevice;
  1533. if(ohcistatus & Ccs)
  1534. XIPRINT("portstatus(%d) = OHCI 0x%.8lux UHCI 0x%.8ux\n",
  1535. port, ohcistatus, v);
  1536. return v;
  1537. }
  1538. /* print any interesting stuff here for debugging purposes */
  1539. static void
  1540. usbdebug(Usbhost *uh, char *s, char *se)
  1541. {
  1542. Udev *dev;
  1543. Endpt *ep;
  1544. int n, i, j;
  1545. n = 0;
  1546. for(i = 0; i < MaxUsbDev; i++)
  1547. if(uh->dev[i])
  1548. n++;
  1549. s = seprint(s, se, "OHCI, %d devices\n", n);
  1550. for(i = 0; i < MaxUsbDev; i++){
  1551. if((dev = uh->dev[i]) == nil)
  1552. continue;
  1553. s = seprint(s, se, "dev 0x%6.6lux, %d epts\n", dev->csp, dev->npt);
  1554. for(j = 0; j < nelem(dev->ep); j++){
  1555. if((ep = dev->ep[j]) == nil)
  1556. continue;
  1557. if(ep->epmode >= 0 && ep->epmode < Nmodes)
  1558. s = seprint(s, se, "ept %d/%d: %s 0x%6.6lux "
  1559. "maxpkt %d %s\n",
  1560. dev->x, ep->x, usbmode[ep->epmode],
  1561. ep->csp, ep->maxpkt, ep->active ? "active" : "idle");
  1562. else{
  1563. s = seprint(s, se, "ept %d/%d: bad mode 0x%6.6lux\n",
  1564. dev->x, ep->x, ep->csp);
  1565. continue;
  1566. }
  1567. switch(ep->epmode){
  1568. case Nomode:
  1569. break;
  1570. case Ctlmode:
  1571. break;
  1572. case Bulkmode:
  1573. break;
  1574. case Intrmode:
  1575. s = seprint(s, se, "\t%d ms\n",
  1576. ep->pollms);
  1577. break;
  1578. case Isomode:
  1579. s = seprint(s, se, "\t%d ms, remain %d, "
  1580. "partial %d, buffered %d, "
  1581. "xdone %d, xstarted %d, err %s\n",
  1582. ep->pollms, ep->remain, ep->partial,
  1583. ep->buffered,
  1584. ep->dir[ep->mode == OREAD].xdone,
  1585. ep->dir[ep->mode == OREAD].xstarted,
  1586. ep->dir[ep->mode == OREAD].err
  1587. ? ep->dir[ep->mode == OREAD].err : "no");
  1588. break;
  1589. }
  1590. }
  1591. }
  1592. }
  1593. /* this is called several times every few seconds, possibly due to usbd */
  1594. static void
  1595. portinfo(Usbhost *uh, char *s, char *se)
  1596. {
  1597. int x, i, j;
  1598. Ctlr *ctlr;
  1599. XIPRINT("ohci: portinfo from devusb\n");
  1600. ctlr = uh->ctlr;
  1601. for(i = 1; i <= ctlr->nports; i++){
  1602. ilock(ctlr);
  1603. x = getportstatus(ctlr, i);
  1604. iunlock(ctlr);
  1605. s = seprint(s, se, "%d %ux", i, x);
  1606. for(j = 0; j < nelem(portstatus); j++)
  1607. if((x & portstatus[j].bit) != 0)
  1608. s = seprint(s, se, " %s", portstatus[j].name);
  1609. s = seprint(s, se, "\n");
  1610. }
  1611. }
  1612. void
  1613. interrupt(Ureg *, void *arg)
  1614. {
  1615. int dirin, cc;
  1616. ulong ctrl, status;
  1617. uchar *p;
  1618. Block *bp;
  1619. Ctlr *ub;
  1620. Endpt *ep;
  1621. Endptx *epx;
  1622. TD *donetd, *nexttd;
  1623. Usbhost *eh;
  1624. XIPRINT("ohci: interrupt\n");
  1625. eh = arg;
  1626. ub = eh->ctlr;
  1627. status = ub->base->intrsts;
  1628. status &= ub->base->intrenable;
  1629. status &= Oc | Rhsc | Fno
  1630. | Ue
  1631. | Rd | Sf | Wdh
  1632. | So;
  1633. if(status & Wdh){
  1634. /* LSb of donehead has bit that says there are other interrupts */
  1635. donetd = hcva2ucva(ub->uchcca->donehead & ~0xf);
  1636. XIPRINT("donetd 0x%.8lux\n", donetd);
  1637. }else
  1638. donetd = 0;
  1639. ub->base->intrsts = status;
  1640. status &= ~Wdh;
  1641. while(donetd){
  1642. ctrl = donetd->ctrl;
  1643. ep = donetd->ep;
  1644. bp = donetd->bp;
  1645. donetd->bp = nil;
  1646. epx = ep->private;
  1647. ohciinterrupts[ep->epmode]++;
  1648. dirin = ((ctrl >> TD_DP_SHIFT) & TD_DP_MASK) == Otokin;
  1649. ep->buffered -= donetd->bytes;
  1650. if(ep->epmode == Isomode){
  1651. dirin = Dirout;
  1652. if(ep->buffered < 0){
  1653. print("intr: buffered %d bytes %ld\n",
  1654. ep->buffered, donetd->bytes);
  1655. ep->buffered = 0;
  1656. }
  1657. }
  1658. cc = (ctrl >> TD_CC_SHIFT) & TD_CC_MASK;
  1659. if((usbhdebug || ep->debug) && (cc != 0 && cc != 9)){
  1660. print("%d/%d: cc %d frnum 0x%lux\n",
  1661. ep->dev->x, ep->x, cc, ub->base->fmnumber);
  1662. dumptd(donetd, "after");
  1663. }
  1664. switch(cc){
  1665. case 8: /* Overrun, Not an error */
  1666. epx->overruns++;
  1667. /* fall through to no error code */
  1668. case 0: /* noerror */
  1669. if((donetd->flags & TD_FLAGS_LAST) == 0)
  1670. break;
  1671. if(dirin){
  1672. if(bp){
  1673. p = hcva2va(donetd->be + 1);
  1674. if(p < bp->wp)
  1675. print("interrupt: bp: rp 0x%lux"
  1676. ", wp 0x%lux→0x%lux\n",
  1677. bp->rp, bp->wp, p);
  1678. bp->wp = p;
  1679. }
  1680. }
  1681. ep->dir[dirin].xdone++;
  1682. wakeup(&ep->dir[dirin].rend);
  1683. break;
  1684. case 9: /* underrun */
  1685. if(bp){
  1686. p = hcva2va(donetd->cbp);
  1687. XEIPRINT("interrupt: bp: rp 0x%lux, wp "
  1688. "0x%lux→0x%lux\n", bp->rp, bp->wp, p);
  1689. bp->wp = p;
  1690. }
  1691. if((donetd->flags & TD_FLAGS_LAST) == 0){
  1692. XEIPRINT("Underrun\n");
  1693. ep->dir[dirin].err = Eunderrun;
  1694. }
  1695. ep->dir[dirin].xdone++;
  1696. wakeup(&ep->dir[dirin].rend);
  1697. break;
  1698. case 1: /* CRC */
  1699. ep->dir[dirin].err = "CRC error";
  1700. goto error;
  1701. case 2: /* Bitstuff */
  1702. ep->dir[dirin].err = "Bitstuff error";
  1703. goto error;
  1704. case 3:
  1705. ep->dir[dirin].err = "data toggle mismatch";
  1706. goto error;
  1707. case 4: /* Stall */
  1708. ep->dir[dirin].err = Estalled;
  1709. goto error;
  1710. case 5: /* No response */
  1711. ep->dir[dirin].err = "No response";
  1712. goto error;
  1713. case 6: /* PIDcheck */
  1714. ep->dir[dirin].err = "PIDcheck";
  1715. goto error;
  1716. case 7: /* UnexpectedPID */
  1717. ep->dir[dirin].err = "badPID";
  1718. goto error;
  1719. error:
  1720. XEPRINT("fail %d (%lud)\n", cc,
  1721. (ctrl >> TD_EC_SHIFT) & TD_EC_MASK);
  1722. ep->dir[dirin].xdone++;
  1723. wakeup(&ep->dir[dirin].rend);
  1724. break;
  1725. default:
  1726. panic("cc %lud unimplemented\n", cc);
  1727. }
  1728. ep->dir[dirin].queued--;
  1729. /* Clean up blocks used for transfers */
  1730. if(dirin == Dirout)
  1731. freeb(bp);
  1732. nexttd = TDgetnexttd(donetd);
  1733. TDfree(ub, donetd);
  1734. donetd = nexttd;
  1735. }
  1736. if(status & Sf){
  1737. if (0)
  1738. XIPRINT(("sof!!\n"));
  1739. // wakeup(&ub->sofr); /* sofr doesn't exist anywhere! */
  1740. status &= ~Sf;
  1741. }
  1742. if(status & Ue){
  1743. ulong curred;
  1744. // usbhdbg(); /* TODO */
  1745. curred = ub->base->periodcurred;
  1746. print("usbh: unrecoverable error frame 0x%.8lux ed 0x%.8lux, "
  1747. "ints %d %d %d %d\n",
  1748. ub->base->fmnumber, curred,
  1749. ohciinterrupts[1], ohciinterrupts[2],
  1750. ohciinterrupts[3], ohciinterrupts[4]);
  1751. if(curred)
  1752. dumped(hcva2ucva(curred));
  1753. }
  1754. if(status)
  1755. IPRINT(("interrupt: unhandled interrupt 0x%.8lux\n", status));
  1756. }
  1757. static void
  1758. usbhattach(Ctlr *ub) /* TODO: is unused now, but it fiddles ctlr */
  1759. {
  1760. ulong ctrl;
  1761. if(ub == nil || ub->base == 0)
  1762. error(Enodev);
  1763. ctrl = ub->base->control;
  1764. if((ctrl & HcfsMask) != HcfsOperational){
  1765. ctrl = (ctrl & ~HcfsMask) | HcfsOperational;
  1766. ub->base->control = ctrl;
  1767. ub->base->rhsts = Sgp;
  1768. }
  1769. }
  1770. static int
  1771. reptdone(void *arg)
  1772. {
  1773. Endpt *ep;
  1774. ep = arg;
  1775. return ep->dir[Dirin].err
  1776. /* Expression crafted to deal with wrap around: */
  1777. || ep->dir[Dirin].xdone - ep->dir[Dirin].xstarted >= 0;
  1778. }
  1779. Block *
  1780. breadusbh(Ctlr *ub, Endpt *ep, long n) /* guts of read() */
  1781. {
  1782. long in, l;
  1783. uchar *p;
  1784. Block *bp;
  1785. Endptx *epx;
  1786. epx = ep->private;
  1787. qlock(&ep->rlock);
  1788. EDsetC(epx->ed, ep->rdata01);
  1789. XEPRINT("breadusbh(%d/%d, %ld, dt %d)\n", ep->dev->x, ep->x, n, ep->rdata01);
  1790. eptenable(ub, ep, Dirin);
  1791. if(waserror()){
  1792. EDcancel(ub, epx->ed, Dirin);
  1793. ep->dir[Dirin].err = nil;
  1794. qunlock(&ep->rlock);
  1795. nexterror();
  1796. }
  1797. if(ep->dir[Dirin].err != nil)
  1798. error("usb: can't happen");
  1799. bp = allocrcvb(n);
  1800. in = n;
  1801. if(in > bp->lim - bp->wp){
  1802. print("usb: read larger than block\n");
  1803. in = bp->lim - bp->wp;
  1804. }
  1805. p = bp->rp;
  1806. do{
  1807. l = qtd(ub, ep, Dirin, bp, p, p+in, Otokin, 0);
  1808. p += l;
  1809. in -= l;
  1810. }while(in > 0);
  1811. sleep(&ep->dir[Dirin].rend, reptdone, ep);
  1812. if(ep->dir[Dirin].err){
  1813. EDcancel(ub, epx->ed, Dirin);
  1814. if(ep->dir[Dirin].err == Eunderrun)
  1815. ep->dir[Dirin].err = nil;
  1816. else
  1817. error(ep->dir[Dirin].err);
  1818. }
  1819. XEPRINT("breadusbh(%d/%d, %ld) returned %ld\n", ep->dev->x, ep->x, n,
  1820. BLEN(bp));
  1821. poperror();
  1822. qunlock(&ep->rlock);
  1823. ep->rdata01 = EDgetC(epx->ed);
  1824. return bp;
  1825. }
  1826. static long
  1827. read(Usbhost *uh, Endpt *ep, void *a, long n, vlong off) /* TODO off */
  1828. {
  1829. long l;
  1830. Block *bp;
  1831. Ctlr *ub;
  1832. XEPRINT("ohci: read from devusb\n");
  1833. USED(off);
  1834. ub = uh->ctlr;
  1835. XEPRINT("%d/%d: read 0x%.8lux %ld\n", ep->dev->x, ep->x, a, n);
  1836. bp = breadusbh(ub, ep, n);
  1837. l = BLEN(bp);
  1838. memmove(a, bp->rp, l);
  1839. printdata(bp->rp, 1, l);
  1840. XEPRINT("ohci: read %ld\n\n", l);
  1841. freeb(bp);
  1842. return l;
  1843. }
  1844. static int
  1845. weptdone(void *arg)
  1846. {
  1847. Endpt *ep;
  1848. ep = arg;
  1849. /*
  1850. * success when all operations are done or when less than
  1851. * a second is buffered in iso connections
  1852. */
  1853. return ep->dir[Dirout].xdone - ep->dir[Dirout].xstarted >= 0
  1854. || (ep->epmode == Isomode && ep->buffered <= ep->bw)
  1855. || ep->dir[Dirout].err;
  1856. }
  1857. /* TODO: honour off */
  1858. static long
  1859. write(Usbhost *uh, Endpt *ep, void *a, long n, vlong off, int tok)
  1860. {
  1861. long m;
  1862. short frnum;
  1863. uchar *p = a;
  1864. Block *b;
  1865. Ctlr *ub;
  1866. Endptx *epx;
  1867. XEPRINT("ohci: write(addr %p, bytes %ld, off %lld, tok %d) from devusb\n",
  1868. a, n, off, tok);
  1869. epx = ep->private;
  1870. ub = uh->ctlr;
  1871. qlock(&ep->wlock);
  1872. XEPRINT("%d/%d: write 0x%.8lux %ld %s\n", ep->dev->x, ep->x, a, n,
  1873. tok == Otoksetup? "setup": "out");
  1874. if(ep->dir[Dirout].xdone - ep->dir[Dirout].xstarted > 0){
  1875. print("done > started, %d %d\n",
  1876. ep->dir[Dirout].xdone, ep->dir[Dirout].xstarted);
  1877. ep->dir[Dirout].xdone = ep->dir[Dirout].xstarted;
  1878. }
  1879. if(waserror()){
  1880. lock(epx);
  1881. EDcancel(ub, epx->ed, Dirout);
  1882. unlock(epx);
  1883. ep->dir[Dirout].err = nil;
  1884. qunlock(&ep->wlock);
  1885. nexterror();
  1886. }
  1887. eptenable(ub, ep, Dirout);
  1888. EDsetC(epx->ed, ep->wdata01);
  1889. if(ep->dir[Dirout].err)
  1890. error(ep->dir[Dirout].err);
  1891. if((m = n) == 0 || p == nil)
  1892. qtd(ub, ep, Dirout, 0, 0, 0, tok, TD_FLAGS_LAST);
  1893. else{
  1894. b = allocwb(m+ep->partial);
  1895. if(ep->partial){
  1896. memmove(b->wp, ep->bpartial->rp, ep->partial);
  1897. b->wp += ep->partial;
  1898. ep->partial = 0;
  1899. }
  1900. validaddr((uintptr)p, m, 0); /* DEBUG */
  1901. memmove(b->wp, a, m);
  1902. b->wp += m;
  1903. printdata(b->rp, 1, m);
  1904. m = BLEN(b);
  1905. dcclean(b->rp, m);
  1906. if(ep->epmode == Isomode && ep->buffered <= ep->bw<<1){
  1907. sleep(&ep->dir[Dirout].rend, weptdone, ep);
  1908. if(ep->dir[Dirout].err)
  1909. error(ep->dir[Dirout].err);
  1910. }
  1911. while(m > 0){
  1912. int l;
  1913. l = qtd(ub, ep, Dirout, b, b->rp, b->wp, tok, 0);
  1914. b->rp += l;
  1915. m -= l;
  1916. tok = Otokout;
  1917. if(ep->partial){
  1918. /* We have some data to save */
  1919. if(ep->bpartial == nil)
  1920. ep->bpartial = allocb(ep->maxpkt);
  1921. if(ep->partial != m)
  1922. print("curious: %d != %ld\n",
  1923. ep->partial, m);
  1924. memmove(ep->bpartial->rp, b->rp, ep->partial);
  1925. ep->bpartial->wp = ep->bpartial->rp + ep->partial;
  1926. break;
  1927. }
  1928. }
  1929. freeb(b);
  1930. }
  1931. if(ep->epmode != Isomode){
  1932. sleep(&ep->dir[Dirout].rend, weptdone, ep);
  1933. if(ep->dir[Dirout].err)
  1934. error(ep->dir[Dirout].err);
  1935. }else if(0 && (frnum = ep->frnum - ub->base->fmnumber) < 0)
  1936. print("too late %d\n", frnum);
  1937. poperror();
  1938. qunlock(&ep->wlock);
  1939. XEPRINT("ohci: wrote %ld\n\n", n);
  1940. ep->wdata01 = EDgetC(epx->ed);
  1941. return n;
  1942. }
  1943. static void
  1944. init(Usbhost*)
  1945. {
  1946. XIPRINT("ohci: init from devusb\n");
  1947. }
  1948. static void
  1949. scanpci(void)
  1950. {
  1951. ulong mem;
  1952. Ctlr *ctlr;
  1953. Pcidev *p;
  1954. static int already = 0;
  1955. if(already)
  1956. return;
  1957. already = 1;
  1958. p = nil;
  1959. while(p = pcimatch(p, 0, 0)) {
  1960. /*
  1961. * Find OHCI controllers (Programming Interface = 0x10).
  1962. */
  1963. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb ||
  1964. p->ccrp != 0x10)
  1965. continue;
  1966. mem = p->mem[0].bar & ~0x0F;
  1967. XPRINT("usbohci: %x/%x port 0x%lux size 0x%x irq %d\n",
  1968. p->vid, p->did, mem, p->mem[0].size, p->intl);
  1969. if(mem == 0){
  1970. print("usbohci: failed to map registers\n");
  1971. continue;
  1972. }
  1973. if(p->intl == 0xFF || p->intl == 0) {
  1974. print("usbohci: no irq assigned for port %#lux\n", mem);
  1975. continue;
  1976. }
  1977. ctlr = malloc(sizeof(Ctlr));
  1978. ctlr->pcidev = p;
  1979. ctlr->base = vmap(mem, p->mem[0].size);
  1980. XPRINT("scanpci: ctlr 0x%lux, base 0x%lux\n", ctlr, ctlr->base);
  1981. pcisetbme(p);
  1982. pcisetpms(p, 0);
  1983. if(ctlrhead != nil)
  1984. ctlrtail->next = ctlr;
  1985. else
  1986. ctlrhead = ctlr;
  1987. ctlrtail = ctlr;
  1988. }
  1989. }
  1990. static int
  1991. reset(Usbhost *uh)
  1992. {
  1993. int i, linesize;
  1994. ulong io, fminterval, ctrl;
  1995. Ctlr *ctlr;
  1996. HCCA *atmp;
  1997. OHCI *ohci;
  1998. Pcidev *p;
  1999. QTree *qt;
  2000. /*
  2001. * data cache line size; probably doesn't matter on pc
  2002. * except that it must be a power of 2 for xspanalloc.
  2003. */
  2004. dcls = 32;
  2005. scanpci();
  2006. /*
  2007. * Any adapter matches if no uh->port is supplied,
  2008. * otherwise the ports must match.
  2009. */
  2010. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  2011. if(ctlr->active)
  2012. continue;
  2013. if(uh->port == 0 || uh->port == (uintptr)ctlr->base){
  2014. ctlr->active = 1;
  2015. break;
  2016. }
  2017. }
  2018. if(ctlr == nil)
  2019. return -1;
  2020. io = (uintptr)ctlr->base; /* TODO: correct? */
  2021. ohci = ctlr->base;
  2022. XPRINT("OHCI ctlr 0x%lux, base 0x%lux\n", ctlr, ohci);
  2023. p = ctlr->pcidev;
  2024. uh->ctlr = ctlr;
  2025. uh->port = io;
  2026. uh->irq = p->intl;
  2027. uh->tbdf = p->tbdf;
  2028. XPRINT("OHCI revision %ld.%ld\n", (ohci->revision >> 4) & 0xf,
  2029. ohci->revision & 0xf);
  2030. XPRINT("Host revision %ld.%ld\n", (ohci->hostrevision >> 4) & 0xf,
  2031. ohci->hostrevision & 0xf);
  2032. ctlr->nports = ohci->rhdesca & 0xff;
  2033. XPRINT("HcControl 0x%.8lux, %d ports\n", ohci->control, ctlr->nports);
  2034. delay(100); /* anything greater than 50 should ensure reset is done */
  2035. if(ohci->control == ~0){
  2036. ctlrhead = nil;
  2037. return -1;
  2038. }
  2039. /*
  2040. * usually enter here in reset, wait till its through,
  2041. * then do our own so we are on known timing conditions.
  2042. */
  2043. ohci->control = 0;
  2044. delay(100);
  2045. fminterval = ohci->fminterval;
  2046. /* legacy support register: turn off lunacy mode */
  2047. pcicfgw16(p, 0xc0, 0x2000);
  2048. /*
  2049. * transfer descs need at least 16 byte alignment, but
  2050. * align to dcache line size since the access will always be uncached.
  2051. * linesize must be a power of 2 for xspanalloc.
  2052. */
  2053. linesize = dcls;
  2054. if(linesize < 0x20)
  2055. linesize = 0x20;
  2056. ctlr->td.pool = va2ucva(xspanalloc(Ntd * sizeof(TD), linesize, 0));
  2057. if(ctlr->td.pool == nil)
  2058. panic("usbohci: no memory for TD pool");
  2059. for(i = Ntd - 1; --i >= 0;){
  2060. ctlr->td.pool[i].next = ctlr->td.free;
  2061. ctlr->td.free = &ctlr->td.pool[i];
  2062. }
  2063. ctlr->td.alloced = 0;
  2064. ctlr->ed.pool = va2ucva(xspanalloc(Ned*sizeof(ED), linesize, 0));
  2065. if(ctlr->ed.pool == nil)
  2066. panic("usbohci: no memory for ED pool");
  2067. for(i = Ned - 1; --i >= 0;){
  2068. ctlr->ed.pool[i].next = (ulong)ctlr->ed.free;
  2069. ctlr->ed.free = &ctlr->ed.pool[i];
  2070. }
  2071. ctlr->ed.alloced = 0;
  2072. atmp = xspanalloc(sizeof(HCCA), 256, 0);
  2073. if(atmp == nil)
  2074. panic("usbhreset: no memory for HCCA");
  2075. memset(atmp, 0, sizeof(*atmp));
  2076. ctlr->uchcca = atmp;
  2077. qt = mkqhtree(ctlr);
  2078. if(qt == nil){
  2079. panic("usb: can't allocate scheduling tree");
  2080. return -1;
  2081. }
  2082. ctlr->tree = qt;
  2083. ctlr->base = ohci;
  2084. /* time to move to rest then suspend mode. */
  2085. ohci->cmdsts = 1; /* reset the block */
  2086. while(ohci->cmdsts == 1)
  2087. continue; /* wait till reset complete, OHCI says 10us max. */
  2088. /*
  2089. * now that soft reset is done we are in suspend state.
  2090. * Setup registers which take in suspend state
  2091. * (will only be here for 2ms).
  2092. */
  2093. ohci->hcca = va2hcva(ctlr->uchcca);
  2094. OHCIsetControlHeadED(ctlr->base, 0);
  2095. ctlr->base->ctlcurred = 0;
  2096. OHCIsetBulkHeadED(ctlr->base, 0);
  2097. ctlr->base->bulkcurred = 0;
  2098. ohci->intrenable = Mie | Wdh | Ue;
  2099. ohci->control |= Cle | Ble | Ple | Ie | HcfsOperational;
  2100. /* set frame after operational */
  2101. ohci->fminterval = (fminterval &
  2102. ~(HcFmIntvl_FSMaxpack_MASK << HcFmIntvl_FSMaxpack_SHIFT)) |
  2103. 5120 << HcFmIntvl_FSMaxpack_SHIFT;
  2104. ohci->rhdesca = 1 << 9;
  2105. for(i = 0; i < ctlr->nports; i++)
  2106. ohci->rhportsts[i] = Spp | Spr;
  2107. delay(100);
  2108. ctrl = ohci->control;
  2109. if((ctrl & HcfsMask) != HcfsOperational){
  2110. XIPRINT("ohci: reset, take ctlr out of Suspend\n");
  2111. ctrl = (ctrl & ~HcfsMask) | HcfsOperational;
  2112. ohci->control = ctrl;
  2113. ohci->rhsts = Sgp;
  2114. }
  2115. p = ctlr->pcidev;
  2116. ctlr->irq = p->intl;
  2117. ctlr->tbdf = p->tbdf;
  2118. /*
  2119. * Linkage to the generic USB driver.
  2120. */
  2121. uh->init = init;
  2122. uh->interrupt = interrupt;
  2123. uh->debug = usbdebug;
  2124. uh->portinfo = portinfo;
  2125. uh->portreset = portreset;
  2126. uh->portenable = portenable;
  2127. uh->epalloc = epalloc;
  2128. uh->epfree = epfree;
  2129. uh->epopen = epopen;
  2130. uh->epclose = epclose;
  2131. uh->epmode = epmode;
  2132. uh->epmaxpkt = epmaxpkt;
  2133. uh->read = read;
  2134. uh->write = write;
  2135. uh->tokin = Otokin;
  2136. uh->tokout = Otokout;
  2137. uh->toksetup = Otoksetup;
  2138. return 0;
  2139. }
  2140. void
  2141. usbohcilink(void)
  2142. {
  2143. addusbtype("ohci", reset);
  2144. }