2005-11-05 17:22:28 +03:00
|
|
|
/*
|
|
|
|
* USB UHCI controller emulation
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2005-11-05 17:22:28 +03:00
|
|
|
* Copyright (c) 2005 Fabrice Bellard
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2008-08-21 23:30:31 +04:00
|
|
|
* Copyright (c) 2008 Max Krasnyansky
|
|
|
|
* Magor rewrite of the UHCI data structures parser and frame processor
|
|
|
|
* Support for fully async operation and multiple outstanding transactions
|
|
|
|
*
|
2005-11-05 17:22:28 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2007-11-17 20:14:51 +03:00
|
|
|
#include "hw.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "pci.h"
|
|
|
|
#include "qemu-timer.h"
|
2009-11-11 15:59:56 +03:00
|
|
|
#include "usb-uhci.h"
|
2005-11-05 17:22:28 +03:00
|
|
|
|
|
|
|
//#define DEBUG
|
2008-08-21 23:30:31 +04:00
|
|
|
//#define DEBUG_DUMP_DATA
|
2005-11-05 17:22:28 +03:00
|
|
|
|
2007-02-22 23:21:33 +03:00
|
|
|
#define UHCI_CMD_FGR (1 << 4)
|
|
|
|
#define UHCI_CMD_EGSM (1 << 3)
|
2005-11-05 17:22:28 +03:00
|
|
|
#define UHCI_CMD_GRESET (1 << 2)
|
|
|
|
#define UHCI_CMD_HCRESET (1 << 1)
|
|
|
|
#define UHCI_CMD_RS (1 << 0)
|
|
|
|
|
|
|
|
#define UHCI_STS_HCHALTED (1 << 5)
|
|
|
|
#define UHCI_STS_HCPERR (1 << 4)
|
|
|
|
#define UHCI_STS_HSERR (1 << 3)
|
|
|
|
#define UHCI_STS_RD (1 << 2)
|
|
|
|
#define UHCI_STS_USBERR (1 << 1)
|
|
|
|
#define UHCI_STS_USBINT (1 << 0)
|
|
|
|
|
|
|
|
#define TD_CTRL_SPD (1 << 29)
|
|
|
|
#define TD_CTRL_ERROR_SHIFT 27
|
|
|
|
#define TD_CTRL_IOS (1 << 25)
|
|
|
|
#define TD_CTRL_IOC (1 << 24)
|
|
|
|
#define TD_CTRL_ACTIVE (1 << 23)
|
|
|
|
#define TD_CTRL_STALL (1 << 22)
|
|
|
|
#define TD_CTRL_BABBLE (1 << 20)
|
|
|
|
#define TD_CTRL_NAK (1 << 19)
|
|
|
|
#define TD_CTRL_TIMEOUT (1 << 18)
|
|
|
|
|
|
|
|
#define UHCI_PORT_RESET (1 << 9)
|
|
|
|
#define UHCI_PORT_LSDA (1 << 8)
|
|
|
|
#define UHCI_PORT_ENC (1 << 3)
|
|
|
|
#define UHCI_PORT_EN (1 << 2)
|
|
|
|
#define UHCI_PORT_CSC (1 << 1)
|
|
|
|
#define UHCI_PORT_CCS (1 << 0)
|
|
|
|
|
|
|
|
#define FRAME_TIMER_FREQ 1000
|
|
|
|
|
|
|
|
#define FRAME_MAX_LOOPS 100
|
|
|
|
|
|
|
|
#define NB_PORTS 2
|
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
#ifdef DEBUG
|
2010-02-07 02:03:50 +03:00
|
|
|
#define DPRINTF printf
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2009-07-20 21:19:25 +04:00
|
|
|
static const char *pid2str(int pid)
|
2008-08-21 23:30:31 +04:00
|
|
|
{
|
|
|
|
switch (pid) {
|
|
|
|
case USB_TOKEN_SETUP: return "SETUP";
|
|
|
|
case USB_TOKEN_IN: return "IN";
|
|
|
|
case USB_TOKEN_OUT: return "OUT";
|
|
|
|
}
|
|
|
|
return "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2010-02-07 02:03:50 +03:00
|
|
|
#define DPRINTF(...)
|
2008-08-21 23:30:31 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DEBUG_DUMP_DATA
|
|
|
|
static void dump_data(const uint8_t *data, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("uhci: data: ");
|
|
|
|
for(i = 0; i < len; i++)
|
|
|
|
printf(" %02x", data[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void dump_data(const uint8_t *data, int len) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pending async transaction.
|
|
|
|
* 'packet' must be the first field because completion
|
|
|
|
* handler does "(UHCIAsync *) pkt" cast.
|
|
|
|
*/
|
|
|
|
typedef struct UHCIAsync {
|
|
|
|
USBPacket packet;
|
|
|
|
struct UHCIAsync *next;
|
|
|
|
uint32_t td;
|
|
|
|
uint32_t token;
|
|
|
|
int8_t valid;
|
2010-02-03 18:49:39 +03:00
|
|
|
uint8_t isoc;
|
2008-08-21 23:30:31 +04:00
|
|
|
uint8_t done;
|
|
|
|
uint8_t buffer[2048];
|
|
|
|
} UHCIAsync;
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
typedef struct UHCIPort {
|
|
|
|
USBPort port;
|
|
|
|
uint16_t ctrl;
|
|
|
|
} UHCIPort;
|
|
|
|
|
|
|
|
typedef struct UHCIState {
|
|
|
|
PCIDevice dev;
|
2009-09-17 00:25:29 +04:00
|
|
|
USBBus bus;
|
2005-11-05 17:22:28 +03:00
|
|
|
uint16_t cmd; /* cmd register */
|
|
|
|
uint16_t status;
|
|
|
|
uint16_t intr; /* interrupt enable register */
|
|
|
|
uint16_t frnum; /* frame number */
|
|
|
|
uint32_t fl_base_addr; /* frame list base address */
|
|
|
|
uint8_t sof_timing;
|
|
|
|
uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
|
2010-02-03 18:49:39 +03:00
|
|
|
int64_t expire_time;
|
2005-11-05 17:22:28 +03:00
|
|
|
QEMUTimer *frame_timer;
|
|
|
|
UHCIPort ports[NB_PORTS];
|
2006-08-12 05:04:27 +04:00
|
|
|
|
|
|
|
/* Interrupts that should be raised at the end of the current frame. */
|
|
|
|
uint32_t pending_int_mask;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
/* Active packets */
|
|
|
|
UHCIAsync *async_pending;
|
|
|
|
UHCIAsync *async_pool;
|
2009-10-14 14:21:50 +04:00
|
|
|
uint8_t num_ports_vmstate;
|
2005-11-05 17:22:28 +03:00
|
|
|
} UHCIState;
|
|
|
|
|
|
|
|
typedef struct UHCI_TD {
|
|
|
|
uint32_t link;
|
|
|
|
uint32_t ctrl; /* see TD_CTRL_xxx */
|
|
|
|
uint32_t token;
|
|
|
|
uint32_t buffer;
|
|
|
|
} UHCI_TD;
|
|
|
|
|
|
|
|
typedef struct UHCI_QH {
|
|
|
|
uint32_t link;
|
|
|
|
uint32_t el_link;
|
|
|
|
} UHCI_QH;
|
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
static UHCIAsync *uhci_async_alloc(UHCIState *s)
|
|
|
|
{
|
|
|
|
UHCIAsync *async = qemu_malloc(sizeof(UHCIAsync));
|
2009-02-06 01:06:05 +03:00
|
|
|
|
|
|
|
memset(&async->packet, 0, sizeof(async->packet));
|
|
|
|
async->valid = 0;
|
|
|
|
async->td = 0;
|
|
|
|
async->token = 0;
|
|
|
|
async->done = 0;
|
2010-02-03 18:49:39 +03:00
|
|
|
async->isoc = 0;
|
2009-02-06 01:06:05 +03:00
|
|
|
async->next = NULL;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
return async;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_async_free(UHCIState *s, UHCIAsync *async)
|
|
|
|
{
|
|
|
|
qemu_free(async);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_async_link(UHCIState *s, UHCIAsync *async)
|
|
|
|
{
|
|
|
|
async->next = s->async_pending;
|
|
|
|
s->async_pending = async;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_async_unlink(UHCIState *s, UHCIAsync *async)
|
|
|
|
{
|
|
|
|
UHCIAsync *curr = s->async_pending;
|
|
|
|
UHCIAsync **prev = &s->async_pending;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
if (curr == async) {
|
|
|
|
*prev = curr->next;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = &curr->next;
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_async_cancel(UHCIState *s, UHCIAsync *async)
|
|
|
|
{
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: cancel td 0x%x token 0x%x done %u\n",
|
2008-08-21 23:30:31 +04:00
|
|
|
async->td, async->token, async->done);
|
|
|
|
|
|
|
|
if (!async->done)
|
|
|
|
usb_cancel_packet(&async->packet);
|
|
|
|
uhci_async_free(s, async);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark all outstanding async packets as invalid.
|
|
|
|
* This is used for canceling them when TDs are removed by the HCD.
|
|
|
|
*/
|
|
|
|
static UHCIAsync *uhci_async_validate_begin(UHCIState *s)
|
|
|
|
{
|
|
|
|
UHCIAsync *async = s->async_pending;
|
|
|
|
|
|
|
|
while (async) {
|
|
|
|
async->valid--;
|
|
|
|
async = async->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cancel async packets that are no longer valid
|
|
|
|
*/
|
|
|
|
static void uhci_async_validate_end(UHCIState *s)
|
|
|
|
{
|
|
|
|
UHCIAsync *curr = s->async_pending;
|
|
|
|
UHCIAsync **prev = &s->async_pending;
|
|
|
|
UHCIAsync *next;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
if (curr->valid > 0) {
|
|
|
|
prev = &curr->next;
|
|
|
|
curr = curr->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
next = curr->next;
|
|
|
|
|
|
|
|
/* Unlink */
|
|
|
|
*prev = next;
|
|
|
|
|
|
|
|
uhci_async_cancel(s, curr);
|
|
|
|
|
|
|
|
curr = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_async_cancel_all(UHCIState *s)
|
|
|
|
{
|
|
|
|
UHCIAsync *curr = s->async_pending;
|
|
|
|
UHCIAsync *next;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
next = curr->next;
|
|
|
|
|
|
|
|
uhci_async_cancel(s, curr);
|
|
|
|
|
|
|
|
curr = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->async_pending = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t addr, uint32_t token)
|
|
|
|
{
|
|
|
|
UHCIAsync *async = s->async_pending;
|
2008-08-22 13:23:06 +04:00
|
|
|
UHCIAsync *match = NULL;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're looking for the best match here. ie both td addr and token.
|
|
|
|
* Otherwise we return last good match. ie just token.
|
|
|
|
* It's ok to match just token because it identifies the transaction
|
|
|
|
* rather well, token includes: device addr, endpoint, size, etc.
|
|
|
|
*
|
|
|
|
* Also since we queue async transactions in reverse order by returning
|
|
|
|
* last good match we restores the order.
|
|
|
|
*
|
|
|
|
* It's expected that we wont have a ton of outstanding transactions.
|
|
|
|
* If we ever do we'd want to optimize this algorithm.
|
|
|
|
*/
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
while (async) {
|
2008-08-22 13:23:06 +04:00
|
|
|
if (async->token == token) {
|
|
|
|
/* Good match */
|
|
|
|
match = async;
|
|
|
|
|
|
|
|
if (async->td == addr) {
|
|
|
|
/* Best match */
|
|
|
|
break;
|
2008-08-21 23:30:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async = async->next;
|
2008-08-22 13:23:06 +04:00
|
|
|
count++;
|
2008-08-21 23:30:31 +04:00
|
|
|
}
|
2008-08-22 13:23:06 +04:00
|
|
|
|
|
|
|
if (count > 64)
|
|
|
|
fprintf(stderr, "uhci: warning lots of async transactions\n");
|
|
|
|
|
|
|
|
return match;
|
2008-08-21 23:30:31 +04:00
|
|
|
}
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
static void uhci_attach(USBPort *port1, USBDevice *dev);
|
|
|
|
|
|
|
|
static void uhci_update_irq(UHCIState *s)
|
|
|
|
{
|
|
|
|
int level;
|
|
|
|
if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
|
|
|
|
((s->status2 & 2) && (s->intr & (1 << 3))) ||
|
|
|
|
((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
|
|
|
|
((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
|
|
|
|
(s->status & UHCI_STS_HSERR) ||
|
|
|
|
(s->status & UHCI_STS_HCPERR)) {
|
|
|
|
level = 1;
|
|
|
|
} else {
|
|
|
|
level = 0;
|
|
|
|
}
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_set_irq(s->dev.irq[3], level);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
|
2009-06-17 20:32:02 +04:00
|
|
|
static void uhci_reset(void *opaque)
|
2005-11-05 17:22:28 +03:00
|
|
|
{
|
2009-06-17 20:32:02 +04:00
|
|
|
UHCIState *s = opaque;
|
2005-11-05 17:22:28 +03:00
|
|
|
uint8_t *pci_conf;
|
|
|
|
int i;
|
|
|
|
UHCIPort *port;
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: full reset\n");
|
2008-08-21 23:33:09 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
pci_conf = s->dev.config;
|
|
|
|
|
|
|
|
pci_conf[0x6a] = 0x01; /* usb clock */
|
|
|
|
pci_conf[0x6b] = 0x00;
|
|
|
|
s->cmd = 0;
|
|
|
|
s->status = 0;
|
|
|
|
s->status2 = 0;
|
|
|
|
s->intr = 0;
|
|
|
|
s->fl_base_addr = 0;
|
|
|
|
s->sof_timing = 64;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
for(i = 0; i < NB_PORTS; i++) {
|
|
|
|
port = &s->ports[i];
|
|
|
|
port->ctrl = 0x0080;
|
2005-11-06 19:13:29 +03:00
|
|
|
if (port->port.dev)
|
|
|
|
uhci_attach(&port->port, port->port.dev);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
uhci_async_cancel_all(s);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
|
2009-10-14 14:49:30 +04:00
|
|
|
static void uhci_pre_save(void *opaque)
|
2007-10-05 02:47:34 +04:00
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
|
|
|
|
2008-08-21 23:33:09 +04:00
|
|
|
uhci_async_cancel_all(s);
|
2007-10-05 02:47:34 +04:00
|
|
|
}
|
|
|
|
|
2009-10-14 14:49:30 +04:00
|
|
|
static const VMStateDescription vmstate_uhci_port = {
|
|
|
|
.name = "uhci port",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.fields = (VMStateField []) {
|
|
|
|
VMSTATE_UINT16(ctrl, UHCIPort),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_uhci = {
|
|
|
|
.name = "uhci",
|
2010-06-01 08:26:20 +04:00
|
|
|
.version_id = 2,
|
2009-10-14 14:49:30 +04:00
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
|
|
|
.pre_save = uhci_pre_save,
|
|
|
|
.fields = (VMStateField []) {
|
|
|
|
VMSTATE_PCI_DEVICE(dev, UHCIState),
|
|
|
|
VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
|
|
|
|
VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
|
|
|
|
vmstate_uhci_port, UHCIPort),
|
|
|
|
VMSTATE_UINT16(cmd, UHCIState),
|
|
|
|
VMSTATE_UINT16(status, UHCIState),
|
|
|
|
VMSTATE_UINT16(intr, UHCIState),
|
|
|
|
VMSTATE_UINT16(frnum, UHCIState),
|
|
|
|
VMSTATE_UINT32(fl_base_addr, UHCIState),
|
|
|
|
VMSTATE_UINT8(sof_timing, UHCIState),
|
|
|
|
VMSTATE_UINT8(status2, UHCIState),
|
|
|
|
VMSTATE_TIMER(frame_timer, UHCIState),
|
2010-06-01 08:26:20 +04:00
|
|
|
VMSTATE_INT64_V(expire_time, UHCIState, 2),
|
2009-10-14 14:49:30 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2007-10-05 02:47:34 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
addr &= 0x1f;
|
|
|
|
switch(addr) {
|
|
|
|
case 0x0c:
|
|
|
|
s->sof_timing = val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
|
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
addr &= 0x1f;
|
|
|
|
switch(addr) {
|
|
|
|
case 0x0c:
|
|
|
|
val = s->sof_timing;
|
2006-03-12 00:46:12 +03:00
|
|
|
break;
|
2005-11-05 17:22:28 +03:00
|
|
|
default:
|
|
|
|
val = 0xff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
addr &= 0x1f;
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: writew port=0x%04x val=0x%04x\n", addr, val);
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
switch(addr) {
|
|
|
|
case 0x00:
|
|
|
|
if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
|
|
|
|
/* start frame processing */
|
|
|
|
qemu_mod_timer(s->frame_timer, qemu_get_clock(vm_clock));
|
2006-04-25 01:38:50 +04:00
|
|
|
s->status &= ~UHCI_STS_HCHALTED;
|
2006-04-26 01:01:19 +04:00
|
|
|
} else if (!(val & UHCI_CMD_RS)) {
|
2006-04-25 01:38:50 +04:00
|
|
|
s->status |= UHCI_STS_HCHALTED;
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
if (val & UHCI_CMD_GRESET) {
|
|
|
|
UHCIPort *port;
|
|
|
|
USBDevice *dev;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* send reset on the USB bus */
|
|
|
|
for(i = 0; i < NB_PORTS; i++) {
|
|
|
|
port = &s->ports[i];
|
2005-11-06 19:13:29 +03:00
|
|
|
dev = port->port.dev;
|
2005-11-05 17:22:28 +03:00
|
|
|
if (dev) {
|
2006-08-12 05:04:27 +04:00
|
|
|
usb_send_msg(dev, USB_MSG_RESET);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
uhci_reset(s);
|
|
|
|
return;
|
|
|
|
}
|
2005-11-19 20:43:37 +03:00
|
|
|
if (val & UHCI_CMD_HCRESET) {
|
2005-11-05 17:22:28 +03:00
|
|
|
uhci_reset(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s->cmd = val;
|
|
|
|
break;
|
|
|
|
case 0x02:
|
|
|
|
s->status &= ~val;
|
|
|
|
/* XXX: the chip spec is not coherent, so we add a hidden
|
|
|
|
register to distinguish between IOC and SPD */
|
|
|
|
if (val & UHCI_STS_USBINT)
|
|
|
|
s->status2 = 0;
|
|
|
|
uhci_update_irq(s);
|
|
|
|
break;
|
|
|
|
case 0x04:
|
|
|
|
s->intr = val;
|
|
|
|
uhci_update_irq(s);
|
|
|
|
break;
|
|
|
|
case 0x06:
|
|
|
|
if (s->status & UHCI_STS_HCHALTED)
|
|
|
|
s->frnum = val & 0x7ff;
|
|
|
|
break;
|
|
|
|
case 0x10 ... 0x1f:
|
|
|
|
{
|
|
|
|
UHCIPort *port;
|
|
|
|
USBDevice *dev;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = (addr >> 1) & 7;
|
|
|
|
if (n >= NB_PORTS)
|
|
|
|
return;
|
|
|
|
port = &s->ports[n];
|
2005-11-06 19:13:29 +03:00
|
|
|
dev = port->port.dev;
|
2005-11-05 17:22:28 +03:00
|
|
|
if (dev) {
|
|
|
|
/* port reset */
|
2007-09-17 01:08:06 +04:00
|
|
|
if ( (val & UHCI_PORT_RESET) &&
|
2005-11-05 17:22:28 +03:00
|
|
|
!(port->ctrl & UHCI_PORT_RESET) ) {
|
2006-08-12 05:04:27 +04:00
|
|
|
usb_send_msg(dev, USB_MSG_RESET);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
port->ctrl = (port->ctrl & 0x01fb) | (val & ~0x01fb);
|
|
|
|
/* some bits are reset when a '1' is written to them */
|
|
|
|
port->ctrl &= ~(val & 0x000a);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
|
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
addr &= 0x1f;
|
|
|
|
switch(addr) {
|
|
|
|
case 0x00:
|
|
|
|
val = s->cmd;
|
|
|
|
break;
|
|
|
|
case 0x02:
|
|
|
|
val = s->status;
|
|
|
|
break;
|
|
|
|
case 0x04:
|
|
|
|
val = s->intr;
|
|
|
|
break;
|
|
|
|
case 0x06:
|
|
|
|
val = s->frnum;
|
|
|
|
break;
|
|
|
|
case 0x10 ... 0x1f:
|
|
|
|
{
|
|
|
|
UHCIPort *port;
|
|
|
|
int n;
|
|
|
|
n = (addr >> 1) & 7;
|
2007-09-17 01:08:06 +04:00
|
|
|
if (n >= NB_PORTS)
|
2005-11-05 17:22:28 +03:00
|
|
|
goto read_default;
|
|
|
|
port = &s->ports[n];
|
|
|
|
val = port->ctrl;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
read_default:
|
|
|
|
val = 0xff7f; /* disabled port */
|
|
|
|
break;
|
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: readw port=0x%04x val=0x%04x\n", addr, val);
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
|
|
|
|
|
|
|
addr &= 0x1f;
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: writel port=0x%04x val=0x%08x\n", addr, val);
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
switch(addr) {
|
|
|
|
case 0x08:
|
|
|
|
s->fl_base_addr = val & ~0xfff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
|
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
addr &= 0x1f;
|
|
|
|
switch(addr) {
|
|
|
|
case 0x08:
|
|
|
|
val = s->fl_base_addr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
val = 0xffffffff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2007-02-22 23:21:33 +03:00
|
|
|
/* signal resume if controller suspended */
|
|
|
|
static void uhci_resume (void *opaque)
|
|
|
|
{
|
|
|
|
UHCIState *s = (UHCIState *)opaque;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (s->cmd & UHCI_CMD_EGSM) {
|
|
|
|
s->cmd |= UHCI_CMD_FGR;
|
|
|
|
s->status |= UHCI_STS_RD;
|
|
|
|
uhci_update_irq(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
static void uhci_attach(USBPort *port1, USBDevice *dev)
|
|
|
|
{
|
|
|
|
UHCIState *s = port1->opaque;
|
|
|
|
UHCIPort *port = &s->ports[port1->index];
|
|
|
|
|
|
|
|
if (dev) {
|
2005-11-06 19:13:29 +03:00
|
|
|
if (port->port.dev) {
|
2005-11-05 17:22:28 +03:00
|
|
|
usb_attach(port1, NULL);
|
|
|
|
}
|
|
|
|
/* set connect status */
|
2006-05-22 21:17:06 +04:00
|
|
|
port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
/* update speed */
|
|
|
|
if (dev->speed == USB_SPEED_LOW)
|
|
|
|
port->ctrl |= UHCI_PORT_LSDA;
|
|
|
|
else
|
|
|
|
port->ctrl &= ~UHCI_PORT_LSDA;
|
2007-02-22 23:21:33 +03:00
|
|
|
|
|
|
|
uhci_resume(s);
|
|
|
|
|
2005-11-06 19:13:29 +03:00
|
|
|
port->port.dev = dev;
|
2005-11-05 17:22:28 +03:00
|
|
|
/* send the attach message */
|
2006-08-12 05:04:27 +04:00
|
|
|
usb_send_msg(dev, USB_MSG_ATTACH);
|
2005-11-05 17:22:28 +03:00
|
|
|
} else {
|
|
|
|
/* set connect status */
|
2006-05-22 21:17:06 +04:00
|
|
|
if (port->ctrl & UHCI_PORT_CCS) {
|
|
|
|
port->ctrl &= ~UHCI_PORT_CCS;
|
|
|
|
port->ctrl |= UHCI_PORT_CSC;
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
/* disable port */
|
|
|
|
if (port->ctrl & UHCI_PORT_EN) {
|
|
|
|
port->ctrl &= ~UHCI_PORT_EN;
|
|
|
|
port->ctrl |= UHCI_PORT_ENC;
|
|
|
|
}
|
2007-02-22 23:21:33 +03:00
|
|
|
|
|
|
|
uhci_resume(s);
|
|
|
|
|
2005-11-06 19:13:29 +03:00
|
|
|
dev = port->port.dev;
|
2005-11-05 17:22:28 +03:00
|
|
|
if (dev) {
|
|
|
|
/* send the detach message */
|
2006-08-12 05:04:27 +04:00
|
|
|
usb_send_msg(dev, USB_MSG_DETACH);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
2005-11-06 19:13:29 +03:00
|
|
|
port->port.dev = NULL;
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-12 05:04:27 +04:00
|
|
|
static int uhci_broadcast_packet(UHCIState *s, USBPacket *p)
|
2005-11-05 17:22:28 +03:00
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: packet enter. pid %s addr 0x%02x ep %d len %d\n",
|
2008-08-21 23:30:31 +04:00
|
|
|
pid2str(p->pid), p->devaddr, p->devep, p->len);
|
2008-08-22 12:58:08 +04:00
|
|
|
if (p->pid == USB_TOKEN_OUT || p->pid == USB_TOKEN_SETUP)
|
2008-08-21 23:30:31 +04:00
|
|
|
dump_data(p->data, p->len);
|
|
|
|
|
|
|
|
ret = USB_RET_NODEV;
|
|
|
|
for (i = 0; i < NB_PORTS && ret == USB_RET_NODEV; i++) {
|
|
|
|
UHCIPort *port = &s->ports[i];
|
|
|
|
USBDevice *dev = port->port.dev;
|
|
|
|
|
|
|
|
if (dev && (port->ctrl & UHCI_PORT_EN))
|
2009-08-31 16:23:59 +04:00
|
|
|
ret = dev->info->handle_packet(dev, p);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: packet exit. ret %d len %d\n", ret, p->len);
|
2008-08-21 23:30:31 +04:00
|
|
|
if (p->pid == USB_TOKEN_IN && ret > 0)
|
|
|
|
dump_data(p->data, ret);
|
|
|
|
|
|
|
|
return ret;
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
static void uhci_async_complete(USBPacket * packet, void *opaque);
|
|
|
|
static void uhci_process_frame(UHCIState *s);
|
2006-08-12 05:04:27 +04:00
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
/* return -1 if fatal error (frame must be stopped)
|
|
|
|
0 if TD successful
|
|
|
|
1 if TD unsuccessful or inactive
|
|
|
|
*/
|
2008-08-21 23:30:31 +04:00
|
|
|
static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
|
2005-11-05 17:22:28 +03:00
|
|
|
{
|
2008-08-21 23:30:31 +04:00
|
|
|
int len = 0, max_len, err, ret;
|
2005-11-05 17:22:28 +03:00
|
|
|
uint8_t pid;
|
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
max_len = ((td->token >> 21) + 1) & 0x7ff;
|
|
|
|
pid = td->token & 0xff;
|
|
|
|
|
|
|
|
ret = async->packet.len;
|
|
|
|
|
|
|
|
if (td->ctrl & TD_CTRL_IOS)
|
|
|
|
td->ctrl &= ~TD_CTRL_ACTIVE;
|
2005-11-05 17:22:28 +03:00
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2007-10-05 02:47:34 +04:00
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
len = async->packet.len;
|
|
|
|
td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
|
|
|
|
|
|
|
|
/* The NAK bit may have been set by a previous frame, so clear it
|
|
|
|
here. The docs are somewhat unclear, but win2k relies on this
|
|
|
|
behavior. */
|
|
|
|
td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
|
2010-04-05 00:48:31 +04:00
|
|
|
if (td->ctrl & TD_CTRL_IOC)
|
|
|
|
*int_mask |= 0x01;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
if (pid == USB_TOKEN_IN) {
|
|
|
|
if (len > max_len) {
|
|
|
|
ret = USB_RET_BABBLE;
|
|
|
|
goto out;
|
2006-08-12 05:04:27 +04:00
|
|
|
}
|
2007-10-05 02:47:34 +04:00
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
if (len > 0) {
|
|
|
|
/* write the data back */
|
|
|
|
cpu_physical_memory_write(td->buffer, async->buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
|
2005-11-05 17:22:28 +03:00
|
|
|
*int_mask |= 0x02;
|
|
|
|
/* short packet: do not update QH */
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token);
|
2005-11-05 17:22:28 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
switch(ret) {
|
|
|
|
case USB_RET_STALL:
|
|
|
|
td->ctrl |= TD_CTRL_STALL;
|
|
|
|
td->ctrl &= ~TD_CTRL_ACTIVE;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case USB_RET_BABBLE:
|
|
|
|
td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
|
|
|
|
td->ctrl &= ~TD_CTRL_ACTIVE;
|
|
|
|
/* frame interrupted */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case USB_RET_NAK:
|
|
|
|
td->ctrl |= TD_CTRL_NAK;
|
|
|
|
if (pid == USB_TOKEN_SETUP)
|
|
|
|
break;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case USB_RET_NODEV:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Retry the TD if error count is not zero */
|
|
|
|
|
|
|
|
td->ctrl |= TD_CTRL_TIMEOUT;
|
|
|
|
err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
|
|
|
|
if (err != 0) {
|
|
|
|
err--;
|
|
|
|
if (err == 0) {
|
2005-11-05 17:22:28 +03:00
|
|
|
td->ctrl &= ~TD_CTRL_ACTIVE;
|
2008-08-21 23:30:31 +04:00
|
|
|
s->status |= UHCI_STS_USBERR;
|
2010-04-05 00:48:31 +04:00
|
|
|
if (td->ctrl & TD_CTRL_IOC)
|
|
|
|
*int_mask |= 0x01;
|
2008-08-21 23:30:31 +04:00
|
|
|
uhci_update_irq(s);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
|
|
|
|
(err << TD_CTRL_ERROR_SHIFT);
|
|
|
|
return 1;
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask)
|
|
|
|
{
|
|
|
|
UHCIAsync *async;
|
2008-08-22 12:58:08 +04:00
|
|
|
int len = 0, max_len;
|
2010-02-03 18:49:39 +03:00
|
|
|
uint8_t pid, isoc;
|
|
|
|
uint32_t token;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
/* Is active ? */
|
|
|
|
if (!(td->ctrl & TD_CTRL_ACTIVE))
|
|
|
|
return 1;
|
|
|
|
|
2010-02-03 18:49:39 +03:00
|
|
|
/* token field is not unique for isochronous requests,
|
|
|
|
* so use the destination buffer
|
|
|
|
*/
|
|
|
|
if (td->ctrl & TD_CTRL_IOS) {
|
|
|
|
token = td->buffer;
|
|
|
|
isoc = 1;
|
|
|
|
} else {
|
|
|
|
token = td->token;
|
|
|
|
isoc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async = uhci_async_find_td(s, addr, token);
|
2008-08-21 23:30:31 +04:00
|
|
|
if (async) {
|
|
|
|
/* Already submitted */
|
2008-09-11 23:42:00 +04:00
|
|
|
async->valid = 32;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
if (!async->done)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
uhci_async_unlink(s, async);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate new packet */
|
|
|
|
async = uhci_async_alloc(s);
|
|
|
|
if (!async)
|
|
|
|
return 1;
|
|
|
|
|
2010-02-03 18:49:39 +03:00
|
|
|
/* valid needs to be large enough to handle 10 frame delay
|
|
|
|
* for initial isochronous requests
|
|
|
|
*/
|
|
|
|
async->valid = 32;
|
2008-08-21 23:30:31 +04:00
|
|
|
async->td = addr;
|
2010-02-03 18:49:39 +03:00
|
|
|
async->token = token;
|
|
|
|
async->isoc = isoc;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
max_len = ((td->token >> 21) + 1) & 0x7ff;
|
|
|
|
pid = td->token & 0xff;
|
|
|
|
|
|
|
|
async->packet.pid = pid;
|
|
|
|
async->packet.devaddr = (td->token >> 8) & 0x7f;
|
|
|
|
async->packet.devep = (td->token >> 15) & 0xf;
|
|
|
|
async->packet.data = async->buffer;
|
|
|
|
async->packet.len = max_len;
|
|
|
|
async->packet.complete_cb = uhci_async_complete;
|
|
|
|
async->packet.complete_opaque = s;
|
|
|
|
|
|
|
|
switch(pid) {
|
|
|
|
case USB_TOKEN_OUT:
|
|
|
|
case USB_TOKEN_SETUP:
|
|
|
|
cpu_physical_memory_read(td->buffer, async->buffer, max_len);
|
2008-08-22 12:58:08 +04:00
|
|
|
len = uhci_broadcast_packet(s, &async->packet);
|
|
|
|
if (len >= 0)
|
|
|
|
len = max_len;
|
2008-08-21 23:30:31 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_TOKEN_IN:
|
2008-08-22 12:58:08 +04:00
|
|
|
len = uhci_broadcast_packet(s, &async->packet);
|
2008-08-21 23:30:31 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* invalid pid : frame interrupted */
|
|
|
|
uhci_async_free(s, async);
|
|
|
|
s->status |= UHCI_STS_HCPERR;
|
|
|
|
uhci_update_irq(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-22 12:58:08 +04:00
|
|
|
if (len == USB_RET_ASYNC) {
|
2008-08-21 23:30:31 +04:00
|
|
|
uhci_async_link(s, async);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2008-08-22 12:58:08 +04:00
|
|
|
async->packet.len = len;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
done:
|
2008-08-22 12:58:08 +04:00
|
|
|
len = uhci_complete_td(s, td, async, int_mask);
|
2008-08-21 23:30:31 +04:00
|
|
|
uhci_async_free(s, async);
|
2008-08-22 12:58:08 +04:00
|
|
|
return len;
|
2008-08-21 23:30:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_async_complete(USBPacket *packet, void *opaque)
|
2006-08-12 05:04:27 +04:00
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
2008-08-21 23:30:31 +04:00
|
|
|
UHCIAsync *async = (UHCIAsync *) packet;
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token);
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2010-02-03 18:49:39 +03:00
|
|
|
if (async->isoc) {
|
|
|
|
UHCI_TD td;
|
|
|
|
uint32_t link = async->td;
|
|
|
|
uint32_t int_mask = 0, val;
|
2010-04-25 22:23:04 +04:00
|
|
|
|
2010-02-03 18:49:39 +03:00
|
|
|
cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td));
|
|
|
|
le32_to_cpus(&td.link);
|
|
|
|
le32_to_cpus(&td.ctrl);
|
|
|
|
le32_to_cpus(&td.token);
|
|
|
|
le32_to_cpus(&td.buffer);
|
|
|
|
|
|
|
|
uhci_async_unlink(s, async);
|
2010-04-25 22:23:04 +04:00
|
|
|
uhci_complete_td(s, &td, async, &int_mask);
|
2010-02-03 18:49:39 +03:00
|
|
|
s->pending_int_mask |= int_mask;
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2010-02-03 18:49:39 +03:00
|
|
|
/* update the status bits of the TD */
|
|
|
|
val = cpu_to_le32(td.ctrl);
|
|
|
|
cpu_physical_memory_write((link & ~0xf) + 4,
|
|
|
|
(const uint8_t *)&val, sizeof(val));
|
|
|
|
uhci_async_free(s, async);
|
|
|
|
} else {
|
|
|
|
async->done = 1;
|
|
|
|
uhci_process_frame(s);
|
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int is_valid(uint32_t link)
|
|
|
|
{
|
|
|
|
return (link & 1) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_qh(uint32_t link)
|
|
|
|
{
|
|
|
|
return (link & 2) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int depth_first(uint32_t link)
|
|
|
|
{
|
|
|
|
return (link & 4) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* QH DB used for detecting QH loops */
|
|
|
|
#define UHCI_MAX_QUEUES 128
|
|
|
|
typedef struct {
|
|
|
|
uint32_t addr[UHCI_MAX_QUEUES];
|
|
|
|
int count;
|
|
|
|
} QhDb;
|
|
|
|
|
|
|
|
static void qhdb_reset(QhDb *db)
|
|
|
|
{
|
|
|
|
db->count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add QH to DB. Returns 1 if already present or DB is full. */
|
|
|
|
static int qhdb_insert(QhDb *db, uint32_t addr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < db->count; i++)
|
|
|
|
if (db->addr[i] == addr)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (db->count >= UHCI_MAX_QUEUES)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
db->addr[db->count++] = addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uhci_process_frame(UHCIState *s)
|
|
|
|
{
|
|
|
|
uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
|
|
|
|
uint32_t curr_qh;
|
|
|
|
int cnt, ret;
|
2006-08-12 05:04:27 +04:00
|
|
|
UHCI_TD td;
|
2008-08-21 23:30:31 +04:00
|
|
|
UHCI_QH qh;
|
|
|
|
QhDb qhdb;
|
2006-08-12 05:04:27 +04:00
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr);
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4);
|
|
|
|
le32_to_cpus(&link);
|
2007-10-05 02:47:34 +04:00
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
int_mask = 0;
|
|
|
|
curr_qh = 0;
|
|
|
|
|
|
|
|
qhdb_reset(&qhdb);
|
|
|
|
|
|
|
|
for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
|
|
|
|
if (is_qh(link)) {
|
|
|
|
/* QH */
|
|
|
|
|
|
|
|
if (qhdb_insert(&qhdb, link)) {
|
|
|
|
/*
|
|
|
|
* We're going in circles. Which is not a bug because
|
|
|
|
* HCD is allowed to do that as part of the BW management.
|
|
|
|
* In our case though it makes no sense to spin here. Sync transations
|
|
|
|
* are already done, and async completion handler will re-process
|
|
|
|
* the frame when something is ready.
|
|
|
|
*/
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: detected loop. qh 0x%x\n", link);
|
2008-08-21 23:30:31 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu_physical_memory_read(link & ~0xf, (uint8_t *) &qh, sizeof(qh));
|
|
|
|
le32_to_cpus(&qh.link);
|
|
|
|
le32_to_cpus(&qh.el_link);
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
|
2008-08-21 23:30:31 +04:00
|
|
|
link, qh.link, qh.el_link);
|
|
|
|
|
|
|
|
if (!is_valid(qh.el_link)) {
|
|
|
|
/* QH w/o elements */
|
|
|
|
curr_qh = 0;
|
|
|
|
link = qh.link;
|
|
|
|
} else {
|
|
|
|
/* QH with elements */
|
|
|
|
curr_qh = link;
|
|
|
|
link = qh.el_link;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TD */
|
|
|
|
cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td));
|
2007-10-05 02:47:34 +04:00
|
|
|
le32_to_cpus(&td.link);
|
|
|
|
le32_to_cpus(&td.ctrl);
|
|
|
|
le32_to_cpus(&td.token);
|
|
|
|
le32_to_cpus(&td.buffer);
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
|
2008-08-21 23:30:31 +04:00
|
|
|
link, td.link, td.ctrl, td.token, curr_qh);
|
|
|
|
|
|
|
|
old_td_ctrl = td.ctrl;
|
|
|
|
ret = uhci_handle_td(s, link, &td, &int_mask);
|
2007-10-05 02:47:34 +04:00
|
|
|
if (old_td_ctrl != td.ctrl) {
|
2008-08-21 23:30:31 +04:00
|
|
|
/* update the status bits of the TD */
|
2007-10-05 02:47:34 +04:00
|
|
|
val = cpu_to_le32(td.ctrl);
|
|
|
|
cpu_physical_memory_write((link & ~0xf) + 4,
|
2008-08-21 23:30:31 +04:00
|
|
|
(const uint8_t *)&val, sizeof(val));
|
2007-10-05 02:47:34 +04:00
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
/* interrupted frame */
|
|
|
|
break;
|
2007-10-05 02:47:34 +04:00
|
|
|
}
|
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
if (ret == 2 || ret == 1) {
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
|
2008-08-21 23:30:31 +04:00
|
|
|
link, ret == 2 ? "pend" : "skip",
|
|
|
|
td.link, td.ctrl, td.token, curr_qh);
|
2007-10-05 02:47:34 +04:00
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
link = curr_qh ? qh.link : td.link;
|
|
|
|
continue;
|
2006-08-12 05:04:27 +04:00
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
/* completed TD */
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
|
2008-08-21 23:30:31 +04:00
|
|
|
link, td.link, td.ctrl, td.token, curr_qh);
|
|
|
|
|
|
|
|
link = td.link;
|
|
|
|
|
|
|
|
if (curr_qh) {
|
|
|
|
/* update QH element link */
|
|
|
|
qh.el_link = link;
|
2006-08-12 05:04:27 +04:00
|
|
|
val = cpu_to_le32(qh.el_link);
|
2008-08-21 23:30:31 +04:00
|
|
|
cpu_physical_memory_write((curr_qh & ~0xf) + 4,
|
|
|
|
(const uint8_t *)&val, sizeof(val));
|
|
|
|
|
|
|
|
if (!depth_first(link)) {
|
|
|
|
/* done with this QH */
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
|
2008-08-21 23:30:31 +04:00
|
|
|
curr_qh, qh.link, qh.el_link);
|
|
|
|
|
|
|
|
curr_qh = 0;
|
|
|
|
link = qh.link;
|
|
|
|
}
|
2006-08-12 05:04:27 +04:00
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
/* go to the next entry */
|
2006-08-12 05:04:27 +04:00
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2010-02-03 18:49:39 +03:00
|
|
|
s->pending_int_mask |= int_mask;
|
2006-08-12 05:04:27 +04:00
|
|
|
}
|
|
|
|
|
2005-11-05 17:22:28 +03:00
|
|
|
static void uhci_frame_timer(void *opaque)
|
|
|
|
{
|
|
|
|
UHCIState *s = opaque;
|
2010-02-03 18:49:39 +03:00
|
|
|
|
|
|
|
/* prepare the timer for the next frame */
|
|
|
|
s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
|
2005-11-05 17:22:28 +03:00
|
|
|
|
|
|
|
if (!(s->cmd & UHCI_CMD_RS)) {
|
2008-08-21 23:30:31 +04:00
|
|
|
/* Full stop */
|
2005-11-05 17:22:28 +03:00
|
|
|
qemu_del_timer(s->frame_timer);
|
2006-04-25 01:38:50 +04:00
|
|
|
/* set hchalted bit in status - UHCI11D 2.1.2 */
|
|
|
|
s->status |= UHCI_STS_HCHALTED;
|
2008-08-21 23:33:09 +04:00
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: halted\n");
|
2005-11-05 17:22:28 +03:00
|
|
|
return;
|
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
/* Complete the previous frame */
|
2006-08-12 05:04:27 +04:00
|
|
|
if (s->pending_int_mask) {
|
|
|
|
s->status2 |= s->pending_int_mask;
|
2008-08-21 23:30:31 +04:00
|
|
|
s->status |= UHCI_STS_USBINT;
|
2006-08-12 05:04:27 +04:00
|
|
|
uhci_update_irq(s);
|
|
|
|
}
|
2010-02-03 18:49:39 +03:00
|
|
|
s->pending_int_mask = 0;
|
2007-10-05 02:47:34 +04:00
|
|
|
|
2008-08-21 23:30:31 +04:00
|
|
|
/* Start new frame */
|
|
|
|
s->frnum = (s->frnum + 1) & 0x7ff;
|
|
|
|
|
2010-02-07 02:03:50 +03:00
|
|
|
DPRINTF("uhci: new frame #%u\n" , s->frnum);
|
2008-08-21 23:30:31 +04:00
|
|
|
|
|
|
|
uhci_async_validate_begin(s);
|
|
|
|
|
|
|
|
uhci_process_frame(s);
|
|
|
|
|
|
|
|
uhci_async_validate_end(s);
|
2007-10-05 02:47:34 +04:00
|
|
|
|
2010-02-03 18:49:39 +03:00
|
|
|
qemu_mod_timer(s->frame_timer, s->expire_time);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
static void uhci_map(PCIDevice *pci_dev, int region_num,
|
2009-10-30 15:21:08 +03:00
|
|
|
pcibus_t addr, pcibus_t size, int type)
|
2005-11-05 17:22:28 +03:00
|
|
|
{
|
|
|
|
UHCIState *s = (UHCIState *)pci_dev;
|
|
|
|
|
|
|
|
register_ioport_write(addr, 32, 2, uhci_ioport_writew, s);
|
|
|
|
register_ioport_read(addr, 32, 2, uhci_ioport_readw, s);
|
|
|
|
register_ioport_write(addr, 32, 4, uhci_ioport_writel, s);
|
|
|
|
register_ioport_read(addr, 32, 4, uhci_ioport_readl, s);
|
|
|
|
register_ioport_write(addr, 32, 1, uhci_ioport_writeb, s);
|
|
|
|
register_ioport_read(addr, 32, 1, uhci_ioport_readb, s);
|
|
|
|
}
|
|
|
|
|
2009-08-31 16:24:02 +04:00
|
|
|
static int usb_uhci_common_initfn(UHCIState *s)
|
2005-11-05 17:22:28 +03:00
|
|
|
{
|
2009-08-31 16:24:02 +04:00
|
|
|
uint8_t *pci_conf = s->dev.config;
|
2005-11-05 17:22:28 +03:00
|
|
|
int i;
|
|
|
|
|
2009-12-10 20:25:03 +03:00
|
|
|
pci_conf[PCI_REVISION_ID] = 0x01; // revision number
|
|
|
|
pci_conf[PCI_CLASS_PROG] = 0x00;
|
2009-02-01 22:26:20 +03:00
|
|
|
pci_config_set_class(pci_conf, PCI_CLASS_SERIAL_USB);
|
2009-05-03 23:03:00 +04:00
|
|
|
pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
|
2009-12-10 20:25:03 +03:00
|
|
|
/* TODO: reset value should be 0. */
|
|
|
|
pci_conf[PCI_INTERRUPT_PIN] = 4; // interrupt pin 3
|
2006-03-11 21:03:38 +03:00
|
|
|
pci_conf[0x60] = 0x10; // release number
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2009-09-17 00:25:29 +04:00
|
|
|
usb_bus_new(&s->bus, &s->dev.qdev);
|
2005-11-05 17:22:28 +03:00
|
|
|
for(i = 0; i < NB_PORTS; i++) {
|
2009-09-17 00:25:29 +04:00
|
|
|
usb_register_port(&s->bus, &s->ports[i].port, s, i, uhci_attach);
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
|
|
|
s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s);
|
2010-02-03 18:49:39 +03:00
|
|
|
s->expire_time = qemu_get_clock(vm_clock) +
|
|
|
|
(get_ticks_per_sec() / FRAME_TIMER_FREQ);
|
2009-10-14 14:21:50 +04:00
|
|
|
s->num_ports_vmstate = NB_PORTS;
|
2005-11-05 17:22:28 +03:00
|
|
|
|
2009-06-27 11:25:07 +04:00
|
|
|
qemu_register_reset(uhci_reset, s);
|
2005-11-05 17:22:28 +03:00
|
|
|
|
2006-03-11 21:03:38 +03:00
|
|
|
/* Use region 4 for consistency with real hardware. BSD guests seem
|
|
|
|
to rely on this. */
|
2009-06-14 12:38:53 +04:00
|
|
|
pci_register_bar(&s->dev, 4, 0x20,
|
2009-10-30 15:21:03 +03:00
|
|
|
PCI_BASE_ADDRESS_SPACE_IO, uhci_map);
|
2008-08-21 23:33:09 +04:00
|
|
|
|
2009-08-31 16:24:02 +04:00
|
|
|
return 0;
|
2005-11-05 17:22:28 +03:00
|
|
|
}
|
2007-06-06 20:26:14 +04:00
|
|
|
|
2009-08-31 16:24:02 +04:00
|
|
|
static int usb_uhci_piix3_initfn(PCIDevice *dev)
|
2007-06-06 20:26:14 +04:00
|
|
|
{
|
2009-08-31 16:24:02 +04:00
|
|
|
UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
|
|
|
|
uint8_t *pci_conf = s->dev.config;
|
|
|
|
|
|
|
|
pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
|
|
|
|
pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_2);
|
|
|
|
return usb_uhci_common_initfn(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usb_uhci_piix4_initfn(PCIDevice *dev)
|
|
|
|
{
|
|
|
|
UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
|
|
|
|
uint8_t *pci_conf = s->dev.config;
|
2007-06-06 20:26:14 +04:00
|
|
|
|
2009-01-26 18:37:35 +03:00
|
|
|
pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
|
|
|
|
pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_2);
|
2009-08-31 16:24:02 +04:00
|
|
|
return usb_uhci_common_initfn(s);
|
|
|
|
}
|
2007-06-06 20:26:14 +04:00
|
|
|
|
2010-06-29 06:50:09 +04:00
|
|
|
static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
|
|
|
|
{
|
|
|
|
UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
|
|
|
|
uint8_t *pci_conf = s->dev.config;
|
|
|
|
|
|
|
|
pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_VIA);
|
|
|
|
pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_VIA_UHCI);
|
|
|
|
|
|
|
|
/* USB misc control 1/2 */
|
|
|
|
pci_set_long(pci_conf + 0x40,0x00001000);
|
|
|
|
/* PM capability */
|
|
|
|
pci_set_long(pci_conf + 0x80,0x00020001);
|
|
|
|
/* USB legacy support */
|
|
|
|
pci_set_long(pci_conf + 0xc0,0x00002000);
|
|
|
|
|
|
|
|
return usb_uhci_common_initfn(s);
|
|
|
|
}
|
|
|
|
|
2009-08-31 16:24:02 +04:00
|
|
|
static PCIDeviceInfo uhci_info[] = {
|
|
|
|
{
|
2009-12-09 19:07:53 +03:00
|
|
|
.qdev.name = "piix3-usb-uhci",
|
2009-08-31 16:24:02 +04:00
|
|
|
.qdev.size = sizeof(UHCIState),
|
2009-12-02 14:36:46 +03:00
|
|
|
.qdev.vmsd = &vmstate_uhci,
|
2009-08-31 16:24:02 +04:00
|
|
|
.init = usb_uhci_piix3_initfn,
|
|
|
|
},{
|
2009-12-09 19:07:53 +03:00
|
|
|
.qdev.name = "piix4-usb-uhci",
|
2009-08-31 16:24:02 +04:00
|
|
|
.qdev.size = sizeof(UHCIState),
|
2009-12-02 14:36:46 +03:00
|
|
|
.qdev.vmsd = &vmstate_uhci,
|
2009-08-31 16:24:02 +04:00
|
|
|
.init = usb_uhci_piix4_initfn,
|
2010-06-29 06:50:09 +04:00
|
|
|
},{
|
|
|
|
.qdev.name = "vt82c686b-usb-uhci",
|
|
|
|
.qdev.size = sizeof(UHCIState),
|
|
|
|
.qdev.vmsd = &vmstate_uhci,
|
|
|
|
.init = usb_uhci_vt82c686b_initfn,
|
2009-08-31 16:24:02 +04:00
|
|
|
},{
|
|
|
|
/* end of list */
|
2007-06-06 20:26:14 +04:00
|
|
|
}
|
2009-08-31 16:24:02 +04:00
|
|
|
};
|
2007-06-06 20:26:14 +04:00
|
|
|
|
2009-08-31 16:24:02 +04:00
|
|
|
static void uhci_register(void)
|
|
|
|
{
|
|
|
|
pci_qdev_register_many(uhci_info);
|
|
|
|
}
|
|
|
|
device_init(uhci_register);
|
2007-06-06 20:26:14 +04:00
|
|
|
|
2009-08-31 16:24:02 +04:00
|
|
|
void usb_uhci_piix3_init(PCIBus *bus, int devfn)
|
|
|
|
{
|
2009-12-09 19:07:53 +03:00
|
|
|
pci_create_simple(bus, devfn, "piix3-usb-uhci");
|
2009-08-31 16:24:02 +04:00
|
|
|
}
|
2008-08-21 23:30:31 +04:00
|
|
|
|
2009-08-31 16:24:02 +04:00
|
|
|
void usb_uhci_piix4_init(PCIBus *bus, int devfn)
|
|
|
|
{
|
2009-12-09 19:07:53 +03:00
|
|
|
pci_create_simple(bus, devfn, "piix4-usb-uhci");
|
2007-06-06 20:26:14 +04:00
|
|
|
}
|
2010-06-29 06:50:09 +04:00
|
|
|
|
|
|
|
void usb_uhci_vt82c686b_init(PCIBus *bus, int devfn)
|
|
|
|
{
|
|
|
|
pci_create_simple(bus, devfn, "vt82c686b-usb-uhci");
|
|
|
|
}
|