Statistics
| Branch: | Revision:

root / hw / usb-uhci.c @ 618c169b

History | View | Annotate | Download (30.3 kB)

1
/*
2
 * USB UHCI controller emulation
3
 *
4
 * Copyright (c) 2005 Fabrice Bellard
5
 *
6
 * Copyright (c) 2008 Max Krasnyansky
7
 *     Magor rewrite of the UHCI data structures parser and frame processor
8
 *     Support for fully async operation and multiple outstanding transactions
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
#include "hw.h"
29
#include "usb.h"
30
#include "pci.h"
31
#include "qemu-timer.h"
32
#include "usb-uhci.h"
33

    
34
//#define DEBUG
35
//#define DEBUG_DUMP_DATA
36

    
37
#define UHCI_CMD_FGR      (1 << 4)
38
#define UHCI_CMD_EGSM     (1 << 3)
39
#define UHCI_CMD_GRESET   (1 << 2)
40
#define UHCI_CMD_HCRESET  (1 << 1)
41
#define UHCI_CMD_RS       (1 << 0)
42

    
43
#define UHCI_STS_HCHALTED (1 << 5)
44
#define UHCI_STS_HCPERR   (1 << 4)
45
#define UHCI_STS_HSERR    (1 << 3)
46
#define UHCI_STS_RD       (1 << 2)
47
#define UHCI_STS_USBERR   (1 << 1)
48
#define UHCI_STS_USBINT   (1 << 0)
49

    
50
#define TD_CTRL_SPD     (1 << 29)
51
#define TD_CTRL_ERROR_SHIFT  27
52
#define TD_CTRL_IOS     (1 << 25)
53
#define TD_CTRL_IOC     (1 << 24)
54
#define TD_CTRL_ACTIVE  (1 << 23)
55
#define TD_CTRL_STALL   (1 << 22)
56
#define TD_CTRL_BABBLE  (1 << 20)
57
#define TD_CTRL_NAK     (1 << 19)
58
#define TD_CTRL_TIMEOUT (1 << 18)
59

    
60
#define UHCI_PORT_RESET (1 << 9)
61
#define UHCI_PORT_LSDA  (1 << 8)
62
#define UHCI_PORT_ENC   (1 << 3)
63
#define UHCI_PORT_EN    (1 << 2)
64
#define UHCI_PORT_CSC   (1 << 1)
65
#define UHCI_PORT_CCS   (1 << 0)
66

    
67
#define FRAME_TIMER_FREQ 1000
68

    
69
#define FRAME_MAX_LOOPS  100
70

    
71
#define NB_PORTS 2
72

    
73
#ifdef DEBUG
74
#define DPRINTF printf
75

    
76
static const char *pid2str(int pid)
77
{
78
    switch (pid) {
79
    case USB_TOKEN_SETUP: return "SETUP";
80
    case USB_TOKEN_IN:    return "IN";
81
    case USB_TOKEN_OUT:   return "OUT";
82
    }
83
    return "?";
84
}
85

    
86
#else
87
#define DPRINTF(...)
88
#endif
89

    
90
#ifdef DEBUG_DUMP_DATA
91
static void dump_data(const uint8_t *data, int len)
92
{
93
    int i;
94

    
95
    printf("uhci: data: ");
96
    for(i = 0; i < len; i++)
97
        printf(" %02x", data[i]);
98
    printf("\n");
99
}
100
#else
101
static void dump_data(const uint8_t *data, int len) {}
102
#endif
103

    
104
/* 
105
 * Pending async transaction.
106
 * 'packet' must be the first field because completion
107
 * handler does "(UHCIAsync *) pkt" cast.
108
 */
