xref: /openbmc/qemu/hw/usb/hcd-uhci.c (revision 60f8afcb)
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"
34f1ae32a1SGerd Hoffmann 
35f1ae32a1SGerd Hoffmann //#define DEBUG
36f1ae32a1SGerd Hoffmann //#define DEBUG_DUMP_DATA
37f1ae32a1SGerd Hoffmann 
38f1ae32a1SGerd Hoffmann #define UHCI_CMD_FGR      (1 << 4)
39f1ae32a1SGerd Hoffmann #define UHCI_CMD_EGSM     (1 << 3)
40f1ae32a1SGerd Hoffmann #define UHCI_CMD_GRESET   (1 << 2)
41f1ae32a1SGerd Hoffmann #define UHCI_CMD_HCRESET  (1 << 1)
42f1ae32a1SGerd Hoffmann #define UHCI_CMD_RS       (1 << 0)
43f1ae32a1SGerd Hoffmann 
44f1ae32a1SGerd Hoffmann #define UHCI_STS_HCHALTED (1 << 5)
45f1ae32a1SGerd Hoffmann #define UHCI_STS_HCPERR   (1 << 4)
46f1ae32a1SGerd Hoffmann #define UHCI_STS_HSERR    (1 << 3)
47f1ae32a1SGerd Hoffmann #define UHCI_STS_RD       (1 << 2)
48f1ae32a1SGerd Hoffmann #define UHCI_STS_USBERR   (1 << 1)
49f1ae32a1SGerd Hoffmann #define UHCI_STS_USBINT   (1 << 0)
50f1ae32a1SGerd Hoffmann 
51f1ae32a1SGerd Hoffmann #define TD_CTRL_SPD     (1 << 29)
52f1ae32a1SGerd Hoffmann #define TD_CTRL_ERROR_SHIFT  27
53f1ae32a1SGerd Hoffmann #define TD_CTRL_IOS     (1 << 25)
54f1ae32a1SGerd Hoffmann #define TD_CTRL_IOC     (1 << 24)
55f1ae32a1SGerd Hoffmann #define TD_CTRL_ACTIVE  (1 << 23)
56f1ae32a1SGerd Hoffmann #define TD_CTRL_STALL   (1 << 22)
57f1ae32a1SGerd Hoffmann #define TD_CTRL_BABBLE  (1 << 20)
58f1ae32a1SGerd Hoffmann #define TD_CTRL_NAK     (1 << 19)
59f1ae32a1SGerd Hoffmann #define TD_CTRL_TIMEOUT (1 << 18)
60f1ae32a1SGerd Hoffmann 
61f1ae32a1SGerd Hoffmann #define UHCI_PORT_SUSPEND (1 << 12)
62f1ae32a1SGerd Hoffmann #define UHCI_PORT_RESET (1 << 9)
63f1ae32a1SGerd Hoffmann #define UHCI_PORT_LSDA  (1 << 8)
64f1ae32a1SGerd Hoffmann #define UHCI_PORT_RD    (1 << 6)
65f1ae32a1SGerd Hoffmann #define UHCI_PORT_ENC   (1 << 3)
66f1ae32a1SGerd Hoffmann #define UHCI_PORT_EN    (1 << 2)
67f1ae32a1SGerd Hoffmann #define UHCI_PORT_CSC   (1 << 1)
68f1ae32a1SGerd Hoffmann #define UHCI_PORT_CCS   (1 << 0)
69f1ae32a1SGerd Hoffmann 
70f1ae32a1SGerd Hoffmann #define UHCI_PORT_READ_ONLY    (0x1bb)
71f1ae32a1SGerd Hoffmann #define UHCI_PORT_WRITE_CLEAR  (UHCI_PORT_CSC | UHCI_PORT_ENC)
72f1ae32a1SGerd Hoffmann 
73f1ae32a1SGerd Hoffmann #define FRAME_TIMER_FREQ 1000
74f1ae32a1SGerd Hoffmann 
75f1ae32a1SGerd Hoffmann #define FRAME_MAX_LOOPS  256
76f1ae32a1SGerd Hoffmann 
77f1ae32a1SGerd Hoffmann #define NB_PORTS 2
78f1ae32a1SGerd Hoffmann 
79f1ae32a1SGerd Hoffmann #ifdef DEBUG
80f1ae32a1SGerd Hoffmann #define DPRINTF printf
81f1ae32a1SGerd Hoffmann 
82f1ae32a1SGerd Hoffmann static const char *pid2str(int pid)
83f1ae32a1SGerd Hoffmann {
84f1ae32a1SGerd Hoffmann     switch (pid) {
85f1ae32a1SGerd Hoffmann     case USB_TOKEN_SETUP: return "SETUP";
86f1ae32a1SGerd Hoffmann     case USB_TOKEN_IN:    return "IN";
87f1ae32a1SGerd Hoffmann     case USB_TOKEN_OUT:   return "OUT";
88f1ae32a1SGerd Hoffmann     }
89f1ae32a1SGerd Hoffmann     return "?";
90f1ae32a1SGerd Hoffmann }
91f1ae32a1SGerd Hoffmann 
92f1ae32a1SGerd Hoffmann #else
93f1ae32a1SGerd Hoffmann #define DPRINTF(...)
94f1ae32a1SGerd Hoffmann #endif
95f1ae32a1SGerd Hoffmann 
96f1ae32a1SGerd Hoffmann typedef struct UHCIState UHCIState;
97f1ae32a1SGerd Hoffmann typedef struct UHCIAsync UHCIAsync;
98f1ae32a1SGerd Hoffmann typedef struct UHCIQueue UHCIQueue;
99f1ae32a1SGerd Hoffmann 
100f1ae32a1SGerd Hoffmann /*
101f1ae32a1SGerd Hoffmann  * Pending async transaction.
102f1ae32a1SGerd Hoffmann  * 'packet' must be the first field because completion
103f1ae32a1SGerd Hoffmann  * handler does "(UHCIAsync *) pkt" cast.
104f1ae32a1SGerd Hoffmann  */
105f1ae32a1SGerd Hoffmann 
106f1ae32a1SGerd Hoffmann struct UHCIAsync {
107f1ae32a1SGerd Hoffmann     USBPacket packet;
108f1ae32a1SGerd Hoffmann     QEMUSGList sgl;
109f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
110f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIAsync) next;
111f1ae32a1SGerd Hoffmann     uint32_t  td;
112f1ae32a1SGerd Hoffmann     uint8_t   isoc;
113f1ae32a1SGerd Hoffmann     uint8_t   done;
114f1ae32a1SGerd Hoffmann };
115f1ae32a1SGerd Hoffmann 
116f1ae32a1SGerd Hoffmann struct UHCIQueue {
117f1ae32a1SGerd Hoffmann     uint32_t  token;
118f1ae32a1SGerd Hoffmann     UHCIState *uhci;
119f1ae32a1SGerd Hoffmann     QTAILQ_ENTRY(UHCIQueue) next;
120f1ae32a1SGerd Hoffmann     QTAILQ_HEAD(, UHCIAsync) asyncs;
121f1ae32a1SGerd Hoffmann     int8_t    valid;
122f1ae32a1SGerd Hoffmann };
123f1ae32a1SGerd Hoffmann 
124f1ae32a1SGerd Hoffmann typedef struct UHCIPort {
125f1ae32a1SGerd Hoffmann     USBPort port;
126f1ae32a1SGerd Hoffmann     uint16_t ctrl;
127f1ae32a1SGerd Hoffmann } UHCIPort;
128f1ae32a1SGerd Hoffmann 
129f1ae32a1SGerd Hoffmann struct UHCIState {
130f1ae32a1SGerd Hoffmann     PCIDevice dev;
131f1ae32a1SGerd Hoffmann     MemoryRegion io_bar;
132f1ae32a1SGerd Hoffmann     USBBus bus; /* Note unused when we're a companion controller */
133f1ae32a1SGerd Hoffmann     uint16_t cmd; /* cmd register */
134f1ae32a1SGerd Hoffmann     uint16_t status;
135f1ae32a1SGerd Hoffmann     uint16_t intr; /* interrupt enable register */
136f1ae32a1SGerd Hoffmann     uint16_t frnum; /* frame number */
137f1ae32a1SGerd Hoffmann     uint32_t fl_base_addr; /* frame list base address */
138f1ae32a1SGerd Hoffmann     uint8_t sof_timing;
139f1ae32a1SGerd Hoffmann     uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
140f1ae32a1SGerd Hoffmann     int64_t expire_time;
141f1ae32a1SGerd Hoffmann     QEMUTimer *frame_timer;
142f1ae32a1SGerd Hoffmann     UHCIPort ports[NB_PORTS];
143f1ae32a1SGerd Hoffmann 
144f1ae32a1SGerd Hoffmann     /* Interrupts that should be raised at the end of the current frame.  */
145f1ae32a1SGerd Hoffmann     uint32_t pending_int_mask;
146f1ae32a1SGerd Hoffmann 
147f1ae32a1SGerd Hoffmann     /* Active packets */
148f1ae32a1SGerd Hoffmann     QTAILQ_HEAD(, UHCIQueue) queues;
149f1ae32a1SGerd Hoffmann     uint8_t num_ports_vmstate;
150f1ae32a1SGerd Hoffmann 
151f1ae32a1SGerd Hoffmann     /* Properties */
152f1ae32a1SGerd Hoffmann     char *masterbus;
153f1ae32a1SGerd Hoffmann     uint32_t firstport;
154f1ae32a1SGerd Hoffmann };
155f1ae32a1SGerd Hoffmann 
156f1ae32a1SGerd Hoffmann typedef struct UHCI_TD {
157f1ae32a1SGerd Hoffmann     uint32_t link;
158f1ae32a1SGerd Hoffmann     uint32_t ctrl; /* see TD_CTRL_xxx */
159f1ae32a1SGerd Hoffmann     uint32_t token;
160f1ae32a1SGerd Hoffmann     uint32_t buffer;
161f1ae32a1SGerd Hoffmann } UHCI_TD;
162f1ae32a1SGerd Hoffmann 
163f1ae32a1SGerd Hoffmann typedef struct UHCI_QH {
164f1ae32a1SGerd Hoffmann     uint32_t link;
165f1ae32a1SGerd Hoffmann     uint32_t el_link;
166f1ae32a1SGerd Hoffmann } UHCI_QH;
167f1ae32a1SGerd Hoffmann 
168f1ae32a1SGerd Hoffmann static inline int32_t uhci_queue_token(UHCI_TD *td)
169f1ae32a1SGerd Hoffmann {
170f1ae32a1SGerd Hoffmann     /* covers ep, dev, pid -> identifies the endpoint */
171f1ae32a1SGerd Hoffmann     return td->token & 0x7ffff;
172f1ae32a1SGerd Hoffmann }
173f1ae32a1SGerd Hoffmann 
174f1ae32a1SGerd Hoffmann static UHCIQueue *uhci_queue_get(UHCIState *s, UHCI_TD *td)
175f1ae32a1SGerd Hoffmann {
176f1ae32a1SGerd Hoffmann     uint32_t token = uhci_queue_token(td);
177f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
178f1ae32a1SGerd Hoffmann 
179f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
180f1ae32a1SGerd Hoffmann         if (queue->token == token) {
181f1ae32a1SGerd Hoffmann             return queue;
182f1ae32a1SGerd Hoffmann         }
183f1ae32a1SGerd Hoffmann     }
184f1ae32a1SGerd Hoffmann 
185f1ae32a1SGerd Hoffmann     queue = g_new0(UHCIQueue, 1);
186f1ae32a1SGerd Hoffmann     queue->uhci = s;
187f1ae32a1SGerd Hoffmann     queue->token = token;
188f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&queue->asyncs);
189f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_HEAD(&s->queues, queue, next);
190f1ae32a1SGerd Hoffmann     return queue;
191f1ae32a1SGerd Hoffmann }
192f1ae32a1SGerd Hoffmann 
193f1ae32a1SGerd Hoffmann static void uhci_queue_free(UHCIQueue *queue)
194f1ae32a1SGerd Hoffmann {
195f1ae32a1SGerd Hoffmann     UHCIState *s = queue->uhci;
196f1ae32a1SGerd Hoffmann 
197f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&s->queues, queue, next);
198f1ae32a1SGerd Hoffmann     g_free(queue);
199f1ae32a1SGerd Hoffmann }
200f1ae32a1SGerd Hoffmann 
20116ce543eSGerd Hoffmann static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t addr)
202f1ae32a1SGerd Hoffmann {
203f1ae32a1SGerd Hoffmann     UHCIAsync *async = g_new0(UHCIAsync, 1);
204f1ae32a1SGerd Hoffmann 
205f1ae32a1SGerd Hoffmann     async->queue = queue;
20616ce543eSGerd Hoffmann     async->td = addr;
207f1ae32a1SGerd Hoffmann     usb_packet_init(&async->packet);
208f1ae32a1SGerd Hoffmann     pci_dma_sglist_init(&async->sgl, &queue->uhci->dev, 1);
209f1ae32a1SGerd Hoffmann 
210f1ae32a1SGerd Hoffmann     return async;
211f1ae32a1SGerd Hoffmann }
212f1ae32a1SGerd Hoffmann 
213f1ae32a1SGerd Hoffmann static void uhci_async_free(UHCIAsync *async)
214f1ae32a1SGerd Hoffmann {
215f1ae32a1SGerd Hoffmann     usb_packet_cleanup(&async->packet);
216f1ae32a1SGerd Hoffmann     qemu_sglist_destroy(&async->sgl);
217f1ae32a1SGerd Hoffmann     g_free(async);
218f1ae32a1SGerd Hoffmann }
219f1ae32a1SGerd Hoffmann 
220f1ae32a1SGerd Hoffmann static void uhci_async_link(UHCIAsync *async)
221f1ae32a1SGerd Hoffmann {
222f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
223f1ae32a1SGerd Hoffmann     QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
224f1ae32a1SGerd Hoffmann }
225f1ae32a1SGerd Hoffmann 
226f1ae32a1SGerd Hoffmann static void uhci_async_unlink(UHCIAsync *async)
227f1ae32a1SGerd Hoffmann {
228f1ae32a1SGerd Hoffmann     UHCIQueue *queue = async->queue;
229f1ae32a1SGerd Hoffmann     QTAILQ_REMOVE(&queue->asyncs, async, next);
230f1ae32a1SGerd Hoffmann }
231f1ae32a1SGerd Hoffmann 
232f1ae32a1SGerd Hoffmann static void uhci_async_cancel(UHCIAsync *async)
233f1ae32a1SGerd Hoffmann {
234f1ae32a1SGerd Hoffmann     DPRINTF("uhci: cancel td 0x%x token 0x%x done %u\n",
235f1ae32a1SGerd Hoffmann            async->td, async->token, async->done);
236f1ae32a1SGerd Hoffmann 
237f1ae32a1SGerd Hoffmann     if (!async->done)
238f1ae32a1SGerd Hoffmann         usb_cancel_packet(&async->packet);
239f1ae32a1SGerd Hoffmann     uhci_async_free(async);
240f1ae32a1SGerd Hoffmann }
241f1ae32a1SGerd Hoffmann 
242f1ae32a1SGerd Hoffmann /*
243f1ae32a1SGerd Hoffmann  * Mark all outstanding async packets as invalid.
244f1ae32a1SGerd Hoffmann  * This is used for canceling them when TDs are removed by the HCD.
245f1ae32a1SGerd Hoffmann  */
246f1ae32a1SGerd Hoffmann static void uhci_async_validate_begin(UHCIState *s)
247f1ae32a1SGerd Hoffmann {
248f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
249f1ae32a1SGerd Hoffmann 
250f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
251f1ae32a1SGerd Hoffmann         queue->valid--;
252f1ae32a1SGerd Hoffmann     }
253f1ae32a1SGerd Hoffmann }
254f1ae32a1SGerd Hoffmann 
255f1ae32a1SGerd Hoffmann /*
256f1ae32a1SGerd Hoffmann  * Cancel async packets that are no longer valid
257f1ae32a1SGerd Hoffmann  */
258f1ae32a1SGerd Hoffmann static void uhci_async_validate_end(UHCIState *s)
259f1ae32a1SGerd Hoffmann {
260f1ae32a1SGerd Hoffmann     UHCIQueue *queue, *n;
261f1ae32a1SGerd Hoffmann     UHCIAsync *async;
262f1ae32a1SGerd Hoffmann 
263f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
264f1ae32a1SGerd Hoffmann         if (queue->valid > 0) {
265f1ae32a1SGerd Hoffmann             continue;
266f1ae32a1SGerd Hoffmann         }
267f1ae32a1SGerd Hoffmann         while (!QTAILQ_EMPTY(&queue->asyncs)) {
268f1ae32a1SGerd Hoffmann             async = QTAILQ_FIRST(&queue->asyncs);
269f1ae32a1SGerd Hoffmann             uhci_async_unlink(async);
270f1ae32a1SGerd Hoffmann             uhci_async_cancel(async);
271f1ae32a1SGerd Hoffmann         }
272f1ae32a1SGerd Hoffmann         uhci_queue_free(queue);
273f1ae32a1SGerd Hoffmann     }
274f1ae32a1SGerd Hoffmann }
275f1ae32a1SGerd Hoffmann 
276f1ae32a1SGerd Hoffmann static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
277f1ae32a1SGerd Hoffmann {
278f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
279f1ae32a1SGerd Hoffmann     UHCIAsync *curr, *n;
280f1ae32a1SGerd Hoffmann 
281f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
282f1ae32a1SGerd Hoffmann         QTAILQ_FOREACH_SAFE(curr, &queue->asyncs, next, n) {
283f1ae32a1SGerd Hoffmann             if (!usb_packet_is_inflight(&curr->packet) ||
284f1ae32a1SGerd Hoffmann                 curr->packet.ep->dev != dev) {
285f1ae32a1SGerd Hoffmann                 continue;
286f1ae32a1SGerd Hoffmann             }
287f1ae32a1SGerd Hoffmann             uhci_async_unlink(curr);
288f1ae32a1SGerd Hoffmann             uhci_async_cancel(curr);
289f1ae32a1SGerd Hoffmann         }
290f1ae32a1SGerd Hoffmann     }
291f1ae32a1SGerd Hoffmann }
292f1ae32a1SGerd Hoffmann 
293f1ae32a1SGerd Hoffmann static void uhci_async_cancel_all(UHCIState *s)
294f1ae32a1SGerd Hoffmann {
295f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
296f1ae32a1SGerd Hoffmann     UHCIAsync *curr, *n;
297f1ae32a1SGerd Hoffmann 
298f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
299f1ae32a1SGerd Hoffmann         QTAILQ_FOREACH_SAFE(curr, &queue->asyncs, next, n) {
300f1ae32a1SGerd Hoffmann             uhci_async_unlink(curr);
301f1ae32a1SGerd Hoffmann             uhci_async_cancel(curr);
302f1ae32a1SGerd Hoffmann         }
303*60f8afcbSGerd Hoffmann         uhci_queue_free(queue);
304f1ae32a1SGerd Hoffmann     }
305f1ae32a1SGerd Hoffmann }
306f1ae32a1SGerd Hoffmann 
307f1ae32a1SGerd Hoffmann static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t addr, UHCI_TD *td)
308f1ae32a1SGerd Hoffmann {
309f1ae32a1SGerd Hoffmann     uint32_t token = uhci_queue_token(td);
310f1ae32a1SGerd Hoffmann     UHCIQueue *queue;
311f1ae32a1SGerd Hoffmann     UHCIAsync *async;
312f1ae32a1SGerd Hoffmann 
313f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(queue, &s->queues, next) {
314f1ae32a1SGerd Hoffmann         if (queue->token == token) {
315f1ae32a1SGerd Hoffmann             break;
316f1ae32a1SGerd Hoffmann         }
317f1ae32a1SGerd Hoffmann     }
318f1ae32a1SGerd Hoffmann     if (queue == NULL) {
319f1ae32a1SGerd Hoffmann         return NULL;
320f1ae32a1SGerd Hoffmann     }
321f1ae32a1SGerd Hoffmann 
322f1ae32a1SGerd Hoffmann     QTAILQ_FOREACH(async, &queue->asyncs, next) {
323f1ae32a1SGerd Hoffmann         if (async->td == addr) {
324f1ae32a1SGerd Hoffmann             return async;
325f1ae32a1SGerd Hoffmann         }
326f1ae32a1SGerd Hoffmann     }
327f1ae32a1SGerd Hoffmann 
328f1ae32a1SGerd Hoffmann     return NULL;
329f1ae32a1SGerd Hoffmann }
330f1ae32a1SGerd Hoffmann 
331f1ae32a1SGerd Hoffmann static void uhci_update_irq(UHCIState *s)
332f1ae32a1SGerd Hoffmann {
333f1ae32a1SGerd Hoffmann     int level;
334f1ae32a1SGerd Hoffmann     if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
335f1ae32a1SGerd Hoffmann         ((s->status2 & 2) && (s->intr & (1 << 3))) ||
336f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
337f1ae32a1SGerd Hoffmann         ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
338f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HSERR) ||
339f1ae32a1SGerd Hoffmann         (s->status & UHCI_STS_HCPERR)) {
340f1ae32a1SGerd Hoffmann         level = 1;
341f1ae32a1SGerd Hoffmann     } else {
342f1ae32a1SGerd Hoffmann         level = 0;
343f1ae32a1SGerd Hoffmann     }
344f1ae32a1SGerd Hoffmann     qemu_set_irq(s->dev.irq[3], level);
345f1ae32a1SGerd Hoffmann }
346f1ae32a1SGerd Hoffmann 
347f1ae32a1SGerd Hoffmann static void uhci_reset(void *opaque)
348f1ae32a1SGerd Hoffmann {
349f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
350f1ae32a1SGerd Hoffmann     uint8_t *pci_conf;
351f1ae32a1SGerd Hoffmann     int i;
352f1ae32a1SGerd Hoffmann     UHCIPort *port;
353f1ae32a1SGerd Hoffmann 
354f1ae32a1SGerd Hoffmann     DPRINTF("uhci: full reset\n");
355f1ae32a1SGerd Hoffmann 
356f1ae32a1SGerd Hoffmann     pci_conf = s->dev.config;
357f1ae32a1SGerd Hoffmann 
358f1ae32a1SGerd Hoffmann     pci_conf[0x6a] = 0x01; /* usb clock */
359f1ae32a1SGerd Hoffmann     pci_conf[0x6b] = 0x00;
360f1ae32a1SGerd Hoffmann     s->cmd = 0;
361f1ae32a1SGerd Hoffmann     s->status = 0;
362f1ae32a1SGerd Hoffmann     s->status2 = 0;
363f1ae32a1SGerd Hoffmann     s->intr = 0;
364f1ae32a1SGerd Hoffmann     s->fl_base_addr = 0;
365f1ae32a1SGerd Hoffmann     s->sof_timing = 64;
366f1ae32a1SGerd Hoffmann 
367f1ae32a1SGerd Hoffmann     for(i = 0; i < NB_PORTS; i++) {
368f1ae32a1SGerd Hoffmann         port = &s->ports[i];
369f1ae32a1SGerd Hoffmann         port->ctrl = 0x0080;
370f1ae32a1SGerd Hoffmann         if (port->port.dev && port->port.dev->attached) {
371f1ae32a1SGerd Hoffmann             usb_port_reset(&port->port);
372f1ae32a1SGerd Hoffmann         }
373f1ae32a1SGerd Hoffmann     }
374f1ae32a1SGerd Hoffmann 
375f1ae32a1SGerd Hoffmann     uhci_async_cancel_all(s);
376f1ae32a1SGerd Hoffmann }
377f1ae32a1SGerd Hoffmann 
378f1ae32a1SGerd Hoffmann static void uhci_pre_save(void *opaque)
379f1ae32a1SGerd Hoffmann {
380f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
381f1ae32a1SGerd Hoffmann 
382f1ae32a1SGerd Hoffmann     uhci_async_cancel_all(s);
383f1ae32a1SGerd Hoffmann }
384f1ae32a1SGerd Hoffmann 
385f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci_port = {
386f1ae32a1SGerd Hoffmann     .name = "uhci port",
387f1ae32a1SGerd Hoffmann     .version_id = 1,
388f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
389f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
390f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
391f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(ctrl, UHCIPort),
392f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
393f1ae32a1SGerd Hoffmann     }
394f1ae32a1SGerd Hoffmann };
395f1ae32a1SGerd Hoffmann 
396f1ae32a1SGerd Hoffmann static const VMStateDescription vmstate_uhci = {
397f1ae32a1SGerd Hoffmann     .name = "uhci",
398f1ae32a1SGerd Hoffmann     .version_id = 2,
399f1ae32a1SGerd Hoffmann     .minimum_version_id = 1,
400f1ae32a1SGerd Hoffmann     .minimum_version_id_old = 1,
401f1ae32a1SGerd Hoffmann     .pre_save = uhci_pre_save,
402f1ae32a1SGerd Hoffmann     .fields      = (VMStateField []) {
403f1ae32a1SGerd Hoffmann         VMSTATE_PCI_DEVICE(dev, UHCIState),
404f1ae32a1SGerd Hoffmann         VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState),
405f1ae32a1SGerd Hoffmann         VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
406f1ae32a1SGerd Hoffmann                              vmstate_uhci_port, UHCIPort),
407f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(cmd, UHCIState),
408f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(status, UHCIState),
409f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(intr, UHCIState),
410f1ae32a1SGerd Hoffmann         VMSTATE_UINT16(frnum, UHCIState),
411f1ae32a1SGerd Hoffmann         VMSTATE_UINT32(fl_base_addr, UHCIState),
412f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(sof_timing, UHCIState),
413f1ae32a1SGerd Hoffmann         VMSTATE_UINT8(status2, UHCIState),
414f1ae32a1SGerd Hoffmann         VMSTATE_TIMER(frame_timer, UHCIState),
415f1ae32a1SGerd Hoffmann         VMSTATE_INT64_V(expire_time, UHCIState, 2),
416f1ae32a1SGerd Hoffmann         VMSTATE_END_OF_LIST()
417f1ae32a1SGerd Hoffmann     }
418f1ae32a1SGerd Hoffmann };
419f1ae32a1SGerd Hoffmann 
420f1ae32a1SGerd Hoffmann static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
421f1ae32a1SGerd Hoffmann {
422f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
423f1ae32a1SGerd Hoffmann 
424f1ae32a1SGerd Hoffmann     addr &= 0x1f;
425f1ae32a1SGerd Hoffmann     switch(addr) {
426f1ae32a1SGerd Hoffmann     case 0x0c:
427f1ae32a1SGerd Hoffmann         s->sof_timing = val;
428f1ae32a1SGerd Hoffmann         break;
429f1ae32a1SGerd Hoffmann     }
430f1ae32a1SGerd Hoffmann }
431f1ae32a1SGerd Hoffmann 
432f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr)
433f1ae32a1SGerd Hoffmann {
434f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
435f1ae32a1SGerd Hoffmann     uint32_t val;
436f1ae32a1SGerd Hoffmann 
437f1ae32a1SGerd Hoffmann     addr &= 0x1f;
438f1ae32a1SGerd Hoffmann     switch(addr) {
439f1ae32a1SGerd Hoffmann     case 0x0c:
440f1ae32a1SGerd Hoffmann         val = s->sof_timing;
441f1ae32a1SGerd Hoffmann         break;
442f1ae32a1SGerd Hoffmann     default:
443f1ae32a1SGerd Hoffmann         val = 0xff;
444f1ae32a1SGerd Hoffmann         break;
445f1ae32a1SGerd Hoffmann     }
446f1ae32a1SGerd Hoffmann     return val;
447f1ae32a1SGerd Hoffmann }
448f1ae32a1SGerd Hoffmann 
449f1ae32a1SGerd Hoffmann static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
450f1ae32a1SGerd Hoffmann {
451f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
452f1ae32a1SGerd Hoffmann 
453f1ae32a1SGerd Hoffmann     addr &= 0x1f;
454f1ae32a1SGerd Hoffmann     DPRINTF("uhci: writew port=0x%04x val=0x%04x\n", addr, val);
455f1ae32a1SGerd Hoffmann 
456f1ae32a1SGerd Hoffmann     switch(addr) {
457f1ae32a1SGerd Hoffmann     case 0x00:
458f1ae32a1SGerd Hoffmann         if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
459f1ae32a1SGerd Hoffmann             /* start frame processing */
460f1ae32a1SGerd Hoffmann             s->expire_time = qemu_get_clock_ns(vm_clock) +
461f1ae32a1SGerd Hoffmann                 (get_ticks_per_sec() / FRAME_TIMER_FREQ);
462f1ae32a1SGerd Hoffmann             qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
463f1ae32a1SGerd Hoffmann             s->status &= ~UHCI_STS_HCHALTED;
464f1ae32a1SGerd Hoffmann         } else if (!(val & UHCI_CMD_RS)) {
465f1ae32a1SGerd Hoffmann             s->status |= UHCI_STS_HCHALTED;
466f1ae32a1SGerd Hoffmann         }
467f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_GRESET) {
468f1ae32a1SGerd Hoffmann             UHCIPort *port;
469f1ae32a1SGerd Hoffmann             int i;
470f1ae32a1SGerd Hoffmann 
471f1ae32a1SGerd Hoffmann             /* send reset on the USB bus */
472f1ae32a1SGerd Hoffmann             for(i = 0; i < NB_PORTS; i++) {
473f1ae32a1SGerd Hoffmann                 port = &s->ports[i];
474f1ae32a1SGerd Hoffmann                 usb_device_reset(port->port.dev);
475f1ae32a1SGerd Hoffmann             }
476f1ae32a1SGerd Hoffmann             uhci_reset(s);
477f1ae32a1SGerd Hoffmann             return;
478f1ae32a1SGerd Hoffmann         }
479f1ae32a1SGerd Hoffmann         if (val & UHCI_CMD_HCRESET) {
480f1ae32a1SGerd Hoffmann             uhci_reset(s);
481f1ae32a1SGerd Hoffmann             return;
482f1ae32a1SGerd Hoffmann         }
483f1ae32a1SGerd Hoffmann         s->cmd = val;
484f1ae32a1SGerd Hoffmann         break;
485f1ae32a1SGerd Hoffmann     case 0x02:
486f1ae32a1SGerd Hoffmann         s->status &= ~val;
487f1ae32a1SGerd Hoffmann         /* XXX: the chip spec is not coherent, so we add a hidden
488f1ae32a1SGerd Hoffmann            register to distinguish between IOC and SPD */
489f1ae32a1SGerd Hoffmann         if (val & UHCI_STS_USBINT)
490f1ae32a1SGerd Hoffmann             s->status2 = 0;
491f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
492f1ae32a1SGerd Hoffmann         break;
493f1ae32a1SGerd Hoffmann     case 0x04:
494f1ae32a1SGerd Hoffmann         s->intr = val;
495f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
496f1ae32a1SGerd Hoffmann         break;
497f1ae32a1SGerd Hoffmann     case 0x06:
498f1ae32a1SGerd Hoffmann         if (s->status & UHCI_STS_HCHALTED)
499f1ae32a1SGerd Hoffmann             s->frnum = val & 0x7ff;
500f1ae32a1SGerd Hoffmann         break;
501f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
502f1ae32a1SGerd Hoffmann         {
503f1ae32a1SGerd Hoffmann             UHCIPort *port;
504f1ae32a1SGerd Hoffmann             USBDevice *dev;
505f1ae32a1SGerd Hoffmann             int n;
506f1ae32a1SGerd Hoffmann 
507f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
508f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
509f1ae32a1SGerd Hoffmann                 return;
510f1ae32a1SGerd Hoffmann             port = &s->ports[n];
511f1ae32a1SGerd Hoffmann             dev = port->port.dev;
512f1ae32a1SGerd Hoffmann             if (dev && dev->attached) {
513f1ae32a1SGerd Hoffmann                 /* port reset */
514f1ae32a1SGerd Hoffmann                 if ( (val & UHCI_PORT_RESET) &&
515f1ae32a1SGerd Hoffmann                      !(port->ctrl & UHCI_PORT_RESET) ) {
516f1ae32a1SGerd Hoffmann                     usb_device_reset(dev);
517f1ae32a1SGerd Hoffmann                 }
518f1ae32a1SGerd Hoffmann             }
519f1ae32a1SGerd Hoffmann             port->ctrl &= UHCI_PORT_READ_ONLY;
520f1ae32a1SGerd Hoffmann             port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
521f1ae32a1SGerd Hoffmann             /* some bits are reset when a '1' is written to them */
522f1ae32a1SGerd Hoffmann             port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
523f1ae32a1SGerd Hoffmann         }
524f1ae32a1SGerd Hoffmann         break;
525f1ae32a1SGerd Hoffmann     }
526f1ae32a1SGerd Hoffmann }
527f1ae32a1SGerd Hoffmann 
528f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr)
529f1ae32a1SGerd Hoffmann {
530f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
531f1ae32a1SGerd Hoffmann     uint32_t val;
532f1ae32a1SGerd Hoffmann 
533f1ae32a1SGerd Hoffmann     addr &= 0x1f;
534f1ae32a1SGerd Hoffmann     switch(addr) {
535f1ae32a1SGerd Hoffmann     case 0x00:
536f1ae32a1SGerd Hoffmann         val = s->cmd;
537f1ae32a1SGerd Hoffmann         break;
538f1ae32a1SGerd Hoffmann     case 0x02:
539f1ae32a1SGerd Hoffmann         val = s->status;
540f1ae32a1SGerd Hoffmann         break;
541f1ae32a1SGerd Hoffmann     case 0x04:
542f1ae32a1SGerd Hoffmann         val = s->intr;
543f1ae32a1SGerd Hoffmann         break;
544f1ae32a1SGerd Hoffmann     case 0x06:
545f1ae32a1SGerd Hoffmann         val = s->frnum;
546f1ae32a1SGerd Hoffmann         break;
547f1ae32a1SGerd Hoffmann     case 0x10 ... 0x1f:
548f1ae32a1SGerd Hoffmann         {
549f1ae32a1SGerd Hoffmann             UHCIPort *port;
550f1ae32a1SGerd Hoffmann             int n;
551f1ae32a1SGerd Hoffmann             n = (addr >> 1) & 7;
552f1ae32a1SGerd Hoffmann             if (n >= NB_PORTS)
553f1ae32a1SGerd Hoffmann                 goto read_default;
554f1ae32a1SGerd Hoffmann             port = &s->ports[n];
555f1ae32a1SGerd Hoffmann             val = port->ctrl;
556f1ae32a1SGerd Hoffmann         }
557f1ae32a1SGerd Hoffmann         break;
558f1ae32a1SGerd Hoffmann     default:
559f1ae32a1SGerd Hoffmann     read_default:
560f1ae32a1SGerd Hoffmann         val = 0xff7f; /* disabled port */
561f1ae32a1SGerd Hoffmann         break;
562f1ae32a1SGerd Hoffmann     }
563f1ae32a1SGerd Hoffmann 
564f1ae32a1SGerd Hoffmann     DPRINTF("uhci: readw port=0x%04x val=0x%04x\n", addr, val);
565f1ae32a1SGerd Hoffmann 
566f1ae32a1SGerd Hoffmann     return val;
567f1ae32a1SGerd Hoffmann }
568f1ae32a1SGerd Hoffmann 
569f1ae32a1SGerd Hoffmann static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
570f1ae32a1SGerd Hoffmann {
571f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
572f1ae32a1SGerd Hoffmann 
573f1ae32a1SGerd Hoffmann     addr &= 0x1f;
574f1ae32a1SGerd Hoffmann     DPRINTF("uhci: writel port=0x%04x val=0x%08x\n", addr, val);
575f1ae32a1SGerd Hoffmann 
576f1ae32a1SGerd Hoffmann     switch(addr) {
577f1ae32a1SGerd Hoffmann     case 0x08:
578f1ae32a1SGerd Hoffmann         s->fl_base_addr = val & ~0xfff;
579f1ae32a1SGerd Hoffmann         break;
580f1ae32a1SGerd Hoffmann     }
581f1ae32a1SGerd Hoffmann }
582f1ae32a1SGerd Hoffmann 
583f1ae32a1SGerd Hoffmann static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr)
584f1ae32a1SGerd Hoffmann {
585f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
586f1ae32a1SGerd Hoffmann     uint32_t val;
587f1ae32a1SGerd Hoffmann 
588f1ae32a1SGerd Hoffmann     addr &= 0x1f;
589f1ae32a1SGerd Hoffmann     switch(addr) {
590f1ae32a1SGerd Hoffmann     case 0x08:
591f1ae32a1SGerd Hoffmann         val = s->fl_base_addr;
592f1ae32a1SGerd Hoffmann         break;
593f1ae32a1SGerd Hoffmann     default:
594f1ae32a1SGerd Hoffmann         val = 0xffffffff;
595f1ae32a1SGerd Hoffmann         break;
596f1ae32a1SGerd Hoffmann     }
597f1ae32a1SGerd Hoffmann     return val;
598f1ae32a1SGerd Hoffmann }
599f1ae32a1SGerd Hoffmann 
600f1ae32a1SGerd Hoffmann /* signal resume if controller suspended */
601f1ae32a1SGerd Hoffmann static void uhci_resume (void *opaque)
602f1ae32a1SGerd Hoffmann {
603f1ae32a1SGerd Hoffmann     UHCIState *s = (UHCIState *)opaque;
604f1ae32a1SGerd Hoffmann 
605f1ae32a1SGerd Hoffmann     if (!s)
606f1ae32a1SGerd Hoffmann         return;
607f1ae32a1SGerd Hoffmann 
608f1ae32a1SGerd Hoffmann     if (s->cmd & UHCI_CMD_EGSM) {
609f1ae32a1SGerd Hoffmann         s->cmd |= UHCI_CMD_FGR;
610f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_RD;
611f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
612f1ae32a1SGerd Hoffmann     }
613f1ae32a1SGerd Hoffmann }
614f1ae32a1SGerd Hoffmann 
615f1ae32a1SGerd Hoffmann static void uhci_attach(USBPort *port1)
616f1ae32a1SGerd Hoffmann {
617f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
618f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
619f1ae32a1SGerd Hoffmann 
620f1ae32a1SGerd Hoffmann     /* set connect status */
621f1ae32a1SGerd Hoffmann     port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
622f1ae32a1SGerd Hoffmann 
623f1ae32a1SGerd Hoffmann     /* update speed */
624f1ae32a1SGerd Hoffmann     if (port->port.dev->speed == USB_SPEED_LOW) {
625f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_LSDA;
626f1ae32a1SGerd Hoffmann     } else {
627f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_LSDA;
628f1ae32a1SGerd Hoffmann     }
629f1ae32a1SGerd Hoffmann 
630f1ae32a1SGerd Hoffmann     uhci_resume(s);
631f1ae32a1SGerd Hoffmann }
632f1ae32a1SGerd Hoffmann 
633f1ae32a1SGerd Hoffmann static void uhci_detach(USBPort *port1)
634f1ae32a1SGerd Hoffmann {
635f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
636f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
637f1ae32a1SGerd Hoffmann 
638f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, port1->dev);
639f1ae32a1SGerd Hoffmann 
640f1ae32a1SGerd Hoffmann     /* set connect status */
641f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_CCS) {
642f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_CCS;
643f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_CSC;
644f1ae32a1SGerd Hoffmann     }
645f1ae32a1SGerd Hoffmann     /* disable port */
646f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_EN) {
647f1ae32a1SGerd Hoffmann         port->ctrl &= ~UHCI_PORT_EN;
648f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_ENC;
649f1ae32a1SGerd Hoffmann     }
650f1ae32a1SGerd Hoffmann 
651f1ae32a1SGerd Hoffmann     uhci_resume(s);
652f1ae32a1SGerd Hoffmann }
653f1ae32a1SGerd Hoffmann 
654f1ae32a1SGerd Hoffmann static void uhci_child_detach(USBPort *port1, USBDevice *child)
655f1ae32a1SGerd Hoffmann {
656f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
657f1ae32a1SGerd Hoffmann 
658f1ae32a1SGerd Hoffmann     uhci_async_cancel_device(s, child);
659f1ae32a1SGerd Hoffmann }
660f1ae32a1SGerd Hoffmann 
661f1ae32a1SGerd Hoffmann static void uhci_wakeup(USBPort *port1)
662f1ae32a1SGerd Hoffmann {
663f1ae32a1SGerd Hoffmann     UHCIState *s = port1->opaque;
664f1ae32a1SGerd Hoffmann     UHCIPort *port = &s->ports[port1->index];
665f1ae32a1SGerd Hoffmann 
666f1ae32a1SGerd Hoffmann     if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
667f1ae32a1SGerd Hoffmann         port->ctrl |= UHCI_PORT_RD;
668f1ae32a1SGerd Hoffmann         uhci_resume(s);
669f1ae32a1SGerd Hoffmann     }
670f1ae32a1SGerd Hoffmann }
671f1ae32a1SGerd Hoffmann 
672f1ae32a1SGerd Hoffmann static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
673f1ae32a1SGerd Hoffmann {
674f1ae32a1SGerd Hoffmann     USBDevice *dev;
675f1ae32a1SGerd Hoffmann     int i;
676f1ae32a1SGerd Hoffmann 
677f1ae32a1SGerd Hoffmann     for (i = 0; i < NB_PORTS; i++) {
678f1ae32a1SGerd Hoffmann         UHCIPort *port = &s->ports[i];
679f1ae32a1SGerd Hoffmann         if (!(port->ctrl & UHCI_PORT_EN)) {
680f1ae32a1SGerd Hoffmann             continue;
681f1ae32a1SGerd Hoffmann         }
682f1ae32a1SGerd Hoffmann         dev = usb_find_device(&port->port, addr);
683f1ae32a1SGerd Hoffmann         if (dev != NULL) {
684f1ae32a1SGerd Hoffmann             return dev;
685f1ae32a1SGerd Hoffmann         }
686f1ae32a1SGerd Hoffmann     }
687f1ae32a1SGerd Hoffmann     return NULL;
688f1ae32a1SGerd Hoffmann }
689f1ae32a1SGerd Hoffmann 
690f1ae32a1SGerd Hoffmann static void uhci_async_complete(USBPort *port, USBPacket *packet);
691f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s);
692f1ae32a1SGerd Hoffmann 
693f1ae32a1SGerd Hoffmann /* return -1 if fatal error (frame must be stopped)
694f1ae32a1SGerd Hoffmann           0 if TD successful
695f1ae32a1SGerd Hoffmann           1 if TD unsuccessful or inactive
696f1ae32a1SGerd Hoffmann */
697f1ae32a1SGerd Hoffmann static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
698f1ae32a1SGerd Hoffmann {
699f1ae32a1SGerd Hoffmann     int len = 0, max_len, err, ret;
700f1ae32a1SGerd Hoffmann     uint8_t pid;
701f1ae32a1SGerd Hoffmann 
702f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
703f1ae32a1SGerd Hoffmann     pid = td->token & 0xff;
704f1ae32a1SGerd Hoffmann 
705f1ae32a1SGerd Hoffmann     ret = async->packet.result;
706f1ae32a1SGerd Hoffmann 
707f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOS)
708f1ae32a1SGerd Hoffmann         td->ctrl &= ~TD_CTRL_ACTIVE;
709f1ae32a1SGerd Hoffmann 
710f1ae32a1SGerd Hoffmann     if (ret < 0)
711f1ae32a1SGerd Hoffmann         goto out;
712f1ae32a1SGerd Hoffmann 
713f1ae32a1SGerd Hoffmann     len = async->packet.result;
714f1ae32a1SGerd Hoffmann     td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
715f1ae32a1SGerd Hoffmann 
716f1ae32a1SGerd Hoffmann     /* The NAK bit may have been set by a previous frame, so clear it
717f1ae32a1SGerd Hoffmann        here.  The docs are somewhat unclear, but win2k relies on this
718f1ae32a1SGerd Hoffmann        behavior.  */
719f1ae32a1SGerd Hoffmann     td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
720f1ae32a1SGerd Hoffmann     if (td->ctrl & TD_CTRL_IOC)
721f1ae32a1SGerd Hoffmann         *int_mask |= 0x01;
722f1ae32a1SGerd Hoffmann 
723f1ae32a1SGerd Hoffmann     if (pid == USB_TOKEN_IN) {
724f1ae32a1SGerd Hoffmann         if (len > max_len) {
725f1ae32a1SGerd Hoffmann             ret = USB_RET_BABBLE;
726f1ae32a1SGerd Hoffmann             goto out;
727f1ae32a1SGerd Hoffmann         }
728f1ae32a1SGerd Hoffmann 
729f1ae32a1SGerd Hoffmann         if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
730f1ae32a1SGerd Hoffmann             *int_mask |= 0x02;
731f1ae32a1SGerd Hoffmann             /* short packet: do not update QH */
732f1ae32a1SGerd Hoffmann             DPRINTF("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token);
733f1ae32a1SGerd Hoffmann             return 1;
734f1ae32a1SGerd Hoffmann         }
735f1ae32a1SGerd Hoffmann     }
736f1ae32a1SGerd Hoffmann 
737f1ae32a1SGerd Hoffmann     /* success */
738f1ae32a1SGerd Hoffmann     return 0;
739f1ae32a1SGerd Hoffmann 
740f1ae32a1SGerd Hoffmann out:
741f1ae32a1SGerd Hoffmann     switch(ret) {
742f1ae32a1SGerd Hoffmann     case USB_RET_STALL:
743f1ae32a1SGerd Hoffmann         td->ctrl |= TD_CTRL_STALL;
744f1ae32a1SGerd Hoffmann         td->ctrl &= ~TD_CTRL_ACTIVE;
745f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_USBERR;
746f1ae32a1SGerd Hoffmann         if (td->ctrl & TD_CTRL_IOC) {
747f1ae32a1SGerd Hoffmann             *int_mask |= 0x01;
748f1ae32a1SGerd Hoffmann         }
749f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
750f1ae32a1SGerd Hoffmann         return 1;
751f1ae32a1SGerd Hoffmann 
752f1ae32a1SGerd Hoffmann     case USB_RET_BABBLE:
753f1ae32a1SGerd Hoffmann         td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
754f1ae32a1SGerd Hoffmann         td->ctrl &= ~TD_CTRL_ACTIVE;
755f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_USBERR;
756f1ae32a1SGerd Hoffmann         if (td->ctrl & TD_CTRL_IOC) {
757f1ae32a1SGerd Hoffmann             *int_mask |= 0x01;
758f1ae32a1SGerd Hoffmann         }
759f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
760f1ae32a1SGerd Hoffmann         /* frame interrupted */
761f1ae32a1SGerd Hoffmann         return -1;
762f1ae32a1SGerd Hoffmann 
763f1ae32a1SGerd Hoffmann     case USB_RET_NAK:
764f1ae32a1SGerd Hoffmann         td->ctrl |= TD_CTRL_NAK;
765f1ae32a1SGerd Hoffmann         if (pid == USB_TOKEN_SETUP)
766f1ae32a1SGerd Hoffmann             break;
767f1ae32a1SGerd Hoffmann 	return 1;
768f1ae32a1SGerd Hoffmann 
769f1ae32a1SGerd Hoffmann     case USB_RET_IOERROR:
770f1ae32a1SGerd Hoffmann     case USB_RET_NODEV:
771f1ae32a1SGerd Hoffmann     default:
772f1ae32a1SGerd Hoffmann 	break;
773f1ae32a1SGerd Hoffmann     }
774f1ae32a1SGerd Hoffmann 
775f1ae32a1SGerd Hoffmann     /* Retry the TD if error count is not zero */
776f1ae32a1SGerd Hoffmann 
777f1ae32a1SGerd Hoffmann     td->ctrl |= TD_CTRL_TIMEOUT;
778f1ae32a1SGerd Hoffmann     err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
779f1ae32a1SGerd Hoffmann     if (err != 0) {
780f1ae32a1SGerd Hoffmann         err--;
781f1ae32a1SGerd Hoffmann         if (err == 0) {
782f1ae32a1SGerd Hoffmann             td->ctrl &= ~TD_CTRL_ACTIVE;
783f1ae32a1SGerd Hoffmann             s->status |= UHCI_STS_USBERR;
784f1ae32a1SGerd Hoffmann             if (td->ctrl & TD_CTRL_IOC)
785f1ae32a1SGerd Hoffmann                 *int_mask |= 0x01;
786f1ae32a1SGerd Hoffmann             uhci_update_irq(s);
787f1ae32a1SGerd Hoffmann         }
788f1ae32a1SGerd Hoffmann     }
789f1ae32a1SGerd Hoffmann     td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
790f1ae32a1SGerd Hoffmann         (err << TD_CTRL_ERROR_SHIFT);
791f1ae32a1SGerd Hoffmann     return 1;
792f1ae32a1SGerd Hoffmann }
793f1ae32a1SGerd Hoffmann 
794f1ae32a1SGerd Hoffmann static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask)
795f1ae32a1SGerd Hoffmann {
796f1ae32a1SGerd Hoffmann     UHCIAsync *async;
797f1ae32a1SGerd Hoffmann     int len = 0, max_len;
798f1ae32a1SGerd Hoffmann     uint8_t pid;
799f1ae32a1SGerd Hoffmann     USBDevice *dev;
800f1ae32a1SGerd Hoffmann     USBEndpoint *ep;
801f1ae32a1SGerd Hoffmann 
802f1ae32a1SGerd Hoffmann     /* Is active ? */
803f1ae32a1SGerd Hoffmann     if (!(td->ctrl & TD_CTRL_ACTIVE))
804f1ae32a1SGerd Hoffmann         return 1;
805f1ae32a1SGerd Hoffmann 
806f1ae32a1SGerd Hoffmann     async = uhci_async_find_td(s, addr, td);
807f1ae32a1SGerd Hoffmann     if (async) {
808f1ae32a1SGerd Hoffmann         /* Already submitted */
809f1ae32a1SGerd Hoffmann         async->queue->valid = 32;
810f1ae32a1SGerd Hoffmann 
811f1ae32a1SGerd Hoffmann         if (!async->done)
812f1ae32a1SGerd Hoffmann             return 1;
813f1ae32a1SGerd Hoffmann 
814f1ae32a1SGerd Hoffmann         uhci_async_unlink(async);
815f1ae32a1SGerd Hoffmann         goto done;
816f1ae32a1SGerd Hoffmann     }
817f1ae32a1SGerd Hoffmann 
818f1ae32a1SGerd Hoffmann     /* Allocate new packet */
81916ce543eSGerd Hoffmann     async = uhci_async_alloc(uhci_queue_get(s, td), addr);
820f1ae32a1SGerd Hoffmann     if (!async)
821f1ae32a1SGerd Hoffmann         return 1;
822f1ae32a1SGerd Hoffmann 
823f1ae32a1SGerd Hoffmann     /* valid needs to be large enough to handle 10 frame delay
824f1ae32a1SGerd Hoffmann      * for initial isochronous requests
825f1ae32a1SGerd Hoffmann      */
826f1ae32a1SGerd Hoffmann     async->queue->valid = 32;
827f1ae32a1SGerd Hoffmann     async->isoc  = td->ctrl & TD_CTRL_IOS;
828f1ae32a1SGerd Hoffmann 
829f1ae32a1SGerd Hoffmann     max_len = ((td->token >> 21) + 1) & 0x7ff;
830f1ae32a1SGerd Hoffmann     pid = td->token & 0xff;
831f1ae32a1SGerd Hoffmann 
832f1ae32a1SGerd Hoffmann     dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
833f1ae32a1SGerd Hoffmann     ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
834f1ae32a1SGerd Hoffmann     usb_packet_setup(&async->packet, pid, ep);
835f1ae32a1SGerd Hoffmann     qemu_sglist_add(&async->sgl, td->buffer, max_len);
836f1ae32a1SGerd Hoffmann     usb_packet_map(&async->packet, &async->sgl);
837f1ae32a1SGerd Hoffmann 
838f1ae32a1SGerd Hoffmann     switch(pid) {
839f1ae32a1SGerd Hoffmann     case USB_TOKEN_OUT:
840f1ae32a1SGerd Hoffmann     case USB_TOKEN_SETUP:
841f1ae32a1SGerd Hoffmann         len = usb_handle_packet(dev, &async->packet);
842f1ae32a1SGerd Hoffmann         if (len >= 0)
843f1ae32a1SGerd Hoffmann             len = max_len;
844f1ae32a1SGerd Hoffmann         break;
845f1ae32a1SGerd Hoffmann 
846f1ae32a1SGerd Hoffmann     case USB_TOKEN_IN:
847f1ae32a1SGerd Hoffmann         len = usb_handle_packet(dev, &async->packet);
848f1ae32a1SGerd Hoffmann         break;
849f1ae32a1SGerd Hoffmann 
850f1ae32a1SGerd Hoffmann     default:
851f1ae32a1SGerd Hoffmann         /* invalid pid : frame interrupted */
852f1ae32a1SGerd Hoffmann         uhci_async_free(async);
853f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_HCPERR;
854f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
855f1ae32a1SGerd Hoffmann         return -1;
856f1ae32a1SGerd Hoffmann     }
857f1ae32a1SGerd Hoffmann 
858f1ae32a1SGerd Hoffmann     if (len == USB_RET_ASYNC) {
859f1ae32a1SGerd Hoffmann         uhci_async_link(async);
860f1ae32a1SGerd Hoffmann         return 2;
861f1ae32a1SGerd Hoffmann     }
862f1ae32a1SGerd Hoffmann 
863f1ae32a1SGerd Hoffmann     async->packet.result = len;
864f1ae32a1SGerd Hoffmann 
865f1ae32a1SGerd Hoffmann done:
866f1ae32a1SGerd Hoffmann     len = uhci_complete_td(s, td, async, int_mask);
867f1ae32a1SGerd Hoffmann     usb_packet_unmap(&async->packet);
868f1ae32a1SGerd Hoffmann     uhci_async_free(async);
869f1ae32a1SGerd Hoffmann     return len;
870f1ae32a1SGerd Hoffmann }
871f1ae32a1SGerd Hoffmann 
872f1ae32a1SGerd Hoffmann static void uhci_async_complete(USBPort *port, USBPacket *packet)
873f1ae32a1SGerd Hoffmann {
874f1ae32a1SGerd Hoffmann     UHCIAsync *async = container_of(packet, UHCIAsync, packet);
875f1ae32a1SGerd Hoffmann     UHCIState *s = async->queue->uhci;
876f1ae32a1SGerd Hoffmann 
877f1ae32a1SGerd Hoffmann     DPRINTF("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token);
878f1ae32a1SGerd Hoffmann 
879f1ae32a1SGerd Hoffmann     if (async->isoc) {
880f1ae32a1SGerd Hoffmann         UHCI_TD td;
881f1ae32a1SGerd Hoffmann         uint32_t link = async->td;
882f1ae32a1SGerd Hoffmann         uint32_t int_mask = 0, val;
883f1ae32a1SGerd Hoffmann 
884f1ae32a1SGerd Hoffmann         pci_dma_read(&s->dev, link & ~0xf, &td, sizeof(td));
885f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.link);
886f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.ctrl);
887f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.token);
888f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.buffer);
889f1ae32a1SGerd Hoffmann 
890f1ae32a1SGerd Hoffmann         uhci_async_unlink(async);
891f1ae32a1SGerd Hoffmann         uhci_complete_td(s, &td, async, &int_mask);
892f1ae32a1SGerd Hoffmann         s->pending_int_mask |= int_mask;
893f1ae32a1SGerd Hoffmann 
894f1ae32a1SGerd Hoffmann         /* update the status bits of the TD */
895f1ae32a1SGerd Hoffmann         val = cpu_to_le32(td.ctrl);
896f1ae32a1SGerd Hoffmann         pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
897f1ae32a1SGerd Hoffmann         uhci_async_free(async);
898f1ae32a1SGerd Hoffmann     } else {
899f1ae32a1SGerd Hoffmann         async->done = 1;
900f1ae32a1SGerd Hoffmann         uhci_process_frame(s);
901f1ae32a1SGerd Hoffmann     }
902f1ae32a1SGerd Hoffmann }
903f1ae32a1SGerd Hoffmann 
904f1ae32a1SGerd Hoffmann static int is_valid(uint32_t link)
905f1ae32a1SGerd Hoffmann {
906f1ae32a1SGerd Hoffmann     return (link & 1) == 0;
907f1ae32a1SGerd Hoffmann }
908f1ae32a1SGerd Hoffmann 
909f1ae32a1SGerd Hoffmann static int is_qh(uint32_t link)
910f1ae32a1SGerd Hoffmann {
911f1ae32a1SGerd Hoffmann     return (link & 2) != 0;
912f1ae32a1SGerd Hoffmann }
913f1ae32a1SGerd Hoffmann 
914f1ae32a1SGerd Hoffmann static int depth_first(uint32_t link)
915f1ae32a1SGerd Hoffmann {
916f1ae32a1SGerd Hoffmann     return (link & 4) != 0;
917f1ae32a1SGerd Hoffmann }
918f1ae32a1SGerd Hoffmann 
919f1ae32a1SGerd Hoffmann /* QH DB used for detecting QH loops */
920f1ae32a1SGerd Hoffmann #define UHCI_MAX_QUEUES 128
921f1ae32a1SGerd Hoffmann typedef struct {
922f1ae32a1SGerd Hoffmann     uint32_t addr[UHCI_MAX_QUEUES];
923f1ae32a1SGerd Hoffmann     int      count;
924f1ae32a1SGerd Hoffmann } QhDb;
925f1ae32a1SGerd Hoffmann 
926f1ae32a1SGerd Hoffmann static void qhdb_reset(QhDb *db)
927f1ae32a1SGerd Hoffmann {
928f1ae32a1SGerd Hoffmann     db->count = 0;
929f1ae32a1SGerd Hoffmann }
930f1ae32a1SGerd Hoffmann 
931f1ae32a1SGerd Hoffmann /* Add QH to DB. Returns 1 if already present or DB is full. */
932f1ae32a1SGerd Hoffmann static int qhdb_insert(QhDb *db, uint32_t addr)
933f1ae32a1SGerd Hoffmann {
934f1ae32a1SGerd Hoffmann     int i;
935f1ae32a1SGerd Hoffmann     for (i = 0; i < db->count; i++)
936f1ae32a1SGerd Hoffmann         if (db->addr[i] == addr)
937f1ae32a1SGerd Hoffmann             return 1;
938f1ae32a1SGerd Hoffmann 
939f1ae32a1SGerd Hoffmann     if (db->count >= UHCI_MAX_QUEUES)
940f1ae32a1SGerd Hoffmann         return 1;
941f1ae32a1SGerd Hoffmann 
942f1ae32a1SGerd Hoffmann     db->addr[db->count++] = addr;
943f1ae32a1SGerd Hoffmann     return 0;
944f1ae32a1SGerd Hoffmann }
945f1ae32a1SGerd Hoffmann 
946f1ae32a1SGerd Hoffmann static void uhci_fill_queue(UHCIState *s, UHCI_TD *td)
947f1ae32a1SGerd Hoffmann {
948f1ae32a1SGerd Hoffmann     uint32_t int_mask = 0;
949f1ae32a1SGerd Hoffmann     uint32_t plink = td->link;
950f1ae32a1SGerd Hoffmann     uint32_t token = uhci_queue_token(td);
951f1ae32a1SGerd Hoffmann     UHCI_TD ptd;
952f1ae32a1SGerd Hoffmann     int ret;
953f1ae32a1SGerd Hoffmann 
954f1ae32a1SGerd Hoffmann     while (is_valid(plink)) {
955f1ae32a1SGerd Hoffmann         pci_dma_read(&s->dev, plink & ~0xf, &ptd, sizeof(ptd));
956f1ae32a1SGerd Hoffmann         le32_to_cpus(&ptd.link);
957f1ae32a1SGerd Hoffmann         le32_to_cpus(&ptd.ctrl);
958f1ae32a1SGerd Hoffmann         le32_to_cpus(&ptd.token);
959f1ae32a1SGerd Hoffmann         le32_to_cpus(&ptd.buffer);
960f1ae32a1SGerd Hoffmann         if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
961f1ae32a1SGerd Hoffmann             break;
962f1ae32a1SGerd Hoffmann         }
963f1ae32a1SGerd Hoffmann         if (uhci_queue_token(&ptd) != token) {
964f1ae32a1SGerd Hoffmann             break;
965f1ae32a1SGerd Hoffmann         }
966f1ae32a1SGerd Hoffmann         ret = uhci_handle_td(s, plink, &ptd, &int_mask);
967f1ae32a1SGerd Hoffmann         assert(ret == 2); /* got USB_RET_ASYNC */
968f1ae32a1SGerd Hoffmann         assert(int_mask == 0);
969f1ae32a1SGerd Hoffmann         plink = ptd.link;
970f1ae32a1SGerd Hoffmann     }
971f1ae32a1SGerd Hoffmann }
972f1ae32a1SGerd Hoffmann 
973f1ae32a1SGerd Hoffmann static void uhci_process_frame(UHCIState *s)
974f1ae32a1SGerd Hoffmann {
975f1ae32a1SGerd Hoffmann     uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
976f1ae32a1SGerd Hoffmann     uint32_t curr_qh, td_count = 0, bytes_count = 0;
977f1ae32a1SGerd Hoffmann     int cnt, ret;
978f1ae32a1SGerd Hoffmann     UHCI_TD td;
979f1ae32a1SGerd Hoffmann     UHCI_QH qh;
980f1ae32a1SGerd Hoffmann     QhDb qhdb;
981f1ae32a1SGerd Hoffmann 
982f1ae32a1SGerd Hoffmann     frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
983f1ae32a1SGerd Hoffmann 
984f1ae32a1SGerd Hoffmann     DPRINTF("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr);
985f1ae32a1SGerd Hoffmann 
986f1ae32a1SGerd Hoffmann     pci_dma_read(&s->dev, frame_addr, &link, 4);
987f1ae32a1SGerd Hoffmann     le32_to_cpus(&link);
988f1ae32a1SGerd Hoffmann 
989f1ae32a1SGerd Hoffmann     int_mask = 0;
990f1ae32a1SGerd Hoffmann     curr_qh  = 0;
991f1ae32a1SGerd Hoffmann 
992f1ae32a1SGerd Hoffmann     qhdb_reset(&qhdb);
993f1ae32a1SGerd Hoffmann 
994f1ae32a1SGerd Hoffmann     for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
995f1ae32a1SGerd Hoffmann         if (is_qh(link)) {
996f1ae32a1SGerd Hoffmann             /* QH */
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                  *
1003f1ae32a1SGerd Hoffmann                  * Stop processing here if
1004f1ae32a1SGerd Hoffmann                  *  (a) no transaction has been done since we've been
1005f1ae32a1SGerd Hoffmann                  *      here last time, or
1006f1ae32a1SGerd Hoffmann                  *  (b) we've reached the usb 1.1 bandwidth, which is
1007f1ae32a1SGerd Hoffmann                  *      1280 bytes/frame.
1008f1ae32a1SGerd Hoffmann                  */
1009f1ae32a1SGerd Hoffmann                 DPRINTF("uhci: detected loop. qh 0x%x\n", link);
1010f1ae32a1SGerd Hoffmann                 if (td_count == 0) {
1011f1ae32a1SGerd Hoffmann                     DPRINTF("uhci: no transaction last round, stop\n");
1012f1ae32a1SGerd Hoffmann                     break;
1013f1ae32a1SGerd Hoffmann                 } else if (bytes_count >= 1280) {
1014f1ae32a1SGerd Hoffmann                     DPRINTF("uhci: bandwidth limit reached, stop\n");
1015f1ae32a1SGerd Hoffmann                     break;
1016f1ae32a1SGerd Hoffmann                 } else {
1017f1ae32a1SGerd Hoffmann                     td_count = 0;
1018f1ae32a1SGerd Hoffmann                     qhdb_reset(&qhdb);
1019f1ae32a1SGerd Hoffmann                     qhdb_insert(&qhdb, link);
1020f1ae32a1SGerd Hoffmann                 }
1021f1ae32a1SGerd Hoffmann             }
1022f1ae32a1SGerd Hoffmann 
1023f1ae32a1SGerd Hoffmann             pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
1024f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.link);
1025f1ae32a1SGerd Hoffmann             le32_to_cpus(&qh.el_link);
1026f1ae32a1SGerd Hoffmann 
1027f1ae32a1SGerd Hoffmann             DPRINTF("uhci: QH 0x%x load. link 0x%x elink 0x%x\n",
1028f1ae32a1SGerd Hoffmann                     link, qh.link, qh.el_link);
1029f1ae32a1SGerd Hoffmann 
1030f1ae32a1SGerd Hoffmann             if (!is_valid(qh.el_link)) {
1031f1ae32a1SGerd Hoffmann                 /* QH w/o elements */
1032f1ae32a1SGerd Hoffmann                 curr_qh = 0;
1033f1ae32a1SGerd Hoffmann                 link = qh.link;
1034f1ae32a1SGerd Hoffmann             } else {
1035f1ae32a1SGerd Hoffmann                 /* QH with elements */
1036f1ae32a1SGerd Hoffmann             	curr_qh = link;
1037f1ae32a1SGerd Hoffmann             	link = qh.el_link;
1038f1ae32a1SGerd Hoffmann             }
1039f1ae32a1SGerd Hoffmann             continue;
1040f1ae32a1SGerd Hoffmann         }
1041f1ae32a1SGerd Hoffmann 
1042f1ae32a1SGerd Hoffmann         /* TD */
1043f1ae32a1SGerd Hoffmann         pci_dma_read(&s->dev, link & ~0xf, &td, sizeof(td));
1044f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.link);
1045f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.ctrl);
1046f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.token);
1047f1ae32a1SGerd Hoffmann         le32_to_cpus(&td.buffer);
1048f1ae32a1SGerd Hoffmann 
1049f1ae32a1SGerd Hoffmann         DPRINTF("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1050f1ae32a1SGerd Hoffmann                 link, td.link, td.ctrl, td.token, curr_qh);
1051f1ae32a1SGerd Hoffmann 
1052f1ae32a1SGerd Hoffmann         old_td_ctrl = td.ctrl;
1053f1ae32a1SGerd Hoffmann         ret = uhci_handle_td(s, link, &td, &int_mask);
1054f1ae32a1SGerd Hoffmann         if (old_td_ctrl != td.ctrl) {
1055f1ae32a1SGerd Hoffmann             /* update the status bits of the TD */
1056f1ae32a1SGerd Hoffmann             val = cpu_to_le32(td.ctrl);
1057f1ae32a1SGerd Hoffmann             pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
1058f1ae32a1SGerd Hoffmann         }
1059f1ae32a1SGerd Hoffmann 
1060f1ae32a1SGerd Hoffmann         switch (ret) {
1061f1ae32a1SGerd Hoffmann         case -1: /* interrupted frame */
1062f1ae32a1SGerd Hoffmann             goto out;
1063f1ae32a1SGerd Hoffmann 
1064f1ae32a1SGerd Hoffmann         case 1: /* goto next queue */
1065f1ae32a1SGerd Hoffmann             DPRINTF("uhci: TD 0x%x skip. "
1066f1ae32a1SGerd Hoffmann                     "link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1067f1ae32a1SGerd Hoffmann                     link, td.link, td.ctrl, td.token, curr_qh);
1068f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1069f1ae32a1SGerd Hoffmann             continue;
1070f1ae32a1SGerd Hoffmann 
1071f1ae32a1SGerd Hoffmann         case 2: /* got USB_RET_ASYNC */
1072f1ae32a1SGerd Hoffmann             DPRINTF("uhci: TD 0x%x async. "
1073f1ae32a1SGerd Hoffmann                     "link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1074f1ae32a1SGerd Hoffmann                     link, td.link, td.ctrl, td.token, curr_qh);
1075f1ae32a1SGerd Hoffmann             if (is_valid(td.link)) {
1076f1ae32a1SGerd Hoffmann                 uhci_fill_queue(s, &td);
1077f1ae32a1SGerd Hoffmann             }
1078f1ae32a1SGerd Hoffmann             link = curr_qh ? qh.link : td.link;
1079f1ae32a1SGerd Hoffmann             continue;
1080f1ae32a1SGerd Hoffmann 
1081f1ae32a1SGerd Hoffmann         case 0: /* completed TD */
1082f1ae32a1SGerd Hoffmann             DPRINTF("uhci: TD 0x%x done. "
1083f1ae32a1SGerd Hoffmann                     "link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n",
1084f1ae32a1SGerd Hoffmann                     link, td.link, td.ctrl, td.token, curr_qh);
1085f1ae32a1SGerd Hoffmann 
1086f1ae32a1SGerd Hoffmann             link = td.link;
1087f1ae32a1SGerd Hoffmann             td_count++;
1088f1ae32a1SGerd Hoffmann             bytes_count += (td.ctrl & 0x7ff) + 1;
1089f1ae32a1SGerd Hoffmann 
1090f1ae32a1SGerd Hoffmann             if (curr_qh) {
1091f1ae32a1SGerd Hoffmann                 /* update QH element link */
1092f1ae32a1SGerd Hoffmann                 qh.el_link = link;
1093f1ae32a1SGerd Hoffmann                 val = cpu_to_le32(qh.el_link);
1094f1ae32a1SGerd Hoffmann                 pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
1095f1ae32a1SGerd Hoffmann 
1096f1ae32a1SGerd Hoffmann                 if (!depth_first(link)) {
1097f1ae32a1SGerd Hoffmann                     /* done with this QH */
1098f1ae32a1SGerd Hoffmann 
1099f1ae32a1SGerd Hoffmann                     DPRINTF("uhci: QH 0x%x done. link 0x%x elink 0x%x\n",
1100f1ae32a1SGerd Hoffmann                             curr_qh, qh.link, qh.el_link);
1101f1ae32a1SGerd Hoffmann 
1102f1ae32a1SGerd Hoffmann                     curr_qh = 0;
1103f1ae32a1SGerd Hoffmann                     link    = qh.link;
1104f1ae32a1SGerd Hoffmann                 }
1105f1ae32a1SGerd Hoffmann             }
1106f1ae32a1SGerd Hoffmann             break;
1107f1ae32a1SGerd Hoffmann 
1108f1ae32a1SGerd Hoffmann         default:
1109f1ae32a1SGerd Hoffmann             assert(!"unknown return code");
1110f1ae32a1SGerd Hoffmann         }
1111f1ae32a1SGerd Hoffmann 
1112f1ae32a1SGerd Hoffmann         /* go to the next entry */
1113f1ae32a1SGerd Hoffmann     }
1114f1ae32a1SGerd Hoffmann 
1115f1ae32a1SGerd Hoffmann out:
1116f1ae32a1SGerd Hoffmann     s->pending_int_mask |= int_mask;
1117f1ae32a1SGerd Hoffmann }
1118f1ae32a1SGerd Hoffmann 
1119f1ae32a1SGerd Hoffmann static void uhci_frame_timer(void *opaque)
1120f1ae32a1SGerd Hoffmann {
1121f1ae32a1SGerd Hoffmann     UHCIState *s = opaque;
1122f1ae32a1SGerd Hoffmann 
1123f1ae32a1SGerd Hoffmann     /* prepare the timer for the next frame */
1124f1ae32a1SGerd Hoffmann     s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ);
1125f1ae32a1SGerd Hoffmann 
1126f1ae32a1SGerd Hoffmann     if (!(s->cmd & UHCI_CMD_RS)) {
1127f1ae32a1SGerd Hoffmann         /* Full stop */
1128f1ae32a1SGerd Hoffmann         qemu_del_timer(s->frame_timer);
1129f1ae32a1SGerd Hoffmann         /* set hchalted bit in status - UHCI11D 2.1.2 */
1130f1ae32a1SGerd Hoffmann         s->status |= UHCI_STS_HCHALTED;
1131f1ae32a1SGerd Hoffmann 
1132f1ae32a1SGerd Hoffmann         DPRINTF("uhci: halted\n");
1133f1ae32a1SGerd Hoffmann         return;
1134f1ae32a1SGerd Hoffmann     }
1135f1ae32a1SGerd Hoffmann 
1136f1ae32a1SGerd Hoffmann     /* Complete the previous frame */
1137f1ae32a1SGerd Hoffmann     if (s->pending_int_mask) {
1138f1ae32a1SGerd Hoffmann         s->status2 |= s->pending_int_mask;
1139f1ae32a1SGerd Hoffmann         s->status  |= UHCI_STS_USBINT;
1140f1ae32a1SGerd Hoffmann         uhci_update_irq(s);
1141f1ae32a1SGerd Hoffmann     }
1142f1ae32a1SGerd Hoffmann     s->pending_int_mask = 0;
1143f1ae32a1SGerd Hoffmann 
1144f1ae32a1SGerd Hoffmann     /* Start new frame */
1145f1ae32a1SGerd Hoffmann     s->frnum = (s->frnum + 1) & 0x7ff;
1146f1ae32a1SGerd Hoffmann 
1147f1ae32a1SGerd Hoffmann     DPRINTF("uhci: new frame #%u\n" , s->frnum);
1148f1ae32a1SGerd Hoffmann 
1149f1ae32a1SGerd Hoffmann     uhci_async_validate_begin(s);
1150f1ae32a1SGerd Hoffmann 
1151f1ae32a1SGerd Hoffmann     uhci_process_frame(s);
1152f1ae32a1SGerd Hoffmann 
1153f1ae32a1SGerd Hoffmann     uhci_async_validate_end(s);
1154f1ae32a1SGerd Hoffmann 
1155f1ae32a1SGerd Hoffmann     qemu_mod_timer(s->frame_timer, s->expire_time);
1156f1ae32a1SGerd Hoffmann }
1157f1ae32a1SGerd Hoffmann 
1158f1ae32a1SGerd Hoffmann static const MemoryRegionPortio uhci_portio[] = {
1159f1ae32a1SGerd Hoffmann     { 0, 32, 2, .write = uhci_ioport_writew, },
1160f1ae32a1SGerd Hoffmann     { 0, 32, 2, .read = uhci_ioport_readw, },
1161f1ae32a1SGerd Hoffmann     { 0, 32, 4, .write = uhci_ioport_writel, },
1162f1ae32a1SGerd Hoffmann     { 0, 32, 4, .read = uhci_ioport_readl, },
1163f1ae32a1SGerd Hoffmann     { 0, 32, 1, .write = uhci_ioport_writeb, },
1164f1ae32a1SGerd Hoffmann     { 0, 32, 1, .read = uhci_ioport_readb, },
1165f1ae32a1SGerd Hoffmann     PORTIO_END_OF_LIST()
1166f1ae32a1SGerd Hoffmann };
1167f1ae32a1SGerd Hoffmann 
1168f1ae32a1SGerd Hoffmann static const MemoryRegionOps uhci_ioport_ops = {
1169f1ae32a1SGerd Hoffmann     .old_portio = uhci_portio,
1170f1ae32a1SGerd Hoffmann };
1171f1ae32a1SGerd Hoffmann 
1172f1ae32a1SGerd Hoffmann static USBPortOps uhci_port_ops = {
1173f1ae32a1SGerd Hoffmann     .attach = uhci_attach,
1174f1ae32a1SGerd Hoffmann     .detach = uhci_detach,
1175f1ae32a1SGerd Hoffmann     .child_detach = uhci_child_detach,
1176f1ae32a1SGerd Hoffmann     .wakeup = uhci_wakeup,
1177f1ae32a1SGerd Hoffmann     .complete = uhci_async_complete,
1178f1ae32a1SGerd Hoffmann };
1179f1ae32a1SGerd Hoffmann 
1180f1ae32a1SGerd Hoffmann static USBBusOps uhci_bus_ops = {
1181f1ae32a1SGerd Hoffmann };
1182f1ae32a1SGerd Hoffmann 
1183f1ae32a1SGerd Hoffmann static int usb_uhci_common_initfn(PCIDevice *dev)
1184f1ae32a1SGerd Hoffmann {
1185f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1186f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1187f1ae32a1SGerd Hoffmann     int i;
1188f1ae32a1SGerd Hoffmann 
1189f1ae32a1SGerd Hoffmann     pci_conf[PCI_CLASS_PROG] = 0x00;
1190f1ae32a1SGerd Hoffmann     /* TODO: reset value should be 0. */
1191f1ae32a1SGerd Hoffmann     pci_conf[PCI_INTERRUPT_PIN] = 4; /* interrupt pin D */
1192f1ae32a1SGerd Hoffmann     pci_conf[USB_SBRN] = USB_RELEASE_1; // release number
1193f1ae32a1SGerd Hoffmann 
1194f1ae32a1SGerd Hoffmann     if (s->masterbus) {
1195f1ae32a1SGerd Hoffmann         USBPort *ports[NB_PORTS];
1196f1ae32a1SGerd Hoffmann         for(i = 0; i < NB_PORTS; i++) {
1197f1ae32a1SGerd Hoffmann             ports[i] = &s->ports[i].port;
1198f1ae32a1SGerd Hoffmann         }
1199f1ae32a1SGerd Hoffmann         if (usb_register_companion(s->masterbus, ports, NB_PORTS,
1200f1ae32a1SGerd Hoffmann                 s->firstport, s, &uhci_port_ops,
1201f1ae32a1SGerd Hoffmann                 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
1202f1ae32a1SGerd Hoffmann             return -1;
1203f1ae32a1SGerd Hoffmann         }
1204f1ae32a1SGerd Hoffmann     } else {
1205f1ae32a1SGerd Hoffmann         usb_bus_new(&s->bus, &uhci_bus_ops, &s->dev.qdev);
1206f1ae32a1SGerd Hoffmann         for (i = 0; i < NB_PORTS; i++) {
1207f1ae32a1SGerd Hoffmann             usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
1208f1ae32a1SGerd Hoffmann                               USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1209f1ae32a1SGerd Hoffmann         }
1210f1ae32a1SGerd Hoffmann     }
1211f1ae32a1SGerd Hoffmann     s->frame_timer = qemu_new_timer_ns(vm_clock, uhci_frame_timer, s);
1212f1ae32a1SGerd Hoffmann     s->num_ports_vmstate = NB_PORTS;
1213f1ae32a1SGerd Hoffmann     QTAILQ_INIT(&s->queues);
1214f1ae32a1SGerd Hoffmann 
1215f1ae32a1SGerd Hoffmann     qemu_register_reset(uhci_reset, s);
1216f1ae32a1SGerd Hoffmann 
1217f1ae32a1SGerd Hoffmann     memory_region_init_io(&s->io_bar, &uhci_ioport_ops, s, "uhci", 0x20);
1218f1ae32a1SGerd Hoffmann     /* Use region 4 for consistency with real hardware.  BSD guests seem
1219f1ae32a1SGerd Hoffmann        to rely on this.  */
1220f1ae32a1SGerd Hoffmann     pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
1221f1ae32a1SGerd Hoffmann 
1222f1ae32a1SGerd Hoffmann     return 0;
1223f1ae32a1SGerd Hoffmann }
1224f1ae32a1SGerd Hoffmann 
1225f1ae32a1SGerd Hoffmann static int usb_uhci_vt82c686b_initfn(PCIDevice *dev)
1226f1ae32a1SGerd Hoffmann {
1227f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1228f1ae32a1SGerd Hoffmann     uint8_t *pci_conf = s->dev.config;
1229f1ae32a1SGerd Hoffmann 
1230f1ae32a1SGerd Hoffmann     /* USB misc control 1/2 */
1231f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x40,0x00001000);
1232f1ae32a1SGerd Hoffmann     /* PM capability */
1233f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0x80,0x00020001);
1234f1ae32a1SGerd Hoffmann     /* USB legacy support  */
1235f1ae32a1SGerd Hoffmann     pci_set_long(pci_conf + 0xc0,0x00002000);
1236f1ae32a1SGerd Hoffmann 
1237f1ae32a1SGerd Hoffmann     return usb_uhci_common_initfn(dev);
1238f1ae32a1SGerd Hoffmann }
1239f1ae32a1SGerd Hoffmann 
1240f1ae32a1SGerd Hoffmann static int usb_uhci_exit(PCIDevice *dev)
1241f1ae32a1SGerd Hoffmann {
1242f1ae32a1SGerd Hoffmann     UHCIState *s = DO_UPCAST(UHCIState, dev, dev);
1243f1ae32a1SGerd Hoffmann 
1244f1ae32a1SGerd Hoffmann     memory_region_destroy(&s->io_bar);
1245f1ae32a1SGerd Hoffmann     return 0;
1246f1ae32a1SGerd Hoffmann }
1247f1ae32a1SGerd Hoffmann 
1248f1ae32a1SGerd Hoffmann static Property uhci_properties[] = {
1249f1ae32a1SGerd Hoffmann     DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
1250f1ae32a1SGerd Hoffmann     DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
1251f1ae32a1SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
1252f1ae32a1SGerd Hoffmann };
1253f1ae32a1SGerd Hoffmann 
1254f1ae32a1SGerd Hoffmann static void piix3_uhci_class_init(ObjectClass *klass, void *data)
1255f1ae32a1SGerd Hoffmann {
1256f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1257f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1258f1ae32a1SGerd Hoffmann 
1259f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1260f1ae32a1SGerd Hoffmann     k->exit = usb_uhci_exit;
1261f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1262f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82371SB_2;
1263f1ae32a1SGerd Hoffmann     k->revision = 0x01;
1264f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1265f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1266f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1267f1ae32a1SGerd Hoffmann }
1268f1ae32a1SGerd Hoffmann 
1269f1ae32a1SGerd Hoffmann static TypeInfo piix3_uhci_info = {
1270f1ae32a1SGerd Hoffmann     .name          = "piix3-usb-uhci",
1271f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1272f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1273f1ae32a1SGerd Hoffmann     .class_init    = piix3_uhci_class_init,
1274f1ae32a1SGerd Hoffmann };
1275f1ae32a1SGerd Hoffmann 
1276f1ae32a1SGerd Hoffmann static void piix4_uhci_class_init(ObjectClass *klass, void *data)
1277f1ae32a1SGerd Hoffmann {
1278f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1279f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1280f1ae32a1SGerd Hoffmann 
1281f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1282f1ae32a1SGerd Hoffmann     k->exit = usb_uhci_exit;
1283f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1284f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82371AB_2;
1285f1ae32a1SGerd Hoffmann     k->revision = 0x01;
1286f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1287f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1288f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1289f1ae32a1SGerd Hoffmann }
1290f1ae32a1SGerd Hoffmann 
1291f1ae32a1SGerd Hoffmann static TypeInfo piix4_uhci_info = {
1292f1ae32a1SGerd Hoffmann     .name          = "piix4-usb-uhci",
1293f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1294f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1295f1ae32a1SGerd Hoffmann     .class_init    = piix4_uhci_class_init,
1296f1ae32a1SGerd Hoffmann };
1297f1ae32a1SGerd Hoffmann 
1298f1ae32a1SGerd Hoffmann static void vt82c686b_uhci_class_init(ObjectClass *klass, void *data)
1299f1ae32a1SGerd Hoffmann {
1300f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1301f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1302f1ae32a1SGerd Hoffmann 
1303f1ae32a1SGerd Hoffmann     k->init = usb_uhci_vt82c686b_initfn;
1304f1ae32a1SGerd Hoffmann     k->exit = usb_uhci_exit;
1305f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_VIA;
1306f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_VIA_UHCI;
1307f1ae32a1SGerd Hoffmann     k->revision = 0x01;
1308f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1309f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1310f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1311f1ae32a1SGerd Hoffmann }
1312f1ae32a1SGerd Hoffmann 
1313f1ae32a1SGerd Hoffmann static TypeInfo vt82c686b_uhci_info = {
1314f1ae32a1SGerd Hoffmann     .name          = "vt82c686b-usb-uhci",
1315f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1316f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1317f1ae32a1SGerd Hoffmann     .class_init    = vt82c686b_uhci_class_init,
1318f1ae32a1SGerd Hoffmann };
1319f1ae32a1SGerd Hoffmann 
1320f1ae32a1SGerd Hoffmann static void ich9_uhci1_class_init(ObjectClass *klass, void *data)
1321f1ae32a1SGerd Hoffmann {
1322f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1323f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1324f1ae32a1SGerd Hoffmann 
1325f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1326f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1327f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1;
1328f1ae32a1SGerd Hoffmann     k->revision = 0x03;
1329f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1330f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1331f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1332f1ae32a1SGerd Hoffmann }
1333f1ae32a1SGerd Hoffmann 
1334f1ae32a1SGerd Hoffmann static TypeInfo ich9_uhci1_info = {
1335f1ae32a1SGerd Hoffmann     .name          = "ich9-usb-uhci1",
1336f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1337f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1338f1ae32a1SGerd Hoffmann     .class_init    = ich9_uhci1_class_init,
1339f1ae32a1SGerd Hoffmann };
1340f1ae32a1SGerd Hoffmann 
1341f1ae32a1SGerd Hoffmann static void ich9_uhci2_class_init(ObjectClass *klass, void *data)
1342f1ae32a1SGerd Hoffmann {
1343f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1344f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1345f1ae32a1SGerd Hoffmann 
1346f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1347f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1348f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2;
1349f1ae32a1SGerd Hoffmann     k->revision = 0x03;
1350f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1351f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1352f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1353f1ae32a1SGerd Hoffmann }
1354f1ae32a1SGerd Hoffmann 
1355f1ae32a1SGerd Hoffmann static TypeInfo ich9_uhci2_info = {
1356f1ae32a1SGerd Hoffmann     .name          = "ich9-usb-uhci2",
1357f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1358f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1359f1ae32a1SGerd Hoffmann     .class_init    = ich9_uhci2_class_init,
1360f1ae32a1SGerd Hoffmann };
1361f1ae32a1SGerd Hoffmann 
1362f1ae32a1SGerd Hoffmann static void ich9_uhci3_class_init(ObjectClass *klass, void *data)
1363f1ae32a1SGerd Hoffmann {
1364f1ae32a1SGerd Hoffmann     DeviceClass *dc = DEVICE_CLASS(klass);
1365f1ae32a1SGerd Hoffmann     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1366f1ae32a1SGerd Hoffmann 
1367f1ae32a1SGerd Hoffmann     k->init = usb_uhci_common_initfn;
1368f1ae32a1SGerd Hoffmann     k->vendor_id = PCI_VENDOR_ID_INTEL;
1369f1ae32a1SGerd Hoffmann     k->device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3;
1370f1ae32a1SGerd Hoffmann     k->revision = 0x03;
1371f1ae32a1SGerd Hoffmann     k->class_id = PCI_CLASS_SERIAL_USB;
1372f1ae32a1SGerd Hoffmann     dc->vmsd = &vmstate_uhci;
1373f1ae32a1SGerd Hoffmann     dc->props = uhci_properties;
1374f1ae32a1SGerd Hoffmann }
1375f1ae32a1SGerd Hoffmann 
1376f1ae32a1SGerd Hoffmann static TypeInfo ich9_uhci3_info = {
1377f1ae32a1SGerd Hoffmann     .name          = "ich9-usb-uhci3",
1378f1ae32a1SGerd Hoffmann     .parent        = TYPE_PCI_DEVICE,
1379f1ae32a1SGerd Hoffmann     .instance_size = sizeof(UHCIState),
1380f1ae32a1SGerd Hoffmann     .class_init    = ich9_uhci3_class_init,
1381f1ae32a1SGerd Hoffmann };
1382f1ae32a1SGerd Hoffmann 
1383f1ae32a1SGerd Hoffmann static void uhci_register_types(void)
1384f1ae32a1SGerd Hoffmann {
1385f1ae32a1SGerd Hoffmann     type_register_static(&piix3_uhci_info);
1386f1ae32a1SGerd Hoffmann     type_register_static(&piix4_uhci_info);
1387f1ae32a1SGerd Hoffmann     type_register_static(&vt82c686b_uhci_info);
1388f1ae32a1SGerd Hoffmann     type_register_static(&ich9_uhci1_info);
1389f1ae32a1SGerd Hoffmann     type_register_static(&ich9_uhci2_info);
1390f1ae32a1SGerd Hoffmann     type_register_static(&ich9_uhci3_info);
1391f1ae32a1SGerd Hoffmann }
1392f1ae32a1SGerd Hoffmann 
1393f1ae32a1SGerd Hoffmann type_init(uhci_register_types)
1394