xref: /openbmc/qemu/hw/usb/hcd-uhci.c (revision 5ad23e873c858292dc58b9296261365312b8f683)
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;
91f1ae32a1SGerd Hoffmann 
92f1ae32a1SGerd Hoffmann /*
93f1ae32a1SGerd Hoffmann  * Pending async transaction.
94f1ae32a1SGerd Hoffmann  * 'packet' must be the first field because completion
95f1ae32a1SGerd Hoffmann  * handler does "(UHCIAsync *) pkt" cast.
96f1ae32a1SGerd Hoffmann  */
97f1ae32a1SGerd Hoffmann 
98f1ae32a1SGerd Hoffmann struct UHCIAsync {
99f1ae32a1SGerd Hoffmann     USBPacket packet;
100f1ae32a1SGerd Hoffmann     QEMUSGList sgl;
101f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
102f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIAsync) next;
1031f250cc7SHans de Goede     uint32_t  td_addr;
104f1ae32a1SGerd Hoffmann     uint8_t   done;
105f1ae32a1SGerd Hoffmann };
106f1ae32a1SGerd Hoffmann 
107f1ae32a1SGerd Hoffmann struct UHCIQueue {
108f1ae32a1SGerd Hoffmann     uint32_t  token;
109f1ae32a1SGerd Hoffmann     UHCIState *uhci;
11011d15e40SHans de Goede     USBEndpoint *ep;
111f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIQueue) next;
112f1ae32a1SGerd Hoffmann     QTAILQ_HEAD(, UHCIAsync) asyncs;
113f1ae32a1SGerd Hoffmann     int8_t    valid;
114f1ae32a1SGerd Hoffmann };
115f1ae32a1SGerd Hoffmann 
116f1ae32a1SGerd Hoffmann typedef struct UHCIPort {
117f1ae32a1SGerd Hoffmann     USBPort port;
118f1ae32a1SGerd Hoffmann     uint16_t ctrl;
119f1ae32a1SGerd Hoffmann } UHCIPort;
120f1ae32a1SGerd Hoffmann 
121f1ae32a1SGerd Hoffmann struct UHCIState {
122f1ae32a1SGerd Hoffmann     PCIDevice dev;
123f1ae32a1SGerd Hoffmann     MemoryRegion io_bar;
124f1ae32a1SGerd Hoffmann     USBBus bus; /* Note unused when we're a companion controller */
125f1ae32a1SGerd Hoffmann     uint16_t cmd; /* cmd register */
126f1ae32a1SGerd Hoffmann     uint16_t status;
127f1ae32a1SGerd Hoffmann     uint16_t intr; /* interrupt enable register */
128f1ae32a1SGerd Hoffmann     uint16_t frnum; /* frame number */
129f1ae32a1SGerd Hoffmann     uint32_t fl_base_addr; /* frame list base address */
130f1ae32a1SGerd Hoffmann     uint8_t sof_timing;
131f1ae32a1SGerd Hoffmann     uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
132f1ae32a1SGerd Hoffmann     int64_t expire_time;
133f1ae32a1SGerd Hoffmann     QEMUTimer *frame_timer;
1349a16c595SGerd Hoffmann     QEMUBH *bh;
1354aed20e2SGerd Hoffmann     uint32_t frame_bytes;
13640141d12SGerd Hoffmann     uint32_t frame_bandwidth;
137f1ae32a1SGerd Hoffmann     UHCIPort ports[NB_PORTS];
138f1ae32a1SGerd Hoffmann 
139f1ae32a1SGerd Hoffmann     /* Interrupts that should be raised at the end of the current frame.  */
140f1ae32a1SGerd Hoffmann     uint32_t pending_int_mask;
141973002c1SGerd Hoffmann     int irq_pin;
142f1ae32a1SGerd Hoffmann 
143f1ae32a1SGerd Hoffmann     /* Active packets */
144f1ae32a1SGerd Hoffmann     QTAILQ_HEAD(, UHCIQueue) queues;
145f1ae32a1SGerd Hoffmann     uint8_t num_ports_vmstate;
146f1ae32a1SGerd Hoffmann 
147f1ae32a1SGerd Hoffmann     /* Properties */
148f1ae32a1SGerd Hoffmann     char *masterbus;
149f1ae32a1SGerd Hoffmann     uint32_t firstport;
150f1ae32a1SGerd Hoffmann };
151f1ae32a1SGerd Hoffmann 
152f1ae32a1SGerd Hoffmann typedef struct UHCI_TD {
153f1ae32a1SGerd Hoffmann     uint32_t link;
154f1ae32a1SGerd Hoffmann     uint32_t ctrl; /* see TD_CTRL_xxx */
155f1ae32a1SGerd Hoffmann     uint32_t token;
156f1ae32a1SGerd Hoffmann     uint32_t buffer;
157f1ae32a1SGerd Hoffmann } UHCI_TD;
158f1ae32a1SGerd Hoffmann 
159f1ae32a1SGerd Hoffmann typedef struct UHCI_QH {
160f1ae32a1SGerd Hoffmann     uint32_t link;
161f1ae32a1SGerd Hoffmann     uint32_t el_link;
162f1ae32a1SGerd Hoffmann } UHCI_QH;
163f1ae32a1SGerd Hoffmann 
16440507377SHans de Goede static void uhci_async_cancel(UHCIAsync *async);
16511d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
16640507377SHans de Goede 
167f1ae32a1SGerd Hoffmann static inline int32_t uhci_queue_token(UHCI_TD *td)
168f1ae32a1SGerd Hoffmann {
169f1ae32a1SGerd Hoffmann     /* covers ep, dev, pid -> identifies the endpoint */
170f1ae32a1SGerd Hoffmann     return td->token & 0x7ffff;
171f1ae32a1SGerd Hoffmann }
172f1ae32a1SGerd Hoffmann 
17311d15e40SHans de Goede static UHCIQueue *uhci_queue_get(UHCIState *s, UHCI_TD *td, USBEndpoint *ep)
174f1ae32a1SGerd Hoffmann {
175f1ae32a1SGerd Hoffmann     uint32_t token = uhci_queue_token(td);
176f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
177f1ae32a1SGerd Hoffmann 
178f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
179f1ae32a1SGerd Hoffmann         if (queue->token == token) {
180f1ae32a1SGerd Hoffmann             return queue;
181f1ae32a1SGerd Hoffmann         }
182f1ae32a1SGerd Hoffmann     }
183f1ae32a1SGerd Hoffmann 
184f1ae32a1SGerd Hoffmann     queue = g_new0(UHCIQueue, 1);
185f1ae32a1SGerd Hoffmann     queue->uhci = s;
186f1ae32a1SGerd Hoffmann     queue->token = token;
18711d15e40SHans de Goede     queue->ep = ep;
188f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&queue->asyncs);
189f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_HEAD(&s->queues, queue, next);
19050dcc0f8SGerd Hoffmann     trace_usb_uhci_queue_add(queue->token);
191f1ae32a1SGerd Hoffmann     return queue;
192f1ae32a1SGerd Hoffmann }
193f1ae32a1SGerd Hoffmann 
194f1ae32a1SGerd Hoffmann static void uhci_queue_free(UHCIQueue *queue)
195f1ae32a1SGerd Hoffmann {
196f1ae32a1SGerd Hoffmann     UHCIState *s = queue->uhci;
19740507377SHans de Goede     UHCIAsync *async;
19840507377SHans de Goede 
19940507377SHans de Goede     while (!QTAILQ_EMPTY(&queue->asyncs)) {
20040507377SHans de Goede         async = QTAILQ_FIRST(&queue->asyncs);
20140507377SHans de Goede         uhci_async_cancel(async);
20240507377SHans de Goede     }
203f1ae32a1SGerd Hoffmann 
20450dcc0f8SGerd Hoffmann     trace_usb_uhci_queue_del(queue->token);
205f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&s->queues, queue, next);
206f1ae32a1SGerd Hoffmann     g_free(queue);
207f1ae32a1SGerd Hoffmann }
208f1ae32a1SGerd Hoffmann 
2091f250cc7SHans de Goede static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
210f1ae32a1SGerd Hoffmann {
211f1ae32a1SGerd Hoffmann     UHCIAsync *async = g_new0(UHCIAsync, 1);
212f1ae32a1SGerd Hoffmann 
213f1ae32a1SGerd Hoffmann     async->queue = queue;
2141f250cc7SHans de Goede     async->td_addr = td_addr;
215f1ae32a1SGerd Hoffmann     usb_packet_init(&async->packet);
216f1ae32a1SGerd Hoffmann     pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
2171f250cc7SHans de Goede     trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
218f1ae32a1SGerd Hoffmann 
219f1ae32a1SGerd Hoffmann     return async;
220f1ae32a1SGerd Hoffmann }
221f1ae32a1SGerd Hoffmann 
222f1ae32a1SGerd Hoffmann static void uhci_async_free(UHCIAsync *async)
223f1ae32a1SGerd Hoffmann {
2241f250cc7SHans de Goede     trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
225f1ae32a1SGerd Hoffmann     usb_packet_cleanup(&async->packet);
226f1ae32a1SGerd Hoffmann     qemu_sglist_destroy(&async->sgl);
227f1ae32a1SGerd Hoffmann     g_free(async);
228f1ae32a1SGerd Hoffmann }
229f1ae32a1SGerd Hoffmann 
230f1ae32a1SGerd Hoffmann static void uhci_async_link(UHCIAsync *async)
231f1ae32a1SGerd Hoffmann {
232f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
233f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
2341f250cc7SHans de Goede     trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
235f1ae32a1SGerd Hoffmann }
236f1ae32a1SGerd Hoffmann 
237f1ae32a1SGerd Hoffmann static void uhci_async_unlink(UHCIAsync *async)
238f1ae32a1SGerd Hoffmann {
239f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
240f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&queue->asyncs, async, next);
2411f250cc7SHans de Goede     trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
242f1ae32a1SGerd Hoffmann }
243f1ae32a1SGerd Hoffmann 
244f1ae32a1SGerd Hoffmann static void uhci_async_cancel(UHCIAsync *async)
245f1ae32a1SGerd Hoffmann {
2462f2ee268SHans de Goede     uhci_async_unlink(async);
2471f250cc7SHans de Goede     trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
2481f250cc7SHans de Goede                                  async->done);
249f1ae32a1SGerd Hoffmann     if (!async->done)
250f1ae32a1SGerd Hoffmann         usb_cancel_packet(&async->packet);
25100a0770dSHans de Goede     usb_packet_unmap(&async->packet, &async->sgl);
252f1ae32a1SGerd Hoffmann     uhci_async_free(async);
253f1ae32a1SGerd Hoffmann }
254f1ae32a1SGerd Hoffmann 
255f1ae32a1SGerd Hoffmann /*
256f1ae32a1SGerd Hoffmann  * Mark all outstanding async packets as invalid.
257f1ae32a1SGerd Hoffmann  * This is used for canceling them when TDs are removed by the HCD.
258f1ae32a1SGerd Hoffmann  */
259f1ae32a1SGerd Hoffmann static void uhci_async_validate_begin(UHCIState *s)
260f1ae32a1SGerd Hoffmann {
261f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
262f1ae32a1SGerd Hoffmann 
263f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
264f1ae32a1SGerd Hoffmann         queue->valid--;
265f1ae32a1SGerd Hoffmann     }
266f1ae32a1SGerd Hoffmann }
267f1ae32a1SGerd Hoffmann 
268f1ae32a1SGerd Hoffmann /*
269f1ae32a1SGerd Hoffmann  * Cancel async packets that are no longer valid
270f1ae32a1SGerd Hoffmann  */
271f1ae32a1SGerd Hoffmann static void uhci_async_validate_end(UHCIState *s)
272f1ae32a1SGerd Hoffmann {
273f1ae32a1SGerd Hoffmann     UHCIQueue *queue, *n;
274f1ae32a1SGerd Hoffmann 
275f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
27640507377SHans de Goede         if (!queue->valid) {
277f1ae32a1SGerd Hoffmann             uhci_queue_free(queue);
278f1ae32a1SGerd Hoffmann         }
279f1ae32a1SGerd Hoffmann     }
28040507377SHans de Goede }
281f1ae32a1SGerd Hoffmann 
282f1ae32a1SGerd Hoffmann static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
283f1ae32a1SGerd Hoffmann {
284*5ad23e87SHans de Goede     UHCIQueue *queue, *n;
285f1ae32a1SGerd Hoffmann 
286*5ad23e87SHans de Goede     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
287*5ad23e87SHans de Goede         if (queue->ep->dev == dev) {
288*5ad23e87SHans de Goede             uhci_queue_free(queue, "cancel-device");
289f1ae32a1SGerd Hoffmann         }
290f1ae32a1SGerd Hoffmann     }
291f1ae32a1SGerd Hoffmann }
292f1ae32a1SGerd Hoffmann 
293f1ae32a1SGerd Hoffmann static void uhci_async_cancel_all(UHCIState *s)
294f1ae32a1SGerd Hoffmann {
29577fa9aeeSGerd Hoffmann     UHCIQueue *queue, *nq;
296f1ae32a1SGerd Hoffmann 
29777fa9aeeSGerd Hoffmann     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
29860f8afcbSGerd Hoffmann         uhci_queue_free(queue);
299f1ae32a1SGerd Hoffmann     }
300f1ae32a1SGerd Hoffmann }
301f1ae32a1SGerd Hoffmann 
3021f250cc7SHans de Goede static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr,
3031f250cc7SHans de Goede                                      UHCI_TD *td)
304f1ae32a1SGerd Hoffmann {
305f1ae32a1SGerd Hoffmann     uint32_t token = uhci_queue_token(td);
306f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
307f1ae32a1SGerd Hoffmann     UHCIAsync *async;
308f1ae32a1SGerd Hoffmann 
309f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
310f1ae32a1SGerd Hoffmann         if (queue->token == token) {
311f1ae32a1SGerd Hoffmann             break;
312f1ae32a1SGerd Hoffmann         }
313f1ae32a1SGerd Hoffmann     }
314f1ae32a1SGerd Hoffmann     if (queue == NULL) {
315f1ae32a1SGerd Hoffmann         return NULL;
316f1ae32a1SGerd Hoffmann     }
317f1ae32a1SGerd Hoffmann 
318f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(async, &queue->asyncs, next) {
3191f250cc7SHans de Goede         if (async->td_addr == td_addr) {
320f1ae32a1SGerd Hoffmann             return async;
321f1ae32a1SGerd Hoffmann         }
322f1ae32a1SGerd Hoffmann     }
323f1ae32a1SGerd Hoffmann 
324f1ae32a1SGerd Hoffmann     return NULL;
325f1ae32a1SGerd Hoffmann }
326f1ae32a1SGerd Hoffmann 
327f1ae32a1SGerd Hoffmann static void uhci_update_irq(UHCIState *s)
328f1ae32a1SGerd Hoffmann {
329f1ae32a1SGerd Hoffmann     int level;
330f1ae32a1SGerd Hoffmann     if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
331f1ae32a1SGerd Hoffmann         ((s->status2 & 2) && (s->intr & (1 << 3))) ||
332f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
333f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
334f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HSERR) ||
335f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HCPERR)) {
336f1ae32a1SGerd Hoffmann         level = 1;
337f1ae32a1SGerd Hoffmann     } else {
338f1ae32a1SGerd Hoffmann         level = 0;
339f1ae32a1SGerd Hoffmann     }
340973002c1SGerd Hoffmann     qemu_set_irq(s->dev.irq[s->irq_pin], level);
341f1ae32a1SGerd Hoffmann }
342f1ae32a1SGerd Hoffmann 
343f1ae32a1SGerd Hoffmann static void uhci_reset(void *opaque)
344f1ae32a1SGerd Hoffmann {
345f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
346f1ae32a1SGerd Hoffmann     uint8_t *pci_conf;
347f1ae32a1SGerd Hoffmann     int i;
348f1ae32a1SGerd Hoffmann     UHCIPort *port;
349f1ae32a1SGerd Hoffmann 
35050dcc0f8SGerd Hoffmann     trace_usb_uhci_reset();
351f1ae32a1SGerd Hoffmann 
352f1ae32a1SGerd Hoffmann     pci_conf = s->dev.config;
353f1ae32a1SGerd Hoffmann 
354f1ae32a1SGerd Hoffmann     pci_conf[0x6a] = 0x01; /* usb clock */
355f1ae32a1SGerd Hoffmann     pci_conf[0x6b] = 0x00;
356f1ae32a1SGerd Hoffmann     s->cmd = 0;
357f1ae32a1SGerd Hoffmann     s->status = 0;
358f1ae32a1SGerd Hoffmann     s->status2 = 0;
359f1ae32a1SGerd Hoffmann     s->intr = 0;
360f1ae32a1SGerd Hoffmann     s->fl_base_addr = 0;
361f1ae32a1SGerd Hoffmann     s->sof_timing = 64;
362f1ae32a1SGerd Hoffmann 
363f1ae32a1SGerd Hoffmann     for(i = 0; i < NB_PORTS; i++) {
364f1ae32a1SGerd Hoffmann         port = &s->ports[i];
365f1ae32a1SGerd Hoffmann         port->ctrl = 0x0080;
366f1ae32a1SGerd Hoffmann         if (port->port.dev && port->port.dev->attached) {
367f1ae32a1SGerd Hoffmann             usb_port_reset(&port->port);
368f1ae32a1SGerd Hoffmann         }
369f1ae32a1SGerd Hoffmann     }
370f1ae32a1SGerd Hoffmann 
371f1ae32a1SGerd Hoffmann     uhci_async_cancel_all(s);
3729a16c595SGerd Hoffmann     qemu_bh_cancel(s->bh);
373aba1f242SGerd Hoffmann     uhci_update_irq(s);
374f1ae32a1SGerd Hoffmann }
375f1ae32a1SGerd Hoffmann 
376f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci_port = {
377f1ae32a1SGerd Hoffmann     .name = "uhci port",
378f1ae32a1SGerd Hoffmann     .version_id = 1,
379f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
380f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
381f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
382f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(ctrl, UHCIPort),
383f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
384f1ae32a1SGerd Hoffmann     }
385f1ae32a1SGerd Hoffmann };
386f1ae32a1SGerd Hoffmann 
38775f151cdSGerd Hoffmann static int uhci_post_load(void *opaque, int version_id)
38875f151cdSGerd Hoffmann {
38975f151cdSGerd Hoffmann     UHCIState *s = opaque;
39075f151cdSGerd Hoffmann 
39175f151cdSGerd Hoffmann     if (version_id < 2) {
39275f151cdSGerd Hoffmann         s->expire_time = qemu_get_clock_ns(vm_clock) +
39375f151cdSGerd Hoffmann             (get_ticks_per_sec() / FRAME_TIMER_FREQ);
39475f151cdSGerd Hoffmann     }
39575f151cdSGerd Hoffmann     return 0;
39675f151cdSGerd Hoffmann }
39775f151cdSGerd Hoffmann 
398f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci = {
399f1ae32a1SGerd Hoffmann     .name = "uhci",
400f1ae32a1SGerd Hoffmann     .version_id = 2,
401f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
402f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
40375f151cdSGerd Hoffmann     .post_load = uhci_post_load,
404f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
405f1ae32a1SGerd Hoffmann         VMSTATE_PCI_DEVICE(dev, UHCIState),
406f1ae32a1SGerd Hoffmann         VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
407f1ae32a1SGerd Hoffmann         VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
408f1ae32a1SGerd Hoffmann                              vmstate_uhci_port, UHCIPort),
409f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(cmd, UHCIState),
410f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(status, UHCIState),
411f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(intr, UHCIState),
412f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(frnum, UHCIState),
413f1ae32a1SGerd Hoffmann         VMSTATE_UINT32(fl_base_addr, UHCIState),
414f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(sof_timing, UHCIState),
415f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(status2, UHCIState),
416f1ae32a1SGerd Hoffmann         VMSTATE_TIMER(frame_timer, UHCIState),
417f1ae32a1SGerd Hoffmann         VMSTATE_INT64_V(expire_time, UHCIState, 2),
418f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
419f1ae32a1SGerd Hoffmann     }
420f1ae32a1SGerd Hoffmann };
421f1ae32a1SGerd Hoffmann 
422f1ae32a1SGerd Hoffmann static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
423f1ae32a1SGerd Hoffmann {
424f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
425f1ae32a1SGerd Hoffmann 
426f1ae32a1SGerd Hoffmann     addr &= 0x1f;
427f1ae32a1SGerd Hoffmann     switch(addr) {
428f1ae32a1SGerd Hoffmann     case 0x0c:
429f1ae32a1SGerd Hoffmann         s->sof_timing = val;
430f1ae32a1SGerd Hoffmann         break;
431f1ae32a1SGerd Hoffmann     }
432f1ae32a1SGerd Hoffmann }
433f1ae32a1SGerd Hoffmann 
434f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
435f1ae32a1SGerd Hoffmann {
436f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
437f1ae32a1SGerd Hoffmann     uint32_t val;
438f1ae32a1SGerd Hoffmann 
439f1ae32a1SGerd Hoffmann     addr &= 0x1f;
440f1ae32a1SGerd Hoffmann     switch(addr) {
441f1ae32a1SGerd Hoffmann     case 0x0c:
442f1ae32a1SGerd Hoffmann         val = s->sof_timing;
443f1ae32a1SGerd Hoffmann         break;
444f1ae32a1SGerd Hoffmann     default:
445f1ae32a1SGerd Hoffmann         val = 0xff;
446f1ae32a1SGerd Hoffmann         break;
447f1ae32a1SGerd Hoffmann     }
448f1ae32a1SGerd Hoffmann     return val;
449f1ae32a1SGerd Hoffmann }
450f1ae32a1SGerd Hoffmann 
451f1ae32a1SGerd Hoffmann static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
452f1ae32a1SGerd Hoffmann {
453f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
454f1ae32a1SGerd Hoffmann 
455f1ae32a1SGerd Hoffmann     addr &= 0x1f;
45650dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_writew(addr, val);
457f1ae32a1SGerd Hoffmann 
458f1ae32a1SGerd Hoffmann     switch(addr) {
459f1ae32a1SGerd Hoffmann     case 0x00:
460f1ae32a1SGerd Hoffmann         if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
461f1ae32a1SGerd Hoffmann             /* start frame processing */
46250dcc0f8SGerd Hoffmann             trace_usb_uhci_schedule_start();
463f1ae32a1SGerd Hoffmann             s->expire_time = qemu_get_clock_ns(vm_clock) +
464f1ae32a1SGerd Hoffmann                 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
465f1ae32a1SGerd Hoffmann             qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
466f1ae32a1SGerd Hoffmann             s->status &= ~UHCI_STS_HCHALTED;
467f1ae32a1SGerd Hoffmann         } else if (!(val & UHCI_CMD_RS)) {
468f1ae32a1SGerd Hoffmann             s->status |= UHCI_STS_HCHALTED;
469f1ae32a1SGerd Hoffmann         }
470f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_GRESET) {
471f1ae32a1SGerd Hoffmann             UHCIPort *port;
472f1ae32a1SGerd Hoffmann             int i;
473f1ae32a1SGerd Hoffmann 
474f1ae32a1SGerd Hoffmann             /* send reset on the USB bus */
475f1ae32a1SGerd Hoffmann             for(i = 0; i < NB_PORTS; i++) {
476f1ae32a1SGerd Hoffmann                 port = &s->ports[i];
477f1ae32a1SGerd Hoffmann                 usb_device_reset(port->port.dev);
478f1ae32a1SGerd Hoffmann             }
479f1ae32a1SGerd Hoffmann             uhci_reset(s);
480f1ae32a1SGerd Hoffmann             return;
481f1ae32a1SGerd Hoffmann         }
482f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_HCRESET) {
483f1ae32a1SGerd Hoffmann             uhci_reset(s);
484f1ae32a1SGerd Hoffmann             return;
485f1ae32a1SGerd Hoffmann         }
486f1ae32a1SGerd Hoffmann         s->cmd = val;
487f1ae32a1SGerd Hoffmann         break;
488f1ae32a1SGerd Hoffmann     case 0x02:
489f1ae32a1SGerd Hoffmann         s->status &= ~val;
490f1ae32a1SGerd Hoffmann         /* XXX: the chip spec is not coherent, so we add a hidden
491f1ae32a1SGerd Hoffmann            register to distinguish between IOC and SPD */
492f1ae32a1SGerd Hoffmann         if (val & UHCI_STS_USBINT)
493f1ae32a1SGerd Hoffmann             s->status2 = 0;
494f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
495f1ae32a1SGerd Hoffmann         break;
496f1ae32a1SGerd Hoffmann     case 0x04:
497f1ae32a1SGerd Hoffmann         s->intr = val;
498f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
499f1ae32a1SGerd Hoffmann         break;
500f1ae32a1SGerd Hoffmann     case 0x06:
501f1ae32a1SGerd Hoffmann         if (s->status & UHCI_STS_HCHALTED)
502f1ae32a1SGerd Hoffmann             s->frnum = val & 0x7ff;
503f1ae32a1SGerd Hoffmann         break;
504f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
505f1ae32a1SGerd Hoffmann         {
506f1ae32a1SGerd Hoffmann             UHCIPort *port;
507f1ae32a1SGerd Hoffmann             USBDevice *dev;
508f1ae32a1SGerd Hoffmann             int n;
509f1ae32a1SGerd Hoffmann 
510f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
511f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
512f1ae32a1SGerd Hoffmann                 return;
513f1ae32a1SGerd Hoffmann             port = &s->ports[n];
514f1ae32a1SGerd Hoffmann             dev = port->port.dev;
515f1ae32a1SGerd Hoffmann             if (dev && dev->attached) {
516f1ae32a1SGerd Hoffmann                 /* port reset */
517f1ae32a1SGerd Hoffmann                 if ( (val & UHCI_PORT_RESET) &&
518f1ae32a1SGerd Hoffmann                      !(port->ctrl & UHCI_PORT_RESET) ) {
519f1ae32a1SGerd Hoffmann                     usb_device_reset(dev);
520f1ae32a1SGerd Hoffmann                 }
521f1ae32a1SGerd Hoffmann             }
522f1ae32a1SGerd Hoffmann             port->ctrl &= UHCI_PORT_READ_ONLY;
523f1ae32a1SGerd Hoffmann             port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
524f1ae32a1SGerd Hoffmann             /* some bits are reset when a '1' is written to them */
525f1ae32a1SGerd Hoffmann             port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
526f1ae32a1SGerd Hoffmann         }
527f1ae32a1SGerd Hoffmann         break;
528f1ae32a1SGerd Hoffmann     }
529f1ae32a1SGerd Hoffmann }
530f1ae32a1SGerd Hoffmann 
531f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
532f1ae32a1SGerd Hoffmann {
533f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
534f1ae32a1SGerd Hoffmann     uint32_t val;
535f1ae32a1SGerd Hoffmann 
536f1ae32a1SGerd Hoffmann     addr &= 0x1f;
537f1ae32a1SGerd Hoffmann     switch(addr) {
538f1ae32a1SGerd Hoffmann     case 0x00:
539f1ae32a1SGerd Hoffmann         val = s->cmd;
540f1ae32a1SGerd Hoffmann         break;
541f1ae32a1SGerd Hoffmann     case 0x02:
542f1ae32a1SGerd Hoffmann         val = s->status;
543f1ae32a1SGerd Hoffmann         break;
544f1ae32a1SGerd Hoffmann     case 0x04:
545f1ae32a1SGerd Hoffmann         val = s->intr;
546f1ae32a1SGerd Hoffmann         break;
547f1ae32a1SGerd Hoffmann     case 0x06:
548f1ae32a1SGerd Hoffmann         val = s->frnum;
549f1ae32a1SGerd Hoffmann         break;
550f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
551f1ae32a1SGerd Hoffmann         {
552f1ae32a1SGerd Hoffmann             UHCIPort *port;
553f1ae32a1SGerd Hoffmann             int n;
554f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
555f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
556f1ae32a1SGerd Hoffmann                 goto read_default;
557f1ae32a1SGerd Hoffmann             port = &s->ports[n];
558f1ae32a1SGerd Hoffmann             val = port->ctrl;
559f1ae32a1SGerd Hoffmann         }
560f1ae32a1SGerd Hoffmann         break;
561f1ae32a1SGerd Hoffmann     default:
562f1ae32a1SGerd Hoffmann     read_default:
563f1ae32a1SGerd Hoffmann         val = 0xff7f; /* disabled port */
564f1ae32a1SGerd Hoffmann         break;
565f1ae32a1SGerd Hoffmann     }
566f1ae32a1SGerd Hoffmann 
56750dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_readw(addr, val);
568f1ae32a1SGerd Hoffmann 
569f1ae32a1SGerd Hoffmann     return val;
570f1ae32a1SGerd Hoffmann }
571f1ae32a1SGerd Hoffmann 
572f1ae32a1SGerd Hoffmann static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
573f1ae32a1SGerd Hoffmann {
574f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
575f1ae32a1SGerd Hoffmann 
576f1ae32a1SGerd Hoffmann     addr &= 0x1f;
57750dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_writel(addr, val);
578f1ae32a1SGerd Hoffmann 
579f1ae32a1SGerd Hoffmann     switch(addr) {
580f1ae32a1SGerd Hoffmann     case 0x08:
581f1ae32a1SGerd Hoffmann         s->fl_base_addr = val & ~0xfff;
582f1ae32a1SGerd Hoffmann         break;
583f1ae32a1SGerd Hoffmann     }
584f1ae32a1SGerd Hoffmann }
585f1ae32a1SGerd Hoffmann 
586f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
587f1ae32a1SGerd Hoffmann {
588f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
589f1ae32a1SGerd Hoffmann     uint32_t val;
590f1ae32a1SGerd Hoffmann 
591f1ae32a1SGerd Hoffmann     addr &= 0x1f;
592f1ae32a1SGerd Hoffmann     switch(addr) {
593f1ae32a1SGerd Hoffmann     case 0x08:
594f1ae32a1SGerd Hoffmann         val = s->fl_base_addr;
595f1ae32a1SGerd Hoffmann         break;
596f1ae32a1SGerd Hoffmann     default:
597f1ae32a1SGerd Hoffmann         val = 0xffffffff;
598f1ae32a1SGerd Hoffmann         break;
599f1ae32a1SGerd Hoffmann     }
60050dcc0f8SGerd Hoffmann     trace_usb_uhci_mmio_readl(addr, val);
601f1ae32a1SGerd Hoffmann     return val;
602f1ae32a1SGerd Hoffmann }
603f1ae32a1SGerd Hoffmann 
604f1ae32a1SGerd Hoffmann /* signal resume if controller suspended */
605f1ae32a1SGerd Hoffmann static void uhci_resume (void *opaque)
606f1ae32a1SGerd Hoffmann {
607f1ae32a1SGerd Hoffmann     UHCIState *s = (UHCIState *)opaque;
608f1ae32a1SGerd Hoffmann 
609f1ae32a1SGerd Hoffmann     if (!s)
610f1ae32a1SGerd Hoffmann         return;
611f1ae32a1SGerd Hoffmann 
612f1ae32a1SGerd Hoffmann     if (s->cmd & UHCI_CMD_EGSM) {
613f1ae32a1SGerd Hoffmann         s->cmd |= UHCI_CMD_FGR;
614f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_RD;
615f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
616f1ae32a1SGerd Hoffmann     }
617f1ae32a1SGerd Hoffmann }
618f1ae32a1SGerd Hoffmann 
619f1ae32a1SGerd Hoffmann static void uhci_attach(USBPort *port1)
620f1ae32a1SGerd Hoffmann {
621f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
622f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
623f1ae32a1SGerd Hoffmann 
624f1ae32a1SGerd Hoffmann     /* set connect status */
625f1ae32a1SGerd Hoffmann     port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
626f1ae32a1SGerd Hoffmann 
627f1ae32a1SGerd Hoffmann     /* update speed */
628f1ae32a1SGerd Hoffmann     if (port->port.dev->speed == USB_SPEED_LOW) {
629f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_LSDA;
630f1ae32a1SGerd Hoffmann     } else {
631f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_LSDA;
632f1ae32a1SGerd Hoffmann     }
633f1ae32a1SGerd Hoffmann 
634f1ae32a1SGerd Hoffmann     uhci_resume(s);
635f1ae32a1SGerd Hoffmann }
636f1ae32a1SGerd Hoffmann 
637f1ae32a1SGerd Hoffmann static void uhci_detach(USBPort *port1)
638f1ae32a1SGerd Hoffmann {
639f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
640f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
641f1ae32a1SGerd Hoffmann 
642f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, port1->dev);
643f1ae32a1SGerd Hoffmann 
644f1ae32a1SGerd Hoffmann     /* set connect status */
645f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_CCS) {
646f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_CCS;
647f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_CSC;
648f1ae32a1SGerd Hoffmann     }
649f1ae32a1SGerd Hoffmann     /* disable port */
650f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_EN) {
651f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_EN;
652f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_ENC;
653f1ae32a1SGerd Hoffmann     }
654f1ae32a1SGerd Hoffmann 
655f1ae32a1SGerd Hoffmann     uhci_resume(s);
656f1ae32a1SGerd Hoffmann }
657f1ae32a1SGerd Hoffmann 
658f1ae32a1SGerd Hoffmann static void uhci_child_detach(USBPort *port1, USBDevice *child)
659f1ae32a1SGerd Hoffmann {
660f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
661f1ae32a1SGerd Hoffmann 
662f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, child);
663f1ae32a1SGerd Hoffmann }
664f1ae32a1SGerd Hoffmann 
665f1ae32a1SGerd Hoffmann static void uhci_wakeup(USBPort *port1)
666f1ae32a1SGerd Hoffmann {
667f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
668f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
669f1ae32a1SGerd Hoffmann 
670f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
671f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_RD;
672f1ae32a1SGerd Hoffmann         uhci_resume(s);
673f1ae32a1SGerd Hoffmann     }
674f1ae32a1SGerd Hoffmann }
675f1ae32a1SGerd Hoffmann 
676f1ae32a1SGerd Hoffmann static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
677f1ae32a1SGerd Hoffmann {
678f1ae32a1SGerd Hoffmann     USBDevice *dev;
679f1ae32a1SGerd Hoffmann     int i;
680f1ae32a1SGerd Hoffmann 
681f1ae32a1SGerd Hoffmann     for (i = 0; i < NB_PORTS; i++) {
682f1ae32a1SGerd Hoffmann         UHCIPort *port = &s->ports[i];
683f1ae32a1SGerd Hoffmann         if (!(port->ctrl & UHCI_PORT_EN)) {
684f1ae32a1SGerd Hoffmann             continue;
685f1ae32a1SGerd Hoffmann         }
686f1ae32a1SGerd Hoffmann         dev = usb_find_device(&port->port, addr);
687f1ae32a1SGerd Hoffmann         if (dev != NULL) {
688f1ae32a1SGerd Hoffmann             return dev;
689f1ae32a1SGerd Hoffmann         }
690f1ae32a1SGerd Hoffmann     }
691f1ae32a1SGerd Hoffmann     return NULL;
692f1ae32a1SGerd Hoffmann }
693f1ae32a1SGerd Hoffmann 
694963a68b5SHans de Goede static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
695963a68b5SHans de Goede {
696963a68b5SHans de Goede     pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
697963a68b5SHans de Goede     le32_to_cpus(&td->link);
698963a68b5SHans de Goede     le32_to_cpus(&td->ctrl);
699963a68b5SHans de Goede     le32_to_cpus(&td->token);
700963a68b5SHans de Goede     le32_to_cpus(&td->buffer);
701963a68b5SHans de Goede }
702963a68b5SHans de Goede 
703f1ae32a1SGerd Hoffmann static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
704f1ae32a1SGerd Hoffmann {
705f1ae32a1SGerd Hoffmann     int len = 0, max_len, err, ret;
706f1ae32a1SGerd Hoffmann     uint8_t pid;
707f1ae32a1SGerd Hoffmann 
708f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
709f1ae32a1SGerd Hoffmann     pid = td->token & 0xff;
710f1ae32a1SGerd Hoffmann 
711f1ae32a1SGerd Hoffmann     ret = async->packet.result;
712f1ae32a1SGerd Hoffmann 
713f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOS)
714f1ae32a1SGerd Hoffmann         td->ctrl &= ~TD_CTRL_ACTIVE;
715f1ae32a1SGerd Hoffmann 
716f1ae32a1SGerd Hoffmann     if (ret < 0)
717f1ae32a1SGerd Hoffmann         goto out;
718f1ae32a1SGerd Hoffmann 
719f1ae32a1SGerd Hoffmann     len = async->packet.result;
720f1ae32a1SGerd Hoffmann     td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
721f1ae32a1SGerd Hoffmann 
722f1ae32a1SGerd Hoffmann     /* The NAK bit may have been set by a previous frame, so clear it
723f1ae32a1SGerd Hoffmann        here.  The docs are somewhat unclear, but win2k relies on this
724f1ae32a1SGerd Hoffmann        behavior.  */
725f1ae32a1SGerd Hoffmann     td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
726f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOC)
727f1ae32a1SGerd Hoffmann         *int_mask |= 0x01;
728f1ae32a1SGerd Hoffmann 
729f1ae32a1SGerd Hoffmann     if (pid == USB_TOKEN_IN) {
730f1ae32a1SGerd Hoffmann         if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
731f1ae32a1SGerd Hoffmann             *int_mask |= 0x02;
732f1ae32a1SGerd Hoffmann             /* short packet: do not update QH */
73350dcc0f8SGerd Hoffmann             trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
7341f250cc7SHans de Goede                                                      async->td_addr);
73560e1b2a6SGerd Hoffmann             return TD_RESULT_NEXT_QH;
736f1ae32a1SGerd Hoffmann         }
737f1ae32a1SGerd Hoffmann     }
738f1ae32a1SGerd Hoffmann 
739f1ae32a1SGerd Hoffmann     /* success */
7401f250cc7SHans de Goede     trace_usb_uhci_packet_complete_success(async->queue->token,
7411f250cc7SHans de Goede                                            async->td_addr);
74260e1b2a6SGerd Hoffmann     return TD_RESULT_COMPLETE;
743f1ae32a1SGerd Hoffmann 
744f1ae32a1SGerd Hoffmann out:
745f1ae32a1SGerd Hoffmann     switch(ret) {
746a89e255bSHans de Goede     case USB_RET_NAK:
747a89e255bSHans de Goede         td->ctrl |= TD_CTRL_NAK;
748a89e255bSHans de Goede         return TD_RESULT_NEXT_QH;
749a89e255bSHans de Goede 
750f1ae32a1SGerd Hoffmann     case USB_RET_STALL:
751f1ae32a1SGerd Hoffmann         td->ctrl |= TD_CTRL_STALL;
7521f250cc7SHans de Goede         trace_usb_uhci_packet_complete_stall(async->queue->token,
7531f250cc7SHans de Goede                                              async->td_addr);
754a89e255bSHans de Goede         err = TD_RESULT_NEXT_QH;
755a89e255bSHans de Goede         break;
756f1ae32a1SGerd Hoffmann 
757f1ae32a1SGerd Hoffmann     case USB_RET_BABBLE:
758f1ae32a1SGerd Hoffmann         td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
759f1ae32a1SGerd Hoffmann         /* frame interrupted */
7601f250cc7SHans de Goede         trace_usb_uhci_packet_complete_babble(async->queue->token,
7611f250cc7SHans de Goede                                               async->td_addr);
762a89e255bSHans de Goede         err = TD_RESULT_STOP_FRAME;
763f1ae32a1SGerd Hoffmann         break;
764f1ae32a1SGerd Hoffmann 
765f1ae32a1SGerd Hoffmann     case USB_RET_IOERROR:
766f1ae32a1SGerd Hoffmann     case USB_RET_NODEV:
767f1ae32a1SGerd Hoffmann     default:
768a89e255bSHans de Goede         td->ctrl |= TD_CTRL_TIMEOUT;
769a89e255bSHans de Goede         td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
7701f250cc7SHans de Goede         trace_usb_uhci_packet_complete_error(async->queue->token,
7711f250cc7SHans de Goede                                              async->td_addr);
772a89e255bSHans de Goede         err = TD_RESULT_NEXT_QH;
773f1ae32a1SGerd Hoffmann         break;
774f1ae32a1SGerd Hoffmann     }
775f1ae32a1SGerd Hoffmann 
776f1ae32a1SGerd Hoffmann     td->ctrl &= ~TD_CTRL_ACTIVE;
777f1ae32a1SGerd Hoffmann     s->status |= UHCI_STS_USBERR;
778a89e255bSHans de Goede     if (td->ctrl & TD_CTRL_IOC) {
779f1ae32a1SGerd Hoffmann         *int_mask |= 0x01;
780a89e255bSHans de Goede     }
781f1ae32a1SGerd Hoffmann     uhci_update_irq(s);
782a89e255bSHans de Goede     return err;
783f1ae32a1SGerd Hoffmann }
784f1ae32a1SGerd Hoffmann 
785a4f30cd7SHans de Goede static int uhci_handle_td(UHCIState *s, UHCIQueue *q,
786a4f30cd7SHans de Goede                           UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
787f1ae32a1SGerd Hoffmann {
788f1ae32a1SGerd Hoffmann     UHCIAsync *async;
789f1ae32a1SGerd Hoffmann     int len = 0, max_len;
7906ba43f1fSHans de Goede     bool spd;
791a4f30cd7SHans de Goede     bool queuing = (q != NULL);
79211d15e40SHans de Goede     uint8_t pid = td->token & 0xff;
793f1ae32a1SGerd Hoffmann 
794f1ae32a1SGerd Hoffmann     /* Is active ? */
795883bca77SHans de Goede     if (!(td->ctrl & TD_CTRL_ACTIVE)) {
796883bca77SHans de Goede         /*
797883bca77SHans de Goede          * ehci11d spec page 22: "Even if the Active bit in the TD is already
798883bca77SHans de Goede          * cleared when the TD is fetched ... an IOC interrupt is generated"
799883bca77SHans de Goede          */
800883bca77SHans de Goede         if (td->ctrl & TD_CTRL_IOC) {
801883bca77SHans de Goede                 *int_mask |= 0x01;
802883bca77SHans de Goede         }
80360e1b2a6SGerd Hoffmann         return TD_RESULT_NEXT_QH;
804883bca77SHans de Goede     }
805f1ae32a1SGerd Hoffmann 
8061f250cc7SHans de Goede     async = uhci_async_find_td(s, td_addr, td);
807f1ae32a1SGerd Hoffmann     if (async) {
808f1ae32a1SGerd Hoffmann         /* Already submitted */
809f1ae32a1SGerd Hoffmann         async->queue->valid = 32;
810f1ae32a1SGerd Hoffmann 
811f1ae32a1SGerd Hoffmann         if (!async->done)
8124efe4ef3SGerd Hoffmann             return TD_RESULT_ASYNC_CONT;
813ee008ba6SGerd Hoffmann         if (queuing) {
814ee008ba6SGerd Hoffmann             /* we are busy filling the queue, we are not prepared
815ee008ba6SGerd Hoffmann                to consume completed packages then, just leave them
816ee008ba6SGerd Hoffmann                in async state */
817ee008ba6SGerd Hoffmann             return TD_RESULT_ASYNC_CONT;
818ee008ba6SGerd Hoffmann         }
819f1ae32a1SGerd Hoffmann 
820f1ae32a1SGerd Hoffmann         uhci_async_unlink(async);
821f1ae32a1SGerd Hoffmann         goto done;
822f1ae32a1SGerd Hoffmann     }
823f1ae32a1SGerd Hoffmann 
824f1ae32a1SGerd Hoffmann     /* Allocate new packet */
825a4f30cd7SHans de Goede     if (q == NULL) {
82611d15e40SHans de Goede         USBDevice *dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
82711d15e40SHans de Goede         USBEndpoint *ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
82811d15e40SHans de Goede         q = uhci_queue_get(s, td, ep);
829a4f30cd7SHans de Goede     }
830a4f30cd7SHans de Goede     async = uhci_async_alloc(q, td_addr);
831f1ae32a1SGerd Hoffmann 
832f1ae32a1SGerd Hoffmann     /* valid needs to be large enough to handle 10 frame delay
833f1ae32a1SGerd Hoffmann      * for initial isochronous requests
834f1ae32a1SGerd Hoffmann      */
835f1ae32a1SGerd Hoffmann     async->queue->valid = 32;
836f1ae32a1SGerd Hoffmann 
837f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
8386ba43f1fSHans de Goede     spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
83911d15e40SHans de Goede     usb_packet_setup(&async->packet, pid, q->ep, td_addr, spd,
840a6fb2ddbSHans de Goede                      (td->ctrl & TD_CTRL_IOC) != 0);
841f1ae32a1SGerd Hoffmann     qemu_sglist_add(&async->sgl, td->buffer, max_len);
842f1ae32a1SGerd Hoffmann     usb_packet_map(&async->packet, &async->sgl);
843f1ae32a1SGerd Hoffmann 
844f1ae32a1SGerd Hoffmann     switch(pid) {
845f1ae32a1SGerd Hoffmann     case USB_TOKEN_OUT:
846f1ae32a1SGerd Hoffmann     case USB_TOKEN_SETUP:
84711d15e40SHans de Goede         len = usb_handle_packet(q->ep->dev, &async->packet);
848f1ae32a1SGerd Hoffmann         if (len >= 0)
849f1ae32a1SGerd Hoffmann             len = max_len;
850f1ae32a1SGerd Hoffmann         break;
851f1ae32a1SGerd Hoffmann 
852f1ae32a1SGerd Hoffmann     case USB_TOKEN_IN:
85311d15e40SHans de Goede         len = usb_handle_packet(q->ep->dev, &async->packet);
854f1ae32a1SGerd Hoffmann         break;
855f1ae32a1SGerd Hoffmann 
856f1ae32a1SGerd Hoffmann     default:
857f1ae32a1SGerd Hoffmann         /* invalid pid : frame interrupted */
85800a0770dSHans de Goede         usb_packet_unmap(&async->packet, &async->sgl);
859f1ae32a1SGerd Hoffmann         uhci_async_free(async);
860f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_HCPERR;
861f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
86260e1b2a6SGerd Hoffmann         return TD_RESULT_STOP_FRAME;
863f1ae32a1SGerd Hoffmann     }
864f1ae32a1SGerd Hoffmann 
865f1ae32a1SGerd Hoffmann     if (len == USB_RET_ASYNC) {
866f1ae32a1SGerd Hoffmann         uhci_async_link(async);
867a4f30cd7SHans de Goede         if (!queuing) {
86811d15e40SHans de Goede             uhci_queue_fill(q, td);
869a4f30cd7SHans de Goede         }
8704efe4ef3SGerd Hoffmann         return TD_RESULT_ASYNC_START;
871f1ae32a1SGerd Hoffmann     }
872f1ae32a1SGerd Hoffmann 
873f1ae32a1SGerd Hoffmann     async->packet.result = len;
874f1ae32a1SGerd Hoffmann 
875f1ae32a1SGerd Hoffmann done:
876f1ae32a1SGerd Hoffmann     len = uhci_complete_td(s, td, async, int_mask);
877e2f89926SDavid Gibson     usb_packet_unmap(&async->packet, &async->sgl);
878f1ae32a1SGerd Hoffmann     uhci_async_free(async);
879f1ae32a1SGerd Hoffmann     return len;
880f1ae32a1SGerd Hoffmann }
881f1ae32a1SGerd Hoffmann 
882f1ae32a1SGerd Hoffmann static void uhci_async_complete(USBPort *port, USBPacket *packet)
883f1ae32a1SGerd Hoffmann {
884f1ae32a1SGerd Hoffmann     UHCIAsync *async = container_of(packet, UHCIAsync, packet);
885f1ae32a1SGerd Hoffmann     UHCIState *s = async->queue->uhci;
886f1ae32a1SGerd Hoffmann 
8870cae7b1aSHans de Goede     if (packet->result == USB_RET_REMOVE_FROM_QUEUE) {
8880cae7b1aSHans de Goede         uhci_async_unlink(async);
8890cae7b1aSHans de Goede         uhci_async_cancel(async);
8900cae7b1aSHans de Goede         return;
8910cae7b1aSHans de Goede     }
8920cae7b1aSHans de Goede 
893f1ae32a1SGerd Hoffmann     async->done = 1;
89440141d12SGerd Hoffmann     if (s->frame_bytes < s->frame_bandwidth) {
8959a16c595SGerd Hoffmann         qemu_bh_schedule(s->bh);
8969a16c595SGerd Hoffmann     }
897f1ae32a1SGerd Hoffmann }
898f1ae32a1SGerd Hoffmann 
899f1ae32a1SGerd Hoffmann static int is_valid(uint32_t link)
900f1ae32a1SGerd Hoffmann {
901f1ae32a1SGerd Hoffmann     return (link & 1) == 0;
902f1ae32a1SGerd Hoffmann }
903f1ae32a1SGerd Hoffmann 
904f1ae32a1SGerd Hoffmann static int is_qh(uint32_t link)
905f1ae32a1SGerd Hoffmann {
906f1ae32a1SGerd Hoffmann     return (link & 2) != 0;
907f1ae32a1SGerd Hoffmann }
908f1ae32a1SGerd Hoffmann 
909f1ae32a1SGerd Hoffmann static int depth_first(uint32_t link)
910f1ae32a1SGerd Hoffmann {
911f1ae32a1SGerd Hoffmann     return (link & 4) != 0;
912f1ae32a1SGerd Hoffmann }
913f1ae32a1SGerd Hoffmann 
914f1ae32a1SGerd Hoffmann /* QH DB used for detecting QH loops */
915f1ae32a1SGerd Hoffmann #define UHCI_MAX_QUEUES 128
916f1ae32a1SGerd Hoffmann typedef struct {
917f1ae32a1SGerd Hoffmann     uint32_t addr[UHCI_MAX_QUEUES];
918f1ae32a1SGerd Hoffmann     int      count;
919f1ae32a1SGerd Hoffmann } QhDb;
920f1ae32a1SGerd Hoffmann 
921f1ae32a1SGerd Hoffmann static void qhdb_reset(QhDb *db)
922f1ae32a1SGerd Hoffmann {
923f1ae32a1SGerd Hoffmann     db->count = 0;
924f1ae32a1SGerd Hoffmann }
925f1ae32a1SGerd Hoffmann 
926f1ae32a1SGerd Hoffmann /* Add QH to DB. Returns 1 if already present or DB is full. */
927f1ae32a1SGerd Hoffmann static int qhdb_insert(QhDb *db, uint32_t addr)
928f1ae32a1SGerd Hoffmann {
929f1ae32a1SGerd Hoffmann     int i;
930f1ae32a1SGerd Hoffmann     for (i = 0; i < db->count; i++)
931f1ae32a1SGerd Hoffmann         if (db->addr[i] == addr)
932f1ae32a1SGerd Hoffmann             return 1;
933f1ae32a1SGerd Hoffmann 
934f1ae32a1SGerd Hoffmann     if (db->count >= UHCI_MAX_QUEUES)
935f1ae32a1SGerd Hoffmann         return 1;
936f1ae32a1SGerd Hoffmann 
937f1ae32a1SGerd Hoffmann     db->addr[db->count++] = addr;
938f1ae32a1SGerd Hoffmann     return 0;
939f1ae32a1SGerd Hoffmann }
940f1ae32a1SGerd Hoffmann 
94111d15e40SHans de Goede static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
942f1ae32a1SGerd Hoffmann {
943f1ae32a1SGerd Hoffmann     uint32_t int_mask = 0;
944f1ae32a1SGerd Hoffmann     uint32_t plink = td->link;
945f1ae32a1SGerd Hoffmann     UHCI_TD ptd;
946f1ae32a1SGerd Hoffmann     int ret;
947f1ae32a1SGerd Hoffmann 
9486ba43f1fSHans de Goede     while (is_valid(plink)) {
949a4f30cd7SHans de Goede         uhci_read_td(q->uhci, &ptd, plink);
950f1ae32a1SGerd Hoffmann         if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
951f1ae32a1SGerd Hoffmann             break;
952f1ae32a1SGerd Hoffmann         }
953a4f30cd7SHans de Goede         if (uhci_queue_token(&ptd) != q->token) {
954f1ae32a1SGerd Hoffmann             break;
955f1ae32a1SGerd Hoffmann         }
95650dcc0f8SGerd Hoffmann         trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
957a4f30cd7SHans de Goede         ret = uhci_handle_td(q->uhci, q, &ptd, plink, &int_mask);
95852b0fecdSGerd Hoffmann         if (ret == TD_RESULT_ASYNC_CONT) {
95952b0fecdSGerd Hoffmann             break;
96052b0fecdSGerd Hoffmann         }
9614efe4ef3SGerd Hoffmann         assert(ret == TD_RESULT_ASYNC_START);
962f1ae32a1SGerd Hoffmann         assert(int_mask == 0);
963f1ae32a1SGerd Hoffmann         plink = ptd.link;
964f1ae32a1SGerd Hoffmann     }
96511d15e40SHans de Goede     usb_device_flush_ep_queue(q->ep->dev, q->ep);
966f1ae32a1SGerd Hoffmann }
967f1ae32a1SGerd Hoffmann 
968f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s)
969f1ae32a1SGerd Hoffmann {
970f1ae32a1SGerd Hoffmann     uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
9714aed20e2SGerd Hoffmann     uint32_t curr_qh, td_count = 0;
972f1ae32a1SGerd Hoffmann     int cnt, ret;
973f1ae32a1SGerd Hoffmann     UHCI_TD td;
974f1ae32a1SGerd Hoffmann     UHCI_QH qh;
975f1ae32a1SGerd Hoffmann     QhDb qhdb;
976f1ae32a1SGerd Hoffmann 
977f1ae32a1SGerd Hoffmann     frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
978f1ae32a1SGerd Hoffmann 
979f1ae32a1SGerd Hoffmann     pci_dma_read(&s->dev, frame_addr, &link, 4);
980f1ae32a1SGerd Hoffmann     le32_to_cpus(&link);
981f1ae32a1SGerd Hoffmann 
982f1ae32a1SGerd Hoffmann     int_mask = 0;
983f1ae32a1SGerd Hoffmann     curr_qh  = 0;
984f1ae32a1SGerd Hoffmann 
985f1ae32a1SGerd Hoffmann     qhdb_reset(&qhdb);
986f1ae32a1SGerd Hoffmann 
987f1ae32a1SGerd Hoffmann     for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
98840141d12SGerd Hoffmann         if (s->frame_bytes >= s->frame_bandwidth) {
9894aed20e2SGerd Hoffmann             /* We've reached the usb 1.1 bandwidth, which is
9904aed20e2SGerd Hoffmann                1280 bytes/frame, stop processing */
9914aed20e2SGerd Hoffmann             trace_usb_uhci_frame_stop_bandwidth();
9924aed20e2SGerd Hoffmann             break;
9934aed20e2SGerd Hoffmann         }
994f1ae32a1SGerd Hoffmann         if (is_qh(link)) {
995f1ae32a1SGerd Hoffmann             /* QH */
99650dcc0f8SGerd Hoffmann             trace_usb_uhci_qh_load(link & ~0xf);
997f1ae32a1SGerd Hoffmann 
998f1ae32a1SGerd Hoffmann             if (qhdb_insert(&qhdb, link)) {
999f1ae32a1SGerd Hoffmann                 /*
1000f1ae32a1SGerd Hoffmann                  * We're going in circles. Which is not a bug because
1001f1ae32a1SGerd Hoffmann                  * HCD is allowed to do that as part of the BW management.
1002f1ae32a1SGerd Hoffmann                  *
10034aed20e2SGerd Hoffmann                  * Stop processing here if no transaction has been done
10044aed20e2SGerd Hoffmann                  * since we've been here last time.
1005f1ae32a1SGerd Hoffmann                  */
1006f1ae32a1SGerd Hoffmann                 if (td_count == 0) {
100750dcc0f8SGerd Hoffmann                     trace_usb_uhci_frame_loop_stop_idle();
1008f1ae32a1SGerd Hoffmann                     break;
1009f1ae32a1SGerd Hoffmann                 } else {
101050dcc0f8SGerd Hoffmann                     trace_usb_uhci_frame_loop_continue();
1011f1ae32a1SGerd Hoffmann                     td_count = 0;
1012f1ae32a1SGerd Hoffmann                     qhdb_reset(&qhdb);
1013f1ae32a1SGerd Hoffmann                     qhdb_insert(&qhdb, link);
1014f1ae32a1SGerd Hoffmann                 }
1015f1ae32a1SGerd Hoffmann             }
1016f1ae32a1SGerd Hoffmann 
1017f1ae32a1SGerd Hoffmann             pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1018f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.link);
1019f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.el_link);
1020f1ae32a1SGerd Hoffmann 
1021f1ae32a1SGerd Hoffmann             if (!is_valid(qh.el_link)) {
1022f1ae32a1SGerd Hoffmann                 /* QH w/o elements */
1023f1ae32a1SGerd Hoffmann                 curr_qh = 0;
1024f1ae32a1SGerd Hoffmann                 link = qh.link;
1025f1ae32a1SGerd Hoffmann             } else {
1026f1ae32a1SGerd Hoffmann                 /* QH with elements */
1027f1ae32a1SGerd Hoffmann             	curr_qh = link;
1028f1ae32a1SGerd Hoffmann             	link = qh.el_link;
1029f1ae32a1SGerd Hoffmann             }
1030f1ae32a1SGerd Hoffmann             continue;
1031f1ae32a1SGerd Hoffmann         }
1032f1ae32a1SGerd Hoffmann 
1033f1ae32a1SGerd Hoffmann         /* TD */
1034963a68b5SHans de Goede         uhci_read_td(s, &td, link);
103550dcc0f8SGerd Hoffmann         trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
1036f1ae32a1SGerd Hoffmann 
1037f1ae32a1SGerd Hoffmann         old_td_ctrl = td.ctrl;
1038a4f30cd7SHans de Goede         ret = uhci_handle_td(s, NULL, &td, link, &int_mask);
1039f1ae32a1SGerd Hoffmann         if (old_td_ctrl != td.ctrl) {
1040f1ae32a1SGerd Hoffmann             /* update the status bits of the TD */
1041f1ae32a1SGerd Hoffmann             val = cpu_to_le32(td.ctrl);
1042f1ae32a1SGerd Hoffmann             pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1043f1ae32a1SGerd Hoffmann         }
1044f1ae32a1SGerd Hoffmann 
1045f1ae32a1SGerd Hoffmann         switch (ret) {
104660e1b2a6SGerd Hoffmann         case TD_RESULT_STOP_FRAME: /* interrupted frame */
1047f1ae32a1SGerd Hoffmann             goto out;
1048f1ae32a1SGerd Hoffmann 
104960e1b2a6SGerd Hoffmann         case TD_RESULT_NEXT_QH:
10504efe4ef3SGerd Hoffmann         case TD_RESULT_ASYNC_CONT:
105150dcc0f8SGerd Hoffmann             trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
1052f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1053f1ae32a1SGerd Hoffmann             continue;
1054f1ae32a1SGerd Hoffmann 
10554efe4ef3SGerd Hoffmann         case TD_RESULT_ASYNC_START:
105650dcc0f8SGerd Hoffmann             trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
1057f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1058f1ae32a1SGerd Hoffmann             continue;
1059f1ae32a1SGerd Hoffmann 
106060e1b2a6SGerd Hoffmann         case TD_RESULT_COMPLETE:
106150dcc0f8SGerd Hoffmann             trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
1062f1ae32a1SGerd Hoffmann             link = td.link;
1063f1ae32a1SGerd Hoffmann             td_count++;
10644aed20e2SGerd Hoffmann             s->frame_bytes += (td.ctrl & 0x7ff) + 1;
1065f1ae32a1SGerd Hoffmann 
1066f1ae32a1SGerd Hoffmann             if (curr_qh) {
1067f1ae32a1SGerd Hoffmann                 /* update QH element link */
1068f1ae32a1SGerd Hoffmann                 qh.el_link = link;
1069f1ae32a1SGerd Hoffmann                 val = cpu_to_le32(qh.el_link);
1070f1ae32a1SGerd Hoffmann                 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1071f1ae32a1SGerd Hoffmann 
1072f1ae32a1SGerd Hoffmann                 if (!depth_first(link)) {
1073f1ae32a1SGerd Hoffmann                     /* done with this QH */
1074f1ae32a1SGerd Hoffmann                     curr_qh = 0;
1075f1ae32a1SGerd Hoffmann                     link    = qh.link;
1076f1ae32a1SGerd Hoffmann                 }
1077f1ae32a1SGerd Hoffmann             }
1078f1ae32a1SGerd Hoffmann             break;
1079f1ae32a1SGerd Hoffmann 
1080f1ae32a1SGerd Hoffmann         default:
1081f1ae32a1SGerd Hoffmann             assert(!"unknown return code");
1082f1ae32a1SGerd Hoffmann         }
1083f1ae32a1SGerd Hoffmann 
1084f1ae32a1SGerd Hoffmann         /* go to the next entry */
1085f1ae32a1SGerd Hoffmann     }
1086f1ae32a1SGerd Hoffmann 
1087f1ae32a1SGerd Hoffmann out:
1088f1ae32a1SGerd Hoffmann     s->pending_int_mask |= int_mask;
1089f1ae32a1SGerd Hoffmann }
1090f1ae32a1SGerd Hoffmann 
10919a16c595SGerd Hoffmann static void uhci_bh(void *opaque)
10929a16c595SGerd Hoffmann {
10939a16c595SGerd Hoffmann     UHCIState *s = opaque;
10949a16c595SGerd Hoffmann     uhci_process_frame(s);
10959a16c595SGerd Hoffmann }
10969a16c595SGerd Hoffmann 
1097f1ae32a1SGerd Hoffmann static void uhci_frame_timer(void *opaque)
1098f1ae32a1SGerd Hoffmann {
1099f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
1100f1ae32a1SGerd Hoffmann 
1101f1ae32a1SGerd Hoffmann     /* prepare the timer for the next frame */
1102f1ae32a1SGerd Hoffmann     s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
11034aed20e2SGerd Hoffmann     s->frame_bytes = 0;
11049a16c595SGerd Hoffmann     qemu_bh_cancel(s->bh);
1105f1ae32a1SGerd Hoffmann 
1106f1ae32a1SGerd Hoffmann     if (!(s->cmd & UHCI_CMD_RS)) {
1107f1ae32a1SGerd Hoffmann         /* Full stop */
110850dcc0f8SGerd Hoffmann         trace_usb_uhci_schedule_stop();
1109f1ae32a1SGerd Hoffmann         qemu_del_timer(s->frame_timer);
1110d9a528dbSGerd Hoffmann         uhci_async_cancel_all(s);
1111f1ae32a1SGerd Hoffmann         /* set hchalted bit in status - UHCI11D 2.1.2 */
1112f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_HCHALTED;
1113f1ae32a1SGerd Hoffmann         return;
1114f1ae32a1SGerd Hoffmann     }
1115f1ae32a1SGerd Hoffmann 
1116f1ae32a1SGerd Hoffmann     /* Complete the previous frame */
1117f1ae32a1SGerd Hoffmann     if (s->pending_int_mask) {
1118f1ae32a1SGerd Hoffmann         s->status2 |= s->pending_int_mask;
1119f1ae32a1SGerd Hoffmann         s->status  |= UHCI_STS_USBINT;
1120f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
1121f1ae32a1SGerd Hoffmann     }
1122f1ae32a1SGerd Hoffmann     s->pending_int_mask = 0;
1123f1ae32a1SGerd Hoffmann 
1124f1ae32a1SGerd Hoffmann     /* Start new frame */
1125f1ae32a1SGerd Hoffmann     s->frnum = (s->frnum + 1) & 0x7ff;
1126f1ae32a1SGerd Hoffmann 
112750dcc0f8SGerd Hoffmann     trace_usb_uhci_frame_start(s->frnum);
1128f1ae32a1SGerd Hoffmann 
1129f1ae32a1SGerd Hoffmann     uhci_async_validate_begin(s);
1130f1ae32a1SGerd Hoffmann 
1131f1ae32a1SGerd Hoffmann     uhci_process_frame(s);
1132f1ae32a1SGerd Hoffmann 
1133f1ae32a1SGerd Hoffmann     uhci_async_validate_end(s);
1134f1ae32a1SGerd Hoffmann 
1135f1ae32a1SGerd Hoffmann     qemu_mod_timer(s->frame_timer, s->expire_time);
1136f1ae32a1SGerd Hoffmann }
1137f1ae32a1SGerd Hoffmann 
1138f1ae32a1SGerd Hoffmann static const MemoryRegionPortio uhci_portio[] = {
1139f1ae32a1SGerd Hoffmann     { 0, 32, 2, .write = uhci_ioport_writew, },
1140f1ae32a1SGerd Hoffmann     { 0, 32, 2, .read = uhci_ioport_readw, },
1141f1ae32a1SGerd Hoffmann     { 0, 32, 4, .write = uhci_ioport_writel, },
1142f1ae32a1SGerd Hoffmann     { 0, 32, 4, .read = uhci_ioport_readl, },
1143f1ae32a1SGerd Hoffmann     { 0, 32, 1, .write = uhci_ioport_writeb, },
1144f1ae32a1SGerd Hoffmann     { 0, 32, 1, .read = uhci_ioport_readb, },
1145f1ae32a1SGerd Hoffmann     PORTIO_END_OF_LIST()
1146f1ae32a1SGerd Hoffmann };
1147f1ae32a1SGerd Hoffmann 
1148f1ae32a1SGerd Hoffmann static const MemoryRegionOps uhci_ioport_ops = {
1149f1ae32a1SGerd Hoffmann     .old_portio = uhci_portio,
1150f1ae32a1SGerd Hoffmann };
1151f1ae32a1SGerd Hoffmann 
1152f1ae32a1SGerd Hoffmann static USBPortOps uhci_port_ops = {
1153f1ae32a1SGerd Hoffmann     .attach = uhci_attach,
1154f1ae32a1SGerd Hoffmann     .detach = uhci_detach,
1155f1ae32a1SGerd Hoffmann     .child_detach = uhci_child_detach,
1156f1ae32a1SGerd Hoffmann     .wakeup = uhci_wakeup,
1157f1ae32a1SGerd Hoffmann     .complete = uhci_async_complete,
1158f1ae32a1SGerd Hoffmann };
1159f1ae32a1SGerd Hoffmann 
1160f1ae32a1SGerd Hoffmann static USBBusOps uhci_bus_ops = {
1161f1ae32a1SGerd Hoffmann };
1162f1ae32a1SGerd Hoffmann 
1163f1ae32a1SGerd Hoffmann static int usb_uhci_common_initfn(PCIDevice *dev)
1164f1ae32a1SGerd Hoffmann {
1165973002c1SGerd Hoffmann     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1166f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1167f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1168f1ae32a1SGerd Hoffmann     int i;
1169f1ae32a1SGerd Hoffmann 
1170f1ae32a1SGerd Hoffmann     pci_conf[PCI_CLASS_PROG] = 0x00;
1171f1ae32a1SGerd Hoffmann     /* TODO: reset value should be 0. */
1172f1ae32a1SGerd Hoffmann     pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1173f1ae32a1SGerd Hoffmann 
1174973002c1SGerd Hoffmann     switch (pc->device_id) {
1175973002c1SGerd Hoffmann     case PCI_DEVICE_ID_INTEL_82801I_UHCI1:
1176973002c1SGerd Hoffmann         s->irq_pin = 0;  /* A */
1177973002c1SGerd Hoffmann         break;
1178973002c1SGerd Hoffmann     case PCI_DEVICE_ID_INTEL_82801I_UHCI2:
1179973002c1SGerd Hoffmann         s->irq_pin = 1;  /* B */
1180973002c1SGerd Hoffmann         break;
1181973002c1SGerd Hoffmann     case PCI_DEVICE_ID_INTEL_82801I_UHCI3:
1182973002c1SGerd Hoffmann         s->irq_pin = 2;  /* C */
1183973002c1SGerd Hoffmann         break;
1184973002c1SGerd Hoffmann     default:
1185973002c1SGerd Hoffmann         s->irq_pin = 3;  /* D */
1186973002c1SGerd Hoffmann         break;
1187973002c1SGerd Hoffmann     }
1188973002c1SGerd Hoffmann     pci_config_set_interrupt_pin(pci_conf, s->irq_pin + 1);
1189973002c1SGerd Hoffmann 
1190f1ae32a1SGerd Hoffmann     if (s->masterbus) {
1191f1ae32a1SGerd Hoffmann         USBPort *ports[NB_PORTS];
1192f1ae32a1SGerd Hoffmann         for(i = 0; i < NB_PORTS; i++) {
1193f1ae32a1SGerd Hoffmann             ports[i] = &s->ports[i].port;
1194f1ae32a1SGerd Hoffmann         }
1195f1ae32a1SGerd Hoffmann         if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1196f1ae32a1SGerd Hoffmann                 s->firstport, s, &uhci_port_ops,
1197f1ae32a1SGerd Hoffmann                 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1198f1ae32a1SGerd Hoffmann             return -1;
1199f1ae32a1SGerd Hoffmann         }
1200f1ae32a1SGerd Hoffmann     } else {
1201f1ae32a1SGerd Hoffmann         usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1202f1ae32a1SGerd Hoffmann         for (i = 0; i < NB_PORTS; i++) {
1203f1ae32a1SGerd Hoffmann             usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1204f1ae32a1SGerd Hoffmann                               USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1205f1ae32a1SGerd Hoffmann         }
1206f1ae32a1SGerd Hoffmann     }
12079a16c595SGerd Hoffmann     s->bh = qemu_bh_new(uhci_bh, s);
1208f1ae32a1SGerd Hoffmann     s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1209f1ae32a1SGerd Hoffmann     s->num_ports_vmstate = NB_PORTS;
1210f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&s->queues);
1211f1ae32a1SGerd Hoffmann 
1212f1ae32a1SGerd Hoffmann     qemu_register_reset(uhci_reset, s);
1213f1ae32a1SGerd Hoffmann 
1214f1ae32a1SGerd Hoffmann     memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1215f1ae32a1SGerd Hoffmann     /* Use region 4 for consistency with real hardware.  BSD guests seem
1216f1ae32a1SGerd Hoffmann        to rely on this.  */
1217f1ae32a1SGerd Hoffmann     pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1218f1ae32a1SGerd Hoffmann 
1219f1ae32a1SGerd Hoffmann     return 0;
1220f1ae32a1SGerd Hoffmann }
1221f1ae32a1SGerd Hoffmann 
1222f1ae32a1SGerd Hoffmann static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1223f1ae32a1SGerd Hoffmann {
1224f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1225f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1226f1ae32a1SGerd Hoffmann 
1227f1ae32a1SGerd Hoffmann     /* USB misc control 1/2 */
1228f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x40,0x00001000);
1229f1ae32a1SGerd Hoffmann     /* PM capability */
1230f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x80,0x00020001);
1231f1ae32a1SGerd Hoffmann     /* USB legacy support  */
1232f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0xc0,0x00002000);
1233f1ae32a1SGerd Hoffmann 
1234f1ae32a1SGerd Hoffmann     return usb_uhci_common_initfn(dev);
1235f1ae32a1SGerd Hoffmann }
1236f1ae32a1SGerd Hoffmann 
1237f90c2bcdSAlex Williamson static void usb_uhci_exit(PCIDevice *dev)
1238f1ae32a1SGerd Hoffmann {
1239f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1240f1ae32a1SGerd Hoffmann 
1241f1ae32a1SGerd Hoffmann     memory_region_destroy(&s->io_bar);
1242f1ae32a1SGerd Hoffmann }
1243f1ae32a1SGerd Hoffmann 
1244f1ae32a1SGerd Hoffmann static Property uhci_properties[] = {
1245f1ae32a1SGerd Hoffmann     DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1246f1ae32a1SGerd Hoffmann     DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
124740141d12SGerd Hoffmann     DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
1248f1ae32a1SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
1249f1ae32a1SGerd Hoffmann };
1250f1ae32a1SGerd Hoffmann 
1251f1ae32a1SGerd Hoffmann static void piix3_uhci_class_init(ObjectClass *klass, void *data)
1252f1ae32a1SGerd Hoffmann {
1253f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1254f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1255f1ae32a1SGerd Hoffmann 
1256f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1257f1ae32a1SGerd Hoffmann     k->exit = usb_uhci_exit;
1258f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1259f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82371SB_2;
1260f1ae32a1SGerd Hoffmann     k->revision = 0x01;
1261f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1262f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1263f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1264f1ae32a1SGerd Hoffmann }
1265f1ae32a1SGerd Hoffmann 
1266f1ae32a1SGerd Hoffmann static TypeInfo piix3_uhci_info = {
1267f1ae32a1SGerd Hoffmann     .name          = "piix3-usb-uhci",
1268f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1269f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1270f1ae32a1SGerd Hoffmann     .class_init    = piix3_uhci_class_init,
1271f1ae32a1SGerd Hoffmann };
1272f1ae32a1SGerd Hoffmann 
1273f1ae32a1SGerd Hoffmann static void piix4_uhci_class_init(ObjectClass *klass, void *data)
1274f1ae32a1SGerd Hoffmann {
1275f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1276f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1277f1ae32a1SGerd Hoffmann 
1278f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1279f1ae32a1SGerd Hoffmann     k->exit = usb_uhci_exit;
1280f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1281f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82371AB_2;
1282f1ae32a1SGerd Hoffmann     k->revision = 0x01;
1283f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1284f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1285f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1286f1ae32a1SGerd Hoffmann }
1287f1ae32a1SGerd Hoffmann 
1288f1ae32a1SGerd Hoffmann static TypeInfo piix4_uhci_info = {
1289f1ae32a1SGerd Hoffmann     .name          = "piix4-usb-uhci",
1290f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1291f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1292f1ae32a1SGerd Hoffmann     .class_init    = piix4_uhci_class_init,
1293f1ae32a1SGerd Hoffmann };
1294f1ae32a1SGerd Hoffmann 
1295f1ae32a1SGerd Hoffmann static void vt82c686b_uhci_class_init(ObjectClass *klass, void *data)
1296f1ae32a1SGerd Hoffmann {
1297f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1298f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1299f1ae32a1SGerd Hoffmann 
1300f1ae32a1SGerd Hoffmann     k->init = usb_uhci_vt82c686b_initfn;
1301f1ae32a1SGerd Hoffmann     k->exit = usb_uhci_exit;
1302f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_VIA;
1303f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_VIA_UHCI;
1304f1ae32a1SGerd Hoffmann     k->revision = 0x01;
1305f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1306f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1307f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1308f1ae32a1SGerd Hoffmann }
1309f1ae32a1SGerd Hoffmann 
1310f1ae32a1SGerd Hoffmann static TypeInfo vt82c686b_uhci_info = {
1311f1ae32a1SGerd Hoffmann     .name          = "vt82c686b-usb-uhci",
1312f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1313f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1314f1ae32a1SGerd Hoffmann     .class_init    = vt82c686b_uhci_class_init,
1315f1ae32a1SGerd Hoffmann };
1316f1ae32a1SGerd Hoffmann 
1317f1ae32a1SGerd Hoffmann static void ich9_uhci1_class_init(ObjectClass *klass, void *data)
1318f1ae32a1SGerd Hoffmann {
1319f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1320f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1321f1ae32a1SGerd Hoffmann 
1322f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1323f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1324f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1;
1325f1ae32a1SGerd Hoffmann     k->revision = 0x03;
1326f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1327f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1328f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1329f1ae32a1SGerd Hoffmann }
1330f1ae32a1SGerd Hoffmann 
1331f1ae32a1SGerd Hoffmann static TypeInfo ich9_uhci1_info = {
1332f1ae32a1SGerd Hoffmann     .name          = "ich9-usb-uhci1",
1333f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1334f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1335f1ae32a1SGerd Hoffmann     .class_init    = ich9_uhci1_class_init,
1336f1ae32a1SGerd Hoffmann };
1337f1ae32a1SGerd Hoffmann 
1338f1ae32a1SGerd Hoffmann static void ich9_uhci2_class_init(ObjectClass *klass, void *data)
1339f1ae32a1SGerd Hoffmann {
1340f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1341f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1342f1ae32a1SGerd Hoffmann 
1343f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1344f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1345f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2;
1346f1ae32a1SGerd Hoffmann     k->revision = 0x03;
1347f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1348f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1349f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1350f1ae32a1SGerd Hoffmann }
1351f1ae32a1SGerd Hoffmann 
1352f1ae32a1SGerd Hoffmann static TypeInfo ich9_uhci2_info = {
1353f1ae32a1SGerd Hoffmann     .name          = "ich9-usb-uhci2",
1354f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1355f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1356f1ae32a1SGerd Hoffmann     .class_init    = ich9_uhci2_class_init,
1357f1ae32a1SGerd Hoffmann };
1358f1ae32a1SGerd Hoffmann 
1359f1ae32a1SGerd Hoffmann static void ich9_uhci3_class_init(ObjectClass *klass, void *data)
1360f1ae32a1SGerd Hoffmann {
1361f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1362f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1363f1ae32a1SGerd Hoffmann 
1364f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1365f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1366f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3;
1367f1ae32a1SGerd Hoffmann     k->revision = 0x03;
1368f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1369f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1370f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1371f1ae32a1SGerd Hoffmann }
1372f1ae32a1SGerd Hoffmann 
1373f1ae32a1SGerd Hoffmann static TypeInfo ich9_uhci3_info = {
1374f1ae32a1SGerd Hoffmann     .name          = "ich9-usb-uhci3",
1375f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1376f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1377f1ae32a1SGerd Hoffmann     .class_init    = ich9_uhci3_class_init,
1378f1ae32a1SGerd Hoffmann };
1379f1ae32a1SGerd Hoffmann 
1380f1ae32a1SGerd Hoffmann static void uhci_register_types(void)
1381f1ae32a1SGerd Hoffmann {
1382f1ae32a1SGerd Hoffmann     type_register_static(&piix3_uhci_info);
1383f1ae32a1SGerd Hoffmann     type_register_static(&piix4_uhci_info);
1384f1ae32a1SGerd Hoffmann     type_register_static(&vt82c686b_uhci_info);
1385f1ae32a1SGerd Hoffmann     type_register_static(&ich9_uhci1_info);
1386f1ae32a1SGerd Hoffmann     type_register_static(&ich9_uhci2_info);
1387f1ae32a1SGerd Hoffmann     type_register_static(&ich9_uhci3_info);
1388f1ae32a1SGerd Hoffmann }
1389f1ae32a1SGerd Hoffmann 
1390f1ae32a1SGerd Hoffmann type_init(uhci_register_types)
1391