ohci.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* OHCI (USB 1.1) host controller driver.
  2. Memory-mapped (BAR0) controller with ED/TD lists in the identity-mapped
  3. kernel address space. Polling based (no IRQ): control transfers run on
  4. the control list and are monitored by polling each TD's condition code;
  5. the mouse's interrupt-IN endpoint is a single always-armed TD linked into
  6. every HCCA interrupt-table slot (F = 0 means "every frame"), so the
  7. controller retries NAKs on its own and software only harvests reports.
  8. Up to four controller instances (MCP79 exposes four of them); each PCI
  9. function is one root hub with its own device address space. */
  10. #include "usb.h"
  11. #include "ohci.h"
  12. #include "usb_hcd.h"
  13. #include "pci.h"
  14. #include "paging.h"
  15. #include "mm.h"
  16. #include "serial.h"
  17. #include "timer.h"
  18. /* Register offsets. */
  19. #define R_REVISION 0x00
  20. #define R_CONTROL 0x04
  21. #define R_COMMAND 0x08
  22. #define R_INT_STATUS 0x0C
  23. #define R_INT_ENABLE 0x10
  24. #define R_INT_DISABLE 0x14
  25. #define R_HCCA 0x18
  26. #define R_PERIOD_CUR_ED 0x1C
  27. #define R_CTRL_HEAD_ED 0x20
  28. #define R_CTRL_CUR_ED 0x24
  29. #define R_BULK_HEAD_ED 0x28
  30. #define R_BULK_CUR_ED 0x2C
  31. #define R_DONE_HEAD 0x30
  32. #define R_FM_INTERVAL 0x34
  33. #define R_FM_REMAINING 0x38
  34. #define R_FM_NUMBER 0x3C
  35. #define R_PERIODIC_START 0x40
  36. #define R_LS_THRESHOLD 0x44
  37. #define R_RH_DESC_A 0x48
  38. #define R_RH_DESC_B 0x4C
  39. #define R_RH_STATUS 0x50
  40. #define R_RH_PORT_FIRST 0x54
  41. /* R_CONTROL bits. */
  42. #define CTL_PLE 0x00000004u
  43. #define CTL_CLE 0x00000010u
  44. #define CTL_BLE 0x00000020u
  45. #define CTL_HCFS 0x000000C0u
  46. #define HCFS_RESUME 0x00000040u
  47. #define HCFS_OPER 0x00000080u
  48. /* R_COMMAND bits. */
  49. #define CMD_HCR 0x00000001u
  50. #define CMD_CLF 0x00000002u
  51. /* R_INT_STATUS bits. */
  52. #define INTR_WDH 0x00000002u
  53. /* R_RH_DESC_A bits. */
  54. #define RHA_PSM 0x00000100u
  55. #define RHA_NPS 0x00000200u
  56. #define RHA_POTPGT 0xFF000000u
  57. #define RHA_NDP 0x000000FFu
  58. /* R_RH_STATUS bits. */
  59. #define RHS_LPSC 0x00010000u
  60. /* R_RH_DESC_B: PortPowerControlMask (which ports are individually
  61. switched when PSM is set). */
  62. #define RHB_PPCM 0xFFFF0000u
  63. /* Root hub port status bits. */
  64. #define PS_CCS 0x00000001u
  65. #define PS_PES 0x00000002u
  66. #define PS_PRS 0x00000010u
  67. #define PS_PPS 0x00000100u /* port power (write 1 to switch power on) */
  68. #define PS_LSDA 0x00000200u
  69. #define PS_CSC 0x00010000u
  70. #define PS_PESC 0x00020000u
  71. #define PS_PRSC 0x00100000u
  72. #define PS_WTC (PS_CSC | PS_PESC | PS_PRSC)
  73. /* Endpoint descriptor (ED) fields. */
  74. #define ED_FA(a) ((a) & 0x7Fu)
  75. #define ED_EN(e) (((e) & 0xFu) << 7)
  76. #define ED_S (1u << 13)
  77. #define ED_MPS(m) (((m) & 0x7FFu) << 16)
  78. #define ED_PTR_MASK 0xFFFFFFF0u
  79. #define ED_H 0x00000001u /* head pointer: halted */
  80. #define ED_C 0x00000002u /* head pointer: data-toggle carry */
  81. /* Transfer descriptor (TD) fields. */
  82. #define TD_R (1u << 18)
  83. #define TD_DP(d) (((d) & 3u) << 19)
  84. #define TD_DP_SETUP 0u
  85. #define TD_DP_OUT 1u
  86. #define TD_DP_IN 2u
  87. /* Data toggle field (bits 24-25): 10b = DATA0, 11b = DATA1. */
  88. #define TD_T_DATA0 0x02000000u
  89. #define TD_T_DATA1 0x03000000u
  90. #define TD_CC_SHIFT 28
  91. #define TD_CC_MASK 0xF0000000u
  92. #define TD_NOTACCESSED 0xFu
  93. /* PCI: class 0x0C (serial), subclass 0x03 (USB), prog.if 0x10 (OHCI). */
  94. #define PCI_USB_CLASS 0x0C03u
  95. #define OHCI_MAX_TD 16
  96. struct ohci_ed {
  97. unsigned int flags;
  98. unsigned int tail;
  99. unsigned int head;
  100. unsigned int next;
  101. } __attribute__((aligned(16)));
  102. struct ohci_td {
  103. unsigned int flags;
  104. unsigned int cbp;
  105. unsigned int next;
  106. unsigned int be;
  107. } __attribute__((aligned(16)));
  108. struct ohci_ctrl {
  109. volatile unsigned int *regs;
  110. unsigned int bus, dev, func, bar;
  111. unsigned int hcca;
  112. unsigned int ports;
  113. unsigned int potpgt;
  114. int nps;
  115. /* Interrupt-IN endpoints: slot 0 = mouse, slot 1 = keyboard. All
  116. slots stay linked into the periodic list; empty EDs are skipped
  117. by the controller. */
  118. struct ohci_ed int_ed[USB_HCD_INT_SLOTS];
  119. struct ohci_td int_td[USB_HCD_INT_SLOTS];
  120. struct ohci_td int_dummy[USB_HCD_INT_SLOTS];
  121. unsigned char int_buf[USB_HCD_INT_SLOTS][8] __attribute__((aligned(16)));
  122. unsigned int int_mps[USB_HCD_INT_SLOTS];
  123. unsigned int int_toggle[USB_HCD_INT_SLOTS];
  124. int int_armed[USB_HCD_INT_SLOTS];
  125. };
  126. static struct ohci_ctrl ctrls[USB_HCD_MAX_CTRL];
  127. static unsigned int n_ctrls;
  128. /* Control-transfer scratch: transfers are serialized, so one ED/TD set for
  129. all instances is enough. A dummy TD anchors the tail of each queue (the
  130. controller stops when HeadP == TailP; the dummy is never processed). */
  131. static struct ohci_ed ctrl_ed;
  132. static struct ohci_td ctrl_tds[OHCI_MAX_TD];
  133. static struct ohci_td ctrl_dummy;
  134. static unsigned char setup_buf[8] __attribute__((aligned(16)));
  135. static int ctl_busy;
  136. static void ohci_int_in_stop(unsigned int slot);
  137. static unsigned int rd(struct ohci_ctrl *c, unsigned int ofs)
  138. {
  139. return c->regs[ofs >> 2];
  140. }
  141. static void wr(struct ohci_ctrl *c, unsigned int ofs, unsigned int val)
  142. {
  143. c->regs[ofs >> 2] = val;
  144. }
  145. static void delay_ms(unsigned int ms)
  146. {
  147. usb_delay_ms(ms); /* sleeps inside a sleepable scan */
  148. }
  149. static unsigned int port_reg(unsigned int port)
  150. {
  151. return R_RH_PORT_FIRST + 4u * port;
  152. }
  153. static int ohci_setup_one(unsigned int bus, unsigned int dev, unsigned int func)
  154. {
  155. struct ohci_ctrl *c;
  156. unsigned int bar, cmd, rha, t0, i;
  157. if (n_ctrls >= USB_HCD_MAX_CTRL)
  158. return -1;
  159. bar = pci_cfg_read32(bus, dev, func, 0x10);
  160. if (bar & 0x1u) {
  161. serial_puts(SERIAL_COM1, "ohci: BAR0 is not MMIO\n");
  162. return -1; /* register block must be MMIO */
  163. }
  164. bar &= 0xFFFFFFF0u;
  165. if (!bar) {
  166. serial_puts(SERIAL_COM1, "ohci: BAR0 is unassigned\n");
  167. return -1;
  168. }
  169. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  170. pci_cfg_write32(bus, dev, func, 0x04, cmd | 0x0006u); /* mem + bus master */
  171. /* Disable legacy SMI emulation (same idea as the UHCI driver). */
  172. pci_cfg_write16(bus, dev, func, 0xC0, 0);
  173. c = &ctrls[n_ctrls];
  174. c->regs = (volatile unsigned int *)bar;
  175. c->bus = bus;
  176. c->dev = dev;
  177. c->func = func;
  178. c->bar = bar;
  179. paging_map_mmio(bar, 0x1000);
  180. if (rd(c, R_REVISION) == 0xFFFFFFFFu || rd(c, R_REVISION) == 0) {
  181. serial_puts(SERIAL_COM1, "ohci: no register response (rev ");
  182. serial_puthex(SERIAL_COM1, rd(c, R_REVISION));
  183. serial_puts(SERIAL_COM1, ")\n");
  184. return -1;
  185. }
  186. c->hcca = mm_alloc_page();
  187. if (!c->hcca) {
  188. serial_puts(SERIAL_COM1, "ohci: no memory for HCCA\n");
  189. return -1;
  190. }
  191. for (i = 0; i < 1024; i++)
  192. ((unsigned int *)c->hcca)[i] = 0;
  193. /* Periodic list: the interrupt EDs are chained and referenced from
  194. every HCCA table slot (F = 0 means "every frame"). Idle queues
  195. are empty (HeadP == TailP == dummy) but carry valid ED fields. */
  196. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  197. c->int_ed[i].flags = ED_FA(0) | ED_EN(0) | ED_MPS(8);
  198. c->int_ed[i].tail = (unsigned int)&c->int_dummy[i];
  199. c->int_ed[i].head = (unsigned int)&c->int_dummy[i];
  200. c->int_ed[i].next = (i + 1 < USB_HCD_INT_SLOTS)
  201. ? (unsigned int)&c->int_ed[i + 1] : 0;
  202. c->int_dummy[i].flags = TD_NOTACCESSED << TD_CC_SHIFT;
  203. c->int_dummy[i].cbp = 0;
  204. c->int_dummy[i].next = 0;
  205. c->int_dummy[i].be = 0;
  206. c->int_armed[i] = 0;
  207. }
  208. for (i = 0; i < 32; i++)
  209. ((unsigned int *)c->hcca)[i] = (unsigned int)&c->int_ed[0];
  210. /* Host controller reset. */
  211. wr(c, R_COMMAND, CMD_HCR);
  212. t0 = get_ticks_ms();
  213. while (rd(c, R_COMMAND) & CMD_HCR) {
  214. if (get_ticks_ms() - t0 > 100)
  215. break;
  216. }
  217. delay_ms(2);
  218. /* Standard frame timing (12 MHz / 1 kHz - 1) and list setup. */
  219. wr(c, R_FM_INTERVAL, (0x2778u << 16) | 0x2EDFu);
  220. wr(c, R_PERIODIC_START, 0x2A2Fu);
  221. wr(c, R_LS_THRESHOLD, 0x628u);
  222. wr(c, R_HCCA, c->hcca);
  223. wr(c, R_INT_DISABLE, 0xFFFFFFFFu);
  224. wr(c, R_INT_STATUS, 0xFFFFFFFFu); /* clear pending */
  225. wr(c, R_DONE_HEAD, 0);
  226. /* Root hub: port count and power. */
  227. rha = rd(c, R_RH_DESC_A);
  228. c->ports = rha & RHA_NDP;
  229. if (c->ports > 15)
  230. c->ports = 15;
  231. if (c->ports == 0) {
  232. serial_puts(SERIAL_COM1, "ohci: no downstream ports\n");
  233. return -1;
  234. }
  235. c->nps = (rha & RHA_NPS) ? 1 : 0;
  236. c->potpgt = (rha & RHA_POTPGT) >> 24;
  237. if (!c->nps) {
  238. /* Power switching is implemented: mark every port as
  239. individually switchable, raise global power and switch on
  240. each port, then let the supply settle (POTPGT in 2 ms
  241. units). */
  242. wr(c, R_RH_DESC_B, RHB_PPCM);
  243. wr(c, R_RH_STATUS, RHS_LPSC);
  244. for (i = 0; i < c->ports; i++)
  245. wr(c, port_reg(i), PS_PPS);
  246. delay_ms(c->potpgt * 2 + 20);
  247. } else {
  248. delay_ms(20);
  249. }
  250. /* Leave reset (spec: via USBRESUME) and enter operational with the
  251. periodic + control lists enabled. */
  252. wr(c, R_CONTROL, HCFS_RESUME);
  253. delay_ms(20);
  254. wr(c, R_CONTROL, HCFS_OPER | CTL_PLE | CTL_CLE);
  255. n_ctrls++;
  256. {
  257. unsigned int conn = 0, pwr = 0, st;
  258. for (i = 0; i < c->ports; i++) {
  259. st = rd(c, port_reg(i));
  260. if (st & PS_CCS)
  261. conn++;
  262. if (st & PS_PPS)
  263. pwr++;
  264. }
  265. serial_puts(SERIAL_COM1, "ohci: controller at ");
  266. serial_putdec(SERIAL_COM1, bus);
  267. serial_puts(SERIAL_COM1, ":");
  268. serial_putdec(SERIAL_COM1, dev);
  269. serial_puts(SERIAL_COM1, ".");
  270. serial_putdec(SERIAL_COM1, func);
  271. serial_puts(SERIAL_COM1, " ports ");
  272. serial_putdec(SERIAL_COM1, c->ports);
  273. serial_puts(SERIAL_COM1, " psm ");
  274. serial_putdec(SERIAL_COM1, (rha & RHA_PSM) ? 1u : 0u);
  275. serial_puts(SERIAL_COM1, " nps ");
  276. serial_putdec(SERIAL_COM1, (unsigned int)c->nps);
  277. serial_puts(SERIAL_COM1, " potpgt ");
  278. serial_putdec(SERIAL_COM1, c->potpgt);
  279. serial_puts(SERIAL_COM1, " conn ");
  280. serial_putdec(SERIAL_COM1, conn);
  281. serial_puts(SERIAL_COM1, " pwr ");
  282. serial_putdec(SERIAL_COM1, pwr);
  283. serial_puts(SERIAL_COM1, "\n");
  284. }
  285. return 0;
  286. }
  287. static int ohci_init(void)
  288. {
  289. unsigned int bus, dev, func;
  290. if (n_ctrls)
  291. return (int)n_ctrls;
  292. for (bus = 0; bus < 256 && n_ctrls < USB_HCD_MAX_CTRL; bus++) {
  293. for (dev = 0; dev < 32; dev++) {
  294. for (func = 0; func < 8; func++) {
  295. unsigned int id = pci_cfg_read32(bus, dev, func, 0x00);
  296. unsigned int class_rev;
  297. if (id == 0xFFFFFFFFu || id == 0)
  298. continue;
  299. class_rev = pci_cfg_read32(bus, dev, func, 0x08);
  300. if ((class_rev >> 16) != PCI_USB_CLASS)
  301. continue;
  302. if (((class_rev >> 8) & 0xFFu) != 0x10u)
  303. continue; /* not OHCI */
  304. ohci_setup_one(bus, dev, func);
  305. }
  306. }
  307. }
  308. return (int)n_ctrls;
  309. }
  310. static unsigned int ohci_count(void)
  311. {
  312. return n_ctrls;
  313. }
  314. static void ohci_info(unsigned int ctrl, unsigned int *bus, unsigned int *dev,
  315. unsigned int *func, unsigned int *base, int *mmio)
  316. {
  317. struct ohci_ctrl *c = &ctrls[ctrl];
  318. *bus = c->bus;
  319. *dev = c->dev;
  320. *func = c->func;
  321. *base = c->bar;
  322. *mmio = 1;
  323. }
  324. static unsigned int ohci_port_count(unsigned int ctrl)
  325. {
  326. return ctrls[ctrl].ports;
  327. }
  328. static int ohci_port_connected(unsigned int ctrl, unsigned int port)
  329. {
  330. return (rd(&ctrls[ctrl], port_reg(port)) & PS_CCS) ? 1 : 0;
  331. }
  332. static int ohci_port_low_speed(unsigned int ctrl, unsigned int port)
  333. {
  334. return (rd(&ctrls[ctrl], port_reg(port)) & PS_LSDA) ? 1 : 0;
  335. }
  336. static int ohci_port_take_change(unsigned int ctrl, unsigned int port)
  337. {
  338. struct ohci_ctrl *c = &ctrls[ctrl];
  339. unsigned int ofs = port_reg(port);
  340. if (!(rd(c, ofs) & PS_CSC))
  341. return 0;
  342. wr(c, ofs, PS_CSC); /* write-1-to-clear */
  343. return 1;
  344. }
  345. static int ohci_port_reset(unsigned int ctrl, unsigned int port)
  346. {
  347. struct ohci_ctrl *c = &ctrls[ctrl];
  348. unsigned int ofs = port_reg(port);
  349. unsigned int t0, st;
  350. if (!(rd(c, ofs) & PS_CCS))
  351. return -1;
  352. /* Clear stale change bits. */
  353. wr(c, ofs, PS_WTC);
  354. /* Assert reset; the controller clears PRS when the reset finishes
  355. (at least 10 ms for the device to see it). */
  356. wr(c, ofs, PS_PRS);
  357. t0 = get_ticks_ms();
  358. for (;;) {
  359. if (!(rd(c, ofs) & PS_PRS))
  360. break;
  361. if (get_ticks_ms() - t0 > 200)
  362. return -1;
  363. }
  364. delay_ms(10);
  365. /* Acknowledge reset-complete and make sure the port is enabled. */
  366. wr(c, ofs, PS_PRSC | PS_CSC | PS_PESC);
  367. wr(c, ofs, PS_PES);
  368. st = rd(c, ofs);
  369. if (!(st & PS_CCS) || !(st & PS_PES))
  370. return -1;
  371. return 0;
  372. }
  373. static void td_set(struct ohci_td *td, unsigned int flags, unsigned int cbp,
  374. unsigned int be)
  375. {
  376. td->flags = flags | (TD_NOTACCESSED << TD_CC_SHIFT);
  377. td->cbp = cbp;
  378. td->next = 0;
  379. td->be = be;
  380. }
  381. static int ohci_transfer(struct ohci_ctrl *c, unsigned int addr,
  382. unsigned int mps, int low,
  383. const unsigned char *setup, unsigned char *data,
  384. unsigned int len, int dir_in)
  385. {
  386. unsigned int i, n = 0, n_data = 0, t0;
  387. if (mps == 0 || mps > 64)
  388. mps = 64;
  389. for (i = 0; i < 8; i++)
  390. setup_buf[i] = setup[i];
  391. /* SETUP stage (DATA0). */
  392. td_set(&ctrl_tds[n++], TD_DP(TD_DP_SETUP) | TD_T_DATA0,
  393. (unsigned int)setup_buf, (unsigned int)setup_buf + 7);
  394. /* DATA stage: one TD for the whole buffer, DATA1 (like Linux; the
  395. controller splits it into MPS-sized packets itself). */
  396. if (len > 0) {
  397. td_set(&ctrl_tds[n++],
  398. TD_R | TD_DP(dir_in ? TD_DP_IN : TD_DP_OUT) |
  399. TD_T_DATA1,
  400. (unsigned int)data, (unsigned int)(data + len - 1));
  401. n_data = 1;
  402. }
  403. /* STATUS stage (DATA1, opposite direction, no data). */
  404. td_set(&ctrl_tds[n++],
  405. TD_DP(dir_in ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1, 0, 0);
  406. /* Chain the TDs and anchor the tail on the dummy TD. */
  407. for (i = 0; i + 1 < n; i++)
  408. ctrl_tds[i].next = (unsigned int)&ctrl_tds[i + 1];
  409. ctrl_tds[n - 1].next = (unsigned int)&ctrl_dummy;
  410. ctrl_dummy.flags = TD_NOTACCESSED << TD_CC_SHIFT;
  411. ctrl_dummy.cbp = 0;
  412. ctrl_dummy.next = 0;
  413. ctrl_dummy.be = 0;
  414. ctrl_ed.flags = ED_FA(addr) | ED_EN(0) | ED_MPS(mps) | (low ? ED_S : 0);
  415. ctrl_ed.tail = (unsigned int)&ctrl_dummy;
  416. ctrl_ed.head = (unsigned int)&ctrl_tds[0];
  417. ctrl_ed.next = 0;
  418. wr(c, R_CTRL_HEAD_ED, (unsigned int)&ctrl_ed);
  419. wr(c, R_COMMAND, CMD_CLF);
  420. /* Poll until every TD has left "not accessed". A NAKed TD stays
  421. untouched and is retried by the controller next frame. */
  422. t0 = get_ticks_ms();
  423. for (;;) {
  424. int done = 1, err = 0;
  425. for (i = 0; i < n; i++) {
  426. unsigned int cc = (ctrl_tds[i].flags & TD_CC_MASK)
  427. >> TD_CC_SHIFT;
  428. if (cc == TD_NOTACCESSED) {
  429. done = 0;
  430. continue;
  431. }
  432. if (cc != 0) {
  433. err = 1;
  434. serial_puts(SERIAL_COM1, "ohci: xfer err td ");
  435. serial_putdec(SERIAL_COM1, i);
  436. serial_puts(SERIAL_COM1, " cc ");
  437. serial_putdec(SERIAL_COM1, cc);
  438. serial_putc(SERIAL_COM1, '\n');
  439. }
  440. }
  441. if (err) {
  442. ctrl_ed.head = (unsigned int)&ctrl_dummy;
  443. wr(c, R_CTRL_HEAD_ED, 0);
  444. return -1;
  445. }
  446. if (done)
  447. break;
  448. if (get_ticks_ms() - t0 > 2000) {
  449. serial_puts(SERIAL_COM1, "ohci: xfer timeout\n");
  450. ctrl_ed.head = (unsigned int)&ctrl_dummy;
  451. wr(c, R_CTRL_HEAD_ED, 0);
  452. return -1;
  453. }
  454. }
  455. ctrl_ed.head = (unsigned int)&ctrl_dummy;
  456. wr(c, R_CTRL_HEAD_ED, 0);
  457. /* Bytes actually received: the TD's CBP advances with the data
  458. (the controller clears it to 0 when the whole buffer arrived). */
  459. if (len > 0 && dir_in && n_data == 1) {
  460. unsigned int cbp = ctrl_tds[1].cbp;
  461. if (cbp == 0)
  462. return (int)len;
  463. return (int)(cbp - (unsigned int)data);
  464. }
  465. return (int)len;
  466. }
  467. static int ohci_control_transfer(unsigned int ctrl, unsigned int addr,
  468. unsigned int mps, int low_speed,
  469. const unsigned char *setup,
  470. unsigned char *data, unsigned int len,
  471. int dir_in)
  472. {
  473. int r;
  474. if (ctrl >= n_ctrls || ctl_busy)
  475. return -1;
  476. ctl_busy = 1;
  477. r = ohci_transfer(&ctrls[ctrl], addr, mps, low_speed, setup, data, len,
  478. dir_in);
  479. ctl_busy = 0;
  480. return r;
  481. }
  482. /* Arm one interrupt TD of one slot: (re)write it active. Only call with
  483. the TD retired. The ED stays linked in the periodic chain. */
  484. static void ohci_int_arm(struct ohci_ctrl *c, unsigned int slot)
  485. {
  486. c->int_td[slot].flags = (TD_NOTACCESSED << TD_CC_SHIFT) | TD_R |
  487. TD_DP(TD_DP_IN) |
  488. (c->int_toggle[slot] ? TD_T_DATA1
  489. : TD_T_DATA0);
  490. c->int_td[slot].cbp = (unsigned int)c->int_buf[slot];
  491. c->int_td[slot].next = (unsigned int)&c->int_dummy[slot];
  492. c->int_td[slot].be = (unsigned int)c->int_buf[slot] +
  493. c->int_mps[slot] - 1;
  494. c->int_ed[slot].head = (unsigned int)&c->int_td[slot] |
  495. (c->int_toggle[slot] ? ED_C : 0);
  496. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  497. /* int_ed[slot].next keeps the chain to the other slots. */
  498. c->int_armed[slot] = 1;
  499. }
  500. static int ohci_int_in_start(unsigned int ctrl, unsigned int slot,
  501. unsigned int addr, unsigned int ep,
  502. unsigned int mps, int low_speed)
  503. {
  504. struct ohci_ctrl *c;
  505. if (ctrl >= n_ctrls || slot >= USB_HCD_INT_SLOTS)
  506. return -1;
  507. c = &ctrls[ctrl];
  508. ohci_int_in_stop(slot);
  509. c->int_mps[slot] = mps ? mps : 8;
  510. c->int_ed[slot].flags = ED_FA(addr) | ED_EN(ep) |
  511. ED_MPS(c->int_mps[slot]) |
  512. (low_speed ? ED_S : 0);
  513. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  514. c->int_ed[slot].head = (unsigned int)&c->int_dummy[slot];
  515. c->int_toggle[slot] = 0; /* first report is DATA0 */
  516. ohci_int_arm(c, slot);
  517. return 0;
  518. }
  519. static int ohci_int_in_poll(unsigned int slot, unsigned char *buf,
  520. unsigned int len)
  521. {
  522. struct ohci_ctrl *c = 0;
  523. unsigned int i, cc, n;
  524. if (slot >= USB_HCD_INT_SLOTS)
  525. return 0;
  526. for (i = 0; i < n_ctrls; i++) {
  527. if (ctrls[i].int_armed[slot]) {
  528. c = &ctrls[i];
  529. break;
  530. }
  531. }
  532. if (!c)
  533. return 0;
  534. cc = (c->int_td[slot].flags & TD_CC_MASK) >> TD_CC_SHIFT;
  535. if (cc == TD_NOTACCESSED) {
  536. if (c->int_ed[slot].head & ED_H) {
  537. /* Halted by the controller without a CC update:
  538. clear it and stay armed. */
  539. ohci_int_arm(c, slot);
  540. return -1;
  541. }
  542. return 0; /* still queued; the controller retries NAKs */
  543. }
  544. if (cc != 0) {
  545. /* Report the error but stay armed (toggle preserved). */
  546. ohci_int_arm(c, slot);
  547. return -1;
  548. }
  549. n = c->int_td[slot].cbp
  550. ? (c->int_td[slot].cbp - (unsigned int)c->int_buf[slot])
  551. : c->int_mps[slot];
  552. if (n > len)
  553. n = len;
  554. for (i = 0; i < n; i++)
  555. buf[i] = c->int_buf[slot][i];
  556. c->int_toggle[slot] ^= 1u;
  557. ohci_int_arm(c, slot);
  558. return (int)n;
  559. }
  560. static void ohci_int_in_stop(unsigned int slot)
  561. {
  562. unsigned int i;
  563. if (slot >= USB_HCD_INT_SLOTS)
  564. return;
  565. for (i = 0; i < n_ctrls; i++) {
  566. struct ohci_ctrl *c = &ctrls[i];
  567. if (!c->int_armed[slot])
  568. continue;
  569. c->int_armed[slot] = 0;
  570. c->int_ed[slot].head = (unsigned int)&c->int_dummy[slot];
  571. c->int_ed[slot].tail = (unsigned int)&c->int_dummy[slot];
  572. }
  573. }
  574. const struct usb_hcd_ops usb_ohci_ops = {
  575. "OHCI",
  576. ohci_init,
  577. ohci_count,
  578. ohci_info,
  579. ohci_control_transfer,
  580. ohci_port_count,
  581. ohci_port_connected,
  582. ohci_port_low_speed,
  583. ohci_port_reset,
  584. ohci_port_take_change,
  585. ohci_int_in_start,
  586. ohci_int_in_poll,
  587. ohci_int_in_stop,
  588. 0,
  589. 0,
  590. 0,
  591. };