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 */ 28*e532b2e0SPeter Maydell #include "qemu/osdep.h" 29f1ae32a1SGerd Hoffmann #include "hw/hw.h" 30f1ae32a1SGerd Hoffmann #include "hw/usb.h" 319a1d111eSGerd Hoffmann #include "hw/usb/uhci-regs.h" 32a2cb15b0SMichael S. Tsirkin #include "hw/pci/pci.h" 331de7afc9SPaolo Bonzini #include "qemu/timer.h" 341de7afc9SPaolo Bonzini #include "qemu/iov.h" 359c17d615SPaolo Bonzini #include "sysemu/dma.h" 3650dcc0f8SGerd Hoffmann #include "trace.h" 376a1751b7SAlex Bligh #include "qemu/main-loop.h" 38f1ae32a1SGerd Hoffmann 39f1ae32a1SGerd Hoffmann #define FRAME_TIMER_FREQ 1000 40f1ae32a1SGerd Hoffmann 41f1ae32a1SGerd Hoffmann #define FRAME_MAX_LOOPS 256 42f1ae32a1SGerd Hoffmann 43475443cfSHans de Goede /* Must be large enough to handle 10 frame delay for initial isoc requests */ 44475443cfSHans de Goede #define QH_VALID 32 45475443cfSHans de Goede 46f8f48b69SHans de Goede #define MAX_FRAMES_PER_TICK (QH_VALID / 2) 47f8f48b69SHans de Goede 48f1ae32a1SGerd Hoffmann #define NB_PORTS 2 49f1ae32a1SGerd Hoffmann 5060e1b2a6SGerd Hoffmann enum { 510cd178caSGerd Hoffmann TD_RESULT_STOP_FRAME = 10, 520cd178caSGerd Hoffmann TD_RESULT_COMPLETE, 530cd178caSGerd Hoffmann TD_RESULT_NEXT_QH, 544efe4ef3SGerd Hoffmann TD_RESULT_ASYNC_START, 554efe4ef3SGerd Hoffmann TD_RESULT_ASYNC_CONT, 5660e1b2a6SGerd Hoffmann }; 5760e1b2a6SGerd Hoffmann 58f1ae32a1SGerd Hoffmann typedef struct UHCIState UHCIState; 59f1ae32a1SGerd Hoffmann typedef struct UHCIAsync UHCIAsync; 60f1ae32a1SGerd Hoffmann typedef struct UHCIQueue UHCIQueue; 612c2e8525SGerd Hoffmann typedef struct UHCIInfo UHCIInfo; 628f3f90b0SGerd Hoffmann typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass; 632c2e8525SGerd Hoffmann 642c2e8525SGerd Hoffmann struct UHCIInfo { 652c2e8525SGerd Hoffmann const char *name; 662c2e8525SGerd Hoffmann uint16_t vendor_id; 672c2e8525SGerd Hoffmann uint16_t device_id; 682c2e8525SGerd Hoffmann uint8_t revision; 698f3f90b0SGerd Hoffmann uint8_t irq_pin; 7063216dc7SMarkus Armbruster void (*realize)(PCIDevice *dev, Error **errp); 712c2e8525SGerd Hoffmann bool unplug; 722c2e8525SGerd Hoffmann }; 73f1ae32a1SGerd Hoffmann 748f3f90b0SGerd Hoffmann struct UHCIPCIDeviceClass { 758f3f90b0SGerd Hoffmann PCIDeviceClass parent_class; 768f3f90b0SGerd Hoffmann UHCIInfo info; 778f3f90b0SGerd Hoffmann }; 788f3f90b0SGerd Hoffmann 79f1ae32a1SGerd Hoffmann /* 80f1ae32a1SGerd Hoffmann * Pending async transaction. 81f1ae32a1SGerd Hoffmann * 'packet' must be the first field because completion 82f1ae32a1SGerd Hoffmann * handler does "(UHCIAsync *) pkt" cast. 83f1ae32a1SGerd Hoffmann */ 84f1ae32a1SGerd Hoffmann 85f1ae32a1SGerd Hoffmann struct UHCIAsync { 86f1ae32a1SGerd Hoffmann USBPacket packet; 879822261cSHans de Goede uint8_t static_buf[64]; /* 64 bytes is enough, except for isoc packets */ 889822261cSHans de Goede uint8_t *buf; 89f1ae32a1SGerd Hoffmann UHCIQueue *queue; 90f1ae32a1SGerd Hoffmann QTAILQ_ENTRY(UHCIAsync) next; 911f250cc7SHans de Goede uint32_t td_addr; 92f1ae32a1SGerd Hoffmann uint8_t done; 93f1ae32a1SGerd Hoffmann }; 94f1ae32a1SGerd Hoffmann 95f1ae32a1SGerd Hoffmann struct UHCIQueue { 9666a08cbeSHans de Goede uint32_t qh_addr; 97f1ae32a1SGerd Hoffmann uint32_t token; 98f1ae32a1SGerd Hoffmann UHCIState *uhci; 9911d15e40SHans de Goede USBEndpoint *ep; 100f1ae32a1SGerd Hoffmann QTAILQ_ENTRY(UHCIQueue) next; 1018928c9c4SHans de Goede QTAILQ_HEAD(asyncs_head, UHCIAsync) asyncs; 102f1ae32a1SGerd Hoffmann int8_t valid; 103f1ae32a1SGerd Hoffmann }; 104f1ae32a1SGerd Hoffmann 105f1ae32a1SGerd Hoffmann typedef struct UHCIPort { 106f1ae32a1SGerd Hoffmann USBPort port; 107f1ae32a1SGerd Hoffmann uint16_t ctrl; 108f1ae32a1SGerd Hoffmann } UHCIPort; 109f1ae32a1SGerd Hoffmann 110f1ae32a1SGerd Hoffmann struct UHCIState { 111f1ae32a1SGerd Hoffmann PCIDevice dev; 112f1ae32a1SGerd Hoffmann MemoryRegion io_bar; 113f1ae32a1SGerd Hoffmann USBBus bus; /* Note unused when we're a companion controller */ 114f1ae32a1SGerd Hoffmann uint16_t cmd; /* cmd register */ 115f1ae32a1SGerd Hoffmann uint16_t status; 116f1ae32a1SGerd Hoffmann uint16_t intr; /* interrupt enable register */ 117f1ae32a1SGerd Hoffmann uint16_t frnum; /* frame number */ 118f1ae32a1SGerd Hoffmann uint32_t fl_base_addr; /* frame list base address */ 119f1ae32a1SGerd Hoffmann uint8_t sof_timing; 120f1ae32a1SGerd Hoffmann uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */ 121f1ae32a1SGerd Hoffmann int64_t expire_time; 122f1ae32a1SGerd Hoffmann QEMUTimer *frame_timer; 1239a16c595SGerd Hoffmann QEMUBH *bh; 1244aed20e2SGerd Hoffmann uint32_t frame_bytes; 12540141d12SGerd Hoffmann uint32_t frame_bandwidth; 12688793816SHans de Goede bool completions_only; 127f1ae32a1SGerd Hoffmann UHCIPort ports[NB_PORTS]; 128f1ae32a1SGerd Hoffmann 129f1ae32a1SGerd Hoffmann /* Interrupts that should be raised at the end of the current frame. */ 130f1ae32a1SGerd Hoffmann uint32_t pending_int_mask; 131f1ae32a1SGerd Hoffmann 132f1ae32a1SGerd Hoffmann /* Active packets */ 133f1ae32a1SGerd Hoffmann QTAILQ_HEAD(, UHCIQueue) queues; 134f1ae32a1SGerd Hoffmann uint8_t num_ports_vmstate; 135f1ae32a1SGerd Hoffmann 136f1ae32a1SGerd Hoffmann /* Properties */ 137f1ae32a1SGerd Hoffmann char *masterbus; 138f1ae32a1SGerd Hoffmann uint32_t firstport; 1399fdf7027SHans de Goede uint32_t maxframes; 140f1ae32a1SGerd Hoffmann }; 141f1ae32a1SGerd Hoffmann 142f1ae32a1SGerd Hoffmann typedef struct UHCI_TD { 143f1ae32a1SGerd Hoffmann uint32_t link; 144f1ae32a1SGerd Hoffmann uint32_t ctrl; /* see TD_CTRL_xxx */ 145f1ae32a1SGerd Hoffmann uint32_t token; 146f1ae32a1SGerd Hoffmann uint32_t buffer; 147f1ae32a1SGerd Hoffmann } UHCI_TD; 148f1ae32a1SGerd Hoffmann 149f1ae32a1SGerd Hoffmann typedef struct UHCI_QH { 150f1ae32a1SGerd Hoffmann uint32_t link; 151f1ae32a1SGerd Hoffmann uint32_t el_link; 152f1ae32a1SGerd Hoffmann } UHCI_QH; 153f1ae32a1SGerd Hoffmann 15440507377SHans de Goede static void uhci_async_cancel(UHCIAsync *async); 15511d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td); 1569f0f1a0cSGerd Hoffmann static void uhci_resume(void *opaque); 15740507377SHans de Goede 15849184b62SGonglei #define TYPE_UHCI "pci-uhci-usb" 15949184b62SGonglei #define UHCI(obj) OBJECT_CHECK(UHCIState, (obj), TYPE_UHCI) 16049184b62SGonglei 161f1ae32a1SGerd Hoffmann static inline int32_t uhci_queue_token(UHCI_TD *td) 162f1ae32a1SGerd Hoffmann { 1636fe30910SHans de Goede if ((td->token & (0xf << 15)) == 0) { 1646fe30910SHans de Goede /* ctrl ep, cover ep and dev, not pid! */ 1656fe30910SHans de Goede return td->token & 0x7ff00; 1666fe30910SHans de Goede } else { 167f1ae32a1SGerd Hoffmann /* covers ep, dev, pid -> identifies the endpoint */ 168f1ae32a1SGerd Hoffmann return td->token & 0x7ffff; 169f1ae32a1SGerd Hoffmann } 1706fe30910SHans de Goede } 171f1ae32a1SGerd Hoffmann 17266a08cbeSHans de Goede static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td, 17366a08cbeSHans de Goede USBEndpoint *ep) 174f1ae32a1SGerd Hoffmann { 175f1ae32a1SGerd Hoffmann UHCIQueue *queue; 176f1ae32a1SGerd Hoffmann 177f1ae32a1SGerd Hoffmann queue = g_new0(UHCIQueue, 1); 178f1ae32a1SGerd Hoffmann queue->uhci = s; 17966a08cbeSHans de Goede queue->qh_addr = qh_addr; 18066a08cbeSHans de Goede queue->token = uhci_queue_token(td); 18111d15e40SHans de Goede queue->ep = ep; 182f1ae32a1SGerd Hoffmann QTAILQ_INIT(&queue->asyncs); 183f1ae32a1SGerd Hoffmann QTAILQ_INSERT_HEAD(&s->queues, queue, next); 184475443cfSHans de Goede queue->valid = QH_VALID; 18550dcc0f8SGerd Hoffmann trace_usb_uhci_queue_add(queue->token); 186f1ae32a1SGerd Hoffmann return queue; 187f1ae32a1SGerd Hoffmann } 188f1ae32a1SGerd Hoffmann 18966a08cbeSHans de Goede static void uhci_queue_free(UHCIQueue *queue, const char *reason) 190f1ae32a1SGerd Hoffmann { 191f1ae32a1SGerd Hoffmann UHCIState *s = queue->uhci; 19240507377SHans de Goede UHCIAsync *async; 19340507377SHans de Goede 19440507377SHans de Goede while (!QTAILQ_EMPTY(&queue->asyncs)) { 19540507377SHans de Goede async = QTAILQ_FIRST(&queue->asyncs); 19640507377SHans de Goede uhci_async_cancel(async); 19740507377SHans de Goede } 198f79738b0SHans de Goede usb_device_ep_stopped(queue->ep->dev, queue->ep); 199f1ae32a1SGerd Hoffmann 20066a08cbeSHans de Goede trace_usb_uhci_queue_del(queue->token, reason); 201f1ae32a1SGerd Hoffmann QTAILQ_REMOVE(&s->queues, queue, next); 202f1ae32a1SGerd Hoffmann g_free(queue); 203f1ae32a1SGerd Hoffmann } 204f1ae32a1SGerd Hoffmann 20566a08cbeSHans de Goede static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td) 20666a08cbeSHans de Goede { 20766a08cbeSHans de Goede uint32_t token = uhci_queue_token(td); 20866a08cbeSHans de Goede UHCIQueue *queue; 20966a08cbeSHans de Goede 21066a08cbeSHans de Goede QTAILQ_FOREACH(queue, &s->queues, next) { 21166a08cbeSHans de Goede if (queue->token == token) { 21266a08cbeSHans de Goede return queue; 21366a08cbeSHans de Goede } 21466a08cbeSHans de Goede } 21566a08cbeSHans de Goede return NULL; 21666a08cbeSHans de Goede } 21766a08cbeSHans de Goede 21866a08cbeSHans de Goede static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td, 21966a08cbeSHans de Goede uint32_t td_addr, bool queuing) 22066a08cbeSHans de Goede { 22166a08cbeSHans de Goede UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs); 222c348e481SGerd Hoffmann uint32_t queue_token_addr = (queue->token >> 8) & 0x7f; 22366a08cbeSHans de Goede 22466a08cbeSHans de Goede return queue->qh_addr == qh_addr && 22566a08cbeSHans de Goede queue->token == uhci_queue_token(td) && 226c348e481SGerd Hoffmann queue_token_addr == queue->ep->dev->addr && 22766a08cbeSHans de Goede (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL || 22866a08cbeSHans de Goede first->td_addr == td_addr); 22966a08cbeSHans de Goede } 23066a08cbeSHans de Goede 2311f250cc7SHans de Goede static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr) 232f1ae32a1SGerd Hoffmann { 233f1ae32a1SGerd Hoffmann UHCIAsync *async = g_new0(UHCIAsync, 1); 234f1ae32a1SGerd Hoffmann 235f1ae32a1SGerd Hoffmann async->queue = queue; 2361f250cc7SHans de Goede async->td_addr = td_addr; 237f1ae32a1SGerd Hoffmann usb_packet_init(&async->packet); 2381f250cc7SHans de Goede trace_usb_uhci_packet_add(async->queue->token, async->td_addr); 239f1ae32a1SGerd Hoffmann 240f1ae32a1SGerd Hoffmann return async; 241f1ae32a1SGerd Hoffmann } 242f1ae32a1SGerd Hoffmann 243f1ae32a1SGerd Hoffmann static void uhci_async_free(UHCIAsync *async) 244f1ae32a1SGerd Hoffmann { 2451f250cc7SHans de Goede trace_usb_uhci_packet_del(async->queue->token, async->td_addr); 246f1ae32a1SGerd Hoffmann usb_packet_cleanup(&async->packet); 2479822261cSHans de Goede if (async->buf != async->static_buf) { 2489822261cSHans de Goede g_free(async->buf); 2499822261cSHans de Goede } 250f1ae32a1SGerd Hoffmann g_free(async); 251f1ae32a1SGerd Hoffmann } 252f1ae32a1SGerd Hoffmann 253f1ae32a1SGerd Hoffmann static void uhci_async_link(UHCIAsync *async) 254f1ae32a1SGerd Hoffmann { 255f1ae32a1SGerd Hoffmann UHCIQueue *queue = async->queue; 256f1ae32a1SGerd Hoffmann QTAILQ_INSERT_TAIL(&queue->asyncs, async, next); 2571f250cc7SHans de Goede trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr); 258f1ae32a1SGerd Hoffmann } 259f1ae32a1SGerd Hoffmann 260f1ae32a1SGerd Hoffmann static void uhci_async_unlink(UHCIAsync *async) 261f1ae32a1SGerd Hoffmann { 262f1ae32a1SGerd Hoffmann UHCIQueue *queue = async->queue; 263f1ae32a1SGerd Hoffmann QTAILQ_REMOVE(&queue->asyncs, async, next); 2641f250cc7SHans de Goede trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr); 265f1ae32a1SGerd Hoffmann } 266f1ae32a1SGerd Hoffmann 267f1ae32a1SGerd Hoffmann static void uhci_async_cancel(UHCIAsync *async) 268f1ae32a1SGerd Hoffmann { 2692f2ee268SHans de Goede uhci_async_unlink(async); 2701f250cc7SHans de Goede trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr, 2711f250cc7SHans de Goede async->done); 272f1ae32a1SGerd Hoffmann if (!async->done) 273f1ae32a1SGerd Hoffmann usb_cancel_packet(&async->packet); 274f1ae32a1SGerd Hoffmann uhci_async_free(async); 275f1ae32a1SGerd Hoffmann } 276f1ae32a1SGerd Hoffmann 277f1ae32a1SGerd Hoffmann /* 278f1ae32a1SGerd Hoffmann * Mark all outstanding async packets as invalid. 279f1ae32a1SGerd Hoffmann * This is used for canceling them when TDs are removed by the HCD. 280f1ae32a1SGerd Hoffmann */ 281f1ae32a1SGerd Hoffmann static void uhci_async_validate_begin(UHCIState *s) 282f1ae32a1SGerd Hoffmann { 283f1ae32a1SGerd Hoffmann UHCIQueue *queue; 284f1ae32a1SGerd Hoffmann 285f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(queue, &s->queues, next) { 286f1ae32a1SGerd Hoffmann queue->valid--; 287f1ae32a1SGerd Hoffmann } 288f1ae32a1SGerd Hoffmann } 289f1ae32a1SGerd Hoffmann 290f1ae32a1SGerd Hoffmann /* 291f1ae32a1SGerd Hoffmann * Cancel async packets that are no longer valid 292f1ae32a1SGerd Hoffmann */ 293f1ae32a1SGerd Hoffmann static void uhci_async_validate_end(UHCIState *s) 294f1ae32a1SGerd Hoffmann { 295f1ae32a1SGerd Hoffmann UHCIQueue *queue, *n; 296f1ae32a1SGerd Hoffmann 297f1ae32a1SGerd Hoffmann QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) { 29840507377SHans de Goede if (!queue->valid) { 29966a08cbeSHans de Goede uhci_queue_free(queue, "validate-end"); 300f1ae32a1SGerd Hoffmann } 301f1ae32a1SGerd Hoffmann } 30240507377SHans de Goede } 303f1ae32a1SGerd Hoffmann 304f1ae32a1SGerd Hoffmann static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev) 305f1ae32a1SGerd Hoffmann { 3065ad23e87SHans de Goede UHCIQueue *queue, *n; 307f1ae32a1SGerd Hoffmann 3085ad23e87SHans de Goede QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) { 3095ad23e87SHans de Goede if (queue->ep->dev == dev) { 3105ad23e87SHans de Goede uhci_queue_free(queue, "cancel-device"); 311f1ae32a1SGerd Hoffmann } 312f1ae32a1SGerd Hoffmann } 313f1ae32a1SGerd Hoffmann } 314f1ae32a1SGerd Hoffmann 315f1ae32a1SGerd Hoffmann static void uhci_async_cancel_all(UHCIState *s) 316f1ae32a1SGerd Hoffmann { 31777fa9aeeSGerd Hoffmann UHCIQueue *queue, *nq; 318f1ae32a1SGerd Hoffmann 31977fa9aeeSGerd Hoffmann QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) { 32066a08cbeSHans de Goede uhci_queue_free(queue, "cancel-all"); 321f1ae32a1SGerd Hoffmann } 322f1ae32a1SGerd Hoffmann } 323f1ae32a1SGerd Hoffmann 3248c75a899SHans de Goede static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr) 325f1ae32a1SGerd Hoffmann { 326f1ae32a1SGerd Hoffmann UHCIQueue *queue; 327f1ae32a1SGerd Hoffmann UHCIAsync *async; 328f1ae32a1SGerd Hoffmann 329f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(queue, &s->queues, next) { 330f1ae32a1SGerd Hoffmann QTAILQ_FOREACH(async, &queue->asyncs, next) { 3311f250cc7SHans de Goede if (async->td_addr == td_addr) { 332f1ae32a1SGerd Hoffmann return async; 333f1ae32a1SGerd Hoffmann } 334f1ae32a1SGerd Hoffmann } 3358c75a899SHans de Goede } 336f1ae32a1SGerd Hoffmann return NULL; 337f1ae32a1SGerd Hoffmann } 338f1ae32a1SGerd Hoffmann 339f1ae32a1SGerd Hoffmann static void uhci_update_irq(UHCIState *s) 340f1ae32a1SGerd Hoffmann { 341f1ae32a1SGerd Hoffmann int level; 342f1ae32a1SGerd Hoffmann if (((s->status2 & 1) && (s->intr & (1 << 2))) || 343f1ae32a1SGerd Hoffmann ((s->status2 & 2) && (s->intr & (1 << 3))) || 344f1ae32a1SGerd Hoffmann ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) || 345f1ae32a1SGerd Hoffmann ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) || 346f1ae32a1SGerd Hoffmann (s->status & UHCI_STS_HSERR) || 347f1ae32a1SGerd Hoffmann (s->status & UHCI_STS_HCPERR)) { 348f1ae32a1SGerd Hoffmann level = 1; 349f1ae32a1SGerd Hoffmann } else { 350f1ae32a1SGerd Hoffmann level = 0; 351f1ae32a1SGerd Hoffmann } 3529e64f8a3SMarcel Apfelbaum pci_set_irq(&s->dev, level); 353f1ae32a1SGerd Hoffmann } 354f1ae32a1SGerd Hoffmann 355537e572aSGonglei static void uhci_reset(DeviceState *dev) 356f1ae32a1SGerd Hoffmann { 357537e572aSGonglei PCIDevice *d = PCI_DEVICE(dev); 35849184b62SGonglei UHCIState *s = UHCI(d); 359f1ae32a1SGerd Hoffmann uint8_t *pci_conf; 360f1ae32a1SGerd Hoffmann int i; 361f1ae32a1SGerd Hoffmann UHCIPort *port; 362f1ae32a1SGerd Hoffmann 36350dcc0f8SGerd Hoffmann trace_usb_uhci_reset(); 364f1ae32a1SGerd Hoffmann 365f1ae32a1SGerd Hoffmann pci_conf = s->dev.config; 366f1ae32a1SGerd Hoffmann 367f1ae32a1SGerd Hoffmann pci_conf[0x6a] = 0x01; /* usb clock */ 368f1ae32a1SGerd Hoffmann pci_conf[0x6b] = 0x00; 369f1ae32a1SGerd Hoffmann s->cmd = 0; 370ca5a21c4SGerd Hoffmann s->status = UHCI_STS_HCHALTED; 371f1ae32a1SGerd Hoffmann s->status2 = 0; 372f1ae32a1SGerd Hoffmann s->intr = 0; 373f1ae32a1SGerd Hoffmann s->fl_base_addr = 0; 374f1ae32a1SGerd Hoffmann s->sof_timing = 64; 375f1ae32a1SGerd Hoffmann 376f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 377f1ae32a1SGerd Hoffmann port = &s->ports[i]; 378f1ae32a1SGerd Hoffmann port->ctrl = 0x0080; 379f1ae32a1SGerd Hoffmann if (port->port.dev && port->port.dev->attached) { 380f1ae32a1SGerd Hoffmann usb_port_reset(&port->port); 381f1ae32a1SGerd Hoffmann } 382f1ae32a1SGerd Hoffmann } 383f1ae32a1SGerd Hoffmann 384f1ae32a1SGerd Hoffmann uhci_async_cancel_all(s); 3859a16c595SGerd Hoffmann qemu_bh_cancel(s->bh); 386aba1f242SGerd Hoffmann uhci_update_irq(s); 387f1ae32a1SGerd Hoffmann } 388f1ae32a1SGerd Hoffmann 389f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci_port = { 390f1ae32a1SGerd Hoffmann .name = "uhci port", 391f1ae32a1SGerd Hoffmann .version_id = 1, 392f1ae32a1SGerd Hoffmann .minimum_version_id = 1, 393f1ae32a1SGerd Hoffmann .fields = (VMStateField[]) { 394f1ae32a1SGerd Hoffmann VMSTATE_UINT16(ctrl, UHCIPort), 395f1ae32a1SGerd Hoffmann VMSTATE_END_OF_LIST() 396f1ae32a1SGerd Hoffmann } 397f1ae32a1SGerd Hoffmann }; 398f1ae32a1SGerd Hoffmann 39975f151cdSGerd Hoffmann static int uhci_post_load(void *opaque, int version_id) 40075f151cdSGerd Hoffmann { 40175f151cdSGerd Hoffmann UHCIState *s = opaque; 40275f151cdSGerd Hoffmann 40375f151cdSGerd Hoffmann if (version_id < 2) { 404bc72ad67SAlex Bligh s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 40575f151cdSGerd Hoffmann (get_ticks_per_sec() / FRAME_TIMER_FREQ); 40675f151cdSGerd Hoffmann } 40775f151cdSGerd Hoffmann return 0; 40875f151cdSGerd Hoffmann } 40975f151cdSGerd Hoffmann 410f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci = { 411f1ae32a1SGerd Hoffmann .name = "uhci", 412ecfdc15fSHans de Goede .version_id = 3, 413f1ae32a1SGerd Hoffmann .minimum_version_id = 1, 41475f151cdSGerd Hoffmann .post_load = uhci_post_load, 415f1ae32a1SGerd Hoffmann .fields = (VMStateField[]) { 416f1ae32a1SGerd Hoffmann VMSTATE_PCI_DEVICE(dev, UHCIState), 417f1ae32a1SGerd Hoffmann VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState), 418f1ae32a1SGerd Hoffmann VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1, 419f1ae32a1SGerd Hoffmann vmstate_uhci_port, UHCIPort), 420f1ae32a1SGerd Hoffmann VMSTATE_UINT16(cmd, UHCIState), 421f1ae32a1SGerd Hoffmann VMSTATE_UINT16(status, UHCIState), 422f1ae32a1SGerd Hoffmann VMSTATE_UINT16(intr, UHCIState), 423f1ae32a1SGerd Hoffmann VMSTATE_UINT16(frnum, UHCIState), 424f1ae32a1SGerd Hoffmann VMSTATE_UINT32(fl_base_addr, UHCIState), 425f1ae32a1SGerd Hoffmann VMSTATE_UINT8(sof_timing, UHCIState), 426f1ae32a1SGerd Hoffmann VMSTATE_UINT8(status2, UHCIState), 427e720677eSPaolo Bonzini VMSTATE_TIMER_PTR(frame_timer, UHCIState), 428f1ae32a1SGerd Hoffmann VMSTATE_INT64_V(expire_time, UHCIState, 2), 429ecfdc15fSHans de Goede VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3), 430f1ae32a1SGerd Hoffmann VMSTATE_END_OF_LIST() 431f1ae32a1SGerd Hoffmann } 432f1ae32a1SGerd Hoffmann }; 433f1ae32a1SGerd Hoffmann 43489eb147cSGerd Hoffmann static void uhci_port_write(void *opaque, hwaddr addr, 43589eb147cSGerd Hoffmann uint64_t val, unsigned size) 436f1ae32a1SGerd Hoffmann { 437f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 438f1ae32a1SGerd Hoffmann 43950dcc0f8SGerd Hoffmann trace_usb_uhci_mmio_writew(addr, val); 440f1ae32a1SGerd Hoffmann 441f1ae32a1SGerd Hoffmann switch(addr) { 442f1ae32a1SGerd Hoffmann case 0x00: 443f1ae32a1SGerd Hoffmann if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) { 444f1ae32a1SGerd Hoffmann /* start frame processing */ 44550dcc0f8SGerd Hoffmann trace_usb_uhci_schedule_start(); 446bc72ad67SAlex Bligh s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 447f1ae32a1SGerd Hoffmann (get_ticks_per_sec() / FRAME_TIMER_FREQ); 448bc72ad67SAlex Bligh timer_mod(s->frame_timer, s->expire_time); 449f1ae32a1SGerd Hoffmann s->status &= ~UHCI_STS_HCHALTED; 450f1ae32a1SGerd Hoffmann } else if (!(val & UHCI_CMD_RS)) { 451f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCHALTED; 452f1ae32a1SGerd Hoffmann } 453f1ae32a1SGerd Hoffmann if (val & UHCI_CMD_GRESET) { 454f1ae32a1SGerd Hoffmann UHCIPort *port; 455f1ae32a1SGerd Hoffmann int i; 456f1ae32a1SGerd Hoffmann 457f1ae32a1SGerd Hoffmann /* send reset on the USB bus */ 458f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 459f1ae32a1SGerd Hoffmann port = &s->ports[i]; 460f1ae32a1SGerd Hoffmann usb_device_reset(port->port.dev); 461f1ae32a1SGerd Hoffmann } 462537e572aSGonglei uhci_reset(DEVICE(s)); 463f1ae32a1SGerd Hoffmann return; 464f1ae32a1SGerd Hoffmann } 465f1ae32a1SGerd Hoffmann if (val & UHCI_CMD_HCRESET) { 466537e572aSGonglei uhci_reset(DEVICE(s)); 467f1ae32a1SGerd Hoffmann return; 468f1ae32a1SGerd Hoffmann } 469f1ae32a1SGerd Hoffmann s->cmd = val; 4709f0f1a0cSGerd Hoffmann if (val & UHCI_CMD_EGSM) { 4719f0f1a0cSGerd Hoffmann if ((s->ports[0].ctrl & UHCI_PORT_RD) || 4729f0f1a0cSGerd Hoffmann (s->ports[1].ctrl & UHCI_PORT_RD)) { 4739f0f1a0cSGerd Hoffmann uhci_resume(s); 4749f0f1a0cSGerd Hoffmann } 4759f0f1a0cSGerd Hoffmann } 476f1ae32a1SGerd Hoffmann break; 477f1ae32a1SGerd Hoffmann case 0x02: 478f1ae32a1SGerd Hoffmann s->status &= ~val; 479f1ae32a1SGerd Hoffmann /* XXX: the chip spec is not coherent, so we add a hidden 480f1ae32a1SGerd Hoffmann register to distinguish between IOC and SPD */ 481f1ae32a1SGerd Hoffmann if (val & UHCI_STS_USBINT) 482f1ae32a1SGerd Hoffmann s->status2 = 0; 483f1ae32a1SGerd Hoffmann uhci_update_irq(s); 484f1ae32a1SGerd Hoffmann break; 485f1ae32a1SGerd Hoffmann case 0x04: 486f1ae32a1SGerd Hoffmann s->intr = val; 487f1ae32a1SGerd Hoffmann uhci_update_irq(s); 488f1ae32a1SGerd Hoffmann break; 489f1ae32a1SGerd Hoffmann case 0x06: 490f1ae32a1SGerd Hoffmann if (s->status & UHCI_STS_HCHALTED) 491f1ae32a1SGerd Hoffmann s->frnum = val & 0x7ff; 492f1ae32a1SGerd Hoffmann break; 49389eb147cSGerd Hoffmann case 0x08: 49489eb147cSGerd Hoffmann s->fl_base_addr &= 0xffff0000; 49589eb147cSGerd Hoffmann s->fl_base_addr |= val & ~0xfff; 49689eb147cSGerd Hoffmann break; 49789eb147cSGerd Hoffmann case 0x0a: 49889eb147cSGerd Hoffmann s->fl_base_addr &= 0x0000ffff; 49989eb147cSGerd Hoffmann s->fl_base_addr |= (val << 16); 50089eb147cSGerd Hoffmann break; 50189eb147cSGerd Hoffmann case 0x0c: 50289eb147cSGerd Hoffmann s->sof_timing = val & 0xff; 50389eb147cSGerd Hoffmann break; 504f1ae32a1SGerd Hoffmann case 0x10 ... 0x1f: 505f1ae32a1SGerd Hoffmann { 506f1ae32a1SGerd Hoffmann UHCIPort *port; 507f1ae32a1SGerd Hoffmann USBDevice *dev; 508f1ae32a1SGerd Hoffmann int n; 509f1ae32a1SGerd Hoffmann 510f1ae32a1SGerd Hoffmann n = (addr >> 1) & 7; 511f1ae32a1SGerd Hoffmann if (n >= NB_PORTS) 512f1ae32a1SGerd Hoffmann return; 513f1ae32a1SGerd Hoffmann port = &s->ports[n]; 514f1ae32a1SGerd Hoffmann dev = port->port.dev; 515f1ae32a1SGerd Hoffmann if (dev && dev->attached) { 516f1ae32a1SGerd Hoffmann /* port reset */ 517f1ae32a1SGerd Hoffmann if ( (val & UHCI_PORT_RESET) && 518f1ae32a1SGerd Hoffmann !(port->ctrl & UHCI_PORT_RESET) ) { 519f1ae32a1SGerd Hoffmann usb_device_reset(dev); 520f1ae32a1SGerd Hoffmann } 521f1ae32a1SGerd Hoffmann } 522f1ae32a1SGerd Hoffmann port->ctrl &= UHCI_PORT_READ_ONLY; 5231cbdde90SHans de Goede /* enabled may only be set if a device is connected */ 5241cbdde90SHans de Goede if (!(port->ctrl & UHCI_PORT_CCS)) { 5251cbdde90SHans de Goede val &= ~UHCI_PORT_EN; 5261cbdde90SHans de Goede } 527f1ae32a1SGerd Hoffmann port->ctrl |= (val & ~UHCI_PORT_READ_ONLY); 528f1ae32a1SGerd Hoffmann /* some bits are reset when a '1' is written to them */ 529f1ae32a1SGerd Hoffmann port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR); 530f1ae32a1SGerd Hoffmann } 531f1ae32a1SGerd Hoffmann break; 532f1ae32a1SGerd Hoffmann } 533f1ae32a1SGerd Hoffmann } 534f1ae32a1SGerd Hoffmann 53589eb147cSGerd Hoffmann static uint64_t uhci_port_read(void *opaque, hwaddr addr, unsigned size) 536f1ae32a1SGerd Hoffmann { 537f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 538f1ae32a1SGerd Hoffmann uint32_t val; 539f1ae32a1SGerd Hoffmann 540f1ae32a1SGerd Hoffmann switch(addr) { 541f1ae32a1SGerd Hoffmann case 0x00: 542f1ae32a1SGerd Hoffmann val = s->cmd; 543f1ae32a1SGerd Hoffmann break; 544f1ae32a1SGerd Hoffmann case 0x02: 545f1ae32a1SGerd Hoffmann val = s->status; 546f1ae32a1SGerd Hoffmann break; 547f1ae32a1SGerd Hoffmann case 0x04: 548f1ae32a1SGerd Hoffmann val = s->intr; 549f1ae32a1SGerd Hoffmann break; 550f1ae32a1SGerd Hoffmann case 0x06: 551f1ae32a1SGerd Hoffmann val = s->frnum; 552f1ae32a1SGerd Hoffmann break; 55389eb147cSGerd Hoffmann case 0x08: 55489eb147cSGerd Hoffmann val = s->fl_base_addr & 0xffff; 55589eb147cSGerd Hoffmann break; 55689eb147cSGerd Hoffmann case 0x0a: 55789eb147cSGerd Hoffmann val = (s->fl_base_addr >> 16) & 0xffff; 55889eb147cSGerd Hoffmann break; 55989eb147cSGerd Hoffmann case 0x0c: 56089eb147cSGerd Hoffmann val = s->sof_timing; 56189eb147cSGerd Hoffmann break; 562f1ae32a1SGerd Hoffmann case 0x10 ... 0x1f: 563f1ae32a1SGerd Hoffmann { 564f1ae32a1SGerd Hoffmann UHCIPort *port; 565f1ae32a1SGerd Hoffmann int n; 566f1ae32a1SGerd Hoffmann n = (addr >> 1) & 7; 567f1ae32a1SGerd Hoffmann if (n >= NB_PORTS) 568f1ae32a1SGerd Hoffmann goto read_default; 569f1ae32a1SGerd Hoffmann port = &s->ports[n]; 570f1ae32a1SGerd Hoffmann val = port->ctrl; 571f1ae32a1SGerd Hoffmann } 572f1ae32a1SGerd Hoffmann break; 573f1ae32a1SGerd Hoffmann default: 574f1ae32a1SGerd Hoffmann read_default: 575f1ae32a1SGerd Hoffmann val = 0xff7f; /* disabled port */ 576f1ae32a1SGerd Hoffmann break; 577f1ae32a1SGerd Hoffmann } 578f1ae32a1SGerd Hoffmann 57950dcc0f8SGerd Hoffmann trace_usb_uhci_mmio_readw(addr, val); 580f1ae32a1SGerd Hoffmann 581f1ae32a1SGerd Hoffmann return val; 582f1ae32a1SGerd Hoffmann } 583f1ae32a1SGerd Hoffmann 584f1ae32a1SGerd Hoffmann /* signal resume if controller suspended */ 585f1ae32a1SGerd Hoffmann static void uhci_resume (void *opaque) 586f1ae32a1SGerd Hoffmann { 587f1ae32a1SGerd Hoffmann UHCIState *s = (UHCIState *)opaque; 588f1ae32a1SGerd Hoffmann 589f1ae32a1SGerd Hoffmann if (!s) 590f1ae32a1SGerd Hoffmann return; 591f1ae32a1SGerd Hoffmann 592f1ae32a1SGerd Hoffmann if (s->cmd & UHCI_CMD_EGSM) { 593f1ae32a1SGerd Hoffmann s->cmd |= UHCI_CMD_FGR; 594f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_RD; 595f1ae32a1SGerd Hoffmann uhci_update_irq(s); 596f1ae32a1SGerd Hoffmann } 597f1ae32a1SGerd Hoffmann } 598f1ae32a1SGerd Hoffmann 599f1ae32a1SGerd Hoffmann static void uhci_attach(USBPort *port1) 600f1ae32a1SGerd Hoffmann { 601f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 602f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 603f1ae32a1SGerd Hoffmann 604f1ae32a1SGerd Hoffmann /* set connect status */ 605f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC; 606f1ae32a1SGerd Hoffmann 607f1ae32a1SGerd Hoffmann /* update speed */ 608f1ae32a1SGerd Hoffmann if (port->port.dev->speed == USB_SPEED_LOW) { 609f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_LSDA; 610f1ae32a1SGerd Hoffmann } else { 611f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_LSDA; 612f1ae32a1SGerd Hoffmann } 613f1ae32a1SGerd Hoffmann 614f1ae32a1SGerd Hoffmann uhci_resume(s); 615f1ae32a1SGerd Hoffmann } 616f1ae32a1SGerd Hoffmann 617f1ae32a1SGerd Hoffmann static void uhci_detach(USBPort *port1) 618f1ae32a1SGerd Hoffmann { 619f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 620f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 621f1ae32a1SGerd Hoffmann 622f1ae32a1SGerd Hoffmann uhci_async_cancel_device(s, port1->dev); 623f1ae32a1SGerd Hoffmann 624f1ae32a1SGerd Hoffmann /* set connect status */ 625f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_CCS) { 626f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_CCS; 627f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_CSC; 628f1ae32a1SGerd Hoffmann } 629f1ae32a1SGerd Hoffmann /* disable port */ 630f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_EN) { 631f1ae32a1SGerd Hoffmann port->ctrl &= ~UHCI_PORT_EN; 632f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_ENC; 633f1ae32a1SGerd Hoffmann } 634f1ae32a1SGerd Hoffmann 635f1ae32a1SGerd Hoffmann uhci_resume(s); 636f1ae32a1SGerd Hoffmann } 637f1ae32a1SGerd Hoffmann 638f1ae32a1SGerd Hoffmann static void uhci_child_detach(USBPort *port1, USBDevice *child) 639f1ae32a1SGerd Hoffmann { 640f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 641f1ae32a1SGerd Hoffmann 642f1ae32a1SGerd Hoffmann uhci_async_cancel_device(s, child); 643f1ae32a1SGerd Hoffmann } 644f1ae32a1SGerd Hoffmann 645f1ae32a1SGerd Hoffmann static void uhci_wakeup(USBPort *port1) 646f1ae32a1SGerd Hoffmann { 647f1ae32a1SGerd Hoffmann UHCIState *s = port1->opaque; 648f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[port1->index]; 649f1ae32a1SGerd Hoffmann 650f1ae32a1SGerd Hoffmann if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) { 651f1ae32a1SGerd Hoffmann port->ctrl |= UHCI_PORT_RD; 652f1ae32a1SGerd Hoffmann uhci_resume(s); 653f1ae32a1SGerd Hoffmann } 654f1ae32a1SGerd Hoffmann } 655f1ae32a1SGerd Hoffmann 656f1ae32a1SGerd Hoffmann static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr) 657f1ae32a1SGerd Hoffmann { 658f1ae32a1SGerd Hoffmann USBDevice *dev; 659f1ae32a1SGerd Hoffmann int i; 660f1ae32a1SGerd Hoffmann 661f1ae32a1SGerd Hoffmann for (i = 0; i < NB_PORTS; i++) { 662f1ae32a1SGerd Hoffmann UHCIPort *port = &s->ports[i]; 663f1ae32a1SGerd Hoffmann if (!(port->ctrl & UHCI_PORT_EN)) { 664f1ae32a1SGerd Hoffmann continue; 665f1ae32a1SGerd Hoffmann } 666f1ae32a1SGerd Hoffmann dev = usb_find_device(&port->port, addr); 667f1ae32a1SGerd Hoffmann if (dev != NULL) { 668f1ae32a1SGerd Hoffmann return dev; 669f1ae32a1SGerd Hoffmann } 670f1ae32a1SGerd Hoffmann } 671f1ae32a1SGerd Hoffmann return NULL; 672f1ae32a1SGerd Hoffmann } 673f1ae32a1SGerd Hoffmann 674963a68b5SHans de Goede static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link) 675963a68b5SHans de Goede { 676963a68b5SHans de Goede pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td)); 677963a68b5SHans de Goede le32_to_cpus(&td->link); 678963a68b5SHans de Goede le32_to_cpus(&td->ctrl); 679963a68b5SHans de Goede le32_to_cpus(&td->token); 680963a68b5SHans de Goede le32_to_cpus(&td->buffer); 681963a68b5SHans de Goede } 682963a68b5SHans de Goede 683faccca00SHans de Goede static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr, 684faccca00SHans de Goede int status, uint32_t *int_mask) 685faccca00SHans de Goede { 686faccca00SHans de Goede uint32_t queue_token = uhci_queue_token(td); 687faccca00SHans de Goede int ret; 688faccca00SHans de Goede 689faccca00SHans de Goede switch (status) { 690faccca00SHans de Goede case USB_RET_NAK: 691faccca00SHans de Goede td->ctrl |= TD_CTRL_NAK; 692faccca00SHans de Goede return TD_RESULT_NEXT_QH; 693faccca00SHans de Goede 694faccca00SHans de Goede case USB_RET_STALL: 695faccca00SHans de Goede td->ctrl |= TD_CTRL_STALL; 696faccca00SHans de Goede trace_usb_uhci_packet_complete_stall(queue_token, td_addr); 697faccca00SHans de Goede ret = TD_RESULT_NEXT_QH; 698faccca00SHans de Goede break; 699faccca00SHans de Goede 700faccca00SHans de Goede case USB_RET_BABBLE: 701faccca00SHans de Goede td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL; 702faccca00SHans de Goede /* frame interrupted */ 703faccca00SHans de Goede trace_usb_uhci_packet_complete_babble(queue_token, td_addr); 704faccca00SHans de Goede ret = TD_RESULT_STOP_FRAME; 705faccca00SHans de Goede break; 706faccca00SHans de Goede 707faccca00SHans de Goede case USB_RET_IOERROR: 708faccca00SHans de Goede case USB_RET_NODEV: 709faccca00SHans de Goede default: 710faccca00SHans de Goede td->ctrl |= TD_CTRL_TIMEOUT; 711faccca00SHans de Goede td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT); 712faccca00SHans de Goede trace_usb_uhci_packet_complete_error(queue_token, td_addr); 713faccca00SHans de Goede ret = TD_RESULT_NEXT_QH; 714faccca00SHans de Goede break; 715faccca00SHans de Goede } 716faccca00SHans de Goede 717faccca00SHans de Goede td->ctrl &= ~TD_CTRL_ACTIVE; 718faccca00SHans de Goede s->status |= UHCI_STS_USBERR; 719faccca00SHans de Goede if (td->ctrl & TD_CTRL_IOC) { 720faccca00SHans de Goede *int_mask |= 0x01; 721faccca00SHans de Goede } 722faccca00SHans de Goede uhci_update_irq(s); 723faccca00SHans de Goede return ret; 724faccca00SHans de Goede } 725faccca00SHans de Goede 726f1ae32a1SGerd Hoffmann static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask) 727f1ae32a1SGerd Hoffmann { 7289a77a0f5SHans de Goede int len = 0, max_len; 729f1ae32a1SGerd Hoffmann uint8_t pid; 730f1ae32a1SGerd Hoffmann 731f1ae32a1SGerd Hoffmann max_len = ((td->token >> 21) + 1) & 0x7ff; 732f1ae32a1SGerd Hoffmann pid = td->token & 0xff; 733f1ae32a1SGerd Hoffmann 734f1ae32a1SGerd Hoffmann if (td->ctrl & TD_CTRL_IOS) 735f1ae32a1SGerd Hoffmann td->ctrl &= ~TD_CTRL_ACTIVE; 736f1ae32a1SGerd Hoffmann 7379a77a0f5SHans de Goede if (async->packet.status != USB_RET_SUCCESS) { 7389a77a0f5SHans de Goede return uhci_handle_td_error(s, td, async->td_addr, 7399a77a0f5SHans de Goede async->packet.status, int_mask); 740faccca00SHans de Goede } 741f1ae32a1SGerd Hoffmann 7429a77a0f5SHans de Goede len = async->packet.actual_length; 743f1ae32a1SGerd Hoffmann td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff); 744f1ae32a1SGerd Hoffmann 745f1ae32a1SGerd Hoffmann /* The NAK bit may have been set by a previous frame, so clear it 746f1ae32a1SGerd Hoffmann here. The docs are somewhat unclear, but win2k relies on this 747f1ae32a1SGerd Hoffmann behavior. */ 748f1ae32a1SGerd Hoffmann td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK); 749f1ae32a1SGerd Hoffmann if (td->ctrl & TD_CTRL_IOC) 750f1ae32a1SGerd Hoffmann *int_mask |= 0x01; 751f1ae32a1SGerd Hoffmann 752f1ae32a1SGerd Hoffmann if (pid == USB_TOKEN_IN) { 7539822261cSHans de Goede pci_dma_write(&s->dev, td->buffer, async->buf, len); 754f1ae32a1SGerd Hoffmann if ((td->ctrl & TD_CTRL_SPD) && len < max_len) { 755f1ae32a1SGerd Hoffmann *int_mask |= 0x02; 756f1ae32a1SGerd Hoffmann /* short packet: do not update QH */ 75750dcc0f8SGerd Hoffmann trace_usb_uhci_packet_complete_shortxfer(async->queue->token, 7581f250cc7SHans de Goede async->td_addr); 75960e1b2a6SGerd Hoffmann return TD_RESULT_NEXT_QH; 760f1ae32a1SGerd Hoffmann } 761f1ae32a1SGerd Hoffmann } 762f1ae32a1SGerd Hoffmann 763f1ae32a1SGerd Hoffmann /* success */ 7641f250cc7SHans de Goede trace_usb_uhci_packet_complete_success(async->queue->token, 7651f250cc7SHans de Goede async->td_addr); 76660e1b2a6SGerd Hoffmann return TD_RESULT_COMPLETE; 767f1ae32a1SGerd Hoffmann } 768f1ae32a1SGerd Hoffmann 76966a08cbeSHans de Goede static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr, 770a4f30cd7SHans de Goede UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask) 771f1ae32a1SGerd Hoffmann { 7729a77a0f5SHans de Goede int ret, max_len; 7736ba43f1fSHans de Goede bool spd; 774a4f30cd7SHans de Goede bool queuing = (q != NULL); 77511d15e40SHans de Goede uint8_t pid = td->token & 0xff; 7768c75a899SHans de Goede UHCIAsync *async = uhci_async_find_td(s, td_addr); 7778c75a899SHans de Goede 7788c75a899SHans de Goede if (async) { 7798c75a899SHans de Goede if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) { 7808c75a899SHans de Goede assert(q == NULL || q == async->queue); 7818c75a899SHans de Goede q = async->queue; 7828c75a899SHans de Goede } else { 7838c75a899SHans de Goede uhci_queue_free(async->queue, "guest re-used pending td"); 7848c75a899SHans de Goede async = NULL; 7858c75a899SHans de Goede } 7868c75a899SHans de Goede } 787f1ae32a1SGerd Hoffmann 78866a08cbeSHans de Goede if (q == NULL) { 78966a08cbeSHans de Goede q = uhci_queue_find(s, td); 79066a08cbeSHans de Goede if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) { 79166a08cbeSHans de Goede uhci_queue_free(q, "guest re-used qh"); 79266a08cbeSHans de Goede q = NULL; 79366a08cbeSHans de Goede } 79466a08cbeSHans de Goede } 79566a08cbeSHans de Goede 7963905097eSHans de Goede if (q) { 797475443cfSHans de Goede q->valid = QH_VALID; 7983905097eSHans de Goede } 7993905097eSHans de Goede 800f1ae32a1SGerd Hoffmann /* Is active ? */ 801883bca77SHans de Goede if (!(td->ctrl & TD_CTRL_ACTIVE)) { 802420ca987SHans de Goede if (async) { 803420ca987SHans de Goede /* Guest marked a pending td non-active, cancel the queue */ 804420ca987SHans de Goede uhci_queue_free(async->queue, "pending td non-active"); 805420ca987SHans de Goede } 806883bca77SHans de Goede /* 807883bca77SHans de Goede * ehci11d spec page 22: "Even if the Active bit in the TD is already 808883bca77SHans de Goede * cleared when the TD is fetched ... an IOC interrupt is generated" 809883bca77SHans de Goede */ 810883bca77SHans de Goede if (td->ctrl & TD_CTRL_IOC) { 811883bca77SHans de Goede *int_mask |= 0x01; 812883bca77SHans de Goede } 81360e1b2a6SGerd Hoffmann return TD_RESULT_NEXT_QH; 814883bca77SHans de Goede } 815f1ae32a1SGerd Hoffmann 816f1ae32a1SGerd Hoffmann if (async) { 817ee008ba6SGerd Hoffmann if (queuing) { 818ee008ba6SGerd Hoffmann /* we are busy filling the queue, we are not prepared 819ee008ba6SGerd Hoffmann to consume completed packages then, just leave them 820ee008ba6SGerd Hoffmann in async state */ 821ee008ba6SGerd Hoffmann return TD_RESULT_ASYNC_CONT; 822ee008ba6SGerd Hoffmann } 8238928c9c4SHans de Goede if (!async->done) { 8248928c9c4SHans de Goede UHCI_TD last_td; 8258928c9c4SHans de Goede UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs, asyncs_head); 8268928c9c4SHans de Goede /* 8278928c9c4SHans de Goede * While we are waiting for the current td to complete, the guest 8288928c9c4SHans de Goede * may have added more tds to the queue. Note we re-read the td 8298928c9c4SHans de Goede * rather then caching it, as we want to see guest made changes! 8308928c9c4SHans de Goede */ 8318928c9c4SHans de Goede uhci_read_td(s, &last_td, last->td_addr); 8328928c9c4SHans de Goede uhci_queue_fill(async->queue, &last_td); 833f1ae32a1SGerd Hoffmann 8348928c9c4SHans de Goede return TD_RESULT_ASYNC_CONT; 8358928c9c4SHans de Goede } 836f1ae32a1SGerd Hoffmann uhci_async_unlink(async); 837f1ae32a1SGerd Hoffmann goto done; 838f1ae32a1SGerd Hoffmann } 839f1ae32a1SGerd Hoffmann 84088793816SHans de Goede if (s->completions_only) { 84188793816SHans de Goede return TD_RESULT_ASYNC_CONT; 84288793816SHans de Goede } 84388793816SHans de Goede 844f1ae32a1SGerd Hoffmann /* Allocate new packet */ 845a4f30cd7SHans de Goede if (q == NULL) { 84611d15e40SHans de Goede USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f); 84711d15e40SHans de Goede USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf); 8487f102ebeSHans de Goede 8497f102ebeSHans de Goede if (ep == NULL) { 8507f102ebeSHans de Goede return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV, 8517f102ebeSHans de Goede int_mask); 8527f102ebeSHans de Goede } 85366a08cbeSHans de Goede q = uhci_queue_new(s, qh_addr, td, ep); 854a4f30cd7SHans de Goede } 855a4f30cd7SHans de Goede async = uhci_async_alloc(q, td_addr); 856f1ae32a1SGerd Hoffmann 857f1ae32a1SGerd Hoffmann max_len = ((td->token >> 21) + 1) & 0x7ff; 8586ba43f1fSHans de Goede spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0); 8598550a02dSGerd Hoffmann usb_packet_setup(&async->packet, pid, q->ep, 0, td_addr, spd, 860a6fb2ddbSHans de Goede (td->ctrl & TD_CTRL_IOC) != 0); 8619822261cSHans de Goede if (max_len <= sizeof(async->static_buf)) { 8629822261cSHans de Goede async->buf = async->static_buf; 8639822261cSHans de Goede } else { 8649822261cSHans de Goede async->buf = g_malloc(max_len); 8659822261cSHans de Goede } 8669822261cSHans de Goede usb_packet_addbuf(&async->packet, async->buf, max_len); 867f1ae32a1SGerd Hoffmann 868f1ae32a1SGerd Hoffmann switch(pid) { 869f1ae32a1SGerd Hoffmann case USB_TOKEN_OUT: 870f1ae32a1SGerd Hoffmann case USB_TOKEN_SETUP: 8719822261cSHans de Goede pci_dma_read(&s->dev, td->buffer, async->buf, max_len); 8729a77a0f5SHans de Goede usb_handle_packet(q->ep->dev, &async->packet); 8739a77a0f5SHans de Goede if (async->packet.status == USB_RET_SUCCESS) { 8749a77a0f5SHans de Goede async->packet.actual_length = max_len; 8759a77a0f5SHans de Goede } 876f1ae32a1SGerd Hoffmann break; 877f1ae32a1SGerd Hoffmann 878f1ae32a1SGerd Hoffmann case USB_TOKEN_IN: 8799a77a0f5SHans de Goede usb_handle_packet(q->ep->dev, &async->packet); 880f1ae32a1SGerd Hoffmann break; 881f1ae32a1SGerd Hoffmann 882f1ae32a1SGerd Hoffmann default: 883f1ae32a1SGerd Hoffmann /* invalid pid : frame interrupted */ 884f1ae32a1SGerd Hoffmann uhci_async_free(async); 885f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCPERR; 886f1ae32a1SGerd Hoffmann uhci_update_irq(s); 88760e1b2a6SGerd Hoffmann return TD_RESULT_STOP_FRAME; 888f1ae32a1SGerd Hoffmann } 889f1ae32a1SGerd Hoffmann 8909a77a0f5SHans de Goede if (async->packet.status == USB_RET_ASYNC) { 891f1ae32a1SGerd Hoffmann uhci_async_link(async); 892a4f30cd7SHans de Goede if (!queuing) { 89311d15e40SHans de Goede uhci_queue_fill(q, td); 894a4f30cd7SHans de Goede } 8954efe4ef3SGerd Hoffmann return TD_RESULT_ASYNC_START; 896f1ae32a1SGerd Hoffmann } 897f1ae32a1SGerd Hoffmann 898f1ae32a1SGerd Hoffmann done: 8999a77a0f5SHans de Goede ret = uhci_complete_td(s, td, async, int_mask); 900f1ae32a1SGerd Hoffmann uhci_async_free(async); 9019a77a0f5SHans de Goede return ret; 902f1ae32a1SGerd Hoffmann } 903f1ae32a1SGerd Hoffmann 904f1ae32a1SGerd Hoffmann static void uhci_async_complete(USBPort *port, USBPacket *packet) 905f1ae32a1SGerd Hoffmann { 906f1ae32a1SGerd Hoffmann UHCIAsync *async = container_of(packet, UHCIAsync, packet); 907f1ae32a1SGerd Hoffmann UHCIState *s = async->queue->uhci; 908f1ae32a1SGerd Hoffmann 9099a77a0f5SHans de Goede if (packet->status == USB_RET_REMOVE_FROM_QUEUE) { 9100cae7b1aSHans de Goede uhci_async_cancel(async); 9110cae7b1aSHans de Goede return; 9120cae7b1aSHans de Goede } 9130cae7b1aSHans de Goede 914f1ae32a1SGerd Hoffmann async->done = 1; 91588793816SHans de Goede /* Force processing of this packet *now*, needed for migration */ 91688793816SHans de Goede s->completions_only = true; 9179a16c595SGerd Hoffmann qemu_bh_schedule(s->bh); 9189a16c595SGerd Hoffmann } 919f1ae32a1SGerd Hoffmann 920f1ae32a1SGerd Hoffmann static int is_valid(uint32_t link) 921f1ae32a1SGerd Hoffmann { 922f1ae32a1SGerd Hoffmann return (link & 1) == 0; 923f1ae32a1SGerd Hoffmann } 924f1ae32a1SGerd Hoffmann 925f1ae32a1SGerd Hoffmann static int is_qh(uint32_t link) 926f1ae32a1SGerd Hoffmann { 927f1ae32a1SGerd Hoffmann return (link & 2) != 0; 928f1ae32a1SGerd Hoffmann } 929f1ae32a1SGerd Hoffmann 930f1ae32a1SGerd Hoffmann static int depth_first(uint32_t link) 931f1ae32a1SGerd Hoffmann { 932f1ae32a1SGerd Hoffmann return (link & 4) != 0; 933f1ae32a1SGerd Hoffmann } 934f1ae32a1SGerd Hoffmann 935f1ae32a1SGerd Hoffmann /* QH DB used for detecting QH loops */ 936f1ae32a1SGerd Hoffmann #define UHCI_MAX_QUEUES 128 937f1ae32a1SGerd Hoffmann typedef struct { 938f1ae32a1SGerd Hoffmann uint32_t addr[UHCI_MAX_QUEUES]; 939f1ae32a1SGerd Hoffmann int count; 940f1ae32a1SGerd Hoffmann } QhDb; 941f1ae32a1SGerd Hoffmann 942f1ae32a1SGerd Hoffmann static void qhdb_reset(QhDb *db) 943f1ae32a1SGerd Hoffmann { 944f1ae32a1SGerd Hoffmann db->count = 0; 945f1ae32a1SGerd Hoffmann } 946f1ae32a1SGerd Hoffmann 947f1ae32a1SGerd Hoffmann /* Add QH to DB. Returns 1 if already present or DB is full. */ 948f1ae32a1SGerd Hoffmann static int qhdb_insert(QhDb *db, uint32_t addr) 949f1ae32a1SGerd Hoffmann { 950f1ae32a1SGerd Hoffmann int i; 951f1ae32a1SGerd Hoffmann for (i = 0; i < db->count; i++) 952f1ae32a1SGerd Hoffmann if (db->addr[i] == addr) 953f1ae32a1SGerd Hoffmann return 1; 954f1ae32a1SGerd Hoffmann 955f1ae32a1SGerd Hoffmann if (db->count >= UHCI_MAX_QUEUES) 956f1ae32a1SGerd Hoffmann return 1; 957f1ae32a1SGerd Hoffmann 958f1ae32a1SGerd Hoffmann db->addr[db->count++] = addr; 959f1ae32a1SGerd Hoffmann return 0; 960f1ae32a1SGerd Hoffmann } 961f1ae32a1SGerd Hoffmann 96211d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td) 963f1ae32a1SGerd Hoffmann { 964f1ae32a1SGerd Hoffmann uint32_t int_mask = 0; 965f1ae32a1SGerd Hoffmann uint32_t plink = td->link; 966f1ae32a1SGerd Hoffmann UHCI_TD ptd; 967f1ae32a1SGerd Hoffmann int ret; 968f1ae32a1SGerd Hoffmann 9696ba43f1fSHans de Goede while (is_valid(plink)) { 970a4f30cd7SHans de Goede uhci_read_td(q->uhci, &ptd, plink); 971f1ae32a1SGerd Hoffmann if (!(ptd.ctrl & TD_CTRL_ACTIVE)) { 972f1ae32a1SGerd Hoffmann break; 973f1ae32a1SGerd Hoffmann } 974a4f30cd7SHans de Goede if (uhci_queue_token(&ptd) != q->token) { 975f1ae32a1SGerd Hoffmann break; 976f1ae32a1SGerd Hoffmann } 97750dcc0f8SGerd Hoffmann trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token); 97866a08cbeSHans de Goede ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask); 97952b0fecdSGerd Hoffmann if (ret == TD_RESULT_ASYNC_CONT) { 98052b0fecdSGerd Hoffmann break; 98152b0fecdSGerd Hoffmann } 9824efe4ef3SGerd Hoffmann assert(ret == TD_RESULT_ASYNC_START); 983f1ae32a1SGerd Hoffmann assert(int_mask == 0); 984f1ae32a1SGerd Hoffmann plink = ptd.link; 985f1ae32a1SGerd Hoffmann } 98611d15e40SHans de Goede usb_device_flush_ep_queue(q->ep->dev, q->ep); 987f1ae32a1SGerd Hoffmann } 988f1ae32a1SGerd Hoffmann 989f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s) 990f1ae32a1SGerd Hoffmann { 991f1ae32a1SGerd Hoffmann uint32_t frame_addr, link, old_td_ctrl, val, int_mask; 9924aed20e2SGerd Hoffmann uint32_t curr_qh, td_count = 0; 993f1ae32a1SGerd Hoffmann int cnt, ret; 994f1ae32a1SGerd Hoffmann UHCI_TD td; 995f1ae32a1SGerd Hoffmann UHCI_QH qh; 996f1ae32a1SGerd Hoffmann QhDb qhdb; 997f1ae32a1SGerd Hoffmann 998f1ae32a1SGerd Hoffmann frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2); 999f1ae32a1SGerd Hoffmann 1000f1ae32a1SGerd Hoffmann pci_dma_read(&s->dev, frame_addr, &link, 4); 1001f1ae32a1SGerd Hoffmann le32_to_cpus(&link); 1002f1ae32a1SGerd Hoffmann 1003f1ae32a1SGerd Hoffmann int_mask = 0; 1004f1ae32a1SGerd Hoffmann curr_qh = 0; 1005f1ae32a1SGerd Hoffmann 1006f1ae32a1SGerd Hoffmann qhdb_reset(&qhdb); 1007f1ae32a1SGerd Hoffmann 1008f1ae32a1SGerd Hoffmann for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) { 100988793816SHans de Goede if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) { 10104aed20e2SGerd Hoffmann /* We've reached the usb 1.1 bandwidth, which is 10114aed20e2SGerd Hoffmann 1280 bytes/frame, stop processing */ 10124aed20e2SGerd Hoffmann trace_usb_uhci_frame_stop_bandwidth(); 10134aed20e2SGerd Hoffmann break; 10144aed20e2SGerd Hoffmann } 1015f1ae32a1SGerd Hoffmann if (is_qh(link)) { 1016f1ae32a1SGerd Hoffmann /* QH */ 101750dcc0f8SGerd Hoffmann trace_usb_uhci_qh_load(link & ~0xf); 1018f1ae32a1SGerd Hoffmann 1019f1ae32a1SGerd Hoffmann if (qhdb_insert(&qhdb, link)) { 1020f1ae32a1SGerd Hoffmann /* 1021f1ae32a1SGerd Hoffmann * We're going in circles. Which is not a bug because 1022f1ae32a1SGerd Hoffmann * HCD is allowed to do that as part of the BW management. 1023f1ae32a1SGerd Hoffmann * 10244aed20e2SGerd Hoffmann * Stop processing here if no transaction has been done 10254aed20e2SGerd Hoffmann * since we've been here last time. 1026f1ae32a1SGerd Hoffmann */ 1027f1ae32a1SGerd Hoffmann if (td_count == 0) { 102850dcc0f8SGerd Hoffmann trace_usb_uhci_frame_loop_stop_idle(); 1029f1ae32a1SGerd Hoffmann break; 1030f1ae32a1SGerd Hoffmann } else { 103150dcc0f8SGerd Hoffmann trace_usb_uhci_frame_loop_continue(); 1032f1ae32a1SGerd Hoffmann td_count = 0; 1033f1ae32a1SGerd Hoffmann qhdb_reset(&qhdb); 1034f1ae32a1SGerd Hoffmann qhdb_insert(&qhdb, link); 1035f1ae32a1SGerd Hoffmann } 1036f1ae32a1SGerd Hoffmann } 1037f1ae32a1SGerd Hoffmann 1038f1ae32a1SGerd Hoffmann pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh)); 1039f1ae32a1SGerd Hoffmann le32_to_cpus(&qh.link); 1040f1ae32a1SGerd Hoffmann le32_to_cpus(&qh.el_link); 1041f1ae32a1SGerd Hoffmann 1042f1ae32a1SGerd Hoffmann if (!is_valid(qh.el_link)) { 1043f1ae32a1SGerd Hoffmann /* QH w/o elements */ 1044f1ae32a1SGerd Hoffmann curr_qh = 0; 1045f1ae32a1SGerd Hoffmann link = qh.link; 1046f1ae32a1SGerd Hoffmann } else { 1047f1ae32a1SGerd Hoffmann /* QH with elements */ 1048f1ae32a1SGerd Hoffmann curr_qh = link; 1049f1ae32a1SGerd Hoffmann link = qh.el_link; 1050f1ae32a1SGerd Hoffmann } 1051f1ae32a1SGerd Hoffmann continue; 1052f1ae32a1SGerd Hoffmann } 1053f1ae32a1SGerd Hoffmann 1054f1ae32a1SGerd Hoffmann /* TD */ 1055963a68b5SHans de Goede uhci_read_td(s, &td, link); 105650dcc0f8SGerd Hoffmann trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token); 1057f1ae32a1SGerd Hoffmann 1058f1ae32a1SGerd Hoffmann old_td_ctrl = td.ctrl; 105966a08cbeSHans de Goede ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask); 1060f1ae32a1SGerd Hoffmann if (old_td_ctrl != td.ctrl) { 1061f1ae32a1SGerd Hoffmann /* update the status bits of the TD */ 1062f1ae32a1SGerd Hoffmann val = cpu_to_le32(td.ctrl); 1063f1ae32a1SGerd Hoffmann pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val)); 1064f1ae32a1SGerd Hoffmann } 1065f1ae32a1SGerd Hoffmann 1066f1ae32a1SGerd Hoffmann switch (ret) { 106760e1b2a6SGerd Hoffmann case TD_RESULT_STOP_FRAME: /* interrupted frame */ 1068f1ae32a1SGerd Hoffmann goto out; 1069f1ae32a1SGerd Hoffmann 107060e1b2a6SGerd Hoffmann case TD_RESULT_NEXT_QH: 10714efe4ef3SGerd Hoffmann case TD_RESULT_ASYNC_CONT: 107250dcc0f8SGerd Hoffmann trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf); 1073f1ae32a1SGerd Hoffmann link = curr_qh ? qh.link : td.link; 1074f1ae32a1SGerd Hoffmann continue; 1075f1ae32a1SGerd Hoffmann 10764efe4ef3SGerd Hoffmann case TD_RESULT_ASYNC_START: 107750dcc0f8SGerd Hoffmann trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf); 1078f1ae32a1SGerd Hoffmann link = curr_qh ? qh.link : td.link; 1079f1ae32a1SGerd Hoffmann continue; 1080f1ae32a1SGerd Hoffmann 108160e1b2a6SGerd Hoffmann case TD_RESULT_COMPLETE: 108250dcc0f8SGerd Hoffmann trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf); 1083f1ae32a1SGerd Hoffmann link = td.link; 1084f1ae32a1SGerd Hoffmann td_count++; 10854aed20e2SGerd Hoffmann s->frame_bytes += (td.ctrl & 0x7ff) + 1; 1086f1ae32a1SGerd Hoffmann 1087f1ae32a1SGerd Hoffmann if (curr_qh) { 1088f1ae32a1SGerd Hoffmann /* update QH element link */ 1089f1ae32a1SGerd Hoffmann qh.el_link = link; 1090f1ae32a1SGerd Hoffmann val = cpu_to_le32(qh.el_link); 1091f1ae32a1SGerd Hoffmann pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val)); 1092f1ae32a1SGerd Hoffmann 1093f1ae32a1SGerd Hoffmann if (!depth_first(link)) { 1094f1ae32a1SGerd Hoffmann /* done with this QH */ 1095f1ae32a1SGerd Hoffmann curr_qh = 0; 1096f1ae32a1SGerd Hoffmann link = qh.link; 1097f1ae32a1SGerd Hoffmann } 1098f1ae32a1SGerd Hoffmann } 1099f1ae32a1SGerd Hoffmann break; 1100f1ae32a1SGerd Hoffmann 1101f1ae32a1SGerd Hoffmann default: 1102f1ae32a1SGerd Hoffmann assert(!"unknown return code"); 1103f1ae32a1SGerd Hoffmann } 1104f1ae32a1SGerd Hoffmann 1105f1ae32a1SGerd Hoffmann /* go to the next entry */ 1106f1ae32a1SGerd Hoffmann } 1107f1ae32a1SGerd Hoffmann 1108f1ae32a1SGerd Hoffmann out: 1109f1ae32a1SGerd Hoffmann s->pending_int_mask |= int_mask; 1110f1ae32a1SGerd Hoffmann } 1111f1ae32a1SGerd Hoffmann 11129a16c595SGerd Hoffmann static void uhci_bh(void *opaque) 11139a16c595SGerd Hoffmann { 11149a16c595SGerd Hoffmann UHCIState *s = opaque; 11159a16c595SGerd Hoffmann uhci_process_frame(s); 11169a16c595SGerd Hoffmann } 11179a16c595SGerd Hoffmann 1118f1ae32a1SGerd Hoffmann static void uhci_frame_timer(void *opaque) 1119f1ae32a1SGerd Hoffmann { 1120f1ae32a1SGerd Hoffmann UHCIState *s = opaque; 1121f8f48b69SHans de Goede uint64_t t_now, t_last_run; 1122f8f48b69SHans de Goede int i, frames; 1123f8f48b69SHans de Goede const uint64_t frame_t = get_ticks_per_sec() / FRAME_TIMER_FREQ; 1124f1ae32a1SGerd Hoffmann 112588793816SHans de Goede s->completions_only = false; 11269a16c595SGerd Hoffmann qemu_bh_cancel(s->bh); 1127f1ae32a1SGerd Hoffmann 1128f1ae32a1SGerd Hoffmann if (!(s->cmd & UHCI_CMD_RS)) { 1129f1ae32a1SGerd Hoffmann /* Full stop */ 113050dcc0f8SGerd Hoffmann trace_usb_uhci_schedule_stop(); 1131bc72ad67SAlex Bligh timer_del(s->frame_timer); 1132d9a528dbSGerd Hoffmann uhci_async_cancel_all(s); 1133f1ae32a1SGerd Hoffmann /* set hchalted bit in status - UHCI11D 2.1.2 */ 1134f1ae32a1SGerd Hoffmann s->status |= UHCI_STS_HCHALTED; 1135f1ae32a1SGerd Hoffmann return; 1136f1ae32a1SGerd Hoffmann } 1137f1ae32a1SGerd Hoffmann 1138f8f48b69SHans de Goede /* We still store expire_time in our state, for migration */ 1139f8f48b69SHans de Goede t_last_run = s->expire_time - frame_t; 1140bc72ad67SAlex Bligh t_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1141f8f48b69SHans de Goede 1142f8f48b69SHans de Goede /* Process up to MAX_FRAMES_PER_TICK frames */ 1143f8f48b69SHans de Goede frames = (t_now - t_last_run) / frame_t; 11449fdf7027SHans de Goede if (frames > s->maxframes) { 11459fdf7027SHans de Goede int skipped = frames - s->maxframes; 11469fdf7027SHans de Goede s->expire_time += skipped * frame_t; 11479fdf7027SHans de Goede s->frnum = (s->frnum + skipped) & 0x7ff; 11489fdf7027SHans de Goede frames -= skipped; 11499fdf7027SHans de Goede } 1150f8f48b69SHans de Goede if (frames > MAX_FRAMES_PER_TICK) { 1151f8f48b69SHans de Goede frames = MAX_FRAMES_PER_TICK; 1152f8f48b69SHans de Goede } 1153f8f48b69SHans de Goede 1154f8f48b69SHans de Goede for (i = 0; i < frames; i++) { 1155f8f48b69SHans de Goede s->frame_bytes = 0; 115650dcc0f8SGerd Hoffmann trace_usb_uhci_frame_start(s->frnum); 1157f1ae32a1SGerd Hoffmann uhci_async_validate_begin(s); 1158f1ae32a1SGerd Hoffmann uhci_process_frame(s); 1159f1ae32a1SGerd Hoffmann uhci_async_validate_end(s); 1160f8f48b69SHans de Goede /* The spec says frnum is the frame currently being processed, and 1161f8f48b69SHans de Goede * the guest must look at frnum - 1 on interrupt, so inc frnum now */ 1162719c130dSHans de Goede s->frnum = (s->frnum + 1) & 0x7ff; 1163f8f48b69SHans de Goede s->expire_time += frame_t; 1164f8f48b69SHans de Goede } 1165719c130dSHans de Goede 1166f8f48b69SHans de Goede /* Complete the previous frame(s) */ 1167719c130dSHans de Goede if (s->pending_int_mask) { 1168719c130dSHans de Goede s->status2 |= s->pending_int_mask; 1169719c130dSHans de Goede s->status |= UHCI_STS_USBINT; 1170719c130dSHans de Goede uhci_update_irq(s); 1171719c130dSHans de Goede } 1172719c130dSHans de Goede s->pending_int_mask = 0; 1173719c130dSHans de Goede 1174bc72ad67SAlex Bligh timer_mod(s->frame_timer, t_now + frame_t); 1175f1ae32a1SGerd Hoffmann } 1176f1ae32a1SGerd Hoffmann 1177f1ae32a1SGerd Hoffmann static const MemoryRegionOps uhci_ioport_ops = { 117889eb147cSGerd Hoffmann .read = uhci_port_read, 117989eb147cSGerd Hoffmann .write = uhci_port_write, 118089eb147cSGerd Hoffmann .valid.min_access_size = 1, 118189eb147cSGerd Hoffmann .valid.max_access_size = 4, 118289eb147cSGerd Hoffmann .impl.min_access_size = 2, 118389eb147cSGerd Hoffmann .impl.max_access_size = 2, 118489eb147cSGerd Hoffmann .endianness = DEVICE_LITTLE_ENDIAN, 1185f1ae32a1SGerd Hoffmann }; 1186f1ae32a1SGerd Hoffmann 1187f1ae32a1SGerd Hoffmann static USBPortOps uhci_port_ops = { 1188f1ae32a1SGerd Hoffmann .attach = uhci_attach, 1189f1ae32a1SGerd Hoffmann .detach = uhci_detach, 1190f1ae32a1SGerd Hoffmann .child_detach = uhci_child_detach, 1191f1ae32a1SGerd Hoffmann .wakeup = uhci_wakeup, 1192f1ae32a1SGerd Hoffmann .complete = uhci_async_complete, 1193f1ae32a1SGerd Hoffmann }; 1194f1ae32a1SGerd Hoffmann 1195f1ae32a1SGerd Hoffmann static USBBusOps uhci_bus_ops = { 1196f1ae32a1SGerd Hoffmann }; 1197f1ae32a1SGerd Hoffmann 119863216dc7SMarkus Armbruster static void usb_uhci_common_realize(PCIDevice *dev, Error **errp) 1199f1ae32a1SGerd Hoffmann { 1200f4bbaaf5SMarkus Armbruster Error *err = NULL; 1201973002c1SGerd Hoffmann PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 12028f3f90b0SGerd Hoffmann UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class); 120349184b62SGonglei UHCIState *s = UHCI(dev); 1204f1ae32a1SGerd Hoffmann uint8_t *pci_conf = s->dev.config; 1205f1ae32a1SGerd Hoffmann int i; 1206f1ae32a1SGerd Hoffmann 1207f1ae32a1SGerd Hoffmann pci_conf[PCI_CLASS_PROG] = 0x00; 1208f1ae32a1SGerd Hoffmann /* TODO: reset value should be 0. */ 1209f1ae32a1SGerd Hoffmann pci_conf[USB_SBRN] = USB_RELEASE_1; // release number 1210f1ae32a1SGerd Hoffmann 12119e64f8a3SMarcel Apfelbaum pci_config_set_interrupt_pin(pci_conf, u->info.irq_pin + 1); 1212973002c1SGerd Hoffmann 1213f1ae32a1SGerd Hoffmann if (s->masterbus) { 1214f1ae32a1SGerd Hoffmann USBPort *ports[NB_PORTS]; 1215f1ae32a1SGerd Hoffmann for(i = 0; i < NB_PORTS; i++) { 1216f1ae32a1SGerd Hoffmann ports[i] = &s->ports[i].port; 1217f1ae32a1SGerd Hoffmann } 1218f4bbaaf5SMarkus Armbruster usb_register_companion(s->masterbus, ports, NB_PORTS, 1219f1ae32a1SGerd Hoffmann s->firstport, s, &uhci_port_ops, 1220f4bbaaf5SMarkus Armbruster USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL, 1221f4bbaaf5SMarkus Armbruster &err); 1222f4bbaaf5SMarkus Armbruster if (err) { 122363216dc7SMarkus Armbruster error_propagate(errp, err); 122463216dc7SMarkus Armbruster return; 1225f1ae32a1SGerd Hoffmann } 1226f1ae32a1SGerd Hoffmann } else { 1227c889b3a5SAndreas Färber usb_bus_new(&s->bus, sizeof(s->bus), &uhci_bus_ops, DEVICE(dev)); 1228f1ae32a1SGerd Hoffmann for (i = 0; i < NB_PORTS; i++) { 1229f1ae32a1SGerd Hoffmann usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops, 1230f1ae32a1SGerd Hoffmann USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL); 1231f1ae32a1SGerd Hoffmann } 1232f1ae32a1SGerd Hoffmann } 12339a16c595SGerd Hoffmann s->bh = qemu_bh_new(uhci_bh, s); 1234bc72ad67SAlex Bligh s->frame_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, uhci_frame_timer, s); 1235f1ae32a1SGerd Hoffmann s->num_ports_vmstate = NB_PORTS; 1236f1ae32a1SGerd Hoffmann QTAILQ_INIT(&s->queues); 1237f1ae32a1SGerd Hoffmann 123822fc860bSPaolo Bonzini memory_region_init_io(&s->io_bar, OBJECT(s), &uhci_ioport_ops, s, 123922fc860bSPaolo Bonzini "uhci", 0x20); 124022fc860bSPaolo Bonzini 1241f1ae32a1SGerd Hoffmann /* Use region 4 for consistency with real hardware. BSD guests seem 1242f1ae32a1SGerd Hoffmann to rely on this. */ 1243f1ae32a1SGerd Hoffmann pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar); 1244f1ae32a1SGerd Hoffmann } 1245f1ae32a1SGerd Hoffmann 124663216dc7SMarkus Armbruster static void usb_uhci_vt82c686b_realize(PCIDevice *dev, Error **errp) 1247f1ae32a1SGerd Hoffmann { 124849184b62SGonglei UHCIState *s = UHCI(dev); 1249f1ae32a1SGerd Hoffmann uint8_t *pci_conf = s->dev.config; 1250f1ae32a1SGerd Hoffmann 1251f1ae32a1SGerd Hoffmann /* USB misc control 1/2 */ 1252f1ae32a1SGerd Hoffmann pci_set_long(pci_conf + 0x40,0x00001000); 1253f1ae32a1SGerd Hoffmann /* PM capability */ 1254f1ae32a1SGerd Hoffmann pci_set_long(pci_conf + 0x80,0x00020001); 1255f1ae32a1SGerd Hoffmann /* USB legacy support */ 1256f1ae32a1SGerd Hoffmann pci_set_long(pci_conf + 0xc0,0x00002000); 1257f1ae32a1SGerd Hoffmann 125863216dc7SMarkus Armbruster usb_uhci_common_realize(dev, errp); 1259f1ae32a1SGerd Hoffmann } 1260f1ae32a1SGerd Hoffmann 12613a3464b0SGonglei static void usb_uhci_exit(PCIDevice *dev) 12623a3464b0SGonglei { 126349184b62SGonglei UHCIState *s = UHCI(dev); 12643a3464b0SGonglei 1265d733f74cSGonglei trace_usb_uhci_exit(); 1266d733f74cSGonglei 12673a3464b0SGonglei if (s->frame_timer) { 12683a3464b0SGonglei timer_del(s->frame_timer); 12693a3464b0SGonglei timer_free(s->frame_timer); 12703a3464b0SGonglei s->frame_timer = NULL; 12713a3464b0SGonglei } 12723a3464b0SGonglei 12733a3464b0SGonglei if (s->bh) { 12743a3464b0SGonglei qemu_bh_delete(s->bh); 12753a3464b0SGonglei } 12763a3464b0SGonglei 12773a3464b0SGonglei uhci_async_cancel_all(s); 12783a3464b0SGonglei 12793a3464b0SGonglei if (!s->masterbus) { 12803a3464b0SGonglei usb_bus_release(&s->bus); 12813a3464b0SGonglei } 12823a3464b0SGonglei } 12833a3464b0SGonglei 1284638ca939SGerd Hoffmann static Property uhci_properties_companion[] = { 1285f1ae32a1SGerd Hoffmann DEFINE_PROP_STRING("masterbus", UHCIState, masterbus), 1286f1ae32a1SGerd Hoffmann DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0), 128740141d12SGerd Hoffmann DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280), 12889fdf7027SHans de Goede DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128), 1289f1ae32a1SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 1290f1ae32a1SGerd Hoffmann }; 1291638ca939SGerd Hoffmann static Property uhci_properties_standalone[] = { 1292638ca939SGerd Hoffmann DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280), 1293638ca939SGerd Hoffmann DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128), 1294638ca939SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 1295638ca939SGerd Hoffmann }; 1296f1ae32a1SGerd Hoffmann 12972c2e8525SGerd Hoffmann static void uhci_class_init(ObjectClass *klass, void *data) 1298f1ae32a1SGerd Hoffmann { 1299f1ae32a1SGerd Hoffmann DeviceClass *dc = DEVICE_CLASS(klass); 1300f1ae32a1SGerd Hoffmann PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 130149184b62SGonglei 130249184b62SGonglei k->class_id = PCI_CLASS_SERIAL_USB; 130349184b62SGonglei dc->vmsd = &vmstate_uhci; 130449184b62SGonglei dc->reset = uhci_reset; 130549184b62SGonglei set_bit(DEVICE_CATEGORY_USB, dc->categories); 130649184b62SGonglei } 130749184b62SGonglei 130849184b62SGonglei static const TypeInfo uhci_pci_type_info = { 130949184b62SGonglei .name = TYPE_UHCI, 131049184b62SGonglei .parent = TYPE_PCI_DEVICE, 131149184b62SGonglei .instance_size = sizeof(UHCIState), 131249184b62SGonglei .class_size = sizeof(UHCIPCIDeviceClass), 131349184b62SGonglei .abstract = true, 131449184b62SGonglei .class_init = uhci_class_init, 131549184b62SGonglei }; 131649184b62SGonglei 131749184b62SGonglei static void uhci_data_class_init(ObjectClass *klass, void *data) 131849184b62SGonglei { 131949184b62SGonglei PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 132049184b62SGonglei DeviceClass *dc = DEVICE_CLASS(klass); 13218f3f90b0SGerd Hoffmann UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class); 13222c2e8525SGerd Hoffmann UHCIInfo *info = data; 1323f1ae32a1SGerd Hoffmann 132463216dc7SMarkus Armbruster k->realize = info->realize ? info->realize : usb_uhci_common_realize; 13253a3464b0SGonglei k->exit = info->unplug ? usb_uhci_exit : NULL; 13262c2e8525SGerd Hoffmann k->vendor_id = info->vendor_id; 13272c2e8525SGerd Hoffmann k->device_id = info->device_id; 13282c2e8525SGerd Hoffmann k->revision = info->revision; 1329638ca939SGerd Hoffmann if (!info->unplug) { 1330638ca939SGerd Hoffmann /* uhci controllers in companion setups can't be hotplugged */ 1331638ca939SGerd Hoffmann dc->hotpluggable = false; 1332638ca939SGerd Hoffmann dc->props = uhci_properties_companion; 1333638ca939SGerd Hoffmann } else { 1334638ca939SGerd Hoffmann dc->props = uhci_properties_standalone; 1335638ca939SGerd Hoffmann } 13368f3f90b0SGerd Hoffmann u->info = *info; 1337f1ae32a1SGerd Hoffmann } 1338f1ae32a1SGerd Hoffmann 13392c2e8525SGerd Hoffmann static UHCIInfo uhci_info[] = { 13402c2e8525SGerd Hoffmann { 1341f1ae32a1SGerd Hoffmann .name = "piix3-usb-uhci", 13422c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13432c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82371SB_2, 13442c2e8525SGerd Hoffmann .revision = 0x01, 13458f3f90b0SGerd Hoffmann .irq_pin = 3, 13462c2e8525SGerd Hoffmann .unplug = true, 13472c2e8525SGerd Hoffmann },{ 1348f1ae32a1SGerd Hoffmann .name = "piix4-usb-uhci", 13492c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13502c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82371AB_2, 13512c2e8525SGerd Hoffmann .revision = 0x01, 13528f3f90b0SGerd Hoffmann .irq_pin = 3, 13532c2e8525SGerd Hoffmann .unplug = true, 13542c2e8525SGerd Hoffmann },{ 1355f1ae32a1SGerd Hoffmann .name = "vt82c686b-usb-uhci", 13562c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_VIA, 13572c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_VIA_UHCI, 13582c2e8525SGerd Hoffmann .revision = 0x01, 13598f3f90b0SGerd Hoffmann .irq_pin = 3, 136063216dc7SMarkus Armbruster .realize = usb_uhci_vt82c686b_realize, 13612c2e8525SGerd Hoffmann .unplug = true, 13622c2e8525SGerd Hoffmann },{ 136374625ea2SGerd Hoffmann .name = "ich9-usb-uhci1", /* 00:1d.0 */ 13642c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13652c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1, 13662c2e8525SGerd Hoffmann .revision = 0x03, 13678f3f90b0SGerd Hoffmann .irq_pin = 0, 13682c2e8525SGerd Hoffmann .unplug = false, 13692c2e8525SGerd Hoffmann },{ 137074625ea2SGerd Hoffmann .name = "ich9-usb-uhci2", /* 00:1d.1 */ 13712c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13722c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2, 13732c2e8525SGerd Hoffmann .revision = 0x03, 13748f3f90b0SGerd Hoffmann .irq_pin = 1, 13752c2e8525SGerd Hoffmann .unplug = false, 13762c2e8525SGerd Hoffmann },{ 137774625ea2SGerd Hoffmann .name = "ich9-usb-uhci3", /* 00:1d.2 */ 13782c2e8525SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 13792c2e8525SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3, 13802c2e8525SGerd Hoffmann .revision = 0x03, 13818f3f90b0SGerd Hoffmann .irq_pin = 2, 13822c2e8525SGerd Hoffmann .unplug = false, 138374625ea2SGerd Hoffmann },{ 138474625ea2SGerd Hoffmann .name = "ich9-usb-uhci4", /* 00:1a.0 */ 138574625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 138674625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4, 138774625ea2SGerd Hoffmann .revision = 0x03, 138874625ea2SGerd Hoffmann .irq_pin = 0, 138974625ea2SGerd Hoffmann .unplug = false, 139074625ea2SGerd Hoffmann },{ 139174625ea2SGerd Hoffmann .name = "ich9-usb-uhci5", /* 00:1a.1 */ 139274625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 139374625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5, 139474625ea2SGerd Hoffmann .revision = 0x03, 139574625ea2SGerd Hoffmann .irq_pin = 1, 139674625ea2SGerd Hoffmann .unplug = false, 139774625ea2SGerd Hoffmann },{ 139874625ea2SGerd Hoffmann .name = "ich9-usb-uhci6", /* 00:1a.2 */ 139974625ea2SGerd Hoffmann .vendor_id = PCI_VENDOR_ID_INTEL, 140074625ea2SGerd Hoffmann .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6, 140174625ea2SGerd Hoffmann .revision = 0x03, 140274625ea2SGerd Hoffmann .irq_pin = 2, 140374625ea2SGerd Hoffmann .unplug = false, 14042c2e8525SGerd Hoffmann } 1405f1ae32a1SGerd Hoffmann }; 1406f1ae32a1SGerd Hoffmann 1407f1ae32a1SGerd Hoffmann static void uhci_register_types(void) 1408f1ae32a1SGerd Hoffmann { 14092c2e8525SGerd Hoffmann TypeInfo uhci_type_info = { 141049184b62SGonglei .parent = TYPE_UHCI, 141149184b62SGonglei .class_init = uhci_data_class_init, 14122c2e8525SGerd Hoffmann }; 14132c2e8525SGerd Hoffmann int i; 14142c2e8525SGerd Hoffmann 141549184b62SGonglei type_register_static(&uhci_pci_type_info); 141649184b62SGonglei 14172c2e8525SGerd Hoffmann for (i = 0; i < ARRAY_SIZE(uhci_info); i++) { 14182c2e8525SGerd Hoffmann uhci_type_info.name = uhci_info[i].name; 14192c2e8525SGerd Hoffmann uhci_type_info.class_data = uhci_info + i; 14202c2e8525SGerd Hoffmann type_register(&uhci_type_info); 14212c2e8525SGerd Hoffmann } 1422f1ae32a1SGerd Hoffmann } 1423f1ae32a1SGerd Hoffmann 1424f1ae32a1SGerd Hoffmann type_init(uhci_register_types) 1425