]>
Commit | Line | Data |
---|---|---|
bb36d470 FB |
1 | /* |
2 | * USB UHCI controller emulation | |
5fafdf24 | 3 | * |
bb36d470 | 4 | * Copyright (c) 2005 Fabrice Bellard |
5fafdf24 | 5 | * |
54f254f9 AL |
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 | * | |
bb36d470 FB |
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 | */ | |
87ecb68b PB |
28 | #include "hw.h" |
29 | #include "usb.h" | |
30 | #include "pci.h" | |
31 | #include "qemu-timer.h" | |
18e08a55 | 32 | #include "usb-uhci.h" |
bb36d470 FB |
33 | |
34 | //#define DEBUG | |
54f254f9 | 35 | //#define DEBUG_DUMP_DATA |
bb36d470 | 36 | |
96217e31 TS |
37 | #define UHCI_CMD_FGR (1 << 4) |
38 | #define UHCI_CMD_EGSM (1 << 3) | |
bb36d470 FB |
39 | #define UHCI_CMD_GRESET (1 << 2) |
40 | #define UHCI_CMD_HCRESET (1 << 1) | |
41 | #define UHCI_CMD_RS (1 << 0) | |
42 | ||
43 | #define UHCI_STS_HCHALTED (1 << 5) | |
44 | #define UHCI_STS_HCPERR (1 << 4) | |
45 | #define UHCI_STS_HSERR (1 << 3) | |
46 | #define UHCI_STS_RD (1 << 2) | |
47 | #define UHCI_STS_USBERR (1 << 1) | |
48 | #define UHCI_STS_USBINT (1 << 0) | |
49 | ||
50 | #define TD_CTRL_SPD (1 << 29) | |
51 | #define TD_CTRL_ERROR_SHIFT 27 | |
52 | #define TD_CTRL_IOS (1 << 25) | |
53 | #define TD_CTRL_IOC (1 << 24) | |
54 | #define TD_CTRL_ACTIVE (1 << 23) | |
55 | #define TD_CTRL_STALL (1 << 22) | |
56 | #define TD_CTRL_BABBLE (1 << 20) | |
57 | #define TD_CTRL_NAK (1 << 19) | |
58 | #define TD_CTRL_TIMEOUT (1 << 18) | |
59 | ||
60 | #define UHCI_PORT_RESET (1 << 9) | |
61 | #define UHCI_PORT_LSDA (1 << 8) | |
62 | #define UHCI_PORT_ENC (1 << 3) | |
63 | #define UHCI_PORT_EN (1 << 2) | |
64 | #define UHCI_PORT_CSC (1 << 1) | |
65 | #define UHCI_PORT_CCS (1 << 0) | |
66 | ||
67 | #define FRAME_TIMER_FREQ 1000 | |
68 | ||
69 | #define FRAME_MAX_LOOPS 100 | |
70 | ||
71 | #define NB_PORTS 2 | |
72 | ||
54f254f9 | 73 | #ifdef DEBUG |
d0f2c4c6 | 74 | #define DPRINTF printf |
54f254f9 | 75 | |
0bf9e31a | 76 | static const char *pid2str(int pid) |
54f254f9 AL |
77 | { |
78 | switch (pid) { | |
79 | case USB_TOKEN_SETUP: return "SETUP"; | |
80 | case USB_TOKEN_IN: return "IN"; | |
81 | case USB_TOKEN_OUT: return "OUT"; | |
82 | } | |
83 | return "?"; | |
84 | } | |
85 | ||
86 | #else | |
d0f2c4c6 | 87 | #define DPRINTF(...) |
54f254f9 AL |
88 | #endif |
89 | ||
90 | #ifdef DEBUG_DUMP_DATA | |
91 | static void dump_data(const uint8_t *data, int len) | |
92 | { | |
93 | int i; | |
94 | ||
95 | printf("uhci: data: "); | |
96 | for(i = 0; i < len; i++) | |
97 | printf(" %02x", data[i]); | |
98 | printf("\n"); | |
99 | } | |
100 | #else | |
101 | static void dump_data(const uint8_t *data, int len) {} | |
102 | #endif | |
103 | ||
104 | /* | |
105 | * Pending async transaction. | |
106 | * 'packet' must be the first field because completion | |
107 | * handler does "(UHCIAsync *) pkt" cast. | |
108 | */ | |
109 | typedef struct UHCIAsync { | |
110 | USBPacket packet; | |
111 | struct UHCIAsync *next; | |
112 | uint32_t td; | |
113 | uint32_t token; | |
114 | int8_t valid; | |
8e65b7c0 | 115 | uint8_t isoc; |
54f254f9 AL |
116 | uint8_t done; |
117 | uint8_t buffer[2048]; | |
118 | } UHCIAsync; | |
119 | ||
bb36d470 FB |
120 | typedef struct UHCIPort { |
121 | USBPort port; | |
122 | uint16_t ctrl; | |
bb36d470 FB |
123 | } UHCIPort; |
124 | ||
125 | typedef struct UHCIState { | |
126 | PCIDevice dev; | |
b2317837 | 127 | USBBus bus; |
bb36d470 FB |
128 | uint16_t cmd; /* cmd register */ |
129 | uint16_t status; | |
130 | uint16_t intr; /* interrupt enable register */ | |
131 | uint16_t frnum; /* frame number */ | |
132 | uint32_t fl_base_addr; /* frame list base address */ | |
133 | uint8_t sof_timing; | |
134 | uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */ | |
8e65b7c0 | 135 | int64_t expire_time; |
bb36d470 FB |
136 | QEMUTimer *frame_timer; |
137 | UHCIPort ports[NB_PORTS]; | |
4d611c9a PB |
138 | |
139 | /* Interrupts that should be raised at the end of the current frame. */ | |
140 | uint32_t pending_int_mask; | |
54f254f9 AL |
141 | |
142 | /* Active packets */ | |
143 | UHCIAsync *async_pending; | |
144 | UHCIAsync *async_pool; | |
64e58fe5 | 145 | uint8_t num_ports_vmstate; |
bb36d470 FB |
146 | } UHCIState; |
147 | ||
148 | typedef struct UHCI_TD { | |
149 | uint32_t link; | |
150 | uint32_t ctrl; /* see TD_CTRL_xxx */ | |
151 | uint32_t token; | |
152 | uint32_t buffer; | |
153 | } UHCI_TD; | |
154 | ||
155 | typedef struct UHCI_QH { | |
156 | uint32_t link; | |
157 | uint32_t el_link; | |
158 | } UHCI_QH; | |
159 | ||
54f254f9 AL |
160 | static UHCIAsync *uhci_async_alloc(UHCIState *s) |
161 | { | |
162 | UHCIAsync *async = qemu_malloc(sizeof(UHCIAsync)); | |
487414f1 AL |
163 | |
164 | memset(&async->packet, 0, sizeof(async->packet)); | |
165 | async->valid = 0; | |
166 | async->td = 0; | |
167 | async->token = 0; | |
168 | async->done = 0; | |
8e65b7c0 | 169 | async->isoc = 0; |
487414f1 | 170 | async->next = NULL; |
54f254f9 AL |
171 | |
172 | return async; | |
173 | } | |
174 | ||
175 | static void uhci_async_free(UHCIState *s, UHCIAsync *async) | |
176 | { | |
177 | qemu_free(async); | |
178 | } | |
179 | ||
180 | static void uhci_async_link(UHCIState *s, UHCIAsync *async) | |
181 | { | |
182 | async->next = s->async_pending; | |
183 | s->async_pending = async; | |
184 | } | |
185 | ||
186 | static void uhci_async_unlink(UHCIState *s, UHCIAsync *async) | |
187 | { | |
188 | UHCIAsync *curr = s->async_pending; | |
189 | UHCIAsync **prev = &s->async_pending; | |
190 | ||
191 | while (curr) { | |
192 | if (curr == async) { | |
193 | *prev = curr->next; | |
194 | return; | |
195 | } | |
196 | ||
197 | prev = &curr->next; | |
198 | curr = curr->next; | |
199 | } | |
200 | } | |
201 | ||
202 | static void uhci_async_cancel(UHCIState *s, UHCIAsync *async) | |
203 | { | |
d0f2c4c6 | 204 | DPRINTF("uhci: cancel td 0x%x token 0x%x done %u\n", |
54f254f9 AL |
205 | async->td, async->token, async->done); |
206 | ||
207 | if (!async->done) | |
208 | usb_cancel_packet(&async->packet); | |
209 | uhci_async_free(s, async); | |
210 | } | |
211 | ||
212 | /* | |
213 | * Mark all outstanding async packets as invalid. | |
214 | * This is used for canceling them when TDs are removed by the HCD. | |
215 | */ | |
216 | static UHCIAsync *uhci_async_validate_begin(UHCIState *s) | |
217 | { | |
218 | UHCIAsync *async = s->async_pending; | |
219 | ||
220 | while (async) { | |
221 | async->valid--; | |
222 | async = async->next; | |
223 | } | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | /* | |
228 | * Cancel async packets that are no longer valid | |
229 | */ | |
230 | static void uhci_async_validate_end(UHCIState *s) | |
231 | { | |
232 | UHCIAsync *curr = s->async_pending; | |
233 | UHCIAsync **prev = &s->async_pending; | |
234 | UHCIAsync *next; | |
235 | ||
236 | while (curr) { | |
237 | if (curr->valid > 0) { | |
238 | prev = &curr->next; | |
239 | curr = curr->next; | |
240 | continue; | |
241 | } | |
242 | ||
243 | next = curr->next; | |
244 | ||
245 | /* Unlink */ | |
246 | *prev = next; | |
247 | ||
248 | uhci_async_cancel(s, curr); | |
249 | ||
250 | curr = next; | |
251 | } | |
252 | } | |
253 | ||
254 | static void uhci_async_cancel_all(UHCIState *s) | |
255 | { | |
256 | UHCIAsync *curr = s->async_pending; | |
257 | UHCIAsync *next; | |
258 | ||
259 | while (curr) { | |
260 | next = curr->next; | |
261 | ||
262 | uhci_async_cancel(s, curr); | |
263 | ||
264 | curr = next; | |
265 | } | |
266 | ||
267 | s->async_pending = NULL; | |
268 | } | |
269 | ||
270 | static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t addr, uint32_t token) | |
271 | { | |
272 | UHCIAsync *async = s->async_pending; | |
e8ee3c72 AJ |
273 | UHCIAsync *match = NULL; |
274 | int count = 0; | |
275 | ||
276 | /* | |
277 | * We're looking for the best match here. ie both td addr and token. | |
278 | * Otherwise we return last good match. ie just token. | |
279 | * It's ok to match just token because it identifies the transaction | |
280 | * rather well, token includes: device addr, endpoint, size, etc. | |
281 | * | |
282 | * Also since we queue async transactions in reverse order by returning | |
283 | * last good match we restores the order. | |
284 | * | |
285 | * It's expected that we wont have a ton of outstanding transactions. | |
286 | * If we ever do we'd want to optimize this algorithm. | |
287 | */ | |
54f254f9 AL |
288 | |
289 | while (async) { | |
e8ee3c72 AJ |
290 | if (async->token == token) { |
291 | /* Good match */ | |
292 | match = async; | |
293 | ||
294 | if (async->td == addr) { | |
295 | /* Best match */ | |
296 | break; | |
54f254f9 AL |
297 | } |
298 | } | |
299 | ||
300 | async = async->next; | |
e8ee3c72 | 301 | count++; |
54f254f9 | 302 | } |
e8ee3c72 AJ |
303 | |
304 | if (count > 64) | |
305 | fprintf(stderr, "uhci: warning lots of async transactions\n"); | |
306 | ||
307 | return match; | |
54f254f9 AL |
308 | } |
309 | ||
bb36d470 FB |
310 | static void uhci_attach(USBPort *port1, USBDevice *dev); |
311 | ||
312 | static void uhci_update_irq(UHCIState *s) | |
313 | { | |
314 | int level; | |
315 | if (((s->status2 & 1) && (s->intr & (1 << 2))) || | |
316 | ((s->status2 & 2) && (s->intr & (1 << 3))) || | |
317 | ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) || | |
318 | ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) || | |
319 | (s->status & UHCI_STS_HSERR) || | |
320 | (s->status & UHCI_STS_HCPERR)) { | |
321 | level = 1; | |
322 | } else { | |
323 | level = 0; | |
324 | } | |
d537cf6c | 325 | qemu_set_irq(s->dev.irq[3], level); |
bb36d470 FB |
326 | } |
327 | ||
c8075ac3 | 328 | static void uhci_reset(void *opaque) |
bb36d470 | 329 | { |
c8075ac3 | 330 | UHCIState *s = opaque; |
bb36d470 FB |
331 | uint8_t *pci_conf; |
332 | int i; | |
333 | UHCIPort *port; | |
334 | ||
d0f2c4c6 | 335 | DPRINTF("uhci: full reset\n"); |
6f382b5e | 336 | |
bb36d470 FB |
337 | pci_conf = s->dev.config; |
338 | ||
339 | pci_conf[0x6a] = 0x01; /* usb clock */ | |
340 | pci_conf[0x6b] = 0x00; | |
341 | s->cmd = 0; | |
342 | s->status = 0; | |
343 | s->status2 = 0; | |
344 | s->intr = 0; | |
345 | s->fl_base_addr = 0; | |
346 | s->sof_timing = 64; | |
54f254f9 | 347 | |
bb36d470 FB |
348 | for(i = 0; i < NB_PORTS; i++) { |
349 | port = &s->ports[i]; | |
350 | port->ctrl = 0x0080; | |
a594cfbf FB |
351 | if (port->port.dev) |
352 | uhci_attach(&port->port, port->port.dev); | |
bb36d470 | 353 | } |
54f254f9 AL |
354 | |
355 | uhci_async_cancel_all(s); | |
bb36d470 FB |
356 | } |
357 | ||
817afc61 | 358 | static void uhci_pre_save(void *opaque) |
b9dc033c AZ |
359 | { |
360 | UHCIState *s = opaque; | |
b9dc033c | 361 | |
6f382b5e | 362 | uhci_async_cancel_all(s); |
b9dc033c AZ |
363 | } |
364 | ||
817afc61 JQ |
365 | static const VMStateDescription vmstate_uhci_port = { |
366 | .name = "uhci port", | |
367 | .version_id = 1, | |
368 | .minimum_version_id = 1, | |
369 | .minimum_version_id_old = 1, | |
370 | .fields = (VMStateField []) { | |
371 | VMSTATE_UINT16(ctrl, UHCIPort), | |
372 | VMSTATE_END_OF_LIST() | |
373 | } | |
374 | }; | |
375 | ||
376 | static const VMStateDescription vmstate_uhci = { | |
377 | .name = "uhci", | |
378 | .version_id = 1, | |
379 | .minimum_version_id = 1, | |
380 | .minimum_version_id_old = 1, | |
381 | .pre_save = uhci_pre_save, | |
382 | .fields = (VMStateField []) { | |
383 | VMSTATE_PCI_DEVICE(dev, UHCIState), | |
384 | VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState), | |
385 | VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1, | |
386 | vmstate_uhci_port, UHCIPort), | |
387 | VMSTATE_UINT16(cmd, UHCIState), | |
388 | VMSTATE_UINT16(status, UHCIState), | |
389 | VMSTATE_UINT16(intr, UHCIState), | |
390 | VMSTATE_UINT16(frnum, UHCIState), | |
391 | VMSTATE_UINT32(fl_base_addr, UHCIState), | |
392 | VMSTATE_UINT8(sof_timing, UHCIState), | |
393 | VMSTATE_UINT8(status2, UHCIState), | |
394 | VMSTATE_TIMER(frame_timer, UHCIState), | |
395 | VMSTATE_END_OF_LIST() | |
396 | } | |
397 | }; | |
b9dc033c | 398 | |
bb36d470 FB |
399 | static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val) |
400 | { | |
401 | UHCIState *s = opaque; | |
3b46e624 | 402 | |
bb36d470 FB |
403 | addr &= 0x1f; |
404 | switch(addr) { | |
405 | case 0x0c: | |
406 | s->sof_timing = val; | |
407 | break; | |
408 | } | |
409 | } | |
410 | ||
411 | static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr) | |
412 | { | |
413 | UHCIState *s = opaque; | |
414 | uint32_t val; | |
415 | ||
416 | addr &= 0x1f; | |
417 | switch(addr) { | |
418 | case 0x0c: | |
419 | val = s->sof_timing; | |
d80cfb3f | 420 | break; |
bb36d470 FB |
421 | default: |
422 | val = 0xff; | |
423 | break; | |
424 | } | |
425 | return val; | |
426 | } | |
427 | ||
428 | static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val) | |
429 | { | |
430 | UHCIState *s = opaque; | |
3b46e624 | 431 | |
bb36d470 | 432 | addr &= 0x1f; |
d0f2c4c6 | 433 | DPRINTF("uhci: writew port=0x%04x val=0x%04x\n", addr, val); |
54f254f9 | 434 | |
bb36d470 FB |
435 | switch(addr) { |
436 | case 0x00: | |
437 | if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) { | |
438 | /* start frame processing */ | |
439 | qemu_mod_timer(s->frame_timer, qemu_get_clock(vm_clock)); | |
52328140 | 440 | s->status &= ~UHCI_STS_HCHALTED; |
467d409f | 441 | } else if (!(val & UHCI_CMD_RS)) { |
52328140 | 442 | s->status |= UHCI_STS_HCHALTED; |
bb36d470 FB |
443 | } |
444 | if (val & UHCI_CMD_GRESET) { | |
445 | UHCIPort *port; | |
446 | USBDevice *dev; | |
447 | int i; | |
448 | ||
449 | /* send reset on the USB bus */ | |
450 | for(i = 0; i < NB_PORTS; i++) { | |
451 | port = &s->ports[i]; | |
a594cfbf | 452 | dev = port->port.dev; |
bb36d470 | 453 | if (dev) { |
4d611c9a | 454 | usb_send_msg(dev, USB_MSG_RESET); |
bb36d470 FB |
455 | } |
456 | } | |
457 | uhci_reset(s); | |
458 | return; | |
459 | } | |
5e9ab4c4 | 460 | if (val & UHCI_CMD_HCRESET) { |
bb36d470 FB |
461 | uhci_reset(s); |
462 | return; | |
463 | } | |
464 | s->cmd = val; | |
465 | break; | |
466 | case 0x02: | |
467 | s->status &= ~val; | |
468 | /* XXX: the chip spec is not coherent, so we add a hidden | |
469 | register to distinguish between IOC and SPD */ | |
470 | if (val & UHCI_STS_USBINT) | |
471 | s->status2 = 0; | |
472 | uhci_update_irq(s); | |
473 | break; | |
474 | case 0x04: | |
475 | s->intr = val; | |
476 | uhci_update_irq(s); | |
477 | break; | |
478 | case 0x06: | |
479 | if (s->status & UHCI_STS_HCHALTED) | |
480 | s->frnum = val & 0x7ff; | |
481 | break; | |
482 | case 0x10 ... 0x1f: | |
483 | { | |
484 | UHCIPort *port; | |
485 | USBDevice *dev; | |
486 | int n; | |
487 | ||
488 | n = (addr >> 1) & 7; | |
489 | if (n >= NB_PORTS) | |
490 | return; | |
491 | port = &s->ports[n]; | |
a594cfbf | 492 | dev = port->port.dev; |
bb36d470 FB |
493 | if (dev) { |
494 | /* port reset */ | |
5fafdf24 | 495 | if ( (val & UHCI_PORT_RESET) && |
bb36d470 | 496 | !(port->ctrl & UHCI_PORT_RESET) ) { |
4d611c9a | 497 | usb_send_msg(dev, USB_MSG_RESET); |
bb36d470 FB |
498 | } |
499 | } | |
500 | port->ctrl = (port->ctrl & 0x01fb) | (val & ~0x01fb); | |
501 | /* some bits are reset when a '1' is written to them */ | |
502 | port->ctrl &= ~(val & 0x000a); | |
503 | } | |
504 | break; | |
505 | } | |
506 | } | |
507 | ||
508 | static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr) | |
509 | { | |
510 | UHCIState *s = opaque; | |
511 | uint32_t val; | |
512 | ||
513 | addr &= 0x1f; | |
514 | switch(addr) { | |
515 | case 0x00: | |
516 | val = s->cmd; | |
517 | break; | |
518 | case 0x02: | |
519 | val = s->status; | |
520 | break; | |
521 | case 0x04: | |
522 | val = s->intr; | |
523 | break; | |
524 | case 0x06: | |
525 | val = s->frnum; | |
526 | break; | |
527 | case 0x10 ... 0x1f: | |
528 | { | |
529 | UHCIPort *port; | |
530 | int n; | |
531 | n = (addr >> 1) & 7; | |
5fafdf24 | 532 | if (n >= NB_PORTS) |
bb36d470 FB |
533 | goto read_default; |
534 | port = &s->ports[n]; | |
535 | val = port->ctrl; | |
536 | } | |
537 | break; | |
538 | default: | |
539 | read_default: | |
540 | val = 0xff7f; /* disabled port */ | |
541 | break; | |
542 | } | |
54f254f9 | 543 | |
d0f2c4c6 | 544 | DPRINTF("uhci: readw port=0x%04x val=0x%04x\n", addr, val); |
54f254f9 | 545 | |
bb36d470 FB |
546 | return val; |
547 | } | |
548 | ||
549 | static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val) | |
550 | { | |
551 | UHCIState *s = opaque; | |
552 | ||
553 | addr &= 0x1f; | |
d0f2c4c6 | 554 | DPRINTF("uhci: writel port=0x%04x val=0x%08x\n", addr, val); |
54f254f9 | 555 | |
bb36d470 FB |
556 | switch(addr) { |
557 | case 0x08: | |
558 | s->fl_base_addr = val & ~0xfff; | |
559 | break; | |
560 | } | |
561 | } | |
562 | ||
563 | static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr) | |
564 | { | |
565 | UHCIState *s = opaque; | |
566 | uint32_t val; | |
567 | ||
568 | addr &= 0x1f; | |
569 | switch(addr) { | |
570 | case 0x08: | |
571 | val = s->fl_base_addr; | |
572 | break; | |
573 | default: | |
574 | val = 0xffffffff; | |
575 | break; | |
576 | } | |
577 | return val; | |
578 | } | |
579 | ||
96217e31 TS |
580 | /* signal resume if controller suspended */ |
581 | static void uhci_resume (void *opaque) | |
582 | { | |
583 | UHCIState *s = (UHCIState *)opaque; | |
584 | ||
585 | if (!s) | |
586 | return; | |
587 | ||
588 | if (s->cmd & UHCI_CMD_EGSM) { | |
589 | s->cmd |= UHCI_CMD_FGR; | |
590 | s->status |= UHCI_STS_RD; | |
591 | uhci_update_irq(s); | |
592 | } | |
593 | } | |
594 | ||
bb36d470 FB |
595 | static void uhci_attach(USBPort *port1, USBDevice *dev) |
596 | { | |
597 | UHCIState *s = port1->opaque; | |
598 | UHCIPort *port = &s->ports[port1->index]; | |
599 | ||
600 | if (dev) { | |
a594cfbf | 601 | if (port->port.dev) { |
bb36d470 FB |
602 | usb_attach(port1, NULL); |
603 | } | |
604 | /* set connect status */ | |
61064870 PB |
605 | port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC; |
606 | ||
bb36d470 FB |
607 | /* update speed */ |
608 | if (dev->speed == USB_SPEED_LOW) | |
609 | port->ctrl |= UHCI_PORT_LSDA; | |
610 | else | |
611 | port->ctrl &= ~UHCI_PORT_LSDA; | |
96217e31 TS |
612 | |
613 | uhci_resume(s); | |
614 | ||
a594cfbf | 615 | port->port.dev = dev; |
bb36d470 | 616 | /* send the attach message */ |
4d611c9a | 617 | usb_send_msg(dev, USB_MSG_ATTACH); |
bb36d470 FB |
618 | } else { |
619 | /* set connect status */ | |
61064870 PB |
620 | if (port->ctrl & UHCI_PORT_CCS) { |
621 | port->ctrl &= ~UHCI_PORT_CCS; | |
622 | port->ctrl |= UHCI_PORT_CSC; | |
bb36d470 FB |
623 | } |
624 | /* disable port */ | |
625 | if (port->ctrl & UHCI_PORT_EN) { | |
626 | port->ctrl &= ~UHCI_PORT_EN; | |
627 | port->ctrl |= UHCI_PORT_ENC; | |
628 | } | |
96217e31 TS |
629 | |
630 | uhci_resume(s); | |
631 | ||
a594cfbf | 632 | dev = port->port.dev; |
bb36d470 FB |
633 | if (dev) { |
634 | /* send the detach message */ | |
4d611c9a | 635 | usb_send_msg(dev, USB_MSG_DETACH); |
bb36d470 | 636 | } |
a594cfbf | 637 | port->port.dev = NULL; |
bb36d470 FB |
638 | } |
639 | } | |
640 | ||
4d611c9a | 641 | static int uhci_broadcast_packet(UHCIState *s, USBPacket *p) |
bb36d470 | 642 | { |
bb36d470 FB |
643 | int i, ret; |
644 | ||
d0f2c4c6 | 645 | DPRINTF("uhci: packet enter. pid %s addr 0x%02x ep %d len %d\n", |
54f254f9 | 646 | pid2str(p->pid), p->devaddr, p->devep, p->len); |
5d808245 | 647 | if (p->pid == USB_TOKEN_OUT || p->pid == USB_TOKEN_SETUP) |
54f254f9 AL |
648 | dump_data(p->data, p->len); |
649 | ||
650 | ret = USB_RET_NODEV; | |
651 | for (i = 0; i < NB_PORTS && ret == USB_RET_NODEV; i++) { | |
652 | UHCIPort *port = &s->ports[i]; | |
653 | USBDevice *dev = port->port.dev; | |
654 | ||
655 | if (dev && (port->ctrl & UHCI_PORT_EN)) | |
806b6024 | 656 | ret = dev->info->handle_packet(dev, p); |
bb36d470 | 657 | } |
54f254f9 | 658 | |
d0f2c4c6 | 659 | DPRINTF("uhci: packet exit. ret %d len %d\n", ret, p->len); |
54f254f9 AL |
660 | if (p->pid == USB_TOKEN_IN && ret > 0) |
661 | dump_data(p->data, ret); | |
662 | ||
663 | return ret; | |
bb36d470 FB |
664 | } |
665 | ||
54f254f9 AL |
666 | static void uhci_async_complete(USBPacket * packet, void *opaque); |
667 | static void uhci_process_frame(UHCIState *s); | |
4d611c9a | 668 | |
bb36d470 FB |
669 | /* return -1 if fatal error (frame must be stopped) |
670 | 0 if TD successful | |
671 | 1 if TD unsuccessful or inactive | |
672 | */ | |
54f254f9 | 673 | static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask) |
bb36d470 | 674 | { |
54f254f9 | 675 | int len = 0, max_len, err, ret; |
bb36d470 | 676 | uint8_t pid; |
bb36d470 | 677 | |
54f254f9 AL |
678 | max_len = ((td->token >> 21) + 1) & 0x7ff; |
679 | pid = td->token & 0xff; | |
680 | ||
681 | ret = async->packet.len; | |
682 | ||
54f254f9 AL |
683 | if (td->ctrl & TD_CTRL_IOS) |
684 | td->ctrl &= ~TD_CTRL_ACTIVE; | |
bb36d470 | 685 | |
54f254f9 AL |
686 | if (ret < 0) |
687 | goto out; | |
b9dc033c | 688 | |
54f254f9 AL |
689 | len = async->packet.len; |
690 | td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff); | |
691 | ||
692 | /* The NAK bit may have been set by a previous frame, so clear it | |
693 | here. The docs are somewhat unclear, but win2k relies on this | |
694 | behavior. */ | |
695 | td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK); | |
5bd2c0d7 PB |
696 | if (td->ctrl & TD_CTRL_IOC) |
697 | *int_mask |= 0x01; | |
54f254f9 AL |
698 | |
699 | if (pid == USB_TOKEN_IN) { | |
700 | if (len > max_len) { | |
54f254f9 AL |
701 | ret = USB_RET_BABBLE; |
702 | goto out; | |
4d611c9a | 703 | } |
b9dc033c | 704 | |
54f254f9 AL |
705 | if (len > 0) { |
706 | /* write the data back */ | |
707 | cpu_physical_memory_write(td->buffer, async->buffer, len); | |
708 | } | |
709 | ||
710 | if ((td->ctrl & TD_CTRL_SPD) && len < max_len) { | |
bb36d470 FB |
711 | *int_mask |= 0x02; |
712 | /* short packet: do not update QH */ | |
d0f2c4c6 | 713 | DPRINTF("uhci: short packet. td 0x%x token 0x%x\n", async->td, async->token); |
bb36d470 | 714 | return 1; |
bb36d470 | 715 | } |
54f254f9 AL |
716 | } |
717 | ||
718 | /* success */ | |
719 | return 0; | |
720 | ||
721 | out: | |
722 | switch(ret) { | |
723 | case USB_RET_STALL: | |
724 | td->ctrl |= TD_CTRL_STALL; | |
725 | td->ctrl &= ~TD_CTRL_ACTIVE; | |
726 | return 1; | |
727 | ||
728 | case USB_RET_BABBLE: | |
729 | td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL; | |
730 | td->ctrl &= ~TD_CTRL_ACTIVE; | |
731 | /* frame interrupted */ | |
732 | return -1; | |
733 | ||
734 | case USB_RET_NAK: | |
735 | td->ctrl |= TD_CTRL_NAK; | |
736 | if (pid == USB_TOKEN_SETUP) | |
737 | break; | |
738 | return 1; | |
739 | ||
740 | case USB_RET_NODEV: | |
741 | default: | |
742 | break; | |
743 | } | |
744 | ||
745 | /* Retry the TD if error count is not zero */ | |
746 | ||
747 | td->ctrl |= TD_CTRL_TIMEOUT; | |
748 | err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3; | |
749 | if (err != 0) { | |
750 | err--; | |
751 | if (err == 0) { | |
bb36d470 | 752 | td->ctrl &= ~TD_CTRL_ACTIVE; |
54f254f9 | 753 | s->status |= UHCI_STS_USBERR; |
5bd2c0d7 PB |
754 | if (td->ctrl & TD_CTRL_IOC) |
755 | *int_mask |= 0x01; | |
54f254f9 | 756 | uhci_update_irq(s); |
bb36d470 FB |
757 | } |
758 | } | |
54f254f9 AL |
759 | td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) | |
760 | (err << TD_CTRL_ERROR_SHIFT); | |
761 | return 1; | |
bb36d470 FB |
762 | } |
763 | ||
54f254f9 AL |
764 | static int uhci_handle_td(UHCIState *s, uint32_t addr, UHCI_TD *td, uint32_t *int_mask) |
765 | { | |
766 | UHCIAsync *async; | |
5d808245 | 767 | int len = 0, max_len; |
8e65b7c0 DA |
768 | uint8_t pid, isoc; |
769 | uint32_t token; | |
54f254f9 AL |
770 | |
771 | /* Is active ? */ | |
772 | if (!(td->ctrl & TD_CTRL_ACTIVE)) | |
773 | return 1; | |
774 | ||
8e65b7c0 DA |
775 | /* token field is not unique for isochronous requests, |
776 | * so use the destination buffer | |
777 | */ | |
778 | if (td->ctrl & TD_CTRL_IOS) { | |
779 | token = td->buffer; | |
780 | isoc = 1; | |
781 | } else { | |
782 | token = td->token; | |
783 | isoc = 0; | |
784 | } | |
785 | ||
786 | async = uhci_async_find_td(s, addr, token); | |
54f254f9 AL |
787 | if (async) { |
788 | /* Already submitted */ | |
a145ea51 | 789 | async->valid = 32; |
54f254f9 AL |
790 | |
791 | if (!async->done) | |
792 | return 1; | |
793 | ||
794 | uhci_async_unlink(s, async); | |
795 | goto done; | |
796 | } | |
797 | ||
798 | /* Allocate new packet */ | |
799 | async = uhci_async_alloc(s); | |
800 | if (!async) | |
801 | return 1; | |
802 | ||
8e65b7c0 DA |
803 | /* valid needs to be large enough to handle 10 frame delay |
804 | * for initial isochronous requests | |
805 | */ | |
806 | async->valid = 32; | |
54f254f9 | 807 | async->td = addr; |
8e65b7c0 DA |
808 | async->token = token; |
809 | async->isoc = isoc; | |
54f254f9 AL |
810 | |
811 | max_len = ((td->token >> 21) + 1) & 0x7ff; | |
812 | pid = td->token & 0xff; | |
813 | ||
814 | async->packet.pid = pid; | |
815 | async->packet.devaddr = (td->token >> 8) & 0x7f; | |
816 | async->packet.devep = (td->token >> 15) & 0xf; | |
817 | async->packet.data = async->buffer; | |
818 | async->packet.len = max_len; | |
819 | async->packet.complete_cb = uhci_async_complete; | |
820 | async->packet.complete_opaque = s; | |
821 | ||
822 | switch(pid) { | |
823 | case USB_TOKEN_OUT: | |
824 | case USB_TOKEN_SETUP: | |
825 | cpu_physical_memory_read(td->buffer, async->buffer, max_len); | |
5d808245 AJ |
826 | len = uhci_broadcast_packet(s, &async->packet); |
827 | if (len >= 0) | |
828 | len = max_len; | |
54f254f9 AL |
829 | break; |
830 | ||
831 | case USB_TOKEN_IN: | |
5d808245 | 832 | len = uhci_broadcast_packet(s, &async->packet); |
54f254f9 AL |
833 | break; |
834 | ||
835 | default: | |
836 | /* invalid pid : frame interrupted */ | |
837 | uhci_async_free(s, async); | |
838 | s->status |= UHCI_STS_HCPERR; | |
839 | uhci_update_irq(s); | |
840 | return -1; | |
841 | } | |
842 | ||
5d808245 | 843 | if (len == USB_RET_ASYNC) { |
54f254f9 AL |
844 | uhci_async_link(s, async); |
845 | return 2; | |
846 | } | |
847 | ||
5d808245 | 848 | async->packet.len = len; |
54f254f9 AL |
849 | |
850 | done: | |
5d808245 | 851 | len = uhci_complete_td(s, td, async, int_mask); |
54f254f9 | 852 | uhci_async_free(s, async); |
5d808245 | 853 | return len; |
54f254f9 AL |
854 | } |
855 | ||
856 | static void uhci_async_complete(USBPacket *packet, void *opaque) | |
4d611c9a PB |
857 | { |
858 | UHCIState *s = opaque; | |
54f254f9 AL |
859 | UHCIAsync *async = (UHCIAsync *) packet; |
860 | ||
d0f2c4c6 | 861 | DPRINTF("uhci: async complete. td 0x%x token 0x%x\n", async->td, async->token); |
54f254f9 | 862 | |
8e65b7c0 DA |
863 | if (async->isoc) { |
864 | UHCI_TD td; | |
865 | uint32_t link = async->td; | |
866 | uint32_t int_mask = 0, val; | |
d4c4e6fd | 867 | |
8e65b7c0 DA |
868 | cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td)); |
869 | le32_to_cpus(&td.link); | |
870 | le32_to_cpus(&td.ctrl); | |
871 | le32_to_cpus(&td.token); | |
872 | le32_to_cpus(&td.buffer); | |
873 | ||
874 | uhci_async_unlink(s, async); | |
d4c4e6fd | 875 | uhci_complete_td(s, &td, async, &int_mask); |
8e65b7c0 | 876 | s->pending_int_mask |= int_mask; |
54f254f9 | 877 | |
8e65b7c0 DA |
878 | /* update the status bits of the TD */ |
879 | val = cpu_to_le32(td.ctrl); | |
880 | cpu_physical_memory_write((link & ~0xf) + 4, | |
881 | (const uint8_t *)&val, sizeof(val)); | |
882 | uhci_async_free(s, async); | |
883 | } else { | |
884 | async->done = 1; | |
885 | uhci_process_frame(s); | |
886 | } | |
54f254f9 AL |
887 | } |
888 | ||
889 | static int is_valid(uint32_t link) | |
890 | { | |
891 | return (link & 1) == 0; | |
892 | } | |
893 | ||
894 | static int is_qh(uint32_t link) | |
895 | { | |
896 | return (link & 2) != 0; | |
897 | } | |
898 | ||
899 | static int depth_first(uint32_t link) | |
900 | { | |
901 | return (link & 4) != 0; | |
902 | } | |
903 | ||
904 | /* QH DB used for detecting QH loops */ | |
905 | #define UHCI_MAX_QUEUES 128 | |
906 | typedef struct { | |
907 | uint32_t addr[UHCI_MAX_QUEUES]; | |
908 | int count; | |
909 | } QhDb; | |
910 | ||
911 | static void qhdb_reset(QhDb *db) | |
912 | { | |
913 | db->count = 0; | |
914 | } | |
915 | ||
916 | /* Add QH to DB. Returns 1 if already present or DB is full. */ | |
917 | static int qhdb_insert(QhDb *db, uint32_t addr) | |
918 | { | |
919 | int i; | |
920 | for (i = 0; i < db->count; i++) | |
921 | if (db->addr[i] == addr) | |
922 | return 1; | |
923 | ||
924 | if (db->count >= UHCI_MAX_QUEUES) | |
925 | return 1; | |
926 | ||
927 | db->addr[db->count++] = addr; | |
928 | return 0; | |
929 | } | |
930 | ||
931 | static void uhci_process_frame(UHCIState *s) | |
932 | { | |
933 | uint32_t frame_addr, link, old_td_ctrl, val, int_mask; | |
934 | uint32_t curr_qh; | |
935 | int cnt, ret; | |
4d611c9a | 936 | UHCI_TD td; |
54f254f9 AL |
937 | UHCI_QH qh; |
938 | QhDb qhdb; | |
4d611c9a | 939 | |
54f254f9 AL |
940 | frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2); |
941 | ||
d0f2c4c6 | 942 | DPRINTF("uhci: processing frame %d addr 0x%x\n" , s->frnum, frame_addr); |
54f254f9 AL |
943 | |
944 | cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4); | |
945 | le32_to_cpus(&link); | |
b9dc033c | 946 | |
54f254f9 AL |
947 | int_mask = 0; |
948 | curr_qh = 0; | |
949 | ||
950 | qhdb_reset(&qhdb); | |
951 | ||
952 | for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) { | |
953 | if (is_qh(link)) { | |
954 | /* QH */ | |
955 | ||
956 | if (qhdb_insert(&qhdb, link)) { | |
957 | /* | |
958 | * We're going in circles. Which is not a bug because | |
959 | * HCD is allowed to do that as part of the BW management. | |
960 | * In our case though it makes no sense to spin here. Sync transations | |
961 | * are already done, and async completion handler will re-process | |
962 | * the frame when something is ready. | |
963 | */ | |
d0f2c4c6 | 964 | DPRINTF("uhci: detected loop. qh 0x%x\n", link); |
54f254f9 AL |
965 | break; |
966 | } | |
967 | ||
968 | cpu_physical_memory_read(link & ~0xf, (uint8_t *) &qh, sizeof(qh)); | |
969 | le32_to_cpus(&qh.link); | |
970 | le32_to_cpus(&qh.el_link); | |
971 | ||
d0f2c4c6 | 972 | DPRINTF("uhci: QH 0x%x load. link 0x%x elink 0x%x\n", |
54f254f9 AL |
973 | link, qh.link, qh.el_link); |
974 | ||
975 | if (!is_valid(qh.el_link)) { | |
976 | /* QH w/o elements */ | |
977 | curr_qh = 0; | |
978 | link = qh.link; | |
979 | } else { | |
980 | /* QH with elements */ | |
981 | curr_qh = link; | |
982 | link = qh.el_link; | |
983 | } | |
984 | continue; | |
985 | } | |
986 | ||
987 | /* TD */ | |
988 | cpu_physical_memory_read(link & ~0xf, (uint8_t *) &td, sizeof(td)); | |
b9dc033c AZ |
989 | le32_to_cpus(&td.link); |
990 | le32_to_cpus(&td.ctrl); | |
991 | le32_to_cpus(&td.token); | |
992 | le32_to_cpus(&td.buffer); | |
b9dc033c | 993 | |
d0f2c4c6 | 994 | DPRINTF("uhci: TD 0x%x load. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n", |
54f254f9 AL |
995 | link, td.link, td.ctrl, td.token, curr_qh); |
996 | ||
997 | old_td_ctrl = td.ctrl; | |
998 | ret = uhci_handle_td(s, link, &td, &int_mask); | |
b9dc033c | 999 | if (old_td_ctrl != td.ctrl) { |
54f254f9 | 1000 | /* update the status bits of the TD */ |
b9dc033c AZ |
1001 | val = cpu_to_le32(td.ctrl); |
1002 | cpu_physical_memory_write((link & ~0xf) + 4, | |
54f254f9 | 1003 | (const uint8_t *)&val, sizeof(val)); |
b9dc033c | 1004 | } |
54f254f9 AL |
1005 | |
1006 | if (ret < 0) { | |
1007 | /* interrupted frame */ | |
1008 | break; | |
b9dc033c | 1009 | } |
b9dc033c | 1010 | |
54f254f9 | 1011 | if (ret == 2 || ret == 1) { |
d0f2c4c6 | 1012 | DPRINTF("uhci: TD 0x%x %s. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n", |
54f254f9 AL |
1013 | link, ret == 2 ? "pend" : "skip", |
1014 | td.link, td.ctrl, td.token, curr_qh); | |
b9dc033c | 1015 | |
54f254f9 AL |
1016 | link = curr_qh ? qh.link : td.link; |
1017 | continue; | |
4d611c9a | 1018 | } |
54f254f9 AL |
1019 | |
1020 | /* completed TD */ | |
1021 | ||
d0f2c4c6 | 1022 | DPRINTF("uhci: TD 0x%x done. link 0x%x ctrl 0x%x token 0x%x qh 0x%x\n", |
54f254f9 AL |
1023 | link, td.link, td.ctrl, td.token, curr_qh); |
1024 | ||
1025 | link = td.link; | |
1026 | ||
1027 | if (curr_qh) { | |
1028 | /* update QH element link */ | |
1029 | qh.el_link = link; | |
4d611c9a | 1030 | val = cpu_to_le32(qh.el_link); |
54f254f9 AL |
1031 | cpu_physical_memory_write((curr_qh & ~0xf) + 4, |
1032 | (const uint8_t *)&val, sizeof(val)); | |
1033 | ||
1034 | if (!depth_first(link)) { | |
1035 | /* done with this QH */ | |
1036 | ||
d0f2c4c6 | 1037 | DPRINTF("uhci: QH 0x%x done. link 0x%x elink 0x%x\n", |
54f254f9 AL |
1038 | curr_qh, qh.link, qh.el_link); |
1039 | ||
1040 | curr_qh = 0; | |
1041 | link = qh.link; | |
1042 | } | |
4d611c9a | 1043 | } |
54f254f9 AL |
1044 | |
1045 | /* go to the next entry */ | |
4d611c9a | 1046 | } |
54f254f9 | 1047 | |
8e65b7c0 | 1048 | s->pending_int_mask |= int_mask; |
4d611c9a PB |
1049 | } |
1050 | ||
bb36d470 FB |
1051 | static void uhci_frame_timer(void *opaque) |
1052 | { | |
1053 | UHCIState *s = opaque; | |
8e65b7c0 DA |
1054 | |
1055 | /* prepare the timer for the next frame */ | |
1056 | s->expire_time += (get_ticks_per_sec() / FRAME_TIMER_FREQ); | |
bb36d470 FB |
1057 | |
1058 | if (!(s->cmd & UHCI_CMD_RS)) { | |
54f254f9 | 1059 | /* Full stop */ |
bb36d470 | 1060 | qemu_del_timer(s->frame_timer); |
52328140 FB |
1061 | /* set hchalted bit in status - UHCI11D 2.1.2 */ |
1062 | s->status |= UHCI_STS_HCHALTED; | |
6f382b5e | 1063 | |
d0f2c4c6 | 1064 | DPRINTF("uhci: halted\n"); |
bb36d470 FB |
1065 | return; |
1066 | } | |
54f254f9 AL |
1067 | |
1068 | /* Complete the previous frame */ | |
4d611c9a PB |
1069 | if (s->pending_int_mask) { |
1070 | s->status2 |= s->pending_int_mask; | |
54f254f9 | 1071 | s->status |= UHCI_STS_USBINT; |
4d611c9a PB |
1072 | uhci_update_irq(s); |
1073 | } | |
8e65b7c0 | 1074 | s->pending_int_mask = 0; |
b9dc033c | 1075 | |
54f254f9 AL |
1076 | /* Start new frame */ |
1077 | s->frnum = (s->frnum + 1) & 0x7ff; | |
1078 | ||
d0f2c4c6 | 1079 | DPRINTF("uhci: new frame #%u\n" , s->frnum); |
54f254f9 AL |
1080 | |
1081 | uhci_async_validate_begin(s); | |
1082 | ||
1083 | uhci_process_frame(s); | |
1084 | ||
1085 | uhci_async_validate_end(s); | |
b9dc033c | 1086 | |
8e65b7c0 | 1087 | qemu_mod_timer(s->frame_timer, s->expire_time); |
bb36d470 FB |
1088 | } |
1089 | ||
5fafdf24 | 1090 | static void uhci_map(PCIDevice *pci_dev, int region_num, |
6e355d90 | 1091 | pcibus_t addr, pcibus_t size, int type) |
bb36d470 FB |
1092 | { |
1093 | UHCIState *s = (UHCIState *)pci_dev; | |
1094 | ||
1095 | register_ioport_write(addr, 32, 2, uhci_ioport_writew, s); | |
1096 | register_ioport_read(addr, 32, 2, uhci_ioport_readw, s); | |
1097 | register_ioport_write(addr, 32, 4, uhci_ioport_writel, s); | |
1098 | register_ioport_read(addr, 32, 4, uhci_ioport_readl, s); | |
1099 | register_ioport_write(addr, 32, 1, uhci_ioport_writeb, s); | |
1100 | register_ioport_read(addr, 32, 1, uhci_ioport_readb, s); | |
1101 | } | |
1102 | ||
6cf9b6f1 | 1103 | static int usb_uhci_common_initfn(UHCIState *s) |
bb36d470 | 1104 | { |
6cf9b6f1 | 1105 | uint8_t *pci_conf = s->dev.config; |
bb36d470 FB |
1106 | int i; |
1107 | ||
db579e9e MT |
1108 | pci_conf[PCI_REVISION_ID] = 0x01; // revision number |
1109 | pci_conf[PCI_CLASS_PROG] = 0x00; | |
173a543b | 1110 | pci_config_set_class(pci_conf, PCI_CLASS_SERIAL_USB); |
6407f373 | 1111 | pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type |
db579e9e MT |
1112 | /* TODO: reset value should be 0. */ |
1113 | pci_conf[PCI_INTERRUPT_PIN] = 4; // interrupt pin 3 | |
38ca0f6d | 1114 | pci_conf[0x60] = 0x10; // release number |
3b46e624 | 1115 | |
b2317837 | 1116 | usb_bus_new(&s->bus, &s->dev.qdev); |
bb36d470 | 1117 | for(i = 0; i < NB_PORTS; i++) { |
b2317837 | 1118 | usb_register_port(&s->bus, &s->ports[i].port, s, i, uhci_attach); |
bb36d470 FB |
1119 | } |
1120 | s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s); | |
8e65b7c0 DA |
1121 | s->expire_time = qemu_get_clock(vm_clock) + |
1122 | (get_ticks_per_sec() / FRAME_TIMER_FREQ); | |
64e58fe5 | 1123 | s->num_ports_vmstate = NB_PORTS; |
bb36d470 | 1124 | |
a08d4367 | 1125 | qemu_register_reset(uhci_reset, s); |
bb36d470 | 1126 | |
38ca0f6d PB |
1127 | /* Use region 4 for consistency with real hardware. BSD guests seem |
1128 | to rely on this. */ | |
28c2c264 | 1129 | pci_register_bar(&s->dev, 4, 0x20, |
0392a017 | 1130 | PCI_BASE_ADDRESS_SPACE_IO, uhci_map); |
6f382b5e | 1131 | |
6cf9b6f1 | 1132 | return 0; |
bb36d470 | 1133 | } |
afcc3cdf | 1134 | |
6cf9b6f1 | 1135 | static int usb_uhci_piix3_initfn(PCIDevice *dev) |
afcc3cdf | 1136 | { |
6cf9b6f1 GH |
1137 | UHCIState *s = DO_UPCAST(UHCIState, dev, dev); |
1138 | uint8_t *pci_conf = s->dev.config; | |
1139 | ||
1140 | pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL); | |
1141 | pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371SB_2); | |
1142 | return usb_uhci_common_initfn(s); | |
1143 | } | |
1144 | ||
1145 | static int usb_uhci_piix4_initfn(PCIDevice *dev) | |
1146 | { | |
1147 | UHCIState *s = DO_UPCAST(UHCIState, dev, dev); | |
1148 | uint8_t *pci_conf = s->dev.config; | |
afcc3cdf | 1149 | |
deb54399 AL |
1150 | pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL); |
1151 | pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_2); | |
6cf9b6f1 GH |
1152 | return usb_uhci_common_initfn(s); |
1153 | } | |
afcc3cdf | 1154 | |
6cf9b6f1 GH |
1155 | static PCIDeviceInfo uhci_info[] = { |
1156 | { | |
556cd098 | 1157 | .qdev.name = "piix3-usb-uhci", |
6cf9b6f1 | 1158 | .qdev.size = sizeof(UHCIState), |
be73cfe2 | 1159 | .qdev.vmsd = &vmstate_uhci, |
6cf9b6f1 GH |
1160 | .init = usb_uhci_piix3_initfn, |
1161 | },{ | |
556cd098 | 1162 | .qdev.name = "piix4-usb-uhci", |
6cf9b6f1 | 1163 | .qdev.size = sizeof(UHCIState), |
be73cfe2 | 1164 | .qdev.vmsd = &vmstate_uhci, |
6cf9b6f1 GH |
1165 | .init = usb_uhci_piix4_initfn, |
1166 | },{ | |
1167 | /* end of list */ | |
afcc3cdf | 1168 | } |
6cf9b6f1 | 1169 | }; |
afcc3cdf | 1170 | |
6cf9b6f1 GH |
1171 | static void uhci_register(void) |
1172 | { | |
1173 | pci_qdev_register_many(uhci_info); | |
1174 | } | |
1175 | device_init(uhci_register); | |
afcc3cdf | 1176 | |
6cf9b6f1 GH |
1177 | void usb_uhci_piix3_init(PCIBus *bus, int devfn) |
1178 | { | |
556cd098 | 1179 | pci_create_simple(bus, devfn, "piix3-usb-uhci"); |
6cf9b6f1 | 1180 | } |
54f254f9 | 1181 | |
6cf9b6f1 GH |
1182 | void usb_uhci_piix4_init(PCIBus *bus, int devfn) |
1183 | { | |
556cd098 | 1184 | pci_create_simple(bus, devfn, "piix4-usb-uhci"); |
afcc3cdf | 1185 | } |