1 /* $NetBSD: joy_pci.c,v 1.16 2008/03/26 18:27:07 xtraeme Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Martin Husemann <martin@NetBSD.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: joy_pci.c,v 1.16 2008/03/26 18:27:07 xtraeme Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcidevs.h>
46 #include <dev/ic/joyvar.h>
48 static int bar_is_io(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
);
51 joy_pci_match(device_t parent
, cfdata_t match
, void *aux
)
53 struct pci_attach_args
*pa
= aux
;
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)
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
))
68 /* check if this BAR assigns/requests IO space */
70 bar_is_io(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
)
72 pcireg_t address
, mask
;
76 * The splhigh() is not realy necessary at autoconfig time,
77 * but maybe useful if used in lkm context later.
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
);
86 return (PCI_MAPREG_TYPE(address
) == PCI_MAPREG_TYPE_IO
&& PCI_MAPREG_IO_SIZE(mask
) > 0);
90 joy_pci_attach(device_t parent
, device_t self
, void *aux
)
92 struct joy_softc
*sc
= device_private(self
);
93 struct pci_attach_args
*pa
= aux
;
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
));
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
))
105 if (reg
>= PCI_MAPREG_END
) {
106 aprint_error_dev(self
,
107 "violates PCI spec, no IO region found\n");
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");
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");
129 CFATTACH_DECL_NEW(joy_pci
, sizeof(struct joy_softc
),
130 joy_pci_match
, joy_pci_attach
, NULL
, NULL
);