joy_pci.c revision 1.18 1 /* $NetBSD: joy_pci.c,v 1.18 2011/11/23 23:07:35 jmcneill 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 (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 *
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.18 2011/11/23 23:07:35 jmcneill 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_softc *sc = device_private(self);
92 struct joy_pci_softc *psc = 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,
119 &sc->sc_ioh) < 0) {
120 aprint_error_dev(self, "error mapping subregion\n");
121 return;
122 }
123 }
124
125 mutex_init(&psc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
126 sc->sc_dev = self;
127 sc->sc_lock = &psc->sc_lock;
128
129 joyattach(sc);
130 }
131
132 CFATTACH_DECL_NEW(joy_pci, sizeof(struct joy_pci_softc),
133 joy_pci_match, joy_pci_attach, NULL, NULL);
134