109
typedef struct UHCIAsync {
110
    USBPacket packet;
111
    struct UHCIAsync *next;
112
    uint32_t  td;
113
    uint32_t  token;
114
    int8_t    valid;
115
    uint8_t   isoc;
116
    uint8_t   done;
117
    uint8_t   buffer[2048];
118
} UHCIAsync;
119

    
120
typedef struct UHCIPort {
121
    USBPort port;
122
    uint16_t ctrl;
123
} UHCIPort;
124

    
125
typedef struct UHCIState {
126
    PCIDevice dev;
127
    USBBus bus;
128
    uint16_t cmd; /* cmd register */
129
    uint16_t status;
130
    uint16_t intr; /* interrupt enable register */
131
    uint16_t frnum; /* frame number */
132
    uint32_t fl_base_addr; /* frame list base address */
133
    uint8_t sof_timing;
134
    uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
135
    int64_t expire_time;
136
    QEMUTimer *frame_timer;
137
    UHCIPort ports[NB_PORTS];
138

    
139
    /* Interrupts that should be raised at the end of the current frame.  */
140
    uint32_t pending_int_mask;
141

    
142
    /* Active packets */
143
    UHCIAsync *async_pending;
144
    UHCIAsync *async_pool;
145
    uint8_t num_ports_vmstate;
146
} UHCIState;
147

    
148
typedef struct UHCI_TD {
149
    uint32_t link;
150
    uint32_t ctrl; /* see TD_CTRL_xxx */
151
    uint32_t token;
152
    uint32_t buffer;
153
} UHCI_TD;
154

    
155
typedef struct UHCI_QH {
156
    uint32_t link;
157
    uint32_t el_link;
158
} UHCI_QH;
159

    
160
static UHCIAsync *uhci_async_alloc(UHCIState *s)
161
{
162
    UHCIAsync *async = qemu_malloc(sizeof(UHCIAsync));
163

    
164
    memset(&async->packet, 0, sizeof(async->packet));
165
    async->valid = 0;
166
    async->td    = 0;
167
    async->token = 0;
168
    async->done  = 0;
169
    async->isoc  = 0;
170
    async->next  = NULL;
171

    
172
    return async;
173
}
174

    
175
static void uhci_async_free(UHCIState *s, UHCIAsync *async)
176
{
177
    qemu_free(async);
178
}
179

    
180
static void uhci_async_link(UHCIState *s, UHCIAsync *async)
181
{
182
    async->next = s->async_pending;
183
    s->async_pending = async;
184
}
185

    
186
static void uhci_async_unlink(UHCIState *s, UHCIAsync *async)
187
{
188
    UHCIAsync *curr = s->async_pending;
189
    UHCIAsync **prev = &s->async_pending;
190

    
191
    while (curr) {
192
        if (curr == async) {
193
            *prev = curr->next;
194
            return;
195
        }
196

    
197
        prev = &curr->next;
198
        curr = curr->next;
199
    }
200
}
201

    
202
static void uhci_async_cancel(UHCIState *s, UHCIAsync *async)
203
{
204
    DPRINTF("uhci: cancel td 0x%x token 0x%x done %u\n",
205
           async->td, async->token, async->done);
206

    
207
    if (!async->done)
208
        usb_cancel_packet(&async->packet);
209
    uhci_async_free(s, async);
210
}
211

    
212
/*
213
 * Mark all outstanding async packets as invalid.
214
 * This is used for canceling them when TDs are removed by the HCD.
215
 */
216
static UHCIAsync *uhci_async_validate_begin(UHCIState *s)
217
{
218
    UHCIAsync *async = s->async_pending;
219

    
220
    while (async) {
221
        async->valid--;
222
        async = async->next;
223
    }
224
    return NULL;
225
}
226

    
227
/*
228
 * Cancel async packets that are no longer valid
229
 */
230
static void uhci_async_validate_end(UHCIState *s)
231
{
232
    UHCIAsync *curr = s->async_pending;
233
    UHCIAsync **prev = &s->async_pending;
234
    UHCIAsync *next;
235

    
236
    while (curr) {
237
        if (curr->valid > 0) {
238
            prev = &curr->next;
239
            curr = curr->next;
240
            continue;
241
        }
242

    
243
        next = curr->next;
244

    
245
        /* Unlink */
246
        *prev = next;
247

    
248
        uhci_async_cancel(s, curr);
249

    
250
        curr = next;
251
    }
252
}
253

    
254
static void uhci_async_cancel_all(UHCIState *s)
255
{
256
    UHCIAsync *curr = s->async_pending;
257
    UHCIAsync *next;
258

    
259
    while (curr) {
260
        next = curr->next;
261

    
262
        uhci_async_cancel(s, curr);
263

    
264
        curr = next;
265
    }
266

    
267
    s->async_pending = NULL;
268
}
269

    
270
static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t addr, uint32_t token)
271
{
272
    UHCIAsync *async = s->async_pending;
273
    UHCIAsync *match = NULL;
274
    int count = 0;
275

    
276
    /*
277
     * We're looking for the best match here. ie both td addr and token.
278
     * Otherwise we return last good match. ie just token.
279
     * It's ok to match just token because it identifies the transaction
280
     * rather well, token includes: device addr, endpoint, size, etc.
281
     *
282
     * Also since we queue async transactions in reverse order by returning
283
     * last good match we restores the order.
284
     *
285
     * It's expected that we wont have a ton of outstanding transactions.
286
     * If we ever do we'd want to optimize this algorithm.
287
     */
288

    
289
    while (async) {
290
        if (async->token == token) {
291
            /* Good match */
292
            match = async;
293

    
294
            if (async->td == addr) {
295
                /* Best match */
296
                break;
297
            }
298
        }
299

    
300
        async = async->next;
301
        count++;
302
    }
303

    
304
    if (count > 64)
305
        fprintf(stderr, "uhci: warning lots of async transactions\n");
306

    
307
    return match;
308
}
309

    
310
static void uhci_update_irq(UHCIState *s)
311
{
312
    int level;
313
    if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
314
        ((s->status2 & 2) && (s->intr & (1 << 3))) ||
315
        ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
316
        ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
317
        (s->status & UHCI_STS_HSERR) ||
318
        (s->status & UHCI_STS_HCPERR)) {
319
        level = 1;
320
    } else {
321
        level = 0;
322
    }
