Statistics
| Branch: | Revision:

root / hw / usb-uhci.c @ d8becc35

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