Statistics
| Branch: | Revision:

root / hw / usb-uhci.c @ 1673ad51

History | View | Annotate | Download (28.4 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 bb36d470 bellard
34 bb36d470 bellard
//#define DEBUG
35 54f254f9 aliguori
//#define DEBUG_DUMP_DATA
36 bb36d470 bellard
37 96217e31 ths
#define UHCI_CMD_FGR      (1 << 4)
38 96217e31 ths
#define UHCI_CMD_EGSM     (1 << 3)
39 bb36d470 bellard
#define UHCI_CMD_GRESET   (1 << 2)
40 bb36d470 bellard
#define UHCI_CMD_HCRESET  (1 << 1)
41 bb36d470 bellard
#define UHCI_CMD_RS       (1 << 0)
42 bb36d470 bellard
43 bb36d470 bellard
#define UHCI_STS_HCHALTED (1 << 5)
44 bb36d470 bellard
#define UHCI_STS_HCPERR   (1 << 4)
45 bb36d470 bellard
#define UHCI_STS_HSERR    (1 << 3)
46 bb36d470 bellard
#define UHCI_STS_RD       (1 << 2)
47 bb36d470 bellard
#define UHCI_STS_USBERR   (1 << 1)
48 bb36d470 bellard
#define UHCI_STS_USBINT   (1 << 0)
49 bb36d470 bellard
50 bb36d470 bellard
#define TD_CTRL_SPD     (1 << 29)
51 bb36d470 bellard
#define TD_CTRL_ERROR_SHIFT  27
52 bb36d470 bellard
#define TD_CTRL_IOS     (1 << 25)
53 bb36d470 bellard
#define TD_CTRL_IOC     (1 << 24)
54 bb36d470 bellard
#define TD_CTRL_ACTIVE  (1 << 23)
55 bb36d470 bellard
#define TD_CTRL_STALL   (1 << 22)
56 bb36d470 bellard
#define TD_CTRL_BABBLE  (1 << 20)
57 bb36d470 bellard
#define TD_CTRL_NAK     (1 << 19)
58 bb36d470 bellard
#define TD_CTRL_TIMEOUT (1 << 18)
59 bb36d470 bellard
60 bb36d470 bellard
#define UHCI_PORT_RESET (1 << 9)
61 bb36d470 bellard
#define UHCI_PORT_LSDA  (1 << 8)
62 bb36d470 bellard
#define UHCI_PORT_ENC   (1 << 3)
63 bb36d470 bellard
#define UHCI_PORT_EN    (1 << 2)
64 bb36d470 bellard
#define UHCI_PORT_CSC   (1 << 1)
65 bb36d470 bellard
#define UHCI_PORT_CCS   (1 << 0)
66 bb36d470 bellard
67 bb36d470 bellard
#define FRAME_TIMER_FREQ 1000
68 bb36d470 bellard
69 bb36d470 bellard
#define FRAME_MAX_LOOPS  100
70 bb36d470 bellard
71 bb36d470 bellard
#define NB_PORTS 2
72 bb36d470 bellard
73 54f254f9 aliguori
#ifdef DEBUG
74 54f254f9 aliguori
#define dprintf printf
75 54f254f9 aliguori
76 0bf9e31a Blue Swirl
static const char *pid2str(int pid)
77 54f254f9 aliguori
{
78 54f254f9 aliguori
    switch (pid) {
79 54f254f9 aliguori
    case USB_TOKEN_SETUP: return "SETUP";
80 54f254f9 aliguori
    case USB_TOKEN_IN:    return "IN";
81 54f254f9 aliguori
    case USB_TOKEN_OUT:   return "OUT";
82 54f254f9 aliguori
    }
83 54f254f9 aliguori
    return "?";
84 54f254f9 aliguori
}
85 54f254f9 aliguori
86 54f254f9 aliguori
#else
87 54f254f9 aliguori
#define dprintf(...)
88 54f254f9 aliguori
#endif
89 54f254f9 aliguori
90 54f254f9 aliguori
#ifdef DEBUG_DUMP_DATA
91 54f254f9 aliguori
static void dump_data(const uint8_t *data, int len)
92 54f254f9 aliguori
{
93 54f254f9 aliguori
    int i;
94 54f254f9 aliguori
95 54f254f9 aliguori
    printf("uhci: data: ");
96 54f254f9 aliguori
    for(i = 0; i < len; i++)
97 54f254f9 aliguori
        printf(" %02x", data[i]);
98 54f254f9 aliguori
    printf("\n");
99 54f254f9 aliguori
}
100 54f254f9 aliguori
#else
101 54f254f9 aliguori
static void dump_data(const uint8_t *data, int len) {}
102 54f254f9 aliguori
#endif
103 54f254f9 aliguori
104 54f254f9 aliguori
/* 
105 54f254f9 aliguori
 * Pending async transaction.
106 54f254f9 aliguori
 * 'packet' must be the first field because completion
107 54f254f9 aliguori
 * handler does "(UHCIAsync *) pkt" cast.
108 54f254f9 aliguori
 */
109 54f254f9 aliguori
typedef struct UHCIAsync {
110 54f254f9 aliguori
    USBPacket packet;
111 54f254f9 aliguori
    struct UHCIAsync *next;
112 54f254f9 aliguori
    uint32_t  td;
113 54f254f9 aliguori
    uint32_t  token;
114 54f254f9 aliguori
    int8_t    valid;
115 54f254f9 aliguori
    uint8_t   done;
116 54f254f9 aliguori
    uint8_t   buffer[2048];
117 54f254f9 aliguori
} UHCIAsync;
118 54f254f9 aliguori
119 bb36d470 bellard
typedef struct UHCIPort {
120 bb36d470 bellard
    USBPort port;
121 bb36d470 bellard
    uint16_t ctrl;
122 bb36d470 bellard
} UHCIPort;
123 bb36d470 bellard
124 bb36d470 bellard
typedef struct UHCIState {
125 bb36d470 bellard
    PCIDevice dev;
126 b2317837 Gerd Hoffmann
    USBBus bus;
127 bb36d470 bellard
    uint16_t cmd; /* cmd register */
128 bb36d470 bellard
    uint16_t status;
129 bb36d470 bellard
    uint16_t intr; /* interrupt enable register */
130 bb36d470 bellard
    uint16_t frnum; /* frame number */
131 bb36d470 bellard
    uint32_t fl_base_addr; /* frame list base address */
132 bb36d470 bellard
    uint8_t sof_timing;
133 bb36d470 bellard
    uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
134 bb36d470 bellard
    QEMUTimer *frame_timer;
135 bb36d470 bellard
    UHCIPort ports[NB_PORTS];
136 4d611c9a pbrook
137 4d611c9a pbrook
    /* Interrupts that should be raised at the end of the current frame.  */
138 4d611c9a pbrook
    uint32_t pending_int_mask;
139 54f254f9 aliguori
140 54f254f9 aliguori
    /* Active packets */
141 54f254f9 aliguori
    UHCIAsync *async_pending;
142 54f254f9 aliguori
    UHCIAsync *async_pool;
143 64e58fe5 Juan Quintela
    uint8_t num_ports_vmstate;
144 bb36d470 bellard
} UHCIState;
145 bb36d470 bellard
146 bb36d470 bellard
typedef struct UHCI_TD {
147 bb36d470 bellard
    uint32_t link;
148 bb36d470 bellard
    uint32_t ctrl; /* see TD_CTRL_xxx */
149 bb36d470 bellard
    uint32_t token;
150 bb36d470 bellard
    uint32_t buffer;
151 bb36d470 bellard
} UHCI_TD;
152 bb36d470 bellard
153 bb36d470 bellard
typedef struct UHCI_QH {
154 bb36d470 bellard
    uint32_t link;
155 bb36d470 bellard
    uint32_t el_link;
156 bb36d470 bellard
} UHCI_QH;
157 bb36d470 bellard
158 54f254f9 aliguori
static UHCIAsync *uhci_async_alloc(UHCIState *s)
159 54f254f9 aliguori
{
160 54f254f9 aliguori
    UHCIAsync *async = qemu_malloc(sizeof(UHCIAsync));
161 487414f1 aliguori
162 487414f1 aliguori
    memset(&async->packet, 0, sizeof(async->packet));
163 487414f1 aliguori
    async->valid = 0;
164 487414f1 aliguori
    async->td    = 0;
165 487414f1 aliguori
    async->token = 0;
166 487414f1 aliguori
    async->done  = 0;
167 487414f1 aliguori
    async->next  = NULL;
168 54f254f9 aliguori
169 54f254f9 aliguori
    return async;
170 54f254f9 aliguori
}
171 54f254f9 aliguori
172 54f254f9 aliguori
static void uhci_async_free(UHCIState *s, UHCIAsync *async)
173 54f254f9 aliguori
{
174 54f254f9 aliguori
    qemu_free(async);
175 54f254f9 aliguori
}
176 54f254f9 aliguori
177 54f254f9 aliguori
static void uhci_async_link(UHCIState *s, UHCIAsync *async)
178 54f254f9 aliguori
{
179 54f254f9 aliguori
    async->next = s->async_pending;
180 54f254f9 aliguori
    s->async_pending = async;
181 54f254f9 aliguori
}
182 54f254f9 aliguori
183 54f254f9 aliguori
static void uhci_async_unlink(UHCIState *s, UHCIAsync *async)
184 54f254f9 aliguori
{
185 54f254f9 aliguori
    UHCIAsync *curr = s->async_pending;
186 54f254f9 aliguori
    UHCIAsync **prev = &s->async_pending;
187 54f254f9 aliguori
188 54f254f9 aliguori
    while (curr) {
189 54f254f9 aliguori
        if (curr == async) {
190 54f254f9 aliguori
            *prev = curr->next;
191 54f254f9 aliguori
            return;
192 54f254f9 aliguori
        }
193 54f254f9 aliguori
194 54f254f9 aliguori
        prev = &curr->next;
195 54f254f9 aliguori
        curr = curr->next;
196 54f254f9 aliguori
    }
197 54f254f9 aliguori
}
198 54f254f9 aliguori
199 54f254f9 aliguori
static void uhci_async_cancel(UHCIState *s, UHCIAsync *async)
200 54f254f9 aliguori
{
201 54f254f9 aliguori
    dprintf("uhci: cancel td 0x%x token 0x%x done %u\n",
202 54f254f9 aliguori
           async->td, async->token, async->done);
203 54f254f9 aliguori
204 54f254f9 aliguori
    if (!async->done)
205 54f254f9 aliguori
        usb_cancel_packet(&async->packet);
206 54f254f9 aliguori
    uhci_async_free(s, async);
207 54f254f9 aliguori
}
208 54f254f9 aliguori
209 54f254f9 aliguori
/*
210 54f254f9 aliguori
 * Mark all outstanding async packets as invalid.
211 54f254f9 aliguori
 * This is used for canceling them when TDs are removed by the HCD.
212 54f254f9 aliguori
 */
213 54f254f9 aliguori
static UHCIAsync *uhci_async_validate_begin(UHCIState *s)
214 54f254f9 aliguori
{
215 54f254f9 aliguori
    UHCIAsync *async = s->async_pending;
216 54f254f9 aliguori
217 54f254f9 aliguori
    while (async) {
218 54f254f9 aliguori
        async->valid--;
219 54f254f9 aliguori
        async = async->next;
220 54f254f9 aliguori
    }
221 54f254f9 aliguori
    return NULL;
222 54f254f9 aliguori
}
223 54f254f9 aliguori
224 54f254f9 aliguori
/*
225 54f254f9 aliguori
 * Cancel async packets that are no longer valid
226 54f254f9 aliguori
 */
227 54f254f9 aliguori
static void uhci_async_validate_end(UHCIState *s)
228 54f254f9 aliguori
{
229 54f254f9 aliguori
    UHCIAsync *curr = s->async_pending;
230 54f254f9 aliguori
    UHCIAsync **prev = &s->async_pending;
231 54f254f9 aliguori
    UHCIAsync *next;
232 54f254f9 aliguori
233 54f254f9 aliguori
    while (curr) {
234 54f254f9 aliguori
        if (curr->valid > 0) {
235 54f254f9 aliguori
            prev = &curr->next;
236 54f254f9 aliguori
            curr = curr->next;
237 54f254f9 aliguori
            continue;
238 54f254f9 aliguori
        }
239 54f254f9 aliguori
240 54f254f9 aliguori
        next = curr->next;
241 54f254f9 aliguori
242 54f254f9 aliguori
        /* Unlink */
243 54f254f9 aliguori
        *prev = next;
244 54f254f9 aliguori
245 54f254f9 aliguori
        uhci_async_cancel(s, curr);
246 54f254f9 aliguori
247 54f254f9 aliguori
        curr = next;
248 54f254f9 aliguori
    }
249 54f254f9 aliguori
}
250 54f254f9 aliguori
251 54f254f9 aliguori
static void uhci_async_cancel_all(UHCIState *s)
252 54f254f9 aliguori
{
253 54f254f9 aliguori
    UHCIAsync *curr = s->async_pending;
254 54f254f9 aliguori
    UHCIAsync *next;
255 54f254f9 aliguori
256 54f254f9 aliguori
    while (curr) {
257 54f254f9 aliguori
        next = curr->next;
258 54f254f9 aliguori
259 54f254f9 aliguori
        uhci_async_cancel(s, curr);
260 54f254f9 aliguori
261 54f254f9 aliguori
        curr = next;
262 54f254f9 aliguori
    }
263 54f254f9 aliguori
264 54f254f9 aliguori
    s->async_pending = NULL;
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 54f254f9 aliguori
    UHCIAsync *async = s->async_pending;
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 54f254f9 aliguori
    while (async) {
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 54f254f9 aliguori
297 54f254f9 aliguori
        async = async->next;
298 e8ee3c72 aurel32
        count++;
299 54f254f9 aliguori
    }
300 e8ee3c72 aurel32
301 e8ee3c72 aurel32
    if (count > 64)
302 e8ee3c72 aurel32
        fprintf(stderr, "uhci: warning lots of async transactions\n");
303 e8ee3c72 aurel32
304 e8ee3c72 aurel32
    return match;
305 54f254f9 aliguori
}
306 54f254f9 aliguori
307 bb36d470 bellard
static void uhci_attach(USBPort *port1, USBDevice *dev);
308 bb36d470 bellard
309 bb36d470 bellard
static void uhci_update_irq(UHCIState *s)
310 bb36d470 bellard
{
311 bb36d470 bellard
    int level;
312 bb36d470 bellard
    if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
313 bb36d470 bellard
        ((s->status2 & 2) && (s->intr & (1 << 3))) ||
314 bb36d470 bellard
        ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
315 bb36d470 bellard
        ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
316 bb36d470 bellard
        (s->status & UHCI_STS_HSERR) ||
317 bb36d470 bellard
        (s->status & UHCI_STS_HCPERR)) {
318 bb36d470 bellard
        level = 1;
319 bb36d470 bellard
    } else {
320 bb36d470 bellard
        level = 0;
321 bb36d470 bellard
    }
322 d537cf6c pbrook
    qemu_set_irq(s->dev.irq[3], level);
323 bb36d470 bellard
}
324 bb36d470 bellard
325 c8075ac3 Gleb Natapov
static void uhci_reset(void *opaque)
326 bb36d470 bellard
{
327 c8075ac3 Gleb Natapov
    UHCIState *s = opaque;
328 bb36d470 bellard
    uint8_t *pci_conf;
329 bb36d470 bellard
    int i;
330 bb36d470 bellard
    UHCIPort *port;
331 bb36d470 bellard
332 6f382b5e aliguori
    dprintf("uhci: full reset\n");
333 6f382b5e aliguori
334 bb36d470 bellard
    pci_conf = s->dev.config;
335 bb36d470 bellard
336 bb36d470 bellard
    pci_conf[0x6a] = 0x01; /* usb clock */
337 bb36d470 bellard
    pci_conf[0x6b] = 0x00;
338 bb36d470 bellard
    s->cmd = 0;
339 bb36d470 bellard
    s->status = 0;
340 bb36d470 bellard
    s->status2 = 0;
341 bb36d470 bellard
    s->intr = 0;
342 bb36d470 bellard
    s->fl_base_addr = 0;
343 bb36d470 bellard
    s->sof_timing = 64;
344 54f254f9 aliguori
345 bb36d470 bellard
    for(i = 0; i < NB_PORTS; i++) {
346 bb36d470 bellard
        port = &s->ports[i];
347 bb36d470 bellard
        port->ctrl = 0x0080;
348 a594cfbf bellard
        if (port->port.dev)
349 a594cfbf bellard
            uhci_attach(&port->port, port->port.dev);
350 bb36d470 bellard
    }
351 54f254f9 aliguori
352 54f254f9 aliguori
    uhci_async_cancel_all(s);
353 bb36d470 bellard
}
354 bb36d470 bellard
355 817afc61 Juan Quintela
static void uhci_pre_save(void *opaque)
356 b9dc033c balrog
{
357 b9dc033c balrog
    UHCIState *s = opaque;
358 b9dc033c balrog
359 6f382b5e aliguori
    uhci_async_cancel_all(s);
360 b9dc033c balrog
}
361 b9dc033c balrog
362 817afc61 Juan Quintela
static const VMStateDescription vmstate_uhci_port = {
363 817afc61 Juan Quintela
    .name = "uhci port",
364 817afc61 Juan Quintela
    .version_id = 1,
365 817afc61 Juan Quintela
    .minimum_version_id = 1,
366 817afc61 Juan Quintela
    .minimum_version_id_old = 1,
367 817afc61 Juan Quintela
    .fields      = (VMStateField []) {
368 817afc61 Juan Quintela
        VMSTATE_UINT16(ctrl, UHCIPort),
369 817afc61 Juan Quintela
        VMSTATE_END_OF_LIST()
370 817afc61 Juan Quintela
    }
371 817afc61 Juan Quintela
};
372 817afc61 Juan Quintela
373 817afc61 Juan Quintela
static const VMStateDescription vmstate_uhci = {
374 817afc61 Juan Quintela
    .name = "uhci",
375 817afc61 Juan Quintela
    .version_id = 1,
376 817afc61 Juan Quintela
    .minimum_version_id = 1,
377 817afc61 Juan Quintela
    .minimum_version_id_old = 1,
378 817afc61 Juan Quintela
    .pre_save = uhci_pre_save,
379 817afc61 Juan Quintela
    .fields      = (VMStateField []) {
380 817afc61 Juan Quintela
        VMSTATE_PCI_DEVICE(dev, UHCIState),
381 817afc61 Juan Quintela
        VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
382 817afc61 Juan Quintela
        VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
383 817afc61 Juan Quintela
                             vmstate_uhci_port, UHCIPort),
384 817afc61 Juan Quintela
        VMSTATE_UINT16(cmd, UHCIState),
385 817afc61 Juan Quintela
        VMSTATE_UINT16(status, UHCIState),
386 817afc61 Juan Quintela
        VMSTATE_UINT16(intr, UHCIState),
387 817afc61 Juan Quintela
        VMSTATE_UINT16(frnum, UHCIState),
388 817afc61 Juan Quintela
        VMSTATE_UINT32(fl_base_addr, UHCIState),
389 817afc61 Juan Quintela
        VMSTATE_UINT8(sof_timing, UHCIState),
390 817afc61 Juan Quintela
        VMSTATE_UINT8(status2, UHCIState),
391 817afc61 Juan Quintela
        VMSTATE_TIMER(frame_timer, UHCIState),
392 817afc61 Juan Quintela
        VMSTATE_END_OF_LIST()
393 817afc61 Juan Quintela
    }