323
    qemu_set_irq(s->dev.irq[3], level);
324
}
325

    
326
static void uhci_reset(void *opaque)
327
{
328
    UHCIState *s = opaque;
329
    uint8_t *pci_conf;
330
    int i;
331
    UHCIPort *port;
332

    
333
    DPRINTF("uhci: full reset\n");
334

    
335
    pci_conf = s->dev.config;
336

    
337
    pci_conf[0x6a] = 0x01; /* usb clock */
338
    pci_conf[0x6b] = 0x00;
339
    s->cmd = 0;
340
    s->status = 0;
341
    s->status2 = 0;
342
    s->intr = 0;
343
    s->fl_base_addr = 0;
344
    s->sof_timing = 64;
345

    
346
    for(i = 0; i < NB_PORTS; i++) {
347
        port = &s->ports[i];
348
        port->ctrl = 0x0080;
349
        if (port->port.dev) {
350
            usb_attach(&port->port, port->port.dev);
351
        }
352
    }
353

    
354
    uhci_async_cancel_all(s);
355
}
356

    
357
static void uhci_pre_save(void *opaque)
358
{
359
    UHCIState *s = opaque;
360

    
361
    uhci_async_cancel_all(s);
362
}
363

    
364
static const VMStateDescription vmstate_uhci_port = {
365
    .name = "uhci port",
366
    .version_id = 1,
367
    .minimum_version_id = 1,
368
    .minimum_version_id_old = 1,
369
    .fields      = (VMStateField []) {
370
        VMSTATE_UINT16(ctrl, UHCIPort),
371
        VMSTATE_END_OF_LIST()
372
    }
373
};
374

    
375
static const VMStateDescription vmstate_uhci = {
376
    .name = "uhci",
377
    .version_id = 2,
378
    .minimum_version_id = 1,
379
    .minimum_version_id_old = 1,
380
    .pre_save = uhci_pre_save,
381
    .fields      = (VMStateField []) {
382
        VMSTATE_PCI_DEVICE(dev, UHCIState),
383
        VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
384
        VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
385
                             vmstate_uhci_port, UHCIPort),
386
        VMSTATE_UINT16(cmd, UHCIState),
387
        VMSTATE_UINT16(status, UHCIState),
388
        VMSTATE_UINT16(intr, UHCIState),
389
        VMSTATE_UINT16(frnum, UHCIState),
390
        VMSTATE_UINT32(fl_base_addr, UHCIState),
391
        VMSTATE_UINT8(sof_timing, UHCIState),
392
        VMSTATE_UINT8(status2, UHCIState),
393
        VMSTATE_TIMER(frame_timer, UHCIState),
394
        VMSTATE_INT64_V(expire_time, UHCIState, 2),
395
        VMSTATE_END_OF_LIST()
396
    }
397
};
398

    
399
static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
400
{
401
    UHCIState *s = opaque;
402

    
403
    addr &= 0x1f;
404
    switch(addr) {
405
    case 0x0c:
406
        s->sof_timing = val;
407
        break;
408
    }
409
}
410

    
411
static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
412
{
413
    UHCIState *s = opaque;
414
    uint32_t val;
415

    
416
    addr &= 0x1f;
417
    switch(addr) {
418
    case 0x0c:
419
        val = s->sof_timing;
420
        break;
421
    default:
422
        val = 0xff;
423
        break;
424
    }
425
    return val;
426
}
427

    
428
static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
429
{
430
    UHCIState *s = opaque;
431

    
432
    addr &= 0x1f;
433
    DPRINTF("uhci: writew port=0x%04x val=0x%04x\n", addr, val);
434

    
435
    switch(addr) {
436
    case 0x00:
437
        if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
438
            /* start frame processing */
439
            qemu_mod_timer(s->frame_timer, qemu_get_clock(vm_clock));
440
            s->status &= ~UHCI_STS_HCHALTED;
441
        } else if (!(val & UHCI_CMD_RS)) {
442
            s->status |= UHCI_STS_HCHALTED;
443
        }
444
        if (val & UHCI_CMD_GRESET) {
445
            UHCIPort *port;
446
            USBDevice *dev;
447
            int i;
448

    
449
            /* send reset on the USB bus */
450
            for(i = 0; i < NB_PORTS; i++) {
451
                port = &s->ports[i];
452
                dev = port->port.dev;
453
                if (dev) {
454
                    usb_send_msg(dev, USB_MSG_RESET);
455
                }
456
            }
457
            uhci_reset(s);
458
            return;
459
        }
460
        if (val & UHCI_CMD_HCRESET) {
461
            uhci_reset(s);
462
            return;
463
        }
464
        s->cmd = val;
465
        break;
466
    case 0x02:
467
        s->status &= ~val;
468
        /* XXX: the chip spec is not coherent, so we add a hidden
469
           register to distinguish between IOC and SPD */
470
        if (val & UHCI_STS_USBINT)
471
            s->status2 = 0;
472
        uhci_update_irq(s);
473
        break;
474
    case 0x04:
475
        s->intr = val;
476
        uhci_update_irq(s);
477
        break;
478
    case 0x06:
479
        if (s->status & UHCI_STS_HCHALTED)
480
            s->frnum = val & 0x7ff;
481
        break;
482
    case 0x10 ... 0x1f:
483
        {
484
            UHCIPort *port;
485
            USBDevice *dev;
486
            int n;
487

    
488
            n = (addr >> 1) & 7;
489
            if (n >= NB_PORTS)
490
                return;
491
            port = &s->ports[n];
492
            dev = port->port.dev;
493
            if (dev) {
494
                /* port reset */
495
                if ( (val & UHCI_PORT_RESET) &&
496
                     !(port->ctrl & UHCI_PORT_RESET) ) {
497
                    usb_send_msg(dev, USB_MSG_RESET);
498
                }
499
            }
500
            port->ctrl = (port->ctrl & 0x01fb) | (val & ~0x01fb);
501
            /* some bits are reset when a '1' is written to them */
502
            port->ctrl &= ~(val & 0x000a);
503
        }
504
        break;
505
    }
