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