394 817afc61 Juan Quintela
};
395 b9dc033c balrog
396 bb36d470 bellard
static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
397 bb36d470 bellard
{
398 bb36d470 bellard
    UHCIState *s = opaque;
399 3b46e624 ths
400 bb36d470 bellard
    addr &= 0x1f;
401 bb36d470 bellard
    switch(addr) {
402 bb36d470 bellard
    case 0x0c:
403 bb36d470 bellard
        s->sof_timing = val;
404 bb36d470 bellard
        break;
405 bb36d470 bellard
    }
406 bb36d470 bellard
}
407 bb36d470 bellard
408 bb36d470 bellard
static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
409 bb36d470 bellard
{
410 bb36d470 bellard
    UHCIState *s = opaque;
411 bb36d470 bellard
    uint32_t val;
412 bb36d470 bellard
413 bb36d470 bellard
    addr &= 0x1f;
414 bb36d470 bellard
    switch(addr) {
415 bb36d470 bellard
    case 0x0c:
416 bb36d470 bellard
        val = s->sof_timing;
417 d80cfb3f pbrook
        break;
418 bb36d470 bellard
    default:
419 bb36d470 bellard
        val = 0xff;
420 bb36d470 bellard
        break;
421 bb36d470 bellard
    }
422 bb36d470 bellard
    return val;
423 bb36d470 bellard
}
424 bb36d470 bellard
425 bb36d470 bellard
static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
426 bb36d470 bellard
{
427 bb36d470 bellard
    UHCIState *s = opaque;
428 3b46e624 ths
429 bb36d470 bellard
    addr &= 0x1f;
430 54f254f9 aliguori
    dprintf("uhci: writew port=0x%04x val=0x%04x\n", addr, val);
431 54f254f9 aliguori
432 bb36d470 bellard
    switch(addr) {
433 bb36d470 bellard
    case 0x00:
434 bb36d470 bellard
        if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
435 bb36d470 bellard
            /* start frame processing */
436 bb36d470 bellard
            qemu_mod_timer(s->frame_timer, qemu_get_clock(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 bb36d470 bellard
                if (dev) {
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 bb36d470 bellard
            if (dev) {
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 bb36d470 bellard
            port->ctrl = (port->ctrl & 0x01fb) | (val & ~0x01fb);
498 bb36d470 bellard
            /* some bits are reset when a '1' is written to them */
499 bb36d470 bellard
            port->ctrl &= ~(val & 0x000a);
500 bb36d470 bellard
        }
501 bb36d470 bellard
        break;
502 bb36d470 bellard
    }
503 bb36d470 bellard
}
504 bb36d470 bellard
505 bb36d470 bellard
static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
506 bb36d470 bellard
{
507 bb36d470 bellard
    UHCIState *s = opaque;
508 bb36d470 bellard
    uint32_t val;
509 bb36d470 bellard
510 bb36d470 bellard
    addr &= 0x1f;
511 bb36d470 bellard
    switch(addr) {
512 bb36d470 bellard
    case 0x00:
513 bb36d470 bellard
        val = s->cmd;
514 bb36d470 bellard
        break;
515 bb36d470 bellard
    case 0x02:
516 bb36d470 bellard
        val = s->status;
517 bb36d470 bellard
        break;
518 bb36d470 bellard
    case 0x04:
519 bb36d470 bellard
        val = s->intr;
520 bb36d470 bellard
        break;
521 bb36d470 bellard
    case 0x06:
522 bb36d470 bellard
        val = s->frnum;
523 bb36d470 bellard
        break;
524 bb36d470 bellard
    case 0x10 ... 0x1f:
525 bb36d470 bellard
        {
526 bb36d470 bellard
            UHCIPort *port;
527 bb36d470 bellard
            int n;
528 bb36d470 bellard
            n = (addr >> 1) & 7;
529 5fafdf24 ths
            if (n >= NB_PORTS)
530 bb36d470 bellard
                goto read_default;
531 bb36d470 bellard
            port = &s->ports[n];
532 bb36d470 bellard
            val = port->ctrl;
533 bb36d470 bellard
        }
534 bb36d470 bellard
        break;
535 bb36d470 bellard
    default:
536 bb36d470 bellard
    read_default:
537 bb36d470 bellard
        val = 0xff7f; /* disabled port */
538 bb36d470 bellard
        break;
539 bb36d470 bellard
    }
540 54f254f9 aliguori
541 54f254f9 aliguori
    dprintf("uhci: readw port=0x%04x val=0x%04x\n", addr, val);
542 54f254f9 aliguori
543 bb36d470 bellard
    return val;
544 bb36d470 bellard
}
545 bb36d470 bellard
546 bb36d470 bellard
static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
547 bb36d470 bellard
{
548 bb36d470 bellard
    UHCIState *s = opaque;
549 bb36d470 bellard
550 bb36d470 bellard
    addr &= 0x1f;
551 54f254f9 aliguori
    dprintf("uhci: writel port=0x%04x val=0x%08x\n", addr, val);
552 54f254f9 aliguori
553 bb36d470 bellard
    switch(addr) {
554 bb36d470 bellard
    case 0x08:
555 bb36d470 bellard
        s->fl_base_addr = val & ~0xfff;
556 bb36d470 bellard
        break;
557 bb36d470 bellard
    }
558 bb36d470 bellard
}
559 bb36d470 bellard
560 bb36d470 bellard
static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
561 bb36d470 bellard
{
562 bb36d470 bellard
    UHCIState *s = opaque;
563 bb36d470 bellard
    uint32_t val;
564 bb36d470 bellard
565 bb36d470 bellard
    addr &= 0x1f;
566 bb36d470 bellard
    switch(addr) {
567 bb36d470 bellard
    case 0x08:
568 bb36d470 bellard
        val = s->fl_base_addr;
569 bb36d470 bellard
        break;
570 bb36d470 bellard
    default:
571 bb36d470 bellard
        val = 0xffffffff;
572 bb36d470 bellard
        break;
573 bb36d470 bellard
    }
574 bb36d470 bellard
    return val;
575 bb36d470 bellard
}
576 bb36d470 bellard
577 96217e31 ths
/* signal resume if controller suspended */
578 96217e31 ths
static void uhci_resume (void *opaque)
579 96217e31 ths
{
580 96217e31 ths
    UHCIState *s = (UHCIState *)opaque;
581 96217e31 ths
582 96217e31 ths
    if (!s)
583 96217e31 ths
        return;
584 96217e31 ths
585 96217e31 ths
    if (s->cmd & UHCI_CMD_EGSM) {
586 96217e31 ths
        s->cmd |= UHCI_CMD_FGR;
587 96217e31 ths
        s->status |= UHCI_STS_RD;
588 96217e31 ths
        uhci_update_irq(s);
589 96217e31 ths
    }
590 96217e31 ths
}
591 96217e31 ths
592 bb36d470 bellard
static void uhci_attach(USBPort *port1, USBDevice *dev)
593 bb36d470 bellard
{
594 bb36d470 bellard
    UHCIState *s = port1->opaque;
595 bb36d470 bellard
    UHCIPort *port = &s->ports[port1->index];
596 bb36d470 bellard
597 bb36d470 bellard
    if (dev) {
598 a594cfbf bellard
        if (port->port.dev) {
599 bb36d470 bellard
            usb_attach(port1, NULL);
600 bb36d470 bellard
        }
601 bb36d470 bellard
        /* set connect status */
602 61064870 pbrook
        port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
603 61064870 pbrook
604 bb36d470 bellard
        /* update speed */
605 bb36d470 bellard
        if (dev->speed == USB_SPEED_LOW)
606 bb36d470 bellard
            port->ctrl |= UHCI_PORT_LSDA;
607 bb36d470 bellard
        else
608 bb36d470 bellard
            port->ctrl &= ~UHCI_PORT_LSDA;
609 96217e31 ths
610 96217e31 ths
        uhci_resume(s);
611 96217e31 ths
612 a594cfbf bellard
        port->port.dev = dev;
613 bb36d470 bellard
        /* send the attach message */
614 4d611c9a pbrook
        usb_send_msg(dev, USB_MSG_ATTACH);
615 bb36d470 bellard
    } else {
616 bb36d470 bellard
        /* set connect status */
617 61064870 pbrook
        if (port->ctrl & UHCI_PORT_CCS) {
618 61064870 pbrook
            port->ctrl &= ~UHCI_PORT_CCS;
619 61064870 pbrook
            port->ctrl |= UHCI_PORT_CSC;
620 bb36d470 bellard
        }
621 bb36d470 bellard
        /* disable port */
622 bb36d470 bellard
        if (port->ctrl & UHCI_PORT_EN) {
623 bb36d470 bellard
            port->ctrl &= ~UHCI_PORT_EN;
624 bb36d470 bellard
            port->ctrl |= UHCI_PORT_ENC;
625 bb36d470 bellard
        }
626 96217e31 ths
627 96217e31 ths
        uhci_resume(s);
628 96217e31 ths
629 a594cfbf bellard
        dev = port->port.dev;
630 bb36d470 bellard
        if (dev) {
631 bb36d470 bellard
            /* send the detach message */
632 4d611c9a pbrook
            usb_send_msg(dev, USB_MSG_DETACH);
633 bb36d470 bellard
        }
634 a594cfbf bellard
        port->port.dev = NULL;
635 bb36d470 bellard
    }
636 bb36d470 bellard
}
637 bb36d470 bellard
638 4d611c9a pbrook
static int uhci_broadcast_packet(UHCIState *s, USBPacket *p)
639 bb36d470 bellard
{
640 bb36d470 bellard
    int i, ret;
641 bb36d470 bellard
642 54f254f9 aliguori
    dprintf("uhci: packet enter. pid %s addr 0x%02x ep %d len %d\n",
643 54f254f9 aliguori
           pid2str(p->pid), p->devaddr, p->devep, p->len);
644 5d808245 aurel32
    if (p->pid == USB_TOKEN_OUT || p->pid == USB_TOKEN_SETUP)
645 54f254f9 aliguori
        dump_data(p->data, p->len);
646 54f254f9 aliguori
647 54f254f9 aliguori
    ret = USB_RET_NODEV;
648 54f254f9 aliguori
    for (i = 0; i < NB_PORTS && ret == USB_RET_NODEV; i++) {
649 54f254f9 aliguori
        UHCIPort *port = &s->ports[i];
650 54f254f9 aliguori
        USBDevice *dev = port->port.dev;
651 54f254f9 aliguori
652 54f254f9 aliguori
        if (dev && (port->ctrl & UHCI_PORT_EN))
653 806b6024 Gerd Hoffmann
            ret = dev->info->handle_packet(dev, p);
654 bb36d470 bellard
    }
655 54f254f9 aliguori
656 54f254f9 aliguori
    dprintf("uhci: packet exit. ret %d len %d\n", ret, p->len);
657 54f254f9 aliguori
    if (p->pid == USB_TOKEN_IN && ret > 0)
658 54f254f9 aliguori
        dump_data(p->data, ret);
659 54f254f9 aliguori
660 54f254f9 aliguori
    return ret;
661 bb36d470 bellard
}
662 bb36d470 bellard
663 54f254f9 aliguori
static void uhci_async_complete(USBPacket * packet, void *opaque);
664 54f254f9 aliguori
static void uhci_process_frame(UHCIState *s);
665 4d611c9a pbrook
666 bb36d470 bellard
/* return -1 if fatal error (frame must be stopped)
667 bb36d470 bellard
          0 if TD successful
668 bb36d470 bellard
          1 if TD unsuccessful or inactive
669 bb36d470 bellard
*/
670 54f254f9 aliguori
static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
671 bb36d470 bellard
{
672 54f254f9 aliguori
    int len = 0, max_len, err, ret;
673 bb36d470 bellard
    uint8_t pid;
674 bb36d470 bellard
675 54f254f9 aliguori
    max_len = ((td->token >> 21) + 1) & 0x7ff;
676 54f254f9 aliguori
    pid = td->token & 0xff;
677 54f254f9 aliguori
678 54f254f9 aliguori
    ret = async->packet.len;
679 54f254f9 aliguori
680 54f254f9 aliguori
    if (td->ctrl & TD_CTRL_IOC)
681 bb36d470 bellard
        *int_mask |= 0x01;
682 3b46e624 ths
683 54f254f9 aliguori
    if (td->ctrl & TD_CTRL_IOS)
684 54f254f9 aliguori
        td->ctrl &= ~TD_CTRL_ACTIVE;
685 bb36d470 bellard
686 54f254f9 aliguori
    if (ret < 0)
687 54f254f9 aliguori
        goto out;
688 b9dc033c balrog
689 54f254f9 aliguori
    len = async->packet.len;
690 54f254f9 aliguori
    td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
691 54f254f9 aliguori
692 54f254f9 aliguori
    /* The NAK bit may have been set by a previous frame, so clear it
693 54f254f9 aliguori
       here.  The docs are somewhat unclear, but win2k relies on this
694 54f254f9 aliguori
       behavior.  */
695 54f254f9 aliguori
    td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
696 54f254f9 aliguori
697 54f254f9 aliguori
    if (pid == USB_TOKEN_IN) {
698 54f254f9 aliguori
        if (len > max_len) {
699 4d611c9a pbrook
            len = max_len;
700 54f254f9 aliguori
            ret = USB_RET_BABBLE;
701 54f254f9 aliguori
            goto out;
702 4d611c9a pbrook
        }
703 b9dc033c balrog
704 54f254f9 aliguori
        if (len > 0) {
705 54f254f9 aliguori
            /* write the data back */
706 54f254f9 aliguori
            cpu_physical_memory_write(td->buffer, async->buffer, len);
707 54f254f9 aliguori
        }
708 54f254f9 aliguori
709 54f254f9 aliguori
        if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
710 bb36d470 bellard
            *int_mask |= 0x02;
711 bb36d470 bellard
            /* short packet: do not update QH */
712 54f254f9 aliguori
            dprintf("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token);
713 bb36d470 bellard
            return 1;
714 bb36d470 bellard
        }
715 54f254f9 aliguori
    }
716 54f254f9 aliguori
717 54f254f9 aliguori
    /* success */
718 54f254f9 aliguori
    return 0;
719 54f254f9 aliguori
720 54f254f9 aliguori
out:
721 54f254f9 aliguori
    switch(ret) {
722 54f254f9 aliguori
    case USB_RET_STALL:
723 54f254f9 aliguori
        td->ctrl |= TD_CTRL_STALL;
724 54f254f9 aliguori
        td->ctrl &= ~TD_CTRL_ACTIVE;
725 54f254f9 aliguori
        return 1;
726 54f254f9 aliguori
727 54f254f9 aliguori
    case USB_RET_BABBLE:
728 54f254f9 aliguori
        td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
729 54f254f9 aliguori
        td->ctrl &= ~TD_CTRL_ACTIVE;
730 54f254f9 aliguori
        /* frame interrupted */
731 54f254f9 aliguori
        return -1;
732 54f254f9 aliguori
733 54f254f9 aliguori
    case USB_RET_NAK:
734 54f254f9 aliguori
        td->ctrl |= TD_CTRL_NAK;
735 54f254f9 aliguori
        if (pid == USB_TOKEN_SETUP)
736 54f254f9 aliguori
            break;
737 54f254f9 aliguori
        return 1;
738 54f254f9 aliguori
739 54f254f9 aliguori
    case USB_RET_NODEV:
740 54f254f9 aliguori
    default:
741 54f254f9 aliguori
        break;
742 54f254f9 aliguori
    }
743 54f254f9 aliguori
744 54f254f9 aliguori
    /* Retry the TD if error count is not zero */
745 54f254f9 aliguori
746 54f254f9 aliguori
    td->ctrl |= TD_CTRL_TIMEOUT;
747 54f254f9 aliguori
    err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
748 54f254f9 aliguori
    if (err != 0) {
749 54f254f9 aliguori
        err--;
750 54f254f9 aliguori
        if (err == 0) {
751 bb36d470 bellard
            td->ctrl &= ~TD_CTRL_ACTIVE;
752 54f254f9 aliguori
            s->status |= UHCI_STS_USBERR;
753 54f254f9 aliguori
            uhci_update_irq(s);
754 bb36d470 bellard
        }
755 bb36d470 bellard
    }
756 54f254f9 aliguori
    td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
757 54f254f9 aliguori
        (err << TD_CTRL_ERROR_SHIFT);
758 54f254f9 aliguori
    return 1;
759 bb36d470 bellard
}
760 bb36d470 bellard
761 54f254f9 aliguori
static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask)
762 54f254f9 aliguori
{
763 54f254f9 aliguori
    UHCIAsync *async;
764 5d808245 aurel32
    int len = 0, max_len;
765 54f254f9 aliguori
    uint8_t pid;
766 54f254f9 aliguori
767 54f254f9 aliguori
    /* Is active ? */
768 54f254f9 aliguori
    if (!(td->ctrl & TD_CTRL_ACTIVE))
769 54f254f9 aliguori
        return 1;
770 54f254f9 aliguori
771 54f254f9 aliguori
    async = uhci_async_find_td(s, addr, td->token);
772 54f254f9 aliguori
    if (async) {
773 54f254f9 aliguori
        /* Already submitted */
774 a145ea51 aliguori
        async->valid = 32;
775 54f254f9 aliguori
776 54f254f9 aliguori
        if (!async->done)
777 54f254f9 aliguori
            return 1;
778 54f254f9 aliguori
779 54f254f9 aliguori
        uhci_async_unlink(s, async);
780 54f254f9 aliguori
        goto done;
781 54f254f9 aliguori
    }
782 54f254f9 aliguori
783 54f254f9 aliguori
    /* Allocate new packet */
784 54f254f9 aliguori
    async = uhci_async_alloc(s);
785 54f254f9 aliguori
    if (!async)
786 54f254f9 aliguori
        return 1;
787 54f254f9 aliguori
788 54f254f9 aliguori
    async->valid = 10;
789 54f254f9 aliguori
    async->td    = addr;
790 54f254f9 aliguori
    async->token = td->token;
791 54f254f9 aliguori
792 54f254f9 aliguori
    max_len = ((td->token >> 21) + 1) & 0x7ff;
793 54f254f9 aliguori
    pid = td->token & 0xff;
794 54f254f9 aliguori
795 54f254f9 aliguori
    async->packet.pid     = pid;
796 54f254f9 aliguori
    async->packet.devaddr = (td->token >> 8) & 0x7f;
797 54f254f9 aliguori
    async->packet.devep   = (td->token >> 15) & 0xf;
798 54f254f9 aliguori
    async->packet.data    = async->buffer;
799 54f254f9 aliguori
    async->packet.len     = max_len;
800 54f254f9 aliguori
    async->packet.complete_cb     = uhci_async_complete;
801 54f254f9 aliguori
    async->packet.complete_opaque = s;
802 54f254f9 aliguori
803 54f254f9 aliguori
    switch(pid) {
804 54f254f9 aliguori
    case USB_TOKEN_OUT:
805 54f254f9 aliguori
    case USB_TOKEN_SETUP:
806 54f254f9 aliguori
        cpu_physical_memory_read(td->buffer, async->buffer, max_len);
807 5d808245 aurel32
        len = uhci_broadcast_packet(s, &async->packet);
808 5d808245 aurel32
        if (len >= 0)
809 5d808245 aurel32
            len = max_len;
810 54f254f9 aliguori
        break;
811 54f254f9 aliguori
812 54f254f9 aliguori
    case USB_TOKEN_IN:
813 5d808245 aurel32
        len = uhci_broadcast_packet(s, &async->packet);
814 54f254f9 aliguori
        break;
815 54f254f9 aliguori
816 54f254f9 aliguori
    default:
817 54f254f9 aliguori
        /* invalid pid : frame interrupted */
818 54f254f9 aliguori
        uhci_async_free(s, async);
819 54f254f9 aliguori
        s->status |= UHCI_STS_HCPERR;
820 54f254f9 aliguori
        uhci_update_irq(s);
821 54f254f9 aliguori
        return -1;
822 54f254f9 aliguori
    }
823 54f254f9 aliguori
 
824 5d808245 aurel32
    if (len == USB_RET_ASYNC) {
825 54f254f9 aliguori
        uhci_async_link(s, async);
826 54f254f9 aliguori
        return 2;
827 54f254f9 aliguori
    }
828 54f254f9 aliguori
829 5d808245 aurel32
    async->packet.len = len;
830 54f254f9 aliguori
831 54f254f9 aliguori
done:
832 5d808245 aurel32
    len = uhci_complete_td(s, td, async, int_mask);
833 54f254f9 aliguori
    uhci_async_free(s, async);
834 5d808245 aurel32
    return len;
835 54f254f9 aliguori
}
836 54f254f9 aliguori
837 54f254f9 aliguori
static void uhci_async_complete(USBPacket *packet, void *opaque)
838 4d611c9a pbrook
{
839 4d611c9a pbrook
    UHCIState *s = opaque;
840 54f254f9 aliguori
    UHCIAsync *async = (UHCIAsync *) packet;
841 54f254f9 aliguori
842 54f254f9 aliguori
    dprintf("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token);
843 54f254f9 aliguori
844 54f254f9 aliguori
    async->done = 1;
845 54f254f9 aliguori
846 54f254f9 aliguori
    uhci_process_frame(s);
847 54f254f9 aliguori
}
848 54f254f9 aliguori
849 54f254f9 aliguori
static int is_valid(uint32_t link)
850 54f254f9 aliguori
{
851 54f254f9 aliguori
    return (link & 1) == 0;
852 54f254f9 aliguori
}
853 54f254f9 aliguori
854 54f254f9 aliguori
static int is_qh(uint32_t link)
855 54f254f9 aliguori
{
856 54f254f9 aliguori
    return (link & 2) != 0;
857 54f254f9 aliguori
}
858 54f254f9 aliguori
859 54f254f9 aliguori
static int depth_first(uint32_t link)
860 54f254f9 aliguori
{
861 54f254f9 aliguori
    return (link & 4) != 0;
862 54f254f9 aliguori
}
863 54f254f9 aliguori
864 54f254f9 aliguori
/* QH DB used for detecting QH loops */
865 54f254f9 aliguori
#define UHCI_MAX_QUEUES 128
866 54f254f9 aliguori
typedef struct {
867 54f254f9 aliguori
    uint32_t addr[UHCI_MAX_QUEUES];
868 54f254f9 aliguori
    int      count;
869 54f254f9 aliguori
} QhDb;
870 54f254f9 aliguori
871 54f254f9 aliguori
static void qhdb_reset(QhDb *db)
872 54f254f9 aliguori
{
873 54f254f9 aliguori
    db->count = 0;
874 54f254f9 aliguori
}
875 54f254f9 aliguori
876 54f254f9 aliguori
/* Add QH to DB. Returns 1 if already present or DB is full. */
877 54f254f9 aliguori
static int qhdb_insert(QhDb *db, uint32_t addr)
878 54f254f9 aliguori
{
879 54f254f9 aliguori
    int i;
880 54f254f9 aliguori
    for (i = 0; i < db->count; i++)
881 54f254f9 aliguori
        if (db->addr[i] == addr)
882 54f254f9 aliguori
            return 1;
883 54f254f9 aliguori
884 54f254f9 aliguori
    if (db->count >= UHCI_MAX_QUEUES)
885 54f254f9 aliguori
        return 1;
886 54f254f9 aliguori
887 54f254f9 aliguori
    db->addr[db->count++] = addr;
888 54f254f9 aliguori
    return 0;
889 54f254f9 aliguori
}
890 54f254f9 aliguori
891 54f254f9 aliguori
static void uhci_process_frame(UHCIState *s)
892 54f254f9 aliguori
{
893 54f254f9 aliguori
    uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
894 54f254f9 aliguori
    uint32_t curr_qh;
895 54f254f9 aliguori
    int cnt, ret;
896 4d611c9a pbrook
    UHCI_TD td;
897 54f254f9 aliguori
    UHCI_QH qh;
898 54f254f9 aliguori
    QhDb qhdb;
899 4d611c9a pbrook
900 54f254f9 aliguori
    frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
901 54f254f9 aliguori
902 54f254f9 aliguori
    dprintf("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr);
903 54f254f9 aliguori
904 54f254f9 aliguori
    cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4);
905 54f254f9 aliguori
    le32_to_cpus(&link);
906 b9dc033c balrog
907 54f254f9 aliguori
    int_mask = 0;
908 54f254f9 aliguori
    curr_qh  = 0;
909 54f254f9 aliguori
910 54f254f9 aliguori
    qhdb_reset(&qhdb);
911 54f254f9 aliguori
912 54f254f9 aliguori
    for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
913 54f254f9 aliguori
        if (is_qh(link)) {
914 54f254f9 aliguori
            /* QH */
915 54f254f9 aliguori
916 54f254f9 aliguori
            if (qhdb_insert(&qhdb, link)) {
917 54f254f9 aliguori
                /*
918 54f254f9 aliguori
                 * We're going in circles. Which is not a bug because
919 54f254f9 aliguori
                 * HCD is allowed to do that as part of the BW management. 
920 54f254f9 aliguori
                 * In our case though it makes no sense to spin here. Sync transations 
921 54f254f9 aliguori
                 * are already done, and async completion handler will re-process 
922 54f254f9 aliguori
                 * the frame when something is ready.
923 54f254f9 aliguori
                 */
924 54f254f9 aliguori
                dprintf("uhci: detected loop. qh 0x%x\n", link);
925 54f254f9 aliguori
                break;
926 54f254f9 aliguori
            }
927 54f254f9 aliguori
928 54f254f9 aliguori
            cpu_physical_memory_read(link & ~0xf, (uint8_t *) &qh, sizeof(qh));
929 54f254f9 aliguori
            le32_to_cpus(&qh.link);
930 54f254f9 aliguori
            le32_to_cpus(&qh.el_link);
931 54f254f9 aliguori
932 54f254f9 aliguori
            dprintf("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
933 54f254f9 aliguori
                    link, qh.link, qh.el_link);
934 54f254f9 aliguori
935 54f254f9 aliguori
            if (!is_valid(qh.el_link)) {
936 54f254f9 aliguori
                /* QH w/o elements */
937 54f254f9 aliguori
                curr_qh = 0;
938 54f254f9 aliguori
                link = qh.link;
939 54f254f9 aliguori
            } else {
940 54f254f9 aliguori
                /* QH with elements */
941 54f254f9 aliguori
                    curr_qh = link;
942 54f254f9 aliguori
                    link = qh.el_link;
943 54f254f9 aliguori
            }
944 54f254f9 aliguori
            continue;
945 54f254f9 aliguori
        }
946 54f254f9 aliguori
947 54f254f9 aliguori
        /* TD */
948 54f254f9 aliguori
        cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td));
949 b9dc033c balrog
        le32_to_cpus(&td.link);
950 b9dc033c balrog
        le32_to_cpus(&td.ctrl);
951 b9dc033c balrog
        le32_to_cpus(&td.token);
952 b9dc033c balrog
        le32_to_cpus(&td.buffer);
953 b9dc033c balrog
954 54f254f9 aliguori
        dprintf("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n", 
955 54f254f9 aliguori
                link, td.link, td.ctrl, td.token, curr_qh);
956 54f254f9 aliguori
957 54f254f9 aliguori
        old_td_ctrl = td.ctrl;
958 54f254f9 aliguori
        ret = uhci_handle_td(s, link, &td, &int_mask);
959 b9dc033c balrog
        if (old_td_ctrl != td.ctrl) {
960 54f254f9 aliguori
            /* update the status bits of the TD */
961 b9dc033c balrog
            val = cpu_to_le32(td.ctrl);
962 b9dc033c balrog
            cpu_physical_memory_write((link & ~0xf) + 4,
963 54f254f9 aliguori
                                      (const uint8_t *)&val, sizeof(val));
964 b9dc033c balrog
        }
965 54f254f9 aliguori
966 54f254f9 aliguori
        if (ret < 0) {
967 54f254f9 aliguori
            /* interrupted frame */
968 54f254f9 aliguori
            break;
969 b9dc033c balrog
        }
970 b9dc033c balrog
971 54f254f9 aliguori
        if (ret == 2 || ret == 1) {
972 54f254f9 aliguori
            dprintf("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
973 54f254f9 aliguori
                    link, ret == 2 ? "pend" : "skip",
974 54f254f9 aliguori
                    td.link, td.ctrl, td.token, curr_qh);
975 b9dc033c balrog
976 54f254f9 aliguori
            link = curr_qh ? qh.link : td.link;
977 54f254f9 aliguori
            continue;
978 4d611c9a pbrook
        }
979 54f254f9 aliguori
980 54f254f9 aliguori
        /* completed TD */
981 54f254f9 aliguori
982 54f254f9 aliguori
        dprintf("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n", 
983 54f254f9 aliguori
                link, td.link, td.ctrl, td.token, curr_qh);
984 54f254f9 aliguori
985 54f254f9 aliguori
        link = td.link;
986 54f254f9 aliguori
987 54f254f9 aliguori
        if (curr_qh) {
988 54f254f9 aliguori
            /* update QH element link */
989 54f254f9 aliguori
            qh.el_link = link;
990 4d611c9a pbrook
            val = cpu_to_le32(qh.el_link);
991 54f254f9 aliguori
            cpu_physical_memory_write((curr_qh & ~0xf) + 4,
992 54f254f9 aliguori
                                          (const uint8_t *)&val, sizeof(val));
993 54f254f9 aliguori
994 54f254f9 aliguori
            if (!depth_first(link)) {
995 54f254f9 aliguori
               /* done with this QH */
996 54f254f9 aliguori
997 54f254f9 aliguori
               dprintf("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
998 54f254f9 aliguori
                       curr_qh, qh.link, qh.el_link);
999 54f254f9 aliguori
1000 54f254f9 aliguori
               curr_qh = 0;
1001 54f254f9 aliguori
               link    = qh.link;
1002 54f254f9 aliguori
            }
1003 4d611c9a pbrook
        }
1004 54f254f9 aliguori
1005 54f254f9 aliguori
        /* go to the next entry */
1006 4d611c9a pbrook
    }
1007 54f254f9 aliguori
1008 54f254f9 aliguori
    s->pending_int_mask = int_mask;
1009 4d611c9a pbrook
}
1010 4d611c9a pbrook
1011 bb36d470 bellard
static void uhci_frame_timer(void *opaque)
1012 bb36d470 bellard
{
1013 bb36d470 bellard
    UHCIState *s = opaque;
1014 bb36d470 bellard
    int64_t expire_time;
1015 bb36d470 bellard
1016 bb36d470 bellard
    if (!(s->cmd & UHCI_CMD_RS)) {
1017 54f254f9 aliguori
        /* Full stop */
1018 bb36d470 bellard
        qemu_del_timer(s->frame_timer);
1019 52328140 bellard
        /* set hchalted bit in status - UHCI11D 2.1.2 */
1020 52328140 bellard
        s->status |= UHCI_STS_HCHALTED;
1021 6f382b5e aliguori
1022 6f382b5e aliguori
        dprintf("uhci: halted\n");
1023 bb36d470 bellard
        return;
1024 bb36d470 bellard
    }
1025 54f254f9 aliguori
1026 54f254f9 aliguori
    /* Complete the previous frame */
1027 4d611c9a pbrook
    if (s->pending_int_mask) {
1028 4d611c9a pbrook
        s->status2 |= s->pending_int_mask;
1029 54f254f9 aliguori
        s->status  |= UHCI_STS_USBINT;
1030 4d611c9a pbrook
        uhci_update_irq(s);
1031 4d611c9a pbrook
    }
1032 b9dc033c balrog
1033 54f254f9 aliguori
    /* Start new frame */
1034 54f254f9 aliguori
    s->frnum = (s->frnum + 1) & 0x7ff;
1035 54f254f9 aliguori
1036 54f254f9 aliguori
    dprintf("uhci: new frame #%u\n" , s->frnum);
1037 54f254f9 aliguori
1038 54f254f9 aliguori
    uhci_async_validate_begin(s);
1039 54f254f9 aliguori
1040 54f254f9 aliguori
    uhci_process_frame(s);
1041 54f254f9 aliguori
1042 54f254f9 aliguori
    uhci_async_validate_end(s);
1043 b9dc033c balrog
1044 bb36d470 bellard
    /* prepare the timer for the next frame */
1045 5fafdf24 ths
    expire_time = qemu_get_clock(vm_clock) +
1046 6ee093c9 Juan Quintela
        (get_ticks_per_sec() / FRAME_TIMER_FREQ);
1047 bb36d470 bellard
    qemu_mod_timer(s->frame_timer, expire_time);
1048 bb36d470 bellard
}
1049 bb36d470 bellard
1050 5fafdf24 ths
static void uhci_map(PCIDevice *pci_dev, int region_num,
1051 6e355d90 Isaku Yamahata
                    pcibus_t addr, pcibus_t size, int type)
