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