root / hw / usb.c @ f53c398a
History | View | Annotate | Download (14.1 kB)
1 |
/*
|
---|---|
2 |
* QEMU USB emulation
|
3 |
*
|
4 |
* Copyright (c) 2005 Fabrice Bellard
|
5 |
*
|
6 |
* 2008 Generic packet handler rewrite by Max Krasnyansky
|
7 |
*
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 |
* of this software and associated documentation files (the "Software"), to deal
|
10 |
* in the Software without restriction, including without limitation the rights
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 |
* copies of the Software, and to permit persons to whom the Software is
|
13 |
* furnished to do so, subject to the following conditions:
|
14 |
*
|
15 |
* The above copyright notice and this permission notice shall be included in
|
16 |
* all copies or substantial portions of the Software.
|
17 |
*
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
21 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
24 |
* THE SOFTWARE.
|
25 |
*/
|
26 |
#include "qemu-common.h" |
27 |
#include "usb.h" |
28 |
#include "iov.h" |
29 |
|
30 |
void usb_attach(USBPort *port)
|
31 |
{ |
32 |
USBDevice *dev = port->dev; |
33 |
|
34 |
assert(dev != NULL);
|
35 |
assert(dev->attached); |
36 |
assert(dev->state == USB_STATE_NOTATTACHED); |
37 |
port->ops->attach(port); |
38 |
dev->state = USB_STATE_ATTACHED; |
39 |
usb_device_handle_attach(dev); |
40 |
} |
41 |
|
42 |
void usb_detach(USBPort *port)
|
43 |
{ |
44 |
USBDevice *dev = port->dev; |
45 |
|
46 |
assert(dev != NULL);
|
47 |
assert(dev->state != USB_STATE_NOTATTACHED); |
48 |
port->ops->detach(port); |
49 |
dev->state = USB_STATE_NOTATTACHED; |
50 |
} |
51 |
|
52 |
void usb_port_reset(USBPort *port)
|
53 |
{ |
54 |
USBDevice *dev = port->dev; |
55 |
|
56 |
assert(dev != NULL);
|
57 |
usb_detach(port); |
58 |
usb_attach(port); |
59 |
usb_device_reset(dev); |
60 |
} |
61 |
|
62 |
void usb_device_reset(USBDevice *dev)
|
63 |
{ |
64 |
if (dev == NULL || !dev->attached) { |
65 |
return;
|
66 |
} |
67 |
dev->remote_wakeup = 0;
|
68 |
dev->addr = 0;
|
69 |
dev->state = USB_STATE_DEFAULT; |
70 |
usb_device_handle_reset(dev); |
71 |
} |
72 |
|
73 |
void usb_wakeup(USBDevice *dev)
|
74 |
{ |
75 |
if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
|
76 |
dev->port->ops->wakeup(dev->port); |
77 |
} |
78 |
} |
79 |
|
80 |
/**********************/
|
81 |
|
82 |
/* generic USB device helpers (you are not forced to use them when
|
83 |
writing your USB device driver, but they help handling the
|
84 |
protocol)
|
85 |
*/
|
86 |
|
87 |
#define SETUP_STATE_IDLE 0 |
88 |
#define SETUP_STATE_SETUP 1 |
89 |
#define SETUP_STATE_DATA 2 |
90 |
#define SETUP_STATE_ACK 3 |
91 |
|
92 |
static int do_token_setup(USBDevice *s, USBPacket *p) |
93 |
{ |
94 |
int request, value, index;
|
95 |
int ret = 0; |
96 |
|
97 |
if (p->iov.size != 8) { |
98 |
return USB_RET_STALL;
|
99 |
} |
100 |
|
101 |
usb_packet_copy(p, s->setup_buf, p->iov.size); |
102 |
s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; |
103 |
s->setup_index = 0;
|
104 |
|
105 |
request = (s->setup_buf[0] << 8) | s->setup_buf[1]; |
106 |
value = (s->setup_buf[3] << 8) | s->setup_buf[2]; |
107 |
index = (s->setup_buf[5] << 8) | s->setup_buf[4]; |
108 |
|
109 |
if (s->setup_buf[0] & USB_DIR_IN) { |
110 |
ret = usb_device_handle_control(s, p, request, value, index, |
111 |
s->setup_len, s->data_buf); |
112 |
if (ret == USB_RET_ASYNC) {
|
113 |
s->setup_state = SETUP_STATE_SETUP; |
114 |
return USB_RET_ASYNC;
|
115 |
} |
116 |
if (ret < 0) |
117 |
return ret;
|
118 |
|
119 |
if (ret < s->setup_len)
|
120 |
s->setup_len = ret; |
121 |
s->setup_state = SETUP_STATE_DATA; |
122 |
} else {
|
123 |
if (s->setup_len > sizeof(s->data_buf)) { |
124 |
fprintf(stderr, |
125 |
"usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
|
126 |
s->setup_len, sizeof(s->data_buf));
|
127 |
return USB_RET_STALL;
|
128 |
} |
129 |
if (s->setup_len == 0) |
130 |
s->setup_state = SETUP_STATE_ACK; |
131 |
else
|
132 |
s->setup_state = SETUP_STATE_DATA; |
133 |
} |
134 |
|
135 |
return ret;
|
136 |
} |
137 |
|
138 |
static int do_token_in(USBDevice *s, USBPacket *p) |
139 |
{ |
140 |
int request, value, index;
|
141 |
int ret = 0; |
142 |
|
143 |
assert(p->devep == 0);
|
144 |
|
145 |
request = (s->setup_buf[0] << 8) | s->setup_buf[1]; |
146 |
value = (s->setup_buf[3] << 8) | s->setup_buf[2]; |
147 |
index = (s->setup_buf[5] << 8) | s->setup_buf[4]; |
148 |
|
149 |
switch(s->setup_state) {
|
150 |
case SETUP_STATE_ACK:
|
151 |
if (!(s->setup_buf[0] & USB_DIR_IN)) { |
152 |
ret = usb_device_handle_control(s, p, request, value, index, |
153 |
s->setup_len, s->data_buf); |
154 |
if (ret == USB_RET_ASYNC) {
|
155 |
return USB_RET_ASYNC;
|
156 |
} |
157 |
s->setup_state = SETUP_STATE_IDLE; |
158 |
if (ret > 0) |
159 |
return 0; |
160 |
return ret;
|
161 |
} |
162 |
|
163 |
/* return 0 byte */
|
164 |
return 0; |
165 |
|
166 |
case SETUP_STATE_DATA:
|
167 |
if (s->setup_buf[0] & USB_DIR_IN) { |
168 |
int len = s->setup_len - s->setup_index;
|
169 |
if (len > p->iov.size) {
|
170 |
len = p->iov.size; |
171 |
} |
172 |
usb_packet_copy(p, s->data_buf + s->setup_index, len); |
173 |
s->setup_index += len; |
174 |
if (s->setup_index >= s->setup_len)
|
175 |
s->setup_state = SETUP_STATE_ACK; |
176 |
return len;
|
177 |
} |
178 |
|
179 |
s->setup_state = SETUP_STATE_IDLE; |
180 |
return USB_RET_STALL;
|
181 |
|
182 |
default:
|
183 |
return USB_RET_STALL;
|
184 |
} |
185 |
} |
186 |
|
187 |
static int do_token_out(USBDevice *s, USBPacket *p) |
188 |
{ |
189 |
assert(p->devep == 0);
|
190 |
|
191 |
switch(s->setup_state) {
|
192 |
case SETUP_STATE_ACK:
|
193 |
if (s->setup_buf[0] & USB_DIR_IN) { |
194 |
s->setup_state = SETUP_STATE_IDLE; |
195 |
/* transfer OK */
|
196 |
} else {
|
197 |
/* ignore additional output */
|
198 |
} |
199 |
return 0; |
200 |
|
201 |
case SETUP_STATE_DATA:
|
202 |
if (!(s->setup_buf[0] & USB_DIR_IN)) { |
203 |
int len = s->setup_len - s->setup_index;
|
204 |
if (len > p->iov.size) {
|
205 |
len = p->iov.size; |
206 |
} |
207 |
usb_packet_copy(p, s->data_buf + s->setup_index, len); |
208 |
s->setup_index += len; |
209 |
if (s->setup_index >= s->setup_len)
|
210 |
s->setup_state = SETUP_STATE_ACK; |
211 |
return len;
|
212 |
} |
213 |
|
214 |
s->setup_state = SETUP_STATE_IDLE; |
215 |
return USB_RET_STALL;
|
216 |
|
217 |
default:
|
218 |
return USB_RET_STALL;
|
219 |
} |
220 |
} |
221 |
|
222 |
/* ctrl complete function for devices which use usb_generic_handle_packet and
|
223 |
may return USB_RET_ASYNC from their handle_control callback. Device code
|
224 |
which does this *must* call this function instead of the normal
|
225 |
usb_packet_complete to complete their async control packets. */
|
226 |
void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
|
227 |
{ |
228 |
if (p->result < 0) { |
229 |
s->setup_state = SETUP_STATE_IDLE; |
230 |
} |
231 |
|
232 |
switch (s->setup_state) {
|
233 |
case SETUP_STATE_SETUP:
|
234 |
if (p->result < s->setup_len) {
|
235 |
s->setup_len = p->result; |
236 |
} |
237 |
s->setup_state = SETUP_STATE_DATA; |
238 |
p->result = 8;
|
239 |
break;
|
240 |
|
241 |
case SETUP_STATE_ACK:
|
242 |
s->setup_state = SETUP_STATE_IDLE; |
243 |
p->result = 0;
|
244 |
break;
|
245 |
|
246 |
default:
|
247 |
break;
|
248 |
} |
249 |
usb_packet_complete(s, p); |
250 |
} |
251 |
|
252 |
/* XXX: fix overflow */
|
253 |
int set_usb_string(uint8_t *buf, const char *str) |
254 |
{ |
255 |
int len, i;
|
256 |
uint8_t *q; |
257 |
|
258 |
q = buf; |
259 |
len = strlen(str); |
260 |
*q++ = 2 * len + 2; |
261 |
*q++ = 3;
|
262 |
for(i = 0; i < len; i++) { |
263 |
*q++ = str[i]; |
264 |
*q++ = 0;
|
265 |
} |
266 |
return q - buf;
|
267 |
} |
268 |
|
269 |
USBDevice *usb_find_device(USBPort *port, uint8_t addr) |
270 |
{ |
271 |
USBDevice *dev = port->dev; |
272 |
|
273 |
if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) { |
274 |
return NULL; |
275 |
} |
276 |
if (dev->addr == addr) {
|
277 |
return dev;
|
278 |
} |
279 |
return usb_device_find_device(dev, addr);
|
280 |
} |
281 |
|
282 |
/* Hand over a packet to a device for processing. Return value
|
283 |
USB_RET_ASYNC indicates the processing isn't finished yet, the
|
284 |
driver will call usb_packet_complete() when done processing it. */
|
285 |
int usb_handle_packet(USBDevice *dev, USBPacket *p)
|
286 |
{ |
287 |
int ret;
|
288 |
|
289 |
if (dev == NULL) { |
290 |
return USB_RET_NODEV;
|
291 |
} |
292 |
assert(dev->addr == p->devaddr); |
293 |
assert(dev->state == USB_STATE_DEFAULT); |
294 |
assert(p->state == USB_PACKET_SETUP); |
295 |
|
296 |
if (p->devep == 0) { |
297 |
/* control pipe */
|
298 |
switch (p->pid) {
|
299 |
case USB_TOKEN_SETUP:
|
300 |
ret = do_token_setup(dev, p); |
301 |
break;
|
302 |
case USB_TOKEN_IN:
|
303 |
ret = do_token_in(dev, p); |
304 |
break;
|
305 |
case USB_TOKEN_OUT:
|
306 |
ret = do_token_out(dev, p); |
307 |
break;
|
308 |
default:
|
309 |
ret = USB_RET_STALL; |
310 |
break;
|
311 |
} |
312 |
} else {
|
313 |
/* data pipe */
|
314 |
ret = usb_device_handle_data(dev, p); |
315 |
} |
316 |
|
317 |
if (ret == USB_RET_ASYNC) {
|
318 |
p->ep = usb_ep_get(dev, p->pid, p->devep); |
319 |
p->state = USB_PACKET_ASYNC; |
320 |
} |
321 |
return ret;
|
322 |
} |
323 |
|
324 |
/* Notify the controller that an async packet is complete. This should only
|
325 |
be called for packets previously deferred by returning USB_RET_ASYNC from
|
326 |
handle_packet. */
|
327 |
void usb_packet_complete(USBDevice *dev, USBPacket *p)
|
328 |
{ |
329 |
assert(p->state == USB_PACKET_ASYNC); |
330 |
p->state = USB_PACKET_COMPLETE; |
331 |
dev->port->ops->complete(dev->port, p); |
332 |
} |
333 |
|
334 |
/* Cancel an active packet. The packed must have been deferred by
|
335 |
returning USB_RET_ASYNC from handle_packet, and not yet
|
336 |
completed. */
|
337 |
void usb_cancel_packet(USBPacket * p)
|
338 |
{ |
339 |
assert(p->state == USB_PACKET_ASYNC); |
340 |
p->state = USB_PACKET_CANCELED; |
341 |
usb_device_cancel_packet(p->ep->dev, p); |
342 |
} |
343 |
|
344 |
|
345 |
void usb_packet_init(USBPacket *p)
|
346 |
{ |
347 |
qemu_iovec_init(&p->iov, 1);
|
348 |
} |
349 |
|
350 |
void usb_packet_setup(USBPacket *p, int pid, uint8_t addr, uint8_t ep) |
351 |
{ |
352 |
assert(!usb_packet_is_inflight(p)); |
353 |
p->state = USB_PACKET_SETUP; |
354 |
p->pid = pid; |
355 |
p->devaddr = addr; |
356 |
p->devep = ep; |
357 |
p->result = 0;
|
358 |
qemu_iovec_reset(&p->iov); |
359 |
} |
360 |
|
361 |
void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len) |
362 |
{ |
363 |
qemu_iovec_add(&p->iov, ptr, len); |
364 |
} |
365 |
|
366 |
void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes) |
367 |
{ |
368 |
assert(p->result >= 0);
|
369 |
assert(p->result + bytes <= p->iov.size); |
370 |
switch (p->pid) {
|
371 |
case USB_TOKEN_SETUP:
|
372 |
case USB_TOKEN_OUT:
|
373 |
iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes); |
374 |
break;
|
375 |
case USB_TOKEN_IN:
|
376 |
iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes); |
377 |
break;
|
378 |
default:
|
379 |
fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
|
380 |
abort(); |
381 |
} |
382 |
p->result += bytes; |
383 |
} |
384 |
|
385 |
void usb_packet_skip(USBPacket *p, size_t bytes)
|
386 |
{ |
387 |
assert(p->result >= 0);
|
388 |
assert(p->result + bytes <= p->iov.size); |
389 |
if (p->pid == USB_TOKEN_IN) {
|
390 |
iov_clear(p->iov.iov, p->iov.niov, p->result, bytes); |
391 |
} |
392 |
p->result += bytes; |
393 |
} |
394 |
|
395 |
void usb_packet_cleanup(USBPacket *p)
|
396 |
{ |
397 |
assert(!usb_packet_is_inflight(p)); |
398 |
qemu_iovec_destroy(&p->iov); |
399 |
} |
400 |
|
401 |
void usb_ep_init(USBDevice *dev)
|
402 |
{ |
403 |
int ep;
|
404 |
|
405 |
dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL; |
406 |
dev->ep_ctl.ifnum = 0;
|
407 |
dev->ep_ctl.dev = dev; |
408 |
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { |
409 |
dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID; |
410 |
dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID; |
411 |
dev->ep_in[ep].ifnum = 0;
|
412 |
dev->ep_out[ep].ifnum = 0;
|
413 |
dev->ep_in[ep].dev = dev; |
414 |
dev->ep_out[ep].dev = dev; |
415 |
} |
416 |
} |
417 |
|
418 |
void usb_ep_dump(USBDevice *dev)
|
419 |
{ |
420 |
static const char *tname[] = { |
421 |
[USB_ENDPOINT_XFER_CONTROL] = "control",
|
422 |
[USB_ENDPOINT_XFER_ISOC] = "isoc",
|
423 |
[USB_ENDPOINT_XFER_BULK] = "bulk",
|
424 |
[USB_ENDPOINT_XFER_INT] = "int",
|
425 |
}; |
426 |
int ifnum, ep, first;
|
427 |
|
428 |
fprintf(stderr, "Device \"%s\", config %d\n",
|
429 |
dev->product_desc, dev->configuration); |
430 |
for (ifnum = 0; ifnum < 16; ifnum++) { |
431 |
first = 1;
|
432 |
for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { |
433 |
if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
|
434 |
dev->ep_in[ep].ifnum == ifnum) { |
435 |
if (first) {
|
436 |
first = 0;
|
437 |
fprintf(stderr, " Interface %d, alternative %d\n",
|
438 |
ifnum, dev->altsetting[ifnum]); |
439 |
} |
440 |
fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
|
441 |
tname[dev->ep_in[ep].type], |
442 |
dev->ep_in[ep].max_packet_size); |
443 |
} |
444 |
if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
|
445 |
dev->ep_out[ep].ifnum == ifnum) { |
446 |
if (first) {
|
447 |
first = 0;
|
448 |
fprintf(stderr, " Interface %d, alternative %d\n",
|
449 |
ifnum, dev->altsetting[ifnum]); |
450 |
} |
451 |
fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
|
452 |
tname[dev->ep_out[ep].type], |
453 |
dev->ep_out[ep].max_packet_size); |
454 |
} |
455 |
} |
456 |
} |
457 |
fprintf(stderr, "--\n");
|
458 |
} |
459 |
|
460 |
struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep) |
461 |
{ |
462 |
struct USBEndpoint *eps = pid == USB_TOKEN_IN ? dev->ep_in : dev->ep_out;
|
463 |
if (ep == 0) { |
464 |
return &dev->ep_ctl;
|
465 |
} |
466 |
assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT); |
467 |
assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
|
468 |
return eps + ep - 1; |
469 |
} |
470 |
|
471 |
uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep) |
472 |
{ |
473 |
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
474 |
return uep->type;
|
475 |
} |
476 |
|
477 |
void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type) |
478 |
{ |
479 |
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
480 |
uep->type = type; |
481 |
} |
482 |
|
483 |
uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep) |
484 |
{ |
485 |
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
486 |
return uep->ifnum;
|
487 |
} |
488 |
|
489 |
void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum) |
490 |
{ |
491 |
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
492 |
uep->ifnum = ifnum; |
493 |
} |
494 |
|
495 |
void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep, |
496 |
uint16_t raw) |
497 |
{ |
498 |
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
499 |
int size, microframes;
|
500 |
|
501 |
size = raw & 0x7ff;
|
502 |
switch ((raw >> 11) & 3) { |
503 |
case 1: |
504 |
microframes = 2;
|
505 |
break;
|
506 |
case 2: |
507 |
microframes = 3;
|
508 |
break;
|
509 |
default:
|
510 |
microframes = 1;
|
511 |
break;
|
512 |
} |
513 |
uep->max_packet_size = size * microframes; |
514 |
} |
515 |
|
516 |
int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep) |
517 |
{ |
518 |
struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
|
519 |
return uep->max_packet_size;
|
520 |
} |