1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1998 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 (augustss@carlstedt.se) at 9 * Carlstedt Research & Technology. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* Universal Host Controller Interface 37 * 38 * UHCI spec: http://www.intel.com/ 39 */ 40 41 /* The low level controller code for UHCI has been split into 42 * PCI probes and UHCI specific code. This was done to facilitate the 43 * sharing of code between *BSD's 44 */ 45 46 #include <sys/stdint.h> 47 #include <sys/stddef.h> 48 #include <sys/param.h> 49 #include <sys/queue.h> 50 #include <sys/types.h> 51 #include <sys/systm.h> 52 #include <sys/kernel.h> 53 #include <sys/bus.h> 54 #include <sys/module.h> 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <sys/condvar.h> 58 #include <sys/sysctl.h> 59 #include <sys/sx.h> 60 #include <sys/unistd.h> 61 #include <sys/callout.h> 62 #include <sys/malloc.h> 63 #include <sys/priv.h> 64 65 #include <dev/usb/usb.h> 66 #include <dev/usb/usbdi.h> 67 68 #include <dev/usb/usb_core.h> 69 #include <dev/usb/usb_busdma.h> 70 #include <dev/usb/usb_process.h> 71 #include <dev/usb/usb_util.h> 72 #include <dev/usb/usb_debug.h> 73 74 #include <dev/usb/usb_controller.h> 75 #include <dev/usb/usb_bus.h> 76 #include <dev/usb/usb_pci.h> 77 #include <dev/usb/controller/uhci.h> 78 #include <dev/usb/controller/uhcireg.h> 79 #include "usb_if.h" 80 81 #define PCI_UHCI_VENDORID_INTEL 0x8086 82 #define PCI_UHCI_VENDORID_HP 0x103c 83 #define PCI_UHCI_VENDORID_VIA 0x1106 84 #define PCI_UHCI_VENDORID_VMWARE 0x15ad 85 86 /* PIIX4E has no separate stepping */ 87 88 static device_probe_t uhci_pci_probe; 89 static device_attach_t uhci_pci_attach; 90 static device_detach_t uhci_pci_detach; 91 static usb_take_controller_t uhci_pci_take_controller; 92 93 static int 94 uhci_pci_take_controller(device_t self) 95 { 96 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2); 97 98 return (0); 99 } 100 101 static const char * 102 uhci_pci_match(device_t self) 103 { 104 uint32_t device_id = pci_get_devid(self); 105 106 switch (device_id) { 107 case 0x26888086: 108 return ("Intel 631XESB/632XESB/3100 USB controller USB-1"); 109 110 case 0x26898086: 111 return ("Intel 631XESB/632XESB/3100 USB controller USB-2"); 112 113 case 0x268a8086: 114 return ("Intel 631XESB/632XESB/3100 USB controller USB-3"); 115 116 case 0x268b8086: 117 return ("Intel 631XESB/632XESB/3100 USB controller USB-4"); 118 119 case 0x70208086: 120 return ("Intel 82371SB (PIIX3) USB controller"); 121 122 case 0x71128086: 123 return ("Intel 82371AB/EB (PIIX4) USB controller"); 124 125 case 0x24128086: 126 return ("Intel 82801AA (ICH) USB controller"); 127 128 case 0x24228086: 129 return ("Intel 82801AB (ICH0) USB controller"); 130 131 case 0x24428086: 132 return ("Intel 82801BA/BAM (ICH2) USB controller USB-A"); 133 134 case 0x24448086: 135 return ("Intel 82801BA/BAM (ICH2) USB controller USB-B"); 136 137 case 0x24828086: 138 return ("Intel 82801CA/CAM (ICH3) USB controller USB-A"); 139 140 case 0x24848086: 141 return ("Intel 82801CA/CAM (ICH3) USB controller USB-B"); 142 143 case 0x24878086: 144 return ("Intel 82801CA/CAM (ICH3) USB controller USB-C"); 145 146 case 0x24c28086: 147 return ("Intel 82801DB (ICH4) USB controller USB-A"); 148 149 case 0x24c48086: 150 return ("Intel 82801DB (ICH4) USB controller USB-B"); 151 152 case 0x24c78086: 153 return ("Intel 82801DB (ICH4) USB controller USB-C"); 154 155 case 0x24d28086: 156 return ("Intel 82801EB (ICH5) USB controller USB-A"); 157 158 case 0x24d48086: 159 return ("Intel 82801EB (ICH5) USB controller USB-B"); 160 161 case 0x24d78086: 162 return ("Intel 82801EB (ICH5) USB controller USB-C"); 163 164 case 0x24de8086: 165 return ("Intel 82801EB (ICH5) USB controller USB-D"); 166 167 case 0x25a98086: 168 return ("Intel 6300ESB USB controller USB-A"); 169 170 case 0x25aa8086: 171 return ("Intel 6300ESB USB controller USB-B"); 172 173 case 0x26588086: 174 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-A"); 175 176 case 0x26598086: 177 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-B"); 178 179 case 0x265a8086: 180 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-C"); 181 182 case 0x265b8086: 183 return ("Intel 82801FB/FR/FW/FRW (ICH6) USB controller USB-D"); 184 185 case 0x27c88086: 186 return ("Intel 82801G (ICH7) USB controller USB-A"); 187 case 0x27c98086: 188 return ("Intel 82801G (ICH7) USB controller USB-B"); 189 case 0x27ca8086: 190 return ("Intel 82801G (ICH7) USB controller USB-C"); 191 case 0x27cb8086: 192 return ("Intel 82801G (ICH7) USB controller USB-D"); 193 194 case 0x28308086: 195 return ("Intel 82801H (ICH8) USB controller USB-A"); 196 case 0x28318086: 197 return ("Intel 82801H (ICH8) USB controller USB-B"); 198 case 0x28328086: 199 return ("Intel 82801H (ICH8) USB controller USB-C"); 200 case 0x28348086: 201 return ("Intel 82801H (ICH8) USB controller USB-D"); 202 case 0x28358086: 203 return ("Intel 82801H (ICH8) USB controller USB-E"); 204 case 0x29348086: 205 return ("Intel 82801I (ICH9) USB controller"); 206 case 0x29358086: 207 return ("Intel 82801I (ICH9) USB controller"); 208 case 0x29368086: 209 return ("Intel 82801I (ICH9) USB controller"); 210 case 0x29378086: 211 return ("Intel 82801I (ICH9) USB controller"); 212 case 0x29388086: 213 return ("Intel 82801I (ICH9) USB controller"); 214 case 0x29398086: 215 return ("Intel 82801I (ICH9) USB controller"); 216 case 0x3a348086: 217 return ("Intel 82801JI (ICH10) USB controller USB-A"); 218 case 0x3a358086: 219 return ("Intel 82801JI (ICH10) USB controller USB-B"); 220 case 0x3a368086: 221 return ("Intel 82801JI (ICH10) USB controller USB-C"); 222 case 0x3a378086: 223 return ("Intel 82801JI (ICH10) USB controller USB-D"); 224 case 0x3a388086: 225 return ("Intel 82801JI (ICH10) USB controller USB-E"); 226 case 0x3a398086: 227 return ("Intel 82801JI (ICH10) USB controller USB-F"); 228 229 case 0x719a8086: 230 return ("Intel 82443MX USB controller"); 231 232 case 0x76028086: 233 return ("Intel 82372FB/82468GX USB controller"); 234 235 case 0x3300103c: 236 return ("HP iLO Standard Virtual USB controller"); 237 238 case 0x30381106: 239 return ("VIA 83C572 USB controller"); 240 241 case 0x077415ad: 242 return ("VMware USB controller"); 243 default: 244 break; 245 } 246 247 if ((pci_get_class(self) == PCIC_SERIALBUS) && 248 (pci_get_subclass(self) == PCIS_SERIALBUS_USB) && 249 (pci_get_progif(self) == PCI_INTERFACE_UHCI)) { 250 return ("UHCI (generic) USB controller"); 251 } 252 return (NULL); 253 } 254 255 static int 256 uhci_pci_probe(device_t self) 257 { 258 const char *desc = uhci_pci_match(self); 259 260 if (desc) { 261 device_set_desc(self, desc); 262 return (BUS_PROBE_DEFAULT); 263 } else { 264 return (ENXIO); 265 } 266 } 267 268 static int 269 uhci_pci_attach(device_t self) 270 { 271 uhci_softc_t *sc = device_get_softc(self); 272 int rid; 273 int err; 274 275 /* initialise some bus fields */ 276 sc->sc_bus.parent = self; 277 sc->sc_bus.devices = sc->sc_devices; 278 sc->sc_bus.devices_max = UHCI_MAX_DEVICES; 279 sc->sc_bus.dma_bits = 32; 280 281 /* get all DMA memory */ 282 if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self), 283 &uhci_iterate_hw_softc)) { 284 return ENOMEM; 285 } 286 sc->sc_dev = self; 287 288 pci_enable_busmaster(self); 289 290 rid = PCI_UHCI_BASE_REG; 291 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_IOPORT, &rid, 292 RF_ACTIVE); 293 if (!sc->sc_io_res) { 294 device_printf(self, "Could not map ports\n"); 295 goto error; 296 } 297 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 298 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 299 sc->sc_io_size = rman_get_size(sc->sc_io_res); 300 301 /* disable interrupts */ 302 bus_space_write_2(sc->sc_io_tag, sc->sc_io_hdl, UHCI_INTR, 0); 303 304 rid = 0; 305 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 306 RF_SHAREABLE | RF_ACTIVE); 307 if (sc->sc_irq_res == NULL) { 308 device_printf(self, "Could not allocate irq\n"); 309 goto error; 310 } 311 sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 312 if (!sc->sc_bus.bdev) { 313 device_printf(self, "Could not add USB device\n"); 314 goto error; 315 } 316 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 317 318 /* 319 * uhci_pci_match must never return NULL if uhci_pci_probe 320 * succeeded 321 */ 322 device_set_desc(sc->sc_bus.bdev, uhci_pci_match(self)); 323 switch (pci_get_vendor(self)) { 324 case PCI_UHCI_VENDORID_INTEL: 325 sprintf(sc->sc_vendor, "Intel"); 326 break; 327 case PCI_UHCI_VENDORID_HP: 328 sprintf(sc->sc_vendor, "HP"); 329 break; 330 case PCI_UHCI_VENDORID_VIA: 331 sprintf(sc->sc_vendor, "VIA"); 332 break; 333 case PCI_UHCI_VENDORID_VMWARE: 334 sprintf(sc->sc_vendor, "VMware"); 335 break; 336 default: 337 if (bootverbose) { 338 device_printf(self, "(New UHCI DeviceId=0x%08x)\n", 339 pci_get_devid(self)); 340 } 341 sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self)); 342 } 343 344 switch (pci_read_config(self, PCI_USBREV, 1) & PCI_USB_REV_MASK) { 345 case PCI_USB_REV_PRE_1_0: 346 sc->sc_bus.usbrev = USB_REV_PRE_1_0; 347 break; 348 case PCI_USB_REV_1_0: 349 sc->sc_bus.usbrev = USB_REV_1_0; 350 break; 351 default: 352 /* Quirk for Parallels Desktop 4.0 */ 353 device_printf(self, "USB revision is unknown. Assuming v1.1.\n"); 354 sc->sc_bus.usbrev = USB_REV_1_1; 355 break; 356 } 357 358 #if (__FreeBSD_version >= 700031) 359 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 360 NULL, (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl); 361 #else 362 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 363 (driver_intr_t *)uhci_interrupt, sc, &sc->sc_intr_hdl); 364 #endif 365 366 if (err) { 367 device_printf(self, "Could not setup irq, %d\n", err); 368 sc->sc_intr_hdl = NULL; 369 goto error; 370 } 371 /* 372 * Set the PIRQD enable bit and switch off all the others. We don't 373 * want legacy support to interfere with us XXX Does this also mean 374 * that the BIOS won't touch the keyboard anymore if it is connected 375 * to the ports of the root hub? 376 */ 377 #ifdef USB_DEBUG 378 if (pci_read_config(self, PCI_LEGSUP, 2) != PCI_LEGSUP_USBPIRQDEN) { 379 device_printf(self, "LegSup = 0x%04x\n", 380 pci_read_config(self, PCI_LEGSUP, 2)); 381 } 382 #endif 383 pci_write_config(self, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN, 2); 384 385 err = uhci_init(sc); 386 if (!err) { 387 err = device_probe_and_attach(sc->sc_bus.bdev); 388 } 389 if (err) { 390 device_printf(self, "USB init failed\n"); 391 goto error; 392 } 393 return (0); 394 395 error: 396 uhci_pci_detach(self); 397 return (ENXIO); 398 } 399 400 int 401 uhci_pci_detach(device_t self) 402 { 403 uhci_softc_t *sc = device_get_softc(self); 404 405 /* during module unload there are lots of children leftover */ 406 device_delete_children(self); 407 408 /* 409 * disable interrupts that might have been switched on in 410 * uhci_init. 411 */ 412 if (sc->sc_io_res) { 413 USB_BUS_LOCK(&sc->sc_bus); 414 415 /* stop the controller */ 416 uhci_reset(sc); 417 418 USB_BUS_UNLOCK(&sc->sc_bus); 419 } 420 pci_disable_busmaster(self); 421 422 if (sc->sc_irq_res && sc->sc_intr_hdl) { 423 int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 424 425 if (err) { 426 /* XXX or should we panic? */ 427 device_printf(self, "Could not tear down irq, %d\n", 428 err); 429 } 430 sc->sc_intr_hdl = NULL; 431 } 432 if (sc->sc_irq_res) { 433 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 434 sc->sc_irq_res = NULL; 435 } 436 if (sc->sc_io_res) { 437 bus_release_resource(self, SYS_RES_IOPORT, PCI_UHCI_BASE_REG, 438 sc->sc_io_res); 439 sc->sc_io_res = NULL; 440 } 441 usb_bus_mem_free_all(&sc->sc_bus, &uhci_iterate_hw_softc); 442 443 return (0); 444 } 445 446 static device_method_t uhci_pci_methods[] = { 447 /* Device interface */ 448 DEVMETHOD(device_probe, uhci_pci_probe), 449 DEVMETHOD(device_attach, uhci_pci_attach), 450 DEVMETHOD(device_detach, uhci_pci_detach), 451 DEVMETHOD(device_suspend, bus_generic_suspend), 452 DEVMETHOD(device_resume, bus_generic_resume), 453 DEVMETHOD(device_shutdown, bus_generic_shutdown), 454 DEVMETHOD(usb_take_controller, uhci_pci_take_controller), 455 456 DEVMETHOD_END 457 }; 458 459 static driver_t uhci_driver = { 460 .name = "uhci", 461 .methods = uhci_pci_methods, 462 .size = sizeof(struct uhci_softc), 463 }; 464 465 DRIVER_MODULE(uhci, pci, uhci_driver, 0, 0); 466 MODULE_DEPEND(uhci, usb, 1, 1, 1); 467