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