xref: /openbmc/qemu/hw/usb/hcd-uhci.c (revision 475443cf14d7ef01b9ea56eed8657804f7bdf664)
1f1ae32a1SGerd Hoffmann /*
2f1ae32a1SGerd Hoffmann  * USB UHCI controller emulation
3f1ae32a1SGerd Hoffmann  *
4f1ae32a1SGerd Hoffmann  * Copyright (c) 2005 Fabrice Bellard
5f1ae32a1SGerd Hoffmann  *
6f1ae32a1SGerd Hoffmann  * Copyright (c) 2008 Max Krasnyansky
7f1ae32a1SGerd Hoffmann  *     Magor rewrite of the UHCI data structures parser and frame processor
8f1ae32a1SGerd Hoffmann  *     Support for fully async operation and multiple outstanding transactions
9f1ae32a1SGerd Hoffmann  *
10f1ae32a1SGerd Hoffmann  * Permission is hereby granted, free of charge, to any person obtaining a copy
11f1ae32a1SGerd Hoffmann  * of this software and associated documentation files (the "Software"), to deal
12f1ae32a1SGerd Hoffmann  * in the Software without restriction, including without limitation the rights
13f1ae32a1SGerd Hoffmann  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14f1ae32a1SGerd Hoffmann  * copies of the Software, and to permit persons to whom the Software is
15f1ae32a1SGerd Hoffmann  * furnished to do so, subject to the following conditions:
16f1ae32a1SGerd Hoffmann  *
17f1ae32a1SGerd Hoffmann  * The above copyright notice and this permission notice shall be included in
18f1ae32a1SGerd Hoffmann  * all copies or substantial portions of the Software.
19f1ae32a1SGerd Hoffmann  *
20f1ae32a1SGerd Hoffmann  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21f1ae32a1SGerd Hoffmann  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22f1ae32a1SGerd Hoffmann  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23f1ae32a1SGerd Hoffmann  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24f1ae32a1SGerd Hoffmann  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25f1ae32a1SGerd Hoffmann  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26f1ae32a1SGerd Hoffmann  * THE SOFTWARE.
27f1ae32a1SGerd Hoffmann  */
28f1ae32a1SGerd Hoffmann #include "hw/hw.h"
29f1ae32a1SGerd Hoffmann #include "hw/usb.h"
30a2cb15b0SMichael S. Tsirkin #include "hw/pci/pci.h"
311de7afc9SPaolo Bonzini #include "qemu/timer.h"
321de7afc9SPaolo Bonzini #include "qemu/iov.h"
339c17d615SPaolo Bonzini #include "sysemu/dma.h"
3450dcc0f8SGerd Hoffmann #include "trace.h"
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 
78*475443cfSHans de Goede /* Must be large enough to handle 10 frame delay for initial isoc requests */
79*475443cfSHans de Goede #define QH_VALID         32
80*475443cfSHans de Goede 
81f1ae32a1SGerd Hoffmann #define NB_PORTS 2
82f1ae32a1SGerd Hoffmann 
8360e1b2a6SGerd Hoffmann enum {
840cd178caSGerd Hoffmann     TD_RESULT_STOP_FRAME = 10,
850cd178caSGerd Hoffmann     TD_RESULT_COMPLETE,
860cd178caSGerd Hoffmann     TD_RESULT_NEXT_QH,
874efe4ef3SGerd Hoffmann     TD_RESULT_ASYNC_START,
884efe4ef3SGerd Hoffmann     TD_RESULT_ASYNC_CONT,
8960e1b2a6SGerd Hoffmann };
9060e1b2a6SGerd Hoffmann 
91f1ae32a1SGerd Hoffmann typedef struct UHCIState UHCIState;
92f1ae32a1SGerd Hoffmann typedef struct UHCIAsync UHCIAsync;
93f1ae32a1SGerd Hoffmann typedef struct UHCIQueue UHCIQueue;
942c2e8525SGerd Hoffmann typedef struct UHCIInfo UHCIInfo;
958f3f90b0SGerd Hoffmann typedef struct UHCIPCIDeviceClass UHCIPCIDeviceClass;
962c2e8525SGerd Hoffmann 
972c2e8525SGerd Hoffmann struct UHCIInfo {
982c2e8525SGerd Hoffmann     const char *name;
992c2e8525SGerd Hoffmann     uint16_t   vendor_id;
1002c2e8525SGerd Hoffmann     uint16_t   device_id;
1012c2e8525SGerd Hoffmann     uint8_t    revision;
1028f3f90b0SGerd Hoffmann     uint8_t    irq_pin;
1032c2e8525SGerd Hoffmann     int        (*initfn)(PCIDevice *dev);
1042c2e8525SGerd Hoffmann     bool       unplug;
1052c2e8525SGerd Hoffmann };
106f1ae32a1SGerd Hoffmann 
1078f3f90b0SGerd Hoffmann struct UHCIPCIDeviceClass {
1088f3f90b0SGerd Hoffmann     PCIDeviceClass parent_class;
1098f3f90b0SGerd Hoffmann     UHCIInfo       info;
1108f3f90b0SGerd Hoffmann };
1118f3f90b0SGerd Hoffmann 
112f1ae32a1SGerd Hoffmann /*
113f1ae32a1SGerd Hoffmann  * Pending async transaction.
114f1ae32a1SGerd Hoffmann  * 'packet' must be the first field because completion
115f1ae32a1SGerd Hoffmann  * handler does "(UHCIAsync *) pkt" cast.
116f1ae32a1SGerd Hoffmann  */
117f1ae32a1SGerd Hoffmann 
118f1ae32a1SGerd Hoffmann struct UHCIAsync {
119f1ae32a1SGerd Hoffmann     USBPacket packet;
120f1ae32a1SGerd Hoffmann     QEMUSGList sgl;
121f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
122f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIAsync) next;
1231f250cc7SHans de Goede     uint32_t  td_addr;
124f1ae32a1SGerd Hoffmann     uint8_t   done;
125f1ae32a1SGerd Hoffmann };
126f1ae32a1SGerd Hoffmann 
127f1ae32a1SGerd Hoffmann struct UHCIQueue {
12866a08cbeSHans de Goede     uint32_t  qh_addr;
129f1ae32a1SGerd Hoffmann     uint32_t  token;
130f1ae32a1SGerd Hoffmann     UHCIState *uhci;
13111d15e40SHans de Goede     USBEndpoint *ep;
132f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIQueue) next;
1338928c9c4SHans de Goede     QTAILQ_HEAD(asyncs_head, UHCIAsync) asyncs;
134f1ae32a1SGerd Hoffmann     int8_t    valid;
135f1ae32a1SGerd Hoffmann };
136f1ae32a1SGerd Hoffmann 
137f1ae32a1SGerd Hoffmann typedef struct UHCIPort {
138f1ae32a1SGerd Hoffmann     USBPort port;
139f1ae32a1SGerd Hoffmann     uint16_t ctrl;
140f1ae32a1SGerd Hoffmann } UHCIPort;
141f1ae32a1SGerd Hoffmann 
142f1ae32a1SGerd Hoffmann struct UHCIState {
143f1ae32a1SGerd Hoffmann     PCIDevice dev;
144f1ae32a1SGerd Hoffmann     MemoryRegion io_bar;
145f1ae32a1SGerd Hoffmann     USBBus bus; /* Note unused when we're a companion controller */
146f1ae32a1SGerd Hoffmann     uint16_t cmd; /* cmd register */
147f1ae32a1SGerd Hoffmann     uint16_t status;
148f1ae32a1SGerd Hoffmann     uint16_t intr; /* interrupt enable register */
149f1ae32a1SGerd Hoffmann     uint16_t frnum; /* frame number */
150f1ae32a1SGerd Hoffmann     uint32_t fl_base_addr; /* frame list base address */
151f1ae32a1SGerd Hoffmann     uint8_t sof_timing;
152f1ae32a1SGerd Hoffmann     uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
153f1ae32a1SGerd Hoffmann     int64_t expire_time;
154f1ae32a1SGerd Hoffmann     QEMUTimer *frame_timer;
1559a16c595SGerd Hoffmann     QEMUBH *bh;
1564aed20e2SGerd Hoffmann     uint32_t frame_bytes;
15740141d12SGerd Hoffmann     uint32_t frame_bandwidth;
15888793816SHans de Goede     bool completions_only;
159f1ae32a1SGerd Hoffmann     UHCIPort ports[NB_PORTS];
160f1ae32a1SGerd Hoffmann 
161f1ae32a1SGerd Hoffmann     /* Interrupts that should be raised at the end of the current frame.  */
162f1ae32a1SGerd Hoffmann     uint32_t pending_int_mask;
163973002c1SGerd Hoffmann     int irq_pin;
164f1ae32a1SGerd Hoffmann 
165f1ae32a1SGerd Hoffmann     /* Active packets */
166f1ae32a1SGerd Hoffmann     QTAILQ_HEAD(, UHCIQueue) queues;
167f1ae32a1SGerd Hoffmann     uint8_t num_ports_vmstate;
168f1ae32a1SGerd Hoffmann 
169f1ae32a1SGerd Hoffmann     /* Properties */
170f1ae32a1SGerd Hoffmann     char *masterbus;
171f1ae32a1SGerd Hoffmann     uint32_t firstport;
172f1ae32a1SGerd Hoffmann };
173f1ae32a1SGerd Hoffmann 
174f1ae32a1SGerd Hoffmann typedef struct UHCI_TD {
175f1ae32a1SGerd Hoffmann     uint32_t link;
176f1ae32a1SGerd Hoffmann     uint32_t ctrl; /* see TD_CTRL_xxx */
177f1ae32a1SGerd Hoffmann     uint32_t token;
178f1ae32a1SGerd Hoffmann     uint32_t buffer;
179f1ae32a1SGerd Hoffmann } UHCI_TD;
180f1ae32a1SGerd Hoffmann 
181f1ae32a1SGerd Hoffmann typedef struct UHCI_QH {
182f1ae32a1SGerd Hoffmann     uint32_t link;
183f1ae32a1SGerd Hoffmann     uint32_t el_link;
184f1ae32a1SGerd Hoffmann } UHCI_QH;
185f1ae32a1SGerd Hoffmann 
18640507377SHans de Goede static void uhci_async_cancel(UHCIAsync *async);
18711d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
18840507377SHans de Goede 
189f1ae32a1SGerd Hoffmann static inline int32_t uhci_queue_token(UHCI_TD *td)
190f1ae32a1SGerd Hoffmann {
1916fe30910SHans de Goede     if ((td->token & (0xf << 15)) == 0) {
1926fe30910SHans de Goede         /* ctrl ep, cover ep and dev, not pid! */
1936fe30910SHans de Goede         return td->token & 0x7ff00;
1946fe30910SHans de Goede     } else {
195f1ae32a1SGerd Hoffmann         /* covers ep, dev, pid -> identifies the endpoint */
196f1ae32a1SGerd Hoffmann         return td->token & 0x7ffff;
197f1ae32a1SGerd Hoffmann     }
1986fe30910SHans de Goede }
199f1ae32a1SGerd Hoffmann 
20066a08cbeSHans de Goede static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td,
20166a08cbeSHans de Goede                                  USBEndpoint *ep)
202f1ae32a1SGerd Hoffmann {
203f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
204f1ae32a1SGerd Hoffmann 
205f1ae32a1SGerd Hoffmann     queue = g_new0(UHCIQueue, 1);
206f1ae32a1SGerd Hoffmann     queue->uhci = s;
20766a08cbeSHans de Goede     queue->qh_addr = qh_addr;
20866a08cbeSHans de Goede     queue->token = uhci_queue_token(td);
20911d15e40SHans de Goede     queue->ep = ep;
210f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&queue->asyncs);
211f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_HEAD(&s->queues, queue, next);
212*475443cfSHans de Goede     queue->valid = QH_VALID;
21350dcc0f8SGerd Hoffmann     trace_usb_uhci_queue_add(queue->token);
214f1ae32a1SGerd Hoffmann     return queue;
215f1ae32a1SGerd Hoffmann }
216f1ae32a1SGerd Hoffmann 
21766a08cbeSHans de Goede static void uhci_queue_free(UHCIQueue *queue, const char *reason)
218f1ae32a1SGerd Hoffmann {
219f1ae32a1SGerd Hoffmann     UHCIState *s = queue->uhci;
22040507377SHans de Goede     UHCIAsync *async;
22140507377SHans de Goede 
22240507377SHans de Goede     while (!QTAILQ_EMPTY(&queue->asyncs)) {
22340507377SHans de Goede         async = QTAILQ_FIRST(&queue->asyncs);
22440507377SHans de Goede         uhci_async_cancel(async);
22540507377SHans de Goede     }
226f1ae32a1SGerd Hoffmann 
22766a08cbeSHans de Goede     trace_usb_uhci_queue_del(queue->token, reason);
228f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&s->queues, queue, next);
229f1ae32a1SGerd Hoffmann     g_free(queue);
230f1ae32a1SGerd Hoffmann }
231f1ae32a1SGerd Hoffmann 
23266a08cbeSHans de Goede static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td)
23366a08cbeSHans de Goede {
23466a08cbeSHans de Goede     uint32_t token = uhci_queue_token(td);
23566a08cbeSHans de Goede     UHCIQueue *queue;
23666a08cbeSHans de Goede 
23766a08cbeSHans de Goede     QTAILQ_FOREACH(queue, &s->queues, next) {
23866a08cbeSHans de Goede         if (queue->token == token) {
23966a08cbeSHans de Goede             return queue;
24066a08cbeSHans de Goede         }
24166a08cbeSHans de Goede     }
24266a08cbeSHans de Goede     return NULL;
24366a08cbeSHans de Goede }
24466a08cbeSHans de Goede 
24566a08cbeSHans de Goede static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td,
24666a08cbeSHans de Goede                               uint32_t td_addr, bool queuing)
24766a08cbeSHans de Goede {
24866a08cbeSHans de Goede     UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs);
24966a08cbeSHans de Goede 
25066a08cbeSHans de Goede     return queue->qh_addr == qh_addr &&
25166a08cbeSHans de Goede            queue->token == uhci_queue_token(td) &&
25266a08cbeSHans de Goede            (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL ||
25366a08cbeSHans de Goede             first->td_addr == td_addr);
25466a08cbeSHans de Goede }
25566a08cbeSHans de Goede 
2561f250cc7SHans de Goede static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
257f1ae32a1SGerd Hoffmann {
258f1ae32a1SGerd Hoffmann     UHCIAsync *async = g_new0(UHCIAsync, 1);
259f1ae32a1SGerd Hoffmann 
260f1ae32a1SGerd Hoffmann     async->queue = queue;
2611f250cc7SHans de Goede     async->td_addr = td_addr;
262f1ae32a1SGerd Hoffmann     usb_packet_init(&async->packet);
263f1ae32a1SGerd Hoffmann     pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
2641f250cc7SHans de Goede     trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
265f1ae32a1SGerd Hoffmann 
266f1ae32a1SGerd Hoffmann     return async;
267f1ae32a1SGerd Hoffmann }
268f1ae32a1SGerd Hoffmann 
269f1ae32a1SGerd Hoffmann static void uhci_async_free(UHCIAsync *async)
270f1ae32a1SGerd Hoffmann {
2711f250cc7SHans de Goede     trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
272f1ae32a1SGerd Hoffmann     usb_packet_cleanup(&async->packet);
273f1ae32a1SGerd Hoffmann     qemu_sglist_destroy(&async->sgl);
274f1ae32a1SGerd Hoffmann     g_free(async);
275f1ae32a1SGerd Hoffmann }
276f1ae32a1SGerd Hoffmann 
277f1ae32a1SGerd Hoffmann static void uhci_async_link(UHCIAsync *async)
278f1ae32a1SGerd Hoffmann {
279f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
280f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
2811f250cc7SHans de Goede     trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
282f1ae32a1SGerd Hoffmann }
283f1ae32a1SGerd Hoffmann 
284f1ae32a1SGerd Hoffmann static void uhci_async_unlink(UHCIAsync *async)
285f1ae32a1SGerd Hoffmann {
286f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
287f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&queue->asyncs, async, next);
2881f250cc7SHans de Goede     trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
289f1ae32a1SGerd Hoffmann }
290f1ae32a1SGerd Hoffmann 
291f1ae32a1SGerd Hoffmann static void uhci_async_cancel(UHCIAsync *async)
292f1ae32a1SGerd Hoffmann {
2932f2ee268SHans de Goede     uhci_async_unlink(async);
2941f250cc7SHans de Goede     trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
2951f250cc7SHans de Goede                                  async->done);
296f1ae32a1SGerd Hoffmann     if (!async->done)
297f1ae32a1SGerd Hoffmann         usb_cancel_packet(&async->packet);
29800a0770dSHans de Goede     usb_packet_unmap(&async->packet, &async->sgl);
299f1ae32a1SGerd Hoffmann     uhci_async_free(async);
300f1ae32a1SGerd Hoffmann }
301f1ae32a1SGerd Hoffmann 
302f1ae32a1SGerd Hoffmann /*
303f1ae32a1SGerd Hoffmann  * Mark all outstanding async packets as invalid.
304f1ae32a1SGerd Hoffmann  * This is used for canceling them when TDs are removed by the HCD.
305f1ae32a1SGerd Hoffmann  */
306f1ae32a1SGerd Hoffmann static void uhci_async_validate_begin(UHCIState *s)
307f1ae32a1SGerd Hoffmann {
308f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
309f1ae32a1SGerd Hoffmann 
310f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
311f1ae32a1SGerd Hoffmann         queue->valid--;
312f1ae32a1SGerd Hoffmann     }
313f1ae32a1SGerd Hoffmann }
314f1ae32a1SGerd Hoffmann 
315f1ae32a1SGerd Hoffmann /*
316f1ae32a1SGerd Hoffmann  * Cancel async packets that are no longer valid
317f1ae32a1SGerd Hoffmann  */
318f1ae32a1SGerd Hoffmann static void uhci_async_validate_end(UHCIState *s)
319f1ae32a1SGerd Hoffmann {
320f1ae32a1SGerd Hoffmann     UHCIQueue *queue, *n;
321f1ae32a1SGerd Hoffmann 
322f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
32340507377SHans de Goede         if (!queue->valid) {
32466a08cbeSHans de Goede             uhci_queue_free(queue, "validate-end");
325f1ae32a1SGerd Hoffmann         }
326f1ae32a1SGerd Hoffmann     }
32740507377SHans de Goede }
328f1ae32a1SGerd Hoffmann 
329f1ae32a1SGerd Hoffmann static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
330f1ae32a1SGerd Hoffmann {
3315ad23e87SHans de Goede     UHCIQueue *queue, *n;
332f1ae32a1SGerd Hoffmann 
3335ad23e87SHans de Goede     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
3345ad23e87SHans de Goede         if (queue->ep->dev == dev) {
3355ad23e87SHans de Goede             uhci_queue_free(queue, "cancel-device");
336f1ae32a1SGerd Hoffmann         }
337f1ae32a1SGerd Hoffmann     }
338f1ae32a1SGerd Hoffmann }
339f1ae32a1SGerd Hoffmann 
340f1ae32a1SGerd Hoffmann static void uhci_async_cancel_all(UHCIState *s)
341f1ae32a1SGerd Hoffmann {
34277fa9aeeSGerd Hoffmann     UHCIQueue *queue, *nq;
343f1ae32a1SGerd Hoffmann 
34477fa9aeeSGerd Hoffmann     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
34566a08cbeSHans de Goede         uhci_queue_free(queue, "cancel-all");
346f1ae32a1SGerd Hoffmann     }
347f1ae32a1SGerd Hoffmann }
348f1ae32a1SGerd Hoffmann 
3498c75a899SHans de Goede static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr)
350f1ae32a1SGerd Hoffmann {
351f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
352f1ae32a1SGerd Hoffmann     UHCIAsync *async;
353f1ae32a1SGerd Hoffmann 
354f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
355f1ae32a1SGerd Hoffmann         QTAILQ_FOREACH(async, &queue->asyncs, next) {
3561f250cc7SHans de Goede             if (async->td_addr == td_addr) {
357f1ae32a1SGerd Hoffmann                 return async;
358f1ae32a1SGerd Hoffmann             }
359f1ae32a1SGerd Hoffmann         }
3608c75a899SHans de Goede     }
361f1ae32a1SGerd Hoffmann     return NULL;
362f1ae32a1SGerd Hoffmann }
363f1ae32a1SGerd Hoffmann 
364f1ae32a1SGerd Hoffmann static void uhci_update_irq(UHCIState *s)
365f1ae32a1SGerd Hoffmann {
366f1ae32a1SGerd Hoffmann     int level;
367f1ae32a1SGerd Hoffmann     if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
368f1ae32a1SGerd Hoffmann         ((s->status2 & 2) && (s->intr & (1 << 3))) ||
369f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
370f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
371f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HSERR) ||
372f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HCPERR)) {
373f1ae32a1SGerd Hoffmann         level = 1;
374f1ae32a1SGerd Hoffmann     } else {
375f1ae32a1SGerd Hoffmann         level = 0;
376f1ae32a1SGerd Hoffmann     }
377973002c1SGerd Hoffmann     qemu_set_irq(s->dev.irq[s->irq_pin], level);
378f1ae32a1SGerd Hoffmann }
379f1ae32a1SGerd Hoffmann 
380f1ae32a1SGerd Hoffmann static void uhci_reset(void *opaque)
381f1ae32a1SGerd Hoffmann {
382f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
383f1ae32a1SGerd Hoffmann     uint8_t *pci_conf;
384f1ae32a1SGerd Hoffmann     int i;
385f1ae32a1SGerd Hoffmann     UHCIPort *port;
386f1ae32a1SGerd Hoffmann 
38750dcc0f8SGerd Hoffmann     trace_usb_uhci_reset();
388f1ae32a1SGerd Hoffmann 
389f1ae32a1SGerd Hoffmann     pci_conf = s->dev.config;
390f1ae32a1SGerd Hoffmann 
391f1ae32a1SGerd Hoffmann     pci_conf[0x6a] = 0x01; /* usb clock */
392f1ae32a1SGerd Hoffmann     pci_conf[0x6b] = 0x00;
393f1ae32a1SGerd Hoffmann     s->cmd = 0;
394f1ae32a1SGerd Hoffmann     s->status = 0;
395f1ae32a1SGerd Hoffmann     s->status2 = 0;
396f1ae32a1SGerd Hoffmann     s->intr = 0;
397f1ae32a1SGerd Hoffmann     s->fl_base_addr = 0;
398f1ae32a1SGerd Hoffmann     s->sof_timing = 64;
399f1ae32a1SGerd Hoffmann 
400f1ae32a1SGerd Hoffmann     for(i = 0; i < NB_PORTS; i++) {
401f1ae32a1SGerd Hoffmann         port = &s->ports[i];
402f1ae32a1SGerd Hoffmann         port->ctrl = 0x0080;
403f1ae32a1SGerd Hoffmann         if (port->port.dev && port->port.dev->attached) {
404f1ae32a1SGerd Hoffmann             usb_port_reset(&port->port);
405f1ae32a1SGerd Hoffmann         }
406f1ae32a1SGerd Hoffmann     }
407f1ae32a1SGerd Hoffmann 
408f1ae32a1SGerd Hoffmann     uhci_async_cancel_all(s);
4099a16c595SGerd Hoffmann     qemu_bh_cancel(s->bh);
410aba1f242SGerd Hoffmann     uhci_update_irq(s);
411f1ae32a1SGerd Hoffmann }
412f1ae32a1SGerd Hoffmann 
413f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci_port = {
414f1ae32a1SGerd Hoffmann     .name = "uhci port",
415f1ae32a1SGerd Hoffmann     .version_id = 1,
416f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
417f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
418f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
419f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(ctrl, UHCIPort),
420f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
421f1ae32a1SGerd Hoffmann     }
422f1ae32a1SGerd Hoffmann };
423f1ae32a1SGerd Hoffmann 
42475f151cdSGerd Hoffmann static int uhci_post_load(void *opaque, int version_id)
42575f151cdSGerd Hoffmann {
42675f151cdSGerd Hoffmann     UHCIState *s = opaque;
42775f151cdSGerd Hoffmann 
42875f151cdSGerd Hoffmann     if (version_id < 2) {
42975f151cdSGerd Hoffmann         s->expire_time = qemu_get_clock_ns(vm_clock) +
43075f151cdSGerd Hoffmann             (get_ticks_per_sec() / FRAME_TIMER_FREQ);
43175f151cdSGerd Hoffmann     }
43275f151cdSGerd Hoffmann     return 0;
43375f151cdSGerd Hoffmann }
43475f151cdSGerd Hoffmann 
435f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci = {
436f1ae32a1SGerd Hoffmann     .name = "uhci",
437ecfdc15fSHans de Goede     .version_id = 3,
438f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
439f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
44075f151cdSGerd Hoffmann     .post_load = uhci_post_load,
441f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
442f1ae32a1SGerd Hoffmann         VMSTATE_PCI_DEVICE(dev, UHCIState),
443f1ae32a1SGerd Hoffmann         VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
444f1ae32a1SGerd Hoffmann         VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
445f1ae32a1SGerd Hoffmann                              vmstate_uhci_port, UHCIPort),
446f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(cmd, UHCIState),
447f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(status, UHCIState),
448f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(intr, UHCIState),
449f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(frnum, UHCIState),
450f1ae32a1SGerd Hoffmann         VMSTATE_UINT32(fl_base_addr, UHCIState),
451f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(sof_timing, UHCIState),
452f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(status2, UHCIState),
453f1ae32a1SGerd Hoffmann         VMSTATE_TIMER(frame_timer, UHCIState),
454f1ae32a1SGerd Hoffmann         VMSTATE_INT64_V(expire_time, UHCIState, 2),
455ecfdc15fSHans de Goede         VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3),
456f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
457f1ae32a1SGerd Hoffmann     }
458f1ae32a1SGerd Hoffmann };
459f1ae32a1SGerd Hoffmann 
460f1ae32a1SGerd Hoffmann static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
461f1ae32a1SGerd Hoffmann {
462f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
463f1ae32a1SGerd Hoffmann 
464f1ae32a1SGerd Hoffmann     addr &= 0x1f;
465f1ae32a1SGerd Hoffmann     switch(addr) {
466f1ae32a1SGerd Hoffmann     case 0x0c:
467f1ae32a1SGerd Hoffmann         s->sof_timing = val;
468f1ae32a1SGerd Hoffmann         break;
469f1ae32a1SGerd Hoffmann     }
470f1ae32a1SGerd Hoffmann }
471f1ae32a1SGerd Hoffmann 
472f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
473f1ae32a1SGerd Hoffmann {
474f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
475f1ae32a1SGerd Hoffmann     uint32_t val;
476f1ae32a1SGerd Hoffmann 
477f1ae32a1SGerd Hoffmann     addr &= 0x1f;
478f1ae32a1SGerd Hoffmann     switch(addr) {
479f1ae32a1SGerd Hoffmann     case 0x0c:
480f1ae32a1SGerd Hoffmann         val = s->sof_timing;
481f1ae32a1SGerd Hoffmann         break;
482f1ae32a1SGerd Hoffmann     default:
483f1ae32a1SGerd Hoffmann         val = 0xff;
484f1ae32a1SGerd Hoffmann         break;
485f1ae32a1SGerd Hoffmann     }
486f1ae32a1SGerd Hoffmann     return val;
487f1ae32a1SGerd Hoffmann }
488f1ae32a1SGerd Hoffmann 
489f1ae32a1SGerd Hoffmann static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
490f1ae32a1SGerd Hoffmann {
491f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
492f1ae32a1SGerd Hoffmann 
493f1ae32a1SGerd Hoffmann     addr &= 0x1f;
49450dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_writew(addr, val);
495f1ae32a1SGerd Hoffmann 
496f1ae32a1SGerd Hoffmann     switch(addr) {
497f1ae32a1SGerd Hoffmann     case 0x00:
498f1ae32a1SGerd Hoffmann         if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
499f1ae32a1SGerd Hoffmann             /* start frame processing */
50050dcc0f8SGerd Hoffmann             trace_usb_uhci_schedule_start();
501f1ae32a1SGerd Hoffmann             s->expire_time = qemu_get_clock_ns(vm_clock) +
502f1ae32a1SGerd Hoffmann                 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
503f1ae32a1SGerd Hoffmann             qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
504f1ae32a1SGerd Hoffmann             s->status &= ~UHCI_STS_HCHALTED;
505f1ae32a1SGerd Hoffmann         } else if (!(val & UHCI_CMD_RS)) {
506f1ae32a1SGerd Hoffmann             s->status |= UHCI_STS_HCHALTED;
507f1ae32a1SGerd Hoffmann         }
508f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_GRESET) {
509f1ae32a1SGerd Hoffmann             UHCIPort *port;
510f1ae32a1SGerd Hoffmann             int i;
511f1ae32a1SGerd Hoffmann 
512f1ae32a1SGerd Hoffmann             /* send reset on the USB bus */
513f1ae32a1SGerd Hoffmann             for(i = 0; i < NB_PORTS; i++) {
514f1ae32a1SGerd Hoffmann                 port = &s->ports[i];
515f1ae32a1SGerd Hoffmann                 usb_device_reset(port->port.dev);
516f1ae32a1SGerd Hoffmann             }
517f1ae32a1SGerd Hoffmann             uhci_reset(s);
518f1ae32a1SGerd Hoffmann             return;
519f1ae32a1SGerd Hoffmann         }
520f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_HCRESET) {
521f1ae32a1SGerd Hoffmann             uhci_reset(s);
522f1ae32a1SGerd Hoffmann             return;
523f1ae32a1SGerd Hoffmann         }
524f1ae32a1SGerd Hoffmann         s->cmd = val;
525f1ae32a1SGerd Hoffmann         break;
526f1ae32a1SGerd Hoffmann     case 0x02:
527f1ae32a1SGerd Hoffmann         s->status &= ~val;
528f1ae32a1SGerd Hoffmann         /* XXX: the chip spec is not coherent, so we add a hidden
529f1ae32a1SGerd Hoffmann            register to distinguish between IOC and SPD */
530f1ae32a1SGerd Hoffmann         if (val & UHCI_STS_USBINT)
531f1ae32a1SGerd Hoffmann             s->status2 = 0;
532f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
533f1ae32a1SGerd Hoffmann         break;
534f1ae32a1SGerd Hoffmann     case 0x04:
535f1ae32a1SGerd Hoffmann         s->intr = val;
536f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
537f1ae32a1SGerd Hoffmann         break;
538f1ae32a1SGerd Hoffmann     case 0x06:
539f1ae32a1SGerd Hoffmann         if (s->status & UHCI_STS_HCHALTED)
540f1ae32a1SGerd Hoffmann             s->frnum = val & 0x7ff;
541f1ae32a1SGerd Hoffmann         break;
542f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
543f1ae32a1SGerd Hoffmann         {
544f1ae32a1SGerd Hoffmann             UHCIPort *port;
545f1ae32a1SGerd Hoffmann             USBDevice *dev;
546f1ae32a1SGerd Hoffmann             int n;
547f1ae32a1SGerd Hoffmann 
548f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
549f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
550f1ae32a1SGerd Hoffmann                 return;
551f1ae32a1SGerd Hoffmann             port = &s->ports[n];
552f1ae32a1SGerd Hoffmann             dev = port->port.dev;
553f1ae32a1SGerd Hoffmann             if (dev && dev->attached) {
554f1ae32a1SGerd Hoffmann                 /* port reset */
555f1ae32a1SGerd Hoffmann                 if ( (val & UHCI_PORT_RESET) &&
556f1ae32a1SGerd Hoffmann                      !(port->ctrl & UHCI_PORT_RESET) ) {
557f1ae32a1SGerd Hoffmann                     usb_device_reset(dev);
558f1ae32a1SGerd Hoffmann                 }
559f1ae32a1SGerd Hoffmann             }
560f1ae32a1SGerd Hoffmann             port->ctrl &= UHCI_PORT_READ_ONLY;
5611cbdde90SHans de Goede             /* enabled may only be set if a device is connected */
5621cbdde90SHans de Goede             if (!(port->ctrl & UHCI_PORT_CCS)) {
5631cbdde90SHans de Goede                 val &= ~UHCI_PORT_EN;
5641cbdde90SHans de Goede             }
565f1ae32a1SGerd Hoffmann             port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
566f1ae32a1SGerd Hoffmann             /* some bits are reset when a '1' is written to them */
567f1ae32a1SGerd Hoffmann             port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
568f1ae32a1SGerd Hoffmann         }
569f1ae32a1SGerd Hoffmann         break;
570f1ae32a1SGerd Hoffmann     }
571f1ae32a1SGerd Hoffmann }
572f1ae32a1SGerd Hoffmann 
573f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
574f1ae32a1SGerd Hoffmann {
575f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
576f1ae32a1SGerd Hoffmann     uint32_t val;
577f1ae32a1SGerd Hoffmann 
578f1ae32a1SGerd Hoffmann     addr &= 0x1f;
579f1ae32a1SGerd Hoffmann     switch(addr) {
580f1ae32a1SGerd Hoffmann     case 0x00:
581f1ae32a1SGerd Hoffmann         val = s->cmd;
582f1ae32a1SGerd Hoffmann         break;
583f1ae32a1SGerd Hoffmann     case 0x02:
584f1ae32a1SGerd Hoffmann         val = s->status;
585f1ae32a1SGerd Hoffmann         break;
586f1ae32a1SGerd Hoffmann     case 0x04:
587f1ae32a1SGerd Hoffmann         val = s->intr;
588f1ae32a1SGerd Hoffmann         break;
589f1ae32a1SGerd Hoffmann     case 0x06:
590f1ae32a1SGerd Hoffmann         val = s->frnum;
591f1ae32a1SGerd Hoffmann         break;
592f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
593f1ae32a1SGerd Hoffmann         {
594f1ae32a1SGerd Hoffmann             UHCIPort *port;
595f1ae32a1SGerd Hoffmann             int n;
596f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
597f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
598f1ae32a1SGerd Hoffmann                 goto read_default;
599f1ae32a1SGerd Hoffmann             port = &s->ports[n];
600f1ae32a1SGerd Hoffmann             val = port->ctrl;
601f1ae32a1SGerd Hoffmann         }
602f1ae32a1SGerd Hoffmann         break;
603f1ae32a1SGerd Hoffmann     default:
604f1ae32a1SGerd Hoffmann     read_default:
605f1ae32a1SGerd Hoffmann         val = 0xff7f; /* disabled port */
606f1ae32a1SGerd Hoffmann         break;
607f1ae32a1SGerd Hoffmann     }
608f1ae32a1SGerd Hoffmann 
60950dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_readw(addr, val);
610f1ae32a1SGerd Hoffmann 
611f1ae32a1SGerd Hoffmann     return val;
612f1ae32a1SGerd Hoffmann }
613f1ae32a1SGerd Hoffmann 
614f1ae32a1SGerd Hoffmann static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
615f1ae32a1SGerd Hoffmann {
616f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
617f1ae32a1SGerd Hoffmann 
618f1ae32a1SGerd Hoffmann     addr &= 0x1f;
61950dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_writel(addr, val);
620f1ae32a1SGerd Hoffmann 
621f1ae32a1SGerd Hoffmann     switch(addr) {
622f1ae32a1SGerd Hoffmann     case 0x08:
623f1ae32a1SGerd Hoffmann         s->fl_base_addr = val & ~0xfff;
624f1ae32a1SGerd Hoffmann         break;
625f1ae32a1SGerd Hoffmann     }
626f1ae32a1SGerd Hoffmann }
627f1ae32a1SGerd Hoffmann 
628f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
629f1ae32a1SGerd Hoffmann {
630f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
631f1ae32a1SGerd Hoffmann     uint32_t val;
632f1ae32a1SGerd Hoffmann 
633f1ae32a1SGerd Hoffmann     addr &= 0x1f;
634f1ae32a1SGerd Hoffmann     switch(addr) {
635f1ae32a1SGerd Hoffmann     case 0x08:
636f1ae32a1SGerd Hoffmann         val = s->fl_base_addr;
637f1ae32a1SGerd Hoffmann         break;
638f1ae32a1SGerd Hoffmann     default:
639f1ae32a1SGerd Hoffmann         val = 0xffffffff;
640f1ae32a1SGerd Hoffmann         break;
641f1ae32a1SGerd Hoffmann     }
64250dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_readl(addr, val);
643f1ae32a1SGerd Hoffmann     return val;
644f1ae32a1SGerd Hoffmann }
645f1ae32a1SGerd Hoffmann 
646f1ae32a1SGerd Hoffmann /* signal resume if controller suspended */
647f1ae32a1SGerd Hoffmann static void uhci_resume (void *opaque)
648f1ae32a1SGerd Hoffmann {
649f1ae32a1SGerd Hoffmann     UHCIState *s = (UHCIState *)opaque;
650f1ae32a1SGerd Hoffmann 
651f1ae32a1SGerd Hoffmann     if (!s)
652f1ae32a1SGerd Hoffmann         return;
653f1ae32a1SGerd Hoffmann 
654f1ae32a1SGerd Hoffmann     if (s->cmd & UHCI_CMD_EGSM) {
655f1ae32a1SGerd Hoffmann         s->cmd |= UHCI_CMD_FGR;
656f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_RD;
657f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
658f1ae32a1SGerd Hoffmann     }
659f1ae32a1SGerd Hoffmann }
660f1ae32a1SGerd Hoffmann 
661f1ae32a1SGerd Hoffmann static void uhci_attach(USBPort *port1)
662f1ae32a1SGerd Hoffmann {
663f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
664f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
665f1ae32a1SGerd Hoffmann 
666f1ae32a1SGerd Hoffmann     /* set connect status */
667f1ae32a1SGerd Hoffmann     port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
668f1ae32a1SGerd Hoffmann 
669f1ae32a1SGerd Hoffmann     /* update speed */
670f1ae32a1SGerd Hoffmann     if (port->port.dev->speed == USB_SPEED_LOW) {
671f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_LSDA;
672f1ae32a1SGerd Hoffmann     } else {
673f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_LSDA;
674f1ae32a1SGerd Hoffmann     }
675f1ae32a1SGerd Hoffmann 
676f1ae32a1SGerd Hoffmann     uhci_resume(s);
677f1ae32a1SGerd Hoffmann }
678f1ae32a1SGerd Hoffmann 
679f1ae32a1SGerd Hoffmann static void uhci_detach(USBPort *port1)
680f1ae32a1SGerd Hoffmann {
681f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
682f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
683f1ae32a1SGerd Hoffmann 
684f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, port1->dev);
685f1ae32a1SGerd Hoffmann 
686f1ae32a1SGerd Hoffmann     /* set connect status */
687f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_CCS) {
688f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_CCS;
689f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_CSC;
690f1ae32a1SGerd Hoffmann     }
691f1ae32a1SGerd Hoffmann     /* disable port */
692f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_EN) {
693f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_EN;
694f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_ENC;
695f1ae32a1SGerd Hoffmann     }
696f1ae32a1SGerd Hoffmann 
697f1ae32a1SGerd Hoffmann     uhci_resume(s);
698f1ae32a1SGerd Hoffmann }
699f1ae32a1SGerd Hoffmann 
700f1ae32a1SGerd Hoffmann static void uhci_child_detach(USBPort *port1, USBDevice *child)
701f1ae32a1SGerd Hoffmann {
702f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
703f1ae32a1SGerd Hoffmann 
704f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, child);
705f1ae32a1SGerd Hoffmann }
706f1ae32a1SGerd Hoffmann 
707f1ae32a1SGerd Hoffmann static void uhci_wakeup(USBPort *port1)
708f1ae32a1SGerd Hoffmann {
709f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
710f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
711f1ae32a1SGerd Hoffmann 
712f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
713f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_RD;
714f1ae32a1SGerd Hoffmann         uhci_resume(s);
715f1ae32a1SGerd Hoffmann     }
716f1ae32a1SGerd Hoffmann }
717f1ae32a1SGerd Hoffmann 
718f1ae32a1SGerd Hoffmann static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
719f1ae32a1SGerd Hoffmann {
720f1ae32a1SGerd Hoffmann     USBDevice *dev;
721f1ae32a1SGerd Hoffmann     int i;
722f1ae32a1SGerd Hoffmann 
723f1ae32a1SGerd Hoffmann     for (i = 0; i < NB_PORTS; i++) {
724f1ae32a1SGerd Hoffmann         UHCIPort *port = &s->ports[i];
725f1ae32a1SGerd Hoffmann         if (!(port->ctrl & UHCI_PORT_EN)) {
726f1ae32a1SGerd Hoffmann             continue;
727f1ae32a1SGerd Hoffmann         }
728f1ae32a1SGerd Hoffmann         dev = usb_find_device(&port->port, addr);
729f1ae32a1SGerd Hoffmann         if (dev != NULL) {
730f1ae32a1SGerd Hoffmann             return dev;
731f1ae32a1SGerd Hoffmann         }
732f1ae32a1SGerd Hoffmann     }
733f1ae32a1SGerd Hoffmann     return NULL;
734f1ae32a1SGerd Hoffmann }
735f1ae32a1SGerd Hoffmann 
736963a68b5SHans de Goede static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
737963a68b5SHans de Goede {
738963a68b5SHans de Goede     pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
739963a68b5SHans de Goede     le32_to_cpus(&td->link);
740963a68b5SHans de Goede     le32_to_cpus(&td->ctrl);
741963a68b5SHans de Goede     le32_to_cpus(&td->token);
742963a68b5SHans de Goede     le32_to_cpus(&td->buffer);
743963a68b5SHans de Goede }
744963a68b5SHans de Goede 
745faccca00SHans de Goede static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr,
746faccca00SHans de Goede                                 int status, uint32_t *int_mask)
747faccca00SHans de Goede {
748faccca00SHans de Goede     uint32_t queue_token = uhci_queue_token(td);
749faccca00SHans de Goede     int ret;
750faccca00SHans de Goede 
751faccca00SHans de Goede     switch (status) {
752faccca00SHans de Goede     case USB_RET_NAK:
753faccca00SHans de Goede         td->ctrl |= TD_CTRL_NAK;
754faccca00SHans de Goede         return TD_RESULT_NEXT_QH;
755faccca00SHans de Goede 
756faccca00SHans de Goede     case USB_RET_STALL:
757faccca00SHans de Goede         td->ctrl |= TD_CTRL_STALL;
758faccca00SHans de Goede         trace_usb_uhci_packet_complete_stall(queue_token, td_addr);
759faccca00SHans de Goede         ret = TD_RESULT_NEXT_QH;
760faccca00SHans de Goede         break;
761faccca00SHans de Goede 
762faccca00SHans de Goede     case USB_RET_BABBLE:
763faccca00SHans de Goede         td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
764faccca00SHans de Goede         /* frame interrupted */
765faccca00SHans de Goede         trace_usb_uhci_packet_complete_babble(queue_token, td_addr);
766faccca00SHans de Goede         ret = TD_RESULT_STOP_FRAME;
767faccca00SHans de Goede         break;
768faccca00SHans de Goede 
769faccca00SHans de Goede     case USB_RET_IOERROR:
770faccca00SHans de Goede     case USB_RET_NODEV:
771faccca00SHans de Goede     default:
772faccca00SHans de Goede         td->ctrl |= TD_CTRL_TIMEOUT;
773faccca00SHans de Goede         td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
774faccca00SHans de Goede         trace_usb_uhci_packet_complete_error(queue_token, td_addr);
775faccca00SHans de Goede         ret = TD_RESULT_NEXT_QH;
776faccca00SHans de Goede         break;
777faccca00SHans de Goede     }
778faccca00SHans de Goede 
779faccca00SHans de Goede     td->ctrl &= ~TD_CTRL_ACTIVE;
780faccca00SHans de Goede     s->status |= UHCI_STS_USBERR;
781faccca00SHans de Goede     if (td->ctrl & TD_CTRL_IOC) {
782faccca00SHans de Goede         *int_mask |= 0x01;
783faccca00SHans de Goede     }
784faccca00SHans de Goede     uhci_update_irq(s);
785faccca00SHans de Goede     return ret;
786faccca00SHans de Goede }
787faccca00SHans de Goede 
788f1ae32a1SGerd Hoffmann static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
789f1ae32a1SGerd Hoffmann {
7909a77a0f5SHans de Goede     int len = 0, max_len;
791f1ae32a1SGerd Hoffmann     uint8_t pid;
792f1ae32a1SGerd Hoffmann 
793f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
794f1ae32a1SGerd Hoffmann     pid = td->token & 0xff;
795f1ae32a1SGerd Hoffmann 
796f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOS)
797f1ae32a1SGerd Hoffmann         td->ctrl &= ~TD_CTRL_ACTIVE;
798f1ae32a1SGerd Hoffmann 
7999a77a0f5SHans de Goede     if (async->packet.status != USB_RET_SUCCESS) {
8009a77a0f5SHans de Goede         return uhci_handle_td_error(s, td, async->td_addr,
8019a77a0f5SHans de Goede                                     async->packet.status, int_mask);
802faccca00SHans de Goede     }
803f1ae32a1SGerd Hoffmann 
8049a77a0f5SHans de Goede     len = async->packet.actual_length;
805f1ae32a1SGerd Hoffmann     td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
806f1ae32a1SGerd Hoffmann 
807f1ae32a1SGerd Hoffmann     /* The NAK bit may have been set by a previous frame, so clear it
808f1ae32a1SGerd Hoffmann        here.  The docs are somewhat unclear, but win2k relies on this
809f1ae32a1SGerd Hoffmann        behavior.  */
810f1ae32a1SGerd Hoffmann     td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
811f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOC)
812f1ae32a1SGerd Hoffmann         *int_mask |= 0x01;
813f1ae32a1SGerd Hoffmann 
814f1ae32a1SGerd Hoffmann     if (pid == USB_TOKEN_IN) {
815f1ae32a1SGerd Hoffmann         if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
816f1ae32a1SGerd Hoffmann             *int_mask |= 0x02;
817f1ae32a1SGerd Hoffmann             /* short packet: do not update QH */
81850dcc0f8SGerd Hoffmann             trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
8191f250cc7SHans de Goede                                                      async->td_addr);
82060e1b2a6SGerd Hoffmann             return TD_RESULT_NEXT_QH;
821f1ae32a1SGerd Hoffmann         }
822f1ae32a1SGerd Hoffmann     }
823f1ae32a1SGerd Hoffmann 
824f1ae32a1SGerd Hoffmann     /* success */
8251f250cc7SHans de Goede     trace_usb_uhci_packet_complete_success(async->queue->token,
8261f250cc7SHans de Goede                                            async->td_addr);
82760e1b2a6SGerd Hoffmann     return TD_RESULT_COMPLETE;
828f1ae32a1SGerd Hoffmann }
829f1ae32a1SGerd Hoffmann 
83066a08cbeSHans de Goede static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
831a4f30cd7SHans de Goede                           UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
832f1ae32a1SGerd Hoffmann {
8339a77a0f5SHans de Goede     int ret, max_len;
8346ba43f1fSHans de Goede     bool spd;
835a4f30cd7SHans de Goede     bool queuing = (q != NULL);
83611d15e40SHans de Goede     uint8_t pid = td->token & 0xff;
8378c75a899SHans de Goede     UHCIAsync *async = uhci_async_find_td(s, td_addr);
8388c75a899SHans de Goede 
8398c75a899SHans de Goede     if (async) {
8408c75a899SHans de Goede         if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) {
8418c75a899SHans de Goede             assert(q == NULL || q == async->queue);
8428c75a899SHans de Goede             q = async->queue;
8438c75a899SHans de Goede         } else {
8448c75a899SHans de Goede             uhci_queue_free(async->queue, "guest re-used pending td");
8458c75a899SHans de Goede             async = NULL;
8468c75a899SHans de Goede         }
8478c75a899SHans de Goede     }
848f1ae32a1SGerd Hoffmann 
84966a08cbeSHans de Goede     if (q == NULL) {
85066a08cbeSHans de Goede         q = uhci_queue_find(s, td);
85166a08cbeSHans de Goede         if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) {
85266a08cbeSHans de Goede             uhci_queue_free(q, "guest re-used qh");
85366a08cbeSHans de Goede             q = NULL;
85466a08cbeSHans de Goede         }
85566a08cbeSHans de Goede     }
85666a08cbeSHans de Goede 
8573905097eSHans de Goede     if (q) {
858*475443cfSHans de Goede         q->valid = QH_VALID;
8593905097eSHans de Goede     }
8603905097eSHans de Goede 
861f1ae32a1SGerd Hoffmann     /* Is active ? */
862883bca77SHans de Goede     if (!(td->ctrl & TD_CTRL_ACTIVE)) {
863420ca987SHans de Goede         if (async) {
864420ca987SHans de Goede             /* Guest marked a pending td non-active, cancel the queue */
865420ca987SHans de Goede             uhci_queue_free(async->queue, "pending td non-active");
866420ca987SHans de Goede         }
867883bca77SHans de Goede         /*
868883bca77SHans de Goede          * ehci11d spec page 22: "Even if the Active bit in the TD is already
869883bca77SHans de Goede          * cleared when the TD is fetched ... an IOC interrupt is generated"
870883bca77SHans de Goede          */
871883bca77SHans de Goede         if (td->ctrl & TD_CTRL_IOC) {
872883bca77SHans de Goede                 *int_mask |= 0x01;
873883bca77SHans de Goede         }
87460e1b2a6SGerd Hoffmann         return TD_RESULT_NEXT_QH;
875883bca77SHans de Goede     }
876f1ae32a1SGerd Hoffmann 
877f1ae32a1SGerd Hoffmann     if (async) {
878ee008ba6SGerd Hoffmann         if (queuing) {
879ee008ba6SGerd Hoffmann             /* we are busy filling the queue, we are not prepared
880ee008ba6SGerd Hoffmann                to consume completed packages then, just leave them
881ee008ba6SGerd Hoffmann                in async state */
882ee008ba6SGerd Hoffmann             return TD_RESULT_ASYNC_CONT;
883ee008ba6SGerd Hoffmann         }
8848928c9c4SHans de Goede         if (!async->done) {
8858928c9c4SHans de Goede             UHCI_TD last_td;
8868928c9c4SHans de Goede             UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs, asyncs_head);
8878928c9c4SHans de Goede             /*
8888928c9c4SHans de Goede              * While we are waiting for the current td to complete, the guest
8898928c9c4SHans de Goede              * may have added more tds to the queue. Note we re-read the td
8908928c9c4SHans de Goede              * rather then caching it, as we want to see guest made changes!
8918928c9c4SHans de Goede              */
8928928c9c4SHans de Goede             uhci_read_td(s, &last_td, last->td_addr);
8938928c9c4SHans de Goede             uhci_queue_fill(async->queue, &last_td);
894f1ae32a1SGerd Hoffmann 
8958928c9c4SHans de Goede             return TD_RESULT_ASYNC_CONT;
8968928c9c4SHans de Goede         }
897f1ae32a1SGerd Hoffmann         uhci_async_unlink(async);
898f1ae32a1SGerd Hoffmann         goto done;
899f1ae32a1SGerd Hoffmann     }
900f1ae32a1SGerd Hoffmann 
90188793816SHans de Goede     if (s->completions_only) {
90288793816SHans de Goede         return TD_RESULT_ASYNC_CONT;
90388793816SHans de Goede     }
90488793816SHans de Goede 
905f1ae32a1SGerd Hoffmann     /* Allocate new packet */
906a4f30cd7SHans de Goede     if (q == NULL) {
90711d15e40SHans de Goede         USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
90811d15e40SHans de Goede         USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
9097f102ebeSHans de Goede 
9107f102ebeSHans de Goede         if (ep == NULL) {
9117f102ebeSHans de Goede             return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV,
9127f102ebeSHans de Goede                                         int_mask);
9137f102ebeSHans de Goede         }
91466a08cbeSHans de Goede         q = uhci_queue_new(s, qh_addr, td, ep);
915a4f30cd7SHans de Goede     }
916a4f30cd7SHans de Goede     async = uhci_async_alloc(q, td_addr);
917f1ae32a1SGerd Hoffmann 
918f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
9196ba43f1fSHans de Goede     spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
92011d15e40SHans de Goede     usb_packet_setup(&async->packet, pid, q->ep, td_addr, spd,
921a6fb2ddbSHans de Goede                      (td->ctrl & TD_CTRL_IOC) != 0);
922f1ae32a1SGerd Hoffmann     qemu_sglist_add(&async->sgl, td->buffer, max_len);
923f1ae32a1SGerd Hoffmann     usb_packet_map(&async->packet, &async->sgl);
924f1ae32a1SGerd Hoffmann 
925f1ae32a1SGerd Hoffmann     switch(pid) {
926f1ae32a1SGerd Hoffmann     case USB_TOKEN_OUT:
927f1ae32a1SGerd Hoffmann     case USB_TOKEN_SETUP:
9289a77a0f5SHans de Goede         usb_handle_packet(q->ep->dev, &async->packet);
9299a77a0f5SHans de Goede         if (async->packet.status == USB_RET_SUCCESS) {
9309a77a0f5SHans de Goede             async->packet.actual_length = max_len;
9319a77a0f5SHans de Goede         }
932f1ae32a1SGerd Hoffmann         break;
933f1ae32a1SGerd Hoffmann 
934f1ae32a1SGerd Hoffmann     case USB_TOKEN_IN:
9359a77a0f5SHans de Goede         usb_handle_packet(q->ep->dev, &async->packet);
936f1ae32a1SGerd Hoffmann         break;
937f1ae32a1SGerd Hoffmann 
938f1ae32a1SGerd Hoffmann     default:
939f1ae32a1SGerd Hoffmann         /* invalid pid : frame interrupted */
94000a0770dSHans de Goede         usb_packet_unmap(&async->packet, &async->sgl);
941f1ae32a1SGerd Hoffmann         uhci_async_free(async);
942f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_HCPERR;
943f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
94460e1b2a6SGerd Hoffmann         return TD_RESULT_STOP_FRAME;
945f1ae32a1SGerd Hoffmann     }
946f1ae32a1SGerd Hoffmann 
9479a77a0f5SHans de Goede     if (async->packet.status == USB_RET_ASYNC) {
948f1ae32a1SGerd Hoffmann         uhci_async_link(async);
949a4f30cd7SHans de Goede         if (!queuing) {
95011d15e40SHans de Goede             uhci_queue_fill(q, td);
951a4f30cd7SHans de Goede         }
9524efe4ef3SGerd Hoffmann         return TD_RESULT_ASYNC_START;
953f1ae32a1SGerd Hoffmann     }
954f1ae32a1SGerd Hoffmann 
955f1ae32a1SGerd Hoffmann done:
9569a77a0f5SHans de Goede     ret = uhci_complete_td(s, td, async, int_mask);
957e2f89926SDavid Gibson     usb_packet_unmap(&async->packet, &async->sgl);
958f1ae32a1SGerd Hoffmann     uhci_async_free(async);
9599a77a0f5SHans de Goede     return ret;
960f1ae32a1SGerd Hoffmann }
961f1ae32a1SGerd Hoffmann 
962f1ae32a1SGerd Hoffmann static void uhci_async_complete(USBPort *port, USBPacket *packet)
963f1ae32a1SGerd Hoffmann {
964f1ae32a1SGerd Hoffmann     UHCIAsync *async = container_of(packet, UHCIAsync, packet);
965f1ae32a1SGerd Hoffmann     UHCIState *s = async->queue->uhci;
966f1ae32a1SGerd Hoffmann 
9679a77a0f5SHans de Goede     if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
9680cae7b1aSHans de Goede         uhci_async_cancel(async);
9690cae7b1aSHans de Goede         return;
9700cae7b1aSHans de Goede     }
9710cae7b1aSHans de Goede 
972f1ae32a1SGerd Hoffmann     async->done = 1;
97388793816SHans de Goede     /* Force processing of this packet *now*, needed for migration */
97488793816SHans de Goede     s->completions_only = true;
9759a16c595SGerd Hoffmann     qemu_bh_schedule(s->bh);
9769a16c595SGerd Hoffmann }
977f1ae32a1SGerd Hoffmann 
978f1ae32a1SGerd Hoffmann static int is_valid(uint32_t link)
979f1ae32a1SGerd Hoffmann {
980f1ae32a1SGerd Hoffmann     return (link & 1) == 0;
981f1ae32a1SGerd Hoffmann }
982f1ae32a1SGerd Hoffmann 
983f1ae32a1SGerd Hoffmann static int is_qh(uint32_t link)
984f1ae32a1SGerd Hoffmann {
985f1ae32a1SGerd Hoffmann     return (link & 2) != 0;
986f1ae32a1SGerd Hoffmann }
987f1ae32a1SGerd Hoffmann 
988f1ae32a1SGerd Hoffmann static int depth_first(uint32_t link)
989f1ae32a1SGerd Hoffmann {
990f1ae32a1SGerd Hoffmann     return (link & 4) != 0;
991f1ae32a1SGerd Hoffmann }
992f1ae32a1SGerd Hoffmann 
993f1ae32a1SGerd Hoffmann /* QH DB used for detecting QH loops */
994f1ae32a1SGerd Hoffmann #define UHCI_MAX_QUEUES 128
995f1ae32a1SGerd Hoffmann typedef struct {
996f1ae32a1SGerd Hoffmann     uint32_t addr[UHCI_MAX_QUEUES];
997f1ae32a1SGerd Hoffmann     int      count;
998f1ae32a1SGerd Hoffmann } QhDb;
999f1ae32a1SGerd Hoffmann 
1000f1ae32a1SGerd Hoffmann static void qhdb_reset(QhDb *db)
1001f1ae32a1SGerd Hoffmann {
1002f1ae32a1SGerd Hoffmann     db->count = 0;
1003f1ae32a1SGerd Hoffmann }
1004f1ae32a1SGerd Hoffmann 
1005f1ae32a1SGerd Hoffmann /* Add QH to DB. Returns 1 if already present or DB is full. */
1006f1ae32a1SGerd Hoffmann static int qhdb_insert(QhDb *db, uint32_t addr)
1007f1ae32a1SGerd Hoffmann {
1008f1ae32a1SGerd Hoffmann     int i;
1009f1ae32a1SGerd Hoffmann     for (i = 0; i < db->count; i++)
1010f1ae32a1SGerd Hoffmann         if (db->addr[i] == addr)
1011f1ae32a1SGerd Hoffmann             return 1;
1012f1ae32a1SGerd Hoffmann 
1013f1ae32a1SGerd Hoffmann     if (db->count >= UHCI_MAX_QUEUES)
1014f1ae32a1SGerd Hoffmann         return 1;
1015f1ae32a1SGerd Hoffmann 
1016f1ae32a1SGerd Hoffmann     db->addr[db->count++] = addr;
1017f1ae32a1SGerd Hoffmann     return 0;
1018f1ae32a1SGerd Hoffmann }
1019f1ae32a1SGerd Hoffmann 
102011d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
1021f1ae32a1SGerd Hoffmann {
1022f1ae32a1SGerd Hoffmann     uint32_t int_mask = 0;
1023f1ae32a1SGerd Hoffmann     uint32_t plink = td->link;
1024f1ae32a1SGerd Hoffmann     UHCI_TD ptd;
1025f1ae32a1SGerd Hoffmann     int ret;
1026f1ae32a1SGerd Hoffmann 
10276ba43f1fSHans de Goede     while (is_valid(plink)) {
1028a4f30cd7SHans de Goede         uhci_read_td(q->uhci, &ptd, plink);
1029f1ae32a1SGerd Hoffmann         if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
1030f1ae32a1SGerd Hoffmann             break;
1031f1ae32a1SGerd Hoffmann         }
1032a4f30cd7SHans de Goede         if (uhci_queue_token(&ptd) != q->token) {
1033f1ae32a1SGerd Hoffmann             break;
1034f1ae32a1SGerd Hoffmann         }
103550dcc0f8SGerd Hoffmann         trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
103666a08cbeSHans de Goede         ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask);
103752b0fecdSGerd Hoffmann         if (ret == TD_RESULT_ASYNC_CONT) {
103852b0fecdSGerd Hoffmann             break;
103952b0fecdSGerd Hoffmann         }
10404efe4ef3SGerd Hoffmann         assert(ret == TD_RESULT_ASYNC_START);
1041f1ae32a1SGerd Hoffmann         assert(int_mask == 0);
1042f1ae32a1SGerd Hoffmann         plink = ptd.link;
1043f1ae32a1SGerd Hoffmann     }
104411d15e40SHans de Goede     usb_device_flush_ep_queue(q->ep->dev, q->ep);
1045f1ae32a1SGerd Hoffmann }
1046f1ae32a1SGerd Hoffmann 
1047f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s)
1048f1ae32a1SGerd Hoffmann {
1049f1ae32a1SGerd Hoffmann     uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
10504aed20e2SGerd Hoffmann     uint32_t curr_qh, td_count = 0;
1051f1ae32a1SGerd Hoffmann     int cnt, ret;
1052f1ae32a1SGerd Hoffmann     UHCI_TD td;
1053f1ae32a1SGerd Hoffmann     UHCI_QH qh;
1054f1ae32a1SGerd Hoffmann     QhDb qhdb;
1055f1ae32a1SGerd Hoffmann 
1056f1ae32a1SGerd Hoffmann     frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
1057f1ae32a1SGerd Hoffmann 
1058f1ae32a1SGerd Hoffmann     pci_dma_read(&s->dev, frame_addr, &link, 4);
1059f1ae32a1SGerd Hoffmann     le32_to_cpus(&link);
1060f1ae32a1SGerd Hoffmann 
1061f1ae32a1SGerd Hoffmann     int_mask = 0;
1062f1ae32a1SGerd Hoffmann     curr_qh  = 0;
1063f1ae32a1SGerd Hoffmann 
1064f1ae32a1SGerd Hoffmann     qhdb_reset(&qhdb);
1065f1ae32a1SGerd Hoffmann 
1066f1ae32a1SGerd Hoffmann     for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
106788793816SHans de Goede         if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) {
10684aed20e2SGerd Hoffmann             /* We've reached the usb 1.1 bandwidth, which is
10694aed20e2SGerd Hoffmann                1280 bytes/frame, stop processing */
10704aed20e2SGerd Hoffmann             trace_usb_uhci_frame_stop_bandwidth();
10714aed20e2SGerd Hoffmann             break;
10724aed20e2SGerd Hoffmann         }
1073f1ae32a1SGerd Hoffmann         if (is_qh(link)) {
1074f1ae32a1SGerd Hoffmann             /* QH */
107550dcc0f8SGerd Hoffmann             trace_usb_uhci_qh_load(link & ~0xf);
1076f1ae32a1SGerd Hoffmann 
1077f1ae32a1SGerd Hoffmann             if (qhdb_insert(&qhdb, link)) {
1078f1ae32a1SGerd Hoffmann                 /*
1079f1ae32a1SGerd Hoffmann                  * We're going in circles. Which is not a bug because
1080f1ae32a1SGerd Hoffmann                  * HCD is allowed to do that as part of the BW management.
1081f1ae32a1SGerd Hoffmann                  *
10824aed20e2SGerd Hoffmann                  * Stop processing here if no transaction has been done
10834aed20e2SGerd Hoffmann                  * since we've been here last time.
1084f1ae32a1SGerd Hoffmann                  */
1085f1ae32a1SGerd Hoffmann                 if (td_count == 0) {
108650dcc0f8SGerd Hoffmann                     trace_usb_uhci_frame_loop_stop_idle();
1087f1ae32a1SGerd Hoffmann                     break;
1088f1ae32a1SGerd Hoffmann                 } else {
108950dcc0f8SGerd Hoffmann                     trace_usb_uhci_frame_loop_continue();
1090f1ae32a1SGerd Hoffmann                     td_count = 0;
1091f1ae32a1SGerd Hoffmann                     qhdb_reset(&qhdb);
1092f1ae32a1SGerd Hoffmann                     qhdb_insert(&qhdb, link);
1093f1ae32a1SGerd Hoffmann                 }
1094f1ae32a1SGerd Hoffmann             }
1095f1ae32a1SGerd Hoffmann 
1096f1ae32a1SGerd Hoffmann             pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1097f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.link);
1098f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.el_link);
1099f1ae32a1SGerd Hoffmann 
1100f1ae32a1SGerd Hoffmann             if (!is_valid(qh.el_link)) {
1101f1ae32a1SGerd Hoffmann                 /* QH w/o elements */
1102f1ae32a1SGerd Hoffmann                 curr_qh = 0;
1103f1ae32a1SGerd Hoffmann                 link = qh.link;
1104f1ae32a1SGerd Hoffmann             } else {
1105f1ae32a1SGerd Hoffmann                 /* QH with elements */
1106f1ae32a1SGerd Hoffmann             	curr_qh = link;
1107f1ae32a1SGerd Hoffmann             	link = qh.el_link;
1108f1ae32a1SGerd Hoffmann             }
1109f1ae32a1SGerd Hoffmann             continue;
1110f1ae32a1SGerd Hoffmann         }
1111f1ae32a1SGerd Hoffmann 
1112f1ae32a1SGerd Hoffmann         /* TD */
1113963a68b5SHans de Goede         uhci_read_td(s, &td, link);
111450dcc0f8SGerd Hoffmann         trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
1115f1ae32a1SGerd Hoffmann 
1116f1ae32a1SGerd Hoffmann         old_td_ctrl = td.ctrl;
111766a08cbeSHans de Goede         ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask);
1118f1ae32a1SGerd Hoffmann         if (old_td_ctrl != td.ctrl) {
1119f1ae32a1SGerd Hoffmann             /* update the status bits of the TD */
1120f1ae32a1SGerd Hoffmann             val = cpu_to_le32(td.ctrl);
1121f1ae32a1SGerd Hoffmann             pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1122f1ae32a1SGerd Hoffmann         }
1123f1ae32a1SGerd Hoffmann 
1124f1ae32a1SGerd Hoffmann         switch (ret) {
112560e1b2a6SGerd Hoffmann         case TD_RESULT_STOP_FRAME: /* interrupted frame */
1126f1ae32a1SGerd Hoffmann             goto out;
1127f1ae32a1SGerd Hoffmann 
112860e1b2a6SGerd Hoffmann         case TD_RESULT_NEXT_QH:
11294efe4ef3SGerd Hoffmann         case TD_RESULT_ASYNC_CONT:
113050dcc0f8SGerd Hoffmann             trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
1131f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1132f1ae32a1SGerd Hoffmann             continue;
1133f1ae32a1SGerd Hoffmann 
11344efe4ef3SGerd Hoffmann         case TD_RESULT_ASYNC_START:
113550dcc0f8SGerd Hoffmann             trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
1136f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1137f1ae32a1SGerd Hoffmann             continue;
1138f1ae32a1SGerd Hoffmann 
113960e1b2a6SGerd Hoffmann         case TD_RESULT_COMPLETE:
114050dcc0f8SGerd Hoffmann             trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
1141f1ae32a1SGerd Hoffmann             link = td.link;
1142f1ae32a1SGerd Hoffmann             td_count++;
11434aed20e2SGerd Hoffmann             s->frame_bytes += (td.ctrl & 0x7ff) + 1;
1144f1ae32a1SGerd Hoffmann 
1145f1ae32a1SGerd Hoffmann             if (curr_qh) {
1146f1ae32a1SGerd Hoffmann                 /* update QH element link */
1147f1ae32a1SGerd Hoffmann                 qh.el_link = link;
1148f1ae32a1SGerd Hoffmann                 val = cpu_to_le32(qh.el_link);
1149f1ae32a1SGerd Hoffmann                 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1150f1ae32a1SGerd Hoffmann 
1151f1ae32a1SGerd Hoffmann                 if (!depth_first(link)) {
1152f1ae32a1SGerd Hoffmann                     /* done with this QH */
1153f1ae32a1SGerd Hoffmann                     curr_qh = 0;
1154f1ae32a1SGerd Hoffmann                     link    = qh.link;
1155f1ae32a1SGerd Hoffmann                 }
1156f1ae32a1SGerd Hoffmann             }
1157f1ae32a1SGerd Hoffmann             break;
1158f1ae32a1SGerd Hoffmann 
1159f1ae32a1SGerd Hoffmann         default:
1160f1ae32a1SGerd Hoffmann             assert(!"unknown return code");
1161f1ae32a1SGerd Hoffmann         }
1162f1ae32a1SGerd Hoffmann 
1163f1ae32a1SGerd Hoffmann         /* go to the next entry */
1164f1ae32a1SGerd Hoffmann     }
1165f1ae32a1SGerd Hoffmann 
1166f1ae32a1SGerd Hoffmann out:
1167f1ae32a1SGerd Hoffmann     s->pending_int_mask |= int_mask;
1168f1ae32a1SGerd Hoffmann }
1169f1ae32a1SGerd Hoffmann 
11709a16c595SGerd Hoffmann static void uhci_bh(void *opaque)
11719a16c595SGerd Hoffmann {
11729a16c595SGerd Hoffmann     UHCIState *s = opaque;
11739a16c595SGerd Hoffmann     uhci_process_frame(s);
11749a16c595SGerd Hoffmann }
11759a16c595SGerd Hoffmann 
1176f1ae32a1SGerd Hoffmann static void uhci_frame_timer(void *opaque)
1177f1ae32a1SGerd Hoffmann {
1178f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
1179f1ae32a1SGerd Hoffmann 
1180f1ae32a1SGerd Hoffmann     /* prepare the timer for the next frame */
1181f1ae32a1SGerd Hoffmann     s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
11824aed20e2SGerd Hoffmann     s->frame_bytes = 0;
118388793816SHans de Goede     s->completions_only = false;
11849a16c595SGerd Hoffmann     qemu_bh_cancel(s->bh);
1185f1ae32a1SGerd Hoffmann 
1186f1ae32a1SGerd Hoffmann     if (!(s->cmd & UHCI_CMD_RS)) {
1187f1ae32a1SGerd Hoffmann         /* Full stop */
118850dcc0f8SGerd Hoffmann         trace_usb_uhci_schedule_stop();
1189f1ae32a1SGerd Hoffmann         qemu_del_timer(s->frame_timer);
1190d9a528dbSGerd Hoffmann         uhci_async_cancel_all(s);
1191f1ae32a1SGerd Hoffmann         /* set hchalted bit in status - UHCI11D 2.1.2 */
1192f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_HCHALTED;
1193f1ae32a1SGerd Hoffmann         return;
1194f1ae32a1SGerd Hoffmann     }
1195f1ae32a1SGerd Hoffmann 
1196719c130dSHans de Goede     /* Process the current frame */
119750dcc0f8SGerd Hoffmann     trace_usb_uhci_frame_start(s->frnum);
1198f1ae32a1SGerd Hoffmann 
1199f1ae32a1SGerd Hoffmann     uhci_async_validate_begin(s);
1200f1ae32a1SGerd Hoffmann 
1201f1ae32a1SGerd Hoffmann     uhci_process_frame(s);
1202f1ae32a1SGerd Hoffmann 
1203f1ae32a1SGerd Hoffmann     uhci_async_validate_end(s);
1204f1ae32a1SGerd Hoffmann 
1205719c130dSHans de Goede     /* The uhci spec says frnum reflects the frame currently being processed,
1206719c130dSHans de Goede      * and the guest must look at frnum - 1 on interrupt, so inc frnum now */
1207719c130dSHans de Goede     s->frnum = (s->frnum + 1) & 0x7ff;
1208719c130dSHans de Goede 
1209719c130dSHans de Goede     /* Complete the previous frame */
1210719c130dSHans de Goede     if (s->pending_int_mask) {
1211719c130dSHans de Goede         s->status2 |= s->pending_int_mask;
1212719c130dSHans de Goede         s->status  |= UHCI_STS_USBINT;
1213719c130dSHans de Goede         uhci_update_irq(s);
1214719c130dSHans de Goede     }
1215719c130dSHans de Goede     s->pending_int_mask = 0;
1216719c130dSHans de Goede 
1217f1ae32a1SGerd Hoffmann     qemu_mod_timer(s->frame_timer, s->expire_time);
1218f1ae32a1SGerd Hoffmann }
1219f1ae32a1SGerd Hoffmann 
1220f1ae32a1SGerd Hoffmann static const MemoryRegionPortio uhci_portio[] = {
1221f1ae32a1SGerd Hoffmann     { 0, 32, 2, .write = uhci_ioport_writew, },
1222f1ae32a1SGerd Hoffmann     { 0, 32, 2, .read = uhci_ioport_readw, },
1223f1ae32a1SGerd Hoffmann     { 0, 32, 4, .write = uhci_ioport_writel, },
1224f1ae32a1SGerd Hoffmann     { 0, 32, 4, .read = uhci_ioport_readl, },
1225f1ae32a1SGerd Hoffmann     { 0, 32, 1, .write = uhci_ioport_writeb, },
1226f1ae32a1SGerd Hoffmann     { 0, 32, 1, .read = uhci_ioport_readb, },
1227f1ae32a1SGerd Hoffmann     PORTIO_END_OF_LIST()
1228f1ae32a1SGerd Hoffmann };
1229f1ae32a1SGerd Hoffmann 
1230f1ae32a1SGerd Hoffmann static const MemoryRegionOps uhci_ioport_ops = {
1231f1ae32a1SGerd Hoffmann     .old_portio = uhci_portio,
1232f1ae32a1SGerd Hoffmann };
1233f1ae32a1SGerd Hoffmann 
1234f1ae32a1SGerd Hoffmann static USBPortOps uhci_port_ops = {
1235f1ae32a1SGerd Hoffmann     .attach = uhci_attach,
1236f1ae32a1SGerd Hoffmann     .detach = uhci_detach,
1237f1ae32a1SGerd Hoffmann     .child_detach = uhci_child_detach,
1238f1ae32a1SGerd Hoffmann     .wakeup = uhci_wakeup,
1239f1ae32a1SGerd Hoffmann     .complete = uhci_async_complete,
1240f1ae32a1SGerd Hoffmann };
1241f1ae32a1SGerd Hoffmann 
1242f1ae32a1SGerd Hoffmann static USBBusOps uhci_bus_ops = {
1243f1ae32a1SGerd Hoffmann };
1244f1ae32a1SGerd Hoffmann 
1245f1ae32a1SGerd Hoffmann static int usb_uhci_common_initfn(PCIDevice *dev)
1246f1ae32a1SGerd Hoffmann {
1247973002c1SGerd Hoffmann     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
12488f3f90b0SGerd Hoffmann     UHCIPCIDeviceClass *u = container_of(pc, UHCIPCIDeviceClass, parent_class);
1249f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1250f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1251f1ae32a1SGerd Hoffmann     int i;
1252f1ae32a1SGerd Hoffmann 
1253f1ae32a1SGerd Hoffmann     pci_conf[PCI_CLASS_PROG] = 0x00;
1254f1ae32a1SGerd Hoffmann     /* TODO: reset value should be 0. */
1255f1ae32a1SGerd Hoffmann     pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1256f1ae32a1SGerd Hoffmann 
12578f3f90b0SGerd Hoffmann     s->irq_pin = u->info.irq_pin;
1258973002c1SGerd Hoffmann     pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1);
1259973002c1SGerd Hoffmann 
1260f1ae32a1SGerd Hoffmann     if (s->masterbus) {
1261f1ae32a1SGerd Hoffmann         USBPort *ports[NB_PORTS];
1262f1ae32a1SGerd Hoffmann         for(i = 0; i < NB_PORTS; i++) {
1263f1ae32a1SGerd Hoffmann             ports[i] = &s->ports[i].port;
1264f1ae32a1SGerd Hoffmann         }
1265f1ae32a1SGerd Hoffmann         if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1266f1ae32a1SGerd Hoffmann                 s->firstport, s, &uhci_port_ops,
1267f1ae32a1SGerd Hoffmann                 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1268f1ae32a1SGerd Hoffmann             return -1;
1269f1ae32a1SGerd Hoffmann         }
1270f1ae32a1SGerd Hoffmann     } else {
1271f1ae32a1SGerd Hoffmann         usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1272f1ae32a1SGerd Hoffmann         for (i = 0; i < NB_PORTS; i++) {
1273f1ae32a1SGerd Hoffmann             usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1274f1ae32a1SGerd Hoffmann                               USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1275f1ae32a1SGerd Hoffmann         }
1276f1ae32a1SGerd Hoffmann     }
12779a16c595SGerd Hoffmann     s->bh = qemu_bh_new(uhci_bh, s);
1278f1ae32a1SGerd Hoffmann     s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1279f1ae32a1SGerd Hoffmann     s->num_ports_vmstate = NB_PORTS;
1280f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&s->queues);
1281f1ae32a1SGerd Hoffmann 
1282f1ae32a1SGerd Hoffmann     qemu_register_reset(uhci_reset, s);
1283f1ae32a1SGerd Hoffmann 
1284f1ae32a1SGerd Hoffmann     memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1285f1ae32a1SGerd Hoffmann     /* Use region 4 for consistency with real hardware.  BSD guests seem
1286f1ae32a1SGerd Hoffmann        to rely on this.  */
1287f1ae32a1SGerd Hoffmann     pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1288f1ae32a1SGerd Hoffmann 
1289f1ae32a1SGerd Hoffmann     return 0;
1290f1ae32a1SGerd Hoffmann }
1291f1ae32a1SGerd Hoffmann 
1292f1ae32a1SGerd Hoffmann static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1293f1ae32a1SGerd Hoffmann {
1294f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1295f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1296f1ae32a1SGerd Hoffmann 
1297f1ae32a1SGerd Hoffmann     /* USB misc control 1/2 */
1298f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x40,0x00001000);
1299f1ae32a1SGerd Hoffmann     /* PM capability */
1300f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x80,0x00020001);
1301f1ae32a1SGerd Hoffmann     /* USB legacy support  */
1302f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0xc0,0x00002000);
1303f1ae32a1SGerd Hoffmann 
1304f1ae32a1SGerd Hoffmann     return usb_uhci_common_initfn(dev);
1305f1ae32a1SGerd Hoffmann }
1306f1ae32a1SGerd Hoffmann 
1307f90c2bcdSAlex Williamson static void usb_uhci_exit(PCIDevice *dev)
1308f1ae32a1SGerd Hoffmann {
1309f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1310f1ae32a1SGerd Hoffmann 
1311f1ae32a1SGerd Hoffmann     memory_region_destroy(&s->io_bar);
1312f1ae32a1SGerd Hoffmann }
1313f1ae32a1SGerd Hoffmann 
1314f1ae32a1SGerd Hoffmann static Property uhci_properties[] = {
1315f1ae32a1SGerd Hoffmann     DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1316f1ae32a1SGerd Hoffmann     DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
131740141d12SGerd Hoffmann     DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
1318f1ae32a1SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
1319f1ae32a1SGerd Hoffmann };
1320f1ae32a1SGerd Hoffmann 
13212c2e8525SGerd Hoffmann static void uhci_class_init(ObjectClass *klass, void *data)
1322f1ae32a1SGerd Hoffmann {
1323f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1324f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
13258f3f90b0SGerd Hoffmann     UHCIPCIDeviceClass *u = container_of(k, UHCIPCIDeviceClass, parent_class);
13262c2e8525SGerd Hoffmann     UHCIInfo *info = data;
1327f1ae32a1SGerd Hoffmann 
13282c2e8525SGerd Hoffmann     k->init = info->initfn ? info->initfn : usb_uhci_common_initfn;
13292c2e8525SGerd Hoffmann     k->exit = info->unplug ? usb_uhci_exit : NULL;
13302c2e8525SGerd Hoffmann     k->vendor_id = info->vendor_id;
13312c2e8525SGerd Hoffmann     k->device_id = info->device_id;
13322c2e8525SGerd Hoffmann     k->revision  = info->revision;
1333f1ae32a1SGerd Hoffmann     k->class_id  = PCI_CLASS_SERIAL_USB;
13346c2d1c32SGerd Hoffmann     k->no_hotplug = 1;
1335f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1336f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
13378f3f90b0SGerd Hoffmann     u->info = *info;
1338f1ae32a1SGerd Hoffmann }
1339f1ae32a1SGerd Hoffmann 
13402c2e8525SGerd Hoffmann static UHCIInfo uhci_info[] = {
13412c2e8525SGerd Hoffmann     {
1342f1ae32a1SGerd Hoffmann         .name       = "piix3-usb-uhci",
13432c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
13442c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82371SB_2,
13452c2e8525SGerd Hoffmann         .revision  = 0x01,
13468f3f90b0SGerd Hoffmann         .irq_pin   = 3,
13472c2e8525SGerd Hoffmann         .unplug    = true,
13482c2e8525SGerd Hoffmann     },{
1349f1ae32a1SGerd Hoffmann         .name      = "piix4-usb-uhci",
13502c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
13512c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82371AB_2,
13522c2e8525SGerd Hoffmann         .revision  = 0x01,
13538f3f90b0SGerd Hoffmann         .irq_pin   = 3,
13542c2e8525SGerd Hoffmann         .unplug    = true,
13552c2e8525SGerd Hoffmann     },{
1356f1ae32a1SGerd Hoffmann         .name      = "vt82c686b-usb-uhci",
13572c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_VIA,
13582c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_VIA_UHCI,
13592c2e8525SGerd Hoffmann         .revision  = 0x01,
13608f3f90b0SGerd Hoffmann         .irq_pin   = 3,
13612c2e8525SGerd Hoffmann         .initfn    = usb_uhci_vt82c686b_initfn,
13622c2e8525SGerd Hoffmann         .unplug    = true,
13632c2e8525SGerd Hoffmann     },{
136474625ea2SGerd Hoffmann         .name      = "ich9-usb-uhci1", /* 00:1d.0 */
13652c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
13662c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1,
13672c2e8525SGerd Hoffmann         .revision  = 0x03,
13688f3f90b0SGerd Hoffmann         .irq_pin   = 0,
13692c2e8525SGerd Hoffmann         .unplug    = false,
13702c2e8525SGerd Hoffmann     },{
137174625ea2SGerd Hoffmann         .name      = "ich9-usb-uhci2", /* 00:1d.1 */
13722c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
13732c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2,
13742c2e8525SGerd Hoffmann         .revision  = 0x03,
13758f3f90b0SGerd Hoffmann         .irq_pin   = 1,
13762c2e8525SGerd Hoffmann         .unplug    = false,
13772c2e8525SGerd Hoffmann     },{
137874625ea2SGerd Hoffmann         .name      = "ich9-usb-uhci3", /* 00:1d.2 */
13792c2e8525SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
13802c2e8525SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3,
13812c2e8525SGerd Hoffmann         .revision  = 0x03,
13828f3f90b0SGerd Hoffmann         .irq_pin   = 2,
13832c2e8525SGerd Hoffmann         .unplug    = false,
138474625ea2SGerd Hoffmann     },{
138574625ea2SGerd Hoffmann         .name      = "ich9-usb-uhci4", /* 00:1a.0 */
138674625ea2SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
138774625ea2SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4,
138874625ea2SGerd Hoffmann         .revision  = 0x03,
138974625ea2SGerd Hoffmann         .irq_pin   = 0,
139074625ea2SGerd Hoffmann         .unplug    = false,
139174625ea2SGerd Hoffmann     },{
139274625ea2SGerd Hoffmann         .name      = "ich9-usb-uhci5", /* 00:1a.1 */
139374625ea2SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
139474625ea2SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5,
139574625ea2SGerd Hoffmann         .revision  = 0x03,
139674625ea2SGerd Hoffmann         .irq_pin   = 1,
139774625ea2SGerd Hoffmann         .unplug    = false,
139874625ea2SGerd Hoffmann     },{
139974625ea2SGerd Hoffmann         .name      = "ich9-usb-uhci6", /* 00:1a.2 */
140074625ea2SGerd Hoffmann         .vendor_id = PCI_VENDOR_ID_INTEL,
140174625ea2SGerd Hoffmann         .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6,
140274625ea2SGerd Hoffmann         .revision  = 0x03,
140374625ea2SGerd Hoffmann         .irq_pin   = 2,
140474625ea2SGerd Hoffmann         .unplug    = false,
14052c2e8525SGerd Hoffmann     }
1406f1ae32a1SGerd Hoffmann };
1407f1ae32a1SGerd Hoffmann 
1408f1ae32a1SGerd Hoffmann static void uhci_register_types(void)
1409f1ae32a1SGerd Hoffmann {
14102c2e8525SGerd Hoffmann     TypeInfo uhci_type_info = {
14112c2e8525SGerd Hoffmann         .parent        = TYPE_PCI_DEVICE,
14122c2e8525SGerd Hoffmann         .instance_size = sizeof(UHCIState),
14138f3f90b0SGerd Hoffmann         .class_size    = sizeof(UHCIPCIDeviceClass),
14142c2e8525SGerd Hoffmann         .class_init    = uhci_class_init,
14152c2e8525SGerd Hoffmann     };
14162c2e8525SGerd Hoffmann     int i;
14172c2e8525SGerd Hoffmann 
14182c2e8525SGerd Hoffmann     for (i = 0; i < ARRAY_SIZE(uhci_info); i++) {
14192c2e8525SGerd Hoffmann         uhci_type_info.name = uhci_info[i].name;
14202c2e8525SGerd Hoffmann         uhci_type_info.class_data = uhci_info + i;
14212c2e8525SGerd Hoffmann         type_register(&uhci_type_info);
14222c2e8525SGerd Hoffmann     }
1423f1ae32a1SGerd Hoffmann }
1424f1ae32a1SGerd Hoffmann 
1425f1ae32a1SGerd Hoffmann type_init(uhci_register_types)
1426