hcd-uhci.c 38 KB

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