1 /* $NetBSD: joy_pci.c,v 1.17 2008/04/28 20:23:55 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 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.17 2008/04/28 20:23:55 martin 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 static int bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg); 49 50 static int 51 joy_pci_match(device_t parent, cfdata_t match, void *aux) 52 { 53 struct pci_attach_args *pa = aux; 54 55 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_INPUT && 56 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_INPUT_GAMEPORT && 57 PCI_INTERFACE(pa->pa_class) == 0x10) 58 return 1; 59 60 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CREATIVELABS && 61 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY || 62 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY2)) 63 return 1; 64 65 return 0; 66 } 67 68 /* check if this BAR assigns/requests IO space */ 69 static int 70 bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg) 71 { 72 pcireg_t address, mask; 73 int s; 74 75 /* 76 * The splhigh() is not realy necessary at autoconfig time, 77 * but maybe useful if used in lkm context later. 78 */ 79 s = splhigh(); 80 address = pci_conf_read(pc, tag, reg); 81 pci_conf_write(pc, tag, reg, 0xffffffff); 82 mask = pci_conf_read(pc, tag, reg); 83 pci_conf_write(pc, tag, reg, address); 84 splx(s); 85 86 return (PCI_MAPREG_TYPE(address) == PCI_MAPREG_TYPE_IO && PCI_MAPREG_IO_SIZE(mask) > 0); 87 } 88 89 static void 90 joy_pci_attach(device_t parent, device_t self, void *aux) 91 { 92 struct joy_softc *sc = device_private(self); 93 struct pci_attach_args *pa = aux; 94 char devinfo[256]; 95 bus_size_t mapsize; 96 int reg; 97 98 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 99 aprint_normal(": %s (rev 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class)); 100 101 for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END; 102 reg += sizeof(pcireg_t)) 103 if (bar_is_io(pa->pa_pc, pa->pa_tag, reg)) 104 break; 105 if (reg >= PCI_MAPREG_END) { 106 aprint_error_dev(self, 107 "violates PCI spec, no IO region found\n"); 108 return; 109 } 110 111 if (pci_mapreg_map(pa, reg, PCI_MAPREG_TYPE_IO, 0, 112 &sc->sc_iot, &sc->sc_ioh, NULL, &mapsize)) { 113 aprint_error_dev(self, "could not map IO space\n"); 114 return; 115 } 116 117 if (mapsize != 2) { 118 if (!bus_space_subregion(sc->sc_iot, sc->sc_ioh, 1, 1, &sc->sc_ioh) < 0) { 119 aprint_error_dev(self, "error mapping subregion\n"); 120 return; 121 } 122 } 123 124 sc->sc_dev = self; 125 126 joyattach(sc); 127 } 128 129 CFATTACH_DECL_NEW(joy_pci, sizeof(struct joy_softc), 130 joy_pci_match, joy_pci_attach, NULL, NULL); 131