xref: /openbmc/qemu/hw/usb/hcd-uhci.c (revision 2c2e852509de3fecf606e8f2dcbca0ebd1eddc10)
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"
30f1ae32a1SGerd Hoffmann #include "hw/pci.h"
31f1ae32a1SGerd Hoffmann #include "qemu-timer.h"
32f1ae32a1SGerd Hoffmann #include "iov.h"
33f1ae32a1SGerd Hoffmann #include "dma.h"
3450dcc0f8SGerd Hoffmann #include "trace.h"
35f1ae32a1SGerd Hoffmann 
36f1ae32a1SGerd Hoffmann //#define DEBUG
37f1ae32a1SGerd Hoffmann //#define DEBUG_DUMP_DATA
38f1ae32a1SGerd Hoffmann 
39f1ae32a1SGerd Hoffmann #define UHCI_CMD_FGR      (1 << 4)
40f1ae32a1SGerd Hoffmann #define UHCI_CMD_EGSM     (1 << 3)
41f1ae32a1SGerd Hoffmann #define UHCI_CMD_GRESET   (1 << 2)
42f1ae32a1SGerd Hoffmann #define UHCI_CMD_HCRESET  (1 << 1)
43f1ae32a1SGerd Hoffmann #define UHCI_CMD_RS       (1 << 0)
44f1ae32a1SGerd Hoffmann 
45f1ae32a1SGerd Hoffmann #define UHCI_STS_HCHALTED (1 << 5)
46f1ae32a1SGerd Hoffmann #define UHCI_STS_HCPERR   (1 << 4)
47f1ae32a1SGerd Hoffmann #define UHCI_STS_HSERR    (1 << 3)
48f1ae32a1SGerd Hoffmann #define UHCI_STS_RD       (1 << 2)
49f1ae32a1SGerd Hoffmann #define UHCI_STS_USBERR   (1 << 1)
50f1ae32a1SGerd Hoffmann #define UHCI_STS_USBINT   (1 << 0)
51f1ae32a1SGerd Hoffmann 
52f1ae32a1SGerd Hoffmann #define TD_CTRL_SPD     (1 << 29)
53f1ae32a1SGerd Hoffmann #define TD_CTRL_ERROR_SHIFT  27
54f1ae32a1SGerd Hoffmann #define TD_CTRL_IOS     (1 << 25)
55f1ae32a1SGerd Hoffmann #define TD_CTRL_IOC     (1 << 24)
56f1ae32a1SGerd Hoffmann #define TD_CTRL_ACTIVE  (1 << 23)
57f1ae32a1SGerd Hoffmann #define TD_CTRL_STALL   (1 << 22)
58f1ae32a1SGerd Hoffmann #define TD_CTRL_BABBLE  (1 << 20)
59f1ae32a1SGerd Hoffmann #define TD_CTRL_NAK     (1 << 19)
60f1ae32a1SGerd Hoffmann #define TD_CTRL_TIMEOUT (1 << 18)
61f1ae32a1SGerd Hoffmann 
62f1ae32a1SGerd Hoffmann #define UHCI_PORT_SUSPEND (1 << 12)
63f1ae32a1SGerd Hoffmann #define UHCI_PORT_RESET (1 << 9)
64f1ae32a1SGerd Hoffmann #define UHCI_PORT_LSDA  (1 << 8)
65f1ae32a1SGerd Hoffmann #define UHCI_PORT_RD    (1 << 6)
66f1ae32a1SGerd Hoffmann #define UHCI_PORT_ENC   (1 << 3)
67f1ae32a1SGerd Hoffmann #define UHCI_PORT_EN    (1 << 2)
68f1ae32a1SGerd Hoffmann #define UHCI_PORT_CSC   (1 << 1)
69f1ae32a1SGerd Hoffmann #define UHCI_PORT_CCS   (1 << 0)
70f1ae32a1SGerd Hoffmann 
71f1ae32a1SGerd Hoffmann #define UHCI_PORT_READ_ONLY    (0x1bb)
72f1ae32a1SGerd Hoffmann #define UHCI_PORT_WRITE_CLEAR  (UHCI_PORT_CSC | UHCI_PORT_ENC)
73f1ae32a1SGerd Hoffmann 
74f1ae32a1SGerd Hoffmann #define FRAME_TIMER_FREQ 1000
75f1ae32a1SGerd Hoffmann 
76f1ae32a1SGerd Hoffmann #define FRAME_MAX_LOOPS  256
77f1ae32a1SGerd Hoffmann 
78f1ae32a1SGerd Hoffmann #define NB_PORTS 2
79f1ae32a1SGerd Hoffmann 
8060e1b2a6SGerd Hoffmann enum {
810cd178caSGerd Hoffmann     TD_RESULT_STOP_FRAME = 10,
820cd178caSGerd Hoffmann     TD_RESULT_COMPLETE,
830cd178caSGerd Hoffmann     TD_RESULT_NEXT_QH,
844efe4ef3SGerd Hoffmann     TD_RESULT_ASYNC_START,
854efe4ef3SGerd Hoffmann     TD_RESULT_ASYNC_CONT,
8660e1b2a6SGerd Hoffmann };
8760e1b2a6SGerd Hoffmann 
88f1ae32a1SGerd Hoffmann typedef struct UHCIState UHCIState;
89f1ae32a1SGerd Hoffmann typedef struct UHCIAsync UHCIAsync;
90f1ae32a1SGerd Hoffmann typedef struct UHCIQueue UHCIQueue;
91*2c2e8525SGerd Hoffmann typedef struct UHCIInfo UHCIInfo;
92*2c2e8525SGerd Hoffmann 
93*2c2e8525SGerd Hoffmann struct UHCIInfo {
94*2c2e8525SGerd Hoffmann     const char *name;
95*2c2e8525SGerd Hoffmann     uint16_t   vendor_id;
96*2c2e8525SGerd Hoffmann     uint16_t   device_id;
97*2c2e8525SGerd Hoffmann     uint8_t    revision;
98*2c2e8525SGerd Hoffmann     int        (*initfn)(PCIDevice *dev);
99*2c2e8525SGerd Hoffmann     bool       unplug;
100*2c2e8525SGerd Hoffmann };
101f1ae32a1SGerd Hoffmann 
102f1ae32a1SGerd Hoffmann /*
103f1ae32a1SGerd Hoffmann  * Pending async transaction.
104f1ae32a1SGerd Hoffmann  * 'packet' must be the first field because completion
105f1ae32a1SGerd Hoffmann  * handler does "(UHCIAsync *) pkt" cast.
106f1ae32a1SGerd Hoffmann  */
107f1ae32a1SGerd Hoffmann 
108f1ae32a1SGerd Hoffmann struct UHCIAsync {
109f1ae32a1SGerd Hoffmann     USBPacket packet;
110f1ae32a1SGerd Hoffmann     QEMUSGList sgl;
111f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
112f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIAsync) next;
1131f250cc7SHans de Goede     uint32_t  td_addr;
114f1ae32a1SGerd Hoffmann     uint8_t   done;
115f1ae32a1SGerd Hoffmann };
116f1ae32a1SGerd Hoffmann 
117f1ae32a1SGerd Hoffmann struct UHCIQueue {
11866a08cbeSHans de Goede     uint32_t  qh_addr;
119f1ae32a1SGerd Hoffmann     uint32_t  token;
120f1ae32a1SGerd Hoffmann     UHCIState *uhci;
12111d15e40SHans de Goede     USBEndpoint *ep;
122f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIQueue) next;
1238928c9c4SHans de Goede     QTAILQ_HEAD(asyncs_head, UHCIAsync) asyncs;
124f1ae32a1SGerd Hoffmann     int8_t    valid;
125f1ae32a1SGerd Hoffmann };
126f1ae32a1SGerd Hoffmann 
127f1ae32a1SGerd Hoffmann typedef struct UHCIPort {
128f1ae32a1SGerd Hoffmann     USBPort port;
129f1ae32a1SGerd Hoffmann     uint16_t ctrl;
130f1ae32a1SGerd Hoffmann } UHCIPort;
131f1ae32a1SGerd Hoffmann 
132f1ae32a1SGerd Hoffmann struct UHCIState {
133f1ae32a1SGerd Hoffmann     PCIDevice dev;
134f1ae32a1SGerd Hoffmann     MemoryRegion io_bar;
135f1ae32a1SGerd Hoffmann     USBBus bus; /* Note unused when we're a companion controller */
136f1ae32a1SGerd Hoffmann     uint16_t cmd; /* cmd register */
137f1ae32a1SGerd Hoffmann     uint16_t status;
138f1ae32a1SGerd Hoffmann     uint16_t intr; /* interrupt enable register */
139f1ae32a1SGerd Hoffmann     uint16_t frnum; /* frame number */
140f1ae32a1SGerd Hoffmann     uint32_t fl_base_addr; /* frame list base address */
141f1ae32a1SGerd Hoffmann     uint8_t sof_timing;
142f1ae32a1SGerd Hoffmann     uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
143f1ae32a1SGerd Hoffmann     int64_t expire_time;
144f1ae32a1SGerd Hoffmann     QEMUTimer *frame_timer;
1459a16c595SGerd Hoffmann     QEMUBH *bh;
1464aed20e2SGerd Hoffmann     uint32_t frame_bytes;
14740141d12SGerd Hoffmann     uint32_t frame_bandwidth;
148f1ae32a1SGerd Hoffmann     UHCIPort ports[NB_PORTS];
149f1ae32a1SGerd Hoffmann 
150f1ae32a1SGerd Hoffmann     /* Interrupts that should be raised at the end of the current frame.  */
151f1ae32a1SGerd Hoffmann     uint32_t pending_int_mask;
152973002c1SGerd Hoffmann     int irq_pin;
153f1ae32a1SGerd Hoffmann 
154f1ae32a1SGerd Hoffmann     /* Active packets */
155f1ae32a1SGerd Hoffmann     QTAILQ_HEAD(, UHCIQueue) queues;
156f1ae32a1SGerd Hoffmann     uint8_t num_ports_vmstate;
157f1ae32a1SGerd Hoffmann 
158f1ae32a1SGerd Hoffmann     /* Properties */
159f1ae32a1SGerd Hoffmann     char *masterbus;
160f1ae32a1SGerd Hoffmann     uint32_t firstport;
161f1ae32a1SGerd Hoffmann };
162f1ae32a1SGerd Hoffmann 
163f1ae32a1SGerd Hoffmann typedef struct UHCI_TD {
164f1ae32a1SGerd Hoffmann     uint32_t link;
165f1ae32a1SGerd Hoffmann     uint32_t ctrl; /* see TD_CTRL_xxx */
166f1ae32a1SGerd Hoffmann     uint32_t token;
167f1ae32a1SGerd Hoffmann     uint32_t buffer;
168f1ae32a1SGerd Hoffmann } UHCI_TD;
169f1ae32a1SGerd Hoffmann 
170f1ae32a1SGerd Hoffmann typedef struct UHCI_QH {
171f1ae32a1SGerd Hoffmann     uint32_t link;
172f1ae32a1SGerd Hoffmann     uint32_t el_link;
173f1ae32a1SGerd Hoffmann } UHCI_QH;
174f1ae32a1SGerd Hoffmann 
17540507377SHans de Goede static void uhci_async_cancel(UHCIAsync *async);
17611d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
17740507377SHans de Goede 
178f1ae32a1SGerd Hoffmann static inline int32_t uhci_queue_token(UHCI_TD *td)
179f1ae32a1SGerd Hoffmann {
1806fe30910SHans de Goede     if ((td->token & (0xf << 15)) == 0) {
1816fe30910SHans de Goede         /* ctrl ep, cover ep and dev, not pid! */
1826fe30910SHans de Goede         return td->token & 0x7ff00;
1836fe30910SHans de Goede     } else {
184f1ae32a1SGerd Hoffmann         /* covers ep, dev, pid -> identifies the endpoint */
185f1ae32a1SGerd Hoffmann         return td->token & 0x7ffff;
186f1ae32a1SGerd Hoffmann     }
1876fe30910SHans de Goede }
188f1ae32a1SGerd Hoffmann 
18966a08cbeSHans de Goede static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td,
19066a08cbeSHans de Goede                                  USBEndpoint *ep)
191f1ae32a1SGerd Hoffmann {
192f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
193f1ae32a1SGerd Hoffmann 
194f1ae32a1SGerd Hoffmann     queue = g_new0(UHCIQueue, 1);
195f1ae32a1SGerd Hoffmann     queue->uhci = s;
19666a08cbeSHans de Goede     queue->qh_addr = qh_addr;
19766a08cbeSHans de Goede     queue->token = uhci_queue_token(td);
19811d15e40SHans de Goede     queue->ep = ep;
199f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&queue->asyncs);
200f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_HEAD(&s->queues, queue, next);
2013905097eSHans de Goede     /* valid needs to be large enough to handle 10 frame delay
2023905097eSHans de Goede      * for initial isochronous requests */
2033905097eSHans de Goede     queue->valid = 32;
20450dcc0f8SGerd Hoffmann     trace_usb_uhci_queue_add(queue->token);
205f1ae32a1SGerd Hoffmann     return queue;
206f1ae32a1SGerd Hoffmann }
207f1ae32a1SGerd Hoffmann 
20866a08cbeSHans de Goede static void uhci_queue_free(UHCIQueue *queue, const char *reason)
209f1ae32a1SGerd Hoffmann {
210f1ae32a1SGerd Hoffmann     UHCIState *s = queue->uhci;
21140507377SHans de Goede     UHCIAsync *async;
21240507377SHans de Goede 
21340507377SHans de Goede     while (!QTAILQ_EMPTY(&queue->asyncs)) {
21440507377SHans de Goede         async = QTAILQ_FIRST(&queue->asyncs);
21540507377SHans de Goede         uhci_async_cancel(async);
21640507377SHans de Goede     }
217f1ae32a1SGerd Hoffmann 
21866a08cbeSHans de Goede     trace_usb_uhci_queue_del(queue->token, reason);
219f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&s->queues, queue, next);
220f1ae32a1SGerd Hoffmann     g_free(queue);
221f1ae32a1SGerd Hoffmann }
222f1ae32a1SGerd Hoffmann 
22366a08cbeSHans de Goede static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td)
22466a08cbeSHans de Goede {
22566a08cbeSHans de Goede     uint32_t token = uhci_queue_token(td);
22666a08cbeSHans de Goede     UHCIQueue *queue;
22766a08cbeSHans de Goede 
22866a08cbeSHans de Goede     QTAILQ_FOREACH(queue, &s->queues, next) {
22966a08cbeSHans de Goede         if (queue->token == token) {
23066a08cbeSHans de Goede             return queue;
23166a08cbeSHans de Goede         }
23266a08cbeSHans de Goede     }
23366a08cbeSHans de Goede     return NULL;
23466a08cbeSHans de Goede }
23566a08cbeSHans de Goede 
23666a08cbeSHans de Goede static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td,
23766a08cbeSHans de Goede                               uint32_t td_addr, bool queuing)
23866a08cbeSHans de Goede {
23966a08cbeSHans de Goede     UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs);
24066a08cbeSHans de Goede 
24166a08cbeSHans de Goede     return queue->qh_addr == qh_addr &&
24266a08cbeSHans de Goede            queue->token == uhci_queue_token(td) &&
24366a08cbeSHans de Goede            (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL ||
24466a08cbeSHans de Goede             first->td_addr == td_addr);
24566a08cbeSHans de Goede }
24666a08cbeSHans de Goede 
2471f250cc7SHans de Goede static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
248f1ae32a1SGerd Hoffmann {
249f1ae32a1SGerd Hoffmann     UHCIAsync *async = g_new0(UHCIAsync, 1);
250f1ae32a1SGerd Hoffmann 
251f1ae32a1SGerd Hoffmann     async->queue = queue;
2521f250cc7SHans de Goede     async->td_addr = td_addr;
253f1ae32a1SGerd Hoffmann     usb_packet_init(&async->packet);
254f1ae32a1SGerd Hoffmann     pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
2551f250cc7SHans de Goede     trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
256f1ae32a1SGerd Hoffmann 
257f1ae32a1SGerd Hoffmann     return async;
258f1ae32a1SGerd Hoffmann }
259f1ae32a1SGerd Hoffmann 
260f1ae32a1SGerd Hoffmann static void uhci_async_free(UHCIAsync *async)
261f1ae32a1SGerd Hoffmann {
2621f250cc7SHans de Goede     trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
263f1ae32a1SGerd Hoffmann     usb_packet_cleanup(&async->packet);
264f1ae32a1SGerd Hoffmann     qemu_sglist_destroy(&async->sgl);
265f1ae32a1SGerd Hoffmann     g_free(async);
266f1ae32a1SGerd Hoffmann }
267f1ae32a1SGerd Hoffmann 
268f1ae32a1SGerd Hoffmann static void uhci_async_link(UHCIAsync *async)
269f1ae32a1SGerd Hoffmann {
270f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
271f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
2721f250cc7SHans de Goede     trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
273f1ae32a1SGerd Hoffmann }
274f1ae32a1SGerd Hoffmann 
275f1ae32a1SGerd Hoffmann static void uhci_async_unlink(UHCIAsync *async)
276f1ae32a1SGerd Hoffmann {
277f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
278f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&queue->asyncs, async, next);
2791f250cc7SHans de Goede     trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
280f1ae32a1SGerd Hoffmann }
281f1ae32a1SGerd Hoffmann 
282f1ae32a1SGerd Hoffmann static void uhci_async_cancel(UHCIAsync *async)
283f1ae32a1SGerd Hoffmann {
2842f2ee268SHans de Goede     uhci_async_unlink(async);
2851f250cc7SHans de Goede     trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
2861f250cc7SHans de Goede                                  async->done);
287f1ae32a1SGerd Hoffmann     if (!async->done)
288f1ae32a1SGerd Hoffmann         usb_cancel_packet(&async->packet);
28900a0770dSHans de Goede     usb_packet_unmap(&async->packet, &async->sgl);
290f1ae32a1SGerd Hoffmann     uhci_async_free(async);
291f1ae32a1SGerd Hoffmann }
292f1ae32a1SGerd Hoffmann 
293f1ae32a1SGerd Hoffmann /*
294f1ae32a1SGerd Hoffmann  * Mark all outstanding async packets as invalid.
295f1ae32a1SGerd Hoffmann  * This is used for canceling them when TDs are removed by the HCD.
296f1ae32a1SGerd Hoffmann  */
297f1ae32a1SGerd Hoffmann static void uhci_async_validate_begin(UHCIState *s)
298f1ae32a1SGerd Hoffmann {
299f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
300f1ae32a1SGerd Hoffmann 
301f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
302f1ae32a1SGerd Hoffmann         queue->valid--;
303f1ae32a1SGerd Hoffmann     }
304f1ae32a1SGerd Hoffmann }
305f1ae32a1SGerd Hoffmann 
306f1ae32a1SGerd Hoffmann /*
307f1ae32a1SGerd Hoffmann  * Cancel async packets that are no longer valid
308f1ae32a1SGerd Hoffmann  */
309f1ae32a1SGerd Hoffmann static void uhci_async_validate_end(UHCIState *s)
310f1ae32a1SGerd Hoffmann {
311f1ae32a1SGerd Hoffmann     UHCIQueue *queue, *n;
312f1ae32a1SGerd Hoffmann 
313f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
31440507377SHans de Goede         if (!queue->valid) {
31566a08cbeSHans de Goede             uhci_queue_free(queue, "validate-end");
316f1ae32a1SGerd Hoffmann         }
317f1ae32a1SGerd Hoffmann     }
31840507377SHans de Goede }
319f1ae32a1SGerd Hoffmann 
320f1ae32a1SGerd Hoffmann static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
321f1ae32a1SGerd Hoffmann {
3225ad23e87SHans de Goede     UHCIQueue *queue, *n;
323f1ae32a1SGerd Hoffmann 
3245ad23e87SHans de Goede     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
3255ad23e87SHans de Goede         if (queue->ep->dev == dev) {
3265ad23e87SHans de Goede             uhci_queue_free(queue, "cancel-device");
327f1ae32a1SGerd Hoffmann         }
328f1ae32a1SGerd Hoffmann     }
329f1ae32a1SGerd Hoffmann }
330f1ae32a1SGerd Hoffmann 
331f1ae32a1SGerd Hoffmann static void uhci_async_cancel_all(UHCIState *s)
332f1ae32a1SGerd Hoffmann {
33377fa9aeeSGerd Hoffmann     UHCIQueue *queue, *nq;
334f1ae32a1SGerd Hoffmann 
33577fa9aeeSGerd Hoffmann     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
33666a08cbeSHans de Goede         uhci_queue_free(queue, "cancel-all");
337f1ae32a1SGerd Hoffmann     }
338f1ae32a1SGerd Hoffmann }
339f1ae32a1SGerd Hoffmann 
3408c75a899SHans de Goede static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr)
341f1ae32a1SGerd Hoffmann {
342f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
343f1ae32a1SGerd Hoffmann     UHCIAsync *async;
344f1ae32a1SGerd Hoffmann 
345f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
346f1ae32a1SGerd Hoffmann         QTAILQ_FOREACH(async, &queue->asyncs, next) {
3471f250cc7SHans de Goede             if (async->td_addr == td_addr) {
348f1ae32a1SGerd Hoffmann                 return async;
349f1ae32a1SGerd Hoffmann             }
350f1ae32a1SGerd Hoffmann         }
3518c75a899SHans de Goede     }
352f1ae32a1SGerd Hoffmann     return NULL;
353f1ae32a1SGerd Hoffmann }
354f1ae32a1SGerd Hoffmann 
355f1ae32a1SGerd Hoffmann static void uhci_update_irq(UHCIState *s)
356f1ae32a1SGerd Hoffmann {
357f1ae32a1SGerd Hoffmann     int level;
358f1ae32a1SGerd Hoffmann     if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
359f1ae32a1SGerd Hoffmann         ((s->status2 & 2) && (s->intr & (1 << 3))) ||
360f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
361f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
362f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HSERR) ||
363f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HCPERR)) {
364f1ae32a1SGerd Hoffmann         level = 1;
365f1ae32a1SGerd Hoffmann     } else {
366f1ae32a1SGerd Hoffmann         level = 0;
367f1ae32a1SGerd Hoffmann     }
368973002c1SGerd Hoffmann     qemu_set_irq(s->dev.irq[s->irq_pin], level);
369f1ae32a1SGerd Hoffmann }
370f1ae32a1SGerd Hoffmann 
371f1ae32a1SGerd Hoffmann static void uhci_reset(void *opaque)
372f1ae32a1SGerd Hoffmann {
373f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
374f1ae32a1SGerd Hoffmann     uint8_t *pci_conf;
375f1ae32a1SGerd Hoffmann     int i;
376f1ae32a1SGerd Hoffmann     UHCIPort *port;
377f1ae32a1SGerd Hoffmann 
37850dcc0f8SGerd Hoffmann     trace_usb_uhci_reset();
379f1ae32a1SGerd Hoffmann 
380f1ae32a1SGerd Hoffmann     pci_conf = s->dev.config;
381f1ae32a1SGerd Hoffmann 
382f1ae32a1SGerd Hoffmann     pci_conf[0x6a] = 0x01; /* usb clock */
383f1ae32a1SGerd Hoffmann     pci_conf[0x6b] = 0x00;
384f1ae32a1SGerd Hoffmann     s->cmd = 0;
385f1ae32a1SGerd Hoffmann     s->status = 0;
386f1ae32a1SGerd Hoffmann     s->status2 = 0;
387f1ae32a1SGerd Hoffmann     s->intr = 0;
388f1ae32a1SGerd Hoffmann     s->fl_base_addr = 0;
389f1ae32a1SGerd Hoffmann     s->sof_timing = 64;
390f1ae32a1SGerd Hoffmann 
391f1ae32a1SGerd Hoffmann     for(i = 0; i < NB_PORTS; i++) {
392f1ae32a1SGerd Hoffmann         port = &s->ports[i];
393f1ae32a1SGerd Hoffmann         port->ctrl = 0x0080;
394f1ae32a1SGerd Hoffmann         if (port->port.dev && port->port.dev->attached) {
395f1ae32a1SGerd Hoffmann             usb_port_reset(&port->port);
396f1ae32a1SGerd Hoffmann         }
397f1ae32a1SGerd Hoffmann     }
398f1ae32a1SGerd Hoffmann 
399f1ae32a1SGerd Hoffmann     uhci_async_cancel_all(s);
4009a16c595SGerd Hoffmann     qemu_bh_cancel(s->bh);
401aba1f242SGerd Hoffmann     uhci_update_irq(s);
402f1ae32a1SGerd Hoffmann }
403f1ae32a1SGerd Hoffmann 
404f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci_port = {
405f1ae32a1SGerd Hoffmann     .name = "uhci port",
406f1ae32a1SGerd Hoffmann     .version_id = 1,
407f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
408f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
409f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
410f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(ctrl, UHCIPort),
411f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
412f1ae32a1SGerd Hoffmann     }
413f1ae32a1SGerd Hoffmann };
414f1ae32a1SGerd Hoffmann 
41575f151cdSGerd Hoffmann static int uhci_post_load(void *opaque, int version_id)
41675f151cdSGerd Hoffmann {
41775f151cdSGerd Hoffmann     UHCIState *s = opaque;
41875f151cdSGerd Hoffmann 
41975f151cdSGerd Hoffmann     if (version_id < 2) {
42075f151cdSGerd Hoffmann         s->expire_time = qemu_get_clock_ns(vm_clock) +
42175f151cdSGerd Hoffmann             (get_ticks_per_sec() / FRAME_TIMER_FREQ);
42275f151cdSGerd Hoffmann     }
42375f151cdSGerd Hoffmann     return 0;
42475f151cdSGerd Hoffmann }
42575f151cdSGerd Hoffmann 
426f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci = {
427f1ae32a1SGerd Hoffmann     .name = "uhci",
428f1ae32a1SGerd Hoffmann     .version_id = 2,
429f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
430f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
43175f151cdSGerd Hoffmann     .post_load = uhci_post_load,
432f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
433f1ae32a1SGerd Hoffmann         VMSTATE_PCI_DEVICE(dev, UHCIState),
434f1ae32a1SGerd Hoffmann         VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
435f1ae32a1SGerd Hoffmann         VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
436f1ae32a1SGerd Hoffmann                              vmstate_uhci_port, UHCIPort),
437f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(cmd, UHCIState),
438f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(status, UHCIState),
439f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(intr, UHCIState),
440f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(frnum, UHCIState),
441f1ae32a1SGerd Hoffmann         VMSTATE_UINT32(fl_base_addr, UHCIState),
442f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(sof_timing, UHCIState),
443f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(status2, UHCIState),
444f1ae32a1SGerd Hoffmann         VMSTATE_TIMER(frame_timer, UHCIState),
445f1ae32a1SGerd Hoffmann         VMSTATE_INT64_V(expire_time, UHCIState, 2),
446f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
447f1ae32a1SGerd Hoffmann     }
448f1ae32a1SGerd Hoffmann };
449f1ae32a1SGerd Hoffmann 
450f1ae32a1SGerd Hoffmann static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
451f1ae32a1SGerd Hoffmann {
452f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
453f1ae32a1SGerd Hoffmann 
454f1ae32a1SGerd Hoffmann     addr &= 0x1f;
455f1ae32a1SGerd Hoffmann     switch(addr) {
456f1ae32a1SGerd Hoffmann     case 0x0c:
457f1ae32a1SGerd Hoffmann         s->sof_timing = val;
458f1ae32a1SGerd Hoffmann         break;
459f1ae32a1SGerd Hoffmann     }
460f1ae32a1SGerd Hoffmann }
461f1ae32a1SGerd Hoffmann 
462f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
463f1ae32a1SGerd Hoffmann {
464f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
465f1ae32a1SGerd Hoffmann     uint32_t val;
466f1ae32a1SGerd Hoffmann 
467f1ae32a1SGerd Hoffmann     addr &= 0x1f;
468f1ae32a1SGerd Hoffmann     switch(addr) {
469f1ae32a1SGerd Hoffmann     case 0x0c:
470f1ae32a1SGerd Hoffmann         val = s->sof_timing;
471f1ae32a1SGerd Hoffmann         break;
472f1ae32a1SGerd Hoffmann     default:
473f1ae32a1SGerd Hoffmann         val = 0xff;
474f1ae32a1SGerd Hoffmann         break;
475f1ae32a1SGerd Hoffmann     }
476f1ae32a1SGerd Hoffmann     return val;
477f1ae32a1SGerd Hoffmann }
478f1ae32a1SGerd Hoffmann 
479f1ae32a1SGerd Hoffmann static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
480f1ae32a1SGerd Hoffmann {
481f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
482f1ae32a1SGerd Hoffmann 
483f1ae32a1SGerd Hoffmann     addr &= 0x1f;
48450dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_writew(addr, val);
485f1ae32a1SGerd Hoffmann 
486f1ae32a1SGerd Hoffmann     switch(addr) {
487f1ae32a1SGerd Hoffmann     case 0x00:
488f1ae32a1SGerd Hoffmann         if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
489f1ae32a1SGerd Hoffmann             /* start frame processing */
49050dcc0f8SGerd Hoffmann             trace_usb_uhci_schedule_start();
491f1ae32a1SGerd Hoffmann             s->expire_time = qemu_get_clock_ns(vm_clock) +
492f1ae32a1SGerd Hoffmann                 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
493f1ae32a1SGerd Hoffmann             qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
494f1ae32a1SGerd Hoffmann             s->status &= ~UHCI_STS_HCHALTED;
495f1ae32a1SGerd Hoffmann         } else if (!(val & UHCI_CMD_RS)) {
496f1ae32a1SGerd Hoffmann             s->status |= UHCI_STS_HCHALTED;
497f1ae32a1SGerd Hoffmann         }
498f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_GRESET) {
499f1ae32a1SGerd Hoffmann             UHCIPort *port;
500f1ae32a1SGerd Hoffmann             int i;
501f1ae32a1SGerd Hoffmann 
502f1ae32a1SGerd Hoffmann             /* send reset on the USB bus */
503f1ae32a1SGerd Hoffmann             for(i = 0; i < NB_PORTS; i++) {
504f1ae32a1SGerd Hoffmann                 port = &s->ports[i];
505f1ae32a1SGerd Hoffmann                 usb_device_reset(port->port.dev);
506f1ae32a1SGerd Hoffmann             }
507f1ae32a1SGerd Hoffmann             uhci_reset(s);
508f1ae32a1SGerd Hoffmann             return;
509f1ae32a1SGerd Hoffmann         }
510f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_HCRESET) {
511f1ae32a1SGerd Hoffmann             uhci_reset(s);
512f1ae32a1SGerd Hoffmann             return;
513f1ae32a1SGerd Hoffmann         }
514f1ae32a1SGerd Hoffmann         s->cmd = val;
515f1ae32a1SGerd Hoffmann         break;
516f1ae32a1SGerd Hoffmann     case 0x02:
517f1ae32a1SGerd Hoffmann         s->status &= ~val;
518f1ae32a1SGerd Hoffmann         /* XXX: the chip spec is not coherent, so we add a hidden
519f1ae32a1SGerd Hoffmann            register to distinguish between IOC and SPD */
520f1ae32a1SGerd Hoffmann         if (val & UHCI_STS_USBINT)
521f1ae32a1SGerd Hoffmann             s->status2 = 0;
522f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
523f1ae32a1SGerd Hoffmann         break;
524f1ae32a1SGerd Hoffmann     case 0x04:
525f1ae32a1SGerd Hoffmann         s->intr = val;
526f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
527f1ae32a1SGerd Hoffmann         break;
528f1ae32a1SGerd Hoffmann     case 0x06:
529f1ae32a1SGerd Hoffmann         if (s->status & UHCI_STS_HCHALTED)
530f1ae32a1SGerd Hoffmann             s->frnum = val & 0x7ff;
531f1ae32a1SGerd Hoffmann         break;
532f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
533f1ae32a1SGerd Hoffmann         {
534f1ae32a1SGerd Hoffmann             UHCIPort *port;
535f1ae32a1SGerd Hoffmann             USBDevice *dev;
536f1ae32a1SGerd Hoffmann             int n;
537f1ae32a1SGerd Hoffmann 
538f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
539f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
540f1ae32a1SGerd Hoffmann                 return;
541f1ae32a1SGerd Hoffmann             port = &s->ports[n];
542f1ae32a1SGerd Hoffmann             dev = port->port.dev;
543f1ae32a1SGerd Hoffmann             if (dev && dev->attached) {
544f1ae32a1SGerd Hoffmann                 /* port reset */
545f1ae32a1SGerd Hoffmann                 if ( (val & UHCI_PORT_RESET) &&
546f1ae32a1SGerd Hoffmann                      !(port->ctrl & UHCI_PORT_RESET) ) {
547f1ae32a1SGerd Hoffmann                     usb_device_reset(dev);
548f1ae32a1SGerd Hoffmann                 }
549f1ae32a1SGerd Hoffmann             }
550f1ae32a1SGerd Hoffmann             port->ctrl &= UHCI_PORT_READ_ONLY;
551f1ae32a1SGerd Hoffmann             port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
552f1ae32a1SGerd Hoffmann             /* some bits are reset when a '1' is written to them */
553f1ae32a1SGerd Hoffmann             port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
554f1ae32a1SGerd Hoffmann         }
555f1ae32a1SGerd Hoffmann         break;
556f1ae32a1SGerd Hoffmann     }
557f1ae32a1SGerd Hoffmann }
558f1ae32a1SGerd Hoffmann 
559f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
560f1ae32a1SGerd Hoffmann {
561f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
562f1ae32a1SGerd Hoffmann     uint32_t val;
563f1ae32a1SGerd Hoffmann 
564f1ae32a1SGerd Hoffmann     addr &= 0x1f;
565f1ae32a1SGerd Hoffmann     switch(addr) {
566f1ae32a1SGerd Hoffmann     case 0x00:
567f1ae32a1SGerd Hoffmann         val = s->cmd;
568f1ae32a1SGerd Hoffmann         break;
569f1ae32a1SGerd Hoffmann     case 0x02:
570f1ae32a1SGerd Hoffmann         val = s->status;
571f1ae32a1SGerd Hoffmann         break;
572f1ae32a1SGerd Hoffmann     case 0x04:
573f1ae32a1SGerd Hoffmann         val = s->intr;
574f1ae32a1SGerd Hoffmann         break;
575f1ae32a1SGerd Hoffmann     case 0x06:
576f1ae32a1SGerd Hoffmann         val = s->frnum;
577f1ae32a1SGerd Hoffmann         break;
578f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
579f1ae32a1SGerd Hoffmann         {
580f1ae32a1SGerd Hoffmann             UHCIPort *port;
581f1ae32a1SGerd Hoffmann             int n;
582f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
583f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
584f1ae32a1SGerd Hoffmann                 goto read_default;
585f1ae32a1SGerd Hoffmann             port = &s->ports[n];
586f1ae32a1SGerd Hoffmann             val = port->ctrl;
587f1ae32a1SGerd Hoffmann         }
588f1ae32a1SGerd Hoffmann         break;
589f1ae32a1SGerd Hoffmann     default:
590f1ae32a1SGerd Hoffmann     read_default:
591f1ae32a1SGerd Hoffmann         val = 0xff7f; /* disabled port */
592f1ae32a1SGerd Hoffmann         break;
593f1ae32a1SGerd Hoffmann     }
594f1ae32a1SGerd Hoffmann 
59550dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_readw(addr, val);
596f1ae32a1SGerd Hoffmann 
597f1ae32a1SGerd Hoffmann     return val;
598f1ae32a1SGerd Hoffmann }
599f1ae32a1SGerd Hoffmann 
600f1ae32a1SGerd Hoffmann static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
601f1ae32a1SGerd Hoffmann {
602f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
603f1ae32a1SGerd Hoffmann 
604f1ae32a1SGerd Hoffmann     addr &= 0x1f;
60550dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_writel(addr, val);
606f1ae32a1SGerd Hoffmann 
607f1ae32a1SGerd Hoffmann     switch(addr) {
608f1ae32a1SGerd Hoffmann     case 0x08:
609f1ae32a1SGerd Hoffmann         s->fl_base_addr = val & ~0xfff;
610f1ae32a1SGerd Hoffmann         break;
611f1ae32a1SGerd Hoffmann     }
612f1ae32a1SGerd Hoffmann }
613f1ae32a1SGerd Hoffmann 
614f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
615f1ae32a1SGerd Hoffmann {
616f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
617f1ae32a1SGerd Hoffmann     uint32_t val;
618f1ae32a1SGerd Hoffmann 
619f1ae32a1SGerd Hoffmann     addr &= 0x1f;
620f1ae32a1SGerd Hoffmann     switch(addr) {
621f1ae32a1SGerd Hoffmann     case 0x08:
622f1ae32a1SGerd Hoffmann         val = s->fl_base_addr;
623f1ae32a1SGerd Hoffmann         break;
624f1ae32a1SGerd Hoffmann     default:
625f1ae32a1SGerd Hoffmann         val = 0xffffffff;
626f1ae32a1SGerd Hoffmann         break;
627f1ae32a1SGerd Hoffmann     }
62850dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_readl(addr, val);
629f1ae32a1SGerd Hoffmann     return val;
630f1ae32a1SGerd Hoffmann }
631f1ae32a1SGerd Hoffmann 
632f1ae32a1SGerd Hoffmann /* signal resume if controller suspended */
633f1ae32a1SGerd Hoffmann static void uhci_resume (void *opaque)
634f1ae32a1SGerd Hoffmann {
635f1ae32a1SGerd Hoffmann     UHCIState *s = (UHCIState *)opaque;
636f1ae32a1SGerd Hoffmann 
637f1ae32a1SGerd Hoffmann     if (!s)
638f1ae32a1SGerd Hoffmann         return;
639f1ae32a1SGerd Hoffmann 
640f1ae32a1SGerd Hoffmann     if (s->cmd & UHCI_CMD_EGSM) {
641f1ae32a1SGerd Hoffmann         s->cmd |= UHCI_CMD_FGR;
642f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_RD;
643f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
644f1ae32a1SGerd Hoffmann     }
645f1ae32a1SGerd Hoffmann }
646f1ae32a1SGerd Hoffmann 
647f1ae32a1SGerd Hoffmann static void uhci_attach(USBPort *port1)
648f1ae32a1SGerd Hoffmann {
649f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
650f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
651f1ae32a1SGerd Hoffmann 
652f1ae32a1SGerd Hoffmann     /* set connect status */
653f1ae32a1SGerd Hoffmann     port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
654f1ae32a1SGerd Hoffmann 
655f1ae32a1SGerd Hoffmann     /* update speed */
656f1ae32a1SGerd Hoffmann     if (port->port.dev->speed == USB_SPEED_LOW) {
657f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_LSDA;
658f1ae32a1SGerd Hoffmann     } else {
659f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_LSDA;
660f1ae32a1SGerd Hoffmann     }
661f1ae32a1SGerd Hoffmann 
662f1ae32a1SGerd Hoffmann     uhci_resume(s);
663f1ae32a1SGerd Hoffmann }
664f1ae32a1SGerd Hoffmann 
665f1ae32a1SGerd Hoffmann static void uhci_detach(USBPort *port1)
666f1ae32a1SGerd Hoffmann {
667f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
668f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
669f1ae32a1SGerd Hoffmann 
670f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, port1->dev);
671f1ae32a1SGerd Hoffmann 
672f1ae32a1SGerd Hoffmann     /* set connect status */
673f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_CCS) {
674f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_CCS;
675f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_CSC;
676f1ae32a1SGerd Hoffmann     }
677f1ae32a1SGerd Hoffmann     /* disable port */
678f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_EN) {
679f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_EN;
680f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_ENC;
681f1ae32a1SGerd Hoffmann     }
682f1ae32a1SGerd Hoffmann 
683f1ae32a1SGerd Hoffmann     uhci_resume(s);
684f1ae32a1SGerd Hoffmann }
685f1ae32a1SGerd Hoffmann 
686f1ae32a1SGerd Hoffmann static void uhci_child_detach(USBPort *port1, USBDevice *child)
687f1ae32a1SGerd Hoffmann {
688f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
689f1ae32a1SGerd Hoffmann 
690f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, child);
691f1ae32a1SGerd Hoffmann }
692f1ae32a1SGerd Hoffmann 
693f1ae32a1SGerd Hoffmann static void uhci_wakeup(USBPort *port1)
694f1ae32a1SGerd Hoffmann {
695f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
696f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
697f1ae32a1SGerd Hoffmann 
698f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
699f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_RD;
700f1ae32a1SGerd Hoffmann         uhci_resume(s);
701f1ae32a1SGerd Hoffmann     }
702f1ae32a1SGerd Hoffmann }
703f1ae32a1SGerd Hoffmann 
704f1ae32a1SGerd Hoffmann static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
705f1ae32a1SGerd Hoffmann {
706f1ae32a1SGerd Hoffmann     USBDevice *dev;
707f1ae32a1SGerd Hoffmann     int i;
708f1ae32a1SGerd Hoffmann 
709f1ae32a1SGerd Hoffmann     for (i = 0; i < NB_PORTS; i++) {
710f1ae32a1SGerd Hoffmann         UHCIPort *port = &s->ports[i];
711f1ae32a1SGerd Hoffmann         if (!(port->ctrl & UHCI_PORT_EN)) {
712f1ae32a1SGerd Hoffmann             continue;
713f1ae32a1SGerd Hoffmann         }
714f1ae32a1SGerd Hoffmann         dev = usb_find_device(&port->port, addr);
715f1ae32a1SGerd Hoffmann         if (dev != NULL) {
716f1ae32a1SGerd Hoffmann             return dev;
717f1ae32a1SGerd Hoffmann         }
718f1ae32a1SGerd Hoffmann     }
719f1ae32a1SGerd Hoffmann     return NULL;
720f1ae32a1SGerd Hoffmann }
721f1ae32a1SGerd Hoffmann 
722963a68b5SHans de Goede static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
723963a68b5SHans de Goede {
724963a68b5SHans de Goede     pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
725963a68b5SHans de Goede     le32_to_cpus(&td->link);
726963a68b5SHans de Goede     le32_to_cpus(&td->ctrl);
727963a68b5SHans de Goede     le32_to_cpus(&td->token);
728963a68b5SHans de Goede     le32_to_cpus(&td->buffer);
729963a68b5SHans de Goede }
730963a68b5SHans de Goede 
731f1ae32a1SGerd Hoffmann static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
732f1ae32a1SGerd Hoffmann {
733f1ae32a1SGerd Hoffmann     int len = 0, max_len, err, ret;
734f1ae32a1SGerd Hoffmann     uint8_t pid;
735f1ae32a1SGerd Hoffmann 
736f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
737f1ae32a1SGerd Hoffmann     pid = td->token & 0xff;
738f1ae32a1SGerd Hoffmann 
739f1ae32a1SGerd Hoffmann     ret = async->packet.result;
740f1ae32a1SGerd Hoffmann 
741f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOS)
742f1ae32a1SGerd Hoffmann         td->ctrl &= ~TD_CTRL_ACTIVE;
743f1ae32a1SGerd Hoffmann 
744f1ae32a1SGerd Hoffmann     if (ret < 0)
745f1ae32a1SGerd Hoffmann         goto out;
746f1ae32a1SGerd Hoffmann 
747f1ae32a1SGerd Hoffmann     len = async->packet.result;
748f1ae32a1SGerd Hoffmann     td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
749f1ae32a1SGerd Hoffmann 
750f1ae32a1SGerd Hoffmann     /* The NAK bit may have been set by a previous frame, so clear it
751f1ae32a1SGerd Hoffmann        here.  The docs are somewhat unclear, but win2k relies on this
752f1ae32a1SGerd Hoffmann        behavior.  */
753f1ae32a1SGerd Hoffmann     td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
754f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOC)
755f1ae32a1SGerd Hoffmann         *int_mask |= 0x01;
756f1ae32a1SGerd Hoffmann 
757f1ae32a1SGerd Hoffmann     if (pid == USB_TOKEN_IN) {
758f1ae32a1SGerd Hoffmann         if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
759f1ae32a1SGerd Hoffmann             *int_mask |= 0x02;
760f1ae32a1SGerd Hoffmann             /* short packet: do not update QH */
76150dcc0f8SGerd Hoffmann             trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
7621f250cc7SHans de Goede                                                      async->td_addr);
76360e1b2a6SGerd Hoffmann             return TD_RESULT_NEXT_QH;
764f1ae32a1SGerd Hoffmann         }
765f1ae32a1SGerd Hoffmann     }
766f1ae32a1SGerd Hoffmann 
767f1ae32a1SGerd Hoffmann     /* success */
7681f250cc7SHans de Goede     trace_usb_uhci_packet_complete_success(async->queue->token,
7691f250cc7SHans de Goede                                            async->td_addr);
77060e1b2a6SGerd Hoffmann     return TD_RESULT_COMPLETE;
771f1ae32a1SGerd Hoffmann 
772f1ae32a1SGerd Hoffmann out:
773f1ae32a1SGerd Hoffmann     switch(ret) {
774a89e255bSHans de Goede     case USB_RET_NAK:
775a89e255bSHans de Goede         td->ctrl |= TD_CTRL_NAK;
776a89e255bSHans de Goede         return TD_RESULT_NEXT_QH;
777a89e255bSHans de Goede 
778f1ae32a1SGerd Hoffmann     case USB_RET_STALL:
779f1ae32a1SGerd Hoffmann         td->ctrl |= TD_CTRL_STALL;
7801f250cc7SHans de Goede         trace_usb_uhci_packet_complete_stall(async->queue->token,
7811f250cc7SHans de Goede                                              async->td_addr);
782a89e255bSHans de Goede         err = TD_RESULT_NEXT_QH;
783a89e255bSHans de Goede         break;
784f1ae32a1SGerd Hoffmann 
785f1ae32a1SGerd Hoffmann     case USB_RET_BABBLE:
786f1ae32a1SGerd Hoffmann         td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
787f1ae32a1SGerd Hoffmann         /* frame interrupted */
7881f250cc7SHans de Goede         trace_usb_uhci_packet_complete_babble(async->queue->token,
7891f250cc7SHans de Goede                                               async->td_addr);
790a89e255bSHans de Goede         err = TD_RESULT_STOP_FRAME;
791f1ae32a1SGerd Hoffmann         break;
792f1ae32a1SGerd Hoffmann 
793f1ae32a1SGerd Hoffmann     case USB_RET_IOERROR:
794f1ae32a1SGerd Hoffmann     case USB_RET_NODEV:
795f1ae32a1SGerd Hoffmann     default:
796a89e255bSHans de Goede         td->ctrl |= TD_CTRL_TIMEOUT;
797a89e255bSHans de Goede         td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
7981f250cc7SHans de Goede         trace_usb_uhci_packet_complete_error(async->queue->token,
7991f250cc7SHans de Goede                                              async->td_addr);
800a89e255bSHans de Goede         err = TD_RESULT_NEXT_QH;
801f1ae32a1SGerd Hoffmann         break;
802f1ae32a1SGerd Hoffmann     }
803f1ae32a1SGerd Hoffmann 
804f1ae32a1SGerd Hoffmann     td->ctrl &= ~TD_CTRL_ACTIVE;
805f1ae32a1SGerd Hoffmann     s->status |= UHCI_STS_USBERR;
806a89e255bSHans de Goede     if (td->ctrl & TD_CTRL_IOC) {
807f1ae32a1SGerd Hoffmann         *int_mask |= 0x01;
808a89e255bSHans de Goede     }
809f1ae32a1SGerd Hoffmann     uhci_update_irq(s);
810a89e255bSHans de Goede     return err;
811f1ae32a1SGerd Hoffmann }
812f1ae32a1SGerd Hoffmann 
81366a08cbeSHans de Goede static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
814a4f30cd7SHans de Goede                           UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
815f1ae32a1SGerd Hoffmann {
816f1ae32a1SGerd Hoffmann     int len = 0, max_len;
8176ba43f1fSHans de Goede     bool spd;
818a4f30cd7SHans de Goede     bool queuing = (q != NULL);
81911d15e40SHans de Goede     uint8_t pid = td->token & 0xff;
8208c75a899SHans de Goede     UHCIAsync *async = uhci_async_find_td(s, td_addr);
8218c75a899SHans de Goede 
8228c75a899SHans de Goede     if (async) {
8238c75a899SHans de Goede         if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) {
8248c75a899SHans de Goede             assert(q == NULL || q == async->queue);
8258c75a899SHans de Goede             q = async->queue;
8268c75a899SHans de Goede         } else {
8278c75a899SHans de Goede             uhci_queue_free(async->queue, "guest re-used pending td");
8288c75a899SHans de Goede             async = NULL;
8298c75a899SHans de Goede         }
8308c75a899SHans de Goede     }
831f1ae32a1SGerd Hoffmann 
83266a08cbeSHans de Goede     if (q == NULL) {
83366a08cbeSHans de Goede         q = uhci_queue_find(s, td);
83466a08cbeSHans de Goede         if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) {
83566a08cbeSHans de Goede             uhci_queue_free(q, "guest re-used qh");
83666a08cbeSHans de Goede             q = NULL;
83766a08cbeSHans de Goede         }
83866a08cbeSHans de Goede     }
83966a08cbeSHans de Goede 
8403905097eSHans de Goede     if (q) {
8413905097eSHans de Goede         q->valid = 32;
8423905097eSHans de Goede     }
8433905097eSHans de Goede 
844f1ae32a1SGerd Hoffmann     /* Is active ? */
845883bca77SHans de Goede     if (!(td->ctrl & TD_CTRL_ACTIVE)) {
846420ca987SHans de Goede         if (async) {
847420ca987SHans de Goede             /* Guest marked a pending td non-active, cancel the queue */
848420ca987SHans de Goede             uhci_queue_free(async->queue, "pending td non-active");
849420ca987SHans de Goede         }
850883bca77SHans de Goede         /*
851883bca77SHans de Goede          * ehci11d spec page 22: "Even if the Active bit in the TD is already
852883bca77SHans de Goede          * cleared when the TD is fetched ... an IOC interrupt is generated"
853883bca77SHans de Goede          */
854883bca77SHans de Goede         if (td->ctrl & TD_CTRL_IOC) {
855883bca77SHans de Goede                 *int_mask |= 0x01;
856883bca77SHans de Goede         }
85760e1b2a6SGerd Hoffmann         return TD_RESULT_NEXT_QH;
858883bca77SHans de Goede     }
859f1ae32a1SGerd Hoffmann 
860f1ae32a1SGerd Hoffmann     if (async) {
861ee008ba6SGerd Hoffmann         if (queuing) {
862ee008ba6SGerd Hoffmann             /* we are busy filling the queue, we are not prepared
863ee008ba6SGerd Hoffmann                to consume completed packages then, just leave them
864ee008ba6SGerd Hoffmann                in async state */
865ee008ba6SGerd Hoffmann             return TD_RESULT_ASYNC_CONT;
866ee008ba6SGerd Hoffmann         }
8678928c9c4SHans de Goede         if (!async->done) {
8688928c9c4SHans de Goede             UHCI_TD last_td;
8698928c9c4SHans de Goede             UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs, asyncs_head);
8708928c9c4SHans de Goede             /*
8718928c9c4SHans de Goede              * While we are waiting for the current td to complete, the guest
8728928c9c4SHans de Goede              * may have added more tds to the queue. Note we re-read the td
8738928c9c4SHans de Goede              * rather then caching it, as we want to see guest made changes!
8748928c9c4SHans de Goede              */
8758928c9c4SHans de Goede             uhci_read_td(s, &last_td, last->td_addr);
8768928c9c4SHans de Goede             uhci_queue_fill(async->queue, &last_td);
877f1ae32a1SGerd Hoffmann 
8788928c9c4SHans de Goede             return TD_RESULT_ASYNC_CONT;
8798928c9c4SHans de Goede         }
880f1ae32a1SGerd Hoffmann         uhci_async_unlink(async);
881f1ae32a1SGerd Hoffmann         goto done;
882f1ae32a1SGerd Hoffmann     }
883f1ae32a1SGerd Hoffmann 
884f1ae32a1SGerd Hoffmann     /* Allocate new packet */
885a4f30cd7SHans de Goede     if (q == NULL) {
88611d15e40SHans de Goede         USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
88711d15e40SHans de Goede         USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
88866a08cbeSHans de Goede         q = uhci_queue_new(s, qh_addr, td, ep);
889a4f30cd7SHans de Goede     }
890a4f30cd7SHans de Goede     async = uhci_async_alloc(q, td_addr);
891f1ae32a1SGerd Hoffmann 
892f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
8936ba43f1fSHans de Goede     spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
89411d15e40SHans de Goede     usb_packet_setup(&async->packet, pid, q->ep, td_addr, spd,
895a6fb2ddbSHans de Goede                      (td->ctrl & TD_CTRL_IOC) != 0);
896f1ae32a1SGerd Hoffmann     qemu_sglist_add(&async->sgl, td->buffer, max_len);
897f1ae32a1SGerd Hoffmann     usb_packet_map(&async->packet, &async->sgl);
898f1ae32a1SGerd Hoffmann 
899f1ae32a1SGerd Hoffmann     switch(pid) {
900f1ae32a1SGerd Hoffmann     case USB_TOKEN_OUT:
901f1ae32a1SGerd Hoffmann     case USB_TOKEN_SETUP:
90211d15e40SHans de Goede         len = usb_handle_packet(q->ep->dev, &async->packet);
903f1ae32a1SGerd Hoffmann         if (len >= 0)
904f1ae32a1SGerd Hoffmann             len = max_len;
905f1ae32a1SGerd Hoffmann         break;
906f1ae32a1SGerd Hoffmann 
907f1ae32a1SGerd Hoffmann     case USB_TOKEN_IN:
90811d15e40SHans de Goede         len = usb_handle_packet(q->ep->dev, &async->packet);
909f1ae32a1SGerd Hoffmann         break;
910f1ae32a1SGerd Hoffmann 
911f1ae32a1SGerd Hoffmann     default:
912f1ae32a1SGerd Hoffmann         /* invalid pid : frame interrupted */
91300a0770dSHans de Goede         usb_packet_unmap(&async->packet, &async->sgl);
914f1ae32a1SGerd Hoffmann         uhci_async_free(async);
915f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_HCPERR;
916f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
91760e1b2a6SGerd Hoffmann         return TD_RESULT_STOP_FRAME;
918f1ae32a1SGerd Hoffmann     }
919f1ae32a1SGerd Hoffmann 
920f1ae32a1SGerd Hoffmann     if (len == USB_RET_ASYNC) {
921f1ae32a1SGerd Hoffmann         uhci_async_link(async);
922a4f30cd7SHans de Goede         if (!queuing) {
92311d15e40SHans de Goede             uhci_queue_fill(q, td);
924a4f30cd7SHans de Goede         }
9254efe4ef3SGerd Hoffmann         return TD_RESULT_ASYNC_START;
926f1ae32a1SGerd Hoffmann     }
927f1ae32a1SGerd Hoffmann 
928f1ae32a1SGerd Hoffmann     async->packet.result = len;
929f1ae32a1SGerd Hoffmann 
930f1ae32a1SGerd Hoffmann done:
931f1ae32a1SGerd Hoffmann     len = uhci_complete_td(s, td, async, int_mask);
932e2f89926SDavid Gibson     usb_packet_unmap(&async->packet, &async->sgl);
933f1ae32a1SGerd Hoffmann     uhci_async_free(async);
934f1ae32a1SGerd Hoffmann     return len;
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 
9420cae7b1aSHans de Goede     if (packet->result == USB_RET_REMOVE_FROM_QUEUE) {
9430cae7b1aSHans de Goede         uhci_async_unlink(async);
9440cae7b1aSHans de Goede         uhci_async_cancel(async);
9450cae7b1aSHans de Goede         return;
9460cae7b1aSHans de Goede     }
9470cae7b1aSHans de Goede 
948f1ae32a1SGerd Hoffmann     async->done = 1;
94940141d12SGerd Hoffmann     if (s->frame_bytes < s->frame_bandwidth) {
9509a16c595SGerd Hoffmann         qemu_bh_schedule(s->bh);
9519a16c595SGerd Hoffmann     }
952f1ae32a1SGerd Hoffmann }
953f1ae32a1SGerd Hoffmann 
954f1ae32a1SGerd Hoffmann static int is_valid(uint32_t link)
955f1ae32a1SGerd Hoffmann {
956f1ae32a1SGerd Hoffmann     return (link & 1) == 0;
957f1ae32a1SGerd Hoffmann }
958f1ae32a1SGerd Hoffmann 
959f1ae32a1SGerd Hoffmann static int is_qh(uint32_t link)
960f1ae32a1SGerd Hoffmann {
961f1ae32a1SGerd Hoffmann     return (link & 2) != 0;
962f1ae32a1SGerd Hoffmann }
963f1ae32a1SGerd Hoffmann 
964f1ae32a1SGerd Hoffmann static int depth_first(uint32_t link)
965f1ae32a1SGerd Hoffmann {
966f1ae32a1SGerd Hoffmann     return (link & 4) != 0;
967f1ae32a1SGerd Hoffmann }
968f1ae32a1SGerd Hoffmann 
969f1ae32a1SGerd Hoffmann /* QH DB used for detecting QH loops */
970f1ae32a1SGerd Hoffmann #define UHCI_MAX_QUEUES 128
971f1ae32a1SGerd Hoffmann typedef struct {
972f1ae32a1SGerd Hoffmann     uint32_t addr[UHCI_MAX_QUEUES];
973f1ae32a1SGerd Hoffmann     int      count;
974f1ae32a1SGerd Hoffmann } QhDb;
975f1ae32a1SGerd Hoffmann 
976f1ae32a1SGerd Hoffmann static void qhdb_reset(QhDb *db)
977f1ae32a1SGerd Hoffmann {
978f1ae32a1SGerd Hoffmann     db->count = 0;
979f1ae32a1SGerd Hoffmann }
980f1ae32a1SGerd Hoffmann 
981f1ae32a1SGerd Hoffmann /* Add QH to DB. Returns 1 if already present or DB is full. */
982f1ae32a1SGerd Hoffmann static int qhdb_insert(QhDb *db, uint32_t addr)
983f1ae32a1SGerd Hoffmann {
984f1ae32a1SGerd Hoffmann     int i;
985f1ae32a1SGerd Hoffmann     for (i = 0; i < db->count; i++)
986f1ae32a1SGerd Hoffmann         if (db->addr[i] == addr)
987f1ae32a1SGerd Hoffmann             return 1;
988f1ae32a1SGerd Hoffmann 
989f1ae32a1SGerd Hoffmann     if (db->count >= UHCI_MAX_QUEUES)
990f1ae32a1SGerd Hoffmann         return 1;
991f1ae32a1SGerd Hoffmann 
992f1ae32a1SGerd Hoffmann     db->addr[db->count++] = addr;
993f1ae32a1SGerd Hoffmann     return 0;
994f1ae32a1SGerd Hoffmann }
995f1ae32a1SGerd Hoffmann 
99611d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
997f1ae32a1SGerd Hoffmann {
998f1ae32a1SGerd Hoffmann     uint32_t int_mask = 0;
999f1ae32a1SGerd Hoffmann     uint32_t plink = td->link;
1000f1ae32a1SGerd Hoffmann     UHCI_TD ptd;
1001f1ae32a1SGerd Hoffmann     int ret;
1002f1ae32a1SGerd Hoffmann 
10036ba43f1fSHans de Goede     while (is_valid(plink)) {
1004a4f30cd7SHans de Goede         uhci_read_td(q->uhci, &ptd, plink);
1005f1ae32a1SGerd Hoffmann         if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
1006f1ae32a1SGerd Hoffmann             break;
1007f1ae32a1SGerd Hoffmann         }
1008a4f30cd7SHans de Goede         if (uhci_queue_token(&ptd) != q->token) {
1009f1ae32a1SGerd Hoffmann             break;
1010f1ae32a1SGerd Hoffmann         }
101150dcc0f8SGerd Hoffmann         trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
101266a08cbeSHans de Goede         ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask);
101352b0fecdSGerd Hoffmann         if (ret == TD_RESULT_ASYNC_CONT) {
101452b0fecdSGerd Hoffmann             break;
101552b0fecdSGerd Hoffmann         }
10164efe4ef3SGerd Hoffmann         assert(ret == TD_RESULT_ASYNC_START);
1017f1ae32a1SGerd Hoffmann         assert(int_mask == 0);
1018f1ae32a1SGerd Hoffmann         plink = ptd.link;
1019f1ae32a1SGerd Hoffmann     }
102011d15e40SHans de Goede     usb_device_flush_ep_queue(q->ep->dev, q->ep);
1021f1ae32a1SGerd Hoffmann }
1022f1ae32a1SGerd Hoffmann 
1023f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s)
1024f1ae32a1SGerd Hoffmann {
1025f1ae32a1SGerd Hoffmann     uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
10264aed20e2SGerd Hoffmann     uint32_t curr_qh, td_count = 0;
1027f1ae32a1SGerd Hoffmann     int cnt, ret;
1028f1ae32a1SGerd Hoffmann     UHCI_TD td;
1029f1ae32a1SGerd Hoffmann     UHCI_QH qh;
1030f1ae32a1SGerd Hoffmann     QhDb qhdb;
1031f1ae32a1SGerd Hoffmann 
1032f1ae32a1SGerd Hoffmann     frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
1033f1ae32a1SGerd Hoffmann 
1034f1ae32a1SGerd Hoffmann     pci_dma_read(&s->dev, frame_addr, &link, 4);
1035f1ae32a1SGerd Hoffmann     le32_to_cpus(&link);
1036f1ae32a1SGerd Hoffmann 
1037f1ae32a1SGerd Hoffmann     int_mask = 0;
1038f1ae32a1SGerd Hoffmann     curr_qh  = 0;
1039f1ae32a1SGerd Hoffmann 
1040f1ae32a1SGerd Hoffmann     qhdb_reset(&qhdb);
1041f1ae32a1SGerd Hoffmann 
1042f1ae32a1SGerd Hoffmann     for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
104340141d12SGerd Hoffmann         if (s->frame_bytes >= s->frame_bandwidth) {
10444aed20e2SGerd Hoffmann             /* We've reached the usb 1.1 bandwidth, which is
10454aed20e2SGerd Hoffmann                1280 bytes/frame, stop processing */
10464aed20e2SGerd Hoffmann             trace_usb_uhci_frame_stop_bandwidth();
10474aed20e2SGerd Hoffmann             break;
10484aed20e2SGerd Hoffmann         }
1049f1ae32a1SGerd Hoffmann         if (is_qh(link)) {
1050f1ae32a1SGerd Hoffmann             /* QH */
105150dcc0f8SGerd Hoffmann             trace_usb_uhci_qh_load(link & ~0xf);
1052f1ae32a1SGerd Hoffmann 
1053f1ae32a1SGerd Hoffmann             if (qhdb_insert(&qhdb, link)) {
1054f1ae32a1SGerd Hoffmann                 /*
1055f1ae32a1SGerd Hoffmann                  * We're going in circles. Which is not a bug because
1056f1ae32a1SGerd Hoffmann                  * HCD is allowed to do that as part of the BW management.
1057f1ae32a1SGerd Hoffmann                  *
10584aed20e2SGerd Hoffmann                  * Stop processing here if no transaction has been done
10594aed20e2SGerd Hoffmann                  * since we've been here last time.
1060f1ae32a1SGerd Hoffmann                  */
1061f1ae32a1SGerd Hoffmann                 if (td_count == 0) {
106250dcc0f8SGerd Hoffmann                     trace_usb_uhci_frame_loop_stop_idle();
1063f1ae32a1SGerd Hoffmann                     break;
1064f1ae32a1SGerd Hoffmann                 } else {
106550dcc0f8SGerd Hoffmann                     trace_usb_uhci_frame_loop_continue();
1066f1ae32a1SGerd Hoffmann                     td_count = 0;
1067f1ae32a1SGerd Hoffmann                     qhdb_reset(&qhdb);
1068f1ae32a1SGerd Hoffmann                     qhdb_insert(&qhdb, link);
1069f1ae32a1SGerd Hoffmann                 }
1070f1ae32a1SGerd Hoffmann             }
1071f1ae32a1SGerd Hoffmann 
1072f1ae32a1SGerd Hoffmann             pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1073f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.link);
1074f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.el_link);
1075f1ae32a1SGerd Hoffmann 
1076f1ae32a1SGerd Hoffmann             if (!is_valid(qh.el_link)) {
1077f1ae32a1SGerd Hoffmann                 /* QH w/o elements */
1078f1ae32a1SGerd Hoffmann                 curr_qh = 0;
1079f1ae32a1SGerd Hoffmann                 link = qh.link;
1080f1ae32a1SGerd Hoffmann             } else {
1081f1ae32a1SGerd Hoffmann                 /* QH with elements */
1082f1ae32a1SGerd Hoffmann             	curr_qh = link;
1083f1ae32a1SGerd Hoffmann             	link = qh.el_link;
1084f1ae32a1SGerd Hoffmann             }
1085f1ae32a1SGerd Hoffmann             continue;
1086f1ae32a1SGerd Hoffmann         }
1087f1ae32a1SGerd Hoffmann 
1088f1ae32a1SGerd Hoffmann         /* TD */
1089963a68b5SHans de Goede         uhci_read_td(s, &td, link);
109050dcc0f8SGerd Hoffmann         trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
1091f1ae32a1SGerd Hoffmann 
1092f1ae32a1SGerd Hoffmann         old_td_ctrl = td.ctrl;
109366a08cbeSHans de Goede         ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask);
1094f1ae32a1SGerd Hoffmann         if (old_td_ctrl != td.ctrl) {
1095f1ae32a1SGerd Hoffmann             /* update the status bits of the TD */
1096f1ae32a1SGerd Hoffmann             val = cpu_to_le32(td.ctrl);
1097f1ae32a1SGerd Hoffmann             pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1098f1ae32a1SGerd Hoffmann         }
1099f1ae32a1SGerd Hoffmann 
1100f1ae32a1SGerd Hoffmann         switch (ret) {
110160e1b2a6SGerd Hoffmann         case TD_RESULT_STOP_FRAME: /* interrupted frame */
1102f1ae32a1SGerd Hoffmann             goto out;
1103f1ae32a1SGerd Hoffmann 
110460e1b2a6SGerd Hoffmann         case TD_RESULT_NEXT_QH:
11054efe4ef3SGerd Hoffmann         case TD_RESULT_ASYNC_CONT:
110650dcc0f8SGerd Hoffmann             trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
1107f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1108f1ae32a1SGerd Hoffmann             continue;
1109f1ae32a1SGerd Hoffmann 
11104efe4ef3SGerd Hoffmann         case TD_RESULT_ASYNC_START:
111150dcc0f8SGerd Hoffmann             trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
1112f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1113f1ae32a1SGerd Hoffmann             continue;
1114f1ae32a1SGerd Hoffmann 
111560e1b2a6SGerd Hoffmann         case TD_RESULT_COMPLETE:
111650dcc0f8SGerd Hoffmann             trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
1117f1ae32a1SGerd Hoffmann             link = td.link;
1118f1ae32a1SGerd Hoffmann             td_count++;
11194aed20e2SGerd Hoffmann             s->frame_bytes += (td.ctrl & 0x7ff) + 1;
1120f1ae32a1SGerd Hoffmann 
1121f1ae32a1SGerd Hoffmann             if (curr_qh) {
1122f1ae32a1SGerd Hoffmann                 /* update QH element link */
1123f1ae32a1SGerd Hoffmann                 qh.el_link = link;
1124f1ae32a1SGerd Hoffmann                 val = cpu_to_le32(qh.el_link);
1125f1ae32a1SGerd Hoffmann                 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1126f1ae32a1SGerd Hoffmann 
1127f1ae32a1SGerd Hoffmann                 if (!depth_first(link)) {
1128f1ae32a1SGerd Hoffmann                     /* done with this QH */
1129f1ae32a1SGerd Hoffmann                     curr_qh = 0;
1130f1ae32a1SGerd Hoffmann                     link    = qh.link;
1131f1ae32a1SGerd Hoffmann                 }
1132f1ae32a1SGerd Hoffmann             }
1133f1ae32a1SGerd Hoffmann             break;
1134f1ae32a1SGerd Hoffmann 
1135f1ae32a1SGerd Hoffmann         default:
1136f1ae32a1SGerd Hoffmann             assert(!"unknown return code");
1137f1ae32a1SGerd Hoffmann         }
1138f1ae32a1SGerd Hoffmann 
1139f1ae32a1SGerd Hoffmann         /* go to the next entry */
1140f1ae32a1SGerd Hoffmann     }
1141f1ae32a1SGerd Hoffmann 
1142f1ae32a1SGerd Hoffmann out:
1143f1ae32a1SGerd Hoffmann     s->pending_int_mask |= int_mask;
1144f1ae32a1SGerd Hoffmann }
1145f1ae32a1SGerd Hoffmann 
11469a16c595SGerd Hoffmann static void uhci_bh(void *opaque)
11479a16c595SGerd Hoffmann {
11489a16c595SGerd Hoffmann     UHCIState *s = opaque;
11499a16c595SGerd Hoffmann     uhci_process_frame(s);
11509a16c595SGerd Hoffmann }
11519a16c595SGerd Hoffmann 
1152f1ae32a1SGerd Hoffmann static void uhci_frame_timer(void *opaque)
1153f1ae32a1SGerd Hoffmann {
1154f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
1155f1ae32a1SGerd Hoffmann 
1156f1ae32a1SGerd Hoffmann     /* prepare the timer for the next frame */
1157f1ae32a1SGerd Hoffmann     s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
11584aed20e2SGerd Hoffmann     s->frame_bytes = 0;
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 
1171f1ae32a1SGerd Hoffmann     /* Complete the previous frame */
1172f1ae32a1SGerd Hoffmann     if (s->pending_int_mask) {
1173f1ae32a1SGerd Hoffmann         s->status2 |= s->pending_int_mask;
1174f1ae32a1SGerd Hoffmann         s->status  |= UHCI_STS_USBINT;
1175f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
1176f1ae32a1SGerd Hoffmann     }
1177f1ae32a1SGerd Hoffmann     s->pending_int_mask = 0;
1178f1ae32a1SGerd Hoffmann 
1179f1ae32a1SGerd Hoffmann     /* Start new frame */
1180f1ae32a1SGerd Hoffmann     s->frnum = (s->frnum + 1) & 0x7ff;
1181f1ae32a1SGerd Hoffmann 
118250dcc0f8SGerd Hoffmann     trace_usb_uhci_frame_start(s->frnum);
1183f1ae32a1SGerd Hoffmann 
1184f1ae32a1SGerd Hoffmann     uhci_async_validate_begin(s);
1185f1ae32a1SGerd Hoffmann 
1186f1ae32a1SGerd Hoffmann     uhci_process_frame(s);
1187f1ae32a1SGerd Hoffmann 
1188f1ae32a1SGerd Hoffmann     uhci_async_validate_end(s);
1189f1ae32a1SGerd Hoffmann 
1190f1ae32a1SGerd Hoffmann     qemu_mod_timer(s->frame_timer, s->expire_time);
1191f1ae32a1SGerd Hoffmann }
1192f1ae32a1SGerd Hoffmann 
1193f1ae32a1SGerd Hoffmann static const MemoryRegionPortio uhci_portio[] = {
1194f1ae32a1SGerd Hoffmann     { 0, 32, 2, .write = uhci_ioport_writew, },
1195f1ae32a1SGerd Hoffmann     { 0, 32, 2, .read = uhci_ioport_readw, },
1196f1ae32a1SGerd Hoffmann     { 0, 32, 4, .write = uhci_ioport_writel, },
1197f1ae32a1SGerd Hoffmann     { 0, 32, 4, .read = uhci_ioport_readl, },
1198f1ae32a1SGerd Hoffmann     { 0, 32, 1, .write = uhci_ioport_writeb, },
1199f1ae32a1SGerd Hoffmann     { 0, 32, 1, .read = uhci_ioport_readb, },
1200f1ae32a1SGerd Hoffmann     PORTIO_END_OF_LIST()
1201f1ae32a1SGerd Hoffmann };
1202f1ae32a1SGerd Hoffmann 
1203f1ae32a1SGerd Hoffmann static const MemoryRegionOps uhci_ioport_ops = {
1204f1ae32a1SGerd Hoffmann     .old_portio = uhci_portio,
1205f1ae32a1SGerd Hoffmann };
1206f1ae32a1SGerd Hoffmann 
1207f1ae32a1SGerd Hoffmann static USBPortOps uhci_port_ops = {
1208f1ae32a1SGerd Hoffmann     .attach = uhci_attach,
1209f1ae32a1SGerd Hoffmann     .detach = uhci_detach,
1210f1ae32a1SGerd Hoffmann     .child_detach = uhci_child_detach,
1211f1ae32a1SGerd Hoffmann     .wakeup = uhci_wakeup,
1212f1ae32a1SGerd Hoffmann     .complete = uhci_async_complete,
1213f1ae32a1SGerd Hoffmann };
1214f1ae32a1SGerd Hoffmann 
1215f1ae32a1SGerd Hoffmann static USBBusOps uhci_bus_ops = {
1216f1ae32a1SGerd Hoffmann };
1217f1ae32a1SGerd Hoffmann 
1218f1ae32a1SGerd Hoffmann static int usb_uhci_common_initfn(PCIDevice *dev)
1219f1ae32a1SGerd Hoffmann {
1220973002c1SGerd Hoffmann     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1221f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1222f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1223f1ae32a1SGerd Hoffmann     int i;
1224f1ae32a1SGerd Hoffmann 
1225f1ae32a1SGerd Hoffmann     pci_conf[PCI_CLASS_PROG] = 0x00;
1226f1ae32a1SGerd Hoffmann     /* TODO: reset value should be 0. */
1227f1ae32a1SGerd Hoffmann     pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1228f1ae32a1SGerd Hoffmann 
1229973002c1SGerd Hoffmann     switch (pc->device_id) {
1230973002c1SGerd Hoffmann     case PCI_DEVICE_ID_INTEL_82801I_UHCI1:
1231973002c1SGerd Hoffmann         s->irq_pin = 0;  /* A */
1232973002c1SGerd Hoffmann         break;
1233973002c1SGerd Hoffmann     case PCI_DEVICE_ID_INTEL_82801I_UHCI2:
1234973002c1SGerd Hoffmann         s->irq_pin = 1;  /* B */
1235973002c1SGerd Hoffmann         break;
1236973002c1SGerd Hoffmann     case PCI_DEVICE_ID_INTEL_82801I_UHCI3:
1237973002c1SGerd Hoffmann         s->irq_pin = 2;  /* C */
1238973002c1SGerd Hoffmann         break;
1239973002c1SGerd Hoffmann     default:
1240973002c1SGerd Hoffmann         s->irq_pin = 3;  /* D */
1241973002c1SGerd Hoffmann         break;
1242973002c1SGerd Hoffmann     }
1243973002c1SGerd Hoffmann     pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1);
1244973002c1SGerd Hoffmann 
1245f1ae32a1SGerd Hoffmann     if (s->masterbus) {
1246f1ae32a1SGerd Hoffmann         USBPort *ports[NB_PORTS];
1247f1ae32a1SGerd Hoffmann         for(i = 0; i < NB_PORTS; i++) {
1248f1ae32a1SGerd Hoffmann             ports[i] = &s->ports[i].port;
1249f1ae32a1SGerd Hoffmann         }
1250f1ae32a1SGerd Hoffmann         if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1251f1ae32a1SGerd Hoffmann                 s->firstport, s, &uhci_port_ops,
1252f1ae32a1SGerd Hoffmann                 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1253f1ae32a1SGerd Hoffmann             return -1;
1254f1ae32a1SGerd Hoffmann         }
1255f1ae32a1SGerd Hoffmann     } else {
1256f1ae32a1SGerd Hoffmann         usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1257f1ae32a1SGerd Hoffmann         for (i = 0; i < NB_PORTS; i++) {
1258f1ae32a1SGerd Hoffmann             usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1259f1ae32a1SGerd Hoffmann                               USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1260f1ae32a1SGerd Hoffmann         }
1261f1ae32a1SGerd Hoffmann     }
12629a16c595SGerd Hoffmann     s->bh = qemu_bh_new(uhci_bh, s);
1263f1ae32a1SGerd Hoffmann     s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1264f1ae32a1SGerd Hoffmann     s->num_ports_vmstate = NB_PORTS;
1265f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&s->queues);
1266f1ae32a1SGerd Hoffmann 
1267f1ae32a1SGerd Hoffmann     qemu_register_reset(uhci_reset, s);
1268f1ae32a1SGerd Hoffmann 
1269f1ae32a1SGerd Hoffmann     memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1270f1ae32a1SGerd Hoffmann     /* Use region 4 for consistency with real hardware.  BSD guests seem
1271f1ae32a1SGerd Hoffmann        to rely on this.  */
1272f1ae32a1SGerd Hoffmann     pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1273f1ae32a1SGerd Hoffmann 
1274f1ae32a1SGerd Hoffmann     return 0;
1275f1ae32a1SGerd Hoffmann }
1276f1ae32a1SGerd Hoffmann 
1277f1ae32a1SGerd Hoffmann static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1278f1ae32a1SGerd Hoffmann {
1279f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1280f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1281f1ae32a1SGerd Hoffmann 
1282f1ae32a1SGerd Hoffmann     /* USB misc control 1/2 */
1283f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x40,0x00001000);
1284f1ae32a1SGerd Hoffmann     /* PM capability */
1285f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x80,0x00020001);
1286f1ae32a1SGerd Hoffmann     /* USB legacy support  */
1287f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0xc0,0x00002000);
1288f1ae32a1SGerd Hoffmann 
1289f1ae32a1SGerd Hoffmann     return usb_uhci_common_initfn(dev);
1290f1ae32a1SGerd Hoffmann }
1291f1ae32a1SGerd Hoffmann 
1292f90c2bcdSAlex Williamson static void usb_uhci_exit(PCIDevice *dev)
1293f1ae32a1SGerd Hoffmann {
1294f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1295f1ae32a1SGerd Hoffmann 
1296f1ae32a1SGerd Hoffmann     memory_region_destroy(&s->io_bar);
1297f1ae32a1SGerd Hoffmann }
1298f1ae32a1SGerd Hoffmann 
1299f1ae32a1SGerd Hoffmann static Property uhci_properties[] = {
1300f1ae32a1SGerd Hoffmann     DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1301f1ae32a1SGerd Hoffmann     DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
130240141d12SGerd Hoffmann     DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
1303f1ae32a1SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
1304f1ae32a1SGerd Hoffmann };
1305f1ae32a1SGerd Hoffmann 
1306*2c2e8525SGerd Hoffmann static void uhci_class_init(ObjectClass *klass, void *data)
1307f1ae32a1SGerd Hoffmann {
1308f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1309f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1310*2c2e8525SGerd Hoffmann     UHCIInfo *info = data;
1311f1ae32a1SGerd Hoffmann 
1312*2c2e8525SGerd Hoffmann     k->init = info->initfn ? info->initfn : usb_uhci_common_initfn;
1313*2c2e8525SGerd Hoffmann     k->exit = info->unplug ? usb_uhci_exit : NULL;
1314*2c2e8525SGerd Hoffmann     k->vendor_id = info->vendor_id;
1315*2c2e8525SGerd Hoffmann     k->device_id = info->device_id;
1316*2c2e8525SGerd Hoffmann     k->revision  = info->revision;
1317f1ae32a1SGerd Hoffmann     k->class_id  = PCI_CLASS_SERIAL_USB;
1318f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1319f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1320f1ae32a1SGerd Hoffmann }
1321f1ae32a1SGerd Hoffmann 
1322*2c2e8525SGerd Hoffmann static UHCIInfo uhci_info[] = {
1323*2c2e8525SGerd Hoffmann     {
1324f1ae32a1SGerd Hoffmann         .name       = "piix3-usb-uhci",
1325*2c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
1326*2c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82371SB_2,
1327*2c2e8525SGerd Hoffmann         .revision  = 0x01,
1328*2c2e8525SGerd Hoffmann         .unplug    = true,
1329*2c2e8525SGerd Hoffmann     },{
1330f1ae32a1SGerd Hoffmann         .name      = "piix4-usb-uhci",
1331*2c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
1332*2c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82371AB_2,
1333*2c2e8525SGerd Hoffmann         .revision  = 0x01,
1334*2c2e8525SGerd Hoffmann         .unplug    = true,
1335*2c2e8525SGerd Hoffmann     },{
1336f1ae32a1SGerd Hoffmann         .name      = "vt82c686b-usb-uhci",
1337*2c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_VIA,
1338*2c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_VIA_UHCI,
1339*2c2e8525SGerd Hoffmann         .revision  = 0x01,
1340*2c2e8525SGerd Hoffmann         .initfn    = usb_uhci_vt82c686b_initfn,
1341*2c2e8525SGerd Hoffmann         .unplug    = true,
1342*2c2e8525SGerd Hoffmann     },{
1343f1ae32a1SGerd Hoffmann         .name      = "ich9-usb-uhci1",
1344*2c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
1345*2c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1,
1346*2c2e8525SGerd Hoffmann         .revision  = 0x03,
1347*2c2e8525SGerd Hoffmann         .unplug    = false,
1348*2c2e8525SGerd Hoffmann     },{
1349f1ae32a1SGerd Hoffmann         .name      = "ich9-usb-uhci2",
1350*2c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
1351*2c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2,
1352*2c2e8525SGerd Hoffmann         .revision  = 0x03,
1353*2c2e8525SGerd Hoffmann         .unplug    = false,
1354*2c2e8525SGerd Hoffmann     },{
1355f1ae32a1SGerd Hoffmann         .name      = "ich9-usb-uhci3",
1356*2c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
1357*2c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3,
1358*2c2e8525SGerd Hoffmann         .revision  = 0x03,
1359*2c2e8525SGerd Hoffmann         .unplug    = false,
1360*2c2e8525SGerd Hoffmann     }
1361f1ae32a1SGerd Hoffmann };
1362f1ae32a1SGerd Hoffmann 
1363f1ae32a1SGerd Hoffmann static void uhci_register_types(void)
1364f1ae32a1SGerd Hoffmann {
1365*2c2e8525SGerd Hoffmann     TypeInfo uhci_type_info = {
1366*2c2e8525SGerd Hoffmann         .parent        = TYPE_PCI_DEVICE,
1367*2c2e8525SGerd Hoffmann         .instance_size = sizeof(UHCIState),
1368*2c2e8525SGerd Hoffmann         .class_init    = uhci_class_init,
1369*2c2e8525SGerd Hoffmann     };
1370*2c2e8525SGerd Hoffmann     int i;
1371*2c2e8525SGerd Hoffmann 
1372*2c2e8525SGerd Hoffmann     for (i = 0; i < ARRAY_SIZE(uhci_info); i++) {
1373*2c2e8525SGerd Hoffmann         uhci_type_info.name = uhci_info[i].name;
1374*2c2e8525SGerd Hoffmann         uhci_type_info.class_data = uhci_info + i;
1375*2c2e8525SGerd Hoffmann         type_register(&uhci_type_info);
1376*2c2e8525SGerd Hoffmann     }
1377f1ae32a1SGerd Hoffmann }
1378f1ae32a1SGerd Hoffmann 
1379f1ae32a1SGerd Hoffmann type_init(uhci_register_types)
1380