root / hw / usb-uhci.c @ d861b05e
History | View | Annotate | Download (19.2 kB)
1 | bb36d470 | bellard | /*
|
---|---|---|---|
2 | bb36d470 | bellard | * USB UHCI controller emulation
|
3 | bb36d470 | bellard | *
|
4 | bb36d470 | bellard | * Copyright (c) 2005 Fabrice Bellard
|
5 | bb36d470 | bellard | *
|
6 | bb36d470 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | bb36d470 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | bb36d470 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | bb36d470 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | bb36d470 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | bb36d470 | bellard | * furnished to do so, subject to the following conditions:
|
12 | bb36d470 | bellard | *
|
13 | bb36d470 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | bb36d470 | bellard | * all copies or substantial portions of the Software.
|
15 | bb36d470 | bellard | *
|
16 | bb36d470 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | bb36d470 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | bb36d470 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | bb36d470 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | bb36d470 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | bb36d470 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | bb36d470 | bellard | * THE SOFTWARE.
|
23 | bb36d470 | bellard | */
|
24 | bb36d470 | bellard | #include "vl.h" |
25 | bb36d470 | bellard | |
26 | bb36d470 | bellard | //#define DEBUG
|
27 | bb36d470 | bellard | //#define DEBUG_PACKET
|
28 | bb36d470 | bellard | |
29 | bb36d470 | bellard | #define UHCI_CMD_GRESET (1 << 2) |
30 | bb36d470 | bellard | #define UHCI_CMD_HCRESET (1 << 1) |
31 | bb36d470 | bellard | #define UHCI_CMD_RS (1 << 0) |
32 | bb36d470 | bellard | |
33 | bb36d470 | bellard | #define UHCI_STS_HCHALTED (1 << 5) |
34 | bb36d470 | bellard | #define UHCI_STS_HCPERR (1 << 4) |
35 | bb36d470 | bellard | #define UHCI_STS_HSERR (1 << 3) |
36 | bb36d470 | bellard | #define UHCI_STS_RD (1 << 2) |
37 | bb36d470 | bellard | #define UHCI_STS_USBERR (1 << 1) |
38 | bb36d470 | bellard | #define UHCI_STS_USBINT (1 << 0) |
39 | bb36d470 | bellard | |
40 | bb36d470 | bellard | #define TD_CTRL_SPD (1 << 29) |
41 | bb36d470 | bellard | #define TD_CTRL_ERROR_SHIFT 27 |
42 | bb36d470 | bellard | #define TD_CTRL_IOS (1 << 25) |
43 | bb36d470 | bellard | #define TD_CTRL_IOC (1 << 24) |
44 | bb36d470 | bellard | #define TD_CTRL_ACTIVE (1 << 23) |
45 | bb36d470 | bellard | #define TD_CTRL_STALL (1 << 22) |
46 | bb36d470 | bellard | #define TD_CTRL_BABBLE (1 << 20) |
47 | bb36d470 | bellard | #define TD_CTRL_NAK (1 << 19) |
48 | bb36d470 | bellard | #define TD_CTRL_TIMEOUT (1 << 18) |
49 | bb36d470 | bellard | |
50 | bb36d470 | bellard | #define UHCI_PORT_RESET (1 << 9) |
51 | bb36d470 | bellard | #define UHCI_PORT_LSDA (1 << 8) |
52 | bb36d470 | bellard | #define UHCI_PORT_ENC (1 << 3) |
53 | bb36d470 | bellard | #define UHCI_PORT_EN (1 << 2) |
54 | bb36d470 | bellard | #define UHCI_PORT_CSC (1 << 1) |
55 | bb36d470 | bellard | #define UHCI_PORT_CCS (1 << 0) |
56 | bb36d470 | bellard | |
57 | bb36d470 | bellard | #define FRAME_TIMER_FREQ 1000 |
58 | bb36d470 | bellard | |
59 | bb36d470 | bellard | #define FRAME_MAX_LOOPS 100 |
60 | bb36d470 | bellard | |
61 | bb36d470 | bellard | #define NB_PORTS 2 |
62 | bb36d470 | bellard | |
63 | bb36d470 | bellard | typedef struct UHCIPort { |
64 | bb36d470 | bellard | USBPort port; |
65 | bb36d470 | bellard | uint16_t ctrl; |
66 | bb36d470 | bellard | } UHCIPort; |
67 | bb36d470 | bellard | |
68 | bb36d470 | bellard | typedef struct UHCIState { |
69 | bb36d470 | bellard | PCIDevice dev; |
70 | bb36d470 | bellard | uint16_t cmd; /* cmd register */
|
71 | bb36d470 | bellard | uint16_t status; |
72 | bb36d470 | bellard | uint16_t intr; /* interrupt enable register */
|
73 | bb36d470 | bellard | uint16_t frnum; /* frame number */
|
74 | bb36d470 | bellard | uint32_t fl_base_addr; /* frame list base address */
|
75 | bb36d470 | bellard | uint8_t sof_timing; |
76 | bb36d470 | bellard | uint8_t status2; /* bit 0 and 1 are used to generate UHCI_STS_USBINT */
|
77 | bb36d470 | bellard | QEMUTimer *frame_timer; |
78 | bb36d470 | bellard | UHCIPort ports[NB_PORTS]; |
79 | bb36d470 | bellard | } UHCIState; |
80 | bb36d470 | bellard | |
81 | bb36d470 | bellard | typedef struct UHCI_TD { |
82 | bb36d470 | bellard | uint32_t link; |
83 | bb36d470 | bellard | uint32_t ctrl; /* see TD_CTRL_xxx */
|
84 | bb36d470 | bellard | uint32_t token; |
85 | bb36d470 | bellard | uint32_t buffer; |
86 | bb36d470 | bellard | } UHCI_TD; |
87 | bb36d470 | bellard | |
88 | bb36d470 | bellard | typedef struct UHCI_QH { |
89 | bb36d470 | bellard | uint32_t link; |
90 | bb36d470 | bellard | uint32_t el_link; |
91 | bb36d470 | bellard | } UHCI_QH; |
92 | bb36d470 | bellard | |
93 | bb36d470 | bellard | static void uhci_attach(USBPort *port1, USBDevice *dev); |
94 | bb36d470 | bellard | |
95 | bb36d470 | bellard | static void uhci_update_irq(UHCIState *s) |
96 | bb36d470 | bellard | { |
97 | bb36d470 | bellard | int level;
|
98 | bb36d470 | bellard | if (((s->status2 & 1) && (s->intr & (1 << 2))) || |
99 | bb36d470 | bellard | ((s->status2 & 2) && (s->intr & (1 << 3))) || |
100 | bb36d470 | bellard | ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) || |
101 | bb36d470 | bellard | ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) || |
102 | bb36d470 | bellard | (s->status & UHCI_STS_HSERR) || |
103 | bb36d470 | bellard | (s->status & UHCI_STS_HCPERR)) { |
104 | bb36d470 | bellard | level = 1;
|
105 | bb36d470 | bellard | } else {
|
106 | bb36d470 | bellard | level = 0;
|
107 | bb36d470 | bellard | } |
108 | f04308e4 | bellard | pci_set_irq(&s->dev, 3, level);
|
109 | bb36d470 | bellard | } |
110 | bb36d470 | bellard | |
111 | bb36d470 | bellard | static void uhci_reset(UHCIState *s) |
112 | bb36d470 | bellard | { |
113 | bb36d470 | bellard | uint8_t *pci_conf; |
114 | bb36d470 | bellard | int i;
|
115 | bb36d470 | bellard | UHCIPort *port; |
116 | bb36d470 | bellard | |
117 | bb36d470 | bellard | pci_conf = s->dev.config; |
118 | bb36d470 | bellard | |
119 | bb36d470 | bellard | pci_conf[0x6a] = 0x01; /* usb clock */ |
120 | bb36d470 | bellard | pci_conf[0x6b] = 0x00; |
121 | bb36d470 | bellard | s->cmd = 0;
|
122 | bb36d470 | bellard | s->status = 0;
|
123 | bb36d470 | bellard | s->status2 = 0;
|
124 | bb36d470 | bellard | s->intr = 0;
|
125 | bb36d470 | bellard | s->fl_base_addr = 0;
|
126 | bb36d470 | bellard | s->sof_timing = 64;
|
127 | bb36d470 | bellard | for(i = 0; i < NB_PORTS; i++) { |
128 | bb36d470 | bellard | port = &s->ports[i]; |
129 | bb36d470 | bellard | port->ctrl = 0x0080;
|
130 | a594cfbf | bellard | if (port->port.dev)
|
131 | a594cfbf | bellard | uhci_attach(&port->port, port->port.dev); |
132 | bb36d470 | bellard | } |
133 | bb36d470 | bellard | } |
134 | bb36d470 | bellard | |
135 | bb36d470 | bellard | static void uhci_ioport_writeb(void *opaque, uint32_t addr, uint32_t val) |
136 | bb36d470 | bellard | { |
137 | bb36d470 | bellard | UHCIState *s = opaque; |
138 | bb36d470 | bellard | |
139 | bb36d470 | bellard | addr &= 0x1f;
|
140 | bb36d470 | bellard | switch(addr) {
|
141 | bb36d470 | bellard | case 0x0c: |
142 | bb36d470 | bellard | s->sof_timing = val; |
143 | bb36d470 | bellard | break;
|
144 | bb36d470 | bellard | } |
145 | bb36d470 | bellard | } |
146 | bb36d470 | bellard | |
147 | bb36d470 | bellard | static uint32_t uhci_ioport_readb(void *opaque, uint32_t addr) |
148 | bb36d470 | bellard | { |
149 | bb36d470 | bellard | UHCIState *s = opaque; |
150 | bb36d470 | bellard | uint32_t val; |
151 | bb36d470 | bellard | |
152 | bb36d470 | bellard | addr &= 0x1f;
|
153 | bb36d470 | bellard | switch(addr) {
|
154 | bb36d470 | bellard | case 0x0c: |
155 | bb36d470 | bellard | val = s->sof_timing; |
156 | bb36d470 | bellard | default:
|
157 | bb36d470 | bellard | val = 0xff;
|
158 | bb36d470 | bellard | break;
|
159 | bb36d470 | bellard | } |
160 | bb36d470 | bellard | return val;
|
161 | bb36d470 | bellard | } |
162 | bb36d470 | bellard | |
163 | bb36d470 | bellard | static void uhci_ioport_writew(void *opaque, uint32_t addr, uint32_t val) |
164 | bb36d470 | bellard | { |
165 | bb36d470 | bellard | UHCIState *s = opaque; |
166 | bb36d470 | bellard | |
167 | bb36d470 | bellard | addr &= 0x1f;
|
168 | bb36d470 | bellard | #ifdef DEBUG
|
169 | bb36d470 | bellard | printf("uhci writew port=0x%04x val=0x%04x\n", addr, val);
|
170 | bb36d470 | bellard | #endif
|
171 | bb36d470 | bellard | switch(addr) {
|
172 | bb36d470 | bellard | case 0x00: |
173 | bb36d470 | bellard | if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
|
174 | bb36d470 | bellard | /* start frame processing */
|
175 | bb36d470 | bellard | qemu_mod_timer(s->frame_timer, qemu_get_clock(vm_clock)); |
176 | bb36d470 | bellard | } |
177 | bb36d470 | bellard | if (val & UHCI_CMD_GRESET) {
|
178 | bb36d470 | bellard | UHCIPort *port; |
179 | bb36d470 | bellard | USBDevice *dev; |
180 | bb36d470 | bellard | int i;
|
181 | bb36d470 | bellard | |
182 | bb36d470 | bellard | /* send reset on the USB bus */
|
183 | bb36d470 | bellard | for(i = 0; i < NB_PORTS; i++) { |
184 | bb36d470 | bellard | port = &s->ports[i]; |
185 | a594cfbf | bellard | dev = port->port.dev; |
186 | bb36d470 | bellard | if (dev) {
|
187 | bb36d470 | bellard | dev->handle_packet(dev, |
188 | bb36d470 | bellard | USB_MSG_RESET, 0, 0, NULL, 0); |
189 | bb36d470 | bellard | } |
190 | bb36d470 | bellard | } |
191 | bb36d470 | bellard | uhci_reset(s); |
192 | bb36d470 | bellard | return;
|
193 | bb36d470 | bellard | } |
194 | 5e9ab4c4 | bellard | if (val & UHCI_CMD_HCRESET) {
|
195 | bb36d470 | bellard | uhci_reset(s); |
196 | bb36d470 | bellard | return;
|
197 | bb36d470 | bellard | } |
198 | bb36d470 | bellard | s->cmd = val; |
199 | bb36d470 | bellard | break;
|
200 | bb36d470 | bellard | case 0x02: |
201 | bb36d470 | bellard | s->status &= ~val; |
202 | bb36d470 | bellard | /* XXX: the chip spec is not coherent, so we add a hidden
|
203 | bb36d470 | bellard | register to distinguish between IOC and SPD */
|
204 | bb36d470 | bellard | if (val & UHCI_STS_USBINT)
|
205 | bb36d470 | bellard | s->status2 = 0;
|
206 | bb36d470 | bellard | uhci_update_irq(s); |
207 | bb36d470 | bellard | break;
|
208 | bb36d470 | bellard | case 0x04: |
209 | bb36d470 | bellard | s->intr = val; |
210 | bb36d470 | bellard | uhci_update_irq(s); |
211 | bb36d470 | bellard | break;
|
212 | bb36d470 | bellard | case 0x06: |
213 | bb36d470 | bellard | if (s->status & UHCI_STS_HCHALTED)
|
214 | bb36d470 | bellard | s->frnum = val & 0x7ff;
|
215 | bb36d470 | bellard | break;
|
216 | bb36d470 | bellard | case 0x10 ... 0x1f: |
217 | bb36d470 | bellard | { |
218 | bb36d470 | bellard | UHCIPort *port; |
219 | bb36d470 | bellard | USBDevice *dev; |
220 | bb36d470 | bellard | int n;
|
221 | bb36d470 | bellard | |
222 | bb36d470 | bellard | n = (addr >> 1) & 7; |
223 | bb36d470 | bellard | if (n >= NB_PORTS)
|
224 | bb36d470 | bellard | return;
|
225 | bb36d470 | bellard | port = &s->ports[n]; |
226 | a594cfbf | bellard | dev = port->port.dev; |
227 | bb36d470 | bellard | if (dev) {
|
228 | bb36d470 | bellard | /* port reset */
|
229 | bb36d470 | bellard | if ( (val & UHCI_PORT_RESET) &&
|
230 | bb36d470 | bellard | !(port->ctrl & UHCI_PORT_RESET) ) { |
231 | bb36d470 | bellard | dev->handle_packet(dev, |
232 | bb36d470 | bellard | USB_MSG_RESET, 0, 0, NULL, 0); |
233 | bb36d470 | bellard | } |
234 | bb36d470 | bellard | } |
235 | bb36d470 | bellard | port->ctrl = (port->ctrl & 0x01fb) | (val & ~0x01fb); |
236 | bb36d470 | bellard | /* some bits are reset when a '1' is written to them */
|
237 | bb36d470 | bellard | port->ctrl &= ~(val & 0x000a);
|
238 | bb36d470 | bellard | } |
239 | bb36d470 | bellard | break;
|
240 | bb36d470 | bellard | } |
241 | bb36d470 | bellard | } |
242 | bb36d470 | bellard | |
243 | bb36d470 | bellard | static uint32_t uhci_ioport_readw(void *opaque, uint32_t addr) |
244 | bb36d470 | bellard | { |
245 | bb36d470 | bellard | UHCIState *s = opaque; |
246 | bb36d470 | bellard | uint32_t val; |
247 | bb36d470 | bellard | |
248 | bb36d470 | bellard | addr &= 0x1f;
|
249 | bb36d470 | bellard | switch(addr) {
|
250 | bb36d470 | bellard | case 0x00: |
251 | bb36d470 | bellard | val = s->cmd; |
252 | bb36d470 | bellard | break;
|
253 | bb36d470 | bellard | case 0x02: |
254 | bb36d470 | bellard | val = s->status; |
255 | bb36d470 | bellard | break;
|
256 | bb36d470 | bellard | case 0x04: |
257 | bb36d470 | bellard | val = s->intr; |
258 | bb36d470 | bellard | break;
|
259 | bb36d470 | bellard | case 0x06: |
260 | bb36d470 | bellard | val = s->frnum; |
261 | bb36d470 | bellard | break;
|
262 | bb36d470 | bellard | case 0x10 ... 0x1f: |
263 | bb36d470 | bellard | { |
264 | bb36d470 | bellard | UHCIPort *port; |
265 | bb36d470 | bellard | int n;
|
266 | bb36d470 | bellard | n = (addr >> 1) & 7; |
267 | bb36d470 | bellard | if (n >= NB_PORTS)
|
268 | bb36d470 | bellard | goto read_default;
|
269 | bb36d470 | bellard | port = &s->ports[n]; |
270 | bb36d470 | bellard | val = port->ctrl; |
271 | bb36d470 | bellard | } |
272 | bb36d470 | bellard | break;
|
273 | bb36d470 | bellard | default:
|
274 | bb36d470 | bellard | read_default:
|
275 | bb36d470 | bellard | val = 0xff7f; /* disabled port */ |
276 | bb36d470 | bellard | break;
|
277 | bb36d470 | bellard | } |
278 | bb36d470 | bellard | #ifdef DEBUG
|
279 | bb36d470 | bellard | printf("uhci readw port=0x%04x val=0x%04x\n", addr, val);
|
280 | bb36d470 | bellard | #endif
|
281 | bb36d470 | bellard | return val;
|
282 | bb36d470 | bellard | } |
283 | bb36d470 | bellard | |
284 | bb36d470 | bellard | static void uhci_ioport_writel(void *opaque, uint32_t addr, uint32_t val) |
285 | bb36d470 | bellard | { |
286 | bb36d470 | bellard | UHCIState *s = opaque; |
287 | bb36d470 | bellard | |
288 | bb36d470 | bellard | addr &= 0x1f;
|
289 | bb36d470 | bellard | #ifdef DEBUG
|
290 | bb36d470 | bellard | printf("uhci writel port=0x%04x val=0x%08x\n", addr, val);
|
291 | bb36d470 | bellard | #endif
|
292 | bb36d470 | bellard | switch(addr) {
|
293 | bb36d470 | bellard | case 0x08: |
294 | bb36d470 | bellard | s->fl_base_addr = val & ~0xfff;
|
295 | bb36d470 | bellard | break;
|
296 | bb36d470 | bellard | } |
297 | bb36d470 | bellard | } |
298 | bb36d470 | bellard | |
299 | bb36d470 | bellard | static uint32_t uhci_ioport_readl(void *opaque, uint32_t addr) |
300 | bb36d470 | bellard | { |
301 | bb36d470 | bellard | UHCIState *s = opaque; |
302 | bb36d470 | bellard | uint32_t val; |
303 | bb36d470 | bellard | |
304 | bb36d470 | bellard | addr &= 0x1f;
|
305 | bb36d470 | bellard | switch(addr) {
|
306 | bb36d470 | bellard | case 0x08: |
307 | bb36d470 | bellard | val = s->fl_base_addr; |
308 | bb36d470 | bellard | break;
|
309 | bb36d470 | bellard | default:
|
310 | bb36d470 | bellard | val = 0xffffffff;
|
311 | bb36d470 | bellard | break;
|
312 | bb36d470 | bellard | } |
313 | bb36d470 | bellard | return val;
|
314 | bb36d470 | bellard | } |
315 | bb36d470 | bellard | |
316 | bb36d470 | bellard | static void uhci_attach(USBPort *port1, USBDevice *dev) |
317 | bb36d470 | bellard | { |
318 | bb36d470 | bellard | UHCIState *s = port1->opaque; |
319 | bb36d470 | bellard | UHCIPort *port = &s->ports[port1->index]; |
320 | bb36d470 | bellard | |
321 | bb36d470 | bellard | if (dev) {
|
322 | a594cfbf | bellard | if (port->port.dev) {
|
323 | bb36d470 | bellard | usb_attach(port1, NULL);
|
324 | bb36d470 | bellard | } |
325 | bb36d470 | bellard | /* set connect status */
|
326 | bb36d470 | bellard | if (!(port->ctrl & UHCI_PORT_CCS)) {
|
327 | bb36d470 | bellard | port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC; |
328 | bb36d470 | bellard | } |
329 | bb36d470 | bellard | /* update speed */
|
330 | bb36d470 | bellard | if (dev->speed == USB_SPEED_LOW)
|
331 | bb36d470 | bellard | port->ctrl |= UHCI_PORT_LSDA; |
332 | bb36d470 | bellard | else
|
333 | bb36d470 | bellard | port->ctrl &= ~UHCI_PORT_LSDA; |
334 | a594cfbf | bellard | port->port.dev = dev; |
335 | bb36d470 | bellard | /* send the attach message */
|
336 | bb36d470 | bellard | dev->handle_packet(dev, |
337 | bb36d470 | bellard | USB_MSG_ATTACH, 0, 0, NULL, 0); |
338 | bb36d470 | bellard | } else {
|
339 | bb36d470 | bellard | /* set connect status */
|
340 | bb36d470 | bellard | if (!(port->ctrl & UHCI_PORT_CCS)) {
|
341 | bb36d470 | bellard | port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC; |
342 | bb36d470 | bellard | } |
343 | bb36d470 | bellard | /* disable port */
|
344 | bb36d470 | bellard | if (port->ctrl & UHCI_PORT_EN) {
|
345 | bb36d470 | bellard | port->ctrl &= ~UHCI_PORT_EN; |
346 | bb36d470 | bellard | port->ctrl |= UHCI_PORT_ENC; |
347 | bb36d470 | bellard | } |
348 | a594cfbf | bellard | dev = port->port.dev; |
349 | bb36d470 | bellard | if (dev) {
|
350 | bb36d470 | bellard | /* send the detach message */
|
351 | bb36d470 | bellard | dev->handle_packet(dev, |
352 | bb36d470 | bellard | USB_MSG_DETACH, 0, 0, NULL, 0); |
353 | bb36d470 | bellard | } |
354 | a594cfbf | bellard | port->port.dev = NULL;
|
355 | bb36d470 | bellard | } |
356 | bb36d470 | bellard | } |
357 | bb36d470 | bellard | |
358 | bb36d470 | bellard | static int uhci_broadcast_packet(UHCIState *s, uint8_t pid, |
359 | bb36d470 | bellard | uint8_t devaddr, uint8_t devep, |
360 | bb36d470 | bellard | uint8_t *data, int len)
|
361 | bb36d470 | bellard | { |
362 | bb36d470 | bellard | UHCIPort *port; |
363 | bb36d470 | bellard | USBDevice *dev; |
364 | bb36d470 | bellard | int i, ret;
|
365 | bb36d470 | bellard | |
366 | bb36d470 | bellard | #ifdef DEBUG_PACKET
|
367 | bb36d470 | bellard | { |
368 | bb36d470 | bellard | const char *pidstr; |
369 | bb36d470 | bellard | switch(pid) {
|
370 | bb36d470 | bellard | case USB_TOKEN_SETUP: pidstr = "SETUP"; break; |
371 | bb36d470 | bellard | case USB_TOKEN_IN: pidstr = "IN"; break; |
372 | bb36d470 | bellard | case USB_TOKEN_OUT: pidstr = "OUT"; break; |
373 | bb36d470 | bellard | default: pidstr = "?"; break; |
374 | bb36d470 | bellard | } |
375 | bb36d470 | bellard | printf("frame %d: pid=%s addr=0x%02x ep=%d len=%d\n",
|
376 | bb36d470 | bellard | s->frnum, pidstr, devaddr, devep, len); |
377 | bb36d470 | bellard | if (pid != USB_TOKEN_IN) {
|
378 | bb36d470 | bellard | printf(" data_out=");
|
379 | bb36d470 | bellard | for(i = 0; i < len; i++) { |
380 | bb36d470 | bellard | printf(" %02x", data[i]);
|
381 | bb36d470 | bellard | } |
382 | bb36d470 | bellard | printf("\n");
|
383 | bb36d470 | bellard | } |
384 | bb36d470 | bellard | } |
385 | bb36d470 | bellard | #endif
|
386 | bb36d470 | bellard | for(i = 0; i < NB_PORTS; i++) { |
387 | bb36d470 | bellard | port = &s->ports[i]; |
388 | a594cfbf | bellard | dev = port->port.dev; |
389 | bb36d470 | bellard | if (dev && (port->ctrl & UHCI_PORT_EN)) {
|
390 | bb36d470 | bellard | ret = dev->handle_packet(dev, pid, |
391 | bb36d470 | bellard | devaddr, devep, |
392 | bb36d470 | bellard | data, len); |
393 | bb36d470 | bellard | if (ret != USB_RET_NODEV) {
|
394 | bb36d470 | bellard | #ifdef DEBUG_PACKET
|
395 | bb36d470 | bellard | { |
396 | bb36d470 | bellard | printf(" ret=%d ", ret);
|
397 | bb36d470 | bellard | if (pid == USB_TOKEN_IN && ret > 0) { |
398 | bb36d470 | bellard | printf("data_in=");
|
399 | bb36d470 | bellard | for(i = 0; i < ret; i++) { |
400 | bb36d470 | bellard | printf(" %02x", data[i]);
|
401 | bb36d470 | bellard | } |
402 | bb36d470 | bellard | } |
403 | bb36d470 | bellard | printf("\n");
|
404 | bb36d470 | bellard | } |
405 | bb36d470 | bellard | #endif
|
406 | bb36d470 | bellard | return ret;
|
407 | bb36d470 | bellard | } |
408 | bb36d470 | bellard | } |
409 | bb36d470 | bellard | } |
410 | bb36d470 | bellard | return USB_RET_NODEV;
|
411 | bb36d470 | bellard | } |
412 | bb36d470 | bellard | |
413 | bb36d470 | bellard | /* return -1 if fatal error (frame must be stopped)
|
414 | bb36d470 | bellard | 0 if TD successful
|
415 | bb36d470 | bellard | 1 if TD unsuccessful or inactive
|
416 | bb36d470 | bellard | */
|
417 | bb36d470 | bellard | static int uhci_handle_td(UHCIState *s, UHCI_TD *td, int *int_mask) |
418 | bb36d470 | bellard | { |
419 | bb36d470 | bellard | uint8_t pid; |
420 | bb36d470 | bellard | uint8_t buf[1280];
|
421 | bb36d470 | bellard | int len, max_len, err, ret;
|
422 | bb36d470 | bellard | |
423 | bb36d470 | bellard | if (td->ctrl & TD_CTRL_IOC) {
|
424 | bb36d470 | bellard | *int_mask |= 0x01;
|
425 | bb36d470 | bellard | } |
426 | bb36d470 | bellard | |
427 | bb36d470 | bellard | if (!(td->ctrl & TD_CTRL_ACTIVE))
|
428 | bb36d470 | bellard | return 1; |
429 | bb36d470 | bellard | |
430 | bb36d470 | bellard | /* TD is active */
|
431 | bb36d470 | bellard | max_len = ((td->token >> 21) + 1) & 0x7ff; |
432 | bb36d470 | bellard | pid = td->token & 0xff;
|
433 | bb36d470 | bellard | switch(pid) {
|
434 | bb36d470 | bellard | case USB_TOKEN_OUT:
|
435 | bb36d470 | bellard | case USB_TOKEN_SETUP:
|
436 | bb36d470 | bellard | cpu_physical_memory_read(td->buffer, buf, max_len); |
437 | bb36d470 | bellard | ret = uhci_broadcast_packet(s, pid, |
438 | bb36d470 | bellard | (td->token >> 8) & 0x7f, |
439 | bb36d470 | bellard | (td->token >> 15) & 0xf, |
440 | bb36d470 | bellard | buf, max_len); |
441 | bb36d470 | bellard | len = max_len; |
442 | bb36d470 | bellard | break;
|
443 | bb36d470 | bellard | case USB_TOKEN_IN:
|
444 | bb36d470 | bellard | ret = uhci_broadcast_packet(s, pid, |
445 | bb36d470 | bellard | (td->token >> 8) & 0x7f, |
446 | bb36d470 | bellard | (td->token >> 15) & 0xf, |
447 | bb36d470 | bellard | buf, max_len); |
448 | bb36d470 | bellard | if (ret >= 0) { |
449 | bb36d470 | bellard | len = ret; |
450 | bb36d470 | bellard | if (len > max_len) {
|
451 | bb36d470 | bellard | len = max_len; |
452 | bb36d470 | bellard | ret = USB_RET_BABBLE; |
453 | bb36d470 | bellard | } |
454 | bb36d470 | bellard | if (len > 0) { |
455 | bb36d470 | bellard | /* write the data back */
|
456 | bb36d470 | bellard | cpu_physical_memory_write(td->buffer, buf, len); |
457 | bb36d470 | bellard | } |
458 | bb36d470 | bellard | } else {
|
459 | bb36d470 | bellard | len = 0;
|
460 | bb36d470 | bellard | } |
461 | bb36d470 | bellard | break;
|
462 | bb36d470 | bellard | default:
|
463 | bb36d470 | bellard | /* invalid pid : frame interrupted */
|
464 | bb36d470 | bellard | s->status |= UHCI_STS_HCPERR; |
465 | bb36d470 | bellard | uhci_update_irq(s); |
466 | bb36d470 | bellard | return -1; |
467 | bb36d470 | bellard | } |
468 | bb36d470 | bellard | if (td->ctrl & TD_CTRL_IOS)
|
469 | bb36d470 | bellard | td->ctrl &= ~TD_CTRL_ACTIVE; |
470 | bb36d470 | bellard | if (ret >= 0) { |
471 | bb36d470 | bellard | td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff); |
472 | bb36d470 | bellard | td->ctrl &= ~TD_CTRL_ACTIVE; |
473 | bb36d470 | bellard | if (pid == USB_TOKEN_IN &&
|
474 | bb36d470 | bellard | (td->ctrl & TD_CTRL_SPD) && |
475 | bb36d470 | bellard | len < max_len) { |
476 | bb36d470 | bellard | *int_mask |= 0x02;
|
477 | bb36d470 | bellard | /* short packet: do not update QH */
|
478 | bb36d470 | bellard | return 1; |
479 | bb36d470 | bellard | } else {
|
480 | bb36d470 | bellard | /* success */
|
481 | bb36d470 | bellard | return 0; |
482 | bb36d470 | bellard | } |
483 | bb36d470 | bellard | } else {
|
484 | bb36d470 | bellard | switch(ret) {
|
485 | bb36d470 | bellard | default:
|
486 | bb36d470 | bellard | case USB_RET_NODEV:
|
487 | bb36d470 | bellard | do_timeout:
|
488 | bb36d470 | bellard | td->ctrl |= TD_CTRL_TIMEOUT; |
489 | bb36d470 | bellard | err = (td->ctrl >> TD_CTRL_ERROR_SHIFT) & 3;
|
490 | bb36d470 | bellard | if (err != 0) { |
491 | bb36d470 | bellard | err--; |
492 | bb36d470 | bellard | if (err == 0) { |
493 | bb36d470 | bellard | td->ctrl &= ~TD_CTRL_ACTIVE; |
494 | bb36d470 | bellard | s->status |= UHCI_STS_USBERR; |
495 | bb36d470 | bellard | uhci_update_irq(s); |
496 | bb36d470 | bellard | } |
497 | bb36d470 | bellard | } |
498 | bb36d470 | bellard | td->ctrl = (td->ctrl & ~(3 << TD_CTRL_ERROR_SHIFT)) |
|
499 | bb36d470 | bellard | (err << TD_CTRL_ERROR_SHIFT); |
500 | bb36d470 | bellard | return 1; |
501 | bb36d470 | bellard | case USB_RET_NAK:
|
502 | bb36d470 | bellard | td->ctrl |= TD_CTRL_NAK; |
503 | bb36d470 | bellard | if (pid == USB_TOKEN_SETUP)
|
504 | bb36d470 | bellard | goto do_timeout;
|
505 | bb36d470 | bellard | return 1; |
506 | bb36d470 | bellard | case USB_RET_STALL:
|
507 | bb36d470 | bellard | td->ctrl |= TD_CTRL_STALL; |
508 | bb36d470 | bellard | td->ctrl &= ~TD_CTRL_ACTIVE; |
509 | bb36d470 | bellard | return 1; |
510 | bb36d470 | bellard | case USB_RET_BABBLE:
|
511 | bb36d470 | bellard | td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL; |
512 | bb36d470 | bellard | td->ctrl &= ~TD_CTRL_ACTIVE; |
513 | bb36d470 | bellard | /* frame interrupted */
|
514 | bb36d470 | bellard | return -1; |
515 | bb36d470 | bellard | } |
516 | bb36d470 | bellard | } |
517 | bb36d470 | bellard | } |
518 | bb36d470 | bellard | |
519 | bb36d470 | bellard | static void uhci_frame_timer(void *opaque) |
520 | bb36d470 | bellard | { |
521 | bb36d470 | bellard | UHCIState *s = opaque; |
522 | bb36d470 | bellard | int64_t expire_time; |
523 | bb36d470 | bellard | uint32_t frame_addr, link, old_td_ctrl, val; |
524 | bb36d470 | bellard | int int_mask, cnt, ret;
|
525 | bb36d470 | bellard | UHCI_TD td; |
526 | bb36d470 | bellard | UHCI_QH qh; |
527 | bb36d470 | bellard | |
528 | bb36d470 | bellard | if (!(s->cmd & UHCI_CMD_RS)) {
|
529 | bb36d470 | bellard | qemu_del_timer(s->frame_timer); |
530 | bb36d470 | bellard | return;
|
531 | bb36d470 | bellard | } |
532 | bb36d470 | bellard | frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2); |
533 | bb36d470 | bellard | cpu_physical_memory_read(frame_addr, (uint8_t *)&link, 4);
|
534 | bb36d470 | bellard | le32_to_cpus(&link); |
535 | bb36d470 | bellard | int_mask = 0;
|
536 | bb36d470 | bellard | cnt = FRAME_MAX_LOOPS; |
537 | bb36d470 | bellard | while ((link & 1) == 0) { |
538 | bb36d470 | bellard | if (--cnt == 0) |
539 | bb36d470 | bellard | break;
|
540 | bb36d470 | bellard | /* valid frame */
|
541 | bb36d470 | bellard | if (link & 2) { |
542 | bb36d470 | bellard | /* QH */
|
543 | bb36d470 | bellard | cpu_physical_memory_read(link & ~0xf, (uint8_t *)&qh, sizeof(qh)); |
544 | bb36d470 | bellard | le32_to_cpus(&qh.link); |
545 | bb36d470 | bellard | le32_to_cpus(&qh.el_link); |
546 | bb36d470 | bellard | depth_first:
|
547 | bb36d470 | bellard | if (qh.el_link & 1) { |
548 | bb36d470 | bellard | /* no element : go to next entry */
|
549 | bb36d470 | bellard | link = qh.link; |
550 | bb36d470 | bellard | } else if (qh.el_link & 2) { |
551 | bb36d470 | bellard | /* QH */
|
552 | bb36d470 | bellard | link = qh.el_link; |
553 | bb36d470 | bellard | } else {
|
554 | bb36d470 | bellard | /* TD */
|
555 | bb36d470 | bellard | if (--cnt == 0) |
556 | bb36d470 | bellard | break;
|
557 | bb36d470 | bellard | cpu_physical_memory_read(qh.el_link & ~0xf,
|
558 | bb36d470 | bellard | (uint8_t *)&td, sizeof(td));
|
559 | bb36d470 | bellard | le32_to_cpus(&td.link); |
560 | bb36d470 | bellard | le32_to_cpus(&td.ctrl); |
561 | bb36d470 | bellard | le32_to_cpus(&td.token); |
562 | bb36d470 | bellard | le32_to_cpus(&td.buffer); |
563 | bb36d470 | bellard | old_td_ctrl = td.ctrl; |
564 | bb36d470 | bellard | ret = uhci_handle_td(s, &td, &int_mask); |
565 | bb36d470 | bellard | /* update the status bits of the TD */
|
566 | bb36d470 | bellard | if (old_td_ctrl != td.ctrl) {
|
567 | bb36d470 | bellard | val = cpu_to_le32(td.ctrl); |
568 | bb36d470 | bellard | cpu_physical_memory_write((qh.el_link & ~0xf) + 4, |
569 | bb36d470 | bellard | (const uint8_t *)&val,
|
570 | bb36d470 | bellard | sizeof(val));
|
571 | bb36d470 | bellard | } |
572 | bb36d470 | bellard | if (ret < 0) |
573 | bb36d470 | bellard | break; /* interrupted frame */ |
574 | bb36d470 | bellard | if (ret == 0) { |
575 | bb36d470 | bellard | /* update qh element link */
|
576 | bb36d470 | bellard | qh.el_link = td.link; |
577 | bb36d470 | bellard | val = cpu_to_le32(qh.el_link); |
578 | bb36d470 | bellard | cpu_physical_memory_write((link & ~0xf) + 4, |
579 | bb36d470 | bellard | (const uint8_t *)&val,
|
580 | bb36d470 | bellard | sizeof(val));
|
581 | bb36d470 | bellard | if (qh.el_link & 4) { |
582 | bb36d470 | bellard | /* depth first */
|
583 | bb36d470 | bellard | goto depth_first;
|
584 | bb36d470 | bellard | } |
585 | bb36d470 | bellard | } |
586 | bb36d470 | bellard | /* go to next entry */
|
587 | bb36d470 | bellard | link = qh.link; |
588 | bb36d470 | bellard | } |
589 | bb36d470 | bellard | } else {
|
590 | bb36d470 | bellard | /* TD */
|
591 | bb36d470 | bellard | cpu_physical_memory_read(link & ~0xf, (uint8_t *)&td, sizeof(td)); |
592 | bb36d470 | bellard | le32_to_cpus(&td.link); |
593 | bb36d470 | bellard | le32_to_cpus(&td.ctrl); |
594 | bb36d470 | bellard | le32_to_cpus(&td.token); |
595 | bb36d470 | bellard | le32_to_cpus(&td.buffer); |
596 | bb36d470 | bellard | old_td_ctrl = td.ctrl; |
597 | bb36d470 | bellard | ret = uhci_handle_td(s, &td, &int_mask); |
598 | bb36d470 | bellard | /* update the status bits of the TD */
|
599 | bb36d470 | bellard | if (old_td_ctrl != td.ctrl) {
|
600 | bb36d470 | bellard | val = cpu_to_le32(td.ctrl); |
601 | bb36d470 | bellard | cpu_physical_memory_write((link & ~0xf) + 4, |
602 | bb36d470 | bellard | (const uint8_t *)&val,
|
603 | bb36d470 | bellard | sizeof(val));
|
604 | bb36d470 | bellard | } |
605 | bb36d470 | bellard | if (ret < 0) |
606 | bb36d470 | bellard | break; /* interrupted frame */ |
607 | bb36d470 | bellard | link = td.link; |
608 | bb36d470 | bellard | } |
609 | bb36d470 | bellard | } |
610 | bb36d470 | bellard | s->frnum = (s->frnum + 1) & 0x7ff; |
611 | bb36d470 | bellard | if (int_mask) {
|
612 | bb36d470 | bellard | s->status2 |= int_mask; |
613 | bb36d470 | bellard | s->status |= UHCI_STS_USBINT; |
614 | bb36d470 | bellard | uhci_update_irq(s); |
615 | bb36d470 | bellard | } |
616 | bb36d470 | bellard | /* prepare the timer for the next frame */
|
617 | bb36d470 | bellard | expire_time = qemu_get_clock(vm_clock) + |
618 | bb36d470 | bellard | (ticks_per_sec / FRAME_TIMER_FREQ); |
619 | bb36d470 | bellard | qemu_mod_timer(s->frame_timer, expire_time); |
620 | bb36d470 | bellard | } |
621 | bb36d470 | bellard | |
622 | bb36d470 | bellard | static void uhci_map(PCIDevice *pci_dev, int region_num, |
623 | bb36d470 | bellard | uint32_t addr, uint32_t size, int type)
|
624 | bb36d470 | bellard | { |
625 | bb36d470 | bellard | UHCIState *s = (UHCIState *)pci_dev; |
626 | bb36d470 | bellard | |
627 | bb36d470 | bellard | register_ioport_write(addr, 32, 2, uhci_ioport_writew, s); |
628 | bb36d470 | bellard | register_ioport_read(addr, 32, 2, uhci_ioport_readw, s); |
629 | bb36d470 | bellard | register_ioport_write(addr, 32, 4, uhci_ioport_writel, s); |
630 | bb36d470 | bellard | register_ioport_read(addr, 32, 4, uhci_ioport_readl, s); |
631 | bb36d470 | bellard | register_ioport_write(addr, 32, 1, uhci_ioport_writeb, s); |
632 | bb36d470 | bellard | register_ioport_read(addr, 32, 1, uhci_ioport_readb, s); |
633 | bb36d470 | bellard | } |
634 | bb36d470 | bellard | |
635 | bb36d470 | bellard | void usb_uhci_init(PCIBus *bus, USBPort **usb_ports)
|
636 | bb36d470 | bellard | { |
637 | bb36d470 | bellard | UHCIState *s; |
638 | bb36d470 | bellard | uint8_t *pci_conf; |
639 | bb36d470 | bellard | UHCIPort *port; |
640 | bb36d470 | bellard | int i;
|
641 | bb36d470 | bellard | |
642 | bb36d470 | bellard | s = (UHCIState *)pci_register_device(bus, |
643 | bb36d470 | bellard | "USB-UHCI", sizeof(UHCIState), |
644 | f04308e4 | bellard | ((PCIDevice *)piix3_state)->devfn + 2,
|
645 | bb36d470 | bellard | NULL, NULL); |
646 | bb36d470 | bellard | pci_conf = s->dev.config; |
647 | bb36d470 | bellard | pci_conf[0x00] = 0x86; |
648 | bb36d470 | bellard | pci_conf[0x01] = 0x80; |
649 | bb36d470 | bellard | pci_conf[0x02] = 0x20; |
650 | bb36d470 | bellard | pci_conf[0x03] = 0x70; |
651 | bb36d470 | bellard | pci_conf[0x08] = 0x01; // revision number |
652 | bb36d470 | bellard | pci_conf[0x09] = 0x00; |
653 | bb36d470 | bellard | pci_conf[0x0a] = 0x03; |
654 | bb36d470 | bellard | pci_conf[0x0b] = 0x0c; |
655 | bb36d470 | bellard | pci_conf[0x0e] = 0x00; // header_type |
656 | f04308e4 | bellard | pci_conf[0x3d] = 4; // interrupt pin 3 |
657 | bb36d470 | bellard | |
658 | bb36d470 | bellard | for(i = 0; i < NB_PORTS; i++) { |
659 | bb36d470 | bellard | port = &s->ports[i]; |
660 | bb36d470 | bellard | port->port.opaque = s; |
661 | bb36d470 | bellard | port->port.index = i; |
662 | bb36d470 | bellard | port->port.attach = uhci_attach; |
663 | bb36d470 | bellard | usb_ports[i] = &port->port; |
664 | bb36d470 | bellard | } |
665 | bb36d470 | bellard | s->frame_timer = qemu_new_timer(vm_clock, uhci_frame_timer, s); |
666 | bb36d470 | bellard | |
667 | bb36d470 | bellard | uhci_reset(s); |
668 | bb36d470 | bellard | |
669 | bb36d470 | bellard | pci_register_io_region(&s->dev, 0, 0x20, |
670 | bb36d470 | bellard | PCI_ADDRESS_SPACE_IO, uhci_map); |
671 | bb36d470 | bellard | } |