1f1ae32a1SGerd Hoffmann /* 2f1ae32a1SGerd Hoffmann * USB UHCI controller emulation 3f1ae32a1SGerd Hoffmann * 4f1ae32a1SGerd Hoffmann * Copyright (c) 2005 Fabrice Bellard 5f1ae32a1SGerd Hoffmann * 6f1ae32a1SGerd Hoffmann * Copyright (c) 2008 Max Krasnyansky 7f1ae32a1SGerd Hoffmann * Magor rewrite of the UHCI data structures parser and frame processor 8f1ae32a1SGerd Hoffmann * Support for fully async operation and multiple outstanding transactions 9f1ae32a1SGerd Hoffmann * 10f1ae32a1SGerd Hoffmann * Permission is hereby granted, free of charge, to any person obtaining a copy 11f1ae32a1SGerd Hoffmann * of this software and associated documentation files (the "Software"), to deal 12f1ae32a1SGerd Hoffmann * in the Software without restriction, including without limitation the rights 13f1ae32a1SGerd Hoffmann * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14f1ae32a1SGerd Hoffmann * copies of the Software, and to permit persons to whom the Software is 15f1ae32a1SGerd Hoffmann * furnished to do so, subject to the following conditions: 16f1ae32a1SGerd Hoffmann * 17f1ae32a1SGerd Hoffmann * The above copyright notice and this permission notice shall be included in 18f1ae32a1SGerd Hoffmann * all copies or substantial portions of the Software. 19f1ae32a1SGerd Hoffmann * 20f1ae32a1SGerd Hoffmann * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21f1ae32a1SGerd Hoffmann * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22f1ae32a1SGerd Hoffmann * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23f1ae32a1SGerd Hoffmann * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24f1ae32a1SGerd Hoffmann * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25f1ae32a1SGerd Hoffmann * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26f1ae32a1SGerd Hoffmann * THE SOFTWARE. 27f1ae32a1SGerd Hoffmann */ 28f1ae32a1SGerd Hoffmann #include "hw/hw.h" 29f1ae32a1SGerd Hoffmann #include "hw/usb.h" 30a2cb15b0SMichael S. Tsirkin #include "hw/pci/pci.h" 311de7afc9SPaolo Bonzini #include "qemu/timer.h" 321de7afc9SPaolo Bonzini #include "qemu/iov.h" 339c17d615SPaolo Bonzini #include "sysemu/dma.h" 3450dcc0f8SGerd Hoffmann #include "trace.h" 35*6a1751b7SAlex Bligh #include "qemu/main-loop.h" 36f1ae32a1SGerd Hoffmann 37f1ae32a1SGerd Hoffmann //#define DEBUG 38f1ae32a1SGerd Hoffmann //#define DEBUG_DUMP_DATA 39f1ae32a1SGerd Hoffmann 40f1ae32a1SGerd Hoffmann #define UHCI_CMD_FGR (1 << 4) 41f1ae32a1SGerd Hoffmann #define UHCI_CMD_EGSM (1 << 3) 42f1ae32a1SGerd Hoffmann #define UHCI_CMD_GRESET (1 << 2) 43f1ae32a1SGerd Hoffmann #define UHCI_CMD_HCRESET (1 << 1) 44f1ae32a1SGerd Hoffmann #define UHCI_CMD_RS (1 << 0) 45f1ae32a1SGerd Hoffmann 46f1ae32a1SGerd Hoffmann #define UHCI_STS_HCHALTED (1 << 5) 47f1ae32a1SGerd Hoffmann #define UHCI_STS_HCPERR (1 << 4) 48f1ae32a1SGerd Hoffmann #define UHCI_STS_HSERR (1 << 3) 49f1ae32a1SGerd Hoffmann #define UHCI_STS_RD (1 << 2) 50f1ae32a1SGerd Hoffmann #define UHCI_STS_USBERR (1 << 1) 51f1ae32a1SGerd Hoffmann #define UHCI_STS_USBINT (1 << 0) 52f1ae32a1SGerd Hoffmann 53f1ae32a1SGerd Hoffmann #define TD_CTRL_SPD (1 << 29) 54f1ae32a1SGerd Hoffmann #define TD_CTRL_ERROR_SHIFT 27 55f1ae32a1SGerd Hoffmann #define TD_CTRL_IOS (1 << 25) 56f1ae32a1SGerd Hoffmann #define TD_CTRL_IOC (1 << 24) 57f1ae32a1SGerd Hoffmann #define TD_CTRL_ACTIVE (1 << 23) 58f1ae32a1SGerd Hoffmann #define TD_CTRL_STALL (1 << 22) 59f1ae32a1SGerd Hoffmann #define TD_CTRL_BABBLE (1 << 20) 60f1ae32a1SGerd Hoffmann #define TD_CTRL_NAK (1 << 19) 61f1ae32a1SGerd Hoffmann #define TD_CTRL_TIMEOUT (1 << 18) 62f1ae32a1SGerd Hoffmann 63f1ae32a1SGerd Hoffmann #define UHCI_PORT_SUSPEND (1 << 12) 64f1ae32a1SGerd Hoffmann #define UHCI_PORT_RESET (1 << 9) 65f1ae32a1SGerd Hoffmann #define UHCI_PORT_LSDA (1 << 8) 66f1ae32a1SGerd Hoffmann #define UHCI_PORT_RD (1 << 6) 67f1ae32a1SGerd Hoffmann #define UHCI_PORT_ENC (1 << 3) 68f1ae32a1SGerd Hoffmann #define UHCI_PORT_EN (1 << 2) 69f1ae32a1SGerd Hoffmann #define UHCI_PORT_CSC (1 << 1) 70f1ae32a1SGerd Hoffmann #define UHCI_PORT_CCS (1 << 0) 71f1ae32a1SGerd Hoffmann 72f1ae32a1SGerd Hoffmann #define UHCI_PORT_READ_ONLY (0x1bb) 73f1ae32a1SGerd Hoffmann #define UHCI_PORT_WRITE_CLEAR (UHCI_PORT_CSC | UHCI_PORT_ENC) 74f1ae32a1SGerd Hoffmann 75f1ae32a1SGerd Hoffmann #define FRAME_TIMER_FREQ 1000 76f1ae32a1SGerd Hoffmann 77f1ae32a1SGerd Hoffmann #define FRAME_MAX_LOOPS 256 78f1ae32a1SGerd Hoffmann 79475443cfSHans de Goede /* Must be large enough to handle 10 frame delay for initial isoc requests */ 80475443cfSHans de Goede #define QH_VALID 32 81475443cfSHans de Goede 82f8f48b69SHans de Goede #define MAX_FRAMES_PER_TICK (QH_VALID / 2) 83f8f48b69SHans de Goede 84f1ae32a1SGerd Hoffmann #define NB_PORTS 2 85f1ae32a1SGerd Hoffmann 8660e1b2a6SGerd Hoffmann enum { 870cd178caSGerd Hoffmann TD_RESULT_STOP_FRAME = 10, 880cd178caSGerd Hoffmann TD_RESULT_COMPLETE, 890cd178caSGerd Hoffmann TD_RESULT_NEXT_QH, 904efe4ef3SGerd Hoffmann TD_RESULT_ASYNC_START, 914efe4ef3SGerd Hoffmann TD_RESULT_ASYNC_CONT, 9260e1b2a6SGerd Hoffmann }; 9360e1b2a6SGerd Hoffmann 94f1ae32a1SGerd Hoffmann typedef struct UHCIState UHCIState; 95f1ae32a1SGerd Hoffmann typedef struct UHCIAsync UHCIAsync; 96f1ae32a1SGerd Hoffmann typedef struct UHCIQueue UHCIQueue; 972c2e8525SGerd Hoffmann typedef struct UHCIInfo UHCIInfo; 988f3f90b0SGerd Hoffmann typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass; 992c2e8525SGerd Hoffmann 1002c2e8525SGerd Hoffmann struct UHCIInfo { 1012c2e8525SGerd Hoffmann const char *name; 1022c2e8525SGerd Hoffmann uint16_t vendor_id; 1032c2e8525SGerd Hoffmann uint16_t device_id; 1042c2e8525SGerd Hoffmann uint8_t revision; 1058f3f90b0SGerd Hoffmann uint8_t irq_pin; 1062c2e8525SGerd Hoffmann int (*initfn)(PCIDevice *dev); 1072c2e8525SGerd Hoffmann bool unplug; 1082c2e8525SGerd Hoffmann }; 109f1ae32a1SGerd Hoffmann 1108f3f90b0SGerd Hoffmann struct UHCIPCIDeviceClass { 1118f3f90b0SGerd Hoffmann PCIDeviceClass parent_class; 1128f3f90b0SGerd Hoffmann UHCIInfo info; 1138f3f90b0SGerd Hoffmann }; 1148f3f90b0SGerd Hoffmann 115f1ae32a1SGerd Hoffmann /* 116f1ae32a1SGerd Hoffmann * Pending async transaction. 117f1ae32a1SGerd Hoffmann * 'packet' must be the first field because completion 118f1ae32a1SGerd Hoffmann * handler does "(UHCIAsync *) pkt" cast. 119f1ae32a1SGerd Hoffmann */ 120f1ae32a1SGerd Hoffmann 121f1ae32a1SGerd Hoffmann struct UHCIAsync { 122f1ae32a1SGerd Hoffmann USBPacket packet; 1239822261cSHans de Goede uint8_t static_buf[64]; /* 64 bytes is enough, except for isoc packets */ 1249822261cSHans de Goede uint8_t *buf; 125f1ae32a1SGerd Hoffmann UHCIQueue *queue; 126f1ae32a1SGerd Hoffmann QTAILQ_ENTRY(UHCIAsync) next; 1271f250cc7SHans de Goede uint32_t td_addr; 128f1ae32a1SGerd Hoffmann uint8_t done; 129f1ae32a1SGerd Hoffmann }; 130f1ae32a1SGerd Hoffmann 131f1ae32a1SGerd Hoffmann struct UHCIQueue { 13266a08cbeSHans de Goede uint32_t qh_addr; 133f1ae32a1SGerd Hoffmann uint32_t token; 134f1ae32a1SGerd Hoffmann UHCIState *uhci; 13511d15e40SHans de Goede USBEndpoint *ep; 136f1ae32a1SGerd Hoffmann QTAILQ_ENTRY(UHCIQueue) next; 1378928c9c4SHans de Goede QTAILQ_HEAD(asyncs_head, UHCIAsync) asyncs; 138f1ae32a1SGerd Hoffmann int8_t valid; 139f1ae32a1SGerd Hoffmann }; 140f1ae32a1SGerd Hoffmann 141f1ae32a1SGerd Hoffmann typedef struct UHCIPort { 142f1ae32a1SGerd Hoffmann USBPort port; 143f1ae32a1SGerd Hoffmann uint16_t ctrl; 144f1ae32a1SGerd Hoffmann } UHCIPort; 145f1ae32a1SGerd Hoffmann 146f1ae32a1SGerd Hoffmann struct UHCIState { 147f1ae32a1SGerd Hoffmann PCIDevice dev; 148f1ae32a1SGerd Hoffmann MemoryRegion io_bar; 149f1ae32a1SGerd Hoffmann USBBus bus; /* Note unused when we're a companion controller */ 150f1ae32a1SGerd Hoffmann uint16_t cmd; /* cmd register */ 151f1ae32a1SGerd Hoffmann uint16_t status; 152f1ae32a1SGerd Hoffmann uint16_t intr; /* interrupt enable register */ 153f1ae32a1SGerd Hoffmann uint16_t frnum; /* frame number */ 154f1ae32a1SGerd Hoffmann uint32_t fl_base_addr; /* frame list base address */ 155f1ae32a1SGerd Hoffmann uint8_t sof_timing; 156f1ae32a1SGerd Hoffmann uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */ 157f1ae32a1SGerd Hoffmann int64_t expire_time; 158f1ae32a1SGerd Hoffmann QEMUTimer *frame_timer; 1599a16c595SGerd Hoffmann QEMUBH *bh; 1604aed20e2SGerd Hoffmann uint32_t frame_bytes; 16140141d12SGerd Hoffmann uint32_t frame_bandwidth; 16288793816SHans de Goede bool completions_only; 163f1ae32a1SGerd Hoffmann UHCIPort ports[NB_PORTS]; 164f1ae32a1SGerd Hoffmann 165f1ae32a1SGerd Hoffmann /* Interrupts that should be raised at the end of the current frame. */ 166f1ae32a1SGerd Hoffmann uint32_t pending_int_mask; 167973002c1SGerd Hoffmann int irq_pin; 168f1ae32a1SGerd Hoffmann 169f1ae32a1SGerd Hoffmann /* Active packets */ 170f1ae32a1SGerd Hoffmann QTAILQ_HEAD(, UHCIQueue) queues; 171f1ae32a1SGerd Hoffmann uint8_t num_ports_vmstate; 172f1ae32a1SGerd Hoffmann 173f1ae32a1SGerd Hoffmann /* Properties */ 174f1ae32a1SGerd Hoffmann char *masterbus; 175f1ae32a1SGerd Hoffmann uint32_t firstport; 1769fdf7027SHans de Goede uint32_t maxframes; 177f1ae32a1SGerd Hoffmann }; 178f1ae32a1SGerd Hoffmann 179f1ae32a1SGerd Hoffmann typedef struct UHCI_TD { 180f1ae32a1SGerd Hoffmann uint32_t link; 181f1ae32a1SGerd Hoffmann uint32_t ctrl; /* see TD_CTRL_xxx */ 182f1ae32a1SGerd Hoffmann uint32_t token; 183f1ae32a1SGerd Hoffmann uint32_t buffer; 184f1ae32a1SGerd Hoffmann } UHCI_TD; 185f1ae32a1SGerd Hoffmann 186f1ae32a1SGerd Hoffmann typedef struct UHCI_QH { 187f1ae32a1SGerd Hoffmann uint32_t link; 188f1ae32a1SGerd Hoffmann uint32_t el_link; 189f1ae32a1SGerd Hoffmann } UHCI_QH; 190f1ae32a1SGerd Hoffmann 19140507377SHans de Goede static void uhci_async_cancel(UHCIAsync *async); 19211d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td); 1939f0f1a0cSGerd Hoffmann static void uhci_resume(void *opaque); 19440507377SHans de Goede 195f1ae32a1SGerd Hoffmann static inline int32_t uhci_queue_token(UHCI_TD *td) 196f1ae32a1SGerd Hoffmann { 1976fe30910SHans de Goede if ((td->token & (0xf << 15)) == 0) { 1986fe30910SHans de Goede /* ctrl ep, cover ep and dev, not pid! */ 1996fe30910SHans de Goede return td->token & 0x7ff00; 2006fe30910SHans de Goede } else { 201f1ae32a1SGerd Hoffmann /* covers ep, dev, pid -> identifies the endpoint */ 202f1ae32a1SGerd Hoffmann return td->token & 0x7ffff; 203f1ae32a1SGerd Hoffmann } 2046fe30910SHans de Goede } 205f1ae32a1SGerd Hoffmann 20666a08cbeSHans de Goede static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td, 20766a08cbeSHans de Goede USBEndpoint *ep) 208f1ae32a1SGerd Hoffmann { 209f1ae32a1SGerd Hoffmann UHCIQueue *queue; 210f1ae32a1SGerd Hoffmann 211f1ae32a1SGerd Hoffmann queue = g_new0(UHCIQueue, 1); 212f1ae32a1SGerd Hoffmann queue->uhci = s; 21366a08cbeSHans de Goede queue->qh_addr = qh_addr; 21466a08cbeSHans de Goede queue->token = uhci_queue_token(td); 21511d15e40SHans de Goede queue->ep = ep; 216f1ae32a1SGerd Hoffmann QTAILQ_INIT(&queue->asyncs); 217f1ae32a1SGerd Hoffmann QTAILQ_INSERT_HEAD(&s->queues, queue, next); 218475443cfSHans de Goede queue->valid = QH_VALID; 21950dcc0f8SGerd Hoffmann trace_usb_uhci_queue_add(queue->token); 220f1ae32a1SGerd Hoffmann return queue; 221f1ae32a1SGerd Hoffmann } 222f1ae32a1SGerd Hoffmann 22366a08cbeSHans de Goede static void uhci_queue_free(UHCIQueue *queue, const char *reason) 224f1ae32a1SGerd Hoffmann { 225f1ae32a1SGerd Hoffmann UHCIState *s = queue->uhci; 22640507377SHans de Goede UHCIAsync *async; 22740507377SHans de Goede 22840507377SHans de Goede while (!QTAILQ_EMPTY(&queue->asyncs)) { 22940507377SHans de Goede async = QTAILQ_FIRST(&queue->asyncs); 23040507377SHans de Goede uhci_async_cancel(async); 23140507377SHans de Goede } 232f79738b0SHans de Goede usb_device_ep_stopped(queue->ep->dev, queue->ep); 233f1ae32a1SGerd Hoffmann 23466a08cbeSHans de Goede trace_usb_uhci_queue_del(queue->token, reason); 235f1ae32a1SGerd Hoffmann QTAILQ_REMOVE(&s->queues, queue, next); 236f1ae32a1SGerd Hoffmann g_free(queue); 237f1ae32a1SGerd Hoffmann } 238f1ae32a1SGerd Hoffmann 23966a08cbeSHans de Goede static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td) 24066a08cbeSHans de Goede { 24166a08cbeSHans de Goede uint32_t token = uhci_queue_token(td); 24266a08cbeSHans de Goede UHCIQueue *queue; 24366a08cbeSHans de Goede 24466a08cbeSHans de Goede QTAILQ_FOREACH(queue, &s->queues, next) { 24566a08cbeSHans de Goede if (queue->token == token) { 24666a08cbeSHans de Goede return queue; 24766a08cbeSHans de Goede } 24866a08cbeSHans de Goede } 24966a08cbeSHans de Goede return NULL; 25066a08cbeSHans de Goede } 25166a08cbeSHans de Goede 25266a08cbeSHans de Goede static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td, 25366a08cbeSHans de Goede uint32_t td_addr, bool queuing) 25466a08cbeSHans de Goede { 25566a08cbeSHans de Goede UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs); 25666a08cbeSHans de Goede 25766a08cbeSHans de Goede return queue->qh_addr == qh_addr && 25866a08cbeSHans de Goede queue->token == uhci_queue_token(td) && 25966a08cbeSHans de Goede (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL || 26066a08cbeSHans de Goede first->td_addr == td_addr); 26166a08cbeSHans de Goede } 26266a08cbeSHans de Goede 2631f250cc7SHans de Goede static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr) 264f1ae32a1SGerd Hoffmann { 265f1ae32a1SGerd Hoffmann UHCIAsync *async = g_new0(UHCIAsync, 1); 266f1ae32a1SGerd Hoffmann 267f1ae32a1SGerd Hoffmann async->queue = queue; 2681f250cc7SHans de Goede async->td_addr = td_addr; 269f1ae32a1SGerd Hoffmann usb_packet_init(&async->packet); 2701f250cc7SHans de Goede trace_usb_uhci_packet_add(async->queue->token, async->td_addr); 271f1ae32a1SGerd Hoffmann 272f1ae32a1SGerd Hoffmann return async; 273f1ae32a1SGerd Hoffmann } 274f1ae32a1SGerd Hoffmann 275f1ae32a1SGerd Hoffmann static void uhci_async_free(UHCIAsync *async) 276f1ae32a1SGerd Hoffmann { 2771f250cc7SHans de Goede trace_usb_uhci_packet_del(async->queue->token, async->td_addr); 278f1ae32a1SGerd Hoffmann usb_packet_cleanup(&async->packet); 2799822261cSHans de Goede if (async->buf != async->static_buf) { 2809822261cSHans de Goede g_free(async->buf); 2819822261cSHans de Goede } 282f1ae32a1SGerd Hoffmann g_free(async); 283f1ae32a1SGerd Hoffmann } 284f1ae32a1SGerd Hoffmann 285f1ae32a1SGerd Hoffmann static void uhci_async_link(UHCIAsync *async) 286f1ae32a1SGerd Hoffmann { 287f1ae32a1SGerd Hoffmann UHCIQueue *queue = async->queue; 288f1ae32a1SGerd Hoffmann QTAILQ_INSERT_TAIL(&queue->asyncs, async, next); 2891f250cc7SHans de Goede trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr); 290f1ae32a1SGerd Hoffmann } 291f1ae32a1SGerd Hoffmann 292f1ae32a1SGerd Hoffmann static void uhci_async_unlink(UHCIAsync *async) 293f1ae32a1SGerd Hoffmann { 294f1ae32a1SGerd Hoffmann UHCIQueue *queue = async->queue; 295f1ae32a1SGerd Hoffmann QTAILQ_REMOVE(&queue->asyncs, async, next); 2961f250cc7SHans de Goede trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr); 297f1ae32a1SGerd Hoffmann } 298f1ae32a1SGerd Hoffmann 299f1ae32a1SGerd Hoffmann static void uhci_async_cancel(UHCIAsync *async) 300f1ae32a1SGerd Hoffmann { 3012f2ee268SHans de Goede uhci_async_unlink(async); 3021f250cc7SHans de Goede trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr, 3031f250cc7SHans de Goede async->done); 304f1ae32a1SGerd Hoffmann if (!async->done) 305f1ae32a1SGerd Hoffmann usb_cancel_packet(&async->packet); 306f1ae32a1SGerd Hoffmann uhci_async_free(async); 307f1ae32a1SGerd Hoffmann } 308f1ae32a1SGerd Hoffmann 309f1ae32a1SGerd Hoffmann /* 310f1ae32a1SGerd Hoffmann * Mark all outstanding async packets as invalid. 311f1ae32a1SGerd Hoffmann * This is used for canceling them when TDs are removed by the HCD. 312f1ae32a1SGerd Hoffmann */ 313f1ae32a1SGerd Hoffmann static void uhci_async_validate_begin(UHCIState *s) 314f1ae32a1SGerd Hoffmann { 315f1ae32a1SGerd Hoffmann UHCIQueue *queue; 316f1ae32a1SGerd Hoffmann 317f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(queue, &s->queues, next) { 318f1ae32a1SGerd Hoffmann queue->valid--; 319f1ae32a1SGerd Hoffmann } 320f1ae32a1SGerd Hoffmann } 321f1ae32a1SGerd Hoffmann 322f1ae32a1SGerd Hoffmann /* 323f1ae32a1SGerd Hoffmann * Cancel async packets that are no longer valid 324f1ae32a1SGerd Hoffmann */ 325f1ae32a1SGerd Hoffmann static void uhci_async_validate_end(UHCIState *s) 326f1ae32a1SGerd Hoffmann { 327f1ae32a1SGerd Hoffmann UHCIQueue *queue, *n; 328f1ae32a1SGerd Hoffmann 329f1ae32a1SGerd Hoffmann QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) { 33040507377SHans de Goede if (!queue->valid) { 33166a08cbeSHans de Goede uhci_queue_free(queue, "validate-end"); 332f1ae32a1SGerd Hoffmann } 333f1ae32a1SGerd Hoffmann } 33440507377SHans de Goede } 335f1ae32a1SGerd Hoffmann 336f1ae32a1SGerd Hoffmann static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev) 337f1ae32a1SGerd Hoffmann { 3385ad23e87SHans de Goede UHCIQueue *queue, *n; 339f1ae32a1SGerd Hoffmann 3405ad23e87SHans de Goede QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) { 3415ad23e87SHans de Goede if (queue->ep->dev == dev) { 3425ad23e87SHans de Goede uhci_queue_free(queue, "cancel-device"); 343f1ae32a1SGerd Hoffmann } 344f1ae32a1SGerd Hoffmann } 345f1ae32a1SGerd Hoffmann } 346f1ae32a1SGerd Hoffmann 347f1ae32a1SGerd Hoffmann static void uhci_async_cancel_all(UHCIState *s) 348f1ae32a1SGerd Hoffmann { 34977fa9aeeSGerd Hoffmann UHCIQueue *queue, *nq; 350f1ae32a1SGerd Hoffmann 35177fa9aeeSGerd Hoffmann QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) { 35266a08cbeSHans de Goede uhci_queue_free(queue, "cancel-all"); 353f1ae32a1SGerd Hoffmann } 354f1ae32a1SGerd Hoffmann } 355f1ae32a1SGerd Hoffmann 3568c75a899SHans de Goede static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr) 357f1ae32a1SGerd Hoffmann { 358f1ae32a1SGerd Hoffmann UHCIQueue *queue; 359f1ae32a1SGerd Hoffmann UHCIAsync *async; 360f1ae32a1SGerd Hoffmann 361f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(queue, &s->queues, next) { 362f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(async, &queue->asyncs, next) { 3631f250cc7SHans de Goede if (async->td_addr == td_addr) { 364f1ae32a1SGerd Hoffmann return async; 365f1ae32a1SGerd Hoffmann } 366f1ae32a1SGerd Hoffmann } 3678c75a899SHans de Goede } 368f1ae32a1SGerd Hoffmann return NULL; 369f1ae32a1SGerd Hoffmann } 370f1ae32a1SGerd Hoffmann 371f1ae32a1SGerd Hoffmann static void uhci_update_irq(UHCIState *s) 372f1ae32a1SGerd Hoffmann { 373f1ae32a1SGerd Hoffmann int level; 374f1ae32a1SGerd Hoffmann if (((s->status2 & 1) && (s->intr & (1 << 2))) || 375f1ae32a1SGerd Hoffmann ((s->status2 & 2) && (s->intr & (1 << 3))) || 376f1ae32a1SGerd Hoffmann ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) || 377f1ae32a1SGerd Hoffmann ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) || 378f1ae32a1SGerd Hoffmann (s->status & UHCI_STS_HSERR) || 379f1ae32a1SGerd Hoffmann (s->status & UHCI_STS_HCPERR)) { 380f1ae32a1SGerd Hoffmann level = 1; 381f1ae32a1SGerd Hoffmann } else { 382f1ae32a1SGerd Hoffmann level = 0; 383f1ae32a1SGerd Hoffmann } 384973002c1SGerd Hoffmann qemu_set_irq(s->dev.irq[s->irq_pin], level); 385f1ae32a1SGerd Hoffmann } 386f1ae32a1SGerd Hoffmann 387f1ae32a1SGerd Hoffmann static void uhci_reset(void *opaque) 388f1ae32a1SGerd Hoffmann { 389f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 390f1ae32a1SGerd Hoffmann uint8_t *pci_conf; 391f1ae32a1SGerd Hoffmann int i; 392f1ae32a1SGerd Hoffmann UHCIPort *port; 393f1ae32a1SGerd Hoffmann 39450dcc0f8SGerd Hoffmann trace_usb_uhci_reset(); 395f1ae32a1SGerd Hoffmann 396f1ae32a1SGerd Hoffmann pci_conf = s->dev.config; 397f1ae32a1SGerd Hoffmann 398f1ae32a1SGerd Hoffmann pci_conf[0x6a] = 0x01; /* usb clock */ 399f1ae32a1SGerd Hoffmann pci_conf[0x6b] = 0x00; 400f1ae32a1SGerd Hoffmann s->cmd = 0; 401f1ae32a1SGerd Hoffmann s->status = 0; 402f1ae32a1SGerd Hoffmann s->status2 = 0; 403f1ae32a1SGerd Hoffmann s->intr = 0; 404f1ae32a1SGerd Hoffmann s->fl_base_addr = 0; 405f1ae32a1SGerd Hoffmann s->sof_timing = 64; 406f1ae32a1SGerd Hoffmann 407f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 408f1ae32a1SGerd Hoffmann port = &s->ports[i]; 409f1ae32a1SGerd Hoffmann port->ctrl = 0x0080; 410f1ae32a1SGerd Hoffmann if (port->port.dev && port->port.dev->attached) { 411f1ae32a1SGerd Hoffmann usb_port_reset(&port->port); 412f1ae32a1SGerd Hoffmann } 413f1ae32a1SGerd Hoffmann } 414f1ae32a1SGerd Hoffmann 415f1ae32a1SGerd Hoffmann uhci_async_cancel_all(s); 4169a16c595SGerd Hoffmann qemu_bh_cancel(s->bh); 417aba1f242SGerd Hoffmann uhci_update_irq(s); 418f1ae32a1SGerd Hoffmann } 419f1ae32a1SGerd Hoffmann 420f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci_port = { 421f1ae32a1SGerd Hoffmann .name = "uhci port", 422f1ae32a1SGerd Hoffmann .version_id = 1, 423f1ae32a1SGerd Hoffmann .minimum_version_id = 1, 424f1ae32a1SGerd Hoffmann .minimum_version_id_old = 1, 425f1ae32a1SGerd Hoffmann .fields = (VMStateField []) { 426f1ae32a1SGerd Hoffmann VMSTATE_UINT16(ctrl, UHCIPort), 427f1ae32a1SGerd Hoffmann VMSTATE_END_OF_LIST() 428f1ae32a1SGerd Hoffmann } 429f1ae32a1SGerd Hoffmann }; 430f1ae32a1SGerd Hoffmann 43175f151cdSGerd Hoffmann static int uhci_post_load(void *opaque, int version_id) 43275f151cdSGerd Hoffmann { 43375f151cdSGerd Hoffmann UHCIState *s = opaque; 43475f151cdSGerd Hoffmann 43575f151cdSGerd Hoffmann if (version_id < 2) { 43675f151cdSGerd Hoffmann s->expire_time = qemu_get_clock_ns(vm_clock) + 43775f151cdSGerd Hoffmann (get_ticks_per_sec() / FRAME_TIMER_FREQ); 43875f151cdSGerd Hoffmann } 43975f151cdSGerd Hoffmann return 0; 44075f151cdSGerd Hoffmann } 44175f151cdSGerd Hoffmann 442f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci = { 443f1ae32a1SGerd Hoffmann .name = "uhci", 444ecfdc15fSHans de Goede .version_id = 3, 445f1ae32a1SGerd Hoffmann .minimum_version_id = 1, 446f1ae32a1SGerd Hoffmann .minimum_version_id_old = 1, 44775f151cdSGerd Hoffmann .post_load = uhci_post_load, 448f1ae32a1SGerd Hoffmann .fields = (VMStateField []) { 449f1ae32a1SGerd Hoffmann VMSTATE_PCI_DEVICE(dev, UHCIState), 450f1ae32a1SGerd Hoffmann VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState), 451f1ae32a1SGerd Hoffmann VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1, 452f1ae32a1SGerd Hoffmann vmstate_uhci_port, UHCIPort), 453f1ae32a1SGerd Hoffmann VMSTATE_UINT16(cmd, UHCIState), 454f1ae32a1SGerd Hoffmann VMSTATE_UINT16(status, UHCIState), 455f1ae32a1SGerd Hoffmann VMSTATE_UINT16(intr, UHCIState), 456f1ae32a1SGerd Hoffmann VMSTATE_UINT16(frnum, UHCIState), 457f1ae32a1SGerd Hoffmann VMSTATE_UINT32(fl_base_addr, UHCIState), 458f1ae32a1SGerd Hoffmann VMSTATE_UINT8(sof_timing, UHCIState), 459f1ae32a1SGerd Hoffmann VMSTATE_UINT8(status2, UHCIState), 460f1ae32a1SGerd Hoffmann VMSTATE_TIMER(frame_timer, UHCIState), 461f1ae32a1SGerd Hoffmann VMSTATE_INT64_V(expire_time, UHCIState, 2), 462ecfdc15fSHans de Goede VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3), 463f1ae32a1SGerd Hoffmann VMSTATE_END_OF_LIST() 464f1ae32a1SGerd Hoffmann } 465f1ae32a1SGerd Hoffmann }; 466f1ae32a1SGerd Hoffmann 46789eb147cSGerd Hoffmann static void uhci_port_write(void *opaque, hwaddr addr, 46889eb147cSGerd Hoffmann uint64_t val, unsigned size) 469f1ae32a1SGerd Hoffmann { 470f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 471f1ae32a1SGerd Hoffmann 47250dcc0f8SGerd Hoffmann trace_usb_uhci_mmio_writew(addr, val); 473f1ae32a1SGerd Hoffmann 474f1ae32a1SGerd Hoffmann switch(addr) { 475f1ae32a1SGerd Hoffmann case 0x00: 476f1ae32a1SGerd Hoffmann if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) { 477f1ae32a1SGerd Hoffmann /* start frame processing */ 47850dcc0f8SGerd Hoffmann trace_usb_uhci_schedule_start(); 479f1ae32a1SGerd Hoffmann s->expire_time = qemu_get_clock_ns(vm_clock) + 480f1ae32a1SGerd Hoffmann (get_ticks_per_sec() / FRAME_TIMER_FREQ); 481f8f48b69SHans de Goede qemu_mod_timer(s->frame_timer, s->expire_time); 482f1ae32a1SGerd Hoffmann s->status &= ~UHCI_STS_HCHALTED; 483f1ae32a1SGerd Hoffmann } else if (!(val & UHCI_CMD_RS)) { 484f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCHALTED; 485f1ae32a1SGerd Hoffmann } 486f1ae32a1SGerd Hoffmann if (val & UHCI_CMD_GRESET) { 487f1ae32a1SGerd Hoffmann UHCIPort *port; 488f1ae32a1SGerd Hoffmann int i; 489f1ae32a1SGerd Hoffmann 490f1ae32a1SGerd Hoffmann /* send reset on the USB bus */ 491f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 492f1ae32a1SGerd Hoffmann port = &s->ports[i]; 493f1ae32a1SGerd Hoffmann usb_device_reset(port->port.dev); 494f1ae32a1SGerd Hoffmann } 495f1ae32a1SGerd Hoffmann uhci_reset(s); 496f1ae32a1SGerd Hoffmann return; 497f1ae32a1SGerd Hoffmann } 498f1ae32a1SGerd Hoffmann if (val & UHCI_CMD_HCRESET) { 499f1ae32a1SGerd Hoffmann uhci_reset(s); 500f1ae32a1SGerd Hoffmann return; 501f1ae32a1SGerd Hoffmann } 502f1ae32a1SGerd Hoffmann s->cmd = val; 5039f0f1a0cSGerd Hoffmann if (val & UHCI_CMD_EGSM) { 5049f0f1a0cSGerd Hoffmann if ((s->ports[0].ctrl & UHCI_PORT_RD) || 5059f0f1a0cSGerd Hoffmann (s->ports[1].ctrl & UHCI_PORT_RD)) { 5069f0f1a0cSGerd Hoffmann uhci_resume(s); 5079f0f1a0cSGerd Hoffmann } 5089f0f1a0cSGerd Hoffmann } 509f1ae32a1SGerd Hoffmann break; 510f1ae32a1SGerd Hoffmann case 0x02: 511f1ae32a1SGerd Hoffmann s->status &= ~val; 512f1ae32a1SGerd Hoffmann /* XXX: the chip spec is not coherent, so we add a hidden 513f1ae32a1SGerd Hoffmann register to distinguish between IOC and SPD */ 514f1ae32a1SGerd Hoffmann if (val & UHCI_STS_USBINT) 515f1ae32a1SGerd Hoffmann s->status2 = 0; 516f1ae32a1SGerd Hoffmann uhci_update_irq(s); 517f1ae32a1SGerd Hoffmann break; 518f1ae32a1SGerd Hoffmann case 0x04: 519f1ae32a1SGerd Hoffmann s->intr = val; 520f1ae32a1SGerd Hoffmann uhci_update_irq(s); 521f1ae32a1SGerd Hoffmann break; 522f1ae32a1SGerd Hoffmann case 0x06: 523f1ae32a1SGerd Hoffmann if (s->status & UHCI_STS_HCHALTED) 524f1ae32a1SGerd Hoffmann s->frnum = val & 0x7ff; 525f1ae32a1SGerd Hoffmann break; 52689eb147cSGerd Hoffmann case 0x08: 52789eb147cSGerd Hoffmann s->fl_base_addr &= 0xffff0000; 52889eb147cSGerd Hoffmann s->fl_base_addr |= val & ~0xfff; 52989eb147cSGerd Hoffmann break; 53089eb147cSGerd Hoffmann case 0x0a: 53189eb147cSGerd Hoffmann s->fl_base_addr &= 0x0000ffff; 53289eb147cSGerd Hoffmann s->fl_base_addr |= (val << 16); 53389eb147cSGerd Hoffmann break; 53489eb147cSGerd Hoffmann case 0x0c: 53589eb147cSGerd Hoffmann s->sof_timing = val & 0xff; 53689eb147cSGerd Hoffmann break; 537f1ae32a1SGerd Hoffmann case 0x10 ... 0x1f: 538f1ae32a1SGerd Hoffmann { 539f1ae32a1SGerd Hoffmann UHCIPort *port; 540f1ae32a1SGerd Hoffmann USBDevice *dev; 541f1ae32a1SGerd Hoffmann int n; 542f1ae32a1SGerd Hoffmann 543f1ae32a1SGerd Hoffmann n = (addr >> 1) & 7; 544f1ae32a1SGerd Hoffmann if (n >= NB_PORTS) 545f1ae32a1SGerd Hoffmann return; 546f1ae32a1SGerd Hoffmann port = &s->ports[n]; 547f1ae32a1SGerd Hoffmann dev = port->port.dev; 548f1ae32a1SGerd Hoffmann if (dev && dev->attached) { 549f1ae32a1SGerd Hoffmann /* port reset */ 550f1ae32a1SGerd Hoffmann if ( (val & UHCI_PORT_RESET) && 551f1ae32a1SGerd Hoffmann !(port->ctrl & UHCI_PORT_RESET) ) { 552f1ae32a1SGerd Hoffmann usb_device_reset(dev); 553f1ae32a1SGerd Hoffmann } 554f1ae32a1SGerd Hoffmann } 555f1ae32a1SGerd Hoffmann port->ctrl &= UHCI_PORT_READ_ONLY; 5561cbdde90SHans de Goede /* enabled may only be set if a device is connected */ 5571cbdde90SHans de Goede if (!(port->ctrl & UHCI_PORT_CCS)) { 5581cbdde90SHans de Goede val &= ~UHCI_PORT_EN; 5591cbdde90SHans de Goede } 560f1ae32a1SGerd Hoffmann port->ctrl |= (val & ~UHCI_PORT_READ_ONLY); 561f1ae32a1SGerd Hoffmann /* some bits are reset when a '1' is written to them */ 562f1ae32a1SGerd Hoffmann port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR); 563f1ae32a1SGerd Hoffmann } 564f1ae32a1SGerd Hoffmann break; 565f1ae32a1SGerd Hoffmann } 566f1ae32a1SGerd Hoffmann } 567f1ae32a1SGerd Hoffmann 56889eb147cSGerd Hoffmann static uint64_t uhci_port_read(void *opaque, hwaddr addr, unsigned size) 569f1ae32a1SGerd Hoffmann { 570f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 571f1ae32a1SGerd Hoffmann uint32_t val; 572f1ae32a1SGerd Hoffmann 573f1ae32a1SGerd Hoffmann switch(addr) { 574f1ae32a1SGerd Hoffmann case 0x00: 575f1ae32a1SGerd Hoffmann val = s->cmd; 576f1ae32a1SGerd Hoffmann break; 577f1ae32a1SGerd Hoffmann case 0x02: 578f1ae32a1SGerd Hoffmann val = s->status; 579f1ae32a1SGerd Hoffmann break; 580f1ae32a1SGerd Hoffmann case 0x04: 581f1ae32a1SGerd Hoffmann val = s->intr; 582f1ae32a1SGerd Hoffmann break; 583f1ae32a1SGerd Hoffmann case 0x06: 584f1ae32a1SGerd Hoffmann val = s->frnum; 585f1ae32a1SGerd Hoffmann break; 58689eb147cSGerd Hoffmann case 0x08: 58789eb147cSGerd Hoffmann val = s->fl_base_addr & 0xffff; 58889eb147cSGerd Hoffmann break; 58989eb147cSGerd Hoffmann case 0x0a: 59089eb147cSGerd Hoffmann val = (s->fl_base_addr >> 16) & 0xffff; 59189eb147cSGerd Hoffmann break; 59289eb147cSGerd Hoffmann case 0x0c: 59389eb147cSGerd Hoffmann val = s->sof_timing; 59489eb147cSGerd Hoffmann break; 595f1ae32a1SGerd Hoffmann case 0x10 ... 0x1f: 596f1ae32a1SGerd Hoffmann { 597f1ae32a1SGerd Hoffmann UHCIPort *port; 598f1ae32a1SGerd Hoffmann int n; 599f1ae32a1SGerd Hoffmann n = (addr >> 1) & 7; 600f1ae32a1SGerd Hoffmann if (n >= NB_PORTS) 601f1ae32a1SGerd Hoffmann goto read_default; 602f1ae32a1SGerd Hoffmann port = &s->ports[n]; 603f1ae32a1SGerd Hoffmann val = port->ctrl; 604f1ae32a1SGerd Hoffmann } 605f1ae32a1SGerd Hoffmann break; 606f1ae32a1SGerd Hoffmann default: 607f1ae32a1SGerd Hoffmann read_default: 608f1ae32a1SGerd Hoffmann val = 0xff7f; /* disabled port */ 609f1ae32a1SGerd Hoffmann break; 610f1ae32a1SGerd Hoffmann } 611f1ae32a1SGerd Hoffmann 61250dcc0f8SGerd Hoffmann trace_usb_uhci_mmio_readw(addr, val); 613f1ae32a1SGerd Hoffmann 614f1ae32a1SGerd Hoffmann return val; 615f1ae32a1SGerd Hoffmann } 616f1ae32a1SGerd Hoffmann 617f1ae32a1SGerd Hoffmann /* signal resume if controller suspended */ 618f1ae32a1SGerd Hoffmann static void uhci_resume (void *opaque) 619f1ae32a1SGerd Hoffmann { 620f1ae32a1SGerd Hoffmann UHCIState *s = (UHCIState *)opaque; 621f1ae32a1SGerd Hoffmann 622f1ae32a1SGerd Hoffmann if (!s) 623f1ae32a1SGerd Hoffmann return; 624f1ae32a1SGerd Hoffmann 625f1ae32a1SGerd Hoffmann if (s->cmd & UHCI_CMD_EGSM) { 626f1ae32a1SGerd Hoffmann s->cmd |= UHCI_CMD_FGR; 627f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_RD; 628f1ae32a1SGerd Hoffmann uhci_update_irq(s); 629f1ae32a1SGerd Hoffmann } 630f1ae32a1SGerd Hoffmann } 631f1ae32a1SGerd Hoffmann 632f1ae32a1SGerd Hoffmann static void uhci_attach(USBPort *port1) 633f1ae32a1SGerd Hoffmann { 634f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 635f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 636f1ae32a1SGerd Hoffmann 637f1ae32a1SGerd Hoffmann /* set connect status */ 638f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC; 639f1ae32a1SGerd Hoffmann 640f1ae32a1SGerd Hoffmann /* update speed */ 641f1ae32a1SGerd Hoffmann if (port->port.dev->speed == USB_SPEED_LOW) { 642f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_LSDA; 643f1ae32a1SGerd Hoffmann } else { 644f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_LSDA; 645f1ae32a1SGerd Hoffmann } 646f1ae32a1SGerd Hoffmann 647f1ae32a1SGerd Hoffmann uhci_resume(s); 648f1ae32a1SGerd Hoffmann } 649f1ae32a1SGerd Hoffmann 650f1ae32a1SGerd Hoffmann static void uhci_detach(USBPort *port1) 651f1ae32a1SGerd Hoffmann { 652f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 653f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 654f1ae32a1SGerd Hoffmann 655f1ae32a1SGerd Hoffmann uhci_async_cancel_device(s, port1->dev); 656f1ae32a1SGerd Hoffmann 657f1ae32a1SGerd Hoffmann /* set connect status */ 658f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_CCS) { 659f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_CCS; 660f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_CSC; 661f1ae32a1SGerd Hoffmann } 662f1ae32a1SGerd Hoffmann /* disable port */ 663f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_EN) { 664f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_EN; 665f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_ENC; 666f1ae32a1SGerd Hoffmann } 667f1ae32a1SGerd Hoffmann 668f1ae32a1SGerd Hoffmann uhci_resume(s); 669f1ae32a1SGerd Hoffmann } 670f1ae32a1SGerd Hoffmann 671f1ae32a1SGerd Hoffmann static void uhci_child_detach(USBPort *port1, USBDevice *child) 672f1ae32a1SGerd Hoffmann { 673f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 674f1ae32a1SGerd Hoffmann 675f1ae32a1SGerd Hoffmann uhci_async_cancel_device(s, child); 676f1ae32a1SGerd Hoffmann } 677f1ae32a1SGerd Hoffmann 678f1ae32a1SGerd Hoffmann static void uhci_wakeup(USBPort *port1) 679f1ae32a1SGerd Hoffmann { 680f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 681f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 682f1ae32a1SGerd Hoffmann 683f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) { 684f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_RD; 685f1ae32a1SGerd Hoffmann uhci_resume(s); 686f1ae32a1SGerd Hoffmann } 687f1ae32a1SGerd Hoffmann } 688f1ae32a1SGerd Hoffmann 689f1ae32a1SGerd Hoffmann static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr) 690f1ae32a1SGerd Hoffmann { 691f1ae32a1SGerd Hoffmann USBDevice *dev; 692f1ae32a1SGerd Hoffmann int i; 693f1ae32a1SGerd Hoffmann 694f1ae32a1SGerd Hoffmann for (i = 0; i < NB_PORTS; i++) { 695f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[i]; 696f1ae32a1SGerd Hoffmann if (!(port->ctrl & UHCI_PORT_EN)) { 697f1ae32a1SGerd Hoffmann continue; 698f1ae32a1SGerd Hoffmann } 699f1ae32a1SGerd Hoffmann dev = usb_find_device(&port->port, addr); 700f1ae32a1SGerd Hoffmann if (dev != NULL) { 701f1ae32a1SGerd Hoffmann return dev; 702f1ae32a1SGerd Hoffmann } 703f1ae32a1SGerd Hoffmann } 704f1ae32a1SGerd Hoffmann return NULL; 705f1ae32a1SGerd Hoffmann } 706f1ae32a1SGerd Hoffmann 707963a68b5SHans de Goede static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link) 708963a68b5SHans de Goede { 709963a68b5SHans de Goede pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td)); 710963a68b5SHans de Goede le32_to_cpus(&td->link); 711963a68b5SHans de Goede le32_to_cpus(&td->ctrl); 712963a68b5SHans de Goede le32_to_cpus(&td->token); 713963a68b5SHans de Goede le32_to_cpus(&td->buffer); 714963a68b5SHans de Goede } 715963a68b5SHans de Goede 716faccca00SHans de Goede static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr, 717faccca00SHans de Goede int status, uint32_t *int_mask) 718faccca00SHans de Goede { 719faccca00SHans de Goede uint32_t queue_token = uhci_queue_token(td); 720faccca00SHans de Goede int ret; 721faccca00SHans de Goede 722faccca00SHans de Goede switch (status) { 723faccca00SHans de Goede case USB_RET_NAK: 724faccca00SHans de Goede td->ctrl |= TD_CTRL_NAK; 725faccca00SHans de Goede return TD_RESULT_NEXT_QH; 726faccca00SHans de Goede 727faccca00SHans de Goede case USB_RET_STALL: 728faccca00SHans de Goede td->ctrl |= TD_CTRL_STALL; 729faccca00SHans de Goede trace_usb_uhci_packet_complete_stall(queue_token, td_addr); 730faccca00SHans de Goede ret = TD_RESULT_NEXT_QH; 731faccca00SHans de Goede break; 732faccca00SHans de Goede 733faccca00SHans de Goede case USB_RET_BABBLE: 734faccca00SHans de Goede td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL; 735faccca00SHans de Goede /* frame interrupted */ 736faccca00SHans de Goede trace_usb_uhci_packet_complete_babble(queue_token, td_addr); 737faccca00SHans de Goede ret = TD_RESULT_STOP_FRAME; 738faccca00SHans de Goede break; 739faccca00SHans de Goede 740faccca00SHans de Goede case USB_RET_IOERROR: 741faccca00SHans de Goede case USB_RET_NODEV: 742faccca00SHans de Goede default: 743faccca00SHans de Goede td->ctrl |= TD_CTRL_TIMEOUT; 744faccca00SHans de Goede td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT); 745faccca00SHans de Goede trace_usb_uhci_packet_complete_error(queue_token, td_addr); 746faccca00SHans de Goede ret = TD_RESULT_NEXT_QH; 747faccca00SHans de Goede break; 748faccca00SHans de Goede } 749faccca00SHans de Goede 750faccca00SHans de Goede td->ctrl &= ~TD_CTRL_ACTIVE; 751faccca00SHans de Goede s->status |= UHCI_STS_USBERR; 752faccca00SHans de Goede if (td->ctrl & TD_CTRL_IOC) { 753faccca00SHans de Goede *int_mask |= 0x01; 754faccca00SHans de Goede } 755faccca00SHans de Goede uhci_update_irq(s); 756faccca00SHans de Goede return ret; 757faccca00SHans de Goede } 758faccca00SHans de Goede 759f1ae32a1SGerd Hoffmann static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask) 760f1ae32a1SGerd Hoffmann { 7619a77a0f5SHans de Goede int len = 0, max_len; 762f1ae32a1SGerd Hoffmann uint8_t pid; 763f1ae32a1SGerd Hoffmann 764f1ae32a1SGerd Hoffmann max_len = ((td->token >> 21) + 1) & 0x7ff; 765f1ae32a1SGerd Hoffmann pid = td->token & 0xff; 766f1ae32a1SGerd Hoffmann 767f1ae32a1SGerd Hoffmann if (td->ctrl & TD_CTRL_IOS) 768f1ae32a1SGerd Hoffmann td->ctrl &= ~TD_CTRL_ACTIVE; 769f1ae32a1SGerd Hoffmann 7709a77a0f5SHans de Goede if (async->packet.status != USB_RET_SUCCESS) { 7719a77a0f5SHans de Goede return uhci_handle_td_error(s, td, async->td_addr, 7729a77a0f5SHans de Goede async->packet.status, int_mask); 773faccca00SHans de Goede } 774f1ae32a1SGerd Hoffmann 7759a77a0f5SHans de Goede len = async->packet.actual_length; 776f1ae32a1SGerd Hoffmann td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff); 777f1ae32a1SGerd Hoffmann 778f1ae32a1SGerd Hoffmann /* The NAK bit may have been set by a previous frame, so clear it 779f1ae32a1SGerd Hoffmann here. The docs are somewhat unclear, but win2k relies on this 780f1ae32a1SGerd Hoffmann behavior. */ 781f1ae32a1SGerd Hoffmann td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK); 782f1ae32a1SGerd Hoffmann if (td->ctrl & TD_CTRL_IOC) 783f1ae32a1SGerd Hoffmann *int_mask |= 0x01; 784f1ae32a1SGerd Hoffmann 785f1ae32a1SGerd Hoffmann if (pid == USB_TOKEN_IN) { 7869822261cSHans de Goede pci_dma_write(&s->dev, td->buffer, async->buf, len); 787f1ae32a1SGerd Hoffmann if ((td->ctrl & TD_CTRL_SPD) && len < max_len) { 788f1ae32a1SGerd Hoffmann *int_mask |= 0x02; 789f1ae32a1SGerd Hoffmann /* short packet: do not update QH */ 79050dcc0f8SGerd Hoffmann trace_usb_uhci_packet_complete_shortxfer(async->queue->token, 7911f250cc7SHans de Goede async->td_addr); 79260e1b2a6SGerd Hoffmann return TD_RESULT_NEXT_QH; 793f1ae32a1SGerd Hoffmann } 794f1ae32a1SGerd Hoffmann } 795f1ae32a1SGerd Hoffmann 796f1ae32a1SGerd Hoffmann /* success */ 7971f250cc7SHans de Goede trace_usb_uhci_packet_complete_success(async->queue->token, 7981f250cc7SHans de Goede async->td_addr); 79960e1b2a6SGerd Hoffmann return TD_RESULT_COMPLETE; 800f1ae32a1SGerd Hoffmann } 801f1ae32a1SGerd Hoffmann 80266a08cbeSHans de Goede static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr, 803a4f30cd7SHans de Goede UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask) 804f1ae32a1SGerd Hoffmann { 8059a77a0f5SHans de Goede int ret, max_len; 8066ba43f1fSHans de Goede bool spd; 807a4f30cd7SHans de Goede bool queuing = (q != NULL); 80811d15e40SHans de Goede uint8_t pid = td->token & 0xff; 8098c75a899SHans de Goede UHCIAsync *async = uhci_async_find_td(s, td_addr); 8108c75a899SHans de Goede 8118c75a899SHans de Goede if (async) { 8128c75a899SHans de Goede if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) { 8138c75a899SHans de Goede assert(q == NULL || q == async->queue); 8148c75a899SHans de Goede q = async->queue; 8158c75a899SHans de Goede } else { 8168c75a899SHans de Goede uhci_queue_free(async->queue, "guest re-used pending td"); 8178c75a899SHans de Goede async = NULL; 8188c75a899SHans de Goede } 8198c75a899SHans de Goede } 820f1ae32a1SGerd Hoffmann 82166a08cbeSHans de Goede if (q == NULL) { 82266a08cbeSHans de Goede q = uhci_queue_find(s, td); 82366a08cbeSHans de Goede if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) { 82466a08cbeSHans de Goede uhci_queue_free(q, "guest re-used qh"); 82566a08cbeSHans de Goede q = NULL; 82666a08cbeSHans de Goede } 82766a08cbeSHans de Goede } 82866a08cbeSHans de Goede 8293905097eSHans de Goede if (q) { 830475443cfSHans de Goede q->valid = QH_VALID; 8313905097eSHans de Goede } 8323905097eSHans de Goede 833f1ae32a1SGerd Hoffmann /* Is active ? */ 834883bca77SHans de Goede if (!(td->ctrl & TD_CTRL_ACTIVE)) { 835420ca987SHans de Goede if (async) { 836420ca987SHans de Goede /* Guest marked a pending td non-active, cancel the queue */ 837420ca987SHans de Goede uhci_queue_free(async->queue, "pending td non-active"); 838420ca987SHans de Goede } 839883bca77SHans de Goede /* 840883bca77SHans de Goede * ehci11d spec page 22: "Even if the Active bit in the TD is already 841883bca77SHans de Goede * cleared when the TD is fetched ... an IOC interrupt is generated" 842883bca77SHans de Goede */ 843883bca77SHans de Goede if (td->ctrl & TD_CTRL_IOC) { 844883bca77SHans de Goede *int_mask |= 0x01; 845883bca77SHans de Goede } 84660e1b2a6SGerd Hoffmann return TD_RESULT_NEXT_QH; 847883bca77SHans de Goede } 848f1ae32a1SGerd Hoffmann 849f1ae32a1SGerd Hoffmann if (async) { 850ee008ba6SGerd Hoffmann if (queuing) { 851ee008ba6SGerd Hoffmann /* we are busy filling the queue, we are not prepared 852ee008ba6SGerd Hoffmann to consume completed packages then, just leave them 853ee008ba6SGerd Hoffmann in async state */ 854ee008ba6SGerd Hoffmann return TD_RESULT_ASYNC_CONT; 855ee008ba6SGerd Hoffmann } 8568928c9c4SHans de Goede if (!async->done) { 8578928c9c4SHans de Goede UHCI_TD last_td; 8588928c9c4SHans de Goede UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs, asyncs_head); 8598928c9c4SHans de Goede /* 8608928c9c4SHans de Goede * While we are waiting for the current td to complete, the guest 8618928c9c4SHans de Goede * may have added more tds to the queue. Note we re-read the td 8628928c9c4SHans de Goede * rather then caching it, as we want to see guest made changes! 8638928c9c4SHans de Goede */ 8648928c9c4SHans de Goede uhci_read_td(s, &last_td, last->td_addr); 8658928c9c4SHans de Goede uhci_queue_fill(async->queue, &last_td); 866f1ae32a1SGerd Hoffmann 8678928c9c4SHans de Goede return TD_RESULT_ASYNC_CONT; 8688928c9c4SHans de Goede } 869f1ae32a1SGerd Hoffmann uhci_async_unlink(async); 870f1ae32a1SGerd Hoffmann goto done; 871f1ae32a1SGerd Hoffmann } 872f1ae32a1SGerd Hoffmann 87388793816SHans de Goede if (s->completions_only) { 87488793816SHans de Goede return TD_RESULT_ASYNC_CONT; 87588793816SHans de Goede } 87688793816SHans de Goede 877f1ae32a1SGerd Hoffmann /* Allocate new packet */ 878a4f30cd7SHans de Goede if (q == NULL) { 87911d15e40SHans de Goede USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f); 88011d15e40SHans de Goede USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf); 8817f102ebeSHans de Goede 8827f102ebeSHans de Goede if (ep == NULL) { 8837f102ebeSHans de Goede return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV, 8847f102ebeSHans de Goede int_mask); 8857f102ebeSHans de Goede } 88666a08cbeSHans de Goede q = uhci_queue_new(s, qh_addr, td, ep); 887a4f30cd7SHans de Goede } 888a4f30cd7SHans de Goede async = uhci_async_alloc(q, td_addr); 889f1ae32a1SGerd Hoffmann 890f1ae32a1SGerd Hoffmann max_len = ((td->token >> 21) + 1) & 0x7ff; 8916ba43f1fSHans de Goede spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0); 8928550a02dSGerd Hoffmann usb_packet_setup(&async->packet, pid, q->ep, 0, td_addr, spd, 893a6fb2ddbSHans de Goede (td->ctrl & TD_CTRL_IOC) != 0); 8949822261cSHans de Goede if (max_len <= sizeof(async->static_buf)) { 8959822261cSHans de Goede async->buf = async->static_buf; 8969822261cSHans de Goede } else { 8979822261cSHans de Goede async->buf = g_malloc(max_len); 8989822261cSHans de Goede } 8999822261cSHans de Goede usb_packet_addbuf(&async->packet, async->buf, max_len); 900f1ae32a1SGerd Hoffmann 901f1ae32a1SGerd Hoffmann switch(pid) { 902f1ae32a1SGerd Hoffmann case USB_TOKEN_OUT: 903f1ae32a1SGerd Hoffmann case USB_TOKEN_SETUP: 9049822261cSHans de Goede pci_dma_read(&s->dev, td->buffer, async->buf, max_len); 9059a77a0f5SHans de Goede usb_handle_packet(q->ep->dev, &async->packet); 9069a77a0f5SHans de Goede if (async->packet.status == USB_RET_SUCCESS) { 9079a77a0f5SHans de Goede async->packet.actual_length = max_len; 9089a77a0f5SHans de Goede } 909f1ae32a1SGerd Hoffmann break; 910f1ae32a1SGerd Hoffmann 911f1ae32a1SGerd Hoffmann case USB_TOKEN_IN: 9129a77a0f5SHans de Goede usb_handle_packet(q->ep->dev, &async->packet); 913f1ae32a1SGerd Hoffmann break; 914f1ae32a1SGerd Hoffmann 915f1ae32a1SGerd Hoffmann default: 916f1ae32a1SGerd Hoffmann /* invalid pid : frame interrupted */ 917f1ae32a1SGerd Hoffmann uhci_async_free(async); 918f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCPERR; 919f1ae32a1SGerd Hoffmann uhci_update_irq(s); 92060e1b2a6SGerd Hoffmann return TD_RESULT_STOP_FRAME; 921f1ae32a1SGerd Hoffmann } 922f1ae32a1SGerd Hoffmann 9239a77a0f5SHans de Goede if (async->packet.status == USB_RET_ASYNC) { 924f1ae32a1SGerd Hoffmann uhci_async_link(async); 925a4f30cd7SHans de Goede if (!queuing) { 92611d15e40SHans de Goede uhci_queue_fill(q, td); 927a4f30cd7SHans de Goede } 9284efe4ef3SGerd Hoffmann return TD_RESULT_ASYNC_START; 929f1ae32a1SGerd Hoffmann } 930f1ae32a1SGerd Hoffmann 931f1ae32a1SGerd Hoffmann done: 9329a77a0f5SHans de Goede ret = uhci_complete_td(s, td, async, int_mask); 933f1ae32a1SGerd Hoffmann uhci_async_free(async); 9349a77a0f5SHans de Goede return ret; 935f1ae32a1SGerd Hoffmann } 936f1ae32a1SGerd Hoffmann 937f1ae32a1SGerd Hoffmann static void uhci_async_complete(USBPort *port, USBPacket *packet) 938f1ae32a1SGerd Hoffmann { 939f1ae32a1SGerd Hoffmann UHCIAsync *async = container_of(packet, UHCIAsync, packet); 940f1ae32a1SGerd Hoffmann UHCIState *s = async->queue->uhci; 941f1ae32a1SGerd Hoffmann 9429a77a0f5SHans de Goede if (packet->status == USB_RET_REMOVE_FROM_QUEUE) { 9430cae7b1aSHans de Goede uhci_async_cancel(async); 9440cae7b1aSHans de Goede return; 9450cae7b1aSHans de Goede } 9460cae7b1aSHans de Goede 947f1ae32a1SGerd Hoffmann async->done = 1; 94888793816SHans de Goede /* Force processing of this packet *now*, needed for migration */ 94988793816SHans de Goede s->completions_only = true; 9509a16c595SGerd Hoffmann qemu_bh_schedule(s->bh); 9519a16c595SGerd Hoffmann } 952f1ae32a1SGerd Hoffmann 953f1ae32a1SGerd Hoffmann static int is_valid(uint32_t link) 954f1ae32a1SGerd Hoffmann { 955f1ae32a1SGerd Hoffmann return (link & 1) == 0; 956f1ae32a1SGerd Hoffmann } 957f1ae32a1SGerd Hoffmann 958f1ae32a1SGerd Hoffmann static int is_qh(uint32_t link) 959f1ae32a1SGerd Hoffmann { 960f1ae32a1SGerd Hoffmann return (link & 2) != 0; 961f1ae32a1SGerd Hoffmann } 962f1ae32a1SGerd Hoffmann 963f1ae32a1SGerd Hoffmann static int depth_first(uint32_t link) 964f1ae32a1SGerd Hoffmann { 965f1ae32a1SGerd Hoffmann return (link & 4) != 0; 966f1ae32a1SGerd Hoffmann } 967f1ae32a1SGerd Hoffmann 968f1ae32a1SGerd Hoffmann /* QH DB used for detecting QH loops */ 969f1ae32a1SGerd Hoffmann #define UHCI_MAX_QUEUES 128 970f1ae32a1SGerd Hoffmann typedef struct { 971f1ae32a1SGerd Hoffmann uint32_t addr[UHCI_MAX_QUEUES]; 972f1ae32a1SGerd Hoffmann int count; 973f1ae32a1SGerd Hoffmann } QhDb; 974f1ae32a1SGerd Hoffmann 975f1ae32a1SGerd Hoffmann static void qhdb_reset(QhDb *db) 976f1ae32a1SGerd Hoffmann { 977f1ae32a1SGerd Hoffmann db->count = 0; 978f1ae32a1SGerd Hoffmann } 979f1ae32a1SGerd Hoffmann 980f1ae32a1SGerd Hoffmann /* Add QH to DB. Returns 1 if already present or DB is full. */ 981f1ae32a1SGerd Hoffmann static int qhdb_insert(QhDb *db, uint32_t addr) 982f1ae32a1SGerd Hoffmann { 983f1ae32a1SGerd Hoffmann int i; 984f1ae32a1SGerd Hoffmann for (i = 0; i < db->count; i++) 985f1ae32a1SGerd Hoffmann if (db->addr[i] == addr) 986f1ae32a1SGerd Hoffmann return 1; 987f1ae32a1SGerd Hoffmann 988f1ae32a1SGerd Hoffmann if (db->count >= UHCI_MAX_QUEUES) 989f1ae32a1SGerd Hoffmann return 1; 990f1ae32a1SGerd Hoffmann 991f1ae32a1SGerd Hoffmann db->addr[db->count++] = addr; 992f1ae32a1SGerd Hoffmann return 0; 993f1ae32a1SGerd Hoffmann } 994f1ae32a1SGerd Hoffmann 99511d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td) 996f1ae32a1SGerd Hoffmann { 997f1ae32a1SGerd Hoffmann uint32_t int_mask = 0; 998f1ae32a1SGerd Hoffmann uint32_t plink = td->link; 999f1ae32a1SGerd Hoffmann UHCI_TD ptd; 1000f1ae32a1SGerd Hoffmann int ret; 1001f1ae32a1SGerd Hoffmann 10026ba43f1fSHans de Goede while (is_valid(plink)) { 1003a4f30cd7SHans de Goede uhci_read_td(q->uhci, &ptd, plink); 1004f1ae32a1SGerd Hoffmann if (!(ptd.ctrl & TD_CTRL_ACTIVE)) { 1005f1ae32a1SGerd Hoffmann break; 1006f1ae32a1SGerd Hoffmann } 1007a4f30cd7SHans de Goede if (uhci_queue_token(&ptd) != q->token) { 1008f1ae32a1SGerd Hoffmann break; 1009f1ae32a1SGerd Hoffmann } 101050dcc0f8SGerd Hoffmann trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token); 101166a08cbeSHans de Goede ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask); 101252b0fecdSGerd Hoffmann if (ret == TD_RESULT_ASYNC_CONT) { 101352b0fecdSGerd Hoffmann break; 101452b0fecdSGerd Hoffmann } 10154efe4ef3SGerd Hoffmann assert(ret == TD_RESULT_ASYNC_START); 1016f1ae32a1SGerd Hoffmann assert(int_mask == 0); 1017f1ae32a1SGerd Hoffmann plink = ptd.link; 1018f1ae32a1SGerd Hoffmann } 101911d15e40SHans de Goede usb_device_flush_ep_queue(q->ep->dev, q->ep); 1020f1ae32a1SGerd Hoffmann } 1021f1ae32a1SGerd Hoffmann 1022f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s) 1023f1ae32a1SGerd Hoffmann { 1024f1ae32a1SGerd Hoffmann uint32_t frame_addr, link, old_td_ctrl, val, int_mask; 10254aed20e2SGerd Hoffmann uint32_t curr_qh, td_count = 0; 1026f1ae32a1SGerd Hoffmann int cnt, ret; 1027f1ae32a1SGerd Hoffmann UHCI_TD td; 1028f1ae32a1SGerd Hoffmann UHCI_QH qh; 1029f1ae32a1SGerd Hoffmann QhDb qhdb; 1030f1ae32a1SGerd Hoffmann 1031f1ae32a1SGerd Hoffmann frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2); 1032f1ae32a1SGerd Hoffmann 1033f1ae32a1SGerd Hoffmann pci_dma_read(&s->dev, frame_addr, &link, 4); 1034f1ae32a1SGerd Hoffmann le32_to_cpus(&link); 1035f1ae32a1SGerd Hoffmann 1036f1ae32a1SGerd Hoffmann int_mask = 0; 1037f1ae32a1SGerd Hoffmann curr_qh = 0; 1038f1ae32a1SGerd Hoffmann 1039f1ae32a1SGerd Hoffmann qhdb_reset(&qhdb); 1040f1ae32a1SGerd Hoffmann 1041f1ae32a1SGerd Hoffmann for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) { 104288793816SHans de Goede if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) { 10434aed20e2SGerd Hoffmann /* We've reached the usb 1.1 bandwidth, which is 10444aed20e2SGerd Hoffmann 1280 bytes/frame, stop processing */ 10454aed20e2SGerd Hoffmann trace_usb_uhci_frame_stop_bandwidth(); 10464aed20e2SGerd Hoffmann break; 10474aed20e2SGerd Hoffmann } 1048f1ae32a1SGerd Hoffmann if (is_qh(link)) { 1049f1ae32a1SGerd Hoffmann /* QH */ 105050dcc0f8SGerd Hoffmann trace_usb_uhci_qh_load(link & ~0xf); 1051f1ae32a1SGerd Hoffmann 1052f1ae32a1SGerd Hoffmann if (qhdb_insert(&qhdb, link)) { 1053f1ae32a1SGerd Hoffmann /* 1054f1ae32a1SGerd Hoffmann * We're going in circles. Which is not a bug because 1055f1ae32a1SGerd Hoffmann * HCD is allowed to do that as part of the BW management. 1056f1ae32a1SGerd Hoffmann * 10574aed20e2SGerd Hoffmann * Stop processing here if no transaction has been done 10584aed20e2SGerd Hoffmann * since we've been here last time. 1059f1ae32a1SGerd Hoffmann */ 1060f1ae32a1SGerd Hoffmann if (td_count == 0) { 106150dcc0f8SGerd Hoffmann trace_usb_uhci_frame_loop_stop_idle(); 1062f1ae32a1SGerd Hoffmann break; 1063f1ae32a1SGerd Hoffmann } else { 106450dcc0f8SGerd Hoffmann trace_usb_uhci_frame_loop_continue(); 1065f1ae32a1SGerd Hoffmann td_count = 0; 1066f1ae32a1SGerd Hoffmann qhdb_reset(&qhdb); 1067f1ae32a1SGerd Hoffmann qhdb_insert(&qhdb, link); 1068f1ae32a1SGerd Hoffmann } 1069f1ae32a1SGerd Hoffmann } 1070f1ae32a1SGerd Hoffmann 1071f1ae32a1SGerd Hoffmann pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh)); 1072f1ae32a1SGerd Hoffmann le32_to_cpus(&qh.link); 1073f1ae32a1SGerd Hoffmann le32_to_cpus(&qh.el_link); 1074f1ae32a1SGerd Hoffmann 1075f1ae32a1SGerd Hoffmann if (!is_valid(qh.el_link)) { 1076f1ae32a1SGerd Hoffmann /* QH w/o elements */ 1077f1ae32a1SGerd Hoffmann curr_qh = 0; 1078f1ae32a1SGerd Hoffmann link = qh.link; 1079f1ae32a1SGerd Hoffmann } else { 1080f1ae32a1SGerd Hoffmann /* QH with elements */ 1081f1ae32a1SGerd Hoffmann curr_qh = link; 1082f1ae32a1SGerd Hoffmann link = qh.el_link; 1083f1ae32a1SGerd Hoffmann } 1084f1ae32a1SGerd Hoffmann continue; 1085f1ae32a1SGerd Hoffmann } 1086f1ae32a1SGerd Hoffmann 1087f1ae32a1SGerd Hoffmann /* TD */ 1088963a68b5SHans de Goede uhci_read_td(s, &td, link); 108950dcc0f8SGerd Hoffmann trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token); 1090f1ae32a1SGerd Hoffmann 1091f1ae32a1SGerd Hoffmann old_td_ctrl = td.ctrl; 109266a08cbeSHans de Goede ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask); 1093f1ae32a1SGerd Hoffmann if (old_td_ctrl != td.ctrl) { 1094f1ae32a1SGerd Hoffmann /* update the status bits of the TD */ 1095f1ae32a1SGerd Hoffmann val = cpu_to_le32(td.ctrl); 1096f1ae32a1SGerd Hoffmann pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val)); 1097f1ae32a1SGerd Hoffmann } 1098f1ae32a1SGerd Hoffmann 1099f1ae32a1SGerd Hoffmann switch (ret) { 110060e1b2a6SGerd Hoffmann case TD_RESULT_STOP_FRAME: /* interrupted frame */ 1101f1ae32a1SGerd Hoffmann goto out; 1102f1ae32a1SGerd Hoffmann 110360e1b2a6SGerd Hoffmann case TD_RESULT_NEXT_QH: 11044efe4ef3SGerd Hoffmann case TD_RESULT_ASYNC_CONT: 110550dcc0f8SGerd Hoffmann trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf); 1106f1ae32a1SGerd Hoffmann link = curr_qh ? qh.link : td.link; 1107f1ae32a1SGerd Hoffmann continue; 1108f1ae32a1SGerd Hoffmann 11094efe4ef3SGerd Hoffmann case TD_RESULT_ASYNC_START: 111050dcc0f8SGerd Hoffmann trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf); 1111f1ae32a1SGerd Hoffmann link = curr_qh ? qh.link : td.link; 1112f1ae32a1SGerd Hoffmann continue; 1113f1ae32a1SGerd Hoffmann 111460e1b2a6SGerd Hoffmann case TD_RESULT_COMPLETE: 111550dcc0f8SGerd Hoffmann trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf); 1116f1ae32a1SGerd Hoffmann link = td.link; 1117f1ae32a1SGerd Hoffmann td_count++; 11184aed20e2SGerd Hoffmann s->frame_bytes += (td.ctrl & 0x7ff) + 1; 1119f1ae32a1SGerd Hoffmann 1120f1ae32a1SGerd Hoffmann if (curr_qh) { 1121f1ae32a1SGerd Hoffmann /* update QH element link */ 1122f1ae32a1SGerd Hoffmann qh.el_link = link; 1123f1ae32a1SGerd Hoffmann val = cpu_to_le32(qh.el_link); 1124f1ae32a1SGerd Hoffmann pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val)); 1125f1ae32a1SGerd Hoffmann 1126f1ae32a1SGerd Hoffmann if (!depth_first(link)) { 1127f1ae32a1SGerd Hoffmann /* done with this QH */ 1128f1ae32a1SGerd Hoffmann curr_qh = 0; 1129f1ae32a1SGerd Hoffmann link = qh.link; 1130f1ae32a1SGerd Hoffmann } 1131f1ae32a1SGerd Hoffmann } 1132f1ae32a1SGerd Hoffmann break; 1133f1ae32a1SGerd Hoffmann 1134f1ae32a1SGerd Hoffmann default: 1135f1ae32a1SGerd Hoffmann assert(!"unknown return code"); 1136f1ae32a1SGerd Hoffmann } 1137f1ae32a1SGerd Hoffmann 1138f1ae32a1SGerd Hoffmann /* go to the next entry */ 1139f1ae32a1SGerd Hoffmann } 1140f1ae32a1SGerd Hoffmann 1141f1ae32a1SGerd Hoffmann out: 1142f1ae32a1SGerd Hoffmann s->pending_int_mask |= int_mask; 1143f1ae32a1SGerd Hoffmann } 1144f1ae32a1SGerd Hoffmann 11459a16c595SGerd Hoffmann static void uhci_bh(void *opaque) 11469a16c595SGerd Hoffmann { 11479a16c595SGerd Hoffmann UHCIState *s = opaque; 11489a16c595SGerd Hoffmann uhci_process_frame(s); 11499a16c595SGerd Hoffmann } 11509a16c595SGerd Hoffmann 1151f1ae32a1SGerd Hoffmann static void uhci_frame_timer(void *opaque) 1152f1ae32a1SGerd Hoffmann { 1153f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 1154f8f48b69SHans de Goede uint64_t t_now, t_last_run; 1155f8f48b69SHans de Goede int i, frames; 1156f8f48b69SHans de Goede const uint64_t frame_t = get_ticks_per_sec() / FRAME_TIMER_FREQ; 1157f1ae32a1SGerd Hoffmann 115888793816SHans de Goede s->completions_only = false; 11599a16c595SGerd Hoffmann qemu_bh_cancel(s->bh); 1160f1ae32a1SGerd Hoffmann 1161f1ae32a1SGerd Hoffmann if (!(s->cmd & UHCI_CMD_RS)) { 1162f1ae32a1SGerd Hoffmann /* Full stop */ 116350dcc0f8SGerd Hoffmann trace_usb_uhci_schedule_stop(); 1164f1ae32a1SGerd Hoffmann qemu_del_timer(s->frame_timer); 1165d9a528dbSGerd Hoffmann uhci_async_cancel_all(s); 1166f1ae32a1SGerd Hoffmann /* set hchalted bit in status - UHCI11D 2.1.2 */ 1167f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCHALTED; 1168f1ae32a1SGerd Hoffmann return; 1169f1ae32a1SGerd Hoffmann } 1170f1ae32a1SGerd Hoffmann 1171f8f48b69SHans de Goede /* We still store expire_time in our state, for migration */ 1172f8f48b69SHans de Goede t_last_run = s->expire_time - frame_t; 1173f8f48b69SHans de Goede t_now = qemu_get_clock_ns(vm_clock); 1174f8f48b69SHans de Goede 1175f8f48b69SHans de Goede /* Process up to MAX_FRAMES_PER_TICK frames */ 1176f8f48b69SHans de Goede frames = (t_now - t_last_run) / frame_t; 11779fdf7027SHans de Goede if (frames > s->maxframes) { 11789fdf7027SHans de Goede int skipped = frames - s->maxframes; 11799fdf7027SHans de Goede s->expire_time += skipped * frame_t; 11809fdf7027SHans de Goede s->frnum = (s->frnum + skipped) & 0x7ff; 11819fdf7027SHans de Goede frames -= skipped; 11829fdf7027SHans de Goede } 1183f8f48b69SHans de Goede if (frames > MAX_FRAMES_PER_TICK) { 1184f8f48b69SHans de Goede frames = MAX_FRAMES_PER_TICK; 1185f8f48b69SHans de Goede } 1186f8f48b69SHans de Goede 1187f8f48b69SHans de Goede for (i = 0; i < frames; i++) { 1188f8f48b69SHans de Goede s->frame_bytes = 0; 118950dcc0f8SGerd Hoffmann trace_usb_uhci_frame_start(s->frnum); 1190f1ae32a1SGerd Hoffmann uhci_async_validate_begin(s); 1191f1ae32a1SGerd Hoffmann uhci_process_frame(s); 1192f1ae32a1SGerd Hoffmann uhci_async_validate_end(s); 1193f8f48b69SHans de Goede /* The spec says frnum is the frame currently being processed, and 1194f8f48b69SHans de Goede * the guest must look at frnum - 1 on interrupt, so inc frnum now */ 1195719c130dSHans de Goede s->frnum = (s->frnum + 1) & 0x7ff; 1196f8f48b69SHans de Goede s->expire_time += frame_t; 1197f8f48b69SHans de Goede } 1198719c130dSHans de Goede 1199f8f48b69SHans de Goede /* Complete the previous frame(s) */ 1200719c130dSHans de Goede if (s->pending_int_mask) { 1201719c130dSHans de Goede s->status2 |= s->pending_int_mask; 1202719c130dSHans de Goede s->status |= UHCI_STS_USBINT; 1203719c130dSHans de Goede uhci_update_irq(s); 1204719c130dSHans de Goede } 1205719c130dSHans de Goede s->pending_int_mask = 0; 1206719c130dSHans de Goede 1207f8f48b69SHans de Goede qemu_mod_timer(s->frame_timer, t_now + frame_t); 1208f1ae32a1SGerd Hoffmann } 1209f1ae32a1SGerd Hoffmann 1210f1ae32a1SGerd Hoffmann static const MemoryRegionOps uhci_ioport_ops = { 121189eb147cSGerd Hoffmann .read = uhci_port_read, 121289eb147cSGerd Hoffmann .write = uhci_port_write, 121389eb147cSGerd Hoffmann .valid.min_access_size = 1, 121489eb147cSGerd Hoffmann .valid.max_access_size = 4, 121589eb147cSGerd Hoffmann .impl.min_access_size = 2, 121689eb147cSGerd Hoffmann .impl.max_access_size = 2, 121789eb147cSGerd Hoffmann .endianness = DEVICE_LITTLE_ENDIAN, 1218f1ae32a1SGerd Hoffmann }; 1219f1ae32a1SGerd Hoffmann 1220f1ae32a1SGerd Hoffmann static USBPortOps uhci_port_ops = { 1221f1ae32a1SGerd Hoffmann .attach = uhci_attach, 1222f1ae32a1SGerd Hoffmann .detach = uhci_detach, 1223f1ae32a1SGerd Hoffmann .child_detach = uhci_child_detach, 1224f1ae32a1SGerd Hoffmann .wakeup = uhci_wakeup, 1225f1ae32a1SGerd Hoffmann .complete = uhci_async_complete, 1226f1ae32a1SGerd Hoffmann }; 1227f1ae32a1SGerd Hoffmann 1228f1ae32a1SGerd Hoffmann static USBBusOps uhci_bus_ops = { 1229f1ae32a1SGerd Hoffmann }; 1230f1ae32a1SGerd Hoffmann 1231f1ae32a1SGerd Hoffmann static int usb_uhci_common_initfn(PCIDevice *dev) 1232f1ae32a1SGerd Hoffmann { 1233973002c1SGerd Hoffmann PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 12348f3f90b0SGerd Hoffmann UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class); 1235f1ae32a1SGerd Hoffmann UHCIState *s = DO_UPCAST(UHCIState, dev, dev); 1236f1ae32a1SGerd Hoffmann uint8_t *pci_conf = s->dev.config; 1237f1ae32a1SGerd Hoffmann int i; 1238f1ae32a1SGerd Hoffmann 1239f1ae32a1SGerd Hoffmann pci_conf[PCI_CLASS_PROG] = 0x00; 1240f1ae32a1SGerd Hoffmann /* TODO: reset value should be 0. */ 1241f1ae32a1SGerd Hoffmann pci_conf[USB_SBRN] = USB_RELEASE_1; // release number 1242f1ae32a1SGerd Hoffmann 12438f3f90b0SGerd Hoffmann s->irq_pin = u->info.irq_pin; 1244973002c1SGerd Hoffmann pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1); 1245973002c1SGerd Hoffmann 1246f1ae32a1SGerd Hoffmann if (s->masterbus) { 1247f1ae32a1SGerd Hoffmann USBPort *ports[NB_PORTS]; 1248f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 1249f1ae32a1SGerd Hoffmann ports[i] = &s->ports[i].port; 1250f1ae32a1SGerd Hoffmann } 1251f1ae32a1SGerd Hoffmann if (usb_register_companion(s->masterbus, ports, NB_PORTS, 1252f1ae32a1SGerd Hoffmann s->firstport, s, &uhci_port_ops, 1253f1ae32a1SGerd Hoffmann USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) { 1254f1ae32a1SGerd Hoffmann return -1; 1255f1ae32a1SGerd Hoffmann } 1256f1ae32a1SGerd Hoffmann } else { 1257f1ae32a1SGerd Hoffmann usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev); 1258f1ae32a1SGerd Hoffmann for (i = 0; i < NB_PORTS; i++) { 1259f1ae32a1SGerd Hoffmann usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops, 1260f1ae32a1SGerd Hoffmann USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); 1261f1ae32a1SGerd Hoffmann } 1262f1ae32a1SGerd Hoffmann } 12639a16c595SGerd Hoffmann s->bh = qemu_bh_new(uhci_bh, s); 1264f1ae32a1SGerd Hoffmann s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s); 1265f1ae32a1SGerd Hoffmann s->num_ports_vmstate = NB_PORTS; 1266f1ae32a1SGerd Hoffmann QTAILQ_INIT(&s->queues); 1267f1ae32a1SGerd Hoffmann 1268f1ae32a1SGerd Hoffmann qemu_register_reset(uhci_reset, s); 1269f1ae32a1SGerd Hoffmann 127022fc860bSPaolo Bonzini memory_region_init_io(&s->io_bar, OBJECT(s), &uhci_ioport_ops, s, 127122fc860bSPaolo Bonzini "uhci", 0x20); 127222fc860bSPaolo Bonzini 1273f1ae32a1SGerd Hoffmann /* Use region 4 for consistency with real hardware. BSD guests seem 1274f1ae32a1SGerd Hoffmann to rely on this. */ 1275f1ae32a1SGerd Hoffmann pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar); 1276f1ae32a1SGerd Hoffmann 1277f1ae32a1SGerd Hoffmann return 0; 1278f1ae32a1SGerd Hoffmann } 1279f1ae32a1SGerd Hoffmann 1280f1ae32a1SGerd Hoffmann static int usb_uhci_vt82c686b_initfn(PCIDevice *dev) 1281f1ae32a1SGerd Hoffmann { 1282f1ae32a1SGerd Hoffmann UHCIState *s = DO_UPCAST(UHCIState, dev, dev); 1283f1ae32a1SGerd Hoffmann uint8_t *pci_conf = s->dev.config; 1284f1ae32a1SGerd Hoffmann 1285f1ae32a1SGerd Hoffmann /* USB misc control 1/2 */ 1286f1ae32a1SGerd Hoffmann pci_set_long(pci_conf + 0x40,0x00001000); 1287f1ae32a1SGerd Hoffmann /* PM capability */ 1288f1ae32a1SGerd Hoffmann pci_set_long(pci_conf + 0x80,0x00020001); 1289f1ae32a1SGerd Hoffmann /* USB legacy support */ 1290f1ae32a1SGerd Hoffmann pci_set_long(pci_conf + 0xc0,0x00002000); 1291f1ae32a1SGerd Hoffmann 1292f1ae32a1SGerd Hoffmann return usb_uhci_common_initfn(dev); 1293f1ae32a1SGerd Hoffmann } 1294f1ae32a1SGerd Hoffmann 1295f90c2bcdSAlex Williamson static void usb_uhci_exit(PCIDevice *dev) 1296f1ae32a1SGerd Hoffmann { 1297f1ae32a1SGerd Hoffmann UHCIState *s = DO_UPCAST(UHCIState, dev, dev); 1298f1ae32a1SGerd Hoffmann 1299f1ae32a1SGerd Hoffmann memory_region_destroy(&s->io_bar); 1300f1ae32a1SGerd Hoffmann } 1301f1ae32a1SGerd Hoffmann 1302f1ae32a1SGerd Hoffmann static Property uhci_properties[] = { 1303f1ae32a1SGerd Hoffmann DEFINE_PROP_STRING("masterbus", UHCIState, masterbus), 1304f1ae32a1SGerd Hoffmann DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0), 130540141d12SGerd Hoffmann DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280), 13069fdf7027SHans de Goede DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128), 1307f1ae32a1SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 1308f1ae32a1SGerd Hoffmann }; 1309f1ae32a1SGerd Hoffmann 13102c2e8525SGerd Hoffmann static void uhci_class_init(ObjectClass *klass, void *data) 1311f1ae32a1SGerd Hoffmann { 1312f1ae32a1SGerd Hoffmann DeviceClass *dc = DEVICE_CLASS(klass); 1313f1ae32a1SGerd Hoffmann PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 13148f3f90b0SGerd Hoffmann UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class); 13152c2e8525SGerd Hoffmann UHCIInfo *info = data; 1316f1ae32a1SGerd Hoffmann 13172c2e8525SGerd Hoffmann k->init = info->initfn ? info->initfn : usb_uhci_common_initfn; 13182c2e8525SGerd Hoffmann k->exit = info->unplug ? usb_uhci_exit : NULL; 13192c2e8525SGerd Hoffmann k->vendor_id = info->vendor_id; 13202c2e8525SGerd Hoffmann k->device_id = info->device_id; 13212c2e8525SGerd Hoffmann k->revision = info->revision; 1322f1ae32a1SGerd Hoffmann k->class_id = PCI_CLASS_SERIAL_USB; 13236c2d1c32SGerd Hoffmann k->no_hotplug = 1; 1324f1ae32a1SGerd Hoffmann dc->vmsd = &vmstate_uhci; 1325f1ae32a1SGerd Hoffmann dc->props = uhci_properties; 1326125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_USB, dc->categories); 13278f3f90b0SGerd Hoffmann u->info = *info; 1328f1ae32a1SGerd Hoffmann } 1329f1ae32a1SGerd Hoffmann 13302c2e8525SGerd Hoffmann static UHCIInfo uhci_info[] = { 13312c2e8525SGerd Hoffmann { 1332f1ae32a1SGerd Hoffmann .name = "piix3-usb-uhci", 13332c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13342c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82371SB_2, 13352c2e8525SGerd Hoffmann .revision = 0x01, 13368f3f90b0SGerd Hoffmann .irq_pin = 3, 13372c2e8525SGerd Hoffmann .unplug = true, 13382c2e8525SGerd Hoffmann },{ 1339f1ae32a1SGerd Hoffmann .name = "piix4-usb-uhci", 13402c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13412c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82371AB_2, 13422c2e8525SGerd Hoffmann .revision = 0x01, 13438f3f90b0SGerd Hoffmann .irq_pin = 3, 13442c2e8525SGerd Hoffmann .unplug = true, 13452c2e8525SGerd Hoffmann },{ 1346f1ae32a1SGerd Hoffmann .name = "vt82c686b-usb-uhci", 13472c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_VIA, 13482c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_VIA_UHCI, 13492c2e8525SGerd Hoffmann .revision = 0x01, 13508f3f90b0SGerd Hoffmann .irq_pin = 3, 13512c2e8525SGerd Hoffmann .initfn = usb_uhci_vt82c686b_initfn, 13522c2e8525SGerd Hoffmann .unplug = true, 13532c2e8525SGerd Hoffmann },{ 135474625ea2SGerd Hoffmann .name = "ich9-usb-uhci1", /* 00:1d.0 */ 13552c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13562c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1, 13572c2e8525SGerd Hoffmann .revision = 0x03, 13588f3f90b0SGerd Hoffmann .irq_pin = 0, 13592c2e8525SGerd Hoffmann .unplug = false, 13602c2e8525SGerd Hoffmann },{ 136174625ea2SGerd Hoffmann .name = "ich9-usb-uhci2", /* 00:1d.1 */ 13622c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13632c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2, 13642c2e8525SGerd Hoffmann .revision = 0x03, 13658f3f90b0SGerd Hoffmann .irq_pin = 1, 13662c2e8525SGerd Hoffmann .unplug = false, 13672c2e8525SGerd Hoffmann },{ 136874625ea2SGerd Hoffmann .name = "ich9-usb-uhci3", /* 00:1d.2 */ 13692c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13702c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3, 13712c2e8525SGerd Hoffmann .revision = 0x03, 13728f3f90b0SGerd Hoffmann .irq_pin = 2, 13732c2e8525SGerd Hoffmann .unplug = false, 137474625ea2SGerd Hoffmann },{ 137574625ea2SGerd Hoffmann .name = "ich9-usb-uhci4", /* 00:1a.0 */ 137674625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 137774625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4, 137874625ea2SGerd Hoffmann .revision = 0x03, 137974625ea2SGerd Hoffmann .irq_pin = 0, 138074625ea2SGerd Hoffmann .unplug = false, 138174625ea2SGerd Hoffmann },{ 138274625ea2SGerd Hoffmann .name = "ich9-usb-uhci5", /* 00:1a.1 */ 138374625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 138474625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5, 138574625ea2SGerd Hoffmann .revision = 0x03, 138674625ea2SGerd Hoffmann .irq_pin = 1, 138774625ea2SGerd Hoffmann .unplug = false, 138874625ea2SGerd Hoffmann },{ 138974625ea2SGerd Hoffmann .name = "ich9-usb-uhci6", /* 00:1a.2 */ 139074625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 139174625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6, 139274625ea2SGerd Hoffmann .revision = 0x03, 139374625ea2SGerd Hoffmann .irq_pin = 2, 139474625ea2SGerd Hoffmann .unplug = false, 13952c2e8525SGerd Hoffmann } 1396f1ae32a1SGerd Hoffmann }; 1397f1ae32a1SGerd Hoffmann 1398f1ae32a1SGerd Hoffmann static void uhci_register_types(void) 1399f1ae32a1SGerd Hoffmann { 14002c2e8525SGerd Hoffmann TypeInfo uhci_type_info = { 14012c2e8525SGerd Hoffmann .parent = TYPE_PCI_DEVICE, 14022c2e8525SGerd Hoffmann .instance_size = sizeof(UHCIState), 14038f3f90b0SGerd Hoffmann .class_size = sizeof(UHCIPCIDeviceClass), 14042c2e8525SGerd Hoffmann .class_init = uhci_class_init, 14052c2e8525SGerd Hoffmann }; 14062c2e8525SGerd Hoffmann int i; 14072c2e8525SGerd Hoffmann 14082c2e8525SGerd Hoffmann for (i = 0; i < ARRAY_SIZE(uhci_info); i++) { 14092c2e8525SGerd Hoffmann uhci_type_info.name = uhci_info[i].name; 14102c2e8525SGerd Hoffmann uhci_type_info.class_data = uhci_info + i; 14112c2e8525SGerd Hoffmann type_register(&uhci_type_info); 14122c2e8525SGerd Hoffmann } 1413f1ae32a1SGerd Hoffmann } 1414f1ae32a1SGerd Hoffmann 1415f1ae32a1SGerd Hoffmann type_init(uhci_register_types) 1416