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