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