506
}
507

    
508
static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
509
{
510
    UHCIState *s = opaque;
511
    uint32_t val;
512

    
513
    addr &= 0x1f;
514
    switch(addr) {
515
    case 0x00:
516
        val = s->cmd;
517
        break;
518
    case 0x02:
519
        val = s->status;
520
        break;
521
    case 0x04:
522
        val = s->intr;
523
        break;
524
    case 0x06:
525
        val = s->frnum;
526
        break;
527
    case 0x10 ... 0x1f:
528
        {
529
            UHCIPort *port;
530
            int n;
531
            n = (addr >> 1) & 7;
532
            if (n >= NB_PORTS)
533
                goto read_default;
534
            port = &s->ports[n];
535
            val = port->ctrl;
536
        }
537
        break;
538
    default:
539
    read_default:
540
        val = 0xff7f; /* disabled port */
541
        break;
542
    }
543

    
544
    DPRINTF("uhci: readw port=0x%04x val=0x%04x\n", addr, val);
545

    
546
    return val;
547
}
548

    
549
static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
550
{
551
    UHCIState *s = opaque;
552

    
553
    addr &= 0x1f;
554
    DPRINTF("uhci: writel port=0x%04x val=0x%08x\n", addr, val);
555

    
556
    switch(addr) {
557
    case 0x08:
558
        s->fl_base_addr = val & ~0xfff;
559
        break;
560
    }
561
}
562

    
563
static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
564
{
565
    UHCIState *s = opaque;
566
    uint32_t val;
567

    
568
    addr &= 0x1f;
569
    switch(addr) {
570
    case 0x08:
571
        val = s->fl_base_addr;
572
        break;
573
    default:
574
        val = 0xffffffff;
575
        break;
576
    }
577
    return val;
578
}
579

    
580
/* signal resume if controller suspended */
581
static void uhci_resume (void *opaque)
582
{
583
    UHCIState *s = (UHCIState *)opaque;
584

    
585
    if (!s)
586
        return;
587

    
588
    if (s->cmd & UHCI_CMD_EGSM) {
589
        s->cmd |= UHCI_CMD_FGR;
590
        s->status |= UHCI_STS_RD;
591
        uhci_update_irq(s);
592
    }
593
}
594

    
595
static void uhci_attach(USBPort *port1)
596
{
597
    UHCIState *s = port1->opaque;
598
    UHCIPort *port = &s->ports[port1->index];
599

    
600
    /* set connect status */
601
    port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
602

    
603
    /* update speed */
604
    if (port->port.dev->speed == USB_SPEED_LOW) {
605
        port->ctrl |= UHCI_PORT_LSDA;
606
    } else {
607
        port->ctrl &= ~UHCI_PORT_LSDA;
608
    }
609

    
610
    uhci_resume(s);
611
}
612

    
613
static void uhci_detach(USBPort *port1)
614
{
615
    UHCIState *s = port1->opaque;
616
    UHCIPort *port = &s->ports[port1->index];
617

    
618
    /* set connect status */
619
    if (port->ctrl & UHCI_PORT_CCS) {
620
        port->ctrl &= ~UHCI_PORT_CCS;
621
        port->ctrl |= UHCI_PORT_CSC;
622
    }
623
    /* disable port */
624
    if (port->ctrl & UHCI_PORT_EN) {
625
        port->ctrl &= ~UHCI_PORT_EN;
626
        port->ctrl |= UHCI_PORT_ENC;
627
    }
628

    
629
    uhci_resume(s);
630
}
631

    
632
static int uhci_broadcast_packet(UHCIState *s, USBPacket *p)
633
{
634
    int i, ret;
635

    
636
    DPRINTF("uhci: packet enter. pid %s addr 0x%02x ep %d len %d\n",
637
           pid2str(p->pid), p->devaddr, p->devep, p->len);
638
    if (p->pid == USB_TOKEN_OUT || p->pid == USB_TOKEN_SETUP)
639
        dump_data(p->data, p->len);
640

    
641
    ret = USB_RET_NODEV;
642
    for (i = 0; i < NB_PORTS && ret == USB_RET_NODEV; i++) {
643
        UHCIPort *port = &s->ports[i];
644
        USBDevice *dev = port->port.dev;
645

    
646
        if (dev && (port->ctrl & UHCI_PORT_EN))
647
            ret = dev->info->handle_packet(dev, p);
648
    }
649

    
650
    DPRINTF("uhci: packet exit. ret %d len %d\n", ret, p->len);
651
    if (p->pid == USB_TOKEN_IN && ret > 0)
652
        dump_data(p->data, ret);
653

    
654
    return ret;
655
}
656

    
657
static void uhci_async_complete(USBPacket * packet, void *opaque);
658
static void uhci_process_frame(UHCIState *s);
659

    
660
/* return -1 if fatal error (frame must be stopped)
661
          0 if TD successful
662
          1 if TD unsuccessful or inactive
663
*/
664
static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
665
{
666
    int len = 0, max_len, err, ret;
667
    uint8_t pid;
668

    
669
    max_len = ((td->token >> 21) + 1) & 0x7ff;
670
    pid = td->token & 0xff;
671

    
672
    ret = async->packet.len;
673

    
674
    if (td->ctrl & TD_CTRL_IOS)
675
        td->ctrl &= ~TD_CTRL_ACTIVE;
676

    
677
    if (ret < 0)
678
        goto out;
679

    
680
    len = async->packet.len;
681
    td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
682

    
683
    /* The NAK bit may have been set by a previous frame, so clear it
684
       here.  The docs are somewhat unclear, but win2k relies on this
685
       behavior.  */
686
    td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
687
    if (td->ctrl & TD_CTRL_IOC)
688
        *int_mask |= 0x01;
689

    
690
    if (pid == USB_TOKEN_IN) {
691
        if (len > max_len) {
692
            ret = USB_RET_BABBLE;
693
            goto out;
694
        }
695

    
696
        if (len > 0) {
697
            /* write the data back */
698
            cpu_physical_memory_write(td->buffer, async->buffer, len);
699
        }
700

    
701
        if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
702
            *int_mask |= 0x02;
703
            /* short packet: do not update QH */
704
            DPRINTF("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token);
705
            return 1;
706
        }
707
    }
708

    
709
    /* success */
710
    return 0;
711

    
712
out:
713
    switch(ret) {
714
    case USB_RET_STALL:
715
        td->ctrl |= TD_CTRL_STALL;
716
        td->ctrl &= ~TD_CTRL_ACTIVE;
717
        return 1;
718

    
719
    case USB_RET_BABBLE:
720
        td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
721
        td->ctrl &= ~TD_CTRL_ACTIVE;
722
        /* frame interrupted */
723
        return -1;
724

    
725
    case USB_RET_NAK:
726
        td->ctrl |= TD_CTRL_NAK;
727
        if (pid == USB_TOKEN_SETUP)
728
            break;
729
        return 1;
730

    
731
    case USB_RET_NODEV:
732
    default:
733
        break;
734
    }
735

    
736
    /* Retry the TD if error count is not zero */
737

    
738
    td->ctrl |= TD_CTRL_TIMEOUT;
739
    err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
740
    if (err != 0) {
741
        err--;
742
        if (err == 0) {
743
            td->ctrl &= ~TD_CTRL_ACTIVE;
744
            s->status |= UHCI_STS_USBERR;
745
            if (td->ctrl & TD_CTRL_IOC)
746
                *int_mask |= 0x01;
747
            uhci_update_irq(s);
748
        }
749
    }
750
    td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
751
        (err << TD_CTRL_ERROR_SHIFT);
752
    return 1;
753
}
754

    
755
static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask)
756
{
757
    UHCIAsync *async;
758
    int len = 0, max_len;
759
    uint8_t pid, isoc;
760
    uint32_t token;
761

    
762
    /* Is active ? */
763
    if (!(td->ctrl & TD_CTRL_ACTIVE))
764
        return 1;
765

    
766
    /* token field is not unique for isochronous requests,
767
     * so use the destination buffer 
768
     */
769
    if (td->ctrl & TD_CTRL_IOS) {
770
        token = td->buffer;
771
        isoc = 1;
772
    } else {
773
        token = td->token;
774
        isoc = 0;
775
    }
776

    
777
    async = uhci_async_find_td(s, addr, token);
778
    if (async) {
779
        /* Already submitted */
780
        async->valid = 32;
781

    
782
        if (!async->done)
783
            return 1;
784

    
785
        uhci_async_unlink(s, async);
786
        goto done;
787
    }
788

    
789
    /* Allocate new packet */
790
    async = uhci_async_alloc(s);
791
    if (!async)
792
        return 1;
793

    
794
    /* valid needs to be large enough to handle 10 frame delay
795
     * for initial isochronous requests
796
     */
797
    async->valid = 32;
798
    async->td    = addr;
799
    async->token = token;
800
    async->isoc  = isoc;
801

    
802
    max_len = ((td->token >> 21) + 1) & 0x7ff;
803
    pid = td->token & 0xff;
804

    
805
    async->packet.pid     = pid;
806
    async->packet.devaddr = (td->token >> 8) & 0x7f;
807
    async->packet.devep   = (td->token >> 15) & 0xf;
808
    async->packet.data    = async->buffer;
809
    async->packet.len     = max_len;
810
    async->packet.complete_cb     = uhci_async_complete;
811
    async->packet.complete_opaque = s;
812

    
813
    switch(pid) {
814
    case USB_TOKEN_OUT:
815
    case USB_TOKEN_SETUP:
816
        cpu_physical_memory_read(td->buffer, async->buffer, max_len);
817
        len = uhci_broadcast_packet(s, &async->packet);
818
        if (len >= 0)
819
            len = max_len;
820
        break;
821

    
822
    case USB_TOKEN_IN:
823
        len = uhci_broadcast_packet(s, &async->packet);
824
        break;
825

    
826
    default:
827
        /* invalid pid : frame interrupted */
828
        uhci_async_free(s, async);
829
        s->status |= UHCI_STS_HCPERR;
830
        uhci_update_irq(s);
831
        return -1;
832
    }
833
 
834
    if (len == USB_RET_ASYNC) {
835
        uhci_async_link(s, async);
836
        return 2;
837
    }
838

    
839
    async->packet.len = len;
840

    
841
done:
842
    len = uhci_complete_td(s, td, async, int_mask);
843
    uhci_async_free(s, async);
844
    return len;
845
}
846

    
847
static void uhci_async_complete(USBPacket *packet, void *opaque)
848
{
849
    UHCIState *s = opaque;
850
    UHCIAsync *async = (UHCIAsync *) packet;
851

    
852
    DPRINTF("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token);
853

    
854
    if (async->isoc) {
855
        UHCI_TD td;
856
        uint32_t link = async->td;
857
        uint32_t int_mask = 0, val;
858

    
859
        cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td));
860
        le32_to_cpus(&td.link);
861
        le32_to_cpus(&td.ctrl);
862
        le32_to_cpus(&td.token);
863
        le32_to_cpus(&td.buffer);
864

    
865
        uhci_async_unlink(s, async);
866
        uhci_complete_td(s, &td, async, &int_mask);
867
        s->pending_int_mask |= int_mask;
868

    
869
        /* update the status bits of the TD */
870
        val = cpu_to_le32(td.ctrl);
871
        cpu_physical_memory_write((link & ~0xf) + 4,
872
                                  (const uint8_t *)&val, sizeof(val));
873
        uhci_async_free(s, async);
874
    } else {
875
        async->done = 1;
876
        uhci_process_frame(s);
877
    }
