usb-uhci.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * USB UHCI controller emulation
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  6. * Copyright (c) 2008 Max Krasnyansky
  7. * Magor rewrite of the UHCI data structures parser and frame processor
  8. * Support for fully async operation and multiple outstanding transactions
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include "hw.h"
  29. #include "usb.h"
  30. #include "pci.h"
  31. #include "qemu-timer.h"
  32. //#define DEBUG
  33. //#define DEBUG_DUMP_DATA
  34. #define UHCI_CMD_FGR (1 << 4)
  35. #define UHCI_CMD_EGSM (1 << 3)
  36. #define UHCI_CMD_GRESET (1 << 2)
  37. #define UHCI_CMD_HCRESET (1 << 1)
  38. #define UHCI_CMD_RS (1 << 0)
  39. #define UHCI_STS_HCHALTED (1 << 5)
  40. #define UHCI_STS_HCPERR (1 << 4)
  41. #define UHCI_STS_HSERR (1 << 3)
  42. #define UHCI_STS_RD (1 << 2)
  43. #define UHCI_STS_USBERR (1 << 1)
  44. #define UHCI_STS_USBINT (1 << 0)
  45. #define TD_CTRL_SPD (1 << 29)
  46. #define TD_CTRL_ERROR_SHIFT 27
  47. #define TD_CTRL_IOS (1 << 25)
  48. #define TD_CTRL_IOC (1 << 24)
  49. #define TD_CTRL_ACTIVE (1 << 23)
  50. #define TD_CTRL_STALL (1 << 22)
  51. #define TD_CTRL_BABBLE (1 << 20)
  52. #define TD_CTRL_NAK (1 << 19)
  53. #define TD_CTRL_TIMEOUT (1 << 18)
  54. #define UHCI_PORT_RESET (1 << 9)
  55. #define UHCI_PORT_LSDA (1 << 8)
  56. #define UHCI_PORT_ENC (1 << 3)
  57. #define UHCI_PORT_EN (1 << 2)
  58. #define UHCI_PORT_CSC (1 << 1)
  59. #define UHCI_PORT_CCS (1 << 0)
  60. #define FRAME_TIMER_FREQ 1000
  61. #define FRAME_MAX_LOOPS 100
  62. #define NB_PORTS 2
  63. #ifdef DEBUG
  64. #define dprintf printf
  65. const char *pid2str(int pid)
  66. {
  67. switch (pid) {
  68. case USB_TOKEN_SETUP: return "SETUP";
  69. case USB_TOKEN_IN: return "IN";
  70. case USB_TOKEN_OUT: return "OUT";
  71. }
  72. return "?";
  73. }
  74. #else
  75. #define dprintf(...)
  76. #endif
  77. #ifdef DEBUG_DUMP_DATA
  78. static void dump_data(const uint8_t *data, int len)
  79. {
  80. int i;
  81. printf("uhci: data: ");
  82. for(i = 0; i < len; i++)
  83. printf(" %02x", data[i]);
  84. printf("\n");
  85. }
  86. #else
  87. static void dump_data(const uint8_t *data, int len) {}
  88. #endif
  89. /*
  90. * Pending async transaction.
  91. * 'packet' must be the first field because completion
  92. * handler does "(UHCIAsync *) pkt" cast.
  93. */
  94. typedef struct UHCIAsync {
  95. USBPacket packet;
  96. struct UHCIAsync *next;
  97. uint32_t td;
  98. uint32_t token;
  99. int8_t valid;
  100. uint8_t done;
  101. uint8_t buffer[2048];
  102. } UHCIAsync;
  103. typedef struct UHCIPort {
  104. USBPort port;
  105. uint16_t ctrl;
  106. } UHCIPort;
  107. typedef struct UHCIState {
  108. PCIDevice dev;
  109. uint16_t cmd; /* cmd register */
  110. uint16_t status;
  111. uint16_t intr; /* interrupt enable register */
  112. uint16_t frnum; /* frame number */
  113. uint32_t fl_base_addr; /* frame list base address */
  114. uint8_t sof_timing;
  115. uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
  116. QEMUTimer *frame_timer;
  117. UHCIPort ports[NB_PORTS];
  118. /* Interrupts that should be raised at the end of the current frame. */
  119. uint32_t pending_int_mask;
  120. /* Active packets */
  121. UHCIAsync *async_pending;
  122. UHCIAsync *async_pool;
  123. } UHCIState;
  124. typedef struct UHCI_TD {
  125. uint32_t link;
  126. uint32_t ctrl; /* see TD_CTRL_xxx */
  127. uint32_t token;
  128. uint32_t buffer;
  129. } UHCI_TD;
  130. typedef struct UHCI_QH {
  131. uint32_t link;
  132. uint32_t el_link;
  133. } UHCI_QH;
  134. static UHCIAsync *uhci_async_alloc(UHCIState *s)
  135. {
  136. UHCIAsync *async = qemu_malloc(sizeof(UHCIAsync));
  137. memset(&async->packet, 0, sizeof(async->packet));
  138. async->valid = 0;
  139. async->td = 0;
  140. async->token = 0;
  141. async->done = 0;
  142. async->next = NULL;
  143. return async;
  144. }
  145. static void uhci_async_free(UHCIState *s, UHCIAsync *async)
  146. {
  147. qemu_free(async);
  148. }
  149. static void uhci_async_link(UHCIState *s, UHCIAsync *async)
  150. {
  151. async->next = s->async_pending;
  152. s->async_pending = async;
  153. }
  154. static void uhci_async_unlink(UHCIState *s, UHCIAsync *async)
  155. {
  156. UHCIAsync *curr = s->async_pending;
  157. UHCIAsync **prev = &s->async_pending;
  158. while (curr) {
  159. if (curr == async) {
  160. *prev = curr->next;
  161. return;
  162. }
  163. prev = &curr->next;
  164. curr = curr->next;
  165. }
  166. }
  167. static void uhci_async_cancel(UHCIState *s, UHCIAsync *async)
  168. {
  169. dprintf("uhci: cancel td 0x%x token 0x%x done %u\n",
  170. async->td, async->token, async->done);
  171. if (!async->done)
  172. usb_cancel_packet(&async->packet);
  173. uhci_async_free(s, async);
  174. }
  175. /*
  176. * Mark all outstanding async packets as invalid.
  177. * This is used for canceling them when TDs are removed by the HCD.
  178. */
  179. static UHCIAsync *uhci_async_validate_begin(UHCIState *s)
  180. {
  181. UHCIAsync *async = s->async_pending;
  182. while (async) {
  183. async->valid--;
  184. async = async->next;
  185. }
  186. return NULL;
  187. }
  188. /*
  189. * Cancel async packets that are no longer valid
  190. */
  191. static void uhci_async_validate_end(UHCIState *s)
  192. {
  193. UHCIAsync *curr = s->async_pending;
  194. UHCIAsync **prev = &s->async_pending;
  195. UHCIAsync *next;
  196. while (curr) {
  197. if (curr->valid > 0) {
  198. prev = &curr->next;
  199. curr = curr->next;
  200. continue;
  201. }
  202. next = curr->next;
  203. /* Unlink */
  204. *prev = next;
  205. uhci_async_cancel(s, curr);
  206. curr = next;
  207. }
  208. }
  209. static void uhci_async_cancel_all(UHCIState *s)
  210. {
  211. UHCIAsync *curr = s->async_pending;
  212. UHCIAsync *next;
  213. while (curr) {
  214. next = curr->next;
  215. uhci_async_cancel(s, curr);
  216. curr = next;
  217. }
  218. s->async_pending = NULL;
  219. }
  220. static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t addr, uint32_t token)
  221. {
  222. UHCIAsync *async = s->async_pending;
  223. UHCIAsync *match = NULL;
  224. int count = 0;
  225. /*
  226. * We're looking for the best match here. ie both td addr and token.
  227. * Otherwise we return last good match. ie just token.
  228. * It's ok to match just token because it identifies the transaction
  229. * rather well, token includes: device addr, endpoint, size, etc.
  230. *
  231. * Also since we queue async transactions in reverse order by returning
  232. * last good match we restores the order.
  233. *
  234. * It's expected that we wont have a ton of outstanding transactions.
  235. * If we ever do we'd want to optimize this algorithm.
  236. */
  237. while (async) {
  238. if (async->token == token) {
  239. /* Good match */
  240. match = async;
  241. if (async->td == addr) {
  242. /* Best match */
  243. break;
  244. }
  245. }
  246. async = async->next;
  247. count++;
  248. }
  249. if (count > 64)
  250. fprintf(stderr, "uhci: warning lots of async transactions\n");
  251. return match;
  252. }
  253. static void uhci_attach(USBPort *port1, USBDevice *dev);
  254. static void uhci_update_irq(UHCIState *s)
  255. {
  256. int level;
  257. if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
  258. ((s->status2 & 2) && (s->intr & (1 << 3))) ||
  259. ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
  260. ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
  261. (s->status & UHCI_STS_HSERR) ||
  262. (s->status & UHCI_STS_HCPERR)) {
  263. level = 1;
  264. } else {
  265. level = 0;
  266. }
  267. qemu_set_irq(s->dev.irq[3], level);
  268. }
  269. static void uhci_reset(UHCIState *s)
  270. {
  271. uint8_t *pci_conf;
  272. int i;
  273. UHCIPort *port;
  274. dprintf("uhci: full reset\n");
  275. pci_conf = s->dev.config;
  276. pci_conf[0x6a] = 0x01; /* usb clock */
  277. pci_conf[0x6b] = 0x00;
  278. s->cmd = 0;
  279. s->status = 0;
  280. s->status2 = 0;
  281. s->intr = 0;
  282. s->fl_base_addr = 0;
  283. s->sof_timing = 64;
  284. for(i = 0; i < NB_PORTS; i++) {
  285. port = &s->ports[i];
  286. port->ctrl = 0x0080;
  287. if (port->port.dev)
  288. uhci_attach(&port->port, port->port.dev);
  289. }
  290. uhci_async_cancel_all(s);
  291. }
  292. static void uhci_save(QEMUFile *f, void *opaque)
  293. {
  294. UHCIState *s = opaque;
  295. uint8_t num_ports = NB_PORTS;
  296. int i;
  297. uhci_async_cancel_all(s);
  298. pci_device_save(&s->dev, f);
  299. qemu_put_8s(f, &num_ports);
  300. for (i = 0; i < num_ports; ++i)
  301. qemu_put_be16s(f, &s->ports[i].ctrl);
  302. qemu_put_be16s(f, &s->cmd);
  303. qemu_put_be16s(f, &s->status);
  304. qemu_put_be16s(f, &s->intr);
  305. qemu_put_be16s(f, &s->frnum);
  306. qemu_put_be32s(f, &s->fl_base_addr);
  307. qemu_put_8s(f, &s->sof_timing);
  308. qemu_put_8s(f, &s->status2);
  309. qemu_put_timer(f, s->frame_timer);
  310. }
  311. static int uhci_load(QEMUFile *f, void *opaque, int version_id)
  312. {
  313. UHCIState *s = opaque;
  314. uint8_t num_ports;
  315. int i, ret;
  316. if (version_id > 1)
  317. return -EINVAL;
  318. ret = pci_device_load(&s->dev, f);
  319. if (ret < 0)
  320. return ret;
  321. qemu_get_8s(f, &num_ports);
  322. if (num_ports != NB_PORTS)
  323. return -EINVAL;
  324. for (i = 0; i < num_ports; ++i)
  325. qemu_get_be16s(f, &s->ports[i].ctrl);
  326. qemu_get_be16s(f, &s->cmd);
  327. qemu_get_be16s(f, &s->status);
  328. qemu_get_be16s(f, &s->intr);
  329. qemu_get_be16s(f, &s->frnum);
  330. qemu_get_be32s(f, &s->fl_base_addr);
  331. qemu_get_8s(f, &s->sof_timing);
  332. qemu_get_8s(f, &s->status2);
  333. qemu_get_timer(f, s->frame_timer);
  334. return 0;
  335. }
  336. static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  337. {
  338. UHCIState *s = opaque;
  339. addr &= 0x1f;
  340. switch(addr) {
  341. case 0x0c:
  342. s->sof_timing = val;
  343. break;
  344. }
  345. }
  346. static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
  347. {
  348. UHCIState *s = opaque;
  349. uint32_t val;
  350. addr &= 0x1f;
  351. switch(addr) {
  352. case 0x0c:
  353. val = s->sof_timing;
  354. break;
  355. default:
  356. val = 0xff;
  357. break;
  358. }
  359. return val;
  360. }
  361. static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  362. {
  363. UHCIState *s = opaque;
  364. addr &= 0x1f;
  365. dprintf("uhci: writew port=0x%04x val=0x%04x\n", addr, val);
  366. switch(addr) {
  367. case 0x00:
  368. if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
  369. /* start frame processing */
  370. qemu_mod_timer(s->frame_timer, qemu_get_clock(vm_clock));
  371. s->status &= ~UHCI_STS_HCHALTED;
  372. } else if (!(val & UHCI_CMD_RS)) {
  373. s->status |= UHCI_STS_HCHALTED;
  374. }
  375. if (val & UHCI_CMD_GRESET) {
  376. UHCIPort *port;
  377. USBDevice *dev;
  378. int i;
  379. /* send reset on the USB bus */
  380. for(i = 0; i < NB_PORTS; i++) {
  381. port = &s->ports[i];
  382. dev = port->port.dev;
  383. if (dev) {
  384. usb_send_msg(dev, USB_MSG_RESET);
  385. }
  386. }
  387. uhci_reset(s);
  388. return;
  389. }
  390. if (val & UHCI_CMD_HCRESET) {
  391. uhci_reset(s);
  392. return;
  393. }
  394. s->cmd = val;
  395. break;
  396. case 0x02:
  397. s->status &= ~val;
  398. /* XXX: the chip spec is not coherent, so we add a hidden
  399. register to distinguish between IOC and SPD */
  400. if (val & UHCI_STS_USBINT)
  401. s->status2 = 0;
  402. uhci_update_irq(s);
  403. break;
  404. case 0x04:
  405. s->intr = val;
  406. uhci_update_irq(s);
  407. break;
  408. case 0x06:
  409. if (s->status & UHCI_STS_HCHALTED)
  410. s->frnum = val & 0x7ff;
  411. break;
  412. case 0x10 ... 0x1f:
  413. {
  414. UHCIPort *port;
  415. USBDevice *dev;
  416. int n;
  417. n = (addr >> 1) & 7;
  418. if (n >= NB_PORTS)
  419. return;
  420. port = &s->ports[n];
  421. dev = port->port.dev;
  422. if (dev) {
  423. /* port reset */
  424. if ( (val & UHCI_PORT_RESET) &&
  425. !(port->ctrl & UHCI_PORT_RESET) ) {
  426. usb_send_msg(dev, USB_MSG_RESET);
  427. }
  428. }
  429. port->ctrl = (port->ctrl & 0x01fb) | (val & ~0x01fb);
  430. /* some bits are reset when a '1' is written to them */
  431. port->ctrl &= ~(val & 0x000a);
  432. }
  433. break;
  434. }
  435. }
  436. static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
  437. {
  438. UHCIState *s = opaque;
  439. uint32_t val;
  440. addr &= 0x1f;
  441. switch(addr) {
  442. case 0x00:
  443. val = s->cmd;
  444. break;
  445. case 0x02:
  446. val = s->status;
  447. break;
  448. case 0x04:
  449. val = s->intr;
  450. break;
  451. case 0x06:
  452. val = s->frnum;
  453. break;
  454. case 0x10 ... 0x1f:
  455. {
  456. UHCIPort *port;
  457. int n;
  458. n = (addr >> 1) & 7;
  459. if (n >= NB_PORTS)
  460. goto read_default;
  461. port = &s->ports[n];
  462. val = port->ctrl;
  463. }
  464. break;
  465. default:
  466. read_default:
  467. val = 0xff7f; /* disabled port */
  468. break;
  469. }
  470. dprintf("uhci: readw port=0x%04x val=0x%04x\n", addr, val);
  471. return val;
  472. }
  473. static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
  474. {
  475. UHCIState *s = opaque;
  476. addr &= 0x1f;
  477. dprintf("uhci: writel port=0x%04x val=0x%08x\n", addr, val);
  478. switch(addr) {
  479. case 0x08:
  480. s->fl_base_addr = val & ~0xfff;
  481. break;
  482. }
  483. }
  484. static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
  485. {
  486. UHCIState *s = opaque;
  487. uint32_t val;
  488. addr &= 0x1f;
  489. switch(addr) {
  490. case 0x08:
  491. val = s->fl_base_addr;
  492. break;
  493. default:
  494. val = 0xffffffff;
  495. break;
  496. }
  497. return val;
  498. }
  499. /* signal resume if controller suspended */
  500. static void uhci_resume (void *opaque)
  501. {
  502. UHCIState *s = (UHCIState *)opaque;
  503. if (!s)
  504. return;
  505. if (s->cmd & UHCI_CMD_EGSM) {
  506. s->cmd |= UHCI_CMD_FGR;
  507. s->status |= UHCI_STS_RD;
  508. uhci_update_irq(s);
  509. }
  510. }
  511. static void uhci_attach(USBPort *port1, USBDevice *dev)
  512. {
  513. UHCIState *s = port1->opaque;
  514. UHCIPort *port = &s->ports[port1->index];
  515. if (dev) {
  516. if (port->port.dev) {
  517. usb_attach(port1, NULL);
  518. }
  519. /* set connect status */
  520. port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
  521. /* update speed */
  522. if (dev->speed == USB_SPEED_LOW)
  523. port->ctrl |= UHCI_PORT_LSDA;
  524. else
  525. port->ctrl &= ~UHCI_PORT_LSDA;
  526. uhci_resume(s);
  527. port->port.dev = dev;
  528. /* send the attach message */
  529. usb_send_msg(dev, USB_MSG_ATTACH);
  530. } else {
  531. /* set connect status */
  532. if (port->ctrl & UHCI_PORT_CCS) {
  533. port->ctrl &= ~UHCI_PORT_CCS;
  534. port->ctrl |= UHCI_PORT_CSC;
  535. }
  536. /* disable port */
  537. if (port->ctrl & UHCI_PORT_EN) {
  538. port->ctrl &= ~UHCI_PORT_EN;
  539. port->ctrl |= UHCI_PORT_ENC;
  540. }
  541. uhci_resume(s);
  542. dev = port->port.dev;
  543. if (dev) {
  544. /* send the detach message */
  545. usb_send_msg(dev, USB_MSG_DETACH);
  546. }
  547. port->port.dev = NULL;
  548. }
  549. }
  550. static int uhci_broadcast_packet(UHCIState *s, USBPacket *p)
  551. {
  552. int i, ret;
  553. dprintf("uhci: packet enter. pid %s addr 0x%02x ep %d len %d\n",
  554. pid2str(p->pid), p->devaddr, p->devep, p->len);
  555. if (p->pid == USB_TOKEN_OUT || p->pid == USB_TOKEN_SETUP)
  556. dump_data(p->data, p->len);
  557. ret = USB_RET_NODEV;
  558. for (i = 0; i < NB_PORTS && ret == USB_RET_NODEV; i++) {
  559. UHCIPort *port = &s->ports[i];
  560. USBDevice *dev = port->port.dev;
  561. if (dev && (port->ctrl & UHCI_PORT_EN))
  562. ret = dev->handle_packet(dev, p);
  563. }
  564. dprintf("uhci: packet exit. ret %d len %d\n", ret, p->len);
  565. if (p->pid == USB_TOKEN_IN && ret > 0)
  566. dump_data(p->data, ret);
  567. return ret;
  568. }
  569. static void uhci_async_complete(USBPacket * packet, void *opaque);
  570. static void uhci_process_frame(UHCIState *s);
  571. /* return -1 if fatal error (frame must be stopped)
  572. 0 if TD successful
  573. 1 if TD unsuccessful or inactive
  574. */
  575. static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
  576. {
  577. int len = 0, max_len, err, ret;
  578. uint8_t pid;
  579. max_len = ((td->token >> 21) + 1) & 0x7ff;
  580. pid = td->token & 0xff;
  581. ret = async->packet.len;
  582. if (td->ctrl & TD_CTRL_IOC)
  583. *int_mask |= 0x01;
  584. if (td->ctrl & TD_CTRL_IOS)
  585. td->ctrl &= ~TD_CTRL_ACTIVE;
  586. if (ret < 0)
  587. goto out;
  588. len = async->packet.len;
  589. td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
  590. /* The NAK bit may have been set by a previous frame, so clear it
  591. here. The docs are somewhat unclear, but win2k relies on this
  592. behavior. */
  593. td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
  594. if (pid == USB_TOKEN_IN) {
  595. if (len > max_len) {
  596. len = max_len;
  597. ret = USB_RET_BABBLE;
  598. goto out;
  599. }
  600. if (len > 0) {
  601. /* write the data back */
  602. cpu_physical_memory_write(td->buffer, async->buffer, len);
  603. }
  604. if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
  605. *int_mask |= 0x02;
  606. /* short packet: do not update QH */
  607. dprintf("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token);
  608. return 1;
  609. }
  610. }
  611. /* success */
  612. return 0;
  613. out:
  614. switch(ret) {
  615. case USB_RET_STALL:
  616. td->ctrl |= TD_CTRL_STALL;
  617. td->ctrl &= ~TD_CTRL_ACTIVE;
  618. return 1;
  619. case USB_RET_BABBLE:
  620. td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
  621. td->ctrl &= ~TD_CTRL_ACTIVE;
  622. /* frame interrupted */
  623. return -1;
  624. case USB_RET_NAK:
  625. td->ctrl |= TD_CTRL_NAK;
  626. if (pid == USB_TOKEN_SETUP)
  627. break;
  628. return 1;
  629. case USB_RET_NODEV:
  630. default:
  631. break;
  632. }
  633. /* Retry the TD if error count is not zero */
  634. td->ctrl |= TD_CTRL_TIMEOUT;
  635. err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
  636. if (err != 0) {
  637. err--;
  638. if (err == 0) {
  639. td->ctrl &= ~TD_CTRL_ACTIVE;
  640. s->status |= UHCI_STS_USBERR;
  641. uhci_update_irq(s);
  642. }
  643. }
  644. td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
  645. (err << TD_CTRL_ERROR_SHIFT);
  646. return 1;
  647. }
  648. static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask)
  649. {
  650. UHCIAsync *async;
  651. int len = 0, max_len;
  652. uint8_t pid;
  653. /* Is active ? */
  654. if (!(td->ctrl & TD_CTRL_ACTIVE))
  655. return 1;
  656. async = uhci_async_find_td(s, addr, td->token);
  657. if (async) {
  658. /* Already submitted */
  659. async->valid = 32;
  660. if (!async->done)
  661. return 1;
  662. uhci_async_unlink(s, async);
  663. goto done;
  664. }
  665. /* Allocate new packet */
  666. async = uhci_async_alloc(s);
  667. if (!async)
  668. return 1;
  669. async->valid = 10;
  670. async->td = addr;
  671. async->token = td->token;
  672. max_len = ((td->token >> 21) + 1) & 0x7ff;
  673. pid = td->token & 0xff;
  674. async->packet.pid = pid;
  675. async->packet.devaddr = (td->token >> 8) & 0x7f;
  676. async->packet.devep = (td->token >> 15) & 0xf;
  677. async->packet.data = async->buffer;
  678. async->packet.len = max_len;
  679. async->packet.complete_cb = uhci_async_complete;
  680. async->packet.complete_opaque = s;
  681. switch(pid) {
  682. case USB_TOKEN_OUT:
  683. case USB_TOKEN_SETUP:
  684. cpu_physical_memory_read(td->buffer, async->buffer, max_len);
  685. len = uhci_broadcast_packet(s, &async->packet);
  686. if (len >= 0)
  687. len = max_len;
  688. break;
  689. case USB_TOKEN_IN:
  690. len = uhci_broadcast_packet(s, &async->packet);
  691. break;
  692. default:
  693. /* invalid pid : frame interrupted */
  694. uhci_async_free(s, async);
  695. s->status |= UHCI_STS_HCPERR;
  696. uhci_update_irq(s);
  697. return -1;
  698. }
  699. if (len == USB_RET_ASYNC) {
  700. uhci_async_link(s, async);
  701. return 2;
  702. }
  703. async->packet.len = len;
  704. done:
  705. len = uhci_complete_td(s, td, async, int_mask);
  706. uhci_async_free(s, async);
  707. return len;
  708. }
  709. static void uhci_async_complete(USBPacket *packet, void *opaque)
  710. {
  711. UHCIState *s = opaque;
  712. UHCIAsync *async = (UHCIAsync *) packet;
  713. dprintf("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token);
  714. async->done = 1;
  715. uhci_process_frame(s);
  716. }
  717. static int is_valid(uint32_t link)
  718. {
  719. return (link & 1) == 0;
  720. }
  721. static int is_qh(uint32_t link)
  722. {
  723. return (link & 2) != 0;
  724. }
  725. static int depth_first(uint32_t link)
  726. {
  727. return (link & 4) != 0;
  728. }
  729. /* QH DB used for detecting QH loops */
  730. #define UHCI_MAX_QUEUES 128
  731. typedef struct {
  732. uint32_t addr[UHCI_MAX_QUEUES];
  733. int count;
  734. } QhDb;
  735. static void qhdb_reset(QhDb *db)
  736. {
  737. db->count = 0;
  738. }
  739. /* Add QH to DB. Returns 1 if already present or DB is full. */
  740. static int qhdb_insert(QhDb *db, uint32_t addr)
  741. {
  742. int i;
  743. for (i = 0; i < db->count; i++)
  744. if (db->addr[i] == addr)
  745. return 1;
  746. if (db->count >= UHCI_MAX_QUEUES)
  747. return 1;
  748. db->addr[db->count++] = addr;
  749. return 0;
  750. }
  751. static void uhci_process_frame(UHCIState *s)
  752. {
  753. uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
  754. uint32_t curr_qh;
  755. int cnt, ret;
  756. UHCI_TD td;
  757. UHCI_QH qh;
  758. QhDb qhdb;
  759. frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
  760. dprintf("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr);
  761. cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4);
  762. le32_to_cpus(&link);
  763. int_mask = 0;
  764. curr_qh = 0;
  765. qhdb_reset(&qhdb);
  766. for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
  767. if (is_qh(link)) {
  768. /* QH */
  769. if (qhdb_insert(&qhdb, link)) {
  770. /*
  771. * We're going in circles. Which is not a bug because
  772. * HCD is allowed to do that as part of the BW management.
  773. * In our case though it makes no sense to spin here. Sync transations
  774. * are already done, and async completion handler will re-process
  775. * the frame when something is ready.
  776. */
  777. dprintf("uhci: detected loop. qh 0x%x\n", link);
  778. break;
  779. }
  780. cpu_physical_memory_read(link & ~0xf, (uint8_t *) &qh, sizeof(qh));
  781. le32_to_cpus(&qh.link);
  782. le32_to_cpus(&qh.el_link);
  783. dprintf("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
  784. link, qh.link, qh.el_link);
  785. if (!is_valid(qh.el_link)) {
  786. /* QH w/o elements */
  787. curr_qh = 0;
  788. link = qh.link;
  789. } else {
  790. /* QH with elements */
  791. curr_qh = link;
  792. link = qh.el_link;
  793. }
  794. continue;
  795. }
  796. /* TD */
  797. cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td));
  798. le32_to_cpus(&td.link);
  799. le32_to_cpus(&td.ctrl);
  800. le32_to_cpus(&td.token);
  801. le32_to_cpus(&td.buffer);
  802. dprintf("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
  803. link, td.link, td.ctrl, td.token, curr_qh);
  804. old_td_ctrl = td.ctrl;
  805. ret = uhci_handle_td(s, link, &td, &int_mask);
  806. if (old_td_ctrl != td.ctrl) {
  807. /* update the status bits of the TD */
  808. val = cpu_to_le32(td.ctrl);
  809. cpu_physical_memory_write((link & ~0xf) + 4,
  810. (const uint8_t *)&val, sizeof(val));
  811. }
  812. if (ret < 0) {
  813. /* interrupted frame */
  814. break;
  815. }
  816. if (ret == 2 || ret == 1) {
  817. dprintf("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
  818. link, ret == 2 ? "pend" : "skip",
  819. td.link, td.ctrl, td.token, curr_qh);
  820. link = curr_qh ? qh.link : td.link;
  821. continue;
  822. }
  823. /* completed TD */
  824. dprintf("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
  825. link, td.link, td.ctrl, td.token, curr_qh);
  826. link = td.link;
  827. if (curr_qh) {
  828. /* update QH element link */
  829. qh.el_link = link;
  830. val = cpu_to_le32(qh.el_link);
  831. cpu_physical_memory_write((curr_qh & ~0xf) + 4,
  832. (const uint8_t *)&val, sizeof(val));
  833. if (!depth_first(link)) {
  834. /* done with this QH */
  835. dprintf("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
  836. curr_qh, qh.link, qh.el_link);
  837. curr_qh = 0;
  838. link = qh.link;
  839. }
  840. }
  841. /* go to the next entry */
  842. }
  843. s->pending_int_mask = int_mask;
  844. }
  845. static void uhci_frame_timer(void *opaque)
  846. {
  847. UHCIState *s = opaque;
  848. int64_t expire_time;
  849. if (!(s->cmd & UHCI_CMD_RS)) {
  850. /* Full stop */
  851. qemu_del_timer(s->frame_timer);
  852. /* set hchalted bit in status - UHCI11D 2.1.2 */
  853. s->status |= UHCI_STS_HCHALTED;
  854. dprintf("uhci: halted\n");
  855. return;
  856. }
  857. /* Complete the previous frame */
  858. if (s->pending_int_mask) {
  859. s->status2 |= s->pending_int_mask;
  860. s->status |= UHCI_STS_USBINT;
  861. uhci_update_irq(s);
  862. }
  863. /* Start new frame */
  864. s->frnum = (s->frnum + 1) & 0x7ff;
  865. dprintf("uhci: new frame #%u\n" , s->frnum);
  866. uhci_async_validate_begin(s);
  867. uhci_process_frame(s);
  868. uhci_async_validate_end(s);
  869. /* prepare the timer for the next frame */
  870. expire_time = qemu_get_clock(vm_clock) +
  871. (ticks_per_sec / FRAME_TIMER_FREQ);
  872. qemu_mod_timer(s->frame_timer, expire_time);
  873. }
  874. static void uhci_map(PCIDevice *pci_dev, int region_num,
  875. uint32_t addr, uint32_t size, int type)
  876. {
  877. UHCIState *s = (UHCIState *)pci_dev;
  878. register_ioport_write(addr, 32, 2, uhci_ioport_writew, s);
  879. register_ioport_read(addr, 32, 2, uhci_ioport_readw, s);
  880. register_ioport_write(addr, 32, 4, uhci_ioport_writel, s);
  881. register_ioport_read(addr, 32, 4, uhci_ioport_readl, s);
  882. register_ioport_write(addr, 32, 1, uhci_ioport_writeb, s);
  883. register_ioport_read(addr, 32, 1, uhci_ioport_readb, s);
  884. }
  885. void usb_uhci_piix3_init(PCIBus *bus, int devfn)
  886. {
  887. UHCIState *s;
  888. uint8_t *pci_conf;
  889. int i;
  890. s = (UHCIState *)pci_register_device(bus,
  891. "USB-UHCI", sizeof(UHCIState),
  892. devfn, NULL, NULL);
  893. pci_conf = s->dev.config;
  894. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  895. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_2);
  896. pci_conf[0x08] = 0x01; // revision number
  897. pci_conf[0x09] = 0x00;
  898. pci_config_set_class(pci_conf, PCI_CLASS_SERIAL_USB);
  899. pci_conf[0x0e] = 0x00; // header_type
  900. pci_conf[0x3d] = 4; // interrupt pin 3
  901. pci_conf[0x60] = 0x10; // release number
  902. for(i = 0; i < NB_PORTS; i++) {
  903. qemu_register_usb_port(&s->ports[i].port, s, i, uhci_attach);
  904. }
  905. s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
  906. uhci_reset(s);
  907. /* Use region 4 for consistency with real hardware. BSD guests seem
  908. to rely on this. */
  909. pci_register_io_region(&s->dev, 4, 0x20,
  910. PCI_ADDRESS_SPACE_IO, uhci_map);
  911. register_savevm("uhci", 0, 1, uhci_save, uhci_load, s);
  912. }
  913. void usb_uhci_piix4_init(PCIBus *bus, int devfn)
  914. {
  915. UHCIState *s;
  916. uint8_t *pci_conf;
  917. int i;
  918. s = (UHCIState *)pci_register_device(bus,
  919. "USB-UHCI", sizeof(UHCIState),
  920. devfn, NULL, NULL);
  921. pci_conf = s->dev.config;
  922. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
  923. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_2);
  924. pci_conf[0x08] = 0x01; // revision number
  925. pci_conf[0x09] = 0x00;
  926. pci_config_set_class(pci_conf, PCI_CLASS_SERIAL_USB);
  927. pci_conf[0x0e] = 0x00; // header_type
  928. pci_conf[0x3d] = 4; // interrupt pin 3
  929. pci_conf[0x60] = 0x10; // release number
  930. for(i = 0; i < NB_PORTS; i++) {
  931. qemu_register_usb_port(&s->ports[i].port, s, i, uhci_attach);
  932. }
  933. s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
  934. uhci_reset(s);
  935. /* Use region 4 for consistency with real hardware. BSD guests seem
  936. to rely on this. */
  937. pci_register_io_region(&s->dev, 4, 0x20,
  938. PCI_ADDRESS_SPACE_IO, uhci_map);
  939. register_savevm("uhci", 0, 1, uhci_save, uhci_load, s);
  940. }