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