2
0

hcd-uhci.c 39 KB

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