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