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/osdep.h" 27 #include "qemu-common.h" 28 #include "hw/usb.h" 29 #include "qemu/iov.h" 30 #include "trace.h" 31 32 void usb_pick_speed(USBPort *port) 33 { 34 static const int speeds[] = { 35 USB_SPEED_SUPER, 36 USB_SPEED_HIGH, 37 USB_SPEED_FULL, 38 USB_SPEED_LOW, 39 }; 40 USBDevice *udev = port->dev; 41 int i; 42 43 for (i = 0; i < ARRAY_SIZE(speeds); i++) { 44 if ((udev->speedmask & (1 << speeds[i])) && 45 (port->speedmask & (1 << speeds[i]))) { 46 udev->speed = speeds[i]; 47 return; 48 } 49 } 50 } 51 52 void usb_attach(USBPort *port) 53 { 54 USBDevice *dev = port->dev; 55 56 assert(dev != NULL); 57 assert(dev->attached); 58 assert(dev->state == USB_STATE_NOTATTACHED); 59 usb_pick_speed(port); 60 port->ops->attach(port); 61 dev->state = USB_STATE_ATTACHED; 62 usb_device_handle_attach(dev); 63 } 64 65 void usb_detach(USBPort *port) 66 { 67 USBDevice *dev = port->dev; 68 69 assert(dev != NULL); 70 assert(dev->state != USB_STATE_NOTATTACHED); 71 port->ops->detach(port); 72 dev->state = USB_STATE_NOTATTACHED; 73 } 74 75 void usb_port_reset(USBPort *port) 76 { 77 USBDevice *dev = port->dev; 78 79 assert(dev != NULL); 80 usb_detach(port); 81 usb_attach(port); 82 usb_device_reset(dev); 83 } 84 85 void usb_device_reset(USBDevice *dev) 86 { 87 if (dev == NULL || !dev->attached) { 88 return; 89 } 90 dev->remote_wakeup = 0; 91 dev->addr = 0; 92 dev->state = USB_STATE_DEFAULT; 93 usb_device_handle_reset(dev); 94 } 95 96 void usb_wakeup(USBEndpoint *ep, unsigned int stream) 97 { 98 USBDevice *dev = ep->dev; 99 USBBus *bus = usb_bus_from_device(dev); 100 101 if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) { 102 dev->port->ops->wakeup(dev->port); 103 } 104 if (bus->ops->wakeup_endpoint) { 105 bus->ops->wakeup_endpoint(bus, ep, stream); 106 } 107 } 108 109 /**********************/ 110 111 /* generic USB device helpers (you are not forced to use them when 112 writing your USB device driver, but they help handling the 113 protocol) 114 */ 115 116 #define SETUP_STATE_IDLE 0 117 #define SETUP_STATE_SETUP 1 118 #define SETUP_STATE_DATA 2 119 #define SETUP_STATE_ACK 3 120 #define SETUP_STATE_PARAM 4 121 122 static void do_token_setup(USBDevice *s, USBPacket *p) 123 { 124 int request, value, index; 125 126 if (p->iov.size != 8) { 127 p->status = USB_RET_STALL; 128 return; 129 } 130 131 usb_packet_copy(p, s->setup_buf, p->iov.size); 132 s->setup_index = 0; 133 p->actual_length = 0; 134 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; 135 if (s->setup_len > sizeof(s->data_buf)) { 136 fprintf(stderr, 137 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n", 138 s->setup_len, sizeof(s->data_buf)); 139 p->status = USB_RET_STALL; 140 return; 141 } 142 143 request = (s->setup_buf[0] << 8) | s->setup_buf[1]; 144 value = (s->setup_buf[3] << 8) | s->setup_buf[2]; 145 index = (s->setup_buf[5] << 8) | s->setup_buf[4]; 146 147 if (s->setup_buf[0] & USB_DIR_IN) { 148 usb_device_handle_control(s, p, request, value, index, 149 s->setup_len, s->data_buf); 150 if (p->status == USB_RET_ASYNC) { 151 s->setup_state = SETUP_STATE_SETUP; 152 } 153 if (p->status != USB_RET_SUCCESS) { 154 return; 155 } 156 157 if (p->actual_length < s->setup_len) { 158 s->setup_len = p->actual_length; 159 } 160 s->setup_state = SETUP_STATE_DATA; 161 } else { 162 if (s->setup_len == 0) 163 s->setup_state = SETUP_STATE_ACK; 164 else 165 s->setup_state = SETUP_STATE_DATA; 166 } 167 168 p->actual_length = 8; 169 } 170 171 static void do_token_in(USBDevice *s, USBPacket *p) 172 { 173 int request, value, index; 174 175 assert(p->ep->nr == 0); 176 177 request = (s->setup_buf[0] << 8) | s->setup_buf[1]; 178 value = (s->setup_buf[3] << 8) | s->setup_buf[2]; 179 index = (s->setup_buf[5] << 8) | s->setup_buf[4]; 180 181 switch(s->setup_state) { 182 case SETUP_STATE_ACK: 183 if (!(s->setup_buf[0] & USB_DIR_IN)) { 184 usb_device_handle_control(s, p, request, value, index, 185 s->setup_len, s->data_buf); 186 if (p->status == USB_RET_ASYNC) { 187 return; 188 } 189 s->setup_state = SETUP_STATE_IDLE; 190 p->actual_length = 0; 191 } 192 break; 193 194 case SETUP_STATE_DATA: 195 if (s->setup_buf[0] & USB_DIR_IN) { 196 int len = s->setup_len - s->setup_index; 197 if (len > p->iov.size) { 198 len = p->iov.size; 199 } 200 usb_packet_copy(p, s->data_buf + s->setup_index, len); 201 s->setup_index += len; 202 if (s->setup_index >= s->setup_len) { 203 s->setup_state = SETUP_STATE_ACK; 204 } 205 return; 206 } 207 s->setup_state = SETUP_STATE_IDLE; 208 p->status = USB_RET_STALL; 209 break; 210 211 default: 212 p->status = USB_RET_STALL; 213 } 214 } 215 216 static void do_token_out(USBDevice *s, USBPacket *p) 217 { 218 assert(p->ep->nr == 0); 219 220 switch(s->setup_state) { 221 case SETUP_STATE_ACK: 222 if (s->setup_buf[0] & USB_DIR_IN) { 223 s->setup_state = SETUP_STATE_IDLE; 224 /* transfer OK */ 225 } else { 226 /* ignore additional output */ 227 } 228 break; 229 230 case SETUP_STATE_DATA: 231 if (!(s->setup_buf[0] & USB_DIR_IN)) { 232 int len = s->setup_len - s->setup_index; 233 if (len > p->iov.size) { 234 len = p->iov.size; 235 } 236 usb_packet_copy(p, s->data_buf + s->setup_index, len); 237 s->setup_index += len; 238 if (s->setup_index >= s->setup_len) { 239 s->setup_state = SETUP_STATE_ACK; 240 } 241 return; 242 } 243 s->setup_state = SETUP_STATE_IDLE; 244 p->status = USB_RET_STALL; 245 break; 246 247 default: 248 p->status = USB_RET_STALL; 249 } 250 } 251 252 static void do_parameter(USBDevice *s, USBPacket *p) 253 { 254 int i, request, value, index; 255 256 for (i = 0; i < 8; i++) { 257 s->setup_buf[i] = p->parameter >> (i*8); 258 } 259 260 s->setup_state = SETUP_STATE_PARAM; 261 s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6]; 262 s->setup_index = 0; 263 264 request = (s->setup_buf[0] << 8) | s->setup_buf[1]; 265 value = (s->setup_buf[3] << 8) | s->setup_buf[2]; 266 index = (s->setup_buf[5] << 8) | s->setup_buf[4]; 267 268 if (s->setup_len > sizeof(s->data_buf)) { 269 fprintf(stderr, 270 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n", 271 s->setup_len, sizeof(s->data_buf)); 272 p->status = USB_RET_STALL; 273 return; 274 } 275 276 if (p->pid == USB_TOKEN_OUT) { 277 usb_packet_copy(p, s->data_buf, s->setup_len); 278 } 279 280 usb_device_handle_control(s, p, request, value, index, 281 s->setup_len, s->data_buf); 282 if (p->status == USB_RET_ASYNC) { 283 return; 284 } 285 286 if (p->actual_length < s->setup_len) { 287 s->setup_len = p->actual_length; 288 } 289 if (p->pid == USB_TOKEN_IN) { 290 p->actual_length = 0; 291 usb_packet_copy(p, s->data_buf, s->setup_len); 292 } 293 } 294 295 /* ctrl complete function for devices which use usb_generic_handle_packet and 296 may return USB_RET_ASYNC from their handle_control callback. Device code 297 which does this *must* call this function instead of the normal 298 usb_packet_complete to complete their async control packets. */ 299 void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p) 300 { 301 if (p->status < 0) { 302 s->setup_state = SETUP_STATE_IDLE; 303 } 304 305 switch (s->setup_state) { 306 case SETUP_STATE_SETUP: 307 if (p->actual_length < s->setup_len) { 308 s->setup_len = p->actual_length; 309 } 310 s->setup_state = SETUP_STATE_DATA; 311 p->actual_length = 8; 312 break; 313 314 case SETUP_STATE_ACK: 315 s->setup_state = SETUP_STATE_IDLE; 316 p->actual_length = 0; 317 break; 318 319 case SETUP_STATE_PARAM: 320 if (p->actual_length < s->setup_len) { 321 s->setup_len = p->actual_length; 322 } 323 if (p->pid == USB_TOKEN_IN) { 324 p->actual_length = 0; 325 usb_packet_copy(p, s->data_buf, s->setup_len); 326 } 327 break; 328 329 default: 330 break; 331 } 332 usb_packet_complete(s, p); 333 } 334 335 USBDevice *usb_find_device(USBPort *port, uint8_t addr) 336 { 337 USBDevice *dev = port->dev; 338 339 if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) { 340 return NULL; 341 } 342 if (dev->addr == addr) { 343 return dev; 344 } 345 return usb_device_find_device(dev, addr); 346 } 347 348 static void usb_process_one(USBPacket *p) 349 { 350 USBDevice *dev = p->ep->dev; 351 352 /* 353 * Handlers expect status to be initialized to USB_RET_SUCCESS, but it 354 * can be USB_RET_NAK here from a previous usb_process_one() call, 355 * or USB_RET_ASYNC from going through usb_queue_one(). 356 */ 357 p->status = USB_RET_SUCCESS; 358 359 if (p->ep->nr == 0) { 360 /* control pipe */ 361 if (p->parameter) { 362 do_parameter(dev, p); 363 return; 364 } 365 switch (p->pid) { 366 case USB_TOKEN_SETUP: 367 do_token_setup(dev, p); 368 break; 369 case USB_TOKEN_IN: 370 do_token_in(dev, p); 371 break; 372 case USB_TOKEN_OUT: 373 do_token_out(dev, p); 374 break; 375 default: 376 p->status = USB_RET_STALL; 377 } 378 } else { 379 /* data pipe */ 380 usb_device_handle_data(dev, p); 381 } 382 } 383 384 static void usb_queue_one(USBPacket *p) 385 { 386 usb_packet_set_state(p, USB_PACKET_QUEUED); 387 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue); 388 p->status = USB_RET_ASYNC; 389 } 390 391 /* Hand over a packet to a device for processing. p->status == 392 USB_RET_ASYNC indicates the processing isn't finished yet, the 393 driver will call usb_packet_complete() when done processing it. */ 394 void usb_handle_packet(USBDevice *dev, USBPacket *p) 395 { 396 if (dev == NULL) { 397 p->status = USB_RET_NODEV; 398 return; 399 } 400 assert(dev == p->ep->dev); 401 assert(dev->state == USB_STATE_DEFAULT); 402 usb_packet_check_state(p, USB_PACKET_SETUP); 403 assert(p->ep != NULL); 404 405 /* Submitting a new packet clears halt */ 406 if (p->ep->halted) { 407 assert(QTAILQ_EMPTY(&p->ep->queue)); 408 p->ep->halted = false; 409 } 410 411 if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) { 412 usb_process_one(p); 413 if (p->status == USB_RET_ASYNC) { 414 /* hcd drivers cannot handle async for isoc */ 415 assert(p->ep->type != USB_ENDPOINT_XFER_ISOC); 416 /* using async for interrupt packets breaks migration */ 417 assert(p->ep->type != USB_ENDPOINT_XFER_INT || 418 (dev->flags & (1 << USB_DEV_FLAG_IS_HOST))); 419 usb_packet_set_state(p, USB_PACKET_ASYNC); 420 QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue); 421 } else if (p->status == USB_RET_ADD_TO_QUEUE) { 422 usb_queue_one(p); 423 } else { 424 /* 425 * When pipelining is enabled usb-devices must always return async, 426 * otherwise packets can complete out of order! 427 */ 428 assert(p->stream || !p->ep->pipeline || 429 QTAILQ_EMPTY(&p->ep->queue)); 430 if (p->status != USB_RET_NAK) { 431 usb_packet_set_state(p, USB_PACKET_COMPLETE); 432 } 433 } 434 } else { 435 usb_queue_one(p); 436 } 437 } 438 439 void usb_packet_complete_one(USBDevice *dev, USBPacket *p) 440 { 441 USBEndpoint *ep = p->ep; 442 443 assert(p->stream || QTAILQ_FIRST(&ep->queue) == p); 444 assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK); 445 446 if (p->status != USB_RET_SUCCESS || 447 (p->short_not_ok && (p->actual_length < p->iov.size))) { 448 ep->halted = true; 449 } 450 usb_packet_set_state(p, USB_PACKET_COMPLETE); 451 QTAILQ_REMOVE(&ep->queue, p, queue); 452 dev->port->ops->complete(dev->port, p); 453 } 454 455 /* Notify the controller that an async packet is complete. This should only 456 be called for packets previously deferred by returning USB_RET_ASYNC from 457 handle_packet. */ 458 void usb_packet_complete(USBDevice *dev, USBPacket *p) 459 { 460 USBEndpoint *ep = p->ep; 461 462 usb_packet_check_state(p, USB_PACKET_ASYNC); 463 usb_packet_complete_one(dev, p); 464 465 while (!QTAILQ_EMPTY(&ep->queue)) { 466 p = QTAILQ_FIRST(&ep->queue); 467 if (ep->halted) { 468 /* Empty the queue on a halt */ 469 p->status = USB_RET_REMOVE_FROM_QUEUE; 470 dev->port->ops->complete(dev->port, p); 471 continue; 472 } 473 if (p->state == USB_PACKET_ASYNC) { 474 break; 475 } 476 usb_packet_check_state(p, USB_PACKET_QUEUED); 477 usb_process_one(p); 478 if (p->status == USB_RET_ASYNC) { 479 usb_packet_set_state(p, USB_PACKET_ASYNC); 480 break; 481 } 482 usb_packet_complete_one(ep->dev, p); 483 } 484 } 485 486 /* Cancel an active packet. The packed must have been deferred by 487 returning USB_RET_ASYNC from handle_packet, and not yet 488 completed. */ 489 void usb_cancel_packet(USBPacket * p) 490 { 491 bool callback = (p->state == USB_PACKET_ASYNC); 492 assert(usb_packet_is_inflight(p)); 493 usb_packet_set_state(p, USB_PACKET_CANCELED); 494 QTAILQ_REMOVE(&p->ep->queue, p, queue); 495 if (callback) { 496 usb_device_cancel_packet(p->ep->dev, p); 497 } 498 } 499 500 501 void usb_packet_init(USBPacket *p) 502 { 503 qemu_iovec_init(&p->iov, 1); 504 } 505 506 static const char *usb_packet_state_name(USBPacketState state) 507 { 508 static const char *name[] = { 509 [USB_PACKET_UNDEFINED] = "undef", 510 [USB_PACKET_SETUP] = "setup", 511 [USB_PACKET_QUEUED] = "queued", 512 [USB_PACKET_ASYNC] = "async", 513 [USB_PACKET_COMPLETE] = "complete", 514 [USB_PACKET_CANCELED] = "canceled", 515 }; 516 if (state < ARRAY_SIZE(name)) { 517 return name[state]; 518 } 519 return "INVALID"; 520 } 521 522 void usb_packet_check_state(USBPacket *p, USBPacketState expected) 523 { 524 USBDevice *dev; 525 USBBus *bus; 526 527 if (p->state == expected) { 528 return; 529 } 530 dev = p->ep->dev; 531 bus = usb_bus_from_device(dev); 532 trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p, 533 usb_packet_state_name(p->state), 534 usb_packet_state_name(expected)); 535 assert(!"usb packet state check failed"); 536 } 537 538 void usb_packet_set_state(USBPacket *p, USBPacketState state) 539 { 540 if (p->ep) { 541 USBDevice *dev = p->ep->dev; 542 USBBus *bus = usb_bus_from_device(dev); 543 trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p, 544 usb_packet_state_name(p->state), 545 usb_packet_state_name(state)); 546 } else { 547 trace_usb_packet_state_change(-1, "", -1, p, 548 usb_packet_state_name(p->state), 549 usb_packet_state_name(state)); 550 } 551 p->state = state; 552 } 553 554 void usb_packet_setup(USBPacket *p, int pid, 555 USBEndpoint *ep, unsigned int stream, 556 uint64_t id, bool short_not_ok, bool int_req) 557 { 558 assert(!usb_packet_is_inflight(p)); 559 assert(p->iov.iov != NULL); 560 p->id = id; 561 p->pid = pid; 562 p->ep = ep; 563 p->stream = stream; 564 p->status = USB_RET_SUCCESS; 565 p->actual_length = 0; 566 p->parameter = 0; 567 p->short_not_ok = short_not_ok; 568 p->int_req = int_req; 569 p->combined = NULL; 570 qemu_iovec_reset(&p->iov); 571 usb_packet_set_state(p, USB_PACKET_SETUP); 572 } 573 574 void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len) 575 { 576 qemu_iovec_add(&p->iov, ptr, len); 577 } 578 579 void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes) 580 { 581 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov; 582 583 assert(p->actual_length >= 0); 584 assert(p->actual_length + bytes <= iov->size); 585 switch (p->pid) { 586 case USB_TOKEN_SETUP: 587 case USB_TOKEN_OUT: 588 iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes); 589 break; 590 case USB_TOKEN_IN: 591 iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes); 592 break; 593 default: 594 fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid); 595 abort(); 596 } 597 p->actual_length += bytes; 598 } 599 600 void usb_packet_skip(USBPacket *p, size_t bytes) 601 { 602 QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov; 603 604 assert(p->actual_length >= 0); 605 assert(p->actual_length + bytes <= iov->size); 606 if (p->pid == USB_TOKEN_IN) { 607 iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes); 608 } 609 p->actual_length += bytes; 610 } 611 612 size_t usb_packet_size(USBPacket *p) 613 { 614 return p->combined ? p->combined->iov.size : p->iov.size; 615 } 616 617 void usb_packet_cleanup(USBPacket *p) 618 { 619 assert(!usb_packet_is_inflight(p)); 620 qemu_iovec_destroy(&p->iov); 621 } 622 623 void usb_ep_reset(USBDevice *dev) 624 { 625 int ep; 626 627 dev->ep_ctl.nr = 0; 628 dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL; 629 dev->ep_ctl.ifnum = 0; 630 dev->ep_ctl.max_packet_size = 64; 631 dev->ep_ctl.max_streams = 0; 632 dev->ep_ctl.dev = dev; 633 dev->ep_ctl.pipeline = false; 634 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { 635 dev->ep_in[ep].nr = ep + 1; 636 dev->ep_out[ep].nr = ep + 1; 637 dev->ep_in[ep].pid = USB_TOKEN_IN; 638 dev->ep_out[ep].pid = USB_TOKEN_OUT; 639 dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID; 640 dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID; 641 dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID; 642 dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID; 643 dev->ep_in[ep].max_packet_size = 0; 644 dev->ep_out[ep].max_packet_size = 0; 645 dev->ep_in[ep].max_streams = 0; 646 dev->ep_out[ep].max_streams = 0; 647 dev->ep_in[ep].dev = dev; 648 dev->ep_out[ep].dev = dev; 649 dev->ep_in[ep].pipeline = false; 650 dev->ep_out[ep].pipeline = false; 651 } 652 } 653 654 void usb_ep_init(USBDevice *dev) 655 { 656 int ep; 657 658 usb_ep_reset(dev); 659 QTAILQ_INIT(&dev->ep_ctl.queue); 660 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { 661 QTAILQ_INIT(&dev->ep_in[ep].queue); 662 QTAILQ_INIT(&dev->ep_out[ep].queue); 663 } 664 } 665 666 void usb_ep_dump(USBDevice *dev) 667 { 668 static const char *tname[] = { 669 [USB_ENDPOINT_XFER_CONTROL] = "control", 670 [USB_ENDPOINT_XFER_ISOC] = "isoc", 671 [USB_ENDPOINT_XFER_BULK] = "bulk", 672 [USB_ENDPOINT_XFER_INT] = "int", 673 }; 674 int ifnum, ep, first; 675 676 fprintf(stderr, "Device \"%s\", config %d\n", 677 dev->product_desc, dev->configuration); 678 for (ifnum = 0; ifnum < 16; ifnum++) { 679 first = 1; 680 for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) { 681 if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID && 682 dev->ep_in[ep].ifnum == ifnum) { 683 if (first) { 684 first = 0; 685 fprintf(stderr, " Interface %d, alternative %d\n", 686 ifnum, dev->altsetting[ifnum]); 687 } 688 fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep, 689 tname[dev->ep_in[ep].type], 690 dev->ep_in[ep].max_packet_size); 691 } 692 if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID && 693 dev->ep_out[ep].ifnum == ifnum) { 694 if (first) { 695 first = 0; 696 fprintf(stderr, " Interface %d, alternative %d\n", 697 ifnum, dev->altsetting[ifnum]); 698 } 699 fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep, 700 tname[dev->ep_out[ep].type], 701 dev->ep_out[ep].max_packet_size); 702 } 703 } 704 } 705 fprintf(stderr, "--\n"); 706 } 707 708 struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep) 709 { 710 struct USBEndpoint *eps; 711 712 if (dev == NULL) { 713 return NULL; 714 } 715 eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out; 716 if (ep == 0) { 717 return &dev->ep_ctl; 718 } 719 assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT); 720 assert(ep > 0 && ep <= USB_MAX_ENDPOINTS); 721 return eps + ep - 1; 722 } 723 724 uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep) 725 { 726 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 727 return uep->type; 728 } 729 730 void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type) 731 { 732 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 733 uep->type = type; 734 } 735 736 void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum) 737 { 738 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 739 uep->ifnum = ifnum; 740 } 741 742 void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep, 743 uint16_t raw) 744 { 745 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 746 int size, microframes; 747 748 size = raw & 0x7ff; 749 switch ((raw >> 11) & 3) { 750 case 1: 751 microframes = 2; 752 break; 753 case 2: 754 microframes = 3; 755 break; 756 default: 757 microframes = 1; 758 break; 759 } 760 uep->max_packet_size = size * microframes; 761 } 762 763 void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw) 764 { 765 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 766 int MaxStreams; 767 768 MaxStreams = raw & 0x1f; 769 if (MaxStreams) { 770 uep->max_streams = 1 << MaxStreams; 771 } else { 772 uep->max_streams = 0; 773 } 774 } 775 776 void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted) 777 { 778 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 779 uep->halted = halted; 780 } 781 782 USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep, 783 uint64_t id) 784 { 785 struct USBEndpoint *uep = usb_ep_get(dev, pid, ep); 786 USBPacket *p; 787 788 QTAILQ_FOREACH(p, &uep->queue, queue) { 789 if (p->id == id) { 790 return p; 791 } 792 } 793 794 return NULL; 795 } 796