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