1052 bb36d470 bellard
{
1053 bb36d470 bellard
    UHCIState *s = (UHCIState *)pci_dev;
1054 bb36d470 bellard
1055 bb36d470 bellard
    register_ioport_write(addr, 32, 2, uhci_ioport_writew, s);
1056 bb36d470 bellard
    register_ioport_read(addr, 32, 2, uhci_ioport_readw, s);
1057 bb36d470 bellard
    register_ioport_write(addr, 32, 4, uhci_ioport_writel, s);
1058 bb36d470 bellard
    register_ioport_read(addr, 32, 4, uhci_ioport_readl, s);
1059 bb36d470 bellard
    register_ioport_write(addr, 32, 1, uhci_ioport_writeb, s);
1060 bb36d470 bellard
    register_ioport_read(addr, 32, 1, uhci_ioport_readb, s);
1061 bb36d470 bellard
}
1062 bb36d470 bellard
1063 6cf9b6f1 Gerd Hoffmann
static int usb_uhci_common_initfn(UHCIState *s)
1064 bb36d470 bellard
{
1065 6cf9b6f1 Gerd Hoffmann
    uint8_t *pci_conf = s->dev.config;
1066 bb36d470 bellard
    int i;
1067 bb36d470 bellard
1068 bb36d470 bellard
    pci_conf[0x08] = 0x01; // revision number
1069 bb36d470 bellard
    pci_conf[0x09] = 0x00;
1070 173a543b blueswir1
    pci_config_set_class(pci_conf, PCI_CLASS_SERIAL_USB);
1071 6407f373 Isaku Yamahata
    pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
1072 f04308e4 bellard
    pci_conf[0x3d] = 4; // interrupt pin 3
1073 38ca0f6d pbrook
    pci_conf[0x60] = 0x10; // release number
1074 3b46e624 ths
1075 b2317837 Gerd Hoffmann
    usb_bus_new(&s->bus, &s->dev.qdev);
1076 bb36d470 bellard
    for(i = 0; i < NB_PORTS; i++) {
1077 b2317837 Gerd Hoffmann
        usb_register_port(&s->bus, &s->ports[i].port, s, i, uhci_attach);
1078 bb36d470 bellard
    }
1079 bb36d470 bellard
    s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
1080 64e58fe5 Juan Quintela
    s->num_ports_vmstate = NB_PORTS;
1081 bb36d470 bellard
1082 a08d4367 Jan Kiszka
    qemu_register_reset(uhci_reset, s);
1083 bb36d470 bellard
1084 38ca0f6d pbrook
    /* Use region 4 for consistency with real hardware.  BSD guests seem
1085 38ca0f6d pbrook
       to rely on this.  */
1086 28c2c264 Avi Kivity
    pci_register_bar(&s->dev, 4, 0x20,
1087 0392a017 Isaku Yamahata
                           PCI_BASE_ADDRESS_SPACE_IO, uhci_map);
1088 6f382b5e aliguori
1089 817afc61 Juan Quintela
    vmstate_register(0, &vmstate_uhci, s);
1090 6cf9b6f1 Gerd Hoffmann
    return 0;
1091 bb36d470 bellard
}
1092 afcc3cdf ths
1093 6cf9b6f1 Gerd Hoffmann
static int usb_uhci_piix3_initfn(PCIDevice *dev)
1094 afcc3cdf ths
{
1095 6cf9b6f1 Gerd Hoffmann
    UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1096 6cf9b6f1 Gerd Hoffmann
    uint8_t *pci_conf = s->dev.config;
1097 6cf9b6f1 Gerd Hoffmann
1098 6cf9b6f1 Gerd Hoffmann
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
1099 6cf9b6f1 Gerd Hoffmann
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_2);
1100 6cf9b6f1 Gerd Hoffmann
    return usb_uhci_common_initfn(s);
