1 /* $NetBSD: motg.c,v 1.35 2020/03/14 02:35:33 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2004, 2011, 2012, 2014 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology, Jared D. McNeill (jmcneill@invisible.ca), 10 * Matthew R. Green (mrg@eterna.com.au), and Manuel Bouyer (bouyer@netbsd.org). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 35 /* 36 * This file contains the driver for the Mentor Graphics Inventra USB 37 * 2.0 High Speed Dual-Role controller. 38 * 39 * NOTE: The current implementation only supports Device Side Mode! 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: motg.c,v 1.35 2020/03/14 02:35:33 christos Exp $"); 44 45 #ifdef _KERNEL_OPT 46 #include "opt_usb.h" 47 #endif 48 49 #include <sys/param.h> 50 51 #include <sys/bus.h> 52 #include <sys/cpu.h> 53 #include <sys/device.h> 54 #include <sys/kernel.h> 55 #include <sys/kmem.h> 56 #include <sys/proc.h> 57 #include <sys/queue.h> 58 #include <sys/select.h> 59 #include <sys/sysctl.h> 60 #include <sys/systm.h> 61 62 #include <machine/endian.h> 63 64 #include <dev/usb/usb.h> 65 #include <dev/usb/usbdi.h> 66 #include <dev/usb/usbdivar.h> 67 #include <dev/usb/usb_mem.h> 68 #include <dev/usb/usbhist.h> 69 70 #include <dev/usb/motgreg.h> 71 #include <dev/usb/motgvar.h> 72 #include <dev/usb/usbroothub.h> 73 74 #ifdef USB_DEBUG 75 #ifndef MOTG_DEBUG 76 #define motgdebug 0 77 #else 78 int motgdebug = 0; 79 80 SYSCTL_SETUP(sysctl_hw_motg_setup, "sysctl hw.motg setup") 81 { 82 int err; 83 const struct sysctlnode *rnode; 84 const struct sysctlnode *cnode; 85 86 err = sysctl_createv(clog, 0, NULL, &rnode, 87 CTLFLAG_PERMANENT, CTLTYPE_NODE, "motg", 88 SYSCTL_DESCR("motg global controls"), 89 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 90 91 if (err) 92 goto fail; 93 94 /* control debugging printfs */ 95 err = sysctl_createv(clog, 0, &rnode, &cnode, 96 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 97 "debug", SYSCTL_DESCR("Enable debugging output"), 98 NULL, 0, &motgdebug, sizeof(motgdebug), CTL_CREATE, CTL_EOL); 99 if (err) 100 goto fail; 101 102 return; 103 fail: 104 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 105 } 106 107 #endif /* MOTG_DEBUG */ 108 #endif /* USB_DEBUG */ 109 110 #define MD_ROOT 0x0002 111 #define MD_CTRL 0x0004 112 #define MD_BULK 0x0008 113 114 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(motgdebug,1,FMT,A,B,C,D) 115 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGM(motgdebug,N,FMT,A,B,C,D) 116 #define MOTGHIST_FUNC() USBHIST_FUNC() 117 #define MOTGHIST_CALLED(name) USBHIST_CALLED(motgdebug) 118 119 120 /* various timeouts, for various speeds */ 121 /* control NAK timeouts */ 122 #define NAK_TO_CTRL 10 /* 1024 frames, about 1s */ 123 #define NAK_TO_CTRL_HIGH 13 /* 8k microframes, about 0.8s */ 124 125 /* intr/iso polling intervals */ 126 #define POLL_TO 100 /* 100 frames, about 0.1s */ 127 #define POLL_TO_HIGH 10 /* 100 microframes, about 0.12s */ 128 129 /* bulk NAK timeouts */ 130 #define NAK_TO_BULK 0 /* disabled */ 131 #define NAK_TO_BULK_HIGH 0 132 133 static void motg_hub_change(struct motg_softc *); 134 135 static usbd_status motg_root_intr_transfer(struct usbd_xfer *); 136 static usbd_status motg_root_intr_start(struct usbd_xfer *); 137 static void motg_root_intr_abort(struct usbd_xfer *); 138 static void motg_root_intr_close(struct usbd_pipe *); 139 static void motg_root_intr_done(struct usbd_xfer *); 140 141 static usbd_status motg_open(struct usbd_pipe *); 142 static void motg_poll(struct usbd_bus *); 143 static void motg_softintr(void *); 144 static struct usbd_xfer * 145 motg_allocx(struct usbd_bus *, unsigned int); 146 static void motg_freex(struct usbd_bus *, struct usbd_xfer *); 147 static bool motg_dying(struct usbd_bus *); 148 static void motg_get_lock(struct usbd_bus *, kmutex_t **); 149 static int motg_roothub_ctrl(struct usbd_bus *, usb_device_request_t *, 150 void *, int); 151 152 static void motg_noop(struct usbd_pipe *pipe); 153 static usbd_status motg_portreset(struct motg_softc*); 154 155 static usbd_status motg_device_ctrl_transfer(struct usbd_xfer *); 156 static usbd_status motg_device_ctrl_start(struct usbd_xfer *); 157 static void motg_device_ctrl_abort(struct usbd_xfer *); 158 static void motg_device_ctrl_close(struct usbd_pipe *); 159 static void motg_device_ctrl_done(struct usbd_xfer *); 160 static usbd_status motg_device_ctrl_start1(struct motg_softc *); 161 static void motg_device_ctrl_read(struct usbd_xfer *); 162 static void motg_device_ctrl_intr_rx(struct motg_softc *); 163 static void motg_device_ctrl_intr_tx(struct motg_softc *); 164 165 static usbd_status motg_device_data_transfer(struct usbd_xfer *); 166 static usbd_status motg_device_data_start(struct usbd_xfer *); 167 static usbd_status motg_device_data_start1(struct motg_softc *, 168 struct motg_hw_ep *); 169 static void motg_device_data_abort(struct usbd_xfer *); 170 static void motg_device_data_close(struct usbd_pipe *); 171 static void motg_device_data_done(struct usbd_xfer *); 172 static void motg_device_intr_rx(struct motg_softc *, int); 173 static void motg_device_intr_tx(struct motg_softc *, int); 174 static void motg_device_data_read(struct usbd_xfer *); 175 static void motg_device_data_write(struct usbd_xfer *); 176 177 static void motg_device_clear_toggle(struct usbd_pipe *); 178 static void motg_abortx(struct usbd_xfer *); 179 180 #define UBARR(sc) bus_space_barrier((sc)->sc_iot, (sc)->sc_ioh, 0, (sc)->sc_size, \ 181 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE) 182 #define UWRITE1(sc, r, x) \ 183 do { UBARR(sc); bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \ 184 } while (/*CONSTCOND*/0) 185 #define UWRITE2(sc, r, x) \ 186 do { UBARR(sc); bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \ 187 } while (/*CONSTCOND*/0) 188 #define UWRITE4(sc, r, x) \ 189 do { UBARR(sc); bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \ 190 } while (/*CONSTCOND*/0) 191 192 static __inline uint32_t 193 UREAD1(struct motg_softc *sc, bus_size_t r) 194 { 195 196 UBARR(sc); 197 return bus_space_read_1(sc->sc_iot, sc->sc_ioh, r); 198 } 199 static __inline uint32_t 200 UREAD2(struct motg_softc *sc, bus_size_t r) 201 { 202 203 UBARR(sc); 204 return bus_space_read_2(sc->sc_iot, sc->sc_ioh, r); 205 } 206 207 #if 0 208 static __inline uint32_t 209 UREAD4(struct motg_softc *sc, bus_size_t r) 210 { 211 212 UBARR(sc); 213 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, r); 214 } 215 #endif 216 217 static void 218 musbotg_pull_common(struct motg_softc *sc, uint8_t on) 219 { 220 uint8_t val; 221 222 val = UREAD1(sc, MUSB2_REG_POWER); 223 if (on) 224 val |= MUSB2_MASK_SOFTC; 225 else 226 val &= ~MUSB2_MASK_SOFTC; 227 228 UWRITE1(sc, MUSB2_REG_POWER, val); 229 } 230 231 const struct usbd_bus_methods motg_bus_methods = { 232 .ubm_open = motg_open, 233 .ubm_softint = motg_softintr, 234 .ubm_dopoll = motg_poll, 235 .ubm_allocx = motg_allocx, 236 .ubm_freex = motg_freex, 237 .ubm_abortx = motg_abortx, 238 .ubm_dying = motg_dying, 239 .ubm_getlock = motg_get_lock, 240 .ubm_rhctrl = motg_roothub_ctrl, 241 }; 242 243 const struct usbd_pipe_methods motg_root_intr_methods = { 244 .upm_transfer = motg_root_intr_transfer, 245 .upm_start = motg_root_intr_start, 246 .upm_abort = motg_root_intr_abort, 247 .upm_close = motg_root_intr_close, 248 .upm_cleartoggle = motg_noop, 249 .upm_done = motg_root_intr_done, 250 }; 251 252 const struct usbd_pipe_methods motg_device_ctrl_methods = { 253 .upm_transfer = motg_device_ctrl_transfer, 254 .upm_start = motg_device_ctrl_start, 255 .upm_abort = motg_device_ctrl_abort, 256 .upm_close = motg_device_ctrl_close, 257 .upm_cleartoggle = motg_noop, 258 .upm_done = motg_device_ctrl_done, 259 }; 260 261 const struct usbd_pipe_methods motg_device_data_methods = { 262 .upm_transfer = motg_device_data_transfer, 263 .upm_start = motg_device_data_start, 264 .upm_abort = motg_device_data_abort, 265 .upm_close = motg_device_data_close, 266 .upm_cleartoggle = motg_device_clear_toggle, 267 .upm_done = motg_device_data_done, 268 }; 269 270 int 271 motg_init(struct motg_softc *sc) 272 { 273 uint32_t nrx, ntx, val; 274 int dynfifo; 275 int offset, i; 276 277 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 278 279 if (sc->sc_mode == MOTG_MODE_DEVICE) 280 return ENOTSUP; /* not supported */ 281 282 /* disable all interrupts */ 283 UWRITE1(sc, MUSB2_REG_INTUSBE, 0); 284 UWRITE2(sc, MUSB2_REG_INTTXE, 0); 285 UWRITE2(sc, MUSB2_REG_INTRXE, 0); 286 /* disable pullup */ 287 288 musbotg_pull_common(sc, 0); 289 290 #ifdef MUSB2_REG_RXDBDIS 291 /* disable double packet buffering XXX what's this ? */ 292 UWRITE2(sc, MUSB2_REG_RXDBDIS, 0xFFFF); 293 UWRITE2(sc, MUSB2_REG_TXDBDIS, 0xFFFF); 294 #endif 295 296 /* enable HighSpeed and ISO Update flags */ 297 298 UWRITE1(sc, MUSB2_REG_POWER, 299 MUSB2_MASK_HSENAB | MUSB2_MASK_ISOUPD); 300 301 if (sc->sc_mode == MOTG_MODE_DEVICE) { 302 /* clear Session bit, if set */ 303 val = UREAD1(sc, MUSB2_REG_DEVCTL); 304 val &= ~MUSB2_MASK_SESS; 305 UWRITE1(sc, MUSB2_REG_DEVCTL, val); 306 } else { 307 /* Enter session for Host mode */ 308 val = UREAD1(sc, MUSB2_REG_DEVCTL); 309 val |= MUSB2_MASK_SESS; 310 UWRITE1(sc, MUSB2_REG_DEVCTL, val); 311 } 312 delay(1000); 313 DPRINTF("DEVCTL %#jx", UREAD1(sc, MUSB2_REG_DEVCTL), 0, 0, 0); 314 315 /* disable testmode */ 316 317 UWRITE1(sc, MUSB2_REG_TESTMODE, 0); 318 319 #ifdef MUSB2_REG_MISC 320 /* set default value */ 321 322 UWRITE1(sc, MUSB2_REG_MISC, 0); 323 #endif 324 325 /* select endpoint index 0 */ 326 327 UWRITE1(sc, MUSB2_REG_EPINDEX, 0); 328 329 if (sc->sc_ep_max == 0) { 330 /* read out number of endpoints */ 331 nrx = (UREAD1(sc, MUSB2_REG_EPINFO) / 16); 332 333 ntx = (UREAD1(sc, MUSB2_REG_EPINFO) % 16); 334 335 /* these numbers exclude the control endpoint */ 336 337 DPRINTFN(1,"RX/TX endpoints: %ju/%ju", nrx, ntx, 0, 0); 338 339 sc->sc_ep_max = MAX(nrx, ntx); 340 } else { 341 nrx = ntx = sc->sc_ep_max; 342 } 343 if (sc->sc_ep_max == 0) { 344 aprint_error_dev(sc->sc_dev, " no endpoints\n"); 345 return -1; 346 } 347 KASSERT(sc->sc_ep_max <= MOTG_MAX_HW_EP); 348 /* read out configuration data */ 349 val = UREAD1(sc, MUSB2_REG_CONFDATA); 350 351 DPRINTF("Config Data: 0x%02jx", val, 0, 0, 0); 352 353 dynfifo = (val & MUSB2_MASK_CD_DYNFIFOSZ) ? 1 : 0; 354 355 if (dynfifo) { 356 aprint_normal_dev(sc->sc_dev, "Dynamic FIFO sizing detected, " 357 "assuming 16Kbytes of FIFO RAM\n"); 358 } 359 360 DPRINTF("HW version: 0x%04jx\n", UREAD1(sc, MUSB2_REG_HWVERS), 0, 0, 0); 361 362 /* initialise endpoint profiles */ 363 sc->sc_in_ep[0].ep_fifo_size = 64; 364 sc->sc_out_ep[0].ep_fifo_size = 0; /* not used */ 365 sc->sc_out_ep[0].ep_number = sc->sc_in_ep[0].ep_number = 0; 366 SIMPLEQ_INIT(&sc->sc_in_ep[0].ep_pipes); 367 offset = 64; 368 369 for (i = 1; i <= sc->sc_ep_max; i++) { 370 int fiforx_size, fifotx_size, fifo_size; 371 372 /* select endpoint */ 373 UWRITE1(sc, MUSB2_REG_EPINDEX, i); 374 375 if (sc->sc_ep_fifosize) { 376 fiforx_size = fifotx_size = sc->sc_ep_fifosize; 377 } else { 378 val = UREAD1(sc, MUSB2_REG_FSIZE); 379 fiforx_size = (val & MUSB2_MASK_RX_FSIZE) >> 4; 380 fifotx_size = (val & MUSB2_MASK_TX_FSIZE); 381 } 382 383 DPRINTF("Endpoint %ju FIFO size: IN=%ju, OUT=%ju, DYN=%jd", 384 i, fifotx_size, fiforx_size, dynfifo); 385 386 if (dynfifo) { 387 if (sc->sc_ep_fifosize) { 388 fifo_size = ffs(sc->sc_ep_fifosize) - 1; 389 } else { 390 if (i < 3) { 391 fifo_size = 12; /* 4K */ 392 } else if (i < 10) { 393 fifo_size = 10; /* 1K */ 394 } else { 395 fifo_size = 7; /* 128 bytes */ 396 } 397 } 398 if (fiforx_size && (i <= nrx)) { 399 fiforx_size = fifo_size; 400 if (fifo_size > 7) { 401 #if 0 402 UWRITE1(sc, MUSB2_REG_RXFIFOSZ, 403 MUSB2_VAL_FIFOSZ(fifo_size) | 404 MUSB2_MASK_FIFODB); 405 #else 406 UWRITE1(sc, MUSB2_REG_RXFIFOSZ, 407 MUSB2_VAL_FIFOSZ(fifo_size)); 408 #endif 409 } else { 410 UWRITE1(sc, MUSB2_REG_RXFIFOSZ, 411 MUSB2_VAL_FIFOSZ(fifo_size)); 412 } 413 UWRITE2(sc, MUSB2_REG_RXFIFOADD, 414 offset >> 3); 415 offset += (1 << fiforx_size); 416 } 417 if (fifotx_size && (i <= ntx)) { 418 fifotx_size = fifo_size; 419 if (fifo_size > 7) { 420 #if 0 421 UWRITE1(sc, MUSB2_REG_TXFIFOSZ, 422 MUSB2_VAL_FIFOSZ(fifo_size) | 423 MUSB2_MASK_FIFODB); 424 #else 425 UWRITE1(sc, MUSB2_REG_TXFIFOSZ, 426 MUSB2_VAL_FIFOSZ(fifo_size)); 427 #endif 428 } else { 429 UWRITE1(sc, MUSB2_REG_TXFIFOSZ, 430 MUSB2_VAL_FIFOSZ(fifo_size)); 431 } 432 433 UWRITE2(sc, MUSB2_REG_TXFIFOADD, 434 offset >> 3); 435 436 offset += (1 << fifotx_size); 437 } 438 } 439 if (fiforx_size && (i <= nrx)) { 440 sc->sc_in_ep[i].ep_fifo_size = (1 << fiforx_size); 441 SIMPLEQ_INIT(&sc->sc_in_ep[i].ep_pipes); 442 } 443 if (fifotx_size && (i <= ntx)) { 444 sc->sc_out_ep[i].ep_fifo_size = (1 << fifotx_size); 445 SIMPLEQ_INIT(&sc->sc_out_ep[i].ep_pipes); 446 } 447 sc->sc_out_ep[i].ep_number = sc->sc_in_ep[i].ep_number = i; 448 } 449 450 451 DPRINTF("Dynamic FIFO size = %jd bytes", offset, 0, 0, 0); 452 453 /* turn on default interrupts */ 454 455 if (sc->sc_mode == MOTG_MODE_HOST) { 456 UWRITE1(sc, MUSB2_REG_INTUSBE, 0xff); 457 UWRITE2(sc, MUSB2_REG_INTTXE, 0xffff); 458 UWRITE2(sc, MUSB2_REG_INTRXE, 0xffff); 459 } else 460 UWRITE1(sc, MUSB2_REG_INTUSBE, MUSB2_MASK_IRESET); 461 462 sc->sc_xferpool = pool_cache_init(sizeof(struct motg_xfer), 0, 0, 0, 463 "motgxfer", NULL, IPL_USB, NULL, NULL, NULL); 464 465 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 466 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB); 467 468 /* Set up the bus struct. */ 469 sc->sc_bus.ub_methods = &motg_bus_methods; 470 sc->sc_bus.ub_pipesize= sizeof(struct motg_pipe); 471 sc->sc_bus.ub_revision = USBREV_2_0; 472 sc->sc_bus.ub_usedma = false; 473 sc->sc_bus.ub_hcpriv = sc; 474 sc->sc_child = config_found(sc->sc_dev, &sc->sc_bus, usbctlprint); 475 return 0; 476 } 477 478 static int 479 motg_select_ep(struct motg_softc *sc, struct usbd_pipe *pipe) 480 { 481 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe); 482 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc; 483 struct motg_hw_ep *ep; 484 int i, size; 485 486 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 487 488 ep = (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) ? 489 sc->sc_in_ep : sc->sc_out_ep; 490 size = UE_GET_SIZE(UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize)); 491 492 for (i = sc->sc_ep_max; i >= 1; i--) { 493 DPRINTF(UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN ? 494 "in_ep[%jd].ep_fifo_size %jd size %jd ref %jd" : 495 "out_ep[%jd].ep_fifo_size %jd size %jd ref %jd", i, 496 ep[i].ep_fifo_size, size, ep[i].refcount); 497 if (ep[i].ep_fifo_size >= size) { 498 /* found a suitable endpoint */ 499 otgpipe->hw_ep = &ep[i]; 500 mutex_enter(&sc->sc_lock); 501 if (otgpipe->hw_ep->refcount > 0) { 502 /* no luck, try next */ 503 mutex_exit(&sc->sc_lock); 504 otgpipe->hw_ep = NULL; 505 } else { 506 otgpipe->hw_ep->refcount++; 507 SIMPLEQ_INSERT_TAIL(&otgpipe->hw_ep->ep_pipes, 508 otgpipe, ep_pipe_list); 509 mutex_exit(&sc->sc_lock); 510 return 0; 511 } 512 } 513 } 514 return -1; 515 } 516 517 /* Open a new pipe. */ 518 usbd_status 519 motg_open(struct usbd_pipe *pipe) 520 { 521 struct motg_softc *sc = MOTG_PIPE2SC(pipe); 522 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe); 523 usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc; 524 uint8_t rhaddr = pipe->up_dev->ud_bus->ub_rhaddr; 525 526 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 527 528 DPRINTF("pipe=%#jx, addr=%jd, endpt=%jd (%jd)", (uintptr_t)pipe, 529 pipe->up_dev->ud_addr, ed->bEndpointAddress, rhaddr); 530 531 if (sc->sc_dying) 532 return USBD_IOERROR; 533 534 /* toggle state needed for bulk endpoints */ 535 otgpipe->nexttoggle = pipe->up_endpoint->ue_toggle; 536 537 if (pipe->up_dev->ud_addr == rhaddr) { 538 switch (ed->bEndpointAddress) { 539 case USB_CONTROL_ENDPOINT: 540 pipe->up_methods = &roothub_ctrl_methods; 541 break; 542 case UE_DIR_IN | USBROOTHUB_INTR_ENDPT: 543 pipe->up_methods = &motg_root_intr_methods; 544 break; 545 default: 546 return USBD_INVAL; 547 } 548 } else { 549 switch (ed->bmAttributes & UE_XFERTYPE) { 550 case UE_CONTROL: 551 pipe->up_methods = &motg_device_ctrl_methods; 552 /* always use sc_in_ep[0] for in and out */ 553 otgpipe->hw_ep = &sc->sc_in_ep[0]; 554 mutex_enter(&sc->sc_lock); 555 otgpipe->hw_ep->refcount++; 556 SIMPLEQ_INSERT_TAIL(&otgpipe->hw_ep->ep_pipes, 557 otgpipe, ep_pipe_list); 558 mutex_exit(&sc->sc_lock); 559 break; 560 case UE_BULK: 561 case UE_INTERRUPT: 562 DPRINTFN(MD_BULK, 563 "type %jd dir %jd pipe wMaxPacketSize %jd", 564 UE_GET_XFERTYPE(ed->bmAttributes), 565 UE_GET_DIR(pipe->up_endpoint->ue_edesc->bEndpointAddress), 566 UGETW(pipe->up_endpoint->ue_edesc->wMaxPacketSize), 0); 567 if (motg_select_ep(sc, pipe) != 0) 568 goto bad; 569 KASSERT(otgpipe->hw_ep != NULL); 570 pipe->up_methods = &motg_device_data_methods; 571 otgpipe->nexttoggle = pipe->up_endpoint->ue_toggle; 572 break; 573 default: 574 goto bad; 575 #ifdef notyet 576 case UE_ISOCHRONOUS: 577 ... 578 break; 579 #endif /* notyet */ 580 } 581 } 582 return USBD_NORMAL_COMPLETION; 583 584 bad: 585 return USBD_NOMEM; 586 } 587 588 void 589 motg_softintr(void *v) 590 { 591 struct usbd_bus *bus = v; 592 struct motg_softc *sc = MOTG_BUS2SC(bus); 593 uint16_t rx_status, tx_status; 594 uint8_t ctrl_status; 595 uint32_t val; 596 int i; 597 598 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 599 600 KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock)); 601 602 DPRINTFN(MD_ROOT | MD_CTRL, "sc %#jx", (uintptr_t)sc, 0 ,0 ,0); 603 604 mutex_spin_enter(&sc->sc_intr_lock); 605 rx_status = sc->sc_intr_rx_ep; 606 sc->sc_intr_rx_ep = 0; 607 tx_status = sc->sc_intr_tx_ep; 608 sc->sc_intr_tx_ep = 0; 609 ctrl_status = sc->sc_intr_ctrl; 610 sc->sc_intr_ctrl = 0; 611 mutex_spin_exit(&sc->sc_intr_lock); 612 613 ctrl_status |= UREAD1(sc, MUSB2_REG_INTUSB); 614 615 if (ctrl_status & (MUSB2_MASK_IRESET | 616 MUSB2_MASK_IRESUME | MUSB2_MASK_ISUSP | 617 MUSB2_MASK_ICONN | MUSB2_MASK_IDISC)) { 618 DPRINTFN(MD_ROOT | MD_CTRL, "bus %#jx", ctrl_status, 0, 0, 0); 619 620 if (ctrl_status & MUSB2_MASK_IRESET) { 621 sc->sc_isreset = 1; 622 sc->sc_port_suspended = 0; 623 sc->sc_port_suspended_change = 1; 624 sc->sc_connected_changed = 1; 625 sc->sc_port_enabled = 1; 626 627 val = UREAD1(sc, MUSB2_REG_POWER); 628 if (val & MUSB2_MASK_HSMODE) 629 sc->sc_high_speed = 1; 630 else 631 sc->sc_high_speed = 0; 632 DPRINTFN(MD_ROOT | MD_CTRL, "speed %jd", sc->sc_high_speed, 633 0, 0, 0); 634 635 /* turn off interrupts */ 636 val = MUSB2_MASK_IRESET; 637 val &= ~MUSB2_MASK_IRESUME; 638 val |= MUSB2_MASK_ISUSP; 639 UWRITE1(sc, MUSB2_REG_INTUSBE, val); 640 UWRITE2(sc, MUSB2_REG_INTTXE, 0); 641 UWRITE2(sc, MUSB2_REG_INTRXE, 0); 642 } 643 if (ctrl_status & MUSB2_MASK_IRESUME) { 644 if (sc->sc_port_suspended) { 645 sc->sc_port_suspended = 0; 646 sc->sc_port_suspended_change = 1; 647 val = UREAD1(sc, MUSB2_REG_INTUSBE); 648 /* disable resume interrupt */ 649 val &= ~MUSB2_MASK_IRESUME; 650 /* enable suspend interrupt */ 651 val |= MUSB2_MASK_ISUSP; 652 UWRITE1(sc, MUSB2_REG_INTUSBE, val); 653 } 654 } else if (ctrl_status & MUSB2_MASK_ISUSP) { 655 if (!sc->sc_port_suspended) { 656 sc->sc_port_suspended = 1; 657 sc->sc_port_suspended_change = 1; 658 659 val = UREAD1(sc, MUSB2_REG_INTUSBE); 660 /* disable suspend interrupt */ 661 val &= ~MUSB2_MASK_ISUSP; 662 /* enable resume interrupt */ 663 val |= MUSB2_MASK_IRESUME; 664 UWRITE1(sc, MUSB2_REG_INTUSBE, val); 665 } 666 } 667 if (ctrl_status & MUSB2_MASK_ICONN) { 668 sc->sc_connected = 1; 669 sc->sc_connected_changed = 1; 670 sc->sc_isreset = 1; 671 sc->sc_port_enabled = 1; 672 } else if (ctrl_status & MUSB2_MASK_IDISC) { 673 sc->sc_connected = 0; 674 sc->sc_connected_changed = 1; 675 sc->sc_isreset = 0; 676 sc->sc_port_enabled = 0; 677 } 678 679 /* complete root HUB interrupt endpoint */ 680 681 motg_hub_change(sc); 682 } 683 /* 684 * read in interrupt status and mix with the status we 685 * got from the wrapper 686 */ 687 rx_status |= UREAD2(sc, MUSB2_REG_INTRX); 688 tx_status |= UREAD2(sc, MUSB2_REG_INTTX); 689 690 KASSERTMSG((rx_status & 0x01) == 0, "ctrl_rx %08x", rx_status); 691 if (tx_status & 0x01) 692 motg_device_ctrl_intr_tx(sc); 693 for (i = 1; i <= sc->sc_ep_max; i++) { 694 if (rx_status & (0x01 << i)) 695 motg_device_intr_rx(sc, i); 696 if (tx_status & (0x01 << i)) 697 motg_device_intr_tx(sc, i); 698 } 699 return; 700 } 701 702 void 703 motg_poll(struct usbd_bus *bus) 704 { 705 struct motg_softc *sc = MOTG_BUS2SC(bus); 706 707 sc->sc_intr_poll(sc->sc_intr_poll_arg); 708 mutex_enter(&sc->sc_lock); 709 motg_softintr(bus); 710 mutex_exit(&sc->sc_lock); 711 } 712 713 int 714 motg_intr(struct motg_softc *sc, uint16_t rx_ep, uint16_t tx_ep, 715 uint8_t ctrl) 716 { 717 KASSERT(mutex_owned(&sc->sc_intr_lock)); 718 sc->sc_intr_tx_ep = tx_ep; 719 sc->sc_intr_rx_ep = rx_ep; 720 sc->sc_intr_ctrl = ctrl; 721 722 if (!sc->sc_bus.ub_usepolling) { 723 usb_schedsoftintr(&sc->sc_bus); 724 } 725 return 1; 726 } 727 728 int 729 motg_intr_vbus(struct motg_softc *sc, int vbus) 730 { 731 uint8_t val; 732 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 733 734 if (sc->sc_mode == MOTG_MODE_HOST && vbus == 0) { 735 DPRINTF("vbus down, try to re-enable", 0, 0, 0, 0); 736 /* try to re-enter session for Host mode */ 737 val = UREAD1(sc, MUSB2_REG_DEVCTL); 738 val |= MUSB2_MASK_SESS; 739 UWRITE1(sc, MUSB2_REG_DEVCTL, val); 740 } 741 return 1; 742 } 743 744 struct usbd_xfer * 745 motg_allocx(struct usbd_bus *bus, unsigned int nframes) 746 { 747 struct motg_softc *sc = MOTG_BUS2SC(bus); 748 struct usbd_xfer *xfer; 749 750 xfer = pool_cache_get(sc->sc_xferpool, PR_WAITOK); 751 if (xfer != NULL) { 752 memset(xfer, 0, sizeof(struct motg_xfer)); 753 #ifdef DIAGNOSTIC 754 xfer->ux_state = XFER_BUSY; 755 #endif 756 } 757 return xfer; 758 } 759 760 void 761 motg_freex(struct usbd_bus *bus, struct usbd_xfer *xfer) 762 { 763 struct motg_softc *sc = MOTG_BUS2SC(bus); 764 765 #ifdef DIAGNOSTIC 766 if (xfer->ux_state != XFER_BUSY && 767 xfer->ux_status != USBD_NOT_STARTED) { 768 printf("motg_freex: xfer=%p not busy, 0x%08x\n", xfer, 769 xfer->ux_state); 770 } 771 xfer->ux_state = XFER_FREE; 772 #endif 773 pool_cache_put(sc->sc_xferpool, xfer); 774 } 775 776 static bool 777 motg_dying(struct usbd_bus *bus) 778 { 779 struct motg_softc *sc = MOTG_BUS2SC(bus); 780 781 return sc->sc_dying; 782 } 783 784 static void 785 motg_get_lock(struct usbd_bus *bus, kmutex_t **lock) 786 { 787 struct motg_softc *sc = MOTG_BUS2SC(bus); 788 789 *lock = &sc->sc_lock; 790 } 791 792 /* 793 * Routines to emulate the root hub. 794 */ 795 Static int 796 motg_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req, 797 void *buf, int buflen) 798 { 799 struct motg_softc *sc = MOTG_BUS2SC(bus); 800 int status, change, totlen = 0; 801 uint16_t len, value, index; 802 usb_port_status_t ps; 803 usbd_status err; 804 uint32_t val; 805 806 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 807 808 if (sc->sc_dying) 809 return -1; 810 811 DPRINTFN(MD_ROOT, "type=0x%02jx request=%02jx", req->bmRequestType, 812 req->bRequest, 0, 0); 813 814 len = UGETW(req->wLength); 815 value = UGETW(req->wValue); 816 index = UGETW(req->wIndex); 817 818 #define C(x,y) ((x) | ((y) << 8)) 819 switch (C(req->bRequest, req->bmRequestType)) { 820 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 821 DPRINTFN(MD_ROOT, "wValue=0x%04jx", value, 0, 0, 0); 822 switch (value) { 823 #define sd ((usb_string_descriptor_t *)buf) 824 case C(2, UDESC_STRING): 825 /* Product */ 826 totlen = usb_makestrdesc(sd, len, "MOTG root hub"); 827 break; 828 #undef sd 829 default: 830 /* default from usbroothub */ 831 return buflen; 832 } 833 break; 834 /* Hub requests */ 835 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 836 break; 837 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 838 DPRINTFN(MD_ROOT, 839 "UR_CLEAR_PORT_FEATURE port=%jd feature=%jd", index, value, 840 0, 0); 841 if (index != 1) { 842 return -1; 843 } 844 switch (value) { 845 case UHF_PORT_ENABLE: 846 sc->sc_port_enabled = 0; 847 break; 848 case UHF_PORT_SUSPEND: 849 if (sc->sc_port_suspended != 0) { 850 val = UREAD1(sc, MUSB2_REG_POWER); 851 val &= ~MUSB2_MASK_SUSPMODE; 852 val |= MUSB2_MASK_RESUME; 853 UWRITE1(sc, MUSB2_REG_POWER, val); 854 /* wait 20 milliseconds */ 855 usb_delay_ms(&sc->sc_bus, 20); 856 val = UREAD1(sc, MUSB2_REG_POWER); 857 val &= ~MUSB2_MASK_RESUME; 858 UWRITE1(sc, MUSB2_REG_POWER, val); 859 sc->sc_port_suspended = 0; 860 sc->sc_port_suspended_change = 1; 861 } 862 break; 863 case UHF_PORT_RESET: 864 break; 865 case UHF_C_PORT_CONNECTION: 866 break; 867 case UHF_C_PORT_ENABLE: 868 break; 869 case UHF_C_PORT_OVER_CURRENT: 870 break; 871 case UHF_C_PORT_RESET: 872 sc->sc_isreset = 0; 873 break; 874 case UHF_PORT_POWER: 875 /* XXX todo */ 876 break; 877 case UHF_PORT_CONNECTION: 878 case UHF_PORT_OVER_CURRENT: 879 case UHF_PORT_LOW_SPEED: 880 case UHF_C_PORT_SUSPEND: 881 default: 882 return -1; 883 } 884 break; 885 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER): 886 return -1; 887 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 888 if (len == 0) 889 break; 890 if ((value & 0xff) != 0) { 891 return -1; 892 } 893 totlen = buflen; 894 break; 895 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 896 if (len != 4) { 897 return -1; 898 } 899 memset(buf, 0, len); 900 totlen = len; 901 break; 902 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 903 if (index != 1) { 904 return -1; 905 } 906 if (len != 4) { 907 return -1; 908 } 909 status = change = 0; 910 if (sc->sc_connected) 911 status |= UPS_CURRENT_CONNECT_STATUS; 912 if (sc->sc_connected_changed) { 913 change |= UPS_C_CONNECT_STATUS; 914 sc->sc_connected_changed = 0; 915 } 916 if (sc->sc_port_enabled) 917 status |= UPS_PORT_ENABLED; 918 if (sc->sc_port_enabled_changed) { 919 change |= UPS_C_PORT_ENABLED; 920 sc->sc_port_enabled_changed = 0; 921 } 922 if (sc->sc_port_suspended) 923 status |= UPS_SUSPEND; 924 if (sc->sc_high_speed) 925 status |= UPS_HIGH_SPEED; 926 status |= UPS_PORT_POWER; /* XXX */ 927 if (sc->sc_isreset) 928 change |= UPS_C_PORT_RESET; 929 USETW(ps.wPortStatus, status); 930 USETW(ps.wPortChange, change); 931 totlen = uimin(len, sizeof(ps)); 932 memcpy(buf, &ps, totlen); 933 break; 934 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 935 return -1; 936 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 937 break; 938 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 939 if (index != 1) { 940 return -1; 941 } 942 switch(value) { 943 case UHF_PORT_ENABLE: 944 sc->sc_port_enabled = 1; 945 break; 946 case UHF_PORT_SUSPEND: 947 if (sc->sc_port_suspended == 0) { 948 val = UREAD1(sc, MUSB2_REG_POWER); 949 val |= MUSB2_MASK_SUSPMODE; 950 UWRITE1(sc, MUSB2_REG_POWER, val); 951 /* wait 20 milliseconds */ 952 usb_delay_ms(&sc->sc_bus, 20); 953 sc->sc_port_suspended = 1; 954 sc->sc_port_suspended_change = 1; 955 } 956 break; 957 case UHF_PORT_RESET: 958 err = motg_portreset(sc); 959 if (err != USBD_NORMAL_COMPLETION) 960 return -1; 961 return 0; 962 case UHF_PORT_POWER: 963 /* XXX todo */ 964 return 0; 965 case UHF_C_PORT_CONNECTION: 966 case UHF_C_PORT_ENABLE: 967 case UHF_C_PORT_OVER_CURRENT: 968 case UHF_PORT_CONNECTION: 969 case UHF_PORT_OVER_CURRENT: 970 case UHF_PORT_LOW_SPEED: 971 case UHF_C_PORT_SUSPEND: 972 case UHF_C_PORT_RESET: 973 default: 974 return -1; 975 } 976 break; 977 default: 978 /* default from usbroothub */ 979 return buflen; 980 } 981 982 return totlen; 983 } 984 985 /* Abort a root interrupt request. */ 986 void 987 motg_root_intr_abort(struct usbd_xfer *xfer) 988 { 989 struct motg_softc *sc = MOTG_XFER2SC(xfer); 990 991 KASSERT(mutex_owned(&sc->sc_lock)); 992 KASSERT(xfer->ux_pipe->up_intrxfer == xfer); 993 994 /* If xfer has already completed, nothing to do here. */ 995 if (sc->sc_intr_xfer == NULL) 996 return; 997 998 /* 999 * Otherwise, sc->sc_intr_xfer had better be this transfer. 1000 * Cancel it. 1001 */ 1002 KASSERT(sc->sc_intr_xfer == xfer); 1003 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 1004 xfer->ux_status = USBD_CANCELLED; 1005 usb_transfer_complete(xfer); 1006 } 1007 1008 usbd_status 1009 motg_root_intr_transfer(struct usbd_xfer *xfer) 1010 { 1011 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1012 usbd_status err; 1013 1014 /* Insert last in queue. */ 1015 mutex_enter(&sc->sc_lock); 1016 err = usb_insert_transfer(xfer); 1017 mutex_exit(&sc->sc_lock); 1018 if (err) 1019 return err; 1020 1021 /* 1022 * Pipe isn't running (otherwise err would be USBD_INPROG), 1023 * start first 1024 */ 1025 return motg_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)); 1026 } 1027 1028 /* Start a transfer on the root interrupt pipe */ 1029 usbd_status 1030 motg_root_intr_start(struct usbd_xfer *xfer) 1031 { 1032 struct usbd_pipe *pipe = xfer->ux_pipe; 1033 struct motg_softc *sc = MOTG_PIPE2SC(pipe); 1034 const bool polling = sc->sc_bus.ub_usepolling; 1035 1036 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1037 1038 DPRINTFN(MD_ROOT, "xfer=%#jx len=%jd flags=%jd", (uintptr_t)xfer, 1039 xfer->ux_length, xfer->ux_flags, 0); 1040 1041 if (sc->sc_dying) 1042 return USBD_IOERROR; 1043 1044 if (!polling) 1045 mutex_enter(&sc->sc_lock); 1046 KASSERT(sc->sc_intr_xfer == NULL); 1047 sc->sc_intr_xfer = xfer; 1048 xfer->ux_status = USBD_IN_PROGRESS; 1049 if (!polling) 1050 mutex_exit(&sc->sc_lock); 1051 1052 return USBD_IN_PROGRESS; 1053 } 1054 1055 /* Close the root interrupt pipe. */ 1056 void 1057 motg_root_intr_close(struct usbd_pipe *pipe) 1058 { 1059 struct motg_softc *sc __diagused = MOTG_PIPE2SC(pipe); 1060 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1061 1062 KASSERT(mutex_owned(&sc->sc_lock)); 1063 1064 /* 1065 * Caller must guarantee the xfer has completed first, by 1066 * closing the pipe only after normal completion or an abort. 1067 */ 1068 KASSERT(sc->sc_intr_xfer == NULL); 1069 } 1070 1071 void 1072 motg_root_intr_done(struct usbd_xfer *xfer) 1073 { 1074 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1075 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1076 1077 KASSERT(mutex_owned(&sc->sc_lock)); 1078 1079 /* Claim the xfer so it doesn't get completed again. */ 1080 KASSERT(sc->sc_intr_xfer == xfer); 1081 KASSERT(xfer->ux_status != USBD_IN_PROGRESS); 1082 sc->sc_intr_xfer = NULL; 1083 } 1084 1085 void 1086 motg_noop(struct usbd_pipe *pipe) 1087 { 1088 } 1089 1090 static usbd_status 1091 motg_portreset(struct motg_softc *sc) 1092 { 1093 uint32_t val; 1094 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1095 1096 val = UREAD1(sc, MUSB2_REG_POWER); 1097 val |= MUSB2_MASK_RESET; 1098 UWRITE1(sc, MUSB2_REG_POWER, val); 1099 /* Wait for 20 msec */ 1100 usb_delay_ms(&sc->sc_bus, 20); 1101 1102 val = UREAD1(sc, MUSB2_REG_POWER); 1103 val &= ~MUSB2_MASK_RESET; 1104 UWRITE1(sc, MUSB2_REG_POWER, val); 1105 1106 /* determine line speed */ 1107 val = UREAD1(sc, MUSB2_REG_POWER); 1108 if (val & MUSB2_MASK_HSMODE) 1109 sc->sc_high_speed = 1; 1110 else 1111 sc->sc_high_speed = 0; 1112 DPRINTFN(MD_ROOT | MD_CTRL, "speed %jd", sc->sc_high_speed, 0, 0, 0); 1113 1114 sc->sc_isreset = 1; 1115 sc->sc_port_enabled = 1; 1116 return USBD_NORMAL_COMPLETION; 1117 } 1118 1119 /* 1120 * This routine is executed when an interrupt on the root hub is detected 1121 */ 1122 static void 1123 motg_hub_change(struct motg_softc *sc) 1124 { 1125 struct usbd_xfer *xfer = sc->sc_intr_xfer; 1126 struct usbd_pipe *pipe; 1127 u_char *p; 1128 1129 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1130 1131 if (xfer == NULL) 1132 return; /* the interrupt pipe is not open */ 1133 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 1134 1135 pipe = xfer->ux_pipe; 1136 if (pipe->up_dev == NULL || pipe->up_dev->ud_bus == NULL) 1137 return; /* device has detached */ 1138 1139 p = xfer->ux_buf; 1140 p[0] = 1<<1; 1141 xfer->ux_actlen = 1; 1142 xfer->ux_status = USBD_NORMAL_COMPLETION; 1143 usb_transfer_complete(xfer); 1144 } 1145 1146 static uint8_t 1147 motg_speed(uint8_t speed) 1148 { 1149 switch(speed) { 1150 case USB_SPEED_LOW: 1151 return MUSB2_MASK_TI_SPEED_LO; 1152 case USB_SPEED_FULL: 1153 return MUSB2_MASK_TI_SPEED_FS; 1154 case USB_SPEED_HIGH: 1155 return MUSB2_MASK_TI_SPEED_HS; 1156 default: 1157 panic("motg: unknown speed %d", speed); 1158 /* NOTREACHED */ 1159 } 1160 } 1161 1162 static uint8_t 1163 motg_type(uint8_t type) 1164 { 1165 switch(type) { 1166 case UE_CONTROL: 1167 return MUSB2_MASK_TI_PROTO_CTRL; 1168 case UE_ISOCHRONOUS: 1169 return MUSB2_MASK_TI_PROTO_ISOC; 1170 case UE_BULK: 1171 return MUSB2_MASK_TI_PROTO_BULK; 1172 case UE_INTERRUPT: 1173 return MUSB2_MASK_TI_PROTO_INTR; 1174 default: 1175 panic("motg: unknown type %d", type); 1176 /* NOTREACHED */ 1177 } 1178 } 1179 1180 static void 1181 motg_setup_endpoint_tx(struct usbd_xfer *xfer) 1182 { 1183 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1184 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 1185 struct usbd_device *dev = otgpipe->pipe.up_dev; 1186 int epnumber = otgpipe->hw_ep->ep_number; 1187 1188 UWRITE1(sc, MUSB2_REG_TXFADDR(epnumber), dev->ud_addr); 1189 if (dev->ud_myhsport) { 1190 UWRITE1(sc, MUSB2_REG_TXHADDR(epnumber), 1191 dev->ud_myhsport->up_parent->ud_addr); 1192 UWRITE1(sc, MUSB2_REG_TXHUBPORT(epnumber), 1193 dev->ud_myhsport->up_portno); 1194 } else { 1195 UWRITE1(sc, MUSB2_REG_TXHADDR(epnumber), 0); 1196 UWRITE1(sc, MUSB2_REG_TXHUBPORT(epnumber), 0); 1197 } 1198 UWRITE1(sc, MUSB2_REG_TXTI, 1199 motg_speed(dev->ud_speed) | 1200 UE_GET_ADDR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) | 1201 motg_type(UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes)) 1202 ); 1203 if (epnumber == 0) { 1204 if (sc->sc_high_speed) { 1205 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, 1206 NAK_TO_CTRL_HIGH); 1207 } else { 1208 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, NAK_TO_CTRL); 1209 } 1210 } else { 1211 if ((xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE) 1212 == UE_BULK) { 1213 if (sc->sc_high_speed) { 1214 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, 1215 NAK_TO_BULK_HIGH); 1216 } else { 1217 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, NAK_TO_BULK); 1218 } 1219 } else { 1220 if (sc->sc_high_speed) { 1221 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, POLL_TO_HIGH); 1222 } else { 1223 UWRITE1(sc, MUSB2_REG_TXNAKLIMIT, POLL_TO); 1224 } 1225 } 1226 } 1227 } 1228 1229 static void 1230 motg_setup_endpoint_rx(struct usbd_xfer *xfer) 1231 { 1232 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1233 struct usbd_device *dev = xfer->ux_pipe->up_dev; 1234 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 1235 int epnumber = otgpipe->hw_ep->ep_number; 1236 1237 UWRITE1(sc, MUSB2_REG_RXFADDR(epnumber), dev->ud_addr); 1238 if (dev->ud_myhsport) { 1239 UWRITE1(sc, MUSB2_REG_RXHADDR(epnumber), 1240 dev->ud_myhsport->up_parent->ud_addr); 1241 UWRITE1(sc, MUSB2_REG_RXHUBPORT(epnumber), 1242 dev->ud_myhsport->up_portno); 1243 } else { 1244 UWRITE1(sc, MUSB2_REG_RXHADDR(epnumber), 0); 1245 UWRITE1(sc, MUSB2_REG_RXHUBPORT(epnumber), 0); 1246 } 1247 UWRITE1(sc, MUSB2_REG_RXTI, 1248 motg_speed(dev->ud_speed) | 1249 UE_GET_ADDR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) | 1250 motg_type(UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes)) 1251 ); 1252 if (epnumber == 0) { 1253 if (sc->sc_high_speed) { 1254 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, 1255 NAK_TO_CTRL_HIGH); 1256 } else { 1257 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, NAK_TO_CTRL); 1258 } 1259 } else { 1260 if ((xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE) 1261 == UE_BULK) { 1262 if (sc->sc_high_speed) { 1263 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, 1264 NAK_TO_BULK_HIGH); 1265 } else { 1266 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, NAK_TO_BULK); 1267 } 1268 } else { 1269 if (sc->sc_high_speed) { 1270 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, POLL_TO_HIGH); 1271 } else { 1272 UWRITE1(sc, MUSB2_REG_RXNAKLIMIT, POLL_TO); 1273 } 1274 } 1275 } 1276 } 1277 1278 static usbd_status 1279 motg_device_ctrl_transfer(struct usbd_xfer *xfer) 1280 { 1281 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1282 usbd_status err; 1283 1284 /* Insert last in queue. */ 1285 mutex_enter(&sc->sc_lock); 1286 err = usb_insert_transfer(xfer); 1287 KASSERT(xfer->ux_status == USBD_NOT_STARTED); 1288 mutex_exit(&sc->sc_lock); 1289 if (err) 1290 return err; 1291 1292 /* 1293 * Pipe isn't running (otherwise err would be USBD_INPROG), 1294 * so start it first. 1295 */ 1296 return motg_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)); 1297 } 1298 1299 static usbd_status 1300 motg_device_ctrl_start(struct usbd_xfer *xfer) 1301 { 1302 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1303 usbd_status err; 1304 mutex_enter(&sc->sc_lock); 1305 err = motg_device_ctrl_start1(sc); 1306 mutex_exit(&sc->sc_lock); 1307 return err; 1308 } 1309 1310 static usbd_status 1311 motg_device_ctrl_start1(struct motg_softc *sc) 1312 { 1313 struct motg_hw_ep *ep = &sc->sc_in_ep[0]; 1314 struct usbd_xfer *xfer = NULL; 1315 struct motg_pipe *otgpipe; 1316 usbd_status err = 0; 1317 1318 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1319 1320 KASSERT(mutex_owned(&sc->sc_lock)); 1321 if (sc->sc_dying) 1322 return USBD_IOERROR; 1323 1324 if (!sc->sc_connected) 1325 return USBD_IOERROR; 1326 1327 if (ep->xfer != NULL) { 1328 err = USBD_IN_PROGRESS; 1329 goto end; 1330 } 1331 /* locate the first pipe with work to do */ 1332 SIMPLEQ_FOREACH(otgpipe, &ep->ep_pipes, ep_pipe_list) { 1333 xfer = SIMPLEQ_FIRST(&otgpipe->pipe.up_queue); 1334 DPRINTFN(MD_CTRL, "pipe %#jx xfer %#jx status %jd", 1335 (uintptr_t)otgpipe, (uintptr_t)xfer, 1336 (xfer != NULL) ? xfer->ux_status : 0, 0); 1337 1338 if (xfer != NULL) { 1339 /* move this pipe to the end of the list */ 1340 SIMPLEQ_REMOVE(&ep->ep_pipes, otgpipe, 1341 motg_pipe, ep_pipe_list); 1342 SIMPLEQ_INSERT_TAIL(&ep->ep_pipes, 1343 otgpipe, ep_pipe_list); 1344 break; 1345 } 1346 } 1347 if (xfer == NULL) { 1348 err = USBD_NOT_STARTED; 1349 goto end; 1350 } 1351 if (xfer->ux_status == USBD_NOT_STARTED) { 1352 usbd_xfer_schedule_timeout(xfer); 1353 xfer->ux_status = USBD_IN_PROGRESS; 1354 } else { 1355 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 1356 } 1357 KASSERT(otgpipe == MOTG_PIPE2MPIPE(xfer->ux_pipe)); 1358 KASSERT(otgpipe->hw_ep == ep); 1359 KASSERT(xfer->ux_rqflags & URQ_REQUEST); 1360 // KASSERT(xfer->ux_actlen == 0); 1361 xfer->ux_actlen = 0; 1362 1363 ep->xfer = xfer; 1364 ep->datalen = xfer->ux_length; 1365 if (ep->datalen > 0) 1366 ep->data = xfer->ux_buf; 1367 else 1368 ep->data = NULL; 1369 if ((xfer->ux_flags & USBD_FORCE_SHORT_XFER) && 1370 (ep->datalen % 64) == 0) 1371 ep->need_short_xfer = 1; 1372 else 1373 ep->need_short_xfer = 0; 1374 /* now we need send this request */ 1375 DPRINTFN(MD_CTRL, 1376 "xfer %#jx send data %#jx len %jd short %jd", 1377 (uintptr_t)xfer, (uintptr_t)ep->data, ep->datalen, 1378 ep->need_short_xfer); 1379 DPRINTFN(MD_CTRL, 1380 "xfer %#jx ... speed %jd to %jd", (uintptr_t)xfer, 1381 xfer->ux_pipe->up_dev->ud_speed, 1382 xfer->ux_pipe->up_dev->ud_addr, 0); 1383 KASSERT(ep->phase == IDLE); 1384 ep->phase = SETUP; 1385 /* select endpoint 0 */ 1386 UWRITE1(sc, MUSB2_REG_EPINDEX, 0); 1387 /* fifo should be empty at this point */ 1388 KASSERT((UREAD1(sc, MUSB2_REG_TXCSRL) & MUSB2_MASK_CSR0L_TXPKTRDY) == 0); 1389 /* send data */ 1390 // KASSERT(((vaddr_t)(&xfer->ux_request) & 3) == 0); 1391 KASSERT(sizeof(xfer->ux_request) == 8); 1392 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_EPFIFO(0), 1393 (void *)&xfer->ux_request, sizeof(xfer->ux_request)); 1394 1395 motg_setup_endpoint_tx(xfer); 1396 /* start transaction */ 1397 UWRITE1(sc, MUSB2_REG_TXCSRL, 1398 MUSB2_MASK_CSR0L_TXPKTRDY | MUSB2_MASK_CSR0L_SETUPPKT); 1399 1400 end: 1401 if (err) 1402 return err; 1403 1404 return USBD_IN_PROGRESS; 1405 } 1406 1407 static void 1408 motg_device_ctrl_read(struct usbd_xfer *xfer) 1409 { 1410 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1411 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 1412 /* assume endpoint already selected */ 1413 motg_setup_endpoint_rx(xfer); 1414 /* start transaction */ 1415 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSR0L_REQPKT); 1416 otgpipe->hw_ep->phase = DATA_IN; 1417 } 1418 1419 static void 1420 motg_device_ctrl_intr_rx(struct motg_softc *sc) 1421 { 1422 struct motg_hw_ep *ep = &sc->sc_in_ep[0]; 1423 struct usbd_xfer *xfer = ep->xfer; 1424 uint8_t csr; 1425 int datalen, max_datalen; 1426 char *data; 1427 bool got_short; 1428 usbd_status new_status = USBD_IN_PROGRESS; 1429 1430 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1431 1432 KASSERT(mutex_owned(&sc->sc_lock)); 1433 KASSERT(ep->phase == DATA_IN || ep->phase == STATUS_IN); 1434 1435 /* select endpoint 0 */ 1436 UWRITE1(sc, MUSB2_REG_EPINDEX, 0); 1437 1438 /* read out FIFO status */ 1439 csr = UREAD1(sc, MUSB2_REG_TXCSRL); 1440 DPRINTFN(MD_CTRL, "phase %jd csr %#jx xfer %#jx status %jd", 1441 ep->phase, csr, (uintptr_t)xfer, 1442 (xfer != NULL) ? xfer->ux_status : 0); 1443 1444 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) { 1445 csr &= ~MUSB2_MASK_CSR0L_REQPKT; 1446 UWRITE1(sc, MUSB2_REG_TXCSRL, csr); 1447 1448 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO; 1449 UWRITE1(sc, MUSB2_REG_TXCSRL, csr); 1450 new_status = USBD_TIMEOUT; /* XXX */ 1451 goto complete; 1452 } 1453 if (csr & (MUSB2_MASK_CSR0L_RXSTALL | MUSB2_MASK_CSR0L_ERROR)) { 1454 if (csr & MUSB2_MASK_CSR0L_RXSTALL) 1455 new_status = USBD_STALLED; 1456 else 1457 new_status = USBD_IOERROR; 1458 /* clear status */ 1459 UWRITE1(sc, MUSB2_REG_TXCSRL, 0); 1460 goto complete; 1461 } 1462 if ((csr & MUSB2_MASK_CSR0L_RXPKTRDY) == 0) 1463 return; /* no data yet */ 1464 1465 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS) 1466 goto complete; 1467 1468 if (ep->phase == STATUS_IN) { 1469 new_status = USBD_NORMAL_COMPLETION; 1470 UWRITE1(sc, MUSB2_REG_TXCSRL, 0); 1471 goto complete; 1472 } 1473 datalen = UREAD2(sc, MUSB2_REG_RXCOUNT); 1474 DPRINTFN(MD_CTRL, "phase %jd datalen %jd", ep->phase, datalen, 0, 0); 1475 KASSERT(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize) > 0); 1476 max_datalen = uimin(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize), 1477 ep->datalen); 1478 if (datalen > max_datalen) { 1479 new_status = USBD_IOERROR; 1480 UWRITE1(sc, MUSB2_REG_TXCSRL, 0); 1481 goto complete; 1482 } 1483 got_short = (datalen < max_datalen); 1484 if (datalen > 0) { 1485 KASSERT(ep->phase == DATA_IN); 1486 data = ep->data; 1487 ep->data += datalen; 1488 ep->datalen -= datalen; 1489 xfer->ux_actlen += datalen; 1490 if (((vaddr_t)data & 0x3) == 0 && 1491 (datalen >> 2) > 0) { 1492 DPRINTFN(MD_CTRL, "r4 data %#jx len %jd", 1493 (uintptr_t)data, datalen, 0, 0); 1494 bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, 1495 MUSB2_REG_EPFIFO(0), (void *)data, datalen >> 2); 1496 data += (datalen & ~0x3); 1497 datalen -= (datalen & ~0x3); 1498 } 1499 DPRINTFN(MD_CTRL, "r1 data %#jx len %jd", (uintptr_t)data, 1500 datalen, 0, 0); 1501 if (datalen) { 1502 bus_space_read_multi_1(sc->sc_iot, sc->sc_ioh, 1503 MUSB2_REG_EPFIFO(0), data, datalen); 1504 } 1505 } 1506 UWRITE1(sc, MUSB2_REG_TXCSRL, csr & ~MUSB2_MASK_CSR0L_RXPKTRDY); 1507 KASSERT(ep->phase == DATA_IN); 1508 if (got_short || (ep->datalen == 0)) { 1509 if (ep->need_short_xfer == 0) { 1510 ep->phase = STATUS_OUT; 1511 UWRITE1(sc, MUSB2_REG_TXCSRH, 1512 UREAD1(sc, MUSB2_REG_TXCSRH) | 1513 MUSB2_MASK_CSR0H_PING_DIS); 1514 motg_setup_endpoint_tx(xfer); 1515 UWRITE1(sc, MUSB2_REG_TXCSRL, 1516 MUSB2_MASK_CSR0L_STATUSPKT | 1517 MUSB2_MASK_CSR0L_TXPKTRDY); 1518 return; 1519 } 1520 ep->need_short_xfer = 0; 1521 } 1522 motg_device_ctrl_read(xfer); 1523 return; 1524 complete: 1525 ep->phase = IDLE; 1526 ep->xfer = NULL; 1527 /* 1528 * Try to claim this xfer for completion. If it has already 1529 * completed or aborted, drop it on the floor. 1530 */ 1531 if (xfer && usbd_xfer_trycomplete(xfer)) { 1532 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 1533 KASSERT(new_status != USBD_IN_PROGRESS); 1534 xfer->ux_status = new_status; 1535 usb_transfer_complete(xfer); 1536 } 1537 motg_device_ctrl_start1(sc); 1538 } 1539 1540 static void 1541 motg_device_ctrl_intr_tx(struct motg_softc *sc) 1542 { 1543 struct motg_hw_ep *ep = &sc->sc_in_ep[0]; 1544 struct usbd_xfer *xfer = ep->xfer; 1545 uint8_t csr; 1546 int datalen; 1547 char *data; 1548 usbd_status new_status = USBD_IN_PROGRESS; 1549 1550 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1551 1552 KASSERT(mutex_owned(&sc->sc_lock)); 1553 1554 if (ep->phase == DATA_IN || ep->phase == STATUS_IN) { 1555 motg_device_ctrl_intr_rx(sc); 1556 return; 1557 } 1558 1559 KASSERT(ep->phase == SETUP || ep->phase == DATA_OUT || 1560 ep->phase == STATUS_OUT); 1561 1562 /* select endpoint 0 */ 1563 UWRITE1(sc, MUSB2_REG_EPINDEX, 0); 1564 1565 csr = UREAD1(sc, MUSB2_REG_TXCSRL); 1566 DPRINTFN(MD_CTRL, "phase %jd csr %#jx xfer %#jx status %jd", 1567 ep->phase, csr, (uintptr_t)xfer, 1568 (xfer != NULL) ? xfer->ux_status : 0); 1569 1570 if (csr & MUSB2_MASK_CSR0L_RXSTALL) { 1571 /* command not accepted */ 1572 new_status = USBD_STALLED; 1573 /* clear status */ 1574 UWRITE1(sc, MUSB2_REG_TXCSRL, 0); 1575 goto complete; 1576 } 1577 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) { 1578 new_status = USBD_TIMEOUT; /* XXX */ 1579 /* flush fifo */ 1580 while (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) { 1581 UWRITE1(sc, MUSB2_REG_TXCSRH, 1582 UREAD1(sc, MUSB2_REG_TXCSRH) | 1583 MUSB2_MASK_CSR0H_FFLUSH); 1584 csr = UREAD1(sc, MUSB2_REG_TXCSRL); 1585 } 1586 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO; 1587 UWRITE1(sc, MUSB2_REG_TXCSRL, csr); 1588 goto complete; 1589 } 1590 if (csr & MUSB2_MASK_CSR0L_ERROR) { 1591 new_status = USBD_IOERROR; 1592 /* clear status */ 1593 UWRITE1(sc, MUSB2_REG_TXCSRL, 0); 1594 goto complete; 1595 } 1596 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) { 1597 /* data still not sent */ 1598 return; 1599 } 1600 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS) 1601 goto complete; 1602 if (ep->phase == STATUS_OUT) { 1603 /* 1604 * we have sent status and got no error; 1605 * declare transfer complete 1606 */ 1607 DPRINTFN(MD_CTRL, "xfer %#jx status %jd complete", 1608 (uintptr_t)xfer, xfer->ux_status, 0, 0); 1609 new_status = USBD_NORMAL_COMPLETION; 1610 goto complete; 1611 } 1612 if (ep->datalen == 0) { 1613 if (ep->need_short_xfer) { 1614 ep->need_short_xfer = 0; 1615 /* one more data phase */ 1616 if (xfer->ux_request.bmRequestType & UT_READ) { 1617 DPRINTFN(MD_CTRL, "xfer %#jx to DATA_IN", 1618 (uintptr_t)xfer, 0, 0, 0); 1619 motg_device_ctrl_read(xfer); 1620 return; 1621 } /* else fall back to DATA_OUT */ 1622 } else { 1623 DPRINTFN(MD_CTRL, "xfer %#jx to STATUS_IN, csrh %#jx", 1624 (uintptr_t)xfer, UREAD1(sc, MUSB2_REG_TXCSRH), 1625 0, 0); 1626 ep->phase = STATUS_IN; 1627 UWRITE1(sc, MUSB2_REG_RXCSRH, 1628 UREAD1(sc, MUSB2_REG_RXCSRH) | 1629 MUSB2_MASK_CSR0H_PING_DIS); 1630 motg_setup_endpoint_rx(xfer); 1631 UWRITE1(sc, MUSB2_REG_TXCSRL, 1632 MUSB2_MASK_CSR0L_STATUSPKT | 1633 MUSB2_MASK_CSR0L_REQPKT); 1634 return; 1635 } 1636 } 1637 if (xfer->ux_request.bmRequestType & UT_READ) { 1638 motg_device_ctrl_read(xfer); 1639 return; 1640 } 1641 /* setup a dataout phase */ 1642 datalen = uimin(ep->datalen, 1643 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)); 1644 ep->phase = DATA_OUT; 1645 DPRINTFN(MD_CTRL, "xfer %#jx to DATA_OUT, csrh %#jx", (uintptr_t)xfer, 1646 UREAD1(sc, MUSB2_REG_TXCSRH), 0, 0); 1647 if (datalen) { 1648 data = ep->data; 1649 ep->data += datalen; 1650 ep->datalen -= datalen; 1651 xfer->ux_actlen += datalen; 1652 if (((vaddr_t)data & 0x3) == 0 && 1653 (datalen >> 2) > 0) { 1654 bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh, 1655 MUSB2_REG_EPFIFO(0), (void *)data, datalen >> 2); 1656 data += (datalen & ~0x3); 1657 datalen -= (datalen & ~0x3); 1658 } 1659 if (datalen) { 1660 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh, 1661 MUSB2_REG_EPFIFO(0), data, datalen); 1662 } 1663 } 1664 /* send data */ 1665 motg_setup_endpoint_tx(xfer); 1666 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSR0L_TXPKTRDY); 1667 return; 1668 1669 complete: 1670 ep->phase = IDLE; 1671 ep->xfer = NULL; 1672 /* 1673 * Try to claim this xfer for completion. If it has already 1674 * completed or aborted, drop it on the floor. 1675 */ 1676 if (xfer && usbd_xfer_trycomplete(xfer)) { 1677 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 1678 KASSERT(new_status != USBD_IN_PROGRESS); 1679 xfer->ux_status = new_status; 1680 usb_transfer_complete(xfer); 1681 } 1682 motg_device_ctrl_start1(sc); 1683 } 1684 1685 /* Abort a device control request. */ 1686 void 1687 motg_device_ctrl_abort(struct usbd_xfer *xfer) 1688 { 1689 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1690 1691 usbd_xfer_abort(xfer); 1692 } 1693 1694 /* Close a device control pipe */ 1695 void 1696 motg_device_ctrl_close(struct usbd_pipe *pipe) 1697 { 1698 struct motg_softc *sc __diagused = MOTG_PIPE2SC(pipe); 1699 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe); 1700 struct motg_pipe *otgpipeiter; 1701 1702 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1703 1704 KASSERT(mutex_owned(&sc->sc_lock)); 1705 KASSERT(otgpipe->hw_ep->xfer == NULL || 1706 otgpipe->hw_ep->xfer->ux_pipe != pipe); 1707 1708 SIMPLEQ_FOREACH(otgpipeiter, &otgpipe->hw_ep->ep_pipes, ep_pipe_list) { 1709 if (otgpipeiter == otgpipe) { 1710 /* remove from list */ 1711 SIMPLEQ_REMOVE(&otgpipe->hw_ep->ep_pipes, otgpipe, 1712 motg_pipe, ep_pipe_list); 1713 otgpipe->hw_ep->refcount--; 1714 /* we're done */ 1715 return; 1716 } 1717 } 1718 panic("motg_device_ctrl_close: not found"); 1719 } 1720 1721 void 1722 motg_device_ctrl_done(struct usbd_xfer *xfer) 1723 { 1724 struct motg_pipe *otgpipe __diagused = MOTG_PIPE2MPIPE(xfer->ux_pipe); 1725 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1726 1727 KASSERT(otgpipe->hw_ep->xfer != xfer); 1728 } 1729 1730 static usbd_status 1731 motg_device_data_transfer(struct usbd_xfer *xfer) 1732 { 1733 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1734 usbd_status err; 1735 1736 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1737 1738 /* Insert last in queue. */ 1739 mutex_enter(&sc->sc_lock); 1740 DPRINTF("xfer %#jx status %jd", (uintptr_t)xfer, xfer->ux_status, 0, 0); 1741 err = usb_insert_transfer(xfer); 1742 KASSERT(xfer->ux_status == USBD_NOT_STARTED); 1743 mutex_exit(&sc->sc_lock); 1744 if (err) 1745 return err; 1746 1747 /* 1748 * Pipe isn't running (otherwise err would be USBD_INPROG), 1749 * so start it first. 1750 */ 1751 return motg_device_data_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue)); 1752 } 1753 1754 static usbd_status 1755 motg_device_data_start(struct usbd_xfer *xfer) 1756 { 1757 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1758 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 1759 usbd_status err; 1760 1761 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1762 1763 mutex_enter(&sc->sc_lock); 1764 DPRINTF("xfer %#jx status %jd", (uintptr_t)xfer, xfer->ux_status, 0, 0); 1765 err = motg_device_data_start1(sc, otgpipe->hw_ep); 1766 mutex_exit(&sc->sc_lock); 1767 return err; 1768 } 1769 1770 static usbd_status 1771 motg_device_data_start1(struct motg_softc *sc, struct motg_hw_ep *ep) 1772 { 1773 struct usbd_xfer *xfer = NULL; 1774 struct motg_pipe *otgpipe; 1775 usbd_status err = 0; 1776 uint32_t val __diagused; 1777 1778 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1779 1780 KASSERT(mutex_owned(&sc->sc_lock)); 1781 if (sc->sc_dying) 1782 return USBD_IOERROR; 1783 1784 if (!sc->sc_connected) 1785 return USBD_IOERROR; 1786 1787 if (ep->xfer != NULL) { 1788 err = USBD_IN_PROGRESS; 1789 goto end; 1790 } 1791 /* locate the first pipe with work to do */ 1792 SIMPLEQ_FOREACH(otgpipe, &ep->ep_pipes, ep_pipe_list) { 1793 xfer = SIMPLEQ_FIRST(&otgpipe->pipe.up_queue); 1794 DPRINTFN(MD_BULK, "pipe %#jx xfer %#jx status %jd", 1795 (uintptr_t)otgpipe, (uintptr_t)xfer, 1796 (xfer != NULL) ? xfer->ux_status : 0, 0); 1797 if (xfer != NULL) { 1798 /* move this pipe to the end of the list */ 1799 SIMPLEQ_REMOVE(&ep->ep_pipes, otgpipe, 1800 motg_pipe, ep_pipe_list); 1801 SIMPLEQ_INSERT_TAIL(&ep->ep_pipes, 1802 otgpipe, ep_pipe_list); 1803 break; 1804 } 1805 } 1806 if (xfer == NULL) { 1807 err = USBD_NOT_STARTED; 1808 goto end; 1809 } 1810 if (xfer->ux_status == USBD_NOT_STARTED) { 1811 usbd_xfer_schedule_timeout(xfer); 1812 xfer->ux_status = USBD_IN_PROGRESS; 1813 } else { 1814 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 1815 } 1816 KASSERT(otgpipe == MOTG_PIPE2MPIPE(xfer->ux_pipe)); 1817 KASSERT(otgpipe->hw_ep == ep); 1818 KASSERT(!(xfer->ux_rqflags & URQ_REQUEST)); 1819 // KASSERT(xfer->ux_actlen == 0); 1820 xfer->ux_actlen = 0; 1821 1822 ep->xfer = xfer; 1823 ep->datalen = xfer->ux_length; 1824 KASSERT(ep->datalen > 0); 1825 ep->data = xfer->ux_buf; 1826 if ((xfer->ux_flags & USBD_FORCE_SHORT_XFER) && 1827 (ep->datalen % 64) == 0) 1828 ep->need_short_xfer = 1; 1829 else 1830 ep->need_short_xfer = 0; 1831 /* now we need send this request */ 1832 DPRINTFN(MD_BULK, 1833 UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) == UE_DIR_IN ? 1834 "xfer %#jx in data %#jx len %jd short %jd" : 1835 "xfer %#jx out data %#jx len %jd short %jd", 1836 (uintptr_t)xfer, (uintptr_t)ep->data, ep->datalen, 1837 ep->need_short_xfer); 1838 DPRINTFN(MD_BULK, "... speed %jd to %jd", 1839 xfer->ux_pipe->up_dev->ud_speed, 1840 xfer->ux_pipe->up_dev->ud_addr, 0, 0); 1841 KASSERT(ep->phase == IDLE); 1842 /* select endpoint */ 1843 UWRITE1(sc, MUSB2_REG_EPINDEX, ep->ep_number); 1844 if (UE_GET_DIR(xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress) 1845 == UE_DIR_IN) { 1846 val = UREAD1(sc, MUSB2_REG_RXCSRL); 1847 KASSERT((val & MUSB2_MASK_CSRL_RXPKTRDY) == 0); 1848 motg_device_data_read(xfer); 1849 } else { 1850 ep->phase = DATA_OUT; 1851 val = UREAD1(sc, MUSB2_REG_TXCSRL); 1852 KASSERT((val & MUSB2_MASK_CSRL_TXPKTRDY) == 0); 1853 motg_device_data_write(xfer); 1854 } 1855 end: 1856 if (err) 1857 return err; 1858 1859 return USBD_IN_PROGRESS; 1860 } 1861 1862 static void 1863 motg_device_data_read(struct usbd_xfer *xfer) 1864 { 1865 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1866 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 1867 uint32_t val; 1868 1869 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1870 1871 KASSERT(mutex_owned(&sc->sc_lock)); 1872 /* assume endpoint already selected */ 1873 motg_setup_endpoint_rx(xfer); 1874 /* Max packet size */ 1875 UWRITE2(sc, MUSB2_REG_RXMAXP, 1876 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)); 1877 /* Data Toggle */ 1878 val = UREAD1(sc, MUSB2_REG_RXCSRH); 1879 val |= MUSB2_MASK_CSRH_RXDT_WREN; 1880 if (otgpipe->nexttoggle) 1881 val |= MUSB2_MASK_CSRH_RXDT_VAL; 1882 else 1883 val &= ~MUSB2_MASK_CSRH_RXDT_VAL; 1884 UWRITE1(sc, MUSB2_REG_RXCSRH, val); 1885 1886 DPRINTFN(MD_BULK, "%#jx to DATA_IN on ep %jd, csrh %#jx", 1887 (uintptr_t)xfer, otgpipe->hw_ep->ep_number, 1888 UREAD1(sc, MUSB2_REG_RXCSRH), 0); 1889 /* start transaction */ 1890 UWRITE1(sc, MUSB2_REG_RXCSRL, MUSB2_MASK_CSRL_RXREQPKT); 1891 otgpipe->hw_ep->phase = DATA_IN; 1892 } 1893 1894 static void 1895 motg_device_data_write(struct usbd_xfer *xfer) 1896 { 1897 struct motg_softc *sc = MOTG_XFER2SC(xfer); 1898 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 1899 struct motg_hw_ep *ep = otgpipe->hw_ep; 1900 int datalen; 1901 char *data; 1902 uint32_t val; 1903 1904 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1905 1906 KASSERT(xfer!=NULL); 1907 KASSERT(mutex_owned(&sc->sc_lock)); 1908 1909 datalen = uimin(ep->datalen, 1910 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)); 1911 ep->phase = DATA_OUT; 1912 DPRINTFN(MD_BULK, "%#jx to DATA_OUT on ep %jd, len %jd csrh %#jx", 1913 (uintptr_t)xfer, ep->ep_number, datalen, 1914 UREAD1(sc, MUSB2_REG_TXCSRH)); 1915 1916 /* assume endpoint already selected */ 1917 /* write data to fifo */ 1918 data = ep->data; 1919 ep->data += datalen; 1920 ep->datalen -= datalen; 1921 xfer->ux_actlen += datalen; 1922 if (((vaddr_t)data & 0x3) == 0 && 1923 (datalen >> 2) > 0) { 1924 bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh, 1925 MUSB2_REG_EPFIFO(ep->ep_number), 1926 (void *)data, datalen >> 2); 1927 data += (datalen & ~0x3); 1928 datalen -= (datalen & ~0x3); 1929 } 1930 if (datalen) { 1931 bus_space_write_multi_1(sc->sc_iot, sc->sc_ioh, 1932 MUSB2_REG_EPFIFO(ep->ep_number), data, datalen); 1933 } 1934 1935 motg_setup_endpoint_tx(xfer); 1936 /* Max packet size */ 1937 UWRITE2(sc, MUSB2_REG_TXMAXP, 1938 UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)); 1939 /* Data Toggle */ 1940 val = UREAD1(sc, MUSB2_REG_TXCSRH); 1941 val |= MUSB2_MASK_CSRH_TXDT_WREN; 1942 if (otgpipe->nexttoggle) 1943 val |= MUSB2_MASK_CSRH_TXDT_VAL; 1944 else 1945 val &= ~MUSB2_MASK_CSRH_TXDT_VAL; 1946 UWRITE1(sc, MUSB2_REG_TXCSRH, val); 1947 1948 /* start transaction */ 1949 UWRITE1(sc, MUSB2_REG_TXCSRL, MUSB2_MASK_CSRL_TXPKTRDY); 1950 } 1951 1952 static void 1953 motg_device_intr_rx(struct motg_softc *sc, int epnumber) 1954 { 1955 struct motg_hw_ep *ep = &sc->sc_in_ep[epnumber]; 1956 struct usbd_xfer *xfer = ep->xfer; 1957 uint8_t csr; 1958 int datalen, max_datalen; 1959 char *data; 1960 bool got_short; 1961 usbd_status new_status = USBD_IN_PROGRESS; 1962 1963 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 1964 1965 KASSERT(mutex_owned(&sc->sc_lock)); 1966 KASSERT(ep->ep_number == epnumber); 1967 1968 DPRINTFN(MD_BULK, "on ep %jd", epnumber, 0, 0, 0); 1969 /* select endpoint */ 1970 UWRITE1(sc, MUSB2_REG_EPINDEX, epnumber); 1971 1972 /* read out FIFO status */ 1973 csr = UREAD1(sc, MUSB2_REG_RXCSRL); 1974 DPRINTFN(MD_BULK, "phase %jd csr %#jx", ep->phase, csr ,0 ,0); 1975 1976 if ((csr & (MUSB2_MASK_CSRL_RXNAKTO | MUSB2_MASK_CSRL_RXSTALL | 1977 MUSB2_MASK_CSRL_RXERROR | MUSB2_MASK_CSRL_RXPKTRDY)) == 0) 1978 return; 1979 1980 KASSERTMSG(ep->phase == DATA_IN, "phase %d", ep->phase); 1981 if (csr & MUSB2_MASK_CSRL_RXNAKTO) { 1982 csr &= ~MUSB2_MASK_CSRL_RXREQPKT; 1983 UWRITE1(sc, MUSB2_REG_RXCSRL, csr); 1984 1985 csr &= ~MUSB2_MASK_CSRL_RXNAKTO; 1986 UWRITE1(sc, MUSB2_REG_RXCSRL, csr); 1987 new_status = USBD_TIMEOUT; /* XXX */ 1988 goto complete; 1989 } 1990 if (csr & (MUSB2_MASK_CSRL_RXSTALL | MUSB2_MASK_CSRL_RXERROR)) { 1991 if (csr & MUSB2_MASK_CSRL_RXSTALL) 1992 new_status = USBD_STALLED; 1993 else 1994 new_status = USBD_IOERROR; 1995 /* clear status */ 1996 UWRITE1(sc, MUSB2_REG_RXCSRL, 0); 1997 goto complete; 1998 } 1999 KASSERT(csr & MUSB2_MASK_CSRL_RXPKTRDY); 2000 2001 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS) { 2002 UWRITE1(sc, MUSB2_REG_RXCSRL, 0); 2003 goto complete; 2004 } 2005 2006 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 2007 otgpipe->nexttoggle = otgpipe->nexttoggle ^ 1; 2008 2009 datalen = UREAD2(sc, MUSB2_REG_RXCOUNT); 2010 DPRINTFN(MD_BULK, "phase %jd datalen %jd", ep->phase, datalen ,0 ,0); 2011 KASSERT(UE_GET_SIZE(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)) > 0); 2012 max_datalen = uimin( 2013 UE_GET_SIZE(UGETW(xfer->ux_pipe->up_endpoint->ue_edesc->wMaxPacketSize)), 2014 ep->datalen); 2015 if (datalen > max_datalen) { 2016 new_status = USBD_IOERROR; 2017 UWRITE1(sc, MUSB2_REG_RXCSRL, 0); 2018 goto complete; 2019 } 2020 got_short = (datalen < max_datalen); 2021 if (datalen > 0) { 2022 KASSERT(ep->phase == DATA_IN); 2023 data = ep->data; 2024 ep->data += datalen; 2025 ep->datalen -= datalen; 2026 xfer->ux_actlen += datalen; 2027 if (((vaddr_t)data & 0x3) == 0 && 2028 (datalen >> 2) > 0) { 2029 DPRINTFN(MD_BULK, "r4 data %#jx len %jd", 2030 (uintptr_t)data, datalen, 0, 0); 2031 bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, 2032 MUSB2_REG_EPFIFO(ep->ep_number), 2033 (void *)data, datalen >> 2); 2034 data += (datalen & ~0x3); 2035 datalen -= (datalen & ~0x3); 2036 } 2037 DPRINTFN(MD_BULK, "r1 data %#jx len %jd", (uintptr_t)data, 2038 datalen ,0 ,0); 2039 if (datalen) { 2040 bus_space_read_multi_1(sc->sc_iot, sc->sc_ioh, 2041 MUSB2_REG_EPFIFO(ep->ep_number), data, datalen); 2042 } 2043 } 2044 UWRITE1(sc, MUSB2_REG_RXCSRL, 0); 2045 KASSERT(ep->phase == DATA_IN); 2046 if (got_short || (ep->datalen == 0)) { 2047 if (ep->need_short_xfer == 0) { 2048 new_status = USBD_NORMAL_COMPLETION; 2049 goto complete; 2050 } 2051 ep->need_short_xfer = 0; 2052 } 2053 motg_device_data_read(xfer); 2054 return; 2055 complete: 2056 DPRINTFN(MD_BULK, "xfer %#jx complete, status %jd", (uintptr_t)xfer, 2057 (xfer != NULL) ? xfer->ux_status : 0, 0, 0); 2058 ep->phase = IDLE; 2059 ep->xfer = NULL; 2060 /* 2061 * Try to claim this xfer for completion. If it has already 2062 * completed or aborted, drop it on the floor. 2063 */ 2064 if (xfer && usbd_xfer_trycomplete(xfer)) { 2065 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 2066 KASSERT(new_status != USBD_IN_PROGRESS); 2067 xfer->ux_status = new_status; 2068 usb_transfer_complete(xfer); 2069 } 2070 motg_device_data_start1(sc, ep); 2071 } 2072 2073 static void 2074 motg_device_intr_tx(struct motg_softc *sc, int epnumber) 2075 { 2076 struct motg_hw_ep *ep = &sc->sc_out_ep[epnumber]; 2077 struct usbd_xfer *xfer = ep->xfer; 2078 uint8_t csr; 2079 struct motg_pipe *otgpipe; 2080 usbd_status new_status = USBD_IN_PROGRESS; 2081 2082 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 2083 2084 KASSERT(mutex_owned(&sc->sc_lock)); 2085 KASSERT(ep->ep_number == epnumber); 2086 2087 DPRINTFN(MD_BULK, " on ep %jd", epnumber, 0, 0, 0); 2088 /* select endpoint */ 2089 UWRITE1(sc, MUSB2_REG_EPINDEX, epnumber); 2090 2091 csr = UREAD1(sc, MUSB2_REG_TXCSRL); 2092 DPRINTFN(MD_BULK, "phase %jd csr %#jx", ep->phase, csr, 0, 0); 2093 2094 if (csr & (MUSB2_MASK_CSRL_TXSTALLED|MUSB2_MASK_CSRL_TXERROR)) { 2095 /* command not accepted */ 2096 if (csr & MUSB2_MASK_CSRL_TXSTALLED) 2097 new_status = USBD_STALLED; 2098 else 2099 new_status = USBD_IOERROR; 2100 /* clear status */ 2101 UWRITE1(sc, MUSB2_REG_TXCSRL, 0); 2102 goto complete; 2103 } 2104 if (csr & MUSB2_MASK_CSRL_TXNAKTO) { 2105 new_status = USBD_TIMEOUT; /* XXX */ 2106 csr &= ~MUSB2_MASK_CSRL_TXNAKTO; 2107 UWRITE1(sc, MUSB2_REG_TXCSRL, csr); 2108 /* flush fifo */ 2109 while (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) { 2110 csr |= MUSB2_MASK_CSRL_TXFFLUSH; 2111 csr &= ~MUSB2_MASK_CSRL_TXNAKTO; 2112 UWRITE1(sc, MUSB2_REG_TXCSRL, csr); 2113 delay(1000); 2114 csr = UREAD1(sc, MUSB2_REG_TXCSRL); 2115 DPRINTFN(MD_BULK, "TX fifo flush ep %jd CSR %#jx", 2116 epnumber, csr, 0, 0); 2117 } 2118 goto complete; 2119 } 2120 if (csr & (MUSB2_MASK_CSRL_TXFIFONEMPTY|MUSB2_MASK_CSRL_TXPKTRDY)) { 2121 /* data still not sent */ 2122 return; 2123 } 2124 if (xfer == NULL || xfer->ux_status != USBD_IN_PROGRESS) 2125 goto complete; 2126 KASSERT(ep->phase == DATA_OUT); 2127 2128 otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 2129 otgpipe->nexttoggle = otgpipe->nexttoggle ^ 1; 2130 2131 if (ep->datalen == 0) { 2132 if (ep->need_short_xfer) { 2133 ep->need_short_xfer = 0; 2134 /* one more data phase */ 2135 } else { 2136 new_status = USBD_NORMAL_COMPLETION; 2137 goto complete; 2138 } 2139 } 2140 motg_device_data_write(xfer); 2141 return; 2142 2143 complete: 2144 DPRINTFN(MD_BULK, "xfer %#jx complete, status %jd", (uintptr_t)xfer, 2145 (xfer != NULL) ? xfer->ux_status : 0, 0, 0); 2146 ep->phase = IDLE; 2147 ep->xfer = NULL; 2148 /* 2149 * Try to claim this xfer for completion. If it has already 2150 * completed or aborted, drop it on the floor. 2151 */ 2152 if (xfer && usbd_xfer_trycomplete(xfer)) { 2153 KASSERT(xfer->ux_status == USBD_IN_PROGRESS); 2154 KASSERT(new_status != USBD_IN_PROGRESS); 2155 xfer->ux_status = new_status; 2156 usb_transfer_complete(xfer); 2157 } 2158 motg_device_data_start1(sc, ep); 2159 } 2160 2161 /* Abort a device control request. */ 2162 void 2163 motg_device_data_abort(struct usbd_xfer *xfer) 2164 { 2165 struct motg_softc __diagused *sc = MOTG_XFER2SC(xfer); 2166 KASSERT(mutex_owned(&sc->sc_lock)); 2167 2168 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 2169 2170 usbd_xfer_abort(xfer); 2171 } 2172 2173 /* Close a device control pipe */ 2174 void 2175 motg_device_data_close(struct usbd_pipe *pipe) 2176 { 2177 struct motg_softc *sc __diagused = MOTG_PIPE2SC(pipe); 2178 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe); 2179 struct motg_pipe *otgpipeiter; 2180 2181 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 2182 2183 KASSERT(mutex_owned(&sc->sc_lock)); 2184 KASSERT(otgpipe->hw_ep->xfer == NULL || 2185 otgpipe->hw_ep->xfer->ux_pipe != pipe); 2186 2187 pipe->up_endpoint->ue_toggle = otgpipe->nexttoggle; 2188 SIMPLEQ_FOREACH(otgpipeiter, &otgpipe->hw_ep->ep_pipes, ep_pipe_list) { 2189 if (otgpipeiter == otgpipe) { 2190 /* remove from list */ 2191 SIMPLEQ_REMOVE(&otgpipe->hw_ep->ep_pipes, otgpipe, 2192 motg_pipe, ep_pipe_list); 2193 otgpipe->hw_ep->refcount--; 2194 /* we're done */ 2195 return; 2196 } 2197 } 2198 panic("motg_device_data_close: not found"); 2199 } 2200 2201 void 2202 motg_device_data_done(struct usbd_xfer *xfer) 2203 { 2204 struct motg_pipe *otgpipe __diagused = MOTG_PIPE2MPIPE(xfer->ux_pipe); 2205 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 2206 2207 KASSERT(otgpipe->hw_ep->xfer != xfer); 2208 } 2209 2210 void 2211 motg_device_clear_toggle(struct usbd_pipe *pipe) 2212 { 2213 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(pipe); 2214 otgpipe->nexttoggle = 0; 2215 } 2216 2217 /* Abort a device control request. */ 2218 static void 2219 motg_abortx(struct usbd_xfer *xfer) 2220 { 2221 MOTGHIST_FUNC(); MOTGHIST_CALLED(); 2222 uint8_t csr; 2223 struct motg_softc *sc = MOTG_XFER2SC(xfer); 2224 struct motg_pipe *otgpipe = MOTG_PIPE2MPIPE(xfer->ux_pipe); 2225 2226 KASSERT(mutex_owned(&sc->sc_lock)); 2227 ASSERT_SLEEPABLE(); 2228 2229 /* 2230 * If we're dying, skip the hardware action and just notify the 2231 * software that we're done. 2232 */ 2233 if (sc->sc_dying) { 2234 goto dying; 2235 } 2236 2237 if (otgpipe->hw_ep->xfer == xfer) { 2238 otgpipe->hw_ep->xfer = NULL; 2239 if (otgpipe->hw_ep->ep_number > 0) { 2240 /* select endpoint */ 2241 UWRITE1(sc, MUSB2_REG_EPINDEX, 2242 otgpipe->hw_ep->ep_number); 2243 if (otgpipe->hw_ep->phase == DATA_OUT) { 2244 csr = UREAD1(sc, MUSB2_REG_TXCSRL); 2245 while (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) { 2246 csr |= MUSB2_MASK_CSRL_TXFFLUSH; 2247 UWRITE1(sc, MUSB2_REG_TXCSRL, csr); 2248 csr = UREAD1(sc, MUSB2_REG_TXCSRL); 2249 } 2250 UWRITE1(sc, MUSB2_REG_TXCSRL, 0); 2251 } else if (otgpipe->hw_ep->phase == DATA_IN) { 2252 csr = UREAD1(sc, MUSB2_REG_RXCSRL); 2253 while (csr & MUSB2_MASK_CSRL_RXPKTRDY) { 2254 csr |= MUSB2_MASK_CSRL_RXFFLUSH; 2255 UWRITE1(sc, MUSB2_REG_RXCSRL, csr); 2256 csr = UREAD1(sc, MUSB2_REG_RXCSRL); 2257 } 2258 UWRITE1(sc, MUSB2_REG_RXCSRL, 0); 2259 } 2260 otgpipe->hw_ep->phase = IDLE; 2261 } 2262 } 2263 dying: 2264 usb_transfer_complete(xfer); 2265 KASSERT(mutex_owned(&sc->sc_lock)); 2266 } 2267