878
}
879

    
880
static int is_valid(uint32_t link)
881
{
882
    return (link & 1) == 0;
883
}
884

    
885
static int is_qh(uint32_t link)
886
{
887
    return (link & 2) != 0;
888
}
889

    
890
static int depth_first(uint32_t link)
891
{
892
    return (link & 4) != 0;
893
}
894

    
895
/* QH DB used for detecting QH loops */
896
#define UHCI_MAX_QUEUES 128
897
typedef struct {
898
    uint32_t addr[UHCI_MAX_QUEUES];
899
    int      count;
900
} QhDb;
901

    
902
static void qhdb_reset(QhDb *db)
903
{
904
    db->count = 0;
905
}
906

    
907
/* Add QH to DB. Returns 1 if already present or DB is full. */
908
static int qhdb_insert(QhDb *db, uint32_t addr)
909
{
910
    int i;
911
    for (i = 0; i < db->count; i++)
912
        if (db->addr[i] == addr)
913
            return 1;
914

    
915
    if (db->count >= UHCI_MAX_QUEUES)
916
        return 1;
917

    
918
    db->addr[db->count++] = addr;
919
    return 0;
920
}
921

    
922
static void uhci_process_frame(UHCIState *s)
923
{
924
    uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
925
    uint32_t curr_qh;
926
    int cnt, ret;
927
    UHCI_TD td;
928
    UHCI_QH qh;
929
    QhDb qhdb;
930

    
931
    frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
932

    
933
    DPRINTF("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr);
934

    
935
    cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4);
936
    le32_to_cpus(&link);
937

    
938
    int_mask = 0;
939
    curr_qh  = 0;
940

    
941
    qhdb_reset(&qhdb);
942

    
943
    for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
944
        if (is_qh(link)) {
945
            /* QH */
946

    
947
            if (qhdb_insert(&qhdb, link)) {
948
                /*
949
                 * We're going in circles. Which is not a bug because
950
                 * HCD is allowed to do that as part of the BW management. 
951
                 * In our case though it makes no sense to spin here. Sync transations 
952
                 * are already done, and async completion handler will re-process 
953
                 * the frame when something is ready.
954
                 */
955
                DPRINTF("uhci: detected loop. qh 0x%x\n", link);
956
                break;
957
            }
958

    
959
            cpu_physical_memory_read(link & ~0xf, (uint8_t *) &qh, sizeof(qh));
960
            le32_to_cpus(&qh.link);
961
            le32_to_cpus(&qh.el_link);
962

    
963
            DPRINTF("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
964
                    link, qh.link, qh.el_link);
965

    
966
            if (!is_valid(qh.el_link)) {
967
                /* QH w/o elements */
968
                curr_qh = 0;
969
                link = qh.link;
970
            } else {
971
                /* QH with elements */
972
                    curr_qh = link;
973
                    link = qh.el_link;
974
            }
975
            continue;
976
        }
977

    
978
        /* TD */
979
        cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td));
