1 /* $NetBSD: joy_pci.c,v 1.21 2014/05/07 19:30:09 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 2000, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Martin Husemann <martin@NetBSD.org>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: joy_pci.c,v 1.21 2014/05/07 19:30:09 joerg Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/device.h> 39 40 #include <sys/bus.h> 41 42 #include <dev/pci/pcireg.h> 43 #include <dev/pci/pcivar.h> 44 #include <dev/pci/pcidevs.h> 45 46 #include <dev/ic/joyvar.h> 47 48 struct joy_pci_softc { 49 struct joy_softc sc_joy; 50 kmutex_t sc_lock; 51 }; 52 53 static int bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg); 54 55 static int 56 joy_pci_match(device_t parent, cfdata_t match, void *aux) 57 { 58 struct pci_attach_args *pa = aux; 59 60 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_INPUT && 61 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_INPUT_GAMEPORT && 62 PCI_INTERFACE(pa->pa_class) == 0x10) 63 return 1; 64 65 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CREATIVELABS && 66 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY || 67 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY2)) 68 return 1; 69 70 return 0; 71 } 72 73 /* check if this BAR assigns/requests IO space */ 74 static int 75 bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg) 76 { 77 pcireg_t address, mask; 78 79 address = pci_conf_read(pc, tag, reg); 80 pci_conf_write(pc, tag, reg, 0xffffffff); 81 mask = pci_conf_read(pc, tag, reg); 82 pci_conf_write(pc, tag, reg, address); 83 84 return (PCI_MAPREG_TYPE(address) == PCI_MAPREG_TYPE_IO && 85 PCI_MAPREG_IO_SIZE(mask) > 0); 86 } 87 88 static void 89 joy_pci_attach(device_t parent, device_t self, void *aux) 90 { 91 struct joy_pci_softc *psc = device_private(self); 92 struct joy_softc *sc = &psc->sc_joy; 93 struct pci_attach_args *pa = aux; 94 bus_size_t mapsize; 95 int reg; 96 97 pci_aprint_devinfo(pa, NULL); 98 99 for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; 100 reg += sizeof(pcireg_t)) 101 if (bar_is_io(pa->pa_pc, pa->pa_tag, reg)) 102 break; 103 if (reg >= PCI_MAPREG_END) { 104 aprint_error_dev(self, 105 "violates PCI spec, no IO region found\n"); 106 return; 107 } 108 109 if (pci_mapreg_map(pa, reg, PCI_MAPREG_TYPE_IO, 0, 110 &sc->sc_iot, &sc->sc_ioh, NULL, &mapsize)) { 111 aprint_error_dev(self, "could not map IO space\n"); 112 return; 113 } 114 115 if (mapsize != 2) { 116 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 1, 1, 117 &sc->sc_ioh)) { 118 aprint_error_dev(self, "error mapping subregion\n"); 119 return; 120 } 121 } 122 123 mutex_init(&psc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 124 sc->sc_dev = self; 125 sc->sc_lock = &psc->sc_lock; 126 127 joyattach(sc); 128 } 129 130 CFATTACH_DECL_NEW(joy_pci, sizeof(struct joy_pci_softc), 131 joy_pci_match, joy_pci_attach, NULL, NULL); 132