1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2015 Semihalf. 5 * Copyright (c) 2015 Stormshield. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 #include <sys/stdint.h> 32 #include <sys/stddef.h> 33 #include <sys/param.h> 34 #include <sys/queue.h> 35 #include <sys/types.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/bus.h> 39 #include <sys/module.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/condvar.h> 43 #include <sys/sysctl.h> 44 #include <sys/sx.h> 45 #include <sys/unistd.h> 46 #include <sys/priv.h> 47 #include <sys/rman.h> 48 49 #include <dev/usb/usb.h> 50 #include <dev/usb/usbdi.h> 51 52 #include <dev/usb/usb_core.h> 53 #include <dev/usb/usb_busdma.h> 54 #include <dev/usb/usb_process.h> 55 #include <dev/usb/usb_util.h> 56 57 #include <dev/usb/usb_controller.h> 58 #include <dev/usb/usb_bus.h> 59 #include <dev/usb/controller/xhci.h> 60 #include <dev/usb/controller/xhcireg.h> 61 62 #include "generic_xhci.h" 63 64 #define IS_DMA_32B 1 65 66 int 67 generic_xhci_attach(device_t dev) 68 { 69 struct xhci_softc *sc = device_get_softc(dev); 70 int err = 0, rid = 0; 71 72 sc->sc_bus.parent = dev; 73 sc->sc_bus.devices = sc->sc_devices; 74 sc->sc_bus.devices_max = XHCI_MAX_DEVICES; 75 76 sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 77 RF_ACTIVE); 78 if (sc->sc_io_res == NULL) { 79 device_printf(dev, "Failed to map memory\n"); 80 generic_xhci_detach(dev); 81 return (ENXIO); 82 } 83 84 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 85 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 86 sc->sc_io_size = rman_get_size(sc->sc_io_res); 87 88 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 89 RF_SHAREABLE | RF_ACTIVE); 90 if (sc->sc_irq_res == NULL) { 91 device_printf(dev, "Failed to allocate IRQ\n"); 92 generic_xhci_detach(dev); 93 return (ENXIO); 94 } 95 96 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1); 97 if (sc->sc_bus.bdev == NULL) { 98 device_printf(dev, "Failed to add USB device\n"); 99 generic_xhci_detach(dev); 100 return (ENXIO); 101 } 102 103 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 104 105 sprintf(sc->sc_vendor, XHCI_HC_VENDOR); 106 device_set_desc(sc->sc_bus.bdev, XHCI_HC_DEVSTR); 107 108 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 109 NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl); 110 if (err != 0) { 111 device_printf(dev, "Failed to setup error IRQ, %d\n", err); 112 sc->sc_intr_hdl = NULL; 113 generic_xhci_detach(dev); 114 return (err); 115 } 116 117 err = xhci_init(sc, dev, IS_DMA_32B); 118 if (err != 0) { 119 device_printf(dev, "Failed to init XHCI, with error %d\n", err); 120 generic_xhci_detach(dev); 121 return (ENXIO); 122 } 123 124 err = xhci_start_controller(sc); 125 if (err != 0) { 126 device_printf(dev, "Failed to start XHCI controller, with error %d\n", err); 127 generic_xhci_detach(dev); 128 return (ENXIO); 129 } 130 131 err = device_probe_and_attach(sc->sc_bus.bdev); 132 if (err != 0) { 133 device_printf(dev, "Failed to initialize USB, with error %d\n", err); 134 generic_xhci_detach(dev); 135 return (ENXIO); 136 } 137 138 return (0); 139 } 140 141 int 142 generic_xhci_detach(device_t dev) 143 { 144 struct xhci_softc *sc = device_get_softc(dev); 145 int err; 146 147 /* during module unload there are lots of children leftover */ 148 device_delete_children(dev); 149 150 if (sc->sc_irq_res != NULL && sc->sc_intr_hdl != NULL) { 151 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl); 152 if (err != 0) 153 device_printf(dev, "Could not tear down irq, %d\n", 154 err); 155 sc->sc_intr_hdl = NULL; 156 } 157 158 if (sc->sc_irq_res != NULL) { 159 bus_release_resource(dev, SYS_RES_IRQ, 160 rman_get_rid(sc->sc_irq_res), sc->sc_irq_res); 161 sc->sc_irq_res = NULL; 162 } 163 164 if (sc->sc_io_res != NULL) { 165 bus_release_resource(dev, SYS_RES_MEMORY, 166 rman_get_rid(sc->sc_io_res), sc->sc_io_res); 167 sc->sc_io_res = NULL; 168 } 169 170 xhci_uninit(sc); 171 172 return (0); 173 } 174 175 static device_method_t xhci_methods[] = { 176 /* Device interface */ 177 DEVMETHOD(device_attach, generic_xhci_attach), 178 DEVMETHOD(device_detach, generic_xhci_detach), 179 DEVMETHOD(device_suspend, bus_generic_suspend), 180 DEVMETHOD(device_resume, bus_generic_resume), 181 DEVMETHOD(device_shutdown, bus_generic_shutdown), 182 183 DEVMETHOD_END 184 }; 185 186 driver_t generic_xhci_driver = { 187 "xhci", 188 xhci_methods, 189 sizeof(struct xhci_softc), 190 }; 191