980
        le32_to_cpus(&td.link);
981
        le32_to_cpus(&td.ctrl);
982
        le32_to_cpus(&td.token);
983
        le32_to_cpus(&td.buffer);
984

    
985
        DPRINTF("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n", 
986
                link, td.link, td.ctrl, td.token, curr_qh);
987

    
988
        old_td_ctrl = td.ctrl;
989
        ret = uhci_handle_td(s, link, &td, &int_mask);
990
        if (old_td_ctrl != td.ctrl) {
991
            /* update the status bits of the TD */
992
            val = cpu_to_le32(td.ctrl);
993
            cpu_physical_memory_write((link & ~0xf) + 4,
994
                                      (const uint8_t *)&val, sizeof(val));
995
        }
996

    
997
        if (ret < 0) {
998
            /* interrupted frame */
999
            break;
1000
        }
1001

    
1002
        if (ret == 2 || ret == 1) {
1003
            DPRINTF("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1004
                    link, ret == 2 ? "pend" : "skip",
1005
                    td.link, td.ctrl, td.token, curr_qh);
1006

    
1007
            link = curr_qh ? qh.link : td.link;
1008
            continue;
1009
        }
1010

    
1011
        /* completed TD */
1012

    
1013
        DPRINTF("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n", 
1014
                link, td.link, td.ctrl, td.token, curr_qh);
