xref: /dragonfly/sys/dev/netif/ral/if_ral_pci.c (revision f8f04fe3)
1 /*
2  * Copyright (c) 2005, 2006
3  *	Damien Bergamini <damien.bergamini@free.fr>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  *
17  * $FreeBSD: src/sys/dev/ral/if_ral_pci.c,v 1.4 2006/03/05 23:27:51 silby Exp $
18  * $DragonFly: src/sys/dev/netif/ral/if_ral_pci.c,v 1.5 2007/04/02 14:25:38 sephe Exp $
19  */
20 
21 /*
22  * PCI/Cardbus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
23  */
24 
25 #include <sys/param.h>
26 #include <sys/sysctl.h>
27 #include <sys/sockio.h>
28 #include <sys/mbuf.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/rman.h>
36 #include <sys/endian.h>
37 
38 #include <net/bpf.h>
39 #include <net/if.h>
40 #include <net/if_arp.h>
41 #include <net/ethernet.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_types.h>
45 
46 #include <netproto/802_11/ieee80211_var.h>
47 #include <netproto/802_11/ieee80211_radiotap.h>
48 
49 #include <bus/pci/pcidevs.h>
50 #include <bus/pci/pcireg.h>
51 #include <bus/pci/pcivar.h>
52 
53 #include <dev/netif/ral/rt2560var.h>
54 #include <dev/netif/ral/rt2661var.h>
55 
56 struct ral_opns {
57 	int	(*attach)(device_t, int);
58 	int	(*detach)(void *);
59 	void	(*shutdown)(void *);
60 	void	(*suspend)(void *);
61 	void	(*resume)(void *);
62 };
63 
64 static const struct ral_opns ral_rt2560_opns = {
65 	.attach		= rt2560_attach,
66 	.detach		= rt2560_detach,
67 	.shutdown	= rt2560_shutdown,
68 	.suspend	= rt2560_suspend,
69 	.resume		= rt2560_resume,
70 };
71 
72 static const struct ral_opns ral_rt2661_opns = {
73 	.attach		= rt2661_attach,
74 	.detach		= rt2661_detach,
75 	.shutdown	= rt2661_shutdown,
76 	.suspend	= rt2661_suspend,
77 	.resume		= rt2661_resume,
78 };
79 
80 static const struct ral_pci_ident {
81 	uint16_t		vendor;
82 	uint16_t		device;
83 	const char		*name;
84 	const struct ral_opns	*opns;
85 } ral_pci_ids[] = {
86 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560,
87 		"Ralink Technology RT2560", &ral_rt2560_opns },
88 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S,
89 		"Ralink Technology RT2561S", &ral_rt2661_opns },
90 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561,
91 		"Ralink Technology RT2561", &ral_rt2661_opns },
92 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661,
93 		"Ralink Technology RT2661", &ral_rt2661_opns },
94 	{ 0, 0, NULL, NULL }
95 };
96 
97 struct ral_pci_softc {
98 	/* XXX MUST be the first field */
99 	union {
100 		struct rt2560_softc sc_rt2560;
101 		struct rt2661_softc sc_rt2661;
102 	} u;
103 
104 	const struct ral_opns	*sc_opns;
105 	int			mem_rid;
106 	struct resource		*mem;
107 };
108 
109 static int ral_pci_probe(device_t);
110 static int ral_pci_attach(device_t);
111 static int ral_pci_detach(device_t);
112 static int ral_pci_shutdown(device_t);
113 static int ral_pci_suspend(device_t);
114 static int ral_pci_resume(device_t);
115 
116 static device_method_t ral_pci_methods[] = {
117 	/* Device interface */
118 	DEVMETHOD(device_probe,		ral_pci_probe),
119 	DEVMETHOD(device_attach,	ral_pci_attach),
120 	DEVMETHOD(device_detach,	ral_pci_detach),
121 	DEVMETHOD(device_shutdown,	ral_pci_shutdown),
122 	DEVMETHOD(device_suspend,	ral_pci_suspend),
123 	DEVMETHOD(device_resume,	ral_pci_resume),
124 
125 	{ 0, 0 }
126 };
127 
128 static driver_t ral_pci_driver = {
129 	"ral",
130 	ral_pci_methods,
131 	sizeof (struct ral_pci_softc)
132 };
133 
134 static devclass_t ral_devclass;
135 
136 DRIVER_MODULE(ral, pci, ral_pci_driver, ral_devclass, 0, 0);
137 DRIVER_MODULE(ral, cardbus, ral_pci_driver, ral_devclass, 0, 0);
138 
139 MODULE_DEPEND(ral, wlan, 1, 1, 1);
140 MODULE_DEPEND(ral, wlan_ratectl_onoe, 1, 1, 1);
141 MODULE_DEPEND(ral, wlan_ratectl_sample, 1, 1, 1);
142 MODULE_DEPEND(ral, pci, 1, 1, 1);
143 MODULE_DEPEND(ral, cardbus, 1, 1, 1);
144 
145 static int
146 ral_pci_probe(device_t dev)
147 {
148 	const struct ral_pci_ident *ident;
149 	uint16_t vid, did;
150 
151 	vid = pci_get_vendor(dev);
152 	did = pci_get_device(dev);
153 	for (ident = ral_pci_ids; ident->name != NULL; ident++) {
154 		if (vid == ident->vendor && did == ident->device) {
155 			struct ral_pci_softc *psc = device_get_softc(dev);
156 
157 			psc->sc_opns = ident->opns;
158 			device_set_desc(dev, ident->name);
159 			return 0;
160 		}
161 	}
162 	return ENXIO;
163 }
164 
165 /* Base Address Register */
166 #define RAL_PCI_BAR0	0x10
167 
168 static int
169 ral_pci_attach(device_t dev)
170 {
171 	struct ral_pci_softc *psc = device_get_softc(dev);
172 	struct rt2560_softc *sc = &psc->u.sc_rt2560;
173 	int error;
174 
175 	/* Assign `dev' earlier, so that we can do possible error clean up */
176 	sc->sc_dev = dev;
177 
178 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
179 		device_printf(dev, "chip is in D%d power mode "
180 		    "-- setting to D0\n", pci_get_powerstate(dev));
181 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
182 	}
183 
184 	/* enable bus-mastering */
185 	pci_enable_busmaster(dev);
186 
187 	psc->mem_rid = RAL_PCI_BAR0;
188 	psc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &psc->mem_rid,
189 					  RF_ACTIVE);
190 	if (psc->mem == NULL) {
191 		device_printf(dev, "could not allocate memory resource\n");
192 		return ENXIO;
193 	}
194 	sc->sc_st = rman_get_bustag(psc->mem);
195 	sc->sc_sh = rman_get_bushandle(psc->mem);
196 
197 	error = psc->sc_opns->attach(dev, pci_get_device(dev));
198 	if (error != 0)
199 		ral_pci_detach(dev);
200 
201 	return error;
202 }
203 
204 static int
205 ral_pci_detach(device_t dev)
206 {
207 	struct ral_pci_softc *psc = device_get_softc(dev);
208 
209 	if (device_is_attached(dev))
210 		psc->sc_opns->detach(psc);
211 
212 	bus_generic_detach(dev);
213 
214 	if (psc->mem != NULL) {
215 		bus_release_resource(dev, SYS_RES_MEMORY, psc->mem_rid,
216 				     psc->mem);
217 	}
218 	return 0;
219 }
220 
221 static int
222 ral_pci_shutdown(device_t dev)
223 {
224 	struct ral_pci_softc *psc = device_get_softc(dev);
225 
226 	psc->sc_opns->shutdown(psc);
227 	return 0;
228 }
229 
230 static int
231 ral_pci_suspend(device_t dev)
232 {
233 	struct ral_pci_softc *psc = device_get_softc(dev);
234 
235 	psc->sc_opns->suspend(psc);
236 	return 0;
237 }
238 
239 static int
240 ral_pci_resume(device_t dev)
241 {
242 	struct ral_pci_softc *psc = device_get_softc(dev);
243 
244 	psc->sc_opns->resume(psc);
245 	return 0;
246 }
247