1 /* 2 * QEMU USB EHCI Emulation 3 * 4 * Copyright(c) 2008 Emutex Ltd. (address@hidden) 5 * Copyright(c) 2011-2012 Red Hat, Inc. 6 * 7 * Red Hat Authors: 8 * Gerd Hoffmann <kraxel@redhat.com> 9 * Hans de Goede <hdegoede@redhat.com> 10 * 11 * EHCI project was started by Mark Burkley, with contributions by 12 * Niels de Vos. David S. Ahern continued working on it. Kevin Wolf, 13 * Jan Kiszka and Vincent Palatin contributed bugfixes. 14 * 15 * This library is free software; you can redistribute it and/or 16 * modify it under the terms of the GNU Lesser General Public 17 * License as published by the Free Software Foundation; either 18 * version 2.1 of the License, or (at your option) any later version. 19 * 20 * This library is distributed in the hope that it will be useful, 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 * Lesser General Public License for more details. 24 * 25 * You should have received a copy of the GNU Lesser General Public License 26 * along with this program; if not, see <http://www.gnu.org/licenses/>. 27 */ 28 29 #include "qemu/osdep.h" 30 #include "qapi/error.h" 31 #include "hw/irq.h" 32 #include "hw/usb/ehci-regs.h" 33 #include "hw/usb/hcd-ehci.h" 34 #include "migration/vmstate.h" 35 #include "trace.h" 36 #include "qemu/error-report.h" 37 38 #define FRAME_TIMER_FREQ 1000 39 #define FRAME_TIMER_NS (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ) 40 #define UFRAME_TIMER_NS (FRAME_TIMER_NS / 8) 41 42 #define NB_MAXINTRATE 8 // Max rate at which controller issues ints 43 #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction 44 #define MAX_QH 100 // Max allowable queue heads in a chain 45 #define MIN_UFR_PER_TICK 24 /* Min frames to process when catching up */ 46 #define PERIODIC_ACTIVE 512 /* Micro-frames */ 47 48 /* Internal periodic / asynchronous schedule state machine states 49 */ 50 typedef enum { 51 EST_INACTIVE = 1000, 52 EST_ACTIVE, 53 EST_EXECUTING, 54 EST_SLEEPING, 55 /* The following states are internal to the state machine function 56 */ 57 EST_WAITLISTHEAD, 58 EST_FETCHENTRY, 59 EST_FETCHQH, 60 EST_FETCHITD, 61 EST_FETCHSITD, 62 EST_ADVANCEQUEUE, 63 EST_FETCHQTD, 64 EST_EXECUTE, 65 EST_WRITEBACK, 66 EST_HORIZONTALQH 67 } EHCI_STATES; 68 69 /* macros for accessing fields within next link pointer entry */ 70 #define NLPTR_GET(x) ((x) & 0xffffffe0) 71 #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3) 72 #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid 73 74 /* link pointer types */ 75 #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor 76 #define NLPTR_TYPE_QH 1 // queue head 77 #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor 78 #define NLPTR_TYPE_FSTN 3 // frame span traversal node 79 80 #define SET_LAST_RUN_CLOCK(s) \ 81 (s)->last_run_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 82 83 /* nifty macros from Arnon's EHCI version */ 84 #define get_field(data, field) \ 85 (((data) & field##_MASK) >> field##_SH) 86 87 #define set_field(data, newval, field) do { \ 88 uint32_t val = *data; \ 89 val &= ~ field##_MASK; \ 90 val |= ((newval) << field##_SH) & field##_MASK; \ 91 *data = val; \ 92 } while(0) 93 94 static const char *ehci_state_names[] = { 95 [EST_INACTIVE] = "INACTIVE", 96 [EST_ACTIVE] = "ACTIVE", 97 [EST_EXECUTING] = "EXECUTING", 98 [EST_SLEEPING] = "SLEEPING", 99 [EST_WAITLISTHEAD] = "WAITLISTHEAD", 100 [EST_FETCHENTRY] = "FETCH ENTRY", 101 [EST_FETCHQH] = "FETCH QH", 102 [EST_FETCHITD] = "FETCH ITD", 103 [EST_ADVANCEQUEUE] = "ADVANCEQUEUE", 104 [EST_FETCHQTD] = "FETCH QTD", 105 [EST_EXECUTE] = "EXECUTE", 106 [EST_WRITEBACK] = "WRITEBACK", 107 [EST_HORIZONTALQH] = "HORIZONTALQH", 108 }; 109 110 static const char *ehci_mmio_names[] = { 111 [USBCMD] = "USBCMD", 112 [USBSTS] = "USBSTS", 113 [USBINTR] = "USBINTR", 114 [FRINDEX] = "FRINDEX", 115 [PERIODICLISTBASE] = "P-LIST BASE", 116 [ASYNCLISTADDR] = "A-LIST ADDR", 117 [CONFIGFLAG] = "CONFIGFLAG", 118 }; 119 120 static int ehci_state_executing(EHCIQueue *q); 121 static int ehci_state_writeback(EHCIQueue *q); 122 static int ehci_state_advqueue(EHCIQueue *q); 123 static int ehci_fill_queue(EHCIPacket *p); 124 static void ehci_free_packet(EHCIPacket *p); 125 126 static const char *nr2str(const char **n, size_t len, uint32_t nr) 127 { 128 if (nr < len && n[nr] != NULL) { 129 return n[nr]; 130 } else { 131 return "unknown"; 132 } 133 } 134 135 static const char *state2str(uint32_t state) 136 { 137 return nr2str(ehci_state_names, ARRAY_SIZE(ehci_state_names), state); 138 } 139 140 static const char *addr2str(hwaddr addr) 141 { 142 return nr2str(ehci_mmio_names, ARRAY_SIZE(ehci_mmio_names), addr); 143 } 144 145 static void ehci_trace_usbsts(uint32_t mask, int state) 146 { 147 /* interrupts */ 148 if (mask & USBSTS_INT) { 149 trace_usb_ehci_usbsts("INT", state); 150 } 151 if (mask & USBSTS_ERRINT) { 152 trace_usb_ehci_usbsts("ERRINT", state); 153 } 154 if (mask & USBSTS_PCD) { 155 trace_usb_ehci_usbsts("PCD", state); 156 } 157 if (mask & USBSTS_FLR) { 158 trace_usb_ehci_usbsts("FLR", state); 159 } 160 if (mask & USBSTS_HSE) { 161 trace_usb_ehci_usbsts("HSE", state); 162 } 163 if (mask & USBSTS_IAA) { 164 trace_usb_ehci_usbsts("IAA", state); 165 } 166 167 /* status */ 168 if (mask & USBSTS_HALT) { 169 trace_usb_ehci_usbsts("HALT", state); 170 } 171 if (mask & USBSTS_REC) { 172 trace_usb_ehci_usbsts("REC", state); 173 } 174 if (mask & USBSTS_PSS) { 175 trace_usb_ehci_usbsts("PSS", state); 176 } 177 if (mask & USBSTS_ASS) { 178 trace_usb_ehci_usbsts("ASS", state); 179 } 180 } 181 182 static inline void ehci_set_usbsts(EHCIState *s, int mask) 183 { 184 if ((s->usbsts & mask) == mask) { 185 return; 186 } 187 ehci_trace_usbsts(mask, 1); 188 s->usbsts |= mask; 189 } 190 191 static inline void ehci_clear_usbsts(EHCIState *s, int mask) 192 { 193 if ((s->usbsts & mask) == 0) { 194 return; 195 } 196 ehci_trace_usbsts(mask, 0); 197 s->usbsts &= ~mask; 198 } 199 200 /* update irq line */ 201 static inline void ehci_update_irq(EHCIState *s) 202 { 203 int level = 0; 204 205 if ((s->usbsts & USBINTR_MASK) & s->usbintr) { 206 level = 1; 207 } 208 209 trace_usb_ehci_irq(level, s->frindex, s->usbsts, s->usbintr); 210 qemu_set_irq(s->irq, level); 211 } 212 213 /* flag interrupt condition */ 214 static inline void ehci_raise_irq(EHCIState *s, int intr) 215 { 216 if (intr & (USBSTS_PCD | USBSTS_FLR | USBSTS_HSE)) { 217 s->usbsts |= intr; 218 ehci_update_irq(s); 219 } else { 220 s->usbsts_pending |= intr; 221 } 222 } 223 224 /* 225 * Commit pending interrupts (added via ehci_raise_irq), 226 * at the rate allowed by "Interrupt Threshold Control". 227 */ 228 static inline void ehci_commit_irq(EHCIState *s) 229 { 230 uint32_t itc; 231 232 if (!s->usbsts_pending) { 233 return; 234 } 235 if (s->usbsts_frindex > s->frindex) { 236 return; 237 } 238 239 itc = (s->usbcmd >> 16) & 0xff; 240 s->usbsts |= s->usbsts_pending; 241 s->usbsts_pending = 0; 242 s->usbsts_frindex = s->frindex + itc; 243 ehci_update_irq(s); 244 } 245 246 static void ehci_update_halt(EHCIState *s) 247 { 248 if (s->usbcmd & USBCMD_RUNSTOP) { 249 ehci_clear_usbsts(s, USBSTS_HALT); 250 } else { 251 if (s->astate == EST_INACTIVE && s->pstate == EST_INACTIVE) { 252 ehci_set_usbsts(s, USBSTS_HALT); 253 } 254 } 255 } 256 257 static void ehci_set_state(EHCIState *s, int async, int state) 258 { 259 if (async) { 260 trace_usb_ehci_state("async", state2str(state)); 261 s->astate = state; 262 if (s->astate == EST_INACTIVE) { 263 ehci_clear_usbsts(s, USBSTS_ASS); 264 ehci_update_halt(s); 265 } else { 266 ehci_set_usbsts(s, USBSTS_ASS); 267 } 268 } else { 269 trace_usb_ehci_state("periodic", state2str(state)); 270 s->pstate = state; 271 if (s->pstate == EST_INACTIVE) { 272 ehci_clear_usbsts(s, USBSTS_PSS); 273 ehci_update_halt(s); 274 } else { 275 ehci_set_usbsts(s, USBSTS_PSS); 276 } 277 } 278 } 279 280 static int ehci_get_state(EHCIState *s, int async) 281 { 282 return async ? s->astate : s->pstate; 283 } 284 285 static void ehci_set_fetch_addr(EHCIState *s, int async, uint32_t addr) 286 { 287 if (async) { 288 s->a_fetch_addr = addr; 289 } else { 290 s->p_fetch_addr = addr; 291 } 292 } 293 294 static int ehci_get_fetch_addr(EHCIState *s, int async) 295 { 296 return async ? s->a_fetch_addr : s->p_fetch_addr; 297 } 298 299 static void ehci_trace_qh(EHCIQueue *q, hwaddr addr, EHCIqh *qh) 300 { 301 /* need three here due to argument count limits */ 302 trace_usb_ehci_qh_ptrs(q, addr, qh->next, 303 qh->current_qtd, qh->next_qtd, qh->altnext_qtd); 304 trace_usb_ehci_qh_fields(addr, 305 get_field(qh->epchar, QH_EPCHAR_RL), 306 get_field(qh->epchar, QH_EPCHAR_MPLEN), 307 get_field(qh->epchar, QH_EPCHAR_EPS), 308 get_field(qh->epchar, QH_EPCHAR_EP), 309 get_field(qh->epchar, QH_EPCHAR_DEVADDR)); 310 trace_usb_ehci_qh_bits(addr, 311 (bool)(qh->epchar & QH_EPCHAR_C), 312 (bool)(qh->epchar & QH_EPCHAR_H), 313 (bool)(qh->epchar & QH_EPCHAR_DTC), 314 (bool)(qh->epchar & QH_EPCHAR_I)); 315 } 316 317 static void ehci_trace_qtd(EHCIQueue *q, hwaddr addr, EHCIqtd *qtd) 318 { 319 /* need three here due to argument count limits */ 320 trace_usb_ehci_qtd_ptrs(q, addr, qtd->next, qtd->altnext); 321 trace_usb_ehci_qtd_fields(addr, 322 get_field(qtd->token, QTD_TOKEN_TBYTES), 323 get_field(qtd->token, QTD_TOKEN_CPAGE), 324 get_field(qtd->token, QTD_TOKEN_CERR), 325 get_field(qtd->token, QTD_TOKEN_PID)); 326 trace_usb_ehci_qtd_bits(addr, 327 (bool)(qtd->token & QTD_TOKEN_IOC), 328 (bool)(qtd->token & QTD_TOKEN_ACTIVE), 329 (bool)(qtd->token & QTD_TOKEN_HALT), 330 (bool)(qtd->token & QTD_TOKEN_BABBLE), 331 (bool)(qtd->token & QTD_TOKEN_XACTERR)); 332 } 333 334 static void ehci_trace_itd(EHCIState *s, hwaddr addr, EHCIitd *itd) 335 { 336 trace_usb_ehci_itd(addr, itd->next, 337 get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT), 338 get_field(itd->bufptr[2], ITD_BUFPTR_MULT), 339 get_field(itd->bufptr[0], ITD_BUFPTR_EP), 340 get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR)); 341 } 342 343 static void ehci_trace_sitd(EHCIState *s, hwaddr addr, 344 EHCIsitd *sitd) 345 { 346 trace_usb_ehci_sitd(addr, sitd->next, 347 (bool)(sitd->results & SITD_RESULTS_ACTIVE)); 348 } 349 350 static void ehci_trace_guest_bug(EHCIState *s, const char *message) 351 { 352 trace_usb_ehci_guest_bug(message); 353 warn_report("%s", message); 354 } 355 356 static inline bool ehci_enabled(EHCIState *s) 357 { 358 return s->usbcmd & USBCMD_RUNSTOP; 359 } 360 361 static inline bool ehci_async_enabled(EHCIState *s) 362 { 363 return ehci_enabled(s) && (s->usbcmd & USBCMD_ASE); 364 } 365 366 static inline bool ehci_periodic_enabled(EHCIState *s) 367 { 368 return ehci_enabled(s) && (s->usbcmd & USBCMD_PSE); 369 } 370 371 /* Get an array of dwords from main memory */ 372 static inline int get_dwords(EHCIState *ehci, uint32_t addr, 373 uint32_t *buf, int num) 374 { 375 int i; 376 377 if (!ehci->as) { 378 ehci_raise_irq(ehci, USBSTS_HSE); 379 ehci->usbcmd &= ~USBCMD_RUNSTOP; 380 trace_usb_ehci_dma_error(); 381 return -1; 382 } 383 384 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { 385 dma_memory_read(ehci->as, addr, buf, sizeof(*buf)); 386 *buf = le32_to_cpu(*buf); 387 } 388 389 return num; 390 } 391 392 /* Put an array of dwords in to main memory */ 393 static inline int put_dwords(EHCIState *ehci, uint32_t addr, 394 uint32_t *buf, int num) 395 { 396 int i; 397 398 if (!ehci->as) { 399 ehci_raise_irq(ehci, USBSTS_HSE); 400 ehci->usbcmd &= ~USBCMD_RUNSTOP; 401 trace_usb_ehci_dma_error(); 402 return -1; 403 } 404 405 for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) { 406 uint32_t tmp = cpu_to_le32(*buf); 407 dma_memory_write(ehci->as, addr, &tmp, sizeof(tmp)); 408 } 409 410 return num; 411 } 412 413 static int ehci_get_pid(EHCIqtd *qtd) 414 { 415 switch (get_field(qtd->token, QTD_TOKEN_PID)) { 416 case 0: 417 return USB_TOKEN_OUT; 418 case 1: 419 return USB_TOKEN_IN; 420 case 2: 421 return USB_TOKEN_SETUP; 422 default: 423 fprintf(stderr, "bad token\n"); 424 return 0; 425 } 426 } 427 428 static bool ehci_verify_qh(EHCIQueue *q, EHCIqh *qh) 429 { 430 uint32_t devaddr = get_field(qh->epchar, QH_EPCHAR_DEVADDR); 431 uint32_t endp = get_field(qh->epchar, QH_EPCHAR_EP); 432 if ((devaddr != get_field(q->qh.epchar, QH_EPCHAR_DEVADDR)) || 433 (endp != get_field(q->qh.epchar, QH_EPCHAR_EP)) || 434 (qh->current_qtd != q->qh.current_qtd) || 435 (q->async && qh->next_qtd != q->qh.next_qtd) || 436 (memcmp(&qh->altnext_qtd, &q->qh.altnext_qtd, 437 7 * sizeof(uint32_t)) != 0) || 438 (q->dev != NULL && q->dev->addr != devaddr)) { 439 return false; 440 } else { 441 return true; 442 } 443 } 444 445 static bool ehci_verify_qtd(EHCIPacket *p, EHCIqtd *qtd) 446 { 447 if (p->qtdaddr != p->queue->qtdaddr || 448 (p->queue->async && !NLPTR_TBIT(p->qtd.next) && 449 (p->qtd.next != qtd->next)) || 450 (!NLPTR_TBIT(p->qtd.altnext) && (p->qtd.altnext != qtd->altnext)) || 451 p->qtd.token != qtd->token || 452 p->qtd.bufptr[0] != qtd->bufptr[0]) { 453 return false; 454 } else { 455 return true; 456 } 457 } 458 459 static bool ehci_verify_pid(EHCIQueue *q, EHCIqtd *qtd) 460 { 461 int ep = get_field(q->qh.epchar, QH_EPCHAR_EP); 462 int pid = ehci_get_pid(qtd); 463 464 /* Note the pid changing is normal for ep 0 (the control ep) */ 465 if (q->last_pid && ep != 0 && pid != q->last_pid) { 466 return false; 467 } else { 468 return true; 469 } 470 } 471 472 /* Finish executing and writeback a packet outside of the regular 473 fetchqh -> fetchqtd -> execute -> writeback cycle */ 474 static void ehci_writeback_async_complete_packet(EHCIPacket *p) 475 { 476 EHCIQueue *q = p->queue; 477 EHCIqtd qtd; 478 EHCIqh qh; 479 int state; 480 481 /* Verify the qh + qtd, like we do when going through fetchqh & fetchqtd */ 482 get_dwords(q->ehci, NLPTR_GET(q->qhaddr), 483 (uint32_t *) &qh, sizeof(EHCIqh) >> 2); 484 get_dwords(q->ehci, NLPTR_GET(q->qtdaddr), 485 (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2); 486 if (!ehci_verify_qh(q, &qh) || !ehci_verify_qtd(p, &qtd)) { 487 p->async = EHCI_ASYNC_INITIALIZED; 488 ehci_free_packet(p); 489 return; 490 } 491 492 state = ehci_get_state(q->ehci, q->async); 493 ehci_state_executing(q); 494 ehci_state_writeback(q); /* Frees the packet! */ 495 if (!(q->qh.token & QTD_TOKEN_HALT)) { 496 ehci_state_advqueue(q); 497 } 498 ehci_set_state(q->ehci, q->async, state); 499 } 500 501 /* packet management */ 502 503 static EHCIPacket *ehci_alloc_packet(EHCIQueue *q) 504 { 505 EHCIPacket *p; 506 507 p = g_new0(EHCIPacket, 1); 508 p->queue = q; 509 usb_packet_init(&p->packet); 510 QTAILQ_INSERT_TAIL(&q->packets, p, next); 511 trace_usb_ehci_packet_action(p->queue, p, "alloc"); 512 return p; 513 } 514 515 static void ehci_free_packet(EHCIPacket *p) 516 { 517 if (p->async == EHCI_ASYNC_FINISHED && 518 !(p->queue->qh.token & QTD_TOKEN_HALT)) { 519 ehci_writeback_async_complete_packet(p); 520 return; 521 } 522 trace_usb_ehci_packet_action(p->queue, p, "free"); 523 if (p->async == EHCI_ASYNC_INFLIGHT) { 524 usb_cancel_packet(&p->packet); 525 } 526 if (p->async == EHCI_ASYNC_FINISHED && 527 p->packet.status == USB_RET_SUCCESS) { 528 fprintf(stderr, 529 "EHCI: Dropping completed packet from halted %s ep %02X\n", 530 (p->pid == USB_TOKEN_IN) ? "in" : "out", 531 get_field(p->queue->qh.epchar, QH_EPCHAR_EP)); 532 } 533 if (p->async != EHCI_ASYNC_NONE) { 534 usb_packet_unmap(&p->packet, &p->sgl); 535 qemu_sglist_destroy(&p->sgl); 536 } 537 QTAILQ_REMOVE(&p->queue->packets, p, next); 538 usb_packet_cleanup(&p->packet); 539 g_free(p); 540 } 541 542 /* queue management */ 543 544 static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, uint32_t addr, int async) 545 { 546 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; 547 EHCIQueue *q; 548 549 q = g_malloc0(sizeof(*q)); 550 q->ehci = ehci; 551 q->qhaddr = addr; 552 q->async = async; 553 QTAILQ_INIT(&q->packets); 554 QTAILQ_INSERT_HEAD(head, q, next); 555 trace_usb_ehci_queue_action(q, "alloc"); 556 return q; 557 } 558 559 static void ehci_queue_stopped(EHCIQueue *q) 560 { 561 int endp = get_field(q->qh.epchar, QH_EPCHAR_EP); 562 563 if (!q->last_pid || !q->dev) { 564 return; 565 } 566 567 usb_device_ep_stopped(q->dev, usb_ep_get(q->dev, q->last_pid, endp)); 568 } 569 570 static int ehci_cancel_queue(EHCIQueue *q) 571 { 572 EHCIPacket *p; 573 int packets = 0; 574 575 p = QTAILQ_FIRST(&q->packets); 576 if (p == NULL) { 577 goto leave; 578 } 579 580 trace_usb_ehci_queue_action(q, "cancel"); 581 do { 582 ehci_free_packet(p); 583 packets++; 584 } while ((p = QTAILQ_FIRST(&q->packets)) != NULL); 585 586 leave: 587 ehci_queue_stopped(q); 588 return packets; 589 } 590 591 static int ehci_reset_queue(EHCIQueue *q) 592 { 593 int packets; 594 595 trace_usb_ehci_queue_action(q, "reset"); 596 packets = ehci_cancel_queue(q); 597 q->dev = NULL; 598 q->qtdaddr = 0; 599 q->last_pid = 0; 600 return packets; 601 } 602 603 static void ehci_free_queue(EHCIQueue *q, const char *warn) 604 { 605 EHCIQueueHead *head = q->async ? &q->ehci->aqueues : &q->ehci->pqueues; 606 int cancelled; 607 608 trace_usb_ehci_queue_action(q, "free"); 609 cancelled = ehci_cancel_queue(q); 610 if (warn && cancelled > 0) { 611 ehci_trace_guest_bug(q->ehci, warn); 612 } 613 QTAILQ_REMOVE(head, q, next); 614 g_free(q); 615 } 616 617 static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr, 618 int async) 619 { 620 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; 621 EHCIQueue *q; 622 623 QTAILQ_FOREACH(q, head, next) { 624 if (addr == q->qhaddr) { 625 return q; 626 } 627 } 628 return NULL; 629 } 630 631 static void ehci_queues_rip_unused(EHCIState *ehci, int async) 632 { 633 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; 634 const char *warn = async ? "guest unlinked busy QH" : NULL; 635 uint64_t maxage = FRAME_TIMER_NS * ehci->maxframes * 4; 636 EHCIQueue *q, *tmp; 637 638 QTAILQ_FOREACH_SAFE(q, head, next, tmp) { 639 if (q->seen) { 640 q->seen = 0; 641 q->ts = ehci->last_run_ns; 642 continue; 643 } 644 if (ehci->last_run_ns < q->ts + maxage) { 645 continue; 646 } 647 ehci_free_queue(q, warn); 648 } 649 } 650 651 static void ehci_queues_rip_unseen(EHCIState *ehci, int async) 652 { 653 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; 654 EHCIQueue *q, *tmp; 655 656 QTAILQ_FOREACH_SAFE(q, head, next, tmp) { 657 if (!q->seen) { 658 ehci_free_queue(q, NULL); 659 } 660 } 661 } 662 663 static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev, int async) 664 { 665 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; 666 EHCIQueue *q, *tmp; 667 668 QTAILQ_FOREACH_SAFE(q, head, next, tmp) { 669 if (q->dev != dev) { 670 continue; 671 } 672 ehci_free_queue(q, NULL); 673 } 674 } 675 676 static void ehci_queues_rip_all(EHCIState *ehci, int async) 677 { 678 EHCIQueueHead *head = async ? &ehci->aqueues : &ehci->pqueues; 679 const char *warn = async ? "guest stopped busy async schedule" : NULL; 680 EHCIQueue *q, *tmp; 681 682 QTAILQ_FOREACH_SAFE(q, head, next, tmp) { 683 ehci_free_queue(q, warn); 684 } 685 } 686 687 /* Attach or detach a device on root hub */ 688 689 static void ehci_attach(USBPort *port) 690 { 691 EHCIState *s = port->opaque; 692 uint32_t *portsc = &s->portsc[port->index]; 693 const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci"; 694 695 trace_usb_ehci_port_attach(port->index, owner, port->dev->product_desc); 696 697 if (*portsc & PORTSC_POWNER) { 698 USBPort *companion = s->companion_ports[port->index]; 699 companion->dev = port->dev; 700 companion->ops->attach(companion); 701 return; 702 } 703 704 *portsc |= PORTSC_CONNECT; 705 *portsc |= PORTSC_CSC; 706 707 ehci_raise_irq(s, USBSTS_PCD); 708 } 709 710 static void ehci_detach(USBPort *port) 711 { 712 EHCIState *s = port->opaque; 713 uint32_t *portsc = &s->portsc[port->index]; 714 const char *owner = (*portsc & PORTSC_POWNER) ? "comp" : "ehci"; 715 716 trace_usb_ehci_port_detach(port->index, owner); 717 718 if (*portsc & PORTSC_POWNER) { 719 USBPort *companion = s->companion_ports[port->index]; 720 companion->ops->detach(companion); 721 companion->dev = NULL; 722 /* 723 * EHCI spec 4.2.2: "When a disconnect occurs... On the event, 724 * the port ownership is returned immediately to the EHCI controller." 725 */ 726 *portsc &= ~PORTSC_POWNER; 727 return; 728 } 729 730 ehci_queues_rip_device(s, port->dev, 0); 731 ehci_queues_rip_device(s, port->dev, 1); 732 733 *portsc &= ~(PORTSC_CONNECT|PORTSC_PED|PORTSC_SUSPEND); 734 *portsc |= PORTSC_CSC; 735 736 ehci_raise_irq(s, USBSTS_PCD); 737 } 738 739 static void ehci_child_detach(USBPort *port, USBDevice *child) 740 { 741 EHCIState *s = port->opaque; 742 uint32_t portsc = s->portsc[port->index]; 743 744 if (portsc & PORTSC_POWNER) { 745 USBPort *companion = s->companion_ports[port->index]; 746 companion->ops->child_detach(companion, child); 747 return; 748 } 749 750 ehci_queues_rip_device(s, child, 0); 751 ehci_queues_rip_device(s, child, 1); 752 } 753 754 static void ehci_wakeup(USBPort *port) 755 { 756 EHCIState *s = port->opaque; 757 uint32_t *portsc = &s->portsc[port->index]; 758 759 if (*portsc & PORTSC_POWNER) { 760 USBPort *companion = s->companion_ports[port->index]; 761 if (companion->ops->wakeup) { 762 companion->ops->wakeup(companion); 763 } 764 return; 765 } 766 767 if (*portsc & PORTSC_SUSPEND) { 768 trace_usb_ehci_port_wakeup(port->index); 769 *portsc |= PORTSC_FPRES; 770 ehci_raise_irq(s, USBSTS_PCD); 771 } 772 773 qemu_bh_schedule(s->async_bh); 774 } 775 776 static void ehci_register_companion(USBBus *bus, USBPort *ports[], 777 uint32_t portcount, uint32_t firstport, 778 Error **errp) 779 { 780 EHCIState *s = container_of(bus, EHCIState, bus); 781 uint32_t i; 782 783 if (firstport + portcount > NB_PORTS) { 784 error_setg(errp, "firstport must be between 0 and %u", 785 NB_PORTS - portcount); 786 return; 787 } 788 789 for (i = 0; i < portcount; i++) { 790 if (s->companion_ports[firstport + i]) { 791 error_setg(errp, "firstport %u asks for ports %u-%u," 792 " but port %u has a companion assigned already", 793 firstport, firstport, firstport + portcount - 1, 794 firstport + i); 795 return; 796 } 797 } 798 799 for (i = 0; i < portcount; i++) { 800 s->companion_ports[firstport + i] = ports[i]; 801 s->ports[firstport + i].speedmask |= 802 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL; 803 /* Ensure devs attached before the initial reset go to the companion */ 804 s->portsc[firstport + i] = PORTSC_POWNER; 805 } 806 807 s->companion_count++; 808 s->caps[0x05] = (s->companion_count << 4) | portcount; 809 } 810 811 static void ehci_wakeup_endpoint(USBBus *bus, USBEndpoint *ep, 812 unsigned int stream) 813 { 814 EHCIState *s = container_of(bus, EHCIState, bus); 815 uint32_t portsc = s->portsc[ep->dev->port->index]; 816 817 if (portsc & PORTSC_POWNER) { 818 return; 819 } 820 821 s->periodic_sched_active = PERIODIC_ACTIVE; 822 qemu_bh_schedule(s->async_bh); 823 } 824 825 static USBDevice *ehci_find_device(EHCIState *ehci, uint8_t addr) 826 { 827 USBDevice *dev; 828 USBPort *port; 829 int i; 830 831 for (i = 0; i < NB_PORTS; i++) { 832 port = &ehci->ports[i]; 833 if (!(ehci->portsc[i] & PORTSC_PED)) { 834 DPRINTF("Port %d not enabled\n", i); 835 continue; 836 } 837 dev = usb_find_device(port, addr); 838 if (dev != NULL) { 839 return dev; 840 } 841 } 842 return NULL; 843 } 844 845 /* 4.1 host controller initialization */ 846 void ehci_reset(void *opaque) 847 { 848 EHCIState *s = opaque; 849 int i; 850 USBDevice *devs[NB_PORTS]; 851 852 trace_usb_ehci_reset(); 853 854 /* 855 * Do the detach before touching portsc, so that it correctly gets send to 856 * us or to our companion based on PORTSC_POWNER before the reset. 857 */ 858 for(i = 0; i < NB_PORTS; i++) { 859 devs[i] = s->ports[i].dev; 860 if (devs[i] && devs[i]->attached) { 861 usb_detach(&s->ports[i]); 862 } 863 } 864 865 memset(&s->opreg, 0x00, sizeof(s->opreg)); 866 memset(&s->portsc, 0x00, sizeof(s->portsc)); 867 868 s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH; 869 s->usbsts = USBSTS_HALT; 870 s->usbsts_pending = 0; 871 s->usbsts_frindex = 0; 872 ehci_update_irq(s); 873 874 s->astate = EST_INACTIVE; 875 s->pstate = EST_INACTIVE; 876 877 for(i = 0; i < NB_PORTS; i++) { 878 if (s->companion_ports[i]) { 879 s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER; 880 } else { 881 s->portsc[i] = PORTSC_PPOWER; 882 } 883 if (devs[i] && devs[i]->attached) { 884 usb_attach(&s->ports[i]); 885 usb_device_reset(devs[i]); 886 } 887 } 888 ehci_queues_rip_all(s, 0); 889 ehci_queues_rip_all(s, 1); 890 timer_del(s->frame_timer); 891 qemu_bh_cancel(s->async_bh); 892 } 893 894 static uint64_t ehci_caps_read(void *ptr, hwaddr addr, 895 unsigned size) 896 { 897 EHCIState *s = ptr; 898 return s->caps[addr]; 899 } 900 901 static void ehci_caps_write(void *ptr, hwaddr addr, 902 uint64_t val, unsigned size) 903 { 904 } 905 906 static uint64_t ehci_opreg_read(void *ptr, hwaddr addr, 907 unsigned size) 908 { 909 EHCIState *s = ptr; 910 uint32_t val; 911 912 switch (addr) { 913 case FRINDEX: 914 /* Round down to mult of 8, else it can go backwards on migration */ 915 val = s->frindex & ~7; 916 break; 917 default: 918 val = s->opreg[addr >> 2]; 919 } 920 921 trace_usb_ehci_opreg_read(addr + s->opregbase, addr2str(addr), val); 922 return val; 923 } 924 925 static uint64_t ehci_port_read(void *ptr, hwaddr addr, 926 unsigned size) 927 { 928 EHCIState *s = ptr; 929 uint32_t val; 930 931 val = s->portsc[addr >> 2]; 932 trace_usb_ehci_portsc_read(addr + s->portscbase, addr >> 2, val); 933 return val; 934 } 935 936 static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner) 937 { 938 USBDevice *dev = s->ports[port].dev; 939 uint32_t *portsc = &s->portsc[port]; 940 uint32_t orig; 941 942 if (s->companion_ports[port] == NULL) 943 return; 944 945 owner = owner & PORTSC_POWNER; 946 orig = *portsc & PORTSC_POWNER; 947 948 if (!(owner ^ orig)) { 949 return; 950 } 951 952 if (dev && dev->attached) { 953 usb_detach(&s->ports[port]); 954 } 955 956 *portsc &= ~PORTSC_POWNER; 957 *portsc |= owner; 958 959 if (dev && dev->attached) { 960 usb_attach(&s->ports[port]); 961 } 962 } 963 964 static void ehci_port_write(void *ptr, hwaddr addr, 965 uint64_t val, unsigned size) 966 { 967 EHCIState *s = ptr; 968 int port = addr >> 2; 969 uint32_t *portsc = &s->portsc[port]; 970 uint32_t old = *portsc; 971 USBDevice *dev = s->ports[port].dev; 972 973 trace_usb_ehci_portsc_write(addr + s->portscbase, addr >> 2, val); 974 975 /* Clear rwc bits */ 976 *portsc &= ~(val & PORTSC_RWC_MASK); 977 /* The guest may clear, but not set the PED bit */ 978 *portsc &= val | ~PORTSC_PED; 979 /* POWNER is masked out by RO_MASK as it is RO when we've no companion */ 980 handle_port_owner_write(s, port, val); 981 /* And finally apply RO_MASK */ 982 val &= PORTSC_RO_MASK; 983 984 if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) { 985 trace_usb_ehci_port_reset(port, 1); 986 } 987 988 if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) { 989 trace_usb_ehci_port_reset(port, 0); 990 if (dev && dev->attached) { 991 usb_port_reset(&s->ports[port]); 992 *portsc &= ~PORTSC_CSC; 993 } 994 995 /* 996 * Table 2.16 Set the enable bit(and enable bit change) to indicate 997 * to SW that this port has a high speed device attached 998 */ 999 if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) { 1000 val |= PORTSC_PED; 1001 } 1002 } 1003 1004 if ((val & PORTSC_SUSPEND) && !(*portsc & PORTSC_SUSPEND)) { 1005 trace_usb_ehci_port_suspend(port); 1006 } 1007 if (!(val & PORTSC_FPRES) && (*portsc & PORTSC_FPRES)) { 1008 trace_usb_ehci_port_resume(port); 1009 val &= ~PORTSC_SUSPEND; 1010 } 1011 1012 *portsc &= ~PORTSC_RO_MASK; 1013 *portsc |= val; 1014 trace_usb_ehci_portsc_change(addr + s->portscbase, addr >> 2, *portsc, old); 1015 } 1016 1017 static void ehci_opreg_write(void *ptr, hwaddr addr, 1018 uint64_t val, unsigned size) 1019 { 1020 EHCIState *s = ptr; 1021 uint32_t *mmio = s->opreg + (addr >> 2); 1022 uint32_t old = *mmio; 1023 int i; 1024 1025 trace_usb_ehci_opreg_write(addr + s->opregbase, addr2str(addr), val); 1026 1027 switch (addr) { 1028 case USBCMD: 1029 if (val & USBCMD_HCRESET) { 1030 ehci_reset(s); 1031 val = s->usbcmd; 1032 break; 1033 } 1034 1035 /* not supporting dynamic frame list size at the moment */ 1036 if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) { 1037 fprintf(stderr, "attempt to set frame list size -- value %d\n", 1038 (int)val & USBCMD_FLS); 1039 val &= ~USBCMD_FLS; 1040 } 1041 1042 if (val & USBCMD_IAAD) { 1043 /* 1044 * Process IAAD immediately, otherwise the Linux IAAD watchdog may 1045 * trigger and re-use a qh without us seeing the unlink. 1046 */ 1047 s->async_stepdown = 0; 1048 qemu_bh_schedule(s->async_bh); 1049 trace_usb_ehci_doorbell_ring(); 1050 } 1051 1052 if (((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & val) != 1053 ((USBCMD_RUNSTOP | USBCMD_PSE | USBCMD_ASE) & s->usbcmd)) { 1054 if (s->pstate == EST_INACTIVE) { 1055 SET_LAST_RUN_CLOCK(s); 1056 } 1057 s->usbcmd = val; /* Set usbcmd for ehci_update_halt() */ 1058 ehci_update_halt(s); 1059 s->async_stepdown = 0; 1060 qemu_bh_schedule(s->async_bh); 1061 } 1062 break; 1063 1064 case USBSTS: 1065 val &= USBSTS_RO_MASK; // bits 6 through 31 are RO 1066 ehci_clear_usbsts(s, val); // bits 0 through 5 are R/WC 1067 val = s->usbsts; 1068 ehci_update_irq(s); 1069 break; 1070 1071 case USBINTR: 1072 val &= USBINTR_MASK; 1073 if (ehci_enabled(s) && (USBSTS_FLR & val)) { 1074 qemu_bh_schedule(s->async_bh); 1075 } 1076 break; 1077 1078 case FRINDEX: 1079 val &= 0x00003fff; /* frindex is 14bits */ 1080 s->usbsts_frindex = val; 1081 break; 1082 1083 case CONFIGFLAG: 1084 val &= 0x1; 1085 if (val) { 1086 for(i = 0; i < NB_PORTS; i++) 1087 handle_port_owner_write(s, i, 0); 1088 } 1089 break; 1090 1091 case PERIODICLISTBASE: 1092 if (ehci_periodic_enabled(s)) { 1093 fprintf(stderr, 1094 "ehci: PERIODIC list base register set while periodic schedule\n" 1095 " is enabled and HC is enabled\n"); 1096 } 1097 break; 1098 1099 case ASYNCLISTADDR: 1100 if (ehci_async_enabled(s)) { 1101 fprintf(stderr, 1102 "ehci: ASYNC list address register set while async schedule\n" 1103 " is enabled and HC is enabled\n"); 1104 } 1105 break; 1106 } 1107 1108 *mmio = val; 1109 trace_usb_ehci_opreg_change(addr + s->opregbase, addr2str(addr), 1110 *mmio, old); 1111 } 1112 1113 /* 1114 * Write the qh back to guest physical memory. This step isn't 1115 * in the EHCI spec but we need to do it since we don't share 1116 * physical memory with our guest VM. 1117 * 1118 * The first three dwords are read-only for the EHCI, so skip them 1119 * when writing back the qh. 1120 */ 1121 static void ehci_flush_qh(EHCIQueue *q) 1122 { 1123 uint32_t *qh = (uint32_t *) &q->qh; 1124 uint32_t dwords = sizeof(EHCIqh) >> 2; 1125 uint32_t addr = NLPTR_GET(q->qhaddr); 1126 1127 put_dwords(q->ehci, addr + 3 * sizeof(uint32_t), qh + 3, dwords - 3); 1128 } 1129 1130 // 4.10.2 1131 1132 static int ehci_qh_do_overlay(EHCIQueue *q) 1133 { 1134 EHCIPacket *p = QTAILQ_FIRST(&q->packets); 1135 int i; 1136 int dtoggle; 1137 int ping; 1138 int eps; 1139 int reload; 1140 1141 assert(p != NULL); 1142 assert(p->qtdaddr == q->qtdaddr); 1143 1144 // remember values in fields to preserve in qh after overlay 1145 1146 dtoggle = q->qh.token & QTD_TOKEN_DTOGGLE; 1147 ping = q->qh.token & QTD_TOKEN_PING; 1148 1149 q->qh.current_qtd = p->qtdaddr; 1150 q->qh.next_qtd = p->qtd.next; 1151 q->qh.altnext_qtd = p->qtd.altnext; 1152 q->qh.token = p->qtd.token; 1153 1154 1155 eps = get_field(q->qh.epchar, QH_EPCHAR_EPS); 1156 if (eps == EHCI_QH_EPS_HIGH) { 1157 q->qh.token &= ~QTD_TOKEN_PING; 1158 q->qh.token |= ping; 1159 } 1160 1161 reload = get_field(q->qh.epchar, QH_EPCHAR_RL); 1162 set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT); 1163 1164 for (i = 0; i < 5; i++) { 1165 q->qh.bufptr[i] = p->qtd.bufptr[i]; 1166 } 1167 1168 if (!(q->qh.epchar & QH_EPCHAR_DTC)) { 1169 // preserve QH DT bit 1170 q->qh.token &= ~QTD_TOKEN_DTOGGLE; 1171 q->qh.token |= dtoggle; 1172 } 1173 1174 q->qh.bufptr[1] &= ~BUFPTR_CPROGMASK_MASK; 1175 q->qh.bufptr[2] &= ~BUFPTR_FRAMETAG_MASK; 1176 1177 ehci_flush_qh(q); 1178 1179 return 0; 1180 } 1181 1182 static int ehci_init_transfer(EHCIPacket *p) 1183 { 1184 uint32_t cpage, offset, bytes, plen; 1185 dma_addr_t page; 1186 1187 cpage = get_field(p->qtd.token, QTD_TOKEN_CPAGE); 1188 bytes = get_field(p->qtd.token, QTD_TOKEN_TBYTES); 1189 offset = p->qtd.bufptr[0] & ~QTD_BUFPTR_MASK; 1190 qemu_sglist_init(&p->sgl, p->queue->ehci->device, 5, p->queue->ehci->as); 1191 1192 while (bytes > 0) { 1193 if (cpage > 4) { 1194 fprintf(stderr, "cpage out of range (%d)\n", cpage); 1195 qemu_sglist_destroy(&p->sgl); 1196 return -1; 1197 } 1198 1199 page = p->qtd.bufptr[cpage] & QTD_BUFPTR_MASK; 1200 page += offset; 1201 plen = bytes; 1202 if (plen > 4096 - offset) { 1203 plen = 4096 - offset; 1204 offset = 0; 1205 cpage++; 1206 } 1207 1208 qemu_sglist_add(&p->sgl, page, plen); 1209 bytes -= plen; 1210 } 1211 return 0; 1212 } 1213 1214 static void ehci_finish_transfer(EHCIQueue *q, int len) 1215 { 1216 uint32_t cpage, offset; 1217 1218 if (len > 0) { 1219 /* update cpage & offset */ 1220 cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE); 1221 offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK; 1222 1223 offset += len; 1224 cpage += offset >> QTD_BUFPTR_SH; 1225 offset &= ~QTD_BUFPTR_MASK; 1226 1227 set_field(&q->qh.token, cpage, QTD_TOKEN_CPAGE); 1228 q->qh.bufptr[0] &= QTD_BUFPTR_MASK; 1229 q->qh.bufptr[0] |= offset; 1230 } 1231 } 1232 1233 static void ehci_async_complete_packet(USBPort *port, USBPacket *packet) 1234 { 1235 EHCIPacket *p; 1236 EHCIState *s = port->opaque; 1237 uint32_t portsc = s->portsc[port->index]; 1238 1239 if (portsc & PORTSC_POWNER) { 1240 USBPort *companion = s->companion_ports[port->index]; 1241 companion->ops->complete(companion, packet); 1242 return; 1243 } 1244 1245 p = container_of(packet, EHCIPacket, packet); 1246 assert(p->async == EHCI_ASYNC_INFLIGHT); 1247 1248 if (packet->status == USB_RET_REMOVE_FROM_QUEUE) { 1249 trace_usb_ehci_packet_action(p->queue, p, "remove"); 1250 ehci_free_packet(p); 1251 return; 1252 } 1253 1254 trace_usb_ehci_packet_action(p->queue, p, "wakeup"); 1255 p->async = EHCI_ASYNC_FINISHED; 1256 1257 if (!p->queue->async) { 1258 s->periodic_sched_active = PERIODIC_ACTIVE; 1259 } 1260 qemu_bh_schedule(s->async_bh); 1261 } 1262 1263 static void ehci_execute_complete(EHCIQueue *q) 1264 { 1265 EHCIPacket *p = QTAILQ_FIRST(&q->packets); 1266 uint32_t tbytes; 1267 1268 assert(p != NULL); 1269 assert(p->qtdaddr == q->qtdaddr); 1270 assert(p->async == EHCI_ASYNC_INITIALIZED || 1271 p->async == EHCI_ASYNC_FINISHED); 1272 1273 DPRINTF("execute_complete: qhaddr 0x%x, next 0x%x, qtdaddr 0x%x, " 1274 "status %d, actual_length %d\n", 1275 q->qhaddr, q->qh.next, q->qtdaddr, 1276 p->packet.status, p->packet.actual_length); 1277 1278 switch (p->packet.status) { 1279 case USB_RET_SUCCESS: 1280 break; 1281 case USB_RET_IOERROR: 1282 case USB_RET_NODEV: 1283 q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR); 1284 set_field(&q->qh.token, 0, QTD_TOKEN_CERR); 1285 ehci_raise_irq(q->ehci, USBSTS_ERRINT); 1286 break; 1287 case USB_RET_STALL: 1288 q->qh.token |= QTD_TOKEN_HALT; 1289 ehci_raise_irq(q->ehci, USBSTS_ERRINT); 1290 break; 1291 case USB_RET_NAK: 1292 set_field(&q->qh.altnext_qtd, 0, QH_ALTNEXT_NAKCNT); 1293 return; /* We're not done yet with this transaction */ 1294 case USB_RET_BABBLE: 1295 q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE); 1296 ehci_raise_irq(q->ehci, USBSTS_ERRINT); 1297 break; 1298 default: 1299 /* should not be triggerable */ 1300 fprintf(stderr, "USB invalid response %d\n", p->packet.status); 1301 g_assert_not_reached(); 1302 break; 1303 } 1304 1305 /* TODO check 4.12 for splits */ 1306 tbytes = get_field(q->qh.token, QTD_TOKEN_TBYTES); 1307 if (tbytes && p->pid == USB_TOKEN_IN) { 1308 tbytes -= p->packet.actual_length; 1309 if (tbytes) { 1310 /* 4.15.1.2 must raise int on a short input packet */ 1311 ehci_raise_irq(q->ehci, USBSTS_INT); 1312 if (q->async) { 1313 q->ehci->int_req_by_async = true; 1314 } 1315 } 1316 } else { 1317 tbytes = 0; 1318 } 1319 DPRINTF("updating tbytes to %d\n", tbytes); 1320 set_field(&q->qh.token, tbytes, QTD_TOKEN_TBYTES); 1321 1322 ehci_finish_transfer(q, p->packet.actual_length); 1323 usb_packet_unmap(&p->packet, &p->sgl); 1324 qemu_sglist_destroy(&p->sgl); 1325 p->async = EHCI_ASYNC_NONE; 1326 1327 q->qh.token ^= QTD_TOKEN_DTOGGLE; 1328 q->qh.token &= ~QTD_TOKEN_ACTIVE; 1329 1330 if (q->qh.token & QTD_TOKEN_IOC) { 1331 ehci_raise_irq(q->ehci, USBSTS_INT); 1332 if (q->async) { 1333 q->ehci->int_req_by_async = true; 1334 } 1335 } 1336 } 1337 1338 /* 4.10.3 returns "again" */ 1339 static int ehci_execute(EHCIPacket *p, const char *action) 1340 { 1341 USBEndpoint *ep; 1342 int endp; 1343 bool spd; 1344 1345 assert(p->async == EHCI_ASYNC_NONE || 1346 p->async == EHCI_ASYNC_INITIALIZED); 1347 1348 if (!(p->qtd.token & QTD_TOKEN_ACTIVE)) { 1349 fprintf(stderr, "Attempting to execute inactive qtd\n"); 1350 return -1; 1351 } 1352 1353 if (get_field(p->qtd.token, QTD_TOKEN_TBYTES) > BUFF_SIZE) { 1354 ehci_trace_guest_bug(p->queue->ehci, 1355 "guest requested more bytes than allowed"); 1356 return -1; 1357 } 1358 1359 if (!ehci_verify_pid(p->queue, &p->qtd)) { 1360 ehci_queue_stopped(p->queue); /* Mark the ep in the prev dir stopped */ 1361 } 1362 p->pid = ehci_get_pid(&p->qtd); 1363 p->queue->last_pid = p->pid; 1364 endp = get_field(p->queue->qh.epchar, QH_EPCHAR_EP); 1365 ep = usb_ep_get(p->queue->dev, p->pid, endp); 1366 1367 if (p->async == EHCI_ASYNC_NONE) { 1368 if (ehci_init_transfer(p) != 0) { 1369 return -1; 1370 } 1371 1372 spd = (p->pid == USB_TOKEN_IN && NLPTR_TBIT(p->qtd.altnext) == 0); 1373 usb_packet_setup(&p->packet, p->pid, ep, 0, p->qtdaddr, spd, 1374 (p->qtd.token & QTD_TOKEN_IOC) != 0); 1375 usb_packet_map(&p->packet, &p->sgl); 1376 p->async = EHCI_ASYNC_INITIALIZED; 1377 } 1378 1379 trace_usb_ehci_packet_action(p->queue, p, action); 1380 usb_handle_packet(p->queue->dev, &p->packet); 1381 DPRINTF("submit: qh 0x%x next 0x%x qtd 0x%x pid 0x%x len %zd endp 0x%x " 1382 "status %d actual_length %d\n", p->queue->qhaddr, p->qtd.next, 1383 p->qtdaddr, p->pid, p->packet.iov.size, endp, p->packet.status, 1384 p->packet.actual_length); 1385 1386 if (p->packet.actual_length > BUFF_SIZE) { 1387 fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n"); 1388 return -1; 1389 } 1390 1391 return 1; 1392 } 1393 1394 /* 4.7.2 1395 */ 1396 1397 static int ehci_process_itd(EHCIState *ehci, 1398 EHCIitd *itd, 1399 uint32_t addr) 1400 { 1401 USBDevice *dev; 1402 USBEndpoint *ep; 1403 uint32_t i, len, pid, dir, devaddr, endp; 1404 uint32_t pg, off, ptr1, ptr2, max, mult; 1405 1406 ehci->periodic_sched_active = PERIODIC_ACTIVE; 1407 1408 dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION); 1409 devaddr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR); 1410 endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP); 1411 max = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT); 1412 mult = get_field(itd->bufptr[2], ITD_BUFPTR_MULT); 1413 1414 for(i = 0; i < 8; i++) { 1415 if (itd->transact[i] & ITD_XACT_ACTIVE) { 1416 pg = get_field(itd->transact[i], ITD_XACT_PGSEL); 1417 off = itd->transact[i] & ITD_XACT_OFFSET_MASK; 1418 len = get_field(itd->transact[i], ITD_XACT_LENGTH); 1419 1420 if (len > max * mult) { 1421 len = max * mult; 1422 } 1423 if (len > BUFF_SIZE || pg > 6) { 1424 return -1; 1425 } 1426 1427 ptr1 = (itd->bufptr[pg] & ITD_BUFPTR_MASK); 1428 qemu_sglist_init(&ehci->isgl, ehci->device, 2, ehci->as); 1429 if (off + len > 4096) { 1430 /* transfer crosses page border */ 1431 if (pg == 6) { 1432 qemu_sglist_destroy(&ehci->isgl); 1433 return -1; /* avoid page pg + 1 */ 1434 } 1435 ptr2 = (itd->bufptr[pg + 1] & ITD_BUFPTR_MASK); 1436 uint32_t len2 = off + len - 4096; 1437 uint32_t len1 = len - len2; 1438 qemu_sglist_add(&ehci->isgl, ptr1 + off, len1); 1439 qemu_sglist_add(&ehci->isgl, ptr2, len2); 1440 } else { 1441 qemu_sglist_add(&ehci->isgl, ptr1 + off, len); 1442 } 1443 1444 dev = ehci_find_device(ehci, devaddr); 1445 if (dev == NULL) { 1446 ehci_trace_guest_bug(ehci, "no device found"); 1447 return -1; 1448 } 1449 pid = dir ? USB_TOKEN_IN : USB_TOKEN_OUT; 1450 ep = usb_ep_get(dev, pid, endp); 1451 if (ep && ep->type == USB_ENDPOINT_XFER_ISOC) { 1452 usb_packet_setup(&ehci->ipacket, pid, ep, 0, addr, false, 1453 (itd->transact[i] & ITD_XACT_IOC) != 0); 1454 usb_packet_map(&ehci->ipacket, &ehci->isgl); 1455 usb_handle_packet(dev, &ehci->ipacket); 1456 usb_packet_unmap(&ehci->ipacket, &ehci->isgl); 1457 } else { 1458 DPRINTF("ISOCH: attempt to addess non-iso endpoint\n"); 1459 ehci->ipacket.status = USB_RET_NAK; 1460 ehci->ipacket.actual_length = 0; 1461 } 1462 qemu_sglist_destroy(&ehci->isgl); 1463 1464 switch (ehci->ipacket.status) { 1465 case USB_RET_SUCCESS: 1466 break; 1467 default: 1468 fprintf(stderr, "Unexpected iso usb result: %d\n", 1469 ehci->ipacket.status); 1470 /* Fall through */ 1471 case USB_RET_IOERROR: 1472 case USB_RET_NODEV: 1473 /* 3.3.2: XACTERR is only allowed on IN transactions */ 1474 if (dir) { 1475 itd->transact[i] |= ITD_XACT_XACTERR; 1476 ehci_raise_irq(ehci, USBSTS_ERRINT); 1477 } 1478 break; 1479 case USB_RET_BABBLE: 1480 itd->transact[i] |= ITD_XACT_BABBLE; 1481 ehci_raise_irq(ehci, USBSTS_ERRINT); 1482 break; 1483 case USB_RET_NAK: 1484 /* no data for us, so do a zero-length transfer */ 1485 ehci->ipacket.actual_length = 0; 1486 break; 1487 } 1488 if (!dir) { 1489 set_field(&itd->transact[i], len - ehci->ipacket.actual_length, 1490 ITD_XACT_LENGTH); /* OUT */ 1491 } else { 1492 set_field(&itd->transact[i], ehci->ipacket.actual_length, 1493 ITD_XACT_LENGTH); /* IN */ 1494 } 1495 if (itd->transact[i] & ITD_XACT_IOC) { 1496 ehci_raise_irq(ehci, USBSTS_INT); 1497 } 1498 itd->transact[i] &= ~ITD_XACT_ACTIVE; 1499 } 1500 } 1501 return 0; 1502 } 1503 1504 1505 /* This state is the entry point for asynchronous schedule 1506 * processing. Entry here consitutes a EHCI start event state (4.8.5) 1507 */ 1508 static int ehci_state_waitlisthead(EHCIState *ehci, int async) 1509 { 1510 EHCIqh qh; 1511 int i = 0; 1512 int again = 0; 1513 uint32_t entry = ehci->asynclistaddr; 1514 1515 /* set reclamation flag at start event (4.8.6) */ 1516 if (async) { 1517 ehci_set_usbsts(ehci, USBSTS_REC); 1518 } 1519 1520 ehci_queues_rip_unused(ehci, async); 1521 1522 /* Find the head of the list (4.9.1.1) */ 1523 for(i = 0; i < MAX_QH; i++) { 1524 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh, 1525 sizeof(EHCIqh) >> 2) < 0) { 1526 return 0; 1527 } 1528 ehci_trace_qh(NULL, NLPTR_GET(entry), &qh); 1529 1530 if (qh.epchar & QH_EPCHAR_H) { 1531 if (async) { 1532 entry |= (NLPTR_TYPE_QH << 1); 1533 } 1534 1535 ehci_set_fetch_addr(ehci, async, entry); 1536 ehci_set_state(ehci, async, EST_FETCHENTRY); 1537 again = 1; 1538 goto out; 1539 } 1540 1541 entry = qh.next; 1542 if (entry == ehci->asynclistaddr) { 1543 break; 1544 } 1545 } 1546 1547 /* no head found for list. */ 1548 1549 ehci_set_state(ehci, async, EST_ACTIVE); 1550 1551 out: 1552 return again; 1553 } 1554 1555 1556 /* This state is the entry point for periodic schedule processing as 1557 * well as being a continuation state for async processing. 1558 */ 1559 static int ehci_state_fetchentry(EHCIState *ehci, int async) 1560 { 1561 int again = 0; 1562 uint32_t entry = ehci_get_fetch_addr(ehci, async); 1563 1564 if (NLPTR_TBIT(entry)) { 1565 ehci_set_state(ehci, async, EST_ACTIVE); 1566 goto out; 1567 } 1568 1569 /* section 4.8, only QH in async schedule */ 1570 if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) { 1571 fprintf(stderr, "non queue head request in async schedule\n"); 1572 return -1; 1573 } 1574 1575 switch (NLPTR_TYPE_GET(entry)) { 1576 case NLPTR_TYPE_QH: 1577 ehci_set_state(ehci, async, EST_FETCHQH); 1578 again = 1; 1579 break; 1580 1581 case NLPTR_TYPE_ITD: 1582 ehci_set_state(ehci, async, EST_FETCHITD); 1583 again = 1; 1584 break; 1585 1586 case NLPTR_TYPE_STITD: 1587 ehci_set_state(ehci, async, EST_FETCHSITD); 1588 again = 1; 1589 break; 1590 1591 default: 1592 /* TODO: handle FSTN type */ 1593 fprintf(stderr, "FETCHENTRY: entry at %X is of type %d " 1594 "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry)); 1595 return -1; 1596 } 1597 1598 out: 1599 return again; 1600 } 1601 1602 static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async) 1603 { 1604 uint32_t entry; 1605 EHCIQueue *q; 1606 EHCIqh qh; 1607 1608 entry = ehci_get_fetch_addr(ehci, async); 1609 q = ehci_find_queue_by_qh(ehci, entry, async); 1610 if (q == NULL) { 1611 q = ehci_alloc_queue(ehci, entry, async); 1612 } 1613 1614 q->seen++; 1615 if (q->seen > 1) { 1616 /* we are going in circles -- stop processing */ 1617 ehci_set_state(ehci, async, EST_ACTIVE); 1618 q = NULL; 1619 goto out; 1620 } 1621 1622 if (get_dwords(ehci, NLPTR_GET(q->qhaddr), 1623 (uint32_t *) &qh, sizeof(EHCIqh) >> 2) < 0) { 1624 q = NULL; 1625 goto out; 1626 } 1627 ehci_trace_qh(q, NLPTR_GET(q->qhaddr), &qh); 1628 1629 /* 1630 * The overlay area of the qh should never be changed by the guest, 1631 * except when idle, in which case the reset is a nop. 1632 */ 1633 if (!ehci_verify_qh(q, &qh)) { 1634 if (ehci_reset_queue(q) > 0) { 1635 ehci_trace_guest_bug(ehci, "guest updated active QH"); 1636 } 1637 } 1638 q->qh = qh; 1639 1640 q->transact_ctr = get_field(q->qh.epcap, QH_EPCAP_MULT); 1641 if (q->transact_ctr == 0) { /* Guest bug in some versions of windows */ 1642 q->transact_ctr = 4; 1643 } 1644 1645 if (q->dev == NULL) { 1646 q->dev = ehci_find_device(q->ehci, 1647 get_field(q->qh.epchar, QH_EPCHAR_DEVADDR)); 1648 } 1649 1650 if (async && (q->qh.epchar & QH_EPCHAR_H)) { 1651 1652 /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */ 1653 if (ehci->usbsts & USBSTS_REC) { 1654 ehci_clear_usbsts(ehci, USBSTS_REC); 1655 } else { 1656 DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset" 1657 " - done processing\n", q->qhaddr); 1658 ehci_set_state(ehci, async, EST_ACTIVE); 1659 q = NULL; 1660 goto out; 1661 } 1662 } 1663 1664 #if EHCI_DEBUG 1665 if (q->qhaddr != q->qh.next) { 1666 DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n", 1667 q->qhaddr, 1668 q->qh.epchar & QH_EPCHAR_H, 1669 q->qh.token & QTD_TOKEN_HALT, 1670 q->qh.token & QTD_TOKEN_ACTIVE, 1671 q->qh.next); 1672 } 1673 #endif 1674 1675 if (q->qh.token & QTD_TOKEN_HALT) { 1676 ehci_set_state(ehci, async, EST_HORIZONTALQH); 1677 1678 } else if ((q->qh.token & QTD_TOKEN_ACTIVE) && 1679 (NLPTR_TBIT(q->qh.current_qtd) == 0) && 1680 (q->qh.current_qtd != 0)) { 1681 q->qtdaddr = q->qh.current_qtd; 1682 ehci_set_state(ehci, async, EST_FETCHQTD); 1683 1684 } else { 1685 /* EHCI spec version 1.0 Section 4.10.2 */ 1686 ehci_set_state(ehci, async, EST_ADVANCEQUEUE); 1687 } 1688 1689 out: 1690 return q; 1691 } 1692 1693 static int ehci_state_fetchitd(EHCIState *ehci, int async) 1694 { 1695 uint32_t entry; 1696 EHCIitd itd; 1697 1698 assert(!async); 1699 entry = ehci_get_fetch_addr(ehci, async); 1700 1701 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd, 1702 sizeof(EHCIitd) >> 2) < 0) { 1703 return -1; 1704 } 1705 ehci_trace_itd(ehci, entry, &itd); 1706 1707 if (ehci_process_itd(ehci, &itd, entry) != 0) { 1708 return -1; 1709 } 1710 1711 put_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd, 1712 sizeof(EHCIitd) >> 2); 1713 ehci_set_fetch_addr(ehci, async, itd.next); 1714 ehci_set_state(ehci, async, EST_FETCHENTRY); 1715 1716 return 1; 1717 } 1718 1719 static int ehci_state_fetchsitd(EHCIState *ehci, int async) 1720 { 1721 uint32_t entry; 1722 EHCIsitd sitd; 1723 1724 assert(!async); 1725 entry = ehci_get_fetch_addr(ehci, async); 1726 1727 if (get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd, 1728 sizeof(EHCIsitd) >> 2) < 0) { 1729 return 0; 1730 } 1731 ehci_trace_sitd(ehci, entry, &sitd); 1732 1733 if (!(sitd.results & SITD_RESULTS_ACTIVE)) { 1734 /* siTD is not active, nothing to do */; 1735 } else { 1736 /* TODO: split transfers are not implemented */ 1737 warn_report("Skipping active siTD"); 1738 } 1739 1740 ehci_set_fetch_addr(ehci, async, sitd.next); 1741 ehci_set_state(ehci, async, EST_FETCHENTRY); 1742 return 1; 1743 } 1744 1745 /* Section 4.10.2 - paragraph 3 */ 1746 static int ehci_state_advqueue(EHCIQueue *q) 1747 { 1748 #if 0 1749 /* TO-DO: 4.10.2 - paragraph 2 1750 * if I-bit is set to 1 and QH is not active 1751 * go to horizontal QH 1752 */ 1753 if (I-bit set) { 1754 ehci_set_state(ehci, async, EST_HORIZONTALQH); 1755 goto out; 1756 } 1757 #endif 1758 1759 /* 1760 * want data and alt-next qTD is valid 1761 */ 1762 if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) && 1763 (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) { 1764 q->qtdaddr = q->qh.altnext_qtd; 1765 ehci_set_state(q->ehci, q->async, EST_FETCHQTD); 1766 1767 /* 1768 * next qTD is valid 1769 */ 1770 } else if (NLPTR_TBIT(q->qh.next_qtd) == 0) { 1771 q->qtdaddr = q->qh.next_qtd; 1772 ehci_set_state(q->ehci, q->async, EST_FETCHQTD); 1773 1774 /* 1775 * no valid qTD, try next QH 1776 */ 1777 } else { 1778 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); 1779 } 1780 1781 return 1; 1782 } 1783 1784 /* Section 4.10.2 - paragraph 4 */ 1785 static int ehci_state_fetchqtd(EHCIQueue *q) 1786 { 1787 EHCIqtd qtd; 1788 EHCIPacket *p; 1789 int again = 1; 1790 uint32_t addr; 1791 1792 addr = NLPTR_GET(q->qtdaddr); 1793 if (get_dwords(q->ehci, addr + 8, &qtd.token, 1) < 0) { 1794 return 0; 1795 } 1796 barrier(); 1797 if (get_dwords(q->ehci, addr + 0, &qtd.next, 1) < 0 || 1798 get_dwords(q->ehci, addr + 4, &qtd.altnext, 1) < 0 || 1799 get_dwords(q->ehci, addr + 12, qtd.bufptr, 1800 ARRAY_SIZE(qtd.bufptr)) < 0) { 1801 return 0; 1802 } 1803 ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &qtd); 1804 1805 p = QTAILQ_FIRST(&q->packets); 1806 if (p != NULL) { 1807 if (!ehci_verify_qtd(p, &qtd)) { 1808 ehci_cancel_queue(q); 1809 if (qtd.token & QTD_TOKEN_ACTIVE) { 1810 ehci_trace_guest_bug(q->ehci, "guest updated active qTD"); 1811 } 1812 p = NULL; 1813 } else { 1814 p->qtd = qtd; 1815 ehci_qh_do_overlay(q); 1816 } 1817 } 1818 1819 if (!(qtd.token & QTD_TOKEN_ACTIVE)) { 1820 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); 1821 } else if (p != NULL) { 1822 switch (p->async) { 1823 case EHCI_ASYNC_NONE: 1824 case EHCI_ASYNC_INITIALIZED: 1825 /* Not yet executed (MULT), or previously nacked (int) packet */ 1826 ehci_set_state(q->ehci, q->async, EST_EXECUTE); 1827 break; 1828 case EHCI_ASYNC_INFLIGHT: 1829 /* Check if the guest has added new tds to the queue */ 1830 again = ehci_fill_queue(QTAILQ_LAST(&q->packets)); 1831 /* Unfinished async handled packet, go horizontal */ 1832 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); 1833 break; 1834 case EHCI_ASYNC_FINISHED: 1835 /* Complete executing of the packet */ 1836 ehci_set_state(q->ehci, q->async, EST_EXECUTING); 1837 break; 1838 } 1839 } else { 1840 p = ehci_alloc_packet(q); 1841 p->qtdaddr = q->qtdaddr; 1842 p->qtd = qtd; 1843 ehci_set_state(q->ehci, q->async, EST_EXECUTE); 1844 } 1845 1846 return again; 1847 } 1848 1849 static int ehci_state_horizqh(EHCIQueue *q) 1850 { 1851 int again = 0; 1852 1853 if (ehci_get_fetch_addr(q->ehci, q->async) != q->qh.next) { 1854 ehci_set_fetch_addr(q->ehci, q->async, q->qh.next); 1855 ehci_set_state(q->ehci, q->async, EST_FETCHENTRY); 1856 again = 1; 1857 } else { 1858 ehci_set_state(q->ehci, q->async, EST_ACTIVE); 1859 } 1860 1861 return again; 1862 } 1863 1864 /* Returns "again" */ 1865 static int ehci_fill_queue(EHCIPacket *p) 1866 { 1867 USBEndpoint *ep = p->packet.ep; 1868 EHCIQueue *q = p->queue; 1869 EHCIqtd qtd = p->qtd; 1870 uint32_t qtdaddr; 1871 1872 for (;;) { 1873 if (NLPTR_TBIT(qtd.next) != 0) { 1874 break; 1875 } 1876 qtdaddr = qtd.next; 1877 /* 1878 * Detect circular td lists, Windows creates these, counting on the 1879 * active bit going low after execution to make the queue stop. 1880 */ 1881 QTAILQ_FOREACH(p, &q->packets, next) { 1882 if (p->qtdaddr == qtdaddr) { 1883 goto leave; 1884 } 1885 } 1886 if (get_dwords(q->ehci, NLPTR_GET(qtdaddr), 1887 (uint32_t *) &qtd, sizeof(EHCIqtd) >> 2) < 0) { 1888 return -1; 1889 } 1890 ehci_trace_qtd(q, NLPTR_GET(qtdaddr), &qtd); 1891 if (!(qtd.token & QTD_TOKEN_ACTIVE)) { 1892 break; 1893 } 1894 if (!ehci_verify_pid(q, &qtd)) { 1895 ehci_trace_guest_bug(q->ehci, "guest queued token with wrong pid"); 1896 break; 1897 } 1898 p = ehci_alloc_packet(q); 1899 p->qtdaddr = qtdaddr; 1900 p->qtd = qtd; 1901 if (ehci_execute(p, "queue") == -1) { 1902 return -1; 1903 } 1904 assert(p->packet.status == USB_RET_ASYNC); 1905 p->async = EHCI_ASYNC_INFLIGHT; 1906 } 1907 leave: 1908 usb_device_flush_ep_queue(ep->dev, ep); 1909 return 1; 1910 } 1911 1912 static int ehci_state_execute(EHCIQueue *q) 1913 { 1914 EHCIPacket *p = QTAILQ_FIRST(&q->packets); 1915 int again = 0; 1916 1917 assert(p != NULL); 1918 assert(p->qtdaddr == q->qtdaddr); 1919 1920 if (ehci_qh_do_overlay(q) != 0) { 1921 return -1; 1922 } 1923 1924 // TODO verify enough time remains in the uframe as in 4.4.1.1 1925 // TODO write back ptr to async list when done or out of time 1926 1927 /* 4.10.3, bottom of page 82, go horizontal on transaction counter == 0 */ 1928 if (!q->async && q->transact_ctr == 0) { 1929 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); 1930 again = 1; 1931 goto out; 1932 } 1933 1934 if (q->async) { 1935 ehci_set_usbsts(q->ehci, USBSTS_REC); 1936 } 1937 1938 again = ehci_execute(p, "process"); 1939 if (again == -1) { 1940 goto out; 1941 } 1942 if (p->packet.status == USB_RET_ASYNC) { 1943 ehci_flush_qh(q); 1944 trace_usb_ehci_packet_action(p->queue, p, "async"); 1945 p->async = EHCI_ASYNC_INFLIGHT; 1946 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); 1947 if (q->async) { 1948 again = ehci_fill_queue(p); 1949 } else { 1950 again = 1; 1951 } 1952 goto out; 1953 } 1954 1955 ehci_set_state(q->ehci, q->async, EST_EXECUTING); 1956 again = 1; 1957 1958 out: 1959 return again; 1960 } 1961 1962 static int ehci_state_executing(EHCIQueue *q) 1963 { 1964 EHCIPacket *p = QTAILQ_FIRST(&q->packets); 1965 1966 assert(p != NULL); 1967 assert(p->qtdaddr == q->qtdaddr); 1968 1969 ehci_execute_complete(q); 1970 1971 /* 4.10.3 */ 1972 if (!q->async && q->transact_ctr > 0) { 1973 q->transact_ctr--; 1974 } 1975 1976 /* 4.10.5 */ 1977 if (p->packet.status == USB_RET_NAK) { 1978 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); 1979 } else { 1980 ehci_set_state(q->ehci, q->async, EST_WRITEBACK); 1981 } 1982 1983 ehci_flush_qh(q); 1984 return 1; 1985 } 1986 1987 1988 static int ehci_state_writeback(EHCIQueue *q) 1989 { 1990 EHCIPacket *p = QTAILQ_FIRST(&q->packets); 1991 uint32_t *qtd, addr; 1992 int again = 0; 1993 1994 /* Write back the QTD from the QH area */ 1995 assert(p != NULL); 1996 assert(p->qtdaddr == q->qtdaddr); 1997 1998 ehci_trace_qtd(q, NLPTR_GET(p->qtdaddr), (EHCIqtd *) &q->qh.next_qtd); 1999 qtd = (uint32_t *) &q->qh.next_qtd; 2000 addr = NLPTR_GET(p->qtdaddr); 2001 put_dwords(q->ehci, addr + 2 * sizeof(uint32_t), qtd + 2, 2); 2002 ehci_free_packet(p); 2003 2004 /* 2005 * EHCI specs say go horizontal here. 2006 * 2007 * We can also advance the queue here for performance reasons. We 2008 * need to take care to only take that shortcut in case we've 2009 * processed the qtd just written back without errors, i.e. halt 2010 * bit is clear. 2011 */ 2012 if (q->qh.token & QTD_TOKEN_HALT) { 2013 ehci_set_state(q->ehci, q->async, EST_HORIZONTALQH); 2014 again = 1; 2015 } else { 2016 ehci_set_state(q->ehci, q->async, EST_ADVANCEQUEUE); 2017 again = 1; 2018 } 2019 return again; 2020 } 2021 2022 /* 2023 * This is the state machine that is common to both async and periodic 2024 */ 2025 2026 static void ehci_advance_state(EHCIState *ehci, int async) 2027 { 2028 EHCIQueue *q = NULL; 2029 int itd_count = 0; 2030 int again; 2031 2032 do { 2033 switch(ehci_get_state(ehci, async)) { 2034 case EST_WAITLISTHEAD: 2035 again = ehci_state_waitlisthead(ehci, async); 2036 break; 2037 2038 case EST_FETCHENTRY: 2039 again = ehci_state_fetchentry(ehci, async); 2040 break; 2041 2042 case EST_FETCHQH: 2043 q = ehci_state_fetchqh(ehci, async); 2044 if (q != NULL) { 2045 assert(q->async == async); 2046 again = 1; 2047 } else { 2048 again = 0; 2049 } 2050 break; 2051 2052 case EST_FETCHITD: 2053 again = ehci_state_fetchitd(ehci, async); 2054 itd_count++; 2055 break; 2056 2057 case EST_FETCHSITD: 2058 again = ehci_state_fetchsitd(ehci, async); 2059 itd_count++; 2060 break; 2061 2062 case EST_ADVANCEQUEUE: 2063 assert(q != NULL); 2064 again = ehci_state_advqueue(q); 2065 break; 2066 2067 case EST_FETCHQTD: 2068 assert(q != NULL); 2069 again = ehci_state_fetchqtd(q); 2070 break; 2071 2072 case EST_HORIZONTALQH: 2073 assert(q != NULL); 2074 again = ehci_state_horizqh(q); 2075 break; 2076 2077 case EST_EXECUTE: 2078 assert(q != NULL); 2079 again = ehci_state_execute(q); 2080 if (async) { 2081 ehci->async_stepdown = 0; 2082 } 2083 break; 2084 2085 case EST_EXECUTING: 2086 assert(q != NULL); 2087 if (async) { 2088 ehci->async_stepdown = 0; 2089 } 2090 again = ehci_state_executing(q); 2091 break; 2092 2093 case EST_WRITEBACK: 2094 assert(q != NULL); 2095 again = ehci_state_writeback(q); 2096 if (!async) { 2097 ehci->periodic_sched_active = PERIODIC_ACTIVE; 2098 } 2099 break; 2100 2101 default: 2102 fprintf(stderr, "Bad state!\n"); 2103 again = -1; 2104 g_assert_not_reached(); 2105 break; 2106 } 2107 2108 if (again < 0 || itd_count > 16) { 2109 /* TODO: notify guest (raise HSE irq?) */ 2110 fprintf(stderr, "processing error - resetting ehci HC\n"); 2111 ehci_reset(ehci); 2112 again = 0; 2113 } 2114 } 2115 while (again); 2116 } 2117 2118 static void ehci_advance_async_state(EHCIState *ehci) 2119 { 2120 const int async = 1; 2121 2122 switch(ehci_get_state(ehci, async)) { 2123 case EST_INACTIVE: 2124 if (!ehci_async_enabled(ehci)) { 2125 break; 2126 } 2127 ehci_set_state(ehci, async, EST_ACTIVE); 2128 // No break, fall through to ACTIVE 2129 2130 case EST_ACTIVE: 2131 if (!ehci_async_enabled(ehci)) { 2132 ehci_queues_rip_all(ehci, async); 2133 ehci_set_state(ehci, async, EST_INACTIVE); 2134 break; 2135 } 2136 2137 /* make sure guest has acknowledged the doorbell interrupt */ 2138 /* TO-DO: is this really needed? */ 2139 if (ehci->usbsts & USBSTS_IAA) { 2140 DPRINTF("IAA status bit still set.\n"); 2141 break; 2142 } 2143 2144 /* check that address register has been set */ 2145 if (ehci->asynclistaddr == 0) { 2146 break; 2147 } 2148 2149 ehci_set_state(ehci, async, EST_WAITLISTHEAD); 2150 ehci_advance_state(ehci, async); 2151 2152 /* If the doorbell is set, the guest wants to make a change to the 2153 * schedule. The host controller needs to release cached data. 2154 * (section 4.8.2) 2155 */ 2156 if (ehci->usbcmd & USBCMD_IAAD) { 2157 /* Remove all unseen qhs from the async qhs queue */ 2158 ehci_queues_rip_unseen(ehci, async); 2159 trace_usb_ehci_doorbell_ack(); 2160 ehci->usbcmd &= ~USBCMD_IAAD; 2161 ehci_raise_irq(ehci, USBSTS_IAA); 2162 } 2163 break; 2164 2165 default: 2166 /* this should only be due to a developer mistake */ 2167 fprintf(stderr, "ehci: Bad asynchronous state %d. " 2168 "Resetting to active\n", ehci->astate); 2169 g_assert_not_reached(); 2170 } 2171 } 2172 2173 static void ehci_advance_periodic_state(EHCIState *ehci) 2174 { 2175 uint32_t entry; 2176 uint32_t list; 2177 const int async = 0; 2178 2179 // 4.6 2180 2181 switch(ehci_get_state(ehci, async)) { 2182 case EST_INACTIVE: 2183 if (!(ehci->frindex & 7) && ehci_periodic_enabled(ehci)) { 2184 ehci_set_state(ehci, async, EST_ACTIVE); 2185 // No break, fall through to ACTIVE 2186 } else 2187 break; 2188 2189 case EST_ACTIVE: 2190 if (!(ehci->frindex & 7) && !ehci_periodic_enabled(ehci)) { 2191 ehci_queues_rip_all(ehci, async); 2192 ehci_set_state(ehci, async, EST_INACTIVE); 2193 break; 2194 } 2195 2196 list = ehci->periodiclistbase & 0xfffff000; 2197 /* check that register has been set */ 2198 if (list == 0) { 2199 break; 2200 } 2201 list |= ((ehci->frindex & 0x1ff8) >> 1); 2202 2203 if (get_dwords(ehci, list, &entry, 1) < 0) { 2204 break; 2205 } 2206 2207 DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n", 2208 ehci->frindex / 8, list, entry); 2209 ehci_set_fetch_addr(ehci, async,entry); 2210 ehci_set_state(ehci, async, EST_FETCHENTRY); 2211 ehci_advance_state(ehci, async); 2212 ehci_queues_rip_unused(ehci, async); 2213 break; 2214 2215 default: 2216 /* this should only be due to a developer mistake */ 2217 fprintf(stderr, "ehci: Bad periodic state %d. " 2218 "Resetting to active\n", ehci->pstate); 2219 g_assert_not_reached(); 2220 } 2221 } 2222 2223 static void ehci_update_frindex(EHCIState *ehci, int uframes) 2224 { 2225 if (!ehci_enabled(ehci) && ehci->pstate == EST_INACTIVE) { 2226 return; 2227 } 2228 2229 /* Generate FLR interrupt if frame index rolls over 0x2000 */ 2230 if ((ehci->frindex % 0x2000) + uframes >= 0x2000) { 2231 ehci_raise_irq(ehci, USBSTS_FLR); 2232 } 2233 2234 /* How many times will frindex roll over 0x4000 with this frame count? 2235 * usbsts_frindex is decremented by 0x4000 on rollover until it reaches 0 2236 */ 2237 int rollovers = (ehci->frindex + uframes) / 0x4000; 2238 if (rollovers > 0) { 2239 if (ehci->usbsts_frindex >= (rollovers * 0x4000)) { 2240 ehci->usbsts_frindex -= 0x4000 * rollovers; 2241 } else { 2242 ehci->usbsts_frindex = 0; 2243 } 2244 } 2245 2246 ehci->frindex = (ehci->frindex + uframes) % 0x4000; 2247 } 2248 2249 static void ehci_work_bh(void *opaque) 2250 { 2251 EHCIState *ehci = opaque; 2252 int need_timer = 0; 2253 int64_t expire_time, t_now; 2254 uint64_t ns_elapsed; 2255 uint64_t uframes, skipped_uframes; 2256 int i; 2257 2258 if (ehci->working) { 2259 return; 2260 } 2261 ehci->working = true; 2262 2263 t_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 2264 ns_elapsed = t_now - ehci->last_run_ns; 2265 uframes = ns_elapsed / UFRAME_TIMER_NS; 2266 2267 if (ehci_periodic_enabled(ehci) || ehci->pstate != EST_INACTIVE) { 2268 need_timer++; 2269 2270 if (uframes > (ehci->maxframes * 8)) { 2271 skipped_uframes = uframes - (ehci->maxframes * 8); 2272 ehci_update_frindex(ehci, skipped_uframes); 2273 ehci->last_run_ns += UFRAME_TIMER_NS * skipped_uframes; 2274 uframes -= skipped_uframes; 2275 DPRINTF("WARNING - EHCI skipped %d uframes\n", skipped_uframes); 2276 } 2277 2278 for (i = 0; i < uframes; i++) { 2279 /* 2280 * If we're running behind schedule, we should not catch up 2281 * too fast, as that will make some guests unhappy: 2282 * 1) We must process a minimum of MIN_UFR_PER_TICK frames, 2283 * otherwise we will never catch up 2284 * 2) Process frames until the guest has requested an irq (IOC) 2285 */ 2286 if (i >= MIN_UFR_PER_TICK) { 2287 ehci_commit_irq(ehci); 2288 if ((ehci->usbsts & USBINTR_MASK) & ehci->usbintr) { 2289 break; 2290 } 2291 } 2292 if (ehci->periodic_sched_active) { 2293 ehci->periodic_sched_active--; 2294 } 2295 ehci_update_frindex(ehci, 1); 2296 if ((ehci->frindex & 7) == 0) { 2297 ehci_advance_periodic_state(ehci); 2298 } 2299 ehci->last_run_ns += UFRAME_TIMER_NS; 2300 } 2301 } else { 2302 ehci->periodic_sched_active = 0; 2303 ehci_update_frindex(ehci, uframes); 2304 ehci->last_run_ns += UFRAME_TIMER_NS * uframes; 2305 } 2306 2307 if (ehci->periodic_sched_active) { 2308 ehci->async_stepdown = 0; 2309 } else if (ehci->async_stepdown < ehci->maxframes / 2) { 2310 ehci->async_stepdown++; 2311 } 2312 2313 /* Async is not inside loop since it executes everything it can once 2314 * called 2315 */ 2316 if (ehci_async_enabled(ehci) || ehci->astate != EST_INACTIVE) { 2317 need_timer++; 2318 ehci_advance_async_state(ehci); 2319 } 2320 2321 ehci_commit_irq(ehci); 2322 if (ehci->usbsts_pending) { 2323 need_timer++; 2324 ehci->async_stepdown = 0; 2325 } 2326 2327 if (ehci_enabled(ehci) && (ehci->usbintr & USBSTS_FLR)) { 2328 need_timer++; 2329 } 2330 2331 if (need_timer) { 2332 /* If we've raised int, we speed up the timer, so that we quickly 2333 * notice any new packets queued up in response */ 2334 if (ehci->int_req_by_async && (ehci->usbsts & USBSTS_INT)) { 2335 expire_time = t_now + 2336 NANOSECONDS_PER_SECOND / (FRAME_TIMER_FREQ * 4); 2337 ehci->int_req_by_async = false; 2338 } else { 2339 expire_time = t_now + (NANOSECONDS_PER_SECOND 2340 * (ehci->async_stepdown+1) / FRAME_TIMER_FREQ); 2341 } 2342 timer_mod(ehci->frame_timer, expire_time); 2343 } 2344 2345 ehci->working = false; 2346 } 2347 2348 static void ehci_work_timer(void *opaque) 2349 { 2350 EHCIState *ehci = opaque; 2351 2352 qemu_bh_schedule(ehci->async_bh); 2353 } 2354 2355 static const MemoryRegionOps ehci_mmio_caps_ops = { 2356 .read = ehci_caps_read, 2357 .write = ehci_caps_write, 2358 .valid.min_access_size = 1, 2359 .valid.max_access_size = 4, 2360 .impl.min_access_size = 1, 2361 .impl.max_access_size = 1, 2362 .endianness = DEVICE_LITTLE_ENDIAN, 2363 }; 2364 2365 static const MemoryRegionOps ehci_mmio_opreg_ops = { 2366 .read = ehci_opreg_read, 2367 .write = ehci_opreg_write, 2368 .valid.min_access_size = 4, 2369 .valid.max_access_size = 4, 2370 .endianness = DEVICE_LITTLE_ENDIAN, 2371 }; 2372 2373 static const MemoryRegionOps ehci_mmio_port_ops = { 2374 .read = ehci_port_read, 2375 .write = ehci_port_write, 2376 .valid.min_access_size = 4, 2377 .valid.max_access_size = 4, 2378 .endianness = DEVICE_LITTLE_ENDIAN, 2379 }; 2380 2381 static USBPortOps ehci_port_ops = { 2382 .attach = ehci_attach, 2383 .detach = ehci_detach, 2384 .child_detach = ehci_child_detach, 2385 .wakeup = ehci_wakeup, 2386 .complete = ehci_async_complete_packet, 2387 }; 2388 2389 static USBBusOps ehci_bus_ops_companion = { 2390 .register_companion = ehci_register_companion, 2391 .wakeup_endpoint = ehci_wakeup_endpoint, 2392 }; 2393 static USBBusOps ehci_bus_ops_standalone = { 2394 .wakeup_endpoint = ehci_wakeup_endpoint, 2395 }; 2396 2397 static int usb_ehci_pre_save(void *opaque) 2398 { 2399 EHCIState *ehci = opaque; 2400 uint32_t new_frindex; 2401 2402 /* Round down frindex to a multiple of 8 for migration compatibility */ 2403 new_frindex = ehci->frindex & ~7; 2404 ehci->last_run_ns -= (ehci->frindex - new_frindex) * UFRAME_TIMER_NS; 2405 ehci->frindex = new_frindex; 2406 2407 return 0; 2408 } 2409 2410 static int usb_ehci_post_load(void *opaque, int version_id) 2411 { 2412 EHCIState *s = opaque; 2413 int i; 2414 2415 for (i = 0; i < NB_PORTS; i++) { 2416 USBPort *companion = s->companion_ports[i]; 2417 if (companion == NULL) { 2418 continue; 2419 } 2420 if (s->portsc[i] & PORTSC_POWNER) { 2421 companion->dev = s->ports[i].dev; 2422 } else { 2423 companion->dev = NULL; 2424 } 2425 } 2426 2427 return 0; 2428 } 2429 2430 static void usb_ehci_vm_state_change(void *opaque, int running, RunState state) 2431 { 2432 EHCIState *ehci = opaque; 2433 2434 /* 2435 * We don't migrate the EHCIQueue-s, instead we rebuild them for the 2436 * schedule in guest memory. We must do the rebuilt ASAP, so that 2437 * USB-devices which have async handled packages have a packet in the 2438 * ep queue to match the completion with. 2439 */ 2440 if (state == RUN_STATE_RUNNING) { 2441 ehci_advance_async_state(ehci); 2442 } 2443 2444 /* 2445 * The schedule rebuilt from guest memory could cause the migration dest 2446 * to miss a QH unlink, and fail to cancel packets, since the unlinked QH 2447 * will never have existed on the destination. Therefor we must flush the 2448 * async schedule on savevm to catch any not yet noticed unlinks. 2449 */ 2450 if (state == RUN_STATE_SAVE_VM) { 2451 ehci_advance_async_state(ehci); 2452 ehci_queues_rip_unseen(ehci, 1); 2453 } 2454 } 2455 2456 const VMStateDescription vmstate_ehci = { 2457 .name = "ehci-core", 2458 .version_id = 2, 2459 .minimum_version_id = 1, 2460 .pre_save = usb_ehci_pre_save, 2461 .post_load = usb_ehci_post_load, 2462 .fields = (VMStateField[]) { 2463 /* mmio registers */ 2464 VMSTATE_UINT32(usbcmd, EHCIState), 2465 VMSTATE_UINT32(usbsts, EHCIState), 2466 VMSTATE_UINT32_V(usbsts_pending, EHCIState, 2), 2467 VMSTATE_UINT32_V(usbsts_frindex, EHCIState, 2), 2468 VMSTATE_UINT32(usbintr, EHCIState), 2469 VMSTATE_UINT32(frindex, EHCIState), 2470 VMSTATE_UINT32(ctrldssegment, EHCIState), 2471 VMSTATE_UINT32(periodiclistbase, EHCIState), 2472 VMSTATE_UINT32(asynclistaddr, EHCIState), 2473 VMSTATE_UINT32(configflag, EHCIState), 2474 VMSTATE_UINT32(portsc[0], EHCIState), 2475 VMSTATE_UINT32(portsc[1], EHCIState), 2476 VMSTATE_UINT32(portsc[2], EHCIState), 2477 VMSTATE_UINT32(portsc[3], EHCIState), 2478 VMSTATE_UINT32(portsc[4], EHCIState), 2479 VMSTATE_UINT32(portsc[5], EHCIState), 2480 /* frame timer */ 2481 VMSTATE_TIMER_PTR(frame_timer, EHCIState), 2482 VMSTATE_UINT64(last_run_ns, EHCIState), 2483 VMSTATE_UINT32(async_stepdown, EHCIState), 2484 /* schedule state */ 2485 VMSTATE_UINT32(astate, EHCIState), 2486 VMSTATE_UINT32(pstate, EHCIState), 2487 VMSTATE_UINT32(a_fetch_addr, EHCIState), 2488 VMSTATE_UINT32(p_fetch_addr, EHCIState), 2489 VMSTATE_END_OF_LIST() 2490 } 2491 }; 2492 2493 void usb_ehci_realize(EHCIState *s, DeviceState *dev, Error **errp) 2494 { 2495 int i; 2496 2497 if (s->portnr > NB_PORTS) { 2498 error_setg(errp, "Too many ports! Max. port number is %d.", 2499 NB_PORTS); 2500 return; 2501 } 2502 if (s->maxframes < 8 || s->maxframes > 512) { 2503 error_setg(errp, "maxframes %d out if range (8 .. 512)", 2504 s->maxframes); 2505 return; 2506 } 2507 2508 usb_bus_new(&s->bus, sizeof(s->bus), s->companion_enable ? 2509 &ehci_bus_ops_companion : &ehci_bus_ops_standalone, dev); 2510 for (i = 0; i < s->portnr; i++) { 2511 usb_register_port(&s->bus, &s->ports[i], s, i, &ehci_port_ops, 2512 USB_SPEED_MASK_HIGH); 2513 s->ports[i].dev = 0; 2514 } 2515 2516 s->frame_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ehci_work_timer, s); 2517 s->async_bh = qemu_bh_new(ehci_work_bh, s); 2518 s->device = dev; 2519 2520 s->vmstate = qemu_add_vm_change_state_handler(usb_ehci_vm_state_change, s); 2521 } 2522 2523 void usb_ehci_unrealize(EHCIState *s, DeviceState *dev, Error **errp) 2524 { 2525 trace_usb_ehci_unrealize(); 2526 2527 if (s->frame_timer) { 2528 timer_del(s->frame_timer); 2529 timer_free(s->frame_timer); 2530 s->frame_timer = NULL; 2531 } 2532 if (s->async_bh) { 2533 qemu_bh_delete(s->async_bh); 2534 } 2535 2536 ehci_queues_rip_all(s, 0); 2537 ehci_queues_rip_all(s, 1); 2538 2539 memory_region_del_subregion(&s->mem, &s->mem_caps); 2540 memory_region_del_subregion(&s->mem, &s->mem_opreg); 2541 memory_region_del_subregion(&s->mem, &s->mem_ports); 2542 2543 usb_bus_release(&s->bus); 2544 2545 if (s->vmstate) { 2546 qemu_del_vm_change_state_handler(s->vmstate); 2547 } 2548 } 2549 2550 void usb_ehci_init(EHCIState *s, DeviceState *dev) 2551 { 2552 /* 2.2 host controller interface version */ 2553 s->caps[0x00] = (uint8_t)(s->opregbase - s->capsbase); 2554 s->caps[0x01] = 0x00; 2555 s->caps[0x02] = 0x00; 2556 s->caps[0x03] = 0x01; /* HC version */ 2557 s->caps[0x04] = s->portnr; /* Number of downstream ports */ 2558 s->caps[0x05] = 0x00; /* No companion ports at present */ 2559 s->caps[0x06] = 0x00; 2560 s->caps[0x07] = 0x00; 2561 s->caps[0x08] = 0x80; /* We can cache whole frame, no 64-bit */ 2562 s->caps[0x0a] = 0x00; 2563 s->caps[0x0b] = 0x00; 2564 2565 QTAILQ_INIT(&s->aqueues); 2566 QTAILQ_INIT(&s->pqueues); 2567 usb_packet_init(&s->ipacket); 2568 2569 memory_region_init(&s->mem, OBJECT(dev), "ehci", MMIO_SIZE); 2570 memory_region_init_io(&s->mem_caps, OBJECT(dev), &ehci_mmio_caps_ops, s, 2571 "capabilities", CAPA_SIZE); 2572 memory_region_init_io(&s->mem_opreg, OBJECT(dev), &ehci_mmio_opreg_ops, s, 2573 "operational", s->portscbase); 2574 memory_region_init_io(&s->mem_ports, OBJECT(dev), &ehci_mmio_port_ops, s, 2575 "ports", 4 * s->portnr); 2576 2577 memory_region_add_subregion(&s->mem, s->capsbase, &s->mem_caps); 2578 memory_region_add_subregion(&s->mem, s->opregbase, &s->mem_opreg); 2579 memory_region_add_subregion(&s->mem, s->opregbase + s->portscbase, 2580 &s->mem_ports); 2581 } 2582 2583 void usb_ehci_finalize(EHCIState *s) 2584 { 2585 usb_packet_cleanup(&s->ipacket); 2586 } 2587 2588 /* 2589 * vim: expandtab ts=4 2590 */ 2591