Home | History | Annotate | Line # | Download | only in pci
joy_pci.c revision 1.15
      1 /*	$NetBSD: joy_pci.c,v 1.15 2007/10/19 12:00:51 ad 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 (at) 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: joy_pci.c,v 1.15 2007/10/19 12:00:51 ad Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/device.h>
     46 
     47 #include <sys/bus.h>
     48 
     49 #include <dev/pci/pcireg.h>
     50 #include <dev/pci/pcivar.h>
     51 #include <dev/pci/pcidevs.h>
     52 
     53 #include <dev/ic/joyvar.h>
     54 
     55 static int bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg);
     56 
     57 static int
     58 joy_pci_match(struct device *parent, struct cfdata *match,
     59     void *aux)
     60 {
     61 	struct pci_attach_args *pa = aux;
     62 
     63 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_INPUT &&
     64 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_INPUT_GAMEPORT &&
     65 	    PCI_INTERFACE(pa->pa_class) == 0x10)
     66 		return (1);
     67 
     68 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CREATIVELABS &&
     69 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY ||
     70 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CREATIVELABS_SBJOY2))
     71 	{
     72 		return (1);
     73 	}
     74 
     75 	return (0);
     76 }
     77 
     78 /* check if this BAR assigns/requests IO space */
     79 static int
     80 bar_is_io(pci_chipset_tag_t pc, pcitag_t tag, int reg)
     81 {
     82 	pcireg_t address, mask;
     83 	int s;
     84 
     85 	/*
     86 	 * The splhigh() is not realy necessary at autoconfig time,
     87 	 * but maybe useful if used in lkm context later.
     88 	 */
     89 	s = splhigh();
     90 	address = pci_conf_read(pc, tag, reg);
     91 	pci_conf_write(pc, tag, reg, 0xffffffff);
     92 	mask = pci_conf_read(pc, tag, reg);
     93 	pci_conf_write(pc, tag, reg, address);
     94 	splx(s);
     95 
     96 	return (PCI_MAPREG_TYPE(address) == PCI_MAPREG_TYPE_IO && PCI_MAPREG_IO_SIZE(mask) > 0);
     97 }
     98 
     99 static void
    100 joy_pci_attach(struct device *parent, struct device *self, void *aux)
    101 {
    102 	struct joy_softc *sc = (struct joy_softc *)self;
    103 	struct pci_attach_args *pa = aux;
    104 	char devinfo[256];
    105 	bus_size_t mapsize;
    106 	int reg;
    107 
    108 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    109 	printf(": %s (rev 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    110 
    111 	for (reg = PCI_MAPREG_START; reg < PCI_MAPREG_END;
    112 	     reg += sizeof(pcireg_t))
    113 		if (bar_is_io(pa->pa_pc, pa->pa_tag, reg))
    114 			break;
    115 	if (reg >= PCI_MAPREG_END) {
    116 		printf("%s: violates PCI spec, no IO region found\n",
    117 		       sc->sc_dev.dv_xname);
    118 		return;
    119 	}
    120 
    121 	if (pci_mapreg_map(pa, reg, PCI_MAPREG_TYPE_IO, 0,
    122 	    &sc->sc_iot, &sc->sc_ioh, NULL, &mapsize)) {
    123 		printf("%s: could not map IO space\n", sc->sc_dev.dv_xname);
    124 		return;
    125 	}
    126 
    127 	if (mapsize != 2) {
    128 		if (!bus_space_subregion(sc->sc_iot, sc->sc_ioh, 1, 1, &sc->sc_ioh) < 0) {
    129 			printf("%s: error mapping subregion\n", sc->sc_dev.dv_xname);
    130 			return;
    131 		}
    132 	}
    133 
    134 	joyattach(sc);
    135 }
    136 
    137 CFATTACH_DECL(joy_pci, sizeof(struct joy_softc),
    138     joy_pci_match, joy_pci_attach, NULL, NULL);
    139