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