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