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 #include "trace.h" 30 31 void usb_attach(USBPort *port) 32 { 33 USBDevice *dev = port->dev; 34 35 assert(dev != NULL); 36 assert(dev->attached); 37 assert(dev->state == USB_STATE_NOTATTACHED); 38 port->ops->attach(port); 39 dev->state = USB_STATE_ATTACHED; 40 usb_device_handle_attach(dev); 41 } 42 43 void usb_detach(USBPort *port) 44 { 45 USBDevice *dev = port->dev; 46 47 assert(dev != NULL); 48 assert(dev->state != USB_STATE_NOTATTACHED); 49 port->ops->detach(port); 50 dev->state = USB_STATE_NOTATTACHED; 51 } 52 53 void usb_port_reset(USBPort *port) 54 { 55 USBDevice *dev = port->dev; 56 57 assert(dev != NULL); 58 usb_detach(port); 59 usb_attach(port); 60 usb_device_reset(dev); 61 } 62 63 void usb_device_reset(USBDevice *dev) 64 { 65 if (dev == NULL || !dev->attached) { 66 return; 67 } 68 dev->remote_wakeup = 0; 69 dev->addr = 0; 70 dev->state = USB_STATE_DEFAULT; 71 usb_device_handle_reset(dev); 72 } 73 74 void usb_wakeup(USBEndpoint *ep) 75 { 76 USBDevice *dev = ep->dev; 77 USBBus *bus = usb_bus_from_device(dev); 78 79 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) { 80 dev->port->ops->wakeup(dev->port); 81 } 82 if (bus->ops->wakeup_endpoint) { 83 bus->ops->wakeup_endpoint(bus, ep); 84 } 85 } 86 87 /**********************/ 88 89 /* generic USB device helpers (you are not forced to use them when 90 writing your USB device driver, but they help handling the 91 protocol) 92 */ 93 94 #define SETUP_STATE_IDLE 0 95 #define SETUP_STATE_SETUP 1 96 #define SETUP_STATE_DATA 2 97 #define SETUP_STATE_ACK 3 98 99 static int do_token_setup(USBDevice *s, USBPacket *p) 100 { 101 int request, value, index; 102 int ret = 0; 103 104 if (p->iov.size != 8) { 105 return USB_RET_STALL; 106 } 107 108 usb_packet_copy(p, s->setup_buf, p->iov.size); 109 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; 110 s->setup_index = 0; 111 112 request = (s->setup_buf[0] << 8) | s->setup_buf[1]; 113 value = (s->setup_buf[3] << 8) | s->setup_buf[2]; 114 index = (s->setup_buf[5] << 8) | s->setup_buf[4]; 115 116 if (s->setup_buf[0] & USB_DIR_IN) { 117 ret = usb_device_handle_control(s, p, request, value, index, 118 s->setup_len, s->data_buf); 119 if (ret == USB_RET_ASYNC) { 120 s->setup_state = SETUP_STATE_SETUP; 121 return USB_RET_ASYNC; 122 } 123 if (ret < 0) 124 return ret; 125 126 if (ret < s->setup_len) 127 s->setup_len = ret; 128 s->setup_state = SETUP_STATE_DATA; 129 } else { 130 if (s->setup_len > sizeof(s->data_buf)) { 131 fprintf(stderr, 132 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n", 133 s->setup_len, sizeof(s->data_buf)); 134 return USB_RET_STALL; 135 } 136 if (s->setup_len == 0) 137 s->setup_state = SETUP_STATE_ACK; 138 else 139 s->setup_state = SETUP_STATE_DATA; 140 } 141 142 return ret; 143 } 144 145 static int do_token_in(USBDevice *s, USBPacket *p) 146 { 147 int request, value, index; 148 int ret = 0; 149 150 assert(p->ep->nr == 0); 151 152 request = (s->setup_buf[0] << 8) | s->setup_buf[1]; 153 value = (s->setup_buf[3] << 8) | s->setup_buf[2]; 154 index = (s->setup_buf[5] << 8) | s->setup_buf[4]; 155 156 switch(s->setup_state) { 157 case SETUP_STATE_ACK: 158 if (!(s->setup_buf[0] & USB_DIR_IN)) { 159 ret = usb_device_handle_control(s, p, request, value, index, 160 s->setup_len, s->data_buf); 161 if (ret == USB_RET_ASYNC) { 162 return USB_RET_ASYNC; 163 } 164 s->setup_state = SETUP_STATE_IDLE; 165 if (ret > 0) 166 return 0; 167 return ret; 168 } 169 170 /* return 0 byte */ 171 return 0; 172 173 case SETUP_STATE_DATA: 174 if (s->setup_buf[0] & USB_DIR_IN) { 175 int len = s->setup_len - s->setup_index; 176 if (len > p->iov.size) { 177 len = p->iov.size; 178 } 179 usb_packet_copy(p, s->data_buf + s->setup_index, len); 180 s->setup_index += len; 181 if (s->setup_index >= s->setup_len) 182 s->setup_state = SETUP_STATE_ACK; 183 return len; 184 } 185 186 s->setup_state = SETUP_STATE_IDLE; 187 return USB_RET_STALL; 188 189 default: 190 return USB_RET_STALL; 191 } 192 } 193 194 static int do_token_out(USBDevice *s, USBPacket *p) 195 { 196 assert(p->ep->nr == 0); 197 198 switch(s->setup_state) { 199 case SETUP_STATE_ACK: 200 if (s->setup_buf[0] & USB_DIR_IN) { 201 s->setup_state = SETUP_STATE_IDLE; 202 /* transfer OK */ 203 } else { 204 /* ignore additional output */ 205 } 206 return 0; 207 208 case SETUP_STATE_DATA: 209 if (!(s->setup_buf[0] & USB_DIR_IN)) { 210 int len = s->setup_len - s->setup_index; 211 if (len > p->iov.size) { 212 len = p->iov.size; 213 } 214 usb_packet_copy(p, s->data_buf + s->setup_index, len); 215 s->setup_index += len; 216 if (s->setup_index >= s->setup_len) 217 s->setup_state = SETUP_STATE_ACK; 218 return len; 219 } 220 221 s->setup_state = SETUP_STATE_IDLE; 222 return USB_RET_STALL; 223 224 default: 225 return USB_RET_STALL; 226 } 227 } 228 229 /* ctrl complete function for devices which use usb_generic_handle_packet and 230 may return USB_RET_ASYNC from their handle_control callback. Device code 231 which does this *must* call this function instead of the normal 232 usb_packet_complete to complete their async control packets. */ 233 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p) 234 { 235 if (p->result < 0) { 236 s->setup_state = SETUP_STATE_IDLE; 237 } 238 239 switch (s->setup_state) { 240 case SETUP_STATE_SETUP: 241 if (p->result < s->setup_len) { 242 s->setup_len = p->result; 243 } 244 s->setup_state = SETUP_STATE_DATA; 245 p->result = 8; 246 break; 247 248 case SETUP_STATE_ACK: 249 s->setup_state = SETUP_STATE_IDLE; 250 p->result = 0; 251 break; 252 253 default: 254 break; 255 } 256 usb_packet_complete(s, p); 257 } 258 259 /* XXX: fix overflow */ 260 int set_usb_string(uint8_t *buf, const char *str) 261 { 262 int len, i; 263 uint8_t *q; 264 265 q = buf; 266 len = strlen(str); 267 *q++ = 2 * len + 2; 268 *q++ = 3; 269 for(i = 0; i < len; i++) { 270 *q++ = str[i]; 271 *q++ = 0; 272 } 273 return q - buf; 274 } 275 276 USBDevice *usb_find_device(USBPort *port, uint8_t addr) 277 { 278 USBDevice *dev = port->dev; 279 280 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) { 281 return NULL; 282 } 283 if (dev->addr == addr) { 284 return dev; 285 } 286 return usb_device_find_device(dev, addr); 287 } 288 289 static int usb_process_one(USBPacket *p) 290 { 291 USBDevice *dev = p->ep->dev; 292 293 if (p->ep->nr == 0) { 294 /* control pipe */ 295 switch (p->pid) { 296 case USB_TOKEN_SETUP: 297 return do_token_setup(dev, p); 298 case USB_TOKEN_IN: 299 return do_token_in(dev, p); 300 case USB_TOKEN_OUT: 301 return do_token_out(dev, p); 302 default: 303 return USB_RET_STALL; 304 } 305 } else { 306 /* data pipe */ 307 return usb_device_handle_data(dev, p); 308 } 309 } 310 311 /* Hand over a packet to a device for processing. Return value 312 USB_RET_ASYNC indicates the processing isn't finished yet, the 313 driver will call usb_packet_complete() when done processing it. */ 314 int usb_handle_packet(USBDevice *dev, USBPacket *p) 315 { 316 int ret; 317 318 if (dev == NULL) { 319 return USB_RET_NODEV; 320 } 321 assert(dev == p->ep->dev); 322 assert(dev->state == USB_STATE_DEFAULT); 323 assert(p->state == USB_PACKET_SETUP); 324 assert(p->ep != NULL); 325 326 if (QTAILQ_EMPTY(&p->ep->queue)) { 327 ret = usb_process_one(p); 328 if (ret == USB_RET_ASYNC) { 329 usb_packet_set_state(p, USB_PACKET_ASYNC); 330 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue); 331 } else { 332 p->result = ret; 333 usb_packet_set_state(p, USB_PACKET_COMPLETE); 334 } 335 } else { 336 ret = USB_RET_ASYNC; 337 usb_packet_set_state(p, USB_PACKET_QUEUED); 338 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue); 339 } 340 return ret; 341 } 342 343 /* Notify the controller that an async packet is complete. This should only 344 be called for packets previously deferred by returning USB_RET_ASYNC from 345 handle_packet. */ 346 void usb_packet_complete(USBDevice *dev, USBPacket *p) 347 { 348 USBEndpoint *ep = p->ep; 349 int ret; 350 351 assert(p->state == USB_PACKET_ASYNC); 352 assert(QTAILQ_FIRST(&ep->queue) == p); 353 usb_packet_set_state(p, USB_PACKET_COMPLETE); 354 QTAILQ_REMOVE(&ep->queue, p, queue); 355 dev->port->ops->complete(dev->port, p); 356 357 while (!QTAILQ_EMPTY(&ep->queue)) { 358 p = QTAILQ_FIRST(&ep->queue); 359 if (p->state == USB_PACKET_ASYNC) { 360 break; 361 } 362 assert(p->state == USB_PACKET_QUEUED); 363 ret = usb_process_one(p); 364 if (ret == USB_RET_ASYNC) { 365 usb_packet_set_state(p, USB_PACKET_ASYNC); 366 break; 367 } 368 p->result = ret; 369 usb_packet_set_state(p, USB_PACKET_COMPLETE); 370 QTAILQ_REMOVE(&ep->queue, p, queue); 371 dev->port->ops->complete(dev->port, p); 372 } 373 } 374 375 /* Cancel an active packet. The packed must have been deferred by 376 returning USB_RET_ASYNC from handle_packet, and not yet 377 completed. */ 378 void usb_cancel_packet(USBPacket * p) 379 { 380 bool callback = (p->state == USB_PACKET_ASYNC); 381 assert(usb_packet_is_inflight(p)); 382 usb_packet_set_state(p, USB_PACKET_CANCELED); 383 QTAILQ_REMOVE(&p->ep->queue, p, queue); 384 if (callback) { 385 usb_device_cancel_packet(p->ep->dev, p); 386 } 387 } 388 389 390 void usb_packet_init(USBPacket *p) 391 { 392 qemu_iovec_init(&p->iov, 1); 393 } 394 395 void usb_packet_set_state(USBPacket *p, USBPacketState state) 396 { 397 static const char *name[] = { 398 [USB_PACKET_UNDEFINED] = "undef", 399 [USB_PACKET_SETUP] = "setup", 400 [USB_PACKET_QUEUED] = "queued", 401 [USB_PACKET_ASYNC] = "async", 402 [USB_PACKET_COMPLETE] = "complete", 403 [USB_PACKET_CANCELED] = "canceled", 404 }; 405 USBDevice *dev = p->ep->dev; 406 USBBus *bus = usb_bus_from_device(dev); 407 408 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, 409 p, name[p->state], name[state]); 410 p->state = state; 411 } 412 413 void usb_packet_setup(USBPacket *p, int pid, USBEndpoint *ep) 414 { 415 assert(!usb_packet_is_inflight(p)); 416 p->pid = pid; 417 p->ep = ep; 418 p->result = 0; 419 qemu_iovec_reset(&p->iov); 420 usb_packet_set_state(p, USB_PACKET_SETUP); 421 } 422 423 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len) 424 { 425 qemu_iovec_add(&p->iov, ptr, len); 426 } 427 428 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes) 429 { 430 assert(p->result >= 0); 431 assert(p->result + bytes <= p->iov.size); 432 switch (p->pid) { 433 case USB_TOKEN_SETUP: 434 case USB_TOKEN_OUT: 435 iov_to_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes); 436 break; 437 case USB_TOKEN_IN: 438 iov_from_buf(p->iov.iov, p->iov.niov, ptr, p->result, bytes); 439 break; 440 default: 441 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid); 442 abort(); 443 } 444 p->result += bytes; 445 } 446 447 void usb_packet_skip(USBPacket *p, size_t bytes) 448 { 449 assert(p->result >= 0); 450 assert(p->result + bytes <= p->iov.size); 451 if (p->pid == USB_TOKEN_IN) { 452 iov_clear(p->iov.iov, p->iov.niov, p->result, bytes); 453 } 454 p->result += bytes; 455 } 456 457 void usb_packet_cleanup(USBPacket *p) 458 { 459 assert(!usb_packet_is_inflight(p)); 460 qemu_iovec_destroy(&p->iov); 461 } 462 463 void usb_ep_init(USBDevice *dev) 464 { 465 int ep; 466 467 dev->ep_ctl.nr = 0; 468 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL; 469 dev->ep_ctl.ifnum = 0; 470 dev->ep_ctl.dev = dev; 471 QTAILQ_INIT(&dev->ep_ctl.queue); 472 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { 473 dev->ep_in[ep].nr = ep + 1; 474 dev->ep_out[ep].nr = ep + 1; 475 dev->ep_in[ep].pid = USB_TOKEN_IN; 476 dev->ep_out[ep].pid = USB_TOKEN_OUT; 477 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID; 478 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID; 479 dev->ep_in[ep].ifnum = 0; 480 dev->ep_out[ep].ifnum = 0; 481 dev->ep_in[ep].dev = dev; 482 dev->ep_out[ep].dev = dev; 483 QTAILQ_INIT(&dev->ep_in[ep].queue); 484 QTAILQ_INIT(&dev->ep_out[ep].queue); 485 } 486 } 487 488 void usb_ep_dump(USBDevice *dev) 489 { 490 static const char *tname[] = { 491 [USB_ENDPOINT_XFER_CONTROL] = "control", 492 [USB_ENDPOINT_XFER_ISOC] = "isoc", 493 [USB_ENDPOINT_XFER_BULK] = "bulk", 494 [USB_ENDPOINT_XFER_INT] = "int", 495 }; 496 int ifnum, ep, first; 497 498 fprintf(stderr, "Device \"%s\", config %d\n", 499 dev->product_desc, dev->configuration); 500 for (ifnum = 0; ifnum < 16; ifnum++) { 501 first = 1; 502 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { 503 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID && 504 dev->ep_in[ep].ifnum == ifnum) { 505 if (first) { 506 first = 0; 507 fprintf(stderr, " Interface %d, alternative %d\n", 508 ifnum, dev->altsetting[ifnum]); 509 } 510 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep, 511 tname[dev->ep_in[ep].type], 512 dev->ep_in[ep].max_packet_size); 513 } 514 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID && 515 dev->ep_out[ep].ifnum == ifnum) { 516 if (first) { 517 first = 0; 518 fprintf(stderr, " Interface %d, alternative %d\n", 519 ifnum, dev->altsetting[ifnum]); 520 } 521 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep, 522 tname[dev->ep_out[ep].type], 523 dev->ep_out[ep].max_packet_size); 524 } 525 } 526 } 527 fprintf(stderr, "--\n"); 528 } 529 530 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep) 531 { 532 struct USBEndpoint *eps; 533 534 if (dev == NULL) { 535 return NULL; 536 } 537 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out; 538 if (ep == 0) { 539 return &dev->ep_ctl; 540 } 541 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT); 542 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS); 543 return eps + ep - 1; 544 } 545 546 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep) 547 { 548 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 549 return uep->type; 550 } 551 552 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type) 553 { 554 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 555 uep->type = type; 556 } 557 558 uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep) 559 { 560 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 561 return uep->ifnum; 562 } 563 564 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum) 565 { 566 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 567 uep->ifnum = ifnum; 568 } 569 570 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep, 571 uint16_t raw) 572 { 573 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 574 int size, microframes; 575 576 size = raw & 0x7ff; 577 switch ((raw >> 11) & 3) { 578 case 1: 579 microframes = 2; 580 break; 581 case 2: 582 microframes = 3; 583 break; 584 default: 585 microframes = 1; 586 break; 587 } 588 uep->max_packet_size = size * microframes; 589 } 590 591 int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep) 592 { 593 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 594 return uep->max_packet_size; 595 } 596