1015

    
1016
        link = td.link;
1017

    
1018
        if (curr_qh) {
1019
            /* update QH element link */
1020
            qh.el_link = link;
1021
            val = cpu_to_le32(qh.el_link);
1022
            cpu_physical_memory_write((curr_qh & ~0xf) + 4,
1023
                                          (const uint8_t *)&val, sizeof(val));
1024

    
1025
            if (!depth_first(link)) {
1026
               /* done with this QH */
1027

    
1028
               DPRINTF("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
1029
                       curr_qh, qh.link, qh.el_link);
1030

    
1031
               curr_qh = 0;
1032
               link    = qh.link;
1033
            }
1034
        }
1035

    
1036
        /* go to the next entry */
1037
    }
1038

    
1039
    s->pending_int_mask |= int_mask;
1040
}
1041

    
1042
static void uhci_frame_timer(void *opaque)
1043
{
1044
    UHCIState *s = opaque;
1045

    
1046
    /* prepare the timer for the next frame */
1047
    s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
1048

    
1049
    if (!(s->cmd & UHCI_CMD_RS)) {
1050
        /* Full stop */
1051
        qemu_del_timer(s->frame_timer);
1052
        /* set hchalted bit in status - UHCI11D 2.1.2 */
1053
        s->status |= UHCI_STS_HCHALTED;
1054

    
1055
        DPRINTF("uhci: halted\n");
1056
        return;
1057
    }
1058

    
1059
    /* Complete the previous frame */
1060
    if (s->pending_int_mask) {
1061
        s->status2 |= s->pending_int_mask;
1062
        s->status  |= UHCI_STS_USBINT;
1063
        uhci_update_irq(s);
1064
    }
1065
    s->pending_int_mask = 0;
1066

    
1067
    /* Start new frame */
1068
    s->frnum = (s->frnum + 1) & 0x7ff;
1069

    
1070
    DPRINTF("uhci: new frame #%u\n" , s->frnum);
1071

    
1072
    uhci_async_validate_begin(s);
1073

    
1074
    uhci_process_frame(s);
1075

    
1076
    uhci_async_validate_end(s);
1077

    
1078
    qemu_mod_timer(s->frame_timer, s->expire_time);
1079
}
1080

    
1081
static void uhci_map(PCIDevice *pci_dev, int region_num,
1082
                    pcibus_t addr, pcibus_t size, int type)