1101 6cf9b6f1 Gerd Hoffmann
}
1102 6cf9b6f1 Gerd Hoffmann
1103 6cf9b6f1 Gerd Hoffmann
static int usb_uhci_piix4_initfn(PCIDevice *dev)
1104 6cf9b6f1 Gerd Hoffmann
{
1105 6cf9b6f1 Gerd Hoffmann
    UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1106 6cf9b6f1 Gerd Hoffmann
    uint8_t *pci_conf = s->dev.config;
1107 afcc3cdf ths
1108 deb54399 aliguori
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
1109 deb54399 aliguori
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_2);
1110 6cf9b6f1 Gerd Hoffmann
    return usb_uhci_common_initfn(s);
1111 6cf9b6f1 Gerd Hoffmann
}
1112 afcc3cdf ths
1113 6cf9b6f1 Gerd Hoffmann
static PCIDeviceInfo uhci_info[] = {
1114 6cf9b6f1 Gerd Hoffmann
    {
1115 6cf9b6f1 Gerd Hoffmann
        .qdev.name    = "PIIX3 USB-UHCI",
1116 6cf9b6f1 Gerd Hoffmann
        .qdev.size    = sizeof(UHCIState),
1117 6cf9b6f1 Gerd Hoffmann
        .init         = usb_uhci_piix3_initfn,
1118 6cf9b6f1 Gerd Hoffmann
    },{
1119 6cf9b6f1 Gerd Hoffmann
        .qdev.name    = "PIIX4 USB-UHCI",
1120 6cf9b6f1 Gerd Hoffmann
        .qdev.size    = sizeof(UHCIState),
1121 6cf9b6f1 Gerd Hoffmann
        .init         = usb_uhci_piix4_initfn,
1122 6cf9b6f1 Gerd Hoffmann
    },{
1123 6cf9b6f1 Gerd Hoffmann
        /* end of list */
1124 afcc3cdf ths
    }
1125 6cf9b6f1 Gerd Hoffmann
};
1126 afcc3cdf ths
1127 6cf9b6f1 Gerd Hoffmann
static void uhci_register(void)
1128 6cf9b6f1 Gerd Hoffmann
{
1129 6cf9b6f1 Gerd Hoffmann
    pci_qdev_register_many(uhci_info);
1130 6cf9b6f1 Gerd Hoffmann
}
1131 6cf9b6f1 Gerd Hoffmann
device_init(uhci_register);
1132 afcc3cdf ths
1133 6cf9b6f1 Gerd Hoffmann
void usb_uhci_piix3_init(PCIBus *bus, int devfn)
1134 6cf9b6f1 Gerd Hoffmann
{
1135 6cf9b6f1 Gerd Hoffmann
    pci_create_simple(bus, devfn, "PIIX3 USB-UHCI");
1136 6cf9b6f1 Gerd Hoffmann
}
1137 54f254f9 aliguori
1138 6cf9b6f1 Gerd Hoffmann
void usb_uhci_piix4_init(PCIBus *bus, int devfn)
1139 6cf9b6f1 Gerd Hoffmann
{
1140 6cf9b6f1 Gerd Hoffmann
    pci_create_simple(bus, devfn, "PIIX4 USB-UHCI");
1141 afcc3cdf ths
}