Statistics
| Branch: | Revision:

root / hw / usb-uhci.c @ 771124e1

History | View | Annotate | Download (33.9 kB)

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