xref: /netbsd-src/sys/dev/pci/if_an_pci.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: if_an_pci.c,v 1.24 2007/12/09 20:28:08 jmcneill 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 Atsushi Onoe.
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 /*
40  * PCI bus front-end for the Aironet PC4500/PC4800 Wireless LAN Adapter.
41  * Unlike WaveLAN, this adapter attached as PCI device using a PLX 9050
42  * PCI to "dumb bus" bridge chip.
43  */
44 
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: if_an_pci.c,v 1.24 2007/12/09 20:28:08 jmcneill Exp $");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <sys/errno.h>
56 #include <sys/device.h>
57 #include <sys/callout.h>
58 
59 #include <machine/endian.h>
60 
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64 #include <net/if_ether.h>
65 
66 #include <net80211/ieee80211_netbsd.h>
67 #include <net80211/ieee80211_var.h>
68 
69 #include <sys/bus.h>
70 #include <sys/intr.h>
71 
72 #include <dev/ic/anreg.h>
73 #include <dev/ic/anvar.h>
74 
75 #include <dev/pci/pcivar.h>
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcidevs.h>
78 
79 #define	AN_PCI_PLX_IOBA		0x14	/* i/o base for PLX chip */
80 #define	AN_PCI_IOBA		0x18	/* i/o base */
81 
82 struct an_pci_softc {
83 	struct an_softc sc_an;		/* real "an" softc */
84 	pci_chipset_tag_t sc_pct;
85 	pcitag_t sc_pcitag;
86 
87 	/* PCI-specific goo. */
88 	void	*sc_ih;			/* interrupt handle */
89 };
90 
91 static int	an_pci_match(struct device *, struct cfdata *, void *);
92 static void	an_pci_attach(struct device *, struct device *, void *);
93 
94 CFATTACH_DECL(an_pci, sizeof(struct an_pci_softc),
95     an_pci_match, an_pci_attach, NULL, NULL);
96 
97 static const struct an_pci_product {
98 	u_int32_t	app_vendor;	/* PCI vendor ID */
99 	u_int32_t	app_product;	/* PCI product ID */
100 } an_pci_products[] = {
101 	{ PCI_VENDOR_AIRONET,		PCI_PRODUCT_AIRONET_PC4xxx },
102 	{ PCI_VENDOR_AIRONET,		PCI_PRODUCT_AIRONET_PC4500 },
103 	{ PCI_VENDOR_AIRONET,		PCI_PRODUCT_AIRONET_PC4800 },
104 	{ PCI_VENDOR_AIRONET,		PCI_PRODUCT_AIRONET_PCI350 },
105 	{ 0,				0			   }
106 };
107 
108 static int
109 an_pci_match(struct device *parent, struct cfdata *match,
110     void *aux)
111 {
112 	struct pci_attach_args *pa = aux;
113 	const struct an_pci_product *app;
114 
115 	for (app = an_pci_products; app->app_vendor != 0; app++) {
116 		if (PCI_VENDOR(pa->pa_id) == app->app_vendor &&
117 		    PCI_PRODUCT(pa->pa_id) == app->app_product)
118 			return 1;
119 	}
120 	return 0;
121 }
122 
123 static void
124 an_pci_attach(struct device *parent, struct device *self, void *aux)
125 {
126         struct pci_attach_args *pa = (struct pci_attach_args *)aux;
127 	struct an_pci_softc *psc = (struct an_pci_softc *) self;
128 	struct an_softc *sc = &psc->sc_an;
129         char devinfo[256];
130 	char const *intrstr;
131 	pci_intr_handle_t ih;
132 	bus_size_t iosize;
133 	u_int32_t csr;
134 
135 	psc->sc_pct = pa->pa_pc;
136 	psc->sc_pcitag = pa->pa_tag;
137 
138 	aprint_naive(": 802.11 controller\n");
139 
140         pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
141         aprint_normal(": %s\n", devinfo);
142 
143         /* Map I/O registers */
144         if (pci_mapreg_map(pa, AN_PCI_IOBA, PCI_MAPREG_TYPE_IO, 0,
145 	    &sc->sc_iot, &sc->sc_ioh, NULL, &iosize) != 0) {
146                 aprint_error("%s: unable to map registers\n", self->dv_xname);
147                 return;
148         }
149 
150         /* Enable the device. */
151         csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
152         pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
153                        csr | PCI_COMMAND_MASTER_ENABLE);
154 
155 	/* Map and establish the interrupt. */
156 	if (pci_intr_map(pa, &ih)) {
157         	aprint_error("%s: unable to map interrupt\n", self->dv_xname);
158 		return;
159 	}
160 	intrstr = pci_intr_string(pa->pa_pc, ih);
161 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, an_intr, sc);
162 	if (psc->sc_ih == NULL) {
163 		aprint_error("%s: unable to establish interrupt",
164 		    self->dv_xname);
165 		if (intrstr != NULL)
166 			aprint_normal(" at %s", intrstr);
167 		aprint_normal("\n");
168 		return;
169 	}
170 	aprint_normal("%s: interrupting at %s\n", self->dv_xname, intrstr);
171 	sc->sc_enabled = 1;
172 
173 	if (an_attach(sc) != 0) {
174 		aprint_error("%s: failed to attach controller\n",
175 		    self->dv_xname);
176 		pci_intr_disestablish(pa->pa_pc, psc->sc_ih);
177 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
178 	}
179 
180 	if (!pmf_device_register(self, NULL, NULL))
181 		aprint_error_dev(self, "couldn't establish power handler\n");
182 	else
183 		pmf_class_network_register(self, &sc->sc_if);
184 }
185