xref: /netbsd-src/sys/dev/ic/rt2560.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: rt2560.c,v 1.33 2018/05/01 16:18:13 maya Exp $	*/
2 /*	$OpenBSD: rt2560.c,v 1.15 2006/04/20 20:31:12 miod Exp $  */
3 /*	$FreeBSD: rt2560.c,v 1.3 2006/03/21 21:15:43 damien Exp $*/
4 
5 /*-
6  * Copyright (c) 2005, 2006
7  *	Damien Bergamini <damien.bergamini@free.fr>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 /*-
23  * Ralink Technology RT2560 chipset driver
24  * http://www.ralinktech.com/
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: rt2560.c,v 1.33 2018/05/01 16:18:13 maya Exp $");
28 
29 
30 #include <sys/param.h>
31 #include <sys/sockio.h>
32 #include <sys/mbuf.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/callout.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 
41 #include <sys/bus.h>
42 #include <machine/endian.h>
43 #include <sys/intr.h>
44 
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/if_ether.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_rssadapt.h>
60 #include <net80211/ieee80211_radiotap.h>
61 
62 #include <dev/ic/rt2560reg.h>
63 #include <dev/ic/rt2560var.h>
64 
65 #ifdef RAL_DEBUG
66 #define DPRINTF(x)	do { if (rt2560_debug > 0) printf x; } while (0)
67 #define DPRINTFN(n, x)	do { if (rt2560_debug >= (n)) printf x; } while (0)
68 int rt2560_debug = 0;
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n, x)
72 #endif
73 
74 static int	rt2560_alloc_tx_ring(struct rt2560_softc *,
75 		    struct rt2560_tx_ring *, int);
76 static void	rt2560_reset_tx_ring(struct rt2560_softc *,
77 		    struct rt2560_tx_ring *);
78 static void	rt2560_free_tx_ring(struct rt2560_softc *,
79 		    struct rt2560_tx_ring *);
80 static int	rt2560_alloc_rx_ring(struct rt2560_softc *,
81 		    struct rt2560_rx_ring *, int);
82 static void	rt2560_reset_rx_ring(struct rt2560_softc *,
83 		    struct rt2560_rx_ring *);
84 static void	rt2560_free_rx_ring(struct rt2560_softc *,
85 		    struct rt2560_rx_ring *);
86 static struct ieee80211_node *
87 		rt2560_node_alloc(struct ieee80211_node_table *);
88 static int	rt2560_media_change(struct ifnet *);
89 static void	rt2560_next_scan(void *);
90 static void	rt2560_iter_func(void *, struct ieee80211_node *);
91 static void	rt2560_update_rssadapt(void *);
92 static int	rt2560_newstate(struct ieee80211com *, enum ieee80211_state,
93     		    int);
94 static uint16_t	rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
95 static void	rt2560_encryption_intr(struct rt2560_softc *);
96 static void	rt2560_tx_intr(struct rt2560_softc *);
97 static void	rt2560_prio_intr(struct rt2560_softc *);
98 static void	rt2560_decryption_intr(struct rt2560_softc *);
99 static void	rt2560_rx_intr(struct rt2560_softc *);
100 static void	rt2560_beacon_expire(struct rt2560_softc *);
101 static void	rt2560_wakeup_expire(struct rt2560_softc *);
102 static uint8_t	rt2560_rxrate(struct rt2560_rx_desc *);
103 static int	rt2560_ack_rate(struct ieee80211com *, int);
104 static uint16_t	rt2560_txtime(int, int, uint32_t);
105 static uint8_t	rt2560_plcp_signal(int);
106 static void	rt2560_setup_tx_desc(struct rt2560_softc *,
107 		    struct rt2560_tx_desc *, uint32_t, int, int, int,
108 		    bus_addr_t);
109 static int	rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
110 		    struct ieee80211_node *);
111 static int	rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
112 		    struct ieee80211_node *);
113 static struct mbuf *rt2560_get_rts(struct rt2560_softc *,
114 		    struct ieee80211_frame *, uint16_t);
115 static int	rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
116 		    struct ieee80211_node *);
117 static void	rt2560_start(struct ifnet *);
118 static void	rt2560_watchdog(struct ifnet *);
119 static int	rt2560_reset(struct ifnet *);
120 static int	rt2560_ioctl(struct ifnet *, u_long, void *);
121 static void	rt2560_bbp_write(struct rt2560_softc *, uint8_t, uint8_t);
122 static uint8_t	rt2560_bbp_read(struct rt2560_softc *, uint8_t);
123 static void	rt2560_rf_write(struct rt2560_softc *, uint8_t, uint32_t);
124 static void	rt2560_set_chan(struct rt2560_softc *,
125 		    struct ieee80211_channel *);
126 static void	rt2560_disable_rf_tune(struct rt2560_softc *);
127 static void	rt2560_enable_tsf_sync(struct rt2560_softc *);
128 static void	rt2560_update_plcp(struct rt2560_softc *);
129 static void	rt2560_update_slot(struct ifnet *);
130 static void	rt2560_set_basicrates(struct rt2560_softc *);
131 static void	rt2560_update_led(struct rt2560_softc *, int, int);
132 static void	rt2560_set_bssid(struct rt2560_softc *, uint8_t *);
133 static void	rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
134 static void	rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
135 static void	rt2560_update_promisc(struct rt2560_softc *);
136 static void	rt2560_set_txantenna(struct rt2560_softc *, int);
137 static void	rt2560_set_rxantenna(struct rt2560_softc *, int);
138 static const char *rt2560_get_rf(int);
139 static void	rt2560_read_eeprom(struct rt2560_softc *);
140 static int	rt2560_bbp_init(struct rt2560_softc *);
141 static int	rt2560_init(struct ifnet *);
142 static void	rt2560_stop(struct ifnet *, int);
143 static void	rt2560_softintr(void *);
144 
145 /*
146  * Default values for MAC registers; values taken from the reference driver.
147  */
148 static const struct {
149 	uint32_t	reg;
150 	uint32_t	val;
151 } rt2560_def_mac[] = {
152 	{ RT2560_PSCSR0,      0x00020002 },
153 	{ RT2560_PSCSR1,      0x00000002 },
154 	{ RT2560_PSCSR2,      0x00020002 },
155 	{ RT2560_PSCSR3,      0x00000002 },
156 	{ RT2560_TIMECSR,     0x00003f21 },
157 	{ RT2560_CSR9,        0x00000780 },
158 	{ RT2560_CSR11,       0x07041483 },
159 	{ RT2560_CNT3,        0x00000000 },
160 	{ RT2560_TXCSR1,      0x07614562 },
161 	{ RT2560_ARSP_PLCP_0, 0x8c8d8b8a },
162 	{ RT2560_ACKPCTCSR,   0x7038140a },
163 	{ RT2560_ARTCSR1,     0x1d21252d },
164 	{ RT2560_ARTCSR2,     0x1919191d },
165 	{ RT2560_RXCSR0,      0xffffffff },
166 	{ RT2560_RXCSR3,      0xb3aab3af },
167 	{ RT2560_PCICSR,      0x000003b8 },
168 	{ RT2560_PWRCSR0,     0x3f3b3100 },
169 	{ RT2560_GPIOCSR,     0x0000ff00 },
170 	{ RT2560_TESTCSR,     0x000000f0 },
171 	{ RT2560_PWRCSR1,     0x000001ff },
172 	{ RT2560_MACCSR0,     0x00213223 },
173 	{ RT2560_MACCSR1,     0x00235518 },
174 	{ RT2560_RLPWCSR,     0x00000040 },
175 	{ RT2560_RALINKCSR,   0x9a009a11 },
176 	{ RT2560_CSR7,        0xffffffff },
177 	{ RT2560_BBPCSR1,     0x82188200 },
178 	{ RT2560_TXACKCSR0,   0x00000020 },
179 	{ RT2560_SECCSR3,     0x0000e78f }
180 };
181 
182 /*
183  * Default values for BBP registers; values taken from the reference driver.
184  */
185 static const struct {
186 	uint8_t	reg;
187 	uint8_t	val;
188 } rt2560_def_bbp[] = {
189 	{  3, 0x02 },
190 	{  4, 0x19 },
191 	{ 14, 0x1c },
192 	{ 15, 0x30 },
193 	{ 16, 0xac },
194 	{ 17, 0x48 },
195 	{ 18, 0x18 },
196 	{ 19, 0xff },
197 	{ 20, 0x1e },
198 	{ 21, 0x08 },
199 	{ 22, 0x08 },
200 	{ 23, 0x08 },
201 	{ 24, 0x80 },
202 	{ 25, 0x50 },
203 	{ 26, 0x08 },
204 	{ 27, 0x23 },
205 	{ 30, 0x10 },
206 	{ 31, 0x2b },
207 	{ 32, 0xb9 },
208 	{ 34, 0x12 },
209 	{ 35, 0x50 },
210 	{ 39, 0xc4 },
211 	{ 40, 0x02 },
212 	{ 41, 0x60 },
213 	{ 53, 0x10 },
214 	{ 54, 0x18 },
215 	{ 56, 0x08 },
216 	{ 57, 0x10 },
217 	{ 58, 0x08 },
218 	{ 61, 0x60 },
219 	{ 62, 0x10 },
220 	{ 75, 0xff }
221 };
222 
223 /*
224  * Default values for RF register R2 indexed by channel numbers; values taken
225  * from the reference driver.
226  */
227 static const uint32_t rt2560_rf2522_r2[] = {
228 	0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
229 	0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
230 };
231 
232 static const uint32_t rt2560_rf2523_r2[] = {
233 	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
234 	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
235 };
236 
237 static const uint32_t rt2560_rf2524_r2[] = {
238 	0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
239 	0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
240 };
241 
242 static const uint32_t rt2560_rf2525_r2[] = {
243 	0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
244 	0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
245 };
246 
247 static const uint32_t rt2560_rf2525_hi_r2[] = {
248 	0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
249 	0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
250 };
251 
252 static const uint32_t rt2560_rf2525e_r2[] = {
253 	0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
254 	0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
255 };
256 
257 static const uint32_t rt2560_rf2526_hi_r2[] = {
258 	0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
259 	0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
260 };
261 
262 static const uint32_t rt2560_rf2526_r2[] = {
263 	0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
264 	0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
265 };
266 
267 /*
268  * For dual-band RF, RF registers R1 and R4 also depend on channel number;
269  * values taken from the reference driver.
270  */
271 static const struct {
272 	uint8_t		chan;
273 	uint32_t	r1;
274 	uint32_t	r2;
275 	uint32_t	r4;
276 } rt2560_rf5222[] = {
277 	{   1, 0x08808, 0x0044d, 0x00282 },
278 	{   2, 0x08808, 0x0044e, 0x00282 },
279 	{   3, 0x08808, 0x0044f, 0x00282 },
280 	{   4, 0x08808, 0x00460, 0x00282 },
281 	{   5, 0x08808, 0x00461, 0x00282 },
282 	{   6, 0x08808, 0x00462, 0x00282 },
283 	{   7, 0x08808, 0x00463, 0x00282 },
284 	{   8, 0x08808, 0x00464, 0x00282 },
285 	{   9, 0x08808, 0x00465, 0x00282 },
286 	{  10, 0x08808, 0x00466, 0x00282 },
287 	{  11, 0x08808, 0x00467, 0x00282 },
288 	{  12, 0x08808, 0x00468, 0x00282 },
289 	{  13, 0x08808, 0x00469, 0x00282 },
290 	{  14, 0x08808, 0x0046b, 0x00286 },
291 
292 	{  36, 0x08804, 0x06225, 0x00287 },
293 	{  40, 0x08804, 0x06226, 0x00287 },
294 	{  44, 0x08804, 0x06227, 0x00287 },
295 	{  48, 0x08804, 0x06228, 0x00287 },
296 	{  52, 0x08804, 0x06229, 0x00287 },
297 	{  56, 0x08804, 0x0622a, 0x00287 },
298 	{  60, 0x08804, 0x0622b, 0x00287 },
299 	{  64, 0x08804, 0x0622c, 0x00287 },
300 
301 	{ 100, 0x08804, 0x02200, 0x00283 },
302 	{ 104, 0x08804, 0x02201, 0x00283 },
303 	{ 108, 0x08804, 0x02202, 0x00283 },
304 	{ 112, 0x08804, 0x02203, 0x00283 },
305 	{ 116, 0x08804, 0x02204, 0x00283 },
306 	{ 120, 0x08804, 0x02205, 0x00283 },
307 	{ 124, 0x08804, 0x02206, 0x00283 },
308 	{ 128, 0x08804, 0x02207, 0x00283 },
309 	{ 132, 0x08804, 0x02208, 0x00283 },
310 	{ 136, 0x08804, 0x02209, 0x00283 },
311 	{ 140, 0x08804, 0x0220a, 0x00283 },
312 
313 	{ 149, 0x08808, 0x02429, 0x00281 },
314 	{ 153, 0x08808, 0x0242b, 0x00281 },
315 	{ 157, 0x08808, 0x0242d, 0x00281 },
316 	{ 161, 0x08808, 0x0242f, 0x00281 }
317 };
318 
319 int
320 rt2560_attach(void *xsc, int id)
321 {
322 	struct rt2560_softc *sc = xsc;
323 	struct ieee80211com *ic = &sc->sc_ic;
324 	struct ifnet *ifp = &sc->sc_if;
325 	int error, i;
326 
327 	callout_init(&sc->scan_ch, 0);
328 	callout_init(&sc->rssadapt_ch, 0);
329 
330 	/* retrieve RT2560 rev. no */
331 	sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
332 
333 	/* retrieve MAC address */
334 	rt2560_get_macaddr(sc, ic->ic_myaddr);
335 
336 	aprint_normal_dev(sc->sc_dev, "802.11 address %s\n",
337 	    ether_sprintf(ic->ic_myaddr));
338 
339 	/* retrieve RF rev. no and various other things from EEPROM */
340 	rt2560_read_eeprom(sc);
341 
342 	aprint_normal_dev(sc->sc_dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
343 	    sc->asic_rev, rt2560_get_rf(sc->rf_rev));
344 
345 	sc->sc_soft_ih = softint_establish(SOFTINT_NET, rt2560_softintr, sc);
346 	if (sc->sc_soft_ih == NULL) {
347 		aprint_error_dev(sc->sc_dev, "could not establish softint\n)");
348 		goto fail0;
349 	}
350 
351 	/*
352 	 * Allocate Tx and Rx rings.
353 	 */
354 	error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
355 	if (error != 0) {
356 		aprint_error_dev(sc->sc_dev, "could not allocate Tx ring\n)");
357 		goto fail1;
358 	}
359 
360 	error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
361 	if (error != 0) {
362 		aprint_error_dev(sc->sc_dev, "could not allocate ATIM ring\n");
363 		goto fail2;
364 	}
365 
366 	error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
367 	if (error != 0) {
368 		aprint_error_dev(sc->sc_dev, "could not allocate Prio ring\n");
369 		goto fail3;
370 	}
371 
372 	error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
373 	if (error != 0) {
374 		aprint_error_dev(sc->sc_dev, "could not allocate Beacon ring\n");
375 		goto fail4;
376 	}
377 
378 	error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
379 	if (error != 0) {
380 		aprint_error_dev(sc->sc_dev, "could not allocate Rx ring\n");
381 		goto fail5;
382 	}
383 
384 	ifp->if_softc = sc;
385 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
386 	ifp->if_init = rt2560_init;
387 	ifp->if_stop = rt2560_stop;
388 	ifp->if_ioctl = rt2560_ioctl;
389 	ifp->if_start = rt2560_start;
390 	ifp->if_watchdog = rt2560_watchdog;
391 	IFQ_SET_READY(&ifp->if_snd);
392 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
393 
394 	ic->ic_ifp = ifp;
395 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
396 	ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
397 	ic->ic_state = IEEE80211_S_INIT;
398 
399 	/* set device capabilities */
400 	ic->ic_caps =
401 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
402 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
403 	    IEEE80211_C_HOSTAP |	/* HostAp mode supported */
404 	    IEEE80211_C_TXPMGT |	/* tx power management */
405 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
406 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
407 	    IEEE80211_C_WPA;		/* 802.11i */
408 
409 	if (sc->rf_rev == RT2560_RF_5222) {
410 		/* set supported .11a rates */
411 		ic->ic_sup_rates[IEEE80211_MODE_11A] = ieee80211_std_rateset_11a;
412 
413 		/* set supported .11a channels */
414 		for (i = 36; i <= 64; i += 4) {
415 			ic->ic_channels[i].ic_freq =
416 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
417 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
418 		}
419 		for (i = 100; i <= 140; i += 4) {
420 			ic->ic_channels[i].ic_freq =
421 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
422 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
423 		}
424 		for (i = 149; i <= 161; i += 4) {
425 			ic->ic_channels[i].ic_freq =
426 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
427 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
428 		}
429 	}
430 
431 	/* set supported .11b and .11g rates */
432 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
433 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
434 
435 	/* set supported .11b and .11g channels (1 through 14) */
436 	for (i = 1; i <= 14; i++) {
437 		ic->ic_channels[i].ic_freq =
438 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
439 		ic->ic_channels[i].ic_flags =
440 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
441 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
442 	}
443 
444 	error = if_initialize(ifp);
445 	if (error != 0) {
446 		aprint_error_dev(sc->sc_dev, "if_initialize failed(%d)\n",
447 		    error);
448 		goto fail6;
449 	}
450 	ieee80211_ifattach(ic);
451 	/* Use common softint-based if_input */
452 	ifp->if_percpuq = if_percpuq_create(ifp);
453 	if_register(ifp);
454 
455 	ic->ic_node_alloc = rt2560_node_alloc;
456 	ic->ic_updateslot = rt2560_update_slot;
457 	ic->ic_reset = rt2560_reset;
458 
459 	/* override state transition machine */
460 	sc->sc_newstate = ic->ic_newstate;
461 	ic->ic_newstate = rt2560_newstate;
462 	ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status);
463 
464 	bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
465 	    sizeof(struct ieee80211_frame) + 64, &sc->sc_drvbpf);
466 
467 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
468 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
469 	sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
470 
471 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
472 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
473 	sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
474 
475 
476 	sc->dwelltime = 200;
477 
478 	ieee80211_announce(ic);
479 
480 	if (pmf_device_register(sc->sc_dev, NULL, NULL))
481 		pmf_class_network_register(sc->sc_dev, ifp);
482 	else
483 		aprint_error_dev(sc->sc_dev,
484 		    "couldn't establish power handler\n");
485 
486 	return 0;
487 
488 fail6:	rt2560_free_rx_ring(sc, &sc->rxq);
489 fail5:	rt2560_free_tx_ring(sc, &sc->bcnq);
490 fail4:	rt2560_free_tx_ring(sc, &sc->prioq);
491 fail3:	rt2560_free_tx_ring(sc, &sc->atimq);
492 fail2:	rt2560_free_tx_ring(sc, &sc->txq);
493 fail1:	softint_disestablish(sc->sc_soft_ih);
494 	sc->sc_soft_ih = NULL;
495 fail0:	return ENXIO;
496 }
497 
498 
499 int
500 rt2560_detach(void *xsc)
501 {
502 	struct rt2560_softc *sc = xsc;
503 	struct ifnet *ifp = &sc->sc_if;
504 
505 	callout_stop(&sc->scan_ch);
506 	callout_stop(&sc->rssadapt_ch);
507 
508 	pmf_device_deregister(sc->sc_dev);
509 
510 	rt2560_stop(ifp, 1);
511 
512 	ieee80211_ifdetach(&sc->sc_ic);	/* free all nodes */
513 	if_detach(ifp);
514 
515 	rt2560_free_tx_ring(sc, &sc->txq);
516 	rt2560_free_tx_ring(sc, &sc->atimq);
517 	rt2560_free_tx_ring(sc, &sc->prioq);
518 	rt2560_free_tx_ring(sc, &sc->bcnq);
519 	rt2560_free_rx_ring(sc, &sc->rxq);
520 
521 	if (sc->sc_soft_ih != NULL) {
522 		softint_disestablish(sc->sc_soft_ih);
523 		sc->sc_soft_ih = NULL;
524 	}
525 
526 	return 0;
527 }
528 
529 int
530 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
531     int count)
532 {
533 	int i, nsegs, error;
534 
535 	ring->count = count;
536 	ring->queued = 0;
537 	ring->cur = ring->next = 0;
538 	ring->cur_encrypt = ring->next_encrypt = 0;
539 
540 	error = bus_dmamap_create(sc->sc_dmat, count * RT2560_TX_DESC_SIZE, 1,
541 	    count * RT2560_TX_DESC_SIZE, 0, BUS_DMA_NOWAIT, &ring->map);
542 	if (error != 0) {
543 		aprint_error_dev(sc->sc_dev, "could not create desc DMA map\n");
544 		goto fail;
545 	}
546 
547 	error = bus_dmamem_alloc(sc->sc_dmat, count * RT2560_TX_DESC_SIZE,
548 	    PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT);
549 	if (error != 0) {
550 		aprint_error_dev(sc->sc_dev, "could not allocate DMA memory\n");
551 		goto fail;
552 	}
553 
554 	error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
555 	    count * RT2560_TX_DESC_SIZE, (void **)&ring->desc,
556 	    BUS_DMA_NOWAIT);
557 	if (error != 0) {
558 		aprint_error_dev(sc->sc_dev, "could not map desc DMA memory\n");
559 		goto fail;
560 	}
561 
562 	error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
563 	    count * RT2560_TX_DESC_SIZE, NULL, BUS_DMA_NOWAIT);
564 	if (error != 0) {
565 		aprint_error_dev(sc->sc_dev, "could not load desc DMA map\n");
566 		goto fail;
567 	}
568 
569 	memset(ring->desc, 0, count * RT2560_TX_DESC_SIZE);
570 	ring->physaddr = ring->map->dm_segs->ds_addr;
571 
572 	ring->data = malloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
573 	    M_NOWAIT);
574 	if (ring->data == NULL) {
575 		aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
576 		error = ENOMEM;
577 		goto fail;
578 	}
579 
580 	memset(ring->data, 0, count * sizeof (struct rt2560_tx_data));
581 	for (i = 0; i < count; i++) {
582 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
583 		    RT2560_MAX_SCATTER, MCLBYTES, 0, BUS_DMA_NOWAIT,
584 		    &ring->data[i].map);
585 		if (error != 0) {
586 			aprint_error_dev(sc->sc_dev, "could not create DMA map\n");
587 			goto fail;
588 		}
589 	}
590 
591 	return 0;
592 
593 fail:	rt2560_free_tx_ring(sc, ring);
594 	return error;
595 }
596 
597 void
598 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
599 {
600 	struct rt2560_tx_desc *desc;
601 	struct rt2560_tx_data *data;
602 	int i;
603 
604 	for (i = 0; i < ring->count; i++) {
605 		desc = &ring->desc[i];
606 		data = &ring->data[i];
607 
608 		if (data->m != NULL) {
609 			bus_dmamap_sync(sc->sc_dmat, data->map, 0,
610 			    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
611 			bus_dmamap_unload(sc->sc_dmat, data->map);
612 			m_freem(data->m);
613 			data->m = NULL;
614 		}
615 
616 		if (data->ni != NULL) {
617 			ieee80211_free_node(data->ni);
618 			data->ni = NULL;
619 		}
620 
621 		desc->flags = 0;
622 	}
623 
624 	bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
625 	    BUS_DMASYNC_PREWRITE);
626 
627 	ring->queued = 0;
628 	ring->cur = ring->next = 0;
629 	ring->cur_encrypt = ring->next_encrypt = 0;
630 }
631 
632 void
633 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
634 {
635 	struct rt2560_tx_data *data;
636 	int i;
637 
638 	if (ring->desc != NULL) {
639 		bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
640 		    ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
641 		bus_dmamap_unload(sc->sc_dmat, ring->map);
642 		bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
643 		    ring->count * RT2560_TX_DESC_SIZE);
644 		bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
645 	}
646 
647 	if (ring->data != NULL) {
648 		for (i = 0; i < ring->count; i++) {
649 			data = &ring->data[i];
650 
651 			if (data->m != NULL) {
652 				bus_dmamap_sync(sc->sc_dmat, data->map, 0,
653 				    data->map->dm_mapsize,
654 				    BUS_DMASYNC_POSTWRITE);
655 				bus_dmamap_unload(sc->sc_dmat, data->map);
656 				m_freem(data->m);
657 			}
658 
659 			if (data->ni != NULL)
660 				ieee80211_free_node(data->ni);
661 
662 
663 			if (data->map != NULL)
664 				bus_dmamap_destroy(sc->sc_dmat, data->map);
665 		}
666 		free(ring->data, M_DEVBUF);
667 	}
668 }
669 
670 int
671 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
672     int count)
673 {
674 	struct rt2560_rx_desc *desc;
675 	struct rt2560_rx_data *data;
676 	int i, nsegs, error;
677 
678 	ring->count = count;
679 	ring->cur = ring->next = 0;
680 	ring->cur_decrypt = 0;
681 
682 	error = bus_dmamap_create(sc->sc_dmat, count * RT2560_RX_DESC_SIZE, 1,
683 	    count * RT2560_RX_DESC_SIZE, 0, BUS_DMA_NOWAIT, &ring->map);
684 	if (error != 0) {
685 		aprint_error_dev(sc->sc_dev, "could not create desc DMA map\n");
686 		goto fail;
687 	}
688 
689 	error = bus_dmamem_alloc(sc->sc_dmat, count * RT2560_RX_DESC_SIZE,
690 	    PAGE_SIZE, 0, &ring->seg, 1, &nsegs, BUS_DMA_NOWAIT);
691 	if (error != 0) {
692 		aprint_error_dev(sc->sc_dev, "could not allocate DMA memory\n");
693 		goto fail;
694 	}
695 
696 	error = bus_dmamem_map(sc->sc_dmat, &ring->seg, nsegs,
697 	    count * RT2560_RX_DESC_SIZE, (void **)&ring->desc,
698 	    BUS_DMA_NOWAIT);
699 	if (error != 0) {
700 		aprint_error_dev(sc->sc_dev, "could not map desc DMA memory\n");
701 		goto fail;
702 	}
703 
704 	error = bus_dmamap_load(sc->sc_dmat, ring->map, ring->desc,
705 	    count * RT2560_RX_DESC_SIZE, NULL, BUS_DMA_NOWAIT);
706 	if (error != 0) {
707 		aprint_error_dev(sc->sc_dev, "could not load desc DMA map\n");
708 		goto fail;
709 	}
710 
711 	memset(ring->desc, 0, count * RT2560_RX_DESC_SIZE);
712 	ring->physaddr = ring->map->dm_segs->ds_addr;
713 
714 	ring->data = malloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
715 	    M_NOWAIT);
716 	if (ring->data == NULL) {
717 		aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
718 		error = ENOMEM;
719 		goto fail;
720 	}
721 
722 	/*
723 	 * Pre-allocate Rx buffers and populate Rx ring.
724 	 */
725 	memset(ring->data, 0, count * sizeof (struct rt2560_rx_data));
726 	for (i = 0; i < count; i++) {
727 		desc = &sc->rxq.desc[i];
728 		data = &sc->rxq.data[i];
729 
730 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
731 		    0, BUS_DMA_NOWAIT, &data->map);
732 		if (error != 0) {
733 			aprint_error_dev(sc->sc_dev, "could not create DMA map\n");
734 			goto fail;
735 		}
736 
737 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
738 		if (data->m == NULL) {
739 			aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
740 			error = ENOMEM;
741 			goto fail;
742 		}
743 
744 		MCLGET(data->m, M_DONTWAIT);
745 		if (!(data->m->m_flags & M_EXT)) {
746 			aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf cluster\n");
747 			error = ENOMEM;
748 			goto fail;
749 		}
750 
751 		error = bus_dmamap_load(sc->sc_dmat, data->map,
752 		    mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
753 		if (error != 0) {
754 			aprint_error_dev(sc->sc_dev, "could not load rx buf DMA map");
755 			goto fail;
756 		}
757 
758 		desc->flags = htole32(RT2560_RX_BUSY);
759 		desc->physaddr = htole32(data->map->dm_segs->ds_addr);
760 	}
761 
762 	bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
763 	    BUS_DMASYNC_PREWRITE);
764 
765 	return 0;
766 
767 fail:	rt2560_free_rx_ring(sc, ring);
768 	return error;
769 }
770 
771 void
772 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
773 {
774 	int i;
775 
776 	for (i = 0; i < ring->count; i++) {
777 		ring->desc[i].flags = htole32(RT2560_RX_BUSY);
778 		ring->data[i].drop = 0;
779 	}
780 
781 	bus_dmamap_sync(sc->sc_dmat, ring->map, 0, ring->map->dm_mapsize,
782 	    BUS_DMASYNC_PREWRITE);
783 
784 	ring->cur = ring->next = 0;
785 	ring->cur_decrypt = 0;
786 }
787 
788 void
789 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
790 {
791 	struct rt2560_rx_data *data;
792 	int i;
793 
794 	if (ring->desc != NULL) {
795 		bus_dmamap_sync(sc->sc_dmat, ring->map, 0,
796 		    ring->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
797 		bus_dmamap_unload(sc->sc_dmat, ring->map);
798 		bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
799 		    ring->count * RT2560_RX_DESC_SIZE);
800 		bus_dmamem_free(sc->sc_dmat, &ring->seg, 1);
801 	}
802 
803 	if (ring->data != NULL) {
804 		for (i = 0; i < ring->count; i++) {
805 			data = &ring->data[i];
806 
807 			if (data->m != NULL) {
808 				bus_dmamap_sync(sc->sc_dmat, data->map, 0,
809 				    data->map->dm_mapsize,
810 				    BUS_DMASYNC_POSTREAD);
811 				bus_dmamap_unload(sc->sc_dmat, data->map);
812 				m_freem(data->m);
813 			}
814 
815 			if (data->map != NULL)
816 				bus_dmamap_destroy(sc->sc_dmat, data->map);
817 		}
818 		free(ring->data, M_DEVBUF);
819 	}
820 }
821 
822 struct ieee80211_node *
823 rt2560_node_alloc(struct ieee80211_node_table *nt)
824 {
825 	struct rt2560_node *rn;
826 
827 	rn = malloc(sizeof (struct rt2560_node), M_80211_NODE,
828 	    M_NOWAIT | M_ZERO);
829 
830 	return (rn != NULL) ? &rn->ni : NULL;
831 }
832 
833 int
834 rt2560_media_change(struct ifnet *ifp)
835 {
836 	int error;
837 
838 	error = ieee80211_media_change(ifp);
839 	if (error != ENETRESET)
840 		return error;
841 
842 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
843 		rt2560_init(ifp);
844 
845 	return 0;
846 }
847 
848 /*
849  * This function is called periodically (every 200ms) during scanning to
850  * switch from one channel to another.
851  */
852 void
853 rt2560_next_scan(void *arg)
854 {
855 	struct rt2560_softc *sc = arg;
856 	struct ieee80211com *ic = &sc->sc_ic;
857 	int s;
858 
859 	s = splnet();
860 	if (ic->ic_state == IEEE80211_S_SCAN)
861 		ieee80211_next_scan(ic);
862 	splx(s);
863 }
864 
865 /*
866  * This function is called for each neighbor node.
867  */
868 void
869 rt2560_iter_func(void *arg, struct ieee80211_node *ni)
870 {
871 	struct rt2560_node *rn = (struct rt2560_node *)ni;
872 
873 	ieee80211_rssadapt_updatestats(&rn->rssadapt);
874 }
875 
876 /*
877  * This function is called periodically (every 100ms) in RUN state to update
878  * the rate adaptation statistics.
879  */
880 void
881 rt2560_update_rssadapt(void *arg)
882 {
883 	struct rt2560_softc *sc = arg;
884 	struct ieee80211com *ic = &sc->sc_ic;
885 	int s;
886 
887 	s = splnet();
888 	ieee80211_iterate_nodes(&ic->ic_sta, rt2560_iter_func, arg);
889 
890 	callout_reset(&sc->rssadapt_ch, hz / 10, rt2560_update_rssadapt, sc);
891 	splx(s);
892 }
893 
894 int
895 rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
896 {
897 	struct rt2560_softc *sc = ic->ic_ifp->if_softc;
898 	enum ieee80211_state ostate;
899 	struct ieee80211_node *ni;
900 	struct mbuf *m;
901 	int error = 0;
902 
903 	ostate = ic->ic_state;
904 	callout_stop(&sc->scan_ch);
905 
906 	switch (nstate) {
907 	case IEEE80211_S_INIT:
908 		callout_stop(&sc->rssadapt_ch);
909 
910 		if (ostate == IEEE80211_S_RUN) {
911 			/* abort TSF synchronization */
912 			RAL_WRITE(sc, RT2560_CSR14, 0);
913 
914 			/* turn association led off */
915 			rt2560_update_led(sc, 0, 0);
916 		}
917 		break;
918 
919 	case IEEE80211_S_SCAN:
920 		rt2560_set_chan(sc, ic->ic_curchan);
921 		callout_reset(&sc->scan_ch, (sc->dwelltime * hz) / 1000,
922 		    rt2560_next_scan, sc);
923 		break;
924 
925 	case IEEE80211_S_AUTH:
926 		rt2560_set_chan(sc, ic->ic_curchan);
927 		break;
928 
929 	case IEEE80211_S_ASSOC:
930 		rt2560_set_chan(sc, ic->ic_curchan);
931 		break;
932 
933 	case IEEE80211_S_RUN:
934 		rt2560_set_chan(sc, ic->ic_curchan);
935 
936 		ni = ic->ic_bss;
937 
938 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
939 			rt2560_update_plcp(sc);
940 			rt2560_set_basicrates(sc);
941 			rt2560_set_bssid(sc, ni->ni_bssid);
942 		}
943 
944 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
945 		    ic->ic_opmode == IEEE80211_M_IBSS) {
946 			m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
947 			if (m == NULL) {
948 				aprint_error_dev(sc->sc_dev, "could not allocate beacon\n");
949 				error = ENOBUFS;
950 				break;
951 			}
952 
953 			ieee80211_ref_node(ni);
954 			error = rt2560_tx_bcn(sc, m, ni);
955 			if (error != 0)
956 				break;
957 		}
958 
959 		/* turn association led on */
960 		rt2560_update_led(sc, 1, 0);
961 
962 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
963 			callout_reset(&sc->rssadapt_ch, hz / 10,
964 			    rt2560_update_rssadapt, sc);
965 			rt2560_enable_tsf_sync(sc);
966 		}
967 		break;
968 	}
969 
970 	return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
971 }
972 
973 /*
974  * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
975  * 93C66).
976  */
977 uint16_t
978 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
979 {
980 	uint32_t tmp;
981 	uint16_t val;
982 	int n;
983 
984 	/* clock C once before the first command */
985 	RT2560_EEPROM_CTL(sc, 0);
986 
987 	RT2560_EEPROM_CTL(sc, RT2560_S);
988 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
989 	RT2560_EEPROM_CTL(sc, RT2560_S);
990 
991 	/* write start bit (1) */
992 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
993 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
994 
995 	/* write READ opcode (10) */
996 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
997 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
998 	RT2560_EEPROM_CTL(sc, RT2560_S);
999 	RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
1000 
1001 	/* write address (A5-A0 or A7-A0) */
1002 	n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
1003 	for (; n >= 0; n--) {
1004 		RT2560_EEPROM_CTL(sc, RT2560_S |
1005 		    (((addr >> n) & 1) << RT2560_SHIFT_D));
1006 		RT2560_EEPROM_CTL(sc, RT2560_S |
1007 		    (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
1008 	}
1009 
1010 	RT2560_EEPROM_CTL(sc, RT2560_S);
1011 
1012 	/* read data Q15-Q0 */
1013 	val = 0;
1014 	for (n = 15; n >= 0; n--) {
1015 		RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
1016 		tmp = RAL_READ(sc, RT2560_CSR21);
1017 		val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
1018 		RT2560_EEPROM_CTL(sc, RT2560_S);
1019 	}
1020 
1021 	RT2560_EEPROM_CTL(sc, 0);
1022 
1023 	/* clear Chip Select and clock C */
1024 	RT2560_EEPROM_CTL(sc, RT2560_S);
1025 	RT2560_EEPROM_CTL(sc, 0);
1026 	RT2560_EEPROM_CTL(sc, RT2560_C);
1027 
1028 	return val;
1029 }
1030 
1031 /*
1032  * Some frames were processed by the hardware cipher engine and are ready for
1033  * transmission.
1034  */
1035 void
1036 rt2560_encryption_intr(struct rt2560_softc *sc)
1037 {
1038 	struct rt2560_tx_desc *desc;
1039 	int hw;
1040 
1041 	/* retrieve last descriptor index processed by cipher engine */
1042 	hw = (RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr) /
1043 	    RT2560_TX_DESC_SIZE;
1044 
1045 	for (; sc->txq.next_encrypt != hw;) {
1046 		desc = &sc->txq.desc[sc->txq.next_encrypt];
1047 
1048 		bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
1049 		    sc->txq.next_encrypt * RT2560_TX_DESC_SIZE,
1050 		    RT2560_TX_DESC_SIZE, BUS_DMASYNC_POSTREAD);
1051 
1052 		if (le32toh(desc->flags) &
1053 		    (RT2560_TX_BUSY | RT2560_TX_CIPHER_BUSY))
1054 			break;
1055 
1056 		/* for TKIP, swap eiv field to fix a bug in ASIC */
1057 		if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
1058 		    RT2560_TX_CIPHER_TKIP)
1059 			desc->eiv = bswap32(desc->eiv);
1060 
1061 		/* mark the frame ready for transmission */
1062 		desc->flags |= htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
1063 
1064 		bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
1065 		    sc->txq.next_encrypt * RT2560_TX_DESC_SIZE,
1066 		    RT2560_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1067 
1068 		DPRINTFN(15, ("encryption done idx=%u\n",
1069 		    sc->txq.next_encrypt));
1070 
1071 		sc->txq.next_encrypt =
1072 		    (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
1073 	}
1074 
1075 	/* kick Tx */
1076 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
1077 }
1078 
1079 void
1080 rt2560_tx_intr(struct rt2560_softc *sc)
1081 {
1082 	struct ieee80211com *ic = &sc->sc_ic;
1083 	struct ifnet *ifp = ic->ic_ifp;
1084 	struct rt2560_tx_desc *desc;
1085 	struct rt2560_tx_data *data;
1086 	struct rt2560_node *rn;
1087 	int s;
1088 
1089 	s = splnet();
1090 
1091 	for (;;) {
1092 		desc = &sc->txq.desc[sc->txq.next];
1093 		data = &sc->txq.data[sc->txq.next];
1094 
1095 		bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
1096 		    sc->txq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1097 		    BUS_DMASYNC_POSTREAD);
1098 
1099 		if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1100 		    (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY) ||
1101 		    !(le32toh(desc->flags) & RT2560_TX_VALID))
1102 			break;
1103 
1104 		rn = (struct rt2560_node *)data->ni;
1105 
1106 		switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1107 		case RT2560_TX_SUCCESS:
1108 			DPRINTFN(10, ("data frame sent successfully\n"));
1109 			if (data->id.id_node != NULL) {
1110 				ieee80211_rssadapt_raise_rate(ic,
1111 				    &rn->rssadapt, &data->id);
1112 			}
1113 			ifp->if_opackets++;
1114 			break;
1115 
1116 		case RT2560_TX_SUCCESS_RETRY:
1117 			DPRINTFN(9, ("data frame sent after %u retries\n",
1118 			    (le32toh(desc->flags) >> 5) & 0x7));
1119 			ifp->if_opackets++;
1120 			break;
1121 
1122 		case RT2560_TX_FAIL_RETRY:
1123 			DPRINTFN(9, ("sending data frame failed (too much "
1124 			    "retries)\n"));
1125 			if (data->id.id_node != NULL) {
1126 				ieee80211_rssadapt_lower_rate(ic, data->ni,
1127 				    &rn->rssadapt, &data->id);
1128 			}
1129 			ifp->if_oerrors++;
1130 			break;
1131 
1132 		case RT2560_TX_FAIL_INVALID:
1133 		case RT2560_TX_FAIL_OTHER:
1134 		default:
1135 			aprint_error_dev(sc->sc_dev,
1136 			    "sending data frame failed 0x%08x\n",
1137 			    le32toh(desc->flags));
1138 			ifp->if_oerrors++;
1139 		}
1140 
1141 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1142 		    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1143 		bus_dmamap_unload(sc->sc_dmat, data->map);
1144 		m_freem(data->m);
1145 		data->m = NULL;
1146 		ieee80211_free_node(data->ni);
1147 		data->ni = NULL;
1148 
1149 		/* descriptor is no longer valid */
1150 		desc->flags &= ~htole32(RT2560_TX_VALID);
1151 
1152 		bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
1153 		    sc->txq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1154 		    BUS_DMASYNC_PREWRITE);
1155 
1156 		DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
1157 
1158 		sc->txq.queued--;
1159 		sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1160 	}
1161 
1162 	sc->sc_tx_timer = 0;
1163 	ifp->if_flags &= ~IFF_OACTIVE;
1164 	rt2560_start(ifp); /* in softint */
1165 
1166 	splx(s);
1167 }
1168 
1169 void
1170 rt2560_prio_intr(struct rt2560_softc *sc)
1171 {
1172 	struct ieee80211com *ic = &sc->sc_ic;
1173 	struct ifnet *ifp = ic->ic_ifp;
1174 	struct rt2560_tx_desc *desc;
1175 	struct rt2560_tx_data *data;
1176 	int s;
1177 
1178 	s = splnet();
1179 
1180 	for (;;) {
1181 		desc = &sc->prioq.desc[sc->prioq.next];
1182 		data = &sc->prioq.data[sc->prioq.next];
1183 
1184 		bus_dmamap_sync(sc->sc_dmat, sc->prioq.map,
1185 		    sc->prioq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1186 		    BUS_DMASYNC_POSTREAD);
1187 
1188 		if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1189 		    !(le32toh(desc->flags) & RT2560_TX_VALID))
1190 			break;
1191 
1192 		switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1193 		case RT2560_TX_SUCCESS:
1194 			DPRINTFN(10, ("mgt frame sent successfully\n"));
1195 			break;
1196 
1197 		case RT2560_TX_SUCCESS_RETRY:
1198 			DPRINTFN(9, ("mgt frame sent after %u retries\n",
1199 			    (le32toh(desc->flags) >> 5) & 0x7));
1200 			break;
1201 
1202 		case RT2560_TX_FAIL_RETRY:
1203 			DPRINTFN(9, ("sending mgt frame failed (too much "
1204 			    "retries)\n"));
1205 			break;
1206 
1207 		case RT2560_TX_FAIL_INVALID:
1208 		case RT2560_TX_FAIL_OTHER:
1209 		default:
1210 			aprint_error_dev(sc->sc_dev, "sending mgt frame failed 0x%08x\n",
1211 			    le32toh(desc->flags));
1212 		}
1213 
1214 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1215 		    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1216 		bus_dmamap_unload(sc->sc_dmat, data->map);
1217 		m_freem(data->m);
1218 		data->m = NULL;
1219 		ieee80211_free_node(data->ni);
1220 		data->ni = NULL;
1221 
1222 		/* descriptor is no longer valid */
1223 		desc->flags &= ~htole32(RT2560_TX_VALID);
1224 
1225 		bus_dmamap_sync(sc->sc_dmat, sc->prioq.map,
1226 		    sc->prioq.next * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1227 		    BUS_DMASYNC_PREWRITE);
1228 
1229 		DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next));
1230 
1231 		sc->prioq.queued--;
1232 		sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1233 	}
1234 
1235 	sc->sc_tx_timer = 0;
1236 	ifp->if_flags &= ~IFF_OACTIVE;
1237 	rt2560_start(ifp); /* in softint */
1238 
1239 	splx(s);
1240 }
1241 
1242 /*
1243  * Some frames were processed by the hardware cipher engine and are ready for
1244  * transmission to the IEEE802.11 layer.
1245  */
1246 void
1247 rt2560_decryption_intr(struct rt2560_softc *sc)
1248 {
1249 	struct ieee80211com *ic = &sc->sc_ic;
1250 	struct ifnet *ifp = ic->ic_ifp;
1251 	struct rt2560_rx_desc *desc;
1252 	struct rt2560_rx_data *data;
1253 	struct rt2560_node *rn;
1254 	struct ieee80211_frame *wh;
1255 	struct ieee80211_node *ni;
1256 	struct mbuf *mnew, *m;
1257 	int hw, error, s;
1258 
1259 	/* retrieve last decriptor index processed by cipher engine */
1260 	hw = (RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr) /
1261 	    RT2560_RX_DESC_SIZE;
1262 
1263 	for (; sc->rxq.cur_decrypt != hw;) {
1264 		desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1265 		data = &sc->rxq.data[sc->rxq.cur_decrypt];
1266 
1267 		bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
1268 		    sc->rxq.cur_decrypt * RT2560_TX_DESC_SIZE,
1269 		    RT2560_TX_DESC_SIZE, BUS_DMASYNC_POSTREAD);
1270 
1271 		if (le32toh(desc->flags) &
1272 		    (RT2560_RX_BUSY | RT2560_RX_CIPHER_BUSY))
1273 			break;
1274 
1275 		if (data->drop) {
1276 			ifp->if_ierrors++;
1277 			goto skip;
1278 		}
1279 
1280 		if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1281 		    (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1282 			ifp->if_ierrors++;
1283 			goto skip;
1284 		}
1285 
1286 		/*
1287 		 * Try to allocate a new mbuf for this ring element and load it
1288 		 * before processing the current mbuf.  If the ring element
1289 		 * cannot be loaded, drop the received packet and reuse the old
1290 		 * mbuf.  In the unlikely case that the old mbuf can't be
1291 		 * reloaded either, explicitly panic.
1292 		 */
1293 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1294 		if (mnew == NULL) {
1295 			ifp->if_ierrors++;
1296 			goto skip;
1297 		}
1298 
1299 		MCLGET(mnew, M_DONTWAIT);
1300 		if (!(mnew->m_flags & M_EXT)) {
1301 			m_freem(mnew);
1302 			ifp->if_ierrors++;
1303 			goto skip;
1304 		}
1305 
1306 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1307 		    data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1308 		bus_dmamap_unload(sc->sc_dmat, data->map);
1309 
1310 		error = bus_dmamap_load(sc->sc_dmat, data->map,
1311 		    mtod(mnew, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
1312 		if (error != 0) {
1313 			m_freem(mnew);
1314 
1315 			/* try to reload the old mbuf */
1316 			error = bus_dmamap_load(sc->sc_dmat, data->map,
1317 			    mtod(data->m, void *), MCLBYTES, NULL,
1318 			    BUS_DMA_NOWAIT);
1319 			if (error != 0) {
1320 				/* very unlikely that it will fail... */
1321 				panic("%s: could not load old rx mbuf",
1322 				    device_xname(sc->sc_dev));
1323 			}
1324 			/* physical address may have changed */
1325 			desc->physaddr = htole32(data->map->dm_segs->ds_addr);
1326 			ifp->if_ierrors++;
1327 			goto skip;
1328 		}
1329 
1330 		/*
1331 		 * New mbuf successfully loaded, update Rx ring and continue
1332 		 * processing.
1333 		 */
1334 		m = data->m;
1335 		data->m = mnew;
1336 		desc->physaddr = htole32(data->map->dm_segs->ds_addr);
1337 
1338 		/* finalize mbuf */
1339 		m_set_rcvif(m, ifp);
1340 		m->m_pkthdr.len = m->m_len =
1341 		    (le32toh(desc->flags) >> 16) & 0xfff;
1342 
1343 		s = splnet();
1344 
1345 		if (sc->sc_drvbpf != NULL) {
1346 			struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1347 			uint32_t tsf_lo, tsf_hi;
1348 
1349 			/* get timestamp (low and high 32 bits) */
1350 			tsf_hi = RAL_READ(sc, RT2560_CSR17);
1351 			tsf_lo = RAL_READ(sc, RT2560_CSR16);
1352 
1353 			tap->wr_tsf =
1354 			    htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1355 			tap->wr_flags = 0;
1356 			tap->wr_rate = rt2560_rxrate(desc);
1357 			tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1358 			tap->wr_chan_flags =
1359 			    htole16(ic->ic_ibss_chan->ic_flags);
1360 			tap->wr_antenna = sc->rx_ant;
1361 			tap->wr_antsignal = desc->rssi;
1362 
1363 			bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
1364 		}
1365 
1366 		wh = mtod(m, struct ieee80211_frame *);
1367 		ni = ieee80211_find_rxnode(ic,
1368 		    (struct ieee80211_frame_min *)wh);
1369 
1370 		/* send the frame to the 802.11 layer */
1371 		ieee80211_input(ic, m, ni, desc->rssi, 0);
1372 
1373 		/* give rssi to the rate adatation algorithm */
1374 		rn = (struct rt2560_node *)ni;
1375 		ieee80211_rssadapt_input(ic, ni, &rn->rssadapt, desc->rssi);
1376 
1377 		/* node is no longer needed */
1378 		ieee80211_free_node(ni);
1379 
1380 		splx(s);
1381 
1382 skip:		desc->flags = htole32(RT2560_RX_BUSY);
1383 
1384 		bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
1385 		    sc->rxq.cur_decrypt * RT2560_TX_DESC_SIZE,
1386 		    RT2560_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1387 
1388 		DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt));
1389 
1390 		sc->rxq.cur_decrypt =
1391 		    (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1392 	}
1393 
1394 	/*
1395 	 * In HostAP mode, ieee80211_input() will enqueue packets in if_snd
1396 	 * without calling if_start().
1397 	 */
1398 	s = splnet();
1399 	if (!IFQ_IS_EMPTY(&ifp->if_snd) && !(ifp->if_flags & IFF_OACTIVE))
1400 		rt2560_start(ifp);
1401 	splx(s);
1402 }
1403 
1404 /*
1405  * Some frames were received. Pass them to the hardware cipher engine before
1406  * sending them to the 802.11 layer.
1407  */
1408 void
1409 rt2560_rx_intr(struct rt2560_softc *sc)
1410 {
1411 	struct rt2560_rx_desc *desc;
1412 	struct rt2560_rx_data *data;
1413 
1414 	for (;;) {
1415 		desc = &sc->rxq.desc[sc->rxq.cur];
1416 		data = &sc->rxq.data[sc->rxq.cur];
1417 
1418 		bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
1419 		    sc->rxq.cur * RT2560_RX_DESC_SIZE, RT2560_RX_DESC_SIZE,
1420 		    BUS_DMASYNC_POSTREAD);
1421 
1422 		if (le32toh(desc->flags) &
1423 		    (RT2560_RX_BUSY | RT2560_RX_CIPHER_BUSY))
1424 			break;
1425 
1426 		data->drop = 0;
1427 
1428 		if (le32toh(desc->flags) &
1429 		    (RT2560_RX_PHY_ERROR | RT2560_RX_CRC_ERROR)) {
1430 			/*
1431 			 * This should not happen since we did not request
1432 			 * to receive those frames when we filled RXCSR0.
1433 			 */
1434 			DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1435 			    le32toh(desc->flags)));
1436 			data->drop = 1;
1437 		}
1438 
1439 		if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1440 			DPRINTFN(5, ("bad length\n"));
1441 			data->drop = 1;
1442 		}
1443 
1444 		/* mark the frame for decryption */
1445 		desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1446 
1447 		bus_dmamap_sync(sc->sc_dmat, sc->rxq.map,
1448 		    sc->rxq.cur * RT2560_RX_DESC_SIZE, RT2560_RX_DESC_SIZE,
1449 		    BUS_DMASYNC_PREWRITE);
1450 
1451 		DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1452 
1453 		sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1454 	}
1455 
1456 	/* kick decrypt */
1457 	RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1458 }
1459 
1460 /*
1461  * This function is called periodically in IBSS mode when a new beacon must be
1462  * sent out.
1463  */
1464 static void
1465 rt2560_beacon_expire(struct rt2560_softc *sc)
1466 {
1467 	struct ieee80211com *ic = &sc->sc_ic;
1468 	struct rt2560_tx_data *data;
1469 
1470 	if (ic->ic_opmode != IEEE80211_M_IBSS &&
1471 	    ic->ic_opmode != IEEE80211_M_HOSTAP)
1472 		return;
1473 
1474 	data = &sc->bcnq.data[sc->bcnq.next];
1475 
1476 	bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1477 	    data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1478 	bus_dmamap_unload(sc->sc_dmat, data->map);
1479 
1480 	ieee80211_beacon_update(ic, data->ni, &sc->sc_bo, data->m, 1);
1481 
1482 	bpf_mtap3(ic->ic_rawbpf, data->m);
1483 	rt2560_tx_bcn(sc, data->m, data->ni);
1484 
1485 	DPRINTFN(15, ("beacon expired\n"));
1486 
1487 	sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1488 }
1489 
1490 static void
1491 rt2560_wakeup_expire(struct rt2560_softc *sc)
1492 {
1493 	DPRINTFN(15, ("wakeup expired\n"));
1494 }
1495 
1496 int
1497 rt2560_intr(void *arg)
1498 {
1499 	struct rt2560_softc *sc = arg;
1500 	struct ifnet *ifp = &sc->sc_if;
1501 	uint32_t r;
1502 
1503 	if (!device_is_active(sc->sc_dev))
1504 		return 0;
1505 
1506 	if ((r = RAL_READ(sc, RT2560_CSR7)) == 0)
1507 		return 0;       /* not for us */
1508 
1509 	/* disable interrupts */
1510 	RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1511 
1512 	/* don't re-enable interrupts if we're shutting down */
1513 	if (!(ifp->if_flags & IFF_RUNNING))
1514 		return 0;
1515 
1516 	softint_schedule(sc->sc_soft_ih);
1517 	return 1;
1518 }
1519 
1520 static void
1521 rt2560_softintr(void *arg)
1522 {
1523 	struct rt2560_softc *sc = arg;
1524 	struct ifnet *ifp = &sc->sc_if;
1525 	uint32_t r;
1526 
1527 	if (!device_is_active(sc->sc_dev) || !(ifp->if_flags & IFF_RUNNING))
1528 		return;
1529 
1530 	if ((r = RAL_READ(sc, RT2560_CSR7)) == 0)
1531 		goto out;
1532 
1533 	/* acknowledge interrupts */
1534 	RAL_WRITE(sc, RT2560_CSR7, r);
1535 
1536 	if (r & RT2560_BEACON_EXPIRE)
1537 		rt2560_beacon_expire(sc);
1538 
1539 	if (r & RT2560_WAKEUP_EXPIRE)
1540 		rt2560_wakeup_expire(sc);
1541 
1542 	if (r & RT2560_ENCRYPTION_DONE)
1543 		rt2560_encryption_intr(sc);
1544 
1545 	if (r & RT2560_TX_DONE)
1546 		rt2560_tx_intr(sc);
1547 
1548 	if (r & RT2560_PRIO_DONE)
1549 		rt2560_prio_intr(sc);
1550 
1551 	if (r & RT2560_DECRYPTION_DONE)
1552 		rt2560_decryption_intr(sc);
1553 
1554 	if (r & RT2560_RX_DONE)
1555 		rt2560_rx_intr(sc);
1556 
1557 out:
1558 	/* re-enable interrupts */
1559 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1560 }
1561 
1562 /* quickly determine if a given rate is CCK or OFDM */
1563 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1564 
1565 #define RAL_ACK_SIZE	14	/* 10 + 4(FCS) */
1566 #define RAL_CTS_SIZE	14	/* 10 + 4(FCS) */
1567 
1568 #define RAL_SIFS		10	/* us */
1569 
1570 #define RT2560_RXTX_TURNAROUND	10	/* us */
1571 
1572 /*
1573  * This function is only used by the Rx radiotap code. It returns the rate at
1574  * which a given frame was received.
1575  */
1576 static uint8_t
1577 rt2560_rxrate(struct rt2560_rx_desc *desc)
1578 {
1579 	if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1580 		/* reverse function of rt2560_plcp_signal */
1581 		switch (desc->rate) {
1582 		case 0xb:	return 12;
1583 		case 0xf:	return 18;
1584 		case 0xa:	return 24;
1585 		case 0xe:	return 36;
1586 		case 0x9:	return 48;
1587 		case 0xd:	return 72;
1588 		case 0x8:	return 96;
1589 		case 0xc:	return 108;
1590 		}
1591 	} else {
1592 		if (desc->rate == 10)
1593 			return 2;
1594 		if (desc->rate == 20)
1595 			return 4;
1596 		if (desc->rate == 55)
1597 			return 11;
1598 		if (desc->rate == 110)
1599 			return 22;
1600 	}
1601 	return 2;	/* should not get there */
1602 }
1603 
1604 /*
1605  * Return the expected ack rate for a frame transmitted at rate `rate'.
1606  * XXX: this should depend on the destination node basic rate set.
1607  */
1608 static int
1609 rt2560_ack_rate(struct ieee80211com *ic, int rate)
1610 {
1611 	switch (rate) {
1612 	/* CCK rates */
1613 	case 2:
1614 		return 2;
1615 	case 4:
1616 	case 11:
1617 	case 22:
1618 		return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1619 
1620 	/* OFDM rates */
1621 	case 12:
1622 	case 18:
1623 		return 12;
1624 	case 24:
1625 	case 36:
1626 		return 24;
1627 	case 48:
1628 	case 72:
1629 	case 96:
1630 	case 108:
1631 		return 48;
1632 	}
1633 
1634 	/* default to 1Mbps */
1635 	return 2;
1636 }
1637 
1638 /*
1639  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1640  * The function automatically determines the operating mode depending on the
1641  * given rate. `flags' indicates whether short preamble is in use or not.
1642  */
1643 static uint16_t
1644 rt2560_txtime(int len, int rate, uint32_t flags)
1645 {
1646 	uint16_t txtime;
1647 
1648 	if (RAL_RATE_IS_OFDM(rate)) {
1649 		/* IEEE Std 802.11a-1999, pp. 37 */
1650 		txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1651 		txtime = 16 + 4 + 4 * txtime + 6;
1652 	} else {
1653 		/* IEEE Std 802.11b-1999, pp. 28 */
1654 		txtime = (16 * len + rate - 1) / rate;
1655 		if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1656 			txtime +=  72 + 24;
1657 		else
1658 			txtime += 144 + 48;
1659 	}
1660 	return txtime;
1661 }
1662 
1663 static uint8_t
1664 rt2560_plcp_signal(int rate)
1665 {
1666 	switch (rate) {
1667 	/* CCK rates (returned values are device-dependent) */
1668 	case 2:		return 0x0;
1669 	case 4:		return 0x1;
1670 	case 11:	return 0x2;
1671 	case 22:	return 0x3;
1672 
1673 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1674 	case 12:	return 0xb;
1675 	case 18:	return 0xf;
1676 	case 24:	return 0xa;
1677 	case 36:	return 0xe;
1678 	case 48:	return 0x9;
1679 	case 72:	return 0xd;
1680 	case 96:	return 0x8;
1681 	case 108:	return 0xc;
1682 
1683 	/* unsupported rates (should not get there) */
1684 	default:	return 0xff;
1685 	}
1686 }
1687 
1688 static void
1689 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1690     uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1691 {
1692 	struct ieee80211com *ic = &sc->sc_ic;
1693 	uint16_t plcp_length;
1694 	int remainder;
1695 
1696 	desc->flags = htole32(flags);
1697 	desc->flags |= htole32(len << 16);
1698 	desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) :
1699 	    htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
1700 
1701 	desc->physaddr = htole32(physaddr);
1702 	desc->wme = htole16(
1703 	    RT2560_AIFSN(2) |
1704 	    RT2560_LOGCWMIN(3) |
1705 	    RT2560_LOGCWMAX(8));
1706 
1707 	/* setup PLCP fields */
1708 	desc->plcp_signal  = rt2560_plcp_signal(rate);
1709 	desc->plcp_service = 4;
1710 
1711 	len += IEEE80211_CRC_LEN;
1712 	if (RAL_RATE_IS_OFDM(rate)) {
1713 		desc->flags |= htole32(RT2560_TX_OFDM);
1714 
1715 		plcp_length = len & 0xfff;
1716 		desc->plcp_length_hi = plcp_length >> 6;
1717 		desc->plcp_length_lo = plcp_length & 0x3f;
1718 	} else {
1719 		plcp_length = (16 * len + rate - 1) / rate;
1720 		if (rate == 22) {
1721 			remainder = (16 * len) % 22;
1722 			if (remainder != 0 && remainder < 7)
1723 				desc->plcp_service |= RT2560_PLCP_LENGEXT;
1724 		}
1725 		desc->plcp_length_hi = plcp_length >> 8;
1726 		desc->plcp_length_lo = plcp_length & 0xff;
1727 
1728 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1729 			desc->plcp_signal |= 0x08;
1730 	}
1731 }
1732 
1733 static int
1734 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1735     struct ieee80211_node *ni)
1736 {
1737 	struct rt2560_tx_desc *desc;
1738 	struct rt2560_tx_data *data;
1739 	int rate, error;
1740 
1741 	desc = &sc->bcnq.desc[sc->bcnq.cur];
1742 	data = &sc->bcnq.data[sc->bcnq.cur];
1743 
1744 	rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1745 
1746 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1747 	    BUS_DMA_NOWAIT);
1748 	if (error != 0) {
1749 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1750 		    error);
1751 		m_freem(m0);
1752 		return error;
1753 	}
1754 
1755 	data->m = m0;
1756 	data->ni = ni;
1757 
1758 	rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1759 	    RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0,
1760 	    data->map->dm_segs->ds_addr);
1761 
1762 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1763 	    BUS_DMASYNC_PREWRITE);
1764 	bus_dmamap_sync(sc->sc_dmat, sc->bcnq.map,
1765 	    sc->bcnq.cur * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1766 	    BUS_DMASYNC_PREWRITE);
1767 
1768 	return 0;
1769 }
1770 
1771 static int
1772 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1773     struct ieee80211_node *ni)
1774 {
1775 	struct ieee80211com *ic = &sc->sc_ic;
1776 	struct rt2560_tx_desc *desc;
1777 	struct rt2560_tx_data *data;
1778 	struct ieee80211_frame *wh;
1779 	struct ieee80211_key *k;
1780 	uint16_t dur;
1781 	uint32_t flags = 0;
1782 	int rate, error;
1783 
1784 	desc = &sc->prioq.desc[sc->prioq.cur];
1785 	data = &sc->prioq.data[sc->prioq.cur];
1786 
1787 	rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1788 
1789 	wh = mtod(m0, struct ieee80211_frame *);
1790 
1791 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1792 		k = ieee80211_crypto_encap(ic, ni, m0);
1793 		if (k == NULL) {
1794 			m_freem(m0);
1795 			return ENOBUFS;
1796 		}
1797 
1798 		/* packet header may have moved, reset our local pointer */
1799 		wh = mtod(m0, struct ieee80211_frame *);
1800 	}
1801 
1802 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1803 	    BUS_DMA_NOWAIT);
1804 	if (error != 0) {
1805 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1806 		    error);
1807 		m_freem(m0);
1808 		return error;
1809 	}
1810 
1811 	if (sc->sc_drvbpf != NULL) {
1812 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1813 
1814 		tap->wt_flags = 0;
1815 		tap->wt_rate = rate;
1816 		tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1817 		tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1818 		tap->wt_antenna = sc->tx_ant;
1819 
1820 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1821 	}
1822 
1823 	data->m = m0;
1824 	data->ni = ni;
1825 
1826 	wh = mtod(m0, struct ieee80211_frame *);
1827 
1828 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1829 		flags |= RT2560_TX_ACK;
1830 
1831 		dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
1832 		    RAL_SIFS;
1833 		*(uint16_t *)wh->i_dur = htole16(dur);
1834 
1835 		/* tell hardware to add timestamp for probe responses */
1836 		if ((wh->i_fc[0] &
1837 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1838 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1839 			flags |= RT2560_TX_TIMESTAMP;
1840 	}
1841 
1842 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1843 	    data->map->dm_segs->ds_addr);
1844 
1845 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1846 	    BUS_DMASYNC_PREWRITE);
1847 	bus_dmamap_sync(sc->sc_dmat, sc->prioq.map,
1848 	    sc->prioq.cur * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
1849 	    BUS_DMASYNC_PREWRITE);
1850 
1851 	DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1852 	    m0->m_pkthdr.len, sc->prioq.cur, rate));
1853 
1854 	/* kick prio */
1855 	sc->prioq.queued++;
1856 	sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1857 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1858 
1859 	return 0;
1860 }
1861 
1862 /*
1863  * Build a RTS control frame.
1864  */
1865 static struct mbuf *
1866 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1867     uint16_t dur)
1868 {
1869 	struct ieee80211_frame_rts *rts;
1870 	struct mbuf *m;
1871 
1872 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1873 	if (m == NULL) {
1874 		sc->sc_ic.ic_stats.is_tx_nobuf++;
1875 		aprint_error_dev(sc->sc_dev, "could not allocate RTS frame\n");
1876 		return NULL;
1877 	}
1878 
1879 	rts = mtod(m, struct ieee80211_frame_rts *);
1880 
1881 	rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1882 	    IEEE80211_FC0_SUBTYPE_RTS;
1883 	rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1884 	*(uint16_t *)rts->i_dur = htole16(dur);
1885 	IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1886 	IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1887 
1888 	m->m_pkthdr.len = m->m_len = sizeof (struct ieee80211_frame_rts);
1889 
1890 	return m;
1891 }
1892 
1893 static int
1894 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1895     struct ieee80211_node *ni)
1896 {
1897 	struct ieee80211com *ic = &sc->sc_ic;
1898 	struct rt2560_tx_desc *desc;
1899 	struct rt2560_tx_data *data;
1900 	struct rt2560_node *rn;
1901 	struct ieee80211_rateset *rs;
1902 	struct ieee80211_frame *wh;
1903 	struct ieee80211_key *k;
1904 	struct mbuf *mnew;
1905 	uint16_t dur;
1906 	uint32_t flags = 0;
1907 	int rate, error;
1908 
1909 	wh = mtod(m0, struct ieee80211_frame *);
1910 
1911 	if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1912 		rs = &ic->ic_sup_rates[ic->ic_curmode];
1913 		rate = rs->rs_rates[ic->ic_fixed_rate];
1914 	} else {
1915 		rs = &ni->ni_rates;
1916 		rn = (struct rt2560_node *)ni;
1917 		ni->ni_txrate = ieee80211_rssadapt_choose(&rn->rssadapt, rs,
1918 		    wh, m0->m_pkthdr.len, -1, NULL, 0);
1919 		rate = rs->rs_rates[ni->ni_txrate];
1920 	}
1921 	rate &= IEEE80211_RATE_VAL;
1922 
1923 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1924 		k = ieee80211_crypto_encap(ic, ni, m0);
1925 		if (k == NULL) {
1926 			m_freem(m0);
1927 			return ENOBUFS;
1928 		}
1929 
1930 		/* packet header may have moved, reset our local pointer */
1931 		wh = mtod(m0, struct ieee80211_frame *);
1932 	}
1933 
1934 	/*
1935 	 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1936 	 * for directed frames only when the length of the MPDU is greater
1937 	 * than the length threshold indicated by [...]" ic_rtsthreshold.
1938 	 */
1939 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1940 	    m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1941 		struct mbuf *m;
1942 		int rtsrate, ackrate;
1943 
1944 		rtsrate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1945 		ackrate = rt2560_ack_rate(ic, rate);
1946 
1947 		dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) +
1948 		      rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) +
1949 		      rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
1950 		      3 * RAL_SIFS;
1951 
1952 		m = rt2560_get_rts(sc, wh, dur);
1953 
1954 		desc = &sc->txq.desc[sc->txq.cur_encrypt];
1955 		data = &sc->txq.data[sc->txq.cur_encrypt];
1956 
1957 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m,
1958 		    BUS_DMA_NOWAIT);
1959 		if (error != 0) {
1960 			aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1961 			    error);
1962 			m_freem(m);
1963 			m_freem(m0);
1964 			return error;
1965 		}
1966 
1967 		/* avoid multiple free() of the same node for each fragment */
1968 		ieee80211_ref_node(ni);
1969 
1970 		data->m = m;
1971 		data->ni = ni;
1972 
1973 		/* RTS frames are not taken into account for rssadapt */
1974 		data->id.id_node = NULL;
1975 
1976 		rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1977 		    RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1,
1978 		    data->map->dm_segs->ds_addr);
1979 
1980 		bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1981 		    data->map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1982 		bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
1983 		    sc->txq.cur_encrypt * RT2560_TX_DESC_SIZE,
1984 		    RT2560_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1985 
1986 		sc->txq.queued++;
1987 		sc->txq.cur_encrypt =
1988 		    (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1989 
1990 		/*
1991 		 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1992 		 * asynchronous data frame shall be transmitted after the CTS
1993 		 * frame and a SIFS period.
1994 		 */
1995 		flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1996 	}
1997 
1998 	data = &sc->txq.data[sc->txq.cur_encrypt];
1999 	desc = &sc->txq.desc[sc->txq.cur_encrypt];
2000 
2001 	error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2002 	    BUS_DMA_NOWAIT);
2003 	if (error != 0 && error != EFBIG) {
2004 		aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
2005 		    error);
2006 		m_freem(m0);
2007 		return error;
2008 	}
2009 	if (error != 0) {
2010 		/* too many fragments, linearize */
2011 
2012 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
2013 		if (mnew == NULL) {
2014 			m_freem(m0);
2015 			return ENOMEM;
2016 		}
2017 
2018 		M_COPY_PKTHDR(mnew, m0);
2019 		if (m0->m_pkthdr.len > MHLEN) {
2020 			MCLGET(mnew, M_DONTWAIT);
2021 			if (!(mnew->m_flags & M_EXT)) {
2022 				m_freem(m0);
2023 				m_freem(mnew);
2024 				return ENOMEM;
2025 			}
2026 		}
2027 
2028 		m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
2029 		m_freem(m0);
2030 		mnew->m_len = mnew->m_pkthdr.len;
2031 		m0 = mnew;
2032 
2033 		error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2034 		    BUS_DMA_NOWAIT);
2035 		if (error != 0) {
2036 			aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
2037 			    error);
2038 			m_freem(m0);
2039 			return error;
2040 		}
2041 
2042 		/* packet header have moved, reset our local pointer */
2043 		wh = mtod(m0, struct ieee80211_frame *);
2044 	}
2045 
2046 	if (sc->sc_drvbpf != NULL) {
2047 		struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
2048 
2049 		tap->wt_flags = 0;
2050 		tap->wt_rate = rate;
2051 		tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
2052 		tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
2053 		tap->wt_antenna = sc->tx_ant;
2054 
2055 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
2056 	}
2057 
2058 	data->m = m0;
2059 	data->ni = ni;
2060 
2061 	/* remember link conditions for rate adaptation algorithm */
2062 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
2063 		data->id.id_len = m0->m_pkthdr.len;
2064 		data->id.id_rateidx = ni->ni_txrate;
2065 		data->id.id_node = ni;
2066 		data->id.id_rssi = ni->ni_rssi;
2067 	} else
2068 		data->id.id_node = NULL;
2069 
2070 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2071 		flags |= RT2560_TX_ACK;
2072 
2073 		dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate),
2074 		    ic->ic_flags) + RAL_SIFS;
2075 		*(uint16_t *)wh->i_dur = htole16(dur);
2076 	}
2077 
2078 	rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
2079 	    data->map->dm_segs->ds_addr);
2080 
2081 	bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
2082 	    BUS_DMASYNC_PREWRITE);
2083 	bus_dmamap_sync(sc->sc_dmat, sc->txq.map,
2084 	    sc->txq.cur_encrypt * RT2560_TX_DESC_SIZE, RT2560_TX_DESC_SIZE,
2085 	    BUS_DMASYNC_PREWRITE);
2086 
2087 	DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
2088 	    m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
2089 
2090 	/* kick encrypt */
2091 	sc->txq.queued++;
2092 	sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
2093 	RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
2094 
2095 	return 0;
2096 }
2097 
2098 static void
2099 rt2560_start(struct ifnet *ifp)
2100 {
2101 	struct rt2560_softc *sc = ifp->if_softc;
2102 	struct ieee80211com *ic = &sc->sc_ic;
2103 	struct mbuf *m0;
2104 	struct ieee80211_node *ni;
2105 	struct ether_header *eh;
2106 
2107 	/*
2108 	 * net80211 may still try to send management frames even if the
2109 	 * IFF_RUNNING flag is not set...
2110 	 */
2111 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2112 		return;
2113 
2114 	for (;;) {
2115 		IF_POLL(&ic->ic_mgtq, m0);
2116 		if (m0 != NULL) {
2117 			if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2118 				ifp->if_flags |= IFF_OACTIVE;
2119 				break;
2120 			}
2121 			IF_DEQUEUE(&ic->ic_mgtq, m0);
2122 			if (m0 == NULL)
2123 				break;
2124 
2125 			ni = M_GETCTX(m0, struct ieee80211_node *);
2126 			M_CLEARCTX(m0);
2127 			bpf_mtap3(ic->ic_rawbpf, m0);
2128 			if (rt2560_tx_mgt(sc, m0, ni) != 0)
2129 				break;
2130 
2131 		} else {
2132 			if (ic->ic_state != IEEE80211_S_RUN)
2133 				break;
2134 			IFQ_DEQUEUE(&ifp->if_snd, m0);
2135 			if (m0 == NULL)
2136 				break;
2137 			if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
2138 				ifp->if_flags |= IFF_OACTIVE;
2139 				break;
2140 			}
2141 
2142 			if (m0->m_len < sizeof (struct ether_header) &&
2143 			    !(m0 = m_pullup(m0, sizeof (struct ether_header))))
2144                                 continue;
2145 
2146 			eh = mtod(m0, struct ether_header *);
2147 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2148 			if (ni == NULL) {
2149 				m_freem(m0);
2150 				continue;
2151 			}
2152 			bpf_mtap(ifp, m0);
2153 
2154 			m0 = ieee80211_encap(ic, m0, ni);
2155 			if (m0 == NULL) {
2156 				ieee80211_free_node(ni);
2157 				continue;
2158                         }
2159 
2160 			bpf_mtap3(ic->ic_rawbpf, m0);
2161 
2162 			if (rt2560_tx_data(sc, m0, ni) != 0) {
2163 				ieee80211_free_node(ni);
2164 				ifp->if_oerrors++;
2165 				break;
2166 			}
2167 		}
2168 
2169 		sc->sc_tx_timer = 5;
2170 		ifp->if_timer = 1;
2171 	}
2172 }
2173 
2174 static void
2175 rt2560_watchdog(struct ifnet *ifp)
2176 {
2177 	struct rt2560_softc *sc = ifp->if_softc;
2178 
2179 	ifp->if_timer = 0;
2180 
2181 	if (sc->sc_tx_timer > 0) {
2182 		if (--sc->sc_tx_timer == 0) {
2183 			aprint_error_dev(sc->sc_dev, "device timeout\n");
2184 			rt2560_init(ifp);
2185 			ifp->if_oerrors++;
2186 			return;
2187 		}
2188 		ifp->if_timer = 1;
2189 	}
2190 
2191 	ieee80211_watchdog(&sc->sc_ic);
2192 }
2193 
2194 /*
2195  * This function allows for fast channel switching in monitor mode (used by
2196  * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2197  * generate a new beacon frame.
2198  */
2199 static int
2200 rt2560_reset(struct ifnet *ifp)
2201 {
2202 	struct rt2560_softc *sc = ifp->if_softc;
2203 	struct ieee80211com *ic = &sc->sc_ic;
2204 
2205 	if (ic->ic_opmode != IEEE80211_M_MONITOR)
2206 		return ENETRESET;
2207 
2208 	rt2560_set_chan(sc, ic->ic_curchan);
2209 
2210 	return 0;
2211 }
2212 
2213 int
2214 rt2560_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2215 {
2216 	struct rt2560_softc *sc = ifp->if_softc;
2217 	struct ieee80211com *ic = &sc->sc_ic;
2218 	int s, error = 0;
2219 
2220 	s = splnet();
2221 
2222 	switch (cmd) {
2223 	case SIOCSIFFLAGS:
2224 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
2225 			break;
2226 		if (ifp->if_flags & IFF_UP) {
2227 			if (ifp->if_flags & IFF_RUNNING)
2228 				rt2560_update_promisc(sc);
2229 			else
2230 				rt2560_init(ifp);
2231 		} else {
2232 			if (ifp->if_flags & IFF_RUNNING)
2233 				rt2560_stop(ifp, 1);
2234 		}
2235 		break;
2236 
2237 	case SIOCADDMULTI:
2238 	case SIOCDELMULTI:
2239 		/* XXX no h/w multicast filter? --dyoung */
2240 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET)
2241 			error = 0;
2242 		break;
2243 
2244 	case SIOCS80211CHANNEL:
2245 		/*
2246 		 * This allows for fast channel switching in monitor mode
2247 		 * (used by kismet). In IBSS mode, we must explicitly reset
2248 		 * the interface to generate a new beacon frame.
2249 		 */
2250 		error = ieee80211_ioctl(ic, cmd, data);
2251 		if (error == ENETRESET &&
2252 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
2253 			rt2560_set_chan(sc, ic->ic_ibss_chan);
2254 			error = 0;
2255 		}
2256 		break;
2257 
2258 	default:
2259 		error = ieee80211_ioctl(ic, cmd, data);
2260 	}
2261 
2262 	if (error == ENETRESET) {
2263 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2264 		    (IFF_UP | IFF_RUNNING))
2265 			rt2560_init(ifp);
2266 		error = 0;
2267 	}
2268 
2269 	splx(s);
2270 
2271 	return error;
2272 }
2273 
2274 static void
2275 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2276 {
2277 	uint32_t tmp;
2278 	int ntries;
2279 
2280 	for (ntries = 0; ntries < 100; ntries++) {
2281 		if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2282 			break;
2283 		DELAY(1);
2284 	}
2285 	if (ntries == 100) {
2286 		aprint_error_dev(sc->sc_dev, "could not write to BBP\n");
2287 		return;
2288 	}
2289 
2290 	tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2291 	RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2292 
2293 	DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2294 }
2295 
2296 static uint8_t
2297 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2298 {
2299 	uint32_t val;
2300 	int ntries;
2301 
2302 	val = RT2560_BBP_BUSY | reg << 8;
2303 	RAL_WRITE(sc, RT2560_BBPCSR, val);
2304 
2305 	for (ntries = 0; ntries < 100; ntries++) {
2306 		val = RAL_READ(sc, RT2560_BBPCSR);
2307 		if (!(val & RT2560_BBP_BUSY))
2308 			return val & 0xff;
2309 		DELAY(1);
2310 	}
2311 
2312 	aprint_error_dev(sc->sc_dev, "could not read from BBP\n");
2313 	return 0;
2314 }
2315 
2316 static void
2317 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2318 {
2319 	uint32_t tmp;
2320 	int ntries;
2321 
2322 	for (ntries = 0; ntries < 100; ntries++) {
2323 		if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2324 			break;
2325 		DELAY(1);
2326 	}
2327 	if (ntries == 100) {
2328 		aprint_error_dev(sc->sc_dev, "could not write to RF\n");
2329 		return;
2330 	}
2331 
2332 	tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2333 	    (reg & 0x3);
2334 	RAL_WRITE(sc, RT2560_RFCSR, tmp);
2335 
2336 	/* remember last written value in sc */
2337 	sc->rf_regs[reg] = val;
2338 
2339 	DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2340 }
2341 
2342 static void
2343 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2344 {
2345 	struct ieee80211com *ic = &sc->sc_ic;
2346 	uint8_t power, tmp;
2347 	u_int i, chan;
2348 
2349 	chan = ieee80211_chan2ieee(ic, c);
2350 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2351 		return;
2352 
2353 	if (IEEE80211_IS_CHAN_2GHZ(c))
2354 		power = min(sc->txpow[chan - 1], 31);
2355 	else
2356 		power = 31;
2357 
2358 	DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2359 
2360 	switch (sc->rf_rev) {
2361 	case RT2560_RF_2522:
2362 		rt2560_rf_write(sc, RT2560_RF1, 0x00814);
2363 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2522_r2[chan - 1]);
2364 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
2365 		break;
2366 
2367 	case RT2560_RF_2523:
2368 		rt2560_rf_write(sc, RT2560_RF1, 0x08804);
2369 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2523_r2[chan - 1]);
2370 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x38044);
2371 		rt2560_rf_write(sc, RT2560_RF4,
2372 		    (chan == 14) ? 0x00280 : 0x00286);
2373 		break;
2374 
2375 	case RT2560_RF_2524:
2376 		rt2560_rf_write(sc, RT2560_RF1, 0x0c808);
2377 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2524_r2[chan - 1]);
2378 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
2379 		rt2560_rf_write(sc, RT2560_RF4,
2380 		    (chan == 14) ? 0x00280 : 0x00286);
2381 		break;
2382 
2383 	case RT2560_RF_2525:
2384 		rt2560_rf_write(sc, RT2560_RF1, 0x08808);
2385 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2386 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2387 		rt2560_rf_write(sc, RT2560_RF4,
2388 		    (chan == 14) ? 0x00280 : 0x00286);
2389 
2390 		rt2560_rf_write(sc, RT2560_RF1, 0x08808);
2391 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525_r2[chan - 1]);
2392 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2393 		rt2560_rf_write(sc, RT2560_RF4,
2394 		    (chan == 14) ? 0x00280 : 0x00286);
2395 		break;
2396 
2397 	case RT2560_RF_2525E:
2398 		rt2560_rf_write(sc, RT2560_RF1, 0x08808);
2399 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2525e_r2[chan - 1]);
2400 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2401 		rt2560_rf_write(sc, RT2560_RF4,
2402 		    (chan == 14) ? 0x00286 : 0x00282);
2403 		break;
2404 
2405 	case RT2560_RF_2526:
2406 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2407 		rt2560_rf_write(sc, RT2560_RF4,
2408 		   (chan & 1) ? 0x00386 : 0x00381);
2409 		rt2560_rf_write(sc, RT2560_RF1, 0x08804);
2410 
2411 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf2526_r2[chan - 1]);
2412 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x18044);
2413 		rt2560_rf_write(sc, RT2560_RF4,
2414 		    (chan & 1) ? 0x00386 : 0x00381);
2415 		break;
2416 
2417 	/* dual-band RF */
2418 	case RT2560_RF_5222:
2419 		for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2420 
2421 		rt2560_rf_write(sc, RT2560_RF1, rt2560_rf5222[i].r1);
2422 		rt2560_rf_write(sc, RT2560_RF2, rt2560_rf5222[i].r2);
2423 		rt2560_rf_write(sc, RT2560_RF3, power << 7 | 0x00040);
2424 		rt2560_rf_write(sc, RT2560_RF4, rt2560_rf5222[i].r4);
2425 		break;
2426 	}
2427 
2428 	if (ic->ic_opmode != IEEE80211_M_MONITOR &&
2429 	    ic->ic_state != IEEE80211_S_SCAN) {
2430 		/* set Japan filter bit for channel 14 */
2431 		tmp = rt2560_bbp_read(sc, 70);
2432 
2433 		tmp &= ~RT2560_JAPAN_FILTER;
2434 		if (chan == 14)
2435 			tmp |= RT2560_JAPAN_FILTER;
2436 
2437 		rt2560_bbp_write(sc, 70, tmp);
2438 
2439 		DELAY(1000); /* RF needs a 1ms delay here */
2440 		rt2560_disable_rf_tune(sc);
2441 
2442 		/* clear CRC errors */
2443 		RAL_READ(sc, RT2560_CNT0);
2444 	}
2445 }
2446 
2447 /*
2448  * Disable RF auto-tuning.
2449  */
2450 static void
2451 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2452 {
2453 	uint32_t tmp;
2454 
2455 	if (sc->rf_rev != RT2560_RF_2523) {
2456 		tmp = sc->rf_regs[RT2560_RF1] & ~RT2560_RF1_AUTOTUNE;
2457 		rt2560_rf_write(sc, RT2560_RF1, tmp);
2458 	}
2459 
2460 	tmp = sc->rf_regs[RT2560_RF3] & ~RT2560_RF3_AUTOTUNE;
2461 	rt2560_rf_write(sc, RT2560_RF3, tmp);
2462 
2463 	DPRINTFN(2, ("disabling RF autotune\n"));
2464 }
2465 
2466 /*
2467  * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2468  * synchronization.
2469  */
2470 static void
2471 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2472 {
2473 	struct ieee80211com *ic = &sc->sc_ic;
2474 	uint16_t logcwmin, preload;
2475 	uint32_t tmp;
2476 
2477 	/* first, disable TSF synchronization */
2478 	RAL_WRITE(sc, RT2560_CSR14, 0);
2479 
2480 	tmp = 16 * ic->ic_bss->ni_intval;
2481 	RAL_WRITE(sc, RT2560_CSR12, tmp);
2482 
2483 	RAL_WRITE(sc, RT2560_CSR13, 0);
2484 
2485 	logcwmin = 5;
2486 	preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2487 	tmp = logcwmin << 16 | preload;
2488 	RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2489 
2490 	/* finally, enable TSF synchronization */
2491 	tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2492 	if (ic->ic_opmode == IEEE80211_M_STA)
2493 		tmp |= RT2560_ENABLE_TSF_SYNC(1);
2494 	else
2495 		tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2496 		       RT2560_ENABLE_BEACON_GENERATOR;
2497 	RAL_WRITE(sc, RT2560_CSR14, tmp);
2498 
2499 	DPRINTF(("enabling TSF synchronization\n"));
2500 }
2501 
2502 static void
2503 rt2560_update_plcp(struct rt2560_softc *sc)
2504 {
2505 	struct ieee80211com *ic = &sc->sc_ic;
2506 
2507 	/* no short preamble for 1Mbps */
2508 	RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2509 
2510 	if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2511 		/* values taken from the reference driver */
2512 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380401);
2513 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2514 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b8403);
2515 	} else {
2516 		/* same values as above or'ed 0x8 */
2517 		RAL_WRITE(sc, RT2560_PLCP2MCSR,   0x00380409);
2518 		RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2519 		RAL_WRITE(sc, RT2560_PLCP11MCSR,  0x000b840b);
2520 	}
2521 
2522 	DPRINTF(("updating PLCP for %s preamble\n",
2523 	    (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2524 }
2525 
2526 /*
2527  * IEEE 802.11a uses short slot time. Refer to IEEE Std 802.11-1999 pp. 85 to
2528  * know how these values are computed.
2529  */
2530 static void
2531 rt2560_update_slot(struct ifnet *ifp)
2532 {
2533 	struct rt2560_softc *sc = ifp->if_softc;
2534 	struct ieee80211com *ic = &sc->sc_ic;
2535 	uint8_t slottime;
2536 	uint16_t sifs, pifs, difs, eifs;
2537 	uint32_t tmp;
2538 
2539 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2540 
2541 	/* define the MAC slot boundaries */
2542 	sifs = RAL_SIFS - RT2560_RXTX_TURNAROUND;
2543 	pifs = sifs + slottime;
2544 	difs = sifs + 2 * slottime;
2545 	eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2546 
2547 	tmp = RAL_READ(sc, RT2560_CSR11);
2548 	tmp = (tmp & ~0x1f00) | slottime << 8;
2549 	RAL_WRITE(sc, RT2560_CSR11, tmp);
2550 
2551 	tmp = pifs << 16 | sifs;
2552 	RAL_WRITE(sc, RT2560_CSR18, tmp);
2553 
2554 	tmp = eifs << 16 | difs;
2555 	RAL_WRITE(sc, RT2560_CSR19, tmp);
2556 
2557 	DPRINTF(("setting slottime to %uus\n", slottime));
2558 }
2559 
2560 static void
2561 rt2560_set_basicrates(struct rt2560_softc *sc)
2562 {
2563 	struct ieee80211com *ic = &sc->sc_ic;
2564 
2565 	/* update basic rate set */
2566 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
2567 		/* 11b basic rates: 1, 2Mbps */
2568 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2569 	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
2570 		/* 11a basic rates: 6, 12, 24Mbps */
2571 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2572 	} else {
2573 		/* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2574 		RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2575 	}
2576 }
2577 
2578 static void
2579 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2580 {
2581 	uint32_t tmp;
2582 
2583 	/* set ON period to 70ms and OFF period to 30ms */
2584 	tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2585 	RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2586 }
2587 
2588 static void
2589 rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
2590 {
2591 	uint32_t tmp;
2592 
2593 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2594 	RAL_WRITE(sc, RT2560_CSR5, tmp);
2595 
2596 	tmp = bssid[4] | bssid[5] << 8;
2597 	RAL_WRITE(sc, RT2560_CSR6, tmp);
2598 
2599 	DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid)));
2600 }
2601 
2602 static void
2603 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2604 {
2605 	uint32_t tmp;
2606 
2607 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2608 	RAL_WRITE(sc, RT2560_CSR3, tmp);
2609 
2610 	tmp = addr[4] | addr[5] << 8;
2611 	RAL_WRITE(sc, RT2560_CSR4, tmp);
2612 
2613 	DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr)));
2614 }
2615 
2616 static void
2617 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2618 {
2619 	uint32_t tmp;
2620 
2621 	tmp = RAL_READ(sc, RT2560_CSR3);
2622 	addr[0] = tmp & 0xff;
2623 	addr[1] = (tmp >>  8) & 0xff;
2624 	addr[2] = (tmp >> 16) & 0xff;
2625 	addr[3] = (tmp >> 24);
2626 
2627 	tmp = RAL_READ(sc, RT2560_CSR4);
2628 	addr[4] = tmp & 0xff;
2629 	addr[5] = (tmp >> 8) & 0xff;
2630 }
2631 
2632 static void
2633 rt2560_update_promisc(struct rt2560_softc *sc)
2634 {
2635 	struct ifnet *ifp = &sc->sc_if;
2636 	uint32_t tmp;
2637 
2638 	tmp = RAL_READ(sc, RT2560_RXCSR0);
2639 
2640 	tmp &= ~RT2560_DROP_NOT_TO_ME;
2641 	if (!(ifp->if_flags & IFF_PROMISC))
2642 		tmp |= RT2560_DROP_NOT_TO_ME;
2643 
2644 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2645 
2646 	DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2647 	    "entering" : "leaving"));
2648 }
2649 
2650 static void
2651 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2652 {
2653 	uint32_t tmp;
2654 	uint8_t tx;
2655 
2656 	tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2657 	if (antenna == 1)
2658 		tx |= RT2560_BBP_ANTA;
2659 	else if (antenna == 2)
2660 		tx |= RT2560_BBP_ANTB;
2661 	else
2662 		tx |= RT2560_BBP_DIVERSITY;
2663 
2664 	/* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2665 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2666 	    sc->rf_rev == RT2560_RF_5222)
2667 		tx |= RT2560_BBP_FLIPIQ;
2668 
2669 	rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2670 
2671 	/* update values for CCK and OFDM in BBPCSR1 */
2672 	tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2673 	tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2674 	RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2675 }
2676 
2677 static void
2678 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2679 {
2680 	uint8_t rx;
2681 
2682 	rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2683 	if (antenna == 1)
2684 		rx |= RT2560_BBP_ANTA;
2685 	else if (antenna == 2)
2686 		rx |= RT2560_BBP_ANTB;
2687 	else
2688 		rx |= RT2560_BBP_DIVERSITY;
2689 
2690 	/* need to force no I/Q flip for RF 2525e and 2526 */
2691 	if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2692 		rx &= ~RT2560_BBP_FLIPIQ;
2693 
2694 	rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2695 }
2696 
2697 static const char *
2698 rt2560_get_rf(int rev)
2699 {
2700 	switch (rev) {
2701 	case RT2560_RF_2522:	return "RT2522";
2702 	case RT2560_RF_2523:	return "RT2523";
2703 	case RT2560_RF_2524:	return "RT2524";
2704 	case RT2560_RF_2525:	return "RT2525";
2705 	case RT2560_RF_2525E:	return "RT2525e";
2706 	case RT2560_RF_2526:	return "RT2526";
2707 	case RT2560_RF_5222:	return "RT5222";
2708 	default:		return "unknown";
2709 	}
2710 }
2711 
2712 static void
2713 rt2560_read_eeprom(struct rt2560_softc *sc)
2714 {
2715 	uint16_t val;
2716 	int i;
2717 
2718 	val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2719 	sc->rf_rev =   (val >> 11) & 0x1f;
2720 	sc->hw_radio = (val >> 10) & 0x1;
2721 	sc->led_mode = (val >> 6)  & 0x7;
2722 	sc->rx_ant =   (val >> 4)  & 0x3;
2723 	sc->tx_ant =   (val >> 2)  & 0x3;
2724 	sc->nb_ant =   val & 0x3;
2725 
2726 	/* read default values for BBP registers */
2727 	for (i = 0; i < 16; i++) {
2728 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2729 		sc->bbp_prom[i].reg = val >> 8;
2730 		sc->bbp_prom[i].val = val & 0xff;
2731 	}
2732 
2733 	/* read Tx power for all b/g channels */
2734 	for (i = 0; i < 14 / 2; i++) {
2735 		val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2736 		sc->txpow[i * 2] = val >> 8;
2737 		sc->txpow[i * 2 + 1] = val & 0xff;
2738 	}
2739 }
2740 
2741 static int
2742 rt2560_bbp_init(struct rt2560_softc *sc)
2743 {
2744 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2745 	int i, ntries;
2746 
2747 	/* wait for BBP to be ready */
2748 	for (ntries = 0; ntries < 100; ntries++) {
2749 		if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2750 			break;
2751 		DELAY(1);
2752 	}
2753 	if (ntries == 100) {
2754 		aprint_error_dev(sc->sc_dev, "timeout waiting for BBP\n");
2755 		return EIO;
2756 	}
2757 
2758 	/* initialize BBP registers to default values */
2759 	for (i = 0; i < N(rt2560_def_bbp); i++) {
2760 		rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2761 		    rt2560_def_bbp[i].val);
2762 	}
2763 #if 0
2764 	/* initialize BBP registers to values stored in EEPROM */
2765 	for (i = 0; i < 16; i++) {
2766 		if (sc->bbp_prom[i].reg == 0xff)
2767 			continue;
2768 		rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2769 	}
2770 #endif
2771 
2772 	return 0;
2773 #undef N
2774 }
2775 
2776 static int
2777 rt2560_init(struct ifnet *ifp)
2778 {
2779 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
2780 	struct rt2560_softc *sc = ifp->if_softc;
2781 	struct ieee80211com *ic = &sc->sc_ic;
2782 	uint32_t tmp;
2783 	int i;
2784 
2785 	/* for CardBus, power on the socket */
2786 	if (!(sc->sc_flags & RT2560_ENABLED)) {
2787 		if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
2788 			aprint_error_dev(sc->sc_dev, "could not enable device\n");
2789 			return EIO;
2790 		}
2791 		sc->sc_flags |= RT2560_ENABLED;
2792 	}
2793 
2794 	rt2560_stop(ifp, 1);
2795 
2796 	/* setup tx rings */
2797 	tmp = RT2560_PRIO_RING_COUNT << 24 |
2798 	      RT2560_ATIM_RING_COUNT << 16 |
2799 	      RT2560_TX_RING_COUNT   <<  8 |
2800 	      RT2560_TX_DESC_SIZE;
2801 
2802 	/* rings _must_ be initialized in this _exact_ order! */
2803 	RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2804 	RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2805 	RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2806 	RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2807 	RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2808 
2809 	/* setup rx ring */
2810 	tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2811 
2812 	RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2813 	RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2814 
2815 	/* initialize MAC registers to default values */
2816 	for (i = 0; i < N(rt2560_def_mac); i++)
2817 		RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2818 
2819 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2820 	rt2560_set_macaddr(sc, ic->ic_myaddr);
2821 
2822 	/* set basic rate set (will be updated later) */
2823 	RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2824 
2825 	rt2560_update_slot(ifp);
2826 	rt2560_update_plcp(sc);
2827 	rt2560_update_led(sc, 0, 0);
2828 
2829 	RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2830 	RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2831 
2832 	if (rt2560_bbp_init(sc) != 0) {
2833 		rt2560_stop(ifp, 1);
2834 		return EIO;
2835 	}
2836 
2837 	rt2560_set_txantenna(sc, 1);
2838 	rt2560_set_rxantenna(sc, 1);
2839 
2840 	/* set default BSS channel */
2841 	ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2842 	rt2560_set_chan(sc, ic->ic_bss->ni_chan);
2843 
2844 	/* kick Rx */
2845 	tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2846 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2847 		tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2848 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2849 			tmp |= RT2560_DROP_TODS;
2850 		if (!(ifp->if_flags & IFF_PROMISC))
2851 			tmp |= RT2560_DROP_NOT_TO_ME;
2852 	}
2853 	RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2854 
2855 	/* clear old FCS and Rx FIFO errors */
2856 	RAL_READ(sc, RT2560_CNT0);
2857 	RAL_READ(sc, RT2560_CNT4);
2858 
2859 	/* clear any pending interrupts */
2860 	RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2861 
2862 	/* enable interrupts */
2863 	RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2864 
2865 	ifp->if_flags &= ~IFF_OACTIVE;
2866 	ifp->if_flags |= IFF_RUNNING;
2867 
2868 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2869 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2870 	else
2871 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2872 
2873 	return 0;
2874 #undef N
2875 }
2876 
2877 static void
2878 rt2560_stop(struct ifnet *ifp, int disable)
2879 {
2880 	struct rt2560_softc *sc = ifp->if_softc;
2881 	struct ieee80211com *ic = &sc->sc_ic;
2882 
2883 	sc->sc_tx_timer = 0;
2884 	ifp->if_timer = 0;
2885 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2886 
2887 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
2888 
2889 	/* abort Tx */
2890 	RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2891 
2892 	/* disable Rx */
2893 	RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2894 
2895 	/* reset ASIC (and thus, BBP) */
2896 	RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2897 	RAL_WRITE(sc, RT2560_CSR1, 0);
2898 
2899 	/* disable interrupts */
2900 	RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2901 
2902 	/* clear any pending interrupt */
2903 	RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2904 
2905 	/* reset Tx and Rx rings */
2906 	rt2560_reset_tx_ring(sc, &sc->txq);
2907 	rt2560_reset_tx_ring(sc, &sc->atimq);
2908 	rt2560_reset_tx_ring(sc, &sc->prioq);
2909 	rt2560_reset_tx_ring(sc, &sc->bcnq);
2910 	rt2560_reset_rx_ring(sc, &sc->rxq);
2911 }
2912