1083
{
1084
    UHCIState *s = (UHCIState *)pci_dev;
1085

    
1086
    register_ioport_write(addr, 32, 2, uhci_ioport_writew, s);
1087
    register_ioport_read(addr, 32, 2, uhci_ioport_readw, s);
1088
    register_ioport_write(addr, 32, 4, uhci_ioport_writel, s);
1089
    register_ioport_read(addr, 32, 4, uhci_ioport_readl, s);
1090
    register_ioport_write(addr, 32, 1, uhci_ioport_writeb, s);
1091
    register_ioport_read(addr, 32, 1, uhci_ioport_readb, s);
1092
}
1093

    
1094
static USBPortOps uhci_port_ops = {
1095
    .attach = uhci_attach,
1096
    .detach = uhci_detach,
1097
};
1098

    
1099
static int usb_uhci_common_initfn(UHCIState *s)
1100
{
1101
    uint8_t *pci_conf = s->dev.config;
1102
    int i;
1103

    
1104
    pci_conf[PCI_REVISION_ID] = 0x01; // revision number
1105
    pci_conf[PCI_CLASS_PROG] = 0x00;
1106
    pci_config_set_class(pci_conf, PCI_CLASS_SERIAL_USB);
1107
    /* TODO: reset value should be 0. */
1108
    pci_conf[PCI_INTERRUPT_PIN] = 4; // interrupt pin 3
1109
    pci_conf[0x60] = 0x10; // release number
1110

    
1111
    usb_bus_new(&s->bus, &s->dev.qdev);
1112
    for(i = 0; i < NB_PORTS; i++) {
1113
        usb_register_port(&s->bus, &s->ports[i].port, s, i, NULL, &uhci_port_ops);
1114
    }
1115
    s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
1116
    s->expire_time = qemu_get_clock(vm_clock) +
1117
        (get_ticks_per_sec() / FRAME_TIMER_FREQ);
1118
    s->num_ports_vmstate = NB_PORTS;
1119

    
1120
    qemu_register_reset(uhci_reset, s);
1121

    
1122
    /* Use region 4 for consistency with real hardware.  BSD guests seem
1123
       to rely on this.  */
1124
    pci_register_bar(&s->dev, 4, 0x20,
1125
                           PCI_BASE_ADDRESS_SPACE_IO, uhci_map);
1126

    
1127
    return 0;
1128
}
1129

    
1130
static int usb_uhci_piix3_initfn(PCIDevice *dev)
1131
{
1132
    UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1133
    uint8_t *pci_conf = s->dev.config;
1134

    
1135
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
1136
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_2);
1137
    return usb_uhci_common_initfn(s);
1138
}
1139

    
1140
static int usb_uhci_piix4_initfn(PCIDevice *dev)
1141
{
1142
    UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1143
    uint8_t *pci_conf = s->dev.config;
1144

    
1145
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
1146
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_2);
1147
    return usb_uhci_common_initfn(s);
1148
}
1149

    
1150
static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1151
{
1152
    UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1153
    uint8_t *pci_conf = s->dev.config;
1154

    
1155
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_VIA);
1156
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_VIA_UHCI);
1157

    
1158
    /* USB misc control 1/2 */
1159
    pci_set_long(pci_conf + 0x40,0x00001000);
1160
    /* PM capability */
1161
    pci_set_long(pci_conf + 0x80,0x00020001);
1162
    /* USB legacy support  */
1163
    pci_set_long(pci_conf + 0xc0,0x00002000);
1164

    
1165
    return usb_uhci_common_initfn(s);
1166
}
1167

    
1168
static PCIDeviceInfo uhci_info[] = {
1169
    {
1170
        .qdev.name    = "piix3-usb-uhci",
1171
        .qdev.size    = sizeof(UHCIState),
1172
        .qdev.vmsd    = &vmstate_uhci,
1173
        .init         = usb_uhci_piix3_initfn,
1174
    },{
1175
        .qdev.name    = "piix4-usb-uhci",
1176
        .qdev.size    = sizeof(UHCIState),
1177
        .qdev.vmsd    = &vmstate_uhci,
1178
        .init         = usb_uhci_piix4_initfn,
1179
    },{
1180
        .qdev.name    = "vt82c686b-usb-uhci",
1181
        .qdev.size    = sizeof(UHCIState),
1182
        .qdev.vmsd    = &vmstate_uhci,
1183
        .init         = usb_uhci_vt82c686b_initfn,
1184
    },{
1185
        /* end of list */
1186
    }
1187
};
1188

    
1189
static void uhci_register(void)
1190
{
1191
    pci_qdev_register_many(uhci_info);
1192
}
1193
device_init(uhci_register);
1194

    
1195
void usb_uhci_piix3_init(PCIBus *bus, int devfn)
1196
{
1197
    pci_create_simple(bus, devfn, "piix3-usb-uhci");
1198
}
1199

    
1200
void usb_uhci_piix4_init(PCIBus *bus, int devfn)
1201
{
1202
    pci_create_simple(bus, devfn, "piix4-usb-uhci");
1203
}
1204

    
1205
void usb_uhci_vt82c686b_init(PCIBus *bus, int devfn)
1206
{
1207
    pci_create_simple(bus, devfn, "vt82c686b-usb-uhci");
1208
}