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