]> git.proxmox.com Git - qemu.git/blame - hw/usb-uhci.c
usb-ehci: fix reset
[qemu.git] / hw / usb-uhci.c
CommitLineData
bb36d470
FB
1/*
2 * USB UHCI controller emulation
5fafdf24 3 *
bb36d470 4 * Copyright (c) 2005 Fabrice Bellard
5fafdf24 5 *
54f254f9
AL
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 *
bb36d470
FB
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 */
87ecb68b
PB
28#include "hw.h"
29#include "usb.h"
30#include "pci.h"
31#include "qemu-timer.h"
18e08a55 32#include "usb-uhci.h"
4f4321c1 33#include "iov.h"
df5e66ee 34#include "dma.h"
bb36d470
FB
35
36//#define DEBUG
54f254f9 37//#define DEBUG_DUMP_DATA
bb36d470 38
96217e31
TS
39#define UHCI_CMD_FGR (1 << 4)
40#define UHCI_CMD_EGSM (1 << 3)
bb36d470
FB
41#define UHCI_CMD_GRESET (1 << 2)
42#define UHCI_CMD_HCRESET (1 << 1)
43#define UHCI_CMD_RS (1 << 0)
44
45#define UHCI_STS_HCHALTED (1 << 5)
46#define UHCI_STS_HCPERR (1 << 4)
47#define UHCI_STS_HSERR (1 << 3)
48#define UHCI_STS_RD (1 << 2)
49#define UHCI_STS_USBERR (1 << 1)
50#define UHCI_STS_USBINT (1 << 0)
51
52#define TD_CTRL_SPD (1 << 29)
53#define TD_CTRL_ERROR_SHIFT 27
54#define TD_CTRL_IOS (1 << 25)
55#define TD_CTRL_IOC (1 << 24)
56#define TD_CTRL_ACTIVE (1 << 23)
57#define TD_CTRL_STALL (1 << 22)
58#define TD_CTRL_BABBLE (1 << 20)
59#define TD_CTRL_NAK (1 << 19)
60#define TD_CTRL_TIMEOUT (1 << 18)
61
9159f679 62#define UHCI_PORT_SUSPEND (1 << 12)
bb36d470
FB
63#define UHCI_PORT_RESET (1 << 9)
64#define UHCI_PORT_LSDA (1 << 8)
9159f679 65#define UHCI_PORT_RD (1 << 6)
bb36d470
FB
66#define UHCI_PORT_ENC (1 << 3)
67#define UHCI_PORT_EN (1 << 2)
68#define UHCI_PORT_CSC (1 << 1)
69#define UHCI_PORT_CCS (1 << 0)
70
9159f679
GH
71#define UHCI_PORT_READ_ONLY (0x1bb)
72#define UHCI_PORT_WRITE_CLEAR (UHCI_PORT_CSC | UHCI_PORT_ENC)
73
bb36d470
FB
74#define FRAME_TIMER_FREQ 1000
75
3200d108 76#define FRAME_MAX_LOOPS 256
bb36d470
FB
77
78#define NB_PORTS 2
79
54f254f9 80#ifdef DEBUG
d0f2c4c6 81#define DPRINTF printf
54f254f9 82
0bf9e31a 83static const char *pid2str(int pid)
54f254f9
AL
84{
85 switch (pid) {
86 case USB_TOKEN_SETUP: return "SETUP";
87 case USB_TOKEN_IN: return "IN";
88 case USB_TOKEN_OUT: return "OUT";
89 }
90 return "?";
91}
92
93#else
d0f2c4c6 94#define DPRINTF(...)
54f254f9
AL
95#endif
96
7b5a44c5
GH
97typedef struct UHCIState UHCIState;
98
54f254f9
AL
99/*
100 * Pending async transaction.
101 * 'packet' must be the first field because completion
102 * handler does "(UHCIAsync *) pkt" cast.
103 */
104typedef struct UHCIAsync {
105 USBPacket packet;
df5e66ee 106 QEMUSGList sgl;
7b5a44c5 107 UHCIState *uhci;
ddf6583f 108 QTAILQ_ENTRY(UHCIAsync) next;
54f254f9
AL
109 uint32_t td;
110 uint32_t token;
111 int8_t valid;
8e65b7c0 112 uint8_t isoc;
54f254f9 113 uint8_t done;
54f254f9
AL
114} UHCIAsync;
115
bb36d470
FB
116typedef struct UHCIPort {
117 USBPort port;
118 uint16_t ctrl;
bb36d470
FB
119} UHCIPort;
120
7b5a44c5 121struct UHCIState {
bb36d470 122 PCIDevice dev;
a03f66e4 123 MemoryRegion io_bar;
35e4977f 124 USBBus bus; /* Note unused when we're a companion controller */
bb36d470
FB
125 uint16_t cmd; /* cmd register */
126 uint16_t status;
127 uint16_t intr; /* interrupt enable register */
128 uint16_t frnum; /* frame number */
129 uint32_t fl_base_addr; /* frame list base address */
130 uint8_t sof_timing;
131 uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
8e65b7c0 132 int64_t expire_time;
bb36d470
FB
133 QEMUTimer *frame_timer;
134 UHCIPort ports[NB_PORTS];
4d611c9a
PB
135
136 /* Interrupts that should be raised at the end of the current frame. */
137 uint32_t pending_int_mask;
54f254f9
AL
138
139 /* Active packets */
ddf6583f 140 QTAILQ_HEAD(,UHCIAsync) async_pending;
64e58fe5 141 uint8_t num_ports_vmstate;
35e4977f
HG
142
143 /* Properties */
144 char *masterbus;
145 uint32_t firstport;
7b5a44c5 146};
bb36d470
FB
147
148typedef struct UHCI_TD {
149 uint32_t link;
150 uint32_t ctrl; /* see TD_CTRL_xxx */
151 uint32_t token;
152 uint32_t buffer;
153} UHCI_TD;
154
155typedef struct UHCI_QH {
156 uint32_t link;
157 uint32_t el_link;
158} UHCI_QH;
159
54f254f9
AL
160static UHCIAsync *uhci_async_alloc(UHCIState *s)
161{
7267c094 162 UHCIAsync *async = g_malloc(sizeof(UHCIAsync));
487414f1
AL
163
164 memset(&async->packet, 0, sizeof(async->packet));
7b5a44c5 165 async->uhci = s;
487414f1
AL
166 async->valid = 0;
167 async->td = 0;
168 async->token = 0;
169 async->done = 0;
8e65b7c0 170 async->isoc = 0;
4f4321c1 171 usb_packet_init(&async->packet);
fff23ee9 172 pci_dma_sglist_init(&async->sgl, &s->dev, 1);
54f254f9
AL
173
174 return async;
175}
176
177static void uhci_async_free(UHCIState *s, UHCIAsync *async)
178{
4f4321c1 179 usb_packet_cleanup(&async->packet);
df5e66ee 180 qemu_sglist_destroy(&async->sgl);
7267c094 181 g_free(async);
54f254f9
AL
182}
183
184static void uhci_async_link(UHCIState *s, UHCIAsync *async)
185{
ddf6583f 186 QTAILQ_INSERT_HEAD(&s->async_pending, async, next);
54f254f9
AL
187}
188
189static void uhci_async_unlink(UHCIState *s, UHCIAsync *async)
190{
ddf6583f 191 QTAILQ_REMOVE(&s->async_pending, async, next);
54f254f9
AL
192}
193
194static void uhci_async_cancel(UHCIState *s, UHCIAsync *async)
195{
d0f2c4c6 196 DPRINTF("uhci: cancel td 0x%x token 0x%x done %u\n",
54f254f9
AL
197 async->td, async->token, async->done);
198
199 if (!async->done)
200 usb_cancel_packet(&async->packet);
201 uhci_async_free(s, async);
202}
203
204/*
205 * Mark all outstanding async packets as invalid.
206 * This is used for canceling them when TDs are removed by the HCD.
207 */
208static UHCIAsync *uhci_async_validate_begin(UHCIState *s)
209{
ddf6583f 210 UHCIAsync *async;
54f254f9 211
ddf6583f 212 QTAILQ_FOREACH(async, &s->async_pending, next) {
54f254f9 213 async->valid--;
54f254f9
AL
214 }
215 return NULL;
216}
217
218/*
219 * Cancel async packets that are no longer valid
220 */
221static void uhci_async_validate_end(UHCIState *s)
222{
ddf6583f 223 UHCIAsync *curr, *n;
54f254f9 224
ddf6583f 225 QTAILQ_FOREACH_SAFE(curr, &s->async_pending, next, n) {
54f254f9 226 if (curr->valid > 0) {
54f254f9
AL
227 continue;
228 }
ddf6583f 229 uhci_async_unlink(s, curr);
54f254f9 230 uhci_async_cancel(s, curr);
54f254f9
AL
231 }
232}
233
07771f6f
GH
234static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
235{
236 UHCIAsync *curr, *n;
237
238 QTAILQ_FOREACH_SAFE(curr, &s->async_pending, next, n) {
f53c398a
GH
239 if (!usb_packet_is_inflight(&curr->packet) ||
240 curr->packet.ep->dev != dev) {
07771f6f
GH
241 continue;
242 }
243 uhci_async_unlink(s, curr);
244 uhci_async_cancel(s, curr);
245 }
246}
247
54f254f9
AL
248static void uhci_async_cancel_all(UHCIState *s)
249{
ddf6583f 250 UHCIAsync *curr, *n;
54f254f9 251
ddf6583f
GH
252 QTAILQ_FOREACH_SAFE(curr, &s->async_pending, next, n) {
253 uhci_async_unlink(s, curr);
54f254f9 254 uhci_async_cancel(s, curr);
54f254f9 255 }
54f254f9
AL
256}
257
258static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t addr, uint32_t token)
259{
ddf6583f 260 UHCIAsync *async;
e8ee3c72
AJ
261 UHCIAsync *match = NULL;
262 int count = 0;
263
264 /*
265 * We're looking for the best match here. ie both td addr and token.
266 * Otherwise we return last good match. ie just token.
267 * It's ok to match just token because it identifies the transaction
268 * rather well, token includes: device addr, endpoint, size, etc.
269 *
270 * Also since we queue async transactions in reverse order by returning
271 * last good match we restores the order.
272 *
273 * It's expected that we wont have a ton of outstanding transactions.
274 * If we ever do we'd want to optimize this algorithm.
275 */
54f254f9 276
ddf6583f 277 QTAILQ_FOREACH(async, &s->async_pending, next) {
e8ee3c72
AJ
278 if (async->token == token) {
279 /* Good match */
280 match = async;
281
282 if (async->td == addr) {
283 /* Best match */
284 break;
54f254f9
AL
285 }
286 }
e8ee3c72 287 count++;
54f254f9 288 }
e8ee3c72
AJ
289
290 if (count > 64)
291 fprintf(stderr, "uhci: warning lots of async transactions\n");
292
293 return match;
54f254f9
AL
294}
295
bb36d470
FB
296static void uhci_update_irq(UHCIState *s)
297{
298 int level;
299 if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
300 ((s->status2 & 2) && (s->intr & (1 << 3))) ||
301 ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
302 ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
303 (s->status & UHCI_STS_HSERR) ||
304 (s->status & UHCI_STS_HCPERR)) {
305 level = 1;
306 } else {
307 level = 0;
308 }
d537cf6c 309 qemu_set_irq(s->dev.irq[3], level);
bb36d470
FB
310}
311
c8075ac3 312static void uhci_reset(void *opaque)
bb36d470 313{
c8075ac3 314 UHCIState *s = opaque;
bb36d470
FB
315 uint8_t *pci_conf;
316 int i;
317 UHCIPort *port;
318
d0f2c4c6 319 DPRINTF("uhci: full reset\n");
6f382b5e 320
bb36d470
FB
321 pci_conf = s->dev.config;
322
323 pci_conf[0x6a] = 0x01; /* usb clock */
324 pci_conf[0x6b] = 0x00;
325 s->cmd = 0;
326 s->status = 0;
327 s->status2 = 0;
328 s->intr = 0;
329 s->fl_base_addr = 0;
330 s->sof_timing = 64;
54f254f9 331
bb36d470
FB
332 for(i = 0; i < NB_PORTS; i++) {
333 port = &s->ports[i];
334 port->ctrl = 0x0080;
891fb2cd 335 if (port->port.dev && port->port.dev->attached) {
d28f4e2d 336 usb_port_reset(&port->port);
618c169b 337 }
bb36d470 338 }
54f254f9
AL
339
340 uhci_async_cancel_all(s);
bb36d470
FB
341}
342
817afc61 343static void uhci_pre_save(void *opaque)
b9dc033c
AZ
344{
345 UHCIState *s = opaque;
b9dc033c 346
6f382b5e 347 uhci_async_cancel_all(s);
b9dc033c
AZ
348}
349
817afc61
JQ
350static const VMStateDescription vmstate_uhci_port = {
351 .name = "uhci port",
352 .version_id = 1,
353 .minimum_version_id = 1,
354 .minimum_version_id_old = 1,
355 .fields = (VMStateField []) {
356 VMSTATE_UINT16(ctrl, UHCIPort),
357 VMSTATE_END_OF_LIST()
358 }
359};
360
361static const VMStateDescription vmstate_uhci = {
362 .name = "uhci",
6881dd5f 363 .version_id = 2,
817afc61
JQ
364 .minimum_version_id = 1,
365 .minimum_version_id_old = 1,
366 .pre_save = uhci_pre_save,
367 .fields = (VMStateField []) {
368 VMSTATE_PCI_DEVICE(dev, UHCIState),
369 VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
370 VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
371 vmstate_uhci_port, UHCIPort),
372 VMSTATE_UINT16(cmd, UHCIState),
373 VMSTATE_UINT16(status, UHCIState),
374 VMSTATE_UINT16(intr, UHCIState),
375 VMSTATE_UINT16(frnum, UHCIState),
376 VMSTATE_UINT32(fl_base_addr, UHCIState),
377 VMSTATE_UINT8(sof_timing, UHCIState),
378 VMSTATE_UINT8(status2, UHCIState),
379 VMSTATE_TIMER(frame_timer, UHCIState),
6881dd5f 380 VMSTATE_INT64_V(expire_time, UHCIState, 2),
817afc61
JQ
381 VMSTATE_END_OF_LIST()
382 }
383};
b9dc033c 384
bb36d470
FB
385static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
386{
387 UHCIState *s = opaque;
3b46e624 388
bb36d470
FB
389 addr &= 0x1f;
390 switch(addr) {
391 case 0x0c:
392 s->sof_timing = val;
393 break;
394 }
395}
396
397static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
398{
399 UHCIState *s = opaque;
400 uint32_t val;
401
402 addr &= 0x1f;
403 switch(addr) {
404 case 0x0c:
405 val = s->sof_timing;
d80cfb3f 406 break;
bb36d470
FB
407 default:
408 val = 0xff;
409 break;
410 }
411 return val;
412}
413
414static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
415{
416 UHCIState *s = opaque;
3b46e624 417
bb36d470 418 addr &= 0x1f;
d0f2c4c6 419 DPRINTF("uhci: writew port=0x%04x val=0x%04x\n", addr, val);
54f254f9 420
bb36d470
FB
421 switch(addr) {
422 case 0x00:
423 if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
424 /* start frame processing */
94cc916a
GH
425 s->expire_time = qemu_get_clock_ns(vm_clock) +
426 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
74475455 427 qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
52328140 428 s->status &= ~UHCI_STS_HCHALTED;
467d409f 429 } else if (!(val & UHCI_CMD_RS)) {
52328140 430 s->status |= UHCI_STS_HCHALTED;
bb36d470
FB
431 }
432 if (val & UHCI_CMD_GRESET) {
433 UHCIPort *port;
bb36d470
FB
434 int i;
435
436 /* send reset on the USB bus */
437 for(i = 0; i < NB_PORTS; i++) {
438 port = &s->ports[i];
d28f4e2d 439 usb_device_reset(port->port.dev);
bb36d470
FB
440 }
441 uhci_reset(s);
442 return;
443 }
5e9ab4c4 444 if (val & UHCI_CMD_HCRESET) {
bb36d470
FB
445 uhci_reset(s);
446 return;
447 }
448 s->cmd = val;
449 break;
450 case 0x02:
451 s->status &= ~val;
452 /* XXX: the chip spec is not coherent, so we add a hidden
453 register to distinguish between IOC and SPD */
454 if (val & UHCI_STS_USBINT)
455 s->status2 = 0;
456 uhci_update_irq(s);
457 break;
458 case 0x04:
459 s->intr = val;
460 uhci_update_irq(s);
461 break;
462 case 0x06:
463 if (s->status & UHCI_STS_HCHALTED)
464 s->frnum = val & 0x7ff;
465 break;
466 case 0x10 ... 0x1f:
467 {
468 UHCIPort *port;
469 USBDevice *dev;
470 int n;
471
472 n = (addr >> 1) & 7;
473 if (n >= NB_PORTS)
474 return;
475 port = &s->ports[n];
a594cfbf 476 dev = port->port.dev;
891fb2cd 477 if (dev && dev->attached) {
bb36d470 478 /* port reset */
5fafdf24 479 if ( (val & UHCI_PORT_RESET) &&
bb36d470 480 !(port->ctrl & UHCI_PORT_RESET) ) {
d28f4e2d 481 usb_device_reset(dev);
bb36d470
FB
482 }
483 }
9159f679
GH
484 port->ctrl &= UHCI_PORT_READ_ONLY;
485 port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
bb36d470 486 /* some bits are reset when a '1' is written to them */
9159f679 487 port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
bb36d470
FB
488 }
489 break;
490 }
491}
492
493static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
494{
495 UHCIState *s = opaque;
496 uint32_t val;
497
498 addr &= 0x1f;
499 switch(addr) {
500 case 0x00:
501 val = s->cmd;
502 break;
503 case 0x02:
504 val = s->status;
505 break;
506 case 0x04:
507 val = s->intr;
508 break;
509 case 0x06:
510 val = s->frnum;
511 break;
512 case 0x10 ... 0x1f:
513 {
514 UHCIPort *port;
515 int n;
516 n = (addr >> 1) & 7;
5fafdf24 517 if (n >= NB_PORTS)
bb36d470
FB
518 goto read_default;
519 port = &s->ports[n];
520 val = port->ctrl;
521 }
522 break;
523 default:
524 read_default:
525 val = 0xff7f; /* disabled port */
526 break;
527 }
54f254f9 528
d0f2c4c6 529 DPRINTF("uhci: readw port=0x%04x val=0x%04x\n", addr, val);
54f254f9 530
bb36d470
FB
531 return val;
532}
533
534static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
535{
536 UHCIState *s = opaque;
537
538 addr &= 0x1f;
d0f2c4c6 539 DPRINTF("uhci: writel port=0x%04x val=0x%08x\n", addr, val);
54f254f9 540
bb36d470
FB
541 switch(addr) {
542 case 0x08:
543 s->fl_base_addr = val & ~0xfff;
544 break;
545 }
546}
547
548static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
549{
550 UHCIState *s = opaque;
551 uint32_t val;
552
553 addr &= 0x1f;
554 switch(addr) {
555 case 0x08:
556 val = s->fl_base_addr;
557 break;
558 default:
559 val = 0xffffffff;
560 break;
561 }
562 return val;
563}
564
96217e31
TS
565/* signal resume if controller suspended */
566static void uhci_resume (void *opaque)
567{
568 UHCIState *s = (UHCIState *)opaque;
569
570 if (!s)
571 return;
572
573 if (s->cmd & UHCI_CMD_EGSM) {
574 s->cmd |= UHCI_CMD_FGR;
575 s->status |= UHCI_STS_RD;
576 uhci_update_irq(s);
577 }
578}
579
618c169b 580static void uhci_attach(USBPort *port1)
bb36d470
FB
581{
582 UHCIState *s = port1->opaque;
583 UHCIPort *port = &s->ports[port1->index];
584
618c169b
GH
585 /* set connect status */
586 port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
61064870 587
618c169b
GH
588 /* update speed */
589 if (port->port.dev->speed == USB_SPEED_LOW) {
590 port->ctrl |= UHCI_PORT_LSDA;
bb36d470 591 } else {
618c169b
GH
592 port->ctrl &= ~UHCI_PORT_LSDA;
593 }
96217e31 594
618c169b
GH
595 uhci_resume(s);
596}
96217e31 597
618c169b
GH
598static void uhci_detach(USBPort *port1)
599{
600 UHCIState *s = port1->opaque;
601 UHCIPort *port = &s->ports[port1->index];
602
4706ab6c
HG
603 uhci_async_cancel_device(s, port1->dev);
604
618c169b
GH
605 /* set connect status */
606 if (port->ctrl & UHCI_PORT_CCS) {
607 port->ctrl &= ~UHCI_PORT_CCS;
608 port->ctrl |= UHCI_PORT_CSC;
bb36d470 609 }
618c169b
GH
610 /* disable port */
611 if (port->ctrl & UHCI_PORT_EN) {
612 port->ctrl &= ~UHCI_PORT_EN;
613 port->ctrl |= UHCI_PORT_ENC;
614 }
615
616 uhci_resume(s);
bb36d470
FB
617}
618
4706ab6c
HG
619static void uhci_child_detach(USBPort *port1, USBDevice *child)
620{
621 UHCIState *s = port1->opaque;
622
623 uhci_async_cancel_device(s, child);
624}
625
d47e59b8 626static void uhci_wakeup(USBPort *port1)
9159f679 627{
d47e59b8
HG
628 UHCIState *s = port1->opaque;
629 UHCIPort *port = &s->ports[port1->index];
9159f679
GH
630
631 if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
632 port->ctrl |= UHCI_PORT_RD;
633 uhci_resume(s);
634 }
635}
636
461700c1 637static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
bb36d470 638{
461700c1
GH
639 USBDevice *dev;
640 int i;
54f254f9 641
461700c1 642 for (i = 0; i < NB_PORTS; i++) {
54f254f9 643 UHCIPort *port = &s->ports[i];
461700c1
GH
644 if (!(port->ctrl & UHCI_PORT_EN)) {
645 continue;
646 }
647 dev = usb_find_device(&port->port, addr);
648 if (dev != NULL) {
649 return dev;
891fb2cd 650 }
bb36d470 651 }
461700c1 652 return NULL;
bb36d470
FB
653}
654
d47e59b8 655static void uhci_async_complete(USBPort *port, USBPacket *packet);
54f254f9 656static void uhci_process_frame(UHCIState *s);
4d611c9a 657
bb36d470
FB
658/* return -1 if fatal error (frame must be stopped)
659 0 if TD successful
660 1 if TD unsuccessful or inactive
661*/
54f254f9 662static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
bb36d470 663{
54f254f9 664 int len = 0, max_len, err, ret;
bb36d470 665 uint8_t pid;
bb36d470 666
54f254f9
AL
667 max_len = ((td->token >> 21) + 1) & 0x7ff;
668 pid = td->token & 0xff;
669
4f4321c1 670 ret = async->packet.result;
54f254f9 671
54f254f9
AL
672 if (td->ctrl & TD_CTRL_IOS)
673 td->ctrl &= ~TD_CTRL_ACTIVE;
bb36d470 674
54f254f9
AL
675 if (ret < 0)
676 goto out;
b9dc033c 677
4f4321c1 678 len = async->packet.result;
54f254f9
AL
679 td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
680
681 /* The NAK bit may have been set by a previous frame, so clear it
682 here. The docs are somewhat unclear, but win2k relies on this
683 behavior. */
684 td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
5bd2c0d7
PB
685 if (td->ctrl & TD_CTRL_IOC)
686 *int_mask |= 0x01;
54f254f9
AL
687
688 if (pid == USB_TOKEN_IN) {
689 if (len > max_len) {
54f254f9
AL
690 ret = USB_RET_BABBLE;
691 goto out;
4d611c9a 692 }
b9dc033c 693
54f254f9 694 if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
bb36d470
FB
695 *int_mask |= 0x02;
696 /* short packet: do not update QH */
d0f2c4c6 697 DPRINTF("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token);
bb36d470 698 return 1;
bb36d470 699 }
54f254f9
AL
700 }
701
702 /* success */
703 return 0;
704
705out:
706 switch(ret) {
707 case USB_RET_STALL:
708 td->ctrl |= TD_CTRL_STALL;
709 td->ctrl &= ~TD_CTRL_ACTIVE;
8656954a 710 s->status |= UHCI_STS_USBERR;
0070f095
GH
711 if (td->ctrl & TD_CTRL_IOC) {
712 *int_mask |= 0x01;
713 }
8656954a 714 uhci_update_irq(s);
54f254f9
AL
715 return 1;
716
717 case USB_RET_BABBLE:
718 td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
719 td->ctrl &= ~TD_CTRL_ACTIVE;
8656954a 720 s->status |= UHCI_STS_USBERR;
0070f095
GH
721 if (td->ctrl & TD_CTRL_IOC) {
722 *int_mask |= 0x01;
723 }
8656954a 724 uhci_update_irq(s);
54f254f9
AL
725 /* frame interrupted */
726 return -1;
727
728 case USB_RET_NAK:
729 td->ctrl |= TD_CTRL_NAK;
730 if (pid == USB_TOKEN_SETUP)
731 break;
732 return 1;
733
734 case USB_RET_NODEV:
735 default:
736 break;
737 }
738
739 /* Retry the TD if error count is not zero */
740
741 td->ctrl |= TD_CTRL_TIMEOUT;
742 err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
743 if (err != 0) {
744 err--;
745 if (err == 0) {
bb36d470 746 td->ctrl &= ~TD_CTRL_ACTIVE;
54f254f9 747 s->status |= UHCI_STS_USBERR;
5bd2c0d7
PB
748 if (td->ctrl & TD_CTRL_IOC)
749 *int_mask |= 0x01;
54f254f9 750 uhci_update_irq(s);
bb36d470
FB
751 }
752 }
54f254f9
AL
753 td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
754 (err << TD_CTRL_ERROR_SHIFT);
755 return 1;
bb36d470
FB
756}
757
54f254f9
AL
758static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask)
759{
760 UHCIAsync *async;
5d808245 761 int len = 0, max_len;
8e65b7c0
DA
762 uint8_t pid, isoc;
763 uint32_t token;
079d0b7f
GH
764 USBDevice *dev;
765 USBEndpoint *ep;
54f254f9
AL
766
767 /* Is active ? */
768 if (!(td->ctrl & TD_CTRL_ACTIVE))
769 return 1;
770
8e65b7c0
DA
771 /* token field is not unique for isochronous requests,
772 * so use the destination buffer
773 */
774 if (td->ctrl & TD_CTRL_IOS) {
775 token = td->buffer;
776 isoc = 1;
777 } else {
778 token = td->token;
779 isoc = 0;
780 }
781
782 async = uhci_async_find_td(s, addr, token);
54f254f9
AL
783 if (async) {
784 /* Already submitted */
a145ea51 785 async->valid = 32;
54f254f9
AL
786
787 if (!async->done)
788 return 1;
789
790 uhci_async_unlink(s, async);
791 goto done;
792 }
793
794 /* Allocate new packet */
795 async = uhci_async_alloc(s);
796 if (!async)
797 return 1;
798
8e65b7c0
DA
799 /* valid needs to be large enough to handle 10 frame delay
800 * for initial isochronous requests
801 */
802 async->valid = 32;
54f254f9 803 async->td = addr;
8e65b7c0
DA
804 async->token = token;
805 async->isoc = isoc;
54f254f9
AL
806
807 max_len = ((td->token >> 21) + 1) & 0x7ff;
808 pid = td->token & 0xff;
809
079d0b7f
GH
810 dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
811 ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
812 usb_packet_setup(&async->packet, pid, ep);
df5e66ee
GH
813 qemu_sglist_add(&async->sgl, td->buffer, max_len);
814 usb_packet_map(&async->packet, &async->sgl);
54f254f9
AL
815
816 switch(pid) {
817 case USB_TOKEN_OUT:
818 case USB_TOKEN_SETUP:
079d0b7f 819 len = usb_handle_packet(dev, &async->packet);
5d808245
AJ
820 if (len >= 0)
821 len = max_len;
54f254f9
AL
822 break;
823
824 case USB_TOKEN_IN:
079d0b7f 825 len = usb_handle_packet(dev, &async->packet);
54f254f9
AL
826 break;
827
828 default:
829 /* invalid pid : frame interrupted */
830 uhci_async_free(s, async);
831 s->status |= UHCI_STS_HCPERR;
832 uhci_update_irq(s);
833 return -1;
834 }
835
5d808245 836 if (len == USB_RET_ASYNC) {
54f254f9
AL
837 uhci_async_link(s, async);
838 return 2;
839 }
840
4f4321c1 841 async->packet.result = len;
54f254f9
AL
842
843done:
5d808245 844 len = uhci_complete_td(s, td, async, int_mask);
df5e66ee 845 usb_packet_unmap(&async->packet);
54f254f9 846 uhci_async_free(s, async);
5d808245 847 return len;
54f254f9
AL
848}
849
d47e59b8 850static void uhci_async_complete(USBPort *port, USBPacket *packet)
4d611c9a 851{
7b5a44c5
GH
852 UHCIAsync *async = container_of(packet, UHCIAsync, packet);
853 UHCIState *s = async->uhci;
54f254f9 854
d0f2c4c6 855 DPRINTF("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token);
54f254f9 856
8e65b7c0
DA
857 if (async->isoc) {
858 UHCI_TD td;
859 uint32_t link = async->td;
860 uint32_t int_mask = 0, val;
d4c4e6fd 861
9fe2fd67 862 pci_dma_read(&s->dev, link & ~0xf, &td, sizeof(td));
8e65b7c0
DA
863 le32_to_cpus(&td.link);
864 le32_to_cpus(&td.ctrl);
865 le32_to_cpus(&td.token);
866 le32_to_cpus(&td.buffer);
867
868 uhci_async_unlink(s, async);
d4c4e6fd 869 uhci_complete_td(s, &td, async, &int_mask);
8e65b7c0 870 s->pending_int_mask |= int_mask;
54f254f9 871
8e65b7c0
DA
872 /* update the status bits of the TD */
873 val = cpu_to_le32(td.ctrl);
9fe2fd67 874 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
8e65b7c0
DA
875 uhci_async_free(s, async);
876 } else {
877 async->done = 1;
878 uhci_process_frame(s);
879 }
54f254f9
AL
880}
881
882static int is_valid(uint32_t link)
883{
884 return (link & 1) == 0;
885}
886
887static int is_qh(uint32_t link)
888{
889 return (link & 2) != 0;
890}
891
892static int depth_first(uint32_t link)
893{
894 return (link & 4) != 0;
895}
896
897/* QH DB used for detecting QH loops */
898#define UHCI_MAX_QUEUES 128
899typedef struct {
900 uint32_t addr[UHCI_MAX_QUEUES];
901 int count;
902} QhDb;
903
904static void qhdb_reset(QhDb *db)
905{
906 db->count = 0;
907}
908
909/* Add QH to DB. Returns 1 if already present or DB is full. */
910static int qhdb_insert(QhDb *db, uint32_t addr)
911{
912 int i;
913 for (i = 0; i < db->count; i++)
914 if (db->addr[i] == addr)
915 return 1;
916
917 if (db->count >= UHCI_MAX_QUEUES)
918 return 1;
919
920 db->addr[db->count++] = addr;
921 return 0;
922}
923
924static void uhci_process_frame(UHCIState *s)
925{
926 uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
3200d108 927 uint32_t curr_qh, td_count = 0, bytes_count = 0;
54f254f9 928 int cnt, ret;
4d611c9a 929 UHCI_TD td;
54f254f9
AL
930 UHCI_QH qh;
931 QhDb qhdb;
4d611c9a 932
54f254f9
AL
933 frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
934
d0f2c4c6 935 DPRINTF("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr);
54f254f9 936
9fe2fd67 937 pci_dma_read(&s->dev, frame_addr, &link, 4);
54f254f9 938 le32_to_cpus(&link);
b9dc033c 939
54f254f9
AL
940 int_mask = 0;
941 curr_qh = 0;
942
943 qhdb_reset(&qhdb);
944
945 for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
946 if (is_qh(link)) {
947 /* QH */
948
949 if (qhdb_insert(&qhdb, link)) {
950 /*
951 * We're going in circles. Which is not a bug because
3200d108
GH
952 * HCD is allowed to do that as part of the BW management.
953 *
954 * Stop processing here if
955 * (a) no transaction has been done since we've been
956 * here last time, or
957 * (b) we've reached the usb 1.1 bandwidth, which is
958 * 1280 bytes/frame.
54f254f9 959 */
d0f2c4c6 960 DPRINTF("uhci: detected loop. qh 0x%x\n", link);
3200d108
GH
961 if (td_count == 0) {
962 DPRINTF("uhci: no transaction last round, stop\n");
963 break;
964 } else if (bytes_count >= 1280) {
965 DPRINTF("uhci: bandwidth limit reached, stop\n");
966 break;
967 } else {
968 td_count = 0;
969 qhdb_reset(&qhdb);
970 qhdb_insert(&qhdb, link);
971 }
54f254f9
AL
972 }
973
9fe2fd67 974 pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
54f254f9
AL
975 le32_to_cpus(&qh.link);
976 le32_to_cpus(&qh.el_link);
977
d0f2c4c6 978 DPRINTF("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
54f254f9
AL
979 link, qh.link, qh.el_link);
980
981 if (!is_valid(qh.el_link)) {
982 /* QH w/o elements */
983 curr_qh = 0;
984 link = qh.link;
985 } else {
986 /* QH with elements */
987 curr_qh = link;
988 link = qh.el_link;
989 }
990 continue;
991 }
992
993 /* TD */
9fe2fd67 994 pci_dma_read(&s->dev, link & ~0xf, &td, sizeof(td));
b9dc033c
AZ
995 le32_to_cpus(&td.link);
996 le32_to_cpus(&td.ctrl);
997 le32_to_cpus(&td.token);
998 le32_to_cpus(&td.buffer);
b9dc033c 999
d0f2c4c6 1000 DPRINTF("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
54f254f9
AL
1001 link, td.link, td.ctrl, td.token, curr_qh);
1002
1003 old_td_ctrl = td.ctrl;
1004 ret = uhci_handle_td(s, link, &td, &int_mask);
b9dc033c 1005 if (old_td_ctrl != td.ctrl) {
54f254f9 1006 /* update the status bits of the TD */
b9dc033c 1007 val = cpu_to_le32(td.ctrl);
9fe2fd67 1008 pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
b9dc033c 1009 }
54f254f9
AL
1010
1011 if (ret < 0) {
1012 /* interrupted frame */
1013 break;
b9dc033c 1014 }
b9dc033c 1015
54f254f9 1016 if (ret == 2 || ret == 1) {
d0f2c4c6 1017 DPRINTF("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
54f254f9
AL
1018 link, ret == 2 ? "pend" : "skip",
1019 td.link, td.ctrl, td.token, curr_qh);
b9dc033c 1020
54f254f9
AL
1021 link = curr_qh ? qh.link : td.link;
1022 continue;
4d611c9a 1023 }
54f254f9
AL
1024
1025 /* completed TD */
1026
d0f2c4c6 1027 DPRINTF("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
54f254f9
AL
1028 link, td.link, td.ctrl, td.token, curr_qh);
1029
1030 link = td.link;
3200d108
GH
1031 td_count++;
1032 bytes_count += (td.ctrl & 0x7ff) + 1;
54f254f9
AL
1033
1034 if (curr_qh) {
1035 /* update QH element link */
1036 qh.el_link = link;
4d611c9a 1037 val = cpu_to_le32(qh.el_link);
9fe2fd67 1038 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
54f254f9
AL
1039
1040 if (!depth_first(link)) {
1041 /* done with this QH */
1042
d0f2c4c6 1043 DPRINTF("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
54f254f9
AL
1044 curr_qh, qh.link, qh.el_link);
1045
1046 curr_qh = 0;
1047 link = qh.link;
1048 }
4d611c9a 1049 }
54f254f9
AL
1050
1051 /* go to the next entry */
4d611c9a 1052 }
54f254f9 1053
8e65b7c0 1054 s->pending_int_mask |= int_mask;
4d611c9a
PB
1055}
1056
bb36d470
FB
1057static void uhci_frame_timer(void *opaque)
1058{
1059 UHCIState *s = opaque;
8e65b7c0
DA
1060
1061 /* prepare the timer for the next frame */
1062 s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
bb36d470
FB
1063
1064 if (!(s->cmd & UHCI_CMD_RS)) {
54f254f9 1065 /* Full stop */
bb36d470 1066 qemu_del_timer(s->frame_timer);
52328140
FB
1067 /* set hchalted bit in status - UHCI11D 2.1.2 */
1068 s->status |= UHCI_STS_HCHALTED;
6f382b5e 1069
d0f2c4c6 1070 DPRINTF("uhci: halted\n");
bb36d470
FB
1071 return;
1072 }
54f254f9
AL
1073
1074 /* Complete the previous frame */
4d611c9a
PB
1075 if (s->pending_int_mask) {
1076 s->status2 |= s->pending_int_mask;
54f254f9 1077 s->status |= UHCI_STS_USBINT;
4d611c9a
PB
1078 uhci_update_irq(s);
1079 }
8e65b7c0 1080 s->pending_int_mask = 0;
b9dc033c 1081
54f254f9
AL
1082 /* Start new frame */
1083 s->frnum = (s->frnum + 1) & 0x7ff;
1084
d0f2c4c6 1085 DPRINTF("uhci: new frame #%u\n" , s->frnum);
54f254f9
AL
1086
1087 uhci_async_validate_begin(s);
1088
1089 uhci_process_frame(s);
1090
1091 uhci_async_validate_end(s);
b9dc033c 1092
8e65b7c0 1093 qemu_mod_timer(s->frame_timer, s->expire_time);
bb36d470
FB
1094}
1095
a03f66e4
AK
1096static const MemoryRegionPortio uhci_portio[] = {
1097 { 0, 32, 2, .write = uhci_ioport_writew, },
1098 { 0, 32, 2, .read = uhci_ioport_readw, },
1099 { 0, 32, 4, .write = uhci_ioport_writel, },
1100 { 0, 32, 4, .read = uhci_ioport_readl, },
1101 { 0, 32, 1, .write = uhci_ioport_writeb, },
1102 { 0, 32, 1, .read = uhci_ioport_readb, },
1103 PORTIO_END_OF_LIST()
1104};
1105
1106static const MemoryRegionOps uhci_ioport_ops = {
1107 .old_portio = uhci_portio,
1108};
bb36d470 1109
0d86d2be
GH
1110static USBPortOps uhci_port_ops = {
1111 .attach = uhci_attach,
618c169b 1112 .detach = uhci_detach,
4706ab6c 1113 .child_detach = uhci_child_detach,
9159f679 1114 .wakeup = uhci_wakeup,
13a9a0d3 1115 .complete = uhci_async_complete,
0d86d2be
GH
1116};
1117
07771f6f 1118static USBBusOps uhci_bus_ops = {
07771f6f
GH
1119};
1120
dc638fad 1121static int usb_uhci_common_initfn(PCIDevice *dev)
bb36d470 1122{
dc638fad 1123 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
6cf9b6f1 1124 uint8_t *pci_conf = s->dev.config;
bb36d470
FB
1125 int i;
1126
db579e9e 1127 pci_conf[PCI_CLASS_PROG] = 0x00;
db579e9e 1128 /* TODO: reset value should be 0. */
817e0b6f 1129 pci_conf[PCI_INTERRUPT_PIN] = 4; /* interrupt pin D */
e59d33a7 1130 pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
3b46e624 1131
35e4977f
HG
1132 if (s->masterbus) {
1133 USBPort *ports[NB_PORTS];
1134 for(i = 0; i < NB_PORTS; i++) {
1135 ports[i] = &s->ports[i].port;
1136 }
1137 if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1138 s->firstport, s, &uhci_port_ops,
1139 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1140 return -1;
1141 }
1142 } else {
1143 usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1144 for (i = 0; i < NB_PORTS; i++) {
1145 usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1146 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1147 }
bb36d470 1148 }
74475455 1149 s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
64e58fe5 1150 s->num_ports_vmstate = NB_PORTS;
ddf6583f 1151 QTAILQ_INIT(&s->async_pending);
bb36d470 1152
a08d4367 1153 qemu_register_reset(uhci_reset, s);
bb36d470 1154
a03f66e4 1155 memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
38ca0f6d
PB
1156 /* Use region 4 for consistency with real hardware. BSD guests seem
1157 to rely on this. */
e824b2cc 1158 pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
6f382b5e 1159
6cf9b6f1 1160 return 0;
bb36d470 1161}
afcc3cdf 1162
30235a54
HC
1163static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1164{
1165 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1166 uint8_t *pci_conf = s->dev.config;
1167
30235a54
HC
1168 /* USB misc control 1/2 */
1169 pci_set_long(pci_conf + 0x40,0x00001000);
1170 /* PM capability */
1171 pci_set_long(pci_conf + 0x80,0x00020001);
1172 /* USB legacy support */
1173 pci_set_long(pci_conf + 0xc0,0x00002000);
1174
dc638fad 1175 return usb_uhci_common_initfn(dev);
30235a54
HC
1176}
1177
a03f66e4
AK
1178static int usb_uhci_exit(PCIDevice *dev)
1179{
1180 UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1181
1182 memory_region_destroy(&s->io_bar);
1183 return 0;
1184}
1185
1b5a7570
GH
1186static Property uhci_properties[] = {
1187 DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1188 DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
1189 DEFINE_PROP_END_OF_LIST(),
1190};
1191
40021f08
AL
1192static void piix3_uhci_class_init(ObjectClass *klass, void *data)
1193{
39bffca2 1194 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1195 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1196
1197 k->init = usb_uhci_common_initfn;
1198 k->exit = usb_uhci_exit;
1199 k->vendor_id = PCI_VENDOR_ID_INTEL;
1200 k->device_id = PCI_DEVICE_ID_INTEL_82371SB_2;
1201 k->revision = 0x01;
1202 k->class_id = PCI_CLASS_SERIAL_USB;
39bffca2
AL
1203 dc->vmsd = &vmstate_uhci;
1204 dc->props = uhci_properties;
40021f08
AL
1205}
1206
39bffca2
AL
1207static TypeInfo piix3_uhci_info = {
1208 .name = "piix3-usb-uhci",
1209 .parent = TYPE_PCI_DEVICE,
1210 .instance_size = sizeof(UHCIState),
1211 .class_init = piix3_uhci_class_init,
e855761c
AL
1212};
1213
40021f08
AL
1214static void piix4_uhci_class_init(ObjectClass *klass, void *data)
1215{
39bffca2 1216 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1217 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1218
1219 k->init = usb_uhci_common_initfn;
1220 k->exit = usb_uhci_exit;
1221 k->vendor_id = PCI_VENDOR_ID_INTEL;
1222 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_2;
1223 k->revision = 0x01;
1224 k->class_id = PCI_CLASS_SERIAL_USB;
39bffca2
AL
1225 dc->vmsd = &vmstate_uhci;
1226 dc->props = uhci_properties;
40021f08
AL
1227}
1228
39bffca2
AL
1229static TypeInfo piix4_uhci_info = {
1230 .name = "piix4-usb-uhci",
1231 .parent = TYPE_PCI_DEVICE,
1232 .instance_size = sizeof(UHCIState),
1233 .class_init = piix4_uhci_class_init,
e855761c
AL
1234};
1235
40021f08
AL
1236static void vt82c686b_uhci_class_init(ObjectClass *klass, void *data)
1237{
39bffca2 1238 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1239 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1240
1241 k->init = usb_uhci_vt82c686b_initfn;
1242 k->exit = usb_uhci_exit;
1243 k->vendor_id = PCI_VENDOR_ID_VIA;
1244 k->device_id = PCI_DEVICE_ID_VIA_UHCI;
1245 k->revision = 0x01;
1246 k->class_id = PCI_CLASS_SERIAL_USB;
39bffca2
AL
1247 dc->vmsd = &vmstate_uhci;
1248 dc->props = uhci_properties;
40021f08
AL
1249}
1250
39bffca2
AL
1251static TypeInfo vt82c686b_uhci_info = {
1252 .name = "vt82c686b-usb-uhci",
1253 .parent = TYPE_PCI_DEVICE,
1254 .instance_size = sizeof(UHCIState),
1255 .class_init = vt82c686b_uhci_class_init,
e855761c
AL
1256};
1257
40021f08
AL
1258static void ich9_uhci1_class_init(ObjectClass *klass, void *data)
1259{
39bffca2 1260 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1261 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1262
1263 k->init = usb_uhci_common_initfn;
1264 k->vendor_id = PCI_VENDOR_ID_INTEL;
1265 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1;
1266 k->revision = 0x03;
1267 k->class_id = PCI_CLASS_SERIAL_USB;
39bffca2
AL
1268 dc->vmsd = &vmstate_uhci;
1269 dc->props = uhci_properties;
40021f08
AL
1270}
1271
39bffca2
AL
1272static TypeInfo ich9_uhci1_info = {
1273 .name = "ich9-usb-uhci1",
1274 .parent = TYPE_PCI_DEVICE,
1275 .instance_size = sizeof(UHCIState),
1276 .class_init = ich9_uhci1_class_init,
e855761c
AL
1277};
1278
40021f08
AL
1279static void ich9_uhci2_class_init(ObjectClass *klass, void *data)
1280{
39bffca2 1281 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1282 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1283
1284 k->init = usb_uhci_common_initfn;
1285 k->vendor_id = PCI_VENDOR_ID_INTEL;
1286 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2;
1287 k->revision = 0x03;
1288 k->class_id = PCI_CLASS_SERIAL_USB;
39bffca2
AL
1289 dc->vmsd = &vmstate_uhci;
1290 dc->props = uhci_properties;
40021f08
AL
1291}
1292
39bffca2
AL
1293static TypeInfo ich9_uhci2_info = {
1294 .name = "ich9-usb-uhci2",
1295 .parent = TYPE_PCI_DEVICE,
1296 .instance_size = sizeof(UHCIState),
1297 .class_init = ich9_uhci2_class_init,
e855761c
AL
1298};
1299
40021f08
AL
1300static void ich9_uhci3_class_init(ObjectClass *klass, void *data)
1301{
39bffca2 1302 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1303 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1304
1305 k->init = usb_uhci_common_initfn;
1306 k->vendor_id = PCI_VENDOR_ID_INTEL;
1307 k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3;
1308 k->revision = 0x03;
1309 k->class_id = PCI_CLASS_SERIAL_USB;
39bffca2
AL
1310 dc->vmsd = &vmstate_uhci;
1311 dc->props = uhci_properties;
40021f08
AL
1312}
1313
39bffca2
AL
1314static TypeInfo ich9_uhci3_info = {
1315 .name = "ich9-usb-uhci3",
1316 .parent = TYPE_PCI_DEVICE,
1317 .instance_size = sizeof(UHCIState),
1318 .class_init = ich9_uhci3_class_init,
6cf9b6f1 1319};
afcc3cdf 1320
83f7d43a 1321static void uhci_register_types(void)
6cf9b6f1 1322{
39bffca2
AL
1323 type_register_static(&piix3_uhci_info);
1324 type_register_static(&piix4_uhci_info);
1325 type_register_static(&vt82c686b_uhci_info);
1326 type_register_static(&ich9_uhci1_info);
1327 type_register_static(&ich9_uhci2_info);
1328 type_register_static(&ich9_uhci3_info);
6cf9b6f1 1329}
83f7d43a
AF
1330
1331type_init(uhci_register_types)
afcc3cdf 1332
6cf9b6f1
GH
1333void usb_uhci_piix3_init(PCIBus *bus, int devfn)
1334{
556cd098 1335 pci_create_simple(bus, devfn, "piix3-usb-uhci");
6cf9b6f1 1336}
54f254f9 1337
6cf9b6f1
GH
1338void usb_uhci_piix4_init(PCIBus *bus, int devfn)
1339{
556cd098 1340 pci_create_simple(bus, devfn, "piix4-usb-uhci");
afcc3cdf 1341}
30235a54
HC
1342
1343void usb_uhci_vt82c686b_init(PCIBus *bus, int devfn)
1344{
1345 pci_create_simple(bus, devfn, "vt82c686b-usb-uhci");
1346}