hcd-uhci.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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/hw.h"
  29. #include "hw/usb.h"
  30. #include "hw/pci/pci.h"
  31. #include "qemu/timer.h"
  32. #include "qemu/iov.h"
  33. #include "sysemu/dma.h"
  34. #include "trace.h"
  35. //#define DEBUG
  36. //#define DEBUG_DUMP_DATA
  37. #define UHCI_CMD_FGR (1 << 4)
  38. #define UHCI_CMD_EGSM (1 << 3)
  39. #define UHCI_CMD_GRESET (1 << 2)
  40. #define UHCI_CMD_HCRESET (1 << 1)
  41. #define UHCI_CMD_RS (1 << 0)
  42. #define UHCI_STS_HCHALTED (1 << 5)
  43. #define UHCI_STS_HCPERR (1 << 4)
  44. #define UHCI_STS_HSERR (1 << 3)
  45. #define UHCI_STS_RD (1 << 2)
  46. #define UHCI_STS_USBERR (1 << 1)
  47. #define UHCI_STS_USBINT (1 << 0)
  48. #define TD_CTRL_SPD (1 << 29)
  49. #define TD_CTRL_ERROR_SHIFT 27
  50. #define TD_CTRL_IOS (1 << 25)
  51. #define TD_CTRL_IOC (1 << 24)
  52. #define TD_CTRL_ACTIVE (1 << 23)
  53. #define TD_CTRL_STALL (1 << 22)
  54. #define TD_CTRL_BABBLE (1 << 20)
  55. #define TD_CTRL_NAK (1 << 19)
  56. #define TD_CTRL_TIMEOUT (1 << 18)
  57. #define UHCI_PORT_SUSPEND (1 << 12)
  58. #define UHCI_PORT_RESET (1 << 9)
  59. #define UHCI_PORT_LSDA (1 << 8)
  60. #define UHCI_PORT_RD (1 << 6)
  61. #define UHCI_PORT_ENC (1 << 3)
  62. #define UHCI_PORT_EN (1 << 2)
  63. #define UHCI_PORT_CSC (1 << 1)
  64. #define UHCI_PORT_CCS (1 << 0)
  65. #define UHCI_PORT_READ_ONLY (0x1bb)
  66. #define UHCI_PORT_WRITE_CLEAR (UHCI_PORT_CSC | UHCI_PORT_ENC)
  67. #define FRAME_TIMER_FREQ 1000
  68. #define FRAME_MAX_LOOPS 256
  69. /* Must be large enough to handle 10 frame delay for initial isoc requests */
  70. #define QH_VALID 32
  71. #define MAX_FRAMES_PER_TICK (QH_VALID / 2)
  72. #define NB_PORTS 2
  73. enum {
  74. TD_RESULT_STOP_FRAME = 10,
  75. TD_RESULT_COMPLETE,
  76. TD_RESULT_NEXT_QH,
  77. TD_RESULT_ASYNC_START,
  78. TD_RESULT_ASYNC_CONT,
  79. };
  80. typedef struct UHCIState UHCIState;
  81. typedef struct UHCIAsync UHCIAsync;
  82. typedef struct UHCIQueue UHCIQueue;
  83. typedef struct UHCIInfo UHCIInfo;
  84. typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass;
  85. struct UHCIInfo {
  86. const char *name;
  87. uint16_t vendor_id;
  88. uint16_t device_id;
  89. uint8_t revision;
  90. uint8_t irq_pin;
  91. int (*initfn)(PCIDevice *dev);
  92. bool unplug;
  93. };
  94. struct UHCIPCIDeviceClass {
  95. PCIDeviceClass parent_class;
  96. UHCIInfo info;
  97. };
  98. /*
  99. * Pending async transaction.
  100. * 'packet' must be the first field because completion
  101. * handler does "(UHCIAsync *) pkt" cast.
  102. */
  103. struct UHCIAsync {
  104. USBPacket packet;
  105. QEMUSGList sgl;
  106. UHCIQueue *queue;
  107. QTAILQ_ENTRY(UHCIAsync) next;
  108. uint32_t td_addr;
  109. uint8_t done;
  110. };
  111. struct UHCIQueue {
  112. uint32_t qh_addr;
  113. uint32_t token;
  114. UHCIState *uhci;
  115. USBEndpoint *ep;
  116. QTAILQ_ENTRY(UHCIQueue) next;
  117. QTAILQ_HEAD(asyncs_head, UHCIAsync) asyncs;
  118. int8_t valid;
  119. };
  120. typedef struct UHCIPort {
  121. USBPort port;
  122. uint16_t ctrl;
  123. } UHCIPort;
  124. struct UHCIState {
  125. PCIDevice dev;
  126. MemoryRegion io_bar;
  127. USBBus bus; /* Note unused when we're a companion controller */
  128. uint16_t cmd; /* cmd register */
  129. uint16_t status;
  130. uint16_t intr; /* interrupt enable register */
  131. uint16_t frnum; /* frame number */
  132. uint32_t fl_base_addr; /* frame list base address */
  133. uint8_t sof_timing;
  134. uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
  135. int64_t expire_time;
  136. QEMUTimer *frame_timer;
  137. QEMUBH *bh;
  138. uint32_t frame_bytes;
  139. uint32_t frame_bandwidth;
  140. bool completions_only;
  141. UHCIPort ports[NB_PORTS];
  142. /* Interrupts that should be raised at the end of the current frame. */
  143. uint32_t pending_int_mask;
  144. int irq_pin;
  145. /* Active packets */
  146. QTAILQ_HEAD(, UHCIQueue) queues;
  147. uint8_t num_ports_vmstate;
  148. /* Properties */
  149. char *masterbus;
  150. uint32_t firstport;
  151. uint32_t maxframes;
  152. };
  153. typedef struct UHCI_TD {
  154. uint32_t link;
  155. uint32_t ctrl; /* see TD_CTRL_xxx */
  156. uint32_t token;
  157. uint32_t buffer;
  158. } UHCI_TD;
  159. typedef struct UHCI_QH {
  160. uint32_t link;
  161. uint32_t el_link;
  162. } UHCI_QH;
  163. static void uhci_async_cancel(UHCIAsync *async);
  164. static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
  165. static inline int32_t uhci_queue_token(UHCI_TD *td)
  166. {
  167. if ((td->token & (0xf << 15)) == 0) {
  168. /* ctrl ep, cover ep and dev, not pid! */
  169. return td->token & 0x7ff00;
  170. } else {
  171. /* covers ep, dev, pid -> identifies the endpoint */
  172. return td->token & 0x7ffff;
  173. }
  174. }
  175. static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td,
  176. USBEndpoint *ep)
  177. {
  178. UHCIQueue *queue;
  179. queue = g_new0(UHCIQueue, 1);
  180. queue->uhci = s;
  181. queue->qh_addr = qh_addr;
  182. queue->token = uhci_queue_token(td);
  183. queue->ep = ep;
  184. QTAILQ_INIT(&queue->asyncs);
  185. QTAILQ_INSERT_HEAD(&s->queues, queue, next);
  186. queue->valid = QH_VALID;
  187. trace_usb_uhci_queue_add(queue->token);
  188. return queue;
  189. }
  190. static void uhci_queue_free(UHCIQueue *queue, const char *reason)
  191. {
  192. UHCIState *s = queue->uhci;
  193. UHCIAsync *async;
  194. while (!QTAILQ_EMPTY(&queue->asyncs)) {
  195. async = QTAILQ_FIRST(&queue->asyncs);
  196. uhci_async_cancel(async);
  197. }
  198. usb_device_ep_stopped(queue->ep->dev, queue->ep);
  199. trace_usb_uhci_queue_del(queue->token, reason);
  200. QTAILQ_REMOVE(&s->queues, queue, next);
  201. g_free(queue);
  202. }
  203. static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td)
  204. {
  205. uint32_t token = uhci_queue_token(td);
  206. UHCIQueue *queue;
  207. QTAILQ_FOREACH(queue, &s->queues, next) {
  208. if (queue->token == token) {
  209. return queue;
  210. }
  211. }
  212. return NULL;
  213. }
  214. static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td,
  215. uint32_t td_addr, bool queuing)
  216. {
  217. UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs);
  218. return queue->qh_addr == qh_addr &&
  219. queue->token == uhci_queue_token(td) &&
  220. (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL ||
  221. first->td_addr == td_addr);
  222. }
  223. static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
  224. {
  225. UHCIAsync *async = g_new0(UHCIAsync, 1);
  226. async->queue = queue;
  227. async->td_addr = td_addr;
  228. usb_packet_init(&async->packet);
  229. pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
  230. trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
  231. return async;
  232. }
  233. static void uhci_async_free(UHCIAsync *async)
  234. {
  235. trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
  236. usb_packet_cleanup(&async->packet);
  237. qemu_sglist_destroy(&async->sgl);
  238. g_free(async);
  239. }
  240. static void uhci_async_link(UHCIAsync *async)
  241. {
  242. UHCIQueue *queue = async->queue;
  243. QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
  244. trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
  245. }
  246. static void uhci_async_unlink(UHCIAsync *async)
  247. {
  248. UHCIQueue *queue = async->queue;
  249. QTAILQ_REMOVE(&queue->asyncs, async, next);
  250. trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
  251. }
  252. static void uhci_async_cancel(UHCIAsync *async)
  253. {
  254. uhci_async_unlink(async);
  255. trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
  256. async->done);
  257. if (!async->done)
  258. usb_cancel_packet(&async->packet);
  259. usb_packet_unmap(&async->packet, &async->sgl);
  260. uhci_async_free(async);
  261. }
  262. /*
  263. * Mark all outstanding async packets as invalid.
  264. * This is used for canceling them when TDs are removed by the HCD.
  265. */
  266. static void uhci_async_validate_begin(UHCIState *s)
  267. {
  268. UHCIQueue *queue;
  269. QTAILQ_FOREACH(queue, &s->queues, next) {
  270. queue->valid--;
  271. }
  272. }
  273. /*
  274. * Cancel async packets that are no longer valid
  275. */
  276. static void uhci_async_validate_end(UHCIState *s)
  277. {
  278. UHCIQueue *queue, *n;
  279. QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
  280. if (!queue->valid) {
  281. uhci_queue_free(queue, "validate-end");
  282. }
  283. }
  284. }
  285. static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
  286. {
  287. UHCIQueue *queue, *n;
  288. QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
  289. if (queue->ep->dev == dev) {
  290. uhci_queue_free(queue, "cancel-device");
  291. }
  292. }
  293. }
  294. static void uhci_async_cancel_all(UHCIState *s)
  295. {
  296. UHCIQueue *queue, *nq;
  297. QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
  298. uhci_queue_free(queue, "cancel-all");
  299. }
  300. }
  301. static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr)
  302. {
  303. UHCIQueue *queue;
  304. UHCIAsync *async;
  305. QTAILQ_FOREACH(queue, &s->queues, next) {
  306. QTAILQ_FOREACH(async, &queue->asyncs, next) {
  307. if (async->td_addr == td_addr) {
  308. return async;
  309. }
  310. }
  311. }
  312. return NULL;
  313. }
  314. static void uhci_update_irq(UHCIState *s)
  315. {
  316. int level;
  317. if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
  318. ((s->status2 & 2) && (s->intr & (1 << 3))) ||
  319. ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
  320. ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
  321. (s->status & UHCI_STS_HSERR) ||
  322. (s->status & UHCI_STS_HCPERR)) {
  323. level = 1;
  324. } else {
  325. level = 0;
  326. }
  327. qemu_set_irq(s->dev.irq[s->irq_pin], level);
  328. }
  329. static void uhci_reset(void *opaque)
  330. {
  331. UHCIState *s = opaque;
  332. uint8_t *pci_conf;
  333. int i;
  334. UHCIPort *port;
  335. trace_usb_uhci_reset();
  336. pci_conf = s->dev.config;
  337. pci_conf[0x6a] = 0x01; /* usb clock */
  338. pci_conf[0x6b] = 0x00;
  339. s->cmd = 0;
  340. s->status = 0;
  341. s->status2 = 0;
  342. s->intr = 0;
  343. s->fl_base_addr = 0;
  344. s->sof_timing = 64;
  345. for(i = 0; i < NB_PORTS; i++) {
  346. port = &s->ports[i];
  347. port->ctrl = 0x0080;
  348. if (port->port.dev && port->port.dev->attached) {
  349. usb_port_reset(&port->port);
  350. }
  351. }
  352. uhci_async_cancel_all(s);
  353. qemu_bh_cancel(s->bh);
  354. uhci_update_irq(s);
  355. }
  356. static const VMStateDescription vmstate_uhci_port = {
  357. .name = "uhci port",
  358. .version_id = 1,
  359. .minimum_version_id = 1,
  360. .minimum_version_id_old = 1,
  361. .fields = (VMStateField []) {
  362. VMSTATE_UINT16(ctrl, UHCIPort),
  363. VMSTATE_END_OF_LIST()
  364. }
  365. };
  366. static int uhci_post_load(void *opaque, int version_id)
  367. {
  368. UHCIState *s = opaque;
  369. if (version_id < 2) {
  370. s->expire_time = qemu_get_clock_ns(vm_clock) +
  371. (get_ticks_per_sec() / FRAME_TIMER_FREQ);
  372. }
  373. return 0;
  374. }
  375. static const VMStateDescription vmstate_uhci = {
  376. .name = "uhci",
  377. .version_id = 3,
  378. .minimum_version_id = 1,
  379. .minimum_version_id_old = 1,
  380. .post_load = uhci_post_load,
  381. .fields = (VMStateField []) {
  382. VMSTATE_PCI_DEVICE(dev, UHCIState),
  383. VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
  384. VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
  385. vmstate_uhci_port, UHCIPort),
  386. VMSTATE_UINT16(cmd, UHCIState),
  387. VMSTATE_UINT16(status, UHCIState),
  388. VMSTATE_UINT16(intr, UHCIState),
  389. VMSTATE_UINT16(frnum, UHCIState),
  390. VMSTATE_UINT32(fl_base_addr, UHCIState),
  391. VMSTATE_UINT8(sof_timing, UHCIState),
  392. VMSTATE_UINT8(status2, UHCIState),
  393. VMSTATE_TIMER(frame_timer, UHCIState),
  394. VMSTATE_INT64_V(expire_time, UHCIState, 2),
  395. VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3),
  396. VMSTATE_END_OF_LIST()
  397. }
  398. };
  399. static void uhci_port_write(void *opaque, hwaddr addr,
  400. uint64_t val, unsigned size)
  401. {
  402. UHCIState *s = opaque;
  403. trace_usb_uhci_mmio_writew(addr, val);
  404. switch(addr) {
  405. case 0x00:
  406. if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
  407. /* start frame processing */
  408. trace_usb_uhci_schedule_start();
  409. s->expire_time = qemu_get_clock_ns(vm_clock) +
  410. (get_ticks_per_sec() / FRAME_TIMER_FREQ);
  411. qemu_mod_timer(s->frame_timer, s->expire_time);
  412. s->status &= ~UHCI_STS_HCHALTED;
  413. } else if (!(val & UHCI_CMD_RS)) {
  414. s->status |= UHCI_STS_HCHALTED;
  415. }
  416. if (val & UHCI_CMD_GRESET) {
  417. UHCIPort *port;
  418. int i;
  419. /* send reset on the USB bus */
  420. for(i = 0; i < NB_PORTS; i++) {
  421. port = &s->ports[i];
  422. usb_device_reset(port->port.dev);
  423. }
  424. uhci_reset(s);
  425. return;
  426. }
  427. if (val & UHCI_CMD_HCRESET) {
  428. uhci_reset(s);
  429. return;
  430. }
  431. s->cmd = val;
  432. break;
  433. case 0x02:
  434. s->status &= ~val;
  435. /* XXX: the chip spec is not coherent, so we add a hidden
  436. register to distinguish between IOC and SPD */
  437. if (val & UHCI_STS_USBINT)
  438. s->status2 = 0;
  439. uhci_update_irq(s);
  440. break;
  441. case 0x04:
  442. s->intr = val;
  443. uhci_update_irq(s);
  444. break;
  445. case 0x06:
  446. if (s->status & UHCI_STS_HCHALTED)
  447. s->frnum = val & 0x7ff;
  448. break;
  449. case 0x08:
  450. s->fl_base_addr &= 0xffff0000;
  451. s->fl_base_addr |= val & ~0xfff;
  452. break;
  453. case 0x0a:
  454. s->fl_base_addr &= 0x0000ffff;
  455. s->fl_base_addr |= (val << 16);
  456. break;
  457. case 0x0c:
  458. s->sof_timing = val & 0xff;
  459. break;
  460. case 0x10 ... 0x1f:
  461. {
  462. UHCIPort *port;
  463. USBDevice *dev;
  464. int n;
  465. n = (addr >> 1) & 7;
  466. if (n >= NB_PORTS)
  467. return;
  468. port = &s->ports[n];
  469. dev = port->port.dev;
  470. if (dev && dev->attached) {
  471. /* port reset */
  472. if ( (val & UHCI_PORT_RESET) &&
  473. !(port->ctrl & UHCI_PORT_RESET) ) {
  474. usb_device_reset(dev);
  475. }
  476. }
  477. port->ctrl &= UHCI_PORT_READ_ONLY;
  478. /* enabled may only be set if a device is connected */
  479. if (!(port->ctrl & UHCI_PORT_CCS)) {
  480. val &= ~UHCI_PORT_EN;
  481. }
  482. port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
  483. /* some bits are reset when a '1' is written to them */
  484. port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
  485. }
  486. break;
  487. }
  488. }
  489. static uint64_t uhci_port_read(void *opaque, hwaddr addr, unsigned size)
  490. {
  491. UHCIState *s = opaque;
  492. uint32_t val;
  493. switch(addr) {
  494. case 0x00:
  495. val = s->cmd;
  496. break;
  497. case 0x02:
  498. val = s->status;
  499. break;
  500. case 0x04:
  501. val = s->intr;
  502. break;
  503. case 0x06:
  504. val = s->frnum;
  505. break;
  506. case 0x08:
  507. val = s->fl_base_addr & 0xffff;
  508. break;
  509. case 0x0a:
  510. val = (s->fl_base_addr >> 16) & 0xffff;
  511. break;
  512. case 0x0c:
  513. val = s->sof_timing;
  514. break;
  515. case 0x10 ... 0x1f:
  516. {
  517. UHCIPort *port;
  518. int n;
  519. n = (addr >> 1) & 7;
  520. if (n >= NB_PORTS)
  521. goto read_default;
  522. port = &s->ports[n];
  523. val = port->ctrl;
  524. }
  525. break;
  526. default:
  527. read_default:
  528. val = 0xff7f; /* disabled port */
  529. break;
  530. }
  531. trace_usb_uhci_mmio_readw(addr, val);
  532. return val;
  533. }
  534. /* signal resume if controller suspended */
  535. static void uhci_resume (void *opaque)
  536. {
  537. UHCIState *s = (UHCIState *)opaque;
  538. if (!s)
  539. return;
  540. if (s->cmd & UHCI_CMD_EGSM) {
  541. s->cmd |= UHCI_CMD_FGR;
  542. s->status |= UHCI_STS_RD;
  543. uhci_update_irq(s);
  544. }
  545. }
  546. static void uhci_attach(USBPort *port1)
  547. {
  548. UHCIState *s = port1->opaque;
  549. UHCIPort *port = &s->ports[port1->index];
  550. /* set connect status */
  551. port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
  552. /* update speed */
  553. if (port->port.dev->speed == USB_SPEED_LOW) {
  554. port->ctrl |= UHCI_PORT_LSDA;
  555. } else {
  556. port->ctrl &= ~UHCI_PORT_LSDA;
  557. }
  558. uhci_resume(s);
  559. }
  560. static void uhci_detach(USBPort *port1)
  561. {
  562. UHCIState *s = port1->opaque;
  563. UHCIPort *port = &s->ports[port1->index];
  564. uhci_async_cancel_device(s, port1->dev);
  565. /* set connect status */
  566. if (port->ctrl & UHCI_PORT_CCS) {
  567. port->ctrl &= ~UHCI_PORT_CCS;
  568. port->ctrl |= UHCI_PORT_CSC;
  569. }
  570. /* disable port */
  571. if (port->ctrl & UHCI_PORT_EN) {
  572. port->ctrl &= ~UHCI_PORT_EN;
  573. port->ctrl |= UHCI_PORT_ENC;
  574. }
  575. uhci_resume(s);
  576. }
  577. static void uhci_child_detach(USBPort *port1, USBDevice *child)
  578. {
  579. UHCIState *s = port1->opaque;
  580. uhci_async_cancel_device(s, child);
  581. }
  582. static void uhci_wakeup(USBPort *port1)
  583. {
  584. UHCIState *s = port1->opaque;
  585. UHCIPort *port = &s->ports[port1->index];
  586. if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
  587. port->ctrl |= UHCI_PORT_RD;
  588. uhci_resume(s);
  589. }
  590. }
  591. static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
  592. {
  593. USBDevice *dev;
  594. int i;
  595. for (i = 0; i < NB_PORTS; i++) {
  596. UHCIPort *port = &s->ports[i];
  597. if (!(port->ctrl & UHCI_PORT_EN)) {
  598. continue;
  599. }
  600. dev = usb_find_device(&port->port, addr);
  601. if (dev != NULL) {
  602. return dev;
  603. }
  604. }
  605. return NULL;
  606. }
  607. static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
  608. {
  609. pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
  610. le32_to_cpus(&td->link);
  611. le32_to_cpus(&td->ctrl);
  612. le32_to_cpus(&td->token);
  613. le32_to_cpus(&td->buffer);
  614. }
  615. static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr,
  616. int status, uint32_t *int_mask)
  617. {
  618. uint32_t queue_token = uhci_queue_token(td);
  619. int ret;
  620. switch (status) {
  621. case USB_RET_NAK:
  622. td->ctrl |= TD_CTRL_NAK;
  623. return TD_RESULT_NEXT_QH;
  624. case USB_RET_STALL:
  625. td->ctrl |= TD_CTRL_STALL;
  626. trace_usb_uhci_packet_complete_stall(queue_token, td_addr);
  627. ret = TD_RESULT_NEXT_QH;
  628. break;
  629. case USB_RET_BABBLE:
  630. td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
  631. /* frame interrupted */
  632. trace_usb_uhci_packet_complete_babble(queue_token, td_addr);
  633. ret = TD_RESULT_STOP_FRAME;
  634. break;
  635. case USB_RET_IOERROR:
  636. case USB_RET_NODEV:
  637. default:
  638. td->ctrl |= TD_CTRL_TIMEOUT;
  639. td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
  640. trace_usb_uhci_packet_complete_error(queue_token, td_addr);
  641. ret = TD_RESULT_NEXT_QH;
  642. break;
  643. }
  644. td->ctrl &= ~TD_CTRL_ACTIVE;
  645. s->status |= UHCI_STS_USBERR;
  646. if (td->ctrl & TD_CTRL_IOC) {
  647. *int_mask |= 0x01;
  648. }
  649. uhci_update_irq(s);
  650. return ret;
  651. }
  652. static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
  653. {
  654. int len = 0, max_len;
  655. uint8_t pid;
  656. max_len = ((td->token >> 21) + 1) & 0x7ff;
  657. pid = td->token & 0xff;
  658. if (td->ctrl & TD_CTRL_IOS)
  659. td->ctrl &= ~TD_CTRL_ACTIVE;
  660. if (async->packet.status != USB_RET_SUCCESS) {
  661. return uhci_handle_td_error(s, td, async->td_addr,
  662. async->packet.status, int_mask);
  663. }
  664. len = async->packet.actual_length;
  665. td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
  666. /* The NAK bit may have been set by a previous frame, so clear it
  667. here. The docs are somewhat unclear, but win2k relies on this
  668. behavior. */
  669. td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
  670. if (td->ctrl & TD_CTRL_IOC)
  671. *int_mask |= 0x01;
  672. if (pid == USB_TOKEN_IN) {
  673. if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
  674. *int_mask |= 0x02;
  675. /* short packet: do not update QH */
  676. trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
  677. async->td_addr);
  678. return TD_RESULT_NEXT_QH;
  679. }
  680. }
  681. /* success */
  682. trace_usb_uhci_packet_complete_success(async->queue->token,
  683. async->td_addr);
  684. return TD_RESULT_COMPLETE;
  685. }
  686. static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
  687. UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
  688. {
  689. int ret, max_len;
  690. bool spd;
  691. bool queuing = (q != NULL);
  692. uint8_t pid = td->token & 0xff;
  693. UHCIAsync *async = uhci_async_find_td(s, td_addr);
  694. if (async) {
  695. if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) {
  696. assert(q == NULL || q == async->queue);
  697. q = async->queue;
  698. } else {
  699. uhci_queue_free(async->queue, "guest re-used pending td");
  700. async = NULL;
  701. }
  702. }
  703. if (q == NULL) {
  704. q = uhci_queue_find(s, td);
  705. if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) {
  706. uhci_queue_free(q, "guest re-used qh");
  707. q = NULL;
  708. }
  709. }
  710. if (q) {
  711. q->valid = QH_VALID;
  712. }
  713. /* Is active ? */
  714. if (!(td->ctrl & TD_CTRL_ACTIVE)) {
  715. if (async) {
  716. /* Guest marked a pending td non-active, cancel the queue */
  717. uhci_queue_free(async->queue, "pending td non-active");
  718. }
  719. /*
  720. * ehci11d spec page 22: "Even if the Active bit in the TD is already
  721. * cleared when the TD is fetched ... an IOC interrupt is generated"
  722. */
  723. if (td->ctrl & TD_CTRL_IOC) {
  724. *int_mask |= 0x01;
  725. }
  726. return TD_RESULT_NEXT_QH;
  727. }
  728. if (async) {
  729. if (queuing) {
  730. /* we are busy filling the queue, we are not prepared
  731. to consume completed packages then, just leave them
  732. in async state */
  733. return TD_RESULT_ASYNC_CONT;
  734. }
  735. if (!async->done) {
  736. UHCI_TD last_td;
  737. UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs, asyncs_head);
  738. /*
  739. * While we are waiting for the current td to complete, the guest
  740. * may have added more tds to the queue. Note we re-read the td
  741. * rather then caching it, as we want to see guest made changes!
  742. */
  743. uhci_read_td(s, &last_td, last->td_addr);
  744. uhci_queue_fill(async->queue, &last_td);
  745. return TD_RESULT_ASYNC_CONT;
  746. }
  747. uhci_async_unlink(async);
  748. goto done;
  749. }
  750. if (s->completions_only) {
  751. return TD_RESULT_ASYNC_CONT;
  752. }
  753. /* Allocate new packet */
  754. if (q == NULL) {
  755. USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
  756. USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
  757. if (ep == NULL) {
  758. return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV,
  759. int_mask);
  760. }
  761. q = uhci_queue_new(s, qh_addr, td, ep);
  762. }
  763. async = uhci_async_alloc(q, td_addr);
  764. max_len = ((td->token >> 21) + 1) & 0x7ff;
  765. spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
  766. usb_packet_setup(&async->packet, pid, q->ep, td_addr, spd,
  767. (td->ctrl & TD_CTRL_IOC) != 0);
  768. qemu_sglist_add(&async->sgl, td->buffer, max_len);
  769. usb_packet_map(&async->packet, &async->sgl);
  770. switch(pid) {
  771. case USB_TOKEN_OUT:
  772. case USB_TOKEN_SETUP:
  773. usb_handle_packet(q->ep->dev, &async->packet);
  774. if (async->packet.status == USB_RET_SUCCESS) {
  775. async->packet.actual_length = max_len;
  776. }
  777. break;
  778. case USB_TOKEN_IN:
  779. usb_handle_packet(q->ep->dev, &async->packet);
  780. break;
  781. default:
  782. /* invalid pid : frame interrupted */
  783. usb_packet_unmap(&async->packet, &async->sgl);
  784. uhci_async_free(async);
  785. s->status |= UHCI_STS_HCPERR;
  786. uhci_update_irq(s);
  787. return TD_RESULT_STOP_FRAME;
  788. }
  789. if (async->packet.status == USB_RET_ASYNC) {
  790. uhci_async_link(async);
  791. if (!queuing) {
  792. uhci_queue_fill(q, td);
  793. }
  794. return TD_RESULT_ASYNC_START;
  795. }
  796. done:
  797. ret = uhci_complete_td(s, td, async, int_mask);
  798. usb_packet_unmap(&async->packet, &async->sgl);
  799. uhci_async_free(async);
  800. return ret;
  801. }
  802. static void uhci_async_complete(USBPort *port, USBPacket *packet)
  803. {
  804. UHCIAsync *async = container_of(packet, UHCIAsync, packet);
  805. UHCIState *s = async->queue->uhci;
  806. if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
  807. uhci_async_cancel(async);
  808. return;
  809. }
  810. async->done = 1;
  811. /* Force processing of this packet *now*, needed for migration */
  812. s->completions_only = true;
  813. qemu_bh_schedule(s->bh);
  814. }
  815. static int is_valid(uint32_t link)
  816. {
  817. return (link & 1) == 0;
  818. }
  819. static int is_qh(uint32_t link)
  820. {
  821. return (link & 2) != 0;
  822. }
  823. static int depth_first(uint32_t link)
  824. {
  825. return (link & 4) != 0;
  826. }
  827. /* QH DB used for detecting QH loops */
  828. #define UHCI_MAX_QUEUES 128
  829. typedef struct {
  830. uint32_t addr[UHCI_MAX_QUEUES];
  831. int count;
  832. } QhDb;
  833. static void qhdb_reset(QhDb *db)
  834. {
  835. db->count = 0;
  836. }
  837. /* Add QH to DB. Returns 1 if already present or DB is full. */
  838. static int qhdb_insert(QhDb *db, uint32_t addr)
  839. {
  840. int i;
  841. for (i = 0; i < db->count; i++)
  842. if (db->addr[i] == addr)
  843. return 1;
  844. if (db->count >= UHCI_MAX_QUEUES)
  845. return 1;
  846. db->addr[db->count++] = addr;
  847. return 0;
  848. }
  849. static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
  850. {
  851. uint32_t int_mask = 0;
  852. uint32_t plink = td->link;
  853. UHCI_TD ptd;
  854. int ret;
  855. while (is_valid(plink)) {
  856. uhci_read_td(q->uhci, &ptd, plink);
  857. if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
  858. break;
  859. }
  860. if (uhci_queue_token(&ptd) != q->token) {
  861. break;
  862. }
  863. trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
  864. ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask);
  865. if (ret == TD_RESULT_ASYNC_CONT) {
  866. break;
  867. }
  868. assert(ret == TD_RESULT_ASYNC_START);
  869. assert(int_mask == 0);
  870. plink = ptd.link;
  871. }
  872. usb_device_flush_ep_queue(q->ep->dev, q->ep);
  873. }
  874. static void uhci_process_frame(UHCIState *s)
  875. {
  876. uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
  877. uint32_t curr_qh, td_count = 0;
  878. int cnt, ret;
  879. UHCI_TD td;
  880. UHCI_QH qh;
  881. QhDb qhdb;
  882. frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
  883. pci_dma_read(&s->dev, frame_addr, &link, 4);
  884. le32_to_cpus(&link);
  885. int_mask = 0;
  886. curr_qh = 0;
  887. qhdb_reset(&qhdb);
  888. for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
  889. if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) {
  890. /* We've reached the usb 1.1 bandwidth, which is
  891. 1280 bytes/frame, stop processing */
  892. trace_usb_uhci_frame_stop_bandwidth();
  893. break;
  894. }
  895. if (is_qh(link)) {
  896. /* QH */
  897. trace_usb_uhci_qh_load(link & ~0xf);
  898. if (qhdb_insert(&qhdb, link)) {
  899. /*
  900. * We're going in circles. Which is not a bug because
  901. * HCD is allowed to do that as part of the BW management.
  902. *
  903. * Stop processing here if no transaction has been done
  904. * since we've been here last time.
  905. */
  906. if (td_count == 0) {
  907. trace_usb_uhci_frame_loop_stop_idle();
  908. break;
  909. } else {
  910. trace_usb_uhci_frame_loop_continue();
  911. td_count = 0;
  912. qhdb_reset(&qhdb);
  913. qhdb_insert(&qhdb, link);
  914. }
  915. }
  916. pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
  917. le32_to_cpus(&qh.link);
  918. le32_to_cpus(&qh.el_link);
  919. if (!is_valid(qh.el_link)) {
  920. /* QH w/o elements */
  921. curr_qh = 0;
  922. link = qh.link;
  923. } else {
  924. /* QH with elements */
  925. curr_qh = link;
  926. link = qh.el_link;
  927. }
  928. continue;
  929. }
  930. /* TD */
  931. uhci_read_td(s, &td, link);
  932. trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
  933. old_td_ctrl = td.ctrl;
  934. ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask);
  935. if (old_td_ctrl != td.ctrl) {
  936. /* update the status bits of the TD */
  937. val = cpu_to_le32(td.ctrl);
  938. pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
  939. }
  940. switch (ret) {
  941. case TD_RESULT_STOP_FRAME: /* interrupted frame */
  942. goto out;
  943. case TD_RESULT_NEXT_QH:
  944. case TD_RESULT_ASYNC_CONT:
  945. trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
  946. link = curr_qh ? qh.link : td.link;
  947. continue;
  948. case TD_RESULT_ASYNC_START:
  949. trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
  950. link = curr_qh ? qh.link : td.link;
  951. continue;
  952. case TD_RESULT_COMPLETE:
  953. trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
  954. link = td.link;
  955. td_count++;
  956. s->frame_bytes += (td.ctrl & 0x7ff) + 1;
  957. if (curr_qh) {
  958. /* update QH element link */
  959. qh.el_link = link;
  960. val = cpu_to_le32(qh.el_link);
  961. pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
  962. if (!depth_first(link)) {
  963. /* done with this QH */
  964. curr_qh = 0;
  965. link = qh.link;
  966. }
  967. }
  968. break;
  969. default:
  970. assert(!"unknown return code");
  971. }
  972. /* go to the next entry */
  973. }
  974. out:
  975. s->pending_int_mask |= int_mask;
  976. }
  977. static void uhci_bh(void *opaque)
  978. {
  979. UHCIState *s = opaque;
  980. uhci_process_frame(s);
  981. }
  982. static void uhci_frame_timer(void *opaque)
  983. {
  984. UHCIState *s = opaque;
  985. uint64_t t_now, t_last_run;
  986. int i, frames;
  987. const uint64_t frame_t = get_ticks_per_sec() / FRAME_TIMER_FREQ;
  988. s->completions_only = false;
  989. qemu_bh_cancel(s->bh);
  990. if (!(s->cmd & UHCI_CMD_RS)) {
  991. /* Full stop */
  992. trace_usb_uhci_schedule_stop();
  993. qemu_del_timer(s->frame_timer);
  994. uhci_async_cancel_all(s);
  995. /* set hchalted bit in status - UHCI11D 2.1.2 */
  996. s->status |= UHCI_STS_HCHALTED;
  997. return;
  998. }
  999. /* We still store expire_time in our state, for migration */
  1000. t_last_run = s->expire_time - frame_t;
  1001. t_now = qemu_get_clock_ns(vm_clock);
  1002. /* Process up to MAX_FRAMES_PER_TICK frames */
  1003. frames = (t_now - t_last_run) / frame_t;
  1004. if (frames > s->maxframes) {
  1005. int skipped = frames - s->maxframes;
  1006. s->expire_time += skipped * frame_t;
  1007. s->frnum = (s->frnum + skipped) & 0x7ff;
  1008. frames -= skipped;
  1009. }
  1010. if (frames > MAX_FRAMES_PER_TICK) {
  1011. frames = MAX_FRAMES_PER_TICK;
  1012. }
  1013. for (i = 0; i < frames; i++) {
  1014. s->frame_bytes = 0;
  1015. trace_usb_uhci_frame_start(s->frnum);
  1016. uhci_async_validate_begin(s);
  1017. uhci_process_frame(s);
  1018. uhci_async_validate_end(s);
  1019. /* The spec says frnum is the frame currently being processed, and
  1020. * the guest must look at frnum - 1 on interrupt, so inc frnum now */
  1021. s->frnum = (s->frnum + 1) & 0x7ff;
  1022. s->expire_time += frame_t;
  1023. }
  1024. /* Complete the previous frame(s) */
  1025. if (s->pending_int_mask) {
  1026. s->status2 |= s->pending_int_mask;
  1027. s->status |= UHCI_STS_USBINT;
  1028. uhci_update_irq(s);
  1029. }
  1030. s->pending_int_mask = 0;
  1031. qemu_mod_timer(s->frame_timer, t_now + frame_t);
  1032. }
  1033. static const MemoryRegionOps uhci_ioport_ops = {
  1034. .read = uhci_port_read,
  1035. .write = uhci_port_write,
  1036. .valid.min_access_size = 1,
  1037. .valid.max_access_size = 4,
  1038. .impl.min_access_size = 2,
  1039. .impl.max_access_size = 2,
  1040. .endianness = DEVICE_LITTLE_ENDIAN,
  1041. };
  1042. static USBPortOps uhci_port_ops = {
  1043. .attach = uhci_attach,
  1044. .detach = uhci_detach,
  1045. .child_detach = uhci_child_detach,
  1046. .wakeup = uhci_wakeup,
  1047. .complete = uhci_async_complete,
  1048. };
  1049. static USBBusOps uhci_bus_ops = {
  1050. };
  1051. static int usb_uhci_common_initfn(PCIDevice *dev)
  1052. {
  1053. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
  1054. UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class);
  1055. UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
  1056. uint8_t *pci_conf = s->dev.config;
  1057. int i;
  1058. pci_conf[PCI_CLASS_PROG] = 0x00;
  1059. /* TODO: reset value should be 0. */
  1060. pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
  1061. s->irq_pin = u->info.irq_pin;
  1062. pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1);
  1063. if (s->masterbus) {
  1064. USBPort *ports[NB_PORTS];
  1065. for(i = 0; i < NB_PORTS; i++) {
  1066. ports[i] = &s->ports[i].port;
  1067. }
  1068. if (usb_register_companion(s->masterbus, ports, NB_PORTS,
  1069. s->firstport, s, &uhci_port_ops,
  1070. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
  1071. return -1;
  1072. }
  1073. } else {
  1074. usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
  1075. for (i = 0; i < NB_PORTS; i++) {
  1076. usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
  1077. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  1078. }
  1079. }
  1080. s->bh = qemu_bh_new(uhci_bh, s);
  1081. s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
  1082. s->num_ports_vmstate = NB_PORTS;
  1083. QTAILQ_INIT(&s->queues);
  1084. qemu_register_reset(uhci_reset, s);
  1085. memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
  1086. /* Use region 4 for consistency with real hardware. BSD guests seem
  1087. to rely on this. */
  1088. pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
  1089. return 0;
  1090. }
  1091. static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
  1092. {
  1093. UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
  1094. uint8_t *pci_conf = s->dev.config;
  1095. /* USB misc control 1/2 */
  1096. pci_set_long(pci_conf + 0x40,0x00001000);
  1097. /* PM capability */
  1098. pci_set_long(pci_conf + 0x80,0x00020001);
  1099. /* USB legacy support */
  1100. pci_set_long(pci_conf + 0xc0,0x00002000);
  1101. return usb_uhci_common_initfn(dev);
  1102. }
  1103. static void usb_uhci_exit(PCIDevice *dev)
  1104. {
  1105. UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
  1106. memory_region_destroy(&s->io_bar);
  1107. }
  1108. static Property uhci_properties[] = {
  1109. DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
  1110. DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
  1111. DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
  1112. DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128),
  1113. DEFINE_PROP_END_OF_LIST(),
  1114. };
  1115. static void uhci_class_init(ObjectClass *klass, void *data)
  1116. {
  1117. DeviceClass *dc = DEVICE_CLASS(klass);
  1118. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1119. UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class);
  1120. UHCIInfo *info = data;
  1121. k->init = info->initfn ? info->initfn : usb_uhci_common_initfn;
  1122. k->exit = info->unplug ? usb_uhci_exit : NULL;
  1123. k->vendor_id = info->vendor_id;
  1124. k->device_id = info->device_id;
  1125. k->revision = info->revision;
  1126. k->class_id = PCI_CLASS_SERIAL_USB;
  1127. k->no_hotplug = 1;
  1128. dc->vmsd = &vmstate_uhci;
  1129. dc->props = uhci_properties;
  1130. u->info = *info;
  1131. }
  1132. static UHCIInfo uhci_info[] = {
  1133. {
  1134. .name = "piix3-usb-uhci",
  1135. .vendor_id = PCI_VENDOR_ID_INTEL,
  1136. .device_id = PCI_DEVICE_ID_INTEL_82371SB_2,
  1137. .revision = 0x01,
  1138. .irq_pin = 3,
  1139. .unplug = true,
  1140. },{
  1141. .name = "piix4-usb-uhci",
  1142. .vendor_id = PCI_VENDOR_ID_INTEL,
  1143. .device_id = PCI_DEVICE_ID_INTEL_82371AB_2,
  1144. .revision = 0x01,
  1145. .irq_pin = 3,
  1146. .unplug = true,
  1147. },{
  1148. .name = "vt82c686b-usb-uhci",
  1149. .vendor_id = PCI_VENDOR_ID_VIA,
  1150. .device_id = PCI_DEVICE_ID_VIA_UHCI,
  1151. .revision = 0x01,
  1152. .irq_pin = 3,
  1153. .initfn = usb_uhci_vt82c686b_initfn,
  1154. .unplug = true,
  1155. },{
  1156. .name = "ich9-usb-uhci1", /* 00:1d.0 */
  1157. .vendor_id = PCI_VENDOR_ID_INTEL,
  1158. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1,
  1159. .revision = 0x03,
  1160. .irq_pin = 0,
  1161. .unplug = false,
  1162. },{
  1163. .name = "ich9-usb-uhci2", /* 00:1d.1 */
  1164. .vendor_id = PCI_VENDOR_ID_INTEL,
  1165. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2,
  1166. .revision = 0x03,
  1167. .irq_pin = 1,
  1168. .unplug = false,
  1169. },{
  1170. .name = "ich9-usb-uhci3", /* 00:1d.2 */
  1171. .vendor_id = PCI_VENDOR_ID_INTEL,
  1172. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3,
  1173. .revision = 0x03,
  1174. .irq_pin = 2,
  1175. .unplug = false,
  1176. },{
  1177. .name = "ich9-usb-uhci4", /* 00:1a.0 */
  1178. .vendor_id = PCI_VENDOR_ID_INTEL,
  1179. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4,
  1180. .revision = 0x03,
  1181. .irq_pin = 0,
  1182. .unplug = false,
  1183. },{
  1184. .name = "ich9-usb-uhci5", /* 00:1a.1 */
  1185. .vendor_id = PCI_VENDOR_ID_INTEL,
  1186. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5,
  1187. .revision = 0x03,
  1188. .irq_pin = 1,
  1189. .unplug = false,
  1190. },{
  1191. .name = "ich9-usb-uhci6", /* 00:1a.2 */
  1192. .vendor_id = PCI_VENDOR_ID_INTEL,
  1193. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6,
  1194. .revision = 0x03,
  1195. .irq_pin = 2,
  1196. .unplug = false,
  1197. }
  1198. };
  1199. static void uhci_register_types(void)
  1200. {
  1201. TypeInfo uhci_type_info = {
  1202. .parent = TYPE_PCI_DEVICE,
  1203. .instance_size = sizeof(UHCIState),
  1204. .class_size = sizeof(UHCIPCIDeviceClass),
  1205. .class_init = uhci_class_init,
  1206. };
  1207. int i;
  1208. for (i = 0; i < ARRAY_SIZE(uhci_info); i++) {
  1209. uhci_type_info.name = uhci_info[i].name;
  1210. uhci_type_info.class_data = uhci_info + i;
  1211. type_register(&uhci_type_info);
  1212. }
  1213. }
  1214. type_init(uhci_register_types)